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/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
40 u8 kbd_led_levels_off_1;
42 int needs_kbd_timeouts;
44 * Ordered list of timeouts expressed in seconds.
45 * The list must end with -1
50 static struct quirk_entry *quirks;
52 static struct quirk_entry quirk_dell_vostro_v130 = {
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
58 quirks = dmi->driver_data;
63 * These values come from Windows utility provided by Dell. If any other value
64 * is used then BIOS silently set timeout to 0 without any error message.
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 .needs_kbd_timeouts = 1,
68 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
71 static struct quirk_entry quirk_dell_latitude_e6410 = {
72 .kbd_led_levels_off_1 = 1,
75 static struct platform_driver platform_driver = {
77 .name = "dell-laptop",
81 static struct calling_interface_buffer *buffer;
82 static struct platform_device *platform_device;
83 static struct backlight_device *dell_backlight_device;
84 static struct rfkill *wifi_rfkill;
85 static struct rfkill *bluetooth_rfkill;
86 static struct rfkill *wwan_rfkill;
87 static bool force_rfkill;
89 module_param(force_rfkill, bool, 0444);
90 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
92 static const struct dmi_system_id dell_device_table[] __initconst = {
94 .ident = "Dell laptop",
96 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
97 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
103 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
108 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
109 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
113 .ident = "Dell Computer Corporation",
115 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
116 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
121 MODULE_DEVICE_TABLE(dmi, dell_device_table);
123 static const struct dmi_system_id dell_quirks[] __initconst = {
125 .callback = dmi_matched,
126 .ident = "Dell Vostro V130",
128 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
129 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
131 .driver_data = &quirk_dell_vostro_v130,
134 .callback = dmi_matched,
135 .ident = "Dell Vostro V131",
137 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
138 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
140 .driver_data = &quirk_dell_vostro_v130,
143 .callback = dmi_matched,
144 .ident = "Dell Vostro 3350",
146 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
147 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
149 .driver_data = &quirk_dell_vostro_v130,
152 .callback = dmi_matched,
153 .ident = "Dell Vostro 3555",
155 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
156 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
158 .driver_data = &quirk_dell_vostro_v130,
161 .callback = dmi_matched,
162 .ident = "Dell Inspiron N311z",
164 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
165 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
167 .driver_data = &quirk_dell_vostro_v130,
170 .callback = dmi_matched,
171 .ident = "Dell Inspiron M5110",
173 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
174 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
176 .driver_data = &quirk_dell_vostro_v130,
179 .callback = dmi_matched,
180 .ident = "Dell Vostro 3360",
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
183 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
185 .driver_data = &quirk_dell_vostro_v130,
188 .callback = dmi_matched,
189 .ident = "Dell Vostro 3460",
191 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
192 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
194 .driver_data = &quirk_dell_vostro_v130,
197 .callback = dmi_matched,
198 .ident = "Dell Vostro 3560",
200 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
201 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
203 .driver_data = &quirk_dell_vostro_v130,
206 .callback = dmi_matched,
207 .ident = "Dell Vostro 3450",
209 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
210 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
212 .driver_data = &quirk_dell_vostro_v130,
215 .callback = dmi_matched,
216 .ident = "Dell Inspiron 5420",
218 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
219 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
221 .driver_data = &quirk_dell_vostro_v130,
224 .callback = dmi_matched,
225 .ident = "Dell Inspiron 5520",
227 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
228 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
230 .driver_data = &quirk_dell_vostro_v130,
233 .callback = dmi_matched,
234 .ident = "Dell Inspiron 5720",
236 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
237 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
239 .driver_data = &quirk_dell_vostro_v130,
242 .callback = dmi_matched,
243 .ident = "Dell Inspiron 7420",
245 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
246 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
248 .driver_data = &quirk_dell_vostro_v130,
251 .callback = dmi_matched,
252 .ident = "Dell Inspiron 7520",
254 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
255 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
257 .driver_data = &quirk_dell_vostro_v130,
260 .callback = dmi_matched,
261 .ident = "Dell Inspiron 7720",
263 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
264 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
266 .driver_data = &quirk_dell_vostro_v130,
269 .callback = dmi_matched,
270 .ident = "Dell XPS13 9333",
272 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
273 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
275 .driver_data = &quirk_dell_xps13_9333,
278 .callback = dmi_matched,
279 .ident = "Dell Latitude E6410",
281 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
282 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
284 .driver_data = &quirk_dell_latitude_e6410,
289 void dell_set_arguments(u32 arg0, u32 arg1, u32 arg2, u32 arg3)
291 memset(buffer, 0, sizeof(struct calling_interface_buffer));
292 buffer->input[0] = arg0;
293 buffer->input[1] = arg1;
294 buffer->input[2] = arg2;
295 buffer->input[3] = arg3;
298 int dell_send_request(u16 class, u16 select)
302 buffer->cmd_class = class;
303 buffer->cmd_select = select;
304 ret = dell_smbios_call(buffer);
307 return dell_smbios_error(buffer->output[0]);
311 * Derived from information in smbios-wireless-ctl:
313 * cbSelect 17, Value 11
315 * Return Wireless Info
316 * cbArg1, byte0 = 0x00
318 * cbRes1 Standard return codes (0, -1, -2)
319 * cbRes2 Info bit flags:
321 * 0 Hardware switch supported (1)
322 * 1 WiFi locator supported (1)
323 * 2 WLAN supported (1)
324 * 3 Bluetooth (BT) supported (1)
325 * 4 WWAN supported (1)
326 * 5 Wireless KBD supported (1)
327 * 6 Uw b supported (1)
328 * 7 WiGig supported (1)
329 * 8 WLAN installed (1)
331 * 10 WWAN installed (1)
332 * 11 Uw b installed (1)
333 * 12 WiGig installed (1)
335 * 16 Hardware (HW) switch is On (1)
336 * 17 WLAN disabled (1)
338 * 19 WWAN disabled (1)
339 * 20 Uw b disabled (1)
340 * 21 WiGig disabled (1)
343 * cbRes3 NVRAM size in bytes
344 * cbRes4, byte 0 NVRAM format version number
347 * Set QuickSet Radio Disable Flag
348 * cbArg1, byte0 = 0x01
357 * cbArg1, byte2 Flag bits:
358 * 0 QuickSet disables radio (1)
361 * cbRes1 Standard return codes (0, -1, -2)
362 * cbRes2 QuickSet (QS) radio disable bit map:
367 * 4 QS disables WIGIG
370 * Wireless Switch Configuration
371 * cbArg1, byte0 = 0x02
377 * 2 Set WiFi locator enable/disable
379 * Switch settings (if byte 1==1):
380 * 0 WLAN sw itch control (1)
381 * 1 BT sw itch control (1)
382 * 2 WWAN sw itch control (1)
383 * 3 UWB sw itch control (1)
384 * 4 WiGig sw itch control (1)
386 * cbArg1, byte2 Enable bits (if byte 1==2):
387 * 0 Enable WiFi locator (1)
389 * cbRes1 Standard return codes (0, -1, -2)
390 * cbRes2 QuickSet radio disable bit map:
391 * 0 WLAN controlled by sw itch (1)
392 * 1 BT controlled by sw itch (1)
393 * 2 WWAN controlled by sw itch (1)
394 * 3 UWB controlled by sw itch (1)
395 * 4 WiGig controlled by sw itch (1)
397 * 7 Wireless sw itch config locked (1)
398 * 8 WiFi locator enabled (1)
400 * 15 WiFi locator setting locked (1)
403 * Read Local Config Data (LCD)
404 * cbArg1, byte0 = 0x10
405 * cbArg1, byte1 NVRAM index low byte
406 * cbArg1, byte2 NVRAM index high byte
407 * cbRes1 Standard return codes (0, -1, -2)
408 * cbRes2 4 bytes read from LCD[index]
409 * cbRes3 4 bytes read from LCD[index+4]
410 * cbRes4 4 bytes read from LCD[index+8]
412 * Write Local Config Data (LCD)
413 * cbArg1, byte0 = 0x11
414 * cbArg1, byte1 NVRAM index low byte
415 * cbArg1, byte2 NVRAM index high byte
416 * cbArg2 4 bytes to w rite at LCD[index]
417 * cbArg3 4 bytes to w rite at LCD[index+4]
418 * cbArg4 4 bytes to w rite at LCD[index+8]
419 * cbRes1 Standard return codes (0, -1, -2)
421 * Populate Local Config Data from NVRAM
422 * cbArg1, byte0 = 0x12
423 * cbRes1 Standard return codes (0, -1, -2)
425 * Commit Local Config Data to NVRAM
426 * cbArg1, byte0 = 0x13
427 * cbRes1 Standard return codes (0, -1, -2)
430 static int dell_rfkill_set(void *data, bool blocked)
432 int disable = blocked ? 1 : 0;
433 unsigned long radio = (unsigned long)data;
434 int hwswitch_bit = (unsigned long)data - 1;
439 dell_set_arguments(0, 0, 0, 0);
440 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
443 status = buffer->output[1];
445 dell_set_arguments(0x2, 0, 0, 0);
446 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
449 hwswitch = buffer->output[1];
451 /* If the hardware switch controls this radio, and the hardware
452 switch is disabled, always disable the radio */
453 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
454 (status & BIT(0)) && !(status & BIT(16)))
457 dell_set_arguments(1 | (radio<<8) | (disable << 16), 0, 0, 0);
458 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
462 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
465 if (status & BIT(0)) {
466 /* Has hw-switch, sync sw_state to BIOS */
467 int block = rfkill_blocked(rfkill);
468 dell_set_arguments(1 | (radio << 8) | (block << 16), 0, 0, 0);
469 dell_send_request(CLASS_INFO, SELECT_RFKILL);
471 /* No hw-switch, sync BIOS state to sw_state */
472 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
476 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
477 int status, int hwswitch)
479 if (hwswitch & (BIT(radio - 1)))
480 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
483 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
485 int radio = ((unsigned long)data & 0xF);
490 dell_set_arguments(0, 0, 0, 0);
491 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
492 status = buffer->output[1];
494 if (ret != 0 || !(status & BIT(0))) {
498 dell_set_arguments(0, 0x2, 0, 0);
499 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
500 hwswitch = buffer->output[1];
505 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
508 static const struct rfkill_ops dell_rfkill_ops = {
509 .set_block = dell_rfkill_set,
510 .query = dell_rfkill_query,
513 static struct dentry *dell_laptop_dir;
515 static int dell_debugfs_show(struct seq_file *s, void *data)
522 dell_set_arguments(0, 0, 0, 0);
523 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
526 status = buffer->output[1];
528 dell_set_arguments(0, 0x2, 0, 0);
529 hwswitch_ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
532 hwswitch_state = buffer->output[1];
534 seq_printf(s, "return:\t%d\n", ret);
535 seq_printf(s, "status:\t0x%X\n", status);
536 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
538 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
539 (status & BIT(1)) >> 1);
540 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
541 (status & BIT(2)) >> 2);
542 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
543 (status & BIT(3)) >> 3);
544 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
545 (status & BIT(4)) >> 4);
546 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
547 (status & BIT(5)) >> 5);
548 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
549 (status & BIT(6)) >> 6);
550 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
551 (status & BIT(7)) >> 7);
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 11: UWB installed: %lu\n",
559 (status & BIT(11)) >> 11);
560 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
561 (status & BIT(12)) >> 12);
563 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
564 (status & BIT(16)) >> 16);
565 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
566 (status & BIT(17)) >> 17);
567 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
568 (status & BIT(18)) >> 18);
569 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
570 (status & BIT(19)) >> 19);
571 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
572 (status & BIT(20)) >> 20);
573 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
574 (status & BIT(21)) >> 21);
576 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
577 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
578 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
579 hwswitch_state & BIT(0));
580 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
581 (hwswitch_state & BIT(1)) >> 1);
582 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
583 (hwswitch_state & BIT(2)) >> 2);
584 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
585 (hwswitch_state & BIT(3)) >> 3);
586 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
587 (hwswitch_state & BIT(4)) >> 4);
588 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
589 (hwswitch_state & BIT(7)) >> 7);
590 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
591 (hwswitch_state & BIT(8)) >> 8);
592 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
593 (hwswitch_state & BIT(15)) >> 15);
598 static int dell_debugfs_open(struct inode *inode, struct file *file)
600 return single_open(file, dell_debugfs_show, inode->i_private);
603 static const struct file_operations dell_debugfs_fops = {
604 .owner = THIS_MODULE,
605 .open = dell_debugfs_open,
608 .release = single_release,
611 static void dell_update_rfkill(struct work_struct *ignored)
617 dell_set_arguments(0, 0, 0, 0);
618 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
619 status = buffer->output[1];
624 dell_set_arguments(0, 0x2, 0, 0);
625 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
627 if (ret == 0 && (status & BIT(0)))
628 hwswitch = buffer->output[1];
631 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
632 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
634 if (bluetooth_rfkill) {
635 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
637 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
640 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
641 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
644 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
646 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
649 static bool extended;
651 if (str & I8042_STR_AUXDATA)
654 if (unlikely(data == 0xe0)) {
657 } else if (unlikely(extended)) {
660 schedule_delayed_work(&dell_rfkill_work,
661 round_jiffies_relative(HZ / 4));
670 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
671 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
673 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
674 unsigned long action, void *data)
676 schedule_delayed_work(&dell_rfkill_work, 0);
680 static struct notifier_block dell_laptop_rbtn_notifier = {
681 .notifier_call = dell_laptop_rbtn_notifier_call,
684 static int __init dell_setup_rfkill(void)
686 int status, ret, whitelisted;
690 * rfkill support causes trouble on various models, mostly Inspirons.
691 * So we whitelist certain series, and don't support rfkill on others.
694 product = dmi_get_system_info(DMI_PRODUCT_NAME);
695 if (product && (strncmp(product, "Latitude", 8) == 0 ||
696 strncmp(product, "Precision", 9) == 0))
698 if (!force_rfkill && !whitelisted)
701 dell_set_arguments(0, 0, 0, 0);
702 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
703 status = buffer->output[1];
705 /* dell wireless info smbios call is not supported */
709 /* rfkill is only tested on laptops with a hwswitch */
710 if (!(status & BIT(0)) && !force_rfkill)
713 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
714 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
716 &dell_rfkill_ops, (void *) 1);
721 ret = rfkill_register(wifi_rfkill);
726 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
727 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
728 &platform_device->dev,
729 RFKILL_TYPE_BLUETOOTH,
730 &dell_rfkill_ops, (void *) 2);
731 if (!bluetooth_rfkill) {
735 ret = rfkill_register(bluetooth_rfkill);
740 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
741 wwan_rfkill = rfkill_alloc("dell-wwan",
742 &platform_device->dev,
744 &dell_rfkill_ops, (void *) 3);
749 ret = rfkill_register(wwan_rfkill);
755 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
756 * which can receive events from HW slider switch.
758 * Dell SMBIOS on whitelisted models supports controlling radio devices
759 * but does not support receiving HW button switch events. We can use
760 * i8042 filter hook function to receive keyboard data and handle
761 * keycode for HW button.
763 * So if it is possible we will use Dell Airplane Mode Switch ACPI
764 * driver for receiving HW events and Dell SMBIOS for setting rfkill
765 * states. If ACPI driver or device is not available we will fallback to
766 * i8042 filter hook function.
768 * To prevent duplicate rfkill devices which control and do same thing,
769 * dell-rbtn driver will automatically remove its own rfkill devices
770 * once function dell_rbtn_notifier_register() is called.
773 dell_rbtn_notifier_register_func =
774 symbol_request(dell_rbtn_notifier_register);
775 if (dell_rbtn_notifier_register_func) {
776 dell_rbtn_notifier_unregister_func =
777 symbol_request(dell_rbtn_notifier_unregister);
778 if (!dell_rbtn_notifier_unregister_func) {
779 symbol_put(dell_rbtn_notifier_register);
780 dell_rbtn_notifier_register_func = NULL;
784 if (dell_rbtn_notifier_register_func) {
785 ret = dell_rbtn_notifier_register_func(
786 &dell_laptop_rbtn_notifier);
787 symbol_put(dell_rbtn_notifier_register);
788 dell_rbtn_notifier_register_func = NULL;
790 symbol_put(dell_rbtn_notifier_unregister);
791 dell_rbtn_notifier_unregister_func = NULL;
794 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
799 pr_info("Using dell-rbtn acpi driver for receiving events\n");
800 } else if (ret != -ENODEV) {
801 pr_warn("Unable to register dell rbtn notifier\n");
804 ret = i8042_install_filter(dell_laptop_i8042_filter);
806 pr_warn("Unable to install key filter\n");
809 pr_info("Using i8042 filter function for receiving events\n");
815 rfkill_unregister(wwan_rfkill);
817 rfkill_destroy(wwan_rfkill);
818 if (bluetooth_rfkill)
819 rfkill_unregister(bluetooth_rfkill);
821 rfkill_destroy(bluetooth_rfkill);
823 rfkill_unregister(wifi_rfkill);
825 rfkill_destroy(wifi_rfkill);
830 static void dell_cleanup_rfkill(void)
832 if (dell_rbtn_notifier_unregister_func) {
833 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
834 symbol_put(dell_rbtn_notifier_unregister);
835 dell_rbtn_notifier_unregister_func = NULL;
837 i8042_remove_filter(dell_laptop_i8042_filter);
839 cancel_delayed_work_sync(&dell_rfkill_work);
841 rfkill_unregister(wifi_rfkill);
842 rfkill_destroy(wifi_rfkill);
844 if (bluetooth_rfkill) {
845 rfkill_unregister(bluetooth_rfkill);
846 rfkill_destroy(bluetooth_rfkill);
849 rfkill_unregister(wwan_rfkill);
850 rfkill_destroy(wwan_rfkill);
854 static int dell_send_intensity(struct backlight_device *bd)
856 struct calling_interface_token *token;
859 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
863 dell_set_arguments(token->location, bd->props.brightness, 0, 0);
864 if (power_supply_is_system_supplied() > 0)
865 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
867 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
872 static int dell_get_intensity(struct backlight_device *bd)
874 struct calling_interface_token *token;
877 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
881 dell_set_arguments(token->location, 0, 0, 0);
882 if (power_supply_is_system_supplied() > 0)
883 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_AC);
885 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
888 ret = buffer->output[1];
892 static const struct backlight_ops dell_ops = {
893 .get_brightness = dell_get_intensity,
894 .update_status = dell_send_intensity,
897 static void touchpad_led_on(void)
901 i8042_command(&data, command | 1 << 12);
904 static void touchpad_led_off(void)
908 i8042_command(&data, command | 1 << 12);
911 static void touchpad_led_set(struct led_classdev *led_cdev,
912 enum led_brightness value)
920 static struct led_classdev touchpad_led = {
921 .name = "dell-laptop::touchpad",
922 .brightness_set = touchpad_led_set,
923 .flags = LED_CORE_SUSPENDRESUME,
926 static int __init touchpad_led_init(struct device *dev)
928 return led_classdev_register(dev, &touchpad_led);
931 static void touchpad_led_exit(void)
933 led_classdev_unregister(&touchpad_led);
937 * Derived from information in smbios-keyboard-ctl:
941 * Keyboard illumination
942 * cbArg1 determines the function to be performed
944 * cbArg1 0x0 = Get Feature Information
945 * cbRES1 Standard return codes (0, -1, -2)
946 * cbRES2, word0 Bitmap of user-selectable modes
947 * bit 0 Always off (All systems)
948 * bit 1 Always on (Travis ATG, Siberia)
949 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
950 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
951 * bit 4 Auto: Input-activity-based On; input-activity based Off
952 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
953 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
954 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
955 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
956 * bits 9-15 Reserved for future use
957 * cbRES2, byte2 Reserved for future use
958 * cbRES2, byte3 Keyboard illumination type
962 * 3-255 Reserved for future use
963 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
964 * bit 0 Any keystroke
965 * bit 1 Touchpad activity
966 * bit 2 Pointing stick
968 * bits 4-7 Reserved for future use
969 * cbRES3, byte1 Supported timeout unit bitmap
974 * bits 4-7 Reserved for future use
975 * cbRES3, byte2 Number of keyboard light brightness levels
976 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
977 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
978 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
979 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
981 * cbArg1 0x1 = Get Current State
982 * cbRES1 Standard return codes (0, -1, -2)
983 * cbRES2, word0 Bitmap of current mode state
984 * bit 0 Always off (All systems)
985 * bit 1 Always on (Travis ATG, Siberia)
986 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
987 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
988 * bit 4 Auto: Input-activity-based On; input-activity based Off
989 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
990 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
991 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
992 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
993 * bits 9-15 Reserved for future use
994 * Note: Only One bit can be set
995 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
996 * bit 0 Any keystroke
997 * bit 1 Touchpad activity
998 * bit 2 Pointing stick
1000 * bits 4-7 Reserved for future use
1001 * cbRES2, byte3 Current Timeout on battery
1002 * bits 7:6 Timeout units indicator:
1007 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1008 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1009 * are set upon return from the [Get feature information] call.
1010 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1011 * cbRES3, byte1 Current ALS reading
1012 * cbRES3, byte2 Current keyboard light level.
1013 * cbRES3, byte3 Current timeout on AC Power
1014 * bits 7:6 Timeout units indicator:
1019 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1020 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1021 * are set upon return from the upon return from the [Get Feature information] call.
1023 * cbArg1 0x2 = Set New State
1024 * cbRES1 Standard return codes (0, -1, -2)
1025 * cbArg2, word0 Bitmap of current mode state
1026 * bit 0 Always off (All systems)
1027 * bit 1 Always on (Travis ATG, Siberia)
1028 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1029 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1030 * bit 4 Auto: Input-activity-based On; input-activity based Off
1031 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1032 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1033 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1034 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1035 * bits 9-15 Reserved for future use
1036 * Note: Only One bit can be set
1037 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1038 * keyboard to turn off automatically.
1039 * bit 0 Any keystroke
1040 * bit 1 Touchpad activity
1041 * bit 2 Pointing stick
1043 * bits 4-7 Reserved for future use
1044 * cbArg2, byte3 Desired Timeout on battery
1045 * bits 7:6 Timeout units indicator:
1050 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1051 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1052 * cbArg3, byte2 Desired keyboard light level.
1053 * cbArg3, byte3 Desired Timeout on AC power
1054 * bits 7:6 Timeout units indicator:
1059 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1063 enum kbd_timeout_unit {
1064 KBD_TIMEOUT_SECONDS = 0,
1065 KBD_TIMEOUT_MINUTES,
1071 KBD_MODE_BIT_OFF = 0,
1074 KBD_MODE_BIT_TRIGGER_ALS,
1075 KBD_MODE_BIT_TRIGGER,
1076 KBD_MODE_BIT_TRIGGER_25,
1077 KBD_MODE_BIT_TRIGGER_50,
1078 KBD_MODE_BIT_TRIGGER_75,
1079 KBD_MODE_BIT_TRIGGER_100,
1082 #define kbd_is_als_mode_bit(bit) \
1083 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1084 #define kbd_is_trigger_mode_bit(bit) \
1085 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1086 #define kbd_is_level_mode_bit(bit) \
1087 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1105 u8 timeout_value_ac;
1112 static const int kbd_tokens[] = {
1114 KBD_LED_AUTO_25_TOKEN,
1115 KBD_LED_AUTO_50_TOKEN,
1116 KBD_LED_AUTO_75_TOKEN,
1117 KBD_LED_AUTO_100_TOKEN,
1121 static u16 kbd_token_bits;
1123 static struct kbd_info kbd_info;
1124 static bool kbd_als_supported;
1125 static bool kbd_triggers_supported;
1126 static bool kbd_timeout_ac_supported;
1128 static u8 kbd_mode_levels[16];
1129 static int kbd_mode_levels_count;
1131 static u8 kbd_previous_level;
1132 static u8 kbd_previous_mode_bit;
1134 static bool kbd_led_present;
1135 static DEFINE_MUTEX(kbd_led_mutex);
1138 * NOTE: there are three ways to set the keyboard backlight level.
1139 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1140 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1141 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1143 * There are laptops which support only one of these methods. If we want to
1144 * support as many machines as possible we need to implement all three methods.
1145 * The first two methods use the kbd_state structure. The third uses SMBIOS
1146 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1147 * keyboard backlight level via kbd_state.level.
1150 static int kbd_get_info(struct kbd_info *info)
1155 dell_set_arguments(0, 0, 0, 0);
1156 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1160 info->modes = buffer->output[1] & 0xFFFF;
1161 info->type = (buffer->output[1] >> 24) & 0xFF;
1162 info->triggers = buffer->output[2] & 0xFF;
1163 units = (buffer->output[2] >> 8) & 0xFF;
1164 info->levels = (buffer->output[2] >> 16) & 0xFF;
1166 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1170 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1172 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1174 info->hours = (buffer->output[3] >> 16) & 0xFF;
1176 info->days = (buffer->output[3] >> 24) & 0xFF;
1181 static unsigned int kbd_get_max_level(void)
1183 if (kbd_info.levels != 0)
1184 return kbd_info.levels;
1185 if (kbd_mode_levels_count > 0)
1186 return kbd_mode_levels_count - 1;
1190 static int kbd_get_level(struct kbd_state *state)
1194 if (kbd_info.levels != 0)
1195 return state->level;
1197 if (kbd_mode_levels_count > 0) {
1198 for (i = 0; i < kbd_mode_levels_count; ++i)
1199 if (kbd_mode_levels[i] == state->mode_bit)
1207 static int kbd_set_level(struct kbd_state *state, u8 level)
1209 if (kbd_info.levels != 0) {
1211 kbd_previous_level = level;
1212 if (state->level == level)
1214 state->level = level;
1215 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1216 state->mode_bit = kbd_previous_mode_bit;
1217 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1218 kbd_previous_mode_bit = state->mode_bit;
1219 state->mode_bit = KBD_MODE_BIT_OFF;
1224 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1226 kbd_previous_level = level;
1227 state->mode_bit = kbd_mode_levels[level];
1234 static int kbd_get_state(struct kbd_state *state)
1238 dell_set_arguments(0x1, 0, 0, 0);
1239 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1243 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1244 if (state->mode_bit != 0)
1247 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1248 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1249 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1250 state->als_setting = buffer->output[2] & 0xFF;
1251 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1252 state->level = (buffer->output[2] >> 16) & 0xFF;
1253 state->timeout_value_ac = (buffer->output[2] >> 24) & 0x3F;
1254 state->timeout_unit_ac = (buffer->output[2] >> 30) & 0x3;
1259 static int kbd_set_state(struct kbd_state *state)
1265 input1 = BIT(state->mode_bit) & 0xFFFF;
1266 input1 |= (state->triggers & 0xFF) << 16;
1267 input1 |= (state->timeout_value & 0x3F) << 24;
1268 input1 |= (state->timeout_unit & 0x3) << 30;
1269 input2 = state->als_setting & 0xFF;
1270 input2 |= (state->level & 0xFF) << 16;
1271 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1272 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1273 dell_set_arguments(0x2, input1, input2, 0);
1274 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1279 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1283 ret = kbd_set_state(state);
1288 * When setting the new state fails,try to restore the previous one.
1289 * This is needed on some machines where BIOS sets a default state when
1290 * setting a new state fails. This default state could be all off.
1293 if (kbd_set_state(old))
1294 pr_err("Setting old previous keyboard state failed\n");
1299 static int kbd_set_token_bit(u8 bit)
1301 struct calling_interface_token *token;
1304 if (bit >= ARRAY_SIZE(kbd_tokens))
1307 token = dell_smbios_find_token(kbd_tokens[bit]);
1311 dell_set_arguments(token->location, token->value, 0, 0);
1312 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1317 static int kbd_get_token_bit(u8 bit)
1319 struct calling_interface_token *token;
1323 if (bit >= ARRAY_SIZE(kbd_tokens))
1326 token = dell_smbios_find_token(kbd_tokens[bit]);
1330 dell_set_arguments(token->location, 0, 0, 0);
1331 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1332 val = buffer->output[1];
1337 return (val == token->value);
1340 static int kbd_get_first_active_token_bit(void)
1345 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1346 ret = kbd_get_token_bit(i);
1354 static int kbd_get_valid_token_counts(void)
1356 return hweight16(kbd_token_bits);
1359 static inline int kbd_init_info(void)
1361 struct kbd_state state;
1365 ret = kbd_get_info(&kbd_info);
1369 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1370 * timeout value which is shared for both battery and AC power
1371 * settings. So do not try to set AC values on old models.
1373 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1374 kbd_timeout_ac_supported = true;
1376 kbd_get_state(&state);
1378 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1379 if (kbd_info.seconds > 63)
1380 kbd_info.seconds = 63;
1381 if (kbd_info.minutes > 63)
1382 kbd_info.minutes = 63;
1383 if (kbd_info.hours > 63)
1384 kbd_info.hours = 63;
1385 if (kbd_info.days > 63)
1388 /* NOTE: On tested machines ON mode did not work and caused
1389 * problems (turned backlight off) so do not use it
1391 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1393 kbd_previous_level = kbd_get_level(&state);
1394 kbd_previous_mode_bit = state.mode_bit;
1396 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1397 kbd_previous_level = 1;
1399 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1400 kbd_previous_mode_bit =
1401 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1402 if (kbd_previous_mode_bit != 0)
1403 kbd_previous_mode_bit--;
1406 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1407 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1408 kbd_als_supported = true;
1410 if (kbd_info.modes & (
1411 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1412 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1413 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1415 kbd_triggers_supported = true;
1417 /* kbd_mode_levels[0] is reserved, see below */
1418 for (i = 0; i < 16; ++i)
1419 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1420 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1423 * Find the first supported mode and assign to kbd_mode_levels[0].
1424 * This should be 0 (off), but we cannot depend on the BIOS to
1427 if (kbd_mode_levels_count > 0) {
1428 for (i = 0; i < 16; ++i) {
1429 if (BIT(i) & kbd_info.modes) {
1430 kbd_mode_levels[0] = i;
1434 kbd_mode_levels_count++;
1441 static inline void kbd_init_tokens(void)
1445 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1446 if (dell_smbios_find_token(kbd_tokens[i]))
1447 kbd_token_bits |= BIT(i);
1450 static void kbd_init(void)
1454 ret = kbd_init_info();
1458 * Only supports keyboard backlight when it has at least two modes.
1460 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1461 || kbd_get_valid_token_counts() >= 2)
1462 kbd_led_present = true;
1465 static ssize_t kbd_led_timeout_store(struct device *dev,
1466 struct device_attribute *attr,
1467 const char *buf, size_t count)
1469 struct kbd_state new_state;
1470 struct kbd_state state;
1478 ret = sscanf(buf, "%d %c", &value, &ch);
1491 if (value > kbd_info.seconds)
1493 unit = KBD_TIMEOUT_SECONDS;
1496 if (value > kbd_info.minutes)
1498 unit = KBD_TIMEOUT_MINUTES;
1501 if (value > kbd_info.hours)
1503 unit = KBD_TIMEOUT_HOURS;
1506 if (value > kbd_info.days)
1508 unit = KBD_TIMEOUT_DAYS;
1514 if (quirks && quirks->needs_kbd_timeouts)
1518 /* Convert value from current units to seconds */
1520 case KBD_TIMEOUT_DAYS:
1522 case KBD_TIMEOUT_HOURS:
1524 case KBD_TIMEOUT_MINUTES:
1526 unit = KBD_TIMEOUT_SECONDS;
1529 if (quirks && quirks->needs_kbd_timeouts) {
1530 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1531 if (value <= quirks->kbd_timeouts[i]) {
1532 value = quirks->kbd_timeouts[i];
1538 if (value <= kbd_info.seconds && kbd_info.seconds) {
1539 unit = KBD_TIMEOUT_SECONDS;
1540 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1542 unit = KBD_TIMEOUT_MINUTES;
1543 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1545 unit = KBD_TIMEOUT_HOURS;
1546 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1547 value /= (60 * 60 * 24);
1548 unit = KBD_TIMEOUT_DAYS;
1554 mutex_lock(&kbd_led_mutex);
1556 ret = kbd_get_state(&state);
1562 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1563 new_state.timeout_value_ac = value;
1564 new_state.timeout_unit_ac = unit;
1566 new_state.timeout_value = value;
1567 new_state.timeout_unit = unit;
1570 ret = kbd_set_state_safe(&new_state, &state);
1576 mutex_unlock(&kbd_led_mutex);
1580 static ssize_t kbd_led_timeout_show(struct device *dev,
1581 struct device_attribute *attr, char *buf)
1583 struct kbd_state state;
1589 ret = kbd_get_state(&state);
1593 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1594 value = state.timeout_value_ac;
1595 unit = state.timeout_unit_ac;
1597 value = state.timeout_value;
1598 unit = state.timeout_unit;
1601 len = sprintf(buf, "%d", value);
1604 case KBD_TIMEOUT_SECONDS:
1605 return len + sprintf(buf+len, "s\n");
1606 case KBD_TIMEOUT_MINUTES:
1607 return len + sprintf(buf+len, "m\n");
1608 case KBD_TIMEOUT_HOURS:
1609 return len + sprintf(buf+len, "h\n");
1610 case KBD_TIMEOUT_DAYS:
1611 return len + sprintf(buf+len, "d\n");
1619 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1620 kbd_led_timeout_show, kbd_led_timeout_store);
1622 static const char * const kbd_led_triggers[] = {
1625 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1629 static ssize_t kbd_led_triggers_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;
1636 int trigger_bit = -1;
1640 ret = sscanf(buf, "%20s", trigger);
1644 if (trigger[0] != '+' && trigger[0] != '-')
1647 mutex_lock(&kbd_led_mutex);
1649 ret = kbd_get_state(&state);
1653 if (kbd_triggers_supported)
1654 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1656 if (kbd_triggers_supported) {
1657 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1658 if (!(kbd_info.triggers & BIT(i)))
1660 if (!kbd_led_triggers[i])
1662 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1664 if (trigger[0] == '+' &&
1665 triggers_enabled && (state.triggers & BIT(i))) {
1669 if (trigger[0] == '-' &&
1670 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1679 if (trigger_bit == -1) {
1685 if (trigger[0] == '+')
1686 new_state.triggers |= BIT(trigger_bit);
1688 new_state.triggers &= ~BIT(trigger_bit);
1690 * NOTE: trackstick bit (2) must be disabled when
1691 * disabling touchpad bit (1), otherwise touchpad
1692 * bit (1) will not be disabled
1694 if (trigger_bit == 1)
1695 new_state.triggers &= ~BIT(2);
1697 if ((kbd_info.triggers & new_state.triggers) !=
1698 new_state.triggers) {
1702 if (new_state.triggers && !triggers_enabled) {
1703 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1704 kbd_set_level(&new_state, kbd_previous_level);
1705 } else if (new_state.triggers == 0) {
1706 kbd_set_level(&new_state, 0);
1708 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1712 ret = kbd_set_state_safe(&new_state, &state);
1715 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1716 kbd_previous_mode_bit = new_state.mode_bit;
1719 mutex_unlock(&kbd_led_mutex);
1723 static ssize_t kbd_led_triggers_show(struct device *dev,
1724 struct device_attribute *attr, char *buf)
1726 struct kbd_state state;
1727 bool triggers_enabled;
1731 ret = kbd_get_state(&state);
1737 if (kbd_triggers_supported) {
1738 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1739 level = kbd_get_level(&state);
1740 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1741 if (!(kbd_info.triggers & BIT(i)))
1743 if (!kbd_led_triggers[i])
1745 if ((triggers_enabled || level <= 0) &&
1746 (state.triggers & BIT(i)))
1750 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1755 buf[len - 1] = '\n';
1760 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1761 kbd_led_triggers_show, kbd_led_triggers_store);
1763 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1764 struct device_attribute *attr,
1765 const char *buf, size_t count)
1767 struct kbd_state new_state;
1768 struct kbd_state state;
1769 bool triggers_enabled = false;
1773 ret = kstrtoint(buf, 0, &enable);
1777 mutex_lock(&kbd_led_mutex);
1779 ret = kbd_get_state(&state);
1783 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1790 if (kbd_triggers_supported)
1791 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1794 if (triggers_enabled)
1795 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1797 new_state.mode_bit = KBD_MODE_BIT_ALS;
1799 if (triggers_enabled) {
1800 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1801 kbd_set_level(&new_state, kbd_previous_level);
1803 new_state.mode_bit = KBD_MODE_BIT_ON;
1806 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1811 ret = kbd_set_state_safe(&new_state, &state);
1814 kbd_previous_mode_bit = new_state.mode_bit;
1818 mutex_unlock(&kbd_led_mutex);
1822 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1823 struct device_attribute *attr,
1826 struct kbd_state state;
1827 bool enabled = false;
1830 ret = kbd_get_state(&state);
1833 enabled = kbd_is_als_mode_bit(state.mode_bit);
1835 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1838 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1839 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1841 static ssize_t kbd_led_als_setting_store(struct device *dev,
1842 struct device_attribute *attr,
1843 const char *buf, size_t count)
1845 struct kbd_state state;
1846 struct kbd_state new_state;
1850 ret = kstrtou8(buf, 10, &setting);
1854 mutex_lock(&kbd_led_mutex);
1856 ret = kbd_get_state(&state);
1861 new_state.als_setting = setting;
1863 ret = kbd_set_state_safe(&new_state, &state);
1869 mutex_unlock(&kbd_led_mutex);
1873 static ssize_t kbd_led_als_setting_show(struct device *dev,
1874 struct device_attribute *attr,
1877 struct kbd_state state;
1880 ret = kbd_get_state(&state);
1884 return sprintf(buf, "%d\n", state.als_setting);
1887 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1888 kbd_led_als_setting_show, kbd_led_als_setting_store);
1890 static struct attribute *kbd_led_attrs[] = {
1891 &dev_attr_stop_timeout.attr,
1892 &dev_attr_start_triggers.attr,
1896 static const struct attribute_group kbd_led_group = {
1897 .attrs = kbd_led_attrs,
1900 static struct attribute *kbd_led_als_attrs[] = {
1901 &dev_attr_als_enabled.attr,
1902 &dev_attr_als_setting.attr,
1906 static const struct attribute_group kbd_led_als_group = {
1907 .attrs = kbd_led_als_attrs,
1910 static const struct attribute_group *kbd_led_groups[] = {
1916 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1920 struct kbd_state state;
1922 if (kbd_get_max_level()) {
1923 ret = kbd_get_state(&state);
1926 ret = kbd_get_level(&state);
1932 if (kbd_get_valid_token_counts()) {
1933 ret = kbd_get_first_active_token_bit();
1936 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1937 num &= num - 1; /* clear the first bit set */
1940 return ffs(num) - 1;
1943 pr_warn("Keyboard brightness level control not supported\n");
1947 static int kbd_led_level_set(struct led_classdev *led_cdev,
1948 enum led_brightness value)
1950 struct kbd_state state;
1951 struct kbd_state new_state;
1955 mutex_lock(&kbd_led_mutex);
1957 if (kbd_get_max_level()) {
1958 ret = kbd_get_state(&state);
1962 ret = kbd_set_level(&new_state, value);
1965 ret = kbd_set_state_safe(&new_state, &state);
1966 } else if (kbd_get_valid_token_counts()) {
1967 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1968 num &= num - 1; /* clear the first bit set */
1972 ret = kbd_set_token_bit(ffs(num) - 1);
1974 pr_warn("Keyboard brightness level control not supported\n");
1979 mutex_unlock(&kbd_led_mutex);
1983 static struct led_classdev kbd_led = {
1984 .name = "dell::kbd_backlight",
1985 .flags = LED_BRIGHT_HW_CHANGED,
1986 .brightness_set_blocking = kbd_led_level_set,
1987 .brightness_get = kbd_led_level_get,
1988 .groups = kbd_led_groups,
1991 static int __init kbd_led_init(struct device *dev)
1996 if (!kbd_led_present)
1998 if (!kbd_als_supported)
1999 kbd_led_groups[1] = NULL;
2000 kbd_led.max_brightness = kbd_get_max_level();
2001 if (!kbd_led.max_brightness) {
2002 kbd_led.max_brightness = kbd_get_valid_token_counts();
2003 if (kbd_led.max_brightness)
2004 kbd_led.max_brightness--;
2006 ret = led_classdev_register(dev, &kbd_led);
2008 kbd_led_present = false;
2013 static void brightness_set_exit(struct led_classdev *led_cdev,
2014 enum led_brightness value)
2016 /* Don't change backlight level on exit */
2019 static void kbd_led_exit(void)
2021 if (!kbd_led_present)
2023 kbd_led.brightness_set = brightness_set_exit;
2024 led_classdev_unregister(&kbd_led);
2027 static int dell_laptop_notifier_call(struct notifier_block *nb,
2028 unsigned long action, void *data)
2031 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2032 if (!kbd_led_present)
2035 led_classdev_notify_brightness_hw_changed(&kbd_led,
2036 kbd_led_level_get(&kbd_led));
2043 static struct notifier_block dell_laptop_notifier = {
2044 .notifier_call = dell_laptop_notifier_call,
2047 int dell_micmute_led_set(int state)
2049 struct calling_interface_token *token;
2052 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2053 else if (state == 1)
2054 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2061 dell_set_arguments(token->location, token->value, 0, 0);
2062 dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2066 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2068 static int __init dell_init(void)
2070 struct calling_interface_token *token;
2071 int max_intensity = 0;
2074 if (!dmi_check_system(dell_device_table))
2078 /* find if this machine support other functions */
2079 dmi_check_system(dell_quirks);
2081 ret = platform_driver_register(&platform_driver);
2083 goto fail_platform_driver;
2084 platform_device = platform_device_alloc("dell-laptop", -1);
2085 if (!platform_device) {
2087 goto fail_platform_device1;
2089 ret = platform_device_add(platform_device);
2091 goto fail_platform_device2;
2093 buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
2100 ret = dell_setup_rfkill();
2103 pr_warn("Unable to setup rfkill\n");
2107 if (quirks && quirks->touchpad_led)
2108 touchpad_led_init(&platform_device->dev);
2110 kbd_led_init(&platform_device->dev);
2112 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2113 if (dell_laptop_dir != NULL)
2114 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2115 &dell_debugfs_fops);
2117 dell_laptop_register_notifier(&dell_laptop_notifier);
2119 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2122 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2124 dell_set_arguments(token->location, 0, 0, 0);
2125 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2127 max_intensity = buffer->output[3];
2130 if (max_intensity) {
2131 struct backlight_properties props;
2132 memset(&props, 0, sizeof(struct backlight_properties));
2133 props.type = BACKLIGHT_PLATFORM;
2134 props.max_brightness = max_intensity;
2135 dell_backlight_device = backlight_device_register("dell_backlight",
2136 &platform_device->dev,
2141 if (IS_ERR(dell_backlight_device)) {
2142 ret = PTR_ERR(dell_backlight_device);
2143 dell_backlight_device = NULL;
2144 goto fail_backlight;
2147 dell_backlight_device->props.brightness =
2148 dell_get_intensity(dell_backlight_device);
2149 if (dell_backlight_device->props.brightness < 0) {
2150 ret = dell_backlight_device->props.brightness;
2151 goto fail_get_brightness;
2153 backlight_update_status(dell_backlight_device);
2158 fail_get_brightness:
2159 backlight_device_unregister(dell_backlight_device);
2163 dell_cleanup_rfkill();
2165 platform_device_del(platform_device);
2166 fail_platform_device2:
2167 platform_device_put(platform_device);
2168 fail_platform_device1:
2169 platform_driver_unregister(&platform_driver);
2170 fail_platform_driver:
2174 static void __exit dell_exit(void)
2176 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2177 debugfs_remove_recursive(dell_laptop_dir);
2178 if (quirks && quirks->touchpad_led)
2179 touchpad_led_exit();
2181 backlight_device_unregister(dell_backlight_device);
2183 dell_cleanup_rfkill();
2184 if (platform_device) {
2185 platform_device_unregister(platform_device);
2186 platform_driver_unregister(&platform_driver);
2190 /* dell-rbtn.c driver export functions which will not work correctly (and could
2191 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2192 * not problem when dell-rbtn.c is compiled as external module. When both files
2193 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2194 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2195 * This can be achieved by late_initcall() instead module_init().
2197 late_initcall(dell_init);
2198 module_exit(dell_exit);
2200 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2201 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2202 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2203 MODULE_DESCRIPTION("Dell laptop driver");
2204 MODULE_LICENSE("GPL");