2 * Driver for Dell laptop extras
4 * Copyright (c) Red Hat <mjg@redhat.com>
6 * Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/platform_device.h>
20 #include <linux/backlight.h>
21 #include <linux/err.h>
22 #include <linux/dmi.h>
24 #include <linux/rfkill.h>
25 #include <linux/power_supply.h>
26 #include <linux/acpi.h>
28 #include <linux/i8042.h>
29 #include <linux/slab.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include "../../firmware/dcdbas.h"
34 #define BRIGHTNESS_TOKEN 0x7d
36 /* This structure will be modified by the firmware when we enter
37 * system management mode, hence the volatiles */
39 struct calling_interface_buffer {
42 volatile u32 input[4];
43 volatile u32 output[4];
46 struct calling_interface_token {
55 struct calling_interface_structure {
56 struct dmi_header header;
60 struct calling_interface_token tokens[];
67 static struct quirk_entry *quirks;
69 static struct quirk_entry quirk_dell_vostro_v130 = {
73 static int dmi_matched(const struct dmi_system_id *dmi)
75 quirks = dmi->driver_data;
79 static int da_command_address;
80 static int da_command_code;
81 static int da_num_tokens;
82 static struct calling_interface_token *da_tokens;
84 static struct platform_driver platform_driver = {
86 .name = "dell-laptop",
91 static struct platform_device *platform_device;
92 static struct backlight_device *dell_backlight_device;
93 static struct rfkill *wifi_rfkill;
94 static struct rfkill *bluetooth_rfkill;
95 static struct rfkill *wwan_rfkill;
97 static const struct dmi_system_id dell_device_table[] __initconst = {
99 .ident = "Dell laptop",
101 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
102 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
108 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
112 .ident = "Dell Computer Corporation",
114 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
115 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
120 MODULE_DEVICE_TABLE(dmi, dell_device_table);
122 static struct dmi_system_id dell_quirks[] = {
124 .callback = dmi_matched,
125 .ident = "Dell Vostro V130",
127 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
130 .driver_data = &quirk_dell_vostro_v130,
133 .callback = dmi_matched,
134 .ident = "Dell Vostro V131",
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
137 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
139 .driver_data = &quirk_dell_vostro_v130,
142 .callback = dmi_matched,
143 .ident = "Dell Vostro 3350",
145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
146 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
148 .driver_data = &quirk_dell_vostro_v130,
151 .callback = dmi_matched,
152 .ident = "Dell Vostro 3555",
154 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
155 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
157 .driver_data = &quirk_dell_vostro_v130,
160 .callback = dmi_matched,
161 .ident = "Dell Inspiron N311z",
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
166 .driver_data = &quirk_dell_vostro_v130,
169 .callback = dmi_matched,
170 .ident = "Dell Inspiron M5110",
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
175 .driver_data = &quirk_dell_vostro_v130,
178 .callback = dmi_matched,
179 .ident = "Dell Vostro 3360",
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
184 .driver_data = &quirk_dell_vostro_v130,
187 .callback = dmi_matched,
188 .ident = "Dell Vostro 3460",
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
193 .driver_data = &quirk_dell_vostro_v130,
196 .callback = dmi_matched,
197 .ident = "Dell Vostro 3560",
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
202 .driver_data = &quirk_dell_vostro_v130,
205 .callback = dmi_matched,
206 .ident = "Dell Vostro 3450",
208 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
209 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
211 .driver_data = &quirk_dell_vostro_v130,
214 .callback = dmi_matched,
215 .ident = "Dell Inspiron 5420",
217 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
218 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
220 .driver_data = &quirk_dell_vostro_v130,
223 .callback = dmi_matched,
224 .ident = "Dell Inspiron 5520",
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
229 .driver_data = &quirk_dell_vostro_v130,
232 .callback = dmi_matched,
233 .ident = "Dell Inspiron 5720",
235 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
236 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
238 .driver_data = &quirk_dell_vostro_v130,
241 .callback = dmi_matched,
242 .ident = "Dell Inspiron 7420",
244 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
245 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
247 .driver_data = &quirk_dell_vostro_v130,
250 .callback = dmi_matched,
251 .ident = "Dell Inspiron 7520",
253 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
254 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
256 .driver_data = &quirk_dell_vostro_v130,
259 .callback = dmi_matched,
260 .ident = "Dell Inspiron 7720",
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
263 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
265 .driver_data = &quirk_dell_vostro_v130,
270 static struct calling_interface_buffer *buffer;
271 static struct page *bufferpage;
272 static DEFINE_MUTEX(buffer_mutex);
274 static int hwswitch_state;
276 static void get_buffer(void)
278 mutex_lock(&buffer_mutex);
279 memset(buffer, 0, sizeof(struct calling_interface_buffer));
282 static void release_buffer(void)
284 mutex_unlock(&buffer_mutex);
287 static void __init parse_da_table(const struct dmi_header *dm)
289 /* Final token is a terminator, so we don't want to copy it */
290 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
291 struct calling_interface_token *new_da_tokens;
292 struct calling_interface_structure *table =
293 container_of(dm, struct calling_interface_structure, header);
295 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
301 da_command_address = table->cmdIOAddress;
302 da_command_code = table->cmdIOCode;
304 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
305 sizeof(struct calling_interface_token),
310 da_tokens = new_da_tokens;
312 memcpy(da_tokens+da_num_tokens, table->tokens,
313 sizeof(struct calling_interface_token) * tokens);
315 da_num_tokens += tokens;
318 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
321 case 0xd4: /* Indexed IO */
322 case 0xd5: /* Protected Area Type 1 */
323 case 0xd6: /* Protected Area Type 2 */
325 case 0xda: /* Calling interface */
331 static int find_token_location(int tokenid)
334 for (i = 0; i < da_num_tokens; i++) {
335 if (da_tokens[i].tokenID == tokenid)
336 return da_tokens[i].location;
342 static struct calling_interface_buffer *
343 dell_send_request(struct calling_interface_buffer *buffer, int class,
346 struct smi_cmd command;
348 command.magic = SMI_CMD_MAGIC;
349 command.command_address = da_command_address;
350 command.command_code = da_command_code;
351 command.ebx = virt_to_phys(buffer);
352 command.ecx = 0x42534931;
354 buffer->class = class;
355 buffer->select = select;
357 dcdbas_smi_request(&command);
362 /* Derived from information in DellWirelessCtl.cpp:
363 Class 17, select 11 is radio control. It returns an array of 32-bit values.
365 Input byte 0 = 0: Wireless information
367 result[0]: return code
369 Bit 0: Hardware switch supported
370 Bit 1: Wifi locator supported
371 Bit 2: Wifi is supported
372 Bit 3: Bluetooth is supported
373 Bit 4: WWAN is supported
374 Bit 5: Wireless keyboard supported
376 Bit 8: Wifi is installed
377 Bit 9: Bluetooth is installed
378 Bit 10: WWAN is installed
380 Bit 16: Hardware switch is on
381 Bit 17: Wifi is blocked
382 Bit 18: Bluetooth is blocked
383 Bit 19: WWAN is blocked
385 result[2]: NVRAM size in bytes
386 result[3]: NVRAM format version number
388 Input byte 0 = 2: Wireless switch configuration
389 result[0]: return code
391 Bit 0: Wifi controlled by switch
392 Bit 1: Bluetooth controlled by switch
393 Bit 2: WWAN controlled by switch
395 Bit 7: Wireless switch config locked
396 Bit 8: Wifi locator enabled
398 Bit 15: Wifi locator setting locked
402 static int dell_rfkill_set(void *data, bool blocked)
404 int disable = blocked ? 1 : 0;
405 unsigned long radio = (unsigned long)data;
406 int hwswitch_bit = (unsigned long)data - 1;
410 dell_send_request(buffer, 17, 11);
412 /* If the hardware switch controls this radio, and the hardware
413 switch is disabled, don't allow changing the software state */
414 if ((hwswitch_state & BIT(hwswitch_bit)) &&
415 !(buffer->output[1] & BIT(16))) {
420 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
421 dell_send_request(buffer, 17, 11);
428 static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
431 if (!(status & BIT(0))) {
432 /* No hw-switch, sync BIOS state to sw_state */
433 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
437 static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
440 if (hwswitch_state & (BIT(radio - 1)))
441 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
444 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
449 dell_send_request(buffer, 17, 11);
450 status = buffer->output[1];
453 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
456 static const struct rfkill_ops dell_rfkill_ops = {
457 .set_block = dell_rfkill_set,
458 .query = dell_rfkill_query,
461 static struct dentry *dell_laptop_dir;
463 static int dell_debugfs_show(struct seq_file *s, void *data)
468 dell_send_request(buffer, 17, 11);
469 status = buffer->output[1];
472 seq_printf(s, "status:\t0x%X\n", status);
473 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
475 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
476 (status & BIT(1)) >> 1);
477 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
478 (status & BIT(2)) >> 2);
479 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
480 (status & BIT(3)) >> 3);
481 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
482 (status & BIT(4)) >> 4);
483 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
484 (status & BIT(5)) >> 5);
485 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
486 (status & BIT(8)) >> 8);
487 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
488 (status & BIT(9)) >> 9);
489 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
490 (status & BIT(10)) >> 10);
491 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
492 (status & BIT(16)) >> 16);
493 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
494 (status & BIT(17)) >> 17);
495 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
496 (status & BIT(18)) >> 18);
497 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
498 (status & BIT(19)) >> 19);
500 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
501 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
502 hwswitch_state & BIT(0));
503 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
504 (hwswitch_state & BIT(1)) >> 1);
505 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
506 (hwswitch_state & BIT(2)) >> 2);
507 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
508 (hwswitch_state & BIT(7)) >> 7);
509 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
510 (hwswitch_state & BIT(8)) >> 8);
511 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
512 (hwswitch_state & BIT(15)) >> 15);
517 static int dell_debugfs_open(struct inode *inode, struct file *file)
519 return single_open(file, dell_debugfs_show, inode->i_private);
522 static const struct file_operations dell_debugfs_fops = {
523 .owner = THIS_MODULE,
524 .open = dell_debugfs_open,
527 .release = single_release,
530 static void dell_update_rfkill(struct work_struct *ignored)
535 dell_send_request(buffer, 17, 11);
536 status = buffer->output[1];
540 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
541 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
543 if (bluetooth_rfkill) {
544 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
545 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
548 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
549 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
552 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
555 static int __init dell_setup_rfkill(void)
562 * rfkill causes trouble on various non Latitudes, according to Dell
563 * actually testing the rfkill functionality is only done on Latitudes.
565 product = dmi_get_system_info(DMI_PRODUCT_NAME);
566 if (!product || strncmp(product, "Latitude", 8))
570 dell_send_request(buffer, 17, 11);
571 status = buffer->output[1];
572 buffer->input[0] = 0x2;
573 dell_send_request(buffer, 17, 11);
574 hwswitch_state = buffer->output[1];
575 /* If there is no hwswitch, then clear all hw-controlled bits */
576 if (!(status & BIT(0)))
577 hwswitch_state &= ~7;
580 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
581 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
583 &dell_rfkill_ops, (void *) 1);
588 ret = rfkill_register(wifi_rfkill);
593 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
594 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
595 &platform_device->dev,
596 RFKILL_TYPE_BLUETOOTH,
597 &dell_rfkill_ops, (void *) 2);
598 if (!bluetooth_rfkill) {
602 ret = rfkill_register(bluetooth_rfkill);
607 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
608 wwan_rfkill = rfkill_alloc("dell-wwan",
609 &platform_device->dev,
611 &dell_rfkill_ops, (void *) 3);
616 ret = rfkill_register(wwan_rfkill);
623 rfkill_destroy(wwan_rfkill);
624 if (bluetooth_rfkill)
625 rfkill_unregister(bluetooth_rfkill);
627 rfkill_destroy(bluetooth_rfkill);
629 rfkill_unregister(wifi_rfkill);
631 rfkill_destroy(wifi_rfkill);
636 static void dell_cleanup_rfkill(void)
639 rfkill_unregister(wifi_rfkill);
640 rfkill_destroy(wifi_rfkill);
642 if (bluetooth_rfkill) {
643 rfkill_unregister(bluetooth_rfkill);
644 rfkill_destroy(bluetooth_rfkill);
647 rfkill_unregister(wwan_rfkill);
648 rfkill_destroy(wwan_rfkill);
652 static int dell_send_intensity(struct backlight_device *bd)
657 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
658 buffer->input[1] = bd->props.brightness;
660 if (buffer->input[0] == -1) {
665 if (power_supply_is_system_supplied() > 0)
666 dell_send_request(buffer, 1, 2);
668 dell_send_request(buffer, 1, 1);
675 static int dell_get_intensity(struct backlight_device *bd)
680 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
682 if (buffer->input[0] == -1) {
687 if (power_supply_is_system_supplied() > 0)
688 dell_send_request(buffer, 0, 2);
690 dell_send_request(buffer, 0, 1);
692 ret = buffer->output[1];
699 static const struct backlight_ops dell_ops = {
700 .get_brightness = dell_get_intensity,
701 .update_status = dell_send_intensity,
704 static void touchpad_led_on(void)
708 i8042_command(&data, command | 1 << 12);
711 static void touchpad_led_off(void)
715 i8042_command(&data, command | 1 << 12);
718 static void touchpad_led_set(struct led_classdev *led_cdev,
719 enum led_brightness value)
727 static struct led_classdev touchpad_led = {
728 .name = "dell-laptop::touchpad",
729 .brightness_set = touchpad_led_set,
730 .flags = LED_CORE_SUSPENDRESUME,
733 static int touchpad_led_init(struct device *dev)
735 return led_classdev_register(dev, &touchpad_led);
738 static void touchpad_led_exit(void)
740 led_classdev_unregister(&touchpad_led);
743 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
746 static bool extended;
751 if (unlikely(data == 0xe0)) {
754 } else if (unlikely(extended)) {
757 schedule_delayed_work(&dell_rfkill_work,
758 round_jiffies_relative(HZ));
767 static int __init dell_init(void)
769 int max_intensity = 0;
772 if (!dmi_check_system(dell_device_table))
776 /* find if this machine support other functions */
777 dmi_check_system(dell_quirks);
779 dmi_walk(find_tokens, NULL);
782 pr_info("Unable to find dmi tokens\n");
786 ret = platform_driver_register(&platform_driver);
788 goto fail_platform_driver;
789 platform_device = platform_device_alloc("dell-laptop", -1);
790 if (!platform_device) {
792 goto fail_platform_device1;
794 ret = platform_device_add(platform_device);
796 goto fail_platform_device2;
799 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
800 * is passed to SMI handler.
802 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
807 buffer = page_address(bufferpage);
809 ret = dell_setup_rfkill();
812 pr_warn("Unable to setup rfkill\n");
816 ret = i8042_install_filter(dell_laptop_i8042_filter);
818 pr_warn("Unable to install key filter\n");
822 if (quirks && quirks->touchpad_led)
823 touchpad_led_init(&platform_device->dev);
825 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
826 if (dell_laptop_dir != NULL)
827 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
831 /* In the event of an ACPI backlight being available, don't
832 * register the platform controller.
834 if (acpi_video_backlight_support())
839 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
840 if (buffer->input[0] != -1) {
841 dell_send_request(buffer, 0, 2);
842 max_intensity = buffer->output[3];
847 struct backlight_properties props;
848 memset(&props, 0, sizeof(struct backlight_properties));
849 props.type = BACKLIGHT_PLATFORM;
850 props.max_brightness = max_intensity;
851 dell_backlight_device = backlight_device_register("dell_backlight",
852 &platform_device->dev,
857 if (IS_ERR(dell_backlight_device)) {
858 ret = PTR_ERR(dell_backlight_device);
859 dell_backlight_device = NULL;
863 dell_backlight_device->props.brightness =
864 dell_get_intensity(dell_backlight_device);
865 backlight_update_status(dell_backlight_device);
871 i8042_remove_filter(dell_laptop_i8042_filter);
872 cancel_delayed_work_sync(&dell_rfkill_work);
874 dell_cleanup_rfkill();
876 free_page((unsigned long)bufferpage);
878 platform_device_del(platform_device);
879 fail_platform_device2:
880 platform_device_put(platform_device);
881 fail_platform_device1:
882 platform_driver_unregister(&platform_driver);
883 fail_platform_driver:
888 static void __exit dell_exit(void)
890 debugfs_remove_recursive(dell_laptop_dir);
891 if (quirks && quirks->touchpad_led)
893 i8042_remove_filter(dell_laptop_i8042_filter);
894 cancel_delayed_work_sync(&dell_rfkill_work);
895 backlight_device_unregister(dell_backlight_device);
896 dell_cleanup_rfkill();
897 if (platform_device) {
898 platform_device_unregister(platform_device);
899 platform_driver_unregister(&platform_driver);
902 free_page((unsigned long)buffer);
905 module_init(dell_init);
906 module_exit(dell_exit);
908 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
909 MODULE_DESCRIPTION("Dell laptop driver");
910 MODULE_LICENSE("GPL");