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