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"
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 __init dell_setup_rfkill(void)
648 int status, ret, whitelisted;
652 * rfkill support causes trouble on various models, mostly Inspirons.
653 * So we whitelist certain series, and don't support rfkill on others.
656 product = dmi_get_system_info(DMI_PRODUCT_NAME);
657 if (product && (strncmp(product, "Latitude", 8) == 0 ||
658 strncmp(product, "Precision", 9) == 0))
660 if (!force_rfkill && !whitelisted)
664 dell_send_request(buffer, 17, 11);
665 status = buffer->output[1];
666 buffer->input[0] = 0x2;
667 dell_send_request(buffer, 17, 11);
668 hwswitch_state = buffer->output[1];
671 if (!(status & BIT(0))) {
673 /* No hwsitch, clear all hw-controlled bits */
674 hwswitch_state &= ~7;
676 /* rfkill is only tested on laptops with a hwswitch */
681 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
682 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
684 &dell_rfkill_ops, (void *) 1);
689 ret = rfkill_register(wifi_rfkill);
694 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
695 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
696 &platform_device->dev,
697 RFKILL_TYPE_BLUETOOTH,
698 &dell_rfkill_ops, (void *) 2);
699 if (!bluetooth_rfkill) {
703 ret = rfkill_register(bluetooth_rfkill);
708 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
709 wwan_rfkill = rfkill_alloc("dell-wwan",
710 &platform_device->dev,
712 &dell_rfkill_ops, (void *) 3);
717 ret = rfkill_register(wwan_rfkill);
722 ret = i8042_install_filter(dell_laptop_i8042_filter);
724 pr_warn("Unable to install key filter\n");
731 rfkill_unregister(wwan_rfkill);
733 rfkill_destroy(wwan_rfkill);
734 if (bluetooth_rfkill)
735 rfkill_unregister(bluetooth_rfkill);
737 rfkill_destroy(bluetooth_rfkill);
739 rfkill_unregister(wifi_rfkill);
741 rfkill_destroy(wifi_rfkill);
746 static void dell_cleanup_rfkill(void)
749 rfkill_unregister(wifi_rfkill);
750 rfkill_destroy(wifi_rfkill);
752 if (bluetooth_rfkill) {
753 rfkill_unregister(bluetooth_rfkill);
754 rfkill_destroy(bluetooth_rfkill);
757 rfkill_unregister(wwan_rfkill);
758 rfkill_destroy(wwan_rfkill);
762 static int dell_send_intensity(struct backlight_device *bd)
767 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
768 buffer->input[1] = bd->props.brightness;
770 if (buffer->input[0] == -1) {
775 if (power_supply_is_system_supplied() > 0)
776 dell_send_request(buffer, 1, 2);
778 dell_send_request(buffer, 1, 1);
785 static int dell_get_intensity(struct backlight_device *bd)
790 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
792 if (buffer->input[0] == -1) {
797 if (power_supply_is_system_supplied() > 0)
798 dell_send_request(buffer, 0, 2);
800 dell_send_request(buffer, 0, 1);
802 ret = buffer->output[1];
809 static const struct backlight_ops dell_ops = {
810 .get_brightness = dell_get_intensity,
811 .update_status = dell_send_intensity,
814 static void touchpad_led_on(void)
818 i8042_command(&data, command | 1 << 12);
821 static void touchpad_led_off(void)
825 i8042_command(&data, command | 1 << 12);
828 static void touchpad_led_set(struct led_classdev *led_cdev,
829 enum led_brightness value)
837 static struct led_classdev touchpad_led = {
838 .name = "dell-laptop::touchpad",
839 .brightness_set = touchpad_led_set,
840 .flags = LED_CORE_SUSPENDRESUME,
843 static int __init touchpad_led_init(struct device *dev)
845 return led_classdev_register(dev, &touchpad_led);
848 static void touchpad_led_exit(void)
850 led_classdev_unregister(&touchpad_led);
854 * Derived from information in smbios-keyboard-ctl:
858 * Keyboard illumination
859 * cbArg1 determines the function to be performed
861 * cbArg1 0x0 = Get Feature Information
862 * cbRES1 Standard return codes (0, -1, -2)
863 * cbRES2, word0 Bitmap of user-selectable modes
864 * bit 0 Always off (All systems)
865 * bit 1 Always on (Travis ATG, Siberia)
866 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
867 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
868 * bit 4 Auto: Input-activity-based On; input-activity based Off
869 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
870 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
871 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
872 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
873 * bits 9-15 Reserved for future use
874 * cbRES2, byte2 Reserved for future use
875 * cbRES2, byte3 Keyboard illumination type
879 * 3-255 Reserved for future use
880 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
881 * bit 0 Any keystroke
882 * bit 1 Touchpad activity
883 * bit 2 Pointing stick
885 * bits 4-7 Reserved for future use
886 * cbRES3, byte1 Supported timeout unit bitmap
891 * bits 4-7 Reserved for future use
892 * cbRES3, byte2 Number of keyboard light brightness levels
893 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
894 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
895 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
896 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
898 * cbArg1 0x1 = Get Current State
899 * cbRES1 Standard return codes (0, -1, -2)
900 * cbRES2, word0 Bitmap of current mode state
901 * bit 0 Always off (All systems)
902 * bit 1 Always on (Travis ATG, Siberia)
903 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
904 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
905 * bit 4 Auto: Input-activity-based On; input-activity based Off
906 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
907 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
908 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
909 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
910 * bits 9-15 Reserved for future use
911 * Note: Only One bit can be set
912 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
913 * bit 0 Any keystroke
914 * bit 1 Touchpad activity
915 * bit 2 Pointing stick
917 * bits 4-7 Reserved for future use
918 * cbRES2, byte3 Current Timeout
919 * bits 7:6 Timeout units indicator:
924 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
925 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
926 * are set upon return from the [Get feature information] call.
927 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
928 * cbRES3, byte1 Current ALS reading
929 * cbRES3, byte2 Current keyboard light level.
931 * cbArg1 0x2 = Set New State
932 * cbRES1 Standard return codes (0, -1, -2)
933 * cbArg2, word0 Bitmap of current mode state
934 * bit 0 Always off (All systems)
935 * bit 1 Always on (Travis ATG, Siberia)
936 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
937 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
938 * bit 4 Auto: Input-activity-based On; input-activity based Off
939 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
940 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
941 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
942 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
943 * bits 9-15 Reserved for future use
944 * Note: Only One bit can be set
945 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
946 * keyboard to turn off automatically.
947 * bit 0 Any keystroke
948 * bit 1 Touchpad activity
949 * bit 2 Pointing stick
951 * bits 4-7 Reserved for future use
952 * cbArg2, byte3 Desired Timeout
953 * bits 7:6 Timeout units indicator:
958 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
959 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
960 * cbArg3, byte2 Desired keyboard light level.
964 enum kbd_timeout_unit {
965 KBD_TIMEOUT_SECONDS = 0,
972 KBD_MODE_BIT_OFF = 0,
975 KBD_MODE_BIT_TRIGGER_ALS,
976 KBD_MODE_BIT_TRIGGER,
977 KBD_MODE_BIT_TRIGGER_25,
978 KBD_MODE_BIT_TRIGGER_50,
979 KBD_MODE_BIT_TRIGGER_75,
980 KBD_MODE_BIT_TRIGGER_100,
983 #define kbd_is_als_mode_bit(bit) \
984 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
985 #define kbd_is_trigger_mode_bit(bit) \
986 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
987 #define kbd_is_level_mode_bit(bit) \
988 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1011 static const int kbd_tokens[] = {
1013 KBD_LED_AUTO_25_TOKEN,
1014 KBD_LED_AUTO_50_TOKEN,
1015 KBD_LED_AUTO_75_TOKEN,
1016 KBD_LED_AUTO_100_TOKEN,
1020 static u16 kbd_token_bits;
1022 static struct kbd_info kbd_info;
1023 static bool kbd_als_supported;
1024 static bool kbd_triggers_supported;
1026 static u8 kbd_mode_levels[16];
1027 static int kbd_mode_levels_count;
1029 static u8 kbd_previous_level;
1030 static u8 kbd_previous_mode_bit;
1032 static bool kbd_led_present;
1035 * NOTE: there are three ways to set the keyboard backlight level.
1036 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1037 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1038 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1040 * There are laptops which support only one of these methods. If we want to
1041 * support as many machines as possible we need to implement all three methods.
1042 * The first two methods use the kbd_state structure. The third uses SMBIOS
1043 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1044 * keyboard backlight level via kbd_state.level.
1047 static int kbd_get_info(struct kbd_info *info)
1054 buffer->input[0] = 0x0;
1055 dell_send_request(buffer, 4, 11);
1056 ret = buffer->output[0];
1059 ret = dell_smi_error(ret);
1063 info->modes = buffer->output[1] & 0xFFFF;
1064 info->type = (buffer->output[1] >> 24) & 0xFF;
1065 info->triggers = buffer->output[2] & 0xFF;
1066 units = (buffer->output[2] >> 8) & 0xFF;
1067 info->levels = (buffer->output[2] >> 16) & 0xFF;
1070 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1072 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1074 info->hours = (buffer->output[3] >> 16) & 0xFF;
1076 info->days = (buffer->output[3] >> 24) & 0xFF;
1083 static unsigned int kbd_get_max_level(void)
1085 if (kbd_info.levels != 0)
1086 return kbd_info.levels;
1087 if (kbd_mode_levels_count > 0)
1088 return kbd_mode_levels_count - 1;
1092 static int kbd_get_level(struct kbd_state *state)
1096 if (kbd_info.levels != 0)
1097 return state->level;
1099 if (kbd_mode_levels_count > 0) {
1100 for (i = 0; i < kbd_mode_levels_count; ++i)
1101 if (kbd_mode_levels[i] == state->mode_bit)
1109 static int kbd_set_level(struct kbd_state *state, u8 level)
1111 if (kbd_info.levels != 0) {
1113 kbd_previous_level = level;
1114 if (state->level == level)
1116 state->level = level;
1117 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1118 state->mode_bit = kbd_previous_mode_bit;
1119 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1120 kbd_previous_mode_bit = state->mode_bit;
1121 state->mode_bit = KBD_MODE_BIT_OFF;
1126 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1128 kbd_previous_level = level;
1129 state->mode_bit = kbd_mode_levels[level];
1136 static int kbd_get_state(struct kbd_state *state)
1142 buffer->input[0] = 0x1;
1143 dell_send_request(buffer, 4, 11);
1144 ret = buffer->output[0];
1147 ret = dell_smi_error(ret);
1151 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1152 if (state->mode_bit != 0)
1155 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1156 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1157 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1158 state->als_setting = buffer->output[2] & 0xFF;
1159 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1160 state->level = (buffer->output[2] >> 16) & 0xFF;
1167 static int kbd_set_state(struct kbd_state *state)
1172 buffer->input[0] = 0x2;
1173 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1174 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1175 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1176 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1177 buffer->input[2] = state->als_setting & 0xFF;
1178 buffer->input[2] |= (state->level & 0xFF) << 16;
1179 dell_send_request(buffer, 4, 11);
1180 ret = buffer->output[0];
1183 return dell_smi_error(ret);
1186 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1190 ret = kbd_set_state(state);
1195 * When setting the new state fails,try to restore the previous one.
1196 * This is needed on some machines where BIOS sets a default state when
1197 * setting a new state fails. This default state could be all off.
1200 if (kbd_set_state(old))
1201 pr_err("Setting old previous keyboard state failed\n");
1206 static int kbd_set_token_bit(u8 bit)
1211 if (bit >= ARRAY_SIZE(kbd_tokens))
1214 id = find_token_id(kbd_tokens[bit]);
1219 buffer->input[0] = da_tokens[id].location;
1220 buffer->input[1] = da_tokens[id].value;
1221 dell_send_request(buffer, 1, 0);
1222 ret = buffer->output[0];
1225 return dell_smi_error(ret);
1228 static int kbd_get_token_bit(u8 bit)
1234 if (bit >= ARRAY_SIZE(kbd_tokens))
1237 id = find_token_id(kbd_tokens[bit]);
1242 buffer->input[0] = da_tokens[id].location;
1243 dell_send_request(buffer, 0, 0);
1244 ret = buffer->output[0];
1245 val = buffer->output[1];
1249 return dell_smi_error(ret);
1251 return (val == da_tokens[id].value);
1254 static int kbd_get_first_active_token_bit(void)
1259 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1260 ret = kbd_get_token_bit(i);
1268 static int kbd_get_valid_token_counts(void)
1270 return hweight16(kbd_token_bits);
1273 static inline int kbd_init_info(void)
1275 struct kbd_state state;
1279 ret = kbd_get_info(&kbd_info);
1283 kbd_get_state(&state);
1285 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1286 if (kbd_info.seconds > 63)
1287 kbd_info.seconds = 63;
1288 if (kbd_info.minutes > 63)
1289 kbd_info.minutes = 63;
1290 if (kbd_info.hours > 63)
1291 kbd_info.hours = 63;
1292 if (kbd_info.days > 63)
1295 /* NOTE: On tested machines ON mode did not work and caused
1296 * problems (turned backlight off) so do not use it
1298 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1300 kbd_previous_level = kbd_get_level(&state);
1301 kbd_previous_mode_bit = state.mode_bit;
1303 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1304 kbd_previous_level = 1;
1306 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1307 kbd_previous_mode_bit =
1308 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1309 if (kbd_previous_mode_bit != 0)
1310 kbd_previous_mode_bit--;
1313 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1314 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1315 kbd_als_supported = true;
1317 if (kbd_info.modes & (
1318 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1319 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1320 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1322 kbd_triggers_supported = true;
1324 /* kbd_mode_levels[0] is reserved, see below */
1325 for (i = 0; i < 16; ++i)
1326 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1327 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1330 * Find the first supported mode and assign to kbd_mode_levels[0].
1331 * This should be 0 (off), but we cannot depend on the BIOS to
1334 if (kbd_mode_levels_count > 0) {
1335 for (i = 0; i < 16; ++i) {
1336 if (BIT(i) & kbd_info.modes) {
1337 kbd_mode_levels[0] = i;
1341 kbd_mode_levels_count++;
1348 static inline void kbd_init_tokens(void)
1352 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1353 if (find_token_id(kbd_tokens[i]) != -1)
1354 kbd_token_bits |= BIT(i);
1357 static void kbd_init(void)
1361 ret = kbd_init_info();
1364 if (kbd_token_bits != 0 || ret == 0)
1365 kbd_led_present = true;
1368 static ssize_t kbd_led_timeout_store(struct device *dev,
1369 struct device_attribute *attr,
1370 const char *buf, size_t count)
1372 struct kbd_state new_state;
1373 struct kbd_state state;
1381 ret = sscanf(buf, "%d %c", &value, &ch);
1394 if (value > kbd_info.seconds)
1396 unit = KBD_TIMEOUT_SECONDS;
1399 if (value > kbd_info.minutes)
1401 unit = KBD_TIMEOUT_MINUTES;
1404 if (value > kbd_info.hours)
1406 unit = KBD_TIMEOUT_HOURS;
1409 if (value > kbd_info.days)
1411 unit = KBD_TIMEOUT_DAYS;
1417 if (quirks && quirks->needs_kbd_timeouts)
1421 /* Convert value from current units to seconds */
1423 case KBD_TIMEOUT_DAYS:
1425 case KBD_TIMEOUT_HOURS:
1427 case KBD_TIMEOUT_MINUTES:
1429 unit = KBD_TIMEOUT_SECONDS;
1432 if (quirks && quirks->needs_kbd_timeouts) {
1433 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1434 if (value <= quirks->kbd_timeouts[i]) {
1435 value = quirks->kbd_timeouts[i];
1441 if (value <= kbd_info.seconds && kbd_info.seconds) {
1442 unit = KBD_TIMEOUT_SECONDS;
1443 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1445 unit = KBD_TIMEOUT_MINUTES;
1446 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1448 unit = KBD_TIMEOUT_HOURS;
1449 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1450 value /= (60 * 60 * 24);
1451 unit = KBD_TIMEOUT_DAYS;
1457 ret = kbd_get_state(&state);
1462 new_state.timeout_value = value;
1463 new_state.timeout_unit = unit;
1465 ret = kbd_set_state_safe(&new_state, &state);
1472 static ssize_t kbd_led_timeout_show(struct device *dev,
1473 struct device_attribute *attr, char *buf)
1475 struct kbd_state state;
1479 ret = kbd_get_state(&state);
1483 len = sprintf(buf, "%d", state.timeout_value);
1485 switch (state.timeout_unit) {
1486 case KBD_TIMEOUT_SECONDS:
1487 return len + sprintf(buf+len, "s\n");
1488 case KBD_TIMEOUT_MINUTES:
1489 return len + sprintf(buf+len, "m\n");
1490 case KBD_TIMEOUT_HOURS:
1491 return len + sprintf(buf+len, "h\n");
1492 case KBD_TIMEOUT_DAYS:
1493 return len + sprintf(buf+len, "d\n");
1501 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1502 kbd_led_timeout_show, kbd_led_timeout_store);
1504 static const char * const kbd_led_triggers[] = {
1507 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1511 static ssize_t kbd_led_triggers_store(struct device *dev,
1512 struct device_attribute *attr,
1513 const char *buf, size_t count)
1515 struct kbd_state new_state;
1516 struct kbd_state state;
1517 bool triggers_enabled = false;
1518 int trigger_bit = -1;
1522 ret = sscanf(buf, "%20s", trigger);
1526 if (trigger[0] != '+' && trigger[0] != '-')
1529 ret = kbd_get_state(&state);
1533 if (kbd_triggers_supported)
1534 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1536 if (kbd_triggers_supported) {
1537 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1538 if (!(kbd_info.triggers & BIT(i)))
1540 if (!kbd_led_triggers[i])
1542 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1544 if (trigger[0] == '+' &&
1545 triggers_enabled && (state.triggers & BIT(i)))
1547 if (trigger[0] == '-' &&
1548 (!triggers_enabled || !(state.triggers & BIT(i))))
1555 if (trigger_bit != -1) {
1557 if (trigger[0] == '+')
1558 new_state.triggers |= BIT(trigger_bit);
1560 new_state.triggers &= ~BIT(trigger_bit);
1561 /* NOTE: trackstick bit (2) must be disabled when
1562 * disabling touchpad bit (1), otherwise touchpad
1563 * bit (1) will not be disabled */
1564 if (trigger_bit == 1)
1565 new_state.triggers &= ~BIT(2);
1567 if ((kbd_info.triggers & new_state.triggers) !=
1570 if (new_state.triggers && !triggers_enabled) {
1571 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1572 kbd_set_level(&new_state, kbd_previous_level);
1573 } else if (new_state.triggers == 0) {
1574 kbd_set_level(&new_state, 0);
1576 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1578 ret = kbd_set_state_safe(&new_state, &state);
1581 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1582 kbd_previous_mode_bit = new_state.mode_bit;
1589 static ssize_t kbd_led_triggers_show(struct device *dev,
1590 struct device_attribute *attr, char *buf)
1592 struct kbd_state state;
1593 bool triggers_enabled;
1597 ret = kbd_get_state(&state);
1603 if (kbd_triggers_supported) {
1604 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1605 level = kbd_get_level(&state);
1606 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1607 if (!(kbd_info.triggers & BIT(i)))
1609 if (!kbd_led_triggers[i])
1611 if ((triggers_enabled || level <= 0) &&
1612 (state.triggers & BIT(i)))
1616 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1621 buf[len - 1] = '\n';
1626 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1627 kbd_led_triggers_show, kbd_led_triggers_store);
1629 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1630 struct device_attribute *attr,
1631 const char *buf, size_t count)
1633 struct kbd_state new_state;
1634 struct kbd_state state;
1635 bool triggers_enabled = false;
1639 ret = kstrtoint(buf, 0, &enable);
1643 ret = kbd_get_state(&state);
1647 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1652 if (kbd_triggers_supported)
1653 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1656 if (triggers_enabled)
1657 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1659 new_state.mode_bit = KBD_MODE_BIT_ALS;
1661 if (triggers_enabled) {
1662 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1663 kbd_set_level(&new_state, kbd_previous_level);
1665 new_state.mode_bit = KBD_MODE_BIT_ON;
1668 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1671 ret = kbd_set_state_safe(&new_state, &state);
1674 kbd_previous_mode_bit = new_state.mode_bit;
1679 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1680 struct device_attribute *attr,
1683 struct kbd_state state;
1684 bool enabled = false;
1687 ret = kbd_get_state(&state);
1690 enabled = kbd_is_als_mode_bit(state.mode_bit);
1692 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1695 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1696 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1698 static ssize_t kbd_led_als_setting_store(struct device *dev,
1699 struct device_attribute *attr,
1700 const char *buf, size_t count)
1702 struct kbd_state state;
1703 struct kbd_state new_state;
1707 ret = kstrtou8(buf, 10, &setting);
1711 ret = kbd_get_state(&state);
1716 new_state.als_setting = setting;
1718 ret = kbd_set_state_safe(&new_state, &state);
1725 static ssize_t kbd_led_als_setting_show(struct device *dev,
1726 struct device_attribute *attr,
1729 struct kbd_state state;
1732 ret = kbd_get_state(&state);
1736 return sprintf(buf, "%d\n", state.als_setting);
1739 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1740 kbd_led_als_setting_show, kbd_led_als_setting_store);
1742 static struct attribute *kbd_led_attrs[] = {
1743 &dev_attr_stop_timeout.attr,
1744 &dev_attr_start_triggers.attr,
1748 static const struct attribute_group kbd_led_group = {
1749 .attrs = kbd_led_attrs,
1752 static struct attribute *kbd_led_als_attrs[] = {
1753 &dev_attr_als_enabled.attr,
1754 &dev_attr_als_setting.attr,
1758 static const struct attribute_group kbd_led_als_group = {
1759 .attrs = kbd_led_als_attrs,
1762 static const struct attribute_group *kbd_led_groups[] = {
1768 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1772 struct kbd_state state;
1774 if (kbd_get_max_level()) {
1775 ret = kbd_get_state(&state);
1778 ret = kbd_get_level(&state);
1784 if (kbd_get_valid_token_counts()) {
1785 ret = kbd_get_first_active_token_bit();
1788 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1789 num &= num - 1; /* clear the first bit set */
1792 return ffs(num) - 1;
1795 pr_warn("Keyboard brightness level control not supported\n");
1799 static void kbd_led_level_set(struct led_classdev *led_cdev,
1800 enum led_brightness value)
1802 struct kbd_state state;
1803 struct kbd_state new_state;
1806 if (kbd_get_max_level()) {
1807 if (kbd_get_state(&state))
1810 if (kbd_set_level(&new_state, value))
1812 kbd_set_state_safe(&new_state, &state);
1816 if (kbd_get_valid_token_counts()) {
1817 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1818 num &= num - 1; /* clear the first bit set */
1821 kbd_set_token_bit(ffs(num) - 1);
1825 pr_warn("Keyboard brightness level control not supported\n");
1828 static struct led_classdev kbd_led = {
1829 .name = "dell::kbd_backlight",
1830 .brightness_set = kbd_led_level_set,
1831 .brightness_get = kbd_led_level_get,
1832 .groups = kbd_led_groups,
1835 static int __init kbd_led_init(struct device *dev)
1838 if (!kbd_led_present)
1840 if (!kbd_als_supported)
1841 kbd_led_groups[1] = NULL;
1842 kbd_led.max_brightness = kbd_get_max_level();
1843 if (!kbd_led.max_brightness) {
1844 kbd_led.max_brightness = kbd_get_valid_token_counts();
1845 if (kbd_led.max_brightness)
1846 kbd_led.max_brightness--;
1848 return led_classdev_register(dev, &kbd_led);
1851 static void brightness_set_exit(struct led_classdev *led_cdev,
1852 enum led_brightness value)
1854 /* Don't change backlight level on exit */
1857 static void kbd_led_exit(void)
1859 if (!kbd_led_present)
1861 kbd_led.brightness_set = brightness_set_exit;
1862 led_classdev_unregister(&kbd_led);
1865 static int __init dell_init(void)
1867 int max_intensity = 0;
1870 if (!dmi_check_system(dell_device_table))
1874 /* find if this machine support other functions */
1875 dmi_check_system(dell_quirks);
1877 dmi_walk(find_tokens, NULL);
1880 pr_info("Unable to find dmi tokens\n");
1884 ret = platform_driver_register(&platform_driver);
1886 goto fail_platform_driver;
1887 platform_device = platform_device_alloc("dell-laptop", -1);
1888 if (!platform_device) {
1890 goto fail_platform_device1;
1892 ret = platform_device_add(platform_device);
1894 goto fail_platform_device2;
1897 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
1898 * is passed to SMI handler.
1900 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
1905 buffer = page_address(bufferpage);
1907 ret = dell_setup_rfkill();
1910 pr_warn("Unable to setup rfkill\n");
1914 if (quirks && quirks->touchpad_led)
1915 touchpad_led_init(&platform_device->dev);
1917 kbd_led_init(&platform_device->dev);
1919 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
1920 if (dell_laptop_dir != NULL)
1921 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
1922 &dell_debugfs_fops);
1924 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
1928 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
1929 if (buffer->input[0] != -1) {
1930 dell_send_request(buffer, 0, 2);
1931 max_intensity = buffer->output[3];
1935 if (max_intensity) {
1936 struct backlight_properties props;
1937 memset(&props, 0, sizeof(struct backlight_properties));
1938 props.type = BACKLIGHT_PLATFORM;
1939 props.max_brightness = max_intensity;
1940 dell_backlight_device = backlight_device_register("dell_backlight",
1941 &platform_device->dev,
1946 if (IS_ERR(dell_backlight_device)) {
1947 ret = PTR_ERR(dell_backlight_device);
1948 dell_backlight_device = NULL;
1949 goto fail_backlight;
1952 dell_backlight_device->props.brightness =
1953 dell_get_intensity(dell_backlight_device);
1954 backlight_update_status(dell_backlight_device);
1960 i8042_remove_filter(dell_laptop_i8042_filter);
1961 cancel_delayed_work_sync(&dell_rfkill_work);
1962 dell_cleanup_rfkill();
1964 free_page((unsigned long)bufferpage);
1966 platform_device_del(platform_device);
1967 fail_platform_device2:
1968 platform_device_put(platform_device);
1969 fail_platform_device1:
1970 platform_driver_unregister(&platform_driver);
1971 fail_platform_driver:
1976 static void __exit dell_exit(void)
1978 debugfs_remove_recursive(dell_laptop_dir);
1979 if (quirks && quirks->touchpad_led)
1980 touchpad_led_exit();
1982 i8042_remove_filter(dell_laptop_i8042_filter);
1983 cancel_delayed_work_sync(&dell_rfkill_work);
1984 backlight_device_unregister(dell_backlight_device);
1985 dell_cleanup_rfkill();
1986 if (platform_device) {
1987 platform_device_unregister(platform_device);
1988 platform_driver_unregister(&platform_driver);
1991 free_page((unsigned long)buffer);
1994 module_init(dell_init);
1995 module_exit(dell_exit);
1997 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
1998 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
1999 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2000 MODULE_DESCRIPTION("Dell laptop driver");
2001 MODULE_LICENSE("GPL");