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