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