hp-wmi.c 25 KB

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