file, i915: fix file reference for mmap_singleton()
[linux-block.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/acpi.h>
17 #include <linux/backlight.h>
18 #include <linux/debugfs.h>
19 #include <linux/dmi.h>
20 #include <linux/fb.h>
21 #include <linux/hwmon.h>
22 #include <linux/hwmon-sysfs.h>
23 #include <linux/init.h>
24 #include <linux/input.h>
25 #include <linux/input/sparse-keymap.h>
26 #include <linux/kernel.h>
27 #include <linux/leds.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/pci_hotplug.h>
31 #include <linux/platform_data/x86/asus-wmi.h>
32 #include <linux/platform_device.h>
33 #include <linux/platform_profile.h>
34 #include <linux/power_supply.h>
35 #include <linux/rfkill.h>
36 #include <linux/seq_file.h>
37 #include <linux/slab.h>
38 #include <linux/types.h>
39 #include <linux/units.h>
40
41 #include <acpi/battery.h>
42 #include <acpi/video.h>
43
44 #include "asus-wmi.h"
45
46 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
47 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
48 MODULE_DESCRIPTION("Asus Generic WMI Driver");
49 MODULE_LICENSE("GPL");
50
51 static bool fnlock_default = true;
52 module_param(fnlock_default, bool, 0444);
53
54 #define to_asus_wmi_driver(pdrv)                                        \
55         (container_of((pdrv), struct asus_wmi_driver, platform_driver))
56
57 #define ASUS_WMI_MGMT_GUID      "97845ED0-4E6D-11DE-8A39-0800200C9A66"
58
59 #define NOTIFY_BRNUP_MIN                0x11
60 #define NOTIFY_BRNUP_MAX                0x1f
61 #define NOTIFY_BRNDOWN_MIN              0x20
62 #define NOTIFY_BRNDOWN_MAX              0x2e
63 #define NOTIFY_FNLOCK_TOGGLE            0x4e
64 #define NOTIFY_KBD_DOCK_CHANGE          0x75
65 #define NOTIFY_KBD_BRTUP                0xc4
66 #define NOTIFY_KBD_BRTDWN               0xc5
67 #define NOTIFY_KBD_BRTTOGGLE            0xc7
68 #define NOTIFY_KBD_FBM                  0x99
69 #define NOTIFY_KBD_TTP                  0xae
70 #define NOTIFY_LID_FLIP                 0xfa
71 #define NOTIFY_LID_FLIP_ROG             0xbd
72
73 #define ASUS_WMI_FNLOCK_BIOS_DISABLED   BIT(0)
74
75 #define ASUS_MID_FAN_DESC               "mid_fan"
76 #define ASUS_GPU_FAN_DESC               "gpu_fan"
77 #define ASUS_FAN_DESC                   "cpu_fan"
78 #define ASUS_FAN_MFUN                   0x13
79 #define ASUS_FAN_SFUN_READ              0x06
80 #define ASUS_FAN_SFUN_WRITE             0x07
81
82 /* Based on standard hwmon pwmX_enable values */
83 #define ASUS_FAN_CTRL_FULLSPEED         0
84 #define ASUS_FAN_CTRL_MANUAL            1
85 #define ASUS_FAN_CTRL_AUTO              2
86
87 #define ASUS_FAN_BOOST_MODE_NORMAL              0
88 #define ASUS_FAN_BOOST_MODE_OVERBOOST           1
89 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK      0x01
90 #define ASUS_FAN_BOOST_MODE_SILENT              2
91 #define ASUS_FAN_BOOST_MODE_SILENT_MASK         0x02
92 #define ASUS_FAN_BOOST_MODES_MASK               0x03
93
94 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT    0
95 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST  1
96 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT     2
97
98 #define USB_INTEL_XUSB2PR               0xD0
99 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI   0x9c31
100
101 #define ASUS_ACPI_UID_ASUSWMI           "ASUSWMI"
102 #define ASUS_ACPI_UID_ATK               "ATK"
103
104 #define WMI_EVENT_QUEUE_SIZE            0x10
105 #define WMI_EVENT_QUEUE_END             0x1
106 #define WMI_EVENT_MASK                  0xFFFF
107 /* The WMI hotkey event value is always the same. */
108 #define WMI_EVENT_VALUE_ATK             0xFF
109
110 #define WMI_EVENT_MASK                  0xFFFF
111
112 #define FAN_CURVE_POINTS                8
113 #define FAN_CURVE_BUF_LEN               32
114 #define FAN_CURVE_DEV_CPU               0x00
115 #define FAN_CURVE_DEV_GPU               0x01
116 #define FAN_CURVE_DEV_MID               0x02
117 /* Mask to determine if setting temperature or percentage */
118 #define FAN_CURVE_PWM_MASK              0x04
119
120 /* Limits for tunables available on ASUS ROG laptops */
121 #define PPT_TOTAL_MIN           5
122 #define PPT_TOTAL_MAX           250
123 #define PPT_CPU_MIN                     5
124 #define PPT_CPU_MAX                     130
125 #define NVIDIA_BOOST_MIN        5
126 #define NVIDIA_BOOST_MAX        25
127 #define NVIDIA_TEMP_MIN         75
128 #define NVIDIA_TEMP_MAX         87
129
130 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
131
132 static int throttle_thermal_policy_write(struct asus_wmi *);
133
134 static bool ashs_present(void)
135 {
136         int i = 0;
137         while (ashs_ids[i]) {
138                 if (acpi_dev_found(ashs_ids[i++]))
139                         return true;
140         }
141         return false;
142 }
143
144 struct bios_args {
145         u32 arg0;
146         u32 arg1;
147         u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
148         u32 arg3;
149         u32 arg4; /* Some ROG laptops require a full 5 input args */
150         u32 arg5;
151 } __packed;
152
153 /*
154  * Struct that's used for all methods called via AGFN. Naming is
155  * identically to the AML code.
156  */
157 struct agfn_args {
158         u16 mfun; /* probably "Multi-function" to be called */
159         u16 sfun; /* probably "Sub-function" to be called */
160         u16 len;  /* size of the hole struct, including subfunction fields */
161         u8 stas;  /* not used by now */
162         u8 err;   /* zero on success */
163 } __packed;
164
165 /* struct used for calling fan read and write methods */
166 struct agfn_fan_args {
167         struct agfn_args agfn;  /* common fields */
168         u8 fan;                 /* fan number: 0: set auto mode 1: 1st fan */
169         u32 speed;              /* read: RPM/100 - write: 0-255 */
170 } __packed;
171
172 /*
173  * <platform>/    - debugfs root directory
174  *   dev_id      - current dev_id
175  *   ctrl_param  - current ctrl_param
176  *   method_id   - current method_id
177  *   devs        - call DEVS(dev_id, ctrl_param) and print result
178  *   dsts        - call DSTS(dev_id)  and print result
179  *   call        - call method_id(dev_id, ctrl_param) and print result
180  */
181 struct asus_wmi_debug {
182         struct dentry *root;
183         u32 method_id;
184         u32 dev_id;
185         u32 ctrl_param;
186 };
187
188 struct asus_rfkill {
189         struct asus_wmi *asus;
190         struct rfkill *rfkill;
191         u32 dev_id;
192 };
193
194 enum fan_type {
195         FAN_TYPE_NONE = 0,
196         FAN_TYPE_AGFN,          /* deprecated on newer platforms */
197         FAN_TYPE_SPEC83,        /* starting in Spec 8.3, use CPU_FAN_CTRL */
198 };
199
200 struct fan_curve_data {
201         bool enabled;
202         u32 device_id;
203         u8 temps[FAN_CURVE_POINTS];
204         u8 percents[FAN_CURVE_POINTS];
205 };
206
207 struct asus_wmi {
208         int dsts_id;
209         int spec;
210         int sfun;
211         bool wmi_event_queue;
212
213         struct input_dev *inputdev;
214         struct backlight_device *backlight_device;
215         struct platform_device *platform_device;
216
217         struct led_classdev wlan_led;
218         int wlan_led_wk;
219         struct led_classdev tpd_led;
220         int tpd_led_wk;
221         struct led_classdev kbd_led;
222         int kbd_led_wk;
223         struct led_classdev lightbar_led;
224         int lightbar_led_wk;
225         struct led_classdev micmute_led;
226         struct workqueue_struct *led_workqueue;
227         struct work_struct tpd_led_work;
228         struct work_struct wlan_led_work;
229         struct work_struct lightbar_led_work;
230
231         struct asus_rfkill wlan;
232         struct asus_rfkill bluetooth;
233         struct asus_rfkill wimax;
234         struct asus_rfkill wwan3g;
235         struct asus_rfkill gps;
236         struct asus_rfkill uwb;
237
238         int tablet_switch_event_code;
239         u32 tablet_switch_dev_id;
240         bool tablet_switch_inverted;
241
242         enum fan_type fan_type;
243         enum fan_type gpu_fan_type;
244         enum fan_type mid_fan_type;
245         int fan_pwm_mode;
246         int gpu_fan_pwm_mode;
247         int mid_fan_pwm_mode;
248         int agfn_pwm;
249
250         bool fan_boost_mode_available;
251         u8 fan_boost_mode_mask;
252         u8 fan_boost_mode;
253
254         bool charge_mode_available;
255         bool egpu_enable_available;
256         bool egpu_connect_available;
257         bool dgpu_disable_available;
258         bool gpu_mux_mode_available;
259
260         /* Tunables provided by ASUS for gaming laptops */
261         bool ppt_pl2_sppt_available;
262         bool ppt_pl1_spl_available;
263         bool ppt_apu_sppt_available;
264         bool ppt_plat_sppt_available;
265         bool ppt_fppt_available;
266         bool nv_dyn_boost_available;
267         bool nv_temp_tgt_available;
268
269         bool kbd_rgb_mode_available;
270         bool kbd_rgb_state_available;
271
272         bool throttle_thermal_policy_available;
273         u8 throttle_thermal_policy_mode;
274
275         bool cpu_fan_curve_available;
276         bool gpu_fan_curve_available;
277         bool mid_fan_curve_available;
278         struct fan_curve_data custom_fan_curves[3];
279
280         struct platform_profile_handler platform_profile_handler;
281         bool platform_profile_support;
282
283         // The RSOC controls the maximum charging percentage.
284         bool battery_rsoc_available;
285
286         bool panel_overdrive_available;
287         bool mini_led_mode_available;
288
289         struct hotplug_slot hotplug_slot;
290         struct mutex hotplug_lock;
291         struct mutex wmi_lock;
292         struct workqueue_struct *hotplug_workqueue;
293         struct work_struct hotplug_work;
294
295         bool fnlock_locked;
296
297         struct asus_wmi_debug debug;
298
299         struct asus_wmi_driver *driver;
300 };
301
302 /* WMI ************************************************************************/
303
304 static int asus_wmi_evaluate_method3(u32 method_id,
305                 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
306 {
307         struct bios_args args = {
308                 .arg0 = arg0,
309                 .arg1 = arg1,
310                 .arg2 = arg2,
311         };
312         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
313         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
314         acpi_status status;
315         union acpi_object *obj;
316         u32 tmp = 0;
317
318         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
319                                      &input, &output);
320
321         if (ACPI_FAILURE(status))
322                 return -EIO;
323
324         obj = (union acpi_object *)output.pointer;
325         if (obj && obj->type == ACPI_TYPE_INTEGER)
326                 tmp = (u32) obj->integer.value;
327
328         if (retval)
329                 *retval = tmp;
330
331         kfree(obj);
332
333         if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
334                 return -ENODEV;
335
336         return 0;
337 }
338
339 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
340 {
341         return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
342 }
343 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
344
345 static int asus_wmi_evaluate_method5(u32 method_id,
346                 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
347 {
348         struct bios_args args = {
349                 .arg0 = arg0,
350                 .arg1 = arg1,
351                 .arg2 = arg2,
352                 .arg3 = arg3,
353                 .arg4 = arg4,
354         };
355         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
356         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
357         acpi_status status;
358         union acpi_object *obj;
359         u32 tmp = 0;
360
361         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
362                                      &input, &output);
363
364         if (ACPI_FAILURE(status))
365                 return -EIO;
366
367         obj = (union acpi_object *)output.pointer;
368         if (obj && obj->type == ACPI_TYPE_INTEGER)
369                 tmp = (u32) obj->integer.value;
370
371         if (retval)
372                 *retval = tmp;
373
374         kfree(obj);
375
376         if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
377                 return -ENODEV;
378
379         return 0;
380 }
381
382 /*
383  * Returns as an error if the method output is not a buffer. Typically this
384  * means that the method called is unsupported.
385  */
386 static int asus_wmi_evaluate_method_buf(u32 method_id,
387                 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
388 {
389         struct bios_args args = {
390                 .arg0 = arg0,
391                 .arg1 = arg1,
392                 .arg2 = 0,
393         };
394         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
395         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
396         acpi_status status;
397         union acpi_object *obj;
398         int err = 0;
399
400         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
401                                      &input, &output);
402
403         if (ACPI_FAILURE(status))
404                 return -EIO;
405
406         obj = (union acpi_object *)output.pointer;
407
408         switch (obj->type) {
409         case ACPI_TYPE_BUFFER:
410                 if (obj->buffer.length > size) {
411                         err = -ENOSPC;
412                         break;
413                 }
414                 if (obj->buffer.length == 0) {
415                         err = -ENODATA;
416                         break;
417                 }
418
419                 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
420                 break;
421         case ACPI_TYPE_INTEGER:
422                 err = (u32)obj->integer.value;
423
424                 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
425                         err = -ENODEV;
426                 /*
427                  * At least one method returns a 0 with no buffer if no arg
428                  * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
429                  */
430                 if (err == 0)
431                         err = -ENODATA;
432                 break;
433         default:
434                 err = -ENODATA;
435                 break;
436         }
437
438         kfree(obj);
439
440         if (err)
441                 return err;
442
443         return 0;
444 }
445
446 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
447 {
448         struct acpi_buffer input;
449         u64 phys_addr;
450         u32 retval;
451         u32 status;
452
453         /*
454          * Copy to dma capable address otherwise memory corruption occurs as
455          * bios has to be able to access it.
456          */
457         input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
458         input.length = args.length;
459         if (!input.pointer)
460                 return -ENOMEM;
461         phys_addr = virt_to_phys(input.pointer);
462
463         status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
464                                         phys_addr, 0, &retval);
465         if (!status)
466                 memcpy(args.pointer, input.pointer, args.length);
467
468         kfree(input.pointer);
469         if (status)
470                 return -ENXIO;
471
472         return retval;
473 }
474
475 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
476 {
477         return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
478 }
479
480 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
481                                  u32 *retval)
482 {
483         return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
484                                         ctrl_param, retval);
485 }
486
487 /* Helper for special devices with magic return codes */
488 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
489                                       u32 dev_id, u32 mask)
490 {
491         u32 retval = 0;
492         int err;
493
494         err = asus_wmi_get_devstate(asus, dev_id, &retval);
495         if (err < 0)
496                 return err;
497
498         if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
499                 return -ENODEV;
500
501         if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
502                 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
503                         return -ENODEV;
504         }
505
506         return retval & mask;
507 }
508
509 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
510 {
511         return asus_wmi_get_devstate_bits(asus, dev_id,
512                                           ASUS_WMI_DSTS_STATUS_BIT);
513 }
514
515 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
516 {
517         u32 retval;
518         int status = asus_wmi_get_devstate(asus, dev_id, &retval);
519
520         return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
521 }
522
523 /* Input **********************************************************************/
524 static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
525 {
526         input_report_switch(asus->inputdev, SW_TABLET_MODE,
527                             asus->tablet_switch_inverted ? !value : value);
528         input_sync(asus->inputdev);
529 }
530
531 static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
532 {
533         struct device *dev = &asus->platform_device->dev;
534         int result;
535
536         result = asus_wmi_get_devstate_simple(asus, dev_id);
537         if (result >= 0) {
538                 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
539                 asus_wmi_tablet_sw_report(asus, result);
540                 asus->tablet_switch_dev_id = dev_id;
541                 asus->tablet_switch_event_code = event_code;
542         } else if (result == -ENODEV) {
543                 dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
544         } else {
545                 dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
546         }
547 }
548
549 static int asus_wmi_input_init(struct asus_wmi *asus)
550 {
551         struct device *dev = &asus->platform_device->dev;
552         int err;
553
554         asus->inputdev = input_allocate_device();
555         if (!asus->inputdev)
556                 return -ENOMEM;
557
558         asus->inputdev->name = asus->driver->input_name;
559         asus->inputdev->phys = asus->driver->input_phys;
560         asus->inputdev->id.bustype = BUS_HOST;
561         asus->inputdev->dev.parent = dev;
562         set_bit(EV_REP, asus->inputdev->evbit);
563
564         err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
565         if (err)
566                 goto err_free_dev;
567
568         switch (asus->driver->quirks->tablet_switch_mode) {
569         case asus_wmi_no_tablet_switch:
570                 break;
571         case asus_wmi_kbd_dock_devid:
572                 asus->tablet_switch_inverted = true;
573                 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
574                 break;
575         case asus_wmi_lid_flip_devid:
576                 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
577                 break;
578         case asus_wmi_lid_flip_rog_devid:
579                 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
580                 break;
581         }
582
583         err = input_register_device(asus->inputdev);
584         if (err)
585                 goto err_free_dev;
586
587         return 0;
588
589 err_free_dev:
590         input_free_device(asus->inputdev);
591         return err;
592 }
593
594 static void asus_wmi_input_exit(struct asus_wmi *asus)
595 {
596         if (asus->inputdev)
597                 input_unregister_device(asus->inputdev);
598
599         asus->inputdev = NULL;
600 }
601
602 /* Tablet mode ****************************************************************/
603
604 static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
605 {
606         int result;
607
608         if (!asus->tablet_switch_dev_id)
609                 return;
610
611         result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
612         if (result >= 0)
613                 asus_wmi_tablet_sw_report(asus, result);
614 }
615
616 /* Charging mode, 1=Barrel, 2=USB ******************************************/
617 static ssize_t charge_mode_show(struct device *dev,
618                                    struct device_attribute *attr, char *buf)
619 {
620         struct asus_wmi *asus = dev_get_drvdata(dev);
621         int result, value;
622
623         result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
624         if (result < 0)
625                 return result;
626
627         return sysfs_emit(buf, "%d\n", value & 0xff);
628 }
629
630 static DEVICE_ATTR_RO(charge_mode);
631
632 /* dGPU ********************************************************************/
633 static ssize_t dgpu_disable_show(struct device *dev,
634                                    struct device_attribute *attr, char *buf)
635 {
636         struct asus_wmi *asus = dev_get_drvdata(dev);
637         int result;
638
639         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
640         if (result < 0)
641                 return result;
642
643         return sysfs_emit(buf, "%d\n", result);
644 }
645
646 /*
647  * A user may be required to store the value twice, typcial store first, then
648  * rescan PCI bus to activate power, then store a second time to save correctly.
649  * The reason for this is that an extra code path in the ACPI is enabled when
650  * the device and bus are powered.
651  */
652 static ssize_t dgpu_disable_store(struct device *dev,
653                                     struct device_attribute *attr,
654                                     const char *buf, size_t count)
655 {
656         int result, err;
657         u32 disable;
658
659         struct asus_wmi *asus = dev_get_drvdata(dev);
660
661         result = kstrtou32(buf, 10, &disable);
662         if (result)
663                 return result;
664
665         if (disable > 1)
666                 return -EINVAL;
667
668         if (asus->gpu_mux_mode_available) {
669                 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
670                 if (result < 0)
671                         /* An error here may signal greater failure of GPU handling */
672                         return result;
673                 if (!result && disable) {
674                         err = -ENODEV;
675                         pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err);
676                         return err;
677                 }
678         }
679
680         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
681         if (err) {
682                 pr_warn("Failed to set dgpu disable: %d\n", err);
683                 return err;
684         }
685
686         if (result > 1) {
687                 pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
688                 return -EIO;
689         }
690
691         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
692
693         return count;
694 }
695 static DEVICE_ATTR_RW(dgpu_disable);
696
697 /* eGPU ********************************************************************/
698 static ssize_t egpu_enable_show(struct device *dev,
699                                    struct device_attribute *attr, char *buf)
700 {
701         struct asus_wmi *asus = dev_get_drvdata(dev);
702         int result;
703
704         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
705         if (result < 0)
706                 return result;
707
708         return sysfs_emit(buf, "%d\n", result);
709 }
710
711 /* The ACPI call to enable the eGPU also disables the internal dGPU */
712 static ssize_t egpu_enable_store(struct device *dev,
713                                     struct device_attribute *attr,
714                                     const char *buf, size_t count)
715 {
716         int result, err;
717         u32 enable;
718
719         struct asus_wmi *asus = dev_get_drvdata(dev);
720
721         err = kstrtou32(buf, 10, &enable);
722         if (err)
723                 return err;
724
725         if (enable > 1)
726                 return -EINVAL;
727
728         err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
729         if (err < 0) {
730                 pr_warn("Failed to get egpu connection status: %d\n", err);
731                 return err;
732         }
733
734         if (asus->gpu_mux_mode_available) {
735                 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
736                 if (result < 0) {
737                         /* An error here may signal greater failure of GPU handling */
738                         pr_warn("Failed to get gpu mux status: %d\n", result);
739                         return result;
740                 }
741                 if (!result && enable) {
742                         err = -ENODEV;
743                         pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err);
744                         return err;
745                 }
746         }
747
748         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
749         if (err) {
750                 pr_warn("Failed to set egpu state: %d\n", err);
751                 return err;
752         }
753
754         if (result > 1) {
755                 pr_warn("Failed to set egpu state (retval): 0x%x\n", result);
756                 return -EIO;
757         }
758
759         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
760
761         return count;
762 }
763 static DEVICE_ATTR_RW(egpu_enable);
764
765 /* Is eGPU connected? *********************************************************/
766 static ssize_t egpu_connected_show(struct device *dev,
767                                    struct device_attribute *attr, char *buf)
768 {
769         struct asus_wmi *asus = dev_get_drvdata(dev);
770         int result;
771
772         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
773         if (result < 0)
774                 return result;
775
776         return sysfs_emit(buf, "%d\n", result);
777 }
778
779 static DEVICE_ATTR_RO(egpu_connected);
780
781 /* gpu mux switch *************************************************************/
782 static ssize_t gpu_mux_mode_show(struct device *dev,
783                                  struct device_attribute *attr, char *buf)
784 {
785         struct asus_wmi *asus = dev_get_drvdata(dev);
786         int result;
787
788         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
789         if (result < 0)
790                 return result;
791
792         return sysfs_emit(buf, "%d\n", result);
793 }
794
795 static ssize_t gpu_mux_mode_store(struct device *dev,
796                                   struct device_attribute *attr,
797                                   const char *buf, size_t count)
798 {
799         struct asus_wmi *asus = dev_get_drvdata(dev);
800         int result, err;
801         u32 optimus;
802
803         err = kstrtou32(buf, 10, &optimus);
804         if (err)
805                 return err;
806
807         if (optimus > 1)
808                 return -EINVAL;
809
810         if (asus->dgpu_disable_available) {
811                 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
812                 if (result < 0)
813                         /* An error here may signal greater failure of GPU handling */
814                         return result;
815                 if (result && !optimus) {
816                         err = -ENODEV;
817                         pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err);
818                         return err;
819                 }
820         }
821
822         if (asus->egpu_enable_available) {
823                 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
824                 if (result < 0)
825                         /* An error here may signal greater failure of GPU handling */
826                         return result;
827                 if (result && !optimus) {
828                         err = -ENODEV;
829                         pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err);
830                         return err;
831                 }
832         }
833
834         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_MUX, optimus, &result);
835         if (err) {
836                 dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
837                 return err;
838         }
839         /* !1 is considered a fail by ASUS */
840         if (result != 1) {
841                 dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
842                 return -EIO;
843         }
844
845         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
846
847         return count;
848 }
849 static DEVICE_ATTR_RW(gpu_mux_mode);
850
851 /* TUF Laptop Keyboard RGB Modes **********************************************/
852 static ssize_t kbd_rgb_mode_store(struct device *dev,
853                                  struct device_attribute *attr,
854                                  const char *buf, size_t count)
855 {
856         u32 cmd, mode, r, g, b, speed;
857         int err;
858
859         if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
860                 return -EINVAL;
861
862         /* B3 is set and B4 is save to BIOS */
863         switch (cmd) {
864         case 0:
865                 cmd = 0xb3;
866                 break;
867         case 1:
868                 cmd = 0xb4;
869                 break;
870         default:
871                 return -EINVAL;
872         }
873
874         /* These are the known usable modes across all TUF/ROG */
875         if (mode >= 12 || mode == 9)
876                 mode = 10;
877
878         switch (speed) {
879         case 0:
880                 speed = 0xe1;
881                 break;
882         case 1:
883                 speed = 0xeb;
884                 break;
885         case 2:
886                 speed = 0xf5;
887                 break;
888         default:
889                 speed = 0xeb;
890         }
891
892         err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, ASUS_WMI_DEVID_TUF_RGB_MODE,
893                         cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
894         if (err)
895                 return err;
896
897         return count;
898 }
899 static DEVICE_ATTR_WO(kbd_rgb_mode);
900
901 static ssize_t kbd_rgb_mode_index_show(struct device *device,
902                                                  struct device_attribute *attr,
903                                                  char *buf)
904 {
905         return sysfs_emit(buf, "%s\n", "cmd mode red green blue speed");
906 }
907 static DEVICE_ATTR_RO(kbd_rgb_mode_index);
908
909 static struct attribute *kbd_rgb_mode_attrs[] = {
910         &dev_attr_kbd_rgb_mode.attr,
911         &dev_attr_kbd_rgb_mode_index.attr,
912         NULL,
913 };
914
915 static const struct attribute_group kbd_rgb_mode_group = {
916         .attrs = kbd_rgb_mode_attrs,
917 };
918
919 /* TUF Laptop Keyboard RGB State **********************************************/
920 static ssize_t kbd_rgb_state_store(struct device *dev,
921                                  struct device_attribute *attr,
922                                  const char *buf, size_t count)
923 {
924         u32 flags, cmd, boot, awake, sleep, keyboard;
925         int err;
926
927         if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
928                 return -EINVAL;
929
930         if (cmd)
931                 cmd = BIT(2);
932
933         flags = 0;
934         if (boot)
935                 flags |= BIT(1);
936         if (awake)
937                 flags |= BIT(3);
938         if (sleep)
939                 flags |= BIT(5);
940         if (keyboard)
941                 flags |= BIT(7);
942
943         /* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
944         err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
945                         ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
946         if (err)
947                 return err;
948
949         return count;
950 }
951 static DEVICE_ATTR_WO(kbd_rgb_state);
952
953 static ssize_t kbd_rgb_state_index_show(struct device *device,
954                                                  struct device_attribute *attr,
955                                                  char *buf)
956 {
957         return sysfs_emit(buf, "%s\n", "cmd boot awake sleep keyboard");
958 }
959 static DEVICE_ATTR_RO(kbd_rgb_state_index);
960
961 static struct attribute *kbd_rgb_state_attrs[] = {
962         &dev_attr_kbd_rgb_state.attr,
963         &dev_attr_kbd_rgb_state_index.attr,
964         NULL,
965 };
966
967 static const struct attribute_group kbd_rgb_state_group = {
968         .attrs = kbd_rgb_state_attrs,
969 };
970
971 static const struct attribute_group *kbd_rgb_mode_groups[] = {
972         NULL,
973         NULL,
974         NULL,
975 };
976
977 /* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/
978 static ssize_t ppt_pl2_sppt_store(struct device *dev,
979                                     struct device_attribute *attr,
980                                     const char *buf, size_t count)
981 {
982         int result, err;
983         u32 value;
984
985         struct asus_wmi *asus = dev_get_drvdata(dev);
986
987         result = kstrtou32(buf, 10, &value);
988         if (result)
989                 return result;
990
991         if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
992                 return -EINVAL;
993
994         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result);
995         if (err) {
996                 pr_warn("Failed to set ppt_pl2_sppt: %d\n", err);
997                 return err;
998         }
999
1000         if (result > 1) {
1001                 pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result);
1002                 return -EIO;
1003         }
1004
1005         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt");
1006
1007         return count;
1008 }
1009 static DEVICE_ATTR_WO(ppt_pl2_sppt);
1010
1011 /* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/
1012 static ssize_t ppt_pl1_spl_store(struct device *dev,
1013                                     struct device_attribute *attr,
1014                                     const char *buf, size_t count)
1015 {
1016         int result, err;
1017         u32 value;
1018
1019         struct asus_wmi *asus = dev_get_drvdata(dev);
1020
1021         result = kstrtou32(buf, 10, &value);
1022         if (result)
1023                 return result;
1024
1025         if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1026                 return -EINVAL;
1027
1028         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result);
1029         if (err) {
1030                 pr_warn("Failed to set ppt_pl1_spl: %d\n", err);
1031                 return err;
1032         }
1033
1034         if (result > 1) {
1035                 pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result);
1036                 return -EIO;
1037         }
1038
1039         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl");
1040
1041         return count;
1042 }
1043 static DEVICE_ATTR_WO(ppt_pl1_spl);
1044
1045 /* Tunable: PPT APU FPPT ******************************************************/
1046 static ssize_t ppt_fppt_store(struct device *dev,
1047                                     struct device_attribute *attr,
1048                                     const char *buf, size_t count)
1049 {
1050         int result, err;
1051         u32 value;
1052
1053         struct asus_wmi *asus = dev_get_drvdata(dev);
1054
1055         result = kstrtou32(buf, 10, &value);
1056         if (result)
1057                 return result;
1058
1059         if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1060                 return -EINVAL;
1061
1062         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_FPPT, value, &result);
1063         if (err) {
1064                 pr_warn("Failed to set ppt_fppt: %d\n", err);
1065                 return err;
1066         }
1067
1068         if (result > 1) {
1069                 pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result);
1070                 return -EIO;
1071         }
1072
1073         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt");
1074
1075         return count;
1076 }
1077 static DEVICE_ATTR_WO(ppt_fppt);
1078
1079 /* Tunable: PPT APU SPPT *****************************************************/
1080 static ssize_t ppt_apu_sppt_store(struct device *dev,
1081                                     struct device_attribute *attr,
1082                                     const char *buf, size_t count)
1083 {
1084         int result, err;
1085         u32 value;
1086
1087         struct asus_wmi *asus = dev_get_drvdata(dev);
1088
1089         result = kstrtou32(buf, 10, &value);
1090         if (result)
1091                 return result;
1092
1093         if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1094                 return -EINVAL;
1095
1096         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result);
1097         if (err) {
1098                 pr_warn("Failed to set ppt_apu_sppt: %d\n", err);
1099                 return err;
1100         }
1101
1102         if (result > 1) {
1103                 pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result);
1104                 return -EIO;
1105         }
1106
1107         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt");
1108
1109         return count;
1110 }
1111 static DEVICE_ATTR_WO(ppt_apu_sppt);
1112
1113 /* Tunable: PPT platform SPPT ************************************************/
1114 static ssize_t ppt_platform_sppt_store(struct device *dev,
1115                                     struct device_attribute *attr,
1116                                     const char *buf, size_t count)
1117 {
1118         int result, err;
1119         u32 value;
1120
1121         struct asus_wmi *asus = dev_get_drvdata(dev);
1122
1123         result = kstrtou32(buf, 10, &value);
1124         if (result)
1125                 return result;
1126
1127         if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1128                 return -EINVAL;
1129
1130         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result);
1131         if (err) {
1132                 pr_warn("Failed to set ppt_platform_sppt: %d\n", err);
1133                 return err;
1134         }
1135
1136         if (result > 1) {
1137                 pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result);
1138                 return -EIO;
1139         }
1140
1141         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt");
1142
1143         return count;
1144 }
1145 static DEVICE_ATTR_WO(ppt_platform_sppt);
1146
1147 /* Tunable: NVIDIA dynamic boost *********************************************/
1148 static ssize_t nv_dynamic_boost_store(struct device *dev,
1149                                     struct device_attribute *attr,
1150                                     const char *buf, size_t count)
1151 {
1152         int result, err;
1153         u32 value;
1154
1155         struct asus_wmi *asus = dev_get_drvdata(dev);
1156
1157         result = kstrtou32(buf, 10, &value);
1158         if (result)
1159                 return result;
1160
1161         if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX)
1162                 return -EINVAL;
1163
1164         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result);
1165         if (err) {
1166                 pr_warn("Failed to set nv_dynamic_boost: %d\n", err);
1167                 return err;
1168         }
1169
1170         if (result > 1) {
1171                 pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result);
1172                 return -EIO;
1173         }
1174
1175         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost");
1176
1177         return count;
1178 }
1179 static DEVICE_ATTR_WO(nv_dynamic_boost);
1180
1181 /* Tunable: NVIDIA temperature target ****************************************/
1182 static ssize_t nv_temp_target_store(struct device *dev,
1183                                     struct device_attribute *attr,
1184                                     const char *buf, size_t count)
1185 {
1186         int result, err;
1187         u32 value;
1188
1189         struct asus_wmi *asus = dev_get_drvdata(dev);
1190
1191         result = kstrtou32(buf, 10, &value);
1192         if (result)
1193                 return result;
1194
1195         if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX)
1196                 return -EINVAL;
1197
1198         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result);
1199         if (err) {
1200                 pr_warn("Failed to set nv_temp_target: %d\n", err);
1201                 return err;
1202         }
1203
1204         if (result > 1) {
1205                 pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result);
1206                 return -EIO;
1207         }
1208
1209         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target");
1210
1211         return count;
1212 }
1213 static DEVICE_ATTR_WO(nv_temp_target);
1214
1215 /* Battery ********************************************************************/
1216
1217 /* The battery maximum charging percentage */
1218 static int charge_end_threshold;
1219
1220 static ssize_t charge_control_end_threshold_store(struct device *dev,
1221                                                   struct device_attribute *attr,
1222                                                   const char *buf, size_t count)
1223 {
1224         int value, ret, rv;
1225
1226         ret = kstrtouint(buf, 10, &value);
1227         if (ret)
1228                 return ret;
1229
1230         if (value < 0 || value > 100)
1231                 return -EINVAL;
1232
1233         ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
1234         if (ret)
1235                 return ret;
1236
1237         if (rv != 1)
1238                 return -EIO;
1239
1240         /* There isn't any method in the DSDT to read the threshold, so we
1241          * save the threshold.
1242          */
1243         charge_end_threshold = value;
1244         return count;
1245 }
1246
1247 static ssize_t charge_control_end_threshold_show(struct device *device,
1248                                                  struct device_attribute *attr,
1249                                                  char *buf)
1250 {
1251         return sysfs_emit(buf, "%d\n", charge_end_threshold);
1252 }
1253
1254 static DEVICE_ATTR_RW(charge_control_end_threshold);
1255
1256 static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
1257 {
1258         /* The WMI method does not provide a way to specific a battery, so we
1259          * just assume it is the first battery.
1260          * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
1261          * battery is named BATT.
1262          */
1263         if (strcmp(battery->desc->name, "BAT0") != 0 &&
1264             strcmp(battery->desc->name, "BAT1") != 0 &&
1265             strcmp(battery->desc->name, "BATC") != 0 &&
1266             strcmp(battery->desc->name, "BATT") != 0)
1267                 return -ENODEV;
1268
1269         if (device_create_file(&battery->dev,
1270             &dev_attr_charge_control_end_threshold))
1271                 return -ENODEV;
1272
1273         /* The charge threshold is only reset when the system is power cycled,
1274          * and we can't get the current threshold so let set it to 100% when
1275          * a battery is added.
1276          */
1277         asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
1278         charge_end_threshold = 100;
1279
1280         return 0;
1281 }
1282
1283 static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
1284 {
1285         device_remove_file(&battery->dev,
1286                            &dev_attr_charge_control_end_threshold);
1287         return 0;
1288 }
1289
1290 static struct acpi_battery_hook battery_hook = {
1291         .add_battery = asus_wmi_battery_add,
1292         .remove_battery = asus_wmi_battery_remove,
1293         .name = "ASUS Battery Extension",
1294 };
1295
1296 static void asus_wmi_battery_init(struct asus_wmi *asus)
1297 {
1298         asus->battery_rsoc_available = false;
1299         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
1300                 asus->battery_rsoc_available = true;
1301                 battery_hook_register(&battery_hook);
1302         }
1303 }
1304
1305 static void asus_wmi_battery_exit(struct asus_wmi *asus)
1306 {
1307         if (asus->battery_rsoc_available)
1308                 battery_hook_unregister(&battery_hook);
1309 }
1310
1311 /* LEDs ***********************************************************************/
1312
1313 /*
1314  * These functions actually update the LED's, and are called from a
1315  * workqueue. By doing this as separate work rather than when the LED
1316  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
1317  * potentially bad time, such as a timer interrupt.
1318  */
1319 static void tpd_led_update(struct work_struct *work)
1320 {
1321         int ctrl_param;
1322         struct asus_wmi *asus;
1323
1324         asus = container_of(work, struct asus_wmi, tpd_led_work);
1325
1326         ctrl_param = asus->tpd_led_wk;
1327         asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
1328 }
1329
1330 static void tpd_led_set(struct led_classdev *led_cdev,
1331                         enum led_brightness value)
1332 {
1333         struct asus_wmi *asus;
1334
1335         asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1336
1337         asus->tpd_led_wk = !!value;
1338         queue_work(asus->led_workqueue, &asus->tpd_led_work);
1339 }
1340
1341 static int read_tpd_led_state(struct asus_wmi *asus)
1342 {
1343         return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
1344 }
1345
1346 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
1347 {
1348         struct asus_wmi *asus;
1349
1350         asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1351
1352         return read_tpd_led_state(asus);
1353 }
1354
1355 static void kbd_led_update(struct asus_wmi *asus)
1356 {
1357         int ctrl_param = 0;
1358
1359         ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
1360         asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
1361 }
1362
1363 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
1364 {
1365         int retval;
1366
1367         /*
1368          * bits 0-2: level
1369          * bit 7: light on/off
1370          * bit 8-10: environment (0: dark, 1: normal, 2: light)
1371          * bit 17: status unknown
1372          */
1373         retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
1374                                             0xFFFF);
1375
1376         /* Unknown status is considered as off */
1377         if (retval == 0x8000)
1378                 retval = 0;
1379
1380         if (retval < 0)
1381                 return retval;
1382
1383         if (level)
1384                 *level = retval & 0x7F;
1385         if (env)
1386                 *env = (retval >> 8) & 0x7F;
1387         return 0;
1388 }
1389
1390 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
1391 {
1392         struct asus_wmi *asus;
1393         int max_level;
1394
1395         asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1396         max_level = asus->kbd_led.max_brightness;
1397
1398         asus->kbd_led_wk = clamp_val(value, 0, max_level);
1399         kbd_led_update(asus);
1400 }
1401
1402 static void kbd_led_set(struct led_classdev *led_cdev,
1403                         enum led_brightness value)
1404 {
1405         /* Prevent disabling keyboard backlight on module unregister */
1406         if (led_cdev->flags & LED_UNREGISTERING)
1407                 return;
1408
1409         do_kbd_led_set(led_cdev, value);
1410 }
1411
1412 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
1413 {
1414         struct led_classdev *led_cdev = &asus->kbd_led;
1415
1416         do_kbd_led_set(led_cdev, value);
1417         led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
1418 }
1419
1420 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1421 {
1422         struct asus_wmi *asus;
1423         int retval, value;
1424
1425         asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1426
1427         retval = kbd_led_read(asus, &value, NULL);
1428         if (retval < 0)
1429                 return retval;
1430
1431         return value;
1432 }
1433
1434 static int wlan_led_unknown_state(struct asus_wmi *asus)
1435 {
1436         u32 result;
1437
1438         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1439
1440         return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
1441 }
1442
1443 static void wlan_led_update(struct work_struct *work)
1444 {
1445         int ctrl_param;
1446         struct asus_wmi *asus;
1447
1448         asus = container_of(work, struct asus_wmi, wlan_led_work);
1449
1450         ctrl_param = asus->wlan_led_wk;
1451         asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
1452 }
1453
1454 static void wlan_led_set(struct led_classdev *led_cdev,
1455                          enum led_brightness value)
1456 {
1457         struct asus_wmi *asus;
1458
1459         asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1460
1461         asus->wlan_led_wk = !!value;
1462         queue_work(asus->led_workqueue, &asus->wlan_led_work);
1463 }
1464
1465 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
1466 {
1467         struct asus_wmi *asus;
1468         u32 result;
1469
1470         asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1471         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1472
1473         return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1474 }
1475
1476 static void lightbar_led_update(struct work_struct *work)
1477 {
1478         struct asus_wmi *asus;
1479         int ctrl_param;
1480
1481         asus = container_of(work, struct asus_wmi, lightbar_led_work);
1482
1483         ctrl_param = asus->lightbar_led_wk;
1484         asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1485 }
1486
1487 static void lightbar_led_set(struct led_classdev *led_cdev,
1488                              enum led_brightness value)
1489 {
1490         struct asus_wmi *asus;
1491
1492         asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1493
1494         asus->lightbar_led_wk = !!value;
1495         queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1496 }
1497
1498 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1499 {
1500         struct asus_wmi *asus;
1501         u32 result;
1502
1503         asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1504         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1505
1506         return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1507 }
1508
1509 static int micmute_led_set(struct led_classdev *led_cdev,
1510                            enum led_brightness brightness)
1511 {
1512         int state = brightness != LED_OFF;
1513         int err;
1514
1515         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1516         return err < 0 ? err : 0;
1517 }
1518
1519 static void asus_wmi_led_exit(struct asus_wmi *asus)
1520 {
1521         led_classdev_unregister(&asus->kbd_led);
1522         led_classdev_unregister(&asus->tpd_led);
1523         led_classdev_unregister(&asus->wlan_led);
1524         led_classdev_unregister(&asus->lightbar_led);
1525         led_classdev_unregister(&asus->micmute_led);
1526
1527         if (asus->led_workqueue)
1528                 destroy_workqueue(asus->led_workqueue);
1529 }
1530
1531 static int asus_wmi_led_init(struct asus_wmi *asus)
1532 {
1533         int rv = 0, num_rgb_groups = 0, led_val;
1534
1535         if (asus->kbd_rgb_mode_available)
1536                 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
1537         if (asus->kbd_rgb_state_available)
1538                 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
1539
1540         asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1541         if (!asus->led_workqueue)
1542                 return -ENOMEM;
1543
1544         if (read_tpd_led_state(asus) >= 0) {
1545                 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1546
1547                 asus->tpd_led.name = "asus::touchpad";
1548                 asus->tpd_led.brightness_set = tpd_led_set;
1549                 asus->tpd_led.brightness_get = tpd_led_get;
1550                 asus->tpd_led.max_brightness = 1;
1551
1552                 rv = led_classdev_register(&asus->platform_device->dev,
1553                                            &asus->tpd_led);
1554                 if (rv)
1555                         goto error;
1556         }
1557
1558         if (!kbd_led_read(asus, &led_val, NULL)) {
1559                 asus->kbd_led_wk = led_val;
1560                 asus->kbd_led.name = "asus::kbd_backlight";
1561                 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1562                 asus->kbd_led.brightness_set = kbd_led_set;
1563                 asus->kbd_led.brightness_get = kbd_led_get;
1564                 asus->kbd_led.max_brightness = 3;
1565
1566                 if (num_rgb_groups != 0)
1567                         asus->kbd_led.groups = kbd_rgb_mode_groups;
1568
1569                 rv = led_classdev_register(&asus->platform_device->dev,
1570                                            &asus->kbd_led);
1571                 if (rv)
1572                         goto error;
1573         }
1574
1575         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1576                         && (asus->driver->quirks->wapf > 0)) {
1577                 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1578
1579                 asus->wlan_led.name = "asus::wlan";
1580                 asus->wlan_led.brightness_set = wlan_led_set;
1581                 if (!wlan_led_unknown_state(asus))
1582                         asus->wlan_led.brightness_get = wlan_led_get;
1583                 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1584                 asus->wlan_led.max_brightness = 1;
1585                 asus->wlan_led.default_trigger = "asus-wlan";
1586
1587                 rv = led_classdev_register(&asus->platform_device->dev,
1588                                            &asus->wlan_led);
1589                 if (rv)
1590                         goto error;
1591         }
1592
1593         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1594                 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1595
1596                 asus->lightbar_led.name = "asus::lightbar";
1597                 asus->lightbar_led.brightness_set = lightbar_led_set;
1598                 asus->lightbar_led.brightness_get = lightbar_led_get;
1599                 asus->lightbar_led.max_brightness = 1;
1600
1601                 rv = led_classdev_register(&asus->platform_device->dev,
1602                                            &asus->lightbar_led);
1603         }
1604
1605         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1606                 asus->micmute_led.name = "platform::micmute";
1607                 asus->micmute_led.max_brightness = 1;
1608                 asus->micmute_led.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
1609                 asus->micmute_led.brightness_set_blocking = micmute_led_set;
1610                 asus->micmute_led.default_trigger = "audio-micmute";
1611
1612                 rv = led_classdev_register(&asus->platform_device->dev,
1613                                                 &asus->micmute_led);
1614                 if (rv)
1615                         goto error;
1616         }
1617
1618 error:
1619         if (rv)
1620                 asus_wmi_led_exit(asus);
1621
1622         return rv;
1623 }
1624
1625 /* RF *************************************************************************/
1626
1627 /*
1628  * PCI hotplug (for wlan rfkill)
1629  */
1630 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1631 {
1632         int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1633
1634         if (result < 0)
1635                 return false;
1636         return !result;
1637 }
1638
1639 static void asus_rfkill_hotplug(struct asus_wmi *asus)
1640 {
1641         struct pci_dev *dev;
1642         struct pci_bus *bus;
1643         bool blocked;
1644         bool absent;
1645         u32 l;
1646
1647         mutex_lock(&asus->wmi_lock);
1648         blocked = asus_wlan_rfkill_blocked(asus);
1649         mutex_unlock(&asus->wmi_lock);
1650
1651         mutex_lock(&asus->hotplug_lock);
1652         pci_lock_rescan_remove();
1653
1654         if (asus->wlan.rfkill)
1655                 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1656
1657         if (asus->hotplug_slot.ops) {
1658                 bus = pci_find_bus(0, 1);
1659                 if (!bus) {
1660                         pr_warn("Unable to find PCI bus 1?\n");
1661                         goto out_unlock;
1662                 }
1663
1664                 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1665                         pr_err("Unable to read PCI config space?\n");
1666                         goto out_unlock;
1667                 }
1668                 absent = (l == 0xffffffff);
1669
1670                 if (blocked != absent) {
1671                         pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1672                                 blocked ? "blocked" : "unblocked",
1673                                 absent ? "absent" : "present");
1674                         pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1675                         goto out_unlock;
1676                 }
1677
1678                 if (!blocked) {
1679                         dev = pci_get_slot(bus, 0);
1680                         if (dev) {
1681                                 /* Device already present */
1682                                 pci_dev_put(dev);
1683                                 goto out_unlock;
1684                         }
1685                         dev = pci_scan_single_device(bus, 0);
1686                         if (dev) {
1687                                 pci_bus_assign_resources(bus);
1688                                 pci_bus_add_device(dev);
1689                         }
1690                 } else {
1691                         dev = pci_get_slot(bus, 0);
1692                         if (dev) {
1693                                 pci_stop_and_remove_bus_device(dev);
1694                                 pci_dev_put(dev);
1695                         }
1696                 }
1697         }
1698
1699 out_unlock:
1700         pci_unlock_rescan_remove();
1701         mutex_unlock(&asus->hotplug_lock);
1702 }
1703
1704 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1705 {
1706         struct asus_wmi *asus = data;
1707
1708         if (event != ACPI_NOTIFY_BUS_CHECK)
1709                 return;
1710
1711         /*
1712          * We can't call directly asus_rfkill_hotplug because most
1713          * of the time WMBC is still being executed and not reetrant.
1714          * There is currently no way to tell ACPICA that  we want this
1715          * method to be serialized, we schedule a asus_rfkill_hotplug
1716          * call later, in a safer context.
1717          */
1718         queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1719 }
1720
1721 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1722 {
1723         acpi_status status;
1724         acpi_handle handle;
1725
1726         status = acpi_get_handle(NULL, node, &handle);
1727         if (ACPI_FAILURE(status))
1728                 return -ENODEV;
1729
1730         status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1731                                              asus_rfkill_notify, asus);
1732         if (ACPI_FAILURE(status))
1733                 pr_warn("Failed to register notify on %s\n", node);
1734
1735         return 0;
1736 }
1737
1738 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1739 {
1740         acpi_status status = AE_OK;
1741         acpi_handle handle;
1742
1743         status = acpi_get_handle(NULL, node, &handle);
1744         if (ACPI_FAILURE(status))
1745                 return;
1746
1747         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1748                                             asus_rfkill_notify);
1749         if (ACPI_FAILURE(status))
1750                 pr_err("Error removing rfkill notify handler %s\n", node);
1751 }
1752
1753 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1754                                    u8 *value)
1755 {
1756         struct asus_wmi *asus = container_of(hotplug_slot,
1757                                              struct asus_wmi, hotplug_slot);
1758         int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1759
1760         if (result < 0)
1761                 return result;
1762
1763         *value = !!result;
1764         return 0;
1765 }
1766
1767 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1768         .get_adapter_status = asus_get_adapter_status,
1769         .get_power_status = asus_get_adapter_status,
1770 };
1771
1772 static void asus_hotplug_work(struct work_struct *work)
1773 {
1774         struct asus_wmi *asus;
1775
1776         asus = container_of(work, struct asus_wmi, hotplug_work);
1777         asus_rfkill_hotplug(asus);
1778 }
1779
1780 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1781 {
1782         int ret = -ENOMEM;
1783         struct pci_bus *bus = pci_find_bus(0, 1);
1784
1785         if (!bus) {
1786                 pr_err("Unable to find wifi PCI bus\n");
1787                 return -ENODEV;
1788         }
1789
1790         asus->hotplug_workqueue =
1791             create_singlethread_workqueue("hotplug_workqueue");
1792         if (!asus->hotplug_workqueue)
1793                 goto error_workqueue;
1794
1795         INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1796
1797         asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1798
1799         ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1800         if (ret) {
1801                 pr_err("Unable to register hotplug slot - %d\n", ret);
1802                 goto error_register;
1803         }
1804
1805         return 0;
1806
1807 error_register:
1808         asus->hotplug_slot.ops = NULL;
1809         destroy_workqueue(asus->hotplug_workqueue);
1810 error_workqueue:
1811         return ret;
1812 }
1813
1814 /*
1815  * Rfkill devices
1816  */
1817 static int asus_rfkill_set(void *data, bool blocked)
1818 {
1819         struct asus_rfkill *priv = data;
1820         u32 ctrl_param = !blocked;
1821         u32 dev_id = priv->dev_id;
1822
1823         /*
1824          * If the user bit is set, BIOS can't set and record the wlan status,
1825          * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1826          * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1827          * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1828          * while setting the wlan status through WMI.
1829          * This is also the behavior that windows app will do.
1830          */
1831         if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1832              priv->asus->driver->wlan_ctrl_by_user)
1833                 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1834
1835         return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1836 }
1837
1838 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1839 {
1840         struct asus_rfkill *priv = data;
1841         int result;
1842
1843         result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1844
1845         if (result < 0)
1846                 return;
1847
1848         rfkill_set_sw_state(priv->rfkill, !result);
1849 }
1850
1851 static int asus_rfkill_wlan_set(void *data, bool blocked)
1852 {
1853         struct asus_rfkill *priv = data;
1854         struct asus_wmi *asus = priv->asus;
1855         int ret;
1856
1857         /*
1858          * This handler is enabled only if hotplug is enabled.
1859          * In this case, the asus_wmi_set_devstate() will
1860          * trigger a wmi notification and we need to wait
1861          * this call to finish before being able to call
1862          * any wmi method
1863          */
1864         mutex_lock(&asus->wmi_lock);
1865         ret = asus_rfkill_set(data, blocked);
1866         mutex_unlock(&asus->wmi_lock);
1867         return ret;
1868 }
1869
1870 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1871         .set_block = asus_rfkill_wlan_set,
1872         .query = asus_rfkill_query,
1873 };
1874
1875 static const struct rfkill_ops asus_rfkill_ops = {
1876         .set_block = asus_rfkill_set,
1877         .query = asus_rfkill_query,
1878 };
1879
1880 static int asus_new_rfkill(struct asus_wmi *asus,
1881                            struct asus_rfkill *arfkill,
1882                            const char *name, enum rfkill_type type, int dev_id)
1883 {
1884         int result = asus_wmi_get_devstate_simple(asus, dev_id);
1885         struct rfkill **rfkill = &arfkill->rfkill;
1886
1887         if (result < 0)
1888                 return result;
1889
1890         arfkill->dev_id = dev_id;
1891         arfkill->asus = asus;
1892
1893         if (dev_id == ASUS_WMI_DEVID_WLAN &&
1894             asus->driver->quirks->hotplug_wireless)
1895                 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1896                                        &asus_rfkill_wlan_ops, arfkill);
1897         else
1898                 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1899                                        &asus_rfkill_ops, arfkill);
1900
1901         if (!*rfkill)
1902                 return -EINVAL;
1903
1904         if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1905                         (asus->driver->quirks->wapf > 0))
1906                 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1907
1908         rfkill_init_sw_state(*rfkill, !result);
1909         result = rfkill_register(*rfkill);
1910         if (result) {
1911                 rfkill_destroy(*rfkill);
1912                 *rfkill = NULL;
1913                 return result;
1914         }
1915         return 0;
1916 }
1917
1918 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1919 {
1920         if (asus->driver->wlan_ctrl_by_user && ashs_present())
1921                 return;
1922
1923         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1924         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1925         asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1926         if (asus->wlan.rfkill) {
1927                 rfkill_unregister(asus->wlan.rfkill);
1928                 rfkill_destroy(asus->wlan.rfkill);
1929                 asus->wlan.rfkill = NULL;
1930         }
1931         /*
1932          * Refresh pci hotplug in case the rfkill state was changed after
1933          * asus_unregister_rfkill_notifier()
1934          */
1935         asus_rfkill_hotplug(asus);
1936         if (asus->hotplug_slot.ops)
1937                 pci_hp_deregister(&asus->hotplug_slot);
1938         if (asus->hotplug_workqueue)
1939                 destroy_workqueue(asus->hotplug_workqueue);
1940
1941         if (asus->bluetooth.rfkill) {
1942                 rfkill_unregister(asus->bluetooth.rfkill);
1943                 rfkill_destroy(asus->bluetooth.rfkill);
1944                 asus->bluetooth.rfkill = NULL;
1945         }
1946         if (asus->wimax.rfkill) {
1947                 rfkill_unregister(asus->wimax.rfkill);
1948                 rfkill_destroy(asus->wimax.rfkill);
1949                 asus->wimax.rfkill = NULL;
1950         }
1951         if (asus->wwan3g.rfkill) {
1952                 rfkill_unregister(asus->wwan3g.rfkill);
1953                 rfkill_destroy(asus->wwan3g.rfkill);
1954                 asus->wwan3g.rfkill = NULL;
1955         }
1956         if (asus->gps.rfkill) {
1957                 rfkill_unregister(asus->gps.rfkill);
1958                 rfkill_destroy(asus->gps.rfkill);
1959                 asus->gps.rfkill = NULL;
1960         }
1961         if (asus->uwb.rfkill) {
1962                 rfkill_unregister(asus->uwb.rfkill);
1963                 rfkill_destroy(asus->uwb.rfkill);
1964                 asus->uwb.rfkill = NULL;
1965         }
1966 }
1967
1968 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1969 {
1970         int result = 0;
1971
1972         mutex_init(&asus->hotplug_lock);
1973         mutex_init(&asus->wmi_lock);
1974
1975         result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1976                                  RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1977
1978         if (result && result != -ENODEV)
1979                 goto exit;
1980
1981         result = asus_new_rfkill(asus, &asus->bluetooth,
1982                                  "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1983                                  ASUS_WMI_DEVID_BLUETOOTH);
1984
1985         if (result && result != -ENODEV)
1986                 goto exit;
1987
1988         result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1989                                  RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1990
1991         if (result && result != -ENODEV)
1992                 goto exit;
1993
1994         result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1995                                  RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1996
1997         if (result && result != -ENODEV)
1998                 goto exit;
1999
2000         result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
2001                                  RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
2002
2003         if (result && result != -ENODEV)
2004                 goto exit;
2005
2006         result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
2007                                  RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
2008
2009         if (result && result != -ENODEV)
2010                 goto exit;
2011
2012         if (!asus->driver->quirks->hotplug_wireless)
2013                 goto exit;
2014
2015         result = asus_setup_pci_hotplug(asus);
2016         /*
2017          * If we get -EBUSY then something else is handling the PCI hotplug -
2018          * don't fail in this case
2019          */
2020         if (result == -EBUSY)
2021                 result = 0;
2022
2023         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
2024         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
2025         asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
2026         /*
2027          * Refresh pci hotplug in case the rfkill state was changed during
2028          * setup.
2029          */
2030         asus_rfkill_hotplug(asus);
2031
2032 exit:
2033         if (result && result != -ENODEV)
2034                 asus_wmi_rfkill_exit(asus);
2035
2036         if (result == -ENODEV)
2037                 result = 0;
2038
2039         return result;
2040 }
2041
2042 /* Panel Overdrive ************************************************************/
2043 static ssize_t panel_od_show(struct device *dev,
2044                                    struct device_attribute *attr, char *buf)
2045 {
2046         struct asus_wmi *asus = dev_get_drvdata(dev);
2047         int result;
2048
2049         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
2050         if (result < 0)
2051                 return result;
2052
2053         return sysfs_emit(buf, "%d\n", result);
2054 }
2055
2056 static ssize_t panel_od_store(struct device *dev,
2057                                     struct device_attribute *attr,
2058                                     const char *buf, size_t count)
2059 {
2060         int result, err;
2061         u32 overdrive;
2062
2063         struct asus_wmi *asus = dev_get_drvdata(dev);
2064
2065         result = kstrtou32(buf, 10, &overdrive);
2066         if (result)
2067                 return result;
2068
2069         if (overdrive > 1)
2070                 return -EINVAL;
2071
2072         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
2073
2074         if (err) {
2075                 pr_warn("Failed to set panel overdrive: %d\n", err);
2076                 return err;
2077         }
2078
2079         if (result > 1) {
2080                 pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
2081                 return -EIO;
2082         }
2083
2084         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
2085
2086         return count;
2087 }
2088 static DEVICE_ATTR_RW(panel_od);
2089
2090 /* Mini-LED mode **************************************************************/
2091 static ssize_t mini_led_mode_show(struct device *dev,
2092                                    struct device_attribute *attr, char *buf)
2093 {
2094         struct asus_wmi *asus = dev_get_drvdata(dev);
2095         int result;
2096
2097         result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
2098         if (result < 0)
2099                 return result;
2100
2101         return sysfs_emit(buf, "%d\n", result);
2102 }
2103
2104 static ssize_t mini_led_mode_store(struct device *dev,
2105                                     struct device_attribute *attr,
2106                                     const char *buf, size_t count)
2107 {
2108         int result, err;
2109         u32 mode;
2110
2111         struct asus_wmi *asus = dev_get_drvdata(dev);
2112
2113         result = kstrtou32(buf, 10, &mode);
2114         if (result)
2115                 return result;
2116
2117         if (mode > 1)
2118                 return -EINVAL;
2119
2120         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MINI_LED_MODE, mode, &result);
2121
2122         if (err) {
2123                 pr_warn("Failed to set mini-LED: %d\n", err);
2124                 return err;
2125         }
2126
2127         if (result > 1) {
2128                 pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result);
2129                 return -EIO;
2130         }
2131
2132         sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode");
2133
2134         return count;
2135 }
2136 static DEVICE_ATTR_RW(mini_led_mode);
2137
2138 /* Quirks *********************************************************************/
2139
2140 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
2141 {
2142         struct pci_dev *xhci_pdev;
2143         u32 orig_ports_available;
2144         u32 ports_available = asus->driver->quirks->xusb2pr;
2145
2146         xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2147                         PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
2148                         NULL);
2149
2150         if (!xhci_pdev)
2151                 return;
2152
2153         pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2154                                 &orig_ports_available);
2155
2156         pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2157                                 cpu_to_le32(ports_available));
2158
2159         pci_dev_put(xhci_pdev);
2160
2161         pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
2162                         orig_ports_available, ports_available);
2163 }
2164
2165 /*
2166  * Some devices dont support or have borcken get_als method
2167  * but still support set method.
2168  */
2169 static void asus_wmi_set_als(void)
2170 {
2171         asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
2172 }
2173
2174 /* Hwmon device ***************************************************************/
2175
2176 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
2177                                           int *speed)
2178 {
2179         struct agfn_fan_args args = {
2180                 .agfn.len = sizeof(args),
2181                 .agfn.mfun = ASUS_FAN_MFUN,
2182                 .agfn.sfun = ASUS_FAN_SFUN_READ,
2183                 .fan = fan,
2184                 .speed = 0,
2185         };
2186         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2187         int status;
2188
2189         if (fan != 1)
2190                 return -EINVAL;
2191
2192         status = asus_wmi_evaluate_method_agfn(input);
2193
2194         if (status || args.agfn.err)
2195                 return -ENXIO;
2196
2197         if (speed)
2198                 *speed = args.speed;
2199
2200         return 0;
2201 }
2202
2203 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
2204                                      int *speed)
2205 {
2206         struct agfn_fan_args args = {
2207                 .agfn.len = sizeof(args),
2208                 .agfn.mfun = ASUS_FAN_MFUN,
2209                 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
2210                 .fan = fan,
2211                 .speed = speed ?  *speed : 0,
2212         };
2213         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2214         int status;
2215
2216         /* 1: for setting 1st fan's speed 0: setting auto mode */
2217         if (fan != 1 && fan != 0)
2218                 return -EINVAL;
2219
2220         status = asus_wmi_evaluate_method_agfn(input);
2221
2222         if (status || args.agfn.err)
2223                 return -ENXIO;
2224
2225         if (speed && fan == 1)
2226                 asus->agfn_pwm = *speed;
2227
2228         return 0;
2229 }
2230
2231 /*
2232  * Check if we can read the speed of one fan. If true we assume we can also
2233  * control it.
2234  */
2235 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
2236 {
2237         int status;
2238         int speed;
2239         u32 value;
2240
2241         status = asus_agfn_fan_speed_read(asus, 1, &speed);
2242         if (status != 0)
2243                 return false;
2244
2245         status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2246         if (status != 0)
2247                 return false;
2248
2249         /*
2250          * We need to find a better way, probably using sfun,
2251          * bits or spec ...
2252          * Currently we disable it if:
2253          * - ASUS_WMI_UNSUPPORTED_METHOD is returned
2254          * - reverved bits are non-zero
2255          * - sfun and presence bit are not set
2256          */
2257         return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
2258                  || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
2259 }
2260
2261 static int asus_fan_set_auto(struct asus_wmi *asus)
2262 {
2263         int status;
2264         u32 retval;
2265
2266         switch (asus->fan_type) {
2267         case FAN_TYPE_SPEC83:
2268                 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2269                                                0, &retval);
2270                 if (status)
2271                         return status;
2272
2273                 if (retval != 1)
2274                         return -EIO;
2275                 break;
2276
2277         case FAN_TYPE_AGFN:
2278                 status = asus_agfn_fan_speed_write(asus, 0, NULL);
2279                 if (status)
2280                         return -ENXIO;
2281                 break;
2282
2283         default:
2284                 return -ENXIO;
2285         }
2286
2287         /*
2288          * Modern models like the G713 also have GPU fan control (this is not AGFN)
2289          */
2290         if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
2291                 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2292                                                0, &retval);
2293                 if (status)
2294                         return status;
2295
2296                 if (retval != 1)
2297                         return -EIO;
2298         }
2299
2300         return 0;
2301 }
2302
2303 static ssize_t pwm1_show(struct device *dev,
2304                                struct device_attribute *attr,
2305                                char *buf)
2306 {
2307         struct asus_wmi *asus = dev_get_drvdata(dev);
2308         int err;
2309         int value;
2310
2311         /* If we already set a value then just return it */
2312         if (asus->agfn_pwm >= 0)
2313                 return sprintf(buf, "%d\n", asus->agfn_pwm);
2314
2315         /*
2316          * If we haven't set already set a value through the AGFN interface,
2317          * we read a current value through the (now-deprecated) FAN_CTRL device.
2318          */
2319         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2320         if (err < 0)
2321                 return err;
2322
2323         value &= 0xFF;
2324
2325         if (value == 1) /* Low Speed */
2326                 value = 85;
2327         else if (value == 2)
2328                 value = 170;
2329         else if (value == 3)
2330                 value = 255;
2331         else if (value) {
2332                 pr_err("Unknown fan speed %#x\n", value);
2333                 value = -1;
2334         }
2335
2336         return sysfs_emit(buf, "%d\n", value);
2337 }
2338
2339 static ssize_t pwm1_store(struct device *dev,
2340                                      struct device_attribute *attr,
2341                                      const char *buf, size_t count) {
2342         struct asus_wmi *asus = dev_get_drvdata(dev);
2343         int value;
2344         int state;
2345         int ret;
2346
2347         ret = kstrtouint(buf, 10, &value);
2348         if (ret)
2349                 return ret;
2350
2351         value = clamp(value, 0, 255);
2352
2353         state = asus_agfn_fan_speed_write(asus, 1, &value);
2354         if (state)
2355                 pr_warn("Setting fan speed failed: %d\n", state);
2356         else
2357                 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
2358
2359         return count;
2360 }
2361
2362 static ssize_t fan1_input_show(struct device *dev,
2363                                         struct device_attribute *attr,
2364                                         char *buf)
2365 {
2366         struct asus_wmi *asus = dev_get_drvdata(dev);
2367         int value;
2368         int ret;
2369
2370         switch (asus->fan_type) {
2371         case FAN_TYPE_SPEC83:
2372                 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
2373                                             &value);
2374                 if (ret < 0)
2375                         return ret;
2376
2377                 value &= 0xffff;
2378                 break;
2379
2380         case FAN_TYPE_AGFN:
2381                 /* no speed readable on manual mode */
2382                 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
2383                         return -ENXIO;
2384
2385                 ret = asus_agfn_fan_speed_read(asus, 1, &value);
2386                 if (ret) {
2387                         pr_warn("reading fan speed failed: %d\n", ret);
2388                         return -ENXIO;
2389                 }
2390                 break;
2391
2392         default:
2393                 return -ENXIO;
2394         }
2395
2396         return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
2397 }
2398
2399 static ssize_t pwm1_enable_show(struct device *dev,
2400                                                  struct device_attribute *attr,
2401                                                  char *buf)
2402 {
2403         struct asus_wmi *asus = dev_get_drvdata(dev);
2404
2405         /*
2406          * Just read back the cached pwm mode.
2407          *
2408          * For the CPU_FAN device, the spec indicates that we should be
2409          * able to read the device status and consult bit 19 to see if we
2410          * are in Full On or Automatic mode. However, this does not work
2411          * in practice on X532FL at least (the bit is always 0) and there's
2412          * also nothing in the DSDT to indicate that this behaviour exists.
2413          */
2414         return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
2415 }
2416
2417 static ssize_t pwm1_enable_store(struct device *dev,
2418                                                   struct device_attribute *attr,
2419                                                   const char *buf, size_t count)
2420 {
2421         struct asus_wmi *asus = dev_get_drvdata(dev);
2422         int status = 0;
2423         int state;
2424         int value;
2425         int ret;
2426         u32 retval;
2427
2428         ret = kstrtouint(buf, 10, &state);
2429         if (ret)
2430                 return ret;
2431
2432         if (asus->fan_type == FAN_TYPE_SPEC83) {
2433                 switch (state) { /* standard documented hwmon values */
2434                 case ASUS_FAN_CTRL_FULLSPEED:
2435                         value = 1;
2436                         break;
2437                 case ASUS_FAN_CTRL_AUTO:
2438                         value = 0;
2439                         break;
2440                 default:
2441                         return -EINVAL;
2442                 }
2443
2444                 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2445                                             value, &retval);
2446                 if (ret)
2447                         return ret;
2448
2449                 if (retval != 1)
2450                         return -EIO;
2451         } else if (asus->fan_type == FAN_TYPE_AGFN) {
2452                 switch (state) {
2453                 case ASUS_FAN_CTRL_MANUAL:
2454                         break;
2455
2456                 case ASUS_FAN_CTRL_AUTO:
2457                         status = asus_fan_set_auto(asus);
2458                         if (status)
2459                                 return status;
2460                         break;
2461
2462                 default:
2463                         return -EINVAL;
2464                 }
2465         }
2466
2467         asus->fan_pwm_mode = state;
2468
2469         /* Must set to disabled if mode is toggled */
2470         if (asus->cpu_fan_curve_available)
2471                 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2472         if (asus->gpu_fan_curve_available)
2473                 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2474         if (asus->mid_fan_curve_available)
2475                 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
2476
2477         return count;
2478 }
2479
2480 static ssize_t fan1_label_show(struct device *dev,
2481                                           struct device_attribute *attr,
2482                                           char *buf)
2483 {
2484         return sysfs_emit(buf, "%s\n", ASUS_FAN_DESC);
2485 }
2486
2487 static ssize_t asus_hwmon_temp1(struct device *dev,
2488                                 struct device_attribute *attr,
2489                                 char *buf)
2490 {
2491         struct asus_wmi *asus = dev_get_drvdata(dev);
2492         u32 value;
2493         int err;
2494
2495         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
2496         if (err < 0)
2497                 return err;
2498
2499         return sprintf(buf, "%ld\n",
2500                        deci_kelvin_to_millicelsius(value & 0xFFFF));
2501 }
2502
2503 /* GPU fan on modern ROG laptops */
2504 static ssize_t fan2_input_show(struct device *dev,
2505                                         struct device_attribute *attr,
2506                                         char *buf)
2507 {
2508         struct asus_wmi *asus = dev_get_drvdata(dev);
2509         int value;
2510         int ret;
2511
2512         ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
2513         if (ret < 0)
2514                 return ret;
2515
2516         value &= 0xffff;
2517
2518         return sysfs_emit(buf, "%d\n", value * 100);
2519 }
2520
2521 static ssize_t fan2_label_show(struct device *dev,
2522                                           struct device_attribute *attr,
2523                                           char *buf)
2524 {
2525         return sysfs_emit(buf, "%s\n", ASUS_GPU_FAN_DESC);
2526 }
2527
2528 /* Middle/Center fan on modern ROG laptops */
2529 static ssize_t fan3_input_show(struct device *dev,
2530                                         struct device_attribute *attr,
2531                                         char *buf)
2532 {
2533         struct asus_wmi *asus = dev_get_drvdata(dev);
2534         int value;
2535         int ret;
2536
2537         ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value);
2538         if (ret < 0)
2539                 return ret;
2540
2541         value &= 0xffff;
2542
2543         return sysfs_emit(buf, "%d\n", value * 100);
2544 }
2545
2546 static ssize_t fan3_label_show(struct device *dev,
2547                                           struct device_attribute *attr,
2548                                           char *buf)
2549 {
2550         return sysfs_emit(buf, "%s\n", ASUS_MID_FAN_DESC);
2551 }
2552
2553 static ssize_t pwm2_enable_show(struct device *dev,
2554                                 struct device_attribute *attr,
2555                                 char *buf)
2556 {
2557         struct asus_wmi *asus = dev_get_drvdata(dev);
2558
2559         return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
2560 }
2561
2562 static ssize_t pwm2_enable_store(struct device *dev,
2563                                  struct device_attribute *attr,
2564                                  const char *buf, size_t count)
2565 {
2566         struct asus_wmi *asus = dev_get_drvdata(dev);
2567         int state;
2568         int value;
2569         int ret;
2570         u32 retval;
2571
2572         ret = kstrtouint(buf, 10, &state);
2573         if (ret)
2574                 return ret;
2575
2576         switch (state) { /* standard documented hwmon values */
2577         case ASUS_FAN_CTRL_FULLSPEED:
2578                 value = 1;
2579                 break;
2580         case ASUS_FAN_CTRL_AUTO:
2581                 value = 0;
2582                 break;
2583         default:
2584                 return -EINVAL;
2585         }
2586
2587         ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2588                                     value, &retval);
2589         if (ret)
2590                 return ret;
2591
2592         if (retval != 1)
2593                 return -EIO;
2594
2595         asus->gpu_fan_pwm_mode = state;
2596         return count;
2597 }
2598
2599 static ssize_t pwm3_enable_show(struct device *dev,
2600                                 struct device_attribute *attr,
2601                                 char *buf)
2602 {
2603         struct asus_wmi *asus = dev_get_drvdata(dev);
2604
2605         return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode);
2606 }
2607
2608 static ssize_t pwm3_enable_store(struct device *dev,
2609                                  struct device_attribute *attr,
2610                                  const char *buf, size_t count)
2611 {
2612         struct asus_wmi *asus = dev_get_drvdata(dev);
2613         int state;
2614         int value;
2615         int ret;
2616         u32 retval;
2617
2618         ret = kstrtouint(buf, 10, &state);
2619         if (ret)
2620                 return ret;
2621
2622         switch (state) { /* standard documented hwmon values */
2623         case ASUS_FAN_CTRL_FULLSPEED:
2624                 value = 1;
2625                 break;
2626         case ASUS_FAN_CTRL_AUTO:
2627                 value = 0;
2628                 break;
2629         default:
2630                 return -EINVAL;
2631         }
2632
2633         ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL,
2634                                     value, &retval);
2635         if (ret)
2636                 return ret;
2637
2638         if (retval != 1)
2639                 return -EIO;
2640
2641         asus->mid_fan_pwm_mode = state;
2642         return count;
2643 }
2644
2645 /* Fan1 */
2646 static DEVICE_ATTR_RW(pwm1);
2647 static DEVICE_ATTR_RW(pwm1_enable);
2648 static DEVICE_ATTR_RO(fan1_input);
2649 static DEVICE_ATTR_RO(fan1_label);
2650 /* Fan2 - GPU fan */
2651 static DEVICE_ATTR_RW(pwm2_enable);
2652 static DEVICE_ATTR_RO(fan2_input);
2653 static DEVICE_ATTR_RO(fan2_label);
2654 /* Fan3 - Middle/center fan */
2655 static DEVICE_ATTR_RW(pwm3_enable);
2656 static DEVICE_ATTR_RO(fan3_input);
2657 static DEVICE_ATTR_RO(fan3_label);
2658
2659 /* Temperature */
2660 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
2661
2662 static struct attribute *hwmon_attributes[] = {
2663         &dev_attr_pwm1.attr,
2664         &dev_attr_pwm1_enable.attr,
2665         &dev_attr_pwm2_enable.attr,
2666         &dev_attr_pwm3_enable.attr,
2667         &dev_attr_fan1_input.attr,
2668         &dev_attr_fan1_label.attr,
2669         &dev_attr_fan2_input.attr,
2670         &dev_attr_fan2_label.attr,
2671         &dev_attr_fan3_input.attr,
2672         &dev_attr_fan3_label.attr,
2673
2674         &dev_attr_temp1_input.attr,
2675         NULL
2676 };
2677
2678 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2679                                           struct attribute *attr, int idx)
2680 {
2681         struct device *dev = kobj_to_dev(kobj);
2682         struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2683         u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2684
2685         if (attr == &dev_attr_pwm1.attr) {
2686                 if (asus->fan_type != FAN_TYPE_AGFN)
2687                         return 0;
2688         } else if (attr == &dev_attr_fan1_input.attr
2689             || attr == &dev_attr_fan1_label.attr
2690             || attr == &dev_attr_pwm1_enable.attr) {
2691                 if (asus->fan_type == FAN_TYPE_NONE)
2692                         return 0;
2693         } else if (attr == &dev_attr_fan2_input.attr
2694             || attr == &dev_attr_fan2_label.attr
2695             || attr == &dev_attr_pwm2_enable.attr) {
2696                 if (asus->gpu_fan_type == FAN_TYPE_NONE)
2697                         return 0;
2698         } else if (attr == &dev_attr_fan3_input.attr
2699             || attr == &dev_attr_fan3_label.attr
2700             || attr == &dev_attr_pwm3_enable.attr) {
2701                 if (asus->mid_fan_type == FAN_TYPE_NONE)
2702                         return 0;
2703         } else if (attr == &dev_attr_temp1_input.attr) {
2704                 int err = asus_wmi_get_devstate(asus,
2705                                                 ASUS_WMI_DEVID_THERMAL_CTRL,
2706                                                 &value);
2707
2708                 if (err < 0)
2709                         return 0; /* can't return negative here */
2710
2711                 /*
2712                  * If the temperature value in deci-Kelvin is near the absolute
2713                  * zero temperature, something is clearly wrong
2714                  */
2715                 if (value == 0 || value == 1)
2716                         return 0;
2717         }
2718
2719         return attr->mode;
2720 }
2721
2722 static const struct attribute_group hwmon_attribute_group = {
2723         .is_visible = asus_hwmon_sysfs_is_visible,
2724         .attrs = hwmon_attributes
2725 };
2726 __ATTRIBUTE_GROUPS(hwmon_attribute);
2727
2728 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2729 {
2730         struct device *dev = &asus->platform_device->dev;
2731         struct device *hwmon;
2732
2733         hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2734                         hwmon_attribute_groups);
2735
2736         if (IS_ERR(hwmon)) {
2737                 pr_err("Could not register asus hwmon device\n");
2738                 return PTR_ERR(hwmon);
2739         }
2740         return 0;
2741 }
2742
2743 static int asus_wmi_fan_init(struct asus_wmi *asus)
2744 {
2745         asus->gpu_fan_type = FAN_TYPE_NONE;
2746         asus->mid_fan_type = FAN_TYPE_NONE;
2747         asus->fan_type = FAN_TYPE_NONE;
2748         asus->agfn_pwm = -1;
2749
2750         if (asus->driver->quirks->wmi_ignore_fan)
2751                 asus->fan_type = FAN_TYPE_NONE;
2752         else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2753                 asus->fan_type = FAN_TYPE_SPEC83;
2754         else if (asus_wmi_has_agfn_fan(asus))
2755                 asus->fan_type = FAN_TYPE_AGFN;
2756
2757         /*  Modern models like G713 also have GPU fan control */
2758         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
2759                 asus->gpu_fan_type = FAN_TYPE_SPEC83;
2760
2761         /* Some models also have a center/middle fan */
2762         if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL))
2763                 asus->mid_fan_type = FAN_TYPE_SPEC83;
2764
2765         if (asus->fan_type == FAN_TYPE_NONE)
2766                 return -ENODEV;
2767
2768         asus_fan_set_auto(asus);
2769         asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2770         return 0;
2771 }
2772
2773 /* Fan mode *******************************************************************/
2774
2775 static int fan_boost_mode_check_present(struct asus_wmi *asus)
2776 {
2777         u32 result;
2778         int err;
2779
2780         asus->fan_boost_mode_available = false;
2781
2782         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2783                                     &result);
2784         if (err) {
2785                 if (err == -ENODEV)
2786                         return 0;
2787                 else
2788                         return err;
2789         }
2790
2791         if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2792                         (result & ASUS_FAN_BOOST_MODES_MASK)) {
2793                 asus->fan_boost_mode_available = true;
2794                 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2795         }
2796
2797         return 0;
2798 }
2799
2800 static int fan_boost_mode_write(struct asus_wmi *asus)
2801 {
2802         u32 retval;
2803         u8 value;
2804         int err;
2805
2806         value = asus->fan_boost_mode;
2807
2808         pr_info("Set fan boost mode: %u\n", value);
2809         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2810                                     &retval);
2811
2812         sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2813                         "fan_boost_mode");
2814
2815         if (err) {
2816                 pr_warn("Failed to set fan boost mode: %d\n", err);
2817                 return err;
2818         }
2819
2820         if (retval != 1) {
2821                 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2822                         retval);
2823                 return -EIO;
2824         }
2825
2826         return 0;
2827 }
2828
2829 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2830 {
2831         u8 mask = asus->fan_boost_mode_mask;
2832
2833         if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2834                 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2835                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2836                 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2837                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2838         } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2839                 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2840                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2841                 else
2842                         asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2843         } else {
2844                 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2845         }
2846
2847         return fan_boost_mode_write(asus);
2848 }
2849
2850 static ssize_t fan_boost_mode_show(struct device *dev,
2851                                    struct device_attribute *attr, char *buf)
2852 {
2853         struct asus_wmi *asus = dev_get_drvdata(dev);
2854
2855         return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
2856 }
2857
2858 static ssize_t fan_boost_mode_store(struct device *dev,
2859                                     struct device_attribute *attr,
2860                                     const char *buf, size_t count)
2861 {
2862         struct asus_wmi *asus = dev_get_drvdata(dev);
2863         u8 mask = asus->fan_boost_mode_mask;
2864         u8 new_mode;
2865         int result;
2866
2867         result = kstrtou8(buf, 10, &new_mode);
2868         if (result < 0) {
2869                 pr_warn("Trying to store invalid value\n");
2870                 return result;
2871         }
2872
2873         if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2874                 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2875                         return -EINVAL;
2876         } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2877                 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2878                         return -EINVAL;
2879         } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2880                 return -EINVAL;
2881         }
2882
2883         asus->fan_boost_mode = new_mode;
2884         fan_boost_mode_write(asus);
2885
2886         return count;
2887 }
2888
2889 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2890 static DEVICE_ATTR_RW(fan_boost_mode);
2891
2892 /* Custom fan curves **********************************************************/
2893
2894 static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2895 {
2896         int i;
2897
2898         for (i = 0; i < FAN_CURVE_POINTS; i++) {
2899                 data->temps[i] = buf[i];
2900         }
2901
2902         for (i = 0; i < FAN_CURVE_POINTS; i++) {
2903                 data->percents[i] =
2904                         255 * buf[i + FAN_CURVE_POINTS] / 100;
2905         }
2906 }
2907
2908 static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2909 {
2910         struct fan_curve_data *curves;
2911         u8 buf[FAN_CURVE_BUF_LEN];
2912         int err, fan_idx;
2913         u8 mode = 0;
2914
2915         if (asus->throttle_thermal_policy_available)
2916                 mode = asus->throttle_thermal_policy_mode;
2917         /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2918         if (mode == 2)
2919                 mode = 1;
2920         else if (mode == 1)
2921                 mode = 2;
2922
2923         err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2924                                            FAN_CURVE_BUF_LEN);
2925         if (err) {
2926                 pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
2927                 return err;
2928         }
2929
2930         fan_idx = FAN_CURVE_DEV_CPU;
2931         if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2932                 fan_idx = FAN_CURVE_DEV_GPU;
2933
2934         if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE)
2935                 fan_idx = FAN_CURVE_DEV_MID;
2936
2937         curves = &asus->custom_fan_curves[fan_idx];
2938         curves->device_id = fan_dev;
2939
2940         fan_curve_copy_from_buf(curves, buf);
2941         return 0;
2942 }
2943
2944 /* Check if capability exists, and populate defaults */
2945 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2946                                    u32 fan_dev)
2947 {
2948         int err;
2949
2950         *available = false;
2951
2952         if (asus->fan_type == FAN_TYPE_NONE)
2953                 return 0;
2954
2955         err = fan_curve_get_factory_default(asus, fan_dev);
2956         if (err) {
2957                 return 0;
2958         }
2959
2960         *available = true;
2961         return 0;
2962 }
2963
2964 /* Determine which fan the attribute is for if SENSOR_ATTR */
2965 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2966                                               struct device_attribute *attr)
2967 {
2968         int index = to_sensor_dev_attr(attr)->index;
2969
2970         return &asus->custom_fan_curves[index];
2971 }
2972
2973 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2974 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2975                                             struct device_attribute *attr)
2976 {
2977         int nr = to_sensor_dev_attr_2(attr)->nr;
2978
2979         return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK];
2980 }
2981
2982 static ssize_t fan_curve_show(struct device *dev,
2983                               struct device_attribute *attr, char *buf)
2984 {
2985         struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2986         struct asus_wmi *asus = dev_get_drvdata(dev);
2987         struct fan_curve_data *data;
2988         int value, pwm, index;
2989
2990         data = fan_curve_attr_2_select(asus, attr);
2991         pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2992         index = dev_attr->index;
2993
2994         if (pwm)
2995                 value = data->percents[index];
2996         else
2997                 value = data->temps[index];
2998
2999         return sysfs_emit(buf, "%d\n", value);
3000 }
3001
3002 /*
3003  * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
3004  */
3005 static int fan_curve_write(struct asus_wmi *asus,
3006                            struct fan_curve_data *data)
3007 {
3008         u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
3009         u8 *percents = data->percents;
3010         u8 *temps = data->temps;
3011         int ret, i, shift = 0;
3012
3013         if (!data->enabled)
3014                 return 0;
3015
3016         for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
3017                 arg1 += (temps[i]) << shift;
3018                 arg2 += (temps[i + 4]) << shift;
3019                 /* Scale to percentage for device */
3020                 arg3 += (100 * percents[i] / 255) << shift;
3021                 arg4 += (100 * percents[i + 4] / 255) << shift;
3022                 shift += 8;
3023         }
3024
3025         return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
3026                                          data->device_id,
3027                                          arg1, arg2, arg3, arg4, &ret);
3028 }
3029
3030 static ssize_t fan_curve_store(struct device *dev,
3031                                struct device_attribute *attr, const char *buf,
3032                                size_t count)
3033 {
3034         struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3035         struct asus_wmi *asus = dev_get_drvdata(dev);
3036         struct fan_curve_data *data;
3037         int err, pwm, index;
3038         u8 value;
3039
3040         data = fan_curve_attr_2_select(asus, attr);
3041         pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3042         index = dev_attr->index;
3043
3044         err = kstrtou8(buf, 10, &value);
3045         if (err < 0)
3046                 return err;
3047
3048         if (pwm)
3049                 data->percents[index] = value;
3050         else
3051                 data->temps[index] = value;
3052
3053         /*
3054          * Mark as disabled so the user has to explicitly enable to apply a
3055          * changed fan curve. This prevents potential lockups from writing out
3056          * many changes as one-write-per-change.
3057          */
3058         data->enabled = false;
3059
3060         return count;
3061 }
3062
3063 static ssize_t fan_curve_enable_show(struct device *dev,
3064                                      struct device_attribute *attr, char *buf)
3065 {
3066         struct asus_wmi *asus = dev_get_drvdata(dev);
3067         struct fan_curve_data *data;
3068         int out = 2;
3069
3070         data = fan_curve_attr_select(asus, attr);
3071
3072         if (data->enabled)
3073                 out = 1;
3074
3075         return sysfs_emit(buf, "%d\n", out);
3076 }
3077
3078 static ssize_t fan_curve_enable_store(struct device *dev,
3079                                       struct device_attribute *attr,
3080                                       const char *buf, size_t count)
3081 {
3082         struct asus_wmi *asus = dev_get_drvdata(dev);
3083         struct fan_curve_data *data;
3084         int value, err;
3085
3086         data = fan_curve_attr_select(asus, attr);
3087
3088         err = kstrtoint(buf, 10, &value);
3089         if (err < 0)
3090                 return err;
3091
3092         switch (value) {
3093         case 1:
3094                 data->enabled = true;
3095                 break;
3096         case 2:
3097                 data->enabled = false;
3098                 break;
3099         /*
3100          * Auto + reset the fan curve data to defaults. Make it an explicit
3101          * option so that users don't accidentally overwrite a set fan curve.
3102          */
3103         case 3:
3104                 err = fan_curve_get_factory_default(asus, data->device_id);
3105                 if (err)
3106                         return err;
3107                 data->enabled = false;
3108                 break;
3109         default:
3110                 return -EINVAL;
3111         }
3112
3113         if (data->enabled) {
3114                 err = fan_curve_write(asus, data);
3115                 if (err)
3116                         return err;
3117         } else {
3118                 /*
3119                  * For machines with throttle this is the only way to reset fans
3120                  * to default mode of operation (does not erase curve data).
3121                  */
3122                 if (asus->throttle_thermal_policy_available) {
3123                         err = throttle_thermal_policy_write(asus);
3124                         if (err)
3125                                 return err;
3126                 /* Similar is true for laptops with this fan */
3127                 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
3128                         err = asus_fan_set_auto(asus);
3129                         if (err)
3130                                 return err;
3131                 } else {
3132                         /* Safeguard against fautly ACPI tables */
3133                         err = fan_curve_get_factory_default(asus, data->device_id);
3134                         if (err)
3135                                 return err;
3136                         err = fan_curve_write(asus, data);
3137                         if (err)
3138                                 return err;
3139                 }
3140         }
3141         return count;
3142 }
3143
3144 /* CPU */
3145 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
3146 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
3147                                FAN_CURVE_DEV_CPU, 0);
3148 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
3149                                FAN_CURVE_DEV_CPU, 1);
3150 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
3151                                FAN_CURVE_DEV_CPU, 2);
3152 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
3153                                FAN_CURVE_DEV_CPU, 3);
3154 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
3155                                FAN_CURVE_DEV_CPU, 4);
3156 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
3157                                FAN_CURVE_DEV_CPU, 5);
3158 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
3159                                FAN_CURVE_DEV_CPU, 6);
3160 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
3161                                FAN_CURVE_DEV_CPU, 7);
3162
3163 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
3164                                 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
3165 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
3166                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
3167 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
3168                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
3169 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
3170                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
3171 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
3172                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
3173 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
3174                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
3175 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
3176                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
3177 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
3178                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
3179
3180 /* GPU */
3181 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
3182 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
3183                                FAN_CURVE_DEV_GPU, 0);
3184 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
3185                                FAN_CURVE_DEV_GPU, 1);
3186 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
3187                                FAN_CURVE_DEV_GPU, 2);
3188 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
3189                                FAN_CURVE_DEV_GPU, 3);
3190 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
3191                                FAN_CURVE_DEV_GPU, 4);
3192 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
3193                                FAN_CURVE_DEV_GPU, 5);
3194 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
3195                                FAN_CURVE_DEV_GPU, 6);
3196 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
3197                                FAN_CURVE_DEV_GPU, 7);
3198
3199 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
3200                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
3201 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
3202                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
3203 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
3204                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
3205 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
3206                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
3207 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
3208                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
3209 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
3210                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
3211 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
3212                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
3213 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
3214                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
3215
3216 /* MID */
3217 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID);
3218 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve,
3219                                FAN_CURVE_DEV_MID, 0);
3220 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve,
3221                                FAN_CURVE_DEV_MID, 1);
3222 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve,
3223                                FAN_CURVE_DEV_MID, 2);
3224 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve,
3225                                FAN_CURVE_DEV_MID, 3);
3226 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve,
3227                                FAN_CURVE_DEV_MID, 4);
3228 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve,
3229                                FAN_CURVE_DEV_MID, 5);
3230 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve,
3231                                FAN_CURVE_DEV_MID, 6);
3232 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve,
3233                                FAN_CURVE_DEV_MID, 7);
3234
3235 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve,
3236                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0);
3237 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve,
3238                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1);
3239 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve,
3240                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2);
3241 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve,
3242                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3);
3243 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve,
3244                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4);
3245 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve,
3246                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5);
3247 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve,
3248                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6);
3249 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve,
3250                                FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7);
3251
3252 static struct attribute *asus_fan_curve_attr[] = {
3253         /* CPU */
3254         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3255         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
3256         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
3257         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
3258         &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
3259         &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
3260         &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
3261         &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
3262         &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
3263         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
3264         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
3265         &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
3266         &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
3267         &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
3268         &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
3269         &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
3270         &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
3271         /* GPU */
3272         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
3273         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
3274         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
3275         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
3276         &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
3277         &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
3278         &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
3279         &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
3280         &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
3281         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
3282         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
3283         &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
3284         &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
3285         &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
3286         &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
3287         &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
3288         &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
3289         /* MID */
3290         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
3291         &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
3292         &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
3293         &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
3294         &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
3295         &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
3296         &sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr,
3297         &sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr,
3298         &sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr,
3299         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
3300         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
3301         &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
3302         &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
3303         &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
3304         &sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr,
3305         &sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr,
3306         &sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr,
3307         NULL
3308 };
3309
3310 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
3311                                          struct attribute *attr, int idx)
3312 {
3313         struct device *dev = kobj_to_dev(kobj);
3314         struct asus_wmi *asus = dev_get_drvdata(dev->parent);
3315
3316         /*
3317          * Check the char instead of casting attr as there are two attr types
3318          * involved here (attr1 and attr2)
3319          */
3320         if (asus->cpu_fan_curve_available && attr->name[3] == '1')
3321                 return 0644;
3322
3323         if (asus->gpu_fan_curve_available && attr->name[3] == '2')
3324                 return 0644;
3325
3326         if (asus->mid_fan_curve_available && attr->name[3] == '3')
3327                 return 0644;
3328
3329         return 0;
3330 }
3331
3332 static const struct attribute_group asus_fan_curve_attr_group = {
3333         .is_visible = asus_fan_curve_is_visible,
3334         .attrs = asus_fan_curve_attr,
3335 };
3336 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
3337
3338 /*
3339  * Must be initialised after throttle_thermal_policy_check_present() as
3340  * we check the status of throttle_thermal_policy_available during init.
3341  */
3342 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
3343 {
3344         struct device *dev = &asus->platform_device->dev;
3345         struct device *hwmon;
3346         int err;
3347
3348         err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
3349                                       ASUS_WMI_DEVID_CPU_FAN_CURVE);
3350         if (err)
3351                 return err;
3352
3353         err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
3354                                       ASUS_WMI_DEVID_GPU_FAN_CURVE);
3355         if (err)
3356                 return err;
3357
3358         err = fan_curve_check_present(asus, &asus->mid_fan_curve_available,
3359                                       ASUS_WMI_DEVID_MID_FAN_CURVE);
3360         if (err)
3361                 return err;
3362
3363         if (!asus->cpu_fan_curve_available
3364                 && !asus->gpu_fan_curve_available
3365                 && !asus->mid_fan_curve_available)
3366                 return 0;
3367
3368         hwmon = devm_hwmon_device_register_with_groups(
3369                 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
3370
3371         if (IS_ERR(hwmon)) {
3372                 dev_err(dev,
3373                         "Could not register asus_custom_fan_curve device\n");
3374                 return PTR_ERR(hwmon);
3375         }
3376
3377         return 0;
3378 }
3379
3380 /* Throttle thermal policy ****************************************************/
3381
3382 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
3383 {
3384         u32 result;
3385         int err;
3386
3387         asus->throttle_thermal_policy_available = false;
3388
3389         err = asus_wmi_get_devstate(asus,
3390                                     ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3391                                     &result);
3392         if (err) {
3393                 if (err == -ENODEV)
3394                         return 0;
3395                 return err;
3396         }
3397
3398         if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
3399                 asus->throttle_thermal_policy_available = true;
3400
3401         return 0;
3402 }
3403
3404 static int throttle_thermal_policy_write(struct asus_wmi *asus)
3405 {
3406         int err;
3407         u8 value;
3408         u32 retval;
3409
3410         value = asus->throttle_thermal_policy_mode;
3411
3412         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3413                                     value, &retval);
3414
3415         sysfs_notify(&asus->platform_device->dev.kobj, NULL,
3416                         "throttle_thermal_policy");
3417
3418         if (err) {
3419                 pr_warn("Failed to set throttle thermal policy: %d\n", err);
3420                 return err;
3421         }
3422
3423         if (retval != 1) {
3424                 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
3425                         retval);
3426                 return -EIO;
3427         }
3428
3429         /* Must set to disabled if mode is toggled */
3430         if (asus->cpu_fan_curve_available)
3431                 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
3432         if (asus->gpu_fan_curve_available)
3433                 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
3434         if (asus->mid_fan_curve_available)
3435                 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
3436
3437         return 0;
3438 }
3439
3440 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
3441 {
3442         if (!asus->throttle_thermal_policy_available)
3443                 return 0;
3444
3445         asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3446         return throttle_thermal_policy_write(asus);
3447 }
3448
3449 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
3450 {
3451         u8 new_mode = asus->throttle_thermal_policy_mode + 1;
3452         int err;
3453
3454         if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3455                 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3456
3457         asus->throttle_thermal_policy_mode = new_mode;
3458         err = throttle_thermal_policy_write(asus);
3459         if (err)
3460                 return err;
3461
3462         /*
3463          * Ensure that platform_profile updates userspace with the change to ensure
3464          * that platform_profile and throttle_thermal_policy_mode are in sync.
3465          */
3466         platform_profile_notify();
3467
3468         return 0;
3469 }
3470
3471 static ssize_t throttle_thermal_policy_show(struct device *dev,
3472                                    struct device_attribute *attr, char *buf)
3473 {
3474         struct asus_wmi *asus = dev_get_drvdata(dev);
3475         u8 mode = asus->throttle_thermal_policy_mode;
3476
3477         return sysfs_emit(buf, "%d\n", mode);
3478 }
3479
3480 static ssize_t throttle_thermal_policy_store(struct device *dev,
3481                                     struct device_attribute *attr,
3482                                     const char *buf, size_t count)
3483 {
3484         struct asus_wmi *asus = dev_get_drvdata(dev);
3485         u8 new_mode;
3486         int result;
3487         int err;
3488
3489         result = kstrtou8(buf, 10, &new_mode);
3490         if (result < 0)
3491                 return result;
3492
3493         if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3494                 return -EINVAL;
3495
3496         asus->throttle_thermal_policy_mode = new_mode;
3497         err = throttle_thermal_policy_write(asus);
3498         if (err)
3499                 return err;
3500
3501         /*
3502          * Ensure that platform_profile updates userspace with the change to ensure
3503          * that platform_profile and throttle_thermal_policy_mode are in sync.
3504          */
3505         platform_profile_notify();
3506
3507         return count;
3508 }
3509
3510 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
3511 static DEVICE_ATTR_RW(throttle_thermal_policy);
3512
3513 /* Platform profile ***********************************************************/
3514 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
3515                                         enum platform_profile_option *profile)
3516 {
3517         struct asus_wmi *asus;
3518         int tp;
3519
3520         asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3521
3522         tp = asus->throttle_thermal_policy_mode;
3523
3524         switch (tp) {
3525         case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
3526                 *profile = PLATFORM_PROFILE_BALANCED;
3527                 break;
3528         case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
3529                 *profile = PLATFORM_PROFILE_PERFORMANCE;
3530                 break;
3531         case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
3532                 *profile = PLATFORM_PROFILE_QUIET;
3533                 break;
3534         default:
3535                 return -EINVAL;
3536         }
3537
3538         return 0;
3539 }
3540
3541 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
3542                                         enum platform_profile_option profile)
3543 {
3544         struct asus_wmi *asus;
3545         int tp;
3546
3547         asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3548
3549         switch (profile) {
3550         case PLATFORM_PROFILE_PERFORMANCE:
3551                 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
3552                 break;
3553         case PLATFORM_PROFILE_BALANCED:
3554                 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3555                 break;
3556         case PLATFORM_PROFILE_QUIET:
3557                 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
3558                 break;
3559         default:
3560                 return -EOPNOTSUPP;
3561         }
3562
3563         asus->throttle_thermal_policy_mode = tp;
3564         return throttle_thermal_policy_write(asus);
3565 }
3566
3567 static int platform_profile_setup(struct asus_wmi *asus)
3568 {
3569         struct device *dev = &asus->platform_device->dev;
3570         int err;
3571
3572         /*
3573          * Not an error if a component platform_profile relies on is unavailable
3574          * so early return, skipping the setup of platform_profile.
3575          */
3576         if (!asus->throttle_thermal_policy_available)
3577                 return 0;
3578
3579         dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
3580
3581         asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
3582         asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
3583
3584         set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
3585         set_bit(PLATFORM_PROFILE_BALANCED,
3586                 asus->platform_profile_handler.choices);
3587         set_bit(PLATFORM_PROFILE_PERFORMANCE,
3588                 asus->platform_profile_handler.choices);
3589
3590         err = platform_profile_register(&asus->platform_profile_handler);
3591         if (err)
3592                 return err;
3593
3594         asus->platform_profile_support = true;
3595         return 0;
3596 }
3597
3598 /* Backlight ******************************************************************/
3599
3600 static int read_backlight_power(struct asus_wmi *asus)
3601 {
3602         int ret;
3603
3604         if (asus->driver->quirks->store_backlight_power)
3605                 ret = !asus->driver->panel_power;
3606         else
3607                 ret = asus_wmi_get_devstate_simple(asus,
3608                                                    ASUS_WMI_DEVID_BACKLIGHT);
3609
3610         if (ret < 0)
3611                 return ret;
3612
3613         return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3614 }
3615
3616 static int read_brightness_max(struct asus_wmi *asus)
3617 {
3618         u32 retval;
3619         int err;
3620
3621         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3622         if (err < 0)
3623                 return err;
3624
3625         retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
3626         retval >>= 8;
3627
3628         if (!retval)
3629                 return -ENODEV;
3630
3631         return retval;
3632 }
3633
3634 static int read_brightness(struct backlight_device *bd)
3635 {
3636         struct asus_wmi *asus = bl_get_data(bd);
3637         u32 retval;
3638         int err;
3639
3640         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3641         if (err < 0)
3642                 return err;
3643
3644         return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3645 }
3646
3647 static u32 get_scalar_command(struct backlight_device *bd)
3648 {
3649         struct asus_wmi *asus = bl_get_data(bd);
3650         u32 ctrl_param = 0;
3651
3652         if ((asus->driver->brightness < bd->props.brightness) ||
3653             bd->props.brightness == bd->props.max_brightness)
3654                 ctrl_param = 0x00008001;
3655         else if ((asus->driver->brightness > bd->props.brightness) ||
3656                  bd->props.brightness == 0)
3657                 ctrl_param = 0x00008000;
3658
3659         asus->driver->brightness = bd->props.brightness;
3660
3661         return ctrl_param;
3662 }
3663
3664 static int update_bl_status(struct backlight_device *bd)
3665 {
3666         struct asus_wmi *asus = bl_get_data(bd);
3667         u32 ctrl_param;
3668         int power, err = 0;
3669
3670         power = read_backlight_power(asus);
3671         if (power != -ENODEV && bd->props.power != power) {
3672                 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
3673                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
3674                                             ctrl_param, NULL);
3675                 if (asus->driver->quirks->store_backlight_power)
3676                         asus->driver->panel_power = bd->props.power;
3677
3678                 /* When using scalar brightness, updating the brightness
3679                  * will mess with the backlight power */
3680                 if (asus->driver->quirks->scalar_panel_brightness)
3681                         return err;
3682         }
3683
3684         if (asus->driver->quirks->scalar_panel_brightness)
3685                 ctrl_param = get_scalar_command(bd);
3686         else
3687                 ctrl_param = bd->props.brightness;
3688
3689         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
3690                                     ctrl_param, NULL);
3691
3692         return err;
3693 }
3694
3695 static const struct backlight_ops asus_wmi_bl_ops = {
3696         .get_brightness = read_brightness,
3697         .update_status = update_bl_status,
3698 };
3699
3700 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3701 {
3702         struct backlight_device *bd = asus->backlight_device;
3703         int old = bd->props.brightness;
3704         int new = old;
3705
3706         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3707                 new = code - NOTIFY_BRNUP_MIN + 1;
3708         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3709                 new = code - NOTIFY_BRNDOWN_MIN;
3710
3711         bd->props.brightness = new;
3712         backlight_update_status(bd);
3713         backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
3714
3715         return old;
3716 }
3717
3718 static int asus_wmi_backlight_init(struct asus_wmi *asus)
3719 {
3720         struct backlight_device *bd;
3721         struct backlight_properties props;
3722         int max;
3723         int power;
3724
3725         max = read_brightness_max(asus);
3726         if (max < 0)
3727                 return max;
3728
3729         power = read_backlight_power(asus);
3730         if (power == -ENODEV)
3731                 power = FB_BLANK_UNBLANK;
3732         else if (power < 0)
3733                 return power;
3734
3735         memset(&props, 0, sizeof(struct backlight_properties));
3736         props.type = BACKLIGHT_PLATFORM;
3737         props.max_brightness = max;
3738         bd = backlight_device_register(asus->driver->name,
3739                                        &asus->platform_device->dev, asus,
3740                                        &asus_wmi_bl_ops, &props);
3741         if (IS_ERR(bd)) {
3742                 pr_err("Could not register backlight device\n");
3743                 return PTR_ERR(bd);
3744         }
3745
3746         asus->backlight_device = bd;
3747
3748         if (asus->driver->quirks->store_backlight_power)
3749                 asus->driver->panel_power = power;
3750
3751         bd->props.brightness = read_brightness(bd);
3752         bd->props.power = power;
3753         backlight_update_status(bd);
3754
3755         asus->driver->brightness = bd->props.brightness;
3756
3757         return 0;
3758 }
3759
3760 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3761 {
3762         backlight_device_unregister(asus->backlight_device);
3763
3764         asus->backlight_device = NULL;
3765 }
3766
3767 static int is_display_toggle(int code)
3768 {
3769         /* display toggle keys */
3770         if ((code >= 0x61 && code <= 0x67) ||
3771             (code >= 0x8c && code <= 0x93) ||
3772             (code >= 0xa0 && code <= 0xa7) ||
3773             (code >= 0xd0 && code <= 0xd5))
3774                 return 1;
3775
3776         return 0;
3777 }
3778
3779 /* Fn-lock ********************************************************************/
3780
3781 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3782 {
3783         u32 result;
3784
3785         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3786
3787         return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3788                 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3789 }
3790
3791 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3792 {
3793         int mode = asus->fnlock_locked;
3794
3795         asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3796 }
3797
3798 /* WMI events *****************************************************************/
3799
3800 static int asus_wmi_get_event_code(u32 value)
3801 {
3802         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3803         union acpi_object *obj;
3804         acpi_status status;
3805         int code;
3806
3807         status = wmi_get_event_data(value, &response);
3808         if (ACPI_FAILURE(status)) {
3809                 pr_warn("Failed to get WMI notify code: %s\n",
3810                                 acpi_format_exception(status));
3811                 return -EIO;
3812         }
3813
3814         obj = (union acpi_object *)response.pointer;
3815
3816         if (obj && obj->type == ACPI_TYPE_INTEGER)
3817                 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3818         else
3819                 code = -EIO;
3820
3821         kfree(obj);
3822         return code;
3823 }
3824
3825 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3826 {
3827         unsigned int key_value = 1;
3828         bool autorelease = 1;
3829         int orig_code = code;
3830
3831         if (asus->driver->key_filter) {
3832                 asus->driver->key_filter(asus->driver, &code, &key_value,
3833                                          &autorelease);
3834                 if (code == ASUS_WMI_KEY_IGNORE)
3835                         return;
3836         }
3837
3838         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3839                 code = ASUS_WMI_BRN_UP;
3840         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3841                 code = ASUS_WMI_BRN_DOWN;
3842
3843         if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3844                 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3845                         asus_wmi_backlight_notify(asus, orig_code);
3846                         return;
3847                 }
3848         }
3849
3850         if (code == NOTIFY_KBD_BRTUP) {
3851                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3852                 return;
3853         }
3854         if (code == NOTIFY_KBD_BRTDWN) {
3855                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3856                 return;
3857         }
3858         if (code == NOTIFY_KBD_BRTTOGGLE) {
3859                 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3860                         kbd_led_set_by_kbd(asus, 0);
3861                 else
3862                         kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3863                 return;
3864         }
3865
3866         if (code == NOTIFY_FNLOCK_TOGGLE) {
3867                 asus->fnlock_locked = !asus->fnlock_locked;
3868                 asus_wmi_fnlock_update(asus);
3869                 return;
3870         }
3871
3872         if (code == asus->tablet_switch_event_code) {
3873                 asus_wmi_tablet_mode_get_state(asus);
3874                 return;
3875         }
3876
3877         if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
3878                 if (asus->fan_boost_mode_available)
3879                         fan_boost_mode_switch_next(asus);
3880                 if (asus->throttle_thermal_policy_available)
3881                         throttle_thermal_policy_switch_next(asus);
3882                 return;
3883
3884         }
3885
3886         if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3887                 return;
3888
3889         if (!sparse_keymap_report_event(asus->inputdev, code,
3890                                         key_value, autorelease))
3891                 pr_info("Unknown key code 0x%x\n", code);
3892 }
3893
3894 static void asus_wmi_notify(u32 value, void *context)
3895 {
3896         struct asus_wmi *asus = context;
3897         int code;
3898         int i;
3899
3900         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3901                 code = asus_wmi_get_event_code(value);
3902                 if (code < 0) {
3903                         pr_warn("Failed to get notify code: %d\n", code);
3904                         return;
3905                 }
3906
3907                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3908                         return;
3909
3910                 asus_wmi_handle_event_code(code, asus);
3911
3912                 /*
3913                  * Double check that queue is present:
3914                  * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3915                  */
3916                 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3917                         return;
3918         }
3919
3920         pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3921 }
3922
3923 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3924 {
3925         int code;
3926         int i;
3927
3928         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3929                 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3930                 if (code < 0) {
3931                         pr_warn("Failed to get event during flush: %d\n", code);
3932                         return code;
3933                 }
3934
3935                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3936                         return 0;
3937         }
3938
3939         pr_warn("Failed to flush event queue\n");
3940         return -EIO;
3941 }
3942
3943 /* Sysfs **********************************************************************/
3944
3945 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3946                              const char *buf, size_t count)
3947 {
3948         u32 retval;
3949         int err, value;
3950
3951         value = asus_wmi_get_devstate_simple(asus, devid);
3952         if (value < 0)
3953                 return value;
3954
3955         err = kstrtoint(buf, 0, &value);
3956         if (err)
3957                 return err;
3958
3959         err = asus_wmi_set_devstate(devid, value, &retval);
3960         if (err < 0)
3961                 return err;
3962
3963         return count;
3964 }
3965
3966 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3967 {
3968         int value = asus_wmi_get_devstate_simple(asus, devid);
3969
3970         if (value < 0)
3971                 return value;
3972
3973         return sprintf(buf, "%d\n", value);
3974 }
3975
3976 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)                  \
3977         static ssize_t show_##_name(struct device *dev,                 \
3978                                     struct device_attribute *attr,      \
3979                                     char *buf)                          \
3980         {                                                               \
3981                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
3982                                                                         \
3983                 return show_sys_wmi(asus, _cm, buf);                    \
3984         }                                                               \
3985         static ssize_t store_##_name(struct device *dev,                \
3986                                      struct device_attribute *attr,     \
3987                                      const char *buf, size_t count)     \
3988         {                                                               \
3989                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
3990                                                                         \
3991                 return store_sys_wmi(asus, _cm, buf, count);            \
3992         }                                                               \
3993         static struct device_attribute dev_attr_##_name = {             \
3994                 .attr = {                                               \
3995                         .name = __stringify(_name),                     \
3996                         .mode = _mode },                                \
3997                 .show   = show_##_name,                                 \
3998                 .store  = store_##_name,                                \
3999         }
4000
4001 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
4002 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
4003 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
4004 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
4005 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
4006
4007 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
4008                            const char *buf, size_t count)
4009 {
4010         int value, rv;
4011
4012         rv = kstrtoint(buf, 0, &value);
4013         if (rv)
4014                 return rv;
4015
4016         if (value < 0 || value > 2)
4017                 return -EINVAL;
4018
4019         rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
4020         if (rv < 0)
4021                 return rv;
4022
4023         return count;
4024 }
4025
4026 static DEVICE_ATTR_WO(cpufv);
4027
4028 static struct attribute *platform_attributes[] = {
4029         &dev_attr_cpufv.attr,
4030         &dev_attr_camera.attr,
4031         &dev_attr_cardr.attr,
4032         &dev_attr_touchpad.attr,
4033         &dev_attr_charge_mode.attr,
4034         &dev_attr_egpu_enable.attr,
4035         &dev_attr_egpu_connected.attr,
4036         &dev_attr_dgpu_disable.attr,
4037         &dev_attr_gpu_mux_mode.attr,
4038         &dev_attr_lid_resume.attr,
4039         &dev_attr_als_enable.attr,
4040         &dev_attr_fan_boost_mode.attr,
4041         &dev_attr_throttle_thermal_policy.attr,
4042         &dev_attr_ppt_pl2_sppt.attr,
4043         &dev_attr_ppt_pl1_spl.attr,
4044         &dev_attr_ppt_fppt.attr,
4045         &dev_attr_ppt_apu_sppt.attr,
4046         &dev_attr_ppt_platform_sppt.attr,
4047         &dev_attr_nv_dynamic_boost.attr,
4048         &dev_attr_nv_temp_target.attr,
4049         &dev_attr_panel_od.attr,
4050         &dev_attr_mini_led_mode.attr,
4051         NULL
4052 };
4053
4054 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
4055                                     struct attribute *attr, int idx)
4056 {
4057         struct device *dev = kobj_to_dev(kobj);
4058         struct asus_wmi *asus = dev_get_drvdata(dev);
4059         bool ok = true;
4060         int devid = -1;
4061
4062         if (attr == &dev_attr_camera.attr)
4063                 devid = ASUS_WMI_DEVID_CAMERA;
4064         else if (attr == &dev_attr_cardr.attr)
4065                 devid = ASUS_WMI_DEVID_CARDREADER;
4066         else if (attr == &dev_attr_touchpad.attr)
4067                 devid = ASUS_WMI_DEVID_TOUCHPAD;
4068         else if (attr == &dev_attr_lid_resume.attr)
4069                 devid = ASUS_WMI_DEVID_LID_RESUME;
4070         else if (attr == &dev_attr_als_enable.attr)
4071                 devid = ASUS_WMI_DEVID_ALS_ENABLE;
4072         else if (attr == &dev_attr_charge_mode.attr)
4073                 ok = asus->charge_mode_available;
4074         else if (attr == &dev_attr_egpu_enable.attr)
4075                 ok = asus->egpu_enable_available;
4076         else if (attr == &dev_attr_egpu_connected.attr)
4077                 ok = asus->egpu_connect_available;
4078         else if (attr == &dev_attr_dgpu_disable.attr)
4079                 ok = asus->dgpu_disable_available;
4080         else if (attr == &dev_attr_gpu_mux_mode.attr)
4081                 ok = asus->gpu_mux_mode_available;
4082         else if (attr == &dev_attr_fan_boost_mode.attr)
4083                 ok = asus->fan_boost_mode_available;
4084         else if (attr == &dev_attr_throttle_thermal_policy.attr)
4085                 ok = asus->throttle_thermal_policy_available;
4086         else if (attr == &dev_attr_ppt_pl2_sppt.attr)
4087                 ok = asus->ppt_pl2_sppt_available;
4088         else if (attr == &dev_attr_ppt_pl1_spl.attr)
4089                 ok = asus->ppt_pl1_spl_available;
4090         else if (attr == &dev_attr_ppt_fppt.attr)
4091                 ok = asus->ppt_fppt_available;
4092         else if (attr == &dev_attr_ppt_apu_sppt.attr)
4093                 ok = asus->ppt_apu_sppt_available;
4094         else if (attr == &dev_attr_ppt_platform_sppt.attr)
4095                 ok = asus->ppt_plat_sppt_available;
4096         else if (attr == &dev_attr_nv_dynamic_boost.attr)
4097                 ok = asus->nv_dyn_boost_available;
4098         else if (attr == &dev_attr_nv_temp_target.attr)
4099                 ok = asus->nv_temp_tgt_available;
4100         else if (attr == &dev_attr_panel_od.attr)
4101                 ok = asus->panel_overdrive_available;
4102         else if (attr == &dev_attr_mini_led_mode.attr)
4103                 ok = asus->mini_led_mode_available;
4104
4105         if (devid != -1)
4106                 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
4107
4108         return ok ? attr->mode : 0;
4109 }
4110
4111 static const struct attribute_group platform_attribute_group = {
4112         .is_visible = asus_sysfs_is_visible,
4113         .attrs = platform_attributes
4114 };
4115
4116 static void asus_wmi_sysfs_exit(struct platform_device *device)
4117 {
4118         sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
4119 }
4120
4121 static int asus_wmi_sysfs_init(struct platform_device *device)
4122 {
4123         return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
4124 }
4125
4126 /* Platform device ************************************************************/
4127
4128 static int asus_wmi_platform_init(struct asus_wmi *asus)
4129 {
4130         struct device *dev = &asus->platform_device->dev;
4131         char *wmi_uid;
4132         int rv;
4133
4134         /* INIT enable hotkeys on some models */
4135         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
4136                 pr_info("Initialization: %#x\n", rv);
4137
4138         /* We don't know yet what to do with this version... */
4139         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
4140                 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
4141                 asus->spec = rv;
4142         }
4143
4144         /*
4145          * The SFUN method probably allows the original driver to get the list
4146          * of features supported by a given model. For now, 0x0100 or 0x0800
4147          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
4148          * The significance of others is yet to be found.
4149          */
4150         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
4151                 pr_info("SFUN value: %#x\n", rv);
4152                 asus->sfun = rv;
4153         }
4154
4155         /*
4156          * Eee PC and Notebooks seems to have different method_id for DSTS,
4157          * but it may also be related to the BIOS's SPEC.
4158          * Note, on most Eeepc, there is no way to check if a method exist
4159          * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
4160          * but once again, SPEC may probably be used for that kind of things.
4161          *
4162          * Additionally at least TUF Gaming series laptops return nothing for
4163          * unknown methods, so the detection in this way is not possible.
4164          *
4165          * There is strong indication that only ACPI WMI devices that have _UID
4166          * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
4167          */
4168         wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
4169         if (!wmi_uid)
4170                 return -ENODEV;
4171
4172         if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
4173                 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
4174                 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
4175         } else {
4176                 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
4177                 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
4178         }
4179
4180         /*
4181          * Some devices can have multiple event codes stored in a queue before
4182          * the module load if it was unloaded intermittently after calling
4183          * the INIT method (enables event handling). The WMI notify handler is
4184          * expected to retrieve all event codes until a retrieved code equals
4185          * queue end marker (One or Ones). Old codes are flushed from the queue
4186          * upon module load. Not enabling this when it should be has minimal
4187          * visible impact so fall back if anything goes wrong.
4188          */
4189         wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
4190         if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
4191                 dev_info(dev, "Detected ATK, enable event queue\n");
4192
4193                 if (!asus_wmi_notify_queue_flush(asus))
4194                         asus->wmi_event_queue = true;
4195         }
4196
4197         /* CWAP allow to define the behavior of the Fn+F2 key,
4198          * this method doesn't seems to be present on Eee PCs */
4199         if (asus->driver->quirks->wapf >= 0)
4200                 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
4201                                       asus->driver->quirks->wapf, NULL);
4202
4203         return 0;
4204 }
4205
4206 /* debugfs ********************************************************************/
4207
4208 struct asus_wmi_debugfs_node {
4209         struct asus_wmi *asus;
4210         char *name;
4211         int (*show) (struct seq_file *m, void *data);
4212 };
4213
4214 static int show_dsts(struct seq_file *m, void *data)
4215 {
4216         struct asus_wmi *asus = m->private;
4217         int err;
4218         u32 retval = -1;
4219
4220         err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
4221         if (err < 0)
4222                 return err;
4223
4224         seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
4225
4226         return 0;
4227 }
4228
4229 static int show_devs(struct seq_file *m, void *data)
4230 {
4231         struct asus_wmi *asus = m->private;
4232         int err;
4233         u32 retval = -1;
4234
4235         err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
4236                                     &retval);
4237         if (err < 0)
4238                 return err;
4239
4240         seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
4241                    asus->debug.ctrl_param, retval);
4242
4243         return 0;
4244 }
4245
4246 static int show_call(struct seq_file *m, void *data)
4247 {
4248         struct asus_wmi *asus = m->private;
4249         struct bios_args args = {
4250                 .arg0 = asus->debug.dev_id,
4251                 .arg1 = asus->debug.ctrl_param,
4252         };
4253         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
4254         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
4255         union acpi_object *obj;
4256         acpi_status status;
4257
4258         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
4259                                      0, asus->debug.method_id,
4260                                      &input, &output);
4261
4262         if (ACPI_FAILURE(status))
4263                 return -EIO;
4264
4265         obj = (union acpi_object *)output.pointer;
4266         if (obj && obj->type == ACPI_TYPE_INTEGER)
4267                 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
4268                            asus->debug.dev_id, asus->debug.ctrl_param,
4269                            (u32) obj->integer.value);
4270         else
4271                 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
4272                            asus->debug.dev_id, asus->debug.ctrl_param,
4273                            obj ? obj->type : -1);
4274
4275         kfree(obj);
4276
4277         return 0;
4278 }
4279
4280 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
4281         {NULL, "devs", show_devs},
4282         {NULL, "dsts", show_dsts},
4283         {NULL, "call", show_call},
4284 };
4285
4286 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
4287 {
4288         struct asus_wmi_debugfs_node *node = inode->i_private;
4289
4290         return single_open(file, node->show, node->asus);
4291 }
4292
4293 static const struct file_operations asus_wmi_debugfs_io_ops = {
4294         .owner = THIS_MODULE,
4295         .open = asus_wmi_debugfs_open,
4296         .read = seq_read,
4297         .llseek = seq_lseek,
4298         .release = single_release,
4299 };
4300
4301 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
4302 {
4303         debugfs_remove_recursive(asus->debug.root);
4304 }
4305
4306 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
4307 {
4308         int i;
4309
4310         asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
4311
4312         debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
4313                            &asus->debug.method_id);
4314
4315         debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
4316                            &asus->debug.dev_id);
4317
4318         debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
4319                            &asus->debug.ctrl_param);
4320
4321         for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
4322                 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
4323
4324                 node->asus = asus;
4325                 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
4326                                     asus->debug.root, node,
4327                                     &asus_wmi_debugfs_io_ops);
4328         }
4329 }
4330
4331 /* Init / exit ****************************************************************/
4332
4333 static int asus_wmi_add(struct platform_device *pdev)
4334 {
4335         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4336         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4337         struct asus_wmi *asus;
4338         acpi_status status;
4339         int err;
4340         u32 result;
4341
4342         asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
4343         if (!asus)
4344                 return -ENOMEM;
4345
4346         asus->driver = wdrv;
4347         asus->platform_device = pdev;
4348         wdrv->platform_device = pdev;
4349         platform_set_drvdata(asus->platform_device, asus);
4350
4351         if (wdrv->detect_quirks)
4352                 wdrv->detect_quirks(asus->driver);
4353
4354         err = asus_wmi_platform_init(asus);
4355         if (err)
4356                 goto fail_platform;
4357
4358         asus->charge_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CHARGE_MODE);
4359         asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
4360         asus->egpu_connect_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
4361         asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
4362         asus->gpu_mux_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX);
4363         asus->kbd_rgb_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE);
4364         asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
4365         asus->ppt_pl2_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL2_SPPT);
4366         asus->ppt_pl1_spl_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL1_SPL);
4367         asus->ppt_fppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_FPPT);
4368         asus->ppt_apu_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_APU_SPPT);
4369         asus->ppt_plat_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PLAT_SPPT);
4370         asus->nv_dyn_boost_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_DYN_BOOST);
4371         asus->nv_temp_tgt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_THERM_TARGET);
4372         asus->panel_overdrive_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PANEL_OD);
4373         asus->mini_led_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
4374
4375         err = fan_boost_mode_check_present(asus);
4376         if (err)
4377                 goto fail_fan_boost_mode;
4378
4379         err = throttle_thermal_policy_check_present(asus);
4380         if (err)
4381                 goto fail_throttle_thermal_policy;
4382         else
4383                 throttle_thermal_policy_set_default(asus);
4384
4385         err = platform_profile_setup(asus);
4386         if (err)
4387                 goto fail_platform_profile_setup;
4388
4389         err = asus_wmi_sysfs_init(asus->platform_device);
4390         if (err)
4391                 goto fail_sysfs;
4392
4393         err = asus_wmi_input_init(asus);
4394         if (err)
4395                 goto fail_input;
4396
4397         err = asus_wmi_fan_init(asus); /* probably no problems on error */
4398
4399         err = asus_wmi_hwmon_init(asus);
4400         if (err)
4401                 goto fail_hwmon;
4402
4403         err = asus_wmi_custom_fan_curve_init(asus);
4404         if (err)
4405                 goto fail_custom_fan_curve;
4406
4407         err = asus_wmi_led_init(asus);
4408         if (err)
4409                 goto fail_leds;
4410
4411         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
4412         if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
4413                 asus->driver->wlan_ctrl_by_user = 1;
4414
4415         if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
4416                 err = asus_wmi_rfkill_init(asus);
4417                 if (err)
4418                         goto fail_rfkill;
4419         }
4420
4421         if (asus->driver->quirks->wmi_force_als_set)
4422                 asus_wmi_set_als();
4423
4424         if (asus->driver->quirks->xusb2pr)
4425                 asus_wmi_set_xusb2pr(asus);
4426
4427         if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
4428                 err = asus_wmi_backlight_init(asus);
4429                 if (err && err != -ENODEV)
4430                         goto fail_backlight;
4431         } else if (asus->driver->quirks->wmi_backlight_set_devstate)
4432                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
4433
4434         if (asus_wmi_has_fnlock_key(asus)) {
4435                 asus->fnlock_locked = fnlock_default;
4436                 asus_wmi_fnlock_update(asus);
4437         }
4438
4439         status = wmi_install_notify_handler(asus->driver->event_guid,
4440                                             asus_wmi_notify, asus);
4441         if (ACPI_FAILURE(status)) {
4442                 pr_err("Unable to register notify handler - %d\n", status);
4443                 err = -ENODEV;
4444                 goto fail_wmi_handler;
4445         }
4446
4447         asus_wmi_battery_init(asus);
4448
4449         asus_wmi_debugfs_init(asus);
4450
4451         return 0;
4452
4453 fail_wmi_handler:
4454         asus_wmi_backlight_exit(asus);
4455 fail_backlight:
4456         asus_wmi_rfkill_exit(asus);
4457 fail_rfkill:
4458         asus_wmi_led_exit(asus);
4459 fail_leds:
4460 fail_hwmon:
4461         asus_wmi_input_exit(asus);
4462 fail_input:
4463         asus_wmi_sysfs_exit(asus->platform_device);
4464 fail_sysfs:
4465 fail_throttle_thermal_policy:
4466 fail_custom_fan_curve:
4467 fail_platform_profile_setup:
4468         if (asus->platform_profile_support)
4469                 platform_profile_remove();
4470 fail_fan_boost_mode:
4471 fail_platform:
4472         kfree(asus);
4473         return err;
4474 }
4475
4476 static int asus_wmi_remove(struct platform_device *device)
4477 {
4478         struct asus_wmi *asus;
4479
4480         asus = platform_get_drvdata(device);
4481         wmi_remove_notify_handler(asus->driver->event_guid);
4482         asus_wmi_backlight_exit(asus);
4483         asus_wmi_input_exit(asus);
4484         asus_wmi_led_exit(asus);
4485         asus_wmi_rfkill_exit(asus);
4486         asus_wmi_debugfs_exit(asus);
4487         asus_wmi_sysfs_exit(asus->platform_device);
4488         asus_fan_set_auto(asus);
4489         throttle_thermal_policy_set_default(asus);
4490         asus_wmi_battery_exit(asus);
4491
4492         if (asus->platform_profile_support)
4493                 platform_profile_remove();
4494
4495         kfree(asus);
4496         return 0;
4497 }
4498
4499 /* Platform driver - hibernate/resume callbacks *******************************/
4500
4501 static int asus_hotk_thaw(struct device *device)
4502 {
4503         struct asus_wmi *asus = dev_get_drvdata(device);
4504
4505         if (asus->wlan.rfkill) {
4506                 bool wlan;
4507
4508                 /*
4509                  * Work around bios bug - acpi _PTS turns off the wireless led
4510                  * during suspend.  Normally it restores it on resume, but
4511                  * we should kick it ourselves in case hibernation is aborted.
4512                  */
4513                 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
4514                 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
4515         }
4516
4517         return 0;
4518 }
4519
4520 static int asus_hotk_resume(struct device *device)
4521 {
4522         struct asus_wmi *asus = dev_get_drvdata(device);
4523
4524         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4525                 kbd_led_update(asus);
4526
4527         if (asus_wmi_has_fnlock_key(asus))
4528                 asus_wmi_fnlock_update(asus);
4529
4530         asus_wmi_tablet_mode_get_state(asus);
4531         return 0;
4532 }
4533
4534 static int asus_hotk_restore(struct device *device)
4535 {
4536         struct asus_wmi *asus = dev_get_drvdata(device);
4537         int bl;
4538
4539         /* Refresh both wlan rfkill state and pci hotplug */
4540         if (asus->wlan.rfkill)
4541                 asus_rfkill_hotplug(asus);
4542
4543         if (asus->bluetooth.rfkill) {
4544                 bl = !asus_wmi_get_devstate_simple(asus,
4545                                                    ASUS_WMI_DEVID_BLUETOOTH);
4546                 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
4547         }
4548         if (asus->wimax.rfkill) {
4549                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
4550                 rfkill_set_sw_state(asus->wimax.rfkill, bl);
4551         }
4552         if (asus->wwan3g.rfkill) {
4553                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
4554                 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
4555         }
4556         if (asus->gps.rfkill) {
4557                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
4558                 rfkill_set_sw_state(asus->gps.rfkill, bl);
4559         }
4560         if (asus->uwb.rfkill) {
4561                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
4562                 rfkill_set_sw_state(asus->uwb.rfkill, bl);
4563         }
4564         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4565                 kbd_led_update(asus);
4566
4567         if (asus_wmi_has_fnlock_key(asus))
4568                 asus_wmi_fnlock_update(asus);
4569
4570         asus_wmi_tablet_mode_get_state(asus);
4571         return 0;
4572 }
4573
4574 static const struct dev_pm_ops asus_pm_ops = {
4575         .thaw = asus_hotk_thaw,
4576         .restore = asus_hotk_restore,
4577         .resume = asus_hotk_resume,
4578 };
4579
4580 /* Registration ***************************************************************/
4581
4582 static int asus_wmi_probe(struct platform_device *pdev)
4583 {
4584         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4585         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4586         int ret;
4587
4588         if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
4589                 pr_warn("ASUS Management GUID not found\n");
4590                 return -ENODEV;
4591         }
4592
4593         if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
4594                 pr_warn("ASUS Event GUID not found\n");
4595                 return -ENODEV;
4596         }
4597
4598         if (wdrv->probe) {
4599                 ret = wdrv->probe(pdev);
4600                 if (ret)
4601                         return ret;
4602         }
4603
4604         return asus_wmi_add(pdev);
4605 }
4606
4607 static bool used;
4608
4609 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
4610 {
4611         struct platform_driver *platform_driver;
4612         struct platform_device *platform_device;
4613
4614         if (used)
4615                 return -EBUSY;
4616
4617         platform_driver = &driver->platform_driver;
4618         platform_driver->remove = asus_wmi_remove;
4619         platform_driver->driver.owner = driver->owner;
4620         platform_driver->driver.name = driver->name;
4621         platform_driver->driver.pm = &asus_pm_ops;
4622
4623         platform_device = platform_create_bundle(platform_driver,
4624                                                  asus_wmi_probe,
4625                                                  NULL, 0, NULL, 0);
4626         if (IS_ERR(platform_device))
4627                 return PTR_ERR(platform_device);
4628
4629         used = true;
4630         return 0;
4631 }
4632 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
4633
4634 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
4635 {
4636         platform_device_unregister(driver->platform_device);
4637         platform_driver_unregister(&driver->platform_driver);
4638         used = false;
4639 }
4640 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
4641
4642 static int __init asus_wmi_init(void)
4643 {
4644         pr_info("ASUS WMI generic driver loaded\n");
4645         return 0;
4646 }
4647
4648 static void __exit asus_wmi_exit(void)
4649 {
4650         pr_info("ASUS WMI generic driver unloaded\n");
4651 }
4652
4653 module_init(asus_wmi_init);
4654 module_exit(asus_wmi_exit);