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