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