hp-wmi.c 25 KB

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