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