Loading...
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/backlight.h>
21#include <linux/err.h>
22#include <linux/dmi.h>
23#include <linux/io.h>
24#include <linux/rfkill.h>
25#include <linux/power_supply.h>
26#include <linux/acpi.h>
27#include <linux/mm.h>
28#include <linux/i8042.h>
29#include <linux/slab.h>
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
32#include "../../firmware/dcdbas.h"
33
34#define BRIGHTNESS_TOKEN 0x7d
35
36/* This structure will be modified by the firmware when we enter
37 * system management mode, hence the volatiles */
38
39struct calling_interface_buffer {
40 u16 class;
41 u16 select;
42 volatile u32 input[4];
43 volatile u32 output[4];
44} __packed;
45
46struct calling_interface_token {
47 u16 tokenID;
48 u16 location;
49 union {
50 u16 value;
51 u16 stringlength;
52 };
53};
54
55struct calling_interface_structure {
56 struct dmi_header header;
57 u16 cmdIOAddress;
58 u8 cmdIOCode;
59 u32 supportedCmds;
60 struct calling_interface_token tokens[];
61} __packed;
62
63static int da_command_address;
64static int da_command_code;
65static int da_num_tokens;
66static struct calling_interface_token *da_tokens;
67
68static struct platform_driver platform_driver = {
69 .driver = {
70 .name = "dell-laptop",
71 .owner = THIS_MODULE,
72 }
73};
74
75static struct platform_device *platform_device;
76static struct backlight_device *dell_backlight_device;
77static struct rfkill *wifi_rfkill;
78static struct rfkill *bluetooth_rfkill;
79static struct rfkill *wwan_rfkill;
80
81static const struct dmi_system_id __initdata dell_device_table[] = {
82 {
83 .ident = "Dell laptop",
84 .matches = {
85 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
86 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
87 },
88 },
89 {
90 .matches = {
91 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
92 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
93 },
94 },
95 {
96 .ident = "Dell Computer Corporation",
97 .matches = {
98 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
99 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
100 },
101 },
102 { }
103};
104
105static struct dmi_system_id __devinitdata dell_blacklist[] = {
106 /* Supported by compal-laptop */
107 {
108 .ident = "Dell Mini 9",
109 .matches = {
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
111 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
112 },
113 },
114 {
115 .ident = "Dell Mini 10",
116 .matches = {
117 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
118 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
119 },
120 },
121 {
122 .ident = "Dell Mini 10v",
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
125 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
126 },
127 },
128 {
129 .ident = "Dell Mini 1012",
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
133 },
134 },
135 {
136 .ident = "Dell Inspiron 11z",
137 .matches = {
138 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
139 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
140 },
141 },
142 {
143 .ident = "Dell Mini 12",
144 .matches = {
145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
146 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
147 },
148 },
149 {}
150};
151
152static struct calling_interface_buffer *buffer;
153static struct page *bufferpage;
154static DEFINE_MUTEX(buffer_mutex);
155
156static int hwswitch_state;
157
158static void get_buffer(void)
159{
160 mutex_lock(&buffer_mutex);
161 memset(buffer, 0, sizeof(struct calling_interface_buffer));
162}
163
164static void release_buffer(void)
165{
166 mutex_unlock(&buffer_mutex);
167}
168
169static void __init parse_da_table(const struct dmi_header *dm)
170{
171 /* Final token is a terminator, so we don't want to copy it */
172 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
173 struct calling_interface_structure *table =
174 container_of(dm, struct calling_interface_structure, header);
175
176 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
177 6 bytes of entry */
178
179 if (dm->length < 17)
180 return;
181
182 da_command_address = table->cmdIOAddress;
183 da_command_code = table->cmdIOCode;
184
185 da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
186 sizeof(struct calling_interface_token),
187 GFP_KERNEL);
188
189 if (!da_tokens)
190 return;
191
192 memcpy(da_tokens+da_num_tokens, table->tokens,
193 sizeof(struct calling_interface_token) * tokens);
194
195 da_num_tokens += tokens;
196}
197
198static void __init find_tokens(const struct dmi_header *dm, void *dummy)
199{
200 switch (dm->type) {
201 case 0xd4: /* Indexed IO */
202 break;
203 case 0xd5: /* Protected Area Type 1 */
204 break;
205 case 0xd6: /* Protected Area Type 2 */
206 break;
207 case 0xda: /* Calling interface */
208 parse_da_table(dm);
209 break;
210 }
211}
212
213static int find_token_location(int tokenid)
214{
215 int i;
216 for (i = 0; i < da_num_tokens; i++) {
217 if (da_tokens[i].tokenID == tokenid)
218 return da_tokens[i].location;
219 }
220
221 return -1;
222}
223
224static struct calling_interface_buffer *
225dell_send_request(struct calling_interface_buffer *buffer, int class,
226 int select)
227{
228 struct smi_cmd command;
229
230 command.magic = SMI_CMD_MAGIC;
231 command.command_address = da_command_address;
232 command.command_code = da_command_code;
233 command.ebx = virt_to_phys(buffer);
234 command.ecx = 0x42534931;
235
236 buffer->class = class;
237 buffer->select = select;
238
239 dcdbas_smi_request(&command);
240
241 return buffer;
242}
243
244/* Derived from information in DellWirelessCtl.cpp:
245 Class 17, select 11 is radio control. It returns an array of 32-bit values.
246
247 Input byte 0 = 0: Wireless information
248
249 result[0]: return code
250 result[1]:
251 Bit 0: Hardware switch supported
252 Bit 1: Wifi locator supported
253 Bit 2: Wifi is supported
254 Bit 3: Bluetooth is supported
255 Bit 4: WWAN is supported
256 Bit 5: Wireless keyboard supported
257 Bits 6-7: Reserved
258 Bit 8: Wifi is installed
259 Bit 9: Bluetooth is installed
260 Bit 10: WWAN is installed
261 Bits 11-15: Reserved
262 Bit 16: Hardware switch is on
263 Bit 17: Wifi is blocked
264 Bit 18: Bluetooth is blocked
265 Bit 19: WWAN is blocked
266 Bits 20-31: Reserved
267 result[2]: NVRAM size in bytes
268 result[3]: NVRAM format version number
269
270 Input byte 0 = 2: Wireless switch configuration
271 result[0]: return code
272 result[1]:
273 Bit 0: Wifi controlled by switch
274 Bit 1: Bluetooth controlled by switch
275 Bit 2: WWAN controlled by switch
276 Bits 3-6: Reserved
277 Bit 7: Wireless switch config locked
278 Bit 8: Wifi locator enabled
279 Bits 9-14: Reserved
280 Bit 15: Wifi locator setting locked
281 Bits 16-31: Reserved
282*/
283
284static int dell_rfkill_set(void *data, bool blocked)
285{
286 int disable = blocked ? 1 : 0;
287 unsigned long radio = (unsigned long)data;
288 int hwswitch_bit = (unsigned long)data - 1;
289 int ret = 0;
290
291 get_buffer();
292 dell_send_request(buffer, 17, 11);
293
294 /* If the hardware switch controls this radio, and the hardware
295 switch is disabled, don't allow changing the software state */
296 if ((hwswitch_state & BIT(hwswitch_bit)) &&
297 !(buffer->output[1] & BIT(16))) {
298 ret = -EINVAL;
299 goto out;
300 }
301
302 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
303 dell_send_request(buffer, 17, 11);
304
305out:
306 release_buffer();
307 return ret;
308}
309
310static void dell_rfkill_query(struct rfkill *rfkill, void *data)
311{
312 int status;
313 int bit = (unsigned long)data + 16;
314 int hwswitch_bit = (unsigned long)data - 1;
315
316 get_buffer();
317 dell_send_request(buffer, 17, 11);
318 status = buffer->output[1];
319 release_buffer();
320
321 rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
322
323 if (hwswitch_state & (BIT(hwswitch_bit)))
324 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
325}
326
327static const struct rfkill_ops dell_rfkill_ops = {
328 .set_block = dell_rfkill_set,
329 .query = dell_rfkill_query,
330};
331
332static struct dentry *dell_laptop_dir;
333
334static int dell_debugfs_show(struct seq_file *s, void *data)
335{
336 int status;
337
338 get_buffer();
339 dell_send_request(buffer, 17, 11);
340 status = buffer->output[1];
341 release_buffer();
342
343 seq_printf(s, "status:\t0x%X\n", status);
344 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
345 status & BIT(0));
346 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
347 (status & BIT(1)) >> 1);
348 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
349 (status & BIT(2)) >> 2);
350 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
351 (status & BIT(3)) >> 3);
352 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
353 (status & BIT(4)) >> 4);
354 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
355 (status & BIT(5)) >> 5);
356 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
357 (status & BIT(8)) >> 8);
358 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
359 (status & BIT(9)) >> 9);
360 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
361 (status & BIT(10)) >> 10);
362 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
363 (status & BIT(16)) >> 16);
364 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
365 (status & BIT(17)) >> 17);
366 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
367 (status & BIT(18)) >> 18);
368 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
369 (status & BIT(19)) >> 19);
370
371 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
372 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
373 hwswitch_state & BIT(0));
374 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
375 (hwswitch_state & BIT(1)) >> 1);
376 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
377 (hwswitch_state & BIT(2)) >> 2);
378 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
379 (hwswitch_state & BIT(7)) >> 7);
380 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
381 (hwswitch_state & BIT(8)) >> 8);
382 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
383 (hwswitch_state & BIT(15)) >> 15);
384
385 return 0;
386}
387
388static int dell_debugfs_open(struct inode *inode, struct file *file)
389{
390 return single_open(file, dell_debugfs_show, inode->i_private);
391}
392
393static const struct file_operations dell_debugfs_fops = {
394 .owner = THIS_MODULE,
395 .open = dell_debugfs_open,
396 .read = seq_read,
397 .llseek = seq_lseek,
398 .release = single_release,
399};
400
401static void dell_update_rfkill(struct work_struct *ignored)
402{
403 if (wifi_rfkill)
404 dell_rfkill_query(wifi_rfkill, (void *)1);
405 if (bluetooth_rfkill)
406 dell_rfkill_query(bluetooth_rfkill, (void *)2);
407 if (wwan_rfkill)
408 dell_rfkill_query(wwan_rfkill, (void *)3);
409}
410static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
411
412
413static int __init dell_setup_rfkill(void)
414{
415 int status;
416 int ret;
417
418 if (dmi_check_system(dell_blacklist)) {
419 pr_info("Blacklisted hardware detected - not enabling rfkill\n");
420 return 0;
421 }
422
423 get_buffer();
424 dell_send_request(buffer, 17, 11);
425 status = buffer->output[1];
426 buffer->input[0] = 0x2;
427 dell_send_request(buffer, 17, 11);
428 hwswitch_state = buffer->output[1];
429 release_buffer();
430
431 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
432 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
433 RFKILL_TYPE_WLAN,
434 &dell_rfkill_ops, (void *) 1);
435 if (!wifi_rfkill) {
436 ret = -ENOMEM;
437 goto err_wifi;
438 }
439 ret = rfkill_register(wifi_rfkill);
440 if (ret)
441 goto err_wifi;
442 }
443
444 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
445 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
446 &platform_device->dev,
447 RFKILL_TYPE_BLUETOOTH,
448 &dell_rfkill_ops, (void *) 2);
449 if (!bluetooth_rfkill) {
450 ret = -ENOMEM;
451 goto err_bluetooth;
452 }
453 ret = rfkill_register(bluetooth_rfkill);
454 if (ret)
455 goto err_bluetooth;
456 }
457
458 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
459 wwan_rfkill = rfkill_alloc("dell-wwan",
460 &platform_device->dev,
461 RFKILL_TYPE_WWAN,
462 &dell_rfkill_ops, (void *) 3);
463 if (!wwan_rfkill) {
464 ret = -ENOMEM;
465 goto err_wwan;
466 }
467 ret = rfkill_register(wwan_rfkill);
468 if (ret)
469 goto err_wwan;
470 }
471
472 return 0;
473err_wwan:
474 rfkill_destroy(wwan_rfkill);
475 if (bluetooth_rfkill)
476 rfkill_unregister(bluetooth_rfkill);
477err_bluetooth:
478 rfkill_destroy(bluetooth_rfkill);
479 if (wifi_rfkill)
480 rfkill_unregister(wifi_rfkill);
481err_wifi:
482 rfkill_destroy(wifi_rfkill);
483
484 return ret;
485}
486
487static void dell_cleanup_rfkill(void)
488{
489 if (wifi_rfkill) {
490 rfkill_unregister(wifi_rfkill);
491 rfkill_destroy(wifi_rfkill);
492 }
493 if (bluetooth_rfkill) {
494 rfkill_unregister(bluetooth_rfkill);
495 rfkill_destroy(bluetooth_rfkill);
496 }
497 if (wwan_rfkill) {
498 rfkill_unregister(wwan_rfkill);
499 rfkill_destroy(wwan_rfkill);
500 }
501}
502
503static int dell_send_intensity(struct backlight_device *bd)
504{
505 int ret = 0;
506
507 get_buffer();
508 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
509 buffer->input[1] = bd->props.brightness;
510
511 if (buffer->input[0] == -1) {
512 ret = -ENODEV;
513 goto out;
514 }
515
516 if (power_supply_is_system_supplied() > 0)
517 dell_send_request(buffer, 1, 2);
518 else
519 dell_send_request(buffer, 1, 1);
520
521out:
522 release_buffer();
523 return 0;
524}
525
526static int dell_get_intensity(struct backlight_device *bd)
527{
528 int ret = 0;
529
530 get_buffer();
531 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
532
533 if (buffer->input[0] == -1) {
534 ret = -ENODEV;
535 goto out;
536 }
537
538 if (power_supply_is_system_supplied() > 0)
539 dell_send_request(buffer, 0, 2);
540 else
541 dell_send_request(buffer, 0, 1);
542
543 ret = buffer->output[1];
544
545out:
546 release_buffer();
547 return ret;
548}
549
550static const struct backlight_ops dell_ops = {
551 .get_brightness = dell_get_intensity,
552 .update_status = dell_send_intensity,
553};
554
555static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
556 struct serio *port)
557{
558 static bool extended;
559
560 if (str & 0x20)
561 return false;
562
563 if (unlikely(data == 0xe0)) {
564 extended = true;
565 return false;
566 } else if (unlikely(extended)) {
567 switch (data) {
568 case 0x8:
569 schedule_delayed_work(&dell_rfkill_work,
570 round_jiffies_relative(HZ));
571 break;
572 }
573 extended = false;
574 }
575
576 return false;
577}
578
579static int __init dell_init(void)
580{
581 int max_intensity = 0;
582 int ret;
583
584 if (!dmi_check_system(dell_device_table))
585 return -ENODEV;
586
587 dmi_walk(find_tokens, NULL);
588
589 if (!da_tokens) {
590 pr_info("Unable to find dmi tokens\n");
591 return -ENODEV;
592 }
593
594 ret = platform_driver_register(&platform_driver);
595 if (ret)
596 goto fail_platform_driver;
597 platform_device = platform_device_alloc("dell-laptop", -1);
598 if (!platform_device) {
599 ret = -ENOMEM;
600 goto fail_platform_device1;
601 }
602 ret = platform_device_add(platform_device);
603 if (ret)
604 goto fail_platform_device2;
605
606 /*
607 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
608 * is passed to SMI handler.
609 */
610 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
611
612 if (!bufferpage)
613 goto fail_buffer;
614 buffer = page_address(bufferpage);
615
616 ret = dell_setup_rfkill();
617
618 if (ret) {
619 pr_warn("Unable to setup rfkill\n");
620 goto fail_rfkill;
621 }
622
623 ret = i8042_install_filter(dell_laptop_i8042_filter);
624 if (ret) {
625 pr_warn("Unable to install key filter\n");
626 goto fail_filter;
627 }
628
629 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
630 if (dell_laptop_dir != NULL)
631 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
632 &dell_debugfs_fops);
633
634#ifdef CONFIG_ACPI
635 /* In the event of an ACPI backlight being available, don't
636 * register the platform controller.
637 */
638 if (acpi_video_backlight_support())
639 return 0;
640#endif
641
642 get_buffer();
643 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
644 if (buffer->input[0] != -1) {
645 dell_send_request(buffer, 0, 2);
646 max_intensity = buffer->output[3];
647 }
648 release_buffer();
649
650 if (max_intensity) {
651 struct backlight_properties props;
652 memset(&props, 0, sizeof(struct backlight_properties));
653 props.type = BACKLIGHT_PLATFORM;
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
674fail_backlight:
675 i8042_remove_filter(dell_laptop_i8042_filter);
676 cancel_delayed_work_sync(&dell_rfkill_work);
677fail_filter:
678 dell_cleanup_rfkill();
679fail_rfkill:
680 free_page((unsigned long)bufferpage);
681fail_buffer:
682 platform_device_del(platform_device);
683fail_platform_device2:
684 platform_device_put(platform_device);
685fail_platform_device1:
686 platform_driver_unregister(&platform_driver);
687fail_platform_driver:
688 kfree(da_tokens);
689 return ret;
690}
691
692static 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
707module_init(dell_init);
708module_exit(dell_exit);
709
710MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
711MODULE_DESCRIPTION("Dell laptop driver");
712MODULE_LICENSE("GPL");
713MODULE_ALIAS("dmi:*svnDellInc.:*:ct8:*");
714MODULE_ALIAS("dmi:*svnDellInc.:*:ct9:*");
715MODULE_ALIAS("dmi:*svnDellComputerCorporation.:*:ct8:*");
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/backlight.h>
21#include <linux/err.h>
22#include <linux/dmi.h>
23#include <linux/io.h>
24#include <linux/rfkill.h>
25#include <linux/power_supply.h>
26#include <linux/acpi.h>
27#include <linux/mm.h>
28#include <linux/i8042.h>
29#include <linux/slab.h>
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
32#include "../../firmware/dcdbas.h"
33
34#define BRIGHTNESS_TOKEN 0x7d
35
36/* This structure will be modified by the firmware when we enter
37 * system management mode, hence the volatiles */
38
39struct calling_interface_buffer {
40 u16 class;
41 u16 select;
42 volatile u32 input[4];
43 volatile u32 output[4];
44} __packed;
45
46struct calling_interface_token {
47 u16 tokenID;
48 u16 location;
49 union {
50 u16 value;
51 u16 stringlength;
52 };
53};
54
55struct calling_interface_structure {
56 struct dmi_header header;
57 u16 cmdIOAddress;
58 u8 cmdIOCode;
59 u32 supportedCmds;
60 struct calling_interface_token tokens[];
61} __packed;
62
63struct quirk_entry {
64 u8 touchpad_led;
65};
66
67static struct quirk_entry *quirks;
68
69static struct quirk_entry quirk_dell_vostro_v130 = {
70 .touchpad_led = 1,
71};
72
73static int dmi_matched(const struct dmi_system_id *dmi)
74{
75 quirks = dmi->driver_data;
76 return 1;
77}
78
79static int da_command_address;
80static int da_command_code;
81static int da_num_tokens;
82static struct calling_interface_token *da_tokens;
83
84static struct platform_driver platform_driver = {
85 .driver = {
86 .name = "dell-laptop",
87 .owner = THIS_MODULE,
88 }
89};
90
91static struct platform_device *platform_device;
92static struct backlight_device *dell_backlight_device;
93static struct rfkill *wifi_rfkill;
94static struct rfkill *bluetooth_rfkill;
95static struct rfkill *wwan_rfkill;
96static bool force_rfkill;
97
98module_param(force_rfkill, bool, 0444);
99MODULE_PARM_DESC(force_rfkill, "enable rfkill on non whitelisted models");
100
101static const struct dmi_system_id dell_device_table[] __initconst = {
102 {
103 .ident = "Dell laptop",
104 .matches = {
105 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
106 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
107 },
108 },
109 {
110 .matches = {
111 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
112 DMI_MATCH(DMI_CHASSIS_TYPE, "9"), /*Laptop*/
113 },
114 },
115 {
116 .ident = "Dell Computer Corporation",
117 .matches = {
118 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
119 DMI_MATCH(DMI_CHASSIS_TYPE, "8"),
120 },
121 },
122 { }
123};
124MODULE_DEVICE_TABLE(dmi, dell_device_table);
125
126static struct dmi_system_id dell_quirks[] = {
127 {
128 .callback = dmi_matched,
129 .ident = "Dell Vostro V130",
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V130"),
133 },
134 .driver_data = &quirk_dell_vostro_v130,
135 },
136 {
137 .callback = dmi_matched,
138 .ident = "Dell Vostro V131",
139 .matches = {
140 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
141 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
142 },
143 .driver_data = &quirk_dell_vostro_v130,
144 },
145 {
146 .callback = dmi_matched,
147 .ident = "Dell Vostro 3350",
148 .matches = {
149 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
150 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
151 },
152 .driver_data = &quirk_dell_vostro_v130,
153 },
154 {
155 .callback = dmi_matched,
156 .ident = "Dell Vostro 3555",
157 .matches = {
158 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
159 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3555"),
160 },
161 .driver_data = &quirk_dell_vostro_v130,
162 },
163 {
164 .callback = dmi_matched,
165 .ident = "Dell Inspiron N311z",
166 .matches = {
167 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
168 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron N311z"),
169 },
170 .driver_data = &quirk_dell_vostro_v130,
171 },
172 {
173 .callback = dmi_matched,
174 .ident = "Dell Inspiron M5110",
175 .matches = {
176 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
177 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron M5110"),
178 },
179 .driver_data = &quirk_dell_vostro_v130,
180 },
181 {
182 .callback = dmi_matched,
183 .ident = "Dell Vostro 3360",
184 .matches = {
185 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
186 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
187 },
188 .driver_data = &quirk_dell_vostro_v130,
189 },
190 {
191 .callback = dmi_matched,
192 .ident = "Dell Vostro 3460",
193 .matches = {
194 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
195 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3460"),
196 },
197 .driver_data = &quirk_dell_vostro_v130,
198 },
199 {
200 .callback = dmi_matched,
201 .ident = "Dell Vostro 3560",
202 .matches = {
203 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
204 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3560"),
205 },
206 .driver_data = &quirk_dell_vostro_v130,
207 },
208 {
209 .callback = dmi_matched,
210 .ident = "Dell Vostro 3450",
211 .matches = {
212 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
213 DMI_MATCH(DMI_PRODUCT_NAME, "Dell System Vostro 3450"),
214 },
215 .driver_data = &quirk_dell_vostro_v130,
216 },
217 {
218 .callback = dmi_matched,
219 .ident = "Dell Inspiron 5420",
220 .matches = {
221 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5420"),
223 },
224 .driver_data = &quirk_dell_vostro_v130,
225 },
226 {
227 .callback = dmi_matched,
228 .ident = "Dell Inspiron 5520",
229 .matches = {
230 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
231 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5520"),
232 },
233 .driver_data = &quirk_dell_vostro_v130,
234 },
235 {
236 .callback = dmi_matched,
237 .ident = "Dell Inspiron 5720",
238 .matches = {
239 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
240 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5720"),
241 },
242 .driver_data = &quirk_dell_vostro_v130,
243 },
244 {
245 .callback = dmi_matched,
246 .ident = "Dell Inspiron 7420",
247 .matches = {
248 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
249 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7420"),
250 },
251 .driver_data = &quirk_dell_vostro_v130,
252 },
253 {
254 .callback = dmi_matched,
255 .ident = "Dell Inspiron 7520",
256 .matches = {
257 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
258 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
259 },
260 .driver_data = &quirk_dell_vostro_v130,
261 },
262 {
263 .callback = dmi_matched,
264 .ident = "Dell Inspiron 7720",
265 .matches = {
266 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
267 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
268 },
269 .driver_data = &quirk_dell_vostro_v130,
270 },
271 { }
272};
273
274static struct calling_interface_buffer *buffer;
275static struct page *bufferpage;
276static DEFINE_MUTEX(buffer_mutex);
277
278static int hwswitch_state;
279
280static void get_buffer(void)
281{
282 mutex_lock(&buffer_mutex);
283 memset(buffer, 0, sizeof(struct calling_interface_buffer));
284}
285
286static void release_buffer(void)
287{
288 mutex_unlock(&buffer_mutex);
289}
290
291static void __init parse_da_table(const struct dmi_header *dm)
292{
293 /* Final token is a terminator, so we don't want to copy it */
294 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
295 struct calling_interface_token *new_da_tokens;
296 struct calling_interface_structure *table =
297 container_of(dm, struct calling_interface_structure, header);
298
299 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
300 6 bytes of entry */
301
302 if (dm->length < 17)
303 return;
304
305 da_command_address = table->cmdIOAddress;
306 da_command_code = table->cmdIOCode;
307
308 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
309 sizeof(struct calling_interface_token),
310 GFP_KERNEL);
311
312 if (!new_da_tokens)
313 return;
314 da_tokens = new_da_tokens;
315
316 memcpy(da_tokens+da_num_tokens, table->tokens,
317 sizeof(struct calling_interface_token) * tokens);
318
319 da_num_tokens += tokens;
320}
321
322static void __init find_tokens(const struct dmi_header *dm, void *dummy)
323{
324 switch (dm->type) {
325 case 0xd4: /* Indexed IO */
326 case 0xd5: /* Protected Area Type 1 */
327 case 0xd6: /* Protected Area Type 2 */
328 break;
329 case 0xda: /* Calling interface */
330 parse_da_table(dm);
331 break;
332 }
333}
334
335static int find_token_location(int tokenid)
336{
337 int i;
338 for (i = 0; i < da_num_tokens; i++) {
339 if (da_tokens[i].tokenID == tokenid)
340 return da_tokens[i].location;
341 }
342
343 return -1;
344}
345
346static struct calling_interface_buffer *
347dell_send_request(struct calling_interface_buffer *buffer, int class,
348 int select)
349{
350 struct smi_cmd command;
351
352 command.magic = SMI_CMD_MAGIC;
353 command.command_address = da_command_address;
354 command.command_code = da_command_code;
355 command.ebx = virt_to_phys(buffer);
356 command.ecx = 0x42534931;
357
358 buffer->class = class;
359 buffer->select = select;
360
361 dcdbas_smi_request(&command);
362
363 return buffer;
364}
365
366/* Derived from information in DellWirelessCtl.cpp:
367 Class 17, select 11 is radio control. It returns an array of 32-bit values.
368
369 Input byte 0 = 0: Wireless information
370
371 result[0]: return code
372 result[1]:
373 Bit 0: Hardware switch supported
374 Bit 1: Wifi locator supported
375 Bit 2: Wifi is supported
376 Bit 3: Bluetooth is supported
377 Bit 4: WWAN is supported
378 Bit 5: Wireless keyboard supported
379 Bits 6-7: Reserved
380 Bit 8: Wifi is installed
381 Bit 9: Bluetooth is installed
382 Bit 10: WWAN is installed
383 Bits 11-15: Reserved
384 Bit 16: Hardware switch is on
385 Bit 17: Wifi is blocked
386 Bit 18: Bluetooth is blocked
387 Bit 19: WWAN is blocked
388 Bits 20-31: Reserved
389 result[2]: NVRAM size in bytes
390 result[3]: NVRAM format version number
391
392 Input byte 0 = 2: Wireless switch configuration
393 result[0]: return code
394 result[1]:
395 Bit 0: Wifi controlled by switch
396 Bit 1: Bluetooth controlled by switch
397 Bit 2: WWAN controlled by switch
398 Bits 3-6: Reserved
399 Bit 7: Wireless switch config locked
400 Bit 8: Wifi locator enabled
401 Bits 9-14: Reserved
402 Bit 15: Wifi locator setting locked
403 Bits 16-31: Reserved
404*/
405
406static int dell_rfkill_set(void *data, bool blocked)
407{
408 int disable = blocked ? 1 : 0;
409 unsigned long radio = (unsigned long)data;
410 int hwswitch_bit = (unsigned long)data - 1;
411
412 get_buffer();
413 dell_send_request(buffer, 17, 11);
414
415 /* If the hardware switch controls this radio, and the hardware
416 switch is disabled, always disable the radio */
417 if ((hwswitch_state & BIT(hwswitch_bit)) &&
418 !(buffer->output[1] & BIT(16)))
419 disable = 1;
420
421 buffer->input[0] = (1 | (radio<<8) | (disable << 16));
422 dell_send_request(buffer, 17, 11);
423
424 release_buffer();
425 return 0;
426}
427
428/* Must be called with the buffer held */
429static void dell_rfkill_update_sw_state(struct rfkill *rfkill, int radio,
430 int status)
431{
432 if (status & BIT(0)) {
433 /* Has hw-switch, sync sw_state to BIOS */
434 int block = rfkill_blocked(rfkill);
435 buffer->input[0] = (1 | (radio << 8) | (block << 16));
436 dell_send_request(buffer, 17, 11);
437 } else {
438 /* No hw-switch, sync BIOS state to sw_state */
439 rfkill_set_sw_state(rfkill, !!(status & BIT(radio + 16)));
440 }
441}
442
443static void dell_rfkill_update_hw_state(struct rfkill *rfkill, int radio,
444 int status)
445{
446 if (hwswitch_state & (BIT(radio - 1)))
447 rfkill_set_hw_state(rfkill, !(status & BIT(16)));
448}
449
450static void dell_rfkill_query(struct rfkill *rfkill, void *data)
451{
452 int status;
453
454 get_buffer();
455 dell_send_request(buffer, 17, 11);
456 status = buffer->output[1];
457
458 dell_rfkill_update_hw_state(rfkill, (unsigned long)data, status);
459
460 release_buffer();
461}
462
463static const struct rfkill_ops dell_rfkill_ops = {
464 .set_block = dell_rfkill_set,
465 .query = dell_rfkill_query,
466};
467
468static struct dentry *dell_laptop_dir;
469
470static int dell_debugfs_show(struct seq_file *s, void *data)
471{
472 int status;
473
474 get_buffer();
475 dell_send_request(buffer, 17, 11);
476 status = buffer->output[1];
477 release_buffer();
478
479 seq_printf(s, "status:\t0x%X\n", status);
480 seq_printf(s, "Bit 0 : Hardware switch supported: %lu\n",
481 status & BIT(0));
482 seq_printf(s, "Bit 1 : Wifi locator supported: %lu\n",
483 (status & BIT(1)) >> 1);
484 seq_printf(s, "Bit 2 : Wifi is supported: %lu\n",
485 (status & BIT(2)) >> 2);
486 seq_printf(s, "Bit 3 : Bluetooth is supported: %lu\n",
487 (status & BIT(3)) >> 3);
488 seq_printf(s, "Bit 4 : WWAN is supported: %lu\n",
489 (status & BIT(4)) >> 4);
490 seq_printf(s, "Bit 5 : Wireless keyboard supported: %lu\n",
491 (status & BIT(5)) >> 5);
492 seq_printf(s, "Bit 8 : Wifi is installed: %lu\n",
493 (status & BIT(8)) >> 8);
494 seq_printf(s, "Bit 9 : Bluetooth is installed: %lu\n",
495 (status & BIT(9)) >> 9);
496 seq_printf(s, "Bit 10: WWAN is installed: %lu\n",
497 (status & BIT(10)) >> 10);
498 seq_printf(s, "Bit 16: Hardware switch is on: %lu\n",
499 (status & BIT(16)) >> 16);
500 seq_printf(s, "Bit 17: Wifi is blocked: %lu\n",
501 (status & BIT(17)) >> 17);
502 seq_printf(s, "Bit 18: Bluetooth is blocked: %lu\n",
503 (status & BIT(18)) >> 18);
504 seq_printf(s, "Bit 19: WWAN is blocked: %lu\n",
505 (status & BIT(19)) >> 19);
506
507 seq_printf(s, "\nhwswitch_state:\t0x%X\n", hwswitch_state);
508 seq_printf(s, "Bit 0 : Wifi controlled by switch: %lu\n",
509 hwswitch_state & BIT(0));
510 seq_printf(s, "Bit 1 : Bluetooth controlled by switch: %lu\n",
511 (hwswitch_state & BIT(1)) >> 1);
512 seq_printf(s, "Bit 2 : WWAN controlled by switch: %lu\n",
513 (hwswitch_state & BIT(2)) >> 2);
514 seq_printf(s, "Bit 7 : Wireless switch config locked: %lu\n",
515 (hwswitch_state & BIT(7)) >> 7);
516 seq_printf(s, "Bit 8 : Wifi locator enabled: %lu\n",
517 (hwswitch_state & BIT(8)) >> 8);
518 seq_printf(s, "Bit 15: Wifi locator setting locked: %lu\n",
519 (hwswitch_state & BIT(15)) >> 15);
520
521 return 0;
522}
523
524static int dell_debugfs_open(struct inode *inode, struct file *file)
525{
526 return single_open(file, dell_debugfs_show, inode->i_private);
527}
528
529static const struct file_operations dell_debugfs_fops = {
530 .owner = THIS_MODULE,
531 .open = dell_debugfs_open,
532 .read = seq_read,
533 .llseek = seq_lseek,
534 .release = single_release,
535};
536
537static void dell_update_rfkill(struct work_struct *ignored)
538{
539 int status;
540
541 get_buffer();
542 dell_send_request(buffer, 17, 11);
543 status = buffer->output[1];
544
545 if (wifi_rfkill) {
546 dell_rfkill_update_hw_state(wifi_rfkill, 1, status);
547 dell_rfkill_update_sw_state(wifi_rfkill, 1, status);
548 }
549 if (bluetooth_rfkill) {
550 dell_rfkill_update_hw_state(bluetooth_rfkill, 2, status);
551 dell_rfkill_update_sw_state(bluetooth_rfkill, 2, status);
552 }
553 if (wwan_rfkill) {
554 dell_rfkill_update_hw_state(wwan_rfkill, 3, status);
555 dell_rfkill_update_sw_state(wwan_rfkill, 3, status);
556 }
557
558 release_buffer();
559}
560static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
561
562static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
563 struct serio *port)
564{
565 static bool extended;
566
567 if (str & 0x20)
568 return false;
569
570 if (unlikely(data == 0xe0)) {
571 extended = true;
572 return false;
573 } else if (unlikely(extended)) {
574 switch (data) {
575 case 0x8:
576 schedule_delayed_work(&dell_rfkill_work,
577 round_jiffies_relative(HZ / 4));
578 break;
579 }
580 extended = false;
581 }
582
583 return false;
584}
585
586static int __init dell_setup_rfkill(void)
587{
588 int status, ret, whitelisted;
589 const char *product;
590
591 /*
592 * rfkill support causes trouble on various models, mostly Inspirons.
593 * So we whitelist certain series, and don't support rfkill on others.
594 */
595 whitelisted = 0;
596 product = dmi_get_system_info(DMI_PRODUCT_NAME);
597 if (product && (strncmp(product, "Latitude", 8) == 0 ||
598 strncmp(product, "Precision", 9) == 0))
599 whitelisted = 1;
600 if (!force_rfkill && !whitelisted)
601 return 0;
602
603 get_buffer();
604 dell_send_request(buffer, 17, 11);
605 status = buffer->output[1];
606 buffer->input[0] = 0x2;
607 dell_send_request(buffer, 17, 11);
608 hwswitch_state = buffer->output[1];
609 release_buffer();
610
611 if (!(status & BIT(0))) {
612 if (force_rfkill) {
613 /* No hwsitch, clear all hw-controlled bits */
614 hwswitch_state &= ~7;
615 } else {
616 /* rfkill is only tested on laptops with a hwswitch */
617 return 0;
618 }
619 }
620
621 if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
622 wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
623 RFKILL_TYPE_WLAN,
624 &dell_rfkill_ops, (void *) 1);
625 if (!wifi_rfkill) {
626 ret = -ENOMEM;
627 goto err_wifi;
628 }
629 ret = rfkill_register(wifi_rfkill);
630 if (ret)
631 goto err_wifi;
632 }
633
634 if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
635 bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
636 &platform_device->dev,
637 RFKILL_TYPE_BLUETOOTH,
638 &dell_rfkill_ops, (void *) 2);
639 if (!bluetooth_rfkill) {
640 ret = -ENOMEM;
641 goto err_bluetooth;
642 }
643 ret = rfkill_register(bluetooth_rfkill);
644 if (ret)
645 goto err_bluetooth;
646 }
647
648 if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
649 wwan_rfkill = rfkill_alloc("dell-wwan",
650 &platform_device->dev,
651 RFKILL_TYPE_WWAN,
652 &dell_rfkill_ops, (void *) 3);
653 if (!wwan_rfkill) {
654 ret = -ENOMEM;
655 goto err_wwan;
656 }
657 ret = rfkill_register(wwan_rfkill);
658 if (ret)
659 goto err_wwan;
660 }
661
662 ret = i8042_install_filter(dell_laptop_i8042_filter);
663 if (ret) {
664 pr_warn("Unable to install key filter\n");
665 goto err_filter;
666 }
667
668 return 0;
669err_filter:
670 if (wwan_rfkill)
671 rfkill_unregister(wwan_rfkill);
672err_wwan:
673 rfkill_destroy(wwan_rfkill);
674 if (bluetooth_rfkill)
675 rfkill_unregister(bluetooth_rfkill);
676err_bluetooth:
677 rfkill_destroy(bluetooth_rfkill);
678 if (wifi_rfkill)
679 rfkill_unregister(wifi_rfkill);
680err_wifi:
681 rfkill_destroy(wifi_rfkill);
682
683 return ret;
684}
685
686static void dell_cleanup_rfkill(void)
687{
688 if (wifi_rfkill) {
689 rfkill_unregister(wifi_rfkill);
690 rfkill_destroy(wifi_rfkill);
691 }
692 if (bluetooth_rfkill) {
693 rfkill_unregister(bluetooth_rfkill);
694 rfkill_destroy(bluetooth_rfkill);
695 }
696 if (wwan_rfkill) {
697 rfkill_unregister(wwan_rfkill);
698 rfkill_destroy(wwan_rfkill);
699 }
700}
701
702static int dell_send_intensity(struct backlight_device *bd)
703{
704 int ret = 0;
705
706 get_buffer();
707 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
708 buffer->input[1] = bd->props.brightness;
709
710 if (buffer->input[0] == -1) {
711 ret = -ENODEV;
712 goto out;
713 }
714
715 if (power_supply_is_system_supplied() > 0)
716 dell_send_request(buffer, 1, 2);
717 else
718 dell_send_request(buffer, 1, 1);
719
720out:
721 release_buffer();
722 return ret;
723}
724
725static int dell_get_intensity(struct backlight_device *bd)
726{
727 int ret = 0;
728
729 get_buffer();
730 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
731
732 if (buffer->input[0] == -1) {
733 ret = -ENODEV;
734 goto out;
735 }
736
737 if (power_supply_is_system_supplied() > 0)
738 dell_send_request(buffer, 0, 2);
739 else
740 dell_send_request(buffer, 0, 1);
741
742 ret = buffer->output[1];
743
744out:
745 release_buffer();
746 return ret;
747}
748
749static const struct backlight_ops dell_ops = {
750 .get_brightness = dell_get_intensity,
751 .update_status = dell_send_intensity,
752};
753
754static void touchpad_led_on(void)
755{
756 int command = 0x97;
757 char data = 1;
758 i8042_command(&data, command | 1 << 12);
759}
760
761static void touchpad_led_off(void)
762{
763 int command = 0x97;
764 char data = 2;
765 i8042_command(&data, command | 1 << 12);
766}
767
768static void touchpad_led_set(struct led_classdev *led_cdev,
769 enum led_brightness value)
770{
771 if (value > 0)
772 touchpad_led_on();
773 else
774 touchpad_led_off();
775}
776
777static struct led_classdev touchpad_led = {
778 .name = "dell-laptop::touchpad",
779 .brightness_set = touchpad_led_set,
780 .flags = LED_CORE_SUSPENDRESUME,
781};
782
783static int touchpad_led_init(struct device *dev)
784{
785 return led_classdev_register(dev, &touchpad_led);
786}
787
788static void touchpad_led_exit(void)
789{
790 led_classdev_unregister(&touchpad_led);
791}
792
793static int __init dell_init(void)
794{
795 int max_intensity = 0;
796 int ret;
797
798 if (!dmi_check_system(dell_device_table))
799 return -ENODEV;
800
801 quirks = NULL;
802 /* find if this machine support other functions */
803 dmi_check_system(dell_quirks);
804
805 dmi_walk(find_tokens, NULL);
806
807 if (!da_tokens) {
808 pr_info("Unable to find dmi tokens\n");
809 return -ENODEV;
810 }
811
812 ret = platform_driver_register(&platform_driver);
813 if (ret)
814 goto fail_platform_driver;
815 platform_device = platform_device_alloc("dell-laptop", -1);
816 if (!platform_device) {
817 ret = -ENOMEM;
818 goto fail_platform_device1;
819 }
820 ret = platform_device_add(platform_device);
821 if (ret)
822 goto fail_platform_device2;
823
824 /*
825 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
826 * is passed to SMI handler.
827 */
828 bufferpage = alloc_page(GFP_KERNEL | GFP_DMA32);
829 if (!bufferpage) {
830 ret = -ENOMEM;
831 goto fail_buffer;
832 }
833 buffer = page_address(bufferpage);
834
835 ret = dell_setup_rfkill();
836
837 if (ret) {
838 pr_warn("Unable to setup rfkill\n");
839 goto fail_rfkill;
840 }
841
842 if (quirks && quirks->touchpad_led)
843 touchpad_led_init(&platform_device->dev);
844
845 dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
846 if (dell_laptop_dir != NULL)
847 debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
848 &dell_debugfs_fops);
849
850#ifdef CONFIG_ACPI
851 /* In the event of an ACPI backlight being available, don't
852 * register the platform controller.
853 */
854 if (acpi_video_backlight_support())
855 return 0;
856#endif
857
858 get_buffer();
859 buffer->input[0] = find_token_location(BRIGHTNESS_TOKEN);
860 if (buffer->input[0] != -1) {
861 dell_send_request(buffer, 0, 2);
862 max_intensity = buffer->output[3];
863 }
864 release_buffer();
865
866 if (max_intensity) {
867 struct backlight_properties props;
868 memset(&props, 0, sizeof(struct backlight_properties));
869 props.type = BACKLIGHT_PLATFORM;
870 props.max_brightness = max_intensity;
871 dell_backlight_device = backlight_device_register("dell_backlight",
872 &platform_device->dev,
873 NULL,
874 &dell_ops,
875 &props);
876
877 if (IS_ERR(dell_backlight_device)) {
878 ret = PTR_ERR(dell_backlight_device);
879 dell_backlight_device = NULL;
880 goto fail_backlight;
881 }
882
883 dell_backlight_device->props.brightness =
884 dell_get_intensity(dell_backlight_device);
885 backlight_update_status(dell_backlight_device);
886 }
887
888 return 0;
889
890fail_backlight:
891 i8042_remove_filter(dell_laptop_i8042_filter);
892 cancel_delayed_work_sync(&dell_rfkill_work);
893 dell_cleanup_rfkill();
894fail_rfkill:
895 free_page((unsigned long)bufferpage);
896fail_buffer:
897 platform_device_del(platform_device);
898fail_platform_device2:
899 platform_device_put(platform_device);
900fail_platform_device1:
901 platform_driver_unregister(&platform_driver);
902fail_platform_driver:
903 kfree(da_tokens);
904 return ret;
905}
906
907static void __exit dell_exit(void)
908{
909 debugfs_remove_recursive(dell_laptop_dir);
910 if (quirks && quirks->touchpad_led)
911 touchpad_led_exit();
912 i8042_remove_filter(dell_laptop_i8042_filter);
913 cancel_delayed_work_sync(&dell_rfkill_work);
914 backlight_device_unregister(dell_backlight_device);
915 dell_cleanup_rfkill();
916 if (platform_device) {
917 platform_device_unregister(platform_device);
918 platform_driver_unregister(&platform_driver);
919 }
920 kfree(da_tokens);
921 free_page((unsigned long)buffer);
922}
923
924module_init(dell_init);
925module_exit(dell_exit);
926
927MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
928MODULE_DESCRIPTION("Dell laptop driver");
929MODULE_LICENSE("GPL");