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