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