dell-wmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Dell WMI hotkeys
  3. *
  4. * Copyright (C) 2008 Red Hat <mjg@redhat.com>
  5. * Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
  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/acpi.h>
  35. #include <linux/string.h>
  36. #include <linux/dmi.h>
  37. #include <acpi/video.h>
  38. #include "dell-smbios.h"
  39. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  40. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  41. MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
  42. MODULE_LICENSE("GPL");
  43. #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
  44. #define DELL_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492"
  45. static u32 dell_wmi_interface_version;
  46. static bool wmi_requires_smbios_request;
  47. MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
  48. MODULE_ALIAS("wmi:"DELL_DESCRIPTOR_GUID);
  49. static int __init dmi_matched(const struct dmi_system_id *dmi)
  50. {
  51. wmi_requires_smbios_request = 1;
  52. return 1;
  53. }
  54. static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
  55. {
  56. .callback = dmi_matched,
  57. .ident = "Dell Inspiron M5110",
  58. .matches = {
  59. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  60. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
  61. },
  62. },
  63. {
  64. .callback = dmi_matched,
  65. .ident = "Dell Vostro V131",
  66. .matches = {
  67. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  68. DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
  69. },
  70. },
  71. { }
  72. };
  73. /*
  74. * Keymap for WMI events of type 0x0000
  75. *
  76. * Certain keys are flagged as KE_IGNORE. All of these are either
  77. * notifications (rather than requests for change) or are also sent
  78. * via the keyboard controller so should not be sent again.
  79. */
  80. static const struct key_entry dell_wmi_keymap_type_0000[] __initconst = {
  81. { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
  82. /* Key code is followed by brightness level */
  83. { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
  84. { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
  85. /* Battery health status button */
  86. { KE_KEY, 0xe007, { KEY_BATTERY } },
  87. /* Radio devices state change, key code is followed by other values */
  88. { KE_IGNORE, 0xe008, { KEY_RFKILL } },
  89. { KE_KEY, 0xe009, { KEY_EJECTCD } },
  90. /* Key code is followed by: next, active and attached devices */
  91. { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
  92. /* Key code is followed by keyboard illumination level */
  93. { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
  94. /* BIOS error detected */
  95. { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
  96. /* Battery was removed or inserted */
  97. { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
  98. /* Wifi Catcher */
  99. { KE_KEY, 0xe011, { KEY_PROG2 } },
  100. /* Ambient light sensor toggle */
  101. { KE_IGNORE, 0xe013, { KEY_RESERVED } },
  102. { KE_IGNORE, 0xe020, { KEY_MUTE } },
  103. /* Unknown, defined in ACPI DSDT */
  104. /* { KE_IGNORE, 0xe023, { KEY_RESERVED } }, */
  105. /* Untested, Dell Instant Launch key on Inspiron 7520 */
  106. /* { KE_IGNORE, 0xe024, { KEY_RESERVED } }, */
  107. /* Dell Instant Launch key */
  108. { KE_KEY, 0xe025, { KEY_PROG4 } },
  109. /* Audio panel key */
  110. { KE_IGNORE, 0xe026, { KEY_RESERVED } },
  111. /* LCD Display On/Off Control key */
  112. { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
  113. /* Untested, Multimedia key on Dell Vostro 3560 */
  114. /* { KE_IGNORE, 0xe028, { KEY_RESERVED } }, */
  115. /* Dell Instant Launch key */
  116. { KE_KEY, 0xe029, { KEY_PROG4 } },
  117. /* Untested, Windows Mobility Center button on Inspiron 7520 */
  118. /* { KE_IGNORE, 0xe02a, { KEY_RESERVED } }, */
  119. /* Unknown, defined in ACPI DSDT */
  120. /* { KE_IGNORE, 0xe02b, { KEY_RESERVED } }, */
  121. /* Untested, Dell Audio With Preset Switch button on Inspiron 7520 */
  122. /* { KE_IGNORE, 0xe02c, { KEY_RESERVED } }, */
  123. { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
  124. { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
  125. { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
  126. { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
  127. { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
  128. /* NIC Link is Up */
  129. { KE_IGNORE, 0xe043, { KEY_RESERVED } },
  130. /* NIC Link is Down */
  131. { KE_IGNORE, 0xe044, { KEY_RESERVED } },
  132. /*
  133. * This entry is very suspicious!
  134. * Originally Matthew Garrett created this dell-wmi driver specially for
  135. * "button with a picture of a battery" which has event code 0xe045.
  136. * Later Mario Limonciello from Dell told us that event code 0xe045 is
  137. * reported by Num Lock and should be ignored because key is send also
  138. * by keyboard controller.
  139. * So for now we will ignore this event to prevent potential double
  140. * Num Lock key press.
  141. */
  142. { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
  143. /* Scroll lock and also going to tablet mode on portable devices */
  144. { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
  145. /* Untested, going from tablet mode on portable devices */
  146. /* { KE_IGNORE, 0xe047, { KEY_RESERVED } }, */
  147. /* Dell Support Center key */
  148. { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
  149. { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
  150. { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
  151. { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
  152. };
  153. struct dell_bios_keymap_entry {
  154. u16 scancode;
  155. u16 keycode;
  156. };
  157. struct dell_bios_hotkey_table {
  158. struct dmi_header header;
  159. struct dell_bios_keymap_entry keymap[];
  160. };
  161. struct dell_dmi_results {
  162. int err;
  163. int keymap_size;
  164. struct key_entry *keymap;
  165. };
  166. /* Uninitialized entries here are KEY_RESERVED == 0. */
  167. static const u16 bios_to_linux_keycode[256] __initconst = {
  168. [0] = KEY_MEDIA,
  169. [1] = KEY_NEXTSONG,
  170. [2] = KEY_PLAYPAUSE,
  171. [3] = KEY_PREVIOUSSONG,
  172. [4] = KEY_STOPCD,
  173. [5] = KEY_UNKNOWN,
  174. [6] = KEY_UNKNOWN,
  175. [7] = KEY_UNKNOWN,
  176. [8] = KEY_WWW,
  177. [9] = KEY_UNKNOWN,
  178. [10] = KEY_VOLUMEDOWN,
  179. [11] = KEY_MUTE,
  180. [12] = KEY_VOLUMEUP,
  181. [13] = KEY_UNKNOWN,
  182. [14] = KEY_BATTERY,
  183. [15] = KEY_EJECTCD,
  184. [16] = KEY_UNKNOWN,
  185. [17] = KEY_SLEEP,
  186. [18] = KEY_PROG1,
  187. [19] = KEY_BRIGHTNESSDOWN,
  188. [20] = KEY_BRIGHTNESSUP,
  189. [21] = KEY_UNKNOWN,
  190. [22] = KEY_KBDILLUMTOGGLE,
  191. [23] = KEY_UNKNOWN,
  192. [24] = KEY_SWITCHVIDEOMODE,
  193. [25] = KEY_UNKNOWN,
  194. [26] = KEY_UNKNOWN,
  195. [27] = KEY_SWITCHVIDEOMODE,
  196. [28] = KEY_UNKNOWN,
  197. [29] = KEY_UNKNOWN,
  198. [30] = KEY_PROG2,
  199. [31] = KEY_UNKNOWN,
  200. [32] = KEY_UNKNOWN,
  201. [33] = KEY_UNKNOWN,
  202. [34] = KEY_UNKNOWN,
  203. [35] = KEY_UNKNOWN,
  204. [36] = KEY_UNKNOWN,
  205. [37] = KEY_UNKNOWN,
  206. [38] = KEY_MICMUTE,
  207. [255] = KEY_PROG3,
  208. };
  209. /*
  210. * Keymap for WMI events of type 0x0010
  211. *
  212. * These are applied if the 0xB2 DMI hotkey table is present and doesn't
  213. * override them.
  214. */
  215. static const struct key_entry dell_wmi_keymap_type_0010[] __initconst = {
  216. /* Fn-lock */
  217. { KE_IGNORE, 0x151, { KEY_RESERVED } },
  218. /* Change keyboard illumination */
  219. { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
  220. /*
  221. * Radio disable (notify only -- there is no model for which the
  222. * WMI event is supposed to trigger an action).
  223. */
  224. { KE_IGNORE, 0x153, { KEY_RFKILL } },
  225. /* RGB keyboard backlight control */
  226. { KE_IGNORE, 0x154, { KEY_RESERVED } },
  227. /* Stealth mode toggle */
  228. { KE_IGNORE, 0x155, { KEY_RESERVED } },
  229. };
  230. /*
  231. * Keymap for WMI events of type 0x0011
  232. */
  233. static const struct key_entry dell_wmi_keymap_type_0011[] __initconst = {
  234. /* Battery unplugged */
  235. { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
  236. /* Battery inserted */
  237. { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
  238. /* Keyboard backlight level changed */
  239. { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
  240. { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
  241. { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
  242. { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
  243. { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
  244. };
  245. static struct input_dev *dell_wmi_input_dev;
  246. static void dell_wmi_process_key(int type, int code)
  247. {
  248. const struct key_entry *key;
  249. key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
  250. (type << 16) | code);
  251. if (!key) {
  252. pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
  253. type, code);
  254. return;
  255. }
  256. pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
  257. /* Don't report brightness notifications that will also come via ACPI */
  258. if ((key->keycode == KEY_BRIGHTNESSUP ||
  259. key->keycode == KEY_BRIGHTNESSDOWN) &&
  260. acpi_video_handles_brightness_key_presses())
  261. return;
  262. if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
  263. return;
  264. sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
  265. }
  266. static void dell_wmi_notify(u32 value, void *context)
  267. {
  268. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  269. union acpi_object *obj;
  270. acpi_status status;
  271. acpi_size buffer_size;
  272. u16 *buffer_entry, *buffer_end;
  273. int len, i;
  274. status = wmi_get_event_data(value, &response);
  275. if (status != AE_OK) {
  276. pr_warn("bad event status 0x%x\n", status);
  277. return;
  278. }
  279. obj = (union acpi_object *)response.pointer;
  280. if (!obj) {
  281. pr_warn("no response\n");
  282. return;
  283. }
  284. if (obj->type != ACPI_TYPE_BUFFER) {
  285. pr_warn("bad response type %x\n", obj->type);
  286. kfree(obj);
  287. return;
  288. }
  289. pr_debug("Received WMI event (%*ph)\n",
  290. obj->buffer.length, obj->buffer.pointer);
  291. buffer_entry = (u16 *)obj->buffer.pointer;
  292. buffer_size = obj->buffer.length/2;
  293. buffer_end = buffer_entry + buffer_size;
  294. /*
  295. * BIOS/ACPI on devices with WMI interface version 0 does not clear
  296. * buffer before filling it. So next time when BIOS/ACPI send WMI event
  297. * which is smaller as previous then it contains garbage in buffer from
  298. * previous event.
  299. *
  300. * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
  301. * sometimes send more events in buffer at one call.
  302. *
  303. * So to prevent reading garbage from buffer we will process only first
  304. * one event on devices with WMI interface version 0.
  305. */
  306. if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
  307. if (buffer_end > buffer_entry + buffer_entry[0] + 1)
  308. buffer_end = buffer_entry + buffer_entry[0] + 1;
  309. while (buffer_entry < buffer_end) {
  310. len = buffer_entry[0];
  311. if (len == 0)
  312. break;
  313. len++;
  314. if (buffer_entry + len > buffer_end) {
  315. pr_warn("Invalid length of WMI event\n");
  316. break;
  317. }
  318. pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
  319. switch (buffer_entry[1]) {
  320. case 0x0000: /* One key pressed or event occurred */
  321. if (len > 2)
  322. dell_wmi_process_key(0x0000, buffer_entry[2]);
  323. /* Other entries could contain additional information */
  324. break;
  325. case 0x0010: /* Sequence of keys pressed */
  326. case 0x0011: /* Sequence of events occurred */
  327. for (i = 2; i < len; ++i)
  328. dell_wmi_process_key(buffer_entry[1],
  329. buffer_entry[i]);
  330. break;
  331. default: /* Unknown event */
  332. pr_info("Unknown WMI event type 0x%x\n",
  333. (int)buffer_entry[1]);
  334. break;
  335. }
  336. buffer_entry += len;
  337. }
  338. kfree(obj);
  339. }
  340. static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
  341. {
  342. int i;
  343. for (i = 0; i < len; i++)
  344. if (keymap[i].code == scancode)
  345. return true;
  346. return false;
  347. }
  348. static void __init handle_dmi_entry(const struct dmi_header *dm,
  349. void *opaque)
  350. {
  351. struct dell_dmi_results *results = opaque;
  352. struct dell_bios_hotkey_table *table;
  353. int hotkey_num, i, pos = 0;
  354. struct key_entry *keymap;
  355. if (results->err || results->keymap)
  356. return; /* We already found the hotkey table. */
  357. if (dm->type != 0xb2)
  358. return;
  359. table = container_of(dm, struct dell_bios_hotkey_table, header);
  360. hotkey_num = (table->header.length -
  361. sizeof(struct dell_bios_hotkey_table)) /
  362. sizeof(struct dell_bios_keymap_entry);
  363. if (hotkey_num < 1) {
  364. /*
  365. * Historically, dell-wmi would ignore a DMI entry of
  366. * fewer than 7 bytes. Sizes between 4 and 8 bytes are
  367. * nonsensical (both the header and all entries are 4
  368. * bytes), so we approximate the old behavior by
  369. * ignoring tables with fewer than one entry.
  370. */
  371. return;
  372. }
  373. keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
  374. if (!keymap) {
  375. results->err = -ENOMEM;
  376. return;
  377. }
  378. for (i = 0; i < hotkey_num; i++) {
  379. const struct dell_bios_keymap_entry *bios_entry =
  380. &table->keymap[i];
  381. /* Uninitialized entries are 0 aka KEY_RESERVED. */
  382. u16 keycode = (bios_entry->keycode <
  383. ARRAY_SIZE(bios_to_linux_keycode)) ?
  384. bios_to_linux_keycode[bios_entry->keycode] :
  385. KEY_RESERVED;
  386. /*
  387. * Log if we find an entry in the DMI table that we don't
  388. * understand. If this happens, we should figure out what
  389. * the entry means and add it to bios_to_linux_keycode.
  390. */
  391. if (keycode == KEY_RESERVED) {
  392. pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
  393. bios_entry->scancode, bios_entry->keycode);
  394. continue;
  395. }
  396. if (keycode == KEY_KBDILLUMTOGGLE)
  397. keymap[pos].type = KE_IGNORE;
  398. else
  399. keymap[pos].type = KE_KEY;
  400. keymap[pos].code = bios_entry->scancode;
  401. keymap[pos].keycode = keycode;
  402. pos++;
  403. }
  404. results->keymap = keymap;
  405. results->keymap_size = pos;
  406. }
  407. static int __init dell_wmi_input_setup(void)
  408. {
  409. struct dell_dmi_results dmi_results = {};
  410. struct key_entry *keymap;
  411. int err, i, pos = 0;
  412. dell_wmi_input_dev = input_allocate_device();
  413. if (!dell_wmi_input_dev)
  414. return -ENOMEM;
  415. dell_wmi_input_dev->name = "Dell WMI hotkeys";
  416. dell_wmi_input_dev->phys = "wmi/input0";
  417. dell_wmi_input_dev->id.bustype = BUS_HOST;
  418. if (dmi_walk(handle_dmi_entry, &dmi_results)) {
  419. /*
  420. * Historically, dell-wmi ignored dmi_walk errors. A failure
  421. * is certainly surprising, but it probably just indicates
  422. * a very old laptop.
  423. */
  424. pr_warn("no DMI; using the old-style hotkey interface\n");
  425. }
  426. if (dmi_results.err) {
  427. err = dmi_results.err;
  428. goto err_free_dev;
  429. }
  430. keymap = kcalloc(dmi_results.keymap_size +
  431. ARRAY_SIZE(dell_wmi_keymap_type_0000) +
  432. ARRAY_SIZE(dell_wmi_keymap_type_0010) +
  433. ARRAY_SIZE(dell_wmi_keymap_type_0011) +
  434. 1,
  435. sizeof(struct key_entry), GFP_KERNEL);
  436. if (!keymap) {
  437. kfree(dmi_results.keymap);
  438. err = -ENOMEM;
  439. goto err_free_dev;
  440. }
  441. /* Append table with events of type 0x0010 which comes from DMI */
  442. for (i = 0; i < dmi_results.keymap_size; i++) {
  443. keymap[pos] = dmi_results.keymap[i];
  444. keymap[pos].code |= (0x0010 << 16);
  445. pos++;
  446. }
  447. kfree(dmi_results.keymap);
  448. /* Append table with extra events of type 0x0010 which are not in DMI */
  449. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
  450. const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
  451. /*
  452. * Check if we've already found this scancode. This takes
  453. * quadratic time, but it doesn't matter unless the list
  454. * of extra keys gets very long.
  455. */
  456. if (dmi_results.keymap_size &&
  457. have_scancode(entry->code | (0x0010 << 16),
  458. keymap, dmi_results.keymap_size)
  459. )
  460. continue;
  461. keymap[pos] = *entry;
  462. keymap[pos].code |= (0x0010 << 16);
  463. pos++;
  464. }
  465. /* Append table with events of type 0x0011 */
  466. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
  467. keymap[pos] = dell_wmi_keymap_type_0011[i];
  468. keymap[pos].code |= (0x0011 << 16);
  469. pos++;
  470. }
  471. /*
  472. * Now append also table with "legacy" events of type 0x0000. Some of
  473. * them are reported also on laptops which have scancodes in DMI.
  474. */
  475. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
  476. keymap[pos] = dell_wmi_keymap_type_0000[i];
  477. pos++;
  478. }
  479. keymap[pos].type = KE_END;
  480. err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
  481. /*
  482. * Sparse keymap library makes a copy of keymap so we don't need the
  483. * original one that was allocated.
  484. */
  485. kfree(keymap);
  486. if (err)
  487. goto err_free_dev;
  488. err = input_register_device(dell_wmi_input_dev);
  489. if (err)
  490. goto err_free_keymap;
  491. return 0;
  492. err_free_keymap:
  493. sparse_keymap_free(dell_wmi_input_dev);
  494. err_free_dev:
  495. input_free_device(dell_wmi_input_dev);
  496. return err;
  497. }
  498. static void dell_wmi_input_destroy(void)
  499. {
  500. sparse_keymap_free(dell_wmi_input_dev);
  501. input_unregister_device(dell_wmi_input_dev);
  502. }
  503. /*
  504. * Descriptor buffer is 128 byte long and contains:
  505. *
  506. * Name Offset Length Value
  507. * Vendor Signature 0 4 "DELL"
  508. * Object Signature 4 4 " WMI"
  509. * WMI Interface Version 8 4 <version>
  510. * WMI buffer length 12 4 4096
  511. */
  512. static int __init dell_wmi_check_descriptor_buffer(void)
  513. {
  514. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  515. union acpi_object *obj;
  516. acpi_status status;
  517. u32 *buffer;
  518. status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
  519. if (ACPI_FAILURE(status)) {
  520. pr_err("Cannot read Dell descriptor buffer - %d\n", status);
  521. return status;
  522. }
  523. obj = (union acpi_object *)out.pointer;
  524. if (!obj) {
  525. pr_err("Dell descriptor buffer is empty\n");
  526. return -EINVAL;
  527. }
  528. if (obj->type != ACPI_TYPE_BUFFER) {
  529. pr_err("Cannot read Dell descriptor buffer\n");
  530. kfree(obj);
  531. return -EINVAL;
  532. }
  533. if (obj->buffer.length != 128) {
  534. pr_err("Dell descriptor buffer has invalid length (%d)\n",
  535. obj->buffer.length);
  536. if (obj->buffer.length < 16) {
  537. kfree(obj);
  538. return -EINVAL;
  539. }
  540. }
  541. buffer = (u32 *)obj->buffer.pointer;
  542. if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
  543. pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
  544. 8, buffer);
  545. if (buffer[2] != 0 && buffer[2] != 1)
  546. pr_warn("Dell descriptor buffer has unknown version (%d)\n",
  547. buffer[2]);
  548. if (buffer[3] != 4096)
  549. pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
  550. buffer[3]);
  551. dell_wmi_interface_version = buffer[2];
  552. pr_info("Detected Dell WMI interface version %u\n",
  553. dell_wmi_interface_version);
  554. kfree(obj);
  555. return 0;
  556. }
  557. /*
  558. * According to Dell SMBIOS documentation:
  559. *
  560. * 17 3 Application Program Registration
  561. *
  562. * cbArg1 Application ID 1 = 0x00010000
  563. * cbArg2 Application ID 2
  564. * QUICKSET/DCP = 0x51534554 "QSET"
  565. * ALS Driver = 0x416c7353 "AlsS"
  566. * Latitude ON = 0x4c6f6e52 "LonR"
  567. * cbArg3 Application version or revision number
  568. * cbArg4 0 = Unregister application
  569. * 1 = Register application
  570. * cbRes1 Standard return codes (0, -1, -2)
  571. */
  572. static int dell_wmi_events_set_enabled(bool enable)
  573. {
  574. struct calling_interface_buffer *buffer;
  575. int ret;
  576. buffer = dell_smbios_get_buffer();
  577. buffer->input[0] = 0x10000;
  578. buffer->input[1] = 0x51534554;
  579. buffer->input[3] = enable;
  580. dell_smbios_send_request(17, 3);
  581. ret = buffer->output[0];
  582. dell_smbios_release_buffer();
  583. return dell_smbios_error(ret);
  584. }
  585. static int __init dell_wmi_init(void)
  586. {
  587. int err;
  588. acpi_status status;
  589. if (!wmi_has_guid(DELL_EVENT_GUID) ||
  590. !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
  591. pr_warn("Dell WMI GUID were not found\n");
  592. return -ENODEV;
  593. }
  594. err = dell_wmi_check_descriptor_buffer();
  595. if (err)
  596. return err;
  597. err = dell_wmi_input_setup();
  598. if (err)
  599. return err;
  600. status = wmi_install_notify_handler(DELL_EVENT_GUID,
  601. dell_wmi_notify, NULL);
  602. if (ACPI_FAILURE(status)) {
  603. dell_wmi_input_destroy();
  604. pr_err("Unable to register notify handler - %d\n", status);
  605. return -ENODEV;
  606. }
  607. dmi_check_system(dell_wmi_smbios_list);
  608. if (wmi_requires_smbios_request) {
  609. err = dell_wmi_events_set_enabled(true);
  610. if (err) {
  611. pr_err("Failed to enable WMI events\n");
  612. wmi_remove_notify_handler(DELL_EVENT_GUID);
  613. dell_wmi_input_destroy();
  614. return err;
  615. }
  616. }
  617. return 0;
  618. }
  619. module_init(dell_wmi_init);
  620. static void __exit dell_wmi_exit(void)
  621. {
  622. if (wmi_requires_smbios_request)
  623. dell_wmi_events_set_enabled(false);
  624. wmi_remove_notify_handler(DELL_EVENT_GUID);
  625. dell_wmi_input_destroy();
  626. }
  627. module_exit(dell_wmi_exit);