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 "../../firmware/dcdbas.h"
35 #include "dell-rbtn.h"
37 #define BRIGHTNESS_TOKEN 0x7d
38 #define KBD_LED_OFF_TOKEN 0x01E1
39 #define KBD_LED_ON_TOKEN 0x01E2
40 #define KBD_LED_AUTO_TOKEN 0x01E3
41 #define KBD_LED_AUTO_25_TOKEN 0x02EA
42 #define KBD_LED_AUTO_50_TOKEN 0x02EB
43 #define KBD_LED_AUTO_75_TOKEN 0x02EC
44 #define KBD_LED_AUTO_100_TOKEN 0x02F6
46 /* This structure will be modified by the firmware when we enter
47 * system management mode, hence the volatiles */
49 struct calling_interface_buffer {
52 volatile u32 input[4];
53 volatile u32 output[4];
56 struct calling_interface_token {
65 struct calling_interface_structure {
66 struct dmi_header header;
70 struct calling_interface_token tokens[];
76 int needs_kbd_timeouts;
78 * Ordered list of timeouts expressed in seconds.
79 * The list must end with -1
84 static struct quirk_entry *quirks;
86 static struct quirk_entry quirk_dell_vostro_v130 = {
90 static int __init dmi_matched(const struct dmi_system_id *dmi)
92 quirks = dmi->driver_data;
97 * These values come from Windows utility provided by Dell. If any other value
98 * is used then BIOS silently set timeout to 0 without any error message.
100 static struct quirk_entry quirk_dell_xps13_9333 = {
101 .needs_kbd_timeouts = 1,
102 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
105 static int da_command_address;
106 static int da_command_code;
107 static int da_num_tokens;
108 static struct calling_interface_token *da_tokens;
110 static struct platform_driver platform_driver = {
112 .name = "dell-laptop",
116 static struct platform_device *platform_device;
117 static struct backlight_device *dell_backlight_device;
118 static struct rfkill *wifi_rfkill;
119 static struct rfkill *bluetooth_rfkill;
120 static struct rfkill *wwan_rfkill;
121 static bool force_rfkill;
123 module_param(force_rfkill, bool, 0444);
124 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
126 static const struct dmi_system_id dell_device_table[] __initconst = {
128 .ident = "Dell laptop",
130 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
131 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
137 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
141 .ident = "Dell Computer Corporation",
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
144 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
149 MODULE_DEVICE_TABLE(dmi, dell_device_table);
151 static const struct dmi_system_id dell_quirks[] __initconst = {
153 .callback = dmi_matched,
154 .ident = "Dell Vostro V130",
156 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
157 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
159 .driver_data = &quirk_dell_vostro_v130,
162 .callback = dmi_matched,
163 .ident = "Dell Vostro V131",
165 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
166 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
168 .driver_data = &quirk_dell_vostro_v130,
171 .callback = dmi_matched,
172 .ident = "Dell Vostro 3350",
174 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
175 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
177 .driver_data = &quirk_dell_vostro_v130,
180 .callback = dmi_matched,
181 .ident = "Dell Vostro 3555",
183 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
184 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
186 .driver_data = &quirk_dell_vostro_v130,
189 .callback = dmi_matched,
190 .ident = "Dell Inspiron N311z",
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
193 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
195 .driver_data = &quirk_dell_vostro_v130,
198 .callback = dmi_matched,
199 .ident = "Dell Inspiron M5110",
201 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
202 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
204 .driver_data = &quirk_dell_vostro_v130,
207 .callback = dmi_matched,
208 .ident = "Dell Vostro 3360",
210 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
211 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
213 .driver_data = &quirk_dell_vostro_v130,
216 .callback = dmi_matched,
217 .ident = "Dell Vostro 3460",
219 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
220 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
222 .driver_data = &quirk_dell_vostro_v130,
225 .callback = dmi_matched,
226 .ident = "Dell Vostro 3560",
228 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
229 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
231 .driver_data = &quirk_dell_vostro_v130,
234 .callback = dmi_matched,
235 .ident = "Dell Vostro 3450",
237 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
238 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
240 .driver_data = &quirk_dell_vostro_v130,
243 .callback = dmi_matched,
244 .ident = "Dell Inspiron 5420",
246 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
247 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
249 .driver_data = &quirk_dell_vostro_v130,
252 .callback = dmi_matched,
253 .ident = "Dell Inspiron 5520",
255 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
256 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
258 .driver_data = &quirk_dell_vostro_v130,
261 .callback = dmi_matched,
262 .ident = "Dell Inspiron 5720",
264 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
265 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
267 .driver_data = &quirk_dell_vostro_v130,
270 .callback = dmi_matched,
271 .ident = "Dell Inspiron 7420",
273 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
274 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
276 .driver_data = &quirk_dell_vostro_v130,
279 .callback = dmi_matched,
280 .ident = "Dell Inspiron 7520",
282 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
283 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
285 .driver_data = &quirk_dell_vostro_v130,
288 .callback = dmi_matched,
289 .ident = "Dell Inspiron 7720",
291 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
292 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
294 .driver_data = &quirk_dell_vostro_v130,
297 .callback = dmi_matched,
298 .ident = "Dell XPS13 9333",
300 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
301 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
303 .driver_data = &quirk_dell_xps13_9333,
308 static struct calling_interface_buffer *buffer;
309 static struct page *bufferpage;
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 */
426 /* Derived from information in DellWirelessCtl.cpp:
427 Class 17, select 11 is radio control. It returns an array of 32-bit values.
429 Input byte 0 = 0: Wireless information
431 result[0]: return code
433 Bit 0: Hardware switch supported
434 Bit 1: Wifi locator supported
435 Bit 2: Wifi is supported
436 Bit 3: Bluetooth is supported
437 Bit 4: WWAN is supported
438 Bit 5: Wireless keyboard supported
440 Bit 8: Wifi is installed
441 Bit 9: Bluetooth is installed
442 Bit 10: WWAN is installed
444 Bit 16: Hardware switch is on
445 Bit 17: Wifi is blocked
446 Bit 18: Bluetooth is blocked
447 Bit 19: WWAN is blocked
449 result[2]: NVRAM size in bytes
450 result[3]: NVRAM format version number
452 Input byte 0 = 2: Wireless switch configuration
453 result[0]: return code
455 Bit 0: Wifi controlled by switch
456 Bit 1: Bluetooth controlled by switch
457 Bit 2: WWAN controlled by switch
459 Bit 7: Wireless switch config locked
460 Bit 8: Wifi locator enabled
462 Bit 15: Wifi locator setting locked
466 static int dell_rfkill_set(void *data, bool blocked)
468 int disable = blocked ? 1 : 0;
469 unsigned long radio = (unsigned long)data;
470 int hwswitch_bit = (unsigned long)data - 1;
473 dell_send_request(buffer, 17, 11);
475 /* If the hardware switch controls this radio, and the hardware
476 switch is disabled, always disable the radio */
477 if ((hwswitch_state & BIT(hwswitch_bit)) &&
478 !(buffer->output[1] & BIT(16)))
481 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
482 dell_send_request(buffer, 17, 11);
488 /* Must be called with the buffer held */
489 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
492 if (status & BIT(0)) {
493 /* Has hw-switch, sync sw_state to BIOS */
494 int block = rfkill_blocked(rfkill);
495 buffer->input[0] = (1 | (radio << 8) | (block << 16));
496 dell_send_request(buffer, 17, 11);
498 /* No hw-switch, sync BIOS state to sw_state */
499 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
503 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
506 if (hwswitch_state & (BIT(radio - 1)))
507 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
510 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
515 dell_send_request(buffer, 17, 11);
516 status = buffer->output[1];
518 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
523 static const struct rfkill_ops dell_rfkill_ops = {
524 .set_block = dell_rfkill_set,
525 .query = dell_rfkill_query,
528 static struct dentry *dell_laptop_dir;
530 static int dell_debugfs_show(struct seq_file *s, void *data)
535 dell_send_request(buffer, 17, 11);
536 status = buffer->output[1];
539 seq_printf(s, "status:\t0x%X\n", status);
540 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
542 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
543 (status & BIT(1)) >> 1);
544 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
545 (status & BIT(2)) >> 2);
546 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
547 (status & BIT(3)) >> 3);
548 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
549 (status & BIT(4)) >> 4);
550 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
551 (status & BIT(5)) >> 5);
552 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
553 (status & BIT(8)) >> 8);
554 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
555 (status & BIT(9)) >> 9);
556 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
557 (status & BIT(10)) >> 10);
558 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
559 (status & BIT(16)) >> 16);
560 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
561 (status & BIT(17)) >> 17);
562 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
563 (status & BIT(18)) >> 18);
564 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
565 (status & BIT(19)) >> 19);
567 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
568 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
569 hwswitch_state & BIT(0));
570 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
571 (hwswitch_state & BIT(1)) >> 1);
572 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
573 (hwswitch_state & BIT(2)) >> 2);
574 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
575 (hwswitch_state & BIT(7)) >> 7);
576 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
577 (hwswitch_state & BIT(8)) >> 8);
578 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
579 (hwswitch_state & BIT(15)) >> 15);
584 static int dell_debugfs_open(struct inode *inode, struct file *file)
586 return single_open(file, dell_debugfs_show, inode->i_private);
589 static const struct file_operations dell_debugfs_fops = {
590 .owner = THIS_MODULE,
591 .open = dell_debugfs_open,
594 .release = single_release,
597 static void dell_update_rfkill(struct work_struct *ignored)
602 dell_send_request(buffer, 17, 11);
603 status = buffer->output[1];
606 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
607 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
609 if (bluetooth_rfkill) {
610 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
611 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
614 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
615 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
620 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
622 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
625 static bool extended;
627 if (str & I8042_STR_AUXDATA)
630 if (unlikely(data == 0xe0)) {
633 } else if (unlikely(extended)) {
636 schedule_delayed_work(&dell_rfkill_work,
637 round_jiffies_relative(HZ / 4));
646 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
647 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
649 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
650 unsigned long action, void *data)
652 schedule_delayed_work(&dell_rfkill_work, 0);
656 static struct notifier_block dell_laptop_rbtn_notifier = {
657 .notifier_call = dell_laptop_rbtn_notifier_call,
660 static int __init dell_setup_rfkill(void)
662 int status, ret, whitelisted;
666 * rfkill support causes trouble on various models, mostly Inspirons.
667 * So we whitelist certain series, and don't support rfkill on others.
670 product = dmi_get_system_info(DMI_PRODUCT_NAME);
671 if (product && (strncmp(product, "Latitude", 8) == 0 ||
672 strncmp(product, "Precision", 9) == 0))
674 if (!force_rfkill && !whitelisted)
678 dell_send_request(buffer, 17, 11);
679 status = buffer->output[1];
680 buffer->input[0] = 0x2;
681 dell_send_request(buffer, 17, 11);
682 hwswitch_state = buffer->output[1];
685 if (!(status & BIT(0))) {
687 /* No hwsitch, clear all hw-controlled bits */
688 hwswitch_state &= ~7;
690 /* rfkill is only tested on laptops with a hwswitch */
695 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
696 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
698 &dell_rfkill_ops, (void *) 1);
703 ret = rfkill_register(wifi_rfkill);
708 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
709 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
710 &platform_device->dev,
711 RFKILL_TYPE_BLUETOOTH,
712 &dell_rfkill_ops, (void *) 2);
713 if (!bluetooth_rfkill) {
717 ret = rfkill_register(bluetooth_rfkill);
722 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
723 wwan_rfkill = rfkill_alloc("dell-wwan",
724 &platform_device->dev,
726 &dell_rfkill_ops, (void *) 3);
731 ret = rfkill_register(wwan_rfkill);
737 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
738 * which can receive events from HW slider switch.
740 * Dell SMBIOS on whitelisted models supports controlling radio devices
741 * but does not support receiving HW button switch events. We can use
742 * i8042 filter hook function to receive keyboard data and handle
743 * keycode for HW button.
745 * So if it is possible we will use Dell Airplane Mode Switch ACPI
746 * driver for receiving HW events and Dell SMBIOS for setting rfkill
747 * states. If ACPI driver or device is not available we will fallback to
748 * i8042 filter hook function.
750 * To prevent duplicate rfkill devices which control and do same thing,
751 * dell-rbtn driver will automatically remove its own rfkill devices
752 * once function dell_rbtn_notifier_register() is called.
755 dell_rbtn_notifier_register_func =
756 symbol_request(dell_rbtn_notifier_register);
757 if (dell_rbtn_notifier_register_func) {
758 dell_rbtn_notifier_unregister_func =
759 symbol_request(dell_rbtn_notifier_unregister);
760 if (!dell_rbtn_notifier_unregister_func) {
761 symbol_put(dell_rbtn_notifier_register);
762 dell_rbtn_notifier_register_func = NULL;
766 if (dell_rbtn_notifier_register_func) {
767 ret = dell_rbtn_notifier_register_func(
768 &dell_laptop_rbtn_notifier);
769 symbol_put(dell_rbtn_notifier_register);
770 dell_rbtn_notifier_register_func = NULL;
772 symbol_put(dell_rbtn_notifier_unregister);
773 dell_rbtn_notifier_unregister_func = NULL;
776 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
781 pr_info("Using dell-rbtn acpi driver for receiving events\n");
782 } else if (ret != -ENODEV) {
783 pr_warn("Unable to register dell rbtn notifier\n");
786 ret = i8042_install_filter(dell_laptop_i8042_filter);
788 pr_warn("Unable to install key filter\n");
791 pr_info("Using i8042 filter function for receiving events\n");
797 rfkill_unregister(wwan_rfkill);
799 rfkill_destroy(wwan_rfkill);
800 if (bluetooth_rfkill)
801 rfkill_unregister(bluetooth_rfkill);
803 rfkill_destroy(bluetooth_rfkill);
805 rfkill_unregister(wifi_rfkill);
807 rfkill_destroy(wifi_rfkill);
812 static void dell_cleanup_rfkill(void)
814 if (dell_rbtn_notifier_unregister_func) {
815 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
816 symbol_put(dell_rbtn_notifier_unregister);
817 dell_rbtn_notifier_unregister_func = NULL;
819 i8042_remove_filter(dell_laptop_i8042_filter);
821 cancel_delayed_work_sync(&dell_rfkill_work);
823 rfkill_unregister(wifi_rfkill);
824 rfkill_destroy(wifi_rfkill);
826 if (bluetooth_rfkill) {
827 rfkill_unregister(bluetooth_rfkill);
828 rfkill_destroy(bluetooth_rfkill);
831 rfkill_unregister(wwan_rfkill);
832 rfkill_destroy(wwan_rfkill);
836 static int dell_send_intensity(struct backlight_device *bd)
841 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
842 buffer->input[1] = bd->props.brightness;
844 if (buffer->input[0] == -1) {
849 if (power_supply_is_system_supplied() > 0)
850 dell_send_request(buffer, 1, 2);
852 dell_send_request(buffer, 1, 1);
859 static int dell_get_intensity(struct backlight_device *bd)
864 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
866 if (buffer->input[0] == -1) {
871 if (power_supply_is_system_supplied() > 0)
872 dell_send_request(buffer, 0, 2);
874 dell_send_request(buffer, 0, 1);
876 ret = buffer->output[1];
883 static const struct backlight_ops dell_ops = {
884 .get_brightness = dell_get_intensity,
885 .update_status = dell_send_intensity,
888 static void touchpad_led_on(void)
892 i8042_command(&data, command | 1 << 12);
895 static void touchpad_led_off(void)
899 i8042_command(&data, command | 1 << 12);
902 static void touchpad_led_set(struct led_classdev *led_cdev,
903 enum led_brightness value)
911 static struct led_classdev touchpad_led = {
912 .name = "dell-laptop::touchpad",
913 .brightness_set = touchpad_led_set,
914 .flags = LED_CORE_SUSPENDRESUME,
917 static int __init touchpad_led_init(struct device *dev)
919 return led_classdev_register(dev, &touchpad_led);
922 static void touchpad_led_exit(void)
924 led_classdev_unregister(&touchpad_led);
928 * Derived from information in smbios-keyboard-ctl:
932 * Keyboard illumination
933 * cbArg1 determines the function to be performed
935 * cbArg1 0x0 = Get Feature Information
936 * cbRES1 Standard return codes (0, -1, -2)
937 * cbRES2, word0 Bitmap of user-selectable modes
938 * bit 0 Always off (All systems)
939 * bit 1 Always on (Travis ATG, Siberia)
940 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
941 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
942 * bit 4 Auto: Input-activity-based On; input-activity based Off
943 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
944 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
945 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
946 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
947 * bits 9-15 Reserved for future use
948 * cbRES2, byte2 Reserved for future use
949 * cbRES2, byte3 Keyboard illumination type
953 * 3-255 Reserved for future use
954 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
955 * bit 0 Any keystroke
956 * bit 1 Touchpad activity
957 * bit 2 Pointing stick
959 * bits 4-7 Reserved for future use
960 * cbRES3, byte1 Supported timeout unit bitmap
965 * bits 4-7 Reserved for future use
966 * cbRES3, byte2 Number of keyboard light brightness levels
967 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
968 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
969 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
970 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
972 * cbArg1 0x1 = Get Current State
973 * cbRES1 Standard return codes (0, -1, -2)
974 * cbRES2, word0 Bitmap of current mode state
975 * bit 0 Always off (All systems)
976 * bit 1 Always on (Travis ATG, Siberia)
977 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
978 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
979 * bit 4 Auto: Input-activity-based On; input-activity based Off
980 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
981 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
982 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
983 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
984 * bits 9-15 Reserved for future use
985 * Note: Only One bit can be set
986 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
987 * bit 0 Any keystroke
988 * bit 1 Touchpad activity
989 * bit 2 Pointing stick
991 * bits 4-7 Reserved for future use
992 * cbRES2, byte3 Current Timeout
993 * bits 7:6 Timeout units indicator:
998 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
999 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1000 * are set upon return from the [Get feature information] call.
1001 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1002 * cbRES3, byte1 Current ALS reading
1003 * cbRES3, byte2 Current keyboard light level.
1005 * cbArg1 0x2 = Set New State
1006 * cbRES1 Standard return codes (0, -1, -2)
1007 * cbArg2, word0 Bitmap of current mode state
1008 * bit 0 Always off (All systems)
1009 * bit 1 Always on (Travis ATG, Siberia)
1010 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1011 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1012 * bit 4 Auto: Input-activity-based On; input-activity based Off
1013 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1014 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1015 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1016 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1017 * bits 9-15 Reserved for future use
1018 * Note: Only One bit can be set
1019 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1020 * keyboard to turn off automatically.
1021 * bit 0 Any keystroke
1022 * bit 1 Touchpad activity
1023 * bit 2 Pointing stick
1025 * bits 4-7 Reserved for future use
1026 * cbArg2, byte3 Desired Timeout
1027 * bits 7:6 Timeout units indicator:
1032 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1033 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1034 * cbArg3, byte2 Desired keyboard light level.
1038 enum kbd_timeout_unit {
1039 KBD_TIMEOUT_SECONDS = 0,
1040 KBD_TIMEOUT_MINUTES,
1046 KBD_MODE_BIT_OFF = 0,
1049 KBD_MODE_BIT_TRIGGER_ALS,
1050 KBD_MODE_BIT_TRIGGER,
1051 KBD_MODE_BIT_TRIGGER_25,
1052 KBD_MODE_BIT_TRIGGER_50,
1053 KBD_MODE_BIT_TRIGGER_75,
1054 KBD_MODE_BIT_TRIGGER_100,
1057 #define kbd_is_als_mode_bit(bit) \
1058 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1059 #define kbd_is_trigger_mode_bit(bit) \
1060 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1061 #define kbd_is_level_mode_bit(bit) \
1062 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1085 static const int kbd_tokens[] = {
1087 KBD_LED_AUTO_25_TOKEN,
1088 KBD_LED_AUTO_50_TOKEN,
1089 KBD_LED_AUTO_75_TOKEN,
1090 KBD_LED_AUTO_100_TOKEN,
1094 static u16 kbd_token_bits;
1096 static struct kbd_info kbd_info;
1097 static bool kbd_als_supported;
1098 static bool kbd_triggers_supported;
1100 static u8 kbd_mode_levels[16];
1101 static int kbd_mode_levels_count;
1103 static u8 kbd_previous_level;
1104 static u8 kbd_previous_mode_bit;
1106 static bool kbd_led_present;
1109 * NOTE: there are three ways to set the keyboard backlight level.
1110 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1111 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1112 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1114 * There are laptops which support only one of these methods. If we want to
1115 * support as many machines as possible we need to implement all three methods.
1116 * The first two methods use the kbd_state structure. The third uses SMBIOS
1117 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1118 * keyboard backlight level via kbd_state.level.
1121 static int kbd_get_info(struct kbd_info *info)
1128 buffer->input[0] = 0x0;
1129 dell_send_request(buffer, 4, 11);
1130 ret = buffer->output[0];
1133 ret = dell_smi_error(ret);
1137 info->modes = buffer->output[1] & 0xFFFF;
1138 info->type = (buffer->output[1] >> 24) & 0xFF;
1139 info->triggers = buffer->output[2] & 0xFF;
1140 units = (buffer->output[2] >> 8) & 0xFF;
1141 info->levels = (buffer->output[2] >> 16) & 0xFF;
1144 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1146 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1148 info->hours = (buffer->output[3] >> 16) & 0xFF;
1150 info->days = (buffer->output[3] >> 24) & 0xFF;
1157 static unsigned int kbd_get_max_level(void)
1159 if (kbd_info.levels != 0)
1160 return kbd_info.levels;
1161 if (kbd_mode_levels_count > 0)
1162 return kbd_mode_levels_count - 1;
1166 static int kbd_get_level(struct kbd_state *state)
1170 if (kbd_info.levels != 0)
1171 return state->level;
1173 if (kbd_mode_levels_count > 0) {
1174 for (i = 0; i < kbd_mode_levels_count; ++i)
1175 if (kbd_mode_levels[i] == state->mode_bit)
1183 static int kbd_set_level(struct kbd_state *state, u8 level)
1185 if (kbd_info.levels != 0) {
1187 kbd_previous_level = level;
1188 if (state->level == level)
1190 state->level = level;
1191 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1192 state->mode_bit = kbd_previous_mode_bit;
1193 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1194 kbd_previous_mode_bit = state->mode_bit;
1195 state->mode_bit = KBD_MODE_BIT_OFF;
1200 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1202 kbd_previous_level = level;
1203 state->mode_bit = kbd_mode_levels[level];
1210 static int kbd_get_state(struct kbd_state *state)
1216 buffer->input[0] = 0x1;
1217 dell_send_request(buffer, 4, 11);
1218 ret = buffer->output[0];
1221 ret = dell_smi_error(ret);
1225 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1226 if (state->mode_bit != 0)
1229 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1230 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1231 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1232 state->als_setting = buffer->output[2] & 0xFF;
1233 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1234 state->level = (buffer->output[2] >> 16) & 0xFF;
1241 static int kbd_set_state(struct kbd_state *state)
1246 buffer->input[0] = 0x2;
1247 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1248 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1249 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1250 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1251 buffer->input[2] = state->als_setting & 0xFF;
1252 buffer->input[2] |= (state->level & 0xFF) << 16;
1253 dell_send_request(buffer, 4, 11);
1254 ret = buffer->output[0];
1257 return dell_smi_error(ret);
1260 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1264 ret = kbd_set_state(state);
1269 * When setting the new state fails,try to restore the previous one.
1270 * This is needed on some machines where BIOS sets a default state when
1271 * setting a new state fails. This default state could be all off.
1274 if (kbd_set_state(old))
1275 pr_err("Setting old previous keyboard state failed\n");
1280 static int kbd_set_token_bit(u8 bit)
1285 if (bit >= ARRAY_SIZE(kbd_tokens))
1288 id = find_token_id(kbd_tokens[bit]);
1293 buffer->input[0] = da_tokens[id].location;
1294 buffer->input[1] = da_tokens[id].value;
1295 dell_send_request(buffer, 1, 0);
1296 ret = buffer->output[0];
1299 return dell_smi_error(ret);
1302 static int kbd_get_token_bit(u8 bit)
1308 if (bit >= ARRAY_SIZE(kbd_tokens))
1311 id = find_token_id(kbd_tokens[bit]);
1316 buffer->input[0] = da_tokens[id].location;
1317 dell_send_request(buffer, 0, 0);
1318 ret = buffer->output[0];
1319 val = buffer->output[1];
1323 return dell_smi_error(ret);
1325 return (val == da_tokens[id].value);
1328 static int kbd_get_first_active_token_bit(void)
1333 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1334 ret = kbd_get_token_bit(i);
1342 static int kbd_get_valid_token_counts(void)
1344 return hweight16(kbd_token_bits);
1347 static inline int kbd_init_info(void)
1349 struct kbd_state state;
1353 ret = kbd_get_info(&kbd_info);
1357 kbd_get_state(&state);
1359 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1360 if (kbd_info.seconds > 63)
1361 kbd_info.seconds = 63;
1362 if (kbd_info.minutes > 63)
1363 kbd_info.minutes = 63;
1364 if (kbd_info.hours > 63)
1365 kbd_info.hours = 63;
1366 if (kbd_info.days > 63)
1369 /* NOTE: On tested machines ON mode did not work and caused
1370 * problems (turned backlight off) so do not use it
1372 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1374 kbd_previous_level = kbd_get_level(&state);
1375 kbd_previous_mode_bit = state.mode_bit;
1377 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1378 kbd_previous_level = 1;
1380 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1381 kbd_previous_mode_bit =
1382 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1383 if (kbd_previous_mode_bit != 0)
1384 kbd_previous_mode_bit--;
1387 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1388 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1389 kbd_als_supported = true;
1391 if (kbd_info.modes & (
1392 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1393 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1394 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1396 kbd_triggers_supported = true;
1398 /* kbd_mode_levels[0] is reserved, see below */
1399 for (i = 0; i < 16; ++i)
1400 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1401 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1404 * Find the first supported mode and assign to kbd_mode_levels[0].
1405 * This should be 0 (off), but we cannot depend on the BIOS to
1408 if (kbd_mode_levels_count > 0) {
1409 for (i = 0; i < 16; ++i) {
1410 if (BIT(i) & kbd_info.modes) {
1411 kbd_mode_levels[0] = i;
1415 kbd_mode_levels_count++;
1422 static inline void kbd_init_tokens(void)
1426 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1427 if (find_token_id(kbd_tokens[i]) != -1)
1428 kbd_token_bits |= BIT(i);
1431 static void kbd_init(void)
1435 ret = kbd_init_info();
1438 if (kbd_token_bits != 0 || ret == 0)
1439 kbd_led_present = true;
1442 static ssize_t kbd_led_timeout_store(struct device *dev,
1443 struct device_attribute *attr,
1444 const char *buf, size_t count)
1446 struct kbd_state new_state;
1447 struct kbd_state state;
1455 ret = sscanf(buf, "%d %c", &value, &ch);
1468 if (value > kbd_info.seconds)
1470 unit = KBD_TIMEOUT_SECONDS;
1473 if (value > kbd_info.minutes)
1475 unit = KBD_TIMEOUT_MINUTES;
1478 if (value > kbd_info.hours)
1480 unit = KBD_TIMEOUT_HOURS;
1483 if (value > kbd_info.days)
1485 unit = KBD_TIMEOUT_DAYS;
1491 if (quirks && quirks->needs_kbd_timeouts)
1495 /* Convert value from current units to seconds */
1497 case KBD_TIMEOUT_DAYS:
1499 case KBD_TIMEOUT_HOURS:
1501 case KBD_TIMEOUT_MINUTES:
1503 unit = KBD_TIMEOUT_SECONDS;
1506 if (quirks && quirks->needs_kbd_timeouts) {
1507 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1508 if (value <= quirks->kbd_timeouts[i]) {
1509 value = quirks->kbd_timeouts[i];
1515 if (value <= kbd_info.seconds && kbd_info.seconds) {
1516 unit = KBD_TIMEOUT_SECONDS;
1517 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1519 unit = KBD_TIMEOUT_MINUTES;
1520 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1522 unit = KBD_TIMEOUT_HOURS;
1523 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1524 value /= (60 * 60 * 24);
1525 unit = KBD_TIMEOUT_DAYS;
1531 ret = kbd_get_state(&state);
1536 new_state.timeout_value = value;
1537 new_state.timeout_unit = unit;
1539 ret = kbd_set_state_safe(&new_state, &state);
1546 static ssize_t kbd_led_timeout_show(struct device *dev,
1547 struct device_attribute *attr, char *buf)
1549 struct kbd_state state;
1553 ret = kbd_get_state(&state);
1557 len = sprintf(buf, "%d", state.timeout_value);
1559 switch (state.timeout_unit) {
1560 case KBD_TIMEOUT_SECONDS:
1561 return len + sprintf(buf+len, "s\n");
1562 case KBD_TIMEOUT_MINUTES:
1563 return len + sprintf(buf+len, "m\n");
1564 case KBD_TIMEOUT_HOURS:
1565 return len + sprintf(buf+len, "h\n");
1566 case KBD_TIMEOUT_DAYS:
1567 return len + sprintf(buf+len, "d\n");
1575 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1576 kbd_led_timeout_show, kbd_led_timeout_store);
1578 static const char * const kbd_led_triggers[] = {
1581 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1585 static ssize_t kbd_led_triggers_store(struct device *dev,
1586 struct device_attribute *attr,
1587 const char *buf, size_t count)
1589 struct kbd_state new_state;
1590 struct kbd_state state;
1591 bool triggers_enabled = false;
1592 int trigger_bit = -1;
1596 ret = sscanf(buf, "%20s", trigger);
1600 if (trigger[0] != '+' && trigger[0] != '-')
1603 ret = kbd_get_state(&state);
1607 if (kbd_triggers_supported)
1608 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1610 if (kbd_triggers_supported) {
1611 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1612 if (!(kbd_info.triggers & BIT(i)))
1614 if (!kbd_led_triggers[i])
1616 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1618 if (trigger[0] == '+' &&
1619 triggers_enabled && (state.triggers & BIT(i)))
1621 if (trigger[0] == '-' &&
1622 (!triggers_enabled || !(state.triggers & BIT(i))))
1629 if (trigger_bit != -1) {
1631 if (trigger[0] == '+')
1632 new_state.triggers |= BIT(trigger_bit);
1634 new_state.triggers &= ~BIT(trigger_bit);
1635 /* NOTE: trackstick bit (2) must be disabled when
1636 * disabling touchpad bit (1), otherwise touchpad
1637 * bit (1) will not be disabled */
1638 if (trigger_bit == 1)
1639 new_state.triggers &= ~BIT(2);
1641 if ((kbd_info.triggers & new_state.triggers) !=
1644 if (new_state.triggers && !triggers_enabled) {
1645 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1646 kbd_set_level(&new_state, kbd_previous_level);
1647 } else if (new_state.triggers == 0) {
1648 kbd_set_level(&new_state, 0);
1650 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1652 ret = kbd_set_state_safe(&new_state, &state);
1655 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1656 kbd_previous_mode_bit = new_state.mode_bit;
1663 static ssize_t kbd_led_triggers_show(struct device *dev,
1664 struct device_attribute *attr, char *buf)
1666 struct kbd_state state;
1667 bool triggers_enabled;
1671 ret = kbd_get_state(&state);
1677 if (kbd_triggers_supported) {
1678 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1679 level = kbd_get_level(&state);
1680 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1681 if (!(kbd_info.triggers & BIT(i)))
1683 if (!kbd_led_triggers[i])
1685 if ((triggers_enabled || level <= 0) &&
1686 (state.triggers & BIT(i)))
1690 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1695 buf[len - 1] = '\n';
1700 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1701 kbd_led_triggers_show, kbd_led_triggers_store);
1703 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1704 struct device_attribute *attr,
1705 const char *buf, size_t count)
1707 struct kbd_state new_state;
1708 struct kbd_state state;
1709 bool triggers_enabled = false;
1713 ret = kstrtoint(buf, 0, &enable);
1717 ret = kbd_get_state(&state);
1721 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1726 if (kbd_triggers_supported)
1727 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1730 if (triggers_enabled)
1731 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1733 new_state.mode_bit = KBD_MODE_BIT_ALS;
1735 if (triggers_enabled) {
1736 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1737 kbd_set_level(&new_state, kbd_previous_level);
1739 new_state.mode_bit = KBD_MODE_BIT_ON;
1742 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1745 ret = kbd_set_state_safe(&new_state, &state);
1748 kbd_previous_mode_bit = new_state.mode_bit;
1753 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1754 struct device_attribute *attr,
1757 struct kbd_state state;
1758 bool enabled = false;
1761 ret = kbd_get_state(&state);
1764 enabled = kbd_is_als_mode_bit(state.mode_bit);
1766 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1769 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1770 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1772 static ssize_t kbd_led_als_setting_store(struct device *dev,
1773 struct device_attribute *attr,
1774 const char *buf, size_t count)
1776 struct kbd_state state;
1777 struct kbd_state new_state;
1781 ret = kstrtou8(buf, 10, &setting);
1785 ret = kbd_get_state(&state);
1790 new_state.als_setting = setting;
1792 ret = kbd_set_state_safe(&new_state, &state);
1799 static ssize_t kbd_led_als_setting_show(struct device *dev,
1800 struct device_attribute *attr,
1803 struct kbd_state state;
1806 ret = kbd_get_state(&state);
1810 return sprintf(buf, "%d\n", state.als_setting);
1813 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1814 kbd_led_als_setting_show, kbd_led_als_setting_store);
1816 static struct attribute *kbd_led_attrs[] = {
1817 &dev_attr_stop_timeout.attr,
1818 &dev_attr_start_triggers.attr,
1822 static const struct attribute_group kbd_led_group = {
1823 .attrs = kbd_led_attrs,
1826 static struct attribute *kbd_led_als_attrs[] = {
1827 &dev_attr_als_enabled.attr,
1828 &dev_attr_als_setting.attr,
1832 static const struct attribute_group kbd_led_als_group = {
1833 .attrs = kbd_led_als_attrs,
1836 static const struct attribute_group *kbd_led_groups[] = {
1842 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1846 struct kbd_state state;
1848 if (kbd_get_max_level()) {
1849 ret = kbd_get_state(&state);
1852 ret = kbd_get_level(&state);
1858 if (kbd_get_valid_token_counts()) {
1859 ret = kbd_get_first_active_token_bit();
1862 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1863 num &= num - 1; /* clear the first bit set */
1866 return ffs(num) - 1;
1869 pr_warn("Keyboard brightness level control not supported\n");
1873 static void kbd_led_level_set(struct led_classdev *led_cdev,
1874 enum led_brightness value)
1876 struct kbd_state state;
1877 struct kbd_state new_state;
1880 if (kbd_get_max_level()) {
1881 if (kbd_get_state(&state))
1884 if (kbd_set_level(&new_state, value))
1886 kbd_set_state_safe(&new_state, &state);
1890 if (kbd_get_valid_token_counts()) {
1891 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1892 num &= num - 1; /* clear the first bit set */
1895 kbd_set_token_bit(ffs(num) - 1);
1899 pr_warn("Keyboard brightness level control not supported\n");
1902 static struct led_classdev kbd_led = {
1903 .name = "dell::kbd_backlight",
1904 .brightness_set = kbd_led_level_set,
1905 .brightness_get = kbd_led_level_get,
1906 .groups = kbd_led_groups,
1909 static int __init kbd_led_init(struct device *dev)
1912 if (!kbd_led_present)
1914 if (!kbd_als_supported)
1915 kbd_led_groups[1] = NULL;
1916 kbd_led.max_brightness = kbd_get_max_level();
1917 if (!kbd_led.max_brightness) {
1918 kbd_led.max_brightness = kbd_get_valid_token_counts();
1919 if (kbd_led.max_brightness)
1920 kbd_led.max_brightness--;
1922 return led_classdev_register(dev, &kbd_led);
1925 static void brightness_set_exit(struct led_classdev *led_cdev,
1926 enum led_brightness value)
1928 /* Don't change backlight level on exit */
1931 static void kbd_led_exit(void)
1933 if (!kbd_led_present)
1935 kbd_led.brightness_set = brightness_set_exit;
1936 led_classdev_unregister(&kbd_led);
1939 static int __init dell_init(void)
1941 int max_intensity = 0;
1944 if (!dmi_check_system(dell_device_table))
1948 /* find if this machine support other functions */
1949 dmi_check_system(dell_quirks);
1951 dmi_walk(find_tokens, NULL);
1954 pr_info("Unable to find dmi tokens\n");
1958 ret = platform_driver_register(&platform_driver);
1960 goto fail_platform_driver;
1961 platform_device = platform_device_alloc("dell-laptop", -1);
1962 if (!platform_device) {
1964 goto fail_platform_device1;
1966 ret = platform_device_add(platform_device);
1968 goto fail_platform_device2;
1971 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
1972 * is passed to SMI handler.
1974 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
1979 buffer = page_address(bufferpage);
1981 ret = dell_setup_rfkill();
1984 pr_warn("Unable to setup rfkill\n");
1988 if (quirks && quirks->touchpad_led)
1989 touchpad_led_init(&platform_device->dev);
1991 kbd_led_init(&platform_device->dev);
1993 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
1994 if (dell_laptop_dir != NULL)
1995 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
1996 &dell_debugfs_fops);
1999 /* In the event of an ACPI backlight being available, don't
2000 * register the platform controller.
2002 if (acpi_video_backlight_support())
2007 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
2008 if (buffer->input[0] != -1) {
2009 dell_send_request(buffer, 0, 2);
2010 max_intensity = buffer->output[3];
2014 if (max_intensity) {
2015 struct backlight_properties props;
2016 memset(&props, 0, sizeof(struct backlight_properties));
2017 props.type = BACKLIGHT_PLATFORM;
2018 props.max_brightness = max_intensity;
2019 dell_backlight_device = backlight_device_register("dell_backlight",
2020 &platform_device->dev,
2025 if (IS_ERR(dell_backlight_device)) {
2026 ret = PTR_ERR(dell_backlight_device);
2027 dell_backlight_device = NULL;
2028 goto fail_backlight;
2031 dell_backlight_device->props.brightness =
2032 dell_get_intensity(dell_backlight_device);
2033 backlight_update_status(dell_backlight_device);
2039 dell_cleanup_rfkill();
2041 free_page((unsigned long)bufferpage);
2043 platform_device_del(platform_device);
2044 fail_platform_device2:
2045 platform_device_put(platform_device);
2046 fail_platform_device1:
2047 platform_driver_unregister(&platform_driver);
2048 fail_platform_driver:
2053 static void __exit dell_exit(void)
2055 debugfs_remove_recursive(dell_laptop_dir);
2056 if (quirks && quirks->touchpad_led)
2057 touchpad_led_exit();
2059 backlight_device_unregister(dell_backlight_device);
2060 dell_cleanup_rfkill();
2061 if (platform_device) {
2062 platform_device_unregister(platform_device);
2063 platform_driver_unregister(&platform_driver);
2066 free_page((unsigned long)buffer);
2069 /* dell-rbtn.c driver export functions which will not work correctly (and could
2070 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2071 * not problem when dell-rbtn.c is compiled as external module. When both files
2072 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2073 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2074 * This can be achieved by late_initcall() instead module_init().
2076 late_initcall(dell_init);
2077 module_exit(dell_exit);
2079 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2080 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2081 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2082 MODULE_DESCRIPTION("Dell laptop driver");
2083 MODULE_LICENSE("GPL");