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"
34 #include "dell-wmi-privacy.h"
38 bool kbd_led_not_present;
39 bool kbd_led_levels_off_1;
40 bool kbd_missing_ac_tag;
42 bool needs_kbd_timeouts;
44 * Ordered list of timeouts expressed in seconds.
45 * The list must end with -1
50 static struct quirk_entry *quirks;
52 static struct quirk_entry quirk_dell_vostro_v130 = {
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
58 quirks = dmi->driver_data;
63 * These values come from Windows utility provided by Dell. If any other value
64 * is used then BIOS silently set timeout to 0 without any error message.
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 .needs_kbd_timeouts = true,
68 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
71 static struct quirk_entry quirk_dell_xps13_9370 = {
72 .kbd_missing_ac_tag = true,
75 static struct quirk_entry quirk_dell_latitude_e6410 = {
76 .kbd_led_levels_off_1 = true,
79 static struct quirk_entry quirk_dell_inspiron_1012 = {
80 .kbd_led_not_present = true,
83 static struct platform_driver platform_driver = {
85 .name = "dell-laptop",
89 static struct platform_device *platform_device;
90 static struct backlight_device *dell_backlight_device;
91 static struct rfkill *wifi_rfkill;
92 static struct rfkill *bluetooth_rfkill;
93 static struct rfkill *wwan_rfkill;
94 static bool force_rfkill;
95 static bool micmute_led_registered;
97 module_param(force_rfkill, bool, 0444);
98 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
100 static const struct dmi_system_id dell_device_table[] __initconst = {
102 .ident = "Dell laptop",
104 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
105 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
111 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
116 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
117 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
122 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
123 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
128 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
129 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
134 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
135 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
139 .ident = "Dell Computer Corporation",
141 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
142 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
147 MODULE_DEVICE_TABLE(dmi, dell_device_table);
149 static const struct dmi_system_id dell_quirks[] __initconst = {
151 .callback = dmi_matched,
152 .ident = "Dell Vostro V130",
154 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
155 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
157 .driver_data = &quirk_dell_vostro_v130,
160 .callback = dmi_matched,
161 .ident = "Dell Vostro V131",
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
166 .driver_data = &quirk_dell_vostro_v130,
169 .callback = dmi_matched,
170 .ident = "Dell Vostro 3350",
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
175 .driver_data = &quirk_dell_vostro_v130,
178 .callback = dmi_matched,
179 .ident = "Dell Vostro 3555",
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
184 .driver_data = &quirk_dell_vostro_v130,
187 .callback = dmi_matched,
188 .ident = "Dell Inspiron N311z",
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
193 .driver_data = &quirk_dell_vostro_v130,
196 .callback = dmi_matched,
197 .ident = "Dell Inspiron M5110",
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
202 .driver_data = &quirk_dell_vostro_v130,
205 .callback = dmi_matched,
206 .ident = "Dell Vostro 3360",
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
209 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
211 .driver_data = &quirk_dell_vostro_v130,
214 .callback = dmi_matched,
215 .ident = "Dell Vostro 3460",
217 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
218 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
220 .driver_data = &quirk_dell_vostro_v130,
223 .callback = dmi_matched,
224 .ident = "Dell Vostro 3560",
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
229 .driver_data = &quirk_dell_vostro_v130,
232 .callback = dmi_matched,
233 .ident = "Dell Vostro 3450",
235 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
236 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
238 .driver_data = &quirk_dell_vostro_v130,
241 .callback = dmi_matched,
242 .ident = "Dell Inspiron 5420",
244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
245 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
247 .driver_data = &quirk_dell_vostro_v130,
250 .callback = dmi_matched,
251 .ident = "Dell Inspiron 5520",
253 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
256 .driver_data = &quirk_dell_vostro_v130,
259 .callback = dmi_matched,
260 .ident = "Dell Inspiron 5720",
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
263 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
265 .driver_data = &quirk_dell_vostro_v130,
268 .callback = dmi_matched,
269 .ident = "Dell Inspiron 7420",
271 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
272 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
274 .driver_data = &quirk_dell_vostro_v130,
277 .callback = dmi_matched,
278 .ident = "Dell Inspiron 7520",
280 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
281 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
283 .driver_data = &quirk_dell_vostro_v130,
286 .callback = dmi_matched,
287 .ident = "Dell Inspiron 7720",
289 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
290 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
292 .driver_data = &quirk_dell_vostro_v130,
295 .callback = dmi_matched,
296 .ident = "Dell XPS13 9333",
298 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
299 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
301 .driver_data = &quirk_dell_xps13_9333,
304 .callback = dmi_matched,
305 .ident = "Dell XPS 13 9370",
307 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
310 .driver_data = &quirk_dell_xps13_9370,
313 .callback = dmi_matched,
314 .ident = "Dell Latitude E6410",
316 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
319 .driver_data = &quirk_dell_latitude_e6410,
322 .callback = dmi_matched,
323 .ident = "Dell Inspiron 1012",
325 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
326 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
328 .driver_data = &quirk_dell_inspiron_1012,
331 .callback = dmi_matched,
332 .ident = "Dell Inspiron 1018",
334 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
335 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
337 .driver_data = &quirk_dell_inspiron_1012,
342 static void dell_fill_request(struct calling_interface_buffer *buffer,
343 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
345 memset(buffer, 0, sizeof(struct calling_interface_buffer));
346 buffer->input[0] = arg0;
347 buffer->input[1] = arg1;
348 buffer->input[2] = arg2;
349 buffer->input[3] = arg3;
352 static int dell_send_request(struct calling_interface_buffer *buffer,
353 u16 class, u16 select)
357 buffer->cmd_class = class;
358 buffer->cmd_select = select;
359 ret = dell_smbios_call(buffer);
362 return dell_smbios_error(buffer->output[0]);
366 * Derived from information in smbios-wireless-ctl:
368 * cbSelect 17, Value 11
370 * Return Wireless Info
371 * cbArg1, byte0 = 0x00
373 * cbRes1 Standard return codes (0, -1, -2)
374 * cbRes2 Info bit flags:
376 * 0 Hardware switch supported (1)
377 * 1 WiFi locator supported (1)
378 * 2 WLAN supported (1)
379 * 3 Bluetooth (BT) supported (1)
380 * 4 WWAN supported (1)
381 * 5 Wireless KBD supported (1)
382 * 6 Uw b supported (1)
383 * 7 WiGig supported (1)
384 * 8 WLAN installed (1)
386 * 10 WWAN installed (1)
387 * 11 Uw b installed (1)
388 * 12 WiGig installed (1)
390 * 16 Hardware (HW) switch is On (1)
391 * 17 WLAN disabled (1)
393 * 19 WWAN disabled (1)
394 * 20 Uw b disabled (1)
395 * 21 WiGig disabled (1)
398 * cbRes3 NVRAM size in bytes
399 * cbRes4, byte 0 NVRAM format version number
402 * Set QuickSet Radio Disable Flag
403 * cbArg1, byte0 = 0x01
412 * cbArg1, byte2 Flag bits:
413 * 0 QuickSet disables radio (1)
416 * cbRes1 Standard return codes (0, -1, -2)
417 * cbRes2 QuickSet (QS) radio disable bit map:
422 * 4 QS disables WIGIG
425 * Wireless Switch Configuration
426 * cbArg1, byte0 = 0x02
432 * 2 Set WiFi locator enable/disable
434 * Switch settings (if byte 1==1):
435 * 0 WLAN sw itch control (1)
436 * 1 BT sw itch control (1)
437 * 2 WWAN sw itch control (1)
438 * 3 UWB sw itch control (1)
439 * 4 WiGig sw itch control (1)
441 * cbArg1, byte2 Enable bits (if byte 1==2):
442 * 0 Enable WiFi locator (1)
444 * cbRes1 Standard return codes (0, -1, -2)
445 * cbRes2 QuickSet radio disable bit map:
446 * 0 WLAN controlled by sw itch (1)
447 * 1 BT controlled by sw itch (1)
448 * 2 WWAN controlled by sw itch (1)
449 * 3 UWB controlled by sw itch (1)
450 * 4 WiGig controlled by sw itch (1)
452 * 7 Wireless sw itch config locked (1)
453 * 8 WiFi locator enabled (1)
455 * 15 WiFi locator setting locked (1)
458 * Read Local Config Data (LCD)
459 * cbArg1, byte0 = 0x10
460 * cbArg1, byte1 NVRAM index low byte
461 * cbArg1, byte2 NVRAM index high byte
462 * cbRes1 Standard return codes (0, -1, -2)
463 * cbRes2 4 bytes read from LCD[index]
464 * cbRes3 4 bytes read from LCD[index+4]
465 * cbRes4 4 bytes read from LCD[index+8]
467 * Write Local Config Data (LCD)
468 * cbArg1, byte0 = 0x11
469 * cbArg1, byte1 NVRAM index low byte
470 * cbArg1, byte2 NVRAM index high byte
471 * cbArg2 4 bytes to w rite at LCD[index]
472 * cbArg3 4 bytes to w rite at LCD[index+4]
473 * cbArg4 4 bytes to w rite at LCD[index+8]
474 * cbRes1 Standard return codes (0, -1, -2)
476 * Populate Local Config Data from NVRAM
477 * cbArg1, byte0 = 0x12
478 * cbRes1 Standard return codes (0, -1, -2)
480 * Commit Local Config Data to NVRAM
481 * cbArg1, byte0 = 0x13
482 * cbRes1 Standard return codes (0, -1, -2)
485 static int dell_rfkill_set(void *data, bool blocked)
487 int disable = blocked ? 1 : 0;
488 unsigned long radio = (unsigned long)data;
489 int hwswitch_bit = (unsigned long)data - 1;
490 struct calling_interface_buffer buffer;
495 dell_fill_request(&buffer, 0, 0, 0, 0);
496 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
499 status = buffer.output[1];
501 dell_fill_request(&buffer, 0x2, 0, 0, 0);
502 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
505 hwswitch = buffer.output[1];
507 /* If the hardware switch controls this radio, and the hardware
508 switch is disabled, always disable the radio */
509 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
510 (status & BIT(0)) && !(status & BIT(16)))
513 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
514 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
518 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
521 if (status & BIT(0)) {
522 /* Has hw-switch, sync sw_state to BIOS */
523 struct calling_interface_buffer buffer;
524 int block = rfkill_blocked(rfkill);
525 dell_fill_request(&buffer,
526 1 | (radio << 8) | (block << 16), 0, 0, 0);
527 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
529 /* No hw-switch, sync BIOS state to sw_state */
530 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
534 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
535 int status, int hwswitch)
537 if (hwswitch & (BIT(radio - 1)))
538 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
541 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
543 int radio = ((unsigned long)data & 0xF);
544 struct calling_interface_buffer buffer;
549 dell_fill_request(&buffer, 0, 0, 0, 0);
550 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
551 status = buffer.output[1];
553 if (ret != 0 || !(status & BIT(0))) {
557 dell_fill_request(&buffer, 0x2, 0, 0, 0);
558 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
559 hwswitch = buffer.output[1];
564 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
567 static const struct rfkill_ops dell_rfkill_ops = {
568 .set_block = dell_rfkill_set,
569 .query = dell_rfkill_query,
572 static struct dentry *dell_laptop_dir;
574 static int dell_debugfs_show(struct seq_file *s, void *data)
576 struct calling_interface_buffer buffer;
582 dell_fill_request(&buffer, 0, 0, 0, 0);
583 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
586 status = buffer.output[1];
588 dell_fill_request(&buffer, 0x2, 0, 0, 0);
589 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
592 hwswitch_state = buffer.output[1];
594 seq_printf(s, "return:\t%d\n", ret);
595 seq_printf(s, "status:\t0x%X\n", status);
596 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
598 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
599 (status & BIT(1)) >> 1);
600 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
601 (status & BIT(2)) >> 2);
602 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
603 (status & BIT(3)) >> 3);
604 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
605 (status & BIT(4)) >> 4);
606 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
607 (status & BIT(5)) >> 5);
608 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
609 (status & BIT(6)) >> 6);
610 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
611 (status & BIT(7)) >> 7);
612 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
613 (status & BIT(8)) >> 8);
614 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
615 (status & BIT(9)) >> 9);
616 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
617 (status & BIT(10)) >> 10);
618 seq_printf(s, "Bit 11: UWB installed: %lu\n",
619 (status & BIT(11)) >> 11);
620 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
621 (status & BIT(12)) >> 12);
623 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
624 (status & BIT(16)) >> 16);
625 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
626 (status & BIT(17)) >> 17);
627 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
628 (status & BIT(18)) >> 18);
629 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
630 (status & BIT(19)) >> 19);
631 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
632 (status & BIT(20)) >> 20);
633 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
634 (status & BIT(21)) >> 21);
636 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
637 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
638 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
639 hwswitch_state & BIT(0));
640 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
641 (hwswitch_state & BIT(1)) >> 1);
642 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
643 (hwswitch_state & BIT(2)) >> 2);
644 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
645 (hwswitch_state & BIT(3)) >> 3);
646 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
647 (hwswitch_state & BIT(4)) >> 4);
648 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
649 (hwswitch_state & BIT(7)) >> 7);
650 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
651 (hwswitch_state & BIT(8)) >> 8);
652 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
653 (hwswitch_state & BIT(15)) >> 15);
657 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
659 static void dell_update_rfkill(struct work_struct *ignored)
661 struct calling_interface_buffer buffer;
666 dell_fill_request(&buffer, 0, 0, 0, 0);
667 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
668 status = buffer.output[1];
673 dell_fill_request(&buffer, 0x2, 0, 0, 0);
674 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
676 if (ret == 0 && (status & BIT(0)))
677 hwswitch = buffer.output[1];
680 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
681 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
683 if (bluetooth_rfkill) {
684 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
686 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
689 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
690 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
693 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
695 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
698 static bool extended;
700 if (str & I8042_STR_AUXDATA)
703 if (unlikely(data == 0xe0)) {
706 } else if (unlikely(extended)) {
709 schedule_delayed_work(&dell_rfkill_work,
710 round_jiffies_relative(HZ / 4));
719 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
720 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
722 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
723 unsigned long action, void *data)
725 schedule_delayed_work(&dell_rfkill_work, 0);
729 static struct notifier_block dell_laptop_rbtn_notifier = {
730 .notifier_call = dell_laptop_rbtn_notifier_call,
733 static int __init dell_setup_rfkill(void)
735 struct calling_interface_buffer buffer;
736 int status, ret, whitelisted;
740 * rfkill support causes trouble on various models, mostly Inspirons.
741 * So we whitelist certain series, and don't support rfkill on others.
744 product = dmi_get_system_info(DMI_PRODUCT_NAME);
745 if (product && (strncmp(product, "Latitude", 8) == 0 ||
746 strncmp(product, "Precision", 9) == 0))
748 if (!force_rfkill && !whitelisted)
751 dell_fill_request(&buffer, 0, 0, 0, 0);
752 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
753 status = buffer.output[1];
755 /* dell wireless info smbios call is not supported */
759 /* rfkill is only tested on laptops with a hwswitch */
760 if (!(status & BIT(0)) && !force_rfkill)
763 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
764 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
766 &dell_rfkill_ops, (void *) 1);
771 ret = rfkill_register(wifi_rfkill);
776 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
777 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
778 &platform_device->dev,
779 RFKILL_TYPE_BLUETOOTH,
780 &dell_rfkill_ops, (void *) 2);
781 if (!bluetooth_rfkill) {
785 ret = rfkill_register(bluetooth_rfkill);
790 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
791 wwan_rfkill = rfkill_alloc("dell-wwan",
792 &platform_device->dev,
794 &dell_rfkill_ops, (void *) 3);
799 ret = rfkill_register(wwan_rfkill);
805 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
806 * which can receive events from HW slider switch.
808 * Dell SMBIOS on whitelisted models supports controlling radio devices
809 * but does not support receiving HW button switch events. We can use
810 * i8042 filter hook function to receive keyboard data and handle
811 * keycode for HW button.
813 * So if it is possible we will use Dell Airplane Mode Switch ACPI
814 * driver for receiving HW events and Dell SMBIOS for setting rfkill
815 * states. If ACPI driver or device is not available we will fallback to
816 * i8042 filter hook function.
818 * To prevent duplicate rfkill devices which control and do same thing,
819 * dell-rbtn driver will automatically remove its own rfkill devices
820 * once function dell_rbtn_notifier_register() is called.
823 dell_rbtn_notifier_register_func =
824 symbol_request(dell_rbtn_notifier_register);
825 if (dell_rbtn_notifier_register_func) {
826 dell_rbtn_notifier_unregister_func =
827 symbol_request(dell_rbtn_notifier_unregister);
828 if (!dell_rbtn_notifier_unregister_func) {
829 symbol_put(dell_rbtn_notifier_register);
830 dell_rbtn_notifier_register_func = NULL;
834 if (dell_rbtn_notifier_register_func) {
835 ret = dell_rbtn_notifier_register_func(
836 &dell_laptop_rbtn_notifier);
837 symbol_put(dell_rbtn_notifier_register);
838 dell_rbtn_notifier_register_func = NULL;
840 symbol_put(dell_rbtn_notifier_unregister);
841 dell_rbtn_notifier_unregister_func = NULL;
844 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
849 pr_info("Using dell-rbtn acpi driver for receiving events\n");
850 } else if (ret != -ENODEV) {
851 pr_warn("Unable to register dell rbtn notifier\n");
854 ret = i8042_install_filter(dell_laptop_i8042_filter);
856 pr_warn("Unable to install key filter\n");
859 pr_info("Using i8042 filter function for receiving events\n");
865 rfkill_unregister(wwan_rfkill);
867 rfkill_destroy(wwan_rfkill);
868 if (bluetooth_rfkill)
869 rfkill_unregister(bluetooth_rfkill);
871 rfkill_destroy(bluetooth_rfkill);
873 rfkill_unregister(wifi_rfkill);
875 rfkill_destroy(wifi_rfkill);
880 static void dell_cleanup_rfkill(void)
882 if (dell_rbtn_notifier_unregister_func) {
883 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
884 symbol_put(dell_rbtn_notifier_unregister);
885 dell_rbtn_notifier_unregister_func = NULL;
887 i8042_remove_filter(dell_laptop_i8042_filter);
889 cancel_delayed_work_sync(&dell_rfkill_work);
891 rfkill_unregister(wifi_rfkill);
892 rfkill_destroy(wifi_rfkill);
894 if (bluetooth_rfkill) {
895 rfkill_unregister(bluetooth_rfkill);
896 rfkill_destroy(bluetooth_rfkill);
899 rfkill_unregister(wwan_rfkill);
900 rfkill_destroy(wwan_rfkill);
904 static int dell_send_intensity(struct backlight_device *bd)
906 struct calling_interface_buffer buffer;
907 struct calling_interface_token *token;
910 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
914 dell_fill_request(&buffer,
915 token->location, bd->props.brightness, 0, 0);
916 if (power_supply_is_system_supplied() > 0)
917 ret = dell_send_request(&buffer,
918 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
920 ret = dell_send_request(&buffer,
921 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
926 static int dell_get_intensity(struct backlight_device *bd)
928 struct calling_interface_buffer buffer;
929 struct calling_interface_token *token;
932 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
936 dell_fill_request(&buffer, token->location, 0, 0, 0);
937 if (power_supply_is_system_supplied() > 0)
938 ret = dell_send_request(&buffer,
939 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
941 ret = dell_send_request(&buffer,
942 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
945 ret = buffer.output[1];
950 static const struct backlight_ops dell_ops = {
951 .get_brightness = dell_get_intensity,
952 .update_status = dell_send_intensity,
955 static void touchpad_led_on(void)
959 i8042_command(&data, command | 1 << 12);
962 static void touchpad_led_off(void)
966 i8042_command(&data, command | 1 << 12);
969 static void touchpad_led_set(struct led_classdev *led_cdev,
970 enum led_brightness value)
978 static struct led_classdev touchpad_led = {
979 .name = "dell-laptop::touchpad",
980 .brightness_set = touchpad_led_set,
981 .flags = LED_CORE_SUSPENDRESUME,
984 static int __init touchpad_led_init(struct device *dev)
986 return led_classdev_register(dev, &touchpad_led);
989 static void touchpad_led_exit(void)
991 led_classdev_unregister(&touchpad_led);
995 * Derived from information in smbios-keyboard-ctl:
999 * Keyboard illumination
1000 * cbArg1 determines the function to be performed
1002 * cbArg1 0x0 = Get Feature Information
1003 * cbRES1 Standard return codes (0, -1, -2)
1004 * cbRES2, word0 Bitmap of user-selectable modes
1005 * bit 0 Always off (All systems)
1006 * bit 1 Always on (Travis ATG, Siberia)
1007 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1008 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1009 * bit 4 Auto: Input-activity-based On; input-activity based Off
1010 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1011 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1012 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1013 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1014 * bits 9-15 Reserved for future use
1015 * cbRES2, byte2 Reserved for future use
1016 * cbRES2, byte3 Keyboard illumination type
1020 * 3-255 Reserved for future use
1021 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1022 * bit 0 Any keystroke
1023 * bit 1 Touchpad activity
1024 * bit 2 Pointing stick
1026 * bits 4-7 Reserved for future use
1027 * cbRES3, byte1 Supported timeout unit bitmap
1032 * bits 4-7 Reserved for future use
1033 * cbRES3, byte2 Number of keyboard light brightness levels
1034 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1035 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1036 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1037 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1039 * cbArg1 0x1 = Get Current State
1040 * cbRES1 Standard return codes (0, -1, -2)
1041 * cbRES2, word0 Bitmap of current mode state
1042 * bit 0 Always off (All systems)
1043 * bit 1 Always on (Travis ATG, Siberia)
1044 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1045 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1046 * bit 4 Auto: Input-activity-based On; input-activity based Off
1047 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1048 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1049 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1050 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1051 * bits 9-15 Reserved for future use
1052 * Note: Only One bit can be set
1053 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1054 * bit 0 Any keystroke
1055 * bit 1 Touchpad activity
1056 * bit 2 Pointing stick
1058 * bits 4-7 Reserved for future use
1059 * cbRES2, byte3 Current Timeout on battery
1060 * bits 7:6 Timeout units indicator:
1065 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1066 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1067 * are set upon return from the [Get feature information] call.
1068 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1069 * cbRES3, byte1 Current ALS reading
1070 * cbRES3, byte2 Current keyboard light level.
1071 * cbRES3, byte3 Current timeout on AC Power
1072 * bits 7:6 Timeout units indicator:
1077 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1078 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1079 * are set upon return from the upon return from the [Get Feature information] call.
1081 * cbArg1 0x2 = Set New State
1082 * cbRES1 Standard return codes (0, -1, -2)
1083 * cbArg2, word0 Bitmap of current mode state
1084 * bit 0 Always off (All systems)
1085 * bit 1 Always on (Travis ATG, Siberia)
1086 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1087 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1088 * bit 4 Auto: Input-activity-based On; input-activity based Off
1089 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1090 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1091 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1092 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1093 * bits 9-15 Reserved for future use
1094 * Note: Only One bit can be set
1095 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1096 * keyboard to turn off automatically.
1097 * bit 0 Any keystroke
1098 * bit 1 Touchpad activity
1099 * bit 2 Pointing stick
1101 * bits 4-7 Reserved for future use
1102 * cbArg2, byte3 Desired Timeout on battery
1103 * bits 7:6 Timeout units indicator:
1108 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1109 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1110 * cbArg3, byte2 Desired keyboard light level.
1111 * cbArg3, byte3 Desired Timeout on AC power
1112 * bits 7:6 Timeout units indicator:
1117 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1121 enum kbd_timeout_unit {
1122 KBD_TIMEOUT_SECONDS = 0,
1123 KBD_TIMEOUT_MINUTES,
1129 KBD_MODE_BIT_OFF = 0,
1132 KBD_MODE_BIT_TRIGGER_ALS,
1133 KBD_MODE_BIT_TRIGGER,
1134 KBD_MODE_BIT_TRIGGER_25,
1135 KBD_MODE_BIT_TRIGGER_50,
1136 KBD_MODE_BIT_TRIGGER_75,
1137 KBD_MODE_BIT_TRIGGER_100,
1140 #define kbd_is_als_mode_bit(bit) \
1141 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1142 #define kbd_is_trigger_mode_bit(bit) \
1143 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1144 #define kbd_is_level_mode_bit(bit) \
1145 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1163 u8 timeout_value_ac;
1170 static const int kbd_tokens[] = {
1172 KBD_LED_AUTO_25_TOKEN,
1173 KBD_LED_AUTO_50_TOKEN,
1174 KBD_LED_AUTO_75_TOKEN,
1175 KBD_LED_AUTO_100_TOKEN,
1179 static u16 kbd_token_bits;
1181 static struct kbd_info kbd_info;
1182 static bool kbd_als_supported;
1183 static bool kbd_triggers_supported;
1184 static bool kbd_timeout_ac_supported;
1186 static u8 kbd_mode_levels[16];
1187 static int kbd_mode_levels_count;
1189 static u8 kbd_previous_level;
1190 static u8 kbd_previous_mode_bit;
1192 static bool kbd_led_present;
1193 static DEFINE_MUTEX(kbd_led_mutex);
1194 static enum led_brightness kbd_led_level;
1197 * NOTE: there are three ways to set the keyboard backlight level.
1198 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1199 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1200 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1202 * There are laptops which support only one of these methods. If we want to
1203 * support as many machines as possible we need to implement all three methods.
1204 * The first two methods use the kbd_state structure. The third uses SMBIOS
1205 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1206 * keyboard backlight level via kbd_state.level.
1209 static int kbd_get_info(struct kbd_info *info)
1211 struct calling_interface_buffer buffer;
1215 dell_fill_request(&buffer, 0, 0, 0, 0);
1216 ret = dell_send_request(&buffer,
1217 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1221 info->modes = buffer.output[1] & 0xFFFF;
1222 info->type = (buffer.output[1] >> 24) & 0xFF;
1223 info->triggers = buffer.output[2] & 0xFF;
1224 units = (buffer.output[2] >> 8) & 0xFF;
1225 info->levels = (buffer.output[2] >> 16) & 0xFF;
1227 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1231 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1233 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1235 info->hours = (buffer.output[3] >> 16) & 0xFF;
1237 info->days = (buffer.output[3] >> 24) & 0xFF;
1242 static unsigned int kbd_get_max_level(void)
1244 if (kbd_info.levels != 0)
1245 return kbd_info.levels;
1246 if (kbd_mode_levels_count > 0)
1247 return kbd_mode_levels_count - 1;
1251 static int kbd_get_level(struct kbd_state *state)
1255 if (kbd_info.levels != 0)
1256 return state->level;
1258 if (kbd_mode_levels_count > 0) {
1259 for (i = 0; i < kbd_mode_levels_count; ++i)
1260 if (kbd_mode_levels[i] == state->mode_bit)
1268 static int kbd_set_level(struct kbd_state *state, u8 level)
1270 if (kbd_info.levels != 0) {
1272 kbd_previous_level = level;
1273 if (state->level == level)
1275 state->level = level;
1276 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1277 state->mode_bit = kbd_previous_mode_bit;
1278 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1279 kbd_previous_mode_bit = state->mode_bit;
1280 state->mode_bit = KBD_MODE_BIT_OFF;
1285 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1287 kbd_previous_level = level;
1288 state->mode_bit = kbd_mode_levels[level];
1295 static int kbd_get_state(struct kbd_state *state)
1297 struct calling_interface_buffer buffer;
1300 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1301 ret = dell_send_request(&buffer,
1302 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1306 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1307 if (state->mode_bit != 0)
1310 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1311 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1312 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1313 state->als_setting = buffer.output[2] & 0xFF;
1314 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1315 state->level = (buffer.output[2] >> 16) & 0xFF;
1316 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1317 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1322 static int kbd_set_state(struct kbd_state *state)
1324 struct calling_interface_buffer buffer;
1329 input1 = BIT(state->mode_bit) & 0xFFFF;
1330 input1 |= (state->triggers & 0xFF) << 16;
1331 input1 |= (state->timeout_value & 0x3F) << 24;
1332 input1 |= (state->timeout_unit & 0x3) << 30;
1333 input2 = state->als_setting & 0xFF;
1334 input2 |= (state->level & 0xFF) << 16;
1335 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1336 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1337 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1338 ret = dell_send_request(&buffer,
1339 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1344 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1348 ret = kbd_set_state(state);
1353 * When setting the new state fails,try to restore the previous one.
1354 * This is needed on some machines where BIOS sets a default state when
1355 * setting a new state fails. This default state could be all off.
1358 if (kbd_set_state(old))
1359 pr_err("Setting old previous keyboard state failed\n");
1364 static int kbd_set_token_bit(u8 bit)
1366 struct calling_interface_buffer buffer;
1367 struct calling_interface_token *token;
1370 if (bit >= ARRAY_SIZE(kbd_tokens))
1373 token = dell_smbios_find_token(kbd_tokens[bit]);
1377 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1378 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1383 static int kbd_get_token_bit(u8 bit)
1385 struct calling_interface_buffer buffer;
1386 struct calling_interface_token *token;
1390 if (bit >= ARRAY_SIZE(kbd_tokens))
1393 token = dell_smbios_find_token(kbd_tokens[bit]);
1397 dell_fill_request(&buffer, token->location, 0, 0, 0);
1398 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1399 val = buffer.output[1];
1404 return (val == token->value);
1407 static int kbd_get_first_active_token_bit(void)
1412 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1413 ret = kbd_get_token_bit(i);
1421 static int kbd_get_valid_token_counts(void)
1423 return hweight16(kbd_token_bits);
1426 static inline int kbd_init_info(void)
1428 struct kbd_state state;
1432 ret = kbd_get_info(&kbd_info);
1436 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1437 * timeout value which is shared for both battery and AC power
1438 * settings. So do not try to set AC values on old models.
1440 if ((quirks && quirks->kbd_missing_ac_tag) ||
1441 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1442 kbd_timeout_ac_supported = true;
1444 kbd_get_state(&state);
1446 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1447 if (kbd_info.seconds > 63)
1448 kbd_info.seconds = 63;
1449 if (kbd_info.minutes > 63)
1450 kbd_info.minutes = 63;
1451 if (kbd_info.hours > 63)
1452 kbd_info.hours = 63;
1453 if (kbd_info.days > 63)
1456 /* NOTE: On tested machines ON mode did not work and caused
1457 * problems (turned backlight off) so do not use it
1459 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1461 kbd_previous_level = kbd_get_level(&state);
1462 kbd_previous_mode_bit = state.mode_bit;
1464 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1465 kbd_previous_level = 1;
1467 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1468 kbd_previous_mode_bit =
1469 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1470 if (kbd_previous_mode_bit != 0)
1471 kbd_previous_mode_bit--;
1474 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1475 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1476 kbd_als_supported = true;
1478 if (kbd_info.modes & (
1479 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1480 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1481 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1483 kbd_triggers_supported = true;
1485 /* kbd_mode_levels[0] is reserved, see below */
1486 for (i = 0; i < 16; ++i)
1487 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1488 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1491 * Find the first supported mode and assign to kbd_mode_levels[0].
1492 * This should be 0 (off), but we cannot depend on the BIOS to
1495 if (kbd_mode_levels_count > 0) {
1496 for (i = 0; i < 16; ++i) {
1497 if (BIT(i) & kbd_info.modes) {
1498 kbd_mode_levels[0] = i;
1502 kbd_mode_levels_count++;
1509 static inline void kbd_init_tokens(void)
1513 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1514 if (dell_smbios_find_token(kbd_tokens[i]))
1515 kbd_token_bits |= BIT(i);
1518 static void kbd_init(void)
1522 if (quirks && quirks->kbd_led_not_present)
1525 ret = kbd_init_info();
1529 * Only supports keyboard backlight when it has at least two modes.
1531 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1532 || kbd_get_valid_token_counts() >= 2)
1533 kbd_led_present = true;
1536 static ssize_t kbd_led_timeout_store(struct device *dev,
1537 struct device_attribute *attr,
1538 const char *buf, size_t count)
1540 struct kbd_state new_state;
1541 struct kbd_state state;
1549 ret = sscanf(buf, "%d %c", &value, &ch);
1562 if (value > kbd_info.seconds)
1564 unit = KBD_TIMEOUT_SECONDS;
1567 if (value > kbd_info.minutes)
1569 unit = KBD_TIMEOUT_MINUTES;
1572 if (value > kbd_info.hours)
1574 unit = KBD_TIMEOUT_HOURS;
1577 if (value > kbd_info.days)
1579 unit = KBD_TIMEOUT_DAYS;
1585 if (quirks && quirks->needs_kbd_timeouts)
1589 /* Convert value from current units to seconds */
1591 case KBD_TIMEOUT_DAYS:
1594 case KBD_TIMEOUT_HOURS:
1597 case KBD_TIMEOUT_MINUTES:
1599 unit = KBD_TIMEOUT_SECONDS;
1602 if (quirks && quirks->needs_kbd_timeouts) {
1603 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1604 if (value <= quirks->kbd_timeouts[i]) {
1605 value = quirks->kbd_timeouts[i];
1611 if (value <= kbd_info.seconds && kbd_info.seconds) {
1612 unit = KBD_TIMEOUT_SECONDS;
1613 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1615 unit = KBD_TIMEOUT_MINUTES;
1616 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1618 unit = KBD_TIMEOUT_HOURS;
1619 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1620 value /= (60 * 60 * 24);
1621 unit = KBD_TIMEOUT_DAYS;
1627 mutex_lock(&kbd_led_mutex);
1629 ret = kbd_get_state(&state);
1635 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1636 new_state.timeout_value_ac = value;
1637 new_state.timeout_unit_ac = unit;
1639 new_state.timeout_value = value;
1640 new_state.timeout_unit = unit;
1643 ret = kbd_set_state_safe(&new_state, &state);
1649 mutex_unlock(&kbd_led_mutex);
1653 static ssize_t kbd_led_timeout_show(struct device *dev,
1654 struct device_attribute *attr, char *buf)
1656 struct kbd_state state;
1662 ret = kbd_get_state(&state);
1666 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1667 value = state.timeout_value_ac;
1668 unit = state.timeout_unit_ac;
1670 value = state.timeout_value;
1671 unit = state.timeout_unit;
1674 len = sprintf(buf, "%d", value);
1677 case KBD_TIMEOUT_SECONDS:
1678 return len + sprintf(buf+len, "s\n");
1679 case KBD_TIMEOUT_MINUTES:
1680 return len + sprintf(buf+len, "m\n");
1681 case KBD_TIMEOUT_HOURS:
1682 return len + sprintf(buf+len, "h\n");
1683 case KBD_TIMEOUT_DAYS:
1684 return len + sprintf(buf+len, "d\n");
1692 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1693 kbd_led_timeout_show, kbd_led_timeout_store);
1695 static const char * const kbd_led_triggers[] = {
1698 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1702 static ssize_t kbd_led_triggers_store(struct device *dev,
1703 struct device_attribute *attr,
1704 const char *buf, size_t count)
1706 struct kbd_state new_state;
1707 struct kbd_state state;
1708 bool triggers_enabled = false;
1709 int trigger_bit = -1;
1713 ret = sscanf(buf, "%20s", trigger);
1717 if (trigger[0] != '+' && trigger[0] != '-')
1720 mutex_lock(&kbd_led_mutex);
1722 ret = kbd_get_state(&state);
1726 if (kbd_triggers_supported)
1727 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1729 if (kbd_triggers_supported) {
1730 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1731 if (!(kbd_info.triggers & BIT(i)))
1733 if (!kbd_led_triggers[i])
1735 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1737 if (trigger[0] == '+' &&
1738 triggers_enabled && (state.triggers & BIT(i))) {
1742 if (trigger[0] == '-' &&
1743 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1752 if (trigger_bit == -1) {
1758 if (trigger[0] == '+')
1759 new_state.triggers |= BIT(trigger_bit);
1761 new_state.triggers &= ~BIT(trigger_bit);
1763 * NOTE: trackstick bit (2) must be disabled when
1764 * disabling touchpad bit (1), otherwise touchpad
1765 * bit (1) will not be disabled
1767 if (trigger_bit == 1)
1768 new_state.triggers &= ~BIT(2);
1770 if ((kbd_info.triggers & new_state.triggers) !=
1771 new_state.triggers) {
1775 if (new_state.triggers && !triggers_enabled) {
1776 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1777 kbd_set_level(&new_state, kbd_previous_level);
1778 } else if (new_state.triggers == 0) {
1779 kbd_set_level(&new_state, 0);
1781 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1785 ret = kbd_set_state_safe(&new_state, &state);
1788 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1789 kbd_previous_mode_bit = new_state.mode_bit;
1792 mutex_unlock(&kbd_led_mutex);
1796 static ssize_t kbd_led_triggers_show(struct device *dev,
1797 struct device_attribute *attr, char *buf)
1799 struct kbd_state state;
1800 bool triggers_enabled;
1804 ret = kbd_get_state(&state);
1810 if (kbd_triggers_supported) {
1811 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1812 level = kbd_get_level(&state);
1813 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1814 if (!(kbd_info.triggers & BIT(i)))
1816 if (!kbd_led_triggers[i])
1818 if ((triggers_enabled || level <= 0) &&
1819 (state.triggers & BIT(i)))
1823 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1828 buf[len - 1] = '\n';
1833 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1834 kbd_led_triggers_show, kbd_led_triggers_store);
1836 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1837 struct device_attribute *attr,
1838 const char *buf, size_t count)
1840 struct kbd_state new_state;
1841 struct kbd_state state;
1842 bool triggers_enabled = false;
1846 ret = kstrtoint(buf, 0, &enable);
1850 mutex_lock(&kbd_led_mutex);
1852 ret = kbd_get_state(&state);
1856 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1863 if (kbd_triggers_supported)
1864 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1867 if (triggers_enabled)
1868 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1870 new_state.mode_bit = KBD_MODE_BIT_ALS;
1872 if (triggers_enabled) {
1873 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1874 kbd_set_level(&new_state, kbd_previous_level);
1876 new_state.mode_bit = KBD_MODE_BIT_ON;
1879 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1884 ret = kbd_set_state_safe(&new_state, &state);
1887 kbd_previous_mode_bit = new_state.mode_bit;
1891 mutex_unlock(&kbd_led_mutex);
1895 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1896 struct device_attribute *attr,
1899 struct kbd_state state;
1900 bool enabled = false;
1903 ret = kbd_get_state(&state);
1906 enabled = kbd_is_als_mode_bit(state.mode_bit);
1908 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1911 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1912 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1914 static ssize_t kbd_led_als_setting_store(struct device *dev,
1915 struct device_attribute *attr,
1916 const char *buf, size_t count)
1918 struct kbd_state state;
1919 struct kbd_state new_state;
1923 ret = kstrtou8(buf, 10, &setting);
1927 mutex_lock(&kbd_led_mutex);
1929 ret = kbd_get_state(&state);
1934 new_state.als_setting = setting;
1936 ret = kbd_set_state_safe(&new_state, &state);
1942 mutex_unlock(&kbd_led_mutex);
1946 static ssize_t kbd_led_als_setting_show(struct device *dev,
1947 struct device_attribute *attr,
1950 struct kbd_state state;
1953 ret = kbd_get_state(&state);
1957 return sprintf(buf, "%d\n", state.als_setting);
1960 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1961 kbd_led_als_setting_show, kbd_led_als_setting_store);
1963 static struct attribute *kbd_led_attrs[] = {
1964 &dev_attr_stop_timeout.attr,
1965 &dev_attr_start_triggers.attr,
1969 static const struct attribute_group kbd_led_group = {
1970 .attrs = kbd_led_attrs,
1973 static struct attribute *kbd_led_als_attrs[] = {
1974 &dev_attr_als_enabled.attr,
1975 &dev_attr_als_setting.attr,
1979 static const struct attribute_group kbd_led_als_group = {
1980 .attrs = kbd_led_als_attrs,
1983 static const struct attribute_group *kbd_led_groups[] = {
1989 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1993 struct kbd_state state;
1995 if (kbd_get_max_level()) {
1996 ret = kbd_get_state(&state);
1999 ret = kbd_get_level(&state);
2005 if (kbd_get_valid_token_counts()) {
2006 ret = kbd_get_first_active_token_bit();
2009 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2010 num &= num - 1; /* clear the first bit set */
2013 return ffs(num) - 1;
2016 pr_warn("Keyboard brightness level control not supported\n");
2020 static int kbd_led_level_set(struct led_classdev *led_cdev,
2021 enum led_brightness value)
2023 enum led_brightness new_value = value;
2024 struct kbd_state state;
2025 struct kbd_state new_state;
2029 mutex_lock(&kbd_led_mutex);
2031 if (kbd_get_max_level()) {
2032 ret = kbd_get_state(&state);
2036 ret = kbd_set_level(&new_state, value);
2039 ret = kbd_set_state_safe(&new_state, &state);
2040 } else if (kbd_get_valid_token_counts()) {
2041 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2042 num &= num - 1; /* clear the first bit set */
2046 ret = kbd_set_token_bit(ffs(num) - 1);
2048 pr_warn("Keyboard brightness level control not supported\n");
2054 kbd_led_level = new_value;
2056 mutex_unlock(&kbd_led_mutex);
2060 static struct led_classdev kbd_led = {
2061 .name = "dell::kbd_backlight",
2062 .flags = LED_BRIGHT_HW_CHANGED,
2063 .brightness_set_blocking = kbd_led_level_set,
2064 .brightness_get = kbd_led_level_get,
2065 .groups = kbd_led_groups,
2068 static int __init kbd_led_init(struct device *dev)
2073 if (!kbd_led_present)
2075 if (!kbd_als_supported)
2076 kbd_led_groups[1] = NULL;
2077 kbd_led.max_brightness = kbd_get_max_level();
2078 if (!kbd_led.max_brightness) {
2079 kbd_led.max_brightness = kbd_get_valid_token_counts();
2080 if (kbd_led.max_brightness)
2081 kbd_led.max_brightness--;
2084 kbd_led_level = kbd_led_level_get(NULL);
2086 ret = led_classdev_register(dev, &kbd_led);
2088 kbd_led_present = false;
2093 static void brightness_set_exit(struct led_classdev *led_cdev,
2094 enum led_brightness value)
2096 /* Don't change backlight level on exit */
2099 static void kbd_led_exit(void)
2101 if (!kbd_led_present)
2103 kbd_led.brightness_set = brightness_set_exit;
2104 led_classdev_unregister(&kbd_led);
2107 static int dell_laptop_notifier_call(struct notifier_block *nb,
2108 unsigned long action, void *data)
2110 bool changed = false;
2111 enum led_brightness new_kbd_led_level;
2114 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2115 if (!kbd_led_present)
2118 mutex_lock(&kbd_led_mutex);
2119 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2120 if (kbd_led_level != new_kbd_led_level) {
2121 kbd_led_level = new_kbd_led_level;
2124 mutex_unlock(&kbd_led_mutex);
2127 led_classdev_notify_brightness_hw_changed(&kbd_led,
2135 static struct notifier_block dell_laptop_notifier = {
2136 .notifier_call = dell_laptop_notifier_call,
2139 static int micmute_led_set(struct led_classdev *led_cdev,
2140 enum led_brightness brightness)
2142 struct calling_interface_buffer buffer;
2143 struct calling_interface_token *token;
2144 int state = brightness != LED_OFF;
2147 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2149 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2154 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2155 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2160 static struct led_classdev micmute_led_cdev = {
2161 .name = "platform::micmute",
2162 .max_brightness = 1,
2163 .brightness_set_blocking = micmute_led_set,
2164 .default_trigger = "audio-micmute",
2167 static int __init dell_init(void)
2169 struct calling_interface_token *token;
2170 int max_intensity = 0;
2173 if (!dmi_check_system(dell_device_table))
2177 /* find if this machine support other functions */
2178 dmi_check_system(dell_quirks);
2180 ret = platform_driver_register(&platform_driver);
2182 goto fail_platform_driver;
2183 platform_device = platform_device_alloc("dell-laptop", -1);
2184 if (!platform_device) {
2186 goto fail_platform_device1;
2188 ret = platform_device_add(platform_device);
2190 goto fail_platform_device2;
2192 ret = dell_setup_rfkill();
2195 pr_warn("Unable to setup rfkill\n");
2199 if (quirks && quirks->touchpad_led)
2200 touchpad_led_init(&platform_device->dev);
2202 kbd_led_init(&platform_device->dev);
2204 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2205 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2206 &dell_debugfs_fops);
2208 dell_laptop_register_notifier(&dell_laptop_notifier);
2210 if (dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE) &&
2211 dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE) &&
2212 !dell_privacy_has_mic_mute()) {
2213 micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2214 ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2217 micmute_led_registered = true;
2220 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2223 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2225 struct calling_interface_buffer buffer;
2227 dell_fill_request(&buffer, token->location, 0, 0, 0);
2228 ret = dell_send_request(&buffer,
2229 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2231 max_intensity = buffer.output[3];
2234 if (max_intensity) {
2235 struct backlight_properties props;
2236 memset(&props, 0, sizeof(struct backlight_properties));
2237 props.type = BACKLIGHT_PLATFORM;
2238 props.max_brightness = max_intensity;
2239 dell_backlight_device = backlight_device_register("dell_backlight",
2240 &platform_device->dev,
2245 if (IS_ERR(dell_backlight_device)) {
2246 ret = PTR_ERR(dell_backlight_device);
2247 dell_backlight_device = NULL;
2248 goto fail_backlight;
2251 dell_backlight_device->props.brightness =
2252 dell_get_intensity(dell_backlight_device);
2253 if (dell_backlight_device->props.brightness < 0) {
2254 ret = dell_backlight_device->props.brightness;
2255 goto fail_get_brightness;
2257 backlight_update_status(dell_backlight_device);
2262 fail_get_brightness:
2263 backlight_device_unregister(dell_backlight_device);
2265 if (micmute_led_registered)
2266 led_classdev_unregister(&micmute_led_cdev);
2268 dell_cleanup_rfkill();
2270 platform_device_del(platform_device);
2271 fail_platform_device2:
2272 platform_device_put(platform_device);
2273 fail_platform_device1:
2274 platform_driver_unregister(&platform_driver);
2275 fail_platform_driver:
2279 static void __exit dell_exit(void)
2281 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2282 debugfs_remove_recursive(dell_laptop_dir);
2283 if (quirks && quirks->touchpad_led)
2284 touchpad_led_exit();
2286 backlight_device_unregister(dell_backlight_device);
2287 if (micmute_led_registered)
2288 led_classdev_unregister(&micmute_led_cdev);
2289 dell_cleanup_rfkill();
2290 if (platform_device) {
2291 platform_device_unregister(platform_device);
2292 platform_driver_unregister(&platform_driver);
2296 /* dell-rbtn.c driver export functions which will not work correctly (and could
2297 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2298 * not problem when dell-rbtn.c is compiled as external module. When both files
2299 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2300 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2301 * This can be achieved by late_initcall() instead module_init().
2303 late_initcall(dell_init);
2304 module_exit(dell_exit);
2306 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2307 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2308 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
2309 MODULE_DESCRIPTION("Dell laptop driver");
2310 MODULE_LICENSE("GPL");