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