xref: /openbmc/linux/drivers/platform/x86/asus-wmi.c (revision f14c1a14)
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/acpi.h>
17 #include <linux/backlight.h>
18 #include <linux/debugfs.h>
19 #include <linux/dmi.h>
20 #include <linux/fb.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/init.h>
24 #include <linux/input.h>
25 #include <linux/input/sparse-keymap.h>
26 #include <linux/kernel.h>
27 #include <linux/leds.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/pci_hotplug.h>
31 #include <linux/platform_data/x86/asus-wmi.h>
32 #include <linux/platform_device.h>
33 #include <linux/platform_profile.h>
34 #include <linux/power_supply.h>
35 #include <linux/rfkill.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/types.h>
39 #include <linux/units.h>
40 
41 #include <acpi/battery.h>
42 #include <acpi/video.h>
43 
44 #include "asus-wmi.h"
45 
46 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
47 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
48 MODULE_DESCRIPTION("Asus Generic WMI Driver");
49 MODULE_LICENSE("GPL");
50 
51 static bool fnlock_default = true;
52 module_param(fnlock_default, bool, 0444);
53 
54 #define to_asus_wmi_driver(pdrv)					\
55 	(container_of((pdrv), struct asus_wmi_driver, platform_driver))
56 
57 #define ASUS_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
58 
59 #define NOTIFY_BRNUP_MIN		0x11
60 #define NOTIFY_BRNUP_MAX		0x1f
61 #define NOTIFY_BRNDOWN_MIN		0x20
62 #define NOTIFY_BRNDOWN_MAX		0x2e
63 #define NOTIFY_FNLOCK_TOGGLE		0x4e
64 #define NOTIFY_KBD_DOCK_CHANGE		0x75
65 #define NOTIFY_KBD_BRTUP		0xc4
66 #define NOTIFY_KBD_BRTDWN		0xc5
67 #define NOTIFY_KBD_BRTTOGGLE		0xc7
68 #define NOTIFY_KBD_FBM			0x99
69 #define NOTIFY_KBD_TTP			0xae
70 #define NOTIFY_LID_FLIP			0xfa
71 #define NOTIFY_LID_FLIP_ROG		0xbd
72 
73 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
74 
75 #define ASUS_GPU_FAN_DESC		"gpu_fan"
76 #define ASUS_FAN_DESC			"cpu_fan"
77 #define ASUS_FAN_MFUN			0x13
78 #define ASUS_FAN_SFUN_READ		0x06
79 #define ASUS_FAN_SFUN_WRITE		0x07
80 
81 /* Based on standard hwmon pwmX_enable values */
82 #define ASUS_FAN_CTRL_FULLSPEED		0
83 #define ASUS_FAN_CTRL_MANUAL		1
84 #define ASUS_FAN_CTRL_AUTO		2
85 
86 #define ASUS_FAN_BOOST_MODE_NORMAL		0
87 #define ASUS_FAN_BOOST_MODE_OVERBOOST		1
88 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK	0x01
89 #define ASUS_FAN_BOOST_MODE_SILENT		2
90 #define ASUS_FAN_BOOST_MODE_SILENT_MASK		0x02
91 #define ASUS_FAN_BOOST_MODES_MASK		0x03
92 
93 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT	0
94 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST	1
95 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT	2
96 
97 #define USB_INTEL_XUSB2PR		0xD0
98 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
99 
100 #define ASUS_ACPI_UID_ASUSWMI		"ASUSWMI"
101 #define ASUS_ACPI_UID_ATK		"ATK"
102 
103 #define WMI_EVENT_QUEUE_SIZE		0x10
104 #define WMI_EVENT_QUEUE_END		0x1
105 #define WMI_EVENT_MASK			0xFFFF
106 /* The WMI hotkey event value is always the same. */
107 #define WMI_EVENT_VALUE_ATK		0xFF
108 
109 #define WMI_EVENT_MASK			0xFFFF
110 
111 #define FAN_CURVE_POINTS		8
112 #define FAN_CURVE_BUF_LEN		32
113 #define FAN_CURVE_DEV_CPU		0x00
114 #define FAN_CURVE_DEV_GPU		0x01
115 /* Mask to determine if setting temperature or percentage */
116 #define FAN_CURVE_PWM_MASK		0x04
117 
118 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
119 
120 static int throttle_thermal_policy_write(struct asus_wmi *);
121 
122 static bool ashs_present(void)
123 {
124 	int i = 0;
125 	while (ashs_ids[i]) {
126 		if (acpi_dev_found(ashs_ids[i++]))
127 			return true;
128 	}
129 	return false;
130 }
131 
132 struct bios_args {
133 	u32 arg0;
134 	u32 arg1;
135 	u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
136 	u32 arg3;
137 	u32 arg4; /* Some ROG laptops require a full 5 input args */
138 	u32 arg5;
139 } __packed;
140 
141 /*
142  * Struct that's used for all methods called via AGFN. Naming is
143  * identically to the AML code.
144  */
145 struct agfn_args {
146 	u16 mfun; /* probably "Multi-function" to be called */
147 	u16 sfun; /* probably "Sub-function" to be called */
148 	u16 len;  /* size of the hole struct, including subfunction fields */
149 	u8 stas;  /* not used by now */
150 	u8 err;   /* zero on success */
151 } __packed;
152 
153 /* struct used for calling fan read and write methods */
154 struct agfn_fan_args {
155 	struct agfn_args agfn;	/* common fields */
156 	u8 fan;			/* fan number: 0: set auto mode 1: 1st fan */
157 	u32 speed;		/* read: RPM/100 - write: 0-255 */
158 } __packed;
159 
160 /*
161  * <platform>/    - debugfs root directory
162  *   dev_id      - current dev_id
163  *   ctrl_param  - current ctrl_param
164  *   method_id   - current method_id
165  *   devs        - call DEVS(dev_id, ctrl_param) and print result
166  *   dsts        - call DSTS(dev_id)  and print result
167  *   call        - call method_id(dev_id, ctrl_param) and print result
168  */
169 struct asus_wmi_debug {
170 	struct dentry *root;
171 	u32 method_id;
172 	u32 dev_id;
173 	u32 ctrl_param;
174 };
175 
176 struct asus_rfkill {
177 	struct asus_wmi *asus;
178 	struct rfkill *rfkill;
179 	u32 dev_id;
180 };
181 
182 enum fan_type {
183 	FAN_TYPE_NONE = 0,
184 	FAN_TYPE_AGFN,		/* deprecated on newer platforms */
185 	FAN_TYPE_SPEC83,	/* starting in Spec 8.3, use CPU_FAN_CTRL */
186 };
187 
188 struct fan_curve_data {
189 	bool enabled;
190 	u32 device_id;
191 	u8 temps[FAN_CURVE_POINTS];
192 	u8 percents[FAN_CURVE_POINTS];
193 };
194 
195 struct asus_wmi {
196 	int dsts_id;
197 	int spec;
198 	int sfun;
199 	bool wmi_event_queue;
200 
201 	struct input_dev *inputdev;
202 	struct backlight_device *backlight_device;
203 	struct platform_device *platform_device;
204 
205 	struct led_classdev wlan_led;
206 	int wlan_led_wk;
207 	struct led_classdev tpd_led;
208 	int tpd_led_wk;
209 	struct led_classdev kbd_led;
210 	int kbd_led_wk;
211 	struct led_classdev lightbar_led;
212 	int lightbar_led_wk;
213 	struct led_classdev micmute_led;
214 	struct workqueue_struct *led_workqueue;
215 	struct work_struct tpd_led_work;
216 	struct work_struct wlan_led_work;
217 	struct work_struct lightbar_led_work;
218 
219 	struct asus_rfkill wlan;
220 	struct asus_rfkill bluetooth;
221 	struct asus_rfkill wimax;
222 	struct asus_rfkill wwan3g;
223 	struct asus_rfkill gps;
224 	struct asus_rfkill uwb;
225 
226 	int tablet_switch_event_code;
227 	u32 tablet_switch_dev_id;
228 	bool tablet_switch_inverted;
229 
230 	enum fan_type fan_type;
231 	enum fan_type gpu_fan_type;
232 	int fan_pwm_mode;
233 	int gpu_fan_pwm_mode;
234 	int agfn_pwm;
235 
236 	bool fan_boost_mode_available;
237 	u8 fan_boost_mode_mask;
238 	u8 fan_boost_mode;
239 
240 	bool egpu_enable_available;
241 	bool dgpu_disable_available;
242 	bool gpu_mux_mode_available;
243 
244 	bool kbd_rgb_mode_available;
245 	bool kbd_rgb_state_available;
246 
247 	bool throttle_thermal_policy_available;
248 	u8 throttle_thermal_policy_mode;
249 
250 	bool cpu_fan_curve_available;
251 	bool gpu_fan_curve_available;
252 	struct fan_curve_data custom_fan_curves[2];
253 
254 	struct platform_profile_handler platform_profile_handler;
255 	bool platform_profile_support;
256 
257 	// The RSOC controls the maximum charging percentage.
258 	bool battery_rsoc_available;
259 
260 	bool panel_overdrive_available;
261 
262 	struct hotplug_slot hotplug_slot;
263 	struct mutex hotplug_lock;
264 	struct mutex wmi_lock;
265 	struct workqueue_struct *hotplug_workqueue;
266 	struct work_struct hotplug_work;
267 
268 	bool fnlock_locked;
269 
270 	struct asus_wmi_debug debug;
271 
272 	struct asus_wmi_driver *driver;
273 };
274 
275 /* WMI ************************************************************************/
276 
277 static int asus_wmi_evaluate_method3(u32 method_id,
278 		u32 arg0, u32 arg1, u32 arg2, u32 *retval)
279 {
280 	struct bios_args args = {
281 		.arg0 = arg0,
282 		.arg1 = arg1,
283 		.arg2 = arg2,
284 	};
285 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
286 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
287 	acpi_status status;
288 	union acpi_object *obj;
289 	u32 tmp = 0;
290 
291 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
292 				     &input, &output);
293 
294 	if (ACPI_FAILURE(status))
295 		return -EIO;
296 
297 	obj = (union acpi_object *)output.pointer;
298 	if (obj && obj->type == ACPI_TYPE_INTEGER)
299 		tmp = (u32) obj->integer.value;
300 
301 	if (retval)
302 		*retval = tmp;
303 
304 	kfree(obj);
305 
306 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
307 		return -ENODEV;
308 
309 	return 0;
310 }
311 
312 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
313 {
314 	return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
315 }
316 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
317 
318 static int asus_wmi_evaluate_method5(u32 method_id,
319 		u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
320 {
321 	struct bios_args args = {
322 		.arg0 = arg0,
323 		.arg1 = arg1,
324 		.arg2 = arg2,
325 		.arg3 = arg3,
326 		.arg4 = arg4,
327 	};
328 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
329 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
330 	acpi_status status;
331 	union acpi_object *obj;
332 	u32 tmp = 0;
333 
334 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
335 				     &input, &output);
336 
337 	if (ACPI_FAILURE(status))
338 		return -EIO;
339 
340 	obj = (union acpi_object *)output.pointer;
341 	if (obj && obj->type == ACPI_TYPE_INTEGER)
342 		tmp = (u32) obj->integer.value;
343 
344 	if (retval)
345 		*retval = tmp;
346 
347 	kfree(obj);
348 
349 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
350 		return -ENODEV;
351 
352 	return 0;
353 }
354 
355 /*
356  * Returns as an error if the method output is not a buffer. Typically this
357  * means that the method called is unsupported.
358  */
359 static int asus_wmi_evaluate_method_buf(u32 method_id,
360 		u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
361 {
362 	struct bios_args args = {
363 		.arg0 = arg0,
364 		.arg1 = arg1,
365 		.arg2 = 0,
366 	};
367 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
368 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
369 	acpi_status status;
370 	union acpi_object *obj;
371 	int err = 0;
372 
373 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
374 				     &input, &output);
375 
376 	if (ACPI_FAILURE(status))
377 		return -EIO;
378 
379 	obj = (union acpi_object *)output.pointer;
380 
381 	switch (obj->type) {
382 	case ACPI_TYPE_BUFFER:
383 		if (obj->buffer.length > size) {
384 			err = -ENOSPC;
385 			break;
386 		}
387 		if (obj->buffer.length == 0) {
388 			err = -ENODATA;
389 			break;
390 		}
391 
392 		memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
393 		break;
394 	case ACPI_TYPE_INTEGER:
395 		err = (u32)obj->integer.value;
396 
397 		if (err == ASUS_WMI_UNSUPPORTED_METHOD)
398 			err = -ENODEV;
399 		/*
400 		 * At least one method returns a 0 with no buffer if no arg
401 		 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
402 		 */
403 		if (err == 0)
404 			err = -ENODATA;
405 		break;
406 	default:
407 		err = -ENODATA;
408 		break;
409 	}
410 
411 	kfree(obj);
412 
413 	if (err)
414 		return err;
415 
416 	return 0;
417 }
418 
419 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
420 {
421 	struct acpi_buffer input;
422 	u64 phys_addr;
423 	u32 retval;
424 	u32 status;
425 
426 	/*
427 	 * Copy to dma capable address otherwise memory corruption occurs as
428 	 * bios has to be able to access it.
429 	 */
430 	input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
431 	input.length = args.length;
432 	if (!input.pointer)
433 		return -ENOMEM;
434 	phys_addr = virt_to_phys(input.pointer);
435 
436 	status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
437 					phys_addr, 0, &retval);
438 	if (!status)
439 		memcpy(args.pointer, input.pointer, args.length);
440 
441 	kfree(input.pointer);
442 	if (status)
443 		return -ENXIO;
444 
445 	return retval;
446 }
447 
448 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
449 {
450 	return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
451 }
452 
453 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
454 				 u32 *retval)
455 {
456 	return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
457 					ctrl_param, retval);
458 }
459 
460 /* Helper for special devices with magic return codes */
461 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
462 				      u32 dev_id, u32 mask)
463 {
464 	u32 retval = 0;
465 	int err;
466 
467 	err = asus_wmi_get_devstate(asus, dev_id, &retval);
468 	if (err < 0)
469 		return err;
470 
471 	if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
472 		return -ENODEV;
473 
474 	if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
475 		if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
476 			return -ENODEV;
477 	}
478 
479 	return retval & mask;
480 }
481 
482 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
483 {
484 	return asus_wmi_get_devstate_bits(asus, dev_id,
485 					  ASUS_WMI_DSTS_STATUS_BIT);
486 }
487 
488 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
489 {
490 	u32 retval;
491 	int status = asus_wmi_get_devstate(asus, dev_id, &retval);
492 
493 	return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
494 }
495 
496 /* Input **********************************************************************/
497 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
498 {
499 	input_report_switch(asus->inputdev, SW_TABLET_MODE,
500 			    asus->tablet_switch_inverted ? !value : value);
501 	input_sync(asus->inputdev);
502 }
503 
504 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
505 {
506 	struct device *dev = &asus->platform_device->dev;
507 	int result;
508 
509 	result = asus_wmi_get_devstate_simple(asus, dev_id);
510 	if (result >= 0) {
511 		input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
512 		asus_wmi_tablet_sw_report(asus, result);
513 		asus->tablet_switch_dev_id = dev_id;
514 		asus->tablet_switch_event_code = event_code;
515 	} else if (result == -ENODEV) {
516 		dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
517 	} else {
518 		dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
519 	}
520 }
521 
522 static int asus_wmi_input_init(struct asus_wmi *asus)
523 {
524 	struct device *dev = &asus->platform_device->dev;
525 	int err;
526 
527 	asus->inputdev = input_allocate_device();
528 	if (!asus->inputdev)
529 		return -ENOMEM;
530 
531 	asus->inputdev->name = asus->driver->input_name;
532 	asus->inputdev->phys = asus->driver->input_phys;
533 	asus->inputdev->id.bustype = BUS_HOST;
534 	asus->inputdev->dev.parent = dev;
535 	set_bit(EV_REP, asus->inputdev->evbit);
536 
537 	err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
538 	if (err)
539 		goto err_free_dev;
540 
541 	switch (asus->driver->quirks->tablet_switch_mode) {
542 	case asus_wmi_no_tablet_switch:
543 		break;
544 	case asus_wmi_kbd_dock_devid:
545 		asus->tablet_switch_inverted = true;
546 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
547 		break;
548 	case asus_wmi_lid_flip_devid:
549 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
550 		break;
551 	case asus_wmi_lid_flip_rog_devid:
552 		asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
553 		break;
554 	}
555 
556 	err = input_register_device(asus->inputdev);
557 	if (err)
558 		goto err_free_dev;
559 
560 	return 0;
561 
562 err_free_dev:
563 	input_free_device(asus->inputdev);
564 	return err;
565 }
566 
567 static void asus_wmi_input_exit(struct asus_wmi *asus)
568 {
569 	if (asus->inputdev)
570 		input_unregister_device(asus->inputdev);
571 
572 	asus->inputdev = NULL;
573 }
574 
575 /* Tablet mode ****************************************************************/
576 
577 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
578 {
579 	int result;
580 
581 	if (!asus->tablet_switch_dev_id)
582 		return;
583 
584 	result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
585 	if (result >= 0)
586 		asus_wmi_tablet_sw_report(asus, result);
587 }
588 
589 /* dGPU ********************************************************************/
590 static ssize_t dgpu_disable_show(struct device *dev,
591 				   struct device_attribute *attr, char *buf)
592 {
593 	struct asus_wmi *asus = dev_get_drvdata(dev);
594 	int result;
595 
596 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
597 	if (result < 0)
598 		return result;
599 
600 	return sysfs_emit(buf, "%d\n", result);
601 }
602 
603 /*
604  * A user may be required to store the value twice, typcial store first, then
605  * rescan PCI bus to activate power, then store a second time to save correctly.
606  * The reason for this is that an extra code path in the ACPI is enabled when
607  * the device and bus are powered.
608  */
609 static ssize_t dgpu_disable_store(struct device *dev,
610 				    struct device_attribute *attr,
611 				    const char *buf, size_t count)
612 {
613 	int result, err;
614 	u32 disable;
615 
616 	struct asus_wmi *asus = dev_get_drvdata(dev);
617 
618 	result = kstrtou32(buf, 10, &disable);
619 	if (result)
620 		return result;
621 
622 	if (disable > 1)
623 		return -EINVAL;
624 
625 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
626 	if (err) {
627 		pr_warn("Failed to set dgpu disable: %d\n", err);
628 		return err;
629 	}
630 
631 	if (result > 1) {
632 		pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
633 		return -EIO;
634 	}
635 
636 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
637 
638 	return count;
639 }
640 static DEVICE_ATTR_RW(dgpu_disable);
641 
642 /* eGPU ********************************************************************/
643 static ssize_t egpu_enable_show(struct device *dev,
644 				   struct device_attribute *attr, char *buf)
645 {
646 	struct asus_wmi *asus = dev_get_drvdata(dev);
647 	int result;
648 
649 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
650 	if (result < 0)
651 		return result;
652 
653 	return sysfs_emit(buf, "%d\n", result);
654 }
655 
656 /* The ACPI call to enable the eGPU also disables the internal dGPU */
657 static ssize_t egpu_enable_store(struct device *dev,
658 				    struct device_attribute *attr,
659 				    const char *buf, size_t count)
660 {
661 	int result, err;
662 	u32 enable;
663 
664 	struct asus_wmi *asus = dev_get_drvdata(dev);
665 
666 	err = kstrtou32(buf, 10, &enable);
667 	if (err)
668 		return err;
669 
670 	if (enable > 1)
671 		return -EINVAL;
672 
673 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
674 	if (err) {
675 		pr_warn("Failed to set egpu disable: %d\n", err);
676 		return err;
677 	}
678 
679 	if (result > 1) {
680 		pr_warn("Failed to set egpu disable (retval): 0x%x\n", result);
681 		return -EIO;
682 	}
683 
684 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
685 
686 	return count;
687 }
688 static DEVICE_ATTR_RW(egpu_enable);
689 
690 /* gpu mux switch *************************************************************/
691 static ssize_t gpu_mux_mode_show(struct device *dev,
692 				 struct device_attribute *attr, char *buf)
693 {
694 	struct asus_wmi *asus = dev_get_drvdata(dev);
695 	int result;
696 
697 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
698 	if (result < 0)
699 		return result;
700 
701 	return sysfs_emit(buf, "%d\n", result);
702 }
703 
704 static ssize_t gpu_mux_mode_store(struct device *dev,
705 				  struct device_attribute *attr,
706 				  const char *buf, size_t count)
707 {
708 	struct asus_wmi *asus = dev_get_drvdata(dev);
709 	int result, err;
710 	u32 optimus;
711 
712 	err = kstrtou32(buf, 10, &optimus);
713 	if (err)
714 		return err;
715 
716 	if (optimus > 1)
717 		return -EINVAL;
718 
719 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_MUX, optimus, &result);
720 	if (err) {
721 		dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
722 		return err;
723 	}
724 	/* !1 is considered a fail by ASUS */
725 	if (result != 1) {
726 		dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
727 		return -EIO;
728 	}
729 
730 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
731 
732 	return count;
733 }
734 static DEVICE_ATTR_RW(gpu_mux_mode);
735 
736 /* TUF Laptop Keyboard RGB Modes **********************************************/
737 static ssize_t kbd_rgb_mode_store(struct device *dev,
738 				 struct device_attribute *attr,
739 				 const char *buf, size_t count)
740 {
741 	u32 cmd, mode, r, g, b, speed;
742 	int err;
743 
744 	if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
745 		return -EINVAL;
746 
747 	/* B3 is set and B4 is save to BIOS */
748 	switch (cmd) {
749 	case 0:
750 		cmd = 0xb3;
751 		break;
752 	case 1:
753 		cmd = 0xb4;
754 		break;
755 	default:
756 		return -EINVAL;
757 	}
758 
759 	/* These are the known usable modes across all TUF/ROG */
760 	if (mode >= 12 || mode == 9)
761 		mode = 10;
762 
763 	switch (speed) {
764 	case 0:
765 		speed = 0xe1;
766 		break;
767 	case 1:
768 		speed = 0xeb;
769 		break;
770 	case 2:
771 		speed = 0xf5;
772 		break;
773 	default:
774 		speed = 0xeb;
775 	}
776 
777 	err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, ASUS_WMI_DEVID_TUF_RGB_MODE,
778 			cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
779 	if (err)
780 		return err;
781 
782 	return count;
783 }
784 static DEVICE_ATTR_WO(kbd_rgb_mode);
785 
786 static ssize_t kbd_rgb_mode_index_show(struct device *device,
787 						 struct device_attribute *attr,
788 						 char *buf)
789 {
790 	return sysfs_emit(buf, "%s\n", "cmd mode red green blue speed");
791 }
792 static DEVICE_ATTR_RO(kbd_rgb_mode_index);
793 
794 static struct attribute *kbd_rgb_mode_attrs[] = {
795 	&dev_attr_kbd_rgb_mode.attr,
796 	&dev_attr_kbd_rgb_mode_index.attr,
797 	NULL,
798 };
799 
800 static const struct attribute_group kbd_rgb_mode_group = {
801 	.attrs = kbd_rgb_mode_attrs,
802 };
803 
804 /* TUF Laptop Keyboard RGB State **********************************************/
805 static ssize_t kbd_rgb_state_store(struct device *dev,
806 				 struct device_attribute *attr,
807 				 const char *buf, size_t count)
808 {
809 	u32 flags, cmd, boot, awake, sleep, keyboard;
810 	int err;
811 
812 	if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
813 		return -EINVAL;
814 
815 	if (cmd)
816 		cmd = BIT(2);
817 
818 	flags = 0;
819 	if (boot)
820 		flags |= BIT(1);
821 	if (awake)
822 		flags |= BIT(3);
823 	if (sleep)
824 		flags |= BIT(5);
825 	if (keyboard)
826 		flags |= BIT(7);
827 
828 	/* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
829 	err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
830 			ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
831 	if (err)
832 		return err;
833 
834 	return count;
835 }
836 static DEVICE_ATTR_WO(kbd_rgb_state);
837 
838 static ssize_t kbd_rgb_state_index_show(struct device *device,
839 						 struct device_attribute *attr,
840 						 char *buf)
841 {
842 	return sysfs_emit(buf, "%s\n", "cmd boot awake sleep keyboard");
843 }
844 static DEVICE_ATTR_RO(kbd_rgb_state_index);
845 
846 static struct attribute *kbd_rgb_state_attrs[] = {
847 	&dev_attr_kbd_rgb_state.attr,
848 	&dev_attr_kbd_rgb_state_index.attr,
849 	NULL,
850 };
851 
852 static const struct attribute_group kbd_rgb_state_group = {
853 	.attrs = kbd_rgb_state_attrs,
854 };
855 
856 static const struct attribute_group *kbd_rgb_mode_groups[] = {
857 	NULL,
858 	NULL,
859 	NULL,
860 };
861 
862 /* Battery ********************************************************************/
863 
864 /* The battery maximum charging percentage */
865 static int charge_end_threshold;
866 
867 static ssize_t charge_control_end_threshold_store(struct device *dev,
868 						  struct device_attribute *attr,
869 						  const char *buf, size_t count)
870 {
871 	int value, ret, rv;
872 
873 	ret = kstrtouint(buf, 10, &value);
874 	if (ret)
875 		return ret;
876 
877 	if (value < 0 || value > 100)
878 		return -EINVAL;
879 
880 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
881 	if (ret)
882 		return ret;
883 
884 	if (rv != 1)
885 		return -EIO;
886 
887 	/* There isn't any method in the DSDT to read the threshold, so we
888 	 * save the threshold.
889 	 */
890 	charge_end_threshold = value;
891 	return count;
892 }
893 
894 static ssize_t charge_control_end_threshold_show(struct device *device,
895 						 struct device_attribute *attr,
896 						 char *buf)
897 {
898 	return sysfs_emit(buf, "%d\n", charge_end_threshold);
899 }
900 
901 static DEVICE_ATTR_RW(charge_control_end_threshold);
902 
903 static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
904 {
905 	/* The WMI method does not provide a way to specific a battery, so we
906 	 * just assume it is the first battery.
907 	 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
908 	 * battery is named BATT.
909 	 */
910 	if (strcmp(battery->desc->name, "BAT0") != 0 &&
911 	    strcmp(battery->desc->name, "BAT1") != 0 &&
912 	    strcmp(battery->desc->name, "BATC") != 0 &&
913 	    strcmp(battery->desc->name, "BATT") != 0)
914 		return -ENODEV;
915 
916 	if (device_create_file(&battery->dev,
917 	    &dev_attr_charge_control_end_threshold))
918 		return -ENODEV;
919 
920 	/* The charge threshold is only reset when the system is power cycled,
921 	 * and we can't get the current threshold so let set it to 100% when
922 	 * a battery is added.
923 	 */
924 	asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
925 	charge_end_threshold = 100;
926 
927 	return 0;
928 }
929 
930 static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
931 {
932 	device_remove_file(&battery->dev,
933 			   &dev_attr_charge_control_end_threshold);
934 	return 0;
935 }
936 
937 static struct acpi_battery_hook battery_hook = {
938 	.add_battery = asus_wmi_battery_add,
939 	.remove_battery = asus_wmi_battery_remove,
940 	.name = "ASUS Battery Extension",
941 };
942 
943 static void asus_wmi_battery_init(struct asus_wmi *asus)
944 {
945 	asus->battery_rsoc_available = false;
946 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
947 		asus->battery_rsoc_available = true;
948 		battery_hook_register(&battery_hook);
949 	}
950 }
951 
952 static void asus_wmi_battery_exit(struct asus_wmi *asus)
953 {
954 	if (asus->battery_rsoc_available)
955 		battery_hook_unregister(&battery_hook);
956 }
957 
958 /* LEDs ***********************************************************************/
959 
960 /*
961  * These functions actually update the LED's, and are called from a
962  * workqueue. By doing this as separate work rather than when the LED
963  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
964  * potentially bad time, such as a timer interrupt.
965  */
966 static void tpd_led_update(struct work_struct *work)
967 {
968 	int ctrl_param;
969 	struct asus_wmi *asus;
970 
971 	asus = container_of(work, struct asus_wmi, tpd_led_work);
972 
973 	ctrl_param = asus->tpd_led_wk;
974 	asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
975 }
976 
977 static void tpd_led_set(struct led_classdev *led_cdev,
978 			enum led_brightness value)
979 {
980 	struct asus_wmi *asus;
981 
982 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
983 
984 	asus->tpd_led_wk = !!value;
985 	queue_work(asus->led_workqueue, &asus->tpd_led_work);
986 }
987 
988 static int read_tpd_led_state(struct asus_wmi *asus)
989 {
990 	return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
991 }
992 
993 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
994 {
995 	struct asus_wmi *asus;
996 
997 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
998 
999 	return read_tpd_led_state(asus);
1000 }
1001 
1002 static void kbd_led_update(struct asus_wmi *asus)
1003 {
1004 	int ctrl_param = 0;
1005 
1006 	ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
1007 	asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
1008 }
1009 
1010 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
1011 {
1012 	int retval;
1013 
1014 	/*
1015 	 * bits 0-2: level
1016 	 * bit 7: light on/off
1017 	 * bit 8-10: environment (0: dark, 1: normal, 2: light)
1018 	 * bit 17: status unknown
1019 	 */
1020 	retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
1021 					    0xFFFF);
1022 
1023 	/* Unknown status is considered as off */
1024 	if (retval == 0x8000)
1025 		retval = 0;
1026 
1027 	if (retval < 0)
1028 		return retval;
1029 
1030 	if (level)
1031 		*level = retval & 0x7F;
1032 	if (env)
1033 		*env = (retval >> 8) & 0x7F;
1034 	return 0;
1035 }
1036 
1037 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
1038 {
1039 	struct asus_wmi *asus;
1040 	int max_level;
1041 
1042 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1043 	max_level = asus->kbd_led.max_brightness;
1044 
1045 	asus->kbd_led_wk = clamp_val(value, 0, max_level);
1046 	kbd_led_update(asus);
1047 }
1048 
1049 static void kbd_led_set(struct led_classdev *led_cdev,
1050 			enum led_brightness value)
1051 {
1052 	/* Prevent disabling keyboard backlight on module unregister */
1053 	if (led_cdev->flags & LED_UNREGISTERING)
1054 		return;
1055 
1056 	do_kbd_led_set(led_cdev, value);
1057 }
1058 
1059 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
1060 {
1061 	struct led_classdev *led_cdev = &asus->kbd_led;
1062 
1063 	do_kbd_led_set(led_cdev, value);
1064 	led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
1065 }
1066 
1067 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1068 {
1069 	struct asus_wmi *asus;
1070 	int retval, value;
1071 
1072 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1073 
1074 	retval = kbd_led_read(asus, &value, NULL);
1075 	if (retval < 0)
1076 		return retval;
1077 
1078 	return value;
1079 }
1080 
1081 static int wlan_led_unknown_state(struct asus_wmi *asus)
1082 {
1083 	u32 result;
1084 
1085 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1086 
1087 	return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
1088 }
1089 
1090 static void wlan_led_update(struct work_struct *work)
1091 {
1092 	int ctrl_param;
1093 	struct asus_wmi *asus;
1094 
1095 	asus = container_of(work, struct asus_wmi, wlan_led_work);
1096 
1097 	ctrl_param = asus->wlan_led_wk;
1098 	asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
1099 }
1100 
1101 static void wlan_led_set(struct led_classdev *led_cdev,
1102 			 enum led_brightness value)
1103 {
1104 	struct asus_wmi *asus;
1105 
1106 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1107 
1108 	asus->wlan_led_wk = !!value;
1109 	queue_work(asus->led_workqueue, &asus->wlan_led_work);
1110 }
1111 
1112 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
1113 {
1114 	struct asus_wmi *asus;
1115 	u32 result;
1116 
1117 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1118 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1119 
1120 	return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1121 }
1122 
1123 static void lightbar_led_update(struct work_struct *work)
1124 {
1125 	struct asus_wmi *asus;
1126 	int ctrl_param;
1127 
1128 	asus = container_of(work, struct asus_wmi, lightbar_led_work);
1129 
1130 	ctrl_param = asus->lightbar_led_wk;
1131 	asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1132 }
1133 
1134 static void lightbar_led_set(struct led_classdev *led_cdev,
1135 			     enum led_brightness value)
1136 {
1137 	struct asus_wmi *asus;
1138 
1139 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1140 
1141 	asus->lightbar_led_wk = !!value;
1142 	queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1143 }
1144 
1145 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1146 {
1147 	struct asus_wmi *asus;
1148 	u32 result;
1149 
1150 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1151 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1152 
1153 	return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1154 }
1155 
1156 static int micmute_led_set(struct led_classdev *led_cdev,
1157 			   enum led_brightness brightness)
1158 {
1159 	int state = brightness != LED_OFF;
1160 	int err;
1161 
1162 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1163 	return err < 0 ? err : 0;
1164 }
1165 
1166 static void asus_wmi_led_exit(struct asus_wmi *asus)
1167 {
1168 	led_classdev_unregister(&asus->kbd_led);
1169 	led_classdev_unregister(&asus->tpd_led);
1170 	led_classdev_unregister(&asus->wlan_led);
1171 	led_classdev_unregister(&asus->lightbar_led);
1172 	led_classdev_unregister(&asus->micmute_led);
1173 
1174 	if (asus->led_workqueue)
1175 		destroy_workqueue(asus->led_workqueue);
1176 }
1177 
1178 static int asus_wmi_led_init(struct asus_wmi *asus)
1179 {
1180 	int rv = 0, num_rgb_groups = 0, led_val;
1181 
1182 	if (asus->kbd_rgb_mode_available)
1183 		kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
1184 	if (asus->kbd_rgb_state_available)
1185 		kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
1186 
1187 	asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1188 	if (!asus->led_workqueue)
1189 		return -ENOMEM;
1190 
1191 	if (read_tpd_led_state(asus) >= 0) {
1192 		INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1193 
1194 		asus->tpd_led.name = "asus::touchpad";
1195 		asus->tpd_led.brightness_set = tpd_led_set;
1196 		asus->tpd_led.brightness_get = tpd_led_get;
1197 		asus->tpd_led.max_brightness = 1;
1198 
1199 		rv = led_classdev_register(&asus->platform_device->dev,
1200 					   &asus->tpd_led);
1201 		if (rv)
1202 			goto error;
1203 	}
1204 
1205 	if (!kbd_led_read(asus, &led_val, NULL)) {
1206 		asus->kbd_led_wk = led_val;
1207 		asus->kbd_led.name = "asus::kbd_backlight";
1208 		asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1209 		asus->kbd_led.brightness_set = kbd_led_set;
1210 		asus->kbd_led.brightness_get = kbd_led_get;
1211 		asus->kbd_led.max_brightness = 3;
1212 
1213 		if (num_rgb_groups != 0)
1214 			asus->kbd_led.groups = kbd_rgb_mode_groups;
1215 
1216 		rv = led_classdev_register(&asus->platform_device->dev,
1217 					   &asus->kbd_led);
1218 		if (rv)
1219 			goto error;
1220 	}
1221 
1222 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1223 			&& (asus->driver->quirks->wapf > 0)) {
1224 		INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1225 
1226 		asus->wlan_led.name = "asus::wlan";
1227 		asus->wlan_led.brightness_set = wlan_led_set;
1228 		if (!wlan_led_unknown_state(asus))
1229 			asus->wlan_led.brightness_get = wlan_led_get;
1230 		asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1231 		asus->wlan_led.max_brightness = 1;
1232 		asus->wlan_led.default_trigger = "asus-wlan";
1233 
1234 		rv = led_classdev_register(&asus->platform_device->dev,
1235 					   &asus->wlan_led);
1236 		if (rv)
1237 			goto error;
1238 	}
1239 
1240 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1241 		INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1242 
1243 		asus->lightbar_led.name = "asus::lightbar";
1244 		asus->lightbar_led.brightness_set = lightbar_led_set;
1245 		asus->lightbar_led.brightness_get = lightbar_led_get;
1246 		asus->lightbar_led.max_brightness = 1;
1247 
1248 		rv = led_classdev_register(&asus->platform_device->dev,
1249 					   &asus->lightbar_led);
1250 	}
1251 
1252 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1253 		asus->micmute_led.name = "platform::micmute";
1254 		asus->micmute_led.max_brightness = 1;
1255 		asus->micmute_led.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
1256 		asus->micmute_led.brightness_set_blocking = micmute_led_set;
1257 		asus->micmute_led.default_trigger = "audio-micmute";
1258 
1259 		rv = led_classdev_register(&asus->platform_device->dev,
1260 						&asus->micmute_led);
1261 		if (rv)
1262 			goto error;
1263 	}
1264 
1265 error:
1266 	if (rv)
1267 		asus_wmi_led_exit(asus);
1268 
1269 	return rv;
1270 }
1271 
1272 /* RF *************************************************************************/
1273 
1274 /*
1275  * PCI hotplug (for wlan rfkill)
1276  */
1277 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1278 {
1279 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1280 
1281 	if (result < 0)
1282 		return false;
1283 	return !result;
1284 }
1285 
1286 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1287 {
1288 	struct pci_dev *dev;
1289 	struct pci_bus *bus;
1290 	bool blocked;
1291 	bool absent;
1292 	u32 l;
1293 
1294 	mutex_lock(&asus->wmi_lock);
1295 	blocked = asus_wlan_rfkill_blocked(asus);
1296 	mutex_unlock(&asus->wmi_lock);
1297 
1298 	mutex_lock(&asus->hotplug_lock);
1299 	pci_lock_rescan_remove();
1300 
1301 	if (asus->wlan.rfkill)
1302 		rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1303 
1304 	if (asus->hotplug_slot.ops) {
1305 		bus = pci_find_bus(0, 1);
1306 		if (!bus) {
1307 			pr_warn("Unable to find PCI bus 1?\n");
1308 			goto out_unlock;
1309 		}
1310 
1311 		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1312 			pr_err("Unable to read PCI config space?\n");
1313 			goto out_unlock;
1314 		}
1315 		absent = (l == 0xffffffff);
1316 
1317 		if (blocked != absent) {
1318 			pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1319 				blocked ? "blocked" : "unblocked",
1320 				absent ? "absent" : "present");
1321 			pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1322 			goto out_unlock;
1323 		}
1324 
1325 		if (!blocked) {
1326 			dev = pci_get_slot(bus, 0);
1327 			if (dev) {
1328 				/* Device already present */
1329 				pci_dev_put(dev);
1330 				goto out_unlock;
1331 			}
1332 			dev = pci_scan_single_device(bus, 0);
1333 			if (dev) {
1334 				pci_bus_assign_resources(bus);
1335 				pci_bus_add_device(dev);
1336 			}
1337 		} else {
1338 			dev = pci_get_slot(bus, 0);
1339 			if (dev) {
1340 				pci_stop_and_remove_bus_device(dev);
1341 				pci_dev_put(dev);
1342 			}
1343 		}
1344 	}
1345 
1346 out_unlock:
1347 	pci_unlock_rescan_remove();
1348 	mutex_unlock(&asus->hotplug_lock);
1349 }
1350 
1351 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1352 {
1353 	struct asus_wmi *asus = data;
1354 
1355 	if (event != ACPI_NOTIFY_BUS_CHECK)
1356 		return;
1357 
1358 	/*
1359 	 * We can't call directly asus_rfkill_hotplug because most
1360 	 * of the time WMBC is still being executed and not reetrant.
1361 	 * There is currently no way to tell ACPICA that  we want this
1362 	 * method to be serialized, we schedule a asus_rfkill_hotplug
1363 	 * call later, in a safer context.
1364 	 */
1365 	queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1366 }
1367 
1368 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1369 {
1370 	acpi_status status;
1371 	acpi_handle handle;
1372 
1373 	status = acpi_get_handle(NULL, node, &handle);
1374 	if (ACPI_FAILURE(status))
1375 		return -ENODEV;
1376 
1377 	status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1378 					     asus_rfkill_notify, asus);
1379 	if (ACPI_FAILURE(status))
1380 		pr_warn("Failed to register notify on %s\n", node);
1381 
1382 	return 0;
1383 }
1384 
1385 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1386 {
1387 	acpi_status status = AE_OK;
1388 	acpi_handle handle;
1389 
1390 	status = acpi_get_handle(NULL, node, &handle);
1391 	if (ACPI_FAILURE(status))
1392 		return;
1393 
1394 	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1395 					    asus_rfkill_notify);
1396 	if (ACPI_FAILURE(status))
1397 		pr_err("Error removing rfkill notify handler %s\n", node);
1398 }
1399 
1400 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1401 				   u8 *value)
1402 {
1403 	struct asus_wmi *asus = container_of(hotplug_slot,
1404 					     struct asus_wmi, hotplug_slot);
1405 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1406 
1407 	if (result < 0)
1408 		return result;
1409 
1410 	*value = !!result;
1411 	return 0;
1412 }
1413 
1414 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1415 	.get_adapter_status = asus_get_adapter_status,
1416 	.get_power_status = asus_get_adapter_status,
1417 };
1418 
1419 static void asus_hotplug_work(struct work_struct *work)
1420 {
1421 	struct asus_wmi *asus;
1422 
1423 	asus = container_of(work, struct asus_wmi, hotplug_work);
1424 	asus_rfkill_hotplug(asus);
1425 }
1426 
1427 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1428 {
1429 	int ret = -ENOMEM;
1430 	struct pci_bus *bus = pci_find_bus(0, 1);
1431 
1432 	if (!bus) {
1433 		pr_err("Unable to find wifi PCI bus\n");
1434 		return -ENODEV;
1435 	}
1436 
1437 	asus->hotplug_workqueue =
1438 	    create_singlethread_workqueue("hotplug_workqueue");
1439 	if (!asus->hotplug_workqueue)
1440 		goto error_workqueue;
1441 
1442 	INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1443 
1444 	asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1445 
1446 	ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1447 	if (ret) {
1448 		pr_err("Unable to register hotplug slot - %d\n", ret);
1449 		goto error_register;
1450 	}
1451 
1452 	return 0;
1453 
1454 error_register:
1455 	asus->hotplug_slot.ops = NULL;
1456 	destroy_workqueue(asus->hotplug_workqueue);
1457 error_workqueue:
1458 	return ret;
1459 }
1460 
1461 /*
1462  * Rfkill devices
1463  */
1464 static int asus_rfkill_set(void *data, bool blocked)
1465 {
1466 	struct asus_rfkill *priv = data;
1467 	u32 ctrl_param = !blocked;
1468 	u32 dev_id = priv->dev_id;
1469 
1470 	/*
1471 	 * If the user bit is set, BIOS can't set and record the wlan status,
1472 	 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1473 	 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1474 	 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1475 	 * while setting the wlan status through WMI.
1476 	 * This is also the behavior that windows app will do.
1477 	 */
1478 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1479 	     priv->asus->driver->wlan_ctrl_by_user)
1480 		dev_id = ASUS_WMI_DEVID_WLAN_LED;
1481 
1482 	return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1483 }
1484 
1485 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1486 {
1487 	struct asus_rfkill *priv = data;
1488 	int result;
1489 
1490 	result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1491 
1492 	if (result < 0)
1493 		return;
1494 
1495 	rfkill_set_sw_state(priv->rfkill, !result);
1496 }
1497 
1498 static int asus_rfkill_wlan_set(void *data, bool blocked)
1499 {
1500 	struct asus_rfkill *priv = data;
1501 	struct asus_wmi *asus = priv->asus;
1502 	int ret;
1503 
1504 	/*
1505 	 * This handler is enabled only if hotplug is enabled.
1506 	 * In this case, the asus_wmi_set_devstate() will
1507 	 * trigger a wmi notification and we need to wait
1508 	 * this call to finish before being able to call
1509 	 * any wmi method
1510 	 */
1511 	mutex_lock(&asus->wmi_lock);
1512 	ret = asus_rfkill_set(data, blocked);
1513 	mutex_unlock(&asus->wmi_lock);
1514 	return ret;
1515 }
1516 
1517 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1518 	.set_block = asus_rfkill_wlan_set,
1519 	.query = asus_rfkill_query,
1520 };
1521 
1522 static const struct rfkill_ops asus_rfkill_ops = {
1523 	.set_block = asus_rfkill_set,
1524 	.query = asus_rfkill_query,
1525 };
1526 
1527 static int asus_new_rfkill(struct asus_wmi *asus,
1528 			   struct asus_rfkill *arfkill,
1529 			   const char *name, enum rfkill_type type, int dev_id)
1530 {
1531 	int result = asus_wmi_get_devstate_simple(asus, dev_id);
1532 	struct rfkill **rfkill = &arfkill->rfkill;
1533 
1534 	if (result < 0)
1535 		return result;
1536 
1537 	arfkill->dev_id = dev_id;
1538 	arfkill->asus = asus;
1539 
1540 	if (dev_id == ASUS_WMI_DEVID_WLAN &&
1541 	    asus->driver->quirks->hotplug_wireless)
1542 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1543 				       &asus_rfkill_wlan_ops, arfkill);
1544 	else
1545 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1546 				       &asus_rfkill_ops, arfkill);
1547 
1548 	if (!*rfkill)
1549 		return -EINVAL;
1550 
1551 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1552 			(asus->driver->quirks->wapf > 0))
1553 		rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1554 
1555 	rfkill_init_sw_state(*rfkill, !result);
1556 	result = rfkill_register(*rfkill);
1557 	if (result) {
1558 		rfkill_destroy(*rfkill);
1559 		*rfkill = NULL;
1560 		return result;
1561 	}
1562 	return 0;
1563 }
1564 
1565 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1566 {
1567 	if (asus->driver->wlan_ctrl_by_user && ashs_present())
1568 		return;
1569 
1570 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1571 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1572 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1573 	if (asus->wlan.rfkill) {
1574 		rfkill_unregister(asus->wlan.rfkill);
1575 		rfkill_destroy(asus->wlan.rfkill);
1576 		asus->wlan.rfkill = NULL;
1577 	}
1578 	/*
1579 	 * Refresh pci hotplug in case the rfkill state was changed after
1580 	 * asus_unregister_rfkill_notifier()
1581 	 */
1582 	asus_rfkill_hotplug(asus);
1583 	if (asus->hotplug_slot.ops)
1584 		pci_hp_deregister(&asus->hotplug_slot);
1585 	if (asus->hotplug_workqueue)
1586 		destroy_workqueue(asus->hotplug_workqueue);
1587 
1588 	if (asus->bluetooth.rfkill) {
1589 		rfkill_unregister(asus->bluetooth.rfkill);
1590 		rfkill_destroy(asus->bluetooth.rfkill);
1591 		asus->bluetooth.rfkill = NULL;
1592 	}
1593 	if (asus->wimax.rfkill) {
1594 		rfkill_unregister(asus->wimax.rfkill);
1595 		rfkill_destroy(asus->wimax.rfkill);
1596 		asus->wimax.rfkill = NULL;
1597 	}
1598 	if (asus->wwan3g.rfkill) {
1599 		rfkill_unregister(asus->wwan3g.rfkill);
1600 		rfkill_destroy(asus->wwan3g.rfkill);
1601 		asus->wwan3g.rfkill = NULL;
1602 	}
1603 	if (asus->gps.rfkill) {
1604 		rfkill_unregister(asus->gps.rfkill);
1605 		rfkill_destroy(asus->gps.rfkill);
1606 		asus->gps.rfkill = NULL;
1607 	}
1608 	if (asus->uwb.rfkill) {
1609 		rfkill_unregister(asus->uwb.rfkill);
1610 		rfkill_destroy(asus->uwb.rfkill);
1611 		asus->uwb.rfkill = NULL;
1612 	}
1613 }
1614 
1615 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1616 {
1617 	int result = 0;
1618 
1619 	mutex_init(&asus->hotplug_lock);
1620 	mutex_init(&asus->wmi_lock);
1621 
1622 	result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1623 				 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1624 
1625 	if (result && result != -ENODEV)
1626 		goto exit;
1627 
1628 	result = asus_new_rfkill(asus, &asus->bluetooth,
1629 				 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1630 				 ASUS_WMI_DEVID_BLUETOOTH);
1631 
1632 	if (result && result != -ENODEV)
1633 		goto exit;
1634 
1635 	result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1636 				 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1637 
1638 	if (result && result != -ENODEV)
1639 		goto exit;
1640 
1641 	result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1642 				 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1643 
1644 	if (result && result != -ENODEV)
1645 		goto exit;
1646 
1647 	result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1648 				 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1649 
1650 	if (result && result != -ENODEV)
1651 		goto exit;
1652 
1653 	result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1654 				 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1655 
1656 	if (result && result != -ENODEV)
1657 		goto exit;
1658 
1659 	if (!asus->driver->quirks->hotplug_wireless)
1660 		goto exit;
1661 
1662 	result = asus_setup_pci_hotplug(asus);
1663 	/*
1664 	 * If we get -EBUSY then something else is handling the PCI hotplug -
1665 	 * don't fail in this case
1666 	 */
1667 	if (result == -EBUSY)
1668 		result = 0;
1669 
1670 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1671 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1672 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1673 	/*
1674 	 * Refresh pci hotplug in case the rfkill state was changed during
1675 	 * setup.
1676 	 */
1677 	asus_rfkill_hotplug(asus);
1678 
1679 exit:
1680 	if (result && result != -ENODEV)
1681 		asus_wmi_rfkill_exit(asus);
1682 
1683 	if (result == -ENODEV)
1684 		result = 0;
1685 
1686 	return result;
1687 }
1688 
1689 /* Panel Overdrive ************************************************************/
1690 static ssize_t panel_od_show(struct device *dev,
1691 				   struct device_attribute *attr, char *buf)
1692 {
1693 	struct asus_wmi *asus = dev_get_drvdata(dev);
1694 	int result;
1695 
1696 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
1697 	if (result < 0)
1698 		return result;
1699 
1700 	return sysfs_emit(buf, "%d\n", result);
1701 }
1702 
1703 static ssize_t panel_od_store(struct device *dev,
1704 				    struct device_attribute *attr,
1705 				    const char *buf, size_t count)
1706 {
1707 	int result, err;
1708 	u32 overdrive;
1709 
1710 	struct asus_wmi *asus = dev_get_drvdata(dev);
1711 
1712 	result = kstrtou32(buf, 10, &overdrive);
1713 	if (result)
1714 		return result;
1715 
1716 	if (overdrive > 1)
1717 		return -EINVAL;
1718 
1719 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
1720 
1721 	if (err) {
1722 		pr_warn("Failed to set panel overdrive: %d\n", err);
1723 		return err;
1724 	}
1725 
1726 	if (result > 1) {
1727 		pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
1728 		return -EIO;
1729 	}
1730 
1731 	sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1732 
1733 	return count;
1734 }
1735 static DEVICE_ATTR_RW(panel_od);
1736 
1737 /* Quirks *********************************************************************/
1738 
1739 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1740 {
1741 	struct pci_dev *xhci_pdev;
1742 	u32 orig_ports_available;
1743 	u32 ports_available = asus->driver->quirks->xusb2pr;
1744 
1745 	xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1746 			PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1747 			NULL);
1748 
1749 	if (!xhci_pdev)
1750 		return;
1751 
1752 	pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1753 				&orig_ports_available);
1754 
1755 	pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1756 				cpu_to_le32(ports_available));
1757 
1758 	pci_dev_put(xhci_pdev);
1759 
1760 	pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1761 			orig_ports_available, ports_available);
1762 }
1763 
1764 /*
1765  * Some devices dont support or have borcken get_als method
1766  * but still support set method.
1767  */
1768 static void asus_wmi_set_als(void)
1769 {
1770 	asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1771 }
1772 
1773 /* Hwmon device ***************************************************************/
1774 
1775 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1776 					  int *speed)
1777 {
1778 	struct agfn_fan_args args = {
1779 		.agfn.len = sizeof(args),
1780 		.agfn.mfun = ASUS_FAN_MFUN,
1781 		.agfn.sfun = ASUS_FAN_SFUN_READ,
1782 		.fan = fan,
1783 		.speed = 0,
1784 	};
1785 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1786 	int status;
1787 
1788 	if (fan != 1)
1789 		return -EINVAL;
1790 
1791 	status = asus_wmi_evaluate_method_agfn(input);
1792 
1793 	if (status || args.agfn.err)
1794 		return -ENXIO;
1795 
1796 	if (speed)
1797 		*speed = args.speed;
1798 
1799 	return 0;
1800 }
1801 
1802 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1803 				     int *speed)
1804 {
1805 	struct agfn_fan_args args = {
1806 		.agfn.len = sizeof(args),
1807 		.agfn.mfun = ASUS_FAN_MFUN,
1808 		.agfn.sfun = ASUS_FAN_SFUN_WRITE,
1809 		.fan = fan,
1810 		.speed = speed ?  *speed : 0,
1811 	};
1812 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1813 	int status;
1814 
1815 	/* 1: for setting 1st fan's speed 0: setting auto mode */
1816 	if (fan != 1 && fan != 0)
1817 		return -EINVAL;
1818 
1819 	status = asus_wmi_evaluate_method_agfn(input);
1820 
1821 	if (status || args.agfn.err)
1822 		return -ENXIO;
1823 
1824 	if (speed && fan == 1)
1825 		asus->agfn_pwm = *speed;
1826 
1827 	return 0;
1828 }
1829 
1830 /*
1831  * Check if we can read the speed of one fan. If true we assume we can also
1832  * control it.
1833  */
1834 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1835 {
1836 	int status;
1837 	int speed;
1838 	u32 value;
1839 
1840 	status = asus_agfn_fan_speed_read(asus, 1, &speed);
1841 	if (status != 0)
1842 		return false;
1843 
1844 	status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1845 	if (status != 0)
1846 		return false;
1847 
1848 	/*
1849 	 * We need to find a better way, probably using sfun,
1850 	 * bits or spec ...
1851 	 * Currently we disable it if:
1852 	 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1853 	 * - reverved bits are non-zero
1854 	 * - sfun and presence bit are not set
1855 	 */
1856 	return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1857 		 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1858 }
1859 
1860 static int asus_fan_set_auto(struct asus_wmi *asus)
1861 {
1862 	int status;
1863 	u32 retval;
1864 
1865 	switch (asus->fan_type) {
1866 	case FAN_TYPE_SPEC83:
1867 		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1868 					       0, &retval);
1869 		if (status)
1870 			return status;
1871 
1872 		if (retval != 1)
1873 			return -EIO;
1874 		break;
1875 
1876 	case FAN_TYPE_AGFN:
1877 		status = asus_agfn_fan_speed_write(asus, 0, NULL);
1878 		if (status)
1879 			return -ENXIO;
1880 		break;
1881 
1882 	default:
1883 		return -ENXIO;
1884 	}
1885 
1886 	/*
1887 	 * Modern models like the G713 also have GPU fan control (this is not AGFN)
1888 	 */
1889 	if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
1890 		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
1891 					       0, &retval);
1892 		if (status)
1893 			return status;
1894 
1895 		if (retval != 1)
1896 			return -EIO;
1897 	}
1898 
1899 	return 0;
1900 }
1901 
1902 static ssize_t pwm1_show(struct device *dev,
1903 			       struct device_attribute *attr,
1904 			       char *buf)
1905 {
1906 	struct asus_wmi *asus = dev_get_drvdata(dev);
1907 	int err;
1908 	int value;
1909 
1910 	/* If we already set a value then just return it */
1911 	if (asus->agfn_pwm >= 0)
1912 		return sprintf(buf, "%d\n", asus->agfn_pwm);
1913 
1914 	/*
1915 	 * If we haven't set already set a value through the AGFN interface,
1916 	 * we read a current value through the (now-deprecated) FAN_CTRL device.
1917 	 */
1918 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1919 	if (err < 0)
1920 		return err;
1921 
1922 	value &= 0xFF;
1923 
1924 	if (value == 1) /* Low Speed */
1925 		value = 85;
1926 	else if (value == 2)
1927 		value = 170;
1928 	else if (value == 3)
1929 		value = 255;
1930 	else if (value) {
1931 		pr_err("Unknown fan speed %#x\n", value);
1932 		value = -1;
1933 	}
1934 
1935 	return sysfs_emit(buf, "%d\n", value);
1936 }
1937 
1938 static ssize_t pwm1_store(struct device *dev,
1939 				     struct device_attribute *attr,
1940 				     const char *buf, size_t count) {
1941 	struct asus_wmi *asus = dev_get_drvdata(dev);
1942 	int value;
1943 	int state;
1944 	int ret;
1945 
1946 	ret = kstrtouint(buf, 10, &value);
1947 	if (ret)
1948 		return ret;
1949 
1950 	value = clamp(value, 0, 255);
1951 
1952 	state = asus_agfn_fan_speed_write(asus, 1, &value);
1953 	if (state)
1954 		pr_warn("Setting fan speed failed: %d\n", state);
1955 	else
1956 		asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1957 
1958 	return count;
1959 }
1960 
1961 static ssize_t fan1_input_show(struct device *dev,
1962 					struct device_attribute *attr,
1963 					char *buf)
1964 {
1965 	struct asus_wmi *asus = dev_get_drvdata(dev);
1966 	int value;
1967 	int ret;
1968 
1969 	switch (asus->fan_type) {
1970 	case FAN_TYPE_SPEC83:
1971 		ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1972 					    &value);
1973 		if (ret < 0)
1974 			return ret;
1975 
1976 		value &= 0xffff;
1977 		break;
1978 
1979 	case FAN_TYPE_AGFN:
1980 		/* no speed readable on manual mode */
1981 		if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1982 			return -ENXIO;
1983 
1984 		ret = asus_agfn_fan_speed_read(asus, 1, &value);
1985 		if (ret) {
1986 			pr_warn("reading fan speed failed: %d\n", ret);
1987 			return -ENXIO;
1988 		}
1989 		break;
1990 
1991 	default:
1992 		return -ENXIO;
1993 	}
1994 
1995 	return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
1996 }
1997 
1998 static ssize_t pwm1_enable_show(struct device *dev,
1999 						 struct device_attribute *attr,
2000 						 char *buf)
2001 {
2002 	struct asus_wmi *asus = dev_get_drvdata(dev);
2003 
2004 	/*
2005 	 * Just read back the cached pwm mode.
2006 	 *
2007 	 * For the CPU_FAN device, the spec indicates that we should be
2008 	 * able to read the device status and consult bit 19 to see if we
2009 	 * are in Full On or Automatic mode. However, this does not work
2010 	 * in practice on X532FL at least (the bit is always 0) and there's
2011 	 * also nothing in the DSDT to indicate that this behaviour exists.
2012 	 */
2013 	return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
2014 }
2015 
2016 static ssize_t pwm1_enable_store(struct device *dev,
2017 						  struct device_attribute *attr,
2018 						  const char *buf, size_t count)
2019 {
2020 	struct asus_wmi *asus = dev_get_drvdata(dev);
2021 	int status = 0;
2022 	int state;
2023 	int value;
2024 	int ret;
2025 	u32 retval;
2026 
2027 	ret = kstrtouint(buf, 10, &state);
2028 	if (ret)
2029 		return ret;
2030 
2031 	if (asus->fan_type == FAN_TYPE_SPEC83) {
2032 		switch (state) { /* standard documented hwmon values */
2033 		case ASUS_FAN_CTRL_FULLSPEED:
2034 			value = 1;
2035 			break;
2036 		case ASUS_FAN_CTRL_AUTO:
2037 			value = 0;
2038 			break;
2039 		default:
2040 			return -EINVAL;
2041 		}
2042 
2043 		ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2044 					    value, &retval);
2045 		if (ret)
2046 			return ret;
2047 
2048 		if (retval != 1)
2049 			return -EIO;
2050 	} else if (asus->fan_type == FAN_TYPE_AGFN) {
2051 		switch (state) {
2052 		case ASUS_FAN_CTRL_MANUAL:
2053 			break;
2054 
2055 		case ASUS_FAN_CTRL_AUTO:
2056 			status = asus_fan_set_auto(asus);
2057 			if (status)
2058 				return status;
2059 			break;
2060 
2061 		default:
2062 			return -EINVAL;
2063 		}
2064 	}
2065 
2066 	asus->fan_pwm_mode = state;
2067 
2068 	/* Must set to disabled if mode is toggled */
2069 	if (asus->cpu_fan_curve_available)
2070 		asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2071 	if (asus->gpu_fan_curve_available)
2072 		asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2073 
2074 	return count;
2075 }
2076 
2077 static ssize_t fan1_label_show(struct device *dev,
2078 					  struct device_attribute *attr,
2079 					  char *buf)
2080 {
2081 	return sysfs_emit(buf, "%s\n", ASUS_FAN_DESC);
2082 }
2083 
2084 static ssize_t asus_hwmon_temp1(struct device *dev,
2085 				struct device_attribute *attr,
2086 				char *buf)
2087 {
2088 	struct asus_wmi *asus = dev_get_drvdata(dev);
2089 	u32 value;
2090 	int err;
2091 
2092 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
2093 	if (err < 0)
2094 		return err;
2095 
2096 	return sprintf(buf, "%ld\n",
2097 		       deci_kelvin_to_millicelsius(value & 0xFFFF));
2098 }
2099 
2100 /* GPU fan on modern ROG laptops */
2101 static ssize_t fan2_input_show(struct device *dev,
2102 					struct device_attribute *attr,
2103 					char *buf)
2104 {
2105 	struct asus_wmi *asus = dev_get_drvdata(dev);
2106 	int value;
2107 	int ret;
2108 
2109 	ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
2110 	if (ret < 0)
2111 		return ret;
2112 
2113 	value &= 0xffff;
2114 
2115 	return sysfs_emit(buf, "%d\n", value * 100);
2116 }
2117 
2118 static ssize_t fan2_label_show(struct device *dev,
2119 					  struct device_attribute *attr,
2120 					  char *buf)
2121 {
2122 	return sysfs_emit(buf, "%s\n", ASUS_GPU_FAN_DESC);
2123 }
2124 
2125 static ssize_t pwm2_enable_show(struct device *dev,
2126 				struct device_attribute *attr,
2127 				char *buf)
2128 {
2129 	struct asus_wmi *asus = dev_get_drvdata(dev);
2130 
2131 	return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
2132 }
2133 
2134 static ssize_t pwm2_enable_store(struct device *dev,
2135 				 struct device_attribute *attr,
2136 				 const char *buf, size_t count)
2137 {
2138 	struct asus_wmi *asus = dev_get_drvdata(dev);
2139 	int state;
2140 	int value;
2141 	int ret;
2142 	u32 retval;
2143 
2144 	ret = kstrtouint(buf, 10, &state);
2145 	if (ret)
2146 		return ret;
2147 
2148 	switch (state) { /* standard documented hwmon values */
2149 	case ASUS_FAN_CTRL_FULLSPEED:
2150 		value = 1;
2151 		break;
2152 	case ASUS_FAN_CTRL_AUTO:
2153 		value = 0;
2154 		break;
2155 	default:
2156 		return -EINVAL;
2157 	}
2158 
2159 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2160 				    value, &retval);
2161 	if (ret)
2162 		return ret;
2163 
2164 	if (retval != 1)
2165 		return -EIO;
2166 
2167 	asus->gpu_fan_pwm_mode = state;
2168 	return count;
2169 }
2170 
2171 /* Fan1 */
2172 static DEVICE_ATTR_RW(pwm1);
2173 static DEVICE_ATTR_RW(pwm1_enable);
2174 static DEVICE_ATTR_RO(fan1_input);
2175 static DEVICE_ATTR_RO(fan1_label);
2176 /* Fan2 - GPU fan */
2177 static DEVICE_ATTR_RW(pwm2_enable);
2178 static DEVICE_ATTR_RO(fan2_input);
2179 static DEVICE_ATTR_RO(fan2_label);
2180 
2181 /* Temperature */
2182 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
2183 
2184 static struct attribute *hwmon_attributes[] = {
2185 	&dev_attr_pwm1.attr,
2186 	&dev_attr_pwm1_enable.attr,
2187 	&dev_attr_pwm2_enable.attr,
2188 	&dev_attr_fan1_input.attr,
2189 	&dev_attr_fan1_label.attr,
2190 	&dev_attr_fan2_input.attr,
2191 	&dev_attr_fan2_label.attr,
2192 
2193 	&dev_attr_temp1_input.attr,
2194 	NULL
2195 };
2196 
2197 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2198 					  struct attribute *attr, int idx)
2199 {
2200 	struct device *dev = kobj_to_dev(kobj);
2201 	struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2202 	u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2203 
2204 	if (attr == &dev_attr_pwm1.attr) {
2205 		if (asus->fan_type != FAN_TYPE_AGFN)
2206 			return 0;
2207 	} else if (attr == &dev_attr_fan1_input.attr
2208 	    || attr == &dev_attr_fan1_label.attr
2209 	    || attr == &dev_attr_pwm1_enable.attr) {
2210 		if (asus->fan_type == FAN_TYPE_NONE)
2211 			return 0;
2212 	} else if (attr == &dev_attr_fan2_input.attr
2213 	    || attr == &dev_attr_fan2_label.attr
2214 	    || attr == &dev_attr_pwm2_enable.attr) {
2215 		if (asus->gpu_fan_type == FAN_TYPE_NONE)
2216 			return 0;
2217 	} else if (attr == &dev_attr_temp1_input.attr) {
2218 		int err = asus_wmi_get_devstate(asus,
2219 						ASUS_WMI_DEVID_THERMAL_CTRL,
2220 						&value);
2221 
2222 		if (err < 0)
2223 			return 0; /* can't return negative here */
2224 
2225 		/*
2226 		 * If the temperature value in deci-Kelvin is near the absolute
2227 		 * zero temperature, something is clearly wrong
2228 		 */
2229 		if (value == 0 || value == 1)
2230 			return 0;
2231 	}
2232 
2233 	return attr->mode;
2234 }
2235 
2236 static const struct attribute_group hwmon_attribute_group = {
2237 	.is_visible = asus_hwmon_sysfs_is_visible,
2238 	.attrs = hwmon_attributes
2239 };
2240 __ATTRIBUTE_GROUPS(hwmon_attribute);
2241 
2242 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2243 {
2244 	struct device *dev = &asus->platform_device->dev;
2245 	struct device *hwmon;
2246 
2247 	hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2248 			hwmon_attribute_groups);
2249 
2250 	if (IS_ERR(hwmon)) {
2251 		pr_err("Could not register asus hwmon device\n");
2252 		return PTR_ERR(hwmon);
2253 	}
2254 	return 0;
2255 }
2256 
2257 static int asus_wmi_fan_init(struct asus_wmi *asus)
2258 {
2259 	asus->gpu_fan_type = FAN_TYPE_NONE;
2260 	asus->fan_type = FAN_TYPE_NONE;
2261 	asus->agfn_pwm = -1;
2262 
2263 	if (asus->driver->quirks->wmi_ignore_fan)
2264 		asus->fan_type = FAN_TYPE_NONE;
2265 	else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2266 		asus->fan_type = FAN_TYPE_SPEC83;
2267 	else if (asus_wmi_has_agfn_fan(asus))
2268 		asus->fan_type = FAN_TYPE_AGFN;
2269 
2270 	/*  Modern models like G713 also have GPU fan control */
2271 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
2272 		asus->gpu_fan_type = FAN_TYPE_SPEC83;
2273 
2274 	if (asus->fan_type == FAN_TYPE_NONE)
2275 		return -ENODEV;
2276 
2277 	asus_fan_set_auto(asus);
2278 	asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2279 	return 0;
2280 }
2281 
2282 /* Fan mode *******************************************************************/
2283 
2284 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2285 {
2286 	u32 result;
2287 	int err;
2288 
2289 	asus->fan_boost_mode_available = false;
2290 
2291 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2292 				    &result);
2293 	if (err) {
2294 		if (err == -ENODEV)
2295 			return 0;
2296 		else
2297 			return err;
2298 	}
2299 
2300 	if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2301 			(result & ASUS_FAN_BOOST_MODES_MASK)) {
2302 		asus->fan_boost_mode_available = true;
2303 		asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2304 	}
2305 
2306 	return 0;
2307 }
2308 
2309 static int fan_boost_mode_write(struct asus_wmi *asus)
2310 {
2311 	u32 retval;
2312 	u8 value;
2313 	int err;
2314 
2315 	value = asus->fan_boost_mode;
2316 
2317 	pr_info("Set fan boost mode: %u\n", value);
2318 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2319 				    &retval);
2320 
2321 	sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2322 			"fan_boost_mode");
2323 
2324 	if (err) {
2325 		pr_warn("Failed to set fan boost mode: %d\n", err);
2326 		return err;
2327 	}
2328 
2329 	if (retval != 1) {
2330 		pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2331 			retval);
2332 		return -EIO;
2333 	}
2334 
2335 	return 0;
2336 }
2337 
2338 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2339 {
2340 	u8 mask = asus->fan_boost_mode_mask;
2341 
2342 	if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2343 		if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2344 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2345 		else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2346 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2347 	} else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2348 		if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2349 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2350 		else
2351 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2352 	} else {
2353 		asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2354 	}
2355 
2356 	return fan_boost_mode_write(asus);
2357 }
2358 
2359 static ssize_t fan_boost_mode_show(struct device *dev,
2360 				   struct device_attribute *attr, char *buf)
2361 {
2362 	struct asus_wmi *asus = dev_get_drvdata(dev);
2363 
2364 	return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
2365 }
2366 
2367 static ssize_t fan_boost_mode_store(struct device *dev,
2368 				    struct device_attribute *attr,
2369 				    const char *buf, size_t count)
2370 {
2371 	struct asus_wmi *asus = dev_get_drvdata(dev);
2372 	u8 mask = asus->fan_boost_mode_mask;
2373 	u8 new_mode;
2374 	int result;
2375 
2376 	result = kstrtou8(buf, 10, &new_mode);
2377 	if (result < 0) {
2378 		pr_warn("Trying to store invalid value\n");
2379 		return result;
2380 	}
2381 
2382 	if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2383 		if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2384 			return -EINVAL;
2385 	} else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2386 		if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2387 			return -EINVAL;
2388 	} else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2389 		return -EINVAL;
2390 	}
2391 
2392 	asus->fan_boost_mode = new_mode;
2393 	fan_boost_mode_write(asus);
2394 
2395 	return count;
2396 }
2397 
2398 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2399 static DEVICE_ATTR_RW(fan_boost_mode);
2400 
2401 /* Custom fan curves **********************************************************/
2402 
2403 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2404 {
2405 	int i;
2406 
2407 	for (i = 0; i < FAN_CURVE_POINTS; i++) {
2408 		data->temps[i] = buf[i];
2409 	}
2410 
2411 	for (i = 0; i < FAN_CURVE_POINTS; i++) {
2412 		data->percents[i] =
2413 			255 * buf[i + FAN_CURVE_POINTS] / 100;
2414 	}
2415 }
2416 
2417 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2418 {
2419 	struct fan_curve_data *curves;
2420 	u8 buf[FAN_CURVE_BUF_LEN];
2421 	int fan_idx = 0;
2422 	u8 mode = 0;
2423 	int err;
2424 
2425 	if (asus->throttle_thermal_policy_available)
2426 		mode = asus->throttle_thermal_policy_mode;
2427 	/* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2428 	if (mode == 2)
2429 		mode = 1;
2430 	else if (mode == 1)
2431 		mode = 2;
2432 
2433 	if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2434 		fan_idx = FAN_CURVE_DEV_GPU;
2435 
2436 	curves = &asus->custom_fan_curves[fan_idx];
2437 	err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2438 					   FAN_CURVE_BUF_LEN);
2439 	if (err) {
2440 		pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
2441 		return err;
2442 	}
2443 
2444 	fan_curve_copy_from_buf(curves, buf);
2445 	curves->device_id = fan_dev;
2446 
2447 	return 0;
2448 }
2449 
2450 /* Check if capability exists, and populate defaults */
2451 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2452 				   u32 fan_dev)
2453 {
2454 	int err;
2455 
2456 	*available = false;
2457 
2458 	if (asus->fan_type == FAN_TYPE_NONE)
2459 		return 0;
2460 
2461 	err = fan_curve_get_factory_default(asus, fan_dev);
2462 	if (err) {
2463 		return 0;
2464 	}
2465 
2466 	*available = true;
2467 	return 0;
2468 }
2469 
2470 /* Determine which fan the attribute is for if SENSOR_ATTR */
2471 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2472 					      struct device_attribute *attr)
2473 {
2474 	int index = to_sensor_dev_attr(attr)->index;
2475 
2476 	return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2477 }
2478 
2479 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2480 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2481 					    struct device_attribute *attr)
2482 {
2483 	int nr = to_sensor_dev_attr_2(attr)->nr;
2484 
2485 	return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2486 }
2487 
2488 static ssize_t fan_curve_show(struct device *dev,
2489 			      struct device_attribute *attr, char *buf)
2490 {
2491 	struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2492 	struct asus_wmi *asus = dev_get_drvdata(dev);
2493 	struct fan_curve_data *data;
2494 	int value, index, nr;
2495 
2496 	data = fan_curve_attr_2_select(asus, attr);
2497 	index = dev_attr->index;
2498 	nr = dev_attr->nr;
2499 
2500 	if (nr & FAN_CURVE_PWM_MASK)
2501 		value = data->percents[index];
2502 	else
2503 		value = data->temps[index];
2504 
2505 	return sysfs_emit(buf, "%d\n", value);
2506 }
2507 
2508 /*
2509  * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2510  */
2511 static int fan_curve_write(struct asus_wmi *asus,
2512 			   struct fan_curve_data *data)
2513 {
2514 	u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2515 	u8 *percents = data->percents;
2516 	u8 *temps = data->temps;
2517 	int ret, i, shift = 0;
2518 
2519 	if (!data->enabled)
2520 		return 0;
2521 
2522 	for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2523 		arg1 += (temps[i]) << shift;
2524 		arg2 += (temps[i + 4]) << shift;
2525 		/* Scale to percentage for device */
2526 		arg3 += (100 * percents[i] / 255) << shift;
2527 		arg4 += (100 * percents[i + 4] / 255) << shift;
2528 		shift += 8;
2529 	}
2530 
2531 	return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2532 					 data->device_id,
2533 					 arg1, arg2, arg3, arg4, &ret);
2534 }
2535 
2536 static ssize_t fan_curve_store(struct device *dev,
2537 			       struct device_attribute *attr, const char *buf,
2538 			       size_t count)
2539 {
2540 	struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2541 	struct asus_wmi *asus = dev_get_drvdata(dev);
2542 	struct fan_curve_data *data;
2543 	u8 value;
2544 	int err;
2545 
2546 	int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2547 	int index = dev_attr->index;
2548 
2549 	data = fan_curve_attr_2_select(asus, attr);
2550 
2551 	err = kstrtou8(buf, 10, &value);
2552 	if (err < 0)
2553 		return err;
2554 
2555 	if (pwm) {
2556 		data->percents[index] = value;
2557 	} else {
2558 		data->temps[index] = value;
2559 	}
2560 
2561 	/*
2562 	 * Mark as disabled so the user has to explicitly enable to apply a
2563 	 * changed fan curve. This prevents potential lockups from writing out
2564 	 * many changes as one-write-per-change.
2565 	 */
2566 	data->enabled = false;
2567 
2568 	return count;
2569 }
2570 
2571 static ssize_t fan_curve_enable_show(struct device *dev,
2572 				     struct device_attribute *attr, char *buf)
2573 {
2574 	struct asus_wmi *asus = dev_get_drvdata(dev);
2575 	struct fan_curve_data *data;
2576 	int out = 2;
2577 
2578 	data = fan_curve_attr_select(asus, attr);
2579 
2580 	if (data->enabled)
2581 		out = 1;
2582 
2583 	return sysfs_emit(buf, "%d\n", out);
2584 }
2585 
2586 static ssize_t fan_curve_enable_store(struct device *dev,
2587 				      struct device_attribute *attr,
2588 				      const char *buf, size_t count)
2589 {
2590 	struct asus_wmi *asus = dev_get_drvdata(dev);
2591 	struct fan_curve_data *data;
2592 	int value, err;
2593 
2594 	data = fan_curve_attr_select(asus, attr);
2595 
2596 	err = kstrtoint(buf, 10, &value);
2597 	if (err < 0)
2598 		return err;
2599 
2600 	switch (value) {
2601 	case 1:
2602 		data->enabled = true;
2603 		break;
2604 	case 2:
2605 		data->enabled = false;
2606 		break;
2607 	/*
2608 	 * Auto + reset the fan curve data to defaults. Make it an explicit
2609 	 * option so that users don't accidentally overwrite a set fan curve.
2610 	 */
2611 	case 3:
2612 		err = fan_curve_get_factory_default(asus, data->device_id);
2613 		if (err)
2614 			return err;
2615 		data->enabled = false;
2616 		break;
2617 	default:
2618 		return -EINVAL;
2619 	}
2620 
2621 	if (data->enabled) {
2622 		err = fan_curve_write(asus, data);
2623 		if (err)
2624 			return err;
2625 	} else {
2626 		/*
2627 		 * For machines with throttle this is the only way to reset fans
2628 		 * to default mode of operation (does not erase curve data).
2629 		 */
2630 		if (asus->throttle_thermal_policy_available) {
2631 			err = throttle_thermal_policy_write(asus);
2632 			if (err)
2633 				return err;
2634 		/* Similar is true for laptops with this fan */
2635 		} else if (asus->fan_type == FAN_TYPE_SPEC83) {
2636 			err = asus_fan_set_auto(asus);
2637 			if (err)
2638 				return err;
2639 		} else {
2640 			/* Safeguard against fautly ACPI tables */
2641 			err = fan_curve_get_factory_default(asus, data->device_id);
2642 			if (err)
2643 				return err;
2644 			err = fan_curve_write(asus, data);
2645 			if (err)
2646 				return err;
2647 		}
2648 	}
2649 	return count;
2650 }
2651 
2652 /* CPU */
2653 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2654 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2655 			       FAN_CURVE_DEV_CPU, 0);
2656 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2657 			       FAN_CURVE_DEV_CPU, 1);
2658 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2659 			       FAN_CURVE_DEV_CPU, 2);
2660 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2661 			       FAN_CURVE_DEV_CPU, 3);
2662 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2663 			       FAN_CURVE_DEV_CPU, 4);
2664 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2665 			       FAN_CURVE_DEV_CPU, 5);
2666 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2667 			       FAN_CURVE_DEV_CPU, 6);
2668 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2669 			       FAN_CURVE_DEV_CPU, 7);
2670 
2671 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2672 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2673 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2674 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2675 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2676 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2677 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2678 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2679 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2680 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2681 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2682 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2683 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2684 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2685 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2686 			       FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2687 
2688 /* GPU */
2689 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2690 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2691 			       FAN_CURVE_DEV_GPU, 0);
2692 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2693 			       FAN_CURVE_DEV_GPU, 1);
2694 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2695 			       FAN_CURVE_DEV_GPU, 2);
2696 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2697 			       FAN_CURVE_DEV_GPU, 3);
2698 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2699 			       FAN_CURVE_DEV_GPU, 4);
2700 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2701 			       FAN_CURVE_DEV_GPU, 5);
2702 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2703 			       FAN_CURVE_DEV_GPU, 6);
2704 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2705 			       FAN_CURVE_DEV_GPU, 7);
2706 
2707 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2708 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2709 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2710 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2711 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2712 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2713 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2714 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2715 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2716 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2717 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2718 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2719 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2720 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2721 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2722 			       FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2723 
2724 static struct attribute *asus_fan_curve_attr[] = {
2725 	/* CPU */
2726 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
2727 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2728 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2729 	&sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2730 	&sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2731 	&sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2732 	&sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2733 	&sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2734 	&sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2735 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2736 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2737 	&sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2738 	&sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2739 	&sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2740 	&sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2741 	&sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2742 	&sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2743 	/* GPU */
2744 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
2745 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2746 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2747 	&sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2748 	&sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2749 	&sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2750 	&sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2751 	&sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2752 	&sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2753 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2754 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2755 	&sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2756 	&sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2757 	&sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2758 	&sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2759 	&sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2760 	&sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2761 	NULL
2762 };
2763 
2764 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2765 					 struct attribute *attr, int idx)
2766 {
2767 	struct device *dev = kobj_to_dev(kobj);
2768 	struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2769 
2770 	/*
2771 	 * Check the char instead of casting attr as there are two attr types
2772 	 * involved here (attr1 and attr2)
2773 	 */
2774 	if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2775 		return 0644;
2776 
2777 	if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2778 		return 0644;
2779 
2780 	return 0;
2781 }
2782 
2783 static const struct attribute_group asus_fan_curve_attr_group = {
2784 	.is_visible = asus_fan_curve_is_visible,
2785 	.attrs = asus_fan_curve_attr,
2786 };
2787 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2788 
2789 /*
2790  * Must be initialised after throttle_thermal_policy_check_present() as
2791  * we check the status of throttle_thermal_policy_available during init.
2792  */
2793 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2794 {
2795 	struct device *dev = &asus->platform_device->dev;
2796 	struct device *hwmon;
2797 	int err;
2798 
2799 	err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2800 				      ASUS_WMI_DEVID_CPU_FAN_CURVE);
2801 	if (err)
2802 		return err;
2803 
2804 	err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2805 				      ASUS_WMI_DEVID_GPU_FAN_CURVE);
2806 	if (err)
2807 		return err;
2808 
2809 	if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2810 		return 0;
2811 
2812 	hwmon = devm_hwmon_device_register_with_groups(
2813 		dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2814 
2815 	if (IS_ERR(hwmon)) {
2816 		dev_err(dev,
2817 			"Could not register asus_custom_fan_curve device\n");
2818 		return PTR_ERR(hwmon);
2819 	}
2820 
2821 	return 0;
2822 }
2823 
2824 /* Throttle thermal policy ****************************************************/
2825 
2826 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2827 {
2828 	u32 result;
2829 	int err;
2830 
2831 	asus->throttle_thermal_policy_available = false;
2832 
2833 	err = asus_wmi_get_devstate(asus,
2834 				    ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2835 				    &result);
2836 	if (err) {
2837 		if (err == -ENODEV)
2838 			return 0;
2839 		return err;
2840 	}
2841 
2842 	if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2843 		asus->throttle_thermal_policy_available = true;
2844 
2845 	return 0;
2846 }
2847 
2848 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2849 {
2850 	int err;
2851 	u8 value;
2852 	u32 retval;
2853 
2854 	value = asus->throttle_thermal_policy_mode;
2855 
2856 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2857 				    value, &retval);
2858 
2859 	sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2860 			"throttle_thermal_policy");
2861 
2862 	if (err) {
2863 		pr_warn("Failed to set throttle thermal policy: %d\n", err);
2864 		return err;
2865 	}
2866 
2867 	if (retval != 1) {
2868 		pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2869 			retval);
2870 		return -EIO;
2871 	}
2872 
2873 	/* Must set to disabled if mode is toggled */
2874 	if (asus->cpu_fan_curve_available)
2875 		asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2876 	if (asus->gpu_fan_curve_available)
2877 		asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2878 
2879 	return 0;
2880 }
2881 
2882 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2883 {
2884 	if (!asus->throttle_thermal_policy_available)
2885 		return 0;
2886 
2887 	asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2888 	return throttle_thermal_policy_write(asus);
2889 }
2890 
2891 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2892 {
2893 	u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2894 	int err;
2895 
2896 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2897 		new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2898 
2899 	asus->throttle_thermal_policy_mode = new_mode;
2900 	err = throttle_thermal_policy_write(asus);
2901 	if (err)
2902 		return err;
2903 
2904 	/*
2905 	 * Ensure that platform_profile updates userspace with the change to ensure
2906 	 * that platform_profile and throttle_thermal_policy_mode are in sync.
2907 	 */
2908 	platform_profile_notify();
2909 
2910 	return 0;
2911 }
2912 
2913 static ssize_t throttle_thermal_policy_show(struct device *dev,
2914 				   struct device_attribute *attr, char *buf)
2915 {
2916 	struct asus_wmi *asus = dev_get_drvdata(dev);
2917 	u8 mode = asus->throttle_thermal_policy_mode;
2918 
2919 	return sysfs_emit(buf, "%d\n", mode);
2920 }
2921 
2922 static ssize_t throttle_thermal_policy_store(struct device *dev,
2923 				    struct device_attribute *attr,
2924 				    const char *buf, size_t count)
2925 {
2926 	struct asus_wmi *asus = dev_get_drvdata(dev);
2927 	u8 new_mode;
2928 	int result;
2929 	int err;
2930 
2931 	result = kstrtou8(buf, 10, &new_mode);
2932 	if (result < 0)
2933 		return result;
2934 
2935 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2936 		return -EINVAL;
2937 
2938 	asus->throttle_thermal_policy_mode = new_mode;
2939 	err = throttle_thermal_policy_write(asus);
2940 	if (err)
2941 		return err;
2942 
2943 	/*
2944 	 * Ensure that platform_profile updates userspace with the change to ensure
2945 	 * that platform_profile and throttle_thermal_policy_mode are in sync.
2946 	 */
2947 	platform_profile_notify();
2948 
2949 	return count;
2950 }
2951 
2952 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2953 static DEVICE_ATTR_RW(throttle_thermal_policy);
2954 
2955 /* Platform profile ***********************************************************/
2956 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2957 					enum platform_profile_option *profile)
2958 {
2959 	struct asus_wmi *asus;
2960 	int tp;
2961 
2962 	asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2963 
2964 	tp = asus->throttle_thermal_policy_mode;
2965 
2966 	switch (tp) {
2967 	case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2968 		*profile = PLATFORM_PROFILE_BALANCED;
2969 		break;
2970 	case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2971 		*profile = PLATFORM_PROFILE_PERFORMANCE;
2972 		break;
2973 	case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2974 		*profile = PLATFORM_PROFILE_QUIET;
2975 		break;
2976 	default:
2977 		return -EINVAL;
2978 	}
2979 
2980 	return 0;
2981 }
2982 
2983 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2984 					enum platform_profile_option profile)
2985 {
2986 	struct asus_wmi *asus;
2987 	int tp;
2988 
2989 	asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2990 
2991 	switch (profile) {
2992 	case PLATFORM_PROFILE_PERFORMANCE:
2993 		tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2994 		break;
2995 	case PLATFORM_PROFILE_BALANCED:
2996 		tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2997 		break;
2998 	case PLATFORM_PROFILE_QUIET:
2999 		tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
3000 		break;
3001 	default:
3002 		return -EOPNOTSUPP;
3003 	}
3004 
3005 	asus->throttle_thermal_policy_mode = tp;
3006 	return throttle_thermal_policy_write(asus);
3007 }
3008 
3009 static int platform_profile_setup(struct asus_wmi *asus)
3010 {
3011 	struct device *dev = &asus->platform_device->dev;
3012 	int err;
3013 
3014 	/*
3015 	 * Not an error if a component platform_profile relies on is unavailable
3016 	 * so early return, skipping the setup of platform_profile.
3017 	 */
3018 	if (!asus->throttle_thermal_policy_available)
3019 		return 0;
3020 
3021 	dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
3022 
3023 	asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
3024 	asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
3025 
3026 	set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
3027 	set_bit(PLATFORM_PROFILE_BALANCED,
3028 		asus->platform_profile_handler.choices);
3029 	set_bit(PLATFORM_PROFILE_PERFORMANCE,
3030 		asus->platform_profile_handler.choices);
3031 
3032 	err = platform_profile_register(&asus->platform_profile_handler);
3033 	if (err)
3034 		return err;
3035 
3036 	asus->platform_profile_support = true;
3037 	return 0;
3038 }
3039 
3040 /* Backlight ******************************************************************/
3041 
3042 static int read_backlight_power(struct asus_wmi *asus)
3043 {
3044 	int ret;
3045 
3046 	if (asus->driver->quirks->store_backlight_power)
3047 		ret = !asus->driver->panel_power;
3048 	else
3049 		ret = asus_wmi_get_devstate_simple(asus,
3050 						   ASUS_WMI_DEVID_BACKLIGHT);
3051 
3052 	if (ret < 0)
3053 		return ret;
3054 
3055 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3056 }
3057 
3058 static int read_brightness_max(struct asus_wmi *asus)
3059 {
3060 	u32 retval;
3061 	int err;
3062 
3063 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3064 	if (err < 0)
3065 		return err;
3066 
3067 	retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
3068 	retval >>= 8;
3069 
3070 	if (!retval)
3071 		return -ENODEV;
3072 
3073 	return retval;
3074 }
3075 
3076 static int read_brightness(struct backlight_device *bd)
3077 {
3078 	struct asus_wmi *asus = bl_get_data(bd);
3079 	u32 retval;
3080 	int err;
3081 
3082 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3083 	if (err < 0)
3084 		return err;
3085 
3086 	return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3087 }
3088 
3089 static u32 get_scalar_command(struct backlight_device *bd)
3090 {
3091 	struct asus_wmi *asus = bl_get_data(bd);
3092 	u32 ctrl_param = 0;
3093 
3094 	if ((asus->driver->brightness < bd->props.brightness) ||
3095 	    bd->props.brightness == bd->props.max_brightness)
3096 		ctrl_param = 0x00008001;
3097 	else if ((asus->driver->brightness > bd->props.brightness) ||
3098 		 bd->props.brightness == 0)
3099 		ctrl_param = 0x00008000;
3100 
3101 	asus->driver->brightness = bd->props.brightness;
3102 
3103 	return ctrl_param;
3104 }
3105 
3106 static int update_bl_status(struct backlight_device *bd)
3107 {
3108 	struct asus_wmi *asus = bl_get_data(bd);
3109 	u32 ctrl_param;
3110 	int power, err = 0;
3111 
3112 	power = read_backlight_power(asus);
3113 	if (power != -ENODEV && bd->props.power != power) {
3114 		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
3115 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
3116 					    ctrl_param, NULL);
3117 		if (asus->driver->quirks->store_backlight_power)
3118 			asus->driver->panel_power = bd->props.power;
3119 
3120 		/* When using scalar brightness, updating the brightness
3121 		 * will mess with the backlight power */
3122 		if (asus->driver->quirks->scalar_panel_brightness)
3123 			return err;
3124 	}
3125 
3126 	if (asus->driver->quirks->scalar_panel_brightness)
3127 		ctrl_param = get_scalar_command(bd);
3128 	else
3129 		ctrl_param = bd->props.brightness;
3130 
3131 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
3132 				    ctrl_param, NULL);
3133 
3134 	return err;
3135 }
3136 
3137 static const struct backlight_ops asus_wmi_bl_ops = {
3138 	.get_brightness = read_brightness,
3139 	.update_status = update_bl_status,
3140 };
3141 
3142 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3143 {
3144 	struct backlight_device *bd = asus->backlight_device;
3145 	int old = bd->props.brightness;
3146 	int new = old;
3147 
3148 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3149 		new = code - NOTIFY_BRNUP_MIN + 1;
3150 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3151 		new = code - NOTIFY_BRNDOWN_MIN;
3152 
3153 	bd->props.brightness = new;
3154 	backlight_update_status(bd);
3155 	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
3156 
3157 	return old;
3158 }
3159 
3160 static int asus_wmi_backlight_init(struct asus_wmi *asus)
3161 {
3162 	struct backlight_device *bd;
3163 	struct backlight_properties props;
3164 	int max;
3165 	int power;
3166 
3167 	max = read_brightness_max(asus);
3168 	if (max < 0)
3169 		return max;
3170 
3171 	power = read_backlight_power(asus);
3172 	if (power == -ENODEV)
3173 		power = FB_BLANK_UNBLANK;
3174 	else if (power < 0)
3175 		return power;
3176 
3177 	memset(&props, 0, sizeof(struct backlight_properties));
3178 	props.type = BACKLIGHT_PLATFORM;
3179 	props.max_brightness = max;
3180 	bd = backlight_device_register(asus->driver->name,
3181 				       &asus->platform_device->dev, asus,
3182 				       &asus_wmi_bl_ops, &props);
3183 	if (IS_ERR(bd)) {
3184 		pr_err("Could not register backlight device\n");
3185 		return PTR_ERR(bd);
3186 	}
3187 
3188 	asus->backlight_device = bd;
3189 
3190 	if (asus->driver->quirks->store_backlight_power)
3191 		asus->driver->panel_power = power;
3192 
3193 	bd->props.brightness = read_brightness(bd);
3194 	bd->props.power = power;
3195 	backlight_update_status(bd);
3196 
3197 	asus->driver->brightness = bd->props.brightness;
3198 
3199 	return 0;
3200 }
3201 
3202 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3203 {
3204 	backlight_device_unregister(asus->backlight_device);
3205 
3206 	asus->backlight_device = NULL;
3207 }
3208 
3209 static int is_display_toggle(int code)
3210 {
3211 	/* display toggle keys */
3212 	if ((code >= 0x61 && code <= 0x67) ||
3213 	    (code >= 0x8c && code <= 0x93) ||
3214 	    (code >= 0xa0 && code <= 0xa7) ||
3215 	    (code >= 0xd0 && code <= 0xd5))
3216 		return 1;
3217 
3218 	return 0;
3219 }
3220 
3221 /* Fn-lock ********************************************************************/
3222 
3223 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3224 {
3225 	u32 result;
3226 
3227 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3228 
3229 	return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3230 		!(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3231 }
3232 
3233 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3234 {
3235 	int mode = asus->fnlock_locked;
3236 
3237 	asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3238 }
3239 
3240 /* WMI events *****************************************************************/
3241 
3242 static int asus_wmi_get_event_code(u32 value)
3243 {
3244 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3245 	union acpi_object *obj;
3246 	acpi_status status;
3247 	int code;
3248 
3249 	status = wmi_get_event_data(value, &response);
3250 	if (ACPI_FAILURE(status)) {
3251 		pr_warn("Failed to get WMI notify code: %s\n",
3252 				acpi_format_exception(status));
3253 		return -EIO;
3254 	}
3255 
3256 	obj = (union acpi_object *)response.pointer;
3257 
3258 	if (obj && obj->type == ACPI_TYPE_INTEGER)
3259 		code = (int)(obj->integer.value & WMI_EVENT_MASK);
3260 	else
3261 		code = -EIO;
3262 
3263 	kfree(obj);
3264 	return code;
3265 }
3266 
3267 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3268 {
3269 	unsigned int key_value = 1;
3270 	bool autorelease = 1;
3271 	int orig_code = code;
3272 
3273 	if (asus->driver->key_filter) {
3274 		asus->driver->key_filter(asus->driver, &code, &key_value,
3275 					 &autorelease);
3276 		if (code == ASUS_WMI_KEY_IGNORE)
3277 			return;
3278 	}
3279 
3280 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3281 		code = ASUS_WMI_BRN_UP;
3282 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3283 		code = ASUS_WMI_BRN_DOWN;
3284 
3285 	if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3286 		if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3287 			asus_wmi_backlight_notify(asus, orig_code);
3288 			return;
3289 		}
3290 	}
3291 
3292 	if (code == NOTIFY_KBD_BRTUP) {
3293 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3294 		return;
3295 	}
3296 	if (code == NOTIFY_KBD_BRTDWN) {
3297 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3298 		return;
3299 	}
3300 	if (code == NOTIFY_KBD_BRTTOGGLE) {
3301 		if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3302 			kbd_led_set_by_kbd(asus, 0);
3303 		else
3304 			kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3305 		return;
3306 	}
3307 
3308 	if (code == NOTIFY_FNLOCK_TOGGLE) {
3309 		asus->fnlock_locked = !asus->fnlock_locked;
3310 		asus_wmi_fnlock_update(asus);
3311 		return;
3312 	}
3313 
3314 	if (code == asus->tablet_switch_event_code) {
3315 		asus_wmi_tablet_mode_get_state(asus);
3316 		return;
3317 	}
3318 
3319 	if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
3320 		if (asus->fan_boost_mode_available)
3321 			fan_boost_mode_switch_next(asus);
3322 		if (asus->throttle_thermal_policy_available)
3323 			throttle_thermal_policy_switch_next(asus);
3324 		return;
3325 
3326 	}
3327 
3328 	if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3329 		return;
3330 
3331 	if (!sparse_keymap_report_event(asus->inputdev, code,
3332 					key_value, autorelease))
3333 		pr_info("Unknown key code 0x%x\n", code);
3334 }
3335 
3336 static void asus_wmi_notify(u32 value, void *context)
3337 {
3338 	struct asus_wmi *asus = context;
3339 	int code;
3340 	int i;
3341 
3342 	for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3343 		code = asus_wmi_get_event_code(value);
3344 		if (code < 0) {
3345 			pr_warn("Failed to get notify code: %d\n", code);
3346 			return;
3347 		}
3348 
3349 		if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3350 			return;
3351 
3352 		asus_wmi_handle_event_code(code, asus);
3353 
3354 		/*
3355 		 * Double check that queue is present:
3356 		 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3357 		 */
3358 		if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3359 			return;
3360 	}
3361 
3362 	pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3363 }
3364 
3365 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3366 {
3367 	int code;
3368 	int i;
3369 
3370 	for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3371 		code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3372 		if (code < 0) {
3373 			pr_warn("Failed to get event during flush: %d\n", code);
3374 			return code;
3375 		}
3376 
3377 		if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3378 			return 0;
3379 	}
3380 
3381 	pr_warn("Failed to flush event queue\n");
3382 	return -EIO;
3383 }
3384 
3385 /* Sysfs **********************************************************************/
3386 
3387 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3388 			     const char *buf, size_t count)
3389 {
3390 	u32 retval;
3391 	int err, value;
3392 
3393 	value = asus_wmi_get_devstate_simple(asus, devid);
3394 	if (value < 0)
3395 		return value;
3396 
3397 	err = kstrtoint(buf, 0, &value);
3398 	if (err)
3399 		return err;
3400 
3401 	err = asus_wmi_set_devstate(devid, value, &retval);
3402 	if (err < 0)
3403 		return err;
3404 
3405 	return count;
3406 }
3407 
3408 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3409 {
3410 	int value = asus_wmi_get_devstate_simple(asus, devid);
3411 
3412 	if (value < 0)
3413 		return value;
3414 
3415 	return sprintf(buf, "%d\n", value);
3416 }
3417 
3418 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
3419 	static ssize_t show_##_name(struct device *dev,			\
3420 				    struct device_attribute *attr,	\
3421 				    char *buf)				\
3422 	{								\
3423 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
3424 									\
3425 		return show_sys_wmi(asus, _cm, buf);			\
3426 	}								\
3427 	static ssize_t store_##_name(struct device *dev,		\
3428 				     struct device_attribute *attr,	\
3429 				     const char *buf, size_t count)	\
3430 	{								\
3431 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
3432 									\
3433 		return store_sys_wmi(asus, _cm, buf, count);		\
3434 	}								\
3435 	static struct device_attribute dev_attr_##_name = {		\
3436 		.attr = {						\
3437 			.name = __stringify(_name),			\
3438 			.mode = _mode },				\
3439 		.show   = show_##_name,					\
3440 		.store  = store_##_name,				\
3441 	}
3442 
3443 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3444 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3445 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3446 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3447 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3448 
3449 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3450 			   const char *buf, size_t count)
3451 {
3452 	int value, rv;
3453 
3454 	rv = kstrtoint(buf, 0, &value);
3455 	if (rv)
3456 		return rv;
3457 
3458 	if (value < 0 || value > 2)
3459 		return -EINVAL;
3460 
3461 	rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3462 	if (rv < 0)
3463 		return rv;
3464 
3465 	return count;
3466 }
3467 
3468 static DEVICE_ATTR_WO(cpufv);
3469 
3470 static struct attribute *platform_attributes[] = {
3471 	&dev_attr_cpufv.attr,
3472 	&dev_attr_camera.attr,
3473 	&dev_attr_cardr.attr,
3474 	&dev_attr_touchpad.attr,
3475 	&dev_attr_egpu_enable.attr,
3476 	&dev_attr_dgpu_disable.attr,
3477 	&dev_attr_gpu_mux_mode.attr,
3478 	&dev_attr_lid_resume.attr,
3479 	&dev_attr_als_enable.attr,
3480 	&dev_attr_fan_boost_mode.attr,
3481 	&dev_attr_throttle_thermal_policy.attr,
3482 	&dev_attr_panel_od.attr,
3483 	NULL
3484 };
3485 
3486 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3487 				    struct attribute *attr, int idx)
3488 {
3489 	struct device *dev = kobj_to_dev(kobj);
3490 	struct asus_wmi *asus = dev_get_drvdata(dev);
3491 	bool ok = true;
3492 	int devid = -1;
3493 
3494 	if (attr == &dev_attr_camera.attr)
3495 		devid = ASUS_WMI_DEVID_CAMERA;
3496 	else if (attr == &dev_attr_cardr.attr)
3497 		devid = ASUS_WMI_DEVID_CARDREADER;
3498 	else if (attr == &dev_attr_touchpad.attr)
3499 		devid = ASUS_WMI_DEVID_TOUCHPAD;
3500 	else if (attr == &dev_attr_lid_resume.attr)
3501 		devid = ASUS_WMI_DEVID_LID_RESUME;
3502 	else if (attr == &dev_attr_als_enable.attr)
3503 		devid = ASUS_WMI_DEVID_ALS_ENABLE;
3504 	else if (attr == &dev_attr_egpu_enable.attr)
3505 		ok = asus->egpu_enable_available;
3506 	else if (attr == &dev_attr_dgpu_disable.attr)
3507 		ok = asus->dgpu_disable_available;
3508 	else if (attr == &dev_attr_gpu_mux_mode.attr)
3509 		ok = asus->gpu_mux_mode_available;
3510 	else if (attr == &dev_attr_fan_boost_mode.attr)
3511 		ok = asus->fan_boost_mode_available;
3512 	else if (attr == &dev_attr_throttle_thermal_policy.attr)
3513 		ok = asus->throttle_thermal_policy_available;
3514 	else if (attr == &dev_attr_panel_od.attr)
3515 		ok = asus->panel_overdrive_available;
3516 
3517 	if (devid != -1)
3518 		ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3519 
3520 	return ok ? attr->mode : 0;
3521 }
3522 
3523 static const struct attribute_group platform_attribute_group = {
3524 	.is_visible = asus_sysfs_is_visible,
3525 	.attrs = platform_attributes
3526 };
3527 
3528 static void asus_wmi_sysfs_exit(struct platform_device *device)
3529 {
3530 	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3531 }
3532 
3533 static int asus_wmi_sysfs_init(struct platform_device *device)
3534 {
3535 	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3536 }
3537 
3538 /* Platform device ************************************************************/
3539 
3540 static int asus_wmi_platform_init(struct asus_wmi *asus)
3541 {
3542 	struct device *dev = &asus->platform_device->dev;
3543 	char *wmi_uid;
3544 	int rv;
3545 
3546 	/* INIT enable hotkeys on some models */
3547 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3548 		pr_info("Initialization: %#x\n", rv);
3549 
3550 	/* We don't know yet what to do with this version... */
3551 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3552 		pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3553 		asus->spec = rv;
3554 	}
3555 
3556 	/*
3557 	 * The SFUN method probably allows the original driver to get the list
3558 	 * of features supported by a given model. For now, 0x0100 or 0x0800
3559 	 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3560 	 * The significance of others is yet to be found.
3561 	 */
3562 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3563 		pr_info("SFUN value: %#x\n", rv);
3564 		asus->sfun = rv;
3565 	}
3566 
3567 	/*
3568 	 * Eee PC and Notebooks seems to have different method_id for DSTS,
3569 	 * but it may also be related to the BIOS's SPEC.
3570 	 * Note, on most Eeepc, there is no way to check if a method exist
3571 	 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3572 	 * but once again, SPEC may probably be used for that kind of things.
3573 	 *
3574 	 * Additionally at least TUF Gaming series laptops return nothing for
3575 	 * unknown methods, so the detection in this way is not possible.
3576 	 *
3577 	 * There is strong indication that only ACPI WMI devices that have _UID
3578 	 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3579 	 */
3580 	wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3581 	if (!wmi_uid)
3582 		return -ENODEV;
3583 
3584 	if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3585 		dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3586 		asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3587 	} else {
3588 		dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3589 		asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3590 	}
3591 
3592 	/*
3593 	 * Some devices can have multiple event codes stored in a queue before
3594 	 * the module load if it was unloaded intermittently after calling
3595 	 * the INIT method (enables event handling). The WMI notify handler is
3596 	 * expected to retrieve all event codes until a retrieved code equals
3597 	 * queue end marker (One or Ones). Old codes are flushed from the queue
3598 	 * upon module load. Not enabling this when it should be has minimal
3599 	 * visible impact so fall back if anything goes wrong.
3600 	 */
3601 	wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3602 	if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3603 		dev_info(dev, "Detected ATK, enable event queue\n");
3604 
3605 		if (!asus_wmi_notify_queue_flush(asus))
3606 			asus->wmi_event_queue = true;
3607 	}
3608 
3609 	/* CWAP allow to define the behavior of the Fn+F2 key,
3610 	 * this method doesn't seems to be present on Eee PCs */
3611 	if (asus->driver->quirks->wapf >= 0)
3612 		asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3613 				      asus->driver->quirks->wapf, NULL);
3614 
3615 	return 0;
3616 }
3617 
3618 /* debugfs ********************************************************************/
3619 
3620 struct asus_wmi_debugfs_node {
3621 	struct asus_wmi *asus;
3622 	char *name;
3623 	int (*show) (struct seq_file *m, void *data);
3624 };
3625 
3626 static int show_dsts(struct seq_file *m, void *data)
3627 {
3628 	struct asus_wmi *asus = m->private;
3629 	int err;
3630 	u32 retval = -1;
3631 
3632 	err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3633 	if (err < 0)
3634 		return err;
3635 
3636 	seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3637 
3638 	return 0;
3639 }
3640 
3641 static int show_devs(struct seq_file *m, void *data)
3642 {
3643 	struct asus_wmi *asus = m->private;
3644 	int err;
3645 	u32 retval = -1;
3646 
3647 	err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3648 				    &retval);
3649 	if (err < 0)
3650 		return err;
3651 
3652 	seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3653 		   asus->debug.ctrl_param, retval);
3654 
3655 	return 0;
3656 }
3657 
3658 static int show_call(struct seq_file *m, void *data)
3659 {
3660 	struct asus_wmi *asus = m->private;
3661 	struct bios_args args = {
3662 		.arg0 = asus->debug.dev_id,
3663 		.arg1 = asus->debug.ctrl_param,
3664 	};
3665 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3666 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3667 	union acpi_object *obj;
3668 	acpi_status status;
3669 
3670 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3671 				     0, asus->debug.method_id,
3672 				     &input, &output);
3673 
3674 	if (ACPI_FAILURE(status))
3675 		return -EIO;
3676 
3677 	obj = (union acpi_object *)output.pointer;
3678 	if (obj && obj->type == ACPI_TYPE_INTEGER)
3679 		seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3680 			   asus->debug.dev_id, asus->debug.ctrl_param,
3681 			   (u32) obj->integer.value);
3682 	else
3683 		seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3684 			   asus->debug.dev_id, asus->debug.ctrl_param,
3685 			   obj ? obj->type : -1);
3686 
3687 	kfree(obj);
3688 
3689 	return 0;
3690 }
3691 
3692 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3693 	{NULL, "devs", show_devs},
3694 	{NULL, "dsts", show_dsts},
3695 	{NULL, "call", show_call},
3696 };
3697 
3698 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3699 {
3700 	struct asus_wmi_debugfs_node *node = inode->i_private;
3701 
3702 	return single_open(file, node->show, node->asus);
3703 }
3704 
3705 static const struct file_operations asus_wmi_debugfs_io_ops = {
3706 	.owner = THIS_MODULE,
3707 	.open = asus_wmi_debugfs_open,
3708 	.read = seq_read,
3709 	.llseek = seq_lseek,
3710 	.release = single_release,
3711 };
3712 
3713 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3714 {
3715 	debugfs_remove_recursive(asus->debug.root);
3716 }
3717 
3718 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3719 {
3720 	int i;
3721 
3722 	asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3723 
3724 	debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3725 			   &asus->debug.method_id);
3726 
3727 	debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3728 			   &asus->debug.dev_id);
3729 
3730 	debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3731 			   &asus->debug.ctrl_param);
3732 
3733 	for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3734 		struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3735 
3736 		node->asus = asus;
3737 		debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3738 				    asus->debug.root, node,
3739 				    &asus_wmi_debugfs_io_ops);
3740 	}
3741 }
3742 
3743 /* Init / exit ****************************************************************/
3744 
3745 static int asus_wmi_add(struct platform_device *pdev)
3746 {
3747 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3748 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3749 	struct asus_wmi *asus;
3750 	acpi_status status;
3751 	int err;
3752 	u32 result;
3753 
3754 	asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3755 	if (!asus)
3756 		return -ENOMEM;
3757 
3758 	asus->driver = wdrv;
3759 	asus->platform_device = pdev;
3760 	wdrv->platform_device = pdev;
3761 	platform_set_drvdata(asus->platform_device, asus);
3762 
3763 	if (wdrv->detect_quirks)
3764 		wdrv->detect_quirks(asus->driver);
3765 
3766 	err = asus_wmi_platform_init(asus);
3767 	if (err)
3768 		goto fail_platform;
3769 
3770 	asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
3771 	asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
3772 	asus->gpu_mux_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX);
3773 	asus->kbd_rgb_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE);
3774 	asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
3775 	asus->panel_overdrive_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PANEL_OD);
3776 
3777 	err = fan_boost_mode_check_present(asus);
3778 	if (err)
3779 		goto fail_fan_boost_mode;
3780 
3781 	err = throttle_thermal_policy_check_present(asus);
3782 	if (err)
3783 		goto fail_throttle_thermal_policy;
3784 	else
3785 		throttle_thermal_policy_set_default(asus);
3786 
3787 	err = platform_profile_setup(asus);
3788 	if (err)
3789 		goto fail_platform_profile_setup;
3790 
3791 	err = asus_wmi_sysfs_init(asus->platform_device);
3792 	if (err)
3793 		goto fail_sysfs;
3794 
3795 	err = asus_wmi_input_init(asus);
3796 	if (err)
3797 		goto fail_input;
3798 
3799 	err = asus_wmi_fan_init(asus); /* probably no problems on error */
3800 
3801 	err = asus_wmi_hwmon_init(asus);
3802 	if (err)
3803 		goto fail_hwmon;
3804 
3805 	err = asus_wmi_custom_fan_curve_init(asus);
3806 	if (err)
3807 		goto fail_custom_fan_curve;
3808 
3809 	err = asus_wmi_led_init(asus);
3810 	if (err)
3811 		goto fail_leds;
3812 
3813 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3814 	if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3815 		asus->driver->wlan_ctrl_by_user = 1;
3816 
3817 	if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3818 		err = asus_wmi_rfkill_init(asus);
3819 		if (err)
3820 			goto fail_rfkill;
3821 	}
3822 
3823 	if (asus->driver->quirks->wmi_force_als_set)
3824 		asus_wmi_set_als();
3825 
3826 	if (asus->driver->quirks->xusb2pr)
3827 		asus_wmi_set_xusb2pr(asus);
3828 
3829 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3830 		err = asus_wmi_backlight_init(asus);
3831 		if (err && err != -ENODEV)
3832 			goto fail_backlight;
3833 	} else if (asus->driver->quirks->wmi_backlight_set_devstate)
3834 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3835 
3836 	if (asus_wmi_has_fnlock_key(asus)) {
3837 		asus->fnlock_locked = fnlock_default;
3838 		asus_wmi_fnlock_update(asus);
3839 	}
3840 
3841 	status = wmi_install_notify_handler(asus->driver->event_guid,
3842 					    asus_wmi_notify, asus);
3843 	if (ACPI_FAILURE(status)) {
3844 		pr_err("Unable to register notify handler - %d\n", status);
3845 		err = -ENODEV;
3846 		goto fail_wmi_handler;
3847 	}
3848 
3849 	asus_wmi_battery_init(asus);
3850 
3851 	asus_wmi_debugfs_init(asus);
3852 
3853 	return 0;
3854 
3855 fail_wmi_handler:
3856 	asus_wmi_backlight_exit(asus);
3857 fail_backlight:
3858 	asus_wmi_rfkill_exit(asus);
3859 fail_rfkill:
3860 	asus_wmi_led_exit(asus);
3861 fail_leds:
3862 fail_hwmon:
3863 	asus_wmi_input_exit(asus);
3864 fail_input:
3865 	asus_wmi_sysfs_exit(asus->platform_device);
3866 fail_sysfs:
3867 fail_throttle_thermal_policy:
3868 fail_custom_fan_curve:
3869 fail_platform_profile_setup:
3870 	if (asus->platform_profile_support)
3871 		platform_profile_remove();
3872 fail_fan_boost_mode:
3873 fail_platform:
3874 	kfree(asus);
3875 	return err;
3876 }
3877 
3878 static int asus_wmi_remove(struct platform_device *device)
3879 {
3880 	struct asus_wmi *asus;
3881 
3882 	asus = platform_get_drvdata(device);
3883 	wmi_remove_notify_handler(asus->driver->event_guid);
3884 	asus_wmi_backlight_exit(asus);
3885 	asus_wmi_input_exit(asus);
3886 	asus_wmi_led_exit(asus);
3887 	asus_wmi_rfkill_exit(asus);
3888 	asus_wmi_debugfs_exit(asus);
3889 	asus_wmi_sysfs_exit(asus->platform_device);
3890 	asus_fan_set_auto(asus);
3891 	throttle_thermal_policy_set_default(asus);
3892 	asus_wmi_battery_exit(asus);
3893 
3894 	if (asus->platform_profile_support)
3895 		platform_profile_remove();
3896 
3897 	kfree(asus);
3898 	return 0;
3899 }
3900 
3901 /* Platform driver - hibernate/resume callbacks *******************************/
3902 
3903 static int asus_hotk_thaw(struct device *device)
3904 {
3905 	struct asus_wmi *asus = dev_get_drvdata(device);
3906 
3907 	if (asus->wlan.rfkill) {
3908 		bool wlan;
3909 
3910 		/*
3911 		 * Work around bios bug - acpi _PTS turns off the wireless led
3912 		 * during suspend.  Normally it restores it on resume, but
3913 		 * we should kick it ourselves in case hibernation is aborted.
3914 		 */
3915 		wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3916 		asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3917 	}
3918 
3919 	return 0;
3920 }
3921 
3922 static int asus_hotk_resume(struct device *device)
3923 {
3924 	struct asus_wmi *asus = dev_get_drvdata(device);
3925 
3926 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3927 		kbd_led_update(asus);
3928 
3929 	if (asus_wmi_has_fnlock_key(asus))
3930 		asus_wmi_fnlock_update(asus);
3931 
3932 	asus_wmi_tablet_mode_get_state(asus);
3933 	return 0;
3934 }
3935 
3936 static int asus_hotk_restore(struct device *device)
3937 {
3938 	struct asus_wmi *asus = dev_get_drvdata(device);
3939 	int bl;
3940 
3941 	/* Refresh both wlan rfkill state and pci hotplug */
3942 	if (asus->wlan.rfkill)
3943 		asus_rfkill_hotplug(asus);
3944 
3945 	if (asus->bluetooth.rfkill) {
3946 		bl = !asus_wmi_get_devstate_simple(asus,
3947 						   ASUS_WMI_DEVID_BLUETOOTH);
3948 		rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3949 	}
3950 	if (asus->wimax.rfkill) {
3951 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3952 		rfkill_set_sw_state(asus->wimax.rfkill, bl);
3953 	}
3954 	if (asus->wwan3g.rfkill) {
3955 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3956 		rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3957 	}
3958 	if (asus->gps.rfkill) {
3959 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3960 		rfkill_set_sw_state(asus->gps.rfkill, bl);
3961 	}
3962 	if (asus->uwb.rfkill) {
3963 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3964 		rfkill_set_sw_state(asus->uwb.rfkill, bl);
3965 	}
3966 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3967 		kbd_led_update(asus);
3968 
3969 	if (asus_wmi_has_fnlock_key(asus))
3970 		asus_wmi_fnlock_update(asus);
3971 
3972 	asus_wmi_tablet_mode_get_state(asus);
3973 	return 0;
3974 }
3975 
3976 static const struct dev_pm_ops asus_pm_ops = {
3977 	.thaw = asus_hotk_thaw,
3978 	.restore = asus_hotk_restore,
3979 	.resume = asus_hotk_resume,
3980 };
3981 
3982 /* Registration ***************************************************************/
3983 
3984 static int asus_wmi_probe(struct platform_device *pdev)
3985 {
3986 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3987 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3988 	int ret;
3989 
3990 	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3991 		pr_warn("ASUS Management GUID not found\n");
3992 		return -ENODEV;
3993 	}
3994 
3995 	if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3996 		pr_warn("ASUS Event GUID not found\n");
3997 		return -ENODEV;
3998 	}
3999 
4000 	if (wdrv->probe) {
4001 		ret = wdrv->probe(pdev);
4002 		if (ret)
4003 			return ret;
4004 	}
4005 
4006 	return asus_wmi_add(pdev);
4007 }
4008 
4009 static bool used;
4010 
4011 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
4012 {
4013 	struct platform_driver *platform_driver;
4014 	struct platform_device *platform_device;
4015 
4016 	if (used)
4017 		return -EBUSY;
4018 
4019 	platform_driver = &driver->platform_driver;
4020 	platform_driver->remove = asus_wmi_remove;
4021 	platform_driver->driver.owner = driver->owner;
4022 	platform_driver->driver.name = driver->name;
4023 	platform_driver->driver.pm = &asus_pm_ops;
4024 
4025 	platform_device = platform_create_bundle(platform_driver,
4026 						 asus_wmi_probe,
4027 						 NULL, 0, NULL, 0);
4028 	if (IS_ERR(platform_device))
4029 		return PTR_ERR(platform_device);
4030 
4031 	used = true;
4032 	return 0;
4033 }
4034 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
4035 
4036 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
4037 {
4038 	platform_device_unregister(driver->platform_device);
4039 	platform_driver_unregister(&driver->platform_driver);
4040 	used = false;
4041 }
4042 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
4043 
4044 static int __init asus_wmi_init(void)
4045 {
4046 	pr_info("ASUS WMI generic driver loaded\n");
4047 	return 0;
4048 }
4049 
4050 static void __exit asus_wmi_exit(void)
4051 {
4052 	pr_info("ASUS WMI generic driver unloaded\n");
4053 }
4054 
4055 module_init(asus_wmi_init);
4056 module_exit(asus_wmi_exit);
4057