2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
40 bool kbd_led_levels_off_1;
42 bool needs_kbd_timeouts;
44 * Ordered list of timeouts expressed in seconds.
45 * The list must end with -1
50 static struct quirk_entry *quirks;
52 static struct quirk_entry quirk_dell_vostro_v130 = {
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
58 quirks = dmi->driver_data;
63 * These values come from Windows utility provided by Dell. If any other value
64 * is used then BIOS silently set timeout to 0 without any error message.
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 .needs_kbd_timeouts = true,
68 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
71 static struct quirk_entry quirk_dell_latitude_e6410 = {
72 .kbd_led_levels_off_1 = true,
75 static struct platform_driver platform_driver = {
77 .name = "dell-laptop",
81 static struct platform_device *platform_device;
82 static struct backlight_device *dell_backlight_device;
83 static struct rfkill *wifi_rfkill;
84 static struct rfkill *bluetooth_rfkill;
85 static struct rfkill *wwan_rfkill;
86 static bool force_rfkill;
88 module_param(force_rfkill, bool, 0444);
89 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
91 static const struct dmi_system_id dell_device_table[] __initconst = {
93 .ident = "Dell laptop",
95 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
96 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
120 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
125 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
126 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
137 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
138 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
144 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
148 .ident = "Dell Computer Corporation",
150 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
151 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
156 MODULE_DEVICE_TABLE(dmi, dell_device_table);
158 static const struct dmi_system_id dell_quirks[] __initconst = {
160 .callback = dmi_matched,
161 .ident = "Dell Vostro V130",
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
166 .driver_data = &quirk_dell_vostro_v130,
169 .callback = dmi_matched,
170 .ident = "Dell Vostro V131",
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
175 .driver_data = &quirk_dell_vostro_v130,
178 .callback = dmi_matched,
179 .ident = "Dell Vostro 3350",
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
184 .driver_data = &quirk_dell_vostro_v130,
187 .callback = dmi_matched,
188 .ident = "Dell Vostro 3555",
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
193 .driver_data = &quirk_dell_vostro_v130,
196 .callback = dmi_matched,
197 .ident = "Dell Inspiron N311z",
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
202 .driver_data = &quirk_dell_vostro_v130,
205 .callback = dmi_matched,
206 .ident = "Dell Inspiron M5110",
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
209 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
211 .driver_data = &quirk_dell_vostro_v130,
214 .callback = dmi_matched,
215 .ident = "Dell Vostro 3360",
217 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
218 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
220 .driver_data = &quirk_dell_vostro_v130,
223 .callback = dmi_matched,
224 .ident = "Dell Vostro 3460",
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
229 .driver_data = &quirk_dell_vostro_v130,
232 .callback = dmi_matched,
233 .ident = "Dell Vostro 3560",
235 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
236 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
238 .driver_data = &quirk_dell_vostro_v130,
241 .callback = dmi_matched,
242 .ident = "Dell Vostro 3450",
244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
245 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
247 .driver_data = &quirk_dell_vostro_v130,
250 .callback = dmi_matched,
251 .ident = "Dell Inspiron 5420",
253 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
256 .driver_data = &quirk_dell_vostro_v130,
259 .callback = dmi_matched,
260 .ident = "Dell Inspiron 5520",
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
263 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
265 .driver_data = &quirk_dell_vostro_v130,
268 .callback = dmi_matched,
269 .ident = "Dell Inspiron 5720",
271 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
272 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
274 .driver_data = &quirk_dell_vostro_v130,
277 .callback = dmi_matched,
278 .ident = "Dell Inspiron 7420",
280 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
281 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
283 .driver_data = &quirk_dell_vostro_v130,
286 .callback = dmi_matched,
287 .ident = "Dell Inspiron 7520",
289 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
290 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
292 .driver_data = &quirk_dell_vostro_v130,
295 .callback = dmi_matched,
296 .ident = "Dell Inspiron 7720",
298 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
299 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
301 .driver_data = &quirk_dell_vostro_v130,
304 .callback = dmi_matched,
305 .ident = "Dell XPS13 9333",
307 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
310 .driver_data = &quirk_dell_xps13_9333,
313 .callback = dmi_matched,
314 .ident = "Dell Latitude E6410",
316 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
319 .driver_data = &quirk_dell_latitude_e6410,
324 static void dell_fill_request(struct calling_interface_buffer *buffer,
325 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
327 memset(buffer, 0, sizeof(struct calling_interface_buffer));
328 buffer->input[0] = arg0;
329 buffer->input[1] = arg1;
330 buffer->input[2] = arg2;
331 buffer->input[3] = arg3;
334 static int dell_send_request(struct calling_interface_buffer *buffer,
335 u16 class, u16 select)
339 buffer->cmd_class = class;
340 buffer->cmd_select = select;
341 ret = dell_smbios_call(buffer);
344 return dell_smbios_error(buffer->output[0]);
348 * Derived from information in smbios-wireless-ctl:
350 * cbSelect 17, Value 11
352 * Return Wireless Info
353 * cbArg1, byte0 = 0x00
355 * cbRes1 Standard return codes (0, -1, -2)
356 * cbRes2 Info bit flags:
358 * 0 Hardware switch supported (1)
359 * 1 WiFi locator supported (1)
360 * 2 WLAN supported (1)
361 * 3 Bluetooth (BT) supported (1)
362 * 4 WWAN supported (1)
363 * 5 Wireless KBD supported (1)
364 * 6 Uw b supported (1)
365 * 7 WiGig supported (1)
366 * 8 WLAN installed (1)
368 * 10 WWAN installed (1)
369 * 11 Uw b installed (1)
370 * 12 WiGig installed (1)
372 * 16 Hardware (HW) switch is On (1)
373 * 17 WLAN disabled (1)
375 * 19 WWAN disabled (1)
376 * 20 Uw b disabled (1)
377 * 21 WiGig disabled (1)
380 * cbRes3 NVRAM size in bytes
381 * cbRes4, byte 0 NVRAM format version number
384 * Set QuickSet Radio Disable Flag
385 * cbArg1, byte0 = 0x01
394 * cbArg1, byte2 Flag bits:
395 * 0 QuickSet disables radio (1)
398 * cbRes1 Standard return codes (0, -1, -2)
399 * cbRes2 QuickSet (QS) radio disable bit map:
404 * 4 QS disables WIGIG
407 * Wireless Switch Configuration
408 * cbArg1, byte0 = 0x02
414 * 2 Set WiFi locator enable/disable
416 * Switch settings (if byte 1==1):
417 * 0 WLAN sw itch control (1)
418 * 1 BT sw itch control (1)
419 * 2 WWAN sw itch control (1)
420 * 3 UWB sw itch control (1)
421 * 4 WiGig sw itch control (1)
423 * cbArg1, byte2 Enable bits (if byte 1==2):
424 * 0 Enable WiFi locator (1)
426 * cbRes1 Standard return codes (0, -1, -2)
427 * cbRes2 QuickSet radio disable bit map:
428 * 0 WLAN controlled by sw itch (1)
429 * 1 BT controlled by sw itch (1)
430 * 2 WWAN controlled by sw itch (1)
431 * 3 UWB controlled by sw itch (1)
432 * 4 WiGig controlled by sw itch (1)
434 * 7 Wireless sw itch config locked (1)
435 * 8 WiFi locator enabled (1)
437 * 15 WiFi locator setting locked (1)
440 * Read Local Config Data (LCD)
441 * cbArg1, byte0 = 0x10
442 * cbArg1, byte1 NVRAM index low byte
443 * cbArg1, byte2 NVRAM index high byte
444 * cbRes1 Standard return codes (0, -1, -2)
445 * cbRes2 4 bytes read from LCD[index]
446 * cbRes3 4 bytes read from LCD[index+4]
447 * cbRes4 4 bytes read from LCD[index+8]
449 * Write Local Config Data (LCD)
450 * cbArg1, byte0 = 0x11
451 * cbArg1, byte1 NVRAM index low byte
452 * cbArg1, byte2 NVRAM index high byte
453 * cbArg2 4 bytes to w rite at LCD[index]
454 * cbArg3 4 bytes to w rite at LCD[index+4]
455 * cbArg4 4 bytes to w rite at LCD[index+8]
456 * cbRes1 Standard return codes (0, -1, -2)
458 * Populate Local Config Data from NVRAM
459 * cbArg1, byte0 = 0x12
460 * cbRes1 Standard return codes (0, -1, -2)
462 * Commit Local Config Data to NVRAM
463 * cbArg1, byte0 = 0x13
464 * cbRes1 Standard return codes (0, -1, -2)
467 static int dell_rfkill_set(void *data, bool blocked)
469 int disable = blocked ? 1 : 0;
470 unsigned long radio = (unsigned long)data;
471 int hwswitch_bit = (unsigned long)data - 1;
472 struct calling_interface_buffer buffer;
477 dell_fill_request(&buffer, 0, 0, 0, 0);
478 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
481 status = buffer.output[1];
483 dell_fill_request(&buffer, 0x2, 0, 0, 0);
484 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
487 hwswitch = buffer.output[1];
489 /* If the hardware switch controls this radio, and the hardware
490 switch is disabled, always disable the radio */
491 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
492 (status & BIT(0)) && !(status & BIT(16)))
495 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
496 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
500 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
503 if (status & BIT(0)) {
504 /* Has hw-switch, sync sw_state to BIOS */
505 struct calling_interface_buffer buffer;
506 int block = rfkill_blocked(rfkill);
507 dell_fill_request(&buffer,
508 1 | (radio << 8) | (block << 16), 0, 0, 0);
509 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
511 /* No hw-switch, sync BIOS state to sw_state */
512 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
516 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
517 int status, int hwswitch)
519 if (hwswitch & (BIT(radio - 1)))
520 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
523 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
525 int radio = ((unsigned long)data & 0xF);
526 struct calling_interface_buffer buffer;
531 dell_fill_request(&buffer, 0, 0, 0, 0);
532 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
533 status = buffer.output[1];
535 if (ret != 0 || !(status & BIT(0))) {
539 dell_fill_request(&buffer, 0, 0x2, 0, 0);
540 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
541 hwswitch = buffer.output[1];
546 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
549 static const struct rfkill_ops dell_rfkill_ops = {
550 .set_block = dell_rfkill_set,
551 .query = dell_rfkill_query,
554 static struct dentry *dell_laptop_dir;
556 static int dell_debugfs_show(struct seq_file *s, void *data)
558 struct calling_interface_buffer buffer;
564 dell_fill_request(&buffer, 0, 0, 0, 0);
565 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
568 status = buffer.output[1];
570 dell_fill_request(&buffer, 0, 0x2, 0, 0);
571 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
574 hwswitch_state = buffer.output[1];
576 seq_printf(s, "return:\t%d\n", ret);
577 seq_printf(s, "status:\t0x%X\n", status);
578 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
580 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
581 (status & BIT(1)) >> 1);
582 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
583 (status & BIT(2)) >> 2);
584 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
585 (status & BIT(3)) >> 3);
586 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
587 (status & BIT(4)) >> 4);
588 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
589 (status & BIT(5)) >> 5);
590 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
591 (status & BIT(6)) >> 6);
592 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
593 (status & BIT(7)) >> 7);
594 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
595 (status & BIT(8)) >> 8);
596 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
597 (status & BIT(9)) >> 9);
598 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
599 (status & BIT(10)) >> 10);
600 seq_printf(s, "Bit 11: UWB installed: %lu\n",
601 (status & BIT(11)) >> 11);
602 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
603 (status & BIT(12)) >> 12);
605 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
606 (status & BIT(16)) >> 16);
607 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
608 (status & BIT(17)) >> 17);
609 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
610 (status & BIT(18)) >> 18);
611 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
612 (status & BIT(19)) >> 19);
613 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
614 (status & BIT(20)) >> 20);
615 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
616 (status & BIT(21)) >> 21);
618 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
619 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
620 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
621 hwswitch_state & BIT(0));
622 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
623 (hwswitch_state & BIT(1)) >> 1);
624 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
625 (hwswitch_state & BIT(2)) >> 2);
626 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
627 (hwswitch_state & BIT(3)) >> 3);
628 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
629 (hwswitch_state & BIT(4)) >> 4);
630 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
631 (hwswitch_state & BIT(7)) >> 7);
632 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
633 (hwswitch_state & BIT(8)) >> 8);
634 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
635 (hwswitch_state & BIT(15)) >> 15);
640 static int dell_debugfs_open(struct inode *inode, struct file *file)
642 return single_open(file, dell_debugfs_show, inode->i_private);
645 static const struct file_operations dell_debugfs_fops = {
646 .owner = THIS_MODULE,
647 .open = dell_debugfs_open,
650 .release = single_release,
653 static void dell_update_rfkill(struct work_struct *ignored)
655 struct calling_interface_buffer buffer;
660 dell_fill_request(&buffer, 0, 0, 0, 0);
661 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
662 status = buffer.output[1];
667 dell_fill_request(&buffer, 0, 0x2, 0, 0);
668 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
670 if (ret == 0 && (status & BIT(0)))
671 hwswitch = buffer.output[1];
674 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
675 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
677 if (bluetooth_rfkill) {
678 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
680 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
683 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
684 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
687 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
689 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
692 static bool extended;
694 if (str & I8042_STR_AUXDATA)
697 if (unlikely(data == 0xe0)) {
700 } else if (unlikely(extended)) {
703 schedule_delayed_work(&dell_rfkill_work,
704 round_jiffies_relative(HZ / 4));
713 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
714 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
716 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
717 unsigned long action, void *data)
719 schedule_delayed_work(&dell_rfkill_work, 0);
723 static struct notifier_block dell_laptop_rbtn_notifier = {
724 .notifier_call = dell_laptop_rbtn_notifier_call,
727 static int __init dell_setup_rfkill(void)
729 struct calling_interface_buffer buffer;
730 int status, ret, whitelisted;
734 * rfkill support causes trouble on various models, mostly Inspirons.
735 * So we whitelist certain series, and don't support rfkill on others.
738 product = dmi_get_system_info(DMI_PRODUCT_NAME);
739 if (product && (strncmp(product, "Latitude", 8) == 0 ||
740 strncmp(product, "Precision", 9) == 0))
742 if (!force_rfkill && !whitelisted)
745 dell_fill_request(&buffer, 0, 0, 0, 0);
746 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
747 status = buffer.output[1];
749 /* dell wireless info smbios call is not supported */
753 /* rfkill is only tested on laptops with a hwswitch */
754 if (!(status & BIT(0)) && !force_rfkill)
757 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
758 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
760 &dell_rfkill_ops, (void *) 1);
765 ret = rfkill_register(wifi_rfkill);
770 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
771 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
772 &platform_device->dev,
773 RFKILL_TYPE_BLUETOOTH,
774 &dell_rfkill_ops, (void *) 2);
775 if (!bluetooth_rfkill) {
779 ret = rfkill_register(bluetooth_rfkill);
784 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
785 wwan_rfkill = rfkill_alloc("dell-wwan",
786 &platform_device->dev,
788 &dell_rfkill_ops, (void *) 3);
793 ret = rfkill_register(wwan_rfkill);
799 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
800 * which can receive events from HW slider switch.
802 * Dell SMBIOS on whitelisted models supports controlling radio devices
803 * but does not support receiving HW button switch events. We can use
804 * i8042 filter hook function to receive keyboard data and handle
805 * keycode for HW button.
807 * So if it is possible we will use Dell Airplane Mode Switch ACPI
808 * driver for receiving HW events and Dell SMBIOS for setting rfkill
809 * states. If ACPI driver or device is not available we will fallback to
810 * i8042 filter hook function.
812 * To prevent duplicate rfkill devices which control and do same thing,
813 * dell-rbtn driver will automatically remove its own rfkill devices
814 * once function dell_rbtn_notifier_register() is called.
817 dell_rbtn_notifier_register_func =
818 symbol_request(dell_rbtn_notifier_register);
819 if (dell_rbtn_notifier_register_func) {
820 dell_rbtn_notifier_unregister_func =
821 symbol_request(dell_rbtn_notifier_unregister);
822 if (!dell_rbtn_notifier_unregister_func) {
823 symbol_put(dell_rbtn_notifier_register);
824 dell_rbtn_notifier_register_func = NULL;
828 if (dell_rbtn_notifier_register_func) {
829 ret = dell_rbtn_notifier_register_func(
830 &dell_laptop_rbtn_notifier);
831 symbol_put(dell_rbtn_notifier_register);
832 dell_rbtn_notifier_register_func = NULL;
834 symbol_put(dell_rbtn_notifier_unregister);
835 dell_rbtn_notifier_unregister_func = NULL;
838 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
843 pr_info("Using dell-rbtn acpi driver for receiving events\n");
844 } else if (ret != -ENODEV) {
845 pr_warn("Unable to register dell rbtn notifier\n");
848 ret = i8042_install_filter(dell_laptop_i8042_filter);
850 pr_warn("Unable to install key filter\n");
853 pr_info("Using i8042 filter function for receiving events\n");
859 rfkill_unregister(wwan_rfkill);
861 rfkill_destroy(wwan_rfkill);
862 if (bluetooth_rfkill)
863 rfkill_unregister(bluetooth_rfkill);
865 rfkill_destroy(bluetooth_rfkill);
867 rfkill_unregister(wifi_rfkill);
869 rfkill_destroy(wifi_rfkill);
874 static void dell_cleanup_rfkill(void)
876 if (dell_rbtn_notifier_unregister_func) {
877 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
878 symbol_put(dell_rbtn_notifier_unregister);
879 dell_rbtn_notifier_unregister_func = NULL;
881 i8042_remove_filter(dell_laptop_i8042_filter);
883 cancel_delayed_work_sync(&dell_rfkill_work);
885 rfkill_unregister(wifi_rfkill);
886 rfkill_destroy(wifi_rfkill);
888 if (bluetooth_rfkill) {
889 rfkill_unregister(bluetooth_rfkill);
890 rfkill_destroy(bluetooth_rfkill);
893 rfkill_unregister(wwan_rfkill);
894 rfkill_destroy(wwan_rfkill);
898 static int dell_send_intensity(struct backlight_device *bd)
900 struct calling_interface_buffer buffer;
901 struct calling_interface_token *token;
904 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
908 dell_fill_request(&buffer,
909 token->location, bd->props.brightness, 0, 0);
910 if (power_supply_is_system_supplied() > 0)
911 ret = dell_send_request(&buffer,
912 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
914 ret = dell_send_request(&buffer,
915 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
920 static int dell_get_intensity(struct backlight_device *bd)
922 struct calling_interface_buffer buffer;
923 struct calling_interface_token *token;
926 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
930 dell_fill_request(&buffer, token->location, 0, 0, 0);
931 if (power_supply_is_system_supplied() > 0)
932 ret = dell_send_request(&buffer,
933 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
935 ret = dell_send_request(&buffer,
936 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
939 ret = buffer.output[1];
944 static const struct backlight_ops dell_ops = {
945 .get_brightness = dell_get_intensity,
946 .update_status = dell_send_intensity,
949 static void touchpad_led_on(void)
953 i8042_command(&data, command | 1 << 12);
956 static void touchpad_led_off(void)
960 i8042_command(&data, command | 1 << 12);
963 static void touchpad_led_set(struct led_classdev *led_cdev,
964 enum led_brightness value)
972 static struct led_classdev touchpad_led = {
973 .name = "dell-laptop::touchpad",
974 .brightness_set = touchpad_led_set,
975 .flags = LED_CORE_SUSPENDRESUME,
978 static int __init touchpad_led_init(struct device *dev)
980 return led_classdev_register(dev, &touchpad_led);
983 static void touchpad_led_exit(void)
985 led_classdev_unregister(&touchpad_led);
989 * Derived from information in smbios-keyboard-ctl:
993 * Keyboard illumination
994 * cbArg1 determines the function to be performed
996 * cbArg1 0x0 = Get Feature Information
997 * cbRES1 Standard return codes (0, -1, -2)
998 * cbRES2, word0 Bitmap of user-selectable modes
999 * bit 0 Always off (All systems)
1000 * bit 1 Always on (Travis ATG, Siberia)
1001 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1002 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1003 * bit 4 Auto: Input-activity-based On; input-activity based Off
1004 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1005 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1006 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1007 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1008 * bits 9-15 Reserved for future use
1009 * cbRES2, byte2 Reserved for future use
1010 * cbRES2, byte3 Keyboard illumination type
1014 * 3-255 Reserved for future use
1015 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1016 * bit 0 Any keystroke
1017 * bit 1 Touchpad activity
1018 * bit 2 Pointing stick
1020 * bits 4-7 Reserved for future use
1021 * cbRES3, byte1 Supported timeout unit bitmap
1026 * bits 4-7 Reserved for future use
1027 * cbRES3, byte2 Number of keyboard light brightness levels
1028 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1029 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1030 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1031 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1033 * cbArg1 0x1 = Get Current State
1034 * cbRES1 Standard return codes (0, -1, -2)
1035 * cbRES2, word0 Bitmap of current mode state
1036 * bit 0 Always off (All systems)
1037 * bit 1 Always on (Travis ATG, Siberia)
1038 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1039 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1040 * bit 4 Auto: Input-activity-based On; input-activity based Off
1041 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1042 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1043 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1044 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1045 * bits 9-15 Reserved for future use
1046 * Note: Only One bit can be set
1047 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1048 * bit 0 Any keystroke
1049 * bit 1 Touchpad activity
1050 * bit 2 Pointing stick
1052 * bits 4-7 Reserved for future use
1053 * cbRES2, byte3 Current Timeout on battery
1054 * bits 7:6 Timeout units indicator:
1059 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1060 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1061 * are set upon return from the [Get feature information] call.
1062 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1063 * cbRES3, byte1 Current ALS reading
1064 * cbRES3, byte2 Current keyboard light level.
1065 * cbRES3, byte3 Current timeout on AC Power
1066 * bits 7:6 Timeout units indicator:
1071 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1072 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1073 * are set upon return from the upon return from the [Get Feature information] call.
1075 * cbArg1 0x2 = Set New State
1076 * cbRES1 Standard return codes (0, -1, -2)
1077 * cbArg2, word0 Bitmap of current mode state
1078 * bit 0 Always off (All systems)
1079 * bit 1 Always on (Travis ATG, Siberia)
1080 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1081 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1082 * bit 4 Auto: Input-activity-based On; input-activity based Off
1083 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1084 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1085 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1086 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1087 * bits 9-15 Reserved for future use
1088 * Note: Only One bit can be set
1089 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1090 * keyboard to turn off automatically.
1091 * bit 0 Any keystroke
1092 * bit 1 Touchpad activity
1093 * bit 2 Pointing stick
1095 * bits 4-7 Reserved for future use
1096 * cbArg2, byte3 Desired Timeout on battery
1097 * bits 7:6 Timeout units indicator:
1102 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1103 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1104 * cbArg3, byte2 Desired keyboard light level.
1105 * cbArg3, byte3 Desired Timeout on AC power
1106 * bits 7:6 Timeout units indicator:
1111 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1115 enum kbd_timeout_unit {
1116 KBD_TIMEOUT_SECONDS = 0,
1117 KBD_TIMEOUT_MINUTES,
1123 KBD_MODE_BIT_OFF = 0,
1126 KBD_MODE_BIT_TRIGGER_ALS,
1127 KBD_MODE_BIT_TRIGGER,
1128 KBD_MODE_BIT_TRIGGER_25,
1129 KBD_MODE_BIT_TRIGGER_50,
1130 KBD_MODE_BIT_TRIGGER_75,
1131 KBD_MODE_BIT_TRIGGER_100,
1134 #define kbd_is_als_mode_bit(bit) \
1135 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1136 #define kbd_is_trigger_mode_bit(bit) \
1137 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1138 #define kbd_is_level_mode_bit(bit) \
1139 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1157 u8 timeout_value_ac;
1164 static const int kbd_tokens[] = {
1166 KBD_LED_AUTO_25_TOKEN,
1167 KBD_LED_AUTO_50_TOKEN,
1168 KBD_LED_AUTO_75_TOKEN,
1169 KBD_LED_AUTO_100_TOKEN,
1173 static u16 kbd_token_bits;
1175 static struct kbd_info kbd_info;
1176 static bool kbd_als_supported;
1177 static bool kbd_triggers_supported;
1178 static bool kbd_timeout_ac_supported;
1180 static u8 kbd_mode_levels[16];
1181 static int kbd_mode_levels_count;
1183 static u8 kbd_previous_level;
1184 static u8 kbd_previous_mode_bit;
1186 static bool kbd_led_present;
1187 static DEFINE_MUTEX(kbd_led_mutex);
1188 static enum led_brightness kbd_led_level;
1191 * NOTE: there are three ways to set the keyboard backlight level.
1192 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1193 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1194 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1196 * There are laptops which support only one of these methods. If we want to
1197 * support as many machines as possible we need to implement all three methods.
1198 * The first two methods use the kbd_state structure. The third uses SMBIOS
1199 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1200 * keyboard backlight level via kbd_state.level.
1203 static int kbd_get_info(struct kbd_info *info)
1205 struct calling_interface_buffer buffer;
1209 dell_fill_request(&buffer, 0, 0, 0, 0);
1210 ret = dell_send_request(&buffer,
1211 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1215 info->modes = buffer.output[1] & 0xFFFF;
1216 info->type = (buffer.output[1] >> 24) & 0xFF;
1217 info->triggers = buffer.output[2] & 0xFF;
1218 units = (buffer.output[2] >> 8) & 0xFF;
1219 info->levels = (buffer.output[2] >> 16) & 0xFF;
1221 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1225 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1227 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1229 info->hours = (buffer.output[3] >> 16) & 0xFF;
1231 info->days = (buffer.output[3] >> 24) & 0xFF;
1236 static unsigned int kbd_get_max_level(void)
1238 if (kbd_info.levels != 0)
1239 return kbd_info.levels;
1240 if (kbd_mode_levels_count > 0)
1241 return kbd_mode_levels_count - 1;
1245 static int kbd_get_level(struct kbd_state *state)
1249 if (kbd_info.levels != 0)
1250 return state->level;
1252 if (kbd_mode_levels_count > 0) {
1253 for (i = 0; i < kbd_mode_levels_count; ++i)
1254 if (kbd_mode_levels[i] == state->mode_bit)
1262 static int kbd_set_level(struct kbd_state *state, u8 level)
1264 if (kbd_info.levels != 0) {
1266 kbd_previous_level = level;
1267 if (state->level == level)
1269 state->level = level;
1270 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1271 state->mode_bit = kbd_previous_mode_bit;
1272 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1273 kbd_previous_mode_bit = state->mode_bit;
1274 state->mode_bit = KBD_MODE_BIT_OFF;
1279 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1281 kbd_previous_level = level;
1282 state->mode_bit = kbd_mode_levels[level];
1289 static int kbd_get_state(struct kbd_state *state)
1291 struct calling_interface_buffer buffer;
1294 dell_fill_request(&buffer, 0, 0, 0, 0);
1295 ret = dell_send_request(&buffer,
1296 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1300 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1301 if (state->mode_bit != 0)
1304 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1305 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1306 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1307 state->als_setting = buffer.output[2] & 0xFF;
1308 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1309 state->level = (buffer.output[2] >> 16) & 0xFF;
1310 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1311 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1316 static int kbd_set_state(struct kbd_state *state)
1318 struct calling_interface_buffer buffer;
1323 input1 = BIT(state->mode_bit) & 0xFFFF;
1324 input1 |= (state->triggers & 0xFF) << 16;
1325 input1 |= (state->timeout_value & 0x3F) << 24;
1326 input1 |= (state->timeout_unit & 0x3) << 30;
1327 input2 = state->als_setting & 0xFF;
1328 input2 |= (state->level & 0xFF) << 16;
1329 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1330 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1331 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1332 ret = dell_send_request(&buffer,
1333 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1338 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1342 ret = kbd_set_state(state);
1347 * When setting the new state fails,try to restore the previous one.
1348 * This is needed on some machines where BIOS sets a default state when
1349 * setting a new state fails. This default state could be all off.
1352 if (kbd_set_state(old))
1353 pr_err("Setting old previous keyboard state failed\n");
1358 static int kbd_set_token_bit(u8 bit)
1360 struct calling_interface_buffer buffer;
1361 struct calling_interface_token *token;
1364 if (bit >= ARRAY_SIZE(kbd_tokens))
1367 token = dell_smbios_find_token(kbd_tokens[bit]);
1371 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1372 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1377 static int kbd_get_token_bit(u8 bit)
1379 struct calling_interface_buffer buffer;
1380 struct calling_interface_token *token;
1384 if (bit >= ARRAY_SIZE(kbd_tokens))
1387 token = dell_smbios_find_token(kbd_tokens[bit]);
1391 dell_fill_request(&buffer, token->location, 0, 0, 0);
1392 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1393 val = buffer.output[1];
1398 return (val == token->value);
1401 static int kbd_get_first_active_token_bit(void)
1406 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1407 ret = kbd_get_token_bit(i);
1415 static int kbd_get_valid_token_counts(void)
1417 return hweight16(kbd_token_bits);
1420 static inline int kbd_init_info(void)
1422 struct kbd_state state;
1426 ret = kbd_get_info(&kbd_info);
1430 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1431 * timeout value which is shared for both battery and AC power
1432 * settings. So do not try to set AC values on old models.
1434 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1435 kbd_timeout_ac_supported = true;
1437 kbd_get_state(&state);
1439 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1440 if (kbd_info.seconds > 63)
1441 kbd_info.seconds = 63;
1442 if (kbd_info.minutes > 63)
1443 kbd_info.minutes = 63;
1444 if (kbd_info.hours > 63)
1445 kbd_info.hours = 63;
1446 if (kbd_info.days > 63)
1449 /* NOTE: On tested machines ON mode did not work and caused
1450 * problems (turned backlight off) so do not use it
1452 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1454 kbd_previous_level = kbd_get_level(&state);
1455 kbd_previous_mode_bit = state.mode_bit;
1457 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1458 kbd_previous_level = 1;
1460 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1461 kbd_previous_mode_bit =
1462 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1463 if (kbd_previous_mode_bit != 0)
1464 kbd_previous_mode_bit--;
1467 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1468 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1469 kbd_als_supported = true;
1471 if (kbd_info.modes & (
1472 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1473 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1474 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1476 kbd_triggers_supported = true;
1478 /* kbd_mode_levels[0] is reserved, see below */
1479 for (i = 0; i < 16; ++i)
1480 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1481 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1484 * Find the first supported mode and assign to kbd_mode_levels[0].
1485 * This should be 0 (off), but we cannot depend on the BIOS to
1488 if (kbd_mode_levels_count > 0) {
1489 for (i = 0; i < 16; ++i) {
1490 if (BIT(i) & kbd_info.modes) {
1491 kbd_mode_levels[0] = i;
1495 kbd_mode_levels_count++;
1502 static inline void kbd_init_tokens(void)
1506 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1507 if (dell_smbios_find_token(kbd_tokens[i]))
1508 kbd_token_bits |= BIT(i);
1511 static void kbd_init(void)
1515 ret = kbd_init_info();
1519 * Only supports keyboard backlight when it has at least two modes.
1521 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1522 || kbd_get_valid_token_counts() >= 2)
1523 kbd_led_present = true;
1526 static ssize_t kbd_led_timeout_store(struct device *dev,
1527 struct device_attribute *attr,
1528 const char *buf, size_t count)
1530 struct kbd_state new_state;
1531 struct kbd_state state;
1539 ret = sscanf(buf, "%d %c", &value, &ch);
1552 if (value > kbd_info.seconds)
1554 unit = KBD_TIMEOUT_SECONDS;
1557 if (value > kbd_info.minutes)
1559 unit = KBD_TIMEOUT_MINUTES;
1562 if (value > kbd_info.hours)
1564 unit = KBD_TIMEOUT_HOURS;
1567 if (value > kbd_info.days)
1569 unit = KBD_TIMEOUT_DAYS;
1575 if (quirks && quirks->needs_kbd_timeouts)
1579 /* Convert value from current units to seconds */
1581 case KBD_TIMEOUT_DAYS:
1583 case KBD_TIMEOUT_HOURS:
1585 case KBD_TIMEOUT_MINUTES:
1587 unit = KBD_TIMEOUT_SECONDS;
1590 if (quirks && quirks->needs_kbd_timeouts) {
1591 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1592 if (value <= quirks->kbd_timeouts[i]) {
1593 value = quirks->kbd_timeouts[i];
1599 if (value <= kbd_info.seconds && kbd_info.seconds) {
1600 unit = KBD_TIMEOUT_SECONDS;
1601 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1603 unit = KBD_TIMEOUT_MINUTES;
1604 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1606 unit = KBD_TIMEOUT_HOURS;
1607 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1608 value /= (60 * 60 * 24);
1609 unit = KBD_TIMEOUT_DAYS;
1615 mutex_lock(&kbd_led_mutex);
1617 ret = kbd_get_state(&state);
1623 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1624 new_state.timeout_value_ac = value;
1625 new_state.timeout_unit_ac = unit;
1627 new_state.timeout_value = value;
1628 new_state.timeout_unit = unit;
1631 ret = kbd_set_state_safe(&new_state, &state);
1637 mutex_unlock(&kbd_led_mutex);
1641 static ssize_t kbd_led_timeout_show(struct device *dev,
1642 struct device_attribute *attr, char *buf)
1644 struct kbd_state state;
1650 ret = kbd_get_state(&state);
1654 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1655 value = state.timeout_value_ac;
1656 unit = state.timeout_unit_ac;
1658 value = state.timeout_value;
1659 unit = state.timeout_unit;
1662 len = sprintf(buf, "%d", value);
1665 case KBD_TIMEOUT_SECONDS:
1666 return len + sprintf(buf+len, "s\n");
1667 case KBD_TIMEOUT_MINUTES:
1668 return len + sprintf(buf+len, "m\n");
1669 case KBD_TIMEOUT_HOURS:
1670 return len + sprintf(buf+len, "h\n");
1671 case KBD_TIMEOUT_DAYS:
1672 return len + sprintf(buf+len, "d\n");
1680 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1681 kbd_led_timeout_show, kbd_led_timeout_store);
1683 static const char * const kbd_led_triggers[] = {
1686 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1690 static ssize_t kbd_led_triggers_store(struct device *dev,
1691 struct device_attribute *attr,
1692 const char *buf, size_t count)
1694 struct kbd_state new_state;
1695 struct kbd_state state;
1696 bool triggers_enabled = false;
1697 int trigger_bit = -1;
1701 ret = sscanf(buf, "%20s", trigger);
1705 if (trigger[0] != '+' && trigger[0] != '-')
1708 mutex_lock(&kbd_led_mutex);
1710 ret = kbd_get_state(&state);
1714 if (kbd_triggers_supported)
1715 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1717 if (kbd_triggers_supported) {
1718 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1719 if (!(kbd_info.triggers & BIT(i)))
1721 if (!kbd_led_triggers[i])
1723 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1725 if (trigger[0] == '+' &&
1726 triggers_enabled && (state.triggers & BIT(i))) {
1730 if (trigger[0] == '-' &&
1731 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1740 if (trigger_bit == -1) {
1746 if (trigger[0] == '+')
1747 new_state.triggers |= BIT(trigger_bit);
1749 new_state.triggers &= ~BIT(trigger_bit);
1751 * NOTE: trackstick bit (2) must be disabled when
1752 * disabling touchpad bit (1), otherwise touchpad
1753 * bit (1) will not be disabled
1755 if (trigger_bit == 1)
1756 new_state.triggers &= ~BIT(2);
1758 if ((kbd_info.triggers & new_state.triggers) !=
1759 new_state.triggers) {
1763 if (new_state.triggers && !triggers_enabled) {
1764 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1765 kbd_set_level(&new_state, kbd_previous_level);
1766 } else if (new_state.triggers == 0) {
1767 kbd_set_level(&new_state, 0);
1769 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1773 ret = kbd_set_state_safe(&new_state, &state);
1776 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1777 kbd_previous_mode_bit = new_state.mode_bit;
1780 mutex_unlock(&kbd_led_mutex);
1784 static ssize_t kbd_led_triggers_show(struct device *dev,
1785 struct device_attribute *attr, char *buf)
1787 struct kbd_state state;
1788 bool triggers_enabled;
1792 ret = kbd_get_state(&state);
1798 if (kbd_triggers_supported) {
1799 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1800 level = kbd_get_level(&state);
1801 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1802 if (!(kbd_info.triggers & BIT(i)))
1804 if (!kbd_led_triggers[i])
1806 if ((triggers_enabled || level <= 0) &&
1807 (state.triggers & BIT(i)))
1811 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1816 buf[len - 1] = '\n';
1821 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1822 kbd_led_triggers_show, kbd_led_triggers_store);
1824 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1825 struct device_attribute *attr,
1826 const char *buf, size_t count)
1828 struct kbd_state new_state;
1829 struct kbd_state state;
1830 bool triggers_enabled = false;
1834 ret = kstrtoint(buf, 0, &enable);
1838 mutex_lock(&kbd_led_mutex);
1840 ret = kbd_get_state(&state);
1844 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1851 if (kbd_triggers_supported)
1852 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1855 if (triggers_enabled)
1856 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1858 new_state.mode_bit = KBD_MODE_BIT_ALS;
1860 if (triggers_enabled) {
1861 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1862 kbd_set_level(&new_state, kbd_previous_level);
1864 new_state.mode_bit = KBD_MODE_BIT_ON;
1867 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1872 ret = kbd_set_state_safe(&new_state, &state);
1875 kbd_previous_mode_bit = new_state.mode_bit;
1879 mutex_unlock(&kbd_led_mutex);
1883 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1884 struct device_attribute *attr,
1887 struct kbd_state state;
1888 bool enabled = false;
1891 ret = kbd_get_state(&state);
1894 enabled = kbd_is_als_mode_bit(state.mode_bit);
1896 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1899 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1900 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1902 static ssize_t kbd_led_als_setting_store(struct device *dev,
1903 struct device_attribute *attr,
1904 const char *buf, size_t count)
1906 struct kbd_state state;
1907 struct kbd_state new_state;
1911 ret = kstrtou8(buf, 10, &setting);
1915 mutex_lock(&kbd_led_mutex);
1917 ret = kbd_get_state(&state);
1922 new_state.als_setting = setting;
1924 ret = kbd_set_state_safe(&new_state, &state);
1930 mutex_unlock(&kbd_led_mutex);
1934 static ssize_t kbd_led_als_setting_show(struct device *dev,
1935 struct device_attribute *attr,
1938 struct kbd_state state;
1941 ret = kbd_get_state(&state);
1945 return sprintf(buf, "%d\n", state.als_setting);
1948 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1949 kbd_led_als_setting_show, kbd_led_als_setting_store);
1951 static struct attribute *kbd_led_attrs[] = {
1952 &dev_attr_stop_timeout.attr,
1953 &dev_attr_start_triggers.attr,
1957 static const struct attribute_group kbd_led_group = {
1958 .attrs = kbd_led_attrs,
1961 static struct attribute *kbd_led_als_attrs[] = {
1962 &dev_attr_als_enabled.attr,
1963 &dev_attr_als_setting.attr,
1967 static const struct attribute_group kbd_led_als_group = {
1968 .attrs = kbd_led_als_attrs,
1971 static const struct attribute_group *kbd_led_groups[] = {
1977 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1981 struct kbd_state state;
1983 if (kbd_get_max_level()) {
1984 ret = kbd_get_state(&state);
1987 ret = kbd_get_level(&state);
1993 if (kbd_get_valid_token_counts()) {
1994 ret = kbd_get_first_active_token_bit();
1997 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1998 num &= num - 1; /* clear the first bit set */
2001 return ffs(num) - 1;
2004 pr_warn("Keyboard brightness level control not supported\n");
2008 static int kbd_led_level_set(struct led_classdev *led_cdev,
2009 enum led_brightness value)
2011 enum led_brightness new_value = value;
2012 struct kbd_state state;
2013 struct kbd_state new_state;
2017 mutex_lock(&kbd_led_mutex);
2019 if (kbd_get_max_level()) {
2020 ret = kbd_get_state(&state);
2024 ret = kbd_set_level(&new_state, value);
2027 ret = kbd_set_state_safe(&new_state, &state);
2028 } else if (kbd_get_valid_token_counts()) {
2029 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2030 num &= num - 1; /* clear the first bit set */
2034 ret = kbd_set_token_bit(ffs(num) - 1);
2036 pr_warn("Keyboard brightness level control not supported\n");
2042 kbd_led_level = new_value;
2044 mutex_unlock(&kbd_led_mutex);
2048 static struct led_classdev kbd_led = {
2049 .name = "dell::kbd_backlight",
2050 .flags = LED_BRIGHT_HW_CHANGED,
2051 .brightness_set_blocking = kbd_led_level_set,
2052 .brightness_get = kbd_led_level_get,
2053 .groups = kbd_led_groups,
2056 static int __init kbd_led_init(struct device *dev)
2061 if (!kbd_led_present)
2063 if (!kbd_als_supported)
2064 kbd_led_groups[1] = NULL;
2065 kbd_led.max_brightness = kbd_get_max_level();
2066 if (!kbd_led.max_brightness) {
2067 kbd_led.max_brightness = kbd_get_valid_token_counts();
2068 if (kbd_led.max_brightness)
2069 kbd_led.max_brightness--;
2072 kbd_led_level = kbd_led_level_get(NULL);
2074 ret = led_classdev_register(dev, &kbd_led);
2076 kbd_led_present = false;
2081 static void brightness_set_exit(struct led_classdev *led_cdev,
2082 enum led_brightness value)
2084 /* Don't change backlight level on exit */
2087 static void kbd_led_exit(void)
2089 if (!kbd_led_present)
2091 kbd_led.brightness_set = brightness_set_exit;
2092 led_classdev_unregister(&kbd_led);
2095 static int dell_laptop_notifier_call(struct notifier_block *nb,
2096 unsigned long action, void *data)
2098 bool changed = false;
2099 enum led_brightness new_kbd_led_level;
2102 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2103 if (!kbd_led_present)
2106 mutex_lock(&kbd_led_mutex);
2107 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2108 if (kbd_led_level != new_kbd_led_level) {
2109 kbd_led_level = new_kbd_led_level;
2112 mutex_unlock(&kbd_led_mutex);
2115 led_classdev_notify_brightness_hw_changed(&kbd_led,
2123 static struct notifier_block dell_laptop_notifier = {
2124 .notifier_call = dell_laptop_notifier_call,
2127 int dell_micmute_led_set(int state)
2129 struct calling_interface_buffer buffer;
2130 struct calling_interface_token *token;
2133 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2134 else if (state == 1)
2135 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2142 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2143 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2147 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2149 static int __init dell_init(void)
2151 struct calling_interface_token *token;
2152 int max_intensity = 0;
2155 if (!dmi_check_system(dell_device_table))
2159 /* find if this machine support other functions */
2160 dmi_check_system(dell_quirks);
2162 ret = platform_driver_register(&platform_driver);
2164 goto fail_platform_driver;
2165 platform_device = platform_device_alloc("dell-laptop", -1);
2166 if (!platform_device) {
2168 goto fail_platform_device1;
2170 ret = platform_device_add(platform_device);
2172 goto fail_platform_device2;
2174 ret = dell_setup_rfkill();
2177 pr_warn("Unable to setup rfkill\n");
2181 if (quirks && quirks->touchpad_led)
2182 touchpad_led_init(&platform_device->dev);
2184 kbd_led_init(&platform_device->dev);
2186 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2187 if (dell_laptop_dir != NULL)
2188 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2189 &dell_debugfs_fops);
2191 dell_laptop_register_notifier(&dell_laptop_notifier);
2193 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2196 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2198 struct calling_interface_buffer buffer;
2200 dell_fill_request(&buffer, token->location, 0, 0, 0);
2201 ret = dell_send_request(&buffer,
2202 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2204 max_intensity = buffer.output[3];
2207 if (max_intensity) {
2208 struct backlight_properties props;
2209 memset(&props, 0, sizeof(struct backlight_properties));
2210 props.type = BACKLIGHT_PLATFORM;
2211 props.max_brightness = max_intensity;
2212 dell_backlight_device = backlight_device_register("dell_backlight",
2213 &platform_device->dev,
2218 if (IS_ERR(dell_backlight_device)) {
2219 ret = PTR_ERR(dell_backlight_device);
2220 dell_backlight_device = NULL;
2221 goto fail_backlight;
2224 dell_backlight_device->props.brightness =
2225 dell_get_intensity(dell_backlight_device);
2226 if (dell_backlight_device->props.brightness < 0) {
2227 ret = dell_backlight_device->props.brightness;
2228 goto fail_get_brightness;
2230 backlight_update_status(dell_backlight_device);
2235 fail_get_brightness:
2236 backlight_device_unregister(dell_backlight_device);
2238 dell_cleanup_rfkill();
2240 platform_device_del(platform_device);
2241 fail_platform_device2:
2242 platform_device_put(platform_device);
2243 fail_platform_device1:
2244 platform_driver_unregister(&platform_driver);
2245 fail_platform_driver:
2249 static void __exit dell_exit(void)
2251 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2252 debugfs_remove_recursive(dell_laptop_dir);
2253 if (quirks && quirks->touchpad_led)
2254 touchpad_led_exit();
2256 backlight_device_unregister(dell_backlight_device);
2257 dell_cleanup_rfkill();
2258 if (platform_device) {
2259 platform_device_unregister(platform_device);
2260 platform_driver_unregister(&platform_driver);
2264 /* dell-rbtn.c driver export functions which will not work correctly (and could
2265 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2266 * not problem when dell-rbtn.c is compiled as external module. When both files
2267 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2268 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2269 * This can be achieved by late_initcall() instead module_init().
2271 late_initcall(dell_init);
2272 module_exit(dell_exit);
2274 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2275 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2276 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2277 MODULE_DESCRIPTION("Dell laptop driver");
2278 MODULE_LICENSE("GPL");