summaryrefslogtreecommitdiff
path: root/drivers/platform/x86/dell-laptop.c
blob: 4e2f76aa98de10d3c5aefffd6f83a53769d5a61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
// SPDX-License-Identifier: GPL-2.0-only
/*
 *  Driver for Dell laptop extras
 *
 *  Copyright (c) Red Hat <mjg@redhat.com>
 *  Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
 *  Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
 *
 *  Based on documentation in the libsmbios package:
 *  Copyright (C) 2005-2014 Dell Inc.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/dmi.h>
#include <linux/io.h>
#include <linux/rfkill.h>
#include <linux/power_supply.h>
#include <linux/acpi.h>
#include <linux/mm.h>
#include <linux/i8042.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <acpi/video.h>
#include "dell-rbtn.h"
#include "dell-smbios.h"

struct quirk_entry {
	bool touchpad_led;
	bool kbd_led_levels_off_1;
	bool kbd_missing_ac_tag;

	bool needs_kbd_timeouts;
	/*
	 * Ordered list of timeouts expressed in seconds.
	 * The list must end with -1
	 */
	int kbd_timeouts[];
};

static struct quirk_entry *quirks;

static struct quirk_entry quirk_dell_vostro_v130 = {
	.touchpad_led = true,
};

static int __init dmi_matched(const struct dmi_system_id *dmi)
{
	quirks = dmi->driver_data;
	return 1;
}

/*
 * These values come from Windows utility provided by Dell. If any other value
 * is used then BIOS silently set timeout to 0 without any error message.
 */
static struct quirk_entry quirk_dell_xps13_9333 = {
	.needs_kbd_timeouts = true,
	.kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
};

static struct quirk_entry quirk_dell_xps13_9370 = {
	.kbd_missing_ac_tag = true,
};

static struct quirk_entry quirk_dell_latitude_e6410 = {
	.kbd_led_levels_off_1 = true,
};

static struct platform_driver platform_driver = {
	.driver = {
		.name = "dell-laptop",
	}
};

static struct platform_device *platform_device;
static struct backlight_device *dell_backlight_device;
static struct rfkill *wifi_rfkill;
static struct rfkill *bluetooth_rfkill;
static struct rfkill *wwan_rfkill;
static bool force_rfkill;

module_param(force_rfkill, bool, 0444);
MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");

static const struct dmi_system_id dell_device_table[] __initconst = {
	{
		.ident = "Dell laptop",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
		},
	},
	{
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
		},
	},
	{
		.ident = "Dell Computer Corporation",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
			DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
		},
	},
	{ }
};
MODULE_DEVICE_TABLE(dmi, dell_device_table);

static const struct dmi_system_id dell_quirks[] __initconst = {
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro V130",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro V131",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3350",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3555",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron N311z",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron M5110",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3360",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3460",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3560",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Vostro 3450",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5420",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 5720",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7420",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Inspiron 7720",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
		},
		.driver_data = &quirk_dell_vostro_v130,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell XPS13 9333",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
		},
		.driver_data = &quirk_dell_xps13_9333,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell XPS 13 9370",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
		},
		.driver_data = &quirk_dell_xps13_9370,
	},
	{
		.callback = dmi_matched,
		.ident = "Dell Latitude E6410",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
		},
		.driver_data = &quirk_dell_latitude_e6410,
	},
	{ }
};

static void dell_fill_request(struct calling_interface_buffer *buffer,
			       u32 arg0, u32 arg1, u32 arg2, u32 arg3)
{
	memset(buffer, 0, sizeof(struct calling_interface_buffer));
	buffer->input[0] = arg0;
	buffer->input[1] = arg1;
	buffer->input[2] = arg2;
	buffer->input[3] = arg3;
}

static int dell_send_request(struct calling_interface_buffer *buffer,
			     u16 class, u16 select)
{
	int ret;

	buffer->cmd_class = class;
	buffer->cmd_select = select;
	ret = dell_smbios_call(buffer);
	if (ret != 0)
		return ret;
	return dell_smbios_error(buffer->output[0]);
}

/*
 * Derived from information in smbios-wireless-ctl:
 *
 * cbSelect 17, Value 11
 *
 * Return Wireless Info
 * cbArg1, byte0 = 0x00
 *
 *     cbRes1 Standard return codes (0, -1, -2)
 *     cbRes2 Info bit flags:
 *
 *     0 Hardware switch supported (1)
 *     1 WiFi locator supported (1)
 *     2 WLAN supported (1)
 *     3 Bluetooth (BT) supported (1)
 *     4 WWAN supported (1)
 *     5 Wireless KBD supported (1)
 *     6 Uw b supported (1)
 *     7 WiGig supported (1)
 *     8 WLAN installed (1)
 *     9 BT installed (1)
 *     10 WWAN installed (1)
 *     11 Uw b installed (1)
 *     12 WiGig installed (1)
 *     13-15 Reserved (0)
 *     16 Hardware (HW) switch is On (1)
 *     17 WLAN disabled (1)
 *     18 BT disabled (1)
 *     19 WWAN disabled (1)
 *     20 Uw b disabled (1)
 *     21 WiGig disabled (1)
 *     20-31 Reserved (0)
 *
 *     cbRes3 NVRAM size in bytes
 *     cbRes4, byte 0 NVRAM format version number
 *
 *
 * Set QuickSet Radio Disable Flag
 *     cbArg1, byte0 = 0x01
 *     cbArg1, byte1
 *     Radio ID     value:
 *     0        Radio Status
 *     1        WLAN ID
 *     2        BT ID
 *     3        WWAN ID
 *     4        UWB ID
 *     5        WIGIG ID
 *     cbArg1, byte2    Flag bits:
 *             0 QuickSet disables radio (1)
 *             1-7 Reserved (0)
 *
 *     cbRes1    Standard return codes (0, -1, -2)
 *     cbRes2    QuickSet (QS) radio disable bit map:
 *     0 QS disables WLAN
 *     1 QS disables BT
 *     2 QS disables WWAN
 *     3 QS disables UWB
 *     4 QS disables WIGIG
 *     5-31 Reserved (0)
 *
 * Wireless Switch Configuration
 *     cbArg1, byte0 = 0x02
 *
 *     cbArg1, byte1
 *     Subcommand:
 *     0 Get config
 *     1 Set config
 *     2 Set WiFi locator enable/disable
 *     cbArg1,byte2
 *     Switch settings (if byte 1==1):
 *     0 WLAN sw itch control (1)
 *     1 BT sw itch control (1)
 *     2 WWAN sw itch control (1)
 *     3 UWB sw itch control (1)
 *     4 WiGig sw itch control (1)
 *     5-7 Reserved (0)
 *    cbArg1, byte2 Enable bits (if byte 1==2):
 *     0 Enable WiFi locator (1)
 *
 *    cbRes1     Standard return codes (0, -1, -2)
 *    cbRes2 QuickSet radio disable bit map:
 *     0 WLAN controlled by sw itch (1)
 *     1 BT controlled by sw itch (1)
 *     2 WWAN controlled by sw itch (1)
 *     3 UWB controlled by sw itch (1)
 *     4 WiGig controlled by sw itch (1)
 *     5-6 Reserved (0)
 *     7 Wireless sw itch config locked (1)
 *     8 WiFi locator enabled (1)
 *     9-14 Reserved (0)
 *     15 WiFi locator setting locked (1)
 *     16-31 Reserved (0)
 *
 * Read Local Config Data (LCD)
 *     cbArg1, byte0 = 0x10
 *     cbArg1, byte1 NVRAM index low byte
 *     cbArg1, byte2 NVRAM index high byte
 *     cbRes1 Standard return codes (0, -1, -2)
 *     cbRes2 4 bytes read from LCD[index]
 *     cbRes3 4 bytes read from LCD[index+4]
 *     cbRes4 4 bytes read from LCD[index+8]
 *
 * Write Local Config Data (LCD)
 *     cbArg1, byte0 = 0x11
 *     cbArg1, byte1 NVRAM index low byte
 *     cbArg1, byte2 NVRAM index high byte
 *     cbArg2 4 bytes to w rite at LCD[index]
 *     cbArg3 4 bytes to w rite at LCD[index+4]
 *     cbArg4 4 bytes to w rite at LCD[index+8]
 *     cbRes1 Standard return codes (0, -1, -2)
 *
 * Populate Local Config Data from NVRAM
 *     cbArg1, byte0 = 0x12
 *     cbRes1 Standard return codes (0, -1, -2)
 *
 * Commit Local Config Data to NVRAM
 *     cbArg1, byte0 = 0x13
 *     cbRes1 Standard return codes (0, -1, -2)
 */

static int dell_rfkill_set(void *data, bool blocked)
{
	int disable = blocked ? 1 : 0;
	unsigned long radio = (unsigned long)data;
	int hwswitch_bit = (unsigned long)data - 1;
	struct calling_interface_buffer buffer;
	int hwswitch;
	int status;
	int ret;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	if (ret)
		return ret;
	status = buffer.output[1];

	dell_fill_request(&buffer, 0x2, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	if (ret)
		return ret;
	hwswitch = buffer.output[1];

	/* If the hardware switch controls this radio, and the hardware
	   switch is disabled, always disable the radio */
	if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
	    (status & BIT(0)) && !(status & BIT(16)))
		disable = 1;

	dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	return ret;
}

static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
					int status)
{
	if (status & BIT(0)) {
		/* Has hw-switch, sync sw_state to BIOS */
		struct calling_interface_buffer buffer;
		int block = rfkill_blocked(rfkill);
		dell_fill_request(&buffer,
				   1 | (radio << 8) | (block << 16), 0, 0, 0);
		dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	} else {
		/* No hw-switch, sync BIOS state to sw_state */
		rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
	}
}

static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
					int status, int hwswitch)
{
	if (hwswitch & (BIT(radio - 1)))
		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
}

static void dell_rfkill_query(struct rfkill *rfkill, void *data)
{
	int radio = ((unsigned long)data & 0xF);
	struct calling_interface_buffer buffer;
	int hwswitch;
	int status;
	int ret;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	status = buffer.output[1];

	if (ret != 0 || !(status & BIT(0))) {
		return;
	}

	dell_fill_request(&buffer, 0x2, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	hwswitch = buffer.output[1];

	if (ret != 0)
		return;

	dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
}

static const struct rfkill_ops dell_rfkill_ops = {
	.set_block = dell_rfkill_set,
	.query = dell_rfkill_query,
};

static struct dentry *dell_laptop_dir;

static int dell_debugfs_show(struct seq_file *s, void *data)
{
	struct calling_interface_buffer buffer;
	int hwswitch_state;
	int hwswitch_ret;
	int status;
	int ret;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	if (ret)
		return ret;
	status = buffer.output[1];

	dell_fill_request(&buffer, 0x2, 0, 0, 0);
	hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	if (hwswitch_ret)
		return hwswitch_ret;
	hwswitch_state = buffer.output[1];

	seq_printf(s, "return:\t%d\n", ret);
	seq_printf(s, "status:\t0x%X\n", status);
	seq_printf(s, "Bit 0 : Hardware switch supported:   %lu\n",
		   status & BIT(0));
	seq_printf(s, "Bit 1 : Wifi locator supported:      %lu\n",
		  (status & BIT(1)) >> 1);
	seq_printf(s, "Bit 2 : Wifi is supported:           %lu\n",
		  (status & BIT(2)) >> 2);
	seq_printf(s, "Bit 3 : Bluetooth is supported:      %lu\n",
		  (status & BIT(3)) >> 3);
	seq_printf(s, "Bit 4 : WWAN is supported:           %lu\n",
		  (status & BIT(4)) >> 4);
	seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
		  (status & BIT(5)) >> 5);
	seq_printf(s, "Bit 6 : UWB supported:               %lu\n",
		  (status & BIT(6)) >> 6);
	seq_printf(s, "Bit 7 : WiGig supported:             %lu\n",
		  (status & BIT(7)) >> 7);
	seq_printf(s, "Bit 8 : Wifi is installed:           %lu\n",
		  (status & BIT(8)) >> 8);
	seq_printf(s, "Bit 9 : Bluetooth is installed:      %lu\n",
		  (status & BIT(9)) >> 9);
	seq_printf(s, "Bit 10: WWAN is installed:           %lu\n",
		  (status & BIT(10)) >> 10);
	seq_printf(s, "Bit 11: UWB installed:               %lu\n",
		  (status & BIT(11)) >> 11);
	seq_printf(s, "Bit 12: WiGig installed:             %lu\n",
		  (status & BIT(12)) >> 12);

	seq_printf(s, "Bit 16: Hardware switch is on:       %lu\n",
		  (status & BIT(16)) >> 16);
	seq_printf(s, "Bit 17: Wifi is blocked:             %lu\n",
		  (status & BIT(17)) >> 17);
	seq_printf(s, "Bit 18: Bluetooth is blocked:        %lu\n",
		  (status & BIT(18)) >> 18);
	seq_printf(s, "Bit 19: WWAN is blocked:             %lu\n",
		  (status & BIT(19)) >> 19);
	seq_printf(s, "Bit 20: UWB is blocked:              %lu\n",
		  (status & BIT(20)) >> 20);
	seq_printf(s, "Bit 21: WiGig is blocked:            %lu\n",
		  (status & BIT(21)) >> 21);

	seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
	seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
	seq_printf(s, "Bit 0 : Wifi controlled by switch:      %lu\n",
		   hwswitch_state & BIT(0));
	seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
		   (hwswitch_state & BIT(1)) >> 1);
	seq_printf(s, "Bit 2 : WWAN controlled by switch:      %lu\n",
		   (hwswitch_state & BIT(2)) >> 2);
	seq_printf(s, "Bit 3 : UWB controlled by switch:       %lu\n",
		   (hwswitch_state & BIT(3)) >> 3);
	seq_printf(s, "Bit 4 : WiGig controlled by switch:     %lu\n",
		   (hwswitch_state & BIT(4)) >> 4);
	seq_printf(s, "Bit 7 : Wireless switch config locked:  %lu\n",
		   (hwswitch_state & BIT(7)) >> 7);
	seq_printf(s, "Bit 8 : Wifi locator enabled:           %lu\n",
		   (hwswitch_state & BIT(8)) >> 8);
	seq_printf(s, "Bit 15: Wifi locator setting locked:    %lu\n",
		   (hwswitch_state & BIT(15)) >> 15);

	return 0;
}
DEFINE_SHOW_ATTRIBUTE(dell_debugfs);

static void dell_update_rfkill(struct work_struct *ignored)
{
	struct calling_interface_buffer buffer;
	int hwswitch = 0;
	int status;
	int ret;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	status = buffer.output[1];

	if (ret != 0)
		return;

	dell_fill_request(&buffer, 0x2, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);

	if (ret == 0 && (status & BIT(0)))
		hwswitch = buffer.output[1];

	if (wifi_rfkill) {
		dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
		dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
	}
	if (bluetooth_rfkill) {
		dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
					    hwswitch);
		dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
	}
	if (wwan_rfkill) {
		dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
		dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
	}
}
static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);

static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
			      struct serio *port)
{
	static bool extended;

	if (str & I8042_STR_AUXDATA)
		return false;

	if (unlikely(data == 0xe0)) {
		extended = true;
		return false;
	} else if (unlikely(extended)) {
		switch (data) {
		case 0x8:
			schedule_delayed_work(&dell_rfkill_work,
					      round_jiffies_relative(HZ / 4));
			break;
		}
		extended = false;
	}

	return false;
}

static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);

static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
					  unsigned long action, void *data)
{
	schedule_delayed_work(&dell_rfkill_work, 0);
	return NOTIFY_OK;
}

static struct notifier_block dell_laptop_rbtn_notifier = {
	.notifier_call = dell_laptop_rbtn_notifier_call,
};

static int __init dell_setup_rfkill(void)
{
	struct calling_interface_buffer buffer;
	int status, ret, whitelisted;
	const char *product;

	/*
	 * rfkill support causes trouble on various models, mostly Inspirons.
	 * So we whitelist certain series, and don't support rfkill on others.
	 */
	whitelisted = 0;
	product = dmi_get_system_info(DMI_PRODUCT_NAME);
	if (product &&  (strncmp(product, "Latitude", 8) == 0 ||
			 strncmp(product, "Precision", 9) == 0))
		whitelisted = 1;
	if (!force_rfkill && !whitelisted)
		return 0;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
	status = buffer.output[1];

	/* dell wireless info smbios call is not supported */
	if (ret != 0)
		return 0;

	/* rfkill is only tested on laptops with a hwswitch */
	if (!(status & BIT(0)) && !force_rfkill)
		return 0;

	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
					   RFKILL_TYPE_WLAN,
					   &dell_rfkill_ops, (void *) 1);
		if (!wifi_rfkill) {
			ret = -ENOMEM;
			goto err_wifi;
		}
		ret = rfkill_register(wifi_rfkill);
		if (ret)
			goto err_wifi;
	}

	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
						&platform_device->dev,
						RFKILL_TYPE_BLUETOOTH,
						&dell_rfkill_ops, (void *) 2);
		if (!bluetooth_rfkill) {
			ret = -ENOMEM;
			goto err_bluetooth;
		}
		ret = rfkill_register(bluetooth_rfkill);
		if (ret)
			goto err_bluetooth;
	}

	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
		wwan_rfkill = rfkill_alloc("dell-wwan",
					   &platform_device->dev,
					   RFKILL_TYPE_WWAN,
					   &dell_rfkill_ops, (void *) 3);
		if (!wwan_rfkill) {
			ret = -ENOMEM;
			goto err_wwan;
		}
		ret = rfkill_register(wwan_rfkill);
		if (ret)
			goto err_wwan;
	}

	/*
	 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
	 * which can receive events from HW slider switch.
	 *
	 * Dell SMBIOS on whitelisted models supports controlling radio devices
	 * but does not support receiving HW button switch events. We can use
	 * i8042 filter hook function to receive keyboard data and handle
	 * keycode for HW button.
	 *
	 * So if it is possible we will use Dell Airplane Mode Switch ACPI
	 * driver for receiving HW events and Dell SMBIOS for setting rfkill
	 * states. If ACPI driver or device is not available we will fallback to
	 * i8042 filter hook function.
	 *
	 * To prevent duplicate rfkill devices which control and do same thing,
	 * dell-rbtn driver will automatically remove its own rfkill devices
	 * once function dell_rbtn_notifier_register() is called.
	 */

	dell_rbtn_notifier_register_func =
		symbol_request(dell_rbtn_notifier_register);
	if (dell_rbtn_notifier_register_func) {
		dell_rbtn_notifier_unregister_func =
			symbol_request(dell_rbtn_notifier_unregister);
		if (!dell_rbtn_notifier_unregister_func) {
			symbol_put(dell_rbtn_notifier_register);
			dell_rbtn_notifier_register_func = NULL;
		}
	}

	if (dell_rbtn_notifier_register_func) {
		ret = dell_rbtn_notifier_register_func(
			&dell_laptop_rbtn_notifier);
		symbol_put(dell_rbtn_notifier_register);
		dell_rbtn_notifier_register_func = NULL;
		if (ret != 0) {
			symbol_put(dell_rbtn_notifier_unregister);
			dell_rbtn_notifier_unregister_func = NULL;
		}
	} else {
		pr_info("Symbols from dell-rbtn acpi driver are not available\n");
		ret = -ENODEV;
	}

	if (ret == 0) {
		pr_info("Using dell-rbtn acpi driver for receiving events\n");
	} else if (ret != -ENODEV) {
		pr_warn("Unable to register dell rbtn notifier\n");
		goto err_filter;
	} else {
		ret = i8042_install_filter(dell_laptop_i8042_filter);
		if (ret) {
			pr_warn("Unable to install key filter\n");
			goto err_filter;
		}
		pr_info("Using i8042 filter function for receiving events\n");
	}

	return 0;
err_filter:
	if (wwan_rfkill)
		rfkill_unregister(wwan_rfkill);
err_wwan:
	rfkill_destroy(wwan_rfkill);
	if (bluetooth_rfkill)
		rfkill_unregister(bluetooth_rfkill);
err_bluetooth:
	rfkill_destroy(bluetooth_rfkill);
	if (wifi_rfkill)
		rfkill_unregister(wifi_rfkill);
err_wifi:
	rfkill_destroy(wifi_rfkill);

	return ret;
}

static void dell_cleanup_rfkill(void)
{
	if (dell_rbtn_notifier_unregister_func) {
		dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
		symbol_put(dell_rbtn_notifier_unregister);
		dell_rbtn_notifier_unregister_func = NULL;
	} else {
		i8042_remove_filter(dell_laptop_i8042_filter);
	}
	cancel_delayed_work_sync(&dell_rfkill_work);
	if (wifi_rfkill) {
		rfkill_unregister(wifi_rfkill);
		rfkill_destroy(wifi_rfkill);
	}
	if (bluetooth_rfkill) {
		rfkill_unregister(bluetooth_rfkill);
		rfkill_destroy(bluetooth_rfkill);
	}
	if (wwan_rfkill) {
		rfkill_unregister(wwan_rfkill);
		rfkill_destroy(wwan_rfkill);
	}
}

static int dell_send_intensity(struct backlight_device *bd)
{
	struct calling_interface_buffer buffer;
	struct calling_interface_token *token;
	int ret;

	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
	if (!token)
		return -ENODEV;

	dell_fill_request(&buffer,
			   token->location, bd->props.brightness, 0, 0);
	if (power_supply_is_system_supplied() > 0)
		ret = dell_send_request(&buffer,
					CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
	else
		ret = dell_send_request(&buffer,
					CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);

	return ret;
}

static int dell_get_intensity(struct backlight_device *bd)
{
	struct calling_interface_buffer buffer;
	struct calling_interface_token *token;
	int ret;

	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
	if (!token)
		return -ENODEV;

	dell_fill_request(&buffer, token->location, 0, 0, 0);
	if (power_supply_is_system_supplied() > 0)
		ret = dell_send_request(&buffer,
					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
	else
		ret = dell_send_request(&buffer,
					CLASS_TOKEN_READ, SELECT_TOKEN_BAT);

	if (ret == 0)
		ret = buffer.output[1];

	return ret;
}

static const struct backlight_ops dell_ops = {
	.get_brightness = dell_get_intensity,
	.update_status  = dell_send_intensity,
};

static void touchpad_led_on(void)
{
	int command = 0x97;
	char data = 1;
	i8042_command(&data, command | 1 << 12);
}

static void touchpad_led_off(void)
{
	int command = 0x97;
	char data = 2;
	i8042_command(&data, command | 1 << 12);
}

static void touchpad_led_set(struct led_classdev *led_cdev,
	enum led_brightness value)
{
	if (value > 0)
		touchpad_led_on();
	else
		touchpad_led_off();
}

static struct led_classdev touchpad_led = {
	.name = "dell-laptop::touchpad",
	.brightness_set = touchpad_led_set,
	.flags = LED_CORE_SUSPENDRESUME,
};

static int __init touchpad_led_init(struct device *dev)
{
	return led_classdev_register(dev, &touchpad_led);
}

static void touchpad_led_exit(void)
{
	led_classdev_unregister(&touchpad_led);
}

/*
 * Derived from information in smbios-keyboard-ctl:
 *
 * cbClass 4
 * cbSelect 11
 * Keyboard illumination
 * cbArg1 determines the function to be performed
 *
 * cbArg1 0x0 = Get Feature Information
 *  cbRES1         Standard return codes (0, -1, -2)
 *  cbRES2, word0  Bitmap of user-selectable modes
 *     bit 0     Always off (All systems)
 *     bit 1     Always on (Travis ATG, Siberia)
 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
 *     bit 4     Auto: Input-activity-based On; input-activity based Off
 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
 *     bits 9-15 Reserved for future use
 *  cbRES2, byte2  Reserved for future use
 *  cbRES2, byte3  Keyboard illumination type
 *     0         Reserved
 *     1         Tasklight
 *     2         Backlight
 *     3-255     Reserved for future use
 *  cbRES3, byte0  Supported auto keyboard illumination trigger bitmap.
 *     bit 0     Any keystroke
 *     bit 1     Touchpad activity
 *     bit 2     Pointing stick
 *     bit 3     Any mouse
 *     bits 4-7  Reserved for future use
 *  cbRES3, byte1  Supported timeout unit bitmap
 *     bit 0     Seconds
 *     bit 1     Minutes
 *     bit 2     Hours
 *     bit 3     Days
 *     bits 4-7  Reserved for future use
 *  cbRES3, byte2  Number of keyboard light brightness levels
 *  cbRES4, byte0  Maximum acceptable seconds value (0 if seconds not supported).
 *  cbRES4, byte1  Maximum acceptable minutes value (0 if minutes not supported).
 *  cbRES4, byte2  Maximum acceptable hours value (0 if hours not supported).
 *  cbRES4, byte3  Maximum acceptable days value (0 if days not supported)
 *
 * cbArg1 0x1 = Get Current State
 *  cbRES1         Standard return codes (0, -1, -2)
 *  cbRES2, word0  Bitmap of current mode state
 *     bit 0     Always off (All systems)
 *     bit 1     Always on (Travis ATG, Siberia)
 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
 *     bit 4     Auto: Input-activity-based On; input-activity based Off
 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
 *     bits 9-15 Reserved for future use
 *     Note: Only One bit can be set
 *  cbRES2, byte2  Currently active auto keyboard illumination triggers.
 *     bit 0     Any keystroke
 *     bit 1     Touchpad activity
 *     bit 2     Pointing stick
 *     bit 3     Any mouse
 *     bits 4-7  Reserved for future use
 *  cbRES2, byte3  Current Timeout on battery
 *     bits 7:6  Timeout units indicator:
 *     00b       Seconds
 *     01b       Minutes
 *     10b       Hours
 *     11b       Days
 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
 *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
 *     are set upon return from the [Get feature information] call.
 *  cbRES3, byte0  Current setting of ALS value that turns the light on or off.
 *  cbRES3, byte1  Current ALS reading
 *  cbRES3, byte2  Current keyboard light level.
 *  cbRES3, byte3  Current timeout on AC Power
 *     bits 7:6  Timeout units indicator:
 *     00b       Seconds
 *     01b       Minutes
 *     10b       Hours
 *     11b       Days
 *     Bits 5:0  Timeout value (0-63) in sec/min/hr/day
 *     NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
 *     are set upon return from the upon return from the [Get Feature information] call.
 *
 * cbArg1 0x2 = Set New State
 *  cbRES1         Standard return codes (0, -1, -2)
 *  cbArg2, word0  Bitmap of current mode state
 *     bit 0     Always off (All systems)
 *     bit 1     Always on (Travis ATG, Siberia)
 *     bit 2     Auto: ALS-based On; ALS-based Off (Travis ATG)
 *     bit 3     Auto: ALS- and input-activity-based On; input-activity based Off
 *     bit 4     Auto: Input-activity-based On; input-activity based Off
 *     bit 5     Auto: Input-activity-based On (illumination level 25%); input-activity based Off
 *     bit 6     Auto: Input-activity-based On (illumination level 50%); input-activity based Off
 *     bit 7     Auto: Input-activity-based On (illumination level 75%); input-activity based Off
 *     bit 8     Auto: Input-activity-based On (illumination level 100%); input-activity based Off
 *     bits 9-15 Reserved for future use
 *     Note: Only One bit can be set
 *  cbArg2, byte2  Desired auto keyboard illumination triggers. Must remain inactive to allow
 *                 keyboard to turn off automatically.
 *     bit 0     Any keystroke
 *     bit 1     Touchpad activity
 *     bit 2     Pointing stick
 *     bit 3     Any mouse
 *     bits 4-7  Reserved for future use
 *  cbArg2, byte3  Desired Timeout on battery
 *     bits 7:6  Timeout units indicator:
 *     00b       Seconds
 *     01b       Minutes
 *     10b       Hours
 *     11b       Days
 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
 *  cbArg3, byte0  Desired setting of ALS value that turns the light on or off.
 *  cbArg3, byte2  Desired keyboard light level.
 *  cbArg3, byte3  Desired Timeout on AC power
 *     bits 7:6  Timeout units indicator:
 *     00b       Seconds
 *     01b       Minutes
 *     10b       Hours
 *     11b       Days
 *     bits 5:0  Timeout value (0-63) in sec/min/hr/day
 */


enum kbd_timeout_unit {
	KBD_TIMEOUT_SECONDS = 0,
	KBD_TIMEOUT_MINUTES,
	KBD_TIMEOUT_HOURS,
	KBD_TIMEOUT_DAYS,
};

enum kbd_mode_bit {
	KBD_MODE_BIT_OFF = 0,
	KBD_MODE_BIT_ON,
	KBD_MODE_BIT_ALS,
	KBD_MODE_BIT_TRIGGER_ALS,
	KBD_MODE_BIT_TRIGGER,
	KBD_MODE_BIT_TRIGGER_25,
	KBD_MODE_BIT_TRIGGER_50,
	KBD_MODE_BIT_TRIGGER_75,
	KBD_MODE_BIT_TRIGGER_100,
};

#define kbd_is_als_mode_bit(bit) \
	((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
#define kbd_is_trigger_mode_bit(bit) \
	((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
#define kbd_is_level_mode_bit(bit) \
	((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)

struct kbd_info {
	u16 modes;
	u8 type;
	u8 triggers;
	u8 levels;
	u8 seconds;
	u8 minutes;
	u8 hours;
	u8 days;
};

struct kbd_state {
	u8 mode_bit;
	u8 triggers;
	u8 timeout_value;
	u8 timeout_unit;
	u8 timeout_value_ac;
	u8 timeout_unit_ac;
	u8 als_setting;
	u8 als_value;
	u8 level;
};

static const int kbd_tokens[] = {
	KBD_LED_OFF_TOKEN,
	KBD_LED_AUTO_25_TOKEN,
	KBD_LED_AUTO_50_TOKEN,
	KBD_LED_AUTO_75_TOKEN,
	KBD_LED_AUTO_100_TOKEN,
	KBD_LED_ON_TOKEN,
};

static u16 kbd_token_bits;

static struct kbd_info kbd_info;
static bool kbd_als_supported;
static bool kbd_triggers_supported;
static bool kbd_timeout_ac_supported;

static u8 kbd_mode_levels[16];
static int kbd_mode_levels_count;

static u8 kbd_previous_level;
static u8 kbd_previous_mode_bit;

static bool kbd_led_present;
static DEFINE_MUTEX(kbd_led_mutex);
static enum led_brightness kbd_led_level;

/*
 * NOTE: there are three ways to set the keyboard backlight level.
 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
 *
 * There are laptops which support only one of these methods. If we want to
 * support as many machines as possible we need to implement all three methods.
 * The first two methods use the kbd_state structure. The third uses SMBIOS
 * tokens. If kbd_info.levels == 0, the machine does not support setting the
 * keyboard backlight level via kbd_state.level.
 */

static int kbd_get_info(struct kbd_info *info)
{
	struct calling_interface_buffer buffer;
	u8 units;
	int ret;

	dell_fill_request(&buffer, 0, 0, 0, 0);
	ret = dell_send_request(&buffer,
				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
	if (ret)
		return ret;

	info->modes = buffer.output[1] & 0xFFFF;
	info->type = (buffer.output[1] >> 24) & 0xFF;
	info->triggers = buffer.output[2] & 0xFF;
	units = (buffer.output[2] >> 8) & 0xFF;
	info->levels = (buffer.output[2] >> 16) & 0xFF;

	if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
		info->levels--;

	if (units & BIT(0))
		info->seconds = (buffer.output[3] >> 0) & 0xFF;
	if (units & BIT(1))
		info->minutes = (buffer.output[3] >> 8) & 0xFF;
	if (units & BIT(2))
		info->hours = (buffer.output[3] >> 16) & 0xFF;
	if (units & BIT(3))
		info->days = (buffer.output[3] >> 24) & 0xFF;

	return ret;
}

static unsigned int kbd_get_max_level(void)
{
	if (kbd_info.levels != 0)
		return kbd_info.levels;
	if (kbd_mode_levels_count > 0)
		return kbd_mode_levels_count - 1;
	return 0;
}

static int kbd_get_level(struct kbd_state *state)
{
	int i;

	if (kbd_info.levels != 0)
		return state->level;

	if (kbd_mode_levels_count > 0) {
		for (i = 0; i < kbd_mode_levels_count; ++i)
			if (kbd_mode_levels[i] == state->mode_bit)
				return i;
		return 0;
	}

	return -EINVAL;
}

static int kbd_set_level(struct kbd_state *state, u8 level)
{
	if (kbd_info.levels != 0) {
		if (level != 0)
			kbd_previous_level = level;
		if (state->level == level)
			return 0;
		state->level = level;
		if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
			state->mode_bit = kbd_previous_mode_bit;
		else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
			kbd_previous_mode_bit = state->mode_bit;
			state->mode_bit = KBD_MODE_BIT_OFF;
		}
		return 0;
	}

	if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
		if (level != 0)
			kbd_previous_level = level;
		state->mode_bit = kbd_mode_levels[level];
		return 0;
	}

	return -EINVAL;
}

static int kbd_get_state(struct kbd_state *state)
{
	struct calling_interface_buffer buffer;
	int ret;

	dell_fill_request(&buffer, 0x1, 0, 0, 0);
	ret = dell_send_request(&buffer,
				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
	if (ret)
		return ret;

	state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
	if (state->mode_bit != 0)
		state->mode_bit--;

	state->triggers = (buffer.output[1] >> 16) & 0xFF;
	state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
	state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
	state->als_setting = buffer.output[2] & 0xFF;
	state->als_value = (buffer.output[2] >> 8) & 0xFF;
	state->level = (buffer.output[2] >> 16) & 0xFF;
	state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
	state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;

	return ret;
}

static int kbd_set_state(struct kbd_state *state)
{
	struct calling_interface_buffer buffer;
	int ret;
	u32 input1;
	u32 input2;

	input1 = BIT(state->mode_bit) & 0xFFFF;
	input1 |= (state->triggers & 0xFF) << 16;
	input1 |= (state->timeout_value & 0x3F) << 24;
	input1 |= (state->timeout_unit & 0x3) << 30;
	input2 = state->als_setting & 0xFF;
	input2 |= (state->level & 0xFF) << 16;
	input2 |= (state->timeout_value_ac & 0x3F) << 24;
	input2 |= (state->timeout_unit_ac & 0x3) << 30;
	dell_fill_request(&buffer, 0x2, input1, input2, 0);
	ret = dell_send_request(&buffer,
				CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);

	return ret;
}

static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
{
	int ret;

	ret = kbd_set_state(state);
	if (ret == 0)
		return 0;

	/*
	 * When setting the new state fails,try to restore the previous one.
	 * This is needed on some machines where BIOS sets a default state when
	 * setting a new state fails. This default state could be all off.
	 */

	if (kbd_set_state(old))
		pr_err("Setting old previous keyboard state failed\n");

	return ret;
}

static int kbd_set_token_bit(u8 bit)
{
	struct calling_interface_buffer buffer;
	struct calling_interface_token *token;
	int ret;

	if (bit >= ARRAY_SIZE(kbd_tokens))
		return -EINVAL;

	token = dell_smbios_find_token(kbd_tokens[bit]);
	if (!token)
		return -EINVAL;

	dell_fill_request(&buffer, token->location, token->value, 0, 0);
	ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);

	return ret;
}

static int kbd_get_token_bit(u8 bit)
{
	struct calling_interface_buffer buffer;
	struct calling_interface_token *token;
	int ret;
	int val;

	if (bit >= ARRAY_SIZE(kbd_tokens))
		return -EINVAL;

	token = dell_smbios_find_token(kbd_tokens[bit]);
	if (!token)
		return -EINVAL;

	dell_fill_request(&buffer, token->location, 0, 0, 0);
	ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
	val = buffer.output[1];

	if (ret)
		return ret;

	return (val == token->value);
}

static int kbd_get_first_active_token_bit(void)
{
	int i;
	int ret;

	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
		ret = kbd_get_token_bit(i);
		if (ret == 1)
			return i;
	}

	return ret;
}

static int kbd_get_valid_token_counts(void)
{
	return hweight16(kbd_token_bits);
}

static inline int kbd_init_info(void)
{
	struct kbd_state state;
	int ret;
	int i;

	ret = kbd_get_info(&kbd_info);
	if (ret)
		return ret;

	/* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
	 *       timeout value which is shared for both battery and AC power
	 *       settings. So do not try to set AC values on old models.
	 */
	if ((quirks && quirks->kbd_missing_ac_tag) ||
	    dell_smbios_find_token(KBD_LED_AC_TOKEN))
		kbd_timeout_ac_supported = true;

	kbd_get_state(&state);

	/* NOTE: timeout value is stored in 6 bits so max value is 63 */
	if (kbd_info.seconds > 63)
		kbd_info.seconds = 63;
	if (kbd_info.minutes > 63)
		kbd_info.minutes = 63;
	if (kbd_info.hours > 63)
		kbd_info.hours = 63;
	if (kbd_info.days > 63)
		kbd_info.days = 63;

	/* NOTE: On tested machines ON mode did not work and caused
	 *       problems (turned backlight off) so do not use it
	 */
	kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);

	kbd_previous_level = kbd_get_level(&state);
	kbd_previous_mode_bit = state.mode_bit;

	if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
		kbd_previous_level = 1;

	if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
		kbd_previous_mode_bit =
			ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
		if (kbd_previous_mode_bit != 0)
			kbd_previous_mode_bit--;
	}

	if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
			      BIT(KBD_MODE_BIT_TRIGGER_ALS)))
		kbd_als_supported = true;

	if (kbd_info.modes & (
	    BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
	    BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
	    BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
	   ))
		kbd_triggers_supported = true;

	/* kbd_mode_levels[0] is reserved, see below */
	for (i = 0; i < 16; ++i)
		if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
			kbd_mode_levels[1 + kbd_mode_levels_count++] = i;

	/*
	 * Find the first supported mode and assign to kbd_mode_levels[0].
	 * This should be 0 (off), but we cannot depend on the BIOS to
	 * support 0.
	 */
	if (kbd_mode_levels_count > 0) {
		for (i = 0; i < 16; ++i) {
			if (BIT(i) & kbd_info.modes) {
				kbd_mode_levels[0] = i;
				break;
			}
		}
		kbd_mode_levels_count++;
	}

	return 0;

}

static inline void kbd_init_tokens(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
		if (dell_smbios_find_token(kbd_tokens[i]))
			kbd_token_bits |= BIT(i);
}

static void kbd_init(void)
{
	int ret;

	ret = kbd_init_info();
	kbd_init_tokens();

	/*
	 * Only supports keyboard backlight when it has at least two modes.
	 */
	if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
	    || kbd_get_valid_token_counts() >= 2)
		kbd_led_present = true;
}

static ssize_t kbd_led_timeout_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct kbd_state new_state;
	struct kbd_state state;
	bool convert;
	int value;
	int ret;
	char ch;
	u8 unit;
	int i;

	ret = sscanf(buf, "%d %c", &value, &ch);
	if (ret < 1)
		return -EINVAL;
	else if (ret == 1)
		ch = 's';

	if (value < 0)
		return -EINVAL;

	convert = false;

	switch (ch) {
	case 's':
		if (value > kbd_info.seconds)
			convert = true;
		unit = KBD_TIMEOUT_SECONDS;
		break;
	case 'm':
		if (value > kbd_info.minutes)
			convert = true;
		unit = KBD_TIMEOUT_MINUTES;
		break;
	case 'h':
		if (value > kbd_info.hours)
			convert = true;
		unit = KBD_TIMEOUT_HOURS;
		break;
	case 'd':
		if (value > kbd_info.days)
			convert = true;
		unit = KBD_TIMEOUT_DAYS;
		break;
	default:
		return -EINVAL;
	}

	if (quirks && quirks->needs_kbd_timeouts)
		convert = true;

	if (convert) {
		/* Convert value from current units to seconds */
		switch (unit) {
		case KBD_TIMEOUT_DAYS:
			value *= 24;
			/* fall through */
		case KBD_TIMEOUT_HOURS:
			value *= 60;
			/* fall through */
		case KBD_TIMEOUT_MINUTES:
			value *= 60;
			unit = KBD_TIMEOUT_SECONDS;
		}

		if (quirks && quirks->needs_kbd_timeouts) {
			for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
				if (value <= quirks->kbd_timeouts[i]) {
					value = quirks->kbd_timeouts[i];
					break;
				}
			}
		}

		if (value <= kbd_info.seconds && kbd_info.seconds) {
			unit = KBD_TIMEOUT_SECONDS;
		} else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
			value /= 60;
			unit = KBD_TIMEOUT_MINUTES;
		} else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
			value /= (60 * 60);
			unit = KBD_TIMEOUT_HOURS;
		} else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
			value /= (60 * 60 * 24);
			unit = KBD_TIMEOUT_DAYS;
		} else {
			return -EINVAL;
		}
	}

	mutex_lock(&kbd_led_mutex);

	ret = kbd_get_state(&state);
	if (ret)
		goto out;

	new_state = state;

	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
		new_state.timeout_value_ac = value;
		new_state.timeout_unit_ac = unit;
	} else {
		new_state.timeout_value = value;
		new_state.timeout_unit = unit;
	}

	ret = kbd_set_state_safe(&new_state, &state);
	if (ret)
		goto out;

	ret = count;
out:
	mutex_unlock(&kbd_led_mutex);
	return ret;
}

static ssize_t kbd_led_timeout_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct kbd_state state;
	int value;
	int ret;
	int len;
	u8 unit;

	ret = kbd_get_state(&state);
	if (ret)
		return ret;

	if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
		value = state.timeout_value_ac;
		unit = state.timeout_unit_ac;
	} else {
		value = state.timeout_value;
		unit = state.timeout_unit;
	}

	len = sprintf(buf, "%d", value);

	switch (unit) {
	case KBD_TIMEOUT_SECONDS:
		return len + sprintf(buf+len, "s\n");
	case KBD_TIMEOUT_MINUTES:
		return len + sprintf(buf+len, "m\n");
	case KBD_TIMEOUT_HOURS:
		return len + sprintf(buf+len, "h\n");
	case KBD_TIMEOUT_DAYS:
		return len + sprintf(buf+len, "d\n");
	default:
		return -EINVAL;
	}

	return len;
}

static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
		   kbd_led_timeout_show, kbd_led_timeout_store);

static const char * const kbd_led_triggers[] = {
	"keyboard",
	"touchpad",
	/*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
	"mouse",
};

static ssize_t kbd_led_triggers_store(struct device *dev,
				      struct device_attribute *attr,
				      const char *buf, size_t count)
{
	struct kbd_state new_state;
	struct kbd_state state;
	bool triggers_enabled = false;
	int trigger_bit = -1;
	char trigger[21];
	int i, ret;

	ret = sscanf(buf, "%20s", trigger);
	if (ret != 1)
		return -EINVAL;

	if (trigger[0] != '+' && trigger[0] != '-')
		return -EINVAL;

	mutex_lock(&kbd_led_mutex);

	ret = kbd_get_state(&state);
	if (ret)
		goto out;

	if (kbd_triggers_supported)
		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);

	if (kbd_triggers_supported) {
		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
			if (!(kbd_info.triggers & BIT(i)))
				continue;
			if (!kbd_led_triggers[i])
				continue;
			if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
				continue;
			if (trigger[0] == '+' &&
			    triggers_enabled && (state.triggers & BIT(i))) {
				ret = count;
				goto out;
			}
			if (trigger[0] == '-' &&
			    (!triggers_enabled || !(state.triggers & BIT(i)))) {
				ret = count;
				goto out;
			}
			trigger_bit = i;
			break;
		}
	}

	if (trigger_bit == -1) {
		ret = -EINVAL;
		goto out;
	}

	new_state = state;
	if (trigger[0] == '+')
		new_state.triggers |= BIT(trigger_bit);
	else {
		new_state.triggers &= ~BIT(trigger_bit);
		/*
		 * NOTE: trackstick bit (2) must be disabled when
		 *       disabling touchpad bit (1), otherwise touchpad
		 *       bit (1) will not be disabled
		 */
		if (trigger_bit == 1)
			new_state.triggers &= ~BIT(2);
	}
	if ((kbd_info.triggers & new_state.triggers) !=
	    new_state.triggers) {
		ret = -EINVAL;
		goto out;
	}
	if (new_state.triggers && !triggers_enabled) {
		new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
		kbd_set_level(&new_state, kbd_previous_level);
	} else if (new_state.triggers == 0) {
		kbd_set_level(&new_state, 0);
	}
	if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
		ret = -EINVAL;
		goto out;
	}
	ret = kbd_set_state_safe(&new_state, &state);
	if (ret)
		goto out;
	if (new_state.mode_bit != KBD_MODE_BIT_OFF)
		kbd_previous_mode_bit = new_state.mode_bit;
	ret = count;
out:
	mutex_unlock(&kbd_led_mutex);
	return ret;
}

static ssize_t kbd_led_triggers_show(struct device *dev,
				     struct device_attribute *attr, char *buf)
{
	struct kbd_state state;
	bool triggers_enabled;
	int level, i, ret;
	int len = 0;

	ret = kbd_get_state(&state);
	if (ret)
		return ret;

	len = 0;

	if (kbd_triggers_supported) {
		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
		level = kbd_get_level(&state);
		for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
			if (!(kbd_info.triggers & BIT(i)))
				continue;
			if (!kbd_led_triggers[i])
				continue;
			if ((triggers_enabled || level <= 0) &&
			    (state.triggers & BIT(i)))
				buf[len++] = '+';
			else
				buf[len++] = '-';
			len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
		}
	}

	if (len)
		buf[len - 1] = '\n';

	return len;
}

static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
		   kbd_led_triggers_show, kbd_led_triggers_store);

static ssize_t kbd_led_als_enabled_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t count)
{
	struct kbd_state new_state;
	struct kbd_state state;
	bool triggers_enabled = false;
	int enable;
	int ret;

	ret = kstrtoint(buf, 0, &enable);
	if (ret)
		return ret;

	mutex_lock(&kbd_led_mutex);

	ret = kbd_get_state(&state);
	if (ret)
		goto out;

	if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
		ret = count;
		goto out;
	}

	new_state = state;

	if (kbd_triggers_supported)
		triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);

	if (enable) {
		if (triggers_enabled)
			new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
		else
			new_state.mode_bit = KBD_MODE_BIT_ALS;
	} else {
		if (triggers_enabled) {
			new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
			kbd_set_level(&new_state, kbd_previous_level);
		} else {
			new_state.mode_bit = KBD_MODE_BIT_ON;
		}
	}
	if (!(kbd_info.modes & BIT(new_state.mode_bit)))  {
		ret = -EINVAL;
		goto out;
	}

	ret = kbd_set_state_safe(&new_state, &state);
	if (ret)
		goto out;
	kbd_previous_mode_bit = new_state.mode_bit;

	ret = count;
out:
	mutex_unlock(&kbd_led_mutex);
	return ret;
}

static ssize_t kbd_led_als_enabled_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct kbd_state state;
	bool enabled = false;
	int ret;

	ret = kbd_get_state(&state);
	if (ret)
		return ret;
	enabled = kbd_is_als_mode_bit(state.mode_bit);

	return sprintf(buf, "%d\n", enabled ? 1 : 0);
}

static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
		   kbd_led_als_enabled_show, kbd_led_als_enabled_store);

static ssize_t kbd_led_als_setting_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t count)
{
	struct kbd_state state;
	struct kbd_state new_state;
	u8 setting;
	int ret;

	ret = kstrtou8(buf, 10, &setting);
	if (ret)
		return ret;

	mutex_lock(&kbd_led_mutex);

	ret = kbd_get_state(&state);
	if (ret)
		goto out;

	new_state = state;
	new_state.als_setting = setting;

	ret = kbd_set_state_safe(&new_state, &state);
	if (ret)
		goto out;

	ret = count;
out:
	mutex_unlock(&kbd_led_mutex);
	return ret;
}

static ssize_t kbd_led_als_setting_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct kbd_state state;
	int ret;

	ret = kbd_get_state(&state);
	if (ret)
		return ret;

	return sprintf(buf, "%d\n", state.als_setting);
}

static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
		   kbd_led_als_setting_show, kbd_led_als_setting_store);

static struct attribute *kbd_led_attrs[] = {
	&dev_attr_stop_timeout.attr,
	&dev_attr_start_triggers.attr,
	NULL,
};

static const struct attribute_group kbd_led_group = {
	.attrs = kbd_led_attrs,
};

static struct attribute *kbd_led_als_attrs[] = {
	&dev_attr_als_enabled.attr,
	&dev_attr_als_setting.attr,
	NULL,
};

static const struct attribute_group kbd_led_als_group = {
	.attrs = kbd_led_als_attrs,
};

static const struct attribute_group *kbd_led_groups[] = {
	&kbd_led_group,
	&kbd_led_als_group,
	NULL,
};

static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
{
	int ret;
	u16 num;
	struct kbd_state state;

	if (kbd_get_max_level()) {
		ret = kbd_get_state(&state);
		if (ret)
			return 0;
		ret = kbd_get_level(&state);
		if (ret < 0)
			return 0;
		return ret;
	}

	if (kbd_get_valid_token_counts()) {
		ret = kbd_get_first_active_token_bit();
		if (ret < 0)
			return 0;
		for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
			num &= num - 1; /* clear the first bit set */
		if (num == 0)
			return 0;
		return ffs(num) - 1;
	}

	pr_warn("Keyboard brightness level control not supported\n");
	return 0;
}

static int kbd_led_level_set(struct led_classdev *led_cdev,
			     enum led_brightness value)
{
	enum led_brightness new_value = value;
	struct kbd_state state;
	struct kbd_state new_state;
	u16 num;
	int ret;

	mutex_lock(&kbd_led_mutex);

	if (kbd_get_max_level()) {
		ret = kbd_get_state(&state);
		if (ret)
			goto out;
		new_state = state;
		ret = kbd_set_level(&new_state, value);
		if (ret)
			goto out;
		ret = kbd_set_state_safe(&new_state, &state);
	} else if (kbd_get_valid_token_counts()) {
		for (num = kbd_token_bits; num != 0 && value > 0; --value)
			num &= num - 1; /* clear the first bit set */
		if (num == 0)
			ret = 0;
		else
			ret = kbd_set_token_bit(ffs(num) - 1);
	} else {
		pr_warn("Keyboard brightness level control not supported\n");
		ret = -ENXIO;
	}

out:
	if (ret == 0)
		kbd_led_level = new_value;

	mutex_unlock(&kbd_led_mutex);
	return ret;
}

static struct led_classdev kbd_led = {
	.name           = "dell::kbd_backlight",
	.flags		= LED_BRIGHT_HW_CHANGED,
	.brightness_set_blocking = kbd_led_level_set,
	.brightness_get = kbd_led_level_get,
	.groups         = kbd_led_groups,
};

static int __init kbd_led_init(struct device *dev)
{
	int ret;

	kbd_init();
	if (!kbd_led_present)
		return -ENODEV;
	if (!kbd_als_supported)
		kbd_led_groups[1] = NULL;
	kbd_led.max_brightness = kbd_get_max_level();
	if (!kbd_led.max_brightness) {
		kbd_led.max_brightness = kbd_get_valid_token_counts();
		if (kbd_led.max_brightness)
			kbd_led.max_brightness--;
	}

	kbd_led_level = kbd_led_level_get(NULL);

	ret = led_classdev_register(dev, &kbd_led);
	if (ret)
		kbd_led_present = false;

	return ret;
}

static void brightness_set_exit(struct led_classdev *led_cdev,
				enum led_brightness value)
{
	/* Don't change backlight level on exit */
};

static void kbd_led_exit(void)
{
	if (!kbd_led_present)
		return;
	kbd_led.brightness_set = brightness_set_exit;
	led_classdev_unregister(&kbd_led);
}

static int dell_laptop_notifier_call(struct notifier_block *nb,
				     unsigned long action, void *data)
{
	bool changed = false;
	enum led_brightness new_kbd_led_level;

	switch (action) {
	case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
		if (!kbd_led_present)
			break;

		mutex_lock(&kbd_led_mutex);
		new_kbd_led_level = kbd_led_level_get(&kbd_led);
		if (kbd_led_level != new_kbd_led_level) {
			kbd_led_level = new_kbd_led_level;
			changed = true;
		}
		mutex_unlock(&kbd_led_mutex);

		if (changed)
			led_classdev_notify_brightness_hw_changed(&kbd_led,
								kbd_led_level);
		break;
	}

	return NOTIFY_OK;
}

static struct notifier_block dell_laptop_notifier = {
	.notifier_call = dell_laptop_notifier_call,
};

static int micmute_led_set(struct led_classdev *led_cdev,
			   enum led_brightness brightness)
{
	struct calling_interface_buffer buffer;
	struct calling_interface_token *token;
	int state = brightness != LED_OFF;

	if (state == 0)
		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
	else
		token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);

	if (!token)
		return -ENODEV;

	dell_fill_request(&buffer, token->location, token->value, 0, 0);
	dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);

	return 0;
}

static struct led_classdev micmute_led_cdev = {
	.name = "platform::micmute",
	.max_brightness = 1,
	.brightness_set_blocking = micmute_led_set,
	.default_trigger = "audio-micmute",
};

static int __init dell_init(void)
{
	struct calling_interface_token *token;
	int max_intensity = 0;
	int ret;

	if (!dmi_check_system(dell_device_table))
		return -ENODEV;

	quirks = NULL;
	/* find if this machine support other functions */
	dmi_check_system(dell_quirks);

	ret = platform_driver_register(&platform_driver);
	if (ret)
		goto fail_platform_driver;
	platform_device = platform_device_alloc("dell-laptop", -1);
	if (!platform_device) {
		ret = -ENOMEM;
		goto fail_platform_device1;
	}
	ret = platform_device_add(platform_device);
	if (ret)
		goto fail_platform_device2;

	ret = dell_setup_rfkill();

	if (ret) {
		pr_warn("Unable to setup rfkill\n");
		goto fail_rfkill;
	}

	if (quirks && quirks->touchpad_led)
		touchpad_led_init(&platform_device->dev);

	kbd_led_init(&platform_device->dev);

	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
	if (dell_laptop_dir != NULL)
		debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
				    &dell_debugfs_fops);

	dell_laptop_register_notifier(&dell_laptop_notifier);

	micmute_led_cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
	ret = led_classdev_register(&platform_device->dev, &micmute_led_cdev);
	if (ret < 0)
		goto fail_led;

	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
		return 0;

	token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
	if (token) {
		struct calling_interface_buffer buffer;

		dell_fill_request(&buffer, token->location, 0, 0, 0);
		ret = dell_send_request(&buffer,
					CLASS_TOKEN_READ, SELECT_TOKEN_AC);
		if (ret == 0)
			max_intensity = buffer.output[3];
	}

	if (max_intensity) {
		struct backlight_properties props;
		memset(&props, 0, sizeof(struct backlight_properties));
		props.type = BACKLIGHT_PLATFORM;
		props.max_brightness = max_intensity;
		dell_backlight_device = backlight_device_register("dell_backlight",
								  &platform_device->dev,
								  NULL,
								  &dell_ops,
								  &props);

		if (IS_ERR(dell_backlight_device)) {
			ret = PTR_ERR(dell_backlight_device);
			dell_backlight_device = NULL;
			goto fail_backlight;
		}

		dell_backlight_device->props.brightness =
			dell_get_intensity(dell_backlight_device);
		if (dell_backlight_device->props.brightness < 0) {
			ret = dell_backlight_device->props.brightness;
			goto fail_get_brightness;
		}
		backlight_update_status(dell_backlight_device);
	}

	return 0;

fail_get_brightness:
	backlight_device_unregister(dell_backlight_device);
fail_backlight:
	led_classdev_unregister(&micmute_led_cdev);
fail_led:
	dell_cleanup_rfkill();
fail_rfkill:
	platform_device_del(platform_device);
fail_platform_device2:
	platform_device_put(platform_device);
fail_platform_device1:
	platform_driver_unregister(&platform_driver);
fail_platform_driver:
	return ret;
}

static void __exit dell_exit(void)
{
	dell_laptop_unregister_notifier(&dell_laptop_notifier);
	debugfs_remove_recursive(dell_laptop_dir);
	if (quirks && quirks->touchpad_led)
		touchpad_led_exit();
	kbd_led_exit();
	backlight_device_unregister(dell_backlight_device);
	led_classdev_unregister(&micmute_led_cdev);
	dell_cleanup_rfkill();
	if (platform_device) {
		platform_device_unregister(platform_device);
		platform_driver_unregister(&platform_driver);
	}
}

/* dell-rbtn.c driver export functions which will not work correctly (and could
 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
 * not problem when dell-rbtn.c is compiled as external module. When both files
 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
 * need to ensure that dell_init() will be called after initializing dell-rbtn.
 * This can be achieved by late_initcall() instead module_init().
 */
late_initcall(dell_init);
module_exit(dell_exit);

MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
MODULE_DESCRIPTION("Dell laptop driver");
MODULE_LICENSE("GPL");