hdaps.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/platform_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 platform_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 platform_device *dev)
  255. {
  256. return hdaps_device_init();
  257. }
  258. static struct platform_driver hdaps_driver = {
  259. .probe = hdaps_probe,
  260. .resume = hdaps_resume,
  261. .driver = {
  262. .name = "hdaps",
  263. .owner = THIS_MODULE,
  264. },
  265. };
  266. /* Input class stuff */
  267. static struct input_dev hdaps_idev = {
  268. .name = "hdaps",
  269. .evbit = { BIT(EV_ABS) },
  270. .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
  271. .absmin = { [ABS_X] = -256, [ABS_Y] = -256 },
  272. .absmax = { [ABS_X] = 256, [ABS_Y] = 256 },
  273. .absfuzz = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
  274. .absflat = { [ABS_X] = HDAPS_INPUT_FUZZ, [ABS_Y] = HDAPS_INPUT_FUZZ },
  275. };
  276. /*
  277. * hdaps_calibrate - Set our "resting" values. Callers must hold hdaps_sem.
  278. */
  279. static void hdaps_calibrate(void)
  280. {
  281. __hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &rest_x, &rest_y);
  282. }
  283. static void hdaps_mousedev_poll(unsigned long unused)
  284. {
  285. int x, y;
  286. /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
  287. if (down_trylock(&hdaps_sem)) {
  288. mod_timer(&hdaps_timer,jiffies + HDAPS_POLL_PERIOD);
  289. return;
  290. }
  291. if (__hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y))
  292. goto out;
  293. input_report_abs(&hdaps_idev, ABS_X, x - rest_x);
  294. input_report_abs(&hdaps_idev, ABS_Y, y - rest_y);
  295. input_sync(&hdaps_idev);
  296. mod_timer(&hdaps_timer, jiffies + HDAPS_POLL_PERIOD);
  297. out:
  298. up(&hdaps_sem);
  299. }
  300. /* Sysfs Files */
  301. static ssize_t hdaps_position_show(struct device *dev,
  302. struct device_attribute *attr, char *buf)
  303. {
  304. int ret, x, y;
  305. ret = hdaps_read_pair(HDAPS_PORT_XPOS, HDAPS_PORT_YPOS, &x, &y);
  306. if (ret)
  307. return ret;
  308. return sprintf(buf, "(%d,%d)\n", x, y);
  309. }
  310. static ssize_t hdaps_variance_show(struct device *dev,
  311. struct device_attribute *attr, char *buf)
  312. {
  313. int ret, x, y;
  314. ret = hdaps_read_pair(HDAPS_PORT_XVAR, HDAPS_PORT_YVAR, &x, &y);
  315. if (ret)
  316. return ret;
  317. return sprintf(buf, "(%d,%d)\n", x, y);
  318. }
  319. static ssize_t hdaps_temp1_show(struct device *dev,
  320. struct device_attribute *attr, char *buf)
  321. {
  322. u8 temp;
  323. int ret;
  324. ret = hdaps_readb_one(HDAPS_PORT_TEMP1, &temp);
  325. if (ret < 0)
  326. return ret;
  327. return sprintf(buf, "%u\n", temp);
  328. }
  329. static ssize_t hdaps_temp2_show(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. u8 temp;
  333. int ret;
  334. ret = hdaps_readb_one(HDAPS_PORT_TEMP2, &temp);
  335. if (ret < 0)
  336. return ret;
  337. return sprintf(buf, "%u\n", temp);
  338. }
  339. static ssize_t hdaps_keyboard_activity_show(struct device *dev,
  340. struct device_attribute *attr,
  341. char *buf)
  342. {
  343. return sprintf(buf, "%u\n", KEYBD_ISSET(km_activity));
  344. }
  345. static ssize_t hdaps_mouse_activity_show(struct device *dev,
  346. struct device_attribute *attr,
  347. char *buf)
  348. {
  349. return sprintf(buf, "%u\n", MOUSE_ISSET(km_activity));
  350. }
  351. static ssize_t hdaps_calibrate_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. return sprintf(buf, "(%d,%d)\n", rest_x, rest_y);
  355. }
  356. static ssize_t hdaps_calibrate_store(struct device *dev,
  357. struct device_attribute *attr,
  358. const char *buf, size_t count)
  359. {
  360. down(&hdaps_sem);
  361. hdaps_calibrate();
  362. up(&hdaps_sem);
  363. return count;
  364. }
  365. static ssize_t hdaps_invert_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. return sprintf(buf, "%u\n", hdaps_invert);
  369. }
  370. static ssize_t hdaps_invert_store(struct device *dev,
  371. struct device_attribute *attr,
  372. const char *buf, size_t count)
  373. {
  374. int invert;
  375. if (sscanf(buf, "%d", &invert) != 1 || (invert != 1 && invert != 0))
  376. return -EINVAL;
  377. hdaps_invert = invert;
  378. hdaps_calibrate();
  379. return count;
  380. }
  381. static DEVICE_ATTR(position, 0444, hdaps_position_show, NULL);
  382. static DEVICE_ATTR(variance, 0444, hdaps_variance_show, NULL);
  383. static DEVICE_ATTR(temp1, 0444, hdaps_temp1_show, NULL);
  384. static DEVICE_ATTR(temp2, 0444, hdaps_temp2_show, NULL);
  385. static DEVICE_ATTR(keyboard_activity, 0444, hdaps_keyboard_activity_show, NULL);
  386. static DEVICE_ATTR(mouse_activity, 0444, hdaps_mouse_activity_show, NULL);
  387. static DEVICE_ATTR(calibrate, 0644, hdaps_calibrate_show,hdaps_calibrate_store);
  388. static DEVICE_ATTR(invert, 0644, hdaps_invert_show, hdaps_invert_store);
  389. static struct attribute *hdaps_attributes[] = {
  390. &dev_attr_position.attr,
  391. &dev_attr_variance.attr,
  392. &dev_attr_temp1.attr,
  393. &dev_attr_temp2.attr,
  394. &dev_attr_keyboard_activity.attr,
  395. &dev_attr_mouse_activity.attr,
  396. &dev_attr_calibrate.attr,
  397. &dev_attr_invert.attr,
  398. NULL,
  399. };
  400. static struct attribute_group hdaps_attribute_group = {
  401. .attrs = hdaps_attributes,
  402. };
  403. /* Module stuff */
  404. /* hdaps_dmi_match - found a match. return one, short-circuiting the hunt. */
  405. static int hdaps_dmi_match(struct dmi_system_id *id)
  406. {
  407. printk(KERN_INFO "hdaps: %s detected.\n", id->ident);
  408. return 1;
  409. }
  410. /* hdaps_dmi_match_invert - found an inverted match. */
  411. static int hdaps_dmi_match_invert(struct dmi_system_id *id)
  412. {
  413. hdaps_invert = 1;
  414. printk(KERN_INFO "hdaps: inverting axis readings.\n");
  415. return hdaps_dmi_match(id);
  416. }
  417. #define HDAPS_DMI_MATCH_NORMAL(model) { \
  418. .ident = "IBM " model, \
  419. .callback = hdaps_dmi_match, \
  420. .matches = { \
  421. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), \
  422. DMI_MATCH(DMI_PRODUCT_VERSION, model) \
  423. } \
  424. }
  425. #define HDAPS_DMI_MATCH_INVERT(model) { \
  426. .ident = "IBM " model, \
  427. .callback = hdaps_dmi_match_invert, \
  428. .matches = { \
  429. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), \
  430. DMI_MATCH(DMI_PRODUCT_VERSION, model) \
  431. } \
  432. }
  433. static int __init hdaps_init(void)
  434. {
  435. int ret;
  436. /* Note that DMI_MATCH(...,"ThinkPad T42") will match "ThinkPad T42p" */
  437. struct dmi_system_id hdaps_whitelist[] = {
  438. HDAPS_DMI_MATCH_INVERT("ThinkPad R50p"),
  439. HDAPS_DMI_MATCH_NORMAL("ThinkPad R50"),
  440. HDAPS_DMI_MATCH_NORMAL("ThinkPad R51"),
  441. HDAPS_DMI_MATCH_NORMAL("ThinkPad R52"),
  442. HDAPS_DMI_MATCH_INVERT("ThinkPad T41p"),
  443. HDAPS_DMI_MATCH_NORMAL("ThinkPad T41"),
  444. HDAPS_DMI_MATCH_INVERT("ThinkPad T42p"),
  445. HDAPS_DMI_MATCH_NORMAL("ThinkPad T42"),
  446. HDAPS_DMI_MATCH_NORMAL("ThinkPad T43"),
  447. HDAPS_DMI_MATCH_NORMAL("ThinkPad X40"),
  448. HDAPS_DMI_MATCH_NORMAL("ThinkPad X41 Tablet"),
  449. HDAPS_DMI_MATCH_NORMAL("ThinkPad X41"),
  450. { .ident = NULL }
  451. };
  452. if (!dmi_check_system(hdaps_whitelist)) {
  453. printk(KERN_WARNING "hdaps: supported laptop not found!\n");
  454. ret = -ENXIO;
  455. goto out;
  456. }
  457. if (!request_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS, "hdaps")) {
  458. ret = -ENXIO;
  459. goto out;
  460. }
  461. ret = platform_driver_register(&hdaps_driver);
  462. if (ret)
  463. goto out_region;
  464. pdev = platform_device_register_simple("hdaps", -1, NULL, 0);
  465. if (IS_ERR(pdev)) {
  466. ret = PTR_ERR(pdev);
  467. goto out_driver;
  468. }
  469. ret = sysfs_create_group(&pdev->dev.kobj, &hdaps_attribute_group);
  470. if (ret)
  471. goto out_device;
  472. /* initial calibrate for the input device */
  473. hdaps_calibrate();
  474. /* initialize the input class */
  475. hdaps_idev.dev = &pdev->dev;
  476. input_register_device(&hdaps_idev);
  477. /* start up our timer for the input device */
  478. init_timer(&hdaps_timer);
  479. hdaps_timer.function = hdaps_mousedev_poll;
  480. hdaps_timer.expires = jiffies + HDAPS_POLL_PERIOD;
  481. add_timer(&hdaps_timer);
  482. printk(KERN_INFO "hdaps: driver successfully loaded.\n");
  483. return 0;
  484. out_device:
  485. platform_device_unregister(pdev);
  486. out_driver:
  487. platform_driver_unregister(&hdaps_driver);
  488. out_region:
  489. release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
  490. out:
  491. printk(KERN_WARNING "hdaps: driver init failed (ret=%d)!\n", ret);
  492. return ret;
  493. }
  494. static void __exit hdaps_exit(void)
  495. {
  496. del_timer_sync(&hdaps_timer);
  497. input_unregister_device(&hdaps_idev);
  498. sysfs_remove_group(&pdev->dev.kobj, &hdaps_attribute_group);
  499. platform_device_unregister(pdev);
  500. platform_driver_unregister(&hdaps_driver);
  501. release_region(HDAPS_LOW_PORT, HDAPS_NR_PORTS);
  502. printk(KERN_INFO "hdaps: driver unloaded.\n");
  503. }
  504. module_init(hdaps_init);
  505. module_exit(hdaps_exit);
  506. module_param_named(invert, hdaps_invert, bool, 0);
  507. MODULE_PARM_DESC(invert, "invert data along each axis");
  508. MODULE_AUTHOR("Robert Love");
  509. MODULE_DESCRIPTION("IBM Hard Drive Active Protection System (HDAPS) driver");
  510. MODULE_LICENSE("GPL v2");