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