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