acer-wmi.c 53.9 KB
Newer Older
1 2 3
/*
 *  Acer WMI Laptop Extras
 *
4
 *  Copyright (C) 2007-2009	Carlos Corbacho <carlos@strangeworlds.co.uk>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  Based on acer_acpi:
 *    Copyright (C) 2005-2007	E.M. Smith
 *    Copyright (C) 2007-2008	Carlos Corbacho <cathectic@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

25 26
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

27 28 29 30 31
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/dmi.h>
32
#include <linux/fb.h>
33 34 35 36 37
#include <linux/backlight.h>
#include <linux/leds.h>
#include <linux/platform_device.h>
#include <linux/acpi.h>
#include <linux/i8042.h>
38 39
#include <linux/rfkill.h>
#include <linux/workqueue.h>
40
#include <linux/debugfs.h>
41
#include <linux/slab.h>
42 43
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
44
#include <acpi/video.h>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

MODULE_AUTHOR("Carlos Corbacho");
MODULE_DESCRIPTION("Acer Laptop WMI Extras Driver");
MODULE_LICENSE("GPL");

/*
 * Magic Number
 * Meaning is unknown - this number is required for writing to ACPI for AMW0
 * (it's also used in acerhk when directly accessing the BIOS)
 */
#define ACER_AMW0_WRITE	0x9610

/*
 * Bit masks for the AMW0 interface
 */
#define ACER_AMW0_WIRELESS_MASK  0x35
#define ACER_AMW0_BLUETOOTH_MASK 0x34
#define ACER_AMW0_MAILLED_MASK   0x31

/*
 * Method IDs for WMID interface
 */
#define ACER_WMID_GET_WIRELESS_METHODID		1
#define ACER_WMID_GET_BLUETOOTH_METHODID	2
#define ACER_WMID_GET_BRIGHTNESS_METHODID	3
#define ACER_WMID_SET_WIRELESS_METHODID		4
#define ACER_WMID_SET_BLUETOOTH_METHODID	5
#define ACER_WMID_SET_BRIGHTNESS_METHODID	6
#define ACER_WMID_GET_THREEG_METHODID		10
#define ACER_WMID_SET_THREEG_METHODID		11

/*
 * Acer ACPI method GUIDs
 */
#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"
P
Pali Rohár 已提交
82
#define WMID_GUID2		"95764E09-FB56-4E83-B31A-37761F60994A"
83
#define WMID_GUID3		"61EF69EA-865C-4BC3-A502-A0DEBA0CB531"
84

85 86 87 88 89
/*
 * Acer ACPI event GUIDs
 */
#define ACERWMID_EVENT_GUID "676AA15E-6A47-4D9F-A2CC-1E6D18D14026"

90
MODULE_ALIAS("wmi:67C3371D-95A3-4C37-BB61-DD47B491DAAB");
91
MODULE_ALIAS("wmi:6AF4F258-B401-42FD-BE91-3D4AC2D7C0D3");
92 93 94 95
MODULE_ALIAS("wmi:676AA15E-6A47-4D9F-A2CC-1E6D18D14026");

enum acer_wmi_event_ids {
	WMID_HOTKEY_EVENT = 0x1,
96
	WMID_ACCEL_EVENT = 0x5,
97 98
};

99
static const struct key_entry acer_wmi_keymap[] __initconst = {
100
	{KE_KEY, 0x01, {KEY_WLAN} },     /* WiFi */
101
	{KE_KEY, 0x03, {KEY_WLAN} },     /* WiFi */
102
	{KE_KEY, 0x04, {KEY_WLAN} },     /* WiFi */
103 104 105 106 107
	{KE_KEY, 0x12, {KEY_BLUETOOTH} },	/* BT */
	{KE_KEY, 0x21, {KEY_PROG1} },    /* Backup */
	{KE_KEY, 0x22, {KEY_PROG2} },    /* Arcade */
	{KE_KEY, 0x23, {KEY_PROG3} },    /* P_Key */
	{KE_KEY, 0x24, {KEY_PROG4} },    /* Social networking_Key */
108
	{KE_KEY, 0x29, {KEY_PROG3} },    /* P_Key for TM8372 */
109 110
	{KE_IGNORE, 0x41, {KEY_MUTE} },
	{KE_IGNORE, 0x42, {KEY_PREVIOUSSONG} },
111
	{KE_IGNORE, 0x4d, {KEY_PREVIOUSSONG} },
112
	{KE_IGNORE, 0x43, {KEY_NEXTSONG} },
113
	{KE_IGNORE, 0x4e, {KEY_NEXTSONG} },
114
	{KE_IGNORE, 0x44, {KEY_PLAYPAUSE} },
115
	{KE_IGNORE, 0x4f, {KEY_PLAYPAUSE} },
116
	{KE_IGNORE, 0x45, {KEY_STOP} },
117
	{KE_IGNORE, 0x50, {KEY_STOP} },
118 119
	{KE_IGNORE, 0x48, {KEY_VOLUMEUP} },
	{KE_IGNORE, 0x49, {KEY_VOLUMEDOWN} },
120
	{KE_IGNORE, 0x4a, {KEY_VOLUMEDOWN} },
121 122 123
	{KE_IGNORE, 0x61, {KEY_SWITCHVIDEOMODE} },
	{KE_IGNORE, 0x62, {KEY_BRIGHTNESSUP} },
	{KE_IGNORE, 0x63, {KEY_BRIGHTNESSDOWN} },
124
	{KE_KEY, 0x64, {KEY_SWITCHVIDEOMODE} },	/* Display Switch */
125
	{KE_IGNORE, 0x81, {KEY_SLEEP} },
126 127 128
	{KE_KEY, 0x82, {KEY_TOUCHPAD_TOGGLE} },	/* Touch Pad Toggle */
	{KE_KEY, KEY_TOUCHPAD_ON, {KEY_TOUCHPAD_ON} },
	{KE_KEY, KEY_TOUCHPAD_OFF, {KEY_TOUCHPAD_OFF} },
129
	{KE_IGNORE, 0x83, {KEY_TOUCHPAD_TOGGLE} },
130
	{KE_KEY, 0x85, {KEY_TOUCHPAD_TOGGLE} },
131 132 133 134
	{KE_END, 0}
};

static struct input_dev *acer_wmi_input_dev;
135
static struct input_dev *acer_wmi_accel_dev;
136 137 138 139 140 141 142

struct event_return_value {
	u8 function;
	u8 key_num;
	u16 device_state;
	u32 reserved;
} __attribute__((packed));
143

144 145 146
/*
 * GUID3 Get Device Status device flags
 */
147
#define ACER_WMID3_GDS_WIRELESS		(1<<0)	/* WiFi */
148
#define ACER_WMID3_GDS_THREEG		(1<<6)	/* 3G */
149
#define ACER_WMID3_GDS_WIMAX		(1<<7)	/* WiMAX */
150
#define ACER_WMID3_GDS_BLUETOOTH	(1<<11)	/* BT */
151
#define ACER_WMID3_GDS_TOUCHPAD		(1<<1)	/* Touchpad */
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166
struct lm_input_params {
	u8 function_num;        /* Function Number */
	u16 commun_devices;     /* Communication type devices default status */
	u16 devices;            /* Other type devices default status */
	u8 lm_status;           /* Launch Manager Status */
	u16 reserved;
} __attribute__((packed));

struct lm_return_value {
	u8 error_code;          /* Error Code */
	u8 ec_return_value;     /* EC Return Value */
	u16 reserved;
} __attribute__((packed));

167 168 169 170 171 172 173 174
struct wmid3_gds_set_input_param {     /* Set Device Status input parameter */
	u8 function_num;        /* Function Number */
	u8 hotkey_number;       /* Hotkey Number */
	u16 devices;            /* Set Device */
	u8 volume_value;        /* Volume Value */
} __attribute__((packed));

struct wmid3_gds_get_input_param {     /* Get Device Status input parameter */
175 176 177 178 179 180 181 182 183 184 185 186
	u8 function_num;	/* Function Number */
	u8 hotkey_number;	/* Hotkey Number */
	u16 devices;		/* Get Device */
} __attribute__((packed));

struct wmid3_gds_return_value {	/* Get Device Status return value*/
	u8 error_code;		/* Error Code */
	u8 ec_return_value;	/* EC Return Value */
	u16 devices;		/* Current Device Status */
	u32 reserved;
} __attribute__((packed));

187 188 189 190 191
struct hotkey_function_type_aa {
	u8 type;
	u8 length;
	u16 handle;
	u16 commun_func_bitmap;
192 193 194 195 196
	u16 application_func_bitmap;
	u16 media_func_bitmap;
	u16 display_func_bitmap;
	u16 others_func_bitmap;
	u8 commun_fn_key_number;
197 198
} __attribute__((packed));

199 200 201 202 203 204 205 206
/*
 * Interface capability flags
 */
#define ACER_CAP_MAILLED		(1<<0)
#define ACER_CAP_WIRELESS		(1<<1)
#define ACER_CAP_BLUETOOTH		(1<<2)
#define ACER_CAP_BRIGHTNESS		(1<<3)
#define ACER_CAP_THREEG			(1<<4)
207
#define ACER_CAP_ACCEL			(1<<5)
208 209 210 211 212 213 214 215 216
#define ACER_CAP_ANY			(0xFFFFFFFF)

/*
 * Interface type flags
 */
enum interface_flags {
	ACER_AMW0,
	ACER_AMW0_V2,
	ACER_WMID,
217
	ACER_WMID_v2,
218 219 220 221 222 223 224 225 226 227 228 229 230
};

#define ACER_DEFAULT_WIRELESS  0
#define ACER_DEFAULT_BLUETOOTH 0
#define ACER_DEFAULT_MAILLED   0
#define ACER_DEFAULT_THREEG    0

static int max_brightness = 0xF;

static int mailled = -1;
static int brightness = -1;
static int threeg = -1;
static int force_series;
231
static bool ec_raw_mode;
232
static bool has_type_aa;
233
static u16 commun_func_bitmap;
234
static u8 commun_fn_key_number;
235 236 237 238 239

module_param(mailled, int, 0444);
module_param(brightness, int, 0444);
module_param(threeg, int, 0444);
module_param(force_series, int, 0444);
240
module_param(ec_raw_mode, bool, 0444);
241 242 243 244
MODULE_PARM_DESC(mailled, "Set initial state of Mail LED");
MODULE_PARM_DESC(brightness, "Set initial LCD backlight brightness");
MODULE_PARM_DESC(threeg, "Set initial state of 3G hardware");
MODULE_PARM_DESC(force_series, "Force a different laptop series");
245
MODULE_PARM_DESC(ec_raw_mode, "Enable EC raw mode");
246 247 248 249 250 251 252

struct acer_data {
	int mailled;
	int threeg;
	int brightness;
};

253 254 255 256 257 258
struct acer_debug {
	struct dentry *root;
	struct dentry *devices;
	u32 wmid_devices;
};

259 260
static struct rfkill *wireless_rfkill;
static struct rfkill *bluetooth_rfkill;
261
static struct rfkill *threeg_rfkill;
262
static bool rfkill_inited;
263

264 265 266 267 268 269 270 271 272 273
/* Each low-level interface must define at least some of the following */
struct wmi_interface {
	/* The WMI device type */
	u32 type;

	/* The capabilities this interface provides */
	u32 capability;

	/* Private data for the current interface */
	struct acer_data data;
274 275 276

	/* debugfs entries associated with this interface */
	struct acer_debug debug;
277 278 279 280 281 282 283 284 285 286 287 288 289 290
};

/* The static interface pointer, points to the currently detected interface */
static struct wmi_interface *interface;

/*
 * Embedded Controller quirks
 * Some laptops require us to directly access the EC to either enable or query
 * features that are not available through WMI.
 */

struct quirk_entry {
	u8 wireless;
	u8 mailled;
291
	s8 brightness;
292 293 294 295 296
	u8 bluetooth;
};

static struct quirk_entry *quirks;

297
static void __init set_quirks(void)
298
{
299 300 301
	if (!interface)
		return;

302 303 304 305 306 307 308
	if (quirks->mailled)
		interface->capability |= ACER_CAP_MAILLED;

	if (quirks->brightness)
		interface->capability |= ACER_CAP_BRIGHTNESS;
}

309
static int __init dmi_matched(const struct dmi_system_id *dmi)
310 311
{
	quirks = dmi->driver_data;
312
	return 1;
313 314 315 316 317
}

static struct quirk_entry quirk_unknown = {
};

318 319 320 321
static struct quirk_entry quirk_acer_aspire_1520 = {
	.brightness = -1,
};

322 323 324 325 326 327 328 329 330
static struct quirk_entry quirk_acer_travelmate_2490 = {
	.mailled = 1,
};

/* This AMW0 laptop has no bluetooth */
static struct quirk_entry quirk_medion_md_98300 = {
	.wireless = 1,
};

331 332 333 334
static struct quirk_entry quirk_fujitsu_amilo_li_1718 = {
	.wireless = 2,
};

335 336 337 338
static struct quirk_entry quirk_lenovo_ideapad_s205 = {
	.wireless = 3,
};

339
/* The Aspire One has a dummy ACPI-WMI interface - disable it */
340
static const struct dmi_system_id acer_blacklist[] __initconst = {
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	{
		.ident = "Acer Aspire One (SSD)",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
		},
	},
	{
		.ident = "Acer Aspire One (HDD)",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "AOA150"),
		},
	},
	{}
};

358
static const struct dmi_system_id acer_quirks[] __initconst = {
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 1360",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"),
		},
		.driver_data = &quirk_acer_aspire_1520,
	},
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 1520",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"),
		},
		.driver_data = &quirk_acer_aspire_1520,
	},
377 378 379 380 381 382 383 384 385
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 3100",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3100"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
386 387 388 389 390 391 392 393 394
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 3610",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3610"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
395 396 397 398 399 400 401 402 403
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 5100",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5100"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
404 405 406 407 408 409 410 411 412
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 5610",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5610"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 5630",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5630"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 5650",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5650"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 5680",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5680"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
	{
		.callback = dmi_matched,
		.ident = "Acer Aspire 9110",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 9110"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
	{
		.callback = dmi_matched,
		.ident = "Acer TravelMate 2490",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2490"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
458 459 460 461 462 463 464 465 466
	{
		.callback = dmi_matched,
		.ident = "Acer TravelMate 4200",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4200"),
		},
		.driver_data = &quirk_acer_travelmate_2490,
	},
467 468 469 470 471 472 473 474 475
	{
		.callback = dmi_matched,
		.ident = "Fujitsu Siemens Amilo Li 1718",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
			DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"),
		},
		.driver_data = &quirk_fujitsu_amilo_li_1718,
	},
476 477 478 479 480 481 482 483 484
	{
		.callback = dmi_matched,
		.ident = "Medion MD 98300",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
			DMI_MATCH(DMI_PRODUCT_NAME, "WAM2030"),
		},
		.driver_data = &quirk_medion_md_98300,
	},
485 486 487 488 489 490 491 492 493
	{
		.callback = dmi_matched,
		.ident = "Lenovo Ideapad S205",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "10382LG"),
		},
		.driver_data = &quirk_lenovo_ideapad_s205,
	},
494 495 496 497 498 499 500 501 502
	{
		.callback = dmi_matched,
		.ident = "Lenovo Ideapad S205 (Brazos)",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Brazos"),
		},
		.driver_data = &quirk_lenovo_ideapad_s205,
	},
503 504 505 506 507 508 509 510 511
	{
		.callback = dmi_matched,
		.ident = "Lenovo 3000 N200",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "0687A31"),
		},
		.driver_data = &quirk_fujitsu_amilo_li_1718,
	},
512 513 514 515 516 517 518 519 520
	{
		.callback = dmi_matched,
		.ident = "Lenovo Ideapad S205-10382JG",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "10382JG"),
		},
		.driver_data = &quirk_lenovo_ideapad_s205,
	},
521 522 523 524 525 526 527 528 529
	{
		.callback = dmi_matched,
		.ident = "Lenovo Ideapad S205-1038DPG",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
			DMI_MATCH(DMI_PRODUCT_NAME, "1038DPG"),
		},
		.driver_data = &quirk_lenovo_ideapad_s205,
	},
530 531 532
	{}
};

533 534
static int __init
video_set_backlight_video_vendor(const struct dmi_system_id *d)
535 536 537 538 539 540
{
	interface->capability &= ~ACER_CAP_BRIGHTNESS;
	pr_info("Brightness must be controlled by generic video driver\n");
	return 0;
}

541
static const struct dmi_system_id video_vendor_dmi_table[] __initconst = {
542 543 544 545 546 547 548 549
	{
		.callback = video_set_backlight_video_vendor,
		.ident = "Acer TravelMate 4750",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 4750"),
		},
	},
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	{
		.callback = video_set_backlight_video_vendor,
		.ident = "Acer Extensa 5235",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Extensa 5235"),
		},
	},
	{
		.callback = video_set_backlight_video_vendor,
		.ident = "Acer TravelMate 5760",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 5760"),
		},
	},
	{
		.callback = video_set_backlight_video_vendor,
		.ident = "Acer Aspire 5750",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5750"),
		},
	},
574 575 576 577 578 579 580 581
	{
		.callback = video_set_backlight_video_vendor,
		.ident = "Acer Aspire 5741",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5741"),
		},
	},
582 583 584 585 586 587 588 589 590 591 592
	{
		/*
		 * Note no video_set_backlight_video_vendor, we must use the
		 * acer interface, as there is no native backlight interface.
		 */
		.ident = "Acer KAV80",
		.matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
			DMI_MATCH(DMI_PRODUCT_NAME, "KAV80"),
		},
	},
593 594 595
	{}
};

596
/* Find which quirks are needed for a particular vendor/ model pair */
597
static void __init find_quirks(void)
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
{
	if (!force_series) {
		dmi_check_system(acer_quirks);
	} else if (force_series == 2490) {
		quirks = &quirk_acer_travelmate_2490;
	}

	if (quirks == NULL)
		quirks = &quirk_unknown;

	set_quirks();
}

/*
 * General interface convenience methods
 */

static bool has_cap(u32 cap)
{
	if ((interface->capability & cap) != 0)
		return 1;

	return 0;
}

/*
 * AMW0 (V1) interface
 */
struct wmab_args {
	u32 eax;
	u32 ebx;
	u32 ecx;
	u32 edx;
};

struct wmab_ret {
	u32 eax;
	u32 ebx;
	u32 ecx;
	u32 edx;
	u32 eex;
};

static acpi_status wmab_execute(struct wmab_args *regbuf,
struct acpi_buffer *result)
{
	struct acpi_buffer input;
	acpi_status status;
	input.length = sizeof(struct wmab_args);
	input.pointer = (u8 *)regbuf;

	status = wmi_evaluate_method(AMW0_GUID1, 1, 1, &input, result);

	return status;
}

654
static acpi_status AMW0_get_u32(u32 *value, u32 cap)
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
{
	int err;
	u8 result;

	switch (cap) {
	case ACER_CAP_MAILLED:
		switch (quirks->mailled) {
		default:
			err = ec_read(0xA, &result);
			if (err)
				return AE_ERROR;
			*value = (result >> 7) & 0x1;
			return AE_OK;
		}
		break;
	case ACER_CAP_WIRELESS:
		switch (quirks->wireless) {
		case 1:
			err = ec_read(0x7B, &result);
			if (err)
				return AE_ERROR;
			*value = result & 0x1;
			return AE_OK;
678 679 680 681 682 683
		case 2:
			err = ec_read(0x71, &result);
			if (err)
				return AE_ERROR;
			*value = result & 0x1;
			return AE_OK;
684 685 686 687 688 689
		case 3:
			err = ec_read(0x78, &result);
			if (err)
				return AE_ERROR;
			*value = result & 0x1;
			return AE_OK;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
		default:
			err = ec_read(0xA, &result);
			if (err)
				return AE_ERROR;
			*value = (result >> 2) & 0x1;
			return AE_OK;
		}
		break;
	case ACER_CAP_BLUETOOTH:
		switch (quirks->bluetooth) {
		default:
			err = ec_read(0xA, &result);
			if (err)
				return AE_ERROR;
			*value = (result >> 4) & 0x1;
			return AE_OK;
		}
		break;
	case ACER_CAP_BRIGHTNESS:
		switch (quirks->brightness) {
		default:
			err = ec_read(0x83, &result);
			if (err)
				return AE_ERROR;
			*value = result;
			return AE_OK;
		}
		break;
	default:
719
		return AE_ERROR;
720 721 722 723
	}
	return AE_OK;
}

724
static acpi_status AMW0_set_u32(u32 value, u32 cap)
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
{
	struct wmab_args args;

	args.eax = ACER_AMW0_WRITE;
	args.ebx = value ? (1<<8) : 0;
	args.ecx = args.edx = 0;

	switch (cap) {
	case ACER_CAP_MAILLED:
		if (value > 1)
			return AE_BAD_PARAMETER;
		args.ebx |= ACER_AMW0_MAILLED_MASK;
		break;
	case ACER_CAP_WIRELESS:
		if (value > 1)
			return AE_BAD_PARAMETER;
		args.ebx |= ACER_AMW0_WIRELESS_MASK;
		break;
	case ACER_CAP_BLUETOOTH:
		if (value > 1)
			return AE_BAD_PARAMETER;
		args.ebx |= ACER_AMW0_BLUETOOTH_MASK;
		break;
	case ACER_CAP_BRIGHTNESS:
		if (value > max_brightness)
			return AE_BAD_PARAMETER;
		switch (quirks->brightness) {
		default:
753 754
			return ec_write(0x83, value);
			break;
755 756
		}
	default:
757
		return AE_ERROR;
758 759 760 761 762 763
	}

	/* Actually do the set */
	return wmab_execute(&args, NULL);
}

764
static acpi_status __init AMW0_find_mailled(void)
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
{
	struct wmab_args args;
	struct wmab_ret ret;
	acpi_status status = AE_OK;
	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;

	args.eax = 0x86;
	args.ebx = args.ecx = args.edx = 0;

	status = wmab_execute(&args, &out);
	if (ACPI_FAILURE(status))
		return status;

	obj = (union acpi_object *) out.pointer;
	if (obj && obj->type == ACPI_TYPE_BUFFER &&
	obj->buffer.length == sizeof(struct wmab_ret)) {
		ret = *((struct wmab_ret *) obj->buffer.pointer);
	} else {
784
		kfree(out.pointer);
785 786 787 788 789 790 791 792 793 794 795
		return AE_ERROR;
	}

	if (ret.eex & 0x1)
		interface->capability |= ACER_CAP_MAILLED;

	kfree(out.pointer);

	return AE_OK;
}

796
static int AMW0_set_cap_acpi_check_device_found __initdata;
797

798
static acpi_status __init AMW0_set_cap_acpi_check_device_cb(acpi_handle handle,
799 800 801 802 803 804
	u32 level, void *context, void **retval)
{
	AMW0_set_cap_acpi_check_device_found = 1;
	return AE_OK;
}

805
static const struct acpi_device_id norfkill_ids[] __initconst = {
806 807 808
	{ "VPC2004", 0},
	{ "IBM0068", 0},
	{ "LEN0068", 0},
809
	{ "SNY5001", 0},	/* sony-laptop in charge */
810
	{ "HPQ6601", 0},
811 812 813
	{ "", 0},
};

814
static int __init AMW0_set_cap_acpi_check_device(void)
815 816 817 818 819 820 821 822 823
{
	const struct acpi_device_id *id;

	for (id = norfkill_ids; id->id[0]; id++)
		acpi_get_devices(id->id, AMW0_set_cap_acpi_check_device_cb,
				NULL, NULL);
	return AMW0_set_cap_acpi_check_device_found;
}

824
static acpi_status __init AMW0_set_capabilities(void)
825 826 827
{
	struct wmab_args args;
	struct wmab_ret ret;
828
	acpi_status status;
829 830 831
	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;

832 833 834 835 836
	/*
	 * On laptops with this strange GUID (non Acer), normal probing doesn't
	 * work.
	 */
	if (wmi_has_guid(AMW0_GUID2)) {
837 838 839
		if ((quirks != &quirk_unknown) ||
		    !AMW0_set_cap_acpi_check_device())
			interface->capability |= ACER_CAP_WIRELESS;
840 841 842
		return AE_OK;
	}

843 844 845 846 847 848 849 850 851 852
	args.eax = ACER_AMW0_WRITE;
	args.ecx = args.edx = 0;

	args.ebx = 0xa2 << 8;
	args.ebx |= ACER_AMW0_WIRELESS_MASK;

	status = wmab_execute(&args, &out);
	if (ACPI_FAILURE(status))
		return status;

853
	obj = out.pointer;
854 855 856 857
	if (obj && obj->type == ACPI_TYPE_BUFFER &&
	obj->buffer.length == sizeof(struct wmab_ret)) {
		ret = *((struct wmab_ret *) obj->buffer.pointer);
	} else {
858 859
		status = AE_ERROR;
		goto out;
860 861 862 863 864 865 866 867
	}

	if (ret.eax & 0x1)
		interface->capability |= ACER_CAP_WIRELESS;

	args.ebx = 2 << 8;
	args.ebx |= ACER_AMW0_BLUETOOTH_MASK;

868 869 870 871
	/*
	 * It's ok to use existing buffer for next wmab_execute call.
	 * But we need to kfree(out.pointer) if next wmab_execute fail.
	 */
872 873
	status = wmab_execute(&args, &out);
	if (ACPI_FAILURE(status))
874
		goto out;
875 876 877 878 879 880

	obj = (union acpi_object *) out.pointer;
	if (obj && obj->type == ACPI_TYPE_BUFFER
	&& obj->buffer.length == sizeof(struct wmab_ret)) {
		ret = *((struct wmab_ret *) obj->buffer.pointer);
	} else {
881 882
		status = AE_ERROR;
		goto out;
883 884 885 886 887 888 889 890 891 892
	}

	if (ret.eax & 0x1)
		interface->capability |= ACER_CAP_BLUETOOTH;

	/*
	 * This appears to be safe to enable, since all Wistron based laptops
	 * appear to use the same EC register for brightness, even if they
	 * differ for wireless, etc
	 */
893 894
	if (quirks->brightness >= 0)
		interface->capability |= ACER_CAP_BRIGHTNESS;
895

896 897 898 899
	status = AE_OK;
out:
	kfree(out.pointer);
	return status;
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
}

static struct wmi_interface AMW0_interface = {
	.type = ACER_AMW0,
};

static struct wmi_interface AMW0_V2_interface = {
	.type = ACER_AMW0_V2,
};

/*
 * New interface (The WMID interface)
 */
static acpi_status
WMI_execute_u32(u32 method_id, u32 in, u32 *out)
{
	struct acpi_buffer input = { (acpi_size) sizeof(u32), (void *)(&in) };
	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;
919
	u32 tmp = 0;
920 921 922 923 924 925 926 927
	acpi_status status;

	status = wmi_evaluate_method(WMID_GUID1, 1, method_id, &input, &result);

	if (ACPI_FAILURE(status))
		return status;

	obj = (union acpi_object *) result.pointer;
928 929 930 931 932 933 934 935
	if (obj) {
		if (obj->type == ACPI_TYPE_BUFFER &&
			(obj->buffer.length == sizeof(u32) ||
			obj->buffer.length == sizeof(u64))) {
			tmp = *((u32 *) obj->buffer.pointer);
		} else if (obj->type == ACPI_TYPE_INTEGER) {
			tmp = (u32) obj->integer.value;
		}
936 937 938 939 940 941 942 943 944 945
	}

	if (out)
		*out = tmp;

	kfree(result.pointer);

	return status;
}

946
static acpi_status WMID_get_u32(u32 *value, u32 cap)
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
{
	acpi_status status;
	u8 tmp;
	u32 result, method_id = 0;

	switch (cap) {
	case ACER_CAP_WIRELESS:
		method_id = ACER_WMID_GET_WIRELESS_METHODID;
		break;
	case ACER_CAP_BLUETOOTH:
		method_id = ACER_WMID_GET_BLUETOOTH_METHODID;
		break;
	case ACER_CAP_BRIGHTNESS:
		method_id = ACER_WMID_GET_BRIGHTNESS_METHODID;
		break;
	case ACER_CAP_THREEG:
		method_id = ACER_WMID_GET_THREEG_METHODID;
		break;
	case ACER_CAP_MAILLED:
		if (quirks->mailled == 1) {
			ec_read(0x9f, &tmp);
			*value = tmp & 0x1;
			return 0;
		}
	default:
972
		return AE_ERROR;
973 974 975 976 977 978 979 980 981
	}
	status = WMI_execute_u32(method_id, 0, &result);

	if (ACPI_SUCCESS(status))
		*value = (u8)result;

	return status;
}

982
static acpi_status WMID_set_u32(u32 value, u32 cap)
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
{
	u32 method_id = 0;
	char param;

	switch (cap) {
	case ACER_CAP_BRIGHTNESS:
		if (value > max_brightness)
			return AE_BAD_PARAMETER;
		method_id = ACER_WMID_SET_BRIGHTNESS_METHODID;
		break;
	case ACER_CAP_WIRELESS:
		if (value > 1)
			return AE_BAD_PARAMETER;
		method_id = ACER_WMID_SET_WIRELESS_METHODID;
		break;
	case ACER_CAP_BLUETOOTH:
		if (value > 1)
			return AE_BAD_PARAMETER;
		method_id = ACER_WMID_SET_BLUETOOTH_METHODID;
		break;
	case ACER_CAP_THREEG:
		if (value > 1)
			return AE_BAD_PARAMETER;
		method_id = ACER_WMID_SET_THREEG_METHODID;
		break;
	case ACER_CAP_MAILLED:
		if (value > 1)
			return AE_BAD_PARAMETER;
		if (quirks->mailled == 1) {
			param = value ? 0x92 : 0x93;
1013
			i8042_lock_chip();
1014
			i8042_command(&param, 0x1059);
1015
			i8042_unlock_chip();
1016 1017 1018 1019
			return 0;
		}
		break;
	default:
1020
		return AE_ERROR;
1021 1022 1023 1024
	}
	return WMI_execute_u32(method_id, (u32)value, NULL);
}

1025 1026 1027 1028 1029
static acpi_status wmid3_get_device_status(u32 *value, u16 device)
{
	struct wmid3_gds_return_value return_value;
	acpi_status status;
	union acpi_object *obj;
1030
	struct wmid3_gds_get_input_param params = {
1031
		.function_num = 0x1,
1032
		.hotkey_number = commun_fn_key_number,
1033 1034 1035
		.devices = device,
	};
	struct acpi_buffer input = {
1036
		sizeof(struct wmid3_gds_get_input_param),
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
		&params
	};
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };

	status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &input, &output);
	if (ACPI_FAILURE(status))
		return status;

	obj = output.pointer;

	if (!obj)
		return AE_ERROR;
	else if (obj->type != ACPI_TYPE_BUFFER) {
		kfree(obj);
		return AE_ERROR;
	}
	if (obj->buffer.length != 8) {
		pr_warn("Unknown buffer length %d\n", obj->buffer.length);
		kfree(obj);
		return AE_ERROR;
	}

	return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
	kfree(obj);

	if (return_value.error_code || return_value.ec_return_value)
		pr_warn("Get 0x%x Device Status failed: 0x%x - 0x%x\n",
			device,
			return_value.error_code,
			return_value.ec_return_value);
	else
		*value = !!(return_value.devices & device);

	return status;
}

static acpi_status wmid_v2_get_u32(u32 *value, u32 cap)
{
	u16 device;

	switch (cap) {
	case ACER_CAP_WIRELESS:
		device = ACER_WMID3_GDS_WIRELESS;
		break;
	case ACER_CAP_BLUETOOTH:
		device = ACER_WMID3_GDS_BLUETOOTH;
		break;
	case ACER_CAP_THREEG:
		device = ACER_WMID3_GDS_THREEG;
		break;
	default:
		return AE_ERROR;
	}
	return wmid3_get_device_status(value, device);
}

static acpi_status wmid3_set_device_status(u32 value, u16 device)
{
	struct wmid3_gds_return_value return_value;
	acpi_status status;
	union acpi_object *obj;
	u16 devices;
1099
	struct wmid3_gds_get_input_param get_params = {
1100
		.function_num = 0x1,
1101
		.hotkey_number = commun_fn_key_number,
1102
		.devices = commun_func_bitmap,
1103
	};
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	struct acpi_buffer get_input = {
		sizeof(struct wmid3_gds_get_input_param),
		&get_params
	};
	struct wmid3_gds_set_input_param set_params = {
		.function_num = 0x2,
		.hotkey_number = commun_fn_key_number,
		.devices = commun_func_bitmap,
	};
	struct acpi_buffer set_input = {
		sizeof(struct wmid3_gds_set_input_param),
		&set_params
1116 1117 1118 1119
	};
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_buffer output2 = { ACPI_ALLOCATE_BUFFER, NULL };

1120
	status = wmi_evaluate_method(WMID_GUID3, 0, 0x2, &get_input, &output);
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	if (ACPI_FAILURE(status))
		return status;

	obj = output.pointer;

	if (!obj)
		return AE_ERROR;
	else if (obj->type != ACPI_TYPE_BUFFER) {
		kfree(obj);
		return AE_ERROR;
	}
	if (obj->buffer.length != 8) {
1133
		pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1134 1135 1136 1137 1138 1139 1140 1141
		kfree(obj);
		return AE_ERROR;
	}

	return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
	kfree(obj);

	if (return_value.error_code || return_value.ec_return_value) {
1142 1143
		pr_warn("Get Current Device Status failed: 0x%x - 0x%x\n",
			return_value.error_code,
1144 1145 1146 1147 1148
			return_value.ec_return_value);
		return status;
	}

	devices = return_value.devices;
1149
	set_params.devices = (value) ? (devices | device) : (devices & ~device);
1150

1151
	status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &set_input, &output2);
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	if (ACPI_FAILURE(status))
		return status;

	obj = output2.pointer;

	if (!obj)
		return AE_ERROR;
	else if (obj->type != ACPI_TYPE_BUFFER) {
		kfree(obj);
		return AE_ERROR;
	}
	if (obj->buffer.length != 4) {
1164
		pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1165 1166 1167 1168 1169 1170 1171 1172
		kfree(obj);
		return AE_ERROR;
	}

	return_value = *((struct wmid3_gds_return_value *)obj->buffer.pointer);
	kfree(obj);

	if (return_value.error_code || return_value.ec_return_value)
1173 1174
		pr_warn("Set Device Status failed: 0x%x - 0x%x\n",
			return_value.error_code,
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
			return_value.ec_return_value);

	return status;
}

static acpi_status wmid_v2_set_u32(u32 value, u32 cap)
{
	u16 device;

	switch (cap) {
	case ACER_CAP_WIRELESS:
		device = ACER_WMID3_GDS_WIRELESS;
		break;
	case ACER_CAP_BLUETOOTH:
		device = ACER_WMID3_GDS_BLUETOOTH;
		break;
	case ACER_CAP_THREEG:
		device = ACER_WMID3_GDS_THREEG;
		break;
	default:
		return AE_ERROR;
	}
	return wmid3_set_device_status(value, device);
}

1200
static void __init type_aa_dmi_decode(const struct dmi_header *header, void *d)
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
{
	struct hotkey_function_type_aa *type_aa;

	/* We are looking for OEM-specific Type AAh */
	if (header->type != 0xAA)
		return;

	has_type_aa = true;
	type_aa = (struct hotkey_function_type_aa *) header;

1211
	pr_info("Function bitmap for Communication Button: 0x%x\n",
1212
		type_aa->commun_func_bitmap);
1213
	commun_func_bitmap = type_aa->commun_func_bitmap;
1214 1215 1216 1217 1218 1219 1220

	if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_WIRELESS)
		interface->capability |= ACER_CAP_WIRELESS;
	if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_THREEG)
		interface->capability |= ACER_CAP_THREEG;
	if (type_aa->commun_func_bitmap & ACER_WMID3_GDS_BLUETOOTH)
		interface->capability |= ACER_CAP_BLUETOOTH;
1221 1222

	commun_fn_key_number = type_aa->commun_fn_key_number;
1223 1224
}

1225
static acpi_status __init WMID_set_capabilities(void)
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
{
	struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
	union acpi_object *obj;
	acpi_status status;
	u32 devices;

	status = wmi_query_block(WMID_GUID2, 1, &out);
	if (ACPI_FAILURE(status))
		return status;

	obj = (union acpi_object *) out.pointer;
1237 1238 1239 1240 1241 1242 1243
	if (obj) {
		if (obj->type == ACPI_TYPE_BUFFER &&
			(obj->buffer.length == sizeof(u32) ||
			obj->buffer.length == sizeof(u64))) {
			devices = *((u32 *) obj->buffer.pointer);
		} else if (obj->type == ACPI_TYPE_INTEGER) {
			devices = (u32) obj->integer.value;
1244 1245 1246
		} else {
			kfree(out.pointer);
			return AE_ERROR;
1247
		}
1248
	} else {
1249
		kfree(out.pointer);
1250 1251 1252
		return AE_ERROR;
	}

1253 1254 1255
	pr_info("Function bitmap for Communication Device: 0x%x\n", devices);
	if (devices & 0x07)
		interface->capability |= ACER_CAP_WIRELESS;
1256 1257 1258 1259
	if (devices & 0x40)
		interface->capability |= ACER_CAP_THREEG;
	if (devices & 0x10)
		interface->capability |= ACER_CAP_BLUETOOTH;
1260 1261 1262 1263

	if (!(devices & 0x20))
		max_brightness = 0x9;

1264
	kfree(out.pointer);
1265 1266 1267 1268 1269 1270 1271
	return status;
}

static struct wmi_interface wmid_interface = {
	.type = ACER_WMID,
};

1272 1273 1274 1275
static struct wmi_interface wmid_v2_interface = {
	.type = ACER_WMID_v2,
};

1276 1277 1278 1279 1280 1281
/*
 * Generic Device (interface-independent)
 */

static acpi_status get_u32(u32 *value, u32 cap)
{
1282
	acpi_status status = AE_ERROR;
1283 1284 1285

	switch (interface->type) {
	case ACER_AMW0:
1286
		status = AMW0_get_u32(value, cap);
1287 1288 1289
		break;
	case ACER_AMW0_V2:
		if (cap == ACER_CAP_MAILLED) {
1290
			status = AMW0_get_u32(value, cap);
1291 1292 1293
			break;
		}
	case ACER_WMID:
1294
		status = WMID_get_u32(value, cap);
1295
		break;
1296 1297 1298 1299 1300 1301
	case ACER_WMID_v2:
		if (cap & (ACER_CAP_WIRELESS |
			   ACER_CAP_BLUETOOTH |
			   ACER_CAP_THREEG))
			status = wmid_v2_get_u32(value, cap);
		else if (wmi_has_guid(WMID_GUID2))
1302
			status = WMID_get_u32(value, cap);
1303
		break;
1304 1305 1306 1307 1308 1309 1310
	}

	return status;
}

static acpi_status set_u32(u32 value, u32 cap)
{
1311 1312
	acpi_status status;

1313 1314 1315
	if (interface->capability & cap) {
		switch (interface->type) {
		case ACER_AMW0:
1316
			return AMW0_set_u32(value, cap);
1317
		case ACER_AMW0_V2:
1318
			if (cap == ACER_CAP_MAILLED)
1319
				return AMW0_set_u32(value, cap);
1320 1321 1322 1323 1324 1325 1326 1327 1328

			/*
			 * On some models, some WMID methods don't toggle
			 * properly. For those cases, we want to run the AMW0
			 * method afterwards to be certain we've really toggled
			 * the device state.
			 */
			if (cap == ACER_CAP_WIRELESS ||
				cap == ACER_CAP_BLUETOOTH) {
1329
				status = WMID_set_u32(value, cap);
1330 1331 1332
				if (ACPI_FAILURE(status))
					return status;

1333
				return AMW0_set_u32(value, cap);
1334
			}
1335
		case ACER_WMID:
1336
			return WMID_set_u32(value, cap);
1337 1338 1339 1340 1341 1342
		case ACER_WMID_v2:
			if (cap & (ACER_CAP_WIRELESS |
				   ACER_CAP_BLUETOOTH |
				   ACER_CAP_THREEG))
				return wmid_v2_set_u32(value, cap);
			else if (wmi_has_guid(WMID_GUID2))
1343
				return WMID_set_u32(value, cap);
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
		default:
			return AE_BAD_PARAMETER;
		}
	}
	return AE_BAD_PARAMETER;
}

static void __init acer_commandline_init(void)
{
	/*
	 * These will all fail silently if the value given is invalid, or the
	 * capability isn't available on the given interface
	 */
1357 1358 1359
	if (mailled >= 0)
		set_u32(mailled, ACER_CAP_MAILLED);
	if (!has_type_aa && threeg >= 0)
1360
		set_u32(threeg, ACER_CAP_THREEG);
1361 1362
	if (brightness >= 0)
		set_u32(brightness, ACER_CAP_BRIGHTNESS);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
}

/*
 * LED device (Mail LED only, no other LEDs known yet)
 */
static void mail_led_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
	set_u32(value, ACER_CAP_MAILLED);
}

static struct led_classdev mail_led = {
1375
	.name = "acer-wmi::mail",
1376 1377 1378
	.brightness_set = mail_led_set,
};

1379
static int acer_led_init(struct device *dev)
1380 1381 1382 1383 1384 1385
{
	return led_classdev_register(dev, &mail_led);
}

static void acer_led_exit(void)
{
1386
	set_u32(LED_OFF, ACER_CAP_MAILLED);
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	led_classdev_unregister(&mail_led);
}

/*
 * Backlight device
 */
static struct backlight_device *acer_backlight_device;

static int read_brightness(struct backlight_device *bd)
{
	u32 value;
	get_u32(&value, ACER_CAP_BRIGHTNESS);
	return value;
}

static int update_bl_status(struct backlight_device *bd)
{
1404 1405 1406 1407 1408 1409 1410 1411 1412
	int intensity = bd->props.brightness;

	if (bd->props.power != FB_BLANK_UNBLANK)
		intensity = 0;
	if (bd->props.fb_blank != FB_BLANK_UNBLANK)
		intensity = 0;

	set_u32(intensity, ACER_CAP_BRIGHTNESS);

1413 1414 1415
	return 0;
}

L
Lionel Debroux 已提交
1416
static const struct backlight_ops acer_bl_ops = {
1417 1418 1419 1420
	.get_brightness = read_brightness,
	.update_status = update_bl_status,
};

1421
static int acer_backlight_init(struct device *dev)
1422
{
1423
	struct backlight_properties props;
1424 1425
	struct backlight_device *bd;

1426
	memset(&props, 0, sizeof(struct backlight_properties));
M
Matthew Garrett 已提交
1427
	props.type = BACKLIGHT_PLATFORM;
1428 1429 1430
	props.max_brightness = max_brightness;
	bd = backlight_device_register("acer-wmi", dev, NULL, &acer_bl_ops,
				       &props);
1431
	if (IS_ERR(bd)) {
1432
		pr_err("Could not register Acer backlight device\n");
1433 1434 1435 1436 1437 1438
		acer_backlight_device = NULL;
		return PTR_ERR(bd);
	}

	acer_backlight_device = bd;

1439
	bd->props.power = FB_BLANK_UNBLANK;
1440
	bd->props.brightness = read_brightness(bd);
1441 1442 1443 1444
	backlight_update_status(bd);
	return 0;
}

1445
static void acer_backlight_exit(void)
1446 1447 1448 1449
{
	backlight_device_unregister(acer_backlight_device);
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
/*
 * Accelerometer device
 */
static acpi_handle gsensor_handle;

static int acer_gsensor_init(void)
{
	acpi_status status;
	struct acpi_buffer output;
	union acpi_object out_obj;

	output.length = sizeof(out_obj);
	output.pointer = &out_obj;
	status = acpi_evaluate_object(gsensor_handle, "_INI", NULL, &output);
	if (ACPI_FAILURE(status))
		return -1;

	return 0;
}

static int acer_gsensor_open(struct input_dev *input)
{
	return acer_gsensor_init();
}

static int acer_gsensor_event(void)
{
	acpi_status status;
	struct acpi_buffer output;
	union acpi_object out_obj[5];

	if (!has_cap(ACER_CAP_ACCEL))
		return -1;

	output.length = sizeof(out_obj);
	output.pointer = out_obj;

	status = acpi_evaluate_object(gsensor_handle, "RDVL", NULL, &output);
	if (ACPI_FAILURE(status))
		return -1;

	if (out_obj->package.count != 4)
		return -1;

	input_report_abs(acer_wmi_accel_dev, ABS_X,
		(s16)out_obj->package.elements[0].integer.value);
	input_report_abs(acer_wmi_accel_dev, ABS_Y,
		(s16)out_obj->package.elements[1].integer.value);
	input_report_abs(acer_wmi_accel_dev, ABS_Z,
		(s16)out_obj->package.elements[2].integer.value);
	input_sync(acer_wmi_accel_dev);
	return 0;
}

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
/*
 * Rfkill devices
 */
static void acer_rfkill_update(struct work_struct *ignored);
static DECLARE_DELAYED_WORK(acer_rfkill_work, acer_rfkill_update);
static void acer_rfkill_update(struct work_struct *ignored)
{
	u32 state;
	acpi_status status;

1514 1515 1516 1517 1518 1519 1520
	if (has_cap(ACER_CAP_WIRELESS)) {
		status = get_u32(&state, ACER_CAP_WIRELESS);
		if (ACPI_SUCCESS(status)) {
			if (quirks->wireless == 3)
				rfkill_set_hw_state(wireless_rfkill, !state);
			else
				rfkill_set_sw_state(wireless_rfkill, !state);
1521 1522
		}
	}
1523 1524 1525 1526

	if (has_cap(ACER_CAP_BLUETOOTH)) {
		status = get_u32(&state, ACER_CAP_BLUETOOTH);
		if (ACPI_SUCCESS(status))
T
Troy Moure 已提交
1527
			rfkill_set_sw_state(bluetooth_rfkill, !state);
1528 1529
	}

1530
	if (has_cap(ACER_CAP_THREEG) && wmi_has_guid(WMID_GUID3)) {
1531
		status = get_u32(&state, ACER_WMID3_GDS_THREEG);
1532 1533 1534 1535
		if (ACPI_SUCCESS(status))
			rfkill_set_sw_state(threeg_rfkill, !state);
	}

1536
	schedule_delayed_work(&acer_rfkill_work, round_jiffies_relative(HZ));
1537 1538
}

J
Johannes Berg 已提交
1539
static int acer_rfkill_set(void *data, bool blocked)
1540 1541
{
	acpi_status status;
J
Johannes Berg 已提交
1542
	u32 cap = (unsigned long)data;
1543 1544

	if (rfkill_inited) {
1545
		status = set_u32(!blocked, cap);
1546 1547 1548 1549
		if (ACPI_FAILURE(status))
			return -ENODEV;
	}

1550 1551 1552
	return 0;
}

J
Johannes Berg 已提交
1553 1554 1555 1556 1557 1558 1559
static const struct rfkill_ops acer_rfkill_ops = {
	.set_block = acer_rfkill_set,
};

static struct rfkill *acer_rfkill_register(struct device *dev,
					   enum rfkill_type type,
					   char *name, u32 cap)
1560 1561 1562
{
	int err;
	struct rfkill *rfkill_dev;
1563 1564
	u32 state;
	acpi_status status;
1565

J
Johannes Berg 已提交
1566 1567 1568
	rfkill_dev = rfkill_alloc(name, dev, type,
				  &acer_rfkill_ops,
				  (void *)(unsigned long)cap);
1569 1570 1571
	if (!rfkill_dev)
		return ERR_PTR(-ENOMEM);

1572
	status = get_u32(&state, cap);
1573

1574 1575
	err = rfkill_register(rfkill_dev);
	if (err) {
J
Johannes Berg 已提交
1576
		rfkill_destroy(rfkill_dev);
1577 1578
		return ERR_PTR(err);
	}
1579 1580 1581 1582

	if (ACPI_SUCCESS(status))
		rfkill_set_sw_state(rfkill_dev, !state);

1583 1584 1585 1586 1587
	return rfkill_dev;
}

static int acer_rfkill_init(struct device *dev)
{
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
	int err;

	if (has_cap(ACER_CAP_WIRELESS)) {
		wireless_rfkill = acer_rfkill_register(dev, RFKILL_TYPE_WLAN,
			"acer-wireless", ACER_CAP_WIRELESS);
		if (IS_ERR(wireless_rfkill)) {
			err = PTR_ERR(wireless_rfkill);
			goto error_wireless;
		}
	}
1598 1599 1600 1601 1602 1603

	if (has_cap(ACER_CAP_BLUETOOTH)) {
		bluetooth_rfkill = acer_rfkill_register(dev,
			RFKILL_TYPE_BLUETOOTH, "acer-bluetooth",
			ACER_CAP_BLUETOOTH);
		if (IS_ERR(bluetooth_rfkill)) {
1604 1605
			err = PTR_ERR(bluetooth_rfkill);
			goto error_bluetooth;
1606 1607 1608
		}
	}

1609 1610 1611 1612 1613
	if (has_cap(ACER_CAP_THREEG)) {
		threeg_rfkill = acer_rfkill_register(dev,
			RFKILL_TYPE_WWAN, "acer-threeg",
			ACER_CAP_THREEG);
		if (IS_ERR(threeg_rfkill)) {
1614 1615
			err = PTR_ERR(threeg_rfkill);
			goto error_threeg;
1616 1617 1618
		}
	}

1619 1620
	rfkill_inited = true;

1621 1622
	if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
	    has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1623 1624
		schedule_delayed_work(&acer_rfkill_work,
			round_jiffies_relative(HZ));
1625 1626

	return 0;
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639

error_threeg:
	if (has_cap(ACER_CAP_BLUETOOTH)) {
		rfkill_unregister(bluetooth_rfkill);
		rfkill_destroy(bluetooth_rfkill);
	}
error_bluetooth:
	if (has_cap(ACER_CAP_WIRELESS)) {
		rfkill_unregister(wireless_rfkill);
		rfkill_destroy(wireless_rfkill);
	}
error_wireless:
	return err;
1640 1641 1642 1643
}

static void acer_rfkill_exit(void)
{
1644 1645
	if ((ec_raw_mode || !wmi_has_guid(ACERWMID_EVENT_GUID)) &&
	    has_cap(ACER_CAP_WIRELESS | ACER_CAP_BLUETOOTH | ACER_CAP_THREEG))
1646
		cancel_delayed_work_sync(&acer_rfkill_work);
J
Johannes Berg 已提交
1647

1648 1649 1650 1651
	if (has_cap(ACER_CAP_WIRELESS)) {
		rfkill_unregister(wireless_rfkill);
		rfkill_destroy(wireless_rfkill);
	}
J
Johannes Berg 已提交
1652

1653 1654
	if (has_cap(ACER_CAP_BLUETOOTH)) {
		rfkill_unregister(bluetooth_rfkill);
J
Johannes Berg 已提交
1655
		rfkill_destroy(bluetooth_rfkill);
1656
	}
1657 1658 1659 1660 1661

	if (has_cap(ACER_CAP_THREEG)) {
		rfkill_unregister(threeg_rfkill);
		rfkill_destroy(threeg_rfkill);
	}
1662 1663 1664
	return;
}

1665
/*
1666
 * sysfs interface
1667
 */
1668 1669 1670
static ssize_t show_bool_threeg(struct device *dev,
	struct device_attribute *attr, char *buf)
{
1671
	u32 result; \
1672
	acpi_status status;
1673

1674
	pr_info("This threeg sysfs will be removed in 2014 - used by: %s\n",
1675
		current->comm);
1676
	status = get_u32(&result, ACER_CAP_THREEG);
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
	if (ACPI_SUCCESS(status))
		return sprintf(buf, "%u\n", result);
	return sprintf(buf, "Read error\n");
}

static ssize_t set_bool_threeg(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	u32 tmp = simple_strtoul(buf, NULL, 10);
	acpi_status status = set_u32(tmp, ACER_CAP_THREEG);
1687
	pr_info("This threeg sysfs will be removed in 2014 - used by: %s\n",
1688
		current->comm);
1689 1690
	if (ACPI_FAILURE(status))
		return -EINVAL;
1691 1692
	return count;
}
1693
static DEVICE_ATTR(threeg, S_IRUGO | S_IWUSR, show_bool_threeg,
1694
	set_bool_threeg);
1695 1696 1697 1698

static ssize_t show_interface(struct device *dev, struct device_attribute *attr,
	char *buf)
{
1699
	pr_info("This interface sysfs will be removed in 2014 - used by: %s\n",
1700
		current->comm);
1701 1702 1703 1704 1705 1706 1707
	switch (interface->type) {
	case ACER_AMW0:
		return sprintf(buf, "AMW0\n");
	case ACER_AMW0_V2:
		return sprintf(buf, "AMW0 v2\n");
	case ACER_WMID:
		return sprintf(buf, "WMID\n");
1708 1709
	case ACER_WMID_v2:
		return sprintf(buf, "WMID v2\n");
1710 1711 1712 1713 1714
	default:
		return sprintf(buf, "Error!\n");
	}
}

1715
static DEVICE_ATTR(interface, S_IRUGO, show_interface, NULL);
1716

1717 1718 1719 1720 1721 1722
static void acer_wmi_notify(u32 value, void *context)
{
	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;
	struct event_return_value return_value;
	acpi_status status;
1723 1724
	u16 device_state;
	const struct key_entry *key;
1725
	u32 scancode;
1726 1727 1728

	status = wmi_get_event_data(value, &response);
	if (status != AE_OK) {
J
Joe Perches 已提交
1729
		pr_warn("bad event status 0x%x\n", status);
1730 1731 1732 1733 1734 1735 1736 1737
		return;
	}

	obj = (union acpi_object *)response.pointer;

	if (!obj)
		return;
	if (obj->type != ACPI_TYPE_BUFFER) {
J
Joe Perches 已提交
1738
		pr_warn("Unknown response received %d\n", obj->type);
1739 1740 1741 1742
		kfree(obj);
		return;
	}
	if (obj->buffer.length != 8) {
J
Joe Perches 已提交
1743
		pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1744 1745 1746 1747 1748 1749 1750 1751 1752
		kfree(obj);
		return;
	}

	return_value = *((struct event_return_value *)obj->buffer.pointer);
	kfree(obj);

	switch (return_value.function) {
	case WMID_HOTKEY_EVENT:
1753 1754 1755 1756 1757 1758
		device_state = return_value.device_state;
		pr_debug("device state: 0x%x\n", device_state);

		key = sparse_keymap_entry_from_scancode(acer_wmi_input_dev,
							return_value.key_num);
		if (!key) {
J
Joe Perches 已提交
1759
			pr_warn("Unknown key number - 0x%x\n",
1760
				return_value.key_num);
1761
		} else {
1762
			scancode = return_value.key_num;
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
			switch (key->keycode) {
			case KEY_WLAN:
			case KEY_BLUETOOTH:
				if (has_cap(ACER_CAP_WIRELESS))
					rfkill_set_sw_state(wireless_rfkill,
						!(device_state & ACER_WMID3_GDS_WIRELESS));
				if (has_cap(ACER_CAP_THREEG))
					rfkill_set_sw_state(threeg_rfkill,
						!(device_state & ACER_WMID3_GDS_THREEG));
				if (has_cap(ACER_CAP_BLUETOOTH))
					rfkill_set_sw_state(bluetooth_rfkill,
						!(device_state & ACER_WMID3_GDS_BLUETOOTH));
				break;
1776 1777 1778
			case KEY_TOUCHPAD_TOGGLE:
				scancode = (device_state & ACER_WMID3_GDS_TOUCHPAD) ?
						KEY_TOUCHPAD_ON : KEY_TOUCHPAD_OFF;
1779
			}
1780
			sparse_keymap_report_event(acer_wmi_input_dev, scancode, 1, true);
1781
		}
1782
		break;
1783 1784 1785
	case WMID_ACCEL_EVENT:
		acer_gsensor_event();
		break;
1786
	default:
J
Joe Perches 已提交
1787
		pr_warn("Unknown function number - %d - %d\n",
1788 1789 1790 1791 1792
			return_value.function, return_value.key_num);
		break;
	}
}

1793
static acpi_status __init
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
wmid3_set_lm_mode(struct lm_input_params *params,
		  struct lm_return_value *return_value)
{
	acpi_status status;
	union acpi_object *obj;

	struct acpi_buffer input = { sizeof(struct lm_input_params), params };
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };

	status = wmi_evaluate_method(WMID_GUID3, 0, 0x1, &input, &output);
	if (ACPI_FAILURE(status))
		return status;

	obj = output.pointer;

	if (!obj)
		return AE_ERROR;
	else if (obj->type != ACPI_TYPE_BUFFER) {
		kfree(obj);
		return AE_ERROR;
	}
	if (obj->buffer.length != 4) {
J
Joe Perches 已提交
1816
		pr_warn("Unknown buffer length %d\n", obj->buffer.length);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
		kfree(obj);
		return AE_ERROR;
	}

	*return_value = *((struct lm_return_value *)obj->buffer.pointer);
	kfree(obj);

	return status;
}

1827
static int __init acer_wmi_enable_ec_raw(void)
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
{
	struct lm_return_value return_value;
	acpi_status status;
	struct lm_input_params params = {
		.function_num = 0x1,
		.commun_devices = 0xFFFF,
		.devices = 0xFFFF,
		.lm_status = 0x00,            /* Launch Manager Deactive */
	};

	status = wmid3_set_lm_mode(&params, &return_value);

	if (return_value.error_code || return_value.ec_return_value)
J
Joe Perches 已提交
1841 1842 1843
		pr_warn("Enabling EC raw mode failed: 0x%x - 0x%x\n",
			return_value.error_code,
			return_value.ec_return_value);
1844
	else
J
Joe Perches 已提交
1845
		pr_info("Enabled EC raw mode\n");
1846 1847 1848 1849

	return status;
}

1850
static int __init acer_wmi_enable_lm(void)
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
{
	struct lm_return_value return_value;
	acpi_status status;
	struct lm_input_params params = {
		.function_num = 0x1,
		.commun_devices = 0xFFFF,
		.devices = 0xFFFF,
		.lm_status = 0x01,            /* Launch Manager Active */
	};

	status = wmid3_set_lm_mode(&params, &return_value);

	if (return_value.error_code || return_value.ec_return_value)
J
Joe Perches 已提交
1864 1865 1866
		pr_warn("Enabling Launch Manager failed: 0x%x - 0x%x\n",
			return_value.error_code,
			return_value.ec_return_value);
1867 1868 1869 1870

	return status;
}

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
static acpi_status __init acer_wmi_get_handle_cb(acpi_handle ah, u32 level,
						void *ctx, void **retval)
{
	*(acpi_handle *)retval = ah;
	return AE_OK;
}

static int __init acer_wmi_get_handle(const char *name, const char *prop,
					acpi_handle *ah)
{
	acpi_status status;
	acpi_handle handle;

	BUG_ON(!name || !ah);

M
Marek Vasut 已提交
1886
	handle = NULL;
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	status = acpi_get_devices(prop, acer_wmi_get_handle_cb,
					(void *)name, &handle);

	if (ACPI_SUCCESS(status)) {
		*ah = handle;
		return 0;
	} else {
		return -ENODEV;
	}
}

static int __init acer_wmi_accel_setup(void)
{
	int err;

	err = acer_wmi_get_handle("SENR", "BST0001", &gsensor_handle);
	if (err)
		return err;

	interface->capability |= ACER_CAP_ACCEL;

	acer_wmi_accel_dev = input_allocate_device();
	if (!acer_wmi_accel_dev)
		return -ENOMEM;

	acer_wmi_accel_dev->open = acer_gsensor_open;

	acer_wmi_accel_dev->name = "Acer BMA150 accelerometer";
	acer_wmi_accel_dev->phys = "wmi/input1";
	acer_wmi_accel_dev->id.bustype = BUS_HOST;
	acer_wmi_accel_dev->evbit[0] = BIT_MASK(EV_ABS);
	input_set_abs_params(acer_wmi_accel_dev, ABS_X, -16384, 16384, 0, 0);
	input_set_abs_params(acer_wmi_accel_dev, ABS_Y, -16384, 16384, 0, 0);
	input_set_abs_params(acer_wmi_accel_dev, ABS_Z, -16384, 16384, 0, 0);

	err = input_register_device(acer_wmi_accel_dev);
	if (err)
		goto err_free_dev;

	return 0;

err_free_dev:
	input_free_device(acer_wmi_accel_dev);
	return err;
}

static void acer_wmi_accel_destroy(void)
{
	input_unregister_device(acer_wmi_accel_dev);
}

1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
static int __init acer_wmi_input_setup(void)
{
	acpi_status status;
	int err;

	acer_wmi_input_dev = input_allocate_device();
	if (!acer_wmi_input_dev)
		return -ENOMEM;

	acer_wmi_input_dev->name = "Acer WMI hotkeys";
	acer_wmi_input_dev->phys = "wmi/input0";
	acer_wmi_input_dev->id.bustype = BUS_HOST;

	err = sparse_keymap_setup(acer_wmi_input_dev, acer_wmi_keymap, NULL);
	if (err)
		goto err_free_dev;

	status = wmi_install_notify_handler(ACERWMID_EVENT_GUID,
						acer_wmi_notify, NULL);
	if (ACPI_FAILURE(status)) {
		err = -EIO;
		goto err_free_keymap;
	}

	err = input_register_device(acer_wmi_input_dev);
	if (err)
		goto err_uninstall_notifier;

	return 0;

err_uninstall_notifier:
	wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
err_free_keymap:
	sparse_keymap_free(acer_wmi_input_dev);
err_free_dev:
	input_free_device(acer_wmi_input_dev);
	return err;
}

static void acer_wmi_input_destroy(void)
{
	wmi_remove_notify_handler(ACERWMID_EVENT_GUID);
	sparse_keymap_free(acer_wmi_input_dev);
	input_unregister_device(acer_wmi_input_dev);
}

1984 1985 1986 1987 1988 1989 1990 1991
/*
 * debugfs functions
 */
static u32 get_wmid_devices(void)
{
	struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
	union acpi_object *obj;
	acpi_status status;
1992
	u32 devices = 0;
1993 1994 1995 1996 1997 1998

	status = wmi_query_block(WMID_GUID2, 1, &out);
	if (ACPI_FAILURE(status))
		return 0;

	obj = (union acpi_object *) out.pointer;
1999 2000 2001 2002 2003 2004 2005 2006
	if (obj) {
		if (obj->type == ACPI_TYPE_BUFFER &&
			(obj->buffer.length == sizeof(u32) ||
			obj->buffer.length == sizeof(u64))) {
			devices = *((u32 *) obj->buffer.pointer);
		} else if (obj->type == ACPI_TYPE_INTEGER) {
			devices = (u32) obj->integer.value;
		}
2007
	}
2008 2009 2010

	kfree(out.pointer);
	return devices;
2011 2012
}

2013 2014 2015
/*
 * Platform device
 */
2016
static int acer_platform_probe(struct platform_device *device)
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
{
	int err;

	if (has_cap(ACER_CAP_MAILLED)) {
		err = acer_led_init(&device->dev);
		if (err)
			goto error_mailled;
	}

	if (has_cap(ACER_CAP_BRIGHTNESS)) {
		err = acer_backlight_init(&device->dev);
		if (err)
			goto error_brightness;
	}

2032
	err = acer_rfkill_init(&device->dev);
2033 2034
	if (err)
		goto error_rfkill;
2035 2036

	return err;
2037

2038 2039 2040
error_rfkill:
	if (has_cap(ACER_CAP_BRIGHTNESS))
		acer_backlight_exit();
2041
error_brightness:
2042 2043
	if (has_cap(ACER_CAP_MAILLED))
		acer_led_exit();
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
error_mailled:
	return err;
}

static int acer_platform_remove(struct platform_device *device)
{
	if (has_cap(ACER_CAP_MAILLED))
		acer_led_exit();
	if (has_cap(ACER_CAP_BRIGHTNESS))
		acer_backlight_exit();
2054 2055

	acer_rfkill_exit();
2056 2057 2058
	return 0;
}

2059
#ifdef CONFIG_PM_SLEEP
2060
static int acer_suspend(struct device *dev)
2061 2062 2063 2064 2065 2066 2067 2068 2069
{
	u32 value;
	struct acer_data *data = &interface->data;

	if (!data)
		return -ENOMEM;

	if (has_cap(ACER_CAP_MAILLED)) {
		get_u32(&value, ACER_CAP_MAILLED);
2070
		set_u32(LED_OFF, ACER_CAP_MAILLED);
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
		data->mailled = value;
	}

	if (has_cap(ACER_CAP_BRIGHTNESS)) {
		get_u32(&value, ACER_CAP_BRIGHTNESS);
		data->brightness = value;
	}

	return 0;
}

2082
static int acer_resume(struct device *dev)
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
{
	struct acer_data *data = &interface->data;

	if (!data)
		return -ENOMEM;

	if (has_cap(ACER_CAP_MAILLED))
		set_u32(data->mailled, ACER_CAP_MAILLED);

	if (has_cap(ACER_CAP_BRIGHTNESS))
		set_u32(data->brightness, ACER_CAP_BRIGHTNESS);

2095 2096 2097
	if (has_cap(ACER_CAP_ACCEL))
		acer_gsensor_init();

2098 2099
	return 0;
}
2100 2101 2102 2103
#else
#define acer_suspend	NULL
#define acer_resume	NULL
#endif
2104

2105 2106
static SIMPLE_DEV_PM_OPS(acer_pm, acer_suspend, acer_resume);

2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
static void acer_platform_shutdown(struct platform_device *device)
{
	struct acer_data *data = &interface->data;

	if (!data)
		return;

	if (has_cap(ACER_CAP_MAILLED))
		set_u32(LED_OFF, ACER_CAP_MAILLED);
}

2118 2119 2120
static struct platform_driver acer_platform_driver = {
	.driver = {
		.name = "acer-wmi",
2121
		.pm = &acer_pm,
2122 2123 2124
	},
	.probe = acer_platform_probe,
	.remove = acer_platform_remove,
2125
	.shutdown = acer_platform_shutdown,
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
};

static struct platform_device *acer_platform_device;

static int remove_sysfs(struct platform_device *device)
{
	if (has_cap(ACER_CAP_THREEG))
		device_remove_file(&device->dev, &dev_attr_threeg);

	device_remove_file(&device->dev, &dev_attr_interface);

	return 0;
}

2140
static int __init create_sysfs(void)
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
{
	int retval = -ENOMEM;

	if (has_cap(ACER_CAP_THREEG)) {
		retval = device_create_file(&acer_platform_device->dev,
			&dev_attr_threeg);
		if (retval)
			goto error_sysfs;
	}

	retval = device_create_file(&acer_platform_device->dev,
		&dev_attr_interface);
	if (retval)
		goto error_sysfs;

	return 0;

error_sysfs:
		remove_sysfs(acer_platform_device);
	return retval;
}

2163 2164 2165 2166 2167 2168
static void remove_debugfs(void)
{
	debugfs_remove(interface->debug.devices);
	debugfs_remove(interface->debug.root);
}

2169
static int __init create_debugfs(void)
2170 2171 2172
{
	interface->debug.root = debugfs_create_dir("acer-wmi", NULL);
	if (!interface->debug.root) {
2173
		pr_err("Failed to create debugfs directory");
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
		return -ENOMEM;
	}

	interface->debug.devices = debugfs_create_u32("devices", S_IRUGO,
					interface->debug.root,
					&interface->debug.wmid_devices);
	if (!interface->debug.devices)
		goto error_debugfs;

	return 0;

error_debugfs:
2186
	remove_debugfs();
2187 2188 2189
	return -ENOMEM;
}

2190 2191 2192 2193
static int __init acer_wmi_init(void)
{
	int err;

2194
	pr_info("Acer Laptop ACPI-WMI Extras\n");
2195

2196
	if (dmi_check_system(acer_blacklist)) {
2197
		pr_info("Blacklisted hardware detected - not loading\n");
2198 2199 2200
		return -ENODEV;
	}

2201 2202
	find_quirks();

2203 2204 2205 2206 2207 2208 2209 2210 2211
	/*
	 * Detect which ACPI-WMI interface we're using.
	 */
	if (wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
		interface = &AMW0_V2_interface;

	if (!wmi_has_guid(AMW0_GUID1) && wmi_has_guid(WMID_GUID1))
		interface = &wmid_interface;

2212 2213 2214 2215 2216 2217
	if (wmi_has_guid(WMID_GUID3))
		interface = &wmid_v2_interface;

	if (interface)
		dmi_walk(type_aa_dmi_decode, NULL);

2218
	if (wmi_has_guid(WMID_GUID2) && interface) {
2219
		if (!has_type_aa && ACPI_FAILURE(WMID_set_capabilities())) {
2220
			pr_err("Unable to detect available WMID devices\n");
2221 2222
			return -ENODEV;
		}
2223 2224 2225
		/* WMID always provides brightness methods */
		interface->capability |= ACER_CAP_BRIGHTNESS;
	} else if (!wmi_has_guid(WMID_GUID2) && interface && !has_type_aa) {
2226
		pr_err("No WMID device detection method found\n");
2227 2228 2229 2230 2231 2232 2233
		return -ENODEV;
	}

	if (wmi_has_guid(AMW0_GUID1) && !wmi_has_guid(WMID_GUID1)) {
		interface = &AMW0_interface;

		if (ACPI_FAILURE(AMW0_set_capabilities())) {
2234
			pr_err("Unable to detect available AMW0 devices\n");
2235 2236 2237 2238
			return -ENODEV;
		}
	}

2239 2240
	if (wmi_has_guid(AMW0_GUID1))
		AMW0_find_mailled();
2241 2242

	if (!interface) {
2243
		pr_err("No or unsupported WMI interface, unable to load\n");
2244 2245 2246
		return -ENODEV;
	}

2247 2248
	set_quirks();

2249
	if (dmi_check_system(video_vendor_dmi_table))
2250 2251 2252
		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);

	if (acpi_video_get_backlight_type() != acpi_backlight_vendor)
2253
		interface->capability &= ~ACER_CAP_BRIGHTNESS;
2254

2255 2256 2257
	if (wmi_has_guid(WMID_GUID3)) {
		if (ec_raw_mode) {
			if (ACPI_FAILURE(acer_wmi_enable_ec_raw())) {
2258
				pr_err("Cannot enable EC raw mode\n");
2259 2260 2261
				return -ENODEV;
			}
		} else if (ACPI_FAILURE(acer_wmi_enable_lm())) {
2262
			pr_err("Cannot enable Launch Manager mode\n");
2263 2264 2265
			return -ENODEV;
		}
	} else if (ec_raw_mode) {
2266
		pr_info("No WMID EC raw mode enable method\n");
2267 2268
	}

2269 2270 2271 2272 2273 2274
	if (wmi_has_guid(ACERWMID_EVENT_GUID)) {
		err = acer_wmi_input_setup();
		if (err)
			return err;
	}

2275 2276
	acer_wmi_accel_setup();

2277 2278
	err = platform_driver_register(&acer_platform_driver);
	if (err) {
2279
		pr_err("Unable to register platform driver\n");
2280 2281
		goto error_platform_register;
	}
2282

2283
	acer_platform_device = platform_device_alloc("acer-wmi", -1);
2284 2285 2286 2287 2288 2289 2290 2291
	if (!acer_platform_device) {
		err = -ENOMEM;
		goto error_device_alloc;
	}

	err = platform_device_add(acer_platform_device);
	if (err)
		goto error_device_add;
2292 2293 2294

	err = create_sysfs();
	if (err)
2295
		goto error_create_sys;
2296

2297 2298 2299 2300
	if (wmi_has_guid(WMID_GUID2)) {
		interface->debug.wmid_devices = get_wmid_devices();
		err = create_debugfs();
		if (err)
2301
			goto error_create_debugfs;
2302 2303
	}

2304 2305 2306 2307 2308
	/* Override any initial settings with values from the commandline */
	acer_commandline_init();

	return 0;

2309 2310 2311 2312 2313 2314 2315 2316
error_create_debugfs:
	remove_sysfs(acer_platform_device);
error_create_sys:
	platform_device_del(acer_platform_device);
error_device_add:
	platform_device_put(acer_platform_device);
error_device_alloc:
	platform_driver_unregister(&acer_platform_driver);
2317
error_platform_register:
2318 2319
	if (wmi_has_guid(ACERWMID_EVENT_GUID))
		acer_wmi_input_destroy();
2320 2321
	if (has_cap(ACER_CAP_ACCEL))
		acer_wmi_accel_destroy();
2322

2323
	return err;
2324 2325 2326 2327
}

static void __exit acer_wmi_exit(void)
{
2328 2329 2330
	if (wmi_has_guid(ACERWMID_EVENT_GUID))
		acer_wmi_input_destroy();

2331 2332 2333
	if (has_cap(ACER_CAP_ACCEL))
		acer_wmi_accel_destroy();

2334
	remove_sysfs(acer_platform_device);
2335
	remove_debugfs();
2336
	platform_device_unregister(acer_platform_device);
2337 2338
	platform_driver_unregister(&acer_platform_driver);

2339
	pr_info("Acer Laptop WMI Extras unloaded\n");
2340 2341 2342 2343 2344
	return;
}

module_init(acer_wmi_init);
module_exit(acer_wmi_exit);