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