hp_accel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * hp_accel.c - Interface between LIS3LV02DL driver and HP ACPI BIOS
  3. *
  4. * Copyright (C) 2007-2008 Yan Burman
  5. * Copyright (C) 2008 Eric Piel
  6. * Copyright (C) 2008-2009 Pavel Machek
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/dmi.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/wait.h>
  32. #include <linux/poll.h>
  33. #include <linux/freezer.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/leds.h>
  36. #include <linux/atomic.h>
  37. #include <linux/acpi.h>
  38. #include <linux/i8042.h>
  39. #include <linux/serio.h>
  40. #include "../../misc/lis3lv02d/lis3lv02d.h"
  41. #define DRIVER_NAME "hp_accel"
  42. #define ACPI_MDPS_CLASS "accelerometer"
  43. /* Delayed LEDs infrastructure ------------------------------------ */
  44. /* Special LED class that can defer work */
  45. struct delayed_led_classdev {
  46. struct led_classdev led_classdev;
  47. struct work_struct work;
  48. enum led_brightness new_brightness;
  49. unsigned int led; /* For driver */
  50. void (*set_brightness)(struct delayed_led_classdev *data, enum led_brightness value);
  51. };
  52. static inline void delayed_set_status_worker(struct work_struct *work)
  53. {
  54. struct delayed_led_classdev *data =
  55. container_of(work, struct delayed_led_classdev, work);
  56. data->set_brightness(data, data->new_brightness);
  57. }
  58. static inline void delayed_sysfs_set(struct led_classdev *led_cdev,
  59. enum led_brightness brightness)
  60. {
  61. struct delayed_led_classdev *data = container_of(led_cdev,
  62. struct delayed_led_classdev, led_classdev);
  63. data->new_brightness = brightness;
  64. schedule_work(&data->work);
  65. }
  66. /* HP-specific accelerometer driver ------------------------------------ */
  67. /* e0 25, e0 26, e0 27, e0 28 are scan codes that the accelerometer with acpi id
  68. * HPQ6000 sends through the keyboard bus */
  69. #define ACCEL_1 0x25
  70. #define ACCEL_2 0x26
  71. #define ACCEL_3 0x27
  72. #define ACCEL_4 0x28
  73. /* For automatic insertion of the module */
  74. static const struct acpi_device_id lis3lv02d_device_ids[] = {
  75. {"HPQ0004", 0}, /* HP Mobile Data Protection System PNP */
  76. {"HPQ6000", 0}, /* HP Mobile Data Protection System PNP */
  77. {"HPQ6007", 0}, /* HP Mobile Data Protection System PNP */
  78. {"", 0},
  79. };
  80. MODULE_DEVICE_TABLE(acpi, lis3lv02d_device_ids);
  81. /**
  82. * lis3lv02d_acpi_init - ACPI _INI method: initialize the device.
  83. * @lis3: pointer to the device struct
  84. *
  85. * Returns 0 on success.
  86. */
  87. static int lis3lv02d_acpi_init(struct lis3lv02d *lis3)
  88. {
  89. struct acpi_device *dev = lis3->bus_priv;
  90. if (!lis3->init_required)
  91. return 0;
  92. if (acpi_evaluate_object(dev->handle, METHOD_NAME__INI,
  93. NULL, NULL) != AE_OK)
  94. return -EINVAL;
  95. return 0;
  96. }
  97. /**
  98. * lis3lv02d_acpi_read - ACPI ALRD method: read a register
  99. * @lis3: pointer to the device struct
  100. * @reg: the register to read
  101. * @ret: result of the operation
  102. *
  103. * Returns 0 on success.
  104. */
  105. static int lis3lv02d_acpi_read(struct lis3lv02d *lis3, int reg, u8 *ret)
  106. {
  107. struct acpi_device *dev = lis3->bus_priv;
  108. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  109. struct acpi_object_list args = { 1, &arg0 };
  110. unsigned long long lret;
  111. acpi_status status;
  112. arg0.integer.value = reg;
  113. status = acpi_evaluate_integer(dev->handle, "ALRD", &args, &lret);
  114. if (ACPI_FAILURE(status))
  115. return -EINVAL;
  116. *ret = lret;
  117. return 0;
  118. }
  119. /**
  120. * lis3lv02d_acpi_write - ACPI ALWR method: write to a register
  121. * @lis3: pointer to the device struct
  122. * @reg: the register to write to
  123. * @val: the value to write
  124. *
  125. * Returns 0 on success.
  126. */
  127. static int lis3lv02d_acpi_write(struct lis3lv02d *lis3, int reg, u8 val)
  128. {
  129. struct acpi_device *dev = lis3->bus_priv;
  130. unsigned long long ret; /* Not used when writting */
  131. union acpi_object in_obj[2];
  132. struct acpi_object_list args = { 2, in_obj };
  133. in_obj[0].type = ACPI_TYPE_INTEGER;
  134. in_obj[0].integer.value = reg;
  135. in_obj[1].type = ACPI_TYPE_INTEGER;
  136. in_obj[1].integer.value = val;
  137. if (acpi_evaluate_integer(dev->handle, "ALWR", &args, &ret) != AE_OK)
  138. return -EINVAL;
  139. return 0;
  140. }
  141. static int lis3lv02d_dmi_matched(const struct dmi_system_id *dmi)
  142. {
  143. lis3_dev.ac = *((union axis_conversion *)dmi->driver_data);
  144. pr_info("hardware type %s found\n", dmi->ident);
  145. return 1;
  146. }
  147. /* Represents, for each axis seen by userspace, the corresponding hw axis (+1).
  148. * If the value is negative, the opposite of the hw value is used. */
  149. #define DEFINE_CONV(name, x, y, z) \
  150. static union axis_conversion lis3lv02d_axis_##name = \
  151. { .as_array = { x, y, z } }
  152. DEFINE_CONV(normal, 1, 2, 3);
  153. DEFINE_CONV(y_inverted, 1, -2, 3);
  154. DEFINE_CONV(x_inverted, -1, 2, 3);
  155. DEFINE_CONV(x_inverted_usd, -1, 2, -3);
  156. DEFINE_CONV(z_inverted, 1, 2, -3);
  157. DEFINE_CONV(xy_swap, 2, 1, 3);
  158. DEFINE_CONV(xy_rotated_left, -2, 1, 3);
  159. DEFINE_CONV(xy_rotated_left_usd, -2, 1, -3);
  160. DEFINE_CONV(xy_swap_inverted, -2, -1, 3);
  161. DEFINE_CONV(xy_rotated_right, 2, -1, 3);
  162. DEFINE_CONV(xy_swap_yz_inverted, 2, -1, -3);
  163. #define AXIS_DMI_MATCH(_ident, _name, _axis) { \
  164. .ident = _ident, \
  165. .callback = lis3lv02d_dmi_matched, \
  166. .matches = { \
  167. DMI_MATCH(DMI_PRODUCT_NAME, _name) \
  168. }, \
  169. .driver_data = &lis3lv02d_axis_##_axis \
  170. }
  171. #define AXIS_DMI_MATCH2(_ident, _class1, _name1, \
  172. _class2, _name2, \
  173. _axis) { \
  174. .ident = _ident, \
  175. .callback = lis3lv02d_dmi_matched, \
  176. .matches = { \
  177. DMI_MATCH(DMI_##_class1, _name1), \
  178. DMI_MATCH(DMI_##_class2, _name2), \
  179. }, \
  180. .driver_data = &lis3lv02d_axis_##_axis \
  181. }
  182. static const struct dmi_system_id lis3lv02d_dmi_ids[] = {
  183. /* product names are truncated to match all kinds of a same model */
  184. AXIS_DMI_MATCH("NC64x0", "HP Compaq nc64", x_inverted),
  185. AXIS_DMI_MATCH("NC84x0", "HP Compaq nc84", z_inverted),
  186. AXIS_DMI_MATCH("NX9420", "HP Compaq nx9420", x_inverted),
  187. AXIS_DMI_MATCH("NW9440", "HP Compaq nw9440", x_inverted),
  188. AXIS_DMI_MATCH("NC2510", "HP Compaq 2510", y_inverted),
  189. AXIS_DMI_MATCH("NC2710", "HP Compaq 2710", xy_swap),
  190. AXIS_DMI_MATCH("NC8510", "HP Compaq 8510", xy_swap_inverted),
  191. AXIS_DMI_MATCH("HP2133", "HP 2133", xy_rotated_left),
  192. AXIS_DMI_MATCH("HP2140", "HP 2140", xy_swap_inverted),
  193. AXIS_DMI_MATCH("NC653x", "HP Compaq 653", xy_rotated_left_usd),
  194. AXIS_DMI_MATCH("NC6730b", "HP Compaq 6730b", xy_rotated_left_usd),
  195. AXIS_DMI_MATCH("NC6730s", "HP Compaq 6730s", xy_swap),
  196. AXIS_DMI_MATCH("NC651xx", "HP Compaq 651", xy_rotated_right),
  197. AXIS_DMI_MATCH("NC6710x", "HP Compaq 6710", xy_swap_yz_inverted),
  198. AXIS_DMI_MATCH("NC6715x", "HP Compaq 6715", y_inverted),
  199. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 693", xy_rotated_right),
  200. AXIS_DMI_MATCH("NC693xx", "HP EliteBook 853", xy_swap),
  201. AXIS_DMI_MATCH("NC854xx", "HP EliteBook 854", y_inverted),
  202. AXIS_DMI_MATCH("NC273xx", "HP EliteBook 273", y_inverted),
  203. /* Intel-based HP Pavilion dv5 */
  204. AXIS_DMI_MATCH2("HPDV5_I",
  205. PRODUCT_NAME, "HP Pavilion dv5",
  206. BOARD_NAME, "3603",
  207. x_inverted),
  208. /* AMD-based HP Pavilion dv5 */
  209. AXIS_DMI_MATCH2("HPDV5_A",
  210. PRODUCT_NAME, "HP Pavilion dv5",
  211. BOARD_NAME, "3600",
  212. y_inverted),
  213. AXIS_DMI_MATCH("DV7", "HP Pavilion dv7", x_inverted),
  214. AXIS_DMI_MATCH("HP8710", "HP Compaq 8710", y_inverted),
  215. AXIS_DMI_MATCH("HDX18", "HP HDX 18", x_inverted),
  216. AXIS_DMI_MATCH("HPB432x", "HP ProBook 432", xy_rotated_left),
  217. AXIS_DMI_MATCH("HPB440G3", "HP ProBook 440 G3", x_inverted_usd),
  218. AXIS_DMI_MATCH("HPB440G4", "HP ProBook 440 G4", x_inverted),
  219. AXIS_DMI_MATCH("HPB442x", "HP ProBook 442", xy_rotated_left),
  220. AXIS_DMI_MATCH("HPB452x", "HP ProBook 452", y_inverted),
  221. AXIS_DMI_MATCH("HPB522x", "HP ProBook 522", xy_swap),
  222. AXIS_DMI_MATCH("HPB532x", "HP ProBook 532", y_inverted),
  223. AXIS_DMI_MATCH("HPB655x", "HP ProBook 655", xy_swap_inverted),
  224. AXIS_DMI_MATCH("Mini510x", "HP Mini 510", xy_rotated_left_usd),
  225. AXIS_DMI_MATCH("HPB63xx", "HP ProBook 63", xy_swap),
  226. AXIS_DMI_MATCH("HPB64xx", "HP ProBook 64", xy_swap),
  227. AXIS_DMI_MATCH("HPB64xx", "HP EliteBook 84", xy_swap),
  228. AXIS_DMI_MATCH("HPB65xx", "HP ProBook 65", x_inverted),
  229. AXIS_DMI_MATCH("HPZBook15", "HP ZBook 15", x_inverted),
  230. AXIS_DMI_MATCH("HPZBook17", "HP ZBook 17", xy_swap_yz_inverted),
  231. { NULL, }
  232. /* Laptop models without axis info (yet):
  233. * "NC6910" "HP Compaq 6910"
  234. * "NC2400" "HP Compaq nc2400"
  235. * "NX74x0" "HP Compaq nx74"
  236. * "NX6325" "HP Compaq nx6325"
  237. * "NC4400" "HP Compaq nc4400"
  238. */
  239. };
  240. static void hpled_set(struct delayed_led_classdev *led_cdev, enum led_brightness value)
  241. {
  242. struct acpi_device *dev = lis3_dev.bus_priv;
  243. unsigned long long ret; /* Not used when writing */
  244. union acpi_object in_obj[1];
  245. struct acpi_object_list args = { 1, in_obj };
  246. in_obj[0].type = ACPI_TYPE_INTEGER;
  247. in_obj[0].integer.value = !!value;
  248. acpi_evaluate_integer(dev->handle, "ALED", &args, &ret);
  249. }
  250. static struct delayed_led_classdev hpled_led = {
  251. .led_classdev = {
  252. .name = "hp::hddprotect",
  253. .default_trigger = "none",
  254. .brightness_set = delayed_sysfs_set,
  255. .flags = LED_CORE_SUSPENDRESUME,
  256. },
  257. .set_brightness = hpled_set,
  258. };
  259. static acpi_status
  260. lis3lv02d_get_resource(struct acpi_resource *resource, void *context)
  261. {
  262. if (resource->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  263. struct acpi_resource_extended_irq *irq;
  264. u32 *device_irq = context;
  265. irq = &resource->data.extended_irq;
  266. *device_irq = irq->interrupts[0];
  267. }
  268. return AE_OK;
  269. }
  270. static void lis3lv02d_enum_resources(struct acpi_device *device)
  271. {
  272. acpi_status status;
  273. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  274. lis3lv02d_get_resource, &lis3_dev.irq);
  275. if (ACPI_FAILURE(status))
  276. printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n");
  277. }
  278. static bool hp_accel_i8042_filter(unsigned char data, unsigned char str,
  279. struct serio *port)
  280. {
  281. static bool extended;
  282. if (str & I8042_STR_AUXDATA)
  283. return false;
  284. if (data == 0xe0) {
  285. extended = true;
  286. return true;
  287. } else if (unlikely(extended)) {
  288. extended = false;
  289. switch (data) {
  290. case ACCEL_1:
  291. case ACCEL_2:
  292. case ACCEL_3:
  293. case ACCEL_4:
  294. return true;
  295. default:
  296. serio_interrupt(port, 0xe0, 0);
  297. return false;
  298. }
  299. }
  300. return false;
  301. }
  302. static int lis3lv02d_add(struct acpi_device *device)
  303. {
  304. int ret;
  305. if (!device)
  306. return -EINVAL;
  307. lis3_dev.bus_priv = device;
  308. lis3_dev.init = lis3lv02d_acpi_init;
  309. lis3_dev.read = lis3lv02d_acpi_read;
  310. lis3_dev.write = lis3lv02d_acpi_write;
  311. strcpy(acpi_device_name(device), DRIVER_NAME);
  312. strcpy(acpi_device_class(device), ACPI_MDPS_CLASS);
  313. device->driver_data = &lis3_dev;
  314. /* obtain IRQ number of our device from ACPI */
  315. lis3lv02d_enum_resources(device);
  316. /* If possible use a "standard" axes order */
  317. if (lis3_dev.ac.x && lis3_dev.ac.y && lis3_dev.ac.z) {
  318. pr_info("Using custom axes %d,%d,%d\n",
  319. lis3_dev.ac.x, lis3_dev.ac.y, lis3_dev.ac.z);
  320. } else if (dmi_check_system(lis3lv02d_dmi_ids) == 0) {
  321. pr_info("laptop model unknown, using default axes configuration\n");
  322. lis3_dev.ac = lis3lv02d_axis_normal;
  323. }
  324. /* call the core layer do its init */
  325. lis3_dev.init_required = true;
  326. ret = lis3lv02d_init_device(&lis3_dev);
  327. if (ret)
  328. return ret;
  329. /* filter to remove HPQ6000 accelerometer data
  330. * from keyboard bus stream */
  331. if (strstr(dev_name(&device->dev), "HPQ6000"))
  332. i8042_install_filter(hp_accel_i8042_filter);
  333. INIT_WORK(&hpled_led.work, delayed_set_status_worker);
  334. ret = led_classdev_register(NULL, &hpled_led.led_classdev);
  335. if (ret) {
  336. lis3lv02d_joystick_disable(&lis3_dev);
  337. lis3lv02d_poweroff(&lis3_dev);
  338. flush_work(&hpled_led.work);
  339. return ret;
  340. }
  341. return ret;
  342. }
  343. static int lis3lv02d_remove(struct acpi_device *device)
  344. {
  345. if (!device)
  346. return -EINVAL;
  347. i8042_remove_filter(hp_accel_i8042_filter);
  348. lis3lv02d_joystick_disable(&lis3_dev);
  349. lis3lv02d_poweroff(&lis3_dev);
  350. led_classdev_unregister(&hpled_led.led_classdev);
  351. flush_work(&hpled_led.work);
  352. return lis3lv02d_remove_fs(&lis3_dev);
  353. }
  354. #ifdef CONFIG_PM_SLEEP
  355. static int lis3lv02d_suspend(struct device *dev)
  356. {
  357. /* make sure the device is off when we suspend */
  358. lis3lv02d_poweroff(&lis3_dev);
  359. return 0;
  360. }
  361. static int lis3lv02d_resume(struct device *dev)
  362. {
  363. lis3_dev.init_required = false;
  364. lis3lv02d_poweron(&lis3_dev);
  365. return 0;
  366. }
  367. static int lis3lv02d_restore(struct device *dev)
  368. {
  369. lis3_dev.init_required = true;
  370. lis3lv02d_poweron(&lis3_dev);
  371. return 0;
  372. }
  373. static const struct dev_pm_ops hp_accel_pm = {
  374. .suspend = lis3lv02d_suspend,
  375. .resume = lis3lv02d_resume,
  376. .freeze = lis3lv02d_suspend,
  377. .thaw = lis3lv02d_resume,
  378. .poweroff = lis3lv02d_suspend,
  379. .restore = lis3lv02d_restore,
  380. };
  381. #define HP_ACCEL_PM (&hp_accel_pm)
  382. #else
  383. #define HP_ACCEL_PM NULL
  384. #endif
  385. /* For the HP MDPS aka 3D Driveguard */
  386. static struct acpi_driver lis3lv02d_driver = {
  387. .name = DRIVER_NAME,
  388. .class = ACPI_MDPS_CLASS,
  389. .ids = lis3lv02d_device_ids,
  390. .ops = {
  391. .add = lis3lv02d_add,
  392. .remove = lis3lv02d_remove,
  393. },
  394. .drv.pm = HP_ACCEL_PM,
  395. };
  396. module_acpi_driver(lis3lv02d_driver);
  397. MODULE_DESCRIPTION("Glue between LIS3LV02Dx and HP ACPI BIOS and support for disk protection LED.");
  398. MODULE_AUTHOR("Yan Burman, Eric Piel, Pavel Machek");
  399. MODULE_LICENSE("GPL");