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/power_supply.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/platform_data/x86/asus-wmi.h>
35 #include <linux/platform_device.h>
36 #include <linux/acpi.h>
37 #include <linux/dmi.h>
38 #include <linux/units.h>
40 #include <acpi/battery.h>
41 #include <acpi/video.h>
45 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
46 "Yong Wang <yong.y.wang@intel.com>");
47 MODULE_DESCRIPTION("Asus Generic WMI Driver");
48 MODULE_LICENSE("GPL");
50 #define to_asus_wmi_driver(pdrv) \
51 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
53 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
55 #define NOTIFY_BRNUP_MIN 0x11
56 #define NOTIFY_BRNUP_MAX 0x1f
57 #define NOTIFY_BRNDOWN_MIN 0x20
58 #define NOTIFY_BRNDOWN_MAX 0x2e
59 #define NOTIFY_FNLOCK_TOGGLE 0x4e
60 #define NOTIFY_KBD_DOCK_CHANGE 0x75
61 #define NOTIFY_KBD_BRTUP 0xc4
62 #define NOTIFY_KBD_BRTDWN 0xc5
63 #define NOTIFY_KBD_BRTTOGGLE 0xc7
64 #define NOTIFY_KBD_FBM 0x99
65 #define NOTIFY_KBD_TTP 0xae
66 #define NOTIFY_LID_FLIP 0xfa
67 #define NOTIFY_LID_FLIP_ROG 0xbd
69 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
71 #define ASUS_FAN_DESC "cpu_fan"
72 #define ASUS_FAN_MFUN 0x13
73 #define ASUS_FAN_SFUN_READ 0x06
74 #define ASUS_FAN_SFUN_WRITE 0x07
76 /* Based on standard hwmon pwmX_enable values */
77 #define ASUS_FAN_CTRL_FULLSPEED 0
78 #define ASUS_FAN_CTRL_MANUAL 1
79 #define ASUS_FAN_CTRL_AUTO 2
81 #define ASUS_FAN_BOOST_MODE_NORMAL 0
82 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
83 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
84 #define ASUS_FAN_BOOST_MODE_SILENT 2
85 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
86 #define ASUS_FAN_BOOST_MODES_MASK 0x03
88 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
89 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
90 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
92 #define USB_INTEL_XUSB2PR 0xD0
93 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
95 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
96 #define ASUS_ACPI_UID_ATK "ATK"
98 #define WMI_EVENT_QUEUE_SIZE 0x10
99 #define WMI_EVENT_QUEUE_END 0x1
100 #define WMI_EVENT_MASK 0xFFFF
101 /* The WMI hotkey event value is always the same. */
102 #define WMI_EVENT_VALUE_ATK 0xFF
104 #define WMI_EVENT_MASK 0xFFFF
106 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
108 static bool ashs_present(void)
111 while (ashs_ids[i]) {
112 if (acpi_dev_found(ashs_ids[i++]))
121 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
127 * Struct that's used for all methods called via AGFN. Naming is
128 * identically to the AML code.
131 u16 mfun; /* probably "Multi-function" to be called */
132 u16 sfun; /* probably "Sub-function" to be called */
133 u16 len; /* size of the hole struct, including subfunction fields */
134 u8 stas; /* not used by now */
135 u8 err; /* zero on success */
138 /* struct used for calling fan read and write methods */
139 struct agfn_fan_args {
140 struct agfn_args agfn; /* common fields */
141 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
142 u32 speed; /* read: RPM/100 - write: 0-255 */
146 * <platform>/ - debugfs root directory
147 * dev_id - current dev_id
148 * ctrl_param - current ctrl_param
149 * method_id - current method_id
150 * devs - call DEVS(dev_id, ctrl_param) and print result
151 * dsts - call DSTS(dev_id) and print result
152 * call - call method_id(dev_id, ctrl_param) and print result
154 struct asus_wmi_debug {
162 struct asus_wmi *asus;
163 struct rfkill *rfkill;
169 FAN_TYPE_AGFN, /* deprecated on newer platforms */
170 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
177 bool wmi_event_queue;
179 struct input_dev *inputdev;
180 struct backlight_device *backlight_device;
181 struct platform_device *platform_device;
183 struct led_classdev wlan_led;
185 struct led_classdev tpd_led;
187 struct led_classdev kbd_led;
189 struct led_classdev lightbar_led;
191 struct workqueue_struct *led_workqueue;
192 struct work_struct tpd_led_work;
193 struct work_struct wlan_led_work;
194 struct work_struct lightbar_led_work;
196 struct asus_rfkill wlan;
197 struct asus_rfkill bluetooth;
198 struct asus_rfkill wimax;
199 struct asus_rfkill wwan3g;
200 struct asus_rfkill gps;
201 struct asus_rfkill uwb;
203 int tablet_switch_event_code;
204 u32 tablet_switch_dev_id;
205 bool tablet_switch_inverted;
207 enum fan_type fan_type;
211 bool fan_boost_mode_available;
212 u8 fan_boost_mode_mask;
215 bool dgpu_disable_available;
218 bool throttle_thermal_policy_available;
219 u8 throttle_thermal_policy_mode;
221 // The RSOC controls the maximum charging percentage.
222 bool battery_rsoc_available;
224 struct hotplug_slot hotplug_slot;
225 struct mutex hotplug_lock;
226 struct mutex wmi_lock;
227 struct workqueue_struct *hotplug_workqueue;
228 struct work_struct hotplug_work;
232 struct asus_wmi_debug debug;
234 struct asus_wmi_driver *driver;
237 /* WMI ************************************************************************/
239 static int asus_wmi_evaluate_method3(u32 method_id,
240 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
242 struct bios_args args = {
247 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
248 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
250 union acpi_object *obj;
253 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
256 if (ACPI_FAILURE(status))
259 obj = (union acpi_object *)output.pointer;
260 if (obj && obj->type == ACPI_TYPE_INTEGER)
261 tmp = (u32) obj->integer.value;
268 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
274 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
276 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
278 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
280 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
282 struct acpi_buffer input;
288 * Copy to dma capable address otherwise memory corruption occurs as
289 * bios has to be able to access it.
291 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
292 input.length = args.length;
295 phys_addr = virt_to_phys(input.pointer);
297 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
298 phys_addr, 0, &retval);
300 memcpy(args.pointer, input.pointer, args.length);
302 kfree(input.pointer);
309 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
311 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
314 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
317 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
321 /* Helper for special devices with magic return codes */
322 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
323 u32 dev_id, u32 mask)
328 err = asus_wmi_get_devstate(asus, dev_id, &retval);
332 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
335 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
336 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
340 return retval & mask;
343 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
345 return asus_wmi_get_devstate_bits(asus, dev_id,
346 ASUS_WMI_DSTS_STATUS_BIT);
349 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
352 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
354 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
357 /* Input **********************************************************************/
358 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
360 input_report_switch(asus->inputdev, SW_TABLET_MODE,
361 asus->tablet_switch_inverted ? !value : value);
362 input_sync(asus->inputdev);
365 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
367 struct device *dev = &asus->platform_device->dev;
370 result = asus_wmi_get_devstate_simple(asus, dev_id);
372 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
373 asus_wmi_tablet_sw_report(asus, result);
374 asus->tablet_switch_dev_id = dev_id;
375 asus->tablet_switch_event_code = event_code;
376 } else if (result == -ENODEV) {
377 dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
379 dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
383 static int asus_wmi_input_init(struct asus_wmi *asus)
385 struct device *dev = &asus->platform_device->dev;
388 asus->inputdev = input_allocate_device();
392 asus->inputdev->name = asus->driver->input_name;
393 asus->inputdev->phys = asus->driver->input_phys;
394 asus->inputdev->id.bustype = BUS_HOST;
395 asus->inputdev->dev.parent = dev;
396 set_bit(EV_REP, asus->inputdev->evbit);
398 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
402 switch (asus->driver->quirks->tablet_switch_mode) {
403 case asus_wmi_no_tablet_switch:
405 case asus_wmi_kbd_dock_devid:
406 asus->tablet_switch_inverted = true;
407 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
409 case asus_wmi_lid_flip_devid:
410 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
412 case asus_wmi_lid_flip_rog_devid:
413 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
417 err = input_register_device(asus->inputdev);
424 input_free_device(asus->inputdev);
428 static void asus_wmi_input_exit(struct asus_wmi *asus)
431 input_unregister_device(asus->inputdev);
433 asus->inputdev = NULL;
436 /* Tablet mode ****************************************************************/
438 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
442 if (!asus->tablet_switch_dev_id)
445 result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
447 asus_wmi_tablet_sw_report(asus, result);
450 /* dGPU ********************************************************************/
451 static int dgpu_disable_check_present(struct asus_wmi *asus)
456 asus->dgpu_disable_available = false;
458 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_DGPU, &result);
465 if (result & ASUS_WMI_DSTS_PRESENCE_BIT) {
466 asus->dgpu_disable_available = true;
467 asus->dgpu_disable = result & ASUS_WMI_DSTS_STATUS_BIT;
473 static int dgpu_disable_write(struct asus_wmi *asus)
479 /* Don't rely on type conversion */
480 value = asus->dgpu_disable ? 1 : 0;
482 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, value, &retval);
484 pr_warn("Failed to set dgpu disable: %d\n", err);
488 if (retval > 1 || retval < 0) {
489 pr_warn("Failed to set dgpu disable (retval): 0x%x\n", retval);
493 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
498 static ssize_t dgpu_disable_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct asus_wmi *asus = dev_get_drvdata(dev);
502 u8 mode = asus->dgpu_disable;
504 return sysfs_emit(buf, "%d\n", mode);
508 * A user may be required to store the value twice, typcial store first, then
509 * rescan PCI bus to activate power, then store a second time to save correctly.
510 * The reason for this is that an extra code path in the ACPI is enabled when
511 * the device and bus are powered.
513 static ssize_t dgpu_disable_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
520 struct asus_wmi *asus = dev_get_drvdata(dev);
522 result = kstrtobool(buf, &disable);
526 asus->dgpu_disable = disable;
528 result = dgpu_disable_write(asus);
535 static DEVICE_ATTR_RW(dgpu_disable);
537 /* Battery ********************************************************************/
539 /* The battery maximum charging percentage */
540 static int charge_end_threshold;
542 static ssize_t charge_control_end_threshold_store(struct device *dev,
543 struct device_attribute *attr,
544 const char *buf, size_t count)
548 ret = kstrtouint(buf, 10, &value);
552 if (value < 0 || value > 100)
555 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
562 /* There isn't any method in the DSDT to read the threshold, so we
563 * save the threshold.
565 charge_end_threshold = value;
569 static ssize_t charge_control_end_threshold_show(struct device *device,
570 struct device_attribute *attr,
573 return sprintf(buf, "%d\n", charge_end_threshold);
576 static DEVICE_ATTR_RW(charge_control_end_threshold);
578 static int asus_wmi_battery_add(struct power_supply *battery)
580 /* The WMI method does not provide a way to specific a battery, so we
581 * just assume it is the first battery.
582 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
583 * battery is named BATT.
585 if (strcmp(battery->desc->name, "BAT0") != 0 &&
586 strcmp(battery->desc->name, "BAT1") != 0 &&
587 strcmp(battery->desc->name, "BATC") != 0 &&
588 strcmp(battery->desc->name, "BATT") != 0)
591 if (device_create_file(&battery->dev,
592 &dev_attr_charge_control_end_threshold))
595 /* The charge threshold is only reset when the system is power cycled,
596 * and we can't get the current threshold so let set it to 100% when
597 * a battery is added.
599 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
600 charge_end_threshold = 100;
605 static int asus_wmi_battery_remove(struct power_supply *battery)
607 device_remove_file(&battery->dev,
608 &dev_attr_charge_control_end_threshold);
612 static struct acpi_battery_hook battery_hook = {
613 .add_battery = asus_wmi_battery_add,
614 .remove_battery = asus_wmi_battery_remove,
615 .name = "ASUS Battery Extension",
618 static void asus_wmi_battery_init(struct asus_wmi *asus)
620 asus->battery_rsoc_available = false;
621 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
622 asus->battery_rsoc_available = true;
623 battery_hook_register(&battery_hook);
627 static void asus_wmi_battery_exit(struct asus_wmi *asus)
629 if (asus->battery_rsoc_available)
630 battery_hook_unregister(&battery_hook);
633 /* LEDs ***********************************************************************/
636 * These functions actually update the LED's, and are called from a
637 * workqueue. By doing this as separate work rather than when the LED
638 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
639 * potentially bad time, such as a timer interrupt.
641 static void tpd_led_update(struct work_struct *work)
644 struct asus_wmi *asus;
646 asus = container_of(work, struct asus_wmi, tpd_led_work);
648 ctrl_param = asus->tpd_led_wk;
649 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
652 static void tpd_led_set(struct led_classdev *led_cdev,
653 enum led_brightness value)
655 struct asus_wmi *asus;
657 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
659 asus->tpd_led_wk = !!value;
660 queue_work(asus->led_workqueue, &asus->tpd_led_work);
663 static int read_tpd_led_state(struct asus_wmi *asus)
665 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
668 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
670 struct asus_wmi *asus;
672 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
674 return read_tpd_led_state(asus);
677 static void kbd_led_update(struct asus_wmi *asus)
681 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
682 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
685 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
691 * bit 7: light on/off
692 * bit 8-10: environment (0: dark, 1: normal, 2: light)
693 * bit 17: status unknown
695 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
698 /* Unknown status is considered as off */
699 if (retval == 0x8000)
706 *level = retval & 0x7F;
708 *env = (retval >> 8) & 0x7F;
712 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
714 struct asus_wmi *asus;
717 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
718 max_level = asus->kbd_led.max_brightness;
720 asus->kbd_led_wk = clamp_val(value, 0, max_level);
721 kbd_led_update(asus);
724 static void kbd_led_set(struct led_classdev *led_cdev,
725 enum led_brightness value)
727 /* Prevent disabling keyboard backlight on module unregister */
728 if (led_cdev->flags & LED_UNREGISTERING)
731 do_kbd_led_set(led_cdev, value);
734 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
736 struct led_classdev *led_cdev = &asus->kbd_led;
738 do_kbd_led_set(led_cdev, value);
739 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
742 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
744 struct asus_wmi *asus;
747 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
749 retval = kbd_led_read(asus, &value, NULL);
756 static int wlan_led_unknown_state(struct asus_wmi *asus)
760 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
762 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
765 static void wlan_led_update(struct work_struct *work)
768 struct asus_wmi *asus;
770 asus = container_of(work, struct asus_wmi, wlan_led_work);
772 ctrl_param = asus->wlan_led_wk;
773 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
776 static void wlan_led_set(struct led_classdev *led_cdev,
777 enum led_brightness value)
779 struct asus_wmi *asus;
781 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
783 asus->wlan_led_wk = !!value;
784 queue_work(asus->led_workqueue, &asus->wlan_led_work);
787 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
789 struct asus_wmi *asus;
792 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
793 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
795 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
798 static void lightbar_led_update(struct work_struct *work)
800 struct asus_wmi *asus;
803 asus = container_of(work, struct asus_wmi, lightbar_led_work);
805 ctrl_param = asus->lightbar_led_wk;
806 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
809 static void lightbar_led_set(struct led_classdev *led_cdev,
810 enum led_brightness value)
812 struct asus_wmi *asus;
814 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
816 asus->lightbar_led_wk = !!value;
817 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
820 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
822 struct asus_wmi *asus;
825 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
826 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
828 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
831 static void asus_wmi_led_exit(struct asus_wmi *asus)
833 led_classdev_unregister(&asus->kbd_led);
834 led_classdev_unregister(&asus->tpd_led);
835 led_classdev_unregister(&asus->wlan_led);
836 led_classdev_unregister(&asus->lightbar_led);
838 if (asus->led_workqueue)
839 destroy_workqueue(asus->led_workqueue);
842 static int asus_wmi_led_init(struct asus_wmi *asus)
846 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
847 if (!asus->led_workqueue)
850 if (read_tpd_led_state(asus) >= 0) {
851 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
853 asus->tpd_led.name = "asus::touchpad";
854 asus->tpd_led.brightness_set = tpd_led_set;
855 asus->tpd_led.brightness_get = tpd_led_get;
856 asus->tpd_led.max_brightness = 1;
858 rv = led_classdev_register(&asus->platform_device->dev,
864 if (!kbd_led_read(asus, &led_val, NULL)) {
865 asus->kbd_led_wk = led_val;
866 asus->kbd_led.name = "asus::kbd_backlight";
867 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
868 asus->kbd_led.brightness_set = kbd_led_set;
869 asus->kbd_led.brightness_get = kbd_led_get;
870 asus->kbd_led.max_brightness = 3;
872 rv = led_classdev_register(&asus->platform_device->dev,
878 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
879 && (asus->driver->quirks->wapf > 0)) {
880 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
882 asus->wlan_led.name = "asus::wlan";
883 asus->wlan_led.brightness_set = wlan_led_set;
884 if (!wlan_led_unknown_state(asus))
885 asus->wlan_led.brightness_get = wlan_led_get;
886 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
887 asus->wlan_led.max_brightness = 1;
888 asus->wlan_led.default_trigger = "asus-wlan";
890 rv = led_classdev_register(&asus->platform_device->dev,
896 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
897 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
899 asus->lightbar_led.name = "asus::lightbar";
900 asus->lightbar_led.brightness_set = lightbar_led_set;
901 asus->lightbar_led.brightness_get = lightbar_led_get;
902 asus->lightbar_led.max_brightness = 1;
904 rv = led_classdev_register(&asus->platform_device->dev,
905 &asus->lightbar_led);
910 asus_wmi_led_exit(asus);
915 /* RF *************************************************************************/
918 * PCI hotplug (for wlan rfkill)
920 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
922 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
929 static void asus_rfkill_hotplug(struct asus_wmi *asus)
937 mutex_lock(&asus->wmi_lock);
938 blocked = asus_wlan_rfkill_blocked(asus);
939 mutex_unlock(&asus->wmi_lock);
941 mutex_lock(&asus->hotplug_lock);
942 pci_lock_rescan_remove();
944 if (asus->wlan.rfkill)
945 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
947 if (asus->hotplug_slot.ops) {
948 bus = pci_find_bus(0, 1);
950 pr_warn("Unable to find PCI bus 1?\n");
954 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
955 pr_err("Unable to read PCI config space?\n");
958 absent = (l == 0xffffffff);
960 if (blocked != absent) {
961 pr_warn("BIOS says wireless lan is %s, "
962 "but the pci device is %s\n",
963 blocked ? "blocked" : "unblocked",
964 absent ? "absent" : "present");
965 pr_warn("skipped wireless hotplug as probably "
966 "inappropriate for this model\n");
971 dev = pci_get_slot(bus, 0);
973 /* Device already present */
977 dev = pci_scan_single_device(bus, 0);
979 pci_bus_assign_resources(bus);
980 pci_bus_add_device(dev);
983 dev = pci_get_slot(bus, 0);
985 pci_stop_and_remove_bus_device(dev);
992 pci_unlock_rescan_remove();
993 mutex_unlock(&asus->hotplug_lock);
996 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
998 struct asus_wmi *asus = data;
1000 if (event != ACPI_NOTIFY_BUS_CHECK)
1004 * We can't call directly asus_rfkill_hotplug because most
1005 * of the time WMBC is still being executed and not reetrant.
1006 * There is currently no way to tell ACPICA that we want this
1007 * method to be serialized, we schedule a asus_rfkill_hotplug
1008 * call later, in a safer context.
1010 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1013 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1018 status = acpi_get_handle(NULL, node, &handle);
1019 if (ACPI_FAILURE(status))
1022 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1023 asus_rfkill_notify, asus);
1024 if (ACPI_FAILURE(status))
1025 pr_warn("Failed to register notify on %s\n", node);
1030 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1032 acpi_status status = AE_OK;
1035 status = acpi_get_handle(NULL, node, &handle);
1036 if (ACPI_FAILURE(status))
1039 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1040 asus_rfkill_notify);
1041 if (ACPI_FAILURE(status))
1042 pr_err("Error removing rfkill notify handler %s\n", node);
1045 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1048 struct asus_wmi *asus = container_of(hotplug_slot,
1049 struct asus_wmi, hotplug_slot);
1050 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1059 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1060 .get_adapter_status = asus_get_adapter_status,
1061 .get_power_status = asus_get_adapter_status,
1064 static void asus_hotplug_work(struct work_struct *work)
1066 struct asus_wmi *asus;
1068 asus = container_of(work, struct asus_wmi, hotplug_work);
1069 asus_rfkill_hotplug(asus);
1072 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1075 struct pci_bus *bus = pci_find_bus(0, 1);
1078 pr_err("Unable to find wifi PCI bus\n");
1082 asus->hotplug_workqueue =
1083 create_singlethread_workqueue("hotplug_workqueue");
1084 if (!asus->hotplug_workqueue)
1085 goto error_workqueue;
1087 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1089 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1091 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1093 pr_err("Unable to register hotplug slot - %d\n", ret);
1094 goto error_register;
1100 asus->hotplug_slot.ops = NULL;
1101 destroy_workqueue(asus->hotplug_workqueue);
1109 static int asus_rfkill_set(void *data, bool blocked)
1111 struct asus_rfkill *priv = data;
1112 u32 ctrl_param = !blocked;
1113 u32 dev_id = priv->dev_id;
1116 * If the user bit is set, BIOS can't set and record the wlan status,
1117 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1118 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1119 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1120 * while setting the wlan status through WMI.
1121 * This is also the behavior that windows app will do.
1123 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1124 priv->asus->driver->wlan_ctrl_by_user)
1125 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1127 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1130 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1132 struct asus_rfkill *priv = data;
1135 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1140 rfkill_set_sw_state(priv->rfkill, !result);
1143 static int asus_rfkill_wlan_set(void *data, bool blocked)
1145 struct asus_rfkill *priv = data;
1146 struct asus_wmi *asus = priv->asus;
1150 * This handler is enabled only if hotplug is enabled.
1151 * In this case, the asus_wmi_set_devstate() will
1152 * trigger a wmi notification and we need to wait
1153 * this call to finish before being able to call
1156 mutex_lock(&asus->wmi_lock);
1157 ret = asus_rfkill_set(data, blocked);
1158 mutex_unlock(&asus->wmi_lock);
1162 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1163 .set_block = asus_rfkill_wlan_set,
1164 .query = asus_rfkill_query,
1167 static const struct rfkill_ops asus_rfkill_ops = {
1168 .set_block = asus_rfkill_set,
1169 .query = asus_rfkill_query,
1172 static int asus_new_rfkill(struct asus_wmi *asus,
1173 struct asus_rfkill *arfkill,
1174 const char *name, enum rfkill_type type, int dev_id)
1176 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1177 struct rfkill **rfkill = &arfkill->rfkill;
1182 arfkill->dev_id = dev_id;
1183 arfkill->asus = asus;
1185 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1186 asus->driver->quirks->hotplug_wireless)
1187 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1188 &asus_rfkill_wlan_ops, arfkill);
1190 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1191 &asus_rfkill_ops, arfkill);
1196 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1197 (asus->driver->quirks->wapf > 0))
1198 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1200 rfkill_init_sw_state(*rfkill, !result);
1201 result = rfkill_register(*rfkill);
1203 rfkill_destroy(*rfkill);
1210 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1212 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1215 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1216 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1217 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1218 if (asus->wlan.rfkill) {
1219 rfkill_unregister(asus->wlan.rfkill);
1220 rfkill_destroy(asus->wlan.rfkill);
1221 asus->wlan.rfkill = NULL;
1224 * Refresh pci hotplug in case the rfkill state was changed after
1225 * asus_unregister_rfkill_notifier()
1227 asus_rfkill_hotplug(asus);
1228 if (asus->hotplug_slot.ops)
1229 pci_hp_deregister(&asus->hotplug_slot);
1230 if (asus->hotplug_workqueue)
1231 destroy_workqueue(asus->hotplug_workqueue);
1233 if (asus->bluetooth.rfkill) {
1234 rfkill_unregister(asus->bluetooth.rfkill);
1235 rfkill_destroy(asus->bluetooth.rfkill);
1236 asus->bluetooth.rfkill = NULL;
1238 if (asus->wimax.rfkill) {
1239 rfkill_unregister(asus->wimax.rfkill);
1240 rfkill_destroy(asus->wimax.rfkill);
1241 asus->wimax.rfkill = NULL;
1243 if (asus->wwan3g.rfkill) {
1244 rfkill_unregister(asus->wwan3g.rfkill);
1245 rfkill_destroy(asus->wwan3g.rfkill);
1246 asus->wwan3g.rfkill = NULL;
1248 if (asus->gps.rfkill) {
1249 rfkill_unregister(asus->gps.rfkill);
1250 rfkill_destroy(asus->gps.rfkill);
1251 asus->gps.rfkill = NULL;
1253 if (asus->uwb.rfkill) {
1254 rfkill_unregister(asus->uwb.rfkill);
1255 rfkill_destroy(asus->uwb.rfkill);
1256 asus->uwb.rfkill = NULL;
1260 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1264 mutex_init(&asus->hotplug_lock);
1265 mutex_init(&asus->wmi_lock);
1267 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1268 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1270 if (result && result != -ENODEV)
1273 result = asus_new_rfkill(asus, &asus->bluetooth,
1274 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1275 ASUS_WMI_DEVID_BLUETOOTH);
1277 if (result && result != -ENODEV)
1280 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1281 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1283 if (result && result != -ENODEV)
1286 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1287 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1289 if (result && result != -ENODEV)
1292 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1293 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1295 if (result && result != -ENODEV)
1298 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1299 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1301 if (result && result != -ENODEV)
1304 if (!asus->driver->quirks->hotplug_wireless)
1307 result = asus_setup_pci_hotplug(asus);
1309 * If we get -EBUSY then something else is handling the PCI hotplug -
1310 * don't fail in this case
1312 if (result == -EBUSY)
1315 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1316 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1317 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1319 * Refresh pci hotplug in case the rfkill state was changed during
1322 asus_rfkill_hotplug(asus);
1325 if (result && result != -ENODEV)
1326 asus_wmi_rfkill_exit(asus);
1328 if (result == -ENODEV)
1334 /* Quirks *********************************************************************/
1336 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1338 struct pci_dev *xhci_pdev;
1339 u32 orig_ports_available;
1340 u32 ports_available = asus->driver->quirks->xusb2pr;
1342 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1343 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1349 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1350 &orig_ports_available);
1352 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1353 cpu_to_le32(ports_available));
1355 pci_dev_put(xhci_pdev);
1357 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1358 orig_ports_available, ports_available);
1362 * Some devices dont support or have borcken get_als method
1363 * but still support set method.
1365 static void asus_wmi_set_als(void)
1367 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1370 /* Hwmon device ***************************************************************/
1372 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1375 struct agfn_fan_args args = {
1376 .agfn.len = sizeof(args),
1377 .agfn.mfun = ASUS_FAN_MFUN,
1378 .agfn.sfun = ASUS_FAN_SFUN_READ,
1382 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1388 status = asus_wmi_evaluate_method_agfn(input);
1390 if (status || args.agfn.err)
1394 *speed = args.speed;
1399 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1402 struct agfn_fan_args args = {
1403 .agfn.len = sizeof(args),
1404 .agfn.mfun = ASUS_FAN_MFUN,
1405 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1407 .speed = speed ? *speed : 0,
1409 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1412 /* 1: for setting 1st fan's speed 0: setting auto mode */
1413 if (fan != 1 && fan != 0)
1416 status = asus_wmi_evaluate_method_agfn(input);
1418 if (status || args.agfn.err)
1421 if (speed && fan == 1)
1422 asus->agfn_pwm = *speed;
1428 * Check if we can read the speed of one fan. If true we assume we can also
1431 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1437 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1441 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1446 * We need to find a better way, probably using sfun,
1448 * Currently we disable it if:
1449 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1450 * - reverved bits are non-zero
1451 * - sfun and presence bit are not set
1453 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1454 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1457 static int asus_fan_set_auto(struct asus_wmi *asus)
1462 switch (asus->fan_type) {
1463 case FAN_TYPE_SPEC83:
1464 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1474 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1487 static ssize_t pwm1_show(struct device *dev,
1488 struct device_attribute *attr,
1491 struct asus_wmi *asus = dev_get_drvdata(dev);
1495 /* If we already set a value then just return it */
1496 if (asus->agfn_pwm >= 0)
1497 return sprintf(buf, "%d\n", asus->agfn_pwm);
1500 * If we haven't set already set a value through the AGFN interface,
1501 * we read a current value through the (now-deprecated) FAN_CTRL device.
1503 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1509 if (value == 1) /* Low Speed */
1511 else if (value == 2)
1513 else if (value == 3)
1516 pr_err("Unknown fan speed %#x\n", value);
1520 return sprintf(buf, "%d\n", value);
1523 static ssize_t pwm1_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count) {
1526 struct asus_wmi *asus = dev_get_drvdata(dev);
1531 ret = kstrtouint(buf, 10, &value);
1535 value = clamp(value, 0, 255);
1537 state = asus_agfn_fan_speed_write(asus, 1, &value);
1539 pr_warn("Setting fan speed failed: %d\n", state);
1541 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1546 static ssize_t fan1_input_show(struct device *dev,
1547 struct device_attribute *attr,
1550 struct asus_wmi *asus = dev_get_drvdata(dev);
1554 switch (asus->fan_type) {
1555 case FAN_TYPE_SPEC83:
1556 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1565 /* no speed readable on manual mode */
1566 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1569 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1571 pr_warn("reading fan speed failed: %d\n", ret);
1580 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1583 static ssize_t pwm1_enable_show(struct device *dev,
1584 struct device_attribute *attr,
1587 struct asus_wmi *asus = dev_get_drvdata(dev);
1590 * Just read back the cached pwm mode.
1592 * For the CPU_FAN device, the spec indicates that we should be
1593 * able to read the device status and consult bit 19 to see if we
1594 * are in Full On or Automatic mode. However, this does not work
1595 * in practice on X532FL at least (the bit is always 0) and there's
1596 * also nothing in the DSDT to indicate that this behaviour exists.
1598 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1601 static ssize_t pwm1_enable_store(struct device *dev,
1602 struct device_attribute *attr,
1603 const char *buf, size_t count)
1605 struct asus_wmi *asus = dev_get_drvdata(dev);
1612 ret = kstrtouint(buf, 10, &state);
1616 if (asus->fan_type == FAN_TYPE_SPEC83) {
1617 switch (state) { /* standard documented hwmon values */
1618 case ASUS_FAN_CTRL_FULLSPEED:
1621 case ASUS_FAN_CTRL_AUTO:
1628 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1635 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1637 case ASUS_FAN_CTRL_MANUAL:
1640 case ASUS_FAN_CTRL_AUTO:
1641 status = asus_fan_set_auto(asus);
1651 asus->fan_pwm_mode = state;
1655 static ssize_t fan1_label_show(struct device *dev,
1656 struct device_attribute *attr,
1659 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1662 static ssize_t asus_hwmon_temp1(struct device *dev,
1663 struct device_attribute *attr,
1666 struct asus_wmi *asus = dev_get_drvdata(dev);
1670 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1674 return sprintf(buf, "%ld\n",
1675 deci_kelvin_to_millicelsius(value & 0xFFFF));
1679 static DEVICE_ATTR_RW(pwm1);
1680 static DEVICE_ATTR_RW(pwm1_enable);
1681 static DEVICE_ATTR_RO(fan1_input);
1682 static DEVICE_ATTR_RO(fan1_label);
1685 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1687 static struct attribute *hwmon_attributes[] = {
1688 &dev_attr_pwm1.attr,
1689 &dev_attr_pwm1_enable.attr,
1690 &dev_attr_fan1_input.attr,
1691 &dev_attr_fan1_label.attr,
1693 &dev_attr_temp1_input.attr,
1697 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1698 struct attribute *attr, int idx)
1700 struct device *dev = container_of(kobj, struct device, kobj);
1701 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1702 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1704 if (attr == &dev_attr_pwm1.attr) {
1705 if (asus->fan_type != FAN_TYPE_AGFN)
1707 } else if (attr == &dev_attr_fan1_input.attr
1708 || attr == &dev_attr_fan1_label.attr
1709 || attr == &dev_attr_pwm1_enable.attr) {
1710 if (asus->fan_type == FAN_TYPE_NONE)
1712 } else if (attr == &dev_attr_temp1_input.attr) {
1713 int err = asus_wmi_get_devstate(asus,
1714 ASUS_WMI_DEVID_THERMAL_CTRL,
1718 return 0; /* can't return negative here */
1721 * If the temperature value in deci-Kelvin is near the absolute
1722 * zero temperature, something is clearly wrong
1724 if (value == 0 || value == 1)
1731 static const struct attribute_group hwmon_attribute_group = {
1732 .is_visible = asus_hwmon_sysfs_is_visible,
1733 .attrs = hwmon_attributes
1735 __ATTRIBUTE_GROUPS(hwmon_attribute);
1737 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1739 struct device *dev = &asus->platform_device->dev;
1740 struct device *hwmon;
1742 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1743 hwmon_attribute_groups);
1745 if (IS_ERR(hwmon)) {
1746 pr_err("Could not register asus hwmon device\n");
1747 return PTR_ERR(hwmon);
1752 static int asus_wmi_fan_init(struct asus_wmi *asus)
1754 asus->fan_type = FAN_TYPE_NONE;
1755 asus->agfn_pwm = -1;
1757 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1758 asus->fan_type = FAN_TYPE_SPEC83;
1759 else if (asus_wmi_has_agfn_fan(asus))
1760 asus->fan_type = FAN_TYPE_AGFN;
1762 if (asus->fan_type == FAN_TYPE_NONE)
1765 asus_fan_set_auto(asus);
1766 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1770 /* Fan mode *******************************************************************/
1772 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1777 asus->fan_boost_mode_available = false;
1779 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1788 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1789 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1790 asus->fan_boost_mode_available = true;
1791 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1797 static int fan_boost_mode_write(struct asus_wmi *asus)
1803 value = asus->fan_boost_mode;
1805 pr_info("Set fan boost mode: %u\n", value);
1806 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1809 pr_warn("Failed to set fan boost mode: %d\n", err);
1814 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1822 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1824 u8 mask = asus->fan_boost_mode_mask;
1826 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1827 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1828 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1829 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1830 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1831 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1832 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1833 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1835 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1837 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1840 return fan_boost_mode_write(asus);
1843 static ssize_t fan_boost_mode_show(struct device *dev,
1844 struct device_attribute *attr, char *buf)
1846 struct asus_wmi *asus = dev_get_drvdata(dev);
1848 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
1851 static ssize_t fan_boost_mode_store(struct device *dev,
1852 struct device_attribute *attr,
1853 const char *buf, size_t count)
1857 struct asus_wmi *asus = dev_get_drvdata(dev);
1858 u8 mask = asus->fan_boost_mode_mask;
1860 result = kstrtou8(buf, 10, &new_mode);
1862 pr_warn("Trying to store invalid value\n");
1866 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1867 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
1869 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1870 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
1872 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
1876 asus->fan_boost_mode = new_mode;
1877 fan_boost_mode_write(asus);
1882 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1883 static DEVICE_ATTR_RW(fan_boost_mode);
1885 /* Throttle thermal policy ****************************************************/
1887 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
1892 asus->throttle_thermal_policy_available = false;
1894 err = asus_wmi_get_devstate(asus,
1895 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1903 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
1904 asus->throttle_thermal_policy_available = true;
1909 static int throttle_thermal_policy_write(struct asus_wmi *asus)
1915 value = asus->throttle_thermal_policy_mode;
1917 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1920 pr_warn("Failed to set throttle thermal policy: %d\n", err);
1925 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
1933 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
1935 if (!asus->throttle_thermal_policy_available)
1938 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1939 return throttle_thermal_policy_write(asus);
1942 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
1944 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
1946 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1947 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1949 asus->throttle_thermal_policy_mode = new_mode;
1950 return throttle_thermal_policy_write(asus);
1953 static ssize_t throttle_thermal_policy_show(struct device *dev,
1954 struct device_attribute *attr, char *buf)
1956 struct asus_wmi *asus = dev_get_drvdata(dev);
1957 u8 mode = asus->throttle_thermal_policy_mode;
1959 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
1962 static ssize_t throttle_thermal_policy_store(struct device *dev,
1963 struct device_attribute *attr,
1964 const char *buf, size_t count)
1968 struct asus_wmi *asus = dev_get_drvdata(dev);
1970 result = kstrtou8(buf, 10, &new_mode);
1974 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1977 asus->throttle_thermal_policy_mode = new_mode;
1978 throttle_thermal_policy_write(asus);
1983 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
1984 static DEVICE_ATTR_RW(throttle_thermal_policy);
1986 /* Backlight ******************************************************************/
1988 static int read_backlight_power(struct asus_wmi *asus)
1992 if (asus->driver->quirks->store_backlight_power)
1993 ret = !asus->driver->panel_power;
1995 ret = asus_wmi_get_devstate_simple(asus,
1996 ASUS_WMI_DEVID_BACKLIGHT);
2001 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2004 static int read_brightness_max(struct asus_wmi *asus)
2009 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2013 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2022 static int read_brightness(struct backlight_device *bd)
2024 struct asus_wmi *asus = bl_get_data(bd);
2028 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2032 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2035 static u32 get_scalar_command(struct backlight_device *bd)
2037 struct asus_wmi *asus = bl_get_data(bd);
2040 if ((asus->driver->brightness < bd->props.brightness) ||
2041 bd->props.brightness == bd->props.max_brightness)
2042 ctrl_param = 0x00008001;
2043 else if ((asus->driver->brightness > bd->props.brightness) ||
2044 bd->props.brightness == 0)
2045 ctrl_param = 0x00008000;
2047 asus->driver->brightness = bd->props.brightness;
2052 static int update_bl_status(struct backlight_device *bd)
2054 struct asus_wmi *asus = bl_get_data(bd);
2058 power = read_backlight_power(asus);
2059 if (power != -ENODEV && bd->props.power != power) {
2060 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2061 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2063 if (asus->driver->quirks->store_backlight_power)
2064 asus->driver->panel_power = bd->props.power;
2066 /* When using scalar brightness, updating the brightness
2067 * will mess with the backlight power */
2068 if (asus->driver->quirks->scalar_panel_brightness)
2072 if (asus->driver->quirks->scalar_panel_brightness)
2073 ctrl_param = get_scalar_command(bd);
2075 ctrl_param = bd->props.brightness;
2077 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2083 static const struct backlight_ops asus_wmi_bl_ops = {
2084 .get_brightness = read_brightness,
2085 .update_status = update_bl_status,
2088 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2090 struct backlight_device *bd = asus->backlight_device;
2091 int old = bd->props.brightness;
2094 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2095 new = code - NOTIFY_BRNUP_MIN + 1;
2096 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2097 new = code - NOTIFY_BRNDOWN_MIN;
2099 bd->props.brightness = new;
2100 backlight_update_status(bd);
2101 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2106 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2108 struct backlight_device *bd;
2109 struct backlight_properties props;
2113 max = read_brightness_max(asus);
2117 power = read_backlight_power(asus);
2118 if (power == -ENODEV)
2119 power = FB_BLANK_UNBLANK;
2123 memset(&props, 0, sizeof(struct backlight_properties));
2124 props.type = BACKLIGHT_PLATFORM;
2125 props.max_brightness = max;
2126 bd = backlight_device_register(asus->driver->name,
2127 &asus->platform_device->dev, asus,
2128 &asus_wmi_bl_ops, &props);
2130 pr_err("Could not register backlight device\n");
2134 asus->backlight_device = bd;
2136 if (asus->driver->quirks->store_backlight_power)
2137 asus->driver->panel_power = power;
2139 bd->props.brightness = read_brightness(bd);
2140 bd->props.power = power;
2141 backlight_update_status(bd);
2143 asus->driver->brightness = bd->props.brightness;
2148 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2150 backlight_device_unregister(asus->backlight_device);
2152 asus->backlight_device = NULL;
2155 static int is_display_toggle(int code)
2157 /* display toggle keys */
2158 if ((code >= 0x61 && code <= 0x67) ||
2159 (code >= 0x8c && code <= 0x93) ||
2160 (code >= 0xa0 && code <= 0xa7) ||
2161 (code >= 0xd0 && code <= 0xd5))
2167 /* Fn-lock ********************************************************************/
2169 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2173 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2175 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2176 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2179 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2181 int mode = asus->fnlock_locked;
2183 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2186 /* WMI events *****************************************************************/
2188 static int asus_wmi_get_event_code(u32 value)
2190 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2191 union acpi_object *obj;
2195 status = wmi_get_event_data(value, &response);
2196 if (ACPI_FAILURE(status)) {
2197 pr_warn("Failed to get WMI notify code: %s\n",
2198 acpi_format_exception(status));
2202 obj = (union acpi_object *)response.pointer;
2204 if (obj && obj->type == ACPI_TYPE_INTEGER)
2205 code = (int)(obj->integer.value & WMI_EVENT_MASK);
2213 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2215 unsigned int key_value = 1;
2216 bool autorelease = 1;
2217 int orig_code = code;
2219 if (asus->driver->key_filter) {
2220 asus->driver->key_filter(asus->driver, &code, &key_value,
2222 if (code == ASUS_WMI_KEY_IGNORE)
2226 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2227 code = ASUS_WMI_BRN_UP;
2228 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2229 code = ASUS_WMI_BRN_DOWN;
2231 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
2232 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2233 asus_wmi_backlight_notify(asus, orig_code);
2238 if (code == NOTIFY_KBD_BRTUP) {
2239 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2242 if (code == NOTIFY_KBD_BRTDWN) {
2243 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
2246 if (code == NOTIFY_KBD_BRTTOGGLE) {
2247 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
2248 kbd_led_set_by_kbd(asus, 0);
2250 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2254 if (code == NOTIFY_FNLOCK_TOGGLE) {
2255 asus->fnlock_locked = !asus->fnlock_locked;
2256 asus_wmi_fnlock_update(asus);
2260 if (code == asus->tablet_switch_event_code) {
2261 asus_wmi_tablet_mode_get_state(asus);
2265 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2266 fan_boost_mode_switch_next(asus);
2270 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2271 throttle_thermal_policy_switch_next(asus);
2275 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2278 if (!sparse_keymap_report_event(asus->inputdev, code,
2279 key_value, autorelease))
2280 pr_info("Unknown key %x pressed\n", code);
2283 static void asus_wmi_notify(u32 value, void *context)
2285 struct asus_wmi *asus = context;
2289 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2290 code = asus_wmi_get_event_code(value);
2292 pr_warn("Failed to get notify code: %d\n", code);
2296 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2299 asus_wmi_handle_event_code(code, asus);
2302 * Double check that queue is present:
2303 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2305 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2309 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2312 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2317 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2318 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2320 pr_warn("Failed to get event during flush: %d\n", code);
2324 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2328 pr_warn("Failed to flush event queue\n");
2332 /* Sysfs **********************************************************************/
2334 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2335 const char *buf, size_t count)
2340 value = asus_wmi_get_devstate_simple(asus, devid);
2344 err = kstrtoint(buf, 0, &value);
2348 err = asus_wmi_set_devstate(devid, value, &retval);
2355 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2357 int value = asus_wmi_get_devstate_simple(asus, devid);
2362 return sprintf(buf, "%d\n", value);
2365 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
2366 static ssize_t show_##_name(struct device *dev, \
2367 struct device_attribute *attr, \
2370 struct asus_wmi *asus = dev_get_drvdata(dev); \
2372 return show_sys_wmi(asus, _cm, buf); \
2374 static ssize_t store_##_name(struct device *dev, \
2375 struct device_attribute *attr, \
2376 const char *buf, size_t count) \
2378 struct asus_wmi *asus = dev_get_drvdata(dev); \
2380 return store_sys_wmi(asus, _cm, buf, count); \
2382 static struct device_attribute dev_attr_##_name = { \
2384 .name = __stringify(_name), \
2386 .show = show_##_name, \
2387 .store = store_##_name, \
2390 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2391 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2392 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2393 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2394 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2396 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2397 const char *buf, size_t count)
2401 rv = kstrtoint(buf, 0, &value);
2405 if (value < 0 || value > 2)
2408 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2415 static DEVICE_ATTR_WO(cpufv);
2417 static struct attribute *platform_attributes[] = {
2418 &dev_attr_cpufv.attr,
2419 &dev_attr_camera.attr,
2420 &dev_attr_cardr.attr,
2421 &dev_attr_touchpad.attr,
2422 &dev_attr_dgpu_disable.attr,
2423 &dev_attr_lid_resume.attr,
2424 &dev_attr_als_enable.attr,
2425 &dev_attr_fan_boost_mode.attr,
2426 &dev_attr_throttle_thermal_policy.attr,
2430 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2431 struct attribute *attr, int idx)
2433 struct device *dev = container_of(kobj, struct device, kobj);
2434 struct asus_wmi *asus = dev_get_drvdata(dev);
2438 if (attr == &dev_attr_camera.attr)
2439 devid = ASUS_WMI_DEVID_CAMERA;
2440 else if (attr == &dev_attr_cardr.attr)
2441 devid = ASUS_WMI_DEVID_CARDREADER;
2442 else if (attr == &dev_attr_touchpad.attr)
2443 devid = ASUS_WMI_DEVID_TOUCHPAD;
2444 else if (attr == &dev_attr_lid_resume.attr)
2445 devid = ASUS_WMI_DEVID_LID_RESUME;
2446 else if (attr == &dev_attr_als_enable.attr)
2447 devid = ASUS_WMI_DEVID_ALS_ENABLE;
2448 else if (attr == &dev_attr_dgpu_disable.attr)
2449 ok = asus->dgpu_disable_available;
2450 else if (attr == &dev_attr_fan_boost_mode.attr)
2451 ok = asus->fan_boost_mode_available;
2452 else if (attr == &dev_attr_throttle_thermal_policy.attr)
2453 ok = asus->throttle_thermal_policy_available;
2456 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2458 return ok ? attr->mode : 0;
2461 static const struct attribute_group platform_attribute_group = {
2462 .is_visible = asus_sysfs_is_visible,
2463 .attrs = platform_attributes
2466 static void asus_wmi_sysfs_exit(struct platform_device *device)
2468 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2471 static int asus_wmi_sysfs_init(struct platform_device *device)
2473 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2476 /* Platform device ************************************************************/
2478 static int asus_wmi_platform_init(struct asus_wmi *asus)
2480 struct device *dev = &asus->platform_device->dev;
2484 /* INIT enable hotkeys on some models */
2485 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2486 pr_info("Initialization: %#x\n", rv);
2488 /* We don't know yet what to do with this version... */
2489 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2490 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2495 * The SFUN method probably allows the original driver to get the list
2496 * of features supported by a given model. For now, 0x0100 or 0x0800
2497 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2498 * The significance of others is yet to be found.
2500 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2501 pr_info("SFUN value: %#x\n", rv);
2506 * Eee PC and Notebooks seems to have different method_id for DSTS,
2507 * but it may also be related to the BIOS's SPEC.
2508 * Note, on most Eeepc, there is no way to check if a method exist
2509 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2510 * but once again, SPEC may probably be used for that kind of things.
2512 * Additionally at least TUF Gaming series laptops return nothing for
2513 * unknown methods, so the detection in this way is not possible.
2515 * There is strong indication that only ACPI WMI devices that have _UID
2516 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2518 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2522 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2523 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2524 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2526 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2527 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2531 * Some devices can have multiple event codes stored in a queue before
2532 * the module load if it was unloaded intermittently after calling
2533 * the INIT method (enables event handling). The WMI notify handler is
2534 * expected to retrieve all event codes until a retrieved code equals
2535 * queue end marker (One or Ones). Old codes are flushed from the queue
2536 * upon module load. Not enabling this when it should be has minimal
2537 * visible impact so fall back if anything goes wrong.
2539 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2540 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2541 dev_info(dev, "Detected ATK, enable event queue\n");
2543 if (!asus_wmi_notify_queue_flush(asus))
2544 asus->wmi_event_queue = true;
2547 /* CWAP allow to define the behavior of the Fn+F2 key,
2548 * this method doesn't seems to be present on Eee PCs */
2549 if (asus->driver->quirks->wapf >= 0)
2550 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2551 asus->driver->quirks->wapf, NULL);
2556 /* debugfs ********************************************************************/
2558 struct asus_wmi_debugfs_node {
2559 struct asus_wmi *asus;
2561 int (*show) (struct seq_file *m, void *data);
2564 static int show_dsts(struct seq_file *m, void *data)
2566 struct asus_wmi *asus = m->private;
2570 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2574 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2579 static int show_devs(struct seq_file *m, void *data)
2581 struct asus_wmi *asus = m->private;
2585 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2590 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2591 asus->debug.ctrl_param, retval);
2596 static int show_call(struct seq_file *m, void *data)
2598 struct asus_wmi *asus = m->private;
2599 struct bios_args args = {
2600 .arg0 = asus->debug.dev_id,
2601 .arg1 = asus->debug.ctrl_param,
2603 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2604 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2605 union acpi_object *obj;
2608 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2609 0, asus->debug.method_id,
2612 if (ACPI_FAILURE(status))
2615 obj = (union acpi_object *)output.pointer;
2616 if (obj && obj->type == ACPI_TYPE_INTEGER)
2617 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2618 asus->debug.dev_id, asus->debug.ctrl_param,
2619 (u32) obj->integer.value);
2621 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2622 asus->debug.dev_id, asus->debug.ctrl_param,
2623 obj ? obj->type : -1);
2630 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2631 {NULL, "devs", show_devs},
2632 {NULL, "dsts", show_dsts},
2633 {NULL, "call", show_call},
2636 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2638 struct asus_wmi_debugfs_node *node = inode->i_private;
2640 return single_open(file, node->show, node->asus);
2643 static const struct file_operations asus_wmi_debugfs_io_ops = {
2644 .owner = THIS_MODULE,
2645 .open = asus_wmi_debugfs_open,
2647 .llseek = seq_lseek,
2648 .release = single_release,
2651 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2653 debugfs_remove_recursive(asus->debug.root);
2656 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2660 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2662 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2663 &asus->debug.method_id);
2665 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2666 &asus->debug.dev_id);
2668 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2669 &asus->debug.ctrl_param);
2671 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2672 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2675 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2676 asus->debug.root, node,
2677 &asus_wmi_debugfs_io_ops);
2681 /* Init / exit ****************************************************************/
2683 static int asus_wmi_add(struct platform_device *pdev)
2685 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2686 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2687 struct asus_wmi *asus;
2688 const char *chassis_type;
2693 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2697 asus->driver = wdrv;
2698 asus->platform_device = pdev;
2699 wdrv->platform_device = pdev;
2700 platform_set_drvdata(asus->platform_device, asus);
2702 if (wdrv->detect_quirks)
2703 wdrv->detect_quirks(asus->driver);
2705 err = asus_wmi_platform_init(asus);
2709 err = dgpu_disable_check_present(asus);
2711 goto fail_dgpu_disable;
2713 err = fan_boost_mode_check_present(asus);
2715 goto fail_fan_boost_mode;
2717 err = throttle_thermal_policy_check_present(asus);
2719 goto fail_throttle_thermal_policy;
2721 throttle_thermal_policy_set_default(asus);
2723 err = asus_wmi_sysfs_init(asus->platform_device);
2727 err = asus_wmi_input_init(asus);
2731 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2733 err = asus_wmi_hwmon_init(asus);
2737 err = asus_wmi_led_init(asus);
2741 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2742 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2743 asus->driver->wlan_ctrl_by_user = 1;
2745 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2746 err = asus_wmi_rfkill_init(asus);
2751 if (asus->driver->quirks->wmi_force_als_set)
2754 /* Some Asus desktop boards export an acpi-video backlight interface,
2755 stop this from showing up */
2756 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2757 if (chassis_type && !strcmp(chassis_type, "3"))
2758 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2760 if (asus->driver->quirks->wmi_backlight_power)
2761 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2763 if (asus->driver->quirks->wmi_backlight_native)
2764 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2766 if (asus->driver->quirks->xusb2pr)
2767 asus_wmi_set_xusb2pr(asus);
2769 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2770 err = asus_wmi_backlight_init(asus);
2771 if (err && err != -ENODEV)
2772 goto fail_backlight;
2773 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
2774 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2776 if (asus_wmi_has_fnlock_key(asus)) {
2777 asus->fnlock_locked = true;
2778 asus_wmi_fnlock_update(asus);
2781 status = wmi_install_notify_handler(asus->driver->event_guid,
2782 asus_wmi_notify, asus);
2783 if (ACPI_FAILURE(status)) {
2784 pr_err("Unable to register notify handler - %d\n", status);
2786 goto fail_wmi_handler;
2789 if (asus->driver->quirks->i8042_filter) {
2790 err = i8042_install_filter(asus->driver->quirks->i8042_filter);
2792 pr_warn("Unable to install key filter - %d\n", err);
2795 asus_wmi_battery_init(asus);
2797 asus_wmi_debugfs_init(asus);
2802 asus_wmi_backlight_exit(asus);
2804 asus_wmi_rfkill_exit(asus);
2806 asus_wmi_led_exit(asus);
2809 asus_wmi_input_exit(asus);
2811 asus_wmi_sysfs_exit(asus->platform_device);
2813 fail_throttle_thermal_policy:
2814 fail_fan_boost_mode:
2821 static int asus_wmi_remove(struct platform_device *device)
2823 struct asus_wmi *asus;
2825 asus = platform_get_drvdata(device);
2826 if (asus->driver->quirks->i8042_filter)
2827 i8042_remove_filter(asus->driver->quirks->i8042_filter);
2828 wmi_remove_notify_handler(asus->driver->event_guid);
2829 asus_wmi_backlight_exit(asus);
2830 asus_wmi_input_exit(asus);
2831 asus_wmi_led_exit(asus);
2832 asus_wmi_rfkill_exit(asus);
2833 asus_wmi_debugfs_exit(asus);
2834 asus_wmi_sysfs_exit(asus->platform_device);
2835 asus_fan_set_auto(asus);
2836 asus_wmi_battery_exit(asus);
2842 /* Platform driver - hibernate/resume callbacks *******************************/
2844 static int asus_hotk_thaw(struct device *device)
2846 struct asus_wmi *asus = dev_get_drvdata(device);
2848 if (asus->wlan.rfkill) {
2852 * Work around bios bug - acpi _PTS turns off the wireless led
2853 * during suspend. Normally it restores it on resume, but
2854 * we should kick it ourselves in case hibernation is aborted.
2856 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2857 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2863 static int asus_hotk_resume(struct device *device)
2865 struct asus_wmi *asus = dev_get_drvdata(device);
2867 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2868 kbd_led_update(asus);
2870 if (asus_wmi_has_fnlock_key(asus))
2871 asus_wmi_fnlock_update(asus);
2873 asus_wmi_tablet_mode_get_state(asus);
2877 static int asus_hotk_restore(struct device *device)
2879 struct asus_wmi *asus = dev_get_drvdata(device);
2882 /* Refresh both wlan rfkill state and pci hotplug */
2883 if (asus->wlan.rfkill)
2884 asus_rfkill_hotplug(asus);
2886 if (asus->bluetooth.rfkill) {
2887 bl = !asus_wmi_get_devstate_simple(asus,
2888 ASUS_WMI_DEVID_BLUETOOTH);
2889 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2891 if (asus->wimax.rfkill) {
2892 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2893 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2895 if (asus->wwan3g.rfkill) {
2896 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2897 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2899 if (asus->gps.rfkill) {
2900 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2901 rfkill_set_sw_state(asus->gps.rfkill, bl);
2903 if (asus->uwb.rfkill) {
2904 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2905 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2907 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2908 kbd_led_update(asus);
2910 if (asus_wmi_has_fnlock_key(asus))
2911 asus_wmi_fnlock_update(asus);
2913 asus_wmi_tablet_mode_get_state(asus);
2917 static const struct dev_pm_ops asus_pm_ops = {
2918 .thaw = asus_hotk_thaw,
2919 .restore = asus_hotk_restore,
2920 .resume = asus_hotk_resume,
2923 /* Registration ***************************************************************/
2925 static int asus_wmi_probe(struct platform_device *pdev)
2927 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2928 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2931 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2932 pr_warn("ASUS Management GUID not found\n");
2936 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2937 pr_warn("ASUS Event GUID not found\n");
2942 ret = wdrv->probe(pdev);
2947 return asus_wmi_add(pdev);
2952 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2954 struct platform_driver *platform_driver;
2955 struct platform_device *platform_device;
2960 platform_driver = &driver->platform_driver;
2961 platform_driver->remove = asus_wmi_remove;
2962 platform_driver->driver.owner = driver->owner;
2963 platform_driver->driver.name = driver->name;
2964 platform_driver->driver.pm = &asus_pm_ops;
2966 platform_device = platform_create_bundle(platform_driver,
2969 if (IS_ERR(platform_device))
2970 return PTR_ERR(platform_device);
2975 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2977 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2979 platform_device_unregister(driver->platform_device);
2980 platform_driver_unregister(&driver->platform_driver);
2983 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2985 static int __init asus_wmi_init(void)
2987 pr_info("ASUS WMI generic driver loaded\n");
2991 static void __exit asus_wmi_exit(void)
2993 pr_info("ASUS WMI generic driver unloaded\n");
2996 module_init(asus_wmi_init);
2997 module_exit(asus_wmi_exit);