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.rohar@gmail.com>
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_levels_off_1;
37 bool kbd_missing_ac_tag;
39 bool needs_kbd_timeouts;
41 * Ordered list of timeouts expressed in seconds.
42 * The list must end with -1
47 static struct quirk_entry *quirks;
49 static struct quirk_entry quirk_dell_vostro_v130 = {
53 static int __init dmi_matched(const struct dmi_system_id *dmi)
55 quirks = dmi->driver_data;
60 * These values come from Windows utility provided by Dell. If any other value
61 * is used then BIOS silently set timeout to 0 without any error message.
63 static struct quirk_entry quirk_dell_xps13_9333 = {
64 .needs_kbd_timeouts = true,
65 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
68 static struct quirk_entry quirk_dell_xps13_9370 = {
69 .kbd_missing_ac_tag = true,
72 static struct quirk_entry quirk_dell_latitude_e6410 = {
73 .kbd_led_levels_off_1 = true,
76 static struct platform_driver platform_driver = {
78 .name = "dell-laptop",
82 static struct platform_device *platform_device;
83 static struct backlight_device *dell_backlight_device;
84 static struct rfkill *wifi_rfkill;
85 static struct rfkill *bluetooth_rfkill;
86 static struct rfkill *wwan_rfkill;
87 static bool force_rfkill;
89 module_param(force_rfkill, bool, 0444);
90 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
92 static const struct dmi_system_id dell_device_table[] __initconst = {
94 .ident = "Dell laptop",
96 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
97 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
103 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
108 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
109 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
114 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
115 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
120 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
121 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
126 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
127 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
131 .ident = "Dell Computer Corporation",
133 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
134 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
139 MODULE_DEVICE_TABLE(dmi, dell_device_table);
141 static const struct dmi_system_id dell_quirks[] __initconst = {
143 .callback = dmi_matched,
144 .ident = "Dell Vostro V130",
146 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
147 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
149 .driver_data = &quirk_dell_vostro_v130,
152 .callback = dmi_matched,
153 .ident = "Dell Vostro V131",
155 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
156 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
158 .driver_data = &quirk_dell_vostro_v130,
161 .callback = dmi_matched,
162 .ident = "Dell Vostro 3350",
164 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
165 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
167 .driver_data = &quirk_dell_vostro_v130,
170 .callback = dmi_matched,
171 .ident = "Dell Vostro 3555",
173 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
174 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
176 .driver_data = &quirk_dell_vostro_v130,
179 .callback = dmi_matched,
180 .ident = "Dell Inspiron N311z",
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
183 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
185 .driver_data = &quirk_dell_vostro_v130,
188 .callback = dmi_matched,
189 .ident = "Dell Inspiron M5110",
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
194 .driver_data = &quirk_dell_vostro_v130,
197 .callback = dmi_matched,
198 .ident = "Dell Vostro 3360",
200 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
201 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
203 .driver_data = &quirk_dell_vostro_v130,
206 .callback = dmi_matched,
207 .ident = "Dell Vostro 3460",
209 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
210 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
212 .driver_data = &quirk_dell_vostro_v130,
215 .callback = dmi_matched,
216 .ident = "Dell Vostro 3560",
218 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
219 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
221 .driver_data = &quirk_dell_vostro_v130,
224 .callback = dmi_matched,
225 .ident = "Dell Vostro 3450",
227 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
228 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
230 .driver_data = &quirk_dell_vostro_v130,
233 .callback = dmi_matched,
234 .ident = "Dell Inspiron 5420",
236 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
237 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
239 .driver_data = &quirk_dell_vostro_v130,
242 .callback = dmi_matched,
243 .ident = "Dell Inspiron 5520",
245 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
246 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
248 .driver_data = &quirk_dell_vostro_v130,
251 .callback = dmi_matched,
252 .ident = "Dell Inspiron 5720",
254 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
255 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
257 .driver_data = &quirk_dell_vostro_v130,
260 .callback = dmi_matched,
261 .ident = "Dell Inspiron 7420",
263 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
264 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
266 .driver_data = &quirk_dell_vostro_v130,
269 .callback = dmi_matched,
270 .ident = "Dell Inspiron 7520",
272 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
273 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
275 .driver_data = &quirk_dell_vostro_v130,
278 .callback = dmi_matched,
279 .ident = "Dell Inspiron 7720",
281 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
282 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
284 .driver_data = &quirk_dell_vostro_v130,
287 .callback = dmi_matched,
288 .ident = "Dell XPS13 9333",
290 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
291 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
293 .driver_data = &quirk_dell_xps13_9333,
296 .callback = dmi_matched,
297 .ident = "Dell XPS 13 9370",
299 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
300 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
302 .driver_data = &quirk_dell_xps13_9370,
305 .callback = dmi_matched,
306 .ident = "Dell Latitude E6410",
308 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
309 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
311 .driver_data = &quirk_dell_latitude_e6410,
316 static void dell_fill_request(struct calling_interface_buffer *buffer,
317 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
319 memset(buffer, 0, sizeof(struct calling_interface_buffer));
320 buffer->input[0] = arg0;
321 buffer->input[1] = arg1;
322 buffer->input[2] = arg2;
323 buffer->input[3] = arg3;
326 static int dell_send_request(struct calling_interface_buffer *buffer,
327 u16 class, u16 select)
331 buffer->cmd_class = class;
332 buffer->cmd_select = select;
333 ret = dell_smbios_call(buffer);
336 return dell_smbios_error(buffer->output[0]);
340 * Derived from information in smbios-wireless-ctl:
342 * cbSelect 17, Value 11
344 * Return Wireless Info
345 * cbArg1, byte0 = 0x00
347 * cbRes1 Standard return codes (0, -1, -2)
348 * cbRes2 Info bit flags:
350 * 0 Hardware switch supported (1)
351 * 1 WiFi locator supported (1)
352 * 2 WLAN supported (1)
353 * 3 Bluetooth (BT) supported (1)
354 * 4 WWAN supported (1)
355 * 5 Wireless KBD supported (1)
356 * 6 Uw b supported (1)
357 * 7 WiGig supported (1)
358 * 8 WLAN installed (1)
360 * 10 WWAN installed (1)
361 * 11 Uw b installed (1)
362 * 12 WiGig installed (1)
364 * 16 Hardware (HW) switch is On (1)
365 * 17 WLAN disabled (1)
367 * 19 WWAN disabled (1)
368 * 20 Uw b disabled (1)
369 * 21 WiGig disabled (1)
372 * cbRes3 NVRAM size in bytes
373 * cbRes4, byte 0 NVRAM format version number
376 * Set QuickSet Radio Disable Flag
377 * cbArg1, byte0 = 0x01
386 * cbArg1, byte2 Flag bits:
387 * 0 QuickSet disables radio (1)
390 * cbRes1 Standard return codes (0, -1, -2)
391 * cbRes2 QuickSet (QS) radio disable bit map:
396 * 4 QS disables WIGIG
399 * Wireless Switch Configuration
400 * cbArg1, byte0 = 0x02
406 * 2 Set WiFi locator enable/disable
408 * Switch settings (if byte 1==1):
409 * 0 WLAN sw itch control (1)
410 * 1 BT sw itch control (1)
411 * 2 WWAN sw itch control (1)
412 * 3 UWB sw itch control (1)
413 * 4 WiGig sw itch control (1)
415 * cbArg1, byte2 Enable bits (if byte 1==2):
416 * 0 Enable WiFi locator (1)
418 * cbRes1 Standard return codes (0, -1, -2)
419 * cbRes2 QuickSet radio disable bit map:
420 * 0 WLAN controlled by sw itch (1)
421 * 1 BT controlled by sw itch (1)
422 * 2 WWAN controlled by sw itch (1)
423 * 3 UWB controlled by sw itch (1)
424 * 4 WiGig controlled by sw itch (1)
426 * 7 Wireless sw itch config locked (1)
427 * 8 WiFi locator enabled (1)
429 * 15 WiFi locator setting locked (1)
432 * Read Local Config Data (LCD)
433 * cbArg1, byte0 = 0x10
434 * cbArg1, byte1 NVRAM index low byte
435 * cbArg1, byte2 NVRAM index high byte
436 * cbRes1 Standard return codes (0, -1, -2)
437 * cbRes2 4 bytes read from LCD[index]
438 * cbRes3 4 bytes read from LCD[index+4]
439 * cbRes4 4 bytes read from LCD[index+8]
441 * Write Local Config Data (LCD)
442 * cbArg1, byte0 = 0x11
443 * cbArg1, byte1 NVRAM index low byte
444 * cbArg1, byte2 NVRAM index high byte
445 * cbArg2 4 bytes to w rite at LCD[index]
446 * cbArg3 4 bytes to w rite at LCD[index+4]
447 * cbArg4 4 bytes to w rite at LCD[index+8]
448 * cbRes1 Standard return codes (0, -1, -2)
450 * Populate Local Config Data from NVRAM
451 * cbArg1, byte0 = 0x12
452 * cbRes1 Standard return codes (0, -1, -2)
454 * Commit Local Config Data to NVRAM
455 * cbArg1, byte0 = 0x13
456 * cbRes1 Standard return codes (0, -1, -2)
459 static int dell_rfkill_set(void *data, bool blocked)
461 int disable = blocked ? 1 : 0;
462 unsigned long radio = (unsigned long)data;
463 int hwswitch_bit = (unsigned long)data - 1;
464 struct calling_interface_buffer buffer;
469 dell_fill_request(&buffer, 0, 0, 0, 0);
470 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
473 status = buffer.output[1];
475 dell_fill_request(&buffer, 0x2, 0, 0, 0);
476 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
479 hwswitch = buffer.output[1];
481 /* If the hardware switch controls this radio, and the hardware
482 switch is disabled, always disable the radio */
483 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
484 (status & BIT(0)) && !(status & BIT(16)))
487 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
488 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
492 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
495 if (status & BIT(0)) {
496 /* Has hw-switch, sync sw_state to BIOS */
497 struct calling_interface_buffer buffer;
498 int block = rfkill_blocked(rfkill);
499 dell_fill_request(&buffer,
500 1 | (radio << 8) | (block << 16), 0, 0, 0);
501 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
503 /* No hw-switch, sync BIOS state to sw_state */
504 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
508 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
509 int status, int hwswitch)
511 if (hwswitch & (BIT(radio - 1)))
512 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
515 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
517 int radio = ((unsigned long)data & 0xF);
518 struct calling_interface_buffer buffer;
523 dell_fill_request(&buffer, 0, 0, 0, 0);
524 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
525 status = buffer.output[1];
527 if (ret != 0 || !(status & BIT(0))) {
531 dell_fill_request(&buffer, 0x2, 0, 0, 0);
532 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
533 hwswitch = buffer.output[1];
538 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
541 static const struct rfkill_ops dell_rfkill_ops = {
542 .set_block = dell_rfkill_set,
543 .query = dell_rfkill_query,
546 static struct dentry *dell_laptop_dir;
548 static int dell_debugfs_show(struct seq_file *s, void *data)
550 struct calling_interface_buffer buffer;
556 dell_fill_request(&buffer, 0, 0, 0, 0);
557 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
560 status = buffer.output[1];
562 dell_fill_request(&buffer, 0x2, 0, 0, 0);
563 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
566 hwswitch_state = buffer.output[1];
568 seq_printf(s, "return:\t%d\n", ret);
569 seq_printf(s, "status:\t0x%X\n", status);
570 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
572 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
573 (status & BIT(1)) >> 1);
574 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
575 (status & BIT(2)) >> 2);
576 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
577 (status & BIT(3)) >> 3);
578 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
579 (status & BIT(4)) >> 4);
580 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
581 (status & BIT(5)) >> 5);
582 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
583 (status & BIT(6)) >> 6);
584 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
585 (status & BIT(7)) >> 7);
586 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
587 (status & BIT(8)) >> 8);
588 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
589 (status & BIT(9)) >> 9);
590 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
591 (status & BIT(10)) >> 10);
592 seq_printf(s, "Bit 11: UWB installed: %lu\n",
593 (status & BIT(11)) >> 11);
594 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
595 (status & BIT(12)) >> 12);
597 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
598 (status & BIT(16)) >> 16);
599 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
600 (status & BIT(17)) >> 17);
601 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
602 (status & BIT(18)) >> 18);
603 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
604 (status & BIT(19)) >> 19);
605 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
606 (status & BIT(20)) >> 20);
607 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
608 (status & BIT(21)) >> 21);
610 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
611 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
612 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
613 hwswitch_state & BIT(0));
614 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
615 (hwswitch_state & BIT(1)) >> 1);
616 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
617 (hwswitch_state & BIT(2)) >> 2);
618 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
619 (hwswitch_state & BIT(3)) >> 3);
620 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
621 (hwswitch_state & BIT(4)) >> 4);
622 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
623 (hwswitch_state & BIT(7)) >> 7);
624 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
625 (hwswitch_state & BIT(8)) >> 8);
626 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
627 (hwswitch_state & BIT(15)) >> 15);
631 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
633 static void dell_update_rfkill(struct work_struct *ignored)
635 struct calling_interface_buffer buffer;
640 dell_fill_request(&buffer, 0, 0, 0, 0);
641 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
642 status = buffer.output[1];
647 dell_fill_request(&buffer, 0x2, 0, 0, 0);
648 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
650 if (ret == 0 && (status & BIT(0)))
651 hwswitch = buffer.output[1];
654 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
655 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
657 if (bluetooth_rfkill) {
658 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
660 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
663 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
664 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
667 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
669 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
672 static bool extended;
674 if (str & I8042_STR_AUXDATA)
677 if (unlikely(data == 0xe0)) {
680 } else if (unlikely(extended)) {
683 schedule_delayed_work(&dell_rfkill_work,
684 round_jiffies_relative(HZ / 4));
693 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
694 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
696 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
697 unsigned long action, void *data)
699 schedule_delayed_work(&dell_rfkill_work, 0);
703 static struct notifier_block dell_laptop_rbtn_notifier = {
704 .notifier_call = dell_laptop_rbtn_notifier_call,
707 static int __init dell_setup_rfkill(void)
709 struct calling_interface_buffer buffer;
710 int status, ret, whitelisted;
714 * rfkill support causes trouble on various models, mostly Inspirons.
715 * So we whitelist certain series, and don't support rfkill on others.
718 product = dmi_get_system_info(DMI_PRODUCT_NAME);
719 if (product && (strncmp(product, "Latitude", 8) == 0 ||
720 strncmp(product, "Precision", 9) == 0))
722 if (!force_rfkill && !whitelisted)
725 dell_fill_request(&buffer, 0, 0, 0, 0);
726 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
727 status = buffer.output[1];
729 /* dell wireless info smbios call is not supported */
733 /* rfkill is only tested on laptops with a hwswitch */
734 if (!(status & BIT(0)) && !force_rfkill)
737 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
738 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
740 &dell_rfkill_ops, (void *) 1);
745 ret = rfkill_register(wifi_rfkill);
750 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
751 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
752 &platform_device->dev,
753 RFKILL_TYPE_BLUETOOTH,
754 &dell_rfkill_ops, (void *) 2);
755 if (!bluetooth_rfkill) {
759 ret = rfkill_register(bluetooth_rfkill);
764 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
765 wwan_rfkill = rfkill_alloc("dell-wwan",
766 &platform_device->dev,
768 &dell_rfkill_ops, (void *) 3);
773 ret = rfkill_register(wwan_rfkill);
779 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
780 * which can receive events from HW slider switch.
782 * Dell SMBIOS on whitelisted models supports controlling radio devices
783 * but does not support receiving HW button switch events. We can use
784 * i8042 filter hook function to receive keyboard data and handle
785 * keycode for HW button.
787 * So if it is possible we will use Dell Airplane Mode Switch ACPI
788 * driver for receiving HW events and Dell SMBIOS for setting rfkill
789 * states. If ACPI driver or device is not available we will fallback to
790 * i8042 filter hook function.
792 * To prevent duplicate rfkill devices which control and do same thing,
793 * dell-rbtn driver will automatically remove its own rfkill devices
794 * once function dell_rbtn_notifier_register() is called.
797 dell_rbtn_notifier_register_func =
798 symbol_request(dell_rbtn_notifier_register);
799 if (dell_rbtn_notifier_register_func) {
800 dell_rbtn_notifier_unregister_func =
801 symbol_request(dell_rbtn_notifier_unregister);
802 if (!dell_rbtn_notifier_unregister_func) {
803 symbol_put(dell_rbtn_notifier_register);
804 dell_rbtn_notifier_register_func = NULL;
808 if (dell_rbtn_notifier_register_func) {
809 ret = dell_rbtn_notifier_register_func(
810 &dell_laptop_rbtn_notifier);
811 symbol_put(dell_rbtn_notifier_register);
812 dell_rbtn_notifier_register_func = NULL;
814 symbol_put(dell_rbtn_notifier_unregister);
815 dell_rbtn_notifier_unregister_func = NULL;
818 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
823 pr_info("Using dell-rbtn acpi driver for receiving events\n");
824 } else if (ret != -ENODEV) {
825 pr_warn("Unable to register dell rbtn notifier\n");
828 ret = i8042_install_filter(dell_laptop_i8042_filter);
830 pr_warn("Unable to install key filter\n");
833 pr_info("Using i8042 filter function for receiving events\n");
839 rfkill_unregister(wwan_rfkill);
841 rfkill_destroy(wwan_rfkill);
842 if (bluetooth_rfkill)
843 rfkill_unregister(bluetooth_rfkill);
845 rfkill_destroy(bluetooth_rfkill);
847 rfkill_unregister(wifi_rfkill);
849 rfkill_destroy(wifi_rfkill);
854 static void dell_cleanup_rfkill(void)
856 if (dell_rbtn_notifier_unregister_func) {
857 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
858 symbol_put(dell_rbtn_notifier_unregister);
859 dell_rbtn_notifier_unregister_func = NULL;
861 i8042_remove_filter(dell_laptop_i8042_filter);
863 cancel_delayed_work_sync(&dell_rfkill_work);
865 rfkill_unregister(wifi_rfkill);
866 rfkill_destroy(wifi_rfkill);
868 if (bluetooth_rfkill) {
869 rfkill_unregister(bluetooth_rfkill);
870 rfkill_destroy(bluetooth_rfkill);
873 rfkill_unregister(wwan_rfkill);
874 rfkill_destroy(wwan_rfkill);
878 static int dell_send_intensity(struct backlight_device *bd)
880 struct calling_interface_buffer buffer;
881 struct calling_interface_token *token;
884 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
888 dell_fill_request(&buffer,
889 token->location, bd->props.brightness, 0, 0);
890 if (power_supply_is_system_supplied() > 0)
891 ret = dell_send_request(&buffer,
892 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
894 ret = dell_send_request(&buffer,
895 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
900 static int dell_get_intensity(struct backlight_device *bd)
902 struct calling_interface_buffer buffer;
903 struct calling_interface_token *token;
906 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
910 dell_fill_request(&buffer, token->location, 0, 0, 0);
911 if (power_supply_is_system_supplied() > 0)
912 ret = dell_send_request(&buffer,
913 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
915 ret = dell_send_request(&buffer,
916 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
919 ret = buffer.output[1];
924 static const struct backlight_ops dell_ops = {
925 .get_brightness = dell_get_intensity,
926 .update_status = dell_send_intensity,
929 static void touchpad_led_on(void)
933 i8042_command(&data, command | 1 << 12);
936 static void touchpad_led_off(void)
940 i8042_command(&data, command | 1 << 12);
943 static void touchpad_led_set(struct led_classdev *led_cdev,
944 enum led_brightness value)
952 static struct led_classdev touchpad_led = {
953 .name = "dell-laptop::touchpad",
954 .brightness_set = touchpad_led_set,
955 .flags = LED_CORE_SUSPENDRESUME,
958 static int __init touchpad_led_init(struct device *dev)
960 return led_classdev_register(dev, &touchpad_led);
963 static void touchpad_led_exit(void)
965 led_classdev_unregister(&touchpad_led);
969 * Derived from information in smbios-keyboard-ctl:
973 * Keyboard illumination
974 * cbArg1 determines the function to be performed
976 * cbArg1 0x0 = Get Feature Information
977 * cbRES1 Standard return codes (0, -1, -2)
978 * cbRES2, word0 Bitmap of user-selectable modes
979 * bit 0 Always off (All systems)
980 * bit 1 Always on (Travis ATG, Siberia)
981 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
982 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
983 * bit 4 Auto: Input-activity-based On; input-activity based Off
984 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
985 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
986 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
987 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
988 * bits 9-15 Reserved for future use
989 * cbRES2, byte2 Reserved for future use
990 * cbRES2, byte3 Keyboard illumination type
994 * 3-255 Reserved for future use
995 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
996 * bit 0 Any keystroke
997 * bit 1 Touchpad activity
998 * bit 2 Pointing stick
1000 * bits 4-7 Reserved for future use
1001 * cbRES3, byte1 Supported timeout unit bitmap
1006 * bits 4-7 Reserved for future use
1007 * cbRES3, byte2 Number of keyboard light brightness levels
1008 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1009 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1010 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1011 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1013 * cbArg1 0x1 = Get Current State
1014 * cbRES1 Standard return codes (0, -1, -2)
1015 * cbRES2, word0 Bitmap of current mode state
1016 * bit 0 Always off (All systems)
1017 * bit 1 Always on (Travis ATG, Siberia)
1018 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1019 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1020 * bit 4 Auto: Input-activity-based On; input-activity based Off
1021 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1022 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1023 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1024 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1025 * bits 9-15 Reserved for future use
1026 * Note: Only One bit can be set
1027 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1028 * bit 0 Any keystroke
1029 * bit 1 Touchpad activity
1030 * bit 2 Pointing stick
1032 * bits 4-7 Reserved for future use
1033 * cbRES2, byte3 Current Timeout on battery
1034 * bits 7:6 Timeout units indicator:
1039 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1040 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1041 * are set upon return from the [Get feature information] call.
1042 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1043 * cbRES3, byte1 Current ALS reading
1044 * cbRES3, byte2 Current keyboard light level.
1045 * cbRES3, byte3 Current timeout on AC Power
1046 * bits 7:6 Timeout units indicator:
1051 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1052 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1053 * are set upon return from the upon return from the [Get Feature information] call.
1055 * cbArg1 0x2 = Set New State
1056 * cbRES1 Standard return codes (0, -1, -2)
1057 * cbArg2, word0 Bitmap of current mode state
1058 * bit 0 Always off (All systems)
1059 * bit 1 Always on (Travis ATG, Siberia)
1060 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1061 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1062 * bit 4 Auto: Input-activity-based On; input-activity based Off
1063 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1064 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1065 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1066 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1067 * bits 9-15 Reserved for future use
1068 * Note: Only One bit can be set
1069 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1070 * keyboard to turn off automatically.
1071 * bit 0 Any keystroke
1072 * bit 1 Touchpad activity
1073 * bit 2 Pointing stick
1075 * bits 4-7 Reserved for future use
1076 * cbArg2, byte3 Desired Timeout on battery
1077 * bits 7:6 Timeout units indicator:
1082 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1083 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1084 * cbArg3, byte2 Desired keyboard light level.
1085 * cbArg3, byte3 Desired Timeout on AC power
1086 * bits 7:6 Timeout units indicator:
1091 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1095 enum kbd_timeout_unit {
1096 KBD_TIMEOUT_SECONDS = 0,
1097 KBD_TIMEOUT_MINUTES,
1103 KBD_MODE_BIT_OFF = 0,
1106 KBD_MODE_BIT_TRIGGER_ALS,
1107 KBD_MODE_BIT_TRIGGER,
1108 KBD_MODE_BIT_TRIGGER_25,
1109 KBD_MODE_BIT_TRIGGER_50,
1110 KBD_MODE_BIT_TRIGGER_75,
1111 KBD_MODE_BIT_TRIGGER_100,
1114 #define kbd_is_als_mode_bit(bit) \
1115 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1116 #define kbd_is_trigger_mode_bit(bit) \
1117 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1118 #define kbd_is_level_mode_bit(bit) \
1119 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1137 u8 timeout_value_ac;
1144 static const int kbd_tokens[] = {
1146 KBD_LED_AUTO_25_TOKEN,
1147 KBD_LED_AUTO_50_TOKEN,
1148 KBD_LED_AUTO_75_TOKEN,
1149 KBD_LED_AUTO_100_TOKEN,
1153 static u16 kbd_token_bits;
1155 static struct kbd_info kbd_info;
1156 static bool kbd_als_supported;
1157 static bool kbd_triggers_supported;
1158 static bool kbd_timeout_ac_supported;
1160 static u8 kbd_mode_levels[16];
1161 static int kbd_mode_levels_count;
1163 static u8 kbd_previous_level;
1164 static u8 kbd_previous_mode_bit;
1166 static bool kbd_led_present;
1167 static DEFINE_MUTEX(kbd_led_mutex);
1168 static enum led_brightness kbd_led_level;
1171 * NOTE: there are three ways to set the keyboard backlight level.
1172 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1173 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1174 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1176 * There are laptops which support only one of these methods. If we want to
1177 * support as many machines as possible we need to implement all three methods.
1178 * The first two methods use the kbd_state structure. The third uses SMBIOS
1179 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1180 * keyboard backlight level via kbd_state.level.
1183 static int kbd_get_info(struct kbd_info *info)
1185 struct calling_interface_buffer buffer;
1189 dell_fill_request(&buffer, 0, 0, 0, 0);
1190 ret = dell_send_request(&buffer,
1191 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1195 info->modes = buffer.output[1] & 0xFFFF;
1196 info->type = (buffer.output[1] >> 24) & 0xFF;
1197 info->triggers = buffer.output[2] & 0xFF;
1198 units = (buffer.output[2] >> 8) & 0xFF;
1199 info->levels = (buffer.output[2] >> 16) & 0xFF;
1201 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1205 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1207 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1209 info->hours = (buffer.output[3] >> 16) & 0xFF;
1211 info->days = (buffer.output[3] >> 24) & 0xFF;
1216 static unsigned int kbd_get_max_level(void)
1218 if (kbd_info.levels != 0)
1219 return kbd_info.levels;
1220 if (kbd_mode_levels_count > 0)
1221 return kbd_mode_levels_count - 1;
1225 static int kbd_get_level(struct kbd_state *state)
1229 if (kbd_info.levels != 0)
1230 return state->level;
1232 if (kbd_mode_levels_count > 0) {
1233 for (i = 0; i < kbd_mode_levels_count; ++i)
1234 if (kbd_mode_levels[i] == state->mode_bit)
1242 static int kbd_set_level(struct kbd_state *state, u8 level)
1244 if (kbd_info.levels != 0) {
1246 kbd_previous_level = level;
1247 if (state->level == level)
1249 state->level = level;
1250 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1251 state->mode_bit = kbd_previous_mode_bit;
1252 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1253 kbd_previous_mode_bit = state->mode_bit;
1254 state->mode_bit = KBD_MODE_BIT_OFF;
1259 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1261 kbd_previous_level = level;
1262 state->mode_bit = kbd_mode_levels[level];
1269 static int kbd_get_state(struct kbd_state *state)
1271 struct calling_interface_buffer buffer;
1274 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1275 ret = dell_send_request(&buffer,
1276 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1280 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1281 if (state->mode_bit != 0)
1284 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1285 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1286 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1287 state->als_setting = buffer.output[2] & 0xFF;
1288 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1289 state->level = (buffer.output[2] >> 16) & 0xFF;
1290 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1291 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1296 static int kbd_set_state(struct kbd_state *state)
1298 struct calling_interface_buffer buffer;
1303 input1 = BIT(state->mode_bit) & 0xFFFF;
1304 input1 |= (state->triggers & 0xFF) << 16;
1305 input1 |= (state->timeout_value & 0x3F) << 24;
1306 input1 |= (state->timeout_unit & 0x3) << 30;
1307 input2 = state->als_setting & 0xFF;
1308 input2 |= (state->level & 0xFF) << 16;
1309 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1310 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1311 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1312 ret = dell_send_request(&buffer,
1313 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1318 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1322 ret = kbd_set_state(state);
1327 * When setting the new state fails,try to restore the previous one.
1328 * This is needed on some machines where BIOS sets a default state when
1329 * setting a new state fails. This default state could be all off.
1332 if (kbd_set_state(old))
1333 pr_err("Setting old previous keyboard state failed\n");
1338 static int kbd_set_token_bit(u8 bit)
1340 struct calling_interface_buffer buffer;
1341 struct calling_interface_token *token;
1344 if (bit >= ARRAY_SIZE(kbd_tokens))
1347 token = dell_smbios_find_token(kbd_tokens[bit]);
1351 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1352 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1357 static int kbd_get_token_bit(u8 bit)
1359 struct calling_interface_buffer buffer;
1360 struct calling_interface_token *token;
1364 if (bit >= ARRAY_SIZE(kbd_tokens))
1367 token = dell_smbios_find_token(kbd_tokens[bit]);
1371 dell_fill_request(&buffer, token->location, 0, 0, 0);
1372 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1373 val = buffer.output[1];
1378 return (val == token->value);
1381 static int kbd_get_first_active_token_bit(void)
1386 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1387 ret = kbd_get_token_bit(i);
1395 static int kbd_get_valid_token_counts(void)
1397 return hweight16(kbd_token_bits);
1400 static inline int kbd_init_info(void)
1402 struct kbd_state state;
1406 ret = kbd_get_info(&kbd_info);
1410 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1411 * timeout value which is shared for both battery and AC power
1412 * settings. So do not try to set AC values on old models.
1414 if ((quirks && quirks->kbd_missing_ac_tag) ||
1415 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1416 kbd_timeout_ac_supported = true;
1418 kbd_get_state(&state);
1420 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1421 if (kbd_info.seconds > 63)
1422 kbd_info.seconds = 63;
1423 if (kbd_info.minutes > 63)
1424 kbd_info.minutes = 63;
1425 if (kbd_info.hours > 63)
1426 kbd_info.hours = 63;
1427 if (kbd_info.days > 63)
1430 /* NOTE: On tested machines ON mode did not work and caused
1431 * problems (turned backlight off) so do not use it
1433 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1435 kbd_previous_level = kbd_get_level(&state);
1436 kbd_previous_mode_bit = state.mode_bit;
1438 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1439 kbd_previous_level = 1;
1441 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1442 kbd_previous_mode_bit =
1443 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1444 if (kbd_previous_mode_bit != 0)
1445 kbd_previous_mode_bit--;
1448 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1449 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1450 kbd_als_supported = true;
1452 if (kbd_info.modes & (
1453 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1454 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1455 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1457 kbd_triggers_supported = true;
1459 /* kbd_mode_levels[0] is reserved, see below */
1460 for (i = 0; i < 16; ++i)
1461 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1462 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1465 * Find the first supported mode and assign to kbd_mode_levels[0].
1466 * This should be 0 (off), but we cannot depend on the BIOS to
1469 if (kbd_mode_levels_count > 0) {
1470 for (i = 0; i < 16; ++i) {
1471 if (BIT(i) & kbd_info.modes) {
1472 kbd_mode_levels[0] = i;
1476 kbd_mode_levels_count++;
1483 static inline void kbd_init_tokens(void)
1487 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1488 if (dell_smbios_find_token(kbd_tokens[i]))
1489 kbd_token_bits |= BIT(i);
1492 static void kbd_init(void)
1496 ret = kbd_init_info();
1500 * Only supports keyboard backlight when it has at least two modes.
1502 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1503 || kbd_get_valid_token_counts() >= 2)
1504 kbd_led_present = true;
1507 static ssize_t kbd_led_timeout_store(struct device *dev,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1511 struct kbd_state new_state;
1512 struct kbd_state state;
1520 ret = sscanf(buf, "%d %c", &value, &ch);
1533 if (value > kbd_info.seconds)
1535 unit = KBD_TIMEOUT_SECONDS;
1538 if (value > kbd_info.minutes)
1540 unit = KBD_TIMEOUT_MINUTES;
1543 if (value > kbd_info.hours)
1545 unit = KBD_TIMEOUT_HOURS;
1548 if (value > kbd_info.days)
1550 unit = KBD_TIMEOUT_DAYS;
1556 if (quirks && quirks->needs_kbd_timeouts)
1560 /* Convert value from current units to seconds */
1562 case KBD_TIMEOUT_DAYS:
1565 case KBD_TIMEOUT_HOURS:
1568 case KBD_TIMEOUT_MINUTES:
1570 unit = KBD_TIMEOUT_SECONDS;
1573 if (quirks && quirks->needs_kbd_timeouts) {
1574 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1575 if (value <= quirks->kbd_timeouts[i]) {
1576 value = quirks->kbd_timeouts[i];
1582 if (value <= kbd_info.seconds && kbd_info.seconds) {
1583 unit = KBD_TIMEOUT_SECONDS;
1584 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1586 unit = KBD_TIMEOUT_MINUTES;
1587 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1589 unit = KBD_TIMEOUT_HOURS;
1590 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1591 value /= (60 * 60 * 24);
1592 unit = KBD_TIMEOUT_DAYS;
1598 mutex_lock(&kbd_led_mutex);
1600 ret = kbd_get_state(&state);
1606 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1607 new_state.timeout_value_ac = value;
1608 new_state.timeout_unit_ac = unit;
1610 new_state.timeout_value = value;
1611 new_state.timeout_unit = unit;
1614 ret = kbd_set_state_safe(&new_state, &state);
1620 mutex_unlock(&kbd_led_mutex);
1624 static ssize_t kbd_led_timeout_show(struct device *dev,
1625 struct device_attribute *attr, char *buf)
1627 struct kbd_state state;
1633 ret = kbd_get_state(&state);
1637 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1638 value = state.timeout_value_ac;
1639 unit = state.timeout_unit_ac;
1641 value = state.timeout_value;
1642 unit = state.timeout_unit;
1645 len = sprintf(buf, "%d", value);
1648 case KBD_TIMEOUT_SECONDS:
1649 return len + sprintf(buf+len, "s\n");
1650 case KBD_TIMEOUT_MINUTES:
1651 return len + sprintf(buf+len, "m\n");
1652 case KBD_TIMEOUT_HOURS:
1653 return len + sprintf(buf+len, "h\n");
1654 case KBD_TIMEOUT_DAYS:
1655 return len + sprintf(buf+len, "d\n");
1663 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1664 kbd_led_timeout_show, kbd_led_timeout_store);
1666 static const char * const kbd_led_triggers[] = {
1669 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1673 static ssize_t kbd_led_triggers_store(struct device *dev,
1674 struct device_attribute *attr,
1675 const char *buf, size_t count)
1677 struct kbd_state new_state;
1678 struct kbd_state state;
1679 bool triggers_enabled = false;
1680 int trigger_bit = -1;
1684 ret = sscanf(buf, "%20s", trigger);
1688 if (trigger[0] != '+' && trigger[0] != '-')
1691 mutex_lock(&kbd_led_mutex);
1693 ret = kbd_get_state(&state);
1697 if (kbd_triggers_supported)
1698 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1700 if (kbd_triggers_supported) {
1701 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1702 if (!(kbd_info.triggers & BIT(i)))
1704 if (!kbd_led_triggers[i])
1706 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1708 if (trigger[0] == '+' &&
1709 triggers_enabled && (state.triggers & BIT(i))) {
1713 if (trigger[0] == '-' &&
1714 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1723 if (trigger_bit == -1) {
1729 if (trigger[0] == '+')
1730 new_state.triggers |= BIT(trigger_bit);
1732 new_state.triggers &= ~BIT(trigger_bit);
1734 * NOTE: trackstick bit (2) must be disabled when
1735 * disabling touchpad bit (1), otherwise touchpad
1736 * bit (1) will not be disabled
1738 if (trigger_bit == 1)
1739 new_state.triggers &= ~BIT(2);
1741 if ((kbd_info.triggers & new_state.triggers) !=
1742 new_state.triggers) {
1746 if (new_state.triggers && !triggers_enabled) {
1747 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1748 kbd_set_level(&new_state, kbd_previous_level);
1749 } else if (new_state.triggers == 0) {
1750 kbd_set_level(&new_state, 0);
1752 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1756 ret = kbd_set_state_safe(&new_state, &state);
1759 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1760 kbd_previous_mode_bit = new_state.mode_bit;
1763 mutex_unlock(&kbd_led_mutex);
1767 static ssize_t kbd_led_triggers_show(struct device *dev,
1768 struct device_attribute *attr, char *buf)
1770 struct kbd_state state;
1771 bool triggers_enabled;
1775 ret = kbd_get_state(&state);
1781 if (kbd_triggers_supported) {
1782 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1783 level = kbd_get_level(&state);
1784 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1785 if (!(kbd_info.triggers & BIT(i)))
1787 if (!kbd_led_triggers[i])
1789 if ((triggers_enabled || level <= 0) &&
1790 (state.triggers & BIT(i)))
1794 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1799 buf[len - 1] = '\n';
1804 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1805 kbd_led_triggers_show, kbd_led_triggers_store);
1807 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1808 struct device_attribute *attr,
1809 const char *buf, size_t count)
1811 struct kbd_state new_state;
1812 struct kbd_state state;
1813 bool triggers_enabled = false;
1817 ret = kstrtoint(buf, 0, &enable);
1821 mutex_lock(&kbd_led_mutex);
1823 ret = kbd_get_state(&state);
1827 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1834 if (kbd_triggers_supported)
1835 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1838 if (triggers_enabled)
1839 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1841 new_state.mode_bit = KBD_MODE_BIT_ALS;
1843 if (triggers_enabled) {
1844 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1845 kbd_set_level(&new_state, kbd_previous_level);
1847 new_state.mode_bit = KBD_MODE_BIT_ON;
1850 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1855 ret = kbd_set_state_safe(&new_state, &state);
1858 kbd_previous_mode_bit = new_state.mode_bit;
1862 mutex_unlock(&kbd_led_mutex);
1866 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1867 struct device_attribute *attr,
1870 struct kbd_state state;
1871 bool enabled = false;
1874 ret = kbd_get_state(&state);
1877 enabled = kbd_is_als_mode_bit(state.mode_bit);
1879 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1882 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1883 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1885 static ssize_t kbd_led_als_setting_store(struct device *dev,
1886 struct device_attribute *attr,
1887 const char *buf, size_t count)
1889 struct kbd_state state;
1890 struct kbd_state new_state;
1894 ret = kstrtou8(buf, 10, &setting);
1898 mutex_lock(&kbd_led_mutex);
1900 ret = kbd_get_state(&state);
1905 new_state.als_setting = setting;
1907 ret = kbd_set_state_safe(&new_state, &state);
1913 mutex_unlock(&kbd_led_mutex);
1917 static ssize_t kbd_led_als_setting_show(struct device *dev,
1918 struct device_attribute *attr,
1921 struct kbd_state state;
1924 ret = kbd_get_state(&state);
1928 return sprintf(buf, "%d\n", state.als_setting);
1931 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1932 kbd_led_als_setting_show, kbd_led_als_setting_store);
1934 static struct attribute *kbd_led_attrs[] = {
1935 &dev_attr_stop_timeout.attr,
1936 &dev_attr_start_triggers.attr,
1940 static const struct attribute_group kbd_led_group = {
1941 .attrs = kbd_led_attrs,
1944 static struct attribute *kbd_led_als_attrs[] = {
1945 &dev_attr_als_enabled.attr,
1946 &dev_attr_als_setting.attr,
1950 static const struct attribute_group kbd_led_als_group = {
1951 .attrs = kbd_led_als_attrs,
1954 static const struct attribute_group *kbd_led_groups[] = {
1960 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1964 struct kbd_state state;
1966 if (kbd_get_max_level()) {
1967 ret = kbd_get_state(&state);
1970 ret = kbd_get_level(&state);
1976 if (kbd_get_valid_token_counts()) {
1977 ret = kbd_get_first_active_token_bit();
1980 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1981 num &= num - 1; /* clear the first bit set */
1984 return ffs(num) - 1;
1987 pr_warn("Keyboard brightness level control not supported\n");
1991 static int kbd_led_level_set(struct led_classdev *led_cdev,
1992 enum led_brightness value)
1994 enum led_brightness new_value = value;
1995 struct kbd_state state;
1996 struct kbd_state new_state;
2000 mutex_lock(&kbd_led_mutex);
2002 if (kbd_get_max_level()) {
2003 ret = kbd_get_state(&state);
2007 ret = kbd_set_level(&new_state, value);
2010 ret = kbd_set_state_safe(&new_state, &state);
2011 } else if (kbd_get_valid_token_counts()) {
2012 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2013 num &= num - 1; /* clear the first bit set */
2017 ret = kbd_set_token_bit(ffs(num) - 1);
2019 pr_warn("Keyboard brightness level control not supported\n");
2025 kbd_led_level = new_value;
2027 mutex_unlock(&kbd_led_mutex);
2031 static struct led_classdev kbd_led = {
2032 .name = "dell::kbd_backlight",
2033 .flags = LED_BRIGHT_HW_CHANGED,
2034 .brightness_set_blocking = kbd_led_level_set,
2035 .brightness_get = kbd_led_level_get,
2036 .groups = kbd_led_groups,
2039 static int __init kbd_led_init(struct device *dev)
2044 if (!kbd_led_present)
2046 if (!kbd_als_supported)
2047 kbd_led_groups[1] = NULL;
2048 kbd_led.max_brightness = kbd_get_max_level();
2049 if (!kbd_led.max_brightness) {
2050 kbd_led.max_brightness = kbd_get_valid_token_counts();
2051 if (kbd_led.max_brightness)
2052 kbd_led.max_brightness--;
2055 kbd_led_level = kbd_led_level_get(NULL);
2057 ret = led_classdev_register(dev, &kbd_led);
2059 kbd_led_present = false;
2064 static void brightness_set_exit(struct led_classdev *led_cdev,
2065 enum led_brightness value)
2067 /* Don't change backlight level on exit */
2070 static void kbd_led_exit(void)
2072 if (!kbd_led_present)
2074 kbd_led.brightness_set = brightness_set_exit;
2075 led_classdev_unregister(&kbd_led);
2078 static int dell_laptop_notifier_call(struct notifier_block *nb,
2079 unsigned long action, void *data)
2081 bool changed = false;
2082 enum led_brightness new_kbd_led_level;
2085 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2086 if (!kbd_led_present)
2089 mutex_lock(&kbd_led_mutex);
2090 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2091 if (kbd_led_level != new_kbd_led_level) {
2092 kbd_led_level = new_kbd_led_level;
2095 mutex_unlock(&kbd_led_mutex);
2098 led_classdev_notify_brightness_hw_changed(&kbd_led,
2106 static struct notifier_block dell_laptop_notifier = {
2107 .notifier_call = dell_laptop_notifier_call,
2110 static int micmute_led_set(struct led_classdev *led_cdev,
2111 enum led_brightness brightness)
2113 struct calling_interface_buffer buffer;
2114 struct calling_interface_token *token;
2115 int state = brightness != LED_OFF;
2118 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2120 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2125 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2126 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2131 static struct led_classdev micmute_led_cdev = {
2132 .name = "platform::micmute",
2133 .max_brightness = 1,
2134 .brightness_set_blocking = micmute_led_set,
2135 .default_trigger = "audio-micmute",
2138 static int __init dell_init(void)
2140 struct calling_interface_token *token;
2141 int max_intensity = 0;
2144 if (!dmi_check_system(dell_device_table))
2148 /* find if this machine support other functions */
2149 dmi_check_system(dell_quirks);
2151 ret = platform_driver_register(&platform_driver);
2153 goto fail_platform_driver;
2154 platform_device = platform_device_alloc("dell-laptop", -1);
2155 if (!platform_device) {
2157 goto fail_platform_device1;
2159 ret = platform_device_add(platform_device);
2161 goto fail_platform_device2;
2163 ret = dell_setup_rfkill();
2166 pr_warn("Unable to setup rfkill\n");
2170 if (quirks && quirks->touchpad_led)
2171 touchpad_led_init(&platform_device->dev);
2173 kbd_led_init(&platform_device->dev);
2175 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2176 if (dell_laptop_dir != NULL)
2177 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2178 &dell_debugfs_fops);
2180 dell_laptop_register_notifier(&dell_laptop_notifier);
2182 micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
2183 ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2187 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2190 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2192 struct calling_interface_buffer buffer;
2194 dell_fill_request(&buffer, token->location, 0, 0, 0);
2195 ret = dell_send_request(&buffer,
2196 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2198 max_intensity = buffer.output[3];
2201 if (max_intensity) {
2202 struct backlight_properties props;
2203 memset(&props, 0, sizeof(struct backlight_properties));
2204 props.type = BACKLIGHT_PLATFORM;
2205 props.max_brightness = max_intensity;
2206 dell_backlight_device = backlight_device_register("dell_backlight",
2207 &platform_device->dev,
2212 if (IS_ERR(dell_backlight_device)) {
2213 ret = PTR_ERR(dell_backlight_device);
2214 dell_backlight_device = NULL;
2215 goto fail_backlight;
2218 dell_backlight_device->props.brightness =
2219 dell_get_intensity(dell_backlight_device);
2220 if (dell_backlight_device->props.brightness < 0) {
2221 ret = dell_backlight_device->props.brightness;
2222 goto fail_get_brightness;
2224 backlight_update_status(dell_backlight_device);
2229 fail_get_brightness:
2230 backlight_device_unregister(dell_backlight_device);
2232 led_classdev_unregister(&micmute_led_cdev);
2234 dell_cleanup_rfkill();
2236 platform_device_del(platform_device);
2237 fail_platform_device2:
2238 platform_device_put(platform_device);
2239 fail_platform_device1:
2240 platform_driver_unregister(&platform_driver);
2241 fail_platform_driver:
2245 static void __exit dell_exit(void)
2247 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2248 debugfs_remove_recursive(dell_laptop_dir);
2249 if (quirks && quirks->touchpad_led)
2250 touchpad_led_exit();
2252 backlight_device_unregister(dell_backlight_device);
2253 led_classdev_unregister(&micmute_led_cdev);
2254 dell_cleanup_rfkill();
2255 if (platform_device) {
2256 platform_device_unregister(platform_device);
2257 platform_driver_unregister(&platform_driver);
2261 /* dell-rbtn.c driver export functions which will not work correctly (and could
2262 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2263 * not problem when dell-rbtn.c is compiled as external module. When both files
2264 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2265 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2266 * This can be achieved by late_initcall() instead module_init().
2268 late_initcall(dell_init);
2269 module_exit(dell_exit);
2271 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2272 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2273 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2274 MODULE_DESCRIPTION("Dell laptop driver");
2275 MODULE_LICENSE("GPL");