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/platform_device.h> 25 #include <linux/slab.h> 26 #include <linux/dmi.h> 27 #include <linux/suspend.h> 28 #include <linux/acpi.h> 29 #include <acpi/video.h> 30 #include <linux/uaccess.h> 31 #include <linux/string_choices.h> 32 33 #define ACPI_VIDEO_BUS_NAME "Video Bus" 34 #define ACPI_VIDEO_DEVICE_NAME "Video Device" 35 36 #define MAX_NAME_LEN 20 37 38 MODULE_AUTHOR("Bruno Ducrot"); 39 MODULE_DESCRIPTION("ACPI Video Driver"); 40 MODULE_LICENSE("GPL"); 41 42 static bool brightness_switch_enabled = true; 43 module_param(brightness_switch_enabled, bool, 0644); 44 45 /* 46 * By default, we don't allow duplicate ACPI video bus devices 47 * under the same VGA controller 48 */ 49 static bool allow_duplicates; 50 module_param(allow_duplicates, bool, 0644); 51 52 #define REPORT_OUTPUT_KEY_EVENTS 0x01 53 #define REPORT_BRIGHTNESS_KEY_EVENTS 0x02 54 static int report_key_events = -1; 55 module_param(report_key_events, int, 0644); 56 MODULE_PARM_DESC(report_key_events, 57 "0: none, 1: output changes, 2: brightness changes, 3: all"); 58 59 static int hw_changes_brightness = -1; 60 module_param(hw_changes_brightness, int, 0644); 61 MODULE_PARM_DESC(hw_changes_brightness, 62 "Set this to 1 on buggy hw which changes the brightness itself when " 63 "a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness"); 64 65 /* 66 * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be 67 * assumed even if not actually set. 68 */ 69 static bool device_id_scheme = false; 70 module_param(device_id_scheme, bool, 0444); 71 72 static int only_lcd; 73 module_param(only_lcd, int, 0444); 74 75 static bool may_report_brightness_keys; 76 static int register_count; 77 static DEFINE_MUTEX(register_count_mutex); 78 static DEFINE_MUTEX(video_list_lock); 79 static LIST_HEAD(video_bus_head); 80 static int acpi_video_bus_probe(struct platform_device *pdev); 81 static void acpi_video_bus_remove(struct platform_device *pdev); 82 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data); 83 84 /* 85 * Indices in the _BCL method response: the first two items are special, 86 * the rest are all supported levels. 87 * 88 * See page 575 of the ACPI spec 3.0 89 */ 90 enum acpi_video_level_idx { 91 ACPI_VIDEO_AC_LEVEL, /* level when machine has full power */ 92 ACPI_VIDEO_BATTERY_LEVEL, /* level when machine is on batteries */ 93 ACPI_VIDEO_FIRST_LEVEL, /* actual supported levels begin here */ 94 }; 95 96 static const struct acpi_device_id video_device_ids[] = { 97 {ACPI_VIDEO_HID, 0}, 98 {"", 0}, 99 }; 100 MODULE_DEVICE_TABLE(acpi, video_device_ids); 101 102 static struct platform_driver acpi_video_bus = { 103 .probe = acpi_video_bus_probe, 104 .remove = acpi_video_bus_remove, 105 .driver = { 106 .name = "acpi-video", 107 .acpi_match_table = video_device_ids, 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_obj(*br); 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_objs(*br->levels, 840 obj->package.count + ACPI_VIDEO_FIRST_LEVEL); 841 if (!br->levels) { 842 result = -ENOMEM; 843 goto out_free; 844 } 845 846 for (i = 0; i < obj->package.count; i++) { 847 o = (union acpi_object *)&obj->package.elements[i]; 848 if (o->type != ACPI_TYPE_INTEGER) { 849 acpi_handle_info(device->handle, "Invalid data\n"); 850 continue; 851 } 852 value = (u32) o->integer.value; 853 /* Skip duplicate entries */ 854 if (count > ACPI_VIDEO_FIRST_LEVEL 855 && br->levels[count - 1] == value) 856 continue; 857 858 br->levels[count] = value; 859 860 if (br->levels[count] > max_level) 861 max_level = br->levels[count]; 862 count++; 863 } 864 865 /* 866 * some buggy BIOS don't export the levels 867 * when machine is on AC/Battery in _BCL package. 868 * In this case, the first two elements in _BCL packages 869 * are also supported brightness levels that OS should take care of. 870 */ 871 for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) { 872 if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL]) 873 level_ac_battery++; 874 if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL]) 875 level_ac_battery++; 876 } 877 878 if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) { 879 level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery; 880 br->flags._BCL_no_ac_battery_levels = 1; 881 for (i = (count - 1 + level_ac_battery); 882 i >= ACPI_VIDEO_FIRST_LEVEL; i--) 883 br->levels[i] = br->levels[i - level_ac_battery]; 884 count += level_ac_battery; 885 } else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL) 886 acpi_handle_info(device->handle, 887 "Too many duplicates in _BCL package"); 888 889 /* Check if the _BCL package is in a reversed order */ 890 if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) { 891 br->flags._BCL_reversed = 1; 892 sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL], 893 count - ACPI_VIDEO_FIRST_LEVEL, 894 sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]), 895 acpi_video_cmp_level, NULL); 896 } else if (max_level != br->levels[count - 1]) 897 acpi_handle_info(device->handle, 898 "Found unordered _BCL package"); 899 900 br->count = count; 901 *dev_br = br; 902 if (pmax_level) 903 *pmax_level = max_level; 904 905 out: 906 kfree(obj); 907 return result; 908 out_free: 909 kfree(br); 910 goto out; 911 } 912 EXPORT_SYMBOL(acpi_video_get_levels); 913 914 /* 915 * Arg: 916 * device : video output device (LCD, CRT, ..) 917 * 918 * Return Value: 919 * Maximum brightness level 920 * 921 * Allocate and initialize device->brightness. 922 */ 923 924 static int 925 acpi_video_init_brightness(struct acpi_video_device *device) 926 { 927 int i, max_level = 0; 928 unsigned long long level, level_old; 929 struct acpi_video_device_brightness *br = NULL; 930 int result; 931 932 result = acpi_video_get_levels(device->dev, &br, &max_level); 933 if (result) 934 return result; 935 device->brightness = br; 936 937 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */ 938 br->curr = level = max_level; 939 940 if (!device->cap._BQC) 941 goto set_level; 942 943 result = acpi_video_device_lcd_get_level_current(device, 944 &level_old, true); 945 if (result) 946 goto out_free_levels; 947 948 result = acpi_video_bqc_quirk(device, max_level, level_old); 949 if (result) 950 goto out_free_levels; 951 /* 952 * cap._BQC may get cleared due to _BQC is found to be broken 953 * in acpi_video_bqc_quirk, so check again here. 954 */ 955 if (!device->cap._BQC) 956 goto set_level; 957 958 level = acpi_video_bqc_value_to_level(device, level_old); 959 /* 960 * On some buggy laptops, _BQC returns an uninitialized 961 * value when invoked for the first time, i.e. 962 * level_old is invalid (no matter whether it's a level 963 * or an index). Set the backlight to max_level in this case. 964 */ 965 for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++) 966 if (level == br->levels[i]) 967 break; 968 if (i == br->count || !level) 969 level = max_level; 970 971 set_level: 972 result = acpi_video_device_lcd_set_level(device, level); 973 if (result) 974 goto out_free_levels; 975 976 acpi_handle_debug(device->dev->handle, "found %d brightness levels\n", 977 br->count - ACPI_VIDEO_FIRST_LEVEL); 978 979 return 0; 980 981 out_free_levels: 982 kfree(br->levels); 983 kfree(br); 984 device->brightness = NULL; 985 return result; 986 } 987 988 /* 989 * Arg: 990 * device : video output device (LCD, CRT, ..) 991 * 992 * Return Value: 993 * None 994 * 995 * Find out all required AML methods defined under the output 996 * device. 997 */ 998 999 static void acpi_video_device_find_cap(struct acpi_video_device *device) 1000 { 1001 if (acpi_has_method(device->dev->handle, "_ADR")) 1002 device->cap._ADR = 1; 1003 if (acpi_has_method(device->dev->handle, "_BCL")) 1004 device->cap._BCL = 1; 1005 if (acpi_has_method(device->dev->handle, "_BCM")) 1006 device->cap._BCM = 1; 1007 if (acpi_has_method(device->dev->handle, "_BQC")) { 1008 device->cap._BQC = 1; 1009 } else if (acpi_has_method(device->dev->handle, "_BCQ")) { 1010 acpi_handle_info(device->dev->handle, 1011 "_BCQ is used instead of _BQC\n"); 1012 device->cap._BCQ = 1; 1013 } 1014 1015 if (acpi_has_method(device->dev->handle, "_DDC")) 1016 device->cap._DDC = 1; 1017 } 1018 1019 /* 1020 * Arg: 1021 * device : video output device (VGA) 1022 * 1023 * Return Value: 1024 * None 1025 * 1026 * Find out all required AML methods defined under the video bus device. 1027 */ 1028 1029 static void acpi_video_bus_find_cap(struct acpi_video_bus *video) 1030 { 1031 if (acpi_has_method(video->device->handle, "_DOS")) 1032 video->cap._DOS = 1; 1033 if (acpi_has_method(video->device->handle, "_DOD")) 1034 video->cap._DOD = 1; 1035 if (acpi_has_method(video->device->handle, "_ROM")) 1036 video->cap._ROM = 1; 1037 if (acpi_has_method(video->device->handle, "_GPD")) 1038 video->cap._GPD = 1; 1039 if (acpi_has_method(video->device->handle, "_SPD")) 1040 video->cap._SPD = 1; 1041 if (acpi_has_method(video->device->handle, "_VPO")) 1042 video->cap._VPO = 1; 1043 } 1044 1045 /* 1046 * Check whether the video bus device has required AML method to 1047 * support the desired features 1048 */ 1049 1050 static int acpi_video_bus_check(struct acpi_video_bus *video) 1051 { 1052 acpi_status status = -ENOENT; 1053 struct pci_dev *dev; 1054 1055 if (!video) 1056 return -EINVAL; 1057 1058 dev = acpi_get_pci_dev(video->device->handle); 1059 if (!dev) 1060 return -ENODEV; 1061 pci_dev_put(dev); 1062 1063 /* 1064 * Since there is no HID, CID and so on for VGA driver, we have 1065 * to check well known required nodes. 1066 */ 1067 1068 /* Does this device support video switching? */ 1069 if (video->cap._DOS || video->cap._DOD) { 1070 if (!video->cap._DOS) { 1071 pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n", 1072 acpi_device_bid(video->device)); 1073 } 1074 video->flags.multihead = 1; 1075 status = 0; 1076 } 1077 1078 /* Does this device support retrieving a video ROM? */ 1079 if (video->cap._ROM) { 1080 video->flags.rom = 1; 1081 status = 0; 1082 } 1083 1084 /* Does this device support configuring which video device to POST? */ 1085 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) { 1086 video->flags.post = 1; 1087 status = 0; 1088 } 1089 1090 return status; 1091 } 1092 1093 /* 1094 * -------------------------------------------------------------------------- 1095 * Driver Interface 1096 * -------------------------------------------------------------------------- 1097 */ 1098 1099 /* device interface */ 1100 static struct acpi_video_device_attrib * 1101 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id) 1102 { 1103 struct acpi_video_enumerated_device *ids; 1104 int i; 1105 1106 for (i = 0; i < video->attached_count; i++) { 1107 ids = &video->attached_array[i]; 1108 if ((ids->value.int_val & 0xffff) == device_id) 1109 return &ids->value.attrib; 1110 } 1111 1112 return NULL; 1113 } 1114 1115 static int 1116 acpi_video_get_device_type(struct acpi_video_bus *video, 1117 unsigned long device_id) 1118 { 1119 struct acpi_video_enumerated_device *ids; 1120 int i; 1121 1122 for (i = 0; i < video->attached_count; i++) { 1123 ids = &video->attached_array[i]; 1124 if ((ids->value.int_val & 0xffff) == device_id) 1125 return ids->value.int_val; 1126 } 1127 1128 return 0; 1129 } 1130 1131 static int acpi_video_bus_get_one_device(struct acpi_device *device, void *arg) 1132 { 1133 struct acpi_video_bus *video = arg; 1134 struct acpi_video_device_attrib *attribute; 1135 struct acpi_video_device *data; 1136 int device_type; 1137 u64 device_id; 1138 1139 /* Skip devices without _ADR instead of failing. */ 1140 if (acpi_get_local_u64_address(device->handle, &device_id)) 1141 goto exit; 1142 1143 data = kzalloc_obj(struct acpi_video_device); 1144 if (!data) { 1145 dev_dbg(&device->dev, "Cannot attach\n"); 1146 return -ENOMEM; 1147 } 1148 1149 strscpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME); 1150 strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS); 1151 1152 data->device_id = device_id; 1153 data->video = video; 1154 data->dev = device; 1155 INIT_DELAYED_WORK(&data->switch_brightness_work, 1156 acpi_video_switch_brightness); 1157 1158 attribute = acpi_video_get_device_attr(video, device_id); 1159 1160 if (attribute && (attribute->device_id_scheme || device_id_scheme)) { 1161 switch (attribute->display_type) { 1162 case ACPI_VIDEO_DISPLAY_CRT: 1163 data->flags.crt = 1; 1164 break; 1165 case ACPI_VIDEO_DISPLAY_TV: 1166 data->flags.tvout = 1; 1167 break; 1168 case ACPI_VIDEO_DISPLAY_DVI: 1169 data->flags.dvi = 1; 1170 break; 1171 case ACPI_VIDEO_DISPLAY_LCD: 1172 data->flags.lcd = 1; 1173 break; 1174 default: 1175 data->flags.unknown = 1; 1176 break; 1177 } 1178 if (attribute->bios_can_detect) 1179 data->flags.bios = 1; 1180 } else { 1181 /* Check for legacy IDs */ 1182 device_type = acpi_video_get_device_type(video, device_id); 1183 /* Ignore bits 16 and 18-20 */ 1184 switch (device_type & 0xffe2ffff) { 1185 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR: 1186 data->flags.crt = 1; 1187 break; 1188 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL: 1189 data->flags.lcd = 1; 1190 break; 1191 case ACPI_VIDEO_DISPLAY_LEGACY_TV: 1192 data->flags.tvout = 1; 1193 break; 1194 default: 1195 data->flags.unknown = 1; 1196 } 1197 } 1198 1199 acpi_video_device_bind(video, data); 1200 acpi_video_device_find_cap(data); 1201 1202 if (data->cap._BCM && data->cap._BCL) 1203 may_report_brightness_keys = true; 1204 1205 mutex_lock(&video->device_list_lock); 1206 list_add_tail(&data->entry, &video->video_device_list); 1207 mutex_unlock(&video->device_list_lock); 1208 1209 exit: 1210 video->child_count++; 1211 return 0; 1212 } 1213 1214 /* 1215 * Arg: 1216 * video : video bus device 1217 * 1218 * Return: 1219 * none 1220 * 1221 * Enumerate the video device list of the video bus, 1222 * bind the ids with the corresponding video devices 1223 * under the video bus. 1224 */ 1225 1226 static void acpi_video_device_rebind(struct acpi_video_bus *video) 1227 { 1228 struct acpi_video_device *dev; 1229 1230 mutex_lock(&video->device_list_lock); 1231 1232 list_for_each_entry(dev, &video->video_device_list, entry) 1233 acpi_video_device_bind(video, dev); 1234 1235 mutex_unlock(&video->device_list_lock); 1236 } 1237 1238 /* 1239 * Arg: 1240 * video : video bus device 1241 * device : video output device under the video 1242 * bus 1243 * 1244 * Return: 1245 * none 1246 * 1247 * Bind the ids with the corresponding video devices 1248 * under the video bus. 1249 */ 1250 1251 static void 1252 acpi_video_device_bind(struct acpi_video_bus *video, 1253 struct acpi_video_device *device) 1254 { 1255 struct acpi_video_enumerated_device *ids; 1256 int i; 1257 1258 for (i = 0; i < video->attached_count; i++) { 1259 ids = &video->attached_array[i]; 1260 if (device->device_id == (ids->value.int_val & 0xffff)) { 1261 ids->bind_info = device; 1262 acpi_handle_debug(video->device->handle, "%s: %d\n", 1263 __func__, i); 1264 } 1265 } 1266 } 1267 1268 static bool acpi_video_device_in_dod(struct acpi_video_device *device) 1269 { 1270 struct acpi_video_bus *video = device->video; 1271 int i; 1272 1273 /* 1274 * If we have a broken _DOD or we have more than 8 output devices 1275 * under the graphics controller node that we can't proper deal with 1276 * in the operation region code currently, no need to test. 1277 */ 1278 if (!video->attached_count || video->child_count > 8) 1279 return true; 1280 1281 for (i = 0; i < video->attached_count; i++) { 1282 if ((video->attached_array[i].value.int_val & 0xfff) == 1283 (device->device_id & 0xfff)) 1284 return true; 1285 } 1286 1287 return false; 1288 } 1289 1290 /* 1291 * Arg: 1292 * video : video bus device 1293 * 1294 * Return: 1295 * < 0 : error 1296 * 1297 * Call _DOD to enumerate all devices attached to display adapter 1298 * 1299 */ 1300 1301 static int acpi_video_device_enumerate(struct acpi_video_bus *video) 1302 { 1303 int status; 1304 int count; 1305 int i; 1306 struct acpi_video_enumerated_device *active_list; 1307 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 1308 union acpi_object *dod = NULL; 1309 union acpi_object *obj; 1310 1311 if (!video->cap._DOD) 1312 return AE_NOT_EXIST; 1313 1314 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer); 1315 if (ACPI_FAILURE(status)) { 1316 acpi_handle_info(video->device->handle, 1317 "_DOD evaluation failed: %s\n", 1318 acpi_format_exception(status)); 1319 return status; 1320 } 1321 1322 dod = buffer.pointer; 1323 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) { 1324 acpi_handle_info(video->device->handle, "Invalid _DOD data\n"); 1325 status = -EFAULT; 1326 goto out; 1327 } 1328 1329 acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n", 1330 dod->package.count); 1331 1332 active_list = kzalloc_objs(struct acpi_video_enumerated_device, 1333 1 + dod->package.count); 1334 if (!active_list) { 1335 status = -ENOMEM; 1336 goto out; 1337 } 1338 1339 count = 0; 1340 for (i = 0; i < dod->package.count; i++) { 1341 obj = &dod->package.elements[i]; 1342 1343 if (obj->type != ACPI_TYPE_INTEGER) { 1344 acpi_handle_info(video->device->handle, 1345 "Invalid _DOD data in element %d\n", i); 1346 continue; 1347 } 1348 1349 active_list[count].value.int_val = obj->integer.value; 1350 active_list[count].bind_info = NULL; 1351 1352 acpi_handle_debug(video->device->handle, 1353 "_DOD element[%d] = %d\n", i, 1354 (int)obj->integer.value); 1355 1356 count++; 1357 } 1358 1359 kfree(video->attached_array); 1360 1361 video->attached_array = active_list; 1362 video->attached_count = count; 1363 1364 out: 1365 kfree(buffer.pointer); 1366 return status; 1367 } 1368 1369 static int 1370 acpi_video_get_next_level(struct acpi_video_device *device, 1371 u32 level_current, u32 event) 1372 { 1373 int min, max, min_above, max_below, i, l, delta = 255; 1374 max = max_below = 0; 1375 min = min_above = 255; 1376 /* Find closest level to level_current */ 1377 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) { 1378 l = device->brightness->levels[i]; 1379 if (abs(l - level_current) < abs(delta)) { 1380 delta = l - level_current; 1381 if (!delta) 1382 break; 1383 } 1384 } 1385 /* Adjust level_current to closest available level */ 1386 level_current += delta; 1387 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) { 1388 l = device->brightness->levels[i]; 1389 if (l < min) 1390 min = l; 1391 if (l > max) 1392 max = l; 1393 if (l < min_above && l > level_current) 1394 min_above = l; 1395 if (l > max_below && l < level_current) 1396 max_below = l; 1397 } 1398 1399 switch (event) { 1400 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: 1401 return (level_current < max) ? min_above : min; 1402 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: 1403 return (level_current < max) ? min_above : max; 1404 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: 1405 return (level_current > min) ? max_below : min; 1406 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: 1407 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: 1408 return 0; 1409 default: 1410 return level_current; 1411 } 1412 } 1413 1414 static void 1415 acpi_video_switch_brightness(struct work_struct *work) 1416 { 1417 struct acpi_video_device *device = container_of(to_delayed_work(work), 1418 struct acpi_video_device, switch_brightness_work); 1419 unsigned long long level_current, level_next; 1420 int event = device->switch_brightness_event; 1421 int result = -EINVAL; 1422 1423 /* no warning message if acpi_backlight=vendor or a quirk is used */ 1424 if (!device->backlight) 1425 return; 1426 1427 if (!device->brightness) 1428 goto out; 1429 1430 result = acpi_video_device_lcd_get_level_current(device, 1431 &level_current, 1432 false); 1433 if (result) 1434 goto out; 1435 1436 level_next = acpi_video_get_next_level(device, level_current, event); 1437 1438 result = acpi_video_device_lcd_set_level(device, level_next); 1439 1440 if (!result) 1441 backlight_force_update(device->backlight, 1442 BACKLIGHT_UPDATE_HOTKEY); 1443 1444 out: 1445 if (result) 1446 acpi_handle_info(device->dev->handle, 1447 "Failed to switch brightness\n"); 1448 } 1449 1450 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id, 1451 void **edid) 1452 { 1453 struct acpi_video_bus *video; 1454 struct acpi_video_device *video_device; 1455 int i, length, ret; 1456 1457 if (!device || !acpi_driver_data(device)) 1458 return -EINVAL; 1459 1460 video = acpi_driver_data(device); 1461 1462 for (i = 0; i < video->attached_count; i++) { 1463 video_device = video->attached_array[i].bind_info; 1464 1465 if (!video_device) 1466 continue; 1467 1468 if (!video_device->cap._DDC) 1469 continue; 1470 1471 if (type) { 1472 switch (type) { 1473 case ACPI_VIDEO_DISPLAY_CRT: 1474 if (!video_device->flags.crt) 1475 continue; 1476 break; 1477 case ACPI_VIDEO_DISPLAY_TV: 1478 if (!video_device->flags.tvout) 1479 continue; 1480 break; 1481 case ACPI_VIDEO_DISPLAY_DVI: 1482 if (!video_device->flags.dvi) 1483 continue; 1484 break; 1485 case ACPI_VIDEO_DISPLAY_LCD: 1486 if (!video_device->flags.lcd) 1487 continue; 1488 break; 1489 } 1490 } else if (video_device->device_id != device_id) { 1491 continue; 1492 } 1493 1494 for (length = 512; length > 0; length -= 128) { 1495 ret = acpi_video_device_EDID(video_device, edid, length); 1496 if (ret > 0) 1497 return ret; 1498 } 1499 } 1500 1501 return -ENODEV; 1502 } 1503 EXPORT_SYMBOL(acpi_video_get_edid); 1504 1505 static int 1506 acpi_video_bus_get_devices(struct acpi_video_bus *video, 1507 struct acpi_device *device) 1508 { 1509 /* 1510 * There are systems where video module known to work fine regardless 1511 * of broken _DOD and ignoring returned value here doesn't cause 1512 * any issues later. 1513 */ 1514 acpi_video_device_enumerate(video); 1515 1516 return acpi_dev_for_each_child(device, acpi_video_bus_get_one_device, video); 1517 } 1518 1519 /* acpi_video interface */ 1520 1521 /* 1522 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't 1523 * perform any automatic brightness change on receiving a notification. 1524 */ 1525 static int acpi_video_bus_start_devices(struct acpi_video_bus *video) 1526 { 1527 return acpi_video_bus_DOS(video, 0, 1528 acpi_osi_is_win8() ? 1 : 0); 1529 } 1530 1531 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video) 1532 { 1533 return acpi_video_bus_DOS(video, 0, 1534 acpi_osi_is_win8() ? 0 : 1); 1535 } 1536 1537 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data) 1538 { 1539 struct acpi_video_bus *video = data; 1540 struct acpi_device *device = video->device; 1541 struct input_dev *input; 1542 int keycode = 0; 1543 1544 input = video->input; 1545 1546 switch (event) { 1547 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch, 1548 * most likely via hotkey. */ 1549 keycode = KEY_SWITCHVIDEOMODE; 1550 break; 1551 1552 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video 1553 * connector. */ 1554 acpi_video_device_enumerate(video); 1555 acpi_video_device_rebind(video); 1556 keycode = KEY_SWITCHVIDEOMODE; 1557 break; 1558 1559 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */ 1560 keycode = KEY_SWITCHVIDEOMODE; 1561 break; 1562 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */ 1563 keycode = KEY_VIDEO_NEXT; 1564 break; 1565 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */ 1566 keycode = KEY_VIDEO_PREV; 1567 break; 1568 1569 default: 1570 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", 1571 event); 1572 break; 1573 } 1574 1575 if (acpi_notifier_call_chain(device, event, 0)) 1576 /* Something vetoed the keypress. */ 1577 keycode = 0; 1578 1579 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) { 1580 input_report_key(input, keycode, 1); 1581 input_sync(input); 1582 input_report_key(input, keycode, 0); 1583 input_sync(input); 1584 } 1585 } 1586 1587 static void brightness_switch_event(struct acpi_video_device *video_device, 1588 u32 event) 1589 { 1590 if (!brightness_switch_enabled) 1591 return; 1592 1593 video_device->switch_brightness_event = event; 1594 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10); 1595 } 1596 1597 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data) 1598 { 1599 struct acpi_video_device *video_device = data; 1600 struct acpi_device *device = NULL; 1601 struct acpi_video_bus *bus; 1602 struct input_dev *input; 1603 int keycode = 0; 1604 1605 if (!video_device) 1606 return; 1607 1608 device = video_device->dev; 1609 bus = video_device->video; 1610 input = bus->input; 1611 1612 if (hw_changes_brightness > 0) { 1613 if (video_device->backlight) 1614 backlight_force_update(video_device->backlight, 1615 BACKLIGHT_UPDATE_HOTKEY); 1616 acpi_notifier_call_chain(device, event, 0); 1617 return; 1618 } 1619 1620 switch (event) { 1621 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */ 1622 brightness_switch_event(video_device, event); 1623 keycode = KEY_BRIGHTNESS_CYCLE; 1624 break; 1625 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */ 1626 brightness_switch_event(video_device, event); 1627 keycode = KEY_BRIGHTNESSUP; 1628 break; 1629 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */ 1630 brightness_switch_event(video_device, event); 1631 keycode = KEY_BRIGHTNESSDOWN; 1632 break; 1633 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */ 1634 brightness_switch_event(video_device, event); 1635 keycode = KEY_BRIGHTNESS_ZERO; 1636 break; 1637 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */ 1638 brightness_switch_event(video_device, event); 1639 keycode = KEY_DISPLAY_OFF; 1640 break; 1641 default: 1642 acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event); 1643 break; 1644 } 1645 1646 if (keycode) 1647 may_report_brightness_keys = true; 1648 1649 acpi_notifier_call_chain(device, event, 0); 1650 1651 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) { 1652 input_report_key(input, keycode, 1); 1653 input_sync(input); 1654 input_report_key(input, keycode, 0); 1655 input_sync(input); 1656 } 1657 } 1658 1659 static int acpi_video_resume(struct notifier_block *nb, 1660 unsigned long val, void *ign) 1661 { 1662 struct acpi_video_bus *video; 1663 struct acpi_video_device *video_device; 1664 int i; 1665 1666 switch (val) { 1667 case PM_POST_HIBERNATION: 1668 case PM_POST_SUSPEND: 1669 case PM_POST_RESTORE: 1670 video = container_of(nb, struct acpi_video_bus, pm_nb); 1671 1672 dev_info(&video->device->dev, "Restoring backlight state\n"); 1673 1674 for (i = 0; i < video->attached_count; i++) { 1675 video_device = video->attached_array[i].bind_info; 1676 if (video_device && video_device->brightness) 1677 acpi_video_device_lcd_set_level(video_device, 1678 video_device->brightness->curr); 1679 } 1680 1681 return NOTIFY_OK; 1682 } 1683 return NOTIFY_DONE; 1684 } 1685 1686 static acpi_status 1687 acpi_video_bus_match(acpi_handle handle, u32 level, void *context, 1688 void **return_value) 1689 { 1690 struct acpi_device *device = context; 1691 struct acpi_device *sibling; 1692 1693 if (handle == device->handle) 1694 return AE_CTRL_TERMINATE; 1695 1696 sibling = acpi_fetch_acpi_dev(handle); 1697 if (!sibling) 1698 return AE_OK; 1699 1700 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME)) 1701 return AE_ALREADY_EXISTS; 1702 1703 return AE_OK; 1704 } 1705 1706 static void acpi_video_dev_register_backlight(struct acpi_video_device *device) 1707 { 1708 struct backlight_properties props; 1709 struct pci_dev *pdev; 1710 acpi_handle acpi_parent; 1711 struct device *parent = NULL; 1712 int result; 1713 static int count; 1714 char *name; 1715 1716 result = acpi_video_init_brightness(device); 1717 if (result) 1718 return; 1719 1720 name = kasprintf(GFP_KERNEL, "acpi_video%d", count); 1721 if (!name) 1722 return; 1723 count++; 1724 1725 if (ACPI_SUCCESS(acpi_get_parent(device->dev->handle, &acpi_parent))) { 1726 pdev = acpi_get_pci_dev(acpi_parent); 1727 if (pdev) { 1728 parent = &pdev->dev; 1729 pci_dev_put(pdev); 1730 } 1731 } 1732 1733 memset(&props, 0, sizeof(struct backlight_properties)); 1734 props.type = BACKLIGHT_FIRMWARE; 1735 props.max_brightness = 1736 device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1; 1737 device->backlight = backlight_device_register(name, 1738 parent, 1739 device, 1740 &acpi_backlight_ops, 1741 &props); 1742 kfree(name); 1743 if (IS_ERR(device->backlight)) { 1744 device->backlight = NULL; 1745 return; 1746 } 1747 1748 /* 1749 * Save current brightness level in case we have to restore it 1750 * before acpi_video_device_lcd_set_level() is called next time. 1751 */ 1752 device->backlight->props.brightness = 1753 acpi_video_get_brightness(device->backlight); 1754 1755 device->cooling_dev = thermal_cooling_device_register("LCD", device, 1756 &video_cooling_ops); 1757 if (IS_ERR(device->cooling_dev)) { 1758 /* 1759 * Set cooling_dev to NULL so we don't crash trying to free it. 1760 * Also, why the hell we are returning early and not attempt to 1761 * register video output if cooling device registration failed? 1762 * -- dtor 1763 */ 1764 device->cooling_dev = NULL; 1765 return; 1766 } 1767 1768 dev_info(&device->dev->dev, "registered as cooling_device%d\n", 1769 device->cooling_dev->id); 1770 result = sysfs_create_link(&device->dev->dev.kobj, 1771 &device->cooling_dev->device.kobj, 1772 "thermal_cooling"); 1773 if (result) 1774 pr_info("sysfs link creation failed\n"); 1775 1776 result = sysfs_create_link(&device->cooling_dev->device.kobj, 1777 &device->dev->dev.kobj, "device"); 1778 if (result) 1779 pr_info("Reverse sysfs link creation failed\n"); 1780 } 1781 1782 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video) 1783 { 1784 struct acpi_video_device *dev; 1785 union acpi_object *levels; 1786 1787 mutex_lock(&video->device_list_lock); 1788 list_for_each_entry(dev, &video->video_device_list, entry) { 1789 if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels)) 1790 kfree(levels); 1791 } 1792 mutex_unlock(&video->device_list_lock); 1793 } 1794 1795 static bool acpi_video_should_register_backlight(struct acpi_video_device *dev) 1796 { 1797 /* 1798 * Do not create backlight device for video output 1799 * device that is not in the enumerated list. 1800 */ 1801 if (!acpi_video_device_in_dod(dev)) { 1802 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n"); 1803 return false; 1804 } 1805 1806 if (only_lcd) 1807 return dev->flags.lcd; 1808 return true; 1809 } 1810 1811 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video) 1812 { 1813 struct acpi_video_device *dev; 1814 1815 if (video->backlight_registered) 1816 return 0; 1817 1818 if (acpi_video_get_backlight_type() != acpi_backlight_video) 1819 return 0; 1820 1821 mutex_lock(&video->device_list_lock); 1822 list_for_each_entry(dev, &video->video_device_list, entry) { 1823 if (acpi_video_should_register_backlight(dev)) 1824 acpi_video_dev_register_backlight(dev); 1825 } 1826 mutex_unlock(&video->device_list_lock); 1827 1828 video->backlight_registered = true; 1829 1830 video->pm_nb.notifier_call = acpi_video_resume; 1831 video->pm_nb.priority = 0; 1832 return register_pm_notifier(&video->pm_nb); 1833 } 1834 1835 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device) 1836 { 1837 if (device->backlight) { 1838 backlight_device_unregister(device->backlight); 1839 device->backlight = NULL; 1840 } 1841 if (device->brightness) { 1842 kfree(device->brightness->levels); 1843 kfree(device->brightness); 1844 device->brightness = NULL; 1845 } 1846 if (device->cooling_dev) { 1847 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling"); 1848 sysfs_remove_link(&device->cooling_dev->device.kobj, "device"); 1849 thermal_cooling_device_unregister(device->cooling_dev); 1850 device->cooling_dev = NULL; 1851 } 1852 } 1853 1854 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video) 1855 { 1856 struct acpi_video_device *dev; 1857 int error; 1858 1859 if (!video->backlight_registered) 1860 return 0; 1861 1862 error = unregister_pm_notifier(&video->pm_nb); 1863 1864 mutex_lock(&video->device_list_lock); 1865 list_for_each_entry(dev, &video->video_device_list, entry) 1866 acpi_video_dev_unregister_backlight(dev); 1867 mutex_unlock(&video->device_list_lock); 1868 1869 video->backlight_registered = false; 1870 1871 return error; 1872 } 1873 1874 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device) 1875 { 1876 acpi_status status; 1877 struct acpi_device *adev = device->dev; 1878 1879 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 1880 acpi_video_device_notify, device); 1881 if (ACPI_FAILURE(status)) 1882 dev_err(&adev->dev, "Error installing notify handler\n"); 1883 else 1884 device->flags.notify = 1; 1885 } 1886 1887 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video, 1888 struct platform_device *pdev) 1889 { 1890 struct input_dev *input; 1891 struct acpi_video_device *dev; 1892 int error; 1893 1894 video->input = input = input_allocate_device(); 1895 if (!input) { 1896 error = -ENOMEM; 1897 goto out; 1898 } 1899 1900 error = acpi_video_bus_start_devices(video); 1901 if (error) 1902 goto err_free_input; 1903 1904 snprintf(video->phys, sizeof(video->phys), 1905 "%s/video/input0", acpi_device_hid(video->device)); 1906 1907 input->name = acpi_device_name(video->device); 1908 input->phys = video->phys; 1909 input->id.bustype = BUS_HOST; 1910 input->id.product = 0x06; 1911 input->dev.parent = &pdev->dev; 1912 input->evbit[0] = BIT(EV_KEY); 1913 set_bit(KEY_SWITCHVIDEOMODE, input->keybit); 1914 set_bit(KEY_VIDEO_NEXT, input->keybit); 1915 set_bit(KEY_VIDEO_PREV, input->keybit); 1916 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit); 1917 set_bit(KEY_BRIGHTNESSUP, input->keybit); 1918 set_bit(KEY_BRIGHTNESSDOWN, input->keybit); 1919 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit); 1920 set_bit(KEY_DISPLAY_OFF, input->keybit); 1921 1922 error = input_register_device(input); 1923 if (error) 1924 goto err_stop_dev; 1925 1926 mutex_lock(&video->device_list_lock); 1927 list_for_each_entry(dev, &video->video_device_list, entry) 1928 acpi_video_dev_add_notify_handler(dev); 1929 mutex_unlock(&video->device_list_lock); 1930 1931 return 0; 1932 1933 err_stop_dev: 1934 acpi_video_bus_stop_devices(video); 1935 err_free_input: 1936 input_free_device(input); 1937 video->input = NULL; 1938 out: 1939 return error; 1940 } 1941 1942 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev) 1943 { 1944 if (dev->flags.notify) { 1945 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY, 1946 acpi_video_device_notify); 1947 dev->flags.notify = 0; 1948 } 1949 } 1950 1951 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video) 1952 { 1953 struct acpi_video_device *dev; 1954 1955 mutex_lock(&video->device_list_lock); 1956 list_for_each_entry(dev, &video->video_device_list, entry) { 1957 acpi_video_dev_remove_notify_handler(dev); 1958 cancel_delayed_work_sync(&dev->switch_brightness_work); 1959 } 1960 mutex_unlock(&video->device_list_lock); 1961 1962 acpi_video_bus_stop_devices(video); 1963 input_unregister_device(video->input); 1964 video->input = NULL; 1965 } 1966 1967 static int acpi_video_bus_put_devices(struct acpi_video_bus *video) 1968 { 1969 struct acpi_video_device *dev, *next; 1970 1971 mutex_lock(&video->device_list_lock); 1972 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) { 1973 list_del(&dev->entry); 1974 kfree(dev); 1975 } 1976 mutex_unlock(&video->device_list_lock); 1977 1978 return 0; 1979 } 1980 1981 static int instance; 1982 1983 static int acpi_video_bus_probe(struct platform_device *pdev) 1984 { 1985 struct acpi_device *device = ACPI_COMPANION(&pdev->dev); 1986 struct acpi_video_bus *video; 1987 bool auto_detect; 1988 int error; 1989 acpi_status status; 1990 1991 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, 1992 acpi_dev_parent(device)->handle, 1, 1993 acpi_video_bus_match, NULL, 1994 device, NULL); 1995 if (status == AE_ALREADY_EXISTS) { 1996 pr_info(FW_BUG 1997 "Duplicate ACPI video bus devices for the" 1998 " same VGA controller, please try module " 1999 "parameter \"video.allow_duplicates=1\"" 2000 "if the current driver doesn't work.\n"); 2001 if (!allow_duplicates) 2002 return -ENODEV; 2003 } 2004 2005 video = kzalloc_obj(struct acpi_video_bus); 2006 if (!video) 2007 return -ENOMEM; 2008 2009 /* a hack to fix the duplicate name "VID" problem on T61 */ 2010 if (!strcmp(device->pnp.bus_id, "VID")) { 2011 if (instance) 2012 device->pnp.bus_id[3] = '0' + instance; 2013 instance++; 2014 } 2015 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */ 2016 if (!strcmp(device->pnp.bus_id, "VGA")) { 2017 if (instance) 2018 device->pnp.bus_id[3] = '0' + instance; 2019 instance++; 2020 } 2021 2022 platform_set_drvdata(pdev, video); 2023 2024 video->device = device; 2025 strscpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME); 2026 strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS); 2027 device->driver_data = video; 2028 2029 acpi_video_bus_find_cap(video); 2030 error = acpi_video_bus_check(video); 2031 if (error) 2032 goto err_free_video; 2033 2034 mutex_init(&video->device_list_lock); 2035 INIT_LIST_HEAD(&video->video_device_list); 2036 2037 error = acpi_video_bus_get_devices(video, device); 2038 if (error) 2039 goto err_put_video; 2040 2041 /* 2042 * HP ZBook Fury 16 G10 requires ACPI video's child devices have _PS0 2043 * evaluated to have functional panel brightness control. 2044 */ 2045 acpi_device_fix_up_power_children(device); 2046 2047 pr_info("%s [%s] (multi-head: %s rom: %s post: %s)\n", 2048 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device), 2049 str_yes_no(video->flags.multihead), 2050 str_yes_no(video->flags.rom), 2051 str_yes_no(video->flags.post)); 2052 mutex_lock(&video_list_lock); 2053 list_add_tail(&video->entry, &video_bus_head); 2054 mutex_unlock(&video_list_lock); 2055 2056 /* 2057 * If backlight-type auto-detection is used then a native backlight may 2058 * show up later and this may change the result from video to native. 2059 * Therefor normally the userspace visible /sys/class/backlight device 2060 * gets registered separately by the GPU driver calling 2061 * acpi_video_register_backlight() when an internal panel is detected. 2062 * Register the backlight now when not using auto-detection, so that 2063 * when the kernel cmdline or DMI-quirks are used the backlight will 2064 * get registered even if acpi_video_register_backlight() is not called. 2065 */ 2066 acpi_video_run_bcl_for_osi(video); 2067 if (__acpi_video_get_backlight_type(false, &auto_detect) == acpi_backlight_video && 2068 !auto_detect) 2069 acpi_video_bus_register_backlight(video); 2070 2071 error = acpi_video_bus_add_notify_handler(video, pdev); 2072 if (error) 2073 goto err_del; 2074 2075 error = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY, 2076 acpi_video_bus_notify, video); 2077 if (error) 2078 goto err_remove; 2079 2080 return 0; 2081 2082 err_remove: 2083 acpi_video_bus_remove_notify_handler(video); 2084 err_del: 2085 mutex_lock(&video_list_lock); 2086 list_del(&video->entry); 2087 mutex_unlock(&video_list_lock); 2088 acpi_video_bus_unregister_backlight(video); 2089 err_put_video: 2090 acpi_video_bus_put_devices(video); 2091 kfree(video->attached_array); 2092 err_free_video: 2093 kfree(video); 2094 device->driver_data = NULL; 2095 2096 return error; 2097 } 2098 2099 static void acpi_video_bus_remove(struct platform_device *pdev) 2100 { 2101 struct acpi_video_bus *video = platform_get_drvdata(pdev); 2102 struct acpi_device *device = ACPI_COMPANION(&pdev->dev); 2103 2104 acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY, 2105 acpi_video_bus_notify); 2106 2107 mutex_lock(&video_list_lock); 2108 list_del(&video->entry); 2109 mutex_unlock(&video_list_lock); 2110 2111 acpi_video_bus_remove_notify_handler(video); 2112 acpi_video_bus_unregister_backlight(video); 2113 acpi_video_bus_put_devices(video); 2114 2115 kfree(video->attached_array); 2116 kfree(video); 2117 device->driver_data = NULL; 2118 } 2119 2120 static int __init is_i740(struct pci_dev *dev) 2121 { 2122 if (dev->device == 0x00D1) 2123 return 1; 2124 if (dev->device == 0x7000) 2125 return 1; 2126 return 0; 2127 } 2128 2129 static int __init intel_opregion_present(void) 2130 { 2131 int opregion = 0; 2132 struct pci_dev *dev = NULL; 2133 u32 address; 2134 2135 for_each_pci_dev(dev) { 2136 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) 2137 continue; 2138 if (dev->vendor != PCI_VENDOR_ID_INTEL) 2139 continue; 2140 /* We don't want to poke around undefined i740 registers */ 2141 if (is_i740(dev)) 2142 continue; 2143 pci_read_config_dword(dev, 0xfc, &address); 2144 if (!address) 2145 continue; 2146 opregion = 1; 2147 } 2148 return opregion; 2149 } 2150 2151 int acpi_video_register(void) 2152 { 2153 int ret = 0; 2154 2155 mutex_lock(®ister_count_mutex); 2156 if (register_count) { 2157 /* 2158 * if the function of acpi_video_register is already called, 2159 * don't register the acpi_video_bus again and return no error. 2160 */ 2161 goto leave; 2162 } 2163 2164 dmi_check_system(video_dmi_table); 2165 2166 ret = platform_driver_register(&acpi_video_bus); 2167 if (ret) 2168 goto leave; 2169 2170 /* 2171 * When the acpi_video_bus is loaded successfully, increase 2172 * the counter reference. 2173 */ 2174 register_count = 1; 2175 2176 leave: 2177 mutex_unlock(®ister_count_mutex); 2178 return ret; 2179 } 2180 EXPORT_SYMBOL(acpi_video_register); 2181 2182 void acpi_video_unregister(void) 2183 { 2184 mutex_lock(®ister_count_mutex); 2185 if (register_count) { 2186 platform_driver_unregister(&acpi_video_bus); 2187 register_count = 0; 2188 may_report_brightness_keys = false; 2189 } 2190 mutex_unlock(®ister_count_mutex); 2191 } 2192 EXPORT_SYMBOL(acpi_video_unregister); 2193 2194 void acpi_video_register_backlight(void) 2195 { 2196 struct acpi_video_bus *video; 2197 2198 mutex_lock(&video_list_lock); 2199 list_for_each_entry(video, &video_bus_head, entry) 2200 acpi_video_bus_register_backlight(video); 2201 mutex_unlock(&video_list_lock); 2202 } 2203 EXPORT_SYMBOL(acpi_video_register_backlight); 2204 2205 bool acpi_video_handles_brightness_key_presses(void) 2206 { 2207 return may_report_brightness_keys && 2208 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS); 2209 } 2210 EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses); 2211 2212 /* 2213 * This is kind of nasty. Hardware using Intel chipsets may require 2214 * the video opregion code to be run first in order to initialise 2215 * state before any ACPI video calls are made. To handle this we defer 2216 * registration of the video class until the opregion code has run. 2217 */ 2218 2219 static int __init acpi_video_init(void) 2220 { 2221 /* 2222 * Let the module load even if ACPI is disabled (e.g. due to 2223 * a broken BIOS) so that i915.ko can still be loaded on such 2224 * old systems without an AcpiOpRegion. 2225 * 2226 * acpi_video_register() will report -ENODEV later as well due 2227 * to acpi_disabled when i915.ko tries to register itself afterwards. 2228 */ 2229 if (acpi_disabled) 2230 return 0; 2231 2232 if (intel_opregion_present()) 2233 return 0; 2234 2235 return acpi_video_register(); 2236 } 2237 2238 static void __exit acpi_video_exit(void) 2239 { 2240 acpi_video_unregister(); 2241 } 2242 2243 module_init(acpi_video_init); 2244 module_exit(acpi_video_exit); 2245