hp-wmi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/input.h>
  31. #include <acpi/acpi_drivers.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/acpi.h>
  34. #include <linux/rfkill.h>
  35. #include <linux/string.h>
  36. MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
  37. MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
  40. MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
  41. #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
  42. #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
  43. #define HPWMI_DISPLAY_QUERY 0x1
  44. #define HPWMI_HDDTEMP_QUERY 0x2
  45. #define HPWMI_ALS_QUERY 0x3
  46. #define HPWMI_HARDWARE_QUERY 0x4
  47. #define HPWMI_WIRELESS_QUERY 0x5
  48. #define HPWMI_HOTKEY_QUERY 0xc
  49. #define PREFIX "HP WMI: "
  50. #define UNIMP "Unimplemented "
  51. enum hp_wmi_radio {
  52. HPWMI_WIFI = 0,
  53. HPWMI_BLUETOOTH = 1,
  54. HPWMI_WWAN = 2,
  55. };
  56. enum hp_wmi_event_ids {
  57. HPWMI_DOCK_EVENT = 1,
  58. HPWMI_PARK_HDD = 2,
  59. HPWMI_SMART_ADAPTER = 3,
  60. HPWMI_BEZEL_BUTTON = 4,
  61. HPWMI_WIRELESS = 5,
  62. HPWMI_CPU_BATTERY_THROTTLE = 6,
  63. HPWMI_LOCK_SWITCH = 7,
  64. };
  65. static int __devinit hp_wmi_bios_setup(struct platform_device *device);
  66. static int __exit hp_wmi_bios_remove(struct platform_device *device);
  67. static int hp_wmi_resume_handler(struct device *device);
  68. struct bios_args {
  69. u32 signature;
  70. u32 command;
  71. u32 commandtype;
  72. u32 datasize;
  73. char *data;
  74. };
  75. struct bios_return {
  76. u32 sigpass;
  77. u32 return_code;
  78. };
  79. struct key_entry {
  80. char type; /* See KE_* below */
  81. u16 code;
  82. u16 keycode;
  83. };
  84. enum { KE_KEY, KE_END };
  85. static struct key_entry hp_wmi_keymap[] = {
  86. {KE_KEY, 0x02, KEY_BRIGHTNESSUP},
  87. {KE_KEY, 0x03, KEY_BRIGHTNESSDOWN},
  88. {KE_KEY, 0x20e6, KEY_PROG1},
  89. {KE_KEY, 0x20e8, KEY_MEDIA},
  90. {KE_KEY, 0x2142, KEY_MEDIA},
  91. {KE_KEY, 0x213b, KEY_INFO},
  92. {KE_KEY, 0x2169, KEY_DIRECTION},
  93. {KE_KEY, 0x231b, KEY_HELP},
  94. {KE_END, 0}
  95. };
  96. static struct input_dev *hp_wmi_input_dev;
  97. static struct platform_device *hp_wmi_platform_dev;
  98. static struct rfkill *wifi_rfkill;
  99. static struct rfkill *bluetooth_rfkill;
  100. static struct rfkill *wwan_rfkill;
  101. static const struct dev_pm_ops hp_wmi_pm_ops = {
  102. .resume = hp_wmi_resume_handler,
  103. .restore = hp_wmi_resume_handler,
  104. };
  105. static struct platform_driver hp_wmi_driver = {
  106. .driver = {
  107. .name = "hp-wmi",
  108. .owner = THIS_MODULE,
  109. .pm = &hp_wmi_pm_ops,
  110. },
  111. .probe = hp_wmi_bios_setup,
  112. .remove = hp_wmi_bios_remove,
  113. };
  114. /*
  115. * hp_wmi_perform_query
  116. *
  117. * query: The commandtype -> What should be queried
  118. * write: The command -> 0 read, 1 write, 3 ODM specific
  119. * buffer: Buffer used as input and/or output
  120. * buffersize: Size of buffer
  121. *
  122. * returns zero on success
  123. * an HP WMI query specific error code (which is positive)
  124. * -EINVAL if the query was not successful at all
  125. * -EINVAL if the output buffer size exceeds buffersize
  126. *
  127. * Note: The buffersize must at least be the maximum of the input and output
  128. * size. E.g. Battery info query (0x7) is defined to have 1 byte input
  129. * and 128 byte output. The caller would do:
  130. * buffer = kzalloc(128, GFP_KERNEL);
  131. * ret = hp_wmi_perform_query(0x7, 0, buffer, 128)
  132. */
  133. static int hp_wmi_perform_query(int query, int write, char *buffer,
  134. int buffersize)
  135. {
  136. struct bios_return bios_return;
  137. acpi_status status;
  138. union acpi_object *obj;
  139. struct bios_args args = {
  140. .signature = 0x55434553,
  141. .command = write ? 0x2 : 0x1,
  142. .commandtype = query,
  143. .datasize = buffersize,
  144. .data = buffer,
  145. };
  146. struct acpi_buffer input = { sizeof(struct bios_args), &args };
  147. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  148. status = wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
  149. obj = output.pointer;
  150. if (!obj)
  151. return -EINVAL;
  152. else if (obj->type != ACPI_TYPE_BUFFER) {
  153. kfree(obj);
  154. return -EINVAL;
  155. }
  156. bios_return = *((struct bios_return *)obj->buffer.pointer);
  157. if (bios_return.return_code) {
  158. printk(KERN_WARNING PREFIX "Query %d returned %d\n", query,
  159. bios_return.return_code);
  160. kfree(obj);
  161. return bios_return.return_code;
  162. }
  163. if (obj->buffer.length - sizeof(bios_return) > buffersize) {
  164. kfree(obj);
  165. return -EINVAL;
  166. }
  167. memset(buffer, 0, buffersize);
  168. memcpy(buffer,
  169. ((char *)obj->buffer.pointer) + sizeof(struct bios_return),
  170. obj->buffer.length - sizeof(bios_return));
  171. kfree(obj);
  172. return 0;
  173. }
  174. static int hp_wmi_display_state(void)
  175. {
  176. int state;
  177. int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, (char *)&state,
  178. sizeof(state));
  179. if (ret)
  180. return -EINVAL;
  181. return state;
  182. }
  183. static int hp_wmi_hddtemp_state(void)
  184. {
  185. int state;
  186. int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, (char *)&state,
  187. sizeof(state));
  188. if (ret)
  189. return -EINVAL;
  190. return state;
  191. }
  192. static int hp_wmi_als_state(void)
  193. {
  194. int state;
  195. int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, (char *)&state,
  196. sizeof(state));
  197. if (ret)
  198. return -EINVAL;
  199. return state;
  200. }
  201. static int hp_wmi_dock_state(void)
  202. {
  203. int state;
  204. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, (char *)&state,
  205. sizeof(state));
  206. if (ret)
  207. return -EINVAL;
  208. return state & 0x1;
  209. }
  210. static int hp_wmi_tablet_state(void)
  211. {
  212. int state;
  213. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, (char *)&state,
  214. sizeof(state));
  215. if (ret)
  216. return ret;
  217. return (state & 0x4) ? 1 : 0;
  218. }
  219. static int hp_wmi_set_block(void *data, bool blocked)
  220. {
  221. enum hp_wmi_radio r = (enum hp_wmi_radio) data;
  222. int query = BIT(r + 8) | ((!blocked) << r);
  223. int ret;
  224. ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
  225. (char *)&query, sizeof(query));
  226. if (ret)
  227. return -EINVAL;
  228. return 0;
  229. }
  230. static const struct rfkill_ops hp_wmi_rfkill_ops = {
  231. .set_block = hp_wmi_set_block,
  232. };
  233. static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
  234. {
  235. int wireless;
  236. int mask;
  237. hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
  238. (char *)&wireless, sizeof(wireless));
  239. /* TBD: Pass error */
  240. mask = 0x200 << (r * 8);
  241. if (wireless & mask)
  242. return false;
  243. else
  244. return true;
  245. }
  246. static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
  247. {
  248. int wireless;
  249. int mask;
  250. hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
  251. (char *)&wireless, sizeof(wireless));
  252. /* TBD: Pass error */
  253. mask = 0x800 << (r * 8);
  254. if (wireless & mask)
  255. return false;
  256. else
  257. return true;
  258. }
  259. static ssize_t show_display(struct device *dev, struct device_attribute *attr,
  260. char *buf)
  261. {
  262. int value = hp_wmi_display_state();
  263. if (value < 0)
  264. return -EINVAL;
  265. return sprintf(buf, "%d\n", value);
  266. }
  267. static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. int value = hp_wmi_hddtemp_state();
  271. if (value < 0)
  272. return -EINVAL;
  273. return sprintf(buf, "%d\n", value);
  274. }
  275. static ssize_t show_als(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. int value = hp_wmi_als_state();
  279. if (value < 0)
  280. return -EINVAL;
  281. return sprintf(buf, "%d\n", value);
  282. }
  283. static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
  284. char *buf)
  285. {
  286. int value = hp_wmi_dock_state();
  287. if (value < 0)
  288. return -EINVAL;
  289. return sprintf(buf, "%d\n", value);
  290. }
  291. static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
  292. char *buf)
  293. {
  294. int value = hp_wmi_tablet_state();
  295. if (value < 0)
  296. return -EINVAL;
  297. return sprintf(buf, "%d\n", value);
  298. }
  299. static ssize_t set_als(struct device *dev, struct device_attribute *attr,
  300. const char *buf, size_t count)
  301. {
  302. u32 tmp = simple_strtoul(buf, NULL, 10);
  303. int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, (char *)&tmp,
  304. sizeof(tmp));
  305. if (ret)
  306. return -EINVAL;
  307. return count;
  308. }
  309. static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
  310. static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
  311. static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
  312. static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
  313. static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
  314. static struct key_entry *hp_wmi_get_entry_by_scancode(unsigned int code)
  315. {
  316. struct key_entry *key;
  317. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  318. if (code == key->code)
  319. return key;
  320. return NULL;
  321. }
  322. static struct key_entry *hp_wmi_get_entry_by_keycode(unsigned int keycode)
  323. {
  324. struct key_entry *key;
  325. for (key = hp_wmi_keymap; key->type != KE_END; key++)
  326. if (key->type == KE_KEY && keycode == key->keycode)
  327. return key;
  328. return NULL;
  329. }
  330. static int hp_wmi_getkeycode(struct input_dev *dev,
  331. unsigned int scancode, unsigned int *keycode)
  332. {
  333. struct key_entry *key = hp_wmi_get_entry_by_scancode(scancode);
  334. if (key && key->type == KE_KEY) {
  335. *keycode = key->keycode;
  336. return 0;
  337. }
  338. return -EINVAL;
  339. }
  340. static int hp_wmi_setkeycode(struct input_dev *dev,
  341. unsigned int scancode, unsigned int keycode)
  342. {
  343. struct key_entry *key;
  344. unsigned int old_keycode;
  345. key = hp_wmi_get_entry_by_scancode(scancode);
  346. if (key && key->type == KE_KEY) {
  347. old_keycode = key->keycode;
  348. key->keycode = keycode;
  349. set_bit(keycode, dev->keybit);
  350. if (!hp_wmi_get_entry_by_keycode(old_keycode))
  351. clear_bit(old_keycode, dev->keybit);
  352. return 0;
  353. }
  354. return -EINVAL;
  355. }
  356. static void hp_wmi_notify(u32 value, void *context)
  357. {
  358. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  359. static struct key_entry *key;
  360. union acpi_object *obj;
  361. u32 event_id, event_data;
  362. int key_code, ret;
  363. u32 *location;
  364. acpi_status status;
  365. status = wmi_get_event_data(value, &response);
  366. if (status != AE_OK) {
  367. printk(KERN_INFO PREFIX "bad event status 0x%x\n", status);
  368. return;
  369. }
  370. obj = (union acpi_object *)response.pointer;
  371. if (obj || obj->type != ACPI_TYPE_BUFFER) {
  372. printk(KERN_INFO "hp-wmi: Unknown response received %d\n",
  373. obj->type);
  374. kfree(obj);
  375. return;
  376. }
  377. /*
  378. * Depending on ACPI version the concatenation of id and event data
  379. * inside _WED function will result in a 8 or 16 byte buffer.
  380. */
  381. location = (u32 *)obj->buffer.pointer;
  382. if (obj->buffer.length == 8) {
  383. event_id = *location;
  384. event_data = *(location + 1);
  385. } else if (obj->buffer.length == 16) {
  386. event_id = *location;
  387. event_data = *(location + 2);
  388. } else {
  389. printk(KERN_INFO "hp-wmi: Unknown buffer length %d\n",
  390. obj->buffer.length);
  391. kfree(obj);
  392. return;
  393. }
  394. kfree(obj);
  395. switch (event_id) {
  396. case HPWMI_DOCK_EVENT:
  397. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  398. hp_wmi_dock_state());
  399. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  400. hp_wmi_tablet_state());
  401. input_sync(hp_wmi_input_dev);
  402. break;
  403. case HPWMI_PARK_HDD:
  404. break;
  405. case HPWMI_SMART_ADAPTER:
  406. break;
  407. case HPWMI_BEZEL_BUTTON:
  408. ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
  409. (char *)&key_code,
  410. sizeof(key_code));
  411. if (ret)
  412. break;
  413. key = hp_wmi_get_entry_by_scancode(key_code);
  414. if (key) {
  415. switch (key->type) {
  416. case KE_KEY:
  417. input_report_key(hp_wmi_input_dev,
  418. key->keycode, 1);
  419. input_sync(hp_wmi_input_dev);
  420. input_report_key(hp_wmi_input_dev,
  421. key->keycode, 0);
  422. input_sync(hp_wmi_input_dev);
  423. break;
  424. }
  425. } else
  426. printk(KERN_INFO PREFIX "Unknown key code - 0x%x\n",
  427. key_code);
  428. break;
  429. case HPWMI_WIRELESS:
  430. if (wifi_rfkill)
  431. rfkill_set_states(wifi_rfkill,
  432. hp_wmi_get_sw_state(HPWMI_WIFI),
  433. hp_wmi_get_hw_state(HPWMI_WIFI));
  434. if (bluetooth_rfkill)
  435. rfkill_set_states(bluetooth_rfkill,
  436. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  437. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  438. if (wwan_rfkill)
  439. rfkill_set_states(wwan_rfkill,
  440. hp_wmi_get_sw_state(HPWMI_WWAN),
  441. hp_wmi_get_hw_state(HPWMI_WWAN));
  442. break;
  443. case HPWMI_CPU_BATTERY_THROTTLE:
  444. printk(KERN_INFO PREFIX UNIMP "CPU throttle because of 3 Cell"
  445. " battery event detected\n");
  446. break;
  447. case HPWMI_LOCK_SWITCH:
  448. break;
  449. default:
  450. printk(KERN_INFO PREFIX "Unknown event_id - %d - 0x%x\n",
  451. event_id, event_data);
  452. break;
  453. }
  454. }
  455. static int __init hp_wmi_input_setup(void)
  456. {
  457. struct key_entry *key;
  458. int err;
  459. hp_wmi_input_dev = input_allocate_device();
  460. hp_wmi_input_dev->name = "HP WMI hotkeys";
  461. hp_wmi_input_dev->phys = "wmi/input0";
  462. hp_wmi_input_dev->id.bustype = BUS_HOST;
  463. hp_wmi_input_dev->getkeycode = hp_wmi_getkeycode;
  464. hp_wmi_input_dev->setkeycode = hp_wmi_setkeycode;
  465. for (key = hp_wmi_keymap; key->type != KE_END; key++) {
  466. switch (key->type) {
  467. case KE_KEY:
  468. set_bit(EV_KEY, hp_wmi_input_dev->evbit);
  469. set_bit(key->keycode, hp_wmi_input_dev->keybit);
  470. break;
  471. }
  472. }
  473. set_bit(EV_SW, hp_wmi_input_dev->evbit);
  474. set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
  475. set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
  476. /* Set initial hardware state */
  477. input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
  478. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  479. hp_wmi_tablet_state());
  480. input_sync(hp_wmi_input_dev);
  481. err = input_register_device(hp_wmi_input_dev);
  482. if (err) {
  483. input_free_device(hp_wmi_input_dev);
  484. return err;
  485. }
  486. return 0;
  487. }
  488. static void cleanup_sysfs(struct platform_device *device)
  489. {
  490. device_remove_file(&device->dev, &dev_attr_display);
  491. device_remove_file(&device->dev, &dev_attr_hddtemp);
  492. device_remove_file(&device->dev, &dev_attr_als);
  493. device_remove_file(&device->dev, &dev_attr_dock);
  494. device_remove_file(&device->dev, &dev_attr_tablet);
  495. }
  496. static int __devinit hp_wmi_bios_setup(struct platform_device *device)
  497. {
  498. int err;
  499. int wireless;
  500. err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, (char *)&wireless,
  501. sizeof(wireless));
  502. if (err)
  503. return err;
  504. err = device_create_file(&device->dev, &dev_attr_display);
  505. if (err)
  506. goto add_sysfs_error;
  507. err = device_create_file(&device->dev, &dev_attr_hddtemp);
  508. if (err)
  509. goto add_sysfs_error;
  510. err = device_create_file(&device->dev, &dev_attr_als);
  511. if (err)
  512. goto add_sysfs_error;
  513. err = device_create_file(&device->dev, &dev_attr_dock);
  514. if (err)
  515. goto add_sysfs_error;
  516. err = device_create_file(&device->dev, &dev_attr_tablet);
  517. if (err)
  518. goto add_sysfs_error;
  519. if (wireless & 0x1) {
  520. wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
  521. RFKILL_TYPE_WLAN,
  522. &hp_wmi_rfkill_ops,
  523. (void *) HPWMI_WIFI);
  524. rfkill_init_sw_state(wifi_rfkill,
  525. hp_wmi_get_sw_state(HPWMI_WIFI));
  526. rfkill_set_hw_state(wifi_rfkill,
  527. hp_wmi_get_hw_state(HPWMI_WIFI));
  528. err = rfkill_register(wifi_rfkill);
  529. if (err)
  530. goto register_wifi_error;
  531. }
  532. if (wireless & 0x2) {
  533. bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
  534. RFKILL_TYPE_BLUETOOTH,
  535. &hp_wmi_rfkill_ops,
  536. (void *) HPWMI_BLUETOOTH);
  537. rfkill_init_sw_state(bluetooth_rfkill,
  538. hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
  539. rfkill_set_hw_state(bluetooth_rfkill,
  540. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  541. err = rfkill_register(bluetooth_rfkill);
  542. if (err)
  543. goto register_bluetooth_error;
  544. }
  545. if (wireless & 0x4) {
  546. wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
  547. RFKILL_TYPE_WWAN,
  548. &hp_wmi_rfkill_ops,
  549. (void *) HPWMI_WWAN);
  550. rfkill_init_sw_state(wwan_rfkill,
  551. hp_wmi_get_sw_state(HPWMI_WWAN));
  552. rfkill_set_hw_state(wwan_rfkill,
  553. hp_wmi_get_hw_state(HPWMI_WWAN));
  554. err = rfkill_register(wwan_rfkill);
  555. if (err)
  556. goto register_wwan_err;
  557. }
  558. return 0;
  559. register_wwan_err:
  560. rfkill_destroy(wwan_rfkill);
  561. if (bluetooth_rfkill)
  562. rfkill_unregister(bluetooth_rfkill);
  563. register_bluetooth_error:
  564. rfkill_destroy(bluetooth_rfkill);
  565. if (wifi_rfkill)
  566. rfkill_unregister(wifi_rfkill);
  567. register_wifi_error:
  568. rfkill_destroy(wifi_rfkill);
  569. add_sysfs_error:
  570. cleanup_sysfs(device);
  571. return err;
  572. }
  573. static int __exit hp_wmi_bios_remove(struct platform_device *device)
  574. {
  575. cleanup_sysfs(device);
  576. if (wifi_rfkill) {
  577. rfkill_unregister(wifi_rfkill);
  578. rfkill_destroy(wifi_rfkill);
  579. }
  580. if (bluetooth_rfkill) {
  581. rfkill_unregister(bluetooth_rfkill);
  582. rfkill_destroy(bluetooth_rfkill);
  583. }
  584. if (wwan_rfkill) {
  585. rfkill_unregister(wwan_rfkill);
  586. rfkill_destroy(wwan_rfkill);
  587. }
  588. return 0;
  589. }
  590. static int hp_wmi_resume_handler(struct device *device)
  591. {
  592. /*
  593. * Hardware state may have changed while suspended, so trigger
  594. * input events for the current state. As this is a switch,
  595. * the input layer will only actually pass it on if the state
  596. * changed.
  597. */
  598. if (hp_wmi_input_dev) {
  599. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  600. hp_wmi_dock_state());
  601. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  602. hp_wmi_tablet_state());
  603. input_sync(hp_wmi_input_dev);
  604. }
  605. if (wifi_rfkill)
  606. rfkill_set_states(wifi_rfkill,
  607. hp_wmi_get_sw_state(HPWMI_WIFI),
  608. hp_wmi_get_hw_state(HPWMI_WIFI));
  609. if (bluetooth_rfkill)
  610. rfkill_set_states(bluetooth_rfkill,
  611. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  612. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  613. if (wwan_rfkill)
  614. rfkill_set_states(wwan_rfkill,
  615. hp_wmi_get_sw_state(HPWMI_WWAN),
  616. hp_wmi_get_hw_state(HPWMI_WWAN));
  617. return 0;
  618. }
  619. static int __init hp_wmi_init(void)
  620. {
  621. int err;
  622. int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
  623. int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
  624. if (event_capable) {
  625. err = wmi_install_notify_handler(HPWMI_EVENT_GUID,
  626. hp_wmi_notify, NULL);
  627. if (ACPI_FAILURE(err))
  628. return -EINVAL;
  629. err = hp_wmi_input_setup();
  630. if (err) {
  631. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  632. return err;
  633. }
  634. }
  635. if (bios_capable) {
  636. err = platform_driver_register(&hp_wmi_driver);
  637. if (err)
  638. goto err_driver_reg;
  639. hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
  640. if (!hp_wmi_platform_dev) {
  641. err = -ENOMEM;
  642. goto err_device_alloc;
  643. }
  644. err = platform_device_add(hp_wmi_platform_dev);
  645. if (err)
  646. goto err_device_add;
  647. }
  648. if (!bios_capable && !event_capable)
  649. return -ENODEV;
  650. return 0;
  651. err_device_add:
  652. platform_device_put(hp_wmi_platform_dev);
  653. err_device_alloc:
  654. platform_driver_unregister(&hp_wmi_driver);
  655. err_driver_reg:
  656. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  657. input_unregister_device(hp_wmi_input_dev);
  658. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  659. }
  660. return err;
  661. }
  662. static void __exit hp_wmi_exit(void)
  663. {
  664. if (wmi_has_guid(HPWMI_EVENT_GUID)) {
  665. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  666. input_unregister_device(hp_wmi_input_dev);
  667. }
  668. if (hp_wmi_platform_dev) {
  669. platform_device_unregister(hp_wmi_platform_dev);
  670. platform_driver_unregister(&hp_wmi_driver);
  671. }
  672. }
  673. module_init(hp_wmi_init);
  674. module_exit(hp_wmi_exit);