hp-wmi.c 19 KB

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