2 * Driver for Dell laptop extras
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
41 int needs_kbd_timeouts;
43 * Ordered list of timeouts expressed in seconds.
44 * The list must end with -1
49 static struct quirk_entry *quirks;
51 static struct quirk_entry quirk_dell_vostro_v130 = {
55 static int __init dmi_matched(const struct dmi_system_id *dmi)
57 quirks = dmi->driver_data;
62 * These values come from Windows utility provided by Dell. If any other value
63 * is used then BIOS silently set timeout to 0 without any error message.
65 static struct quirk_entry quirk_dell_xps13_9333 = {
66 .needs_kbd_timeouts = 1,
67 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
70 static struct platform_driver platform_driver = {
72 .name = "dell-laptop",
76 static struct calling_interface_buffer *buffer;
77 static struct platform_device *platform_device;
78 static struct backlight_device *dell_backlight_device;
79 static struct rfkill *wifi_rfkill;
80 static struct rfkill *bluetooth_rfkill;
81 static struct rfkill *wwan_rfkill;
82 static bool force_rfkill;
84 module_param(force_rfkill, bool, 0444);
85 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
87 static const struct dmi_system_id dell_device_table[] __initconst = {
89 .ident = "Dell laptop",
91 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
92 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
97 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
98 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
103 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
104 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
108 .ident = "Dell Computer Corporation",
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
111 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
116 MODULE_DEVICE_TABLE(dmi, dell_device_table);
118 static const struct dmi_system_id dell_quirks[] __initconst = {
120 .callback = dmi_matched,
121 .ident = "Dell Vostro V130",
123 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
124 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
126 .driver_data = &quirk_dell_vostro_v130,
129 .callback = dmi_matched,
130 .ident = "Dell Vostro V131",
132 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
133 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
135 .driver_data = &quirk_dell_vostro_v130,
138 .callback = dmi_matched,
139 .ident = "Dell Vostro 3350",
141 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
142 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
144 .driver_data = &quirk_dell_vostro_v130,
147 .callback = dmi_matched,
148 .ident = "Dell Vostro 3555",
150 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
151 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
153 .driver_data = &quirk_dell_vostro_v130,
156 .callback = dmi_matched,
157 .ident = "Dell Inspiron N311z",
159 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
160 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
162 .driver_data = &quirk_dell_vostro_v130,
165 .callback = dmi_matched,
166 .ident = "Dell Inspiron M5110",
168 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
169 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
171 .driver_data = &quirk_dell_vostro_v130,
174 .callback = dmi_matched,
175 .ident = "Dell Vostro 3360",
177 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
180 .driver_data = &quirk_dell_vostro_v130,
183 .callback = dmi_matched,
184 .ident = "Dell Vostro 3460",
186 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
189 .driver_data = &quirk_dell_vostro_v130,
192 .callback = dmi_matched,
193 .ident = "Dell Vostro 3560",
195 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
198 .driver_data = &quirk_dell_vostro_v130,
201 .callback = dmi_matched,
202 .ident = "Dell Vostro 3450",
204 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
205 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
207 .driver_data = &quirk_dell_vostro_v130,
210 .callback = dmi_matched,
211 .ident = "Dell Inspiron 5420",
213 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
214 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
216 .driver_data = &quirk_dell_vostro_v130,
219 .callback = dmi_matched,
220 .ident = "Dell Inspiron 5520",
222 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
223 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
225 .driver_data = &quirk_dell_vostro_v130,
228 .callback = dmi_matched,
229 .ident = "Dell Inspiron 5720",
231 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
234 .driver_data = &quirk_dell_vostro_v130,
237 .callback = dmi_matched,
238 .ident = "Dell Inspiron 7420",
240 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
241 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
243 .driver_data = &quirk_dell_vostro_v130,
246 .callback = dmi_matched,
247 .ident = "Dell Inspiron 7520",
249 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
252 .driver_data = &quirk_dell_vostro_v130,
255 .callback = dmi_matched,
256 .ident = "Dell Inspiron 7720",
258 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
261 .driver_data = &quirk_dell_vostro_v130,
264 .callback = dmi_matched,
265 .ident = "Dell XPS13 9333",
267 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
270 .driver_data = &quirk_dell_xps13_9333,
275 void dell_set_arguments(u32 arg0, u32 arg1, u32 arg2, u32 arg3)
277 memset(buffer, 0, sizeof(struct calling_interface_buffer));
278 buffer->input[0] = arg0;
279 buffer->input[1] = arg1;
280 buffer->input[2] = arg2;
281 buffer->input[3] = arg3;
284 int dell_send_request(u16 class, u16 select)
288 buffer->cmd_class = class;
289 buffer->cmd_select = select;
290 ret = dell_smbios_call(buffer);
293 return dell_smbios_error(buffer->output[0]);
297 * Derived from information in smbios-wireless-ctl:
299 * cbSelect 17, Value 11
301 * Return Wireless Info
302 * cbArg1, byte0 = 0x00
304 * cbRes1 Standard return codes (0, -1, -2)
305 * cbRes2 Info bit flags:
307 * 0 Hardware switch supported (1)
308 * 1 WiFi locator supported (1)
309 * 2 WLAN supported (1)
310 * 3 Bluetooth (BT) supported (1)
311 * 4 WWAN supported (1)
312 * 5 Wireless KBD supported (1)
313 * 6 Uw b supported (1)
314 * 7 WiGig supported (1)
315 * 8 WLAN installed (1)
317 * 10 WWAN installed (1)
318 * 11 Uw b installed (1)
319 * 12 WiGig installed (1)
321 * 16 Hardware (HW) switch is On (1)
322 * 17 WLAN disabled (1)
324 * 19 WWAN disabled (1)
325 * 20 Uw b disabled (1)
326 * 21 WiGig disabled (1)
329 * cbRes3 NVRAM size in bytes
330 * cbRes4, byte 0 NVRAM format version number
333 * Set QuickSet Radio Disable Flag
334 * cbArg1, byte0 = 0x01
343 * cbArg1, byte2 Flag bits:
344 * 0 QuickSet disables radio (1)
347 * cbRes1 Standard return codes (0, -1, -2)
348 * cbRes2 QuickSet (QS) radio disable bit map:
353 * 4 QS disables WIGIG
356 * Wireless Switch Configuration
357 * cbArg1, byte0 = 0x02
363 * 2 Set WiFi locator enable/disable
365 * Switch settings (if byte 1==1):
366 * 0 WLAN sw itch control (1)
367 * 1 BT sw itch control (1)
368 * 2 WWAN sw itch control (1)
369 * 3 UWB sw itch control (1)
370 * 4 WiGig sw itch control (1)
372 * cbArg1, byte2 Enable bits (if byte 1==2):
373 * 0 Enable WiFi locator (1)
375 * cbRes1 Standard return codes (0, -1, -2)
376 * cbRes2 QuickSet radio disable bit map:
377 * 0 WLAN controlled by sw itch (1)
378 * 1 BT controlled by sw itch (1)
379 * 2 WWAN controlled by sw itch (1)
380 * 3 UWB controlled by sw itch (1)
381 * 4 WiGig controlled by sw itch (1)
383 * 7 Wireless sw itch config locked (1)
384 * 8 WiFi locator enabled (1)
386 * 15 WiFi locator setting locked (1)
389 * Read Local Config Data (LCD)
390 * cbArg1, byte0 = 0x10
391 * cbArg1, byte1 NVRAM index low byte
392 * cbArg1, byte2 NVRAM index high byte
393 * cbRes1 Standard return codes (0, -1, -2)
394 * cbRes2 4 bytes read from LCD[index]
395 * cbRes3 4 bytes read from LCD[index+4]
396 * cbRes4 4 bytes read from LCD[index+8]
398 * Write Local Config Data (LCD)
399 * cbArg1, byte0 = 0x11
400 * cbArg1, byte1 NVRAM index low byte
401 * cbArg1, byte2 NVRAM index high byte
402 * cbArg2 4 bytes to w rite at LCD[index]
403 * cbArg3 4 bytes to w rite at LCD[index+4]
404 * cbArg4 4 bytes to w rite at LCD[index+8]
405 * cbRes1 Standard return codes (0, -1, -2)
407 * Populate Local Config Data from NVRAM
408 * cbArg1, byte0 = 0x12
409 * cbRes1 Standard return codes (0, -1, -2)
411 * Commit Local Config Data to NVRAM
412 * cbArg1, byte0 = 0x13
413 * cbRes1 Standard return codes (0, -1, -2)
416 static int dell_rfkill_set(void *data, bool blocked)
418 int disable = blocked ? 1 : 0;
419 unsigned long radio = (unsigned long)data;
420 int hwswitch_bit = (unsigned long)data - 1;
425 dell_set_arguments(0, 0, 0, 0);
426 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
429 status = buffer->output[1];
431 dell_set_arguments(0x2, 0, 0, 0);
432 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
435 hwswitch = buffer->output[1];
437 /* If the hardware switch controls this radio, and the hardware
438 switch is disabled, always disable the radio */
439 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
440 (status & BIT(0)) && !(status & BIT(16)))
443 dell_set_arguments(1 | (radio<<8) | (disable << 16), 0, 0, 0);
444 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
448 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
451 if (status & BIT(0)) {
452 /* Has hw-switch, sync sw_state to BIOS */
453 int block = rfkill_blocked(rfkill);
454 dell_set_arguments(1 | (radio << 8) | (block << 16), 0, 0, 0);
455 dell_send_request(CLASS_INFO, SELECT_RFKILL);
457 /* No hw-switch, sync BIOS state to sw_state */
458 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
462 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
463 int status, int hwswitch)
465 if (hwswitch & (BIT(radio - 1)))
466 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
469 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
471 int radio = ((unsigned long)data & 0xF);
476 dell_set_arguments(0, 0, 0, 0);
477 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
478 status = buffer->output[1];
480 if (ret != 0 || !(status & BIT(0))) {
484 dell_set_arguments(0, 0x2, 0, 0);
485 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
486 hwswitch = buffer->output[1];
491 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
494 static const struct rfkill_ops dell_rfkill_ops = {
495 .set_block = dell_rfkill_set,
496 .query = dell_rfkill_query,
499 static struct dentry *dell_laptop_dir;
501 static int dell_debugfs_show(struct seq_file *s, void *data)
508 dell_set_arguments(0, 0, 0, 0);
509 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
512 status = buffer->output[1];
514 dell_set_arguments(0, 0x2, 0, 0);
515 hwswitch_ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
518 hwswitch_state = buffer->output[1];
520 seq_printf(s, "return:\t%d\n", ret);
521 seq_printf(s, "status:\t0x%X\n", status);
522 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
524 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
525 (status & BIT(1)) >> 1);
526 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
527 (status & BIT(2)) >> 2);
528 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
529 (status & BIT(3)) >> 3);
530 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
531 (status & BIT(4)) >> 4);
532 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
533 (status & BIT(5)) >> 5);
534 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
535 (status & BIT(6)) >> 6);
536 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
537 (status & BIT(7)) >> 7);
538 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
539 (status & BIT(8)) >> 8);
540 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
541 (status & BIT(9)) >> 9);
542 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
543 (status & BIT(10)) >> 10);
544 seq_printf(s, "Bit 11: UWB installed: %lu\n",
545 (status & BIT(11)) >> 11);
546 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
547 (status & BIT(12)) >> 12);
549 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
550 (status & BIT(16)) >> 16);
551 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
552 (status & BIT(17)) >> 17);
553 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
554 (status & BIT(18)) >> 18);
555 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
556 (status & BIT(19)) >> 19);
557 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
558 (status & BIT(20)) >> 20);
559 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
560 (status & BIT(21)) >> 21);
562 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
563 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
564 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
565 hwswitch_state & BIT(0));
566 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
567 (hwswitch_state & BIT(1)) >> 1);
568 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
569 (hwswitch_state & BIT(2)) >> 2);
570 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
571 (hwswitch_state & BIT(3)) >> 3);
572 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
573 (hwswitch_state & BIT(4)) >> 4);
574 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
575 (hwswitch_state & BIT(7)) >> 7);
576 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
577 (hwswitch_state & BIT(8)) >> 8);
578 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
579 (hwswitch_state & BIT(15)) >> 15);
584 static int dell_debugfs_open(struct inode *inode, struct file *file)
586 return single_open(file, dell_debugfs_show, inode->i_private);
589 static const struct file_operations dell_debugfs_fops = {
590 .owner = THIS_MODULE,
591 .open = dell_debugfs_open,
594 .release = single_release,
597 static void dell_update_rfkill(struct work_struct *ignored)
603 dell_set_arguments(0, 0, 0, 0);
604 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
605 status = buffer->output[1];
610 dell_set_arguments(0, 0x2, 0, 0);
611 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
613 if (ret == 0 && (status & BIT(0)))
614 hwswitch = buffer->output[1];
617 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
618 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
620 if (bluetooth_rfkill) {
621 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
623 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
626 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
627 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
630 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
632 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
635 static bool extended;
637 if (str & I8042_STR_AUXDATA)
640 if (unlikely(data == 0xe0)) {
643 } else if (unlikely(extended)) {
646 schedule_delayed_work(&dell_rfkill_work,
647 round_jiffies_relative(HZ / 4));
656 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
657 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
659 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
660 unsigned long action, void *data)
662 schedule_delayed_work(&dell_rfkill_work, 0);
666 static struct notifier_block dell_laptop_rbtn_notifier = {
667 .notifier_call = dell_laptop_rbtn_notifier_call,
670 static int __init dell_setup_rfkill(void)
672 int status, ret, whitelisted;
676 * rfkill support causes trouble on various models, mostly Inspirons.
677 * So we whitelist certain series, and don't support rfkill on others.
680 product = dmi_get_system_info(DMI_PRODUCT_NAME);
681 if (product && (strncmp(product, "Latitude", 8) == 0 ||
682 strncmp(product, "Precision", 9) == 0))
684 if (!force_rfkill && !whitelisted)
687 dell_set_arguments(0, 0, 0, 0);
688 ret = dell_send_request(CLASS_INFO, SELECT_RFKILL);
689 status = buffer->output[1];
691 /* dell wireless info smbios call is not supported */
695 /* rfkill is only tested on laptops with a hwswitch */
696 if (!(status & BIT(0)) && !force_rfkill)
699 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
700 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
702 &dell_rfkill_ops, (void *) 1);
707 ret = rfkill_register(wifi_rfkill);
712 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
713 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
714 &platform_device->dev,
715 RFKILL_TYPE_BLUETOOTH,
716 &dell_rfkill_ops, (void *) 2);
717 if (!bluetooth_rfkill) {
721 ret = rfkill_register(bluetooth_rfkill);
726 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
727 wwan_rfkill = rfkill_alloc("dell-wwan",
728 &platform_device->dev,
730 &dell_rfkill_ops, (void *) 3);
735 ret = rfkill_register(wwan_rfkill);
741 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
742 * which can receive events from HW slider switch.
744 * Dell SMBIOS on whitelisted models supports controlling radio devices
745 * but does not support receiving HW button switch events. We can use
746 * i8042 filter hook function to receive keyboard data and handle
747 * keycode for HW button.
749 * So if it is possible we will use Dell Airplane Mode Switch ACPI
750 * driver for receiving HW events and Dell SMBIOS for setting rfkill
751 * states. If ACPI driver or device is not available we will fallback to
752 * i8042 filter hook function.
754 * To prevent duplicate rfkill devices which control and do same thing,
755 * dell-rbtn driver will automatically remove its own rfkill devices
756 * once function dell_rbtn_notifier_register() is called.
759 dell_rbtn_notifier_register_func =
760 symbol_request(dell_rbtn_notifier_register);
761 if (dell_rbtn_notifier_register_func) {
762 dell_rbtn_notifier_unregister_func =
763 symbol_request(dell_rbtn_notifier_unregister);
764 if (!dell_rbtn_notifier_unregister_func) {
765 symbol_put(dell_rbtn_notifier_register);
766 dell_rbtn_notifier_register_func = NULL;
770 if (dell_rbtn_notifier_register_func) {
771 ret = dell_rbtn_notifier_register_func(
772 &dell_laptop_rbtn_notifier);
773 symbol_put(dell_rbtn_notifier_register);
774 dell_rbtn_notifier_register_func = NULL;
776 symbol_put(dell_rbtn_notifier_unregister);
777 dell_rbtn_notifier_unregister_func = NULL;
780 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
785 pr_info("Using dell-rbtn acpi driver for receiving events\n");
786 } else if (ret != -ENODEV) {
787 pr_warn("Unable to register dell rbtn notifier\n");
790 ret = i8042_install_filter(dell_laptop_i8042_filter);
792 pr_warn("Unable to install key filter\n");
795 pr_info("Using i8042 filter function for receiving events\n");
801 rfkill_unregister(wwan_rfkill);
803 rfkill_destroy(wwan_rfkill);
804 if (bluetooth_rfkill)
805 rfkill_unregister(bluetooth_rfkill);
807 rfkill_destroy(bluetooth_rfkill);
809 rfkill_unregister(wifi_rfkill);
811 rfkill_destroy(wifi_rfkill);
816 static void dell_cleanup_rfkill(void)
818 if (dell_rbtn_notifier_unregister_func) {
819 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
820 symbol_put(dell_rbtn_notifier_unregister);
821 dell_rbtn_notifier_unregister_func = NULL;
823 i8042_remove_filter(dell_laptop_i8042_filter);
825 cancel_delayed_work_sync(&dell_rfkill_work);
827 rfkill_unregister(wifi_rfkill);
828 rfkill_destroy(wifi_rfkill);
830 if (bluetooth_rfkill) {
831 rfkill_unregister(bluetooth_rfkill);
832 rfkill_destroy(bluetooth_rfkill);
835 rfkill_unregister(wwan_rfkill);
836 rfkill_destroy(wwan_rfkill);
840 static int dell_send_intensity(struct backlight_device *bd)
842 struct calling_interface_token *token;
845 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
849 dell_set_arguments(token->location, bd->props.brightness, 0, 0);
850 if (power_supply_is_system_supplied() > 0)
851 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
853 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
858 static int dell_get_intensity(struct backlight_device *bd)
860 struct calling_interface_token *token;
863 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
867 dell_set_arguments(token->location, 0, 0, 0);
868 if (power_supply_is_system_supplied() > 0)
869 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_AC);
871 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
874 ret = buffer->output[1];
878 static const struct backlight_ops dell_ops = {
879 .get_brightness = dell_get_intensity,
880 .update_status = dell_send_intensity,
883 static void touchpad_led_on(void)
887 i8042_command(&data, command | 1 << 12);
890 static void touchpad_led_off(void)
894 i8042_command(&data, command | 1 << 12);
897 static void touchpad_led_set(struct led_classdev *led_cdev,
898 enum led_brightness value)
906 static struct led_classdev touchpad_led = {
907 .name = "dell-laptop::touchpad",
908 .brightness_set = touchpad_led_set,
909 .flags = LED_CORE_SUSPENDRESUME,
912 static int __init touchpad_led_init(struct device *dev)
914 return led_classdev_register(dev, &touchpad_led);
917 static void touchpad_led_exit(void)
919 led_classdev_unregister(&touchpad_led);
923 * Derived from information in smbios-keyboard-ctl:
927 * Keyboard illumination
928 * cbArg1 determines the function to be performed
930 * cbArg1 0x0 = Get Feature Information
931 * cbRES1 Standard return codes (0, -1, -2)
932 * cbRES2, word0 Bitmap of user-selectable modes
933 * bit 0 Always off (All systems)
934 * bit 1 Always on (Travis ATG, Siberia)
935 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
936 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
937 * bit 4 Auto: Input-activity-based On; input-activity based Off
938 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
939 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
940 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
941 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
942 * bits 9-15 Reserved for future use
943 * cbRES2, byte2 Reserved for future use
944 * cbRES2, byte3 Keyboard illumination type
948 * 3-255 Reserved for future use
949 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
950 * bit 0 Any keystroke
951 * bit 1 Touchpad activity
952 * bit 2 Pointing stick
954 * bits 4-7 Reserved for future use
955 * cbRES3, byte1 Supported timeout unit bitmap
960 * bits 4-7 Reserved for future use
961 * cbRES3, byte2 Number of keyboard light brightness levels
962 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
963 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
964 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
965 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
967 * cbArg1 0x1 = Get Current State
968 * cbRES1 Standard return codes (0, -1, -2)
969 * cbRES2, word0 Bitmap of current mode state
970 * bit 0 Always off (All systems)
971 * bit 1 Always on (Travis ATG, Siberia)
972 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
973 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
974 * bit 4 Auto: Input-activity-based On; input-activity based Off
975 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
976 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
977 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
978 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
979 * bits 9-15 Reserved for future use
980 * Note: Only One bit can be set
981 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
982 * bit 0 Any keystroke
983 * bit 1 Touchpad activity
984 * bit 2 Pointing stick
986 * bits 4-7 Reserved for future use
987 * cbRES2, byte3 Current Timeout on battery
988 * bits 7:6 Timeout units indicator:
993 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
994 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
995 * are set upon return from the [Get feature information] call.
996 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
997 * cbRES3, byte1 Current ALS reading
998 * cbRES3, byte2 Current keyboard light level.
999 * cbRES3, byte3 Current timeout on AC Power
1000 * bits 7:6 Timeout units indicator:
1005 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1006 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1007 * are set upon return from the upon return from the [Get Feature information] call.
1009 * cbArg1 0x2 = Set New State
1010 * cbRES1 Standard return codes (0, -1, -2)
1011 * cbArg2, word0 Bitmap of current mode state
1012 * bit 0 Always off (All systems)
1013 * bit 1 Always on (Travis ATG, Siberia)
1014 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1015 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1016 * bit 4 Auto: Input-activity-based On; input-activity based Off
1017 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1018 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1019 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1020 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1021 * bits 9-15 Reserved for future use
1022 * Note: Only One bit can be set
1023 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1024 * keyboard to turn off automatically.
1025 * bit 0 Any keystroke
1026 * bit 1 Touchpad activity
1027 * bit 2 Pointing stick
1029 * bits 4-7 Reserved for future use
1030 * cbArg2, byte3 Desired Timeout on battery
1031 * bits 7:6 Timeout units indicator:
1036 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1037 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1038 * cbArg3, byte2 Desired keyboard light level.
1039 * cbArg3, byte3 Desired Timeout on AC power
1040 * bits 7:6 Timeout units indicator:
1045 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1049 enum kbd_timeout_unit {
1050 KBD_TIMEOUT_SECONDS = 0,
1051 KBD_TIMEOUT_MINUTES,
1057 KBD_MODE_BIT_OFF = 0,
1060 KBD_MODE_BIT_TRIGGER_ALS,
1061 KBD_MODE_BIT_TRIGGER,
1062 KBD_MODE_BIT_TRIGGER_25,
1063 KBD_MODE_BIT_TRIGGER_50,
1064 KBD_MODE_BIT_TRIGGER_75,
1065 KBD_MODE_BIT_TRIGGER_100,
1068 #define kbd_is_als_mode_bit(bit) \
1069 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1070 #define kbd_is_trigger_mode_bit(bit) \
1071 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1072 #define kbd_is_level_mode_bit(bit) \
1073 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1091 u8 timeout_value_ac;
1098 static const int kbd_tokens[] = {
1100 KBD_LED_AUTO_25_TOKEN,
1101 KBD_LED_AUTO_50_TOKEN,
1102 KBD_LED_AUTO_75_TOKEN,
1103 KBD_LED_AUTO_100_TOKEN,
1107 static u16 kbd_token_bits;
1109 static struct kbd_info kbd_info;
1110 static bool kbd_als_supported;
1111 static bool kbd_triggers_supported;
1112 static bool kbd_timeout_ac_supported;
1114 static u8 kbd_mode_levels[16];
1115 static int kbd_mode_levels_count;
1117 static u8 kbd_previous_level;
1118 static u8 kbd_previous_mode_bit;
1120 static bool kbd_led_present;
1121 static DEFINE_MUTEX(kbd_led_mutex);
1124 * NOTE: there are three ways to set the keyboard backlight level.
1125 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1126 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1127 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1129 * There are laptops which support only one of these methods. If we want to
1130 * support as many machines as possible we need to implement all three methods.
1131 * The first two methods use the kbd_state structure. The third uses SMBIOS
1132 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1133 * keyboard backlight level via kbd_state.level.
1136 static int kbd_get_info(struct kbd_info *info)
1141 dell_set_arguments(0, 0, 0, 0);
1142 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1146 info->modes = buffer->output[1] & 0xFFFF;
1147 info->type = (buffer->output[1] >> 24) & 0xFF;
1148 info->triggers = buffer->output[2] & 0xFF;
1149 units = (buffer->output[2] >> 8) & 0xFF;
1150 info->levels = (buffer->output[2] >> 16) & 0xFF;
1153 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1155 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1157 info->hours = (buffer->output[3] >> 16) & 0xFF;
1159 info->days = (buffer->output[3] >> 24) & 0xFF;
1164 static unsigned int kbd_get_max_level(void)
1166 if (kbd_info.levels != 0)
1167 return kbd_info.levels;
1168 if (kbd_mode_levels_count > 0)
1169 return kbd_mode_levels_count - 1;
1173 static int kbd_get_level(struct kbd_state *state)
1177 if (kbd_info.levels != 0)
1178 return state->level;
1180 if (kbd_mode_levels_count > 0) {
1181 for (i = 0; i < kbd_mode_levels_count; ++i)
1182 if (kbd_mode_levels[i] == state->mode_bit)
1190 static int kbd_set_level(struct kbd_state *state, u8 level)
1192 if (kbd_info.levels != 0) {
1194 kbd_previous_level = level;
1195 if (state->level == level)
1197 state->level = level;
1198 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1199 state->mode_bit = kbd_previous_mode_bit;
1200 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1201 kbd_previous_mode_bit = state->mode_bit;
1202 state->mode_bit = KBD_MODE_BIT_OFF;
1207 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1209 kbd_previous_level = level;
1210 state->mode_bit = kbd_mode_levels[level];
1217 static int kbd_get_state(struct kbd_state *state)
1221 dell_set_arguments(0x1, 0, 0, 0);
1222 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1226 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1227 if (state->mode_bit != 0)
1230 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1231 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1232 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1233 state->als_setting = buffer->output[2] & 0xFF;
1234 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1235 state->level = (buffer->output[2] >> 16) & 0xFF;
1236 state->timeout_value_ac = (buffer->output[2] >> 24) & 0x3F;
1237 state->timeout_unit_ac = (buffer->output[2] >> 30) & 0x3;
1242 static int kbd_set_state(struct kbd_state *state)
1248 input1 = BIT(state->mode_bit) & 0xFFFF;
1249 input1 |= (state->triggers & 0xFF) << 16;
1250 input1 |= (state->timeout_value & 0x3F) << 24;
1251 input1 |= (state->timeout_unit & 0x3) << 30;
1252 input2 = state->als_setting & 0xFF;
1253 input2 |= (state->level & 0xFF) << 16;
1254 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1255 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1256 dell_set_arguments(0x2, input1, input2, 0);
1257 ret = dell_send_request(CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1262 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1266 ret = kbd_set_state(state);
1271 * When setting the new state fails,try to restore the previous one.
1272 * This is needed on some machines where BIOS sets a default state when
1273 * setting a new state fails. This default state could be all off.
1276 if (kbd_set_state(old))
1277 pr_err("Setting old previous keyboard state failed\n");
1282 static int kbd_set_token_bit(u8 bit)
1284 struct calling_interface_token *token;
1287 if (bit >= ARRAY_SIZE(kbd_tokens))
1290 token = dell_smbios_find_token(kbd_tokens[bit]);
1294 dell_set_arguments(token->location, token->value, 0, 0);
1295 ret = dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1300 static int kbd_get_token_bit(u8 bit)
1302 struct calling_interface_token *token;
1306 if (bit >= ARRAY_SIZE(kbd_tokens))
1309 token = dell_smbios_find_token(kbd_tokens[bit]);
1313 dell_set_arguments(token->location, 0, 0, 0);
1314 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1315 val = buffer->output[1];
1320 return (val == token->value);
1323 static int kbd_get_first_active_token_bit(void)
1328 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1329 ret = kbd_get_token_bit(i);
1337 static int kbd_get_valid_token_counts(void)
1339 return hweight16(kbd_token_bits);
1342 static inline int kbd_init_info(void)
1344 struct kbd_state state;
1348 ret = kbd_get_info(&kbd_info);
1352 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1353 * timeout value which is shared for both battery and AC power
1354 * settings. So do not try to set AC values on old models.
1356 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1357 kbd_timeout_ac_supported = true;
1359 kbd_get_state(&state);
1361 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1362 if (kbd_info.seconds > 63)
1363 kbd_info.seconds = 63;
1364 if (kbd_info.minutes > 63)
1365 kbd_info.minutes = 63;
1366 if (kbd_info.hours > 63)
1367 kbd_info.hours = 63;
1368 if (kbd_info.days > 63)
1371 /* NOTE: On tested machines ON mode did not work and caused
1372 * problems (turned backlight off) so do not use it
1374 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1376 kbd_previous_level = kbd_get_level(&state);
1377 kbd_previous_mode_bit = state.mode_bit;
1379 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1380 kbd_previous_level = 1;
1382 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1383 kbd_previous_mode_bit =
1384 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1385 if (kbd_previous_mode_bit != 0)
1386 kbd_previous_mode_bit--;
1389 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1390 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1391 kbd_als_supported = true;
1393 if (kbd_info.modes & (
1394 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1395 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1396 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1398 kbd_triggers_supported = true;
1400 /* kbd_mode_levels[0] is reserved, see below */
1401 for (i = 0; i < 16; ++i)
1402 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1403 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1406 * Find the first supported mode and assign to kbd_mode_levels[0].
1407 * This should be 0 (off), but we cannot depend on the BIOS to
1410 if (kbd_mode_levels_count > 0) {
1411 for (i = 0; i < 16; ++i) {
1412 if (BIT(i) & kbd_info.modes) {
1413 kbd_mode_levels[0] = i;
1417 kbd_mode_levels_count++;
1424 static inline void kbd_init_tokens(void)
1428 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1429 if (dell_smbios_find_token(kbd_tokens[i]))
1430 kbd_token_bits |= BIT(i);
1433 static void kbd_init(void)
1437 ret = kbd_init_info();
1441 * Only supports keyboard backlight when it has at least two modes.
1443 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1444 || kbd_get_valid_token_counts() >= 2)
1445 kbd_led_present = true;
1448 static ssize_t kbd_led_timeout_store(struct device *dev,
1449 struct device_attribute *attr,
1450 const char *buf, size_t count)
1452 struct kbd_state new_state;
1453 struct kbd_state state;
1461 ret = sscanf(buf, "%d %c", &value, &ch);
1474 if (value > kbd_info.seconds)
1476 unit = KBD_TIMEOUT_SECONDS;
1479 if (value > kbd_info.minutes)
1481 unit = KBD_TIMEOUT_MINUTES;
1484 if (value > kbd_info.hours)
1486 unit = KBD_TIMEOUT_HOURS;
1489 if (value > kbd_info.days)
1491 unit = KBD_TIMEOUT_DAYS;
1497 if (quirks && quirks->needs_kbd_timeouts)
1501 /* Convert value from current units to seconds */
1503 case KBD_TIMEOUT_DAYS:
1505 case KBD_TIMEOUT_HOURS:
1507 case KBD_TIMEOUT_MINUTES:
1509 unit = KBD_TIMEOUT_SECONDS;
1512 if (quirks && quirks->needs_kbd_timeouts) {
1513 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1514 if (value <= quirks->kbd_timeouts[i]) {
1515 value = quirks->kbd_timeouts[i];
1521 if (value <= kbd_info.seconds && kbd_info.seconds) {
1522 unit = KBD_TIMEOUT_SECONDS;
1523 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1525 unit = KBD_TIMEOUT_MINUTES;
1526 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1528 unit = KBD_TIMEOUT_HOURS;
1529 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1530 value /= (60 * 60 * 24);
1531 unit = KBD_TIMEOUT_DAYS;
1537 mutex_lock(&kbd_led_mutex);
1539 ret = kbd_get_state(&state);
1545 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1546 new_state.timeout_value_ac = value;
1547 new_state.timeout_unit_ac = unit;
1549 new_state.timeout_value = value;
1550 new_state.timeout_unit = unit;
1553 ret = kbd_set_state_safe(&new_state, &state);
1559 mutex_unlock(&kbd_led_mutex);
1563 static ssize_t kbd_led_timeout_show(struct device *dev,
1564 struct device_attribute *attr, char *buf)
1566 struct kbd_state state;
1572 ret = kbd_get_state(&state);
1576 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1577 value = state.timeout_value_ac;
1578 unit = state.timeout_unit_ac;
1580 value = state.timeout_value;
1581 unit = state.timeout_unit;
1584 len = sprintf(buf, "%d", value);
1587 case KBD_TIMEOUT_SECONDS:
1588 return len + sprintf(buf+len, "s\n");
1589 case KBD_TIMEOUT_MINUTES:
1590 return len + sprintf(buf+len, "m\n");
1591 case KBD_TIMEOUT_HOURS:
1592 return len + sprintf(buf+len, "h\n");
1593 case KBD_TIMEOUT_DAYS:
1594 return len + sprintf(buf+len, "d\n");
1602 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1603 kbd_led_timeout_show, kbd_led_timeout_store);
1605 static const char * const kbd_led_triggers[] = {
1608 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1612 static ssize_t kbd_led_triggers_store(struct device *dev,
1613 struct device_attribute *attr,
1614 const char *buf, size_t count)
1616 struct kbd_state new_state;
1617 struct kbd_state state;
1618 bool triggers_enabled = false;
1619 int trigger_bit = -1;
1623 ret = sscanf(buf, "%20s", trigger);
1627 if (trigger[0] != '+' && trigger[0] != '-')
1630 mutex_lock(&kbd_led_mutex);
1632 ret = kbd_get_state(&state);
1636 if (kbd_triggers_supported)
1637 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1639 if (kbd_triggers_supported) {
1640 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1641 if (!(kbd_info.triggers & BIT(i)))
1643 if (!kbd_led_triggers[i])
1645 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1647 if (trigger[0] == '+' &&
1648 triggers_enabled && (state.triggers & BIT(i))) {
1652 if (trigger[0] == '-' &&
1653 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1662 if (trigger_bit == -1) {
1668 if (trigger[0] == '+')
1669 new_state.triggers |= BIT(trigger_bit);
1671 new_state.triggers &= ~BIT(trigger_bit);
1673 * NOTE: trackstick bit (2) must be disabled when
1674 * disabling touchpad bit (1), otherwise touchpad
1675 * bit (1) will not be disabled
1677 if (trigger_bit == 1)
1678 new_state.triggers &= ~BIT(2);
1680 if ((kbd_info.triggers & new_state.triggers) !=
1681 new_state.triggers) {
1685 if (new_state.triggers && !triggers_enabled) {
1686 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1687 kbd_set_level(&new_state, kbd_previous_level);
1688 } else if (new_state.triggers == 0) {
1689 kbd_set_level(&new_state, 0);
1691 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1695 ret = kbd_set_state_safe(&new_state, &state);
1698 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1699 kbd_previous_mode_bit = new_state.mode_bit;
1702 mutex_unlock(&kbd_led_mutex);
1706 static ssize_t kbd_led_triggers_show(struct device *dev,
1707 struct device_attribute *attr, char *buf)
1709 struct kbd_state state;
1710 bool triggers_enabled;
1714 ret = kbd_get_state(&state);
1720 if (kbd_triggers_supported) {
1721 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1722 level = kbd_get_level(&state);
1723 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1724 if (!(kbd_info.triggers & BIT(i)))
1726 if (!kbd_led_triggers[i])
1728 if ((triggers_enabled || level <= 0) &&
1729 (state.triggers & BIT(i)))
1733 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1738 buf[len - 1] = '\n';
1743 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1744 kbd_led_triggers_show, kbd_led_triggers_store);
1746 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1747 struct device_attribute *attr,
1748 const char *buf, size_t count)
1750 struct kbd_state new_state;
1751 struct kbd_state state;
1752 bool triggers_enabled = false;
1756 ret = kstrtoint(buf, 0, &enable);
1760 mutex_lock(&kbd_led_mutex);
1762 ret = kbd_get_state(&state);
1766 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1773 if (kbd_triggers_supported)
1774 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1777 if (triggers_enabled)
1778 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1780 new_state.mode_bit = KBD_MODE_BIT_ALS;
1782 if (triggers_enabled) {
1783 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1784 kbd_set_level(&new_state, kbd_previous_level);
1786 new_state.mode_bit = KBD_MODE_BIT_ON;
1789 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1794 ret = kbd_set_state_safe(&new_state, &state);
1797 kbd_previous_mode_bit = new_state.mode_bit;
1801 mutex_unlock(&kbd_led_mutex);
1805 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1806 struct device_attribute *attr,
1809 struct kbd_state state;
1810 bool enabled = false;
1813 ret = kbd_get_state(&state);
1816 enabled = kbd_is_als_mode_bit(state.mode_bit);
1818 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1821 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1822 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1824 static ssize_t kbd_led_als_setting_store(struct device *dev,
1825 struct device_attribute *attr,
1826 const char *buf, size_t count)
1828 struct kbd_state state;
1829 struct kbd_state new_state;
1833 ret = kstrtou8(buf, 10, &setting);
1837 mutex_lock(&kbd_led_mutex);
1839 ret = kbd_get_state(&state);
1844 new_state.als_setting = setting;
1846 ret = kbd_set_state_safe(&new_state, &state);
1852 mutex_unlock(&kbd_led_mutex);
1856 static ssize_t kbd_led_als_setting_show(struct device *dev,
1857 struct device_attribute *attr,
1860 struct kbd_state state;
1863 ret = kbd_get_state(&state);
1867 return sprintf(buf, "%d\n", state.als_setting);
1870 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1871 kbd_led_als_setting_show, kbd_led_als_setting_store);
1873 static struct attribute *kbd_led_attrs[] = {
1874 &dev_attr_stop_timeout.attr,
1875 &dev_attr_start_triggers.attr,
1879 static const struct attribute_group kbd_led_group = {
1880 .attrs = kbd_led_attrs,
1883 static struct attribute *kbd_led_als_attrs[] = {
1884 &dev_attr_als_enabled.attr,
1885 &dev_attr_als_setting.attr,
1889 static const struct attribute_group kbd_led_als_group = {
1890 .attrs = kbd_led_als_attrs,
1893 static const struct attribute_group *kbd_led_groups[] = {
1899 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1903 struct kbd_state state;
1905 if (kbd_get_max_level()) {
1906 ret = kbd_get_state(&state);
1909 ret = kbd_get_level(&state);
1915 if (kbd_get_valid_token_counts()) {
1916 ret = kbd_get_first_active_token_bit();
1919 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1920 num &= num - 1; /* clear the first bit set */
1923 return ffs(num) - 1;
1926 pr_warn("Keyboard brightness level control not supported\n");
1930 static int kbd_led_level_set(struct led_classdev *led_cdev,
1931 enum led_brightness value)
1933 struct kbd_state state;
1934 struct kbd_state new_state;
1938 mutex_lock(&kbd_led_mutex);
1940 if (kbd_get_max_level()) {
1941 ret = kbd_get_state(&state);
1945 ret = kbd_set_level(&new_state, value);
1948 ret = kbd_set_state_safe(&new_state, &state);
1949 } else if (kbd_get_valid_token_counts()) {
1950 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1951 num &= num - 1; /* clear the first bit set */
1955 ret = kbd_set_token_bit(ffs(num) - 1);
1957 pr_warn("Keyboard brightness level control not supported\n");
1962 mutex_unlock(&kbd_led_mutex);
1966 static struct led_classdev kbd_led = {
1967 .name = "dell::kbd_backlight",
1968 .flags = LED_BRIGHT_HW_CHANGED,
1969 .brightness_set_blocking = kbd_led_level_set,
1970 .brightness_get = kbd_led_level_get,
1971 .groups = kbd_led_groups,
1974 static int __init kbd_led_init(struct device *dev)
1979 if (!kbd_led_present)
1981 if (!kbd_als_supported)
1982 kbd_led_groups[1] = NULL;
1983 kbd_led.max_brightness = kbd_get_max_level();
1984 if (!kbd_led.max_brightness) {
1985 kbd_led.max_brightness = kbd_get_valid_token_counts();
1986 if (kbd_led.max_brightness)
1987 kbd_led.max_brightness--;
1989 ret = led_classdev_register(dev, &kbd_led);
1991 kbd_led_present = false;
1996 static void brightness_set_exit(struct led_classdev *led_cdev,
1997 enum led_brightness value)
1999 /* Don't change backlight level on exit */
2002 static void kbd_led_exit(void)
2004 if (!kbd_led_present)
2006 kbd_led.brightness_set = brightness_set_exit;
2007 led_classdev_unregister(&kbd_led);
2010 static int dell_laptop_notifier_call(struct notifier_block *nb,
2011 unsigned long action, void *data)
2014 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2015 if (!kbd_led_present)
2018 led_classdev_notify_brightness_hw_changed(&kbd_led,
2019 kbd_led_level_get(&kbd_led));
2026 static struct notifier_block dell_laptop_notifier = {
2027 .notifier_call = dell_laptop_notifier_call,
2030 int dell_micmute_led_set(int state)
2032 struct calling_interface_token *token;
2035 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2036 else if (state == 1)
2037 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2044 dell_set_arguments(token->location, token->value, 0, 0);
2045 dell_send_request(CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2049 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2051 static int __init dell_init(void)
2053 struct calling_interface_token *token;
2054 int max_intensity = 0;
2057 if (!dmi_check_system(dell_device_table))
2061 /* find if this machine support other functions */
2062 dmi_check_system(dell_quirks);
2064 ret = platform_driver_register(&platform_driver);
2066 goto fail_platform_driver;
2067 platform_device = platform_device_alloc("dell-laptop", -1);
2068 if (!platform_device) {
2070 goto fail_platform_device1;
2072 ret = platform_device_add(platform_device);
2074 goto fail_platform_device2;
2076 buffer = kzalloc(sizeof(struct calling_interface_buffer), GFP_KERNEL);
2083 ret = dell_setup_rfkill();
2086 pr_warn("Unable to setup rfkill\n");
2090 if (quirks && quirks->touchpad_led)
2091 touchpad_led_init(&platform_device->dev);
2093 kbd_led_init(&platform_device->dev);
2095 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2096 if (dell_laptop_dir != NULL)
2097 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2098 &dell_debugfs_fops);
2100 dell_laptop_register_notifier(&dell_laptop_notifier);
2102 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2105 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2107 dell_set_arguments(token->location, 0, 0, 0);
2108 ret = dell_send_request(CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2110 max_intensity = buffer->output[3];
2113 if (max_intensity) {
2114 struct backlight_properties props;
2115 memset(&props, 0, sizeof(struct backlight_properties));
2116 props.type = BACKLIGHT_PLATFORM;
2117 props.max_brightness = max_intensity;
2118 dell_backlight_device = backlight_device_register("dell_backlight",
2119 &platform_device->dev,
2124 if (IS_ERR(dell_backlight_device)) {
2125 ret = PTR_ERR(dell_backlight_device);
2126 dell_backlight_device = NULL;
2127 goto fail_backlight;
2130 dell_backlight_device->props.brightness =
2131 dell_get_intensity(dell_backlight_device);
2132 if (dell_backlight_device->props.brightness < 0) {
2133 ret = dell_backlight_device->props.brightness;
2134 goto fail_get_brightness;
2136 backlight_update_status(dell_backlight_device);
2141 fail_get_brightness:
2142 backlight_device_unregister(dell_backlight_device);
2146 dell_cleanup_rfkill();
2148 platform_device_del(platform_device);
2149 fail_platform_device2:
2150 platform_device_put(platform_device);
2151 fail_platform_device1:
2152 platform_driver_unregister(&platform_driver);
2153 fail_platform_driver:
2157 static void __exit dell_exit(void)
2159 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2160 debugfs_remove_recursive(dell_laptop_dir);
2161 if (quirks && quirks->touchpad_led)
2162 touchpad_led_exit();
2164 backlight_device_unregister(dell_backlight_device);
2166 dell_cleanup_rfkill();
2167 if (platform_device) {
2168 platform_device_unregister(platform_device);
2169 platform_driver_unregister(&platform_driver);
2173 /* dell-rbtn.c driver export functions which will not work correctly (and could
2174 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2175 * not problem when dell-rbtn.c is compiled as external module. When both files
2176 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2177 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2178 * This can be achieved by late_initcall() instead module_init().
2180 late_initcall(dell_init);
2181 module_exit(dell_exit);
2186 MODULE_DESCRIPTION("Dell laptop driver");
2187 MODULE_LICENSE("GPL");