hp-wmi.c 25 KB

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