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