Linux Audio

Check our new training course

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