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