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