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