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