hp-wmi.c 14 KB

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