2 * Driver for Dell laptop extras
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
40 bool kbd_led_levels_off_1;
41 bool kbd_missing_ac_tag;
43 bool needs_kbd_timeouts;
45 * Ordered list of timeouts expressed in seconds.
46 * The list must end with -1
51 static struct quirk_entry *quirks;
53 static struct quirk_entry quirk_dell_vostro_v130 = {
57 static int __init dmi_matched(const struct dmi_system_id *dmi)
59 quirks = dmi->driver_data;
64 * These values come from Windows utility provided by Dell. If any other value
65 * is used then BIOS silently set timeout to 0 without any error message.
67 static struct quirk_entry quirk_dell_xps13_9333 = {
68 .needs_kbd_timeouts = true,
69 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
72 static struct quirk_entry quirk_dell_xps13_9370 = {
73 .kbd_missing_ac_tag = true,
76 static struct quirk_entry quirk_dell_latitude_e6410 = {
77 .kbd_led_levels_off_1 = true,
80 static struct platform_driver platform_driver = {
82 .name = "dell-laptop",
86 static struct platform_device *platform_device;
87 static struct backlight_device *dell_backlight_device;
88 static struct rfkill *wifi_rfkill;
89 static struct rfkill *bluetooth_rfkill;
90 static struct rfkill *wwan_rfkill;
91 static bool force_rfkill;
93 module_param(force_rfkill, bool, 0444);
94 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
96 static const struct dmi_system_id dell_device_table[] __initconst = {
98 .ident = "Dell laptop",
100 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
101 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
106 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
107 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
112 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
113 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
118 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
119 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
124 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
125 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
130 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
131 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
135 .ident = "Dell Computer Corporation",
137 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
138 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
143 MODULE_DEVICE_TABLE(dmi, dell_device_table);
145 static const struct dmi_system_id dell_quirks[] __initconst = {
147 .callback = dmi_matched,
148 .ident = "Dell Vostro V130",
150 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
151 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
153 .driver_data = &quirk_dell_vostro_v130,
156 .callback = dmi_matched,
157 .ident = "Dell Vostro V131",
159 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
160 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
162 .driver_data = &quirk_dell_vostro_v130,
165 .callback = dmi_matched,
166 .ident = "Dell Vostro 3350",
168 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
169 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
171 .driver_data = &quirk_dell_vostro_v130,
174 .callback = dmi_matched,
175 .ident = "Dell Vostro 3555",
177 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
180 .driver_data = &quirk_dell_vostro_v130,
183 .callback = dmi_matched,
184 .ident = "Dell Inspiron N311z",
186 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
189 .driver_data = &quirk_dell_vostro_v130,
192 .callback = dmi_matched,
193 .ident = "Dell Inspiron M5110",
195 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
198 .driver_data = &quirk_dell_vostro_v130,
201 .callback = dmi_matched,
202 .ident = "Dell Vostro 3360",
204 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
205 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
207 .driver_data = &quirk_dell_vostro_v130,
210 .callback = dmi_matched,
211 .ident = "Dell Vostro 3460",
213 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
214 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
216 .driver_data = &quirk_dell_vostro_v130,
219 .callback = dmi_matched,
220 .ident = "Dell Vostro 3560",
222 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
223 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
225 .driver_data = &quirk_dell_vostro_v130,
228 .callback = dmi_matched,
229 .ident = "Dell Vostro 3450",
231 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
234 .driver_data = &quirk_dell_vostro_v130,
237 .callback = dmi_matched,
238 .ident = "Dell Inspiron 5420",
240 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
241 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
243 .driver_data = &quirk_dell_vostro_v130,
246 .callback = dmi_matched,
247 .ident = "Dell Inspiron 5520",
249 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
252 .driver_data = &quirk_dell_vostro_v130,
255 .callback = dmi_matched,
256 .ident = "Dell Inspiron 5720",
258 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
261 .driver_data = &quirk_dell_vostro_v130,
264 .callback = dmi_matched,
265 .ident = "Dell Inspiron 7420",
267 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
270 .driver_data = &quirk_dell_vostro_v130,
273 .callback = dmi_matched,
274 .ident = "Dell Inspiron 7520",
276 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
277 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
279 .driver_data = &quirk_dell_vostro_v130,
282 .callback = dmi_matched,
283 .ident = "Dell Inspiron 7720",
285 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
286 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
288 .driver_data = &quirk_dell_vostro_v130,
291 .callback = dmi_matched,
292 .ident = "Dell XPS13 9333",
294 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
295 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
297 .driver_data = &quirk_dell_xps13_9333,
300 .callback = dmi_matched,
301 .ident = "Dell XPS 13 9370",
303 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
304 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
306 .driver_data = &quirk_dell_xps13_9370,
309 .callback = dmi_matched,
310 .ident = "Dell Latitude E6410",
312 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
313 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
315 .driver_data = &quirk_dell_latitude_e6410,
320 static void dell_fill_request(struct calling_interface_buffer *buffer,
321 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
323 memset(buffer, 0, sizeof(struct calling_interface_buffer));
324 buffer->input[0] = arg0;
325 buffer->input[1] = arg1;
326 buffer->input[2] = arg2;
327 buffer->input[3] = arg3;
330 static int dell_send_request(struct calling_interface_buffer *buffer,
331 u16 class, u16 select)
335 buffer->cmd_class = class;
336 buffer->cmd_select = select;
337 ret = dell_smbios_call(buffer);
340 return dell_smbios_error(buffer->output[0]);
344 * Derived from information in smbios-wireless-ctl:
346 * cbSelect 17, Value 11
348 * Return Wireless Info
349 * cbArg1, byte0 = 0x00
351 * cbRes1 Standard return codes (0, -1, -2)
352 * cbRes2 Info bit flags:
354 * 0 Hardware switch supported (1)
355 * 1 WiFi locator supported (1)
356 * 2 WLAN supported (1)
357 * 3 Bluetooth (BT) supported (1)
358 * 4 WWAN supported (1)
359 * 5 Wireless KBD supported (1)
360 * 6 Uw b supported (1)
361 * 7 WiGig supported (1)
362 * 8 WLAN installed (1)
364 * 10 WWAN installed (1)
365 * 11 Uw b installed (1)
366 * 12 WiGig installed (1)
368 * 16 Hardware (HW) switch is On (1)
369 * 17 WLAN disabled (1)
371 * 19 WWAN disabled (1)
372 * 20 Uw b disabled (1)
373 * 21 WiGig disabled (1)
376 * cbRes3 NVRAM size in bytes
377 * cbRes4, byte 0 NVRAM format version number
380 * Set QuickSet Radio Disable Flag
381 * cbArg1, byte0 = 0x01
390 * cbArg1, byte2 Flag bits:
391 * 0 QuickSet disables radio (1)
394 * cbRes1 Standard return codes (0, -1, -2)
395 * cbRes2 QuickSet (QS) radio disable bit map:
400 * 4 QS disables WIGIG
403 * Wireless Switch Configuration
404 * cbArg1, byte0 = 0x02
410 * 2 Set WiFi locator enable/disable
412 * Switch settings (if byte 1==1):
413 * 0 WLAN sw itch control (1)
414 * 1 BT sw itch control (1)
415 * 2 WWAN sw itch control (1)
416 * 3 UWB sw itch control (1)
417 * 4 WiGig sw itch control (1)
419 * cbArg1, byte2 Enable bits (if byte 1==2):
420 * 0 Enable WiFi locator (1)
422 * cbRes1 Standard return codes (0, -1, -2)
423 * cbRes2 QuickSet radio disable bit map:
424 * 0 WLAN controlled by sw itch (1)
425 * 1 BT controlled by sw itch (1)
426 * 2 WWAN controlled by sw itch (1)
427 * 3 UWB controlled by sw itch (1)
428 * 4 WiGig controlled by sw itch (1)
430 * 7 Wireless sw itch config locked (1)
431 * 8 WiFi locator enabled (1)
433 * 15 WiFi locator setting locked (1)
436 * Read Local Config Data (LCD)
437 * cbArg1, byte0 = 0x10
438 * cbArg1, byte1 NVRAM index low byte
439 * cbArg1, byte2 NVRAM index high byte
440 * cbRes1 Standard return codes (0, -1, -2)
441 * cbRes2 4 bytes read from LCD[index]
442 * cbRes3 4 bytes read from LCD[index+4]
443 * cbRes4 4 bytes read from LCD[index+8]
445 * Write Local Config Data (LCD)
446 * cbArg1, byte0 = 0x11
447 * cbArg1, byte1 NVRAM index low byte
448 * cbArg1, byte2 NVRAM index high byte
449 * cbArg2 4 bytes to w rite at LCD[index]
450 * cbArg3 4 bytes to w rite at LCD[index+4]
451 * cbArg4 4 bytes to w rite at LCD[index+8]
452 * cbRes1 Standard return codes (0, -1, -2)
454 * Populate Local Config Data from NVRAM
455 * cbArg1, byte0 = 0x12
456 * cbRes1 Standard return codes (0, -1, -2)
458 * Commit Local Config Data to NVRAM
459 * cbArg1, byte0 = 0x13
460 * cbRes1 Standard return codes (0, -1, -2)
463 static int dell_rfkill_set(void *data, bool blocked)
465 int disable = blocked ? 1 : 0;
466 unsigned long radio = (unsigned long)data;
467 int hwswitch_bit = (unsigned long)data - 1;
468 struct calling_interface_buffer buffer;
473 dell_fill_request(&buffer, 0, 0, 0, 0);
474 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
477 status = buffer.output[1];
479 dell_fill_request(&buffer, 0x2, 0, 0, 0);
480 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
483 hwswitch = buffer.output[1];
485 /* If the hardware switch controls this radio, and the hardware
486 switch is disabled, always disable the radio */
487 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
488 (status & BIT(0)) && !(status & BIT(16)))
491 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
492 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
496 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
499 if (status & BIT(0)) {
500 /* Has hw-switch, sync sw_state to BIOS */
501 struct calling_interface_buffer buffer;
502 int block = rfkill_blocked(rfkill);
503 dell_fill_request(&buffer,
504 1 | (radio << 8) | (block << 16), 0, 0, 0);
505 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
507 /* No hw-switch, sync BIOS state to sw_state */
508 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
512 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
513 int status, int hwswitch)
515 if (hwswitch & (BIT(radio - 1)))
516 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
519 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
521 int radio = ((unsigned long)data & 0xF);
522 struct calling_interface_buffer buffer;
527 dell_fill_request(&buffer, 0, 0, 0, 0);
528 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
529 status = buffer.output[1];
531 if (ret != 0 || !(status & BIT(0))) {
535 dell_fill_request(&buffer, 0, 0x2, 0, 0);
536 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
537 hwswitch = buffer.output[1];
542 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
545 static const struct rfkill_ops dell_rfkill_ops = {
546 .set_block = dell_rfkill_set,
547 .query = dell_rfkill_query,
550 static struct dentry *dell_laptop_dir;
552 static int dell_debugfs_show(struct seq_file *s, void *data)
554 struct calling_interface_buffer buffer;
560 dell_fill_request(&buffer, 0, 0, 0, 0);
561 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
564 status = buffer.output[1];
566 dell_fill_request(&buffer, 0, 0x2, 0, 0);
567 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
570 hwswitch_state = buffer.output[1];
572 seq_printf(s, "return:\t%d\n", ret);
573 seq_printf(s, "status:\t0x%X\n", status);
574 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
576 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
577 (status & BIT(1)) >> 1);
578 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
579 (status & BIT(2)) >> 2);
580 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
581 (status & BIT(3)) >> 3);
582 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
583 (status & BIT(4)) >> 4);
584 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
585 (status & BIT(5)) >> 5);
586 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
587 (status & BIT(6)) >> 6);
588 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
589 (status & BIT(7)) >> 7);
590 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
591 (status & BIT(8)) >> 8);
592 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
593 (status & BIT(9)) >> 9);
594 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
595 (status & BIT(10)) >> 10);
596 seq_printf(s, "Bit 11: UWB installed: %lu\n",
597 (status & BIT(11)) >> 11);
598 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
599 (status & BIT(12)) >> 12);
601 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
602 (status & BIT(16)) >> 16);
603 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
604 (status & BIT(17)) >> 17);
605 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
606 (status & BIT(18)) >> 18);
607 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
608 (status & BIT(19)) >> 19);
609 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
610 (status & BIT(20)) >> 20);
611 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
612 (status & BIT(21)) >> 21);
614 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
615 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
616 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
617 hwswitch_state & BIT(0));
618 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
619 (hwswitch_state & BIT(1)) >> 1);
620 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
621 (hwswitch_state & BIT(2)) >> 2);
622 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
623 (hwswitch_state & BIT(3)) >> 3);
624 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
625 (hwswitch_state & BIT(4)) >> 4);
626 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
627 (hwswitch_state & BIT(7)) >> 7);
628 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
629 (hwswitch_state & BIT(8)) >> 8);
630 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
631 (hwswitch_state & BIT(15)) >> 15);
635 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
637 static void dell_update_rfkill(struct work_struct *ignored)
639 struct calling_interface_buffer buffer;
644 dell_fill_request(&buffer, 0, 0, 0, 0);
645 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
646 status = buffer.output[1];
651 dell_fill_request(&buffer, 0, 0x2, 0, 0);
652 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
654 if (ret == 0 && (status & BIT(0)))
655 hwswitch = buffer.output[1];
658 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
659 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
661 if (bluetooth_rfkill) {
662 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
664 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
667 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
668 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
671 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
673 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
676 static bool extended;
678 if (str & I8042_STR_AUXDATA)
681 if (unlikely(data == 0xe0)) {
684 } else if (unlikely(extended)) {
687 schedule_delayed_work(&dell_rfkill_work,
688 round_jiffies_relative(HZ / 4));
697 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
698 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
700 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
701 unsigned long action, void *data)
703 schedule_delayed_work(&dell_rfkill_work, 0);
707 static struct notifier_block dell_laptop_rbtn_notifier = {
708 .notifier_call = dell_laptop_rbtn_notifier_call,
711 static int __init dell_setup_rfkill(void)
713 struct calling_interface_buffer buffer;
714 int status, ret, whitelisted;
718 * rfkill support causes trouble on various models, mostly Inspirons.
719 * So we whitelist certain series, and don't support rfkill on others.
722 product = dmi_get_system_info(DMI_PRODUCT_NAME);
723 if (product && (strncmp(product, "Latitude", 8) == 0 ||
724 strncmp(product, "Precision", 9) == 0))
726 if (!force_rfkill && !whitelisted)
729 dell_fill_request(&buffer, 0, 0, 0, 0);
730 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
731 status = buffer.output[1];
733 /* dell wireless info smbios call is not supported */
737 /* rfkill is only tested on laptops with a hwswitch */
738 if (!(status & BIT(0)) && !force_rfkill)
741 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
742 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
744 &dell_rfkill_ops, (void *) 1);
749 ret = rfkill_register(wifi_rfkill);
754 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
755 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
756 &platform_device->dev,
757 RFKILL_TYPE_BLUETOOTH,
758 &dell_rfkill_ops, (void *) 2);
759 if (!bluetooth_rfkill) {
763 ret = rfkill_register(bluetooth_rfkill);
768 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
769 wwan_rfkill = rfkill_alloc("dell-wwan",
770 &platform_device->dev,
772 &dell_rfkill_ops, (void *) 3);
777 ret = rfkill_register(wwan_rfkill);
783 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
784 * which can receive events from HW slider switch.
786 * Dell SMBIOS on whitelisted models supports controlling radio devices
787 * but does not support receiving HW button switch events. We can use
788 * i8042 filter hook function to receive keyboard data and handle
789 * keycode for HW button.
791 * So if it is possible we will use Dell Airplane Mode Switch ACPI
792 * driver for receiving HW events and Dell SMBIOS for setting rfkill
793 * states. If ACPI driver or device is not available we will fallback to
794 * i8042 filter hook function.
796 * To prevent duplicate rfkill devices which control and do same thing,
797 * dell-rbtn driver will automatically remove its own rfkill devices
798 * once function dell_rbtn_notifier_register() is called.
801 dell_rbtn_notifier_register_func =
802 symbol_request(dell_rbtn_notifier_register);
803 if (dell_rbtn_notifier_register_func) {
804 dell_rbtn_notifier_unregister_func =
805 symbol_request(dell_rbtn_notifier_unregister);
806 if (!dell_rbtn_notifier_unregister_func) {
807 symbol_put(dell_rbtn_notifier_register);
808 dell_rbtn_notifier_register_func = NULL;
812 if (dell_rbtn_notifier_register_func) {
813 ret = dell_rbtn_notifier_register_func(
814 &dell_laptop_rbtn_notifier);
815 symbol_put(dell_rbtn_notifier_register);
816 dell_rbtn_notifier_register_func = NULL;
818 symbol_put(dell_rbtn_notifier_unregister);
819 dell_rbtn_notifier_unregister_func = NULL;
822 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
827 pr_info("Using dell-rbtn acpi driver for receiving events\n");
828 } else if (ret != -ENODEV) {
829 pr_warn("Unable to register dell rbtn notifier\n");
832 ret = i8042_install_filter(dell_laptop_i8042_filter);
834 pr_warn("Unable to install key filter\n");
837 pr_info("Using i8042 filter function for receiving events\n");
843 rfkill_unregister(wwan_rfkill);
845 rfkill_destroy(wwan_rfkill);
846 if (bluetooth_rfkill)
847 rfkill_unregister(bluetooth_rfkill);
849 rfkill_destroy(bluetooth_rfkill);
851 rfkill_unregister(wifi_rfkill);
853 rfkill_destroy(wifi_rfkill);
858 static void dell_cleanup_rfkill(void)
860 if (dell_rbtn_notifier_unregister_func) {
861 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
862 symbol_put(dell_rbtn_notifier_unregister);
863 dell_rbtn_notifier_unregister_func = NULL;
865 i8042_remove_filter(dell_laptop_i8042_filter);
867 cancel_delayed_work_sync(&dell_rfkill_work);
869 rfkill_unregister(wifi_rfkill);
870 rfkill_destroy(wifi_rfkill);
872 if (bluetooth_rfkill) {
873 rfkill_unregister(bluetooth_rfkill);
874 rfkill_destroy(bluetooth_rfkill);
877 rfkill_unregister(wwan_rfkill);
878 rfkill_destroy(wwan_rfkill);
882 static int dell_send_intensity(struct backlight_device *bd)
884 struct calling_interface_buffer buffer;
885 struct calling_interface_token *token;
888 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
892 dell_fill_request(&buffer,
893 token->location, bd->props.brightness, 0, 0);
894 if (power_supply_is_system_supplied() > 0)
895 ret = dell_send_request(&buffer,
896 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
898 ret = dell_send_request(&buffer,
899 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
904 static int dell_get_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, token->location, 0, 0, 0);
915 if (power_supply_is_system_supplied() > 0)
916 ret = dell_send_request(&buffer,
917 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
919 ret = dell_send_request(&buffer,
920 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
923 ret = buffer.output[1];
928 static const struct backlight_ops dell_ops = {
929 .get_brightness = dell_get_intensity,
930 .update_status = dell_send_intensity,
933 static void touchpad_led_on(void)
937 i8042_command(&data, command | 1 << 12);
940 static void touchpad_led_off(void)
944 i8042_command(&data, command | 1 << 12);
947 static void touchpad_led_set(struct led_classdev *led_cdev,
948 enum led_brightness value)
956 static struct led_classdev touchpad_led = {
957 .name = "dell-laptop::touchpad",
958 .brightness_set = touchpad_led_set,
959 .flags = LED_CORE_SUSPENDRESUME,
962 static int __init touchpad_led_init(struct device *dev)
964 return led_classdev_register(dev, &touchpad_led);
967 static void touchpad_led_exit(void)
969 led_classdev_unregister(&touchpad_led);
973 * Derived from information in smbios-keyboard-ctl:
977 * Keyboard illumination
978 * cbArg1 determines the function to be performed
980 * cbArg1 0x0 = Get Feature Information
981 * cbRES1 Standard return codes (0, -1, -2)
982 * cbRES2, word0 Bitmap of user-selectable modes
983 * bit 0 Always off (All systems)
984 * bit 1 Always on (Travis ATG, Siberia)
985 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
986 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
987 * bit 4 Auto: Input-activity-based On; input-activity based Off
988 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
989 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
990 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
991 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
992 * bits 9-15 Reserved for future use
993 * cbRES2, byte2 Reserved for future use
994 * cbRES2, byte3 Keyboard illumination type
998 * 3-255 Reserved for future use
999 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1000 * bit 0 Any keystroke
1001 * bit 1 Touchpad activity
1002 * bit 2 Pointing stick
1004 * bits 4-7 Reserved for future use
1005 * cbRES3, byte1 Supported timeout unit bitmap
1010 * bits 4-7 Reserved for future use
1011 * cbRES3, byte2 Number of keyboard light brightness levels
1012 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1013 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1014 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1015 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1017 * cbArg1 0x1 = Get Current State
1018 * cbRES1 Standard return codes (0, -1, -2)
1019 * cbRES2, word0 Bitmap of current mode state
1020 * bit 0 Always off (All systems)
1021 * bit 1 Always on (Travis ATG, Siberia)
1022 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1023 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1024 * bit 4 Auto: Input-activity-based On; input-activity based Off
1025 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1026 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1027 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1028 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1029 * bits 9-15 Reserved for future use
1030 * Note: Only One bit can be set
1031 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1032 * bit 0 Any keystroke
1033 * bit 1 Touchpad activity
1034 * bit 2 Pointing stick
1036 * bits 4-7 Reserved for future use
1037 * cbRES2, byte3 Current Timeout on battery
1038 * bits 7:6 Timeout units indicator:
1043 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1044 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1045 * are set upon return from the [Get feature information] call.
1046 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1047 * cbRES3, byte1 Current ALS reading
1048 * cbRES3, byte2 Current keyboard light level.
1049 * cbRES3, byte3 Current timeout on AC Power
1050 * bits 7:6 Timeout units indicator:
1055 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1056 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1057 * are set upon return from the upon return from the [Get Feature information] call.
1059 * cbArg1 0x2 = Set New State
1060 * cbRES1 Standard return codes (0, -1, -2)
1061 * cbArg2, word0 Bitmap of current mode state
1062 * bit 0 Always off (All systems)
1063 * bit 1 Always on (Travis ATG, Siberia)
1064 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1065 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1066 * bit 4 Auto: Input-activity-based On; input-activity based Off
1067 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1068 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1069 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1070 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1071 * bits 9-15 Reserved for future use
1072 * Note: Only One bit can be set
1073 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1074 * keyboard to turn off automatically.
1075 * bit 0 Any keystroke
1076 * bit 1 Touchpad activity
1077 * bit 2 Pointing stick
1079 * bits 4-7 Reserved for future use
1080 * cbArg2, byte3 Desired Timeout on battery
1081 * bits 7:6 Timeout units indicator:
1086 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1087 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1088 * cbArg3, byte2 Desired keyboard light level.
1089 * cbArg3, byte3 Desired Timeout on AC power
1090 * bits 7:6 Timeout units indicator:
1095 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1099 enum kbd_timeout_unit {
1100 KBD_TIMEOUT_SECONDS = 0,
1101 KBD_TIMEOUT_MINUTES,
1107 KBD_MODE_BIT_OFF = 0,
1110 KBD_MODE_BIT_TRIGGER_ALS,
1111 KBD_MODE_BIT_TRIGGER,
1112 KBD_MODE_BIT_TRIGGER_25,
1113 KBD_MODE_BIT_TRIGGER_50,
1114 KBD_MODE_BIT_TRIGGER_75,
1115 KBD_MODE_BIT_TRIGGER_100,
1118 #define kbd_is_als_mode_bit(bit) \
1119 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1120 #define kbd_is_trigger_mode_bit(bit) \
1121 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1122 #define kbd_is_level_mode_bit(bit) \
1123 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1141 u8 timeout_value_ac;
1148 static const int kbd_tokens[] = {
1150 KBD_LED_AUTO_25_TOKEN,
1151 KBD_LED_AUTO_50_TOKEN,
1152 KBD_LED_AUTO_75_TOKEN,
1153 KBD_LED_AUTO_100_TOKEN,
1157 static u16 kbd_token_bits;
1159 static struct kbd_info kbd_info;
1160 static bool kbd_als_supported;
1161 static bool kbd_triggers_supported;
1162 static bool kbd_timeout_ac_supported;
1164 static u8 kbd_mode_levels[16];
1165 static int kbd_mode_levels_count;
1167 static u8 kbd_previous_level;
1168 static u8 kbd_previous_mode_bit;
1170 static bool kbd_led_present;
1171 static DEFINE_MUTEX(kbd_led_mutex);
1172 static enum led_brightness kbd_led_level;
1175 * NOTE: there are three ways to set the keyboard backlight level.
1176 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1177 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1178 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1180 * There are laptops which support only one of these methods. If we want to
1181 * support as many machines as possible we need to implement all three methods.
1182 * The first two methods use the kbd_state structure. The third uses SMBIOS
1183 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1184 * keyboard backlight level via kbd_state.level.
1187 static int kbd_get_info(struct kbd_info *info)
1189 struct calling_interface_buffer buffer;
1193 dell_fill_request(&buffer, 0, 0, 0, 0);
1194 ret = dell_send_request(&buffer,
1195 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1199 info->modes = buffer.output[1] & 0xFFFF;
1200 info->type = (buffer.output[1] >> 24) & 0xFF;
1201 info->triggers = buffer.output[2] & 0xFF;
1202 units = (buffer.output[2] >> 8) & 0xFF;
1203 info->levels = (buffer.output[2] >> 16) & 0xFF;
1205 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1209 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1211 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1213 info->hours = (buffer.output[3] >> 16) & 0xFF;
1215 info->days = (buffer.output[3] >> 24) & 0xFF;
1220 static unsigned int kbd_get_max_level(void)
1222 if (kbd_info.levels != 0)
1223 return kbd_info.levels;
1224 if (kbd_mode_levels_count > 0)
1225 return kbd_mode_levels_count - 1;
1229 static int kbd_get_level(struct kbd_state *state)
1233 if (kbd_info.levels != 0)
1234 return state->level;
1236 if (kbd_mode_levels_count > 0) {
1237 for (i = 0; i < kbd_mode_levels_count; ++i)
1238 if (kbd_mode_levels[i] == state->mode_bit)
1246 static int kbd_set_level(struct kbd_state *state, u8 level)
1248 if (kbd_info.levels != 0) {
1250 kbd_previous_level = level;
1251 if (state->level == level)
1253 state->level = level;
1254 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1255 state->mode_bit = kbd_previous_mode_bit;
1256 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1257 kbd_previous_mode_bit = state->mode_bit;
1258 state->mode_bit = KBD_MODE_BIT_OFF;
1263 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1265 kbd_previous_level = level;
1266 state->mode_bit = kbd_mode_levels[level];
1273 static int kbd_get_state(struct kbd_state *state)
1275 struct calling_interface_buffer buffer;
1278 dell_fill_request(&buffer, 0x1, 0, 0, 0);
1279 ret = dell_send_request(&buffer,
1280 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1284 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1285 if (state->mode_bit != 0)
1288 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1289 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1290 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1291 state->als_setting = buffer.output[2] & 0xFF;
1292 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1293 state->level = (buffer.output[2] >> 16) & 0xFF;
1294 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1295 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1300 static int kbd_set_state(struct kbd_state *state)
1302 struct calling_interface_buffer buffer;
1307 input1 = BIT(state->mode_bit) & 0xFFFF;
1308 input1 |= (state->triggers & 0xFF) << 16;
1309 input1 |= (state->timeout_value & 0x3F) << 24;
1310 input1 |= (state->timeout_unit & 0x3) << 30;
1311 input2 = state->als_setting & 0xFF;
1312 input2 |= (state->level & 0xFF) << 16;
1313 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1314 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1315 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1316 ret = dell_send_request(&buffer,
1317 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1322 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1326 ret = kbd_set_state(state);
1331 * When setting the new state fails,try to restore the previous one.
1332 * This is needed on some machines where BIOS sets a default state when
1333 * setting a new state fails. This default state could be all off.
1336 if (kbd_set_state(old))
1337 pr_err("Setting old previous keyboard state failed\n");
1342 static int kbd_set_token_bit(u8 bit)
1344 struct calling_interface_buffer buffer;
1345 struct calling_interface_token *token;
1348 if (bit >= ARRAY_SIZE(kbd_tokens))
1351 token = dell_smbios_find_token(kbd_tokens[bit]);
1355 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1356 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1361 static int kbd_get_token_bit(u8 bit)
1363 struct calling_interface_buffer buffer;
1364 struct calling_interface_token *token;
1368 if (bit >= ARRAY_SIZE(kbd_tokens))
1371 token = dell_smbios_find_token(kbd_tokens[bit]);
1375 dell_fill_request(&buffer, token->location, 0, 0, 0);
1376 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1377 val = buffer.output[1];
1382 return (val == token->value);
1385 static int kbd_get_first_active_token_bit(void)
1390 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1391 ret = kbd_get_token_bit(i);
1399 static int kbd_get_valid_token_counts(void)
1401 return hweight16(kbd_token_bits);
1404 static inline int kbd_init_info(void)
1406 struct kbd_state state;
1410 ret = kbd_get_info(&kbd_info);
1414 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1415 * timeout value which is shared for both battery and AC power
1416 * settings. So do not try to set AC values on old models.
1418 if ((quirks && quirks->kbd_missing_ac_tag) ||
1419 dell_smbios_find_token(KBD_LED_AC_TOKEN))
1420 kbd_timeout_ac_supported = true;
1422 kbd_get_state(&state);
1424 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1425 if (kbd_info.seconds > 63)
1426 kbd_info.seconds = 63;
1427 if (kbd_info.minutes > 63)
1428 kbd_info.minutes = 63;
1429 if (kbd_info.hours > 63)
1430 kbd_info.hours = 63;
1431 if (kbd_info.days > 63)
1434 /* NOTE: On tested machines ON mode did not work and caused
1435 * problems (turned backlight off) so do not use it
1437 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1439 kbd_previous_level = kbd_get_level(&state);
1440 kbd_previous_mode_bit = state.mode_bit;
1442 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1443 kbd_previous_level = 1;
1445 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1446 kbd_previous_mode_bit =
1447 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1448 if (kbd_previous_mode_bit != 0)
1449 kbd_previous_mode_bit--;
1452 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1453 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1454 kbd_als_supported = true;
1456 if (kbd_info.modes & (
1457 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1458 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1459 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1461 kbd_triggers_supported = true;
1463 /* kbd_mode_levels[0] is reserved, see below */
1464 for (i = 0; i < 16; ++i)
1465 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1466 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1469 * Find the first supported mode and assign to kbd_mode_levels[0].
1470 * This should be 0 (off), but we cannot depend on the BIOS to
1473 if (kbd_mode_levels_count > 0) {
1474 for (i = 0; i < 16; ++i) {
1475 if (BIT(i) & kbd_info.modes) {
1476 kbd_mode_levels[0] = i;
1480 kbd_mode_levels_count++;
1487 static inline void kbd_init_tokens(void)
1491 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1492 if (dell_smbios_find_token(kbd_tokens[i]))
1493 kbd_token_bits |= BIT(i);
1496 static void kbd_init(void)
1500 ret = kbd_init_info();
1504 * Only supports keyboard backlight when it has at least two modes.
1506 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1507 || kbd_get_valid_token_counts() >= 2)
1508 kbd_led_present = true;
1511 static ssize_t kbd_led_timeout_store(struct device *dev,
1512 struct device_attribute *attr,
1513 const char *buf, size_t count)
1515 struct kbd_state new_state;
1516 struct kbd_state state;
1524 ret = sscanf(buf, "%d %c", &value, &ch);
1537 if (value > kbd_info.seconds)
1539 unit = KBD_TIMEOUT_SECONDS;
1542 if (value > kbd_info.minutes)
1544 unit = KBD_TIMEOUT_MINUTES;
1547 if (value > kbd_info.hours)
1549 unit = KBD_TIMEOUT_HOURS;
1552 if (value > kbd_info.days)
1554 unit = KBD_TIMEOUT_DAYS;
1560 if (quirks && quirks->needs_kbd_timeouts)
1564 /* Convert value from current units to seconds */
1566 case KBD_TIMEOUT_DAYS:
1568 case KBD_TIMEOUT_HOURS:
1570 case KBD_TIMEOUT_MINUTES:
1572 unit = KBD_TIMEOUT_SECONDS;
1575 if (quirks && quirks->needs_kbd_timeouts) {
1576 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1577 if (value <= quirks->kbd_timeouts[i]) {
1578 value = quirks->kbd_timeouts[i];
1584 if (value <= kbd_info.seconds && kbd_info.seconds) {
1585 unit = KBD_TIMEOUT_SECONDS;
1586 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1588 unit = KBD_TIMEOUT_MINUTES;
1589 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1591 unit = KBD_TIMEOUT_HOURS;
1592 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1593 value /= (60 * 60 * 24);
1594 unit = KBD_TIMEOUT_DAYS;
1600 mutex_lock(&kbd_led_mutex);
1602 ret = kbd_get_state(&state);
1608 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1609 new_state.timeout_value_ac = value;
1610 new_state.timeout_unit_ac = unit;
1612 new_state.timeout_value = value;
1613 new_state.timeout_unit = unit;
1616 ret = kbd_set_state_safe(&new_state, &state);
1622 mutex_unlock(&kbd_led_mutex);
1626 static ssize_t kbd_led_timeout_show(struct device *dev,
1627 struct device_attribute *attr, char *buf)
1629 struct kbd_state state;
1635 ret = kbd_get_state(&state);
1639 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1640 value = state.timeout_value_ac;
1641 unit = state.timeout_unit_ac;
1643 value = state.timeout_value;
1644 unit = state.timeout_unit;
1647 len = sprintf(buf, "%d", value);
1650 case KBD_TIMEOUT_SECONDS:
1651 return len + sprintf(buf+len, "s\n");
1652 case KBD_TIMEOUT_MINUTES:
1653 return len + sprintf(buf+len, "m\n");
1654 case KBD_TIMEOUT_HOURS:
1655 return len + sprintf(buf+len, "h\n");
1656 case KBD_TIMEOUT_DAYS:
1657 return len + sprintf(buf+len, "d\n");
1665 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1666 kbd_led_timeout_show, kbd_led_timeout_store);
1668 static const char * const kbd_led_triggers[] = {
1671 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1675 static ssize_t kbd_led_triggers_store(struct device *dev,
1676 struct device_attribute *attr,
1677 const char *buf, size_t count)
1679 struct kbd_state new_state;
1680 struct kbd_state state;
1681 bool triggers_enabled = false;
1682 int trigger_bit = -1;
1686 ret = sscanf(buf, "%20s", trigger);
1690 if (trigger[0] != '+' && trigger[0] != '-')
1693 mutex_lock(&kbd_led_mutex);
1695 ret = kbd_get_state(&state);
1699 if (kbd_triggers_supported)
1700 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1702 if (kbd_triggers_supported) {
1703 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1704 if (!(kbd_info.triggers & BIT(i)))
1706 if (!kbd_led_triggers[i])
1708 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1710 if (trigger[0] == '+' &&
1711 triggers_enabled && (state.triggers & BIT(i))) {
1715 if (trigger[0] == '-' &&
1716 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1725 if (trigger_bit == -1) {
1731 if (trigger[0] == '+')
1732 new_state.triggers |= BIT(trigger_bit);
1734 new_state.triggers &= ~BIT(trigger_bit);
1736 * NOTE: trackstick bit (2) must be disabled when
1737 * disabling touchpad bit (1), otherwise touchpad
1738 * bit (1) will not be disabled
1740 if (trigger_bit == 1)
1741 new_state.triggers &= ~BIT(2);
1743 if ((kbd_info.triggers & new_state.triggers) !=
1744 new_state.triggers) {
1748 if (new_state.triggers && !triggers_enabled) {
1749 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1750 kbd_set_level(&new_state, kbd_previous_level);
1751 } else if (new_state.triggers == 0) {
1752 kbd_set_level(&new_state, 0);
1754 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1758 ret = kbd_set_state_safe(&new_state, &state);
1761 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1762 kbd_previous_mode_bit = new_state.mode_bit;
1765 mutex_unlock(&kbd_led_mutex);
1769 static ssize_t kbd_led_triggers_show(struct device *dev,
1770 struct device_attribute *attr, char *buf)
1772 struct kbd_state state;
1773 bool triggers_enabled;
1777 ret = kbd_get_state(&state);
1783 if (kbd_triggers_supported) {
1784 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1785 level = kbd_get_level(&state);
1786 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1787 if (!(kbd_info.triggers & BIT(i)))
1789 if (!kbd_led_triggers[i])
1791 if ((triggers_enabled || level <= 0) &&
1792 (state.triggers & BIT(i)))
1796 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1801 buf[len - 1] = '\n';
1806 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1807 kbd_led_triggers_show, kbd_led_triggers_store);
1809 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1810 struct device_attribute *attr,
1811 const char *buf, size_t count)
1813 struct kbd_state new_state;
1814 struct kbd_state state;
1815 bool triggers_enabled = false;
1819 ret = kstrtoint(buf, 0, &enable);
1823 mutex_lock(&kbd_led_mutex);
1825 ret = kbd_get_state(&state);
1829 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1836 if (kbd_triggers_supported)
1837 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1840 if (triggers_enabled)
1841 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1843 new_state.mode_bit = KBD_MODE_BIT_ALS;
1845 if (triggers_enabled) {
1846 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1847 kbd_set_level(&new_state, kbd_previous_level);
1849 new_state.mode_bit = KBD_MODE_BIT_ON;
1852 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1857 ret = kbd_set_state_safe(&new_state, &state);
1860 kbd_previous_mode_bit = new_state.mode_bit;
1864 mutex_unlock(&kbd_led_mutex);
1868 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1869 struct device_attribute *attr,
1872 struct kbd_state state;
1873 bool enabled = false;
1876 ret = kbd_get_state(&state);
1879 enabled = kbd_is_als_mode_bit(state.mode_bit);
1881 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1884 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1885 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1887 static ssize_t kbd_led_als_setting_store(struct device *dev,
1888 struct device_attribute *attr,
1889 const char *buf, size_t count)
1891 struct kbd_state state;
1892 struct kbd_state new_state;
1896 ret = kstrtou8(buf, 10, &setting);
1900 mutex_lock(&kbd_led_mutex);
1902 ret = kbd_get_state(&state);
1907 new_state.als_setting = setting;
1909 ret = kbd_set_state_safe(&new_state, &state);
1915 mutex_unlock(&kbd_led_mutex);
1919 static ssize_t kbd_led_als_setting_show(struct device *dev,
1920 struct device_attribute *attr,
1923 struct kbd_state state;
1926 ret = kbd_get_state(&state);
1930 return sprintf(buf, "%d\n", state.als_setting);
1933 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1934 kbd_led_als_setting_show, kbd_led_als_setting_store);
1936 static struct attribute *kbd_led_attrs[] = {
1937 &dev_attr_stop_timeout.attr,
1938 &dev_attr_start_triggers.attr,
1942 static const struct attribute_group kbd_led_group = {
1943 .attrs = kbd_led_attrs,
1946 static struct attribute *kbd_led_als_attrs[] = {
1947 &dev_attr_als_enabled.attr,
1948 &dev_attr_als_setting.attr,
1952 static const struct attribute_group kbd_led_als_group = {
1953 .attrs = kbd_led_als_attrs,
1956 static const struct attribute_group *kbd_led_groups[] = {
1962 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1966 struct kbd_state state;
1968 if (kbd_get_max_level()) {
1969 ret = kbd_get_state(&state);
1972 ret = kbd_get_level(&state);
1978 if (kbd_get_valid_token_counts()) {
1979 ret = kbd_get_first_active_token_bit();
1982 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1983 num &= num - 1; /* clear the first bit set */
1986 return ffs(num) - 1;
1989 pr_warn("Keyboard brightness level control not supported\n");
1993 static int kbd_led_level_set(struct led_classdev *led_cdev,
1994 enum led_brightness value)
1996 enum led_brightness new_value = value;
1997 struct kbd_state state;
1998 struct kbd_state new_state;
2002 mutex_lock(&kbd_led_mutex);
2004 if (kbd_get_max_level()) {
2005 ret = kbd_get_state(&state);
2009 ret = kbd_set_level(&new_state, value);
2012 ret = kbd_set_state_safe(&new_state, &state);
2013 } else if (kbd_get_valid_token_counts()) {
2014 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2015 num &= num - 1; /* clear the first bit set */
2019 ret = kbd_set_token_bit(ffs(num) - 1);
2021 pr_warn("Keyboard brightness level control not supported\n");
2027 kbd_led_level = new_value;
2029 mutex_unlock(&kbd_led_mutex);
2033 static struct led_classdev kbd_led = {
2034 .name = "dell::kbd_backlight",
2035 .flags = LED_BRIGHT_HW_CHANGED,
2036 .brightness_set_blocking = kbd_led_level_set,
2037 .brightness_get = kbd_led_level_get,
2038 .groups = kbd_led_groups,
2041 static int __init kbd_led_init(struct device *dev)
2046 if (!kbd_led_present)
2048 if (!kbd_als_supported)
2049 kbd_led_groups[1] = NULL;
2050 kbd_led.max_brightness = kbd_get_max_level();
2051 if (!kbd_led.max_brightness) {
2052 kbd_led.max_brightness = kbd_get_valid_token_counts();
2053 if (kbd_led.max_brightness)
2054 kbd_led.max_brightness--;
2057 kbd_led_level = kbd_led_level_get(NULL);
2059 ret = led_classdev_register(dev, &kbd_led);
2061 kbd_led_present = false;
2066 static void brightness_set_exit(struct led_classdev *led_cdev,
2067 enum led_brightness value)
2069 /* Don't change backlight level on exit */
2072 static void kbd_led_exit(void)
2074 if (!kbd_led_present)
2076 kbd_led.brightness_set = brightness_set_exit;
2077 led_classdev_unregister(&kbd_led);
2080 static int dell_laptop_notifier_call(struct notifier_block *nb,
2081 unsigned long action, void *data)
2083 bool changed = false;
2084 enum led_brightness new_kbd_led_level;
2087 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2088 if (!kbd_led_present)
2091 mutex_lock(&kbd_led_mutex);
2092 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2093 if (kbd_led_level != new_kbd_led_level) {
2094 kbd_led_level = new_kbd_led_level;
2097 mutex_unlock(&kbd_led_mutex);
2100 led_classdev_notify_brightness_hw_changed(&kbd_led,
2108 static struct notifier_block dell_laptop_notifier = {
2109 .notifier_call = dell_laptop_notifier_call,
2112 int dell_micmute_led_set(int state)
2114 struct calling_interface_buffer buffer;
2115 struct calling_interface_token *token;
2118 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2119 else if (state == 1)
2120 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2127 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2128 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2132 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2134 static int __init dell_init(void)
2136 struct calling_interface_token *token;
2137 int max_intensity = 0;
2140 if (!dmi_check_system(dell_device_table))
2144 /* find if this machine support other functions */
2145 dmi_check_system(dell_quirks);
2147 ret = platform_driver_register(&platform_driver);
2149 goto fail_platform_driver;
2150 platform_device = platform_device_alloc("dell-laptop", -1);
2151 if (!platform_device) {
2153 goto fail_platform_device1;
2155 ret = platform_device_add(platform_device);
2157 goto fail_platform_device2;
2159 ret = dell_setup_rfkill();
2162 pr_warn("Unable to setup rfkill\n");
2166 if (quirks && quirks->touchpad_led)
2167 touchpad_led_init(&platform_device->dev);
2169 kbd_led_init(&platform_device->dev);
2171 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2172 if (dell_laptop_dir != NULL)
2173 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2174 &dell_debugfs_fops);
2176 dell_laptop_register_notifier(&dell_laptop_notifier);
2178 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2181 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2183 struct calling_interface_buffer buffer;
2185 dell_fill_request(&buffer, token->location, 0, 0, 0);
2186 ret = dell_send_request(&buffer,
2187 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2189 max_intensity = buffer.output[3];
2192 if (max_intensity) {
2193 struct backlight_properties props;
2194 memset(&props, 0, sizeof(struct backlight_properties));
2195 props.type = BACKLIGHT_PLATFORM;
2196 props.max_brightness = max_intensity;
2197 dell_backlight_device = backlight_device_register("dell_backlight",
2198 &platform_device->dev,
2203 if (IS_ERR(dell_backlight_device)) {
2204 ret = PTR_ERR(dell_backlight_device);
2205 dell_backlight_device = NULL;
2206 goto fail_backlight;
2209 dell_backlight_device->props.brightness =
2210 dell_get_intensity(dell_backlight_device);
2211 if (dell_backlight_device->props.brightness < 0) {
2212 ret = dell_backlight_device->props.brightness;
2213 goto fail_get_brightness;
2215 backlight_update_status(dell_backlight_device);
2220 fail_get_brightness:
2221 backlight_device_unregister(dell_backlight_device);
2223 dell_cleanup_rfkill();
2225 platform_device_del(platform_device);
2226 fail_platform_device2:
2227 platform_device_put(platform_device);
2228 fail_platform_device1:
2229 platform_driver_unregister(&platform_driver);
2230 fail_platform_driver:
2234 static void __exit dell_exit(void)
2236 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2237 debugfs_remove_recursive(dell_laptop_dir);
2238 if (quirks && quirks->touchpad_led)
2239 touchpad_led_exit();
2241 backlight_device_unregister(dell_backlight_device);
2242 dell_cleanup_rfkill();
2243 if (platform_device) {
2244 platform_device_unregister(platform_device);
2245 platform_driver_unregister(&platform_driver);
2249 /* dell-rbtn.c driver export functions which will not work correctly (and could
2250 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2251 * not problem when dell-rbtn.c is compiled as external module. When both files
2252 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2253 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2254 * This can be achieved by late_initcall() instead module_init().
2256 late_initcall(dell_init);
2257 module_exit(dell_exit);
2262 MODULE_DESCRIPTION("Dell laptop driver");
2263 MODULE_LICENSE("GPL");