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