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