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