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 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/backlight.h>
19 #include <linux/err.h>
20 #include <linux/dmi.h>
22 #include <linux/rfkill.h>
23 #include <linux/power_supply.h>
24 #include <linux/acpi.h>
26 #include <linux/i8042.h>
27 #include <linux/slab.h>
28 #include "../../firmware/dcdbas.h"
30 #define BRIGHTNESS_TOKEN 0x7d
32 /* This structure will be modified by the firmware when we enter
33 * system management mode, hence the volatiles */
35 struct calling_interface_buffer {
38 volatile u32 input[4];
39 volatile u32 output[4];
42 struct calling_interface_token {
51 struct calling_interface_structure {
52 struct dmi_header header;
56 struct calling_interface_token tokens[];
59 static int da_command_address;
60 static int da_command_code;
61 static int da_num_tokens;
62 static struct calling_interface_token *da_tokens;
64 static struct platform_driver platform_driver = {
66 .name = "dell-laptop",
71 static struct platform_device *platform_device;
72 static struct backlight_device *dell_backlight_device;
73 static struct rfkill *wifi_rfkill;
74 static struct rfkill *bluetooth_rfkill;
75 static struct rfkill *wwan_rfkill;
77 static const struct dmi_system_id __initdata dell_device_table[] = {
79 .ident = "Dell laptop",
81 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
82 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
87 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
88 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
92 .ident = "Dell Computer Corporation",
94 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
95 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
101 static struct dmi_system_id __devinitdata dell_blacklist[] = {
102 /* Supported by compal-laptop */
104 .ident = "Dell Mini 9",
106 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
107 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
111 .ident = "Dell Mini 10",
113 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
114 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
118 .ident = "Dell Mini 10v",
120 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
121 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
125 .ident = "Dell Inspiron 11z",
127 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
128 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
132 .ident = "Dell Mini 12",
134 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
135 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
141 static struct calling_interface_buffer *buffer;
142 static struct page *bufferpage;
143 static DEFINE_MUTEX(buffer_mutex);
145 static int hwswitch_state;
147 static void get_buffer(void)
149 mutex_lock(&buffer_mutex);
150 memset(buffer, 0, sizeof(struct calling_interface_buffer));
153 static void release_buffer(void)
155 mutex_unlock(&buffer_mutex);
158 static void __init parse_da_table(const struct dmi_header *dm)
160 /* Final token is a terminator, so we don't want to copy it */
161 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
162 struct calling_interface_structure *table =
163 container_of(dm, struct calling_interface_structure, header);
165 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
171 da_command_address = table->cmdIOAddress;
172 da_command_code = table->cmdIOCode;
174 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
175 sizeof(struct calling_interface_token),
181 memcpy(da_tokens+da_num_tokens, table->tokens,
182 sizeof(struct calling_interface_token) * tokens);
184 da_num_tokens += tokens;
187 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
190 case 0xd4: /* Indexed IO */
192 case 0xd5: /* Protected Area Type 1 */
194 case 0xd6: /* Protected Area Type 2 */
196 case 0xda: /* Calling interface */
202 static int find_token_location(int tokenid)
205 for (i = 0; i < da_num_tokens; i++) {
206 if (da_tokens[i].tokenID == tokenid)
207 return da_tokens[i].location;
213 static struct calling_interface_buffer *
214 dell_send_request(struct calling_interface_buffer *buffer, int class,
217 struct smi_cmd command;
219 command.magic = SMI_CMD_MAGIC;
220 command.command_address = da_command_address;
221 command.command_code = da_command_code;
222 command.ebx = virt_to_phys(buffer);
223 command.ecx = 0x42534931;
225 buffer->class = class;
226 buffer->select = select;
228 dcdbas_smi_request(&command);
233 /* Derived from information in DellWirelessCtl.cpp:
234 Class 17, select 11 is radio control. It returns an array of 32-bit values.
236 Input byte 0 = 0: Wireless information
238 result[0]: return code
240 Bit 0: Hardware switch supported
241 Bit 1: Wifi locator supported
242 Bit 2: Wifi is supported
243 Bit 3: Bluetooth is supported
244 Bit 4: WWAN is supported
245 Bit 5: Wireless keyboard supported
247 Bit 8: Wifi is installed
248 Bit 9: Bluetooth is installed
249 Bit 10: WWAN is installed
251 Bit 16: Hardware switch is on
252 Bit 17: Wifi is blocked
253 Bit 18: Bluetooth is blocked
254 Bit 19: WWAN is blocked
256 result[2]: NVRAM size in bytes
257 result[3]: NVRAM format version number
259 Input byte 0 = 2: Wireless switch configuration
260 result[0]: return code
262 Bit 0: Wifi controlled by switch
263 Bit 1: Bluetooth controlled by switch
264 Bit 2: WWAN controlled by switch
266 Bit 7: Wireless switch config locked
267 Bit 8: Wifi locator enabled
269 Bit 15: Wifi locator setting locked
273 static int dell_rfkill_set(void *data, bool blocked)
275 int disable = blocked ? 1 : 0;
276 unsigned long radio = (unsigned long)data;
277 int hwswitch_bit = (unsigned long)data - 1;
281 dell_send_request(buffer, 17, 11);
283 /* If the hardware switch controls this radio, and the hardware
284 switch is disabled, don't allow changing the software state */
285 if ((hwswitch_state & BIT(hwswitch_bit)) &&
286 !(buffer->output[1] & BIT(16))) {
291 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
292 dell_send_request(buffer, 17, 11);
299 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
302 int bit = (unsigned long)data + 16;
303 int hwswitch_bit = (unsigned long)data - 1;
306 dell_send_request(buffer, 17, 11);
307 status = buffer->output[1];
310 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
312 if (hwswitch_state & (BIT(hwswitch_bit)))
313 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
316 static const struct rfkill_ops dell_rfkill_ops = {
317 .set_block = dell_rfkill_set,
318 .query = dell_rfkill_query,
321 static void dell_update_rfkill(struct work_struct *ignored)
324 dell_rfkill_query(wifi_rfkill, (void *)1);
325 if (bluetooth_rfkill)
326 dell_rfkill_query(bluetooth_rfkill, (void *)2);
328 dell_rfkill_query(wwan_rfkill, (void *)3);
330 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
333 static int __init dell_setup_rfkill(void)
338 if (dmi_check_system(dell_blacklist)) {
339 printk(KERN_INFO "dell-laptop: Blacklisted hardware detected - "
340 "not enabling rfkill\n");
345 dell_send_request(buffer, 17, 11);
346 status = buffer->output[1];
347 buffer->input[0] = 0x2;
348 dell_send_request(buffer, 17, 11);
349 hwswitch_state = buffer->output[1];
352 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
353 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
355 &dell_rfkill_ops, (void *) 1);
360 ret = rfkill_register(wifi_rfkill);
365 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
366 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
367 &platform_device->dev,
368 RFKILL_TYPE_BLUETOOTH,
369 &dell_rfkill_ops, (void *) 2);
370 if (!bluetooth_rfkill) {
374 ret = rfkill_register(bluetooth_rfkill);
379 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
380 wwan_rfkill = rfkill_alloc("dell-wwan",
381 &platform_device->dev,
383 &dell_rfkill_ops, (void *) 3);
388 ret = rfkill_register(wwan_rfkill);
395 rfkill_destroy(wwan_rfkill);
396 if (bluetooth_rfkill)
397 rfkill_unregister(bluetooth_rfkill);
399 rfkill_destroy(bluetooth_rfkill);
401 rfkill_unregister(wifi_rfkill);
403 rfkill_destroy(wifi_rfkill);
408 static void dell_cleanup_rfkill(void)
411 rfkill_unregister(wifi_rfkill);
412 rfkill_destroy(wifi_rfkill);
414 if (bluetooth_rfkill) {
415 rfkill_unregister(bluetooth_rfkill);
416 rfkill_destroy(bluetooth_rfkill);
419 rfkill_unregister(wwan_rfkill);
420 rfkill_destroy(wwan_rfkill);
424 static int dell_send_intensity(struct backlight_device *bd)
429 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
430 buffer->input[1] = bd->props.brightness;
432 if (buffer->input[0] == -1) {
437 if (power_supply_is_system_supplied() > 0)
438 dell_send_request(buffer, 1, 2);
440 dell_send_request(buffer, 1, 1);
447 static int dell_get_intensity(struct backlight_device *bd)
452 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
454 if (buffer->input[0] == -1) {
459 if (power_supply_is_system_supplied() > 0)
460 dell_send_request(buffer, 0, 2);
462 dell_send_request(buffer, 0, 1);
468 return buffer->output[1];
471 static struct backlight_ops dell_ops = {
472 .get_brightness = dell_get_intensity,
473 .update_status = dell_send_intensity,
476 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
479 static bool extended;
484 if (unlikely(data == 0xe0)) {
487 } else if (unlikely(extended)) {
490 schedule_delayed_work(&dell_rfkill_work,
491 round_jiffies_relative(HZ));
500 static int __init dell_init(void)
502 int max_intensity = 0;
505 if (!dmi_check_system(dell_device_table))
508 dmi_walk(find_tokens, NULL);
511 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
515 ret = platform_driver_register(&platform_driver);
517 goto fail_platform_driver;
518 platform_device = platform_device_alloc("dell-laptop", -1);
519 if (!platform_device) {
521 goto fail_platform_device1;
523 ret = platform_device_add(platform_device);
525 goto fail_platform_device2;
528 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
529 * is passed to SMI handler.
531 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
535 buffer = page_address(bufferpage);
536 mutex_init(&buffer_mutex);
538 ret = dell_setup_rfkill();
541 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
545 ret = i8042_install_filter(dell_laptop_i8042_filter);
548 "dell-laptop: Unable to install key filter\n");
553 /* In the event of an ACPI backlight being available, don't
554 * register the platform controller.
556 if (acpi_video_backlight_support())
561 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
562 if (buffer->input[0] != -1) {
563 dell_send_request(buffer, 0, 2);
564 max_intensity = buffer->output[3];
569 struct backlight_properties props;
570 memset(&props, 0, sizeof(struct backlight_properties));
571 props.max_brightness = max_intensity;
572 dell_backlight_device = backlight_device_register("dell_backlight",
573 &platform_device->dev,
578 if (IS_ERR(dell_backlight_device)) {
579 ret = PTR_ERR(dell_backlight_device);
580 dell_backlight_device = NULL;
584 dell_backlight_device->props.brightness =
585 dell_get_intensity(dell_backlight_device);
586 backlight_update_status(dell_backlight_device);
592 i8042_remove_filter(dell_laptop_i8042_filter);
593 cancel_delayed_work_sync(&dell_rfkill_work);
595 dell_cleanup_rfkill();
597 free_page((unsigned long)bufferpage);
599 platform_device_del(platform_device);
600 fail_platform_device2:
601 platform_device_put(platform_device);
602 fail_platform_device1:
603 platform_driver_unregister(&platform_driver);
604 fail_platform_driver:
609 static void __exit dell_exit(void)
611 i8042_remove_filter(dell_laptop_i8042_filter);
612 cancel_delayed_work_sync(&dell_rfkill_work);
613 backlight_device_unregister(dell_backlight_device);
614 dell_cleanup_rfkill();
615 if (platform_device) {
616 platform_device_unregister(platform_device);
617 platform_driver_unregister(&platform_driver);
620 free_page((unsigned long)buffer);
623 module_init(dell_init);
624 module_exit(dell_exit);
626 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
627 MODULE_DESCRIPTION("Dell laptop driver");
628 MODULE_LICENSE("GPL");
629 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
630 MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
631 MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");