2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
40 bool kbd_led_not_present;
41 bool kbd_led_levels_off_1;
42 bool kbd_missing_ac_tag;
44 bool needs_kbd_timeouts;
46 * Ordered list of timeouts expressed in seconds.
47 * The list must end with -1
52 static struct quirk_entry *quirks;
54 static struct quirk_entry quirk_dell_vostro_v130 = {
58 static int __init dmi_matched(const struct dmi_system_id *dmi)
60 quirks = dmi->driver_data;
65 * These values come from Windows utility provided by Dell. If any other value
66 * is used then BIOS silently set timeout to 0 without any error message.
68 static struct quirk_entry quirk_dell_xps13_9333 = {
69 .needs_kbd_timeouts = true,
70 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
73 static struct quirk_entry quirk_dell_xps13_9370 = {
74 .kbd_missing_ac_tag = true,
77 static struct quirk_entry quirk_dell_latitude_e6410 = {
78 .kbd_led_levels_off_1 = true,
81 static struct quirk_entry quirk_dell_inspiron_1012 = {
82 .kbd_led_not_present = true,
85 static struct platform_driver platform_driver = {
87 .name = "dell-laptop",
91 static struct platform_device *platform_device;
92 static struct backlight_device *dell_backlight_device;
93 static struct rfkill *wifi_rfkill;
94 static struct rfkill *bluetooth_rfkill;
95 static struct rfkill *wwan_rfkill;
96 static bool force_rfkill;
98 module_param(force_rfkill, bool, 0444);
99 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
101 static const struct dmi_system_id dell_device_table[] __initconst = {
103 .ident = "Dell laptop",
105 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
106 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
111 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
112 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
117 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
118 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
123 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
124 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
129 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
130 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
135 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
136 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
140 .ident = "Dell Computer Corporation",
142 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
143 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
148 MODULE_DEVICE_TABLE(dmi, dell_device_table);
150 static const struct dmi_system_id dell_quirks[] __initconst = {
152 .callback = dmi_matched,
153 .ident = "Dell Vostro V130",
155 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
156 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
158 .driver_data = &quirk_dell_vostro_v130,
161 .callback = dmi_matched,
162 .ident = "Dell Vostro V131",
164 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
165 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
167 .driver_data = &quirk_dell_vostro_v130,
170 .callback = dmi_matched,
171 .ident = "Dell Vostro 3350",
173 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
174 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
176 .driver_data = &quirk_dell_vostro_v130,
179 .callback = dmi_matched,
180 .ident = "Dell Vostro 3555",
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
183 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
185 .driver_data = &quirk_dell_vostro_v130,
188 .callback = dmi_matched,
189 .ident = "Dell Inspiron N311z",
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
194 .driver_data = &quirk_dell_vostro_v130,
197 .callback = dmi_matched,
198 .ident = "Dell Inspiron M5110",
200 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
201 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
203 .driver_data = &quirk_dell_vostro_v130,
206 .callback = dmi_matched,
207 .ident = "Dell Vostro 3360",
209 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
210 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
212 .driver_data = &quirk_dell_vostro_v130,
215 .callback = dmi_matched,
216 .ident = "Dell Vostro 3460",
218 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
219 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
221 .driver_data = &quirk_dell_vostro_v130,
224 .callback = dmi_matched,
225 .ident = "Dell Vostro 3560",
227 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
228 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
230 .driver_data = &quirk_dell_vostro_v130,
233 .callback = dmi_matched,
234 .ident = "Dell Vostro 3450",
236 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
237 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
239 .driver_data = &quirk_dell_vostro_v130,
242 .callback = dmi_matched,
243 .ident = "Dell Inspiron 5420",
245 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
246 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
248 .driver_data = &quirk_dell_vostro_v130,
251 .callback = dmi_matched,
252 .ident = "Dell Inspiron 5520",
254 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
255 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
257 .driver_data = &quirk_dell_vostro_v130,
260 .callback = dmi_matched,
261 .ident = "Dell Inspiron 5720",
263 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
264 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
266 .driver_data = &quirk_dell_vostro_v130,
269 .callback = dmi_matched,
270 .ident = "Dell Inspiron 7420",
272 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
273 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
275 .driver_data = &quirk_dell_vostro_v130,
278 .callback = dmi_matched,
279 .ident = "Dell Inspiron 7520",
281 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
282 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
284 .driver_data = &quirk_dell_vostro_v130,
287 .callback = dmi_matched,
288 .ident = "Dell Inspiron 7720",
290 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
291 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
293 .driver_data = &quirk_dell_vostro_v130,
296 .callback = dmi_matched,
297 .ident = "Dell XPS13 9333",
299 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
300 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
302 .driver_data = &quirk_dell_xps13_9333,
305 .callback = dmi_matched,
306 .ident = "Dell XPS 13 9370",
308 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
309 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
311 .driver_data = &quirk_dell_xps13_9370,
314 .callback = dmi_matched,
315 .ident = "Dell Latitude E6410",
317 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
318 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
320 .driver_data = &quirk_dell_latitude_e6410,
323 .callback = dmi_matched,
324 .ident = "Dell Inspiron 1012",
326 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
327 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
329 .driver_data = &quirk_dell_inspiron_1012,
332 .callback = dmi_matched,
333 .ident = "Dell Inspiron 1018",
335 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
336 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
338 .driver_data = &quirk_dell_inspiron_1012,
343 static void dell_fill_request(struct calling_interface_buffer *buffer,
344 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
346 memset(buffer, 0, sizeof(struct calling_interface_buffer));
347 buffer->input[0] = arg0;
348 buffer->input[1] = arg1;
349 buffer->input[2] = arg2;
350 buffer->input[3] = arg3;
353 static int dell_send_request(struct calling_interface_buffer *buffer,
354 u16 class, u16 select)
358 buffer->cmd_class = class;
359 buffer->cmd_select = select;
360 ret = dell_smbios_call(buffer);
363 return dell_smbios_error(buffer->output[0]);
367 * Derived from information in smbios-wireless-ctl:
369 * cbSelect 17, Value 11
371 * Return Wireless Info
372 * cbArg1, byte0 = 0x00
374 * cbRes1 Standard return codes (0, -1, -2)
375 * cbRes2 Info bit flags:
377 * 0 Hardware switch supported (1)
378 * 1 WiFi locator supported (1)
379 * 2 WLAN supported (1)
380 * 3 Bluetooth (BT) supported (1)
381 * 4 WWAN supported (1)
382 * 5 Wireless KBD supported (1)
383 * 6 Uw b supported (1)
384 * 7 WiGig supported (1)
385 * 8 WLAN installed (1)
387 * 10 WWAN installed (1)
388 * 11 Uw b installed (1)
389 * 12 WiGig installed (1)
391 * 16 Hardware (HW) switch is On (1)
392 * 17 WLAN disabled (1)
394 * 19 WWAN disabled (1)
395 * 20 Uw b disabled (1)
396 * 21 WiGig disabled (1)
399 * cbRes3 NVRAM size in bytes
400 * cbRes4, byte 0 NVRAM format version number
403 * Set QuickSet Radio Disable Flag
404 * cbArg1, byte0 = 0x01
413 * cbArg1, byte2 Flag bits:
414 * 0 QuickSet disables radio (1)
417 * cbRes1 Standard return codes (0, -1, -2)
418 * cbRes2 QuickSet (QS) radio disable bit map:
423 * 4 QS disables WIGIG
426 * Wireless Switch Configuration
427 * cbArg1, byte0 = 0x02
433 * 2 Set WiFi locator enable/disable
435 * Switch settings (if byte 1==1):
436 * 0 WLAN sw itch control (1)
437 * 1 BT sw itch control (1)
438 * 2 WWAN sw itch control (1)
439 * 3 UWB sw itch control (1)
440 * 4 WiGig sw itch control (1)
442 * cbArg1, byte2 Enable bits (if byte 1==2):
443 * 0 Enable WiFi locator (1)
445 * cbRes1 Standard return codes (0, -1, -2)
446 * cbRes2 QuickSet radio disable bit map:
447 * 0 WLAN controlled by sw itch (1)
448 * 1 BT controlled by sw itch (1)
449 * 2 WWAN controlled by sw itch (1)
450 * 3 UWB controlled by sw itch (1)
451 * 4 WiGig controlled by sw itch (1)
453 * 7 Wireless sw itch config locked (1)
454 * 8 WiFi locator enabled (1)
456 * 15 WiFi locator setting locked (1)
459 * Read Local Config Data (LCD)
460 * cbArg1, byte0 = 0x10
461 * cbArg1, byte1 NVRAM index low byte
462 * cbArg1, byte2 NVRAM index high byte
463 * cbRes1 Standard return codes (0, -1, -2)
464 * cbRes2 4 bytes read from LCD[index]
465 * cbRes3 4 bytes read from LCD[index+4]
466 * cbRes4 4 bytes read from LCD[index+8]
468 * Write Local Config Data (LCD)
469 * cbArg1, byte0 = 0x11
470 * cbArg1, byte1 NVRAM index low byte
471 * cbArg1, byte2 NVRAM index high byte
472 * cbArg2 4 bytes to w rite at LCD[index]
473 * cbArg3 4 bytes to w rite at LCD[index+4]
474 * cbArg4 4 bytes to w rite at LCD[index+8]
475 * cbRes1 Standard return codes (0, -1, -2)
477 * Populate Local Config Data from NVRAM
478 * cbArg1, byte0 = 0x12
479 * cbRes1 Standard return codes (0, -1, -2)
481 * Commit Local Config Data to NVRAM
482 * cbArg1, byte0 = 0x13
483 * cbRes1 Standard return codes (0, -1, -2)
486 static int dell_rfkill_set(void *data, bool blocked)
488 int disable = blocked ? 1 : 0;
489 unsigned long radio = (unsigned long)data;
490 int hwswitch_bit = (unsigned long)data - 1;
491 struct calling_interface_buffer buffer;
496 dell_fill_request(&buffer, 0, 0, 0, 0);
497 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
500 status = buffer.output[1];
502 dell_fill_request(&buffer, 0x2, 0, 0, 0);
503 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
506 hwswitch = buffer.output[1];
508 /* If the hardware switch controls this radio, and the hardware
509 switch is disabled, always disable the radio */
510 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
511 (status & BIT(0)) && !(status & BIT(16)))
514 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
515 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
519 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
522 if (status & BIT(0)) {
523 /* Has hw-switch, sync sw_state to BIOS */
524 struct calling_interface_buffer buffer;
525 int block = rfkill_blocked(rfkill);
526 dell_fill_request(&buffer,
527 1 | (radio << 8) | (block << 16), 0, 0, 0);
528 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
530 /* No hw-switch, sync BIOS state to sw_state */
531 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
535 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
536 int status, int hwswitch)
538 if (hwswitch & (BIT(radio - 1)))
539 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
542 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
544 int radio = ((unsigned long)data & 0xF);
545 struct calling_interface_buffer buffer;
550 dell_fill_request(&buffer, 0, 0, 0, 0);
551 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
552 status = buffer.output[1];
554 if (ret != 0 || !(status & BIT(0))) {
558 dell_fill_request(&buffer, 0x2, 0, 0, 0);
559 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
560 hwswitch = buffer.output[1];
565 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
568 static const struct rfkill_ops dell_rfkill_ops = {
569 .set_block = dell_rfkill_set,
570 .query = dell_rfkill_query,
573 static struct dentry *dell_laptop_dir;
575 static int dell_debugfs_show(struct seq_file *s, void *data)
577 struct calling_interface_buffer buffer;
583 dell_fill_request(&buffer, 0, 0, 0, 0);
584 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
587 status = buffer.output[1];
589 dell_fill_request(&buffer, 0x2, 0, 0, 0);
590 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
593 hwswitch_state = buffer.output[1];
595 seq_printf(s, "return:\t%d\n", ret);
596 seq_printf(s, "status:\t0x%X\n", status);
597 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
599 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
600 (status & BIT(1)) >> 1);
601 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
602 (status & BIT(2)) >> 2);
603 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
604 (status & BIT(3)) >> 3);
605 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
606 (status & BIT(4)) >> 4);
607 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
608 (status & BIT(5)) >> 5);
609 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
610 (status & BIT(6)) >> 6);
611 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
612 (status & BIT(7)) >> 7);
613 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
614 (status & BIT(8)) >> 8);
615 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
616 (status & BIT(9)) >> 9);
617 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
618 (status & BIT(10)) >> 10);
619 seq_printf(s, "Bit 11: UWB installed: %lu\n",
620 (status & BIT(11)) >> 11);
621 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
622 (status & BIT(12)) >> 12);
624 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
625 (status & BIT(16)) >> 16);
626 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
627 (status & BIT(17)) >> 17);
628 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
629 (status & BIT(18)) >> 18);
630 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
631 (status & BIT(19)) >> 19);
632 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
633 (status & BIT(20)) >> 20);
634 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
635 (status & BIT(21)) >> 21);
637 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
638 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
639 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
640 hwswitch_state & BIT(0));
641 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
642 (hwswitch_state & BIT(1)) >> 1);
643 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
644 (hwswitch_state & BIT(2)) >> 2);
645 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
646 (hwswitch_state & BIT(3)) >> 3);
647 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
648 (hwswitch_state & BIT(4)) >> 4);
649 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
650 (hwswitch_state & BIT(7)) >> 7);
651 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
652 (hwswitch_state & BIT(8)) >> 8);
653 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
654 (hwswitch_state & BIT(15)) >> 15);
658 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
660 static void dell_update_rfkill(struct work_struct *ignored)
662 struct calling_interface_buffer buffer;
667 dell_fill_request(&buffer, 0, 0, 0, 0);
668 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
669 status = buffer.output[1];
674 dell_fill_request(&buffer, 0x2, 0, 0, 0);
675 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
677 if (ret == 0 && (status & BIT(0)))
678 hwswitch = buffer.output[1];
681 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
682 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
684 if (bluetooth_rfkill) {
685 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
687 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
690 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
691 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
694 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
696 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
699 static bool extended;
701 if (str & I8042_STR_AUXDATA)
704 if (unlikely(data == 0xe0)) {
707 } else if (unlikely(extended)) {
710 schedule_delayed_work(&dell_rfkill_work,
711 round_jiffies_relative(HZ / 4));
720 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
721 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
723 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
724 unsigned long action, void *data)
726 schedule_delayed_work(&dell_rfkill_work, 0);
730 static struct notifier_block dell_laptop_rbtn_notifier = {
731 .notifier_call = dell_laptop_rbtn_notifier_call,
734 static int __init dell_setup_rfkill(void)
736 struct calling_interface_buffer buffer;
737 int status, ret, whitelisted;
741 * rfkill support causes trouble on various models, mostly Inspirons.
742 * So we whitelist certain series, and don't support rfkill on others.
745 product = dmi_get_system_info(DMI_PRODUCT_NAME);
746 if (product && (strncmp(product, "Latitude", 8) == 0 ||
747 strncmp(product, "Precision", 9) == 0))
749 if (!force_rfkill && !whitelisted)
752 dell_fill_request(&buffer, 0, 0, 0, 0);
753 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
754 status = buffer.output[1];
756 /* dell wireless info smbios call is not supported */
760 /* rfkill is only tested on laptops with a hwswitch */
761 if (!(status & BIT(0)) && !force_rfkill)
764 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
765 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
767 &dell_rfkill_ops, (void *) 1);
772 ret = rfkill_register(wifi_rfkill);
777 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
778 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
779 &platform_device->dev,
780 RFKILL_TYPE_BLUETOOTH,
781 &dell_rfkill_ops, (void *) 2);
782 if (!bluetooth_rfkill) {
786 ret = rfkill_register(bluetooth_rfkill);
791 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
792 wwan_rfkill = rfkill_alloc("dell-wwan",
793 &platform_device->dev,
795 &dell_rfkill_ops, (void *) 3);
800 ret = rfkill_register(wwan_rfkill);
806 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
807 * which can receive events from HW slider switch.
809 * Dell SMBIOS on whitelisted models supports controlling radio devices
810 * but does not support receiving HW button switch events. We can use
811 * i8042 filter hook function to receive keyboard data and handle
812 * keycode for HW button.
814 * So if it is possible we will use Dell Airplane Mode Switch ACPI
815 * driver for receiving HW events and Dell SMBIOS for setting rfkill
816 * states. If ACPI driver or device is not available we will fallback to
817 * i8042 filter hook function.
819 * To prevent duplicate rfkill devices which control and do same thing,
820 * dell-rbtn driver will automatically remove its own rfkill devices
821 * once function dell_rbtn_notifier_register() is called.
824 dell_rbtn_notifier_register_func =
825 symbol_request(dell_rbtn_notifier_register);
826 if (dell_rbtn_notifier_register_func) {
827 dell_rbtn_notifier_unregister_func =
828 symbol_request(dell_rbtn_notifier_unregister);
829 if (!dell_rbtn_notifier_unregister_func) {
830 symbol_put(dell_rbtn_notifier_register);
831 dell_rbtn_notifier_register_func = NULL;
835 if (dell_rbtn_notifier_register_func) {
836 ret = dell_rbtn_notifier_register_func(
837 &dell_laptop_rbtn_notifier);
838 symbol_put(dell_rbtn_notifier_register);
839 dell_rbtn_notifier_register_func = NULL;
841 symbol_put(dell_rbtn_notifier_unregister);
842 dell_rbtn_notifier_unregister_func = NULL;
845 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
850 pr_info("Using dell-rbtn acpi driver for receiving events\n");
851 } else if (ret != -ENODEV) {
852 pr_warn("Unable to register dell rbtn notifier\n");
855 ret = i8042_install_filter(dell_laptop_i8042_filter);
857 pr_warn("Unable to install key filter\n");
860 pr_info("Using i8042 filter function for receiving events\n");
866 rfkill_unregister(wwan_rfkill);
868 rfkill_destroy(wwan_rfkill);
869 if (bluetooth_rfkill)
870 rfkill_unregister(bluetooth_rfkill);
872 rfkill_destroy(bluetooth_rfkill);
874 rfkill_unregister(wifi_rfkill);
876 rfkill_destroy(wifi_rfkill);
881 static void dell_cleanup_rfkill(void)
883 if (dell_rbtn_notifier_unregister_func) {
884 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
885 symbol_put(dell_rbtn_notifier_unregister);
886 dell_rbtn_notifier_unregister_func = NULL;
888 i8042_remove_filter(dell_laptop_i8042_filter);
890 cancel_delayed_work_sync(&dell_rfkill_work);
892 rfkill_unregister(wifi_rfkill);
893 rfkill_destroy(wifi_rfkill);
895 if (bluetooth_rfkill) {
896 rfkill_unregister(bluetooth_rfkill);
897 rfkill_destroy(bluetooth_rfkill);
900 rfkill_unregister(wwan_rfkill);
901 rfkill_destroy(wwan_rfkill);
905 static int dell_send_intensity(struct backlight_device *bd)
907 struct calling_interface_buffer buffer;
908 struct calling_interface_token *token;
911 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
915 dell_fill_request(&buffer,
916 token->location, bd->props.brightness, 0, 0);
917 if (power_supply_is_system_supplied() > 0)
918 ret = dell_send_request(&buffer,
919 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
921 ret = dell_send_request(&buffer,
922 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
927 static int dell_get_intensity(struct backlight_device *bd)
929 struct calling_interface_buffer buffer;
930 struct calling_interface_token *token;
933 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
937 dell_fill_request(&buffer, token->location, 0, 0, 0);
938 if (power_supply_is_system_supplied() > 0)
939 ret = dell_send_request(&buffer,
940 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
942 ret = dell_send_request(&buffer,
943 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
946 ret = buffer.output[1];
951 static const struct backlight_ops dell_ops = {
952 .get_brightness = dell_get_intensity,
953 .update_status = dell_send_intensity,
956 static void touchpad_led_on(void)
960 i8042_command(&data, command | 1 << 12);
963 static void touchpad_led_off(void)
967 i8042_command(&data, command | 1 << 12);
970 static void touchpad_led_set(struct led_classdev *led_cdev,
971 enum led_brightness value)
979 static struct led_classdev touchpad_led = {
980 .name = "dell-laptop::touchpad",
981 .brightness_set = touchpad_led_set,
982 .flags = LED_CORE_SUSPENDRESUME,
985 static int __init touchpad_led_init(struct device *dev)
987 return led_classdev_register(dev, &touchpad_led);
990 static void touchpad_led_exit(void)
992 led_classdev_unregister(&touchpad_led);
996 * Derived from information in smbios-keyboard-ctl:
1000 * Keyboard illumination
1001 * cbArg1 determines the function to be performed
1003 * cbArg1 0x0 = Get Feature Information
1004 * cbRES1 Standard return codes (0, -1, -2)
1005 * cbRES2, word0 Bitmap of user-selectable modes
1006 * bit 0 Always off (All systems)
1007 * bit 1 Always on (Travis ATG, Siberia)
1008 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1009 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1010 * bit 4 Auto: Input-activity-based On; input-activity based Off
1011 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1012 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1013 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1014 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1015 * bits 9-15 Reserved for future use
1016 * cbRES2, byte2 Reserved for future use
1017 * cbRES2, byte3 Keyboard illumination type
1021 * 3-255 Reserved for future use
1022 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1023 * bit 0 Any keystroke
1024 * bit 1 Touchpad activity
1025 * bit 2 Pointing stick
1027 * bits 4-7 Reserved for future use
1028 * cbRES3, byte1 Supported timeout unit bitmap
1033 * bits 4-7 Reserved for future use
1034 * cbRES3, byte2 Number of keyboard light brightness levels
1035 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1036 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1037 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1038 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1040 * cbArg1 0x1 = Get Current State
1041 * cbRES1 Standard return codes (0, -1, -2)
1042 * cbRES2, word0 Bitmap of current mode state
1043 * bit 0 Always off (All systems)
1044 * bit 1 Always on (Travis ATG, Siberia)
1045 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1046 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1047 * bit 4 Auto: Input-activity-based On; input-activity based Off
1048 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1049 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1050 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1051 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1052 * bits 9-15 Reserved for future use
1053 * Note: Only One bit can be set
1054 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1055 * bit 0 Any keystroke
1056 * bit 1 Touchpad activity
1057 * bit 2 Pointing stick
1059 * bits 4-7 Reserved for future use
1060 * cbRES2, byte3 Current Timeout on battery
1061 * bits 7:6 Timeout units indicator:
1066 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1067 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1068 * are set upon return from the [Get feature information] call.
1069 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1070 * cbRES3, byte1 Current ALS reading
1071 * cbRES3, byte2 Current keyboard light level.
1072 * cbRES3, byte3 Current timeout on AC Power
1073 * bits 7:6 Timeout units indicator:
1078 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1079 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1080 * are set upon return from the upon return from the [Get Feature information] call.
1082 * cbArg1 0x2 = Set New State
1083 * cbRES1 Standard return codes (0, -1, -2)
1084 * cbArg2, word0 Bitmap of current mode state
1085 * bit 0 Always off (All systems)
1086 * bit 1 Always on (Travis ATG, Siberia)
1087 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1088 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1089 * bit 4 Auto: Input-activity-based On; input-activity based Off
1090 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1091 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1092 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1093 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1094 * bits 9-15 Reserved for future use
1095 * Note: Only One bit can be set
1096 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1097 * keyboard to turn off automatically.
1098 * bit 0 Any keystroke
1099 * bit 1 Touchpad activity
1100 * bit 2 Pointing stick
1102 * bits 4-7 Reserved for future use
1103 * cbArg2, byte3 Desired Timeout on battery
1104 * bits 7:6 Timeout units indicator:
1109 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1110 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1111 * cbArg3, byte2 Desired keyboard light level.
1112 * cbArg3, byte3 Desired Timeout on AC power
1113 * bits 7:6 Timeout units indicator:
1118 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1122 enum kbd_timeout_unit {
1123 KBD_TIMEOUT_SECONDS = 0,
1124 KBD_TIMEOUT_MINUTES,
1130 KBD_MODE_BIT_OFF = 0,
1133 KBD_MODE_BIT_TRIGGER_ALS,
1134 KBD_MODE_BIT_TRIGGER,
1135 KBD_MODE_BIT_TRIGGER_25,
1136 KBD_MODE_BIT_TRIGGER_50,
1137 KBD_MODE_BIT_TRIGGER_75,
1138 KBD_MODE_BIT_TRIGGER_100,
1141 #define kbd_is_als_mode_bit(bit) \
1142 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1143 #define kbd_is_trigger_mode_bit(bit) \
1144 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1145 #define kbd_is_level_mode_bit(bit) \
1146 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1164 u8 timeout_value_ac;
1171 static const int kbd_tokens[] = {
1173 KBD_LED_AUTO_25_TOKEN,
1174 KBD_LED_AUTO_50_TOKEN,
1175 KBD_LED_AUTO_75_TOKEN,
1176 KBD_LED_AUTO_100_TOKEN,
1180 static u16 kbd_token_bits;
1182 static struct kbd_info kbd_info;
1183 static bool kbd_als_supported;
1184 static bool kbd_triggers_supported;
1185 static bool kbd_timeout_ac_supported;
1187 static u8 kbd_mode_levels[16];
1188 static int kbd_mode_levels_count;
1190 static u8 kbd_previous_level;
1191 static u8 kbd_previous_mode_bit;
1193 static bool kbd_led_present;
1194 static DEFINE_MUTEX(kbd_led_mutex);
1195 static enum led_brightness kbd_led_level;
1198 * NOTE: there are three ways to set the keyboard backlight level.
1199 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1200 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1201 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1203 * There are laptops which support only one of these methods. If we want to
1204 * support as many machines as possible we need to implement all three methods.
1205 * The first two methods use the kbd_state structure. The third uses SMBIOS
1206 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1207 * keyboard backlight level via kbd_state.level.
1210 static int kbd_get_info(struct kbd_info *info)
1212 struct calling_interface_buffer buffer;
1216 dell_fill_request(&buffer, 0, 0, 0, 0);
1217 ret = dell_send_request(&buffer,
1218 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1222 info->modes = buffer.output[1] & 0xFFFF;
1223 info->type = (buffer.output[1] >> 24) & 0xFF;
1224 info->triggers = buffer.output[2] & 0xFF;
1225 units = (buffer.output[2] >> 8) & 0xFF;
1226 info->levels = (buffer.output[2] >> 16) & 0xFF;
1228 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1232 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1234 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1236 info->hours = (buffer.output[3] >> 16) & 0xFF;
1238 info->days = (buffer.output[3] >> 24) & 0xFF;
1243 static unsigned int kbd_get_max_level(void)
1245 if (kbd_info.levels != 0)
1246 return kbd_info.levels;
1247 if (kbd_mode_levels_count > 0)
1248 return kbd_mode_levels_count - 1;
1252 static int kbd_get_level(struct kbd_state *state)
1256 if (kbd_info.levels != 0)
1257 return state->level;
1259 if (kbd_mode_levels_count > 0) {
1260 for (i = 0; i < kbd_mode_levels_count; ++i)
1261 if (kbd_mode_levels[i] == state->mode_bit)
1269 static int kbd_set_level(struct kbd_state *state, u8 level)
1271 if (kbd_info.levels != 0) {
1273 kbd_previous_level = level;
1274 if (state->level == level)
1276 state->level = level;
1277 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1278 state->mode_bit = kbd_previous_mode_bit;
1279 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1280 kbd_previous_mode_bit = state->mode_bit;
1281 state->mode_bit = KBD_MODE_BIT_OFF;
1286 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1288 kbd_previous_level = level;
1289 state->mode_bit = kbd_mode_levels[level];
1296 static int kbd_get_state(struct kbd_state *state)
1298 struct calling_interface_buffer buffer;
1301 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1302 ret = dell_send_request(&buffer,
1303 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1307 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1308 if (state->mode_bit != 0)
1311 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1312 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1313 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1314 state->als_setting = buffer.output[2] & 0xFF;
1315 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1316 state->level = (buffer.output[2] >> 16) & 0xFF;
1317 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1318 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1323 static int kbd_set_state(struct kbd_state *state)
1325 struct calling_interface_buffer buffer;
1330 input1 = BIT(state->mode_bit) & 0xFFFF;
1331 input1 |= (state->triggers & 0xFF) << 16;
1332 input1 |= (state->timeout_value & 0x3F) << 24;
1333 input1 |= (state->timeout_unit & 0x3) << 30;
1334 input2 = state->als_setting & 0xFF;
1335 input2 |= (state->level & 0xFF) << 16;
1336 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1337 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1338 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1339 ret = dell_send_request(&buffer,
1340 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1345 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1349 ret = kbd_set_state(state);
1354 * When setting the new state fails,try to restore the previous one.
1355 * This is needed on some machines where BIOS sets a default state when
1356 * setting a new state fails. This default state could be all off.
1359 if (kbd_set_state(old))
1360 pr_err("Setting old previous keyboard state failed\n");
1365 static int kbd_set_token_bit(u8 bit)
1367 struct calling_interface_buffer buffer;
1368 struct calling_interface_token *token;
1371 if (bit >= ARRAY_SIZE(kbd_tokens))
1374 token = dell_smbios_find_token(kbd_tokens[bit]);
1378 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1379 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1384 static int kbd_get_token_bit(u8 bit)
1386 struct calling_interface_buffer buffer;
1387 struct calling_interface_token *token;
1391 if (bit >= ARRAY_SIZE(kbd_tokens))
1394 token = dell_smbios_find_token(kbd_tokens[bit]);
1398 dell_fill_request(&buffer, token->location, 0, 0, 0);
1399 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1400 val = buffer.output[1];
1405 return (val == token->value);
1408 static int kbd_get_first_active_token_bit(void)
1413 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1414 ret = kbd_get_token_bit(i);
1422 static int kbd_get_valid_token_counts(void)
1424 return hweight16(kbd_token_bits);
1427 static inline int kbd_init_info(void)
1429 struct kbd_state state;
1433 ret = kbd_get_info(&kbd_info);
1437 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1438 * timeout value which is shared for both battery and AC power
1439 * settings. So do not try to set AC values on old models.
1441 if ((quirks && quirks->kbd_missing_ac_tag) ||
1442 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1443 kbd_timeout_ac_supported = true;
1445 kbd_get_state(&state);
1447 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1448 if (kbd_info.seconds > 63)
1449 kbd_info.seconds = 63;
1450 if (kbd_info.minutes > 63)
1451 kbd_info.minutes = 63;
1452 if (kbd_info.hours > 63)
1453 kbd_info.hours = 63;
1454 if (kbd_info.days > 63)
1457 /* NOTE: On tested machines ON mode did not work and caused
1458 * problems (turned backlight off) so do not use it
1460 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1462 kbd_previous_level = kbd_get_level(&state);
1463 kbd_previous_mode_bit = state.mode_bit;
1465 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1466 kbd_previous_level = 1;
1468 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1469 kbd_previous_mode_bit =
1470 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1471 if (kbd_previous_mode_bit != 0)
1472 kbd_previous_mode_bit--;
1475 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1476 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1477 kbd_als_supported = true;
1479 if (kbd_info.modes & (
1480 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1481 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1482 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1484 kbd_triggers_supported = true;
1486 /* kbd_mode_levels[0] is reserved, see below */
1487 for (i = 0; i < 16; ++i)
1488 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1489 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1492 * Find the first supported mode and assign to kbd_mode_levels[0].
1493 * This should be 0 (off), but we cannot depend on the BIOS to
1496 if (kbd_mode_levels_count > 0) {
1497 for (i = 0; i < 16; ++i) {
1498 if (BIT(i) & kbd_info.modes) {
1499 kbd_mode_levels[0] = i;
1503 kbd_mode_levels_count++;
1510 static inline void kbd_init_tokens(void)
1514 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1515 if (dell_smbios_find_token(kbd_tokens[i]))
1516 kbd_token_bits |= BIT(i);
1519 static void kbd_init(void)
1523 if (quirks && quirks->kbd_led_not_present)
1526 ret = kbd_init_info();
1530 * Only supports keyboard backlight when it has at least two modes.
1532 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1533 || kbd_get_valid_token_counts() >= 2)
1534 kbd_led_present = true;
1537 static ssize_t kbd_led_timeout_store(struct device *dev,
1538 struct device_attribute *attr,
1539 const char *buf, size_t count)
1541 struct kbd_state new_state;
1542 struct kbd_state state;
1550 ret = sscanf(buf, "%d %c", &value, &ch);
1563 if (value > kbd_info.seconds)
1565 unit = KBD_TIMEOUT_SECONDS;
1568 if (value > kbd_info.minutes)
1570 unit = KBD_TIMEOUT_MINUTES;
1573 if (value > kbd_info.hours)
1575 unit = KBD_TIMEOUT_HOURS;
1578 if (value > kbd_info.days)
1580 unit = KBD_TIMEOUT_DAYS;
1586 if (quirks && quirks->needs_kbd_timeouts)
1590 /* Convert value from current units to seconds */
1592 case KBD_TIMEOUT_DAYS:
1594 case KBD_TIMEOUT_HOURS:
1596 case KBD_TIMEOUT_MINUTES:
1598 unit = KBD_TIMEOUT_SECONDS;
1601 if (quirks && quirks->needs_kbd_timeouts) {
1602 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1603 if (value <= quirks->kbd_timeouts[i]) {
1604 value = quirks->kbd_timeouts[i];
1610 if (value <= kbd_info.seconds && kbd_info.seconds) {
1611 unit = KBD_TIMEOUT_SECONDS;
1612 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1614 unit = KBD_TIMEOUT_MINUTES;
1615 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1617 unit = KBD_TIMEOUT_HOURS;
1618 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1619 value /= (60 * 60 * 24);
1620 unit = KBD_TIMEOUT_DAYS;
1626 mutex_lock(&kbd_led_mutex);
1628 ret = kbd_get_state(&state);
1634 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1635 new_state.timeout_value_ac = value;
1636 new_state.timeout_unit_ac = unit;
1638 new_state.timeout_value = value;
1639 new_state.timeout_unit = unit;
1642 ret = kbd_set_state_safe(&new_state, &state);
1648 mutex_unlock(&kbd_led_mutex);
1652 static ssize_t kbd_led_timeout_show(struct device *dev,
1653 struct device_attribute *attr, char *buf)
1655 struct kbd_state state;
1661 ret = kbd_get_state(&state);
1665 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1666 value = state.timeout_value_ac;
1667 unit = state.timeout_unit_ac;
1669 value = state.timeout_value;
1670 unit = state.timeout_unit;
1673 len = sprintf(buf, "%d", value);
1676 case KBD_TIMEOUT_SECONDS:
1677 return len + sprintf(buf+len, "s\n");
1678 case KBD_TIMEOUT_MINUTES:
1679 return len + sprintf(buf+len, "m\n");
1680 case KBD_TIMEOUT_HOURS:
1681 return len + sprintf(buf+len, "h\n");
1682 case KBD_TIMEOUT_DAYS:
1683 return len + sprintf(buf+len, "d\n");
1691 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1692 kbd_led_timeout_show, kbd_led_timeout_store);
1694 static const char * const kbd_led_triggers[] = {
1697 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1701 static ssize_t kbd_led_triggers_store(struct device *dev,
1702 struct device_attribute *attr,
1703 const char *buf, size_t count)
1705 struct kbd_state new_state;
1706 struct kbd_state state;
1707 bool triggers_enabled = false;
1708 int trigger_bit = -1;
1712 ret = sscanf(buf, "%20s", trigger);
1716 if (trigger[0] != '+' && trigger[0] != '-')
1719 mutex_lock(&kbd_led_mutex);
1721 ret = kbd_get_state(&state);
1725 if (kbd_triggers_supported)
1726 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1728 if (kbd_triggers_supported) {
1729 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1730 if (!(kbd_info.triggers & BIT(i)))
1732 if (!kbd_led_triggers[i])
1734 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1736 if (trigger[0] == '+' &&
1737 triggers_enabled && (state.triggers & BIT(i))) {
1741 if (trigger[0] == '-' &&
1742 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1751 if (trigger_bit == -1) {
1757 if (trigger[0] == '+')
1758 new_state.triggers |= BIT(trigger_bit);
1760 new_state.triggers &= ~BIT(trigger_bit);
1762 * NOTE: trackstick bit (2) must be disabled when
1763 * disabling touchpad bit (1), otherwise touchpad
1764 * bit (1) will not be disabled
1766 if (trigger_bit == 1)
1767 new_state.triggers &= ~BIT(2);
1769 if ((kbd_info.triggers & new_state.triggers) !=
1770 new_state.triggers) {
1774 if (new_state.triggers && !triggers_enabled) {
1775 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1776 kbd_set_level(&new_state, kbd_previous_level);
1777 } else if (new_state.triggers == 0) {
1778 kbd_set_level(&new_state, 0);
1780 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1784 ret = kbd_set_state_safe(&new_state, &state);
1787 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1788 kbd_previous_mode_bit = new_state.mode_bit;
1791 mutex_unlock(&kbd_led_mutex);
1795 static ssize_t kbd_led_triggers_show(struct device *dev,
1796 struct device_attribute *attr, char *buf)
1798 struct kbd_state state;
1799 bool triggers_enabled;
1803 ret = kbd_get_state(&state);
1809 if (kbd_triggers_supported) {
1810 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1811 level = kbd_get_level(&state);
1812 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1813 if (!(kbd_info.triggers & BIT(i)))
1815 if (!kbd_led_triggers[i])
1817 if ((triggers_enabled || level <= 0) &&
1818 (state.triggers & BIT(i)))
1822 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1827 buf[len - 1] = '\n';
1832 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1833 kbd_led_triggers_show, kbd_led_triggers_store);
1835 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1836 struct device_attribute *attr,
1837 const char *buf, size_t count)
1839 struct kbd_state new_state;
1840 struct kbd_state state;
1841 bool triggers_enabled = false;
1845 ret = kstrtoint(buf, 0, &enable);
1849 mutex_lock(&kbd_led_mutex);
1851 ret = kbd_get_state(&state);
1855 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1862 if (kbd_triggers_supported)
1863 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1866 if (triggers_enabled)
1867 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1869 new_state.mode_bit = KBD_MODE_BIT_ALS;
1871 if (triggers_enabled) {
1872 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1873 kbd_set_level(&new_state, kbd_previous_level);
1875 new_state.mode_bit = KBD_MODE_BIT_ON;
1878 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1883 ret = kbd_set_state_safe(&new_state, &state);
1886 kbd_previous_mode_bit = new_state.mode_bit;
1890 mutex_unlock(&kbd_led_mutex);
1894 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1895 struct device_attribute *attr,
1898 struct kbd_state state;
1899 bool enabled = false;
1902 ret = kbd_get_state(&state);
1905 enabled = kbd_is_als_mode_bit(state.mode_bit);
1907 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1910 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1911 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1913 static ssize_t kbd_led_als_setting_store(struct device *dev,
1914 struct device_attribute *attr,
1915 const char *buf, size_t count)
1917 struct kbd_state state;
1918 struct kbd_state new_state;
1922 ret = kstrtou8(buf, 10, &setting);
1926 mutex_lock(&kbd_led_mutex);
1928 ret = kbd_get_state(&state);
1933 new_state.als_setting = setting;
1935 ret = kbd_set_state_safe(&new_state, &state);
1941 mutex_unlock(&kbd_led_mutex);
1945 static ssize_t kbd_led_als_setting_show(struct device *dev,
1946 struct device_attribute *attr,
1949 struct kbd_state state;
1952 ret = kbd_get_state(&state);
1956 return sprintf(buf, "%d\n", state.als_setting);
1959 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1960 kbd_led_als_setting_show, kbd_led_als_setting_store);
1962 static struct attribute *kbd_led_attrs[] = {
1963 &dev_attr_stop_timeout.attr,
1964 &dev_attr_start_triggers.attr,
1968 static const struct attribute_group kbd_led_group = {
1969 .attrs = kbd_led_attrs,
1972 static struct attribute *kbd_led_als_attrs[] = {
1973 &dev_attr_als_enabled.attr,
1974 &dev_attr_als_setting.attr,
1978 static const struct attribute_group kbd_led_als_group = {
1979 .attrs = kbd_led_als_attrs,
1982 static const struct attribute_group *kbd_led_groups[] = {
1988 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1992 struct kbd_state state;
1994 if (kbd_get_max_level()) {
1995 ret = kbd_get_state(&state);
1998 ret = kbd_get_level(&state);
2004 if (kbd_get_valid_token_counts()) {
2005 ret = kbd_get_first_active_token_bit();
2008 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2009 num &= num - 1; /* clear the first bit set */
2012 return ffs(num) - 1;
2015 pr_warn("Keyboard brightness level control not supported\n");
2019 static int kbd_led_level_set(struct led_classdev *led_cdev,
2020 enum led_brightness value)
2022 enum led_brightness new_value = value;
2023 struct kbd_state state;
2024 struct kbd_state new_state;
2028 mutex_lock(&kbd_led_mutex);
2030 if (kbd_get_max_level()) {
2031 ret = kbd_get_state(&state);
2035 ret = kbd_set_level(&new_state, value);
2038 ret = kbd_set_state_safe(&new_state, &state);
2039 } else if (kbd_get_valid_token_counts()) {
2040 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2041 num &= num - 1; /* clear the first bit set */
2045 ret = kbd_set_token_bit(ffs(num) - 1);
2047 pr_warn("Keyboard brightness level control not supported\n");
2053 kbd_led_level = new_value;
2055 mutex_unlock(&kbd_led_mutex);
2059 static struct led_classdev kbd_led = {
2060 .name = "dell::kbd_backlight",
2061 .flags = LED_BRIGHT_HW_CHANGED,
2062 .brightness_set_blocking = kbd_led_level_set,
2063 .brightness_get = kbd_led_level_get,
2064 .groups = kbd_led_groups,
2067 static int __init kbd_led_init(struct device *dev)
2072 if (!kbd_led_present)
2074 if (!kbd_als_supported)
2075 kbd_led_groups[1] = NULL;
2076 kbd_led.max_brightness = kbd_get_max_level();
2077 if (!kbd_led.max_brightness) {
2078 kbd_led.max_brightness = kbd_get_valid_token_counts();
2079 if (kbd_led.max_brightness)
2080 kbd_led.max_brightness--;
2083 kbd_led_level = kbd_led_level_get(NULL);
2085 ret = led_classdev_register(dev, &kbd_led);
2087 kbd_led_present = false;
2092 static void brightness_set_exit(struct led_classdev *led_cdev,
2093 enum led_brightness value)
2095 /* Don't change backlight level on exit */
2098 static void kbd_led_exit(void)
2100 if (!kbd_led_present)
2102 kbd_led.brightness_set = brightness_set_exit;
2103 led_classdev_unregister(&kbd_led);
2106 static int dell_laptop_notifier_call(struct notifier_block *nb,
2107 unsigned long action, void *data)
2109 bool changed = false;
2110 enum led_brightness new_kbd_led_level;
2113 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2114 if (!kbd_led_present)
2117 mutex_lock(&kbd_led_mutex);
2118 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2119 if (kbd_led_level != new_kbd_led_level) {
2120 kbd_led_level = new_kbd_led_level;
2123 mutex_unlock(&kbd_led_mutex);
2126 led_classdev_notify_brightness_hw_changed(&kbd_led,
2134 static struct notifier_block dell_laptop_notifier = {
2135 .notifier_call = dell_laptop_notifier_call,
2138 int dell_micmute_led_set(int state)
2140 struct calling_interface_buffer buffer;
2141 struct calling_interface_token *token;
2144 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2145 else if (state == 1)
2146 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2153 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2154 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2158 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2160 static int __init dell_init(void)
2162 struct calling_interface_token *token;
2163 int max_intensity = 0;
2166 if (!dmi_check_system(dell_device_table))
2170 /* find if this machine support other functions */
2171 dmi_check_system(dell_quirks);
2173 ret = platform_driver_register(&platform_driver);
2175 goto fail_platform_driver;
2176 platform_device = platform_device_alloc("dell-laptop", -1);
2177 if (!platform_device) {
2179 goto fail_platform_device1;
2181 ret = platform_device_add(platform_device);
2183 goto fail_platform_device2;
2185 ret = dell_setup_rfkill();
2188 pr_warn("Unable to setup rfkill\n");
2192 if (quirks && quirks->touchpad_led)
2193 touchpad_led_init(&platform_device->dev);
2195 kbd_led_init(&platform_device->dev);
2197 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2198 if (dell_laptop_dir != NULL)
2199 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2200 &dell_debugfs_fops);
2202 dell_laptop_register_notifier(&dell_laptop_notifier);
2204 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2207 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2209 struct calling_interface_buffer buffer;
2211 dell_fill_request(&buffer, token->location, 0, 0, 0);
2212 ret = dell_send_request(&buffer,
2213 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2215 max_intensity = buffer.output[3];
2218 if (max_intensity) {
2219 struct backlight_properties props;
2220 memset(&props, 0, sizeof(struct backlight_properties));
2221 props.type = BACKLIGHT_PLATFORM;
2222 props.max_brightness = max_intensity;
2223 dell_backlight_device = backlight_device_register("dell_backlight",
2224 &platform_device->dev,
2229 if (IS_ERR(dell_backlight_device)) {
2230 ret = PTR_ERR(dell_backlight_device);
2231 dell_backlight_device = NULL;
2232 goto fail_backlight;
2235 dell_backlight_device->props.brightness =
2236 dell_get_intensity(dell_backlight_device);
2237 if (dell_backlight_device->props.brightness < 0) {
2238 ret = dell_backlight_device->props.brightness;
2239 goto fail_get_brightness;
2241 backlight_update_status(dell_backlight_device);
2246 fail_get_brightness:
2247 backlight_device_unregister(dell_backlight_device);
2249 dell_cleanup_rfkill();
2251 platform_device_del(platform_device);
2252 fail_platform_device2:
2253 platform_device_put(platform_device);
2254 fail_platform_device1:
2255 platform_driver_unregister(&platform_driver);
2256 fail_platform_driver:
2260 static void __exit dell_exit(void)
2262 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2263 debugfs_remove_recursive(dell_laptop_dir);
2264 if (quirks && quirks->touchpad_led)
2265 touchpad_led_exit();
2267 backlight_device_unregister(dell_backlight_device);
2268 dell_cleanup_rfkill();
2269 if (platform_device) {
2270 platform_device_unregister(platform_device);
2271 platform_driver_unregister(&platform_driver);
2275 /* dell-rbtn.c driver export functions which will not work correctly (and could
2276 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2277 * not problem when dell-rbtn.c is compiled as external module. When both files
2278 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2279 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2280 * This can be achieved by late_initcall() instead module_init().
2282 late_initcall(dell_init);
2283 module_exit(dell_exit);
2285 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2286 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2287 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2288 MODULE_DESCRIPTION("Dell laptop driver");
2289 MODULE_LICENSE("GPL");