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