hp-wmi.c 24 KB

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