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