]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/acer-wmi.c
acer-wmi: Add rfkill support for wireless and bluetooth
[linux-2.6-omap-h63xx.git] / drivers / misc / acer-wmi.c
1 /*
2  *  Acer WMI Laptop Extras
3  *
4  *  Copyright (C) 2007-2008     Carlos Corbacho <carlos@strangeworlds.co.uk>
5  *
6  *  Based on acer_acpi:
7  *    Copyright (C) 2005-2007   E.M. Smith
8  *    Copyright (C) 2007-2008   Carlos Corbacho <cathectic@gmail.com>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/dmi.h>
30 #include <linux/fb.h>
31 #include <linux/backlight.h>
32 #include <linux/leds.h>
33 #include <linux/platform_device.h>
34 #include <linux/acpi.h>
35 #include <linux/i8042.h>
36 #include <linux/rfkill.h>
37 #include <linux/workqueue.h>
38 #include <linux/debugfs.h>
39
40 #include <acpi/acpi_drivers.h>
41
42 MODULE_AUTHOR("Carlos Corbacho");
43 MODULE_DESCRIPTION("Acer Laptop WMI Extras Driver");
44 MODULE_LICENSE("GPL");
45
46 #define ACER_LOGPREFIX "acer-wmi: "
47 #define ACER_ERR KERN_ERR ACER_LOGPREFIX
48 #define ACER_NOTICE KERN_NOTICE ACER_LOGPREFIX
49 #define ACER_INFO KERN_INFO ACER_LOGPREFIX
50
51 /*
52  * The following defines quirks to get some specific functions to work
53  * which are known to not be supported over ACPI-WMI (such as the mail LED
54  * on WMID based Acer's)
55  */
56 struct acer_quirks {
57         const char *vendor;
58         const char *model;
59         u16 quirks;
60 };
61
62 /*
63  * Magic Number
64  * Meaning is unknown - this number is required for writing to ACPI for AMW0
65  * (it's also used in acerhk when directly accessing the BIOS)
66  */
67 #define ACER_AMW0_WRITE 0x9610
68
69 /*
70  * Bit masks for the AMW0 interface
71  */
72 #define ACER_AMW0_WIRELESS_MASK  0x35
73 #define ACER_AMW0_BLUETOOTH_MASK 0x34
74 #define ACER_AMW0_MAILLED_MASK   0x31
75
76 /*
77  * Method IDs for WMID interface
78  */
79 #define ACER_WMID_GET_WIRELESS_METHODID         1
80 #define ACER_WMID_GET_BLUETOOTH_METHODID        2
81 #define ACER_WMID_GET_BRIGHTNESS_METHODID       3
82 #define ACER_WMID_SET_WIRELESS_METHODID         4
83 #define ACER_WMID_SET_BLUETOOTH_METHODID        5
84 #define ACER_WMID_SET_BRIGHTNESS_METHODID       6
85 #define ACER_WMID_GET_THREEG_METHODID           10
86 #define ACER_WMID_SET_THREEG_METHODID           11
87
88 /*
89  * Acer ACPI method GUIDs
90  */
91 #define AMW0_GUID1              "67C3371D-95A3-4C37-BB61-DD47B491DAAB"
92 #define AMW0_GUID2              "431F16ED-0C2B-444C-B267-27DEB140CF9C"
93 #define WMID_GUID1              "6AF4F258-B401-42fd-BE91-3D4AC2D7C0D3"
94 #define WMID_GUID2              "95764E09-FB56-4e83-B31A-37761F60994A"
95
96 MODULE_ALIAS("wmi:67C3371D-95A3-4C37-BB61-DD47B491DAAB");
97 MODULE_ALIAS("wmi:6AF4F258-B401-42fd-BE91-3D4AC2D7C0D3");
98
99 /* Temporary workaround until the WMI sysfs interface goes in */
100 MODULE_ALIAS("dmi:*:*Acer*:*:");
101
102 /*
103  * Interface capability flags
104  */
105 #define ACER_CAP_MAILLED                (1<<0)
106 #define ACER_CAP_WIRELESS               (1<<1)
107 #define ACER_CAP_BLUETOOTH              (1<<2)
108 #define ACER_CAP_BRIGHTNESS             (1<<3)
109 #define ACER_CAP_THREEG                 (1<<4)
110 #define ACER_CAP_ANY                    (0xFFFFFFFF)
111
112 /*
113  * Interface type flags
114  */
115 enum interface_flags {
116         ACER_AMW0,
117         ACER_AMW0_V2,
118         ACER_WMID,
119 };
120
121 #define ACER_DEFAULT_WIRELESS  0
122 #define ACER_DEFAULT_BLUETOOTH 0
123 #define ACER_DEFAULT_MAILLED   0
124 #define ACER_DEFAULT_THREEG    0
125
126 static int max_brightness = 0xF;
127
128 static int wireless = -1;
129 static int bluetooth = -1;
130 static int mailled = -1;
131 static int brightness = -1;
132 static int threeg = -1;
133 static int force_series;
134
135 module_param(mailled, int, 0444);
136 module_param(wireless, int, 0444);
137 module_param(bluetooth, int, 0444);
138 module_param(brightness, int, 0444);
139 module_param(threeg, int, 0444);
140 module_param(force_series, int, 0444);
141 MODULE_PARM_DESC(wireless, "Set initial state of Wireless hardware");
142 MODULE_PARM_DESC(bluetooth, "Set initial state of Bluetooth hardware");
143 MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
144 MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
145 MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
146 MODULE_PARM_DESC(force_series, "Force a different laptop series");
147
148 struct acer_data {
149         int mailled;
150         int wireless;
151         int bluetooth;
152         int threeg;
153         int brightness;
154 };
155
156 struct acer_debug {
157         struct dentry *root;
158         struct dentry *devices;
159         u32 wmid_devices;
160 };
161
162 static struct rfkill *wireless_rfkill;
163 static struct rfkill *bluetooth_rfkill;
164
165 /* Each low-level interface must define at least some of the following */
166 struct wmi_interface {
167         /* The WMI device type */
168         u32 type;
169
170         /* The capabilities this interface provides */
171         u32 capability;
172
173         /* Private data for the current interface */
174         struct acer_data data;
175
176         /* debugfs entries associated with this interface */
177         struct acer_debug debug;
178 };
179
180 /* The static interface pointer, points to the currently detected interface */
181 static struct wmi_interface *interface;
182
183 /*
184  * Embedded Controller quirks
185  * Some laptops require us to directly access the EC to either enable or query
186  * features that are not available through WMI.
187  */
188
189 struct quirk_entry {
190         u8 wireless;
191         u8 mailled;
192         s8 brightness;
193         u8 bluetooth;
194 };
195
196 static struct quirk_entry *quirks;
197
198 static void set_quirks(void)
199 {
200         if (!interface)
201                 return;
202
203         if (quirks->mailled)
204                 interface->capability |= ACER_CAP_MAILLED;
205
206         if (quirks->brightness)
207                 interface->capability |= ACER_CAP_BRIGHTNESS;
208 }
209
210 static int dmi_matched(const struct dmi_system_id *dmi)
211 {
212         quirks = dmi->driver_data;
213         return 0;
214 }
215
216 static struct quirk_entry quirk_unknown = {
217 };
218
219 static struct quirk_entry quirk_acer_aspire_1520 = {
220         .brightness = -1,
221 };
222
223 static struct quirk_entry quirk_acer_travelmate_2490 = {
224         .mailled = 1,
225 };
226
227 /* This AMW0 laptop has no bluetooth */
228 static struct quirk_entry quirk_medion_md_98300 = {
229         .wireless = 1,
230 };
231
232 static struct quirk_entry quirk_fujitsu_amilo_li_1718 = {
233         .wireless = 2,
234 };
235
236 static struct dmi_system_id acer_quirks[] = {
237         {
238                 .callback = dmi_matched,
239                 .ident = "Acer Aspire 1360",
240                 .matches = {
241                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
242                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"),
243                 },
244                 .driver_data = &quirk_acer_aspire_1520,
245         },
246         {
247                 .callback = dmi_matched,
248                 .ident = "Acer Aspire 1520",
249                 .matches = {
250                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
251                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"),
252                 },
253                 .driver_data = &quirk_acer_aspire_1520,
254         },
255         {
256                 .callback = dmi_matched,
257                 .ident = "Acer Aspire 3100",
258                 .matches = {
259                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
260                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"),
261                 },
262                 .driver_data = &quirk_acer_travelmate_2490,
263         },
264         {
265                 .callback = dmi_matched,
266                 .ident = "Acer Aspire 3610",
267                 .matches = {
268                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
269                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3610"),
270                 },
271                 .driver_data = &quirk_acer_travelmate_2490,
272         },
273         {
274                 .callback = dmi_matched,
275                 .ident = "Acer Aspire 5100",
276                 .matches = {
277                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
278                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"),
279                 },
280                 .driver_data = &quirk_acer_travelmate_2490,
281         },
282         {
283                 .callback = dmi_matched,
284                 .ident = "Acer Aspire 5610",
285                 .matches = {
286                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
287                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5610"),
288                 },
289                 .driver_data = &quirk_acer_travelmate_2490,
290         },
291         {
292                 .callback = dmi_matched,
293                 .ident = "Acer Aspire 5630",
294                 .matches = {
295                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
296                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"),
297                 },
298                 .driver_data = &quirk_acer_travelmate_2490,
299         },
300         {
301                 .callback = dmi_matched,
302                 .ident = "Acer Aspire 5650",
303                 .matches = {
304                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
305                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"),
306                 },
307                 .driver_data = &quirk_acer_travelmate_2490,
308         },
309         {
310                 .callback = dmi_matched,
311                 .ident = "Acer Aspire 5680",
312                 .matches = {
313                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
314                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"),
315                 },
316                 .driver_data = &quirk_acer_travelmate_2490,
317         },
318         {
319                 .callback = dmi_matched,
320                 .ident = "Acer Aspire 9110",
321                 .matches = {
322                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
323                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 9110"),
324                 },
325                 .driver_data = &quirk_acer_travelmate_2490,
326         },
327         {
328                 .callback = dmi_matched,
329                 .ident = "Acer TravelMate 2490",
330                 .matches = {
331                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
332                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"),
333                 },
334                 .driver_data = &quirk_acer_travelmate_2490,
335         },
336         {
337                 .callback = dmi_matched,
338                 .ident = "Acer TravelMate 4200",
339                 .matches = {
340                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
341                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4200"),
342                 },
343                 .driver_data = &quirk_acer_travelmate_2490,
344         },
345         {
346                 .callback = dmi_matched,
347                 .ident = "Fujitsu Siemens Amilo Li 1718",
348                 .matches = {
349                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
350                         DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"),
351                 },
352                 .driver_data = &quirk_fujitsu_amilo_li_1718,
353         },
354         {
355                 .callback = dmi_matched,
356                 .ident = "Medion MD 98300",
357                 .matches = {
358                         DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
359                         DMI_MATCH(DMI_PRODUCT_NAME, "WAM2030"),
360                 },
361                 .driver_data = &quirk_medion_md_98300,
362         },
363         {}
364 };
365
366 /* Find which quirks are needed for a particular vendor/ model pair */
367 static void find_quirks(void)
368 {
369         if (!force_series) {
370                 dmi_check_system(acer_quirks);
371         } else if (force_series == 2490) {
372                 quirks = &quirk_acer_travelmate_2490;
373         }
374
375         if (quirks == NULL)
376                 quirks = &quirk_unknown;
377
378         set_quirks();
379 }
380
381 /*
382  * General interface convenience methods
383  */
384
385 static bool has_cap(u32 cap)
386 {
387         if ((interface->capability & cap) != 0)
388                 return 1;
389
390         return 0;
391 }
392
393 /*
394  * AMW0 (V1) interface
395  */
396 struct wmab_args {
397         u32 eax;
398         u32 ebx;
399         u32 ecx;
400         u32 edx;
401 };
402
403 struct wmab_ret {
404         u32 eax;
405         u32 ebx;
406         u32 ecx;
407         u32 edx;
408         u32 eex;
409 };
410
411 static acpi_status wmab_execute(struct wmab_args *regbuf,
412 struct acpi_buffer *result)
413 {
414         struct acpi_buffer input;
415         acpi_status status;
416         input.length = sizeof(struct wmab_args);
417         input.pointer = (u8 *)regbuf;
418
419         status = wmi_evaluate_method(AMW0_GUID1, 1, 1, &input, result);
420
421         return status;
422 }
423
424 static acpi_status AMW0_get_u32(u32 *value, u32 cap,
425 struct wmi_interface *iface)
426 {
427         int err;
428         u8 result;
429
430         switch (cap) {
431         case ACER_CAP_MAILLED:
432                 switch (quirks->mailled) {
433                 default:
434                         err = ec_read(0xA, &result);
435                         if (err)
436                                 return AE_ERROR;
437                         *value = (result >> 7) & 0x1;
438                         return AE_OK;
439                 }
440                 break;
441         case ACER_CAP_WIRELESS:
442                 switch (quirks->wireless) {
443                 case 1:
444                         err = ec_read(0x7B, &result);
445                         if (err)
446                                 return AE_ERROR;
447                         *value = result & 0x1;
448                         return AE_OK;
449                 case 2:
450                         err = ec_read(0x71, &result);
451                         if (err)
452                                 return AE_ERROR;
453                         *value = result & 0x1;
454                         return AE_OK;
455                 default:
456                         err = ec_read(0xA, &result);
457                         if (err)
458                                 return AE_ERROR;
459                         *value = (result >> 2) & 0x1;
460                         return AE_OK;
461                 }
462                 break;
463         case ACER_CAP_BLUETOOTH:
464                 switch (quirks->bluetooth) {
465                 default:
466                         err = ec_read(0xA, &result);
467                         if (err)
468                                 return AE_ERROR;
469                         *value = (result >> 4) & 0x1;
470                         return AE_OK;
471                 }
472                 break;
473         case ACER_CAP_BRIGHTNESS:
474                 switch (quirks->brightness) {
475                 default:
476                         err = ec_read(0x83, &result);
477                         if (err)
478                                 return AE_ERROR;
479                         *value = result;
480                         return AE_OK;
481                 }
482                 break;
483         default:
484                 return AE_BAD_ADDRESS;
485         }
486         return AE_OK;
487 }
488
489 static acpi_status AMW0_set_u32(u32 value, u32 cap, struct wmi_interface *iface)
490 {
491         struct wmab_args args;
492
493         args.eax = ACER_AMW0_WRITE;
494         args.ebx = value ? (1<<8) : 0;
495         args.ecx = args.edx = 0;
496
497         switch (cap) {
498         case ACER_CAP_MAILLED:
499                 if (value > 1)
500                         return AE_BAD_PARAMETER;
501                 args.ebx |= ACER_AMW0_MAILLED_MASK;
502                 break;
503         case ACER_CAP_WIRELESS:
504                 if (value > 1)
505                         return AE_BAD_PARAMETER;
506                 args.ebx |= ACER_AMW0_WIRELESS_MASK;
507                 break;
508         case ACER_CAP_BLUETOOTH:
509                 if (value > 1)
510                         return AE_BAD_PARAMETER;
511                 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
512                 break;
513         case ACER_CAP_BRIGHTNESS:
514                 if (value > max_brightness)
515                         return AE_BAD_PARAMETER;
516                 switch (quirks->brightness) {
517                 default:
518                         return ec_write(0x83, value);
519                         break;
520                 }
521         default:
522                 return AE_BAD_ADDRESS;
523         }
524
525         /* Actually do the set */
526         return wmab_execute(&args, NULL);
527 }
528
529 static acpi_status AMW0_find_mailled(void)
530 {
531         struct wmab_args args;
532         struct wmab_ret ret;
533         acpi_status status = AE_OK;
534         struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
535         union acpi_object *obj;
536
537         args.eax = 0x86;
538         args.ebx = args.ecx = args.edx = 0;
539
540         status = wmab_execute(&args, &out);
541         if (ACPI_FAILURE(status))
542                 return status;
543
544         obj = (union acpi_object *) out.pointer;
545         if (obj && obj->type == ACPI_TYPE_BUFFER &&
546         obj->buffer.length == sizeof(struct wmab_ret)) {
547                 ret = *((struct wmab_ret *) obj->buffer.pointer);
548         } else {
549                 return AE_ERROR;
550         }
551
552         if (ret.eex & 0x1)
553                 interface->capability |= ACER_CAP_MAILLED;
554
555         kfree(out.pointer);
556
557         return AE_OK;
558 }
559
560 static acpi_status AMW0_set_capabilities(void)
561 {
562         struct wmab_args args;
563         struct wmab_ret ret;
564         acpi_status status = AE_OK;
565         struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
566         union acpi_object *obj;
567
568         /*
569          * On laptops with this strange GUID (non Acer), normal probing doesn't
570          * work.
571          */
572         if (wmi_has_guid(AMW0_GUID2)) {
573                 interface->capability |= ACER_CAP_WIRELESS;
574                 return AE_OK;
575         }
576
577         args.eax = ACER_AMW0_WRITE;
578         args.ecx = args.edx = 0;
579
580         args.ebx = 0xa2 << 8;
581         args.ebx |= ACER_AMW0_WIRELESS_MASK;
582
583         status = wmab_execute(&args, &out);
584         if (ACPI_FAILURE(status))
585                 return status;
586
587         obj = (union acpi_object *) out.pointer;
588         if (obj && obj->type == ACPI_TYPE_BUFFER &&
589         obj->buffer.length == sizeof(struct wmab_ret)) {
590                 ret = *((struct wmab_ret *) obj->buffer.pointer);
591         } else {
592                 return AE_ERROR;
593         }
594
595         if (ret.eax & 0x1)
596                 interface->capability |= ACER_CAP_WIRELESS;
597
598         args.ebx = 2 << 8;
599         args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
600
601         status = wmab_execute(&args, &out);
602         if (ACPI_FAILURE(status))
603                 return status;
604
605         obj = (union acpi_object *) out.pointer;
606         if (obj && obj->type == ACPI_TYPE_BUFFER
607         && obj->buffer.length == sizeof(struct wmab_ret)) {
608                 ret = *((struct wmab_ret *) obj->buffer.pointer);
609         } else {
610                 return AE_ERROR;
611         }
612
613         if (ret.eax & 0x1)
614                 interface->capability |= ACER_CAP_BLUETOOTH;
615
616         kfree(out.pointer);
617
618         /*
619          * This appears to be safe to enable, since all Wistron based laptops
620          * appear to use the same EC register for brightness, even if they
621          * differ for wireless, etc
622          */
623         if (quirks->brightness >= 0)
624                 interface->capability |= ACER_CAP_BRIGHTNESS;
625
626         return AE_OK;
627 }
628
629 static struct wmi_interface AMW0_interface = {
630         .type = ACER_AMW0,
631 };
632
633 static struct wmi_interface AMW0_V2_interface = {
634         .type = ACER_AMW0_V2,
635 };
636
637 /*
638  * New interface (The WMID interface)
639  */
640 static acpi_status
641 WMI_execute_u32(u32 method_id, u32 in, u32 *out)
642 {
643         struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
644         struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
645         union acpi_object *obj;
646         u32 tmp;
647         acpi_status status;
648
649         status = wmi_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);
650
651         if (ACPI_FAILURE(status))
652                 return status;
653
654         obj = (union acpi_object *) result.pointer;
655         if (obj && obj->type == ACPI_TYPE_BUFFER &&
656                 obj->buffer.length == sizeof(u32)) {
657                 tmp = *((u32 *) obj->buffer.pointer);
658         } else {
659                 tmp = 0;
660         }
661
662         if (out)
663                 *out = tmp;
664
665         kfree(result.pointer);
666
667         return status;
668 }
669
670 static acpi_status WMID_get_u32(u32 *value, u32 cap,
671 struct wmi_interface *iface)
672 {
673         acpi_status status;
674         u8 tmp;
675         u32 result, method_id = 0;
676
677         switch (cap) {
678         case ACER_CAP_WIRELESS:
679                 method_id = ACER_WMID_GET_WIRELESS_METHODID;
680                 break;
681         case ACER_CAP_BLUETOOTH:
682                 method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
683                 break;
684         case ACER_CAP_BRIGHTNESS:
685                 method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
686                 break;
687         case ACER_CAP_THREEG:
688                 method_id = ACER_WMID_GET_THREEG_METHODID;
689                 break;
690         case ACER_CAP_MAILLED:
691                 if (quirks->mailled == 1) {
692                         ec_read(0x9f, &tmp);
693                         *value = tmp & 0x1;
694                         return 0;
695                 }
696         default:
697                 return AE_BAD_ADDRESS;
698         }
699         status = WMI_execute_u32(method_id, 0, &result);
700
701         if (ACPI_SUCCESS(status))
702                 *value = (u8)result;
703
704         return status;
705 }
706
707 static acpi_status WMID_set_u32(u32 value, u32 cap, struct wmi_interface *iface)
708 {
709         u32 method_id = 0;
710         char param;
711
712         switch (cap) {
713         case ACER_CAP_BRIGHTNESS:
714                 if (value > max_brightness)
715                         return AE_BAD_PARAMETER;
716                 method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
717                 break;
718         case ACER_CAP_WIRELESS:
719                 if (value > 1)
720                         return AE_BAD_PARAMETER;
721                 method_id = ACER_WMID_SET_WIRELESS_METHODID;
722                 break;
723         case ACER_CAP_BLUETOOTH:
724                 if (value > 1)
725                         return AE_BAD_PARAMETER;
726                 method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
727                 break;
728         case ACER_CAP_THREEG:
729                 if (value > 1)
730                         return AE_BAD_PARAMETER;
731                 method_id = ACER_WMID_SET_THREEG_METHODID;
732                 break;
733         case ACER_CAP_MAILLED:
734                 if (value > 1)
735                         return AE_BAD_PARAMETER;
736                 if (quirks->mailled == 1) {
737                         param = value ? 0x92 : 0x93;
738                         i8042_command(&param, 0x1059);
739                         return 0;
740                 }
741                 break;
742         default:
743                 return AE_BAD_ADDRESS;
744         }
745         return WMI_execute_u32(method_id, (u32)value, NULL);
746 }
747
748 static acpi_status WMID_set_capabilities(void)
749 {
750         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
751         union acpi_object *obj;
752         acpi_status status;
753         u32 devices;
754
755         status = wmi_query_block(WMID_GUID2, 1, &out);
756         if (ACPI_FAILURE(status))
757                 return status;
758
759         obj = (union acpi_object *) out.pointer;
760         if (obj && obj->type == ACPI_TYPE_BUFFER &&
761                 obj->buffer.length == sizeof(u32)) {
762                 devices = *((u32 *) obj->buffer.pointer);
763         } else {
764                 return AE_ERROR;
765         }
766
767         /* Not sure on the meaning of the relevant bits yet to detect these */
768         interface->capability |= ACER_CAP_WIRELESS;
769         interface->capability |= ACER_CAP_THREEG;
770
771         /* WMID always provides brightness methods */
772         interface->capability |= ACER_CAP_BRIGHTNESS;
773
774         if (devices & 0x10)
775                 interface->capability |= ACER_CAP_BLUETOOTH;
776
777         if (!(devices & 0x20))
778                 max_brightness = 0x9;
779
780         return status;
781 }
782
783 static struct wmi_interface wmid_interface = {
784         .type = ACER_WMID,
785 };
786
787 /*
788  * Generic Device (interface-independent)
789  */
790
791 static acpi_status get_u32(u32 *value, u32 cap)
792 {
793         acpi_status status = AE_BAD_ADDRESS;
794
795         switch (interface->type) {
796         case ACER_AMW0:
797                 status = AMW0_get_u32(value, cap, interface);
798                 break;
799         case ACER_AMW0_V2:
800                 if (cap == ACER_CAP_MAILLED) {
801                         status = AMW0_get_u32(value, cap, interface);
802                         break;
803                 }
804         case ACER_WMID:
805                 status = WMID_get_u32(value, cap, interface);
806                 break;
807         }
808
809         return status;
810 }
811
812 static acpi_status set_u32(u32 value, u32 cap)
813 {
814         acpi_status status;
815
816         if (interface->capability & cap) {
817                 switch (interface->type) {
818                 case ACER_AMW0:
819                         return AMW0_set_u32(value, cap, interface);
820                 case ACER_AMW0_V2:
821                         if (cap == ACER_CAP_MAILLED)
822                                 return AMW0_set_u32(value, cap, interface);
823
824                         /*
825                          * On some models, some WMID methods don't toggle
826                          * properly. For those cases, we want to run the AMW0
827                          * method afterwards to be certain we've really toggled
828                          * the device state.
829                          */
830                         if (cap == ACER_CAP_WIRELESS ||
831                                 cap == ACER_CAP_BLUETOOTH) {
832                                 status = WMID_set_u32(value, cap, interface);
833                                 if (ACPI_FAILURE(status))
834                                         return status;
835
836                                 return AMW0_set_u32(value, cap, interface);
837                         }
838                 case ACER_WMID:
839                         return WMID_set_u32(value, cap, interface);
840                 default:
841                         return AE_BAD_PARAMETER;
842                 }
843         }
844         return AE_BAD_PARAMETER;
845 }
846
847 static void __init acer_commandline_init(void)
848 {
849         /*
850          * These will all fail silently if the value given is invalid, or the
851          * capability isn't available on the given interface
852          */
853         set_u32(mailled, ACER_CAP_MAILLED);
854         set_u32(wireless, ACER_CAP_WIRELESS);
855         set_u32(bluetooth, ACER_CAP_BLUETOOTH);
856         set_u32(threeg, ACER_CAP_THREEG);
857         set_u32(brightness, ACER_CAP_BRIGHTNESS);
858 }
859
860 /*
861  * LED device (Mail LED only, no other LEDs known yet)
862  */
863 static void mail_led_set(struct led_classdev *led_cdev,
864 enum led_brightness value)
865 {
866         set_u32(value, ACER_CAP_MAILLED);
867 }
868
869 static struct led_classdev mail_led = {
870         .name = "acer-wmi::mail",
871         .brightness_set = mail_led_set,
872 };
873
874 static int __devinit acer_led_init(struct device *dev)
875 {
876         return led_classdev_register(dev, &mail_led);
877 }
878
879 static void acer_led_exit(void)
880 {
881         led_classdev_unregister(&mail_led);
882 }
883
884 /*
885  * Backlight device
886  */
887 static struct backlight_device *acer_backlight_device;
888
889 static int read_brightness(struct backlight_device *bd)
890 {
891         u32 value;
892         get_u32(&value, ACER_CAP_BRIGHTNESS);
893         return value;
894 }
895
896 static int update_bl_status(struct backlight_device *bd)
897 {
898         int intensity = bd->props.brightness;
899
900         if (bd->props.power != FB_BLANK_UNBLANK)
901                 intensity = 0;
902         if (bd->props.fb_blank != FB_BLANK_UNBLANK)
903                 intensity = 0;
904
905         set_u32(intensity, ACER_CAP_BRIGHTNESS);
906
907         return 0;
908 }
909
910 static struct backlight_ops acer_bl_ops = {
911         .get_brightness = read_brightness,
912         .update_status = update_bl_status,
913 };
914
915 static int __devinit acer_backlight_init(struct device *dev)
916 {
917         struct backlight_device *bd;
918
919         bd = backlight_device_register("acer-wmi", dev, NULL, &acer_bl_ops);
920         if (IS_ERR(bd)) {
921                 printk(ACER_ERR "Could not register Acer backlight device\n");
922                 acer_backlight_device = NULL;
923                 return PTR_ERR(bd);
924         }
925
926         acer_backlight_device = bd;
927
928         bd->props.power = FB_BLANK_UNBLANK;
929         bd->props.brightness = max_brightness;
930         bd->props.max_brightness = max_brightness;
931         backlight_update_status(bd);
932         return 0;
933 }
934
935 static void acer_backlight_exit(void)
936 {
937         backlight_device_unregister(acer_backlight_device);
938 }
939
940 /*
941  * Rfkill devices
942  */
943 static struct workqueue_struct *rfkill_workqueue;
944
945 static void acer_rfkill_update(struct work_struct *ignored);
946 static DECLARE_DELAYED_WORK(acer_rfkill_work, acer_rfkill_update);
947 static void acer_rfkill_update(struct work_struct *ignored)
948 {
949         u32 state;
950         acpi_status status;
951
952         status = get_u32(&state, ACER_CAP_WIRELESS);
953         if (ACPI_SUCCESS(status))
954                 rfkill_force_state(wireless_rfkill, state ?
955                         RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED);
956
957         if (has_cap(ACER_CAP_BLUETOOTH)) {
958                 status = get_u32(&state, ACER_CAP_BLUETOOTH);
959                 if (ACPI_SUCCESS(status))
960                         rfkill_force_state(bluetooth_rfkill, state ?
961                                 RFKILL_STATE_UNBLOCKED :
962                                 RFKILL_STATE_SOFT_BLOCKED);
963         }
964
965         queue_delayed_work(rfkill_workqueue, &acer_rfkill_work,
966                 round_jiffies_relative(HZ));
967 }
968
969 static int acer_rfkill_set(void *data, enum rfkill_state state)
970 {
971         acpi_status status;
972         u32 *cap = data;
973         status = set_u32((u32) (state == RFKILL_STATE_UNBLOCKED), *cap);
974         if (ACPI_FAILURE(status))
975                 return -ENODEV;
976         return 0;
977 }
978
979 static struct rfkill * acer_rfkill_register(struct device *dev,
980 enum rfkill_type type, char *name, u32 cap)
981 {
982         int err;
983         u32 state;
984         u32 *data;
985         struct rfkill *rfkill_dev;
986
987         rfkill_dev = rfkill_allocate(dev, type);
988         if (!rfkill_dev)
989                 return ERR_PTR(-ENOMEM);
990         rfkill_dev->name = name;
991         get_u32(&state, cap);
992         rfkill_dev->state = state ? RFKILL_STATE_UNBLOCKED :
993                 RFKILL_STATE_SOFT_BLOCKED;
994         data = kzalloc(sizeof(u32), GFP_KERNEL);
995         if (!data) {
996                 rfkill_free(rfkill_dev);
997                 return ERR_PTR(-ENOMEM);
998         }
999         *data = cap;
1000         rfkill_dev->data = data;
1001         rfkill_dev->toggle_radio = acer_rfkill_set;
1002         rfkill_dev->user_claim_unsupported = 1;
1003
1004         err = rfkill_register(rfkill_dev);
1005         if (err) {
1006                 kfree(rfkill_dev->data);
1007                 rfkill_free(rfkill_dev);
1008                 return ERR_PTR(err);
1009         }
1010         return rfkill_dev;
1011 }
1012
1013 static int acer_rfkill_init(struct device *dev)
1014 {
1015         wireless_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_WLAN,
1016                 "acer-wireless", ACER_CAP_WIRELESS);
1017         if (IS_ERR(wireless_rfkill))
1018                 return PTR_ERR(wireless_rfkill);
1019
1020         if (has_cap(ACER_CAP_BLUETOOTH)) {
1021                 bluetooth_rfkill = acer_rfkill_register(dev,
1022                         RFKILL_TYPE_BLUETOOTH, "acer-bluetooth",
1023                         ACER_CAP_BLUETOOTH);
1024                 if (IS_ERR(bluetooth_rfkill)) {
1025                         kfree(wireless_rfkill->data);
1026                         rfkill_unregister(wireless_rfkill);
1027                         return PTR_ERR(bluetooth_rfkill);
1028                 }
1029         }
1030
1031         rfkill_workqueue = create_singlethread_workqueue("rfkill_workqueue");
1032         if (!rfkill_workqueue) {
1033                 if (has_cap(ACER_CAP_BLUETOOTH)) {
1034                         kfree(bluetooth_rfkill->data);
1035                         rfkill_unregister(bluetooth_rfkill);
1036                 }
1037                 kfree(wireless_rfkill->data);
1038                 rfkill_unregister(wireless_rfkill);
1039                 return -ENOMEM;
1040         }
1041         queue_delayed_work(rfkill_workqueue, &acer_rfkill_work, HZ);
1042
1043         return 0;
1044 }
1045
1046 static void acer_rfkill_exit(void)
1047 {
1048         cancel_delayed_work_sync(&acer_rfkill_work);
1049         destroy_workqueue(rfkill_workqueue);
1050         kfree(wireless_rfkill->data);
1051         rfkill_unregister(wireless_rfkill);
1052         if (has_cap(ACER_CAP_BLUETOOTH)) {
1053                 kfree(wireless_rfkill->data);
1054                 rfkill_unregister(bluetooth_rfkill);
1055         }
1056         return;
1057 }
1058
1059 /*
1060  * Read/ write bool sysfs macro
1061  */
1062 #define show_set_bool(value, cap) \
1063 static ssize_t \
1064 show_bool_##value(struct device *dev, struct device_attribute *attr, \
1065         char *buf) \
1066 { \
1067         u32 result; \
1068         acpi_status status = get_u32(&result, cap); \
1069         if (ACPI_SUCCESS(status)) \
1070                 return sprintf(buf, "%u\n", result); \
1071         return sprintf(buf, "Read error\n"); \
1072 } \
1073 \
1074 static ssize_t \
1075 set_bool_##value(struct device *dev, struct device_attribute *attr, \
1076         const char *buf, size_t count) \
1077 { \
1078         u32 tmp = simple_strtoul(buf, NULL, 10); \
1079         acpi_status status = set_u32(tmp, cap); \
1080                 if (ACPI_FAILURE(status)) \
1081                         return -EINVAL; \
1082         return count; \
1083 } \
1084 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
1085         show_bool_##value, set_bool_##value);
1086
1087 show_set_bool(wireless, ACER_CAP_WIRELESS);
1088 show_set_bool(bluetooth, ACER_CAP_BLUETOOTH);
1089 show_set_bool(threeg, ACER_CAP_THREEG);
1090
1091 /*
1092  * Read interface sysfs macro
1093  */
1094 static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
1095         char *buf)
1096 {
1097         switch (interface->type) {
1098         case ACER_AMW0:
1099                 return sprintf(buf, "AMW0\n");
1100         case ACER_AMW0_V2:
1101                 return sprintf(buf, "AMW0 v2\n");
1102         case ACER_WMID:
1103                 return sprintf(buf, "WMID\n");
1104         default:
1105                 return sprintf(buf, "Error!\n");
1106         }
1107 }
1108
1109 static DEVICE_ATTR(interface, S_IWUGO | S_IRUGO | S_IWUSR,
1110         show_interface, NULL);
1111
1112 /*
1113  * debugfs functions
1114  */
1115 static u32 get_wmid_devices(void)
1116 {
1117         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1118         union acpi_object *obj;
1119         acpi_status status;
1120
1121         status = wmi_query_block(WMID_GUID2, 1, &out);
1122         if (ACPI_FAILURE(status))
1123                 return 0;
1124
1125         obj = (union acpi_object *) out.pointer;
1126         if (obj && obj->type == ACPI_TYPE_BUFFER &&
1127                 obj->buffer.length == sizeof(u32)) {
1128                 return *((u32 *) obj->buffer.pointer);
1129         } else {
1130                 return 0;
1131         }
1132 }
1133
1134 /*
1135  * Platform device
1136  */
1137 static int __devinit acer_platform_probe(struct platform_device *device)
1138 {
1139         int err;
1140
1141         if (has_cap(ACER_CAP_MAILLED)) {
1142                 err = acer_led_init(&device->dev);
1143                 if (err)
1144                         goto error_mailled;
1145         }
1146
1147         if (has_cap(ACER_CAP_BRIGHTNESS)) {
1148                 err = acer_backlight_init(&device->dev);
1149                 if (err)
1150                         goto error_brightness;
1151         }
1152
1153         err = acer_rfkill_init(&device->dev);
1154
1155         return err;
1156
1157 error_brightness:
1158         acer_led_exit();
1159 error_mailled:
1160         return err;
1161 }
1162
1163 static int acer_platform_remove(struct platform_device *device)
1164 {
1165         if (has_cap(ACER_CAP_MAILLED))
1166                 acer_led_exit();
1167         if (has_cap(ACER_CAP_BRIGHTNESS))
1168                 acer_backlight_exit();
1169
1170         acer_rfkill_exit();
1171         return 0;
1172 }
1173
1174 static int acer_platform_suspend(struct platform_device *dev,
1175 pm_message_t state)
1176 {
1177         u32 value;
1178         struct acer_data *data = &interface->data;
1179
1180         if (!data)
1181                 return -ENOMEM;
1182
1183         if (has_cap(ACER_CAP_WIRELESS)) {
1184                 get_u32(&value, ACER_CAP_WIRELESS);
1185                 data->wireless = value;
1186         }
1187
1188         if (has_cap(ACER_CAP_BLUETOOTH)) {
1189                 get_u32(&value, ACER_CAP_BLUETOOTH);
1190                 data->bluetooth = value;
1191         }
1192
1193         if (has_cap(ACER_CAP_MAILLED)) {
1194                 get_u32(&value, ACER_CAP_MAILLED);
1195                 data->mailled = value;
1196         }
1197
1198         if (has_cap(ACER_CAP_BRIGHTNESS)) {
1199                 get_u32(&value, ACER_CAP_BRIGHTNESS);
1200                 data->brightness = value;
1201         }
1202
1203         return 0;
1204 }
1205
1206 static int acer_platform_resume(struct platform_device *device)
1207 {
1208         struct acer_data *data = &interface->data;
1209
1210         if (!data)
1211                 return -ENOMEM;
1212
1213         if (has_cap(ACER_CAP_WIRELESS))
1214                 set_u32(data->wireless, ACER_CAP_WIRELESS);
1215
1216         if (has_cap(ACER_CAP_BLUETOOTH))
1217                 set_u32(data->bluetooth, ACER_CAP_BLUETOOTH);
1218
1219         if (has_cap(ACER_CAP_THREEG))
1220                 set_u32(data->threeg, ACER_CAP_THREEG);
1221
1222         if (has_cap(ACER_CAP_MAILLED))
1223                 set_u32(data->mailled, ACER_CAP_MAILLED);
1224
1225         if (has_cap(ACER_CAP_BRIGHTNESS))
1226                 set_u32(data->brightness, ACER_CAP_BRIGHTNESS);
1227
1228         return 0;
1229 }
1230
1231 static struct platform_driver acer_platform_driver = {
1232         .driver = {
1233                 .name = "acer-wmi",
1234                 .owner = THIS_MODULE,
1235         },
1236         .probe = acer_platform_probe,
1237         .remove = acer_platform_remove,
1238         .suspend = acer_platform_suspend,
1239         .resume = acer_platform_resume,
1240 };
1241
1242 static struct platform_device *acer_platform_device;
1243
1244 static int remove_sysfs(struct platform_device *device)
1245 {
1246         if (has_cap(ACER_CAP_WIRELESS))
1247                 device_remove_file(&device->dev, &dev_attr_wireless);
1248
1249         if (has_cap(ACER_CAP_BLUETOOTH))
1250                 device_remove_file(&device->dev, &dev_attr_bluetooth);
1251
1252         if (has_cap(ACER_CAP_THREEG))
1253                 device_remove_file(&device->dev, &dev_attr_threeg);
1254
1255         device_remove_file(&device->dev, &dev_attr_interface);
1256
1257         return 0;
1258 }
1259
1260 static int create_sysfs(void)
1261 {
1262         int retval = -ENOMEM;
1263
1264         if (has_cap(ACER_CAP_WIRELESS)) {
1265                 retval = device_create_file(&acer_platform_device->dev,
1266                         &dev_attr_wireless);
1267                 if (retval)
1268                         goto error_sysfs;
1269         }
1270
1271         if (has_cap(ACER_CAP_BLUETOOTH)) {
1272                 retval = device_create_file(&acer_platform_device->dev,
1273                         &dev_attr_bluetooth);
1274                 if (retval)
1275                         goto error_sysfs;
1276         }
1277
1278         if (has_cap(ACER_CAP_THREEG)) {
1279                 retval = device_create_file(&acer_platform_device->dev,
1280                         &dev_attr_threeg);
1281                 if (retval)
1282                         goto error_sysfs;
1283         }
1284
1285         retval = device_create_file(&acer_platform_device->dev,
1286                 &dev_attr_interface);
1287         if (retval)
1288                 goto error_sysfs;
1289
1290         return 0;
1291
1292 error_sysfs:
1293                 remove_sysfs(acer_platform_device);
1294         return retval;
1295 }
1296
1297 static void remove_debugfs(void)
1298 {
1299         debugfs_remove(interface->debug.devices);
1300         debugfs_remove(interface->debug.root);
1301 }
1302
1303 static int create_debugfs(void)
1304 {
1305         interface->debug.root = debugfs_create_dir("acer-wmi", NULL);
1306         if (!interface->debug.root) {
1307                 printk(ACER_ERR "Failed to create debugfs directory");
1308                 return -ENOMEM;
1309         }
1310
1311         interface->debug.devices = debugfs_create_u32("devices", S_IRUGO,
1312                                         interface->debug.root,
1313                                         &interface->debug.wmid_devices);
1314         if (!interface->debug.devices)
1315                 goto error_debugfs;
1316
1317         return 0;
1318
1319 error_debugfs:
1320         remove_debugfs();
1321         return -ENOMEM;
1322 }
1323
1324 static int __init acer_wmi_init(void)
1325 {
1326         int err;
1327
1328         printk(ACER_INFO "Acer Laptop ACPI-WMI Extras\n");
1329
1330         find_quirks();
1331
1332         /*
1333          * Detect which ACPI-WMI interface we're using.
1334          */
1335         if (wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1336                 interface = &AMW0_V2_interface;
1337
1338         if (!wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1339                 interface = &wmid_interface;
1340
1341         if (wmi_has_guid(WMID_GUID2) && interface) {
1342                 if (ACPI_FAILURE(WMID_set_capabilities())) {
1343                         printk(ACER_ERR "Unable to detect available WMID "
1344                                         "devices\n");
1345                         return -ENODEV;
1346                 }
1347         } else if (!wmi_has_guid(WMID_GUID2) && interface) {
1348                 printk(ACER_ERR "No WMID device detection method found\n");
1349                 return -ENODEV;
1350         }
1351
1352         if (wmi_has_guid(AMW0_GUID1) && !wmi_has_guid(WMID_GUID1)) {
1353                 interface = &AMW0_interface;
1354
1355                 if (ACPI_FAILURE(AMW0_set_capabilities())) {
1356                         printk(ACER_ERR "Unable to detect available AMW0 "
1357                                         "devices\n");
1358                         return -ENODEV;
1359                 }
1360         }
1361
1362         if (wmi_has_guid(AMW0_GUID1))
1363                 AMW0_find_mailled();
1364
1365         if (!interface) {
1366                 printk(ACER_ERR "No or unsupported WMI interface, unable to "
1367                                 "load\n");
1368                 return -ENODEV;
1369         }
1370
1371         set_quirks();
1372
1373         if (platform_driver_register(&acer_platform_driver)) {
1374                 printk(ACER_ERR "Unable to register platform driver.\n");
1375                 goto error_platform_register;
1376         }
1377         acer_platform_device = platform_device_alloc("acer-wmi", -1);
1378         platform_device_add(acer_platform_device);
1379
1380         err = create_sysfs();
1381         if (err)
1382                 return err;
1383
1384         if (wmi_has_guid(WMID_GUID2)) {
1385                 interface->debug.wmid_devices = get_wmid_devices();
1386                 err = create_debugfs();
1387                 if (err)
1388                         return err;
1389         }
1390
1391         /* Override any initial settings with values from the commandline */
1392         acer_commandline_init();
1393
1394         return 0;
1395
1396 error_platform_register:
1397         return -ENODEV;
1398 }
1399
1400 static void __exit acer_wmi_exit(void)
1401 {
1402         remove_sysfs(acer_platform_device);
1403         remove_debugfs();
1404         platform_device_del(acer_platform_device);
1405         platform_driver_unregister(&acer_platform_driver);
1406
1407         printk(ACER_INFO "Acer Laptop WMI Extras unloaded\n");
1408         return;
1409 }
1410
1411 module_init(acer_wmi_init);
1412 module_exit(acer_wmi_exit);