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