hp-wmi.c 26 KB

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