dell-wmi.c 19 KB

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