asus-wmi.c 32.7 KB
Newer Older
1
/*
2
 * Asus PC WMI hotkey driver
3 4
 *
 * Copyright(C) 2010 Intel Corporation.
C
Corentin Chary 已提交
5
 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Portions based on wistron_btns.c:
 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
 *
 *  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
 */

27 28
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

29 30 31 32
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
T
Tejun Heo 已提交
33
#include <linux/slab.h>
34 35
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
Y
Yong Wang 已提交
36 37
#include <linux/fb.h>
#include <linux/backlight.h>
38
#include <linux/leds.h>
39
#include <linux/rfkill.h>
40 41
#include <linux/pci.h>
#include <linux/pci_hotplug.h>
C
Corentin Chary 已提交
42 43
#include <linux/debugfs.h>
#include <linux/seq_file.h>
44
#include <linux/platform_device.h>
45 46 47
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

48
#include "asus-wmi.h"
49

50 51 52
MODULE_AUTHOR("Corentin Chary <corentincj@iksaif.net>, "
	      "Yong Wang <yong.y.wang@intel.com>");
MODULE_DESCRIPTION("Asus Generic WMI Driver");
53 54
MODULE_LICENSE("GPL");

55 56
#define to_platform_driver(drv)					\
	(container_of((drv), struct platform_driver, driver))
57

58 59
#define to_asus_wmi_driver(pdrv)					\
	(container_of((pdrv), struct asus_wmi_driver, platform_driver))
60

61
#define ASUS_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
62

C
Corentin Chary 已提交
63 64 65 66
#define NOTIFY_BRNUP_MIN		0x11
#define NOTIFY_BRNUP_MAX		0x1f
#define NOTIFY_BRNDOWN_MIN		0x20
#define NOTIFY_BRNDOWN_MAX		0x2e
67

C
Corentin Chary 已提交
68
/* WMI Methods */
69 70 71
#define ASUS_WMI_METHODID_DSTS		0x53544344
#define ASUS_WMI_METHODID_DEVS		0x53564544
#define ASUS_WMI_METHODID_CFVS		0x53564643
Y
Yong Wang 已提交
72

C
Corentin Chary 已提交
73
/* Wireless */
74 75 76 77
#define ASUS_WMI_DEVID_WLAN		0x00010011
#define ASUS_WMI_DEVID_BLUETOOTH	0x00010013
#define ASUS_WMI_DEVID_WIMAX		0x00010017
#define ASUS_WMI_DEVID_WWAN3G		0x00010019
C
Corentin Chary 已提交
78 79

/* Backlight and Brightness */
80 81
#define ASUS_WMI_DEVID_BACKLIGHT	0x00050011
#define ASUS_WMI_DEVID_BRIGHTNESS	0x00050012
C
Corentin Chary 已提交
82 83

/* Misc */
84
#define ASUS_WMI_DEVID_CAMERA		0x00060013
C
Corentin Chary 已提交
85 86

/* Storage */
87
#define ASUS_WMI_DEVID_CARDREADER	0x00080013
C
Corentin Chary 已提交
88 89

/* Input */
C
Corentin Chary 已提交
90
#define ASUS_WMI_DEVID_TOUCHPAD		0x00100011
91
#define ASUS_WMI_DEVID_TOUCHPAD_LED	0x00100012
Y
Yong Wang 已提交
92

C
Corentin Chary 已提交
93
/* DSTS masks */
94 95 96 97
#define ASUS_WMI_DSTS_STATUS_BIT	0x00000001
#define ASUS_WMI_DSTS_PRESENCE_BIT	0x00010000
#define ASUS_WMI_DSTS_BRIGHTNESS_MASK	0x000000FF
#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK	0x0000FF00
98

Y
Yong Wang 已提交
99
struct bios_args {
100 101
	u32 dev_id;
	u32 ctrl_param;
Y
Yong Wang 已提交
102 103
};

C
Corentin Chary 已提交
104
/*
105
 * <platform>/    - debugfs root directory
C
Corentin Chary 已提交
106 107 108 109 110
 *   dev_id      - current dev_id
 *   ctrl_param  - current ctrl_param
 *   devs        - call DEVS(dev_id, ctrl_param) and print result
 *   dsts        - call DSTS(dev_id)  and print result
 */
111
struct asus_wmi_debug {
C
Corentin Chary 已提交
112 113 114 115 116
	struct dentry *root;
	u32 dev_id;
	u32 ctrl_param;
};

117 118 119 120 121 122
struct asus_rfkill {
	struct asus_wmi *asus;
	struct rfkill *rfkill;
	u32 dev_id;
};

123
struct asus_wmi {
124
	struct input_dev *inputdev;
Y
Yong Wang 已提交
125
	struct backlight_device *backlight_device;
126
	struct platform_device *platform_device;
127 128 129 130 131

	struct led_classdev tpd_led;
	int tpd_led_wk;
	struct workqueue_struct *led_workqueue;
	struct work_struct tpd_led_work;
132

133 134 135 136
	struct asus_rfkill wlan;
	struct asus_rfkill bluetooth;
	struct asus_rfkill wimax;
	struct asus_rfkill wwan3g;
C
Corentin Chary 已提交
137

138 139
	struct hotplug_slot *hotplug_slot;
	struct mutex hotplug_lock;
140 141 142
	struct mutex wmi_lock;
	struct workqueue_struct *hotplug_workqueue;
	struct work_struct hotplug_work;
143

144 145 146
	struct asus_wmi_debug debug;

	struct asus_wmi_driver *driver;
147 148
};

149
static int asus_wmi_input_init(struct asus_wmi *asus)
150 151 152
{
	int err;

153 154
	asus->inputdev = input_allocate_device();
	if (!asus->inputdev)
155 156
		return -ENOMEM;

157 158 159 160
	asus->inputdev->name = asus->driver->input_phys;
	asus->inputdev->phys = asus->driver->input_name;
	asus->inputdev->id.bustype = BUS_HOST;
	asus->inputdev->dev.parent = &asus->platform_device->dev;
161

162
	err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
163 164 165
	if (err)
		goto err_free_dev;

166
	err = input_register_device(asus->inputdev);
167 168 169 170 171 172
	if (err)
		goto err_free_keymap;

	return 0;

err_free_keymap:
173
	sparse_keymap_free(asus->inputdev);
174
err_free_dev:
175
	input_free_device(asus->inputdev);
176 177 178
	return err;
}

179
static void asus_wmi_input_exit(struct asus_wmi *asus)
180
{
181 182 183
	if (asus->inputdev) {
		sparse_keymap_free(asus->inputdev);
		input_unregister_device(asus->inputdev);
184 185
	}

186
	asus->inputdev = NULL;
187 188
}

189
static acpi_status asus_wmi_get_devstate(u32 dev_id, u32 *retval)
Y
Yong Wang 已提交
190
{
191
	struct acpi_buffer input = { (acpi_size) sizeof(u32), &dev_id };
Y
Yong Wang 已提交
192 193 194 195 196
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;
	acpi_status status;
	u32 tmp;

197 198
	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
				     1, ASUS_WMI_METHODID_DSTS,
199
				     &input, &output);
Y
Yong Wang 已提交
200 201 202 203 204 205

	if (ACPI_FAILURE(status))
		return status;

	obj = (union acpi_object *)output.pointer;
	if (obj && obj->type == ACPI_TYPE_INTEGER)
206
		tmp = (u32) obj->integer.value;
Y
Yong Wang 已提交
207 208 209
	else
		tmp = 0;

210 211
	if (retval)
		*retval = tmp;
Y
Yong Wang 已提交
212 213 214 215 216 217 218

	kfree(obj);

	return status;

}

219 220
static acpi_status asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
					 u32 *retval)
Y
Yong Wang 已提交
221 222 223 224 225
{
	struct bios_args args = {
		.dev_id = dev_id,
		.ctrl_param = ctrl_param,
	};
226
	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Y
Yong Wang 已提交
227 228
	acpi_status status;

C
Corentin Chary 已提交
229
	if (!retval) {
230 231
		status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1,
					     ASUS_WMI_METHODID_DEVS,
C
Corentin Chary 已提交
232 233 234 235 236 237
					     &input, NULL);
	} else {
		struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
		union acpi_object *obj;
		u32 tmp;

238 239
		status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1,
					     ASUS_WMI_METHODID_DEVS,
C
Corentin Chary 已提交
240 241 242 243 244 245 246
					     &input, &output);

		if (ACPI_FAILURE(status))
			return status;

		obj = (union acpi_object *)output.pointer;
		if (obj && obj->type == ACPI_TYPE_INTEGER)
247
			tmp = (u32) obj->integer.value;
C
Corentin Chary 已提交
248 249 250 251 252 253 254
		else
			tmp = 0;

		*retval = tmp;

		kfree(obj);
	}
Y
Yong Wang 已提交
255 256 257 258

	return status;
}

259
/* Helper for special devices with magic return codes */
260
static int asus_wmi_get_devstate_bits(u32 dev_id, u32 mask)
261 262 263 264
{
	u32 retval = 0;
	acpi_status status;

265
	status = asus_wmi_get_devstate(dev_id, &retval);
266 267 268 269

	if (ACPI_FAILURE(status))
		return -EINVAL;

270
	if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
271 272
		return -ENODEV;

273 274 275
	return retval & mask;
}

276
static int asus_wmi_get_devstate_simple(u32 dev_id)
277
{
278
	return asus_wmi_get_devstate_bits(dev_id, ASUS_WMI_DSTS_STATUS_BIT);
279 280
}

281 282 283 284 285 286
/*
 * LEDs
 */
/*
 * These functions actually update the LED's, and are called from a
 * workqueue. By doing this as separate work rather than when the LED
287
 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
288 289 290 291 292
 * potentially bad time, such as a timer interrupt.
 */
static void tpd_led_update(struct work_struct *work)
{
	int ctrl_param;
293
	struct asus_wmi *asus;
294

295
	asus = container_of(work, struct asus_wmi, tpd_led_work);
296

297 298
	ctrl_param = asus->tpd_led_wk;
	asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
299 300 301 302 303
}

static void tpd_led_set(struct led_classdev *led_cdev,
			enum led_brightness value)
{
304
	struct asus_wmi *asus;
305

306
	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
307

308 309
	asus->tpd_led_wk = !!value;
	queue_work(asus->led_workqueue, &asus->tpd_led_work);
310 311
}

312
static int read_tpd_led_state(struct asus_wmi *asus)
313
{
314
	return asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_TOUCHPAD_LED);
315 316 317 318
}

static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
{
319
	struct asus_wmi *asus;
320

321
	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
322

323
	return read_tpd_led_state(asus);
324 325
}

326
static int asus_wmi_led_init(struct asus_wmi *asus)
327 328 329
{
	int rv;

330
	if (read_tpd_led_state(asus) < 0)
331 332
		return 0;

333 334
	asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
	if (!asus->led_workqueue)
335
		return -ENOMEM;
336
	INIT_WORK(&asus->tpd_led_work, tpd_led_update);
337

338 339 340 341
	asus->tpd_led.name = "asus::touchpad";
	asus->tpd_led.brightness_set = tpd_led_set;
	asus->tpd_led.brightness_get = tpd_led_get;
	asus->tpd_led.max_brightness = 1;
342

343
	rv = led_classdev_register(&asus->platform_device->dev, &asus->tpd_led);
344
	if (rv) {
345
		destroy_workqueue(asus->led_workqueue);
346 347 348 349 350 351
		return rv;
	}

	return 0;
}

352
static void asus_wmi_led_exit(struct asus_wmi *asus)
353
{
354 355 356 357
	if (asus->tpd_led.dev)
		led_classdev_unregister(&asus->tpd_led);
	if (asus->led_workqueue)
		destroy_workqueue(asus->led_workqueue);
358 359
}

360 361 362
/*
 * PCI hotplug (for wlan rfkill)
 */
363
static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
364
{
365
	int result = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
366

367
	if (result < 0)
368
		return false;
369
	return !result;
370 371
}

372
static void asus_rfkill_hotplug(struct asus_wmi *asus)
373 374 375
{
	struct pci_dev *dev;
	struct pci_bus *bus;
376
	bool blocked;
377 378 379
	bool absent;
	u32 l;

380 381 382
	mutex_lock(&asus->wmi_lock);
	blocked = asus_wlan_rfkill_blocked(asus);
	mutex_unlock(&asus->wmi_lock);
383

384
	mutex_lock(&asus->hotplug_lock);
385

386 387
	if (asus->wlan.rfkill)
		rfkill_set_sw_state(asus->wlan.rfkill, blocked);
388

389
	if (asus->hotplug_slot) {
390 391 392 393 394 395 396 397 398 399 400 401 402 403
		bus = pci_find_bus(0, 1);
		if (!bus) {
			pr_warning("Unable to find PCI bus 1?\n");
			goto out_unlock;
		}

		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
			pr_err("Unable to read PCI config space?\n");
			goto out_unlock;
		}
		absent = (l == 0xffffffff);

		if (blocked != absent) {
			pr_warning("BIOS says wireless lan is %s, "
404 405 406
				   "but the pci device is %s\n",
				   blocked ? "blocked" : "unblocked",
				   absent ? "absent" : "present");
407
			pr_warning("skipped wireless hotplug as probably "
408
				   "inappropriate for this model\n");
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
			goto out_unlock;
		}

		if (!blocked) {
			dev = pci_get_slot(bus, 0);
			if (dev) {
				/* Device already present */
				pci_dev_put(dev);
				goto out_unlock;
			}
			dev = pci_scan_single_device(bus, 0);
			if (dev) {
				pci_bus_assign_resources(bus);
				if (pci_bus_add_device(dev))
					pr_err("Unable to hotplug wifi\n");
			}
		} else {
			dev = pci_get_slot(bus, 0);
			if (dev) {
				pci_remove_bus_device(dev);
				pci_dev_put(dev);
			}
		}
	}

out_unlock:
435
	mutex_unlock(&asus->hotplug_lock);
436 437
}

438
static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
439
{
440
	struct asus_wmi *asus = data;
441 442 443 444

	if (event != ACPI_NOTIFY_BUS_CHECK)
		return;

445
	/*
446
	 * We can't call directly asus_rfkill_hotplug because most
447 448
	 * of the time WMBC is still being executed and not reetrant.
	 * There is currently no way to tell ACPICA that  we want this
449
	 * method to be serialized, we schedule a asus_rfkill_hotplug
450 451
	 * call later, in a safer context.
	 */
452
	queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
453 454
}

455
static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
456 457 458 459 460 461 462 463 464
{
	acpi_status status;
	acpi_handle handle;

	status = acpi_get_handle(NULL, node, &handle);

	if (ACPI_SUCCESS(status)) {
		status = acpi_install_notify_handler(handle,
						     ACPI_SYSTEM_NOTIFY,
465
						     asus_rfkill_notify, asus);
466 467 468 469 470 471 472 473
		if (ACPI_FAILURE(status))
			pr_warning("Failed to register notify on %s\n", node);
	} else
		return -ENODEV;

	return 0;
}

474
static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
475 476 477 478 479 480 481 482
{
	acpi_status status = AE_OK;
	acpi_handle handle;

	status = acpi_get_handle(NULL, node, &handle);

	if (ACPI_SUCCESS(status)) {
		status = acpi_remove_notify_handler(handle,
483 484
						    ACPI_SYSTEM_NOTIFY,
						    asus_rfkill_notify);
485 486
		if (ACPI_FAILURE(status))
			pr_err("Error removing rfkill notify handler %s\n",
487
			       node);
488 489 490
	}
}

491 492
static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
				   u8 *value)
493
{
494
	int result = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
495

496 497
	if (result < 0)
		return result;
498

499
	*value = !!result;
500 501 502
	return 0;
}

503
static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
504 505 506 507 508
{
	kfree(hotplug_slot->info);
	kfree(hotplug_slot);
}

509
static struct hotplug_slot_ops asus_hotplug_slot_ops = {
510
	.owner = THIS_MODULE,
511 512
	.get_adapter_status = asus_get_adapter_status,
	.get_power_status = asus_get_adapter_status,
513 514
};

515
static void asus_hotplug_work(struct work_struct *work)
516
{
517
	struct asus_wmi *asus;
518

519 520
	asus = container_of(work, struct asus_wmi, hotplug_work);
	asus_rfkill_hotplug(asus);
521 522
}

523
static int asus_setup_pci_hotplug(struct asus_wmi *asus)
524 525 526 527 528 529 530 531 532
{
	int ret = -ENOMEM;
	struct pci_bus *bus = pci_find_bus(0, 1);

	if (!bus) {
		pr_err("Unable to find wifi PCI bus\n");
		return -ENODEV;
	}

533 534 535
	asus->hotplug_workqueue =
	    create_singlethread_workqueue("hotplug_workqueue");
	if (!asus->hotplug_workqueue)
536 537
		goto error_workqueue;

538
	INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
539

540 541
	asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
	if (!asus->hotplug_slot)
542 543
		goto error_slot;

544 545 546
	asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
					   GFP_KERNEL);
	if (!asus->hotplug_slot->info)
547 548
		goto error_info;

549 550 551 552 553
	asus->hotplug_slot->private = asus;
	asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
	asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
	asus_get_adapter_status(asus->hotplug_slot,
				&asus->hotplug_slot->info->adapter_status);
554

555
	ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
556 557 558 559 560 561 562 563
	if (ret) {
		pr_err("Unable to register hotplug slot - %d\n", ret);
		goto error_register;
	}

	return 0;

error_register:
564
	kfree(asus->hotplug_slot->info);
565
error_info:
566 567
	kfree(asus->hotplug_slot);
	asus->hotplug_slot = NULL;
568
error_slot:
569
	destroy_workqueue(asus->hotplug_workqueue);
570
error_workqueue:
571 572 573
	return ret;
}

574 575 576
/*
 * Rfkill devices
 */
577
static int asus_rfkill_set(void *data, bool blocked)
578
{
579
	struct asus_rfkill *priv = data;
580
	u32 ctrl_param = !blocked;
581 582
	acpi_status status;

583
	status = asus_wmi_set_devstate(priv->dev_id, ctrl_param, NULL);
584 585 586

	if (ACPI_FAILURE(status))
		return -EIO;
587

588
	return 0;
589 590
}

591
static void asus_rfkill_query(struct rfkill *rfkill, void *data)
592
{
593
	struct asus_rfkill *priv = data;
594
	int result;
595

596
	result = asus_wmi_get_devstate_simple(priv->dev_id);
597

598
	if (result < 0)
599
		return;
600

601
	rfkill_set_sw_state(priv->rfkill, !result);
602 603
}

604
static int asus_rfkill_wlan_set(void *data, bool blocked)
605
{
606 607
	struct asus_rfkill *priv = data;
	struct asus_wmi *asus = priv->asus;
608 609 610 611
	int ret;

	/*
	 * This handler is enabled only if hotplug is enabled.
612
	 * In this case, the asus_wmi_set_devstate() will
613 614 615 616
	 * trigger a wmi notification and we need to wait
	 * this call to finish before being able to call
	 * any wmi method
	 */
617
	mutex_lock(&asus->wmi_lock);
618
	ret = asus_rfkill_set(data, blocked);
619
	mutex_unlock(&asus->wmi_lock);
620 621 622
	return ret;
}

623 624
static const struct rfkill_ops asus_rfkill_wlan_ops = {
	.set_block = asus_rfkill_wlan_set,
625
	.query = asus_rfkill_query,
626 627
};

628 629 630
static const struct rfkill_ops asus_rfkill_ops = {
	.set_block = asus_rfkill_set,
	.query = asus_rfkill_query,
631 632
};

633
static int asus_new_rfkill(struct asus_wmi *asus,
634
			   struct asus_rfkill *arfkill,
635
			   const char *name, enum rfkill_type type, int dev_id)
636
{
637
	int result = asus_wmi_get_devstate_simple(dev_id);
638
	struct rfkill **rfkill = &arfkill->rfkill;
639

640 641
	if (result < 0)
		return result;
642

643 644 645
	arfkill->dev_id = dev_id;
	arfkill->asus = asus;

646 647
	if (dev_id == ASUS_WMI_DEVID_WLAN && asus->driver->hotplug_wireless)
		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
648
				       &asus_rfkill_wlan_ops, arfkill);
649
	else
650
		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
651
				       &asus_rfkill_ops, arfkill);
652 653 654 655

	if (!*rfkill)
		return -EINVAL;

656
	rfkill_init_sw_state(*rfkill, !result);
657 658 659 660 661 662 663 664 665
	result = rfkill_register(*rfkill);
	if (result) {
		rfkill_destroy(*rfkill);
		*rfkill = NULL;
		return result;
	}
	return 0;
}

666
static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
667
{
668 669 670
	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
671 672 673 674
	if (asus->wlan.rfkill) {
		rfkill_unregister(asus->wlan.rfkill);
		rfkill_destroy(asus->wlan.rfkill);
		asus->wlan.rfkill = NULL;
675
	}
676 677
	/*
	 * Refresh pci hotplug in case the rfkill state was changed after
678
	 * asus_unregister_rfkill_notifier()
679
	 */
680 681 682 683 684 685
	asus_rfkill_hotplug(asus);
	if (asus->hotplug_slot)
		pci_hp_deregister(asus->hotplug_slot);
	if (asus->hotplug_workqueue)
		destroy_workqueue(asus->hotplug_workqueue);

686 687 688 689
	if (asus->bluetooth.rfkill) {
		rfkill_unregister(asus->bluetooth.rfkill);
		rfkill_destroy(asus->bluetooth.rfkill);
		asus->bluetooth.rfkill = NULL;
690
	}
691 692 693 694
	if (asus->wimax.rfkill) {
		rfkill_unregister(asus->wimax.rfkill);
		rfkill_destroy(asus->wimax.rfkill);
		asus->wimax.rfkill = NULL;
C
Corentin Chary 已提交
695
	}
696 697 698 699
	if (asus->wwan3g.rfkill) {
		rfkill_unregister(asus->wwan3g.rfkill);
		rfkill_destroy(asus->wwan3g.rfkill);
		asus->wwan3g.rfkill = NULL;
700 701 702
	}
}

703
static int asus_wmi_rfkill_init(struct asus_wmi *asus)
704 705 706
{
	int result = 0;

707 708
	mutex_init(&asus->hotplug_lock);
	mutex_init(&asus->wmi_lock);
709

710 711
	result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
				 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
712 713 714 715

	if (result && result != -ENODEV)
		goto exit;

716
	result = asus_new_rfkill(asus, &asus->bluetooth,
717 718
				 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
				 ASUS_WMI_DEVID_BLUETOOTH);
719 720 721 722

	if (result && result != -ENODEV)
		goto exit;

723 724
	result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
				 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
C
Corentin Chary 已提交
725 726 727 728

	if (result && result != -ENODEV)
		goto exit;

729 730
	result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
				 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
731 732 733 734

	if (result && result != -ENODEV)
		goto exit;

735
	if (!asus->driver->hotplug_wireless)
736 737
		goto exit;

738
	result = asus_setup_pci_hotplug(asus);
739 740 741 742 743 744 745
	/*
	 * If we get -EBUSY then something else is handling the PCI hotplug -
	 * don't fail in this case
	 */
	if (result == -EBUSY)
		result = 0;

746 747 748
	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
749 750 751 752
	/*
	 * Refresh pci hotplug in case the rfkill state was changed during
	 * setup.
	 */
753
	asus_rfkill_hotplug(asus);
754

755 756
exit:
	if (result && result != -ENODEV)
757
		asus_wmi_rfkill_exit(asus);
758 759 760 761 762 763 764

	if (result == -ENODEV)
		result = 0;

	return result;
}

765 766 767
/*
 * Backlight
 */
768 769
static int read_backlight_power(void)
{
770
	int ret = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_BACKLIGHT);
771 772 773 774 775 776 777

	if (ret < 0)
		return ret;

	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
}

Y
Yong Wang 已提交
778 779
static int read_brightness(struct backlight_device *bd)
{
780
	u32 retval;
Y
Yong Wang 已提交
781 782
	acpi_status status;

783
	status = asus_wmi_get_devstate(ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Y
Yong Wang 已提交
784 785

	if (ACPI_FAILURE(status))
786
		return -EIO;
Y
Yong Wang 已提交
787
	else
788
		return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Y
Yong Wang 已提交
789 790 791 792
}

static int update_bl_status(struct backlight_device *bd)
{
793
	u32 ctrl_param;
Y
Yong Wang 已提交
794
	acpi_status status;
795
	int power;
Y
Yong Wang 已提交
796 797 798

	ctrl_param = bd->props.brightness;

799 800
	status = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
				       ctrl_param, NULL);
Y
Yong Wang 已提交
801 802

	if (ACPI_FAILURE(status))
803 804 805 806 807
		return -EIO;

	power = read_backlight_power();
	if (power != -ENODEV && bd->props.power != power) {
		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
808 809
		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
					       ctrl_param, NULL);
810 811 812 813 814

		if (ACPI_FAILURE(status))
			return -EIO;
	}
	return 0;
Y
Yong Wang 已提交
815 816
}

817
static const struct backlight_ops asus_wmi_bl_ops = {
Y
Yong Wang 已提交
818 819 820 821
	.get_brightness = read_brightness,
	.update_status = update_bl_status,
};

822
static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Y
Yong Wang 已提交
823
{
824
	struct backlight_device *bd = asus->backlight_device;
Y
Yong Wang 已提交
825
	int old = bd->props.brightness;
826
	int new = old;
Y
Yong Wang 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839

	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
		new = code - NOTIFY_BRNUP_MIN + 1;
	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
		new = code - NOTIFY_BRNDOWN_MIN;

	bd->props.brightness = new;
	backlight_update_status(bd);
	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);

	return old;
}

840
static int asus_wmi_backlight_init(struct asus_wmi *asus)
Y
Yong Wang 已提交
841 842 843
{
	struct backlight_device *bd;
	struct backlight_properties props;
844 845 846
	int max;
	int power;

847 848
	max = asus_wmi_get_devstate_bits(ASUS_WMI_DEVID_BRIGHTNESS,
					 ASUS_WMI_DSTS_MAX_BRIGTH_MASK);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
	power = read_backlight_power();

	if (max < 0 && power < 0) {
		/* Try to keep the original error */
		if (max == -ENODEV && power == -ENODEV)
			return -ENODEV;
		if (max != -ENODEV)
			return max;
		else
			return power;
	}
	if (max == -ENODEV)
		max = 0;
	if (power == -ENODEV)
		power = FB_BLANK_UNBLANK;
Y
Yong Wang 已提交
864 865

	memset(&props, 0, sizeof(struct backlight_properties));
866
	props.max_brightness = max;
867 868 869
	bd = backlight_device_register(asus->driver->name,
				       &asus->platform_device->dev, asus,
				       &asus_wmi_bl_ops, &props);
Y
Yong Wang 已提交
870 871 872 873 874
	if (IS_ERR(bd)) {
		pr_err("Could not register backlight device\n");
		return PTR_ERR(bd);
	}

875
	asus->backlight_device = bd;
Y
Yong Wang 已提交
876 877

	bd->props.brightness = read_brightness(bd);
878
	bd->props.power = power;
Y
Yong Wang 已提交
879 880 881 882 883
	backlight_update_status(bd);

	return 0;
}

884
static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Y
Yong Wang 已提交
885
{
886 887
	if (asus->backlight_device)
		backlight_device_unregister(asus->backlight_device);
Y
Yong Wang 已提交
888

889
	asus->backlight_device = NULL;
Y
Yong Wang 已提交
890 891
}

892
static void asus_wmi_notify(u32 value, void *context)
Y
Yong Wang 已提交
893
{
894
	struct asus_wmi *asus = context;
Y
Yong Wang 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908
	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *obj;
	acpi_status status;
	int code;
	int orig_code;

	status = wmi_get_event_data(value, &response);
	if (status != AE_OK) {
		pr_err("bad event status 0x%x\n", status);
		return;
	}

	obj = (union acpi_object *)response.pointer;

C
Corentin Chary 已提交
909 910
	if (!obj || obj->type != ACPI_TYPE_INTEGER)
		goto exit;
Y
Yong Wang 已提交
911

C
Corentin Chary 已提交
912 913
	code = obj->integer.value;
	orig_code = code;
Y
Yong Wang 已提交
914

C
Corentin Chary 已提交
915 916 917 918 919
	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
		code = NOTIFY_BRNUP_MIN;
	else if (code >= NOTIFY_BRNDOWN_MIN &&
		 code <= NOTIFY_BRNDOWN_MAX)
		code = NOTIFY_BRNDOWN_MIN;
Y
Yong Wang 已提交
920

C
Corentin Chary 已提交
921 922 923 924 925
	if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
		if (!acpi_video_backlight_support())
			asus_wmi_backlight_notify(asus, orig_code);
	} else if (!sparse_keymap_report_event(asus->inputdev, code, 1, true))
		pr_info("Unknown key %x pressed\n", code);
Y
Yong Wang 已提交
926

C
Corentin Chary 已提交
927
exit:
Y
Yong Wang 已提交
928 929 930
	kfree(obj);
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
/*
 * Sys helpers
 */
static int parse_arg(const char *buf, unsigned long count, int *val)
{
	if (!count)
		return 0;
	if (sscanf(buf, "%i", val) != 1)
		return -EINVAL;
	return count;
}

static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
{
	acpi_status status;
	u32 retval;
	int rv, value;

949 950
	value = asus_wmi_get_devstate_simple(devid);
	if (value == -ENODEV)	/* Check device presence */
951 952 953
		return value;

	rv = parse_arg(buf, count, &value);
954
	status = asus_wmi_set_devstate(devid, value, &retval);
955 956 957 958 959 960 961 962

	if (ACPI_FAILURE(status))
		return -EIO;
	return rv;
}

static ssize_t show_sys_wmi(int devid, char *buf)
{
963
	int value = asus_wmi_get_devstate_simple(devid);
964 965 966 967 968 969 970

	if (value < 0)
		return value;

	return sprintf(buf, "%d\n", value);
}

971
#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	static ssize_t show_##_name(struct device *dev,			\
				    struct device_attribute *attr,	\
				    char *buf)				\
	{								\
		return show_sys_wmi(_cm, buf);				\
	}								\
	static ssize_t store_##_name(struct device *dev,		\
				     struct device_attribute *attr,	\
				     const char *buf, size_t count)	\
	{								\
		return store_sys_wmi(_cm, buf, count);			\
	}								\
	static struct device_attribute dev_attr_##_name = {		\
		.attr = {						\
			.name = __stringify(_name),			\
			.mode = _mode },				\
		.show   = show_##_name,					\
		.store  = store_##_name,				\
	}

992 993 994
ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
995

D
Dmitry Torokhov 已提交
996 997
static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
998 999
{
	int value;
1000
	struct acpi_buffer input = { (acpi_size) sizeof(value), &value };
1001 1002 1003 1004 1005 1006 1007
	acpi_status status;

	if (!count || sscanf(buf, "%i", &value) != 1)
		return -EINVAL;
	if (value < 0 || value > 2)
		return -EINVAL;

1008 1009
	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
				     1, ASUS_WMI_METHODID_CFVS, &input, NULL);
1010 1011 1012 1013 1014 1015 1016 1017 1018

	if (ACPI_FAILURE(status))
		return -EIO;
	else
		return count;
}

static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);

1019 1020
static struct attribute *platform_attributes[] = {
	&dev_attr_cpufv.attr,
1021 1022
	&dev_attr_camera.attr,
	&dev_attr_cardr.attr,
1023
	&dev_attr_touchpad.attr,
1024 1025 1026
	NULL
};

1027 1028
static mode_t asus_sysfs_is_visible(struct kobject *kobj,
				    struct attribute *attr, int idx)
1029 1030 1031 1032 1033
{
	bool supported = true;
	int devid = -1;

	if (attr == &dev_attr_camera.attr)
1034
		devid = ASUS_WMI_DEVID_CAMERA;
1035
	else if (attr == &dev_attr_cardr.attr)
1036
		devid = ASUS_WMI_DEVID_CARDREADER;
1037
	else if (attr == &dev_attr_touchpad.attr)
1038
		devid = ASUS_WMI_DEVID_TOUCHPAD;
1039 1040

	if (devid != -1)
1041
		supported = asus_wmi_get_devstate_simple(devid) != -ENODEV;
1042 1043 1044 1045

	return supported ? attr->mode : 0;
}

1046
static struct attribute_group platform_attribute_group = {
1047 1048
	.is_visible = asus_sysfs_is_visible,
	.attrs = platform_attributes
1049 1050
};

1051
static void asus_wmi_sysfs_exit(struct platform_device *device)
1052
{
1053
	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1054 1055
}

1056
static int asus_wmi_sysfs_init(struct platform_device *device)
1057
{
1058
	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1059 1060
}

1061 1062 1063
/*
 * Platform device
 */
1064
static int __init asus_wmi_platform_init(struct asus_wmi *asus)
1065
{
1066
	return asus_wmi_sysfs_init(asus->platform_device);
1067 1068
}

1069
static void asus_wmi_platform_exit(struct asus_wmi *asus)
1070
{
1071
	asus_wmi_sysfs_exit(asus->platform_device);
1072 1073
}

C
Corentin Chary 已提交
1074 1075 1076
/*
 * debugfs
 */
1077 1078
struct asus_wmi_debugfs_node {
	struct asus_wmi *asus;
C
Corentin Chary 已提交
1079
	char *name;
1080
	int (*show) (struct seq_file *m, void *data);
C
Corentin Chary 已提交
1081 1082 1083 1084
};

static int show_dsts(struct seq_file *m, void *data)
{
1085
	struct asus_wmi *asus = m->private;
C
Corentin Chary 已提交
1086 1087 1088
	acpi_status status;
	u32 retval = -1;

1089
	status = asus_wmi_get_devstate(asus->debug.dev_id, &retval);
C
Corentin Chary 已提交
1090 1091 1092 1093

	if (ACPI_FAILURE(status))
		return -EIO;

1094
	seq_printf(m, "DSTS(%x) = %x\n", asus->debug.dev_id, retval);
C
Corentin Chary 已提交
1095 1096 1097 1098 1099 1100

	return 0;
}

static int show_devs(struct seq_file *m, void *data)
{
1101
	struct asus_wmi *asus = m->private;
C
Corentin Chary 已提交
1102 1103 1104
	acpi_status status;
	u32 retval = -1;

1105 1106
	status = asus_wmi_set_devstate(asus->debug.dev_id,
				       asus->debug.ctrl_param, &retval);
C
Corentin Chary 已提交
1107 1108 1109
	if (ACPI_FAILURE(status))
		return -EIO;

1110 1111
	seq_printf(m, "DEVS(%x, %x) = %x\n", asus->debug.dev_id,
		   asus->debug.ctrl_param, retval);
C
Corentin Chary 已提交
1112 1113 1114 1115

	return 0;
}

1116 1117 1118
static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
	{NULL, "devs", show_devs},
	{NULL, "dsts", show_dsts},
C
Corentin Chary 已提交
1119 1120
};

1121
static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
C
Corentin Chary 已提交
1122
{
1123
	struct asus_wmi_debugfs_node *node = inode->i_private;
C
Corentin Chary 已提交
1124

1125
	return single_open(file, node->show, node->asus);
C
Corentin Chary 已提交
1126 1127
}

1128
static const struct file_operations asus_wmi_debugfs_io_ops = {
C
Corentin Chary 已提交
1129
	.owner = THIS_MODULE,
1130
	.open = asus_wmi_debugfs_open,
C
Corentin Chary 已提交
1131 1132 1133 1134 1135
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

1136
static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
C
Corentin Chary 已提交
1137
{
1138
	debugfs_remove_recursive(asus->debug.root);
C
Corentin Chary 已提交
1139 1140
}

1141
static int asus_wmi_debugfs_init(struct asus_wmi *asus)
C
Corentin Chary 已提交
1142 1143 1144 1145
{
	struct dentry *dent;
	int i;

1146 1147
	asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
	if (!asus->debug.root) {
C
Corentin Chary 已提交
1148 1149 1150 1151
		pr_err("failed to create debugfs directory");
		goto error_debugfs;
	}

1152 1153
	dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
				  asus->debug.root, &asus->debug.dev_id);
C
Corentin Chary 已提交
1154 1155 1156
	if (!dent)
		goto error_debugfs;

1157 1158
	dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
				  asus->debug.root, &asus->debug.ctrl_param);
C
Corentin Chary 已提交
1159 1160 1161
	if (!dent)
		goto error_debugfs;

1162 1163
	for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
		struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
C
Corentin Chary 已提交
1164

1165
		node->asus = asus;
C
Corentin Chary 已提交
1166
		dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1167 1168
					   asus->debug.root, node,
					   &asus_wmi_debugfs_io_ops);
C
Corentin Chary 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177
		if (!dent) {
			pr_err("failed to create debug file: %s\n", node->name);
			goto error_debugfs;
		}
	}

	return 0;

error_debugfs:
1178
	asus_wmi_debugfs_exit(asus);
C
Corentin Chary 已提交
1179 1180 1181
	return -ENOMEM;
}

1182 1183 1184
/*
 * WMI Driver
 */
1185
static int asus_wmi_add(struct platform_device *pdev)
1186
{
1187 1188 1189
	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
	struct asus_wmi *asus;
1190
	acpi_status status;
1191
	int err;
1192

1193 1194
	asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
	if (!asus)
1195 1196
		return -ENOMEM;

1197 1198 1199 1200
	asus->driver = wdrv;
	asus->platform_device = pdev;
	wdrv->platform_device = pdev;
	platform_set_drvdata(asus->platform_device, asus);
1201

1202 1203
	if (wdrv->quirks)
		wdrv->quirks(asus->driver);
1204

1205
	err = asus_wmi_platform_init(asus);
1206 1207
	if (err)
		goto fail_platform;
1208

1209
	err = asus_wmi_input_init(asus);
1210
	if (err)
1211
		goto fail_input;
Y
Yong Wang 已提交
1212

1213
	err = asus_wmi_led_init(asus);
1214 1215 1216
	if (err)
		goto fail_leds;

1217
	err = asus_wmi_rfkill_init(asus);
1218 1219 1220
	if (err)
		goto fail_rfkill;

Y
Yong Wang 已提交
1221
	if (!acpi_video_backlight_support()) {
1222
		err = asus_wmi_backlight_init(asus);
1223
		if (err && err != -ENODEV)
1224
			goto fail_backlight;
Y
Yong Wang 已提交
1225 1226
	} else
		pr_info("Backlight controlled by ACPI video driver\n");
1227

1228 1229
	status = wmi_install_notify_handler(asus->driver->event_guid,
					    asus_wmi_notify, asus);
1230
	if (ACPI_FAILURE(status)) {
1231
		pr_err("Unable to register notify handler - %d\n", status);
1232
		err = -ENODEV;
1233
		goto fail_wmi_handler;
1234 1235
	}

1236
	err = asus_wmi_debugfs_init(asus);
C
Corentin Chary 已提交
1237 1238 1239
	if (err)
		goto fail_debugfs;

1240
	return 0;
1241

C
Corentin Chary 已提交
1242
fail_debugfs:
1243
	wmi_remove_notify_handler(asus->driver->event_guid);
1244
fail_wmi_handler:
1245
	asus_wmi_backlight_exit(asus);
1246
fail_backlight:
1247
	asus_wmi_rfkill_exit(asus);
1248
fail_rfkill:
1249
	asus_wmi_led_exit(asus);
1250
fail_leds:
1251
	asus_wmi_input_exit(asus);
1252
fail_input:
1253
	asus_wmi_platform_exit(asus);
1254
fail_platform:
1255
	kfree(asus);
1256
	return err;
1257 1258
}

1259
static int asus_wmi_remove(struct platform_device *device)
1260
{
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	struct asus_wmi *asus;

	asus = platform_get_drvdata(device);
	wmi_remove_notify_handler(asus->driver->event_guid);
	asus_wmi_backlight_exit(asus);
	asus_wmi_input_exit(asus);
	asus_wmi_led_exit(asus);
	asus_wmi_rfkill_exit(asus);
	asus_wmi_debugfs_exit(asus);
	asus_wmi_platform_exit(asus);

	kfree(asus);
1273 1274 1275
	return 0;
}

1276 1277 1278
/*
 * Platform driver - hibernate/resume callbacks
 */
1279
static int asus_hotk_thaw(struct device *device)
1280
{
1281
	struct asus_wmi *asus = dev_get_drvdata(device);
1282

1283
	if (asus->wlan.rfkill) {
1284 1285 1286 1287 1288 1289 1290
		bool wlan;

		/*
		 * Work around bios bug - acpi _PTS turns off the wireless led
		 * during suspend.  Normally it restores it on resume, but
		 * we should kick it ourselves in case hibernation is aborted.
		 */
1291 1292
		wlan = asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WLAN);
		asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
1293 1294 1295 1296 1297
	}

	return 0;
}

1298
static int asus_hotk_restore(struct device *device)
1299
{
1300
	struct asus_wmi *asus = dev_get_drvdata(device);
1301 1302 1303
	int bl;

	/* Refresh both wlan rfkill state and pci hotplug */
1304
	if (asus->wlan.rfkill)
1305
		asus_rfkill_hotplug(asus);
1306

1307
	if (asus->bluetooth.rfkill) {
1308
		bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_BLUETOOTH);
1309
		rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
C
Corentin Chary 已提交
1310
	}
1311
	if (asus->wimax.rfkill) {
1312
		bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WIMAX);
1313
		rfkill_set_sw_state(asus->wimax.rfkill, bl);
C
Corentin Chary 已提交
1314
	}
1315
	if (asus->wwan3g.rfkill) {
1316
		bl = !asus_wmi_get_devstate_simple(ASUS_WMI_DEVID_WWAN3G);
1317
		rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
1318 1319 1320 1321 1322
	}

	return 0;
}

1323 1324 1325
static const struct dev_pm_ops asus_pm_ops = {
	.thaw = asus_hotk_thaw,
	.restore = asus_hotk_restore,
1326 1327
};

1328
static int asus_wmi_probe(struct platform_device *pdev)
1329
{
1330 1331 1332
	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
	int ret;
1333

1334 1335
	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
		pr_warning("Management GUID not found\n");
1336 1337 1338
		return -ENODEV;
	}

1339 1340
	if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
		pr_warning("Event GUID not found\n");
1341 1342 1343
		return -ENODEV;
	}

1344 1345 1346 1347 1348 1349 1350
	if (wdrv->probe) {
		ret = wdrv->probe(pdev);
		if (ret)
			return ret;
	}

	return asus_wmi_add(pdev);
1351
}
1352

1353
static bool used;
1354

1355
int asus_wmi_register_driver(struct asus_wmi_driver *driver)
1356
{
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	struct platform_driver *platform_driver;
	struct platform_device *platform_device;

	if (used)
		return -EBUSY;

	platform_driver = &driver->platform_driver;
	platform_driver->remove = asus_wmi_remove;
	platform_driver->driver.owner = driver->owner;
	platform_driver->driver.name = driver->name;
	platform_driver->driver.pm = &asus_pm_ops;

	platform_device = platform_create_bundle(platform_driver,
						 asus_wmi_probe,
1371 1372 1373
						 NULL, 0, NULL, 0);
	if (IS_ERR(platform_device))
		return PTR_ERR(platform_device);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

	used = true;
	return 0;
}
EXPORT_SYMBOL_GPL(asus_wmi_register_driver);

void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
{
	platform_device_unregister(driver->platform_device);
	platform_driver_unregister(&driver->platform_driver);
	used = false;
}
EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);

static int __init asus_wmi_init(void)
{
	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
		pr_info("Asus Management GUID not found");
		return -ENODEV;
	}

	pr_info("ASUS WMI generic driver loaded");
1396 1397 1398
	return 0;
}

1399
static void __exit asus_wmi_exit(void)
1400
{
1401
	pr_info("ASUS WMI generic driver unloaded");
1402 1403
}

1404 1405
module_init(asus_wmi_init);
module_exit(asus_wmi_exit);