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