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