Linux 4.14.51
[linux/fpc-iii.git] / drivers / platform / x86 / asus-wmi.c
blob48e1541dc8d4efa3cf24e9c0df5861aeaa431798
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_device.h>
47 #include <linux/thermal.h>
48 #include <linux/acpi.h>
49 #include <linux/dmi.h>
50 #include <acpi/video.h>
52 #include "asus-wmi.h"
54 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
55 "Yong Wang <yong.y.wang@intel.com>");
56 MODULE_DESCRIPTION("Asus Generic WMI Driver");
57 MODULE_LICENSE("GPL");
59 #define to_asus_wmi_driver(pdrv) \
60 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
62 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
64 #define NOTIFY_BRNUP_MIN 0x11
65 #define NOTIFY_BRNUP_MAX 0x1f
66 #define NOTIFY_BRNDOWN_MIN 0x20
67 #define NOTIFY_BRNDOWN_MAX 0x2e
68 #define NOTIFY_KBD_BRTUP 0xc4
69 #define NOTIFY_KBD_BRTDWN 0xc5
71 /* WMI Methods */
72 #define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
73 #define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
74 #define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
75 #define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
76 #define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
77 #define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
78 #define ASUS_WMI_METHODID_AGFN 0x4E464741 /* FaN? */
79 #define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
80 #define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
81 #define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
82 #define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
83 #define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
84 #define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
85 #define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
86 #define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
87 #define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
88 #define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
89 #define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
90 #define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
91 #define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
93 #define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
95 /* Wireless */
96 #define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
97 #define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
98 #define ASUS_WMI_DEVID_CWAP 0x00010003
99 #define ASUS_WMI_DEVID_WLAN 0x00010011
100 #define ASUS_WMI_DEVID_WLAN_LED 0x00010012
101 #define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
102 #define ASUS_WMI_DEVID_GPS 0x00010015
103 #define ASUS_WMI_DEVID_WIMAX 0x00010017
104 #define ASUS_WMI_DEVID_WWAN3G 0x00010019
105 #define ASUS_WMI_DEVID_UWB 0x00010021
107 /* Leds */
108 /* 0x000200XX and 0x000400XX */
109 #define ASUS_WMI_DEVID_LED1 0x00020011
110 #define ASUS_WMI_DEVID_LED2 0x00020012
111 #define ASUS_WMI_DEVID_LED3 0x00020013
112 #define ASUS_WMI_DEVID_LED4 0x00020014
113 #define ASUS_WMI_DEVID_LED5 0x00020015
114 #define ASUS_WMI_DEVID_LED6 0x00020016
116 /* Backlight and Brightness */
117 #define ASUS_WMI_DEVID_ALS_ENABLE 0x00050001 /* Ambient Light Sensor */
118 #define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
119 #define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
120 #define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
121 #define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
123 /* Misc */
124 #define ASUS_WMI_DEVID_CAMERA 0x00060013
126 /* Storage */
127 #define ASUS_WMI_DEVID_CARDREADER 0x00080013
129 /* Input */
130 #define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
131 #define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
133 /* Fan, Thermal */
134 #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
135 #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
137 /* Power */
138 #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
140 /* Deep S3 / Resume on LID open */
141 #define ASUS_WMI_DEVID_LID_RESUME 0x00120031
143 /* DSTS masks */
144 #define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
145 #define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
146 #define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
147 #define ASUS_WMI_DSTS_USER_BIT 0x00020000
148 #define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
149 #define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
150 #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
152 #define ASUS_FAN_DESC "cpu_fan"
153 #define ASUS_FAN_MFUN 0x13
154 #define ASUS_FAN_SFUN_READ 0x06
155 #define ASUS_FAN_SFUN_WRITE 0x07
156 #define ASUS_FAN_CTRL_MANUAL 1
157 #define ASUS_FAN_CTRL_AUTO 2
159 #define USB_INTEL_XUSB2PR 0xD0
160 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
162 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
164 struct bios_args {
165 u32 arg0;
166 u32 arg1;
167 } __packed;
170 * Struct that's used for all methods called via AGFN. Naming is
171 * identically to the AML code.
173 struct agfn_args {
174 u16 mfun; /* probably "Multi-function" to be called */
175 u16 sfun; /* probably "Sub-function" to be called */
176 u16 len; /* size of the hole struct, including subfunction fields */
177 u8 stas; /* not used by now */
178 u8 err; /* zero on success */
179 } __packed;
181 /* struct used for calling fan read and write methods */
182 struct fan_args {
183 struct agfn_args agfn; /* common fields */
184 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
185 u32 speed; /* read: RPM/100 - write: 0-255 */
186 } __packed;
189 * <platform>/ - debugfs root directory
190 * dev_id - current dev_id
191 * ctrl_param - current ctrl_param
192 * method_id - current method_id
193 * devs - call DEVS(dev_id, ctrl_param) and print result
194 * dsts - call DSTS(dev_id) and print result
195 * call - call method_id(dev_id, ctrl_param) and print result
197 struct asus_wmi_debug {
198 struct dentry *root;
199 u32 method_id;
200 u32 dev_id;
201 u32 ctrl_param;
204 struct asus_rfkill {
205 struct asus_wmi *asus;
206 struct rfkill *rfkill;
207 u32 dev_id;
210 struct asus_wmi {
211 int dsts_id;
212 int spec;
213 int sfun;
215 struct input_dev *inputdev;
216 struct backlight_device *backlight_device;
217 struct platform_device *platform_device;
219 struct led_classdev wlan_led;
220 int wlan_led_wk;
221 struct led_classdev tpd_led;
222 int tpd_led_wk;
223 struct led_classdev kbd_led;
224 int kbd_led_wk;
225 struct workqueue_struct *led_workqueue;
226 struct work_struct tpd_led_work;
227 struct work_struct kbd_led_work;
228 struct work_struct wlan_led_work;
230 struct asus_rfkill wlan;
231 struct asus_rfkill bluetooth;
232 struct asus_rfkill wimax;
233 struct asus_rfkill wwan3g;
234 struct asus_rfkill gps;
235 struct asus_rfkill uwb;
237 bool asus_hwmon_fan_manual_mode;
238 int asus_hwmon_num_fans;
239 int asus_hwmon_pwm;
241 struct hotplug_slot *hotplug_slot;
242 struct mutex hotplug_lock;
243 struct mutex wmi_lock;
244 struct workqueue_struct *hotplug_workqueue;
245 struct work_struct hotplug_work;
247 struct asus_wmi_debug debug;
249 struct asus_wmi_driver *driver;
252 static int asus_wmi_input_init(struct asus_wmi *asus)
254 int err;
256 asus->inputdev = input_allocate_device();
257 if (!asus->inputdev)
258 return -ENOMEM;
260 asus->inputdev->name = asus->driver->input_name;
261 asus->inputdev->phys = asus->driver->input_phys;
262 asus->inputdev->id.bustype = BUS_HOST;
263 asus->inputdev->dev.parent = &asus->platform_device->dev;
264 set_bit(EV_REP, asus->inputdev->evbit);
266 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
267 if (err)
268 goto err_free_dev;
270 err = input_register_device(asus->inputdev);
271 if (err)
272 goto err_free_dev;
274 return 0;
276 err_free_dev:
277 input_free_device(asus->inputdev);
278 return err;
281 static void asus_wmi_input_exit(struct asus_wmi *asus)
283 if (asus->inputdev)
284 input_unregister_device(asus->inputdev);
286 asus->inputdev = NULL;
289 static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
290 u32 *retval)
292 struct bios_args args = {
293 .arg0 = arg0,
294 .arg1 = arg1,
296 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
297 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
298 acpi_status status;
299 union acpi_object *obj;
300 u32 tmp = 0;
302 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
303 &input, &output);
305 if (ACPI_FAILURE(status))
306 goto exit;
308 obj = (union acpi_object *)output.pointer;
309 if (obj && obj->type == ACPI_TYPE_INTEGER)
310 tmp = (u32) obj->integer.value;
312 if (retval)
313 *retval = tmp;
315 kfree(obj);
317 exit:
318 if (ACPI_FAILURE(status))
319 return -EIO;
321 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
322 return -ENODEV;
324 return 0;
327 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
329 struct acpi_buffer input;
330 u64 phys_addr;
331 u32 retval;
332 u32 status = -1;
335 * Copy to dma capable address otherwise memory corruption occurs as
336 * bios has to be able to access it.
338 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
339 input.length = args.length;
340 if (!input.pointer)
341 return -ENOMEM;
342 phys_addr = virt_to_phys(input.pointer);
343 memcpy(input.pointer, args.pointer, args.length);
345 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
346 phys_addr, 0, &retval);
347 if (!status)
348 memcpy(args.pointer, input.pointer, args.length);
350 kfree(input.pointer);
351 if (status)
352 return -ENXIO;
354 return retval;
357 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
359 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
362 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
363 u32 *retval)
365 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
366 ctrl_param, retval);
369 /* Helper for special devices with magic return codes */
370 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
371 u32 dev_id, u32 mask)
373 u32 retval = 0;
374 int err;
376 err = asus_wmi_get_devstate(asus, dev_id, &retval);
378 if (err < 0)
379 return err;
381 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
382 return -ENODEV;
384 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
385 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
386 return -ENODEV;
389 return retval & mask;
392 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
394 return asus_wmi_get_devstate_bits(asus, dev_id,
395 ASUS_WMI_DSTS_STATUS_BIT);
399 * LEDs
402 * These functions actually update the LED's, and are called from a
403 * workqueue. By doing this as separate work rather than when the LED
404 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
405 * potentially bad time, such as a timer interrupt.
407 static void tpd_led_update(struct work_struct *work)
409 int ctrl_param;
410 struct asus_wmi *asus;
412 asus = container_of(work, struct asus_wmi, tpd_led_work);
414 ctrl_param = asus->tpd_led_wk;
415 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
418 static void tpd_led_set(struct led_classdev *led_cdev,
419 enum led_brightness value)
421 struct asus_wmi *asus;
423 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
425 asus->tpd_led_wk = !!value;
426 queue_work(asus->led_workqueue, &asus->tpd_led_work);
429 static int read_tpd_led_state(struct asus_wmi *asus)
431 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
434 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
436 struct asus_wmi *asus;
438 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
440 return read_tpd_led_state(asus);
443 static void kbd_led_update(struct work_struct *work)
445 int ctrl_param = 0;
446 struct asus_wmi *asus;
448 asus = container_of(work, struct asus_wmi, kbd_led_work);
451 * bits 0-2: level
452 * bit 7: light on/off
454 if (asus->kbd_led_wk > 0)
455 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
457 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
460 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
462 int retval;
465 * bits 0-2: level
466 * bit 7: light on/off
467 * bit 8-10: environment (0: dark, 1: normal, 2: light)
468 * bit 17: status unknown
470 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
471 0xFFFF);
473 /* Unknown status is considered as off */
474 if (retval == 0x8000)
475 retval = 0;
477 if (retval >= 0) {
478 if (level)
479 *level = retval & 0x7F;
480 if (env)
481 *env = (retval >> 8) & 0x7F;
482 retval = 0;
485 return retval;
488 static void kbd_led_set(struct led_classdev *led_cdev,
489 enum led_brightness value)
491 struct asus_wmi *asus;
493 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
495 if (value > asus->kbd_led.max_brightness)
496 value = asus->kbd_led.max_brightness;
497 else if (value < 0)
498 value = 0;
500 asus->kbd_led_wk = value;
501 queue_work(asus->led_workqueue, &asus->kbd_led_work);
504 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
506 struct asus_wmi *asus;
507 int retval, value;
509 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
511 retval = kbd_led_read(asus, &value, NULL);
513 if (retval < 0)
514 return retval;
516 return value;
519 static int wlan_led_unknown_state(struct asus_wmi *asus)
521 u32 result;
523 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
525 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
528 static int wlan_led_presence(struct asus_wmi *asus)
530 u32 result;
532 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
534 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
537 static void wlan_led_update(struct work_struct *work)
539 int ctrl_param;
540 struct asus_wmi *asus;
542 asus = container_of(work, struct asus_wmi, wlan_led_work);
544 ctrl_param = asus->wlan_led_wk;
545 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
548 static void wlan_led_set(struct led_classdev *led_cdev,
549 enum led_brightness value)
551 struct asus_wmi *asus;
553 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
555 asus->wlan_led_wk = !!value;
556 queue_work(asus->led_workqueue, &asus->wlan_led_work);
559 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
561 struct asus_wmi *asus;
562 u32 result;
564 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
565 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
567 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
570 static void asus_wmi_led_exit(struct asus_wmi *asus)
572 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
573 led_classdev_unregister(&asus->kbd_led);
574 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
575 led_classdev_unregister(&asus->tpd_led);
576 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
577 led_classdev_unregister(&asus->wlan_led);
578 if (asus->led_workqueue)
579 destroy_workqueue(asus->led_workqueue);
582 static int asus_wmi_led_init(struct asus_wmi *asus)
584 int rv = 0, led_val;
586 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
587 if (!asus->led_workqueue)
588 return -ENOMEM;
590 if (read_tpd_led_state(asus) >= 0) {
591 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
593 asus->tpd_led.name = "asus::touchpad";
594 asus->tpd_led.brightness_set = tpd_led_set;
595 asus->tpd_led.brightness_get = tpd_led_get;
596 asus->tpd_led.max_brightness = 1;
598 rv = led_classdev_register(&asus->platform_device->dev,
599 &asus->tpd_led);
600 if (rv)
601 goto error;
604 led_val = kbd_led_read(asus, NULL, NULL);
605 if (led_val >= 0) {
606 INIT_WORK(&asus->kbd_led_work, kbd_led_update);
608 asus->kbd_led_wk = led_val;
609 asus->kbd_led.name = "asus::kbd_backlight";
610 asus->kbd_led.brightness_set = kbd_led_set;
611 asus->kbd_led.brightness_get = kbd_led_get;
612 asus->kbd_led.max_brightness = 3;
614 rv = led_classdev_register(&asus->platform_device->dev,
615 &asus->kbd_led);
616 if (rv)
617 goto error;
620 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
621 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
623 asus->wlan_led.name = "asus::wlan";
624 asus->wlan_led.brightness_set = wlan_led_set;
625 if (!wlan_led_unknown_state(asus))
626 asus->wlan_led.brightness_get = wlan_led_get;
627 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
628 asus->wlan_led.max_brightness = 1;
629 asus->wlan_led.default_trigger = "asus-wlan";
631 rv = led_classdev_register(&asus->platform_device->dev,
632 &asus->wlan_led);
635 error:
636 if (rv)
637 asus_wmi_led_exit(asus);
639 return rv;
644 * PCI hotplug (for wlan rfkill)
646 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
648 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
650 if (result < 0)
651 return false;
652 return !result;
655 static void asus_rfkill_hotplug(struct asus_wmi *asus)
657 struct pci_dev *dev;
658 struct pci_bus *bus;
659 bool blocked;
660 bool absent;
661 u32 l;
663 mutex_lock(&asus->wmi_lock);
664 blocked = asus_wlan_rfkill_blocked(asus);
665 mutex_unlock(&asus->wmi_lock);
667 mutex_lock(&asus->hotplug_lock);
668 pci_lock_rescan_remove();
670 if (asus->wlan.rfkill)
671 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
673 if (asus->hotplug_slot) {
674 bus = pci_find_bus(0, 1);
675 if (!bus) {
676 pr_warn("Unable to find PCI bus 1?\n");
677 goto out_unlock;
680 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
681 pr_err("Unable to read PCI config space?\n");
682 goto out_unlock;
684 absent = (l == 0xffffffff);
686 if (blocked != absent) {
687 pr_warn("BIOS says wireless lan is %s, "
688 "but the pci device is %s\n",
689 blocked ? "blocked" : "unblocked",
690 absent ? "absent" : "present");
691 pr_warn("skipped wireless hotplug as probably "
692 "inappropriate for this model\n");
693 goto out_unlock;
696 if (!blocked) {
697 dev = pci_get_slot(bus, 0);
698 if (dev) {
699 /* Device already present */
700 pci_dev_put(dev);
701 goto out_unlock;
703 dev = pci_scan_single_device(bus, 0);
704 if (dev) {
705 pci_bus_assign_resources(bus);
706 pci_bus_add_device(dev);
708 } else {
709 dev = pci_get_slot(bus, 0);
710 if (dev) {
711 pci_stop_and_remove_bus_device(dev);
712 pci_dev_put(dev);
717 out_unlock:
718 pci_unlock_rescan_remove();
719 mutex_unlock(&asus->hotplug_lock);
722 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
724 struct asus_wmi *asus = data;
726 if (event != ACPI_NOTIFY_BUS_CHECK)
727 return;
730 * We can't call directly asus_rfkill_hotplug because most
731 * of the time WMBC is still being executed and not reetrant.
732 * There is currently no way to tell ACPICA that we want this
733 * method to be serialized, we schedule a asus_rfkill_hotplug
734 * call later, in a safer context.
736 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
739 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
741 acpi_status status;
742 acpi_handle handle;
744 status = acpi_get_handle(NULL, node, &handle);
746 if (ACPI_SUCCESS(status)) {
747 status = acpi_install_notify_handler(handle,
748 ACPI_SYSTEM_NOTIFY,
749 asus_rfkill_notify, asus);
750 if (ACPI_FAILURE(status))
751 pr_warn("Failed to register notify on %s\n", node);
752 } else
753 return -ENODEV;
755 return 0;
758 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
760 acpi_status status = AE_OK;
761 acpi_handle handle;
763 status = acpi_get_handle(NULL, node, &handle);
765 if (ACPI_SUCCESS(status)) {
766 status = acpi_remove_notify_handler(handle,
767 ACPI_SYSTEM_NOTIFY,
768 asus_rfkill_notify);
769 if (ACPI_FAILURE(status))
770 pr_err("Error removing rfkill notify handler %s\n",
771 node);
775 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
776 u8 *value)
778 struct asus_wmi *asus = hotplug_slot->private;
779 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
781 if (result < 0)
782 return result;
784 *value = !!result;
785 return 0;
788 static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
790 kfree(hotplug_slot->info);
791 kfree(hotplug_slot);
794 static struct hotplug_slot_ops asus_hotplug_slot_ops = {
795 .owner = THIS_MODULE,
796 .get_adapter_status = asus_get_adapter_status,
797 .get_power_status = asus_get_adapter_status,
800 static void asus_hotplug_work(struct work_struct *work)
802 struct asus_wmi *asus;
804 asus = container_of(work, struct asus_wmi, hotplug_work);
805 asus_rfkill_hotplug(asus);
808 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
810 int ret = -ENOMEM;
811 struct pci_bus *bus = pci_find_bus(0, 1);
813 if (!bus) {
814 pr_err("Unable to find wifi PCI bus\n");
815 return -ENODEV;
818 asus->hotplug_workqueue =
819 create_singlethread_workqueue("hotplug_workqueue");
820 if (!asus->hotplug_workqueue)
821 goto error_workqueue;
823 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
825 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
826 if (!asus->hotplug_slot)
827 goto error_slot;
829 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
830 GFP_KERNEL);
831 if (!asus->hotplug_slot->info)
832 goto error_info;
834 asus->hotplug_slot->private = asus;
835 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
836 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
837 asus_get_adapter_status(asus->hotplug_slot,
838 &asus->hotplug_slot->info->adapter_status);
840 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
841 if (ret) {
842 pr_err("Unable to register hotplug slot - %d\n", ret);
843 goto error_register;
846 return 0;
848 error_register:
849 kfree(asus->hotplug_slot->info);
850 error_info:
851 kfree(asus->hotplug_slot);
852 asus->hotplug_slot = NULL;
853 error_slot:
854 destroy_workqueue(asus->hotplug_workqueue);
855 error_workqueue:
856 return ret;
860 * Rfkill devices
862 static int asus_rfkill_set(void *data, bool blocked)
864 struct asus_rfkill *priv = data;
865 u32 ctrl_param = !blocked;
866 u32 dev_id = priv->dev_id;
869 * If the user bit is set, BIOS can't set and record the wlan status,
870 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
871 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
872 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
873 * while setting the wlan status through WMI.
874 * This is also the behavior that windows app will do.
876 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
877 priv->asus->driver->wlan_ctrl_by_user)
878 dev_id = ASUS_WMI_DEVID_WLAN_LED;
880 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
883 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
885 struct asus_rfkill *priv = data;
886 int result;
888 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
890 if (result < 0)
891 return;
893 rfkill_set_sw_state(priv->rfkill, !result);
896 static int asus_rfkill_wlan_set(void *data, bool blocked)
898 struct asus_rfkill *priv = data;
899 struct asus_wmi *asus = priv->asus;
900 int ret;
903 * This handler is enabled only if hotplug is enabled.
904 * In this case, the asus_wmi_set_devstate() will
905 * trigger a wmi notification and we need to wait
906 * this call to finish before being able to call
907 * any wmi method
909 mutex_lock(&asus->wmi_lock);
910 ret = asus_rfkill_set(data, blocked);
911 mutex_unlock(&asus->wmi_lock);
912 return ret;
915 static const struct rfkill_ops asus_rfkill_wlan_ops = {
916 .set_block = asus_rfkill_wlan_set,
917 .query = asus_rfkill_query,
920 static const struct rfkill_ops asus_rfkill_ops = {
921 .set_block = asus_rfkill_set,
922 .query = asus_rfkill_query,
925 static int asus_new_rfkill(struct asus_wmi *asus,
926 struct asus_rfkill *arfkill,
927 const char *name, enum rfkill_type type, int dev_id)
929 int result = asus_wmi_get_devstate_simple(asus, dev_id);
930 struct rfkill **rfkill = &arfkill->rfkill;
932 if (result < 0)
933 return result;
935 arfkill->dev_id = dev_id;
936 arfkill->asus = asus;
938 if (dev_id == ASUS_WMI_DEVID_WLAN &&
939 asus->driver->quirks->hotplug_wireless)
940 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
941 &asus_rfkill_wlan_ops, arfkill);
942 else
943 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
944 &asus_rfkill_ops, arfkill);
946 if (!*rfkill)
947 return -EINVAL;
949 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
950 (asus->driver->quirks->wapf > 0))
951 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
953 rfkill_init_sw_state(*rfkill, !result);
954 result = rfkill_register(*rfkill);
955 if (result) {
956 rfkill_destroy(*rfkill);
957 *rfkill = NULL;
958 return result;
960 return 0;
963 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
965 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
966 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
967 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
968 if (asus->wlan.rfkill) {
969 rfkill_unregister(asus->wlan.rfkill);
970 rfkill_destroy(asus->wlan.rfkill);
971 asus->wlan.rfkill = NULL;
974 * Refresh pci hotplug in case the rfkill state was changed after
975 * asus_unregister_rfkill_notifier()
977 asus_rfkill_hotplug(asus);
978 if (asus->hotplug_slot)
979 pci_hp_deregister(asus->hotplug_slot);
980 if (asus->hotplug_workqueue)
981 destroy_workqueue(asus->hotplug_workqueue);
983 if (asus->bluetooth.rfkill) {
984 rfkill_unregister(asus->bluetooth.rfkill);
985 rfkill_destroy(asus->bluetooth.rfkill);
986 asus->bluetooth.rfkill = NULL;
988 if (asus->wimax.rfkill) {
989 rfkill_unregister(asus->wimax.rfkill);
990 rfkill_destroy(asus->wimax.rfkill);
991 asus->wimax.rfkill = NULL;
993 if (asus->wwan3g.rfkill) {
994 rfkill_unregister(asus->wwan3g.rfkill);
995 rfkill_destroy(asus->wwan3g.rfkill);
996 asus->wwan3g.rfkill = NULL;
998 if (asus->gps.rfkill) {
999 rfkill_unregister(asus->gps.rfkill);
1000 rfkill_destroy(asus->gps.rfkill);
1001 asus->gps.rfkill = NULL;
1003 if (asus->uwb.rfkill) {
1004 rfkill_unregister(asus->uwb.rfkill);
1005 rfkill_destroy(asus->uwb.rfkill);
1006 asus->uwb.rfkill = NULL;
1010 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1012 int result = 0;
1014 mutex_init(&asus->hotplug_lock);
1015 mutex_init(&asus->wmi_lock);
1017 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1018 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1020 if (result && result != -ENODEV)
1021 goto exit;
1023 result = asus_new_rfkill(asus, &asus->bluetooth,
1024 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1025 ASUS_WMI_DEVID_BLUETOOTH);
1027 if (result && result != -ENODEV)
1028 goto exit;
1030 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1031 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1033 if (result && result != -ENODEV)
1034 goto exit;
1036 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1037 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1039 if (result && result != -ENODEV)
1040 goto exit;
1042 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1043 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1045 if (result && result != -ENODEV)
1046 goto exit;
1048 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1049 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1051 if (result && result != -ENODEV)
1052 goto exit;
1054 if (!asus->driver->quirks->hotplug_wireless)
1055 goto exit;
1057 result = asus_setup_pci_hotplug(asus);
1059 * If we get -EBUSY then something else is handling the PCI hotplug -
1060 * don't fail in this case
1062 if (result == -EBUSY)
1063 result = 0;
1065 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1066 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1067 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1069 * Refresh pci hotplug in case the rfkill state was changed during
1070 * setup.
1072 asus_rfkill_hotplug(asus);
1074 exit:
1075 if (result && result != -ENODEV)
1076 asus_wmi_rfkill_exit(asus);
1078 if (result == -ENODEV)
1079 result = 0;
1081 return result;
1084 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1086 struct pci_dev *xhci_pdev;
1087 u32 orig_ports_available;
1088 u32 ports_available = asus->driver->quirks->xusb2pr;
1090 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1091 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1092 NULL);
1094 if (!xhci_pdev)
1095 return;
1097 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1098 &orig_ports_available);
1100 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1101 cpu_to_le32(ports_available));
1103 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1104 orig_ports_available, ports_available);
1108 * Some devices dont support or have borcken get_als method
1109 * but still support set method.
1111 static void asus_wmi_set_als(void)
1113 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1117 * Hwmon device
1119 static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1120 int *speed)
1122 struct fan_args args = {
1123 .agfn.len = sizeof(args),
1124 .agfn.mfun = ASUS_FAN_MFUN,
1125 .agfn.sfun = ASUS_FAN_SFUN_READ,
1126 .fan = fan,
1127 .speed = 0,
1129 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1130 int status;
1132 if (fan != 1)
1133 return -EINVAL;
1135 status = asus_wmi_evaluate_method_agfn(input);
1137 if (status || args.agfn.err)
1138 return -ENXIO;
1140 if (speed)
1141 *speed = args.speed;
1143 return 0;
1146 static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1147 int *speed)
1149 struct fan_args args = {
1150 .agfn.len = sizeof(args),
1151 .agfn.mfun = ASUS_FAN_MFUN,
1152 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1153 .fan = fan,
1154 .speed = speed ? *speed : 0,
1156 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1157 int status;
1159 /* 1: for setting 1st fan's speed 0: setting auto mode */
1160 if (fan != 1 && fan != 0)
1161 return -EINVAL;
1163 status = asus_wmi_evaluate_method_agfn(input);
1165 if (status || args.agfn.err)
1166 return -ENXIO;
1168 if (speed && fan == 1)
1169 asus->asus_hwmon_pwm = *speed;
1171 return 0;
1175 * Check if we can read the speed of one fan. If true we assume we can also
1176 * control it.
1178 static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1180 int status;
1181 int speed = 0;
1183 *num_fans = 0;
1185 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1186 if (!status)
1187 *num_fans = 1;
1189 return 0;
1192 static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1194 int status;
1196 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1197 if (status)
1198 return -ENXIO;
1200 asus->asus_hwmon_fan_manual_mode = false;
1202 return 0;
1205 static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1207 struct asus_wmi *asus = dev_get_drvdata(dev);
1208 int value;
1209 int ret;
1211 /* no speed readable on manual mode */
1212 if (asus->asus_hwmon_fan_manual_mode)
1213 return -ENXIO;
1215 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1216 if (ret) {
1217 pr_warn("reading fan speed failed: %d\n", ret);
1218 return -ENXIO;
1221 return value;
1224 static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1226 int err;
1228 if (asus->asus_hwmon_pwm >= 0) {
1229 *value = asus->asus_hwmon_pwm;
1230 return;
1233 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1234 if (err < 0)
1235 return;
1237 *value &= 0xFF;
1239 if (*value == 1) /* Low Speed */
1240 *value = 85;
1241 else if (*value == 2)
1242 *value = 170;
1243 else if (*value == 3)
1244 *value = 255;
1245 else if (*value) {
1246 pr_err("Unknown fan speed %#x\n", *value);
1247 *value = -1;
1251 static ssize_t pwm1_show(struct device *dev,
1252 struct device_attribute *attr,
1253 char *buf)
1255 struct asus_wmi *asus = dev_get_drvdata(dev);
1256 int value;
1258 asus_hwmon_pwm_show(asus, 0, &value);
1260 return sprintf(buf, "%d\n", value);
1263 static ssize_t pwm1_store(struct device *dev,
1264 struct device_attribute *attr,
1265 const char *buf, size_t count) {
1266 struct asus_wmi *asus = dev_get_drvdata(dev);
1267 int value;
1268 int state;
1269 int ret;
1271 ret = kstrtouint(buf, 10, &value);
1273 if (ret)
1274 return ret;
1276 value = clamp(value, 0, 255);
1278 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1279 if (state)
1280 pr_warn("Setting fan speed failed: %d\n", state);
1281 else
1282 asus->asus_hwmon_fan_manual_mode = true;
1284 return count;
1287 static ssize_t fan1_input_show(struct device *dev,
1288 struct device_attribute *attr,
1289 char *buf)
1291 int value = asus_hwmon_fan_rpm_show(dev, 0);
1293 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1297 static ssize_t pwm1_enable_show(struct device *dev,
1298 struct device_attribute *attr,
1299 char *buf)
1301 struct asus_wmi *asus = dev_get_drvdata(dev);
1303 if (asus->asus_hwmon_fan_manual_mode)
1304 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1306 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1309 static ssize_t pwm1_enable_store(struct device *dev,
1310 struct device_attribute *attr,
1311 const char *buf, size_t count)
1313 struct asus_wmi *asus = dev_get_drvdata(dev);
1314 int status = 0;
1315 int state;
1316 int ret;
1318 ret = kstrtouint(buf, 10, &state);
1320 if (ret)
1321 return ret;
1323 if (state == ASUS_FAN_CTRL_MANUAL)
1324 asus->asus_hwmon_fan_manual_mode = true;
1325 else
1326 status = asus_hwmon_fan_set_auto(asus);
1328 if (status)
1329 return status;
1331 return count;
1334 static ssize_t fan1_label_show(struct device *dev,
1335 struct device_attribute *attr,
1336 char *buf)
1338 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1341 static ssize_t asus_hwmon_temp1(struct device *dev,
1342 struct device_attribute *attr,
1343 char *buf)
1345 struct asus_wmi *asus = dev_get_drvdata(dev);
1346 u32 value;
1347 int err;
1349 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1351 if (err < 0)
1352 return err;
1354 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1356 return sprintf(buf, "%d\n", value);
1359 /* Fan1 */
1360 static DEVICE_ATTR_RW(pwm1);
1361 static DEVICE_ATTR_RW(pwm1_enable);
1362 static DEVICE_ATTR_RO(fan1_input);
1363 static DEVICE_ATTR_RO(fan1_label);
1365 /* Temperature */
1366 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1368 static struct attribute *hwmon_attributes[] = {
1369 &dev_attr_pwm1.attr,
1370 &dev_attr_pwm1_enable.attr,
1371 &dev_attr_fan1_input.attr,
1372 &dev_attr_fan1_label.attr,
1374 &dev_attr_temp1_input.attr,
1375 NULL
1378 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1379 struct attribute *attr, int idx)
1381 struct device *dev = container_of(kobj, struct device, kobj);
1382 struct platform_device *pdev = to_platform_device(dev->parent);
1383 struct asus_wmi *asus = platform_get_drvdata(pdev);
1384 int dev_id = -1;
1385 int fan_attr = -1;
1386 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1387 bool ok = true;
1389 if (attr == &dev_attr_pwm1.attr)
1390 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1391 else if (attr == &dev_attr_temp1_input.attr)
1392 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1395 if (attr == &dev_attr_fan1_input.attr
1396 || attr == &dev_attr_fan1_label.attr
1397 || attr == &dev_attr_pwm1.attr
1398 || attr == &dev_attr_pwm1_enable.attr) {
1399 fan_attr = 1;
1402 if (dev_id != -1) {
1403 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1405 if (err < 0 && fan_attr == -1)
1406 return 0; /* can't return negative here */
1409 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1411 * We need to find a better way, probably using sfun,
1412 * bits or spec ...
1413 * Currently we disable it if:
1414 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1415 * - reverved bits are non-zero
1416 * - sfun and presence bit are not set
1418 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1419 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1420 ok = false;
1421 else
1422 ok = fan_attr <= asus->asus_hwmon_num_fans;
1423 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1424 /* If value is zero, something is clearly wrong */
1425 if (!value)
1426 ok = false;
1427 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1428 ok = true;
1429 } else {
1430 ok = false;
1433 return ok ? attr->mode : 0;
1436 static const struct attribute_group hwmon_attribute_group = {
1437 .is_visible = asus_hwmon_sysfs_is_visible,
1438 .attrs = hwmon_attributes
1440 __ATTRIBUTE_GROUPS(hwmon_attribute);
1442 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1444 struct device *hwmon;
1446 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1447 "asus", asus,
1448 hwmon_attribute_groups);
1449 if (IS_ERR(hwmon)) {
1450 pr_err("Could not register asus hwmon device\n");
1451 return PTR_ERR(hwmon);
1453 return 0;
1457 * Backlight
1459 static int read_backlight_power(struct asus_wmi *asus)
1461 int ret;
1462 if (asus->driver->quirks->store_backlight_power)
1463 ret = !asus->driver->panel_power;
1464 else
1465 ret = asus_wmi_get_devstate_simple(asus,
1466 ASUS_WMI_DEVID_BACKLIGHT);
1468 if (ret < 0)
1469 return ret;
1471 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1474 static int read_brightness_max(struct asus_wmi *asus)
1476 u32 retval;
1477 int err;
1479 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1481 if (err < 0)
1482 return err;
1484 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1485 retval >>= 8;
1487 if (!retval)
1488 return -ENODEV;
1490 return retval;
1493 static int read_brightness(struct backlight_device *bd)
1495 struct asus_wmi *asus = bl_get_data(bd);
1496 u32 retval;
1497 int err;
1499 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1501 if (err < 0)
1502 return err;
1504 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1507 static u32 get_scalar_command(struct backlight_device *bd)
1509 struct asus_wmi *asus = bl_get_data(bd);
1510 u32 ctrl_param = 0;
1512 if ((asus->driver->brightness < bd->props.brightness) ||
1513 bd->props.brightness == bd->props.max_brightness)
1514 ctrl_param = 0x00008001;
1515 else if ((asus->driver->brightness > bd->props.brightness) ||
1516 bd->props.brightness == 0)
1517 ctrl_param = 0x00008000;
1519 asus->driver->brightness = bd->props.brightness;
1521 return ctrl_param;
1524 static int update_bl_status(struct backlight_device *bd)
1526 struct asus_wmi *asus = bl_get_data(bd);
1527 u32 ctrl_param;
1528 int power, err = 0;
1530 power = read_backlight_power(asus);
1531 if (power != -ENODEV && bd->props.power != power) {
1532 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1533 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1534 ctrl_param, NULL);
1535 if (asus->driver->quirks->store_backlight_power)
1536 asus->driver->panel_power = bd->props.power;
1538 /* When using scalar brightness, updating the brightness
1539 * will mess with the backlight power */
1540 if (asus->driver->quirks->scalar_panel_brightness)
1541 return err;
1544 if (asus->driver->quirks->scalar_panel_brightness)
1545 ctrl_param = get_scalar_command(bd);
1546 else
1547 ctrl_param = bd->props.brightness;
1549 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1550 ctrl_param, NULL);
1552 return err;
1555 static const struct backlight_ops asus_wmi_bl_ops = {
1556 .get_brightness = read_brightness,
1557 .update_status = update_bl_status,
1560 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1562 struct backlight_device *bd = asus->backlight_device;
1563 int old = bd->props.brightness;
1564 int new = old;
1566 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1567 new = code - NOTIFY_BRNUP_MIN + 1;
1568 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1569 new = code - NOTIFY_BRNDOWN_MIN;
1571 bd->props.brightness = new;
1572 backlight_update_status(bd);
1573 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1575 return old;
1578 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1580 struct backlight_device *bd;
1581 struct backlight_properties props;
1582 int max;
1583 int power;
1585 max = read_brightness_max(asus);
1586 if (max < 0)
1587 return max;
1589 power = read_backlight_power(asus);
1591 if (power == -ENODEV)
1592 power = FB_BLANK_UNBLANK;
1593 else if (power < 0)
1594 return power;
1596 memset(&props, 0, sizeof(struct backlight_properties));
1597 props.type = BACKLIGHT_PLATFORM;
1598 props.max_brightness = max;
1599 bd = backlight_device_register(asus->driver->name,
1600 &asus->platform_device->dev, asus,
1601 &asus_wmi_bl_ops, &props);
1602 if (IS_ERR(bd)) {
1603 pr_err("Could not register backlight device\n");
1604 return PTR_ERR(bd);
1607 asus->backlight_device = bd;
1609 if (asus->driver->quirks->store_backlight_power)
1610 asus->driver->panel_power = power;
1612 bd->props.brightness = read_brightness(bd);
1613 bd->props.power = power;
1614 backlight_update_status(bd);
1616 asus->driver->brightness = bd->props.brightness;
1618 return 0;
1621 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1623 backlight_device_unregister(asus->backlight_device);
1625 asus->backlight_device = NULL;
1628 static int is_display_toggle(int code)
1630 /* display toggle keys */
1631 if ((code >= 0x61 && code <= 0x67) ||
1632 (code >= 0x8c && code <= 0x93) ||
1633 (code >= 0xa0 && code <= 0xa7) ||
1634 (code >= 0xd0 && code <= 0xd5))
1635 return 1;
1637 return 0;
1640 static void asus_wmi_notify(u32 value, void *context)
1642 struct asus_wmi *asus = context;
1643 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1644 union acpi_object *obj;
1645 acpi_status status;
1646 int code;
1647 int orig_code;
1648 unsigned int key_value = 1;
1649 bool autorelease = 1;
1651 status = wmi_get_event_data(value, &response);
1652 if (status != AE_OK) {
1653 pr_err("bad event status 0x%x\n", status);
1654 return;
1657 obj = (union acpi_object *)response.pointer;
1659 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1660 goto exit;
1662 code = obj->integer.value;
1663 orig_code = code;
1665 if (asus->driver->key_filter) {
1666 asus->driver->key_filter(asus->driver, &code, &key_value,
1667 &autorelease);
1668 if (code == ASUS_WMI_KEY_IGNORE)
1669 goto exit;
1672 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1673 code = ASUS_WMI_BRN_UP;
1674 else if (code >= NOTIFY_BRNDOWN_MIN &&
1675 code <= NOTIFY_BRNDOWN_MAX)
1676 code = ASUS_WMI_BRN_DOWN;
1678 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1679 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1680 asus_wmi_backlight_notify(asus, orig_code);
1681 goto exit;
1685 if (is_display_toggle(code) &&
1686 asus->driver->quirks->no_display_toggle)
1687 goto exit;
1689 if (!sparse_keymap_report_event(asus->inputdev, code,
1690 key_value, autorelease))
1691 pr_info("Unknown key %x pressed\n", code);
1693 exit:
1694 kfree(obj);
1698 * Sys helpers
1700 static int parse_arg(const char *buf, unsigned long count, int *val)
1702 if (!count)
1703 return 0;
1704 if (sscanf(buf, "%i", val) != 1)
1705 return -EINVAL;
1706 return count;
1709 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1710 const char *buf, size_t count)
1712 u32 retval;
1713 int rv, err, value;
1715 value = asus_wmi_get_devstate_simple(asus, devid);
1716 if (value < 0)
1717 return value;
1719 rv = parse_arg(buf, count, &value);
1720 err = asus_wmi_set_devstate(devid, value, &retval);
1722 if (err < 0)
1723 return err;
1725 return rv;
1728 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1730 int value = asus_wmi_get_devstate_simple(asus, devid);
1732 if (value < 0)
1733 return value;
1735 return sprintf(buf, "%d\n", value);
1738 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1739 static ssize_t show_##_name(struct device *dev, \
1740 struct device_attribute *attr, \
1741 char *buf) \
1743 struct asus_wmi *asus = dev_get_drvdata(dev); \
1745 return show_sys_wmi(asus, _cm, buf); \
1747 static ssize_t store_##_name(struct device *dev, \
1748 struct device_attribute *attr, \
1749 const char *buf, size_t count) \
1751 struct asus_wmi *asus = dev_get_drvdata(dev); \
1753 return store_sys_wmi(asus, _cm, buf, count); \
1755 static struct device_attribute dev_attr_##_name = { \
1756 .attr = { \
1757 .name = __stringify(_name), \
1758 .mode = _mode }, \
1759 .show = show_##_name, \
1760 .store = store_##_name, \
1763 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1764 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1765 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
1766 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1767 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
1769 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
1770 const char *buf, size_t count)
1772 int value, rv;
1774 if (!count || sscanf(buf, "%i", &value) != 1)
1775 return -EINVAL;
1776 if (value < 0 || value > 2)
1777 return -EINVAL;
1779 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1780 if (rv < 0)
1781 return rv;
1783 return count;
1786 static DEVICE_ATTR_WO(cpufv);
1788 static struct attribute *platform_attributes[] = {
1789 &dev_attr_cpufv.attr,
1790 &dev_attr_camera.attr,
1791 &dev_attr_cardr.attr,
1792 &dev_attr_touchpad.attr,
1793 &dev_attr_lid_resume.attr,
1794 &dev_attr_als_enable.attr,
1795 NULL
1798 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1799 struct attribute *attr, int idx)
1801 struct device *dev = container_of(kobj, struct device, kobj);
1802 struct platform_device *pdev = to_platform_device(dev);
1803 struct asus_wmi *asus = platform_get_drvdata(pdev);
1804 bool ok = true;
1805 int devid = -1;
1807 if (attr == &dev_attr_camera.attr)
1808 devid = ASUS_WMI_DEVID_CAMERA;
1809 else if (attr == &dev_attr_cardr.attr)
1810 devid = ASUS_WMI_DEVID_CARDREADER;
1811 else if (attr == &dev_attr_touchpad.attr)
1812 devid = ASUS_WMI_DEVID_TOUCHPAD;
1813 else if (attr == &dev_attr_lid_resume.attr)
1814 devid = ASUS_WMI_DEVID_LID_RESUME;
1815 else if (attr == &dev_attr_als_enable.attr)
1816 devid = ASUS_WMI_DEVID_ALS_ENABLE;
1818 if (devid != -1)
1819 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1821 return ok ? attr->mode : 0;
1824 static const struct attribute_group platform_attribute_group = {
1825 .is_visible = asus_sysfs_is_visible,
1826 .attrs = platform_attributes
1829 static void asus_wmi_sysfs_exit(struct platform_device *device)
1831 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1834 static int asus_wmi_sysfs_init(struct platform_device *device)
1836 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1840 * Platform device
1842 static int asus_wmi_platform_init(struct asus_wmi *asus)
1844 int rv;
1846 /* INIT enable hotkeys on some models */
1847 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1848 pr_info("Initialization: %#x\n", rv);
1850 /* We don't know yet what to do with this version... */
1851 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1852 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
1853 asus->spec = rv;
1857 * The SFUN method probably allows the original driver to get the list
1858 * of features supported by a given model. For now, 0x0100 or 0x0800
1859 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1860 * The significance of others is yet to be found.
1862 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1863 pr_info("SFUN value: %#x\n", rv);
1864 asus->sfun = rv;
1868 * Eee PC and Notebooks seems to have different method_id for DSTS,
1869 * but it may also be related to the BIOS's SPEC.
1870 * Note, on most Eeepc, there is no way to check if a method exist
1871 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1872 * but once again, SPEC may probably be used for that kind of things.
1874 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1875 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1876 else
1877 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1879 /* CWAP allow to define the behavior of the Fn+F2 key,
1880 * this method doesn't seems to be present on Eee PCs */
1881 if (asus->driver->quirks->wapf >= 0)
1882 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1883 asus->driver->quirks->wapf, NULL);
1885 return asus_wmi_sysfs_init(asus->platform_device);
1888 static void asus_wmi_platform_exit(struct asus_wmi *asus)
1890 asus_wmi_sysfs_exit(asus->platform_device);
1894 * debugfs
1896 struct asus_wmi_debugfs_node {
1897 struct asus_wmi *asus;
1898 char *name;
1899 int (*show) (struct seq_file *m, void *data);
1902 static int show_dsts(struct seq_file *m, void *data)
1904 struct asus_wmi *asus = m->private;
1905 int err;
1906 u32 retval = -1;
1908 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1910 if (err < 0)
1911 return err;
1913 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1915 return 0;
1918 static int show_devs(struct seq_file *m, void *data)
1920 struct asus_wmi *asus = m->private;
1921 int err;
1922 u32 retval = -1;
1924 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1925 &retval);
1927 if (err < 0)
1928 return err;
1930 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1931 asus->debug.ctrl_param, retval);
1933 return 0;
1936 static int show_call(struct seq_file *m, void *data)
1938 struct asus_wmi *asus = m->private;
1939 struct bios_args args = {
1940 .arg0 = asus->debug.dev_id,
1941 .arg1 = asus->debug.ctrl_param,
1943 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1944 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1945 union acpi_object *obj;
1946 acpi_status status;
1948 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1949 0, asus->debug.method_id,
1950 &input, &output);
1952 if (ACPI_FAILURE(status))
1953 return -EIO;
1955 obj = (union acpi_object *)output.pointer;
1956 if (obj && obj->type == ACPI_TYPE_INTEGER)
1957 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1958 asus->debug.dev_id, asus->debug.ctrl_param,
1959 (u32) obj->integer.value);
1960 else
1961 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1962 asus->debug.dev_id, asus->debug.ctrl_param,
1963 obj ? obj->type : -1);
1965 kfree(obj);
1967 return 0;
1970 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1971 {NULL, "devs", show_devs},
1972 {NULL, "dsts", show_dsts},
1973 {NULL, "call", show_call},
1976 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
1978 struct asus_wmi_debugfs_node *node = inode->i_private;
1980 return single_open(file, node->show, node->asus);
1983 static const struct file_operations asus_wmi_debugfs_io_ops = {
1984 .owner = THIS_MODULE,
1985 .open = asus_wmi_debugfs_open,
1986 .read = seq_read,
1987 .llseek = seq_lseek,
1988 .release = single_release,
1991 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
1993 debugfs_remove_recursive(asus->debug.root);
1996 static int asus_wmi_debugfs_init(struct asus_wmi *asus)
1998 struct dentry *dent;
1999 int i;
2001 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2002 if (!asus->debug.root) {
2003 pr_err("failed to create debugfs directory\n");
2004 goto error_debugfs;
2007 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2008 asus->debug.root, &asus->debug.method_id);
2009 if (!dent)
2010 goto error_debugfs;
2012 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2013 asus->debug.root, &asus->debug.dev_id);
2014 if (!dent)
2015 goto error_debugfs;
2017 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2018 asus->debug.root, &asus->debug.ctrl_param);
2019 if (!dent)
2020 goto error_debugfs;
2022 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2023 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2025 node->asus = asus;
2026 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2027 asus->debug.root, node,
2028 &asus_wmi_debugfs_io_ops);
2029 if (!dent) {
2030 pr_err("failed to create debug file: %s\n", node->name);
2031 goto error_debugfs;
2035 return 0;
2037 error_debugfs:
2038 asus_wmi_debugfs_exit(asus);
2039 return -ENOMEM;
2042 static int asus_wmi_fan_init(struct asus_wmi *asus)
2044 int status;
2046 asus->asus_hwmon_pwm = -1;
2047 asus->asus_hwmon_num_fans = -1;
2048 asus->asus_hwmon_fan_manual_mode = false;
2050 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2051 if (status) {
2052 asus->asus_hwmon_num_fans = 0;
2053 pr_warn("Could not determine number of fans: %d\n", status);
2054 return -ENXIO;
2057 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2058 return 0;
2061 static bool ashs_present(void)
2063 int i = 0;
2064 while (ashs_ids[i]) {
2065 if (acpi_dev_found(ashs_ids[i++]))
2066 return true;
2068 return false;
2072 * WMI Driver
2074 static int asus_wmi_add(struct platform_device *pdev)
2076 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2077 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2078 struct asus_wmi *asus;
2079 const char *chassis_type;
2080 acpi_status status;
2081 int err;
2082 u32 result;
2084 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2085 if (!asus)
2086 return -ENOMEM;
2088 asus->driver = wdrv;
2089 asus->platform_device = pdev;
2090 wdrv->platform_device = pdev;
2091 platform_set_drvdata(asus->platform_device, asus);
2093 if (wdrv->detect_quirks)
2094 wdrv->detect_quirks(asus->driver);
2096 err = asus_wmi_platform_init(asus);
2097 if (err)
2098 goto fail_platform;
2100 err = asus_wmi_input_init(asus);
2101 if (err)
2102 goto fail_input;
2104 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2105 asus_hwmon_fan_set_auto(asus);
2107 err = asus_wmi_hwmon_init(asus);
2108 if (err)
2109 goto fail_hwmon;
2111 err = asus_wmi_led_init(asus);
2112 if (err)
2113 goto fail_leds;
2115 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2116 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2117 asus->driver->wlan_ctrl_by_user = 1;
2119 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2120 err = asus_wmi_rfkill_init(asus);
2121 if (err)
2122 goto fail_rfkill;
2125 if (asus->driver->quirks->wmi_force_als_set)
2126 asus_wmi_set_als();
2128 /* Some Asus desktop boards export an acpi-video backlight interface,
2129 stop this from showing up */
2130 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2131 if (chassis_type && !strcmp(chassis_type, "3"))
2132 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2134 if (asus->driver->quirks->wmi_backlight_power)
2135 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2137 if (asus->driver->quirks->wmi_backlight_native)
2138 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2140 if (asus->driver->quirks->xusb2pr)
2141 asus_wmi_set_xusb2pr(asus);
2143 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2144 err = asus_wmi_backlight_init(asus);
2145 if (err && err != -ENODEV)
2146 goto fail_backlight;
2149 status = wmi_install_notify_handler(asus->driver->event_guid,
2150 asus_wmi_notify, asus);
2151 if (ACPI_FAILURE(status)) {
2152 pr_err("Unable to register notify handler - %d\n", status);
2153 err = -ENODEV;
2154 goto fail_wmi_handler;
2157 err = asus_wmi_debugfs_init(asus);
2158 if (err)
2159 goto fail_debugfs;
2161 return 0;
2163 fail_debugfs:
2164 wmi_remove_notify_handler(asus->driver->event_guid);
2165 fail_wmi_handler:
2166 asus_wmi_backlight_exit(asus);
2167 fail_backlight:
2168 asus_wmi_rfkill_exit(asus);
2169 fail_rfkill:
2170 asus_wmi_led_exit(asus);
2171 fail_leds:
2172 fail_hwmon:
2173 asus_wmi_input_exit(asus);
2174 fail_input:
2175 asus_wmi_platform_exit(asus);
2176 fail_platform:
2177 kfree(asus);
2178 return err;
2181 static int asus_wmi_remove(struct platform_device *device)
2183 struct asus_wmi *asus;
2185 asus = platform_get_drvdata(device);
2186 wmi_remove_notify_handler(asus->driver->event_guid);
2187 asus_wmi_backlight_exit(asus);
2188 asus_wmi_input_exit(asus);
2189 asus_wmi_led_exit(asus);
2190 asus_wmi_rfkill_exit(asus);
2191 asus_wmi_debugfs_exit(asus);
2192 asus_wmi_platform_exit(asus);
2193 asus_hwmon_fan_set_auto(asus);
2195 kfree(asus);
2196 return 0;
2200 * Platform driver - hibernate/resume callbacks
2202 static int asus_hotk_thaw(struct device *device)
2204 struct asus_wmi *asus = dev_get_drvdata(device);
2206 if (asus->wlan.rfkill) {
2207 bool wlan;
2210 * Work around bios bug - acpi _PTS turns off the wireless led
2211 * during suspend. Normally it restores it on resume, but
2212 * we should kick it ourselves in case hibernation is aborted.
2214 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2215 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2218 return 0;
2221 static int asus_hotk_resume(struct device *device)
2223 struct asus_wmi *asus = dev_get_drvdata(device);
2225 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2226 queue_work(asus->led_workqueue, &asus->kbd_led_work);
2228 return 0;
2231 static int asus_hotk_restore(struct device *device)
2233 struct asus_wmi *asus = dev_get_drvdata(device);
2234 int bl;
2236 /* Refresh both wlan rfkill state and pci hotplug */
2237 if (asus->wlan.rfkill)
2238 asus_rfkill_hotplug(asus);
2240 if (asus->bluetooth.rfkill) {
2241 bl = !asus_wmi_get_devstate_simple(asus,
2242 ASUS_WMI_DEVID_BLUETOOTH);
2243 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2245 if (asus->wimax.rfkill) {
2246 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2247 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2249 if (asus->wwan3g.rfkill) {
2250 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2251 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2253 if (asus->gps.rfkill) {
2254 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2255 rfkill_set_sw_state(asus->gps.rfkill, bl);
2257 if (asus->uwb.rfkill) {
2258 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2259 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2261 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2262 queue_work(asus->led_workqueue, &asus->kbd_led_work);
2264 return 0;
2267 static const struct dev_pm_ops asus_pm_ops = {
2268 .thaw = asus_hotk_thaw,
2269 .restore = asus_hotk_restore,
2270 .resume = asus_hotk_resume,
2273 static int asus_wmi_probe(struct platform_device *pdev)
2275 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2276 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2277 int ret;
2279 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2280 pr_warn("Management GUID not found\n");
2281 return -ENODEV;
2284 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2285 pr_warn("Event GUID not found\n");
2286 return -ENODEV;
2289 if (wdrv->probe) {
2290 ret = wdrv->probe(pdev);
2291 if (ret)
2292 return ret;
2295 return asus_wmi_add(pdev);
2298 static bool used;
2300 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2302 struct platform_driver *platform_driver;
2303 struct platform_device *platform_device;
2305 if (used)
2306 return -EBUSY;
2308 platform_driver = &driver->platform_driver;
2309 platform_driver->remove = asus_wmi_remove;
2310 platform_driver->driver.owner = driver->owner;
2311 platform_driver->driver.name = driver->name;
2312 platform_driver->driver.pm = &asus_pm_ops;
2314 platform_device = platform_create_bundle(platform_driver,
2315 asus_wmi_probe,
2316 NULL, 0, NULL, 0);
2317 if (IS_ERR(platform_device))
2318 return PTR_ERR(platform_device);
2320 used = true;
2321 return 0;
2323 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2325 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2327 platform_device_unregister(driver->platform_device);
2328 platform_driver_unregister(&driver->platform_driver);
2329 used = false;
2331 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2333 static int __init asus_wmi_init(void)
2335 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2336 pr_info("Asus Management GUID not found\n");
2337 return -ENODEV;
2340 pr_info("ASUS WMI generic driver loaded\n");
2341 return 0;
2344 static void __exit asus_wmi_exit(void)
2346 pr_info("ASUS WMI generic driver unloaded\n");
2349 module_init(asus_wmi_init);
2350 module_exit(asus_wmi_exit);