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