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