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
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, "BATC") != 0 &&
446 strcmp(battery->desc->name, "BATT") != 0)
449 if (device_create_file(&battery->dev,
450 &dev_attr_charge_control_end_threshold))
453 /* The charge threshold is only reset when the system is power cycled,
454 * and we can't get the current threshold so let set it to 100% when
455 * a battery is added.
457 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
458 charge_end_threshold = 100;
463 static int asus_wmi_battery_remove(struct power_supply *battery)
465 device_remove_file(&battery->dev,
466 &dev_attr_charge_control_end_threshold);
470 static struct acpi_battery_hook battery_hook = {
471 .add_battery = asus_wmi_battery_add,
472 .remove_battery = asus_wmi_battery_remove,
473 .name = "ASUS Battery Extension",
476 static void asus_wmi_battery_init(struct asus_wmi *asus)
478 asus->battery_rsoc_available = false;
479 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
480 asus->battery_rsoc_available = true;
481 battery_hook_register(&battery_hook);
485 static void asus_wmi_battery_exit(struct asus_wmi *asus)
487 if (asus->battery_rsoc_available)
488 battery_hook_unregister(&battery_hook);
491 /* LEDs ***********************************************************************/
494 * These functions actually update the LED's, and are called from a
495 * workqueue. By doing this as separate work rather than when the LED
496 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
497 * potentially bad time, such as a timer interrupt.
499 static void tpd_led_update(struct work_struct *work)
502 struct asus_wmi *asus;
504 asus = container_of(work, struct asus_wmi, tpd_led_work);
506 ctrl_param = asus->tpd_led_wk;
507 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
510 static void tpd_led_set(struct led_classdev *led_cdev,
511 enum led_brightness value)
513 struct asus_wmi *asus;
515 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
517 asus->tpd_led_wk = !!value;
518 queue_work(asus->led_workqueue, &asus->tpd_led_work);
521 static int read_tpd_led_state(struct asus_wmi *asus)
523 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
526 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
528 struct asus_wmi *asus;
530 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
532 return read_tpd_led_state(asus);
535 static void kbd_led_update(struct asus_wmi *asus)
539 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
540 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
543 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
549 * bit 7: light on/off
550 * bit 8-10: environment (0: dark, 1: normal, 2: light)
551 * bit 17: status unknown
553 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
556 /* Unknown status is considered as off */
557 if (retval == 0x8000)
564 *level = retval & 0x7F;
566 *env = (retval >> 8) & 0x7F;
570 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
572 struct asus_wmi *asus;
575 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
576 max_level = asus->kbd_led.max_brightness;
578 asus->kbd_led_wk = clamp_val(value, 0, max_level);
579 kbd_led_update(asus);
582 static void kbd_led_set(struct led_classdev *led_cdev,
583 enum led_brightness value)
585 /* Prevent disabling keyboard backlight on module unregister */
586 if (led_cdev->flags & LED_UNREGISTERING)
589 do_kbd_led_set(led_cdev, value);
592 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
594 struct led_classdev *led_cdev = &asus->kbd_led;
596 do_kbd_led_set(led_cdev, value);
597 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
600 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
602 struct asus_wmi *asus;
605 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
607 retval = kbd_led_read(asus, &value, NULL);
614 static int wlan_led_unknown_state(struct asus_wmi *asus)
618 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
620 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
623 static void wlan_led_update(struct work_struct *work)
626 struct asus_wmi *asus;
628 asus = container_of(work, struct asus_wmi, wlan_led_work);
630 ctrl_param = asus->wlan_led_wk;
631 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
634 static void wlan_led_set(struct led_classdev *led_cdev,
635 enum led_brightness value)
637 struct asus_wmi *asus;
639 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
641 asus->wlan_led_wk = !!value;
642 queue_work(asus->led_workqueue, &asus->wlan_led_work);
645 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
647 struct asus_wmi *asus;
650 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
651 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
653 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
656 static void lightbar_led_update(struct work_struct *work)
658 struct asus_wmi *asus;
661 asus = container_of(work, struct asus_wmi, lightbar_led_work);
663 ctrl_param = asus->lightbar_led_wk;
664 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
667 static void lightbar_led_set(struct led_classdev *led_cdev,
668 enum led_brightness value)
670 struct asus_wmi *asus;
672 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
674 asus->lightbar_led_wk = !!value;
675 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
678 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
680 struct asus_wmi *asus;
683 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
684 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
686 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
689 static void asus_wmi_led_exit(struct asus_wmi *asus)
691 led_classdev_unregister(&asus->kbd_led);
692 led_classdev_unregister(&asus->tpd_led);
693 led_classdev_unregister(&asus->wlan_led);
694 led_classdev_unregister(&asus->lightbar_led);
696 if (asus->led_workqueue)
697 destroy_workqueue(asus->led_workqueue);
700 static int asus_wmi_led_init(struct asus_wmi *asus)
704 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
705 if (!asus->led_workqueue)
708 if (read_tpd_led_state(asus) >= 0) {
709 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
711 asus->tpd_led.name = "asus::touchpad";
712 asus->tpd_led.brightness_set = tpd_led_set;
713 asus->tpd_led.brightness_get = tpd_led_get;
714 asus->tpd_led.max_brightness = 1;
716 rv = led_classdev_register(&asus->platform_device->dev,
722 if (!kbd_led_read(asus, &led_val, NULL)) {
723 asus->kbd_led_wk = led_val;
724 asus->kbd_led.name = "asus::kbd_backlight";
725 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
726 asus->kbd_led.brightness_set = kbd_led_set;
727 asus->kbd_led.brightness_get = kbd_led_get;
728 asus->kbd_led.max_brightness = 3;
730 rv = led_classdev_register(&asus->platform_device->dev,
736 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
737 && (asus->driver->quirks->wapf > 0)) {
738 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
740 asus->wlan_led.name = "asus::wlan";
741 asus->wlan_led.brightness_set = wlan_led_set;
742 if (!wlan_led_unknown_state(asus))
743 asus->wlan_led.brightness_get = wlan_led_get;
744 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
745 asus->wlan_led.max_brightness = 1;
746 asus->wlan_led.default_trigger = "asus-wlan";
748 rv = led_classdev_register(&asus->platform_device->dev,
754 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
755 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
757 asus->lightbar_led.name = "asus::lightbar";
758 asus->lightbar_led.brightness_set = lightbar_led_set;
759 asus->lightbar_led.brightness_get = lightbar_led_get;
760 asus->lightbar_led.max_brightness = 1;
762 rv = led_classdev_register(&asus->platform_device->dev,
763 &asus->lightbar_led);
768 asus_wmi_led_exit(asus);
773 /* RF *************************************************************************/
776 * PCI hotplug (for wlan rfkill)
778 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
780 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
787 static void asus_rfkill_hotplug(struct asus_wmi *asus)
795 mutex_lock(&asus->wmi_lock);
796 blocked = asus_wlan_rfkill_blocked(asus);
797 mutex_unlock(&asus->wmi_lock);
799 mutex_lock(&asus->hotplug_lock);
800 pci_lock_rescan_remove();
802 if (asus->wlan.rfkill)
803 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
805 if (asus->hotplug_slot.ops) {
806 bus = pci_find_bus(0, 1);
808 pr_warn("Unable to find PCI bus 1?\n");
812 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
813 pr_err("Unable to read PCI config space?\n");
816 absent = (l == 0xffffffff);
818 if (blocked != absent) {
819 pr_warn("BIOS says wireless lan is %s, "
820 "but the pci device is %s\n",
821 blocked ? "blocked" : "unblocked",
822 absent ? "absent" : "present");
823 pr_warn("skipped wireless hotplug as probably "
824 "inappropriate for this model\n");
829 dev = pci_get_slot(bus, 0);
831 /* Device already present */
835 dev = pci_scan_single_device(bus, 0);
837 pci_bus_assign_resources(bus);
838 pci_bus_add_device(dev);
841 dev = pci_get_slot(bus, 0);
843 pci_stop_and_remove_bus_device(dev);
850 pci_unlock_rescan_remove();
851 mutex_unlock(&asus->hotplug_lock);
854 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
856 struct asus_wmi *asus = data;
858 if (event != ACPI_NOTIFY_BUS_CHECK)
862 * We can't call directly asus_rfkill_hotplug because most
863 * of the time WMBC is still being executed and not reetrant.
864 * There is currently no way to tell ACPICA that we want this
865 * method to be serialized, we schedule a asus_rfkill_hotplug
866 * call later, in a safer context.
868 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
871 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
876 status = acpi_get_handle(NULL, node, &handle);
877 if (ACPI_FAILURE(status))
880 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
881 asus_rfkill_notify, asus);
882 if (ACPI_FAILURE(status))
883 pr_warn("Failed to register notify on %s\n", node);
888 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
890 acpi_status status = AE_OK;
893 status = acpi_get_handle(NULL, node, &handle);
894 if (ACPI_FAILURE(status))
897 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
899 if (ACPI_FAILURE(status))
900 pr_err("Error removing rfkill notify handler %s\n", node);
903 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
906 struct asus_wmi *asus = container_of(hotplug_slot,
907 struct asus_wmi, hotplug_slot);
908 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
917 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
918 .get_adapter_status = asus_get_adapter_status,
919 .get_power_status = asus_get_adapter_status,
922 static void asus_hotplug_work(struct work_struct *work)
924 struct asus_wmi *asus;
926 asus = container_of(work, struct asus_wmi, hotplug_work);
927 asus_rfkill_hotplug(asus);
930 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
933 struct pci_bus *bus = pci_find_bus(0, 1);
936 pr_err("Unable to find wifi PCI bus\n");
940 asus->hotplug_workqueue =
941 create_singlethread_workqueue("hotplug_workqueue");
942 if (!asus->hotplug_workqueue)
943 goto error_workqueue;
945 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
947 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
949 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
951 pr_err("Unable to register hotplug slot - %d\n", ret);
958 asus->hotplug_slot.ops = NULL;
959 destroy_workqueue(asus->hotplug_workqueue);
967 static int asus_rfkill_set(void *data, bool blocked)
969 struct asus_rfkill *priv = data;
970 u32 ctrl_param = !blocked;
971 u32 dev_id = priv->dev_id;
974 * If the user bit is set, BIOS can't set and record the wlan status,
975 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
976 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
977 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
978 * while setting the wlan status through WMI.
979 * This is also the behavior that windows app will do.
981 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
982 priv->asus->driver->wlan_ctrl_by_user)
983 dev_id = ASUS_WMI_DEVID_WLAN_LED;
985 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
988 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
990 struct asus_rfkill *priv = data;
993 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
998 rfkill_set_sw_state(priv->rfkill, !result);
1001 static int asus_rfkill_wlan_set(void *data, bool blocked)
1003 struct asus_rfkill *priv = data;
1004 struct asus_wmi *asus = priv->asus;
1008 * This handler is enabled only if hotplug is enabled.
1009 * In this case, the asus_wmi_set_devstate() will
1010 * trigger a wmi notification and we need to wait
1011 * this call to finish before being able to call
1014 mutex_lock(&asus->wmi_lock);
1015 ret = asus_rfkill_set(data, blocked);
1016 mutex_unlock(&asus->wmi_lock);
1020 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1021 .set_block = asus_rfkill_wlan_set,
1022 .query = asus_rfkill_query,
1025 static const struct rfkill_ops asus_rfkill_ops = {
1026 .set_block = asus_rfkill_set,
1027 .query = asus_rfkill_query,
1030 static int asus_new_rfkill(struct asus_wmi *asus,
1031 struct asus_rfkill *arfkill,
1032 const char *name, enum rfkill_type type, int dev_id)
1034 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1035 struct rfkill **rfkill = &arfkill->rfkill;
1040 arfkill->dev_id = dev_id;
1041 arfkill->asus = asus;
1043 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1044 asus->driver->quirks->hotplug_wireless)
1045 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1046 &asus_rfkill_wlan_ops, arfkill);
1048 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1049 &asus_rfkill_ops, arfkill);
1054 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1055 (asus->driver->quirks->wapf > 0))
1056 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1058 rfkill_init_sw_state(*rfkill, !result);
1059 result = rfkill_register(*rfkill);
1061 rfkill_destroy(*rfkill);
1068 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1070 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1073 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1074 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1075 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1076 if (asus->wlan.rfkill) {
1077 rfkill_unregister(asus->wlan.rfkill);
1078 rfkill_destroy(asus->wlan.rfkill);
1079 asus->wlan.rfkill = NULL;
1082 * Refresh pci hotplug in case the rfkill state was changed after
1083 * asus_unregister_rfkill_notifier()
1085 asus_rfkill_hotplug(asus);
1086 if (asus->hotplug_slot.ops)
1087 pci_hp_deregister(&asus->hotplug_slot);
1088 if (asus->hotplug_workqueue)
1089 destroy_workqueue(asus->hotplug_workqueue);
1091 if (asus->bluetooth.rfkill) {
1092 rfkill_unregister(asus->bluetooth.rfkill);
1093 rfkill_destroy(asus->bluetooth.rfkill);
1094 asus->bluetooth.rfkill = NULL;
1096 if (asus->wimax.rfkill) {
1097 rfkill_unregister(asus->wimax.rfkill);
1098 rfkill_destroy(asus->wimax.rfkill);
1099 asus->wimax.rfkill = NULL;
1101 if (asus->wwan3g.rfkill) {
1102 rfkill_unregister(asus->wwan3g.rfkill);
1103 rfkill_destroy(asus->wwan3g.rfkill);
1104 asus->wwan3g.rfkill = NULL;
1106 if (asus->gps.rfkill) {
1107 rfkill_unregister(asus->gps.rfkill);
1108 rfkill_destroy(asus->gps.rfkill);
1109 asus->gps.rfkill = NULL;
1111 if (asus->uwb.rfkill) {
1112 rfkill_unregister(asus->uwb.rfkill);
1113 rfkill_destroy(asus->uwb.rfkill);
1114 asus->uwb.rfkill = NULL;
1118 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1122 mutex_init(&asus->hotplug_lock);
1123 mutex_init(&asus->wmi_lock);
1125 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1126 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1128 if (result && result != -ENODEV)
1131 result = asus_new_rfkill(asus, &asus->bluetooth,
1132 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1133 ASUS_WMI_DEVID_BLUETOOTH);
1135 if (result && result != -ENODEV)
1138 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1139 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1141 if (result && result != -ENODEV)
1144 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1145 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1147 if (result && result != -ENODEV)
1150 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1151 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1153 if (result && result != -ENODEV)
1156 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1157 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1159 if (result && result != -ENODEV)
1162 if (!asus->driver->quirks->hotplug_wireless)
1165 result = asus_setup_pci_hotplug(asus);
1167 * If we get -EBUSY then something else is handling the PCI hotplug -
1168 * don't fail in this case
1170 if (result == -EBUSY)
1173 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1174 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1175 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1177 * Refresh pci hotplug in case the rfkill state was changed during
1180 asus_rfkill_hotplug(asus);
1183 if (result && result != -ENODEV)
1184 asus_wmi_rfkill_exit(asus);
1186 if (result == -ENODEV)
1192 /* Quirks *********************************************************************/
1194 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1196 struct pci_dev *xhci_pdev;
1197 u32 orig_ports_available;
1198 u32 ports_available = asus->driver->quirks->xusb2pr;
1200 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1201 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1207 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1208 &orig_ports_available);
1210 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1211 cpu_to_le32(ports_available));
1213 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1214 orig_ports_available, ports_available);
1218 * Some devices dont support or have borcken get_als method
1219 * but still support set method.
1221 static void asus_wmi_set_als(void)
1223 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1226 /* Hwmon device ***************************************************************/
1228 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1231 struct agfn_fan_args args = {
1232 .agfn.len = sizeof(args),
1233 .agfn.mfun = ASUS_FAN_MFUN,
1234 .agfn.sfun = ASUS_FAN_SFUN_READ,
1238 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1244 status = asus_wmi_evaluate_method_agfn(input);
1246 if (status || args.agfn.err)
1250 *speed = args.speed;
1255 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1258 struct agfn_fan_args args = {
1259 .agfn.len = sizeof(args),
1260 .agfn.mfun = ASUS_FAN_MFUN,
1261 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1263 .speed = speed ? *speed : 0,
1265 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1268 /* 1: for setting 1st fan's speed 0: setting auto mode */
1269 if (fan != 1 && fan != 0)
1272 status = asus_wmi_evaluate_method_agfn(input);
1274 if (status || args.agfn.err)
1277 if (speed && fan == 1)
1278 asus->agfn_pwm = *speed;
1284 * Check if we can read the speed of one fan. If true we assume we can also
1287 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1293 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1297 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1302 * We need to find a better way, probably using sfun,
1304 * Currently we disable it if:
1305 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1306 * - reverved bits are non-zero
1307 * - sfun and presence bit are not set
1309 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1310 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1313 static int asus_fan_set_auto(struct asus_wmi *asus)
1318 switch (asus->fan_type) {
1319 case FAN_TYPE_SPEC83:
1320 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1330 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1343 static ssize_t pwm1_show(struct device *dev,
1344 struct device_attribute *attr,
1347 struct asus_wmi *asus = dev_get_drvdata(dev);
1351 /* If we already set a value then just return it */
1352 if (asus->agfn_pwm >= 0)
1353 return sprintf(buf, "%d\n", asus->agfn_pwm);
1356 * If we haven't set already set a value through the AGFN interface,
1357 * we read a current value through the (now-deprecated) FAN_CTRL device.
1359 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1365 if (value == 1) /* Low Speed */
1367 else if (value == 2)
1369 else if (value == 3)
1372 pr_err("Unknown fan speed %#x\n", value);
1376 return sprintf(buf, "%d\n", value);
1379 static ssize_t pwm1_store(struct device *dev,
1380 struct device_attribute *attr,
1381 const char *buf, size_t count) {
1382 struct asus_wmi *asus = dev_get_drvdata(dev);
1387 ret = kstrtouint(buf, 10, &value);
1391 value = clamp(value, 0, 255);
1393 state = asus_agfn_fan_speed_write(asus, 1, &value);
1395 pr_warn("Setting fan speed failed: %d\n", state);
1397 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1402 static ssize_t fan1_input_show(struct device *dev,
1403 struct device_attribute *attr,
1406 struct asus_wmi *asus = dev_get_drvdata(dev);
1410 switch (asus->fan_type) {
1411 case FAN_TYPE_SPEC83:
1412 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1421 /* no speed readable on manual mode */
1422 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1425 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1427 pr_warn("reading fan speed failed: %d\n", ret);
1436 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1439 static ssize_t pwm1_enable_show(struct device *dev,
1440 struct device_attribute *attr,
1443 struct asus_wmi *asus = dev_get_drvdata(dev);
1446 * Just read back the cached pwm mode.
1448 * For the CPU_FAN device, the spec indicates that we should be
1449 * able to read the device status and consult bit 19 to see if we
1450 * are in Full On or Automatic mode. However, this does not work
1451 * in practice on X532FL at least (the bit is always 0) and there's
1452 * also nothing in the DSDT to indicate that this behaviour exists.
1454 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1457 static ssize_t pwm1_enable_store(struct device *dev,
1458 struct device_attribute *attr,
1459 const char *buf, size_t count)
1461 struct asus_wmi *asus = dev_get_drvdata(dev);
1468 ret = kstrtouint(buf, 10, &state);
1472 if (asus->fan_type == FAN_TYPE_SPEC83) {
1473 switch (state) { /* standard documented hwmon values */
1474 case ASUS_FAN_CTRL_FULLSPEED:
1477 case ASUS_FAN_CTRL_AUTO:
1484 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1491 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1493 case ASUS_FAN_CTRL_MANUAL:
1496 case ASUS_FAN_CTRL_AUTO:
1497 status = asus_fan_set_auto(asus);
1507 asus->fan_pwm_mode = state;
1511 static ssize_t fan1_label_show(struct device *dev,
1512 struct device_attribute *attr,
1515 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1518 static ssize_t asus_hwmon_temp1(struct device *dev,
1519 struct device_attribute *attr,
1522 struct asus_wmi *asus = dev_get_drvdata(dev);
1526 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1530 return sprintf(buf, "%ld\n",
1531 deci_kelvin_to_millicelsius(value & 0xFFFF));
1535 static DEVICE_ATTR_RW(pwm1);
1536 static DEVICE_ATTR_RW(pwm1_enable);
1537 static DEVICE_ATTR_RO(fan1_input);
1538 static DEVICE_ATTR_RO(fan1_label);
1541 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1543 static struct attribute *hwmon_attributes[] = {
1544 &dev_attr_pwm1.attr,
1545 &dev_attr_pwm1_enable.attr,
1546 &dev_attr_fan1_input.attr,
1547 &dev_attr_fan1_label.attr,
1549 &dev_attr_temp1_input.attr,
1553 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1554 struct attribute *attr, int idx)
1556 struct device *dev = container_of(kobj, struct device, kobj);
1557 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1558 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1560 if (attr == &dev_attr_pwm1.attr) {
1561 if (asus->fan_type != FAN_TYPE_AGFN)
1563 } else if (attr == &dev_attr_fan1_input.attr
1564 || attr == &dev_attr_fan1_label.attr
1565 || attr == &dev_attr_pwm1_enable.attr) {
1566 if (asus->fan_type == FAN_TYPE_NONE)
1568 } else if (attr == &dev_attr_temp1_input.attr) {
1569 int err = asus_wmi_get_devstate(asus,
1570 ASUS_WMI_DEVID_THERMAL_CTRL,
1574 return 0; /* can't return negative here */
1577 * If the temperature value in deci-Kelvin is near the absolute
1578 * zero temperature, something is clearly wrong
1580 if (value == 0 || value == 1)
1587 static const struct attribute_group hwmon_attribute_group = {
1588 .is_visible = asus_hwmon_sysfs_is_visible,
1589 .attrs = hwmon_attributes
1591 __ATTRIBUTE_GROUPS(hwmon_attribute);
1593 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1595 struct device *dev = &asus->platform_device->dev;
1596 struct device *hwmon;
1598 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1599 hwmon_attribute_groups);
1601 if (IS_ERR(hwmon)) {
1602 pr_err("Could not register asus hwmon device\n");
1603 return PTR_ERR(hwmon);
1608 static int asus_wmi_fan_init(struct asus_wmi *asus)
1610 asus->fan_type = FAN_TYPE_NONE;
1611 asus->agfn_pwm = -1;
1613 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1614 asus->fan_type = FAN_TYPE_SPEC83;
1615 else if (asus_wmi_has_agfn_fan(asus))
1616 asus->fan_type = FAN_TYPE_AGFN;
1618 if (asus->fan_type == FAN_TYPE_NONE)
1621 asus_fan_set_auto(asus);
1622 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1626 /* Fan mode *******************************************************************/
1628 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1633 asus->fan_boost_mode_available = false;
1635 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1644 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1645 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1646 asus->fan_boost_mode_available = true;
1647 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1653 static int fan_boost_mode_write(struct asus_wmi *asus)
1659 value = asus->fan_boost_mode;
1661 pr_info("Set fan boost mode: %u\n", value);
1662 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1665 pr_warn("Failed to set fan boost mode: %d\n", err);
1670 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1678 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1680 u8 mask = asus->fan_boost_mode_mask;
1682 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1683 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1684 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1685 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1686 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1687 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1688 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1689 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1691 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1693 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1696 return fan_boost_mode_write(asus);
1699 static ssize_t fan_boost_mode_show(struct device *dev,
1700 struct device_attribute *attr, char *buf)
1702 struct asus_wmi *asus = dev_get_drvdata(dev);
1704 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
1707 static ssize_t fan_boost_mode_store(struct device *dev,
1708 struct device_attribute *attr,
1709 const char *buf, size_t count)
1713 struct asus_wmi *asus = dev_get_drvdata(dev);
1714 u8 mask = asus->fan_boost_mode_mask;
1716 result = kstrtou8(buf, 10, &new_mode);
1718 pr_warn("Trying to store invalid value\n");
1722 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1723 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
1725 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1726 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
1728 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
1732 asus->fan_boost_mode = new_mode;
1733 fan_boost_mode_write(asus);
1738 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1739 static DEVICE_ATTR_RW(fan_boost_mode);
1741 /* Throttle thermal policy ****************************************************/
1743 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
1748 asus->throttle_thermal_policy_available = false;
1750 err = asus_wmi_get_devstate(asus,
1751 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1759 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
1760 asus->throttle_thermal_policy_available = true;
1765 static int throttle_thermal_policy_write(struct asus_wmi *asus)
1771 value = asus->throttle_thermal_policy_mode;
1773 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1776 pr_warn("Failed to set throttle thermal policy: %d\n", err);
1781 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
1789 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
1791 if (!asus->throttle_thermal_policy_available)
1794 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1795 return throttle_thermal_policy_write(asus);
1798 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
1800 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
1802 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1803 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1805 asus->throttle_thermal_policy_mode = new_mode;
1806 return throttle_thermal_policy_write(asus);
1809 static ssize_t throttle_thermal_policy_show(struct device *dev,
1810 struct device_attribute *attr, char *buf)
1812 struct asus_wmi *asus = dev_get_drvdata(dev);
1813 u8 mode = asus->throttle_thermal_policy_mode;
1815 return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
1818 static ssize_t throttle_thermal_policy_store(struct device *dev,
1819 struct device_attribute *attr,
1820 const char *buf, size_t count)
1824 struct asus_wmi *asus = dev_get_drvdata(dev);
1826 result = kstrtou8(buf, 10, &new_mode);
1830 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1833 asus->throttle_thermal_policy_mode = new_mode;
1834 throttle_thermal_policy_write(asus);
1839 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
1840 static DEVICE_ATTR_RW(throttle_thermal_policy);
1842 /* Backlight ******************************************************************/
1844 static int read_backlight_power(struct asus_wmi *asus)
1848 if (asus->driver->quirks->store_backlight_power)
1849 ret = !asus->driver->panel_power;
1851 ret = asus_wmi_get_devstate_simple(asus,
1852 ASUS_WMI_DEVID_BACKLIGHT);
1857 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1860 static int read_brightness_max(struct asus_wmi *asus)
1865 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1869 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1878 static int read_brightness(struct backlight_device *bd)
1880 struct asus_wmi *asus = bl_get_data(bd);
1884 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1888 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1891 static u32 get_scalar_command(struct backlight_device *bd)
1893 struct asus_wmi *asus = bl_get_data(bd);
1896 if ((asus->driver->brightness < bd->props.brightness) ||
1897 bd->props.brightness == bd->props.max_brightness)
1898 ctrl_param = 0x00008001;
1899 else if ((asus->driver->brightness > bd->props.brightness) ||
1900 bd->props.brightness == 0)
1901 ctrl_param = 0x00008000;
1903 asus->driver->brightness = bd->props.brightness;
1908 static int update_bl_status(struct backlight_device *bd)
1910 struct asus_wmi *asus = bl_get_data(bd);
1914 power = read_backlight_power(asus);
1915 if (power != -ENODEV && bd->props.power != power) {
1916 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1917 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1919 if (asus->driver->quirks->store_backlight_power)
1920 asus->driver->panel_power = bd->props.power;
1922 /* When using scalar brightness, updating the brightness
1923 * will mess with the backlight power */
1924 if (asus->driver->quirks->scalar_panel_brightness)
1928 if (asus->driver->quirks->scalar_panel_brightness)
1929 ctrl_param = get_scalar_command(bd);
1931 ctrl_param = bd->props.brightness;
1933 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1939 static const struct backlight_ops asus_wmi_bl_ops = {
1940 .get_brightness = read_brightness,
1941 .update_status = update_bl_status,
1944 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1946 struct backlight_device *bd = asus->backlight_device;
1947 int old = bd->props.brightness;
1950 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1951 new = code - NOTIFY_BRNUP_MIN + 1;
1952 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1953 new = code - NOTIFY_BRNDOWN_MIN;
1955 bd->props.brightness = new;
1956 backlight_update_status(bd);
1957 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1962 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1964 struct backlight_device *bd;
1965 struct backlight_properties props;
1969 max = read_brightness_max(asus);
1973 power = read_backlight_power(asus);
1974 if (power == -ENODEV)
1975 power = FB_BLANK_UNBLANK;
1979 memset(&props, 0, sizeof(struct backlight_properties));
1980 props.type = BACKLIGHT_PLATFORM;
1981 props.max_brightness = max;
1982 bd = backlight_device_register(asus->driver->name,
1983 &asus->platform_device->dev, asus,
1984 &asus_wmi_bl_ops, &props);
1986 pr_err("Could not register backlight device\n");
1990 asus->backlight_device = bd;
1992 if (asus->driver->quirks->store_backlight_power)
1993 asus->driver->panel_power = power;
1995 bd->props.brightness = read_brightness(bd);
1996 bd->props.power = power;
1997 backlight_update_status(bd);
1999 asus->driver->brightness = bd->props.brightness;
2004 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2006 backlight_device_unregister(asus->backlight_device);
2008 asus->backlight_device = NULL;
2011 static int is_display_toggle(int code)
2013 /* display toggle keys */
2014 if ((code >= 0x61 && code <= 0x67) ||
2015 (code >= 0x8c && code <= 0x93) ||
2016 (code >= 0xa0 && code <= 0xa7) ||
2017 (code >= 0xd0 && code <= 0xd5))
2023 /* Fn-lock ********************************************************************/
2025 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2029 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2031 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2032 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2035 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2037 int mode = asus->fnlock_locked;
2039 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2042 /* WMI events *****************************************************************/
2044 static int asus_wmi_get_event_code(u32 value)
2046 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2047 union acpi_object *obj;
2051 status = wmi_get_event_data(value, &response);
2052 if (ACPI_FAILURE(status)) {
2053 pr_warn("Failed to get WMI notify code: %s\n",
2054 acpi_format_exception(status));
2058 obj = (union acpi_object *)response.pointer;
2060 if (obj && obj->type == ACPI_TYPE_INTEGER)
2061 code = (int)(obj->integer.value & WMI_EVENT_MASK);
2069 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2071 unsigned int key_value = 1;
2072 bool autorelease = 1;
2073 int result, orig_code;
2077 if (asus->driver->key_filter) {
2078 asus->driver->key_filter(asus->driver, &code, &key_value,
2080 if (code == ASUS_WMI_KEY_IGNORE)
2084 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2085 code = ASUS_WMI_BRN_UP;
2086 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2087 code = ASUS_WMI_BRN_DOWN;
2089 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
2090 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2091 asus_wmi_backlight_notify(asus, orig_code);
2096 if (code == NOTIFY_KBD_BRTUP) {
2097 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2100 if (code == NOTIFY_KBD_BRTDWN) {
2101 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
2104 if (code == NOTIFY_KBD_BRTTOGGLE) {
2105 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
2106 kbd_led_set_by_kbd(asus, 0);
2108 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2112 if (code == NOTIFY_FNLOCK_TOGGLE) {
2113 asus->fnlock_locked = !asus->fnlock_locked;
2114 asus_wmi_fnlock_update(asus);
2118 if (code == NOTIFY_KBD_DOCK_CHANGE) {
2119 result = asus_wmi_get_devstate_simple(asus,
2120 ASUS_WMI_DEVID_KBD_DOCK);
2122 input_report_switch(asus->inputdev, SW_TABLET_MODE,
2124 input_sync(asus->inputdev);
2129 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2130 fan_boost_mode_switch_next(asus);
2134 if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2135 throttle_thermal_policy_switch_next(asus);
2139 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2142 if (!sparse_keymap_report_event(asus->inputdev, code,
2143 key_value, autorelease))
2144 pr_info("Unknown key %x pressed\n", code);
2147 static void asus_wmi_notify(u32 value, void *context)
2149 struct asus_wmi *asus = context;
2153 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2154 code = asus_wmi_get_event_code(value);
2156 pr_warn("Failed to get notify code: %d\n", code);
2160 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2163 asus_wmi_handle_event_code(code, asus);
2166 * Double check that queue is present:
2167 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2169 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2173 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2176 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2181 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2182 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2184 pr_warn("Failed to get event during flush: %d\n", code);
2188 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2192 pr_warn("Failed to flush event queue\n");
2196 /* Sysfs **********************************************************************/
2198 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2199 const char *buf, size_t count)
2204 value = asus_wmi_get_devstate_simple(asus, devid);
2208 err = kstrtoint(buf, 0, &value);
2212 err = asus_wmi_set_devstate(devid, value, &retval);
2219 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2221 int value = asus_wmi_get_devstate_simple(asus, devid);
2226 return sprintf(buf, "%d\n", value);
2229 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
2230 static ssize_t show_##_name(struct device *dev, \
2231 struct device_attribute *attr, \
2234 struct asus_wmi *asus = dev_get_drvdata(dev); \
2236 return show_sys_wmi(asus, _cm, buf); \
2238 static ssize_t store_##_name(struct device *dev, \
2239 struct device_attribute *attr, \
2240 const char *buf, size_t count) \
2242 struct asus_wmi *asus = dev_get_drvdata(dev); \
2244 return store_sys_wmi(asus, _cm, buf, count); \
2246 static struct device_attribute dev_attr_##_name = { \
2248 .name = __stringify(_name), \
2250 .show = show_##_name, \
2251 .store = store_##_name, \
2254 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2255 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2256 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2257 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2258 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2260 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2261 const char *buf, size_t count)
2265 rv = kstrtoint(buf, 0, &value);
2269 if (value < 0 || value > 2)
2272 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2279 static DEVICE_ATTR_WO(cpufv);
2281 static struct attribute *platform_attributes[] = {
2282 &dev_attr_cpufv.attr,
2283 &dev_attr_camera.attr,
2284 &dev_attr_cardr.attr,
2285 &dev_attr_touchpad.attr,
2286 &dev_attr_lid_resume.attr,
2287 &dev_attr_als_enable.attr,
2288 &dev_attr_fan_boost_mode.attr,
2289 &dev_attr_throttle_thermal_policy.attr,
2293 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2294 struct attribute *attr, int idx)
2296 struct device *dev = container_of(kobj, struct device, kobj);
2297 struct asus_wmi *asus = dev_get_drvdata(dev);
2301 if (attr == &dev_attr_camera.attr)
2302 devid = ASUS_WMI_DEVID_CAMERA;
2303 else if (attr == &dev_attr_cardr.attr)
2304 devid = ASUS_WMI_DEVID_CARDREADER;
2305 else if (attr == &dev_attr_touchpad.attr)
2306 devid = ASUS_WMI_DEVID_TOUCHPAD;
2307 else if (attr == &dev_attr_lid_resume.attr)
2308 devid = ASUS_WMI_DEVID_LID_RESUME;
2309 else if (attr == &dev_attr_als_enable.attr)
2310 devid = ASUS_WMI_DEVID_ALS_ENABLE;
2311 else if (attr == &dev_attr_fan_boost_mode.attr)
2312 ok = asus->fan_boost_mode_available;
2313 else if (attr == &dev_attr_throttle_thermal_policy.attr)
2314 ok = asus->throttle_thermal_policy_available;
2317 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2319 return ok ? attr->mode : 0;
2322 static const struct attribute_group platform_attribute_group = {
2323 .is_visible = asus_sysfs_is_visible,
2324 .attrs = platform_attributes
2327 static void asus_wmi_sysfs_exit(struct platform_device *device)
2329 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2332 static int asus_wmi_sysfs_init(struct platform_device *device)
2334 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2337 /* Platform device ************************************************************/
2339 static int asus_wmi_platform_init(struct asus_wmi *asus)
2341 struct device *dev = &asus->platform_device->dev;
2345 /* INIT enable hotkeys on some models */
2346 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2347 pr_info("Initialization: %#x\n", rv);
2349 /* We don't know yet what to do with this version... */
2350 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2351 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2356 * The SFUN method probably allows the original driver to get the list
2357 * of features supported by a given model. For now, 0x0100 or 0x0800
2358 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2359 * The significance of others is yet to be found.
2361 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2362 pr_info("SFUN value: %#x\n", rv);
2367 * Eee PC and Notebooks seems to have different method_id for DSTS,
2368 * but it may also be related to the BIOS's SPEC.
2369 * Note, on most Eeepc, there is no way to check if a method exist
2370 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2371 * but once again, SPEC may probably be used for that kind of things.
2373 * Additionally at least TUF Gaming series laptops return nothing for
2374 * unknown methods, so the detection in this way is not possible.
2376 * There is strong indication that only ACPI WMI devices that have _UID
2377 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2379 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2383 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2384 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2385 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2387 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2388 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2392 * Some devices can have multiple event codes stored in a queue before
2393 * the module load if it was unloaded intermittently after calling
2394 * the INIT method (enables event handling). The WMI notify handler is
2395 * expected to retrieve all event codes until a retrieved code equals
2396 * queue end marker (One or Ones). Old codes are flushed from the queue
2397 * upon module load. Not enabling this when it should be has minimal
2398 * visible impact so fall back if anything goes wrong.
2400 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2401 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2402 dev_info(dev, "Detected ATK, enable event queue\n");
2404 if (!asus_wmi_notify_queue_flush(asus))
2405 asus->wmi_event_queue = true;
2408 /* CWAP allow to define the behavior of the Fn+F2 key,
2409 * this method doesn't seems to be present on Eee PCs */
2410 if (asus->driver->quirks->wapf >= 0)
2411 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2412 asus->driver->quirks->wapf, NULL);
2417 /* debugfs ********************************************************************/
2419 struct asus_wmi_debugfs_node {
2420 struct asus_wmi *asus;
2422 int (*show) (struct seq_file *m, void *data);
2425 static int show_dsts(struct seq_file *m, void *data)
2427 struct asus_wmi *asus = m->private;
2431 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2435 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2440 static int show_devs(struct seq_file *m, void *data)
2442 struct asus_wmi *asus = m->private;
2446 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2451 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2452 asus->debug.ctrl_param, retval);
2457 static int show_call(struct seq_file *m, void *data)
2459 struct asus_wmi *asus = m->private;
2460 struct bios_args args = {
2461 .arg0 = asus->debug.dev_id,
2462 .arg1 = asus->debug.ctrl_param,
2464 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2465 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2466 union acpi_object *obj;
2469 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2470 0, asus->debug.method_id,
2473 if (ACPI_FAILURE(status))
2476 obj = (union acpi_object *)output.pointer;
2477 if (obj && obj->type == ACPI_TYPE_INTEGER)
2478 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2479 asus->debug.dev_id, asus->debug.ctrl_param,
2480 (u32) obj->integer.value);
2482 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2483 asus->debug.dev_id, asus->debug.ctrl_param,
2484 obj ? obj->type : -1);
2491 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2492 {NULL, "devs", show_devs},
2493 {NULL, "dsts", show_dsts},
2494 {NULL, "call", show_call},
2497 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2499 struct asus_wmi_debugfs_node *node = inode->i_private;
2501 return single_open(file, node->show, node->asus);
2504 static const struct file_operations asus_wmi_debugfs_io_ops = {
2505 .owner = THIS_MODULE,
2506 .open = asus_wmi_debugfs_open,
2508 .llseek = seq_lseek,
2509 .release = single_release,
2512 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2514 debugfs_remove_recursive(asus->debug.root);
2517 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2521 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2523 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2524 &asus->debug.method_id);
2526 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2527 &asus->debug.dev_id);
2529 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2530 &asus->debug.ctrl_param);
2532 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2533 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2536 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2537 asus->debug.root, node,
2538 &asus_wmi_debugfs_io_ops);
2542 /* Init / exit ****************************************************************/
2544 static int asus_wmi_add(struct platform_device *pdev)
2546 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2547 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2548 struct asus_wmi *asus;
2549 const char *chassis_type;
2554 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2558 asus->driver = wdrv;
2559 asus->platform_device = pdev;
2560 wdrv->platform_device = pdev;
2561 platform_set_drvdata(asus->platform_device, asus);
2563 if (wdrv->detect_quirks)
2564 wdrv->detect_quirks(asus->driver);
2566 err = asus_wmi_platform_init(asus);
2570 err = fan_boost_mode_check_present(asus);
2572 goto fail_fan_boost_mode;
2574 err = throttle_thermal_policy_check_present(asus);
2576 goto fail_throttle_thermal_policy;
2578 throttle_thermal_policy_set_default(asus);
2580 err = asus_wmi_sysfs_init(asus->platform_device);
2584 err = asus_wmi_input_init(asus);
2588 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2590 err = asus_wmi_hwmon_init(asus);
2594 err = asus_wmi_led_init(asus);
2598 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2599 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2600 asus->driver->wlan_ctrl_by_user = 1;
2602 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2603 err = asus_wmi_rfkill_init(asus);
2608 if (asus->driver->quirks->wmi_force_als_set)
2611 /* Some Asus desktop boards export an acpi-video backlight interface,
2612 stop this from showing up */
2613 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2614 if (chassis_type && !strcmp(chassis_type, "3"))
2615 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2617 if (asus->driver->quirks->wmi_backlight_power)
2618 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2620 if (asus->driver->quirks->wmi_backlight_native)
2621 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2623 if (asus->driver->quirks->xusb2pr)
2624 asus_wmi_set_xusb2pr(asus);
2626 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2627 err = asus_wmi_backlight_init(asus);
2628 if (err && err != -ENODEV)
2629 goto fail_backlight;
2630 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
2631 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2633 if (asus_wmi_has_fnlock_key(asus)) {
2634 asus->fnlock_locked = true;
2635 asus_wmi_fnlock_update(asus);
2638 status = wmi_install_notify_handler(asus->driver->event_guid,
2639 asus_wmi_notify, asus);
2640 if (ACPI_FAILURE(status)) {
2641 pr_err("Unable to register notify handler - %d\n", status);
2643 goto fail_wmi_handler;
2646 asus_wmi_battery_init(asus);
2648 asus_wmi_debugfs_init(asus);
2653 asus_wmi_backlight_exit(asus);
2655 asus_wmi_rfkill_exit(asus);
2657 asus_wmi_led_exit(asus);
2660 asus_wmi_input_exit(asus);
2662 asus_wmi_sysfs_exit(asus->platform_device);
2664 fail_throttle_thermal_policy:
2665 fail_fan_boost_mode:
2671 static int asus_wmi_remove(struct platform_device *device)
2673 struct asus_wmi *asus;
2675 asus = platform_get_drvdata(device);
2676 wmi_remove_notify_handler(asus->driver->event_guid);
2677 asus_wmi_backlight_exit(asus);
2678 asus_wmi_input_exit(asus);
2679 asus_wmi_led_exit(asus);
2680 asus_wmi_rfkill_exit(asus);
2681 asus_wmi_debugfs_exit(asus);
2682 asus_wmi_sysfs_exit(asus->platform_device);
2683 asus_fan_set_auto(asus);
2684 asus_wmi_battery_exit(asus);
2690 /* Platform driver - hibernate/resume callbacks *******************************/
2692 static int asus_hotk_thaw(struct device *device)
2694 struct asus_wmi *asus = dev_get_drvdata(device);
2696 if (asus->wlan.rfkill) {
2700 * Work around bios bug - acpi _PTS turns off the wireless led
2701 * during suspend. Normally it restores it on resume, but
2702 * we should kick it ourselves in case hibernation is aborted.
2704 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2705 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2711 static int asus_hotk_resume(struct device *device)
2713 struct asus_wmi *asus = dev_get_drvdata(device);
2715 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2716 kbd_led_update(asus);
2718 if (asus_wmi_has_fnlock_key(asus))
2719 asus_wmi_fnlock_update(asus);
2723 static int asus_hotk_restore(struct device *device)
2725 struct asus_wmi *asus = dev_get_drvdata(device);
2728 /* Refresh both wlan rfkill state and pci hotplug */
2729 if (asus->wlan.rfkill)
2730 asus_rfkill_hotplug(asus);
2732 if (asus->bluetooth.rfkill) {
2733 bl = !asus_wmi_get_devstate_simple(asus,
2734 ASUS_WMI_DEVID_BLUETOOTH);
2735 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2737 if (asus->wimax.rfkill) {
2738 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2739 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2741 if (asus->wwan3g.rfkill) {
2742 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2743 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2745 if (asus->gps.rfkill) {
2746 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2747 rfkill_set_sw_state(asus->gps.rfkill, bl);
2749 if (asus->uwb.rfkill) {
2750 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2751 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2753 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2754 kbd_led_update(asus);
2756 if (asus_wmi_has_fnlock_key(asus))
2757 asus_wmi_fnlock_update(asus);
2761 static const struct dev_pm_ops asus_pm_ops = {
2762 .thaw = asus_hotk_thaw,
2763 .restore = asus_hotk_restore,
2764 .resume = asus_hotk_resume,
2767 /* Registration ***************************************************************/
2769 static int asus_wmi_probe(struct platform_device *pdev)
2771 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2772 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2775 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2776 pr_warn("ASUS Management GUID not found\n");
2780 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2781 pr_warn("ASUS Event GUID not found\n");
2786 ret = wdrv->probe(pdev);
2791 return asus_wmi_add(pdev);
2796 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2798 struct platform_driver *platform_driver;
2799 struct platform_device *platform_device;
2804 platform_driver = &driver->platform_driver;
2805 platform_driver->remove = asus_wmi_remove;
2806 platform_driver->driver.owner = driver->owner;
2807 platform_driver->driver.name = driver->name;
2808 platform_driver->driver.pm = &asus_pm_ops;
2810 platform_device = platform_create_bundle(platform_driver,
2813 if (IS_ERR(platform_device))
2814 return PTR_ERR(platform_device);
2819 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2821 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2823 platform_device_unregister(driver->platform_device);
2824 platform_driver_unregister(&driver->platform_driver);
2827 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2829 static int __init asus_wmi_init(void)
2831 pr_info("ASUS WMI generic driver loaded\n");
2835 static void __exit asus_wmi_exit(void)
2837 pr_info("ASUS WMI generic driver unloaded\n");
2840 module_init(asus_wmi_init);
2841 module_exit(asus_wmi_exit);