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