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 struct page *bufferpage;
311 static DEFINE_MUTEX(buffer_mutex);
313 static int hwswitch_state;
315 static void get_buffer(void)
317 mutex_lock(&buffer_mutex);
318 memset(buffer, 0, sizeof(struct calling_interface_buffer));
321 static void release_buffer(void)
323 mutex_unlock(&buffer_mutex);
326 static void __init parse_da_table(const struct dmi_header *dm)
328 /* Final token is a terminator, so we don't want to copy it */
329 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
330 struct calling_interface_token *new_da_tokens;
331 struct calling_interface_structure *table =
332 container_of(dm, struct calling_interface_structure, header);
334 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
340 da_command_address = table->cmdIOAddress;
341 da_command_code = table->cmdIOCode;
343 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
344 sizeof(struct calling_interface_token),
349 da_tokens = new_da_tokens;
351 memcpy(da_tokens+da_num_tokens, table->tokens,
352 sizeof(struct calling_interface_token) * tokens);
354 da_num_tokens += tokens;
357 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
360 case 0xd4: /* Indexed IO */
361 case 0xd5: /* Protected Area Type 1 */
362 case 0xd6: /* Protected Area Type 2 */
364 case 0xda: /* Calling interface */
370 static int find_token_id(int tokenid)
374 for (i = 0; i < da_num_tokens; i++) {
375 if (da_tokens[i].tokenID == tokenid)
382 static int find_token_location(int tokenid)
386 id = find_token_id(tokenid);
390 return da_tokens[id].location;
393 static struct calling_interface_buffer *
394 dell_send_request(struct calling_interface_buffer *buffer, int class,
397 struct smi_cmd command;
399 command.magic = SMI_CMD_MAGIC;
400 command.command_address = da_command_address;
401 command.command_code = da_command_code;
402 command.ebx = virt_to_phys(buffer);
403 command.ecx = 0x42534931;
405 buffer->class = class;
406 buffer->select = select;
408 dcdbas_smi_request(&command);
413 static inline int dell_smi_error(int value)
416 case 0: /* Completed successfully */
418 case -1: /* Completed with error */
420 case -2: /* Function not supported */
422 default: /* Unknown error */
427 /* Derived from information in DellWirelessCtl.cpp:
428 Class 17, select 11 is radio control. It returns an array of 32-bit values.
430 Input byte 0 = 0: Wireless information
432 result[0]: return code
434 Bit 0: Hardware switch supported
435 Bit 1: Wifi locator supported
436 Bit 2: Wifi is supported
437 Bit 3: Bluetooth is supported
438 Bit 4: WWAN is supported
439 Bit 5: Wireless keyboard supported
441 Bit 8: Wifi is installed
442 Bit 9: Bluetooth is installed
443 Bit 10: WWAN is installed
445 Bit 16: Hardware switch is on
446 Bit 17: Wifi is blocked
447 Bit 18: Bluetooth is blocked
448 Bit 19: WWAN is blocked
450 result[2]: NVRAM size in bytes
451 result[3]: NVRAM format version number
453 Input byte 0 = 2: Wireless switch configuration
454 result[0]: return code
456 Bit 0: Wifi controlled by switch
457 Bit 1: Bluetooth controlled by switch
458 Bit 2: WWAN controlled by switch
460 Bit 7: Wireless switch config locked
461 Bit 8: Wifi locator enabled
463 Bit 15: Wifi locator setting locked
467 static int dell_rfkill_set(void *data, bool blocked)
469 int disable = blocked ? 1 : 0;
470 unsigned long radio = (unsigned long)data;
471 int hwswitch_bit = (unsigned long)data - 1;
474 dell_send_request(buffer, 17, 11);
476 /* If the hardware switch controls this radio, and the hardware
477 switch is disabled, always disable the radio */
478 if ((hwswitch_state & BIT(hwswitch_bit)) &&
479 !(buffer->output[1] & BIT(16)))
482 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
483 dell_send_request(buffer, 17, 11);
489 /* Must be called with the buffer held */
490 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
493 if (status & BIT(0)) {
494 /* Has hw-switch, sync sw_state to BIOS */
495 int block = rfkill_blocked(rfkill);
496 buffer->input[0] = (1 | (radio << 8) | (block << 16));
497 dell_send_request(buffer, 17, 11);
499 /* No hw-switch, sync BIOS state to sw_state */
500 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
504 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
507 if (hwswitch_state & (BIT(radio - 1)))
508 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
511 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
516 dell_send_request(buffer, 17, 11);
517 status = buffer->output[1];
519 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
524 static const struct rfkill_ops dell_rfkill_ops = {
525 .set_block = dell_rfkill_set,
526 .query = dell_rfkill_query,
529 static struct dentry *dell_laptop_dir;
531 static int dell_debugfs_show(struct seq_file *s, void *data)
536 dell_send_request(buffer, 17, 11);
537 status = buffer->output[1];
540 seq_printf(s, "status:\t0x%X\n", status);
541 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
543 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
544 (status & BIT(1)) >> 1);
545 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
546 (status & BIT(2)) >> 2);
547 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
548 (status & BIT(3)) >> 3);
549 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
550 (status & BIT(4)) >> 4);
551 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
552 (status & BIT(5)) >> 5);
553 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
554 (status & BIT(8)) >> 8);
555 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
556 (status & BIT(9)) >> 9);
557 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
558 (status & BIT(10)) >> 10);
559 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
560 (status & BIT(16)) >> 16);
561 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
562 (status & BIT(17)) >> 17);
563 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
564 (status & BIT(18)) >> 18);
565 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
566 (status & BIT(19)) >> 19);
568 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
569 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
570 hwswitch_state & BIT(0));
571 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
572 (hwswitch_state & BIT(1)) >> 1);
573 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
574 (hwswitch_state & BIT(2)) >> 2);
575 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
576 (hwswitch_state & BIT(7)) >> 7);
577 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
578 (hwswitch_state & BIT(8)) >> 8);
579 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
580 (hwswitch_state & BIT(15)) >> 15);
585 static int dell_debugfs_open(struct inode *inode, struct file *file)
587 return single_open(file, dell_debugfs_show, inode->i_private);
590 static const struct file_operations dell_debugfs_fops = {
591 .owner = THIS_MODULE,
592 .open = dell_debugfs_open,
595 .release = single_release,
598 static void dell_update_rfkill(struct work_struct *ignored)
603 dell_send_request(buffer, 17, 11);
604 status = buffer->output[1];
607 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
608 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
610 if (bluetooth_rfkill) {
611 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
612 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
615 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
616 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
621 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
623 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
626 static bool extended;
628 if (str & I8042_STR_AUXDATA)
631 if (unlikely(data == 0xe0)) {
634 } else if (unlikely(extended)) {
637 schedule_delayed_work(&dell_rfkill_work,
638 round_jiffies_relative(HZ / 4));
647 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
648 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
650 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
651 unsigned long action, void *data)
653 schedule_delayed_work(&dell_rfkill_work, 0);
657 static struct notifier_block dell_laptop_rbtn_notifier = {
658 .notifier_call = dell_laptop_rbtn_notifier_call,
661 static int __init dell_setup_rfkill(void)
663 int status, ret, whitelisted;
667 * rfkill support causes trouble on various models, mostly Inspirons.
668 * So we whitelist certain series, and don't support rfkill on others.
671 product = dmi_get_system_info(DMI_PRODUCT_NAME);
672 if (product && (strncmp(product, "Latitude", 8) == 0 ||
673 strncmp(product, "Precision", 9) == 0))
675 if (!force_rfkill && !whitelisted)
679 dell_send_request(buffer, 17, 11);
680 status = buffer->output[1];
681 buffer->input[0] = 0x2;
682 dell_send_request(buffer, 17, 11);
683 hwswitch_state = buffer->output[1];
686 if (!(status & BIT(0))) {
688 /* No hwsitch, clear all hw-controlled bits */
689 hwswitch_state &= ~7;
691 /* rfkill is only tested on laptops with a hwswitch */
696 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
697 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
699 &dell_rfkill_ops, (void *) 1);
704 ret = rfkill_register(wifi_rfkill);
709 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
710 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
711 &platform_device->dev,
712 RFKILL_TYPE_BLUETOOTH,
713 &dell_rfkill_ops, (void *) 2);
714 if (!bluetooth_rfkill) {
718 ret = rfkill_register(bluetooth_rfkill);
723 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
724 wwan_rfkill = rfkill_alloc("dell-wwan",
725 &platform_device->dev,
727 &dell_rfkill_ops, (void *) 3);
732 ret = rfkill_register(wwan_rfkill);
738 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
739 * which can receive events from HW slider switch.
741 * Dell SMBIOS on whitelisted models supports controlling radio devices
742 * but does not support receiving HW button switch events. We can use
743 * i8042 filter hook function to receive keyboard data and handle
744 * keycode for HW button.
746 * So if it is possible we will use Dell Airplane Mode Switch ACPI
747 * driver for receiving HW events and Dell SMBIOS for setting rfkill
748 * states. If ACPI driver or device is not available we will fallback to
749 * i8042 filter hook function.
751 * To prevent duplicate rfkill devices which control and do same thing,
752 * dell-rbtn driver will automatically remove its own rfkill devices
753 * once function dell_rbtn_notifier_register() is called.
756 dell_rbtn_notifier_register_func =
757 symbol_request(dell_rbtn_notifier_register);
758 if (dell_rbtn_notifier_register_func) {
759 dell_rbtn_notifier_unregister_func =
760 symbol_request(dell_rbtn_notifier_unregister);
761 if (!dell_rbtn_notifier_unregister_func) {
762 symbol_put(dell_rbtn_notifier_register);
763 dell_rbtn_notifier_register_func = NULL;
767 if (dell_rbtn_notifier_register_func) {
768 ret = dell_rbtn_notifier_register_func(
769 &dell_laptop_rbtn_notifier);
770 symbol_put(dell_rbtn_notifier_register);
771 dell_rbtn_notifier_register_func = NULL;
773 symbol_put(dell_rbtn_notifier_unregister);
774 dell_rbtn_notifier_unregister_func = NULL;
777 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
782 pr_info("Using dell-rbtn acpi driver for receiving events\n");
783 } else if (ret != -ENODEV) {
784 pr_warn("Unable to register dell rbtn notifier\n");
787 ret = i8042_install_filter(dell_laptop_i8042_filter);
789 pr_warn("Unable to install key filter\n");
792 pr_info("Using i8042 filter function for receiving events\n");
798 rfkill_unregister(wwan_rfkill);
800 rfkill_destroy(wwan_rfkill);
801 if (bluetooth_rfkill)
802 rfkill_unregister(bluetooth_rfkill);
804 rfkill_destroy(bluetooth_rfkill);
806 rfkill_unregister(wifi_rfkill);
808 rfkill_destroy(wifi_rfkill);
813 static void dell_cleanup_rfkill(void)
815 if (dell_rbtn_notifier_unregister_func) {
816 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
817 symbol_put(dell_rbtn_notifier_unregister);
818 dell_rbtn_notifier_unregister_func = NULL;
820 i8042_remove_filter(dell_laptop_i8042_filter);
822 cancel_delayed_work_sync(&dell_rfkill_work);
824 rfkill_unregister(wifi_rfkill);
825 rfkill_destroy(wifi_rfkill);
827 if (bluetooth_rfkill) {
828 rfkill_unregister(bluetooth_rfkill);
829 rfkill_destroy(bluetooth_rfkill);
832 rfkill_unregister(wwan_rfkill);
833 rfkill_destroy(wwan_rfkill);
837 static int dell_send_intensity(struct backlight_device *bd)
842 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
843 buffer->input[1] = bd->props.brightness;
845 if (buffer->input[0] == -1) {
850 if (power_supply_is_system_supplied() > 0)
851 dell_send_request(buffer, 1, 2);
853 dell_send_request(buffer, 1, 1);
860 static int dell_get_intensity(struct backlight_device *bd)
865 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
867 if (buffer->input[0] == -1) {
872 if (power_supply_is_system_supplied() > 0)
873 dell_send_request(buffer, 0, 2);
875 dell_send_request(buffer, 0, 1);
877 ret = buffer->output[1];
884 static const struct backlight_ops dell_ops = {
885 .get_brightness = dell_get_intensity,
886 .update_status = dell_send_intensity,
889 static void touchpad_led_on(void)
893 i8042_command(&data, command | 1 << 12);
896 static void touchpad_led_off(void)
900 i8042_command(&data, command | 1 << 12);
903 static void touchpad_led_set(struct led_classdev *led_cdev,
904 enum led_brightness value)
912 static struct led_classdev touchpad_led = {
913 .name = "dell-laptop::touchpad",
914 .brightness_set = touchpad_led_set,
915 .flags = LED_CORE_SUSPENDRESUME,
918 static int __init touchpad_led_init(struct device *dev)
920 return led_classdev_register(dev, &touchpad_led);
923 static void touchpad_led_exit(void)
925 led_classdev_unregister(&touchpad_led);
929 * Derived from information in smbios-keyboard-ctl:
933 * Keyboard illumination
934 * cbArg1 determines the function to be performed
936 * cbArg1 0x0 = Get Feature Information
937 * cbRES1 Standard return codes (0, -1, -2)
938 * cbRES2, word0 Bitmap of user-selectable modes
939 * bit 0 Always off (All systems)
940 * bit 1 Always on (Travis ATG, Siberia)
941 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
942 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
943 * bit 4 Auto: Input-activity-based On; input-activity based Off
944 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
945 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
946 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
947 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
948 * bits 9-15 Reserved for future use
949 * cbRES2, byte2 Reserved for future use
950 * cbRES2, byte3 Keyboard illumination type
954 * 3-255 Reserved for future use
955 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
956 * bit 0 Any keystroke
957 * bit 1 Touchpad activity
958 * bit 2 Pointing stick
960 * bits 4-7 Reserved for future use
961 * cbRES3, byte1 Supported timeout unit bitmap
966 * bits 4-7 Reserved for future use
967 * cbRES3, byte2 Number of keyboard light brightness levels
968 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
969 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
970 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
971 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
973 * cbArg1 0x1 = Get Current State
974 * cbRES1 Standard return codes (0, -1, -2)
975 * cbRES2, word0 Bitmap of current mode state
976 * bit 0 Always off (All systems)
977 * bit 1 Always on (Travis ATG, Siberia)
978 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
979 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
980 * bit 4 Auto: Input-activity-based On; input-activity based Off
981 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
982 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
983 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
984 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
985 * bits 9-15 Reserved for future use
986 * Note: Only One bit can be set
987 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
988 * bit 0 Any keystroke
989 * bit 1 Touchpad activity
990 * bit 2 Pointing stick
992 * bits 4-7 Reserved for future use
993 * cbRES2, byte3 Current Timeout
994 * bits 7:6 Timeout units indicator:
999 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1000 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1001 * are set upon return from the [Get feature information] call.
1002 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1003 * cbRES3, byte1 Current ALS reading
1004 * cbRES3, byte2 Current keyboard light level.
1006 * cbArg1 0x2 = Set New State
1007 * cbRES1 Standard return codes (0, -1, -2)
1008 * cbArg2, word0 Bitmap of current mode state
1009 * bit 0 Always off (All systems)
1010 * bit 1 Always on (Travis ATG, Siberia)
1011 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1012 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1013 * bit 4 Auto: Input-activity-based On; input-activity based Off
1014 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1015 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1016 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1017 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1018 * bits 9-15 Reserved for future use
1019 * Note: Only One bit can be set
1020 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1021 * keyboard to turn off automatically.
1022 * bit 0 Any keystroke
1023 * bit 1 Touchpad activity
1024 * bit 2 Pointing stick
1026 * bits 4-7 Reserved for future use
1027 * cbArg2, byte3 Desired Timeout
1028 * bits 7:6 Timeout units indicator:
1033 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1034 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1035 * cbArg3, byte2 Desired keyboard light level.
1039 enum kbd_timeout_unit {
1040 KBD_TIMEOUT_SECONDS = 0,
1041 KBD_TIMEOUT_MINUTES,
1047 KBD_MODE_BIT_OFF = 0,
1050 KBD_MODE_BIT_TRIGGER_ALS,
1051 KBD_MODE_BIT_TRIGGER,
1052 KBD_MODE_BIT_TRIGGER_25,
1053 KBD_MODE_BIT_TRIGGER_50,
1054 KBD_MODE_BIT_TRIGGER_75,
1055 KBD_MODE_BIT_TRIGGER_100,
1058 #define kbd_is_als_mode_bit(bit) \
1059 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1060 #define kbd_is_trigger_mode_bit(bit) \
1061 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1062 #define kbd_is_level_mode_bit(bit) \
1063 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1086 static const int kbd_tokens[] = {
1088 KBD_LED_AUTO_25_TOKEN,
1089 KBD_LED_AUTO_50_TOKEN,
1090 KBD_LED_AUTO_75_TOKEN,
1091 KBD_LED_AUTO_100_TOKEN,
1095 static u16 kbd_token_bits;
1097 static struct kbd_info kbd_info;
1098 static bool kbd_als_supported;
1099 static bool kbd_triggers_supported;
1101 static u8 kbd_mode_levels[16];
1102 static int kbd_mode_levels_count;
1104 static u8 kbd_previous_level;
1105 static u8 kbd_previous_mode_bit;
1107 static bool kbd_led_present;
1110 * NOTE: there are three ways to set the keyboard backlight level.
1111 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1112 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1113 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1115 * There are laptops which support only one of these methods. If we want to
1116 * support as many machines as possible we need to implement all three methods.
1117 * The first two methods use the kbd_state structure. The third uses SMBIOS
1118 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1119 * keyboard backlight level via kbd_state.level.
1122 static int kbd_get_info(struct kbd_info *info)
1129 buffer->input[0] = 0x0;
1130 dell_send_request(buffer, 4, 11);
1131 ret = buffer->output[0];
1134 ret = dell_smi_error(ret);
1138 info->modes = buffer->output[1] & 0xFFFF;
1139 info->type = (buffer->output[1] >> 24) & 0xFF;
1140 info->triggers = buffer->output[2] & 0xFF;
1141 units = (buffer->output[2] >> 8) & 0xFF;
1142 info->levels = (buffer->output[2] >> 16) & 0xFF;
1145 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1147 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1149 info->hours = (buffer->output[3] >> 16) & 0xFF;
1151 info->days = (buffer->output[3] >> 24) & 0xFF;
1158 static unsigned int kbd_get_max_level(void)
1160 if (kbd_info.levels != 0)
1161 return kbd_info.levels;
1162 if (kbd_mode_levels_count > 0)
1163 return kbd_mode_levels_count - 1;
1167 static int kbd_get_level(struct kbd_state *state)
1171 if (kbd_info.levels != 0)
1172 return state->level;
1174 if (kbd_mode_levels_count > 0) {
1175 for (i = 0; i < kbd_mode_levels_count; ++i)
1176 if (kbd_mode_levels[i] == state->mode_bit)
1184 static int kbd_set_level(struct kbd_state *state, u8 level)
1186 if (kbd_info.levels != 0) {
1188 kbd_previous_level = level;
1189 if (state->level == level)
1191 state->level = level;
1192 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1193 state->mode_bit = kbd_previous_mode_bit;
1194 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1195 kbd_previous_mode_bit = state->mode_bit;
1196 state->mode_bit = KBD_MODE_BIT_OFF;
1201 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1203 kbd_previous_level = level;
1204 state->mode_bit = kbd_mode_levels[level];
1211 static int kbd_get_state(struct kbd_state *state)
1217 buffer->input[0] = 0x1;
1218 dell_send_request(buffer, 4, 11);
1219 ret = buffer->output[0];
1222 ret = dell_smi_error(ret);
1226 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1227 if (state->mode_bit != 0)
1230 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1231 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1232 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1233 state->als_setting = buffer->output[2] & 0xFF;
1234 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1235 state->level = (buffer->output[2] >> 16) & 0xFF;
1242 static int kbd_set_state(struct kbd_state *state)
1247 buffer->input[0] = 0x2;
1248 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1249 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1250 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1251 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1252 buffer->input[2] = state->als_setting & 0xFF;
1253 buffer->input[2] |= (state->level & 0xFF) << 16;
1254 dell_send_request(buffer, 4, 11);
1255 ret = buffer->output[0];
1258 return dell_smi_error(ret);
1261 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1265 ret = kbd_set_state(state);
1270 * When setting the new state fails,try to restore the previous one.
1271 * This is needed on some machines where BIOS sets a default state when
1272 * setting a new state fails. This default state could be all off.
1275 if (kbd_set_state(old))
1276 pr_err("Setting old previous keyboard state failed\n");
1281 static int kbd_set_token_bit(u8 bit)
1286 if (bit >= ARRAY_SIZE(kbd_tokens))
1289 id = find_token_id(kbd_tokens[bit]);
1294 buffer->input[0] = da_tokens[id].location;
1295 buffer->input[1] = da_tokens[id].value;
1296 dell_send_request(buffer, 1, 0);
1297 ret = buffer->output[0];
1300 return dell_smi_error(ret);
1303 static int kbd_get_token_bit(u8 bit)
1309 if (bit >= ARRAY_SIZE(kbd_tokens))
1312 id = find_token_id(kbd_tokens[bit]);
1317 buffer->input[0] = da_tokens[id].location;
1318 dell_send_request(buffer, 0, 0);
1319 ret = buffer->output[0];
1320 val = buffer->output[1];
1324 return dell_smi_error(ret);
1326 return (val == da_tokens[id].value);
1329 static int kbd_get_first_active_token_bit(void)
1334 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1335 ret = kbd_get_token_bit(i);
1343 static int kbd_get_valid_token_counts(void)
1345 return hweight16(kbd_token_bits);
1348 static inline int kbd_init_info(void)
1350 struct kbd_state state;
1354 ret = kbd_get_info(&kbd_info);
1358 kbd_get_state(&state);
1360 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1361 if (kbd_info.seconds > 63)
1362 kbd_info.seconds = 63;
1363 if (kbd_info.minutes > 63)
1364 kbd_info.minutes = 63;
1365 if (kbd_info.hours > 63)
1366 kbd_info.hours = 63;
1367 if (kbd_info.days > 63)
1370 /* NOTE: On tested machines ON mode did not work and caused
1371 * problems (turned backlight off) so do not use it
1373 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1375 kbd_previous_level = kbd_get_level(&state);
1376 kbd_previous_mode_bit = state.mode_bit;
1378 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1379 kbd_previous_level = 1;
1381 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1382 kbd_previous_mode_bit =
1383 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1384 if (kbd_previous_mode_bit != 0)
1385 kbd_previous_mode_bit--;
1388 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1389 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1390 kbd_als_supported = true;
1392 if (kbd_info.modes & (
1393 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1394 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1395 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1397 kbd_triggers_supported = true;
1399 /* kbd_mode_levels[0] is reserved, see below */
1400 for (i = 0; i < 16; ++i)
1401 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1402 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1405 * Find the first supported mode and assign to kbd_mode_levels[0].
1406 * This should be 0 (off), but we cannot depend on the BIOS to
1409 if (kbd_mode_levels_count > 0) {
1410 for (i = 0; i < 16; ++i) {
1411 if (BIT(i) & kbd_info.modes) {
1412 kbd_mode_levels[0] = i;
1416 kbd_mode_levels_count++;
1423 static inline void kbd_init_tokens(void)
1427 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1428 if (find_token_id(kbd_tokens[i]) != -1)
1429 kbd_token_bits |= BIT(i);
1432 static void kbd_init(void)
1436 ret = kbd_init_info();
1439 if (kbd_token_bits != 0 || ret == 0)
1440 kbd_led_present = true;
1443 static ssize_t kbd_led_timeout_store(struct device *dev,
1444 struct device_attribute *attr,
1445 const char *buf, size_t count)
1447 struct kbd_state new_state;
1448 struct kbd_state state;
1456 ret = sscanf(buf, "%d %c", &value, &ch);
1469 if (value > kbd_info.seconds)
1471 unit = KBD_TIMEOUT_SECONDS;
1474 if (value > kbd_info.minutes)
1476 unit = KBD_TIMEOUT_MINUTES;
1479 if (value > kbd_info.hours)
1481 unit = KBD_TIMEOUT_HOURS;
1484 if (value > kbd_info.days)
1486 unit = KBD_TIMEOUT_DAYS;
1492 if (quirks && quirks->needs_kbd_timeouts)
1496 /* Convert value from current units to seconds */
1498 case KBD_TIMEOUT_DAYS:
1500 case KBD_TIMEOUT_HOURS:
1502 case KBD_TIMEOUT_MINUTES:
1504 unit = KBD_TIMEOUT_SECONDS;
1507 if (quirks && quirks->needs_kbd_timeouts) {
1508 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1509 if (value <= quirks->kbd_timeouts[i]) {
1510 value = quirks->kbd_timeouts[i];
1516 if (value <= kbd_info.seconds && kbd_info.seconds) {
1517 unit = KBD_TIMEOUT_SECONDS;
1518 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1520 unit = KBD_TIMEOUT_MINUTES;
1521 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1523 unit = KBD_TIMEOUT_HOURS;
1524 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1525 value /= (60 * 60 * 24);
1526 unit = KBD_TIMEOUT_DAYS;
1532 ret = kbd_get_state(&state);
1537 new_state.timeout_value = value;
1538 new_state.timeout_unit = unit;
1540 ret = kbd_set_state_safe(&new_state, &state);
1547 static ssize_t kbd_led_timeout_show(struct device *dev,
1548 struct device_attribute *attr, char *buf)
1550 struct kbd_state state;
1554 ret = kbd_get_state(&state);
1558 len = sprintf(buf, "%d", state.timeout_value);
1560 switch (state.timeout_unit) {
1561 case KBD_TIMEOUT_SECONDS:
1562 return len + sprintf(buf+len, "s\n");
1563 case KBD_TIMEOUT_MINUTES:
1564 return len + sprintf(buf+len, "m\n");
1565 case KBD_TIMEOUT_HOURS:
1566 return len + sprintf(buf+len, "h\n");
1567 case KBD_TIMEOUT_DAYS:
1568 return len + sprintf(buf+len, "d\n");
1576 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1577 kbd_led_timeout_show, kbd_led_timeout_store);
1579 static const char * const kbd_led_triggers[] = {
1582 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1586 static ssize_t kbd_led_triggers_store(struct device *dev,
1587 struct device_attribute *attr,
1588 const char *buf, size_t count)
1590 struct kbd_state new_state;
1591 struct kbd_state state;
1592 bool triggers_enabled = false;
1593 int trigger_bit = -1;
1597 ret = sscanf(buf, "%20s", trigger);
1601 if (trigger[0] != '+' && trigger[0] != '-')
1604 ret = kbd_get_state(&state);
1608 if (kbd_triggers_supported)
1609 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1611 if (kbd_triggers_supported) {
1612 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1613 if (!(kbd_info.triggers & BIT(i)))
1615 if (!kbd_led_triggers[i])
1617 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1619 if (trigger[0] == '+' &&
1620 triggers_enabled && (state.triggers & BIT(i)))
1622 if (trigger[0] == '-' &&
1623 (!triggers_enabled || !(state.triggers & BIT(i))))
1630 if (trigger_bit != -1) {
1632 if (trigger[0] == '+')
1633 new_state.triggers |= BIT(trigger_bit);
1635 new_state.triggers &= ~BIT(trigger_bit);
1636 /* NOTE: trackstick bit (2) must be disabled when
1637 * disabling touchpad bit (1), otherwise touchpad
1638 * bit (1) will not be disabled */
1639 if (trigger_bit == 1)
1640 new_state.triggers &= ~BIT(2);
1642 if ((kbd_info.triggers & new_state.triggers) !=
1645 if (new_state.triggers && !triggers_enabled) {
1646 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1647 kbd_set_level(&new_state, kbd_previous_level);
1648 } else if (new_state.triggers == 0) {
1649 kbd_set_level(&new_state, 0);
1651 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1653 ret = kbd_set_state_safe(&new_state, &state);
1656 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1657 kbd_previous_mode_bit = new_state.mode_bit;
1664 static ssize_t kbd_led_triggers_show(struct device *dev,
1665 struct device_attribute *attr, char *buf)
1667 struct kbd_state state;
1668 bool triggers_enabled;
1672 ret = kbd_get_state(&state);
1678 if (kbd_triggers_supported) {
1679 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1680 level = kbd_get_level(&state);
1681 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1682 if (!(kbd_info.triggers & BIT(i)))
1684 if (!kbd_led_triggers[i])
1686 if ((triggers_enabled || level <= 0) &&
1687 (state.triggers & BIT(i)))
1691 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1696 buf[len - 1] = '\n';
1701 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1702 kbd_led_triggers_show, kbd_led_triggers_store);
1704 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1705 struct device_attribute *attr,
1706 const char *buf, size_t count)
1708 struct kbd_state new_state;
1709 struct kbd_state state;
1710 bool triggers_enabled = false;
1714 ret = kstrtoint(buf, 0, &enable);
1718 ret = kbd_get_state(&state);
1722 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1727 if (kbd_triggers_supported)
1728 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1731 if (triggers_enabled)
1732 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1734 new_state.mode_bit = KBD_MODE_BIT_ALS;
1736 if (triggers_enabled) {
1737 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1738 kbd_set_level(&new_state, kbd_previous_level);
1740 new_state.mode_bit = KBD_MODE_BIT_ON;
1743 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1746 ret = kbd_set_state_safe(&new_state, &state);
1749 kbd_previous_mode_bit = new_state.mode_bit;
1754 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1755 struct device_attribute *attr,
1758 struct kbd_state state;
1759 bool enabled = false;
1762 ret = kbd_get_state(&state);
1765 enabled = kbd_is_als_mode_bit(state.mode_bit);
1767 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1770 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1771 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1773 static ssize_t kbd_led_als_setting_store(struct device *dev,
1774 struct device_attribute *attr,
1775 const char *buf, size_t count)
1777 struct kbd_state state;
1778 struct kbd_state new_state;
1782 ret = kstrtou8(buf, 10, &setting);
1786 ret = kbd_get_state(&state);
1791 new_state.als_setting = setting;
1793 ret = kbd_set_state_safe(&new_state, &state);
1800 static ssize_t kbd_led_als_setting_show(struct device *dev,
1801 struct device_attribute *attr,
1804 struct kbd_state state;
1807 ret = kbd_get_state(&state);
1811 return sprintf(buf, "%d\n", state.als_setting);
1814 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1815 kbd_led_als_setting_show, kbd_led_als_setting_store);
1817 static struct attribute *kbd_led_attrs[] = {
1818 &dev_attr_stop_timeout.attr,
1819 &dev_attr_start_triggers.attr,
1823 static const struct attribute_group kbd_led_group = {
1824 .attrs = kbd_led_attrs,
1827 static struct attribute *kbd_led_als_attrs[] = {
1828 &dev_attr_als_enabled.attr,
1829 &dev_attr_als_setting.attr,
1833 static const struct attribute_group kbd_led_als_group = {
1834 .attrs = kbd_led_als_attrs,
1837 static const struct attribute_group *kbd_led_groups[] = {
1843 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1847 struct kbd_state state;
1849 if (kbd_get_max_level()) {
1850 ret = kbd_get_state(&state);
1853 ret = kbd_get_level(&state);
1859 if (kbd_get_valid_token_counts()) {
1860 ret = kbd_get_first_active_token_bit();
1863 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1864 num &= num - 1; /* clear the first bit set */
1867 return ffs(num) - 1;
1870 pr_warn("Keyboard brightness level control not supported\n");
1874 static void kbd_led_level_set(struct led_classdev *led_cdev,
1875 enum led_brightness value)
1877 struct kbd_state state;
1878 struct kbd_state new_state;
1881 if (kbd_get_max_level()) {
1882 if (kbd_get_state(&state))
1885 if (kbd_set_level(&new_state, value))
1887 kbd_set_state_safe(&new_state, &state);
1891 if (kbd_get_valid_token_counts()) {
1892 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1893 num &= num - 1; /* clear the first bit set */
1896 kbd_set_token_bit(ffs(num) - 1);
1900 pr_warn("Keyboard brightness level control not supported\n");
1903 static struct led_classdev kbd_led = {
1904 .name = "dell::kbd_backlight",
1905 .brightness_set = kbd_led_level_set,
1906 .brightness_get = kbd_led_level_get,
1907 .groups = kbd_led_groups,
1910 static int __init kbd_led_init(struct device *dev)
1913 if (!kbd_led_present)
1915 if (!kbd_als_supported)
1916 kbd_led_groups[1] = NULL;
1917 kbd_led.max_brightness = kbd_get_max_level();
1918 if (!kbd_led.max_brightness) {
1919 kbd_led.max_brightness = kbd_get_valid_token_counts();
1920 if (kbd_led.max_brightness)
1921 kbd_led.max_brightness--;
1923 return led_classdev_register(dev, &kbd_led);
1926 static void brightness_set_exit(struct led_classdev *led_cdev,
1927 enum led_brightness value)
1929 /* Don't change backlight level on exit */
1932 static void kbd_led_exit(void)
1934 if (!kbd_led_present)
1936 kbd_led.brightness_set = brightness_set_exit;
1937 led_classdev_unregister(&kbd_led);
1940 static int __init dell_init(void)
1942 int max_intensity = 0;
1945 if (!dmi_check_system(dell_device_table))
1949 /* find if this machine support other functions */
1950 dmi_check_system(dell_quirks);
1952 dmi_walk(find_tokens, NULL);
1955 pr_info("Unable to find dmi tokens\n");
1959 ret = platform_driver_register(&platform_driver);
1961 goto fail_platform_driver;
1962 platform_device = platform_device_alloc("dell-laptop", -1);
1963 if (!platform_device) {
1965 goto fail_platform_device1;
1967 ret = platform_device_add(platform_device);
1969 goto fail_platform_device2;
1972 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
1973 * is passed to SMI handler.
1975 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
1980 buffer = page_address(bufferpage);
1982 ret = dell_setup_rfkill();
1985 pr_warn("Unable to setup rfkill\n");
1989 if (quirks && quirks->touchpad_led)
1990 touchpad_led_init(&platform_device->dev);
1992 kbd_led_init(&platform_device->dev);
1994 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
1995 if (dell_laptop_dir != NULL)
1996 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
1997 &dell_debugfs_fops);
1999 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2003 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
2004 if (buffer->input[0] != -1) {
2005 dell_send_request(buffer, 0, 2);
2006 max_intensity = buffer->output[3];
2010 if (max_intensity) {
2011 struct backlight_properties props;
2012 memset(&props, 0, sizeof(struct backlight_properties));
2013 props.type = BACKLIGHT_PLATFORM;
2014 props.max_brightness = max_intensity;
2015 dell_backlight_device = backlight_device_register("dell_backlight",
2016 &platform_device->dev,
2021 if (IS_ERR(dell_backlight_device)) {
2022 ret = PTR_ERR(dell_backlight_device);
2023 dell_backlight_device = NULL;
2024 goto fail_backlight;
2027 dell_backlight_device->props.brightness =
2028 dell_get_intensity(dell_backlight_device);
2029 backlight_update_status(dell_backlight_device);
2035 dell_cleanup_rfkill();
2037 free_page((unsigned long)bufferpage);
2039 platform_device_del(platform_device);
2040 fail_platform_device2:
2041 platform_device_put(platform_device);
2042 fail_platform_device1:
2043 platform_driver_unregister(&platform_driver);
2044 fail_platform_driver:
2049 static void __exit dell_exit(void)
2051 debugfs_remove_recursive(dell_laptop_dir);
2052 if (quirks && quirks->touchpad_led)
2053 touchpad_led_exit();
2055 backlight_device_unregister(dell_backlight_device);
2056 dell_cleanup_rfkill();
2057 if (platform_device) {
2058 platform_device_unregister(platform_device);
2059 platform_driver_unregister(&platform_driver);
2062 free_page((unsigned long)buffer);
2065 /* dell-rbtn.c driver export functions which will not work correctly (and could
2066 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2067 * not problem when dell-rbtn.c is compiled as external module. When both files
2068 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2069 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2070 * This can be achieved by late_initcall() instead module_init().
2072 late_initcall(dell_init);
2073 module_exit(dell_exit);
2075 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2076 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2077 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2078 MODULE_DESCRIPTION("Dell laptop driver");
2079 MODULE_LICENSE("GPL");