ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / platform / x86 / acer-wmi.c
blob2080b223c74a9d1bc91faa71eeb36d6a46995664
1 /*
2 * Acer WMI Laptop Extras
4 * Copyright (C) 2007-2009 Carlos Corbacho <carlos@strangeworlds.co.uk>
6 * Based on acer_acpi:
7 * Copyright (C) 2005-2007 E.M. Smith
8 * Copyright (C) 2007-2008 Carlos Corbacho <cathectic@gmail.com>
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.
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.
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
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
45 #include <acpi/acpi_drivers.h>
47 MODULE_AUTHOR("Carlos Corbacho");
48 MODULE_DESCRIPTION("Acer Laptop WMI Extras Driver");
49 MODULE_LICENSE("GPL");
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)
56 #define ACER_AMW0_WRITE 0x9610
59 * Bit masks for the AMW0 interface
61 #define ACER_AMW0_WIRELESS_MASK 0x35
62 #define ACER_AMW0_BLUETOOTH_MASK 0x34
63 #define ACER_AMW0_MAILLED_MASK 0x31
66 * Method IDs for WMID interface
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
78 * Acer ACPI method GUIDs
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"
87 * Acer ACPI event GUIDs
89 #define ACERWMID_EVENT_GUID "676AA15E-6A47-4D9F-A2CC-1E6D18D14026"
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");
95 enum acer_wmi_event_ids {
96 WMID_HOTKEY_EVENT = 0x1,
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, 0x12, {KEY_BLUETOOTH} }, /* BT */
103 {KE_KEY, 0x21, {KEY_PROG1} }, /* Backup */
104 {KE_KEY, 0x22, {KEY_PROG2} }, /* Arcade */
105 {KE_KEY, 0x23, {KEY_PROG3} }, /* P_Key */
106 {KE_KEY, 0x24, {KEY_PROG4} }, /* Social networking_Key */
107 {KE_IGNORE, 0x41, {KEY_MUTE} },
108 {KE_IGNORE, 0x42, {KEY_PREVIOUSSONG} },
109 {KE_IGNORE, 0x43, {KEY_NEXTSONG} },
110 {KE_IGNORE, 0x44, {KEY_PLAYPAUSE} },
111 {KE_IGNORE, 0x45, {KEY_STOP} },
112 {KE_IGNORE, 0x48, {KEY_VOLUMEUP} },
113 {KE_IGNORE, 0x49, {KEY_VOLUMEDOWN} },
114 {KE_IGNORE, 0x61, {KEY_SWITCHVIDEOMODE} },
115 {KE_IGNORE, 0x62, {KEY_BRIGHTNESSUP} },
116 {KE_IGNORE, 0x63, {KEY_BRIGHTNESSDOWN} },
117 {KE_KEY, 0x64, {KEY_SWITCHVIDEOMODE} }, /* Display Switch */
118 {KE_IGNORE, 0x81, {KEY_SLEEP} },
119 {KE_KEY, 0x82, {KEY_TOUCHPAD_TOGGLE} }, /* Touch Pad On/Off */
120 {KE_IGNORE, 0x83, {KEY_TOUCHPAD_TOGGLE} },
121 {KE_END, 0}
124 static struct input_dev *acer_wmi_input_dev;
126 struct event_return_value {
127 u8 function;
128 u8 key_num;
129 u16 device_state;
130 u32 reserved;
131 } __attribute__((packed));
134 * GUID3 Get Device Status device flags
136 #define ACER_WMID3_GDS_WIRELESS (1<<0) /* WiFi */
137 #define ACER_WMID3_GDS_THREEG (1<<6) /* 3G */
138 #define ACER_WMID3_GDS_WIMAX (1<<7) /* WiMAX */
139 #define ACER_WMID3_GDS_BLUETOOTH (1<<11) /* BT */
141 struct lm_input_params {
142 u8 function_num; /* Function Number */
143 u16 commun_devices; /* Communication type devices default status */
144 u16 devices; /* Other type devices default status */
145 u8 lm_status; /* Launch Manager Status */
146 u16 reserved;
147 } __attribute__((packed));
149 struct lm_return_value {
150 u8 error_code; /* Error Code */
151 u8 ec_return_value; /* EC Return Value */
152 u16 reserved;
153 } __attribute__((packed));
155 struct wmid3_gds_input_param { /* Get Device Status input parameter */
156 u8 function_num; /* Function Number */
157 u8 hotkey_number; /* Hotkey Number */
158 u16 devices; /* Get Device */
159 } __attribute__((packed));
161 struct wmid3_gds_return_value { /* Get Device Status return value*/
162 u8 error_code; /* Error Code */
163 u8 ec_return_value; /* EC Return Value */
164 u16 devices; /* Current Device Status */
165 u32 reserved;
166 } __attribute__((packed));
168 struct hotkey_function_type_aa {
169 u8 type;
170 u8 length;
171 u16 handle;
172 u16 commun_func_bitmap;
173 } __attribute__((packed));
176 * Interface capability flags
178 #define ACER_CAP_MAILLED (1<<0)
179 #define ACER_CAP_WIRELESS (1<<1)
180 #define ACER_CAP_BLUETOOTH (1<<2)
181 #define ACER_CAP_BRIGHTNESS (1<<3)
182 #define ACER_CAP_THREEG (1<<4)
183 #define ACER_CAP_ANY (0xFFFFFFFF)
186 * Interface type flags
188 enum interface_flags {
189 ACER_AMW0,
190 ACER_AMW0_V2,
191 ACER_WMID,
194 #define ACER_DEFAULT_WIRELESS 0
195 #define ACER_DEFAULT_BLUETOOTH 0
196 #define ACER_DEFAULT_MAILLED 0
197 #define ACER_DEFAULT_THREEG 0
199 static int max_brightness = 0xF;
201 static int mailled = -1;
202 static int brightness = -1;
203 static int threeg = -1;
204 static int force_series;
205 static bool ec_raw_mode;
206 static bool has_type_aa;
208 module_param(mailled, int, 0444);
209 module_param(brightness, int, 0444);
210 module_param(threeg, int, 0444);
211 module_param(force_series, int, 0444);
212 module_param(ec_raw_mode, bool, 0444);
213 MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
214 MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
215 MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
216 MODULE_PARM_DESC(force_series, "Force a different laptop series");
217 MODULE_PARM_DESC(ec_raw_mode, "Enable EC raw mode");
219 struct acer_data {
220 int mailled;
221 int threeg;
222 int brightness;
225 struct acer_debug {
226 struct dentry *root;
227 struct dentry *devices;
228 u32 wmid_devices;
231 static struct rfkill *wireless_rfkill;
232 static struct rfkill *bluetooth_rfkill;
233 static struct rfkill *threeg_rfkill;
234 static bool rfkill_inited;
236 /* Each low-level interface must define at least some of the following */
237 struct wmi_interface {
238 /* The WMI device type */
239 u32 type;
241 /* The capabilities this interface provides */
242 u32 capability;
244 /* Private data for the current interface */
245 struct acer_data data;
247 /* debugfs entries associated with this interface */
248 struct acer_debug debug;
251 /* The static interface pointer, points to the currently detected interface */
252 static struct wmi_interface *interface;
255 * Embedded Controller quirks
256 * Some laptops require us to directly access the EC to either enable or query
257 * features that are not available through WMI.
260 struct quirk_entry {
261 u8 wireless;
262 u8 mailled;
263 s8 brightness;
264 u8 bluetooth;
267 static struct quirk_entry *quirks;
269 static void set_quirks(void)
271 if (!interface)
272 return;
274 if (quirks->mailled)
275 interface->capability |= ACER_CAP_MAILLED;
277 if (quirks->brightness)
278 interface->capability |= ACER_CAP_BRIGHTNESS;
281 static int dmi_matched(const struct dmi_system_id *dmi)
283 quirks = dmi->driver_data;
284 return 1;
287 static struct quirk_entry quirk_unknown = {
290 static struct quirk_entry quirk_acer_aspire_1520 = {
291 .brightness = -1,
294 static struct quirk_entry quirk_acer_travelmate_2490 = {
295 .mailled = 1,
298 /* This AMW0 laptop has no bluetooth */
299 static struct quirk_entry quirk_medion_md_98300 = {
300 .wireless = 1,
303 static struct quirk_entry quirk_fujitsu_amilo_li_1718 = {
304 .wireless = 2,
307 static struct quirk_entry quirk_lenovo_ideapad_s205 = {
308 .wireless = 3,
311 /* The Aspire One has a dummy ACPI-WMI interface - disable it */
312 static struct dmi_system_id __devinitdata acer_blacklist[] = {
314 .ident = "Acer Aspire One (SSD)",
315 .matches = {
316 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
317 DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
321 .ident = "Acer Aspire One (HDD)",
322 .matches = {
323 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
324 DMI_MATCH(DMI_PRODUCT_NAME, "AOA150"),
330 static struct dmi_system_id acer_quirks[] = {
332 .callback = dmi_matched,
333 .ident = "Acer Aspire 1360",
334 .matches = {
335 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
336 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"),
338 .driver_data = &quirk_acer_aspire_1520,
341 .callback = dmi_matched,
342 .ident = "Acer Aspire 1520",
343 .matches = {
344 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
345 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"),
347 .driver_data = &quirk_acer_aspire_1520,
350 .callback = dmi_matched,
351 .ident = "Acer Aspire 3100",
352 .matches = {
353 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
354 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"),
356 .driver_data = &quirk_acer_travelmate_2490,
359 .callback = dmi_matched,
360 .ident = "Acer Aspire 3610",
361 .matches = {
362 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
363 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3610"),
365 .driver_data = &quirk_acer_travelmate_2490,
368 .callback = dmi_matched,
369 .ident = "Acer Aspire 5100",
370 .matches = {
371 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
372 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"),
374 .driver_data = &quirk_acer_travelmate_2490,
377 .callback = dmi_matched,
378 .ident = "Acer Aspire 5610",
379 .matches = {
380 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
381 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5610"),
383 .driver_data = &quirk_acer_travelmate_2490,
386 .callback = dmi_matched,
387 .ident = "Acer Aspire 5630",
388 .matches = {
389 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
390 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"),
392 .driver_data = &quirk_acer_travelmate_2490,
395 .callback = dmi_matched,
396 .ident = "Acer Aspire 5650",
397 .matches = {
398 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
399 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"),
401 .driver_data = &quirk_acer_travelmate_2490,
404 .callback = dmi_matched,
405 .ident = "Acer Aspire 5680",
406 .matches = {
407 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
408 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"),
410 .driver_data = &quirk_acer_travelmate_2490,
413 .callback = dmi_matched,
414 .ident = "Acer Aspire 9110",
415 .matches = {
416 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
417 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 9110"),
419 .driver_data = &quirk_acer_travelmate_2490,
422 .callback = dmi_matched,
423 .ident = "Acer TravelMate 2490",
424 .matches = {
425 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
426 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"),
428 .driver_data = &quirk_acer_travelmate_2490,
431 .callback = dmi_matched,
432 .ident = "Acer TravelMate 4200",
433 .matches = {
434 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
435 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4200"),
437 .driver_data = &quirk_acer_travelmate_2490,
440 .callback = dmi_matched,
441 .ident = "Fujitsu Siemens Amilo Li 1718",
442 .matches = {
443 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
444 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"),
446 .driver_data = &quirk_fujitsu_amilo_li_1718,
449 .callback = dmi_matched,
450 .ident = "Medion MD 98300",
451 .matches = {
452 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
453 DMI_MATCH(DMI_PRODUCT_NAME, "WAM2030"),
455 .driver_data = &quirk_medion_md_98300,
458 .callback = dmi_matched,
459 .ident = "Lenovo Ideapad S205",
460 .matches = {
461 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
462 DMI_MATCH(DMI_PRODUCT_NAME, "10382LG"),
464 .driver_data = &quirk_lenovo_ideapad_s205,
467 .callback = dmi_matched,
468 .ident = "Lenovo 3000 N200",
469 .matches = {
470 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
471 DMI_MATCH(DMI_PRODUCT_NAME, "0687A31"),
473 .driver_data = &quirk_fujitsu_amilo_li_1718,
478 /* Find which quirks are needed for a particular vendor/ model pair */
479 static void find_quirks(void)
481 if (!force_series) {
482 dmi_check_system(acer_quirks);
483 } else if (force_series == 2490) {
484 quirks = &quirk_acer_travelmate_2490;
487 if (quirks == NULL)
488 quirks = &quirk_unknown;
490 set_quirks();
494 * General interface convenience methods
497 static bool has_cap(u32 cap)
499 if ((interface->capability & cap) != 0)
500 return 1;
502 return 0;
506 * AMW0 (V1) interface
508 struct wmab_args {
509 u32 eax;
510 u32 ebx;
511 u32 ecx;
512 u32 edx;
515 struct wmab_ret {
516 u32 eax;
517 u32 ebx;
518 u32 ecx;
519 u32 edx;
520 u32 eex;
523 static acpi_status wmab_execute(struct wmab_args *regbuf,
524 struct acpi_buffer *result)
526 struct acpi_buffer input;
527 acpi_status status;
528 input.length = sizeof(struct wmab_args);
529 input.pointer = (u8 *)regbuf;
531 status = wmi_evaluate_method(AMW0_GUID1, 1, 1, &input, result);
533 return status;
536 static acpi_status AMW0_get_u32(u32 *value, u32 cap,
537 struct wmi_interface *iface)
539 int err;
540 u8 result;
542 switch (cap) {
543 case ACER_CAP_MAILLED:
544 switch (quirks->mailled) {
545 default:
546 err = ec_read(0xA, &result);
547 if (err)
548 return AE_ERROR;
549 *value = (result >> 7) & 0x1;
550 return AE_OK;
552 break;
553 case ACER_CAP_WIRELESS:
554 switch (quirks->wireless) {
555 case 1:
556 err = ec_read(0x7B, &result);
557 if (err)
558 return AE_ERROR;
559 *value = result & 0x1;
560 return AE_OK;
561 case 2:
562 err = ec_read(0x71, &result);
563 if (err)
564 return AE_ERROR;
565 *value = result & 0x1;
566 return AE_OK;
567 case 3:
568 err = ec_read(0x78, &result);
569 if (err)
570 return AE_ERROR;
571 *value = result & 0x1;
572 return AE_OK;
573 default:
574 err = ec_read(0xA, &result);
575 if (err)
576 return AE_ERROR;
577 *value = (result >> 2) & 0x1;
578 return AE_OK;
580 break;
581 case ACER_CAP_BLUETOOTH:
582 switch (quirks->bluetooth) {
583 default:
584 err = ec_read(0xA, &result);
585 if (err)
586 return AE_ERROR;
587 *value = (result >> 4) & 0x1;
588 return AE_OK;
590 break;
591 case ACER_CAP_BRIGHTNESS:
592 switch (quirks->brightness) {
593 default:
594 err = ec_read(0x83, &result);
595 if (err)
596 return AE_ERROR;
597 *value = result;
598 return AE_OK;
600 break;
601 default:
602 return AE_ERROR;
604 return AE_OK;
607 static acpi_status AMW0_set_u32(u32 value, u32 cap, struct wmi_interface *iface)
609 struct wmab_args args;
611 args.eax = ACER_AMW0_WRITE;
612 args.ebx = value ? (1<<8) : 0;
613 args.ecx = args.edx = 0;
615 switch (cap) {
616 case ACER_CAP_MAILLED:
617 if (value > 1)
618 return AE_BAD_PARAMETER;
619 args.ebx |= ACER_AMW0_MAILLED_MASK;
620 break;
621 case ACER_CAP_WIRELESS:
622 if (value > 1)
623 return AE_BAD_PARAMETER;
624 args.ebx |= ACER_AMW0_WIRELESS_MASK;
625 break;
626 case ACER_CAP_BLUETOOTH:
627 if (value > 1)
628 return AE_BAD_PARAMETER;
629 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
630 break;
631 case ACER_CAP_BRIGHTNESS:
632 if (value > max_brightness)
633 return AE_BAD_PARAMETER;
634 switch (quirks->brightness) {
635 default:
636 return ec_write(0x83, value);
637 break;
639 default:
640 return AE_ERROR;
643 /* Actually do the set */
644 return wmab_execute(&args, NULL);
647 static acpi_status AMW0_find_mailled(void)
649 struct wmab_args args;
650 struct wmab_ret ret;
651 acpi_status status = AE_OK;
652 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
653 union acpi_object *obj;
655 args.eax = 0x86;
656 args.ebx = args.ecx = args.edx = 0;
658 status = wmab_execute(&args, &out);
659 if (ACPI_FAILURE(status))
660 return status;
662 obj = (union acpi_object *) out.pointer;
663 if (obj && obj->type == ACPI_TYPE_BUFFER &&
664 obj->buffer.length == sizeof(struct wmab_ret)) {
665 ret = *((struct wmab_ret *) obj->buffer.pointer);
666 } else {
667 kfree(out.pointer);
668 return AE_ERROR;
671 if (ret.eex & 0x1)
672 interface->capability |= ACER_CAP_MAILLED;
674 kfree(out.pointer);
676 return AE_OK;
679 static int AMW0_set_cap_acpi_check_device_found;
681 static acpi_status AMW0_set_cap_acpi_check_device_cb(acpi_handle handle,
682 u32 level, void *context, void **retval)
684 AMW0_set_cap_acpi_check_device_found = 1;
685 return AE_OK;
688 static const struct acpi_device_id norfkill_ids[] = {
689 { "VPC2004", 0},
690 { "IBM0068", 0},
691 { "LEN0068", 0},
692 { "SNY5001", 0}, /* sony-laptop in charge */
693 { "", 0},
696 static int AMW0_set_cap_acpi_check_device(void)
698 const struct acpi_device_id *id;
700 for (id = norfkill_ids; id->id[0]; id++)
701 acpi_get_devices(id->id, AMW0_set_cap_acpi_check_device_cb,
702 NULL, NULL);
703 return AMW0_set_cap_acpi_check_device_found;
706 static acpi_status AMW0_set_capabilities(void)
708 struct wmab_args args;
709 struct wmab_ret ret;
710 acpi_status status;
711 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
712 union acpi_object *obj;
715 * On laptops with this strange GUID (non Acer), normal probing doesn't
716 * work.
718 if (wmi_has_guid(AMW0_GUID2)) {
719 if ((quirks != &quirk_unknown) ||
720 !AMW0_set_cap_acpi_check_device())
721 interface->capability |= ACER_CAP_WIRELESS;
722 return AE_OK;
725 args.eax = ACER_AMW0_WRITE;
726 args.ecx = args.edx = 0;
728 args.ebx = 0xa2 << 8;
729 args.ebx |= ACER_AMW0_WIRELESS_MASK;
731 status = wmab_execute(&args, &out);
732 if (ACPI_FAILURE(status))
733 return status;
735 obj = out.pointer;
736 if (obj && obj->type == ACPI_TYPE_BUFFER &&
737 obj->buffer.length == sizeof(struct wmab_ret)) {
738 ret = *((struct wmab_ret *) obj->buffer.pointer);
739 } else {
740 status = AE_ERROR;
741 goto out;
744 if (ret.eax & 0x1)
745 interface->capability |= ACER_CAP_WIRELESS;
747 args.ebx = 2 << 8;
748 args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
751 * It's ok to use existing buffer for next wmab_execute call.
752 * But we need to kfree(out.pointer) if next wmab_execute fail.
754 status = wmab_execute(&args, &out);
755 if (ACPI_FAILURE(status))
756 goto out;
758 obj = (union acpi_object *) out.pointer;
759 if (obj && obj->type == ACPI_TYPE_BUFFER
760 && obj->buffer.length == sizeof(struct wmab_ret)) {
761 ret = *((struct wmab_ret *) obj->buffer.pointer);
762 } else {
763 status = AE_ERROR;
764 goto out;
767 if (ret.eax & 0x1)
768 interface->capability |= ACER_CAP_BLUETOOTH;
771 * This appears to be safe to enable, since all Wistron based laptops
772 * appear to use the same EC register for brightness, even if they
773 * differ for wireless, etc
775 if (quirks->brightness >= 0)
776 interface->capability |= ACER_CAP_BRIGHTNESS;
778 status = AE_OK;
779 out:
780 kfree(out.pointer);
781 return status;
784 static struct wmi_interface AMW0_interface = {
785 .type = ACER_AMW0,
788 static struct wmi_interface AMW0_V2_interface = {
789 .type = ACER_AMW0_V2,
793 * New interface (The WMID interface)
795 static acpi_status
796 WMI_execute_u32(u32 method_id, u32 in, u32 *out)
798 struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
799 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
800 union acpi_object *obj;
801 u32 tmp;
802 acpi_status status;
804 status = wmi_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);
806 if (ACPI_FAILURE(status))
807 return status;
809 obj = (union acpi_object *) result.pointer;
810 if (obj && obj->type == ACPI_TYPE_BUFFER &&
811 (obj->buffer.length == sizeof(u32) ||
812 obj->buffer.length == sizeof(u64))) {
813 tmp = *((u32 *) obj->buffer.pointer);
814 } else if (obj->type == ACPI_TYPE_INTEGER) {
815 tmp = (u32) obj->integer.value;
816 } else {
817 tmp = 0;
820 if (out)
821 *out = tmp;
823 kfree(result.pointer);
825 return status;
828 static acpi_status WMID_get_u32(u32 *value, u32 cap,
829 struct wmi_interface *iface)
831 acpi_status status;
832 u8 tmp;
833 u32 result, method_id = 0;
835 switch (cap) {
836 case ACER_CAP_WIRELESS:
837 method_id = ACER_WMID_GET_WIRELESS_METHODID;
838 break;
839 case ACER_CAP_BLUETOOTH:
840 method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
841 break;
842 case ACER_CAP_BRIGHTNESS:
843 method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
844 break;
845 case ACER_CAP_THREEG:
846 method_id = ACER_WMID_GET_THREEG_METHODID;
847 break;
848 case ACER_CAP_MAILLED:
849 if (quirks->mailled == 1) {
850 ec_read(0x9f, &tmp);
851 *value = tmp & 0x1;
852 return 0;
854 default:
855 return AE_ERROR;
857 status = WMI_execute_u32(method_id, 0, &result);
859 if (ACPI_SUCCESS(status))
860 *value = (u8)result;
862 return status;
865 static acpi_status WMID_set_u32(u32 value, u32 cap, struct wmi_interface *iface)
867 u32 method_id = 0;
868 char param;
870 switch (cap) {
871 case ACER_CAP_BRIGHTNESS:
872 if (value > max_brightness)
873 return AE_BAD_PARAMETER;
874 method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
875 break;
876 case ACER_CAP_WIRELESS:
877 if (value > 1)
878 return AE_BAD_PARAMETER;
879 method_id = ACER_WMID_SET_WIRELESS_METHODID;
880 break;
881 case ACER_CAP_BLUETOOTH:
882 if (value > 1)
883 return AE_BAD_PARAMETER;
884 method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
885 break;
886 case ACER_CAP_THREEG:
887 if (value > 1)
888 return AE_BAD_PARAMETER;
889 method_id = ACER_WMID_SET_THREEG_METHODID;
890 break;
891 case ACER_CAP_MAILLED:
892 if (value > 1)
893 return AE_BAD_PARAMETER;
894 if (quirks->mailled == 1) {
895 param = value ? 0x92 : 0x93;
896 i8042_lock_chip();
897 i8042_command(&param, 0x1059);
898 i8042_unlock_chip();
899 return 0;
901 break;
902 default:
903 return AE_ERROR;
905 return WMI_execute_u32(method_id, (u32)value, NULL);
908 static void type_aa_dmi_decode(const struct dmi_header *header, void *dummy)
910 struct hotkey_function_type_aa *type_aa;
912 /* We are looking for OEM-specific Type AAh */
913 if (header->type != 0xAA)
914 return;
916 has_type_aa = true;
917 type_aa = (struct hotkey_function_type_aa *) header;
919 pr_info("Function bitmap for Communication Button: 0x%x\n",
920 type_aa->commun_func_bitmap);
922 if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_WIRELESS)
923 interface->capability |= ACER_CAP_WIRELESS;
924 if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_THREEG)
925 interface->capability |= ACER_CAP_THREEG;
926 if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_BLUETOOTH)
927 interface->capability |= ACER_CAP_BLUETOOTH;
930 static acpi_status WMID_set_capabilities(void)
932 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
933 union acpi_object *obj;
934 acpi_status status;
935 u32 devices;
937 status = wmi_query_block(WMID_GUID2, 1, &out);
938 if (ACPI_FAILURE(status))
939 return status;
941 obj = (union acpi_object *) out.pointer;
942 if (obj && obj->type == ACPI_TYPE_BUFFER &&
943 (obj->buffer.length == sizeof(u32) ||
944 obj->buffer.length == sizeof(u64))) {
945 devices = *((u32 *) obj->buffer.pointer);
946 } else if (obj->type == ACPI_TYPE_INTEGER) {
947 devices = (u32) obj->integer.value;
948 } else {
949 kfree(out.pointer);
950 return AE_ERROR;
953 dmi_walk(type_aa_dmi_decode, NULL);
954 if (!has_type_aa) {
955 interface->capability |= ACER_CAP_WIRELESS;
956 if (devices & 0x40)
957 interface->capability |= ACER_CAP_THREEG;
958 if (devices & 0x10)
959 interface->capability |= ACER_CAP_BLUETOOTH;
962 /* WMID always provides brightness methods */
963 interface->capability |= ACER_CAP_BRIGHTNESS;
965 if (!(devices & 0x20))
966 max_brightness = 0x9;
968 kfree(out.pointer);
969 return status;
972 static struct wmi_interface wmid_interface = {
973 .type = ACER_WMID,
977 * Generic Device (interface-independent)
980 static acpi_status get_u32(u32 *value, u32 cap)
982 acpi_status status = AE_ERROR;
984 switch (interface->type) {
985 case ACER_AMW0:
986 status = AMW0_get_u32(value, cap, interface);
987 break;
988 case ACER_AMW0_V2:
989 if (cap == ACER_CAP_MAILLED) {
990 status = AMW0_get_u32(value, cap, interface);
991 break;
993 case ACER_WMID:
994 status = WMID_get_u32(value, cap, interface);
995 break;
998 return status;
1001 static acpi_status set_u32(u32 value, u32 cap)
1003 acpi_status status;
1005 if (interface->capability & cap) {
1006 switch (interface->type) {
1007 case ACER_AMW0:
1008 return AMW0_set_u32(value, cap, interface);
1009 case ACER_AMW0_V2:
1010 if (cap == ACER_CAP_MAILLED)
1011 return AMW0_set_u32(value, cap, interface);
1014 * On some models, some WMID methods don't toggle
1015 * properly. For those cases, we want to run the AMW0
1016 * method afterwards to be certain we've really toggled
1017 * the device state.
1019 if (cap == ACER_CAP_WIRELESS ||
1020 cap == ACER_CAP_BLUETOOTH) {
1021 status = WMID_set_u32(value, cap, interface);
1022 if (ACPI_FAILURE(status))
1023 return status;
1025 return AMW0_set_u32(value, cap, interface);
1027 case ACER_WMID:
1028 return WMID_set_u32(value, cap, interface);
1029 default:
1030 return AE_BAD_PARAMETER;
1033 return AE_BAD_PARAMETER;
1036 static void __init acer_commandline_init(void)
1039 * These will all fail silently if the value given is invalid, or the
1040 * capability isn't available on the given interface
1042 if (mailled >= 0)
1043 set_u32(mailled, ACER_CAP_MAILLED);
1044 if (!has_type_aa && threeg >= 0)
1045 set_u32(threeg, ACER_CAP_THREEG);
1046 if (brightness >= 0)
1047 set_u32(brightness, ACER_CAP_BRIGHTNESS);
1051 * LED device (Mail LED only, no other LEDs known yet)
1053 static void mail_led_set(struct led_classdev *led_cdev,
1054 enum led_brightness value)
1056 set_u32(value, ACER_CAP_MAILLED);
1059 static struct led_classdev mail_led = {
1060 .name = "acer-wmi::mail",
1061 .brightness_set = mail_led_set,
1064 static int __devinit acer_led_init(struct device *dev)
1066 return led_classdev_register(dev, &mail_led);
1069 static void acer_led_exit(void)
1071 set_u32(LED_OFF, ACER_CAP_MAILLED);
1072 led_classdev_unregister(&mail_led);
1076 * Backlight device
1078 static struct backlight_device *acer_backlight_device;
1080 static int read_brightness(struct backlight_device *bd)
1082 u32 value;
1083 get_u32(&value, ACER_CAP_BRIGHTNESS);
1084 return value;
1087 static int update_bl_status(struct backlight_device *bd)
1089 int intensity = bd->props.brightness;
1091 if (bd->props.power != FB_BLANK_UNBLANK)
1092 intensity = 0;
1093 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
1094 intensity = 0;
1096 set_u32(intensity, ACER_CAP_BRIGHTNESS);
1098 return 0;
1101 static const struct backlight_ops acer_bl_ops = {
1102 .get_brightness = read_brightness,
1103 .update_status = update_bl_status,
1106 static int __devinit acer_backlight_init(struct device *dev)
1108 struct backlight_properties props;
1109 struct backlight_device *bd;
1111 memset(&props, 0, sizeof(struct backlight_properties));
1112 props.type = BACKLIGHT_PLATFORM;
1113 props.max_brightness = max_brightness;
1114 bd = backlight_device_register("acer-wmi", dev, NULL, &acer_bl_ops,
1115 &props);
1116 if (IS_ERR(bd)) {
1117 pr_err("Could not register Acer backlight device\n");
1118 acer_backlight_device = NULL;
1119 return PTR_ERR(bd);
1122 acer_backlight_device = bd;
1124 bd->props.power = FB_BLANK_UNBLANK;
1125 bd->props.brightness = read_brightness(bd);
1126 backlight_update_status(bd);
1127 return 0;
1130 static void acer_backlight_exit(void)
1132 backlight_device_unregister(acer_backlight_device);
1135 static acpi_status wmid3_get_device_status(u32 *value, u16 device)
1137 struct wmid3_gds_return_value return_value;
1138 acpi_status status;
1139 union acpi_object *obj;
1140 struct wmid3_gds_input_param params = {
1141 .function_num = 0x1,
1142 .hotkey_number = 0x01,
1143 .devices = device,
1145 struct acpi_buffer input = {
1146 sizeof(struct wmid3_gds_input_param),
1147 &params
1149 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1151 status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &input, &output);
1152 if (ACPI_FAILURE(status))
1153 return status;
1155 obj = output.pointer;
1157 if (!obj)
1158 return AE_ERROR;
1159 else if (obj->type != ACPI_TYPE_BUFFER) {
1160 kfree(obj);
1161 return AE_ERROR;
1163 if (obj->buffer.length != 8) {
1164 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1165 kfree(obj);
1166 return AE_ERROR;
1169 return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
1170 kfree(obj);
1172 if (return_value.error_code || return_value.ec_return_value)
1173 pr_warn("Get Device Status failed: 0x%x - 0x%x\n",
1174 return_value.error_code,
1175 return_value.ec_return_value);
1176 else
1177 *value = !!(return_value.devices & device);
1179 return status;
1182 static acpi_status get_device_status(u32 *value, u32 cap)
1184 if (wmi_has_guid(WMID_GUID3)) {
1185 u16 device;
1187 switch (cap) {
1188 case ACER_CAP_WIRELESS:
1189 device = ACER_WMID3_GDS_WIRELESS;
1190 break;
1191 case ACER_CAP_BLUETOOTH:
1192 device = ACER_WMID3_GDS_BLUETOOTH;
1193 break;
1194 case ACER_CAP_THREEG:
1195 device = ACER_WMID3_GDS_THREEG;
1196 break;
1197 default:
1198 return AE_ERROR;
1200 return wmid3_get_device_status(value, device);
1202 } else {
1203 return get_u32(value, cap);
1207 static acpi_status wmid3_set_device_status(u32 value, u16 device)
1209 struct wmid3_gds_return_value return_value;
1210 acpi_status status;
1211 union acpi_object *obj;
1212 u16 devices;
1213 struct wmid3_gds_input_param params = {
1214 .function_num = 0x1,
1215 .hotkey_number = 0x01,
1216 .devices = ACER_WMID3_GDS_WIRELESS |
1217 ACER_WMID3_GDS_THREEG |
1218 ACER_WMID3_GDS_WIMAX |
1219 ACER_WMID3_GDS_BLUETOOTH,
1221 struct acpi_buffer input = {
1222 sizeof(struct wmid3_gds_input_param),
1223 &params
1225 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1226 struct acpi_buffer output2 = { ACPI_ALLOCATE_BUFFER, NULL };
1228 status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &input, &output);
1229 if (ACPI_FAILURE(status))
1230 return status;
1232 obj = output.pointer;
1234 if (!obj)
1235 return AE_ERROR;
1236 else if (obj->type != ACPI_TYPE_BUFFER) {
1237 kfree(obj);
1238 return AE_ERROR;
1240 if (obj->buffer.length != 8) {
1241 pr_warning("Unknown buffer length %d\n", obj->buffer.length);
1242 kfree(obj);
1243 return AE_ERROR;
1246 return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
1247 kfree(obj);
1249 if (return_value.error_code || return_value.ec_return_value) {
1250 pr_warning("Get Current Device Status failed: "
1251 "0x%x - 0x%x\n", return_value.error_code,
1252 return_value.ec_return_value);
1253 return status;
1256 devices = return_value.devices;
1257 params.function_num = 0x2;
1258 params.hotkey_number = 0x01;
1259 params.devices = (value) ? (devices | device) : (devices & ~device);
1261 status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &input, &output2);
1262 if (ACPI_FAILURE(status))
1263 return status;
1265 obj = output2.pointer;
1267 if (!obj)
1268 return AE_ERROR;
1269 else if (obj->type != ACPI_TYPE_BUFFER) {
1270 kfree(obj);
1271 return AE_ERROR;
1273 if (obj->buffer.length != 4) {
1274 pr_warning("Unknown buffer length %d\n", obj->buffer.length);
1275 kfree(obj);
1276 return AE_ERROR;
1279 return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
1280 kfree(obj);
1282 if (return_value.error_code || return_value.ec_return_value)
1283 pr_warning("Set Device Status failed: "
1284 "0x%x - 0x%x\n", return_value.error_code,
1285 return_value.ec_return_value);
1287 return status;
1290 static acpi_status set_device_status(u32 value, u32 cap)
1292 if (wmi_has_guid(WMID_GUID3)) {
1293 u16 device;
1295 switch (cap) {
1296 case ACER_CAP_WIRELESS:
1297 device = ACER_WMID3_GDS_WIRELESS;
1298 break;
1299 case ACER_CAP_BLUETOOTH:
1300 device = ACER_WMID3_GDS_BLUETOOTH;
1301 break;
1302 case ACER_CAP_THREEG:
1303 device = ACER_WMID3_GDS_THREEG;
1304 break;
1305 default:
1306 return AE_ERROR;
1308 return wmid3_set_device_status(value, device);
1310 } else {
1311 return set_u32(value, cap);
1316 * Rfkill devices
1318 static void acer_rfkill_update(struct work_struct *ignored);
1319 static DECLARE_DELAYED_WORK(acer_rfkill_work, acer_rfkill_update);
1320 static void acer_rfkill_update(struct work_struct *ignored)
1322 u32 state;
1323 acpi_status status;
1325 if (has_cap(ACER_CAP_WIRELESS)) {
1326 status = get_u32(&state, ACER_CAP_WIRELESS);
1327 if (ACPI_SUCCESS(status)) {
1328 if (quirks->wireless == 3)
1329 rfkill_set_hw_state(wireless_rfkill, !state);
1330 else
1331 rfkill_set_sw_state(wireless_rfkill, !state);
1335 if (has_cap(ACER_CAP_BLUETOOTH)) {
1336 status = get_u32(&state, ACER_CAP_BLUETOOTH);
1337 if (ACPI_SUCCESS(status))
1338 rfkill_set_sw_state(bluetooth_rfkill, !state);
1341 if (has_cap(ACER_CAP_THREEG) && wmi_has_guid(WMID_GUID3)) {
1342 status = wmid3_get_device_status(&state,
1343 ACER_WMID3_GDS_THREEG);
1344 if (ACPI_SUCCESS(status))
1345 rfkill_set_sw_state(threeg_rfkill, !state);
1348 schedule_delayed_work(&acer_rfkill_work, round_jiffies_relative(HZ));
1351 static int acer_rfkill_set(void *data, bool blocked)
1353 acpi_status status;
1354 u32 cap = (unsigned long)data;
1356 if (rfkill_inited) {
1357 status = set_device_status(!blocked, cap);
1358 if (ACPI_FAILURE(status))
1359 return -ENODEV;
1362 return 0;
1365 static const struct rfkill_ops acer_rfkill_ops = {
1366 .set_block = acer_rfkill_set,
1369 static struct rfkill *acer_rfkill_register(struct device *dev,
1370 enum rfkill_type type,
1371 char *name, u32 cap)
1373 int err;
1374 struct rfkill *rfkill_dev;
1375 u32 state;
1376 acpi_status status;
1378 rfkill_dev = rfkill_alloc(name, dev, type,
1379 &acer_rfkill_ops,
1380 (void *)(unsigned long)cap);
1381 if (!rfkill_dev)
1382 return ERR_PTR(-ENOMEM);
1384 status = get_device_status(&state, cap);
1386 err = rfkill_register(rfkill_dev);
1387 if (err) {
1388 rfkill_destroy(rfkill_dev);
1389 return ERR_PTR(err);
1392 if (ACPI_SUCCESS(status))
1393 rfkill_set_sw_state(rfkill_dev, !state);
1395 return rfkill_dev;
1398 static int acer_rfkill_init(struct device *dev)
1400 int err;
1402 if (has_cap(ACER_CAP_WIRELESS)) {
1403 wireless_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_WLAN,
1404 "acer-wireless", ACER_CAP_WIRELESS);
1405 if (IS_ERR(wireless_rfkill)) {
1406 err = PTR_ERR(wireless_rfkill);
1407 goto error_wireless;
1411 if (has_cap(ACER_CAP_BLUETOOTH)) {
1412 bluetooth_rfkill = acer_rfkill_register(dev,
1413 RFKILL_TYPE_BLUETOOTH, "acer-bluetooth",
1414 ACER_CAP_BLUETOOTH);
1415 if (IS_ERR(bluetooth_rfkill)) {
1416 err = PTR_ERR(bluetooth_rfkill);
1417 goto error_bluetooth;
1421 if (has_cap(ACER_CAP_THREEG)) {
1422 threeg_rfkill = acer_rfkill_register(dev,
1423 RFKILL_TYPE_WWAN, "acer-threeg",
1424 ACER_CAP_THREEG);
1425 if (IS_ERR(threeg_rfkill)) {
1426 err = PTR_ERR(threeg_rfkill);
1427 goto error_threeg;
1431 rfkill_inited = true;
1433 if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
1434 has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1435 schedule_delayed_work(&acer_rfkill_work,
1436 round_jiffies_relative(HZ));
1438 return 0;
1440 error_threeg:
1441 if (has_cap(ACER_CAP_BLUETOOTH)) {
1442 rfkill_unregister(bluetooth_rfkill);
1443 rfkill_destroy(bluetooth_rfkill);
1445 error_bluetooth:
1446 if (has_cap(ACER_CAP_WIRELESS)) {
1447 rfkill_unregister(wireless_rfkill);
1448 rfkill_destroy(wireless_rfkill);
1450 error_wireless:
1451 return err;
1454 static void acer_rfkill_exit(void)
1456 if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
1457 has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1458 cancel_delayed_work_sync(&acer_rfkill_work);
1460 if (has_cap(ACER_CAP_WIRELESS)) {
1461 rfkill_unregister(wireless_rfkill);
1462 rfkill_destroy(wireless_rfkill);
1465 if (has_cap(ACER_CAP_BLUETOOTH)) {
1466 rfkill_unregister(bluetooth_rfkill);
1467 rfkill_destroy(bluetooth_rfkill);
1470 if (has_cap(ACER_CAP_THREEG)) {
1471 rfkill_unregister(threeg_rfkill);
1472 rfkill_destroy(threeg_rfkill);
1474 return;
1478 * sysfs interface
1480 static ssize_t show_bool_threeg(struct device *dev,
1481 struct device_attribute *attr, char *buf)
1483 u32 result; \
1484 acpi_status status;
1485 if (wmi_has_guid(WMID_GUID3))
1486 status = wmid3_get_device_status(&result,
1487 ACER_WMID3_GDS_THREEG);
1488 else
1489 status = get_u32(&result, ACER_CAP_THREEG);
1490 if (ACPI_SUCCESS(status))
1491 return sprintf(buf, "%u\n", result);
1492 return sprintf(buf, "Read error\n");
1495 static ssize_t set_bool_threeg(struct device *dev,
1496 struct device_attribute *attr, const char *buf, size_t count)
1498 u32 tmp = simple_strtoul(buf, NULL, 10);
1499 acpi_status status = set_u32(tmp, ACER_CAP_THREEG);
1500 if (ACPI_FAILURE(status))
1501 return -EINVAL;
1502 return count;
1504 static DEVICE_ATTR(threeg, S_IRUGO | S_IWUSR, show_bool_threeg,
1505 set_bool_threeg);
1507 static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
1508 char *buf)
1510 switch (interface->type) {
1511 case ACER_AMW0:
1512 return sprintf(buf, "AMW0\n");
1513 case ACER_AMW0_V2:
1514 return sprintf(buf, "AMW0 v2\n");
1515 case ACER_WMID:
1516 return sprintf(buf, "WMID\n");
1517 default:
1518 return sprintf(buf, "Error!\n");
1522 static DEVICE_ATTR(interface, S_IRUGO, show_interface, NULL);
1524 static void acer_wmi_notify(u32 value, void *context)
1526 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1527 union acpi_object *obj;
1528 struct event_return_value return_value;
1529 acpi_status status;
1530 u16 device_state;
1531 const struct key_entry *key;
1533 status = wmi_get_event_data(value, &response);
1534 if (status != AE_OK) {
1535 pr_warn("bad event status 0x%x\n", status);
1536 return;
1539 obj = (union acpi_object *)response.pointer;
1541 if (!obj)
1542 return;
1543 if (obj->type != ACPI_TYPE_BUFFER) {
1544 pr_warn("Unknown response received %d\n", obj->type);
1545 kfree(obj);
1546 return;
1548 if (obj->buffer.length != 8) {
1549 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1550 kfree(obj);
1551 return;
1554 return_value = *((struct event_return_value *)obj->buffer.pointer);
1555 kfree(obj);
1557 switch (return_value.function) {
1558 case WMID_HOTKEY_EVENT:
1559 device_state = return_value.device_state;
1560 pr_debug("device state: 0x%x\n", device_state);
1562 key = sparse_keymap_entry_from_scancode(acer_wmi_input_dev,
1563 return_value.key_num);
1564 if (!key) {
1565 pr_warn("Unknown key number - 0x%x\n",
1566 return_value.key_num);
1567 } else {
1568 switch (key->keycode) {
1569 case KEY_WLAN:
1570 case KEY_BLUETOOTH:
1571 if (has_cap(ACER_CAP_WIRELESS))
1572 rfkill_set_sw_state(wireless_rfkill,
1573 !(device_state & ACER_WMID3_GDS_WIRELESS));
1574 if (has_cap(ACER_CAP_THREEG))
1575 rfkill_set_sw_state(threeg_rfkill,
1576 !(device_state & ACER_WMID3_GDS_THREEG));
1577 if (has_cap(ACER_CAP_BLUETOOTH))
1578 rfkill_set_sw_state(bluetooth_rfkill,
1579 !(device_state & ACER_WMID3_GDS_BLUETOOTH));
1580 break;
1582 sparse_keymap_report_entry(acer_wmi_input_dev, key,
1583 1, true);
1585 break;
1586 default:
1587 pr_warn("Unknown function number - %d - %d\n",
1588 return_value.function, return_value.key_num);
1589 break;
1593 static acpi_status
1594 wmid3_set_lm_mode(struct lm_input_params *params,
1595 struct lm_return_value *return_value)
1597 acpi_status status;
1598 union acpi_object *obj;
1600 struct acpi_buffer input = { sizeof(struct lm_input_params), params };
1601 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1603 status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &input, &output);
1604 if (ACPI_FAILURE(status))
1605 return status;
1607 obj = output.pointer;
1609 if (!obj)
1610 return AE_ERROR;
1611 else if (obj->type != ACPI_TYPE_BUFFER) {
1612 kfree(obj);
1613 return AE_ERROR;
1615 if (obj->buffer.length != 4) {
1616 pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1617 kfree(obj);
1618 return AE_ERROR;
1621 *return_value = *((struct lm_return_value *)obj->buffer.pointer);
1622 kfree(obj);
1624 return status;
1627 static int acer_wmi_enable_ec_raw(void)
1629 struct lm_return_value return_value;
1630 acpi_status status;
1631 struct lm_input_params params = {
1632 .function_num = 0x1,
1633 .commun_devices = 0xFFFF,
1634 .devices = 0xFFFF,
1635 .lm_status = 0x00, /* Launch Manager Deactive */
1638 status = wmid3_set_lm_mode(&params, &return_value);
1640 if (return_value.error_code || return_value.ec_return_value)
1641 pr_warn("Enabling EC raw mode failed: 0x%x - 0x%x\n",
1642 return_value.error_code,
1643 return_value.ec_return_value);
1644 else
1645 pr_info("Enabled EC raw mode\n");
1647 return status;
1650 static int acer_wmi_enable_lm(void)
1652 struct lm_return_value return_value;
1653 acpi_status status;
1654 struct lm_input_params params = {
1655 .function_num = 0x1,
1656 .commun_devices = 0xFFFF,
1657 .devices = 0xFFFF,
1658 .lm_status = 0x01, /* Launch Manager Active */
1661 status = wmid3_set_lm_mode(&params, &return_value);
1663 if (return_value.error_code || return_value.ec_return_value)
1664 pr_warn("Enabling Launch Manager failed: 0x%x - 0x%x\n",
1665 return_value.error_code,
1666 return_value.ec_return_value);
1668 return status;
1671 static int __init acer_wmi_input_setup(void)
1673 acpi_status status;
1674 int err;
1676 acer_wmi_input_dev = input_allocate_device();
1677 if (!acer_wmi_input_dev)
1678 return -ENOMEM;
1680 acer_wmi_input_dev->name = "Acer WMI hotkeys";
1681 acer_wmi_input_dev->phys = "wmi/input0";
1682 acer_wmi_input_dev->id.bustype = BUS_HOST;
1684 err = sparse_keymap_setup(acer_wmi_input_dev, acer_wmi_keymap, NULL);
1685 if (err)
1686 goto err_free_dev;
1688 status = wmi_install_notify_handler(ACERWMID_EVENT_GUID,
1689 acer_wmi_notify, NULL);
1690 if (ACPI_FAILURE(status)) {
1691 err = -EIO;
1692 goto err_free_keymap;
1695 err = input_register_device(acer_wmi_input_dev);
1696 if (err)
1697 goto err_uninstall_notifier;
1699 return 0;
1701 err_uninstall_notifier:
1702 wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
1703 err_free_keymap:
1704 sparse_keymap_free(acer_wmi_input_dev);
1705 err_free_dev:
1706 input_free_device(acer_wmi_input_dev);
1707 return err;
1710 static void acer_wmi_input_destroy(void)
1712 wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
1713 sparse_keymap_free(acer_wmi_input_dev);
1714 input_unregister_device(acer_wmi_input_dev);
1718 * debugfs functions
1720 static u32 get_wmid_devices(void)
1722 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
1723 union acpi_object *obj;
1724 acpi_status status;
1725 u32 devices = 0;
1727 status = wmi_query_block(WMID_GUID2, 1, &out);
1728 if (ACPI_FAILURE(status))
1729 return 0;
1731 obj = (union acpi_object *) out.pointer;
1732 if (obj && obj->type == ACPI_TYPE_BUFFER &&
1733 (obj->buffer.length == sizeof(u32) ||
1734 obj->buffer.length == sizeof(u64))) {
1735 devices = *((u32 *) obj->buffer.pointer);
1736 } else if (obj->type == ACPI_TYPE_INTEGER) {
1737 devices = (u32) obj->integer.value;
1740 kfree(out.pointer);
1741 return devices;
1745 * Platform device
1747 static int __devinit acer_platform_probe(struct platform_device *device)
1749 int err;
1751 if (has_cap(ACER_CAP_MAILLED)) {
1752 err = acer_led_init(&device->dev);
1753 if (err)
1754 goto error_mailled;
1757 if (has_cap(ACER_CAP_BRIGHTNESS)) {
1758 err = acer_backlight_init(&device->dev);
1759 if (err)
1760 goto error_brightness;
1763 err = acer_rfkill_init(&device->dev);
1764 if (err)
1765 goto error_rfkill;
1767 return err;
1769 error_rfkill:
1770 if (has_cap(ACER_CAP_BRIGHTNESS))
1771 acer_backlight_exit();
1772 error_brightness:
1773 if (has_cap(ACER_CAP_MAILLED))
1774 acer_led_exit();
1775 error_mailled:
1776 return err;
1779 static int acer_platform_remove(struct platform_device *device)
1781 if (has_cap(ACER_CAP_MAILLED))
1782 acer_led_exit();
1783 if (has_cap(ACER_CAP_BRIGHTNESS))
1784 acer_backlight_exit();
1786 acer_rfkill_exit();
1787 return 0;
1790 static int acer_platform_suspend(struct platform_device *dev,
1791 pm_message_t state)
1793 u32 value;
1794 struct acer_data *data = &interface->data;
1796 if (!data)
1797 return -ENOMEM;
1799 if (has_cap(ACER_CAP_MAILLED)) {
1800 get_u32(&value, ACER_CAP_MAILLED);
1801 set_u32(LED_OFF, ACER_CAP_MAILLED);
1802 data->mailled = value;
1805 if (has_cap(ACER_CAP_BRIGHTNESS)) {
1806 get_u32(&value, ACER_CAP_BRIGHTNESS);
1807 data->brightness = value;
1810 return 0;
1813 static int acer_platform_resume(struct platform_device *device)
1815 struct acer_data *data = &interface->data;
1817 if (!data)
1818 return -ENOMEM;
1820 if (has_cap(ACER_CAP_MAILLED))
1821 set_u32(data->mailled, ACER_CAP_MAILLED);
1823 if (has_cap(ACER_CAP_BRIGHTNESS))
1824 set_u32(data->brightness, ACER_CAP_BRIGHTNESS);
1826 return 0;
1829 static void acer_platform_shutdown(struct platform_device *device)
1831 struct acer_data *data = &interface->data;
1833 if (!data)
1834 return;
1836 if (has_cap(ACER_CAP_MAILLED))
1837 set_u32(LED_OFF, ACER_CAP_MAILLED);
1840 static struct platform_driver acer_platform_driver = {
1841 .driver = {
1842 .name = "acer-wmi",
1843 .owner = THIS_MODULE,
1845 .probe = acer_platform_probe,
1846 .remove = acer_platform_remove,
1847 .suspend = acer_platform_suspend,
1848 .resume = acer_platform_resume,
1849 .shutdown = acer_platform_shutdown,
1852 static struct platform_device *acer_platform_device;
1854 static int remove_sysfs(struct platform_device *device)
1856 if (has_cap(ACER_CAP_THREEG))
1857 device_remove_file(&device->dev, &dev_attr_threeg);
1859 device_remove_file(&device->dev, &dev_attr_interface);
1861 return 0;
1864 static int create_sysfs(void)
1866 int retval = -ENOMEM;
1868 if (has_cap(ACER_CAP_THREEG)) {
1869 retval = device_create_file(&acer_platform_device->dev,
1870 &dev_attr_threeg);
1871 if (retval)
1872 goto error_sysfs;
1875 retval = device_create_file(&acer_platform_device->dev,
1876 &dev_attr_interface);
1877 if (retval)
1878 goto error_sysfs;
1880 return 0;
1882 error_sysfs:
1883 remove_sysfs(acer_platform_device);
1884 return retval;
1887 static void remove_debugfs(void)
1889 debugfs_remove(interface->debug.devices);
1890 debugfs_remove(interface->debug.root);
1893 static int create_debugfs(void)
1895 interface->debug.root = debugfs_create_dir("acer-wmi", NULL);
1896 if (!interface->debug.root) {
1897 pr_err("Failed to create debugfs directory");
1898 return -ENOMEM;
1901 interface->debug.devices = debugfs_create_u32("devices", S_IRUGO,
1902 interface->debug.root,
1903 &interface->debug.wmid_devices);
1904 if (!interface->debug.devices)
1905 goto error_debugfs;
1907 return 0;
1909 error_debugfs:
1910 remove_debugfs();
1911 return -ENOMEM;
1914 static int __init acer_wmi_init(void)
1916 int err;
1918 pr_info("Acer Laptop ACPI-WMI Extras\n");
1920 if (dmi_check_system(acer_blacklist)) {
1921 pr_info("Blacklisted hardware detected - not loading\n");
1922 return -ENODEV;
1925 find_quirks();
1928 * Detect which ACPI-WMI interface we're using.
1930 if (wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1931 interface = &AMW0_V2_interface;
1933 if (!wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
1934 interface = &wmid_interface;
1936 if (wmi_has_guid(WMID_GUID2) && interface) {
1937 if (ACPI_FAILURE(WMID_set_capabilities())) {
1938 pr_err("Unable to detect available WMID devices\n");
1939 return -ENODEV;
1941 } else if (!wmi_has_guid(WMID_GUID2) && interface) {
1942 pr_err("No WMID device detection method found\n");
1943 return -ENODEV;
1946 if (wmi_has_guid(AMW0_GUID1) && !wmi_has_guid(WMID_GUID1)) {
1947 interface = &AMW0_interface;
1949 if (ACPI_FAILURE(AMW0_set_capabilities())) {
1950 pr_err("Unable to detect available AMW0 devices\n");
1951 return -ENODEV;
1955 if (wmi_has_guid(AMW0_GUID1))
1956 AMW0_find_mailled();
1958 if (!interface) {
1959 pr_err("No or unsupported WMI interface, unable to load\n");
1960 return -ENODEV;
1963 set_quirks();
1965 if (acpi_video_backlight_support() && has_cap(ACER_CAP_BRIGHTNESS)) {
1966 interface->capability &= ~ACER_CAP_BRIGHTNESS;
1967 pr_info("Brightness must be controlled by "
1968 "generic video driver\n");
1971 if (wmi_has_guid(WMID_GUID3)) {
1972 if (ec_raw_mode) {
1973 if (ACPI_FAILURE(acer_wmi_enable_ec_raw())) {
1974 pr_err("Cannot enable EC raw mode\n");
1975 return -ENODEV;
1977 } else if (ACPI_FAILURE(acer_wmi_enable_lm())) {
1978 pr_err("Cannot enable Launch Manager mode\n");
1979 return -ENODEV;
1981 } else if (ec_raw_mode) {
1982 pr_info("No WMID EC raw mode enable method\n");
1985 if (wmi_has_guid(ACERWMID_EVENT_GUID)) {
1986 err = acer_wmi_input_setup();
1987 if (err)
1988 return err;
1991 err = platform_driver_register(&acer_platform_driver);
1992 if (err) {
1993 pr_err("Unable to register platform driver.\n");
1994 goto error_platform_register;
1997 acer_platform_device = platform_device_alloc("acer-wmi", -1);
1998 if (!acer_platform_device) {
1999 err = -ENOMEM;
2000 goto error_device_alloc;
2003 err = platform_device_add(acer_platform_device);
2004 if (err)
2005 goto error_device_add;
2007 err = create_sysfs();
2008 if (err)
2009 goto error_create_sys;
2011 if (wmi_has_guid(WMID_GUID2)) {
2012 interface->debug.wmid_devices = get_wmid_devices();
2013 err = create_debugfs();
2014 if (err)
2015 goto error_create_debugfs;
2018 /* Override any initial settings with values from the commandline */
2019 acer_commandline_init();
2021 return 0;
2023 error_create_debugfs:
2024 remove_sysfs(acer_platform_device);
2025 error_create_sys:
2026 platform_device_del(acer_platform_device);
2027 error_device_add:
2028 platform_device_put(acer_platform_device);
2029 error_device_alloc:
2030 platform_driver_unregister(&acer_platform_driver);
2031 error_platform_register:
2032 if (wmi_has_guid(ACERWMID_EVENT_GUID))
2033 acer_wmi_input_destroy();
2035 return err;
2038 static void __exit acer_wmi_exit(void)
2040 if (wmi_has_guid(ACERWMID_EVENT_GUID))
2041 acer_wmi_input_destroy();
2043 remove_sysfs(acer_platform_device);
2044 remove_debugfs();
2045 platform_device_unregister(acer_platform_device);
2046 platform_driver_unregister(&acer_platform_driver);
2048 pr_info("Acer Laptop WMI Extras unloaded\n");
2049 return;
2052 module_init(acer_wmi_init);
2053 module_exit(acer_wmi_exit);