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