2 * Driver for Dell laptop extras
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"
38 #define BRIGHTNESS_TOKEN 0x7d
39 #define KBD_LED_OFF_TOKEN 0x01E1
40 #define KBD_LED_ON_TOKEN 0x01E2
41 #define KBD_LED_AUTO_TOKEN 0x01E3
42 #define KBD_LED_AUTO_25_TOKEN 0x02EA
43 #define KBD_LED_AUTO_50_TOKEN 0x02EB
44 #define KBD_LED_AUTO_75_TOKEN 0x02EC
45 #define KBD_LED_AUTO_100_TOKEN 0x02F6
46 #define GLOBAL_MIC_MUTE_ENABLE 0x0364
47 #define GLOBAL_MIC_MUTE_DISABLE 0x0365
48 #define KBD_LED_AC_TOKEN 0x0451
53 int needs_kbd_timeouts;
55 * Ordered list of timeouts expressed in seconds.
56 * The list must end with -1
61 static struct quirk_entry *quirks;
63 static struct quirk_entry quirk_dell_vostro_v130 = {
67 static int __init dmi_matched(const struct dmi_system_id *dmi)
69 quirks = dmi->driver_data;
74 * These values come from Windows utility provided by Dell. If any other value
75 * is used then BIOS silently set timeout to 0 without any error message.
77 static struct quirk_entry quirk_dell_xps13_9333 = {
78 .needs_kbd_timeouts = 1,
79 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
82 static struct platform_driver platform_driver = {
84 .name = "dell-laptop",
88 static struct platform_device *platform_device;
89 static struct backlight_device *dell_backlight_device;
90 static struct rfkill *wifi_rfkill;
91 static struct rfkill *bluetooth_rfkill;
92 static struct rfkill *wwan_rfkill;
93 static bool force_rfkill;
95 module_param(force_rfkill, bool, 0444);
96 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
98 static const struct dmi_system_id dell_device_table[] __initconst = {
100 .ident = "Dell laptop",
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
103 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
108 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
109 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
114 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
115 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
119 .ident = "Dell Computer Corporation",
121 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
122 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
127 MODULE_DEVICE_TABLE(dmi, dell_device_table);
129 static const struct dmi_system_id dell_quirks[] __initconst = {
131 .callback = dmi_matched,
132 .ident = "Dell Vostro V130",
134 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
135 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
137 .driver_data = &quirk_dell_vostro_v130,
140 .callback = dmi_matched,
141 .ident = "Dell Vostro V131",
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
144 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
146 .driver_data = &quirk_dell_vostro_v130,
149 .callback = dmi_matched,
150 .ident = "Dell Vostro 3350",
152 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
153 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
155 .driver_data = &quirk_dell_vostro_v130,
158 .callback = dmi_matched,
159 .ident = "Dell Vostro 3555",
161 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
162 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
164 .driver_data = &quirk_dell_vostro_v130,
167 .callback = dmi_matched,
168 .ident = "Dell Inspiron N311z",
170 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
171 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
173 .driver_data = &quirk_dell_vostro_v130,
176 .callback = dmi_matched,
177 .ident = "Dell Inspiron M5110",
179 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
180 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
182 .driver_data = &quirk_dell_vostro_v130,
185 .callback = dmi_matched,
186 .ident = "Dell Vostro 3360",
188 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
189 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
191 .driver_data = &quirk_dell_vostro_v130,
194 .callback = dmi_matched,
195 .ident = "Dell Vostro 3460",
197 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
198 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
200 .driver_data = &quirk_dell_vostro_v130,
203 .callback = dmi_matched,
204 .ident = "Dell Vostro 3560",
206 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
207 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
209 .driver_data = &quirk_dell_vostro_v130,
212 .callback = dmi_matched,
213 .ident = "Dell Vostro 3450",
215 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
216 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
218 .driver_data = &quirk_dell_vostro_v130,
221 .callback = dmi_matched,
222 .ident = "Dell Inspiron 5420",
224 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
225 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
227 .driver_data = &quirk_dell_vostro_v130,
230 .callback = dmi_matched,
231 .ident = "Dell Inspiron 5520",
233 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
234 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
236 .driver_data = &quirk_dell_vostro_v130,
239 .callback = dmi_matched,
240 .ident = "Dell Inspiron 5720",
242 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
243 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
245 .driver_data = &quirk_dell_vostro_v130,
248 .callback = dmi_matched,
249 .ident = "Dell Inspiron 7420",
251 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
252 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
254 .driver_data = &quirk_dell_vostro_v130,
257 .callback = dmi_matched,
258 .ident = "Dell Inspiron 7520",
260 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
261 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
263 .driver_data = &quirk_dell_vostro_v130,
266 .callback = dmi_matched,
267 .ident = "Dell Inspiron 7720",
269 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
270 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
272 .driver_data = &quirk_dell_vostro_v130,
275 .callback = dmi_matched,
276 .ident = "Dell XPS13 9333",
278 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
279 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
281 .driver_data = &quirk_dell_xps13_9333,
287 * Derived from information in smbios-wireless-ctl:
289 * cbSelect 17, Value 11
291 * Return Wireless Info
292 * cbArg1, byte0 = 0x00
294 * cbRes1 Standard return codes (0, -1, -2)
295 * cbRes2 Info bit flags:
297 * 0 Hardware switch supported (1)
298 * 1 WiFi locator supported (1)
299 * 2 WLAN supported (1)
300 * 3 Bluetooth (BT) supported (1)
301 * 4 WWAN supported (1)
302 * 5 Wireless KBD supported (1)
303 * 6 Uw b supported (1)
304 * 7 WiGig supported (1)
305 * 8 WLAN installed (1)
307 * 10 WWAN installed (1)
308 * 11 Uw b installed (1)
309 * 12 WiGig installed (1)
311 * 16 Hardware (HW) switch is On (1)
312 * 17 WLAN disabled (1)
314 * 19 WWAN disabled (1)
315 * 20 Uw b disabled (1)
316 * 21 WiGig disabled (1)
319 * cbRes3 NVRAM size in bytes
320 * cbRes4, byte 0 NVRAM format version number
323 * Set QuickSet Radio Disable Flag
324 * cbArg1, byte0 = 0x01
333 * cbArg1, byte2 Flag bits:
334 * 0 QuickSet disables radio (1)
337 * cbRes1 Standard return codes (0, -1, -2)
338 * cbRes2 QuickSet (QS) radio disable bit map:
343 * 4 QS disables WIGIG
346 * Wireless Switch Configuration
347 * cbArg1, byte0 = 0x02
353 * 2 Set WiFi locator enable/disable
355 * Switch settings (if byte 1==1):
356 * 0 WLAN sw itch control (1)
357 * 1 BT sw itch control (1)
358 * 2 WWAN sw itch control (1)
359 * 3 UWB sw itch control (1)
360 * 4 WiGig sw itch control (1)
362 * cbArg1, byte2 Enable bits (if byte 1==2):
363 * 0 Enable WiFi locator (1)
365 * cbRes1 Standard return codes (0, -1, -2)
366 * cbRes2 QuickSet radio disable bit map:
367 * 0 WLAN controlled by sw itch (1)
368 * 1 BT controlled by sw itch (1)
369 * 2 WWAN controlled by sw itch (1)
370 * 3 UWB controlled by sw itch (1)
371 * 4 WiGig controlled by sw itch (1)
373 * 7 Wireless sw itch config locked (1)
374 * 8 WiFi locator enabled (1)
376 * 15 WiFi locator setting locked (1)
379 * Read Local Config Data (LCD)
380 * cbArg1, byte0 = 0x10
381 * cbArg1, byte1 NVRAM index low byte
382 * cbArg1, byte2 NVRAM index high byte
383 * cbRes1 Standard return codes (0, -1, -2)
384 * cbRes2 4 bytes read from LCD[index]
385 * cbRes3 4 bytes read from LCD[index+4]
386 * cbRes4 4 bytes read from LCD[index+8]
388 * Write Local Config Data (LCD)
389 * cbArg1, byte0 = 0x11
390 * cbArg1, byte1 NVRAM index low byte
391 * cbArg1, byte2 NVRAM index high byte
392 * cbArg2 4 bytes to w rite at LCD[index]
393 * cbArg3 4 bytes to w rite at LCD[index+4]
394 * cbArg4 4 bytes to w rite at LCD[index+8]
395 * cbRes1 Standard return codes (0, -1, -2)
397 * Populate Local Config Data from NVRAM
398 * cbArg1, byte0 = 0x12
399 * cbRes1 Standard return codes (0, -1, -2)
401 * Commit Local Config Data to NVRAM
402 * cbArg1, byte0 = 0x13
403 * cbRes1 Standard return codes (0, -1, -2)
406 static int dell_rfkill_set(void *data, bool blocked)
408 struct calling_interface_buffer *buffer;
409 int disable = blocked ? 1 : 0;
410 unsigned long radio = (unsigned long)data;
411 int hwswitch_bit = (unsigned long)data - 1;
416 buffer = dell_smbios_get_buffer();
418 dell_smbios_send_request(17, 11);
419 ret = buffer->output[0];
420 status = buffer->output[1];
425 dell_smbios_clear_buffer();
427 buffer->input[0] = 0x2;
428 dell_smbios_send_request(17, 11);
429 ret = buffer->output[0];
430 hwswitch = buffer->output[1];
432 /* If the hardware switch controls this radio, and the hardware
433 switch is disabled, always disable the radio */
434 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
435 (status & BIT(0)) && !(status & BIT(16)))
438 dell_smbios_clear_buffer();
440 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
441 dell_smbios_send_request(17, 11);
442 ret = buffer->output[0];
445 dell_smbios_release_buffer();
446 return dell_smbios_error(ret);
449 /* Must be called with the buffer held */
450 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
452 struct calling_interface_buffer *buffer)
454 if (status & BIT(0)) {
455 /* Has hw-switch, sync sw_state to BIOS */
456 int block = rfkill_blocked(rfkill);
457 dell_smbios_clear_buffer();
458 buffer->input[0] = (1 | (radio << 8) | (block << 16));
459 dell_smbios_send_request(17, 11);
461 /* No hw-switch, sync BIOS state to sw_state */
462 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
466 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
467 int status, int hwswitch)
469 if (hwswitch & (BIT(radio - 1)))
470 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
473 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
475 struct calling_interface_buffer *buffer;
476 int radio = ((unsigned long)data & 0xF);
481 buffer = dell_smbios_get_buffer();
483 dell_smbios_send_request(17, 11);
484 ret = buffer->output[0];
485 status = buffer->output[1];
487 if (ret != 0 || !(status & BIT(0))) {
488 dell_smbios_release_buffer();
492 dell_smbios_clear_buffer();
494 buffer->input[0] = 0x2;
495 dell_smbios_send_request(17, 11);
496 ret = buffer->output[0];
497 hwswitch = buffer->output[1];
499 dell_smbios_release_buffer();
504 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
507 static const struct rfkill_ops dell_rfkill_ops = {
508 .set_block = dell_rfkill_set,
509 .query = dell_rfkill_query,
512 static struct dentry *dell_laptop_dir;
514 static int dell_debugfs_show(struct seq_file *s, void *data)
516 struct calling_interface_buffer *buffer;
522 buffer = dell_smbios_get_buffer();
524 dell_smbios_send_request(17, 11);
525 ret = buffer->output[0];
526 status = buffer->output[1];
528 dell_smbios_clear_buffer();
530 buffer->input[0] = 0x2;
531 dell_smbios_send_request(17, 11);
532 hwswitch_ret = buffer->output[0];
533 hwswitch_state = buffer->output[1];
535 dell_smbios_release_buffer();
537 seq_printf(s, "return:\t%d\n", ret);
538 seq_printf(s, "status:\t0x%X\n", status);
539 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
541 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
542 (status & BIT(1)) >> 1);
543 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
544 (status & BIT(2)) >> 2);
545 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
546 (status & BIT(3)) >> 3);
547 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
548 (status & BIT(4)) >> 4);
549 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
550 (status & BIT(5)) >> 5);
551 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
552 (status & BIT(6)) >> 6);
553 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
554 (status & BIT(7)) >> 7);
555 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
556 (status & BIT(8)) >> 8);
557 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
558 (status & BIT(9)) >> 9);
559 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
560 (status & BIT(10)) >> 10);
561 seq_printf(s, "Bit 11: UWB installed: %lu\n",
562 (status & BIT(11)) >> 11);
563 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
564 (status & BIT(12)) >> 12);
566 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
567 (status & BIT(16)) >> 16);
568 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
569 (status & BIT(17)) >> 17);
570 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
571 (status & BIT(18)) >> 18);
572 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
573 (status & BIT(19)) >> 19);
574 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
575 (status & BIT(20)) >> 20);
576 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
577 (status & BIT(21)) >> 21);
579 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
580 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
581 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
582 hwswitch_state & BIT(0));
583 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
584 (hwswitch_state & BIT(1)) >> 1);
585 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
586 (hwswitch_state & BIT(2)) >> 2);
587 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
588 (hwswitch_state & BIT(3)) >> 3);
589 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
590 (hwswitch_state & BIT(4)) >> 4);
591 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
592 (hwswitch_state & BIT(7)) >> 7);
593 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
594 (hwswitch_state & BIT(8)) >> 8);
595 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
596 (hwswitch_state & BIT(15)) >> 15);
601 static int dell_debugfs_open(struct inode *inode, struct file *file)
603 return single_open(file, dell_debugfs_show, inode->i_private);
606 static const struct file_operations dell_debugfs_fops = {
607 .owner = THIS_MODULE,
608 .open = dell_debugfs_open,
611 .release = single_release,
614 static void dell_update_rfkill(struct work_struct *ignored)
616 struct calling_interface_buffer *buffer;
621 buffer = dell_smbios_get_buffer();
623 dell_smbios_send_request(17, 11);
624 ret = buffer->output[0];
625 status = buffer->output[1];
630 dell_smbios_clear_buffer();
632 buffer->input[0] = 0x2;
633 dell_smbios_send_request(17, 11);
634 ret = buffer->output[0];
636 if (ret == 0 && (status & BIT(0)))
637 hwswitch = buffer->output[1];
640 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
641 dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
643 if (bluetooth_rfkill) {
644 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
646 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
650 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
651 dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
655 dell_smbios_release_buffer();
657 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
659 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
662 static bool extended;
664 if (str & I8042_STR_AUXDATA)
667 if (unlikely(data == 0xe0)) {
670 } else if (unlikely(extended)) {
673 schedule_delayed_work(&dell_rfkill_work,
674 round_jiffies_relative(HZ / 4));
683 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
684 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
686 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
687 unsigned long action, void *data)
689 schedule_delayed_work(&dell_rfkill_work, 0);
693 static struct notifier_block dell_laptop_rbtn_notifier = {
694 .notifier_call = dell_laptop_rbtn_notifier_call,
697 static int __init dell_setup_rfkill(void)
699 struct calling_interface_buffer *buffer;
700 int status, ret, whitelisted;
704 * rfkill support causes trouble on various models, mostly Inspirons.
705 * So we whitelist certain series, and don't support rfkill on others.
708 product = dmi_get_system_info(DMI_PRODUCT_NAME);
709 if (product && (strncmp(product, "Latitude", 8) == 0 ||
710 strncmp(product, "Precision", 9) == 0))
712 if (!force_rfkill && !whitelisted)
715 buffer = dell_smbios_get_buffer();
716 dell_smbios_send_request(17, 11);
717 ret = buffer->output[0];
718 status = buffer->output[1];
719 dell_smbios_release_buffer();
721 /* dell wireless info smbios call is not supported */
725 /* rfkill is only tested on laptops with a hwswitch */
726 if (!(status & BIT(0)) && !force_rfkill)
729 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
730 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
732 &dell_rfkill_ops, (void *) 1);
737 ret = rfkill_register(wifi_rfkill);
742 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
743 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
744 &platform_device->dev,
745 RFKILL_TYPE_BLUETOOTH,
746 &dell_rfkill_ops, (void *) 2);
747 if (!bluetooth_rfkill) {
751 ret = rfkill_register(bluetooth_rfkill);
756 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
757 wwan_rfkill = rfkill_alloc("dell-wwan",
758 &platform_device->dev,
760 &dell_rfkill_ops, (void *) 3);
765 ret = rfkill_register(wwan_rfkill);
771 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
772 * which can receive events from HW slider switch.
774 * Dell SMBIOS on whitelisted models supports controlling radio devices
775 * but does not support receiving HW button switch events. We can use
776 * i8042 filter hook function to receive keyboard data and handle
777 * keycode for HW button.
779 * So if it is possible we will use Dell Airplane Mode Switch ACPI
780 * driver for receiving HW events and Dell SMBIOS for setting rfkill
781 * states. If ACPI driver or device is not available we will fallback to
782 * i8042 filter hook function.
784 * To prevent duplicate rfkill devices which control and do same thing,
785 * dell-rbtn driver will automatically remove its own rfkill devices
786 * once function dell_rbtn_notifier_register() is called.
789 dell_rbtn_notifier_register_func =
790 symbol_request(dell_rbtn_notifier_register);
791 if (dell_rbtn_notifier_register_func) {
792 dell_rbtn_notifier_unregister_func =
793 symbol_request(dell_rbtn_notifier_unregister);
794 if (!dell_rbtn_notifier_unregister_func) {
795 symbol_put(dell_rbtn_notifier_register);
796 dell_rbtn_notifier_register_func = NULL;
800 if (dell_rbtn_notifier_register_func) {
801 ret = dell_rbtn_notifier_register_func(
802 &dell_laptop_rbtn_notifier);
803 symbol_put(dell_rbtn_notifier_register);
804 dell_rbtn_notifier_register_func = NULL;
806 symbol_put(dell_rbtn_notifier_unregister);
807 dell_rbtn_notifier_unregister_func = NULL;
810 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
815 pr_info("Using dell-rbtn acpi driver for receiving events\n");
816 } else if (ret != -ENODEV) {
817 pr_warn("Unable to register dell rbtn notifier\n");
820 ret = i8042_install_filter(dell_laptop_i8042_filter);
822 pr_warn("Unable to install key filter\n");
825 pr_info("Using i8042 filter function for receiving events\n");
831 rfkill_unregister(wwan_rfkill);
833 rfkill_destroy(wwan_rfkill);
834 if (bluetooth_rfkill)
835 rfkill_unregister(bluetooth_rfkill);
837 rfkill_destroy(bluetooth_rfkill);
839 rfkill_unregister(wifi_rfkill);
841 rfkill_destroy(wifi_rfkill);
846 static void dell_cleanup_rfkill(void)
848 if (dell_rbtn_notifier_unregister_func) {
849 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
850 symbol_put(dell_rbtn_notifier_unregister);
851 dell_rbtn_notifier_unregister_func = NULL;
853 i8042_remove_filter(dell_laptop_i8042_filter);
855 cancel_delayed_work_sync(&dell_rfkill_work);
857 rfkill_unregister(wifi_rfkill);
858 rfkill_destroy(wifi_rfkill);
860 if (bluetooth_rfkill) {
861 rfkill_unregister(bluetooth_rfkill);
862 rfkill_destroy(bluetooth_rfkill);
865 rfkill_unregister(wwan_rfkill);
866 rfkill_destroy(wwan_rfkill);
870 static int dell_send_intensity(struct backlight_device *bd)
872 struct calling_interface_buffer *buffer;
873 struct calling_interface_token *token;
876 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
880 buffer = dell_smbios_get_buffer();
881 buffer->input[0] = token->location;
882 buffer->input[1] = bd->props.brightness;
884 if (power_supply_is_system_supplied() > 0)
885 dell_smbios_send_request(1, 2);
887 dell_smbios_send_request(1, 1);
889 ret = dell_smbios_error(buffer->output[0]);
891 dell_smbios_release_buffer();
895 static int dell_get_intensity(struct backlight_device *bd)
897 struct calling_interface_buffer *buffer;
898 struct calling_interface_token *token;
901 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
905 buffer = dell_smbios_get_buffer();
906 buffer->input[0] = token->location;
908 if (power_supply_is_system_supplied() > 0)
909 dell_smbios_send_request(0, 2);
911 dell_smbios_send_request(0, 1);
913 if (buffer->output[0])
914 ret = dell_smbios_error(buffer->output[0]);
916 ret = buffer->output[1];
918 dell_smbios_release_buffer();
922 static const struct backlight_ops dell_ops = {
923 .get_brightness = dell_get_intensity,
924 .update_status = dell_send_intensity,
927 static void touchpad_led_on(void)
931 i8042_command(&data, command | 1 << 12);
934 static void touchpad_led_off(void)
938 i8042_command(&data, command | 1 << 12);
941 static void touchpad_led_set(struct led_classdev *led_cdev,
942 enum led_brightness value)
950 static struct led_classdev touchpad_led = {
951 .name = "dell-laptop::touchpad",
952 .brightness_set = touchpad_led_set,
953 .flags = LED_CORE_SUSPENDRESUME,
956 static int __init touchpad_led_init(struct device *dev)
958 return led_classdev_register(dev, &touchpad_led);
961 static void touchpad_led_exit(void)
963 led_classdev_unregister(&touchpad_led);
967 * Derived from information in smbios-keyboard-ctl:
971 * Keyboard illumination
972 * cbArg1 determines the function to be performed
974 * cbArg1 0x0 = Get Feature Information
975 * cbRES1 Standard return codes (0, -1, -2)
976 * cbRES2, word0 Bitmap of user-selectable modes
977 * bit 0 Always off (All systems)
978 * bit 1 Always on (Travis ATG, Siberia)
979 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
980 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
981 * bit 4 Auto: Input-activity-based On; input-activity based Off
982 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
983 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
984 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
985 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
986 * bits 9-15 Reserved for future use
987 * cbRES2, byte2 Reserved for future use
988 * cbRES2, byte3 Keyboard illumination type
992 * 3-255 Reserved for future use
993 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
994 * bit 0 Any keystroke
995 * bit 1 Touchpad activity
996 * bit 2 Pointing stick
998 * bits 4-7 Reserved for future use
999 * cbRES3, byte1 Supported timeout unit bitmap
1004 * bits 4-7 Reserved for future use
1005 * cbRES3, byte2 Number of keyboard light brightness levels
1006 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1007 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1008 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1009 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1011 * cbArg1 0x1 = Get Current State
1012 * cbRES1 Standard return codes (0, -1, -2)
1013 * cbRES2, word0 Bitmap of current mode state
1014 * bit 0 Always off (All systems)
1015 * bit 1 Always on (Travis ATG, Siberia)
1016 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1017 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1018 * bit 4 Auto: Input-activity-based On; input-activity based Off
1019 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1020 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1021 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1022 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1023 * bits 9-15 Reserved for future use
1024 * Note: Only One bit can be set
1025 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1026 * bit 0 Any keystroke
1027 * bit 1 Touchpad activity
1028 * bit 2 Pointing stick
1030 * bits 4-7 Reserved for future use
1031 * cbRES2, byte3 Current Timeout on battery
1032 * bits 7:6 Timeout units indicator:
1037 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1038 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1039 * are set upon return from the [Get feature information] call.
1040 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1041 * cbRES3, byte1 Current ALS reading
1042 * cbRES3, byte2 Current keyboard light level.
1043 * cbRES3, byte3 Current timeout on AC Power
1044 * bits 7:6 Timeout units indicator:
1049 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1050 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1051 * are set upon return from the upon return from the [Get Feature information] call.
1053 * cbArg1 0x2 = Set New State
1054 * cbRES1 Standard return codes (0, -1, -2)
1055 * cbArg2, word0 Bitmap of current mode state
1056 * bit 0 Always off (All systems)
1057 * bit 1 Always on (Travis ATG, Siberia)
1058 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1059 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1060 * bit 4 Auto: Input-activity-based On; input-activity based Off
1061 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1062 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1063 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1064 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1065 * bits 9-15 Reserved for future use
1066 * Note: Only One bit can be set
1067 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1068 * keyboard to turn off automatically.
1069 * bit 0 Any keystroke
1070 * bit 1 Touchpad activity
1071 * bit 2 Pointing stick
1073 * bits 4-7 Reserved for future use
1074 * cbArg2, byte3 Desired Timeout on battery
1075 * bits 7:6 Timeout units indicator:
1080 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1081 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1082 * cbArg3, byte2 Desired keyboard light level.
1083 * cbArg3, byte3 Desired Timeout on AC power
1084 * bits 7:6 Timeout units indicator:
1089 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1093 enum kbd_timeout_unit {
1094 KBD_TIMEOUT_SECONDS = 0,
1095 KBD_TIMEOUT_MINUTES,
1101 KBD_MODE_BIT_OFF = 0,
1104 KBD_MODE_BIT_TRIGGER_ALS,
1105 KBD_MODE_BIT_TRIGGER,
1106 KBD_MODE_BIT_TRIGGER_25,
1107 KBD_MODE_BIT_TRIGGER_50,
1108 KBD_MODE_BIT_TRIGGER_75,
1109 KBD_MODE_BIT_TRIGGER_100,
1112 #define kbd_is_als_mode_bit(bit) \
1113 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1114 #define kbd_is_trigger_mode_bit(bit) \
1115 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1116 #define kbd_is_level_mode_bit(bit) \
1117 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1135 u8 timeout_value_ac;
1142 static const int kbd_tokens[] = {
1144 KBD_LED_AUTO_25_TOKEN,
1145 KBD_LED_AUTO_50_TOKEN,
1146 KBD_LED_AUTO_75_TOKEN,
1147 KBD_LED_AUTO_100_TOKEN,
1151 static u16 kbd_token_bits;
1153 static struct kbd_info kbd_info;
1154 static bool kbd_als_supported;
1155 static bool kbd_triggers_supported;
1156 static bool kbd_timeout_ac_supported;
1158 static u8 kbd_mode_levels[16];
1159 static int kbd_mode_levels_count;
1161 static u8 kbd_previous_level;
1162 static u8 kbd_previous_mode_bit;
1164 static bool kbd_led_present;
1165 static DEFINE_MUTEX(kbd_led_mutex);
1168 * NOTE: there are three ways to set the keyboard backlight level.
1169 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1170 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1171 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1173 * There are laptops which support only one of these methods. If we want to
1174 * support as many machines as possible we need to implement all three methods.
1175 * The first two methods use the kbd_state structure. The third uses SMBIOS
1176 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1177 * keyboard backlight level via kbd_state.level.
1180 static int kbd_get_info(struct kbd_info *info)
1182 struct calling_interface_buffer *buffer;
1186 buffer = dell_smbios_get_buffer();
1188 buffer->input[0] = 0x0;
1189 dell_smbios_send_request(4, 11);
1190 ret = buffer->output[0];
1193 ret = dell_smbios_error(ret);
1197 info->modes = buffer->output[1] & 0xFFFF;
1198 info->type = (buffer->output[1] >> 24) & 0xFF;
1199 info->triggers = buffer->output[2] & 0xFF;
1200 units = (buffer->output[2] >> 8) & 0xFF;
1201 info->levels = (buffer->output[2] >> 16) & 0xFF;
1204 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1206 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1208 info->hours = (buffer->output[3] >> 16) & 0xFF;
1210 info->days = (buffer->output[3] >> 24) & 0xFF;
1213 dell_smbios_release_buffer();
1217 static unsigned int kbd_get_max_level(void)
1219 if (kbd_info.levels != 0)
1220 return kbd_info.levels;
1221 if (kbd_mode_levels_count > 0)
1222 return kbd_mode_levels_count - 1;
1226 static int kbd_get_level(struct kbd_state *state)
1230 if (kbd_info.levels != 0)
1231 return state->level;
1233 if (kbd_mode_levels_count > 0) {
1234 for (i = 0; i < kbd_mode_levels_count; ++i)
1235 if (kbd_mode_levels[i] == state->mode_bit)
1243 static int kbd_set_level(struct kbd_state *state, u8 level)
1245 if (kbd_info.levels != 0) {
1247 kbd_previous_level = level;
1248 if (state->level == level)
1250 state->level = level;
1251 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1252 state->mode_bit = kbd_previous_mode_bit;
1253 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1254 kbd_previous_mode_bit = state->mode_bit;
1255 state->mode_bit = KBD_MODE_BIT_OFF;
1260 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1262 kbd_previous_level = level;
1263 state->mode_bit = kbd_mode_levels[level];
1270 static int kbd_get_state(struct kbd_state *state)
1272 struct calling_interface_buffer *buffer;
1275 buffer = dell_smbios_get_buffer();
1277 buffer->input[0] = 0x1;
1278 dell_smbios_send_request(4, 11);
1279 ret = buffer->output[0];
1282 ret = dell_smbios_error(ret);
1286 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1287 if (state->mode_bit != 0)
1290 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1291 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1292 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1293 state->als_setting = buffer->output[2] & 0xFF;
1294 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1295 state->level = (buffer->output[2] >> 16) & 0xFF;
1296 state->timeout_value_ac = (buffer->output[2] >> 24) & 0x3F;
1297 state->timeout_unit_ac = (buffer->output[2] >> 30) & 0x3;
1300 dell_smbios_release_buffer();
1304 static int kbd_set_state(struct kbd_state *state)
1306 struct calling_interface_buffer *buffer;
1309 buffer = dell_smbios_get_buffer();
1310 buffer->input[0] = 0x2;
1311 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1312 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1313 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1314 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1315 buffer->input[2] = state->als_setting & 0xFF;
1316 buffer->input[2] |= (state->level & 0xFF) << 16;
1317 buffer->input[2] |= (state->timeout_value_ac & 0x3F) << 24;
1318 buffer->input[2] |= (state->timeout_unit_ac & 0x3) << 30;
1319 dell_smbios_send_request(4, 11);
1320 ret = buffer->output[0];
1321 dell_smbios_release_buffer();
1323 return dell_smbios_error(ret);
1326 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1330 ret = kbd_set_state(state);
1335 * When setting the new state fails,try to restore the previous one.
1336 * This is needed on some machines where BIOS sets a default state when
1337 * setting a new state fails. This default state could be all off.
1340 if (kbd_set_state(old))
1341 pr_err("Setting old previous keyboard state failed\n");
1346 static int kbd_set_token_bit(u8 bit)
1348 struct calling_interface_buffer *buffer;
1349 struct calling_interface_token *token;
1352 if (bit >= ARRAY_SIZE(kbd_tokens))
1355 token = dell_smbios_find_token(kbd_tokens[bit]);
1359 buffer = dell_smbios_get_buffer();
1360 buffer->input[0] = token->location;
1361 buffer->input[1] = token->value;
1362 dell_smbios_send_request(1, 0);
1363 ret = buffer->output[0];
1364 dell_smbios_release_buffer();
1366 return dell_smbios_error(ret);
1369 static int kbd_get_token_bit(u8 bit)
1371 struct calling_interface_buffer *buffer;
1372 struct calling_interface_token *token;
1376 if (bit >= ARRAY_SIZE(kbd_tokens))
1379 token = dell_smbios_find_token(kbd_tokens[bit]);
1383 buffer = dell_smbios_get_buffer();
1384 buffer->input[0] = token->location;
1385 dell_smbios_send_request(0, 0);
1386 ret = buffer->output[0];
1387 val = buffer->output[1];
1388 dell_smbios_release_buffer();
1391 return dell_smbios_error(ret);
1393 return (val == token->value);
1396 static int kbd_get_first_active_token_bit(void)
1401 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1402 ret = kbd_get_token_bit(i);
1410 static int kbd_get_valid_token_counts(void)
1412 return hweight16(kbd_token_bits);
1415 static inline int kbd_init_info(void)
1417 struct kbd_state state;
1421 ret = kbd_get_info(&kbd_info);
1425 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1426 * timeout value which is shared for both battery and AC power
1427 * settings. So do not try to set AC values on old models.
1429 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1430 kbd_timeout_ac_supported = true;
1432 kbd_get_state(&state);
1434 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1435 if (kbd_info.seconds > 63)
1436 kbd_info.seconds = 63;
1437 if (kbd_info.minutes > 63)
1438 kbd_info.minutes = 63;
1439 if (kbd_info.hours > 63)
1440 kbd_info.hours = 63;
1441 if (kbd_info.days > 63)
1444 /* NOTE: On tested machines ON mode did not work and caused
1445 * problems (turned backlight off) so do not use it
1447 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1449 kbd_previous_level = kbd_get_level(&state);
1450 kbd_previous_mode_bit = state.mode_bit;
1452 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1453 kbd_previous_level = 1;
1455 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1456 kbd_previous_mode_bit =
1457 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1458 if (kbd_previous_mode_bit != 0)
1459 kbd_previous_mode_bit--;
1462 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1463 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1464 kbd_als_supported = true;
1466 if (kbd_info.modes & (
1467 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1468 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1469 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1471 kbd_triggers_supported = true;
1473 /* kbd_mode_levels[0] is reserved, see below */
1474 for (i = 0; i < 16; ++i)
1475 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1476 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1479 * Find the first supported mode and assign to kbd_mode_levels[0].
1480 * This should be 0 (off), but we cannot depend on the BIOS to
1483 if (kbd_mode_levels_count > 0) {
1484 for (i = 0; i < 16; ++i) {
1485 if (BIT(i) & kbd_info.modes) {
1486 kbd_mode_levels[0] = i;
1490 kbd_mode_levels_count++;
1497 static inline void kbd_init_tokens(void)
1501 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1502 if (dell_smbios_find_token(kbd_tokens[i]))
1503 kbd_token_bits |= BIT(i);
1506 static void kbd_init(void)
1510 ret = kbd_init_info();
1514 * Only supports keyboard backlight when it has at least two modes.
1516 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1517 || kbd_get_valid_token_counts() >= 2)
1518 kbd_led_present = true;
1521 static ssize_t kbd_led_timeout_store(struct device *dev,
1522 struct device_attribute *attr,
1523 const char *buf, size_t count)
1525 struct kbd_state new_state;
1526 struct kbd_state state;
1534 ret = sscanf(buf, "%d %c", &value, &ch);
1547 if (value > kbd_info.seconds)
1549 unit = KBD_TIMEOUT_SECONDS;
1552 if (value > kbd_info.minutes)
1554 unit = KBD_TIMEOUT_MINUTES;
1557 if (value > kbd_info.hours)
1559 unit = KBD_TIMEOUT_HOURS;
1562 if (value > kbd_info.days)
1564 unit = KBD_TIMEOUT_DAYS;
1570 if (quirks && quirks->needs_kbd_timeouts)
1574 /* Convert value from current units to seconds */
1576 case KBD_TIMEOUT_DAYS:
1578 case KBD_TIMEOUT_HOURS:
1580 case KBD_TIMEOUT_MINUTES:
1582 unit = KBD_TIMEOUT_SECONDS;
1585 if (quirks && quirks->needs_kbd_timeouts) {
1586 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1587 if (value <= quirks->kbd_timeouts[i]) {
1588 value = quirks->kbd_timeouts[i];
1594 if (value <= kbd_info.seconds && kbd_info.seconds) {
1595 unit = KBD_TIMEOUT_SECONDS;
1596 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1598 unit = KBD_TIMEOUT_MINUTES;
1599 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1601 unit = KBD_TIMEOUT_HOURS;
1602 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1603 value /= (60 * 60 * 24);
1604 unit = KBD_TIMEOUT_DAYS;
1610 mutex_lock(&kbd_led_mutex);
1612 ret = kbd_get_state(&state);
1618 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1619 new_state.timeout_value_ac = value;
1620 new_state.timeout_unit_ac = unit;
1622 new_state.timeout_value = value;
1623 new_state.timeout_unit = unit;
1626 ret = kbd_set_state_safe(&new_state, &state);
1632 mutex_unlock(&kbd_led_mutex);
1636 static ssize_t kbd_led_timeout_show(struct device *dev,
1637 struct device_attribute *attr, char *buf)
1639 struct kbd_state state;
1645 ret = kbd_get_state(&state);
1649 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1650 value = state.timeout_value_ac;
1651 unit = state.timeout_unit_ac;
1653 value = state.timeout_value;
1654 unit = state.timeout_unit;
1657 len = sprintf(buf, "%d", value);
1660 case KBD_TIMEOUT_SECONDS:
1661 return len + sprintf(buf+len, "s\n");
1662 case KBD_TIMEOUT_MINUTES:
1663 return len + sprintf(buf+len, "m\n");
1664 case KBD_TIMEOUT_HOURS:
1665 return len + sprintf(buf+len, "h\n");
1666 case KBD_TIMEOUT_DAYS:
1667 return len + sprintf(buf+len, "d\n");
1675 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1676 kbd_led_timeout_show, kbd_led_timeout_store);
1678 static const char * const kbd_led_triggers[] = {
1681 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1685 static ssize_t kbd_led_triggers_store(struct device *dev,
1686 struct device_attribute *attr,
1687 const char *buf, size_t count)
1689 struct kbd_state new_state;
1690 struct kbd_state state;
1691 bool triggers_enabled = false;
1692 int trigger_bit = -1;
1696 ret = sscanf(buf, "%20s", trigger);
1700 if (trigger[0] != '+' && trigger[0] != '-')
1703 mutex_lock(&kbd_led_mutex);
1705 ret = kbd_get_state(&state);
1709 if (kbd_triggers_supported)
1710 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1712 if (kbd_triggers_supported) {
1713 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1714 if (!(kbd_info.triggers & BIT(i)))
1716 if (!kbd_led_triggers[i])
1718 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1720 if (trigger[0] == '+' &&
1721 triggers_enabled && (state.triggers & BIT(i))) {
1725 if (trigger[0] == '-' &&
1726 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1735 if (trigger_bit == -1) {
1741 if (trigger[0] == '+')
1742 new_state.triggers |= BIT(trigger_bit);
1744 new_state.triggers &= ~BIT(trigger_bit);
1746 * NOTE: trackstick bit (2) must be disabled when
1747 * disabling touchpad bit (1), otherwise touchpad
1748 * bit (1) will not be disabled
1750 if (trigger_bit == 1)
1751 new_state.triggers &= ~BIT(2);
1753 if ((kbd_info.triggers & new_state.triggers) !=
1754 new_state.triggers) {
1758 if (new_state.triggers && !triggers_enabled) {
1759 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1760 kbd_set_level(&new_state, kbd_previous_level);
1761 } else if (new_state.triggers == 0) {
1762 kbd_set_level(&new_state, 0);
1764 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1768 ret = kbd_set_state_safe(&new_state, &state);
1771 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1772 kbd_previous_mode_bit = new_state.mode_bit;
1775 mutex_unlock(&kbd_led_mutex);
1779 static ssize_t kbd_led_triggers_show(struct device *dev,
1780 struct device_attribute *attr, char *buf)
1782 struct kbd_state state;
1783 bool triggers_enabled;
1787 ret = kbd_get_state(&state);
1793 if (kbd_triggers_supported) {
1794 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1795 level = kbd_get_level(&state);
1796 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1797 if (!(kbd_info.triggers & BIT(i)))
1799 if (!kbd_led_triggers[i])
1801 if ((triggers_enabled || level <= 0) &&
1802 (state.triggers & BIT(i)))
1806 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1811 buf[len - 1] = '\n';
1816 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1817 kbd_led_triggers_show, kbd_led_triggers_store);
1819 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1820 struct device_attribute *attr,
1821 const char *buf, size_t count)
1823 struct kbd_state new_state;
1824 struct kbd_state state;
1825 bool triggers_enabled = false;
1829 ret = kstrtoint(buf, 0, &enable);
1833 mutex_lock(&kbd_led_mutex);
1835 ret = kbd_get_state(&state);
1839 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1846 if (kbd_triggers_supported)
1847 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1850 if (triggers_enabled)
1851 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1853 new_state.mode_bit = KBD_MODE_BIT_ALS;
1855 if (triggers_enabled) {
1856 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1857 kbd_set_level(&new_state, kbd_previous_level);
1859 new_state.mode_bit = KBD_MODE_BIT_ON;
1862 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1867 ret = kbd_set_state_safe(&new_state, &state);
1870 kbd_previous_mode_bit = new_state.mode_bit;
1874 mutex_unlock(&kbd_led_mutex);
1878 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1879 struct device_attribute *attr,
1882 struct kbd_state state;
1883 bool enabled = false;
1886 ret = kbd_get_state(&state);
1889 enabled = kbd_is_als_mode_bit(state.mode_bit);
1891 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1894 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1895 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1897 static ssize_t kbd_led_als_setting_store(struct device *dev,
1898 struct device_attribute *attr,
1899 const char *buf, size_t count)
1901 struct kbd_state state;
1902 struct kbd_state new_state;
1906 ret = kstrtou8(buf, 10, &setting);
1910 mutex_lock(&kbd_led_mutex);
1912 ret = kbd_get_state(&state);
1917 new_state.als_setting = setting;
1919 ret = kbd_set_state_safe(&new_state, &state);
1925 mutex_unlock(&kbd_led_mutex);
1929 static ssize_t kbd_led_als_setting_show(struct device *dev,
1930 struct device_attribute *attr,
1933 struct kbd_state state;
1936 ret = kbd_get_state(&state);
1940 return sprintf(buf, "%d\n", state.als_setting);
1943 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1944 kbd_led_als_setting_show, kbd_led_als_setting_store);
1946 static struct attribute *kbd_led_attrs[] = {
1947 &dev_attr_stop_timeout.attr,
1948 &dev_attr_start_triggers.attr,
1952 static const struct attribute_group kbd_led_group = {
1953 .attrs = kbd_led_attrs,
1956 static struct attribute *kbd_led_als_attrs[] = {
1957 &dev_attr_als_enabled.attr,
1958 &dev_attr_als_setting.attr,
1962 static const struct attribute_group kbd_led_als_group = {
1963 .attrs = kbd_led_als_attrs,
1966 static const struct attribute_group *kbd_led_groups[] = {
1972 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1976 struct kbd_state state;
1978 if (kbd_get_max_level()) {
1979 ret = kbd_get_state(&state);
1982 ret = kbd_get_level(&state);
1988 if (kbd_get_valid_token_counts()) {
1989 ret = kbd_get_first_active_token_bit();
1992 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1993 num &= num - 1; /* clear the first bit set */
1996 return ffs(num) - 1;
1999 pr_warn("Keyboard brightness level control not supported\n");
2003 static int kbd_led_level_set(struct led_classdev *led_cdev,
2004 enum led_brightness value)
2006 struct kbd_state state;
2007 struct kbd_state new_state;
2011 mutex_lock(&kbd_led_mutex);
2013 if (kbd_get_max_level()) {
2014 ret = kbd_get_state(&state);
2018 ret = kbd_set_level(&new_state, value);
2021 ret = kbd_set_state_safe(&new_state, &state);
2022 } else if (kbd_get_valid_token_counts()) {
2023 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2024 num &= num - 1; /* clear the first bit set */
2028 ret = kbd_set_token_bit(ffs(num) - 1);
2030 pr_warn("Keyboard brightness level control not supported\n");
2035 mutex_unlock(&kbd_led_mutex);
2039 static struct led_classdev kbd_led = {
2040 .name = "dell::kbd_backlight",
2041 .flags = LED_BRIGHT_HW_CHANGED,
2042 .brightness_set_blocking = kbd_led_level_set,
2043 .brightness_get = kbd_led_level_get,
2044 .groups = kbd_led_groups,
2047 static int __init kbd_led_init(struct device *dev)
2052 if (!kbd_led_present)
2054 if (!kbd_als_supported)
2055 kbd_led_groups[1] = NULL;
2056 kbd_led.max_brightness = kbd_get_max_level();
2057 if (!kbd_led.max_brightness) {
2058 kbd_led.max_brightness = kbd_get_valid_token_counts();
2059 if (kbd_led.max_brightness)
2060 kbd_led.max_brightness--;
2062 ret = led_classdev_register(dev, &kbd_led);
2064 kbd_led_present = false;
2069 static void brightness_set_exit(struct led_classdev *led_cdev,
2070 enum led_brightness value)
2072 /* Don't change backlight level on exit */
2075 static void kbd_led_exit(void)
2077 if (!kbd_led_present)
2079 kbd_led.brightness_set = brightness_set_exit;
2080 led_classdev_unregister(&kbd_led);
2083 static int dell_laptop_notifier_call(struct notifier_block *nb,
2084 unsigned long action, void *data)
2087 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2088 if (!kbd_led_present)
2091 led_classdev_notify_brightness_hw_changed(&kbd_led,
2092 kbd_led_level_get(&kbd_led));
2099 static struct notifier_block dell_laptop_notifier = {
2100 .notifier_call = dell_laptop_notifier_call,
2103 int dell_micmute_led_set(int state)
2105 struct calling_interface_buffer *buffer;
2106 struct calling_interface_token *token;
2109 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2110 else if (state == 1)
2111 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2118 buffer = dell_smbios_get_buffer();
2119 buffer->input[0] = token->location;
2120 buffer->input[1] = token->value;
2121 dell_smbios_send_request(1, 0);
2122 dell_smbios_release_buffer();
2126 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2128 static int __init dell_init(void)
2130 struct calling_interface_buffer *buffer;
2131 struct calling_interface_token *token;
2132 int max_intensity = 0;
2135 if (!dmi_check_system(dell_device_table))
2139 /* find if this machine support other functions */
2140 dmi_check_system(dell_quirks);
2142 ret = platform_driver_register(&platform_driver);
2144 goto fail_platform_driver;
2145 platform_device = platform_device_alloc("dell-laptop", -1);
2146 if (!platform_device) {
2148 goto fail_platform_device1;
2150 ret = platform_device_add(platform_device);
2152 goto fail_platform_device2;
2154 ret = dell_setup_rfkill();
2157 pr_warn("Unable to setup rfkill\n");
2161 if (quirks && quirks->touchpad_led)
2162 touchpad_led_init(&platform_device->dev);
2164 kbd_led_init(&platform_device->dev);
2166 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2167 if (dell_laptop_dir != NULL)
2168 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2169 &dell_debugfs_fops);
2171 dell_laptop_register_notifier(&dell_laptop_notifier);
2173 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2176 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2178 buffer = dell_smbios_get_buffer();
2179 buffer->input[0] = token->location;
2180 dell_smbios_send_request(0, 2);
2181 if (buffer->output[0] == 0)
2182 max_intensity = buffer->output[3];
2183 dell_smbios_release_buffer();
2186 if (max_intensity) {
2187 struct backlight_properties props;
2188 memset(&props, 0, sizeof(struct backlight_properties));
2189 props.type = BACKLIGHT_PLATFORM;
2190 props.max_brightness = max_intensity;
2191 dell_backlight_device = backlight_device_register("dell_backlight",
2192 &platform_device->dev,
2197 if (IS_ERR(dell_backlight_device)) {
2198 ret = PTR_ERR(dell_backlight_device);
2199 dell_backlight_device = NULL;
2200 goto fail_backlight;
2203 dell_backlight_device->props.brightness =
2204 dell_get_intensity(dell_backlight_device);
2205 if (dell_backlight_device->props.brightness < 0) {
2206 ret = dell_backlight_device->props.brightness;
2207 goto fail_get_brightness;
2209 backlight_update_status(dell_backlight_device);
2214 fail_get_brightness:
2215 backlight_device_unregister(dell_backlight_device);
2217 dell_cleanup_rfkill();
2219 platform_device_del(platform_device);
2220 fail_platform_device2:
2221 platform_device_put(platform_device);
2222 fail_platform_device1:
2223 platform_driver_unregister(&platform_driver);
2224 fail_platform_driver:
2228 static void __exit dell_exit(void)
2230 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2231 debugfs_remove_recursive(dell_laptop_dir);
2232 if (quirks && quirks->touchpad_led)
2233 touchpad_led_exit();
2235 backlight_device_unregister(dell_backlight_device);
2236 dell_cleanup_rfkill();
2237 if (platform_device) {
2238 platform_device_unregister(platform_device);
2239 platform_driver_unregister(&platform_driver);
2243 /* dell-rbtn.c driver export functions which will not work correctly (and could
2244 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2245 * not problem when dell-rbtn.c is compiled as external module. When both files
2246 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2247 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2248 * This can be achieved by late_initcall() instead module_init().
2250 late_initcall(dell_init);
2251 module_exit(dell_exit);
2256 MODULE_DESCRIPTION("Dell laptop driver");
2257 MODULE_LICENSE("GPL");