hp-wmi.c 25 KB

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