hp-wmi.c 25 KB

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