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