hp-wmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * HP WMI hotkeys
  3. *
  4. * Copyright (C) 2008 Red Hat <mjg@redhat.com>
  5. *
  6. * Portions based on wistron_btns.c:
  7. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  8. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  9. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/input.h>
  30. #include <acpi/acpi_drivers.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/acpi.h>
  33. #include <linux/rfkill.h>
  34. #include <linux/string.h>
  35. MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
  36. MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
  37. MODULE_LICENSE("GPL");
  38. MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
  39. MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
  40. #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
  41. #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
  42. #define HPWMI_DISPLAY_QUERY 0x1
  43. #define HPWMI_HDDTEMP_QUERY 0x2
  44. #define HPWMI_ALS_QUERY 0x3
  45. #define HPWMI_DOCK_QUERY 0x4
  46. #define HPWMI_WIRELESS_QUERY 0x5
  47. #define HPWMI_HOTKEY_QUERY 0xc
  48. static int __init hp_wmi_bios_setup(struct platform_device *device);
  49. static int __exit hp_wmi_bios_remove(struct platform_device *device);
  50. struct bios_args {
  51. u32 signature;
  52. u32 command;
  53. u32 commandtype;
  54. u32 datasize;
  55. u32 data;
  56. };
  57. struct bios_return {
  58. u32 sigpass;
  59. u32 return_code;
  60. u32 value;
  61. };
  62. struct key_entry {
  63. char type; /* See KE_* below */
  64. u16 code;
  65. u16 keycode;
  66. };
  67. enum { KE_KEY, KE_SW, KE_END };
  68. static struct key_entry hp_wmi_keymap[] = {
  69. {KE_SW, 0x01, SW_DOCK},
  70. {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
  71. {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
  72. {KE_KEY, 0x20e6, KEY_PROG1},
  73. {KE_KEY, 0x2142, KEY_MEDIA},
  74. {KE_KEY, 0x213b, KEY_INFO},
  75. {KE_KEY, 0x231b, KEY_HELP},
  76. {KE_END, 0}
  77. };
  78. static struct input_dev *hp_wmi_input_dev;
  79. static struct platform_device *hp_wmi_platform_dev;
  80. static struct rfkill *wifi_rfkill;
  81. static struct rfkill *bluetooth_rfkill;
  82. static struct rfkill *wwan_rfkill;
  83. static struct platform_driver hp_wmi_driver = {
  84. .driver = {
  85. .name = "hp-wmi",
  86. .owner = THIS_MODULE,
  87. },
  88. .probe = hp_wmi_bios_setup,
  89. .remove = hp_wmi_bios_remove,
  90. };
  91. static int hp_wmi_perform_query(int query, int write, int value)
  92. {
  93. struct bios_return bios_return;
  94. acpi_status status;
  95. union acpi_object *obj;
  96. struct bios_args args = {
  97. .signature = 0x55434553,
  98. .command = write ? 0x2 : 0x1,
  99. .commandtype = query,
  100. .datasize = write ? 0x4 : 0,
  101. .data = value,
  102. };
  103. struct acpi_buffer input = { sizeof(struct bios_args), &args };
  104. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  105. status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
  106. obj = output.pointer;
  107. if (!obj || obj->type != ACPI_TYPE_BUFFER)
  108. return -EINVAL;
  109. bios_return = *((struct bios_return *)obj->buffer.pointer);
  110. if (bios_return.return_code > 0)
  111. return bios_return.return_code * -1;
  112. else
  113. return bios_return.value;
  114. }
  115. static int hp_wmi_display_state(void)
  116. {
  117. return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
  118. }
  119. static int hp_wmi_hddtemp_state(void)
  120. {
  121. return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
  122. }
  123. static int hp_wmi_als_state(void)
  124. {
  125. return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
  126. }
  127. static int hp_wmi_dock_state(void)
  128. {
  129. return hp_wmi_perform_query(HPWMI_DOCK_QUERY, 0, 0);
  130. }
  131. static int hp_wmi_wifi_set(void *data, enum rfkill_state state)
  132. {
  133. if (state)
  134. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x101);
  135. else
  136. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x100);
  137. }
  138. static int hp_wmi_bluetooth_set(void *data, enum rfkill_state state)
  139. {
  140. if (state)
  141. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x202);
  142. else
  143. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x200);
  144. }
  145. static int hp_wmi_wwan_set(void *data, enum rfkill_state state)
  146. {
  147. if (state)
  148. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x404);
  149. else
  150. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x400);
  151. }
  152. static int hp_wmi_wifi_state(void)
  153. {
  154. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  155. if (wireless & 0x100)
  156. return RFKILL_STATE_UNBLOCKED;
  157. else
  158. return RFKILL_STATE_SOFT_BLOCKED;
  159. }
  160. static int hp_wmi_bluetooth_state(void)
  161. {
  162. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  163. if (wireless & 0x10000)
  164. return RFKILL_STATE_UNBLOCKED;
  165. else
  166. return RFKILL_STATE_SOFT_BLOCKED;
  167. }
  168. static int hp_wmi_wwan_state(void)
  169. {
  170. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  171. if (wireless & 0x1000000)
  172. return RFKILL_STATE_UNBLOCKED;
  173. else
  174. return RFKILL_STATE_SOFT_BLOCKED;
  175. }
  176. static ssize_t show_display(struct device *dev, struct device_attribute *attr,
  177. char *buf)
  178. {
  179. int value = hp_wmi_display_state();
  180. if (value < 0)
  181. return -EINVAL;
  182. return sprintf(buf, "%d\n", value);
  183. }
  184. static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. int value = hp_wmi_hddtemp_state();
  188. if (value < 0)
  189. return -EINVAL;
  190. return sprintf(buf, "%d\n", value);
  191. }
  192. static ssize_t show_als(struct device *dev, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. int value = hp_wmi_als_state();
  196. if (value < 0)
  197. return -EINVAL;
  198. return sprintf(buf, "%d\n", value);
  199. }
  200. static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
  201. char *buf)
  202. {
  203. int value = hp_wmi_dock_state();
  204. if (value < 0)
  205. return -EINVAL;
  206. return sprintf(buf, "%d\n", value);
  207. }
  208. static ssize_t set_als(struct device *dev, struct device_attribute *attr,
  209. const char *buf, size_t count)
  210. {
  211. u32 tmp = simple_strtoul(buf, NULL, 10);
  212. hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
  213. return count;
  214. }
  215. static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
  216. static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
  217. static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
  218. static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
  219. static struct key_entry *hp_wmi_get_entry_by_scancode(int code)
  220. {
  221. struct key_entry *key;
  222. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  223. if (code == key->code)
  224. return key;
  225. return NULL;
  226. }
  227. static struct key_entry *hp_wmi_get_entry_by_keycode(int keycode)
  228. {
  229. struct key_entry *key;
  230. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  231. if (key->type == KE_KEY && keycode == key->keycode)
  232. return key;
  233. return NULL;
  234. }
  235. static int hp_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  236. {
  237. struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
  238. if (key && key->type == KE_KEY) {
  239. *keycode = key->keycode;
  240. return 0;
  241. }
  242. return -EINVAL;
  243. }
  244. static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
  245. {
  246. struct key_entry *key;
  247. int old_keycode;
  248. if (keycode < 0 || keycode > KEY_MAX)
  249. return -EINVAL;
  250. key = hp_wmi_get_entry_by_scancode(scancode);
  251. if (key && key->type == KE_KEY) {
  252. old_keycode = key->keycode;
  253. key->keycode = keycode;
  254. set_bit(keycode, dev->keybit);
  255. if (!hp_wmi_get_entry_by_keycode(old_keycode))
  256. clear_bit(old_keycode, dev->keybit);
  257. return 0;
  258. }
  259. return -EINVAL;
  260. }
  261. static void hp_wmi_notify(u32 value, void *context)
  262. {
  263. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  264. static struct key_entry *key;
  265. union acpi_object *obj;
  266. wmi_get_event_data(value, &response);
  267. obj = (union acpi_object *)response.pointer;
  268. if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 8) {
  269. int eventcode = *((u8 *) obj->buffer.pointer);
  270. if (eventcode == 0x4)
  271. eventcode = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
  272. 0);
  273. key = hp_wmi_get_entry_by_scancode(eventcode);
  274. if (key) {
  275. switch (key->type) {
  276. case KE_KEY:
  277. input_report_key(hp_wmi_input_dev,
  278. key->keycode, 1);
  279. input_sync(hp_wmi_input_dev);
  280. input_report_key(hp_wmi_input_dev,
  281. key->keycode, 0);
  282. input_sync(hp_wmi_input_dev);
  283. break;
  284. case KE_SW:
  285. input_report_switch(hp_wmi_input_dev,
  286. key->keycode,
  287. hp_wmi_dock_state());
  288. input_sync(hp_wmi_input_dev);
  289. break;
  290. }
  291. } else if (eventcode == 0x5) {
  292. if (wifi_rfkill)
  293. rfkill_force_state(wifi_rfkill,
  294. hp_wmi_wifi_state());
  295. if (bluetooth_rfkill)
  296. rfkill_force_state(bluetooth_rfkill,
  297. hp_wmi_bluetooth_state());
  298. if (wwan_rfkill)
  299. rfkill_force_state(wwan_rfkill,
  300. hp_wmi_wwan_state());
  301. } else
  302. printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n",
  303. eventcode);
  304. } else
  305. printk(KERN_INFO "HP WMI: Unknown response received\n");
  306. }
  307. static int __init hp_wmi_input_setup(void)
  308. {
  309. struct key_entry *key;
  310. int err;
  311. hp_wmi_input_dev = input_allocate_device();
  312. hp_wmi_input_dev->name = "HP WMI hotkeys";
  313. hp_wmi_input_dev->phys = "wmi/input0";
  314. hp_wmi_input_dev->id.bustype = BUS_HOST;
  315. hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
  316. hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
  317. for (key = hp_wmi_keymap; key->type != KE_END; key++) {
  318. switch (key->type) {
  319. case KE_KEY:
  320. set_bit(EV_KEY, hp_wmi_input_dev->evbit);
  321. set_bit(key->keycode, hp_wmi_input_dev->keybit);
  322. break;
  323. case KE_SW:
  324. set_bit(EV_SW, hp_wmi_input_dev->evbit);
  325. set_bit(key->keycode, hp_wmi_input_dev->swbit);
  326. break;
  327. }
  328. }
  329. err = input_register_device(hp_wmi_input_dev);
  330. if (err) {
  331. input_free_device(hp_wmi_input_dev);
  332. return err;
  333. }
  334. return 0;
  335. }
  336. static void cleanup_sysfs(struct platform_device *device)
  337. {
  338. device_remove_file(&device->dev, &dev_attr_display);
  339. device_remove_file(&device->dev, &dev_attr_hddtemp);
  340. device_remove_file(&device->dev, &dev_attr_als);
  341. device_remove_file(&device->dev, &dev_attr_dock);
  342. }
  343. static int __init hp_wmi_bios_setup(struct platform_device *device)
  344. {
  345. int err;
  346. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  347. err = device_create_file(&device->dev, &dev_attr_display);
  348. if (err)
  349. goto add_sysfs_error;
  350. err = device_create_file(&device->dev, &dev_attr_hddtemp);
  351. if (err)
  352. goto add_sysfs_error;
  353. err = device_create_file(&device->dev, &dev_attr_als);
  354. if (err)
  355. goto add_sysfs_error;
  356. err = device_create_file(&device->dev, &dev_attr_dock);
  357. if (err)
  358. goto add_sysfs_error;
  359. if (wireless & 0x1) {
  360. wifi_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WLAN);
  361. wifi_rfkill->name = "hp-wifi";
  362. wifi_rfkill->state = hp_wmi_wifi_state();
  363. wifi_rfkill->toggle_radio = hp_wmi_wifi_set;
  364. wifi_rfkill->user_claim_unsupported = 1;
  365. rfkill_register(wifi_rfkill);
  366. }
  367. if (wireless & 0x2) {
  368. bluetooth_rfkill = rfkill_allocate(&device->dev,
  369. RFKILL_TYPE_BLUETOOTH);
  370. bluetooth_rfkill->name = "hp-bluetooth";
  371. bluetooth_rfkill->state = hp_wmi_bluetooth_state();
  372. bluetooth_rfkill->toggle_radio = hp_wmi_bluetooth_set;
  373. bluetooth_rfkill->user_claim_unsupported = 1;
  374. rfkill_register(bluetooth_rfkill);
  375. }
  376. if (wireless & 0x4) {
  377. wwan_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WWAN);
  378. wwan_rfkill->name = "hp-wwan";
  379. wwan_rfkill->state = hp_wmi_wwan_state();
  380. wwan_rfkill->toggle_radio = hp_wmi_wwan_set;
  381. wwan_rfkill->user_claim_unsupported = 1;
  382. rfkill_register(wwan_rfkill);
  383. }
  384. return 0;
  385. add_sysfs_error:
  386. cleanup_sysfs(device);
  387. return err;
  388. }
  389. static int __exit hp_wmi_bios_remove(struct platform_device *device)
  390. {
  391. cleanup_sysfs(device);
  392. if (wifi_rfkill)
  393. rfkill_unregister(wifi_rfkill);
  394. if (bluetooth_rfkill)
  395. rfkill_unregister(bluetooth_rfkill);
  396. if (wwan_rfkill)
  397. rfkill_unregister(wwan_rfkill);
  398. return 0;
  399. }
  400. static int __init hp_wmi_init(void)
  401. {
  402. int err;
  403. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  404. err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
  405. hp_wmi_notify, NULL);
  406. if (!err)
  407. hp_wmi_input_setup();
  408. }
  409. if (wmi_has_guid(HPWMI_BIOS_GUID)) {
  410. err = platform_driver_register(&hp_wmi_driver);
  411. if (err)
  412. return 0;
  413. hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
  414. if (!hp_wmi_platform_dev) {
  415. platform_driver_unregister(&hp_wmi_driver);
  416. return 0;
  417. }
  418. platform_device_add(hp_wmi_platform_dev);
  419. }
  420. return 0;
  421. }
  422. static void __exit hp_wmi_exit(void)
  423. {
  424. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  425. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  426. input_unregister_device(hp_wmi_input_dev);
  427. }
  428. if (hp_wmi_platform_dev) {
  429. platform_device_del(hp_wmi_platform_dev);
  430. platform_driver_unregister(&hp_wmi_driver);
  431. }
  432. }
  433. module_init(hp_wmi_init);
  434. module_exit(hp_wmi_exit);