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