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