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 backlight_device *dell_backlight_device;
62 static struct rfkill *wifi_rfkill;
63 static struct rfkill *bluetooth_rfkill;
64 static struct rfkill *wwan_rfkill;
66 static const struct dmi_system_id __initdata dell_device_table[] = {
68 .ident = "Dell laptop",
70 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
71 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
77 static void parse_da_table(const struct dmi_header *dm)
79 /* Final token is a terminator, so we don't want to copy it */
80 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
81 struct calling_interface_structure *table =
82 container_of(dm, struct calling_interface_structure, header);
84 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
90 da_command_address = table->cmdIOAddress;
91 da_command_code = table->cmdIOCode;
93 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
94 sizeof(struct calling_interface_token),
100 memcpy(da_tokens+da_num_tokens, table->tokens,
101 sizeof(struct calling_interface_token) * tokens);
103 da_num_tokens += tokens;
106 static void find_tokens(const struct dmi_header *dm)
109 case 0xd4: /* Indexed IO */
111 case 0xd5: /* Protected Area Type 1 */
113 case 0xd6: /* Protected Area Type 2 */
115 case 0xda: /* Calling interface */
121 static int find_token_location(int tokenid)
124 for (i = 0; i < da_num_tokens; i++) {
125 if (da_tokens[i].tokenID == tokenid)
126 return da_tokens[i].location;
132 static struct calling_interface_buffer *
133 dell_send_request(struct calling_interface_buffer *buffer, int class,
136 struct smi_cmd command;
138 command.magic = SMI_CMD_MAGIC;
139 command.command_address = da_command_address;
140 command.command_code = da_command_code;
141 command.ebx = virt_to_phys(buffer);
142 command.ecx = 0x42534931;
144 buffer->class = class;
145 buffer->select = select;
147 dcdbas_smi_request(&command);
152 /* Derived from information in DellWirelessCtl.cpp:
153 Class 17, select 11 is radio control. It returns an array of 32-bit values.
155 result[0]: return code
157 Bit 0: Hardware switch supported
158 Bit 1: Wifi locator supported
159 Bit 2: Wifi is supported
160 Bit 3: Bluetooth is supported
161 Bit 4: WWAN is supported
162 Bit 5: Wireless keyboard supported
164 Bit 8: Wifi is installed
165 Bit 9: Bluetooth is installed
166 Bit 10: WWAN is installed
168 Bit 16: Hardware switch is on
169 Bit 17: Wifi is blocked
170 Bit 18: Bluetooth is blocked
171 Bit 19: WWAN is blocked
173 result[2]: NVRAM size in bytes
174 result[3]: NVRAM format version number
177 static int dell_rfkill_set(int radio, enum rfkill_state state)
179 struct calling_interface_buffer buffer;
180 int disable = (state == RFKILL_STATE_UNBLOCKED) ? 0 : 1;
182 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
183 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
184 dell_send_request(&buffer, 17, 11);
189 static int dell_wifi_set(void *data, enum rfkill_state state)
191 return dell_rfkill_set(1, state);
194 static int dell_bluetooth_set(void *data, enum rfkill_state state)
196 return dell_rfkill_set(2, state);
199 static int dell_wwan_set(void *data, enum rfkill_state state)
201 return dell_rfkill_set(3, state);
204 static int dell_rfkill_get(int bit, enum rfkill_state *state)
206 struct calling_interface_buffer buffer;
208 int new_state = RFKILL_STATE_HARD_BLOCKED;
210 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
211 dell_send_request(&buffer, 17, 11);
212 status = buffer.output[1];
214 if (status & (1<<16))
215 new_state = RFKILL_STATE_SOFT_BLOCKED;
217 if (status & (1<<bit))
220 *state = RFKILL_STATE_UNBLOCKED;
225 static int dell_wifi_get(void *data, enum rfkill_state *state)
227 return dell_rfkill_get(17, state);
230 static int dell_bluetooth_get(void *data, enum rfkill_state *state)
232 return dell_rfkill_get(18, state);
235 static int dell_wwan_get(void *data, enum rfkill_state *state)
237 return dell_rfkill_get(19, state);
240 static int dell_setup_rfkill(void)
242 struct calling_interface_buffer buffer;
246 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
247 dell_send_request(&buffer, 17, 11);
248 status = buffer.output[1];
250 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
251 wifi_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_WLAN);
254 wifi_rfkill->name = "dell-wifi";
255 wifi_rfkill->toggle_radio = dell_wifi_set;
256 wifi_rfkill->get_state = dell_wifi_get;
257 ret = rfkill_register(wifi_rfkill);
262 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
263 bluetooth_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_BLUETOOTH);
264 if (!bluetooth_rfkill)
266 bluetooth_rfkill->name = "dell-bluetooth";
267 bluetooth_rfkill->toggle_radio = dell_bluetooth_set;
268 bluetooth_rfkill->get_state = dell_bluetooth_get;
269 ret = rfkill_register(bluetooth_rfkill);
274 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
275 wwan_rfkill = rfkill_allocate(NULL, RFKILL_TYPE_WWAN);
278 wwan_rfkill->name = "dell-wwan";
279 wwan_rfkill->toggle_radio = dell_wwan_set;
280 wwan_rfkill->get_state = dell_wwan_get;
281 ret = rfkill_register(wwan_rfkill);
289 rfkill_free(wwan_rfkill);
290 if (bluetooth_rfkill) {
291 rfkill_unregister(bluetooth_rfkill);
292 bluetooth_rfkill = NULL;
295 if (bluetooth_rfkill)
296 rfkill_free(bluetooth_rfkill);
298 rfkill_unregister(wifi_rfkill);
303 rfkill_free(wifi_rfkill);
308 static int dell_send_intensity(struct backlight_device *bd)
310 struct calling_interface_buffer buffer;
312 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
313 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
314 buffer.input[1] = bd->props.brightness;
316 if (buffer.input[0] == -1)
319 if (power_supply_is_system_supplied() > 0)
320 dell_send_request(&buffer, 1, 2);
322 dell_send_request(&buffer, 1, 1);
327 static int dell_get_intensity(struct backlight_device *bd)
329 struct calling_interface_buffer buffer;
331 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
332 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
334 if (buffer.input[0] == -1)
337 if (power_supply_is_system_supplied() > 0)
338 dell_send_request(&buffer, 0, 2);
340 dell_send_request(&buffer, 0, 1);
342 return buffer.output[1];
345 static struct backlight_ops dell_ops = {
346 .get_brightness = dell_get_intensity,
347 .update_status = dell_send_intensity,
350 static int __init dell_init(void)
352 struct calling_interface_buffer buffer;
353 int max_intensity = 0;
356 if (!dmi_check_system(dell_device_table))
359 dmi_walk(find_tokens);
362 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
366 ret = dell_setup_rfkill();
369 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
374 /* In the event of an ACPI backlight being available, don't
375 * register the platform controller.
377 if (acpi_video_backlight_support())
381 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
382 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
384 if (buffer.input[0] != -1) {
385 dell_send_request(&buffer, 0, 2);
386 max_intensity = buffer.output[3];
390 dell_backlight_device = backlight_device_register(
395 if (IS_ERR(dell_backlight_device)) {
396 ret = PTR_ERR(dell_backlight_device);
397 dell_backlight_device = NULL;
401 dell_backlight_device->props.max_brightness = max_intensity;
402 dell_backlight_device->props.brightness =
403 dell_get_intensity(dell_backlight_device);
404 backlight_update_status(dell_backlight_device);
410 rfkill_unregister(wifi_rfkill);
411 if (bluetooth_rfkill)
412 rfkill_unregister(bluetooth_rfkill);
414 rfkill_unregister(wwan_rfkill);
419 static void __exit dell_exit(void)
421 backlight_device_unregister(dell_backlight_device);
423 rfkill_unregister(wifi_rfkill);
424 if (bluetooth_rfkill)
425 rfkill_unregister(bluetooth_rfkill);
427 rfkill_unregister(wwan_rfkill);
430 module_init(dell_init);
431 module_exit(dell_exit);
433 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
434 MODULE_DESCRIPTION("Dell laptop driver");
435 MODULE_LICENSE("GPL");
436 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");