xref: /openbmc/linux/drivers/platform/x86/asus-wmi.c (revision c2cd9d04)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Asus PC WMI hotkey driver
4  *
5  * Copyright(C) 2010 Intel Corporation.
6  * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
7  *
8  * Portions based on wistron_btns.c:
9  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/fb.h>
24 #include <linux/backlight.h>
25 #include <linux/leds.h>
26 #include <linux/rfkill.h>
27 #include <linux/pci.h>
28 #include <linux/pci_hotplug.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/platform_data/x86/asus-wmi.h>
34 #include <linux/platform_device.h>
35 #include <linux/thermal.h>
36 #include <linux/acpi.h>
37 #include <linux/dmi.h>
38 #include <acpi/video.h>
39 
40 #include "asus-wmi.h"
41 
42 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
43 	      "Yong Wang <yong.y.wang@intel.com>");
44 MODULE_DESCRIPTION("Asus Generic WMI Driver");
45 MODULE_LICENSE("GPL");
46 
47 #define to_asus_wmi_driver(pdrv)					\
48 	(container_of((pdrv), struct asus_wmi_driver, platform_driver))
49 
50 #define ASUS_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
51 
52 #define NOTIFY_BRNUP_MIN		0x11
53 #define NOTIFY_BRNUP_MAX		0x1f
54 #define NOTIFY_BRNDOWN_MIN		0x20
55 #define NOTIFY_BRNDOWN_MAX		0x2e
56 #define NOTIFY_FNLOCK_TOGGLE		0x4e
57 #define NOTIFY_KBD_BRTUP		0xc4
58 #define NOTIFY_KBD_BRTDWN		0xc5
59 #define NOTIFY_KBD_BRTTOGGLE		0xc7
60 
61 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
62 
63 #define ASUS_FAN_DESC			"cpu_fan"
64 #define ASUS_FAN_MFUN			0x13
65 #define ASUS_FAN_SFUN_READ		0x06
66 #define ASUS_FAN_SFUN_WRITE		0x07
67 #define ASUS_FAN_CTRL_MANUAL		1
68 #define ASUS_FAN_CTRL_AUTO		2
69 
70 #define USB_INTEL_XUSB2PR		0xD0
71 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
72 
73 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
74 
75 static bool ashs_present(void)
76 {
77 	int i = 0;
78 	while (ashs_ids[i]) {
79 		if (acpi_dev_found(ashs_ids[i++]))
80 			return true;
81 	}
82 	return false;
83 }
84 
85 struct bios_args {
86 	u32 arg0;
87 	u32 arg1;
88 } __packed;
89 
90 /*
91  * Struct that's used for all methods called via AGFN. Naming is
92  * identically to the AML code.
93  */
94 struct agfn_args {
95 	u16 mfun; /* probably "Multi-function" to be called */
96 	u16 sfun; /* probably "Sub-function" to be called */
97 	u16 len;  /* size of the hole struct, including subfunction fields */
98 	u8 stas;  /* not used by now */
99 	u8 err;   /* zero on success */
100 } __packed;
101 
102 /* struct used for calling fan read and write methods */
103 struct fan_args {
104 	struct agfn_args agfn;	/* common fields */
105 	u8 fan;			/* fan number: 0: set auto mode 1: 1st fan */
106 	u32 speed;		/* read: RPM/100 - write: 0-255 */
107 } __packed;
108 
109 /*
110  * <platform>/    - debugfs root directory
111  *   dev_id      - current dev_id
112  *   ctrl_param  - current ctrl_param
113  *   method_id   - current method_id
114  *   devs        - call DEVS(dev_id, ctrl_param) and print result
115  *   dsts        - call DSTS(dev_id)  and print result
116  *   call        - call method_id(dev_id, ctrl_param) and print result
117  */
118 struct asus_wmi_debug {
119 	struct dentry *root;
120 	u32 method_id;
121 	u32 dev_id;
122 	u32 ctrl_param;
123 };
124 
125 struct asus_rfkill {
126 	struct asus_wmi *asus;
127 	struct rfkill *rfkill;
128 	u32 dev_id;
129 };
130 
131 struct asus_wmi {
132 	int dsts_id;
133 	int spec;
134 	int sfun;
135 
136 	struct input_dev *inputdev;
137 	struct backlight_device *backlight_device;
138 	struct platform_device *platform_device;
139 
140 	struct led_classdev wlan_led;
141 	int wlan_led_wk;
142 	struct led_classdev tpd_led;
143 	int tpd_led_wk;
144 	struct led_classdev kbd_led;
145 	int kbd_led_wk;
146 	struct led_classdev lightbar_led;
147 	int lightbar_led_wk;
148 	struct workqueue_struct *led_workqueue;
149 	struct work_struct tpd_led_work;
150 	struct work_struct wlan_led_work;
151 	struct work_struct lightbar_led_work;
152 
153 	struct asus_rfkill wlan;
154 	struct asus_rfkill bluetooth;
155 	struct asus_rfkill wimax;
156 	struct asus_rfkill wwan3g;
157 	struct asus_rfkill gps;
158 	struct asus_rfkill uwb;
159 
160 	bool asus_hwmon_fan_manual_mode;
161 	int asus_hwmon_num_fans;
162 	int asus_hwmon_pwm;
163 
164 	struct hotplug_slot hotplug_slot;
165 	struct mutex hotplug_lock;
166 	struct mutex wmi_lock;
167 	struct workqueue_struct *hotplug_workqueue;
168 	struct work_struct hotplug_work;
169 
170 	bool fnlock_locked;
171 
172 	struct asus_wmi_debug debug;
173 
174 	struct asus_wmi_driver *driver;
175 };
176 
177 static int asus_wmi_input_init(struct asus_wmi *asus)
178 {
179 	int err;
180 
181 	asus->inputdev = input_allocate_device();
182 	if (!asus->inputdev)
183 		return -ENOMEM;
184 
185 	asus->inputdev->name = asus->driver->input_name;
186 	asus->inputdev->phys = asus->driver->input_phys;
187 	asus->inputdev->id.bustype = BUS_HOST;
188 	asus->inputdev->dev.parent = &asus->platform_device->dev;
189 	set_bit(EV_REP, asus->inputdev->evbit);
190 
191 	err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
192 	if (err)
193 		goto err_free_dev;
194 
195 	err = input_register_device(asus->inputdev);
196 	if (err)
197 		goto err_free_dev;
198 
199 	return 0;
200 
201 err_free_dev:
202 	input_free_device(asus->inputdev);
203 	return err;
204 }
205 
206 static void asus_wmi_input_exit(struct asus_wmi *asus)
207 {
208 	if (asus->inputdev)
209 		input_unregister_device(asus->inputdev);
210 
211 	asus->inputdev = NULL;
212 }
213 
214 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
215 {
216 	struct bios_args args = {
217 		.arg0 = arg0,
218 		.arg1 = arg1,
219 	};
220 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
221 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
222 	acpi_status status;
223 	union acpi_object *obj;
224 	u32 tmp = 0;
225 
226 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
227 				     &input, &output);
228 
229 	if (ACPI_FAILURE(status))
230 		goto exit;
231 
232 	obj = (union acpi_object *)output.pointer;
233 	if (obj && obj->type == ACPI_TYPE_INTEGER)
234 		tmp = (u32) obj->integer.value;
235 
236 	if (retval)
237 		*retval = tmp;
238 
239 	kfree(obj);
240 
241 exit:
242 	if (ACPI_FAILURE(status))
243 		return -EIO;
244 
245 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
246 		return -ENODEV;
247 
248 	return 0;
249 }
250 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
251 
252 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
253 {
254 	struct acpi_buffer input;
255 	u64 phys_addr;
256 	u32 retval;
257 	u32 status = -1;
258 
259 	/*
260 	 * Copy to dma capable address otherwise memory corruption occurs as
261 	 * bios has to be able to access it.
262 	 */
263 	input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
264 	input.length = args.length;
265 	if (!input.pointer)
266 		return -ENOMEM;
267 	phys_addr = virt_to_phys(input.pointer);
268 	memcpy(input.pointer, args.pointer, args.length);
269 
270 	status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
271 					phys_addr, 0, &retval);
272 	if (!status)
273 		memcpy(args.pointer, input.pointer, args.length);
274 
275 	kfree(input.pointer);
276 	if (status)
277 		return -ENXIO;
278 
279 	return retval;
280 }
281 
282 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
283 {
284 	return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
285 }
286 
287 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
288 				 u32 *retval)
289 {
290 	return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
291 					ctrl_param, retval);
292 }
293 
294 /* Helper for special devices with magic return codes */
295 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
296 				      u32 dev_id, u32 mask)
297 {
298 	u32 retval = 0;
299 	int err;
300 
301 	err = asus_wmi_get_devstate(asus, dev_id, &retval);
302 
303 	if (err < 0)
304 		return err;
305 
306 	if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
307 		return -ENODEV;
308 
309 	if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
310 		if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
311 			return -ENODEV;
312 	}
313 
314 	return retval & mask;
315 }
316 
317 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
318 {
319 	return asus_wmi_get_devstate_bits(asus, dev_id,
320 					  ASUS_WMI_DSTS_STATUS_BIT);
321 }
322 
323 /*
324  * LEDs
325  */
326 /*
327  * These functions actually update the LED's, and are called from a
328  * workqueue. By doing this as separate work rather than when the LED
329  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
330  * potentially bad time, such as a timer interrupt.
331  */
332 static void tpd_led_update(struct work_struct *work)
333 {
334 	int ctrl_param;
335 	struct asus_wmi *asus;
336 
337 	asus = container_of(work, struct asus_wmi, tpd_led_work);
338 
339 	ctrl_param = asus->tpd_led_wk;
340 	asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
341 }
342 
343 static void tpd_led_set(struct led_classdev *led_cdev,
344 			enum led_brightness value)
345 {
346 	struct asus_wmi *asus;
347 
348 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
349 
350 	asus->tpd_led_wk = !!value;
351 	queue_work(asus->led_workqueue, &asus->tpd_led_work);
352 }
353 
354 static int read_tpd_led_state(struct asus_wmi *asus)
355 {
356 	return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
357 }
358 
359 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
360 {
361 	struct asus_wmi *asus;
362 
363 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
364 
365 	return read_tpd_led_state(asus);
366 }
367 
368 static void kbd_led_update(struct asus_wmi *asus)
369 {
370 	int ctrl_param = 0;
371 
372 	/*
373 	 * bits 0-2: level
374 	 * bit 7: light on/off
375 	 */
376 	if (asus->kbd_led_wk > 0)
377 		ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
378 
379 	asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
380 }
381 
382 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
383 {
384 	int retval;
385 
386 	/*
387 	 * bits 0-2: level
388 	 * bit 7: light on/off
389 	 * bit 8-10: environment (0: dark, 1: normal, 2: light)
390 	 * bit 17: status unknown
391 	 */
392 	retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
393 					    0xFFFF);
394 
395 	/* Unknown status is considered as off */
396 	if (retval == 0x8000)
397 		retval = 0;
398 
399 	if (retval >= 0) {
400 		if (level)
401 			*level = retval & 0x7F;
402 		if (env)
403 			*env = (retval >> 8) & 0x7F;
404 		retval = 0;
405 	}
406 
407 	return retval;
408 }
409 
410 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
411 {
412 	struct asus_wmi *asus;
413 	int max_level;
414 
415 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
416 	max_level = asus->kbd_led.max_brightness;
417 
418 	if (value > max_level)
419 		value = max_level;
420 	else if (value < 0)
421 		value = 0;
422 
423 	asus->kbd_led_wk = value;
424 	kbd_led_update(asus);
425 }
426 
427 static void kbd_led_set(struct led_classdev *led_cdev,
428 			enum led_brightness value)
429 {
430 	do_kbd_led_set(led_cdev, value);
431 }
432 
433 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
434 {
435 	struct led_classdev *led_cdev = &asus->kbd_led;
436 
437 	do_kbd_led_set(led_cdev, value);
438 	led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
439 }
440 
441 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
442 {
443 	struct asus_wmi *asus;
444 	int retval, value;
445 
446 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
447 
448 	retval = kbd_led_read(asus, &value, NULL);
449 
450 	if (retval < 0)
451 		return retval;
452 
453 	return value;
454 }
455 
456 static int wlan_led_unknown_state(struct asus_wmi *asus)
457 {
458 	u32 result;
459 
460 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
461 
462 	return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
463 }
464 
465 static int wlan_led_presence(struct asus_wmi *asus)
466 {
467 	u32 result;
468 
469 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
470 
471 	return result & ASUS_WMI_DSTS_PRESENCE_BIT;
472 }
473 
474 static void wlan_led_update(struct work_struct *work)
475 {
476 	int ctrl_param;
477 	struct asus_wmi *asus;
478 
479 	asus = container_of(work, struct asus_wmi, wlan_led_work);
480 
481 	ctrl_param = asus->wlan_led_wk;
482 	asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
483 }
484 
485 static void wlan_led_set(struct led_classdev *led_cdev,
486 			 enum led_brightness value)
487 {
488 	struct asus_wmi *asus;
489 
490 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
491 
492 	asus->wlan_led_wk = !!value;
493 	queue_work(asus->led_workqueue, &asus->wlan_led_work);
494 }
495 
496 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
497 {
498 	struct asus_wmi *asus;
499 	u32 result;
500 
501 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
502 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
503 
504 	return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
505 }
506 
507 static void lightbar_led_update(struct work_struct *work)
508 {
509 	struct asus_wmi *asus;
510 	int ctrl_param;
511 
512 	asus = container_of(work, struct asus_wmi, lightbar_led_work);
513 
514 	ctrl_param = asus->lightbar_led_wk;
515 	asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
516 }
517 
518 static void lightbar_led_set(struct led_classdev *led_cdev,
519 			     enum led_brightness value)
520 {
521 	struct asus_wmi *asus;
522 
523 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
524 
525 	asus->lightbar_led_wk = !!value;
526 	queue_work(asus->led_workqueue, &asus->lightbar_led_work);
527 }
528 
529 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
530 {
531 	struct asus_wmi *asus;
532 	u32 result;
533 
534 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
535 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
536 
537 	return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
538 }
539 
540 static int lightbar_led_presence(struct asus_wmi *asus)
541 {
542 	u32 result;
543 
544 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
545 
546 	return result & ASUS_WMI_DSTS_PRESENCE_BIT;
547 }
548 
549 static void asus_wmi_led_exit(struct asus_wmi *asus)
550 {
551 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
552 		led_classdev_unregister(&asus->kbd_led);
553 	if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
554 		led_classdev_unregister(&asus->tpd_led);
555 	if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
556 		led_classdev_unregister(&asus->wlan_led);
557 	if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
558 		led_classdev_unregister(&asus->lightbar_led);
559 	if (asus->led_workqueue)
560 		destroy_workqueue(asus->led_workqueue);
561 }
562 
563 static int asus_wmi_led_init(struct asus_wmi *asus)
564 {
565 	int rv = 0, led_val;
566 
567 	asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
568 	if (!asus->led_workqueue)
569 		return -ENOMEM;
570 
571 	if (read_tpd_led_state(asus) >= 0) {
572 		INIT_WORK(&asus->tpd_led_work, tpd_led_update);
573 
574 		asus->tpd_led.name = "asus::touchpad";
575 		asus->tpd_led.brightness_set = tpd_led_set;
576 		asus->tpd_led.brightness_get = tpd_led_get;
577 		asus->tpd_led.max_brightness = 1;
578 
579 		rv = led_classdev_register(&asus->platform_device->dev,
580 					   &asus->tpd_led);
581 		if (rv)
582 			goto error;
583 	}
584 
585 	led_val = kbd_led_read(asus, NULL, NULL);
586 	if (led_val >= 0) {
587 		asus->kbd_led_wk = led_val;
588 		asus->kbd_led.name = "asus::kbd_backlight";
589 		asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
590 		asus->kbd_led.brightness_set = kbd_led_set;
591 		asus->kbd_led.brightness_get = kbd_led_get;
592 		asus->kbd_led.max_brightness = 3;
593 
594 		rv = led_classdev_register(&asus->platform_device->dev,
595 					   &asus->kbd_led);
596 		if (rv)
597 			goto error;
598 	}
599 
600 	if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
601 		INIT_WORK(&asus->wlan_led_work, wlan_led_update);
602 
603 		asus->wlan_led.name = "asus::wlan";
604 		asus->wlan_led.brightness_set = wlan_led_set;
605 		if (!wlan_led_unknown_state(asus))
606 			asus->wlan_led.brightness_get = wlan_led_get;
607 		asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
608 		asus->wlan_led.max_brightness = 1;
609 		asus->wlan_led.default_trigger = "asus-wlan";
610 
611 		rv = led_classdev_register(&asus->platform_device->dev,
612 					   &asus->wlan_led);
613 		if (rv)
614 			goto error;
615 	}
616 
617 	if (lightbar_led_presence(asus)) {
618 		INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
619 
620 		asus->lightbar_led.name = "asus::lightbar";
621 		asus->lightbar_led.brightness_set = lightbar_led_set;
622 		asus->lightbar_led.brightness_get = lightbar_led_get;
623 		asus->lightbar_led.max_brightness = 1;
624 
625 		rv = led_classdev_register(&asus->platform_device->dev,
626 					   &asus->lightbar_led);
627 	}
628 
629 error:
630 	if (rv)
631 		asus_wmi_led_exit(asus);
632 
633 	return rv;
634 }
635 
636 
637 /*
638  * PCI hotplug (for wlan rfkill)
639  */
640 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
641 {
642 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
643 
644 	if (result < 0)
645 		return false;
646 	return !result;
647 }
648 
649 static void asus_rfkill_hotplug(struct asus_wmi *asus)
650 {
651 	struct pci_dev *dev;
652 	struct pci_bus *bus;
653 	bool blocked;
654 	bool absent;
655 	u32 l;
656 
657 	mutex_lock(&asus->wmi_lock);
658 	blocked = asus_wlan_rfkill_blocked(asus);
659 	mutex_unlock(&asus->wmi_lock);
660 
661 	mutex_lock(&asus->hotplug_lock);
662 	pci_lock_rescan_remove();
663 
664 	if (asus->wlan.rfkill)
665 		rfkill_set_sw_state(asus->wlan.rfkill, blocked);
666 
667 	if (asus->hotplug_slot.ops) {
668 		bus = pci_find_bus(0, 1);
669 		if (!bus) {
670 			pr_warn("Unable to find PCI bus 1?\n");
671 			goto out_unlock;
672 		}
673 
674 		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
675 			pr_err("Unable to read PCI config space?\n");
676 			goto out_unlock;
677 		}
678 		absent = (l == 0xffffffff);
679 
680 		if (blocked != absent) {
681 			pr_warn("BIOS says wireless lan is %s, "
682 				"but the pci device is %s\n",
683 				blocked ? "blocked" : "unblocked",
684 				absent ? "absent" : "present");
685 			pr_warn("skipped wireless hotplug as probably "
686 				"inappropriate for this model\n");
687 			goto out_unlock;
688 		}
689 
690 		if (!blocked) {
691 			dev = pci_get_slot(bus, 0);
692 			if (dev) {
693 				/* Device already present */
694 				pci_dev_put(dev);
695 				goto out_unlock;
696 			}
697 			dev = pci_scan_single_device(bus, 0);
698 			if (dev) {
699 				pci_bus_assign_resources(bus);
700 				pci_bus_add_device(dev);
701 			}
702 		} else {
703 			dev = pci_get_slot(bus, 0);
704 			if (dev) {
705 				pci_stop_and_remove_bus_device(dev);
706 				pci_dev_put(dev);
707 			}
708 		}
709 	}
710 
711 out_unlock:
712 	pci_unlock_rescan_remove();
713 	mutex_unlock(&asus->hotplug_lock);
714 }
715 
716 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
717 {
718 	struct asus_wmi *asus = data;
719 
720 	if (event != ACPI_NOTIFY_BUS_CHECK)
721 		return;
722 
723 	/*
724 	 * We can't call directly asus_rfkill_hotplug because most
725 	 * of the time WMBC is still being executed and not reetrant.
726 	 * There is currently no way to tell ACPICA that  we want this
727 	 * method to be serialized, we schedule a asus_rfkill_hotplug
728 	 * call later, in a safer context.
729 	 */
730 	queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
731 }
732 
733 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
734 {
735 	acpi_status status;
736 	acpi_handle handle;
737 
738 	status = acpi_get_handle(NULL, node, &handle);
739 
740 	if (ACPI_SUCCESS(status)) {
741 		status = acpi_install_notify_handler(handle,
742 						     ACPI_SYSTEM_NOTIFY,
743 						     asus_rfkill_notify, asus);
744 		if (ACPI_FAILURE(status))
745 			pr_warn("Failed to register notify on %s\n", node);
746 	} else
747 		return -ENODEV;
748 
749 	return 0;
750 }
751 
752 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
753 {
754 	acpi_status status = AE_OK;
755 	acpi_handle handle;
756 
757 	status = acpi_get_handle(NULL, node, &handle);
758 
759 	if (ACPI_SUCCESS(status)) {
760 		status = acpi_remove_notify_handler(handle,
761 						    ACPI_SYSTEM_NOTIFY,
762 						    asus_rfkill_notify);
763 		if (ACPI_FAILURE(status))
764 			pr_err("Error removing rfkill notify handler %s\n",
765 			       node);
766 	}
767 }
768 
769 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
770 				   u8 *value)
771 {
772 	struct asus_wmi *asus = container_of(hotplug_slot,
773 					     struct asus_wmi, hotplug_slot);
774 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
775 
776 	if (result < 0)
777 		return result;
778 
779 	*value = !!result;
780 	return 0;
781 }
782 
783 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
784 	.get_adapter_status = asus_get_adapter_status,
785 	.get_power_status = asus_get_adapter_status,
786 };
787 
788 static void asus_hotplug_work(struct work_struct *work)
789 {
790 	struct asus_wmi *asus;
791 
792 	asus = container_of(work, struct asus_wmi, hotplug_work);
793 	asus_rfkill_hotplug(asus);
794 }
795 
796 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
797 {
798 	int ret = -ENOMEM;
799 	struct pci_bus *bus = pci_find_bus(0, 1);
800 
801 	if (!bus) {
802 		pr_err("Unable to find wifi PCI bus\n");
803 		return -ENODEV;
804 	}
805 
806 	asus->hotplug_workqueue =
807 	    create_singlethread_workqueue("hotplug_workqueue");
808 	if (!asus->hotplug_workqueue)
809 		goto error_workqueue;
810 
811 	INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
812 
813 	asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
814 
815 	ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
816 	if (ret) {
817 		pr_err("Unable to register hotplug slot - %d\n", ret);
818 		goto error_register;
819 	}
820 
821 	return 0;
822 
823 error_register:
824 	asus->hotplug_slot.ops = NULL;
825 	destroy_workqueue(asus->hotplug_workqueue);
826 error_workqueue:
827 	return ret;
828 }
829 
830 /*
831  * Rfkill devices
832  */
833 static int asus_rfkill_set(void *data, bool blocked)
834 {
835 	struct asus_rfkill *priv = data;
836 	u32 ctrl_param = !blocked;
837 	u32 dev_id = priv->dev_id;
838 
839 	/*
840 	 * If the user bit is set, BIOS can't set and record the wlan status,
841 	 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
842 	 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
843 	 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
844 	 * while setting the wlan status through WMI.
845 	 * This is also the behavior that windows app will do.
846 	 */
847 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
848 	     priv->asus->driver->wlan_ctrl_by_user)
849 		dev_id = ASUS_WMI_DEVID_WLAN_LED;
850 
851 	return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
852 }
853 
854 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
855 {
856 	struct asus_rfkill *priv = data;
857 	int result;
858 
859 	result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
860 
861 	if (result < 0)
862 		return;
863 
864 	rfkill_set_sw_state(priv->rfkill, !result);
865 }
866 
867 static int asus_rfkill_wlan_set(void *data, bool blocked)
868 {
869 	struct asus_rfkill *priv = data;
870 	struct asus_wmi *asus = priv->asus;
871 	int ret;
872 
873 	/*
874 	 * This handler is enabled only if hotplug is enabled.
875 	 * In this case, the asus_wmi_set_devstate() will
876 	 * trigger a wmi notification and we need to wait
877 	 * this call to finish before being able to call
878 	 * any wmi method
879 	 */
880 	mutex_lock(&asus->wmi_lock);
881 	ret = asus_rfkill_set(data, blocked);
882 	mutex_unlock(&asus->wmi_lock);
883 	return ret;
884 }
885 
886 static const struct rfkill_ops asus_rfkill_wlan_ops = {
887 	.set_block = asus_rfkill_wlan_set,
888 	.query = asus_rfkill_query,
889 };
890 
891 static const struct rfkill_ops asus_rfkill_ops = {
892 	.set_block = asus_rfkill_set,
893 	.query = asus_rfkill_query,
894 };
895 
896 static int asus_new_rfkill(struct asus_wmi *asus,
897 			   struct asus_rfkill *arfkill,
898 			   const char *name, enum rfkill_type type, int dev_id)
899 {
900 	int result = asus_wmi_get_devstate_simple(asus, dev_id);
901 	struct rfkill **rfkill = &arfkill->rfkill;
902 
903 	if (result < 0)
904 		return result;
905 
906 	arfkill->dev_id = dev_id;
907 	arfkill->asus = asus;
908 
909 	if (dev_id == ASUS_WMI_DEVID_WLAN &&
910 	    asus->driver->quirks->hotplug_wireless)
911 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
912 				       &asus_rfkill_wlan_ops, arfkill);
913 	else
914 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
915 				       &asus_rfkill_ops, arfkill);
916 
917 	if (!*rfkill)
918 		return -EINVAL;
919 
920 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
921 			(asus->driver->quirks->wapf > 0))
922 		rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
923 
924 	rfkill_init_sw_state(*rfkill, !result);
925 	result = rfkill_register(*rfkill);
926 	if (result) {
927 		rfkill_destroy(*rfkill);
928 		*rfkill = NULL;
929 		return result;
930 	}
931 	return 0;
932 }
933 
934 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
935 {
936 	if (asus->driver->wlan_ctrl_by_user && ashs_present())
937 		return;
938 
939 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
940 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
941 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
942 	if (asus->wlan.rfkill) {
943 		rfkill_unregister(asus->wlan.rfkill);
944 		rfkill_destroy(asus->wlan.rfkill);
945 		asus->wlan.rfkill = NULL;
946 	}
947 	/*
948 	 * Refresh pci hotplug in case the rfkill state was changed after
949 	 * asus_unregister_rfkill_notifier()
950 	 */
951 	asus_rfkill_hotplug(asus);
952 	if (asus->hotplug_slot.ops)
953 		pci_hp_deregister(&asus->hotplug_slot);
954 	if (asus->hotplug_workqueue)
955 		destroy_workqueue(asus->hotplug_workqueue);
956 
957 	if (asus->bluetooth.rfkill) {
958 		rfkill_unregister(asus->bluetooth.rfkill);
959 		rfkill_destroy(asus->bluetooth.rfkill);
960 		asus->bluetooth.rfkill = NULL;
961 	}
962 	if (asus->wimax.rfkill) {
963 		rfkill_unregister(asus->wimax.rfkill);
964 		rfkill_destroy(asus->wimax.rfkill);
965 		asus->wimax.rfkill = NULL;
966 	}
967 	if (asus->wwan3g.rfkill) {
968 		rfkill_unregister(asus->wwan3g.rfkill);
969 		rfkill_destroy(asus->wwan3g.rfkill);
970 		asus->wwan3g.rfkill = NULL;
971 	}
972 	if (asus->gps.rfkill) {
973 		rfkill_unregister(asus->gps.rfkill);
974 		rfkill_destroy(asus->gps.rfkill);
975 		asus->gps.rfkill = NULL;
976 	}
977 	if (asus->uwb.rfkill) {
978 		rfkill_unregister(asus->uwb.rfkill);
979 		rfkill_destroy(asus->uwb.rfkill);
980 		asus->uwb.rfkill = NULL;
981 	}
982 }
983 
984 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
985 {
986 	int result = 0;
987 
988 	mutex_init(&asus->hotplug_lock);
989 	mutex_init(&asus->wmi_lock);
990 
991 	result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
992 				 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
993 
994 	if (result && result != -ENODEV)
995 		goto exit;
996 
997 	result = asus_new_rfkill(asus, &asus->bluetooth,
998 				 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
999 				 ASUS_WMI_DEVID_BLUETOOTH);
1000 
1001 	if (result && result != -ENODEV)
1002 		goto exit;
1003 
1004 	result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1005 				 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1006 
1007 	if (result && result != -ENODEV)
1008 		goto exit;
1009 
1010 	result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1011 				 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1012 
1013 	if (result && result != -ENODEV)
1014 		goto exit;
1015 
1016 	result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1017 				 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1018 
1019 	if (result && result != -ENODEV)
1020 		goto exit;
1021 
1022 	result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1023 				 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1024 
1025 	if (result && result != -ENODEV)
1026 		goto exit;
1027 
1028 	if (!asus->driver->quirks->hotplug_wireless)
1029 		goto exit;
1030 
1031 	result = asus_setup_pci_hotplug(asus);
1032 	/*
1033 	 * If we get -EBUSY then something else is handling the PCI hotplug -
1034 	 * don't fail in this case
1035 	 */
1036 	if (result == -EBUSY)
1037 		result = 0;
1038 
1039 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1040 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1041 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1042 	/*
1043 	 * Refresh pci hotplug in case the rfkill state was changed during
1044 	 * setup.
1045 	 */
1046 	asus_rfkill_hotplug(asus);
1047 
1048 exit:
1049 	if (result && result != -ENODEV)
1050 		asus_wmi_rfkill_exit(asus);
1051 
1052 	if (result == -ENODEV)
1053 		result = 0;
1054 
1055 	return result;
1056 }
1057 
1058 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1059 {
1060 	struct pci_dev *xhci_pdev;
1061 	u32 orig_ports_available;
1062 	u32 ports_available = asus->driver->quirks->xusb2pr;
1063 
1064 	xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1065 			PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1066 			NULL);
1067 
1068 	if (!xhci_pdev)
1069 		return;
1070 
1071 	pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1072 				&orig_ports_available);
1073 
1074 	pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1075 				cpu_to_le32(ports_available));
1076 
1077 	pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1078 			orig_ports_available, ports_available);
1079 }
1080 
1081 /*
1082  * Some devices dont support or have borcken get_als method
1083  * but still support set method.
1084  */
1085 static void asus_wmi_set_als(void)
1086 {
1087 	asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1088 }
1089 
1090 /*
1091  * Hwmon device
1092  */
1093 static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1094 					  int *speed)
1095 {
1096 	struct fan_args args = {
1097 		.agfn.len = sizeof(args),
1098 		.agfn.mfun = ASUS_FAN_MFUN,
1099 		.agfn.sfun = ASUS_FAN_SFUN_READ,
1100 		.fan = fan,
1101 		.speed = 0,
1102 	};
1103 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1104 	int status;
1105 
1106 	if (fan != 1)
1107 		return -EINVAL;
1108 
1109 	status = asus_wmi_evaluate_method_agfn(input);
1110 
1111 	if (status || args.agfn.err)
1112 		return -ENXIO;
1113 
1114 	if (speed)
1115 		*speed = args.speed;
1116 
1117 	return 0;
1118 }
1119 
1120 static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1121 				     int *speed)
1122 {
1123 	struct fan_args args = {
1124 		.agfn.len = sizeof(args),
1125 		.agfn.mfun = ASUS_FAN_MFUN,
1126 		.agfn.sfun = ASUS_FAN_SFUN_WRITE,
1127 		.fan = fan,
1128 		.speed = speed ?  *speed : 0,
1129 	};
1130 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1131 	int status;
1132 
1133 	/* 1: for setting 1st fan's speed 0: setting auto mode */
1134 	if (fan != 1 && fan != 0)
1135 		return -EINVAL;
1136 
1137 	status = asus_wmi_evaluate_method_agfn(input);
1138 
1139 	if (status || args.agfn.err)
1140 		return -ENXIO;
1141 
1142 	if (speed && fan == 1)
1143 		asus->asus_hwmon_pwm = *speed;
1144 
1145 	return 0;
1146 }
1147 
1148 /*
1149  * Check if we can read the speed of one fan. If true we assume we can also
1150  * control it.
1151  */
1152 static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1153 {
1154 	int status;
1155 	int speed = 0;
1156 
1157 	*num_fans = 0;
1158 
1159 	status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1160 	if (!status)
1161 		*num_fans = 1;
1162 
1163 	return 0;
1164 }
1165 
1166 static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1167 {
1168 	int status;
1169 
1170 	status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1171 	if (status)
1172 		return -ENXIO;
1173 
1174 	asus->asus_hwmon_fan_manual_mode = false;
1175 
1176 	return 0;
1177 }
1178 
1179 static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1180 {
1181 	struct asus_wmi *asus = dev_get_drvdata(dev);
1182 	int value;
1183 	int ret;
1184 
1185 	/* no speed readable on manual mode */
1186 	if (asus->asus_hwmon_fan_manual_mode)
1187 		return -ENXIO;
1188 
1189 	ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1190 	if (ret) {
1191 		pr_warn("reading fan speed failed: %d\n", ret);
1192 		return -ENXIO;
1193 	}
1194 
1195 	return value;
1196 }
1197 
1198 static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1199 {
1200 	int err;
1201 
1202 	if (asus->asus_hwmon_pwm >= 0) {
1203 		*value = asus->asus_hwmon_pwm;
1204 		return;
1205 	}
1206 
1207 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1208 	if (err < 0)
1209 		return;
1210 
1211 	*value &= 0xFF;
1212 
1213 	if (*value == 1) /* Low Speed */
1214 		*value = 85;
1215 	else if (*value == 2)
1216 		*value = 170;
1217 	else if (*value == 3)
1218 		*value = 255;
1219 	else if (*value) {
1220 		pr_err("Unknown fan speed %#x\n", *value);
1221 		*value = -1;
1222 	}
1223 }
1224 
1225 static ssize_t pwm1_show(struct device *dev,
1226 			       struct device_attribute *attr,
1227 			       char *buf)
1228 {
1229 	struct asus_wmi *asus = dev_get_drvdata(dev);
1230 	int value;
1231 
1232 	asus_hwmon_pwm_show(asus, 0, &value);
1233 
1234 	return sprintf(buf, "%d\n", value);
1235 }
1236 
1237 static ssize_t pwm1_store(struct device *dev,
1238 				     struct device_attribute *attr,
1239 				     const char *buf, size_t count) {
1240 	struct asus_wmi *asus = dev_get_drvdata(dev);
1241 	int value;
1242 	int state;
1243 	int ret;
1244 
1245 	ret = kstrtouint(buf, 10, &value);
1246 
1247 	if (ret)
1248 		return ret;
1249 
1250 	value = clamp(value, 0, 255);
1251 
1252 	state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1253 	if (state)
1254 		pr_warn("Setting fan speed failed: %d\n", state);
1255 	else
1256 		asus->asus_hwmon_fan_manual_mode = true;
1257 
1258 	return count;
1259 }
1260 
1261 static ssize_t fan1_input_show(struct device *dev,
1262 					struct device_attribute *attr,
1263 					char *buf)
1264 {
1265 	int value = asus_hwmon_fan_rpm_show(dev, 0);
1266 
1267 	return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1268 
1269 }
1270 
1271 static ssize_t pwm1_enable_show(struct device *dev,
1272 						 struct device_attribute *attr,
1273 						 char *buf)
1274 {
1275 	struct asus_wmi *asus = dev_get_drvdata(dev);
1276 
1277 	if (asus->asus_hwmon_fan_manual_mode)
1278 		return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1279 
1280 	return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1281 }
1282 
1283 static ssize_t pwm1_enable_store(struct device *dev,
1284 						  struct device_attribute *attr,
1285 						  const char *buf, size_t count)
1286 {
1287 	struct asus_wmi *asus = dev_get_drvdata(dev);
1288 	int status = 0;
1289 	int state;
1290 	int ret;
1291 
1292 	ret = kstrtouint(buf, 10, &state);
1293 
1294 	if (ret)
1295 		return ret;
1296 
1297 	if (state == ASUS_FAN_CTRL_MANUAL)
1298 		asus->asus_hwmon_fan_manual_mode = true;
1299 	else
1300 		status = asus_hwmon_fan_set_auto(asus);
1301 
1302 	if (status)
1303 		return status;
1304 
1305 	return count;
1306 }
1307 
1308 static ssize_t fan1_label_show(struct device *dev,
1309 					  struct device_attribute *attr,
1310 					  char *buf)
1311 {
1312 	return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1313 }
1314 
1315 static ssize_t asus_hwmon_temp1(struct device *dev,
1316 				struct device_attribute *attr,
1317 				char *buf)
1318 {
1319 	struct asus_wmi *asus = dev_get_drvdata(dev);
1320 	u32 value;
1321 	int err;
1322 
1323 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1324 
1325 	if (err < 0)
1326 		return err;
1327 
1328 	value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1329 
1330 	return sprintf(buf, "%d\n", value);
1331 }
1332 
1333 /* Fan1 */
1334 static DEVICE_ATTR_RW(pwm1);
1335 static DEVICE_ATTR_RW(pwm1_enable);
1336 static DEVICE_ATTR_RO(fan1_input);
1337 static DEVICE_ATTR_RO(fan1_label);
1338 
1339 /* Temperature */
1340 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1341 
1342 static struct attribute *hwmon_attributes[] = {
1343 	&dev_attr_pwm1.attr,
1344 	&dev_attr_pwm1_enable.attr,
1345 	&dev_attr_fan1_input.attr,
1346 	&dev_attr_fan1_label.attr,
1347 
1348 	&dev_attr_temp1_input.attr,
1349 	NULL
1350 };
1351 
1352 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1353 					  struct attribute *attr, int idx)
1354 {
1355 	struct device *dev = container_of(kobj, struct device, kobj);
1356 	struct platform_device *pdev = to_platform_device(dev->parent);
1357 	struct asus_wmi *asus = platform_get_drvdata(pdev);
1358 	int dev_id = -1;
1359 	int fan_attr = -1;
1360 	u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1361 	bool ok = true;
1362 
1363 	if (attr == &dev_attr_pwm1.attr)
1364 		dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1365 	else if (attr == &dev_attr_temp1_input.attr)
1366 		dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1367 
1368 
1369 	if (attr == &dev_attr_fan1_input.attr
1370 	    || attr == &dev_attr_fan1_label.attr
1371 	    || attr == &dev_attr_pwm1.attr
1372 	    || attr == &dev_attr_pwm1_enable.attr) {
1373 		fan_attr = 1;
1374 	}
1375 
1376 	if (dev_id != -1) {
1377 		int err = asus_wmi_get_devstate(asus, dev_id, &value);
1378 
1379 		if (err < 0 && fan_attr == -1)
1380 			return 0; /* can't return negative here */
1381 	}
1382 
1383 	if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1384 		/*
1385 		 * We need to find a better way, probably using sfun,
1386 		 * bits or spec ...
1387 		 * Currently we disable it if:
1388 		 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1389 		 * - reverved bits are non-zero
1390 		 * - sfun and presence bit are not set
1391 		 */
1392 		if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1393 		    || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1394 			ok = false;
1395 		else
1396 			ok = fan_attr <= asus->asus_hwmon_num_fans;
1397 	} else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1398 		/* If value is zero, something is clearly wrong */
1399 		if (!value)
1400 			ok = false;
1401 	} else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1402 		ok = true;
1403 	} else {
1404 		ok = false;
1405 	}
1406 
1407 	return ok ? attr->mode : 0;
1408 }
1409 
1410 static const struct attribute_group hwmon_attribute_group = {
1411 	.is_visible = asus_hwmon_sysfs_is_visible,
1412 	.attrs = hwmon_attributes
1413 };
1414 __ATTRIBUTE_GROUPS(hwmon_attribute);
1415 
1416 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1417 {
1418 	struct device *hwmon;
1419 
1420 	hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1421 						  "asus", asus,
1422 						  hwmon_attribute_groups);
1423 	if (IS_ERR(hwmon)) {
1424 		pr_err("Could not register asus hwmon device\n");
1425 		return PTR_ERR(hwmon);
1426 	}
1427 	return 0;
1428 }
1429 
1430 /*
1431  * Backlight
1432  */
1433 static int read_backlight_power(struct asus_wmi *asus)
1434 {
1435 	int ret;
1436 	if (asus->driver->quirks->store_backlight_power)
1437 		ret = !asus->driver->panel_power;
1438 	else
1439 		ret = asus_wmi_get_devstate_simple(asus,
1440 						   ASUS_WMI_DEVID_BACKLIGHT);
1441 
1442 	if (ret < 0)
1443 		return ret;
1444 
1445 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1446 }
1447 
1448 static int read_brightness_max(struct asus_wmi *asus)
1449 {
1450 	u32 retval;
1451 	int err;
1452 
1453 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1454 
1455 	if (err < 0)
1456 		return err;
1457 
1458 	retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1459 	retval >>= 8;
1460 
1461 	if (!retval)
1462 		return -ENODEV;
1463 
1464 	return retval;
1465 }
1466 
1467 static int read_brightness(struct backlight_device *bd)
1468 {
1469 	struct asus_wmi *asus = bl_get_data(bd);
1470 	u32 retval;
1471 	int err;
1472 
1473 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1474 
1475 	if (err < 0)
1476 		return err;
1477 
1478 	return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1479 }
1480 
1481 static u32 get_scalar_command(struct backlight_device *bd)
1482 {
1483 	struct asus_wmi *asus = bl_get_data(bd);
1484 	u32 ctrl_param = 0;
1485 
1486 	if ((asus->driver->brightness < bd->props.brightness) ||
1487 	    bd->props.brightness == bd->props.max_brightness)
1488 		ctrl_param = 0x00008001;
1489 	else if ((asus->driver->brightness > bd->props.brightness) ||
1490 		 bd->props.brightness == 0)
1491 		ctrl_param = 0x00008000;
1492 
1493 	asus->driver->brightness = bd->props.brightness;
1494 
1495 	return ctrl_param;
1496 }
1497 
1498 static int update_bl_status(struct backlight_device *bd)
1499 {
1500 	struct asus_wmi *asus = bl_get_data(bd);
1501 	u32 ctrl_param;
1502 	int power, err = 0;
1503 
1504 	power = read_backlight_power(asus);
1505 	if (power != -ENODEV && bd->props.power != power) {
1506 		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1507 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1508 					    ctrl_param, NULL);
1509 		if (asus->driver->quirks->store_backlight_power)
1510 			asus->driver->panel_power = bd->props.power;
1511 
1512 		/* When using scalar brightness, updating the brightness
1513 		 * will mess with the backlight power */
1514 		if (asus->driver->quirks->scalar_panel_brightness)
1515 			return err;
1516 	}
1517 
1518 	if (asus->driver->quirks->scalar_panel_brightness)
1519 		ctrl_param = get_scalar_command(bd);
1520 	else
1521 		ctrl_param = bd->props.brightness;
1522 
1523 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1524 				    ctrl_param, NULL);
1525 
1526 	return err;
1527 }
1528 
1529 static const struct backlight_ops asus_wmi_bl_ops = {
1530 	.get_brightness = read_brightness,
1531 	.update_status = update_bl_status,
1532 };
1533 
1534 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1535 {
1536 	struct backlight_device *bd = asus->backlight_device;
1537 	int old = bd->props.brightness;
1538 	int new = old;
1539 
1540 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1541 		new = code - NOTIFY_BRNUP_MIN + 1;
1542 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1543 		new = code - NOTIFY_BRNDOWN_MIN;
1544 
1545 	bd->props.brightness = new;
1546 	backlight_update_status(bd);
1547 	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1548 
1549 	return old;
1550 }
1551 
1552 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1553 {
1554 	struct backlight_device *bd;
1555 	struct backlight_properties props;
1556 	int max;
1557 	int power;
1558 
1559 	max = read_brightness_max(asus);
1560 	if (max < 0)
1561 		return max;
1562 
1563 	power = read_backlight_power(asus);
1564 
1565 	if (power == -ENODEV)
1566 		power = FB_BLANK_UNBLANK;
1567 	else if (power < 0)
1568 		return power;
1569 
1570 	memset(&props, 0, sizeof(struct backlight_properties));
1571 	props.type = BACKLIGHT_PLATFORM;
1572 	props.max_brightness = max;
1573 	bd = backlight_device_register(asus->driver->name,
1574 				       &asus->platform_device->dev, asus,
1575 				       &asus_wmi_bl_ops, &props);
1576 	if (IS_ERR(bd)) {
1577 		pr_err("Could not register backlight device\n");
1578 		return PTR_ERR(bd);
1579 	}
1580 
1581 	asus->backlight_device = bd;
1582 
1583 	if (asus->driver->quirks->store_backlight_power)
1584 		asus->driver->panel_power = power;
1585 
1586 	bd->props.brightness = read_brightness(bd);
1587 	bd->props.power = power;
1588 	backlight_update_status(bd);
1589 
1590 	asus->driver->brightness = bd->props.brightness;
1591 
1592 	return 0;
1593 }
1594 
1595 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1596 {
1597 	backlight_device_unregister(asus->backlight_device);
1598 
1599 	asus->backlight_device = NULL;
1600 }
1601 
1602 static int is_display_toggle(int code)
1603 {
1604 	/* display toggle keys */
1605 	if ((code >= 0x61 && code <= 0x67) ||
1606 	    (code >= 0x8c && code <= 0x93) ||
1607 	    (code >= 0xa0 && code <= 0xa7) ||
1608 	    (code >= 0xd0 && code <= 0xd5))
1609 		return 1;
1610 
1611 	return 0;
1612 }
1613 
1614 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1615 {
1616 	u32 result;
1617 
1618 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1619 
1620 	return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1621 		!(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1622 }
1623 
1624 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1625 {
1626 	int mode = asus->fnlock_locked;
1627 
1628 	asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1629 }
1630 
1631 static void asus_wmi_notify(u32 value, void *context)
1632 {
1633 	struct asus_wmi *asus = context;
1634 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1635 	union acpi_object *obj;
1636 	acpi_status status;
1637 	int code;
1638 	int orig_code;
1639 	unsigned int key_value = 1;
1640 	bool autorelease = 1;
1641 
1642 	status = wmi_get_event_data(value, &response);
1643 	if (status != AE_OK) {
1644 		pr_err("bad event status 0x%x\n", status);
1645 		return;
1646 	}
1647 
1648 	obj = (union acpi_object *)response.pointer;
1649 
1650 	if (!obj || obj->type != ACPI_TYPE_INTEGER)
1651 		goto exit;
1652 
1653 	code = obj->integer.value;
1654 	orig_code = code;
1655 
1656 	if (asus->driver->key_filter) {
1657 		asus->driver->key_filter(asus->driver, &code, &key_value,
1658 					 &autorelease);
1659 		if (code == ASUS_WMI_KEY_IGNORE)
1660 			goto exit;
1661 	}
1662 
1663 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1664 		code = ASUS_WMI_BRN_UP;
1665 	else if (code >= NOTIFY_BRNDOWN_MIN &&
1666 		 code <= NOTIFY_BRNDOWN_MAX)
1667 		code = ASUS_WMI_BRN_DOWN;
1668 
1669 	if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1670 		if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1671 			asus_wmi_backlight_notify(asus, orig_code);
1672 			goto exit;
1673 		}
1674 	}
1675 
1676 	if (code == NOTIFY_KBD_BRTUP) {
1677 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1678 		goto exit;
1679 	}
1680 	if (code == NOTIFY_KBD_BRTDWN) {
1681 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
1682 		goto exit;
1683 	}
1684 	if (code == NOTIFY_KBD_BRTTOGGLE) {
1685 		if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
1686 			kbd_led_set_by_kbd(asus, 0);
1687 		else
1688 			kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1689 		goto exit;
1690 	}
1691 
1692 	if (code == NOTIFY_FNLOCK_TOGGLE) {
1693 		asus->fnlock_locked = !asus->fnlock_locked;
1694 		asus_wmi_fnlock_update(asus);
1695 		goto exit;
1696 	}
1697 
1698 	if (is_display_toggle(code) &&
1699 	    asus->driver->quirks->no_display_toggle)
1700 		goto exit;
1701 
1702 	if (!sparse_keymap_report_event(asus->inputdev, code,
1703 					key_value, autorelease))
1704 		pr_info("Unknown key %x pressed\n", code);
1705 
1706 exit:
1707 	kfree(obj);
1708 }
1709 
1710 /*
1711  * Sys helpers
1712  */
1713 static int parse_arg(const char *buf, unsigned long count, int *val)
1714 {
1715 	if (!count)
1716 		return 0;
1717 	if (sscanf(buf, "%i", val) != 1)
1718 		return -EINVAL;
1719 	return count;
1720 }
1721 
1722 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1723 			     const char *buf, size_t count)
1724 {
1725 	u32 retval;
1726 	int rv, err, value;
1727 
1728 	value = asus_wmi_get_devstate_simple(asus, devid);
1729 	if (value < 0)
1730 		return value;
1731 
1732 	rv = parse_arg(buf, count, &value);
1733 	err = asus_wmi_set_devstate(devid, value, &retval);
1734 
1735 	if (err < 0)
1736 		return err;
1737 
1738 	return rv;
1739 }
1740 
1741 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1742 {
1743 	int value = asus_wmi_get_devstate_simple(asus, devid);
1744 
1745 	if (value < 0)
1746 		return value;
1747 
1748 	return sprintf(buf, "%d\n", value);
1749 }
1750 
1751 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
1752 	static ssize_t show_##_name(struct device *dev,			\
1753 				    struct device_attribute *attr,	\
1754 				    char *buf)				\
1755 	{								\
1756 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
1757 									\
1758 		return show_sys_wmi(asus, _cm, buf);			\
1759 	}								\
1760 	static ssize_t store_##_name(struct device *dev,		\
1761 				     struct device_attribute *attr,	\
1762 				     const char *buf, size_t count)	\
1763 	{								\
1764 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
1765 									\
1766 		return store_sys_wmi(asus, _cm, buf, count);		\
1767 	}								\
1768 	static struct device_attribute dev_attr_##_name = {		\
1769 		.attr = {						\
1770 			.name = __stringify(_name),			\
1771 			.mode = _mode },				\
1772 		.show   = show_##_name,					\
1773 		.store  = store_##_name,				\
1774 	}
1775 
1776 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1777 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1778 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
1779 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1780 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
1781 
1782 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
1783 			   const char *buf, size_t count)
1784 {
1785 	int value, rv;
1786 
1787 	if (!count || sscanf(buf, "%i", &value) != 1)
1788 		return -EINVAL;
1789 	if (value < 0 || value > 2)
1790 		return -EINVAL;
1791 
1792 	rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1793 	if (rv < 0)
1794 		return rv;
1795 
1796 	return count;
1797 }
1798 
1799 static DEVICE_ATTR_WO(cpufv);
1800 
1801 static struct attribute *platform_attributes[] = {
1802 	&dev_attr_cpufv.attr,
1803 	&dev_attr_camera.attr,
1804 	&dev_attr_cardr.attr,
1805 	&dev_attr_touchpad.attr,
1806 	&dev_attr_lid_resume.attr,
1807 	&dev_attr_als_enable.attr,
1808 	NULL
1809 };
1810 
1811 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1812 				    struct attribute *attr, int idx)
1813 {
1814 	struct device *dev = container_of(kobj, struct device, kobj);
1815 	struct asus_wmi *asus = dev_get_drvdata(dev);
1816 	bool ok = true;
1817 	int devid = -1;
1818 
1819 	if (attr == &dev_attr_camera.attr)
1820 		devid = ASUS_WMI_DEVID_CAMERA;
1821 	else if (attr == &dev_attr_cardr.attr)
1822 		devid = ASUS_WMI_DEVID_CARDREADER;
1823 	else if (attr == &dev_attr_touchpad.attr)
1824 		devid = ASUS_WMI_DEVID_TOUCHPAD;
1825 	else if (attr == &dev_attr_lid_resume.attr)
1826 		devid = ASUS_WMI_DEVID_LID_RESUME;
1827 	else if (attr == &dev_attr_als_enable.attr)
1828 		devid = ASUS_WMI_DEVID_ALS_ENABLE;
1829 
1830 	if (devid != -1)
1831 		ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1832 
1833 	return ok ? attr->mode : 0;
1834 }
1835 
1836 static const struct attribute_group platform_attribute_group = {
1837 	.is_visible = asus_sysfs_is_visible,
1838 	.attrs = platform_attributes
1839 };
1840 
1841 static void asus_wmi_sysfs_exit(struct platform_device *device)
1842 {
1843 	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1844 }
1845 
1846 static int asus_wmi_sysfs_init(struct platform_device *device)
1847 {
1848 	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1849 }
1850 
1851 /*
1852  * Platform device
1853  */
1854 static int asus_wmi_platform_init(struct asus_wmi *asus)
1855 {
1856 	int rv;
1857 
1858 	/* INIT enable hotkeys on some models */
1859 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1860 		pr_info("Initialization: %#x\n", rv);
1861 
1862 	/* We don't know yet what to do with this version... */
1863 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1864 		pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
1865 		asus->spec = rv;
1866 	}
1867 
1868 	/*
1869 	 * The SFUN method probably allows the original driver to get the list
1870 	 * of features supported by a given model. For now, 0x0100 or 0x0800
1871 	 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1872 	 * The significance of others is yet to be found.
1873 	 */
1874 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1875 		pr_info("SFUN value: %#x\n", rv);
1876 		asus->sfun = rv;
1877 	}
1878 
1879 	/*
1880 	 * Eee PC and Notebooks seems to have different method_id for DSTS,
1881 	 * but it may also be related to the BIOS's SPEC.
1882 	 * Note, on most Eeepc, there is no way to check if a method exist
1883 	 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1884 	 * but once again, SPEC may probably be used for that kind of things.
1885 	 */
1886 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1887 		asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1888 	else
1889 		asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1890 
1891 	/* CWAP allow to define the behavior of the Fn+F2 key,
1892 	 * this method doesn't seems to be present on Eee PCs */
1893 	if (asus->driver->quirks->wapf >= 0)
1894 		asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1895 				      asus->driver->quirks->wapf, NULL);
1896 
1897 	return asus_wmi_sysfs_init(asus->platform_device);
1898 }
1899 
1900 static void asus_wmi_platform_exit(struct asus_wmi *asus)
1901 {
1902 	asus_wmi_sysfs_exit(asus->platform_device);
1903 }
1904 
1905 /*
1906  * debugfs
1907  */
1908 struct asus_wmi_debugfs_node {
1909 	struct asus_wmi *asus;
1910 	char *name;
1911 	int (*show) (struct seq_file *m, void *data);
1912 };
1913 
1914 static int show_dsts(struct seq_file *m, void *data)
1915 {
1916 	struct asus_wmi *asus = m->private;
1917 	int err;
1918 	u32 retval = -1;
1919 
1920 	err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1921 
1922 	if (err < 0)
1923 		return err;
1924 
1925 	seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1926 
1927 	return 0;
1928 }
1929 
1930 static int show_devs(struct seq_file *m, void *data)
1931 {
1932 	struct asus_wmi *asus = m->private;
1933 	int err;
1934 	u32 retval = -1;
1935 
1936 	err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1937 				    &retval);
1938 
1939 	if (err < 0)
1940 		return err;
1941 
1942 	seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1943 		   asus->debug.ctrl_param, retval);
1944 
1945 	return 0;
1946 }
1947 
1948 static int show_call(struct seq_file *m, void *data)
1949 {
1950 	struct asus_wmi *asus = m->private;
1951 	struct bios_args args = {
1952 		.arg0 = asus->debug.dev_id,
1953 		.arg1 = asus->debug.ctrl_param,
1954 	};
1955 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1956 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1957 	union acpi_object *obj;
1958 	acpi_status status;
1959 
1960 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1961 				     0, asus->debug.method_id,
1962 				     &input, &output);
1963 
1964 	if (ACPI_FAILURE(status))
1965 		return -EIO;
1966 
1967 	obj = (union acpi_object *)output.pointer;
1968 	if (obj && obj->type == ACPI_TYPE_INTEGER)
1969 		seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1970 			   asus->debug.dev_id, asus->debug.ctrl_param,
1971 			   (u32) obj->integer.value);
1972 	else
1973 		seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1974 			   asus->debug.dev_id, asus->debug.ctrl_param,
1975 			   obj ? obj->type : -1);
1976 
1977 	kfree(obj);
1978 
1979 	return 0;
1980 }
1981 
1982 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1983 	{NULL, "devs", show_devs},
1984 	{NULL, "dsts", show_dsts},
1985 	{NULL, "call", show_call},
1986 };
1987 
1988 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
1989 {
1990 	struct asus_wmi_debugfs_node *node = inode->i_private;
1991 
1992 	return single_open(file, node->show, node->asus);
1993 }
1994 
1995 static const struct file_operations asus_wmi_debugfs_io_ops = {
1996 	.owner = THIS_MODULE,
1997 	.open = asus_wmi_debugfs_open,
1998 	.read = seq_read,
1999 	.llseek = seq_lseek,
2000 	.release = single_release,
2001 };
2002 
2003 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2004 {
2005 	debugfs_remove_recursive(asus->debug.root);
2006 }
2007 
2008 static int asus_wmi_debugfs_init(struct asus_wmi *asus)
2009 {
2010 	struct dentry *dent;
2011 	int i;
2012 
2013 	asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2014 	if (!asus->debug.root) {
2015 		pr_err("failed to create debugfs directory\n");
2016 		goto error_debugfs;
2017 	}
2018 
2019 	dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2020 				  asus->debug.root, &asus->debug.method_id);
2021 	if (!dent)
2022 		goto error_debugfs;
2023 
2024 	dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2025 				  asus->debug.root, &asus->debug.dev_id);
2026 	if (!dent)
2027 		goto error_debugfs;
2028 
2029 	dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2030 				  asus->debug.root, &asus->debug.ctrl_param);
2031 	if (!dent)
2032 		goto error_debugfs;
2033 
2034 	for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2035 		struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2036 
2037 		node->asus = asus;
2038 		dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2039 					   asus->debug.root, node,
2040 					   &asus_wmi_debugfs_io_ops);
2041 		if (!dent) {
2042 			pr_err("failed to create debug file: %s\n", node->name);
2043 			goto error_debugfs;
2044 		}
2045 	}
2046 
2047 	return 0;
2048 
2049 error_debugfs:
2050 	asus_wmi_debugfs_exit(asus);
2051 	return -ENOMEM;
2052 }
2053 
2054 static int asus_wmi_fan_init(struct asus_wmi *asus)
2055 {
2056 	int status;
2057 
2058 	asus->asus_hwmon_pwm = -1;
2059 	asus->asus_hwmon_num_fans = -1;
2060 	asus->asus_hwmon_fan_manual_mode = false;
2061 
2062 	status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2063 	if (status) {
2064 		asus->asus_hwmon_num_fans = 0;
2065 		pr_warn("Could not determine number of fans: %d\n", status);
2066 		return -ENXIO;
2067 	}
2068 
2069 	pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2070 	return 0;
2071 }
2072 
2073 /*
2074  * WMI Driver
2075  */
2076 static int asus_wmi_add(struct platform_device *pdev)
2077 {
2078 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2079 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2080 	struct asus_wmi *asus;
2081 	const char *chassis_type;
2082 	acpi_status status;
2083 	int err;
2084 	u32 result;
2085 
2086 	asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2087 	if (!asus)
2088 		return -ENOMEM;
2089 
2090 	asus->driver = wdrv;
2091 	asus->platform_device = pdev;
2092 	wdrv->platform_device = pdev;
2093 	platform_set_drvdata(asus->platform_device, asus);
2094 
2095 	if (wdrv->detect_quirks)
2096 		wdrv->detect_quirks(asus->driver);
2097 
2098 	err = asus_wmi_platform_init(asus);
2099 	if (err)
2100 		goto fail_platform;
2101 
2102 	err = asus_wmi_input_init(asus);
2103 	if (err)
2104 		goto fail_input;
2105 
2106 	err = asus_wmi_fan_init(asus); /* probably no problems on error */
2107 	asus_hwmon_fan_set_auto(asus);
2108 
2109 	err = asus_wmi_hwmon_init(asus);
2110 	if (err)
2111 		goto fail_hwmon;
2112 
2113 	err = asus_wmi_led_init(asus);
2114 	if (err)
2115 		goto fail_leds;
2116 
2117 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2118 	if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2119 		asus->driver->wlan_ctrl_by_user = 1;
2120 
2121 	if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2122 		err = asus_wmi_rfkill_init(asus);
2123 		if (err)
2124 			goto fail_rfkill;
2125 	}
2126 
2127 	if (asus->driver->quirks->wmi_force_als_set)
2128 		asus_wmi_set_als();
2129 
2130 	/* Some Asus desktop boards export an acpi-video backlight interface,
2131 	   stop this from showing up */
2132 	chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2133 	if (chassis_type && !strcmp(chassis_type, "3"))
2134 		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2135 
2136 	if (asus->driver->quirks->wmi_backlight_power)
2137 		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2138 
2139 	if (asus->driver->quirks->wmi_backlight_native)
2140 		acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2141 
2142 	if (asus->driver->quirks->xusb2pr)
2143 		asus_wmi_set_xusb2pr(asus);
2144 
2145 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2146 		err = asus_wmi_backlight_init(asus);
2147 		if (err && err != -ENODEV)
2148 			goto fail_backlight;
2149 	} else if (asus->driver->quirks->wmi_backlight_set_devstate)
2150 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2151 
2152 	if (asus_wmi_has_fnlock_key(asus)) {
2153 		asus->fnlock_locked = true;
2154 		asus_wmi_fnlock_update(asus);
2155 	}
2156 
2157 	status = wmi_install_notify_handler(asus->driver->event_guid,
2158 					    asus_wmi_notify, asus);
2159 	if (ACPI_FAILURE(status)) {
2160 		pr_err("Unable to register notify handler - %d\n", status);
2161 		err = -ENODEV;
2162 		goto fail_wmi_handler;
2163 	}
2164 
2165 	err = asus_wmi_debugfs_init(asus);
2166 	if (err)
2167 		goto fail_debugfs;
2168 
2169 	return 0;
2170 
2171 fail_debugfs:
2172 	wmi_remove_notify_handler(asus->driver->event_guid);
2173 fail_wmi_handler:
2174 	asus_wmi_backlight_exit(asus);
2175 fail_backlight:
2176 	asus_wmi_rfkill_exit(asus);
2177 fail_rfkill:
2178 	asus_wmi_led_exit(asus);
2179 fail_leds:
2180 fail_hwmon:
2181 	asus_wmi_input_exit(asus);
2182 fail_input:
2183 	asus_wmi_platform_exit(asus);
2184 fail_platform:
2185 	kfree(asus);
2186 	return err;
2187 }
2188 
2189 static int asus_wmi_remove(struct platform_device *device)
2190 {
2191 	struct asus_wmi *asus;
2192 
2193 	asus = platform_get_drvdata(device);
2194 	wmi_remove_notify_handler(asus->driver->event_guid);
2195 	asus_wmi_backlight_exit(asus);
2196 	asus_wmi_input_exit(asus);
2197 	asus_wmi_led_exit(asus);
2198 	asus_wmi_rfkill_exit(asus);
2199 	asus_wmi_debugfs_exit(asus);
2200 	asus_wmi_platform_exit(asus);
2201 	asus_hwmon_fan_set_auto(asus);
2202 
2203 	kfree(asus);
2204 	return 0;
2205 }
2206 
2207 /*
2208  * Platform driver - hibernate/resume callbacks
2209  */
2210 static int asus_hotk_thaw(struct device *device)
2211 {
2212 	struct asus_wmi *asus = dev_get_drvdata(device);
2213 
2214 	if (asus->wlan.rfkill) {
2215 		bool wlan;
2216 
2217 		/*
2218 		 * Work around bios bug - acpi _PTS turns off the wireless led
2219 		 * during suspend.  Normally it restores it on resume, but
2220 		 * we should kick it ourselves in case hibernation is aborted.
2221 		 */
2222 		wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2223 		asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2224 	}
2225 
2226 	return 0;
2227 }
2228 
2229 static int asus_hotk_resume(struct device *device)
2230 {
2231 	struct asus_wmi *asus = dev_get_drvdata(device);
2232 
2233 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2234 		kbd_led_update(asus);
2235 
2236 	if (asus_wmi_has_fnlock_key(asus))
2237 		asus_wmi_fnlock_update(asus);
2238 	return 0;
2239 }
2240 
2241 static int asus_hotk_restore(struct device *device)
2242 {
2243 	struct asus_wmi *asus = dev_get_drvdata(device);
2244 	int bl;
2245 
2246 	/* Refresh both wlan rfkill state and pci hotplug */
2247 	if (asus->wlan.rfkill)
2248 		asus_rfkill_hotplug(asus);
2249 
2250 	if (asus->bluetooth.rfkill) {
2251 		bl = !asus_wmi_get_devstate_simple(asus,
2252 						   ASUS_WMI_DEVID_BLUETOOTH);
2253 		rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2254 	}
2255 	if (asus->wimax.rfkill) {
2256 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2257 		rfkill_set_sw_state(asus->wimax.rfkill, bl);
2258 	}
2259 	if (asus->wwan3g.rfkill) {
2260 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2261 		rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2262 	}
2263 	if (asus->gps.rfkill) {
2264 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2265 		rfkill_set_sw_state(asus->gps.rfkill, bl);
2266 	}
2267 	if (asus->uwb.rfkill) {
2268 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2269 		rfkill_set_sw_state(asus->uwb.rfkill, bl);
2270 	}
2271 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2272 		kbd_led_update(asus);
2273 
2274 	if (asus_wmi_has_fnlock_key(asus))
2275 		asus_wmi_fnlock_update(asus);
2276 	return 0;
2277 }
2278 
2279 static const struct dev_pm_ops asus_pm_ops = {
2280 	.thaw = asus_hotk_thaw,
2281 	.restore = asus_hotk_restore,
2282 	.resume = asus_hotk_resume,
2283 };
2284 
2285 static int asus_wmi_probe(struct platform_device *pdev)
2286 {
2287 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2288 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2289 	int ret;
2290 
2291 	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2292 		pr_warn("ASUS Management GUID not found\n");
2293 		return -ENODEV;
2294 	}
2295 
2296 	if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2297 		pr_warn("ASUS Event GUID not found\n");
2298 		return -ENODEV;
2299 	}
2300 
2301 	if (wdrv->probe) {
2302 		ret = wdrv->probe(pdev);
2303 		if (ret)
2304 			return ret;
2305 	}
2306 
2307 	return asus_wmi_add(pdev);
2308 }
2309 
2310 static bool used;
2311 
2312 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2313 {
2314 	struct platform_driver *platform_driver;
2315 	struct platform_device *platform_device;
2316 
2317 	if (used)
2318 		return -EBUSY;
2319 
2320 	platform_driver = &driver->platform_driver;
2321 	platform_driver->remove = asus_wmi_remove;
2322 	platform_driver->driver.owner = driver->owner;
2323 	platform_driver->driver.name = driver->name;
2324 	platform_driver->driver.pm = &asus_pm_ops;
2325 
2326 	platform_device = platform_create_bundle(platform_driver,
2327 						 asus_wmi_probe,
2328 						 NULL, 0, NULL, 0);
2329 	if (IS_ERR(platform_device))
2330 		return PTR_ERR(platform_device);
2331 
2332 	used = true;
2333 	return 0;
2334 }
2335 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2336 
2337 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2338 {
2339 	platform_device_unregister(driver->platform_device);
2340 	platform_driver_unregister(&driver->platform_driver);
2341 	used = false;
2342 }
2343 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2344 
2345 static int __init asus_wmi_init(void)
2346 {
2347 	pr_info("ASUS WMI generic driver loaded\n");
2348 	return 0;
2349 }
2350 
2351 static void __exit asus_wmi_exit(void)
2352 {
2353 	pr_info("ASUS WMI generic driver unloaded\n");
2354 }
2355 
2356 module_init(asus_wmi_init);
2357 module_exit(asus_wmi_exit);
2358