hdaps.c 16.7 KB
Newer Older
R
Robert Love 已提交
1
/*
J
Jean Delvare 已提交
2
 * hdaps.c - driver for IBM's Hard Drive Active Protection System
R
Robert Love 已提交
3 4 5 6
 *
 * Copyright (C) 2005 Robert Love <rml@novell.com>
 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
 *
R
Robert Love 已提交
7 8 9
 * The HardDisk Active Protection System (hdaps) is present in IBM ThinkPads
 * starting with the R40, T41, and X40.  It provides a basic two-axis
 * accelerometer and other data, such as the device's temperature.
R
Robert Love 已提交
10
 *
R
Robert Love 已提交
11
 * This driver is based on the document by Mark A. Smith available at
R
Robert Love 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html and a lot of trial
 * and error.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License v2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

29 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

R
Robert Love 已提交
31
#include <linux/delay.h>
32
#include <linux/platform_device.h>
33
#include <linux/input-polldev.h>
R
Robert Love 已提交
34
#include <linux/kernel.h>
35
#include <linux/mutex.h>
R
Robert Love 已提交
36 37 38
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/dmi.h>
39
#include <linux/jiffies.h>
40
#include <linux/io.h>
R
Robert Love 已提交
41 42

#define HDAPS_LOW_PORT		0x1600	/* first port used by hdaps */
R
Robert Love 已提交
43
#define HDAPS_NR_PORTS		0x30	/* number of ports: 0x1600 - 0x162f */
R
Robert Love 已提交
44 45 46 47

#define HDAPS_PORT_STATE	0x1611	/* device state */
#define HDAPS_PORT_YPOS		0x1612	/* y-axis position */
#define	HDAPS_PORT_XPOS		0x1614	/* x-axis position */
48
#define HDAPS_PORT_TEMP1	0x1616	/* device temperature, in Celsius */
R
Robert Love 已提交
49 50 51 52 53 54
#define HDAPS_PORT_YVAR		0x1617	/* y-axis variance (what is this?) */
#define HDAPS_PORT_XVAR		0x1619	/* x-axis variance (what is this?) */
#define HDAPS_PORT_TEMP2	0x161b	/* device temperature (again?) */
#define HDAPS_PORT_UNKNOWN	0x161c	/* what is this? */
#define HDAPS_PORT_KMACT	0x161d	/* keyboard or mouse activity */

R
Robert Love 已提交
55
#define STATE_FRESH		0x50	/* accelerometer data is fresh */
R
Robert Love 已提交
56 57 58 59 60 61 62 63 64

#define KEYBD_MASK		0x20	/* set if keyboard activity */
#define MOUSE_MASK		0x40	/* set if mouse activity */
#define KEYBD_ISSET(n)		(!! (n & KEYBD_MASK))	/* keyboard used? */
#define MOUSE_ISSET(n)		(!! (n & MOUSE_MASK))	/* mouse used? */

#define INIT_TIMEOUT_MSECS	4000	/* wait up to 4s for device init ... */
#define INIT_WAIT_MSECS		200	/* ... in 200ms increments */

65
#define HDAPS_POLL_INTERVAL	50	/* poll for input every 1/20s (50 ms)*/
R
Robert Love 已提交
66
#define HDAPS_INPUT_FUZZ	4	/* input event threshold */
67
#define HDAPS_INPUT_FLAT	4
R
Robert Love 已提交
68

69 70 71 72
#define HDAPS_X_AXIS		(1 << 0)
#define HDAPS_Y_AXIS		(1 << 1)
#define HDAPS_BOTH_AXES		(HDAPS_X_AXIS | HDAPS_Y_AXIS)

R
Robert Love 已提交
73
static struct platform_device *pdev;
74
static struct input_polled_dev *hdaps_idev;
R
Robert Love 已提交
75 76 77 78 79
static unsigned int hdaps_invert;
static u8 km_activity;
static int rest_x;
static int rest_y;

80
static DEFINE_MUTEX(hdaps_mtx);
R
Robert Love 已提交
81 82

/*
83
 * __get_latch - Get the value from a given port.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
84 85 86
 */
static inline u8 __get_latch(u16 port)
{
R
Robert Love 已提交
87
	return inb(port) & 0xff;
R
Robert Love 已提交
88 89 90
}

/*
R
Robert Love 已提交
91
 * __check_latch - Check a port latch for a given value.  Returns zero if the
92
 * port contains the given value.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
93
 */
R
Robert Love 已提交
94
static inline int __check_latch(u16 port, u8 val)
R
Robert Love 已提交
95 96 97 98 99 100 101 102
{
	if (__get_latch(port) == val)
		return 0;
	return -EINVAL;
}

/*
 * __wait_latch - Wait up to 100us for a port latch to get a certain value,
103
 * returning zero if the value is obtained.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
104
 */
R
Robert Love 已提交
105
static int __wait_latch(u16 port, u8 val)
R
Robert Love 已提交
106 107 108 109 110 111 112 113 114
{
	unsigned int i;

	for (i = 0; i < 20; i++) {
		if (!__check_latch(port, val))
			return 0;
		udelay(5);
	}

R
Robert Love 已提交
115
	return -EIO;
R
Robert Love 已提交
116 117 118
}

/*
R
Robert Love 已提交
119
 * __device_refresh - request a refresh from the accelerometer.  Does not wait
120
 * for refresh to complete.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
121
 */
R
Robert Love 已提交
122
static void __device_refresh(void)
R
Robert Love 已提交
123
{
R
Robert Love 已提交
124 125 126 127 128 129
	udelay(200);
	if (inb(0x1604) != STATE_FRESH) {
		outb(0x11, 0x1610);
		outb(0x01, 0x161f);
	}
}
R
Robert Love 已提交
130

R
Robert Love 已提交
131 132 133
/*
 * __device_refresh_sync - request a synchronous refresh from the
 * accelerometer.  We wait for the refresh to complete.  Returns zero if
134
 * successful and nonzero on error.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
135 136 137 138
 */
static int __device_refresh_sync(void)
{
	__device_refresh();
R
Robert Love 已提交
139 140 141 142
	return __wait_latch(0x1604, STATE_FRESH);
}

/*
R
Robert Love 已提交
143
 * __device_complete - indicate to the accelerometer that we are done reading
144
 * data, and then initiate an async refresh.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
145 146 147 148 149
 */
static inline void __device_complete(void)
{
	inb(0x161f);
	inb(0x1604);
R
Robert Love 已提交
150
	__device_refresh();
R
Robert Love 已提交
151 152 153 154 155 156 157 158 159 160 161
}

/*
 * hdaps_readb_one - reads a byte from a single I/O port, placing the value in
 * the given pointer.  Returns zero on success or a negative error on failure.
 * Can sleep.
 */
static int hdaps_readb_one(unsigned int port, u8 *val)
{
	int ret;

162
	mutex_lock(&hdaps_mtx);
R
Robert Love 已提交
163

R
Robert Love 已提交
164 165 166 167 168 169 170 171 172
	/* do a sync refresh -- we need to be sure that we read fresh data */
	ret = __device_refresh_sync();
	if (ret)
		goto out;

	*val = inb(port);
	__device_complete();

out:
173
	mutex_unlock(&hdaps_mtx);
R
Robert Love 已提交
174 175 176
	return ret;
}

R
Robert Love 已提交
177
/* __hdaps_read_pair - internal lockless helper for hdaps_read_pair(). */
R
Robert Love 已提交
178 179 180 181
static int __hdaps_read_pair(unsigned int port1, unsigned int port2,
			     int *x, int *y)
{
	/* do a sync refresh -- we need to be sure that we read fresh data */
R
Robert Love 已提交
182
	if (__device_refresh_sync())
R
Robert Love 已提交
183 184 185 186 187 188 189
		return -EIO;

	*y = inw(port2);
	*x = inw(port1);
	km_activity = inb(HDAPS_PORT_KMACT);
	__device_complete();

190 191
	/* hdaps_invert is a bitvector to negate the axes */
	if (hdaps_invert & HDAPS_X_AXIS)
R
Robert Love 已提交
192
		*x = -*x;
193
	if (hdaps_invert & HDAPS_Y_AXIS)
R
Robert Love 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
		*y = -*y;

	return 0;
}

/*
 * hdaps_read_pair - reads the values from a pair of ports, placing the values
 * in the given pointers.  Returns zero on success.  Can sleep.
 */
static int hdaps_read_pair(unsigned int port1, unsigned int port2,
			   int *val1, int *val2)
{
	int ret;

208
	mutex_lock(&hdaps_mtx);
R
Robert Love 已提交
209
	ret = __hdaps_read_pair(port1, port2, val1, val2);
210
	mutex_unlock(&hdaps_mtx);
R
Robert Love 已提交
211 212 213 214

	return ret;
}

R
Robert Love 已提交
215 216 217 218
/*
 * hdaps_device_init - initialize the accelerometer.  Returns zero on success
 * and negative error code on failure.  Can sleep.
 */
R
Robert Love 已提交
219 220
static int hdaps_device_init(void)
{
R
Robert Love 已提交
221
	int total, ret = -ENXIO;
R
Robert Love 已提交
222

223
	mutex_lock(&hdaps_mtx);
R
Robert Love 已提交
224 225 226 227 228 229 230

	outb(0x13, 0x1610);
	outb(0x01, 0x161f);
	if (__wait_latch(0x161f, 0x00))
		goto out;

	/*
R
Robert Love 已提交
231 232 233 234
	 * Most ThinkPads return 0x01.
	 *
	 * Others--namely the R50p, T41p, and T42p--return 0x03.  These laptops
	 * have "inverted" axises.
R
Robert Love 已提交
235 236 237 238 239 240 241 242
	 *
	 * The 0x02 value occurs when the chip has been previously initialized.
	 */
	if (__check_latch(0x1611, 0x03) &&
		     __check_latch(0x1611, 0x02) &&
		     __check_latch(0x1611, 0x01))
		goto out;

243
	printk(KERN_DEBUG "hdaps: initial latch check good (0x%02x)\n",
R
Robert Love 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	       __get_latch(0x1611));

	outb(0x17, 0x1610);
	outb(0x81, 0x1611);
	outb(0x01, 0x161f);
	if (__wait_latch(0x161f, 0x00))
		goto out;
	if (__wait_latch(0x1611, 0x00))
		goto out;
	if (__wait_latch(0x1612, 0x60))
		goto out;
	if (__wait_latch(0x1613, 0x00))
		goto out;
	outb(0x14, 0x1610);
	outb(0x01, 0x1611);
	outb(0x01, 0x161f);
	if (__wait_latch(0x161f, 0x00))
		goto out;
	outb(0x10, 0x1610);
	outb(0xc8, 0x1611);
	outb(0x00, 0x1612);
	outb(0x02, 0x1613);
	outb(0x01, 0x161f);
	if (__wait_latch(0x161f, 0x00))
		goto out;
R
Robert Love 已提交
269
	if (__device_refresh_sync())
R
Robert Love 已提交
270 271 272 273 274
		goto out;
	if (__wait_latch(0x1611, 0x00))
		goto out;

	/* we have done our dance, now let's wait for the applause */
R
Robert Love 已提交
275 276
	for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
		int x, y;
R
Robert Love 已提交
277 278

		/* a read of the device helps push it into action */
R
Robert Love 已提交
279
		__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y);
R
Robert Love 已提交
280 281 282 283 284 285 286 287 288
		if (!__wait_latch(0x1611, 0x02)) {
			ret = 0;
			break;
		}

		msleep(INIT_WAIT_MSECS);
	}

out:
289
	mutex_unlock(&hdaps_mtx);
R
Robert Love 已提交
290 291 292 293 294 295
	return ret;
}


/* Device model stuff */

296
static int hdaps_probe(struct platform_device *dev)
R
Robert Love 已提交
297 298 299 300 301 302 303
{
	int ret;

	ret = hdaps_device_init();
	if (ret)
		return ret;

304
	pr_info("device successfully initialized\n");
R
Robert Love 已提交
305 306 307
	return 0;
}

308
static int hdaps_resume(struct platform_device *dev)
R
Robert Love 已提交
309
{
310
	return hdaps_device_init();
R
Robert Love 已提交
311 312
}

313
static struct platform_driver hdaps_driver = {
R
Robert Love 已提交
314
	.probe = hdaps_probe,
315 316 317 318 319
	.resume = hdaps_resume,
	.driver	= {
		.name = "hdaps",
		.owner = THIS_MODULE,
	},
R
Robert Love 已提交
320 321
};

R
Robert Love 已提交
322
/*
323
 * hdaps_calibrate - Set our "resting" values.  Callers must hold hdaps_mtx.
R
Robert Love 已提交
324 325 326 327 328 329
 */
static void hdaps_calibrate(void)
{
	__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &rest_x, &rest_y);
}

330
static void hdaps_mousedev_poll(struct input_polled_dev *dev)
R
Robert Love 已提交
331
{
332
	struct input_dev *input_dev = dev->input;
R
Robert Love 已提交
333 334
	int x, y;

335
	mutex_lock(&hdaps_mtx);
R
Robert Love 已提交
336 337 338 339

	if (__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y))
		goto out;

340 341 342
	input_report_abs(input_dev, ABS_X, x - rest_x);
	input_report_abs(input_dev, ABS_Y, y - rest_y);
	input_sync(input_dev);
R
Robert Love 已提交
343 344

out:
345
	mutex_unlock(&hdaps_mtx);
R
Robert Love 已提交
346 347
}

R
Robert Love 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

/* Sysfs Files */

static ssize_t hdaps_position_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	int ret, x, y;

	ret = hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y);
	if (ret)
		return ret;

	return sprintf(buf, "(%d,%d)\n", x, y);
}

static ssize_t hdaps_variance_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	int ret, x, y;

	ret = hdaps_read_pair(HDAPS_PORT_XVAR, HDAPS_PORT_YVAR, &x, &y);
	if (ret)
		return ret;

	return sprintf(buf, "(%d,%d)\n", x, y);
}

static ssize_t hdaps_temp1_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
378
	u8 uninitialized_var(temp);
R
Robert Love 已提交
379 380 381
	int ret;

	ret = hdaps_readb_one(HDAPS_PORT_TEMP1, &temp);
382
	if (ret)
R
Robert Love 已提交
383 384 385 386 387 388 389 390
		return ret;

	return sprintf(buf, "%u\n", temp);
}

static ssize_t hdaps_temp2_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
391
	u8 uninitialized_var(temp);
R
Robert Love 已提交
392 393 394
	int ret;

	ret = hdaps_readb_one(HDAPS_PORT_TEMP2, &temp);
395
	if (ret)
R
Robert Love 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
		return ret;

	return sprintf(buf, "%u\n", temp);
}

static ssize_t hdaps_keyboard_activity_show(struct device *dev,
					    struct device_attribute *attr,
					    char *buf)
{
	return sprintf(buf, "%u\n", KEYBD_ISSET(km_activity));
}

static ssize_t hdaps_mouse_activity_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	return sprintf(buf, "%u\n", MOUSE_ISSET(km_activity));
}

static ssize_t hdaps_calibrate_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "(%d,%d)\n", rest_x, rest_y);
}

static ssize_t hdaps_calibrate_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
425
	mutex_lock(&hdaps_mtx);
R
Robert Love 已提交
426
	hdaps_calibrate();
427
	mutex_unlock(&hdaps_mtx);
R
Robert Love 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

	return count;
}

static ssize_t hdaps_invert_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "%u\n", hdaps_invert);
}

static ssize_t hdaps_invert_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	int invert;

444 445
	if (sscanf(buf, "%d", &invert) != 1 ||
	    invert < 0 || invert > HDAPS_BOTH_AXES)
R
Robert Love 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
		return -EINVAL;

	hdaps_invert = invert;
	hdaps_calibrate();

	return count;
}

static DEVICE_ATTR(position, 0444, hdaps_position_show, NULL);
static DEVICE_ATTR(variance, 0444, hdaps_variance_show, NULL);
static DEVICE_ATTR(temp1, 0444, hdaps_temp1_show, NULL);
static DEVICE_ATTR(temp2, 0444, hdaps_temp2_show, NULL);
static DEVICE_ATTR(keyboard_activity, 0444, hdaps_keyboard_activity_show, NULL);
static DEVICE_ATTR(mouse_activity, 0444, hdaps_mouse_activity_show, NULL);
static DEVICE_ATTR(calibrate, 0644, hdaps_calibrate_show,hdaps_calibrate_store);
static DEVICE_ATTR(invert, 0644, hdaps_invert_show, hdaps_invert_store);

static struct attribute *hdaps_attributes[] = {
	&dev_attr_position.attr,
	&dev_attr_variance.attr,
	&dev_attr_temp1.attr,
	&dev_attr_temp2.attr,
	&dev_attr_keyboard_activity.attr,
	&dev_attr_mouse_activity.attr,
	&dev_attr_calibrate.attr,
	&dev_attr_invert.attr,
	NULL,
};

static struct attribute_group hdaps_attribute_group = {
	.attrs = hdaps_attributes,
};


/* Module stuff */

R
Robert Love 已提交
482
/* hdaps_dmi_match - found a match.  return one, short-circuiting the hunt. */
483
static int __init hdaps_dmi_match(const struct dmi_system_id *id)
R
Robert Love 已提交
484
{
485
	pr_info("%s detected\n", id->ident);
R
Robert Love 已提交
486
	return 1;
R
Robert Love 已提交
487 488
}

R
Robert Love 已提交
489
/* hdaps_dmi_match_invert - found an inverted match. */
490
static int __init hdaps_dmi_match_invert(const struct dmi_system_id *id)
R
Robert Love 已提交
491
{
492
	hdaps_invert = (unsigned long)id->driver_data;
493
	pr_info("inverting axis (%u) readings\n", hdaps_invert);
R
Robert Love 已提交
494
	return hdaps_dmi_match(id);
R
Robert Love 已提交
495 496
}

497
#define HDAPS_DMI_MATCH_INVERT(vendor, model, axes) {	\
498
	.ident = vendor " " model,			\
R
Robert Love 已提交
499
	.callback = hdaps_dmi_match_invert,		\
500
	.driver_data = (void *)axes,			\
R
Robert Love 已提交
501
	.matches = {					\
502
		DMI_MATCH(DMI_BOARD_VENDOR, vendor),	\
R
Robert Love 已提交
503 504 505 506
		DMI_MATCH(DMI_PRODUCT_VERSION, model)	\
	}						\
}

507 508 509
#define HDAPS_DMI_MATCH_NORMAL(vendor, model)		\
	HDAPS_DMI_MATCH_INVERT(vendor, model, 0)

510
/* Note that HDAPS_DMI_MATCH_NORMAL("ThinkPad T42") would match
511 512 513
   "ThinkPad T42p", so the order of the entries matters.
   If your ThinkPad is not recognized, please update to latest
   BIOS. This is especially the case for some R52 ThinkPads. */
514
static struct dmi_system_id __initdata hdaps_whitelist[] = {
515
	HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad R50p", HDAPS_BOTH_AXES),
516 517 518
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R50"),
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R51"),
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R52"),
519 520 521
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad R61i", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad R61", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T41p", HDAPS_BOTH_AXES),
522
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T41"),
523
	HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T42p", HDAPS_BOTH_AXES),
524 525
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T42"),
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T43"),
526
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T400", HDAPS_BOTH_AXES),
527 528 529
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T60", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T61p", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T61", HDAPS_BOTH_AXES),
530
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad X40"),
531
	HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad X41", HDAPS_Y_AXIS),
532 533 534
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad X60", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad X61s", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad X61", HDAPS_BOTH_AXES),
535
	HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad Z60m"),
536 537
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad Z61m", HDAPS_BOTH_AXES),
	HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad Z61p", HDAPS_BOTH_AXES),
538 539
	{ .ident = NULL }
};
540

R
Robert Love 已提交
541 542
static int __init hdaps_init(void)
{
543
	struct input_dev *idev;
R
Robert Love 已提交
544 545 546
	int ret;

	if (!dmi_check_system(hdaps_whitelist)) {
547
		pr_warn("supported laptop not found!\n");
A
Andrew Morton 已提交
548
		ret = -ENODEV;
R
Robert Love 已提交
549 550 551 552 553 554 555 556
		goto out;
	}

	if (!request_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS, "hdaps")) {
		ret = -ENXIO;
		goto out;
	}

557
	ret = platform_driver_register(&hdaps_driver);
R
Robert Love 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
	if (ret)
		goto out_region;

	pdev = platform_device_register_simple("hdaps", -1, NULL, 0);
	if (IS_ERR(pdev)) {
		ret = PTR_ERR(pdev);
		goto out_driver;
	}

	ret = sysfs_create_group(&pdev->dev.kobj, &hdaps_attribute_group);
	if (ret)
		goto out_device;

571
	hdaps_idev = input_allocate_polled_device();
572 573 574 575 576
	if (!hdaps_idev) {
		ret = -ENOMEM;
		goto out_group;
	}

577 578 579
	hdaps_idev->poll = hdaps_mousedev_poll;
	hdaps_idev->poll_interval = HDAPS_POLL_INTERVAL;

R
Robert Love 已提交
580 581 582 583
	/* initial calibrate for the input device */
	hdaps_calibrate();

	/* initialize the input class */
584 585
	idev = hdaps_idev->input;
	idev->name = "hdaps";
586 587
	idev->phys = "isa1600/input0";
	idev->id.bustype = BUS_ISA;
588
	idev->dev.parent = &pdev->dev;
589
	idev->evbit[0] = BIT_MASK(EV_ABS);
590
	input_set_abs_params(idev, ABS_X,
591
			-256, 256, HDAPS_INPUT_FUZZ, HDAPS_INPUT_FLAT);
592
	input_set_abs_params(idev, ABS_Y,
593 594
			-256, 256, HDAPS_INPUT_FUZZ, HDAPS_INPUT_FLAT);

595
	ret = input_register_polled_device(hdaps_idev);
596 597
	if (ret)
		goto out_idev;
R
Robert Love 已提交
598

599
	pr_info("driver successfully loaded\n");
R
Robert Love 已提交
600 601
	return 0;

602
out_idev:
603
	input_free_polled_device(hdaps_idev);
604 605
out_group:
	sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
R
Robert Love 已提交
606 607 608
out_device:
	platform_device_unregister(pdev);
out_driver:
609
	platform_driver_unregister(&hdaps_driver);
R
Robert Love 已提交
610 611 612
out_region:
	release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
out:
613
	pr_warn("driver init failed (ret=%d)!\n", ret);
R
Robert Love 已提交
614 615 616 617 618
	return ret;
}

static void __exit hdaps_exit(void)
{
619 620
	input_unregister_polled_device(hdaps_idev);
	input_free_polled_device(hdaps_idev);
R
Robert Love 已提交
621 622
	sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
	platform_device_unregister(pdev);
623
	platform_driver_unregister(&hdaps_driver);
R
Robert Love 已提交
624 625
	release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);

626
	pr_info("driver unloaded\n");
R
Robert Love 已提交
627 628 629 630 631
}

module_init(hdaps_init);
module_exit(hdaps_exit);

632 633 634
module_param_named(invert, hdaps_invert, int, 0);
MODULE_PARM_DESC(invert, "invert data along each axis. 1 invert x-axis, "
		 "2 invert y-axis, 3 invert both axes.");
R
Robert Love 已提交
635 636 637 638

MODULE_AUTHOR("Robert Love");
MODULE_DESCRIPTION("IBM Hard Drive Active Protection System (HDAPS) driver");
MODULE_LICENSE("GPL v2");