2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/slab.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "../../firmware/dcdbas.h"
36 #include "dell-rbtn.h"
38 #define BRIGHTNESS_TOKEN 0x7d
39 #define KBD_LED_OFF_TOKEN 0x01E1
40 #define KBD_LED_ON_TOKEN 0x01E2
41 #define KBD_LED_AUTO_TOKEN 0x01E3
42 #define KBD_LED_AUTO_25_TOKEN 0x02EA
43 #define KBD_LED_AUTO_50_TOKEN 0x02EB
44 #define KBD_LED_AUTO_75_TOKEN 0x02EC
45 #define KBD_LED_AUTO_100_TOKEN 0x02F6
47 /* This structure will be modified by the firmware when we enter
48 * system management mode, hence the volatiles */
50 struct calling_interface_buffer {
53 volatile u32 input[4];
54 volatile u32 output[4];
57 struct calling_interface_token {
66 struct calling_interface_structure {
67 struct dmi_header header;
71 struct calling_interface_token tokens[];
77 int needs_kbd_timeouts;
79 * Ordered list of timeouts expressed in seconds.
80 * The list must end with -1
85 static struct quirk_entry *quirks;
87 static struct quirk_entry quirk_dell_vostro_v130 = {
91 static int __init dmi_matched(const struct dmi_system_id *dmi)
93 quirks = dmi->driver_data;
98 * These values come from Windows utility provided by Dell. If any other value
99 * is used then BIOS silently set timeout to 0 without any error message.
101 static struct quirk_entry quirk_dell_xps13_9333 = {
102 .needs_kbd_timeouts = 1,
103 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
106 static int da_command_address;
107 static int da_command_code;
108 static int da_num_tokens;
109 static struct calling_interface_token *da_tokens;
111 static struct platform_driver platform_driver = {
113 .name = "dell-laptop",
117 static struct platform_device *platform_device;
118 static struct backlight_device *dell_backlight_device;
119 static struct rfkill *wifi_rfkill;
120 static struct rfkill *bluetooth_rfkill;
121 static struct rfkill *wwan_rfkill;
122 static bool force_rfkill;
124 module_param(force_rfkill, bool, 0444);
125 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
127 static const struct dmi_system_id dell_device_table[] __initconst = {
129 .ident = "Dell laptop",
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
137 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
138 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
142 .ident = "Dell Computer Corporation",
144 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
145 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
150 MODULE_DEVICE_TABLE(dmi, dell_device_table);
152 static const struct dmi_system_id dell_quirks[] __initconst = {
154 .callback = dmi_matched,
155 .ident = "Dell Vostro V130",
157 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
158 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
160 .driver_data = &quirk_dell_vostro_v130,
163 .callback = dmi_matched,
164 .ident = "Dell Vostro V131",
166 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
167 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
169 .driver_data = &quirk_dell_vostro_v130,
172 .callback = dmi_matched,
173 .ident = "Dell Vostro 3350",
175 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
178 .driver_data = &quirk_dell_vostro_v130,
181 .callback = dmi_matched,
182 .ident = "Dell Vostro 3555",
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
187 .driver_data = &quirk_dell_vostro_v130,
190 .callback = dmi_matched,
191 .ident = "Dell Inspiron N311z",
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
196 .driver_data = &quirk_dell_vostro_v130,
199 .callback = dmi_matched,
200 .ident = "Dell Inspiron M5110",
202 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
203 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
205 .driver_data = &quirk_dell_vostro_v130,
208 .callback = dmi_matched,
209 .ident = "Dell Vostro 3360",
211 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
212 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
214 .driver_data = &quirk_dell_vostro_v130,
217 .callback = dmi_matched,
218 .ident = "Dell Vostro 3460",
220 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
221 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
223 .driver_data = &quirk_dell_vostro_v130,
226 .callback = dmi_matched,
227 .ident = "Dell Vostro 3560",
229 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
230 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
232 .driver_data = &quirk_dell_vostro_v130,
235 .callback = dmi_matched,
236 .ident = "Dell Vostro 3450",
238 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
239 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
241 .driver_data = &quirk_dell_vostro_v130,
244 .callback = dmi_matched,
245 .ident = "Dell Inspiron 5420",
247 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
248 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
250 .driver_data = &quirk_dell_vostro_v130,
253 .callback = dmi_matched,
254 .ident = "Dell Inspiron 5520",
256 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
257 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
259 .driver_data = &quirk_dell_vostro_v130,
262 .callback = dmi_matched,
263 .ident = "Dell Inspiron 5720",
265 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
266 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
268 .driver_data = &quirk_dell_vostro_v130,
271 .callback = dmi_matched,
272 .ident = "Dell Inspiron 7420",
274 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
275 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
277 .driver_data = &quirk_dell_vostro_v130,
280 .callback = dmi_matched,
281 .ident = "Dell Inspiron 7520",
283 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
284 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
286 .driver_data = &quirk_dell_vostro_v130,
289 .callback = dmi_matched,
290 .ident = "Dell Inspiron 7720",
292 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
293 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
295 .driver_data = &quirk_dell_vostro_v130,
298 .callback = dmi_matched,
299 .ident = "Dell XPS13 9333",
301 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
302 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
304 .driver_data = &quirk_dell_xps13_9333,
309 static struct calling_interface_buffer *buffer;
310 static DEFINE_MUTEX(buffer_mutex);
312 static int hwswitch_state;
314 static void get_buffer(void)
316 mutex_lock(&buffer_mutex);
317 memset(buffer, 0, sizeof(struct calling_interface_buffer));
320 static void release_buffer(void)
322 mutex_unlock(&buffer_mutex);
325 static void __init parse_da_table(const struct dmi_header *dm)
327 /* Final token is a terminator, so we don't want to copy it */
328 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
329 struct calling_interface_token *new_da_tokens;
330 struct calling_interface_structure *table =
331 container_of(dm, struct calling_interface_structure, header);
333 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
339 da_command_address = table->cmdIOAddress;
340 da_command_code = table->cmdIOCode;
342 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
343 sizeof(struct calling_interface_token),
348 da_tokens = new_da_tokens;
350 memcpy(da_tokens+da_num_tokens, table->tokens,
351 sizeof(struct calling_interface_token) * tokens);
353 da_num_tokens += tokens;
356 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
359 case 0xd4: /* Indexed IO */
360 case 0xd5: /* Protected Area Type 1 */
361 case 0xd6: /* Protected Area Type 2 */
363 case 0xda: /* Calling interface */
369 static int find_token_id(int tokenid)
373 for (i = 0; i < da_num_tokens; i++) {
374 if (da_tokens[i].tokenID == tokenid)
381 static int find_token_location(int tokenid)
385 id = find_token_id(tokenid);
389 return da_tokens[id].location;
392 static struct calling_interface_buffer *
393 dell_send_request(struct calling_interface_buffer *buffer, int class,
396 struct smi_cmd command;
398 command.magic = SMI_CMD_MAGIC;
399 command.command_address = da_command_address;
400 command.command_code = da_command_code;
401 command.ebx = virt_to_phys(buffer);
402 command.ecx = 0x42534931;
404 buffer->class = class;
405 buffer->select = select;
407 dcdbas_smi_request(&command);
412 static inline int dell_smi_error(int value)
415 case 0: /* Completed successfully */
417 case -1: /* Completed with error */
419 case -2: /* Function not supported */
421 default: /* Unknown error */
427 * Derived from information in smbios-wireless-ctl:
429 * cbSelect 17, Value 11
431 * Return Wireless Info
432 * cbArg1, byte0 = 0x00
434 * cbRes1 Standard return codes (0, -1, -2)
435 * cbRes2 Info bit flags:
437 * 0 Hardware switch supported (1)
438 * 1 WiFi locator supported (1)
439 * 2 WLAN supported (1)
440 * 3 Bluetooth (BT) supported (1)
441 * 4 WWAN supported (1)
442 * 5 Wireless KBD supported (1)
443 * 6 Uw b supported (1)
444 * 7 WiGig supported (1)
445 * 8 WLAN installed (1)
447 * 10 WWAN installed (1)
448 * 11 Uw b installed (1)
449 * 12 WiGig installed (1)
451 * 16 Hardware (HW) switch is On (1)
452 * 17 WLAN disabled (1)
454 * 19 WWAN disabled (1)
455 * 20 Uw b disabled (1)
456 * 21 WiGig disabled (1)
459 * cbRes3 NVRAM size in bytes
460 * cbRes4, byte 0 NVRAM format version number
463 * Set QuickSet Radio Disable Flag
464 * cbArg1, byte0 = 0x01
473 * cbArg1, byte2 Flag bits:
474 * 0 QuickSet disables radio (1)
477 * cbRes1 Standard return codes (0, -1, -2)
478 * cbRes2 QuickSet (QS) radio disable bit map:
483 * 4 QS disables WIGIG
486 * Wireless Switch Configuration
487 * cbArg1, byte0 = 0x02
493 * 2 Set WiFi locator enable/disable
495 * Switch settings (if byte 1==1):
496 * 0 WLAN sw itch control (1)
497 * 1 BT sw itch control (1)
498 * 2 WWAN sw itch control (1)
499 * 3 UWB sw itch control (1)
500 * 4 WiGig sw itch control (1)
502 * cbArg1, byte2 Enable bits (if byte 1==2):
503 * 0 Enable WiFi locator (1)
505 * cbRes1 Standard return codes (0, -1, -2)
506 * cbRes2 QuickSet radio disable bit map:
507 * 0 WLAN controlled by sw itch (1)
508 * 1 BT controlled by sw itch (1)
509 * 2 WWAN controlled by sw itch (1)
510 * 3 UWB controlled by sw itch (1)
511 * 4 WiGig controlled by sw itch (1)
513 * 7 Wireless sw itch config locked (1)
514 * 8 WiFi locator enabled (1)
516 * 15 WiFi locator setting locked (1)
519 * Read Local Config Data (LCD)
520 * cbArg1, byte0 = 0x10
521 * cbArg1, byte1 NVRAM index low byte
522 * cbArg1, byte2 NVRAM index high byte
523 * cbRes1 Standard return codes (0, -1, -2)
524 * cbRes2 4 bytes read from LCD[index]
525 * cbRes3 4 bytes read from LCD[index+4]
526 * cbRes4 4 bytes read from LCD[index+8]
528 * Write Local Config Data (LCD)
529 * cbArg1, byte0 = 0x11
530 * cbArg1, byte1 NVRAM index low byte
531 * cbArg1, byte2 NVRAM index high byte
532 * cbArg2 4 bytes to w rite at LCD[index]
533 * cbArg3 4 bytes to w rite at LCD[index+4]
534 * cbArg4 4 bytes to w rite at LCD[index+8]
535 * cbRes1 Standard return codes (0, -1, -2)
537 * Populate Local Config Data from NVRAM
538 * cbArg1, byte0 = 0x12
539 * cbRes1 Standard return codes (0, -1, -2)
541 * Commit Local Config Data to NVRAM
542 * cbArg1, byte0 = 0x13
543 * cbRes1 Standard return codes (0, -1, -2)
546 static int dell_rfkill_set(void *data, bool blocked)
548 int disable = blocked ? 1 : 0;
549 unsigned long radio = (unsigned long)data;
550 int hwswitch_bit = (unsigned long)data - 1;
553 dell_send_request(buffer, 17, 11);
555 /* If the hardware switch controls this radio, and the hardware
556 switch is disabled, always disable the radio */
557 if ((hwswitch_state & BIT(hwswitch_bit)) &&
558 !(buffer->output[1] & BIT(16)))
561 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
562 dell_send_request(buffer, 17, 11);
568 /* Must be called with the buffer held */
569 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
572 if (status & BIT(0)) {
573 /* Has hw-switch, sync sw_state to BIOS */
574 int block = rfkill_blocked(rfkill);
575 buffer->input[0] = (1 | (radio << 8) | (block << 16));
576 dell_send_request(buffer, 17, 11);
578 /* No hw-switch, sync BIOS state to sw_state */
579 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
583 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
586 if (hwswitch_state & (BIT(radio - 1)))
587 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
590 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
595 dell_send_request(buffer, 17, 11);
596 status = buffer->output[1];
598 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
603 static const struct rfkill_ops dell_rfkill_ops = {
604 .set_block = dell_rfkill_set,
605 .query = dell_rfkill_query,
608 static struct dentry *dell_laptop_dir;
610 static int dell_debugfs_show(struct seq_file *s, void *data)
615 dell_send_request(buffer, 17, 11);
616 status = buffer->output[1];
619 seq_printf(s, "status:\t0x%X\n", status);
620 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
622 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
623 (status & BIT(1)) >> 1);
624 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
625 (status & BIT(2)) >> 2);
626 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
627 (status & BIT(3)) >> 3);
628 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
629 (status & BIT(4)) >> 4);
630 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
631 (status & BIT(5)) >> 5);
632 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
633 (status & BIT(6)) >> 6);
634 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
635 (status & BIT(7)) >> 7);
636 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
637 (status & BIT(8)) >> 8);
638 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
639 (status & BIT(9)) >> 9);
640 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
641 (status & BIT(10)) >> 10);
642 seq_printf(s, "Bit 11: UWB installed: %lu\n",
643 (status & BIT(11)) >> 11);
644 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
645 (status & BIT(12)) >> 12);
647 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
648 (status & BIT(16)) >> 16);
649 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
650 (status & BIT(17)) >> 17);
651 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
652 (status & BIT(18)) >> 18);
653 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
654 (status & BIT(19)) >> 19);
655 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
656 (status & BIT(20)) >> 20);
657 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
658 (status & BIT(21)) >> 21);
660 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
661 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
662 hwswitch_state & BIT(0));
663 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
664 (hwswitch_state & BIT(1)) >> 1);
665 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
666 (hwswitch_state & BIT(2)) >> 2);
667 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
668 (hwswitch_state & BIT(3)) >> 3);
669 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
670 (hwswitch_state & BIT(4)) >> 4);
671 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
672 (hwswitch_state & BIT(7)) >> 7);
673 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
674 (hwswitch_state & BIT(8)) >> 8);
675 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
676 (hwswitch_state & BIT(15)) >> 15);
681 static int dell_debugfs_open(struct inode *inode, struct file *file)
683 return single_open(file, dell_debugfs_show, inode->i_private);
686 static const struct file_operations dell_debugfs_fops = {
687 .owner = THIS_MODULE,
688 .open = dell_debugfs_open,
691 .release = single_release,
694 static void dell_update_rfkill(struct work_struct *ignored)
699 dell_send_request(buffer, 17, 11);
700 status = buffer->output[1];
703 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
704 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
706 if (bluetooth_rfkill) {
707 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
708 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
711 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
712 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
717 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
719 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
722 static bool extended;
724 if (str & I8042_STR_AUXDATA)
727 if (unlikely(data == 0xe0)) {
730 } else if (unlikely(extended)) {
733 schedule_delayed_work(&dell_rfkill_work,
734 round_jiffies_relative(HZ / 4));
743 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
744 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
746 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
747 unsigned long action, void *data)
749 schedule_delayed_work(&dell_rfkill_work, 0);
753 static struct notifier_block dell_laptop_rbtn_notifier = {
754 .notifier_call = dell_laptop_rbtn_notifier_call,
757 static int __init dell_setup_rfkill(void)
759 int status, ret, whitelisted;
763 * rfkill support causes trouble on various models, mostly Inspirons.
764 * So we whitelist certain series, and don't support rfkill on others.
767 product = dmi_get_system_info(DMI_PRODUCT_NAME);
768 if (product && (strncmp(product, "Latitude", 8) == 0 ||
769 strncmp(product, "Precision", 9) == 0))
771 if (!force_rfkill && !whitelisted)
775 dell_send_request(buffer, 17, 11);
776 status = buffer->output[1];
777 buffer->input[0] = 0x2;
778 dell_send_request(buffer, 17, 11);
779 hwswitch_state = buffer->output[1];
782 if (!(status & BIT(0))) {
784 /* No hwsitch, clear all hw-controlled bits */
785 hwswitch_state &= ~7;
787 /* rfkill is only tested on laptops with a hwswitch */
792 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
793 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
795 &dell_rfkill_ops, (void *) 1);
800 ret = rfkill_register(wifi_rfkill);
805 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
806 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
807 &platform_device->dev,
808 RFKILL_TYPE_BLUETOOTH,
809 &dell_rfkill_ops, (void *) 2);
810 if (!bluetooth_rfkill) {
814 ret = rfkill_register(bluetooth_rfkill);
819 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
820 wwan_rfkill = rfkill_alloc("dell-wwan",
821 &platform_device->dev,
823 &dell_rfkill_ops, (void *) 3);
828 ret = rfkill_register(wwan_rfkill);
834 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
835 * which can receive events from HW slider switch.
837 * Dell SMBIOS on whitelisted models supports controlling radio devices
838 * but does not support receiving HW button switch events. We can use
839 * i8042 filter hook function to receive keyboard data and handle
840 * keycode for HW button.
842 * So if it is possible we will use Dell Airplane Mode Switch ACPI
843 * driver for receiving HW events and Dell SMBIOS for setting rfkill
844 * states. If ACPI driver or device is not available we will fallback to
845 * i8042 filter hook function.
847 * To prevent duplicate rfkill devices which control and do same thing,
848 * dell-rbtn driver will automatically remove its own rfkill devices
849 * once function dell_rbtn_notifier_register() is called.
852 dell_rbtn_notifier_register_func =
853 symbol_request(dell_rbtn_notifier_register);
854 if (dell_rbtn_notifier_register_func) {
855 dell_rbtn_notifier_unregister_func =
856 symbol_request(dell_rbtn_notifier_unregister);
857 if (!dell_rbtn_notifier_unregister_func) {
858 symbol_put(dell_rbtn_notifier_register);
859 dell_rbtn_notifier_register_func = NULL;
863 if (dell_rbtn_notifier_register_func) {
864 ret = dell_rbtn_notifier_register_func(
865 &dell_laptop_rbtn_notifier);
866 symbol_put(dell_rbtn_notifier_register);
867 dell_rbtn_notifier_register_func = NULL;
869 symbol_put(dell_rbtn_notifier_unregister);
870 dell_rbtn_notifier_unregister_func = NULL;
873 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
878 pr_info("Using dell-rbtn acpi driver for receiving events\n");
879 } else if (ret != -ENODEV) {
880 pr_warn("Unable to register dell rbtn notifier\n");
883 ret = i8042_install_filter(dell_laptop_i8042_filter);
885 pr_warn("Unable to install key filter\n");
888 pr_info("Using i8042 filter function for receiving events\n");
894 rfkill_unregister(wwan_rfkill);
896 rfkill_destroy(wwan_rfkill);
897 if (bluetooth_rfkill)
898 rfkill_unregister(bluetooth_rfkill);
900 rfkill_destroy(bluetooth_rfkill);
902 rfkill_unregister(wifi_rfkill);
904 rfkill_destroy(wifi_rfkill);
909 static void dell_cleanup_rfkill(void)
911 if (dell_rbtn_notifier_unregister_func) {
912 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
913 symbol_put(dell_rbtn_notifier_unregister);
914 dell_rbtn_notifier_unregister_func = NULL;
916 i8042_remove_filter(dell_laptop_i8042_filter);
918 cancel_delayed_work_sync(&dell_rfkill_work);
920 rfkill_unregister(wifi_rfkill);
921 rfkill_destroy(wifi_rfkill);
923 if (bluetooth_rfkill) {
924 rfkill_unregister(bluetooth_rfkill);
925 rfkill_destroy(bluetooth_rfkill);
928 rfkill_unregister(wwan_rfkill);
929 rfkill_destroy(wwan_rfkill);
933 static int dell_send_intensity(struct backlight_device *bd)
938 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
939 buffer->input[1] = bd->props.brightness;
941 if (buffer->input[0] == -1) {
946 if (power_supply_is_system_supplied() > 0)
947 dell_send_request(buffer, 1, 2);
949 dell_send_request(buffer, 1, 1);
956 static int dell_get_intensity(struct backlight_device *bd)
961 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
963 if (buffer->input[0] == -1) {
968 if (power_supply_is_system_supplied() > 0)
969 dell_send_request(buffer, 0, 2);
971 dell_send_request(buffer, 0, 1);
973 ret = buffer->output[1];
980 static const struct backlight_ops dell_ops = {
981 .get_brightness = dell_get_intensity,
982 .update_status = dell_send_intensity,
985 static void touchpad_led_on(void)
989 i8042_command(&data, command | 1 << 12);
992 static void touchpad_led_off(void)
996 i8042_command(&data, command | 1 << 12);
999 static void touchpad_led_set(struct led_classdev *led_cdev,
1000 enum led_brightness value)
1008 static struct led_classdev touchpad_led = {
1009 .name = "dell-laptop::touchpad",
1010 .brightness_set = touchpad_led_set,
1011 .flags = LED_CORE_SUSPENDRESUME,
1014 static int __init touchpad_led_init(struct device *dev)
1016 return led_classdev_register(dev, &touchpad_led);
1019 static void touchpad_led_exit(void)
1021 led_classdev_unregister(&touchpad_led);
1025 * Derived from information in smbios-keyboard-ctl:
1029 * Keyboard illumination
1030 * cbArg1 determines the function to be performed
1032 * cbArg1 0x0 = Get Feature Information
1033 * cbRES1 Standard return codes (0, -1, -2)
1034 * cbRES2, word0 Bitmap of user-selectable modes
1035 * bit 0 Always off (All systems)
1036 * bit 1 Always on (Travis ATG, Siberia)
1037 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1038 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1039 * bit 4 Auto: Input-activity-based On; input-activity based Off
1040 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1041 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1042 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1043 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1044 * bits 9-15 Reserved for future use
1045 * cbRES2, byte2 Reserved for future use
1046 * cbRES2, byte3 Keyboard illumination type
1050 * 3-255 Reserved for future use
1051 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1052 * bit 0 Any keystroke
1053 * bit 1 Touchpad activity
1054 * bit 2 Pointing stick
1056 * bits 4-7 Reserved for future use
1057 * cbRES3, byte1 Supported timeout unit bitmap
1062 * bits 4-7 Reserved for future use
1063 * cbRES3, byte2 Number of keyboard light brightness levels
1064 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1065 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1066 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1067 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1069 * cbArg1 0x1 = Get Current State
1070 * cbRES1 Standard return codes (0, -1, -2)
1071 * cbRES2, word0 Bitmap of current mode state
1072 * bit 0 Always off (All systems)
1073 * bit 1 Always on (Travis ATG, Siberia)
1074 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1075 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1076 * bit 4 Auto: Input-activity-based On; input-activity based Off
1077 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1078 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1079 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1080 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1081 * bits 9-15 Reserved for future use
1082 * Note: Only One bit can be set
1083 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1084 * bit 0 Any keystroke
1085 * bit 1 Touchpad activity
1086 * bit 2 Pointing stick
1088 * bits 4-7 Reserved for future use
1089 * cbRES2, byte3 Current Timeout
1090 * bits 7:6 Timeout units indicator:
1095 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1096 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1097 * are set upon return from the [Get feature information] call.
1098 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1099 * cbRES3, byte1 Current ALS reading
1100 * cbRES3, byte2 Current keyboard light level.
1102 * cbArg1 0x2 = Set New State
1103 * cbRES1 Standard return codes (0, -1, -2)
1104 * cbArg2, word0 Bitmap of current mode state
1105 * bit 0 Always off (All systems)
1106 * bit 1 Always on (Travis ATG, Siberia)
1107 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1108 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1109 * bit 4 Auto: Input-activity-based On; input-activity based Off
1110 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1111 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1112 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1113 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1114 * bits 9-15 Reserved for future use
1115 * Note: Only One bit can be set
1116 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1117 * keyboard to turn off automatically.
1118 * bit 0 Any keystroke
1119 * bit 1 Touchpad activity
1120 * bit 2 Pointing stick
1122 * bits 4-7 Reserved for future use
1123 * cbArg2, byte3 Desired Timeout
1124 * bits 7:6 Timeout units indicator:
1129 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1130 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1131 * cbArg3, byte2 Desired keyboard light level.
1135 enum kbd_timeout_unit {
1136 KBD_TIMEOUT_SECONDS = 0,
1137 KBD_TIMEOUT_MINUTES,
1143 KBD_MODE_BIT_OFF = 0,
1146 KBD_MODE_BIT_TRIGGER_ALS,
1147 KBD_MODE_BIT_TRIGGER,
1148 KBD_MODE_BIT_TRIGGER_25,
1149 KBD_MODE_BIT_TRIGGER_50,
1150 KBD_MODE_BIT_TRIGGER_75,
1151 KBD_MODE_BIT_TRIGGER_100,
1154 #define kbd_is_als_mode_bit(bit) \
1155 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1156 #define kbd_is_trigger_mode_bit(bit) \
1157 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1158 #define kbd_is_level_mode_bit(bit) \
1159 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1182 static const int kbd_tokens[] = {
1184 KBD_LED_AUTO_25_TOKEN,
1185 KBD_LED_AUTO_50_TOKEN,
1186 KBD_LED_AUTO_75_TOKEN,
1187 KBD_LED_AUTO_100_TOKEN,
1191 static u16 kbd_token_bits;
1193 static struct kbd_info kbd_info;
1194 static bool kbd_als_supported;
1195 static bool kbd_triggers_supported;
1197 static u8 kbd_mode_levels[16];
1198 static int kbd_mode_levels_count;
1200 static u8 kbd_previous_level;
1201 static u8 kbd_previous_mode_bit;
1203 static bool kbd_led_present;
1206 * NOTE: there are three ways to set the keyboard backlight level.
1207 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1208 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1209 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1211 * There are laptops which support only one of these methods. If we want to
1212 * support as many machines as possible we need to implement all three methods.
1213 * The first two methods use the kbd_state structure. The third uses SMBIOS
1214 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1215 * keyboard backlight level via kbd_state.level.
1218 static int kbd_get_info(struct kbd_info *info)
1225 buffer->input[0] = 0x0;
1226 dell_send_request(buffer, 4, 11);
1227 ret = buffer->output[0];
1230 ret = dell_smi_error(ret);
1234 info->modes = buffer->output[1] & 0xFFFF;
1235 info->type = (buffer->output[1] >> 24) & 0xFF;
1236 info->triggers = buffer->output[2] & 0xFF;
1237 units = (buffer->output[2] >> 8) & 0xFF;
1238 info->levels = (buffer->output[2] >> 16) & 0xFF;
1241 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1243 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1245 info->hours = (buffer->output[3] >> 16) & 0xFF;
1247 info->days = (buffer->output[3] >> 24) & 0xFF;
1254 static unsigned int kbd_get_max_level(void)
1256 if (kbd_info.levels != 0)
1257 return kbd_info.levels;
1258 if (kbd_mode_levels_count > 0)
1259 return kbd_mode_levels_count - 1;
1263 static int kbd_get_level(struct kbd_state *state)
1267 if (kbd_info.levels != 0)
1268 return state->level;
1270 if (kbd_mode_levels_count > 0) {
1271 for (i = 0; i < kbd_mode_levels_count; ++i)
1272 if (kbd_mode_levels[i] == state->mode_bit)
1280 static int kbd_set_level(struct kbd_state *state, u8 level)
1282 if (kbd_info.levels != 0) {
1284 kbd_previous_level = level;
1285 if (state->level == level)
1287 state->level = level;
1288 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1289 state->mode_bit = kbd_previous_mode_bit;
1290 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1291 kbd_previous_mode_bit = state->mode_bit;
1292 state->mode_bit = KBD_MODE_BIT_OFF;
1297 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1299 kbd_previous_level = level;
1300 state->mode_bit = kbd_mode_levels[level];
1307 static int kbd_get_state(struct kbd_state *state)
1313 buffer->input[0] = 0x1;
1314 dell_send_request(buffer, 4, 11);
1315 ret = buffer->output[0];
1318 ret = dell_smi_error(ret);
1322 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1323 if (state->mode_bit != 0)
1326 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1327 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1328 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1329 state->als_setting = buffer->output[2] & 0xFF;
1330 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1331 state->level = (buffer->output[2] >> 16) & 0xFF;
1338 static int kbd_set_state(struct kbd_state *state)
1343 buffer->input[0] = 0x2;
1344 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1345 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1346 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1347 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1348 buffer->input[2] = state->als_setting & 0xFF;
1349 buffer->input[2] |= (state->level & 0xFF) << 16;
1350 dell_send_request(buffer, 4, 11);
1351 ret = buffer->output[0];
1354 return dell_smi_error(ret);
1357 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1361 ret = kbd_set_state(state);
1366 * When setting the new state fails,try to restore the previous one.
1367 * This is needed on some machines where BIOS sets a default state when
1368 * setting a new state fails. This default state could be all off.
1371 if (kbd_set_state(old))
1372 pr_err("Setting old previous keyboard state failed\n");
1377 static int kbd_set_token_bit(u8 bit)
1382 if (bit >= ARRAY_SIZE(kbd_tokens))
1385 id = find_token_id(kbd_tokens[bit]);
1390 buffer->input[0] = da_tokens[id].location;
1391 buffer->input[1] = da_tokens[id].value;
1392 dell_send_request(buffer, 1, 0);
1393 ret = buffer->output[0];
1396 return dell_smi_error(ret);
1399 static int kbd_get_token_bit(u8 bit)
1405 if (bit >= ARRAY_SIZE(kbd_tokens))
1408 id = find_token_id(kbd_tokens[bit]);
1413 buffer->input[0] = da_tokens[id].location;
1414 dell_send_request(buffer, 0, 0);
1415 ret = buffer->output[0];
1416 val = buffer->output[1];
1420 return dell_smi_error(ret);
1422 return (val == da_tokens[id].value);
1425 static int kbd_get_first_active_token_bit(void)
1430 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1431 ret = kbd_get_token_bit(i);
1439 static int kbd_get_valid_token_counts(void)
1441 return hweight16(kbd_token_bits);
1444 static inline int kbd_init_info(void)
1446 struct kbd_state state;
1450 ret = kbd_get_info(&kbd_info);
1454 kbd_get_state(&state);
1456 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1457 if (kbd_info.seconds > 63)
1458 kbd_info.seconds = 63;
1459 if (kbd_info.minutes > 63)
1460 kbd_info.minutes = 63;
1461 if (kbd_info.hours > 63)
1462 kbd_info.hours = 63;
1463 if (kbd_info.days > 63)
1466 /* NOTE: On tested machines ON mode did not work and caused
1467 * problems (turned backlight off) so do not use it
1469 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1471 kbd_previous_level = kbd_get_level(&state);
1472 kbd_previous_mode_bit = state.mode_bit;
1474 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1475 kbd_previous_level = 1;
1477 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1478 kbd_previous_mode_bit =
1479 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1480 if (kbd_previous_mode_bit != 0)
1481 kbd_previous_mode_bit--;
1484 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1485 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1486 kbd_als_supported = true;
1488 if (kbd_info.modes & (
1489 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1490 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1491 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1493 kbd_triggers_supported = true;
1495 /* kbd_mode_levels[0] is reserved, see below */
1496 for (i = 0; i < 16; ++i)
1497 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1498 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1501 * Find the first supported mode and assign to kbd_mode_levels[0].
1502 * This should be 0 (off), but we cannot depend on the BIOS to
1505 if (kbd_mode_levels_count > 0) {
1506 for (i = 0; i < 16; ++i) {
1507 if (BIT(i) & kbd_info.modes) {
1508 kbd_mode_levels[0] = i;
1512 kbd_mode_levels_count++;
1519 static inline void kbd_init_tokens(void)
1523 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1524 if (find_token_id(kbd_tokens[i]) != -1)
1525 kbd_token_bits |= BIT(i);
1528 static void kbd_init(void)
1532 ret = kbd_init_info();
1535 if (kbd_token_bits != 0 || ret == 0)
1536 kbd_led_present = true;
1539 static ssize_t kbd_led_timeout_store(struct device *dev,
1540 struct device_attribute *attr,
1541 const char *buf, size_t count)
1543 struct kbd_state new_state;
1544 struct kbd_state state;
1552 ret = sscanf(buf, "%d %c", &value, &ch);
1565 if (value > kbd_info.seconds)
1567 unit = KBD_TIMEOUT_SECONDS;
1570 if (value > kbd_info.minutes)
1572 unit = KBD_TIMEOUT_MINUTES;
1575 if (value > kbd_info.hours)
1577 unit = KBD_TIMEOUT_HOURS;
1580 if (value > kbd_info.days)
1582 unit = KBD_TIMEOUT_DAYS;
1588 if (quirks && quirks->needs_kbd_timeouts)
1592 /* Convert value from current units to seconds */
1594 case KBD_TIMEOUT_DAYS:
1596 case KBD_TIMEOUT_HOURS:
1598 case KBD_TIMEOUT_MINUTES:
1600 unit = KBD_TIMEOUT_SECONDS;
1603 if (quirks && quirks->needs_kbd_timeouts) {
1604 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1605 if (value <= quirks->kbd_timeouts[i]) {
1606 value = quirks->kbd_timeouts[i];
1612 if (value <= kbd_info.seconds && kbd_info.seconds) {
1613 unit = KBD_TIMEOUT_SECONDS;
1614 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1616 unit = KBD_TIMEOUT_MINUTES;
1617 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1619 unit = KBD_TIMEOUT_HOURS;
1620 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1621 value /= (60 * 60 * 24);
1622 unit = KBD_TIMEOUT_DAYS;
1628 ret = kbd_get_state(&state);
1633 new_state.timeout_value = value;
1634 new_state.timeout_unit = unit;
1636 ret = kbd_set_state_safe(&new_state, &state);
1643 static ssize_t kbd_led_timeout_show(struct device *dev,
1644 struct device_attribute *attr, char *buf)
1646 struct kbd_state state;
1650 ret = kbd_get_state(&state);
1654 len = sprintf(buf, "%d", state.timeout_value);
1656 switch (state.timeout_unit) {
1657 case KBD_TIMEOUT_SECONDS:
1658 return len + sprintf(buf+len, "s\n");
1659 case KBD_TIMEOUT_MINUTES:
1660 return len + sprintf(buf+len, "m\n");
1661 case KBD_TIMEOUT_HOURS:
1662 return len + sprintf(buf+len, "h\n");
1663 case KBD_TIMEOUT_DAYS:
1664 return len + sprintf(buf+len, "d\n");
1672 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1673 kbd_led_timeout_show, kbd_led_timeout_store);
1675 static const char * const kbd_led_triggers[] = {
1678 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1682 static ssize_t kbd_led_triggers_store(struct device *dev,
1683 struct device_attribute *attr,
1684 const char *buf, size_t count)
1686 struct kbd_state new_state;
1687 struct kbd_state state;
1688 bool triggers_enabled = false;
1689 int trigger_bit = -1;
1693 ret = sscanf(buf, "%20s", trigger);
1697 if (trigger[0] != '+' && trigger[0] != '-')
1700 ret = kbd_get_state(&state);
1704 if (kbd_triggers_supported)
1705 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1707 if (kbd_triggers_supported) {
1708 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1709 if (!(kbd_info.triggers & BIT(i)))
1711 if (!kbd_led_triggers[i])
1713 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1715 if (trigger[0] == '+' &&
1716 triggers_enabled && (state.triggers & BIT(i)))
1718 if (trigger[0] == '-' &&
1719 (!triggers_enabled || !(state.triggers & BIT(i))))
1726 if (trigger_bit != -1) {
1728 if (trigger[0] == '+')
1729 new_state.triggers |= BIT(trigger_bit);
1731 new_state.triggers &= ~BIT(trigger_bit);
1732 /* NOTE: trackstick bit (2) must be disabled when
1733 * disabling touchpad bit (1), otherwise touchpad
1734 * bit (1) will not be disabled */
1735 if (trigger_bit == 1)
1736 new_state.triggers &= ~BIT(2);
1738 if ((kbd_info.triggers & new_state.triggers) !=
1741 if (new_state.triggers && !triggers_enabled) {
1742 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1743 kbd_set_level(&new_state, kbd_previous_level);
1744 } else if (new_state.triggers == 0) {
1745 kbd_set_level(&new_state, 0);
1747 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1749 ret = kbd_set_state_safe(&new_state, &state);
1752 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1753 kbd_previous_mode_bit = new_state.mode_bit;
1760 static ssize_t kbd_led_triggers_show(struct device *dev,
1761 struct device_attribute *attr, char *buf)
1763 struct kbd_state state;
1764 bool triggers_enabled;
1768 ret = kbd_get_state(&state);
1774 if (kbd_triggers_supported) {
1775 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1776 level = kbd_get_level(&state);
1777 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1778 if (!(kbd_info.triggers & BIT(i)))
1780 if (!kbd_led_triggers[i])
1782 if ((triggers_enabled || level <= 0) &&
1783 (state.triggers & BIT(i)))
1787 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1792 buf[len - 1] = '\n';
1797 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1798 kbd_led_triggers_show, kbd_led_triggers_store);
1800 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1801 struct device_attribute *attr,
1802 const char *buf, size_t count)
1804 struct kbd_state new_state;
1805 struct kbd_state state;
1806 bool triggers_enabled = false;
1810 ret = kstrtoint(buf, 0, &enable);
1814 ret = kbd_get_state(&state);
1818 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1823 if (kbd_triggers_supported)
1824 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1827 if (triggers_enabled)
1828 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1830 new_state.mode_bit = KBD_MODE_BIT_ALS;
1832 if (triggers_enabled) {
1833 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1834 kbd_set_level(&new_state, kbd_previous_level);
1836 new_state.mode_bit = KBD_MODE_BIT_ON;
1839 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1842 ret = kbd_set_state_safe(&new_state, &state);
1845 kbd_previous_mode_bit = new_state.mode_bit;
1850 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1851 struct device_attribute *attr,
1854 struct kbd_state state;
1855 bool enabled = false;
1858 ret = kbd_get_state(&state);
1861 enabled = kbd_is_als_mode_bit(state.mode_bit);
1863 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1866 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1867 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1869 static ssize_t kbd_led_als_setting_store(struct device *dev,
1870 struct device_attribute *attr,
1871 const char *buf, size_t count)
1873 struct kbd_state state;
1874 struct kbd_state new_state;
1878 ret = kstrtou8(buf, 10, &setting);
1882 ret = kbd_get_state(&state);
1887 new_state.als_setting = setting;
1889 ret = kbd_set_state_safe(&new_state, &state);
1896 static ssize_t kbd_led_als_setting_show(struct device *dev,
1897 struct device_attribute *attr,
1900 struct kbd_state state;
1903 ret = kbd_get_state(&state);
1907 return sprintf(buf, "%d\n", state.als_setting);
1910 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1911 kbd_led_als_setting_show, kbd_led_als_setting_store);
1913 static struct attribute *kbd_led_attrs[] = {
1914 &dev_attr_stop_timeout.attr,
1915 &dev_attr_start_triggers.attr,
1919 static const struct attribute_group kbd_led_group = {
1920 .attrs = kbd_led_attrs,
1923 static struct attribute *kbd_led_als_attrs[] = {
1924 &dev_attr_als_enabled.attr,
1925 &dev_attr_als_setting.attr,
1929 static const struct attribute_group kbd_led_als_group = {
1930 .attrs = kbd_led_als_attrs,
1933 static const struct attribute_group *kbd_led_groups[] = {
1939 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1943 struct kbd_state state;
1945 if (kbd_get_max_level()) {
1946 ret = kbd_get_state(&state);
1949 ret = kbd_get_level(&state);
1955 if (kbd_get_valid_token_counts()) {
1956 ret = kbd_get_first_active_token_bit();
1959 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1960 num &= num - 1; /* clear the first bit set */
1963 return ffs(num) - 1;
1966 pr_warn("Keyboard brightness level control not supported\n");
1970 static void kbd_led_level_set(struct led_classdev *led_cdev,
1971 enum led_brightness value)
1973 struct kbd_state state;
1974 struct kbd_state new_state;
1977 if (kbd_get_max_level()) {
1978 if (kbd_get_state(&state))
1981 if (kbd_set_level(&new_state, value))
1983 kbd_set_state_safe(&new_state, &state);
1987 if (kbd_get_valid_token_counts()) {
1988 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1989 num &= num - 1; /* clear the first bit set */
1992 kbd_set_token_bit(ffs(num) - 1);
1996 pr_warn("Keyboard brightness level control not supported\n");
1999 static struct led_classdev kbd_led = {
2000 .name = "dell::kbd_backlight",
2001 .brightness_set = kbd_led_level_set,
2002 .brightness_get = kbd_led_level_get,
2003 .groups = kbd_led_groups,
2006 static int __init kbd_led_init(struct device *dev)
2009 if (!kbd_led_present)
2011 if (!kbd_als_supported)
2012 kbd_led_groups[1] = NULL;
2013 kbd_led.max_brightness = kbd_get_max_level();
2014 if (!kbd_led.max_brightness) {
2015 kbd_led.max_brightness = kbd_get_valid_token_counts();
2016 if (kbd_led.max_brightness)
2017 kbd_led.max_brightness--;
2019 return led_classdev_register(dev, &kbd_led);
2022 static void brightness_set_exit(struct led_classdev *led_cdev,
2023 enum led_brightness value)
2025 /* Don't change backlight level on exit */
2028 static void kbd_led_exit(void)
2030 if (!kbd_led_present)
2032 kbd_led.brightness_set = brightness_set_exit;
2033 led_classdev_unregister(&kbd_led);
2036 static int __init dell_init(void)
2038 int max_intensity = 0;
2041 if (!dmi_check_system(dell_device_table))
2045 /* find if this machine support other functions */
2046 dmi_check_system(dell_quirks);
2048 dmi_walk(find_tokens, NULL);
2051 pr_info("Unable to find dmi tokens\n");
2055 ret = platform_driver_register(&platform_driver);
2057 goto fail_platform_driver;
2058 platform_device = platform_device_alloc("dell-laptop", -1);
2059 if (!platform_device) {
2061 goto fail_platform_device1;
2063 ret = platform_device_add(platform_device);
2065 goto fail_platform_device2;
2068 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
2069 * is passed to SMI handler.
2071 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
2077 ret = dell_setup_rfkill();
2080 pr_warn("Unable to setup rfkill\n");
2084 if (quirks && quirks->touchpad_led)
2085 touchpad_led_init(&platform_device->dev);
2087 kbd_led_init(&platform_device->dev);
2089 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2090 if (dell_laptop_dir != NULL)
2091 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2092 &dell_debugfs_fops);
2094 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2098 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
2099 if (buffer->input[0] != -1) {
2100 dell_send_request(buffer, 0, 2);
2101 max_intensity = buffer->output[3];
2105 if (max_intensity) {
2106 struct backlight_properties props;
2107 memset(&props, 0, sizeof(struct backlight_properties));
2108 props.type = BACKLIGHT_PLATFORM;
2109 props.max_brightness = max_intensity;
2110 dell_backlight_device = backlight_device_register("dell_backlight",
2111 &platform_device->dev,
2116 if (IS_ERR(dell_backlight_device)) {
2117 ret = PTR_ERR(dell_backlight_device);
2118 dell_backlight_device = NULL;
2119 goto fail_backlight;
2122 dell_backlight_device->props.brightness =
2123 dell_get_intensity(dell_backlight_device);
2124 backlight_update_status(dell_backlight_device);
2130 dell_cleanup_rfkill();
2132 free_page((unsigned long)buffer);
2134 platform_device_del(platform_device);
2135 fail_platform_device2:
2136 platform_device_put(platform_device);
2137 fail_platform_device1:
2138 platform_driver_unregister(&platform_driver);
2139 fail_platform_driver:
2144 static void __exit dell_exit(void)
2146 debugfs_remove_recursive(dell_laptop_dir);
2147 if (quirks && quirks->touchpad_led)
2148 touchpad_led_exit();
2150 backlight_device_unregister(dell_backlight_device);
2151 dell_cleanup_rfkill();
2152 if (platform_device) {
2153 platform_device_unregister(platform_device);
2154 platform_driver_unregister(&platform_driver);
2157 free_page((unsigned long)buffer);
2160 /* dell-rbtn.c driver export functions which will not work correctly (and could
2161 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2162 * not problem when dell-rbtn.c is compiled as external module. When both files
2163 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2164 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2165 * This can be achieved by late_initcall() instead module_init().
2167 late_initcall(dell_init);
2168 module_exit(dell_exit);
2170 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2171 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2172 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2173 MODULE_DESCRIPTION("Dell laptop driver");
2174 MODULE_LICENSE("GPL");