xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / platform / x86 / dell-laptop.c
blob2a68f59d2228c921e8609ccba97832e07cbaa582
1 /*
2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
26 #include <linux/rfkill.h>
27 #include <linux/power_supply.h>
28 #include <linux/acpi.h>
29 #include <linux/mm.h>
30 #include <linux/i8042.h>
31 #include <linux/debugfs.h>
32 #include <linux/dell-led.h>
33 #include <linux/seq_file.h>
34 #include <acpi/video.h>
35 #include "dell-rbtn.h"
36 #include "dell-smbios.h"
38 struct quirk_entry {
39 bool touchpad_led;
40 bool kbd_led_levels_off_1;
42 bool needs_kbd_timeouts;
44 * Ordered list of timeouts expressed in seconds.
45 * The list must end with -1
47 int kbd_timeouts[];
50 static struct quirk_entry *quirks;
52 static struct quirk_entry quirk_dell_vostro_v130 = {
53 .touchpad_led = true,
56 static int __init dmi_matched(const struct dmi_system_id *dmi)
58 quirks = dmi->driver_data;
59 return 1;
63 * These values come from Windows utility provided by Dell. If any other value
64 * is used then BIOS silently set timeout to 0 without any error message.
66 static struct quirk_entry quirk_dell_xps13_9333 = {
67 .needs_kbd_timeouts = true,
68 .kbd_timeouts = { 0, 5, 15, 60, 5 * 60, 15 * 60, -1 },
71 static struct quirk_entry quirk_dell_latitude_e6410 = {
72 .kbd_led_levels_off_1 = true,
75 static struct platform_driver platform_driver = {
76 .driver = {
77 .name = "dell-laptop",
81 static struct platform_device *platform_device;
82 static struct backlight_device *dell_backlight_device;
83 static struct rfkill *wifi_rfkill;
84 static struct rfkill *bluetooth_rfkill;
85 static struct rfkill *wwan_rfkill;
86 static bool force_rfkill;
88 module_param(force_rfkill, bool, 0444);
89 MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
91 static const struct dmi_system_id dell_device_table[] __initconst = {
93 .ident = "Dell laptop",
94 .matches = {
95 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
96 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
100 .matches = {
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
106 .matches = {
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "10"), /*Notebook*/
112 .matches = {
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
118 .matches = {
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
120 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
124 .matches = {
125 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
126 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_CHASSIS_TYPE, "30"), /*Tablet*/
136 .matches = {
137 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
138 DMI_MATCH(DMI_CHASSIS_TYPE, "31"), /*Convertible*/
142 .matches = {
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
144 DMI_MATCH(DMI_CHASSIS_TYPE, "32"), /*Detachable*/
148 .ident = "Dell Computer Corporation",
149 .matches = {
150 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
151 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
156 MODULE_DEVICE_TABLE(dmi, dell_device_table);
158 static const struct dmi_system_id dell_quirks[] __initconst = {
160 .callback = dmi_matched,
161 .ident = "Dell Vostro V130",
162 .matches = {
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
166 .driver_data = &quirk_dell_vostro_v130,
169 .callback = dmi_matched,
170 .ident = "Dell Vostro V131",
171 .matches = {
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
175 .driver_data = &quirk_dell_vostro_v130,
178 .callback = dmi_matched,
179 .ident = "Dell Vostro 3350",
180 .matches = {
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
184 .driver_data = &quirk_dell_vostro_v130,
187 .callback = dmi_matched,
188 .ident = "Dell Vostro 3555",
189 .matches = {
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
193 .driver_data = &quirk_dell_vostro_v130,
196 .callback = dmi_matched,
197 .ident = "Dell Inspiron N311z",
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
202 .driver_data = &quirk_dell_vostro_v130,
205 .callback = dmi_matched,
206 .ident = "Dell Inspiron M5110",
207 .matches = {
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
209 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
211 .driver_data = &quirk_dell_vostro_v130,
214 .callback = dmi_matched,
215 .ident = "Dell Vostro 3360",
216 .matches = {
217 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
218 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
220 .driver_data = &quirk_dell_vostro_v130,
223 .callback = dmi_matched,
224 .ident = "Dell Vostro 3460",
225 .matches = {
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
229 .driver_data = &quirk_dell_vostro_v130,
232 .callback = dmi_matched,
233 .ident = "Dell Vostro 3560",
234 .matches = {
235 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
236 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
238 .driver_data = &quirk_dell_vostro_v130,
241 .callback = dmi_matched,
242 .ident = "Dell Vostro 3450",
243 .matches = {
244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
245 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
247 .driver_data = &quirk_dell_vostro_v130,
250 .callback = dmi_matched,
251 .ident = "Dell Inspiron 5420",
252 .matches = {
253 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
256 .driver_data = &quirk_dell_vostro_v130,
259 .callback = dmi_matched,
260 .ident = "Dell Inspiron 5520",
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
263 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
265 .driver_data = &quirk_dell_vostro_v130,
268 .callback = dmi_matched,
269 .ident = "Dell Inspiron 5720",
270 .matches = {
271 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
272 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
274 .driver_data = &quirk_dell_vostro_v130,
277 .callback = dmi_matched,
278 .ident = "Dell Inspiron 7420",
279 .matches = {
280 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
281 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
283 .driver_data = &quirk_dell_vostro_v130,
286 .callback = dmi_matched,
287 .ident = "Dell Inspiron 7520",
288 .matches = {
289 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
290 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
292 .driver_data = &quirk_dell_vostro_v130,
295 .callback = dmi_matched,
296 .ident = "Dell Inspiron 7720",
297 .matches = {
298 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
299 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
301 .driver_data = &quirk_dell_vostro_v130,
304 .callback = dmi_matched,
305 .ident = "Dell XPS13 9333",
306 .matches = {
307 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
310 .driver_data = &quirk_dell_xps13_9333,
313 .callback = dmi_matched,
314 .ident = "Dell Latitude E6410",
315 .matches = {
316 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6410"),
319 .driver_data = &quirk_dell_latitude_e6410,
324 static void dell_fill_request(struct calling_interface_buffer *buffer,
325 u32 arg0, u32 arg1, u32 arg2, u32 arg3)
327 memset(buffer, 0, sizeof(struct calling_interface_buffer));
328 buffer->input[0] = arg0;
329 buffer->input[1] = arg1;
330 buffer->input[2] = arg2;
331 buffer->input[3] = arg3;
334 static int dell_send_request(struct calling_interface_buffer *buffer,
335 u16 class, u16 select)
337 int ret;
339 buffer->cmd_class = class;
340 buffer->cmd_select = select;
341 ret = dell_smbios_call(buffer);
342 if (ret != 0)
343 return ret;
344 return dell_smbios_error(buffer->output[0]);
348 * Derived from information in smbios-wireless-ctl:
350 * cbSelect 17, Value 11
352 * Return Wireless Info
353 * cbArg1, byte0 = 0x00
355 * cbRes1 Standard return codes (0, -1, -2)
356 * cbRes2 Info bit flags:
358 * 0 Hardware switch supported (1)
359 * 1 WiFi locator supported (1)
360 * 2 WLAN supported (1)
361 * 3 Bluetooth (BT) supported (1)
362 * 4 WWAN supported (1)
363 * 5 Wireless KBD supported (1)
364 * 6 Uw b supported (1)
365 * 7 WiGig supported (1)
366 * 8 WLAN installed (1)
367 * 9 BT installed (1)
368 * 10 WWAN installed (1)
369 * 11 Uw b installed (1)
370 * 12 WiGig installed (1)
371 * 13-15 Reserved (0)
372 * 16 Hardware (HW) switch is On (1)
373 * 17 WLAN disabled (1)
374 * 18 BT disabled (1)
375 * 19 WWAN disabled (1)
376 * 20 Uw b disabled (1)
377 * 21 WiGig disabled (1)
378 * 20-31 Reserved (0)
380 * cbRes3 NVRAM size in bytes
381 * cbRes4, byte 0 NVRAM format version number
384 * Set QuickSet Radio Disable Flag
385 * cbArg1, byte0 = 0x01
386 * cbArg1, byte1
387 * Radio ID value:
388 * 0 Radio Status
389 * 1 WLAN ID
390 * 2 BT ID
391 * 3 WWAN ID
392 * 4 UWB ID
393 * 5 WIGIG ID
394 * cbArg1, byte2 Flag bits:
395 * 0 QuickSet disables radio (1)
396 * 1-7 Reserved (0)
398 * cbRes1 Standard return codes (0, -1, -2)
399 * cbRes2 QuickSet (QS) radio disable bit map:
400 * 0 QS disables WLAN
401 * 1 QS disables BT
402 * 2 QS disables WWAN
403 * 3 QS disables UWB
404 * 4 QS disables WIGIG
405 * 5-31 Reserved (0)
407 * Wireless Switch Configuration
408 * cbArg1, byte0 = 0x02
410 * cbArg1, byte1
411 * Subcommand:
412 * 0 Get config
413 * 1 Set config
414 * 2 Set WiFi locator enable/disable
415 * cbArg1,byte2
416 * Switch settings (if byte 1==1):
417 * 0 WLAN sw itch control (1)
418 * 1 BT sw itch control (1)
419 * 2 WWAN sw itch control (1)
420 * 3 UWB sw itch control (1)
421 * 4 WiGig sw itch control (1)
422 * 5-7 Reserved (0)
423 * cbArg1, byte2 Enable bits (if byte 1==2):
424 * 0 Enable WiFi locator (1)
426 * cbRes1 Standard return codes (0, -1, -2)
427 * cbRes2 QuickSet radio disable bit map:
428 * 0 WLAN controlled by sw itch (1)
429 * 1 BT controlled by sw itch (1)
430 * 2 WWAN controlled by sw itch (1)
431 * 3 UWB controlled by sw itch (1)
432 * 4 WiGig controlled by sw itch (1)
433 * 5-6 Reserved (0)
434 * 7 Wireless sw itch config locked (1)
435 * 8 WiFi locator enabled (1)
436 * 9-14 Reserved (0)
437 * 15 WiFi locator setting locked (1)
438 * 16-31 Reserved (0)
440 * Read Local Config Data (LCD)
441 * cbArg1, byte0 = 0x10
442 * cbArg1, byte1 NVRAM index low byte
443 * cbArg1, byte2 NVRAM index high byte
444 * cbRes1 Standard return codes (0, -1, -2)
445 * cbRes2 4 bytes read from LCD[index]
446 * cbRes3 4 bytes read from LCD[index+4]
447 * cbRes4 4 bytes read from LCD[index+8]
449 * Write Local Config Data (LCD)
450 * cbArg1, byte0 = 0x11
451 * cbArg1, byte1 NVRAM index low byte
452 * cbArg1, byte2 NVRAM index high byte
453 * cbArg2 4 bytes to w rite at LCD[index]
454 * cbArg3 4 bytes to w rite at LCD[index+4]
455 * cbArg4 4 bytes to w rite at LCD[index+8]
456 * cbRes1 Standard return codes (0, -1, -2)
458 * Populate Local Config Data from NVRAM
459 * cbArg1, byte0 = 0x12
460 * cbRes1 Standard return codes (0, -1, -2)
462 * Commit Local Config Data to NVRAM
463 * cbArg1, byte0 = 0x13
464 * cbRes1 Standard return codes (0, -1, -2)
467 static int dell_rfkill_set(void *data, bool blocked)
469 int disable = blocked ? 1 : 0;
470 unsigned long radio = (unsigned long)data;
471 int hwswitch_bit = (unsigned long)data - 1;
472 struct calling_interface_buffer buffer;
473 int hwswitch;
474 int status;
475 int ret;
477 dell_fill_request(&buffer, 0, 0, 0, 0);
478 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
479 if (ret)
480 return ret;
481 status = buffer.output[1];
483 dell_fill_request(&buffer, 0x2, 0, 0, 0);
484 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
485 if (ret)
486 return ret;
487 hwswitch = buffer.output[1];
489 /* If the hardware switch controls this radio, and the hardware
490 switch is disabled, always disable the radio */
491 if (ret == 0 && (hwswitch & BIT(hwswitch_bit)) &&
492 (status & BIT(0)) && !(status & BIT(16)))
493 disable = 1;
495 dell_fill_request(&buffer, 1 | (radio<<8) | (disable << 16), 0, 0, 0);
496 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
497 return ret;
500 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
501 int status)
503 if (status & BIT(0)) {
504 /* Has hw-switch, sync sw_state to BIOS */
505 struct calling_interface_buffer buffer;
506 int block = rfkill_blocked(rfkill);
507 dell_fill_request(&buffer,
508 1 | (radio << 8) | (block << 16), 0, 0, 0);
509 dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
510 } else {
511 /* No hw-switch, sync BIOS state to sw_state */
512 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
516 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
517 int status, int hwswitch)
519 if (hwswitch & (BIT(radio - 1)))
520 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
523 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
525 int radio = ((unsigned long)data & 0xF);
526 struct calling_interface_buffer buffer;
527 int hwswitch;
528 int status;
529 int ret;
531 dell_fill_request(&buffer, 0, 0, 0, 0);
532 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
533 status = buffer.output[1];
535 if (ret != 0 || !(status & BIT(0))) {
536 return;
539 dell_fill_request(&buffer, 0, 0x2, 0, 0);
540 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
541 hwswitch = buffer.output[1];
543 if (ret != 0)
544 return;
546 dell_rfkill_update_hw_state(rfkill, radio, status, hwswitch);
549 static const struct rfkill_ops dell_rfkill_ops = {
550 .set_block = dell_rfkill_set,
551 .query = dell_rfkill_query,
554 static struct dentry *dell_laptop_dir;
556 static int dell_debugfs_show(struct seq_file *s, void *data)
558 struct calling_interface_buffer buffer;
559 int hwswitch_state;
560 int hwswitch_ret;
561 int status;
562 int ret;
564 dell_fill_request(&buffer, 0, 0, 0, 0);
565 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
566 if (ret)
567 return ret;
568 status = buffer.output[1];
570 dell_fill_request(&buffer, 0, 0x2, 0, 0);
571 hwswitch_ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
572 if (hwswitch_ret)
573 return hwswitch_ret;
574 hwswitch_state = buffer.output[1];
576 seq_printf(s, "return:\t%d\n", ret);
577 seq_printf(s, "status:\t0x%X\n", status);
578 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
579 status & BIT(0));
580 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
581 (status & BIT(1)) >> 1);
582 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
583 (status & BIT(2)) >> 2);
584 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
585 (status & BIT(3)) >> 3);
586 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
587 (status & BIT(4)) >> 4);
588 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
589 (status & BIT(5)) >> 5);
590 seq_printf(s, "Bit 6 : UWB supported: %lu\n",
591 (status & BIT(6)) >> 6);
592 seq_printf(s, "Bit 7 : WiGig supported: %lu\n",
593 (status & BIT(7)) >> 7);
594 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
595 (status & BIT(8)) >> 8);
596 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
597 (status & BIT(9)) >> 9);
598 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
599 (status & BIT(10)) >> 10);
600 seq_printf(s, "Bit 11: UWB installed: %lu\n",
601 (status & BIT(11)) >> 11);
602 seq_printf(s, "Bit 12: WiGig installed: %lu\n",
603 (status & BIT(12)) >> 12);
605 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
606 (status & BIT(16)) >> 16);
607 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
608 (status & BIT(17)) >> 17);
609 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
610 (status & BIT(18)) >> 18);
611 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
612 (status & BIT(19)) >> 19);
613 seq_printf(s, "Bit 20: UWB is blocked: %lu\n",
614 (status & BIT(20)) >> 20);
615 seq_printf(s, "Bit 21: WiGig is blocked: %lu\n",
616 (status & BIT(21)) >> 21);
618 seq_printf(s, "\nhwswitch_return:\t%d\n", hwswitch_ret);
619 seq_printf(s, "hwswitch_state:\t0x%X\n", hwswitch_state);
620 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
621 hwswitch_state & BIT(0));
622 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
623 (hwswitch_state & BIT(1)) >> 1);
624 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
625 (hwswitch_state & BIT(2)) >> 2);
626 seq_printf(s, "Bit 3 : UWB controlled by switch: %lu\n",
627 (hwswitch_state & BIT(3)) >> 3);
628 seq_printf(s, "Bit 4 : WiGig controlled by switch: %lu\n",
629 (hwswitch_state & BIT(4)) >> 4);
630 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
631 (hwswitch_state & BIT(7)) >> 7);
632 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
633 (hwswitch_state & BIT(8)) >> 8);
634 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
635 (hwswitch_state & BIT(15)) >> 15);
637 return 0;
639 DEFINE_SHOW_ATTRIBUTE(dell_debugfs);
641 static void dell_update_rfkill(struct work_struct *ignored)
643 struct calling_interface_buffer buffer;
644 int hwswitch = 0;
645 int status;
646 int ret;
648 dell_fill_request(&buffer, 0, 0, 0, 0);
649 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
650 status = buffer.output[1];
652 if (ret != 0)
653 return;
655 dell_fill_request(&buffer, 0, 0x2, 0, 0);
656 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
658 if (ret == 0 && (status & BIT(0)))
659 hwswitch = buffer.output[1];
661 if (wifi_rfkill) {
662 dell_rfkill_update_hw_state(wifi_rfkill, 1, status, hwswitch);
663 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
665 if (bluetooth_rfkill) {
666 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status,
667 hwswitch);
668 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
670 if (wwan_rfkill) {
671 dell_rfkill_update_hw_state(wwan_rfkill, 3, status, hwswitch);
672 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
675 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
677 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
678 struct serio *port)
680 static bool extended;
682 if (str & I8042_STR_AUXDATA)
683 return false;
685 if (unlikely(data == 0xe0)) {
686 extended = true;
687 return false;
688 } else if (unlikely(extended)) {
689 switch (data) {
690 case 0x8:
691 schedule_delayed_work(&dell_rfkill_work,
692 round_jiffies_relative(HZ / 4));
693 break;
695 extended = false;
698 return false;
701 static int (*dell_rbtn_notifier_register_func)(struct notifier_block *);
702 static int (*dell_rbtn_notifier_unregister_func)(struct notifier_block *);
704 static int dell_laptop_rbtn_notifier_call(struct notifier_block *nb,
705 unsigned long action, void *data)
707 schedule_delayed_work(&dell_rfkill_work, 0);
708 return NOTIFY_OK;
711 static struct notifier_block dell_laptop_rbtn_notifier = {
712 .notifier_call = dell_laptop_rbtn_notifier_call,
715 static int __init dell_setup_rfkill(void)
717 struct calling_interface_buffer buffer;
718 int status, ret, whitelisted;
719 const char *product;
722 * rfkill support causes trouble on various models, mostly Inspirons.
723 * So we whitelist certain series, and don't support rfkill on others.
725 whitelisted = 0;
726 product = dmi_get_system_info(DMI_PRODUCT_NAME);
727 if (product && (strncmp(product, "Latitude", 8) == 0 ||
728 strncmp(product, "Precision", 9) == 0))
729 whitelisted = 1;
730 if (!force_rfkill && !whitelisted)
731 return 0;
733 dell_fill_request(&buffer, 0, 0, 0, 0);
734 ret = dell_send_request(&buffer, CLASS_INFO, SELECT_RFKILL);
735 status = buffer.output[1];
737 /* dell wireless info smbios call is not supported */
738 if (ret != 0)
739 return 0;
741 /* rfkill is only tested on laptops with a hwswitch */
742 if (!(status & BIT(0)) && !force_rfkill)
743 return 0;
745 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
746 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
747 RFKILL_TYPE_WLAN,
748 &dell_rfkill_ops, (void *) 1);
749 if (!wifi_rfkill) {
750 ret = -ENOMEM;
751 goto err_wifi;
753 ret = rfkill_register(wifi_rfkill);
754 if (ret)
755 goto err_wifi;
758 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
759 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
760 &platform_device->dev,
761 RFKILL_TYPE_BLUETOOTH,
762 &dell_rfkill_ops, (void *) 2);
763 if (!bluetooth_rfkill) {
764 ret = -ENOMEM;
765 goto err_bluetooth;
767 ret = rfkill_register(bluetooth_rfkill);
768 if (ret)
769 goto err_bluetooth;
772 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
773 wwan_rfkill = rfkill_alloc("dell-wwan",
774 &platform_device->dev,
775 RFKILL_TYPE_WWAN,
776 &dell_rfkill_ops, (void *) 3);
777 if (!wwan_rfkill) {
778 ret = -ENOMEM;
779 goto err_wwan;
781 ret = rfkill_register(wwan_rfkill);
782 if (ret)
783 goto err_wwan;
787 * Dell Airplane Mode Switch driver (dell-rbtn) supports ACPI devices
788 * which can receive events from HW slider switch.
790 * Dell SMBIOS on whitelisted models supports controlling radio devices
791 * but does not support receiving HW button switch events. We can use
792 * i8042 filter hook function to receive keyboard data and handle
793 * keycode for HW button.
795 * So if it is possible we will use Dell Airplane Mode Switch ACPI
796 * driver for receiving HW events and Dell SMBIOS for setting rfkill
797 * states. If ACPI driver or device is not available we will fallback to
798 * i8042 filter hook function.
800 * To prevent duplicate rfkill devices which control and do same thing,
801 * dell-rbtn driver will automatically remove its own rfkill devices
802 * once function dell_rbtn_notifier_register() is called.
805 dell_rbtn_notifier_register_func =
806 symbol_request(dell_rbtn_notifier_register);
807 if (dell_rbtn_notifier_register_func) {
808 dell_rbtn_notifier_unregister_func =
809 symbol_request(dell_rbtn_notifier_unregister);
810 if (!dell_rbtn_notifier_unregister_func) {
811 symbol_put(dell_rbtn_notifier_register);
812 dell_rbtn_notifier_register_func = NULL;
816 if (dell_rbtn_notifier_register_func) {
817 ret = dell_rbtn_notifier_register_func(
818 &dell_laptop_rbtn_notifier);
819 symbol_put(dell_rbtn_notifier_register);
820 dell_rbtn_notifier_register_func = NULL;
821 if (ret != 0) {
822 symbol_put(dell_rbtn_notifier_unregister);
823 dell_rbtn_notifier_unregister_func = NULL;
825 } else {
826 pr_info("Symbols from dell-rbtn acpi driver are not available\n");
827 ret = -ENODEV;
830 if (ret == 0) {
831 pr_info("Using dell-rbtn acpi driver for receiving events\n");
832 } else if (ret != -ENODEV) {
833 pr_warn("Unable to register dell rbtn notifier\n");
834 goto err_filter;
835 } else {
836 ret = i8042_install_filter(dell_laptop_i8042_filter);
837 if (ret) {
838 pr_warn("Unable to install key filter\n");
839 goto err_filter;
841 pr_info("Using i8042 filter function for receiving events\n");
844 return 0;
845 err_filter:
846 if (wwan_rfkill)
847 rfkill_unregister(wwan_rfkill);
848 err_wwan:
849 rfkill_destroy(wwan_rfkill);
850 if (bluetooth_rfkill)
851 rfkill_unregister(bluetooth_rfkill);
852 err_bluetooth:
853 rfkill_destroy(bluetooth_rfkill);
854 if (wifi_rfkill)
855 rfkill_unregister(wifi_rfkill);
856 err_wifi:
857 rfkill_destroy(wifi_rfkill);
859 return ret;
862 static void dell_cleanup_rfkill(void)
864 if (dell_rbtn_notifier_unregister_func) {
865 dell_rbtn_notifier_unregister_func(&dell_laptop_rbtn_notifier);
866 symbol_put(dell_rbtn_notifier_unregister);
867 dell_rbtn_notifier_unregister_func = NULL;
868 } else {
869 i8042_remove_filter(dell_laptop_i8042_filter);
871 cancel_delayed_work_sync(&dell_rfkill_work);
872 if (wifi_rfkill) {
873 rfkill_unregister(wifi_rfkill);
874 rfkill_destroy(wifi_rfkill);
876 if (bluetooth_rfkill) {
877 rfkill_unregister(bluetooth_rfkill);
878 rfkill_destroy(bluetooth_rfkill);
880 if (wwan_rfkill) {
881 rfkill_unregister(wwan_rfkill);
882 rfkill_destroy(wwan_rfkill);
886 static int dell_send_intensity(struct backlight_device *bd)
888 struct calling_interface_buffer buffer;
889 struct calling_interface_token *token;
890 int ret;
892 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
893 if (!token)
894 return -ENODEV;
896 dell_fill_request(&buffer,
897 token->location, bd->props.brightness, 0, 0);
898 if (power_supply_is_system_supplied() > 0)
899 ret = dell_send_request(&buffer,
900 CLASS_TOKEN_WRITE, SELECT_TOKEN_AC);
901 else
902 ret = dell_send_request(&buffer,
903 CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT);
905 return ret;
908 static int dell_get_intensity(struct backlight_device *bd)
910 struct calling_interface_buffer buffer;
911 struct calling_interface_token *token;
912 int ret;
914 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
915 if (!token)
916 return -ENODEV;
918 dell_fill_request(&buffer, token->location, 0, 0, 0);
919 if (power_supply_is_system_supplied() > 0)
920 ret = dell_send_request(&buffer,
921 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
922 else
923 ret = dell_send_request(&buffer,
924 CLASS_TOKEN_READ, SELECT_TOKEN_BAT);
926 if (ret == 0)
927 ret = buffer.output[1];
929 return ret;
932 static const struct backlight_ops dell_ops = {
933 .get_brightness = dell_get_intensity,
934 .update_status = dell_send_intensity,
937 static void touchpad_led_on(void)
939 int command = 0x97;
940 char data = 1;
941 i8042_command(&data, command | 1 << 12);
944 static void touchpad_led_off(void)
946 int command = 0x97;
947 char data = 2;
948 i8042_command(&data, command | 1 << 12);
951 static void touchpad_led_set(struct led_classdev *led_cdev,
952 enum led_brightness value)
954 if (value > 0)
955 touchpad_led_on();
956 else
957 touchpad_led_off();
960 static struct led_classdev touchpad_led = {
961 .name = "dell-laptop::touchpad",
962 .brightness_set = touchpad_led_set,
963 .flags = LED_CORE_SUSPENDRESUME,
966 static int __init touchpad_led_init(struct device *dev)
968 return led_classdev_register(dev, &touchpad_led);
971 static void touchpad_led_exit(void)
973 led_classdev_unregister(&touchpad_led);
977 * Derived from information in smbios-keyboard-ctl:
979 * cbClass 4
980 * cbSelect 11
981 * Keyboard illumination
982 * cbArg1 determines the function to be performed
984 * cbArg1 0x0 = Get Feature Information
985 * cbRES1 Standard return codes (0, -1, -2)
986 * cbRES2, word0 Bitmap of user-selectable modes
987 * bit 0 Always off (All systems)
988 * bit 1 Always on (Travis ATG, Siberia)
989 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
990 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
991 * bit 4 Auto: Input-activity-based On; input-activity based Off
992 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
993 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
994 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
995 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
996 * bits 9-15 Reserved for future use
997 * cbRES2, byte2 Reserved for future use
998 * cbRES2, byte3 Keyboard illumination type
999 * 0 Reserved
1000 * 1 Tasklight
1001 * 2 Backlight
1002 * 3-255 Reserved for future use
1003 * cbRES3, byte0 Supported auto keyboard illumination trigger bitmap.
1004 * bit 0 Any keystroke
1005 * bit 1 Touchpad activity
1006 * bit 2 Pointing stick
1007 * bit 3 Any mouse
1008 * bits 4-7 Reserved for future use
1009 * cbRES3, byte1 Supported timeout unit bitmap
1010 * bit 0 Seconds
1011 * bit 1 Minutes
1012 * bit 2 Hours
1013 * bit 3 Days
1014 * bits 4-7 Reserved for future use
1015 * cbRES3, byte2 Number of keyboard light brightness levels
1016 * cbRES4, byte0 Maximum acceptable seconds value (0 if seconds not supported).
1017 * cbRES4, byte1 Maximum acceptable minutes value (0 if minutes not supported).
1018 * cbRES4, byte2 Maximum acceptable hours value (0 if hours not supported).
1019 * cbRES4, byte3 Maximum acceptable days value (0 if days not supported)
1021 * cbArg1 0x1 = Get Current State
1022 * cbRES1 Standard return codes (0, -1, -2)
1023 * cbRES2, word0 Bitmap of current mode state
1024 * bit 0 Always off (All systems)
1025 * bit 1 Always on (Travis ATG, Siberia)
1026 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1027 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1028 * bit 4 Auto: Input-activity-based On; input-activity based Off
1029 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1030 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1031 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1032 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1033 * bits 9-15 Reserved for future use
1034 * Note: Only One bit can be set
1035 * cbRES2, byte2 Currently active auto keyboard illumination triggers.
1036 * bit 0 Any keystroke
1037 * bit 1 Touchpad activity
1038 * bit 2 Pointing stick
1039 * bit 3 Any mouse
1040 * bits 4-7 Reserved for future use
1041 * cbRES2, byte3 Current Timeout on battery
1042 * bits 7:6 Timeout units indicator:
1043 * 00b Seconds
1044 * 01b Minutes
1045 * 10b Hours
1046 * 11b Days
1047 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1048 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte
1049 * are set upon return from the [Get feature information] call.
1050 * cbRES3, byte0 Current setting of ALS value that turns the light on or off.
1051 * cbRES3, byte1 Current ALS reading
1052 * cbRES3, byte2 Current keyboard light level.
1053 * cbRES3, byte3 Current timeout on AC Power
1054 * bits 7:6 Timeout units indicator:
1055 * 00b Seconds
1056 * 01b Minutes
1057 * 10b Hours
1058 * 11b Days
1059 * Bits 5:0 Timeout value (0-63) in sec/min/hr/day
1060 * NOTE: A value of 0 means always on (no timeout) if any bits of RES3 byte2
1061 * are set upon return from the upon return from the [Get Feature information] call.
1063 * cbArg1 0x2 = Set New State
1064 * cbRES1 Standard return codes (0, -1, -2)
1065 * cbArg2, word0 Bitmap of current mode state
1066 * bit 0 Always off (All systems)
1067 * bit 1 Always on (Travis ATG, Siberia)
1068 * bit 2 Auto: ALS-based On; ALS-based Off (Travis ATG)
1069 * bit 3 Auto: ALS- and input-activity-based On; input-activity based Off
1070 * bit 4 Auto: Input-activity-based On; input-activity based Off
1071 * bit 5 Auto: Input-activity-based On (illumination level 25%); input-activity based Off
1072 * bit 6 Auto: Input-activity-based On (illumination level 50%); input-activity based Off
1073 * bit 7 Auto: Input-activity-based On (illumination level 75%); input-activity based Off
1074 * bit 8 Auto: Input-activity-based On (illumination level 100%); input-activity based Off
1075 * bits 9-15 Reserved for future use
1076 * Note: Only One bit can be set
1077 * cbArg2, byte2 Desired auto keyboard illumination triggers. Must remain inactive to allow
1078 * keyboard to turn off automatically.
1079 * bit 0 Any keystroke
1080 * bit 1 Touchpad activity
1081 * bit 2 Pointing stick
1082 * bit 3 Any mouse
1083 * bits 4-7 Reserved for future use
1084 * cbArg2, byte3 Desired Timeout on battery
1085 * bits 7:6 Timeout units indicator:
1086 * 00b Seconds
1087 * 01b Minutes
1088 * 10b Hours
1089 * 11b Days
1090 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1091 * cbArg3, byte0 Desired setting of ALS value that turns the light on or off.
1092 * cbArg3, byte2 Desired keyboard light level.
1093 * cbArg3, byte3 Desired Timeout on AC power
1094 * bits 7:6 Timeout units indicator:
1095 * 00b Seconds
1096 * 01b Minutes
1097 * 10b Hours
1098 * 11b Days
1099 * bits 5:0 Timeout value (0-63) in sec/min/hr/day
1103 enum kbd_timeout_unit {
1104 KBD_TIMEOUT_SECONDS = 0,
1105 KBD_TIMEOUT_MINUTES,
1106 KBD_TIMEOUT_HOURS,
1107 KBD_TIMEOUT_DAYS,
1110 enum kbd_mode_bit {
1111 KBD_MODE_BIT_OFF = 0,
1112 KBD_MODE_BIT_ON,
1113 KBD_MODE_BIT_ALS,
1114 KBD_MODE_BIT_TRIGGER_ALS,
1115 KBD_MODE_BIT_TRIGGER,
1116 KBD_MODE_BIT_TRIGGER_25,
1117 KBD_MODE_BIT_TRIGGER_50,
1118 KBD_MODE_BIT_TRIGGER_75,
1119 KBD_MODE_BIT_TRIGGER_100,
1122 #define kbd_is_als_mode_bit(bit) \
1123 ((bit) == KBD_MODE_BIT_ALS || (bit) == KBD_MODE_BIT_TRIGGER_ALS)
1124 #define kbd_is_trigger_mode_bit(bit) \
1125 ((bit) >= KBD_MODE_BIT_TRIGGER_ALS && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1126 #define kbd_is_level_mode_bit(bit) \
1127 ((bit) >= KBD_MODE_BIT_TRIGGER_25 && (bit) <= KBD_MODE_BIT_TRIGGER_100)
1129 struct kbd_info {
1130 u16 modes;
1131 u8 type;
1132 u8 triggers;
1133 u8 levels;
1134 u8 seconds;
1135 u8 minutes;
1136 u8 hours;
1137 u8 days;
1140 struct kbd_state {
1141 u8 mode_bit;
1142 u8 triggers;
1143 u8 timeout_value;
1144 u8 timeout_unit;
1145 u8 timeout_value_ac;
1146 u8 timeout_unit_ac;
1147 u8 als_setting;
1148 u8 als_value;
1149 u8 level;
1152 static const int kbd_tokens[] = {
1153 KBD_LED_OFF_TOKEN,
1154 KBD_LED_AUTO_25_TOKEN,
1155 KBD_LED_AUTO_50_TOKEN,
1156 KBD_LED_AUTO_75_TOKEN,
1157 KBD_LED_AUTO_100_TOKEN,
1158 KBD_LED_ON_TOKEN,
1161 static u16 kbd_token_bits;
1163 static struct kbd_info kbd_info;
1164 static bool kbd_als_supported;
1165 static bool kbd_triggers_supported;
1166 static bool kbd_timeout_ac_supported;
1168 static u8 kbd_mode_levels[16];
1169 static int kbd_mode_levels_count;
1171 static u8 kbd_previous_level;
1172 static u8 kbd_previous_mode_bit;
1174 static bool kbd_led_present;
1175 static DEFINE_MUTEX(kbd_led_mutex);
1176 static enum led_brightness kbd_led_level;
1179 * NOTE: there are three ways to set the keyboard backlight level.
1180 * First, via kbd_state.mode_bit (assigning KBD_MODE_BIT_TRIGGER_* value).
1181 * Second, via kbd_state.level (assigning numerical value <= kbd_info.levels).
1182 * Third, via SMBIOS tokens (KBD_LED_* in kbd_tokens)
1184 * There are laptops which support only one of these methods. If we want to
1185 * support as many machines as possible we need to implement all three methods.
1186 * The first two methods use the kbd_state structure. The third uses SMBIOS
1187 * tokens. If kbd_info.levels == 0, the machine does not support setting the
1188 * keyboard backlight level via kbd_state.level.
1191 static int kbd_get_info(struct kbd_info *info)
1193 struct calling_interface_buffer buffer;
1194 u8 units;
1195 int ret;
1197 dell_fill_request(&buffer, 0, 0, 0, 0);
1198 ret = dell_send_request(&buffer,
1199 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1200 if (ret)
1201 return ret;
1203 info->modes = buffer.output[1] & 0xFFFF;
1204 info->type = (buffer.output[1] >> 24) & 0xFF;
1205 info->triggers = buffer.output[2] & 0xFF;
1206 units = (buffer.output[2] >> 8) & 0xFF;
1207 info->levels = (buffer.output[2] >> 16) & 0xFF;
1209 if (quirks && quirks->kbd_led_levels_off_1 && info->levels)
1210 info->levels--;
1212 if (units & BIT(0))
1213 info->seconds = (buffer.output[3] >> 0) & 0xFF;
1214 if (units & BIT(1))
1215 info->minutes = (buffer.output[3] >> 8) & 0xFF;
1216 if (units & BIT(2))
1217 info->hours = (buffer.output[3] >> 16) & 0xFF;
1218 if (units & BIT(3))
1219 info->days = (buffer.output[3] >> 24) & 0xFF;
1221 return ret;
1224 static unsigned int kbd_get_max_level(void)
1226 if (kbd_info.levels != 0)
1227 return kbd_info.levels;
1228 if (kbd_mode_levels_count > 0)
1229 return kbd_mode_levels_count - 1;
1230 return 0;
1233 static int kbd_get_level(struct kbd_state *state)
1235 int i;
1237 if (kbd_info.levels != 0)
1238 return state->level;
1240 if (kbd_mode_levels_count > 0) {
1241 for (i = 0; i < kbd_mode_levels_count; ++i)
1242 if (kbd_mode_levels[i] == state->mode_bit)
1243 return i;
1244 return 0;
1247 return -EINVAL;
1250 static int kbd_set_level(struct kbd_state *state, u8 level)
1252 if (kbd_info.levels != 0) {
1253 if (level != 0)
1254 kbd_previous_level = level;
1255 if (state->level == level)
1256 return 0;
1257 state->level = level;
1258 if (level != 0 && state->mode_bit == KBD_MODE_BIT_OFF)
1259 state->mode_bit = kbd_previous_mode_bit;
1260 else if (level == 0 && state->mode_bit != KBD_MODE_BIT_OFF) {
1261 kbd_previous_mode_bit = state->mode_bit;
1262 state->mode_bit = KBD_MODE_BIT_OFF;
1264 return 0;
1267 if (kbd_mode_levels_count > 0 && level < kbd_mode_levels_count) {
1268 if (level != 0)
1269 kbd_previous_level = level;
1270 state->mode_bit = kbd_mode_levels[level];
1271 return 0;
1274 return -EINVAL;
1277 static int kbd_get_state(struct kbd_state *state)
1279 struct calling_interface_buffer buffer;
1280 int ret;
1282 dell_fill_request(&buffer, 0, 0, 0, 0);
1283 ret = dell_send_request(&buffer,
1284 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1285 if (ret)
1286 return ret;
1288 state->mode_bit = ffs(buffer.output[1] & 0xFFFF);
1289 if (state->mode_bit != 0)
1290 state->mode_bit--;
1292 state->triggers = (buffer.output[1] >> 16) & 0xFF;
1293 state->timeout_value = (buffer.output[1] >> 24) & 0x3F;
1294 state->timeout_unit = (buffer.output[1] >> 30) & 0x3;
1295 state->als_setting = buffer.output[2] & 0xFF;
1296 state->als_value = (buffer.output[2] >> 8) & 0xFF;
1297 state->level = (buffer.output[2] >> 16) & 0xFF;
1298 state->timeout_value_ac = (buffer.output[2] >> 24) & 0x3F;
1299 state->timeout_unit_ac = (buffer.output[2] >> 30) & 0x3;
1301 return ret;
1304 static int kbd_set_state(struct kbd_state *state)
1306 struct calling_interface_buffer buffer;
1307 int ret;
1308 u32 input1;
1309 u32 input2;
1311 input1 = BIT(state->mode_bit) & 0xFFFF;
1312 input1 |= (state->triggers & 0xFF) << 16;
1313 input1 |= (state->timeout_value & 0x3F) << 24;
1314 input1 |= (state->timeout_unit & 0x3) << 30;
1315 input2 = state->als_setting & 0xFF;
1316 input2 |= (state->level & 0xFF) << 16;
1317 input2 |= (state->timeout_value_ac & 0x3F) << 24;
1318 input2 |= (state->timeout_unit_ac & 0x3) << 30;
1319 dell_fill_request(&buffer, 0x2, input1, input2, 0);
1320 ret = dell_send_request(&buffer,
1321 CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT);
1323 return ret;
1326 static int kbd_set_state_safe(struct kbd_state *state, struct kbd_state *old)
1328 int ret;
1330 ret = kbd_set_state(state);
1331 if (ret == 0)
1332 return 0;
1335 * When setting the new state fails,try to restore the previous one.
1336 * This is needed on some machines where BIOS sets a default state when
1337 * setting a new state fails. This default state could be all off.
1340 if (kbd_set_state(old))
1341 pr_err("Setting old previous keyboard state failed\n");
1343 return ret;
1346 static int kbd_set_token_bit(u8 bit)
1348 struct calling_interface_buffer buffer;
1349 struct calling_interface_token *token;
1350 int ret;
1352 if (bit >= ARRAY_SIZE(kbd_tokens))
1353 return -EINVAL;
1355 token = dell_smbios_find_token(kbd_tokens[bit]);
1356 if (!token)
1357 return -EINVAL;
1359 dell_fill_request(&buffer, token->location, token->value, 0, 0);
1360 ret = dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
1362 return ret;
1365 static int kbd_get_token_bit(u8 bit)
1367 struct calling_interface_buffer buffer;
1368 struct calling_interface_token *token;
1369 int ret;
1370 int val;
1372 if (bit >= ARRAY_SIZE(kbd_tokens))
1373 return -EINVAL;
1375 token = dell_smbios_find_token(kbd_tokens[bit]);
1376 if (!token)
1377 return -EINVAL;
1379 dell_fill_request(&buffer, token->location, 0, 0, 0);
1380 ret = dell_send_request(&buffer, CLASS_TOKEN_READ, SELECT_TOKEN_STD);
1381 val = buffer.output[1];
1383 if (ret)
1384 return ret;
1386 return (val == token->value);
1389 static int kbd_get_first_active_token_bit(void)
1391 int i;
1392 int ret;
1394 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i) {
1395 ret = kbd_get_token_bit(i);
1396 if (ret == 1)
1397 return i;
1400 return ret;
1403 static int kbd_get_valid_token_counts(void)
1405 return hweight16(kbd_token_bits);
1408 static inline int kbd_init_info(void)
1410 struct kbd_state state;
1411 int ret;
1412 int i;
1414 ret = kbd_get_info(&kbd_info);
1415 if (ret)
1416 return ret;
1418 /* NOTE: Old models without KBD_LED_AC_TOKEN token supports only one
1419 * timeout value which is shared for both battery and AC power
1420 * settings. So do not try to set AC values on old models.
1422 if (dell_smbios_find_token(KBD_LED_AC_TOKEN))
1423 kbd_timeout_ac_supported = true;
1425 kbd_get_state(&state);
1427 /* NOTE: timeout value is stored in 6 bits so max value is 63 */
1428 if (kbd_info.seconds > 63)
1429 kbd_info.seconds = 63;
1430 if (kbd_info.minutes > 63)
1431 kbd_info.minutes = 63;
1432 if (kbd_info.hours > 63)
1433 kbd_info.hours = 63;
1434 if (kbd_info.days > 63)
1435 kbd_info.days = 63;
1437 /* NOTE: On tested machines ON mode did not work and caused
1438 * problems (turned backlight off) so do not use it
1440 kbd_info.modes &= ~BIT(KBD_MODE_BIT_ON);
1442 kbd_previous_level = kbd_get_level(&state);
1443 kbd_previous_mode_bit = state.mode_bit;
1445 if (kbd_previous_level == 0 && kbd_get_max_level() != 0)
1446 kbd_previous_level = 1;
1448 if (kbd_previous_mode_bit == KBD_MODE_BIT_OFF) {
1449 kbd_previous_mode_bit =
1450 ffs(kbd_info.modes & ~BIT(KBD_MODE_BIT_OFF));
1451 if (kbd_previous_mode_bit != 0)
1452 kbd_previous_mode_bit--;
1455 if (kbd_info.modes & (BIT(KBD_MODE_BIT_ALS) |
1456 BIT(KBD_MODE_BIT_TRIGGER_ALS)))
1457 kbd_als_supported = true;
1459 if (kbd_info.modes & (
1460 BIT(KBD_MODE_BIT_TRIGGER_ALS) | BIT(KBD_MODE_BIT_TRIGGER) |
1461 BIT(KBD_MODE_BIT_TRIGGER_25) | BIT(KBD_MODE_BIT_TRIGGER_50) |
1462 BIT(KBD_MODE_BIT_TRIGGER_75) | BIT(KBD_MODE_BIT_TRIGGER_100)
1464 kbd_triggers_supported = true;
1466 /* kbd_mode_levels[0] is reserved, see below */
1467 for (i = 0; i < 16; ++i)
1468 if (kbd_is_level_mode_bit(i) && (BIT(i) & kbd_info.modes))
1469 kbd_mode_levels[1 + kbd_mode_levels_count++] = i;
1472 * Find the first supported mode and assign to kbd_mode_levels[0].
1473 * This should be 0 (off), but we cannot depend on the BIOS to
1474 * support 0.
1476 if (kbd_mode_levels_count > 0) {
1477 for (i = 0; i < 16; ++i) {
1478 if (BIT(i) & kbd_info.modes) {
1479 kbd_mode_levels[0] = i;
1480 break;
1483 kbd_mode_levels_count++;
1486 return 0;
1490 static inline void kbd_init_tokens(void)
1492 int i;
1494 for (i = 0; i < ARRAY_SIZE(kbd_tokens); ++i)
1495 if (dell_smbios_find_token(kbd_tokens[i]))
1496 kbd_token_bits |= BIT(i);
1499 static void kbd_init(void)
1501 int ret;
1503 ret = kbd_init_info();
1504 kbd_init_tokens();
1507 * Only supports keyboard backlight when it has at least two modes.
1509 if ((ret == 0 && (kbd_info.levels != 0 || kbd_mode_levels_count >= 2))
1510 || kbd_get_valid_token_counts() >= 2)
1511 kbd_led_present = true;
1514 static ssize_t kbd_led_timeout_store(struct device *dev,
1515 struct device_attribute *attr,
1516 const char *buf, size_t count)
1518 struct kbd_state new_state;
1519 struct kbd_state state;
1520 bool convert;
1521 int value;
1522 int ret;
1523 char ch;
1524 u8 unit;
1525 int i;
1527 ret = sscanf(buf, "%d %c", &value, &ch);
1528 if (ret < 1)
1529 return -EINVAL;
1530 else if (ret == 1)
1531 ch = 's';
1533 if (value < 0)
1534 return -EINVAL;
1536 convert = false;
1538 switch (ch) {
1539 case 's':
1540 if (value > kbd_info.seconds)
1541 convert = true;
1542 unit = KBD_TIMEOUT_SECONDS;
1543 break;
1544 case 'm':
1545 if (value > kbd_info.minutes)
1546 convert = true;
1547 unit = KBD_TIMEOUT_MINUTES;
1548 break;
1549 case 'h':
1550 if (value > kbd_info.hours)
1551 convert = true;
1552 unit = KBD_TIMEOUT_HOURS;
1553 break;
1554 case 'd':
1555 if (value > kbd_info.days)
1556 convert = true;
1557 unit = KBD_TIMEOUT_DAYS;
1558 break;
1559 default:
1560 return -EINVAL;
1563 if (quirks && quirks->needs_kbd_timeouts)
1564 convert = true;
1566 if (convert) {
1567 /* Convert value from current units to seconds */
1568 switch (unit) {
1569 case KBD_TIMEOUT_DAYS:
1570 value *= 24;
1571 case KBD_TIMEOUT_HOURS:
1572 value *= 60;
1573 case KBD_TIMEOUT_MINUTES:
1574 value *= 60;
1575 unit = KBD_TIMEOUT_SECONDS;
1578 if (quirks && quirks->needs_kbd_timeouts) {
1579 for (i = 0; quirks->kbd_timeouts[i] != -1; i++) {
1580 if (value <= quirks->kbd_timeouts[i]) {
1581 value = quirks->kbd_timeouts[i];
1582 break;
1587 if (value <= kbd_info.seconds && kbd_info.seconds) {
1588 unit = KBD_TIMEOUT_SECONDS;
1589 } else if (value / 60 <= kbd_info.minutes && kbd_info.minutes) {
1590 value /= 60;
1591 unit = KBD_TIMEOUT_MINUTES;
1592 } else if (value / (60 * 60) <= kbd_info.hours && kbd_info.hours) {
1593 value /= (60 * 60);
1594 unit = KBD_TIMEOUT_HOURS;
1595 } else if (value / (60 * 60 * 24) <= kbd_info.days && kbd_info.days) {
1596 value /= (60 * 60 * 24);
1597 unit = KBD_TIMEOUT_DAYS;
1598 } else {
1599 return -EINVAL;
1603 mutex_lock(&kbd_led_mutex);
1605 ret = kbd_get_state(&state);
1606 if (ret)
1607 goto out;
1609 new_state = state;
1611 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1612 new_state.timeout_value_ac = value;
1613 new_state.timeout_unit_ac = unit;
1614 } else {
1615 new_state.timeout_value = value;
1616 new_state.timeout_unit = unit;
1619 ret = kbd_set_state_safe(&new_state, &state);
1620 if (ret)
1621 goto out;
1623 ret = count;
1624 out:
1625 mutex_unlock(&kbd_led_mutex);
1626 return ret;
1629 static ssize_t kbd_led_timeout_show(struct device *dev,
1630 struct device_attribute *attr, char *buf)
1632 struct kbd_state state;
1633 int value;
1634 int ret;
1635 int len;
1636 u8 unit;
1638 ret = kbd_get_state(&state);
1639 if (ret)
1640 return ret;
1642 if (kbd_timeout_ac_supported && power_supply_is_system_supplied() > 0) {
1643 value = state.timeout_value_ac;
1644 unit = state.timeout_unit_ac;
1645 } else {
1646 value = state.timeout_value;
1647 unit = state.timeout_unit;
1650 len = sprintf(buf, "%d", value);
1652 switch (unit) {
1653 case KBD_TIMEOUT_SECONDS:
1654 return len + sprintf(buf+len, "s\n");
1655 case KBD_TIMEOUT_MINUTES:
1656 return len + sprintf(buf+len, "m\n");
1657 case KBD_TIMEOUT_HOURS:
1658 return len + sprintf(buf+len, "h\n");
1659 case KBD_TIMEOUT_DAYS:
1660 return len + sprintf(buf+len, "d\n");
1661 default:
1662 return -EINVAL;
1665 return len;
1668 static DEVICE_ATTR(stop_timeout, S_IRUGO | S_IWUSR,
1669 kbd_led_timeout_show, kbd_led_timeout_store);
1671 static const char * const kbd_led_triggers[] = {
1672 "keyboard",
1673 "touchpad",
1674 /*"trackstick"*/ NULL, /* NOTE: trackstick is just alias for touchpad */
1675 "mouse",
1678 static ssize_t kbd_led_triggers_store(struct device *dev,
1679 struct device_attribute *attr,
1680 const char *buf, size_t count)
1682 struct kbd_state new_state;
1683 struct kbd_state state;
1684 bool triggers_enabled = false;
1685 int trigger_bit = -1;
1686 char trigger[21];
1687 int i, ret;
1689 ret = sscanf(buf, "%20s", trigger);
1690 if (ret != 1)
1691 return -EINVAL;
1693 if (trigger[0] != '+' && trigger[0] != '-')
1694 return -EINVAL;
1696 mutex_lock(&kbd_led_mutex);
1698 ret = kbd_get_state(&state);
1699 if (ret)
1700 goto out;
1702 if (kbd_triggers_supported)
1703 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1705 if (kbd_triggers_supported) {
1706 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1707 if (!(kbd_info.triggers & BIT(i)))
1708 continue;
1709 if (!kbd_led_triggers[i])
1710 continue;
1711 if (strcmp(trigger+1, kbd_led_triggers[i]) != 0)
1712 continue;
1713 if (trigger[0] == '+' &&
1714 triggers_enabled && (state.triggers & BIT(i))) {
1715 ret = count;
1716 goto out;
1718 if (trigger[0] == '-' &&
1719 (!triggers_enabled || !(state.triggers & BIT(i)))) {
1720 ret = count;
1721 goto out;
1723 trigger_bit = i;
1724 break;
1728 if (trigger_bit == -1) {
1729 ret = -EINVAL;
1730 goto out;
1733 new_state = state;
1734 if (trigger[0] == '+')
1735 new_state.triggers |= BIT(trigger_bit);
1736 else {
1737 new_state.triggers &= ~BIT(trigger_bit);
1739 * NOTE: trackstick bit (2) must be disabled when
1740 * disabling touchpad bit (1), otherwise touchpad
1741 * bit (1) will not be disabled
1743 if (trigger_bit == 1)
1744 new_state.triggers &= ~BIT(2);
1746 if ((kbd_info.triggers & new_state.triggers) !=
1747 new_state.triggers) {
1748 ret = -EINVAL;
1749 goto out;
1751 if (new_state.triggers && !triggers_enabled) {
1752 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1753 kbd_set_level(&new_state, kbd_previous_level);
1754 } else if (new_state.triggers == 0) {
1755 kbd_set_level(&new_state, 0);
1757 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1758 ret = -EINVAL;
1759 goto out;
1761 ret = kbd_set_state_safe(&new_state, &state);
1762 if (ret)
1763 goto out;
1764 if (new_state.mode_bit != KBD_MODE_BIT_OFF)
1765 kbd_previous_mode_bit = new_state.mode_bit;
1766 ret = count;
1767 out:
1768 mutex_unlock(&kbd_led_mutex);
1769 return ret;
1772 static ssize_t kbd_led_triggers_show(struct device *dev,
1773 struct device_attribute *attr, char *buf)
1775 struct kbd_state state;
1776 bool triggers_enabled;
1777 int level, i, ret;
1778 int len = 0;
1780 ret = kbd_get_state(&state);
1781 if (ret)
1782 return ret;
1784 len = 0;
1786 if (kbd_triggers_supported) {
1787 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1788 level = kbd_get_level(&state);
1789 for (i = 0; i < ARRAY_SIZE(kbd_led_triggers); ++i) {
1790 if (!(kbd_info.triggers & BIT(i)))
1791 continue;
1792 if (!kbd_led_triggers[i])
1793 continue;
1794 if ((triggers_enabled || level <= 0) &&
1795 (state.triggers & BIT(i)))
1796 buf[len++] = '+';
1797 else
1798 buf[len++] = '-';
1799 len += sprintf(buf+len, "%s ", kbd_led_triggers[i]);
1803 if (len)
1804 buf[len - 1] = '\n';
1806 return len;
1809 static DEVICE_ATTR(start_triggers, S_IRUGO | S_IWUSR,
1810 kbd_led_triggers_show, kbd_led_triggers_store);
1812 static ssize_t kbd_led_als_enabled_store(struct device *dev,
1813 struct device_attribute *attr,
1814 const char *buf, size_t count)
1816 struct kbd_state new_state;
1817 struct kbd_state state;
1818 bool triggers_enabled = false;
1819 int enable;
1820 int ret;
1822 ret = kstrtoint(buf, 0, &enable);
1823 if (ret)
1824 return ret;
1826 mutex_lock(&kbd_led_mutex);
1828 ret = kbd_get_state(&state);
1829 if (ret)
1830 goto out;
1832 if (enable == kbd_is_als_mode_bit(state.mode_bit)) {
1833 ret = count;
1834 goto out;
1837 new_state = state;
1839 if (kbd_triggers_supported)
1840 triggers_enabled = kbd_is_trigger_mode_bit(state.mode_bit);
1842 if (enable) {
1843 if (triggers_enabled)
1844 new_state.mode_bit = KBD_MODE_BIT_TRIGGER_ALS;
1845 else
1846 new_state.mode_bit = KBD_MODE_BIT_ALS;
1847 } else {
1848 if (triggers_enabled) {
1849 new_state.mode_bit = KBD_MODE_BIT_TRIGGER;
1850 kbd_set_level(&new_state, kbd_previous_level);
1851 } else {
1852 new_state.mode_bit = KBD_MODE_BIT_ON;
1855 if (!(kbd_info.modes & BIT(new_state.mode_bit))) {
1856 ret = -EINVAL;
1857 goto out;
1860 ret = kbd_set_state_safe(&new_state, &state);
1861 if (ret)
1862 goto out;
1863 kbd_previous_mode_bit = new_state.mode_bit;
1865 ret = count;
1866 out:
1867 mutex_unlock(&kbd_led_mutex);
1868 return ret;
1871 static ssize_t kbd_led_als_enabled_show(struct device *dev,
1872 struct device_attribute *attr,
1873 char *buf)
1875 struct kbd_state state;
1876 bool enabled = false;
1877 int ret;
1879 ret = kbd_get_state(&state);
1880 if (ret)
1881 return ret;
1882 enabled = kbd_is_als_mode_bit(state.mode_bit);
1884 return sprintf(buf, "%d\n", enabled ? 1 : 0);
1887 static DEVICE_ATTR(als_enabled, S_IRUGO | S_IWUSR,
1888 kbd_led_als_enabled_show, kbd_led_als_enabled_store);
1890 static ssize_t kbd_led_als_setting_store(struct device *dev,
1891 struct device_attribute *attr,
1892 const char *buf, size_t count)
1894 struct kbd_state state;
1895 struct kbd_state new_state;
1896 u8 setting;
1897 int ret;
1899 ret = kstrtou8(buf, 10, &setting);
1900 if (ret)
1901 return ret;
1903 mutex_lock(&kbd_led_mutex);
1905 ret = kbd_get_state(&state);
1906 if (ret)
1907 goto out;
1909 new_state = state;
1910 new_state.als_setting = setting;
1912 ret = kbd_set_state_safe(&new_state, &state);
1913 if (ret)
1914 goto out;
1916 ret = count;
1917 out:
1918 mutex_unlock(&kbd_led_mutex);
1919 return ret;
1922 static ssize_t kbd_led_als_setting_show(struct device *dev,
1923 struct device_attribute *attr,
1924 char *buf)
1926 struct kbd_state state;
1927 int ret;
1929 ret = kbd_get_state(&state);
1930 if (ret)
1931 return ret;
1933 return sprintf(buf, "%d\n", state.als_setting);
1936 static DEVICE_ATTR(als_setting, S_IRUGO | S_IWUSR,
1937 kbd_led_als_setting_show, kbd_led_als_setting_store);
1939 static struct attribute *kbd_led_attrs[] = {
1940 &dev_attr_stop_timeout.attr,
1941 &dev_attr_start_triggers.attr,
1942 NULL,
1945 static const struct attribute_group kbd_led_group = {
1946 .attrs = kbd_led_attrs,
1949 static struct attribute *kbd_led_als_attrs[] = {
1950 &dev_attr_als_enabled.attr,
1951 &dev_attr_als_setting.attr,
1952 NULL,
1955 static const struct attribute_group kbd_led_als_group = {
1956 .attrs = kbd_led_als_attrs,
1959 static const struct attribute_group *kbd_led_groups[] = {
1960 &kbd_led_group,
1961 &kbd_led_als_group,
1962 NULL,
1965 static enum led_brightness kbd_led_level_get(struct led_classdev *led_cdev)
1967 int ret;
1968 u16 num;
1969 struct kbd_state state;
1971 if (kbd_get_max_level()) {
1972 ret = kbd_get_state(&state);
1973 if (ret)
1974 return 0;
1975 ret = kbd_get_level(&state);
1976 if (ret < 0)
1977 return 0;
1978 return ret;
1981 if (kbd_get_valid_token_counts()) {
1982 ret = kbd_get_first_active_token_bit();
1983 if (ret < 0)
1984 return 0;
1985 for (num = kbd_token_bits; num != 0 && ret > 0; --ret)
1986 num &= num - 1; /* clear the first bit set */
1987 if (num == 0)
1988 return 0;
1989 return ffs(num) - 1;
1992 pr_warn("Keyboard brightness level control not supported\n");
1993 return 0;
1996 static int kbd_led_level_set(struct led_classdev *led_cdev,
1997 enum led_brightness value)
1999 enum led_brightness new_value = value;
2000 struct kbd_state state;
2001 struct kbd_state new_state;
2002 u16 num;
2003 int ret;
2005 mutex_lock(&kbd_led_mutex);
2007 if (kbd_get_max_level()) {
2008 ret = kbd_get_state(&state);
2009 if (ret)
2010 goto out;
2011 new_state = state;
2012 ret = kbd_set_level(&new_state, value);
2013 if (ret)
2014 goto out;
2015 ret = kbd_set_state_safe(&new_state, &state);
2016 } else if (kbd_get_valid_token_counts()) {
2017 for (num = kbd_token_bits; num != 0 && value > 0; --value)
2018 num &= num - 1; /* clear the first bit set */
2019 if (num == 0)
2020 ret = 0;
2021 else
2022 ret = kbd_set_token_bit(ffs(num) - 1);
2023 } else {
2024 pr_warn("Keyboard brightness level control not supported\n");
2025 ret = -ENXIO;
2028 out:
2029 if (ret == 0)
2030 kbd_led_level = new_value;
2032 mutex_unlock(&kbd_led_mutex);
2033 return ret;
2036 static struct led_classdev kbd_led = {
2037 .name = "dell::kbd_backlight",
2038 .flags = LED_BRIGHT_HW_CHANGED,
2039 .brightness_set_blocking = kbd_led_level_set,
2040 .brightness_get = kbd_led_level_get,
2041 .groups = kbd_led_groups,
2044 static int __init kbd_led_init(struct device *dev)
2046 int ret;
2048 kbd_init();
2049 if (!kbd_led_present)
2050 return -ENODEV;
2051 if (!kbd_als_supported)
2052 kbd_led_groups[1] = NULL;
2053 kbd_led.max_brightness = kbd_get_max_level();
2054 if (!kbd_led.max_brightness) {
2055 kbd_led.max_brightness = kbd_get_valid_token_counts();
2056 if (kbd_led.max_brightness)
2057 kbd_led.max_brightness--;
2060 kbd_led_level = kbd_led_level_get(NULL);
2062 ret = led_classdev_register(dev, &kbd_led);
2063 if (ret)
2064 kbd_led_present = false;
2066 return ret;
2069 static void brightness_set_exit(struct led_classdev *led_cdev,
2070 enum led_brightness value)
2072 /* Don't change backlight level on exit */
2075 static void kbd_led_exit(void)
2077 if (!kbd_led_present)
2078 return;
2079 kbd_led.brightness_set = brightness_set_exit;
2080 led_classdev_unregister(&kbd_led);
2083 static int dell_laptop_notifier_call(struct notifier_block *nb,
2084 unsigned long action, void *data)
2086 bool changed = false;
2087 enum led_brightness new_kbd_led_level;
2089 switch (action) {
2090 case DELL_LAPTOP_KBD_BACKLIGHT_BRIGHTNESS_CHANGED:
2091 if (!kbd_led_present)
2092 break;
2094 mutex_lock(&kbd_led_mutex);
2095 new_kbd_led_level = kbd_led_level_get(&kbd_led);
2096 if (kbd_led_level != new_kbd_led_level) {
2097 kbd_led_level = new_kbd_led_level;
2098 changed = true;
2100 mutex_unlock(&kbd_led_mutex);
2102 if (changed)
2103 led_classdev_notify_brightness_hw_changed(&kbd_led,
2104 kbd_led_level);
2105 break;
2108 return NOTIFY_OK;
2111 static struct notifier_block dell_laptop_notifier = {
2112 .notifier_call = dell_laptop_notifier_call,
2115 int dell_micmute_led_set(int state)
2117 struct calling_interface_buffer buffer;
2118 struct calling_interface_token *token;
2120 if (state == 0)
2121 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_DISABLE);
2122 else if (state == 1)
2123 token = dell_smbios_find_token(GLOBAL_MIC_MUTE_ENABLE);
2124 else
2125 return -EINVAL;
2127 if (!token)
2128 return -ENODEV;
2130 dell_fill_request(&buffer, token->location, token->value, 0, 0);
2131 dell_send_request(&buffer, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD);
2133 return state;
2135 EXPORT_SYMBOL_GPL(dell_micmute_led_set);
2137 static int __init dell_init(void)
2139 struct calling_interface_token *token;
2140 int max_intensity = 0;
2141 int ret;
2143 if (!dmi_check_system(dell_device_table))
2144 return -ENODEV;
2146 quirks = NULL;
2147 /* find if this machine support other functions */
2148 dmi_check_system(dell_quirks);
2150 ret = platform_driver_register(&platform_driver);
2151 if (ret)
2152 goto fail_platform_driver;
2153 platform_device = platform_device_alloc("dell-laptop", -1);
2154 if (!platform_device) {
2155 ret = -ENOMEM;
2156 goto fail_platform_device1;
2158 ret = platform_device_add(platform_device);
2159 if (ret)
2160 goto fail_platform_device2;
2162 ret = dell_setup_rfkill();
2164 if (ret) {
2165 pr_warn("Unable to setup rfkill\n");
2166 goto fail_rfkill;
2169 if (quirks && quirks->touchpad_led)
2170 touchpad_led_init(&platform_device->dev);
2172 kbd_led_init(&platform_device->dev);
2174 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
2175 if (dell_laptop_dir != NULL)
2176 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
2177 &dell_debugfs_fops);
2179 dell_laptop_register_notifier(&dell_laptop_notifier);
2181 if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2182 return 0;
2184 token = dell_smbios_find_token(BRIGHTNESS_TOKEN);
2185 if (token) {
2186 struct calling_interface_buffer buffer;
2188 dell_fill_request(&buffer, token->location, 0, 0, 0);
2189 ret = dell_send_request(&buffer,
2190 CLASS_TOKEN_READ, SELECT_TOKEN_AC);
2191 if (ret)
2192 max_intensity = buffer.output[3];
2195 if (max_intensity) {
2196 struct backlight_properties props;
2197 memset(&props, 0, sizeof(struct backlight_properties));
2198 props.type = BACKLIGHT_PLATFORM;
2199 props.max_brightness = max_intensity;
2200 dell_backlight_device = backlight_device_register("dell_backlight",
2201 &platform_device->dev,
2202 NULL,
2203 &dell_ops,
2204 &props);
2206 if (IS_ERR(dell_backlight_device)) {
2207 ret = PTR_ERR(dell_backlight_device);
2208 dell_backlight_device = NULL;
2209 goto fail_backlight;
2212 dell_backlight_device->props.brightness =
2213 dell_get_intensity(dell_backlight_device);
2214 if (dell_backlight_device->props.brightness < 0) {
2215 ret = dell_backlight_device->props.brightness;
2216 goto fail_get_brightness;
2218 backlight_update_status(dell_backlight_device);
2221 return 0;
2223 fail_get_brightness:
2224 backlight_device_unregister(dell_backlight_device);
2225 fail_backlight:
2226 dell_cleanup_rfkill();
2227 fail_rfkill:
2228 platform_device_del(platform_device);
2229 fail_platform_device2:
2230 platform_device_put(platform_device);
2231 fail_platform_device1:
2232 platform_driver_unregister(&platform_driver);
2233 fail_platform_driver:
2234 return ret;
2237 static void __exit dell_exit(void)
2239 dell_laptop_unregister_notifier(&dell_laptop_notifier);
2240 debugfs_remove_recursive(dell_laptop_dir);
2241 if (quirks && quirks->touchpad_led)
2242 touchpad_led_exit();
2243 kbd_led_exit();
2244 backlight_device_unregister(dell_backlight_device);
2245 dell_cleanup_rfkill();
2246 if (platform_device) {
2247 platform_device_unregister(platform_device);
2248 platform_driver_unregister(&platform_driver);
2252 /* dell-rbtn.c driver export functions which will not work correctly (and could
2253 * cause kernel crash) if they are called before dell-rbtn.c init code. This is
2254 * not problem when dell-rbtn.c is compiled as external module. When both files
2255 * (dell-rbtn.c and dell-laptop.c) are compiled statically into kernel, then we
2256 * need to ensure that dell_init() will be called after initializing dell-rbtn.
2257 * This can be achieved by late_initcall() instead module_init().
2259 late_initcall(dell_init);
2260 module_exit(dell_exit);
2262 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
2263 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
2264 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
2265 MODULE_DESCRIPTION("Dell laptop driver");
2266 MODULE_LICENSE("GPL");