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