1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Asus PC WMI hotkey driver
5 * Copyright(C) 2010 Intel Corporation.
6 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
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>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
24 #include <linux/backlight.h>
25 #include <linux/leds.h>
26 #include <linux/rfkill.h>
27 #include <linux/pci.h>
28 #include <linux/pci_hotplug.h>
29 #include <linux/platform_profile.h>
30 #include <linux/power_supply.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/platform_data/x86/asus-wmi.h>
36 #include <linux/platform_device.h>
37 #include <linux/acpi.h>
38 #include <linux/dmi.h>
39 #include <linux/units.h>
41 #include <acpi/battery.h>
42 #include <acpi/video.h>
46 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
47 "Yong Wang <yong.y.wang@intel.com>");
48 MODULE_DESCRIPTION("Asus Generic WMI Driver");
49 MODULE_LICENSE("GPL");
51 static bool fnlock_default = true;
52 module_param(fnlock_default, bool, 0444);
54 #define to_asus_wmi_driver(pdrv) \
55 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
57 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
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
72 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
74 #define ASUS_FAN_DESC "cpu_fan"
75 #define ASUS_FAN_MFUN 0x13
76 #define ASUS_FAN_SFUN_READ 0x06
77 #define ASUS_FAN_SFUN_WRITE 0x07
79 /* Based on standard hwmon pwmX_enable values */
80 #define ASUS_FAN_CTRL_FULLSPEED 0
81 #define ASUS_FAN_CTRL_MANUAL 1
82 #define ASUS_FAN_CTRL_AUTO 2
84 #define ASUS_FAN_BOOST_MODE_NORMAL 0
85 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
86 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
87 #define ASUS_FAN_BOOST_MODE_SILENT 2
88 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
89 #define ASUS_FAN_BOOST_MODES_MASK 0x03
91 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
92 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
93 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
95 #define USB_INTEL_XUSB2PR 0xD0
96 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
98 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
99 #define ASUS_ACPI_UID_ATK "ATK"
101 #define WMI_EVENT_QUEUE_SIZE 0x10
102 #define WMI_EVENT_QUEUE_END 0x1
103 #define WMI_EVENT_MASK 0xFFFF
104 /* The WMI hotkey event value is always the same. */
105 #define WMI_EVENT_VALUE_ATK 0xFF
107 #define WMI_EVENT_MASK 0xFFFF
109 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
111 static bool ashs_present(void)
114 while (ashs_ids[i]) {
115 if (acpi_dev_found(ashs_ids[i++]))
124 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
130 * Struct that's used for all methods called via AGFN. Naming is
131 * identically to the AML code.
134 u16 mfun; /* probably "Multi-function" to be called */
135 u16 sfun; /* probably "Sub-function" to be called */
136 u16 len; /* size of the hole struct, including subfunction fields */
137 u8 stas; /* not used by now */
138 u8 err; /* zero on success */
141 /* struct used for calling fan read and write methods */
142 struct agfn_fan_args {
143 struct agfn_args agfn; /* common fields */
144 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
145 u32 speed; /* read: RPM/100 - write: 0-255 */
149 * <platform>/ - debugfs root directory
150 * dev_id - current dev_id
151 * ctrl_param - current ctrl_param
152 * method_id - current method_id
153 * devs - call DEVS(dev_id, ctrl_param) and print result
154 * dsts - call DSTS(dev_id) and print result
155 * call - call method_id(dev_id, ctrl_param) and print result
157 struct asus_wmi_debug {
165 struct asus_wmi *asus;
166 struct rfkill *rfkill;
172 FAN_TYPE_AGFN, /* deprecated on newer platforms */
173 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
180 bool wmi_event_queue;
182 struct input_dev *inputdev;
183 struct backlight_device *backlight_device;
184 struct platform_device *platform_device;
186 struct led_classdev wlan_led;
188 struct led_classdev tpd_led;
190 struct led_classdev kbd_led;
192 struct led_classdev lightbar_led;
194 struct workqueue_struct *led_workqueue;
195 struct work_struct tpd_led_work;
196 struct work_struct wlan_led_work;
197 struct work_struct lightbar_led_work;
199 struct asus_rfkill wlan;
200 struct asus_rfkill bluetooth;
201 struct asus_rfkill wimax;
202 struct asus_rfkill wwan3g;
203 struct asus_rfkill gps;
204 struct asus_rfkill uwb;
206 enum fan_type fan_type;
210 bool fan_boost_mode_available;
211 u8 fan_boost_mode_mask;
214 bool egpu_enable_available; // 0 = enable
217 bool dgpu_disable_available;
220 bool throttle_thermal_policy_available;
221 u8 throttle_thermal_policy_mode;
223 struct platform_profile_handler platform_profile_handler;
224 bool platform_profile_support;
226 // The RSOC controls the maximum charging percentage.
227 bool battery_rsoc_available;
229 bool panel_overdrive_available;
230 bool panel_overdrive;
232 struct hotplug_slot hotplug_slot;
233 struct mutex hotplug_lock;
234 struct mutex wmi_lock;
235 struct workqueue_struct *hotplug_workqueue;
236 struct work_struct hotplug_work;
240 struct asus_wmi_debug debug;
242 struct asus_wmi_driver *driver;
245 /* WMI ************************************************************************/
247 static int asus_wmi_evaluate_method3(u32 method_id,
248 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
250 struct bios_args args = {
255 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
256 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
258 union acpi_object *obj;
261 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
264 if (ACPI_FAILURE(status))
267 obj = (union acpi_object *)output.pointer;
268 if (obj && obj->type == ACPI_TYPE_INTEGER)
269 tmp = (u32) obj->integer.value;
276 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
282 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
284 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
286 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
288 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
290 struct acpi_buffer input;
296 * Copy to dma capable address otherwise memory corruption occurs as
297 * bios has to be able to access it.
299 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
300 input.length = args.length;
303 phys_addr = virt_to_phys(input.pointer);
305 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
306 phys_addr, 0, &retval);
308 memcpy(args.pointer, input.pointer, args.length);
310 kfree(input.pointer);
317 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
319 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
322 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
325 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
329 /* Helper for special devices with magic return codes */
330 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
331 u32 dev_id, u32 mask)
336 err = asus_wmi_get_devstate(asus, dev_id, &retval);
340 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
343 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
344 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
348 return retval & mask;
351 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
353 return asus_wmi_get_devstate_bits(asus, dev_id,
354 ASUS_WMI_DSTS_STATUS_BIT);
357 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
360 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
362 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
365 /* Input **********************************************************************/
367 static int asus_wmi_input_init(struct asus_wmi *asus)
371 asus->inputdev = input_allocate_device();
375 asus->inputdev->name = asus->driver->input_name;
376 asus->inputdev->phys = asus->driver->input_phys;
377 asus->inputdev->id.bustype = BUS_HOST;
378 asus->inputdev->dev.parent = &asus->platform_device->dev;
379 set_bit(EV_REP, asus->inputdev->evbit);
381 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
385 if (asus->driver->quirks->use_kbd_dock_devid) {
386 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
388 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
389 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
390 } else if (result != -ENODEV) {
391 pr_err("Error checking for keyboard-dock: %d\n", result);
395 if (asus->driver->quirks->use_lid_flip_devid) {
396 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
398 asus->driver->quirks->use_lid_flip_devid = 0;
400 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
401 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
402 } else if (result == -ENODEV) {
403 pr_err("This device has lid_flip quirk but got ENODEV checking it. This is a bug.");
405 pr_err("Error checking for lid-flip: %d\n", result);
409 err = input_register_device(asus->inputdev);
416 input_free_device(asus->inputdev);
420 static void asus_wmi_input_exit(struct asus_wmi *asus)
423 input_unregister_device(asus->inputdev);
425 asus->inputdev = NULL;
428 /* Tablet mode ****************************************************************/
430 static void lid_flip_tablet_mode_get_state(struct asus_wmi *asus)
432 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_LID_FLIP);
435 input_report_switch(asus->inputdev, SW_TABLET_MODE, result);
436 input_sync(asus->inputdev);
440 /* dGPU ********************************************************************/
441 static int dgpu_disable_check_present(struct asus_wmi *asus)
446 asus->dgpu_disable_available = false;
448 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
455 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
456 asus->dgpu_disable_available = true;
457 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
463 static int dgpu_disable_write(struct asus_wmi *asus)
469 /* Don't rely on type conversion */
470 value = asus->dgpu_disable ? 1 : 0;
472 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
474 pr_warn("Failed to set dgpu disable: %d\n", err);
479 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
483 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
488 static ssize_t dgpu_disable_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 struct asus_wmi *asus = dev_get_drvdata(dev);
492 u8 mode = asus->dgpu_disable;
494 return sysfs_emit(buf, "%d\n", mode);
498 * A user may be required to store the value twice, typcial store first, then
499 * rescan PCI bus to activate power, then store a second time to save correctly.
500 * The reason for this is that an extra code path in the ACPI is enabled when
501 * the device and bus are powered.
503 static ssize_t dgpu_disable_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
510 struct asus_wmi *asus = dev_get_drvdata(dev);
512 result = kstrtobool(buf, &disable);
516 asus->dgpu_disable = disable;
518 result = dgpu_disable_write(asus);
525 static DEVICE_ATTR_RW(dgpu_disable);
527 /* eGPU ********************************************************************/
528 static int egpu_enable_check_present(struct asus_wmi *asus)
533 asus->egpu_enable_available = false;
535 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_EGPU, &result);
542 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
543 asus->egpu_enable_available = true;
544 asus->egpu_enable = result & ASUS_WMI_DSTS_STATUS_BIT;
550 static int egpu_enable_write(struct asus_wmi *asus)
556 /* Don't rely on type conversion */
557 value = asus->egpu_enable ? 1 : 0;
559 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, value, &retval);
562 pr_warn("Failed to set egpu disable: %d\n", err);
567 pr_warn("Failed to set egpu disable (retval): 0x%x\n", retval);
571 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
576 static ssize_t egpu_enable_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
579 struct asus_wmi *asus = dev_get_drvdata(dev);
580 bool mode = asus->egpu_enable;
582 return sysfs_emit(buf, "%d\n", mode);
585 /* The ACPI call to enable the eGPU also disables the internal dGPU */
586 static ssize_t egpu_enable_store(struct device *dev,
587 struct device_attribute *attr,
588 const char *buf, size_t count)
593 struct asus_wmi *asus = dev_get_drvdata(dev);
595 result = kstrtobool(buf, &enable);
599 asus->egpu_enable = enable;
601 result = egpu_enable_write(asus);
605 /* Ensure that the kernel status of dgpu is updated */
606 result = dgpu_disable_check_present(asus);
613 static DEVICE_ATTR_RW(egpu_enable);
615 /* Battery ********************************************************************/
617 /* The battery maximum charging percentage */
618 static int charge_end_threshold;
620 static ssize_t charge_control_end_threshold_store(struct device *dev,
621 struct device_attribute *attr,
622 const char *buf, size_t count)
626 ret = kstrtouint(buf, 10, &value);
630 if (value < 0 || value > 100)
633 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
640 /* There isn't any method in the DSDT to read the threshold, so we
641 * save the threshold.
643 charge_end_threshold = value;
647 static ssize_t charge_control_end_threshold_show(struct device *device,
648 struct device_attribute *attr,
651 return sprintf(buf, "%d\n", charge_end_threshold);
654 static DEVICE_ATTR_RW(charge_control_end_threshold);
656 static int asus_wmi_battery_add(struct power_supply *battery)
658 /* The WMI method does not provide a way to specific a battery, so we
659 * just assume it is the first battery.
660 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
661 * battery is named BATT.
663 if (strcmp(battery->desc->name, "BAT0") != 0 &&
664 strcmp(battery->desc->name, "BAT1") != 0 &&
665 strcmp(battery->desc->name, "BATC") != 0 &&
666 strcmp(battery->desc->name, "BATT") != 0)
669 if (device_create_file(&battery->dev,
670 &dev_attr_charge_control_end_threshold))
673 /* The charge threshold is only reset when the system is power cycled,
674 * and we can't get the current threshold so let set it to 100% when
675 * a battery is added.
677 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
678 charge_end_threshold = 100;
683 static int asus_wmi_battery_remove(struct power_supply *battery)
685 device_remove_file(&battery->dev,
686 &dev_attr_charge_control_end_threshold);
690 static struct acpi_battery_hook battery_hook = {
691 .add_battery = asus_wmi_battery_add,
692 .remove_battery = asus_wmi_battery_remove,
693 .name = "ASUS Battery Extension",
696 static void asus_wmi_battery_init(struct asus_wmi *asus)
698 asus->battery_rsoc_available = false;
699 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
700 asus->battery_rsoc_available = true;
701 battery_hook_register(&battery_hook);
705 static void asus_wmi_battery_exit(struct asus_wmi *asus)
707 if (asus->battery_rsoc_available)
708 battery_hook_unregister(&battery_hook);
711 /* LEDs ***********************************************************************/
714 * These functions actually update the LED's, and are called from a
715 * workqueue. By doing this as separate work rather than when the LED
716 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
717 * potentially bad time, such as a timer interrupt.
719 static void tpd_led_update(struct work_struct *work)
722 struct asus_wmi *asus;
724 asus = container_of(work, struct asus_wmi, tpd_led_work);
726 ctrl_param = asus->tpd_led_wk;
727 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
730 static void tpd_led_set(struct led_classdev *led_cdev,
731 enum led_brightness value)
733 struct asus_wmi *asus;
735 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
737 asus->tpd_led_wk = !!value;
738 queue_work(asus->led_workqueue, &asus->tpd_led_work);
741 static int read_tpd_led_state(struct asus_wmi *asus)
743 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
746 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
748 struct asus_wmi *asus;
750 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
752 return read_tpd_led_state(asus);
755 static void kbd_led_update(struct asus_wmi *asus)
759 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
760 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
763 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
769 * bit 7: light on/off
770 * bit 8-10: environment (0: dark, 1: normal, 2: light)
771 * bit 17: status unknown
773 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
776 /* Unknown status is considered as off */
777 if (retval == 0x8000)
784 *level = retval & 0x7F;
786 *env = (retval >> 8) & 0x7F;
790 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
792 struct asus_wmi *asus;
795 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
796 max_level = asus->kbd_led.max_brightness;
798 asus->kbd_led_wk = clamp_val(value, 0, max_level);
799 kbd_led_update(asus);
802 static void kbd_led_set(struct led_classdev *led_cdev,
803 enum led_brightness value)
805 /* Prevent disabling keyboard backlight on module unregister */
806 if (led_cdev->flags & LED_UNREGISTERING)
809 do_kbd_led_set(led_cdev, value);
812 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
814 struct led_classdev *led_cdev = &asus->kbd_led;
816 do_kbd_led_set(led_cdev, value);
817 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
820 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
822 struct asus_wmi *asus;
825 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
827 retval = kbd_led_read(asus, &value, NULL);
834 static int wlan_led_unknown_state(struct asus_wmi *asus)
838 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
840 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
843 static void wlan_led_update(struct work_struct *work)
846 struct asus_wmi *asus;
848 asus = container_of(work, struct asus_wmi, wlan_led_work);
850 ctrl_param = asus->wlan_led_wk;
851 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
854 static void wlan_led_set(struct led_classdev *led_cdev,
855 enum led_brightness value)
857 struct asus_wmi *asus;
859 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
861 asus->wlan_led_wk = !!value;
862 queue_work(asus->led_workqueue, &asus->wlan_led_work);
865 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
867 struct asus_wmi *asus;
870 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
871 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
873 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
876 static void lightbar_led_update(struct work_struct *work)
878 struct asus_wmi *asus;
881 asus = container_of(work, struct asus_wmi, lightbar_led_work);
883 ctrl_param = asus->lightbar_led_wk;
884 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
887 static void lightbar_led_set(struct led_classdev *led_cdev,
888 enum led_brightness value)
890 struct asus_wmi *asus;
892 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
894 asus->lightbar_led_wk = !!value;
895 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
898 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
900 struct asus_wmi *asus;
903 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
904 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
906 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
909 static void asus_wmi_led_exit(struct asus_wmi *asus)
911 led_classdev_unregister(&asus->kbd_led);
912 led_classdev_unregister(&asus->tpd_led);
913 led_classdev_unregister(&asus->wlan_led);
914 led_classdev_unregister(&asus->lightbar_led);
916 if (asus->led_workqueue)
917 destroy_workqueue(asus->led_workqueue);
920 static int asus_wmi_led_init(struct asus_wmi *asus)
924 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
925 if (!asus->led_workqueue)
928 if (read_tpd_led_state(asus) >= 0) {
929 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
931 asus->tpd_led.name = "asus::touchpad";
932 asus->tpd_led.brightness_set = tpd_led_set;
933 asus->tpd_led.brightness_get = tpd_led_get;
934 asus->tpd_led.max_brightness = 1;
936 rv = led_classdev_register(&asus->platform_device->dev,
942 if (!kbd_led_read(asus, &led_val, NULL)) {
943 asus->kbd_led_wk = led_val;
944 asus->kbd_led.name = "asus::kbd_backlight";
945 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
946 asus->kbd_led.brightness_set = kbd_led_set;
947 asus->kbd_led.brightness_get = kbd_led_get;
948 asus->kbd_led.max_brightness = 3;
950 rv = led_classdev_register(&asus->platform_device->dev,
956 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
957 && (asus->driver->quirks->wapf > 0)) {
958 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
960 asus->wlan_led.name = "asus::wlan";
961 asus->wlan_led.brightness_set = wlan_led_set;
962 if (!wlan_led_unknown_state(asus))
963 asus->wlan_led.brightness_get = wlan_led_get;
964 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
965 asus->wlan_led.max_brightness = 1;
966 asus->wlan_led.default_trigger = "asus-wlan";
968 rv = led_classdev_register(&asus->platform_device->dev,
974 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
975 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
977 asus->lightbar_led.name = "asus::lightbar";
978 asus->lightbar_led.brightness_set = lightbar_led_set;
979 asus->lightbar_led.brightness_get = lightbar_led_get;
980 asus->lightbar_led.max_brightness = 1;
982 rv = led_classdev_register(&asus->platform_device->dev,
983 &asus->lightbar_led);
988 asus_wmi_led_exit(asus);
993 /* RF *************************************************************************/
996 * PCI hotplug (for wlan rfkill)
998 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1000 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1007 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1009 struct pci_dev *dev;
1010 struct pci_bus *bus;
1015 mutex_lock(&asus->wmi_lock);
1016 blocked = asus_wlan_rfkill_blocked(asus);
1017 mutex_unlock(&asus->wmi_lock);
1019 mutex_lock(&asus->hotplug_lock);
1020 pci_lock_rescan_remove();
1022 if (asus->wlan.rfkill)
1023 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1025 if (asus->hotplug_slot.ops) {
1026 bus = pci_find_bus(0, 1);
1028 pr_warn("Unable to find PCI bus 1?\n");
1032 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1033 pr_err("Unable to read PCI config space?\n");
1036 absent = (l == 0xffffffff);
1038 if (blocked != absent) {
1039 pr_warn("BIOS says wireless lan is %s, "
1040 "but the pci device is %s\n",
1041 blocked ? "blocked" : "unblocked",
1042 absent ? "absent" : "present");
1043 pr_warn("skipped wireless hotplug as probably "
1044 "inappropriate for this model\n");
1049 dev = pci_get_slot(bus, 0);
1051 /* Device already present */
1055 dev = pci_scan_single_device(bus, 0);
1057 pci_bus_assign_resources(bus);
1058 pci_bus_add_device(dev);
1061 dev = pci_get_slot(bus, 0);
1063 pci_stop_and_remove_bus_device(dev);
1070 pci_unlock_rescan_remove();
1071 mutex_unlock(&asus->hotplug_lock);
1074 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1076 struct asus_wmi *asus = data;
1078 if (event != ACPI_NOTIFY_BUS_CHECK)
1082 * We can't call directly asus_rfkill_hotplug because most
1083 * of the time WMBC is still being executed and not reetrant.
1084 * There is currently no way to tell ACPICA that we want this
1085 * method to be serialized, we schedule a asus_rfkill_hotplug
1086 * call later, in a safer context.
1088 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1091 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1096 status = acpi_get_handle(NULL, node, &handle);
1097 if (ACPI_FAILURE(status))
1100 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1101 asus_rfkill_notify, asus);
1102 if (ACPI_FAILURE(status))
1103 pr_warn("Failed to register notify on %s\n", node);
1108 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1110 acpi_status status = AE_OK;
1113 status = acpi_get_handle(NULL, node, &handle);
1114 if (ACPI_FAILURE(status))
1117 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1118 asus_rfkill_notify);
1119 if (ACPI_FAILURE(status))
1120 pr_err("Error removing rfkill notify handler %s\n", node);
1123 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1126 struct asus_wmi *asus = container_of(hotplug_slot,
1127 struct asus_wmi, hotplug_slot);
1128 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1137 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1138 .get_adapter_status = asus_get_adapter_status,
1139 .get_power_status = asus_get_adapter_status,
1142 static void asus_hotplug_work(struct work_struct *work)
1144 struct asus_wmi *asus;
1146 asus = container_of(work, struct asus_wmi, hotplug_work);
1147 asus_rfkill_hotplug(asus);
1150 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1153 struct pci_bus *bus = pci_find_bus(0, 1);
1156 pr_err("Unable to find wifi PCI bus\n");
1160 asus->hotplug_workqueue =
1161 create_singlethread_workqueue("hotplug_workqueue");
1162 if (!asus->hotplug_workqueue)
1163 goto error_workqueue;
1165 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1167 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1169 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1171 pr_err("Unable to register hotplug slot - %d\n", ret);
1172 goto error_register;
1178 asus->hotplug_slot.ops = NULL;
1179 destroy_workqueue(asus->hotplug_workqueue);
1187 static int asus_rfkill_set(void *data, bool blocked)
1189 struct asus_rfkill *priv = data;
1190 u32 ctrl_param = !blocked;
1191 u32 dev_id = priv->dev_id;
1194 * If the user bit is set, BIOS can't set and record the wlan status,
1195 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1196 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1197 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1198 * while setting the wlan status through WMI.
1199 * This is also the behavior that windows app will do.
1201 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1202 priv->asus->driver->wlan_ctrl_by_user)
1203 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1205 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1208 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1210 struct asus_rfkill *priv = data;
1213 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1218 rfkill_set_sw_state(priv->rfkill, !result);
1221 static int asus_rfkill_wlan_set(void *data, bool blocked)
1223 struct asus_rfkill *priv = data;
1224 struct asus_wmi *asus = priv->asus;
1228 * This handler is enabled only if hotplug is enabled.
1229 * In this case, the asus_wmi_set_devstate() will
1230 * trigger a wmi notification and we need to wait
1231 * this call to finish before being able to call
1234 mutex_lock(&asus->wmi_lock);
1235 ret = asus_rfkill_set(data, blocked);
1236 mutex_unlock(&asus->wmi_lock);
1240 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1241 .set_block = asus_rfkill_wlan_set,
1242 .query = asus_rfkill_query,
1245 static const struct rfkill_ops asus_rfkill_ops = {
1246 .set_block = asus_rfkill_set,
1247 .query = asus_rfkill_query,
1250 static int asus_new_rfkill(struct asus_wmi *asus,
1251 struct asus_rfkill *arfkill,
1252 const char *name, enum rfkill_type type, int dev_id)
1254 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1255 struct rfkill **rfkill = &arfkill->rfkill;
1260 arfkill->dev_id = dev_id;
1261 arfkill->asus = asus;
1263 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1264 asus->driver->quirks->hotplug_wireless)
1265 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1266 &asus_rfkill_wlan_ops, arfkill);
1268 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1269 &asus_rfkill_ops, arfkill);
1274 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1275 (asus->driver->quirks->wapf > 0))
1276 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1278 rfkill_init_sw_state(*rfkill, !result);
1279 result = rfkill_register(*rfkill);
1281 rfkill_destroy(*rfkill);
1288 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1290 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1293 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1294 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1295 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1296 if (asus->wlan.rfkill) {
1297 rfkill_unregister(asus->wlan.rfkill);
1298 rfkill_destroy(asus->wlan.rfkill);
1299 asus->wlan.rfkill = NULL;
1302 * Refresh pci hotplug in case the rfkill state was changed after
1303 * asus_unregister_rfkill_notifier()
1305 asus_rfkill_hotplug(asus);
1306 if (asus->hotplug_slot.ops)
1307 pci_hp_deregister(&asus->hotplug_slot);
1308 if (asus->hotplug_workqueue)
1309 destroy_workqueue(asus->hotplug_workqueue);
1311 if (asus->bluetooth.rfkill) {
1312 rfkill_unregister(asus->bluetooth.rfkill);
1313 rfkill_destroy(asus->bluetooth.rfkill);
1314 asus->bluetooth.rfkill = NULL;
1316 if (asus->wimax.rfkill) {
1317 rfkill_unregister(asus->wimax.rfkill);
1318 rfkill_destroy(asus->wimax.rfkill);
1319 asus->wimax.rfkill = NULL;
1321 if (asus->wwan3g.rfkill) {
1322 rfkill_unregister(asus->wwan3g.rfkill);
1323 rfkill_destroy(asus->wwan3g.rfkill);
1324 asus->wwan3g.rfkill = NULL;
1326 if (asus->gps.rfkill) {
1327 rfkill_unregister(asus->gps.rfkill);
1328 rfkill_destroy(asus->gps.rfkill);
1329 asus->gps.rfkill = NULL;
1331 if (asus->uwb.rfkill) {
1332 rfkill_unregister(asus->uwb.rfkill);
1333 rfkill_destroy(asus->uwb.rfkill);
1334 asus->uwb.rfkill = NULL;
1338 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1342 mutex_init(&asus->hotplug_lock);
1343 mutex_init(&asus->wmi_lock);
1345 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1346 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1348 if (result && result != -ENODEV)
1351 result = asus_new_rfkill(asus, &asus->bluetooth,
1352 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1353 ASUS_WMI_DEVID_BLUETOOTH);
1355 if (result && result != -ENODEV)
1358 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1359 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1361 if (result && result != -ENODEV)
1364 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1365 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1367 if (result && result != -ENODEV)
1370 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1371 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1373 if (result && result != -ENODEV)
1376 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1377 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1379 if (result && result != -ENODEV)
1382 if (!asus->driver->quirks->hotplug_wireless)
1385 result = asus_setup_pci_hotplug(asus);
1387 * If we get -EBUSY then something else is handling the PCI hotplug -
1388 * don't fail in this case
1390 if (result == -EBUSY)
1393 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1394 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1395 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1397 * Refresh pci hotplug in case the rfkill state was changed during
1400 asus_rfkill_hotplug(asus);
1403 if (result && result != -ENODEV)
1404 asus_wmi_rfkill_exit(asus);
1406 if (result == -ENODEV)
1412 /* Panel Overdrive ************************************************************/
1413 static int panel_od_check_present(struct asus_wmi *asus)
1418 asus->panel_overdrive_available = false;
1420 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_PANEL_OD, &result);
1427 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
1428 asus->panel_overdrive_available = true;
1429 asus->panel_overdrive = result & ASUS_WMI_DSTS_STATUS_BIT;
1435 static int panel_od_write(struct asus_wmi *asus)
1441 /* Don't rely on type conversion */
1442 value = asus->panel_overdrive ? 1 : 0;
1444 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, value, &retval);
1447 pr_warn("Failed to set panel overdrive: %d\n", err);
1452 pr_warn("Failed to set panel overdrive (retval): 0x%x\n", retval);
1456 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
1461 static ssize_t panel_od_show(struct device *dev,
1462 struct device_attribute *attr, char *buf)
1464 struct asus_wmi *asus = dev_get_drvdata(dev);
1466 return sysfs_emit(buf, "%d\n", asus->panel_overdrive);
1469 static ssize_t panel_od_store(struct device *dev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t count)
1476 struct asus_wmi *asus = dev_get_drvdata(dev);
1478 result = kstrtobool(buf, &overdrive);
1482 asus->panel_overdrive = overdrive;
1483 result = panel_od_write(asus);
1491 static DEVICE_ATTR_RW(panel_od);
1493 /* Quirks *********************************************************************/
1495 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1497 struct pci_dev *xhci_pdev;
1498 u32 orig_ports_available;
1499 u32 ports_available = asus->driver->quirks->xusb2pr;
1501 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1502 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1508 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1509 &orig_ports_available);
1511 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1512 cpu_to_le32(ports_available));
1514 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1515 orig_ports_available, ports_available);
1519 * Some devices dont support or have borcken get_als method
1520 * but still support set method.
1522 static void asus_wmi_set_als(void)
1524 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1527 /* Hwmon device ***************************************************************/
1529 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1532 struct agfn_fan_args args = {
1533 .agfn.len = sizeof(args),
1534 .agfn.mfun = ASUS_FAN_MFUN,
1535 .agfn.sfun = ASUS_FAN_SFUN_READ,
1539 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1545 status = asus_wmi_evaluate_method_agfn(input);
1547 if (status || args.agfn.err)
1551 *speed = args.speed;
1556 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1559 struct agfn_fan_args args = {
1560 .agfn.len = sizeof(args),
1561 .agfn.mfun = ASUS_FAN_MFUN,
1562 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1564 .speed = speed ? *speed : 0,
1566 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1569 /* 1: for setting 1st fan's speed 0: setting auto mode */
1570 if (fan != 1 && fan != 0)
1573 status = asus_wmi_evaluate_method_agfn(input);
1575 if (status || args.agfn.err)
1578 if (speed && fan == 1)
1579 asus->agfn_pwm = *speed;
1585 * Check if we can read the speed of one fan. If true we assume we can also
1588 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1594 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1598 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1603 * We need to find a better way, probably using sfun,
1605 * Currently we disable it if:
1606 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1607 * - reverved bits are non-zero
1608 * - sfun and presence bit are not set
1610 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1611 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1614 static int asus_fan_set_auto(struct asus_wmi *asus)
1619 switch (asus->fan_type) {
1620 case FAN_TYPE_SPEC83:
1621 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1631 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1644 static ssize_t pwm1_show(struct device *dev,
1645 struct device_attribute *attr,
1648 struct asus_wmi *asus = dev_get_drvdata(dev);
1652 /* If we already set a value then just return it */
1653 if (asus->agfn_pwm >= 0)
1654 return sprintf(buf, "%d\n", asus->agfn_pwm);
1657 * If we haven't set already set a value through the AGFN interface,
1658 * we read a current value through the (now-deprecated) FAN_CTRL device.
1660 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1666 if (value == 1) /* Low Speed */
1668 else if (value == 2)
1670 else if (value == 3)
1673 pr_err("Unknown fan speed %#x\n", value);
1677 return sprintf(buf, "%d\n", value);
1680 static ssize_t pwm1_store(struct device *dev,
1681 struct device_attribute *attr,
1682 const char *buf, size_t count) {
1683 struct asus_wmi *asus = dev_get_drvdata(dev);
1688 ret = kstrtouint(buf, 10, &value);
1692 value = clamp(value, 0, 255);
1694 state = asus_agfn_fan_speed_write(asus, 1, &value);
1696 pr_warn("Setting fan speed failed: %d\n", state);
1698 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1703 static ssize_t fan1_input_show(struct device *dev,
1704 struct device_attribute *attr,
1707 struct asus_wmi *asus = dev_get_drvdata(dev);
1711 switch (asus->fan_type) {
1712 case FAN_TYPE_SPEC83:
1713 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1722 /* no speed readable on manual mode */
1723 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1726 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1728 pr_warn("reading fan speed failed: %d\n", ret);
1737 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1740 static ssize_t pwm1_enable_show(struct device *dev,
1741 struct device_attribute *attr,
1744 struct asus_wmi *asus = dev_get_drvdata(dev);
1747 * Just read back the cached pwm mode.
1749 * For the CPU_FAN device, the spec indicates that we should be
1750 * able to read the device status and consult bit 19 to see if we
1751 * are in Full On or Automatic mode. However, this does not work
1752 * in practice on X532FL at least (the bit is always 0) and there's
1753 * also nothing in the DSDT to indicate that this behaviour exists.
1755 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1758 static ssize_t pwm1_enable_store(struct device *dev,
1759 struct device_attribute *attr,
1760 const char *buf, size_t count)
1762 struct asus_wmi *asus = dev_get_drvdata(dev);
1769 ret = kstrtouint(buf, 10, &state);
1773 if (asus->fan_type == FAN_TYPE_SPEC83) {
1774 switch (state) { /* standard documented hwmon values */
1775 case ASUS_FAN_CTRL_FULLSPEED:
1778 case ASUS_FAN_CTRL_AUTO:
1785 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1792 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1794 case ASUS_FAN_CTRL_MANUAL:
1797 case ASUS_FAN_CTRL_AUTO:
1798 status = asus_fan_set_auto(asus);
1808 asus->fan_pwm_mode = state;
1812 static ssize_t fan1_label_show(struct device *dev,
1813 struct device_attribute *attr,
1816 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1819 static ssize_t asus_hwmon_temp1(struct device *dev,
1820 struct device_attribute *attr,
1823 struct asus_wmi *asus = dev_get_drvdata(dev);
1827 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1831 return sprintf(buf, "%ld\n",
1832 deci_kelvin_to_millicelsius(value & 0xFFFF));
1836 static DEVICE_ATTR_RW(pwm1);
1837 static DEVICE_ATTR_RW(pwm1_enable);
1838 static DEVICE_ATTR_RO(fan1_input);
1839 static DEVICE_ATTR_RO(fan1_label);
1842 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1844 static struct attribute *hwmon_attributes[] = {
1845 &dev_attr_pwm1.attr,
1846 &dev_attr_pwm1_enable.attr,
1847 &dev_attr_fan1_input.attr,
1848 &dev_attr_fan1_label.attr,
1850 &dev_attr_temp1_input.attr,
1854 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1855 struct attribute *attr, int idx)
1857 struct device *dev = container_of(kobj, struct device, kobj);
1858 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1859 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1861 if (attr == &dev_attr_pwm1.attr) {
1862 if (asus->fan_type != FAN_TYPE_AGFN)
1864 } else if (attr == &dev_attr_fan1_input.attr
1865 || attr == &dev_attr_fan1_label.attr
1866 || attr == &dev_attr_pwm1_enable.attr) {
1867 if (asus->fan_type == FAN_TYPE_NONE)
1869 } else if (attr == &dev_attr_temp1_input.attr) {
1870 int err = asus_wmi_get_devstate(asus,
1871 ASUS_WMI_DEVID_THERMAL_CTRL,
1875 return 0; /* can't return negative here */
1878 * If the temperature value in deci-Kelvin is near the absolute
1879 * zero temperature, something is clearly wrong
1881 if (value == 0 || value == 1)
1888 static const struct attribute_group hwmon_attribute_group = {
1889 .is_visible = asus_hwmon_sysfs_is_visible,
1890 .attrs = hwmon_attributes
1892 __ATTRIBUTE_GROUPS(hwmon_attribute);
1894 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1896 struct device *dev = &asus->platform_device->dev;
1897 struct device *hwmon;
1899 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1900 hwmon_attribute_groups);
1902 if (IS_ERR(hwmon)) {
1903 pr_err("Could not register asus hwmon device\n");
1904 return PTR_ERR(hwmon);
1909 static int asus_wmi_fan_init(struct asus_wmi *asus)
1911 asus->fan_type = FAN_TYPE_NONE;
1912 asus->agfn_pwm = -1;
1914 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1915 asus->fan_type = FAN_TYPE_SPEC83;
1916 else if (asus_wmi_has_agfn_fan(asus))
1917 asus->fan_type = FAN_TYPE_AGFN;
1919 if (asus->fan_type == FAN_TYPE_NONE)
1922 asus_fan_set_auto(asus);
1923 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1927 /* Fan mode *******************************************************************/
1929 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1934 asus->fan_boost_mode_available = false;
1936 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1945 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1946 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1947 asus->fan_boost_mode_available = true;
1948 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1954 static int fan_boost_mode_write(struct asus_wmi *asus)
1960 value = asus->fan_boost_mode;
1962 pr_info("Set fan boost mode: %u\n", value);
1963 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1966 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
1970 pr_warn("Failed to set fan boost mode: %d\n", err);
1975 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1983 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1985 u8 mask = asus->fan_boost_mode_mask;
1987 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1988 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1989 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1990 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1991 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1992 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1993 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1994 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1996 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1998 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2001 return fan_boost_mode_write(asus);
2004 static ssize_t fan_boost_mode_show(struct device *dev,
2005 struct device_attribute *attr, char *buf)
2007 struct asus_wmi *asus = dev_get_drvdata(dev);
2009 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
2012 static ssize_t fan_boost_mode_store(struct device *dev,
2013 struct device_attribute *attr,
2014 const char *buf, size_t count)
2018 struct asus_wmi *asus = dev_get_drvdata(dev);
2019 u8 mask = asus->fan_boost_mode_mask;
2021 result = kstrtou8(buf, 10, &new_mode);
2023 pr_warn("Trying to store invalid value\n");
2027 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2028 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2030 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2031 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2033 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2037 asus->fan_boost_mode = new_mode;
2038 fan_boost_mode_write(asus);
2043 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2044 static DEVICE_ATTR_RW(fan_boost_mode);
2046 /* Throttle thermal policy ****************************************************/
2048 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2053 asus->throttle_thermal_policy_available = false;
2055 err = asus_wmi_get_devstate(asus,
2056 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2064 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2065 asus->throttle_thermal_policy_available = true;
2070 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2076 value = asus->throttle_thermal_policy_mode;
2078 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2081 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2082 "throttle_thermal_policy");
2085 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2090 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2098 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2100 if (!asus->throttle_thermal_policy_available)
2103 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2104 return throttle_thermal_policy_write(asus);
2107 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2109 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2112 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2113 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2115 asus->throttle_thermal_policy_mode = new_mode;
2116 err = throttle_thermal_policy_write(asus);
2121 * Ensure that platform_profile updates userspace with the change to ensure
2122 * that platform_profile and throttle_thermal_policy_mode are in sync.
2124 platform_profile_notify();
2129 static ssize_t throttle_thermal_policy_show(struct device *dev,
2130 struct device_attribute *attr, char *buf)
2132 struct asus_wmi *asus = dev_get_drvdata(dev);
2133 u8 mode = asus->throttle_thermal_policy_mode;
2135 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2138 static ssize_t throttle_thermal_policy_store(struct device *dev,
2139 struct device_attribute *attr,
2140 const char *buf, size_t count)
2142 struct asus_wmi *asus = dev_get_drvdata(dev);
2147 result = kstrtou8(buf, 10, &new_mode);
2151 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2154 asus->throttle_thermal_policy_mode = new_mode;
2155 err = throttle_thermal_policy_write(asus);
2160 * Ensure that platform_profile updates userspace with the change to ensure
2161 * that platform_profile and throttle_thermal_policy_mode are in sync.
2163 platform_profile_notify();
2168 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2169 static DEVICE_ATTR_RW(throttle_thermal_policy);
2171 /* Platform profile ***********************************************************/
2172 static int platform_profile_get(struct platform_profile_handler *pprof,
2173 enum platform_profile_option *profile)
2175 struct asus_wmi *asus;
2178 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2180 tp = asus->throttle_thermal_policy_mode;
2183 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2184 *profile = PLATFORM_PROFILE_BALANCED;
2186 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2187 *profile = PLATFORM_PROFILE_PERFORMANCE;
2189 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2190 *profile = PLATFORM_PROFILE_QUIET;
2199 static int platform_profile_set(struct platform_profile_handler *pprof,
2200 enum platform_profile_option profile)
2202 struct asus_wmi *asus;
2205 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2208 case PLATFORM_PROFILE_PERFORMANCE:
2209 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2211 case PLATFORM_PROFILE_BALANCED:
2212 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2214 case PLATFORM_PROFILE_QUIET:
2215 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2221 asus->throttle_thermal_policy_mode = tp;
2222 return throttle_thermal_policy_write(asus);
2225 static int platform_profile_setup(struct asus_wmi *asus)
2227 struct device *dev = &asus->platform_device->dev;
2231 * Not an error if a component platform_profile relies on is unavailable
2232 * so early return, skipping the setup of platform_profile.
2234 if (!asus->throttle_thermal_policy_available)
2237 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2239 asus->platform_profile_handler.profile_get = platform_profile_get;
2240 asus->platform_profile_handler.profile_set = platform_profile_set;
2242 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2243 set_bit(PLATFORM_PROFILE_BALANCED,
2244 asus->platform_profile_handler.choices);
2245 set_bit(PLATFORM_PROFILE_PERFORMANCE,
2246 asus->platform_profile_handler.choices);
2248 err = platform_profile_register(&asus->platform_profile_handler);
2252 asus->platform_profile_support = true;
2256 /* Backlight ******************************************************************/
2258 static int read_backlight_power(struct asus_wmi *asus)
2262 if (asus->driver->quirks->store_backlight_power)
2263 ret = !asus->driver->panel_power;
2265 ret = asus_wmi_get_devstate_simple(asus,
2266 ASUS_WMI_DEVID_BACKLIGHT);
2271 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2274 static int read_brightness_max(struct asus_wmi *asus)
2279 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2283 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2292 static int read_brightness(struct backlight_device *bd)
2294 struct asus_wmi *asus = bl_get_data(bd);
2298 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2302 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2305 static u32 get_scalar_command(struct backlight_device *bd)
2307 struct asus_wmi *asus = bl_get_data(bd);
2310 if ((asus->driver->brightness < bd->props.brightness) ||
2311 bd->props.brightness == bd->props.max_brightness)
2312 ctrl_param = 0x00008001;
2313 else if ((asus->driver->brightness > bd->props.brightness) ||
2314 bd->props.brightness == 0)
2315 ctrl_param = 0x00008000;
2317 asus->driver->brightness = bd->props.brightness;
2322 static int update_bl_status(struct backlight_device *bd)
2324 struct asus_wmi *asus = bl_get_data(bd);
2328 power = read_backlight_power(asus);
2329 if (power != -ENODEV && bd->props.power != power) {
2330 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2331 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2333 if (asus->driver->quirks->store_backlight_power)
2334 asus->driver->panel_power = bd->props.power;
2336 /* When using scalar brightness, updating the brightness
2337 * will mess with the backlight power */
2338 if (asus->driver->quirks->scalar_panel_brightness)
2342 if (asus->driver->quirks->scalar_panel_brightness)
2343 ctrl_param = get_scalar_command(bd);
2345 ctrl_param = bd->props.brightness;
2347 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2353 static const struct backlight_ops asus_wmi_bl_ops = {
2354 .get_brightness = read_brightness,
2355 .update_status = update_bl_status,
2358 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2360 struct backlight_device *bd = asus->backlight_device;
2361 int old = bd->props.brightness;
2364 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2365 new = code - NOTIFY_BRNUP_MIN + 1;
2366 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2367 new = code - NOTIFY_BRNDOWN_MIN;
2369 bd->props.brightness = new;
2370 backlight_update_status(bd);
2371 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2376 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2378 struct backlight_device *bd;
2379 struct backlight_properties props;
2383 max = read_brightness_max(asus);
2387 power = read_backlight_power(asus);
2388 if (power == -ENODEV)
2389 power = FB_BLANK_UNBLANK;
2393 memset(&props, 0, sizeof(struct backlight_properties));
2394 props.type = BACKLIGHT_PLATFORM;
2395 props.max_brightness = max;
2396 bd = backlight_device_register(asus->driver->name,
2397 &asus->platform_device->dev, asus,
2398 &asus_wmi_bl_ops, &props);
2400 pr_err("Could not register backlight device\n");
2404 asus->backlight_device = bd;
2406 if (asus->driver->quirks->store_backlight_power)
2407 asus->driver->panel_power = power;
2409 bd->props.brightness = read_brightness(bd);
2410 bd->props.power = power;
2411 backlight_update_status(bd);
2413 asus->driver->brightness = bd->props.brightness;
2418 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2420 backlight_device_unregister(asus->backlight_device);
2422 asus->backlight_device = NULL;
2425 static int is_display_toggle(int code)
2427 /* display toggle keys */
2428 if ((code >= 0x61 && code <= 0x67) ||
2429 (code >= 0x8c && code <= 0x93) ||
2430 (code >= 0xa0 && code <= 0xa7) ||
2431 (code >= 0xd0 && code <= 0xd5))
2437 /* Fn-lock ********************************************************************/
2439 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2443 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2445 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2446 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2449 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2451 int mode = asus->fnlock_locked;
2453 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2456 /* WMI events *****************************************************************/
2458 static int asus_wmi_get_event_code(u32 value)
2460 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2461 union acpi_object *obj;
2465 status = wmi_get_event_data(value, &response);
2466 if (ACPI_FAILURE(status)) {
2467 pr_warn("Failed to get WMI notify code: %s\n",
2468 acpi_format_exception(status));
2472 obj = (union acpi_object *)response.pointer;
2474 if (obj && obj->type == ACPI_TYPE_INTEGER)
2475 code = (int)(obj->integer.value & WMI_EVENT_MASK);
2483 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2485 unsigned int key_value = 1;
2486 bool autorelease = 1;
2487 int result, orig_code;
2491 if (asus->driver->key_filter) {
2492 asus->driver->key_filter(asus->driver, &code, &key_value,
2494 if (code == ASUS_WMI_KEY_IGNORE)
2498 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2499 code = ASUS_WMI_BRN_UP;
2500 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2501 code = ASUS_WMI_BRN_DOWN;
2503 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
2504 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2505 asus_wmi_backlight_notify(asus, orig_code);
2510 if (code == NOTIFY_KBD_BRTUP) {
2511 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2514 if (code == NOTIFY_KBD_BRTDWN) {
2515 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
2518 if (code == NOTIFY_KBD_BRTTOGGLE) {
2519 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
2520 kbd_led_set_by_kbd(asus, 0);
2522 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2526 if (code == NOTIFY_FNLOCK_TOGGLE) {
2527 asus->fnlock_locked = !asus->fnlock_locked;
2528 asus_wmi_fnlock_update(asus);
2532 if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
2533 result = asus_wmi_get_devstate_simple(asus,
2534 ASUS_WMI_DEVID_KBD_DOCK);
2536 input_report_switch(asus->inputdev, SW_TABLET_MODE,
2538 input_sync(asus->inputdev);
2543 if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
2544 lid_flip_tablet_mode_get_state(asus);
2548 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2549 fan_boost_mode_switch_next(asus);
2553 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2554 throttle_thermal_policy_switch_next(asus);
2558 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2561 if (!sparse_keymap_report_event(asus->inputdev, code,
2562 key_value, autorelease))
2563 pr_info("Unknown key %x pressed\n", code);
2566 static void asus_wmi_notify(u32 value, void *context)
2568 struct asus_wmi *asus = context;
2572 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2573 code = asus_wmi_get_event_code(value);
2575 pr_warn("Failed to get notify code: %d\n", code);
2579 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2582 asus_wmi_handle_event_code(code, asus);
2585 * Double check that queue is present:
2586 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2588 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2592 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2595 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2600 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2601 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2603 pr_warn("Failed to get event during flush: %d\n", code);
2607 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2611 pr_warn("Failed to flush event queue\n");
2615 /* Sysfs **********************************************************************/
2617 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2618 const char *buf, size_t count)
2623 value = asus_wmi_get_devstate_simple(asus, devid);
2627 err = kstrtoint(buf, 0, &value);
2631 err = asus_wmi_set_devstate(devid, value, &retval);
2638 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2640 int value = asus_wmi_get_devstate_simple(asus, devid);
2645 return sprintf(buf, "%d\n", value);
2648 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
2649 static ssize_t show_##_name(struct device *dev, \
2650 struct device_attribute *attr, \
2653 struct asus_wmi *asus = dev_get_drvdata(dev); \
2655 return show_sys_wmi(asus, _cm, buf); \
2657 static ssize_t store_##_name(struct device *dev, \
2658 struct device_attribute *attr, \
2659 const char *buf, size_t count) \
2661 struct asus_wmi *asus = dev_get_drvdata(dev); \
2663 return store_sys_wmi(asus, _cm, buf, count); \
2665 static struct device_attribute dev_attr_##_name = { \
2667 .name = __stringify(_name), \
2669 .show = show_##_name, \
2670 .store = store_##_name, \
2673 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2674 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2675 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2676 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2677 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2679 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2680 const char *buf, size_t count)
2684 rv = kstrtoint(buf, 0, &value);
2688 if (value < 0 || value > 2)
2691 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2698 static DEVICE_ATTR_WO(cpufv);
2700 static struct attribute *platform_attributes[] = {
2701 &dev_attr_cpufv.attr,
2702 &dev_attr_camera.attr,
2703 &dev_attr_cardr.attr,
2704 &dev_attr_touchpad.attr,
2705 &dev_attr_egpu_enable.attr,
2706 &dev_attr_dgpu_disable.attr,
2707 &dev_attr_lid_resume.attr,
2708 &dev_attr_als_enable.attr,
2709 &dev_attr_fan_boost_mode.attr,
2710 &dev_attr_throttle_thermal_policy.attr,
2711 &dev_attr_panel_od.attr,
2715 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2716 struct attribute *attr, int idx)
2718 struct device *dev = container_of(kobj, struct device, kobj);
2719 struct asus_wmi *asus = dev_get_drvdata(dev);
2723 if (attr == &dev_attr_camera.attr)
2724 devid = ASUS_WMI_DEVID_CAMERA;
2725 else if (attr == &dev_attr_cardr.attr)
2726 devid = ASUS_WMI_DEVID_CARDREADER;
2727 else if (attr == &dev_attr_touchpad.attr)
2728 devid = ASUS_WMI_DEVID_TOUCHPAD;
2729 else if (attr == &dev_attr_lid_resume.attr)
2730 devid = ASUS_WMI_DEVID_LID_RESUME;
2731 else if (attr == &dev_attr_als_enable.attr)
2732 devid = ASUS_WMI_DEVID_ALS_ENABLE;
2733 else if (attr == &dev_attr_egpu_enable.attr)
2734 ok = asus->egpu_enable_available;
2735 else if (attr == &dev_attr_dgpu_disable.attr)
2736 ok = asus->dgpu_disable_available;
2737 else if (attr == &dev_attr_fan_boost_mode.attr)
2738 ok = asus->fan_boost_mode_available;
2739 else if (attr == &dev_attr_throttle_thermal_policy.attr)
2740 ok = asus->throttle_thermal_policy_available;
2741 else if (attr == &dev_attr_panel_od.attr)
2742 ok = asus->panel_overdrive_available;
2745 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2747 return ok ? attr->mode : 0;
2750 static const struct attribute_group platform_attribute_group = {
2751 .is_visible = asus_sysfs_is_visible,
2752 .attrs = platform_attributes
2755 static void asus_wmi_sysfs_exit(struct platform_device *device)
2757 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2760 static int asus_wmi_sysfs_init(struct platform_device *device)
2762 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2765 /* Platform device ************************************************************/
2767 static int asus_wmi_platform_init(struct asus_wmi *asus)
2769 struct device *dev = &asus->platform_device->dev;
2773 /* INIT enable hotkeys on some models */
2774 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2775 pr_info("Initialization: %#x\n", rv);
2777 /* We don't know yet what to do with this version... */
2778 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2779 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2784 * The SFUN method probably allows the original driver to get the list
2785 * of features supported by a given model. For now, 0x0100 or 0x0800
2786 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2787 * The significance of others is yet to be found.
2789 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2790 pr_info("SFUN value: %#x\n", rv);
2795 * Eee PC and Notebooks seems to have different method_id for DSTS,
2796 * but it may also be related to the BIOS's SPEC.
2797 * Note, on most Eeepc, there is no way to check if a method exist
2798 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2799 * but once again, SPEC may probably be used for that kind of things.
2801 * Additionally at least TUF Gaming series laptops return nothing for
2802 * unknown methods, so the detection in this way is not possible.
2804 * There is strong indication that only ACPI WMI devices that have _UID
2805 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2807 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2811 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2812 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2813 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2815 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2816 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2820 * Some devices can have multiple event codes stored in a queue before
2821 * the module load if it was unloaded intermittently after calling
2822 * the INIT method (enables event handling). The WMI notify handler is
2823 * expected to retrieve all event codes until a retrieved code equals
2824 * queue end marker (One or Ones). Old codes are flushed from the queue
2825 * upon module load. Not enabling this when it should be has minimal
2826 * visible impact so fall back if anything goes wrong.
2828 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2829 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2830 dev_info(dev, "Detected ATK, enable event queue\n");
2832 if (!asus_wmi_notify_queue_flush(asus))
2833 asus->wmi_event_queue = true;
2836 /* CWAP allow to define the behavior of the Fn+F2 key,
2837 * this method doesn't seems to be present on Eee PCs */
2838 if (asus->driver->quirks->wapf >= 0)
2839 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2840 asus->driver->quirks->wapf, NULL);
2845 /* debugfs ********************************************************************/
2847 struct asus_wmi_debugfs_node {
2848 struct asus_wmi *asus;
2850 int (*show) (struct seq_file *m, void *data);
2853 static int show_dsts(struct seq_file *m, void *data)
2855 struct asus_wmi *asus = m->private;
2859 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2863 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2868 static int show_devs(struct seq_file *m, void *data)
2870 struct asus_wmi *asus = m->private;
2874 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2879 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2880 asus->debug.ctrl_param, retval);
2885 static int show_call(struct seq_file *m, void *data)
2887 struct asus_wmi *asus = m->private;
2888 struct bios_args args = {
2889 .arg0 = asus->debug.dev_id,
2890 .arg1 = asus->debug.ctrl_param,
2892 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2893 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2894 union acpi_object *obj;
2897 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2898 0, asus->debug.method_id,
2901 if (ACPI_FAILURE(status))
2904 obj = (union acpi_object *)output.pointer;
2905 if (obj && obj->type == ACPI_TYPE_INTEGER)
2906 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2907 asus->debug.dev_id, asus->debug.ctrl_param,
2908 (u32) obj->integer.value);
2910 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2911 asus->debug.dev_id, asus->debug.ctrl_param,
2912 obj ? obj->type : -1);
2919 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2920 {NULL, "devs", show_devs},
2921 {NULL, "dsts", show_dsts},
2922 {NULL, "call", show_call},
2925 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2927 struct asus_wmi_debugfs_node *node = inode->i_private;
2929 return single_open(file, node->show, node->asus);
2932 static const struct file_operations asus_wmi_debugfs_io_ops = {
2933 .owner = THIS_MODULE,
2934 .open = asus_wmi_debugfs_open,
2936 .llseek = seq_lseek,
2937 .release = single_release,
2940 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2942 debugfs_remove_recursive(asus->debug.root);
2945 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2949 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2951 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2952 &asus->debug.method_id);
2954 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2955 &asus->debug.dev_id);
2957 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2958 &asus->debug.ctrl_param);
2960 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2961 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2964 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2965 asus->debug.root, node,
2966 &asus_wmi_debugfs_io_ops);
2970 /* Init / exit ****************************************************************/
2972 static int asus_wmi_add(struct platform_device *pdev)
2974 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2975 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2976 struct asus_wmi *asus;
2977 const char *chassis_type;
2982 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2986 asus->driver = wdrv;
2987 asus->platform_device = pdev;
2988 wdrv->platform_device = pdev;
2989 platform_set_drvdata(asus->platform_device, asus);
2991 if (wdrv->detect_quirks)
2992 wdrv->detect_quirks(asus->driver);
2994 err = asus_wmi_platform_init(asus);
2998 err = egpu_enable_check_present(asus);
3000 goto fail_egpu_enable;
3002 err = dgpu_disable_check_present(asus);
3004 goto fail_dgpu_disable;
3006 err = fan_boost_mode_check_present(asus);
3008 goto fail_fan_boost_mode;
3010 err = throttle_thermal_policy_check_present(asus);
3012 goto fail_throttle_thermal_policy;
3014 throttle_thermal_policy_set_default(asus);
3016 err = platform_profile_setup(asus);
3018 goto fail_platform_profile_setup;
3020 err = panel_od_check_present(asus);
3024 err = asus_wmi_sysfs_init(asus->platform_device);
3028 err = asus_wmi_input_init(asus);
3032 err = asus_wmi_fan_init(asus); /* probably no problems on error */
3034 err = asus_wmi_hwmon_init(asus);
3038 err = asus_wmi_led_init(asus);
3042 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3043 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3044 asus->driver->wlan_ctrl_by_user = 1;
3046 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3047 err = asus_wmi_rfkill_init(asus);
3052 if (asus->driver->quirks->wmi_force_als_set)
3055 /* Some Asus desktop boards export an acpi-video backlight interface,
3056 stop this from showing up */
3057 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3058 if (chassis_type && !strcmp(chassis_type, "3"))
3059 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3061 if (asus->driver->quirks->wmi_backlight_power)
3062 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3064 if (asus->driver->quirks->wmi_backlight_native)
3065 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3067 if (asus->driver->quirks->xusb2pr)
3068 asus_wmi_set_xusb2pr(asus);
3070 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3071 err = asus_wmi_backlight_init(asus);
3072 if (err && err != -ENODEV)
3073 goto fail_backlight;
3074 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3075 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3077 if (asus_wmi_has_fnlock_key(asus)) {
3078 asus->fnlock_locked = fnlock_default;
3079 asus_wmi_fnlock_update(asus);
3082 status = wmi_install_notify_handler(asus->driver->event_guid,
3083 asus_wmi_notify, asus);
3084 if (ACPI_FAILURE(status)) {
3085 pr_err("Unable to register notify handler - %d\n", status);
3087 goto fail_wmi_handler;
3090 asus_wmi_battery_init(asus);
3092 asus_wmi_debugfs_init(asus);
3097 asus_wmi_backlight_exit(asus);
3099 asus_wmi_rfkill_exit(asus);
3101 asus_wmi_led_exit(asus);
3104 asus_wmi_input_exit(asus);
3106 asus_wmi_sysfs_exit(asus->platform_device);
3108 fail_throttle_thermal_policy:
3109 fail_platform_profile_setup:
3110 if (asus->platform_profile_support)
3111 platform_profile_remove();
3112 fail_fan_boost_mode:
3121 static int asus_wmi_remove(struct platform_device *device)
3123 struct asus_wmi *asus;
3125 asus = platform_get_drvdata(device);
3126 wmi_remove_notify_handler(asus->driver->event_guid);
3127 asus_wmi_backlight_exit(asus);
3128 asus_wmi_input_exit(asus);
3129 asus_wmi_led_exit(asus);
3130 asus_wmi_rfkill_exit(asus);
3131 asus_wmi_debugfs_exit(asus);
3132 asus_wmi_sysfs_exit(asus->platform_device);
3133 asus_fan_set_auto(asus);
3134 asus_wmi_battery_exit(asus);
3136 if (asus->platform_profile_support)
3137 platform_profile_remove();
3143 /* Platform driver - hibernate/resume callbacks *******************************/
3145 static int asus_hotk_thaw(struct device *device)
3147 struct asus_wmi *asus = dev_get_drvdata(device);
3149 if (asus->wlan.rfkill) {
3153 * Work around bios bug - acpi _PTS turns off the wireless led
3154 * during suspend. Normally it restores it on resume, but
3155 * we should kick it ourselves in case hibernation is aborted.
3157 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3158 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3164 static int asus_hotk_resume(struct device *device)
3166 struct asus_wmi *asus = dev_get_drvdata(device);
3168 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3169 kbd_led_update(asus);
3171 if (asus_wmi_has_fnlock_key(asus))
3172 asus_wmi_fnlock_update(asus);
3174 if (asus->driver->quirks->use_lid_flip_devid)
3175 lid_flip_tablet_mode_get_state(asus);
3180 static int asus_hotk_restore(struct device *device)
3182 struct asus_wmi *asus = dev_get_drvdata(device);
3185 /* Refresh both wlan rfkill state and pci hotplug */
3186 if (asus->wlan.rfkill)
3187 asus_rfkill_hotplug(asus);
3189 if (asus->bluetooth.rfkill) {
3190 bl = !asus_wmi_get_devstate_simple(asus,
3191 ASUS_WMI_DEVID_BLUETOOTH);
3192 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3194 if (asus->wimax.rfkill) {
3195 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3196 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3198 if (asus->wwan3g.rfkill) {
3199 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3200 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3202 if (asus->gps.rfkill) {
3203 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3204 rfkill_set_sw_state(asus->gps.rfkill, bl);
3206 if (asus->uwb.rfkill) {
3207 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3208 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3210 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3211 kbd_led_update(asus);
3213 if (asus_wmi_has_fnlock_key(asus))
3214 asus_wmi_fnlock_update(asus);
3216 if (asus->driver->quirks->use_lid_flip_devid)
3217 lid_flip_tablet_mode_get_state(asus);
3222 static const struct dev_pm_ops asus_pm_ops = {
3223 .thaw = asus_hotk_thaw,
3224 .restore = asus_hotk_restore,
3225 .resume = asus_hotk_resume,
3228 /* Registration ***************************************************************/
3230 static int asus_wmi_probe(struct platform_device *pdev)
3232 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3233 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3236 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3237 pr_warn("ASUS Management GUID not found\n");
3241 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3242 pr_warn("ASUS Event GUID not found\n");
3247 ret = wdrv->probe(pdev);
3252 return asus_wmi_add(pdev);
3257 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3259 struct platform_driver *platform_driver;
3260 struct platform_device *platform_device;
3265 platform_driver = &driver->platform_driver;
3266 platform_driver->remove = asus_wmi_remove;
3267 platform_driver->driver.owner = driver->owner;
3268 platform_driver->driver.name = driver->name;
3269 platform_driver->driver.pm = &asus_pm_ops;
3271 platform_device = platform_create_bundle(platform_driver,
3274 if (IS_ERR(platform_device))
3275 return PTR_ERR(platform_device);
3280 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3282 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3284 platform_device_unregister(driver->platform_device);
3285 platform_driver_unregister(&driver->platform_driver);
3288 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3290 static int __init asus_wmi_init(void)
3292 pr_info("ASUS WMI generic driver loaded\n");
3296 static void __exit asus_wmi_exit(void)
3298 pr_info("ASUS WMI generic driver unloaded\n");
3301 module_init(asus_wmi_init);
3302 module_exit(asus_wmi_exit);