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