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 	/* Mic mute */
265 	{ KE_KEY, 0x150, { KEY_MICMUTE } },
266 
267 	/* Fn-lock */
268 	{ KE_IGNORE, 0x151, { KEY_RESERVED } },
269 
270 	/* Change keyboard illumination */
271 	{ KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
272 
273 	/*
274 	 * Radio disable (notify only -- there is no model for which the
275 	 * WMI event is supposed to trigger an action).
276 	 */
277 	{ KE_IGNORE, 0x153, { KEY_RFKILL } },
278 
279 	/* RGB keyboard backlight control */
280 	{ KE_IGNORE, 0x154, { KEY_RESERVED } },
281 
282 	/*
283 	 * Stealth mode toggle. This will "disable all lights and sounds".
284 	 * The action is performed by the BIOS and EC; the WMI event is just
285 	 * a notification. On the XPS 13 9350, this is Fn+F7, and there's
286 	 * a BIOS setting to enable and disable the hotkey.
287 	 */
288 	{ KE_IGNORE, 0x155, { KEY_RESERVED } },
289 
290 	/* Rugged magnetic dock attach/detach events */
291 	{ KE_IGNORE, 0x156, { KEY_RESERVED } },
292 	{ KE_IGNORE, 0x157, { KEY_RESERVED } },
293 
294 	/* Rugged programmable (P1/P2/P3 keys) */
295 	{ KE_KEY,    0x850, { KEY_PROG1 } },
296 	{ KE_KEY,    0x851, { KEY_PROG2 } },
297 	{ KE_KEY,    0x852, { KEY_PROG3 } },
298 
299 	/*
300 	 * Radio disable (notify only -- there is no model for which the
301 	 * WMI event is supposed to trigger an action).
302 	 */
303 	{ KE_IGNORE, 0xe008, { KEY_RFKILL } },
304 
305 	/* Fn-lock */
306 	{ KE_IGNORE, 0xe035, { KEY_RESERVED } },
307 };
308 
309 /*
310  * Keymap for WMI events of type 0x0011
311  */
312 static const struct key_entry dell_wmi_keymap_type_0011[] = {
313 	/* Reflex keyboard switch on 2n1 devices */
314 	{ KE_IGNORE, 0xe070, { KEY_RESERVED } },
315 
316 	/* Battery unplugged */
317 	{ KE_IGNORE, 0xfff0, { KEY_RESERVED } },
318 
319 	/* Battery inserted */
320 	{ KE_IGNORE, 0xfff1, { KEY_RESERVED } },
321 
322 	/*
323 	 * Detachable keyboard detached / undocked
324 	 * Note SW_TABLET_MODE is already reported through the intel_vbtn
325 	 * driver for this, so we ignore it.
326 	 */
327 	{ KE_IGNORE, 0xfff2, { KEY_RESERVED } },
328 
329 	/* Detachable keyboard attached / docked */
330 	{ KE_IGNORE, 0xfff3, { KEY_RESERVED } },
331 
332 	/* Keyboard backlight level changed */
333 	{ KE_IGNORE, KBD_LED_OFF_TOKEN,      { KEY_RESERVED } },
334 	{ KE_IGNORE, KBD_LED_ON_TOKEN,       { KEY_RESERVED } },
335 	{ KE_IGNORE, KBD_LED_AUTO_TOKEN,     { KEY_RESERVED } },
336 	{ KE_IGNORE, KBD_LED_AUTO_25_TOKEN,  { KEY_RESERVED } },
337 	{ KE_IGNORE, KBD_LED_AUTO_50_TOKEN,  { KEY_RESERVED } },
338 	{ KE_IGNORE, KBD_LED_AUTO_75_TOKEN,  { KEY_RESERVED } },
339 	{ KE_IGNORE, KBD_LED_AUTO_100_TOKEN, { KEY_RESERVED } },
340 };
341 
342 /*
343  * Keymap for WMI events of type 0x0012
344  * They are events with extended data
345  */
346 static const struct key_entry dell_wmi_keymap_type_0012[] = {
347 	/* Backlight brightness change event */
348 	{ KE_IGNORE, 0x0003, { KEY_RESERVED } },
349 
350 	/* Ultra-performance mode switch request */
351 	{ KE_IGNORE, 0x000d, { KEY_RESERVED } },
352 
353 	/* Fn-lock button pressed */
354 	{ KE_IGNORE, 0xe035, { KEY_RESERVED } },
355 };
356 
357 static void dell_wmi_switch_event(struct input_dev **subdev,
358 				  const char *devname,
359 				  int switchid,
360 				  int value)
361 {
362 	if (!*subdev) {
363 		struct input_dev *dev = input_allocate_device();
364 
365 		if (!dev) {
366 			pr_warn("could not allocate device for %s\n", devname);
367 			return;
368 		}
369 		__set_bit(EV_SW, (dev)->evbit);
370 		__set_bit(switchid, (dev)->swbit);
371 
372 		(dev)->name = devname;
373 		(dev)->id.bustype = BUS_HOST;
374 		if (input_register_device(dev)) {
375 			input_free_device(dev);
376 			pr_warn("could not register device for %s\n", devname);
377 			return;
378 		}
379 		*subdev = dev;
380 	}
381 
382 	input_report_switch(*subdev, switchid, value);
383 	input_sync(*subdev);
384 }
385 
386 static int dell_wmi_process_key(struct wmi_device *wdev, int type, int code, u16 *buffer, int remaining)
387 {
388 	struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
389 	const struct key_entry *key;
390 	int used = 0;
391 	int value = 1;
392 
393 	key = sparse_keymap_entry_from_scancode(priv->input_dev,
394 						(type << 16) | code);
395 	if (!key) {
396 		pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
397 			type, code);
398 		return 0;
399 	}
400 
401 	pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
402 
403 	/* Don't report brightness notifications that will also come via ACPI */
404 	if ((key->keycode == KEY_BRIGHTNESSUP ||
405 	     key->keycode == KEY_BRIGHTNESSDOWN) &&
406 	    acpi_video_handles_brightness_key_presses())
407 		return 0;
408 
409 	if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
410 		return 0;
411 
412 	if (key->keycode == KEY_KBDILLUMTOGGLE) {
413 		dell_laptop_call_notifier(
414 			DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
415 	} else if (type == 0x0011 && code == 0xe070 && remaining > 0) {
416 		dell_wmi_switch_event(&priv->tabletswitch_dev,
417 				      "Dell tablet mode switch",
418 				      SW_TABLET_MODE, !buffer[0]);
419 		return 1;
420 	} else if (type == 0x0012 && code == 0x000d && remaining > 0) {
421 		value = (buffer[2] == 2);
422 		used = 1;
423 	}
424 
425 	sparse_keymap_report_entry(priv->input_dev, key, value, true);
426 
427 	return used;
428 }
429 
430 static void dell_wmi_notify(struct wmi_device *wdev,
431 			    union acpi_object *obj)
432 {
433 	struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
434 	u16 *buffer_entry, *buffer_end;
435 	acpi_size buffer_size;
436 	int len, i;
437 
438 	if (obj->type != ACPI_TYPE_BUFFER) {
439 		pr_warn("bad response type %x\n", obj->type);
440 		return;
441 	}
442 
443 	pr_debug("Received WMI event (%*ph)\n",
444 		obj->buffer.length, obj->buffer.pointer);
445 
446 	buffer_entry = (u16 *)obj->buffer.pointer;
447 	buffer_size = obj->buffer.length/2;
448 	buffer_end = buffer_entry + buffer_size;
449 
450 	/*
451 	 * BIOS/ACPI on devices with WMI interface version 0 does not clear
452 	 * buffer before filling it. So next time when BIOS/ACPI send WMI event
453 	 * which is smaller as previous then it contains garbage in buffer from
454 	 * previous event.
455 	 *
456 	 * BIOS/ACPI on devices with WMI interface version 1 clears buffer and
457 	 * sometimes send more events in buffer at one call.
458 	 *
459 	 * So to prevent reading garbage from buffer we will process only first
460 	 * one event on devices with WMI interface version 0.
461 	 */
462 	if (priv->interface_version == 0 && buffer_entry < buffer_end)
463 		if (buffer_end > buffer_entry + buffer_entry[0] + 1)
464 			buffer_end = buffer_entry + buffer_entry[0] + 1;
465 
466 	while (buffer_entry < buffer_end) {
467 
468 		len = buffer_entry[0];
469 		if (len == 0)
470 			break;
471 
472 		len++;
473 
474 		if (buffer_entry + len > buffer_end) {
475 			pr_warn("Invalid length of WMI event\n");
476 			break;
477 		}
478 
479 		pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
480 
481 		switch (buffer_entry[1]) {
482 		case 0x0000: /* One key pressed or event occurred */
483 			if (len > 2)
484 				dell_wmi_process_key(wdev, buffer_entry[1],
485 						     buffer_entry[2],
486 						     buffer_entry + 3,
487 						     len - 3);
488 			/* Extended data is currently ignored */
489 			break;
490 		case 0x0010: /* Sequence of keys pressed */
491 		case 0x0011: /* Sequence of events occurred */
492 			for (i = 2; i < len; ++i)
493 				i += dell_wmi_process_key(wdev, buffer_entry[1],
494 							  buffer_entry[i],
495 							  buffer_entry + i,
496 							  len - i - 1);
497 			break;
498 		case 0x0012:
499 			if ((len > 4) && dell_privacy_process_event(buffer_entry[1], buffer_entry[3],
500 								    buffer_entry[4]))
501 				/* dell_privacy_process_event has handled the event */;
502 			else if (len > 2)
503 				dell_wmi_process_key(wdev, buffer_entry[1], buffer_entry[2],
504 						     buffer_entry + 3, len - 3);
505 			break;
506 		default: /* Unknown event */
507 			pr_info("Unknown WMI event type 0x%x\n",
508 				(int)buffer_entry[1]);
509 			break;
510 		}
511 
512 		buffer_entry += len;
513 
514 	}
515 
516 }
517 
518 static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
519 {
520 	int i;
521 
522 	for (i = 0; i < len; i++)
523 		if (keymap[i].code == scancode)
524 			return true;
525 
526 	return false;
527 }
528 
529 static void handle_dmi_entry(const struct dmi_header *dm, void *opaque)
530 {
531 	struct dell_dmi_results *results = opaque;
532 	struct dell_bios_hotkey_table *table;
533 	int hotkey_num, i, pos = 0;
534 	struct key_entry *keymap;
535 
536 	if (results->err || results->keymap)
537 		return;		/* We already found the hotkey table. */
538 
539 	/* The Dell hotkey table is type 0xB2.  Scan until we find it. */
540 	if (dm->type != 0xb2)
541 		return;
542 
543 	table = container_of(dm, struct dell_bios_hotkey_table, header);
544 
545 	hotkey_num = (table->header.length -
546 		      sizeof(struct dell_bios_hotkey_table)) /
547 				sizeof(struct dell_bios_keymap_entry);
548 	if (hotkey_num < 1) {
549 		/*
550 		 * Historically, dell-wmi would ignore a DMI entry of
551 		 * fewer than 7 bytes.  Sizes between 4 and 8 bytes are
552 		 * nonsensical (both the header and all entries are 4
553 		 * bytes), so we approximate the old behavior by
554 		 * ignoring tables with fewer than one entry.
555 		 */
556 		return;
557 	}
558 
559 	keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
560 	if (!keymap) {
561 		results->err = -ENOMEM;
562 		return;
563 	}
564 
565 	for (i = 0; i < hotkey_num; i++) {
566 		const struct dell_bios_keymap_entry *bios_entry =
567 					&table->keymap[i];
568 
569 		/* Uninitialized entries are 0 aka KEY_RESERVED. */
570 		u16 keycode = (bios_entry->keycode <
571 			       ARRAY_SIZE(bios_to_linux_keycode)) ?
572 			bios_to_linux_keycode[bios_entry->keycode] :
573 			(bios_entry->keycode == 0xffff ? KEY_UNKNOWN : KEY_RESERVED);
574 
575 		/*
576 		 * Log if we find an entry in the DMI table that we don't
577 		 * understand.  If this happens, we should figure out what
578 		 * the entry means and add it to bios_to_linux_keycode.
579 		 */
580 		if (keycode == KEY_RESERVED) {
581 			pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
582 				bios_entry->scancode, bios_entry->keycode);
583 			continue;
584 		}
585 
586 		if (keycode == KEY_KBDILLUMTOGGLE)
587 			keymap[pos].type = KE_IGNORE;
588 		else
589 			keymap[pos].type = KE_KEY;
590 		keymap[pos].code = bios_entry->scancode;
591 		keymap[pos].keycode = keycode;
592 
593 		pos++;
594 	}
595 
596 	results->keymap = keymap;
597 	results->keymap_size = pos;
598 }
599 
600 static int dell_wmi_input_setup(struct wmi_device *wdev)
601 {
602 	struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
603 	struct dell_dmi_results dmi_results = {};
604 	struct key_entry *keymap;
605 	int err, i, pos = 0;
606 
607 	priv->input_dev = input_allocate_device();
608 	if (!priv->input_dev)
609 		return -ENOMEM;
610 
611 	priv->input_dev->name = "Dell WMI hotkeys";
612 	priv->input_dev->id.bustype = BUS_HOST;
613 	priv->input_dev->dev.parent = &wdev->dev;
614 
615 	if (dmi_walk(handle_dmi_entry, &dmi_results)) {
616 		/*
617 		 * Historically, dell-wmi ignored dmi_walk errors.  A failure
618 		 * is certainly surprising, but it probably just indicates
619 		 * a very old laptop.
620 		 */
621 		pr_warn("no DMI; using the old-style hotkey interface\n");
622 	}
623 
624 	if (dmi_results.err) {
625 		err = dmi_results.err;
626 		goto err_free_dev;
627 	}
628 
629 	keymap = kcalloc(dmi_results.keymap_size +
630 			 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
631 			 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
632 			 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
633 			 ARRAY_SIZE(dell_wmi_keymap_type_0012) +
634 			 1,
635 			 sizeof(struct key_entry), GFP_KERNEL);
636 	if (!keymap) {
637 		kfree(dmi_results.keymap);
638 		err = -ENOMEM;
639 		goto err_free_dev;
640 	}
641 
642 	/* Append table with events of type 0x0010 which comes from DMI */
643 	for (i = 0; i < dmi_results.keymap_size; i++) {
644 		keymap[pos] = dmi_results.keymap[i];
645 		keymap[pos].code |= (0x0010 << 16);
646 		pos++;
647 	}
648 
649 	kfree(dmi_results.keymap);
650 
651 	/* Append table with extra events of type 0x0010 which are not in DMI */
652 	for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
653 		const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
654 
655 		/*
656 		 * Check if we've already found this scancode.  This takes
657 		 * quadratic time, but it doesn't matter unless the list
658 		 * of extra keys gets very long.
659 		 */
660 		if (dmi_results.keymap_size &&
661 		    have_scancode(entry->code | (0x0010 << 16),
662 				  keymap, dmi_results.keymap_size)
663 		   )
664 			continue;
665 
666 		keymap[pos] = *entry;
667 		keymap[pos].code |= (0x0010 << 16);
668 		pos++;
669 	}
670 
671 	/* Append table with events of type 0x0011 */
672 	for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
673 		keymap[pos] = dell_wmi_keymap_type_0011[i];
674 		keymap[pos].code |= (0x0011 << 16);
675 		pos++;
676 	}
677 
678 	/* Append table with events of type 0x0012 */
679 	for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0012); i++) {
680 		keymap[pos] = dell_wmi_keymap_type_0012[i];
681 		keymap[pos].code |= (0x0012 << 16);
682 		pos++;
683 	}
684 
685 	/*
686 	 * Now append also table with "legacy" events of type 0x0000. Some of
687 	 * them are reported also on laptops which have scancodes in DMI.
688 	 */
689 	for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
690 		keymap[pos] = dell_wmi_keymap_type_0000[i];
691 		pos++;
692 	}
693 
694 	keymap[pos].type = KE_END;
695 
696 	err = sparse_keymap_setup(priv->input_dev, keymap, NULL);
697 	/*
698 	 * Sparse keymap library makes a copy of keymap so we don't need the
699 	 * original one that was allocated.
700 	 */
701 	kfree(keymap);
702 	if (err)
703 		goto err_free_dev;
704 
705 	err = input_register_device(priv->input_dev);
706 	if (err)
707 		goto err_free_dev;
708 
709 	return 0;
710 
711  err_free_dev:
712 	input_free_device(priv->input_dev);
713 	return err;
714 }
715 
716 static void dell_wmi_input_destroy(struct wmi_device *wdev)
717 {
718 	struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
719 
720 	input_unregister_device(priv->input_dev);
721 	if (priv->tabletswitch_dev)
722 		input_unregister_device(priv->tabletswitch_dev);
723 }
724 
725 /*
726  * According to Dell SMBIOS documentation:
727  *
728  * 17  3  Application Program Registration
729  *
730  *     cbArg1 Application ID 1 = 0x00010000
731  *     cbArg2 Application ID 2
732  *            QUICKSET/DCP = 0x51534554 "QSET"
733  *            ALS Driver   = 0x416c7353 "AlsS"
734  *            Latitude ON  = 0x4c6f6e52 "LonR"
735  *     cbArg3 Application version or revision number
736  *     cbArg4 0 = Unregister application
737  *            1 = Register application
738  *     cbRes1 Standard return codes (0, -1, -2)
739  */
740 
741 static int dell_wmi_events_set_enabled(bool enable)
742 {
743 	struct calling_interface_buffer *buffer;
744 	int ret;
745 
746 	buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
747 	if (!buffer)
748 		return -ENOMEM;
749 	buffer->cmd_class = CLASS_INFO;
750 	buffer->cmd_select = SELECT_APP_REGISTRATION;
751 	buffer->input[0] = 0x10000;
752 	buffer->input[1] = 0x51534554;
753 	buffer->input[3] = enable;
754 	ret = dell_smbios_call(buffer);
755 	if (ret == 0)
756 		ret = buffer->output[0];
757 	kfree(buffer);
758 
759 	return dell_smbios_error(ret);
760 }
761 
762 static int dell_wmi_probe(struct wmi_device *wdev, const void *context)
763 {
764 	struct dell_wmi_priv *priv;
765 	int ret;
766 
767 	ret = dell_wmi_get_descriptor_valid();
768 	if (ret)
769 		return ret;
770 
771 	priv = devm_kzalloc(
772 		&wdev->dev, sizeof(struct dell_wmi_priv), GFP_KERNEL);
773 	if (!priv)
774 		return -ENOMEM;
775 	dev_set_drvdata(&wdev->dev, priv);
776 
777 	if (!dell_wmi_get_interface_version(&priv->interface_version))
778 		return -EPROBE_DEFER;
779 
780 	return dell_wmi_input_setup(wdev);
781 }
782 
783 static void dell_wmi_remove(struct wmi_device *wdev)
784 {
785 	dell_wmi_input_destroy(wdev);
786 }
787 static const struct wmi_device_id dell_wmi_id_table[] = {
788 	{ .guid_string = DELL_EVENT_GUID },
789 	{ },
790 };
791 
792 static struct wmi_driver dell_wmi_driver = {
793 	.driver = {
794 		.name = "dell-wmi",
795 	},
796 	.id_table = dell_wmi_id_table,
797 	.probe = dell_wmi_probe,
798 	.remove = dell_wmi_remove,
799 	.notify = dell_wmi_notify,
800 };
801 
802 static int __init dell_wmi_init(void)
803 {
804 	int err;
805 
806 	dmi_check_system(dell_wmi_smbios_list);
807 
808 	if (wmi_requires_smbios_request) {
809 		err = dell_wmi_events_set_enabled(true);
810 		if (err) {
811 			pr_err("Failed to enable WMI events\n");
812 			return err;
813 		}
814 	}
815 
816 	err = dell_privacy_register_driver();
817 	if (err)
818 		return err;
819 
820 	return wmi_driver_register(&dell_wmi_driver);
821 }
822 late_initcall(dell_wmi_init);
823 
824 static void __exit dell_wmi_exit(void)
825 {
826 	if (wmi_requires_smbios_request)
827 		dell_wmi_events_set_enabled(false);
828 
829 	wmi_driver_unregister(&dell_wmi_driver);
830 	dell_privacy_unregister_driver();
831 }
832 module_exit(dell_wmi_exit);
833 
834 MODULE_DEVICE_TABLE(wmi, dell_wmi_id_table);
835