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/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/platform_data/x86/asus-wmi.h>
34 #include <linux/platform_device.h>
35 #include <linux/thermal.h>
36 #include <linux/acpi.h>
37 #include <linux/dmi.h>
38 #include <acpi/video.h>
42 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
43 "Yong Wang <yong.y.wang@intel.com>");
44 MODULE_DESCRIPTION("Asus Generic WMI Driver");
45 MODULE_LICENSE("GPL");
47 #define to_asus_wmi_driver(pdrv) \
48 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
50 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
52 #define NOTIFY_BRNUP_MIN 0x11
53 #define NOTIFY_BRNUP_MAX 0x1f
54 #define NOTIFY_BRNDOWN_MIN 0x20
55 #define NOTIFY_BRNDOWN_MAX 0x2e
56 #define NOTIFY_FNLOCK_TOGGLE 0x4e
57 #define NOTIFY_KBD_BRTUP 0xc4
58 #define NOTIFY_KBD_BRTDWN 0xc5
59 #define NOTIFY_KBD_BRTTOGGLE 0xc7
61 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
63 #define ASUS_FAN_DESC "cpu_fan"
64 #define ASUS_FAN_MFUN 0x13
65 #define ASUS_FAN_SFUN_READ 0x06
66 #define ASUS_FAN_SFUN_WRITE 0x07
67 #define ASUS_FAN_CTRL_MANUAL 1
68 #define ASUS_FAN_CTRL_AUTO 2
70 #define USB_INTEL_XUSB2PR 0xD0
71 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
73 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
75 static bool ashs_present(void)
79 if (acpi_dev_found(ashs_ids[i++]))
91 * Struct that's used for all methods called via AGFN. Naming is
92 * identically to the AML code.
95 u16 mfun; /* probably "Multi-function" to be called */
96 u16 sfun; /* probably "Sub-function" to be called */
97 u16 len; /* size of the hole struct, including subfunction fields */
98 u8 stas; /* not used by now */
99 u8 err; /* zero on success */
102 /* struct used for calling fan read and write methods */
104 struct agfn_args agfn; /* common fields */
105 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
106 u32 speed; /* read: RPM/100 - write: 0-255 */
110 * <platform>/ - debugfs root directory
111 * dev_id - current dev_id
112 * ctrl_param - current ctrl_param
113 * method_id - current method_id
114 * devs - call DEVS(dev_id, ctrl_param) and print result
115 * dsts - call DSTS(dev_id) and print result
116 * call - call method_id(dev_id, ctrl_param) and print result
118 struct asus_wmi_debug {
126 struct asus_wmi *asus;
127 struct rfkill *rfkill;
136 struct input_dev *inputdev;
137 struct backlight_device *backlight_device;
138 struct platform_device *platform_device;
140 struct led_classdev wlan_led;
142 struct led_classdev tpd_led;
144 struct led_classdev kbd_led;
146 struct led_classdev lightbar_led;
148 struct workqueue_struct *led_workqueue;
149 struct work_struct tpd_led_work;
150 struct work_struct wlan_led_work;
151 struct work_struct lightbar_led_work;
153 struct asus_rfkill wlan;
154 struct asus_rfkill bluetooth;
155 struct asus_rfkill wimax;
156 struct asus_rfkill wwan3g;
157 struct asus_rfkill gps;
158 struct asus_rfkill uwb;
160 bool asus_hwmon_fan_manual_mode;
161 int asus_hwmon_num_fans;
164 struct hotplug_slot hotplug_slot;
165 struct mutex hotplug_lock;
166 struct mutex wmi_lock;
167 struct workqueue_struct *hotplug_workqueue;
168 struct work_struct hotplug_work;
172 struct asus_wmi_debug debug;
174 struct asus_wmi_driver *driver;
177 static int asus_wmi_input_init(struct asus_wmi *asus)
181 asus->inputdev = input_allocate_device();
185 asus->inputdev->name = asus->driver->input_name;
186 asus->inputdev->phys = asus->driver->input_phys;
187 asus->inputdev->id.bustype = BUS_HOST;
188 asus->inputdev->dev.parent = &asus->platform_device->dev;
189 set_bit(EV_REP, asus->inputdev->evbit);
191 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
195 err = input_register_device(asus->inputdev);
202 input_free_device(asus->inputdev);
206 static void asus_wmi_input_exit(struct asus_wmi *asus)
209 input_unregister_device(asus->inputdev);
211 asus->inputdev = NULL;
214 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
216 struct bios_args args = {
220 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
221 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
223 union acpi_object *obj;
226 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
229 if (ACPI_FAILURE(status))
232 obj = (union acpi_object *)output.pointer;
233 if (obj && obj->type == ACPI_TYPE_INTEGER)
234 tmp = (u32) obj->integer.value;
242 if (ACPI_FAILURE(status))
245 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
250 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
252 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
254 struct acpi_buffer input;
260 * Copy to dma capable address otherwise memory corruption occurs as
261 * bios has to be able to access it.
263 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
264 input.length = args.length;
267 phys_addr = virt_to_phys(input.pointer);
268 memcpy(input.pointer, args.pointer, args.length);
270 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
271 phys_addr, 0, &retval);
273 memcpy(args.pointer, input.pointer, args.length);
275 kfree(input.pointer);
282 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
284 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
287 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
290 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
294 /* Helper for special devices with magic return codes */
295 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
296 u32 dev_id, u32 mask)
301 err = asus_wmi_get_devstate(asus, dev_id, &retval);
306 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
309 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
310 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
314 return retval & mask;
317 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
319 return asus_wmi_get_devstate_bits(asus, dev_id,
320 ASUS_WMI_DSTS_STATUS_BIT);
327 * These functions actually update the LED's, and are called from a
328 * workqueue. By doing this as separate work rather than when the LED
329 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
330 * potentially bad time, such as a timer interrupt.
332 static void tpd_led_update(struct work_struct *work)
335 struct asus_wmi *asus;
337 asus = container_of(work, struct asus_wmi, tpd_led_work);
339 ctrl_param = asus->tpd_led_wk;
340 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
343 static void tpd_led_set(struct led_classdev *led_cdev,
344 enum led_brightness value)
346 struct asus_wmi *asus;
348 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
350 asus->tpd_led_wk = !!value;
351 queue_work(asus->led_workqueue, &asus->tpd_led_work);
354 static int read_tpd_led_state(struct asus_wmi *asus)
356 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
359 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
361 struct asus_wmi *asus;
363 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
365 return read_tpd_led_state(asus);
368 static void kbd_led_update(struct asus_wmi *asus)
374 * bit 7: light on/off
376 if (asus->kbd_led_wk > 0)
377 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
379 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
382 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
388 * bit 7: light on/off
389 * bit 8-10: environment (0: dark, 1: normal, 2: light)
390 * bit 17: status unknown
392 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
395 /* Unknown status is considered as off */
396 if (retval == 0x8000)
401 *level = retval & 0x7F;
403 *env = (retval >> 8) & 0x7F;
410 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
412 struct asus_wmi *asus;
415 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
416 max_level = asus->kbd_led.max_brightness;
418 if (value > max_level)
423 asus->kbd_led_wk = value;
424 kbd_led_update(asus);
427 static void kbd_led_set(struct led_classdev *led_cdev,
428 enum led_brightness value)
430 do_kbd_led_set(led_cdev, value);
433 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
435 struct led_classdev *led_cdev = &asus->kbd_led;
437 do_kbd_led_set(led_cdev, value);
438 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
441 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
443 struct asus_wmi *asus;
446 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
448 retval = kbd_led_read(asus, &value, NULL);
456 static int wlan_led_unknown_state(struct asus_wmi *asus)
460 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
462 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
465 static int wlan_led_presence(struct asus_wmi *asus)
469 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
471 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
474 static void wlan_led_update(struct work_struct *work)
477 struct asus_wmi *asus;
479 asus = container_of(work, struct asus_wmi, wlan_led_work);
481 ctrl_param = asus->wlan_led_wk;
482 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
485 static void wlan_led_set(struct led_classdev *led_cdev,
486 enum led_brightness value)
488 struct asus_wmi *asus;
490 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
492 asus->wlan_led_wk = !!value;
493 queue_work(asus->led_workqueue, &asus->wlan_led_work);
496 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
498 struct asus_wmi *asus;
501 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
502 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
504 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
507 static void lightbar_led_update(struct work_struct *work)
509 struct asus_wmi *asus;
512 asus = container_of(work, struct asus_wmi, lightbar_led_work);
514 ctrl_param = asus->lightbar_led_wk;
515 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
518 static void lightbar_led_set(struct led_classdev *led_cdev,
519 enum led_brightness value)
521 struct asus_wmi *asus;
523 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
525 asus->lightbar_led_wk = !!value;
526 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
529 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
531 struct asus_wmi *asus;
534 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
535 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
537 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
540 static int lightbar_led_presence(struct asus_wmi *asus)
544 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
546 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
549 static void asus_wmi_led_exit(struct asus_wmi *asus)
551 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
552 led_classdev_unregister(&asus->kbd_led);
553 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
554 led_classdev_unregister(&asus->tpd_led);
555 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
556 led_classdev_unregister(&asus->wlan_led);
557 if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
558 led_classdev_unregister(&asus->lightbar_led);
559 if (asus->led_workqueue)
560 destroy_workqueue(asus->led_workqueue);
563 static int asus_wmi_led_init(struct asus_wmi *asus)
567 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
568 if (!asus->led_workqueue)
571 if (read_tpd_led_state(asus) >= 0) {
572 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
574 asus->tpd_led.name = "asus::touchpad";
575 asus->tpd_led.brightness_set = tpd_led_set;
576 asus->tpd_led.brightness_get = tpd_led_get;
577 asus->tpd_led.max_brightness = 1;
579 rv = led_classdev_register(&asus->platform_device->dev,
585 led_val = kbd_led_read(asus, NULL, NULL);
587 asus->kbd_led_wk = led_val;
588 asus->kbd_led.name = "asus::kbd_backlight";
589 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
590 asus->kbd_led.brightness_set = kbd_led_set;
591 asus->kbd_led.brightness_get = kbd_led_get;
592 asus->kbd_led.max_brightness = 3;
594 rv = led_classdev_register(&asus->platform_device->dev,
600 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
601 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
603 asus->wlan_led.name = "asus::wlan";
604 asus->wlan_led.brightness_set = wlan_led_set;
605 if (!wlan_led_unknown_state(asus))
606 asus->wlan_led.brightness_get = wlan_led_get;
607 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
608 asus->wlan_led.max_brightness = 1;
609 asus->wlan_led.default_trigger = "asus-wlan";
611 rv = led_classdev_register(&asus->platform_device->dev,
617 if (lightbar_led_presence(asus)) {
618 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
620 asus->lightbar_led.name = "asus::lightbar";
621 asus->lightbar_led.brightness_set = lightbar_led_set;
622 asus->lightbar_led.brightness_get = lightbar_led_get;
623 asus->lightbar_led.max_brightness = 1;
625 rv = led_classdev_register(&asus->platform_device->dev,
626 &asus->lightbar_led);
631 asus_wmi_led_exit(asus);
638 * PCI hotplug (for wlan rfkill)
640 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
642 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
649 static void asus_rfkill_hotplug(struct asus_wmi *asus)
657 mutex_lock(&asus->wmi_lock);
658 blocked = asus_wlan_rfkill_blocked(asus);
659 mutex_unlock(&asus->wmi_lock);
661 mutex_lock(&asus->hotplug_lock);
662 pci_lock_rescan_remove();
664 if (asus->wlan.rfkill)
665 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
667 if (asus->hotplug_slot.ops) {
668 bus = pci_find_bus(0, 1);
670 pr_warn("Unable to find PCI bus 1?\n");
674 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
675 pr_err("Unable to read PCI config space?\n");
678 absent = (l == 0xffffffff);
680 if (blocked != absent) {
681 pr_warn("BIOS says wireless lan is %s, "
682 "but the pci device is %s\n",
683 blocked ? "blocked" : "unblocked",
684 absent ? "absent" : "present");
685 pr_warn("skipped wireless hotplug as probably "
686 "inappropriate for this model\n");
691 dev = pci_get_slot(bus, 0);
693 /* Device already present */
697 dev = pci_scan_single_device(bus, 0);
699 pci_bus_assign_resources(bus);
700 pci_bus_add_device(dev);
703 dev = pci_get_slot(bus, 0);
705 pci_stop_and_remove_bus_device(dev);
712 pci_unlock_rescan_remove();
713 mutex_unlock(&asus->hotplug_lock);
716 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
718 struct asus_wmi *asus = data;
720 if (event != ACPI_NOTIFY_BUS_CHECK)
724 * We can't call directly asus_rfkill_hotplug because most
725 * of the time WMBC is still being executed and not reetrant.
726 * There is currently no way to tell ACPICA that we want this
727 * method to be serialized, we schedule a asus_rfkill_hotplug
728 * call later, in a safer context.
730 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
733 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
738 status = acpi_get_handle(NULL, node, &handle);
740 if (ACPI_SUCCESS(status)) {
741 status = acpi_install_notify_handler(handle,
743 asus_rfkill_notify, asus);
744 if (ACPI_FAILURE(status))
745 pr_warn("Failed to register notify on %s\n", node);
752 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
754 acpi_status status = AE_OK;
757 status = acpi_get_handle(NULL, node, &handle);
759 if (ACPI_SUCCESS(status)) {
760 status = acpi_remove_notify_handler(handle,
763 if (ACPI_FAILURE(status))
764 pr_err("Error removing rfkill notify handler %s\n",
769 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
772 struct asus_wmi *asus = container_of(hotplug_slot,
773 struct asus_wmi, hotplug_slot);
774 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
783 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
784 .get_adapter_status = asus_get_adapter_status,
785 .get_power_status = asus_get_adapter_status,
788 static void asus_hotplug_work(struct work_struct *work)
790 struct asus_wmi *asus;
792 asus = container_of(work, struct asus_wmi, hotplug_work);
793 asus_rfkill_hotplug(asus);
796 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
799 struct pci_bus *bus = pci_find_bus(0, 1);
802 pr_err("Unable to find wifi PCI bus\n");
806 asus->hotplug_workqueue =
807 create_singlethread_workqueue("hotplug_workqueue");
808 if (!asus->hotplug_workqueue)
809 goto error_workqueue;
811 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
813 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
815 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
817 pr_err("Unable to register hotplug slot - %d\n", ret);
824 asus->hotplug_slot.ops = NULL;
825 destroy_workqueue(asus->hotplug_workqueue);
833 static int asus_rfkill_set(void *data, bool blocked)
835 struct asus_rfkill *priv = data;
836 u32 ctrl_param = !blocked;
837 u32 dev_id = priv->dev_id;
840 * If the user bit is set, BIOS can't set and record the wlan status,
841 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
842 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
843 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
844 * while setting the wlan status through WMI.
845 * This is also the behavior that windows app will do.
847 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
848 priv->asus->driver->wlan_ctrl_by_user)
849 dev_id = ASUS_WMI_DEVID_WLAN_LED;
851 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
854 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
856 struct asus_rfkill *priv = data;
859 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
864 rfkill_set_sw_state(priv->rfkill, !result);
867 static int asus_rfkill_wlan_set(void *data, bool blocked)
869 struct asus_rfkill *priv = data;
870 struct asus_wmi *asus = priv->asus;
874 * This handler is enabled only if hotplug is enabled.
875 * In this case, the asus_wmi_set_devstate() will
876 * trigger a wmi notification and we need to wait
877 * this call to finish before being able to call
880 mutex_lock(&asus->wmi_lock);
881 ret = asus_rfkill_set(data, blocked);
882 mutex_unlock(&asus->wmi_lock);
886 static const struct rfkill_ops asus_rfkill_wlan_ops = {
887 .set_block = asus_rfkill_wlan_set,
888 .query = asus_rfkill_query,
891 static const struct rfkill_ops asus_rfkill_ops = {
892 .set_block = asus_rfkill_set,
893 .query = asus_rfkill_query,
896 static int asus_new_rfkill(struct asus_wmi *asus,
897 struct asus_rfkill *arfkill,
898 const char *name, enum rfkill_type type, int dev_id)
900 int result = asus_wmi_get_devstate_simple(asus, dev_id);
901 struct rfkill **rfkill = &arfkill->rfkill;
906 arfkill->dev_id = dev_id;
907 arfkill->asus = asus;
909 if (dev_id == ASUS_WMI_DEVID_WLAN &&
910 asus->driver->quirks->hotplug_wireless)
911 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
912 &asus_rfkill_wlan_ops, arfkill);
914 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
915 &asus_rfkill_ops, arfkill);
920 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
921 (asus->driver->quirks->wapf > 0))
922 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
924 rfkill_init_sw_state(*rfkill, !result);
925 result = rfkill_register(*rfkill);
927 rfkill_destroy(*rfkill);
934 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
936 if (asus->driver->wlan_ctrl_by_user && ashs_present())
939 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
940 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
941 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
942 if (asus->wlan.rfkill) {
943 rfkill_unregister(asus->wlan.rfkill);
944 rfkill_destroy(asus->wlan.rfkill);
945 asus->wlan.rfkill = NULL;
948 * Refresh pci hotplug in case the rfkill state was changed after
949 * asus_unregister_rfkill_notifier()
951 asus_rfkill_hotplug(asus);
952 if (asus->hotplug_slot.ops)
953 pci_hp_deregister(&asus->hotplug_slot);
954 if (asus->hotplug_workqueue)
955 destroy_workqueue(asus->hotplug_workqueue);
957 if (asus->bluetooth.rfkill) {
958 rfkill_unregister(asus->bluetooth.rfkill);
959 rfkill_destroy(asus->bluetooth.rfkill);
960 asus->bluetooth.rfkill = NULL;
962 if (asus->wimax.rfkill) {
963 rfkill_unregister(asus->wimax.rfkill);
964 rfkill_destroy(asus->wimax.rfkill);
965 asus->wimax.rfkill = NULL;
967 if (asus->wwan3g.rfkill) {
968 rfkill_unregister(asus->wwan3g.rfkill);
969 rfkill_destroy(asus->wwan3g.rfkill);
970 asus->wwan3g.rfkill = NULL;
972 if (asus->gps.rfkill) {
973 rfkill_unregister(asus->gps.rfkill);
974 rfkill_destroy(asus->gps.rfkill);
975 asus->gps.rfkill = NULL;
977 if (asus->uwb.rfkill) {
978 rfkill_unregister(asus->uwb.rfkill);
979 rfkill_destroy(asus->uwb.rfkill);
980 asus->uwb.rfkill = NULL;
984 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
988 mutex_init(&asus->hotplug_lock);
989 mutex_init(&asus->wmi_lock);
991 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
992 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
994 if (result && result != -ENODEV)
997 result = asus_new_rfkill(asus, &asus->bluetooth,
998 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
999 ASUS_WMI_DEVID_BLUETOOTH);
1001 if (result && result != -ENODEV)
1004 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1005 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1007 if (result && result != -ENODEV)
1010 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1011 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1013 if (result && result != -ENODEV)
1016 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1017 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1019 if (result && result != -ENODEV)
1022 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1023 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1025 if (result && result != -ENODEV)
1028 if (!asus->driver->quirks->hotplug_wireless)
1031 result = asus_setup_pci_hotplug(asus);
1033 * If we get -EBUSY then something else is handling the PCI hotplug -
1034 * don't fail in this case
1036 if (result == -EBUSY)
1039 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1040 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1041 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1043 * Refresh pci hotplug in case the rfkill state was changed during
1046 asus_rfkill_hotplug(asus);
1049 if (result && result != -ENODEV)
1050 asus_wmi_rfkill_exit(asus);
1052 if (result == -ENODEV)
1058 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1060 struct pci_dev *xhci_pdev;
1061 u32 orig_ports_available;
1062 u32 ports_available = asus->driver->quirks->xusb2pr;
1064 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1065 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1071 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1072 &orig_ports_available);
1074 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1075 cpu_to_le32(ports_available));
1077 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1078 orig_ports_available, ports_available);
1082 * Some devices dont support or have borcken get_als method
1083 * but still support set method.
1085 static void asus_wmi_set_als(void)
1087 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1093 static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1096 struct fan_args args = {
1097 .agfn.len = sizeof(args),
1098 .agfn.mfun = ASUS_FAN_MFUN,
1099 .agfn.sfun = ASUS_FAN_SFUN_READ,
1103 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1109 status = asus_wmi_evaluate_method_agfn(input);
1111 if (status || args.agfn.err)
1115 *speed = args.speed;
1120 static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1123 struct fan_args args = {
1124 .agfn.len = sizeof(args),
1125 .agfn.mfun = ASUS_FAN_MFUN,
1126 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1128 .speed = speed ? *speed : 0,
1130 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1133 /* 1: for setting 1st fan's speed 0: setting auto mode */
1134 if (fan != 1 && fan != 0)
1137 status = asus_wmi_evaluate_method_agfn(input);
1139 if (status || args.agfn.err)
1142 if (speed && fan == 1)
1143 asus->asus_hwmon_pwm = *speed;
1149 * Check if we can read the speed of one fan. If true we assume we can also
1152 static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1159 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1166 static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1170 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1174 asus->asus_hwmon_fan_manual_mode = false;
1179 static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1181 struct asus_wmi *asus = dev_get_drvdata(dev);
1185 /* no speed readable on manual mode */
1186 if (asus->asus_hwmon_fan_manual_mode)
1189 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1191 pr_warn("reading fan speed failed: %d\n", ret);
1198 static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1202 if (asus->asus_hwmon_pwm >= 0) {
1203 *value = asus->asus_hwmon_pwm;
1207 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1213 if (*value == 1) /* Low Speed */
1215 else if (*value == 2)
1217 else if (*value == 3)
1220 pr_err("Unknown fan speed %#x\n", *value);
1225 static ssize_t pwm1_show(struct device *dev,
1226 struct device_attribute *attr,
1229 struct asus_wmi *asus = dev_get_drvdata(dev);
1232 asus_hwmon_pwm_show(asus, 0, &value);
1234 return sprintf(buf, "%d\n", value);
1237 static ssize_t pwm1_store(struct device *dev,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count) {
1240 struct asus_wmi *asus = dev_get_drvdata(dev);
1245 ret = kstrtouint(buf, 10, &value);
1250 value = clamp(value, 0, 255);
1252 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1254 pr_warn("Setting fan speed failed: %d\n", state);
1256 asus->asus_hwmon_fan_manual_mode = true;
1261 static ssize_t fan1_input_show(struct device *dev,
1262 struct device_attribute *attr,
1265 int value = asus_hwmon_fan_rpm_show(dev, 0);
1267 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1271 static ssize_t pwm1_enable_show(struct device *dev,
1272 struct device_attribute *attr,
1275 struct asus_wmi *asus = dev_get_drvdata(dev);
1277 if (asus->asus_hwmon_fan_manual_mode)
1278 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1280 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1283 static ssize_t pwm1_enable_store(struct device *dev,
1284 struct device_attribute *attr,
1285 const char *buf, size_t count)
1287 struct asus_wmi *asus = dev_get_drvdata(dev);
1292 ret = kstrtouint(buf, 10, &state);
1297 if (state == ASUS_FAN_CTRL_MANUAL)
1298 asus->asus_hwmon_fan_manual_mode = true;
1300 status = asus_hwmon_fan_set_auto(asus);
1308 static ssize_t fan1_label_show(struct device *dev,
1309 struct device_attribute *attr,
1312 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1315 static ssize_t asus_hwmon_temp1(struct device *dev,
1316 struct device_attribute *attr,
1319 struct asus_wmi *asus = dev_get_drvdata(dev);
1323 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1328 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1330 return sprintf(buf, "%d\n", value);
1334 static DEVICE_ATTR_RW(pwm1);
1335 static DEVICE_ATTR_RW(pwm1_enable);
1336 static DEVICE_ATTR_RO(fan1_input);
1337 static DEVICE_ATTR_RO(fan1_label);
1340 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1342 static struct attribute *hwmon_attributes[] = {
1343 &dev_attr_pwm1.attr,
1344 &dev_attr_pwm1_enable.attr,
1345 &dev_attr_fan1_input.attr,
1346 &dev_attr_fan1_label.attr,
1348 &dev_attr_temp1_input.attr,
1352 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1353 struct attribute *attr, int idx)
1355 struct device *dev = container_of(kobj, struct device, kobj);
1356 struct platform_device *pdev = to_platform_device(dev->parent);
1357 struct asus_wmi *asus = platform_get_drvdata(pdev);
1360 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1363 if (attr == &dev_attr_pwm1.attr)
1364 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1365 else if (attr == &dev_attr_temp1_input.attr)
1366 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1369 if (attr == &dev_attr_fan1_input.attr
1370 || attr == &dev_attr_fan1_label.attr
1371 || attr == &dev_attr_pwm1.attr
1372 || attr == &dev_attr_pwm1_enable.attr) {
1377 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1379 if (err < 0 && fan_attr == -1)
1380 return 0; /* can't return negative here */
1383 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1385 * We need to find a better way, probably using sfun,
1387 * Currently we disable it if:
1388 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1389 * - reverved bits are non-zero
1390 * - sfun and presence bit are not set
1392 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1393 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1396 ok = fan_attr <= asus->asus_hwmon_num_fans;
1397 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1398 /* If value is zero, something is clearly wrong */
1401 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1407 return ok ? attr->mode : 0;
1410 static const struct attribute_group hwmon_attribute_group = {
1411 .is_visible = asus_hwmon_sysfs_is_visible,
1412 .attrs = hwmon_attributes
1414 __ATTRIBUTE_GROUPS(hwmon_attribute);
1416 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1418 struct device *hwmon;
1420 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1422 hwmon_attribute_groups);
1423 if (IS_ERR(hwmon)) {
1424 pr_err("Could not register asus hwmon device\n");
1425 return PTR_ERR(hwmon);
1433 static int read_backlight_power(struct asus_wmi *asus)
1436 if (asus->driver->quirks->store_backlight_power)
1437 ret = !asus->driver->panel_power;
1439 ret = asus_wmi_get_devstate_simple(asus,
1440 ASUS_WMI_DEVID_BACKLIGHT);
1445 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1448 static int read_brightness_max(struct asus_wmi *asus)
1453 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1458 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1467 static int read_brightness(struct backlight_device *bd)
1469 struct asus_wmi *asus = bl_get_data(bd);
1473 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1478 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1481 static u32 get_scalar_command(struct backlight_device *bd)
1483 struct asus_wmi *asus = bl_get_data(bd);
1486 if ((asus->driver->brightness < bd->props.brightness) ||
1487 bd->props.brightness == bd->props.max_brightness)
1488 ctrl_param = 0x00008001;
1489 else if ((asus->driver->brightness > bd->props.brightness) ||
1490 bd->props.brightness == 0)
1491 ctrl_param = 0x00008000;
1493 asus->driver->brightness = bd->props.brightness;
1498 static int update_bl_status(struct backlight_device *bd)
1500 struct asus_wmi *asus = bl_get_data(bd);
1504 power = read_backlight_power(asus);
1505 if (power != -ENODEV && bd->props.power != power) {
1506 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1507 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1509 if (asus->driver->quirks->store_backlight_power)
1510 asus->driver->panel_power = bd->props.power;
1512 /* When using scalar brightness, updating the brightness
1513 * will mess with the backlight power */
1514 if (asus->driver->quirks->scalar_panel_brightness)
1518 if (asus->driver->quirks->scalar_panel_brightness)
1519 ctrl_param = get_scalar_command(bd);
1521 ctrl_param = bd->props.brightness;
1523 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1529 static const struct backlight_ops asus_wmi_bl_ops = {
1530 .get_brightness = read_brightness,
1531 .update_status = update_bl_status,
1534 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1536 struct backlight_device *bd = asus->backlight_device;
1537 int old = bd->props.brightness;
1540 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1541 new = code - NOTIFY_BRNUP_MIN + 1;
1542 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1543 new = code - NOTIFY_BRNDOWN_MIN;
1545 bd->props.brightness = new;
1546 backlight_update_status(bd);
1547 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1552 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1554 struct backlight_device *bd;
1555 struct backlight_properties props;
1559 max = read_brightness_max(asus);
1563 power = read_backlight_power(asus);
1565 if (power == -ENODEV)
1566 power = FB_BLANK_UNBLANK;
1570 memset(&props, 0, sizeof(struct backlight_properties));
1571 props.type = BACKLIGHT_PLATFORM;
1572 props.max_brightness = max;
1573 bd = backlight_device_register(asus->driver->name,
1574 &asus->platform_device->dev, asus,
1575 &asus_wmi_bl_ops, &props);
1577 pr_err("Could not register backlight device\n");
1581 asus->backlight_device = bd;
1583 if (asus->driver->quirks->store_backlight_power)
1584 asus->driver->panel_power = power;
1586 bd->props.brightness = read_brightness(bd);
1587 bd->props.power = power;
1588 backlight_update_status(bd);
1590 asus->driver->brightness = bd->props.brightness;
1595 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1597 backlight_device_unregister(asus->backlight_device);
1599 asus->backlight_device = NULL;
1602 static int is_display_toggle(int code)
1604 /* display toggle keys */
1605 if ((code >= 0x61 && code <= 0x67) ||
1606 (code >= 0x8c && code <= 0x93) ||
1607 (code >= 0xa0 && code <= 0xa7) ||
1608 (code >= 0xd0 && code <= 0xd5))
1614 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1618 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1620 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1621 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1624 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1626 int mode = asus->fnlock_locked;
1628 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1631 static void asus_wmi_notify(u32 value, void *context)
1633 struct asus_wmi *asus = context;
1634 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1635 union acpi_object *obj;
1639 unsigned int key_value = 1;
1640 bool autorelease = 1;
1642 status = wmi_get_event_data(value, &response);
1643 if (status != AE_OK) {
1644 pr_err("bad event status 0x%x\n", status);
1648 obj = (union acpi_object *)response.pointer;
1650 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1653 code = obj->integer.value;
1656 if (asus->driver->key_filter) {
1657 asus->driver->key_filter(asus->driver, &code, &key_value,
1659 if (code == ASUS_WMI_KEY_IGNORE)
1663 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1664 code = ASUS_WMI_BRN_UP;
1665 else if (code >= NOTIFY_BRNDOWN_MIN &&
1666 code <= NOTIFY_BRNDOWN_MAX)
1667 code = ASUS_WMI_BRN_DOWN;
1669 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1670 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1671 asus_wmi_backlight_notify(asus, orig_code);
1676 if (code == NOTIFY_KBD_BRTUP) {
1677 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1680 if (code == NOTIFY_KBD_BRTDWN) {
1681 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
1684 if (code == NOTIFY_KBD_BRTTOGGLE) {
1685 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
1686 kbd_led_set_by_kbd(asus, 0);
1688 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1692 if (code == NOTIFY_FNLOCK_TOGGLE) {
1693 asus->fnlock_locked = !asus->fnlock_locked;
1694 asus_wmi_fnlock_update(asus);
1698 if (is_display_toggle(code) &&
1699 asus->driver->quirks->no_display_toggle)
1702 if (!sparse_keymap_report_event(asus->inputdev, code,
1703 key_value, autorelease))
1704 pr_info("Unknown key %x pressed\n", code);
1713 static int parse_arg(const char *buf, unsigned long count, int *val)
1717 if (sscanf(buf, "%i", val) != 1)
1722 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1723 const char *buf, size_t count)
1728 value = asus_wmi_get_devstate_simple(asus, devid);
1732 rv = parse_arg(buf, count, &value);
1733 err = asus_wmi_set_devstate(devid, value, &retval);
1741 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1743 int value = asus_wmi_get_devstate_simple(asus, devid);
1748 return sprintf(buf, "%d\n", value);
1751 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1752 static ssize_t show_##_name(struct device *dev, \
1753 struct device_attribute *attr, \
1756 struct asus_wmi *asus = dev_get_drvdata(dev); \
1758 return show_sys_wmi(asus, _cm, buf); \
1760 static ssize_t store_##_name(struct device *dev, \
1761 struct device_attribute *attr, \
1762 const char *buf, size_t count) \
1764 struct asus_wmi *asus = dev_get_drvdata(dev); \
1766 return store_sys_wmi(asus, _cm, buf, count); \
1768 static struct device_attribute dev_attr_##_name = { \
1770 .name = __stringify(_name), \
1772 .show = show_##_name, \
1773 .store = store_##_name, \
1776 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1777 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1778 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
1779 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1780 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
1782 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
1783 const char *buf, size_t count)
1787 if (!count || sscanf(buf, "%i", &value) != 1)
1789 if (value < 0 || value > 2)
1792 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1799 static DEVICE_ATTR_WO(cpufv);
1801 static struct attribute *platform_attributes[] = {
1802 &dev_attr_cpufv.attr,
1803 &dev_attr_camera.attr,
1804 &dev_attr_cardr.attr,
1805 &dev_attr_touchpad.attr,
1806 &dev_attr_lid_resume.attr,
1807 &dev_attr_als_enable.attr,
1811 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1812 struct attribute *attr, int idx)
1814 struct device *dev = container_of(kobj, struct device, kobj);
1815 struct asus_wmi *asus = dev_get_drvdata(dev);
1819 if (attr == &dev_attr_camera.attr)
1820 devid = ASUS_WMI_DEVID_CAMERA;
1821 else if (attr == &dev_attr_cardr.attr)
1822 devid = ASUS_WMI_DEVID_CARDREADER;
1823 else if (attr == &dev_attr_touchpad.attr)
1824 devid = ASUS_WMI_DEVID_TOUCHPAD;
1825 else if (attr == &dev_attr_lid_resume.attr)
1826 devid = ASUS_WMI_DEVID_LID_RESUME;
1827 else if (attr == &dev_attr_als_enable.attr)
1828 devid = ASUS_WMI_DEVID_ALS_ENABLE;
1831 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1833 return ok ? attr->mode : 0;
1836 static const struct attribute_group platform_attribute_group = {
1837 .is_visible = asus_sysfs_is_visible,
1838 .attrs = platform_attributes
1841 static void asus_wmi_sysfs_exit(struct platform_device *device)
1843 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1846 static int asus_wmi_sysfs_init(struct platform_device *device)
1848 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1854 static int asus_wmi_platform_init(struct asus_wmi *asus)
1858 /* INIT enable hotkeys on some models */
1859 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1860 pr_info("Initialization: %#x\n", rv);
1862 /* We don't know yet what to do with this version... */
1863 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1864 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
1869 * The SFUN method probably allows the original driver to get the list
1870 * of features supported by a given model. For now, 0x0100 or 0x0800
1871 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1872 * The significance of others is yet to be found.
1874 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1875 pr_info("SFUN value: %#x\n", rv);
1880 * Eee PC and Notebooks seems to have different method_id for DSTS,
1881 * but it may also be related to the BIOS's SPEC.
1882 * Note, on most Eeepc, there is no way to check if a method exist
1883 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1884 * but once again, SPEC may probably be used for that kind of things.
1886 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1887 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1889 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1891 /* CWAP allow to define the behavior of the Fn+F2 key,
1892 * this method doesn't seems to be present on Eee PCs */
1893 if (asus->driver->quirks->wapf >= 0)
1894 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1895 asus->driver->quirks->wapf, NULL);
1897 return asus_wmi_sysfs_init(asus->platform_device);
1900 static void asus_wmi_platform_exit(struct asus_wmi *asus)
1902 asus_wmi_sysfs_exit(asus->platform_device);
1908 struct asus_wmi_debugfs_node {
1909 struct asus_wmi *asus;
1911 int (*show) (struct seq_file *m, void *data);
1914 static int show_dsts(struct seq_file *m, void *data)
1916 struct asus_wmi *asus = m->private;
1920 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1925 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1930 static int show_devs(struct seq_file *m, void *data)
1932 struct asus_wmi *asus = m->private;
1936 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1942 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1943 asus->debug.ctrl_param, retval);
1948 static int show_call(struct seq_file *m, void *data)
1950 struct asus_wmi *asus = m->private;
1951 struct bios_args args = {
1952 .arg0 = asus->debug.dev_id,
1953 .arg1 = asus->debug.ctrl_param,
1955 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1956 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1957 union acpi_object *obj;
1960 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1961 0, asus->debug.method_id,
1964 if (ACPI_FAILURE(status))
1967 obj = (union acpi_object *)output.pointer;
1968 if (obj && obj->type == ACPI_TYPE_INTEGER)
1969 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1970 asus->debug.dev_id, asus->debug.ctrl_param,
1971 (u32) obj->integer.value);
1973 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1974 asus->debug.dev_id, asus->debug.ctrl_param,
1975 obj ? obj->type : -1);
1982 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1983 {NULL, "devs", show_devs},
1984 {NULL, "dsts", show_dsts},
1985 {NULL, "call", show_call},
1988 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
1990 struct asus_wmi_debugfs_node *node = inode->i_private;
1992 return single_open(file, node->show, node->asus);
1995 static const struct file_operations asus_wmi_debugfs_io_ops = {
1996 .owner = THIS_MODULE,
1997 .open = asus_wmi_debugfs_open,
1999 .llseek = seq_lseek,
2000 .release = single_release,
2003 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2005 debugfs_remove_recursive(asus->debug.root);
2008 static int asus_wmi_debugfs_init(struct asus_wmi *asus)
2010 struct dentry *dent;
2013 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2014 if (!asus->debug.root) {
2015 pr_err("failed to create debugfs directory\n");
2019 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2020 asus->debug.root, &asus->debug.method_id);
2024 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2025 asus->debug.root, &asus->debug.dev_id);
2029 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2030 asus->debug.root, &asus->debug.ctrl_param);
2034 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2035 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2038 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2039 asus->debug.root, node,
2040 &asus_wmi_debugfs_io_ops);
2042 pr_err("failed to create debug file: %s\n", node->name);
2050 asus_wmi_debugfs_exit(asus);
2054 static int asus_wmi_fan_init(struct asus_wmi *asus)
2058 asus->asus_hwmon_pwm = -1;
2059 asus->asus_hwmon_num_fans = -1;
2060 asus->asus_hwmon_fan_manual_mode = false;
2062 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2064 asus->asus_hwmon_num_fans = 0;
2065 pr_warn("Could not determine number of fans: %d\n", status);
2069 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2076 static int asus_wmi_add(struct platform_device *pdev)
2078 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2079 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2080 struct asus_wmi *asus;
2081 const char *chassis_type;
2086 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2090 asus->driver = wdrv;
2091 asus->platform_device = pdev;
2092 wdrv->platform_device = pdev;
2093 platform_set_drvdata(asus->platform_device, asus);
2095 if (wdrv->detect_quirks)
2096 wdrv->detect_quirks(asus->driver);
2098 err = asus_wmi_platform_init(asus);
2102 err = asus_wmi_input_init(asus);
2106 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2107 asus_hwmon_fan_set_auto(asus);
2109 err = asus_wmi_hwmon_init(asus);
2113 err = asus_wmi_led_init(asus);
2117 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2118 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2119 asus->driver->wlan_ctrl_by_user = 1;
2121 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2122 err = asus_wmi_rfkill_init(asus);
2127 if (asus->driver->quirks->wmi_force_als_set)
2130 /* Some Asus desktop boards export an acpi-video backlight interface,
2131 stop this from showing up */
2132 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2133 if (chassis_type && !strcmp(chassis_type, "3"))
2134 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2136 if (asus->driver->quirks->wmi_backlight_power)
2137 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2139 if (asus->driver->quirks->wmi_backlight_native)
2140 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2142 if (asus->driver->quirks->xusb2pr)
2143 asus_wmi_set_xusb2pr(asus);
2145 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2146 err = asus_wmi_backlight_init(asus);
2147 if (err && err != -ENODEV)
2148 goto fail_backlight;
2150 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2152 if (asus_wmi_has_fnlock_key(asus)) {
2153 asus->fnlock_locked = true;
2154 asus_wmi_fnlock_update(asus);
2157 status = wmi_install_notify_handler(asus->driver->event_guid,
2158 asus_wmi_notify, asus);
2159 if (ACPI_FAILURE(status)) {
2160 pr_err("Unable to register notify handler - %d\n", status);
2162 goto fail_wmi_handler;
2165 err = asus_wmi_debugfs_init(asus);
2172 wmi_remove_notify_handler(asus->driver->event_guid);
2174 asus_wmi_backlight_exit(asus);
2176 asus_wmi_rfkill_exit(asus);
2178 asus_wmi_led_exit(asus);
2181 asus_wmi_input_exit(asus);
2183 asus_wmi_platform_exit(asus);
2189 static int asus_wmi_remove(struct platform_device *device)
2191 struct asus_wmi *asus;
2193 asus = platform_get_drvdata(device);
2194 wmi_remove_notify_handler(asus->driver->event_guid);
2195 asus_wmi_backlight_exit(asus);
2196 asus_wmi_input_exit(asus);
2197 asus_wmi_led_exit(asus);
2198 asus_wmi_rfkill_exit(asus);
2199 asus_wmi_debugfs_exit(asus);
2200 asus_wmi_platform_exit(asus);
2201 asus_hwmon_fan_set_auto(asus);
2208 * Platform driver - hibernate/resume callbacks
2210 static int asus_hotk_thaw(struct device *device)
2212 struct asus_wmi *asus = dev_get_drvdata(device);
2214 if (asus->wlan.rfkill) {
2218 * Work around bios bug - acpi _PTS turns off the wireless led
2219 * during suspend. Normally it restores it on resume, but
2220 * we should kick it ourselves in case hibernation is aborted.
2222 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2223 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2229 static int asus_hotk_resume(struct device *device)
2231 struct asus_wmi *asus = dev_get_drvdata(device);
2233 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2234 kbd_led_update(asus);
2236 if (asus_wmi_has_fnlock_key(asus))
2237 asus_wmi_fnlock_update(asus);
2241 static int asus_hotk_restore(struct device *device)
2243 struct asus_wmi *asus = dev_get_drvdata(device);
2246 /* Refresh both wlan rfkill state and pci hotplug */
2247 if (asus->wlan.rfkill)
2248 asus_rfkill_hotplug(asus);
2250 if (asus->bluetooth.rfkill) {
2251 bl = !asus_wmi_get_devstate_simple(asus,
2252 ASUS_WMI_DEVID_BLUETOOTH);
2253 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2255 if (asus->wimax.rfkill) {
2256 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2257 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2259 if (asus->wwan3g.rfkill) {
2260 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2261 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2263 if (asus->gps.rfkill) {
2264 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2265 rfkill_set_sw_state(asus->gps.rfkill, bl);
2267 if (asus->uwb.rfkill) {
2268 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2269 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2271 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2272 kbd_led_update(asus);
2274 if (asus_wmi_has_fnlock_key(asus))
2275 asus_wmi_fnlock_update(asus);
2279 static const struct dev_pm_ops asus_pm_ops = {
2280 .thaw = asus_hotk_thaw,
2281 .restore = asus_hotk_restore,
2282 .resume = asus_hotk_resume,
2285 static int asus_wmi_probe(struct platform_device *pdev)
2287 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2288 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2291 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2292 pr_warn("ASUS Management GUID not found\n");
2296 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2297 pr_warn("ASUS Event GUID not found\n");
2302 ret = wdrv->probe(pdev);
2307 return asus_wmi_add(pdev);
2312 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2314 struct platform_driver *platform_driver;
2315 struct platform_device *platform_device;
2320 platform_driver = &driver->platform_driver;
2321 platform_driver->remove = asus_wmi_remove;
2322 platform_driver->driver.owner = driver->owner;
2323 platform_driver->driver.name = driver->name;
2324 platform_driver->driver.pm = &asus_pm_ops;
2326 platform_device = platform_create_bundle(platform_driver,
2329 if (IS_ERR(platform_device))
2330 return PTR_ERR(platform_device);
2335 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2337 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2339 platform_device_unregister(driver->platform_device);
2340 platform_driver_unregister(&driver->platform_driver);
2343 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2345 static int __init asus_wmi_init(void)
2347 pr_info("ASUS WMI generic driver loaded\n");
2351 static void __exit asus_wmi_exit(void)
2353 pr_info("ASUS WMI generic driver unloaded\n");
2356 module_init(asus_wmi_init);
2357 module_exit(asus_wmi_exit);