1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/slab.h>
33#include <linux/types.h>
34#include <linux/input.h>
35#include <linux/input/sparse-keymap.h>
36#include <linux/acpi.h>
37#include <linux/string.h>
38#include <linux/dmi.h>
39#include <linux/wmi.h>
40#include <acpi/video.h>
41#include "dell-smbios.h"
42#include "dell-wmi-descriptor.h"
43
44MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
45MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
46MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
47MODULE_LICENSE("GPL");
48
49#define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
50
51static bool wmi_requires_smbios_request;
52
53MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
54
55struct dell_wmi_priv {
56 struct input_dev *input_dev;
57 u32 interface_version;
58};
59
60static int __init dmi_matched(const struct dmi_system_id *dmi)
61{
62 wmi_requires_smbios_request = 1;
63 return 1;
64}
65
66static const struct dmi_system_id dell_wmi_smbios_list[] __initconst = {
67 {
68 .callback = dmi_matched,
69 .ident = "Dell Inspiron M5110",
70 .matches = {
71 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
72 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
73 },
74 },
75 {
76 .callback = dmi_matched,
77 .ident = "Dell Vostro V131",
78 .matches = {
79 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
80 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
81 },
82 },
83 { }
84};
85
86
87
88
89
90
91
92
93static const struct key_entry dell_wmi_keymap_type_0000[] = {
94 { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
95
96
97 { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
98 { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
99
100
101 { KE_KEY, 0xe007, { KEY_BATTERY } },
102
103
104 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
105
106 { KE_KEY, 0xe009, { KEY_EJECTCD } },
107
108
109 { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
110
111
112 { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
113
114
115 { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
116
117
118 { KE_IGNORE, 0xe00e, { KEY_RESERVED } },
119
120
121 { KE_KEY, 0xe011, { KEY_WLAN } },
122
123
124 { KE_IGNORE, 0xe013, { KEY_RESERVED } },
125
126 { KE_IGNORE, 0xe020, { KEY_MUTE } },
127
128
129
130
131
132
133
134
135 { KE_KEY, 0xe025, { KEY_PROG4 } },
136
137
138 { KE_IGNORE, 0xe026, { KEY_RESERVED } },
139
140
141 { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
142
143
144
145
146
147 { KE_KEY, 0xe029, { KEY_PROG4 } },
148
149
150
151
152
153
154
155
156
157
158 { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
159 { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
160 { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
161 { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
162 { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
163
164
165 { KE_IGNORE, 0xe043, { KEY_RESERVED } },
166
167
168 { KE_IGNORE, 0xe044, { KEY_RESERVED } },
169
170
171
172
173
174
175
176
177
178
179
180 { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
181
182
183 { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
184
185
186
187
188
189 { KE_IGNORE, 0xe06e, { KEY_RESERVED } },
190
191 { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
192 { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
193 { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
194};
195
196struct dell_bios_keymap_entry {
197 u16 scancode;
198 u16 keycode;
199};
200
201struct dell_bios_hotkey_table {
202 struct dmi_header header;
203 struct dell_bios_keymap_entry keymap[];
204
205};
206
207struct dell_dmi_results {
208 int err;
209 int keymap_size;
210 struct key_entry *keymap;
211};
212
213
214static const u16 bios_to_linux_keycode[256] = {
215 [0] = KEY_MEDIA,
216 [1] = KEY_NEXTSONG,
217 [2] = KEY_PLAYPAUSE,
218 [3] = KEY_PREVIOUSSONG,
219 [4] = KEY_STOPCD,
220 [5] = KEY_UNKNOWN,
221 [6] = KEY_UNKNOWN,
222 [7] = KEY_UNKNOWN,
223 [8] = KEY_WWW,
224 [9] = KEY_UNKNOWN,
225 [10] = KEY_VOLUMEDOWN,
226 [11] = KEY_MUTE,
227 [12] = KEY_VOLUMEUP,
228 [13] = KEY_UNKNOWN,
229 [14] = KEY_BATTERY,
230 [15] = KEY_EJECTCD,
231 [16] = KEY_UNKNOWN,
232 [17] = KEY_SLEEP,
233 [18] = KEY_PROG1,
234 [19] = KEY_BRIGHTNESSDOWN,
235 [20] = KEY_BRIGHTNESSUP,
236 [21] = KEY_BRIGHTNESS_AUTO,
237 [22] = KEY_KBDILLUMTOGGLE,
238 [23] = KEY_UNKNOWN,
239 [24] = KEY_SWITCHVIDEOMODE,
240 [25] = KEY_UNKNOWN,
241 [26] = KEY_UNKNOWN,
242 [27] = KEY_SWITCHVIDEOMODE,
243 [28] = KEY_UNKNOWN,
244 [29] = KEY_UNKNOWN,
245 [30] = KEY_PROG2,
246 [31] = KEY_UNKNOWN,
247 [32] = KEY_UNKNOWN,
248 [33] = KEY_UNKNOWN,
249 [34] = KEY_UNKNOWN,
250 [35] = KEY_UNKNOWN,
251 [36] = KEY_UNKNOWN,
252 [37] = KEY_UNKNOWN,
253 [38] = KEY_MICMUTE,
254 [255] = KEY_PROG3,
255};
256
257
258
259
260
261
262
263static const struct key_entry dell_wmi_keymap_type_0010[] = {
264
265 { KE_IGNORE, 0x0, { KEY_RESERVED } },
266
267
268 { KE_IGNORE, 0x1, { KEY_RESERVED } },
269
270
271 { KE_KEY, 0x150, { KEY_MICMUTE } },
272
273
274 { KE_IGNORE, 0x151, { KEY_RESERVED } },
275
276
277 { KE_IGNORE, 0x152, { KEY_KBDILLUMTOGGLE } },
278
279
280
281
282
283 { KE_IGNORE, 0x153, { KEY_RFKILL } },
284
285
286 { KE_IGNORE, 0x154, { KEY_RESERVED } },
287
288
289
290
291
292
293
294 { KE_IGNORE, 0x155, { KEY_RESERVED } },
295
296
297 { KE_IGNORE, 0x156, { KEY_RESERVED } },
298 { KE_IGNORE, 0x157, { KEY_RESERVED } },
299
300
301 { KE_KEY, 0x850, { KEY_PROG1 } },
302 { KE_KEY, 0x851, { KEY_PROG2 } },
303 { KE_KEY, 0x852, { KEY_PROG3 } },
304
305
306
307
308
309 { KE_IGNORE, 0xe008, { KEY_RFKILL } },
310
311
312 { KE_IGNORE, 0xe035, { KEY_RESERVED } },
313};
314
315
316
317
318static const struct key_entry dell_wmi_keymap_type_0011[] = {
319
320 { KE_IGNORE, 0xfff0, { KEY_RESERVED } },
321
322
323 { KE_IGNORE, 0xfff1, { KEY_RESERVED } },
324
325
326 { KE_IGNORE, 0x01e1, { KEY_RESERVED } },
327 { KE_IGNORE, 0x02ea, { KEY_RESERVED } },
328 { KE_IGNORE, 0x02eb, { KEY_RESERVED } },
329 { KE_IGNORE, 0x02ec, { KEY_RESERVED } },
330 { KE_IGNORE, 0x02f6, { KEY_RESERVED } },
331};
332
333static void dell_wmi_process_key(struct wmi_device *wdev, int type, int code)
334{
335 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
336 const struct key_entry *key;
337
338 key = sparse_keymap_entry_from_scancode(priv->input_dev,
339 (type << 16) | code);
340 if (!key) {
341 pr_info("Unknown key with type 0x%04x and code 0x%04x pressed\n",
342 type, code);
343 return;
344 }
345
346 pr_debug("Key with type 0x%04x and code 0x%04x pressed\n", type, code);
347
348
349 if ((key->keycode == KEY_BRIGHTNESSUP ||
350 key->keycode == KEY_BRIGHTNESSDOWN) &&
351 acpi_video_handles_brightness_key_presses())
352 return;
353
354 if (type == 0x0000 && code == 0xe025 && !wmi_requires_smbios_request)
355 return;
356
357 if (key->keycode == KEY_KBDILLUMTOGGLE)
358 dell_laptop_call_notifier(
359 DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED, NULL);
360
361 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
362}
363
364static void dell_wmi_notify(struct wmi_device *wdev,
365 union acpi_object *obj)
366{
367 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
368 u16 *buffer_entry, *buffer_end;
369 acpi_size buffer_size;
370 int len, i;
371
372 if (obj->type != ACPI_TYPE_BUFFER) {
373 pr_warn("bad response type %x\n", obj->type);
374 return;
375 }
376
377 pr_debug("Received WMI event (%*ph)\n",
378 obj->buffer.length, obj->buffer.pointer);
379
380 buffer_entry = (u16 *)obj->buffer.pointer;
381 buffer_size = obj->buffer.length/2;
382 buffer_end = buffer_entry + buffer_size;
383
384
385
386
387
388
389
390
391
392
393
394
395
396 if (priv->interface_version == 0 && buffer_entry < buffer_end)
397 if (buffer_end > buffer_entry + buffer_entry[0] + 1)
398 buffer_end = buffer_entry + buffer_entry[0] + 1;
399
400 while (buffer_entry < buffer_end) {
401
402 len = buffer_entry[0];
403 if (len == 0)
404 break;
405
406 len++;
407
408 if (buffer_entry + len > buffer_end) {
409 pr_warn("Invalid length of WMI event\n");
410 break;
411 }
412
413 pr_debug("Process buffer (%*ph)\n", len*2, buffer_entry);
414
415 switch (buffer_entry[1]) {
416 case 0x0000:
417 if (len > 2)
418 dell_wmi_process_key(wdev, 0x0000,
419 buffer_entry[2]);
420
421 break;
422 case 0x0010:
423 case 0x0011:
424 for (i = 2; i < len; ++i)
425 dell_wmi_process_key(wdev, buffer_entry[1],
426 buffer_entry[i]);
427 break;
428 default:
429 pr_info("Unknown WMI event type 0x%x\n",
430 (int)buffer_entry[1]);
431 break;
432 }
433
434 buffer_entry += len;
435
436 }
437
438}
439
440static bool have_scancode(u32 scancode, const struct key_entry *keymap, int len)
441{
442 int i;
443
444 for (i = 0; i < len; i++)
445 if (keymap[i].code == scancode)
446 return true;
447
448 return false;
449}
450
451static void handle_dmi_entry(const struct dmi_header *dm, void *opaque)
452{
453 struct dell_dmi_results *results = opaque;
454 struct dell_bios_hotkey_table *table;
455 int hotkey_num, i, pos = 0;
456 struct key_entry *keymap;
457
458 if (results->err || results->keymap)
459 return;
460
461
462 if (dm->type != 0xb2)
463 return;
464
465 table = container_of(dm, struct dell_bios_hotkey_table, header);
466
467 hotkey_num = (table->header.length -
468 sizeof(struct dell_bios_hotkey_table)) /
469 sizeof(struct dell_bios_keymap_entry);
470 if (hotkey_num < 1) {
471
472
473
474
475
476
477
478 return;
479 }
480
481 keymap = kcalloc(hotkey_num, sizeof(struct key_entry), GFP_KERNEL);
482 if (!keymap) {
483 results->err = -ENOMEM;
484 return;
485 }
486
487 for (i = 0; i < hotkey_num; i++) {
488 const struct dell_bios_keymap_entry *bios_entry =
489 &table->keymap[i];
490
491
492 u16 keycode = (bios_entry->keycode <
493 ARRAY_SIZE(bios_to_linux_keycode)) ?
494 bios_to_linux_keycode[bios_entry->keycode] :
495 KEY_RESERVED;
496
497
498
499
500
501
502 if (keycode == KEY_RESERVED) {
503 pr_info("firmware scancode 0x%x maps to unrecognized keycode 0x%x\n",
504 bios_entry->scancode, bios_entry->keycode);
505 continue;
506 }
507
508 if (keycode == KEY_KBDILLUMTOGGLE)
509 keymap[pos].type = KE_IGNORE;
510 else
511 keymap[pos].type = KE_KEY;
512 keymap[pos].code = bios_entry->scancode;
513 keymap[pos].keycode = keycode;
514
515 pos++;
516 }
517
518 results->keymap = keymap;
519 results->keymap_size = pos;
520}
521
522static int dell_wmi_input_setup(struct wmi_device *wdev)
523{
524 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
525 struct dell_dmi_results dmi_results = {};
526 struct key_entry *keymap;
527 int err, i, pos = 0;
528
529 priv->input_dev = input_allocate_device();
530 if (!priv->input_dev)
531 return -ENOMEM;
532
533 priv->input_dev->name = "Dell WMI hotkeys";
534 priv->input_dev->id.bustype = BUS_HOST;
535 priv->input_dev->dev.parent = &wdev->dev;
536
537 if (dmi_walk(handle_dmi_entry, &dmi_results)) {
538
539
540
541
542
543 pr_warn("no DMI; using the old-style hotkey interface\n");
544 }
545
546 if (dmi_results.err) {
547 err = dmi_results.err;
548 goto err_free_dev;
549 }
550
551 keymap = kcalloc(dmi_results.keymap_size +
552 ARRAY_SIZE(dell_wmi_keymap_type_0000) +
553 ARRAY_SIZE(dell_wmi_keymap_type_0010) +
554 ARRAY_SIZE(dell_wmi_keymap_type_0011) +
555 1,
556 sizeof(struct key_entry), GFP_KERNEL);
557 if (!keymap) {
558 kfree(dmi_results.keymap);
559 err = -ENOMEM;
560 goto err_free_dev;
561 }
562
563
564 for (i = 0; i < dmi_results.keymap_size; i++) {
565 keymap[pos] = dmi_results.keymap[i];
566 keymap[pos].code |= (0x0010 << 16);
567 pos++;
568 }
569
570 kfree(dmi_results.keymap);
571
572
573 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0010); i++) {
574 const struct key_entry *entry = &dell_wmi_keymap_type_0010[i];
575
576
577
578
579
580
581 if (dmi_results.keymap_size &&
582 have_scancode(entry->code | (0x0010 << 16),
583 keymap, dmi_results.keymap_size)
584 )
585 continue;
586
587 keymap[pos] = *entry;
588 keymap[pos].code |= (0x0010 << 16);
589 pos++;
590 }
591
592
593 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0011); i++) {
594 keymap[pos] = dell_wmi_keymap_type_0011[i];
595 keymap[pos].code |= (0x0011 << 16);
596 pos++;
597 }
598
599
600
601
602
603 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0000); i++) {
604 keymap[pos] = dell_wmi_keymap_type_0000[i];
605 pos++;
606 }
607
608 keymap[pos].type = KE_END;
609
610 err = sparse_keymap_setup(priv->input_dev, keymap, NULL);
611
612
613
614
615 kfree(keymap);
616 if (err)
617 goto err_free_dev;
618
619 err = input_register_device(priv->input_dev);
620 if (err)
621 goto err_free_dev;
622
623 return 0;
624
625 err_free_dev:
626 input_free_device(priv->input_dev);
627 return err;
628}
629
630static void dell_wmi_input_destroy(struct wmi_device *wdev)
631{
632 struct dell_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
633
634 input_unregister_device(priv->input_dev);
635}
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653static int dell_wmi_events_set_enabled(bool enable)
654{
655 struct calling_interface_buffer *buffer;
656 int ret;
657
658 buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
659 if (!buffer)
660 return -ENOMEM;
661 buffer->cmd_class = CLASS_INFO;
662 buffer->cmd_select = SELECT_APP_REGISTRATION;
663 buffer->input[0] = 0x10000;
664 buffer->input[1] = 0x51534554;
665 buffer->input[3] = enable;
666 ret = dell_smbios_call(buffer);
667 if (ret == 0)
668 ret = buffer->output[0];
669 kfree(buffer);
670
671 return dell_smbios_error(ret);
672}
673
674static int dell_wmi_probe(struct wmi_device *wdev)
675{
676 struct dell_wmi_priv *priv;
677 int ret;
678
679 ret = dell_wmi_get_descriptor_valid();
680 if (ret)
681 return ret;
682
683 priv = devm_kzalloc(
684 &wdev->dev, sizeof(struct dell_wmi_priv), GFP_KERNEL);
685 if (!priv)
686 return -ENOMEM;
687 dev_set_drvdata(&wdev->dev, priv);
688
689 if (!dell_wmi_get_interface_version(&priv->interface_version))
690 return -EPROBE_DEFER;
691
692 return dell_wmi_input_setup(wdev);
693}
694
695static int dell_wmi_remove(struct wmi_device *wdev)
696{
697 dell_wmi_input_destroy(wdev);
698 return 0;
699}
700static const struct wmi_device_id dell_wmi_id_table[] = {
701 { .guid_string = DELL_EVENT_GUID },
702 { },
703};
704
705static struct wmi_driver dell_wmi_driver = {
706 .driver = {
707 .name = "dell-wmi",
708 },
709 .id_table = dell_wmi_id_table,
710 .probe = dell_wmi_probe,
711 .remove = dell_wmi_remove,
712 .notify = dell_wmi_notify,
713};
714
715static int __init dell_wmi_init(void)
716{
717 int err;
718
719 dmi_check_system(dell_wmi_smbios_list);
720
721 if (wmi_requires_smbios_request) {
722 err = dell_wmi_events_set_enabled(true);
723 if (err) {
724 pr_err("Failed to enable WMI events\n");
725 return err;
726 }
727 }
728
729 return wmi_driver_register(&dell_wmi_driver);
730}
731late_initcall(dell_wmi_init);
732
733static void __exit dell_wmi_exit(void)
734{
735 if (wmi_requires_smbios_request)
736 dell_wmi_events_set_enabled(false);
737
738 wmi_driver_unregister(&dell_wmi_driver);
739}
740module_exit(dell_wmi_exit);
741