b253e219297e924ab63de51ebbd29ea6759aede3
[~shefty/rdma-dev.git] / drivers / platform / x86 / acer-wmi.c
1 /*
2  *  Acer WMI Laptop Extras
3  *
4  *  Copyright (C) 2007-2009     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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/rfkill.h>
39 #include <linux/workqueue.h>
40 #include <linux/debugfs.h>
41 #include <linux/slab.h>
42 #include <linux/input.h>
43 #include <linux/input/sparse-keymap.h>
44
45 #include <acpi/acpi_drivers.h>
46
47 MODULE_AUTHOR("Carlos Corbacho");
48 MODULE_DESCRIPTION("Acer Laptop WMI Extras Driver");
49 MODULE_LICENSE("GPL");
50
51 /*
52  * Magic Number
53  * Meaning is unknown - this number is required for writing to ACPI for AMW0
54  * (it's also used in acerhk when directly accessing the BIOS)
55  */
56 #define ACER_AMW0_WRITE 0x9610
57
58 /*
59  * Bit masks for the AMW0 interface
60  */
61 #define ACER_AMW0_WIRELESS_MASK  0x35
62 #define ACER_AMW0_BLUETOOTH_MASK 0x34
63 #define ACER_AMW0_MAILLED_MASK   0x31
64
65 /*
66  * Method IDs for WMID interface
67  */
68 #define ACER_WMID_GET_WIRELESS_METHODID         1
69 #define ACER_WMID_GET_BLUETOOTH_METHODID        2
70 #define ACER_WMID_GET_BRIGHTNESS_METHODID       3
71 #define ACER_WMID_SET_WIRELESS_METHODID         4
72 #define ACER_WMID_SET_BLUETOOTH_METHODID        5
73 #define ACER_WMID_SET_BRIGHTNESS_METHODID       6
74 #define ACER_WMID_GET_THREEG_METHODID           10
75 #define ACER_WMID_SET_THREEG_METHODID           11
76
77 /*
78  * Acer ACPI method GUIDs
79  */
80 #define AMW0_GUID1              "67C3371D-95A3-4C37-BB61-DD47B491DAAB"
81 #define AMW0_GUID2              "431F16ED-0C2B-444C-B267-27DEB140CF9C"
82 #define WMID_GUID1              "6AF4F258-B401-42FD-BE91-3D4AC2D7C0D3"
83 #define WMID_GUID2              "95764E09-FB56-4E83-B31A-37761F60994A"
84 #define WMID_GUID3              "61EF69EA-865C-4BC3-A502-A0DEBA0CB531"
85
86 /*
87  * Acer ACPI event GUIDs
88  */
89 #define ACERWMID_EVENT_GUID "676AA15E-6A47-4D9F-A2CC-1E6D18D14026"
90
91 MODULE_ALIAS("wmi:67C3371D-95A3-4C37-BB61-DD47B491DAAB");
92 MODULE_ALIAS("wmi:6AF4F258-B401-42FD-BE91-3D4AC2D7C0D3");
93 MODULE_ALIAS("wmi:676AA15E-6A47-4D9F-A2CC-1E6D18D14026");
94
95 enum acer_wmi_event_ids {
96         WMID_HOTKEY_EVENT = 0x1,
97 };
98
99 static const struct key_entry acer_wmi_keymap[] = {
100         {KE_KEY, 0x01, {KEY_WLAN} },     /* WiFi */
101         {KE_KEY, 0x03, {KEY_WLAN} },     /* WiFi */
102         {KE_KEY, 0x04, {KEY_WLAN} },     /* WiFi */
103         {KE_KEY, 0x12, {KEY_BLUETOOTH} },       /* BT */
104         {KE_KEY, 0x21, {KEY_PROG1} },    /* Backup */
105         {KE_KEY, 0x22, {KEY_PROG2} },    /* Arcade */
106         {KE_KEY, 0x23, {KEY_PROG3} },    /* P_Key */
107         {KE_KEY, 0x24, {KEY_PROG4} },    /* Social networking_Key */
108         {KE_KEY, 0x29, {KEY_PROG3} },    /* P_Key for TM8372 */
109         {KE_IGNORE, 0x41, {KEY_MUTE} },
110         {KE_IGNORE, 0x42, {KEY_PREVIOUSSONG} },
111         {KE_IGNORE, 0x4d, {KEY_PREVIOUSSONG} },
112         {KE_IGNORE, 0x43, {KEY_NEXTSONG} },
113         {KE_IGNORE, 0x4e, {KEY_NEXTSONG} },
114         {KE_IGNORE, 0x44, {KEY_PLAYPAUSE} },
115         {KE_IGNORE, 0x4f, {KEY_PLAYPAUSE} },
116         {KE_IGNORE, 0x45, {KEY_STOP} },
117         {KE_IGNORE, 0x50, {KEY_STOP} },
118         {KE_IGNORE, 0x48, {KEY_VOLUMEUP} },
119         {KE_IGNORE, 0x49, {KEY_VOLUMEDOWN} },
120         {KE_IGNORE, 0x4a, {KEY_VOLUMEDOWN} },
121         {KE_IGNORE, 0x61, {KEY_SWITCHVIDEOMODE} },
122         {KE_IGNORE, 0x62, {KEY_BRIGHTNESSUP} },
123         {KE_IGNORE, 0x63, {KEY_BRIGHTNESSDOWN} },
124         {KE_KEY, 0x64, {KEY_SWITCHVIDEOMODE} }, /* Display Switch */
125         {KE_IGNORE, 0x81, {KEY_SLEEP} },
126         {KE_KEY, 0x82, {KEY_TOUCHPAD_TOGGLE} }, /* Touch Pad On/Off */
127         {KE_IGNORE, 0x83, {KEY_TOUCHPAD_TOGGLE} },
128         {KE_END, 0}
129 };
130
131 static struct input_dev *acer_wmi_input_dev;
132
133 struct event_return_value {
134         u8 function;
135         u8 key_num;
136         u16 device_state;
137         u32 reserved;
138 } __attribute__((packed));
139
140 /*
141  * GUID3 Get Device Status device flags
142  */
143 #define ACER_WMID3_GDS_WIRELESS         (1<<0)  /* WiFi */
144 #define ACER_WMID3_GDS_THREEG           (1<<6)  /* 3G */
145 #define ACER_WMID3_GDS_WIMAX            (1<<7)  /* WiMAX */
146 #define ACER_WMID3_GDS_BLUETOOTH        (1<<11) /* BT */
147
148 struct lm_input_params {
149         u8 function_num;        /* Function Number */
150         u16 commun_devices;     /* Communication type devices default status */
151         u16 devices;            /* Other type devices default status */
152         u8 lm_status;           /* Launch Manager Status */
153         u16 reserved;
154 } __attribute__((packed));
155
156 struct lm_return_value {
157         u8 error_code;          /* Error Code */
158         u8 ec_return_value;     /* EC Return Value */
159         u16 reserved;
160 } __attribute__((packed));
161
162 struct wmid3_gds_set_input_param {     /* Set Device Status input parameter */
163         u8 function_num;        /* Function Number */
164         u8 hotkey_number;       /* Hotkey Number */
165         u16 devices;            /* Set Device */
166         u8 volume_value;        /* Volume Value */
167 } __attribute__((packed));
168
169 struct wmid3_gds_get_input_param {     /* Get Device Status input parameter */
170         u8 function_num;        /* Function Number */
171         u8 hotkey_number;       /* Hotkey Number */
172         u16 devices;            /* Get Device */
173 } __attribute__((packed));
174
175 struct wmid3_gds_return_value { /* Get Device Status return value*/
176         u8 error_code;          /* Error Code */
177         u8 ec_return_value;     /* EC Return Value */
178         u16 devices;            /* Current Device Status */
179         u32 reserved;
180 } __attribute__((packed));
181
182 struct hotkey_function_type_aa {
183         u8 type;
184         u8 length;
185         u16 handle;
186         u16 commun_func_bitmap;
187         u16 application_func_bitmap;
188         u16 media_func_bitmap;
189         u16 display_func_bitmap;
190         u16 others_func_bitmap;
191         u8 commun_fn_key_number;
192 } __attribute__((packed));
193
194 /*
195  * Interface capability flags
196  */
197 #define ACER_CAP_MAILLED                (1<<0)
198 #define ACER_CAP_WIRELESS               (1<<1)
199 #define ACER_CAP_BLUETOOTH              (1<<2)
200 #define ACER_CAP_BRIGHTNESS             (1<<3)
201 #define ACER_CAP_THREEG                 (1<<4)
202 #define ACER_CAP_ANY                    (0xFFFFFFFF)
203
204 /*
205  * Interface type flags
206  */
207 enum interface_flags {
208         ACER_AMW0,
209         ACER_AMW0_V2,
210         ACER_WMID,
211         ACER_WMID_v2,
212 };
213
214 #define ACER_DEFAULT_WIRELESS  0
215 #define ACER_DEFAULT_BLUETOOTH 0
216 #define ACER_DEFAULT_MAILLED   0
217 #define ACER_DEFAULT_THREEG    0
218
219 static int max_brightness = 0xF;
220
221 static int mailled = -1;
222 static int brightness = -1;
223 static int threeg = -1;
224 static int force_series;
225 static bool ec_raw_mode;
226 static bool has_type_aa;
227 static u16 commun_func_bitmap;
228 static u8 commun_fn_key_number;
229
230 module_param(mailled, int, 0444);
231 module_param(brightness, int, 0444);
232 module_param(threeg, int, 0444);
233 module_param(force_series, int, 0444);
234 module_param(ec_raw_mode, bool, 0444);
235 MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
236 MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
237 MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
238 MODULE_PARM_DESC(force_series, "Force a different laptop series");
239 MODULE_PARM_DESC(ec_raw_mode, "Enable EC raw mode");
240
241 struct acer_data {
242         int mailled;
243         int threeg;
244         int brightness;
245 };
246
247 struct acer_debug {
248         struct dentry *root;
249         struct dentry *devices;
250         u32 wmid_devices;
251 };
252
253 static struct rfkill *wireless_rfkill;
254 static struct rfkill *bluetooth_rfkill;
255 static struct rfkill *threeg_rfkill;
256 static bool rfkill_inited;
257
258 /* Each low-level interface must define at least some of the following */
259 struct wmi_interface {
260         /* The WMI device type */
261         u32 type;
262
263         /* The capabilities this interface provides */
264         u32 capability;
265
266         /* Private data for the current interface */
267         struct acer_data data;
268
269         /* debugfs entries associated with this interface */
270         struct acer_debug debug;
271 };
272
273 /* The static interface pointer, points to the currently detected interface */
274 static struct wmi_interface *interface;
275
276 /*
277  * Embedded Controller quirks
278  * Some laptops require us to directly access the EC to either enable or query
279  * features that are not available through WMI.
280  */
281
282 struct quirk_entry {
283         u8 wireless;
284         u8 mailled;
285         s8 brightness;
286         u8 bluetooth;
287 };
288
289 static struct quirk_entry *quirks;
290
291 static void set_quirks(void)
292 {
293         if (!interface)
294                 return;
295
296         if (quirks->mailled)
297                 interface->capability |= ACER_CAP_MAILLED;
298
299         if (quirks->brightness)
300                 interface->capability |= ACER_CAP_BRIGHTNESS;
301 }
302
303 static int dmi_matched(const struct dmi_system_id *dmi)
304 {
305         quirks = dmi->driver_data;
306         return 1;
307 }
308
309 static struct quirk_entry quirk_unknown = {
310 };
311
312 static struct quirk_entry quirk_acer_aspire_1520 = {
313         .brightness = -1,
314 };
315
316 static struct quirk_entry quirk_acer_travelmate_2490 = {
317         .mailled = 1,
318 };
319
320 /* This AMW0 laptop has no bluetooth */
321 static struct quirk_entry quirk_medion_md_98300 = {
322         .wireless = 1,
323 };
324
325 static struct quirk_entry quirk_fujitsu_amilo_li_1718 = {
326         .wireless = 2,
327 };
328
329 static struct quirk_entry quirk_lenovo_ideapad_s205 = {
330         .wireless = 3,
331 };
332
333 /* The Aspire One has a dummy ACPI-WMI interface - disable it */
334 static struct dmi_system_id __devinitdata acer_blacklist[] = {
335         {
336                 .ident = "Acer Aspire One (SSD)",
337                 .matches = {
338                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
339                         DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
340                 },
341         },
342         {
343                 .ident = "Acer Aspire One (HDD)",
344                 .matches = {
345                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
346                         DMI_MATCH(DMI_PRODUCT_NAME, "AOA150"),
347                 },
348         },
349         {}
350 };
351
352 static struct dmi_system_id acer_quirks[] = {
353         {
354                 .callback = dmi_matched,
355                 .ident = "Acer Aspire 1360",
356                 .matches = {
357                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
358                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"),
359                 },
360                 .driver_data = &quirk_acer_aspire_1520,
361         },
362         {
363                 .callback = dmi_matched,
364                 .ident = "Acer Aspire 1520",
365                 .matches = {
366                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
367                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"),
368                 },
369                 .driver_data = &quirk_acer_aspire_1520,
370         },
371         {
372                 .callback = dmi_matched,
373                 .ident = "Acer Aspire 3100",
374                 .matches = {
375                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
376                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"),
377                 },
378                 .driver_data = &quirk_acer_travelmate_2490,
379         },
380         {
381                 .callback = dmi_matched,
382                 .ident = "Acer Aspire 3610",
383                 .matches = {
384                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
385                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3610"),
386                 },
387                 .driver_data = &quirk_acer_travelmate_2490,
388         },
389         {
390                 .callback = dmi_matched,
391                 .ident = "Acer Aspire 5100",
392                 .matches = {
393                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
394                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"),
395                 },
396                 .driver_data = &quirk_acer_travelmate_2490,
397         },
398         {
399                 .callback = dmi_matched,
400                 .ident = "Acer Aspire 5610",
401                 .matches = {
402                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
403                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5610"),
404                 },
405                 .driver_data = &quirk_acer_travelmate_2490,
406         },
407         {
408                 .callback = dmi_matched,
409                 .ident = "Acer Aspire 5630",
410                 .matches = {
411                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
412                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"),
413                 },
414                 .driver_data = &quirk_acer_travelmate_2490,
415         },
416         {
417                 .callback = dmi_matched,
418                 .ident = "Acer Aspire 5650",
419                 .matches = {
420                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
421                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"),
422                 },
423                 .driver_data = &quirk_acer_travelmate_2490,
424         },
425         {
426                 .callback = dmi_matched,
427                 .ident = "Acer Aspire 5680",
428                 .matches = {
429                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
430                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"),
431                 },
432                 .driver_data = &quirk_acer_travelmate_2490,
433         },
434         {
435                 .callback = dmi_matched,
436                 .ident = "Acer Aspire 9110",
437                 .matches = {
438                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
439                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 9110"),
440                 },
441                 .driver_data = &quirk_acer_travelmate_2490,
442         },
443         {
444                 .callback = dmi_matched,
445                 .ident = "Acer TravelMate 2490",
446                 .matches = {
447                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
448                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"),
449                 },
450                 .driver_data = &quirk_acer_travelmate_2490,
451         },
452         {
453                 .callback = dmi_matched,
454                 .ident = "Acer TravelMate 4200",
455                 .matches = {
456                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
457                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4200"),
458                 },
459                 .driver_data = &quirk_acer_travelmate_2490,
460         },
461         {
462                 .callback = dmi_matched,
463                 .ident = "Fujitsu Siemens Amilo Li 1718",
464                 .matches = {
465                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
466                         DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"),
467                 },
468                 .driver_data = &quirk_fujitsu_amilo_li_1718,
469         },
470         {
471                 .callback = dmi_matched,
472                 .ident = "Medion MD 98300",
473                 .matches = {
474                         DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
475                         DMI_MATCH(DMI_PRODUCT_NAME, "WAM2030"),
476                 },
477                 .driver_data = &quirk_medion_md_98300,
478         },
479         {
480                 .callback = dmi_matched,
481                 .ident = "Lenovo Ideapad S205",
482                 .matches = {
483                         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
484                         DMI_MATCH(DMI_PRODUCT_NAME, "10382LG"),
485                 },
486                 .driver_data = &quirk_lenovo_ideapad_s205,
487         },
488         {
489                 .callback = dmi_matched,
490                 .ident = "Lenovo Ideapad S205 (Brazos)",
491                 .matches = {
492                         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
493                         DMI_MATCH(DMI_PRODUCT_NAME, "Brazos"),
494                 },
495                 .driver_data = &quirk_lenovo_ideapad_s205,
496         },
497         {
498                 .callback = dmi_matched,
499                 .ident = "Lenovo 3000 N200",
500                 .matches = {
501                         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
502                         DMI_MATCH(DMI_PRODUCT_NAME, "0687A31"),
503                 },
504                 .driver_data = &quirk_fujitsu_amilo_li_1718,
505         },
506         {}
507 };
508
509 /* Find which quirks are needed for a particular vendor/ model pair */
510 static void find_quirks(void)
511 {
512         if (!force_series) {
513                 dmi_check_system(acer_quirks);
514         } else if (force_series == 2490) {
515                 quirks = &quirk_acer_travelmate_2490;
516         }
517
518         if (quirks == NULL)
519                 quirks = &quirk_unknown;
520
521         set_quirks();
522 }
523
524 /*
525  * General interface convenience methods
526  */
527
528 static bool has_cap(u32 cap)
529 {
530         if ((interface->capability & cap) != 0)
531                 return 1;
532
533         return 0;
534 }
535
536 /*
537  * AMW0 (V1) interface
538  */
539 struct wmab_args {
540         u32 eax;
541         u32 ebx;
542         u32 ecx;
543         u32 edx;
544 };
545
546 struct wmab_ret {
547         u32 eax;
548         u32 ebx;
549         u32 ecx;
550         u32 edx;
551         u32 eex;
552 };
553
554 static acpi_status wmab_execute(struct wmab_args *regbuf,
555 struct acpi_buffer *result)
556 {
557         struct acpi_buffer input;
558         acpi_status status;
559         input.length = sizeof(struct wmab_args);
560         input.pointer = (u8 *)regbuf;
561
562         status = wmi_evaluate_method(AMW0_GUID1, 1, 1, &input, result);
563
564         return status;
565 }
566
567 static acpi_status AMW0_get_u32(u32 *value, u32 cap)
568 {
569         int err;
570         u8 result;
571
572         switch (cap) {
573         case ACER_CAP_MAILLED:
574                 switch (quirks->mailled) {
575                 default:
576                         err = ec_read(0xA, &result);
577                         if (err)
578                                 return AE_ERROR;
579                         *value = (result >> 7) & 0x1;
580                         return AE_OK;
581                 }
582                 break;
583         case ACER_CAP_WIRELESS:
584                 switch (quirks->wireless) {
585                 case 1:
586                         err = ec_read(0x7B, &result);
587                         if (err)
588                                 return AE_ERROR;
589                         *value = result & 0x1;
590                         return AE_OK;
591                 case 2:
592                         err = ec_read(0x71, &result);
593                         if (err)
594                                 return AE_ERROR;
595                         *value = result & 0x1;
596                         return AE_OK;
597                 case 3:
598                         err = ec_read(0x78, &result);
599                         if (err)
600                                 return AE_ERROR;
601                         *value = result & 0x1;
602                         return AE_OK;
603                 default:
604                         err = ec_read(0xA, &result);
605                         if (err)
606                                 return AE_ERROR;
607                         *value = (result >> 2) & 0x1;
608                         return AE_OK;
609                 }
610                 break;
611         case ACER_CAP_BLUETOOTH:
612                 switch (quirks->bluetooth) {
613                 default:
614                         err = ec_read(0xA, &result);
615                         if (err)
616                                 return AE_ERROR;
617                         *value = (result >> 4) & 0x1;
618                         return AE_OK;
619                 }
620                 break;
621         case ACER_CAP_BRIGHTNESS:
622                 switch (quirks->brightness) {
623                 default:
624                         err = ec_read(0x83, &result);
625                         if (err)
626                                 return AE_ERROR;
627                         *value = result;
628                         return AE_OK;
629                 }
630                 break;
631         default:
632                 return AE_ERROR;
633         }
634         return AE_OK;
635 }
636
637 static acpi_status AMW0_set_u32(u32 value, u32 cap)
638 {
639         struct wmab_args args;
640
641         args.eax = ACER_AMW0_WRITE;
642         args.ebx = value ? (1<<8) : 0;
643         args.ecx = args.edx = 0;
644
645         switch (cap) {
646         case ACER_CAP_MAILLED:
647                 if (value > 1)
648                         return AE_BAD_PARAMETER;
649                 args.ebx |= ACER_AMW0_MAILLED_MASK;
650                 break;
651         case ACER_CAP_WIRELESS:
652                 if (value > 1)
653                         return AE_BAD_PARAMETER;
654                 args.ebx |= ACER_AMW0_WIRELESS_MASK;
655                 break;
656         case ACER_CAP_BLUETOOTH:
657                 if (value > 1)
658                         return AE_BAD_PARAMETER;
659                 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
660                 break;
661         case ACER_CAP_BRIGHTNESS:
662                 if (value > max_brightness)
663                         return AE_BAD_PARAMETER;
664                 switch (quirks->brightness) {
665                 default:
666                         return ec_write(0x83, value);
667                         break;
668                 }
669         default:
670                 return AE_ERROR;
671         }
672
673         /* Actually do the set */
674         return wmab_execute(&args, NULL);
675 }
676
677 static acpi_status AMW0_find_mailled(void)
678 {
679         struct wmab_args args;
680         struct wmab_ret ret;
681         acpi_status status = AE_OK;
682         struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
683         union acpi_object *obj;
684
685         args.eax = 0x86;
686         args.ebx = args.ecx = args.edx = 0;
687
688         status = wmab_execute(&args, &out);
689         if (ACPI_FAILURE(status))
690                 return status;
691
692         obj = (union acpi_object *) out.pointer;
693         if (obj && obj->type == ACPI_TYPE_BUFFER &&
694         obj->buffer.length == sizeof(struct wmab_ret)) {
695                 ret = *((struct wmab_ret *) obj->buffer.pointer);
696         } else {
697                 kfree(out.pointer);
698                 return AE_ERROR;
699         }
700
701         if (ret.eex & 0x1)
702                 interface->capability |= ACER_CAP_MAILLED;
703
704         kfree(out.pointer);
705
706         return AE_OK;
707 }
708
709 static int AMW0_set_cap_acpi_check_device_found;
710
711 static acpi_status AMW0_set_cap_acpi_check_device_cb(acpi_handle handle,
712         u32 level, void *context, void **retval)
713 {
714         AMW0_set_cap_acpi_check_device_found = 1;
715         return AE_OK;
716 }
717
718 static const struct acpi_device_id norfkill_ids[] = {
719         { "VPC2004", 0},
720         { "IBM0068", 0},
721         { "LEN0068", 0},
722         { "SNY5001", 0},        /* sony-laptop in charge */
723         { "", 0},
724 };
725
726 static int AMW0_set_cap_acpi_check_device(void)
727 {
728         const struct acpi_device_id *id;
729
730         for (id = norfkill_ids; id->id[0]; id++)
731                 acpi_get_devices(id->id, AMW0_set_cap_acpi_check_device_cb,
732                                 NULL, NULL);
733         return AMW0_set_cap_acpi_check_device_found;
734 }
735
736 static acpi_status AMW0_set_capabilities(void)
737 {
738         struct wmab_args args;
739         struct wmab_ret ret;
740         acpi_status status;
741         struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
742         union acpi_object *obj;
743
744         /*
745          * On laptops with this strange GUID (non Acer), normal probing doesn't
746          * work.
747          */
748         if (wmi_has_guid(AMW0_GUID2)) {
749                 if ((quirks != &quirk_unknown) ||
750                     !AMW0_set_cap_acpi_check_device())
751                         interface->capability |= ACER_CAP_WIRELESS;
752                 return AE_OK;
753         }
754
755         args.eax = ACER_AMW0_WRITE;
756         args.ecx = args.edx = 0;
757
758         args.ebx = 0xa2 << 8;
759         args.ebx |= ACER_AMW0_WIRELESS_MASK;
760
761         status = wmab_execute(&args, &out);
762         if (ACPI_FAILURE(status))
763                 return status;
764
765         obj = out.pointer;
766         if (obj && obj->type == ACPI_TYPE_BUFFER &&
767         obj->buffer.length == sizeof(struct wmab_ret)) {
768                 ret = *((struct wmab_ret *) obj->buffer.pointer);
769         } else {
770                 status = AE_ERROR;
771                 goto out;
772         }
773
774         if (ret.eax & 0x1)
775                 interface->capability |= ACER_CAP_WIRELESS;
776
777         args.ebx = 2 << 8;
778         args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
779
780         /*
781          * It's ok to use existing buffer for next wmab_execute call.
782          * But we need to kfree(out.pointer) if next wmab_execute fail.
783          */
784         status = wmab_execute(&args, &out);
785         if (ACPI_FAILURE(status))
786                 goto out;
787
788         obj = (union acpi_object *) out.pointer;
789         if (obj && obj->type == ACPI_TYPE_BUFFER
790         && obj->buffer.length == sizeof(struct wmab_ret)) {
791                 ret = *((struct wmab_ret *) obj->buffer.pointer);
792         } else {
793                 status = AE_ERROR;
794                 goto out;
795         }
796
797         if (ret.eax & 0x1)
798                 interface->capability |= ACER_CAP_BLUETOOTH;
799
800         /*
801          * This appears to be safe to enable, since all Wistron based laptops
802          * appear to use the same EC register for brightness, even if they
803          * differ for wireless, etc
804          */
805         if (quirks->brightness >= 0)
806                 interface->capability |= ACER_CAP_BRIGHTNESS;
807
808         status = AE_OK;
809 out:
810         kfree(out.pointer);
811         return status;
812 }
813
814 static struct wmi_interface AMW0_interface = {
815         .type = ACER_AMW0,
816 };
817
818 static struct wmi_interface AMW0_V2_interface = {
819         .type = ACER_AMW0_V2,
820 };
821
822 /*
823  * New interface (The WMID interface)
824  */
825 static acpi_status
826 WMI_execute_u32(u32 method_id, u32 in, u32 *out)
827 {
828         struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
829         struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
830         union acpi_object *obj;
831         u32 tmp;
832         acpi_status status;
833
834         status = wmi_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);
835
836         if (ACPI_FAILURE(status))
837                 return status;
838
839         obj = (union acpi_object *) result.pointer;
840         if (obj && obj->type == ACPI_TYPE_BUFFER &&
841                 (obj->buffer.length == sizeof(u32) ||
842                 obj->buffer.length == sizeof(u64))) {
843                 tmp = *((u32 *) obj->buffer.pointer);
844         } else if (obj->type == ACPI_TYPE_INTEGER) {
845                 tmp = (u32) obj->integer.value;
846         } else {
847                 tmp = 0;
848         }
849
850         if (out)
851                 *out = tmp;
852
853         kfree(result.pointer);
854
855         return status;
856 }
857
858 static acpi_status WMID_get_u32(u32 *value, u32 cap)
859 {
860         acpi_status status;
861         u8 tmp;
862         u32 result, method_id = 0;
863
864         switch (cap) {
865         case ACER_CAP_WIRELESS:
866                 method_id = ACER_WMID_GET_WIRELESS_METHODID;
867                 break;
868         case ACER_CAP_BLUETOOTH:
869                 method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
870                 break;
871         case ACER_CAP_BRIGHTNESS:
872                 method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
873                 break;
874         case ACER_CAP_THREEG:
875                 method_id = ACER_WMID_GET_THREEG_METHODID;
876                 break;
877         case ACER_CAP_MAILLED:
878                 if (quirks->mailled == 1) {
879                         ec_read(0x9f, &tmp);
880                         *value = tmp & 0x1;
881                         return 0;
882                 }
883         default:
884                 return AE_ERROR;
885         }
886         status = WMI_execute_u32(method_id, 0, &result);
887
888         if (ACPI_SUCCESS(status))
889                 *value = (u8)result;
890
891         return status;
892 }
893
894 static acpi_status WMID_set_u32(u32 value, u32 cap)
895 {
896         u32 method_id = 0;
897         char param;
898
899         switch (cap) {
900         case ACER_CAP_BRIGHTNESS:
901                 if (value > max_brightness)
902                         return AE_BAD_PARAMETER;
903                 method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
904                 break;
905         case ACER_CAP_WIRELESS:
906                 if (value > 1)
907                         return AE_BAD_PARAMETER;
908                 method_id = ACER_WMID_SET_WIRELESS_METHODID;
909                 break;
910         case ACER_CAP_BLUETOOTH:
911                 if (value > 1)
912                         return AE_BAD_PARAMETER;
913                 method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
914                 break;
915         case ACER_CAP_THREEG:
916                 if (value > 1)
917                         return AE_BAD_PARAMETER;
918                 method_id = ACER_WMID_SET_THREEG_METHODID;
919                 break;
920         case ACER_CAP_MAILLED:
921                 if (value > 1)
922                         return AE_BAD_PARAMETER;
923                 if (quirks->mailled == 1) {
924                         param = value ? 0x92 : 0x93;
925                         i8042_lock_chip();
926                         i8042_command(&param, 0x1059);
927                         i8042_unlock_chip();
928                         return 0;
929                 }
930                 break;
931         default:
932                 return AE_ERROR;
933         }
934         return WMI_execute_u32(method_id, (u32)value, NULL);
935 }
936
937 static acpi_status wmid3_get_device_status(u32 *value, u16 device)
938 {
939         struct wmid3_gds_return_value return_value;
940         acpi_status status;
941         union acpi_object *obj;
942         struct wmid3_gds_get_input_param params = {
943                 .function_num = 0x1,
944                 .hotkey_number = commun_fn_key_number,
945                 .devices = device,
946         };
947         struct acpi_buffer input = {
948                 sizeof(struct wmid3_gds_get_input_param),
949                 &params
950         };
951         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
952
953         status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &input, &output);
954         if (ACPI_FAILURE(status))
955                 return status;
956
957         obj = output.pointer;
958
959         if (!obj)
960                 return AE_ERROR;
961         else if (obj->type != ACPI_TYPE_BUFFER) {
962                 kfree(obj);
963                 return AE_ERROR;
964         }
965         if (obj->buffer.length != 8) {
966                 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
967                 kfree(obj);
968                 return AE_ERROR;
969         }
970
971         return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
972         kfree(obj);
973
974         if (return_value.error_code || return_value.ec_return_value)
975                 pr_warn("Get 0x%x Device Status failed: 0x%x - 0x%x\n",
976                         device,
977                         return_value.error_code,
978                         return_value.ec_return_value);
979         else
980                 *value = !!(return_value.devices & device);
981
982         return status;
983 }
984
985 static acpi_status wmid_v2_get_u32(u32 *value, u32 cap)
986 {
987         u16 device;
988
989         switch (cap) {
990         case ACER_CAP_WIRELESS:
991                 device = ACER_WMID3_GDS_WIRELESS;
992                 break;
993         case ACER_CAP_BLUETOOTH:
994                 device = ACER_WMID3_GDS_BLUETOOTH;
995                 break;
996         case ACER_CAP_THREEG:
997                 device = ACER_WMID3_GDS_THREEG;
998                 break;
999         default:
1000                 return AE_ERROR;
1001         }
1002         return wmid3_get_device_status(value, device);
1003 }
1004
1005 static acpi_status wmid3_set_device_status(u32 value, u16 device)
1006 {
1007         struct wmid3_gds_return_value return_value;
1008         acpi_status status;
1009         union acpi_object *obj;
1010         u16 devices;
1011         struct wmid3_gds_get_input_param get_params = {
1012                 .function_num = 0x1,
1013                 .hotkey_number = commun_fn_key_number,
1014                 .devices = commun_func_bitmap,
1015         };
1016         struct acpi_buffer get_input = {
1017                 sizeof(struct wmid3_gds_get_input_param),
1018                 &get_params
1019         };
1020         struct wmid3_gds_set_input_param set_params = {
1021                 .function_num = 0x2,
1022                 .hotkey_number = commun_fn_key_number,
1023                 .devices = commun_func_bitmap,
1024         };
1025         struct acpi_buffer set_input = {
1026                 sizeof(struct wmid3_gds_set_input_param),
1027                 &set_params
1028         };
1029         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1030         struct acpi_buffer output2 = { ACPI_ALLOCATE_BUFFER, NULL };
1031
1032         status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &get_input, &output);
1033         if (ACPI_FAILURE(status))
1034                 return status;
1035
1036         obj = output.pointer;
1037
1038         if (!obj)
1039                 return AE_ERROR;
1040         else if (obj->type != ACPI_TYPE_BUFFER) {
1041                 kfree(obj);
1042                 return AE_ERROR;
1043         }
1044         if (obj->buffer.length != 8) {
1045                 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1046                 kfree(obj);
1047                 return AE_ERROR;
1048         }
1049
1050         return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
1051         kfree(obj);
1052
1053         if (return_value.error_code || return_value.ec_return_value) {
1054                 pr_warn("Get Current Device Status failed: 0x%x - 0x%x\n",
1055                         return_value.error_code,
1056                         return_value.ec_return_value);
1057                 return status;
1058         }
1059
1060         devices = return_value.devices;
1061         set_params.devices = (value) ? (devices | device) : (devices & ~device);
1062
1063         status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &set_input, &output2);
1064         if (ACPI_FAILURE(status))
1065                 return status;
1066
1067         obj = output2.pointer;
1068
1069         if (!obj)
1070                 return AE_ERROR;
1071         else if (obj->type != ACPI_TYPE_BUFFER) {
1072                 kfree(obj);
1073                 return AE_ERROR;
1074         }
1075         if (obj->buffer.length != 4) {
1076                 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1077                 kfree(obj);
1078                 return AE_ERROR;
1079         }
1080
1081         return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
1082         kfree(obj);
1083
1084         if (return_value.error_code || return_value.ec_return_value)
1085                 pr_warn("Set Device Status failed: 0x%x - 0x%x\n",
1086                         return_value.error_code,
1087                         return_value.ec_return_value);
1088
1089         return status;
1090 }
1091
1092 static acpi_status wmid_v2_set_u32(u32 value, u32 cap)
1093 {
1094         u16 device;
1095
1096         switch (cap) {
1097         case ACER_CAP_WIRELESS:
1098                 device = ACER_WMID3_GDS_WIRELESS;
1099                 break;
1100         case ACER_CAP_BLUETOOTH:
1101                 device = ACER_WMID3_GDS_BLUETOOTH;
1102                 break;
1103         case ACER_CAP_THREEG:
1104                 device = ACER_WMID3_GDS_THREEG;
1105                 break;
1106         default:
1107                 return AE_ERROR;
1108         }
1109         return wmid3_set_device_status(value, device);
1110 }
1111
1112 static void type_aa_dmi_decode(const struct dmi_header *header, void *dummy)
1113 {
1114         struct hotkey_function_type_aa *type_aa;
1115
1116         /* We are looking for OEM-specific Type AAh */
1117         if (header->type != 0xAA)
1118                 return;
1119
1120         has_type_aa = true;
1121         type_aa = (struct hotkey_function_type_aa *) header;
1122
1123         pr_info("Function bitmap for Communication Button: 0x%x\n",
1124                 type_aa->commun_func_bitmap);
1125         commun_func_bitmap = type_aa->commun_func_bitmap;
1126
1127         if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_WIRELESS)
1128                 interface->capability |= ACER_CAP_WIRELESS;
1129         if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_THREEG)
1130                 interface->capability |= ACER_CAP_THREEG;
1131         if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_BLUETOOTH)
1132                 interface->capability |= ACER_CAP_BLUETOOTH;
1133
1134         commun_fn_key_number = type_aa->commun_fn_key_number;
1135 }
1136
1137 static acpi_status WMID_set_capabilities(void)
1138 {
1139         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1140         union acpi_object *obj;
1141         acpi_status status;
1142         u32 devices;
1143
1144         status = wmi_query_block(WMID_GUID2, 1, &out);
1145         if (ACPI_FAILURE(status))
1146                 return status;
1147
1148         obj = (union acpi_object *) out.pointer;
1149         if (obj && obj->type == ACPI_TYPE_BUFFER &&
1150                 (obj->buffer.length == sizeof(u32) ||
1151                 obj->buffer.length == sizeof(u64))) {
1152                 devices = *((u32 *) obj->buffer.pointer);
1153         } else if (obj->type == ACPI_TYPE_INTEGER) {
1154                 devices = (u32) obj->integer.value;
1155         } else {
1156                 kfree(out.pointer);
1157                 return AE_ERROR;
1158         }
1159
1160         pr_info("Function bitmap for Communication Device: 0x%x\n", devices);
1161         if (devices & 0x07)
1162                 interface->capability |= ACER_CAP_WIRELESS;
1163         if (devices & 0x40)
1164                 interface->capability |= ACER_CAP_THREEG;
1165         if (devices & 0x10)
1166                 interface->capability |= ACER_CAP_BLUETOOTH;
1167
1168         if (!(devices & 0x20))
1169                 max_brightness = 0x9;
1170
1171         kfree(out.pointer);
1172         return status;
1173 }
1174
1175 static struct wmi_interface wmid_interface = {
1176         .type = ACER_WMID,
1177 };
1178
1179 static struct wmi_interface wmid_v2_interface = {
1180         .type = ACER_WMID_v2,
1181 };
1182
1183 /*
1184  * Generic Device (interface-independent)
1185  */
1186
1187 static acpi_status get_u32(u32 *value, u32 cap)
1188 {
1189         acpi_status status = AE_ERROR;
1190
1191         switch (interface->type) {
1192         case ACER_AMW0:
1193                 status = AMW0_get_u32(value, cap);
1194                 break;
1195         case ACER_AMW0_V2:
1196                 if (cap == ACER_CAP_MAILLED) {
1197                         status = AMW0_get_u32(value, cap);
1198                         break;
1199                 }
1200         case ACER_WMID:
1201                 status = WMID_get_u32(value, cap);
1202                 break;
1203         case ACER_WMID_v2:
1204                 if (cap & (ACER_CAP_WIRELESS |
1205                            ACER_CAP_BLUETOOTH |
1206                            ACER_CAP_THREEG))
1207                         status = wmid_v2_get_u32(value, cap);
1208                 else if (wmi_has_guid(WMID_GUID2))
1209                         status = WMID_get_u32(value, cap);
1210                 break;
1211         }
1212
1213         return status;
1214 }
1215
1216 static acpi_status set_u32(u32 value, u32 cap)
1217 {
1218         acpi_status status;
1219
1220         if (interface->capability & cap) {
1221                 switch (interface->type) {
1222                 case ACER_AMW0:
1223                         return AMW0_set_u32(value, cap);
1224                 case ACER_AMW0_V2:
1225                         if (cap == ACER_CAP_MAILLED)
1226                                 return AMW0_set_u32(value, cap);
1227
1228                         /*
1229                          * On some models, some WMID methods don't toggle
1230                          * properly. For those cases, we want to run the AMW0
1231                          * method afterwards to be certain we've really toggled
1232                          * the device state.
1233                          */
1234                         if (cap == ACER_CAP_WIRELESS ||
1235                                 cap == ACER_CAP_BLUETOOTH) {
1236                                 status = WMID_set_u32(value, cap);
1237                                 if (ACPI_FAILURE(status))
1238                                         return status;
1239
1240                                 return AMW0_set_u32(value, cap);
1241                         }
1242                 case ACER_WMID:
1243                         return WMID_set_u32(value, cap);
1244                 case ACER_WMID_v2:
1245                         if (cap & (ACER_CAP_WIRELESS |
1246                                    ACER_CAP_BLUETOOTH |
1247                                    ACER_CAP_THREEG))
1248                                 return wmid_v2_set_u32(value, cap);
1249                         else if (wmi_has_guid(WMID_GUID2))
1250                                 return WMID_set_u32(value, cap);
1251                 default:
1252                         return AE_BAD_PARAMETER;
1253                 }
1254         }
1255         return AE_BAD_PARAMETER;
1256 }
1257
1258 static void __init acer_commandline_init(void)
1259 {
1260         /*
1261          * These will all fail silently if the value given is invalid, or the
1262          * capability isn't available on the given interface
1263          */
1264         if (mailled >= 0)
1265                 set_u32(mailled, ACER_CAP_MAILLED);
1266         if (!has_type_aa && threeg >= 0)
1267                 set_u32(threeg, ACER_CAP_THREEG);
1268         if (brightness >= 0)
1269                 set_u32(brightness, ACER_CAP_BRIGHTNESS);
1270 }
1271
1272 /*
1273  * LED device (Mail LED only, no other LEDs known yet)
1274  */
1275 static void mail_led_set(struct led_classdev *led_cdev,
1276 enum led_brightness value)
1277 {
1278         set_u32(value, ACER_CAP_MAILLED);
1279 }
1280
1281 static struct led_classdev mail_led = {
1282         .name = "acer-wmi::mail",
1283         .brightness_set = mail_led_set,
1284 };
1285
1286 static int __devinit acer_led_init(struct device *dev)
1287 {
1288         return led_classdev_register(dev, &mail_led);
1289 }
1290
1291 static void acer_led_exit(void)
1292 {
1293         set_u32(LED_OFF, ACER_CAP_MAILLED);
1294         led_classdev_unregister(&mail_led);
1295 }
1296
1297 /*
1298  * Backlight device
1299  */
1300 static struct backlight_device *acer_backlight_device;
1301
1302 static int read_brightness(struct backlight_device *bd)
1303 {
1304         u32 value;
1305         get_u32(&value, ACER_CAP_BRIGHTNESS);
1306         return value;
1307 }
1308
1309 static int update_bl_status(struct backlight_device *bd)
1310 {
1311         int intensity = bd->props.brightness;
1312
1313         if (bd->props.power != FB_BLANK_UNBLANK)
1314                 intensity = 0;
1315         if (bd->props.fb_blank != FB_BLANK_UNBLANK)
1316                 intensity = 0;
1317
1318         set_u32(intensity, ACER_CAP_BRIGHTNESS);
1319
1320         return 0;
1321 }
1322
1323 static const struct backlight_ops acer_bl_ops = {
1324         .get_brightness = read_brightness,
1325         .update_status = update_bl_status,
1326 };
1327
1328 static int __devinit acer_backlight_init(struct device *dev)
1329 {
1330         struct backlight_properties props;
1331         struct backlight_device *bd;
1332
1333         memset(&props, 0, sizeof(struct backlight_properties));
1334         props.type = BACKLIGHT_PLATFORM;
1335         props.max_brightness = max_brightness;
1336         bd = backlight_device_register("acer-wmi", dev, NULL, &acer_bl_ops,
1337                                        &props);
1338         if (IS_ERR(bd)) {
1339                 pr_err("Could not register Acer backlight device\n");
1340                 acer_backlight_device = NULL;
1341                 return PTR_ERR(bd);
1342         }
1343
1344         acer_backlight_device = bd;
1345
1346         bd->props.power = FB_BLANK_UNBLANK;
1347         bd->props.brightness = read_brightness(bd);
1348         backlight_update_status(bd);
1349         return 0;
1350 }
1351
1352 static void acer_backlight_exit(void)
1353 {
1354         backlight_device_unregister(acer_backlight_device);
1355 }
1356
1357 /*
1358  * Rfkill devices
1359  */
1360 static void acer_rfkill_update(struct work_struct *ignored);
1361 static DECLARE_DELAYED_WORK(acer_rfkill_work, acer_rfkill_update);
1362 static void acer_rfkill_update(struct work_struct *ignored)
1363 {
1364         u32 state;
1365         acpi_status status;
1366
1367         if (has_cap(ACER_CAP_WIRELESS)) {
1368                 status = get_u32(&state, ACER_CAP_WIRELESS);
1369                 if (ACPI_SUCCESS(status)) {
1370                         if (quirks->wireless == 3)
1371                                 rfkill_set_hw_state(wireless_rfkill, !state);
1372                         else
1373                                 rfkill_set_sw_state(wireless_rfkill, !state);
1374                 }
1375         }
1376
1377         if (has_cap(ACER_CAP_BLUETOOTH)) {
1378                 status = get_u32(&state, ACER_CAP_BLUETOOTH);
1379                 if (ACPI_SUCCESS(status))
1380                         rfkill_set_sw_state(bluetooth_rfkill, !state);
1381         }
1382
1383         if (has_cap(ACER_CAP_THREEG) && wmi_has_guid(WMID_GUID3)) {
1384                 status = get_u32(&state, ACER_WMID3_GDS_THREEG);
1385                 if (ACPI_SUCCESS(status))
1386                         rfkill_set_sw_state(threeg_rfkill, !state);
1387         }
1388
1389         schedule_delayed_work(&acer_rfkill_work, round_jiffies_relative(HZ));
1390 }
1391
1392 static int acer_rfkill_set(void *data, bool blocked)
1393 {
1394         acpi_status status;
1395         u32 cap = (unsigned long)data;
1396
1397         if (rfkill_inited) {
1398                 status = set_u32(!blocked, cap);
1399                 if (ACPI_FAILURE(status))
1400                         return -ENODEV;
1401         }
1402
1403         return 0;
1404 }
1405
1406 static const struct rfkill_ops acer_rfkill_ops = {
1407         .set_block = acer_rfkill_set,
1408 };
1409
1410 static struct rfkill *acer_rfkill_register(struct device *dev,
1411                                            enum rfkill_type type,
1412                                            char *name, u32 cap)
1413 {
1414         int err;
1415         struct rfkill *rfkill_dev;
1416         u32 state;
1417         acpi_status status;
1418
1419         rfkill_dev = rfkill_alloc(name, dev, type,
1420                                   &acer_rfkill_ops,
1421                                   (void *)(unsigned long)cap);
1422         if (!rfkill_dev)
1423                 return ERR_PTR(-ENOMEM);
1424
1425         status = get_u32(&state, cap);
1426
1427         err = rfkill_register(rfkill_dev);
1428         if (err) {
1429                 rfkill_destroy(rfkill_dev);
1430                 return ERR_PTR(err);
1431         }
1432
1433         if (ACPI_SUCCESS(status))
1434                 rfkill_set_sw_state(rfkill_dev, !state);
1435
1436         return rfkill_dev;
1437 }
1438
1439 static int acer_rfkill_init(struct device *dev)
1440 {
1441         int err;
1442
1443         if (has_cap(ACER_CAP_WIRELESS)) {
1444                 wireless_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_WLAN,
1445                         "acer-wireless", ACER_CAP_WIRELESS);
1446                 if (IS_ERR(wireless_rfkill)) {
1447                         err = PTR_ERR(wireless_rfkill);
1448                         goto error_wireless;
1449                 }
1450         }
1451
1452         if (has_cap(ACER_CAP_BLUETOOTH)) {
1453                 bluetooth_rfkill = acer_rfkill_register(dev,
1454                         RFKILL_TYPE_BLUETOOTH, "acer-bluetooth",
1455                         ACER_CAP_BLUETOOTH);
1456                 if (IS_ERR(bluetooth_rfkill)) {
1457                         err = PTR_ERR(bluetooth_rfkill);
1458                         goto error_bluetooth;
1459                 }
1460         }
1461
1462         if (has_cap(ACER_CAP_THREEG)) {
1463                 threeg_rfkill = acer_rfkill_register(dev,
1464                         RFKILL_TYPE_WWAN, "acer-threeg",
1465                         ACER_CAP_THREEG);
1466                 if (IS_ERR(threeg_rfkill)) {
1467                         err = PTR_ERR(threeg_rfkill);
1468                         goto error_threeg;
1469                 }
1470         }
1471
1472         rfkill_inited = true;
1473
1474         if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
1475             has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1476                 schedule_delayed_work(&acer_rfkill_work,
1477                         round_jiffies_relative(HZ));
1478
1479         return 0;
1480
1481 error_threeg:
1482         if (has_cap(ACER_CAP_BLUETOOTH)) {
1483                 rfkill_unregister(bluetooth_rfkill);
1484                 rfkill_destroy(bluetooth_rfkill);
1485         }
1486 error_bluetooth:
1487         if (has_cap(ACER_CAP_WIRELESS)) {
1488                 rfkill_unregister(wireless_rfkill);
1489                 rfkill_destroy(wireless_rfkill);
1490         }
1491 error_wireless:
1492         return err;
1493 }
1494
1495 static void acer_rfkill_exit(void)
1496 {
1497         if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
1498             has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1499                 cancel_delayed_work_sync(&acer_rfkill_work);
1500
1501         if (has_cap(ACER_CAP_WIRELESS)) {
1502                 rfkill_unregister(wireless_rfkill);
1503                 rfkill_destroy(wireless_rfkill);
1504         }
1505
1506         if (has_cap(ACER_CAP_BLUETOOTH)) {
1507                 rfkill_unregister(bluetooth_rfkill);
1508                 rfkill_destroy(bluetooth_rfkill);
1509         }
1510
1511         if (has_cap(ACER_CAP_THREEG)) {
1512                 rfkill_unregister(threeg_rfkill);
1513                 rfkill_destroy(threeg_rfkill);
1514         }
1515         return;
1516 }
1517
1518 /*
1519  * sysfs interface
1520  */
1521 static ssize_t show_bool_threeg(struct device *dev,
1522         struct device_attribute *attr, char *buf)
1523 {
1524         u32 result; \
1525         acpi_status status;
1526
1527         pr_info("This threeg sysfs will be removed in 2012 - used by: %s\n",
1528                 current->comm);
1529         status = get_u32(&result, ACER_CAP_THREEG);
1530         if (ACPI_SUCCESS(status))
1531                 return sprintf(buf, "%u\n", result);
1532         return sprintf(buf, "Read error\n");
1533 }
1534
1535 static ssize_t set_bool_threeg(struct device *dev,
1536         struct device_attribute *attr, const char *buf, size_t count)
1537 {
1538         u32 tmp = simple_strtoul(buf, NULL, 10);
1539         acpi_status status = set_u32(tmp, ACER_CAP_THREEG);
1540         pr_info("This threeg sysfs will be removed in 2012 - used by: %s\n",
1541                 current->comm);
1542         if (ACPI_FAILURE(status))
1543                 return -EINVAL;
1544         return count;
1545 }
1546 static DEVICE_ATTR(threeg, S_IRUGO | S_IWUSR, show_bool_threeg,
1547         set_bool_threeg);
1548
1549 static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
1550         char *buf)
1551 {
1552         pr_info("This interface sysfs will be removed in 2012 - used by: %s\n",
1553                 current->comm);
1554         switch (interface->type) {
1555         case ACER_AMW0:
1556                 return sprintf(buf, "AMW0\n");
1557         case ACER_AMW0_V2:
1558                 return sprintf(buf, "AMW0 v2\n");
1559         case ACER_WMID:
1560                 return sprintf(buf, "WMID\n");
1561         case ACER_WMID_v2:
1562                 return sprintf(buf, "WMID v2\n");
1563         default:
1564                 return sprintf(buf, "Error!\n");
1565         }
1566 }
1567
1568 static DEVICE_ATTR(interface, S_IRUGO, show_interface, NULL);
1569
1570 static void acer_wmi_notify(u32 value, void *context)
1571 {
1572         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1573         union acpi_object *obj;
1574         struct event_return_value return_value;
1575         acpi_status status;
1576         u16 device_state;
1577         const struct key_entry *key;
1578
1579         status = wmi_get_event_data(value, &response);
1580         if (status != AE_OK) {
1581                 pr_warn("bad event status 0x%x\n", status);
1582                 return;
1583         }
1584
1585         obj = (union acpi_object *)response.pointer;
1586
1587         if (!obj)
1588                 return;
1589         if (obj->type != ACPI_TYPE_BUFFER) {
1590                 pr_warn("Unknown response received %d\n", obj->type);
1591                 kfree(obj);
1592                 return;
1593         }
1594         if (obj->buffer.length != 8) {
1595                 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1596                 kfree(obj);
1597                 return;
1598         }
1599
1600         return_value = *((struct event_return_value *)obj->buffer.pointer);
1601         kfree(obj);
1602
1603         switch (return_value.function) {
1604         case WMID_HOTKEY_EVENT:
1605                 device_state = return_value.device_state;
1606                 pr_debug("device state: 0x%x\n", device_state);
1607
1608                 key = sparse_keymap_entry_from_scancode(acer_wmi_input_dev,
1609                                                         return_value.key_num);
1610                 if (!key) {
1611                         pr_warn("Unknown key number - 0x%x\n",
1612                                 return_value.key_num);
1613                 } else {
1614                         switch (key->keycode) {
1615                         case KEY_WLAN:
1616                         case KEY_BLUETOOTH:
1617                                 if (has_cap(ACER_CAP_WIRELESS))
1618                                         rfkill_set_sw_state(wireless_rfkill,
1619                                                 !(device_state & ACER_WMID3_GDS_WIRELESS));
1620                                 if (has_cap(ACER_CAP_THREEG))
1621                                         rfkill_set_sw_state(threeg_rfkill,
1622                                                 !(device_state & ACER_WMID3_GDS_THREEG));
1623                                 if (has_cap(ACER_CAP_BLUETOOTH))
1624                                         rfkill_set_sw_state(bluetooth_rfkill,
1625                                                 !(device_state & ACER_WMID3_GDS_BLUETOOTH));
1626                                 break;
1627                         }
1628                         sparse_keymap_report_entry(acer_wmi_input_dev, key,
1629                                                    1, true);
1630                 }
1631                 break;
1632         default:
1633                 pr_warn("Unknown function number - %d - %d\n",
1634                         return_value.function, return_value.key_num);
1635                 break;
1636         }
1637 }
1638
1639 static acpi_status
1640 wmid3_set_lm_mode(struct lm_input_params *params,
1641                   struct lm_return_value *return_value)
1642 {
1643         acpi_status status;
1644         union acpi_object *obj;
1645
1646         struct acpi_buffer input = { sizeof(struct lm_input_params), params };
1647         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1648
1649         status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &input, &output);
1650         if (ACPI_FAILURE(status))
1651                 return status;
1652
1653         obj = output.pointer;
1654
1655         if (!obj)
1656                 return AE_ERROR;
1657         else if (obj->type != ACPI_TYPE_BUFFER) {
1658                 kfree(obj);
1659                 return AE_ERROR;
1660         }
1661         if (obj->buffer.length != 4) {
1662                 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1663                 kfree(obj);
1664                 return AE_ERROR;
1665         }
1666
1667         *return_value = *((struct lm_return_value *)obj->buffer.pointer);
1668         kfree(obj);
1669
1670         return status;
1671 }
1672
1673 static int acer_wmi_enable_ec_raw(void)
1674 {
1675         struct lm_return_value return_value;
1676         acpi_status status;
1677         struct lm_input_params params = {
1678                 .function_num = 0x1,
1679                 .commun_devices = 0xFFFF,
1680                 .devices = 0xFFFF,
1681                 .lm_status = 0x00,            /* Launch Manager Deactive */
1682         };
1683
1684         status = wmid3_set_lm_mode(&params, &return_value);
1685
1686         if (return_value.error_code || return_value.ec_return_value)
1687                 pr_warn("Enabling EC raw mode failed: 0x%x - 0x%x\n",
1688                         return_value.error_code,
1689                         return_value.ec_return_value);
1690         else
1691                 pr_info("Enabled EC raw mode\n");
1692
1693         return status;
1694 }
1695
1696 static int acer_wmi_enable_lm(void)
1697 {
1698         struct lm_return_value return_value;
1699         acpi_status status;
1700         struct lm_input_params params = {
1701                 .function_num = 0x1,
1702                 .commun_devices = 0xFFFF,
1703                 .devices = 0xFFFF,
1704                 .lm_status = 0x01,            /* Launch Manager Active */
1705         };
1706
1707         status = wmid3_set_lm_mode(&params, &return_value);
1708
1709         if (return_value.error_code || return_value.ec_return_value)
1710                 pr_warn("Enabling Launch Manager failed: 0x%x - 0x%x\n",
1711                         return_value.error_code,
1712                         return_value.ec_return_value);
1713
1714         return status;
1715 }
1716
1717 static int __init acer_wmi_input_setup(void)
1718 {
1719         acpi_status status;
1720         int err;
1721
1722         acer_wmi_input_dev = input_allocate_device();
1723         if (!acer_wmi_input_dev)
1724                 return -ENOMEM;
1725
1726         acer_wmi_input_dev->name = "Acer WMI hotkeys";
1727         acer_wmi_input_dev->phys = "wmi/input0";
1728         acer_wmi_input_dev->id.bustype = BUS_HOST;
1729
1730         err = sparse_keymap_setup(acer_wmi_input_dev, acer_wmi_keymap, NULL);
1731         if (err)
1732                 goto err_free_dev;
1733
1734         status = wmi_install_notify_handler(ACERWMID_EVENT_GUID,
1735                                                 acer_wmi_notify, NULL);
1736         if (ACPI_FAILURE(status)) {
1737                 err = -EIO;
1738                 goto err_free_keymap;
1739         }
1740
1741         err = input_register_device(acer_wmi_input_dev);
1742         if (err)
1743                 goto err_uninstall_notifier;
1744
1745         return 0;
1746
1747 err_uninstall_notifier:
1748         wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
1749 err_free_keymap:
1750         sparse_keymap_free(acer_wmi_input_dev);
1751 err_free_dev:
1752         input_free_device(acer_wmi_input_dev);
1753         return err;
1754 }
1755
1756 static void acer_wmi_input_destroy(void)
1757 {
1758         wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
1759         sparse_keymap_free(acer_wmi_input_dev);
1760         input_unregister_device(acer_wmi_input_dev);
1761 }
1762
1763 /*
1764  * debugfs functions
1765  */
1766 static u32 get_wmid_devices(void)
1767 {
1768         struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1769         union acpi_object *obj;
1770         acpi_status status;
1771         u32 devices = 0;
1772
1773         status = wmi_query_block(WMID_GUID2, 1, &out);
1774         if (ACPI_FAILURE(status))
1775                 return 0;
1776
1777         obj = (union acpi_object *) out.pointer;
1778         if (obj && obj->type == ACPI_TYPE_BUFFER &&
1779                 (obj->buffer.length == sizeof(u32) ||
1780                 obj->buffer.length == sizeof(u64))) {
1781                 devices = *((u32 *) obj->buffer.pointer);
1782         } else if (obj->type == ACPI_TYPE_INTEGER) {
1783                 devices = (u32) obj->integer.value;
1784         }
1785
1786         kfree(out.pointer);
1787         return devices;
1788 }
1789
1790 /*
1791  * Platform device
1792  */
1793 static int __devinit acer_platform_probe(struct platform_device *device)
1794 {
1795         int err;
1796
1797         if (has_cap(ACER_CAP_MAILLED)) {
1798                 err = acer_led_init(&device->dev);
1799                 if (err)
1800                         goto error_mailled;
1801         }
1802
1803         if (has_cap(ACER_CAP_BRIGHTNESS)) {
1804                 err = acer_backlight_init(&device->dev);
1805                 if (err)
1806                         goto error_brightness;
1807         }
1808
1809         err = acer_rfkill_init(&device->dev);
1810         if (err)
1811                 goto error_rfkill;
1812
1813         return err;
1814
1815 error_rfkill:
1816         if (has_cap(ACER_CAP_BRIGHTNESS))
1817                 acer_backlight_exit();
1818 error_brightness:
1819         if (has_cap(ACER_CAP_MAILLED))
1820                 acer_led_exit();
1821 error_mailled:
1822         return err;
1823 }
1824
1825 static int acer_platform_remove(struct platform_device *device)
1826 {
1827         if (has_cap(ACER_CAP_MAILLED))
1828                 acer_led_exit();
1829         if (has_cap(ACER_CAP_BRIGHTNESS))
1830                 acer_backlight_exit();
1831
1832         acer_rfkill_exit();
1833         return 0;
1834 }
1835
1836 static int acer_platform_suspend(struct platform_device *dev,
1837 pm_message_t state)
1838 {
1839         u32 value;
1840         struct acer_data *data = &interface->data;
1841
1842         if (!data)
1843                 return -ENOMEM;
1844
1845         if (has_cap(ACER_CAP_MAILLED)) {
1846                 get_u32(&value, ACER_CAP_MAILLED);
1847                 set_u32(LED_OFF, ACER_CAP_MAILLED);
1848                 data->mailled = value;
1849         }
1850
1851         if (has_cap(ACER_CAP_BRIGHTNESS)) {
1852                 get_u32(&value, ACER_CAP_BRIGHTNESS);
1853                 data->brightness = value;
1854         }
1855
1856         return 0;
1857 }
1858
1859 static int acer_platform_resume(struct platform_device *device)
1860 {
1861         struct acer_data *data = &interface->data;
1862
1863         if (!data)
1864                 return -ENOMEM;
1865
1866         if (has_cap(ACER_CAP_MAILLED))
1867                 set_u32(data->mailled, ACER_CAP_MAILLED);
1868
1869         if (has_cap(ACER_CAP_BRIGHTNESS))
1870                 set_u32(data->brightness, ACER_CAP_BRIGHTNESS);
1871
1872         return 0;
1873 }
1874
1875 static void acer_platform_shutdown(struct platform_device *device)
1876 {
1877         struct acer_data *data = &interface->data;
1878
1879         if (!data)
1880                 return;
1881
1882         if (has_cap(ACER_CAP_MAILLED))
1883                 set_u32(LED_OFF, ACER_CAP_MAILLED);
1884 }
1885
1886 static struct platform_driver acer_platform_driver = {
1887         .driver = {
1888                 .name = "acer-wmi",
1889                 .owner = THIS_MODULE,
1890         },
1891         .probe = acer_platform_probe,
1892         .remove = acer_platform_remove,
1893         .suspend = acer_platform_suspend,
1894         .resume = acer_platform_resume,
1895         .shutdown = acer_platform_shutdown,
1896 };
1897
1898 static struct platform_device *acer_platform_device;
1899
1900 static int remove_sysfs(struct platform_device *device)
1901 {
1902         if (has_cap(ACER_CAP_THREEG))
1903                 device_remove_file(&device->dev, &dev_attr_threeg);
1904
1905         device_remove_file(&device->dev, &dev_attr_interface);
1906
1907         return 0;
1908 }
1909
1910 static int create_sysfs(void)
1911 {
1912         int retval = -ENOMEM;
1913
1914         if (has_cap(ACER_CAP_THREEG)) {
1915                 retval = device_create_file(&acer_platform_device->dev,
1916                         &dev_attr_threeg);
1917                 if (retval)
1918                         goto error_sysfs;
1919         }
1920
1921         retval = device_create_file(&acer_platform_device->dev,
1922                 &dev_attr_interface);
1923         if (retval)
1924                 goto error_sysfs;
1925
1926         return 0;
1927
1928 error_sysfs:
1929                 remove_sysfs(acer_platform_device);
1930         return retval;
1931 }
1932
1933 static void remove_debugfs(void)
1934 {
1935         debugfs_remove(interface->debug.devices);
1936         debugfs_remove(interface->debug.root);
1937 }
1938
1939 static int create_debugfs(void)
1940 {
1941         interface->debug.root = debugfs_create_dir("acer-wmi", NULL);
1942         if (!interface->debug.root) {
1943                 pr_err("Failed to create debugfs directory");
1944                 return -ENOMEM;
1945         }
1946
1947         interface->debug.devices = debugfs_create_u32("devices", S_IRUGO,
1948                                         interface->debug.root,
1949                                         &interface->debug.wmid_devices);
1950         if (!interface->debug.devices)
1951                 goto error_debugfs;
1952
1953         return 0;
1954
1955 error_debugfs:
1956         remove_debugfs();
1957         return -ENOMEM;
1958 }
1959
1960 static int __init acer_wmi_init(void)
1961 {
1962         int err;
1963
1964         pr_info("Acer Laptop ACPI-WMI Extras\n");
1965
1966         if (dmi_check_system(acer_blacklist)) {
1967                 pr_info("Blacklisted hardware detected - not loading\n");
1968                 return -ENODEV;
1969         }
1970
1971         find_quirks();
1972
1973         /*
1974          * Detect which ACPI-WMI interface we're using.
1975          */
1976         if (wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1977                 interface = &AMW0_V2_interface;
1978
1979         if (!wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1980                 interface = &wmid_interface;
1981
1982         if (wmi_has_guid(WMID_GUID3))
1983                 interface = &wmid_v2_interface;
1984
1985         if (interface)
1986                 dmi_walk(type_aa_dmi_decode, NULL);
1987
1988         if (wmi_has_guid(WMID_GUID2) && interface) {
1989                 if (!has_type_aa && ACPI_FAILURE(WMID_set_capabilities())) {
1990                         pr_err("Unable to detect available WMID devices\n");
1991                         return -ENODEV;
1992                 }
1993                 /* WMID always provides brightness methods */
1994                 interface->capability |= ACER_CAP_BRIGHTNESS;
1995         } else if (!wmi_has_guid(WMID_GUID2) && interface && !has_type_aa) {
1996                 pr_err("No WMID device detection method found\n");
1997                 return -ENODEV;
1998         }
1999
2000         if (wmi_has_guid(AMW0_GUID1) && !wmi_has_guid(WMID_GUID1)) {
2001                 interface = &AMW0_interface;
2002
2003                 if (ACPI_FAILURE(AMW0_set_capabilities())) {
2004                         pr_err("Unable to detect available AMW0 devices\n");
2005                         return -ENODEV;
2006                 }
2007         }
2008
2009         if (wmi_has_guid(AMW0_GUID1))
2010                 AMW0_find_mailled();
2011
2012         if (!interface) {
2013                 pr_err("No or unsupported WMI interface, unable to load\n");
2014                 return -ENODEV;
2015         }
2016
2017         set_quirks();
2018
2019         if (acpi_video_backlight_support()) {
2020                 interface->capability &= ~ACER_CAP_BRIGHTNESS;
2021                 pr_info("Brightness must be controlled by generic video driver\n");
2022         }
2023
2024         if (wmi_has_guid(WMID_GUID3)) {
2025                 if (ec_raw_mode) {
2026                         if (ACPI_FAILURE(acer_wmi_enable_ec_raw())) {
2027                                 pr_err("Cannot enable EC raw mode\n");
2028                                 return -ENODEV;
2029                         }
2030                 } else if (ACPI_FAILURE(acer_wmi_enable_lm())) {
2031                         pr_err("Cannot enable Launch Manager mode\n");
2032                         return -ENODEV;
2033                 }
2034         } else if (ec_raw_mode) {
2035                 pr_info("No WMID EC raw mode enable method\n");
2036         }
2037
2038         if (wmi_has_guid(ACERWMID_EVENT_GUID)) {
2039                 err = acer_wmi_input_setup();
2040                 if (err)
2041                         return err;
2042         }
2043
2044         err = platform_driver_register(&acer_platform_driver);
2045         if (err) {
2046                 pr_err("Unable to register platform driver\n");
2047                 goto error_platform_register;
2048         }
2049
2050         acer_platform_device = platform_device_alloc("acer-wmi", -1);
2051         if (!acer_platform_device) {
2052                 err = -ENOMEM;
2053                 goto error_device_alloc;
2054         }
2055
2056         err = platform_device_add(acer_platform_device);
2057         if (err)
2058                 goto error_device_add;
2059
2060         err = create_sysfs();
2061         if (err)
2062                 goto error_create_sys;
2063
2064         if (wmi_has_guid(WMID_GUID2)) {
2065                 interface->debug.wmid_devices = get_wmid_devices();
2066                 err = create_debugfs();
2067                 if (err)
2068                         goto error_create_debugfs;
2069         }
2070
2071         /* Override any initial settings with values from the commandline */
2072         acer_commandline_init();
2073
2074         return 0;
2075
2076 error_create_debugfs:
2077         remove_sysfs(acer_platform_device);
2078 error_create_sys:
2079         platform_device_del(acer_platform_device);
2080 error_device_add:
2081         platform_device_put(acer_platform_device);
2082 error_device_alloc:
2083         platform_driver_unregister(&acer_platform_driver);
2084 error_platform_register:
2085         if (wmi_has_guid(ACERWMID_EVENT_GUID))
2086                 acer_wmi_input_destroy();
2087
2088         return err;
2089 }
2090
2091 static void __exit acer_wmi_exit(void)
2092 {
2093         if (wmi_has_guid(ACERWMID_EVENT_GUID))
2094                 acer_wmi_input_destroy();
2095
2096         remove_sysfs(acer_platform_device);
2097         remove_debugfs();
2098         platform_device_unregister(acer_platform_device);
2099         platform_driver_unregister(&acer_platform_driver);
2100
2101         pr_info("Acer Laptop WMI Extras unloaded\n");
2102         return;
2103 }
2104
2105 module_init(acer_wmi_init);
2106 module_exit(acer_wmi_exit);