hdaps.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * drivers/hwmon/hdaps.c - driver for IBM's Hard Drive Active Protection System
  3. *
  4. * Copyright (C) 2005 Robert Love <rml@novell.com>
  5. * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
  6. *
  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.
  10. *
  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
  13. * and error.
  14. *
  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.
  18. *
  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
  22. * more details.
  23. *
  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
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/device.h>
  30. #include <linux/input.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/timer.h>
  34. #include <linux/dmi.h>
  35. #include <asm/io.h>
  36. #define HDAPS_LOW_PORT 0x1600 /* first port used by hdaps */
  37. #define HDAPS_NR_PORTS 0x30 /* number of ports: 0x1600 - 0x162f */
  38. #define HDAPS_PORT_STATE 0x1611 /* device state */
  39. #define HDAPS_PORT_YPOS 0x1612 /* y-axis position */
  40. #define HDAPS_PORT_XPOS 0x1614 /* x-axis position */
  41. #define HDAPS_PORT_TEMP1 0x1616 /* device temperature, in celcius */
  42. #define HDAPS_PORT_YVAR 0x1617 /* y-axis variance (what is this?) */
  43. #define HDAPS_PORT_XVAR 0x1619 /* x-axis variance (what is this?) */
  44. #define HDAPS_PORT_TEMP2 0x161b /* device temperature (again?) */
  45. #define HDAPS_PORT_UNKNOWN 0x161c /* what is this? */
  46. #define HDAPS_PORT_KMACT 0x161d /* keyboard or mouse activity */
  47. #define STATE_FRESH 0x50 /* accelerometer data is fresh */
  48. #define KEYBD_MASK 0x20 /* set if keyboard activity */
  49. #define MOUSE_MASK 0x40 /* set if mouse activity */
  50. #define KEYBD_ISSET(n) (!! (n & KEYBD_MASK)) /* keyboard used? */
  51. #define MOUSE_ISSET(n) (!! (n & MOUSE_MASK)) /* mouse used? */
  52. #define INIT_TIMEOUT_MSECS 4000 /* wait up to 4s for device init ... */
  53. #define INIT_WAIT_MSECS 200 /* ... in 200ms increments */
  54. #define HDAPS_POLL_PERIOD (HZ/20) /* poll for input every 1/20s */
  55. #define HDAPS_INPUT_FUZZ 4 /* input event threshold */
  56. static struct timer_list hdaps_timer;
  57. static struct platform_device *pdev;
  58. static unsigned int hdaps_invert;
  59. static u8 km_activity;
  60. static int rest_x;
  61. static int rest_y;
  62. static DECLARE_MUTEX(hdaps_sem);
  63. /*
  64. * __get_latch - Get the value from a given port. Callers must hold hdaps_sem.
  65. */
  66. static inline u8 __get_latch(u16 port)
  67. {
  68. return inb(port) & 0xff;
  69. }
  70. /*
  71. * __check_latch - Check a port latch for a given value. Returns zero if the
  72. * port contains the given value. Callers must hold hdaps_sem.
  73. */
  74. static inline int __check_latch(u16 port, u8 val)
  75. {
  76. if (__get_latch(port) == val)
  77. return 0;
  78. return -EINVAL;
  79. }
  80. /*
  81. * __wait_latch - Wait up to 100us for a port latch to get a certain value,
  82. * returning zero if the value is obtained. Callers must hold hdaps_sem.
  83. */
  84. static int __wait_latch(u16 port, u8 val)
  85. {
  86. unsigned int i;
  87. for (i = 0; i < 20; i++) {
  88. if (!__check_latch(port, val))
  89. return 0;
  90. udelay(5);
  91. }
  92. return -EIO;
  93. }
  94. /*
  95. * __device_refresh - request a refresh from the accelerometer. Does not wait
  96. * for refresh to complete. Callers must hold hdaps_sem.
  97. */
  98. static void __device_refresh(void)
  99. {
  100. udelay(200);
  101. if (inb(0x1604) != STATE_FRESH) {
  102. outb(0x11, 0x1610);
  103. outb(0x01, 0x161f);
  104. }
  105. }
  106. /*
  107. * __device_refresh_sync - request a synchronous refresh from the
  108. * accelerometer. We wait for the refresh to complete. Returns zero if
  109. * successful and nonzero on error. Callers must hold hdaps_sem.
  110. */
  111. static int __device_refresh_sync(void)
  112. {
  113. __device_refresh();
  114. return __wait_latch(0x1604, STATE_FRESH);
  115. }
  116. /*
  117. * __device_complete - indicate to the accelerometer that we are done reading
  118. * data, and then initiate an async refresh. Callers must hold hdaps_sem.
  119. */
  120. static inline void __device_complete(void)
  121. {
  122. inb(0x161f);
  123. inb(0x1604);
  124. __device_refresh();
  125. }
  126. /*
  127. * hdaps_readb_one - reads a byte from a single I/O port, placing the value in
  128. * the given pointer. Returns zero on success or a negative error on failure.
  129. * Can sleep.
  130. */
  131. static int hdaps_readb_one(unsigned int port, u8 *val)
  132. {
  133. int ret;
  134. down(&hdaps_sem);
  135. /* do a sync refresh -- we need to be sure that we read fresh data */
  136. ret = __device_refresh_sync();
  137. if (ret)
  138. goto out;
  139. *val = inb(port);
  140. __device_complete();
  141. out:
  142. up(&hdaps_sem);
  143. return ret;
  144. }
  145. /* __hdaps_read_pair - internal lockless helper for hdaps_read_pair(). */
  146. static int __hdaps_read_pair(unsigned int port1, unsigned int port2,
  147. int *x, int *y)
  148. {
  149. /* do a sync refresh -- we need to be sure that we read fresh data */
  150. if (__device_refresh_sync())
  151. return -EIO;
  152. *y = inw(port2);
  153. *x = inw(port1);
  154. km_activity = inb(HDAPS_PORT_KMACT);
  155. __device_complete();
  156. /* if hdaps_invert is set, negate the two values */
  157. if (hdaps_invert) {
  158. *x = -*x;
  159. *y = -*y;
  160. }
  161. return 0;
  162. }
  163. /*
  164. * hdaps_read_pair - reads the values from a pair of ports, placing the values
  165. * in the given pointers. Returns zero on success. Can sleep.
  166. */
  167. static int hdaps_read_pair(unsigned int port1, unsigned int port2,
  168. int *val1, int *val2)
  169. {
  170. int ret;
  171. down(&hdaps_sem);
  172. ret = __hdaps_read_pair(port1, port2, val1, val2);
  173. up(&hdaps_sem);
  174. return ret;
  175. }
  176. /*
  177. * hdaps_device_init - initialize the accelerometer. Returns zero on success
  178. * and negative error code on failure. Can sleep.
  179. */
  180. static int hdaps_device_init(void)
  181. {
  182. int total, ret = -ENXIO;
  183. down(&hdaps_sem);
  184. outb(0x13, 0x1610);
  185. outb(0x01, 0x161f);
  186. if (__wait_latch(0x161f, 0x00))
  187. goto out;
  188. /*
  189. * Most ThinkPads return 0x01.
  190. *
  191. * Others--namely the R50p, T41p, and T42p--return 0x03. These laptops
  192. * have "inverted" axises.
  193. *
  194. * The 0x02 value occurs when the chip has been previously initialized.
  195. */
  196. if (__check_latch(0x1611, 0x03) &&
  197. __check_latch(0x1611, 0x02) &&
  198. __check_latch(0x1611, 0x01))
  199. goto out;
  200. printk(KERN_DEBUG "hdaps: initial latch check good (0x%02x).\n",
  201. __get_latch(0x1611));
  202. outb(0x17, 0x1610);
  203. outb(0x81, 0x1611);
  204. outb(0x01, 0x161f);
  205. if (__wait_latch(0x161f, 0x00))
  206. goto out;
  207. if (__wait_latch(0x1611, 0x00))
  208. goto out;
  209. if (__wait_latch(0x1612, 0x60))
  210. goto out;
  211. if (__wait_latch(0x1613, 0x00))
  212. goto out;
  213. outb(0x14, 0x1610);
  214. outb(0x01, 0x1611);
  215. outb(0x01, 0x161f);
  216. if (__wait_latch(0x161f, 0x00))
  217. goto out;
  218. outb(0x10, 0x1610);
  219. outb(0xc8, 0x1611);
  220. outb(0x00, 0x1612);
  221. outb(0x02, 0x1613);
  222. outb(0x01, 0x161f);
  223. if (__wait_latch(0x161f, 0x00))
  224. goto out;
  225. if (__device_refresh_sync())
  226. goto out;
  227. if (__wait_latch(0x1611, 0x00))
  228. goto out;
  229. /* we have done our dance, now let's wait for the applause */
  230. for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
  231. int x, y;
  232. /* a read of the device helps push it into action */
  233. __hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y);
  234. if (!__wait_latch(0x1611, 0x02)) {
  235. ret = 0;
  236. break;
  237. }
  238. msleep(INIT_WAIT_MSECS);
  239. }
  240. out:
  241. up(&hdaps_sem);
  242. return ret;
  243. }
  244. /* Device model stuff */
  245. static int hdaps_probe(struct device *dev)
  246. {
  247. int ret;
  248. ret = hdaps_device_init();
  249. if (ret)
  250. return ret;
  251. printk(KERN_INFO "hdaps: device successfully initialized.\n");
  252. return 0;
  253. }
  254. static int hdaps_resume(struct device *dev)
  255. {
  256. return hdaps_device_init();
  257. }
  258. static struct device_driver hdaps_driver = {
  259. .name = "hdaps",
  260. .bus = &platform_bus_type,
  261. .owner = THIS_MODULE,
  262. .probe = hdaps_probe,
  263. .resume = hdaps_resume
  264. };
  265. /* Input class stuff */
  266. static struct input_dev hdaps_idev = {
  267. .name = "hdaps",
  268. .evbit = { BIT(EV_ABS) },
  269. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  270. .absmin = { [ABS_X] = -256, [ABS_Y] = -256 },
  271. .absmax = { [ABS_X] = 256, [ABS_Y] = 256 },
  272. .absfuzz = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
  273. .absflat = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
  274. };
  275. /*
  276. * hdaps_calibrate - Set our "resting" values. Callers must hold hdaps_sem.
  277. */
  278. static void hdaps_calibrate(void)
  279. {
  280. __hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &rest_x, &rest_y);
  281. }
  282. static void hdaps_mousedev_poll(unsigned long unused)
  283. {
  284. int x, y;
  285. /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
  286. if (down_trylock(&hdaps_sem)) {
  287. mod_timer(&hdaps_timer,jiffies + HDAPS_POLL_PERIOD);
  288. return;
  289. }
  290. if (__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y))
  291. goto out;
  292. input_report_abs(&hdaps_idev, ABS_X, x - rest_x);
  293. input_report_abs(&hdaps_idev, ABS_Y, y - rest_y);
  294. input_sync(&hdaps_idev);
  295. mod_timer(&hdaps_timer, jiffies + HDAPS_POLL_PERIOD);
  296. out:
  297. up(&hdaps_sem);
  298. }
  299. /* Sysfs Files */
  300. static ssize_t hdaps_position_show(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. int ret, x, y;
  304. ret = hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y);
  305. if (ret)
  306. return ret;
  307. return sprintf(buf, "(%d,%d)\n", x, y);
  308. }
  309. static ssize_t hdaps_variance_show(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. int ret, x, y;
  313. ret = hdaps_read_pair(HDAPS_PORT_XVAR, HDAPS_PORT_YVAR, &x, &y);
  314. if (ret)
  315. return ret;
  316. return sprintf(buf, "(%d,%d)\n", x, y);
  317. }
  318. static ssize_t hdaps_temp1_show(struct device *dev,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. u8 temp;
  322. int ret;
  323. ret = hdaps_readb_one(HDAPS_PORT_TEMP1, &temp);
  324. if (ret < 0)
  325. return ret;
  326. return sprintf(buf, "%u\n", temp);
  327. }
  328. static ssize_t hdaps_temp2_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. u8 temp;
  332. int ret;
  333. ret = hdaps_readb_one(HDAPS_PORT_TEMP2, &temp);
  334. if (ret < 0)
  335. return ret;
  336. return sprintf(buf, "%u\n", temp);
  337. }
  338. static ssize_t hdaps_keyboard_activity_show(struct device *dev,
  339. struct device_attribute *attr,
  340. char *buf)
  341. {
  342. return sprintf(buf, "%u\n", KEYBD_ISSET(km_activity));
  343. }
  344. static ssize_t hdaps_mouse_activity_show(struct device *dev,
  345. struct device_attribute *attr,
  346. char *buf)
  347. {
  348. return sprintf(buf, "%u\n", MOUSE_ISSET(km_activity));
  349. }
  350. static ssize_t hdaps_calibrate_show(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. return sprintf(buf, "(%d,%d)\n", rest_x, rest_y);
  354. }
  355. static ssize_t hdaps_calibrate_store(struct device *dev,
  356. struct device_attribute *attr,
  357. const char *buf, size_t count)
  358. {
  359. down(&hdaps_sem);
  360. hdaps_calibrate();
  361. up(&hdaps_sem);
  362. return count;
  363. }
  364. static ssize_t hdaps_invert_show(struct device *dev,
  365. struct device_attribute *attr, char *buf)
  366. {
  367. return sprintf(buf, "%u\n", hdaps_invert);
  368. }
  369. static ssize_t hdaps_invert_store(struct device *dev,
  370. struct device_attribute *attr,
  371. const char *buf, size_t count)
  372. {
  373. int invert;
  374. if (sscanf(buf, "%d", &invert) != 1 || (invert != 1 && invert != 0))
  375. return -EINVAL;
  376. hdaps_invert = invert;
  377. hdaps_calibrate();
  378. return count;
  379. }
  380. static DEVICE_ATTR(position, 0444, hdaps_position_show, NULL);
  381. static DEVICE_ATTR(variance, 0444, hdaps_variance_show, NULL);
  382. static DEVICE_ATTR(temp1, 0444, hdaps_temp1_show, NULL);
  383. static DEVICE_ATTR(temp2, 0444, hdaps_temp2_show, NULL);
  384. static DEVICE_ATTR(keyboard_activity, 0444, hdaps_keyboard_activity_show, NULL);
  385. static DEVICE_ATTR(mouse_activity, 0444, hdaps_mouse_activity_show, NULL);
  386. static DEVICE_ATTR(calibrate, 0644, hdaps_calibrate_show,hdaps_calibrate_store);
  387. static DEVICE_ATTR(invert, 0644, hdaps_invert_show, hdaps_invert_store);
  388. static struct attribute *hdaps_attributes[] = {
  389. &dev_attr_position.attr,
  390. &dev_attr_variance.attr,
  391. &dev_attr_temp1.attr,
  392. &dev_attr_temp2.attr,
  393. &dev_attr_keyboard_activity.attr,
  394. &dev_attr_mouse_activity.attr,
  395. &dev_attr_calibrate.attr,
  396. &dev_attr_invert.attr,
  397. NULL,
  398. };
  399. static struct attribute_group hdaps_attribute_group = {
  400. .attrs = hdaps_attributes,
  401. };
  402. /* Module stuff */
  403. /* hdaps_dmi_match - found a match. return one, short-circuiting the hunt. */
  404. static int hdaps_dmi_match(struct dmi_system_id *id)
  405. {
  406. printk(KERN_INFO "hdaps: %s detected.\n", id->ident);
  407. return 1;
  408. }
  409. /* hdaps_dmi_match_invert - found an inverted match. */
  410. static int hdaps_dmi_match_invert(struct dmi_system_id *id)
  411. {
  412. hdaps_invert = 1;
  413. printk(KERN_INFO "hdaps: inverting axis readings.\n");
  414. return hdaps_dmi_match(id);
  415. }
  416. #define HDAPS_DMI_MATCH_NORMAL(model) { \
  417. .ident = "IBM " model, \
  418. .callback = hdaps_dmi_match, \
  419. .matches = { \
  420. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), \
  421. DMI_MATCH(DMI_PRODUCT_VERSION, model) \
  422. } \
  423. }
  424. #define HDAPS_DMI_MATCH_INVERT(model) { \
  425. .ident = "IBM " model, \
  426. .callback = hdaps_dmi_match_invert, \
  427. .matches = { \
  428. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), \
  429. DMI_MATCH(DMI_PRODUCT_VERSION, model) \
  430. } \
  431. }
  432. static int __init hdaps_init(void)
  433. {
  434. int ret;
  435. /* Note that DMI_MATCH(...,"ThinkPad T42") will match "ThinkPad T42p" */
  436. struct dmi_system_id hdaps_whitelist[] = {
  437. HDAPS_DMI_MATCH_INVERT("ThinkPad R50p"),
  438. HDAPS_DMI_MATCH_NORMAL("ThinkPad R50"),
  439. HDAPS_DMI_MATCH_NORMAL("ThinkPad R51"),
  440. HDAPS_DMI_MATCH_NORMAL("ThinkPad R52"),
  441. HDAPS_DMI_MATCH_INVERT("ThinkPad T41p"),
  442. HDAPS_DMI_MATCH_NORMAL("ThinkPad T41"),
  443. HDAPS_DMI_MATCH_INVERT("ThinkPad T42p"),
  444. HDAPS_DMI_MATCH_NORMAL("ThinkPad T42"),
  445. HDAPS_DMI_MATCH_NORMAL("ThinkPad T43"),
  446. HDAPS_DMI_MATCH_NORMAL("ThinkPad X40"),
  447. HDAPS_DMI_MATCH_NORMAL("ThinkPad X41 Tablet"),
  448. HDAPS_DMI_MATCH_NORMAL("ThinkPad X41"),
  449. { .ident = NULL }
  450. };
  451. if (!dmi_check_system(hdaps_whitelist)) {
  452. printk(KERN_WARNING "hdaps: supported laptop not found!\n");
  453. ret = -ENXIO;
  454. goto out;
  455. }
  456. if (!request_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS, "hdaps")) {
  457. ret = -ENXIO;
  458. goto out;
  459. }
  460. ret = driver_register(&hdaps_driver);
  461. if (ret)
  462. goto out_region;
  463. pdev = platform_device_register_simple("hdaps", -1, NULL, 0);
  464. if (IS_ERR(pdev)) {
  465. ret = PTR_ERR(pdev);
  466. goto out_driver;
  467. }
  468. ret = sysfs_create_group(&pdev->dev.kobj, &hdaps_attribute_group);
  469. if (ret)
  470. goto out_device;
  471. /* initial calibrate for the input device */
  472. hdaps_calibrate();
  473. /* initialize the input class */
  474. hdaps_idev.dev = &pdev->dev;
  475. input_register_device(&hdaps_idev);
  476. /* start up our timer for the input device */
  477. init_timer(&hdaps_timer);
  478. hdaps_timer.function = hdaps_mousedev_poll;
  479. hdaps_timer.expires = jiffies + HDAPS_POLL_PERIOD;
  480. add_timer(&hdaps_timer);
  481. printk(KERN_INFO "hdaps: driver successfully loaded.\n");
  482. return 0;
  483. out_device:
  484. platform_device_unregister(pdev);
  485. out_driver:
  486. driver_unregister(&hdaps_driver);
  487. out_region:
  488. release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
  489. out:
  490. printk(KERN_WARNING "hdaps: driver init failed (ret=%d)!\n", ret);
  491. return ret;
  492. }
  493. static void __exit hdaps_exit(void)
  494. {
  495. del_timer_sync(&hdaps_timer);
  496. input_unregister_device(&hdaps_idev);
  497. sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
  498. platform_device_unregister(pdev);
  499. driver_unregister(&hdaps_driver);
  500. release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
  501. printk(KERN_INFO "hdaps: driver unloaded.\n");
  502. }
  503. module_init(hdaps_init);
  504. module_exit(hdaps_exit);
  505. module_param_named(invert, hdaps_invert, bool, 0);
  506. MODULE_PARM_DESC(invert, "invert data along each axis");
  507. MODULE_AUTHOR("Robert Love");
  508. MODULE_DESCRIPTION("IBM Hard Drive Active Protection System (HDAPS) driver");
  509. MODULE_LICENSE("GPL v2");