OSDN Git Service

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