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