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