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