hp-wmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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_HARDWARE_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. static int hp_wmi_resume_handler(struct platform_device *device);
  51. struct bios_args {
  52. u32 signature;
  53. u32 command;
  54. u32 commandtype;
  55. u32 datasize;
  56. u32 data;
  57. };
  58. struct bios_return {
  59. u32 sigpass;
  60. u32 return_code;
  61. u32 value;
  62. };
  63. struct key_entry {
  64. char type; /* See KE_* below */
  65. u16 code;
  66. u16 keycode;
  67. };
  68. enum { KE_KEY, KE_END };
  69. static struct key_entry hp_wmi_keymap[] = {
  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. .resume = hp_wmi_resume_handler,
  91. };
  92. static int hp_wmi_perform_query(int query, int write, int value)
  93. {
  94. struct bios_return bios_return;
  95. acpi_status status;
  96. union acpi_object *obj;
  97. struct bios_args args = {
  98. .signature = 0x55434553,
  99. .command = write ? 0x2 : 0x1,
  100. .commandtype = query,
  101. .datasize = write ? 0x4 : 0,
  102. .data = value,
  103. };
  104. struct acpi_buffer input = { sizeof(struct bios_args), &args };
  105. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  106. status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
  107. obj = output.pointer;
  108. if (!obj || obj->type != ACPI_TYPE_BUFFER)
  109. return -EINVAL;
  110. bios_return = *((struct bios_return *)obj->buffer.pointer);
  111. if (bios_return.return_code > 0)
  112. return bios_return.return_code * -1;
  113. else
  114. return bios_return.value;
  115. }
  116. static int hp_wmi_display_state(void)
  117. {
  118. return hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, 0);
  119. }
  120. static int hp_wmi_hddtemp_state(void)
  121. {
  122. return hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, 0);
  123. }
  124. static int hp_wmi_als_state(void)
  125. {
  126. return hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, 0);
  127. }
  128. static int hp_wmi_dock_state(void)
  129. {
  130. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
  131. if (ret < 0)
  132. return ret;
  133. return ret & 0x1;
  134. }
  135. static int hp_wmi_tablet_state(void)
  136. {
  137. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, 0);
  138. if (ret < 0)
  139. return ret;
  140. return (ret & 0x4) ? 1 : 0;
  141. }
  142. static int hp_wmi_wifi_set(void *data, enum rfkill_state state)
  143. {
  144. if (state)
  145. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x101);
  146. else
  147. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x100);
  148. }
  149. static int hp_wmi_bluetooth_set(void *data, enum rfkill_state state)
  150. {
  151. if (state)
  152. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x202);
  153. else
  154. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x200);
  155. }
  156. static int hp_wmi_wwan_set(void *data, enum rfkill_state state)
  157. {
  158. if (state)
  159. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x404);
  160. else
  161. return hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, 0x400);
  162. }
  163. static int hp_wmi_wifi_state(void)
  164. {
  165. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  166. if (wireless & 0x100)
  167. return RFKILL_STATE_UNBLOCKED;
  168. else
  169. return RFKILL_STATE_SOFT_BLOCKED;
  170. }
  171. static int hp_wmi_bluetooth_state(void)
  172. {
  173. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  174. if (wireless & 0x10000)
  175. return RFKILL_STATE_UNBLOCKED;
  176. else
  177. return RFKILL_STATE_SOFT_BLOCKED;
  178. }
  179. static int hp_wmi_wwan_state(void)
  180. {
  181. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  182. if (wireless & 0x1000000)
  183. return RFKILL_STATE_UNBLOCKED;
  184. else
  185. return RFKILL_STATE_SOFT_BLOCKED;
  186. }
  187. static ssize_t show_display(struct device *dev, struct device_attribute *attr,
  188. char *buf)
  189. {
  190. int value = hp_wmi_display_state();
  191. if (value < 0)
  192. return -EINVAL;
  193. return sprintf(buf, "%d\n", value);
  194. }
  195. static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
  196. char *buf)
  197. {
  198. int value = hp_wmi_hddtemp_state();
  199. if (value < 0)
  200. return -EINVAL;
  201. return sprintf(buf, "%d\n", value);
  202. }
  203. static ssize_t show_als(struct device *dev, struct device_attribute *attr,
  204. char *buf)
  205. {
  206. int value = hp_wmi_als_state();
  207. if (value < 0)
  208. return -EINVAL;
  209. return sprintf(buf, "%d\n", value);
  210. }
  211. static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
  212. char *buf)
  213. {
  214. int value = hp_wmi_dock_state();
  215. if (value < 0)
  216. return -EINVAL;
  217. return sprintf(buf, "%d\n", value);
  218. }
  219. static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
  220. char *buf)
  221. {
  222. int value = hp_wmi_tablet_state();
  223. if (value < 0)
  224. return -EINVAL;
  225. return sprintf(buf, "%d\n", value);
  226. }
  227. static ssize_t set_als(struct device *dev, struct device_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. u32 tmp = simple_strtoul(buf, NULL, 10);
  231. hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, tmp);
  232. return count;
  233. }
  234. static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
  235. static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
  236. static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
  237. static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
  238. static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
  239. static struct key_entry *hp_wmi_get_entry_by_scancode(int code)
  240. {
  241. struct key_entry *key;
  242. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  243. if (code == key->code)
  244. return key;
  245. return NULL;
  246. }
  247. static struct key_entry *hp_wmi_get_entry_by_keycode(int keycode)
  248. {
  249. struct key_entry *key;
  250. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  251. if (key->type == KE_KEY && keycode == key->keycode)
  252. return key;
  253. return NULL;
  254. }
  255. static int hp_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  256. {
  257. struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
  258. if (key && key->type == KE_KEY) {
  259. *keycode = key->keycode;
  260. return 0;
  261. }
  262. return -EINVAL;
  263. }
  264. static int hp_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
  265. {
  266. struct key_entry *key;
  267. int old_keycode;
  268. if (keycode < 0 || keycode > KEY_MAX)
  269. return -EINVAL;
  270. key = hp_wmi_get_entry_by_scancode(scancode);
  271. if (key && key->type == KE_KEY) {
  272. old_keycode = key->keycode;
  273. key->keycode = keycode;
  274. set_bit(keycode, dev->keybit);
  275. if (!hp_wmi_get_entry_by_keycode(old_keycode))
  276. clear_bit(old_keycode, dev->keybit);
  277. return 0;
  278. }
  279. return -EINVAL;
  280. }
  281. static void hp_wmi_notify(u32 value, void *context)
  282. {
  283. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  284. static struct key_entry *key;
  285. union acpi_object *obj;
  286. wmi_get_event_data(value, &response);
  287. obj = (union acpi_object *)response.pointer;
  288. if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 8) {
  289. int eventcode = *((u8 *) obj->buffer.pointer);
  290. if (eventcode == 0x4)
  291. eventcode = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
  292. 0);
  293. key = hp_wmi_get_entry_by_scancode(eventcode);
  294. if (key) {
  295. switch (key->type) {
  296. case KE_KEY:
  297. input_report_key(hp_wmi_input_dev,
  298. key->keycode, 1);
  299. input_sync(hp_wmi_input_dev);
  300. input_report_key(hp_wmi_input_dev,
  301. key->keycode, 0);
  302. input_sync(hp_wmi_input_dev);
  303. break;
  304. }
  305. } else if (eventcode == 0x1) {
  306. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  307. hp_wmi_dock_state());
  308. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  309. hp_wmi_tablet_state());
  310. input_sync(hp_wmi_input_dev);
  311. } else if (eventcode == 0x5) {
  312. if (wifi_rfkill)
  313. rfkill_force_state(wifi_rfkill,
  314. hp_wmi_wifi_state());
  315. if (bluetooth_rfkill)
  316. rfkill_force_state(bluetooth_rfkill,
  317. hp_wmi_bluetooth_state());
  318. if (wwan_rfkill)
  319. rfkill_force_state(wwan_rfkill,
  320. hp_wmi_wwan_state());
  321. } else
  322. printk(KERN_INFO "HP WMI: Unknown key pressed - %x\n",
  323. eventcode);
  324. } else
  325. printk(KERN_INFO "HP WMI: Unknown response received\n");
  326. }
  327. static int __init hp_wmi_input_setup(void)
  328. {
  329. struct key_entry *key;
  330. int err;
  331. hp_wmi_input_dev = input_allocate_device();
  332. hp_wmi_input_dev->name = "HP WMI hotkeys";
  333. hp_wmi_input_dev->phys = "wmi/input0";
  334. hp_wmi_input_dev->id.bustype = BUS_HOST;
  335. hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
  336. hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
  337. for (key = hp_wmi_keymap; key->type != KE_END; key++) {
  338. switch (key->type) {
  339. case KE_KEY:
  340. set_bit(EV_KEY, hp_wmi_input_dev->evbit);
  341. set_bit(key->keycode, hp_wmi_input_dev->keybit);
  342. break;
  343. }
  344. }
  345. set_bit(EV_SW, hp_wmi_input_dev->evbit);
  346. set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
  347. set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
  348. /* Set initial hardware state */
  349. input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
  350. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  351. hp_wmi_tablet_state());
  352. input_sync(hp_wmi_input_dev);
  353. err = input_register_device(hp_wmi_input_dev);
  354. if (err) {
  355. input_free_device(hp_wmi_input_dev);
  356. return err;
  357. }
  358. return 0;
  359. }
  360. static void cleanup_sysfs(struct platform_device *device)
  361. {
  362. device_remove_file(&device->dev, &dev_attr_display);
  363. device_remove_file(&device->dev, &dev_attr_hddtemp);
  364. device_remove_file(&device->dev, &dev_attr_als);
  365. device_remove_file(&device->dev, &dev_attr_dock);
  366. device_remove_file(&device->dev, &dev_attr_tablet);
  367. }
  368. static int __init hp_wmi_bios_setup(struct platform_device *device)
  369. {
  370. int err;
  371. int wireless = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, 0);
  372. err = device_create_file(&device->dev, &dev_attr_display);
  373. if (err)
  374. goto add_sysfs_error;
  375. err = device_create_file(&device->dev, &dev_attr_hddtemp);
  376. if (err)
  377. goto add_sysfs_error;
  378. err = device_create_file(&device->dev, &dev_attr_als);
  379. if (err)
  380. goto add_sysfs_error;
  381. err = device_create_file(&device->dev, &dev_attr_dock);
  382. if (err)
  383. goto add_sysfs_error;
  384. err = device_create_file(&device->dev, &dev_attr_tablet);
  385. if (err)
  386. goto add_sysfs_error;
  387. if (wireless & 0x1) {
  388. wifi_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WLAN);
  389. wifi_rfkill->name = "hp-wifi";
  390. wifi_rfkill->state = hp_wmi_wifi_state();
  391. wifi_rfkill->toggle_radio = hp_wmi_wifi_set;
  392. wifi_rfkill->user_claim_unsupported = 1;
  393. err = rfkill_register(wifi_rfkill);
  394. if (err)
  395. goto add_sysfs_error;
  396. }
  397. if (wireless & 0x2) {
  398. bluetooth_rfkill = rfkill_allocate(&device->dev,
  399. RFKILL_TYPE_BLUETOOTH);
  400. bluetooth_rfkill->name = "hp-bluetooth";
  401. bluetooth_rfkill->state = hp_wmi_bluetooth_state();
  402. bluetooth_rfkill->toggle_radio = hp_wmi_bluetooth_set;
  403. bluetooth_rfkill->user_claim_unsupported = 1;
  404. err = rfkill_register(bluetooth_rfkill);
  405. if (err)
  406. goto register_bluetooth_error;
  407. }
  408. if (wireless & 0x4) {
  409. wwan_rfkill = rfkill_allocate(&device->dev, RFKILL_TYPE_WWAN);
  410. wwan_rfkill->name = "hp-wwan";
  411. wwan_rfkill->state = hp_wmi_wwan_state();
  412. wwan_rfkill->toggle_radio = hp_wmi_wwan_set;
  413. wwan_rfkill->user_claim_unsupported = 1;
  414. err = rfkill_register(wwan_rfkill);
  415. if (err)
  416. goto register_wwan_err;
  417. }
  418. return 0;
  419. register_wwan_err:
  420. if (bluetooth_rfkill)
  421. rfkill_unregister(bluetooth_rfkill);
  422. register_bluetooth_error:
  423. if (wifi_rfkill)
  424. rfkill_unregister(wifi_rfkill);
  425. add_sysfs_error:
  426. cleanup_sysfs(device);
  427. return err;
  428. }
  429. static int __exit hp_wmi_bios_remove(struct platform_device *device)
  430. {
  431. cleanup_sysfs(device);
  432. if (wifi_rfkill)
  433. rfkill_unregister(wifi_rfkill);
  434. if (bluetooth_rfkill)
  435. rfkill_unregister(bluetooth_rfkill);
  436. if (wwan_rfkill)
  437. rfkill_unregister(wwan_rfkill);
  438. return 0;
  439. }
  440. static int hp_wmi_resume_handler(struct platform_device *device)
  441. {
  442. /*
  443. * Hardware state may have changed while suspended, so trigger
  444. * input events for the current state. As this is a switch,
  445. * the input layer will only actually pass it on if the state
  446. * changed.
  447. */
  448. input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
  449. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  450. hp_wmi_tablet_state());
  451. input_sync(hp_wmi_input_dev);
  452. return 0;
  453. }
  454. static int __init hp_wmi_init(void)
  455. {
  456. int err;
  457. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  458. err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
  459. hp_wmi_notify, NULL);
  460. if (!err)
  461. hp_wmi_input_setup();
  462. }
  463. if (wmi_has_guid(HPWMI_BIOS_GUID)) {
  464. err = platform_driver_register(&hp_wmi_driver);
  465. if (err)
  466. return 0;
  467. hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
  468. if (!hp_wmi_platform_dev) {
  469. platform_driver_unregister(&hp_wmi_driver);
  470. return 0;
  471. }
  472. platform_device_add(hp_wmi_platform_dev);
  473. }
  474. return 0;
  475. }
  476. static void __exit hp_wmi_exit(void)
  477. {
  478. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  479. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  480. input_unregister_device(hp_wmi_input_dev);
  481. }
  482. if (hp_wmi_platform_dev) {
  483. platform_device_del(hp_wmi_platform_dev);
  484. platform_driver_unregister(&hp_wmi_driver);
  485. }
  486. }
  487. module_init(hp_wmi_init);
  488. module_exit(hp_wmi_exit);