dell-wmi.c 20 KB

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