xref: /linux/drivers/platform/x86/dell/dell-laptop.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for Dell laptop extras
4  *
5  *  Copyright (c) Red Hat <mjg@redhat.com>
6  *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
7  *  Copyright (c) 2014 Pali Rohár <pali@kernel.org>
8  *
9  *  Based on documentation in the libsmbios package:
10  *  Copyright (C) 2005-2014 Dell Inc.
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/backlight.h>
20 #include <linux/err.h>
21 #include <linux/dmi.h>
22 #include <linux/io.h>
23 #include <linux/rfkill.h>
24 #include <linux/power_supply.h>
25 #include <linux/acpi.h>
26 #include <linux/mm.h>
27 #include <linux/i8042.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
30 #include <acpi/video.h>
31 #include "dell-rbtn.h"
32 #include "dell-smbios.h"
33 
34 #include "dell-wmi-privacy.h"
35 
36 struct quirk_entry {
37 	bool touchpad_led;
38 	bool kbd_led_not_present;
39 	bool kbd_led_levels_off_1;
40 	bool kbd_missing_ac_tag;
41 
42 	bool needs_kbd_timeouts;
43 	/*
44 	 * Ordered list of timeouts expressed in seconds.
45 	 * The list must end with -1
46 	 */
47 	int kbd_timeouts[];
48 };
49 
50 static struct quirk_entry *quirks;
51 
52 static struct quirk_entry quirk_dell_vostro_v130 = {
53 	.touchpad_led = true,
54 };
55 
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
57 {
58 	quirks = dmi->driver_data;
59 	return 1;
60 }
61 
62 /*
63  * These values come from Windows utility provided by Dell. If any other value
64  * is used then BIOS silently set timeout to 0 without any error message.
65  */
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 	.needs_kbd_timeouts = true,
68 	.kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
69 };
70 
71 static struct quirk_entry quirk_dell_xps13_9370 = {
72 	.kbd_missing_ac_tag = true,
73 };
74 
75 static struct quirk_entry quirk_dell_latitude_e6410 = {
76 	.kbd_led_levels_off_1 = true,
77 };
78 
79 static struct quirk_entry quirk_dell_inspiron_1012 = {
80 	.kbd_led_not_present = true,
81 };
82 
83 static struct quirk_entry quirk_dell_latitude_7520 = {
84 	.kbd_missing_ac_tag = true,
85 };
86 
87 static struct platform_driver platform_driver = {
88 	.driver = {
89 		.name = "dell-laptop",
90 	}
91 };
92 
93 static struct platform_device *platform_device;
94 static struct backlight_device *dell_backlight_device;
95 static struct rfkill *wifi_rfkill;
96 static struct rfkill *bluetooth_rfkill;
97 static struct rfkill *wwan_rfkill;
98 static bool force_rfkill;
99 static bool micmute_led_registered;
100 static bool mute_led_registered;
101 
102 module_param(force_rfkill, bool, 0444);
103 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
104 
105 static const struct dmi_system_id dell_device_table[] __initconst = {
106 	{
107 		.ident = "Dell laptop",
108 		.matches = {
109 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
110 			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
111 		},
112 	},
113 	{
114 		.matches = {
115 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
116 			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
117 		},
118 	},
119 	{
120 		.matches = {
121 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
122 			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
123 		},
124 	},
125 	{
126 		.matches = {
127 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 			DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
129 		},
130 	},
131 	{
132 		.matches = {
133 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
134 			DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
135 		},
136 	},
137 	{
138 		.matches = {
139 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
140 			DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
141 		},
142 	},
143 	{
144 		.ident = "Dell Computer Corporation",
145 		.matches = {
146 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
147 			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
148 		},
149 	},
150 	{ }
151 };
152 MODULE_DEVICE_TABLE(dmi, dell_device_table);
153 
154 static const struct dmi_system_id dell_quirks[] __initconst = {
155 	{
156 		.callback = dmi_matched,
157 		.ident = "Dell Vostro V130",
158 		.matches = {
159 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
160 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
161 		},
162 		.driver_data = &quirk_dell_vostro_v130,
163 	},
164 	{
165 		.callback = dmi_matched,
166 		.ident = "Dell Vostro V131",
167 		.matches = {
168 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
169 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
170 		},
171 		.driver_data = &quirk_dell_vostro_v130,
172 	},
173 	{
174 		.callback = dmi_matched,
175 		.ident = "Dell Vostro 3350",
176 		.matches = {
177 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
178 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
179 		},
180 		.driver_data = &quirk_dell_vostro_v130,
181 	},
182 	{
183 		.callback = dmi_matched,
184 		.ident = "Dell Vostro 3555",
185 		.matches = {
186 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
187 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
188 		},
189 		.driver_data = &quirk_dell_vostro_v130,
190 	},
191 	{
192 		.callback = dmi_matched,
193 		.ident = "Dell Inspiron N311z",
194 		.matches = {
195 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
196 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
197 		},
198 		.driver_data = &quirk_dell_vostro_v130,
199 	},
200 	{
201 		.callback = dmi_matched,
202 		.ident = "Dell Inspiron M5110",
203 		.matches = {
204 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
205 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
206 		},
207 		.driver_data = &quirk_dell_vostro_v130,
208 	},
209 	{
210 		.callback = dmi_matched,
211 		.ident = "Dell Vostro 3360",
212 		.matches = {
213 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
214 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
215 		},
216 		.driver_data = &quirk_dell_vostro_v130,
217 	},
218 	{
219 		.callback = dmi_matched,
220 		.ident = "Dell Vostro 3460",
221 		.matches = {
222 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
223 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
224 		},
225 		.driver_data = &quirk_dell_vostro_v130,
226 	},
227 	{
228 		.callback = dmi_matched,
229 		.ident = "Dell Vostro 3560",
230 		.matches = {
231 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
232 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
233 		},
234 		.driver_data = &quirk_dell_vostro_v130,
235 	},
236 	{
237 		.callback = dmi_matched,
238 		.ident = "Dell Vostro 3450",
239 		.matches = {
240 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
241 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
242 		},
243 		.driver_data = &quirk_dell_vostro_v130,
244 	},
245 	{
246 		.callback = dmi_matched,
247 		.ident = "Dell Inspiron 5420",
248 		.matches = {
249 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
251 		},
252 		.driver_data = &quirk_dell_vostro_v130,
253 	},
254 	{
255 		.callback = dmi_matched,
256 		.ident = "Dell Inspiron 5520",
257 		.matches = {
258 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
260 		},
261 		.driver_data = &quirk_dell_vostro_v130,
262 	},
263 	{
264 		.callback = dmi_matched,
265 		.ident = "Dell Inspiron 5720",
266 		.matches = {
267 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
269 		},
270 		.driver_data = &quirk_dell_vostro_v130,
271 	},
272 	{
273 		.callback = dmi_matched,
274 		.ident = "Dell Inspiron 7420",
275 		.matches = {
276 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
277 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
278 		},
279 		.driver_data = &quirk_dell_vostro_v130,
280 	},
281 	{
282 		.callback = dmi_matched,
283 		.ident = "Dell Inspiron 7520",
284 		.matches = {
285 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
286 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
287 		},
288 		.driver_data = &quirk_dell_vostro_v130,
289 	},
290 	{
291 		.callback = dmi_matched,
292 		.ident = "Dell Inspiron 7720",
293 		.matches = {
294 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
295 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
296 		},
297 		.driver_data = &quirk_dell_vostro_v130,
298 	},
299 	{
300 		.callback = dmi_matched,
301 		.ident = "Dell XPS13 9333",
302 		.matches = {
303 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
304 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
305 		},
306 		.driver_data = &quirk_dell_xps13_9333,
307 	},
308 	{
309 		.callback = dmi_matched,
310 		.ident = "Dell XPS 13 9370",
311 		.matches = {
312 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
313 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
314 		},
315 		.driver_data = &quirk_dell_xps13_9370,
316 	},
317 	{
318 		.callback = dmi_matched,
319 		.ident = "Dell Latitude E6410",
320 		.matches = {
321 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
322 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
323 		},
324 		.driver_data = &quirk_dell_latitude_e6410,
325 	},
326 	{
327 		.callback = dmi_matched,
328 		.ident = "Dell Inspiron 1012",
329 		.matches = {
330 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
331 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
332 		},
333 		.driver_data = &quirk_dell_inspiron_1012,
334 	},
335 	{
336 		.callback = dmi_matched,
337 		.ident = "Dell Inspiron 1018",
338 		.matches = {
339 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
340 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1018"),
341 		},
342 		.driver_data = &quirk_dell_inspiron_1012,
343 	},
344 	{
345 		.callback = dmi_matched,
346 		.ident = "Dell Latitude 7520",
347 		.matches = {
348 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
349 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude 7520"),
350 		},
351 		.driver_data = &quirk_dell_latitude_7520,
352 	},
353 	{ }
354 };
355 
356 /*
357  * Derived from information in smbios-wireless-ctl:
358  *
359  * cbSelect 17, Value 11
360  *
361  * Return Wireless Info
362  * cbArg1, byte0 = 0x00
363  *
364  *     cbRes1 Standard return codes (0, -1, -2)
365  *     cbRes2 Info bit flags:
366  *
367  *     0 Hardware switch supported (1)
368  *     1 WiFi locator supported (1)
369  *     2 WLAN supported (1)
370  *     3 Bluetooth (BT) supported (1)
371  *     4 WWAN supported (1)
372  *     5 Wireless KBD supported (1)
373  *     6 Uw b supported (1)
374  *     7 WiGig supported (1)
375  *     8 WLAN installed (1)
376  *     9 BT installed (1)
377  *     10 WWAN installed (1)
378  *     11 Uw b installed (1)
379  *     12 WiGig installed (1)
380  *     13-15 Reserved (0)
381  *     16 Hardware (HW) switch is On (1)
382  *     17 WLAN disabled (1)
383  *     18 BT disabled (1)
384  *     19 WWAN disabled (1)
385  *     20 Uw b disabled (1)
386  *     21 WiGig disabled (1)
387  *     20-31 Reserved (0)
388  *
389  *     cbRes3 NVRAM size in bytes
390  *     cbRes4, byte 0 NVRAM format version number
391  *
392  *
393  * Set QuickSet Radio Disable Flag
394  *     cbArg1, byte0 = 0x01
395  *     cbArg1, byte1
396  *     Radio ID     value:
397  *     0        Radio Status
398  *     1        WLAN ID
399  *     2        BT ID
400  *     3        WWAN ID
401  *     4        UWB ID
402  *     5        WIGIG ID
403  *     cbArg1, byte2    Flag bits:
404  *             0 QuickSet disables radio (1)
405  *             1-7 Reserved (0)
406  *
407  *     cbRes1    Standard return codes (0, -1, -2)
408  *     cbRes2    QuickSet (QS) radio disable bit map:
409  *     0 QS disables WLAN
410  *     1 QS disables BT
411  *     2 QS disables WWAN
412  *     3 QS disables UWB
413  *     4 QS disables WIGIG
414  *     5-31 Reserved (0)
415  *
416  * Wireless Switch Configuration
417  *     cbArg1, byte0 = 0x02
418  *
419  *     cbArg1, byte1
420  *     Subcommand:
421  *     0 Get config
422  *     1 Set config
423  *     2 Set WiFi locator enable/disable
424  *     cbArg1,byte2
425  *     Switch settings (if byte 1==1):
426  *     0 WLAN sw itch control (1)
427  *     1 BT sw itch control (1)
428  *     2 WWAN sw itch control (1)
429  *     3 UWB sw itch control (1)
430  *     4 WiGig sw itch control (1)
431  *     5-7 Reserved (0)
432  *    cbArg1, byte2 Enable bits (if byte 1==2):
433  *     0 Enable WiFi locator (1)
434  *
435  *    cbRes1     Standard return codes (0, -1, -2)
436  *    cbRes2 QuickSet radio disable bit map:
437  *     0 WLAN controlled by sw itch (1)
438  *     1 BT controlled by sw itch (1)
439  *     2 WWAN controlled by sw itch (1)
440  *     3 UWB controlled by sw itch (1)
441  *     4 WiGig controlled by sw itch (1)
442  *     5-6 Reserved (0)
443  *     7 Wireless sw itch config locked (1)
444  *     8 WiFi locator enabled (1)
445  *     9-14 Reserved (0)
446  *     15 WiFi locator setting locked (1)
447  *     16-31 Reserved (0)
448  *
449  * Read Local Config Data (LCD)
450  *     cbArg1, byte0 = 0x10
451  *     cbArg1, byte1 NVRAM index low byte
452  *     cbArg1, byte2 NVRAM index high byte
453  *     cbRes1 Standard return codes (0, -1, -2)
454  *     cbRes2 4 bytes read from LCD[index]
455  *     cbRes3 4 bytes read from LCD[index+4]
456  *     cbRes4 4 bytes read from LCD[index+8]
457  *
458  * Write Local Config Data (LCD)
459  *     cbArg1, byte0 = 0x11
460  *     cbArg1, byte1 NVRAM index low byte
461  *     cbArg1, byte2 NVRAM index high byte
462  *     cbArg2 4 bytes to w rite at LCD[index]
463  *     cbArg3 4 bytes to w rite at LCD[index+4]
464  *     cbArg4 4 bytes to w rite at LCD[index+8]
465  *     cbRes1 Standard return codes (0, -1, -2)
466  *
467  * Populate Local Config Data from NVRAM
468  *     cbArg1, byte0 = 0x12
469  *     cbRes1 Standard return codes (0, -1, -2)
470  *
471  * Commit Local Config Data to NVRAM
472  *     cbArg1, byte0 = 0x13
473  *     cbRes1 Standard return codes (0, -1, -2)
474  */
475 
476 static int dell_rfkill_set(void *data, bool blocked)
477 {
478 	int disable = blocked ? 1 : 0;
479 	unsigned long radio = (unsigned long)data;
480 	int hwswitch_bit = (unsigned long)data - 1;
481 	struct calling_interface_buffer buffer;
482 	int hwswitch;
483 	int status;
484 	int ret;
485 
486 	dell_fill_request(&buffer, 0, 0, 0, 0);
487 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
488 	if (ret)
489 		return ret;
490 	status = buffer.output[1];
491 
492 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
493 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
494 	if (ret)
495 		return ret;
496 	hwswitch = buffer.output[1];
497 
498 	/* If the hardware switch controls this radio, and the hardware
499 	   switch is disabled, always disable the radio */
500 	if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
501 	    (status & BIT(0)) && !(status & BIT(16)))
502 		disable = 1;
503 
504 	dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
505 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
506 	return ret;
507 }
508 
509 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
510 					int status)
511 {
512 	if (status & BIT(0)) {
513 		/* Has hw-switch, sync sw_state to BIOS */
514 		struct calling_interface_buffer buffer;
515 		int block = rfkill_blocked(rfkill);
516 		dell_fill_request(&buffer,
517 				   1 | (radio << 8) | (block << 16), 0, 0, 0);
518 		dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
519 	} else {
520 		/* No hw-switch, sync BIOS state to sw_state */
521 		rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
522 	}
523 }
524 
525 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
526 					int status, int hwswitch)
527 {
528 	if (hwswitch & (BIT(radio - 1)))
529 		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
530 }
531 
532 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
533 {
534 	int radio = ((unsigned long)data & 0xF);
535 	struct calling_interface_buffer buffer;
536 	int hwswitch;
537 	int status;
538 	int ret;
539 
540 	dell_fill_request(&buffer, 0, 0, 0, 0);
541 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
542 	status = buffer.output[1];
543 
544 	if (ret != 0 || !(status & BIT(0))) {
545 		return;
546 	}
547 
548 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
549 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
550 	hwswitch = buffer.output[1];
551 
552 	if (ret != 0)
553 		return;
554 
555 	dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
556 }
557 
558 static const struct rfkill_ops dell_rfkill_ops = {
559 	.set_block = dell_rfkill_set,
560 	.query = dell_rfkill_query,
561 };
562 
563 static struct dentry *dell_laptop_dir;
564 
565 static int dell_debugfs_show(struct seq_file *s, void *data)
566 {
567 	struct calling_interface_buffer buffer;
568 	int hwswitch_state;
569 	int hwswitch_ret;
570 	int status;
571 	int ret;
572 
573 	dell_fill_request(&buffer, 0, 0, 0, 0);
574 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
575 	if (ret)
576 		return ret;
577 	status = buffer.output[1];
578 
579 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
580 	hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
581 	if (hwswitch_ret)
582 		return hwswitch_ret;
583 	hwswitch_state = buffer.output[1];
584 
585 	seq_printf(s, "return:\t%d\n", ret);
586 	seq_printf(s, "status:\t0x%X\n", status);
587 	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
588 		   status & BIT(0));
589 	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
590 		  (status & BIT(1)) >> 1);
591 	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
592 		  (status & BIT(2)) >> 2);
593 	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
594 		  (status & BIT(3)) >> 3);
595 	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
596 		  (status & BIT(4)) >> 4);
597 	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
598 		  (status & BIT(5)) >> 5);
599 	seq_printf(s, "Bit 6 : UWB supported:               %lu\n",
600 		  (status & BIT(6)) >> 6);
601 	seq_printf(s, "Bit 7 : WiGig supported:             %lu\n",
602 		  (status & BIT(7)) >> 7);
603 	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
604 		  (status & BIT(8)) >> 8);
605 	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
606 		  (status & BIT(9)) >> 9);
607 	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
608 		  (status & BIT(10)) >> 10);
609 	seq_printf(s, "Bit 11: UWB installed:               %lu\n",
610 		  (status & BIT(11)) >> 11);
611 	seq_printf(s, "Bit 12: WiGig installed:             %lu\n",
612 		  (status & BIT(12)) >> 12);
613 
614 	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
615 		  (status & BIT(16)) >> 16);
616 	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
617 		  (status & BIT(17)) >> 17);
618 	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
619 		  (status & BIT(18)) >> 18);
620 	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
621 		  (status & BIT(19)) >> 19);
622 	seq_printf(s, "Bit 20: UWB is blocked:              %lu\n",
623 		  (status & BIT(20)) >> 20);
624 	seq_printf(s, "Bit 21: WiGig is blocked:            %lu\n",
625 		  (status & BIT(21)) >> 21);
626 
627 	seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
628 	seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
629 	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
630 		   hwswitch_state & BIT(0));
631 	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
632 		   (hwswitch_state & BIT(1)) >> 1);
633 	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
634 		   (hwswitch_state & BIT(2)) >> 2);
635 	seq_printf(s, "Bit 3 : UWB controlled by switch:       %lu\n",
636 		   (hwswitch_state & BIT(3)) >> 3);
637 	seq_printf(s, "Bit 4 : WiGig controlled by switch:     %lu\n",
638 		   (hwswitch_state & BIT(4)) >> 4);
639 	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
640 		   (hwswitch_state & BIT(7)) >> 7);
641 	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
642 		   (hwswitch_state & BIT(8)) >> 8);
643 	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
644 		   (hwswitch_state & BIT(15)) >> 15);
645 
646 	return 0;
647 }
648 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
649 
650 static void dell_update_rfkill(struct work_struct *ignored)
651 {
652 	struct calling_interface_buffer buffer;
653 	int hwswitch = 0;
654 	int status;
655 	int ret;
656 
657 	dell_fill_request(&buffer, 0, 0, 0, 0);
658 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
659 	status = buffer.output[1];
660 
661 	if (ret != 0)
662 		return;
663 
664 	dell_fill_request(&buffer, 0x2, 0, 0, 0);
665 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
666 
667 	if (ret == 0 && (status & BIT(0)))
668 		hwswitch = buffer.output[1];
669 
670 	if (wifi_rfkill) {
671 		dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
672 		dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
673 	}
674 	if (bluetooth_rfkill) {
675 		dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
676 					    hwswitch);
677 		dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
678 	}
679 	if (wwan_rfkill) {
680 		dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
681 		dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
682 	}
683 }
684 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
685 
686 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
687 			      struct serio *port)
688 {
689 	static bool extended;
690 
691 	if (str & I8042_STR_AUXDATA)
692 		return false;
693 
694 	if (unlikely(data == 0xe0)) {
695 		extended = true;
696 		return false;
697 	} else if (unlikely(extended)) {
698 		switch (data) {
699 		case 0x8:
700 			schedule_delayed_work(&dell_rfkill_work,
701 					      round_jiffies_relative(HZ / 4));
702 			break;
703 		}
704 		extended = false;
705 	}
706 
707 	return false;
708 }
709 
710 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
711 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
712 
713 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
714 					  unsigned long action, void *data)
715 {
716 	schedule_delayed_work(&dell_rfkill_work, 0);
717 	return NOTIFY_OK;
718 }
719 
720 static struct notifier_block dell_laptop_rbtn_notifier = {
721 	.notifier_call = dell_laptop_rbtn_notifier_call,
722 };
723 
724 static int __init dell_setup_rfkill(void)
725 {
726 	struct calling_interface_buffer buffer;
727 	int status, ret, whitelisted;
728 	const char *product;
729 
730 	/*
731 	 * rfkill support causes trouble on various models, mostly Inspirons.
732 	 * So we whitelist certain series, and don't support rfkill on others.
733 	 */
734 	whitelisted = 0;
735 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
736 	if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
737 			 strncmp(product, "Precision", 9) == 0))
738 		whitelisted = 1;
739 	if (!force_rfkill && !whitelisted)
740 		return 0;
741 
742 	dell_fill_request(&buffer, 0, 0, 0, 0);
743 	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
744 	status = buffer.output[1];
745 
746 	/* dell wireless info smbios call is not supported */
747 	if (ret != 0)
748 		return 0;
749 
750 	/* rfkill is only tested on laptops with a hwswitch */
751 	if (!(status & BIT(0)) && !force_rfkill)
752 		return 0;
753 
754 	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
755 		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
756 					   RFKILL_TYPE_WLAN,
757 					   &dell_rfkill_ops, (void *) 1);
758 		if (!wifi_rfkill) {
759 			ret = -ENOMEM;
760 			goto err_wifi;
761 		}
762 		ret = rfkill_register(wifi_rfkill);
763 		if (ret)
764 			goto err_wifi;
765 	}
766 
767 	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
768 		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
769 						&platform_device->dev,
770 						RFKILL_TYPE_BLUETOOTH,
771 						&dell_rfkill_ops, (void *) 2);
772 		if (!bluetooth_rfkill) {
773 			ret = -ENOMEM;
774 			goto err_bluetooth;
775 		}
776 		ret = rfkill_register(bluetooth_rfkill);
777 		if (ret)
778 			goto err_bluetooth;
779 	}
780 
781 	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
782 		wwan_rfkill = rfkill_alloc("dell-wwan",
783 					   &platform_device->dev,
784 					   RFKILL_TYPE_WWAN,
785 					   &dell_rfkill_ops, (void *) 3);
786 		if (!wwan_rfkill) {
787 			ret = -ENOMEM;
788 			goto err_wwan;
789 		}
790 		ret = rfkill_register(wwan_rfkill);
791 		if (ret)
792 			goto err_wwan;
793 	}
794 
795 	/*
796 	 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
797 	 * which can receive events from HW slider switch.
798 	 *
799 	 * Dell SMBIOS on whitelisted models supports controlling radio devices
800 	 * but does not support receiving HW button switch events. We can use
801 	 * i8042 filter hook function to receive keyboard data and handle
802 	 * keycode for HW button.
803 	 *
804 	 * So if it is possible we will use Dell Airplane Mode Switch ACPI
805 	 * driver for receiving HW events and Dell SMBIOS for setting rfkill
806 	 * states. If ACPI driver or device is not available we will fallback to
807 	 * i8042 filter hook function.
808 	 *
809 	 * To prevent duplicate rfkill devices which control and do same thing,
810 	 * dell-rbtn driver will automatically remove its own rfkill devices
811 	 * once function dell_rbtn_notifier_register() is called.
812 	 */
813 
814 	dell_rbtn_notifier_register_func =
815 		symbol_request(dell_rbtn_notifier_register);
816 	if (dell_rbtn_notifier_register_func) {
817 		dell_rbtn_notifier_unregister_func =
818 			symbol_request(dell_rbtn_notifier_unregister);
819 		if (!dell_rbtn_notifier_unregister_func) {
820 			symbol_put(dell_rbtn_notifier_register);
821 			dell_rbtn_notifier_register_func = NULL;
822 		}
823 	}
824 
825 	if (dell_rbtn_notifier_register_func) {
826 		ret = dell_rbtn_notifier_register_func(
827 			&dell_laptop_rbtn_notifier);
828 		symbol_put(dell_rbtn_notifier_register);
829 		dell_rbtn_notifier_register_func = NULL;
830 		if (ret != 0) {
831 			symbol_put(dell_rbtn_notifier_unregister);
832 			dell_rbtn_notifier_unregister_func = NULL;
833 		}
834 	} else {
835 		pr_info("Symbols from dell-rbtn acpi driver are not available\n");
836 		ret = -ENODEV;
837 	}
838 
839 	if (ret == 0) {
840 		pr_info("Using dell-rbtn acpi driver for receiving events\n");
841 	} else if (ret != -ENODEV) {
842 		pr_warn("Unable to register dell rbtn notifier\n");
843 		goto err_filter;
844 	} else {
845 		ret = i8042_install_filter(dell_laptop_i8042_filter);
846 		if (ret) {
847 			pr_warn("Unable to install key filter\n");
848 			goto err_filter;
849 		}
850 		pr_info("Using i8042 filter function for receiving events\n");
851 	}
852 
853 	return 0;
854 err_filter:
855 	if (wwan_rfkill)
856 		rfkill_unregister(wwan_rfkill);
857 err_wwan:
858 	rfkill_destroy(wwan_rfkill);
859 	if (bluetooth_rfkill)
860 		rfkill_unregister(bluetooth_rfkill);
861 err_bluetooth:
862 	rfkill_destroy(bluetooth_rfkill);
863 	if (wifi_rfkill)
864 		rfkill_unregister(wifi_rfkill);
865 err_wifi:
866 	rfkill_destroy(wifi_rfkill);
867 
868 	return ret;
869 }
870 
871 static void dell_cleanup_rfkill(void)
872 {
873 	if (dell_rbtn_notifier_unregister_func) {
874 		dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
875 		symbol_put(dell_rbtn_notifier_unregister);
876 		dell_rbtn_notifier_unregister_func = NULL;
877 	} else {
878 		i8042_remove_filter(dell_laptop_i8042_filter);
879 	}
880 	cancel_delayed_work_sync(&dell_rfkill_work);
881 	if (wifi_rfkill) {
882 		rfkill_unregister(wifi_rfkill);
883 		rfkill_destroy(wifi_rfkill);
884 	}
885 	if (bluetooth_rfkill) {
886 		rfkill_unregister(bluetooth_rfkill);
887 		rfkill_destroy(bluetooth_rfkill);
888 	}
889 	if (wwan_rfkill) {
890 		rfkill_unregister(wwan_rfkill);
891 		rfkill_destroy(wwan_rfkill);
892 	}
893 }
894 
895 static int dell_send_intensity(struct backlight_device *bd)
896 {
897 	struct calling_interface_buffer buffer;
898 	struct calling_interface_token *token;
899 	int ret;
900 
901 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
902 	if (!token)
903 		return -ENODEV;
904 
905 	dell_fill_request(&buffer,
906 			   token->location, bd->props.brightness, 0, 0);
907 	if (power_supply_is_system_supplied() > 0)
908 		ret = dell_send_request(&buffer,
909 					CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
910 	else
911 		ret = dell_send_request(&buffer,
912 					CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
913 
914 	return ret;
915 }
916 
917 static int dell_get_intensity(struct backlight_device *bd)
918 {
919 	struct calling_interface_buffer buffer;
920 	struct calling_interface_token *token;
921 	int ret;
922 
923 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
924 	if (!token)
925 		return -ENODEV;
926 
927 	dell_fill_request(&buffer, token->location, 0, 0, 0);
928 	if (power_supply_is_system_supplied() > 0)
929 		ret = dell_send_request(&buffer,
930 					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
931 	else
932 		ret = dell_send_request(&buffer,
933 					CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
934 
935 	if (ret == 0)
936 		ret = buffer.output[1];
937 
938 	return ret;
939 }
940 
941 static const struct backlight_ops dell_ops = {
942 	.get_brightness = dell_get_intensity,
943 	.update_status  = dell_send_intensity,
944 };
945 
946 static void touchpad_led_on(void)
947 {
948 	int command = 0x97;
949 	char data = 1;
950 	i8042_command(&data, command | 1 << 12);
951 }
952 
953 static void touchpad_led_off(void)
954 {
955 	int command = 0x97;
956 	char data = 2;
957 	i8042_command(&data, command | 1 << 12);
958 }
959 
960 static void touchpad_led_set(struct led_classdev *led_cdev,
961 	enum led_brightness value)
962 {
963 	if (value > 0)
964 		touchpad_led_on();
965 	else
966 		touchpad_led_off();
967 }
968 
969 static struct led_classdev touchpad_led = {
970 	.name = "dell-laptop::touchpad",
971 	.brightness_set = touchpad_led_set,
972 	.flags = LED_CORE_SUSPENDRESUME,
973 };
974 
975 static int __init touchpad_led_init(struct device *dev)
976 {
977 	return led_classdev_register(dev, &touchpad_led);
978 }
979 
980 static void touchpad_led_exit(void)
981 {
982 	led_classdev_unregister(&touchpad_led);
983 }
984 
985 /*
986  * Derived from information in smbios-keyboard-ctl:
987  *
988  * cbClass 4
989  * cbSelect 11
990  * Keyboard illumination
991  * cbArg1 determines the function to be performed
992  *
993  * cbArg1 0x0 = Get Feature Information
994  *  cbRES1         Standard return codes (0, -1, -2)
995  *  cbRES2, word0  Bitmap of user-selectable modes
996  *     bit 0     Always off (All systems)
997  *     bit 1     Always on (Travis ATG, Siberia)
998  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
999  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1000  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1001  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1002  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1003  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1004  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1005  *     bits 9-15 Reserved for future use
1006  *  cbRES2, byte2  Reserved for future use
1007  *  cbRES2, byte3  Keyboard illumination type
1008  *     0         Reserved
1009  *     1         Tasklight
1010  *     2         Backlight
1011  *     3-255     Reserved for future use
1012  *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
1013  *     bit 0     Any keystroke
1014  *     bit 1     Touchpad activity
1015  *     bit 2     Pointing stick
1016  *     bit 3     Any mouse
1017  *     bits 4-7  Reserved for future use
1018  *  cbRES3, byte1  Supported timeout unit bitmap
1019  *     bit 0     Seconds
1020  *     bit 1     Minutes
1021  *     bit 2     Hours
1022  *     bit 3     Days
1023  *     bits 4-7  Reserved for future use
1024  *  cbRES3, byte2  Number of keyboard light brightness levels
1025  *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
1026  *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
1027  *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
1028  *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
1029  *
1030  * cbArg1 0x1 = Get Current State
1031  *  cbRES1         Standard return codes (0, -1, -2)
1032  *  cbRES2, word0  Bitmap of current mode state
1033  *     bit 0     Always off (All systems)
1034  *     bit 1     Always on (Travis ATG, Siberia)
1035  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1036  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1037  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1038  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1039  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1040  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1041  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1042  *     bits 9-15 Reserved for future use
1043  *     Note: Only One bit can be set
1044  *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
1045  *     bit 0     Any keystroke
1046  *     bit 1     Touchpad activity
1047  *     bit 2     Pointing stick
1048  *     bit 3     Any mouse
1049  *     bits 4-7  Reserved for future use
1050  *  cbRES2, byte3  Current Timeout on battery
1051  *     bits 7:6  Timeout units indicator:
1052  *     00b       Seconds
1053  *     01b       Minutes
1054  *     10b       Hours
1055  *     11b       Days
1056  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1057  *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1058  *     are set upon return from the [Get feature information] call.
1059  *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
1060  *  cbRES3, byte1  Current ALS reading
1061  *  cbRES3, byte2  Current keyboard light level.
1062  *  cbRES3, byte3  Current timeout on AC Power
1063  *     bits 7:6  Timeout units indicator:
1064  *     00b       Seconds
1065  *     01b       Minutes
1066  *     10b       Hours
1067  *     11b       Days
1068  *     Bits 5:0  Timeout value (0-63) in sec/min/hr/day
1069  *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1070  *     are set upon return from the upon return from the [Get Feature information] call.
1071  *
1072  * cbArg1 0x2 = Set New State
1073  *  cbRES1         Standard return codes (0, -1, -2)
1074  *  cbArg2, word0  Bitmap of current mode state
1075  *     bit 0     Always off (All systems)
1076  *     bit 1     Always on (Travis ATG, Siberia)
1077  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1078  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1079  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1080  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1081  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1082  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1083  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1084  *     bits 9-15 Reserved for future use
1085  *     Note: Only One bit can be set
1086  *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
1087  *                 keyboard to turn off automatically.
1088  *     bit 0     Any keystroke
1089  *     bit 1     Touchpad activity
1090  *     bit 2     Pointing stick
1091  *     bit 3     Any mouse
1092  *     bits 4-7  Reserved for future use
1093  *  cbArg2, byte3  Desired Timeout on battery
1094  *     bits 7:6  Timeout units indicator:
1095  *     00b       Seconds
1096  *     01b       Minutes
1097  *     10b       Hours
1098  *     11b       Days
1099  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1100  *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
1101  *  cbArg3, byte2  Desired keyboard light level.
1102  *  cbArg3, byte3  Desired Timeout on AC power
1103  *     bits 7:6  Timeout units indicator:
1104  *     00b       Seconds
1105  *     01b       Minutes
1106  *     10b       Hours
1107  *     11b       Days
1108  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1109  */
1110 
1111 
1112 enum kbd_timeout_unit {
1113 	KBD_TIMEOUT_SECONDS = 0,
1114 	KBD_TIMEOUT_MINUTES,
1115 	KBD_TIMEOUT_HOURS,
1116 	KBD_TIMEOUT_DAYS,
1117 };
1118 
1119 enum kbd_mode_bit {
1120 	KBD_MODE_BIT_OFF = 0,
1121 	KBD_MODE_BIT_ON,
1122 	KBD_MODE_BIT_ALS,
1123 	KBD_MODE_BIT_TRIGGER_ALS,
1124 	KBD_MODE_BIT_TRIGGER,
1125 	KBD_MODE_BIT_TRIGGER_25,
1126 	KBD_MODE_BIT_TRIGGER_50,
1127 	KBD_MODE_BIT_TRIGGER_75,
1128 	KBD_MODE_BIT_TRIGGER_100,
1129 };
1130 
1131 #define kbd_is_als_mode_bit(bit) \
1132 	((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1133 #define kbd_is_trigger_mode_bit(bit) \
1134 	((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1135 #define kbd_is_level_mode_bit(bit) \
1136 	((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1137 
1138 struct kbd_info {
1139 	u16 modes;
1140 	u8 type;
1141 	u8 triggers;
1142 	u8 levels;
1143 	u8 seconds;
1144 	u8 minutes;
1145 	u8 hours;
1146 	u8 days;
1147 };
1148 
1149 struct kbd_state {
1150 	u8 mode_bit;
1151 	u8 triggers;
1152 	u8 timeout_value;
1153 	u8 timeout_unit;
1154 	u8 timeout_value_ac;
1155 	u8 timeout_unit_ac;
1156 	u8 als_setting;
1157 	u8 als_value;
1158 	u8 level;
1159 };
1160 
1161 static const int kbd_tokens[] = {
1162 	KBD_LED_OFF_TOKEN,
1163 	KBD_LED_AUTO_25_TOKEN,
1164 	KBD_LED_AUTO_50_TOKEN,
1165 	KBD_LED_AUTO_75_TOKEN,
1166 	KBD_LED_AUTO_100_TOKEN,
1167 	KBD_LED_ON_TOKEN,
1168 };
1169 
1170 static u16 kbd_token_bits;
1171 
1172 static struct kbd_info kbd_info;
1173 static bool kbd_als_supported;
1174 static bool kbd_triggers_supported;
1175 static bool kbd_timeout_ac_supported;
1176 
1177 static u8 kbd_mode_levels[16];
1178 static int kbd_mode_levels_count;
1179 
1180 static u8 kbd_previous_level;
1181 static u8 kbd_previous_mode_bit;
1182 
1183 static bool kbd_led_present;
1184 static DEFINE_MUTEX(kbd_led_mutex);
1185 static enum led_brightness kbd_led_level;
1186 
1187 /*
1188  * NOTE: there are three ways to set the keyboard backlight level.
1189  * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1190  * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1191  * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1192  *
1193  * There are laptops which support only one of these methods. If we want to
1194  * support as many machines as possible we need to implement all three methods.
1195  * The first two methods use the kbd_state structure. The third uses SMBIOS
1196  * tokens. If kbd_info.levels == 0, the machine does not support setting the
1197  * keyboard backlight level via kbd_state.level.
1198  */
1199 
1200 static int kbd_get_info(struct kbd_info *info)
1201 {
1202 	struct calling_interface_buffer buffer;
1203 	u8 units;
1204 	int ret;
1205 
1206 	dell_fill_request(&buffer, 0, 0, 0, 0);
1207 	ret = dell_send_request(&buffer,
1208 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1209 	if (ret)
1210 		return ret;
1211 
1212 	info->modes = buffer.output[1] & 0xFFFF;
1213 	info->type = (buffer.output[1] >> 24) & 0xFF;
1214 	info->triggers = buffer.output[2] & 0xFF;
1215 	units = (buffer.output[2] >> 8) & 0xFF;
1216 	info->levels = (buffer.output[2] >> 16) & 0xFF;
1217 
1218 	if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1219 		info->levels--;
1220 
1221 	if (units & BIT(0))
1222 		info->seconds = (buffer.output[3] >> 0) & 0xFF;
1223 	if (units & BIT(1))
1224 		info->minutes = (buffer.output[3] >> 8) & 0xFF;
1225 	if (units & BIT(2))
1226 		info->hours = (buffer.output[3] >> 16) & 0xFF;
1227 	if (units & BIT(3))
1228 		info->days = (buffer.output[3] >> 24) & 0xFF;
1229 
1230 	return ret;
1231 }
1232 
1233 static unsigned int kbd_get_max_level(void)
1234 {
1235 	if (kbd_info.levels != 0)
1236 		return kbd_info.levels;
1237 	if (kbd_mode_levels_count > 0)
1238 		return kbd_mode_levels_count - 1;
1239 	return 0;
1240 }
1241 
1242 static int kbd_get_level(struct kbd_state *state)
1243 {
1244 	int i;
1245 
1246 	if (kbd_info.levels != 0)
1247 		return state->level;
1248 
1249 	if (kbd_mode_levels_count > 0) {
1250 		for (i = 0; i < kbd_mode_levels_count; ++i)
1251 			if (kbd_mode_levels[i] == state->mode_bit)
1252 				return i;
1253 		return 0;
1254 	}
1255 
1256 	return -EINVAL;
1257 }
1258 
1259 static int kbd_set_level(struct kbd_state *state, u8 level)
1260 {
1261 	if (kbd_info.levels != 0) {
1262 		if (level != 0)
1263 			kbd_previous_level = level;
1264 		if (state->level == level)
1265 			return 0;
1266 		state->level = level;
1267 		if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1268 			state->mode_bit = kbd_previous_mode_bit;
1269 		else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1270 			kbd_previous_mode_bit = state->mode_bit;
1271 			state->mode_bit = KBD_MODE_BIT_OFF;
1272 		}
1273 		return 0;
1274 	}
1275 
1276 	if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1277 		if (level != 0)
1278 			kbd_previous_level = level;
1279 		state->mode_bit = kbd_mode_levels[level];
1280 		return 0;
1281 	}
1282 
1283 	return -EINVAL;
1284 }
1285 
1286 static int kbd_get_state(struct kbd_state *state)
1287 {
1288 	struct calling_interface_buffer buffer;
1289 	int ret;
1290 
1291 	dell_fill_request(&buffer, 0x1, 0, 0, 0);
1292 	ret = dell_send_request(&buffer,
1293 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1294 	if (ret)
1295 		return ret;
1296 
1297 	state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1298 	if (state->mode_bit != 0)
1299 		state->mode_bit--;
1300 
1301 	state->triggers = (buffer.output[1] >> 16) & 0xFF;
1302 	state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1303 	state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1304 	state->als_setting = buffer.output[2] & 0xFF;
1305 	state->als_value = (buffer.output[2] >> 8) & 0xFF;
1306 	state->level = (buffer.output[2] >> 16) & 0xFF;
1307 	state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1308 	state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1309 
1310 	return ret;
1311 }
1312 
1313 static int kbd_set_state(struct kbd_state *state)
1314 {
1315 	struct calling_interface_buffer buffer;
1316 	int ret;
1317 	u32 input1;
1318 	u32 input2;
1319 
1320 	input1 = BIT(state->mode_bit) & 0xFFFF;
1321 	input1 |= (state->triggers & 0xFF) << 16;
1322 	input1 |= (state->timeout_value & 0x3F) << 24;
1323 	input1 |= (state->timeout_unit & 0x3) << 30;
1324 	input2 = state->als_setting & 0xFF;
1325 	input2 |= (state->level & 0xFF) << 16;
1326 	input2 |= (state->timeout_value_ac & 0x3F) << 24;
1327 	input2 |= (state->timeout_unit_ac & 0x3) << 30;
1328 	dell_fill_request(&buffer, 0x2, input1, input2, 0);
1329 	ret = dell_send_request(&buffer,
1330 				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1331 
1332 	return ret;
1333 }
1334 
1335 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1336 {
1337 	int ret;
1338 
1339 	ret = kbd_set_state(state);
1340 	if (ret == 0)
1341 		return 0;
1342 
1343 	/*
1344 	 * When setting the new state fails,try to restore the previous one.
1345 	 * This is needed on some machines where BIOS sets a default state when
1346 	 * setting a new state fails. This default state could be all off.
1347 	 */
1348 
1349 	if (kbd_set_state(old))
1350 		pr_err("Setting old previous keyboard state failed\n");
1351 
1352 	return ret;
1353 }
1354 
1355 static int kbd_set_token_bit(u8 bit)
1356 {
1357 	struct calling_interface_buffer buffer;
1358 	struct calling_interface_token *token;
1359 	int ret;
1360 
1361 	if (bit >= ARRAY_SIZE(kbd_tokens))
1362 		return -EINVAL;
1363 
1364 	token = dell_smbios_find_token(kbd_tokens[bit]);
1365 	if (!token)
1366 		return -EINVAL;
1367 
1368 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
1369 	ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1370 
1371 	return ret;
1372 }
1373 
1374 static int kbd_get_token_bit(u8 bit)
1375 {
1376 	struct calling_interface_buffer buffer;
1377 	struct calling_interface_token *token;
1378 	int ret;
1379 	int val;
1380 
1381 	if (bit >= ARRAY_SIZE(kbd_tokens))
1382 		return -EINVAL;
1383 
1384 	token = dell_smbios_find_token(kbd_tokens[bit]);
1385 	if (!token)
1386 		return -EINVAL;
1387 
1388 	dell_fill_request(&buffer, token->location, 0, 0, 0);
1389 	ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1390 	val = buffer.output[1];
1391 
1392 	if (ret)
1393 		return ret;
1394 
1395 	return (val == token->value);
1396 }
1397 
1398 static int kbd_get_first_active_token_bit(void)
1399 {
1400 	int i;
1401 	int ret;
1402 
1403 	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1404 		ret = kbd_get_token_bit(i);
1405 		if (ret == 1)
1406 			return i;
1407 	}
1408 
1409 	return ret;
1410 }
1411 
1412 static int kbd_get_valid_token_counts(void)
1413 {
1414 	return hweight16(kbd_token_bits);
1415 }
1416 
1417 static inline int kbd_init_info(void)
1418 {
1419 	struct kbd_state state;
1420 	int ret;
1421 	int i;
1422 
1423 	ret = kbd_get_info(&kbd_info);
1424 	if (ret)
1425 		return ret;
1426 
1427 	/* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1428 	 *       timeout value which is shared for both battery and AC power
1429 	 *       settings. So do not try to set AC values on old models.
1430 	 */
1431 	if ((quirks && quirks->kbd_missing_ac_tag) ||
1432 	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
1433 		kbd_timeout_ac_supported = true;
1434 
1435 	kbd_get_state(&state);
1436 
1437 	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
1438 	if (kbd_info.seconds > 63)
1439 		kbd_info.seconds = 63;
1440 	if (kbd_info.minutes > 63)
1441 		kbd_info.minutes = 63;
1442 	if (kbd_info.hours > 63)
1443 		kbd_info.hours = 63;
1444 	if (kbd_info.days > 63)
1445 		kbd_info.days = 63;
1446 
1447 	/* NOTE: On tested machines ON mode did not work and caused
1448 	 *       problems (turned backlight off) so do not use it
1449 	 */
1450 	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1451 
1452 	kbd_previous_level = kbd_get_level(&state);
1453 	kbd_previous_mode_bit = state.mode_bit;
1454 
1455 	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1456 		kbd_previous_level = 1;
1457 
1458 	if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1459 		kbd_previous_mode_bit =
1460 			ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1461 		if (kbd_previous_mode_bit != 0)
1462 			kbd_previous_mode_bit--;
1463 	}
1464 
1465 	if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1466 			      BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1467 		kbd_als_supported = true;
1468 
1469 	if (kbd_info.modes & (
1470 	    BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1471 	    BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1472 	    BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1473 	   ))
1474 		kbd_triggers_supported = true;
1475 
1476 	/* kbd_mode_levels[0] is reserved, see below */
1477 	for (i = 0; i < 16; ++i)
1478 		if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1479 			kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1480 
1481 	/*
1482 	 * Find the first supported mode and assign to kbd_mode_levels[0].
1483 	 * This should be 0 (off), but we cannot depend on the BIOS to
1484 	 * support 0.
1485 	 */
1486 	if (kbd_mode_levels_count > 0) {
1487 		for (i = 0; i < 16; ++i) {
1488 			if (BIT(i) & kbd_info.modes) {
1489 				kbd_mode_levels[0] = i;
1490 				break;
1491 			}
1492 		}
1493 		kbd_mode_levels_count++;
1494 	}
1495 
1496 	return 0;
1497 
1498 }
1499 
1500 static inline void kbd_init_tokens(void)
1501 {
1502 	int i;
1503 
1504 	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1505 		if (dell_smbios_find_token(kbd_tokens[i]))
1506 			kbd_token_bits |= BIT(i);
1507 }
1508 
1509 static void kbd_init(void)
1510 {
1511 	int ret;
1512 
1513 	if (quirks && quirks->kbd_led_not_present)
1514 		return;
1515 
1516 	ret = kbd_init_info();
1517 	kbd_init_tokens();
1518 
1519 	/*
1520 	 * Only supports keyboard backlight when it has at least two modes.
1521 	 */
1522 	if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1523 	    || kbd_get_valid_token_counts() >= 2)
1524 		kbd_led_present = true;
1525 }
1526 
1527 static ssize_t kbd_led_timeout_store(struct device *dev,
1528 				     struct device_attribute *attr,
1529 				     const char *buf, size_t count)
1530 {
1531 	struct kbd_state new_state;
1532 	struct kbd_state state;
1533 	bool convert;
1534 	int value;
1535 	int ret;
1536 	char ch;
1537 	u8 unit;
1538 	int i;
1539 
1540 	ret = sscanf(buf, "%d %c", &value, &ch);
1541 	if (ret < 1)
1542 		return -EINVAL;
1543 	else if (ret == 1)
1544 		ch = 's';
1545 
1546 	if (value < 0)
1547 		return -EINVAL;
1548 
1549 	convert = false;
1550 
1551 	switch (ch) {
1552 	case 's':
1553 		if (value > kbd_info.seconds)
1554 			convert = true;
1555 		unit = KBD_TIMEOUT_SECONDS;
1556 		break;
1557 	case 'm':
1558 		if (value > kbd_info.minutes)
1559 			convert = true;
1560 		unit = KBD_TIMEOUT_MINUTES;
1561 		break;
1562 	case 'h':
1563 		if (value > kbd_info.hours)
1564 			convert = true;
1565 		unit = KBD_TIMEOUT_HOURS;
1566 		break;
1567 	case 'd':
1568 		if (value > kbd_info.days)
1569 			convert = true;
1570 		unit = KBD_TIMEOUT_DAYS;
1571 		break;
1572 	default:
1573 		return -EINVAL;
1574 	}
1575 
1576 	if (quirks && quirks->needs_kbd_timeouts)
1577 		convert = true;
1578 
1579 	if (convert) {
1580 		/* Convert value from current units to seconds */
1581 		switch (unit) {
1582 		case KBD_TIMEOUT_DAYS:
1583 			value *= 24;
1584 			fallthrough;
1585 		case KBD_TIMEOUT_HOURS:
1586 			value *= 60;
1587 			fallthrough;
1588 		case KBD_TIMEOUT_MINUTES:
1589 			value *= 60;
1590 			unit = KBD_TIMEOUT_SECONDS;
1591 		}
1592 
1593 		if (quirks && quirks->needs_kbd_timeouts) {
1594 			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1595 				if (value <= quirks->kbd_timeouts[i]) {
1596 					value = quirks->kbd_timeouts[i];
1597 					break;
1598 				}
1599 			}
1600 		}
1601 
1602 		if (value <= kbd_info.seconds && kbd_info.seconds) {
1603 			unit = KBD_TIMEOUT_SECONDS;
1604 		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1605 			value /= 60;
1606 			unit = KBD_TIMEOUT_MINUTES;
1607 		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1608 			value /= (60 * 60);
1609 			unit = KBD_TIMEOUT_HOURS;
1610 		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1611 			value /= (60 * 60 * 24);
1612 			unit = KBD_TIMEOUT_DAYS;
1613 		} else {
1614 			return -EINVAL;
1615 		}
1616 	}
1617 
1618 	mutex_lock(&kbd_led_mutex);
1619 
1620 	ret = kbd_get_state(&state);
1621 	if (ret)
1622 		goto out;
1623 
1624 	new_state = state;
1625 
1626 	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1627 		new_state.timeout_value_ac = value;
1628 		new_state.timeout_unit_ac = unit;
1629 	} else {
1630 		new_state.timeout_value = value;
1631 		new_state.timeout_unit = unit;
1632 	}
1633 
1634 	ret = kbd_set_state_safe(&new_state, &state);
1635 	if (ret)
1636 		goto out;
1637 
1638 	ret = count;
1639 out:
1640 	mutex_unlock(&kbd_led_mutex);
1641 	return ret;
1642 }
1643 
1644 static ssize_t kbd_led_timeout_show(struct device *dev,
1645 				    struct device_attribute *attr, char *buf)
1646 {
1647 	struct kbd_state state;
1648 	int value;
1649 	int ret;
1650 	int len;
1651 	u8 unit;
1652 
1653 	ret = kbd_get_state(&state);
1654 	if (ret)
1655 		return ret;
1656 
1657 	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1658 		value = state.timeout_value_ac;
1659 		unit = state.timeout_unit_ac;
1660 	} else {
1661 		value = state.timeout_value;
1662 		unit = state.timeout_unit;
1663 	}
1664 
1665 	len = sprintf(buf, "%d", value);
1666 
1667 	switch (unit) {
1668 	case KBD_TIMEOUT_SECONDS:
1669 		return len + sprintf(buf+len, "s\n");
1670 	case KBD_TIMEOUT_MINUTES:
1671 		return len + sprintf(buf+len, "m\n");
1672 	case KBD_TIMEOUT_HOURS:
1673 		return len + sprintf(buf+len, "h\n");
1674 	case KBD_TIMEOUT_DAYS:
1675 		return len + sprintf(buf+len, "d\n");
1676 	default:
1677 		return -EINVAL;
1678 	}
1679 
1680 	return len;
1681 }
1682 
1683 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1684 		   kbd_led_timeout_show, kbd_led_timeout_store);
1685 
1686 static const char * const kbd_led_triggers[] = {
1687 	"keyboard",
1688 	"touchpad",
1689 	/*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1690 	"mouse",
1691 };
1692 
1693 static ssize_t kbd_led_triggers_store(struct device *dev,
1694 				      struct device_attribute *attr,
1695 				      const char *buf, size_t count)
1696 {
1697 	struct kbd_state new_state;
1698 	struct kbd_state state;
1699 	bool triggers_enabled = false;
1700 	int trigger_bit = -1;
1701 	char trigger[21];
1702 	int i, ret;
1703 
1704 	ret = sscanf(buf, "%20s", trigger);
1705 	if (ret != 1)
1706 		return -EINVAL;
1707 
1708 	if (trigger[0] != '+' && trigger[0] != '-')
1709 		return -EINVAL;
1710 
1711 	mutex_lock(&kbd_led_mutex);
1712 
1713 	ret = kbd_get_state(&state);
1714 	if (ret)
1715 		goto out;
1716 
1717 	if (kbd_triggers_supported)
1718 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1719 
1720 	if (kbd_triggers_supported) {
1721 		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1722 			if (!(kbd_info.triggers & BIT(i)))
1723 				continue;
1724 			if (!kbd_led_triggers[i])
1725 				continue;
1726 			if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1727 				continue;
1728 			if (trigger[0] == '+' &&
1729 			    triggers_enabled && (state.triggers & BIT(i))) {
1730 				ret = count;
1731 				goto out;
1732 			}
1733 			if (trigger[0] == '-' &&
1734 			    (!triggers_enabled || !(state.triggers & BIT(i)))) {
1735 				ret = count;
1736 				goto out;
1737 			}
1738 			trigger_bit = i;
1739 			break;
1740 		}
1741 	}
1742 
1743 	if (trigger_bit == -1) {
1744 		ret = -EINVAL;
1745 		goto out;
1746 	}
1747 
1748 	new_state = state;
1749 	if (trigger[0] == '+')
1750 		new_state.triggers |= BIT(trigger_bit);
1751 	else {
1752 		new_state.triggers &= ~BIT(trigger_bit);
1753 		/*
1754 		 * NOTE: trackstick bit (2) must be disabled when
1755 		 *       disabling touchpad bit (1), otherwise touchpad
1756 		 *       bit (1) will not be disabled
1757 		 */
1758 		if (trigger_bit == 1)
1759 			new_state.triggers &= ~BIT(2);
1760 	}
1761 	if ((kbd_info.triggers & new_state.triggers) !=
1762 	    new_state.triggers) {
1763 		ret = -EINVAL;
1764 		goto out;
1765 	}
1766 	if (new_state.triggers && !triggers_enabled) {
1767 		new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1768 		kbd_set_level(&new_state, kbd_previous_level);
1769 	} else if (new_state.triggers == 0) {
1770 		kbd_set_level(&new_state, 0);
1771 	}
1772 	if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1773 		ret = -EINVAL;
1774 		goto out;
1775 	}
1776 	ret = kbd_set_state_safe(&new_state, &state);
1777 	if (ret)
1778 		goto out;
1779 	if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1780 		kbd_previous_mode_bit = new_state.mode_bit;
1781 	ret = count;
1782 out:
1783 	mutex_unlock(&kbd_led_mutex);
1784 	return ret;
1785 }
1786 
1787 static ssize_t kbd_led_triggers_show(struct device *dev,
1788 				     struct device_attribute *attr, char *buf)
1789 {
1790 	struct kbd_state state;
1791 	bool triggers_enabled;
1792 	int level, i, ret;
1793 	int len = 0;
1794 
1795 	ret = kbd_get_state(&state);
1796 	if (ret)
1797 		return ret;
1798 
1799 	len = 0;
1800 
1801 	if (kbd_triggers_supported) {
1802 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1803 		level = kbd_get_level(&state);
1804 		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1805 			if (!(kbd_info.triggers & BIT(i)))
1806 				continue;
1807 			if (!kbd_led_triggers[i])
1808 				continue;
1809 			if ((triggers_enabled || level <= 0) &&
1810 			    (state.triggers & BIT(i)))
1811 				buf[len++] = '+';
1812 			else
1813 				buf[len++] = '-';
1814 			len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1815 		}
1816 	}
1817 
1818 	if (len)
1819 		buf[len - 1] = '\n';
1820 
1821 	return len;
1822 }
1823 
1824 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1825 		   kbd_led_triggers_show, kbd_led_triggers_store);
1826 
1827 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1828 					 struct device_attribute *attr,
1829 					 const char *buf, size_t count)
1830 {
1831 	struct kbd_state new_state;
1832 	struct kbd_state state;
1833 	bool triggers_enabled = false;
1834 	int enable;
1835 	int ret;
1836 
1837 	ret = kstrtoint(buf, 0, &enable);
1838 	if (ret)
1839 		return ret;
1840 
1841 	mutex_lock(&kbd_led_mutex);
1842 
1843 	ret = kbd_get_state(&state);
1844 	if (ret)
1845 		goto out;
1846 
1847 	if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1848 		ret = count;
1849 		goto out;
1850 	}
1851 
1852 	new_state = state;
1853 
1854 	if (kbd_triggers_supported)
1855 		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1856 
1857 	if (enable) {
1858 		if (triggers_enabled)
1859 			new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1860 		else
1861 			new_state.mode_bit = KBD_MODE_BIT_ALS;
1862 	} else {
1863 		if (triggers_enabled) {
1864 			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1865 			kbd_set_level(&new_state, kbd_previous_level);
1866 		} else {
1867 			new_state.mode_bit = KBD_MODE_BIT_ON;
1868 		}
1869 	}
1870 	if (!(kbd_info.modes & BIT(new_state.mode_bit)))  {
1871 		ret = -EINVAL;
1872 		goto out;
1873 	}
1874 
1875 	ret = kbd_set_state_safe(&new_state, &state);
1876 	if (ret)
1877 		goto out;
1878 	kbd_previous_mode_bit = new_state.mode_bit;
1879 
1880 	ret = count;
1881 out:
1882 	mutex_unlock(&kbd_led_mutex);
1883 	return ret;
1884 }
1885 
1886 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1887 					struct device_attribute *attr,
1888 					char *buf)
1889 {
1890 	struct kbd_state state;
1891 	bool enabled = false;
1892 	int ret;
1893 
1894 	ret = kbd_get_state(&state);
1895 	if (ret)
1896 		return ret;
1897 	enabled = kbd_is_als_mode_bit(state.mode_bit);
1898 
1899 	return sprintf(buf, "%d\n", enabled ? 1 : 0);
1900 }
1901 
1902 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1903 		   kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1904 
1905 static ssize_t kbd_led_als_setting_store(struct device *dev,
1906 					 struct device_attribute *attr,
1907 					 const char *buf, size_t count)
1908 {
1909 	struct kbd_state state;
1910 	struct kbd_state new_state;
1911 	u8 setting;
1912 	int ret;
1913 
1914 	ret = kstrtou8(buf, 10, &setting);
1915 	if (ret)
1916 		return ret;
1917 
1918 	mutex_lock(&kbd_led_mutex);
1919 
1920 	ret = kbd_get_state(&state);
1921 	if (ret)
1922 		goto out;
1923 
1924 	new_state = state;
1925 	new_state.als_setting = setting;
1926 
1927 	ret = kbd_set_state_safe(&new_state, &state);
1928 	if (ret)
1929 		goto out;
1930 
1931 	ret = count;
1932 out:
1933 	mutex_unlock(&kbd_led_mutex);
1934 	return ret;
1935 }
1936 
1937 static ssize_t kbd_led_als_setting_show(struct device *dev,
1938 					struct device_attribute *attr,
1939 					char *buf)
1940 {
1941 	struct kbd_state state;
1942 	int ret;
1943 
1944 	ret = kbd_get_state(&state);
1945 	if (ret)
1946 		return ret;
1947 
1948 	return sprintf(buf, "%d\n", state.als_setting);
1949 }
1950 
1951 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1952 		   kbd_led_als_setting_show, kbd_led_als_setting_store);
1953 
1954 static struct attribute *kbd_led_attrs[] = {
1955 	&dev_attr_stop_timeout.attr,
1956 	&dev_attr_start_triggers.attr,
1957 	NULL,
1958 };
1959 
1960 static const struct attribute_group kbd_led_group = {
1961 	.attrs = kbd_led_attrs,
1962 };
1963 
1964 static struct attribute *kbd_led_als_attrs[] = {
1965 	&dev_attr_als_enabled.attr,
1966 	&dev_attr_als_setting.attr,
1967 	NULL,
1968 };
1969 
1970 static const struct attribute_group kbd_led_als_group = {
1971 	.attrs = kbd_led_als_attrs,
1972 };
1973 
1974 static const struct attribute_group *kbd_led_groups[] = {
1975 	&kbd_led_group,
1976 	&kbd_led_als_group,
1977 	NULL,
1978 };
1979 
1980 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1981 {
1982 	int ret;
1983 	u16 num;
1984 	struct kbd_state state;
1985 
1986 	if (kbd_get_max_level()) {
1987 		ret = kbd_get_state(&state);
1988 		if (ret)
1989 			return 0;
1990 		ret = kbd_get_level(&state);
1991 		if (ret < 0)
1992 			return 0;
1993 		return ret;
1994 	}
1995 
1996 	if (kbd_get_valid_token_counts()) {
1997 		ret = kbd_get_first_active_token_bit();
1998 		if (ret < 0)
1999 			return 0;
2000 		for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
2001 			num &= num - 1; /* clear the first bit set */
2002 		if (num == 0)
2003 			return 0;
2004 		return ffs(num) - 1;
2005 	}
2006 
2007 	pr_warn("Keyboard brightness level control not supported\n");
2008 	return 0;
2009 }
2010 
2011 static int kbd_led_level_set(struct led_classdev *led_cdev,
2012 			     enum led_brightness value)
2013 {
2014 	enum led_brightness new_value = value;
2015 	struct kbd_state state;
2016 	struct kbd_state new_state;
2017 	u16 num;
2018 	int ret;
2019 
2020 	mutex_lock(&kbd_led_mutex);
2021 
2022 	if (kbd_get_max_level()) {
2023 		ret = kbd_get_state(&state);
2024 		if (ret)
2025 			goto out;
2026 		new_state = state;
2027 		ret = kbd_set_level(&new_state, value);
2028 		if (ret)
2029 			goto out;
2030 		ret = kbd_set_state_safe(&new_state, &state);
2031 	} else if (kbd_get_valid_token_counts()) {
2032 		for (num = kbd_token_bits; num != 0 && value > 0; --value)
2033 			num &= num - 1; /* clear the first bit set */
2034 		if (num == 0)
2035 			ret = 0;
2036 		else
2037 			ret = kbd_set_token_bit(ffs(num) - 1);
2038 	} else {
2039 		pr_warn("Keyboard brightness level control not supported\n");
2040 		ret = -ENXIO;
2041 	}
2042 
2043 out:
2044 	if (ret == 0)
2045 		kbd_led_level = new_value;
2046 
2047 	mutex_unlock(&kbd_led_mutex);
2048 	return ret;
2049 }
2050 
2051 static struct led_classdev kbd_led = {
2052 	.name           = "dell::kbd_backlight",
2053 	.flags		= LED_BRIGHT_HW_CHANGED,
2054 	.brightness_set_blocking = kbd_led_level_set,
2055 	.brightness_get = kbd_led_level_get,
2056 	.groups         = kbd_led_groups,
2057 };
2058 
2059 static int __init kbd_led_init(struct device *dev)
2060 {
2061 	int ret;
2062 
2063 	kbd_init();
2064 	if (!kbd_led_present)
2065 		return -ENODEV;
2066 	if (!kbd_als_supported)
2067 		kbd_led_groups[1] = NULL;
2068 	kbd_led.max_brightness = kbd_get_max_level();
2069 	if (!kbd_led.max_brightness) {
2070 		kbd_led.max_brightness = kbd_get_valid_token_counts();
2071 		if (kbd_led.max_brightness)
2072 			kbd_led.max_brightness--;
2073 	}
2074 
2075 	kbd_led_level = kbd_led_level_get(NULL);
2076 
2077 	ret = led_classdev_register(dev, &kbd_led);
2078 	if (ret)
2079 		kbd_led_present = false;
2080 
2081 	return ret;
2082 }
2083 
2084 static void brightness_set_exit(struct led_classdev *led_cdev,
2085 				enum led_brightness value)
2086 {
2087 	/* Don't change backlight level on exit */
2088 };
2089 
2090 static void kbd_led_exit(void)
2091 {
2092 	if (!kbd_led_present)
2093 		return;
2094 	kbd_led.brightness_set = brightness_set_exit;
2095 	led_classdev_unregister(&kbd_led);
2096 }
2097 
2098 static int dell_laptop_notifier_call(struct notifier_block *nb,
2099 				     unsigned long action, void *data)
2100 {
2101 	bool changed = false;
2102 	enum led_brightness new_kbd_led_level;
2103 
2104 	switch (action) {
2105 	case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2106 		if (!kbd_led_present)
2107 			break;
2108 
2109 		mutex_lock(&kbd_led_mutex);
2110 		new_kbd_led_level = kbd_led_level_get(&kbd_led);
2111 		if (kbd_led_level != new_kbd_led_level) {
2112 			kbd_led_level = new_kbd_led_level;
2113 			changed = true;
2114 		}
2115 		mutex_unlock(&kbd_led_mutex);
2116 
2117 		if (changed)
2118 			led_classdev_notify_brightness_hw_changed(&kbd_led,
2119 								kbd_led_level);
2120 		break;
2121 	}
2122 
2123 	return NOTIFY_OK;
2124 }
2125 
2126 static struct notifier_block dell_laptop_notifier = {
2127 	.notifier_call = dell_laptop_notifier_call,
2128 };
2129 
2130 static int micmute_led_set(struct led_classdev *led_cdev,
2131 			   enum led_brightness brightness)
2132 {
2133 	struct calling_interface_buffer buffer;
2134 	struct calling_interface_token *token;
2135 	int state = brightness != LED_OFF;
2136 
2137 	if (state == 0)
2138 		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2139 	else
2140 		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2141 
2142 	if (!token)
2143 		return -ENODEV;
2144 
2145 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
2146 	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2147 
2148 	return 0;
2149 }
2150 
2151 static struct led_classdev micmute_led_cdev = {
2152 	.name = "platform::micmute",
2153 	.max_brightness = 1,
2154 	.brightness_set_blocking = micmute_led_set,
2155 	.default_trigger = "audio-micmute",
2156 };
2157 
2158 static int mute_led_set(struct led_classdev *led_cdev,
2159 			   enum led_brightness brightness)
2160 {
2161 	struct calling_interface_buffer buffer;
2162 	struct calling_interface_token *token;
2163 	int state = brightness != LED_OFF;
2164 
2165 	if (state == 0)
2166 		token = dell_smbios_find_token(GLOBAL_MUTE_DISABLE);
2167 	else
2168 		token = dell_smbios_find_token(GLOBAL_MUTE_ENABLE);
2169 
2170 	if (!token)
2171 		return -ENODEV;
2172 
2173 	dell_fill_request(&buffer, token->location, token->value, 0, 0);
2174 	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2175 
2176 	return 0;
2177 }
2178 
2179 static struct led_classdev mute_led_cdev = {
2180 	.name = "platform::mute",
2181 	.max_brightness = 1,
2182 	.brightness_set_blocking = mute_led_set,
2183 	.default_trigger = "audio-mute",
2184 };
2185 
2186 static int __init dell_init(void)
2187 {
2188 	struct calling_interface_token *token;
2189 	int max_intensity = 0;
2190 	int ret;
2191 
2192 	if (!dmi_check_system(dell_device_table))
2193 		return -ENODEV;
2194 
2195 	quirks = NULL;
2196 	/* find if this machine support other functions */
2197 	dmi_check_system(dell_quirks);
2198 
2199 	ret = platform_driver_register(&platform_driver);
2200 	if (ret)
2201 		goto fail_platform_driver;
2202 	platform_device = platform_device_alloc("dell-laptop", PLATFORM_DEVID_NONE);
2203 	if (!platform_device) {
2204 		ret = -ENOMEM;
2205 		goto fail_platform_device1;
2206 	}
2207 	ret = platform_device_add(platform_device);
2208 	if (ret)
2209 		goto fail_platform_device2;
2210 
2211 	ret = dell_setup_rfkill();
2212 
2213 	if (ret) {
2214 		pr_warn("Unable to setup rfkill\n");
2215 		goto fail_rfkill;
2216 	}
2217 
2218 	if (quirks && quirks->touchpad_led)
2219 		touchpad_led_init(&platform_device->dev);
2220 
2221 	kbd_led_init(&platform_device->dev);
2222 
2223 	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2224 	debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2225 			    &dell_debugfs_fops);
2226 
2227 	dell_laptop_register_notifier(&dell_laptop_notifier);
2228 
2229 	if (dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE) &&
2230 	    dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE) &&
2231 	    !dell_privacy_has_mic_mute()) {
2232 		ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
2233 		if (ret < 0)
2234 			goto fail_led;
2235 		micmute_led_registered = true;
2236 	}
2237 
2238 	if (dell_smbios_find_token(GLOBAL_MUTE_DISABLE) &&
2239 	    dell_smbios_find_token(GLOBAL_MUTE_ENABLE)) {
2240 		ret = led_classdev_register(&platform_device->dev, &mute_led_cdev);
2241 		if (ret < 0)
2242 			goto fail_backlight;
2243 		mute_led_registered = true;
2244 	}
2245 
2246 	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2247 		return 0;
2248 
2249 	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2250 	if (token) {
2251 		struct calling_interface_buffer buffer;
2252 
2253 		dell_fill_request(&buffer, token->location, 0, 0, 0);
2254 		ret = dell_send_request(&buffer,
2255 					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2256 		if (ret == 0)
2257 			max_intensity = buffer.output[3];
2258 	}
2259 
2260 	if (max_intensity) {
2261 		struct backlight_properties props;
2262 		memset(&props, 0, sizeof(struct backlight_properties));
2263 		props.type = BACKLIGHT_PLATFORM;
2264 		props.max_brightness = max_intensity;
2265 		dell_backlight_device = backlight_device_register("dell_backlight",
2266 								  &platform_device->dev,
2267 								  NULL,
2268 								  &dell_ops,
2269 								  &props);
2270 
2271 		if (IS_ERR(dell_backlight_device)) {
2272 			ret = PTR_ERR(dell_backlight_device);
2273 			dell_backlight_device = NULL;
2274 			goto fail_backlight;
2275 		}
2276 
2277 		dell_backlight_device->props.brightness =
2278 			dell_get_intensity(dell_backlight_device);
2279 		if (dell_backlight_device->props.brightness < 0) {
2280 			ret = dell_backlight_device->props.brightness;
2281 			goto fail_get_brightness;
2282 		}
2283 		backlight_update_status(dell_backlight_device);
2284 	}
2285 
2286 	return 0;
2287 
2288 fail_get_brightness:
2289 	backlight_device_unregister(dell_backlight_device);
2290 fail_backlight:
2291 	if (micmute_led_registered)
2292 		led_classdev_unregister(&micmute_led_cdev);
2293 	if (mute_led_registered)
2294 		led_classdev_unregister(&mute_led_cdev);
2295 fail_led:
2296 	dell_cleanup_rfkill();
2297 fail_rfkill:
2298 	platform_device_del(platform_device);
2299 fail_platform_device2:
2300 	platform_device_put(platform_device);
2301 fail_platform_device1:
2302 	platform_driver_unregister(&platform_driver);
2303 fail_platform_driver:
2304 	return ret;
2305 }
2306 
2307 static void __exit dell_exit(void)
2308 {
2309 	dell_laptop_unregister_notifier(&dell_laptop_notifier);
2310 	debugfs_remove_recursive(dell_laptop_dir);
2311 	if (quirks && quirks->touchpad_led)
2312 		touchpad_led_exit();
2313 	kbd_led_exit();
2314 	backlight_device_unregister(dell_backlight_device);
2315 	if (micmute_led_registered)
2316 		led_classdev_unregister(&micmute_led_cdev);
2317 	if (mute_led_registered)
2318 		led_classdev_unregister(&mute_led_cdev);
2319 	dell_cleanup_rfkill();
2320 	if (platform_device) {
2321 		platform_device_unregister(platform_device);
2322 		platform_driver_unregister(&platform_driver);
2323 	}
2324 }
2325 
2326 /* dell-rbtn.c driver export functions which will not work correctly (and could
2327  * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2328  * not problem when dell-rbtn.c is compiled as external module. When both files
2329  * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2330  * need to ensure that dell_init() will be called after initializing dell-rbtn.
2331  * This can be achieved by late_initcall() instead module_init().
2332  */
2333 late_initcall(dell_init);
2334 module_exit(dell_exit);
2335 
2336 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2337 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2338 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
2339 MODULE_DESCRIPTION("Dell laptop driver");
2340 MODULE_LICENSE("GPL");
2341