hp-wmi.c 25 KB

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