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