]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/dell-laptop.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
[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                 .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
640 static int dell_debugfs_open(struct inode *inode, struct file *file)
641 {
642         return single_open(file, dell_debugfs_show, inode->i_private);
643 }
644
645 static const struct file_operations dell_debugfs_fops = {
646         .owner = THIS_MODULE,
647         .open = dell_debugfs_open,
648         .read = seq_read,
649         .llseek = seq_lseek,
650         .release = single_release,
651 };
652
653 static void dell_update_rfkill(struct work_struct *ignored)
654 {
655         struct calling_interface_buffer buffer;
656         int hwswitch = 0;
657         int status;
658         int ret;
659
660         dell_fill_request(&buffer, 0, 0, 0, 0);
661         ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
662         status = buffer.output[1];
663
664         if (ret != 0)
665                 return;
666
667         dell_fill_request(&buffer, 0, 0x2, 0, 0);
668         ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
669
670         if (ret == 0 && (status & BIT(0)))
671                 hwswitch = buffer.output[1];
672
673         if (wifi_rfkill) {
674                 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
675                 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
676         }
677         if (bluetooth_rfkill) {
678                 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
679                                             hwswitch);
680                 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
681         }
682         if (wwan_rfkill) {
683                 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
684                 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
685         }
686 }
687 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
688
689 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
690                               struct serio *port)
691 {
692         static bool extended;
693
694         if (str & I8042_STR_AUXDATA)
695                 return false;
696
697         if (unlikely(data == 0xe0)) {
698                 extended = true;
699                 return false;
700         } else if (unlikely(extended)) {
701                 switch (data) {
702                 case 0x8:
703                         schedule_delayed_work(&dell_rfkill_work,
704                                               round_jiffies_relative(HZ / 4));
705                         break;
706                 }
707                 extended = false;
708         }
709
710         return false;
711 }
712
713 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
714 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
715
716 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
717                                           unsigned long action, void *data)
718 {
719         schedule_delayed_work(&dell_rfkill_work, 0);
720         return NOTIFY_OK;
721 }
722
723 static struct notifier_block dell_laptop_rbtn_notifier = {
724         .notifier_call = dell_laptop_rbtn_notifier_call,
725 };
726
727 static int __init dell_setup_rfkill(void)
728 {
729         struct calling_interface_buffer buffer;
730         int status, ret, whitelisted;
731         const char *product;
732
733         /*
734          * rfkill support causes trouble on various models, mostly Inspirons.
735          * So we whitelist certain series, and don't support rfkill on others.
736          */
737         whitelisted = 0;
738         product = dmi_get_system_info(DMI_PRODUCT_NAME);
739         if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
740                          strncmp(product, "Precision", 9) == 0))
741                 whitelisted = 1;
742         if (!force_rfkill && !whitelisted)
743                 return 0;
744
745         dell_fill_request(&buffer, 0, 0, 0, 0);
746         ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
747         status = buffer.output[1];
748
749         /* dell wireless info smbios call is not supported */
750         if (ret != 0)
751                 return 0;
752
753         /* rfkill is only tested on laptops with a hwswitch */
754         if (!(status & BIT(0)) && !force_rfkill)
755                 return 0;
756
757         if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
758                 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
759                                            RFKILL_TYPE_WLAN,
760                                            &dell_rfkill_ops, (void *) 1);
761                 if (!wifi_rfkill) {
762                         ret = -ENOMEM;
763                         goto err_wifi;
764                 }
765                 ret = rfkill_register(wifi_rfkill);
766                 if (ret)
767                         goto err_wifi;
768         }
769
770         if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
771                 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
772                                                 &platform_device->dev,
773                                                 RFKILL_TYPE_BLUETOOTH,
774                                                 &dell_rfkill_ops, (void *) 2);
775                 if (!bluetooth_rfkill) {
776                         ret = -ENOMEM;
777                         goto err_bluetooth;
778                 }
779                 ret = rfkill_register(bluetooth_rfkill);
780                 if (ret)
781                         goto err_bluetooth;
782         }
783
784         if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
785                 wwan_rfkill = rfkill_alloc("dell-wwan",
786                                            &platform_device->dev,
787                                            RFKILL_TYPE_WWAN,
788                                            &dell_rfkill_ops, (void *) 3);
789                 if (!wwan_rfkill) {
790                         ret = -ENOMEM;
791                         goto err_wwan;
792                 }
793                 ret = rfkill_register(wwan_rfkill);
794                 if (ret)
795                         goto err_wwan;
796         }
797
798         /*
799          * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
800          * which can receive events from HW slider switch.
801          *
802          * Dell SMBIOS on whitelisted models supports controlling radio devices
803          * but does not support receiving HW button switch events. We can use
804          * i8042 filter hook function to receive keyboard data and handle
805          * keycode for HW button.
806          *
807          * So if it is possible we will use Dell Airplane Mode Switch ACPI
808          * driver for receiving HW events and Dell SMBIOS for setting rfkill
809          * states. If ACPI driver or device is not available we will fallback to
810          * i8042 filter hook function.
811          *
812          * To prevent duplicate rfkill devices which control and do same thing,
813          * dell-rbtn driver will automatically remove its own rfkill devices
814          * once function dell_rbtn_notifier_register() is called.
815          */
816
817         dell_rbtn_notifier_register_func =
818                 symbol_request(dell_rbtn_notifier_register);
819         if (dell_rbtn_notifier_register_func) {
820                 dell_rbtn_notifier_unregister_func =
821                         symbol_request(dell_rbtn_notifier_unregister);
822                 if (!dell_rbtn_notifier_unregister_func) {
823                         symbol_put(dell_rbtn_notifier_register);
824                         dell_rbtn_notifier_register_func = NULL;
825                 }
826         }
827
828         if (dell_rbtn_notifier_register_func) {
829                 ret = dell_rbtn_notifier_register_func(
830                         &dell_laptop_rbtn_notifier);
831                 symbol_put(dell_rbtn_notifier_register);
832                 dell_rbtn_notifier_register_func = NULL;
833                 if (ret != 0) {
834                         symbol_put(dell_rbtn_notifier_unregister);
835                         dell_rbtn_notifier_unregister_func = NULL;
836                 }
837         } else {
838                 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
839                 ret = -ENODEV;
840         }
841
842         if (ret == 0) {
843                 pr_info("Using dell-rbtn acpi driver for receiving events\n");
844         } else if (ret != -ENODEV) {
845                 pr_warn("Unable to register dell rbtn notifier\n");
846                 goto err_filter;
847         } else {
848                 ret = i8042_install_filter(dell_laptop_i8042_filter);
849                 if (ret) {
850                         pr_warn("Unable to install key filter\n");
851                         goto err_filter;
852                 }
853                 pr_info("Using i8042 filter function for receiving events\n");
854         }
855
856         return 0;
857 err_filter:
858         if (wwan_rfkill)
859                 rfkill_unregister(wwan_rfkill);
860 err_wwan:
861         rfkill_destroy(wwan_rfkill);
862         if (bluetooth_rfkill)
863                 rfkill_unregister(bluetooth_rfkill);
864 err_bluetooth:
865         rfkill_destroy(bluetooth_rfkill);
866         if (wifi_rfkill)
867                 rfkill_unregister(wifi_rfkill);
868 err_wifi:
869         rfkill_destroy(wifi_rfkill);
870
871         return ret;
872 }
873
874 static void dell_cleanup_rfkill(void)
875 {
876         if (dell_rbtn_notifier_unregister_func) {
877                 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
878                 symbol_put(dell_rbtn_notifier_unregister);
879                 dell_rbtn_notifier_unregister_func = NULL;
880         } else {
881                 i8042_remove_filter(dell_laptop_i8042_filter);
882         }
883         cancel_delayed_work_sync(&dell_rfkill_work);
884         if (wifi_rfkill) {
885                 rfkill_unregister(wifi_rfkill);
886                 rfkill_destroy(wifi_rfkill);
887         }
888         if (bluetooth_rfkill) {
889                 rfkill_unregister(bluetooth_rfkill);
890                 rfkill_destroy(bluetooth_rfkill);
891         }
892         if (wwan_rfkill) {
893                 rfkill_unregister(wwan_rfkill);
894                 rfkill_destroy(wwan_rfkill);
895         }
896 }
897
898 static int dell_send_intensity(struct backlight_device *bd)
899 {
900         struct calling_interface_buffer buffer;
901         struct calling_interface_token *token;
902         int ret;
903
904         token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
905         if (!token)
906                 return -ENODEV;
907
908         dell_fill_request(&buffer,
909                            token->location, bd->props.brightness, 0, 0);
910         if (power_supply_is_system_supplied() > 0)
911                 ret = dell_send_request(&buffer,
912                                         CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
913         else
914                 ret = dell_send_request(&buffer,
915                                         CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
916
917         return ret;
918 }
919
920 static int dell_get_intensity(struct backlight_device *bd)
921 {
922         struct calling_interface_buffer buffer;
923         struct calling_interface_token *token;
924         int ret;
925
926         token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
927         if (!token)
928                 return -ENODEV;
929
930         dell_fill_request(&buffer, token->location, 0, 0, 0);
931         if (power_supply_is_system_supplied() > 0)
932                 ret = dell_send_request(&buffer,
933                                         CLASS_TOKEN_READ, SELECT_TOKEN_AC);
934         else
935                 ret = dell_send_request(&buffer,
936                                         CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
937
938         if (ret == 0)
939                 ret = buffer.output[1];
940
941         return ret;
942 }
943
944 static const struct backlight_ops dell_ops = {
945         .get_brightness = dell_get_intensity,
946         .update_status  = dell_send_intensity,
947 };
948
949 static void touchpad_led_on(void)
950 {
951         int command = 0x97;
952         char data = 1;
953         i8042_command(&data, command | 1 << 12);
954 }
955
956 static void touchpad_led_off(void)
957 {
958         int command = 0x97;
959         char data = 2;
960         i8042_command(&data, command | 1 << 12);
961 }
962
963 static void touchpad_led_set(struct led_classdev *led_cdev,
964         enum led_brightness value)
965 {
966         if (value > 0)
967                 touchpad_led_on();
968         else
969                 touchpad_led_off();
970 }
971
972 static struct led_classdev touchpad_led = {
973         .name = "dell-laptop::touchpad",
974         .brightness_set = touchpad_led_set,
975         .flags = LED_CORE_SUSPENDRESUME,
976 };
977
978 static int __init touchpad_led_init(struct device *dev)
979 {
980         return led_classdev_register(dev, &touchpad_led);
981 }
982
983 static void touchpad_led_exit(void)
984 {
985         led_classdev_unregister(&touchpad_led);
986 }
987
988 /*
989  * Derived from information in smbios-keyboard-ctl:
990  *
991  * cbClass 4
992  * cbSelect 11
993  * Keyboard illumination
994  * cbArg1 determines the function to be performed
995  *
996  * cbArg1 0x0 = Get Feature Information
997  *  cbRES1         Standard return codes (0, -1, -2)
998  *  cbRES2, word0  Bitmap of user-selectable modes
999  *     bit 0     Always off (All systems)
1000  *     bit 1     Always on (Travis ATG, Siberia)
1001  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1002  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1003  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1004  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1005  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1006  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1007  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1008  *     bits 9-15 Reserved for future use
1009  *  cbRES2, byte2  Reserved for future use
1010  *  cbRES2, byte3  Keyboard illumination type
1011  *     0         Reserved
1012  *     1         Tasklight
1013  *     2         Backlight
1014  *     3-255     Reserved for future use
1015  *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
1016  *     bit 0     Any keystroke
1017  *     bit 1     Touchpad activity
1018  *     bit 2     Pointing stick
1019  *     bit 3     Any mouse
1020  *     bits 4-7  Reserved for future use
1021  *  cbRES3, byte1  Supported timeout unit bitmap
1022  *     bit 0     Seconds
1023  *     bit 1     Minutes
1024  *     bit 2     Hours
1025  *     bit 3     Days
1026  *     bits 4-7  Reserved for future use
1027  *  cbRES3, byte2  Number of keyboard light brightness levels
1028  *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
1029  *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
1030  *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
1031  *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
1032  *
1033  * cbArg1 0x1 = Get Current State
1034  *  cbRES1         Standard return codes (0, -1, -2)
1035  *  cbRES2, word0  Bitmap of current mode state
1036  *     bit 0     Always off (All systems)
1037  *     bit 1     Always on (Travis ATG, Siberia)
1038  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1039  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1040  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1041  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1042  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1043  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1044  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1045  *     bits 9-15 Reserved for future use
1046  *     Note: Only One bit can be set
1047  *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
1048  *     bit 0     Any keystroke
1049  *     bit 1     Touchpad activity
1050  *     bit 2     Pointing stick
1051  *     bit 3     Any mouse
1052  *     bits 4-7  Reserved for future use
1053  *  cbRES2, byte3  Current Timeout on battery
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 byte
1061  *     are set upon return from the [Get feature information] call.
1062  *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
1063  *  cbRES3, byte1  Current ALS reading
1064  *  cbRES3, byte2  Current keyboard light level.
1065  *  cbRES3, byte3  Current timeout on AC Power
1066  *     bits 7:6  Timeout units indicator:
1067  *     00b       Seconds
1068  *     01b       Minutes
1069  *     10b       Hours
1070  *     11b       Days
1071  *     Bits 5:0  Timeout value (0-63) in sec/min/hr/day
1072  *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1073  *     are set upon return from the upon return from the [Get Feature information] call.
1074  *
1075  * cbArg1 0x2 = Set New State
1076  *  cbRES1         Standard return codes (0, -1, -2)
1077  *  cbArg2, word0  Bitmap of current mode state
1078  *     bit 0     Always off (All systems)
1079  *     bit 1     Always on (Travis ATG, Siberia)
1080  *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
1081  *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
1082  *     bit 4     Auto: Input-activity-based On; input-activity based Off
1083  *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1084  *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1085  *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1086  *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1087  *     bits 9-15 Reserved for future use
1088  *     Note: Only One bit can be set
1089  *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
1090  *                 keyboard to turn off automatically.
1091  *     bit 0     Any keystroke
1092  *     bit 1     Touchpad activity
1093  *     bit 2     Pointing stick
1094  *     bit 3     Any mouse
1095  *     bits 4-7  Reserved for future use
1096  *  cbArg2, byte3  Desired Timeout on battery
1097  *     bits 7:6  Timeout units indicator:
1098  *     00b       Seconds
1099  *     01b       Minutes
1100  *     10b       Hours
1101  *     11b       Days
1102  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1103  *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
1104  *  cbArg3, byte2  Desired keyboard light level.
1105  *  cbArg3, byte3  Desired Timeout on AC power
1106  *     bits 7:6  Timeout units indicator:
1107  *     00b       Seconds
1108  *     01b       Minutes
1109  *     10b       Hours
1110  *     11b       Days
1111  *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
1112  */
1113
1114
1115 enum kbd_timeout_unit {
1116         KBD_TIMEOUT_SECONDS = 0,
1117         KBD_TIMEOUT_MINUTES,
1118         KBD_TIMEOUT_HOURS,
1119         KBD_TIMEOUT_DAYS,
1120 };
1121
1122 enum kbd_mode_bit {
1123         KBD_MODE_BIT_OFF = 0,
1124         KBD_MODE_BIT_ON,
1125         KBD_MODE_BIT_ALS,
1126         KBD_MODE_BIT_TRIGGER_ALS,
1127         KBD_MODE_BIT_TRIGGER,
1128         KBD_MODE_BIT_TRIGGER_25,
1129         KBD_MODE_BIT_TRIGGER_50,
1130         KBD_MODE_BIT_TRIGGER_75,
1131         KBD_MODE_BIT_TRIGGER_100,
1132 };
1133
1134 #define kbd_is_als_mode_bit(bit) \
1135         ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1136 #define kbd_is_trigger_mode_bit(bit) \
1137         ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1138 #define kbd_is_level_mode_bit(bit) \
1139         ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1140
1141 struct kbd_info {
1142         u16 modes;
1143         u8 type;
1144         u8 triggers;
1145         u8 levels;
1146         u8 seconds;
1147         u8 minutes;
1148         u8 hours;
1149         u8 days;
1150 };
1151
1152 struct kbd_state {
1153         u8 mode_bit;
1154         u8 triggers;
1155         u8 timeout_value;
1156         u8 timeout_unit;
1157         u8 timeout_value_ac;
1158         u8 timeout_unit_ac;
1159         u8 als_setting;
1160         u8 als_value;
1161         u8 level;
1162 };
1163
1164 static const int kbd_tokens[] = {
1165         KBD_LED_OFF_TOKEN,
1166         KBD_LED_AUTO_25_TOKEN,
1167         KBD_LED_AUTO_50_TOKEN,
1168         KBD_LED_AUTO_75_TOKEN,
1169         KBD_LED_AUTO_100_TOKEN,
1170         KBD_LED_ON_TOKEN,
1171 };
1172
1173 static u16 kbd_token_bits;
1174
1175 static struct kbd_info kbd_info;
1176 static bool kbd_als_supported;
1177 static bool kbd_triggers_supported;
1178 static bool kbd_timeout_ac_supported;
1179
1180 static u8 kbd_mode_levels[16];
1181 static int kbd_mode_levels_count;
1182
1183 static u8 kbd_previous_level;
1184 static u8 kbd_previous_mode_bit;
1185
1186 static bool kbd_led_present;
1187 static DEFINE_MUTEX(kbd_led_mutex);
1188 static enum led_brightness kbd_led_level;
1189
1190 /*
1191  * NOTE: there are three ways to set the keyboard backlight level.
1192  * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1193  * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1194  * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1195  *
1196  * There are laptops which support only one of these methods. If we want to
1197  * support as many machines as possible we need to implement all three methods.
1198  * The first two methods use the kbd_state structure. The third uses SMBIOS
1199  * tokens. If kbd_info.levels == 0, the machine does not support setting the
1200  * keyboard backlight level via kbd_state.level.
1201  */
1202
1203 static int kbd_get_info(struct kbd_info *info)
1204 {
1205         struct calling_interface_buffer buffer;
1206         u8 units;
1207         int ret;
1208
1209         dell_fill_request(&buffer, 0, 0, 0, 0);
1210         ret = dell_send_request(&buffer,
1211                                 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1212         if (ret)
1213                 return ret;
1214
1215         info->modes = buffer.output[1] & 0xFFFF;
1216         info->type = (buffer.output[1] >> 24) & 0xFF;
1217         info->triggers = buffer.output[2] & 0xFF;
1218         units = (buffer.output[2] >> 8) & 0xFF;
1219         info->levels = (buffer.output[2] >> 16) & 0xFF;
1220
1221         if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1222                 info->levels--;
1223
1224         if (units & BIT(0))
1225                 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1226         if (units & BIT(1))
1227                 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1228         if (units & BIT(2))
1229                 info->hours = (buffer.output[3] >> 16) & 0xFF;
1230         if (units & BIT(3))
1231                 info->days = (buffer.output[3] >> 24) & 0xFF;
1232
1233         return ret;
1234 }
1235
1236 static unsigned int kbd_get_max_level(void)
1237 {
1238         if (kbd_info.levels != 0)
1239                 return kbd_info.levels;
1240         if (kbd_mode_levels_count > 0)
1241                 return kbd_mode_levels_count - 1;
1242         return 0;
1243 }
1244
1245 static int kbd_get_level(struct kbd_state *state)
1246 {
1247         int i;
1248
1249         if (kbd_info.levels != 0)
1250                 return state->level;
1251
1252         if (kbd_mode_levels_count > 0) {
1253                 for (i = 0; i < kbd_mode_levels_count; ++i)
1254                         if (kbd_mode_levels[i] == state->mode_bit)
1255                                 return i;
1256                 return 0;
1257         }
1258
1259         return -EINVAL;
1260 }
1261
1262 static int kbd_set_level(struct kbd_state *state, u8 level)
1263 {
1264         if (kbd_info.levels != 0) {
1265                 if (level != 0)
1266                         kbd_previous_level = level;
1267                 if (state->level == level)
1268                         return 0;
1269                 state->level = level;
1270                 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1271                         state->mode_bit = kbd_previous_mode_bit;
1272                 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1273                         kbd_previous_mode_bit = state->mode_bit;
1274                         state->mode_bit = KBD_MODE_BIT_OFF;
1275                 }
1276                 return 0;
1277         }
1278
1279         if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1280                 if (level != 0)
1281                         kbd_previous_level = level;
1282                 state->mode_bit = kbd_mode_levels[level];
1283                 return 0;
1284         }
1285
1286         return -EINVAL;
1287 }
1288
1289 static int kbd_get_state(struct kbd_state *state)
1290 {
1291         struct calling_interface_buffer buffer;
1292         int ret;
1293
1294         dell_fill_request(&buffer, 0, 0, 0, 0);
1295         ret = dell_send_request(&buffer,
1296                                 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1297         if (ret)
1298                 return ret;
1299
1300         state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1301         if (state->mode_bit != 0)
1302                 state->mode_bit--;
1303
1304         state->triggers = (buffer.output[1] >> 16) & 0xFF;
1305         state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1306         state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1307         state->als_setting = buffer.output[2] & 0xFF;
1308         state->als_value = (buffer.output[2] >> 8) & 0xFF;
1309         state->level = (buffer.output[2] >> 16) & 0xFF;
1310         state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1311         state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1312
1313         return ret;
1314 }
1315
1316 static int kbd_set_state(struct kbd_state *state)
1317 {
1318         struct calling_interface_buffer buffer;
1319         int ret;
1320         u32 input1;
1321         u32 input2;
1322
1323         input1 = BIT(state->mode_bit) & 0xFFFF;
1324         input1 |= (state->triggers & 0xFF) << 16;
1325         input1 |= (state->timeout_value & 0x3F) << 24;
1326         input1 |= (state->timeout_unit & 0x3) << 30;
1327         input2 = state->als_setting & 0xFF;
1328         input2 |= (state->level & 0xFF) << 16;
1329         input2 |= (state->timeout_value_ac & 0x3F) << 24;
1330         input2 |= (state->timeout_unit_ac & 0x3) << 30;
1331         dell_fill_request(&buffer, 0x2, input1, input2, 0);
1332         ret = dell_send_request(&buffer,
1333                                 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1334
1335         return ret;
1336 }
1337
1338 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1339 {
1340         int ret;
1341
1342         ret = kbd_set_state(state);
1343         if (ret == 0)
1344                 return 0;
1345
1346         /*
1347          * When setting the new state fails,try to restore the previous one.
1348          * This is needed on some machines where BIOS sets a default state when
1349          * setting a new state fails. This default state could be all off.
1350          */
1351
1352         if (kbd_set_state(old))
1353                 pr_err("Setting old previous keyboard state failed\n");
1354
1355         return ret;
1356 }
1357
1358 static int kbd_set_token_bit(u8 bit)
1359 {
1360         struct calling_interface_buffer buffer;
1361         struct calling_interface_token *token;
1362         int ret;
1363
1364         if (bit >= ARRAY_SIZE(kbd_tokens))
1365                 return -EINVAL;
1366
1367         token = dell_smbios_find_token(kbd_tokens[bit]);
1368         if (!token)
1369                 return -EINVAL;
1370
1371         dell_fill_request(&buffer, token->location, token->value, 0, 0);
1372         ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1373
1374         return ret;
1375 }
1376
1377 static int kbd_get_token_bit(u8 bit)
1378 {
1379         struct calling_interface_buffer buffer;
1380         struct calling_interface_token *token;
1381         int ret;
1382         int val;
1383
1384         if (bit >= ARRAY_SIZE(kbd_tokens))
1385                 return -EINVAL;
1386
1387         token = dell_smbios_find_token(kbd_tokens[bit]);
1388         if (!token)
1389                 return -EINVAL;
1390
1391         dell_fill_request(&buffer, token->location, 0, 0, 0);
1392         ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1393         val = buffer.output[1];
1394
1395         if (ret)
1396                 return ret;
1397
1398         return (val == token->value);
1399 }
1400
1401 static int kbd_get_first_active_token_bit(void)
1402 {
1403         int i;
1404         int ret;
1405
1406         for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1407                 ret = kbd_get_token_bit(i);
1408                 if (ret == 1)
1409                         return i;
1410         }
1411
1412         return ret;
1413 }
1414
1415 static int kbd_get_valid_token_counts(void)
1416 {
1417         return hweight16(kbd_token_bits);
1418 }
1419
1420 static inline int kbd_init_info(void)
1421 {
1422         struct kbd_state state;
1423         int ret;
1424         int i;
1425
1426         ret = kbd_get_info(&kbd_info);
1427         if (ret)
1428                 return ret;
1429
1430         /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1431          *       timeout value which is shared for both battery and AC power
1432          *       settings. So do not try to set AC values on old models.
1433          */
1434         if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1435                 kbd_timeout_ac_supported = true;
1436
1437         kbd_get_state(&state);
1438
1439         /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1440         if (kbd_info.seconds > 63)
1441                 kbd_info.seconds = 63;
1442         if (kbd_info.minutes > 63)
1443                 kbd_info.minutes = 63;
1444         if (kbd_info.hours > 63)
1445                 kbd_info.hours = 63;
1446         if (kbd_info.days > 63)
1447                 kbd_info.days = 63;
1448
1449         /* NOTE: On tested machines ON mode did not work and caused
1450          *       problems (turned backlight off) so do not use it
1451          */
1452         kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1453
1454         kbd_previous_level = kbd_get_level(&state);
1455         kbd_previous_mode_bit = state.mode_bit;
1456
1457         if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1458                 kbd_previous_level = 1;
1459
1460         if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1461                 kbd_previous_mode_bit =
1462                         ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1463                 if (kbd_previous_mode_bit != 0)
1464                         kbd_previous_mode_bit--;
1465         }
1466
1467         if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1468                               BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1469                 kbd_als_supported = true;
1470
1471         if (kbd_info.modes & (
1472             BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1473             BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1474             BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1475            ))
1476                 kbd_triggers_supported = true;
1477
1478         /* kbd_mode_levels[0] is reserved, see below */
1479         for (i = 0; i < 16; ++i)
1480                 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1481                         kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1482
1483         /*
1484          * Find the first supported mode and assign to kbd_mode_levels[0].
1485          * This should be 0 (off), but we cannot depend on the BIOS to
1486          * support 0.
1487          */
1488         if (kbd_mode_levels_count > 0) {
1489                 for (i = 0; i < 16; ++i) {
1490                         if (BIT(i) & kbd_info.modes) {
1491                                 kbd_mode_levels[0] = i;
1492                                 break;
1493                         }
1494                 }
1495                 kbd_mode_levels_count++;
1496         }
1497
1498         return 0;
1499
1500 }
1501
1502 static inline void kbd_init_tokens(void)
1503 {
1504         int i;
1505
1506         for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1507                 if (dell_smbios_find_token(kbd_tokens[i]))
1508                         kbd_token_bits |= BIT(i);
1509 }
1510
1511 static void kbd_init(void)
1512 {
1513         int ret;
1514
1515         ret = kbd_init_info();
1516         kbd_init_tokens();
1517
1518         /*
1519          * Only supports keyboard backlight when it has at least two modes.
1520          */
1521         if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1522             || kbd_get_valid_token_counts() >= 2)
1523                 kbd_led_present = true;
1524 }
1525
1526 static ssize_t kbd_led_timeout_store(struct device *dev,
1527                                      struct device_attribute *attr,
1528                                      const char *buf, size_t count)
1529 {
1530         struct kbd_state new_state;
1531         struct kbd_state state;
1532         bool convert;
1533         int value;
1534         int ret;
1535         char ch;
1536         u8 unit;
1537         int i;
1538
1539         ret = sscanf(buf, "%d %c", &value, &ch);
1540         if (ret < 1)
1541                 return -EINVAL;
1542         else if (ret == 1)
1543                 ch = 's';
1544
1545         if (value < 0)
1546                 return -EINVAL;
1547
1548         convert = false;
1549
1550         switch (ch) {
1551         case 's':
1552                 if (value > kbd_info.seconds)
1553                         convert = true;
1554                 unit = KBD_TIMEOUT_SECONDS;
1555                 break;
1556         case 'm':
1557                 if (value > kbd_info.minutes)
1558                         convert = true;
1559                 unit = KBD_TIMEOUT_MINUTES;
1560                 break;
1561         case 'h':
1562                 if (value > kbd_info.hours)
1563                         convert = true;
1564                 unit = KBD_TIMEOUT_HOURS;
1565                 break;
1566         case 'd':
1567                 if (value > kbd_info.days)
1568                         convert = true;
1569                 unit = KBD_TIMEOUT_DAYS;
1570                 break;
1571         default:
1572                 return -EINVAL;
1573         }
1574
1575         if (quirks && quirks->needs_kbd_timeouts)
1576                 convert = true;
1577
1578         if (convert) {
1579                 /* Convert value from current units to seconds */
1580                 switch (unit) {
1581                 case KBD_TIMEOUT_DAYS:
1582                         value *= 24;
1583                 case KBD_TIMEOUT_HOURS:
1584                         value *= 60;
1585                 case KBD_TIMEOUT_MINUTES:
1586                         value *= 60;
1587                         unit = KBD_TIMEOUT_SECONDS;
1588                 }
1589
1590                 if (quirks && quirks->needs_kbd_timeouts) {
1591                         for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1592                                 if (value <= quirks->kbd_timeouts[i]) {
1593                                         value = quirks->kbd_timeouts[i];
1594                                         break;
1595                                 }
1596                         }
1597                 }
1598
1599                 if (value <= kbd_info.seconds && kbd_info.seconds) {
1600                         unit = KBD_TIMEOUT_SECONDS;
1601                 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1602                         value /= 60;
1603                         unit = KBD_TIMEOUT_MINUTES;
1604                 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1605                         value /= (60 * 60);
1606                         unit = KBD_TIMEOUT_HOURS;
1607                 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1608                         value /= (60 * 60 * 24);
1609                         unit = KBD_TIMEOUT_DAYS;
1610                 } else {
1611                         return -EINVAL;
1612                 }
1613         }
1614
1615         mutex_lock(&kbd_led_mutex);
1616
1617         ret = kbd_get_state(&state);
1618         if (ret)
1619                 goto out;
1620
1621         new_state = state;
1622
1623         if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1624                 new_state.timeout_value_ac = value;
1625                 new_state.timeout_unit_ac = unit;
1626         } else {
1627                 new_state.timeout_value = value;
1628                 new_state.timeout_unit = unit;
1629         }
1630
1631         ret = kbd_set_state_safe(&new_state, &state);
1632         if (ret)
1633                 goto out;
1634
1635         ret = count;
1636 out:
1637         mutex_unlock(&kbd_led_mutex);
1638         return ret;
1639 }
1640
1641 static ssize_t kbd_led_timeout_show(struct device *dev,
1642                                     struct device_attribute *attr, char *buf)
1643 {
1644         struct kbd_state state;
1645         int value;
1646         int ret;
1647         int len;
1648         u8 unit;
1649
1650         ret = kbd_get_state(&state);
1651         if (ret)
1652                 return ret;
1653
1654         if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1655                 value = state.timeout_value_ac;
1656                 unit = state.timeout_unit_ac;
1657         } else {
1658                 value = state.timeout_value;
1659                 unit = state.timeout_unit;
1660         }
1661
1662         len = sprintf(buf, "%d", value);
1663
1664         switch (unit) {
1665         case KBD_TIMEOUT_SECONDS:
1666                 return len + sprintf(buf+len, "s\n");
1667         case KBD_TIMEOUT_MINUTES:
1668                 return len + sprintf(buf+len, "m\n");
1669         case KBD_TIMEOUT_HOURS:
1670                 return len + sprintf(buf+len, "h\n");
1671         case KBD_TIMEOUT_DAYS:
1672                 return len + sprintf(buf+len, "d\n");
1673         default:
1674                 return -EINVAL;
1675         }
1676
1677         return len;
1678 }
1679
1680 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1681                    kbd_led_timeout_show, kbd_led_timeout_store);
1682
1683 static const char * const kbd_led_triggers[] = {
1684         "keyboard",
1685         "touchpad",
1686         /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1687         "mouse",
1688 };
1689
1690 static ssize_t kbd_led_triggers_store(struct device *dev,
1691                                       struct device_attribute *attr,
1692                                       const char *buf, size_t count)
1693 {
1694         struct kbd_state new_state;
1695         struct kbd_state state;
1696         bool triggers_enabled = false;
1697         int trigger_bit = -1;
1698         char trigger[21];
1699         int i, ret;
1700
1701         ret = sscanf(buf, "%20s", trigger);
1702         if (ret != 1)
1703                 return -EINVAL;
1704
1705         if (trigger[0] != '+' && trigger[0] != '-')
1706                 return -EINVAL;
1707
1708         mutex_lock(&kbd_led_mutex);
1709
1710         ret = kbd_get_state(&state);
1711         if (ret)
1712                 goto out;
1713
1714         if (kbd_triggers_supported)
1715                 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1716
1717         if (kbd_triggers_supported) {
1718                 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1719                         if (!(kbd_info.triggers & BIT(i)))
1720                                 continue;
1721                         if (!kbd_led_triggers[i])
1722                                 continue;
1723                         if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1724                                 continue;
1725                         if (trigger[0] == '+' &&
1726                             triggers_enabled && (state.triggers & BIT(i))) {
1727                                 ret = count;
1728                                 goto out;
1729                         }
1730                         if (trigger[0] == '-' &&
1731                             (!triggers_enabled || !(state.triggers & BIT(i)))) {
1732                                 ret = count;
1733                                 goto out;
1734                         }
1735                         trigger_bit = i;
1736                         break;
1737                 }
1738         }
1739
1740         if (trigger_bit == -1) {
1741                 ret = -EINVAL;
1742                 goto out;
1743         }
1744
1745         new_state = state;
1746         if (trigger[0] == '+')
1747                 new_state.triggers |= BIT(trigger_bit);
1748         else {
1749                 new_state.triggers &= ~BIT(trigger_bit);
1750                 /*
1751                  * NOTE: trackstick bit (2) must be disabled when
1752                  *       disabling touchpad bit (1), otherwise touchpad
1753                  *       bit (1) will not be disabled
1754                  */
1755                 if (trigger_bit == 1)
1756                         new_state.triggers &= ~BIT(2);
1757         }
1758         if ((kbd_info.triggers & new_state.triggers) !=
1759             new_state.triggers) {
1760                 ret = -EINVAL;
1761                 goto out;
1762         }
1763         if (new_state.triggers && !triggers_enabled) {
1764                 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1765                 kbd_set_level(&new_state, kbd_previous_level);
1766         } else if (new_state.triggers == 0) {
1767                 kbd_set_level(&new_state, 0);
1768         }
1769         if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1770                 ret = -EINVAL;
1771                 goto out;
1772         }
1773         ret = kbd_set_state_safe(&new_state, &state);
1774         if (ret)
1775                 goto out;
1776         if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1777                 kbd_previous_mode_bit = new_state.mode_bit;
1778         ret = count;
1779 out:
1780         mutex_unlock(&kbd_led_mutex);
1781         return ret;
1782 }
1783
1784 static ssize_t kbd_led_triggers_show(struct device *dev,
1785                                      struct device_attribute *attr, char *buf)
1786 {
1787         struct kbd_state state;
1788         bool triggers_enabled;
1789         int level, i, ret;
1790         int len = 0;
1791
1792         ret = kbd_get_state(&state);
1793         if (ret)
1794                 return ret;
1795
1796         len = 0;
1797
1798         if (kbd_triggers_supported) {
1799                 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1800                 level = kbd_get_level(&state);
1801                 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1802                         if (!(kbd_info.triggers & BIT(i)))
1803                                 continue;
1804                         if (!kbd_led_triggers[i])
1805                                 continue;
1806                         if ((triggers_enabled || level <= 0) &&
1807                             (state.triggers & BIT(i)))
1808                                 buf[len++] = '+';
1809                         else
1810                                 buf[len++] = '-';
1811                         len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1812                 }
1813         }
1814
1815         if (len)
1816                 buf[len - 1] = '\n';
1817
1818         return len;
1819 }
1820
1821 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1822                    kbd_led_triggers_show, kbd_led_triggers_store);
1823
1824 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1825                                          struct device_attribute *attr,
1826                                          const char *buf, size_t count)
1827 {
1828         struct kbd_state new_state;
1829         struct kbd_state state;
1830         bool triggers_enabled = false;
1831         int enable;
1832         int ret;
1833
1834         ret = kstrtoint(buf, 0, &enable);
1835         if (ret)
1836                 return ret;
1837
1838         mutex_lock(&kbd_led_mutex);
1839
1840         ret = kbd_get_state(&state);
1841         if (ret)
1842                 goto out;
1843
1844         if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1845                 ret = count;
1846                 goto out;
1847         }
1848
1849         new_state = state;
1850
1851         if (kbd_triggers_supported)
1852                 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1853
1854         if (enable) {
1855                 if (triggers_enabled)
1856                         new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1857                 else
1858                         new_state.mode_bit = KBD_MODE_BIT_ALS;
1859         } else {
1860                 if (triggers_enabled) {
1861                         new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1862                         kbd_set_level(&new_state, kbd_previous_level);
1863                 } else {
1864                         new_state.mode_bit = KBD_MODE_BIT_ON;
1865                 }
1866         }
1867         if (!(kbd_info.modes & BIT(new_state.mode_bit)))  {
1868                 ret = -EINVAL;
1869                 goto out;
1870         }
1871
1872         ret = kbd_set_state_safe(&new_state, &state);
1873         if (ret)
1874                 goto out;
1875         kbd_previous_mode_bit = new_state.mode_bit;
1876
1877         ret = count;
1878 out:
1879         mutex_unlock(&kbd_led_mutex);
1880         return ret;
1881 }
1882
1883 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1884                                         struct device_attribute *attr,
1885                                         char *buf)
1886 {
1887         struct kbd_state state;
1888         bool enabled = false;
1889         int ret;
1890
1891         ret = kbd_get_state(&state);
1892         if (ret)
1893                 return ret;
1894         enabled = kbd_is_als_mode_bit(state.mode_bit);
1895
1896         return sprintf(buf, "%d\n", enabled ? 1 : 0);
1897 }
1898
1899 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1900                    kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1901
1902 static ssize_t kbd_led_als_setting_store(struct device *dev,
1903                                          struct device_attribute *attr,
1904                                          const char *buf, size_t count)
1905 {
1906         struct kbd_state state;
1907         struct kbd_state new_state;
1908         u8 setting;
1909         int ret;
1910
1911         ret = kstrtou8(buf, 10, &setting);
1912         if (ret)
1913                 return ret;
1914
1915         mutex_lock(&kbd_led_mutex);
1916
1917         ret = kbd_get_state(&state);
1918         if (ret)
1919                 goto out;
1920
1921         new_state = state;
1922         new_state.als_setting = setting;
1923
1924         ret = kbd_set_state_safe(&new_state, &state);
1925         if (ret)
1926                 goto out;
1927
1928         ret = count;
1929 out:
1930         mutex_unlock(&kbd_led_mutex);
1931         return ret;
1932 }
1933
1934 static ssize_t kbd_led_als_setting_show(struct device *dev,
1935                                         struct device_attribute *attr,
1936                                         char *buf)
1937 {
1938         struct kbd_state state;
1939         int ret;
1940
1941         ret = kbd_get_state(&state);
1942         if (ret)
1943                 return ret;
1944
1945         return sprintf(buf, "%d\n", state.als_setting);
1946 }
1947
1948 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1949                    kbd_led_als_setting_show, kbd_led_als_setting_store);
1950
1951 static struct attribute *kbd_led_attrs[] = {
1952         &dev_attr_stop_timeout.attr,
1953         &dev_attr_start_triggers.attr,
1954         NULL,
1955 };
1956
1957 static const struct attribute_group kbd_led_group = {
1958         .attrs = kbd_led_attrs,
1959 };
1960
1961 static struct attribute *kbd_led_als_attrs[] = {
1962         &dev_attr_als_enabled.attr,
1963         &dev_attr_als_setting.attr,
1964         NULL,
1965 };
1966
1967 static const struct attribute_group kbd_led_als_group = {
1968         .attrs = kbd_led_als_attrs,
1969 };
1970
1971 static const struct attribute_group *kbd_led_groups[] = {
1972         &kbd_led_group,
1973         &kbd_led_als_group,
1974         NULL,
1975 };
1976
1977 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1978 {
1979         int ret;
1980         u16 num;
1981         struct kbd_state state;
1982
1983         if (kbd_get_max_level()) {
1984                 ret = kbd_get_state(&state);
1985                 if (ret)
1986                         return 0;
1987                 ret = kbd_get_level(&state);
1988                 if (ret < 0)
1989                         return 0;
1990                 return ret;
1991         }
1992
1993         if (kbd_get_valid_token_counts()) {
1994                 ret = kbd_get_first_active_token_bit();
1995                 if (ret < 0)
1996                         return 0;
1997                 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1998                         num &= num - 1; /* clear the first bit set */
1999                 if (num == 0)
2000                         return 0;
2001                 return ffs(num) - 1;
2002         }
2003
2004         pr_warn("Keyboard brightness level control not supported\n");
2005         return 0;
2006 }
2007
2008 static int kbd_led_level_set(struct led_classdev *led_cdev,
2009                              enum led_brightness value)
2010 {
2011         enum led_brightness new_value = value;
2012         struct kbd_state state;
2013         struct kbd_state new_state;
2014         u16 num;
2015         int ret;
2016
2017         mutex_lock(&kbd_led_mutex);
2018
2019         if (kbd_get_max_level()) {
2020                 ret = kbd_get_state(&state);
2021                 if (ret)
2022                         goto out;
2023                 new_state = state;
2024                 ret = kbd_set_level(&new_state, value);
2025                 if (ret)
2026                         goto out;
2027                 ret = kbd_set_state_safe(&new_state, &state);
2028         } else if (kbd_get_valid_token_counts()) {
2029                 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2030                         num &= num - 1; /* clear the first bit set */
2031                 if (num == 0)
2032                         ret = 0;
2033                 else
2034                         ret = kbd_set_token_bit(ffs(num) - 1);
2035         } else {
2036                 pr_warn("Keyboard brightness level control not supported\n");
2037                 ret = -ENXIO;
2038         }
2039
2040 out:
2041         if (ret == 0)
2042                 kbd_led_level = new_value;
2043
2044         mutex_unlock(&kbd_led_mutex);
2045         return ret;
2046 }
2047
2048 static struct led_classdev kbd_led = {
2049         .name           = "dell::kbd_backlight",
2050         .flags          = LED_BRIGHT_HW_CHANGED,
2051         .brightness_set_blocking = kbd_led_level_set,
2052         .brightness_get = kbd_led_level_get,
2053         .groups         = kbd_led_groups,
2054 };
2055
2056 static int __init kbd_led_init(struct device *dev)
2057 {
2058         int ret;
2059
2060         kbd_init();
2061         if (!kbd_led_present)
2062                 return -ENODEV;
2063         if (!kbd_als_supported)
2064                 kbd_led_groups[1] = NULL;
2065         kbd_led.max_brightness = kbd_get_max_level();
2066         if (!kbd_led.max_brightness) {
2067                 kbd_led.max_brightness = kbd_get_valid_token_counts();
2068                 if (kbd_led.max_brightness)
2069                         kbd_led.max_brightness--;
2070         }
2071
2072         kbd_led_level = kbd_led_level_get(NULL);
2073
2074         ret = led_classdev_register(dev, &kbd_led);
2075         if (ret)
2076                 kbd_led_present = false;
2077
2078         return ret;
2079 }
2080
2081 static void brightness_set_exit(struct led_classdev *led_cdev,
2082                                 enum led_brightness value)
2083 {
2084         /* Don't change backlight level on exit */
2085 };
2086
2087 static void kbd_led_exit(void)
2088 {
2089         if (!kbd_led_present)
2090                 return;
2091         kbd_led.brightness_set = brightness_set_exit;
2092         led_classdev_unregister(&kbd_led);
2093 }
2094
2095 static int dell_laptop_notifier_call(struct notifier_block *nb,
2096                                      unsigned long action, void *data)
2097 {
2098         bool changed = false;
2099         enum led_brightness new_kbd_led_level;
2100
2101         switch (action) {
2102         case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2103                 if (!kbd_led_present)
2104                         break;
2105
2106                 mutex_lock(&kbd_led_mutex);
2107                 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2108                 if (kbd_led_level != new_kbd_led_level) {
2109                         kbd_led_level = new_kbd_led_level;
2110                         changed = true;
2111                 }
2112                 mutex_unlock(&kbd_led_mutex);
2113
2114                 if (changed)
2115                         led_classdev_notify_brightness_hw_changed(&kbd_led,
2116                                                                 kbd_led_level);
2117                 break;
2118         }
2119
2120         return NOTIFY_OK;
2121 }
2122
2123 static struct notifier_block dell_laptop_notifier = {
2124         .notifier_call = dell_laptop_notifier_call,
2125 };
2126
2127 int dell_micmute_led_set(int state)
2128 {
2129         struct calling_interface_buffer buffer;
2130         struct calling_interface_token *token;
2131
2132         if (state == 0)
2133                 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2134         else if (state == 1)
2135                 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2136         else
2137                 return -EINVAL;
2138
2139         if (!token)
2140                 return -ENODEV;
2141
2142         dell_fill_request(&buffer, token->location, token->value, 0, 0);
2143         dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2144
2145         return state;
2146 }
2147 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2148
2149 static int __init dell_init(void)
2150 {
2151         struct calling_interface_token *token;
2152         int max_intensity = 0;
2153         int ret;
2154
2155         if (!dmi_check_system(dell_device_table))
2156                 return -ENODEV;
2157
2158         quirks = NULL;
2159         /* find if this machine support other functions */
2160         dmi_check_system(dell_quirks);
2161
2162         ret = platform_driver_register(&platform_driver);
2163         if (ret)
2164                 goto fail_platform_driver;
2165         platform_device = platform_device_alloc("dell-laptop", -1);
2166         if (!platform_device) {
2167                 ret = -ENOMEM;
2168                 goto fail_platform_device1;
2169         }
2170         ret = platform_device_add(platform_device);
2171         if (ret)
2172                 goto fail_platform_device2;
2173
2174         ret = dell_setup_rfkill();
2175
2176         if (ret) {
2177                 pr_warn("Unable to setup rfkill\n");
2178                 goto fail_rfkill;
2179         }
2180
2181         if (quirks && quirks->touchpad_led)
2182                 touchpad_led_init(&platform_device->dev);
2183
2184         kbd_led_init(&platform_device->dev);
2185
2186         dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2187         if (dell_laptop_dir != NULL)
2188                 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2189                                     &dell_debugfs_fops);
2190
2191         dell_laptop_register_notifier(&dell_laptop_notifier);
2192
2193         if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2194                 return 0;
2195
2196         token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2197         if (token) {
2198                 struct calling_interface_buffer buffer;
2199
2200                 dell_fill_request(&buffer, token->location, 0, 0, 0);
2201                 ret = dell_send_request(&buffer,
2202                                         CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2203                 if (ret)
2204                         max_intensity = buffer.output[3];
2205         }
2206
2207         if (max_intensity) {
2208                 struct backlight_properties props;
2209                 memset(&props, 0, sizeof(struct backlight_properties));
2210                 props.type = BACKLIGHT_PLATFORM;
2211                 props.max_brightness = max_intensity;
2212                 dell_backlight_device = backlight_device_register("dell_backlight",
2213                                                                   &platform_device->dev,
2214                                                                   NULL,
2215                                                                   &dell_ops,
2216                                                                   &props);
2217
2218                 if (IS_ERR(dell_backlight_device)) {
2219                         ret = PTR_ERR(dell_backlight_device);
2220                         dell_backlight_device = NULL;
2221                         goto fail_backlight;
2222                 }
2223
2224                 dell_backlight_device->props.brightness =
2225                         dell_get_intensity(dell_backlight_device);
2226                 if (dell_backlight_device->props.brightness < 0) {
2227                         ret = dell_backlight_device->props.brightness;
2228                         goto fail_get_brightness;
2229                 }
2230                 backlight_update_status(dell_backlight_device);
2231         }
2232
2233         return 0;
2234
2235 fail_get_brightness:
2236         backlight_device_unregister(dell_backlight_device);
2237 fail_backlight:
2238         dell_cleanup_rfkill();
2239 fail_rfkill:
2240         platform_device_del(platform_device);
2241 fail_platform_device2:
2242         platform_device_put(platform_device);
2243 fail_platform_device1:
2244         platform_driver_unregister(&platform_driver);
2245 fail_platform_driver:
2246         return ret;
2247 }
2248
2249 static void __exit dell_exit(void)
2250 {
2251         dell_laptop_unregister_notifier(&dell_laptop_notifier);
2252         debugfs_remove_recursive(dell_laptop_dir);
2253         if (quirks && quirks->touchpad_led)
2254                 touchpad_led_exit();
2255         kbd_led_exit();
2256         backlight_device_unregister(dell_backlight_device);
2257         dell_cleanup_rfkill();
2258         if (platform_device) {
2259                 platform_device_unregister(platform_device);
2260                 platform_driver_unregister(&platform_driver);
2261         }
2262 }
2263
2264 /* dell-rbtn.c driver export functions which will not work correctly (and could
2265  * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2266  * not problem when dell-rbtn.c is compiled as external module. When both files
2267  * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2268  * need to ensure that dell_init() will be called after initializing dell-rbtn.
2269  * This can be achieved by late_initcall() instead module_init().
2270  */
2271 late_initcall(dell_init);
2272 module_exit(dell_exit);
2273
2274 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2275 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2276 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2277 MODULE_DESCRIPTION("Dell laptop driver");
2278 MODULE_LICENSE("GPL");