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/slab.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "../../firmware/dcdbas.h"
36 #include "dell-rbtn.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
47 /* This structure will be modified by the firmware when we enter
48 * system management mode, hence the volatiles */
50 struct calling_interface_buffer
{
53 volatile u32 input
[4];
54 volatile u32 output
[4];
57 struct calling_interface_token
{
66 struct calling_interface_structure
{
67 struct dmi_header header
;
71 struct calling_interface_token tokens
[];
77 int needs_kbd_timeouts
;
79 * Ordered list of timeouts expressed in seconds.
80 * The list must end with -1
85 static struct quirk_entry
*quirks
;
87 static struct quirk_entry quirk_dell_vostro_v130
= {
91 static int __init
dmi_matched(const struct dmi_system_id
*dmi
)
93 quirks
= dmi
->driver_data
;
98 * These values come from Windows utility provided by Dell. If any other value
99 * is used then BIOS silently set timeout to 0 without any error message.
101 static struct quirk_entry quirk_dell_xps13_9333
= {
102 .needs_kbd_timeouts
= 1,
103 .kbd_timeouts
= { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
106 static int da_command_address
;
107 static int da_command_code
;
108 static int da_num_tokens
;
109 static struct calling_interface_token
*da_tokens
;
111 static struct platform_driver platform_driver
= {
113 .name
= "dell-laptop",
117 static struct platform_device
*platform_device
;
118 static struct backlight_device
*dell_backlight_device
;
119 static struct rfkill
*wifi_rfkill
;
120 static struct rfkill
*bluetooth_rfkill
;
121 static struct rfkill
*wwan_rfkill
;
122 static bool force_rfkill
;
124 module_param(force_rfkill
, bool, 0444);
125 MODULE_PARM_DESC(force_rfkill
, "enable rfkill on non whitelisted models");
127 static const struct dmi_system_id dell_device_table
[] __initconst
= {
129 .ident
= "Dell laptop",
131 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
132 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"),
137 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
138 DMI_MATCH(DMI_CHASSIS_TYPE
, "9"), /*Laptop*/
142 .ident
= "Dell Computer Corporation",
144 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Computer Corporation"),
145 DMI_MATCH(DMI_CHASSIS_TYPE
, "8"),
150 MODULE_DEVICE_TABLE(dmi
, dell_device_table
);
152 static const struct dmi_system_id dell_quirks
[] __initconst
= {
154 .callback
= dmi_matched
,
155 .ident
= "Dell Vostro V130",
157 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
158 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro V130"),
160 .driver_data
= &quirk_dell_vostro_v130
,
163 .callback
= dmi_matched
,
164 .ident
= "Dell Vostro V131",
166 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
167 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro V131"),
169 .driver_data
= &quirk_dell_vostro_v130
,
172 .callback
= dmi_matched
,
173 .ident
= "Dell Vostro 3350",
175 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
176 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro 3350"),
178 .driver_data
= &quirk_dell_vostro_v130
,
181 .callback
= dmi_matched
,
182 .ident
= "Dell Vostro 3555",
184 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
185 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro 3555"),
187 .driver_data
= &quirk_dell_vostro_v130
,
190 .callback
= dmi_matched
,
191 .ident
= "Dell Inspiron N311z",
193 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron N311z"),
196 .driver_data
= &quirk_dell_vostro_v130
,
199 .callback
= dmi_matched
,
200 .ident
= "Dell Inspiron M5110",
202 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
203 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron M5110"),
205 .driver_data
= &quirk_dell_vostro_v130
,
208 .callback
= dmi_matched
,
209 .ident
= "Dell Vostro 3360",
211 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
212 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro 3360"),
214 .driver_data
= &quirk_dell_vostro_v130
,
217 .callback
= dmi_matched
,
218 .ident
= "Dell Vostro 3460",
220 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
221 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro 3460"),
223 .driver_data
= &quirk_dell_vostro_v130
,
226 .callback
= dmi_matched
,
227 .ident
= "Dell Vostro 3560",
229 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
230 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro 3560"),
232 .driver_data
= &quirk_dell_vostro_v130
,
235 .callback
= dmi_matched
,
236 .ident
= "Dell Vostro 3450",
238 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
239 DMI_MATCH(DMI_PRODUCT_NAME
, "Dell System Vostro 3450"),
241 .driver_data
= &quirk_dell_vostro_v130
,
244 .callback
= dmi_matched
,
245 .ident
= "Dell Inspiron 5420",
247 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
248 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 5420"),
250 .driver_data
= &quirk_dell_vostro_v130
,
253 .callback
= dmi_matched
,
254 .ident
= "Dell Inspiron 5520",
256 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
257 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 5520"),
259 .driver_data
= &quirk_dell_vostro_v130
,
262 .callback
= dmi_matched
,
263 .ident
= "Dell Inspiron 5720",
265 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
266 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 5720"),
268 .driver_data
= &quirk_dell_vostro_v130
,
271 .callback
= dmi_matched
,
272 .ident
= "Dell Inspiron 7420",
274 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
275 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 7420"),
277 .driver_data
= &quirk_dell_vostro_v130
,
280 .callback
= dmi_matched
,
281 .ident
= "Dell Inspiron 7520",
283 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
284 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 7520"),
286 .driver_data
= &quirk_dell_vostro_v130
,
289 .callback
= dmi_matched
,
290 .ident
= "Dell Inspiron 7720",
292 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
293 DMI_MATCH(DMI_PRODUCT_NAME
, "Inspiron 7720"),
295 .driver_data
= &quirk_dell_vostro_v130
,
298 .callback
= dmi_matched
,
299 .ident
= "Dell XPS13 9333",
301 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
302 DMI_MATCH(DMI_PRODUCT_NAME
, "XPS13 9333"),
304 .driver_data
= &quirk_dell_xps13_9333
,
309 static struct calling_interface_buffer
*buffer
;
310 static DEFINE_MUTEX(buffer_mutex
);
312 static void clear_buffer(void)
314 memset(buffer
, 0, sizeof(struct calling_interface_buffer
));
317 static void get_buffer(void)
319 mutex_lock(&buffer_mutex
);
323 static void release_buffer(void)
325 mutex_unlock(&buffer_mutex
);
328 static void __init
parse_da_table(const struct dmi_header
*dm
)
330 /* Final token is a terminator, so we don't want to copy it */
331 int tokens
= (dm
->length
-11)/sizeof(struct calling_interface_token
)-1;
332 struct calling_interface_token
*new_da_tokens
;
333 struct calling_interface_structure
*table
=
334 container_of(dm
, struct calling_interface_structure
, header
);
336 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
342 da_command_address
= table
->cmdIOAddress
;
343 da_command_code
= table
->cmdIOCode
;
345 new_da_tokens
= krealloc(da_tokens
, (da_num_tokens
+ tokens
) *
346 sizeof(struct calling_interface_token
),
351 da_tokens
= new_da_tokens
;
353 memcpy(da_tokens
+da_num_tokens
, table
->tokens
,
354 sizeof(struct calling_interface_token
) * tokens
);
356 da_num_tokens
+= tokens
;
359 static void __init
find_tokens(const struct dmi_header
*dm
, void *dummy
)
362 case 0xd4: /* Indexed IO */
363 case 0xd5: /* Protected Area Type 1 */
364 case 0xd6: /* Protected Area Type 2 */
366 case 0xda: /* Calling interface */
372 static int find_token_id(int tokenid
)
376 for (i
= 0; i
< da_num_tokens
; i
++) {
377 if (da_tokens
[i
].tokenID
== tokenid
)
384 static int find_token_location(int tokenid
)
388 id
= find_token_id(tokenid
);
392 return da_tokens
[id
].location
;
395 static struct calling_interface_buffer
*
396 dell_send_request(struct calling_interface_buffer
*buffer
, int class,
399 struct smi_cmd command
;
401 command
.magic
= SMI_CMD_MAGIC
;
402 command
.command_address
= da_command_address
;
403 command
.command_code
= da_command_code
;
404 command
.ebx
= virt_to_phys(buffer
);
405 command
.ecx
= 0x42534931;
407 buffer
->class = class;
408 buffer
->select
= select
;
410 dcdbas_smi_request(&command
);
415 static inline int dell_smi_error(int value
)
418 case 0: /* Completed successfully */
420 case -1: /* Completed with error */
422 case -2: /* Function not supported */
424 default: /* Unknown error */
430 * Derived from information in smbios-wireless-ctl:
432 * cbSelect 17, Value 11
434 * Return Wireless Info
435 * cbArg1, byte0 = 0x00
437 * cbRes1 Standard return codes (0, -1, -2)
438 * cbRes2 Info bit flags:
440 * 0 Hardware switch supported (1)
441 * 1 WiFi locator supported (1)
442 * 2 WLAN supported (1)
443 * 3 Bluetooth (BT) supported (1)
444 * 4 WWAN supported (1)
445 * 5 Wireless KBD supported (1)
446 * 6 Uw b supported (1)
447 * 7 WiGig supported (1)
448 * 8 WLAN installed (1)
450 * 10 WWAN installed (1)
451 * 11 Uw b installed (1)
452 * 12 WiGig installed (1)
454 * 16 Hardware (HW) switch is On (1)
455 * 17 WLAN disabled (1)
457 * 19 WWAN disabled (1)
458 * 20 Uw b disabled (1)
459 * 21 WiGig disabled (1)
462 * cbRes3 NVRAM size in bytes
463 * cbRes4, byte 0 NVRAM format version number
466 * Set QuickSet Radio Disable Flag
467 * cbArg1, byte0 = 0x01
476 * cbArg1, byte2 Flag bits:
477 * 0 QuickSet disables radio (1)
480 * cbRes1 Standard return codes (0, -1, -2)
481 * cbRes2 QuickSet (QS) radio disable bit map:
486 * 4 QS disables WIGIG
489 * Wireless Switch Configuration
490 * cbArg1, byte0 = 0x02
496 * 2 Set WiFi locator enable/disable
498 * Switch settings (if byte 1==1):
499 * 0 WLAN sw itch control (1)
500 * 1 BT sw itch control (1)
501 * 2 WWAN sw itch control (1)
502 * 3 UWB sw itch control (1)
503 * 4 WiGig sw itch control (1)
505 * cbArg1, byte2 Enable bits (if byte 1==2):
506 * 0 Enable WiFi locator (1)
508 * cbRes1 Standard return codes (0, -1, -2)
509 * cbRes2 QuickSet radio disable bit map:
510 * 0 WLAN controlled by sw itch (1)
511 * 1 BT controlled by sw itch (1)
512 * 2 WWAN controlled by sw itch (1)
513 * 3 UWB controlled by sw itch (1)
514 * 4 WiGig controlled by sw itch (1)
516 * 7 Wireless sw itch config locked (1)
517 * 8 WiFi locator enabled (1)
519 * 15 WiFi locator setting locked (1)
522 * Read Local Config Data (LCD)
523 * cbArg1, byte0 = 0x10
524 * cbArg1, byte1 NVRAM index low byte
525 * cbArg1, byte2 NVRAM index high byte
526 * cbRes1 Standard return codes (0, -1, -2)
527 * cbRes2 4 bytes read from LCD[index]
528 * cbRes3 4 bytes read from LCD[index+4]
529 * cbRes4 4 bytes read from LCD[index+8]
531 * Write Local Config Data (LCD)
532 * cbArg1, byte0 = 0x11
533 * cbArg1, byte1 NVRAM index low byte
534 * cbArg1, byte2 NVRAM index high byte
535 * cbArg2 4 bytes to w rite at LCD[index]
536 * cbArg3 4 bytes to w rite at LCD[index+4]
537 * cbArg4 4 bytes to w rite at LCD[index+8]
538 * cbRes1 Standard return codes (0, -1, -2)
540 * Populate Local Config Data from NVRAM
541 * cbArg1, byte0 = 0x12
542 * cbRes1 Standard return codes (0, -1, -2)
544 * Commit Local Config Data to NVRAM
545 * cbArg1, byte0 = 0x13
546 * cbRes1 Standard return codes (0, -1, -2)
549 static int dell_rfkill_set(void *data
, bool blocked
)
551 int disable
= blocked
? 1 : 0;
552 unsigned long radio
= (unsigned long)data
;
553 int hwswitch_bit
= (unsigned long)data
- 1;
560 dell_send_request(buffer
, 17, 11);
561 ret
= buffer
->output
[0];
562 status
= buffer
->output
[1];
569 buffer
->input
[0] = 0x2;
570 dell_send_request(buffer
, 17, 11);
571 ret
= buffer
->output
[0];
572 hwswitch
= buffer
->output
[1];
574 /* If the hardware switch controls this radio, and the hardware
575 switch is disabled, always disable the radio */
576 if (ret
== 0 && (hwswitch
& BIT(hwswitch_bit
)) &&
577 (status
& BIT(0)) && !(status
& BIT(16)))
582 buffer
->input
[0] = (1 | (radio
<<8) | (disable
<< 16));
583 dell_send_request(buffer
, 17, 11);
584 ret
= buffer
->output
[0];
588 return dell_smi_error(ret
);
591 /* Must be called with the buffer held */
592 static void dell_rfkill_update_sw_state(struct rfkill
*rfkill
, int radio
,
595 if (status
& BIT(0)) {
596 /* Has hw-switch, sync sw_state to BIOS */
597 int block
= rfkill_blocked(rfkill
);
599 buffer
->input
[0] = (1 | (radio
<< 8) | (block
<< 16));
600 dell_send_request(buffer
, 17, 11);
602 /* No hw-switch, sync BIOS state to sw_state */
603 rfkill_set_sw_state(rfkill
, !!(status
& BIT(radio
+ 16)));
607 static void dell_rfkill_update_hw_state(struct rfkill
*rfkill
, int radio
,
608 int status
, int hwswitch
)
610 if (hwswitch
& (BIT(radio
- 1)))
611 rfkill_set_hw_state(rfkill
, !(status
& BIT(16)));
614 static void dell_rfkill_query(struct rfkill
*rfkill
, void *data
)
616 int radio
= ((unsigned long)data
& 0xF);
623 dell_send_request(buffer
, 17, 11);
624 ret
= buffer
->output
[0];
625 status
= buffer
->output
[1];
627 if (ret
!= 0 || !(status
& BIT(0))) {
634 buffer
->input
[0] = 0x2;
635 dell_send_request(buffer
, 17, 11);
636 ret
= buffer
->output
[0];
637 hwswitch
= buffer
->output
[1];
644 dell_rfkill_update_hw_state(rfkill
, radio
, status
, hwswitch
);
647 static const struct rfkill_ops dell_rfkill_ops
= {
648 .set_block
= dell_rfkill_set
,
649 .query
= dell_rfkill_query
,
652 static struct dentry
*dell_laptop_dir
;
654 static int dell_debugfs_show(struct seq_file
*s
, void *data
)
663 dell_send_request(buffer
, 17, 11);
664 ret
= buffer
->output
[0];
665 status
= buffer
->output
[1];
669 buffer
->input
[0] = 0x2;
670 dell_send_request(buffer
, 17, 11);
671 hwswitch_ret
= buffer
->output
[0];
672 hwswitch_state
= buffer
->output
[1];
676 seq_printf(s
, "return:\t%d\n", ret
);
677 seq_printf(s
, "status:\t0x%X\n", status
);
678 seq_printf(s
, "Bit 0 : Hardware switch supported: %lu\n",
680 seq_printf(s
, "Bit 1 : Wifi locator supported: %lu\n",
681 (status
& BIT(1)) >> 1);
682 seq_printf(s
, "Bit 2 : Wifi is supported: %lu\n",
683 (status
& BIT(2)) >> 2);
684 seq_printf(s
, "Bit 3 : Bluetooth is supported: %lu\n",
685 (status
& BIT(3)) >> 3);
686 seq_printf(s
, "Bit 4 : WWAN is supported: %lu\n",
687 (status
& BIT(4)) >> 4);
688 seq_printf(s
, "Bit 5 : Wireless keyboard supported: %lu\n",
689 (status
& BIT(5)) >> 5);
690 seq_printf(s
, "Bit 6 : UWB supported: %lu\n",
691 (status
& BIT(6)) >> 6);
692 seq_printf(s
, "Bit 7 : WiGig supported: %lu\n",
693 (status
& BIT(7)) >> 7);
694 seq_printf(s
, "Bit 8 : Wifi is installed: %lu\n",
695 (status
& BIT(8)) >> 8);
696 seq_printf(s
, "Bit 9 : Bluetooth is installed: %lu\n",
697 (status
& BIT(9)) >> 9);
698 seq_printf(s
, "Bit 10: WWAN is installed: %lu\n",
699 (status
& BIT(10)) >> 10);
700 seq_printf(s
, "Bit 11: UWB installed: %lu\n",
701 (status
& BIT(11)) >> 11);
702 seq_printf(s
, "Bit 12: WiGig installed: %lu\n",
703 (status
& BIT(12)) >> 12);
705 seq_printf(s
, "Bit 16: Hardware switch is on: %lu\n",
706 (status
& BIT(16)) >> 16);
707 seq_printf(s
, "Bit 17: Wifi is blocked: %lu\n",
708 (status
& BIT(17)) >> 17);
709 seq_printf(s
, "Bit 18: Bluetooth is blocked: %lu\n",
710 (status
& BIT(18)) >> 18);
711 seq_printf(s
, "Bit 19: WWAN is blocked: %lu\n",
712 (status
& BIT(19)) >> 19);
713 seq_printf(s
, "Bit 20: UWB is blocked: %lu\n",
714 (status
& BIT(20)) >> 20);
715 seq_printf(s
, "Bit 21: WiGig is blocked: %lu\n",
716 (status
& BIT(21)) >> 21);
718 seq_printf(s
, "\nhwswitch_return:\t%d\n", hwswitch_ret
);
719 seq_printf(s
, "hwswitch_state:\t0x%X\n", hwswitch_state
);
720 seq_printf(s
, "Bit 0 : Wifi controlled by switch: %lu\n",
721 hwswitch_state
& BIT(0));
722 seq_printf(s
, "Bit 1 : Bluetooth controlled by switch: %lu\n",
723 (hwswitch_state
& BIT(1)) >> 1);
724 seq_printf(s
, "Bit 2 : WWAN controlled by switch: %lu\n",
725 (hwswitch_state
& BIT(2)) >> 2);
726 seq_printf(s
, "Bit 3 : UWB controlled by switch: %lu\n",
727 (hwswitch_state
& BIT(3)) >> 3);
728 seq_printf(s
, "Bit 4 : WiGig controlled by switch: %lu\n",
729 (hwswitch_state
& BIT(4)) >> 4);
730 seq_printf(s
, "Bit 7 : Wireless switch config locked: %lu\n",
731 (hwswitch_state
& BIT(7)) >> 7);
732 seq_printf(s
, "Bit 8 : Wifi locator enabled: %lu\n",
733 (hwswitch_state
& BIT(8)) >> 8);
734 seq_printf(s
, "Bit 15: Wifi locator setting locked: %lu\n",
735 (hwswitch_state
& BIT(15)) >> 15);
740 static int dell_debugfs_open(struct inode
*inode
, struct file
*file
)
742 return single_open(file
, dell_debugfs_show
, inode
->i_private
);
745 static const struct file_operations dell_debugfs_fops
= {
746 .owner
= THIS_MODULE
,
747 .open
= dell_debugfs_open
,
750 .release
= single_release
,
753 static void dell_update_rfkill(struct work_struct
*ignored
)
761 dell_send_request(buffer
, 17, 11);
762 ret
= buffer
->output
[0];
763 status
= buffer
->output
[1];
770 buffer
->input
[0] = 0x2;
771 dell_send_request(buffer
, 17, 11);
772 ret
= buffer
->output
[0];
774 if (ret
== 0 && (status
& BIT(0)))
775 hwswitch
= buffer
->output
[1];
778 dell_rfkill_update_hw_state(wifi_rfkill
, 1, status
, hwswitch
);
779 dell_rfkill_update_sw_state(wifi_rfkill
, 1, status
);
781 if (bluetooth_rfkill
) {
782 dell_rfkill_update_hw_state(bluetooth_rfkill
, 2, status
,
784 dell_rfkill_update_sw_state(bluetooth_rfkill
, 2, status
);
787 dell_rfkill_update_hw_state(wwan_rfkill
, 3, status
, hwswitch
);
788 dell_rfkill_update_sw_state(wwan_rfkill
, 3, status
);
794 static DECLARE_DELAYED_WORK(dell_rfkill_work
, dell_update_rfkill
);
796 static bool dell_laptop_i8042_filter(unsigned char data
, unsigned char str
,
799 static bool extended
;
801 if (str
& I8042_STR_AUXDATA
)
804 if (unlikely(data
== 0xe0)) {
807 } else if (unlikely(extended
)) {
810 schedule_delayed_work(&dell_rfkill_work
,
811 round_jiffies_relative(HZ
/ 4));
820 static int (*dell_rbtn_notifier_register_func
)(struct notifier_block
*);
821 static int (*dell_rbtn_notifier_unregister_func
)(struct notifier_block
*);
823 static int dell_laptop_rbtn_notifier_call(struct notifier_block
*nb
,
824 unsigned long action
, void *data
)
826 schedule_delayed_work(&dell_rfkill_work
, 0);
830 static struct notifier_block dell_laptop_rbtn_notifier
= {
831 .notifier_call
= dell_laptop_rbtn_notifier_call
,
834 static int __init
dell_setup_rfkill(void)
836 int status
, ret
, whitelisted
;
840 * rfkill support causes trouble on various models, mostly Inspirons.
841 * So we whitelist certain series, and don't support rfkill on others.
844 product
= dmi_get_system_info(DMI_PRODUCT_NAME
);
845 if (product
&& (strncmp(product
, "Latitude", 8) == 0 ||
846 strncmp(product
, "Precision", 9) == 0))
848 if (!force_rfkill
&& !whitelisted
)
852 dell_send_request(buffer
, 17, 11);
853 ret
= buffer
->output
[0];
854 status
= buffer
->output
[1];
857 /* dell wireless info smbios call is not supported */
861 /* rfkill is only tested on laptops with a hwswitch */
862 if (!(status
& BIT(0)) && !force_rfkill
)
865 if ((status
& (1<<2|1<<8)) == (1<<2|1<<8)) {
866 wifi_rfkill
= rfkill_alloc("dell-wifi", &platform_device
->dev
,
868 &dell_rfkill_ops
, (void *) 1);
873 ret
= rfkill_register(wifi_rfkill
);
878 if ((status
& (1<<3|1<<9)) == (1<<3|1<<9)) {
879 bluetooth_rfkill
= rfkill_alloc("dell-bluetooth",
880 &platform_device
->dev
,
881 RFKILL_TYPE_BLUETOOTH
,
882 &dell_rfkill_ops
, (void *) 2);
883 if (!bluetooth_rfkill
) {
887 ret
= rfkill_register(bluetooth_rfkill
);
892 if ((status
& (1<<4|1<<10)) == (1<<4|1<<10)) {
893 wwan_rfkill
= rfkill_alloc("dell-wwan",
894 &platform_device
->dev
,
896 &dell_rfkill_ops
, (void *) 3);
901 ret
= rfkill_register(wwan_rfkill
);
907 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
908 * which can receive events from HW slider switch.
910 * Dell SMBIOS on whitelisted models supports controlling radio devices
911 * but does not support receiving HW button switch events. We can use
912 * i8042 filter hook function to receive keyboard data and handle
913 * keycode for HW button.
915 * So if it is possible we will use Dell Airplane Mode Switch ACPI
916 * driver for receiving HW events and Dell SMBIOS for setting rfkill
917 * states. If ACPI driver or device is not available we will fallback to
918 * i8042 filter hook function.
920 * To prevent duplicate rfkill devices which control and do same thing,
921 * dell-rbtn driver will automatically remove its own rfkill devices
922 * once function dell_rbtn_notifier_register() is called.
925 dell_rbtn_notifier_register_func
=
926 symbol_request(dell_rbtn_notifier_register
);
927 if (dell_rbtn_notifier_register_func
) {
928 dell_rbtn_notifier_unregister_func
=
929 symbol_request(dell_rbtn_notifier_unregister
);
930 if (!dell_rbtn_notifier_unregister_func
) {
931 symbol_put(dell_rbtn_notifier_register
);
932 dell_rbtn_notifier_register_func
= NULL
;
936 if (dell_rbtn_notifier_register_func
) {
937 ret
= dell_rbtn_notifier_register_func(
938 &dell_laptop_rbtn_notifier
);
939 symbol_put(dell_rbtn_notifier_register
);
940 dell_rbtn_notifier_register_func
= NULL
;
942 symbol_put(dell_rbtn_notifier_unregister
);
943 dell_rbtn_notifier_unregister_func
= NULL
;
946 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
951 pr_info("Using dell-rbtn acpi driver for receiving events\n");
952 } else if (ret
!= -ENODEV
) {
953 pr_warn("Unable to register dell rbtn notifier\n");
956 ret
= i8042_install_filter(dell_laptop_i8042_filter
);
958 pr_warn("Unable to install key filter\n");
961 pr_info("Using i8042 filter function for receiving events\n");
967 rfkill_unregister(wwan_rfkill
);
969 rfkill_destroy(wwan_rfkill
);
970 if (bluetooth_rfkill
)
971 rfkill_unregister(bluetooth_rfkill
);
973 rfkill_destroy(bluetooth_rfkill
);
975 rfkill_unregister(wifi_rfkill
);
977 rfkill_destroy(wifi_rfkill
);
982 static void dell_cleanup_rfkill(void)
984 if (dell_rbtn_notifier_unregister_func
) {
985 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier
);
986 symbol_put(dell_rbtn_notifier_unregister
);
987 dell_rbtn_notifier_unregister_func
= NULL
;
989 i8042_remove_filter(dell_laptop_i8042_filter
);
991 cancel_delayed_work_sync(&dell_rfkill_work
);
993 rfkill_unregister(wifi_rfkill
);
994 rfkill_destroy(wifi_rfkill
);
996 if (bluetooth_rfkill
) {
997 rfkill_unregister(bluetooth_rfkill
);
998 rfkill_destroy(bluetooth_rfkill
);
1001 rfkill_unregister(wwan_rfkill
);
1002 rfkill_destroy(wwan_rfkill
);
1006 static int dell_send_intensity(struct backlight_device
*bd
)
1011 token
= find_token_location(BRIGHTNESS_TOKEN
);
1016 buffer
->input
[0] = token
;
1017 buffer
->input
[1] = bd
->props
.brightness
;
1019 if (power_supply_is_system_supplied() > 0)
1020 dell_send_request(buffer
, 1, 2);
1022 dell_send_request(buffer
, 1, 1);
1024 ret
= dell_smi_error(buffer
->output
[0]);
1030 static int dell_get_intensity(struct backlight_device
*bd
)
1035 token
= find_token_location(BRIGHTNESS_TOKEN
);
1040 buffer
->input
[0] = token
;
1042 if (power_supply_is_system_supplied() > 0)
1043 dell_send_request(buffer
, 0, 2);
1045 dell_send_request(buffer
, 0, 1);
1047 if (buffer
->output
[0])
1048 ret
= dell_smi_error(buffer
->output
[0]);
1050 ret
= buffer
->output
[1];
1056 static const struct backlight_ops dell_ops
= {
1057 .get_brightness
= dell_get_intensity
,
1058 .update_status
= dell_send_intensity
,
1061 static void touchpad_led_on(void)
1065 i8042_command(&data
, command
| 1 << 12);
1068 static void touchpad_led_off(void)
1072 i8042_command(&data
, command
| 1 << 12);
1075 static void touchpad_led_set(struct led_classdev
*led_cdev
,
1076 enum led_brightness value
)
1084 static struct led_classdev touchpad_led
= {
1085 .name
= "dell-laptop::touchpad",
1086 .brightness_set
= touchpad_led_set
,
1087 .flags
= LED_CORE_SUSPENDRESUME
,
1090 static int __init
touchpad_led_init(struct device
*dev
)
1092 return led_classdev_register(dev
, &touchpad_led
);
1095 static void touchpad_led_exit(void)
1097 led_classdev_unregister(&touchpad_led
);
1101 * Derived from information in smbios-keyboard-ctl:
1105 * Keyboard illumination
1106 * cbArg1 determines the function to be performed
1108 * cbArg1 0x0 = Get Feature Information
1109 * cbRES1 Standard return codes (0, -1, -2)
1110 * cbRES2, word0 Bitmap of user-selectable modes
1111 * bit 0 Always off (All systems)
1112 * bit 1 Always on (Travis ATG, Siberia)
1113 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1114 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1115 * bit 4 Auto: Input-activity-based On; input-activity based Off
1116 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1117 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1118 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1119 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1120 * bits 9-15 Reserved for future use
1121 * cbRES2, byte2 Reserved for future use
1122 * cbRES2, byte3 Keyboard illumination type
1126 * 3-255 Reserved for future use
1127 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1128 * bit 0 Any keystroke
1129 * bit 1 Touchpad activity
1130 * bit 2 Pointing stick
1132 * bits 4-7 Reserved for future use
1133 * cbRES3, byte1 Supported timeout unit bitmap
1138 * bits 4-7 Reserved for future use
1139 * cbRES3, byte2 Number of keyboard light brightness levels
1140 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1141 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1142 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1143 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1145 * cbArg1 0x1 = Get Current State
1146 * cbRES1 Standard return codes (0, -1, -2)
1147 * cbRES2, word0 Bitmap of current mode state
1148 * bit 0 Always off (All systems)
1149 * bit 1 Always on (Travis ATG, Siberia)
1150 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1151 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1152 * bit 4 Auto: Input-activity-based On; input-activity based Off
1153 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1154 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1155 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1156 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1157 * bits 9-15 Reserved for future use
1158 * Note: Only One bit can be set
1159 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1160 * bit 0 Any keystroke
1161 * bit 1 Touchpad activity
1162 * bit 2 Pointing stick
1164 * bits 4-7 Reserved for future use
1165 * cbRES2, byte3 Current Timeout
1166 * bits 7:6 Timeout units indicator:
1171 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1172 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1173 * are set upon return from the [Get feature information] call.
1174 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1175 * cbRES3, byte1 Current ALS reading
1176 * cbRES3, byte2 Current keyboard light level.
1178 * cbArg1 0x2 = Set New State
1179 * cbRES1 Standard return codes (0, -1, -2)
1180 * cbArg2, word0 Bitmap of current mode state
1181 * bit 0 Always off (All systems)
1182 * bit 1 Always on (Travis ATG, Siberia)
1183 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1184 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1185 * bit 4 Auto: Input-activity-based On; input-activity based Off
1186 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1187 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1188 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1189 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1190 * bits 9-15 Reserved for future use
1191 * Note: Only One bit can be set
1192 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1193 * keyboard to turn off automatically.
1194 * bit 0 Any keystroke
1195 * bit 1 Touchpad activity
1196 * bit 2 Pointing stick
1198 * bits 4-7 Reserved for future use
1199 * cbArg2, byte3 Desired Timeout
1200 * bits 7:6 Timeout units indicator:
1205 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1206 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1207 * cbArg3, byte2 Desired keyboard light level.
1211 enum kbd_timeout_unit
{
1212 KBD_TIMEOUT_SECONDS
= 0,
1213 KBD_TIMEOUT_MINUTES
,
1219 KBD_MODE_BIT_OFF
= 0,
1222 KBD_MODE_BIT_TRIGGER_ALS
,
1223 KBD_MODE_BIT_TRIGGER
,
1224 KBD_MODE_BIT_TRIGGER_25
,
1225 KBD_MODE_BIT_TRIGGER_50
,
1226 KBD_MODE_BIT_TRIGGER_75
,
1227 KBD_MODE_BIT_TRIGGER_100
,
1230 #define kbd_is_als_mode_bit(bit) \
1231 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1232 #define kbd_is_trigger_mode_bit(bit) \
1233 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1234 #define kbd_is_level_mode_bit(bit) \
1235 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1258 static const int kbd_tokens
[] = {
1260 KBD_LED_AUTO_25_TOKEN
,
1261 KBD_LED_AUTO_50_TOKEN
,
1262 KBD_LED_AUTO_75_TOKEN
,
1263 KBD_LED_AUTO_100_TOKEN
,
1267 static u16 kbd_token_bits
;
1269 static struct kbd_info kbd_info
;
1270 static bool kbd_als_supported
;
1271 static bool kbd_triggers_supported
;
1273 static u8 kbd_mode_levels
[16];
1274 static int kbd_mode_levels_count
;
1276 static u8 kbd_previous_level
;
1277 static u8 kbd_previous_mode_bit
;
1279 static bool kbd_led_present
;
1282 * NOTE: there are three ways to set the keyboard backlight level.
1283 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1284 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1285 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1287 * There are laptops which support only one of these methods. If we want to
1288 * support as many machines as possible we need to implement all three methods.
1289 * The first two methods use the kbd_state structure. The third uses SMBIOS
1290 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1291 * keyboard backlight level via kbd_state.level.
1294 static int kbd_get_info(struct kbd_info
*info
)
1301 buffer
->input
[0] = 0x0;
1302 dell_send_request(buffer
, 4, 11);
1303 ret
= buffer
->output
[0];
1306 ret
= dell_smi_error(ret
);
1310 info
->modes
= buffer
->output
[1] & 0xFFFF;
1311 info
->type
= (buffer
->output
[1] >> 24) & 0xFF;
1312 info
->triggers
= buffer
->output
[2] & 0xFF;
1313 units
= (buffer
->output
[2] >> 8) & 0xFF;
1314 info
->levels
= (buffer
->output
[2] >> 16) & 0xFF;
1317 info
->seconds
= (buffer
->output
[3] >> 0) & 0xFF;
1319 info
->minutes
= (buffer
->output
[3] >> 8) & 0xFF;
1321 info
->hours
= (buffer
->output
[3] >> 16) & 0xFF;
1323 info
->days
= (buffer
->output
[3] >> 24) & 0xFF;
1330 static unsigned int kbd_get_max_level(void)
1332 if (kbd_info
.levels
!= 0)
1333 return kbd_info
.levels
;
1334 if (kbd_mode_levels_count
> 0)
1335 return kbd_mode_levels_count
- 1;
1339 static int kbd_get_level(struct kbd_state
*state
)
1343 if (kbd_info
.levels
!= 0)
1344 return state
->level
;
1346 if (kbd_mode_levels_count
> 0) {
1347 for (i
= 0; i
< kbd_mode_levels_count
; ++i
)
1348 if (kbd_mode_levels
[i
] == state
->mode_bit
)
1356 static int kbd_set_level(struct kbd_state
*state
, u8 level
)
1358 if (kbd_info
.levels
!= 0) {
1360 kbd_previous_level
= level
;
1361 if (state
->level
== level
)
1363 state
->level
= level
;
1364 if (level
!= 0 && state
->mode_bit
== KBD_MODE_BIT_OFF
)
1365 state
->mode_bit
= kbd_previous_mode_bit
;
1366 else if (level
== 0 && state
->mode_bit
!= KBD_MODE_BIT_OFF
) {
1367 kbd_previous_mode_bit
= state
->mode_bit
;
1368 state
->mode_bit
= KBD_MODE_BIT_OFF
;
1373 if (kbd_mode_levels_count
> 0 && level
< kbd_mode_levels_count
) {
1375 kbd_previous_level
= level
;
1376 state
->mode_bit
= kbd_mode_levels
[level
];
1383 static int kbd_get_state(struct kbd_state
*state
)
1389 buffer
->input
[0] = 0x1;
1390 dell_send_request(buffer
, 4, 11);
1391 ret
= buffer
->output
[0];
1394 ret
= dell_smi_error(ret
);
1398 state
->mode_bit
= ffs(buffer
->output
[1] & 0xFFFF);
1399 if (state
->mode_bit
!= 0)
1402 state
->triggers
= (buffer
->output
[1] >> 16) & 0xFF;
1403 state
->timeout_value
= (buffer
->output
[1] >> 24) & 0x3F;
1404 state
->timeout_unit
= (buffer
->output
[1] >> 30) & 0x3;
1405 state
->als_setting
= buffer
->output
[2] & 0xFF;
1406 state
->als_value
= (buffer
->output
[2] >> 8) & 0xFF;
1407 state
->level
= (buffer
->output
[2] >> 16) & 0xFF;
1414 static int kbd_set_state(struct kbd_state
*state
)
1419 buffer
->input
[0] = 0x2;
1420 buffer
->input
[1] = BIT(state
->mode_bit
) & 0xFFFF;
1421 buffer
->input
[1] |= (state
->triggers
& 0xFF) << 16;
1422 buffer
->input
[1] |= (state
->timeout_value
& 0x3F) << 24;
1423 buffer
->input
[1] |= (state
->timeout_unit
& 0x3) << 30;
1424 buffer
->input
[2] = state
->als_setting
& 0xFF;
1425 buffer
->input
[2] |= (state
->level
& 0xFF) << 16;
1426 dell_send_request(buffer
, 4, 11);
1427 ret
= buffer
->output
[0];
1430 return dell_smi_error(ret
);
1433 static int kbd_set_state_safe(struct kbd_state
*state
, struct kbd_state
*old
)
1437 ret
= kbd_set_state(state
);
1442 * When setting the new state fails,try to restore the previous one.
1443 * This is needed on some machines where BIOS sets a default state when
1444 * setting a new state fails. This default state could be all off.
1447 if (kbd_set_state(old
))
1448 pr_err("Setting old previous keyboard state failed\n");
1453 static int kbd_set_token_bit(u8 bit
)
1458 if (bit
>= ARRAY_SIZE(kbd_tokens
))
1461 id
= find_token_id(kbd_tokens
[bit
]);
1466 buffer
->input
[0] = da_tokens
[id
].location
;
1467 buffer
->input
[1] = da_tokens
[id
].value
;
1468 dell_send_request(buffer
, 1, 0);
1469 ret
= buffer
->output
[0];
1472 return dell_smi_error(ret
);
1475 static int kbd_get_token_bit(u8 bit
)
1481 if (bit
>= ARRAY_SIZE(kbd_tokens
))
1484 id
= find_token_id(kbd_tokens
[bit
]);
1489 buffer
->input
[0] = da_tokens
[id
].location
;
1490 dell_send_request(buffer
, 0, 0);
1491 ret
= buffer
->output
[0];
1492 val
= buffer
->output
[1];
1496 return dell_smi_error(ret
);
1498 return (val
== da_tokens
[id
].value
);
1501 static int kbd_get_first_active_token_bit(void)
1506 for (i
= 0; i
< ARRAY_SIZE(kbd_tokens
); ++i
) {
1507 ret
= kbd_get_token_bit(i
);
1515 static int kbd_get_valid_token_counts(void)
1517 return hweight16(kbd_token_bits
);
1520 static inline int kbd_init_info(void)
1522 struct kbd_state state
;
1526 ret
= kbd_get_info(&kbd_info
);
1530 kbd_get_state(&state
);
1532 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1533 if (kbd_info
.seconds
> 63)
1534 kbd_info
.seconds
= 63;
1535 if (kbd_info
.minutes
> 63)
1536 kbd_info
.minutes
= 63;
1537 if (kbd_info
.hours
> 63)
1538 kbd_info
.hours
= 63;
1539 if (kbd_info
.days
> 63)
1542 /* NOTE: On tested machines ON mode did not work and caused
1543 * problems (turned backlight off) so do not use it
1545 kbd_info
.modes
&= ~BIT(KBD_MODE_BIT_ON
);
1547 kbd_previous_level
= kbd_get_level(&state
);
1548 kbd_previous_mode_bit
= state
.mode_bit
;
1550 if (kbd_previous_level
== 0 && kbd_get_max_level() != 0)
1551 kbd_previous_level
= 1;
1553 if (kbd_previous_mode_bit
== KBD_MODE_BIT_OFF
) {
1554 kbd_previous_mode_bit
=
1555 ffs(kbd_info
.modes
& ~BIT(KBD_MODE_BIT_OFF
));
1556 if (kbd_previous_mode_bit
!= 0)
1557 kbd_previous_mode_bit
--;
1560 if (kbd_info
.modes
& (BIT(KBD_MODE_BIT_ALS
) |
1561 BIT(KBD_MODE_BIT_TRIGGER_ALS
)))
1562 kbd_als_supported
= true;
1564 if (kbd_info
.modes
& (
1565 BIT(KBD_MODE_BIT_TRIGGER_ALS
) | BIT(KBD_MODE_BIT_TRIGGER
) |
1566 BIT(KBD_MODE_BIT_TRIGGER_25
) | BIT(KBD_MODE_BIT_TRIGGER_50
) |
1567 BIT(KBD_MODE_BIT_TRIGGER_75
) | BIT(KBD_MODE_BIT_TRIGGER_100
)
1569 kbd_triggers_supported
= true;
1571 /* kbd_mode_levels[0] is reserved, see below */
1572 for (i
= 0; i
< 16; ++i
)
1573 if (kbd_is_level_mode_bit(i
) && (BIT(i
) & kbd_info
.modes
))
1574 kbd_mode_levels
[1 + kbd_mode_levels_count
++] = i
;
1577 * Find the first supported mode and assign to kbd_mode_levels[0].
1578 * This should be 0 (off), but we cannot depend on the BIOS to
1581 if (kbd_mode_levels_count
> 0) {
1582 for (i
= 0; i
< 16; ++i
) {
1583 if (BIT(i
) & kbd_info
.modes
) {
1584 kbd_mode_levels
[0] = i
;
1588 kbd_mode_levels_count
++;
1595 static inline void kbd_init_tokens(void)
1599 for (i
= 0; i
< ARRAY_SIZE(kbd_tokens
); ++i
)
1600 if (find_token_id(kbd_tokens
[i
]) != -1)
1601 kbd_token_bits
|= BIT(i
);
1604 static void kbd_init(void)
1608 ret
= kbd_init_info();
1611 if (kbd_token_bits
!= 0 || ret
== 0)
1612 kbd_led_present
= true;
1615 static ssize_t
kbd_led_timeout_store(struct device
*dev
,
1616 struct device_attribute
*attr
,
1617 const char *buf
, size_t count
)
1619 struct kbd_state new_state
;
1620 struct kbd_state state
;
1628 ret
= sscanf(buf
, "%d %c", &value
, &ch
);
1641 if (value
> kbd_info
.seconds
)
1643 unit
= KBD_TIMEOUT_SECONDS
;
1646 if (value
> kbd_info
.minutes
)
1648 unit
= KBD_TIMEOUT_MINUTES
;
1651 if (value
> kbd_info
.hours
)
1653 unit
= KBD_TIMEOUT_HOURS
;
1656 if (value
> kbd_info
.days
)
1658 unit
= KBD_TIMEOUT_DAYS
;
1664 if (quirks
&& quirks
->needs_kbd_timeouts
)
1668 /* Convert value from current units to seconds */
1670 case KBD_TIMEOUT_DAYS
:
1672 case KBD_TIMEOUT_HOURS
:
1674 case KBD_TIMEOUT_MINUTES
:
1676 unit
= KBD_TIMEOUT_SECONDS
;
1679 if (quirks
&& quirks
->needs_kbd_timeouts
) {
1680 for (i
= 0; quirks
->kbd_timeouts
[i
] != -1; i
++) {
1681 if (value
<= quirks
->kbd_timeouts
[i
]) {
1682 value
= quirks
->kbd_timeouts
[i
];
1688 if (value
<= kbd_info
.seconds
&& kbd_info
.seconds
) {
1689 unit
= KBD_TIMEOUT_SECONDS
;
1690 } else if (value
/ 60 <= kbd_info
.minutes
&& kbd_info
.minutes
) {
1692 unit
= KBD_TIMEOUT_MINUTES
;
1693 } else if (value
/ (60 * 60) <= kbd_info
.hours
&& kbd_info
.hours
) {
1695 unit
= KBD_TIMEOUT_HOURS
;
1696 } else if (value
/ (60 * 60 * 24) <= kbd_info
.days
&& kbd_info
.days
) {
1697 value
/= (60 * 60 * 24);
1698 unit
= KBD_TIMEOUT_DAYS
;
1704 ret
= kbd_get_state(&state
);
1709 new_state
.timeout_value
= value
;
1710 new_state
.timeout_unit
= unit
;
1712 ret
= kbd_set_state_safe(&new_state
, &state
);
1719 static ssize_t
kbd_led_timeout_show(struct device
*dev
,
1720 struct device_attribute
*attr
, char *buf
)
1722 struct kbd_state state
;
1726 ret
= kbd_get_state(&state
);
1730 len
= sprintf(buf
, "%d", state
.timeout_value
);
1732 switch (state
.timeout_unit
) {
1733 case KBD_TIMEOUT_SECONDS
:
1734 return len
+ sprintf(buf
+len
, "s\n");
1735 case KBD_TIMEOUT_MINUTES
:
1736 return len
+ sprintf(buf
+len
, "m\n");
1737 case KBD_TIMEOUT_HOURS
:
1738 return len
+ sprintf(buf
+len
, "h\n");
1739 case KBD_TIMEOUT_DAYS
:
1740 return len
+ sprintf(buf
+len
, "d\n");
1748 static DEVICE_ATTR(stop_timeout
, S_IRUGO
| S_IWUSR
,
1749 kbd_led_timeout_show
, kbd_led_timeout_store
);
1751 static const char * const kbd_led_triggers
[] = {
1754 /*"trackstick"*/ NULL
, /* NOTE: trackstick is just alias for touchpad */
1758 static ssize_t
kbd_led_triggers_store(struct device
*dev
,
1759 struct device_attribute
*attr
,
1760 const char *buf
, size_t count
)
1762 struct kbd_state new_state
;
1763 struct kbd_state state
;
1764 bool triggers_enabled
= false;
1765 int trigger_bit
= -1;
1769 ret
= sscanf(buf
, "%20s", trigger
);
1773 if (trigger
[0] != '+' && trigger
[0] != '-')
1776 ret
= kbd_get_state(&state
);
1780 if (kbd_triggers_supported
)
1781 triggers_enabled
= kbd_is_trigger_mode_bit(state
.mode_bit
);
1783 if (kbd_triggers_supported
) {
1784 for (i
= 0; i
< ARRAY_SIZE(kbd_led_triggers
); ++i
) {
1785 if (!(kbd_info
.triggers
& BIT(i
)))
1787 if (!kbd_led_triggers
[i
])
1789 if (strcmp(trigger
+1, kbd_led_triggers
[i
]) != 0)
1791 if (trigger
[0] == '+' &&
1792 triggers_enabled
&& (state
.triggers
& BIT(i
)))
1794 if (trigger
[0] == '-' &&
1795 (!triggers_enabled
|| !(state
.triggers
& BIT(i
))))
1802 if (trigger_bit
!= -1) {
1804 if (trigger
[0] == '+')
1805 new_state
.triggers
|= BIT(trigger_bit
);
1807 new_state
.triggers
&= ~BIT(trigger_bit
);
1808 /* NOTE: trackstick bit (2) must be disabled when
1809 * disabling touchpad bit (1), otherwise touchpad
1810 * bit (1) will not be disabled */
1811 if (trigger_bit
== 1)
1812 new_state
.triggers
&= ~BIT(2);
1814 if ((kbd_info
.triggers
& new_state
.triggers
) !=
1817 if (new_state
.triggers
&& !triggers_enabled
) {
1818 new_state
.mode_bit
= KBD_MODE_BIT_TRIGGER
;
1819 kbd_set_level(&new_state
, kbd_previous_level
);
1820 } else if (new_state
.triggers
== 0) {
1821 kbd_set_level(&new_state
, 0);
1823 if (!(kbd_info
.modes
& BIT(new_state
.mode_bit
)))
1825 ret
= kbd_set_state_safe(&new_state
, &state
);
1828 if (new_state
.mode_bit
!= KBD_MODE_BIT_OFF
)
1829 kbd_previous_mode_bit
= new_state
.mode_bit
;
1836 static ssize_t
kbd_led_triggers_show(struct device
*dev
,
1837 struct device_attribute
*attr
, char *buf
)
1839 struct kbd_state state
;
1840 bool triggers_enabled
;
1844 ret
= kbd_get_state(&state
);
1850 if (kbd_triggers_supported
) {
1851 triggers_enabled
= kbd_is_trigger_mode_bit(state
.mode_bit
);
1852 level
= kbd_get_level(&state
);
1853 for (i
= 0; i
< ARRAY_SIZE(kbd_led_triggers
); ++i
) {
1854 if (!(kbd_info
.triggers
& BIT(i
)))
1856 if (!kbd_led_triggers
[i
])
1858 if ((triggers_enabled
|| level
<= 0) &&
1859 (state
.triggers
& BIT(i
)))
1863 len
+= sprintf(buf
+len
, "%s ", kbd_led_triggers
[i
]);
1868 buf
[len
- 1] = '\n';
1873 static DEVICE_ATTR(start_triggers
, S_IRUGO
| S_IWUSR
,
1874 kbd_led_triggers_show
, kbd_led_triggers_store
);
1876 static ssize_t
kbd_led_als_enabled_store(struct device
*dev
,
1877 struct device_attribute
*attr
,
1878 const char *buf
, size_t count
)
1880 struct kbd_state new_state
;
1881 struct kbd_state state
;
1882 bool triggers_enabled
= false;
1886 ret
= kstrtoint(buf
, 0, &enable
);
1890 ret
= kbd_get_state(&state
);
1894 if (enable
== kbd_is_als_mode_bit(state
.mode_bit
))
1899 if (kbd_triggers_supported
)
1900 triggers_enabled
= kbd_is_trigger_mode_bit(state
.mode_bit
);
1903 if (triggers_enabled
)
1904 new_state
.mode_bit
= KBD_MODE_BIT_TRIGGER_ALS
;
1906 new_state
.mode_bit
= KBD_MODE_BIT_ALS
;
1908 if (triggers_enabled
) {
1909 new_state
.mode_bit
= KBD_MODE_BIT_TRIGGER
;
1910 kbd_set_level(&new_state
, kbd_previous_level
);
1912 new_state
.mode_bit
= KBD_MODE_BIT_ON
;
1915 if (!(kbd_info
.modes
& BIT(new_state
.mode_bit
)))
1918 ret
= kbd_set_state_safe(&new_state
, &state
);
1921 kbd_previous_mode_bit
= new_state
.mode_bit
;
1926 static ssize_t
kbd_led_als_enabled_show(struct device
*dev
,
1927 struct device_attribute
*attr
,
1930 struct kbd_state state
;
1931 bool enabled
= false;
1934 ret
= kbd_get_state(&state
);
1937 enabled
= kbd_is_als_mode_bit(state
.mode_bit
);
1939 return sprintf(buf
, "%d\n", enabled
? 1 : 0);
1942 static DEVICE_ATTR(als_enabled
, S_IRUGO
| S_IWUSR
,
1943 kbd_led_als_enabled_show
, kbd_led_als_enabled_store
);
1945 static ssize_t
kbd_led_als_setting_store(struct device
*dev
,
1946 struct device_attribute
*attr
,
1947 const char *buf
, size_t count
)
1949 struct kbd_state state
;
1950 struct kbd_state new_state
;
1954 ret
= kstrtou8(buf
, 10, &setting
);
1958 ret
= kbd_get_state(&state
);
1963 new_state
.als_setting
= setting
;
1965 ret
= kbd_set_state_safe(&new_state
, &state
);
1972 static ssize_t
kbd_led_als_setting_show(struct device
*dev
,
1973 struct device_attribute
*attr
,
1976 struct kbd_state state
;
1979 ret
= kbd_get_state(&state
);
1983 return sprintf(buf
, "%d\n", state
.als_setting
);
1986 static DEVICE_ATTR(als_setting
, S_IRUGO
| S_IWUSR
,
1987 kbd_led_als_setting_show
, kbd_led_als_setting_store
);
1989 static struct attribute
*kbd_led_attrs
[] = {
1990 &dev_attr_stop_timeout
.attr
,
1991 &dev_attr_start_triggers
.attr
,
1995 static const struct attribute_group kbd_led_group
= {
1996 .attrs
= kbd_led_attrs
,
1999 static struct attribute
*kbd_led_als_attrs
[] = {
2000 &dev_attr_als_enabled
.attr
,
2001 &dev_attr_als_setting
.attr
,
2005 static const struct attribute_group kbd_led_als_group
= {
2006 .attrs
= kbd_led_als_attrs
,
2009 static const struct attribute_group
*kbd_led_groups
[] = {
2015 static enum led_brightness
kbd_led_level_get(struct led_classdev
*led_cdev
)
2019 struct kbd_state state
;
2021 if (kbd_get_max_level()) {
2022 ret
= kbd_get_state(&state
);
2025 ret
= kbd_get_level(&state
);
2031 if (kbd_get_valid_token_counts()) {
2032 ret
= kbd_get_first_active_token_bit();
2035 for (num
= kbd_token_bits
; num
!= 0 && ret
> 0; --ret
)
2036 num
&= num
- 1; /* clear the first bit set */
2039 return ffs(num
) - 1;
2042 pr_warn("Keyboard brightness level control not supported\n");
2046 static void kbd_led_level_set(struct led_classdev
*led_cdev
,
2047 enum led_brightness value
)
2049 struct kbd_state state
;
2050 struct kbd_state new_state
;
2053 if (kbd_get_max_level()) {
2054 if (kbd_get_state(&state
))
2057 if (kbd_set_level(&new_state
, value
))
2059 kbd_set_state_safe(&new_state
, &state
);
2063 if (kbd_get_valid_token_counts()) {
2064 for (num
= kbd_token_bits
; num
!= 0 && value
> 0; --value
)
2065 num
&= num
- 1; /* clear the first bit set */
2068 kbd_set_token_bit(ffs(num
) - 1);
2072 pr_warn("Keyboard brightness level control not supported\n");
2075 static struct led_classdev kbd_led
= {
2076 .name
= "dell::kbd_backlight",
2077 .brightness_set
= kbd_led_level_set
,
2078 .brightness_get
= kbd_led_level_get
,
2079 .groups
= kbd_led_groups
,
2082 static int __init
kbd_led_init(struct device
*dev
)
2085 if (!kbd_led_present
)
2087 if (!kbd_als_supported
)
2088 kbd_led_groups
[1] = NULL
;
2089 kbd_led
.max_brightness
= kbd_get_max_level();
2090 if (!kbd_led
.max_brightness
) {
2091 kbd_led
.max_brightness
= kbd_get_valid_token_counts();
2092 if (kbd_led
.max_brightness
)
2093 kbd_led
.max_brightness
--;
2095 return led_classdev_register(dev
, &kbd_led
);
2098 static void brightness_set_exit(struct led_classdev
*led_cdev
,
2099 enum led_brightness value
)
2101 /* Don't change backlight level on exit */
2104 static void kbd_led_exit(void)
2106 if (!kbd_led_present
)
2108 kbd_led
.brightness_set
= brightness_set_exit
;
2109 led_classdev_unregister(&kbd_led
);
2112 static int __init
dell_init(void)
2114 int max_intensity
= 0;
2118 if (!dmi_check_system(dell_device_table
))
2122 /* find if this machine support other functions */
2123 dmi_check_system(dell_quirks
);
2125 dmi_walk(find_tokens
, NULL
);
2128 pr_info("Unable to find dmi tokens\n");
2132 ret
= platform_driver_register(&platform_driver
);
2134 goto fail_platform_driver
;
2135 platform_device
= platform_device_alloc("dell-laptop", -1);
2136 if (!platform_device
) {
2138 goto fail_platform_device1
;
2140 ret
= platform_device_add(platform_device
);
2142 goto fail_platform_device2
;
2145 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
2146 * is passed to SMI handler.
2148 buffer
= (void *)__get_free_page(GFP_KERNEL
| GFP_DMA32
);
2154 ret
= dell_setup_rfkill();
2157 pr_warn("Unable to setup rfkill\n");
2161 if (quirks
&& quirks
->touchpad_led
)
2162 touchpad_led_init(&platform_device
->dev
);
2164 kbd_led_init(&platform_device
->dev
);
2166 dell_laptop_dir
= debugfs_create_dir("dell_laptop", NULL
);
2167 if (dell_laptop_dir
!= NULL
)
2168 debugfs_create_file("rfkill", 0444, dell_laptop_dir
, NULL
,
2169 &dell_debugfs_fops
);
2171 if (acpi_video_get_backlight_type() != acpi_backlight_vendor
)
2174 token
= find_token_location(BRIGHTNESS_TOKEN
);
2177 buffer
->input
[0] = token
;
2178 dell_send_request(buffer
, 0, 2);
2179 if (buffer
->output
[0] == 0)
2180 max_intensity
= buffer
->output
[3];
2184 if (max_intensity
) {
2185 struct backlight_properties props
;
2186 memset(&props
, 0, sizeof(struct backlight_properties
));
2187 props
.type
= BACKLIGHT_PLATFORM
;
2188 props
.max_brightness
= max_intensity
;
2189 dell_backlight_device
= backlight_device_register("dell_backlight",
2190 &platform_device
->dev
,
2195 if (IS_ERR(dell_backlight_device
)) {
2196 ret
= PTR_ERR(dell_backlight_device
);
2197 dell_backlight_device
= NULL
;
2198 goto fail_backlight
;
2201 dell_backlight_device
->props
.brightness
=
2202 dell_get_intensity(dell_backlight_device
);
2203 backlight_update_status(dell_backlight_device
);
2209 dell_cleanup_rfkill();
2211 free_page((unsigned long)buffer
);
2213 platform_device_del(platform_device
);
2214 fail_platform_device2
:
2215 platform_device_put(platform_device
);
2216 fail_platform_device1
:
2217 platform_driver_unregister(&platform_driver
);
2218 fail_platform_driver
:
2223 static void __exit
dell_exit(void)
2225 debugfs_remove_recursive(dell_laptop_dir
);
2226 if (quirks
&& quirks
->touchpad_led
)
2227 touchpad_led_exit();
2229 backlight_device_unregister(dell_backlight_device
);
2230 dell_cleanup_rfkill();
2231 if (platform_device
) {
2232 platform_device_unregister(platform_device
);
2233 platform_driver_unregister(&platform_driver
);
2236 free_page((unsigned long)buffer
);
2239 /* dell-rbtn.c driver export functions which will not work correctly (and could
2240 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2241 * not problem when dell-rbtn.c is compiled as external module. When both files
2242 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2243 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2244 * This can be achieved by late_initcall() instead module_init().
2246 late_initcall(dell_init
);
2247 module_exit(dell_exit
);
2249 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2250 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2251 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2252 MODULE_DESCRIPTION("Dell laptop driver");
2253 MODULE_LICENSE("GPL");