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