dell-wmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_WLAN } },
  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. /* Rugged magnetic dock attach/detach events */
  230. { KE_IGNORE, 0x156, { KEY_RESERVED } },
  231. { KE_IGNORE, 0x157, { KEY_RESERVED } },
  232. /* Rugged programmable (P1/P2/P3 keys) */
  233. { KE_KEY, 0x850, { KEY_PROG1 } },
  234. { KE_KEY, 0x851, { KEY_PROG2 } },
  235. { KE_KEY, 0x852, { KEY_PROG3 } },
  236. };
  237. /*
  238. * Keymap for WMI events of type 0x0011
  239. */
  240. static const struct key_entry dell_wmi_keymap_type_0011[] __initconst = {
  241. /* Battery unplugged */
  242. { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
  243. /* Battery inserted */
  244. { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
  245. /* Keyboard backlight level changed */
  246. { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
  247. { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
  248. { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
  249. { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
  250. { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
  251. };
  252. static struct input_dev *dell_wmi_input_dev;
  253. static void dell_wmi_process_key(int type, int code)
  254. {
  255. const struct key_entry *key;
  256. key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
  257. (type << 16) | code);
  258. if (!key) {
  259. pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
  260. type, code);
  261. return;
  262. }
  263. pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
  264. /* Don't report brightness notifications that will also come via ACPI */
  265. if ((key->keycode == KEY_BRIGHTNESSUP ||
  266. key->keycode == KEY_BRIGHTNESSDOWN) &&
  267. acpi_video_handles_brightness_key_presses())
  268. return;
  269. if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
  270. return;
  271. sparse_keymap_report_entry(dell_wmi_input_dev, key, 1, true);
  272. }
  273. static void dell_wmi_notify(u32 value, void *context)
  274. {
  275. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  276. union acpi_object *obj;
  277. acpi_status status;
  278. acpi_size buffer_size;
  279. u16 *buffer_entry, *buffer_end;
  280. int len, i;
  281. status = wmi_get_event_data(value, &response);
  282. if (status != AE_OK) {
  283. pr_warn("bad event status 0x%x\n", status);
  284. return;
  285. }
  286. obj = (union acpi_object *)response.pointer;
  287. if (!obj) {
  288. pr_warn("no response\n");
  289. return;
  290. }
  291. if (obj->type != ACPI_TYPE_BUFFER) {
  292. pr_warn("bad response type %x\n", obj->type);
  293. kfree(obj);
  294. return;
  295. }
  296. pr_debug("Received WMI event (%*ph)\n",
  297. obj->buffer.length, obj->buffer.pointer);
  298. buffer_entry = (u16 *)obj->buffer.pointer;
  299. buffer_size = obj->buffer.length/2;
  300. buffer_end = buffer_entry + buffer_size;
  301. /*
  302. * BIOS/ACPI on devices with WMI interface version 0 does not clear
  303. * buffer before filling it. So next time when BIOS/ACPI send WMI event
  304. * which is smaller as previous then it contains garbage in buffer from
  305. * previous event.
  306. *
  307. * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
  308. * sometimes send more events in buffer at one call.
  309. *
  310. * So to prevent reading garbage from buffer we will process only first
  311. * one event on devices with WMI interface version 0.
  312. */
  313. if (dell_wmi_interface_version == 0 && buffer_entry < buffer_end)
  314. if (buffer_end > buffer_entry + buffer_entry[0] + 1)
  315. buffer_end = buffer_entry + buffer_entry[0] + 1;
  316. while (buffer_entry < buffer_end) {
  317. len = buffer_entry[0];
  318. if (len == 0)
  319. break;
  320. len++;
  321. if (buffer_entry + len > buffer_end) {
  322. pr_warn("Invalid length of WMI event\n");
  323. break;
  324. }
  325. pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
  326. switch (buffer_entry[1]) {
  327. case 0x0000: /* One key pressed or event occurred */
  328. if (len > 2)
  329. dell_wmi_process_key(0x0000, buffer_entry[2]);
  330. /* Other entries could contain additional information */
  331. break;
  332. case 0x0010: /* Sequence of keys pressed */
  333. case 0x0011: /* Sequence of events occurred */
  334. for (i = 2; i < len; ++i)
  335. dell_wmi_process_key(buffer_entry[1],
  336. buffer_entry[i]);
  337. break;
  338. default: /* Unknown event */
  339. pr_info("Unknown WMI event type 0x%x\n",
  340. (int)buffer_entry[1]);
  341. break;
  342. }
  343. buffer_entry += len;
  344. }
  345. kfree(obj);
  346. }
  347. static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
  348. {
  349. int i;
  350. for (i = 0; i < len; i++)
  351. if (keymap[i].code == scancode)
  352. return true;
  353. return false;
  354. }
  355. static void __init handle_dmi_entry(const struct dmi_header *dm,
  356. void *opaque)
  357. {
  358. struct dell_dmi_results *results = opaque;
  359. struct dell_bios_hotkey_table *table;
  360. int hotkey_num, i, pos = 0;
  361. struct key_entry *keymap;
  362. if (results->err || results->keymap)
  363. return; /* We already found the hotkey table. */
  364. if (dm->type != 0xb2)
  365. return;
  366. table = container_of(dm, struct dell_bios_hotkey_table, header);
  367. hotkey_num = (table->header.length -
  368. sizeof(struct dell_bios_hotkey_table)) /
  369. sizeof(struct dell_bios_keymap_entry);
  370. if (hotkey_num < 1) {
  371. /*
  372. * Historically, dell-wmi would ignore a DMI entry of
  373. * fewer than 7 bytes. Sizes between 4 and 8 bytes are
  374. * nonsensical (both the header and all entries are 4
  375. * bytes), so we approximate the old behavior by
  376. * ignoring tables with fewer than one entry.
  377. */
  378. return;
  379. }
  380. keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
  381. if (!keymap) {
  382. results->err = -ENOMEM;
  383. return;
  384. }
  385. for (i = 0; i < hotkey_num; i++) {
  386. const struct dell_bios_keymap_entry *bios_entry =
  387. &table->keymap[i];
  388. /* Uninitialized entries are 0 aka KEY_RESERVED. */
  389. u16 keycode = (bios_entry->keycode <
  390. ARRAY_SIZE(bios_to_linux_keycode)) ?
  391. bios_to_linux_keycode[bios_entry->keycode] :
  392. KEY_RESERVED;
  393. /*
  394. * Log if we find an entry in the DMI table that we don't
  395. * understand. If this happens, we should figure out what
  396. * the entry means and add it to bios_to_linux_keycode.
  397. */
  398. if (keycode == KEY_RESERVED) {
  399. pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
  400. bios_entry->scancode, bios_entry->keycode);
  401. continue;
  402. }
  403. if (keycode == KEY_KBDILLUMTOGGLE)
  404. keymap[pos].type = KE_IGNORE;
  405. else
  406. keymap[pos].type = KE_KEY;
  407. keymap[pos].code = bios_entry->scancode;
  408. keymap[pos].keycode = keycode;
  409. pos++;
  410. }
  411. results->keymap = keymap;
  412. results->keymap_size = pos;
  413. }
  414. static int __init dell_wmi_input_setup(void)
  415. {
  416. struct dell_dmi_results dmi_results = {};
  417. struct key_entry *keymap;
  418. int err, i, pos = 0;
  419. dell_wmi_input_dev = input_allocate_device();
  420. if (!dell_wmi_input_dev)
  421. return -ENOMEM;
  422. dell_wmi_input_dev->name = "Dell WMI hotkeys";
  423. dell_wmi_input_dev->phys = "wmi/input0";
  424. dell_wmi_input_dev->id.bustype = BUS_HOST;
  425. if (dmi_walk(handle_dmi_entry, &dmi_results)) {
  426. /*
  427. * Historically, dell-wmi ignored dmi_walk errors. A failure
  428. * is certainly surprising, but it probably just indicates
  429. * a very old laptop.
  430. */
  431. pr_warn("no DMI; using the old-style hotkey interface\n");
  432. }
  433. if (dmi_results.err) {
  434. err = dmi_results.err;
  435. goto err_free_dev;
  436. }
  437. keymap = kcalloc(dmi_results.keymap_size +
  438. ARRAY_SIZE(dell_wmi_keymap_type_0000) +
  439. ARRAY_SIZE(dell_wmi_keymap_type_0010) +
  440. ARRAY_SIZE(dell_wmi_keymap_type_0011) +
  441. 1,
  442. sizeof(struct key_entry), GFP_KERNEL);
  443. if (!keymap) {
  444. kfree(dmi_results.keymap);
  445. err = -ENOMEM;
  446. goto err_free_dev;
  447. }
  448. /* Append table with events of type 0x0010 which comes from DMI */
  449. for (i = 0; i < dmi_results.keymap_size; i++) {
  450. keymap[pos] = dmi_results.keymap[i];
  451. keymap[pos].code |= (0x0010 << 16);
  452. pos++;
  453. }
  454. kfree(dmi_results.keymap);
  455. /* Append table with extra events of type 0x0010 which are not in DMI */
  456. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
  457. const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
  458. /*
  459. * Check if we've already found this scancode. This takes
  460. * quadratic time, but it doesn't matter unless the list
  461. * of extra keys gets very long.
  462. */
  463. if (dmi_results.keymap_size &&
  464. have_scancode(entry->code | (0x0010 << 16),
  465. keymap, dmi_results.keymap_size)
  466. )
  467. continue;
  468. keymap[pos] = *entry;
  469. keymap[pos].code |= (0x0010 << 16);
  470. pos++;
  471. }
  472. /* Append table with events of type 0x0011 */
  473. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
  474. keymap[pos] = dell_wmi_keymap_type_0011[i];
  475. keymap[pos].code |= (0x0011 << 16);
  476. pos++;
  477. }
  478. /*
  479. * Now append also table with "legacy" events of type 0x0000. Some of
  480. * them are reported also on laptops which have scancodes in DMI.
  481. */
  482. for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
  483. keymap[pos] = dell_wmi_keymap_type_0000[i];
  484. pos++;
  485. }
  486. keymap[pos].type = KE_END;
  487. err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
  488. /*
  489. * Sparse keymap library makes a copy of keymap so we don't need the
  490. * original one that was allocated.
  491. */
  492. kfree(keymap);
  493. if (err)
  494. goto err_free_dev;
  495. err = input_register_device(dell_wmi_input_dev);
  496. if (err)
  497. goto err_free_keymap;
  498. return 0;
  499. err_free_keymap:
  500. sparse_keymap_free(dell_wmi_input_dev);
  501. err_free_dev:
  502. input_free_device(dell_wmi_input_dev);
  503. return err;
  504. }
  505. static void dell_wmi_input_destroy(void)
  506. {
  507. sparse_keymap_free(dell_wmi_input_dev);
  508. input_unregister_device(dell_wmi_input_dev);
  509. }
  510. /*
  511. * Descriptor buffer is 128 byte long and contains:
  512. *
  513. * Name Offset Length Value
  514. * Vendor Signature 0 4 "DELL"
  515. * Object Signature 4 4 " WMI"
  516. * WMI Interface Version 8 4 <version>
  517. * WMI buffer length 12 4 4096
  518. */
  519. static int __init dell_wmi_check_descriptor_buffer(void)
  520. {
  521. struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
  522. union acpi_object *obj;
  523. acpi_status status;
  524. u32 *buffer;
  525. status = wmi_query_block(DELL_DESCRIPTOR_GUID, 0, &out);
  526. if (ACPI_FAILURE(status)) {
  527. pr_err("Cannot read Dell descriptor buffer - %d\n", status);
  528. return status;
  529. }
  530. obj = (union acpi_object *)out.pointer;
  531. if (!obj) {
  532. pr_err("Dell descriptor buffer is empty\n");
  533. return -EINVAL;
  534. }
  535. if (obj->type != ACPI_TYPE_BUFFER) {
  536. pr_err("Cannot read Dell descriptor buffer\n");
  537. kfree(obj);
  538. return -EINVAL;
  539. }
  540. if (obj->buffer.length != 128) {
  541. pr_err("Dell descriptor buffer has invalid length (%d)\n",
  542. obj->buffer.length);
  543. if (obj->buffer.length < 16) {
  544. kfree(obj);
  545. return -EINVAL;
  546. }
  547. }
  548. buffer = (u32 *)obj->buffer.pointer;
  549. if (buffer[0] != 0x4C4C4544 && buffer[1] != 0x494D5720)
  550. pr_warn("Dell descriptor buffer has invalid signature (%*ph)\n",
  551. 8, buffer);
  552. if (buffer[2] != 0 && buffer[2] != 1)
  553. pr_warn("Dell descriptor buffer has unknown version (%d)\n",
  554. buffer[2]);
  555. if (buffer[3] != 4096)
  556. pr_warn("Dell descriptor buffer has invalid buffer length (%d)\n",
  557. buffer[3]);
  558. dell_wmi_interface_version = buffer[2];
  559. pr_info("Detected Dell WMI interface version %u\n",
  560. dell_wmi_interface_version);
  561. kfree(obj);
  562. return 0;
  563. }
  564. /*
  565. * According to Dell SMBIOS documentation:
  566. *
  567. * 17 3 Application Program Registration
  568. *
  569. * cbArg1 Application ID 1 = 0x00010000
  570. * cbArg2 Application ID 2
  571. * QUICKSET/DCP = 0x51534554 "QSET"
  572. * ALS Driver = 0x416c7353 "AlsS"
  573. * Latitude ON = 0x4c6f6e52 "LonR"
  574. * cbArg3 Application version or revision number
  575. * cbArg4 0 = Unregister application
  576. * 1 = Register application
  577. * cbRes1 Standard return codes (0, -1, -2)
  578. */
  579. static int dell_wmi_events_set_enabled(bool enable)
  580. {
  581. struct calling_interface_buffer *buffer;
  582. int ret;
  583. buffer = dell_smbios_get_buffer();
  584. buffer->input[0] = 0x10000;
  585. buffer->input[1] = 0x51534554;
  586. buffer->input[3] = enable;
  587. dell_smbios_send_request(17, 3);
  588. ret = buffer->output[0];
  589. dell_smbios_release_buffer();
  590. return dell_smbios_error(ret);
  591. }
  592. static int __init dell_wmi_init(void)
  593. {
  594. int err;
  595. acpi_status status;
  596. if (!wmi_has_guid(DELL_EVENT_GUID) ||
  597. !wmi_has_guid(DELL_DESCRIPTOR_GUID)) {
  598. pr_warn("Dell WMI GUID were not found\n");
  599. return -ENODEV;
  600. }
  601. err = dell_wmi_check_descriptor_buffer();
  602. if (err)
  603. return err;
  604. err = dell_wmi_input_setup();
  605. if (err)
  606. return err;
  607. status = wmi_install_notify_handler(DELL_EVENT_GUID,
  608. dell_wmi_notify, NULL);
  609. if (ACPI_FAILURE(status)) {
  610. dell_wmi_input_destroy();
  611. pr_err("Unable to register notify handler - %d\n", status);
  612. return -ENODEV;
  613. }
  614. dmi_check_system(dell_wmi_smbios_list);
  615. if (wmi_requires_smbios_request) {
  616. err = dell_wmi_events_set_enabled(true);
  617. if (err) {
  618. pr_err("Failed to enable WMI events\n");
  619. wmi_remove_notify_handler(DELL_EVENT_GUID);
  620. dell_wmi_input_destroy();
  621. return err;
  622. }
  623. }
  624. return 0;
  625. }
  626. module_init(dell_wmi_init);
  627. static void __exit dell_wmi_exit(void)
  628. {
  629. if (wmi_requires_smbios_request)
  630. dell_wmi_events_set_enabled(false);
  631. wmi_remove_notify_handler(DELL_EVENT_GUID);
  632. dell_wmi_input_destroy();
  633. }
  634. module_exit(dell_wmi_exit);