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