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