blob: 81213e8154002ed810dfc7428aa63464abb84960 [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>
Matthew Garrett814cb8a2009-12-09 18:23:36 +000025#include <linux/i8042.h>
Len Browncad73122009-01-09 17:23:38 -050026#include "../../firmware/dcdbas.h"
Matthew Garrettad8f07c2009-01-07 18:08:56 -080027
28#define BRIGHTNESS_TOKEN 0x7d
29
30/* This structure will be modified by the firmware when we enter
31 * system management mode, hence the volatiles */
32
33struct calling_interface_buffer {
34 u16 class;
35 u16 select;
36 volatile u32 input[4];
37 volatile u32 output[4];
38} __packed;
39
40struct calling_interface_token {
41 u16 tokenID;
42 u16 location;
43 union {
44 u16 value;
45 u16 stringlength;
46 };
47};
48
49struct calling_interface_structure {
50 struct dmi_header header;
51 u16 cmdIOAddress;
52 u8 cmdIOCode;
53 u32 supportedCmds;
54 struct calling_interface_token tokens[];
55} __packed;
56
57static int da_command_address;
58static int da_command_code;
59static int da_num_tokens;
60static struct calling_interface_token *da_tokens;
61
Alan Jenkinsada32482009-08-19 15:06:49 +010062static struct platform_driver platform_driver = {
63 .driver = {
64 .name = "dell-laptop",
65 .owner = THIS_MODULE,
66 }
67};
68
69static struct platform_device *platform_device;
Matthew Garrettad8f07c2009-01-07 18:08:56 -080070static struct backlight_device *dell_backlight_device;
71static struct rfkill *wifi_rfkill;
72static struct rfkill *bluetooth_rfkill;
73static struct rfkill *wwan_rfkill;
74
75static const struct dmi_system_id __initdata dell_device_table[] = {
76 {
77 .ident = "Dell laptop",
78 .matches = {
79 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
80 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
81 },
82 },
83 { }
84};
85
Alan Jenkins4788df42009-08-19 15:06:50 +010086static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -080087{
88 /* Final token is a terminator, so we don't want to copy it */
89 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
90 struct calling_interface_structure *table =
91 container_of(dm, struct calling_interface_structure, header);
92
93 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
94 6 bytes of entry */
95
96 if (dm->length < 17)
97 return;
98
99 da_command_address = table->cmdIOAddress;
100 da_command_code = table->cmdIOCode;
101
102 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
103 sizeof(struct calling_interface_token),
104 GFP_KERNEL);
105
106 if (!da_tokens)
107 return;
108
109 memcpy(da_tokens+da_num_tokens, table->tokens,
110 sizeof(struct calling_interface_token) * tokens);
111
112 da_num_tokens += tokens;
113}
114
Alan Jenkins4788df42009-08-19 15:06:50 +0100115static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800116{
117 switch (dm->type) {
118 case 0xd4: /* Indexed IO */
119 break;
120 case 0xd5: /* Protected Area Type 1 */
121 break;
122 case 0xd6: /* Protected Area Type 2 */
123 break;
124 case 0xda: /* Calling interface */
125 parse_da_table(dm);
126 break;
127 }
128}
129
130static int find_token_location(int tokenid)
131{
132 int i;
133 for (i = 0; i < da_num_tokens; i++) {
134 if (da_tokens[i].tokenID == tokenid)
135 return da_tokens[i].location;
136 }
137
138 return -1;
139}
140
141static struct calling_interface_buffer *
142dell_send_request(struct calling_interface_buffer *buffer, int class,
143 int select)
144{
145 struct smi_cmd command;
146
147 command.magic = SMI_CMD_MAGIC;
148 command.command_address = da_command_address;
149 command.command_code = da_command_code;
150 command.ebx = virt_to_phys(buffer);
151 command.ecx = 0x42534931;
152
153 buffer->class = class;
154 buffer->select = select;
155
156 dcdbas_smi_request(&command);
157
158 return buffer;
159}
160
161/* Derived from information in DellWirelessCtl.cpp:
162 Class 17, select 11 is radio control. It returns an array of 32-bit values.
163
164 result[0]: return code
165 result[1]:
166 Bit 0: Hardware switch supported
167 Bit 1: Wifi locator supported
168 Bit 2: Wifi is supported
169 Bit 3: Bluetooth is supported
170 Bit 4: WWAN is supported
171 Bit 5: Wireless keyboard supported
172 Bits 6-7: Reserved
173 Bit 8: Wifi is installed
174 Bit 9: Bluetooth is installed
175 Bit 10: WWAN is installed
176 Bits 11-15: Reserved
177 Bit 16: Hardware switch is on
178 Bit 17: Wifi is blocked
179 Bit 18: Bluetooth is blocked
180 Bit 19: WWAN is blocked
181 Bits 20-31: Reserved
182 result[2]: NVRAM size in bytes
183 result[3]: NVRAM format version number
184*/
185
Johannes Berg19d337d2009-06-02 13:01:37 +0200186static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800187{
188 struct calling_interface_buffer buffer;
Johannes Berg624f0de2009-06-15 16:26:47 +0200189 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200190 unsigned long radio = (unsigned long)data;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800191
192 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
Mario Limoncielloec1722a2010-02-09 14:11:05 -0500193 dell_send_request(&buffer, 17, 11);
194 if (!(buffer.output[1] & BIT(16)))
195 return -EINVAL;
196
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800197 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
198 dell_send_request(&buffer, 17, 11);
199
200 return 0;
201}
202
Johannes Berg19d337d2009-06-02 13:01:37 +0200203static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800204{
205 struct calling_interface_buffer buffer;
206 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200207 int bit = (unsigned long)data + 16;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800208
209 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
210 dell_send_request(&buffer, 17, 11);
211 status = buffer.output[1];
212
Matthew Garrette1fbf342009-07-31 03:25:38 +0100213 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
214 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800215}
216
Johannes Berg19d337d2009-06-02 13:01:37 +0200217static const struct rfkill_ops dell_rfkill_ops = {
218 .set_block = dell_rfkill_set,
219 .query = dell_rfkill_query,
220};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800221
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000222static void dell_update_rfkill(struct work_struct *ignored)
223{
224 if (wifi_rfkill)
225 dell_rfkill_query(wifi_rfkill, (void *)1);
226 if (bluetooth_rfkill)
227 dell_rfkill_query(bluetooth_rfkill, (void *)2);
228 if (wwan_rfkill)
229 dell_rfkill_query(wwan_rfkill, (void *)3);
230}
231static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
232
233
Alan Jenkins4788df42009-08-19 15:06:50 +0100234static int __init dell_setup_rfkill(void)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800235{
236 struct calling_interface_buffer buffer;
237 int status;
238 int ret;
239
240 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
241 dell_send_request(&buffer, 17, 11);
242 status = buffer.output[1];
243
244 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100245 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
246 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200247 &dell_rfkill_ops, (void *) 1);
248 if (!wifi_rfkill) {
249 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800250 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200251 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800252 ret = rfkill_register(wifi_rfkill);
253 if (ret)
254 goto err_wifi;
255 }
256
257 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100258 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
259 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200260 RFKILL_TYPE_BLUETOOTH,
261 &dell_rfkill_ops, (void *) 2);
262 if (!bluetooth_rfkill) {
263 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800264 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200265 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800266 ret = rfkill_register(bluetooth_rfkill);
267 if (ret)
268 goto err_bluetooth;
269 }
270
271 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100272 wwan_rfkill = rfkill_alloc("dell-wwan",
273 &platform_device->dev,
274 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200275 &dell_rfkill_ops, (void *) 3);
276 if (!wwan_rfkill) {
277 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800278 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200279 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800280 ret = rfkill_register(wwan_rfkill);
281 if (ret)
282 goto err_wwan;
283 }
284
285 return 0;
286err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200287 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800288 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200289 rfkill_unregister(bluetooth_rfkill);
290err_bluetooth:
291 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800292 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200293 rfkill_unregister(wifi_rfkill);
294err_wifi:
295 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800296
297 return ret;
298}
299
Alan Jenkins4311bb22009-08-19 15:06:48 +0100300static void dell_cleanup_rfkill(void)
301{
302 if (wifi_rfkill) {
303 rfkill_unregister(wifi_rfkill);
304 rfkill_destroy(wifi_rfkill);
305 }
306 if (bluetooth_rfkill) {
307 rfkill_unregister(bluetooth_rfkill);
308 rfkill_destroy(bluetooth_rfkill);
309 }
310 if (wwan_rfkill) {
311 rfkill_unregister(wwan_rfkill);
312 rfkill_destroy(wwan_rfkill);
313 }
314}
315
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800316static int dell_send_intensity(struct backlight_device *bd)
317{
318 struct calling_interface_buffer buffer;
319
320 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
321 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
322 buffer.input[1] = bd->props.brightness;
323
324 if (buffer.input[0] == -1)
325 return -ENODEV;
326
327 if (power_supply_is_system_supplied() > 0)
328 dell_send_request(&buffer, 1, 2);
329 else
330 dell_send_request(&buffer, 1, 1);
331
332 return 0;
333}
334
335static int dell_get_intensity(struct backlight_device *bd)
336{
337 struct calling_interface_buffer buffer;
338
339 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
340 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
341
342 if (buffer.input[0] == -1)
343 return -ENODEV;
344
345 if (power_supply_is_system_supplied() > 0)
346 dell_send_request(&buffer, 0, 2);
347 else
348 dell_send_request(&buffer, 0, 1);
349
350 return buffer.output[1];
351}
352
353static struct backlight_ops dell_ops = {
354 .get_brightness = dell_get_intensity,
355 .update_status = dell_send_intensity,
356};
357
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000358bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
359 struct serio *port)
360{
361 static bool extended;
362
363 if (str & 0x20)
364 return false;
365
366 if (unlikely(data == 0xe0)) {
367 extended = true;
368 return false;
369 } else if (unlikely(extended)) {
370 switch (data) {
371 case 0x8:
372 schedule_delayed_work(&dell_rfkill_work,
373 round_jiffies_relative(HZ));
374 break;
375 }
376 extended = false;
377 }
378
379 return false;
380}
381
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800382static int __init dell_init(void)
383{
384 struct calling_interface_buffer buffer;
385 int max_intensity = 0;
386 int ret;
387
388 if (!dmi_check_system(dell_device_table))
389 return -ENODEV;
390
Jean Delvaree7a19c562009-03-30 21:46:44 +0200391 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800392
393 if (!da_tokens) {
394 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
395 return -ENODEV;
396 }
397
Alan Jenkinsada32482009-08-19 15:06:49 +0100398 ret = platform_driver_register(&platform_driver);
399 if (ret)
400 goto fail_platform_driver;
401 platform_device = platform_device_alloc("dell-laptop", -1);
402 if (!platform_device) {
403 ret = -ENOMEM;
404 goto fail_platform_device1;
405 }
406 ret = platform_device_add(platform_device);
407 if (ret)
408 goto fail_platform_device2;
409
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800410 ret = dell_setup_rfkill();
411
412 if (ret) {
413 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100414 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800415 }
416
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000417 ret = i8042_install_filter(dell_laptop_i8042_filter);
418 if (ret) {
419 printk(KERN_WARNING
420 "dell-laptop: Unable to install key filter\n");
421 goto fail_filter;
422 }
423
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800424#ifdef CONFIG_ACPI
425 /* In the event of an ACPI backlight being available, don't
426 * register the platform controller.
427 */
428 if (acpi_video_backlight_support())
429 return 0;
430#endif
431
432 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
433 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
434
435 if (buffer.input[0] != -1) {
436 dell_send_request(&buffer, 0, 2);
437 max_intensity = buffer.output[3];
438 }
439
440 if (max_intensity) {
441 dell_backlight_device = backlight_device_register(
442 "dell_backlight",
Alan Jenkinsada32482009-08-19 15:06:49 +0100443 &platform_device->dev, NULL,
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800444 &dell_ops);
445
446 if (IS_ERR(dell_backlight_device)) {
447 ret = PTR_ERR(dell_backlight_device);
448 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100449 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800450 }
451
452 dell_backlight_device->props.max_brightness = max_intensity;
453 dell_backlight_device->props.brightness =
454 dell_get_intensity(dell_backlight_device);
455 backlight_update_status(dell_backlight_device);
456 }
457
458 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100459
460fail_backlight:
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000461 i8042_remove_filter(dell_laptop_i8042_filter);
462fail_filter:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100463 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100464fail_rfkill:
Alan Jenkinsada32482009-08-19 15:06:49 +0100465 platform_device_del(platform_device);
466fail_platform_device2:
467 platform_device_put(platform_device);
468fail_platform_device1:
469 platform_driver_unregister(&platform_driver);
470fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800471 kfree(da_tokens);
472 return ret;
473}
474
475static void __exit dell_exit(void)
476{
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000477 cancel_delayed_work_sync(&dell_rfkill_work);
478 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800479 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100480 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500481 if (platform_device) {
482 platform_device_del(platform_device);
483 platform_driver_unregister(&platform_driver);
484 }
Matthew Garrette5512602010-02-09 14:05:01 -0500485 kfree(da_tokens);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800486}
487
488module_init(dell_init);
489module_exit(dell_exit);
490
491MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
492MODULE_DESCRIPTION("Dell laptop driver");
493MODULE_LICENSE("GPL");
494MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");