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