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