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"
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
52 u8 kbd_led_levels_off_1;
54 int needs_kbd_timeouts;
56 * Ordered list of timeouts expressed in seconds.
57 * The list must end with -1
62 static struct quirk_entry *quirks;
64 static struct quirk_entry quirk_dell_vostro_v130 = {
68 static int __init dmi_matched(const struct dmi_system_id *dmi)
70 quirks = dmi->driver_data;
75 * These values come from Windows utility provided by Dell. If any other value
76 * is used then BIOS silently set timeout to 0 without any error message.
78 static struct quirk_entry quirk_dell_xps13_9333 = {
79 .needs_kbd_timeouts = 1,
80 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
83 static struct quirk_entry quirk_dell_latitude_e6410 = {
84 .kbd_led_levels_off_1 = 1,
87 static struct platform_driver platform_driver = {
89 .name = "dell-laptop",
93 static struct platform_device *platform_device;
94 static struct backlight_device *dell_backlight_device;
95 static struct rfkill *wifi_rfkill;
96 static struct rfkill *bluetooth_rfkill;
97 static struct rfkill *wwan_rfkill;
98 static bool force_rfkill;
100 module_param(force_rfkill, bool, 0444);
101 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
103 static const struct dmi_system_id dell_device_table[] __initconst = {
105 .ident = "Dell laptop",
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
120 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
124 .ident = "Dell Computer Corporation",
126 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
127 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
132 MODULE_DEVICE_TABLE(dmi, dell_device_table);
134 static const struct dmi_system_id dell_quirks[] __initconst = {
136 .callback = dmi_matched,
137 .ident = "Dell Vostro V130",
139 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
140 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
142 .driver_data = &quirk_dell_vostro_v130,
145 .callback = dmi_matched,
146 .ident = "Dell Vostro V131",
148 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
149 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
151 .driver_data = &quirk_dell_vostro_v130,
154 .callback = dmi_matched,
155 .ident = "Dell Vostro 3350",
157 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
158 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
160 .driver_data = &quirk_dell_vostro_v130,
163 .callback = dmi_matched,
164 .ident = "Dell Vostro 3555",
166 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
167 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
169 .driver_data = &quirk_dell_vostro_v130,
172 .callback = dmi_matched,
173 .ident = "Dell Inspiron N311z",
175 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
178 .driver_data = &quirk_dell_vostro_v130,
181 .callback = dmi_matched,
182 .ident = "Dell Inspiron M5110",
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
187 .driver_data = &quirk_dell_vostro_v130,
190 .callback = dmi_matched,
191 .ident = "Dell Vostro 3360",
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
196 .driver_data = &quirk_dell_vostro_v130,
199 .callback = dmi_matched,
200 .ident = "Dell Vostro 3460",
202 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
203 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
205 .driver_data = &quirk_dell_vostro_v130,
208 .callback = dmi_matched,
209 .ident = "Dell Vostro 3560",
211 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
212 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
214 .driver_data = &quirk_dell_vostro_v130,
217 .callback = dmi_matched,
218 .ident = "Dell Vostro 3450",
220 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
221 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
223 .driver_data = &quirk_dell_vostro_v130,
226 .callback = dmi_matched,
227 .ident = "Dell Inspiron 5420",
229 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
230 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
232 .driver_data = &quirk_dell_vostro_v130,
235 .callback = dmi_matched,
236 .ident = "Dell Inspiron 5520",
238 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
239 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
241 .driver_data = &quirk_dell_vostro_v130,
244 .callback = dmi_matched,
245 .ident = "Dell Inspiron 5720",
247 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
248 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
250 .driver_data = &quirk_dell_vostro_v130,
253 .callback = dmi_matched,
254 .ident = "Dell Inspiron 7420",
256 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
257 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
259 .driver_data = &quirk_dell_vostro_v130,
262 .callback = dmi_matched,
263 .ident = "Dell Inspiron 7520",
265 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
266 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
268 .driver_data = &quirk_dell_vostro_v130,
271 .callback = dmi_matched,
272 .ident = "Dell Inspiron 7720",
274 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
275 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
277 .driver_data = &quirk_dell_vostro_v130,
280 .callback = dmi_matched,
281 .ident = "Dell XPS13 9333",
283 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
284 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
286 .driver_data = &quirk_dell_xps13_9333,
289 .callback = dmi_matched,
290 .ident = "Dell Latitude E6410",
292 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
293 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
295 .driver_data = &quirk_dell_latitude_e6410,
301 * Derived from information in smbios-wireless-ctl:
303 * cbSelect 17, Value 11
305 * Return Wireless Info
306 * cbArg1, byte0 = 0x00
308 * cbRes1 Standard return codes (0, -1, -2)
309 * cbRes2 Info bit flags:
311 * 0 Hardware switch supported (1)
312 * 1 WiFi locator supported (1)
313 * 2 WLAN supported (1)
314 * 3 Bluetooth (BT) supported (1)
315 * 4 WWAN supported (1)
316 * 5 Wireless KBD supported (1)
317 * 6 Uw b supported (1)
318 * 7 WiGig supported (1)
319 * 8 WLAN installed (1)
321 * 10 WWAN installed (1)
322 * 11 Uw b installed (1)
323 * 12 WiGig installed (1)
325 * 16 Hardware (HW) switch is On (1)
326 * 17 WLAN disabled (1)
328 * 19 WWAN disabled (1)
329 * 20 Uw b disabled (1)
330 * 21 WiGig disabled (1)
333 * cbRes3 NVRAM size in bytes
334 * cbRes4, byte 0 NVRAM format version number
337 * Set QuickSet Radio Disable Flag
338 * cbArg1, byte0 = 0x01
347 * cbArg1, byte2 Flag bits:
348 * 0 QuickSet disables radio (1)
351 * cbRes1 Standard return codes (0, -1, -2)
352 * cbRes2 QuickSet (QS) radio disable bit map:
357 * 4 QS disables WIGIG
360 * Wireless Switch Configuration
361 * cbArg1, byte0 = 0x02
367 * 2 Set WiFi locator enable/disable
369 * Switch settings (if byte 1==1):
370 * 0 WLAN sw itch control (1)
371 * 1 BT sw itch control (1)
372 * 2 WWAN sw itch control (1)
373 * 3 UWB sw itch control (1)
374 * 4 WiGig sw itch control (1)
376 * cbArg1, byte2 Enable bits (if byte 1==2):
377 * 0 Enable WiFi locator (1)
379 * cbRes1 Standard return codes (0, -1, -2)
380 * cbRes2 QuickSet radio disable bit map:
381 * 0 WLAN controlled by sw itch (1)
382 * 1 BT controlled by sw itch (1)
383 * 2 WWAN controlled by sw itch (1)
384 * 3 UWB controlled by sw itch (1)
385 * 4 WiGig controlled by sw itch (1)
387 * 7 Wireless sw itch config locked (1)
388 * 8 WiFi locator enabled (1)
390 * 15 WiFi locator setting locked (1)
393 * Read Local Config Data (LCD)
394 * cbArg1, byte0 = 0x10
395 * cbArg1, byte1 NVRAM index low byte
396 * cbArg1, byte2 NVRAM index high byte
397 * cbRes1 Standard return codes (0, -1, -2)
398 * cbRes2 4 bytes read from LCD[index]
399 * cbRes3 4 bytes read from LCD[index+4]
400 * cbRes4 4 bytes read from LCD[index+8]
402 * Write Local Config Data (LCD)
403 * cbArg1, byte0 = 0x11
404 * cbArg1, byte1 NVRAM index low byte
405 * cbArg1, byte2 NVRAM index high byte
406 * cbArg2 4 bytes to w rite at LCD[index]
407 * cbArg3 4 bytes to w rite at LCD[index+4]
408 * cbArg4 4 bytes to w rite at LCD[index+8]
409 * cbRes1 Standard return codes (0, -1, -2)
411 * Populate Local Config Data from NVRAM
412 * cbArg1, byte0 = 0x12
413 * cbRes1 Standard return codes (0, -1, -2)
415 * Commit Local Config Data to NVRAM
416 * cbArg1, byte0 = 0x13
417 * cbRes1 Standard return codes (0, -1, -2)
420 static int dell_rfkill_set(void *data, bool blocked)
422 struct calling_interface_buffer *buffer;
423 int disable = blocked ? 1 : 0;
424 unsigned long radio = (unsigned long)data;
425 int hwswitch_bit = (unsigned long)data - 1;
430 buffer = dell_smbios_get_buffer();
432 dell_smbios_send_request(17, 11);
433 ret = buffer->output[0];
434 status = buffer->output[1];
439 dell_smbios_clear_buffer();
441 buffer->input[0] = 0x2;
442 dell_smbios_send_request(17, 11);
443 ret = buffer->output[0];
444 hwswitch = buffer->output[1];
446 /* If the hardware switch controls this radio, and the hardware
447 switch is disabled, always disable the radio */
448 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
449 (status & BIT(0)) && !(status & BIT(16)))
452 dell_smbios_clear_buffer();
454 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
455 dell_smbios_send_request(17, 11);
456 ret = buffer->output[0];
459 dell_smbios_release_buffer();
460 return dell_smbios_error(ret);
463 /* Must be called with the buffer held */
464 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
466 struct calling_interface_buffer *buffer)
468 if (status & BIT(0)) {
469 /* Has hw-switch, sync sw_state to BIOS */
470 int block = rfkill_blocked(rfkill);
471 dell_smbios_clear_buffer();
472 buffer->input[0] = (1 | (radio << 8) | (block << 16));
473 dell_smbios_send_request(17, 11);
475 /* No hw-switch, sync BIOS state to sw_state */
476 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
480 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
481 int status, int hwswitch)
483 if (hwswitch & (BIT(radio - 1)))
484 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
487 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
489 struct calling_interface_buffer *buffer;
490 int radio = ((unsigned long)data & 0xF);
495 buffer = dell_smbios_get_buffer();
497 dell_smbios_send_request(17, 11);
498 ret = buffer->output[0];
499 status = buffer->output[1];
501 if (ret != 0 || !(status & BIT(0))) {
502 dell_smbios_release_buffer();
506 dell_smbios_clear_buffer();
508 buffer->input[0] = 0x2;
509 dell_smbios_send_request(17, 11);
510 ret = buffer->output[0];
511 hwswitch = buffer->output[1];
513 dell_smbios_release_buffer();
518 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
521 static const struct rfkill_ops dell_rfkill_ops = {
522 .set_block = dell_rfkill_set,
523 .query = dell_rfkill_query,
526 static struct dentry *dell_laptop_dir;
528 static int dell_debugfs_show(struct seq_file *s, void *data)
530 struct calling_interface_buffer *buffer;
536 buffer = dell_smbios_get_buffer();
538 dell_smbios_send_request(17, 11);
539 ret = buffer->output[0];
540 status = buffer->output[1];
542 dell_smbios_clear_buffer();
544 buffer->input[0] = 0x2;
545 dell_smbios_send_request(17, 11);
546 hwswitch_ret = buffer->output[0];
547 hwswitch_state = buffer->output[1];
549 dell_smbios_release_buffer();
551 seq_printf(s, "return:\t%d\n", ret);
552 seq_printf(s, "status:\t0x%X\n", status);
553 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
555 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
556 (status & BIT(1)) >> 1);
557 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
558 (status & BIT(2)) >> 2);
559 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
560 (status & BIT(3)) >> 3);
561 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
562 (status & BIT(4)) >> 4);
563 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
564 (status & BIT(5)) >> 5);
565 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
566 (status & BIT(6)) >> 6);
567 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
568 (status & BIT(7)) >> 7);
569 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
570 (status & BIT(8)) >> 8);
571 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
572 (status & BIT(9)) >> 9);
573 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
574 (status & BIT(10)) >> 10);
575 seq_printf(s, "Bit 11: UWB installed: %lu\n",
576 (status & BIT(11)) >> 11);
577 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
578 (status & BIT(12)) >> 12);
580 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
581 (status & BIT(16)) >> 16);
582 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
583 (status & BIT(17)) >> 17);
584 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
585 (status & BIT(18)) >> 18);
586 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
587 (status & BIT(19)) >> 19);
588 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
589 (status & BIT(20)) >> 20);
590 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
591 (status & BIT(21)) >> 21);
593 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
594 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
595 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
596 hwswitch_state & BIT(0));
597 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
598 (hwswitch_state & BIT(1)) >> 1);
599 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
600 (hwswitch_state & BIT(2)) >> 2);
601 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
602 (hwswitch_state & BIT(3)) >> 3);
603 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
604 (hwswitch_state & BIT(4)) >> 4);
605 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
606 (hwswitch_state & BIT(7)) >> 7);
607 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
608 (hwswitch_state & BIT(8)) >> 8);
609 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
610 (hwswitch_state & BIT(15)) >> 15);
615 static int dell_debugfs_open(struct inode *inode, struct file *file)
617 return single_open(file, dell_debugfs_show, inode->i_private);
620 static const struct file_operations dell_debugfs_fops = {
621 .owner = THIS_MODULE,
622 .open = dell_debugfs_open,
625 .release = single_release,
628 static void dell_update_rfkill(struct work_struct *ignored)
630 struct calling_interface_buffer *buffer;
635 buffer = dell_smbios_get_buffer();
637 dell_smbios_send_request(17, 11);
638 ret = buffer->output[0];
639 status = buffer->output[1];
644 dell_smbios_clear_buffer();
646 buffer->input[0] = 0x2;
647 dell_smbios_send_request(17, 11);
648 ret = buffer->output[0];
650 if (ret == 0 && (status & BIT(0)))
651 hwswitch = buffer->output[1];
654 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
655 dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
657 if (bluetooth_rfkill) {
658 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
660 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
664 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
665 dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
669 dell_smbios_release_buffer();
671 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
673 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
676 static bool extended;
678 if (str & I8042_STR_AUXDATA)
681 if (unlikely(data == 0xe0)) {
684 } else if (unlikely(extended)) {
687 schedule_delayed_work(&dell_rfkill_work,
688 round_jiffies_relative(HZ / 4));
697 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
698 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
700 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
701 unsigned long action, void *data)
703 schedule_delayed_work(&dell_rfkill_work, 0);
707 static struct notifier_block dell_laptop_rbtn_notifier = {
708 .notifier_call = dell_laptop_rbtn_notifier_call,
711 static int __init dell_setup_rfkill(void)
713 struct calling_interface_buffer *buffer;
714 int status, ret, whitelisted;
718 * rfkill support causes trouble on various models, mostly Inspirons.
719 * So we whitelist certain series, and don't support rfkill on others.
722 product = dmi_get_system_info(DMI_PRODUCT_NAME);
723 if (product && (strncmp(product, "Latitude", 8) == 0 ||
724 strncmp(product, "Precision", 9) == 0))
726 if (!force_rfkill && !whitelisted)
729 buffer = dell_smbios_get_buffer();
730 dell_smbios_send_request(17, 11);
731 ret = buffer->output[0];
732 status = buffer->output[1];
733 dell_smbios_release_buffer();
735 /* dell wireless info smbios call is not supported */
739 /* rfkill is only tested on laptops with a hwswitch */
740 if (!(status & BIT(0)) && !force_rfkill)
743 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
744 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
746 &dell_rfkill_ops, (void *) 1);
751 ret = rfkill_register(wifi_rfkill);
756 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
757 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
758 &platform_device->dev,
759 RFKILL_TYPE_BLUETOOTH,
760 &dell_rfkill_ops, (void *) 2);
761 if (!bluetooth_rfkill) {
765 ret = rfkill_register(bluetooth_rfkill);
770 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
771 wwan_rfkill = rfkill_alloc("dell-wwan",
772 &platform_device->dev,
774 &dell_rfkill_ops, (void *) 3);
779 ret = rfkill_register(wwan_rfkill);
785 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
786 * which can receive events from HW slider switch.
788 * Dell SMBIOS on whitelisted models supports controlling radio devices
789 * but does not support receiving HW button switch events. We can use
790 * i8042 filter hook function to receive keyboard data and handle
791 * keycode for HW button.
793 * So if it is possible we will use Dell Airplane Mode Switch ACPI
794 * driver for receiving HW events and Dell SMBIOS for setting rfkill
795 * states. If ACPI driver or device is not available we will fallback to
796 * i8042 filter hook function.
798 * To prevent duplicate rfkill devices which control and do same thing,
799 * dell-rbtn driver will automatically remove its own rfkill devices
800 * once function dell_rbtn_notifier_register() is called.
803 dell_rbtn_notifier_register_func =
804 symbol_request(dell_rbtn_notifier_register);
805 if (dell_rbtn_notifier_register_func) {
806 dell_rbtn_notifier_unregister_func =
807 symbol_request(dell_rbtn_notifier_unregister);
808 if (!dell_rbtn_notifier_unregister_func) {
809 symbol_put(dell_rbtn_notifier_register);
810 dell_rbtn_notifier_register_func = NULL;
814 if (dell_rbtn_notifier_register_func) {
815 ret = dell_rbtn_notifier_register_func(
816 &dell_laptop_rbtn_notifier);
817 symbol_put(dell_rbtn_notifier_register);
818 dell_rbtn_notifier_register_func = NULL;
820 symbol_put(dell_rbtn_notifier_unregister);
821 dell_rbtn_notifier_unregister_func = NULL;
824 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
829 pr_info("Using dell-rbtn acpi driver for receiving events\n");
830 } else if (ret != -ENODEV) {
831 pr_warn("Unable to register dell rbtn notifier\n");
834 ret = i8042_install_filter(dell_laptop_i8042_filter);
836 pr_warn("Unable to install key filter\n");
839 pr_info("Using i8042 filter function for receiving events\n");
845 rfkill_unregister(wwan_rfkill);
847 rfkill_destroy(wwan_rfkill);
848 if (bluetooth_rfkill)
849 rfkill_unregister(bluetooth_rfkill);
851 rfkill_destroy(bluetooth_rfkill);
853 rfkill_unregister(wifi_rfkill);
855 rfkill_destroy(wifi_rfkill);
860 static void dell_cleanup_rfkill(void)
862 if (dell_rbtn_notifier_unregister_func) {
863 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
864 symbol_put(dell_rbtn_notifier_unregister);
865 dell_rbtn_notifier_unregister_func = NULL;
867 i8042_remove_filter(dell_laptop_i8042_filter);
869 cancel_delayed_work_sync(&dell_rfkill_work);
871 rfkill_unregister(wifi_rfkill);
872 rfkill_destroy(wifi_rfkill);
874 if (bluetooth_rfkill) {
875 rfkill_unregister(bluetooth_rfkill);
876 rfkill_destroy(bluetooth_rfkill);
879 rfkill_unregister(wwan_rfkill);
880 rfkill_destroy(wwan_rfkill);
884 static int dell_send_intensity(struct backlight_device *bd)
886 struct calling_interface_buffer *buffer;
887 struct calling_interface_token *token;
890 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
894 buffer = dell_smbios_get_buffer();
895 buffer->input[0] = token->location;
896 buffer->input[1] = bd->props.brightness;
898 if (power_supply_is_system_supplied() > 0)
899 dell_smbios_send_request(1, 2);
901 dell_smbios_send_request(1, 1);
903 ret = dell_smbios_error(buffer->output[0]);
905 dell_smbios_release_buffer();
909 static int dell_get_intensity(struct backlight_device *bd)
911 struct calling_interface_buffer *buffer;
912 struct calling_interface_token *token;
915 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
919 buffer = dell_smbios_get_buffer();
920 buffer->input[0] = token->location;
922 if (power_supply_is_system_supplied() > 0)
923 dell_smbios_send_request(0, 2);
925 dell_smbios_send_request(0, 1);
927 if (buffer->output[0])
928 ret = dell_smbios_error(buffer->output[0]);
930 ret = buffer->output[1];
932 dell_smbios_release_buffer();
936 static const struct backlight_ops dell_ops = {
937 .get_brightness = dell_get_intensity,
938 .update_status = dell_send_intensity,
941 static void touchpad_led_on(void)
945 i8042_command(&data, command | 1 << 12);
948 static void touchpad_led_off(void)
952 i8042_command(&data, command | 1 << 12);
955 static void touchpad_led_set(struct led_classdev *led_cdev,
956 enum led_brightness value)
964 static struct led_classdev touchpad_led = {
965 .name = "dell-laptop::touchpad",
966 .brightness_set = touchpad_led_set,
967 .flags = LED_CORE_SUSPENDRESUME,
970 static int __init touchpad_led_init(struct device *dev)
972 return led_classdev_register(dev, &touchpad_led);
975 static void touchpad_led_exit(void)
977 led_classdev_unregister(&touchpad_led);
981 * Derived from information in smbios-keyboard-ctl:
985 * Keyboard illumination
986 * cbArg1 determines the function to be performed
988 * cbArg1 0x0 = Get Feature Information
989 * cbRES1 Standard return codes (0, -1, -2)
990 * cbRES2, word0 Bitmap of user-selectable modes
991 * bit 0 Always off (All systems)
992 * bit 1 Always on (Travis ATG, Siberia)
993 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
994 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
995 * bit 4 Auto: Input-activity-based On; input-activity based Off
996 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
997 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
998 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
999 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1000 * bits 9-15 Reserved for future use
1001 * cbRES2, byte2 Reserved for future use
1002 * cbRES2, byte3 Keyboard illumination type
1006 * 3-255 Reserved for future use
1007 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1008 * bit 0 Any keystroke
1009 * bit 1 Touchpad activity
1010 * bit 2 Pointing stick
1012 * bits 4-7 Reserved for future use
1013 * cbRES3, byte1 Supported timeout unit bitmap
1018 * bits 4-7 Reserved for future use
1019 * cbRES3, byte2 Number of keyboard light brightness levels
1020 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1021 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1022 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1023 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1025 * cbArg1 0x1 = Get Current State
1026 * cbRES1 Standard return codes (0, -1, -2)
1027 * cbRES2, word0 Bitmap of current mode state
1028 * bit 0 Always off (All systems)
1029 * bit 1 Always on (Travis ATG, Siberia)
1030 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1031 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1032 * bit 4 Auto: Input-activity-based On; input-activity based Off
1033 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1034 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1035 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1036 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1037 * bits 9-15 Reserved for future use
1038 * Note: Only One bit can be set
1039 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1040 * bit 0 Any keystroke
1041 * bit 1 Touchpad activity
1042 * bit 2 Pointing stick
1044 * bits 4-7 Reserved for future use
1045 * cbRES2, byte3 Current Timeout on battery
1046 * bits 7:6 Timeout units indicator:
1051 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1052 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1053 * are set upon return from the [Get feature information] call.
1054 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1055 * cbRES3, byte1 Current ALS reading
1056 * cbRES3, byte2 Current keyboard light level.
1057 * cbRES3, byte3 Current timeout on AC Power
1058 * bits 7:6 Timeout units indicator:
1063 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1064 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1065 * are set upon return from the upon return from the [Get Feature information] call.
1067 * cbArg1 0x2 = Set New State
1068 * cbRES1 Standard return codes (0, -1, -2)
1069 * cbArg2, word0 Bitmap of current mode state
1070 * bit 0 Always off (All systems)
1071 * bit 1 Always on (Travis ATG, Siberia)
1072 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1073 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1074 * bit 4 Auto: Input-activity-based On; input-activity based Off
1075 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1076 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1077 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1078 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1079 * bits 9-15 Reserved for future use
1080 * Note: Only One bit can be set
1081 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1082 * keyboard to turn off automatically.
1083 * bit 0 Any keystroke
1084 * bit 1 Touchpad activity
1085 * bit 2 Pointing stick
1087 * bits 4-7 Reserved for future use
1088 * cbArg2, byte3 Desired Timeout on battery
1089 * bits 7:6 Timeout units indicator:
1094 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1095 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1096 * cbArg3, byte2 Desired keyboard light level.
1097 * cbArg3, byte3 Desired Timeout on AC power
1098 * bits 7:6 Timeout units indicator:
1103 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1107 enum kbd_timeout_unit {
1108 KBD_TIMEOUT_SECONDS = 0,
1109 KBD_TIMEOUT_MINUTES,
1115 KBD_MODE_BIT_OFF = 0,
1118 KBD_MODE_BIT_TRIGGER_ALS,
1119 KBD_MODE_BIT_TRIGGER,
1120 KBD_MODE_BIT_TRIGGER_25,
1121 KBD_MODE_BIT_TRIGGER_50,
1122 KBD_MODE_BIT_TRIGGER_75,
1123 KBD_MODE_BIT_TRIGGER_100,
1126 #define kbd_is_als_mode_bit(bit) \
1127 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1128 #define kbd_is_trigger_mode_bit(bit) \
1129 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1130 #define kbd_is_level_mode_bit(bit) \
1131 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1149 u8 timeout_value_ac;
1156 static const int kbd_tokens[] = {
1158 KBD_LED_AUTO_25_TOKEN,
1159 KBD_LED_AUTO_50_TOKEN,
1160 KBD_LED_AUTO_75_TOKEN,
1161 KBD_LED_AUTO_100_TOKEN,
1165 static u16 kbd_token_bits;
1167 static struct kbd_info kbd_info;
1168 static bool kbd_als_supported;
1169 static bool kbd_triggers_supported;
1170 static bool kbd_timeout_ac_supported;
1172 static u8 kbd_mode_levels[16];
1173 static int kbd_mode_levels_count;
1175 static u8 kbd_previous_level;
1176 static u8 kbd_previous_mode_bit;
1178 static bool kbd_led_present;
1179 static DEFINE_MUTEX(kbd_led_mutex);
1180 static enum led_brightness kbd_led_level;
1183 * NOTE: there are three ways to set the keyboard backlight level.
1184 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1185 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1186 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1188 * There are laptops which support only one of these methods. If we want to
1189 * support as many machines as possible we need to implement all three methods.
1190 * The first two methods use the kbd_state structure. The third uses SMBIOS
1191 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1192 * keyboard backlight level via kbd_state.level.
1195 static int kbd_get_info(struct kbd_info *info)
1197 struct calling_interface_buffer *buffer;
1201 buffer = dell_smbios_get_buffer();
1203 buffer->input[0] = 0x0;
1204 dell_smbios_send_request(4, 11);
1205 ret = buffer->output[0];
1208 ret = dell_smbios_error(ret);
1212 info->modes = buffer->output[1] & 0xFFFF;
1213 info->type = (buffer->output[1] >> 24) & 0xFF;
1214 info->triggers = buffer->output[2] & 0xFF;
1215 units = (buffer->output[2] >> 8) & 0xFF;
1216 info->levels = (buffer->output[2] >> 16) & 0xFF;
1218 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1222 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1224 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1226 info->hours = (buffer->output[3] >> 16) & 0xFF;
1228 info->days = (buffer->output[3] >> 24) & 0xFF;
1231 dell_smbios_release_buffer();
1235 static unsigned int kbd_get_max_level(void)
1237 if (kbd_info.levels != 0)
1238 return kbd_info.levels;
1239 if (kbd_mode_levels_count > 0)
1240 return kbd_mode_levels_count - 1;
1244 static int kbd_get_level(struct kbd_state *state)
1248 if (kbd_info.levels != 0)
1249 return state->level;
1251 if (kbd_mode_levels_count > 0) {
1252 for (i = 0; i < kbd_mode_levels_count; ++i)
1253 if (kbd_mode_levels[i] == state->mode_bit)
1261 static int kbd_set_level(struct kbd_state *state, u8 level)
1263 if (kbd_info.levels != 0) {
1265 kbd_previous_level = level;
1266 if (state->level == level)
1268 state->level = level;
1269 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1270 state->mode_bit = kbd_previous_mode_bit;
1271 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1272 kbd_previous_mode_bit = state->mode_bit;
1273 state->mode_bit = KBD_MODE_BIT_OFF;
1278 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1280 kbd_previous_level = level;
1281 state->mode_bit = kbd_mode_levels[level];
1288 static int kbd_get_state(struct kbd_state *state)
1290 struct calling_interface_buffer *buffer;
1293 buffer = dell_smbios_get_buffer();
1295 buffer->input[0] = 0x1;
1296 dell_smbios_send_request(4, 11);
1297 ret = buffer->output[0];
1300 ret = dell_smbios_error(ret);
1304 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1305 if (state->mode_bit != 0)
1308 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1309 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1310 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1311 state->als_setting = buffer->output[2] & 0xFF;
1312 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1313 state->level = (buffer->output[2] >> 16) & 0xFF;
1314 state->timeout_value_ac = (buffer->output[2] >> 24) & 0x3F;
1315 state->timeout_unit_ac = (buffer->output[2] >> 30) & 0x3;
1318 dell_smbios_release_buffer();
1322 static int kbd_set_state(struct kbd_state *state)
1324 struct calling_interface_buffer *buffer;
1327 buffer = dell_smbios_get_buffer();
1328 buffer->input[0] = 0x2;
1329 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1330 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1331 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1332 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1333 buffer->input[2] = state->als_setting & 0xFF;
1334 buffer->input[2] |= (state->level & 0xFF) << 16;
1335 buffer->input[2] |= (state->timeout_value_ac & 0x3F) << 24;
1336 buffer->input[2] |= (state->timeout_unit_ac & 0x3) << 30;
1337 dell_smbios_send_request(4, 11);
1338 ret = buffer->output[0];
1339 dell_smbios_release_buffer();
1341 return dell_smbios_error(ret);
1344 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1348 ret = kbd_set_state(state);
1353 * When setting the new state fails,try to restore the previous one.
1354 * This is needed on some machines where BIOS sets a default state when
1355 * setting a new state fails. This default state could be all off.
1358 if (kbd_set_state(old))
1359 pr_err("Setting old previous keyboard state failed\n");
1364 static int kbd_set_token_bit(u8 bit)
1366 struct calling_interface_buffer *buffer;
1367 struct calling_interface_token *token;
1370 if (bit >= ARRAY_SIZE(kbd_tokens))
1373 token = dell_smbios_find_token(kbd_tokens[bit]);
1377 buffer = dell_smbios_get_buffer();
1378 buffer->input[0] = token->location;
1379 buffer->input[1] = token->value;
1380 dell_smbios_send_request(1, 0);
1381 ret = buffer->output[0];
1382 dell_smbios_release_buffer();
1384 return dell_smbios_error(ret);
1387 static int kbd_get_token_bit(u8 bit)
1389 struct calling_interface_buffer *buffer;
1390 struct calling_interface_token *token;
1394 if (bit >= ARRAY_SIZE(kbd_tokens))
1397 token = dell_smbios_find_token(kbd_tokens[bit]);
1401 buffer = dell_smbios_get_buffer();
1402 buffer->input[0] = token->location;
1403 dell_smbios_send_request(0, 0);
1404 ret = buffer->output[0];
1405 val = buffer->output[1];
1406 dell_smbios_release_buffer();
1409 return dell_smbios_error(ret);
1411 return (val == token->value);
1414 static int kbd_get_first_active_token_bit(void)
1419 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1420 ret = kbd_get_token_bit(i);
1428 static int kbd_get_valid_token_counts(void)
1430 return hweight16(kbd_token_bits);
1433 static inline int kbd_init_info(void)
1435 struct kbd_state state;
1439 ret = kbd_get_info(&kbd_info);
1443 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1444 * timeout value which is shared for both battery and AC power
1445 * settings. So do not try to set AC values on old models.
1447 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1448 kbd_timeout_ac_supported = true;
1450 kbd_get_state(&state);
1452 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1453 if (kbd_info.seconds > 63)
1454 kbd_info.seconds = 63;
1455 if (kbd_info.minutes > 63)
1456 kbd_info.minutes = 63;
1457 if (kbd_info.hours > 63)
1458 kbd_info.hours = 63;
1459 if (kbd_info.days > 63)
1462 /* NOTE: On tested machines ON mode did not work and caused
1463 * problems (turned backlight off) so do not use it
1465 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1467 kbd_previous_level = kbd_get_level(&state);
1468 kbd_previous_mode_bit = state.mode_bit;
1470 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1471 kbd_previous_level = 1;
1473 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1474 kbd_previous_mode_bit =
1475 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1476 if (kbd_previous_mode_bit != 0)
1477 kbd_previous_mode_bit--;
1480 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1481 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1482 kbd_als_supported = true;
1484 if (kbd_info.modes & (
1485 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1486 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1487 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1489 kbd_triggers_supported = true;
1491 /* kbd_mode_levels[0] is reserved, see below */
1492 for (i = 0; i < 16; ++i)
1493 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1494 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1497 * Find the first supported mode and assign to kbd_mode_levels[0].
1498 * This should be 0 (off), but we cannot depend on the BIOS to
1501 if (kbd_mode_levels_count > 0) {
1502 for (i = 0; i < 16; ++i) {
1503 if (BIT(i) & kbd_info.modes) {
1504 kbd_mode_levels[0] = i;
1508 kbd_mode_levels_count++;
1515 static inline void kbd_init_tokens(void)
1519 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1520 if (dell_smbios_find_token(kbd_tokens[i]))
1521 kbd_token_bits |= BIT(i);
1524 static void kbd_init(void)
1528 ret = kbd_init_info();
1532 * Only supports keyboard backlight when it has at least two modes.
1534 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1535 || kbd_get_valid_token_counts() >= 2)
1536 kbd_led_present = true;
1539 static ssize_t kbd_led_timeout_store(struct device *dev,
1540 struct device_attribute *attr,
1541 const char *buf, size_t count)
1543 struct kbd_state new_state;
1544 struct kbd_state state;
1552 ret = sscanf(buf, "%d %c", &value, &ch);
1565 if (value > kbd_info.seconds)
1567 unit = KBD_TIMEOUT_SECONDS;
1570 if (value > kbd_info.minutes)
1572 unit = KBD_TIMEOUT_MINUTES;
1575 if (value > kbd_info.hours)
1577 unit = KBD_TIMEOUT_HOURS;
1580 if (value > kbd_info.days)
1582 unit = KBD_TIMEOUT_DAYS;
1588 if (quirks && quirks->needs_kbd_timeouts)
1592 /* Convert value from current units to seconds */
1594 case KBD_TIMEOUT_DAYS:
1596 case KBD_TIMEOUT_HOURS:
1598 case KBD_TIMEOUT_MINUTES:
1600 unit = KBD_TIMEOUT_SECONDS;
1603 if (quirks && quirks->needs_kbd_timeouts) {
1604 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1605 if (value <= quirks->kbd_timeouts[i]) {
1606 value = quirks->kbd_timeouts[i];
1612 if (value <= kbd_info.seconds && kbd_info.seconds) {
1613 unit = KBD_TIMEOUT_SECONDS;
1614 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1616 unit = KBD_TIMEOUT_MINUTES;
1617 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1619 unit = KBD_TIMEOUT_HOURS;
1620 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1621 value /= (60 * 60 * 24);
1622 unit = KBD_TIMEOUT_DAYS;
1628 mutex_lock(&kbd_led_mutex);
1630 ret = kbd_get_state(&state);
1636 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1637 new_state.timeout_value_ac = value;
1638 new_state.timeout_unit_ac = unit;
1640 new_state.timeout_value = value;
1641 new_state.timeout_unit = unit;
1644 ret = kbd_set_state_safe(&new_state, &state);
1650 mutex_unlock(&kbd_led_mutex);
1654 static ssize_t kbd_led_timeout_show(struct device *dev,
1655 struct device_attribute *attr, char *buf)
1657 struct kbd_state state;
1663 ret = kbd_get_state(&state);
1667 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1668 value = state.timeout_value_ac;
1669 unit = state.timeout_unit_ac;
1671 value = state.timeout_value;
1672 unit = state.timeout_unit;
1675 len = sprintf(buf, "%d", value);
1678 case KBD_TIMEOUT_SECONDS:
1679 return len + sprintf(buf+len, "s\n");
1680 case KBD_TIMEOUT_MINUTES:
1681 return len + sprintf(buf+len, "m\n");
1682 case KBD_TIMEOUT_HOURS:
1683 return len + sprintf(buf+len, "h\n");
1684 case KBD_TIMEOUT_DAYS:
1685 return len + sprintf(buf+len, "d\n");
1693 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1694 kbd_led_timeout_show, kbd_led_timeout_store);
1696 static const char * const kbd_led_triggers[] = {
1699 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1703 static ssize_t kbd_led_triggers_store(struct device *dev,
1704 struct device_attribute *attr,
1705 const char *buf, size_t count)
1707 struct kbd_state new_state;
1708 struct kbd_state state;
1709 bool triggers_enabled = false;
1710 int trigger_bit = -1;
1714 ret = sscanf(buf, "%20s", trigger);
1718 if (trigger[0] != '+' && trigger[0] != '-')
1721 mutex_lock(&kbd_led_mutex);
1723 ret = kbd_get_state(&state);
1727 if (kbd_triggers_supported)
1728 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1730 if (kbd_triggers_supported) {
1731 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1732 if (!(kbd_info.triggers & BIT(i)))
1734 if (!kbd_led_triggers[i])
1736 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1738 if (trigger[0] == '+' &&
1739 triggers_enabled && (state.triggers & BIT(i))) {
1743 if (trigger[0] == '-' &&
1744 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1753 if (trigger_bit == -1) {
1759 if (trigger[0] == '+')
1760 new_state.triggers |= BIT(trigger_bit);
1762 new_state.triggers &= ~BIT(trigger_bit);
1764 * NOTE: trackstick bit (2) must be disabled when
1765 * disabling touchpad bit (1), otherwise touchpad
1766 * bit (1) will not be disabled
1768 if (trigger_bit == 1)
1769 new_state.triggers &= ~BIT(2);
1771 if ((kbd_info.triggers & new_state.triggers) !=
1772 new_state.triggers) {
1776 if (new_state.triggers && !triggers_enabled) {
1777 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1778 kbd_set_level(&new_state, kbd_previous_level);
1779 } else if (new_state.triggers == 0) {
1780 kbd_set_level(&new_state, 0);
1782 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1786 ret = kbd_set_state_safe(&new_state, &state);
1789 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1790 kbd_previous_mode_bit = new_state.mode_bit;
1793 mutex_unlock(&kbd_led_mutex);
1797 static ssize_t kbd_led_triggers_show(struct device *dev,
1798 struct device_attribute *attr, char *buf)
1800 struct kbd_state state;
1801 bool triggers_enabled;
1805 ret = kbd_get_state(&state);
1811 if (kbd_triggers_supported) {
1812 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1813 level = kbd_get_level(&state);
1814 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1815 if (!(kbd_info.triggers & BIT(i)))
1817 if (!kbd_led_triggers[i])
1819 if ((triggers_enabled || level <= 0) &&
1820 (state.triggers & BIT(i)))
1824 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1829 buf[len - 1] = '\n';
1834 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1835 kbd_led_triggers_show, kbd_led_triggers_store);
1837 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1838 struct device_attribute *attr,
1839 const char *buf, size_t count)
1841 struct kbd_state new_state;
1842 struct kbd_state state;
1843 bool triggers_enabled = false;
1847 ret = kstrtoint(buf, 0, &enable);
1851 mutex_lock(&kbd_led_mutex);
1853 ret = kbd_get_state(&state);
1857 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1864 if (kbd_triggers_supported)
1865 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1868 if (triggers_enabled)
1869 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1871 new_state.mode_bit = KBD_MODE_BIT_ALS;
1873 if (triggers_enabled) {
1874 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1875 kbd_set_level(&new_state, kbd_previous_level);
1877 new_state.mode_bit = KBD_MODE_BIT_ON;
1880 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1885 ret = kbd_set_state_safe(&new_state, &state);
1888 kbd_previous_mode_bit = new_state.mode_bit;
1892 mutex_unlock(&kbd_led_mutex);
1896 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1897 struct device_attribute *attr,
1900 struct kbd_state state;
1901 bool enabled = false;
1904 ret = kbd_get_state(&state);
1907 enabled = kbd_is_als_mode_bit(state.mode_bit);
1909 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1912 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1913 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1915 static ssize_t kbd_led_als_setting_store(struct device *dev,
1916 struct device_attribute *attr,
1917 const char *buf, size_t count)
1919 struct kbd_state state;
1920 struct kbd_state new_state;
1924 ret = kstrtou8(buf, 10, &setting);
1928 mutex_lock(&kbd_led_mutex);
1930 ret = kbd_get_state(&state);
1935 new_state.als_setting = setting;
1937 ret = kbd_set_state_safe(&new_state, &state);
1943 mutex_unlock(&kbd_led_mutex);
1947 static ssize_t kbd_led_als_setting_show(struct device *dev,
1948 struct device_attribute *attr,
1951 struct kbd_state state;
1954 ret = kbd_get_state(&state);
1958 return sprintf(buf, "%d\n", state.als_setting);
1961 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1962 kbd_led_als_setting_show, kbd_led_als_setting_store);
1964 static struct attribute *kbd_led_attrs[] = {
1965 &dev_attr_stop_timeout.attr,
1966 &dev_attr_start_triggers.attr,
1970 static const struct attribute_group kbd_led_group = {
1971 .attrs = kbd_led_attrs,
1974 static struct attribute *kbd_led_als_attrs[] = {
1975 &dev_attr_als_enabled.attr,
1976 &dev_attr_als_setting.attr,
1980 static const struct attribute_group kbd_led_als_group = {
1981 .attrs = kbd_led_als_attrs,
1984 static const struct attribute_group *kbd_led_groups[] = {
1990 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1994 struct kbd_state state;
1996 if (kbd_get_max_level()) {
1997 ret = kbd_get_state(&state);
2000 ret = kbd_get_level(&state);
2006 if (kbd_get_valid_token_counts()) {
2007 ret = kbd_get_first_active_token_bit();
2010 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2011 num &= num - 1; /* clear the first bit set */
2014 return ffs(num) - 1;
2017 pr_warn("Keyboard brightness level control not supported\n");
2021 static int kbd_led_level_set(struct led_classdev *led_cdev,
2022 enum led_brightness value)
2024 enum led_brightness new_value = value;
2025 struct kbd_state state;
2026 struct kbd_state new_state;
2030 mutex_lock(&kbd_led_mutex);
2032 if (kbd_get_max_level()) {
2033 ret = kbd_get_state(&state);
2037 ret = kbd_set_level(&new_state, value);
2040 ret = kbd_set_state_safe(&new_state, &state);
2041 } else if (kbd_get_valid_token_counts()) {
2042 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2043 num &= num - 1; /* clear the first bit set */
2047 ret = kbd_set_token_bit(ffs(num) - 1);
2049 pr_warn("Keyboard brightness level control not supported\n");
2055 kbd_led_level = new_value;
2057 mutex_unlock(&kbd_led_mutex);
2061 static struct led_classdev kbd_led = {
2062 .name = "dell::kbd_backlight",
2063 .flags = LED_BRIGHT_HW_CHANGED,
2064 .brightness_set_blocking = kbd_led_level_set,
2065 .brightness_get = kbd_led_level_get,
2066 .groups = kbd_led_groups,
2069 static int __init kbd_led_init(struct device *dev)
2074 if (!kbd_led_present)
2076 if (!kbd_als_supported)
2077 kbd_led_groups[1] = NULL;
2078 kbd_led.max_brightness = kbd_get_max_level();
2079 if (!kbd_led.max_brightness) {
2080 kbd_led.max_brightness = kbd_get_valid_token_counts();
2081 if (kbd_led.max_brightness)
2082 kbd_led.max_brightness--;
2085 kbd_led_level = kbd_led_level_get(NULL);
2087 ret = led_classdev_register(dev, &kbd_led);
2089 kbd_led_present = false;
2094 static void brightness_set_exit(struct led_classdev *led_cdev,
2095 enum led_brightness value)
2097 /* Don't change backlight level on exit */
2100 static void kbd_led_exit(void)
2102 if (!kbd_led_present)
2104 kbd_led.brightness_set = brightness_set_exit;
2105 led_classdev_unregister(&kbd_led);
2108 static int dell_laptop_notifier_call(struct notifier_block *nb,
2109 unsigned long action, void *data)
2111 bool changed = false;
2112 enum led_brightness new_kbd_led_level;
2115 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2116 if (!kbd_led_present)
2119 mutex_lock(&kbd_led_mutex);
2120 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2121 if (kbd_led_level != new_kbd_led_level) {
2122 kbd_led_level = new_kbd_led_level;
2125 mutex_unlock(&kbd_led_mutex);
2128 led_classdev_notify_brightness_hw_changed(&kbd_led,
2136 static struct notifier_block dell_laptop_notifier = {
2137 .notifier_call = dell_laptop_notifier_call,
2140 int dell_micmute_led_set(int state)
2142 struct calling_interface_buffer *buffer;
2143 struct calling_interface_token *token;
2146 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2147 else if (state == 1)
2148 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2155 buffer = dell_smbios_get_buffer();
2156 buffer->input[0] = token->location;
2157 buffer->input[1] = token->value;
2158 dell_smbios_send_request(1, 0);
2159 dell_smbios_release_buffer();
2163 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2165 static int __init dell_init(void)
2167 struct calling_interface_buffer *buffer;
2168 struct calling_interface_token *token;
2169 int max_intensity = 0;
2172 if (!dmi_check_system(dell_device_table))
2176 /* find if this machine support other functions */
2177 dmi_check_system(dell_quirks);
2179 ret = platform_driver_register(&platform_driver);
2181 goto fail_platform_driver;
2182 platform_device = platform_device_alloc("dell-laptop", -1);
2183 if (!platform_device) {
2185 goto fail_platform_device1;
2187 ret = platform_device_add(platform_device);
2189 goto fail_platform_device2;
2191 ret = dell_setup_rfkill();
2194 pr_warn("Unable to setup rfkill\n");
2198 if (quirks && quirks->touchpad_led)
2199 touchpad_led_init(&platform_device->dev);
2201 kbd_led_init(&platform_device->dev);
2203 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2204 if (dell_laptop_dir != NULL)
2205 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2206 &dell_debugfs_fops);
2208 dell_laptop_register_notifier(&dell_laptop_notifier);
2210 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2213 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2215 buffer = dell_smbios_get_buffer();
2216 buffer->input[0] = token->location;
2217 dell_smbios_send_request(0, 2);
2218 if (buffer->output[0] == 0)
2219 max_intensity = buffer->output[3];
2220 dell_smbios_release_buffer();
2223 if (max_intensity) {
2224 struct backlight_properties props;
2225 memset(&props, 0, sizeof(struct backlight_properties));
2226 props.type = BACKLIGHT_PLATFORM;
2227 props.max_brightness = max_intensity;
2228 dell_backlight_device = backlight_device_register("dell_backlight",
2229 &platform_device->dev,
2234 if (IS_ERR(dell_backlight_device)) {
2235 ret = PTR_ERR(dell_backlight_device);
2236 dell_backlight_device = NULL;
2237 goto fail_backlight;
2240 dell_backlight_device->props.brightness =
2241 dell_get_intensity(dell_backlight_device);
2242 if (dell_backlight_device->props.brightness < 0) {
2243 ret = dell_backlight_device->props.brightness;
2244 goto fail_get_brightness;
2246 backlight_update_status(dell_backlight_device);
2251 fail_get_brightness:
2252 backlight_device_unregister(dell_backlight_device);
2254 dell_cleanup_rfkill();
2256 platform_device_del(platform_device);
2257 fail_platform_device2:
2258 platform_device_put(platform_device);
2259 fail_platform_device1:
2260 platform_driver_unregister(&platform_driver);
2261 fail_platform_driver:
2265 static void __exit dell_exit(void)
2267 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2268 debugfs_remove_recursive(dell_laptop_dir);
2269 if (quirks && quirks->touchpad_led)
2270 touchpad_led_exit();
2272 backlight_device_unregister(dell_backlight_device);
2273 dell_cleanup_rfkill();
2274 if (platform_device) {
2275 platform_device_unregister(platform_device);
2276 platform_driver_unregister(&platform_driver);
2280 /* dell-rbtn.c driver export functions which will not work correctly (and could
2281 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2282 * not problem when dell-rbtn.c is compiled as external module. When both files
2283 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2284 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2285 * This can be achieved by late_initcall() instead module_init().
2287 late_initcall(dell_init);
2288 module_exit(dell_exit);
2290 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2291 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2292 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2293 MODULE_DESCRIPTION("Dell laptop driver");
2294 MODULE_LICENSE("GPL");