2 * drivers/hwmon/hdaps.c - driver for IBM's Hard Drive Active Protection System
4 * Copyright (C) 2005 Robert Love <rml@novell.com>
5 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
7 * The HardDisk Active Protection System (hdaps) is present in IBM ThinkPads
8 * starting with the R40, T41, and X40. It provides a basic two-axis
9 * accelerometer and other data, such as the device's temperature.
11 * This driver is based on the document by Mark A. Smith available at
12 * http://www.almaden.ibm.com/cs/people/marksmith/tpaps.html and a lot of trial
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License v2 as published by the
17 * Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/mutex.h>
34 #include <linux/module.h>
35 #include <linux/timer.h>
36 #include <linux/dmi.h>
37 #include <linux/jiffies.h>
41 #define HDAPS_LOW_PORT 0x1600 /* first port used by hdaps */
42 #define HDAPS_NR_PORTS 0x30 /* number of ports: 0x1600 - 0x162f */
44 #define HDAPS_PORT_STATE 0x1611 /* device state */
45 #define HDAPS_PORT_YPOS 0x1612 /* y-axis position */
46 #define HDAPS_PORT_XPOS 0x1614 /* x-axis position */
47 #define HDAPS_PORT_TEMP1 0x1616 /* device temperature, in Celsius */
48 #define HDAPS_PORT_YVAR 0x1617 /* y-axis variance (what is this?) */
49 #define HDAPS_PORT_XVAR 0x1619 /* x-axis variance (what is this?) */
50 #define HDAPS_PORT_TEMP2 0x161b /* device temperature (again?) */
51 #define HDAPS_PORT_UNKNOWN 0x161c /* what is this? */
52 #define HDAPS_PORT_KMACT 0x161d /* keyboard or mouse activity */
54 #define STATE_FRESH 0x50 /* accelerometer data is fresh */
56 #define KEYBD_MASK 0x20 /* set if keyboard activity */
57 #define MOUSE_MASK 0x40 /* set if mouse activity */
58 #define KEYBD_ISSET(n) (!! (n & KEYBD_MASK)) /* keyboard used? */
59 #define MOUSE_ISSET(n) (!! (n & MOUSE_MASK)) /* mouse used? */
61 #define INIT_TIMEOUT_MSECS 4000 /* wait up to 4s for device init ... */
62 #define INIT_WAIT_MSECS 200 /* ... in 200ms increments */
64 #define HDAPS_POLL_PERIOD (HZ/20) /* poll for input every 1/20s */
65 #define HDAPS_INPUT_FUZZ 4 /* input event threshold */
66 #define HDAPS_INPUT_FLAT 4
68 static struct timer_list hdaps_timer
;
69 static struct platform_device
*pdev
;
70 static struct input_dev
*hdaps_idev
;
71 static unsigned int hdaps_invert
;
72 static u8 km_activity
;
76 static DEFINE_MUTEX(hdaps_mtx
);
79 * __get_latch - Get the value from a given port. Callers must hold hdaps_mtx.
81 static inline u8
__get_latch(u16 port
)
83 return inb(port
) & 0xff;
87 * __check_latch - Check a port latch for a given value. Returns zero if the
88 * port contains the given value. Callers must hold hdaps_mtx.
90 static inline int __check_latch(u16 port
, u8 val
)
92 if (__get_latch(port
) == val
)
98 * __wait_latch - Wait up to 100us for a port latch to get a certain value,
99 * returning zero if the value is obtained. Callers must hold hdaps_mtx.
101 static int __wait_latch(u16 port
, u8 val
)
105 for (i
= 0; i
< 20; i
++) {
106 if (!__check_latch(port
, val
))
115 * __device_refresh - request a refresh from the accelerometer. Does not wait
116 * for refresh to complete. Callers must hold hdaps_mtx.
118 static void __device_refresh(void)
121 if (inb(0x1604) != STATE_FRESH
) {
128 * __device_refresh_sync - request a synchronous refresh from the
129 * accelerometer. We wait for the refresh to complete. Returns zero if
130 * successful and nonzero on error. Callers must hold hdaps_mtx.
132 static int __device_refresh_sync(void)
135 return __wait_latch(0x1604, STATE_FRESH
);
139 * __device_complete - indicate to the accelerometer that we are done reading
140 * data, and then initiate an async refresh. Callers must hold hdaps_mtx.
142 static inline void __device_complete(void)
150 * hdaps_readb_one - reads a byte from a single I/O port, placing the value in
151 * the given pointer. Returns zero on success or a negative error on failure.
154 static int hdaps_readb_one(unsigned int port
, u8
*val
)
158 mutex_lock(&hdaps_mtx
);
160 /* do a sync refresh -- we need to be sure that we read fresh data */
161 ret
= __device_refresh_sync();
169 mutex_unlock(&hdaps_mtx
);
173 /* __hdaps_read_pair - internal lockless helper for hdaps_read_pair(). */
174 static int __hdaps_read_pair(unsigned int port1
, unsigned int port2
,
177 /* do a sync refresh -- we need to be sure that we read fresh data */
178 if (__device_refresh_sync())
183 km_activity
= inb(HDAPS_PORT_KMACT
);
186 /* if hdaps_invert is set, negate the two values */
196 * hdaps_read_pair - reads the values from a pair of ports, placing the values
197 * in the given pointers. Returns zero on success. Can sleep.
199 static int hdaps_read_pair(unsigned int port1
, unsigned int port2
,
200 int *val1
, int *val2
)
204 mutex_lock(&hdaps_mtx
);
205 ret
= __hdaps_read_pair(port1
, port2
, val1
, val2
);
206 mutex_unlock(&hdaps_mtx
);
212 * hdaps_device_init - initialize the accelerometer. Returns zero on success
213 * and negative error code on failure. Can sleep.
215 static int hdaps_device_init(void)
217 int total
, ret
= -ENXIO
;
219 mutex_lock(&hdaps_mtx
);
223 if (__wait_latch(0x161f, 0x00))
227 * Most ThinkPads return 0x01.
229 * Others--namely the R50p, T41p, and T42p--return 0x03. These laptops
230 * have "inverted" axises.
232 * The 0x02 value occurs when the chip has been previously initialized.
234 if (__check_latch(0x1611, 0x03) &&
235 __check_latch(0x1611, 0x02) &&
236 __check_latch(0x1611, 0x01))
239 printk(KERN_DEBUG
"hdaps: initial latch check good (0x%02x).\n",
240 __get_latch(0x1611));
245 if (__wait_latch(0x161f, 0x00))
247 if (__wait_latch(0x1611, 0x00))
249 if (__wait_latch(0x1612, 0x60))
251 if (__wait_latch(0x1613, 0x00))
256 if (__wait_latch(0x161f, 0x00))
263 if (__wait_latch(0x161f, 0x00))
265 if (__device_refresh_sync())
267 if (__wait_latch(0x1611, 0x00))
270 /* we have done our dance, now let's wait for the applause */
271 for (total
= INIT_TIMEOUT_MSECS
; total
> 0; total
-= INIT_WAIT_MSECS
) {
274 /* a read of the device helps push it into action */
275 __hdaps_read_pair(HDAPS_PORT_XPOS
, HDAPS_PORT_YPOS
, &x
, &y
);
276 if (!__wait_latch(0x1611, 0x02)) {
281 msleep(INIT_WAIT_MSECS
);
285 mutex_unlock(&hdaps_mtx
);
290 /* Device model stuff */
292 static int hdaps_probe(struct platform_device
*dev
)
296 ret
= hdaps_device_init();
300 printk(KERN_INFO
"hdaps: device successfully initialized.\n");
304 static int hdaps_resume(struct platform_device
*dev
)
306 return hdaps_device_init();
309 static struct platform_driver hdaps_driver
= {
310 .probe
= hdaps_probe
,
311 .resume
= hdaps_resume
,
314 .owner
= THIS_MODULE
,
319 * hdaps_calibrate - Set our "resting" values. Callers must hold hdaps_mtx.
321 static void hdaps_calibrate(void)
323 __hdaps_read_pair(HDAPS_PORT_XPOS
, HDAPS_PORT_YPOS
, &rest_x
, &rest_y
);
326 static void hdaps_mousedev_poll(unsigned long unused
)
330 /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
331 if (mutex_trylock(&hdaps_mtx
)) {
332 mod_timer(&hdaps_timer
,jiffies
+ HDAPS_POLL_PERIOD
);
336 if (__hdaps_read_pair(HDAPS_PORT_XPOS
, HDAPS_PORT_YPOS
, &x
, &y
))
339 input_report_abs(hdaps_idev
, ABS_X
, x
- rest_x
);
340 input_report_abs(hdaps_idev
, ABS_Y
, y
- rest_y
);
341 input_sync(hdaps_idev
);
343 mod_timer(&hdaps_timer
, jiffies
+ HDAPS_POLL_PERIOD
);
346 mutex_unlock(&hdaps_mtx
);
352 static ssize_t
hdaps_position_show(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
357 ret
= hdaps_read_pair(HDAPS_PORT_XPOS
, HDAPS_PORT_YPOS
, &x
, &y
);
361 return sprintf(buf
, "(%d,%d)\n", x
, y
);
364 static ssize_t
hdaps_variance_show(struct device
*dev
,
365 struct device_attribute
*attr
, char *buf
)
369 ret
= hdaps_read_pair(HDAPS_PORT_XVAR
, HDAPS_PORT_YVAR
, &x
, &y
);
373 return sprintf(buf
, "(%d,%d)\n", x
, y
);
376 static ssize_t
hdaps_temp1_show(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
382 ret
= hdaps_readb_one(HDAPS_PORT_TEMP1
, &temp
);
386 return sprintf(buf
, "%u\n", temp
);
389 static ssize_t
hdaps_temp2_show(struct device
*dev
,
390 struct device_attribute
*attr
, char *buf
)
395 ret
= hdaps_readb_one(HDAPS_PORT_TEMP2
, &temp
);
399 return sprintf(buf
, "%u\n", temp
);
402 static ssize_t
hdaps_keyboard_activity_show(struct device
*dev
,
403 struct device_attribute
*attr
,
406 return sprintf(buf
, "%u\n", KEYBD_ISSET(km_activity
));
409 static ssize_t
hdaps_mouse_activity_show(struct device
*dev
,
410 struct device_attribute
*attr
,
413 return sprintf(buf
, "%u\n", MOUSE_ISSET(km_activity
));
416 static ssize_t
hdaps_calibrate_show(struct device
*dev
,
417 struct device_attribute
*attr
, char *buf
)
419 return sprintf(buf
, "(%d,%d)\n", rest_x
, rest_y
);
422 static ssize_t
hdaps_calibrate_store(struct device
*dev
,
423 struct device_attribute
*attr
,
424 const char *buf
, size_t count
)
426 mutex_lock(&hdaps_mtx
);
428 mutex_unlock(&hdaps_mtx
);
433 static ssize_t
hdaps_invert_show(struct device
*dev
,
434 struct device_attribute
*attr
, char *buf
)
436 return sprintf(buf
, "%u\n", hdaps_invert
);
439 static ssize_t
hdaps_invert_store(struct device
*dev
,
440 struct device_attribute
*attr
,
441 const char *buf
, size_t count
)
445 if (sscanf(buf
, "%d", &invert
) != 1 || (invert
!= 1 && invert
!= 0))
448 hdaps_invert
= invert
;
454 static DEVICE_ATTR(position
, 0444, hdaps_position_show
, NULL
);
455 static DEVICE_ATTR(variance
, 0444, hdaps_variance_show
, NULL
);
456 static DEVICE_ATTR(temp1
, 0444, hdaps_temp1_show
, NULL
);
457 static DEVICE_ATTR(temp2
, 0444, hdaps_temp2_show
, NULL
);
458 static DEVICE_ATTR(keyboard_activity
, 0444, hdaps_keyboard_activity_show
, NULL
);
459 static DEVICE_ATTR(mouse_activity
, 0444, hdaps_mouse_activity_show
, NULL
);
460 static DEVICE_ATTR(calibrate
, 0644, hdaps_calibrate_show
,hdaps_calibrate_store
);
461 static DEVICE_ATTR(invert
, 0644, hdaps_invert_show
, hdaps_invert_store
);
463 static struct attribute
*hdaps_attributes
[] = {
464 &dev_attr_position
.attr
,
465 &dev_attr_variance
.attr
,
466 &dev_attr_temp1
.attr
,
467 &dev_attr_temp2
.attr
,
468 &dev_attr_keyboard_activity
.attr
,
469 &dev_attr_mouse_activity
.attr
,
470 &dev_attr_calibrate
.attr
,
471 &dev_attr_invert
.attr
,
475 static struct attribute_group hdaps_attribute_group
= {
476 .attrs
= hdaps_attributes
,
482 /* hdaps_dmi_match - found a match. return one, short-circuiting the hunt. */
483 static int __init
hdaps_dmi_match(struct dmi_system_id
*id
)
485 printk(KERN_INFO
"hdaps: %s detected.\n", id
->ident
);
489 /* hdaps_dmi_match_invert - found an inverted match. */
490 static int __init
hdaps_dmi_match_invert(struct dmi_system_id
*id
)
493 printk(KERN_INFO
"hdaps: inverting axis readings.\n");
494 return hdaps_dmi_match(id
);
497 #define HDAPS_DMI_MATCH_NORMAL(vendor, model) { \
498 .ident = vendor " " model, \
499 .callback = hdaps_dmi_match, \
501 DMI_MATCH(DMI_BOARD_VENDOR, vendor), \
502 DMI_MATCH(DMI_PRODUCT_VERSION, model) \
506 #define HDAPS_DMI_MATCH_INVERT(vendor, model) { \
507 .ident = vendor " " model, \
508 .callback = hdaps_dmi_match_invert, \
510 DMI_MATCH(DMI_BOARD_VENDOR, vendor), \
511 DMI_MATCH(DMI_PRODUCT_VERSION, model) \
515 /* Note that HDAPS_DMI_MATCH_NORMAL("ThinkPad T42") would match
516 "ThinkPad T42p", so the order of the entries matters.
517 If your ThinkPad is not recognized, please update to latest
518 BIOS. This is especially the case for some R52 ThinkPads. */
519 static struct dmi_system_id __initdata hdaps_whitelist
[] = {
520 HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad R50p"),
521 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R50"),
522 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R51"),
523 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad R52"),
524 HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T41p"),
525 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T41"),
526 HDAPS_DMI_MATCH_INVERT("IBM", "ThinkPad T42p"),
527 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T42"),
528 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad T43"),
529 HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad T60"),
530 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad X40"),
531 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad X41"),
532 HDAPS_DMI_MATCH_INVERT("LENOVO", "ThinkPad X60"),
533 HDAPS_DMI_MATCH_NORMAL("IBM", "ThinkPad Z60m"),
537 static int __init
hdaps_init(void)
541 if (!dmi_check_system(hdaps_whitelist
)) {
542 printk(KERN_WARNING
"hdaps: supported laptop not found!\n");
547 if (!request_region(HDAPS_LOW_PORT
, HDAPS_NR_PORTS
, "hdaps")) {
552 ret
= platform_driver_register(&hdaps_driver
);
556 pdev
= platform_device_register_simple("hdaps", -1, NULL
, 0);
562 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &hdaps_attribute_group
);
566 hdaps_idev
= input_allocate_device();
572 /* initial calibrate for the input device */
575 /* initialize the input class */
576 hdaps_idev
->name
= "hdaps";
577 hdaps_idev
->dev
.parent
= &pdev
->dev
;
578 hdaps_idev
->evbit
[0] = BIT(EV_ABS
);
579 input_set_abs_params(hdaps_idev
, ABS_X
,
580 -256, 256, HDAPS_INPUT_FUZZ
, HDAPS_INPUT_FLAT
);
581 input_set_abs_params(hdaps_idev
, ABS_Y
,
582 -256, 256, HDAPS_INPUT_FUZZ
, HDAPS_INPUT_FLAT
);
584 ret
= input_register_device(hdaps_idev
);
588 /* start up our timer for the input device */
589 init_timer(&hdaps_timer
);
590 hdaps_timer
.function
= hdaps_mousedev_poll
;
591 hdaps_timer
.expires
= jiffies
+ HDAPS_POLL_PERIOD
;
592 add_timer(&hdaps_timer
);
594 printk(KERN_INFO
"hdaps: driver successfully loaded.\n");
598 input_free_device(hdaps_idev
);
600 sysfs_remove_group(&pdev
->dev
.kobj
, &hdaps_attribute_group
);
602 platform_device_unregister(pdev
);
604 platform_driver_unregister(&hdaps_driver
);
606 release_region(HDAPS_LOW_PORT
, HDAPS_NR_PORTS
);
608 printk(KERN_WARNING
"hdaps: driver init failed (ret=%d)!\n", ret
);
612 static void __exit
hdaps_exit(void)
614 del_timer_sync(&hdaps_timer
);
615 input_unregister_device(hdaps_idev
);
616 sysfs_remove_group(&pdev
->dev
.kobj
, &hdaps_attribute_group
);
617 platform_device_unregister(pdev
);
618 platform_driver_unregister(&hdaps_driver
);
619 release_region(HDAPS_LOW_PORT
, HDAPS_NR_PORTS
);
621 printk(KERN_INFO
"hdaps: driver unloaded.\n");
624 module_init(hdaps_init
);
625 module_exit(hdaps_exit
);
627 module_param_named(invert
, hdaps_invert
, bool, 0);
628 MODULE_PARM_DESC(invert
, "invert data along each axis");
630 MODULE_AUTHOR("Robert Love");
631 MODULE_DESCRIPTION("IBM Hard Drive Active Protection System (HDAPS) driver");
632 MODULE_LICENSE("GPL v2");