drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / platform / x86 / asus-wmi.c
blobf94691615881e68515215242fd957d7dff6df393
1 /*
2 * Asus PC WMI hotkey driver
4 * Copyright(C) 2010 Intel Corporation.
5 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/fb.h>
37 #include <linux/backlight.h>
38 #include <linux/leds.h>
39 #include <linux/rfkill.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <linux/hwmon.h>
43 #include <linux/hwmon-sysfs.h>
44 #include <linux/debugfs.h>
45 #include <linux/seq_file.h>
46 #include <linux/platform_data/x86/asus-wmi.h>
47 #include <linux/platform_device.h>
48 #include <linux/thermal.h>
49 #include <linux/acpi.h>
50 #include <linux/dmi.h>
51 #include <acpi/video.h>
53 #include "asus-wmi.h"
55 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
56 "Yong Wang <yong.y.wang@intel.com>");
57 MODULE_DESCRIPTION("Asus Generic WMI Driver");
58 MODULE_LICENSE("GPL");
60 #define to_asus_wmi_driver(pdrv) \
61 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
63 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
65 #define NOTIFY_BRNUP_MIN 0x11
66 #define NOTIFY_BRNUP_MAX 0x1f
67 #define NOTIFY_BRNDOWN_MIN 0x20
68 #define NOTIFY_BRNDOWN_MAX 0x2e
69 #define NOTIFY_FNLOCK_TOGGLE 0x4e
70 #define NOTIFY_KBD_BRTUP 0xc4
71 #define NOTIFY_KBD_BRTDWN 0xc5
72 #define NOTIFY_KBD_BRTTOGGLE 0xc7
74 #define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
76 #define ASUS_FAN_DESC "cpu_fan"
77 #define ASUS_FAN_MFUN 0x13
78 #define ASUS_FAN_SFUN_READ 0x06
79 #define ASUS_FAN_SFUN_WRITE 0x07
80 #define ASUS_FAN_CTRL_MANUAL 1
81 #define ASUS_FAN_CTRL_AUTO 2
83 #define USB_INTEL_XUSB2PR 0xD0
84 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
86 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
88 static bool ashs_present(void)
90 int i = 0;
91 while (ashs_ids[i]) {
92 if (acpi_dev_found(ashs_ids[i++]))
93 return true;
95 return false;
98 struct bios_args {
99 u32 arg0;
100 u32 arg1;
101 } __packed;
104 * Struct that's used for all methods called via AGFN. Naming is
105 * identically to the AML code.
107 struct agfn_args {
108 u16 mfun; /* probably "Multi-function" to be called */
109 u16 sfun; /* probably "Sub-function" to be called */
110 u16 len; /* size of the hole struct, including subfunction fields */
111 u8 stas; /* not used by now */
112 u8 err; /* zero on success */
113 } __packed;
115 /* struct used for calling fan read and write methods */
116 struct fan_args {
117 struct agfn_args agfn; /* common fields */
118 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
119 u32 speed; /* read: RPM/100 - write: 0-255 */
120 } __packed;
123 * <platform>/ - debugfs root directory
124 * dev_id - current dev_id
125 * ctrl_param - current ctrl_param
126 * method_id - current method_id
127 * devs - call DEVS(dev_id, ctrl_param) and print result
128 * dsts - call DSTS(dev_id) and print result
129 * call - call method_id(dev_id, ctrl_param) and print result
131 struct asus_wmi_debug {
132 struct dentry *root;
133 u32 method_id;
134 u32 dev_id;
135 u32 ctrl_param;
138 struct asus_rfkill {
139 struct asus_wmi *asus;
140 struct rfkill *rfkill;
141 u32 dev_id;
144 struct asus_wmi {
145 int dsts_id;
146 int spec;
147 int sfun;
149 struct input_dev *inputdev;
150 struct backlight_device *backlight_device;
151 struct platform_device *platform_device;
153 struct led_classdev wlan_led;
154 int wlan_led_wk;
155 struct led_classdev tpd_led;
156 int tpd_led_wk;
157 struct led_classdev kbd_led;
158 int kbd_led_wk;
159 struct led_classdev lightbar_led;
160 int lightbar_led_wk;
161 struct workqueue_struct *led_workqueue;
162 struct work_struct tpd_led_work;
163 struct work_struct wlan_led_work;
164 struct work_struct lightbar_led_work;
166 struct asus_rfkill wlan;
167 struct asus_rfkill bluetooth;
168 struct asus_rfkill wimax;
169 struct asus_rfkill wwan3g;
170 struct asus_rfkill gps;
171 struct asus_rfkill uwb;
173 bool asus_hwmon_fan_manual_mode;
174 int asus_hwmon_num_fans;
175 int asus_hwmon_pwm;
177 struct hotplug_slot hotplug_slot;
178 struct mutex hotplug_lock;
179 struct mutex wmi_lock;
180 struct workqueue_struct *hotplug_workqueue;
181 struct work_struct hotplug_work;
183 bool fnlock_locked;
185 struct asus_wmi_debug debug;
187 struct asus_wmi_driver *driver;
190 static int asus_wmi_input_init(struct asus_wmi *asus)
192 int err;
194 asus->inputdev = input_allocate_device();
195 if (!asus->inputdev)
196 return -ENOMEM;
198 asus->inputdev->name = asus->driver->input_name;
199 asus->inputdev->phys = asus->driver->input_phys;
200 asus->inputdev->id.bustype = BUS_HOST;
201 asus->inputdev->dev.parent = &asus->platform_device->dev;
202 set_bit(EV_REP, asus->inputdev->evbit);
204 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
205 if (err)
206 goto err_free_dev;
208 err = input_register_device(asus->inputdev);
209 if (err)
210 goto err_free_dev;
212 return 0;
214 err_free_dev:
215 input_free_device(asus->inputdev);
216 return err;
219 static void asus_wmi_input_exit(struct asus_wmi *asus)
221 if (asus->inputdev)
222 input_unregister_device(asus->inputdev);
224 asus->inputdev = NULL;
227 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
229 struct bios_args args = {
230 .arg0 = arg0,
231 .arg1 = arg1,
233 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
234 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
235 acpi_status status;
236 union acpi_object *obj;
237 u32 tmp = 0;
239 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
240 &input, &output);
242 if (ACPI_FAILURE(status))
243 goto exit;
245 obj = (union acpi_object *)output.pointer;
246 if (obj && obj->type == ACPI_TYPE_INTEGER)
247 tmp = (u32) obj->integer.value;
249 if (retval)
250 *retval = tmp;
252 kfree(obj);
254 exit:
255 if (ACPI_FAILURE(status))
256 return -EIO;
258 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
259 return -ENODEV;
261 return 0;
263 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
265 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
267 struct acpi_buffer input;
268 u64 phys_addr;
269 u32 retval;
270 u32 status = -1;
273 * Copy to dma capable address otherwise memory corruption occurs as
274 * bios has to be able to access it.
276 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
277 input.length = args.length;
278 if (!input.pointer)
279 return -ENOMEM;
280 phys_addr = virt_to_phys(input.pointer);
281 memcpy(input.pointer, args.pointer, args.length);
283 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
284 phys_addr, 0, &retval);
285 if (!status)
286 memcpy(args.pointer, input.pointer, args.length);
288 kfree(input.pointer);
289 if (status)
290 return -ENXIO;
292 return retval;
295 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
297 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
300 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
301 u32 *retval)
303 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
304 ctrl_param, retval);
307 /* Helper for special devices with magic return codes */
308 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
309 u32 dev_id, u32 mask)
311 u32 retval = 0;
312 int err;
314 err = asus_wmi_get_devstate(asus, dev_id, &retval);
316 if (err < 0)
317 return err;
319 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
320 return -ENODEV;
322 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
323 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
324 return -ENODEV;
327 return retval & mask;
330 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
332 return asus_wmi_get_devstate_bits(asus, dev_id,
333 ASUS_WMI_DSTS_STATUS_BIT);
337 * LEDs
340 * These functions actually update the LED's, and are called from a
341 * workqueue. By doing this as separate work rather than when the LED
342 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
343 * potentially bad time, such as a timer interrupt.
345 static void tpd_led_update(struct work_struct *work)
347 int ctrl_param;
348 struct asus_wmi *asus;
350 asus = container_of(work, struct asus_wmi, tpd_led_work);
352 ctrl_param = asus->tpd_led_wk;
353 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
356 static void tpd_led_set(struct led_classdev *led_cdev,
357 enum led_brightness value)
359 struct asus_wmi *asus;
361 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
363 asus->tpd_led_wk = !!value;
364 queue_work(asus->led_workqueue, &asus->tpd_led_work);
367 static int read_tpd_led_state(struct asus_wmi *asus)
369 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
372 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
374 struct asus_wmi *asus;
376 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
378 return read_tpd_led_state(asus);
381 static void kbd_led_update(struct asus_wmi *asus)
383 int ctrl_param = 0;
386 * bits 0-2: level
387 * bit 7: light on/off
389 if (asus->kbd_led_wk > 0)
390 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
392 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
395 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
397 int retval;
400 * bits 0-2: level
401 * bit 7: light on/off
402 * bit 8-10: environment (0: dark, 1: normal, 2: light)
403 * bit 17: status unknown
405 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
406 0xFFFF);
408 /* Unknown status is considered as off */
409 if (retval == 0x8000)
410 retval = 0;
412 if (retval >= 0) {
413 if (level)
414 *level = retval & 0x7F;
415 if (env)
416 *env = (retval >> 8) & 0x7F;
417 retval = 0;
420 return retval;
423 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
425 struct asus_wmi *asus;
426 int max_level;
428 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
429 max_level = asus->kbd_led.max_brightness;
431 if (value > max_level)
432 value = max_level;
433 else if (value < 0)
434 value = 0;
436 asus->kbd_led_wk = value;
437 kbd_led_update(asus);
440 static void kbd_led_set(struct led_classdev *led_cdev,
441 enum led_brightness value)
443 do_kbd_led_set(led_cdev, value);
446 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
448 struct led_classdev *led_cdev = &asus->kbd_led;
450 do_kbd_led_set(led_cdev, value);
451 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
454 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
456 struct asus_wmi *asus;
457 int retval, value;
459 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
461 retval = kbd_led_read(asus, &value, NULL);
463 if (retval < 0)
464 return retval;
466 return value;
469 static int wlan_led_unknown_state(struct asus_wmi *asus)
471 u32 result;
473 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
475 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
478 static int wlan_led_presence(struct asus_wmi *asus)
480 u32 result;
482 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
484 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
487 static void wlan_led_update(struct work_struct *work)
489 int ctrl_param;
490 struct asus_wmi *asus;
492 asus = container_of(work, struct asus_wmi, wlan_led_work);
494 ctrl_param = asus->wlan_led_wk;
495 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
498 static void wlan_led_set(struct led_classdev *led_cdev,
499 enum led_brightness value)
501 struct asus_wmi *asus;
503 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
505 asus->wlan_led_wk = !!value;
506 queue_work(asus->led_workqueue, &asus->wlan_led_work);
509 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
511 struct asus_wmi *asus;
512 u32 result;
514 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
515 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
517 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
520 static void lightbar_led_update(struct work_struct *work)
522 struct asus_wmi *asus;
523 int ctrl_param;
525 asus = container_of(work, struct asus_wmi, lightbar_led_work);
527 ctrl_param = asus->lightbar_led_wk;
528 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
531 static void lightbar_led_set(struct led_classdev *led_cdev,
532 enum led_brightness value)
534 struct asus_wmi *asus;
536 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
538 asus->lightbar_led_wk = !!value;
539 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
542 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
544 struct asus_wmi *asus;
545 u32 result;
547 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
548 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
550 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
553 static int lightbar_led_presence(struct asus_wmi *asus)
555 u32 result;
557 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
559 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
562 static void asus_wmi_led_exit(struct asus_wmi *asus)
564 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
565 led_classdev_unregister(&asus->kbd_led);
566 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
567 led_classdev_unregister(&asus->tpd_led);
568 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
569 led_classdev_unregister(&asus->wlan_led);
570 if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
571 led_classdev_unregister(&asus->lightbar_led);
572 if (asus->led_workqueue)
573 destroy_workqueue(asus->led_workqueue);
576 static int asus_wmi_led_init(struct asus_wmi *asus)
578 int rv = 0, led_val;
580 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
581 if (!asus->led_workqueue)
582 return -ENOMEM;
584 if (read_tpd_led_state(asus) >= 0) {
585 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
587 asus->tpd_led.name = "asus::touchpad";
588 asus->tpd_led.brightness_set = tpd_led_set;
589 asus->tpd_led.brightness_get = tpd_led_get;
590 asus->tpd_led.max_brightness = 1;
592 rv = led_classdev_register(&asus->platform_device->dev,
593 &asus->tpd_led);
594 if (rv)
595 goto error;
598 led_val = kbd_led_read(asus, NULL, NULL);
599 if (led_val >= 0) {
600 asus->kbd_led_wk = led_val;
601 asus->kbd_led.name = "asus::kbd_backlight";
602 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
603 asus->kbd_led.brightness_set = kbd_led_set;
604 asus->kbd_led.brightness_get = kbd_led_get;
605 asus->kbd_led.max_brightness = 3;
607 rv = led_classdev_register(&asus->platform_device->dev,
608 &asus->kbd_led);
609 if (rv)
610 goto error;
613 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
614 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
616 asus->wlan_led.name = "asus::wlan";
617 asus->wlan_led.brightness_set = wlan_led_set;
618 if (!wlan_led_unknown_state(asus))
619 asus->wlan_led.brightness_get = wlan_led_get;
620 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
621 asus->wlan_led.max_brightness = 1;
622 asus->wlan_led.default_trigger = "asus-wlan";
624 rv = led_classdev_register(&asus->platform_device->dev,
625 &asus->wlan_led);
626 if (rv)
627 goto error;
630 if (lightbar_led_presence(asus)) {
631 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
633 asus->lightbar_led.name = "asus::lightbar";
634 asus->lightbar_led.brightness_set = lightbar_led_set;
635 asus->lightbar_led.brightness_get = lightbar_led_get;
636 asus->lightbar_led.max_brightness = 1;
638 rv = led_classdev_register(&asus->platform_device->dev,
639 &asus->lightbar_led);
642 error:
643 if (rv)
644 asus_wmi_led_exit(asus);
646 return rv;
651 * PCI hotplug (for wlan rfkill)
653 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
655 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
657 if (result < 0)
658 return false;
659 return !result;
662 static void asus_rfkill_hotplug(struct asus_wmi *asus)
664 struct pci_dev *dev;
665 struct pci_bus *bus;
666 bool blocked;
667 bool absent;
668 u32 l;
670 mutex_lock(&asus->wmi_lock);
671 blocked = asus_wlan_rfkill_blocked(asus);
672 mutex_unlock(&asus->wmi_lock);
674 mutex_lock(&asus->hotplug_lock);
675 pci_lock_rescan_remove();
677 if (asus->wlan.rfkill)
678 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
680 if (asus->hotplug_slot.ops) {
681 bus = pci_find_bus(0, 1);
682 if (!bus) {
683 pr_warn("Unable to find PCI bus 1?\n");
684 goto out_unlock;
687 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
688 pr_err("Unable to read PCI config space?\n");
689 goto out_unlock;
691 absent = (l == 0xffffffff);
693 if (blocked != absent) {
694 pr_warn("BIOS says wireless lan is %s, "
695 "but the pci device is %s\n",
696 blocked ? "blocked" : "unblocked",
697 absent ? "absent" : "present");
698 pr_warn("skipped wireless hotplug as probably "
699 "inappropriate for this model\n");
700 goto out_unlock;
703 if (!blocked) {
704 dev = pci_get_slot(bus, 0);
705 if (dev) {
706 /* Device already present */
707 pci_dev_put(dev);
708 goto out_unlock;
710 dev = pci_scan_single_device(bus, 0);
711 if (dev) {
712 pci_bus_assign_resources(bus);
713 pci_bus_add_device(dev);
715 } else {
716 dev = pci_get_slot(bus, 0);
717 if (dev) {
718 pci_stop_and_remove_bus_device(dev);
719 pci_dev_put(dev);
724 out_unlock:
725 pci_unlock_rescan_remove();
726 mutex_unlock(&asus->hotplug_lock);
729 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
731 struct asus_wmi *asus = data;
733 if (event != ACPI_NOTIFY_BUS_CHECK)
734 return;
737 * We can't call directly asus_rfkill_hotplug because most
738 * of the time WMBC is still being executed and not reetrant.
739 * There is currently no way to tell ACPICA that we want this
740 * method to be serialized, we schedule a asus_rfkill_hotplug
741 * call later, in a safer context.
743 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
746 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
748 acpi_status status;
749 acpi_handle handle;
751 status = acpi_get_handle(NULL, node, &handle);
753 if (ACPI_SUCCESS(status)) {
754 status = acpi_install_notify_handler(handle,
755 ACPI_SYSTEM_NOTIFY,
756 asus_rfkill_notify, asus);
757 if (ACPI_FAILURE(status))
758 pr_warn("Failed to register notify on %s\n", node);
759 } else
760 return -ENODEV;
762 return 0;
765 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
767 acpi_status status = AE_OK;
768 acpi_handle handle;
770 status = acpi_get_handle(NULL, node, &handle);
772 if (ACPI_SUCCESS(status)) {
773 status = acpi_remove_notify_handler(handle,
774 ACPI_SYSTEM_NOTIFY,
775 asus_rfkill_notify);
776 if (ACPI_FAILURE(status))
777 pr_err("Error removing rfkill notify handler %s\n",
778 node);
782 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
783 u8 *value)
785 struct asus_wmi *asus = container_of(hotplug_slot,
786 struct asus_wmi, hotplug_slot);
787 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
789 if (result < 0)
790 return result;
792 *value = !!result;
793 return 0;
796 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
797 .get_adapter_status = asus_get_adapter_status,
798 .get_power_status = asus_get_adapter_status,
801 static void asus_hotplug_work(struct work_struct *work)
803 struct asus_wmi *asus;
805 asus = container_of(work, struct asus_wmi, hotplug_work);
806 asus_rfkill_hotplug(asus);
809 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
811 int ret = -ENOMEM;
812 struct pci_bus *bus = pci_find_bus(0, 1);
814 if (!bus) {
815 pr_err("Unable to find wifi PCI bus\n");
816 return -ENODEV;
819 asus->hotplug_workqueue =
820 create_singlethread_workqueue("hotplug_workqueue");
821 if (!asus->hotplug_workqueue)
822 goto error_workqueue;
824 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
826 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
828 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
829 if (ret) {
830 pr_err("Unable to register hotplug slot - %d\n", ret);
831 goto error_register;
834 return 0;
836 error_register:
837 asus->hotplug_slot.ops = NULL;
838 destroy_workqueue(asus->hotplug_workqueue);
839 error_workqueue:
840 return ret;
844 * Rfkill devices
846 static int asus_rfkill_set(void *data, bool blocked)
848 struct asus_rfkill *priv = data;
849 u32 ctrl_param = !blocked;
850 u32 dev_id = priv->dev_id;
853 * If the user bit is set, BIOS can't set and record the wlan status,
854 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
855 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
856 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
857 * while setting the wlan status through WMI.
858 * This is also the behavior that windows app will do.
860 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
861 priv->asus->driver->wlan_ctrl_by_user)
862 dev_id = ASUS_WMI_DEVID_WLAN_LED;
864 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
867 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
869 struct asus_rfkill *priv = data;
870 int result;
872 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
874 if (result < 0)
875 return;
877 rfkill_set_sw_state(priv->rfkill, !result);
880 static int asus_rfkill_wlan_set(void *data, bool blocked)
882 struct asus_rfkill *priv = data;
883 struct asus_wmi *asus = priv->asus;
884 int ret;
887 * This handler is enabled only if hotplug is enabled.
888 * In this case, the asus_wmi_set_devstate() will
889 * trigger a wmi notification and we need to wait
890 * this call to finish before being able to call
891 * any wmi method
893 mutex_lock(&asus->wmi_lock);
894 ret = asus_rfkill_set(data, blocked);
895 mutex_unlock(&asus->wmi_lock);
896 return ret;
899 static const struct rfkill_ops asus_rfkill_wlan_ops = {
900 .set_block = asus_rfkill_wlan_set,
901 .query = asus_rfkill_query,
904 static const struct rfkill_ops asus_rfkill_ops = {
905 .set_block = asus_rfkill_set,
906 .query = asus_rfkill_query,
909 static int asus_new_rfkill(struct asus_wmi *asus,
910 struct asus_rfkill *arfkill,
911 const char *name, enum rfkill_type type, int dev_id)
913 int result = asus_wmi_get_devstate_simple(asus, dev_id);
914 struct rfkill **rfkill = &arfkill->rfkill;
916 if (result < 0)
917 return result;
919 arfkill->dev_id = dev_id;
920 arfkill->asus = asus;
922 if (dev_id == ASUS_WMI_DEVID_WLAN &&
923 asus->driver->quirks->hotplug_wireless)
924 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
925 &asus_rfkill_wlan_ops, arfkill);
926 else
927 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
928 &asus_rfkill_ops, arfkill);
930 if (!*rfkill)
931 return -EINVAL;
933 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
934 (asus->driver->quirks->wapf > 0))
935 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
937 rfkill_init_sw_state(*rfkill, !result);
938 result = rfkill_register(*rfkill);
939 if (result) {
940 rfkill_destroy(*rfkill);
941 *rfkill = NULL;
942 return result;
944 return 0;
947 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
949 if (asus->driver->wlan_ctrl_by_user && ashs_present())
950 return;
952 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
953 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
954 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
955 if (asus->wlan.rfkill) {
956 rfkill_unregister(asus->wlan.rfkill);
957 rfkill_destroy(asus->wlan.rfkill);
958 asus->wlan.rfkill = NULL;
961 * Refresh pci hotplug in case the rfkill state was changed after
962 * asus_unregister_rfkill_notifier()
964 asus_rfkill_hotplug(asus);
965 if (asus->hotplug_slot.ops)
966 pci_hp_deregister(&asus->hotplug_slot);
967 if (asus->hotplug_workqueue)
968 destroy_workqueue(asus->hotplug_workqueue);
970 if (asus->bluetooth.rfkill) {
971 rfkill_unregister(asus->bluetooth.rfkill);
972 rfkill_destroy(asus->bluetooth.rfkill);
973 asus->bluetooth.rfkill = NULL;
975 if (asus->wimax.rfkill) {
976 rfkill_unregister(asus->wimax.rfkill);
977 rfkill_destroy(asus->wimax.rfkill);
978 asus->wimax.rfkill = NULL;
980 if (asus->wwan3g.rfkill) {
981 rfkill_unregister(asus->wwan3g.rfkill);
982 rfkill_destroy(asus->wwan3g.rfkill);
983 asus->wwan3g.rfkill = NULL;
985 if (asus->gps.rfkill) {
986 rfkill_unregister(asus->gps.rfkill);
987 rfkill_destroy(asus->gps.rfkill);
988 asus->gps.rfkill = NULL;
990 if (asus->uwb.rfkill) {
991 rfkill_unregister(asus->uwb.rfkill);
992 rfkill_destroy(asus->uwb.rfkill);
993 asus->uwb.rfkill = NULL;
997 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
999 int result = 0;
1001 mutex_init(&asus->hotplug_lock);
1002 mutex_init(&asus->wmi_lock);
1004 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1005 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1007 if (result && result != -ENODEV)
1008 goto exit;
1010 result = asus_new_rfkill(asus, &asus->bluetooth,
1011 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1012 ASUS_WMI_DEVID_BLUETOOTH);
1014 if (result && result != -ENODEV)
1015 goto exit;
1017 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1018 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1020 if (result && result != -ENODEV)
1021 goto exit;
1023 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1024 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1026 if (result && result != -ENODEV)
1027 goto exit;
1029 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1030 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1032 if (result && result != -ENODEV)
1033 goto exit;
1035 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1036 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1038 if (result && result != -ENODEV)
1039 goto exit;
1041 if (!asus->driver->quirks->hotplug_wireless)
1042 goto exit;
1044 result = asus_setup_pci_hotplug(asus);
1046 * If we get -EBUSY then something else is handling the PCI hotplug -
1047 * don't fail in this case
1049 if (result == -EBUSY)
1050 result = 0;
1052 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1053 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1054 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1056 * Refresh pci hotplug in case the rfkill state was changed during
1057 * setup.
1059 asus_rfkill_hotplug(asus);
1061 exit:
1062 if (result && result != -ENODEV)
1063 asus_wmi_rfkill_exit(asus);
1065 if (result == -ENODEV)
1066 result = 0;
1068 return result;
1071 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1073 struct pci_dev *xhci_pdev;
1074 u32 orig_ports_available;
1075 u32 ports_available = asus->driver->quirks->xusb2pr;
1077 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1078 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1079 NULL);
1081 if (!xhci_pdev)
1082 return;
1084 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1085 &orig_ports_available);
1087 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1088 cpu_to_le32(ports_available));
1090 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1091 orig_ports_available, ports_available);
1095 * Some devices dont support or have borcken get_als method
1096 * but still support set method.
1098 static void asus_wmi_set_als(void)
1100 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1104 * Hwmon device
1106 static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1107 int *speed)
1109 struct fan_args args = {
1110 .agfn.len = sizeof(args),
1111 .agfn.mfun = ASUS_FAN_MFUN,
1112 .agfn.sfun = ASUS_FAN_SFUN_READ,
1113 .fan = fan,
1114 .speed = 0,
1116 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1117 int status;
1119 if (fan != 1)
1120 return -EINVAL;
1122 status = asus_wmi_evaluate_method_agfn(input);
1124 if (status || args.agfn.err)
1125 return -ENXIO;
1127 if (speed)
1128 *speed = args.speed;
1130 return 0;
1133 static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1134 int *speed)
1136 struct fan_args args = {
1137 .agfn.len = sizeof(args),
1138 .agfn.mfun = ASUS_FAN_MFUN,
1139 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1140 .fan = fan,
1141 .speed = speed ? *speed : 0,
1143 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1144 int status;
1146 /* 1: for setting 1st fan's speed 0: setting auto mode */
1147 if (fan != 1 && fan != 0)
1148 return -EINVAL;
1150 status = asus_wmi_evaluate_method_agfn(input);
1152 if (status || args.agfn.err)
1153 return -ENXIO;
1155 if (speed && fan == 1)
1156 asus->asus_hwmon_pwm = *speed;
1158 return 0;
1162 * Check if we can read the speed of one fan. If true we assume we can also
1163 * control it.
1165 static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1167 int status;
1168 int speed = 0;
1170 *num_fans = 0;
1172 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1173 if (!status)
1174 *num_fans = 1;
1176 return 0;
1179 static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1181 int status;
1183 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1184 if (status)
1185 return -ENXIO;
1187 asus->asus_hwmon_fan_manual_mode = false;
1189 return 0;
1192 static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1194 struct asus_wmi *asus = dev_get_drvdata(dev);
1195 int value;
1196 int ret;
1198 /* no speed readable on manual mode */
1199 if (asus->asus_hwmon_fan_manual_mode)
1200 return -ENXIO;
1202 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1203 if (ret) {
1204 pr_warn("reading fan speed failed: %d\n", ret);
1205 return -ENXIO;
1208 return value;
1211 static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1213 int err;
1215 if (asus->asus_hwmon_pwm >= 0) {
1216 *value = asus->asus_hwmon_pwm;
1217 return;
1220 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1221 if (err < 0)
1222 return;
1224 *value &= 0xFF;
1226 if (*value == 1) /* Low Speed */
1227 *value = 85;
1228 else if (*value == 2)
1229 *value = 170;
1230 else if (*value == 3)
1231 *value = 255;
1232 else if (*value) {
1233 pr_err("Unknown fan speed %#x\n", *value);
1234 *value = -1;
1238 static ssize_t pwm1_show(struct device *dev,
1239 struct device_attribute *attr,
1240 char *buf)
1242 struct asus_wmi *asus = dev_get_drvdata(dev);
1243 int value;
1245 asus_hwmon_pwm_show(asus, 0, &value);
1247 return sprintf(buf, "%d\n", value);
1250 static ssize_t pwm1_store(struct device *dev,
1251 struct device_attribute *attr,
1252 const char *buf, size_t count) {
1253 struct asus_wmi *asus = dev_get_drvdata(dev);
1254 int value;
1255 int state;
1256 int ret;
1258 ret = kstrtouint(buf, 10, &value);
1260 if (ret)
1261 return ret;
1263 value = clamp(value, 0, 255);
1265 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1266 if (state)
1267 pr_warn("Setting fan speed failed: %d\n", state);
1268 else
1269 asus->asus_hwmon_fan_manual_mode = true;
1271 return count;
1274 static ssize_t fan1_input_show(struct device *dev,
1275 struct device_attribute *attr,
1276 char *buf)
1278 int value = asus_hwmon_fan_rpm_show(dev, 0);
1280 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1284 static ssize_t pwm1_enable_show(struct device *dev,
1285 struct device_attribute *attr,
1286 char *buf)
1288 struct asus_wmi *asus = dev_get_drvdata(dev);
1290 if (asus->asus_hwmon_fan_manual_mode)
1291 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1293 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1296 static ssize_t pwm1_enable_store(struct device *dev,
1297 struct device_attribute *attr,
1298 const char *buf, size_t count)
1300 struct asus_wmi *asus = dev_get_drvdata(dev);
1301 int status = 0;
1302 int state;
1303 int ret;
1305 ret = kstrtouint(buf, 10, &state);
1307 if (ret)
1308 return ret;
1310 if (state == ASUS_FAN_CTRL_MANUAL)
1311 asus->asus_hwmon_fan_manual_mode = true;
1312 else
1313 status = asus_hwmon_fan_set_auto(asus);
1315 if (status)
1316 return status;
1318 return count;
1321 static ssize_t fan1_label_show(struct device *dev,
1322 struct device_attribute *attr,
1323 char *buf)
1325 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1328 static ssize_t asus_hwmon_temp1(struct device *dev,
1329 struct device_attribute *attr,
1330 char *buf)
1332 struct asus_wmi *asus = dev_get_drvdata(dev);
1333 u32 value;
1334 int err;
1336 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1338 if (err < 0)
1339 return err;
1341 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1343 return sprintf(buf, "%d\n", value);
1346 /* Fan1 */
1347 static DEVICE_ATTR_RW(pwm1);
1348 static DEVICE_ATTR_RW(pwm1_enable);
1349 static DEVICE_ATTR_RO(fan1_input);
1350 static DEVICE_ATTR_RO(fan1_label);
1352 /* Temperature */
1353 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1355 static struct attribute *hwmon_attributes[] = {
1356 &dev_attr_pwm1.attr,
1357 &dev_attr_pwm1_enable.attr,
1358 &dev_attr_fan1_input.attr,
1359 &dev_attr_fan1_label.attr,
1361 &dev_attr_temp1_input.attr,
1362 NULL
1365 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1366 struct attribute *attr, int idx)
1368 struct device *dev = container_of(kobj, struct device, kobj);
1369 struct platform_device *pdev = to_platform_device(dev->parent);
1370 struct asus_wmi *asus = platform_get_drvdata(pdev);
1371 int dev_id = -1;
1372 int fan_attr = -1;
1373 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1374 bool ok = true;
1376 if (attr == &dev_attr_pwm1.attr)
1377 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1378 else if (attr == &dev_attr_temp1_input.attr)
1379 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1382 if (attr == &dev_attr_fan1_input.attr
1383 || attr == &dev_attr_fan1_label.attr
1384 || attr == &dev_attr_pwm1.attr
1385 || attr == &dev_attr_pwm1_enable.attr) {
1386 fan_attr = 1;
1389 if (dev_id != -1) {
1390 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1392 if (err < 0 && fan_attr == -1)
1393 return 0; /* can't return negative here */
1396 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1398 * We need to find a better way, probably using sfun,
1399 * bits or spec ...
1400 * Currently we disable it if:
1401 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1402 * - reverved bits are non-zero
1403 * - sfun and presence bit are not set
1405 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1406 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1407 ok = false;
1408 else
1409 ok = fan_attr <= asus->asus_hwmon_num_fans;
1410 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1411 /* If value is zero, something is clearly wrong */
1412 if (!value)
1413 ok = false;
1414 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1415 ok = true;
1416 } else {
1417 ok = false;
1420 return ok ? attr->mode : 0;
1423 static const struct attribute_group hwmon_attribute_group = {
1424 .is_visible = asus_hwmon_sysfs_is_visible,
1425 .attrs = hwmon_attributes
1427 __ATTRIBUTE_GROUPS(hwmon_attribute);
1429 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1431 struct device *hwmon;
1433 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1434 "asus", asus,
1435 hwmon_attribute_groups);
1436 if (IS_ERR(hwmon)) {
1437 pr_err("Could not register asus hwmon device\n");
1438 return PTR_ERR(hwmon);
1440 return 0;
1444 * Backlight
1446 static int read_backlight_power(struct asus_wmi *asus)
1448 int ret;
1449 if (asus->driver->quirks->store_backlight_power)
1450 ret = !asus->driver->panel_power;
1451 else
1452 ret = asus_wmi_get_devstate_simple(asus,
1453 ASUS_WMI_DEVID_BACKLIGHT);
1455 if (ret < 0)
1456 return ret;
1458 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1461 static int read_brightness_max(struct asus_wmi *asus)
1463 u32 retval;
1464 int err;
1466 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1468 if (err < 0)
1469 return err;
1471 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1472 retval >>= 8;
1474 if (!retval)
1475 return -ENODEV;
1477 return retval;
1480 static int read_brightness(struct backlight_device *bd)
1482 struct asus_wmi *asus = bl_get_data(bd);
1483 u32 retval;
1484 int err;
1486 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1488 if (err < 0)
1489 return err;
1491 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1494 static u32 get_scalar_command(struct backlight_device *bd)
1496 struct asus_wmi *asus = bl_get_data(bd);
1497 u32 ctrl_param = 0;
1499 if ((asus->driver->brightness < bd->props.brightness) ||
1500 bd->props.brightness == bd->props.max_brightness)
1501 ctrl_param = 0x00008001;
1502 else if ((asus->driver->brightness > bd->props.brightness) ||
1503 bd->props.brightness == 0)
1504 ctrl_param = 0x00008000;
1506 asus->driver->brightness = bd->props.brightness;
1508 return ctrl_param;
1511 static int update_bl_status(struct backlight_device *bd)
1513 struct asus_wmi *asus = bl_get_data(bd);
1514 u32 ctrl_param;
1515 int power, err = 0;
1517 power = read_backlight_power(asus);
1518 if (power != -ENODEV && bd->props.power != power) {
1519 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1520 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1521 ctrl_param, NULL);
1522 if (asus->driver->quirks->store_backlight_power)
1523 asus->driver->panel_power = bd->props.power;
1525 /* When using scalar brightness, updating the brightness
1526 * will mess with the backlight power */
1527 if (asus->driver->quirks->scalar_panel_brightness)
1528 return err;
1531 if (asus->driver->quirks->scalar_panel_brightness)
1532 ctrl_param = get_scalar_command(bd);
1533 else
1534 ctrl_param = bd->props.brightness;
1536 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1537 ctrl_param, NULL);
1539 return err;
1542 static const struct backlight_ops asus_wmi_bl_ops = {
1543 .get_brightness = read_brightness,
1544 .update_status = update_bl_status,
1547 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1549 struct backlight_device *bd = asus->backlight_device;
1550 int old = bd->props.brightness;
1551 int new = old;
1553 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1554 new = code - NOTIFY_BRNUP_MIN + 1;
1555 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1556 new = code - NOTIFY_BRNDOWN_MIN;
1558 bd->props.brightness = new;
1559 backlight_update_status(bd);
1560 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1562 return old;
1565 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1567 struct backlight_device *bd;
1568 struct backlight_properties props;
1569 int max;
1570 int power;
1572 max = read_brightness_max(asus);
1573 if (max < 0)
1574 return max;
1576 power = read_backlight_power(asus);
1578 if (power == -ENODEV)
1579 power = FB_BLANK_UNBLANK;
1580 else if (power < 0)
1581 return power;
1583 memset(&props, 0, sizeof(struct backlight_properties));
1584 props.type = BACKLIGHT_PLATFORM;
1585 props.max_brightness = max;
1586 bd = backlight_device_register(asus->driver->name,
1587 &asus->platform_device->dev, asus,
1588 &asus_wmi_bl_ops, &props);
1589 if (IS_ERR(bd)) {
1590 pr_err("Could not register backlight device\n");
1591 return PTR_ERR(bd);
1594 asus->backlight_device = bd;
1596 if (asus->driver->quirks->store_backlight_power)
1597 asus->driver->panel_power = power;
1599 bd->props.brightness = read_brightness(bd);
1600 bd->props.power = power;
1601 backlight_update_status(bd);
1603 asus->driver->brightness = bd->props.brightness;
1605 return 0;
1608 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1610 backlight_device_unregister(asus->backlight_device);
1612 asus->backlight_device = NULL;
1615 static int is_display_toggle(int code)
1617 /* display toggle keys */
1618 if ((code >= 0x61 && code <= 0x67) ||
1619 (code >= 0x8c && code <= 0x93) ||
1620 (code >= 0xa0 && code <= 0xa7) ||
1621 (code >= 0xd0 && code <= 0xd5))
1622 return 1;
1624 return 0;
1627 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1629 u32 result;
1631 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1633 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1634 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1637 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1639 int mode = asus->fnlock_locked;
1641 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1644 static void asus_wmi_notify(u32 value, void *context)
1646 struct asus_wmi *asus = context;
1647 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1648 union acpi_object *obj;
1649 acpi_status status;
1650 int code;
1651 int orig_code;
1652 unsigned int key_value = 1;
1653 bool autorelease = 1;
1655 status = wmi_get_event_data(value, &response);
1656 if (status != AE_OK) {
1657 pr_err("bad event status 0x%x\n", status);
1658 return;
1661 obj = (union acpi_object *)response.pointer;
1663 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1664 goto exit;
1666 code = obj->integer.value;
1667 orig_code = code;
1669 if (asus->driver->key_filter) {
1670 asus->driver->key_filter(asus->driver, &code, &key_value,
1671 &autorelease);
1672 if (code == ASUS_WMI_KEY_IGNORE)
1673 goto exit;
1676 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1677 code = ASUS_WMI_BRN_UP;
1678 else if (code >= NOTIFY_BRNDOWN_MIN &&
1679 code <= NOTIFY_BRNDOWN_MAX)
1680 code = ASUS_WMI_BRN_DOWN;
1682 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1683 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1684 asus_wmi_backlight_notify(asus, orig_code);
1685 goto exit;
1689 if (code == NOTIFY_KBD_BRTUP) {
1690 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1691 goto exit;
1693 if (code == NOTIFY_KBD_BRTDWN) {
1694 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
1695 goto exit;
1697 if (code == NOTIFY_KBD_BRTTOGGLE) {
1698 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
1699 kbd_led_set_by_kbd(asus, 0);
1700 else
1701 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1702 goto exit;
1705 if (code == NOTIFY_FNLOCK_TOGGLE) {
1706 asus->fnlock_locked = !asus->fnlock_locked;
1707 asus_wmi_fnlock_update(asus);
1708 goto exit;
1711 if (is_display_toggle(code) &&
1712 asus->driver->quirks->no_display_toggle)
1713 goto exit;
1715 if (!sparse_keymap_report_event(asus->inputdev, code,
1716 key_value, autorelease))
1717 pr_info("Unknown key %x pressed\n", code);
1719 exit:
1720 kfree(obj);
1724 * Sys helpers
1726 static int parse_arg(const char *buf, unsigned long count, int *val)
1728 if (!count)
1729 return 0;
1730 if (sscanf(buf, "%i", val) != 1)
1731 return -EINVAL;
1732 return count;
1735 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1736 const char *buf, size_t count)
1738 u32 retval;
1739 int rv, err, value;
1741 value = asus_wmi_get_devstate_simple(asus, devid);
1742 if (value < 0)
1743 return value;
1745 rv = parse_arg(buf, count, &value);
1746 err = asus_wmi_set_devstate(devid, value, &retval);
1748 if (err < 0)
1749 return err;
1751 return rv;
1754 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1756 int value = asus_wmi_get_devstate_simple(asus, devid);
1758 if (value < 0)
1759 return value;
1761 return sprintf(buf, "%d\n", value);
1764 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1765 static ssize_t show_##_name(struct device *dev, \
1766 struct device_attribute *attr, \
1767 char *buf) \
1769 struct asus_wmi *asus = dev_get_drvdata(dev); \
1771 return show_sys_wmi(asus, _cm, buf); \
1773 static ssize_t store_##_name(struct device *dev, \
1774 struct device_attribute *attr, \
1775 const char *buf, size_t count) \
1777 struct asus_wmi *asus = dev_get_drvdata(dev); \
1779 return store_sys_wmi(asus, _cm, buf, count); \
1781 static struct device_attribute dev_attr_##_name = { \
1782 .attr = { \
1783 .name = __stringify(_name), \
1784 .mode = _mode }, \
1785 .show = show_##_name, \
1786 .store = store_##_name, \
1789 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1790 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1791 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
1792 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1793 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
1795 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
1796 const char *buf, size_t count)
1798 int value, rv;
1800 if (!count || sscanf(buf, "%i", &value) != 1)
1801 return -EINVAL;
1802 if (value < 0 || value > 2)
1803 return -EINVAL;
1805 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1806 if (rv < 0)
1807 return rv;
1809 return count;
1812 static DEVICE_ATTR_WO(cpufv);
1814 static struct attribute *platform_attributes[] = {
1815 &dev_attr_cpufv.attr,
1816 &dev_attr_camera.attr,
1817 &dev_attr_cardr.attr,
1818 &dev_attr_touchpad.attr,
1819 &dev_attr_lid_resume.attr,
1820 &dev_attr_als_enable.attr,
1821 NULL
1824 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1825 struct attribute *attr, int idx)
1827 struct device *dev = container_of(kobj, struct device, kobj);
1828 struct asus_wmi *asus = dev_get_drvdata(dev);
1829 bool ok = true;
1830 int devid = -1;
1832 if (attr == &dev_attr_camera.attr)
1833 devid = ASUS_WMI_DEVID_CAMERA;
1834 else if (attr == &dev_attr_cardr.attr)
1835 devid = ASUS_WMI_DEVID_CARDREADER;
1836 else if (attr == &dev_attr_touchpad.attr)
1837 devid = ASUS_WMI_DEVID_TOUCHPAD;
1838 else if (attr == &dev_attr_lid_resume.attr)
1839 devid = ASUS_WMI_DEVID_LID_RESUME;
1840 else if (attr == &dev_attr_als_enable.attr)
1841 devid = ASUS_WMI_DEVID_ALS_ENABLE;
1843 if (devid != -1)
1844 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1846 return ok ? attr->mode : 0;
1849 static const struct attribute_group platform_attribute_group = {
1850 .is_visible = asus_sysfs_is_visible,
1851 .attrs = platform_attributes
1854 static void asus_wmi_sysfs_exit(struct platform_device *device)
1856 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1859 static int asus_wmi_sysfs_init(struct platform_device *device)
1861 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1865 * Platform device
1867 static int asus_wmi_platform_init(struct asus_wmi *asus)
1869 int rv;
1871 /* INIT enable hotkeys on some models */
1872 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1873 pr_info("Initialization: %#x\n", rv);
1875 /* We don't know yet what to do with this version... */
1876 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1877 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
1878 asus->spec = rv;
1882 * The SFUN method probably allows the original driver to get the list
1883 * of features supported by a given model. For now, 0x0100 or 0x0800
1884 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1885 * The significance of others is yet to be found.
1887 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1888 pr_info("SFUN value: %#x\n", rv);
1889 asus->sfun = rv;
1893 * Eee PC and Notebooks seems to have different method_id for DSTS,
1894 * but it may also be related to the BIOS's SPEC.
1895 * Note, on most Eeepc, there is no way to check if a method exist
1896 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1897 * but once again, SPEC may probably be used for that kind of things.
1899 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1900 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1901 else
1902 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1904 /* CWAP allow to define the behavior of the Fn+F2 key,
1905 * this method doesn't seems to be present on Eee PCs */
1906 if (asus->driver->quirks->wapf >= 0)
1907 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1908 asus->driver->quirks->wapf, NULL);
1910 return asus_wmi_sysfs_init(asus->platform_device);
1913 static void asus_wmi_platform_exit(struct asus_wmi *asus)
1915 asus_wmi_sysfs_exit(asus->platform_device);
1919 * debugfs
1921 struct asus_wmi_debugfs_node {
1922 struct asus_wmi *asus;
1923 char *name;
1924 int (*show) (struct seq_file *m, void *data);
1927 static int show_dsts(struct seq_file *m, void *data)
1929 struct asus_wmi *asus = m->private;
1930 int err;
1931 u32 retval = -1;
1933 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1935 if (err < 0)
1936 return err;
1938 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1940 return 0;
1943 static int show_devs(struct seq_file *m, void *data)
1945 struct asus_wmi *asus = m->private;
1946 int err;
1947 u32 retval = -1;
1949 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1950 &retval);
1952 if (err < 0)
1953 return err;
1955 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1956 asus->debug.ctrl_param, retval);
1958 return 0;
1961 static int show_call(struct seq_file *m, void *data)
1963 struct asus_wmi *asus = m->private;
1964 struct bios_args args = {
1965 .arg0 = asus->debug.dev_id,
1966 .arg1 = asus->debug.ctrl_param,
1968 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1969 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1970 union acpi_object *obj;
1971 acpi_status status;
1973 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1974 0, asus->debug.method_id,
1975 &input, &output);
1977 if (ACPI_FAILURE(status))
1978 return -EIO;
1980 obj = (union acpi_object *)output.pointer;
1981 if (obj && obj->type == ACPI_TYPE_INTEGER)
1982 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1983 asus->debug.dev_id, asus->debug.ctrl_param,
1984 (u32) obj->integer.value);
1985 else
1986 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1987 asus->debug.dev_id, asus->debug.ctrl_param,
1988 obj ? obj->type : -1);
1990 kfree(obj);
1992 return 0;
1995 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1996 {NULL, "devs", show_devs},
1997 {NULL, "dsts", show_dsts},
1998 {NULL, "call", show_call},
2001 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2003 struct asus_wmi_debugfs_node *node = inode->i_private;
2005 return single_open(file, node->show, node->asus);
2008 static const struct file_operations asus_wmi_debugfs_io_ops = {
2009 .owner = THIS_MODULE,
2010 .open = asus_wmi_debugfs_open,
2011 .read = seq_read,
2012 .llseek = seq_lseek,
2013 .release = single_release,
2016 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2018 debugfs_remove_recursive(asus->debug.root);
2021 static int asus_wmi_debugfs_init(struct asus_wmi *asus)
2023 struct dentry *dent;
2024 int i;
2026 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2027 if (!asus->debug.root) {
2028 pr_err("failed to create debugfs directory\n");
2029 goto error_debugfs;
2032 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2033 asus->debug.root, &asus->debug.method_id);
2034 if (!dent)
2035 goto error_debugfs;
2037 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2038 asus->debug.root, &asus->debug.dev_id);
2039 if (!dent)
2040 goto error_debugfs;
2042 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2043 asus->debug.root, &asus->debug.ctrl_param);
2044 if (!dent)
2045 goto error_debugfs;
2047 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2048 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2050 node->asus = asus;
2051 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2052 asus->debug.root, node,
2053 &asus_wmi_debugfs_io_ops);
2054 if (!dent) {
2055 pr_err("failed to create debug file: %s\n", node->name);
2056 goto error_debugfs;
2060 return 0;
2062 error_debugfs:
2063 asus_wmi_debugfs_exit(asus);
2064 return -ENOMEM;
2067 static int asus_wmi_fan_init(struct asus_wmi *asus)
2069 int status;
2071 asus->asus_hwmon_pwm = -1;
2072 asus->asus_hwmon_num_fans = -1;
2073 asus->asus_hwmon_fan_manual_mode = false;
2075 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2076 if (status) {
2077 asus->asus_hwmon_num_fans = 0;
2078 pr_warn("Could not determine number of fans: %d\n", status);
2079 return -ENXIO;
2082 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2083 return 0;
2087 * WMI Driver
2089 static int asus_wmi_add(struct platform_device *pdev)
2091 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2092 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2093 struct asus_wmi *asus;
2094 const char *chassis_type;
2095 acpi_status status;
2096 int err;
2097 u32 result;
2099 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2100 if (!asus)
2101 return -ENOMEM;
2103 asus->driver = wdrv;
2104 asus->platform_device = pdev;
2105 wdrv->platform_device = pdev;
2106 platform_set_drvdata(asus->platform_device, asus);
2108 if (wdrv->detect_quirks)
2109 wdrv->detect_quirks(asus->driver);
2111 err = asus_wmi_platform_init(asus);
2112 if (err)
2113 goto fail_platform;
2115 err = asus_wmi_input_init(asus);
2116 if (err)
2117 goto fail_input;
2119 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2120 asus_hwmon_fan_set_auto(asus);
2122 err = asus_wmi_hwmon_init(asus);
2123 if (err)
2124 goto fail_hwmon;
2126 err = asus_wmi_led_init(asus);
2127 if (err)
2128 goto fail_leds;
2130 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2131 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2132 asus->driver->wlan_ctrl_by_user = 1;
2134 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2135 err = asus_wmi_rfkill_init(asus);
2136 if (err)
2137 goto fail_rfkill;
2140 if (asus->driver->quirks->wmi_force_als_set)
2141 asus_wmi_set_als();
2143 /* Some Asus desktop boards export an acpi-video backlight interface,
2144 stop this from showing up */
2145 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2146 if (chassis_type && !strcmp(chassis_type, "3"))
2147 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2149 if (asus->driver->quirks->wmi_backlight_power)
2150 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2152 if (asus->driver->quirks->wmi_backlight_native)
2153 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2155 if (asus->driver->quirks->xusb2pr)
2156 asus_wmi_set_xusb2pr(asus);
2158 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2159 err = asus_wmi_backlight_init(asus);
2160 if (err && err != -ENODEV)
2161 goto fail_backlight;
2162 } else
2163 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2165 if (asus_wmi_has_fnlock_key(asus)) {
2166 asus->fnlock_locked = true;
2167 asus_wmi_fnlock_update(asus);
2170 status = wmi_install_notify_handler(asus->driver->event_guid,
2171 asus_wmi_notify, asus);
2172 if (ACPI_FAILURE(status)) {
2173 pr_err("Unable to register notify handler - %d\n", status);
2174 err = -ENODEV;
2175 goto fail_wmi_handler;
2178 err = asus_wmi_debugfs_init(asus);
2179 if (err)
2180 goto fail_debugfs;
2182 return 0;
2184 fail_debugfs:
2185 wmi_remove_notify_handler(asus->driver->event_guid);
2186 fail_wmi_handler:
2187 asus_wmi_backlight_exit(asus);
2188 fail_backlight:
2189 asus_wmi_rfkill_exit(asus);
2190 fail_rfkill:
2191 asus_wmi_led_exit(asus);
2192 fail_leds:
2193 fail_hwmon:
2194 asus_wmi_input_exit(asus);
2195 fail_input:
2196 asus_wmi_platform_exit(asus);
2197 fail_platform:
2198 kfree(asus);
2199 return err;
2202 static int asus_wmi_remove(struct platform_device *device)
2204 struct asus_wmi *asus;
2206 asus = platform_get_drvdata(device);
2207 wmi_remove_notify_handler(asus->driver->event_guid);
2208 asus_wmi_backlight_exit(asus);
2209 asus_wmi_input_exit(asus);
2210 asus_wmi_led_exit(asus);
2211 asus_wmi_rfkill_exit(asus);
2212 asus_wmi_debugfs_exit(asus);
2213 asus_wmi_platform_exit(asus);
2214 asus_hwmon_fan_set_auto(asus);
2216 kfree(asus);
2217 return 0;
2221 * Platform driver - hibernate/resume callbacks
2223 static int asus_hotk_thaw(struct device *device)
2225 struct asus_wmi *asus = dev_get_drvdata(device);
2227 if (asus->wlan.rfkill) {
2228 bool wlan;
2231 * Work around bios bug - acpi _PTS turns off the wireless led
2232 * during suspend. Normally it restores it on resume, but
2233 * we should kick it ourselves in case hibernation is aborted.
2235 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2236 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2239 return 0;
2242 static int asus_hotk_resume(struct device *device)
2244 struct asus_wmi *asus = dev_get_drvdata(device);
2246 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2247 kbd_led_update(asus);
2249 if (asus_wmi_has_fnlock_key(asus))
2250 asus_wmi_fnlock_update(asus);
2251 return 0;
2254 static int asus_hotk_restore(struct device *device)
2256 struct asus_wmi *asus = dev_get_drvdata(device);
2257 int bl;
2259 /* Refresh both wlan rfkill state and pci hotplug */
2260 if (asus->wlan.rfkill)
2261 asus_rfkill_hotplug(asus);
2263 if (asus->bluetooth.rfkill) {
2264 bl = !asus_wmi_get_devstate_simple(asus,
2265 ASUS_WMI_DEVID_BLUETOOTH);
2266 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2268 if (asus->wimax.rfkill) {
2269 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2270 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2272 if (asus->wwan3g.rfkill) {
2273 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2274 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2276 if (asus->gps.rfkill) {
2277 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2278 rfkill_set_sw_state(asus->gps.rfkill, bl);
2280 if (asus->uwb.rfkill) {
2281 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2282 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2284 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2285 kbd_led_update(asus);
2287 if (asus_wmi_has_fnlock_key(asus))
2288 asus_wmi_fnlock_update(asus);
2289 return 0;
2292 static const struct dev_pm_ops asus_pm_ops = {
2293 .thaw = asus_hotk_thaw,
2294 .restore = asus_hotk_restore,
2295 .resume = asus_hotk_resume,
2298 static int asus_wmi_probe(struct platform_device *pdev)
2300 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2301 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2302 int ret;
2304 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2305 pr_warn("ASUS Management GUID not found\n");
2306 return -ENODEV;
2309 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2310 pr_warn("ASUS Event GUID not found\n");
2311 return -ENODEV;
2314 if (wdrv->probe) {
2315 ret = wdrv->probe(pdev);
2316 if (ret)
2317 return ret;
2320 return asus_wmi_add(pdev);
2323 static bool used;
2325 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2327 struct platform_driver *platform_driver;
2328 struct platform_device *platform_device;
2330 if (used)
2331 return -EBUSY;
2333 platform_driver = &driver->platform_driver;
2334 platform_driver->remove = asus_wmi_remove;
2335 platform_driver->driver.owner = driver->owner;
2336 platform_driver->driver.name = driver->name;
2337 platform_driver->driver.pm = &asus_pm_ops;
2339 platform_device = platform_create_bundle(platform_driver,
2340 asus_wmi_probe,
2341 NULL, 0, NULL, 0);
2342 if (IS_ERR(platform_device))
2343 return PTR_ERR(platform_device);
2345 used = true;
2346 return 0;
2348 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2350 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2352 platform_device_unregister(driver->platform_device);
2353 platform_driver_unregister(&driver->platform_driver);
2354 used = false;
2356 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2358 static int __init asus_wmi_init(void)
2360 pr_info("ASUS WMI generic driver loaded\n");
2361 return 0;
2364 static void __exit asus_wmi_exit(void)
2366 pr_info("ASUS WMI generic driver unloaded\n");
2369 module_init(asus_wmi_init);
2370 module_exit(asus_wmi_exit);