hp-wmi.c 25 KB

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