hp-wmi.c 18 KB

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