2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/err.h>
24 #include <linux/dmi.h>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
38 #define BRIGHTNESS_TOKEN 0x7d
39 #define KBD_LED_OFF_TOKEN 0x01E1
40 #define KBD_LED_ON_TOKEN 0x01E2
41 #define KBD_LED_AUTO_TOKEN 0x01E3
42 #define KBD_LED_AUTO_25_TOKEN 0x02EA
43 #define KBD_LED_AUTO_50_TOKEN 0x02EB
44 #define KBD_LED_AUTO_75_TOKEN 0x02EC
45 #define KBD_LED_AUTO_100_TOKEN 0x02F6
46 #define GLOBAL_MIC_MUTE_ENABLE 0x0364
47 #define GLOBAL_MIC_MUTE_DISABLE 0x0365
52 int needs_kbd_timeouts;
54 * Ordered list of timeouts expressed in seconds.
55 * The list must end with -1
60 static struct quirk_entry *quirks;
62 static struct quirk_entry quirk_dell_vostro_v130 = {
66 static int __init dmi_matched(const struct dmi_system_id *dmi)
68 quirks = dmi->driver_data;
73 * These values come from Windows utility provided by Dell. If any other value
74 * is used then BIOS silently set timeout to 0 without any error message.
76 static struct quirk_entry quirk_dell_xps13_9333 = {
77 .needs_kbd_timeouts = 1,
78 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
81 static struct platform_driver platform_driver = {
83 .name = "dell-laptop",
87 static struct platform_device *platform_device;
88 static struct backlight_device *dell_backlight_device;
89 static struct rfkill *wifi_rfkill;
90 static struct rfkill *bluetooth_rfkill;
91 static struct rfkill *wwan_rfkill;
92 static bool force_rfkill;
94 module_param(force_rfkill, bool, 0444);
95 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
97 static const struct dmi_system_id dell_device_table[] __initconst = {
99 .ident = "Dell laptop",
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
118 .ident = "Dell Computer Corporation",
120 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
121 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
126 MODULE_DEVICE_TABLE(dmi, dell_device_table);
128 static const struct dmi_system_id dell_quirks[] __initconst = {
130 .callback = dmi_matched,
131 .ident = "Dell Vostro V130",
133 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
134 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
136 .driver_data = &quirk_dell_vostro_v130,
139 .callback = dmi_matched,
140 .ident = "Dell Vostro V131",
142 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
143 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
145 .driver_data = &quirk_dell_vostro_v130,
148 .callback = dmi_matched,
149 .ident = "Dell Vostro 3350",
151 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
152 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
154 .driver_data = &quirk_dell_vostro_v130,
157 .callback = dmi_matched,
158 .ident = "Dell Vostro 3555",
160 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
161 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
163 .driver_data = &quirk_dell_vostro_v130,
166 .callback = dmi_matched,
167 .ident = "Dell Inspiron N311z",
169 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
170 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
172 .driver_data = &quirk_dell_vostro_v130,
175 .callback = dmi_matched,
176 .ident = "Dell Inspiron M5110",
178 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
179 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
181 .driver_data = &quirk_dell_vostro_v130,
184 .callback = dmi_matched,
185 .ident = "Dell Vostro 3360",
187 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
188 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
190 .driver_data = &quirk_dell_vostro_v130,
193 .callback = dmi_matched,
194 .ident = "Dell Vostro 3460",
196 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
197 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
199 .driver_data = &quirk_dell_vostro_v130,
202 .callback = dmi_matched,
203 .ident = "Dell Vostro 3560",
205 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
206 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
208 .driver_data = &quirk_dell_vostro_v130,
211 .callback = dmi_matched,
212 .ident = "Dell Vostro 3450",
214 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
215 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
217 .driver_data = &quirk_dell_vostro_v130,
220 .callback = dmi_matched,
221 .ident = "Dell Inspiron 5420",
223 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
224 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
226 .driver_data = &quirk_dell_vostro_v130,
229 .callback = dmi_matched,
230 .ident = "Dell Inspiron 5520",
232 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
233 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
235 .driver_data = &quirk_dell_vostro_v130,
238 .callback = dmi_matched,
239 .ident = "Dell Inspiron 5720",
241 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
242 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
244 .driver_data = &quirk_dell_vostro_v130,
247 .callback = dmi_matched,
248 .ident = "Dell Inspiron 7420",
250 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
251 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
253 .driver_data = &quirk_dell_vostro_v130,
256 .callback = dmi_matched,
257 .ident = "Dell Inspiron 7520",
259 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
260 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
262 .driver_data = &quirk_dell_vostro_v130,
265 .callback = dmi_matched,
266 .ident = "Dell Inspiron 7720",
268 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
269 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
271 .driver_data = &quirk_dell_vostro_v130,
274 .callback = dmi_matched,
275 .ident = "Dell XPS13 9333",
277 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
278 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
280 .driver_data = &quirk_dell_xps13_9333,
286 * Derived from information in smbios-wireless-ctl:
288 * cbSelect 17, Value 11
290 * Return Wireless Info
291 * cbArg1, byte0 = 0x00
293 * cbRes1 Standard return codes (0, -1, -2)
294 * cbRes2 Info bit flags:
296 * 0 Hardware switch supported (1)
297 * 1 WiFi locator supported (1)
298 * 2 WLAN supported (1)
299 * 3 Bluetooth (BT) supported (1)
300 * 4 WWAN supported (1)
301 * 5 Wireless KBD supported (1)
302 * 6 Uw b supported (1)
303 * 7 WiGig supported (1)
304 * 8 WLAN installed (1)
306 * 10 WWAN installed (1)
307 * 11 Uw b installed (1)
308 * 12 WiGig installed (1)
310 * 16 Hardware (HW) switch is On (1)
311 * 17 WLAN disabled (1)
313 * 19 WWAN disabled (1)
314 * 20 Uw b disabled (1)
315 * 21 WiGig disabled (1)
318 * cbRes3 NVRAM size in bytes
319 * cbRes4, byte 0 NVRAM format version number
322 * Set QuickSet Radio Disable Flag
323 * cbArg1, byte0 = 0x01
332 * cbArg1, byte2 Flag bits:
333 * 0 QuickSet disables radio (1)
336 * cbRes1 Standard return codes (0, -1, -2)
337 * cbRes2 QuickSet (QS) radio disable bit map:
342 * 4 QS disables WIGIG
345 * Wireless Switch Configuration
346 * cbArg1, byte0 = 0x02
352 * 2 Set WiFi locator enable/disable
354 * Switch settings (if byte 1==1):
355 * 0 WLAN sw itch control (1)
356 * 1 BT sw itch control (1)
357 * 2 WWAN sw itch control (1)
358 * 3 UWB sw itch control (1)
359 * 4 WiGig sw itch control (1)
361 * cbArg1, byte2 Enable bits (if byte 1==2):
362 * 0 Enable WiFi locator (1)
364 * cbRes1 Standard return codes (0, -1, -2)
365 * cbRes2 QuickSet radio disable bit map:
366 * 0 WLAN controlled by sw itch (1)
367 * 1 BT controlled by sw itch (1)
368 * 2 WWAN controlled by sw itch (1)
369 * 3 UWB controlled by sw itch (1)
370 * 4 WiGig controlled by sw itch (1)
372 * 7 Wireless sw itch config locked (1)
373 * 8 WiFi locator enabled (1)
375 * 15 WiFi locator setting locked (1)
378 * Read Local Config Data (LCD)
379 * cbArg1, byte0 = 0x10
380 * cbArg1, byte1 NVRAM index low byte
381 * cbArg1, byte2 NVRAM index high byte
382 * cbRes1 Standard return codes (0, -1, -2)
383 * cbRes2 4 bytes read from LCD[index]
384 * cbRes3 4 bytes read from LCD[index+4]
385 * cbRes4 4 bytes read from LCD[index+8]
387 * Write Local Config Data (LCD)
388 * cbArg1, byte0 = 0x11
389 * cbArg1, byte1 NVRAM index low byte
390 * cbArg1, byte2 NVRAM index high byte
391 * cbArg2 4 bytes to w rite at LCD[index]
392 * cbArg3 4 bytes to w rite at LCD[index+4]
393 * cbArg4 4 bytes to w rite at LCD[index+8]
394 * cbRes1 Standard return codes (0, -1, -2)
396 * Populate Local Config Data from NVRAM
397 * cbArg1, byte0 = 0x12
398 * cbRes1 Standard return codes (0, -1, -2)
400 * Commit Local Config Data to NVRAM
401 * cbArg1, byte0 = 0x13
402 * cbRes1 Standard return codes (0, -1, -2)
405 static int dell_rfkill_set(void *data, bool blocked)
407 struct calling_interface_buffer *buffer;
408 int disable = blocked ? 1 : 0;
409 unsigned long radio = (unsigned long)data;
410 int hwswitch_bit = (unsigned long)data - 1;
415 buffer = dell_smbios_get_buffer();
417 dell_smbios_send_request(17, 11);
418 ret = buffer->output[0];
419 status = buffer->output[1];
424 dell_smbios_clear_buffer();
426 buffer->input[0] = 0x2;
427 dell_smbios_send_request(17, 11);
428 ret = buffer->output[0];
429 hwswitch = buffer->output[1];
431 /* If the hardware switch controls this radio, and the hardware
432 switch is disabled, always disable the radio */
433 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
434 (status & BIT(0)) && !(status & BIT(16)))
437 dell_smbios_clear_buffer();
439 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
440 dell_smbios_send_request(17, 11);
441 ret = buffer->output[0];
444 dell_smbios_release_buffer();
445 return dell_smbios_error(ret);
448 /* Must be called with the buffer held */
449 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
451 struct calling_interface_buffer *buffer)
453 if (status & BIT(0)) {
454 /* Has hw-switch, sync sw_state to BIOS */
455 int block = rfkill_blocked(rfkill);
456 dell_smbios_clear_buffer();
457 buffer->input[0] = (1 | (radio << 8) | (block << 16));
458 dell_smbios_send_request(17, 11);
460 /* No hw-switch, sync BIOS state to sw_state */
461 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
465 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
466 int status, int hwswitch)
468 if (hwswitch & (BIT(radio - 1)))
469 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
472 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
474 struct calling_interface_buffer *buffer;
475 int radio = ((unsigned long)data & 0xF);
480 buffer = dell_smbios_get_buffer();
482 dell_smbios_send_request(17, 11);
483 ret = buffer->output[0];
484 status = buffer->output[1];
486 if (ret != 0 || !(status & BIT(0))) {
487 dell_smbios_release_buffer();
491 dell_smbios_clear_buffer();
493 buffer->input[0] = 0x2;
494 dell_smbios_send_request(17, 11);
495 ret = buffer->output[0];
496 hwswitch = buffer->output[1];
498 dell_smbios_release_buffer();
503 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
506 static const struct rfkill_ops dell_rfkill_ops = {
507 .set_block = dell_rfkill_set,
508 .query = dell_rfkill_query,
511 static struct dentry *dell_laptop_dir;
513 static int dell_debugfs_show(struct seq_file *s, void *data)
515 struct calling_interface_buffer *buffer;
521 buffer = dell_smbios_get_buffer();
523 dell_smbios_send_request(17, 11);
524 ret = buffer->output[0];
525 status = buffer->output[1];
527 dell_smbios_clear_buffer();
529 buffer->input[0] = 0x2;
530 dell_smbios_send_request(17, 11);
531 hwswitch_ret = buffer->output[0];
532 hwswitch_state = buffer->output[1];
534 dell_smbios_release_buffer();
536 seq_printf(s, "return:\t%d\n", ret);
537 seq_printf(s, "status:\t0x%X\n", status);
538 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
540 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
541 (status & BIT(1)) >> 1);
542 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
543 (status & BIT(2)) >> 2);
544 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
545 (status & BIT(3)) >> 3);
546 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
547 (status & BIT(4)) >> 4);
548 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
549 (status & BIT(5)) >> 5);
550 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
551 (status & BIT(6)) >> 6);
552 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
553 (status & BIT(7)) >> 7);
554 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
555 (status & BIT(8)) >> 8);
556 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
557 (status & BIT(9)) >> 9);
558 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
559 (status & BIT(10)) >> 10);
560 seq_printf(s, "Bit 11: UWB installed: %lu\n",
561 (status & BIT(11)) >> 11);
562 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
563 (status & BIT(12)) >> 12);
565 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
566 (status & BIT(16)) >> 16);
567 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
568 (status & BIT(17)) >> 17);
569 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
570 (status & BIT(18)) >> 18);
571 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
572 (status & BIT(19)) >> 19);
573 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
574 (status & BIT(20)) >> 20);
575 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
576 (status & BIT(21)) >> 21);
578 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
579 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
580 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
581 hwswitch_state & BIT(0));
582 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
583 (hwswitch_state & BIT(1)) >> 1);
584 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
585 (hwswitch_state & BIT(2)) >> 2);
586 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
587 (hwswitch_state & BIT(3)) >> 3);
588 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
589 (hwswitch_state & BIT(4)) >> 4);
590 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
591 (hwswitch_state & BIT(7)) >> 7);
592 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
593 (hwswitch_state & BIT(8)) >> 8);
594 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
595 (hwswitch_state & BIT(15)) >> 15);
600 static int dell_debugfs_open(struct inode *inode, struct file *file)
602 return single_open(file, dell_debugfs_show, inode->i_private);
605 static const struct file_operations dell_debugfs_fops = {
606 .owner = THIS_MODULE,
607 .open = dell_debugfs_open,
610 .release = single_release,
613 static void dell_update_rfkill(struct work_struct *ignored)
615 struct calling_interface_buffer *buffer;
620 buffer = dell_smbios_get_buffer();
622 dell_smbios_send_request(17, 11);
623 ret = buffer->output[0];
624 status = buffer->output[1];
629 dell_smbios_clear_buffer();
631 buffer->input[0] = 0x2;
632 dell_smbios_send_request(17, 11);
633 ret = buffer->output[0];
635 if (ret == 0 && (status & BIT(0)))
636 hwswitch = buffer->output[1];
639 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
640 dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
642 if (bluetooth_rfkill) {
643 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
645 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
649 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
650 dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
654 dell_smbios_release_buffer();
656 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
658 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
661 static bool extended;
663 if (str & I8042_STR_AUXDATA)
666 if (unlikely(data == 0xe0)) {
669 } else if (unlikely(extended)) {
672 schedule_delayed_work(&dell_rfkill_work,
673 round_jiffies_relative(HZ / 4));
682 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
683 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
685 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
686 unsigned long action, void *data)
688 schedule_delayed_work(&dell_rfkill_work, 0);
692 static struct notifier_block dell_laptop_rbtn_notifier = {
693 .notifier_call = dell_laptop_rbtn_notifier_call,
696 static int __init dell_setup_rfkill(void)
698 struct calling_interface_buffer *buffer;
699 int status, ret, whitelisted;
703 * rfkill support causes trouble on various models, mostly Inspirons.
704 * So we whitelist certain series, and don't support rfkill on others.
707 product = dmi_get_system_info(DMI_PRODUCT_NAME);
708 if (product && (strncmp(product, "Latitude", 8) == 0 ||
709 strncmp(product, "Precision", 9) == 0))
711 if (!force_rfkill && !whitelisted)
714 buffer = dell_smbios_get_buffer();
715 dell_smbios_send_request(17, 11);
716 ret = buffer->output[0];
717 status = buffer->output[1];
718 dell_smbios_release_buffer();
720 /* dell wireless info smbios call is not supported */
724 /* rfkill is only tested on laptops with a hwswitch */
725 if (!(status & BIT(0)) && !force_rfkill)
728 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
729 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
731 &dell_rfkill_ops, (void *) 1);
736 ret = rfkill_register(wifi_rfkill);
741 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
742 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
743 &platform_device->dev,
744 RFKILL_TYPE_BLUETOOTH,
745 &dell_rfkill_ops, (void *) 2);
746 if (!bluetooth_rfkill) {
750 ret = rfkill_register(bluetooth_rfkill);
755 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
756 wwan_rfkill = rfkill_alloc("dell-wwan",
757 &platform_device->dev,
759 &dell_rfkill_ops, (void *) 3);
764 ret = rfkill_register(wwan_rfkill);
770 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
771 * which can receive events from HW slider switch.
773 * Dell SMBIOS on whitelisted models supports controlling radio devices
774 * but does not support receiving HW button switch events. We can use
775 * i8042 filter hook function to receive keyboard data and handle
776 * keycode for HW button.
778 * So if it is possible we will use Dell Airplane Mode Switch ACPI
779 * driver for receiving HW events and Dell SMBIOS for setting rfkill
780 * states. If ACPI driver or device is not available we will fallback to
781 * i8042 filter hook function.
783 * To prevent duplicate rfkill devices which control and do same thing,
784 * dell-rbtn driver will automatically remove its own rfkill devices
785 * once function dell_rbtn_notifier_register() is called.
788 dell_rbtn_notifier_register_func =
789 symbol_request(dell_rbtn_notifier_register);
790 if (dell_rbtn_notifier_register_func) {
791 dell_rbtn_notifier_unregister_func =
792 symbol_request(dell_rbtn_notifier_unregister);
793 if (!dell_rbtn_notifier_unregister_func) {
794 symbol_put(dell_rbtn_notifier_register);
795 dell_rbtn_notifier_register_func = NULL;
799 if (dell_rbtn_notifier_register_func) {
800 ret = dell_rbtn_notifier_register_func(
801 &dell_laptop_rbtn_notifier);
802 symbol_put(dell_rbtn_notifier_register);
803 dell_rbtn_notifier_register_func = NULL;
805 symbol_put(dell_rbtn_notifier_unregister);
806 dell_rbtn_notifier_unregister_func = NULL;
809 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
814 pr_info("Using dell-rbtn acpi driver for receiving events\n");
815 } else if (ret != -ENODEV) {
816 pr_warn("Unable to register dell rbtn notifier\n");
819 ret = i8042_install_filter(dell_laptop_i8042_filter);
821 pr_warn("Unable to install key filter\n");
824 pr_info("Using i8042 filter function for receiving events\n");
830 rfkill_unregister(wwan_rfkill);
832 rfkill_destroy(wwan_rfkill);
833 if (bluetooth_rfkill)
834 rfkill_unregister(bluetooth_rfkill);
836 rfkill_destroy(bluetooth_rfkill);
838 rfkill_unregister(wifi_rfkill);
840 rfkill_destroy(wifi_rfkill);
845 static void dell_cleanup_rfkill(void)
847 if (dell_rbtn_notifier_unregister_func) {
848 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
849 symbol_put(dell_rbtn_notifier_unregister);
850 dell_rbtn_notifier_unregister_func = NULL;
852 i8042_remove_filter(dell_laptop_i8042_filter);
854 cancel_delayed_work_sync(&dell_rfkill_work);
856 rfkill_unregister(wifi_rfkill);
857 rfkill_destroy(wifi_rfkill);
859 if (bluetooth_rfkill) {
860 rfkill_unregister(bluetooth_rfkill);
861 rfkill_destroy(bluetooth_rfkill);
864 rfkill_unregister(wwan_rfkill);
865 rfkill_destroy(wwan_rfkill);
869 static int dell_send_intensity(struct backlight_device *bd)
871 struct calling_interface_buffer *buffer;
872 struct calling_interface_token *token;
875 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
879 buffer = dell_smbios_get_buffer();
880 buffer->input[0] = token->location;
881 buffer->input[1] = bd->props.brightness;
883 if (power_supply_is_system_supplied() > 0)
884 dell_smbios_send_request(1, 2);
886 dell_smbios_send_request(1, 1);
888 ret = dell_smbios_error(buffer->output[0]);
890 dell_smbios_release_buffer();
894 static int dell_get_intensity(struct backlight_device *bd)
896 struct calling_interface_buffer *buffer;
897 struct calling_interface_token *token;
900 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
904 buffer = dell_smbios_get_buffer();
905 buffer->input[0] = token->location;
907 if (power_supply_is_system_supplied() > 0)
908 dell_smbios_send_request(0, 2);
910 dell_smbios_send_request(0, 1);
912 if (buffer->output[0])
913 ret = dell_smbios_error(buffer->output[0]);
915 ret = buffer->output[1];
917 dell_smbios_release_buffer();
921 static const struct backlight_ops dell_ops = {
922 .get_brightness = dell_get_intensity,
923 .update_status = dell_send_intensity,
926 static void touchpad_led_on(void)
930 i8042_command(&data, command | 1 << 12);
933 static void touchpad_led_off(void)
937 i8042_command(&data, command | 1 << 12);
940 static void touchpad_led_set(struct led_classdev *led_cdev,
941 enum led_brightness value)
949 static struct led_classdev touchpad_led = {
950 .name = "dell-laptop::touchpad",
951 .brightness_set = touchpad_led_set,
952 .flags = LED_CORE_SUSPENDRESUME,
955 static int __init touchpad_led_init(struct device *dev)
957 return led_classdev_register(dev, &touchpad_led);
960 static void touchpad_led_exit(void)
962 led_classdev_unregister(&touchpad_led);
966 * Derived from information in smbios-keyboard-ctl:
970 * Keyboard illumination
971 * cbArg1 determines the function to be performed
973 * cbArg1 0x0 = Get Feature Information
974 * cbRES1 Standard return codes (0, -1, -2)
975 * cbRES2, word0 Bitmap of user-selectable modes
976 * bit 0 Always off (All systems)
977 * bit 1 Always on (Travis ATG, Siberia)
978 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
979 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
980 * bit 4 Auto: Input-activity-based On; input-activity based Off
981 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
982 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
983 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
984 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
985 * bits 9-15 Reserved for future use
986 * cbRES2, byte2 Reserved for future use
987 * cbRES2, byte3 Keyboard illumination type
991 * 3-255 Reserved for future use
992 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
993 * bit 0 Any keystroke
994 * bit 1 Touchpad activity
995 * bit 2 Pointing stick
997 * bits 4-7 Reserved for future use
998 * cbRES3, byte1 Supported timeout unit bitmap
1003 * bits 4-7 Reserved for future use
1004 * cbRES3, byte2 Number of keyboard light brightness levels
1005 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1006 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1007 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1008 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1010 * cbArg1 0x1 = Get Current State
1011 * cbRES1 Standard return codes (0, -1, -2)
1012 * cbRES2, word0 Bitmap of current mode state
1013 * bit 0 Always off (All systems)
1014 * bit 1 Always on (Travis ATG, Siberia)
1015 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1016 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1017 * bit 4 Auto: Input-activity-based On; input-activity based Off
1018 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1019 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1020 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1021 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1022 * bits 9-15 Reserved for future use
1023 * Note: Only One bit can be set
1024 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1025 * bit 0 Any keystroke
1026 * bit 1 Touchpad activity
1027 * bit 2 Pointing stick
1029 * bits 4-7 Reserved for future use
1030 * cbRES2, byte3 Current Timeout
1031 * bits 7:6 Timeout units indicator:
1036 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1037 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1038 * are set upon return from the [Get feature information] call.
1039 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1040 * cbRES3, byte1 Current ALS reading
1041 * cbRES3, byte2 Current keyboard light level.
1043 * cbArg1 0x2 = Set New State
1044 * cbRES1 Standard return codes (0, -1, -2)
1045 * cbArg2, word0 Bitmap of current mode state
1046 * bit 0 Always off (All systems)
1047 * bit 1 Always on (Travis ATG, Siberia)
1048 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1049 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1050 * bit 4 Auto: Input-activity-based On; input-activity based Off
1051 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1052 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1053 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1054 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1055 * bits 9-15 Reserved for future use
1056 * Note: Only One bit can be set
1057 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1058 * keyboard to turn off automatically.
1059 * bit 0 Any keystroke
1060 * bit 1 Touchpad activity
1061 * bit 2 Pointing stick
1063 * bits 4-7 Reserved for future use
1064 * cbArg2, byte3 Desired Timeout
1065 * bits 7:6 Timeout units indicator:
1070 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1071 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1072 * cbArg3, byte2 Desired keyboard light level.
1076 enum kbd_timeout_unit {
1077 KBD_TIMEOUT_SECONDS = 0,
1078 KBD_TIMEOUT_MINUTES,
1084 KBD_MODE_BIT_OFF = 0,
1087 KBD_MODE_BIT_TRIGGER_ALS,
1088 KBD_MODE_BIT_TRIGGER,
1089 KBD_MODE_BIT_TRIGGER_25,
1090 KBD_MODE_BIT_TRIGGER_50,
1091 KBD_MODE_BIT_TRIGGER_75,
1092 KBD_MODE_BIT_TRIGGER_100,
1095 #define kbd_is_als_mode_bit(bit) \
1096 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1097 #define kbd_is_trigger_mode_bit(bit) \
1098 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1099 #define kbd_is_level_mode_bit(bit) \
1100 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1123 static const int kbd_tokens[] = {
1125 KBD_LED_AUTO_25_TOKEN,
1126 KBD_LED_AUTO_50_TOKEN,
1127 KBD_LED_AUTO_75_TOKEN,
1128 KBD_LED_AUTO_100_TOKEN,
1132 static u16 kbd_token_bits;
1134 static struct kbd_info kbd_info;
1135 static bool kbd_als_supported;
1136 static bool kbd_triggers_supported;
1138 static u8 kbd_mode_levels[16];
1139 static int kbd_mode_levels_count;
1141 static u8 kbd_previous_level;
1142 static u8 kbd_previous_mode_bit;
1144 static bool kbd_led_present;
1147 * NOTE: there are three ways to set the keyboard backlight level.
1148 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1149 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1150 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1152 * There are laptops which support only one of these methods. If we want to
1153 * support as many machines as possible we need to implement all three methods.
1154 * The first two methods use the kbd_state structure. The third uses SMBIOS
1155 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1156 * keyboard backlight level via kbd_state.level.
1159 static int kbd_get_info(struct kbd_info *info)
1161 struct calling_interface_buffer *buffer;
1165 buffer = dell_smbios_get_buffer();
1167 buffer->input[0] = 0x0;
1168 dell_smbios_send_request(4, 11);
1169 ret = buffer->output[0];
1172 ret = dell_smbios_error(ret);
1176 info->modes = buffer->output[1] & 0xFFFF;
1177 info->type = (buffer->output[1] >> 24) & 0xFF;
1178 info->triggers = buffer->output[2] & 0xFF;
1179 units = (buffer->output[2] >> 8) & 0xFF;
1180 info->levels = (buffer->output[2] >> 16) & 0xFF;
1183 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1185 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1187 info->hours = (buffer->output[3] >> 16) & 0xFF;
1189 info->days = (buffer->output[3] >> 24) & 0xFF;
1192 dell_smbios_release_buffer();
1196 static unsigned int kbd_get_max_level(void)
1198 if (kbd_info.levels != 0)
1199 return kbd_info.levels;
1200 if (kbd_mode_levels_count > 0)
1201 return kbd_mode_levels_count - 1;
1205 static int kbd_get_level(struct kbd_state *state)
1209 if (kbd_info.levels != 0)
1210 return state->level;
1212 if (kbd_mode_levels_count > 0) {
1213 for (i = 0; i < kbd_mode_levels_count; ++i)
1214 if (kbd_mode_levels[i] == state->mode_bit)
1222 static int kbd_set_level(struct kbd_state *state, u8 level)
1224 if (kbd_info.levels != 0) {
1226 kbd_previous_level = level;
1227 if (state->level == level)
1229 state->level = level;
1230 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1231 state->mode_bit = kbd_previous_mode_bit;
1232 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1233 kbd_previous_mode_bit = state->mode_bit;
1234 state->mode_bit = KBD_MODE_BIT_OFF;
1239 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1241 kbd_previous_level = level;
1242 state->mode_bit = kbd_mode_levels[level];
1249 static int kbd_get_state(struct kbd_state *state)
1251 struct calling_interface_buffer *buffer;
1254 buffer = dell_smbios_get_buffer();
1256 buffer->input[0] = 0x1;
1257 dell_smbios_send_request(4, 11);
1258 ret = buffer->output[0];
1261 ret = dell_smbios_error(ret);
1265 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1266 if (state->mode_bit != 0)
1269 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1270 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1271 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1272 state->als_setting = buffer->output[2] & 0xFF;
1273 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1274 state->level = (buffer->output[2] >> 16) & 0xFF;
1277 dell_smbios_release_buffer();
1281 static int kbd_set_state(struct kbd_state *state)
1283 struct calling_interface_buffer *buffer;
1286 buffer = dell_smbios_get_buffer();
1287 buffer->input[0] = 0x2;
1288 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1289 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1290 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1291 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1292 buffer->input[2] = state->als_setting & 0xFF;
1293 buffer->input[2] |= (state->level & 0xFF) << 16;
1294 dell_smbios_send_request(4, 11);
1295 ret = buffer->output[0];
1296 dell_smbios_release_buffer();
1298 return dell_smbios_error(ret);
1301 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1305 ret = kbd_set_state(state);
1310 * When setting the new state fails,try to restore the previous one.
1311 * This is needed on some machines where BIOS sets a default state when
1312 * setting a new state fails. This default state could be all off.
1315 if (kbd_set_state(old))
1316 pr_err("Setting old previous keyboard state failed\n");
1321 static int kbd_set_token_bit(u8 bit)
1323 struct calling_interface_buffer *buffer;
1324 struct calling_interface_token *token;
1327 if (bit >= ARRAY_SIZE(kbd_tokens))
1330 token = dell_smbios_find_token(kbd_tokens[bit]);
1334 buffer = dell_smbios_get_buffer();
1335 buffer->input[0] = token->location;
1336 buffer->input[1] = token->value;
1337 dell_smbios_send_request(1, 0);
1338 ret = buffer->output[0];
1339 dell_smbios_release_buffer();
1341 return dell_smbios_error(ret);
1344 static int kbd_get_token_bit(u8 bit)
1346 struct calling_interface_buffer *buffer;
1347 struct calling_interface_token *token;
1351 if (bit >= ARRAY_SIZE(kbd_tokens))
1354 token = dell_smbios_find_token(kbd_tokens[bit]);
1358 buffer = dell_smbios_get_buffer();
1359 buffer->input[0] = token->location;
1360 dell_smbios_send_request(0, 0);
1361 ret = buffer->output[0];
1362 val = buffer->output[1];
1363 dell_smbios_release_buffer();
1366 return dell_smbios_error(ret);
1368 return (val == token->value);
1371 static int kbd_get_first_active_token_bit(void)
1376 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1377 ret = kbd_get_token_bit(i);
1385 static int kbd_get_valid_token_counts(void)
1387 return hweight16(kbd_token_bits);
1390 static inline int kbd_init_info(void)
1392 struct kbd_state state;
1396 ret = kbd_get_info(&kbd_info);
1400 kbd_get_state(&state);
1402 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1403 if (kbd_info.seconds > 63)
1404 kbd_info.seconds = 63;
1405 if (kbd_info.minutes > 63)
1406 kbd_info.minutes = 63;
1407 if (kbd_info.hours > 63)
1408 kbd_info.hours = 63;
1409 if (kbd_info.days > 63)
1412 /* NOTE: On tested machines ON mode did not work and caused
1413 * problems (turned backlight off) so do not use it
1415 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1417 kbd_previous_level = kbd_get_level(&state);
1418 kbd_previous_mode_bit = state.mode_bit;
1420 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1421 kbd_previous_level = 1;
1423 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1424 kbd_previous_mode_bit =
1425 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1426 if (kbd_previous_mode_bit != 0)
1427 kbd_previous_mode_bit--;
1430 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1431 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1432 kbd_als_supported = true;
1434 if (kbd_info.modes & (
1435 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1436 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1437 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1439 kbd_triggers_supported = true;
1441 /* kbd_mode_levels[0] is reserved, see below */
1442 for (i = 0; i < 16; ++i)
1443 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1444 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1447 * Find the first supported mode and assign to kbd_mode_levels[0].
1448 * This should be 0 (off), but we cannot depend on the BIOS to
1451 if (kbd_mode_levels_count > 0) {
1452 for (i = 0; i < 16; ++i) {
1453 if (BIT(i) & kbd_info.modes) {
1454 kbd_mode_levels[0] = i;
1458 kbd_mode_levels_count++;
1465 static inline void kbd_init_tokens(void)
1469 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1470 if (dell_smbios_find_token(kbd_tokens[i]))
1471 kbd_token_bits |= BIT(i);
1474 static void kbd_init(void)
1478 ret = kbd_init_info();
1481 if (kbd_token_bits != 0 || ret == 0)
1482 kbd_led_present = true;
1485 static ssize_t kbd_led_timeout_store(struct device *dev,
1486 struct device_attribute *attr,
1487 const char *buf, size_t count)
1489 struct kbd_state new_state;
1490 struct kbd_state state;
1498 ret = sscanf(buf, "%d %c", &value, &ch);
1511 if (value > kbd_info.seconds)
1513 unit = KBD_TIMEOUT_SECONDS;
1516 if (value > kbd_info.minutes)
1518 unit = KBD_TIMEOUT_MINUTES;
1521 if (value > kbd_info.hours)
1523 unit = KBD_TIMEOUT_HOURS;
1526 if (value > kbd_info.days)
1528 unit = KBD_TIMEOUT_DAYS;
1534 if (quirks && quirks->needs_kbd_timeouts)
1538 /* Convert value from current units to seconds */
1540 case KBD_TIMEOUT_DAYS:
1542 case KBD_TIMEOUT_HOURS:
1544 case KBD_TIMEOUT_MINUTES:
1546 unit = KBD_TIMEOUT_SECONDS;
1549 if (quirks && quirks->needs_kbd_timeouts) {
1550 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1551 if (value <= quirks->kbd_timeouts[i]) {
1552 value = quirks->kbd_timeouts[i];
1558 if (value <= kbd_info.seconds && kbd_info.seconds) {
1559 unit = KBD_TIMEOUT_SECONDS;
1560 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1562 unit = KBD_TIMEOUT_MINUTES;
1563 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1565 unit = KBD_TIMEOUT_HOURS;
1566 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1567 value /= (60 * 60 * 24);
1568 unit = KBD_TIMEOUT_DAYS;
1574 ret = kbd_get_state(&state);
1579 new_state.timeout_value = value;
1580 new_state.timeout_unit = unit;
1582 ret = kbd_set_state_safe(&new_state, &state);
1589 static ssize_t kbd_led_timeout_show(struct device *dev,
1590 struct device_attribute *attr, char *buf)
1592 struct kbd_state state;
1596 ret = kbd_get_state(&state);
1600 len = sprintf(buf, "%d", state.timeout_value);
1602 switch (state.timeout_unit) {
1603 case KBD_TIMEOUT_SECONDS:
1604 return len + sprintf(buf+len, "s\n");
1605 case KBD_TIMEOUT_MINUTES:
1606 return len + sprintf(buf+len, "m\n");
1607 case KBD_TIMEOUT_HOURS:
1608 return len + sprintf(buf+len, "h\n");
1609 case KBD_TIMEOUT_DAYS:
1610 return len + sprintf(buf+len, "d\n");
1618 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1619 kbd_led_timeout_show, kbd_led_timeout_store);
1621 static const char * const kbd_led_triggers[] = {
1624 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1628 static ssize_t kbd_led_triggers_store(struct device *dev,
1629 struct device_attribute *attr,
1630 const char *buf, size_t count)
1632 struct kbd_state new_state;
1633 struct kbd_state state;
1634 bool triggers_enabled = false;
1635 int trigger_bit = -1;
1639 ret = sscanf(buf, "%20s", trigger);
1643 if (trigger[0] != '+' && trigger[0] != '-')
1646 ret = kbd_get_state(&state);
1650 if (kbd_triggers_supported)
1651 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1653 if (kbd_triggers_supported) {
1654 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1655 if (!(kbd_info.triggers & BIT(i)))
1657 if (!kbd_led_triggers[i])
1659 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1661 if (trigger[0] == '+' &&
1662 triggers_enabled && (state.triggers & BIT(i)))
1664 if (trigger[0] == '-' &&
1665 (!triggers_enabled || !(state.triggers & BIT(i))))
1672 if (trigger_bit != -1) {
1674 if (trigger[0] == '+')
1675 new_state.triggers |= BIT(trigger_bit);
1677 new_state.triggers &= ~BIT(trigger_bit);
1678 /* NOTE: trackstick bit (2) must be disabled when
1679 * disabling touchpad bit (1), otherwise touchpad
1680 * bit (1) will not be disabled */
1681 if (trigger_bit == 1)
1682 new_state.triggers &= ~BIT(2);
1684 if ((kbd_info.triggers & new_state.triggers) !=
1687 if (new_state.triggers && !triggers_enabled) {
1688 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1689 kbd_set_level(&new_state, kbd_previous_level);
1690 } else if (new_state.triggers == 0) {
1691 kbd_set_level(&new_state, 0);
1693 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;
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 ret = kbd_get_state(&state);
1764 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1769 if (kbd_triggers_supported)
1770 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1773 if (triggers_enabled)
1774 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1776 new_state.mode_bit = KBD_MODE_BIT_ALS;
1778 if (triggers_enabled) {
1779 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1780 kbd_set_level(&new_state, kbd_previous_level);
1782 new_state.mode_bit = KBD_MODE_BIT_ON;
1785 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1788 ret = kbd_set_state_safe(&new_state, &state);
1791 kbd_previous_mode_bit = new_state.mode_bit;
1796 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1797 struct device_attribute *attr,
1800 struct kbd_state state;
1801 bool enabled = false;
1804 ret = kbd_get_state(&state);
1807 enabled = kbd_is_als_mode_bit(state.mode_bit);
1809 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1812 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1813 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1815 static ssize_t kbd_led_als_setting_store(struct device *dev,
1816 struct device_attribute *attr,
1817 const char *buf, size_t count)
1819 struct kbd_state state;
1820 struct kbd_state new_state;
1824 ret = kstrtou8(buf, 10, &setting);
1828 ret = kbd_get_state(&state);
1833 new_state.als_setting = setting;
1835 ret = kbd_set_state_safe(&new_state, &state);
1842 static ssize_t kbd_led_als_setting_show(struct device *dev,
1843 struct device_attribute *attr,
1846 struct kbd_state state;
1849 ret = kbd_get_state(&state);
1853 return sprintf(buf, "%d\n", state.als_setting);
1856 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1857 kbd_led_als_setting_show, kbd_led_als_setting_store);
1859 static struct attribute *kbd_led_attrs[] = {
1860 &dev_attr_stop_timeout.attr,
1861 &dev_attr_start_triggers.attr,
1865 static const struct attribute_group kbd_led_group = {
1866 .attrs = kbd_led_attrs,
1869 static struct attribute *kbd_led_als_attrs[] = {
1870 &dev_attr_als_enabled.attr,
1871 &dev_attr_als_setting.attr,
1875 static const struct attribute_group kbd_led_als_group = {
1876 .attrs = kbd_led_als_attrs,
1879 static const struct attribute_group *kbd_led_groups[] = {
1885 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1889 struct kbd_state state;
1891 if (kbd_get_max_level()) {
1892 ret = kbd_get_state(&state);
1895 ret = kbd_get_level(&state);
1901 if (kbd_get_valid_token_counts()) {
1902 ret = kbd_get_first_active_token_bit();
1905 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1906 num &= num - 1; /* clear the first bit set */
1909 return ffs(num) - 1;
1912 pr_warn("Keyboard brightness level control not supported\n");
1916 static int kbd_led_level_set(struct led_classdev *led_cdev,
1917 enum led_brightness value)
1919 struct kbd_state state;
1920 struct kbd_state new_state;
1924 if (kbd_get_max_level()) {
1925 ret = kbd_get_state(&state);
1929 ret = kbd_set_level(&new_state, value);
1932 return kbd_set_state_safe(&new_state, &state);
1935 if (kbd_get_valid_token_counts()) {
1936 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1937 num &= num - 1; /* clear the first bit set */
1940 return kbd_set_token_bit(ffs(num) - 1);
1943 pr_warn("Keyboard brightness level control not supported\n");
1947 static struct led_classdev kbd_led = {
1948 .name = "dell::kbd_backlight",
1949 .brightness_set_blocking = kbd_led_level_set,
1950 .brightness_get = kbd_led_level_get,
1951 .groups = kbd_led_groups,
1954 static int __init kbd_led_init(struct device *dev)
1957 if (!kbd_led_present)
1959 if (!kbd_als_supported)
1960 kbd_led_groups[1] = NULL;
1961 kbd_led.max_brightness = kbd_get_max_level();
1962 if (!kbd_led.max_brightness) {
1963 kbd_led.max_brightness = kbd_get_valid_token_counts();
1964 if (kbd_led.max_brightness)
1965 kbd_led.max_brightness--;
1967 return led_classdev_register(dev, &kbd_led);
1970 static void brightness_set_exit(struct led_classdev *led_cdev,
1971 enum led_brightness value)
1973 /* Don't change backlight level on exit */
1976 static void kbd_led_exit(void)
1978 if (!kbd_led_present)
1980 kbd_led.brightness_set = brightness_set_exit;
1981 led_classdev_unregister(&kbd_led);
1984 int dell_micmute_led_set(int state)
1986 struct calling_interface_buffer *buffer;
1987 struct calling_interface_token *token;
1990 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
1991 else if (state == 1)
1992 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
1999 buffer = dell_smbios_get_buffer();
2000 buffer->input[0] = token->location;
2001 buffer->input[1] = token->value;
2002 dell_smbios_send_request(1, 0);
2003 dell_smbios_release_buffer();
2007 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2009 static int __init dell_init(void)
2011 struct calling_interface_buffer *buffer;
2012 struct calling_interface_token *token;
2013 int max_intensity = 0;
2016 if (!dmi_check_system(dell_device_table))
2020 /* find if this machine support other functions */
2021 dmi_check_system(dell_quirks);
2023 ret = platform_driver_register(&platform_driver);
2025 goto fail_platform_driver;
2026 platform_device = platform_device_alloc("dell-laptop", -1);
2027 if (!platform_device) {
2029 goto fail_platform_device1;
2031 ret = platform_device_add(platform_device);
2033 goto fail_platform_device2;
2035 ret = dell_setup_rfkill();
2038 pr_warn("Unable to setup rfkill\n");
2042 if (quirks && quirks->touchpad_led)
2043 touchpad_led_init(&platform_device->dev);
2045 kbd_led_init(&platform_device->dev);
2047 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2048 if (dell_laptop_dir != NULL)
2049 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2050 &dell_debugfs_fops);
2052 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2055 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2057 buffer = dell_smbios_get_buffer();
2058 buffer->input[0] = token->location;
2059 dell_smbios_send_request(0, 2);
2060 if (buffer->output[0] == 0)
2061 max_intensity = buffer->output[3];
2062 dell_smbios_release_buffer();
2065 if (max_intensity) {
2066 struct backlight_properties props;
2067 memset(&props, 0, sizeof(struct backlight_properties));
2068 props.type = BACKLIGHT_PLATFORM;
2069 props.max_brightness = max_intensity;
2070 dell_backlight_device = backlight_device_register("dell_backlight",
2071 &platform_device->dev,
2076 if (IS_ERR(dell_backlight_device)) {
2077 ret = PTR_ERR(dell_backlight_device);
2078 dell_backlight_device = NULL;
2079 goto fail_backlight;
2082 dell_backlight_device->props.brightness =
2083 dell_get_intensity(dell_backlight_device);
2084 backlight_update_status(dell_backlight_device);
2090 dell_cleanup_rfkill();
2092 platform_device_del(platform_device);
2093 fail_platform_device2:
2094 platform_device_put(platform_device);
2095 fail_platform_device1:
2096 platform_driver_unregister(&platform_driver);
2097 fail_platform_driver:
2101 static void __exit dell_exit(void)
2103 debugfs_remove_recursive(dell_laptop_dir);
2104 if (quirks && quirks->touchpad_led)
2105 touchpad_led_exit();
2107 backlight_device_unregister(dell_backlight_device);
2108 dell_cleanup_rfkill();
2109 if (platform_device) {
2110 platform_device_unregister(platform_device);
2111 platform_driver_unregister(&platform_driver);
2115 /* dell-rbtn.c driver export functions which will not work correctly (and could
2116 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2117 * not problem when dell-rbtn.c is compiled as external module. When both files
2118 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2119 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2120 * This can be achieved by late_initcall() instead module_init().
2122 late_initcall(dell_init);
2123 module_exit(dell_exit);
2125 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2126 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2127 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2128 MODULE_DESCRIPTION("Dell laptop driver");
2129 MODULE_LICENSE("GPL");