]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/platform/x86/dell-laptop.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[mirror_ubuntu-kernels.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 <linux/mm.h>
26 #include <linux/i8042.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
30 #include "../../firmware/dcdbas.h"
31
32 #define BRIGHTNESS_TOKEN 0x7d
33
34 /* This structure will be modified by the firmware when we enter
35 * system management mode, hence the volatiles */
36
37 struct calling_interface_buffer {
38 u16 class;
39 u16 select;
40 volatile u32 input[4];
41 volatile u32 output[4];
42 } __packed;
43
44 struct calling_interface_token {
45 u16 tokenID;
46 u16 location;
47 union {
48 u16 value;
49 u16 stringlength;
50 };
51 };
52
53 struct calling_interface_structure {
54 struct dmi_header header;
55 u16 cmdIOAddress;
56 u8 cmdIOCode;
57 u32 supportedCmds;
58 struct calling_interface_token tokens[];
59 } __packed;
60
61 static int da_command_address;
62 static int da_command_code;
63 static int da_num_tokens;
64 static struct calling_interface_token *da_tokens;
65
66 static struct platform_driver platform_driver = {
67 .driver = {
68 .name = "dell-laptop",
69 .owner = THIS_MODULE,
70 }
71 };
72
73 static struct platform_device *platform_device;
74 static struct backlight_device *dell_backlight_device;
75 static struct rfkill *wifi_rfkill;
76 static struct rfkill *bluetooth_rfkill;
77 static struct rfkill *wwan_rfkill;
78
79 static const struct dmi_system_id __initdata dell_device_table[] = {
80 {
81 .ident = "Dell laptop",
82 .matches = {
83 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
84 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
85 },
86 },
87 {
88 .matches = {
89 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
90 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
91 },
92 },
93 {
94 .ident = "Dell Computer Corporation",
95 .matches = {
96 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
97 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
98 },
99 },
100 { }
101 };
102
103 static struct dmi_system_id __devinitdata dell_blacklist[] = {
104 /* Supported by compal-laptop */
105 {
106 .ident = "Dell Mini 9",
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
109 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
110 },
111 },
112 {
113 .ident = "Dell Mini 10",
114 .matches = {
115 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
116 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
117 },
118 },
119 {
120 .ident = "Dell Mini 10v",
121 .matches = {
122 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
123 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
124 },
125 },
126 {
127 .ident = "Dell Mini 1012",
128 .matches = {
129 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
130 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
131 },
132 },
133 {
134 .ident = "Dell Inspiron 11z",
135 .matches = {
136 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
137 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
138 },
139 },
140 {
141 .ident = "Dell Mini 12",
142 .matches = {
143 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
144 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
145 },
146 },
147 {}
148 };
149
150 static struct calling_interface_buffer *buffer;
151 static struct page *bufferpage;
152 static DEFINE_MUTEX(buffer_mutex);
153
154 static int hwswitch_state;
155
156 static void get_buffer(void)
157 {
158 mutex_lock(&buffer_mutex);
159 memset(buffer, 0, sizeof(struct calling_interface_buffer));
160 }
161
162 static void release_buffer(void)
163 {
164 mutex_unlock(&buffer_mutex);
165 }
166
167 static void __init parse_da_table(const struct dmi_header *dm)
168 {
169 /* Final token is a terminator, so we don't want to copy it */
170 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
171 struct calling_interface_structure *table =
172 container_of(dm, struct calling_interface_structure, header);
173
174 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
175 6 bytes of entry */
176
177 if (dm->length < 17)
178 return;
179
180 da_command_address = table->cmdIOAddress;
181 da_command_code = table->cmdIOCode;
182
183 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
184 sizeof(struct calling_interface_token),
185 GFP_KERNEL);
186
187 if (!da_tokens)
188 return;
189
190 memcpy(da_tokens+da_num_tokens, table->tokens,
191 sizeof(struct calling_interface_token) * tokens);
192
193 da_num_tokens += tokens;
194 }
195
196 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
197 {
198 switch (dm->type) {
199 case 0xd4: /* Indexed IO */
200 break;
201 case 0xd5: /* Protected Area Type 1 */
202 break;
203 case 0xd6: /* Protected Area Type 2 */
204 break;
205 case 0xda: /* Calling interface */
206 parse_da_table(dm);
207 break;
208 }
209 }
210
211 static int find_token_location(int tokenid)
212 {
213 int i;
214 for (i = 0; i < da_num_tokens; i++) {
215 if (da_tokens[i].tokenID == tokenid)
216 return da_tokens[i].location;
217 }
218
219 return -1;
220 }
221
222 static struct calling_interface_buffer *
223 dell_send_request(struct calling_interface_buffer *buffer, int class,
224 int select)
225 {
226 struct smi_cmd command;
227
228 command.magic = SMI_CMD_MAGIC;
229 command.command_address = da_command_address;
230 command.command_code = da_command_code;
231 command.ebx = virt_to_phys(buffer);
232 command.ecx = 0x42534931;
233
234 buffer->class = class;
235 buffer->select = select;
236
237 dcdbas_smi_request(&command);
238
239 return buffer;
240 }
241
242 /* Derived from information in DellWirelessCtl.cpp:
243 Class 17, select 11 is radio control. It returns an array of 32-bit values.
244
245 Input byte 0 = 0: Wireless information
246
247 result[0]: return code
248 result[1]:
249 Bit 0: Hardware switch supported
250 Bit 1: Wifi locator supported
251 Bit 2: Wifi is supported
252 Bit 3: Bluetooth is supported
253 Bit 4: WWAN is supported
254 Bit 5: Wireless keyboard supported
255 Bits 6-7: Reserved
256 Bit 8: Wifi is installed
257 Bit 9: Bluetooth is installed
258 Bit 10: WWAN is installed
259 Bits 11-15: Reserved
260 Bit 16: Hardware switch is on
261 Bit 17: Wifi is blocked
262 Bit 18: Bluetooth is blocked
263 Bit 19: WWAN is blocked
264 Bits 20-31: Reserved
265 result[2]: NVRAM size in bytes
266 result[3]: NVRAM format version number
267
268 Input byte 0 = 2: Wireless switch configuration
269 result[0]: return code
270 result[1]:
271 Bit 0: Wifi controlled by switch
272 Bit 1: Bluetooth controlled by switch
273 Bit 2: WWAN controlled by switch
274 Bits 3-6: Reserved
275 Bit 7: Wireless switch config locked
276 Bit 8: Wifi locator enabled
277 Bits 9-14: Reserved
278 Bit 15: Wifi locator setting locked
279 Bits 16-31: Reserved
280 */
281
282 static int dell_rfkill_set(void *data, bool blocked)
283 {
284 int disable = blocked ? 1 : 0;
285 unsigned long radio = (unsigned long)data;
286 int hwswitch_bit = (unsigned long)data - 1;
287 int ret = 0;
288
289 get_buffer();
290 dell_send_request(buffer, 17, 11);
291
292 /* If the hardware switch controls this radio, and the hardware
293 switch is disabled, don't allow changing the software state */
294 if ((hwswitch_state & BIT(hwswitch_bit)) &&
295 !(buffer->output[1] & BIT(16))) {
296 ret = -EINVAL;
297 goto out;
298 }
299
300 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
301 dell_send_request(buffer, 17, 11);
302
303 out:
304 release_buffer();
305 return ret;
306 }
307
308 static void dell_rfkill_query(struct rfkill *rfkill, void *data)
309 {
310 int status;
311 int bit = (unsigned long)data + 16;
312 int hwswitch_bit = (unsigned long)data - 1;
313
314 get_buffer();
315 dell_send_request(buffer, 17, 11);
316 status = buffer->output[1];
317 release_buffer();
318
319 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
320
321 if (hwswitch_state & (BIT(hwswitch_bit)))
322 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
323 }
324
325 static const struct rfkill_ops dell_rfkill_ops = {
326 .set_block = dell_rfkill_set,
327 .query = dell_rfkill_query,
328 };
329
330 static struct dentry *dell_laptop_dir;
331
332 static int dell_debugfs_show(struct seq_file *s, void *data)
333 {
334 int status;
335
336 get_buffer();
337 dell_send_request(buffer, 17, 11);
338 status = buffer->output[1];
339 release_buffer();
340
341 seq_printf(s, "status:\t0x%X\n", status);
342 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
343 status & BIT(0));
344 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
345 (status & BIT(1)) >> 1);
346 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
347 (status & BIT(2)) >> 2);
348 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
349 (status & BIT(3)) >> 3);
350 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
351 (status & BIT(4)) >> 4);
352 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
353 (status & BIT(5)) >> 5);
354 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
355 (status & BIT(8)) >> 8);
356 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
357 (status & BIT(9)) >> 9);
358 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
359 (status & BIT(10)) >> 10);
360 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
361 (status & BIT(16)) >> 16);
362 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
363 (status & BIT(17)) >> 17);
364 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
365 (status & BIT(18)) >> 18);
366 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
367 (status & BIT(19)) >> 19);
368
369 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
370 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
371 hwswitch_state & BIT(0));
372 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
373 (hwswitch_state & BIT(1)) >> 1);
374 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
375 (hwswitch_state & BIT(2)) >> 2);
376 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
377 (hwswitch_state & BIT(7)) >> 7);
378 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
379 (hwswitch_state & BIT(8)) >> 8);
380 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
381 (hwswitch_state & BIT(15)) >> 15);
382
383 return 0;
384 }
385
386 static int dell_debugfs_open(struct inode *inode, struct file *file)
387 {
388 return single_open(file, dell_debugfs_show, inode->i_private);
389 }
390
391 static const struct file_operations dell_debugfs_fops = {
392 .owner = THIS_MODULE,
393 .open = dell_debugfs_open,
394 .read = seq_read,
395 .llseek = seq_lseek,
396 .release = single_release,
397 };
398
399 static void dell_update_rfkill(struct work_struct *ignored)
400 {
401 if (wifi_rfkill)
402 dell_rfkill_query(wifi_rfkill, (void *)1);
403 if (bluetooth_rfkill)
404 dell_rfkill_query(bluetooth_rfkill, (void *)2);
405 if (wwan_rfkill)
406 dell_rfkill_query(wwan_rfkill, (void *)3);
407 }
408 static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
409
410
411 static int __init dell_setup_rfkill(void)
412 {
413 int status;
414 int ret;
415
416 if (dmi_check_system(dell_blacklist)) {
417 printk(KERN_INFO "dell-laptop: Blacklisted hardware detected - "
418 "not enabling rfkill\n");
419 return 0;
420 }
421
422 get_buffer();
423 dell_send_request(buffer, 17, 11);
424 status = buffer->output[1];
425 buffer->input[0] = 0x2;
426 dell_send_request(buffer, 17, 11);
427 hwswitch_state = buffer->output[1];
428 release_buffer();
429
430 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
431 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
432 RFKILL_TYPE_WLAN,
433 &dell_rfkill_ops, (void *) 1);
434 if (!wifi_rfkill) {
435 ret = -ENOMEM;
436 goto err_wifi;
437 }
438 ret = rfkill_register(wifi_rfkill);
439 if (ret)
440 goto err_wifi;
441 }
442
443 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
444 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
445 &platform_device->dev,
446 RFKILL_TYPE_BLUETOOTH,
447 &dell_rfkill_ops, (void *) 2);
448 if (!bluetooth_rfkill) {
449 ret = -ENOMEM;
450 goto err_bluetooth;
451 }
452 ret = rfkill_register(bluetooth_rfkill);
453 if (ret)
454 goto err_bluetooth;
455 }
456
457 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
458 wwan_rfkill = rfkill_alloc("dell-wwan",
459 &platform_device->dev,
460 RFKILL_TYPE_WWAN,
461 &dell_rfkill_ops, (void *) 3);
462 if (!wwan_rfkill) {
463 ret = -ENOMEM;
464 goto err_wwan;
465 }
466 ret = rfkill_register(wwan_rfkill);
467 if (ret)
468 goto err_wwan;
469 }
470
471 return 0;
472 err_wwan:
473 rfkill_destroy(wwan_rfkill);
474 if (bluetooth_rfkill)
475 rfkill_unregister(bluetooth_rfkill);
476 err_bluetooth:
477 rfkill_destroy(bluetooth_rfkill);
478 if (wifi_rfkill)
479 rfkill_unregister(wifi_rfkill);
480 err_wifi:
481 rfkill_destroy(wifi_rfkill);
482
483 return ret;
484 }
485
486 static void dell_cleanup_rfkill(void)
487 {
488 if (wifi_rfkill) {
489 rfkill_unregister(wifi_rfkill);
490 rfkill_destroy(wifi_rfkill);
491 }
492 if (bluetooth_rfkill) {
493 rfkill_unregister(bluetooth_rfkill);
494 rfkill_destroy(bluetooth_rfkill);
495 }
496 if (wwan_rfkill) {
497 rfkill_unregister(wwan_rfkill);
498 rfkill_destroy(wwan_rfkill);
499 }
500 }
501
502 static int dell_send_intensity(struct backlight_device *bd)
503 {
504 int ret = 0;
505
506 get_buffer();
507 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
508 buffer->input[1] = bd->props.brightness;
509
510 if (buffer->input[0] == -1) {
511 ret = -ENODEV;
512 goto out;
513 }
514
515 if (power_supply_is_system_supplied() > 0)
516 dell_send_request(buffer, 1, 2);
517 else
518 dell_send_request(buffer, 1, 1);
519
520 out:
521 release_buffer();
522 return 0;
523 }
524
525 static int dell_get_intensity(struct backlight_device *bd)
526 {
527 int ret = 0;
528
529 get_buffer();
530 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
531
532 if (buffer->input[0] == -1) {
533 ret = -ENODEV;
534 goto out;
535 }
536
537 if (power_supply_is_system_supplied() > 0)
538 dell_send_request(buffer, 0, 2);
539 else
540 dell_send_request(buffer, 0, 1);
541
542 out:
543 release_buffer();
544 if (ret)
545 return ret;
546 return buffer->output[1];
547 }
548
549 static const struct backlight_ops dell_ops = {
550 .get_brightness = dell_get_intensity,
551 .update_status = dell_send_intensity,
552 };
553
554 static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
555 struct serio *port)
556 {
557 static bool extended;
558
559 if (str & 0x20)
560 return false;
561
562 if (unlikely(data == 0xe0)) {
563 extended = true;
564 return false;
565 } else if (unlikely(extended)) {
566 switch (data) {
567 case 0x8:
568 schedule_delayed_work(&dell_rfkill_work,
569 round_jiffies_relative(HZ));
570 break;
571 }
572 extended = false;
573 }
574
575 return false;
576 }
577
578 static int __init dell_init(void)
579 {
580 int max_intensity = 0;
581 int ret;
582
583 if (!dmi_check_system(dell_device_table))
584 return -ENODEV;
585
586 dmi_walk(find_tokens, NULL);
587
588 if (!da_tokens) {
589 printk(KERN_INFO "dell-laptop: Unable to find dmi tokens\n");
590 return -ENODEV;
591 }
592
593 ret = platform_driver_register(&platform_driver);
594 if (ret)
595 goto fail_platform_driver;
596 platform_device = platform_device_alloc("dell-laptop", -1);
597 if (!platform_device) {
598 ret = -ENOMEM;
599 goto fail_platform_device1;
600 }
601 ret = platform_device_add(platform_device);
602 if (ret)
603 goto fail_platform_device2;
604
605 /*
606 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
607 * is passed to SMI handler.
608 */
609 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
610
611 if (!bufferpage)
612 goto fail_buffer;
613 buffer = page_address(bufferpage);
614 mutex_init(&buffer_mutex);
615
616 ret = dell_setup_rfkill();
617
618 if (ret) {
619 printk(KERN_WARNING "dell-laptop: Unable to setup rfkill\n");
620 goto fail_rfkill;
621 }
622
623 ret = i8042_install_filter(dell_laptop_i8042_filter);
624 if (ret) {
625 printk(KERN_WARNING
626 "dell-laptop: Unable to install key filter\n");
627 goto fail_filter;
628 }
629
630 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
631 if (dell_laptop_dir != NULL)
632 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
633 &dell_debugfs_fops);
634
635 #ifdef CONFIG_ACPI
636 /* In the event of an ACPI backlight being available, don't
637 * register the platform controller.
638 */
639 if (acpi_video_backlight_support())
640 return 0;
641 #endif
642
643 get_buffer();
644 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
645 if (buffer->input[0] != -1) {
646 dell_send_request(buffer, 0, 2);
647 max_intensity = buffer->output[3];
648 }
649 release_buffer();
650
651 if (max_intensity) {
652 struct backlight_properties props;
653 memset(&props, 0, sizeof(struct backlight_properties));
654 props.max_brightness = max_intensity;
655 dell_backlight_device = backlight_device_register("dell_backlight",
656 &platform_device->dev,
657 NULL,
658 &dell_ops,
659 &props);
660
661 if (IS_ERR(dell_backlight_device)) {
662 ret = PTR_ERR(dell_backlight_device);
663 dell_backlight_device = NULL;
664 goto fail_backlight;
665 }
666
667 dell_backlight_device->props.brightness =
668 dell_get_intensity(dell_backlight_device);
669 backlight_update_status(dell_backlight_device);
670 }
671
672 return 0;
673
674 fail_backlight:
675 i8042_remove_filter(dell_laptop_i8042_filter);
676 cancel_delayed_work_sync(&dell_rfkill_work);
677 fail_filter:
678 dell_cleanup_rfkill();
679 fail_rfkill:
680 free_page((unsigned long)bufferpage);
681 fail_buffer:
682 platform_device_del(platform_device);
683 fail_platform_device2:
684 platform_device_put(platform_device);
685 fail_platform_device1:
686 platform_driver_unregister(&platform_driver);
687 fail_platform_driver:
688 kfree(da_tokens);
689 return ret;
690 }
691
692 static void __exit dell_exit(void)
693 {
694 debugfs_remove_recursive(dell_laptop_dir);
695 i8042_remove_filter(dell_laptop_i8042_filter);
696 cancel_delayed_work_sync(&dell_rfkill_work);
697 backlight_device_unregister(dell_backlight_device);
698 dell_cleanup_rfkill();
699 if (platform_device) {
700 platform_device_unregister(platform_device);
701 platform_driver_unregister(&platform_driver);
702 }
703 kfree(da_tokens);
704 free_page((unsigned long)buffer);
705 }
706
707 module_init(dell_init);
708 module_exit(dell_exit);
709
710 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
711 MODULE_DESCRIPTION("Dell laptop driver");
712 MODULE_LICENSE("GPL");
713 MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
714 MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
715 MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");