Merge branch 'regulator-5.6' into regulator-next
[linux-2.6-microblaze.git] / drivers / platform / x86 / asus-wmi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Asus PC WMI hotkey driver
4  *
5  * Copyright(C) 2010 Intel Corporation.
6  * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
7  *
8  * Portions based on wistron_btns.c:
9  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/fb.h>
24 #include <linux/backlight.h>
25 #include <linux/leds.h>
26 #include <linux/rfkill.h>
27 #include <linux/pci.h>
28 #include <linux/pci_hotplug.h>
29 #include <linux/power_supply.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/platform_data/x86/asus-wmi.h>
35 #include <linux/platform_device.h>
36 #include <linux/thermal.h>
37 #include <linux/acpi.h>
38 #include <linux/dmi.h>
39
40 #include <acpi/battery.h>
41 #include <acpi/video.h>
42
43 #include "asus-wmi.h"
44
45 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
46               "Yong Wang <yong.y.wang@intel.com>");
47 MODULE_DESCRIPTION("Asus Generic WMI Driver");
48 MODULE_LICENSE("GPL");
49
50 #define to_asus_wmi_driver(pdrv)                                        \
51         (container_of((pdrv), struct asus_wmi_driver, platform_driver))
52
53 #define ASUS_WMI_MGMT_GUID      "97845ED0-4E6D-11DE-8A39-0800200C9A66"
54
55 #define NOTIFY_BRNUP_MIN                0x11
56 #define NOTIFY_BRNUP_MAX                0x1f
57 #define NOTIFY_BRNDOWN_MIN              0x20
58 #define NOTIFY_BRNDOWN_MAX              0x2e
59 #define NOTIFY_FNLOCK_TOGGLE            0x4e
60 #define NOTIFY_KBD_BRTUP                0xc4
61 #define NOTIFY_KBD_BRTDWN               0xc5
62 #define NOTIFY_KBD_BRTTOGGLE            0xc7
63 #define NOTIFY_KBD_FBM                  0x99
64
65 #define ASUS_WMI_FNLOCK_BIOS_DISABLED   BIT(0)
66
67 #define ASUS_FAN_DESC                   "cpu_fan"
68 #define ASUS_FAN_MFUN                   0x13
69 #define ASUS_FAN_SFUN_READ              0x06
70 #define ASUS_FAN_SFUN_WRITE             0x07
71
72 /* Based on standard hwmon pwmX_enable values */
73 #define ASUS_FAN_CTRL_FULLSPEED         0
74 #define ASUS_FAN_CTRL_MANUAL            1
75 #define ASUS_FAN_CTRL_AUTO              2
76
77 #define ASUS_FAN_BOOST_MODE_NORMAL              0
78 #define ASUS_FAN_BOOST_MODE_OVERBOOST           1
79 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK      0x01
80 #define ASUS_FAN_BOOST_MODE_SILENT              2
81 #define ASUS_FAN_BOOST_MODE_SILENT_MASK         0x02
82 #define ASUS_FAN_BOOST_MODES_MASK               0x03
83
84 #define USB_INTEL_XUSB2PR               0xD0
85 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI   0x9c31
86
87 #define ASUS_ACPI_UID_ASUSWMI           "ASUSWMI"
88 #define ASUS_ACPI_UID_ATK               "ATK"
89
90 #define WMI_EVENT_QUEUE_SIZE            0x10
91 #define WMI_EVENT_QUEUE_END             0x1
92 #define WMI_EVENT_MASK                  0xFFFF
93 /* The WMI hotkey event value is always the same. */
94 #define WMI_EVENT_VALUE_ATK             0xFF
95
96 #define WMI_EVENT_MASK                  0xFFFF
97
98 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
99
100 static bool ashs_present(void)
101 {
102         int i = 0;
103         while (ashs_ids[i]) {
104                 if (acpi_dev_found(ashs_ids[i++]))
105                         return true;
106         }
107         return false;
108 }
109
110 struct bios_args {
111         u32 arg0;
112         u32 arg1;
113         u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
114 } __packed;
115
116 /*
117  * Struct that's used for all methods called via AGFN. Naming is
118  * identically to the AML code.
119  */
120 struct agfn_args {
121         u16 mfun; /* probably "Multi-function" to be called */
122         u16 sfun; /* probably "Sub-function" to be called */
123         u16 len;  /* size of the hole struct, including subfunction fields */
124         u8 stas;  /* not used by now */
125         u8 err;   /* zero on success */
126 } __packed;
127
128 /* struct used for calling fan read and write methods */
129 struct agfn_fan_args {
130         struct agfn_args agfn;  /* common fields */
131         u8 fan;                 /* fan number: 0: set auto mode 1: 1st fan */
132         u32 speed;              /* read: RPM/100 - write: 0-255 */
133 } __packed;
134
135 /*
136  * <platform>/    - debugfs root directory
137  *   dev_id      - current dev_id
138  *   ctrl_param  - current ctrl_param
139  *   method_id   - current method_id
140  *   devs        - call DEVS(dev_id, ctrl_param) and print result
141  *   dsts        - call DSTS(dev_id)  and print result
142  *   call        - call method_id(dev_id, ctrl_param) and print result
143  */
144 struct asus_wmi_debug {
145         struct dentry *root;
146         u32 method_id;
147         u32 dev_id;
148         u32 ctrl_param;
149 };
150
151 struct asus_rfkill {
152         struct asus_wmi *asus;
153         struct rfkill *rfkill;
154         u32 dev_id;
155 };
156
157 enum fan_type {
158         FAN_TYPE_NONE = 0,
159         FAN_TYPE_AGFN,          /* deprecated on newer platforms */
160         FAN_TYPE_SPEC83,        /* starting in Spec 8.3, use CPU_FAN_CTRL */
161 };
162
163 struct asus_wmi {
164         int dsts_id;
165         int spec;
166         int sfun;
167         bool wmi_event_queue;
168
169         struct input_dev *inputdev;
170         struct backlight_device *backlight_device;
171         struct platform_device *platform_device;
172
173         struct led_classdev wlan_led;
174         int wlan_led_wk;
175         struct led_classdev tpd_led;
176         int tpd_led_wk;
177         struct led_classdev kbd_led;
178         int kbd_led_wk;
179         struct led_classdev lightbar_led;
180         int lightbar_led_wk;
181         struct workqueue_struct *led_workqueue;
182         struct work_struct tpd_led_work;
183         struct work_struct wlan_led_work;
184         struct work_struct lightbar_led_work;
185
186         struct asus_rfkill wlan;
187         struct asus_rfkill bluetooth;
188         struct asus_rfkill wimax;
189         struct asus_rfkill wwan3g;
190         struct asus_rfkill gps;
191         struct asus_rfkill uwb;
192
193         enum fan_type fan_type;
194         int fan_pwm_mode;
195         int agfn_pwm;
196
197         bool fan_boost_mode_available;
198         u8 fan_boost_mode_mask;
199         u8 fan_boost_mode;
200
201         // The RSOC controls the maximum charging percentage.
202         bool battery_rsoc_available;
203
204         struct hotplug_slot hotplug_slot;
205         struct mutex hotplug_lock;
206         struct mutex wmi_lock;
207         struct workqueue_struct *hotplug_workqueue;
208         struct work_struct hotplug_work;
209
210         bool fnlock_locked;
211
212         struct asus_wmi_debug debug;
213
214         struct asus_wmi_driver *driver;
215 };
216
217 /* Input **********************************************************************/
218
219 static int asus_wmi_input_init(struct asus_wmi *asus)
220 {
221         int err;
222
223         asus->inputdev = input_allocate_device();
224         if (!asus->inputdev)
225                 return -ENOMEM;
226
227         asus->inputdev->name = asus->driver->input_name;
228         asus->inputdev->phys = asus->driver->input_phys;
229         asus->inputdev->id.bustype = BUS_HOST;
230         asus->inputdev->dev.parent = &asus->platform_device->dev;
231         set_bit(EV_REP, asus->inputdev->evbit);
232
233         err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
234         if (err)
235                 goto err_free_dev;
236
237         err = input_register_device(asus->inputdev);
238         if (err)
239                 goto err_free_dev;
240
241         return 0;
242
243 err_free_dev:
244         input_free_device(asus->inputdev);
245         return err;
246 }
247
248 static void asus_wmi_input_exit(struct asus_wmi *asus)
249 {
250         if (asus->inputdev)
251                 input_unregister_device(asus->inputdev);
252
253         asus->inputdev = NULL;
254 }
255
256 /* WMI ************************************************************************/
257
258 static int asus_wmi_evaluate_method3(u32 method_id,
259                 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
260 {
261         struct bios_args args = {
262                 .arg0 = arg0,
263                 .arg1 = arg1,
264                 .arg2 = arg2,
265         };
266         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
267         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
268         acpi_status status;
269         union acpi_object *obj;
270         u32 tmp = 0;
271
272         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
273                                      &input, &output);
274
275         if (ACPI_FAILURE(status))
276                 return -EIO;
277
278         obj = (union acpi_object *)output.pointer;
279         if (obj && obj->type == ACPI_TYPE_INTEGER)
280                 tmp = (u32) obj->integer.value;
281
282         if (retval)
283                 *retval = tmp;
284
285         kfree(obj);
286
287         if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
288                 return -ENODEV;
289
290         return 0;
291 }
292
293 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
294 {
295         return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
296 }
297 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
298
299 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
300 {
301         struct acpi_buffer input;
302         u64 phys_addr;
303         u32 retval;
304         u32 status = -1;
305
306         /*
307          * Copy to dma capable address otherwise memory corruption occurs as
308          * bios has to be able to access it.
309          */
310         input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
311         input.length = args.length;
312         if (!input.pointer)
313                 return -ENOMEM;
314         phys_addr = virt_to_phys(input.pointer);
315
316         status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
317                                         phys_addr, 0, &retval);
318         if (!status)
319                 memcpy(args.pointer, input.pointer, args.length);
320
321         kfree(input.pointer);
322         if (status)
323                 return -ENXIO;
324
325         return retval;
326 }
327
328 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
329 {
330         return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
331 }
332
333 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
334                                  u32 *retval)
335 {
336         return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
337                                         ctrl_param, retval);
338 }
339
340 /* Helper for special devices with magic return codes */
341 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
342                                       u32 dev_id, u32 mask)
343 {
344         u32 retval = 0;
345         int err;
346
347         err = asus_wmi_get_devstate(asus, dev_id, &retval);
348         if (err < 0)
349                 return err;
350
351         if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
352                 return -ENODEV;
353
354         if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
355                 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
356                         return -ENODEV;
357         }
358
359         return retval & mask;
360 }
361
362 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
363 {
364         return asus_wmi_get_devstate_bits(asus, dev_id,
365                                           ASUS_WMI_DSTS_STATUS_BIT);
366 }
367
368 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
369 {
370         u32 retval;
371         int status = asus_wmi_get_devstate(asus, dev_id, &retval);
372
373         return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
374 }
375
376 /* Battery ********************************************************************/
377
378 /* The battery maximum charging percentage */
379 static int charge_end_threshold;
380
381 static ssize_t charge_control_end_threshold_store(struct device *dev,
382                                                   struct device_attribute *attr,
383                                                   const char *buf, size_t count)
384 {
385         int value, ret, rv;
386
387         ret = kstrtouint(buf, 10, &value);
388         if (ret)
389                 return ret;
390
391         if (value < 0 || value > 100)
392                 return -EINVAL;
393
394         ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
395         if (ret)
396                 return ret;
397
398         if (rv != 1)
399                 return -EIO;
400
401         /* There isn't any method in the DSDT to read the threshold, so we
402          * save the threshold.
403          */
404         charge_end_threshold = value;
405         return count;
406 }
407
408 static ssize_t charge_control_end_threshold_show(struct device *device,
409                                                  struct device_attribute *attr,
410                                                  char *buf)
411 {
412         return sprintf(buf, "%d\n", charge_end_threshold);
413 }
414
415 static DEVICE_ATTR_RW(charge_control_end_threshold);
416
417 static int asus_wmi_battery_add(struct power_supply *battery)
418 {
419         /* The WMI method does not provide a way to specific a battery, so we
420          * just assume it is the first battery.
421          */
422         if (strcmp(battery->desc->name, "BAT0") != 0)
423                 return -ENODEV;
424
425         if (device_create_file(&battery->dev,
426             &dev_attr_charge_control_end_threshold))
427                 return -ENODEV;
428
429         /* The charge threshold is only reset when the system is power cycled,
430          * and we can't get the current threshold so let set it to 100% when
431          * a battery is added.
432          */
433         asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
434         charge_end_threshold = 100;
435
436         return 0;
437 }
438
439 static int asus_wmi_battery_remove(struct power_supply *battery)
440 {
441         device_remove_file(&battery->dev,
442                            &dev_attr_charge_control_end_threshold);
443         return 0;
444 }
445
446 static struct acpi_battery_hook battery_hook = {
447         .add_battery = asus_wmi_battery_add,
448         .remove_battery = asus_wmi_battery_remove,
449         .name = "ASUS Battery Extension",
450 };
451
452 static void asus_wmi_battery_init(struct asus_wmi *asus)
453 {
454         asus->battery_rsoc_available = false;
455         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
456                 asus->battery_rsoc_available = true;
457                 battery_hook_register(&battery_hook);
458         }
459 }
460
461 static void asus_wmi_battery_exit(struct asus_wmi *asus)
462 {
463         if (asus->battery_rsoc_available)
464                 battery_hook_unregister(&battery_hook);
465 }
466
467 /* LEDs ***********************************************************************/
468
469 /*
470  * These functions actually update the LED's, and are called from a
471  * workqueue. By doing this as separate work rather than when the LED
472  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
473  * potentially bad time, such as a timer interrupt.
474  */
475 static void tpd_led_update(struct work_struct *work)
476 {
477         int ctrl_param;
478         struct asus_wmi *asus;
479
480         asus = container_of(work, struct asus_wmi, tpd_led_work);
481
482         ctrl_param = asus->tpd_led_wk;
483         asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
484 }
485
486 static void tpd_led_set(struct led_classdev *led_cdev,
487                         enum led_brightness value)
488 {
489         struct asus_wmi *asus;
490
491         asus = container_of(led_cdev, struct asus_wmi, tpd_led);
492
493         asus->tpd_led_wk = !!value;
494         queue_work(asus->led_workqueue, &asus->tpd_led_work);
495 }
496
497 static int read_tpd_led_state(struct asus_wmi *asus)
498 {
499         return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
500 }
501
502 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
503 {
504         struct asus_wmi *asus;
505
506         asus = container_of(led_cdev, struct asus_wmi, tpd_led);
507
508         return read_tpd_led_state(asus);
509 }
510
511 static void kbd_led_update(struct asus_wmi *asus)
512 {
513         int ctrl_param = 0;
514
515         ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
516         asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
517 }
518
519 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
520 {
521         int retval;
522
523         /*
524          * bits 0-2: level
525          * bit 7: light on/off
526          * bit 8-10: environment (0: dark, 1: normal, 2: light)
527          * bit 17: status unknown
528          */
529         retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
530                                             0xFFFF);
531
532         /* Unknown status is considered as off */
533         if (retval == 0x8000)
534                 retval = 0;
535
536         if (retval < 0)
537                 return retval;
538
539         if (level)
540                 *level = retval & 0x7F;
541         if (env)
542                 *env = (retval >> 8) & 0x7F;
543         return 0;
544 }
545
546 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
547 {
548         struct asus_wmi *asus;
549         int max_level;
550
551         asus = container_of(led_cdev, struct asus_wmi, kbd_led);
552         max_level = asus->kbd_led.max_brightness;
553
554         asus->kbd_led_wk = clamp_val(value, 0, max_level);
555         kbd_led_update(asus);
556 }
557
558 static void kbd_led_set(struct led_classdev *led_cdev,
559                         enum led_brightness value)
560 {
561         /* Prevent disabling keyboard backlight on module unregister */
562         if (led_cdev->flags & LED_UNREGISTERING)
563                 return;
564
565         do_kbd_led_set(led_cdev, value);
566 }
567
568 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
569 {
570         struct led_classdev *led_cdev = &asus->kbd_led;
571
572         do_kbd_led_set(led_cdev, value);
573         led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
574 }
575
576 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
577 {
578         struct asus_wmi *asus;
579         int retval, value;
580
581         asus = container_of(led_cdev, struct asus_wmi, kbd_led);
582
583         retval = kbd_led_read(asus, &value, NULL);
584         if (retval < 0)
585                 return retval;
586
587         return value;
588 }
589
590 static int wlan_led_unknown_state(struct asus_wmi *asus)
591 {
592         u32 result;
593
594         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
595
596         return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
597 }
598
599 static void wlan_led_update(struct work_struct *work)
600 {
601         int ctrl_param;
602         struct asus_wmi *asus;
603
604         asus = container_of(work, struct asus_wmi, wlan_led_work);
605
606         ctrl_param = asus->wlan_led_wk;
607         asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
608 }
609
610 static void wlan_led_set(struct led_classdev *led_cdev,
611                          enum led_brightness value)
612 {
613         struct asus_wmi *asus;
614
615         asus = container_of(led_cdev, struct asus_wmi, wlan_led);
616
617         asus->wlan_led_wk = !!value;
618         queue_work(asus->led_workqueue, &asus->wlan_led_work);
619 }
620
621 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
622 {
623         struct asus_wmi *asus;
624         u32 result;
625
626         asus = container_of(led_cdev, struct asus_wmi, wlan_led);
627         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
628
629         return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
630 }
631
632 static void lightbar_led_update(struct work_struct *work)
633 {
634         struct asus_wmi *asus;
635         int ctrl_param;
636
637         asus = container_of(work, struct asus_wmi, lightbar_led_work);
638
639         ctrl_param = asus->lightbar_led_wk;
640         asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
641 }
642
643 static void lightbar_led_set(struct led_classdev *led_cdev,
644                              enum led_brightness value)
645 {
646         struct asus_wmi *asus;
647
648         asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
649
650         asus->lightbar_led_wk = !!value;
651         queue_work(asus->led_workqueue, &asus->lightbar_led_work);
652 }
653
654 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
655 {
656         struct asus_wmi *asus;
657         u32 result;
658
659         asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
660         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
661
662         return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
663 }
664
665 static void asus_wmi_led_exit(struct asus_wmi *asus)
666 {
667         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
668                 led_classdev_unregister(&asus->kbd_led);
669         if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
670                 led_classdev_unregister(&asus->tpd_led);
671         if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
672                 led_classdev_unregister(&asus->wlan_led);
673         if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
674                 led_classdev_unregister(&asus->lightbar_led);
675         if (asus->led_workqueue)
676                 destroy_workqueue(asus->led_workqueue);
677 }
678
679 static int asus_wmi_led_init(struct asus_wmi *asus)
680 {
681         int rv = 0, led_val;
682
683         asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
684         if (!asus->led_workqueue)
685                 return -ENOMEM;
686
687         if (read_tpd_led_state(asus) >= 0) {
688                 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
689
690                 asus->tpd_led.name = "asus::touchpad";
691                 asus->tpd_led.brightness_set = tpd_led_set;
692                 asus->tpd_led.brightness_get = tpd_led_get;
693                 asus->tpd_led.max_brightness = 1;
694
695                 rv = led_classdev_register(&asus->platform_device->dev,
696                                            &asus->tpd_led);
697                 if (rv)
698                         goto error;
699         }
700
701         if (!kbd_led_read(asus, &led_val, NULL)) {
702                 asus->kbd_led_wk = led_val;
703                 asus->kbd_led.name = "asus::kbd_backlight";
704                 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
705                 asus->kbd_led.brightness_set = kbd_led_set;
706                 asus->kbd_led.brightness_get = kbd_led_get;
707                 asus->kbd_led.max_brightness = 3;
708
709                 rv = led_classdev_register(&asus->platform_device->dev,
710                                            &asus->kbd_led);
711                 if (rv)
712                         goto error;
713         }
714
715         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
716                         && (asus->driver->quirks->wapf > 0)) {
717                 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
718
719                 asus->wlan_led.name = "asus::wlan";
720                 asus->wlan_led.brightness_set = wlan_led_set;
721                 if (!wlan_led_unknown_state(asus))
722                         asus->wlan_led.brightness_get = wlan_led_get;
723                 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
724                 asus->wlan_led.max_brightness = 1;
725                 asus->wlan_led.default_trigger = "asus-wlan";
726
727                 rv = led_classdev_register(&asus->platform_device->dev,
728                                            &asus->wlan_led);
729                 if (rv)
730                         goto error;
731         }
732
733         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
734                 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
735
736                 asus->lightbar_led.name = "asus::lightbar";
737                 asus->lightbar_led.brightness_set = lightbar_led_set;
738                 asus->lightbar_led.brightness_get = lightbar_led_get;
739                 asus->lightbar_led.max_brightness = 1;
740
741                 rv = led_classdev_register(&asus->platform_device->dev,
742                                            &asus->lightbar_led);
743         }
744
745 error:
746         if (rv)
747                 asus_wmi_led_exit(asus);
748
749         return rv;
750 }
751
752 /* RF *************************************************************************/
753
754 /*
755  * PCI hotplug (for wlan rfkill)
756  */
757 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
758 {
759         int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
760
761         if (result < 0)
762                 return false;
763         return !result;
764 }
765
766 static void asus_rfkill_hotplug(struct asus_wmi *asus)
767 {
768         struct pci_dev *dev;
769         struct pci_bus *bus;
770         bool blocked;
771         bool absent;
772         u32 l;
773
774         mutex_lock(&asus->wmi_lock);
775         blocked = asus_wlan_rfkill_blocked(asus);
776         mutex_unlock(&asus->wmi_lock);
777
778         mutex_lock(&asus->hotplug_lock);
779         pci_lock_rescan_remove();
780
781         if (asus->wlan.rfkill)
782                 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
783
784         if (asus->hotplug_slot.ops) {
785                 bus = pci_find_bus(0, 1);
786                 if (!bus) {
787                         pr_warn("Unable to find PCI bus 1?\n");
788                         goto out_unlock;
789                 }
790
791                 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
792                         pr_err("Unable to read PCI config space?\n");
793                         goto out_unlock;
794                 }
795                 absent = (l == 0xffffffff);
796
797                 if (blocked != absent) {
798                         pr_warn("BIOS says wireless lan is %s, "
799                                 "but the pci device is %s\n",
800                                 blocked ? "blocked" : "unblocked",
801                                 absent ? "absent" : "present");
802                         pr_warn("skipped wireless hotplug as probably "
803                                 "inappropriate for this model\n");
804                         goto out_unlock;
805                 }
806
807                 if (!blocked) {
808                         dev = pci_get_slot(bus, 0);
809                         if (dev) {
810                                 /* Device already present */
811                                 pci_dev_put(dev);
812                                 goto out_unlock;
813                         }
814                         dev = pci_scan_single_device(bus, 0);
815                         if (dev) {
816                                 pci_bus_assign_resources(bus);
817                                 pci_bus_add_device(dev);
818                         }
819                 } else {
820                         dev = pci_get_slot(bus, 0);
821                         if (dev) {
822                                 pci_stop_and_remove_bus_device(dev);
823                                 pci_dev_put(dev);
824                         }
825                 }
826         }
827
828 out_unlock:
829         pci_unlock_rescan_remove();
830         mutex_unlock(&asus->hotplug_lock);
831 }
832
833 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
834 {
835         struct asus_wmi *asus = data;
836
837         if (event != ACPI_NOTIFY_BUS_CHECK)
838                 return;
839
840         /*
841          * We can't call directly asus_rfkill_hotplug because most
842          * of the time WMBC is still being executed and not reetrant.
843          * There is currently no way to tell ACPICA that  we want this
844          * method to be serialized, we schedule a asus_rfkill_hotplug
845          * call later, in a safer context.
846          */
847         queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
848 }
849
850 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
851 {
852         acpi_status status;
853         acpi_handle handle;
854
855         status = acpi_get_handle(NULL, node, &handle);
856         if (ACPI_FAILURE(status))
857                 return -ENODEV;
858
859         status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
860                                              asus_rfkill_notify, asus);
861         if (ACPI_FAILURE(status))
862                 pr_warn("Failed to register notify on %s\n", node);
863
864         return 0;
865 }
866
867 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
868 {
869         acpi_status status = AE_OK;
870         acpi_handle handle;
871
872         status = acpi_get_handle(NULL, node, &handle);
873         if (ACPI_FAILURE(status))
874                 return;
875
876         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
877                                             asus_rfkill_notify);
878         if (ACPI_FAILURE(status))
879                 pr_err("Error removing rfkill notify handler %s\n", node);
880 }
881
882 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
883                                    u8 *value)
884 {
885         struct asus_wmi *asus = container_of(hotplug_slot,
886                                              struct asus_wmi, hotplug_slot);
887         int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
888
889         if (result < 0)
890                 return result;
891
892         *value = !!result;
893         return 0;
894 }
895
896 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
897         .get_adapter_status = asus_get_adapter_status,
898         .get_power_status = asus_get_adapter_status,
899 };
900
901 static void asus_hotplug_work(struct work_struct *work)
902 {
903         struct asus_wmi *asus;
904
905         asus = container_of(work, struct asus_wmi, hotplug_work);
906         asus_rfkill_hotplug(asus);
907 }
908
909 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
910 {
911         int ret = -ENOMEM;
912         struct pci_bus *bus = pci_find_bus(0, 1);
913
914         if (!bus) {
915                 pr_err("Unable to find wifi PCI bus\n");
916                 return -ENODEV;
917         }
918
919         asus->hotplug_workqueue =
920             create_singlethread_workqueue("hotplug_workqueue");
921         if (!asus->hotplug_workqueue)
922                 goto error_workqueue;
923
924         INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
925
926         asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
927
928         ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
929         if (ret) {
930                 pr_err("Unable to register hotplug slot - %d\n", ret);
931                 goto error_register;
932         }
933
934         return 0;
935
936 error_register:
937         asus->hotplug_slot.ops = NULL;
938         destroy_workqueue(asus->hotplug_workqueue);
939 error_workqueue:
940         return ret;
941 }
942
943 /*
944  * Rfkill devices
945  */
946 static int asus_rfkill_set(void *data, bool blocked)
947 {
948         struct asus_rfkill *priv = data;
949         u32 ctrl_param = !blocked;
950         u32 dev_id = priv->dev_id;
951
952         /*
953          * If the user bit is set, BIOS can't set and record the wlan status,
954          * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
955          * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
956          * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
957          * while setting the wlan status through WMI.
958          * This is also the behavior that windows app will do.
959          */
960         if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
961              priv->asus->driver->wlan_ctrl_by_user)
962                 dev_id = ASUS_WMI_DEVID_WLAN_LED;
963
964         return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
965 }
966
967 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
968 {
969         struct asus_rfkill *priv = data;
970         int result;
971
972         result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
973
974         if (result < 0)
975                 return;
976
977         rfkill_set_sw_state(priv->rfkill, !result);
978 }
979
980 static int asus_rfkill_wlan_set(void *data, bool blocked)
981 {
982         struct asus_rfkill *priv = data;
983         struct asus_wmi *asus = priv->asus;
984         int ret;
985
986         /*
987          * This handler is enabled only if hotplug is enabled.
988          * In this case, the asus_wmi_set_devstate() will
989          * trigger a wmi notification and we need to wait
990          * this call to finish before being able to call
991          * any wmi method
992          */
993         mutex_lock(&asus->wmi_lock);
994         ret = asus_rfkill_set(data, blocked);
995         mutex_unlock(&asus->wmi_lock);
996         return ret;
997 }
998
999 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1000         .set_block = asus_rfkill_wlan_set,
1001         .query = asus_rfkill_query,
1002 };
1003
1004 static const struct rfkill_ops asus_rfkill_ops = {
1005         .set_block = asus_rfkill_set,
1006         .query = asus_rfkill_query,
1007 };
1008
1009 static int asus_new_rfkill(struct asus_wmi *asus,
1010                            struct asus_rfkill *arfkill,
1011                            const char *name, enum rfkill_type type, int dev_id)
1012 {
1013         int result = asus_wmi_get_devstate_simple(asus, dev_id);
1014         struct rfkill **rfkill = &arfkill->rfkill;
1015
1016         if (result < 0)
1017                 return result;
1018
1019         arfkill->dev_id = dev_id;
1020         arfkill->asus = asus;
1021
1022         if (dev_id == ASUS_WMI_DEVID_WLAN &&
1023             asus->driver->quirks->hotplug_wireless)
1024                 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1025                                        &asus_rfkill_wlan_ops, arfkill);
1026         else
1027                 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1028                                        &asus_rfkill_ops, arfkill);
1029
1030         if (!*rfkill)
1031                 return -EINVAL;
1032
1033         if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1034                         (asus->driver->quirks->wapf > 0))
1035                 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1036
1037         rfkill_init_sw_state(*rfkill, !result);
1038         result = rfkill_register(*rfkill);
1039         if (result) {
1040                 rfkill_destroy(*rfkill);
1041                 *rfkill = NULL;
1042                 return result;
1043         }
1044         return 0;
1045 }
1046
1047 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1048 {
1049         if (asus->driver->wlan_ctrl_by_user && ashs_present())
1050                 return;
1051
1052         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1053         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1054         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1055         if (asus->wlan.rfkill) {
1056                 rfkill_unregister(asus->wlan.rfkill);
1057                 rfkill_destroy(asus->wlan.rfkill);
1058                 asus->wlan.rfkill = NULL;
1059         }
1060         /*
1061          * Refresh pci hotplug in case the rfkill state was changed after
1062          * asus_unregister_rfkill_notifier()
1063          */
1064         asus_rfkill_hotplug(asus);
1065         if (asus->hotplug_slot.ops)
1066                 pci_hp_deregister(&asus->hotplug_slot);
1067         if (asus->hotplug_workqueue)
1068                 destroy_workqueue(asus->hotplug_workqueue);
1069
1070         if (asus->bluetooth.rfkill) {
1071                 rfkill_unregister(asus->bluetooth.rfkill);
1072                 rfkill_destroy(asus->bluetooth.rfkill);
1073                 asus->bluetooth.rfkill = NULL;
1074         }
1075         if (asus->wimax.rfkill) {
1076                 rfkill_unregister(asus->wimax.rfkill);
1077                 rfkill_destroy(asus->wimax.rfkill);
1078                 asus->wimax.rfkill = NULL;
1079         }
1080         if (asus->wwan3g.rfkill) {
1081                 rfkill_unregister(asus->wwan3g.rfkill);
1082                 rfkill_destroy(asus->wwan3g.rfkill);
1083                 asus->wwan3g.rfkill = NULL;
1084         }
1085         if (asus->gps.rfkill) {
1086                 rfkill_unregister(asus->gps.rfkill);
1087                 rfkill_destroy(asus->gps.rfkill);
1088                 asus->gps.rfkill = NULL;
1089         }
1090         if (asus->uwb.rfkill) {
1091                 rfkill_unregister(asus->uwb.rfkill);
1092                 rfkill_destroy(asus->uwb.rfkill);
1093                 asus->uwb.rfkill = NULL;
1094         }
1095 }
1096
1097 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1098 {
1099         int result = 0;
1100
1101         mutex_init(&asus->hotplug_lock);
1102         mutex_init(&asus->wmi_lock);
1103
1104         result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1105                                  RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1106
1107         if (result && result != -ENODEV)
1108                 goto exit;
1109
1110         result = asus_new_rfkill(asus, &asus->bluetooth,
1111                                  "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1112                                  ASUS_WMI_DEVID_BLUETOOTH);
1113
1114         if (result && result != -ENODEV)
1115                 goto exit;
1116
1117         result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1118                                  RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1119
1120         if (result && result != -ENODEV)
1121                 goto exit;
1122
1123         result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1124                                  RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1125
1126         if (result && result != -ENODEV)
1127                 goto exit;
1128
1129         result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1130                                  RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1131
1132         if (result && result != -ENODEV)
1133                 goto exit;
1134
1135         result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1136                                  RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1137
1138         if (result && result != -ENODEV)
1139                 goto exit;
1140
1141         if (!asus->driver->quirks->hotplug_wireless)
1142                 goto exit;
1143
1144         result = asus_setup_pci_hotplug(asus);
1145         /*
1146          * If we get -EBUSY then something else is handling the PCI hotplug -
1147          * don't fail in this case
1148          */
1149         if (result == -EBUSY)
1150                 result = 0;
1151
1152         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1153         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1154         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1155         /*
1156          * Refresh pci hotplug in case the rfkill state was changed during
1157          * setup.
1158          */
1159         asus_rfkill_hotplug(asus);
1160
1161 exit:
1162         if (result && result != -ENODEV)
1163                 asus_wmi_rfkill_exit(asus);
1164
1165         if (result == -ENODEV)
1166                 result = 0;
1167
1168         return result;
1169 }
1170
1171 /* Quirks *********************************************************************/
1172
1173 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1174 {
1175         struct pci_dev *xhci_pdev;
1176         u32 orig_ports_available;
1177         u32 ports_available = asus->driver->quirks->xusb2pr;
1178
1179         xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1180                         PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1181                         NULL);
1182
1183         if (!xhci_pdev)
1184                 return;
1185
1186         pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1187                                 &orig_ports_available);
1188
1189         pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1190                                 cpu_to_le32(ports_available));
1191
1192         pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1193                         orig_ports_available, ports_available);
1194 }
1195
1196 /*
1197  * Some devices dont support or have borcken get_als method
1198  * but still support set method.
1199  */
1200 static void asus_wmi_set_als(void)
1201 {
1202         asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1203 }
1204
1205 /* Hwmon device ***************************************************************/
1206
1207 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1208                                           int *speed)
1209 {
1210         struct agfn_fan_args args = {
1211                 .agfn.len = sizeof(args),
1212                 .agfn.mfun = ASUS_FAN_MFUN,
1213                 .agfn.sfun = ASUS_FAN_SFUN_READ,
1214                 .fan = fan,
1215                 .speed = 0,
1216         };
1217         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1218         int status;
1219
1220         if (fan != 1)
1221                 return -EINVAL;
1222
1223         status = asus_wmi_evaluate_method_agfn(input);
1224
1225         if (status || args.agfn.err)
1226                 return -ENXIO;
1227
1228         if (speed)
1229                 *speed = args.speed;
1230
1231         return 0;
1232 }
1233
1234 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1235                                      int *speed)
1236 {
1237         struct agfn_fan_args args = {
1238                 .agfn.len = sizeof(args),
1239                 .agfn.mfun = ASUS_FAN_MFUN,
1240                 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1241                 .fan = fan,
1242                 .speed = speed ?  *speed : 0,
1243         };
1244         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1245         int status;
1246
1247         /* 1: for setting 1st fan's speed 0: setting auto mode */
1248         if (fan != 1 && fan != 0)
1249                 return -EINVAL;
1250
1251         status = asus_wmi_evaluate_method_agfn(input);
1252
1253         if (status || args.agfn.err)
1254                 return -ENXIO;
1255
1256         if (speed && fan == 1)
1257                 asus->agfn_pwm = *speed;
1258
1259         return 0;
1260 }
1261
1262 /*
1263  * Check if we can read the speed of one fan. If true we assume we can also
1264  * control it.
1265  */
1266 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1267 {
1268         int status;
1269         int speed;
1270         u32 value;
1271
1272         status = asus_agfn_fan_speed_read(asus, 1, &speed);
1273         if (status != 0)
1274                 return false;
1275
1276         status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1277         if (status != 0)
1278                 return false;
1279
1280         /*
1281          * We need to find a better way, probably using sfun,
1282          * bits or spec ...
1283          * Currently we disable it if:
1284          * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1285          * - reverved bits are non-zero
1286          * - sfun and presence bit are not set
1287          */
1288         return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1289                  || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1290 }
1291
1292 static int asus_fan_set_auto(struct asus_wmi *asus)
1293 {
1294         int status;
1295         u32 retval;
1296
1297         switch (asus->fan_type) {
1298         case FAN_TYPE_SPEC83:
1299                 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1300                                                0, &retval);
1301                 if (status)
1302                         return status;
1303
1304                 if (retval != 1)
1305                         return -EIO;
1306                 break;
1307
1308         case FAN_TYPE_AGFN:
1309                 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1310                 if (status)
1311                         return -ENXIO;
1312                 break;
1313
1314         default:
1315                 return -ENXIO;
1316         }
1317
1318
1319         return 0;
1320 }
1321
1322 static ssize_t pwm1_show(struct device *dev,
1323                                struct device_attribute *attr,
1324                                char *buf)
1325 {
1326         struct asus_wmi *asus = dev_get_drvdata(dev);
1327         int err;
1328         int value;
1329
1330         /* If we already set a value then just return it */
1331         if (asus->agfn_pwm >= 0)
1332                 return sprintf(buf, "%d\n", asus->agfn_pwm);
1333
1334         /*
1335          * If we haven't set already set a value through the AGFN interface,
1336          * we read a current value through the (now-deprecated) FAN_CTRL device.
1337          */
1338         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1339         if (err < 0)
1340                 return err;
1341
1342         value &= 0xFF;
1343
1344         if (value == 1) /* Low Speed */
1345                 value = 85;
1346         else if (value == 2)
1347                 value = 170;
1348         else if (value == 3)
1349                 value = 255;
1350         else if (value) {
1351                 pr_err("Unknown fan speed %#x\n", value);
1352                 value = -1;
1353         }
1354
1355         return sprintf(buf, "%d\n", value);
1356 }
1357
1358 static ssize_t pwm1_store(struct device *dev,
1359                                      struct device_attribute *attr,
1360                                      const char *buf, size_t count) {
1361         struct asus_wmi *asus = dev_get_drvdata(dev);
1362         int value;
1363         int state;
1364         int ret;
1365
1366         ret = kstrtouint(buf, 10, &value);
1367         if (ret)
1368                 return ret;
1369
1370         value = clamp(value, 0, 255);
1371
1372         state = asus_agfn_fan_speed_write(asus, 1, &value);
1373         if (state)
1374                 pr_warn("Setting fan speed failed: %d\n", state);
1375         else
1376                 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1377
1378         return count;
1379 }
1380
1381 static ssize_t fan1_input_show(struct device *dev,
1382                                         struct device_attribute *attr,
1383                                         char *buf)
1384 {
1385         struct asus_wmi *asus = dev_get_drvdata(dev);
1386         int value;
1387         int ret;
1388
1389         switch (asus->fan_type) {
1390         case FAN_TYPE_SPEC83:
1391                 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1392                                             &value);
1393                 if (ret < 0)
1394                         return ret;
1395
1396                 value &= 0xffff;
1397                 break;
1398
1399         case FAN_TYPE_AGFN:
1400                 /* no speed readable on manual mode */
1401                 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1402                         return -ENXIO;
1403
1404                 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1405                 if (ret) {
1406                         pr_warn("reading fan speed failed: %d\n", ret);
1407                         return -ENXIO;
1408                 }
1409                 break;
1410
1411         default:
1412                 return -ENXIO;
1413         }
1414
1415         return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1416 }
1417
1418 static ssize_t pwm1_enable_show(struct device *dev,
1419                                                  struct device_attribute *attr,
1420                                                  char *buf)
1421 {
1422         struct asus_wmi *asus = dev_get_drvdata(dev);
1423
1424         /*
1425          * Just read back the cached pwm mode.
1426          *
1427          * For the CPU_FAN device, the spec indicates that we should be
1428          * able to read the device status and consult bit 19 to see if we
1429          * are in Full On or Automatic mode. However, this does not work
1430          * in practice on X532FL at least (the bit is always 0) and there's
1431          * also nothing in the DSDT to indicate that this behaviour exists.
1432          */
1433         return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1434 }
1435
1436 static ssize_t pwm1_enable_store(struct device *dev,
1437                                                   struct device_attribute *attr,
1438                                                   const char *buf, size_t count)
1439 {
1440         struct asus_wmi *asus = dev_get_drvdata(dev);
1441         int status = 0;
1442         int state;
1443         int value;
1444         int ret;
1445         u32 retval;
1446
1447         ret = kstrtouint(buf, 10, &state);
1448         if (ret)
1449                 return ret;
1450
1451         if (asus->fan_type == FAN_TYPE_SPEC83) {
1452                 switch (state) { /* standard documented hwmon values */
1453                 case ASUS_FAN_CTRL_FULLSPEED:
1454                         value = 1;
1455                         break;
1456                 case ASUS_FAN_CTRL_AUTO:
1457                         value = 0;
1458                         break;
1459                 default:
1460                         return -EINVAL;
1461                 }
1462
1463                 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1464                                             value, &retval);
1465                 if (ret)
1466                         return ret;
1467
1468                 if (retval != 1)
1469                         return -EIO;
1470         } else if (asus->fan_type == FAN_TYPE_AGFN) {
1471                 switch (state) {
1472                 case ASUS_FAN_CTRL_MANUAL:
1473                         break;
1474
1475                 case ASUS_FAN_CTRL_AUTO:
1476                         status = asus_fan_set_auto(asus);
1477                         if (status)
1478                                 return status;
1479                         break;
1480
1481                 default:
1482                         return -EINVAL;
1483                 }
1484         }
1485
1486         asus->fan_pwm_mode = state;
1487         return count;
1488 }
1489
1490 static ssize_t fan1_label_show(struct device *dev,
1491                                           struct device_attribute *attr,
1492                                           char *buf)
1493 {
1494         return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1495 }
1496
1497 static ssize_t asus_hwmon_temp1(struct device *dev,
1498                                 struct device_attribute *attr,
1499                                 char *buf)
1500 {
1501         struct asus_wmi *asus = dev_get_drvdata(dev);
1502         u32 value;
1503         int err;
1504
1505         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1506         if (err < 0)
1507                 return err;
1508
1509         value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1510
1511         return sprintf(buf, "%d\n", value);
1512 }
1513
1514 /* Fan1 */
1515 static DEVICE_ATTR_RW(pwm1);
1516 static DEVICE_ATTR_RW(pwm1_enable);
1517 static DEVICE_ATTR_RO(fan1_input);
1518 static DEVICE_ATTR_RO(fan1_label);
1519
1520 /* Temperature */
1521 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1522
1523 static struct attribute *hwmon_attributes[] = {
1524         &dev_attr_pwm1.attr,
1525         &dev_attr_pwm1_enable.attr,
1526         &dev_attr_fan1_input.attr,
1527         &dev_attr_fan1_label.attr,
1528
1529         &dev_attr_temp1_input.attr,
1530         NULL
1531 };
1532
1533 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1534                                           struct attribute *attr, int idx)
1535 {
1536         struct device *dev = container_of(kobj, struct device, kobj);
1537         struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1538         u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1539
1540         if (attr == &dev_attr_pwm1.attr) {
1541                 if (asus->fan_type != FAN_TYPE_AGFN)
1542                         return 0;
1543         } else if (attr == &dev_attr_fan1_input.attr
1544             || attr == &dev_attr_fan1_label.attr
1545             || attr == &dev_attr_pwm1_enable.attr) {
1546                 if (asus->fan_type == FAN_TYPE_NONE)
1547                         return 0;
1548         } else if (attr == &dev_attr_temp1_input.attr) {
1549                 int err = asus_wmi_get_devstate(asus,
1550                                                 ASUS_WMI_DEVID_THERMAL_CTRL,
1551                                                 &value);
1552
1553                 if (err < 0)
1554                         return 0; /* can't return negative here */
1555
1556                 /*
1557                  * If the temperature value in deci-Kelvin is near the absolute
1558                  * zero temperature, something is clearly wrong
1559                  */
1560                 if (value == 0 || value == 1)
1561                         return 0;
1562         }
1563
1564         return attr->mode;
1565 }
1566
1567 static const struct attribute_group hwmon_attribute_group = {
1568         .is_visible = asus_hwmon_sysfs_is_visible,
1569         .attrs = hwmon_attributes
1570 };
1571 __ATTRIBUTE_GROUPS(hwmon_attribute);
1572
1573 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1574 {
1575         struct device *dev = &asus->platform_device->dev;
1576         struct device *hwmon;
1577
1578         hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1579                         hwmon_attribute_groups);
1580
1581         if (IS_ERR(hwmon)) {
1582                 pr_err("Could not register asus hwmon device\n");
1583                 return PTR_ERR(hwmon);
1584         }
1585         return 0;
1586 }
1587
1588 static int asus_wmi_fan_init(struct asus_wmi *asus)
1589 {
1590         asus->fan_type = FAN_TYPE_NONE;
1591         asus->agfn_pwm = -1;
1592
1593         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1594                 asus->fan_type = FAN_TYPE_SPEC83;
1595         else if (asus_wmi_has_agfn_fan(asus))
1596                 asus->fan_type = FAN_TYPE_AGFN;
1597
1598         if (asus->fan_type == FAN_TYPE_NONE)
1599                 return -ENODEV;
1600
1601         asus_fan_set_auto(asus);
1602         asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1603         return 0;
1604 }
1605
1606 /* Fan mode *******************************************************************/
1607
1608 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1609 {
1610         u32 result;
1611         int err;
1612
1613         asus->fan_boost_mode_available = false;
1614
1615         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1616                                     &result);
1617         if (err) {
1618                 if (err == -ENODEV)
1619                         return 0;
1620                 else
1621                         return err;
1622         }
1623
1624         if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1625                         (result & ASUS_FAN_BOOST_MODES_MASK)) {
1626                 asus->fan_boost_mode_available = true;
1627                 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1628         }
1629
1630         return 0;
1631 }
1632
1633 static int fan_boost_mode_write(struct asus_wmi *asus)
1634 {
1635         int err;
1636         u8 value;
1637         u32 retval;
1638
1639         value = asus->fan_boost_mode;
1640
1641         pr_info("Set fan boost mode: %u\n", value);
1642         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1643                                     &retval);
1644         if (err) {
1645                 pr_warn("Failed to set fan boost mode: %d\n", err);
1646                 return err;
1647         }
1648
1649         if (retval != 1) {
1650                 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1651                         retval);
1652                 return -EIO;
1653         }
1654
1655         return 0;
1656 }
1657
1658 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1659 {
1660         u8 mask = asus->fan_boost_mode_mask;
1661
1662         if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1663                 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1664                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1665                 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1666                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1667         } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1668                 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1669                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1670                 else
1671                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1672         } else {
1673                 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1674         }
1675
1676         return fan_boost_mode_write(asus);
1677 }
1678
1679 static ssize_t fan_boost_mode_show(struct device *dev,
1680                                    struct device_attribute *attr, char *buf)
1681 {
1682         struct asus_wmi *asus = dev_get_drvdata(dev);
1683
1684         return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
1685 }
1686
1687 static ssize_t fan_boost_mode_store(struct device *dev,
1688                                     struct device_attribute *attr,
1689                                     const char *buf, size_t count)
1690 {
1691         int result;
1692         u8 new_mode;
1693         struct asus_wmi *asus = dev_get_drvdata(dev);
1694         u8 mask = asus->fan_boost_mode_mask;
1695
1696         result = kstrtou8(buf, 10, &new_mode);
1697         if (result < 0) {
1698                 pr_warn("Trying to store invalid value\n");
1699                 return result;
1700         }
1701
1702         if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1703                 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
1704                         return -EINVAL;
1705         } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1706                 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
1707                         return -EINVAL;
1708         } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
1709                 return -EINVAL;
1710         }
1711
1712         asus->fan_boost_mode = new_mode;
1713         fan_boost_mode_write(asus);
1714
1715         return result;
1716 }
1717
1718 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1719 static DEVICE_ATTR_RW(fan_boost_mode);
1720
1721 /* Backlight ******************************************************************/
1722
1723 static int read_backlight_power(struct asus_wmi *asus)
1724 {
1725         int ret;
1726
1727         if (asus->driver->quirks->store_backlight_power)
1728                 ret = !asus->driver->panel_power;
1729         else
1730                 ret = asus_wmi_get_devstate_simple(asus,
1731                                                    ASUS_WMI_DEVID_BACKLIGHT);
1732
1733         if (ret < 0)
1734                 return ret;
1735
1736         return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1737 }
1738
1739 static int read_brightness_max(struct asus_wmi *asus)
1740 {
1741         u32 retval;
1742         int err;
1743
1744         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1745         if (err < 0)
1746                 return err;
1747
1748         retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1749         retval >>= 8;
1750
1751         if (!retval)
1752                 return -ENODEV;
1753
1754         return retval;
1755 }
1756
1757 static int read_brightness(struct backlight_device *bd)
1758 {
1759         struct asus_wmi *asus = bl_get_data(bd);
1760         u32 retval;
1761         int err;
1762
1763         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1764         if (err < 0)
1765                 return err;
1766
1767         return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1768 }
1769
1770 static u32 get_scalar_command(struct backlight_device *bd)
1771 {
1772         struct asus_wmi *asus = bl_get_data(bd);
1773         u32 ctrl_param = 0;
1774
1775         if ((asus->driver->brightness < bd->props.brightness) ||
1776             bd->props.brightness == bd->props.max_brightness)
1777                 ctrl_param = 0x00008001;
1778         else if ((asus->driver->brightness > bd->props.brightness) ||
1779                  bd->props.brightness == 0)
1780                 ctrl_param = 0x00008000;
1781
1782         asus->driver->brightness = bd->props.brightness;
1783
1784         return ctrl_param;
1785 }
1786
1787 static int update_bl_status(struct backlight_device *bd)
1788 {
1789         struct asus_wmi *asus = bl_get_data(bd);
1790         u32 ctrl_param;
1791         int power, err = 0;
1792
1793         power = read_backlight_power(asus);
1794         if (power != -ENODEV && bd->props.power != power) {
1795                 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1796                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1797                                             ctrl_param, NULL);
1798                 if (asus->driver->quirks->store_backlight_power)
1799                         asus->driver->panel_power = bd->props.power;
1800
1801                 /* When using scalar brightness, updating the brightness
1802                  * will mess with the backlight power */
1803                 if (asus->driver->quirks->scalar_panel_brightness)
1804                         return err;
1805         }
1806
1807         if (asus->driver->quirks->scalar_panel_brightness)
1808                 ctrl_param = get_scalar_command(bd);
1809         else
1810                 ctrl_param = bd->props.brightness;
1811
1812         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1813                                     ctrl_param, NULL);
1814
1815         return err;
1816 }
1817
1818 static const struct backlight_ops asus_wmi_bl_ops = {
1819         .get_brightness = read_brightness,
1820         .update_status = update_bl_status,
1821 };
1822
1823 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1824 {
1825         struct backlight_device *bd = asus->backlight_device;
1826         int old = bd->props.brightness;
1827         int new = old;
1828
1829         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1830                 new = code - NOTIFY_BRNUP_MIN + 1;
1831         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1832                 new = code - NOTIFY_BRNDOWN_MIN;
1833
1834         bd->props.brightness = new;
1835         backlight_update_status(bd);
1836         backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1837
1838         return old;
1839 }
1840
1841 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1842 {
1843         struct backlight_device *bd;
1844         struct backlight_properties props;
1845         int max;
1846         int power;
1847
1848         max = read_brightness_max(asus);
1849         if (max < 0)
1850                 return max;
1851
1852         power = read_backlight_power(asus);
1853         if (power == -ENODEV)
1854                 power = FB_BLANK_UNBLANK;
1855         else if (power < 0)
1856                 return power;
1857
1858         memset(&props, 0, sizeof(struct backlight_properties));
1859         props.type = BACKLIGHT_PLATFORM;
1860         props.max_brightness = max;
1861         bd = backlight_device_register(asus->driver->name,
1862                                        &asus->platform_device->dev, asus,
1863                                        &asus_wmi_bl_ops, &props);
1864         if (IS_ERR(bd)) {
1865                 pr_err("Could not register backlight device\n");
1866                 return PTR_ERR(bd);
1867         }
1868
1869         asus->backlight_device = bd;
1870
1871         if (asus->driver->quirks->store_backlight_power)
1872                 asus->driver->panel_power = power;
1873
1874         bd->props.brightness = read_brightness(bd);
1875         bd->props.power = power;
1876         backlight_update_status(bd);
1877
1878         asus->driver->brightness = bd->props.brightness;
1879
1880         return 0;
1881 }
1882
1883 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1884 {
1885         backlight_device_unregister(asus->backlight_device);
1886
1887         asus->backlight_device = NULL;
1888 }
1889
1890 static int is_display_toggle(int code)
1891 {
1892         /* display toggle keys */
1893         if ((code >= 0x61 && code <= 0x67) ||
1894             (code >= 0x8c && code <= 0x93) ||
1895             (code >= 0xa0 && code <= 0xa7) ||
1896             (code >= 0xd0 && code <= 0xd5))
1897                 return 1;
1898
1899         return 0;
1900 }
1901
1902 /* Fn-lock ********************************************************************/
1903
1904 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1905 {
1906         u32 result;
1907
1908         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1909
1910         return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1911                 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1912 }
1913
1914 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1915 {
1916         int mode = asus->fnlock_locked;
1917
1918         asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1919 }
1920
1921 /* WMI events *****************************************************************/
1922
1923 static int asus_wmi_get_event_code(u32 value)
1924 {
1925         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1926         union acpi_object *obj;
1927         acpi_status status;
1928         int code;
1929
1930         status = wmi_get_event_data(value, &response);
1931         if (ACPI_FAILURE(status)) {
1932                 pr_warn("Failed to get WMI notify code: %s\n",
1933                                 acpi_format_exception(status));
1934                 return -EIO;
1935         }
1936
1937         obj = (union acpi_object *)response.pointer;
1938
1939         if (obj && obj->type == ACPI_TYPE_INTEGER)
1940                 code = (int)(obj->integer.value & WMI_EVENT_MASK);
1941         else
1942                 code = -EIO;
1943
1944         kfree(obj);
1945         return code;
1946 }
1947
1948 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
1949 {
1950         int orig_code;
1951         unsigned int key_value = 1;
1952         bool autorelease = 1;
1953
1954         orig_code = code;
1955
1956         if (asus->driver->key_filter) {
1957                 asus->driver->key_filter(asus->driver, &code, &key_value,
1958                                          &autorelease);
1959                 if (code == ASUS_WMI_KEY_IGNORE)
1960                         return;
1961         }
1962
1963         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1964                 code = ASUS_WMI_BRN_UP;
1965         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1966                 code = ASUS_WMI_BRN_DOWN;
1967
1968         if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1969                 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1970                         asus_wmi_backlight_notify(asus, orig_code);
1971                         return;
1972                 }
1973         }
1974
1975         if (code == NOTIFY_KBD_BRTUP) {
1976                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1977                 return;
1978         }
1979         if (code == NOTIFY_KBD_BRTDWN) {
1980                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
1981                 return;
1982         }
1983         if (code == NOTIFY_KBD_BRTTOGGLE) {
1984                 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
1985                         kbd_led_set_by_kbd(asus, 0);
1986                 else
1987                         kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
1988                 return;
1989         }
1990
1991         if (code == NOTIFY_FNLOCK_TOGGLE) {
1992                 asus->fnlock_locked = !asus->fnlock_locked;
1993                 asus_wmi_fnlock_update(asus);
1994                 return;
1995         }
1996
1997         if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
1998                 fan_boost_mode_switch_next(asus);
1999                 return;
2000         }
2001
2002         if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2003                 return;
2004
2005         if (!sparse_keymap_report_event(asus->inputdev, code,
2006                                         key_value, autorelease))
2007                 pr_info("Unknown key %x pressed\n", code);
2008 }
2009
2010 static void asus_wmi_notify(u32 value, void *context)
2011 {
2012         struct asus_wmi *asus = context;
2013         int code;
2014         int i;
2015
2016         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2017                 code = asus_wmi_get_event_code(value);
2018                 if (code < 0) {
2019                         pr_warn("Failed to get notify code: %d\n", code);
2020                         return;
2021                 }
2022
2023                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2024                         return;
2025
2026                 asus_wmi_handle_event_code(code, asus);
2027
2028                 /*
2029                  * Double check that queue is present:
2030                  * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2031                  */
2032                 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2033                         return;
2034         }
2035
2036         pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2037 }
2038
2039 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2040 {
2041         int code;
2042         int i;
2043
2044         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2045                 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2046                 if (code < 0) {
2047                         pr_warn("Failed to get event during flush: %d\n", code);
2048                         return code;
2049                 }
2050
2051                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2052                         return 0;
2053         }
2054
2055         pr_warn("Failed to flush event queue\n");
2056         return -EIO;
2057 }
2058
2059 /* Sysfs **********************************************************************/
2060
2061 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2062                              const char *buf, size_t count)
2063 {
2064         u32 retval;
2065         int err, value;
2066
2067         value = asus_wmi_get_devstate_simple(asus, devid);
2068         if (value < 0)
2069                 return value;
2070
2071         err = kstrtoint(buf, 0, &value);
2072         if (err)
2073                 return err;
2074
2075         err = asus_wmi_set_devstate(devid, value, &retval);
2076         if (err < 0)
2077                 return err;
2078
2079         return count;
2080 }
2081
2082 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2083 {
2084         int value = asus_wmi_get_devstate_simple(asus, devid);
2085
2086         if (value < 0)
2087                 return value;
2088
2089         return sprintf(buf, "%d\n", value);
2090 }
2091
2092 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)                  \
2093         static ssize_t show_##_name(struct device *dev,                 \
2094                                     struct device_attribute *attr,      \
2095                                     char *buf)                          \
2096         {                                                               \
2097                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
2098                                                                         \
2099                 return show_sys_wmi(asus, _cm, buf);                    \
2100         }                                                               \
2101         static ssize_t store_##_name(struct device *dev,                \
2102                                      struct device_attribute *attr,     \
2103                                      const char *buf, size_t count)     \
2104         {                                                               \
2105                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
2106                                                                         \
2107                 return store_sys_wmi(asus, _cm, buf, count);            \
2108         }                                                               \
2109         static struct device_attribute dev_attr_##_name = {             \
2110                 .attr = {                                               \
2111                         .name = __stringify(_name),                     \
2112                         .mode = _mode },                                \
2113                 .show   = show_##_name,                                 \
2114                 .store  = store_##_name,                                \
2115         }
2116
2117 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2118 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2119 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2120 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2121 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2122
2123 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2124                            const char *buf, size_t count)
2125 {
2126         int value, rv;
2127
2128         rv = kstrtoint(buf, 0, &value);
2129         if (rv)
2130                 return rv;
2131
2132         if (value < 0 || value > 2)
2133                 return -EINVAL;
2134
2135         rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2136         if (rv < 0)
2137                 return rv;
2138
2139         return count;
2140 }
2141
2142 static DEVICE_ATTR_WO(cpufv);
2143
2144 static struct attribute *platform_attributes[] = {
2145         &dev_attr_cpufv.attr,
2146         &dev_attr_camera.attr,
2147         &dev_attr_cardr.attr,
2148         &dev_attr_touchpad.attr,
2149         &dev_attr_lid_resume.attr,
2150         &dev_attr_als_enable.attr,
2151         &dev_attr_fan_boost_mode.attr,
2152         NULL
2153 };
2154
2155 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2156                                     struct attribute *attr, int idx)
2157 {
2158         struct device *dev = container_of(kobj, struct device, kobj);
2159         struct asus_wmi *asus = dev_get_drvdata(dev);
2160         bool ok = true;
2161         int devid = -1;
2162
2163         if (attr == &dev_attr_camera.attr)
2164                 devid = ASUS_WMI_DEVID_CAMERA;
2165         else if (attr == &dev_attr_cardr.attr)
2166                 devid = ASUS_WMI_DEVID_CARDREADER;
2167         else if (attr == &dev_attr_touchpad.attr)
2168                 devid = ASUS_WMI_DEVID_TOUCHPAD;
2169         else if (attr == &dev_attr_lid_resume.attr)
2170                 devid = ASUS_WMI_DEVID_LID_RESUME;
2171         else if (attr == &dev_attr_als_enable.attr)
2172                 devid = ASUS_WMI_DEVID_ALS_ENABLE;
2173         else if (attr == &dev_attr_fan_boost_mode.attr)
2174                 ok = asus->fan_boost_mode_available;
2175
2176         if (devid != -1)
2177                 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2178
2179         return ok ? attr->mode : 0;
2180 }
2181
2182 static const struct attribute_group platform_attribute_group = {
2183         .is_visible = asus_sysfs_is_visible,
2184         .attrs = platform_attributes
2185 };
2186
2187 static void asus_wmi_sysfs_exit(struct platform_device *device)
2188 {
2189         sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2190 }
2191
2192 static int asus_wmi_sysfs_init(struct platform_device *device)
2193 {
2194         return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2195 }
2196
2197 /* Platform device ************************************************************/
2198
2199 static int asus_wmi_platform_init(struct asus_wmi *asus)
2200 {
2201         struct device *dev = &asus->platform_device->dev;
2202         char *wmi_uid;
2203         int rv;
2204
2205         /* INIT enable hotkeys on some models */
2206         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2207                 pr_info("Initialization: %#x\n", rv);
2208
2209         /* We don't know yet what to do with this version... */
2210         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2211                 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2212                 asus->spec = rv;
2213         }
2214
2215         /*
2216          * The SFUN method probably allows the original driver to get the list
2217          * of features supported by a given model. For now, 0x0100 or 0x0800
2218          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2219          * The significance of others is yet to be found.
2220          */
2221         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2222                 pr_info("SFUN value: %#x\n", rv);
2223                 asus->sfun = rv;
2224         }
2225
2226         /*
2227          * Eee PC and Notebooks seems to have different method_id for DSTS,
2228          * but it may also be related to the BIOS's SPEC.
2229          * Note, on most Eeepc, there is no way to check if a method exist
2230          * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2231          * but once again, SPEC may probably be used for that kind of things.
2232          *
2233          * Additionally at least TUF Gaming series laptops return nothing for
2234          * unknown methods, so the detection in this way is not possible.
2235          *
2236          * There is strong indication that only ACPI WMI devices that have _UID
2237          * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2238          */
2239         wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2240         if (!wmi_uid)
2241                 return -ENODEV;
2242
2243         if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2244                 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2245                 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2246         } else {
2247                 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2248                 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2249         }
2250
2251         /*
2252          * Some devices can have multiple event codes stored in a queue before
2253          * the module load if it was unloaded intermittently after calling
2254          * the INIT method (enables event handling). The WMI notify handler is
2255          * expected to retrieve all event codes until a retrieved code equals
2256          * queue end marker (One or Ones). Old codes are flushed from the queue
2257          * upon module load. Not enabling this when it should be has minimal
2258          * visible impact so fall back if anything goes wrong.
2259          */
2260         wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2261         if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2262                 dev_info(dev, "Detected ATK, enable event queue\n");
2263
2264                 if (!asus_wmi_notify_queue_flush(asus))
2265                         asus->wmi_event_queue = true;
2266         }
2267
2268         /* CWAP allow to define the behavior of the Fn+F2 key,
2269          * this method doesn't seems to be present on Eee PCs */
2270         if (asus->driver->quirks->wapf >= 0)
2271                 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2272                                       asus->driver->quirks->wapf, NULL);
2273
2274         return 0;
2275 }
2276
2277 /* debugfs ********************************************************************/
2278
2279 struct asus_wmi_debugfs_node {
2280         struct asus_wmi *asus;
2281         char *name;
2282         int (*show) (struct seq_file *m, void *data);
2283 };
2284
2285 static int show_dsts(struct seq_file *m, void *data)
2286 {
2287         struct asus_wmi *asus = m->private;
2288         int err;
2289         u32 retval = -1;
2290
2291         err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2292         if (err < 0)
2293                 return err;
2294
2295         seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2296
2297         return 0;
2298 }
2299
2300 static int show_devs(struct seq_file *m, void *data)
2301 {
2302         struct asus_wmi *asus = m->private;
2303         int err;
2304         u32 retval = -1;
2305
2306         err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2307                                     &retval);
2308         if (err < 0)
2309                 return err;
2310
2311         seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2312                    asus->debug.ctrl_param, retval);
2313
2314         return 0;
2315 }
2316
2317 static int show_call(struct seq_file *m, void *data)
2318 {
2319         struct asus_wmi *asus = m->private;
2320         struct bios_args args = {
2321                 .arg0 = asus->debug.dev_id,
2322                 .arg1 = asus->debug.ctrl_param,
2323         };
2324         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2325         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2326         union acpi_object *obj;
2327         acpi_status status;
2328
2329         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2330                                      0, asus->debug.method_id,
2331                                      &input, &output);
2332
2333         if (ACPI_FAILURE(status))
2334                 return -EIO;
2335
2336         obj = (union acpi_object *)output.pointer;
2337         if (obj && obj->type == ACPI_TYPE_INTEGER)
2338                 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2339                            asus->debug.dev_id, asus->debug.ctrl_param,
2340                            (u32) obj->integer.value);
2341         else
2342                 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2343                            asus->debug.dev_id, asus->debug.ctrl_param,
2344                            obj ? obj->type : -1);
2345
2346         kfree(obj);
2347
2348         return 0;
2349 }
2350
2351 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2352         {NULL, "devs", show_devs},
2353         {NULL, "dsts", show_dsts},
2354         {NULL, "call", show_call},
2355 };
2356
2357 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2358 {
2359         struct asus_wmi_debugfs_node *node = inode->i_private;
2360
2361         return single_open(file, node->show, node->asus);
2362 }
2363
2364 static const struct file_operations asus_wmi_debugfs_io_ops = {
2365         .owner = THIS_MODULE,
2366         .open = asus_wmi_debugfs_open,
2367         .read = seq_read,
2368         .llseek = seq_lseek,
2369         .release = single_release,
2370 };
2371
2372 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2373 {
2374         debugfs_remove_recursive(asus->debug.root);
2375 }
2376
2377 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2378 {
2379         int i;
2380
2381         asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2382
2383         debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2384                            &asus->debug.method_id);
2385
2386         debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2387                            &asus->debug.dev_id);
2388
2389         debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2390                            &asus->debug.ctrl_param);
2391
2392         for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2393                 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2394
2395                 node->asus = asus;
2396                 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2397                                     asus->debug.root, node,
2398                                     &asus_wmi_debugfs_io_ops);
2399         }
2400 }
2401
2402 /* Init / exit ****************************************************************/
2403
2404 static int asus_wmi_add(struct platform_device *pdev)
2405 {
2406         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2407         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2408         struct asus_wmi *asus;
2409         const char *chassis_type;
2410         acpi_status status;
2411         int err;
2412         u32 result;
2413
2414         asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2415         if (!asus)
2416                 return -ENOMEM;
2417
2418         asus->driver = wdrv;
2419         asus->platform_device = pdev;
2420         wdrv->platform_device = pdev;
2421         platform_set_drvdata(asus->platform_device, asus);
2422
2423         if (wdrv->detect_quirks)
2424                 wdrv->detect_quirks(asus->driver);
2425
2426         err = asus_wmi_platform_init(asus);
2427         if (err)
2428                 goto fail_platform;
2429
2430         err = fan_boost_mode_check_present(asus);
2431         if (err)
2432                 goto fail_fan_boost_mode;
2433
2434         err = asus_wmi_sysfs_init(asus->platform_device);
2435         if (err)
2436                 goto fail_sysfs;
2437
2438         err = asus_wmi_input_init(asus);
2439         if (err)
2440                 goto fail_input;
2441
2442         err = asus_wmi_fan_init(asus); /* probably no problems on error */
2443
2444         err = asus_wmi_hwmon_init(asus);
2445         if (err)
2446                 goto fail_hwmon;
2447
2448         err = asus_wmi_led_init(asus);
2449         if (err)
2450                 goto fail_leds;
2451
2452         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2453         if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2454                 asus->driver->wlan_ctrl_by_user = 1;
2455
2456         if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2457                 err = asus_wmi_rfkill_init(asus);
2458                 if (err)
2459                         goto fail_rfkill;
2460         }
2461
2462         if (asus->driver->quirks->wmi_force_als_set)
2463                 asus_wmi_set_als();
2464
2465         /* Some Asus desktop boards export an acpi-video backlight interface,
2466            stop this from showing up */
2467         chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2468         if (chassis_type && !strcmp(chassis_type, "3"))
2469                 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2470
2471         if (asus->driver->quirks->wmi_backlight_power)
2472                 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2473
2474         if (asus->driver->quirks->wmi_backlight_native)
2475                 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2476
2477         if (asus->driver->quirks->xusb2pr)
2478                 asus_wmi_set_xusb2pr(asus);
2479
2480         if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2481                 err = asus_wmi_backlight_init(asus);
2482                 if (err && err != -ENODEV)
2483                         goto fail_backlight;
2484         } else if (asus->driver->quirks->wmi_backlight_set_devstate)
2485                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2486
2487         if (asus_wmi_has_fnlock_key(asus)) {
2488                 asus->fnlock_locked = true;
2489                 asus_wmi_fnlock_update(asus);
2490         }
2491
2492         status = wmi_install_notify_handler(asus->driver->event_guid,
2493                                             asus_wmi_notify, asus);
2494         if (ACPI_FAILURE(status)) {
2495                 pr_err("Unable to register notify handler - %d\n", status);
2496                 err = -ENODEV;
2497                 goto fail_wmi_handler;
2498         }
2499
2500         asus_wmi_battery_init(asus);
2501
2502         asus_wmi_debugfs_init(asus);
2503
2504         return 0;
2505
2506 fail_wmi_handler:
2507         asus_wmi_backlight_exit(asus);
2508 fail_backlight:
2509         asus_wmi_rfkill_exit(asus);
2510 fail_rfkill:
2511         asus_wmi_led_exit(asus);
2512 fail_leds:
2513 fail_hwmon:
2514         asus_wmi_input_exit(asus);
2515 fail_input:
2516         asus_wmi_sysfs_exit(asus->platform_device);
2517 fail_sysfs:
2518 fail_fan_boost_mode:
2519 fail_platform:
2520         kfree(asus);
2521         return err;
2522 }
2523
2524 static int asus_wmi_remove(struct platform_device *device)
2525 {
2526         struct asus_wmi *asus;
2527
2528         asus = platform_get_drvdata(device);
2529         wmi_remove_notify_handler(asus->driver->event_guid);
2530         asus_wmi_backlight_exit(asus);
2531         asus_wmi_input_exit(asus);
2532         asus_wmi_led_exit(asus);
2533         asus_wmi_rfkill_exit(asus);
2534         asus_wmi_debugfs_exit(asus);
2535         asus_wmi_sysfs_exit(asus->platform_device);
2536         asus_fan_set_auto(asus);
2537         asus_wmi_battery_exit(asus);
2538
2539         kfree(asus);
2540         return 0;
2541 }
2542
2543 /* Platform driver - hibernate/resume callbacks *******************************/
2544
2545 static int asus_hotk_thaw(struct device *device)
2546 {
2547         struct asus_wmi *asus = dev_get_drvdata(device);
2548
2549         if (asus->wlan.rfkill) {
2550                 bool wlan;
2551
2552                 /*
2553                  * Work around bios bug - acpi _PTS turns off the wireless led
2554                  * during suspend.  Normally it restores it on resume, but
2555                  * we should kick it ourselves in case hibernation is aborted.
2556                  */
2557                 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2558                 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2559         }
2560
2561         return 0;
2562 }
2563
2564 static int asus_hotk_resume(struct device *device)
2565 {
2566         struct asus_wmi *asus = dev_get_drvdata(device);
2567
2568         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2569                 kbd_led_update(asus);
2570
2571         if (asus_wmi_has_fnlock_key(asus))
2572                 asus_wmi_fnlock_update(asus);
2573         return 0;
2574 }
2575
2576 static int asus_hotk_restore(struct device *device)
2577 {
2578         struct asus_wmi *asus = dev_get_drvdata(device);
2579         int bl;
2580
2581         /* Refresh both wlan rfkill state and pci hotplug */
2582         if (asus->wlan.rfkill)
2583                 asus_rfkill_hotplug(asus);
2584
2585         if (asus->bluetooth.rfkill) {
2586                 bl = !asus_wmi_get_devstate_simple(asus,
2587                                                    ASUS_WMI_DEVID_BLUETOOTH);
2588                 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2589         }
2590         if (asus->wimax.rfkill) {
2591                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2592                 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2593         }
2594         if (asus->wwan3g.rfkill) {
2595                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2596                 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2597         }
2598         if (asus->gps.rfkill) {
2599                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2600                 rfkill_set_sw_state(asus->gps.rfkill, bl);
2601         }
2602         if (asus->uwb.rfkill) {
2603                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2604                 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2605         }
2606         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2607                 kbd_led_update(asus);
2608
2609         if (asus_wmi_has_fnlock_key(asus))
2610                 asus_wmi_fnlock_update(asus);
2611         return 0;
2612 }
2613
2614 static const struct dev_pm_ops asus_pm_ops = {
2615         .thaw = asus_hotk_thaw,
2616         .restore = asus_hotk_restore,
2617         .resume = asus_hotk_resume,
2618 };
2619
2620 /* Registration ***************************************************************/
2621
2622 static int asus_wmi_probe(struct platform_device *pdev)
2623 {
2624         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2625         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2626         int ret;
2627
2628         if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2629                 pr_warn("ASUS Management GUID not found\n");
2630                 return -ENODEV;
2631         }
2632
2633         if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2634                 pr_warn("ASUS Event GUID not found\n");
2635                 return -ENODEV;
2636         }
2637
2638         if (wdrv->probe) {
2639                 ret = wdrv->probe(pdev);
2640                 if (ret)
2641                         return ret;
2642         }
2643
2644         return asus_wmi_add(pdev);
2645 }
2646
2647 static bool used;
2648
2649 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2650 {
2651         struct platform_driver *platform_driver;
2652         struct platform_device *platform_device;
2653
2654         if (used)
2655                 return -EBUSY;
2656
2657         platform_driver = &driver->platform_driver;
2658         platform_driver->remove = asus_wmi_remove;
2659         platform_driver->driver.owner = driver->owner;
2660         platform_driver->driver.name = driver->name;
2661         platform_driver->driver.pm = &asus_pm_ops;
2662
2663         platform_device = platform_create_bundle(platform_driver,
2664                                                  asus_wmi_probe,
2665                                                  NULL, 0, NULL, 0);
2666         if (IS_ERR(platform_device))
2667                 return PTR_ERR(platform_device);
2668
2669         used = true;
2670         return 0;
2671 }
2672 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2673
2674 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2675 {
2676         platform_device_unregister(driver->platform_device);
2677         platform_driver_unregister(&driver->platform_driver);
2678         used = false;
2679 }
2680 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2681
2682 static int __init asus_wmi_init(void)
2683 {
2684         pr_info("ASUS WMI generic driver loaded\n");
2685         return 0;
2686 }
2687
2688 static void __exit asus_wmi_exit(void)
2689 {
2690         pr_info("ASUS WMI generic driver unloaded\n");
2691 }
2692
2693 module_init(asus_wmi_init);
2694 module_exit(asus_wmi_exit);