hp-wmi.c 23 KB

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