hp-wmi.c 25 KB

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