hp-wmi.c 23 KB

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