hp-wmi.c 25 KB

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