hp-wmi.c 23 KB

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