1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * video.c - ACPI Video Driver 4 * 5 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com> 6 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org> 7 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net> 8 */ 9 10 #define pr_fmt(fmt) "ACPI: video: " fmt 11 12 #include <linux/auxiliary_bus.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/types.h> 17 #include <linux/list.h> 18 #include <linux/mutex.h> 19 #include <linux/input.h> 20 #include <linux/backlight.h> 21 #include <linux/thermal.h> 22 #include <linux/sort.h> 23 #include <linux/pci.h> 24 #include <linux/pci_ids.h> 25 #include <linux/slab.h> 26 #include <linux/dmi.h> 27 #include <linux/suspend.h> 28 #include <linux/acpi.h> 29 #include <acpi/video.h> 30 #include <linux/uaccess.h> 31 #include <linux/string_choices.h> 32 33 #define ACPI_VIDEO_BUS_NAME "Video Bus" 34 #define ACPI_VIDEO_DEVICE_NAME "Video Device" 35 36 #define MAX_NAME_LEN 20 37 38 MODULE_AUTHOR("Bruno Ducrot"); 39 MODULE_DESCRIPTION("ACPI Video Driver"); 40 MODULE_LICENSE("GPL"); 41 42 static bool brightness_switch_enabled = true; 43 module_param(brightness_switch_enabled, bool, 0644); 44 45 /* 46 * By default, we don't allow duplicate ACPI video bus devices 47 * under the same VGA controller 48 */ 49 static bool allow_duplicates; 50 module_param(allow_duplicates, bool, 0644); 51 52 #define REPORT_OUTPUT_KEY_EVENTS 0x01 53 #define REPORT_BRIGHTNESS_KEY_EVENTS 0x02 54 static int report_key_events = -1; 55 module_param(report_key_events, int, 0644); 56 MODULE_PARM_DESC(report_key_events, 57 "0: none, 1: output changes, 2: brightness changes, 3: all"); 58 59 static int hw_changes_brightness = -1; 60 module_param(hw_changes_brightness, int, 0644); 61 MODULE_PARM_DESC(hw_changes_brightness, 62 "Set this to 1 on buggy hw which changes the brightness itself when " 63 "a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness"); 64 65 /* 66 * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be 67 * assumed even if not actually set. 68 */ 69 static bool device_id_scheme = false; 70 module_param(device_id_scheme, bool, 0444); 71 72 static int only_lcd; 73 module_param(only_lcd, int, 0444); 74 75 static bool may_report_brightness_keys; 76 static int register_count; 77 static DEFINE_MUTEX(register_count_mutex); 78 static DEFINE_MUTEX(video_list_lock); 79 static LIST_HEAD(video_bus_head); 80 static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, 81 const struct auxiliary_device_id *id); 82 static void acpi_video_bus_remove(struct auxiliary_device *aux); 83 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data); 84 85 /* 86 * Indices in the _BCL method response: the first two items are special, 87 * the rest are all supported levels. 88 * 89 * See page 575 of the ACPI spec 3.0 90 */ 91 enum acpi_video_level_idx { 92 ACPI_VIDEO_AC_LEVEL, /* level when machine has full power */ 93 ACPI_VIDEO_BATTERY_LEVEL, /* level when machine is on batteries */ 94 ACPI_VIDEO_FIRST_LEVEL, /* actual supported levels begin here */ 95 }; 96 97 static const struct auxiliary_device_id video_bus_auxiliary_id_table[] = { 98 { .name = "acpi.video_bus" }, 99 {}, 100 }; 101 MODULE_DEVICE_TABLE(auxiliary, video_bus_auxiliary_id_table); 102 103 static struct auxiliary_driver acpi_video_bus = { 104 .probe = acpi_video_bus_probe, 105 .remove = acpi_video_bus_remove, 106 .id_table = video_bus_auxiliary_id_table, 107 }; 108 109 struct acpi_video_bus_flags { 110 u8 multihead:1; /* can switch video heads */ 111 u8 rom:1; /* can retrieve a video rom */ 112 u8 post:1; /* can configure the head to */ 113 u8 reserved:5; 114 }; 115 116 struct acpi_video_bus_cap { 117 u8 _DOS:1; /* Enable/Disable output switching */ 118 u8 _DOD:1; /* Enumerate all devices attached to display adapter */ 119 u8 _ROM:1; /* Get ROM Data */ 120 u8 _GPD:1; /* Get POST Device */ 121 u8 _SPD:1; /* Set POST Device */ 122 u8 _VPO:1; /* Video POST Options */ 123 u8 reserved:2; 124 }; 125 126 struct acpi_video_device_attrib { 127 u32 display_index:4; /* A zero-based instance of the Display */ 128 u32 display_port_attachment:4; /* This field differentiates the display type */ 129 u32 display_type:4; /* Describe the specific type in use */ 130 u32 vendor_specific:4; /* Chipset Vendor Specific */ 131 u32 bios_can_detect:1; /* BIOS can detect the device */ 132 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to 133 the VGA device. */ 134 u32 pipe_id:3; /* For VGA multiple-head devices. */ 135 u32 reserved:10; /* Must be 0 */ 136 137 /* 138 * The device ID might not actually follow the scheme described by this 139 * struct acpi_video_device_attrib. If it does, then this bit 140 * device_id_scheme is set; otherwise, other fields should be ignored. 141 * 142 * (but also see the global flag device_id_scheme) 143 */ 144 u32 device_id_scheme:1; 145 }; 146 147 struct acpi_video_enumerated_device { 148 union { 149 u32 int_val; 150 struct acpi_video_device_attrib attrib; 151 } value; 152 struct acpi_video_device *bind_info; 153 }; 154 155 struct acpi_video_bus { 156 struct acpi_device *device; 157 bool backlight_registered; 158 u8 dos_setting; 159 struct acpi_video_enumerated_device *attached_array; 160 u8 attached_count; 161 u8 child_count; 162 struct acpi_video_bus_cap cap; 163 struct acpi_video_bus_flags flags; 164 struct list_head video_device_list; 165 struct mutex device_list_lock; /* protects video_device_list */ 166 struct list_head entry; 167 struct input_dev *input; 168 char phys[32]; /* for input device */ 169 struct notifier_block pm_nb; 170 }; 171 172 struct acpi_video_device_flags { 173 u8 crt:1; 174 u8 lcd:1; 175 u8 tvout:1; 176 u8 dvi:1; 177 u8 bios:1; 178 u8 unknown:1; 179 u8 notify:1; 180 u8 reserved:1; 181 }; 182 183 struct acpi_video_device_cap { 184 u8 _ADR:1; /* Return the unique ID */ 185 u8 _BCL:1; /* Query list of brightness control levels supported */ 186 u8 _BCM:1; /* Set the brightness level */ 187 u8 _BQC:1; /* Get current brightness level */ 188 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */ 189 u8 _DDC:1; /* Return the EDID for this device */ 190 }; 191 192 struct acpi_video_device { 193 unsigned long device_id; 194 struct acpi_video_device_flags flags; 195 struct acpi_video_device_cap cap; 196 struct list_head entry; 197 struct delayed_work switch_brightness_work; 198 int switch_brightness_event; 199 struct acpi_video_bus *video; 200 struct acpi_device *dev; 201 struct acpi_video_device_brightness *brightness; 202 struct backlight_device *backlight; 203 struct thermal_cooling_device *cooling_dev; 204 }; 205 206 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data); 207 static void acpi_video_device_rebind(struct acpi_video_bus *video); 208 static void acpi_video_device_bind(struct acpi_video_bus *video, 209 struct acpi_video_device *device); 210 static int acpi_video_device_enumerate(struct acpi_video_bus *video); 211 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device, 212 int level); 213 static int acpi_video_device_lcd_get_level_current( 214 struct acpi_video_device *device, 215 unsigned long long *level, bool raw); 216 static int acpi_video_get_next_level(struct acpi_video_device *device, 217 u32 level_current, u32 event); 218 static void acpi_video_switch_brightness(struct work_struct *work); 219 220 /* backlight device sysfs support */ 221 static int acpi_video_get_brightness(struct backlight_device *bd) 222 { 223 unsigned long long cur_level; 224 int i; 225 struct acpi_video_device *vd = bl_get_data(bd); 226 227 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false)) 228 return -EINVAL; 229 for (i = ACPI_VIDEO_FIRST_LEVEL; i < vd->brightness->count; i++) { 230 if (vd->brightness->levels[i] == cur_level) 231 return i - ACPI_VIDEO_FIRST_LEVEL; 232 } 233 return 0; 234 } 235 236 static int acpi_video_set_brightness(struct backlight_device *bd) 237 { 238 int request_level = bd->props.brightness + ACPI_VIDEO_FIRST_LEVEL; 239 struct acpi_video_device *vd = bl_get_data(bd); 240 241 cancel_delayed_work(&vd->switch_brightness_work); 242 return acpi_video_device_lcd_set_level(vd, 243 vd->brightness->levels[request_level]); 244 } 245 246 static const struct backlight_ops acpi_backlight_ops = { 247 .get_brightness = acpi_video_get_brightness, 248 .update_status = acpi_video_set_brightness, 249 }; 250 251 /* thermal cooling device callbacks */ 252 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, 253 unsigned long *state) 254 { 255 struct acpi_video_device *video = cooling_dev->devdata; 256 257 *state = video->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1; 258 return 0; 259 } 260 261 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, 262 unsigned long *state) 263 { 264 struct acpi_video_device *video = cooling_dev->devdata; 265 unsigned long long level; 266 int offset; 267 268 if (acpi_video_device_lcd_get_level_current(video, &level, false)) 269 return -EINVAL; 270 for (offset = ACPI_VIDEO_FIRST_LEVEL; offset < video->brightness->count; 271 offset++) 272 if (level == video->brightness->levels[offset]) { 273 *state = video->brightness->count - offset - 1; 274 return 0; 275 } 276 277 return -EINVAL; 278 } 279 280 static int 281 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state) 282 { 283 struct acpi_video_device *video = cooling_dev->devdata; 284 int level; 285 286 if (state >= video->brightness->count - ACPI_VIDEO_FIRST_LEVEL) 287 return -EINVAL; 288 289 state = video->brightness->count - state; 290 level = video->brightness->levels[state - 1]; 291 return acpi_video_device_lcd_set_level(video, level); 292 } 293 294 static const struct thermal_cooling_device_ops video_cooling_ops = { 295 .get_max_state = video_get_max_state, 296 .get_cur_state = video_get_cur_state, 297 .set_cur_state = video_set_cur_state, 298 }; 299 300 /* 301 * -------------------------------------------------------------------------- 302 * Video Management 303 * -------------------------------------------------------------------------- 304 */ 305 306 static int 307 acpi_video_device_lcd_query_levels(acpi_handle handle, 308 union acpi_object **levels) 309 { 310 int status; 311 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 312 union acpi_object *obj; 313 314 315 *levels = NULL; 316 317 status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer); 318 if (ACPI_FAILURE(status)) 319 return status; 320 obj = (union acpi_object *)buffer.pointer; 321 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) { 322 acpi_handle_info(handle, "Invalid _BCL data\n"); 323 status = -EFAULT; 324 goto err; 325 } 326 327 *levels = obj; 328 329 return 0; 330 331 err: 332 kfree(buffer.pointer); 333 334 return status; 335 } 336 337 static int 338 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level) 339 { 340 int status; 341 int state; 342 343 status = acpi_execute_simple_method(device->dev->handle, 344 "_BCM", level); 345 if (ACPI_FAILURE(status)) { 346 acpi_handle_info(device->dev->handle, "_BCM evaluation failed\n"); 347 return -EIO; 348 } 349 350 device->brightness->curr = level; 351 for (state = ACPI_VIDEO_FIRST_LEVEL; state < device->brightness->count; 352 state++) 353 if (level == device->brightness->levels[state]) { 354 if (device->backlight) 355 device->backlight->props.brightness = 356 state - ACPI_VIDEO_FIRST_LEVEL; 357 return 0; 358 } 359 360 acpi_handle_info(device->dev->handle, "Current brightness invalid\n"); 361 return -EINVAL; 362 } 363 364 /* 365 * For some buggy _BQC methods, we need to add a constant value to 366 * the _BQC return value to get the actual current brightness level 367 */ 368 369 static int bqc_offset_aml_bug_workaround; 370 static int video_set_bqc_offset(const struct dmi_system_id *d) 371 { 372 bqc_offset_aml_bug_workaround = 9; 373 return 0; 374 } 375 376 static int video_set_device_id_scheme(const struct dmi_system_id *d) 377 { 378 device_id_scheme = true; 379 return 0; 380 } 381 382 static int video_enable_only_lcd(const struct dmi_system_id *d) 383 { 384 only_lcd = true; 385 return 0; 386 } 387 388 static int video_set_report_key_events(const struct dmi_system_id *id) 389 { 390 if (report_key_events == -1) 391 report_key_events = (uintptr_t)id->driver_data; 392 return 0; 393 } 394 395 static int video_hw_changes_brightness( 396 const struct dmi_system_id *d) 397 { 398 if (hw_changes_brightness == -1) 399 hw_changes_brightness = 1; 400 return 0; 401 } 402 403 static const struct dmi_system_id video_dmi_table[] = { 404 /* 405 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121 406 */ 407 { 408 .callback = video_set_bqc_offset, 409 .ident = "Acer Aspire 5720", 410 .matches = { 411 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"), 412 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"), 413 }, 414 }, 415 { 416 .callback = video_set_bqc_offset, 417 .ident = "Acer Aspire 5710Z", 418 .matches = { 419 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"), 420 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"), 421 }, 422 }, 423 { 424 .callback = video_set_bqc_offset, 425 .ident = "eMachines E510", 426 .matches = { 427 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"), 428 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"), 429 }, 430 }, 431 { 432 .callback = video_set_bqc_offset, 433 .ident = "Acer Aspire 5315", 434 .matches = { 435 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"), 436 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"), 437 }, 438 }, 439 { 440 .callback = video_set_bqc_offset, 441 .ident = "Acer Aspire 7720", 442 .matches = { 443 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"), 444 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"), 445 }, 446 }, 447 448 /* 449 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set 450 * but the IDs actually follow the Device ID Scheme. 451 */ 452 { 453 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */ 454 .callback = video_set_device_id_scheme, 455 .ident = "ESPRIMO Mobile M9410", 456 .matches = { 457 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 458 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"), 459 }, 460 }, 461 /* 462 * Some machines have multiple video output devices, but only the one 463 * that is the type of LCD can do the backlight control so we should not 464 * register backlight interface for other video output devices. 465 */ 466 { 467 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */ 468 .callback = video_enable_only_lcd, 469 .ident = "ESPRIMO Mobile M9410", 470 .matches = { 471 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), 472 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"), 473 }, 474 }, 475 /* 476 * Some machines report wrong key events on the acpi-bus, suppress 477 * key event reporting on these. Note this is only intended to work 478 * around events which are plain wrong. In some cases we get double 479 * events, in this case acpi-video is considered the canonical source 480 * and the events from the other source should be filtered. E.g. 481 * by calling acpi_video_handles_brightness_key_presses() from the 482 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb 483 */ 484 { 485 .callback = video_set_report_key_events, 486 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS), 487 .ident = "Dell Vostro V131", 488 .matches = { 489 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 490 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"), 491 }, 492 }, 493 { 494 .callback = video_set_report_key_events, 495 .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS), 496 .ident = "Dell Vostro 3350", 497 .matches = { 498 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 499 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"), 500 }, 501 }, 502 { 503 .callback = video_set_report_key_events, 504 .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS), 505 .ident = "COLORFUL X15 AT 23", 506 .matches = { 507 DMI_MATCH(DMI_SYS_VENDOR, "COLORFUL"), 508 DMI_MATCH(DMI_PRODUCT_NAME, "X15 AT 23"), 509 }, 510 }, 511 /* 512 * Some machines change the brightness themselves when a brightness 513 * hotkey gets pressed, despite us telling them not to. In this case 514 * acpi_video_device_notify() should only call backlight_force_update( 515 * BACKLIGHT_UPDATE_HOTKEY) and not do anything else. 516 */ 517 { 518 /* https://bugzilla.kernel.org/show_bug.cgi?id=204077 */ 519 .callback = video_hw_changes_brightness, 520 .ident = "Packard Bell EasyNote MZ35", 521 .matches = { 522 DMI_MATCH(DMI_SYS_VENDOR, "Packard Bell"), 523 DMI_MATCH(DMI_PRODUCT_NAME, "EasyNote MZ35"), 524 }, 525 }, 526 {} 527 }; 528 529 static unsigned long long 530 acpi_video_bqc_value_to_level(struct acpi_video_device *device, 531 unsigned long long bqc_value) 532 { 533 unsigned long long level; 534 535 if (device->brightness->flags._BQC_use_index) { 536 /* 537 * _BQC returns an index that doesn't account for the first 2 538 * items with special meaning (see enum acpi_video_level_idx), 539 * so we need to compensate for that by offsetting ourselves 540 */ 541 if (device->brightness->flags._BCL_reversed) 542 bqc_value = device->brightness->count - 543 ACPI_VIDEO_FIRST_LEVEL - 1 - bqc_value; 544 545 level = device->brightness->levels[bqc_value + 546 ACPI_VIDEO_FIRST_LEVEL]; 547 } else { 548 level = bqc_value; 549 } 550 551 level += bqc_offset_aml_bug_workaround; 552 553 return level; 554 } 555 556 static int 557 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device, 558 unsigned long long *level, bool raw) 559 { 560 acpi_status status = AE_OK; 561 int i; 562 563 if (device->cap._BQC || device->cap._BCQ) { 564 char *buf = device->cap._BQC ? "_BQC" : "_BCQ"; 565 566 status = acpi_evaluate_integer(device->dev->handle, buf, 567 NULL, level); 568 if (ACPI_SUCCESS(status)) { 569 if (raw) { 570 /* 571 * Caller has indicated he wants the raw 572 * value returned by _BQC, so don't furtherly 573 * mess with the value. 574 */ 575 return 0; 576 } 577 578 *level = acpi_video_bqc_value_to_level(device, *level); 579 580 for (i = ACPI_VIDEO_FIRST_LEVEL; 581 i < device->brightness->count; i++) 582 if (device->brightness->levels[i] == *level) { 583 device->brightness->curr = *level; 584 return 0; 585 } 586 /* 587 * BQC returned an invalid level. 588 * Stop using it. 589 */ 590 acpi_handle_info(device->dev->handle, 591 "%s returned an invalid level", buf); 592 device->cap._BQC = device->cap._BCQ = 0; 593 } else { 594 /* 595 * Fixme: 596 * should we return an error or ignore this failure? 597 * dev->brightness->curr is a cached value which stores 598 * the correct current backlight level in most cases. 599 * ACPI video backlight still works w/ buggy _BQC. 600 * http://bugzilla.kernel.org/show_bug.cgi?id=12233 601 */ 602 acpi_handle_info(device->dev->handle, 603 "%s evaluation failed", buf); 604 device->cap._BQC = device->cap._BCQ = 0; 605 } 606 } 607 608 *level = device->brightness->curr; 609 return 0; 610 } 611 612 /** 613 * acpi_video_device_EDID() - Get EDID from ACPI _DDC 614 * @device: video output device (LCD, CRT, ..) 615 * @edid: address for returned EDID pointer 616 * @length: _DDC length to request (must be a multiple of 128) 617 * 618 * Get EDID from ACPI _DDC. On success, a pointer to the EDID data is written 619 * to the @edid address, and the length of the EDID is returned. The caller is 620 * responsible for freeing the edid pointer. 621 * 622 * Return the length of EDID (positive value) on success or error (negative 623 * value). 624 */ 625 static int 626 acpi_video_device_EDID(struct acpi_video_device *device, void **edid, int length) 627 { 628 acpi_status status; 629 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 630 union acpi_object *obj; 631 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 632 struct acpi_object_list args = { 1, &arg0 }; 633 int ret; 634 635 *edid = NULL; 636 637 if (!device) 638 return -ENODEV; 639 if (!length || (length % 128)) 640 return -EINVAL; 641 642 arg0.integer.value = length / 128; 643 644 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer); 645 if (ACPI_FAILURE(status)) 646 return -ENODEV; 647 648 obj = buffer.pointer; 649 650 /* 651 * Some buggy implementations incorrectly return the EDID buffer in an ACPI package. 652 * In this case, extract the buffer from the package. 653 */ 654 if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 1) 655 obj = &obj->package.elements[0]; 656 657 if (obj && obj->type == ACPI_TYPE_BUFFER) { 658 *edid = kmemdup(obj->buffer.pointer, obj->buffer.length, GFP_KERNEL); 659 ret = *edid ? obj->buffer.length : -ENOMEM; 660 } else { 661 acpi_handle_debug(device->dev->handle, 662 "Invalid _DDC data for length %d\n", length); 663 ret = -EFAULT; 664 } 665 666 kfree(buffer.pointer); 667 return ret; 668 } 669 670 /* bus */ 671 672 /* 673 * Arg: 674 * video : video bus device pointer 675 * bios_flag : 676 * 0. The system BIOS should NOT automatically switch(toggle) 677 * the active display output. 678 * 1. The system BIOS should automatically switch (toggle) the 679 * active display output. No switch event. 680 * 2. The _DGS value should be locked. 681 * 3. The system BIOS should not automatically switch (toggle) the 682 * active display output, but instead generate the display switch 683 * event notify code. 684 * lcd_flag : 685 * 0. The system BIOS should automatically control the brightness level 686 * of the LCD when: 687 * - the power changes from AC to DC (ACPI appendix B) 688 * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs) 689 * 1. The system BIOS should NOT automatically control the brightness 690 * level of the LCD when: 691 * - the power changes from AC to DC (ACPI appendix B) 692 * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs) 693 * Return Value: 694 * -EINVAL wrong arg. 695 */ 696 697 static int 698 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag) 699 { 700 acpi_status status; 701 702 if (!video->cap._DOS) 703 return 0; 704 705 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) 706 return -EINVAL; 707 video->dos_setting = (lcd_flag << 2) | bios_flag; 708 status = acpi_execute_simple_method(video->device->handle, "_DOS", 709 (lcd_flag << 2) | bios_flag); 710 if (ACPI_FAILURE(status)) 711 return -EIO; 712 713 return 0; 714 } 715 716 /* 717 * Simple comparison function used to sort backlight levels. 718 */ 719 720 static int 721 acpi_video_cmp_level(const void *a, const void *b) 722 { 723 return *(int *)a - *(int *)b; 724 } 725 726 /* 727 * Decides if _BQC/_BCQ for this system is usable 728 * 729 * We do this by changing the level first and then read out the current 730 * brightness level, if the value does not match, find out if it is using 731 * index. If not, clear the _BQC/_BCQ capability. 732 */ 733 static int acpi_video_bqc_quirk(struct acpi_video_device *device, 734 int max_level, int current_level) 735 { 736 struct acpi_video_device_brightness *br = device->brightness; 737 int result; 738 unsigned long long level; 739 int test_level; 740 741 /* don't mess with existing known broken systems */ 742 if (bqc_offset_aml_bug_workaround) 743 return 0; 744 745 /* 746 * Some systems always report current brightness level as maximum 747 * through _BQC, we need to test another value for them. However, 748 * there is a subtlety: 749 * 750 * If the _BCL package ordering is descending, the first level 751 * (br->levels[2]) is likely to be 0, and if the number of levels 752 * matches the number of steps, we might confuse a returned level to 753 * mean the index. 754 * 755 * For example: 756 * 757 * current_level = max_level = 100 758 * test_level = 0 759 * returned level = 100 760 * 761 * In this case 100 means the level, not the index, and _BCM failed. 762 * Still, if the _BCL package ordering is descending, the index of 763 * level 0 is also 100, so we assume _BQC is indexed, when it's not. 764 * 765 * This causes all _BQC calls to return bogus values causing weird 766 * behavior from the user's perspective. For example: 767 * 768 * xbacklight -set 10; xbacklight -set 20; 769 * 770 * would flash to 90% and then slowly down to the desired level (20). 771 * 772 * The solution is simple; test anything other than the first level 773 * (e.g. 1). 774 */ 775 test_level = current_level == max_level 776 ? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1] 777 : max_level; 778 779 result = acpi_video_device_lcd_set_level(device, test_level); 780 if (result) 781 return result; 782 783 result = acpi_video_device_lcd_get_level_current(device, &level, true); 784 if (result) 785 return result; 786 787 if (level != test_level) { 788 /* buggy _BQC found, need to find out if it uses index */ 789 if (level < br->count) { 790 if (br->flags._BCL_reversed) 791 level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level; 792 if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level) 793 br->flags._BQC_use_index = 1; 794 } 795 796 if (!br->flags._BQC_use_index) 797 device->cap._BQC = device->cap._BCQ = 0; 798 } 799 800 return 0; 801 } 802 803 int acpi_video_get_levels(struct acpi_device *device, 804 struct acpi_video_device_brightness **dev_br, 805 int *pmax_level) 806 { 807 union acpi_object *obj = NULL; 808 int i, max_level = 0, count = 0, level_ac_battery = 0; 809 union acpi_object *o; 810 struct acpi_video_device_brightness *br = NULL; 811 int result = 0; 812 u32 value; 813 814 if (ACPI_FAILURE(acpi_video_device_lcd_query_levels(device->handle, &obj))) { 815 acpi_handle_debug(device->handle, 816 "Could not query available LCD brightness level\n"); 817 result = -ENODEV; 818 goto out; 819 } 820 821 if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) { 822 result = -EINVAL; 823 goto out; 824 } 825 826 br = kzalloc_obj(*br); 827 if (!br) { 828 result = -ENOMEM; 829 goto out; 830 } 831 832 /* 833 * Note that we have to reserve 2 extra items (ACPI_VIDEO_FIRST_LEVEL), 834 * in order to account for buggy BIOS which don't export the first two 835 * special levels (see below) 836 */ 837 br->levels = kmalloc_objs(*br->levels, 838 obj->package.count + ACPI_VIDEO_FIRST_LEVEL); 839 if (!br->levels) { 840 result = -ENOMEM; 841 goto out_free; 842 } 843 844 for (i = 0; i < obj->package.count; i++) { 845 o = (union acpi_object *)&obj->package.elements[i]; 846 if (o->type != ACPI_TYPE_INTEGER) { 847 acpi_handle_info(device->handle, "Invalid data\n"); 848 continue; 849 } 850 value = (u32) o->integer.value; 851 /* Skip duplicate entries */ 852 if (count > ACPI_VIDEO_FIRST_LEVEL 853 && br->levels[count - 1] == value) 854 continue; 855 856 br->levels[count] = value; 857 858 if (br->levels[count] > max_level) 859 max_level = br->levels[count]; 860 count++; 861 } 862 863 /* 864 * some buggy BIOS don't export the levels 865 * when machine is on AC/Battery in _BCL package. 866 * In this case, the first two elements in _BCL packages 867 * are also supported brightness levels that OS should take care of. 868 */ 869 for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) { 870 if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL]) 871 level_ac_battery++; 872 if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL]) 873 level_ac_battery++; 874 } 875 876 if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) { 877 level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery; 878 br->flags._BCL_no_ac_battery_levels = 1; 879 for (i = (count - 1 + level_ac_battery); 880 i >= ACPI_VIDEO_FIRST_LEVEL; i--) 881 br->levels[i] = br->levels[i - level_ac_battery]; 882 count += level_ac_battery; 883 } else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL) 884 acpi_handle_info(device->handle, 885 "Too many duplicates in _BCL package"); 886 887 /* Check if the _BCL package is in a reversed order */ 888 if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) { 889 br->flags._BCL_reversed = 1; 890 sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL], 891 count - ACPI_VIDEO_FIRST_LEVEL, 892 sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]), 893 acpi_video_cmp_level, NULL); 894 } else if (max_level != br->levels[count - 1]) 895 acpi_handle_info(device->handle, 896 "Found unordered _BCL package"); 897 898 br->count = count; 899 *dev_br = br; 900 if (pmax_level) 901 *pmax_level = max_level; 902 903 out: 904 kfree(obj); 905 return result; 906 out_free: 907 kfree(br); 908 goto out; 909 } 910 EXPORT_SYMBOL(acpi_video_get_levels); 911 912 /* 913 * Arg: 914 * device : video output device (LCD, CRT, ..) 915 * 916 * Return Value: 917 * Maximum brightness level 918 * 919 * Allocate and initialize device->brightness. 920 */ 921 922 static int 923 acpi_video_init_brightness(struct acpi_video_device *device) 924 { 925 int i, max_level = 0; 926 unsigned long long level, level_old; 927 struct acpi_video_device_brightness *br = NULL; 928 int result; 929 930 result = acpi_video_get_levels(device->dev, &br, &max_level); 931 if (result) 932 return result; 933 device->brightness = br; 934 935 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */ 936 br->curr = level = max_level; 937 938 if (!device->cap._BQC) 939 goto set_level; 940 941 result = acpi_video_device_lcd_get_level_current(device, 942 &level_old, true); 943 if (result) 944 goto out_free_levels; 945 946 result = acpi_video_bqc_quirk(device, max_level, level_old); 947 if (result) 948 goto out_free_levels; 949 /* 950 * cap._BQC may get cleared due to _BQC is found to be broken 951 * in acpi_video_bqc_quirk, so check again here. 952 */ 953 if (!device->cap._BQC) 954 goto set_level; 955 956 level = acpi_video_bqc_value_to_level(device, level_old); 957 /* 958 * On some buggy laptops, _BQC returns an uninitialized 959 * value when invoked for the first time, i.e. 960 * level_old is invalid (no matter whether it's a level 961 * or an index). Set the backlight to max_level in this case. 962 */ 963 for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++) 964 if (level == br->levels[i]) 965 break; 966 if (i == br->count || !level) 967 level = max_level; 968 969 set_level: 970 result = acpi_video_device_lcd_set_level(device, level); 971 if (result) 972 goto out_free_levels; 973 974 acpi_handle_debug(device->dev->handle, "found %d brightness levels\n", 975 br->count - ACPI_VIDEO_FIRST_LEVEL); 976 977 return 0; 978 979 out_free_levels: 980 kfree(br->levels); 981 kfree(br); 982 device->brightness = NULL; 983 return result; 984 } 985 986 /* 987 * Arg: 988 * device : video output device (LCD, CRT, ..) 989 * 990 * Return Value: 991 * None 992 * 993 * Find out all required AML methods defined under the output 994 * device. 995 */ 996 997 static void acpi_video_device_find_cap(struct acpi_video_device *device) 998 { 999 if (acpi_has_method(device->dev->handle, "_ADR")) 1000 device->cap._ADR = 1; 1001 if (acpi_has_method(device->dev->handle, "_BCL")) 1002 device->cap._BCL = 1; 1003 if (acpi_has_method(device->dev->handle, "_BCM")) 1004 device->cap._BCM = 1; 1005 if (acpi_has_method(device->dev->handle, "_BQC")) { 1006 device->cap._BQC = 1; 1007 } else if (acpi_has_method(device->dev->handle, "_BCQ")) { 1008 acpi_handle_info(device->dev->handle, 1009 "_BCQ is used instead of _BQC\n"); 1010 device->cap._BCQ = 1; 1011 } 1012 1013 if (acpi_has_method(device->dev->handle, "_DDC")) 1014 device->cap._DDC = 1; 1015 } 1016 1017 /* 1018 * Arg: 1019 * device : video output device (VGA) 1020 * 1021 * Return Value: 1022 * None 1023 * 1024 * Find out all required AML methods defined under the video bus device. 1025 */ 1026 1027 static void acpi_video_bus_find_cap(struct acpi_video_bus *video) 1028 { 1029 if (acpi_has_method(video->device->handle, "_DOS")) 1030 video->cap._DOS = 1; 1031 if (acpi_has_method(video->device->handle, "_DOD")) 1032 video->cap._DOD = 1; 1033 if (acpi_has_method(video->device->handle, "_ROM")) 1034 video->cap._ROM = 1; 1035 if (acpi_has_method(video->device->handle, "_GPD")) 1036 video->cap._GPD = 1; 1037 if (acpi_has_method(video->device->handle, "_SPD")) 1038 video->cap._SPD = 1; 1039 if (acpi_has_method(video->device->handle, "_VPO")) 1040 video->cap._VPO = 1; 1041 } 1042 1043 /* 1044 * Check whether the video bus device has required AML method to 1045 * support the desired features 1046 */ 1047 1048 static int acpi_video_bus_check(struct acpi_video_bus *video) 1049 { 1050 acpi_status status = -ENOENT; 1051 struct pci_dev *dev; 1052 1053 if (!video) 1054 return -EINVAL; 1055 1056 dev = acpi_get_pci_dev(video->device->handle); 1057 if (!dev) 1058 return -ENODEV; 1059 pci_dev_put(dev); 1060 1061 /* 1062 * Since there is no HID, CID and so on for VGA driver, we have 1063 * to check well known required nodes. 1064 */ 1065 1066 /* Does this device support video switching? */ 1067 if (video->cap._DOS || video->cap._DOD) { 1068 if (!video->cap._DOS) { 1069 pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n", 1070 acpi_device_bid(video->device)); 1071 } 1072 video->flags.multihead = 1; 1073 status = 0; 1074 } 1075 1076 /* Does this device support retrieving a video ROM? */ 1077 if (video->cap._ROM) { 1078 video->flags.rom = 1; 1079 status = 0; 1080 } 1081 1082 /* Does this device support configuring which video device to POST? */ 1083 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) { 1084 video->flags.post = 1; 1085 status = 0; 1086 } 1087 1088 return status; 1089 } 1090 1091 /* 1092 * -------------------------------------------------------------------------- 1093 * Driver Interface 1094 * -------------------------------------------------------------------------- 1095 */ 1096 1097 /* device interface */ 1098 static struct acpi_video_device_attrib * 1099 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id) 1100 { 1101 struct acpi_video_enumerated_device *ids; 1102 int i; 1103 1104 for (i = 0; i < video->attached_count; i++) { 1105 ids = &video->attached_array[i]; 1106 if ((ids->value.int_val & 0xffff) == device_id) 1107 return &ids->value.attrib; 1108 } 1109 1110 return NULL; 1111 } 1112 1113 static int 1114 acpi_video_get_device_type(struct acpi_video_bus *video, 1115 unsigned long device_id) 1116 { 1117 struct acpi_video_enumerated_device *ids; 1118 int i; 1119 1120 for (i = 0; i < video->attached_count; i++) { 1121 ids = &video->attached_array[i]; 1122 if ((ids->value.int_val & 0xffff) == device_id) 1123 return ids->value.int_val; 1124 } 1125 1126 return 0; 1127 } 1128 1129 static int acpi_video_bus_get_one_device(struct acpi_device *device, void *arg) 1130 { 1131 struct acpi_video_bus *video = arg; 1132 struct acpi_video_device_attrib *attribute; 1133 struct acpi_video_device *data; 1134 int device_type; 1135 u64 device_id; 1136 1137 /* Skip devices without _ADR instead of failing. */ 1138 if (acpi_get_local_u64_address(device->handle, &device_id)) 1139 goto exit; 1140 1141 data = kzalloc_obj(struct acpi_video_device); 1142 if (!data) { 1143 dev_dbg(&device->dev, "Cannot attach\n"); 1144 return -ENOMEM; 1145 } 1146 1147 strscpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME); 1148 strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS); 1149 1150 data->device_id = device_id; 1151 data->video = video; 1152 data->dev = device; 1153 INIT_DELAYED_WORK(&data->switch_brightness_work, 1154 acpi_video_switch_brightness); 1155 1156 attribute = acpi_video_get_device_attr(video, device_id); 1157 1158 if (attribute && (attribute->device_id_scheme || device_id_scheme)) { 1159 switch (attribute->display_type) { 1160 case ACPI_VIDEO_DISPLAY_CRT: 1161 data->flags.crt = 1; 1162 break; 1163 case ACPI_VIDEO_DISPLAY_TV: 1164 data->flags.tvout = 1; 1165 break; 1166 case ACPI_VIDEO_DISPLAY_DVI: 1167 data->flags.dvi = 1; 1168 break; 1169 case ACPI_VIDEO_DISPLAY_LCD: 1170 data->flags.lcd = 1; 1171 break; 1172 default: 1173 data->flags.unknown = 1; 1174 break; 1175 } 1176 if (attribute->bios_can_detect) 1177 data->flags.bios = 1; 1178 } else { 1179 /* Check for legacy IDs */ 1180 device_type = acpi_video_get_device_type(video, device_id); 1181 /* Ignore bits 16 and 18-20 */ 1182 switch (device_type & 0xffe2ffff) { 1183 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR: 1184 data->flags.crt = 1; 1185 break; 1186 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL: 1187 data->flags.lcd = 1; 1188 break; 1189 case ACPI_VIDEO_DISPLAY_LEGACY_TV: 1190 data->flags.tvout = 1; 1191 break; 1192 default: 1193 data->flags.unknown = 1; 1194 } 1195 } 1196 1197 acpi_video_device_bind(video, data); 1198 acpi_video_device_find_cap(data); 1199 1200 if (data->cap._BCM && data->cap._BCL) 1201 may_report_brightness_keys = true; 1202 1203 mutex_lock(&video->device_list_lock); 1204 list_add_tail(&data->entry, &video->video_device_list); 1205 mutex_unlock(&video->device_list_lock); 1206 1207 exit: 1208 video->child_count++; 1209 return 0; 1210 } 1211 1212 /* 1213 * Arg: 1214 * video : video bus device 1215 * 1216 * Return: 1217 * none 1218 * 1219 * Enumerate the video device list of the video bus, 1220 * bind the ids with the corresponding video devices 1221 * under the video bus. 1222 */ 1223 1224 static void acpi_video_device_rebind(struct acpi_video_bus *video) 1225 { 1226 struct acpi_video_device *dev; 1227 1228 mutex_lock(&video->device_list_lock); 1229 1230 list_for_each_entry(dev, &video->video_device_list, entry) 1231 acpi_video_device_bind(video, dev); 1232 1233 mutex_unlock(&video->device_list_lock); 1234 } 1235 1236 /* 1237 * Arg: 1238 * video : video bus device 1239 * device : video output device under the video 1240 * bus 1241 * 1242 * Return: 1243 * none 1244 * 1245 * Bind the ids with the corresponding video devices 1246 * under the video bus. 1247 */ 1248 1249 static void 1250 acpi_video_device_bind(struct acpi_video_bus *video, 1251 struct acpi_video_device *device) 1252 { 1253 struct acpi_video_enumerated_device *ids; 1254 int i; 1255 1256 for (i = 0; i < video->attached_count; i++) { 1257 ids = &video->attached_array[i]; 1258 if (device->device_id == (ids->value.int_val & 0xffff)) { 1259 ids->bind_info = device; 1260 acpi_handle_debug(video->device->handle, "%s: %d\n", 1261 __func__, i); 1262 } 1263 } 1264 } 1265 1266 static bool acpi_video_device_in_dod(struct acpi_video_device *device) 1267 { 1268 struct acpi_video_bus *video = device->video; 1269 int i; 1270 1271 /* 1272 * If we have a broken _DOD or we have more than 8 output devices 1273 * under the graphics controller node that we can't proper deal with 1274 * in the operation region code currently, no need to test. 1275 */ 1276 if (!video->attached_count || video->child_count > 8) 1277 return true; 1278 1279 for (i = 0; i < video->attached_count; i++) { 1280 if ((video->attached_array[i].value.int_val & 0xfff) == 1281 (device->device_id & 0xfff)) 1282 return true; 1283 } 1284 1285 return false; 1286 } 1287 1288 /* 1289 * Arg: 1290 * video : video bus device 1291 * 1292 * Return: 1293 * < 0 : error 1294 * 1295 * Call _DOD to enumerate all devices attached to display adapter 1296 * 1297 */ 1298 1299 static int acpi_video_device_enumerate(struct acpi_video_bus *video) 1300 { 1301 int status; 1302 int count; 1303 int i; 1304 struct acpi_video_enumerated_device *active_list; 1305 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1306 union acpi_object *dod = NULL; 1307 union acpi_object *obj; 1308 1309 if (!video->cap._DOD) 1310 return AE_NOT_EXIST; 1311 1312 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer); 1313 if (ACPI_FAILURE(status)) { 1314 acpi_handle_info(video->device->handle, 1315 "_DOD evaluation failed: %s\n", 1316 acpi_format_exception(status)); 1317 return status; 1318 } 1319 1320 dod = buffer.pointer; 1321 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) { 1322 acpi_handle_info(video->device->handle, "Invalid _DOD data\n"); 1323 status = -EFAULT; 1324 goto out; 1325 } 1326 1327 acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n", 1328 dod->package.count); 1329 1330 active_list = kzalloc_objs(struct acpi_video_enumerated_device, 1331 1 + dod->package.count); 1332 if (!active_list) { 1333 status = -ENOMEM; 1334 goto out; 1335 } 1336 1337 count = 0; 1338 for (i = 0; i < dod->package.count; i++) { 1339 obj = &dod->package.elements[i]; 1340 1341 if (obj->type != ACPI_TYPE_INTEGER) { 1342 acpi_handle_info(video->device->handle, 1343 "Invalid _DOD data in element %d\n", i); 1344 continue; 1345 } 1346 1347 active_list[count].value.int_val = obj->integer.value; 1348 active_list[count].bind_info = NULL; 1349 1350 acpi_handle_debug(video->device->handle, 1351 "_DOD element[%d] = %d\n", i, 1352 (int)obj->integer.value); 1353 1354 count++; 1355 } 1356 1357 kfree(video->attached_array); 1358 1359 video->attached_array = active_list; 1360 video->attached_count = count; 1361 1362 out: 1363 kfree(buffer.pointer); 1364 return status; 1365 } 1366 1367 static int 1368 acpi_video_get_next_level(struct acpi_video_device *device, 1369 u32 level_current, u32 event) 1370 { 1371 int min, max, min_above, max_below, i, l, delta = 255; 1372 max = max_below = 0; 1373 min = min_above = 255; 1374 /* Find closest level to level_current */ 1375 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) { 1376 l = device->brightness->levels[i]; 1377 if (abs(l - level_current) < abs(delta)) { 1378 delta = l - level_current; 1379 if (!delta) 1380 break; 1381 } 1382 } 1383 /* Adjust level_current to closest available level */ 1384 level_current += delta; 1385 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) { 1386 l = device->brightness->levels[i]; 1387 if (l < min) 1388 min = l; 1389 if (l > max) 1390 max = l; 1391 if (l < min_above && l > level_current) 1392 min_above = l; 1393 if (l > max_below && l < level_current) 1394 max_below = l; 1395 } 1396 1397 switch (event) { 1398 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: 1399 return (level_current < max) ? min_above : min; 1400 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: 1401 return (level_current < max) ? min_above : max; 1402 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: 1403 return (level_current > min) ? max_below : min; 1404 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: 1405 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: 1406 return 0; 1407 default: 1408 return level_current; 1409 } 1410 } 1411 1412 static void 1413 acpi_video_switch_brightness(struct work_struct *work) 1414 { 1415 struct acpi_video_device *device = container_of(to_delayed_work(work), 1416 struct acpi_video_device, switch_brightness_work); 1417 unsigned long long level_current, level_next; 1418 int event = device->switch_brightness_event; 1419 int result = -EINVAL; 1420 1421 /* no warning message if acpi_backlight=vendor or a quirk is used */ 1422 if (!device->backlight) 1423 return; 1424 1425 if (!device->brightness) 1426 goto out; 1427 1428 result = acpi_video_device_lcd_get_level_current(device, 1429 &level_current, 1430 false); 1431 if (result) 1432 goto out; 1433 1434 level_next = acpi_video_get_next_level(device, level_current, event); 1435 1436 result = acpi_video_device_lcd_set_level(device, level_next); 1437 1438 if (!result) 1439 backlight_force_update(device->backlight, 1440 BACKLIGHT_UPDATE_HOTKEY); 1441 1442 out: 1443 if (result) 1444 acpi_handle_info(device->dev->handle, 1445 "Failed to switch brightness\n"); 1446 } 1447 1448 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id, 1449 void **edid) 1450 { 1451 struct acpi_video_bus *video; 1452 struct acpi_video_device *video_device; 1453 int i, length, ret; 1454 1455 if (!device || !acpi_driver_data(device)) 1456 return -EINVAL; 1457 1458 video = acpi_driver_data(device); 1459 1460 for (i = 0; i < video->attached_count; i++) { 1461 video_device = video->attached_array[i].bind_info; 1462 1463 if (!video_device) 1464 continue; 1465 1466 if (!video_device->cap._DDC) 1467 continue; 1468 1469 if (type) { 1470 switch (type) { 1471 case ACPI_VIDEO_DISPLAY_CRT: 1472 if (!video_device->flags.crt) 1473 continue; 1474 break; 1475 case ACPI_VIDEO_DISPLAY_TV: 1476 if (!video_device->flags.tvout) 1477 continue; 1478 break; 1479 case ACPI_VIDEO_DISPLAY_DVI: 1480 if (!video_device->flags.dvi) 1481 continue; 1482 break; 1483 case ACPI_VIDEO_DISPLAY_LCD: 1484 if (!video_device->flags.lcd) 1485 continue; 1486 break; 1487 } 1488 } else if (video_device->device_id != device_id) { 1489 continue; 1490 } 1491 1492 for (length = 512; length > 0; length -= 128) { 1493 ret = acpi_video_device_EDID(video_device, edid, length); 1494 if (ret > 0) 1495 return ret; 1496 } 1497 } 1498 1499 return -ENODEV; 1500 } 1501 EXPORT_SYMBOL(acpi_video_get_edid); 1502 1503 static int 1504 acpi_video_bus_get_devices(struct acpi_video_bus *video, 1505 struct acpi_device *device) 1506 { 1507 /* 1508 * There are systems where video module known to work fine regardless 1509 * of broken _DOD and ignoring returned value here doesn't cause 1510 * any issues later. 1511 */ 1512 acpi_video_device_enumerate(video); 1513 1514 return acpi_dev_for_each_child(device, acpi_video_bus_get_one_device, video); 1515 } 1516 1517 /* acpi_video interface */ 1518 1519 /* 1520 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't 1521 * perform any automatic brightness change on receiving a notification. 1522 */ 1523 static int acpi_video_bus_start_devices(struct acpi_video_bus *video) 1524 { 1525 return acpi_video_bus_DOS(video, 0, 1526 acpi_osi_is_win8() ? 1 : 0); 1527 } 1528 1529 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video) 1530 { 1531 return acpi_video_bus_DOS(video, 0, 1532 acpi_osi_is_win8() ? 0 : 1); 1533 } 1534 1535 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data) 1536 { 1537 struct acpi_video_bus *video = data; 1538 struct acpi_device *device = video->device; 1539 struct input_dev *input; 1540 int keycode = 0; 1541 1542 input = video->input; 1543 1544 switch (event) { 1545 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch, 1546 * most likely via hotkey. */ 1547 keycode = KEY_SWITCHVIDEOMODE; 1548 break; 1549 1550 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video 1551 * connector. */ 1552 acpi_video_device_enumerate(video); 1553 acpi_video_device_rebind(video); 1554 keycode = KEY_SWITCHVIDEOMODE; 1555 break; 1556 1557 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */ 1558 keycode = KEY_SWITCHVIDEOMODE; 1559 break; 1560 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */ 1561 keycode = KEY_VIDEO_NEXT; 1562 break; 1563 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */ 1564 keycode = KEY_VIDEO_PREV; 1565 break; 1566 1567 default: 1568 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", 1569 event); 1570 break; 1571 } 1572 1573 if (acpi_notifier_call_chain(device, event, 0)) 1574 /* Something vetoed the keypress. */ 1575 keycode = 0; 1576 1577 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) { 1578 input_report_key(input, keycode, 1); 1579 input_sync(input); 1580 input_report_key(input, keycode, 0); 1581 input_sync(input); 1582 } 1583 } 1584 1585 static void brightness_switch_event(struct acpi_video_device *video_device, 1586 u32 event) 1587 { 1588 if (!brightness_switch_enabled) 1589 return; 1590 1591 video_device->switch_brightness_event = event; 1592 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10); 1593 } 1594 1595 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data) 1596 { 1597 struct acpi_video_device *video_device = data; 1598 struct acpi_device *device = NULL; 1599 struct acpi_video_bus *bus; 1600 struct input_dev *input; 1601 int keycode = 0; 1602 1603 if (!video_device) 1604 return; 1605 1606 device = video_device->dev; 1607 bus = video_device->video; 1608 input = bus->input; 1609 1610 if (hw_changes_brightness > 0) { 1611 if (video_device->backlight) 1612 backlight_force_update(video_device->backlight, 1613 BACKLIGHT_UPDATE_HOTKEY); 1614 acpi_notifier_call_chain(device, event, 0); 1615 return; 1616 } 1617 1618 switch (event) { 1619 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */ 1620 brightness_switch_event(video_device, event); 1621 keycode = KEY_BRIGHTNESS_CYCLE; 1622 break; 1623 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */ 1624 brightness_switch_event(video_device, event); 1625 keycode = KEY_BRIGHTNESSUP; 1626 break; 1627 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */ 1628 brightness_switch_event(video_device, event); 1629 keycode = KEY_BRIGHTNESSDOWN; 1630 break; 1631 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */ 1632 brightness_switch_event(video_device, event); 1633 keycode = KEY_BRIGHTNESS_ZERO; 1634 break; 1635 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */ 1636 brightness_switch_event(video_device, event); 1637 keycode = KEY_DISPLAY_OFF; 1638 break; 1639 default: 1640 acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event); 1641 break; 1642 } 1643 1644 if (keycode) 1645 may_report_brightness_keys = true; 1646 1647 acpi_notifier_call_chain(device, event, 0); 1648 1649 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) { 1650 input_report_key(input, keycode, 1); 1651 input_sync(input); 1652 input_report_key(input, keycode, 0); 1653 input_sync(input); 1654 } 1655 } 1656 1657 static int acpi_video_resume(struct notifier_block *nb, 1658 unsigned long val, void *ign) 1659 { 1660 struct acpi_video_bus *video; 1661 struct acpi_video_device *video_device; 1662 int i; 1663 1664 switch (val) { 1665 case PM_POST_HIBERNATION: 1666 case PM_POST_SUSPEND: 1667 case PM_POST_RESTORE: 1668 video = container_of(nb, struct acpi_video_bus, pm_nb); 1669 1670 dev_info(&video->device->dev, "Restoring backlight state\n"); 1671 1672 for (i = 0; i < video->attached_count; i++) { 1673 video_device = video->attached_array[i].bind_info; 1674 if (video_device && video_device->brightness) 1675 acpi_video_device_lcd_set_level(video_device, 1676 video_device->brightness->curr); 1677 } 1678 1679 return NOTIFY_OK; 1680 } 1681 return NOTIFY_DONE; 1682 } 1683 1684 static acpi_status 1685 acpi_video_bus_match(acpi_handle handle, u32 level, void *context, 1686 void **return_value) 1687 { 1688 struct acpi_device *device = context; 1689 struct acpi_device *sibling; 1690 1691 if (handle == device->handle) 1692 return AE_CTRL_TERMINATE; 1693 1694 sibling = acpi_fetch_acpi_dev(handle); 1695 if (!sibling) 1696 return AE_OK; 1697 1698 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME)) 1699 return AE_ALREADY_EXISTS; 1700 1701 return AE_OK; 1702 } 1703 1704 static void acpi_video_dev_register_backlight(struct acpi_video_device *device) 1705 { 1706 struct backlight_properties props; 1707 struct pci_dev *pdev; 1708 acpi_handle acpi_parent; 1709 struct device *parent = NULL; 1710 int result; 1711 static int count; 1712 char *name; 1713 1714 result = acpi_video_init_brightness(device); 1715 if (result) 1716 return; 1717 1718 name = kasprintf(GFP_KERNEL, "acpi_video%d", count); 1719 if (!name) 1720 return; 1721 count++; 1722 1723 if (ACPI_SUCCESS(acpi_get_parent(device->dev->handle, &acpi_parent))) { 1724 pdev = acpi_get_pci_dev(acpi_parent); 1725 if (pdev) { 1726 parent = &pdev->dev; 1727 pci_dev_put(pdev); 1728 } 1729 } 1730 1731 memset(&props, 0, sizeof(struct backlight_properties)); 1732 props.type = BACKLIGHT_FIRMWARE; 1733 props.max_brightness = 1734 device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1; 1735 device->backlight = backlight_device_register(name, 1736 parent, 1737 device, 1738 &acpi_backlight_ops, 1739 &props); 1740 kfree(name); 1741 if (IS_ERR(device->backlight)) { 1742 device->backlight = NULL; 1743 return; 1744 } 1745 1746 /* 1747 * Save current brightness level in case we have to restore it 1748 * before acpi_video_device_lcd_set_level() is called next time. 1749 */ 1750 device->backlight->props.brightness = 1751 acpi_video_get_brightness(device->backlight); 1752 1753 device->cooling_dev = thermal_cooling_device_register("LCD", device, 1754 &video_cooling_ops); 1755 if (IS_ERR(device->cooling_dev)) { 1756 /* 1757 * Set cooling_dev to NULL so we don't crash trying to free it. 1758 * Also, why the hell we are returning early and not attempt to 1759 * register video output if cooling device registration failed? 1760 * -- dtor 1761 */ 1762 device->cooling_dev = NULL; 1763 return; 1764 } 1765 1766 dev_info(&device->dev->dev, "registered as cooling_device%d\n", 1767 device->cooling_dev->id); 1768 result = sysfs_create_link(&device->dev->dev.kobj, 1769 &device->cooling_dev->device.kobj, 1770 "thermal_cooling"); 1771 if (result) 1772 pr_info("sysfs link creation failed\n"); 1773 1774 result = sysfs_create_link(&device->cooling_dev->device.kobj, 1775 &device->dev->dev.kobj, "device"); 1776 if (result) 1777 pr_info("Reverse sysfs link creation failed\n"); 1778 } 1779 1780 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video) 1781 { 1782 struct acpi_video_device *dev; 1783 union acpi_object *levels; 1784 1785 mutex_lock(&video->device_list_lock); 1786 list_for_each_entry(dev, &video->video_device_list, entry) { 1787 if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels)) 1788 kfree(levels); 1789 } 1790 mutex_unlock(&video->device_list_lock); 1791 } 1792 1793 static bool acpi_video_should_register_backlight(struct acpi_video_device *dev) 1794 { 1795 /* 1796 * Do not create backlight device for video output 1797 * device that is not in the enumerated list. 1798 */ 1799 if (!acpi_video_device_in_dod(dev)) { 1800 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n"); 1801 return false; 1802 } 1803 1804 if (only_lcd) 1805 return dev->flags.lcd; 1806 return true; 1807 } 1808 1809 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video) 1810 { 1811 struct acpi_video_device *dev; 1812 1813 if (video->backlight_registered) 1814 return 0; 1815 1816 if (acpi_video_get_backlight_type() != acpi_backlight_video) 1817 return 0; 1818 1819 mutex_lock(&video->device_list_lock); 1820 list_for_each_entry(dev, &video->video_device_list, entry) { 1821 if (acpi_video_should_register_backlight(dev)) 1822 acpi_video_dev_register_backlight(dev); 1823 } 1824 mutex_unlock(&video->device_list_lock); 1825 1826 video->backlight_registered = true; 1827 1828 video->pm_nb.notifier_call = acpi_video_resume; 1829 video->pm_nb.priority = 0; 1830 return register_pm_notifier(&video->pm_nb); 1831 } 1832 1833 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device) 1834 { 1835 if (device->backlight) { 1836 backlight_device_unregister(device->backlight); 1837 device->backlight = NULL; 1838 } 1839 if (device->brightness) { 1840 kfree(device->brightness->levels); 1841 kfree(device->brightness); 1842 device->brightness = NULL; 1843 } 1844 if (device->cooling_dev) { 1845 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling"); 1846 sysfs_remove_link(&device->cooling_dev->device.kobj, "device"); 1847 thermal_cooling_device_unregister(device->cooling_dev); 1848 device->cooling_dev = NULL; 1849 } 1850 } 1851 1852 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video) 1853 { 1854 struct acpi_video_device *dev; 1855 int error; 1856 1857 if (!video->backlight_registered) 1858 return 0; 1859 1860 error = unregister_pm_notifier(&video->pm_nb); 1861 1862 mutex_lock(&video->device_list_lock); 1863 list_for_each_entry(dev, &video->video_device_list, entry) 1864 acpi_video_dev_unregister_backlight(dev); 1865 mutex_unlock(&video->device_list_lock); 1866 1867 video->backlight_registered = false; 1868 1869 return error; 1870 } 1871 1872 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device) 1873 { 1874 acpi_status status; 1875 struct acpi_device *adev = device->dev; 1876 1877 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 1878 acpi_video_device_notify, device); 1879 if (ACPI_FAILURE(status)) 1880 dev_err(&adev->dev, "Error installing notify handler\n"); 1881 else 1882 device->flags.notify = 1; 1883 } 1884 1885 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video, 1886 struct device *parent) 1887 { 1888 struct input_dev *input; 1889 struct acpi_video_device *dev; 1890 int error; 1891 1892 video->input = input = input_allocate_device(); 1893 if (!input) { 1894 error = -ENOMEM; 1895 goto out; 1896 } 1897 1898 error = acpi_video_bus_start_devices(video); 1899 if (error) 1900 goto err_free_input; 1901 1902 snprintf(video->phys, sizeof(video->phys), 1903 "%s/video/input0", acpi_device_hid(video->device)); 1904 1905 input->name = acpi_device_name(video->device); 1906 input->phys = video->phys; 1907 input->id.bustype = BUS_HOST; 1908 input->id.product = 0x06; 1909 input->dev.parent = parent; 1910 input->evbit[0] = BIT(EV_KEY); 1911 set_bit(KEY_SWITCHVIDEOMODE, input->keybit); 1912 set_bit(KEY_VIDEO_NEXT, input->keybit); 1913 set_bit(KEY_VIDEO_PREV, input->keybit); 1914 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit); 1915 set_bit(KEY_BRIGHTNESSUP, input->keybit); 1916 set_bit(KEY_BRIGHTNESSDOWN, input->keybit); 1917 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit); 1918 set_bit(KEY_DISPLAY_OFF, input->keybit); 1919 1920 error = input_register_device(input); 1921 if (error) 1922 goto err_stop_dev; 1923 1924 mutex_lock(&video->device_list_lock); 1925 list_for_each_entry(dev, &video->video_device_list, entry) 1926 acpi_video_dev_add_notify_handler(dev); 1927 mutex_unlock(&video->device_list_lock); 1928 1929 return 0; 1930 1931 err_stop_dev: 1932 acpi_video_bus_stop_devices(video); 1933 err_free_input: 1934 input_free_device(input); 1935 video->input = NULL; 1936 out: 1937 return error; 1938 } 1939 1940 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev) 1941 { 1942 if (dev->flags.notify) { 1943 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY, 1944 acpi_video_device_notify); 1945 dev->flags.notify = 0; 1946 } 1947 } 1948 1949 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video) 1950 { 1951 struct acpi_video_device *dev; 1952 1953 mutex_lock(&video->device_list_lock); 1954 list_for_each_entry(dev, &video->video_device_list, entry) { 1955 acpi_video_dev_remove_notify_handler(dev); 1956 cancel_delayed_work_sync(&dev->switch_brightness_work); 1957 } 1958 mutex_unlock(&video->device_list_lock); 1959 1960 acpi_video_bus_stop_devices(video); 1961 input_unregister_device(video->input); 1962 video->input = NULL; 1963 } 1964 1965 static int acpi_video_bus_put_devices(struct acpi_video_bus *video) 1966 { 1967 struct acpi_video_device *dev, *next; 1968 1969 mutex_lock(&video->device_list_lock); 1970 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) { 1971 list_del(&dev->entry); 1972 kfree(dev); 1973 } 1974 mutex_unlock(&video->device_list_lock); 1975 1976 return 0; 1977 } 1978 1979 static int instance; 1980 1981 static int acpi_video_bus_probe(struct auxiliary_device *aux_dev, 1982 const struct auxiliary_device_id *id_unused) 1983 { 1984 struct acpi_device *device = ACPI_COMPANION(&aux_dev->dev); 1985 struct acpi_video_bus *video; 1986 bool auto_detect; 1987 int error; 1988 acpi_status status; 1989 1990 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, 1991 acpi_dev_parent(device)->handle, 1, 1992 acpi_video_bus_match, NULL, 1993 device, NULL); 1994 if (status == AE_ALREADY_EXISTS) { 1995 pr_info(FW_BUG 1996 "Duplicate ACPI video bus devices for the" 1997 " same VGA controller, please try module " 1998 "parameter \"video.allow_duplicates=1\"" 1999 "if the current driver doesn't work.\n"); 2000 if (!allow_duplicates) 2001 return -ENODEV; 2002 } 2003 2004 video = kzalloc_obj(struct acpi_video_bus); 2005 if (!video) 2006 return -ENOMEM; 2007 2008 /* a hack to fix the duplicate name "VID" problem on T61 */ 2009 if (!strcmp(device->pnp.bus_id, "VID")) { 2010 if (instance) 2011 device->pnp.bus_id[3] = '0' + instance; 2012 instance++; 2013 } 2014 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */ 2015 if (!strcmp(device->pnp.bus_id, "VGA")) { 2016 if (instance) 2017 device->pnp.bus_id[3] = '0' + instance; 2018 instance++; 2019 } 2020 2021 auxiliary_set_drvdata(aux_dev, video); 2022 2023 video->device = device; 2024 strscpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME); 2025 strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS); 2026 device->driver_data = video; 2027 2028 acpi_video_bus_find_cap(video); 2029 error = acpi_video_bus_check(video); 2030 if (error) 2031 goto err_free_video; 2032 2033 mutex_init(&video->device_list_lock); 2034 INIT_LIST_HEAD(&video->video_device_list); 2035 2036 error = acpi_video_bus_get_devices(video, device); 2037 if (error) 2038 goto err_put_video; 2039 2040 /* 2041 * HP ZBook Fury 16 G10 requires ACPI video's child devices have _PS0 2042 * evaluated to have functional panel brightness control. 2043 */ 2044 acpi_device_fix_up_power_children(device); 2045 2046 pr_info("%s [%s] (multi-head: %s rom: %s post: %s)\n", 2047 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device), 2048 str_yes_no(video->flags.multihead), 2049 str_yes_no(video->flags.rom), 2050 str_yes_no(video->flags.post)); 2051 mutex_lock(&video_list_lock); 2052 list_add_tail(&video->entry, &video_bus_head); 2053 mutex_unlock(&video_list_lock); 2054 2055 /* 2056 * If backlight-type auto-detection is used then a native backlight may 2057 * show up later and this may change the result from video to native. 2058 * Therefor normally the userspace visible /sys/class/backlight device 2059 * gets registered separately by the GPU driver calling 2060 * acpi_video_register_backlight() when an internal panel is detected. 2061 * Register the backlight now when not using auto-detection, so that 2062 * when the kernel cmdline or DMI-quirks are used the backlight will 2063 * get registered even if acpi_video_register_backlight() is not called. 2064 */ 2065 acpi_video_run_bcl_for_osi(video); 2066 if (__acpi_video_get_backlight_type(false, &auto_detect) == acpi_backlight_video && 2067 !auto_detect) 2068 acpi_video_bus_register_backlight(video); 2069 2070 error = acpi_video_bus_add_notify_handler(video, &aux_dev->dev); 2071 if (error) 2072 goto err_del; 2073 2074 error = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY, 2075 acpi_video_bus_notify, video); 2076 if (error) 2077 goto err_remove; 2078 2079 return 0; 2080 2081 err_remove: 2082 acpi_video_bus_remove_notify_handler(video); 2083 err_del: 2084 mutex_lock(&video_list_lock); 2085 list_del(&video->entry); 2086 mutex_unlock(&video_list_lock); 2087 acpi_video_bus_unregister_backlight(video); 2088 err_put_video: 2089 acpi_video_bus_put_devices(video); 2090 kfree(video->attached_array); 2091 err_free_video: 2092 kfree(video); 2093 device->driver_data = NULL; 2094 2095 return error; 2096 } 2097 2098 static void acpi_video_bus_remove(struct auxiliary_device *aux_dev) 2099 { 2100 struct acpi_video_bus *video = auxiliary_get_drvdata(aux_dev); 2101 struct acpi_device *device = ACPI_COMPANION(&aux_dev->dev); 2102 2103 acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY, 2104 acpi_video_bus_notify); 2105 2106 mutex_lock(&video_list_lock); 2107 list_del(&video->entry); 2108 mutex_unlock(&video_list_lock); 2109 2110 acpi_video_bus_remove_notify_handler(video); 2111 acpi_video_bus_unregister_backlight(video); 2112 acpi_video_bus_put_devices(video); 2113 2114 kfree(video->attached_array); 2115 kfree(video); 2116 device->driver_data = NULL; 2117 } 2118 2119 static int __init is_i740(struct pci_dev *dev) 2120 { 2121 if (dev->device == 0x00D1) 2122 return 1; 2123 if (dev->device == 0x7000) 2124 return 1; 2125 return 0; 2126 } 2127 2128 static int __init intel_opregion_present(void) 2129 { 2130 int opregion = 0; 2131 struct pci_dev *dev = NULL; 2132 u32 address; 2133 2134 for_each_pci_dev(dev) { 2135 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) 2136 continue; 2137 if (dev->vendor != PCI_VENDOR_ID_INTEL) 2138 continue; 2139 /* We don't want to poke around undefined i740 registers */ 2140 if (is_i740(dev)) 2141 continue; 2142 pci_read_config_dword(dev, 0xfc, &address); 2143 if (!address) 2144 continue; 2145 opregion = 1; 2146 } 2147 return opregion; 2148 } 2149 2150 int acpi_video_register(void) 2151 { 2152 int ret = 0; 2153 2154 mutex_lock(®ister_count_mutex); 2155 if (register_count) { 2156 /* 2157 * if the function of acpi_video_register is already called, 2158 * don't register the acpi_video_bus again and return no error. 2159 */ 2160 goto leave; 2161 } 2162 2163 dmi_check_system(video_dmi_table); 2164 2165 ret = auxiliary_driver_register(&acpi_video_bus); 2166 if (ret) 2167 goto leave; 2168 2169 /* 2170 * When the acpi_video_bus is loaded successfully, increase 2171 * the counter reference. 2172 */ 2173 register_count = 1; 2174 2175 leave: 2176 mutex_unlock(®ister_count_mutex); 2177 return ret; 2178 } 2179 EXPORT_SYMBOL(acpi_video_register); 2180 2181 void acpi_video_unregister(void) 2182 { 2183 mutex_lock(®ister_count_mutex); 2184 if (register_count) { 2185 auxiliary_driver_unregister(&acpi_video_bus); 2186 register_count = 0; 2187 may_report_brightness_keys = false; 2188 } 2189 mutex_unlock(®ister_count_mutex); 2190 } 2191 EXPORT_SYMBOL(acpi_video_unregister); 2192 2193 void acpi_video_register_backlight(void) 2194 { 2195 struct acpi_video_bus *video; 2196 2197 mutex_lock(&video_list_lock); 2198 list_for_each_entry(video, &video_bus_head, entry) 2199 acpi_video_bus_register_backlight(video); 2200 mutex_unlock(&video_list_lock); 2201 } 2202 EXPORT_SYMBOL(acpi_video_register_backlight); 2203 2204 bool acpi_video_handles_brightness_key_presses(void) 2205 { 2206 return may_report_brightness_keys && 2207 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS); 2208 } 2209 EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses); 2210 2211 /* 2212 * This is kind of nasty. Hardware using Intel chipsets may require 2213 * the video opregion code to be run first in order to initialise 2214 * state before any ACPI video calls are made. To handle this we defer 2215 * registration of the video class until the opregion code has run. 2216 */ 2217 2218 static int __init acpi_video_init(void) 2219 { 2220 /* 2221 * Let the module load even if ACPI is disabled (e.g. due to 2222 * a broken BIOS) so that i915.ko can still be loaded on such 2223 * old systems without an AcpiOpRegion. 2224 * 2225 * acpi_video_register() will report -ENODEV later as well due 2226 * to acpi_disabled when i915.ko tries to register itself afterwards. 2227 */ 2228 if (acpi_disabled) 2229 return 0; 2230 2231 if (intel_opregion_present()) 2232 return 0; 2233 2234 return acpi_video_register(); 2235 } 2236 2237 static void __exit acpi_video_exit(void) 2238 { 2239 acpi_video_unregister(); 2240 } 2241 2242 module_init(acpi_video_init); 2243 module_exit(acpi_video_exit); 2244