hdaps.c 16 KB

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