hp-wmi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * HP WMI hotkeys
  3. *
  4. * Copyright (C) 2008 Red Hat <mjg@redhat.com>
  5. * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
  6. *
  7. * Portions based on wistron_btns.c:
  8. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  9. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  10. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/input.h>
  33. #include <linux/input/sparse-keymap.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/acpi.h>
  36. #include <linux/rfkill.h>
  37. #include <linux/string.h>
  38. MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
  39. MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
  40. MODULE_LICENSE("GPL");
  41. MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
  42. MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
  43. #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
  44. #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
  45. #define HPWMI_DISPLAY_QUERY 0x1
  46. #define HPWMI_HDDTEMP_QUERY 0x2
  47. #define HPWMI_ALS_QUERY 0x3
  48. #define HPWMI_HARDWARE_QUERY 0x4
  49. #define HPWMI_WIRELESS_QUERY 0x5
  50. #define HPWMI_HOTKEY_QUERY 0xc
  51. #define HPWMI_WIRELESS2_QUERY 0x1b
  52. enum hp_wmi_radio {
  53. HPWMI_WIFI = 0,
  54. HPWMI_BLUETOOTH = 1,
  55. HPWMI_WWAN = 2,
  56. };
  57. enum hp_wmi_event_ids {
  58. HPWMI_DOCK_EVENT = 1,
  59. HPWMI_PARK_HDD = 2,
  60. HPWMI_SMART_ADAPTER = 3,
  61. HPWMI_BEZEL_BUTTON = 4,
  62. HPWMI_WIRELESS = 5,
  63. HPWMI_CPU_BATTERY_THROTTLE = 6,
  64. HPWMI_LOCK_SWITCH = 7,
  65. };
  66. static int __devinit hp_wmi_bios_setup(struct platform_device *device);
  67. static int __exit hp_wmi_bios_remove(struct platform_device *device);
  68. static int hp_wmi_resume_handler(struct device *device);
  69. struct bios_args {
  70. u32 signature;
  71. u32 command;
  72. u32 commandtype;
  73. u32 datasize;
  74. u32 data;
  75. };
  76. struct bios_return {
  77. u32 sigpass;
  78. u32 return_code;
  79. };
  80. enum hp_return_value {
  81. HPWMI_RET_WRONG_SIGNATURE = 0x02,
  82. HPWMI_RET_UNKNOWN_COMMAND = 0x03,
  83. HPWMI_RET_UNKNOWN_CMDTYPE = 0x04,
  84. HPWMI_RET_INVALID_PARAMETERS = 0x05,
  85. };
  86. enum hp_wireless2_bits {
  87. HPWMI_POWER_STATE = 0x01,
  88. HPWMI_POWER_SOFT = 0x02,
  89. HPWMI_POWER_BIOS = 0x04,
  90. HPWMI_POWER_HARD = 0x08,
  91. };
  92. #define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
  93. != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
  94. #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
  95. struct bios_rfkill2_device_state {
  96. u8 radio_type;
  97. u8 bus_type;
  98. u16 vendor_id;
  99. u16 product_id;
  100. u16 subsys_vendor_id;
  101. u16 subsys_product_id;
  102. u8 rfkill_id;
  103. u8 power;
  104. u8 unknown[4];
  105. };
  106. /* 7 devices fit into the 128 byte buffer */
  107. #define HPWMI_MAX_RFKILL2_DEVICES 7
  108. struct bios_rfkill2_state {
  109. u8 unknown[7];
  110. u8 count;
  111. u8 pad[8];
  112. struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
  113. };
  114. static const struct key_entry hp_wmi_keymap[] = {
  115. { KE_KEY, 0x02, { KEY_BRIGHTNESSUP } },
  116. { KE_KEY, 0x03, { KEY_BRIGHTNESSDOWN } },
  117. { KE_KEY, 0x20e6, { KEY_PROG1 } },
  118. { KE_KEY, 0x20e8, { KEY_MEDIA } },
  119. { KE_KEY, 0x2142, { KEY_MEDIA } },
  120. { KE_KEY, 0x213b, { KEY_INFO } },
  121. { KE_KEY, 0x2169, { KEY_DIRECTION } },
  122. { KE_KEY, 0x231b, { KEY_HELP } },
  123. { KE_END, 0 }
  124. };
  125. static struct input_dev *hp_wmi_input_dev;
  126. static struct platform_device *hp_wmi_platform_dev;
  127. static struct rfkill *wifi_rfkill;
  128. static struct rfkill *bluetooth_rfkill;
  129. static struct rfkill *wwan_rfkill;
  130. struct rfkill2_device {
  131. u8 id;
  132. int num;
  133. struct rfkill *rfkill;
  134. };
  135. static int rfkill2_count;
  136. static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
  137. static const struct dev_pm_ops hp_wmi_pm_ops = {
  138. .resume = hp_wmi_resume_handler,
  139. .restore = hp_wmi_resume_handler,
  140. };
  141. static struct platform_driver hp_wmi_driver = {
  142. .driver = {
  143. .name = "hp-wmi",
  144. .owner = THIS_MODULE,
  145. .pm = &hp_wmi_pm_ops,
  146. },
  147. .probe = hp_wmi_bios_setup,
  148. .remove = hp_wmi_bios_remove,
  149. };
  150. /*
  151. * hp_wmi_perform_query
  152. *
  153. * query: The commandtype -> What should be queried
  154. * write: The command -> 0 read, 1 write, 3 ODM specific
  155. * buffer: Buffer used as input and/or output
  156. * insize: Size of input buffer
  157. * outsize: Size of output buffer
  158. *
  159. * returns zero on success
  160. * an HP WMI query specific error code (which is positive)
  161. * -EINVAL if the query was not successful at all
  162. * -EINVAL if the output buffer size exceeds buffersize
  163. *
  164. * Note: The buffersize must at least be the maximum of the input and output
  165. * size. E.g. Battery info query (0x7) is defined to have 1 byte input
  166. * and 128 byte output. The caller would do:
  167. * buffer = kzalloc(128, GFP_KERNEL);
  168. * ret = hp_wmi_perform_query(0x7, 0, buffer, 1, 128)
  169. */
  170. static int hp_wmi_perform_query(int query, int write, void *buffer,
  171. int insize, int outsize)
  172. {
  173. struct bios_return *bios_return;
  174. int actual_outsize;
  175. union acpi_object *obj;
  176. struct bios_args args = {
  177. .signature = 0x55434553,
  178. .command = write ? 0x2 : 0x1,
  179. .commandtype = query,
  180. .datasize = insize,
  181. .data = 0,
  182. };
  183. struct acpi_buffer input = { sizeof(struct bios_args), &args };
  184. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  185. u32 rc;
  186. if (WARN_ON(insize > sizeof(args.data)))
  187. return -EINVAL;
  188. memcpy(&args.data, buffer, insize);
  189. wmi_evaluate_method(HPWMI_BIOS_GUID, 0, 0x3, &input, &output);
  190. obj = output.pointer;
  191. if (!obj)
  192. return -EINVAL;
  193. else if (obj->type != ACPI_TYPE_BUFFER) {
  194. kfree(obj);
  195. return -EINVAL;
  196. }
  197. bios_return = (struct bios_return *)obj->buffer.pointer;
  198. rc = bios_return->return_code;
  199. if (rc) {
  200. if (rc != HPWMI_RET_UNKNOWN_CMDTYPE)
  201. pr_warn("query 0x%x returned error 0x%x\n", query, rc);
  202. kfree(obj);
  203. return rc;
  204. }
  205. if (!outsize) {
  206. /* ignore output data */
  207. kfree(obj);
  208. return 0;
  209. }
  210. actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
  211. memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
  212. memset(buffer + actual_outsize, 0, outsize - actual_outsize);
  213. kfree(obj);
  214. return 0;
  215. }
  216. static int hp_wmi_display_state(void)
  217. {
  218. int state = 0;
  219. int ret = hp_wmi_perform_query(HPWMI_DISPLAY_QUERY, 0, &state,
  220. sizeof(state), sizeof(state));
  221. if (ret)
  222. return -EINVAL;
  223. return state;
  224. }
  225. static int hp_wmi_hddtemp_state(void)
  226. {
  227. int state = 0;
  228. int ret = hp_wmi_perform_query(HPWMI_HDDTEMP_QUERY, 0, &state,
  229. sizeof(state), sizeof(state));
  230. if (ret)
  231. return -EINVAL;
  232. return state;
  233. }
  234. static int hp_wmi_als_state(void)
  235. {
  236. int state = 0;
  237. int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 0, &state,
  238. sizeof(state), sizeof(state));
  239. if (ret)
  240. return -EINVAL;
  241. return state;
  242. }
  243. static int hp_wmi_dock_state(void)
  244. {
  245. int state = 0;
  246. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
  247. sizeof(state), sizeof(state));
  248. if (ret)
  249. return -EINVAL;
  250. return state & 0x1;
  251. }
  252. static int hp_wmi_tablet_state(void)
  253. {
  254. int state = 0;
  255. int ret = hp_wmi_perform_query(HPWMI_HARDWARE_QUERY, 0, &state,
  256. sizeof(state), sizeof(state));
  257. if (ret)
  258. return ret;
  259. return (state & 0x4) ? 1 : 0;
  260. }
  261. static int hp_wmi_set_block(void *data, bool blocked)
  262. {
  263. enum hp_wmi_radio r = (enum hp_wmi_radio) data;
  264. int query = BIT(r + 8) | ((!blocked) << r);
  265. int ret;
  266. ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1,
  267. &query, sizeof(query), 0);
  268. if (ret)
  269. return -EINVAL;
  270. return 0;
  271. }
  272. static const struct rfkill_ops hp_wmi_rfkill_ops = {
  273. .set_block = hp_wmi_set_block,
  274. };
  275. static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
  276. {
  277. int wireless = 0;
  278. int mask;
  279. hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
  280. &wireless, sizeof(wireless),
  281. sizeof(wireless));
  282. /* TBD: Pass error */
  283. mask = 0x200 << (r * 8);
  284. if (wireless & mask)
  285. return false;
  286. else
  287. return true;
  288. }
  289. static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
  290. {
  291. int wireless = 0;
  292. int mask;
  293. hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0,
  294. &wireless, sizeof(wireless),
  295. sizeof(wireless));
  296. /* TBD: Pass error */
  297. mask = 0x800 << (r * 8);
  298. if (wireless & mask)
  299. return false;
  300. else
  301. return true;
  302. }
  303. static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
  304. {
  305. int rfkill_id = (int)(long)data;
  306. char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
  307. if (hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 1,
  308. buffer, sizeof(buffer), 0))
  309. return -EINVAL;
  310. return 0;
  311. }
  312. static const struct rfkill_ops hp_wmi_rfkill2_ops = {
  313. .set_block = hp_wmi_rfkill2_set_block,
  314. };
  315. static int hp_wmi_rfkill2_refresh(void)
  316. {
  317. int err, i;
  318. struct bios_rfkill2_state state;
  319. err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
  320. 0, sizeof(state));
  321. if (err)
  322. return err;
  323. for (i = 0; i < rfkill2_count; i++) {
  324. int num = rfkill2[i].num;
  325. struct bios_rfkill2_device_state *devstate;
  326. devstate = &state.device[num];
  327. if (num >= state.count ||
  328. devstate->rfkill_id != rfkill2[i].id) {
  329. pr_warn("power configuration of the wireless devices unexpectedly changed\n");
  330. continue;
  331. }
  332. rfkill_set_states(rfkill2[i].rfkill,
  333. IS_SWBLOCKED(devstate->power),
  334. IS_HWBLOCKED(devstate->power));
  335. }
  336. return 0;
  337. }
  338. static ssize_t show_display(struct device *dev, struct device_attribute *attr,
  339. char *buf)
  340. {
  341. int value = hp_wmi_display_state();
  342. if (value < 0)
  343. return -EINVAL;
  344. return sprintf(buf, "%d\n", value);
  345. }
  346. static ssize_t show_hddtemp(struct device *dev, struct device_attribute *attr,
  347. char *buf)
  348. {
  349. int value = hp_wmi_hddtemp_state();
  350. if (value < 0)
  351. return -EINVAL;
  352. return sprintf(buf, "%d\n", value);
  353. }
  354. static ssize_t show_als(struct device *dev, struct device_attribute *attr,
  355. char *buf)
  356. {
  357. int value = hp_wmi_als_state();
  358. if (value < 0)
  359. return -EINVAL;
  360. return sprintf(buf, "%d\n", value);
  361. }
  362. static ssize_t show_dock(struct device *dev, struct device_attribute *attr,
  363. char *buf)
  364. {
  365. int value = hp_wmi_dock_state();
  366. if (value < 0)
  367. return -EINVAL;
  368. return sprintf(buf, "%d\n", value);
  369. }
  370. static ssize_t show_tablet(struct device *dev, struct device_attribute *attr,
  371. char *buf)
  372. {
  373. int value = hp_wmi_tablet_state();
  374. if (value < 0)
  375. return -EINVAL;
  376. return sprintf(buf, "%d\n", value);
  377. }
  378. static ssize_t set_als(struct device *dev, struct device_attribute *attr,
  379. const char *buf, size_t count)
  380. {
  381. u32 tmp = simple_strtoul(buf, NULL, 10);
  382. int ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, 1, &tmp,
  383. sizeof(tmp), sizeof(tmp));
  384. if (ret)
  385. return -EINVAL;
  386. return count;
  387. }
  388. static DEVICE_ATTR(display, S_IRUGO, show_display, NULL);
  389. static DEVICE_ATTR(hddtemp, S_IRUGO, show_hddtemp, NULL);
  390. static DEVICE_ATTR(als, S_IRUGO | S_IWUSR, show_als, set_als);
  391. static DEVICE_ATTR(dock, S_IRUGO, show_dock, NULL);
  392. static DEVICE_ATTR(tablet, S_IRUGO, show_tablet, NULL);
  393. static void hp_wmi_notify(u32 value, void *context)
  394. {
  395. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  396. union acpi_object *obj;
  397. u32 event_id, event_data;
  398. int key_code = 0, ret;
  399. u32 *location;
  400. acpi_status status;
  401. status = wmi_get_event_data(value, &response);
  402. if (status != AE_OK) {
  403. pr_info("bad event status 0x%x\n", status);
  404. return;
  405. }
  406. obj = (union acpi_object *)response.pointer;
  407. if (!obj)
  408. return;
  409. if (obj->type != ACPI_TYPE_BUFFER) {
  410. pr_info("Unknown response received %d\n", obj->type);
  411. kfree(obj);
  412. return;
  413. }
  414. /*
  415. * Depending on ACPI version the concatenation of id and event data
  416. * inside _WED function will result in a 8 or 16 byte buffer.
  417. */
  418. location = (u32 *)obj->buffer.pointer;
  419. if (obj->buffer.length == 8) {
  420. event_id = *location;
  421. event_data = *(location + 1);
  422. } else if (obj->buffer.length == 16) {
  423. event_id = *location;
  424. event_data = *(location + 2);
  425. } else {
  426. pr_info("Unknown buffer length %d\n", obj->buffer.length);
  427. kfree(obj);
  428. return;
  429. }
  430. kfree(obj);
  431. switch (event_id) {
  432. case HPWMI_DOCK_EVENT:
  433. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  434. hp_wmi_dock_state());
  435. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  436. hp_wmi_tablet_state());
  437. input_sync(hp_wmi_input_dev);
  438. break;
  439. case HPWMI_PARK_HDD:
  440. break;
  441. case HPWMI_SMART_ADAPTER:
  442. break;
  443. case HPWMI_BEZEL_BUTTON:
  444. ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
  445. &key_code,
  446. sizeof(key_code),
  447. sizeof(key_code));
  448. if (ret)
  449. break;
  450. if (!sparse_keymap_report_event(hp_wmi_input_dev,
  451. key_code, 1, true))
  452. pr_info("Unknown key code - 0x%x\n", key_code);
  453. break;
  454. case HPWMI_WIRELESS:
  455. if (rfkill2_count) {
  456. hp_wmi_rfkill2_refresh();
  457. break;
  458. }
  459. if (wifi_rfkill)
  460. rfkill_set_states(wifi_rfkill,
  461. hp_wmi_get_sw_state(HPWMI_WIFI),
  462. hp_wmi_get_hw_state(HPWMI_WIFI));
  463. if (bluetooth_rfkill)
  464. rfkill_set_states(bluetooth_rfkill,
  465. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  466. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  467. if (wwan_rfkill)
  468. rfkill_set_states(wwan_rfkill,
  469. hp_wmi_get_sw_state(HPWMI_WWAN),
  470. hp_wmi_get_hw_state(HPWMI_WWAN));
  471. break;
  472. case HPWMI_CPU_BATTERY_THROTTLE:
  473. pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
  474. break;
  475. case HPWMI_LOCK_SWITCH:
  476. break;
  477. default:
  478. pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
  479. break;
  480. }
  481. }
  482. static int __init hp_wmi_input_setup(void)
  483. {
  484. acpi_status status;
  485. int err;
  486. hp_wmi_input_dev = input_allocate_device();
  487. if (!hp_wmi_input_dev)
  488. return -ENOMEM;
  489. hp_wmi_input_dev->name = "HP WMI hotkeys";
  490. hp_wmi_input_dev->phys = "wmi/input0";
  491. hp_wmi_input_dev->id.bustype = BUS_HOST;
  492. __set_bit(EV_SW, hp_wmi_input_dev->evbit);
  493. __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
  494. __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
  495. err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
  496. if (err)
  497. goto err_free_dev;
  498. /* Set initial hardware state */
  499. input_report_switch(hp_wmi_input_dev, SW_DOCK, hp_wmi_dock_state());
  500. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  501. hp_wmi_tablet_state());
  502. input_sync(hp_wmi_input_dev);
  503. status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
  504. if (ACPI_FAILURE(status)) {
  505. err = -EIO;
  506. goto err_free_keymap;
  507. }
  508. err = input_register_device(hp_wmi_input_dev);
  509. if (err)
  510. goto err_uninstall_notifier;
  511. return 0;
  512. err_uninstall_notifier:
  513. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  514. err_free_keymap:
  515. sparse_keymap_free(hp_wmi_input_dev);
  516. err_free_dev:
  517. input_free_device(hp_wmi_input_dev);
  518. return err;
  519. }
  520. static void hp_wmi_input_destroy(void)
  521. {
  522. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  523. sparse_keymap_free(hp_wmi_input_dev);
  524. input_unregister_device(hp_wmi_input_dev);
  525. }
  526. static void cleanup_sysfs(struct platform_device *device)
  527. {
  528. device_remove_file(&device->dev, &dev_attr_display);
  529. device_remove_file(&device->dev, &dev_attr_hddtemp);
  530. device_remove_file(&device->dev, &dev_attr_als);
  531. device_remove_file(&device->dev, &dev_attr_dock);
  532. device_remove_file(&device->dev, &dev_attr_tablet);
  533. }
  534. static int __devinit hp_wmi_rfkill_setup(struct platform_device *device)
  535. {
  536. int err;
  537. int wireless = 0;
  538. err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
  539. sizeof(wireless), sizeof(wireless));
  540. if (err)
  541. return err;
  542. if (wireless & 0x1) {
  543. wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
  544. RFKILL_TYPE_WLAN,
  545. &hp_wmi_rfkill_ops,
  546. (void *) HPWMI_WIFI);
  547. if (!wifi_rfkill)
  548. return -ENOMEM;
  549. rfkill_init_sw_state(wifi_rfkill,
  550. hp_wmi_get_sw_state(HPWMI_WIFI));
  551. rfkill_set_hw_state(wifi_rfkill,
  552. hp_wmi_get_hw_state(HPWMI_WIFI));
  553. err = rfkill_register(wifi_rfkill);
  554. if (err)
  555. goto register_wifi_error;
  556. }
  557. if (wireless & 0x2) {
  558. bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
  559. RFKILL_TYPE_BLUETOOTH,
  560. &hp_wmi_rfkill_ops,
  561. (void *) HPWMI_BLUETOOTH);
  562. if (!bluetooth_rfkill) {
  563. err = -ENOMEM;
  564. goto register_wifi_error;
  565. }
  566. rfkill_init_sw_state(bluetooth_rfkill,
  567. hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
  568. rfkill_set_hw_state(bluetooth_rfkill,
  569. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  570. err = rfkill_register(bluetooth_rfkill);
  571. if (err)
  572. goto register_bluetooth_error;
  573. }
  574. if (wireless & 0x4) {
  575. wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
  576. RFKILL_TYPE_WWAN,
  577. &hp_wmi_rfkill_ops,
  578. (void *) HPWMI_WWAN);
  579. if (!wwan_rfkill) {
  580. err = -ENOMEM;
  581. goto register_bluetooth_error;
  582. }
  583. rfkill_init_sw_state(wwan_rfkill,
  584. hp_wmi_get_sw_state(HPWMI_WWAN));
  585. rfkill_set_hw_state(wwan_rfkill,
  586. hp_wmi_get_hw_state(HPWMI_WWAN));
  587. err = rfkill_register(wwan_rfkill);
  588. if (err)
  589. goto register_wwan_err;
  590. }
  591. return 0;
  592. register_wwan_err:
  593. rfkill_destroy(wwan_rfkill);
  594. wwan_rfkill = NULL;
  595. if (bluetooth_rfkill)
  596. rfkill_unregister(bluetooth_rfkill);
  597. register_bluetooth_error:
  598. rfkill_destroy(bluetooth_rfkill);
  599. bluetooth_rfkill = NULL;
  600. if (wifi_rfkill)
  601. rfkill_unregister(wifi_rfkill);
  602. register_wifi_error:
  603. rfkill_destroy(wifi_rfkill);
  604. wifi_rfkill = NULL;
  605. return err;
  606. }
  607. static int __devinit hp_wmi_rfkill2_setup(struct platform_device *device)
  608. {
  609. int err, i;
  610. struct bios_rfkill2_state state;
  611. err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
  612. 0, sizeof(state));
  613. if (err)
  614. return err;
  615. if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
  616. pr_warn("unable to parse 0x1b query output\n");
  617. return -EINVAL;
  618. }
  619. for (i = 0; i < state.count; i++) {
  620. struct rfkill *rfkill;
  621. enum rfkill_type type;
  622. char *name;
  623. switch (state.device[i].radio_type) {
  624. case HPWMI_WIFI:
  625. type = RFKILL_TYPE_WLAN;
  626. name = "hp-wifi";
  627. break;
  628. case HPWMI_BLUETOOTH:
  629. type = RFKILL_TYPE_BLUETOOTH;
  630. name = "hp-bluetooth";
  631. break;
  632. case HPWMI_WWAN:
  633. type = RFKILL_TYPE_WWAN;
  634. name = "hp-wwan";
  635. break;
  636. default:
  637. pr_warn("unknown device type 0x%x\n",
  638. state.device[i].radio_type);
  639. continue;
  640. }
  641. if (!state.device[i].vendor_id) {
  642. pr_warn("zero device %d while %d reported\n",
  643. i, state.count);
  644. continue;
  645. }
  646. rfkill = rfkill_alloc(name, &device->dev, type,
  647. &hp_wmi_rfkill2_ops, (void *)(long)i);
  648. if (!rfkill) {
  649. err = -ENOMEM;
  650. goto fail;
  651. }
  652. rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
  653. rfkill2[rfkill2_count].num = i;
  654. rfkill2[rfkill2_count].rfkill = rfkill;
  655. rfkill_init_sw_state(rfkill,
  656. IS_SWBLOCKED(state.device[i].power));
  657. rfkill_set_hw_state(rfkill,
  658. IS_HWBLOCKED(state.device[i].power));
  659. if (!(state.device[i].power & HPWMI_POWER_BIOS))
  660. pr_info("device %s blocked by BIOS\n", name);
  661. err = rfkill_register(rfkill);
  662. if (err) {
  663. rfkill_destroy(rfkill);
  664. goto fail;
  665. }
  666. rfkill2_count++;
  667. }
  668. return 0;
  669. fail:
  670. for (; rfkill2_count > 0; rfkill2_count--) {
  671. rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
  672. rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
  673. }
  674. return err;
  675. }
  676. static int __devinit hp_wmi_bios_setup(struct platform_device *device)
  677. {
  678. int err;
  679. /* clear detected rfkill devices */
  680. wifi_rfkill = NULL;
  681. bluetooth_rfkill = NULL;
  682. wwan_rfkill = NULL;
  683. rfkill2_count = 0;
  684. if (hp_wmi_rfkill_setup(device))
  685. hp_wmi_rfkill2_setup(device);
  686. err = device_create_file(&device->dev, &dev_attr_display);
  687. if (err)
  688. goto add_sysfs_error;
  689. err = device_create_file(&device->dev, &dev_attr_hddtemp);
  690. if (err)
  691. goto add_sysfs_error;
  692. err = device_create_file(&device->dev, &dev_attr_als);
  693. if (err)
  694. goto add_sysfs_error;
  695. err = device_create_file(&device->dev, &dev_attr_dock);
  696. if (err)
  697. goto add_sysfs_error;
  698. err = device_create_file(&device->dev, &dev_attr_tablet);
  699. if (err)
  700. goto add_sysfs_error;
  701. return 0;
  702. add_sysfs_error:
  703. cleanup_sysfs(device);
  704. return err;
  705. }
  706. static int __exit hp_wmi_bios_remove(struct platform_device *device)
  707. {
  708. int i;
  709. cleanup_sysfs(device);
  710. for (i = 0; i < rfkill2_count; i++) {
  711. rfkill_unregister(rfkill2[i].rfkill);
  712. rfkill_destroy(rfkill2[i].rfkill);
  713. }
  714. if (wifi_rfkill) {
  715. rfkill_unregister(wifi_rfkill);
  716. rfkill_destroy(wifi_rfkill);
  717. }
  718. if (bluetooth_rfkill) {
  719. rfkill_unregister(bluetooth_rfkill);
  720. rfkill_destroy(bluetooth_rfkill);
  721. }
  722. if (wwan_rfkill) {
  723. rfkill_unregister(wwan_rfkill);
  724. rfkill_destroy(wwan_rfkill);
  725. }
  726. return 0;
  727. }
  728. static int hp_wmi_resume_handler(struct device *device)
  729. {
  730. /*
  731. * Hardware state may have changed while suspended, so trigger
  732. * input events for the current state. As this is a switch,
  733. * the input layer will only actually pass it on if the state
  734. * changed.
  735. */
  736. if (hp_wmi_input_dev) {
  737. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  738. hp_wmi_dock_state());
  739. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  740. hp_wmi_tablet_state());
  741. input_sync(hp_wmi_input_dev);
  742. }
  743. if (rfkill2_count)
  744. hp_wmi_rfkill2_refresh();
  745. if (wifi_rfkill)
  746. rfkill_set_states(wifi_rfkill,
  747. hp_wmi_get_sw_state(HPWMI_WIFI),
  748. hp_wmi_get_hw_state(HPWMI_WIFI));
  749. if (bluetooth_rfkill)
  750. rfkill_set_states(bluetooth_rfkill,
  751. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  752. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  753. if (wwan_rfkill)
  754. rfkill_set_states(wwan_rfkill,
  755. hp_wmi_get_sw_state(HPWMI_WWAN),
  756. hp_wmi_get_hw_state(HPWMI_WWAN));
  757. return 0;
  758. }
  759. static int __init hp_wmi_init(void)
  760. {
  761. int err;
  762. int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
  763. int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
  764. if (event_capable) {
  765. err = hp_wmi_input_setup();
  766. if (err)
  767. return err;
  768. }
  769. if (bios_capable) {
  770. err = platform_driver_register(&hp_wmi_driver);
  771. if (err)
  772. goto err_driver_reg;
  773. hp_wmi_platform_dev = platform_device_alloc("hp-wmi", -1);
  774. if (!hp_wmi_platform_dev) {
  775. err = -ENOMEM;
  776. goto err_device_alloc;
  777. }
  778. err = platform_device_add(hp_wmi_platform_dev);
  779. if (err)
  780. goto err_device_add;
  781. }
  782. if (!bios_capable && !event_capable)
  783. return -ENODEV;
  784. return 0;
  785. err_device_add:
  786. platform_device_put(hp_wmi_platform_dev);
  787. err_device_alloc:
  788. platform_driver_unregister(&hp_wmi_driver);
  789. err_driver_reg:
  790. if (event_capable)
  791. hp_wmi_input_destroy();
  792. return err;
  793. }
  794. static void __exit hp_wmi_exit(void)
  795. {
  796. if (wmi_has_guid(HPWMI_EVENT_GUID))
  797. hp_wmi_input_destroy();
  798. if (hp_wmi_platform_dev) {
  799. platform_device_unregister(hp_wmi_platform_dev);
  800. platform_driver_unregister(&hp_wmi_driver);
  801. }
  802. }
  803. module_init(hp_wmi_init);
  804. module_exit(hp_wmi_exit);