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