1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Asus PC WMI hotkey driver
5 * Copyright(C) 2010 Intel Corporation.
8 * Portions based on wistron_btns.c:
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>
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
67 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
69 #define ASUS_FAN_DESC "cpu_fan"
70 #define ASUS_FAN_MFUN 0x13
71 #define ASUS_FAN_SFUN_READ 0x06
72 #define ASUS_FAN_SFUN_WRITE 0x07
74 /* Based on standard hwmon pwmX_enable values */
75 #define ASUS_FAN_CTRL_FULLSPEED 0
76 #define ASUS_FAN_CTRL_MANUAL 1
77 #define ASUS_FAN_CTRL_AUTO 2
79 #define ASUS_FAN_BOOST_MODE_NORMAL 0
80 #define ASUS_FAN_BOOST_MODE_OVERBOOST 1
81 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
82 #define ASUS_FAN_BOOST_MODE_SILENT 2
83 #define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
84 #define ASUS_FAN_BOOST_MODES_MASK 0x03
86 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
87 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
88 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
90 #define USB_INTEL_XUSB2PR 0xD0
91 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
93 #define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
94 #define ASUS_ACPI_UID_ATK "ATK"
96 #define WMI_EVENT_QUEUE_SIZE 0x10
97 #define WMI_EVENT_QUEUE_END 0x1
98 #define WMI_EVENT_MASK 0xFFFF
99 /* The WMI hotkey event value is always the same. */
100 #define WMI_EVENT_VALUE_ATK 0xFF
102 #define WMI_EVENT_MASK 0xFFFF
104 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
106 static bool ashs_present(void)
109 while (ashs_ids[i]) {
110 if (acpi_dev_found(ashs_ids[i++]))
119 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
125 * Struct that's used for all methods called via AGFN. Naming is
126 * identically to the AML code.
129 u16 mfun; /* probably "Multi-function" to be called */
130 u16 sfun; /* probably "Sub-function" to be called */
131 u16 len; /* size of the hole struct, including subfunction fields */
132 u8 stas; /* not used by now */
133 u8 err; /* zero on success */
136 /* struct used for calling fan read and write methods */
137 struct agfn_fan_args {
138 struct agfn_args agfn; /* common fields */
139 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
140 u32 speed; /* read: RPM/100 - write: 0-255 */
144 * <platform>/ - debugfs root directory
145 * dev_id - current dev_id
146 * ctrl_param - current ctrl_param
147 * method_id - current method_id
148 * devs - call DEVS(dev_id, ctrl_param) and print result
149 * dsts - call DSTS(dev_id) and print result
150 * call - call method_id(dev_id, ctrl_param) and print result
152 struct asus_wmi_debug {
160 struct asus_wmi *asus;
161 struct rfkill *rfkill;
167 FAN_TYPE_AGFN, /* deprecated on newer platforms */
168 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
175 bool wmi_event_queue;
177 struct input_dev *inputdev;
178 struct backlight_device *backlight_device;
179 struct platform_device *platform_device;
181 struct led_classdev wlan_led;
183 struct led_classdev tpd_led;
185 struct led_classdev kbd_led;
187 struct led_classdev lightbar_led;
189 struct workqueue_struct *led_workqueue;
190 struct work_struct tpd_led_work;
191 struct work_struct wlan_led_work;
192 struct work_struct lightbar_led_work;
194 struct asus_rfkill wlan;
195 struct asus_rfkill bluetooth;
196 struct asus_rfkill wimax;
197 struct asus_rfkill wwan3g;
198 struct asus_rfkill gps;
199 struct asus_rfkill uwb;
201 enum fan_type fan_type;
205 bool fan_boost_mode_available;
206 u8 fan_boost_mode_mask;
209 bool throttle_thermal_policy_available;
210 u8 throttle_thermal_policy_mode;
212 // The RSOC controls the maximum charging percentage.
213 bool battery_rsoc_available;
215 struct hotplug_slot hotplug_slot;
216 struct mutex hotplug_lock;
217 struct mutex wmi_lock;
218 struct workqueue_struct *hotplug_workqueue;
219 struct work_struct hotplug_work;
223 struct asus_wmi_debug debug;
225 struct asus_wmi_driver *driver;
228 /* WMI ************************************************************************/
230 static int asus_wmi_evaluate_method3(u32 method_id,
231 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
233 struct bios_args args = {
238 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
239 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
241 union acpi_object *obj;
244 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
247 if (ACPI_FAILURE(status))
250 obj = (union acpi_object *)output.pointer;
251 if (obj && obj->type == ACPI_TYPE_INTEGER)
252 tmp = (u32) obj->integer.value;
259 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
265 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
267 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
269 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
271 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
273 struct acpi_buffer input;
279 * Copy to dma capable address otherwise memory corruption occurs as
280 * bios has to be able to access it.
282 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
283 input.length = args.length;
286 phys_addr = virt_to_phys(input.pointer);
288 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
289 phys_addr, 0, &retval);
291 memcpy(args.pointer, input.pointer, args.length);
293 kfree(input.pointer);
300 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
302 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
305 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
308 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
312 /* Helper for special devices with magic return codes */
313 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
314 u32 dev_id, u32 mask)
319 err = asus_wmi_get_devstate(asus, dev_id, &retval);
323 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
326 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
327 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
331 return retval & mask;
334 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
336 return asus_wmi_get_devstate_bits(asus, dev_id,
337 ASUS_WMI_DSTS_STATUS_BIT);
340 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
343 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
345 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
348 /* Input **********************************************************************/
350 static int asus_wmi_input_init(struct asus_wmi *asus)
354 asus->inputdev = input_allocate_device();
358 asus->inputdev->name = asus->driver->input_name;
359 asus->inputdev->phys = asus->driver->input_phys;
360 asus->inputdev->id.bustype = BUS_HOST;
361 asus->inputdev->dev.parent = &asus->platform_device->dev;
362 set_bit(EV_REP, asus->inputdev->evbit);
364 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
368 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
370 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
371 input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
372 } else if (result != -ENODEV) {
373 pr_err("Error checking for keyboard-dock: %d\n", result);
376 err = input_register_device(asus->inputdev);
383 input_free_device(asus->inputdev);
387 static void asus_wmi_input_exit(struct asus_wmi *asus)
390 input_unregister_device(asus->inputdev);
392 asus->inputdev = NULL;
395 /* Battery ********************************************************************/
397 /* The battery maximum charging percentage */
398 static int charge_end_threshold;
400 static ssize_t charge_control_end_threshold_store(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
406 ret = kstrtouint(buf, 10, &value);
410 if (value < 0 || value > 100)
413 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
420 /* There isn't any method in the DSDT to read the threshold, so we
421 * save the threshold.
423 charge_end_threshold = value;
427 static ssize_t charge_control_end_threshold_show(struct device *device,
428 struct device_attribute *attr,
431 return sprintf(buf, "%d\n", charge_end_threshold);
434 static DEVICE_ATTR_RW(charge_control_end_threshold);
436 static int asus_wmi_battery_add(struct power_supply *battery)
438 /* The WMI method does not provide a way to specific a battery, so we
439 * just assume it is the first battery.
440 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
441 * battery is named BATT.
443 if (strcmp(battery->desc->name, "BAT0") != 0 &&
444 strcmp(battery->desc->name, "BAT1") != 0 &&
445 strcmp(battery->desc->name, "BATT") != 0)
448 if (device_create_file(&battery->dev,
449 &dev_attr_charge_control_end_threshold))
452 /* The charge threshold is only reset when the system is power cycled,
453 * and we can't get the current threshold so let set it to 100% when
454 * a battery is added.
456 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
457 charge_end_threshold = 100;
462 static int asus_wmi_battery_remove(struct power_supply *battery)
464 device_remove_file(&battery->dev,
465 &dev_attr_charge_control_end_threshold);
469 static struct acpi_battery_hook battery_hook = {
470 .add_battery = asus_wmi_battery_add,
471 .remove_battery = asus_wmi_battery_remove,
472 .name = "ASUS Battery Extension",
475 static void asus_wmi_battery_init(struct asus_wmi *asus)
477 asus->battery_rsoc_available = false;
478 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
479 asus->battery_rsoc_available = true;
480 battery_hook_register(&battery_hook);
484 static void asus_wmi_battery_exit(struct asus_wmi *asus)
486 if (asus->battery_rsoc_available)
487 battery_hook_unregister(&battery_hook);
490 /* LEDs ***********************************************************************/
493 * These functions actually update the LED's, and are called from a
494 * workqueue. By doing this as separate work rather than when the LED
495 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
496 * potentially bad time, such as a timer interrupt.
498 static void tpd_led_update(struct work_struct *work)
501 struct asus_wmi *asus;
503 asus = container_of(work, struct asus_wmi, tpd_led_work);
505 ctrl_param = asus->tpd_led_wk;
506 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
509 static void tpd_led_set(struct led_classdev *led_cdev,
510 enum led_brightness value)
512 struct asus_wmi *asus;
514 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
516 asus->tpd_led_wk = !!value;
517 queue_work(asus->led_workqueue, &asus->tpd_led_work);
520 static int read_tpd_led_state(struct asus_wmi *asus)
522 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
525 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
527 struct asus_wmi *asus;
529 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
531 return read_tpd_led_state(asus);
534 static void kbd_led_update(struct asus_wmi *asus)
538 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
539 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
542 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
548 * bit 7: light on/off
549 * bit 8-10: environment (0: dark, 1: normal, 2: light)
550 * bit 17: status unknown
552 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
555 /* Unknown status is considered as off */
556 if (retval == 0x8000)
563 *level = retval & 0x7F;
565 *env = (retval >> 8) & 0x7F;
569 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
571 struct asus_wmi *asus;
574 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
575 max_level = asus->kbd_led.max_brightness;
577 asus->kbd_led_wk = clamp_val(value, 0, max_level);
578 kbd_led_update(asus);
581 static void kbd_led_set(struct led_classdev *led_cdev,
582 enum led_brightness value)
584 /* Prevent disabling keyboard backlight on module unregister */
585 if (led_cdev->flags & LED_UNREGISTERING)
588 do_kbd_led_set(led_cdev, value);
591 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
593 struct led_classdev *led_cdev = &asus->kbd_led;
595 do_kbd_led_set(led_cdev, value);
596 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
599 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
601 struct asus_wmi *asus;
604 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
606 retval = kbd_led_read(asus, &value, NULL);
613 static int wlan_led_unknown_state(struct asus_wmi *asus)
617 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
619 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
622 static void wlan_led_update(struct work_struct *work)
625 struct asus_wmi *asus;
627 asus = container_of(work, struct asus_wmi, wlan_led_work);
629 ctrl_param = asus->wlan_led_wk;
630 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
633 static void wlan_led_set(struct led_classdev *led_cdev,
634 enum led_brightness value)
636 struct asus_wmi *asus;
638 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
640 asus->wlan_led_wk = !!value;
641 queue_work(asus->led_workqueue, &asus->wlan_led_work);
644 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
646 struct asus_wmi *asus;
649 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
650 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
652 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
655 static void lightbar_led_update(struct work_struct *work)
657 struct asus_wmi *asus;
660 asus = container_of(work, struct asus_wmi, lightbar_led_work);
662 ctrl_param = asus->lightbar_led_wk;
663 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
666 static void lightbar_led_set(struct led_classdev *led_cdev,
667 enum led_brightness value)
669 struct asus_wmi *asus;
671 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
673 asus->lightbar_led_wk = !!value;
674 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
677 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
679 struct asus_wmi *asus;
682 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
683 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
685 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
688 static void asus_wmi_led_exit(struct asus_wmi *asus)
690 led_classdev_unregister(&asus->kbd_led);
691 led_classdev_unregister(&asus->tpd_led);
692 led_classdev_unregister(&asus->wlan_led);
693 led_classdev_unregister(&asus->lightbar_led);
695 if (asus->led_workqueue)
696 destroy_workqueue(asus->led_workqueue);
699 static int asus_wmi_led_init(struct asus_wmi *asus)
703 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
704 if (!asus->led_workqueue)
707 if (read_tpd_led_state(asus) >= 0) {
708 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
710 asus->tpd_led.name = "asus::touchpad";
711 asus->tpd_led.brightness_set = tpd_led_set;
712 asus->tpd_led.brightness_get = tpd_led_get;
713 asus->tpd_led.max_brightness = 1;
715 rv = led_classdev_register(&asus->platform_device->dev,
721 if (!kbd_led_read(asus, &led_val, NULL)) {
722 asus->kbd_led_wk = led_val;
723 asus->kbd_led.name = "asus::kbd_backlight";
724 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
725 asus->kbd_led.brightness_set = kbd_led_set;
726 asus->kbd_led.brightness_get = kbd_led_get;
727 asus->kbd_led.max_brightness = 3;
729 rv = led_classdev_register(&asus->platform_device->dev,
735 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
736 && (asus->driver->quirks->wapf > 0)) {
737 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
739 asus->wlan_led.name = "asus::wlan";
740 asus->wlan_led.brightness_set = wlan_led_set;
741 if (!wlan_led_unknown_state(asus))
742 asus->wlan_led.brightness_get = wlan_led_get;
743 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
744 asus->wlan_led.max_brightness = 1;
745 asus->wlan_led.default_trigger = "asus-wlan";
747 rv = led_classdev_register(&asus->platform_device->dev,
753 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
754 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
756 asus->lightbar_led.name = "asus::lightbar";
757 asus->lightbar_led.brightness_set = lightbar_led_set;
758 asus->lightbar_led.brightness_get = lightbar_led_get;
759 asus->lightbar_led.max_brightness = 1;
761 rv = led_classdev_register(&asus->platform_device->dev,
762 &asus->lightbar_led);
767 asus_wmi_led_exit(asus);
772 /* RF *************************************************************************/
775 * PCI hotplug (for wlan rfkill)
777 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
779 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
786 static void asus_rfkill_hotplug(struct asus_wmi *asus)
794 mutex_lock(&asus->wmi_lock);
795 blocked = asus_wlan_rfkill_blocked(asus);
796 mutex_unlock(&asus->wmi_lock);
798 mutex_lock(&asus->hotplug_lock);
799 pci_lock_rescan_remove();
801 if (asus->wlan.rfkill)
802 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
804 if (asus->hotplug_slot.ops) {
805 bus = pci_find_bus(0, 1);
807 pr_warn("Unable to find PCI bus 1?\n");
811 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
812 pr_err("Unable to read PCI config space?\n");
815 absent = (l == 0xffffffff);
817 if (blocked != absent) {
818 pr_warn("BIOS says wireless lan is %s, "
819 "but the pci device is %s\n",
820 blocked ? "blocked" : "unblocked",
821 absent ? "absent" : "present");
822 pr_warn("skipped wireless hotplug as probably "
823 "inappropriate for this model\n");
828 dev = pci_get_slot(bus, 0);
830 /* Device already present */
834 dev = pci_scan_single_device(bus, 0);
836 pci_bus_assign_resources(bus);
837 pci_bus_add_device(dev);
840 dev = pci_get_slot(bus, 0);
842 pci_stop_and_remove_bus_device(dev);
849 pci_unlock_rescan_remove();
850 mutex_unlock(&asus->hotplug_lock);
853 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
855 struct asus_wmi *asus = data;
857 if (event != ACPI_NOTIFY_BUS_CHECK)
861 * We can't call directly asus_rfkill_hotplug because most
862 * of the time WMBC is still being executed and not reetrant.
863 * There is currently no way to tell ACPICA that we want this
864 * method to be serialized, we schedule a asus_rfkill_hotplug
865 * call later, in a safer context.
867 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
870 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
875 status = acpi_get_handle(NULL, node, &handle);
876 if (ACPI_FAILURE(status))
879 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
880 asus_rfkill_notify, asus);
881 if (ACPI_FAILURE(status))
882 pr_warn("Failed to register notify on %s\n", node);
887 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
889 acpi_status status = AE_OK;
892 status = acpi_get_handle(NULL, node, &handle);
893 if (ACPI_FAILURE(status))
896 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
898 if (ACPI_FAILURE(status))
899 pr_err("Error removing rfkill notify handler %s\n", node);
902 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
905 struct asus_wmi *asus = container_of(hotplug_slot,
906 struct asus_wmi, hotplug_slot);
907 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
916 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
917 .get_adapter_status = asus_get_adapter_status,
918 .get_power_status = asus_get_adapter_status,
921 static void asus_hotplug_work(struct work_struct *work)
923 struct asus_wmi *asus;
925 asus = container_of(work, struct asus_wmi, hotplug_work);
926 asus_rfkill_hotplug(asus);
929 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
932 struct pci_bus *bus = pci_find_bus(0, 1);
935 pr_err("Unable to find wifi PCI bus\n");
939 asus->hotplug_workqueue =
940 create_singlethread_workqueue("hotplug_workqueue");
941 if (!asus->hotplug_workqueue)
942 goto error_workqueue;
944 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
946 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
948 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
950 pr_err("Unable to register hotplug slot - %d\n", ret);
957 asus->hotplug_slot.ops = NULL;
958 destroy_workqueue(asus->hotplug_workqueue);
966 static int asus_rfkill_set(void *data, bool blocked)
968 struct asus_rfkill *priv = data;
969 u32 ctrl_param = !blocked;
970 u32 dev_id = priv->dev_id;
973 * If the user bit is set, BIOS can't set and record the wlan status,
974 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
975 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
976 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
977 * while setting the wlan status through WMI.
978 * This is also the behavior that windows app will do.
980 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
981 priv->asus->driver->wlan_ctrl_by_user)
982 dev_id = ASUS_WMI_DEVID_WLAN_LED;
984 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
987 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
989 struct asus_rfkill *priv = data;
992 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
997 rfkill_set_sw_state(priv->rfkill, !result);
1000 static int asus_rfkill_wlan_set(void *data, bool blocked)
1002 struct asus_rfkill *priv = data;
1003 struct asus_wmi *asus = priv->asus;
1007 * This handler is enabled only if hotplug is enabled.
1008 * In this case, the asus_wmi_set_devstate() will
1009 * trigger a wmi notification and we need to wait
1010 * this call to finish before being able to call
1013 mutex_lock(&asus->wmi_lock);
1014 ret = asus_rfkill_set(data, blocked);
1015 mutex_unlock(&asus->wmi_lock);
1019 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1020 .set_block = asus_rfkill_wlan_set,
1021 .query = asus_rfkill_query,
1024 static const struct rfkill_ops asus_rfkill_ops = {
1025 .set_block = asus_rfkill_set,
1026 .query = asus_rfkill_query,
1029 static int asus_new_rfkill(struct asus_wmi *asus,
1030 struct asus_rfkill *arfkill,
1031 const char *name, enum rfkill_type type, int dev_id)
1033 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1034 struct rfkill **rfkill = &arfkill->rfkill;
1039 arfkill->dev_id = dev_id;
1040 arfkill->asus = asus;
1042 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1043 asus->driver->quirks->hotplug_wireless)
1044 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1045 &asus_rfkill_wlan_ops, arfkill);
1047 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1048 &asus_rfkill_ops, arfkill);
1053 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1054 (asus->driver->quirks->wapf > 0))
1055 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1057 rfkill_init_sw_state(*rfkill, !result);
1058 result = rfkill_register(*rfkill);
1060 rfkill_destroy(*rfkill);
1067 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1069 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1072 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1073 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1074 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1075 if (asus->wlan.rfkill) {
1076 rfkill_unregister(asus->wlan.rfkill);
1077 rfkill_destroy(asus->wlan.rfkill);
1078 asus->wlan.rfkill = NULL;
1081 * Refresh pci hotplug in case the rfkill state was changed after
1082 * asus_unregister_rfkill_notifier()
1084 asus_rfkill_hotplug(asus);
1085 if (asus->hotplug_slot.ops)
1086 pci_hp_deregister(&asus->hotplug_slot);
1087 if (asus->hotplug_workqueue)
1088 destroy_workqueue(asus->hotplug_workqueue);
1090 if (asus->bluetooth.rfkill) {
1091 rfkill_unregister(asus->bluetooth.rfkill);
1092 rfkill_destroy(asus->bluetooth.rfkill);
1093 asus->bluetooth.rfkill = NULL;
1095 if (asus->wimax.rfkill) {
1096 rfkill_unregister(asus->wimax.rfkill);
1097 rfkill_destroy(asus->wimax.rfkill);
1098 asus->wimax.rfkill = NULL;
1100 if (asus->wwan3g.rfkill) {
1101 rfkill_unregister(asus->wwan3g.rfkill);
1102 rfkill_destroy(asus->wwan3g.rfkill);
1103 asus->wwan3g.rfkill = NULL;
1105 if (asus->gps.rfkill) {
1106 rfkill_unregister(asus->gps.rfkill);
1107 rfkill_destroy(asus->gps.rfkill);
1108 asus->gps.rfkill = NULL;
1110 if (asus->uwb.rfkill) {
1111 rfkill_unregister(asus->uwb.rfkill);
1112 rfkill_destroy(asus->uwb.rfkill);
1113 asus->uwb.rfkill = NULL;
1117 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1121 mutex_init(&asus->hotplug_lock);
1122 mutex_init(&asus->wmi_lock);
1124 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1125 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1127 if (result && result != -ENODEV)
1130 result = asus_new_rfkill(asus, &asus->bluetooth,
1131 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1132 ASUS_WMI_DEVID_BLUETOOTH);
1134 if (result && result != -ENODEV)
1137 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1138 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1140 if (result && result != -ENODEV)
1143 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1144 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1146 if (result && result != -ENODEV)
1149 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1150 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1152 if (result && result != -ENODEV)
1155 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1156 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1158 if (result && result != -ENODEV)
1161 if (!asus->driver->quirks->hotplug_wireless)
1164 result = asus_setup_pci_hotplug(asus);
1166 * If we get -EBUSY then something else is handling the PCI hotplug -
1167 * don't fail in this case
1169 if (result == -EBUSY)
1172 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1173 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1174 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1176 * Refresh pci hotplug in case the rfkill state was changed during
1179 asus_rfkill_hotplug(asus);
1182 if (result && result != -ENODEV)
1183 asus_wmi_rfkill_exit(asus);
1185 if (result == -ENODEV)
1191 /* Quirks *********************************************************************/
1193 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1195 struct pci_dev *xhci_pdev;
1196 u32 orig_ports_available;
1197 u32 ports_available = asus->driver->quirks->xusb2pr;
1199 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1200 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1206 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1207 &orig_ports_available);
1209 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1210 cpu_to_le32(ports_available));
1212 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1213 orig_ports_available, ports_available);
1217 * Some devices dont support or have borcken get_als method
1218 * but still support set method.
1220 static void asus_wmi_set_als(void)
1222 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1225 /* Hwmon device ***************************************************************/
1227 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1230 struct agfn_fan_args args = {
1231 .agfn.len = sizeof(args),
1232 .agfn.mfun = ASUS_FAN_MFUN,
1233 .agfn.sfun = ASUS_FAN_SFUN_READ,
1237 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1243 status = asus_wmi_evaluate_method_agfn(input);
1245 if (status || args.agfn.err)
1249 *speed = args.speed;
1254 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1257 struct agfn_fan_args args = {
1258 .agfn.len = sizeof(args),
1259 .agfn.mfun = ASUS_FAN_MFUN,
1260 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1262 .speed = speed ? *speed : 0,
1264 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1267 /* 1: for setting 1st fan's speed 0: setting auto mode */
1268 if (fan != 1 && fan != 0)
1271 status = asus_wmi_evaluate_method_agfn(input);
1273 if (status || args.agfn.err)
1276 if (speed && fan == 1)
1277 asus->agfn_pwm = *speed;
1283 * Check if we can read the speed of one fan. If true we assume we can also
1286 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1292 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1296 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1301 * We need to find a better way, probably using sfun,
1303 * Currently we disable it if:
1304 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1305 * - reverved bits are non-zero
1306 * - sfun and presence bit are not set
1308 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1309 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1312 static int asus_fan_set_auto(struct asus_wmi *asus)
1317 switch (asus->fan_type) {
1318 case FAN_TYPE_SPEC83:
1319 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1329 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1342 static ssize_t pwm1_show(struct device *dev,
1343 struct device_attribute *attr,
1346 struct asus_wmi *asus = dev_get_drvdata(dev);
1350 /* If we already set a value then just return it */
1351 if (asus->agfn_pwm >= 0)
1352 return sprintf(buf, "%d\n", asus->agfn_pwm);
1355 * If we haven't set already set a value through the AGFN interface,
1356 * we read a current value through the (now-deprecated) FAN_CTRL device.
1358 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1364 if (value == 1) /* Low Speed */
1366 else if (value == 2)
1368 else if (value == 3)
1371 pr_err("Unknown fan speed %#x\n", value);
1375 return sprintf(buf, "%d\n", value);
1378 static ssize_t pwm1_store(struct device *dev,
1379 struct device_attribute *attr,
1380 const char *buf, size_t count) {
1381 struct asus_wmi *asus = dev_get_drvdata(dev);
1386 ret = kstrtouint(buf, 10, &value);
1390 value = clamp(value, 0, 255);
1392 state = asus_agfn_fan_speed_write(asus, 1, &value);
1394 pr_warn("Setting fan speed failed: %d\n", state);
1396 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1401 static ssize_t fan1_input_show(struct device *dev,
1402 struct device_attribute *attr,
1405 struct asus_wmi *asus = dev_get_drvdata(dev);
1409 switch (asus->fan_type) {
1410 case FAN_TYPE_SPEC83:
1411 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1420 /* no speed readable on manual mode */
1421 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1424 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1426 pr_warn("reading fan speed failed: %d\n", ret);
1435 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1438 static ssize_t pwm1_enable_show(struct device *dev,
1439 struct device_attribute *attr,
1442 struct asus_wmi *asus = dev_get_drvdata(dev);
1445 * Just read back the cached pwm mode.
1447 * For the CPU_FAN device, the spec indicates that we should be
1448 * able to read the device status and consult bit 19 to see if we
1449 * are in Full On or Automatic mode. However, this does not work
1450 * in practice on X532FL at least (the bit is always 0) and there's
1451 * also nothing in the DSDT to indicate that this behaviour exists.
1453 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1456 static ssize_t pwm1_enable_store(struct device *dev,
1457 struct device_attribute *attr,
1458 const char *buf, size_t count)
1460 struct asus_wmi *asus = dev_get_drvdata(dev);
1467 ret = kstrtouint(buf, 10, &state);
1471 if (asus->fan_type == FAN_TYPE_SPEC83) {
1472 switch (state) { /* standard documented hwmon values */
1473 case ASUS_FAN_CTRL_FULLSPEED:
1476 case ASUS_FAN_CTRL_AUTO:
1483 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1490 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1492 case ASUS_FAN_CTRL_MANUAL:
1495 case ASUS_FAN_CTRL_AUTO:
1496 status = asus_fan_set_auto(asus);
1506 asus->fan_pwm_mode = state;
1510 static ssize_t fan1_label_show(struct device *dev,
1511 struct device_attribute *attr,
1514 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1517 static ssize_t asus_hwmon_temp1(struct device *dev,
1518 struct device_attribute *attr,
1521 struct asus_wmi *asus = dev_get_drvdata(dev);
1525 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1529 return sprintf(buf, "%ld\n",
1530 deci_kelvin_to_millicelsius(value & 0xFFFF));
1534 static DEVICE_ATTR_RW(pwm1);
1535 static DEVICE_ATTR_RW(pwm1_enable);
1536 static DEVICE_ATTR_RO(fan1_input);
1537 static DEVICE_ATTR_RO(fan1_label);
1540 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1542 static struct attribute *hwmon_attributes[] = {
1543 &dev_attr_pwm1.attr,
1544 &dev_attr_pwm1_enable.attr,
1545 &dev_attr_fan1_input.attr,
1546 &dev_attr_fan1_label.attr,
1548 &dev_attr_temp1_input.attr,
1552 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1553 struct attribute *attr, int idx)
1555 struct device *dev = container_of(kobj, struct device, kobj);
1556 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1557 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1559 if (attr == &dev_attr_pwm1.attr) {
1560 if (asus->fan_type != FAN_TYPE_AGFN)
1562 } else if (attr == &dev_attr_fan1_input.attr
1563 || attr == &dev_attr_fan1_label.attr
1564 || attr == &dev_attr_pwm1_enable.attr) {
1565 if (asus->fan_type == FAN_TYPE_NONE)
1567 } else if (attr == &dev_attr_temp1_input.attr) {
1568 int err = asus_wmi_get_devstate(asus,
1569 ASUS_WMI_DEVID_THERMAL_CTRL,
1573 return 0; /* can't return negative here */
1576 * If the temperature value in deci-Kelvin is near the absolute
1577 * zero temperature, something is clearly wrong
1579 if (value == 0 || value == 1)
1586 static const struct attribute_group hwmon_attribute_group = {
1587 .is_visible = asus_hwmon_sysfs_is_visible,
1588 .attrs = hwmon_attributes
1590 __ATTRIBUTE_GROUPS(hwmon_attribute);
1592 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1594 struct device *dev = &asus->platform_device->dev;
1595 struct device *hwmon;
1597 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1598 hwmon_attribute_groups);
1600 if (IS_ERR(hwmon)) {
1601 pr_err("Could not register asus hwmon device\n");
1602 return PTR_ERR(hwmon);
1607 static int asus_wmi_fan_init(struct asus_wmi *asus)
1609 asus->fan_type = FAN_TYPE_NONE;
1610 asus->agfn_pwm = -1;
1612 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1613 asus->fan_type = FAN_TYPE_SPEC83;
1614 else if (asus_wmi_has_agfn_fan(asus))
1615 asus->fan_type = FAN_TYPE_AGFN;
1617 if (asus->fan_type == FAN_TYPE_NONE)
1620 asus_fan_set_auto(asus);
1621 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1625 /* Fan mode *******************************************************************/
1627 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1632 asus->fan_boost_mode_available = false;
1634 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1643 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1644 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1645 asus->fan_boost_mode_available = true;
1646 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1652 static int fan_boost_mode_write(struct asus_wmi *asus)
1658 value = asus->fan_boost_mode;
1660 pr_info("Set fan boost mode: %u\n", value);
1661 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1664 pr_warn("Failed to set fan boost mode: %d\n", err);
1669 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1677 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1679 u8 mask = asus->fan_boost_mode_mask;
1681 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1682 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1683 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1684 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1685 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1686 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1687 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1688 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1690 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1692 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1695 return fan_boost_mode_write(asus);
1698 static ssize_t fan_boost_mode_show(struct device *dev,
1699 struct device_attribute *attr, char *buf)
1701 struct asus_wmi *asus = dev_get_drvdata(dev);
1703 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
1706 static ssize_t fan_boost_mode_store(struct device *dev,
1707 struct device_attribute *attr,
1708 const char *buf, size_t count)
1712 struct asus_wmi *asus = dev_get_drvdata(dev);
1713 u8 mask = asus->fan_boost_mode_mask;
1715 result = kstrtou8(buf, 10, &new_mode);
1717 pr_warn("Trying to store invalid value\n");
1721 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1722 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
1724 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1725 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
1727 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
1731 asus->fan_boost_mode = new_mode;
1732 fan_boost_mode_write(asus);
1737 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1738 static DEVICE_ATTR_RW(fan_boost_mode);
1740 /* Throttle thermal policy ****************************************************/
1742 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
1747 asus->throttle_thermal_policy_available = false;
1749 err = asus_wmi_get_devstate(asus,
1750 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1758 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
1759 asus->throttle_thermal_policy_available = true;
1764 static int throttle_thermal_policy_write(struct asus_wmi *asus)
1770 value = asus->throttle_thermal_policy_mode;
1772 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1775 pr_warn("Failed to set throttle thermal policy: %d\n", err);
1780 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
1788 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
1790 if (!asus->throttle_thermal_policy_available)
1793 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1794 return throttle_thermal_policy_write(asus);
1797 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
1799 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
1801 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1802 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1804 asus->throttle_thermal_policy_mode = new_mode;
1805 return throttle_thermal_policy_write(asus);
1808 static ssize_t throttle_thermal_policy_show(struct device *dev,
1809 struct device_attribute *attr, char *buf)
1811 struct asus_wmi *asus = dev_get_drvdata(dev);
1812 u8 mode = asus->throttle_thermal_policy_mode;
1814 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
1817 static ssize_t throttle_thermal_policy_store(struct device *dev,
1818 struct device_attribute *attr,
1819 const char *buf, size_t count)
1823 struct asus_wmi *asus = dev_get_drvdata(dev);
1825 result = kstrtou8(buf, 10, &new_mode);
1829 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1832 asus->throttle_thermal_policy_mode = new_mode;
1833 throttle_thermal_policy_write(asus);
1838 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
1839 static DEVICE_ATTR_RW(throttle_thermal_policy);
1841 /* Backlight ******************************************************************/
1843 static int read_backlight_power(struct asus_wmi *asus)
1847 if (asus->driver->quirks->store_backlight_power)
1848 ret = !asus->driver->panel_power;
1850 ret = asus_wmi_get_devstate_simple(asus,
1851 ASUS_WMI_DEVID_BACKLIGHT);
1856 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1859 static int read_brightness_max(struct asus_wmi *asus)
1864 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1868 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1877 static int read_brightness(struct backlight_device *bd)
1879 struct asus_wmi *asus = bl_get_data(bd);
1883 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1887 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1890 static u32 get_scalar_command(struct backlight_device *bd)
1892 struct asus_wmi *asus = bl_get_data(bd);
1895 if ((asus->driver->brightness < bd->props.brightness) ||
1896 bd->props.brightness == bd->props.max_brightness)
1897 ctrl_param = 0x00008001;
1898 else if ((asus->driver->brightness > bd->props.brightness) ||
1899 bd->props.brightness == 0)
1900 ctrl_param = 0x00008000;
1902 asus->driver->brightness = bd->props.brightness;
1907 static int update_bl_status(struct backlight_device *bd)
1909 struct asus_wmi *asus = bl_get_data(bd);
1913 power = read_backlight_power(asus);
1914 if (power != -ENODEV && bd->props.power != power) {
1915 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1916 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1918 if (asus->driver->quirks->store_backlight_power)
1919 asus->driver->panel_power = bd->props.power;
1921 /* When using scalar brightness, updating the brightness
1922 * will mess with the backlight power */
1923 if (asus->driver->quirks->scalar_panel_brightness)
1927 if (asus->driver->quirks->scalar_panel_brightness)
1928 ctrl_param = get_scalar_command(bd);
1930 ctrl_param = bd->props.brightness;
1932 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1938 static const struct backlight_ops asus_wmi_bl_ops = {
1939 .get_brightness = read_brightness,
1940 .update_status = update_bl_status,
1943 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1945 struct backlight_device *bd = asus->backlight_device;
1946 int old = bd->props.brightness;
1949 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1950 new = code - NOTIFY_BRNUP_MIN + 1;
1951 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1952 new = code - NOTIFY_BRNDOWN_MIN;
1954 bd->props.brightness = new;
1955 backlight_update_status(bd);
1956 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1961 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1963 struct backlight_device *bd;
1964 struct backlight_properties props;
1968 max = read_brightness_max(asus);
1972 power = read_backlight_power(asus);
1973 if (power == -ENODEV)
1974 power = FB_BLANK_UNBLANK;
1978 memset(&props, 0, sizeof(struct backlight_properties));
1979 props.type = BACKLIGHT_PLATFORM;
1980 props.max_brightness = max;
1981 bd = backlight_device_register(asus->driver->name,
1982 &asus->platform_device->dev, asus,
1983 &asus_wmi_bl_ops, &props);
1985 pr_err("Could not register backlight device\n");
1989 asus->backlight_device = bd;
1991 if (asus->driver->quirks->store_backlight_power)
1992 asus->driver->panel_power = power;
1994 bd->props.brightness = read_brightness(bd);
1995 bd->props.power = power;
1996 backlight_update_status(bd);
1998 asus->driver->brightness = bd->props.brightness;
2003 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2005 backlight_device_unregister(asus->backlight_device);
2007 asus->backlight_device = NULL;
2010 static int is_display_toggle(int code)
2012 /* display toggle keys */
2013 if ((code >= 0x61 && code <= 0x67) ||
2014 (code >= 0x8c && code <= 0x93) ||
2015 (code >= 0xa0 && code <= 0xa7) ||
2016 (code >= 0xd0 && code <= 0xd5))
2022 /* Fn-lock ********************************************************************/
2024 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2028 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2030 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2031 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2034 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2036 int mode = asus->fnlock_locked;
2038 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2041 /* WMI events *****************************************************************/
2043 static int asus_wmi_get_event_code(u32 value)
2045 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2046 union acpi_object *obj;
2050 status = wmi_get_event_data(value, &response);
2051 if (ACPI_FAILURE(status)) {
2052 pr_warn("Failed to get WMI notify code: %s\n",
2053 acpi_format_exception(status));
2057 obj = (union acpi_object *)response.pointer;
2059 if (obj && obj->type == ACPI_TYPE_INTEGER)
2060 code = (int)(obj->integer.value & WMI_EVENT_MASK);
2068 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2070 unsigned int key_value = 1;
2071 bool autorelease = 1;
2072 int result, orig_code;
2076 if (asus->driver->key_filter) {
2077 asus->driver->key_filter(asus->driver, &code, &key_value,
2079 if (code == ASUS_WMI_KEY_IGNORE)
2083 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2084 code = ASUS_WMI_BRN_UP;
2085 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2086 code = ASUS_WMI_BRN_DOWN;
2088 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
2089 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2090 asus_wmi_backlight_notify(asus, orig_code);
2095 if (code == NOTIFY_KBD_BRTUP) {
2096 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2099 if (code == NOTIFY_KBD_BRTDWN) {
2100 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
2103 if (code == NOTIFY_KBD_BRTTOGGLE) {
2104 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
2105 kbd_led_set_by_kbd(asus, 0);
2107 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2111 if (code == NOTIFY_FNLOCK_TOGGLE) {
2112 asus->fnlock_locked = !asus->fnlock_locked;
2113 asus_wmi_fnlock_update(asus);
2117 if (code == NOTIFY_KBD_DOCK_CHANGE) {
2118 result = asus_wmi_get_devstate_simple(asus,
2119 ASUS_WMI_DEVID_KBD_DOCK);
2121 input_report_switch(asus->inputdev, SW_TABLET_MODE,
2123 input_sync(asus->inputdev);
2128 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2129 fan_boost_mode_switch_next(asus);
2133 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2134 throttle_thermal_policy_switch_next(asus);
2138 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2141 if (!sparse_keymap_report_event(asus->inputdev, code,
2142 key_value, autorelease))
2143 pr_info("Unknown key %x pressed\n", code);
2146 static void asus_wmi_notify(u32 value, void *context)
2148 struct asus_wmi *asus = context;
2152 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2153 code = asus_wmi_get_event_code(value);
2155 pr_warn("Failed to get notify code: %d\n", code);
2159 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2162 asus_wmi_handle_event_code(code, asus);
2165 * Double check that queue is present:
2166 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2168 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2172 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2175 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2180 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2181 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2183 pr_warn("Failed to get event during flush: %d\n", code);
2187 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2191 pr_warn("Failed to flush event queue\n");
2195 /* Sysfs **********************************************************************/
2197 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2198 const char *buf, size_t count)
2203 value = asus_wmi_get_devstate_simple(asus, devid);
2207 err = kstrtoint(buf, 0, &value);
2211 err = asus_wmi_set_devstate(devid, value, &retval);
2218 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2220 int value = asus_wmi_get_devstate_simple(asus, devid);
2225 return sprintf(buf, "%d\n", value);
2228 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
2229 static ssize_t show_##_name(struct device *dev, \
2230 struct device_attribute *attr, \
2233 struct asus_wmi *asus = dev_get_drvdata(dev); \
2235 return show_sys_wmi(asus, _cm, buf); \
2237 static ssize_t store_##_name(struct device *dev, \
2238 struct device_attribute *attr, \
2239 const char *buf, size_t count) \
2241 struct asus_wmi *asus = dev_get_drvdata(dev); \
2243 return store_sys_wmi(asus, _cm, buf, count); \
2245 static struct device_attribute dev_attr_##_name = { \
2247 .name = __stringify(_name), \
2249 .show = show_##_name, \
2250 .store = store_##_name, \
2253 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2254 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2255 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2256 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2257 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2259 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2260 const char *buf, size_t count)
2264 rv = kstrtoint(buf, 0, &value);
2268 if (value < 0 || value > 2)
2271 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2278 static DEVICE_ATTR_WO(cpufv);
2280 static struct attribute *platform_attributes[] = {
2281 &dev_attr_cpufv.attr,
2282 &dev_attr_camera.attr,
2283 &dev_attr_cardr.attr,
2284 &dev_attr_touchpad.attr,
2285 &dev_attr_lid_resume.attr,
2286 &dev_attr_als_enable.attr,
2287 &dev_attr_fan_boost_mode.attr,
2288 &dev_attr_throttle_thermal_policy.attr,
2292 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2293 struct attribute *attr, int idx)
2295 struct device *dev = container_of(kobj, struct device, kobj);
2296 struct asus_wmi *asus = dev_get_drvdata(dev);
2300 if (attr == &dev_attr_camera.attr)
2301 devid = ASUS_WMI_DEVID_CAMERA;
2302 else if (attr == &dev_attr_cardr.attr)
2303 devid = ASUS_WMI_DEVID_CARDREADER;
2304 else if (attr == &dev_attr_touchpad.attr)
2305 devid = ASUS_WMI_DEVID_TOUCHPAD;
2306 else if (attr == &dev_attr_lid_resume.attr)
2307 devid = ASUS_WMI_DEVID_LID_RESUME;
2308 else if (attr == &dev_attr_als_enable.attr)
2309 devid = ASUS_WMI_DEVID_ALS_ENABLE;
2310 else if (attr == &dev_attr_fan_boost_mode.attr)
2311 ok = asus->fan_boost_mode_available;
2312 else if (attr == &dev_attr_throttle_thermal_policy.attr)
2313 ok = asus->throttle_thermal_policy_available;
2316 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2318 return ok ? attr->mode : 0;
2321 static const struct attribute_group platform_attribute_group = {
2322 .is_visible = asus_sysfs_is_visible,
2323 .attrs = platform_attributes
2326 static void asus_wmi_sysfs_exit(struct platform_device *device)
2328 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2331 static int asus_wmi_sysfs_init(struct platform_device *device)
2333 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2336 /* Platform device ************************************************************/
2338 static int asus_wmi_platform_init(struct asus_wmi *asus)
2340 struct device *dev = &asus->platform_device->dev;
2344 /* INIT enable hotkeys on some models */
2345 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2346 pr_info("Initialization: %#x\n", rv);
2348 /* We don't know yet what to do with this version... */
2349 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2350 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2355 * The SFUN method probably allows the original driver to get the list
2356 * of features supported by a given model. For now, 0x0100 or 0x0800
2357 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2358 * The significance of others is yet to be found.
2360 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2361 pr_info("SFUN value: %#x\n", rv);
2366 * Eee PC and Notebooks seems to have different method_id for DSTS,
2367 * but it may also be related to the BIOS's SPEC.
2368 * Note, on most Eeepc, there is no way to check if a method exist
2369 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2370 * but once again, SPEC may probably be used for that kind of things.
2372 * Additionally at least TUF Gaming series laptops return nothing for
2373 * unknown methods, so the detection in this way is not possible.
2375 * There is strong indication that only ACPI WMI devices that have _UID
2376 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2378 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2382 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2383 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2384 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2386 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2387 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2391 * Some devices can have multiple event codes stored in a queue before
2392 * the module load if it was unloaded intermittently after calling
2393 * the INIT method (enables event handling). The WMI notify handler is
2394 * expected to retrieve all event codes until a retrieved code equals
2395 * queue end marker (One or Ones). Old codes are flushed from the queue
2396 * upon module load. Not enabling this when it should be has minimal
2397 * visible impact so fall back if anything goes wrong.
2399 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2400 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2401 dev_info(dev, "Detected ATK, enable event queue\n");
2403 if (!asus_wmi_notify_queue_flush(asus))
2404 asus->wmi_event_queue = true;
2407 /* CWAP allow to define the behavior of the Fn+F2 key,
2408 * this method doesn't seems to be present on Eee PCs */
2409 if (asus->driver->quirks->wapf >= 0)
2410 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2411 asus->driver->quirks->wapf, NULL);
2416 /* debugfs ********************************************************************/
2418 struct asus_wmi_debugfs_node {
2419 struct asus_wmi *asus;
2421 int (*show) (struct seq_file *m, void *data);
2424 static int show_dsts(struct seq_file *m, void *data)
2426 struct asus_wmi *asus = m->private;
2430 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2434 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2439 static int show_devs(struct seq_file *m, void *data)
2441 struct asus_wmi *asus = m->private;
2445 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2450 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2451 asus->debug.ctrl_param, retval);
2456 static int show_call(struct seq_file *m, void *data)
2458 struct asus_wmi *asus = m->private;
2459 struct bios_args args = {
2460 .arg0 = asus->debug.dev_id,
2461 .arg1 = asus->debug.ctrl_param,
2463 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2464 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2465 union acpi_object *obj;
2468 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2469 0, asus->debug.method_id,
2472 if (ACPI_FAILURE(status))
2475 obj = (union acpi_object *)output.pointer;
2476 if (obj && obj->type == ACPI_TYPE_INTEGER)
2477 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2478 asus->debug.dev_id, asus->debug.ctrl_param,
2479 (u32) obj->integer.value);
2481 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2482 asus->debug.dev_id, asus->debug.ctrl_param,
2483 obj ? obj->type : -1);
2490 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2491 {NULL, "devs", show_devs},
2492 {NULL, "dsts", show_dsts},
2493 {NULL, "call", show_call},
2496 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2498 struct asus_wmi_debugfs_node *node = inode->i_private;
2500 return single_open(file, node->show, node->asus);
2503 static const struct file_operations asus_wmi_debugfs_io_ops = {
2504 .owner = THIS_MODULE,
2505 .open = asus_wmi_debugfs_open,
2507 .llseek = seq_lseek,
2508 .release = single_release,
2511 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2513 debugfs_remove_recursive(asus->debug.root);
2516 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2520 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2522 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2523 &asus->debug.method_id);
2525 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2526 &asus->debug.dev_id);
2528 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2529 &asus->debug.ctrl_param);
2531 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2532 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2535 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2536 asus->debug.root, node,
2537 &asus_wmi_debugfs_io_ops);
2541 /* Init / exit ****************************************************************/
2543 static int asus_wmi_add(struct platform_device *pdev)
2545 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2546 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2547 struct asus_wmi *asus;
2548 const char *chassis_type;
2553 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2557 asus->driver = wdrv;
2558 asus->platform_device = pdev;
2559 wdrv->platform_device = pdev;
2560 platform_set_drvdata(asus->platform_device, asus);
2562 if (wdrv->detect_quirks)
2563 wdrv->detect_quirks(asus->driver);
2565 err = asus_wmi_platform_init(asus);
2569 err = fan_boost_mode_check_present(asus);
2571 goto fail_fan_boost_mode;
2573 err = throttle_thermal_policy_check_present(asus);
2575 goto fail_throttle_thermal_policy;
2577 throttle_thermal_policy_set_default(asus);
2579 err = asus_wmi_sysfs_init(asus->platform_device);
2583 err = asus_wmi_input_init(asus);
2587 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2589 err = asus_wmi_hwmon_init(asus);
2593 err = asus_wmi_led_init(asus);
2597 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2598 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2599 asus->driver->wlan_ctrl_by_user = 1;
2601 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2602 err = asus_wmi_rfkill_init(asus);
2607 if (asus->driver->quirks->wmi_force_als_set)
2610 /* Some Asus desktop boards export an acpi-video backlight interface,
2611 stop this from showing up */
2612 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2613 if (chassis_type && !strcmp(chassis_type, "3"))
2614 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2616 if (asus->driver->quirks->wmi_backlight_power)
2617 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2619 if (asus->driver->quirks->wmi_backlight_native)
2620 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2622 if (asus->driver->quirks->xusb2pr)
2623 asus_wmi_set_xusb2pr(asus);
2625 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2626 err = asus_wmi_backlight_init(asus);
2627 if (err && err != -ENODEV)
2628 goto fail_backlight;
2629 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
2630 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2632 if (asus_wmi_has_fnlock_key(asus)) {
2633 asus->fnlock_locked = true;
2634 asus_wmi_fnlock_update(asus);
2637 status = wmi_install_notify_handler(asus->driver->event_guid,
2638 asus_wmi_notify, asus);
2639 if (ACPI_FAILURE(status)) {
2640 pr_err("Unable to register notify handler - %d\n", status);
2642 goto fail_wmi_handler;
2645 asus_wmi_battery_init(asus);
2647 asus_wmi_debugfs_init(asus);
2652 asus_wmi_backlight_exit(asus);
2654 asus_wmi_rfkill_exit(asus);
2656 asus_wmi_led_exit(asus);
2659 asus_wmi_input_exit(asus);
2661 asus_wmi_sysfs_exit(asus->platform_device);
2663 fail_throttle_thermal_policy:
2664 fail_fan_boost_mode:
2670 static int asus_wmi_remove(struct platform_device *device)
2672 struct asus_wmi *asus;
2674 asus = platform_get_drvdata(device);
2675 wmi_remove_notify_handler(asus->driver->event_guid);
2676 asus_wmi_backlight_exit(asus);
2677 asus_wmi_input_exit(asus);
2678 asus_wmi_led_exit(asus);
2679 asus_wmi_rfkill_exit(asus);
2680 asus_wmi_debugfs_exit(asus);
2681 asus_wmi_sysfs_exit(asus->platform_device);
2682 asus_fan_set_auto(asus);
2683 asus_wmi_battery_exit(asus);
2689 /* Platform driver - hibernate/resume callbacks *******************************/
2691 static int asus_hotk_thaw(struct device *device)
2693 struct asus_wmi *asus = dev_get_drvdata(device);
2695 if (asus->wlan.rfkill) {
2699 * Work around bios bug - acpi _PTS turns off the wireless led
2700 * during suspend. Normally it restores it on resume, but
2701 * we should kick it ourselves in case hibernation is aborted.
2703 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2704 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2710 static int asus_hotk_resume(struct device *device)
2712 struct asus_wmi *asus = dev_get_drvdata(device);
2714 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2715 kbd_led_update(asus);
2717 if (asus_wmi_has_fnlock_key(asus))
2718 asus_wmi_fnlock_update(asus);
2722 static int asus_hotk_restore(struct device *device)
2724 struct asus_wmi *asus = dev_get_drvdata(device);
2727 /* Refresh both wlan rfkill state and pci hotplug */
2728 if (asus->wlan.rfkill)
2729 asus_rfkill_hotplug(asus);
2731 if (asus->bluetooth.rfkill) {
2732 bl = !asus_wmi_get_devstate_simple(asus,
2733 ASUS_WMI_DEVID_BLUETOOTH);
2734 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2736 if (asus->wimax.rfkill) {
2737 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2738 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2740 if (asus->wwan3g.rfkill) {
2741 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2742 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2744 if (asus->gps.rfkill) {
2745 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2746 rfkill_set_sw_state(asus->gps.rfkill, bl);
2748 if (asus->uwb.rfkill) {
2749 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2750 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2752 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2753 kbd_led_update(asus);
2755 if (asus_wmi_has_fnlock_key(asus))
2756 asus_wmi_fnlock_update(asus);
2760 static const struct dev_pm_ops asus_pm_ops = {
2761 .thaw = asus_hotk_thaw,
2762 .restore = asus_hotk_restore,
2763 .resume = asus_hotk_resume,
2766 /* Registration ***************************************************************/
2768 static int asus_wmi_probe(struct platform_device *pdev)
2770 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2771 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2774 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2775 pr_warn("ASUS Management GUID not found\n");
2779 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2780 pr_warn("ASUS Event GUID not found\n");
2785 ret = wdrv->probe(pdev);
2790 return asus_wmi_add(pdev);
2795 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2797 struct platform_driver *platform_driver;
2798 struct platform_device *platform_device;
2803 platform_driver = &driver->platform_driver;
2804 platform_driver->remove = asus_wmi_remove;
2805 platform_driver->driver.owner = driver->owner;
2806 platform_driver->driver.name = driver->name;
2807 platform_driver->driver.pm = &asus_pm_ops;
2809 platform_device = platform_create_bundle(platform_driver,
2812 if (IS_ERR(platform_device))
2813 return PTR_ERR(platform_device);
2818 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2820 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2822 platform_device_unregister(driver->platform_device);
2823 platform_driver_unregister(&driver->platform_driver);
2826 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2828 static int __init asus_wmi_init(void)
2830 pr_info("ASUS WMI generic driver loaded\n");
2834 static void __exit asus_wmi_exit(void)
2836 pr_info("ASUS WMI generic driver unloaded\n");
2839 module_init(asus_wmi_init);
2840 module_exit(asus_wmi_exit);