hp-wmi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 < 0 ? ret : -EINVAL;
  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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 ret < 0 ? ret : -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 ret < 0 ? ret : -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. if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
  495. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  496. hp_wmi_dock_state());
  497. if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
  498. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  499. hp_wmi_tablet_state());
  500. input_sync(hp_wmi_input_dev);
  501. break;
  502. case HPWMI_PARK_HDD:
  503. break;
  504. case HPWMI_SMART_ADAPTER:
  505. break;
  506. case HPWMI_BEZEL_BUTTON:
  507. ret = hp_wmi_perform_query(HPWMI_HOTKEY_QUERY, 0,
  508. &key_code,
  509. sizeof(key_code),
  510. sizeof(key_code));
  511. if (ret)
  512. break;
  513. if (!sparse_keymap_report_event(hp_wmi_input_dev,
  514. key_code, 1, true))
  515. pr_info("Unknown key code - 0x%x\n", key_code);
  516. break;
  517. case HPWMI_WIRELESS:
  518. if (rfkill2_count) {
  519. hp_wmi_rfkill2_refresh();
  520. break;
  521. }
  522. if (wifi_rfkill)
  523. rfkill_set_states(wifi_rfkill,
  524. hp_wmi_get_sw_state(HPWMI_WIFI),
  525. hp_wmi_get_hw_state(HPWMI_WIFI));
  526. if (bluetooth_rfkill)
  527. rfkill_set_states(bluetooth_rfkill,
  528. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  529. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  530. if (wwan_rfkill)
  531. rfkill_set_states(wwan_rfkill,
  532. hp_wmi_get_sw_state(HPWMI_WWAN),
  533. hp_wmi_get_hw_state(HPWMI_WWAN));
  534. break;
  535. case HPWMI_CPU_BATTERY_THROTTLE:
  536. pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
  537. break;
  538. case HPWMI_LOCK_SWITCH:
  539. break;
  540. case HPWMI_LID_SWITCH:
  541. break;
  542. case HPWMI_SCREEN_ROTATION:
  543. break;
  544. case HPWMI_COOLSENSE_SYSTEM_MOBILE:
  545. break;
  546. case HPWMI_COOLSENSE_SYSTEM_HOT:
  547. break;
  548. case HPWMI_PROXIMITY_SENSOR:
  549. break;
  550. case HPWMI_BACKLIT_KB_BRIGHTNESS:
  551. break;
  552. case HPWMI_PEAKSHIFT_PERIOD:
  553. break;
  554. case HPWMI_BATTERY_CHARGE_PERIOD:
  555. break;
  556. default:
  557. pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
  558. break;
  559. }
  560. }
  561. static int __init hp_wmi_input_setup(void)
  562. {
  563. acpi_status status;
  564. int err;
  565. int val;
  566. hp_wmi_input_dev = input_allocate_device();
  567. if (!hp_wmi_input_dev)
  568. return -ENOMEM;
  569. hp_wmi_input_dev->name = "HP WMI hotkeys";
  570. hp_wmi_input_dev->phys = "wmi/input0";
  571. hp_wmi_input_dev->id.bustype = BUS_HOST;
  572. __set_bit(EV_SW, hp_wmi_input_dev->evbit);
  573. /* Dock */
  574. val = hp_wmi_dock_state();
  575. if (!(val < 0)) {
  576. __set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
  577. input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
  578. }
  579. /* Tablet mode */
  580. val = hp_wmi_tablet_state();
  581. if (!(val < 0)) {
  582. __set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
  583. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
  584. }
  585. err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
  586. if (err)
  587. goto err_free_dev;
  588. /* Set initial hardware state */
  589. input_sync(hp_wmi_input_dev);
  590. if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
  591. hp_wmi_enable_hotkeys();
  592. status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
  593. if (ACPI_FAILURE(status)) {
  594. err = -EIO;
  595. goto err_free_keymap;
  596. }
  597. err = input_register_device(hp_wmi_input_dev);
  598. if (err)
  599. goto err_uninstall_notifier;
  600. return 0;
  601. err_uninstall_notifier:
  602. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  603. err_free_keymap:
  604. sparse_keymap_free(hp_wmi_input_dev);
  605. err_free_dev:
  606. input_free_device(hp_wmi_input_dev);
  607. return err;
  608. }
  609. static void hp_wmi_input_destroy(void)
  610. {
  611. wmi_remove_notify_handler(HPWMI_EVENT_GUID);
  612. sparse_keymap_free(hp_wmi_input_dev);
  613. input_unregister_device(hp_wmi_input_dev);
  614. }
  615. static void cleanup_sysfs(struct platform_device *device)
  616. {
  617. device_remove_file(&device->dev, &dev_attr_display);
  618. device_remove_file(&device->dev, &dev_attr_hddtemp);
  619. device_remove_file(&device->dev, &dev_attr_als);
  620. device_remove_file(&device->dev, &dev_attr_dock);
  621. device_remove_file(&device->dev, &dev_attr_tablet);
  622. device_remove_file(&device->dev, &dev_attr_postcode);
  623. }
  624. static int __init hp_wmi_rfkill_setup(struct platform_device *device)
  625. {
  626. int err;
  627. int wireless = 0;
  628. err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 0, &wireless,
  629. sizeof(wireless), sizeof(wireless));
  630. if (err)
  631. return err;
  632. err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, 1, &wireless,
  633. sizeof(wireless), 0);
  634. if (err)
  635. return err;
  636. if (wireless & 0x1) {
  637. wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
  638. RFKILL_TYPE_WLAN,
  639. &hp_wmi_rfkill_ops,
  640. (void *) HPWMI_WIFI);
  641. if (!wifi_rfkill)
  642. return -ENOMEM;
  643. rfkill_init_sw_state(wifi_rfkill,
  644. hp_wmi_get_sw_state(HPWMI_WIFI));
  645. rfkill_set_hw_state(wifi_rfkill,
  646. hp_wmi_get_hw_state(HPWMI_WIFI));
  647. err = rfkill_register(wifi_rfkill);
  648. if (err)
  649. goto register_wifi_error;
  650. }
  651. if (wireless & 0x2) {
  652. bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
  653. RFKILL_TYPE_BLUETOOTH,
  654. &hp_wmi_rfkill_ops,
  655. (void *) HPWMI_BLUETOOTH);
  656. if (!bluetooth_rfkill) {
  657. err = -ENOMEM;
  658. goto register_bluetooth_error;
  659. }
  660. rfkill_init_sw_state(bluetooth_rfkill,
  661. hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
  662. rfkill_set_hw_state(bluetooth_rfkill,
  663. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  664. err = rfkill_register(bluetooth_rfkill);
  665. if (err)
  666. goto register_bluetooth_error;
  667. }
  668. if (wireless & 0x4) {
  669. wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
  670. RFKILL_TYPE_WWAN,
  671. &hp_wmi_rfkill_ops,
  672. (void *) HPWMI_WWAN);
  673. if (!wwan_rfkill) {
  674. err = -ENOMEM;
  675. goto register_wwan_error;
  676. }
  677. rfkill_init_sw_state(wwan_rfkill,
  678. hp_wmi_get_sw_state(HPWMI_WWAN));
  679. rfkill_set_hw_state(wwan_rfkill,
  680. hp_wmi_get_hw_state(HPWMI_WWAN));
  681. err = rfkill_register(wwan_rfkill);
  682. if (err)
  683. goto register_wwan_error;
  684. }
  685. return 0;
  686. register_wwan_error:
  687. rfkill_destroy(wwan_rfkill);
  688. wwan_rfkill = NULL;
  689. if (bluetooth_rfkill)
  690. rfkill_unregister(bluetooth_rfkill);
  691. register_bluetooth_error:
  692. rfkill_destroy(bluetooth_rfkill);
  693. bluetooth_rfkill = NULL;
  694. if (wifi_rfkill)
  695. rfkill_unregister(wifi_rfkill);
  696. register_wifi_error:
  697. rfkill_destroy(wifi_rfkill);
  698. wifi_rfkill = NULL;
  699. return err;
  700. }
  701. static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
  702. {
  703. int err, i;
  704. struct bios_rfkill2_state state;
  705. err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, 0, &state,
  706. 0, sizeof(state));
  707. if (err)
  708. return err;
  709. if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
  710. pr_warn("unable to parse 0x1b query output\n");
  711. return -EINVAL;
  712. }
  713. for (i = 0; i < state.count; i++) {
  714. struct rfkill *rfkill;
  715. enum rfkill_type type;
  716. char *name;
  717. switch (state.device[i].radio_type) {
  718. case HPWMI_WIFI:
  719. type = RFKILL_TYPE_WLAN;
  720. name = "hp-wifi";
  721. break;
  722. case HPWMI_BLUETOOTH:
  723. type = RFKILL_TYPE_BLUETOOTH;
  724. name = "hp-bluetooth";
  725. break;
  726. case HPWMI_WWAN:
  727. type = RFKILL_TYPE_WWAN;
  728. name = "hp-wwan";
  729. break;
  730. case HPWMI_GPS:
  731. type = RFKILL_TYPE_GPS;
  732. name = "hp-gps";
  733. break;
  734. default:
  735. pr_warn("unknown device type 0x%x\n",
  736. state.device[i].radio_type);
  737. continue;
  738. }
  739. if (!state.device[i].vendor_id) {
  740. pr_warn("zero device %d while %d reported\n",
  741. i, state.count);
  742. continue;
  743. }
  744. rfkill = rfkill_alloc(name, &device->dev, type,
  745. &hp_wmi_rfkill2_ops, (void *)(long)i);
  746. if (!rfkill) {
  747. err = -ENOMEM;
  748. goto fail;
  749. }
  750. rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
  751. rfkill2[rfkill2_count].num = i;
  752. rfkill2[rfkill2_count].rfkill = rfkill;
  753. rfkill_init_sw_state(rfkill,
  754. IS_SWBLOCKED(state.device[i].power));
  755. rfkill_set_hw_state(rfkill,
  756. IS_HWBLOCKED(state.device[i].power));
  757. if (!(state.device[i].power & HPWMI_POWER_BIOS))
  758. pr_info("device %s blocked by BIOS\n", name);
  759. err = rfkill_register(rfkill);
  760. if (err) {
  761. rfkill_destroy(rfkill);
  762. goto fail;
  763. }
  764. rfkill2_count++;
  765. }
  766. return 0;
  767. fail:
  768. for (; rfkill2_count > 0; rfkill2_count--) {
  769. rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
  770. rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
  771. }
  772. return err;
  773. }
  774. static int __init hp_wmi_bios_setup(struct platform_device *device)
  775. {
  776. int err;
  777. /* clear detected rfkill devices */
  778. wifi_rfkill = NULL;
  779. bluetooth_rfkill = NULL;
  780. wwan_rfkill = NULL;
  781. rfkill2_count = 0;
  782. if (hp_wmi_rfkill_setup(device))
  783. hp_wmi_rfkill2_setup(device);
  784. err = device_create_file(&device->dev, &dev_attr_display);
  785. if (err)
  786. goto add_sysfs_error;
  787. err = device_create_file(&device->dev, &dev_attr_hddtemp);
  788. if (err)
  789. goto add_sysfs_error;
  790. err = device_create_file(&device->dev, &dev_attr_als);
  791. if (err)
  792. goto add_sysfs_error;
  793. err = device_create_file(&device->dev, &dev_attr_dock);
  794. if (err)
  795. goto add_sysfs_error;
  796. err = device_create_file(&device->dev, &dev_attr_tablet);
  797. if (err)
  798. goto add_sysfs_error;
  799. err = device_create_file(&device->dev, &dev_attr_postcode);
  800. if (err)
  801. goto add_sysfs_error;
  802. return 0;
  803. add_sysfs_error:
  804. cleanup_sysfs(device);
  805. return err;
  806. }
  807. static int __exit hp_wmi_bios_remove(struct platform_device *device)
  808. {
  809. int i;
  810. cleanup_sysfs(device);
  811. for (i = 0; i < rfkill2_count; i++) {
  812. rfkill_unregister(rfkill2[i].rfkill);
  813. rfkill_destroy(rfkill2[i].rfkill);
  814. }
  815. if (wifi_rfkill) {
  816. rfkill_unregister(wifi_rfkill);
  817. rfkill_destroy(wifi_rfkill);
  818. }
  819. if (bluetooth_rfkill) {
  820. rfkill_unregister(bluetooth_rfkill);
  821. rfkill_destroy(bluetooth_rfkill);
  822. }
  823. if (wwan_rfkill) {
  824. rfkill_unregister(wwan_rfkill);
  825. rfkill_destroy(wwan_rfkill);
  826. }
  827. return 0;
  828. }
  829. static int hp_wmi_resume_handler(struct device *device)
  830. {
  831. /*
  832. * Hardware state may have changed while suspended, so trigger
  833. * input events for the current state. As this is a switch,
  834. * the input layer will only actually pass it on if the state
  835. * changed.
  836. */
  837. if (hp_wmi_input_dev) {
  838. if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
  839. input_report_switch(hp_wmi_input_dev, SW_DOCK,
  840. hp_wmi_dock_state());
  841. if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
  842. input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
  843. hp_wmi_tablet_state());
  844. input_sync(hp_wmi_input_dev);
  845. }
  846. if (rfkill2_count)
  847. hp_wmi_rfkill2_refresh();
  848. if (wifi_rfkill)
  849. rfkill_set_states(wifi_rfkill,
  850. hp_wmi_get_sw_state(HPWMI_WIFI),
  851. hp_wmi_get_hw_state(HPWMI_WIFI));
  852. if (bluetooth_rfkill)
  853. rfkill_set_states(bluetooth_rfkill,
  854. hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
  855. hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
  856. if (wwan_rfkill)
  857. rfkill_set_states(wwan_rfkill,
  858. hp_wmi_get_sw_state(HPWMI_WWAN),
  859. hp_wmi_get_hw_state(HPWMI_WWAN));
  860. return 0;
  861. }
  862. static const struct dev_pm_ops hp_wmi_pm_ops = {
  863. .resume = hp_wmi_resume_handler,
  864. .restore = hp_wmi_resume_handler,
  865. };
  866. static struct platform_driver hp_wmi_driver = {
  867. .driver = {
  868. .name = "hp-wmi",
  869. .pm = &hp_wmi_pm_ops,
  870. },
  871. .remove = __exit_p(hp_wmi_bios_remove),
  872. };
  873. static int __init hp_wmi_init(void)
  874. {
  875. int err;
  876. int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
  877. int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
  878. if (!bios_capable && !event_capable)
  879. return -ENODEV;
  880. if (event_capable) {
  881. err = hp_wmi_input_setup();
  882. if (err)
  883. return err;
  884. }
  885. if (bios_capable) {
  886. hp_wmi_platform_dev =
  887. platform_device_register_simple("hp-wmi", -1, NULL, 0);
  888. if (IS_ERR(hp_wmi_platform_dev)) {
  889. err = PTR_ERR(hp_wmi_platform_dev);
  890. goto err_destroy_input;
  891. }
  892. err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
  893. if (err)
  894. goto err_unregister_device;
  895. }
  896. return 0;
  897. err_unregister_device:
  898. platform_device_unregister(hp_wmi_platform_dev);
  899. err_destroy_input:
  900. if (event_capable)
  901. hp_wmi_input_destroy();
  902. return err;
  903. }
  904. module_init(hp_wmi_init);
  905. static void __exit hp_wmi_exit(void)
  906. {
  907. if (wmi_has_guid(HPWMI_EVENT_GUID))
  908. hp_wmi_input_destroy();
  909. if (hp_wmi_platform_dev) {
  910. platform_device_unregister(hp_wmi_platform_dev);
  911. platform_driver_unregister(&hp_wmi_driver);
  912. }
  913. }
  914. module_exit(hp_wmi_exit);