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