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