root/drivers/platform/x86/asus-wmi.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ashs_present
  2. asus_wmi_input_init
  3. asus_wmi_input_exit
  4. asus_wmi_evaluate_method3
  5. asus_wmi_evaluate_method
  6. asus_wmi_evaluate_method_agfn
  7. asus_wmi_get_devstate
  8. asus_wmi_set_devstate
  9. asus_wmi_get_devstate_bits
  10. asus_wmi_get_devstate_simple
  11. asus_wmi_dev_is_present
  12. charge_control_end_threshold_store
  13. charge_control_end_threshold_show
  14. asus_wmi_battery_add
  15. asus_wmi_battery_remove
  16. asus_wmi_battery_init
  17. asus_wmi_battery_exit
  18. tpd_led_update
  19. tpd_led_set
  20. read_tpd_led_state
  21. tpd_led_get
  22. kbd_led_update
  23. kbd_led_read
  24. do_kbd_led_set
  25. kbd_led_set
  26. kbd_led_set_by_kbd
  27. kbd_led_get
  28. wlan_led_unknown_state
  29. wlan_led_update
  30. wlan_led_set
  31. wlan_led_get
  32. lightbar_led_update
  33. lightbar_led_set
  34. lightbar_led_get
  35. asus_wmi_led_exit
  36. asus_wmi_led_init
  37. asus_wlan_rfkill_blocked
  38. asus_rfkill_hotplug
  39. asus_rfkill_notify
  40. asus_register_rfkill_notifier
  41. asus_unregister_rfkill_notifier
  42. asus_get_adapter_status
  43. asus_hotplug_work
  44. asus_setup_pci_hotplug
  45. asus_rfkill_set
  46. asus_rfkill_query
  47. asus_rfkill_wlan_set
  48. asus_new_rfkill
  49. asus_wmi_rfkill_exit
  50. asus_wmi_rfkill_init
  51. asus_wmi_set_xusb2pr
  52. asus_wmi_set_als
  53. asus_agfn_fan_speed_read
  54. asus_agfn_fan_speed_write
  55. asus_wmi_has_agfn_fan
  56. asus_fan_set_auto
  57. pwm1_show
  58. pwm1_store
  59. fan1_input_show
  60. pwm1_enable_show
  61. pwm1_enable_store
  62. fan1_label_show
  63. asus_hwmon_temp1
  64. asus_hwmon_sysfs_is_visible
  65. asus_wmi_hwmon_init
  66. asus_wmi_fan_init
  67. fan_boost_mode_check_present
  68. fan_boost_mode_write
  69. fan_boost_mode_switch_next
  70. fan_boost_mode_show
  71. fan_boost_mode_store
  72. read_backlight_power
  73. read_brightness_max
  74. read_brightness
  75. get_scalar_command
  76. update_bl_status
  77. asus_wmi_backlight_notify
  78. asus_wmi_backlight_init
  79. asus_wmi_backlight_exit
  80. is_display_toggle
  81. asus_wmi_has_fnlock_key
  82. asus_wmi_fnlock_update
  83. asus_wmi_get_event_code
  84. asus_wmi_handle_event_code
  85. asus_wmi_notify
  86. asus_wmi_notify_queue_flush
  87. store_sys_wmi
  88. show_sys_wmi
  89. cpufv_store
  90. asus_sysfs_is_visible
  91. asus_wmi_sysfs_exit
  92. asus_wmi_sysfs_init
  93. asus_wmi_platform_init
  94. show_dsts
  95. show_devs
  96. show_call
  97. asus_wmi_debugfs_open
  98. asus_wmi_debugfs_exit
  99. asus_wmi_debugfs_init
  100. asus_wmi_add
  101. asus_wmi_remove
  102. asus_hotk_thaw
  103. asus_hotk_resume
  104. asus_hotk_restore
  105. asus_wmi_probe
  106. asus_wmi_register_driver
  107. asus_wmi_unregister_driver
  108. asus_wmi_init
  109. asus_wmi_exit

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

/* [<][>][^][v][top][bottom][index][help] */