Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / platform / x86 / dell-laptop.c
1 /*
2  *  Driver for Dell laptop extras
3  *
4  *  Copyright (c) Red Hat <mjg@redhat.com>
5  *
6  *  Based on documentation in the libsmbios package, Copyright (C) 2005 Dell
7  *  Inc.
8  *
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.
12  */
13
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"
26
27 #define BRIGHTNESS_TOKEN 0x7d
28
29 /* This structure will be modified by the firmware when we enter
30  * system management mode, hence the volatiles */
31
32 struct calling_interface_buffer {
33         u16 class;
34         u16 select;
35         volatile u32 input[4];
36         volatile u32 output[4];
37 } __packed;
38
39 struct calling_interface_token {
40         u16 tokenID;
41         u16 location;
42         union {
43                 u16 value;
44                 u16 stringlength;
45         };
46 };
47
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;
55
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;
60
61 static struct platform_driver platform_driver = {
62         .driver = {
63                 .name = "dell-laptop",
64                 .owner = THIS_MODULE,
65         }
66 };
67
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;
73
74 static const struct dmi_system_id __initdata dell_device_table[] = {
75         {
76                 .ident = "Dell laptop",
77                 .matches = {
78                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
79                         DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
80                 },
81         },
82         { }
83 };
84
85 static void __init parse_da_table(const struct dmi_header *dm)
86 {
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);
91
92         /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
93            6 bytes of entry */
94
95         if (dm->length < 17)
96                 return;
97
98         da_command_address = table->cmdIOAddress;
99         da_command_code = table->cmdIOCode;
100
101         da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
102                              sizeof(struct calling_interface_token),
103                              GFP_KERNEL);
104
105         if (!da_tokens)
106                 return;
107
108         memcpy(da_tokens+da_num_tokens, table->tokens,
109                sizeof(struct calling_interface_token) * tokens);
110
111         da_num_tokens += tokens;
112 }
113
114 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
115 {
116         switch (dm->type) {
117         case 0xd4: /* Indexed IO */
118                 break;
119         case 0xd5: /* Protected Area Type 1 */
120                 break;
121         case 0xd6: /* Protected Area Type 2 */
122                 break;
123         case 0xda: /* Calling interface */
124                 parse_da_table(dm);
125                 break;
126         }
127 }
128
129 static int find_token_location(int tokenid)
130 {
131         int i;
132         for (i = 0; i < da_num_tokens; i++) {
133                 if (da_tokens[i].tokenID == tokenid)
134                         return da_tokens[i].location;
135         }
136
137         return -1;
138 }
139
140 static struct calling_interface_buffer *
141 dell_send_request(struct calling_interface_buffer *buffer, int class,
142                   int select)
143 {
144         struct smi_cmd command;
145
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;
151
152         buffer->class = class;
153         buffer->select = select;
154
155         dcdbas_smi_request(&command);
156
157         return buffer;
158 }
159
160 /* Derived from information in DellWirelessCtl.cpp:
161    Class 17, select 11 is radio control. It returns an array of 32-bit values.
162
163    result[0]: return code
164    result[1]:
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
171      Bits 6-7:   Reserved
172      Bit 8:      Wifi is installed
173      Bit 9:      Bluetooth is installed
174      Bit 10:     WWAN is installed
175      Bits 11-15: Reserved
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
180      Bits 20-31: Reserved
181    result[2]: NVRAM size in bytes
182    result[3]: NVRAM format version number
183 */
184
185 static int dell_rfkill_set(void *data, bool blocked)
186 {
187         struct calling_interface_buffer buffer;
188         int disable = blocked ? 1 : 0;
189         unsigned long radio = (unsigned long)data;
190
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);
194
195         return 0;
196 }
197
198 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
199 {
200         struct calling_interface_buffer buffer;
201         int status;
202         int bit = (unsigned long)data + 16;
203
204         memset(&buffer, 0, sizeof(struct calling_interface_buffer));
205         dell_send_request(&buffer, 17, 11);
206         status = buffer.output[1];
207
208         rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
209         rfkill_set_hw_state(rfkill, !(status & BIT(16)));
210 }
211
212 static const struct rfkill_ops dell_rfkill_ops = {
213         .set_block = dell_rfkill_set,
214         .query = dell_rfkill_query,
215 };
216
217 static int __init dell_setup_rfkill(void)
218 {
219         struct calling_interface_buffer buffer;
220         int status;
221         int ret;
222
223         memset(&buffer, 0, sizeof(struct calling_interface_buffer));
224         dell_send_request(&buffer, 17, 11);
225         status = buffer.output[1];
226
227         if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
228                 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
229                                            RFKILL_TYPE_WLAN,
230                                            &dell_rfkill_ops, (void *) 1);
231                 if (!wifi_rfkill) {
232                         ret = -ENOMEM;
233                         goto err_wifi;
234                 }
235                 ret = rfkill_register(wifi_rfkill);
236                 if (ret)
237                         goto err_wifi;
238         }
239
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) {
246                         ret = -ENOMEM;
247                         goto err_bluetooth;
248                 }
249                 ret = rfkill_register(bluetooth_rfkill);
250                 if (ret)
251                         goto err_bluetooth;
252         }
253
254         if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
255                 wwan_rfkill = rfkill_alloc("dell-wwan",
256                                            &platform_device->dev,
257                                            RFKILL_TYPE_WWAN,
258                                            &dell_rfkill_ops, (void *) 3);
259                 if (!wwan_rfkill) {
260                         ret = -ENOMEM;
261                         goto err_wwan;
262                 }
263                 ret = rfkill_register(wwan_rfkill);
264                 if (ret)
265                         goto err_wwan;
266         }
267
268         return 0;
269 err_wwan:
270         rfkill_destroy(wwan_rfkill);
271         if (bluetooth_rfkill)
272                 rfkill_unregister(bluetooth_rfkill);
273 err_bluetooth:
274         rfkill_destroy(bluetooth_rfkill);
275         if (wifi_rfkill)
276                 rfkill_unregister(wifi_rfkill);
277 err_wifi:
278         rfkill_destroy(wifi_rfkill);
279
280         return ret;
281 }
282
283 static void dell_cleanup_rfkill(void)
284 {
285         if (wifi_rfkill) {
286                 rfkill_unregister(wifi_rfkill);
287                 rfkill_destroy(wifi_rfkill);
288         }
289         if (bluetooth_rfkill) {
290                 rfkill_unregister(bluetooth_rfkill);
291                 rfkill_destroy(bluetooth_rfkill);
292         }
293         if (wwan_rfkill) {
294                 rfkill_unregister(wwan_rfkill);
295                 rfkill_destroy(wwan_rfkill);
296         }
297 }
298
299 static int dell_send_intensity(struct backlight_device *bd)
300 {
301         struct calling_interface_buffer buffer;
302
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;
306
307         if (buffer.input[0] == -1)
308                 return -ENODEV;
309
310         if (power_supply_is_system_supplied() > 0)
311                 dell_send_request(&buffer, 1, 2);
312         else
313                 dell_send_request(&buffer, 1, 1);
314
315         return 0;
316 }
317
318 static int dell_get_intensity(struct backlight_device *bd)
319 {
320         struct calling_interface_buffer buffer;
321
322         memset(&buffer, 0, sizeof(struct calling_interface_buffer));
323         buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
324
325         if (buffer.input[0] == -1)
326                 return -ENODEV;
327
328         if (power_supply_is_system_supplied() > 0)
329                 dell_send_request(&buffer, 0, 2);
330         else
331                 dell_send_request(&buffer, 0, 1);
332
333         return buffer.output[1];
334 }
335
336 static struct backlight_ops dell_ops = {
337         .get_brightness = dell_get_intensity,
338         .update_status  = dell_send_intensity,
339 };
340
341 static int __init dell_init(void)
342 {
343         struct calling_interface_buffer buffer;
344         int max_intensity = 0;
345         int ret;
346
347         if (!dmi_check_system(dell_device_table))
348                 return -ENODEV;
349
350         dmi_walk(find_tokens, NULL);
351
352         if (!da_tokens)  {
353                 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
354                 return -ENODEV;
355         }
356
357         ret = platform_driver_register(&platform_driver);
358         if (ret)
359                 goto fail_platform_driver;
360         platform_device = platform_device_alloc("dell-laptop", -1);
361         if (!platform_device) {
362                 ret = -ENOMEM;
363                 goto fail_platform_device1;
364         }
365         ret = platform_device_add(platform_device);
366         if (ret)
367                 goto fail_platform_device2;
368
369         ret = dell_setup_rfkill();
370
371         if (ret) {
372                 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
373                 goto fail_rfkill;
374         }
375
376 #ifdef CONFIG_ACPI
377         /* In the event of an ACPI backlight being available, don't
378          * register the platform controller.
379          */
380         if (acpi_video_backlight_support())
381                 return 0;
382 #endif
383
384         memset(&buffer, 0, sizeof(struct calling_interface_buffer));
385         buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
386
387         if (buffer.input[0] != -1) {
388                 dell_send_request(&buffer, 0, 2);
389                 max_intensity = buffer.output[3];
390         }
391
392         if (max_intensity) {
393                 dell_backlight_device = backlight_device_register(
394                         "dell_backlight",
395                         &platform_device->dev, NULL,
396                         &dell_ops);
397
398                 if (IS_ERR(dell_backlight_device)) {
399                         ret = PTR_ERR(dell_backlight_device);
400                         dell_backlight_device = NULL;
401                         goto fail_backlight;
402                 }
403
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);
408         }
409
410         return 0;
411
412 fail_backlight:
413         dell_cleanup_rfkill();
414 fail_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:
421         kfree(da_tokens);
422         return ret;
423 }
424
425 static void __exit dell_exit(void)
426 {
427         backlight_device_unregister(dell_backlight_device);
428         dell_cleanup_rfkill();
429 }
430
431 module_init(dell_init);
432 module_exit(dell_exit);
433
434 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
435 MODULE_DESCRIPTION("Dell laptop driver");
436 MODULE_LICENSE("GPL");
437 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");