blob: a4ce64244f6539e5c75c06b37daae2f4dc16ce4d [file] [log] [blame]
Matthew Garrettad8f07c2009-01-07 18:08:56 -08001/*
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>
Len Browncad73122009-01-09 17:23:38 -050025#include "../../firmware/dcdbas.h"
Matthew Garrettad8f07c2009-01-07 18:08:56 -080026
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
32struct calling_interface_buffer {
33 u16 class;
34 u16 select;
35 volatile u32 input[4];
36 volatile u32 output[4];
37} __packed;
38
39struct calling_interface_token {
40 u16 tokenID;
41 u16 location;
42 union {
43 u16 value;
44 u16 stringlength;
45 };
46};
47
48struct 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
56static int da_command_address;
57static int da_command_code;
58static int da_num_tokens;
59static struct calling_interface_token *da_tokens;
60
Alan Jenkinsada32482009-08-19 15:06:49 +010061static struct platform_driver platform_driver = {
62 .driver = {
63 .name = "dell-laptop",
64 .owner = THIS_MODULE,
65 }
66};
67
68static struct platform_device *platform_device;
Matthew Garrettad8f07c2009-01-07 18:08:56 -080069static struct backlight_device *dell_backlight_device;
70static struct rfkill *wifi_rfkill;
71static struct rfkill *bluetooth_rfkill;
72static struct rfkill *wwan_rfkill;
73
74static 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
85static void 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
Jean Delvaree7a19c562009-03-30 21:46:44 +0200114static void find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800115{
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
129static 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
140static struct calling_interface_buffer *
141dell_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
Johannes Berg19d337d2009-06-02 13:01:37 +0200185static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800186{
187 struct calling_interface_buffer buffer;
Johannes Berg624f0de2009-06-15 16:26:47 +0200188 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200189 unsigned long radio = (unsigned long)data;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800190
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
Johannes Berg19d337d2009-06-02 13:01:37 +0200198static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800199{
200 struct calling_interface_buffer buffer;
201 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200202 int bit = (unsigned long)data + 16;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800203
204 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
205 dell_send_request(&buffer, 17, 11);
206 status = buffer.output[1];
207
Matthew Garrette1fbf342009-07-31 03:25:38 +0100208 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
209 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800210}
211
Johannes Berg19d337d2009-06-02 13:01:37 +0200212static const struct rfkill_ops dell_rfkill_ops = {
213 .set_block = dell_rfkill_set,
214 .query = dell_rfkill_query,
215};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800216
217static int 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)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100228 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
229 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200230 &dell_rfkill_ops, (void *) 1);
231 if (!wifi_rfkill) {
232 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800233 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200234 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800235 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)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100241 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
242 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200243 RFKILL_TYPE_BLUETOOTH,
244 &dell_rfkill_ops, (void *) 2);
245 if (!bluetooth_rfkill) {
246 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800247 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200248 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800249 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)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100255 wwan_rfkill = rfkill_alloc("dell-wwan",
256 &platform_device->dev,
257 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200258 &dell_rfkill_ops, (void *) 3);
259 if (!wwan_rfkill) {
260 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800261 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200262 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800263 ret = rfkill_register(wwan_rfkill);
264 if (ret)
265 goto err_wwan;
266 }
267
268 return 0;
269err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200270 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800271 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200272 rfkill_unregister(bluetooth_rfkill);
273err_bluetooth:
274 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800275 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200276 rfkill_unregister(wifi_rfkill);
277err_wifi:
278 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800279
280 return ret;
281}
282
Alan Jenkins4311bb22009-08-19 15:06:48 +0100283static 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
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800299static 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
318static 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
336static struct backlight_ops dell_ops = {
337 .get_brightness = dell_get_intensity,
338 .update_status = dell_send_intensity,
339};
340
341static 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
Jean Delvaree7a19c562009-03-30 21:46:44 +0200350 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800351
352 if (!da_tokens) {
353 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
354 return -ENODEV;
355 }
356
Alan Jenkinsada32482009-08-19 15:06:49 +0100357 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
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800369 ret = dell_setup_rfkill();
370
371 if (ret) {
372 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100373 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800374 }
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",
Alan Jenkinsada32482009-08-19 15:06:49 +0100395 &platform_device->dev, NULL,
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800396 &dell_ops);
397
398 if (IS_ERR(dell_backlight_device)) {
399 ret = PTR_ERR(dell_backlight_device);
400 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100401 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800402 }
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;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100411
412fail_backlight:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100413 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100414fail_rfkill:
Alan Jenkinsada32482009-08-19 15:06:49 +0100415 platform_device_del(platform_device);
416fail_platform_device2:
417 platform_device_put(platform_device);
418fail_platform_device1:
419 platform_driver_unregister(&platform_driver);
420fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800421 kfree(da_tokens);
422 return ret;
423}
424
425static void __exit dell_exit(void)
426{
427 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100428 dell_cleanup_rfkill();
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800429}
430
431module_init(dell_init);
432module_exit(dell_exit);
433
434MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
435MODULE_DESCRIPTION("Dell laptop driver");
436MODULE_LICENSE("GPL");
437MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");