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