hdaps.c 15 KB

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