1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Dell laptop extras
5 * Copyright (c) Red Hat <mjg@redhat.com>
6 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
7 * Copyright (c) 2014 Pali Rohár <pali@kernel.org>
9 * Based on documentation in the libsmbios package:
10 * Copyright (C) 2005-2014 Dell Inc.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/backlight.h>
20 #include <linux/err.h>
21 #include <linux/dmi.h>
23 #include <linux/rfkill.h>
24 #include <linux/power_supply.h>
25 #include <linux/acpi.h>
27 #include <linux/i8042.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
30 #include <acpi/video.h>
31 #include "dell-rbtn.h"
32 #include "dell-smbios.h"
36 bool kbd_led_not_present;
37 bool kbd_led_levels_off_1;
38 bool kbd_missing_ac_tag;
40 bool needs_kbd_timeouts;
42 * Ordered list of timeouts expressed in seconds.
43 * The list must end with -1
48 static struct quirk_entry *quirks;
50 static struct quirk_entry quirk_dell_vostro_v130 = {
54 static int __init dmi_matched(const struct dmi_system_id *dmi)
56 quirks = dmi->driver_data;
61 * These values come from Windows utility provided by Dell. If any other value
62 * is used then BIOS silently set timeout to 0 without any error message.
64 static struct quirk_entry quirk_dell_xps13_9333 = {
65 .needs_kbd_timeouts = true,
66 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
69 static struct quirk_entry quirk_dell_xps13_9370 = {
70 .kbd_missing_ac_tag = true,
73 static struct quirk_entry quirk_dell_latitude_e6410 = {
74 .kbd_led_levels_off_1 = true,
77 static struct quirk_entry quirk_dell_inspiron_1012 = {
78 .kbd_led_not_present = true,
81 static struct platform_driver platform_driver = {
83 .name = "dell-laptop",
87 static struct platform_device *platform_device;
88 static struct backlight_device *dell_backlight_device;
89 static struct rfkill *wifi_rfkill;
90 static struct rfkill *bluetooth_rfkill;
91 static struct rfkill *wwan_rfkill;
92 static bool force_rfkill;
94 module_param(force_rfkill, bool, 0444);
95 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
97 static const struct dmi_system_id dell_device_table[] __initconst = {
99 .ident = "Dell laptop",
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
120 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
125 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
126 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
136 .ident = "Dell Computer Corporation",
138 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
139 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
144 MODULE_DEVICE_TABLE(dmi, dell_device_table);
146 static const struct dmi_system_id dell_quirks[] __initconst = {
148 .callback = dmi_matched,
149 .ident = "Dell Vostro V130",
151 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
152 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
154 .driver_data = &quirk_dell_vostro_v130,
157 .callback = dmi_matched,
158 .ident = "Dell Vostro V131",
160 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
161 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
163 .driver_data = &quirk_dell_vostro_v130,
166 .callback = dmi_matched,
167 .ident = "Dell Vostro 3350",
169 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
170 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
172 .driver_data = &quirk_dell_vostro_v130,
175 .callback = dmi_matched,
176 .ident = "Dell Vostro 3555",
178 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
179 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
181 .driver_data = &quirk_dell_vostro_v130,
184 .callback = dmi_matched,
185 .ident = "Dell Inspiron N311z",
187 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
188 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
190 .driver_data = &quirk_dell_vostro_v130,
193 .callback = dmi_matched,
194 .ident = "Dell Inspiron M5110",
196 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
197 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
199 .driver_data = &quirk_dell_vostro_v130,
202 .callback = dmi_matched,
203 .ident = "Dell Vostro 3360",
205 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
206 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
208 .driver_data = &quirk_dell_vostro_v130,
211 .callback = dmi_matched,
212 .ident = "Dell Vostro 3460",
214 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
215 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
217 .driver_data = &quirk_dell_vostro_v130,
220 .callback = dmi_matched,
221 .ident = "Dell Vostro 3560",
223 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
224 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
226 .driver_data = &quirk_dell_vostro_v130,
229 .callback = dmi_matched,
230 .ident = "Dell Vostro 3450",
232 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
233 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
235 .driver_data = &quirk_dell_vostro_v130,
238 .callback = dmi_matched,
239 .ident = "Dell Inspiron 5420",
241 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
242 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
244 .driver_data = &quirk_dell_vostro_v130,
247 .callback = dmi_matched,
248 .ident = "Dell Inspiron 5520",
250 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
251 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
253 .driver_data = &quirk_dell_vostro_v130,
256 .callback = dmi_matched,
257 .ident = "Dell Inspiron 5720",
259 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
260 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
262 .driver_data = &quirk_dell_vostro_v130,
265 .callback = dmi_matched,
266 .ident = "Dell Inspiron 7420",
268 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
269 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
271 .driver_data = &quirk_dell_vostro_v130,
274 .callback = dmi_matched,
275 .ident = "Dell Inspiron 7520",
277 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
278 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
280 .driver_data = &quirk_dell_vostro_v130,
283 .callback = dmi_matched,
284 .ident = "Dell Inspiron 7720",
286 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
287 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
289 .driver_data = &quirk_dell_vostro_v130,
292 .callback = dmi_matched,
293 .ident = "Dell XPS13 9333",
295 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
296 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
298 .driver_data = &quirk_dell_xps13_9333,
301 .callback = dmi_matched,
302 .ident = "Dell XPS 13 9370",
304 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
305 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
307 .driver_data = &quirk_dell_xps13_9370,
310 .callback = dmi_matched,
311 .ident = "Dell Latitude E6410",
313 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
314 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
316 .driver_data = &quirk_dell_latitude_e6410,
319 .callback = dmi_matched,
320 .ident = "Dell Inspiron 1012",
322 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
323 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
325 .driver_data = &quirk_dell_inspiron_1012,
328 .callback = dmi_matched,
329 .ident = "Dell Inspiron 1018",
331 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
332 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
334 .driver_data = &quirk_dell_inspiron_1012,
339 static void dell_fill_request(struct calling_interface_buffer *buffer,
340 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
342 memset(buffer, 0, sizeof(struct calling_interface_buffer));
343 buffer->input[0] = arg0;
344 buffer->input[1] = arg1;
345 buffer->input[2] = arg2;
346 buffer->input[3] = arg3;
349 static int dell_send_request(struct calling_interface_buffer *buffer,
350 u16 class, u16 select)
354 buffer->cmd_class = class;
355 buffer->cmd_select = select;
356 ret = dell_smbios_call(buffer);
359 return dell_smbios_error(buffer->output[0]);
363 * Derived from information in smbios-wireless-ctl:
365 * cbSelect 17, Value 11
367 * Return Wireless Info
368 * cbArg1, byte0 = 0x00
370 * cbRes1 Standard return codes (0, -1, -2)
371 * cbRes2 Info bit flags:
373 * 0 Hardware switch supported (1)
374 * 1 WiFi locator supported (1)
375 * 2 WLAN supported (1)
376 * 3 Bluetooth (BT) supported (1)
377 * 4 WWAN supported (1)
378 * 5 Wireless KBD supported (1)
379 * 6 Uw b supported (1)
380 * 7 WiGig supported (1)
381 * 8 WLAN installed (1)
383 * 10 WWAN installed (1)
384 * 11 Uw b installed (1)
385 * 12 WiGig installed (1)
387 * 16 Hardware (HW) switch is On (1)
388 * 17 WLAN disabled (1)
390 * 19 WWAN disabled (1)
391 * 20 Uw b disabled (1)
392 * 21 WiGig disabled (1)
395 * cbRes3 NVRAM size in bytes
396 * cbRes4, byte 0 NVRAM format version number
399 * Set QuickSet Radio Disable Flag
400 * cbArg1, byte0 = 0x01
409 * cbArg1, byte2 Flag bits:
410 * 0 QuickSet disables radio (1)
413 * cbRes1 Standard return codes (0, -1, -2)
414 * cbRes2 QuickSet (QS) radio disable bit map:
419 * 4 QS disables WIGIG
422 * Wireless Switch Configuration
423 * cbArg1, byte0 = 0x02
429 * 2 Set WiFi locator enable/disable
431 * Switch settings (if byte 1==1):
432 * 0 WLAN sw itch control (1)
433 * 1 BT sw itch control (1)
434 * 2 WWAN sw itch control (1)
435 * 3 UWB sw itch control (1)
436 * 4 WiGig sw itch control (1)
438 * cbArg1, byte2 Enable bits (if byte 1==2):
439 * 0 Enable WiFi locator (1)
441 * cbRes1 Standard return codes (0, -1, -2)
442 * cbRes2 QuickSet radio disable bit map:
443 * 0 WLAN controlled by sw itch (1)
444 * 1 BT controlled by sw itch (1)
445 * 2 WWAN controlled by sw itch (1)
446 * 3 UWB controlled by sw itch (1)
447 * 4 WiGig controlled by sw itch (1)
449 * 7 Wireless sw itch config locked (1)
450 * 8 WiFi locator enabled (1)
452 * 15 WiFi locator setting locked (1)
455 * Read Local Config Data (LCD)
456 * cbArg1, byte0 = 0x10
457 * cbArg1, byte1 NVRAM index low byte
458 * cbArg1, byte2 NVRAM index high byte
459 * cbRes1 Standard return codes (0, -1, -2)
460 * cbRes2 4 bytes read from LCD[index]
461 * cbRes3 4 bytes read from LCD[index+4]
462 * cbRes4 4 bytes read from LCD[index+8]
464 * Write Local Config Data (LCD)
465 * cbArg1, byte0 = 0x11
466 * cbArg1, byte1 NVRAM index low byte
467 * cbArg1, byte2 NVRAM index high byte
468 * cbArg2 4 bytes to w rite at LCD[index]
469 * cbArg3 4 bytes to w rite at LCD[index+4]
470 * cbArg4 4 bytes to w rite at LCD[index+8]
471 * cbRes1 Standard return codes (0, -1, -2)
473 * Populate Local Config Data from NVRAM
474 * cbArg1, byte0 = 0x12
475 * cbRes1 Standard return codes (0, -1, -2)
477 * Commit Local Config Data to NVRAM
478 * cbArg1, byte0 = 0x13
479 * cbRes1 Standard return codes (0, -1, -2)
482 static int dell_rfkill_set(void *data, bool blocked)
484 int disable = blocked ? 1 : 0;
485 unsigned long radio = (unsigned long)data;
486 int hwswitch_bit = (unsigned long)data - 1;
487 struct calling_interface_buffer buffer;
492 dell_fill_request(&buffer, 0, 0, 0, 0);
493 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
496 status = buffer.output[1];
498 dell_fill_request(&buffer, 0x2, 0, 0, 0);
499 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
502 hwswitch = buffer.output[1];
504 /* If the hardware switch controls this radio, and the hardware
505 switch is disabled, always disable the radio */
506 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
507 (status & BIT(0)) && !(status & BIT(16)))
510 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
511 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
515 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
518 if (status & BIT(0)) {
519 /* Has hw-switch, sync sw_state to BIOS */
520 struct calling_interface_buffer buffer;
521 int block = rfkill_blocked(rfkill);
522 dell_fill_request(&buffer,
523 1 | (radio << 8) | (block << 16), 0, 0, 0);
524 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
526 /* No hw-switch, sync BIOS state to sw_state */
527 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
531 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
532 int status, int hwswitch)
534 if (hwswitch & (BIT(radio - 1)))
535 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
538 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
540 int radio = ((unsigned long)data & 0xF);
541 struct calling_interface_buffer buffer;
546 dell_fill_request(&buffer, 0, 0, 0, 0);
547 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
548 status = buffer.output[1];
550 if (ret != 0 || !(status & BIT(0))) {
554 dell_fill_request(&buffer, 0x2, 0, 0, 0);
555 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
556 hwswitch = buffer.output[1];
561 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
564 static const struct rfkill_ops dell_rfkill_ops = {
565 .set_block = dell_rfkill_set,
566 .query = dell_rfkill_query,
569 static struct dentry *dell_laptop_dir;
571 static int dell_debugfs_show(struct seq_file *s, void *data)
573 struct calling_interface_buffer buffer;
579 dell_fill_request(&buffer, 0, 0, 0, 0);
580 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
583 status = buffer.output[1];
585 dell_fill_request(&buffer, 0x2, 0, 0, 0);
586 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
589 hwswitch_state = buffer.output[1];
591 seq_printf(s, "return:\t%d\n", ret);
592 seq_printf(s, "status:\t0x%X\n", status);
593 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
595 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
596 (status & BIT(1)) >> 1);
597 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
598 (status & BIT(2)) >> 2);
599 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
600 (status & BIT(3)) >> 3);
601 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
602 (status & BIT(4)) >> 4);
603 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
604 (status & BIT(5)) >> 5);
605 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
606 (status & BIT(6)) >> 6);
607 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
608 (status & BIT(7)) >> 7);
609 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
610 (status & BIT(8)) >> 8);
611 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
612 (status & BIT(9)) >> 9);
613 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
614 (status & BIT(10)) >> 10);
615 seq_printf(s, "Bit 11: UWB installed: %lu\n",
616 (status & BIT(11)) >> 11);
617 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
618 (status & BIT(12)) >> 12);
620 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
621 (status & BIT(16)) >> 16);
622 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
623 (status & BIT(17)) >> 17);
624 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
625 (status & BIT(18)) >> 18);
626 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
627 (status & BIT(19)) >> 19);
628 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
629 (status & BIT(20)) >> 20);
630 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
631 (status & BIT(21)) >> 21);
633 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
634 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
635 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
636 hwswitch_state & BIT(0));
637 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
638 (hwswitch_state & BIT(1)) >> 1);
639 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
640 (hwswitch_state & BIT(2)) >> 2);
641 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
642 (hwswitch_state & BIT(3)) >> 3);
643 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
644 (hwswitch_state & BIT(4)) >> 4);
645 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
646 (hwswitch_state & BIT(7)) >> 7);
647 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
648 (hwswitch_state & BIT(8)) >> 8);
649 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
650 (hwswitch_state & BIT(15)) >> 15);
654 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
656 static void dell_update_rfkill(struct work_struct *ignored)
658 struct calling_interface_buffer buffer;
663 dell_fill_request(&buffer, 0, 0, 0, 0);
664 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
665 status = buffer.output[1];
670 dell_fill_request(&buffer, 0x2, 0, 0, 0);
671 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
673 if (ret == 0 && (status & BIT(0)))
674 hwswitch = buffer.output[1];
677 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
678 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
680 if (bluetooth_rfkill) {
681 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
683 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
686 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
687 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
690 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
692 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
695 static bool extended;
697 if (str & I8042_STR_AUXDATA)
700 if (unlikely(data == 0xe0)) {
703 } else if (unlikely(extended)) {
706 schedule_delayed_work(&dell_rfkill_work,
707 round_jiffies_relative(HZ / 4));
716 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
717 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
719 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
720 unsigned long action, void *data)
722 schedule_delayed_work(&dell_rfkill_work, 0);
726 static struct notifier_block dell_laptop_rbtn_notifier = {
727 .notifier_call = dell_laptop_rbtn_notifier_call,
730 static int __init dell_setup_rfkill(void)
732 struct calling_interface_buffer buffer;
733 int status, ret, whitelisted;
737 * rfkill support causes trouble on various models, mostly Inspirons.
738 * So we whitelist certain series, and don't support rfkill on others.
741 product = dmi_get_system_info(DMI_PRODUCT_NAME);
742 if (product && (strncmp(product, "Latitude", 8) == 0 ||
743 strncmp(product, "Precision", 9) == 0))
745 if (!force_rfkill && !whitelisted)
748 dell_fill_request(&buffer, 0, 0, 0, 0);
749 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
750 status = buffer.output[1];
752 /* dell wireless info smbios call is not supported */
756 /* rfkill is only tested on laptops with a hwswitch */
757 if (!(status & BIT(0)) && !force_rfkill)
760 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
761 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
763 &dell_rfkill_ops, (void *) 1);
768 ret = rfkill_register(wifi_rfkill);
773 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
774 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
775 &platform_device->dev,
776 RFKILL_TYPE_BLUETOOTH,
777 &dell_rfkill_ops, (void *) 2);
778 if (!bluetooth_rfkill) {
782 ret = rfkill_register(bluetooth_rfkill);
787 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
788 wwan_rfkill = rfkill_alloc("dell-wwan",
789 &platform_device->dev,
791 &dell_rfkill_ops, (void *) 3);
796 ret = rfkill_register(wwan_rfkill);
802 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
803 * which can receive events from HW slider switch.
805 * Dell SMBIOS on whitelisted models supports controlling radio devices
806 * but does not support receiving HW button switch events. We can use
807 * i8042 filter hook function to receive keyboard data and handle
808 * keycode for HW button.
810 * So if it is possible we will use Dell Airplane Mode Switch ACPI
811 * driver for receiving HW events and Dell SMBIOS for setting rfkill
812 * states. If ACPI driver or device is not available we will fallback to
813 * i8042 filter hook function.
815 * To prevent duplicate rfkill devices which control and do same thing,
816 * dell-rbtn driver will automatically remove its own rfkill devices
817 * once function dell_rbtn_notifier_register() is called.
820 dell_rbtn_notifier_register_func =
821 symbol_request(dell_rbtn_notifier_register);
822 if (dell_rbtn_notifier_register_func) {
823 dell_rbtn_notifier_unregister_func =
824 symbol_request(dell_rbtn_notifier_unregister);
825 if (!dell_rbtn_notifier_unregister_func) {
826 symbol_put(dell_rbtn_notifier_register);
827 dell_rbtn_notifier_register_func = NULL;
831 if (dell_rbtn_notifier_register_func) {
832 ret = dell_rbtn_notifier_register_func(
833 &dell_laptop_rbtn_notifier);
834 symbol_put(dell_rbtn_notifier_register);
835 dell_rbtn_notifier_register_func = NULL;
837 symbol_put(dell_rbtn_notifier_unregister);
838 dell_rbtn_notifier_unregister_func = NULL;
841 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
846 pr_info("Using dell-rbtn acpi driver for receiving events\n");
847 } else if (ret != -ENODEV) {
848 pr_warn("Unable to register dell rbtn notifier\n");
851 ret = i8042_install_filter(dell_laptop_i8042_filter);
853 pr_warn("Unable to install key filter\n");
856 pr_info("Using i8042 filter function for receiving events\n");
862 rfkill_unregister(wwan_rfkill);
864 rfkill_destroy(wwan_rfkill);
865 if (bluetooth_rfkill)
866 rfkill_unregister(bluetooth_rfkill);
868 rfkill_destroy(bluetooth_rfkill);
870 rfkill_unregister(wifi_rfkill);
872 rfkill_destroy(wifi_rfkill);
877 static void dell_cleanup_rfkill(void)
879 if (dell_rbtn_notifier_unregister_func) {
880 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
881 symbol_put(dell_rbtn_notifier_unregister);
882 dell_rbtn_notifier_unregister_func = NULL;
884 i8042_remove_filter(dell_laptop_i8042_filter);
886 cancel_delayed_work_sync(&dell_rfkill_work);
888 rfkill_unregister(wifi_rfkill);
889 rfkill_destroy(wifi_rfkill);
891 if (bluetooth_rfkill) {
892 rfkill_unregister(bluetooth_rfkill);
893 rfkill_destroy(bluetooth_rfkill);
896 rfkill_unregister(wwan_rfkill);
897 rfkill_destroy(wwan_rfkill);
901 static int dell_send_intensity(struct backlight_device *bd)
903 struct calling_interface_buffer buffer;
904 struct calling_interface_token *token;
907 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
911 dell_fill_request(&buffer,
912 token->location, bd->props.brightness, 0, 0);
913 if (power_supply_is_system_supplied() > 0)
914 ret = dell_send_request(&buffer,
915 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
917 ret = dell_send_request(&buffer,
918 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
923 static int dell_get_intensity(struct backlight_device *bd)
925 struct calling_interface_buffer buffer;
926 struct calling_interface_token *token;
929 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
933 dell_fill_request(&buffer, token->location, 0, 0, 0);
934 if (power_supply_is_system_supplied() > 0)
935 ret = dell_send_request(&buffer,
936 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
938 ret = dell_send_request(&buffer,
939 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
942 ret = buffer.output[1];
947 static const struct backlight_ops dell_ops = {
948 .get_brightness = dell_get_intensity,
949 .update_status = dell_send_intensity,
952 static void touchpad_led_on(void)
956 i8042_command(&data, command | 1 << 12);
959 static void touchpad_led_off(void)
963 i8042_command(&data, command | 1 << 12);
966 static void touchpad_led_set(struct led_classdev *led_cdev,
967 enum led_brightness value)
975 static struct led_classdev touchpad_led = {
976 .name = "dell-laptop::touchpad",
977 .brightness_set = touchpad_led_set,
978 .flags = LED_CORE_SUSPENDRESUME,
981 static int __init touchpad_led_init(struct device *dev)
983 return led_classdev_register(dev, &touchpad_led);
986 static void touchpad_led_exit(void)
988 led_classdev_unregister(&touchpad_led);
992 * Derived from information in smbios-keyboard-ctl:
996 * Keyboard illumination
997 * cbArg1 determines the function to be performed
999 * cbArg1 0x0 = Get Feature Information
1000 * cbRES1 Standard return codes (0, -1, -2)
1001 * cbRES2, word0 Bitmap of user-selectable modes
1002 * bit 0 Always off (All systems)
1003 * bit 1 Always on (Travis ATG, Siberia)
1004 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1005 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1006 * bit 4 Auto: Input-activity-based On; input-activity based Off
1007 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1008 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1009 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1010 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1011 * bits 9-15 Reserved for future use
1012 * cbRES2, byte2 Reserved for future use
1013 * cbRES2, byte3 Keyboard illumination type
1017 * 3-255 Reserved for future use
1018 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1019 * bit 0 Any keystroke
1020 * bit 1 Touchpad activity
1021 * bit 2 Pointing stick
1023 * bits 4-7 Reserved for future use
1024 * cbRES3, byte1 Supported timeout unit bitmap
1029 * bits 4-7 Reserved for future use
1030 * cbRES3, byte2 Number of keyboard light brightness levels
1031 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1032 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1033 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1034 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1036 * cbArg1 0x1 = Get Current State
1037 * cbRES1 Standard return codes (0, -1, -2)
1038 * cbRES2, word0 Bitmap of current mode state
1039 * bit 0 Always off (All systems)
1040 * bit 1 Always on (Travis ATG, Siberia)
1041 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1042 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1043 * bit 4 Auto: Input-activity-based On; input-activity based Off
1044 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1045 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1046 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1047 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1048 * bits 9-15 Reserved for future use
1049 * Note: Only One bit can be set
1050 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1051 * bit 0 Any keystroke
1052 * bit 1 Touchpad activity
1053 * bit 2 Pointing stick
1055 * bits 4-7 Reserved for future use
1056 * cbRES2, byte3 Current Timeout on battery
1057 * bits 7:6 Timeout units indicator:
1062 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1063 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1064 * are set upon return from the [Get feature information] call.
1065 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1066 * cbRES3, byte1 Current ALS reading
1067 * cbRES3, byte2 Current keyboard light level.
1068 * cbRES3, byte3 Current timeout on AC Power
1069 * bits 7:6 Timeout units indicator:
1074 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1075 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1076 * are set upon return from the upon return from the [Get Feature information] call.
1078 * cbArg1 0x2 = Set New State
1079 * cbRES1 Standard return codes (0, -1, -2)
1080 * cbArg2, word0 Bitmap of current mode state
1081 * bit 0 Always off (All systems)
1082 * bit 1 Always on (Travis ATG, Siberia)
1083 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1084 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1085 * bit 4 Auto: Input-activity-based On; input-activity based Off
1086 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1087 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1088 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1089 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1090 * bits 9-15 Reserved for future use
1091 * Note: Only One bit can be set
1092 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1093 * keyboard to turn off automatically.
1094 * bit 0 Any keystroke
1095 * bit 1 Touchpad activity
1096 * bit 2 Pointing stick
1098 * bits 4-7 Reserved for future use
1099 * cbArg2, byte3 Desired Timeout on battery
1100 * bits 7:6 Timeout units indicator:
1105 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1106 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1107 * cbArg3, byte2 Desired keyboard light level.
1108 * cbArg3, byte3 Desired Timeout on AC power
1109 * bits 7:6 Timeout units indicator:
1114 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1118 enum kbd_timeout_unit {
1119 KBD_TIMEOUT_SECONDS = 0,
1120 KBD_TIMEOUT_MINUTES,
1126 KBD_MODE_BIT_OFF = 0,
1129 KBD_MODE_BIT_TRIGGER_ALS,
1130 KBD_MODE_BIT_TRIGGER,
1131 KBD_MODE_BIT_TRIGGER_25,
1132 KBD_MODE_BIT_TRIGGER_50,
1133 KBD_MODE_BIT_TRIGGER_75,
1134 KBD_MODE_BIT_TRIGGER_100,
1137 #define kbd_is_als_mode_bit(bit) \
1138 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1139 #define kbd_is_trigger_mode_bit(bit) \
1140 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1141 #define kbd_is_level_mode_bit(bit) \
1142 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1160 u8 timeout_value_ac;
1167 static const int kbd_tokens[] = {
1169 KBD_LED_AUTO_25_TOKEN,
1170 KBD_LED_AUTO_50_TOKEN,
1171 KBD_LED_AUTO_75_TOKEN,
1172 KBD_LED_AUTO_100_TOKEN,
1176 static u16 kbd_token_bits;
1178 static struct kbd_info kbd_info;
1179 static bool kbd_als_supported;
1180 static bool kbd_triggers_supported;
1181 static bool kbd_timeout_ac_supported;
1183 static u8 kbd_mode_levels[16];
1184 static int kbd_mode_levels_count;
1186 static u8 kbd_previous_level;
1187 static u8 kbd_previous_mode_bit;
1189 static bool kbd_led_present;
1190 static DEFINE_MUTEX(kbd_led_mutex);
1191 static enum led_brightness kbd_led_level;
1194 * NOTE: there are three ways to set the keyboard backlight level.
1195 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1196 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1197 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1199 * There are laptops which support only one of these methods. If we want to
1200 * support as many machines as possible we need to implement all three methods.
1201 * The first two methods use the kbd_state structure. The third uses SMBIOS
1202 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1203 * keyboard backlight level via kbd_state.level.
1206 static int kbd_get_info(struct kbd_info *info)
1208 struct calling_interface_buffer buffer;
1212 dell_fill_request(&buffer, 0, 0, 0, 0);
1213 ret = dell_send_request(&buffer,
1214 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1218 info->modes = buffer.output[1] & 0xFFFF;
1219 info->type = (buffer.output[1] >> 24) & 0xFF;
1220 info->triggers = buffer.output[2] & 0xFF;
1221 units = (buffer.output[2] >> 8) & 0xFF;
1222 info->levels = (buffer.output[2] >> 16) & 0xFF;
1224 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1228 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1230 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1232 info->hours = (buffer.output[3] >> 16) & 0xFF;
1234 info->days = (buffer.output[3] >> 24) & 0xFF;
1239 static unsigned int kbd_get_max_level(void)
1241 if (kbd_info.levels != 0)
1242 return kbd_info.levels;
1243 if (kbd_mode_levels_count > 0)
1244 return kbd_mode_levels_count - 1;
1248 static int kbd_get_level(struct kbd_state *state)
1252 if (kbd_info.levels != 0)
1253 return state->level;
1255 if (kbd_mode_levels_count > 0) {
1256 for (i = 0; i < kbd_mode_levels_count; ++i)
1257 if (kbd_mode_levels[i] == state->mode_bit)
1265 static int kbd_set_level(struct kbd_state *state, u8 level)
1267 if (kbd_info.levels != 0) {
1269 kbd_previous_level = level;
1270 if (state->level == level)
1272 state->level = level;
1273 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1274 state->mode_bit = kbd_previous_mode_bit;
1275 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1276 kbd_previous_mode_bit = state->mode_bit;
1277 state->mode_bit = KBD_MODE_BIT_OFF;
1282 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1284 kbd_previous_level = level;
1285 state->mode_bit = kbd_mode_levels[level];
1292 static int kbd_get_state(struct kbd_state *state)
1294 struct calling_interface_buffer buffer;
1297 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1298 ret = dell_send_request(&buffer,
1299 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1303 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1304 if (state->mode_bit != 0)
1307 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1308 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1309 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1310 state->als_setting = buffer.output[2] & 0xFF;
1311 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1312 state->level = (buffer.output[2] >> 16) & 0xFF;
1313 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1314 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1319 static int kbd_set_state(struct kbd_state *state)
1321 struct calling_interface_buffer buffer;
1326 input1 = BIT(state->mode_bit) & 0xFFFF;
1327 input1 |= (state->triggers & 0xFF) << 16;
1328 input1 |= (state->timeout_value & 0x3F) << 24;
1329 input1 |= (state->timeout_unit & 0x3) << 30;
1330 input2 = state->als_setting & 0xFF;
1331 input2 |= (state->level & 0xFF) << 16;
1332 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1333 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1334 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1335 ret = dell_send_request(&buffer,
1336 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1341 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1345 ret = kbd_set_state(state);
1350 * When setting the new state fails,try to restore the previous one.
1351 * This is needed on some machines where BIOS sets a default state when
1352 * setting a new state fails. This default state could be all off.
1355 if (kbd_set_state(old))
1356 pr_err("Setting old previous keyboard state failed\n");
1361 static int kbd_set_token_bit(u8 bit)
1363 struct calling_interface_buffer buffer;
1364 struct calling_interface_token *token;
1367 if (bit >= ARRAY_SIZE(kbd_tokens))
1370 token = dell_smbios_find_token(kbd_tokens[bit]);
1374 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1375 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1380 static int kbd_get_token_bit(u8 bit)
1382 struct calling_interface_buffer buffer;
1383 struct calling_interface_token *token;
1387 if (bit >= ARRAY_SIZE(kbd_tokens))
1390 token = dell_smbios_find_token(kbd_tokens[bit]);
1394 dell_fill_request(&buffer, token->location, 0, 0, 0);
1395 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1396 val = buffer.output[1];
1401 return (val == token->value);
1404 static int kbd_get_first_active_token_bit(void)
1409 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1410 ret = kbd_get_token_bit(i);
1418 static int kbd_get_valid_token_counts(void)
1420 return hweight16(kbd_token_bits);
1423 static inline int kbd_init_info(void)
1425 struct kbd_state state;
1429 ret = kbd_get_info(&kbd_info);
1433 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1434 * timeout value which is shared for both battery and AC power
1435 * settings. So do not try to set AC values on old models.
1437 if ((quirks && quirks->kbd_missing_ac_tag) ||
1438 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1439 kbd_timeout_ac_supported = true;
1441 kbd_get_state(&state);
1443 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1444 if (kbd_info.seconds > 63)
1445 kbd_info.seconds = 63;
1446 if (kbd_info.minutes > 63)
1447 kbd_info.minutes = 63;
1448 if (kbd_info.hours > 63)
1449 kbd_info.hours = 63;
1450 if (kbd_info.days > 63)
1453 /* NOTE: On tested machines ON mode did not work and caused
1454 * problems (turned backlight off) so do not use it
1456 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1458 kbd_previous_level = kbd_get_level(&state);
1459 kbd_previous_mode_bit = state.mode_bit;
1461 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1462 kbd_previous_level = 1;
1464 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1465 kbd_previous_mode_bit =
1466 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1467 if (kbd_previous_mode_bit != 0)
1468 kbd_previous_mode_bit--;
1471 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1472 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1473 kbd_als_supported = true;
1475 if (kbd_info.modes & (
1476 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1477 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1478 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1480 kbd_triggers_supported = true;
1482 /* kbd_mode_levels[0] is reserved, see below */
1483 for (i = 0; i < 16; ++i)
1484 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1485 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1488 * Find the first supported mode and assign to kbd_mode_levels[0].
1489 * This should be 0 (off), but we cannot depend on the BIOS to
1492 if (kbd_mode_levels_count > 0) {
1493 for (i = 0; i < 16; ++i) {
1494 if (BIT(i) & kbd_info.modes) {
1495 kbd_mode_levels[0] = i;
1499 kbd_mode_levels_count++;
1506 static inline void kbd_init_tokens(void)
1510 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1511 if (dell_smbios_find_token(kbd_tokens[i]))
1512 kbd_token_bits |= BIT(i);
1515 static void kbd_init(void)
1519 if (quirks && quirks->kbd_led_not_present)
1522 ret = kbd_init_info();
1526 * Only supports keyboard backlight when it has at least two modes.
1528 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1529 || kbd_get_valid_token_counts() >= 2)
1530 kbd_led_present = true;
1533 static ssize_t kbd_led_timeout_store(struct device *dev,
1534 struct device_attribute *attr,
1535 const char *buf, size_t count)
1537 struct kbd_state new_state;
1538 struct kbd_state state;
1546 ret = sscanf(buf, "%d %c", &value, &ch);
1559 if (value > kbd_info.seconds)
1561 unit = KBD_TIMEOUT_SECONDS;
1564 if (value > kbd_info.minutes)
1566 unit = KBD_TIMEOUT_MINUTES;
1569 if (value > kbd_info.hours)
1571 unit = KBD_TIMEOUT_HOURS;
1574 if (value > kbd_info.days)
1576 unit = KBD_TIMEOUT_DAYS;
1582 if (quirks && quirks->needs_kbd_timeouts)
1586 /* Convert value from current units to seconds */
1588 case KBD_TIMEOUT_DAYS:
1591 case KBD_TIMEOUT_HOURS:
1594 case KBD_TIMEOUT_MINUTES:
1596 unit = KBD_TIMEOUT_SECONDS;
1599 if (quirks && quirks->needs_kbd_timeouts) {
1600 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1601 if (value <= quirks->kbd_timeouts[i]) {
1602 value = quirks->kbd_timeouts[i];
1608 if (value <= kbd_info.seconds && kbd_info.seconds) {
1609 unit = KBD_TIMEOUT_SECONDS;
1610 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1612 unit = KBD_TIMEOUT_MINUTES;
1613 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1615 unit = KBD_TIMEOUT_HOURS;
1616 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1617 value /= (60 * 60 * 24);
1618 unit = KBD_TIMEOUT_DAYS;
1624 mutex_lock(&kbd_led_mutex);
1626 ret = kbd_get_state(&state);
1632 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1633 new_state.timeout_value_ac = value;
1634 new_state.timeout_unit_ac = unit;
1636 new_state.timeout_value = value;
1637 new_state.timeout_unit = unit;
1640 ret = kbd_set_state_safe(&new_state, &state);
1646 mutex_unlock(&kbd_led_mutex);
1650 static ssize_t kbd_led_timeout_show(struct device *dev,
1651 struct device_attribute *attr, char *buf)
1653 struct kbd_state state;
1659 ret = kbd_get_state(&state);
1663 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1664 value = state.timeout_value_ac;
1665 unit = state.timeout_unit_ac;
1667 value = state.timeout_value;
1668 unit = state.timeout_unit;
1671 len = sprintf(buf, "%d", value);
1674 case KBD_TIMEOUT_SECONDS:
1675 return len + sprintf(buf+len, "s\n");
1676 case KBD_TIMEOUT_MINUTES:
1677 return len + sprintf(buf+len, "m\n");
1678 case KBD_TIMEOUT_HOURS:
1679 return len + sprintf(buf+len, "h\n");
1680 case KBD_TIMEOUT_DAYS:
1681 return len + sprintf(buf+len, "d\n");
1689 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1690 kbd_led_timeout_show, kbd_led_timeout_store);
1692 static const char * const kbd_led_triggers[] = {
1695 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1699 static ssize_t kbd_led_triggers_store(struct device *dev,
1700 struct device_attribute *attr,
1701 const char *buf, size_t count)
1703 struct kbd_state new_state;
1704 struct kbd_state state;
1705 bool triggers_enabled = false;
1706 int trigger_bit = -1;
1710 ret = sscanf(buf, "%20s", trigger);
1714 if (trigger[0] != '+' && trigger[0] != '-')
1717 mutex_lock(&kbd_led_mutex);
1719 ret = kbd_get_state(&state);
1723 if (kbd_triggers_supported)
1724 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1726 if (kbd_triggers_supported) {
1727 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1728 if (!(kbd_info.triggers & BIT(i)))
1730 if (!kbd_led_triggers[i])
1732 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1734 if (trigger[0] == '+' &&
1735 triggers_enabled && (state.triggers & BIT(i))) {
1739 if (trigger[0] == '-' &&
1740 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1749 if (trigger_bit == -1) {
1755 if (trigger[0] == '+')
1756 new_state.triggers |= BIT(trigger_bit);
1758 new_state.triggers &= ~BIT(trigger_bit);
1760 * NOTE: trackstick bit (2) must be disabled when
1761 * disabling touchpad bit (1), otherwise touchpad
1762 * bit (1) will not be disabled
1764 if (trigger_bit == 1)
1765 new_state.triggers &= ~BIT(2);
1767 if ((kbd_info.triggers & new_state.triggers) !=
1768 new_state.triggers) {
1772 if (new_state.triggers && !triggers_enabled) {
1773 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1774 kbd_set_level(&new_state, kbd_previous_level);
1775 } else if (new_state.triggers == 0) {
1776 kbd_set_level(&new_state, 0);
1778 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1782 ret = kbd_set_state_safe(&new_state, &state);
1785 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1786 kbd_previous_mode_bit = new_state.mode_bit;
1789 mutex_unlock(&kbd_led_mutex);
1793 static ssize_t kbd_led_triggers_show(struct device *dev,
1794 struct device_attribute *attr, char *buf)
1796 struct kbd_state state;
1797 bool triggers_enabled;
1801 ret = kbd_get_state(&state);
1807 if (kbd_triggers_supported) {
1808 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1809 level = kbd_get_level(&state);
1810 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1811 if (!(kbd_info.triggers & BIT(i)))
1813 if (!kbd_led_triggers[i])
1815 if ((triggers_enabled || level <= 0) &&
1816 (state.triggers & BIT(i)))
1820 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1825 buf[len - 1] = '\n';
1830 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1831 kbd_led_triggers_show, kbd_led_triggers_store);
1833 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1834 struct device_attribute *attr,
1835 const char *buf, size_t count)
1837 struct kbd_state new_state;
1838 struct kbd_state state;
1839 bool triggers_enabled = false;
1843 ret = kstrtoint(buf, 0, &enable);
1847 mutex_lock(&kbd_led_mutex);
1849 ret = kbd_get_state(&state);
1853 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1860 if (kbd_triggers_supported)
1861 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1864 if (triggers_enabled)
1865 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1867 new_state.mode_bit = KBD_MODE_BIT_ALS;
1869 if (triggers_enabled) {
1870 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1871 kbd_set_level(&new_state, kbd_previous_level);
1873 new_state.mode_bit = KBD_MODE_BIT_ON;
1876 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1881 ret = kbd_set_state_safe(&new_state, &state);
1884 kbd_previous_mode_bit = new_state.mode_bit;
1888 mutex_unlock(&kbd_led_mutex);
1892 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1893 struct device_attribute *attr,
1896 struct kbd_state state;
1897 bool enabled = false;
1900 ret = kbd_get_state(&state);
1903 enabled = kbd_is_als_mode_bit(state.mode_bit);
1905 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1908 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1909 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1911 static ssize_t kbd_led_als_setting_store(struct device *dev,
1912 struct device_attribute *attr,
1913 const char *buf, size_t count)
1915 struct kbd_state state;
1916 struct kbd_state new_state;
1920 ret = kstrtou8(buf, 10, &setting);
1924 mutex_lock(&kbd_led_mutex);
1926 ret = kbd_get_state(&state);
1931 new_state.als_setting = setting;
1933 ret = kbd_set_state_safe(&new_state, &state);
1939 mutex_unlock(&kbd_led_mutex);
1943 static ssize_t kbd_led_als_setting_show(struct device *dev,
1944 struct device_attribute *attr,
1947 struct kbd_state state;
1950 ret = kbd_get_state(&state);
1954 return sprintf(buf, "%d\n", state.als_setting);
1957 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1958 kbd_led_als_setting_show, kbd_led_als_setting_store);
1960 static struct attribute *kbd_led_attrs[] = {
1961 &dev_attr_stop_timeout.attr,
1962 &dev_attr_start_triggers.attr,
1966 static const struct attribute_group kbd_led_group = {
1967 .attrs = kbd_led_attrs,
1970 static struct attribute *kbd_led_als_attrs[] = {
1971 &dev_attr_als_enabled.attr,
1972 &dev_attr_als_setting.attr,
1976 static const struct attribute_group kbd_led_als_group = {
1977 .attrs = kbd_led_als_attrs,
1980 static const struct attribute_group *kbd_led_groups[] = {
1986 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1990 struct kbd_state state;
1992 if (kbd_get_max_level()) {
1993 ret = kbd_get_state(&state);
1996 ret = kbd_get_level(&state);
2002 if (kbd_get_valid_token_counts()) {
2003 ret = kbd_get_first_active_token_bit();
2006 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2007 num &= num - 1; /* clear the first bit set */
2010 return ffs(num) - 1;
2013 pr_warn("Keyboard brightness level control not supported\n");
2017 static int kbd_led_level_set(struct led_classdev *led_cdev,
2018 enum led_brightness value)
2020 enum led_brightness new_value = value;
2021 struct kbd_state state;
2022 struct kbd_state new_state;
2026 mutex_lock(&kbd_led_mutex);
2028 if (kbd_get_max_level()) {
2029 ret = kbd_get_state(&state);
2033 ret = kbd_set_level(&new_state, value);
2036 ret = kbd_set_state_safe(&new_state, &state);
2037 } else if (kbd_get_valid_token_counts()) {
2038 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2039 num &= num - 1; /* clear the first bit set */
2043 ret = kbd_set_token_bit(ffs(num) - 1);
2045 pr_warn("Keyboard brightness level control not supported\n");
2051 kbd_led_level = new_value;
2053 mutex_unlock(&kbd_led_mutex);
2057 static struct led_classdev kbd_led = {
2058 .name = "dell::kbd_backlight",
2059 .flags = LED_BRIGHT_HW_CHANGED,
2060 .brightness_set_blocking = kbd_led_level_set,
2061 .brightness_get = kbd_led_level_get,
2062 .groups = kbd_led_groups,
2065 static int __init kbd_led_init(struct device *dev)
2070 if (!kbd_led_present)
2072 if (!kbd_als_supported)
2073 kbd_led_groups[1] = NULL;
2074 kbd_led.max_brightness = kbd_get_max_level();
2075 if (!kbd_led.max_brightness) {
2076 kbd_led.max_brightness = kbd_get_valid_token_counts();
2077 if (kbd_led.max_brightness)
2078 kbd_led.max_brightness--;
2081 kbd_led_level = kbd_led_level_get(NULL);
2083 ret = led_classdev_register(dev, &kbd_led);
2085 kbd_led_present = false;
2090 static void brightness_set_exit(struct led_classdev *led_cdev,
2091 enum led_brightness value)
2093 /* Don't change backlight level on exit */
2096 static void kbd_led_exit(void)
2098 if (!kbd_led_present)
2100 kbd_led.brightness_set = brightness_set_exit;
2101 led_classdev_unregister(&kbd_led);
2104 static int dell_laptop_notifier_call(struct notifier_block *nb,
2105 unsigned long action, void *data)
2107 bool changed = false;
2108 enum led_brightness new_kbd_led_level;
2111 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2112 if (!kbd_led_present)
2115 mutex_lock(&kbd_led_mutex);
2116 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2117 if (kbd_led_level != new_kbd_led_level) {
2118 kbd_led_level = new_kbd_led_level;
2121 mutex_unlock(&kbd_led_mutex);
2124 led_classdev_notify_brightness_hw_changed(&kbd_led,
2132 static struct notifier_block dell_laptop_notifier = {
2133 .notifier_call = dell_laptop_notifier_call,
2136 static int micmute_led_set(struct led_classdev *led_cdev,
2137 enum led_brightness brightness)
2139 struct calling_interface_buffer buffer;
2140 struct calling_interface_token *token;
2141 int state = brightness != LED_OFF;
2144 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2146 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2151 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2152 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2157 static struct led_classdev micmute_led_cdev = {
2158 .name = "platform::micmute",
2159 .max_brightness = 1,
2160 .brightness_set_blocking = micmute_led_set,
2161 .default_trigger = "audio-micmute",
2164 static int __init dell_init(void)
2166 struct calling_interface_token *token;
2167 int max_intensity = 0;
2170 if (!dmi_check_system(dell_device_table))
2174 /* find if this machine support other functions */
2175 dmi_check_system(dell_quirks);
2177 ret = platform_driver_register(&platform_driver);
2179 goto fail_platform_driver;
2180 platform_device = platform_device_alloc("dell-laptop", -1);
2181 if (!platform_device) {
2183 goto fail_platform_device1;
2185 ret = platform_device_add(platform_device);
2187 goto fail_platform_device2;
2189 ret = dell_setup_rfkill();
2192 pr_warn("Unable to setup rfkill\n");
2196 if (quirks && quirks->touchpad_led)
2197 touchpad_led_init(&platform_device->dev);
2199 kbd_led_init(&platform_device->dev);
2201 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2202 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2203 &dell_debugfs_fops);
2205 dell_laptop_register_notifier(&dell_laptop_notifier);
2207 if (dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE) &&
2208 dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE)) {
2209 micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2210 ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2215 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2218 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2220 struct calling_interface_buffer buffer;
2222 dell_fill_request(&buffer, token->location, 0, 0, 0);
2223 ret = dell_send_request(&buffer,
2224 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2226 max_intensity = buffer.output[3];
2229 if (max_intensity) {
2230 struct backlight_properties props;
2231 memset(&props, 0, sizeof(struct backlight_properties));
2232 props.type = BACKLIGHT_PLATFORM;
2233 props.max_brightness = max_intensity;
2234 dell_backlight_device = backlight_device_register("dell_backlight",
2235 &platform_device->dev,
2240 if (IS_ERR(dell_backlight_device)) {
2241 ret = PTR_ERR(dell_backlight_device);
2242 dell_backlight_device = NULL;
2243 goto fail_backlight;
2246 dell_backlight_device->props.brightness =
2247 dell_get_intensity(dell_backlight_device);
2248 if (dell_backlight_device->props.brightness < 0) {
2249 ret = dell_backlight_device->props.brightness;
2250 goto fail_get_brightness;
2252 backlight_update_status(dell_backlight_device);
2257 fail_get_brightness:
2258 backlight_device_unregister(dell_backlight_device);
2260 led_classdev_unregister(&micmute_led_cdev);
2262 dell_cleanup_rfkill();
2264 platform_device_del(platform_device);
2265 fail_platform_device2:
2266 platform_device_put(platform_device);
2267 fail_platform_device1:
2268 platform_driver_unregister(&platform_driver);
2269 fail_platform_driver:
2273 static void __exit dell_exit(void)
2275 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2276 debugfs_remove_recursive(dell_laptop_dir);
2277 if (quirks && quirks->touchpad_led)
2278 touchpad_led_exit();
2280 backlight_device_unregister(dell_backlight_device);
2281 led_classdev_unregister(&micmute_led_cdev);
2282 dell_cleanup_rfkill();
2283 if (platform_device) {
2284 platform_device_unregister(platform_device);
2285 platform_driver_unregister(&platform_driver);
2289 /* dell-rbtn.c driver export functions which will not work correctly (and could
2290 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2291 * not problem when dell-rbtn.c is compiled as external module. When both files
2292 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2293 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2294 * This can be achieved by late_initcall() instead module_init().
2296 late_initcall(dell_init);
2297 module_exit(dell_exit);
2299 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2300 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2301 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
2302 MODULE_DESCRIPTION("Dell laptop driver");
2303 MODULE_LICENSE("GPL");