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