Merge tag 'spi-fix-v6.0-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[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               (FAN_CURVE_POINTS * 2)
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 = "asus::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                 return err;
2238
2239         fan_curve_copy_from_buf(curves, buf);
2240         curves->device_id = fan_dev;
2241
2242         return 0;
2243 }
2244
2245 /* Check if capability exists, and populate defaults */
2246 static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2247                                    u32 fan_dev)
2248 {
2249         int err;
2250
2251         *available = false;
2252
2253         err = fan_curve_get_factory_default(asus, fan_dev);
2254         if (err) {
2255                 pr_debug("fan_curve_get_factory_default(0x%08x) failed: %d\n",
2256                          fan_dev, err);
2257                 /* Don't cause probe to fail on devices without fan-curves */
2258                 return 0;
2259         }
2260
2261         *available = true;
2262         return 0;
2263 }
2264
2265 /* Determine which fan the attribute is for if SENSOR_ATTR */
2266 static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2267                                               struct device_attribute *attr)
2268 {
2269         int index = to_sensor_dev_attr(attr)->index;
2270
2271         return &asus->custom_fan_curves[index & FAN_CURVE_DEV_GPU];
2272 }
2273
2274 /* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2275 static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2276                                             struct device_attribute *attr)
2277 {
2278         int nr = to_sensor_dev_attr_2(attr)->nr;
2279
2280         return &asus->custom_fan_curves[nr & FAN_CURVE_DEV_GPU];
2281 }
2282
2283 static ssize_t fan_curve_show(struct device *dev,
2284                               struct device_attribute *attr, char *buf)
2285 {
2286         struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2287         struct asus_wmi *asus = dev_get_drvdata(dev);
2288         struct fan_curve_data *data;
2289         int value, index, nr;
2290
2291         data = fan_curve_attr_2_select(asus, attr);
2292         index = dev_attr->index;
2293         nr = dev_attr->nr;
2294
2295         if (nr & FAN_CURVE_PWM_MASK)
2296                 value = data->percents[index];
2297         else
2298                 value = data->temps[index];
2299
2300         return sysfs_emit(buf, "%d\n", value);
2301 }
2302
2303 /*
2304  * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
2305  */
2306 static int fan_curve_write(struct asus_wmi *asus,
2307                            struct fan_curve_data *data)
2308 {
2309         u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
2310         u8 *percents = data->percents;
2311         u8 *temps = data->temps;
2312         int ret, i, shift = 0;
2313
2314         if (!data->enabled)
2315                 return 0;
2316
2317         for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
2318                 arg1 += (temps[i]) << shift;
2319                 arg2 += (temps[i + 4]) << shift;
2320                 /* Scale to percentage for device */
2321                 arg3 += (100 * percents[i] / 255) << shift;
2322                 arg4 += (100 * percents[i + 4] / 255) << shift;
2323                 shift += 8;
2324         }
2325
2326         return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
2327                                          data->device_id,
2328                                          arg1, arg2, arg3, arg4, &ret);
2329 }
2330
2331 static ssize_t fan_curve_store(struct device *dev,
2332                                struct device_attribute *attr, const char *buf,
2333                                size_t count)
2334 {
2335         struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
2336         struct asus_wmi *asus = dev_get_drvdata(dev);
2337         struct fan_curve_data *data;
2338         u8 value;
2339         int err;
2340
2341         int pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
2342         int index = dev_attr->index;
2343
2344         data = fan_curve_attr_2_select(asus, attr);
2345
2346         err = kstrtou8(buf, 10, &value);
2347         if (err < 0)
2348                 return err;
2349
2350         if (pwm) {
2351                 data->percents[index] = value;
2352         } else {
2353                 data->temps[index] = value;
2354         }
2355
2356         /*
2357          * Mark as disabled so the user has to explicitly enable to apply a
2358          * changed fan curve. This prevents potential lockups from writing out
2359          * many changes as one-write-per-change.
2360          */
2361         data->enabled = false;
2362
2363         return count;
2364 }
2365
2366 static ssize_t fan_curve_enable_show(struct device *dev,
2367                                      struct device_attribute *attr, char *buf)
2368 {
2369         struct asus_wmi *asus = dev_get_drvdata(dev);
2370         struct fan_curve_data *data;
2371         int out = 2;
2372
2373         data = fan_curve_attr_select(asus, attr);
2374
2375         if (data->enabled)
2376                 out = 1;
2377
2378         return sysfs_emit(buf, "%d\n", out);
2379 }
2380
2381 static ssize_t fan_curve_enable_store(struct device *dev,
2382                                       struct device_attribute *attr,
2383                                       const char *buf, size_t count)
2384 {
2385         struct asus_wmi *asus = dev_get_drvdata(dev);
2386         struct fan_curve_data *data;
2387         int value, err;
2388
2389         data = fan_curve_attr_select(asus, attr);
2390
2391         err = kstrtoint(buf, 10, &value);
2392         if (err < 0)
2393                 return err;
2394
2395         switch (value) {
2396         case 1:
2397                 data->enabled = true;
2398                 break;
2399         case 2:
2400                 data->enabled = false;
2401                 break;
2402         /*
2403          * Auto + reset the fan curve data to defaults. Make it an explicit
2404          * option so that users don't accidentally overwrite a set fan curve.
2405          */
2406         case 3:
2407                 err = fan_curve_get_factory_default(asus, data->device_id);
2408                 if (err)
2409                         return err;
2410                 data->enabled = false;
2411                 break;
2412         default:
2413                 return -EINVAL;
2414         }
2415
2416         if (data->enabled) {
2417                 err = fan_curve_write(asus, data);
2418                 if (err)
2419                         return err;
2420         } else {
2421                 /*
2422                  * For machines with throttle this is the only way to reset fans
2423                  * to default mode of operation (does not erase curve data).
2424                  */
2425                 if (asus->throttle_thermal_policy_available) {
2426                         err = throttle_thermal_policy_write(asus);
2427                         if (err)
2428                                 return err;
2429                 /* Similar is true for laptops with this fan */
2430                 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
2431                         err = asus_fan_set_auto(asus);
2432                         if (err)
2433                                 return err;
2434                 } else {
2435                         /* Safeguard against fautly ACPI tables */
2436                         err = fan_curve_get_factory_default(asus, data->device_id);
2437                         if (err)
2438                                 return err;
2439                         err = fan_curve_write(asus, data);
2440                         if (err)
2441                                 return err;
2442                 }
2443         }
2444         return count;
2445 }
2446
2447 /* CPU */
2448 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
2449 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
2450                                FAN_CURVE_DEV_CPU, 0);
2451 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
2452                                FAN_CURVE_DEV_CPU, 1);
2453 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
2454                                FAN_CURVE_DEV_CPU, 2);
2455 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
2456                                FAN_CURVE_DEV_CPU, 3);
2457 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
2458                                FAN_CURVE_DEV_CPU, 4);
2459 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
2460                                FAN_CURVE_DEV_CPU, 5);
2461 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
2462                                FAN_CURVE_DEV_CPU, 6);
2463 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
2464                                FAN_CURVE_DEV_CPU, 7);
2465
2466 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
2467                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
2468 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
2469                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
2470 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
2471                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
2472 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
2473                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
2474 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
2475                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
2476 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
2477                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
2478 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
2479                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
2480 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
2481                                FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
2482
2483 /* GPU */
2484 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
2485 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
2486                                FAN_CURVE_DEV_GPU, 0);
2487 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
2488                                FAN_CURVE_DEV_GPU, 1);
2489 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
2490                                FAN_CURVE_DEV_GPU, 2);
2491 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
2492                                FAN_CURVE_DEV_GPU, 3);
2493 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
2494                                FAN_CURVE_DEV_GPU, 4);
2495 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
2496                                FAN_CURVE_DEV_GPU, 5);
2497 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
2498                                FAN_CURVE_DEV_GPU, 6);
2499 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
2500                                FAN_CURVE_DEV_GPU, 7);
2501
2502 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
2503                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
2504 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
2505                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
2506 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
2507                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
2508 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
2509                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
2510 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
2511                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
2512 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
2513                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
2514 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
2515                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
2516 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
2517                                FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
2518
2519 static struct attribute *asus_fan_curve_attr[] = {
2520         /* CPU */
2521         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2522         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2523         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2524         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2525         &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2526         &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
2527         &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
2528         &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
2529         &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
2530         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2531         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2532         &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2533         &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2534         &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
2535         &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
2536         &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
2537         &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
2538         /* GPU */
2539         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2540         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2541         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2542         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2543         &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2544         &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
2545         &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
2546         &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
2547         &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
2548         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
2549         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2550         &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2551         &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2552         &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
2553         &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
2554         &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
2555         &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
2556         NULL
2557 };
2558
2559 static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
2560                                          struct attribute *attr, int idx)
2561 {
2562         struct device *dev = kobj_to_dev(kobj);
2563         struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2564
2565         /*
2566          * Check the char instead of casting attr as there are two attr types
2567          * involved here (attr1 and attr2)
2568          */
2569         if (asus->cpu_fan_curve_available && attr->name[3] == '1')
2570                 return 0644;
2571
2572         if (asus->gpu_fan_curve_available && attr->name[3] == '2')
2573                 return 0644;
2574
2575         return 0;
2576 }
2577
2578 static const struct attribute_group asus_fan_curve_attr_group = {
2579         .is_visible = asus_fan_curve_is_visible,
2580         .attrs = asus_fan_curve_attr,
2581 };
2582 __ATTRIBUTE_GROUPS(asus_fan_curve_attr);
2583
2584 /*
2585  * Must be initialised after throttle_thermal_policy_check_present() as
2586  * we check the status of throttle_thermal_policy_available during init.
2587  */
2588 static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
2589 {
2590         struct device *dev = &asus->platform_device->dev;
2591         struct device *hwmon;
2592         int err;
2593
2594         err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
2595                                       ASUS_WMI_DEVID_CPU_FAN_CURVE);
2596         if (err)
2597                 return err;
2598
2599         err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
2600                                       ASUS_WMI_DEVID_GPU_FAN_CURVE);
2601         if (err)
2602                 return err;
2603
2604         if (!asus->cpu_fan_curve_available && !asus->gpu_fan_curve_available)
2605                 return 0;
2606
2607         hwmon = devm_hwmon_device_register_with_groups(
2608                 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
2609
2610         if (IS_ERR(hwmon)) {
2611                 dev_err(dev,
2612                         "Could not register asus_custom_fan_curve device\n");
2613                 return PTR_ERR(hwmon);
2614         }
2615
2616         return 0;
2617 }
2618
2619 /* Throttle thermal policy ****************************************************/
2620
2621 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
2622 {
2623         u32 result;
2624         int err;
2625
2626         asus->throttle_thermal_policy_available = false;
2627
2628         err = asus_wmi_get_devstate(asus,
2629                                     ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2630                                     &result);
2631         if (err) {
2632                 if (err == -ENODEV)
2633                         return 0;
2634                 return err;
2635         }
2636
2637         if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
2638                 asus->throttle_thermal_policy_available = true;
2639
2640         return 0;
2641 }
2642
2643 static int throttle_thermal_policy_write(struct asus_wmi *asus)
2644 {
2645         int err;
2646         u8 value;
2647         u32 retval;
2648
2649         value = asus->throttle_thermal_policy_mode;
2650
2651         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
2652                                     value, &retval);
2653
2654         sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2655                         "throttle_thermal_policy");
2656
2657         if (err) {
2658                 pr_warn("Failed to set throttle thermal policy: %d\n", err);
2659                 return err;
2660         }
2661
2662         if (retval != 1) {
2663                 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
2664                         retval);
2665                 return -EIO;
2666         }
2667
2668         /* Must set to disabled if mode is toggled */
2669         if (asus->cpu_fan_curve_available)
2670                 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2671         if (asus->gpu_fan_curve_available)
2672                 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2673
2674         return 0;
2675 }
2676
2677 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
2678 {
2679         if (!asus->throttle_thermal_policy_available)
2680                 return 0;
2681
2682         asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2683         return throttle_thermal_policy_write(asus);
2684 }
2685
2686 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
2687 {
2688         u8 new_mode = asus->throttle_thermal_policy_mode + 1;
2689         int err;
2690
2691         if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2692                 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2693
2694         asus->throttle_thermal_policy_mode = new_mode;
2695         err = throttle_thermal_policy_write(asus);
2696         if (err)
2697                 return err;
2698
2699         /*
2700          * Ensure that platform_profile updates userspace with the change to ensure
2701          * that platform_profile and throttle_thermal_policy_mode are in sync.
2702          */
2703         platform_profile_notify();
2704
2705         return 0;
2706 }
2707
2708 static ssize_t throttle_thermal_policy_show(struct device *dev,
2709                                    struct device_attribute *attr, char *buf)
2710 {
2711         struct asus_wmi *asus = dev_get_drvdata(dev);
2712         u8 mode = asus->throttle_thermal_policy_mode;
2713
2714         return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
2715 }
2716
2717 static ssize_t throttle_thermal_policy_store(struct device *dev,
2718                                     struct device_attribute *attr,
2719                                     const char *buf, size_t count)
2720 {
2721         struct asus_wmi *asus = dev_get_drvdata(dev);
2722         u8 new_mode;
2723         int result;
2724         int err;
2725
2726         result = kstrtou8(buf, 10, &new_mode);
2727         if (result < 0)
2728                 return result;
2729
2730         if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
2731                 return -EINVAL;
2732
2733         asus->throttle_thermal_policy_mode = new_mode;
2734         err = throttle_thermal_policy_write(asus);
2735         if (err)
2736                 return err;
2737
2738         /*
2739          * Ensure that platform_profile updates userspace with the change to ensure
2740          * that platform_profile and throttle_thermal_policy_mode are in sync.
2741          */
2742         platform_profile_notify();
2743
2744         return count;
2745 }
2746
2747 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
2748 static DEVICE_ATTR_RW(throttle_thermal_policy);
2749
2750 /* Platform profile ***********************************************************/
2751 static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
2752                                         enum platform_profile_option *profile)
2753 {
2754         struct asus_wmi *asus;
2755         int tp;
2756
2757         asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2758
2759         tp = asus->throttle_thermal_policy_mode;
2760
2761         switch (tp) {
2762         case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
2763                 *profile = PLATFORM_PROFILE_BALANCED;
2764                 break;
2765         case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
2766                 *profile = PLATFORM_PROFILE_PERFORMANCE;
2767                 break;
2768         case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
2769                 *profile = PLATFORM_PROFILE_QUIET;
2770                 break;
2771         default:
2772                 return -EINVAL;
2773         }
2774
2775         return 0;
2776 }
2777
2778 static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
2779                                         enum platform_profile_option profile)
2780 {
2781         struct asus_wmi *asus;
2782         int tp;
2783
2784         asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
2785
2786         switch (profile) {
2787         case PLATFORM_PROFILE_PERFORMANCE:
2788                 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
2789                 break;
2790         case PLATFORM_PROFILE_BALANCED:
2791                 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
2792                 break;
2793         case PLATFORM_PROFILE_QUIET:
2794                 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
2795                 break;
2796         default:
2797                 return -EOPNOTSUPP;
2798         }
2799
2800         asus->throttle_thermal_policy_mode = tp;
2801         return throttle_thermal_policy_write(asus);
2802 }
2803
2804 static int platform_profile_setup(struct asus_wmi *asus)
2805 {
2806         struct device *dev = &asus->platform_device->dev;
2807         int err;
2808
2809         /*
2810          * Not an error if a component platform_profile relies on is unavailable
2811          * so early return, skipping the setup of platform_profile.
2812          */
2813         if (!asus->throttle_thermal_policy_available)
2814                 return 0;
2815
2816         dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
2817
2818         asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
2819         asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
2820
2821         set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
2822         set_bit(PLATFORM_PROFILE_BALANCED,
2823                 asus->platform_profile_handler.choices);
2824         set_bit(PLATFORM_PROFILE_PERFORMANCE,
2825                 asus->platform_profile_handler.choices);
2826
2827         err = platform_profile_register(&asus->platform_profile_handler);
2828         if (err)
2829                 return err;
2830
2831         asus->platform_profile_support = true;
2832         return 0;
2833 }
2834
2835 /* Backlight ******************************************************************/
2836
2837 static int read_backlight_power(struct asus_wmi *asus)
2838 {
2839         int ret;
2840
2841         if (asus->driver->quirks->store_backlight_power)
2842                 ret = !asus->driver->panel_power;
2843         else
2844                 ret = asus_wmi_get_devstate_simple(asus,
2845                                                    ASUS_WMI_DEVID_BACKLIGHT);
2846
2847         if (ret < 0)
2848                 return ret;
2849
2850         return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
2851 }
2852
2853 static int read_brightness_max(struct asus_wmi *asus)
2854 {
2855         u32 retval;
2856         int err;
2857
2858         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2859         if (err < 0)
2860                 return err;
2861
2862         retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
2863         retval >>= 8;
2864
2865         if (!retval)
2866                 return -ENODEV;
2867
2868         return retval;
2869 }
2870
2871 static int read_brightness(struct backlight_device *bd)
2872 {
2873         struct asus_wmi *asus = bl_get_data(bd);
2874         u32 retval;
2875         int err;
2876
2877         err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
2878         if (err < 0)
2879                 return err;
2880
2881         return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
2882 }
2883
2884 static u32 get_scalar_command(struct backlight_device *bd)
2885 {
2886         struct asus_wmi *asus = bl_get_data(bd);
2887         u32 ctrl_param = 0;
2888
2889         if ((asus->driver->brightness < bd->props.brightness) ||
2890             bd->props.brightness == bd->props.max_brightness)
2891                 ctrl_param = 0x00008001;
2892         else if ((asus->driver->brightness > bd->props.brightness) ||
2893                  bd->props.brightness == 0)
2894                 ctrl_param = 0x00008000;
2895
2896         asus->driver->brightness = bd->props.brightness;
2897
2898         return ctrl_param;
2899 }
2900
2901 static int update_bl_status(struct backlight_device *bd)
2902 {
2903         struct asus_wmi *asus = bl_get_data(bd);
2904         u32 ctrl_param;
2905         int power, err = 0;
2906
2907         power = read_backlight_power(asus);
2908         if (power != -ENODEV && bd->props.power != power) {
2909                 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
2910                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
2911                                             ctrl_param, NULL);
2912                 if (asus->driver->quirks->store_backlight_power)
2913                         asus->driver->panel_power = bd->props.power;
2914
2915                 /* When using scalar brightness, updating the brightness
2916                  * will mess with the backlight power */
2917                 if (asus->driver->quirks->scalar_panel_brightness)
2918                         return err;
2919         }
2920
2921         if (asus->driver->quirks->scalar_panel_brightness)
2922                 ctrl_param = get_scalar_command(bd);
2923         else
2924                 ctrl_param = bd->props.brightness;
2925
2926         err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
2927                                     ctrl_param, NULL);
2928
2929         return err;
2930 }
2931
2932 static const struct backlight_ops asus_wmi_bl_ops = {
2933         .get_brightness = read_brightness,
2934         .update_status = update_bl_status,
2935 };
2936
2937 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
2938 {
2939         struct backlight_device *bd = asus->backlight_device;
2940         int old = bd->props.brightness;
2941         int new = old;
2942
2943         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2944                 new = code - NOTIFY_BRNUP_MIN + 1;
2945         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2946                 new = code - NOTIFY_BRNDOWN_MIN;
2947
2948         bd->props.brightness = new;
2949         backlight_update_status(bd);
2950         backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
2951
2952         return old;
2953 }
2954
2955 static int asus_wmi_backlight_init(struct asus_wmi *asus)
2956 {
2957         struct backlight_device *bd;
2958         struct backlight_properties props;
2959         int max;
2960         int power;
2961
2962         max = read_brightness_max(asus);
2963         if (max < 0)
2964                 return max;
2965
2966         power = read_backlight_power(asus);
2967         if (power == -ENODEV)
2968                 power = FB_BLANK_UNBLANK;
2969         else if (power < 0)
2970                 return power;
2971
2972         memset(&props, 0, sizeof(struct backlight_properties));
2973         props.type = BACKLIGHT_PLATFORM;
2974         props.max_brightness = max;
2975         bd = backlight_device_register(asus->driver->name,
2976                                        &asus->platform_device->dev, asus,
2977                                        &asus_wmi_bl_ops, &props);
2978         if (IS_ERR(bd)) {
2979                 pr_err("Could not register backlight device\n");
2980                 return PTR_ERR(bd);
2981         }
2982
2983         asus->backlight_device = bd;
2984
2985         if (asus->driver->quirks->store_backlight_power)
2986                 asus->driver->panel_power = power;
2987
2988         bd->props.brightness = read_brightness(bd);
2989         bd->props.power = power;
2990         backlight_update_status(bd);
2991
2992         asus->driver->brightness = bd->props.brightness;
2993
2994         return 0;
2995 }
2996
2997 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2998 {
2999         backlight_device_unregister(asus->backlight_device);
3000
3001         asus->backlight_device = NULL;
3002 }
3003
3004 static int is_display_toggle(int code)
3005 {
3006         /* display toggle keys */
3007         if ((code >= 0x61 && code <= 0x67) ||
3008             (code >= 0x8c && code <= 0x93) ||
3009             (code >= 0xa0 && code <= 0xa7) ||
3010             (code >= 0xd0 && code <= 0xd5))
3011                 return 1;
3012
3013         return 0;
3014 }
3015
3016 /* Fn-lock ********************************************************************/
3017
3018 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3019 {
3020         u32 result;
3021
3022         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3023
3024         return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3025                 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3026 }
3027
3028 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3029 {
3030         int mode = asus->fnlock_locked;
3031
3032         asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3033 }
3034
3035 /* WMI events *****************************************************************/
3036
3037 static int asus_wmi_get_event_code(u32 value)
3038 {
3039         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3040         union acpi_object *obj;
3041         acpi_status status;
3042         int code;
3043
3044         status = wmi_get_event_data(value, &response);
3045         if (ACPI_FAILURE(status)) {
3046                 pr_warn("Failed to get WMI notify code: %s\n",
3047                                 acpi_format_exception(status));
3048                 return -EIO;
3049         }
3050
3051         obj = (union acpi_object *)response.pointer;
3052
3053         if (obj && obj->type == ACPI_TYPE_INTEGER)
3054                 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3055         else
3056                 code = -EIO;
3057
3058         kfree(obj);
3059         return code;
3060 }
3061
3062 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3063 {
3064         unsigned int key_value = 1;
3065         bool autorelease = 1;
3066         int result, orig_code;
3067
3068         orig_code = code;
3069
3070         if (asus->driver->key_filter) {
3071                 asus->driver->key_filter(asus->driver, &code, &key_value,
3072                                          &autorelease);
3073                 if (code == ASUS_WMI_KEY_IGNORE)
3074                         return;
3075         }
3076
3077         if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3078                 code = ASUS_WMI_BRN_UP;
3079         else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3080                 code = ASUS_WMI_BRN_DOWN;
3081
3082         if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
3083                 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3084                         asus_wmi_backlight_notify(asus, orig_code);
3085                         return;
3086                 }
3087         }
3088
3089         if (code == NOTIFY_KBD_BRTUP) {
3090                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3091                 return;
3092         }
3093         if (code == NOTIFY_KBD_BRTDWN) {
3094                 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3095                 return;
3096         }
3097         if (code == NOTIFY_KBD_BRTTOGGLE) {
3098                 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3099                         kbd_led_set_by_kbd(asus, 0);
3100                 else
3101                         kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3102                 return;
3103         }
3104
3105         if (code == NOTIFY_FNLOCK_TOGGLE) {
3106                 asus->fnlock_locked = !asus->fnlock_locked;
3107                 asus_wmi_fnlock_update(asus);
3108                 return;
3109         }
3110
3111         if (asus->driver->quirks->use_kbd_dock_devid && code == NOTIFY_KBD_DOCK_CHANGE) {
3112                 result = asus_wmi_get_devstate_simple(asus,
3113                                                       ASUS_WMI_DEVID_KBD_DOCK);
3114                 if (result >= 0) {
3115                         input_report_switch(asus->inputdev, SW_TABLET_MODE,
3116                                             !result);
3117                         input_sync(asus->inputdev);
3118                 }
3119                 return;
3120         }
3121
3122         if (asus->driver->quirks->use_lid_flip_devid && code == NOTIFY_LID_FLIP) {
3123                 lid_flip_tablet_mode_get_state(asus);
3124                 return;
3125         }
3126
3127         if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
3128                 fan_boost_mode_switch_next(asus);
3129                 return;
3130         }
3131
3132         if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
3133                 throttle_thermal_policy_switch_next(asus);
3134                 return;
3135         }
3136
3137         if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
3138                 return;
3139
3140         if (!sparse_keymap_report_event(asus->inputdev, code,
3141                                         key_value, autorelease))
3142                 pr_info("Unknown key code 0x%x\n", code);
3143 }
3144
3145 static void asus_wmi_notify(u32 value, void *context)
3146 {
3147         struct asus_wmi *asus = context;
3148         int code;
3149         int i;
3150
3151         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3152                 code = asus_wmi_get_event_code(value);
3153                 if (code < 0) {
3154                         pr_warn("Failed to get notify code: %d\n", code);
3155                         return;
3156                 }
3157
3158                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3159                         return;
3160
3161                 asus_wmi_handle_event_code(code, asus);
3162
3163                 /*
3164                  * Double check that queue is present:
3165                  * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
3166                  */
3167                 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
3168                         return;
3169         }
3170
3171         pr_warn("Failed to process event queue, last code: 0x%x\n", code);
3172 }
3173
3174 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
3175 {
3176         int code;
3177         int i;
3178
3179         for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
3180                 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
3181                 if (code < 0) {
3182                         pr_warn("Failed to get event during flush: %d\n", code);
3183                         return code;
3184                 }
3185
3186                 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
3187                         return 0;
3188         }
3189
3190         pr_warn("Failed to flush event queue\n");
3191         return -EIO;
3192 }
3193
3194 /* Sysfs **********************************************************************/
3195
3196 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
3197                              const char *buf, size_t count)
3198 {
3199         u32 retval;
3200         int err, value;
3201
3202         value = asus_wmi_get_devstate_simple(asus, devid);
3203         if (value < 0)
3204                 return value;
3205
3206         err = kstrtoint(buf, 0, &value);
3207         if (err)
3208                 return err;
3209
3210         err = asus_wmi_set_devstate(devid, value, &retval);
3211         if (err < 0)
3212                 return err;
3213
3214         return count;
3215 }
3216
3217 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
3218 {
3219         int value = asus_wmi_get_devstate_simple(asus, devid);
3220
3221         if (value < 0)
3222                 return value;
3223
3224         return sprintf(buf, "%d\n", value);
3225 }
3226
3227 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)                  \
3228         static ssize_t show_##_name(struct device *dev,                 \
3229                                     struct device_attribute *attr,      \
3230                                     char *buf)                          \
3231         {                                                               \
3232                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
3233                                                                         \
3234                 return show_sys_wmi(asus, _cm, buf);                    \
3235         }                                                               \
3236         static ssize_t store_##_name(struct device *dev,                \
3237                                      struct device_attribute *attr,     \
3238                                      const char *buf, size_t count)     \
3239         {                                                               \
3240                 struct asus_wmi *asus = dev_get_drvdata(dev);           \
3241                                                                         \
3242                 return store_sys_wmi(asus, _cm, buf, count);            \
3243         }                                                               \
3244         static struct device_attribute dev_attr_##_name = {             \
3245                 .attr = {                                               \
3246                         .name = __stringify(_name),                     \
3247                         .mode = _mode },                                \
3248                 .show   = show_##_name,                                 \
3249                 .store  = store_##_name,                                \
3250         }
3251
3252 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
3253 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
3254 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
3255 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
3256 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
3257
3258 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
3259                            const char *buf, size_t count)
3260 {
3261         int value, rv;
3262
3263         rv = kstrtoint(buf, 0, &value);
3264         if (rv)
3265                 return rv;
3266
3267         if (value < 0 || value > 2)
3268                 return -EINVAL;
3269
3270         rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
3271         if (rv < 0)
3272                 return rv;
3273
3274         return count;
3275 }
3276
3277 static DEVICE_ATTR_WO(cpufv);
3278
3279 static struct attribute *platform_attributes[] = {
3280         &dev_attr_cpufv.attr,
3281         &dev_attr_camera.attr,
3282         &dev_attr_cardr.attr,
3283         &dev_attr_touchpad.attr,
3284         &dev_attr_egpu_enable.attr,
3285         &dev_attr_dgpu_disable.attr,
3286         &dev_attr_lid_resume.attr,
3287         &dev_attr_als_enable.attr,
3288         &dev_attr_fan_boost_mode.attr,
3289         &dev_attr_throttle_thermal_policy.attr,
3290         &dev_attr_panel_od.attr,
3291         NULL
3292 };
3293
3294 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
3295                                     struct attribute *attr, int idx)
3296 {
3297         struct device *dev = container_of(kobj, struct device, kobj);
3298         struct asus_wmi *asus = dev_get_drvdata(dev);
3299         bool ok = true;
3300         int devid = -1;
3301
3302         if (attr == &dev_attr_camera.attr)
3303                 devid = ASUS_WMI_DEVID_CAMERA;
3304         else if (attr == &dev_attr_cardr.attr)
3305                 devid = ASUS_WMI_DEVID_CARDREADER;
3306         else if (attr == &dev_attr_touchpad.attr)
3307                 devid = ASUS_WMI_DEVID_TOUCHPAD;
3308         else if (attr == &dev_attr_lid_resume.attr)
3309                 devid = ASUS_WMI_DEVID_LID_RESUME;
3310         else if (attr == &dev_attr_als_enable.attr)
3311                 devid = ASUS_WMI_DEVID_ALS_ENABLE;
3312         else if (attr == &dev_attr_egpu_enable.attr)
3313                 ok = asus->egpu_enable_available;
3314         else if (attr == &dev_attr_dgpu_disable.attr)
3315                 ok = asus->dgpu_disable_available;
3316         else if (attr == &dev_attr_fan_boost_mode.attr)
3317                 ok = asus->fan_boost_mode_available;
3318         else if (attr == &dev_attr_throttle_thermal_policy.attr)
3319                 ok = asus->throttle_thermal_policy_available;
3320         else if (attr == &dev_attr_panel_od.attr)
3321                 ok = asus->panel_overdrive_available;
3322
3323         if (devid != -1)
3324                 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
3325
3326         return ok ? attr->mode : 0;
3327 }
3328
3329 static const struct attribute_group platform_attribute_group = {
3330         .is_visible = asus_sysfs_is_visible,
3331         .attrs = platform_attributes
3332 };
3333
3334 static void asus_wmi_sysfs_exit(struct platform_device *device)
3335 {
3336         sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
3337 }
3338
3339 static int asus_wmi_sysfs_init(struct platform_device *device)
3340 {
3341         return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
3342 }
3343
3344 /* Platform device ************************************************************/
3345
3346 static int asus_wmi_platform_init(struct asus_wmi *asus)
3347 {
3348         struct device *dev = &asus->platform_device->dev;
3349         char *wmi_uid;
3350         int rv;
3351
3352         /* INIT enable hotkeys on some models */
3353         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
3354                 pr_info("Initialization: %#x\n", rv);
3355
3356         /* We don't know yet what to do with this version... */
3357         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
3358                 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
3359                 asus->spec = rv;
3360         }
3361
3362         /*
3363          * The SFUN method probably allows the original driver to get the list
3364          * of features supported by a given model. For now, 0x0100 or 0x0800
3365          * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
3366          * The significance of others is yet to be found.
3367          */
3368         if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
3369                 pr_info("SFUN value: %#x\n", rv);
3370                 asus->sfun = rv;
3371         }
3372
3373         /*
3374          * Eee PC and Notebooks seems to have different method_id for DSTS,
3375          * but it may also be related to the BIOS's SPEC.
3376          * Note, on most Eeepc, there is no way to check if a method exist
3377          * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
3378          * but once again, SPEC may probably be used for that kind of things.
3379          *
3380          * Additionally at least TUF Gaming series laptops return nothing for
3381          * unknown methods, so the detection in this way is not possible.
3382          *
3383          * There is strong indication that only ACPI WMI devices that have _UID
3384          * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
3385          */
3386         wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
3387         if (!wmi_uid)
3388                 return -ENODEV;
3389
3390         if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
3391                 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
3392                 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
3393         } else {
3394                 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
3395                 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
3396         }
3397
3398         /*
3399          * Some devices can have multiple event codes stored in a queue before
3400          * the module load if it was unloaded intermittently after calling
3401          * the INIT method (enables event handling). The WMI notify handler is
3402          * expected to retrieve all event codes until a retrieved code equals
3403          * queue end marker (One or Ones). Old codes are flushed from the queue
3404          * upon module load. Not enabling this when it should be has minimal
3405          * visible impact so fall back if anything goes wrong.
3406          */
3407         wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
3408         if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
3409                 dev_info(dev, "Detected ATK, enable event queue\n");
3410
3411                 if (!asus_wmi_notify_queue_flush(asus))
3412                         asus->wmi_event_queue = true;
3413         }
3414
3415         /* CWAP allow to define the behavior of the Fn+F2 key,
3416          * this method doesn't seems to be present on Eee PCs */
3417         if (asus->driver->quirks->wapf >= 0)
3418                 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
3419                                       asus->driver->quirks->wapf, NULL);
3420
3421         return 0;
3422 }
3423
3424 /* debugfs ********************************************************************/
3425
3426 struct asus_wmi_debugfs_node {
3427         struct asus_wmi *asus;
3428         char *name;
3429         int (*show) (struct seq_file *m, void *data);
3430 };
3431
3432 static int show_dsts(struct seq_file *m, void *data)
3433 {
3434         struct asus_wmi *asus = m->private;
3435         int err;
3436         u32 retval = -1;
3437
3438         err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
3439         if (err < 0)
3440                 return err;
3441
3442         seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
3443
3444         return 0;
3445 }
3446
3447 static int show_devs(struct seq_file *m, void *data)
3448 {
3449         struct asus_wmi *asus = m->private;
3450         int err;
3451         u32 retval = -1;
3452
3453         err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
3454                                     &retval);
3455         if (err < 0)
3456                 return err;
3457
3458         seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
3459                    asus->debug.ctrl_param, retval);
3460
3461         return 0;
3462 }
3463
3464 static int show_call(struct seq_file *m, void *data)
3465 {
3466         struct asus_wmi *asus = m->private;
3467         struct bios_args args = {
3468                 .arg0 = asus->debug.dev_id,
3469                 .arg1 = asus->debug.ctrl_param,
3470         };
3471         struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3472         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3473         union acpi_object *obj;
3474         acpi_status status;
3475
3476         status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
3477                                      0, asus->debug.method_id,
3478                                      &input, &output);
3479
3480         if (ACPI_FAILURE(status))
3481                 return -EIO;
3482
3483         obj = (union acpi_object *)output.pointer;
3484         if (obj && obj->type == ACPI_TYPE_INTEGER)
3485                 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
3486                            asus->debug.dev_id, asus->debug.ctrl_param,
3487                            (u32) obj->integer.value);
3488         else
3489                 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
3490                            asus->debug.dev_id, asus->debug.ctrl_param,
3491                            obj ? obj->type : -1);
3492
3493         kfree(obj);
3494
3495         return 0;
3496 }
3497
3498 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
3499         {NULL, "devs", show_devs},
3500         {NULL, "dsts", show_dsts},
3501         {NULL, "call", show_call},
3502 };
3503
3504 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
3505 {
3506         struct asus_wmi_debugfs_node *node = inode->i_private;
3507
3508         return single_open(file, node->show, node->asus);
3509 }
3510
3511 static const struct file_operations asus_wmi_debugfs_io_ops = {
3512         .owner = THIS_MODULE,
3513         .open = asus_wmi_debugfs_open,
3514         .read = seq_read,
3515         .llseek = seq_lseek,
3516         .release = single_release,
3517 };
3518
3519 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
3520 {
3521         debugfs_remove_recursive(asus->debug.root);
3522 }
3523
3524 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
3525 {
3526         int i;
3527
3528         asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
3529
3530         debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
3531                            &asus->debug.method_id);
3532
3533         debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
3534                            &asus->debug.dev_id);
3535
3536         debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
3537                            &asus->debug.ctrl_param);
3538
3539         for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
3540                 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
3541
3542                 node->asus = asus;
3543                 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
3544                                     asus->debug.root, node,
3545                                     &asus_wmi_debugfs_io_ops);
3546         }
3547 }
3548
3549 /* Init / exit ****************************************************************/
3550
3551 static int asus_wmi_add(struct platform_device *pdev)
3552 {
3553         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3554         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3555         struct asus_wmi *asus;
3556         const char *chassis_type;
3557         acpi_status status;
3558         int err;
3559         u32 result;
3560
3561         asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
3562         if (!asus)
3563                 return -ENOMEM;
3564
3565         asus->driver = wdrv;
3566         asus->platform_device = pdev;
3567         wdrv->platform_device = pdev;
3568         platform_set_drvdata(asus->platform_device, asus);
3569
3570         if (wdrv->detect_quirks)
3571                 wdrv->detect_quirks(asus->driver);
3572
3573         err = asus_wmi_platform_init(asus);
3574         if (err)
3575                 goto fail_platform;
3576
3577         err = egpu_enable_check_present(asus);
3578         if (err)
3579                 goto fail_egpu_enable;
3580
3581         err = dgpu_disable_check_present(asus);
3582         if (err)
3583                 goto fail_dgpu_disable;
3584
3585         err = fan_boost_mode_check_present(asus);
3586         if (err)
3587                 goto fail_fan_boost_mode;
3588
3589         err = throttle_thermal_policy_check_present(asus);
3590         if (err)
3591                 goto fail_throttle_thermal_policy;
3592         else
3593                 throttle_thermal_policy_set_default(asus);
3594
3595         err = platform_profile_setup(asus);
3596         if (err)
3597                 goto fail_platform_profile_setup;
3598
3599         err = panel_od_check_present(asus);
3600         if (err)
3601                 goto fail_panel_od;
3602
3603         err = asus_wmi_sysfs_init(asus->platform_device);
3604         if (err)
3605                 goto fail_sysfs;
3606
3607         err = asus_wmi_input_init(asus);
3608         if (err)
3609                 goto fail_input;
3610
3611         err = asus_wmi_fan_init(asus); /* probably no problems on error */
3612
3613         err = asus_wmi_hwmon_init(asus);
3614         if (err)
3615                 goto fail_hwmon;
3616
3617         err = asus_wmi_custom_fan_curve_init(asus);
3618         if (err)
3619                 goto fail_custom_fan_curve;
3620
3621         err = asus_wmi_led_init(asus);
3622         if (err)
3623                 goto fail_leds;
3624
3625         asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
3626         if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
3627                 asus->driver->wlan_ctrl_by_user = 1;
3628
3629         if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
3630                 err = asus_wmi_rfkill_init(asus);
3631                 if (err)
3632                         goto fail_rfkill;
3633         }
3634
3635         if (asus->driver->quirks->wmi_force_als_set)
3636                 asus_wmi_set_als();
3637
3638         /* Some Asus desktop boards export an acpi-video backlight interface,
3639            stop this from showing up */
3640         chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
3641         if (chassis_type && !strcmp(chassis_type, "3"))
3642                 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3643
3644         if (asus->driver->quirks->wmi_backlight_power)
3645                 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
3646
3647         if (asus->driver->quirks->wmi_backlight_native)
3648                 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
3649
3650         if (asus->driver->quirks->xusb2pr)
3651                 asus_wmi_set_xusb2pr(asus);
3652
3653         if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
3654                 err = asus_wmi_backlight_init(asus);
3655                 if (err && err != -ENODEV)
3656                         goto fail_backlight;
3657         } else if (asus->driver->quirks->wmi_backlight_set_devstate)
3658                 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
3659
3660         if (asus_wmi_has_fnlock_key(asus)) {
3661                 asus->fnlock_locked = fnlock_default;
3662                 asus_wmi_fnlock_update(asus);
3663         }
3664
3665         status = wmi_install_notify_handler(asus->driver->event_guid,
3666                                             asus_wmi_notify, asus);
3667         if (ACPI_FAILURE(status)) {
3668                 pr_err("Unable to register notify handler - %d\n", status);
3669                 err = -ENODEV;
3670                 goto fail_wmi_handler;
3671         }
3672
3673         asus_wmi_battery_init(asus);
3674
3675         asus_wmi_debugfs_init(asus);
3676
3677         return 0;
3678
3679 fail_wmi_handler:
3680         asus_wmi_backlight_exit(asus);
3681 fail_backlight:
3682         asus_wmi_rfkill_exit(asus);
3683 fail_rfkill:
3684         asus_wmi_led_exit(asus);
3685 fail_leds:
3686 fail_hwmon:
3687         asus_wmi_input_exit(asus);
3688 fail_input:
3689         asus_wmi_sysfs_exit(asus->platform_device);
3690 fail_sysfs:
3691 fail_throttle_thermal_policy:
3692 fail_custom_fan_curve:
3693 fail_platform_profile_setup:
3694         if (asus->platform_profile_support)
3695                 platform_profile_remove();
3696 fail_fan_boost_mode:
3697 fail_egpu_enable:
3698 fail_dgpu_disable:
3699 fail_platform:
3700 fail_panel_od:
3701         kfree(asus);
3702         return err;
3703 }
3704
3705 static int asus_wmi_remove(struct platform_device *device)
3706 {
3707         struct asus_wmi *asus;
3708
3709         asus = platform_get_drvdata(device);
3710         wmi_remove_notify_handler(asus->driver->event_guid);
3711         asus_wmi_backlight_exit(asus);
3712         asus_wmi_input_exit(asus);
3713         asus_wmi_led_exit(asus);
3714         asus_wmi_rfkill_exit(asus);
3715         asus_wmi_debugfs_exit(asus);
3716         asus_wmi_sysfs_exit(asus->platform_device);
3717         asus_fan_set_auto(asus);
3718         throttle_thermal_policy_set_default(asus);
3719         asus_wmi_battery_exit(asus);
3720
3721         if (asus->platform_profile_support)
3722                 platform_profile_remove();
3723
3724         kfree(asus);
3725         return 0;
3726 }
3727
3728 /* Platform driver - hibernate/resume callbacks *******************************/
3729
3730 static int asus_hotk_thaw(struct device *device)
3731 {
3732         struct asus_wmi *asus = dev_get_drvdata(device);
3733
3734         if (asus->wlan.rfkill) {
3735                 bool wlan;
3736
3737                 /*
3738                  * Work around bios bug - acpi _PTS turns off the wireless led
3739                  * during suspend.  Normally it restores it on resume, but
3740                  * we should kick it ourselves in case hibernation is aborted.
3741                  */
3742                 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
3743                 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
3744         }
3745
3746         return 0;
3747 }
3748
3749 static int asus_hotk_resume(struct device *device)
3750 {
3751         struct asus_wmi *asus = dev_get_drvdata(device);
3752
3753         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3754                 kbd_led_update(asus);
3755
3756         if (asus_wmi_has_fnlock_key(asus))
3757                 asus_wmi_fnlock_update(asus);
3758
3759         if (asus->driver->quirks->use_lid_flip_devid)
3760                 lid_flip_tablet_mode_get_state(asus);
3761
3762         return 0;
3763 }
3764
3765 static int asus_hotk_restore(struct device *device)
3766 {
3767         struct asus_wmi *asus = dev_get_drvdata(device);
3768         int bl;
3769
3770         /* Refresh both wlan rfkill state and pci hotplug */
3771         if (asus->wlan.rfkill)
3772                 asus_rfkill_hotplug(asus);
3773
3774         if (asus->bluetooth.rfkill) {
3775                 bl = !asus_wmi_get_devstate_simple(asus,
3776                                                    ASUS_WMI_DEVID_BLUETOOTH);
3777                 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
3778         }
3779         if (asus->wimax.rfkill) {
3780                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
3781                 rfkill_set_sw_state(asus->wimax.rfkill, bl);
3782         }
3783         if (asus->wwan3g.rfkill) {
3784                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
3785                 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
3786         }
3787         if (asus->gps.rfkill) {
3788                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
3789                 rfkill_set_sw_state(asus->gps.rfkill, bl);
3790         }
3791         if (asus->uwb.rfkill) {
3792                 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
3793                 rfkill_set_sw_state(asus->uwb.rfkill, bl);
3794         }
3795         if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
3796                 kbd_led_update(asus);
3797
3798         if (asus_wmi_has_fnlock_key(asus))
3799                 asus_wmi_fnlock_update(asus);
3800
3801         if (asus->driver->quirks->use_lid_flip_devid)
3802                 lid_flip_tablet_mode_get_state(asus);
3803
3804         return 0;
3805 }
3806
3807 static const struct dev_pm_ops asus_pm_ops = {
3808         .thaw = asus_hotk_thaw,
3809         .restore = asus_hotk_restore,
3810         .resume = asus_hotk_resume,
3811 };
3812
3813 /* Registration ***************************************************************/
3814
3815 static int asus_wmi_probe(struct platform_device *pdev)
3816 {
3817         struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
3818         struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
3819         int ret;
3820
3821         if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
3822                 pr_warn("ASUS Management GUID not found\n");
3823                 return -ENODEV;
3824         }
3825
3826         if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
3827                 pr_warn("ASUS Event GUID not found\n");
3828                 return -ENODEV;
3829         }
3830
3831         if (wdrv->probe) {
3832                 ret = wdrv->probe(pdev);
3833                 if (ret)
3834                         return ret;
3835         }
3836
3837         return asus_wmi_add(pdev);
3838 }
3839
3840 static bool used;
3841
3842 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
3843 {
3844         struct platform_driver *platform_driver;
3845         struct platform_device *platform_device;
3846
3847         if (used)
3848                 return -EBUSY;
3849
3850         platform_driver = &driver->platform_driver;
3851         platform_driver->remove = asus_wmi_remove;
3852         platform_driver->driver.owner = driver->owner;
3853         platform_driver->driver.name = driver->name;
3854         platform_driver->driver.pm = &asus_pm_ops;
3855
3856         platform_device = platform_create_bundle(platform_driver,
3857                                                  asus_wmi_probe,
3858                                                  NULL, 0, NULL, 0);
3859         if (IS_ERR(platform_device))
3860                 return PTR_ERR(platform_device);
3861
3862         used = true;
3863         return 0;
3864 }
3865 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
3866
3867 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
3868 {
3869         platform_device_unregister(driver->platform_device);
3870         platform_driver_unregister(&driver->platform_driver);
3871         used = false;
3872 }
3873 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
3874
3875 static int __init asus_wmi_init(void)
3876 {
3877         pr_info("ASUS WMI generic driver loaded\n");
3878         return 0;
3879 }
3880
3881 static void __exit asus_wmi_exit(void)
3882 {
3883         pr_info("ASUS WMI generic driver unloaded\n");
3884 }
3885
3886 module_init(asus_wmi_init);
3887 module_exit(asus_wmi_exit);