init from v2.6.32.60
[mach-moxart.git] / drivers / platform / x86 / dell-laptop.c
blob07a74dac03006f587987bc08301ed3cc6e82dffd
1 /*
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
7 * Inc.
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>
21 #include <linux/io.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 {
33 u16 class;
34 u16 select;
35 volatile u32 input[4];
36 volatile u32 output[4];
37 } __packed;
39 struct calling_interface_token {
40 u16 tokenID;
41 u16 location;
42 union {
43 u16 value;
44 u16 stringlength;
48 struct calling_interface_structure {
49 struct dmi_header header;
50 u16 cmdIOAddress;
51 u8 cmdIOCode;
52 u32 supportedCmds;
53 struct calling_interface_token tokens[];
54 } __packed;
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",
69 .matches = {
70 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
71 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
75 .matches = {
76 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
77 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
81 .ident = "Dell Computer Corporation",
82 .matches = {
83 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
84 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
87 { }
90 static void parse_da_table(const struct dmi_header *dm)
92 /* Final token is a terminator, so we don't want to copy it */
93 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
94 struct calling_interface_structure *table =
95 container_of(dm, struct calling_interface_structure, header);
97 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
98 6 bytes of entry */
100 if (dm->length < 17)
101 return;
103 da_command_address = table->cmdIOAddress;
104 da_command_code = table->cmdIOCode;
106 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
107 sizeof(struct calling_interface_token),
108 GFP_KERNEL);
110 if (!da_tokens)
111 return;
113 memcpy(da_tokens+da_num_tokens, table->tokens,
114 sizeof(struct calling_interface_token) * tokens);
116 da_num_tokens += tokens;
119 static void find_tokens(const struct dmi_header *dm, void *dummy)
121 switch (dm->type) {
122 case 0xd4: /* Indexed IO */
123 break;
124 case 0xd5: /* Protected Area Type 1 */
125 break;
126 case 0xd6: /* Protected Area Type 2 */
127 break;
128 case 0xda: /* Calling interface */
129 parse_da_table(dm);
130 break;
134 static int find_token_location(int tokenid)
136 int i;
137 for (i = 0; i < da_num_tokens; i++) {
138 if (da_tokens[i].tokenID == tokenid)
139 return da_tokens[i].location;
142 return -1;
145 static struct calling_interface_buffer *
146 dell_send_request(struct calling_interface_buffer *buffer, int class,
147 int select)
149 struct smi_cmd command;
151 command.magic = SMI_CMD_MAGIC;
152 command.command_address = da_command_address;
153 command.command_code = da_command_code;
154 command.ebx = virt_to_phys(buffer);
155 command.ecx = 0x42534931;
157 buffer->class = class;
158 buffer->select = select;
160 dcdbas_smi_request(&command);
162 return buffer;
165 /* Derived from information in DellWirelessCtl.cpp:
166 Class 17, select 11 is radio control. It returns an array of 32-bit values.
168 result[0]: return code
169 result[1]:
170 Bit 0: Hardware switch supported
171 Bit 1: Wifi locator supported
172 Bit 2: Wifi is supported
173 Bit 3: Bluetooth is supported
174 Bit 4: WWAN is supported
175 Bit 5: Wireless keyboard supported
176 Bits 6-7: Reserved
177 Bit 8: Wifi is installed
178 Bit 9: Bluetooth is installed
179 Bit 10: WWAN is installed
180 Bits 11-15: Reserved
181 Bit 16: Hardware switch is on
182 Bit 17: Wifi is blocked
183 Bit 18: Bluetooth is blocked
184 Bit 19: WWAN is blocked
185 Bits 20-31: Reserved
186 result[2]: NVRAM size in bytes
187 result[3]: NVRAM format version number
190 static int dell_rfkill_set(void *data, bool blocked)
192 struct calling_interface_buffer buffer;
193 int disable = blocked ? 1 : 0;
194 unsigned long radio = (unsigned long)data;
196 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
197 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
198 dell_send_request(&buffer, 17, 11);
200 return 0;
203 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
205 struct calling_interface_buffer buffer;
206 int status;
207 int bit = (unsigned long)data + 16;
209 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
210 dell_send_request(&buffer, 17, 11);
211 status = buffer.output[1];
213 if (status & BIT(bit))
214 rfkill_set_hw_state(rfkill, !!(status & BIT(16)));
217 static const struct rfkill_ops dell_rfkill_ops = {
218 .set_block = dell_rfkill_set,
219 .query = dell_rfkill_query,
222 static int dell_setup_rfkill(void)
224 struct calling_interface_buffer buffer;
225 int status;
226 int ret;
228 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
229 dell_send_request(&buffer, 17, 11);
230 status = buffer.output[1];
232 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
233 wifi_rfkill = rfkill_alloc("dell-wifi", NULL, RFKILL_TYPE_WLAN,
234 &dell_rfkill_ops, (void *) 1);
235 if (!wifi_rfkill) {
236 ret = -ENOMEM;
237 goto err_wifi;
239 ret = rfkill_register(wifi_rfkill);
240 if (ret)
241 goto err_wifi;
244 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
245 bluetooth_rfkill = rfkill_alloc("dell-bluetooth", NULL,
246 RFKILL_TYPE_BLUETOOTH,
247 &dell_rfkill_ops, (void *) 2);
248 if (!bluetooth_rfkill) {
249 ret = -ENOMEM;
250 goto err_bluetooth;
252 ret = rfkill_register(bluetooth_rfkill);
253 if (ret)
254 goto err_bluetooth;
257 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
258 wwan_rfkill = rfkill_alloc("dell-wwan", NULL, RFKILL_TYPE_WWAN,
259 &dell_rfkill_ops, (void *) 3);
260 if (!wwan_rfkill) {
261 ret = -ENOMEM;
262 goto err_wwan;
264 ret = rfkill_register(wwan_rfkill);
265 if (ret)
266 goto err_wwan;
269 return 0;
270 err_wwan:
271 rfkill_destroy(wwan_rfkill);
272 if (bluetooth_rfkill)
273 rfkill_unregister(bluetooth_rfkill);
274 err_bluetooth:
275 rfkill_destroy(bluetooth_rfkill);
276 if (wifi_rfkill)
277 rfkill_unregister(wifi_rfkill);
278 err_wifi:
279 rfkill_destroy(wifi_rfkill);
281 return ret;
284 static int dell_send_intensity(struct backlight_device *bd)
286 struct calling_interface_buffer buffer;
288 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
289 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
290 buffer.input[1] = bd->props.brightness;
292 if (buffer.input[0] == -1)
293 return -ENODEV;
295 if (power_supply_is_system_supplied() > 0)
296 dell_send_request(&buffer, 1, 2);
297 else
298 dell_send_request(&buffer, 1, 1);
300 return 0;
303 static int dell_get_intensity(struct backlight_device *bd)
305 struct calling_interface_buffer buffer;
307 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
308 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
310 if (buffer.input[0] == -1)
311 return -ENODEV;
313 if (power_supply_is_system_supplied() > 0)
314 dell_send_request(&buffer, 0, 2);
315 else
316 dell_send_request(&buffer, 0, 1);
318 return buffer.output[1];
321 static struct backlight_ops dell_ops = {
322 .get_brightness = dell_get_intensity,
323 .update_status = dell_send_intensity,
326 static int __init dell_init(void)
328 struct calling_interface_buffer buffer;
329 int max_intensity = 0;
330 int ret;
332 if (!dmi_check_system(dell_device_table))
333 return -ENODEV;
335 dmi_walk(find_tokens, NULL);
337 if (!da_tokens) {
338 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
339 return -ENODEV;
342 ret = dell_setup_rfkill();
344 if (ret) {
345 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
346 goto out;
349 #ifdef CONFIG_ACPI
350 /* In the event of an ACPI backlight being available, don't
351 * register the platform controller.
353 if (acpi_video_backlight_support())
354 return 0;
355 #endif
357 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
358 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
360 if (buffer.input[0] != -1) {
361 dell_send_request(&buffer, 0, 2);
362 max_intensity = buffer.output[3];
365 if (max_intensity) {
366 dell_backlight_device = backlight_device_register(
367 "dell_backlight",
368 NULL, NULL,
369 &dell_ops);
371 if (IS_ERR(dell_backlight_device)) {
372 ret = PTR_ERR(dell_backlight_device);
373 dell_backlight_device = NULL;
374 goto out;
377 dell_backlight_device->props.max_brightness = max_intensity;
378 dell_backlight_device->props.brightness =
379 dell_get_intensity(dell_backlight_device);
380 backlight_update_status(dell_backlight_device);
383 return 0;
384 out:
385 if (wifi_rfkill)
386 rfkill_unregister(wifi_rfkill);
387 if (bluetooth_rfkill)
388 rfkill_unregister(bluetooth_rfkill);
389 if (wwan_rfkill)
390 rfkill_unregister(wwan_rfkill);
391 kfree(da_tokens);
392 return ret;
395 static void __exit dell_exit(void)
397 backlight_device_unregister(dell_backlight_device);
398 if (wifi_rfkill)
399 rfkill_unregister(wifi_rfkill);
400 if (bluetooth_rfkill)
401 rfkill_unregister(bluetooth_rfkill);
402 if (wwan_rfkill)
403 rfkill_unregister(wwan_rfkill);
406 module_init(dell_init);
407 module_exit(dell_exit);
409 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
410 MODULE_DESCRIPTION("Dell laptop driver");
411 MODULE_LICENSE("GPL");
412 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
413 MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
414 MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");