blob: d8b1b39aa9db4d60e6faa0399971b04ac934fce9 [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
Mario Limoncielloe5fefd02010-02-09 17:41:03 -050086static struct dmi_system_id __devinitdata dell_blacklist[] = {
87 /* Supported by compal-laptop */
88 {
89 .ident = "Dell Mini 9",
90 .matches = {
91 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
92 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
93 },
94 },
95 {
96 .ident = "Dell Mini 10",
97 .matches = {
98 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
99 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
100 },
101 },
102 {
103 .ident = "Dell Mini 10v",
104 .matches = {
105 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
106 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
107 },
108 },
109 {
110 .ident = "Dell Inspiron 11z",
111 .matches = {
112 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
113 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
114 },
115 },
116 {
117 .ident = "Dell Mini 12",
118 .matches = {
119 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
120 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
121 },
122 },
123 {}
124};
125
Alan Jenkins4788df42009-08-19 15:06:50 +0100126static void __init parse_da_table(const struct dmi_header *dm)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800127{
128 /* Final token is a terminator, so we don't want to copy it */
129 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
130 struct calling_interface_structure *table =
131 container_of(dm, struct calling_interface_structure, header);
132
133 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
134 6 bytes of entry */
135
136 if (dm->length < 17)
137 return;
138
139 da_command_address = table->cmdIOAddress;
140 da_command_code = table->cmdIOCode;
141
142 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
143 sizeof(struct calling_interface_token),
144 GFP_KERNEL);
145
146 if (!da_tokens)
147 return;
148
149 memcpy(da_tokens+da_num_tokens, table->tokens,
150 sizeof(struct calling_interface_token) * tokens);
151
152 da_num_tokens += tokens;
153}
154
Alan Jenkins4788df42009-08-19 15:06:50 +0100155static void __init find_tokens(const struct dmi_header *dm, void *dummy)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800156{
157 switch (dm->type) {
158 case 0xd4: /* Indexed IO */
159 break;
160 case 0xd5: /* Protected Area Type 1 */
161 break;
162 case 0xd6: /* Protected Area Type 2 */
163 break;
164 case 0xda: /* Calling interface */
165 parse_da_table(dm);
166 break;
167 }
168}
169
170static int find_token_location(int tokenid)
171{
172 int i;
173 for (i = 0; i < da_num_tokens; i++) {
174 if (da_tokens[i].tokenID == tokenid)
175 return da_tokens[i].location;
176 }
177
178 return -1;
179}
180
181static struct calling_interface_buffer *
182dell_send_request(struct calling_interface_buffer *buffer, int class,
183 int select)
184{
185 struct smi_cmd command;
186
187 command.magic = SMI_CMD_MAGIC;
188 command.command_address = da_command_address;
189 command.command_code = da_command_code;
190 command.ebx = virt_to_phys(buffer);
191 command.ecx = 0x42534931;
192
193 buffer->class = class;
194 buffer->select = select;
195
196 dcdbas_smi_request(&command);
197
198 return buffer;
199}
200
201/* Derived from information in DellWirelessCtl.cpp:
202 Class 17, select 11 is radio control. It returns an array of 32-bit values.
203
204 result[0]: return code
205 result[1]:
206 Bit 0: Hardware switch supported
207 Bit 1: Wifi locator supported
208 Bit 2: Wifi is supported
209 Bit 3: Bluetooth is supported
210 Bit 4: WWAN is supported
211 Bit 5: Wireless keyboard supported
212 Bits 6-7: Reserved
213 Bit 8: Wifi is installed
214 Bit 9: Bluetooth is installed
215 Bit 10: WWAN is installed
216 Bits 11-15: Reserved
217 Bit 16: Hardware switch is on
218 Bit 17: Wifi is blocked
219 Bit 18: Bluetooth is blocked
220 Bit 19: WWAN is blocked
221 Bits 20-31: Reserved
222 result[2]: NVRAM size in bytes
223 result[3]: NVRAM format version number
224*/
225
Johannes Berg19d337d2009-06-02 13:01:37 +0200226static int dell_rfkill_set(void *data, bool blocked)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800227{
228 struct calling_interface_buffer buffer;
Johannes Berg624f0de2009-06-15 16:26:47 +0200229 int disable = blocked ? 1 : 0;
Johannes Berg19d337d2009-06-02 13:01:37 +0200230 unsigned long radio = (unsigned long)data;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800231
232 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
Mario Limoncielloec1722a2010-02-09 14:11:05 -0500233 dell_send_request(&buffer, 17, 11);
234 if (!(buffer.output[1] & BIT(16)))
235 return -EINVAL;
236
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800237 buffer.input[0] = (1 | (radio<<8) | (disable << 16));
238 dell_send_request(&buffer, 17, 11);
239
240 return 0;
241}
242
Johannes Berg19d337d2009-06-02 13:01:37 +0200243static void dell_rfkill_query(struct rfkill *rfkill, void *data)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800244{
245 struct calling_interface_buffer buffer;
246 int status;
Johannes Berg19d337d2009-06-02 13:01:37 +0200247 int bit = (unsigned long)data + 16;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800248
249 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
250 dell_send_request(&buffer, 17, 11);
251 status = buffer.output[1];
252
Matthew Garrette1fbf342009-07-31 03:25:38 +0100253 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
254 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800255}
256
Johannes Berg19d337d2009-06-02 13:01:37 +0200257static const struct rfkill_ops dell_rfkill_ops = {
258 .set_block = dell_rfkill_set,
259 .query = dell_rfkill_query,
260};
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800261
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000262static void dell_update_rfkill(struct work_struct *ignored)
263{
264 if (wifi_rfkill)
265 dell_rfkill_query(wifi_rfkill, (void *)1);
266 if (bluetooth_rfkill)
267 dell_rfkill_query(bluetooth_rfkill, (void *)2);
268 if (wwan_rfkill)
269 dell_rfkill_query(wwan_rfkill, (void *)3);
270}
271static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
272
273
Alan Jenkins4788df42009-08-19 15:06:50 +0100274static int __init dell_setup_rfkill(void)
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800275{
276 struct calling_interface_buffer buffer;
277 int status;
278 int ret;
279
Mario Limoncielloe5fefd02010-02-09 17:41:03 -0500280 if (dmi_check_system(dell_blacklist)) {
281 printk(KERN_INFO "dell-laptop: Blacklisted hardware detected - "
282 "not enabling rfkill\n");
283 return 0;
284 }
285
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800286 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
287 dell_send_request(&buffer, 17, 11);
288 status = buffer.output[1];
289
290 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100291 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
292 RFKILL_TYPE_WLAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200293 &dell_rfkill_ops, (void *) 1);
294 if (!wifi_rfkill) {
295 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800296 goto err_wifi;
Johannes Berg19d337d2009-06-02 13:01:37 +0200297 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800298 ret = rfkill_register(wifi_rfkill);
299 if (ret)
300 goto err_wifi;
301 }
302
303 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100304 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
305 &platform_device->dev,
Johannes Berg19d337d2009-06-02 13:01:37 +0200306 RFKILL_TYPE_BLUETOOTH,
307 &dell_rfkill_ops, (void *) 2);
308 if (!bluetooth_rfkill) {
309 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800310 goto err_bluetooth;
Johannes Berg19d337d2009-06-02 13:01:37 +0200311 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800312 ret = rfkill_register(bluetooth_rfkill);
313 if (ret)
314 goto err_bluetooth;
315 }
316
317 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
Alan Jenkinsada32482009-08-19 15:06:49 +0100318 wwan_rfkill = rfkill_alloc("dell-wwan",
319 &platform_device->dev,
320 RFKILL_TYPE_WWAN,
Johannes Berg19d337d2009-06-02 13:01:37 +0200321 &dell_rfkill_ops, (void *) 3);
322 if (!wwan_rfkill) {
323 ret = -ENOMEM;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800324 goto err_wwan;
Johannes Berg19d337d2009-06-02 13:01:37 +0200325 }
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800326 ret = rfkill_register(wwan_rfkill);
327 if (ret)
328 goto err_wwan;
329 }
330
331 return 0;
332err_wwan:
Johannes Berg19d337d2009-06-02 13:01:37 +0200333 rfkill_destroy(wwan_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800334 if (bluetooth_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200335 rfkill_unregister(bluetooth_rfkill);
336err_bluetooth:
337 rfkill_destroy(bluetooth_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800338 if (wifi_rfkill)
Johannes Berg19d337d2009-06-02 13:01:37 +0200339 rfkill_unregister(wifi_rfkill);
340err_wifi:
341 rfkill_destroy(wifi_rfkill);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800342
343 return ret;
344}
345
Alan Jenkins4311bb22009-08-19 15:06:48 +0100346static void dell_cleanup_rfkill(void)
347{
348 if (wifi_rfkill) {
349 rfkill_unregister(wifi_rfkill);
350 rfkill_destroy(wifi_rfkill);
351 }
352 if (bluetooth_rfkill) {
353 rfkill_unregister(bluetooth_rfkill);
354 rfkill_destroy(bluetooth_rfkill);
355 }
356 if (wwan_rfkill) {
357 rfkill_unregister(wwan_rfkill);
358 rfkill_destroy(wwan_rfkill);
359 }
360}
361
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800362static int dell_send_intensity(struct backlight_device *bd)
363{
364 struct calling_interface_buffer buffer;
365
366 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
367 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
368 buffer.input[1] = bd->props.brightness;
369
370 if (buffer.input[0] == -1)
371 return -ENODEV;
372
373 if (power_supply_is_system_supplied() > 0)
374 dell_send_request(&buffer, 1, 2);
375 else
376 dell_send_request(&buffer, 1, 1);
377
378 return 0;
379}
380
381static int dell_get_intensity(struct backlight_device *bd)
382{
383 struct calling_interface_buffer buffer;
384
385 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
386 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
387
388 if (buffer.input[0] == -1)
389 return -ENODEV;
390
391 if (power_supply_is_system_supplied() > 0)
392 dell_send_request(&buffer, 0, 2);
393 else
394 dell_send_request(&buffer, 0, 1);
395
396 return buffer.output[1];
397}
398
399static struct backlight_ops dell_ops = {
400 .get_brightness = dell_get_intensity,
401 .update_status = dell_send_intensity,
402};
403
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000404bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
405 struct serio *port)
406{
407 static bool extended;
408
409 if (str & 0x20)
410 return false;
411
412 if (unlikely(data == 0xe0)) {
413 extended = true;
414 return false;
415 } else if (unlikely(extended)) {
416 switch (data) {
417 case 0x8:
418 schedule_delayed_work(&dell_rfkill_work,
419 round_jiffies_relative(HZ));
420 break;
421 }
422 extended = false;
423 }
424
425 return false;
426}
427
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800428static int __init dell_init(void)
429{
430 struct calling_interface_buffer buffer;
431 int max_intensity = 0;
432 int ret;
433
434 if (!dmi_check_system(dell_device_table))
435 return -ENODEV;
436
Jean Delvaree7a19c562009-03-30 21:46:44 +0200437 dmi_walk(find_tokens, NULL);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800438
439 if (!da_tokens) {
440 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
441 return -ENODEV;
442 }
443
Alan Jenkinsada32482009-08-19 15:06:49 +0100444 ret = platform_driver_register(&platform_driver);
445 if (ret)
446 goto fail_platform_driver;
447 platform_device = platform_device_alloc("dell-laptop", -1);
448 if (!platform_device) {
449 ret = -ENOMEM;
450 goto fail_platform_device1;
451 }
452 ret = platform_device_add(platform_device);
453 if (ret)
454 goto fail_platform_device2;
455
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800456 ret = dell_setup_rfkill();
457
458 if (ret) {
459 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100460 goto fail_rfkill;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800461 }
462
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000463 ret = i8042_install_filter(dell_laptop_i8042_filter);
464 if (ret) {
465 printk(KERN_WARNING
466 "dell-laptop: Unable to install key filter\n");
467 goto fail_filter;
468 }
469
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800470#ifdef CONFIG_ACPI
471 /* In the event of an ACPI backlight being available, don't
472 * register the platform controller.
473 */
474 if (acpi_video_backlight_support())
475 return 0;
476#endif
477
478 memset(&buffer, 0, sizeof(struct calling_interface_buffer));
479 buffer.input[0] = find_token_location(BRIGHTNESS_TOKEN);
480
481 if (buffer.input[0] != -1) {
482 dell_send_request(&buffer, 0, 2);
483 max_intensity = buffer.output[3];
484 }
485
486 if (max_intensity) {
487 dell_backlight_device = backlight_device_register(
488 "dell_backlight",
Alan Jenkinsada32482009-08-19 15:06:49 +0100489 &platform_device->dev, NULL,
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800490 &dell_ops);
491
492 if (IS_ERR(dell_backlight_device)) {
493 ret = PTR_ERR(dell_backlight_device);
494 dell_backlight_device = NULL;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100495 goto fail_backlight;
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800496 }
497
498 dell_backlight_device->props.max_brightness = max_intensity;
499 dell_backlight_device->props.brightness =
500 dell_get_intensity(dell_backlight_device);
501 backlight_update_status(dell_backlight_device);
502 }
503
504 return 0;
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100505
506fail_backlight:
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000507 i8042_remove_filter(dell_laptop_i8042_filter);
508fail_filter:
Alan Jenkins4311bb22009-08-19 15:06:48 +0100509 dell_cleanup_rfkill();
Alan Jenkins71e9dc72009-08-19 15:06:47 +0100510fail_rfkill:
Alan Jenkinsada32482009-08-19 15:06:49 +0100511 platform_device_del(platform_device);
512fail_platform_device2:
513 platform_device_put(platform_device);
514fail_platform_device1:
515 platform_driver_unregister(&platform_driver);
516fail_platform_driver:
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800517 kfree(da_tokens);
518 return ret;
519}
520
521static void __exit dell_exit(void)
522{
Matthew Garrett814cb8a2009-12-09 18:23:36 +0000523 cancel_delayed_work_sync(&dell_rfkill_work);
524 i8042_remove_filter(dell_laptop_i8042_filter);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800525 backlight_device_unregister(dell_backlight_device);
Alan Jenkins4311bb22009-08-19 15:06:48 +0100526 dell_cleanup_rfkill();
Matthew Garrettfacd61d2010-02-09 14:03:04 -0500527 if (platform_device) {
528 platform_device_del(platform_device);
529 platform_driver_unregister(&platform_driver);
530 }
Matthew Garrette5512602010-02-09 14:05:01 -0500531 kfree(da_tokens);
Matthew Garrettad8f07c2009-01-07 18:08:56 -0800532}
533
534module_init(dell_init);
535module_exit(dell_exit);
536
537MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
538MODULE_DESCRIPTION("Dell laptop driver");
539MODULE_LICENSE("GPL");
540MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");