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