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