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>
25 #include "../../firmware/dcdbas.h"
27 #define BRIGHTNESS_TOKEN 0x7d
29 /* This structure will be modified by the firmware when we enter
30 * system management mode, hence the volatiles */
32 struct calling_interface_buffer {
35 volatile u32 input[4];
36 volatile u32 output[4];
39 struct calling_interface_token {
48 struct calling_interface_structure {
49 struct dmi_header header;
53 struct calling_interface_token tokens[];
56 static int da_command_address;
57 static int da_command_code;
58 static int da_num_tokens;
59 static struct calling_interface_token *da_tokens;
61 static struct platform_driver platform_driver = {
63 .name = "dell-laptop",
68 static struct platform_device *platform_device;
69 static struct backlight_device *dell_backlight_device;
70 static struct rfkill *wifi_rfkill;
71 static struct rfkill *bluetooth_rfkill;
72 static struct rfkill *wwan_rfkill;
74 static const struct dmi_system_id __initdata dell_device_table[] = {
76 .ident = "Dell laptop",
78 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
79 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
85 static void __init parse_da_table(const struct dmi_header *dm)
87 /* Final token is a terminator, so we don't want to copy it */
88 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
89 struct calling_interface_structure *table =
90 container_of(dm, struct calling_interface_structure, header);
92 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
98 da_command_address = table->cmdIOAddress;
99 da_command_code = table->cmdIOCode;
101 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
102 sizeof(struct calling_interface_token),
108 memcpy(da_tokens+da_num_tokens, table->tokens,
109 sizeof(struct calling_interface_token) * tokens);
111 da_num_tokens += tokens;
114 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
117 case 0xd4: /* Indexed IO */
119 case 0xd5: /* Protected Area Type 1 */
121 case 0xd6: /* Protected Area Type 2 */
123 case 0xda: /* Calling interface */
129 static int find_token_location(int tokenid)
132 for (i = 0; i < da_num_tokens; i++) {
133 if (da_tokens[i].tokenID == tokenid)
134 return da_tokens[i].location;
140 static struct calling_interface_buffer *
141 dell_send_request(struct calling_interface_buffer *buffer, int class,
144 struct smi_cmd command;
146 command.magic = SMI_CMD_MAGIC;
147 command.command_address = da_command_address;
148 command.command_code = da_command_code;
149 command.ebx = virt_to_phys(buffer);
150 command.ecx = 0x42534931;
152 buffer->class = class;
153 buffer->select = select;
155 dcdbas_smi_request(&command);
160 /* Derived from information in DellWirelessCtl.cpp:
161 Class 17, select 11 is radio control. It returns an array of 32-bit values.
163 result[0]: return code
165 Bit 0: Hardware switch supported
166 Bit 1: Wifi locator supported
167 Bit 2: Wifi is supported
168 Bit 3: Bluetooth is supported
169 Bit 4: WWAN is supported
170 Bit 5: Wireless keyboard supported
172 Bit 8: Wifi is installed
173 Bit 9: Bluetooth is installed
174 Bit 10: WWAN is installed
176 Bit 16: Hardware switch is on
177 Bit 17: Wifi is blocked
178 Bit 18: Bluetooth is blocked
179 Bit 19: WWAN is blocked
181 result[2]: NVRAM size in bytes
182 result[3]: NVRAM format version number
185 static int dell_rfkill_set(void *data, bool blocked)
187 struct calling_interface_buffer buffer;
188 int disable = blocked ? 1 : 0;
189 unsigned long radio = (unsigned long)data;
191 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
192 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
193 dell_send_request(&buffer, 17, 11);
198 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
200 struct calling_interface_buffer buffer;
202 int bit = (unsigned long)data + 16;
204 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
205 dell_send_request(&buffer, 17, 11);
206 status = buffer.output[1];
208 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
209 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
212 static const struct rfkill_ops dell_rfkill_ops = {
213 .set_block = dell_rfkill_set,
214 .query = dell_rfkill_query,
217 static int __init dell_setup_rfkill(void)
219 struct calling_interface_buffer buffer;
223 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
224 dell_send_request(&buffer, 17, 11);
225 status = buffer.output[1];
227 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
228 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
230 &dell_rfkill_ops, (void *) 1);
235 ret = rfkill_register(wifi_rfkill);
240 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
241 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
242 &platform_device->dev,
243 RFKILL_TYPE_BLUETOOTH,
244 &dell_rfkill_ops, (void *) 2);
245 if (!bluetooth_rfkill) {
249 ret = rfkill_register(bluetooth_rfkill);
254 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
255 wwan_rfkill = rfkill_alloc("dell-wwan",
256 &platform_device->dev,
258 &dell_rfkill_ops, (void *) 3);
263 ret = rfkill_register(wwan_rfkill);
270 rfkill_destroy(wwan_rfkill);
271 if (bluetooth_rfkill)
272 rfkill_unregister(bluetooth_rfkill);
274 rfkill_destroy(bluetooth_rfkill);
276 rfkill_unregister(wifi_rfkill);
278 rfkill_destroy(wifi_rfkill);
283 static void dell_cleanup_rfkill(void)
286 rfkill_unregister(wifi_rfkill);
287 rfkill_destroy(wifi_rfkill);
289 if (bluetooth_rfkill) {
290 rfkill_unregister(bluetooth_rfkill);
291 rfkill_destroy(bluetooth_rfkill);
294 rfkill_unregister(wwan_rfkill);
295 rfkill_destroy(wwan_rfkill);
299 static int dell_send_intensity(struct backlight_device *bd)
301 struct calling_interface_buffer buffer;
303 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
304 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
305 buffer.input[1] = bd->props.brightness;
307 if (buffer.input[0] == -1)
310 if (power_supply_is_system_supplied() > 0)
311 dell_send_request(&buffer, 1, 2);
313 dell_send_request(&buffer, 1, 1);
318 static int dell_get_intensity(struct backlight_device *bd)
320 struct calling_interface_buffer buffer;
322 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
323 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
325 if (buffer.input[0] == -1)
328 if (power_supply_is_system_supplied() > 0)
329 dell_send_request(&buffer, 0, 2);
331 dell_send_request(&buffer, 0, 1);
333 return buffer.output[1];
336 static struct backlight_ops dell_ops = {
337 .get_brightness = dell_get_intensity,
338 .update_status = dell_send_intensity,
341 static int __init dell_init(void)
343 struct calling_interface_buffer buffer;
344 int max_intensity = 0;
347 if (!dmi_check_system(dell_device_table))
350 dmi_walk(find_tokens, NULL);
353 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
357 ret = platform_driver_register(&platform_driver);
359 goto fail_platform_driver;
360 platform_device = platform_device_alloc("dell-laptop", -1);
361 if (!platform_device) {
363 goto fail_platform_device1;
365 ret = platform_device_add(platform_device);
367 goto fail_platform_device2;
369 ret = dell_setup_rfkill();
372 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
377 /* In the event of an ACPI backlight being available, don't
378 * register the platform controller.
380 if (acpi_video_backlight_support())
384 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
385 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
387 if (buffer.input[0] != -1) {
388 dell_send_request(&buffer, 0, 2);
389 max_intensity = buffer.output[3];
393 dell_backlight_device = backlight_device_register(
395 &platform_device->dev, NULL,
398 if (IS_ERR(dell_backlight_device)) {
399 ret = PTR_ERR(dell_backlight_device);
400 dell_backlight_device = NULL;
404 dell_backlight_device->props.max_brightness = max_intensity;
405 dell_backlight_device->props.brightness =
406 dell_get_intensity(dell_backlight_device);
407 backlight_update_status(dell_backlight_device);
413 dell_cleanup_rfkill();
415 platform_device_del(platform_device);
416 fail_platform_device2:
417 platform_device_put(platform_device);
418 fail_platform_device1:
419 platform_driver_unregister(&platform_driver);
420 fail_platform_driver:
425 static void __exit dell_exit(void)
427 backlight_device_unregister(dell_backlight_device);
428 dell_cleanup_rfkill();
431 module_init(dell_init);
432 module_exit(dell_exit);
434 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
435 MODULE_DESCRIPTION("Dell laptop driver");
436 MODULE_LICENSE("GPL");
437 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");