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