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