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