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/seq_file.h>
33 #include <acpi/video.h>
34 #include "dell-rbtn.h"
35 #include "dell-smbios.h"
37 #define BRIGHTNESS_TOKEN 0x7d
38 #define KBD_LED_OFF_TOKEN 0x01E1
39 #define KBD_LED_ON_TOKEN 0x01E2
40 #define KBD_LED_AUTO_TOKEN 0x01E3
41 #define KBD_LED_AUTO_25_TOKEN 0x02EA
42 #define KBD_LED_AUTO_50_TOKEN 0x02EB
43 #define KBD_LED_AUTO_75_TOKEN 0x02EC
44 #define KBD_LED_AUTO_100_TOKEN 0x02F6
49 int needs_kbd_timeouts;
51 * Ordered list of timeouts expressed in seconds.
52 * The list must end with -1
57 static struct quirk_entry *quirks;
59 static struct quirk_entry quirk_dell_vostro_v130 = {
63 static int __init dmi_matched(const struct dmi_system_id *dmi)
65 quirks = dmi->driver_data;
70 * These values come from Windows utility provided by Dell. If any other value
71 * is used then BIOS silently set timeout to 0 without any error message.
73 static struct quirk_entry quirk_dell_xps13_9333 = {
74 .needs_kbd_timeouts = 1,
75 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
78 static struct platform_driver platform_driver = {
80 .name = "dell-laptop",
84 static struct platform_device *platform_device;
85 static struct backlight_device *dell_backlight_device;
86 static struct rfkill *wifi_rfkill;
87 static struct rfkill *bluetooth_rfkill;
88 static struct rfkill *wwan_rfkill;
89 static bool force_rfkill;
91 module_param(force_rfkill, bool, 0444);
92 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
94 static const struct dmi_system_id dell_device_table[] __initconst = {
96 .ident = "Dell laptop",
98 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
99 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
104 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
105 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
111 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
115 .ident = "Dell Computer Corporation",
117 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
118 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
123 MODULE_DEVICE_TABLE(dmi, dell_device_table);
125 static const struct dmi_system_id dell_quirks[] __initconst = {
127 .callback = dmi_matched,
128 .ident = "Dell Vostro V130",
130 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
131 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
133 .driver_data = &quirk_dell_vostro_v130,
136 .callback = dmi_matched,
137 .ident = "Dell Vostro V131",
139 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
140 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
142 .driver_data = &quirk_dell_vostro_v130,
145 .callback = dmi_matched,
146 .ident = "Dell Vostro 3350",
148 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
149 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
151 .driver_data = &quirk_dell_vostro_v130,
154 .callback = dmi_matched,
155 .ident = "Dell Vostro 3555",
157 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
158 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
160 .driver_data = &quirk_dell_vostro_v130,
163 .callback = dmi_matched,
164 .ident = "Dell Inspiron N311z",
166 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
167 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
169 .driver_data = &quirk_dell_vostro_v130,
172 .callback = dmi_matched,
173 .ident = "Dell Inspiron M5110",
175 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
176 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
178 .driver_data = &quirk_dell_vostro_v130,
181 .callback = dmi_matched,
182 .ident = "Dell Vostro 3360",
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
187 .driver_data = &quirk_dell_vostro_v130,
190 .callback = dmi_matched,
191 .ident = "Dell Vostro 3460",
193 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
196 .driver_data = &quirk_dell_vostro_v130,
199 .callback = dmi_matched,
200 .ident = "Dell Vostro 3560",
202 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
203 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
205 .driver_data = &quirk_dell_vostro_v130,
208 .callback = dmi_matched,
209 .ident = "Dell Vostro 3450",
211 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
212 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
214 .driver_data = &quirk_dell_vostro_v130,
217 .callback = dmi_matched,
218 .ident = "Dell Inspiron 5420",
220 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
221 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
223 .driver_data = &quirk_dell_vostro_v130,
226 .callback = dmi_matched,
227 .ident = "Dell Inspiron 5520",
229 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
230 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
232 .driver_data = &quirk_dell_vostro_v130,
235 .callback = dmi_matched,
236 .ident = "Dell Inspiron 5720",
238 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
239 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
241 .driver_data = &quirk_dell_vostro_v130,
244 .callback = dmi_matched,
245 .ident = "Dell Inspiron 7420",
247 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
248 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
250 .driver_data = &quirk_dell_vostro_v130,
253 .callback = dmi_matched,
254 .ident = "Dell Inspiron 7520",
256 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
257 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
259 .driver_data = &quirk_dell_vostro_v130,
262 .callback = dmi_matched,
263 .ident = "Dell Inspiron 7720",
265 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
266 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
268 .driver_data = &quirk_dell_vostro_v130,
271 .callback = dmi_matched,
272 .ident = "Dell XPS13 9333",
274 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
275 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
277 .driver_data = &quirk_dell_xps13_9333,
283 * Derived from information in smbios-wireless-ctl:
285 * cbSelect 17, Value 11
287 * Return Wireless Info
288 * cbArg1, byte0 = 0x00
290 * cbRes1 Standard return codes (0, -1, -2)
291 * cbRes2 Info bit flags:
293 * 0 Hardware switch supported (1)
294 * 1 WiFi locator supported (1)
295 * 2 WLAN supported (1)
296 * 3 Bluetooth (BT) supported (1)
297 * 4 WWAN supported (1)
298 * 5 Wireless KBD supported (1)
299 * 6 Uw b supported (1)
300 * 7 WiGig supported (1)
301 * 8 WLAN installed (1)
303 * 10 WWAN installed (1)
304 * 11 Uw b installed (1)
305 * 12 WiGig installed (1)
307 * 16 Hardware (HW) switch is On (1)
308 * 17 WLAN disabled (1)
310 * 19 WWAN disabled (1)
311 * 20 Uw b disabled (1)
312 * 21 WiGig disabled (1)
315 * cbRes3 NVRAM size in bytes
316 * cbRes4, byte 0 NVRAM format version number
319 * Set QuickSet Radio Disable Flag
320 * cbArg1, byte0 = 0x01
329 * cbArg1, byte2 Flag bits:
330 * 0 QuickSet disables radio (1)
333 * cbRes1 Standard return codes (0, -1, -2)
334 * cbRes2 QuickSet (QS) radio disable bit map:
339 * 4 QS disables WIGIG
342 * Wireless Switch Configuration
343 * cbArg1, byte0 = 0x02
349 * 2 Set WiFi locator enable/disable
351 * Switch settings (if byte 1==1):
352 * 0 WLAN sw itch control (1)
353 * 1 BT sw itch control (1)
354 * 2 WWAN sw itch control (1)
355 * 3 UWB sw itch control (1)
356 * 4 WiGig sw itch control (1)
358 * cbArg1, byte2 Enable bits (if byte 1==2):
359 * 0 Enable WiFi locator (1)
361 * cbRes1 Standard return codes (0, -1, -2)
362 * cbRes2 QuickSet radio disable bit map:
363 * 0 WLAN controlled by sw itch (1)
364 * 1 BT controlled by sw itch (1)
365 * 2 WWAN controlled by sw itch (1)
366 * 3 UWB controlled by sw itch (1)
367 * 4 WiGig controlled by sw itch (1)
369 * 7 Wireless sw itch config locked (1)
370 * 8 WiFi locator enabled (1)
372 * 15 WiFi locator setting locked (1)
375 * Read Local Config Data (LCD)
376 * cbArg1, byte0 = 0x10
377 * cbArg1, byte1 NVRAM index low byte
378 * cbArg1, byte2 NVRAM index high byte
379 * cbRes1 Standard return codes (0, -1, -2)
380 * cbRes2 4 bytes read from LCD[index]
381 * cbRes3 4 bytes read from LCD[index+4]
382 * cbRes4 4 bytes read from LCD[index+8]
384 * Write Local Config Data (LCD)
385 * cbArg1, byte0 = 0x11
386 * cbArg1, byte1 NVRAM index low byte
387 * cbArg1, byte2 NVRAM index high byte
388 * cbArg2 4 bytes to w rite at LCD[index]
389 * cbArg3 4 bytes to w rite at LCD[index+4]
390 * cbArg4 4 bytes to w rite at LCD[index+8]
391 * cbRes1 Standard return codes (0, -1, -2)
393 * Populate Local Config Data from NVRAM
394 * cbArg1, byte0 = 0x12
395 * cbRes1 Standard return codes (0, -1, -2)
397 * Commit Local Config Data to NVRAM
398 * cbArg1, byte0 = 0x13
399 * cbRes1 Standard return codes (0, -1, -2)
402 static int dell_rfkill_set(void *data, bool blocked)
404 struct calling_interface_buffer *buffer;
405 int disable = blocked ? 1 : 0;
406 unsigned long radio = (unsigned long)data;
407 int hwswitch_bit = (unsigned long)data - 1;
412 buffer = dell_smbios_get_buffer();
414 dell_smbios_send_request(17, 11);
415 ret = buffer->output[0];
416 status = buffer->output[1];
421 dell_smbios_clear_buffer();
423 buffer->input[0] = 0x2;
424 dell_smbios_send_request(17, 11);
425 ret = buffer->output[0];
426 hwswitch = buffer->output[1];
428 /* If the hardware switch controls this radio, and the hardware
429 switch is disabled, always disable the radio */
430 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
431 (status & BIT(0)) && !(status & BIT(16)))
434 dell_smbios_clear_buffer();
436 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
437 dell_smbios_send_request(17, 11);
438 ret = buffer->output[0];
441 dell_smbios_release_buffer();
442 return dell_smbios_error(ret);
445 /* Must be called with the buffer held */
446 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
448 struct calling_interface_buffer *buffer)
450 if (status & BIT(0)) {
451 /* Has hw-switch, sync sw_state to BIOS */
452 int block = rfkill_blocked(rfkill);
453 dell_smbios_clear_buffer();
454 buffer->input[0] = (1 | (radio << 8) | (block << 16));
455 dell_smbios_send_request(17, 11);
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 struct calling_interface_buffer *buffer;
472 int radio = ((unsigned long)data & 0xF);
477 buffer = dell_smbios_get_buffer();
479 dell_smbios_send_request(17, 11);
480 ret = buffer->output[0];
481 status = buffer->output[1];
483 if (ret != 0 || !(status & BIT(0))) {
484 dell_smbios_release_buffer();
488 dell_smbios_clear_buffer();
490 buffer->input[0] = 0x2;
491 dell_smbios_send_request(17, 11);
492 ret = buffer->output[0];
493 hwswitch = buffer->output[1];
495 dell_smbios_release_buffer();
500 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
503 static const struct rfkill_ops dell_rfkill_ops = {
504 .set_block = dell_rfkill_set,
505 .query = dell_rfkill_query,
508 static struct dentry *dell_laptop_dir;
510 static int dell_debugfs_show(struct seq_file *s, void *data)
512 struct calling_interface_buffer *buffer;
518 buffer = dell_smbios_get_buffer();
520 dell_smbios_send_request(17, 11);
521 ret = buffer->output[0];
522 status = buffer->output[1];
524 dell_smbios_clear_buffer();
526 buffer->input[0] = 0x2;
527 dell_smbios_send_request(17, 11);
528 hwswitch_ret = buffer->output[0];
529 hwswitch_state = buffer->output[1];
531 dell_smbios_release_buffer();
533 seq_printf(s, "return:\t%d\n", ret);
534 seq_printf(s, "status:\t0x%X\n", status);
535 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
537 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
538 (status & BIT(1)) >> 1);
539 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
540 (status & BIT(2)) >> 2);
541 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
542 (status & BIT(3)) >> 3);
543 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
544 (status & BIT(4)) >> 4);
545 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
546 (status & BIT(5)) >> 5);
547 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
548 (status & BIT(6)) >> 6);
549 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
550 (status & BIT(7)) >> 7);
551 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
552 (status & BIT(8)) >> 8);
553 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
554 (status & BIT(9)) >> 9);
555 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
556 (status & BIT(10)) >> 10);
557 seq_printf(s, "Bit 11: UWB installed: %lu\n",
558 (status & BIT(11)) >> 11);
559 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
560 (status & BIT(12)) >> 12);
562 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
563 (status & BIT(16)) >> 16);
564 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
565 (status & BIT(17)) >> 17);
566 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
567 (status & BIT(18)) >> 18);
568 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
569 (status & BIT(19)) >> 19);
570 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
571 (status & BIT(20)) >> 20);
572 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
573 (status & BIT(21)) >> 21);
575 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
576 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
577 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
578 hwswitch_state & BIT(0));
579 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
580 (hwswitch_state & BIT(1)) >> 1);
581 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
582 (hwswitch_state & BIT(2)) >> 2);
583 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
584 (hwswitch_state & BIT(3)) >> 3);
585 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
586 (hwswitch_state & BIT(4)) >> 4);
587 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
588 (hwswitch_state & BIT(7)) >> 7);
589 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
590 (hwswitch_state & BIT(8)) >> 8);
591 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
592 (hwswitch_state & BIT(15)) >> 15);
597 static int dell_debugfs_open(struct inode *inode, struct file *file)
599 return single_open(file, dell_debugfs_show, inode->i_private);
602 static const struct file_operations dell_debugfs_fops = {
603 .owner = THIS_MODULE,
604 .open = dell_debugfs_open,
607 .release = single_release,
610 static void dell_update_rfkill(struct work_struct *ignored)
612 struct calling_interface_buffer *buffer;
617 buffer = dell_smbios_get_buffer();
619 dell_smbios_send_request(17, 11);
620 ret = buffer->output[0];
621 status = buffer->output[1];
626 dell_smbios_clear_buffer();
628 buffer->input[0] = 0x2;
629 dell_smbios_send_request(17, 11);
630 ret = buffer->output[0];
632 if (ret == 0 && (status & BIT(0)))
633 hwswitch = buffer->output[1];
636 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
637 dell_rfkill_update_sw_state(wifi_rfkill, 1, status, buffer);
639 if (bluetooth_rfkill) {
640 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
642 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status,
646 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
647 dell_rfkill_update_sw_state(wwan_rfkill, 3, status, buffer);
651 dell_smbios_release_buffer();
653 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
655 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
658 static bool extended;
660 if (str & I8042_STR_AUXDATA)
663 if (unlikely(data == 0xe0)) {
666 } else if (unlikely(extended)) {
669 schedule_delayed_work(&dell_rfkill_work,
670 round_jiffies_relative(HZ / 4));
679 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
680 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
682 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
683 unsigned long action, void *data)
685 schedule_delayed_work(&dell_rfkill_work, 0);
689 static struct notifier_block dell_laptop_rbtn_notifier = {
690 .notifier_call = dell_laptop_rbtn_notifier_call,
693 static int __init dell_setup_rfkill(void)
695 struct calling_interface_buffer *buffer;
696 int status, ret, whitelisted;
700 * rfkill support causes trouble on various models, mostly Inspirons.
701 * So we whitelist certain series, and don't support rfkill on others.
704 product = dmi_get_system_info(DMI_PRODUCT_NAME);
705 if (product && (strncmp(product, "Latitude", 8) == 0 ||
706 strncmp(product, "Precision", 9) == 0))
708 if (!force_rfkill && !whitelisted)
711 buffer = dell_smbios_get_buffer();
712 dell_smbios_send_request(17, 11);
713 ret = buffer->output[0];
714 status = buffer->output[1];
715 dell_smbios_release_buffer();
717 /* dell wireless info smbios call is not supported */
721 /* rfkill is only tested on laptops with a hwswitch */
722 if (!(status & BIT(0)) && !force_rfkill)
725 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
726 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
728 &dell_rfkill_ops, (void *) 1);
733 ret = rfkill_register(wifi_rfkill);
738 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
739 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
740 &platform_device->dev,
741 RFKILL_TYPE_BLUETOOTH,
742 &dell_rfkill_ops, (void *) 2);
743 if (!bluetooth_rfkill) {
747 ret = rfkill_register(bluetooth_rfkill);
752 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
753 wwan_rfkill = rfkill_alloc("dell-wwan",
754 &platform_device->dev,
756 &dell_rfkill_ops, (void *) 3);
761 ret = rfkill_register(wwan_rfkill);
767 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
768 * which can receive events from HW slider switch.
770 * Dell SMBIOS on whitelisted models supports controlling radio devices
771 * but does not support receiving HW button switch events. We can use
772 * i8042 filter hook function to receive keyboard data and handle
773 * keycode for HW button.
775 * So if it is possible we will use Dell Airplane Mode Switch ACPI
776 * driver for receiving HW events and Dell SMBIOS for setting rfkill
777 * states. If ACPI driver or device is not available we will fallback to
778 * i8042 filter hook function.
780 * To prevent duplicate rfkill devices which control and do same thing,
781 * dell-rbtn driver will automatically remove its own rfkill devices
782 * once function dell_rbtn_notifier_register() is called.
785 dell_rbtn_notifier_register_func =
786 symbol_request(dell_rbtn_notifier_register);
787 if (dell_rbtn_notifier_register_func) {
788 dell_rbtn_notifier_unregister_func =
789 symbol_request(dell_rbtn_notifier_unregister);
790 if (!dell_rbtn_notifier_unregister_func) {
791 symbol_put(dell_rbtn_notifier_register);
792 dell_rbtn_notifier_register_func = NULL;
796 if (dell_rbtn_notifier_register_func) {
797 ret = dell_rbtn_notifier_register_func(
798 &dell_laptop_rbtn_notifier);
799 symbol_put(dell_rbtn_notifier_register);
800 dell_rbtn_notifier_register_func = NULL;
802 symbol_put(dell_rbtn_notifier_unregister);
803 dell_rbtn_notifier_unregister_func = NULL;
806 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
811 pr_info("Using dell-rbtn acpi driver for receiving events\n");
812 } else if (ret != -ENODEV) {
813 pr_warn("Unable to register dell rbtn notifier\n");
816 ret = i8042_install_filter(dell_laptop_i8042_filter);
818 pr_warn("Unable to install key filter\n");
821 pr_info("Using i8042 filter function for receiving events\n");
827 rfkill_unregister(wwan_rfkill);
829 rfkill_destroy(wwan_rfkill);
830 if (bluetooth_rfkill)
831 rfkill_unregister(bluetooth_rfkill);
833 rfkill_destroy(bluetooth_rfkill);
835 rfkill_unregister(wifi_rfkill);
837 rfkill_destroy(wifi_rfkill);
842 static void dell_cleanup_rfkill(void)
844 if (dell_rbtn_notifier_unregister_func) {
845 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
846 symbol_put(dell_rbtn_notifier_unregister);
847 dell_rbtn_notifier_unregister_func = NULL;
849 i8042_remove_filter(dell_laptop_i8042_filter);
851 cancel_delayed_work_sync(&dell_rfkill_work);
853 rfkill_unregister(wifi_rfkill);
854 rfkill_destroy(wifi_rfkill);
856 if (bluetooth_rfkill) {
857 rfkill_unregister(bluetooth_rfkill);
858 rfkill_destroy(bluetooth_rfkill);
861 rfkill_unregister(wwan_rfkill);
862 rfkill_destroy(wwan_rfkill);
866 static int dell_send_intensity(struct backlight_device *bd)
868 struct calling_interface_buffer *buffer;
869 struct calling_interface_token *token;
872 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
876 buffer = dell_smbios_get_buffer();
877 buffer->input[0] = token->location;
878 buffer->input[1] = bd->props.brightness;
880 if (power_supply_is_system_supplied() > 0)
881 dell_smbios_send_request(1, 2);
883 dell_smbios_send_request(1, 1);
885 ret = dell_smbios_error(buffer->output[0]);
887 dell_smbios_release_buffer();
891 static int dell_get_intensity(struct backlight_device *bd)
893 struct calling_interface_buffer *buffer;
894 struct calling_interface_token *token;
897 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
901 buffer = dell_smbios_get_buffer();
902 buffer->input[0] = token->location;
904 if (power_supply_is_system_supplied() > 0)
905 dell_smbios_send_request(0, 2);
907 dell_smbios_send_request(0, 1);
909 if (buffer->output[0])
910 ret = dell_smbios_error(buffer->output[0]);
912 ret = buffer->output[1];
914 dell_smbios_release_buffer();
918 static const struct backlight_ops dell_ops = {
919 .get_brightness = dell_get_intensity,
920 .update_status = dell_send_intensity,
923 static void touchpad_led_on(void)
927 i8042_command(&data, command | 1 << 12);
930 static void touchpad_led_off(void)
934 i8042_command(&data, command | 1 << 12);
937 static void touchpad_led_set(struct led_classdev *led_cdev,
938 enum led_brightness value)
946 static struct led_classdev touchpad_led = {
947 .name = "dell-laptop::touchpad",
948 .brightness_set = touchpad_led_set,
949 .flags = LED_CORE_SUSPENDRESUME,
952 static int __init touchpad_led_init(struct device *dev)
954 return led_classdev_register(dev, &touchpad_led);
957 static void touchpad_led_exit(void)
959 led_classdev_unregister(&touchpad_led);
963 * Derived from information in smbios-keyboard-ctl:
967 * Keyboard illumination
968 * cbArg1 determines the function to be performed
970 * cbArg1 0x0 = Get Feature Information
971 * cbRES1 Standard return codes (0, -1, -2)
972 * cbRES2, word0 Bitmap of user-selectable modes
973 * bit 0 Always off (All systems)
974 * bit 1 Always on (Travis ATG, Siberia)
975 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
976 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
977 * bit 4 Auto: Input-activity-based On; input-activity based Off
978 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
979 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
980 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
981 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
982 * bits 9-15 Reserved for future use
983 * cbRES2, byte2 Reserved for future use
984 * cbRES2, byte3 Keyboard illumination type
988 * 3-255 Reserved for future use
989 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
990 * bit 0 Any keystroke
991 * bit 1 Touchpad activity
992 * bit 2 Pointing stick
994 * bits 4-7 Reserved for future use
995 * cbRES3, byte1 Supported timeout unit bitmap
1000 * bits 4-7 Reserved for future use
1001 * cbRES3, byte2 Number of keyboard light brightness levels
1002 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1003 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1004 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1005 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1007 * cbArg1 0x1 = Get Current State
1008 * cbRES1 Standard return codes (0, -1, -2)
1009 * cbRES2, word0 Bitmap of current mode state
1010 * bit 0 Always off (All systems)
1011 * bit 1 Always on (Travis ATG, Siberia)
1012 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1013 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1014 * bit 4 Auto: Input-activity-based On; input-activity based Off
1015 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1016 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1017 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1018 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1019 * bits 9-15 Reserved for future use
1020 * Note: Only One bit can be set
1021 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1022 * bit 0 Any keystroke
1023 * bit 1 Touchpad activity
1024 * bit 2 Pointing stick
1026 * bits 4-7 Reserved for future use
1027 * cbRES2, byte3 Current Timeout
1028 * bits 7:6 Timeout units indicator:
1033 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1034 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1035 * are set upon return from the [Get feature information] call.
1036 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1037 * cbRES3, byte1 Current ALS reading
1038 * cbRES3, byte2 Current keyboard light level.
1040 * cbArg1 0x2 = Set New State
1041 * cbRES1 Standard return codes (0, -1, -2)
1042 * cbArg2, word0 Bitmap of current mode state
1043 * bit 0 Always off (All systems)
1044 * bit 1 Always on (Travis ATG, Siberia)
1045 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1046 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1047 * bit 4 Auto: Input-activity-based On; input-activity based Off
1048 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1049 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1050 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1051 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1052 * bits 9-15 Reserved for future use
1053 * Note: Only One bit can be set
1054 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1055 * keyboard to turn off automatically.
1056 * bit 0 Any keystroke
1057 * bit 1 Touchpad activity
1058 * bit 2 Pointing stick
1060 * bits 4-7 Reserved for future use
1061 * cbArg2, byte3 Desired Timeout
1062 * bits 7:6 Timeout units indicator:
1067 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1068 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1069 * cbArg3, byte2 Desired keyboard light level.
1073 enum kbd_timeout_unit {
1074 KBD_TIMEOUT_SECONDS = 0,
1075 KBD_TIMEOUT_MINUTES,
1081 KBD_MODE_BIT_OFF = 0,
1084 KBD_MODE_BIT_TRIGGER_ALS,
1085 KBD_MODE_BIT_TRIGGER,
1086 KBD_MODE_BIT_TRIGGER_25,
1087 KBD_MODE_BIT_TRIGGER_50,
1088 KBD_MODE_BIT_TRIGGER_75,
1089 KBD_MODE_BIT_TRIGGER_100,
1092 #define kbd_is_als_mode_bit(bit) \
1093 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1094 #define kbd_is_trigger_mode_bit(bit) \
1095 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1096 #define kbd_is_level_mode_bit(bit) \
1097 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1120 static const int kbd_tokens[] = {
1122 KBD_LED_AUTO_25_TOKEN,
1123 KBD_LED_AUTO_50_TOKEN,
1124 KBD_LED_AUTO_75_TOKEN,
1125 KBD_LED_AUTO_100_TOKEN,
1129 static u16 kbd_token_bits;
1131 static struct kbd_info kbd_info;
1132 static bool kbd_als_supported;
1133 static bool kbd_triggers_supported;
1135 static u8 kbd_mode_levels[16];
1136 static int kbd_mode_levels_count;
1138 static u8 kbd_previous_level;
1139 static u8 kbd_previous_mode_bit;
1141 static bool kbd_led_present;
1144 * NOTE: there are three ways to set the keyboard backlight level.
1145 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1146 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1147 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1149 * There are laptops which support only one of these methods. If we want to
1150 * support as many machines as possible we need to implement all three methods.
1151 * The first two methods use the kbd_state structure. The third uses SMBIOS
1152 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1153 * keyboard backlight level via kbd_state.level.
1156 static int kbd_get_info(struct kbd_info *info)
1158 struct calling_interface_buffer *buffer;
1162 buffer = dell_smbios_get_buffer();
1164 buffer->input[0] = 0x0;
1165 dell_smbios_send_request(4, 11);
1166 ret = buffer->output[0];
1169 ret = dell_smbios_error(ret);
1173 info->modes = buffer->output[1] & 0xFFFF;
1174 info->type = (buffer->output[1] >> 24) & 0xFF;
1175 info->triggers = buffer->output[2] & 0xFF;
1176 units = (buffer->output[2] >> 8) & 0xFF;
1177 info->levels = (buffer->output[2] >> 16) & 0xFF;
1180 info->seconds = (buffer->output[3] >> 0) & 0xFF;
1182 info->minutes = (buffer->output[3] >> 8) & 0xFF;
1184 info->hours = (buffer->output[3] >> 16) & 0xFF;
1186 info->days = (buffer->output[3] >> 24) & 0xFF;
1189 dell_smbios_release_buffer();
1193 static unsigned int kbd_get_max_level(void)
1195 if (kbd_info.levels != 0)
1196 return kbd_info.levels;
1197 if (kbd_mode_levels_count > 0)
1198 return kbd_mode_levels_count - 1;
1202 static int kbd_get_level(struct kbd_state *state)
1206 if (kbd_info.levels != 0)
1207 return state->level;
1209 if (kbd_mode_levels_count > 0) {
1210 for (i = 0; i < kbd_mode_levels_count; ++i)
1211 if (kbd_mode_levels[i] == state->mode_bit)
1219 static int kbd_set_level(struct kbd_state *state, u8 level)
1221 if (kbd_info.levels != 0) {
1223 kbd_previous_level = level;
1224 if (state->level == level)
1226 state->level = level;
1227 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1228 state->mode_bit = kbd_previous_mode_bit;
1229 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1230 kbd_previous_mode_bit = state->mode_bit;
1231 state->mode_bit = KBD_MODE_BIT_OFF;
1236 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1238 kbd_previous_level = level;
1239 state->mode_bit = kbd_mode_levels[level];
1246 static int kbd_get_state(struct kbd_state *state)
1248 struct calling_interface_buffer *buffer;
1251 buffer = dell_smbios_get_buffer();
1253 buffer->input[0] = 0x1;
1254 dell_smbios_send_request(4, 11);
1255 ret = buffer->output[0];
1258 ret = dell_smbios_error(ret);
1262 state->mode_bit = ffs(buffer->output[1] & 0xFFFF);
1263 if (state->mode_bit != 0)
1266 state->triggers = (buffer->output[1] >> 16) & 0xFF;
1267 state->timeout_value = (buffer->output[1] >> 24) & 0x3F;
1268 state->timeout_unit = (buffer->output[1] >> 30) & 0x3;
1269 state->als_setting = buffer->output[2] & 0xFF;
1270 state->als_value = (buffer->output[2] >> 8) & 0xFF;
1271 state->level = (buffer->output[2] >> 16) & 0xFF;
1274 dell_smbios_release_buffer();
1278 static int kbd_set_state(struct kbd_state *state)
1280 struct calling_interface_buffer *buffer;
1283 buffer = dell_smbios_get_buffer();
1284 buffer->input[0] = 0x2;
1285 buffer->input[1] = BIT(state->mode_bit) & 0xFFFF;
1286 buffer->input[1] |= (state->triggers & 0xFF) << 16;
1287 buffer->input[1] |= (state->timeout_value & 0x3F) << 24;
1288 buffer->input[1] |= (state->timeout_unit & 0x3) << 30;
1289 buffer->input[2] = state->als_setting & 0xFF;
1290 buffer->input[2] |= (state->level & 0xFF) << 16;
1291 dell_smbios_send_request(4, 11);
1292 ret = buffer->output[0];
1293 dell_smbios_release_buffer();
1295 return dell_smbios_error(ret);
1298 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1302 ret = kbd_set_state(state);
1307 * When setting the new state fails,try to restore the previous one.
1308 * This is needed on some machines where BIOS sets a default state when
1309 * setting a new state fails. This default state could be all off.
1312 if (kbd_set_state(old))
1313 pr_err("Setting old previous keyboard state failed\n");
1318 static int kbd_set_token_bit(u8 bit)
1320 struct calling_interface_buffer *buffer;
1321 struct calling_interface_token *token;
1324 if (bit >= ARRAY_SIZE(kbd_tokens))
1327 token = dell_smbios_find_token(kbd_tokens[bit]);
1331 buffer = dell_smbios_get_buffer();
1332 buffer->input[0] = token->location;
1333 buffer->input[1] = token->value;
1334 dell_smbios_send_request(1, 0);
1335 ret = buffer->output[0];
1336 dell_smbios_release_buffer();
1338 return dell_smbios_error(ret);
1341 static int kbd_get_token_bit(u8 bit)
1343 struct calling_interface_buffer *buffer;
1344 struct calling_interface_token *token;
1348 if (bit >= ARRAY_SIZE(kbd_tokens))
1351 token = dell_smbios_find_token(kbd_tokens[bit]);
1355 buffer = dell_smbios_get_buffer();
1356 buffer->input[0] = token->location;
1357 dell_smbios_send_request(0, 0);
1358 ret = buffer->output[0];
1359 val = buffer->output[1];
1360 dell_smbios_release_buffer();
1363 return dell_smbios_error(ret);
1365 return (val == token->value);
1368 static int kbd_get_first_active_token_bit(void)
1373 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1374 ret = kbd_get_token_bit(i);
1382 static int kbd_get_valid_token_counts(void)
1384 return hweight16(kbd_token_bits);
1387 static inline int kbd_init_info(void)
1389 struct kbd_state state;
1393 ret = kbd_get_info(&kbd_info);
1397 kbd_get_state(&state);
1399 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1400 if (kbd_info.seconds > 63)
1401 kbd_info.seconds = 63;
1402 if (kbd_info.minutes > 63)
1403 kbd_info.minutes = 63;
1404 if (kbd_info.hours > 63)
1405 kbd_info.hours = 63;
1406 if (kbd_info.days > 63)
1409 /* NOTE: On tested machines ON mode did not work and caused
1410 * problems (turned backlight off) so do not use it
1412 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1414 kbd_previous_level = kbd_get_level(&state);
1415 kbd_previous_mode_bit = state.mode_bit;
1417 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1418 kbd_previous_level = 1;
1420 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1421 kbd_previous_mode_bit =
1422 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1423 if (kbd_previous_mode_bit != 0)
1424 kbd_previous_mode_bit--;
1427 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1428 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1429 kbd_als_supported = true;
1431 if (kbd_info.modes & (
1432 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1433 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1434 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1436 kbd_triggers_supported = true;
1438 /* kbd_mode_levels[0] is reserved, see below */
1439 for (i = 0; i < 16; ++i)
1440 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1441 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1444 * Find the first supported mode and assign to kbd_mode_levels[0].
1445 * This should be 0 (off), but we cannot depend on the BIOS to
1448 if (kbd_mode_levels_count > 0) {
1449 for (i = 0; i < 16; ++i) {
1450 if (BIT(i) & kbd_info.modes) {
1451 kbd_mode_levels[0] = i;
1455 kbd_mode_levels_count++;
1462 static inline void kbd_init_tokens(void)
1466 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1467 if (dell_smbios_find_token(kbd_tokens[i]))
1468 kbd_token_bits |= BIT(i);
1471 static void kbd_init(void)
1475 ret = kbd_init_info();
1478 if (kbd_token_bits != 0 || ret == 0)
1479 kbd_led_present = true;
1482 static ssize_t kbd_led_timeout_store(struct device *dev,
1483 struct device_attribute *attr,
1484 const char *buf, size_t count)
1486 struct kbd_state new_state;
1487 struct kbd_state state;
1495 ret = sscanf(buf, "%d %c", &value, &ch);
1508 if (value > kbd_info.seconds)
1510 unit = KBD_TIMEOUT_SECONDS;
1513 if (value > kbd_info.minutes)
1515 unit = KBD_TIMEOUT_MINUTES;
1518 if (value > kbd_info.hours)
1520 unit = KBD_TIMEOUT_HOURS;
1523 if (value > kbd_info.days)
1525 unit = KBD_TIMEOUT_DAYS;
1531 if (quirks && quirks->needs_kbd_timeouts)
1535 /* Convert value from current units to seconds */
1537 case KBD_TIMEOUT_DAYS:
1539 case KBD_TIMEOUT_HOURS:
1541 case KBD_TIMEOUT_MINUTES:
1543 unit = KBD_TIMEOUT_SECONDS;
1546 if (quirks && quirks->needs_kbd_timeouts) {
1547 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1548 if (value <= quirks->kbd_timeouts[i]) {
1549 value = quirks->kbd_timeouts[i];
1555 if (value <= kbd_info.seconds && kbd_info.seconds) {
1556 unit = KBD_TIMEOUT_SECONDS;
1557 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1559 unit = KBD_TIMEOUT_MINUTES;
1560 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1562 unit = KBD_TIMEOUT_HOURS;
1563 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1564 value /= (60 * 60 * 24);
1565 unit = KBD_TIMEOUT_DAYS;
1571 ret = kbd_get_state(&state);
1576 new_state.timeout_value = value;
1577 new_state.timeout_unit = unit;
1579 ret = kbd_set_state_safe(&new_state, &state);
1586 static ssize_t kbd_led_timeout_show(struct device *dev,
1587 struct device_attribute *attr, char *buf)
1589 struct kbd_state state;
1593 ret = kbd_get_state(&state);
1597 len = sprintf(buf, "%d", state.timeout_value);
1599 switch (state.timeout_unit) {
1600 case KBD_TIMEOUT_SECONDS:
1601 return len + sprintf(buf+len, "s\n");
1602 case KBD_TIMEOUT_MINUTES:
1603 return len + sprintf(buf+len, "m\n");
1604 case KBD_TIMEOUT_HOURS:
1605 return len + sprintf(buf+len, "h\n");
1606 case KBD_TIMEOUT_DAYS:
1607 return len + sprintf(buf+len, "d\n");
1615 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1616 kbd_led_timeout_show, kbd_led_timeout_store);
1618 static const char * const kbd_led_triggers[] = {
1621 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1625 static ssize_t kbd_led_triggers_store(struct device *dev,
1626 struct device_attribute *attr,
1627 const char *buf, size_t count)
1629 struct kbd_state new_state;
1630 struct kbd_state state;
1631 bool triggers_enabled = false;
1632 int trigger_bit = -1;
1636 ret = sscanf(buf, "%20s", trigger);
1640 if (trigger[0] != '+' && trigger[0] != '-')
1643 ret = kbd_get_state(&state);
1647 if (kbd_triggers_supported)
1648 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1650 if (kbd_triggers_supported) {
1651 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1652 if (!(kbd_info.triggers & BIT(i)))
1654 if (!kbd_led_triggers[i])
1656 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1658 if (trigger[0] == '+' &&
1659 triggers_enabled && (state.triggers & BIT(i)))
1661 if (trigger[0] == '-' &&
1662 (!triggers_enabled || !(state.triggers & BIT(i))))
1669 if (trigger_bit != -1) {
1671 if (trigger[0] == '+')
1672 new_state.triggers |= BIT(trigger_bit);
1674 new_state.triggers &= ~BIT(trigger_bit);
1675 /* NOTE: trackstick bit (2) must be disabled when
1676 * disabling touchpad bit (1), otherwise touchpad
1677 * bit (1) will not be disabled */
1678 if (trigger_bit == 1)
1679 new_state.triggers &= ~BIT(2);
1681 if ((kbd_info.triggers & new_state.triggers) !=
1684 if (new_state.triggers && !triggers_enabled) {
1685 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1686 kbd_set_level(&new_state, kbd_previous_level);
1687 } else if (new_state.triggers == 0) {
1688 kbd_set_level(&new_state, 0);
1690 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1692 ret = kbd_set_state_safe(&new_state, &state);
1695 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1696 kbd_previous_mode_bit = new_state.mode_bit;
1703 static ssize_t kbd_led_triggers_show(struct device *dev,
1704 struct device_attribute *attr, char *buf)
1706 struct kbd_state state;
1707 bool triggers_enabled;
1711 ret = kbd_get_state(&state);
1717 if (kbd_triggers_supported) {
1718 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1719 level = kbd_get_level(&state);
1720 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1721 if (!(kbd_info.triggers & BIT(i)))
1723 if (!kbd_led_triggers[i])
1725 if ((triggers_enabled || level <= 0) &&
1726 (state.triggers & BIT(i)))
1730 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1735 buf[len - 1] = '\n';
1740 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1741 kbd_led_triggers_show, kbd_led_triggers_store);
1743 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1744 struct device_attribute *attr,
1745 const char *buf, size_t count)
1747 struct kbd_state new_state;
1748 struct kbd_state state;
1749 bool triggers_enabled = false;
1753 ret = kstrtoint(buf, 0, &enable);
1757 ret = kbd_get_state(&state);
1761 if (enable == kbd_is_als_mode_bit(state.mode_bit))
1766 if (kbd_triggers_supported)
1767 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1770 if (triggers_enabled)
1771 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1773 new_state.mode_bit = KBD_MODE_BIT_ALS;
1775 if (triggers_enabled) {
1776 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1777 kbd_set_level(&new_state, kbd_previous_level);
1779 new_state.mode_bit = KBD_MODE_BIT_ON;
1782 if (!(kbd_info.modes & BIT(new_state.mode_bit)))
1785 ret = kbd_set_state_safe(&new_state, &state);
1788 kbd_previous_mode_bit = new_state.mode_bit;
1793 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1794 struct device_attribute *attr,
1797 struct kbd_state state;
1798 bool enabled = false;
1801 ret = kbd_get_state(&state);
1804 enabled = kbd_is_als_mode_bit(state.mode_bit);
1806 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1809 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1810 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1812 static ssize_t kbd_led_als_setting_store(struct device *dev,
1813 struct device_attribute *attr,
1814 const char *buf, size_t count)
1816 struct kbd_state state;
1817 struct kbd_state new_state;
1821 ret = kstrtou8(buf, 10, &setting);
1825 ret = kbd_get_state(&state);
1830 new_state.als_setting = setting;
1832 ret = kbd_set_state_safe(&new_state, &state);
1839 static ssize_t kbd_led_als_setting_show(struct device *dev,
1840 struct device_attribute *attr,
1843 struct kbd_state state;
1846 ret = kbd_get_state(&state);
1850 return sprintf(buf, "%d\n", state.als_setting);
1853 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1854 kbd_led_als_setting_show, kbd_led_als_setting_store);
1856 static struct attribute *kbd_led_attrs[] = {
1857 &dev_attr_stop_timeout.attr,
1858 &dev_attr_start_triggers.attr,
1862 static const struct attribute_group kbd_led_group = {
1863 .attrs = kbd_led_attrs,
1866 static struct attribute *kbd_led_als_attrs[] = {
1867 &dev_attr_als_enabled.attr,
1868 &dev_attr_als_setting.attr,
1872 static const struct attribute_group kbd_led_als_group = {
1873 .attrs = kbd_led_als_attrs,
1876 static const struct attribute_group *kbd_led_groups[] = {
1882 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1886 struct kbd_state state;
1888 if (kbd_get_max_level()) {
1889 ret = kbd_get_state(&state);
1892 ret = kbd_get_level(&state);
1898 if (kbd_get_valid_token_counts()) {
1899 ret = kbd_get_first_active_token_bit();
1902 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1903 num &= num - 1; /* clear the first bit set */
1906 return ffs(num) - 1;
1909 pr_warn("Keyboard brightness level control not supported\n");
1913 static int kbd_led_level_set(struct led_classdev *led_cdev,
1914 enum led_brightness value)
1916 struct kbd_state state;
1917 struct kbd_state new_state;
1921 if (kbd_get_max_level()) {
1922 ret = kbd_get_state(&state);
1926 ret = kbd_set_level(&new_state, value);
1929 return kbd_set_state_safe(&new_state, &state);
1932 if (kbd_get_valid_token_counts()) {
1933 for (num = kbd_token_bits; num != 0 && value > 0; --value)
1934 num &= num - 1; /* clear the first bit set */
1937 return kbd_set_token_bit(ffs(num) - 1);
1940 pr_warn("Keyboard brightness level control not supported\n");
1944 static struct led_classdev kbd_led = {
1945 .name = "dell::kbd_backlight",
1946 .brightness_set_blocking = kbd_led_level_set,
1947 .brightness_get = kbd_led_level_get,
1948 .groups = kbd_led_groups,
1951 static int __init kbd_led_init(struct device *dev)
1954 if (!kbd_led_present)
1956 if (!kbd_als_supported)
1957 kbd_led_groups[1] = NULL;
1958 kbd_led.max_brightness = kbd_get_max_level();
1959 if (!kbd_led.max_brightness) {
1960 kbd_led.max_brightness = kbd_get_valid_token_counts();
1961 if (kbd_led.max_brightness)
1962 kbd_led.max_brightness--;
1964 return led_classdev_register(dev, &kbd_led);
1967 static void brightness_set_exit(struct led_classdev *led_cdev,
1968 enum led_brightness value)
1970 /* Don't change backlight level on exit */
1973 static void kbd_led_exit(void)
1975 if (!kbd_led_present)
1977 kbd_led.brightness_set = brightness_set_exit;
1978 led_classdev_unregister(&kbd_led);
1981 static int __init dell_init(void)
1983 struct calling_interface_buffer *buffer;
1984 struct calling_interface_token *token;
1985 int max_intensity = 0;
1988 if (!dmi_check_system(dell_device_table))
1992 /* find if this machine support other functions */
1993 dmi_check_system(dell_quirks);
1995 ret = platform_driver_register(&platform_driver);
1997 goto fail_platform_driver;
1998 platform_device = platform_device_alloc("dell-laptop", -1);
1999 if (!platform_device) {
2001 goto fail_platform_device1;
2003 ret = platform_device_add(platform_device);
2005 goto fail_platform_device2;
2007 ret = dell_setup_rfkill();
2010 pr_warn("Unable to setup rfkill\n");
2014 if (quirks && quirks->touchpad_led)
2015 touchpad_led_init(&platform_device->dev);
2017 kbd_led_init(&platform_device->dev);
2019 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2020 if (dell_laptop_dir != NULL)
2021 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2022 &dell_debugfs_fops);
2024 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2027 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2029 buffer = dell_smbios_get_buffer();
2030 buffer->input[0] = token->location;
2031 dell_smbios_send_request(0, 2);
2032 if (buffer->output[0] == 0)
2033 max_intensity = buffer->output[3];
2034 dell_smbios_release_buffer();
2037 if (max_intensity) {
2038 struct backlight_properties props;
2039 memset(&props, 0, sizeof(struct backlight_properties));
2040 props.type = BACKLIGHT_PLATFORM;
2041 props.max_brightness = max_intensity;
2042 dell_backlight_device = backlight_device_register("dell_backlight",
2043 &platform_device->dev,
2048 if (IS_ERR(dell_backlight_device)) {
2049 ret = PTR_ERR(dell_backlight_device);
2050 dell_backlight_device = NULL;
2051 goto fail_backlight;
2054 dell_backlight_device->props.brightness =
2055 dell_get_intensity(dell_backlight_device);
2056 backlight_update_status(dell_backlight_device);
2062 dell_cleanup_rfkill();
2064 platform_device_del(platform_device);
2065 fail_platform_device2:
2066 platform_device_put(platform_device);
2067 fail_platform_device1:
2068 platform_driver_unregister(&platform_driver);
2069 fail_platform_driver:
2073 static void __exit dell_exit(void)
2075 debugfs_remove_recursive(dell_laptop_dir);
2076 if (quirks && quirks->touchpad_led)
2077 touchpad_led_exit();
2079 backlight_device_unregister(dell_backlight_device);
2080 dell_cleanup_rfkill();
2081 if (platform_device) {
2082 platform_device_unregister(platform_device);
2083 platform_driver_unregister(&platform_driver);
2087 /* dell-rbtn.c driver export functions which will not work correctly (and could
2088 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2089 * not problem when dell-rbtn.c is compiled as external module. When both files
2090 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2091 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2092 * This can be achieved by late_initcall() instead module_init().
2094 late_initcall(dell_init);
2095 module_exit(dell_exit);
2097 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2098 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2099 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2100 MODULE_DESCRIPTION("Dell laptop driver");
2101 MODULE_LICENSE("GPL");