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