1 /*- 2 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/power.h> 39 #include <sys/queue.h> 40 #include <sys/sysctl.h> 41 42 #include <contrib/dev/acpica/include/acpi.h> 43 44 #include <dev/acpica/acpivar.h> 45 46 /* ACPI video extension driver. */ 47 struct acpi_video_output { 48 ACPI_HANDLE handle; 49 UINT32 adr; 50 STAILQ_ENTRY(acpi_video_output) vo_next; 51 struct { 52 int num; 53 STAILQ_ENTRY(acpi_video_output) next; 54 } vo_unit; 55 int vo_brightness; 56 int vo_fullpower; 57 int vo_economy; 58 int vo_numlevels; 59 int *vo_levels; 60 struct sysctl_ctx_list vo_sysctl_ctx; 61 struct sysctl_oid *vo_sysctl_tree; 62 }; 63 64 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output); 65 66 struct acpi_video_softc { 67 device_t device; 68 ACPI_HANDLE handle; 69 struct acpi_video_output_queue vid_outputs; 70 eventhandler_tag vid_pwr_evh; 71 }; 72 73 /* interfaces */ 74 static int acpi_video_modevent(struct module*, int, void *); 75 static void acpi_video_identify(driver_t *driver, device_t parent); 76 static int acpi_video_probe(device_t); 77 static int acpi_video_attach(device_t); 78 static int acpi_video_detach(device_t); 79 static int acpi_video_resume(device_t); 80 static int acpi_video_shutdown(device_t); 81 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *); 82 static void acpi_video_power_profile(void *); 83 static void acpi_video_bind_outputs(struct acpi_video_softc *); 84 static struct acpi_video_output *acpi_video_vo_init(UINT32); 85 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE); 86 static void acpi_video_vo_destroy(struct acpi_video_output *); 87 static int acpi_video_vo_check_level(struct acpi_video_output *, int); 88 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *); 89 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS); 90 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS); 91 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS); 92 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS); 93 94 /* operations */ 95 static void vid_set_switch_policy(ACPI_HANDLE, UINT32); 96 static int vid_enum_outputs(ACPI_HANDLE, 97 void(*)(ACPI_HANDLE, UINT32, void *), void *); 98 static int vo_get_brightness_levels(ACPI_HANDLE, int **); 99 static int vo_get_brightness(ACPI_HANDLE); 100 static void vo_set_brightness(ACPI_HANDLE, int); 101 static UINT32 vo_get_device_status(ACPI_HANDLE); 102 static UINT32 vo_get_graphics_state(ACPI_HANDLE); 103 static void vo_set_device_state(ACPI_HANDLE, UINT32); 104 105 /* events */ 106 #define VID_NOTIFY_SWITCHED 0x80 107 #define VID_NOTIFY_REPROBE 0x81 108 #define VID_NOTIFY_CYCLE_BRN 0x85 109 #define VID_NOTIFY_INC_BRN 0x86 110 #define VID_NOTIFY_DEC_BRN 0x87 111 #define VID_NOTIFY_ZERO_BRN 0x88 112 113 /* _DOS (Enable/Disable Output Switching) argument bits */ 114 #define DOS_SWITCH_MASK 3 115 #define DOS_SWITCH_BY_OSPM 0 116 #define DOS_SWITCH_BY_BIOS 1 117 #define DOS_SWITCH_LOCKED 2 118 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2) 119 120 /* _DOD and subdev's _ADR */ 121 #define DOD_DEVID_MASK 0x0f00 122 #define DOD_DEVID_MASK_FULL 0xffff 123 #define DOD_DEVID_MASK_DISPIDX 0x000f 124 #define DOD_DEVID_MASK_DISPPORT 0x00f0 125 #define DOD_DEVID_MONITOR 0x0100 126 #define DOD_DEVID_LCD 0x0110 127 #define DOD_DEVID_TV 0x0200 128 #define DOD_DEVID_EXT 0x0300 129 #define DOD_DEVID_INTDFP 0x0400 130 #define DOD_BIOS (1 << 16) 131 #define DOD_NONVGA (1 << 17) 132 #define DOD_HEAD_ID_SHIFT 18 133 #define DOD_HEAD_ID_BITS 3 134 #define DOD_HEAD_ID_MASK \ 135 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT) 136 #define DOD_DEVID_SCHEME_STD (1U << 31) 137 138 /* _BCL related constants */ 139 #define BCL_FULLPOWER 0 140 #define BCL_ECONOMY 1 141 142 /* _DCS (Device Currrent Status) value bits and masks. */ 143 #define DCS_EXISTS (1 << 0) 144 #define DCS_ACTIVE (1 << 1) 145 #define DCS_READY (1 << 2) 146 #define DCS_FUNCTIONAL (1 << 3) 147 #define DCS_ATTACHED (1 << 4) 148 149 /* _DSS (Device Set Status) argument bits and masks. */ 150 #define DSS_INACTIVE 0 151 #define DSS_ACTIVE (1 << 0) 152 #define DSS_SETNEXT (1 << 30) 153 #define DSS_COMMIT (1U << 31) 154 155 static device_method_t acpi_video_methods[] = { 156 DEVMETHOD(device_identify, acpi_video_identify), 157 DEVMETHOD(device_probe, acpi_video_probe), 158 DEVMETHOD(device_attach, acpi_video_attach), 159 DEVMETHOD(device_detach, acpi_video_detach), 160 DEVMETHOD(device_resume, acpi_video_resume), 161 DEVMETHOD(device_shutdown, acpi_video_shutdown), 162 { 0, 0 } 163 }; 164 165 static driver_t acpi_video_driver = { 166 "acpi_video", 167 acpi_video_methods, 168 sizeof(struct acpi_video_softc), 169 }; 170 171 static devclass_t acpi_video_devclass; 172 173 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass, 174 acpi_video_modevent, NULL); 175 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1); 176 177 static struct sysctl_ctx_list acpi_video_sysctl_ctx; 178 static struct sysctl_oid *acpi_video_sysctl_tree; 179 static struct acpi_video_output_queue crt_units, tv_units, 180 ext_units, lcd_units, other_units; 181 182 /* 183 * The 'video' lock protects the hierarchy of video output devices 184 * (the video "bus"). The 'video_output' lock protects per-output 185 * data is equivalent to a softc lock for each video output. 186 */ 187 ACPI_SERIAL_DECL(video, "ACPI video"); 188 ACPI_SERIAL_DECL(video_output, "ACPI video output"); 189 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension"); 190 191 static int 192 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused) 193 { 194 int err; 195 196 err = 0; 197 switch (evt) { 198 case MOD_LOAD: 199 sysctl_ctx_init(&acpi_video_sysctl_ctx); 200 STAILQ_INIT(&crt_units); 201 STAILQ_INIT(&tv_units); 202 STAILQ_INIT(&ext_units); 203 STAILQ_INIT(&lcd_units); 204 STAILQ_INIT(&other_units); 205 break; 206 case MOD_UNLOAD: 207 sysctl_ctx_free(&acpi_video_sysctl_ctx); 208 acpi_video_sysctl_tree = NULL; 209 break; 210 default: 211 err = EINVAL; 212 } 213 214 return (err); 215 } 216 217 static void 218 acpi_video_identify(driver_t *driver, device_t parent) 219 { 220 221 if (device_find_child(parent, "acpi_video", -1) == NULL) 222 device_add_child(parent, "acpi_video", -1); 223 } 224 225 static int 226 acpi_video_probe(device_t dev) 227 { 228 ACPI_HANDLE devh, h; 229 ACPI_OBJECT_TYPE t_dos; 230 231 devh = acpi_get_handle(dev); 232 if (acpi_disabled("video") || 233 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) || 234 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) || 235 ACPI_FAILURE(AcpiGetType(h, &t_dos)) || 236 t_dos != ACPI_TYPE_METHOD) 237 return (ENXIO); 238 239 device_set_desc(dev, "ACPI video extension"); 240 return (0); 241 } 242 243 static int 244 acpi_video_attach(device_t dev) 245 { 246 struct acpi_softc *acpi_sc; 247 struct acpi_video_softc *sc; 248 249 sc = device_get_softc(dev); 250 251 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0); 252 if (acpi_sc == NULL) 253 return (ENXIO); 254 ACPI_SERIAL_BEGIN(video); 255 if (acpi_video_sysctl_tree == NULL) { 256 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx, 257 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 258 OID_AUTO, "video", CTLFLAG_RD, 0, 259 "video extension control"); 260 } 261 ACPI_SERIAL_END(video); 262 263 sc->device = dev; 264 sc->handle = acpi_get_handle(dev); 265 STAILQ_INIT(&sc->vid_outputs); 266 267 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 268 acpi_video_notify_handler, sc); 269 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change, 270 acpi_video_power_profile, sc, 0); 271 272 ACPI_SERIAL_BEGIN(video); 273 acpi_video_bind_outputs(sc); 274 ACPI_SERIAL_END(video); 275 276 /* 277 * Notify the BIOS that we want to switch both active outputs and 278 * brightness levels. 279 */ 280 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM | 281 DOS_BRIGHTNESS_BY_OSPM); 282 283 acpi_video_power_profile(sc); 284 285 return (0); 286 } 287 288 static int 289 acpi_video_detach(device_t dev) 290 { 291 struct acpi_video_softc *sc; 292 struct acpi_video_output *vo, *vn; 293 294 sc = device_get_softc(dev); 295 296 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS); 297 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh); 298 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 299 acpi_video_notify_handler); 300 301 ACPI_SERIAL_BEGIN(video); 302 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) { 303 acpi_video_vo_destroy(vo); 304 } 305 ACPI_SERIAL_END(video); 306 307 return (0); 308 } 309 310 static int 311 acpi_video_resume(device_t dev) 312 { 313 struct acpi_video_softc *sc; 314 struct acpi_video_output *vo, *vn; 315 int level; 316 317 sc = device_get_softc(dev); 318 319 /* Restore brightness level */ 320 ACPI_SERIAL_BEGIN(video); 321 ACPI_SERIAL_BEGIN(video_output); 322 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) { 323 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD && 324 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP) 325 continue; 326 327 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0) 328 continue; 329 330 level = vo_get_brightness(vo->handle); 331 if (level != -1) 332 vo_set_brightness(vo->handle, level); 333 } 334 ACPI_SERIAL_END(video_output); 335 ACPI_SERIAL_END(video); 336 337 return (0); 338 } 339 340 static int 341 acpi_video_shutdown(device_t dev) 342 { 343 struct acpi_video_softc *sc; 344 345 sc = device_get_softc(dev); 346 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS); 347 348 return (0); 349 } 350 351 static void 352 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context) 353 { 354 struct acpi_video_softc *sc; 355 struct acpi_video_output *vo, *vo_tmp; 356 ACPI_HANDLE lasthand; 357 UINT32 dcs, dss, dss_p; 358 359 sc = (struct acpi_video_softc *)context; 360 361 switch (notify) { 362 case VID_NOTIFY_SWITCHED: 363 dss_p = 0; 364 lasthand = NULL; 365 ACPI_SERIAL_BEGIN(video); 366 ACPI_SERIAL_BEGIN(video_output); 367 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 368 dss = vo_get_graphics_state(vo->handle); 369 dcs = vo_get_device_status(vo->handle); 370 if (!(dcs & DCS_READY)) 371 dss = DSS_INACTIVE; 372 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) || 373 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) { 374 if (lasthand != NULL) 375 vo_set_device_state(lasthand, dss_p); 376 dss_p = dss; 377 lasthand = vo->handle; 378 } 379 } 380 if (lasthand != NULL) 381 vo_set_device_state(lasthand, dss_p|DSS_COMMIT); 382 ACPI_SERIAL_END(video_output); 383 ACPI_SERIAL_END(video); 384 break; 385 case VID_NOTIFY_REPROBE: 386 ACPI_SERIAL_BEGIN(video); 387 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) 388 vo->handle = NULL; 389 acpi_video_bind_outputs(sc); 390 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) { 391 if (vo->handle == NULL) { 392 STAILQ_REMOVE(&sc->vid_outputs, vo, 393 acpi_video_output, vo_next); 394 acpi_video_vo_destroy(vo); 395 } 396 } 397 ACPI_SERIAL_END(video); 398 break; 399 default: 400 device_printf(sc->device, "unknown notify event 0x%x\n", 401 notify); 402 } 403 } 404 405 static void 406 acpi_video_power_profile(void *context) 407 { 408 int state; 409 struct acpi_video_softc *sc; 410 struct acpi_video_output *vo; 411 412 sc = context; 413 state = power_profile_get_state(); 414 if (state != POWER_PROFILE_PERFORMANCE && 415 state != POWER_PROFILE_ECONOMY) 416 return; 417 418 ACPI_SERIAL_BEGIN(video); 419 ACPI_SERIAL_BEGIN(video_output); 420 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 421 if (vo->vo_levels != NULL && vo->vo_brightness == -1) 422 vo_set_brightness(vo->handle, 423 state == POWER_PROFILE_ECONOMY ? 424 vo->vo_economy : vo->vo_fullpower); 425 } 426 ACPI_SERIAL_END(video_output); 427 ACPI_SERIAL_END(video); 428 } 429 430 static void 431 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context) 432 { 433 struct acpi_video_softc *sc; 434 struct acpi_video_output *vo; 435 436 ACPI_SERIAL_ASSERT(video); 437 sc = context; 438 439 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 440 if (vo->adr == adr) { 441 acpi_video_vo_bind(vo, handle); 442 return; 443 } 444 } 445 vo = acpi_video_vo_init(adr); 446 if (vo != NULL) { 447 acpi_video_vo_bind(vo, handle); 448 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next); 449 } 450 } 451 452 static void 453 acpi_video_bind_outputs(struct acpi_video_softc *sc) 454 { 455 456 ACPI_SERIAL_ASSERT(video); 457 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc); 458 } 459 460 static struct acpi_video_output * 461 acpi_video_vo_init(UINT32 adr) 462 { 463 struct acpi_video_output *vn, *vo, *vp; 464 int n, x; 465 char name[8], env[32]; 466 const char *type, *desc; 467 struct acpi_video_output_queue *voqh; 468 469 ACPI_SERIAL_ASSERT(video); 470 471 switch (adr & DOD_DEVID_MASK) { 472 case DOD_DEVID_MONITOR: 473 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) { 474 /* DOD_DEVID_LCD is a common, backward compatible ID */ 475 desc = "Internal/Integrated Digital Flat Panel"; 476 type = "lcd"; 477 voqh = &lcd_units; 478 } else { 479 desc = "VGA CRT or VESA Compatible Analog Monitor"; 480 type = "crt"; 481 voqh = &crt_units; 482 } 483 break; 484 case DOD_DEVID_TV: 485 desc = "TV/HDTV or Analog-Video Monitor"; 486 type = "tv"; 487 voqh = &tv_units; 488 break; 489 case DOD_DEVID_EXT: 490 desc = "External Digital Monitor"; 491 type = "ext"; 492 voqh = &ext_units; 493 break; 494 case DOD_DEVID_INTDFP: 495 desc = "Internal/Integrated Digital Flat Panel"; 496 type = "lcd"; 497 voqh = &lcd_units; 498 break; 499 default: 500 desc = "unknown output"; 501 type = "out"; 502 voqh = &other_units; 503 } 504 505 n = 0; 506 vp = NULL; 507 STAILQ_FOREACH(vn, voqh, vo_unit.next) { 508 if (vn->vo_unit.num != n) 509 break; 510 vp = vn; 511 n++; 512 } 513 514 snprintf(name, sizeof(name), "%s%d", type, n); 515 516 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT); 517 if (vo != NULL) { 518 vo->handle = NULL; 519 vo->adr = adr; 520 vo->vo_unit.num = n; 521 vo->vo_brightness = -1; 522 vo->vo_fullpower = -1; /* TODO: override with tunables */ 523 vo->vo_economy = -1; 524 vo->vo_numlevels = 0; 525 vo->vo_levels = NULL; 526 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name); 527 if (getenv_int(env, &x)) 528 vo->vo_fullpower = x; 529 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name); 530 if (getenv_int(env, &x)) 531 vo->vo_economy = x; 532 533 sysctl_ctx_init(&vo->vo_sysctl_ctx); 534 if (vp != NULL) 535 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next); 536 else 537 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next); 538 if (acpi_video_sysctl_tree != NULL) 539 vo->vo_sysctl_tree = 540 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx, 541 SYSCTL_CHILDREN(acpi_video_sysctl_tree), 542 OID_AUTO, name, CTLFLAG_RD, 0, desc); 543 if (vo->vo_sysctl_tree != NULL) { 544 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 545 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 546 OID_AUTO, "active", 547 CTLTYPE_INT|CTLFLAG_RW, vo, 0, 548 acpi_video_vo_active_sysctl, "I", 549 "current activity of this device"); 550 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 551 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 552 OID_AUTO, "brightness", 553 CTLTYPE_INT|CTLFLAG_RW, vo, 0, 554 acpi_video_vo_bright_sysctl, "I", 555 "current brightness level"); 556 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 557 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 558 OID_AUTO, "fullpower", 559 CTLTYPE_INT|CTLFLAG_RW, vo, 560 POWER_PROFILE_PERFORMANCE, 561 acpi_video_vo_presets_sysctl, "I", 562 "preset level for full power mode"); 563 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 564 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 565 OID_AUTO, "economy", 566 CTLTYPE_INT|CTLFLAG_RW, vo, 567 POWER_PROFILE_ECONOMY, 568 acpi_video_vo_presets_sysctl, "I", 569 "preset level for economy mode"); 570 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 571 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 572 OID_AUTO, "levels", 573 CTLTYPE_INT | CTLFLAG_RD, vo, 0, 574 acpi_video_vo_levels_sysctl, "I", 575 "supported brightness levels"); 576 } else 577 printf("%s: sysctl node creation failed\n", type); 578 } else 579 printf("%s: softc allocation failed\n", type); 580 581 if (bootverbose) { 582 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL); 583 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX); 584 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4); 585 if (adr & DOD_BIOS) 586 printf(", detectable by BIOS"); 587 if (adr & DOD_NONVGA) 588 printf(" (Non-VGA output device whose power " 589 "is related to the VGA device)"); 590 printf(", head #%d\n", 591 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT); 592 } 593 return (vo); 594 } 595 596 static void 597 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle) 598 { 599 600 ACPI_SERIAL_BEGIN(video_output); 601 if (vo->vo_levels != NULL) { 602 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY, 603 acpi_video_vo_notify_handler); 604 AcpiOsFree(vo->vo_levels); 605 vo->vo_levels = NULL; 606 } 607 vo->handle = handle; 608 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels); 609 if (vo->vo_numlevels >= 2) { 610 if (vo->vo_fullpower == -1 || 611 acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) { 612 /* XXX - can't deal with rebinding... */ 613 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER]; 614 } 615 if (vo->vo_economy == -1 || 616 acpi_video_vo_check_level(vo, vo->vo_economy) != 0) { 617 /* XXX - see above. */ 618 vo->vo_economy = vo->vo_levels[BCL_ECONOMY]; 619 } 620 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, 621 acpi_video_vo_notify_handler, vo); 622 } 623 ACPI_SERIAL_END(video_output); 624 } 625 626 static void 627 acpi_video_vo_destroy(struct acpi_video_output *vo) 628 { 629 struct acpi_video_output_queue *voqh; 630 631 ACPI_SERIAL_ASSERT(video); 632 if (vo->vo_sysctl_tree != NULL) { 633 vo->vo_sysctl_tree = NULL; 634 sysctl_ctx_free(&vo->vo_sysctl_ctx); 635 } 636 if (vo->vo_levels != NULL) { 637 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY, 638 acpi_video_vo_notify_handler); 639 AcpiOsFree(vo->vo_levels); 640 } 641 642 switch (vo->adr & DOD_DEVID_MASK) { 643 case DOD_DEVID_MONITOR: 644 voqh = &crt_units; 645 break; 646 case DOD_DEVID_TV: 647 voqh = &tv_units; 648 break; 649 case DOD_DEVID_EXT: 650 voqh = &ext_units; 651 break; 652 case DOD_DEVID_INTDFP: 653 voqh = &lcd_units; 654 break; 655 default: 656 voqh = &other_units; 657 } 658 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next); 659 free(vo, M_ACPIVIDEO); 660 } 661 662 static int 663 acpi_video_vo_check_level(struct acpi_video_output *vo, int level) 664 { 665 int i; 666 667 ACPI_SERIAL_ASSERT(video_output); 668 if (vo->vo_levels == NULL) 669 return (ENODEV); 670 for (i = 0; i < vo->vo_numlevels; i++) 671 if (vo->vo_levels[i] == level) 672 return (0); 673 return (EINVAL); 674 } 675 676 static void 677 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context) 678 { 679 struct acpi_video_output *vo; 680 int i, j, level, new_level; 681 682 vo = context; 683 ACPI_SERIAL_BEGIN(video_output); 684 if (vo->handle != handle) 685 goto out; 686 687 switch (notify) { 688 case VID_NOTIFY_CYCLE_BRN: 689 if (vo->vo_numlevels <= 3) 690 goto out; 691 /* FALLTHROUGH */ 692 case VID_NOTIFY_INC_BRN: 693 case VID_NOTIFY_DEC_BRN: 694 case VID_NOTIFY_ZERO_BRN: 695 if (vo->vo_levels == NULL) 696 goto out; 697 level = vo_get_brightness(handle); 698 if (level < 0) 699 goto out; 700 break; 701 default: 702 printf("unknown notify event 0x%x from %s\n", 703 notify, acpi_name(handle)); 704 goto out; 705 } 706 707 new_level = level; 708 switch (notify) { 709 case VID_NOTIFY_CYCLE_BRN: 710 for (i = 2; i < vo->vo_numlevels; i++) 711 if (vo->vo_levels[i] == level) { 712 new_level = vo->vo_numlevels > i + 1 ? 713 vo->vo_levels[i + 1] : vo->vo_levels[2]; 714 break; 715 } 716 break; 717 case VID_NOTIFY_INC_BRN: 718 case VID_NOTIFY_DEC_BRN: 719 for (i = 0; i < vo->vo_numlevels; i++) { 720 j = vo->vo_levels[i]; 721 if (notify == VID_NOTIFY_INC_BRN) { 722 if (j > level && 723 (j < new_level || level == new_level)) 724 new_level = j; 725 } else { 726 if (j < level && 727 (j > new_level || level == new_level)) 728 new_level = j; 729 } 730 } 731 break; 732 case VID_NOTIFY_ZERO_BRN: 733 for (i = 0; i < vo->vo_numlevels; i++) 734 if (vo->vo_levels[i] == 0) { 735 new_level = 0; 736 break; 737 } 738 break; 739 } 740 if (new_level != level) { 741 vo_set_brightness(handle, new_level); 742 vo->vo_brightness = new_level; 743 } 744 745 out: 746 ACPI_SERIAL_END(video_output); 747 } 748 749 /* ARGSUSED */ 750 static int 751 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS) 752 { 753 struct acpi_video_output *vo; 754 int state, err; 755 756 vo = (struct acpi_video_output *)arg1; 757 if (vo->handle == NULL) 758 return (ENXIO); 759 ACPI_SERIAL_BEGIN(video_output); 760 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0; 761 err = sysctl_handle_int(oidp, &state, 0, req); 762 if (err != 0 || req->newptr == NULL) 763 goto out; 764 vo_set_device_state(vo->handle, 765 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE)); 766 out: 767 ACPI_SERIAL_END(video_output); 768 return (err); 769 } 770 771 /* ARGSUSED */ 772 static int 773 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS) 774 { 775 struct acpi_video_output *vo; 776 int level, preset, err; 777 778 vo = (struct acpi_video_output *)arg1; 779 ACPI_SERIAL_BEGIN(video_output); 780 if (vo->handle == NULL) { 781 err = ENXIO; 782 goto out; 783 } 784 if (vo->vo_levels == NULL) { 785 err = ENODEV; 786 goto out; 787 } 788 789 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ? 790 vo->vo_economy : vo->vo_fullpower; 791 level = vo->vo_brightness; 792 if (level == -1) 793 level = preset; 794 795 err = sysctl_handle_int(oidp, &level, 0, req); 796 if (err != 0 || req->newptr == NULL) 797 goto out; 798 if (level < -1 || level > 100) { 799 err = EINVAL; 800 goto out; 801 } 802 803 if (level != -1 && (err = acpi_video_vo_check_level(vo, level))) 804 goto out; 805 vo->vo_brightness = level; 806 vo_set_brightness(vo->handle, (level == -1) ? preset : level); 807 808 out: 809 ACPI_SERIAL_END(video_output); 810 return (err); 811 } 812 813 static int 814 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS) 815 { 816 struct acpi_video_output *vo; 817 int i, level, *preset, err; 818 819 vo = (struct acpi_video_output *)arg1; 820 ACPI_SERIAL_BEGIN(video_output); 821 if (vo->handle == NULL) { 822 err = ENXIO; 823 goto out; 824 } 825 if (vo->vo_levels == NULL) { 826 err = ENODEV; 827 goto out; 828 } 829 preset = (arg2 == POWER_PROFILE_ECONOMY) ? 830 &vo->vo_economy : &vo->vo_fullpower; 831 level = *preset; 832 err = sysctl_handle_int(oidp, &level, 0, req); 833 if (err != 0 || req->newptr == NULL) 834 goto out; 835 if (level < -1 || level > 100) { 836 err = EINVAL; 837 goto out; 838 } 839 if (level == -1) { 840 i = (arg2 == POWER_PROFILE_ECONOMY) ? 841 BCL_ECONOMY : BCL_FULLPOWER; 842 level = vo->vo_levels[i]; 843 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0) 844 goto out; 845 846 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2)) 847 vo_set_brightness(vo->handle, level); 848 *preset = level; 849 850 out: 851 ACPI_SERIAL_END(video_output); 852 return (err); 853 } 854 855 /* ARGSUSED */ 856 static int 857 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS) 858 { 859 struct acpi_video_output *vo; 860 int err; 861 862 vo = (struct acpi_video_output *)arg1; 863 ACPI_SERIAL_BEGIN(video_output); 864 if (vo->vo_levels == NULL) { 865 err = ENODEV; 866 goto out; 867 } 868 if (req->newptr != NULL) { 869 err = EPERM; 870 goto out; 871 } 872 err = sysctl_handle_opaque(oidp, vo->vo_levels, 873 vo->vo_numlevels * sizeof(*vo->vo_levels), req); 874 875 out: 876 ACPI_SERIAL_END(video_output); 877 return (err); 878 } 879 880 static void 881 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy) 882 { 883 ACPI_STATUS status; 884 885 status = acpi_SetInteger(handle, "_DOS", policy); 886 if (ACPI_FAILURE(status)) 887 printf("can't evaluate %s._DOS - %s\n", 888 acpi_name(handle), AcpiFormatException(status)); 889 } 890 891 struct enum_callback_arg { 892 void (*callback)(ACPI_HANDLE, UINT32, void *); 893 void *context; 894 ACPI_OBJECT *dod_pkg; 895 int count; 896 }; 897 898 static ACPI_STATUS 899 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused, 900 void *context, void **retp __unused) 901 { 902 ACPI_STATUS status; 903 UINT32 adr, val; 904 struct enum_callback_arg *argset; 905 size_t i; 906 907 ACPI_SERIAL_ASSERT(video); 908 argset = context; 909 status = acpi_GetInteger(handle, "_ADR", &adr); 910 if (ACPI_FAILURE(status)) 911 return (AE_OK); 912 913 for (i = 0; i < argset->dod_pkg->Package.Count; i++) { 914 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 && 915 (val & DOD_DEVID_MASK_FULL) == 916 (adr & DOD_DEVID_MASK_FULL)) { 917 argset->callback(handle, val, argset->context); 918 argset->count++; 919 } 920 } 921 922 return (AE_OK); 923 } 924 925 static int 926 vid_enum_outputs(ACPI_HANDLE handle, 927 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context) 928 { 929 ACPI_STATUS status; 930 ACPI_BUFFER dod_buf; 931 ACPI_OBJECT *res; 932 struct enum_callback_arg argset; 933 934 ACPI_SERIAL_ASSERT(video); 935 dod_buf.Length = ACPI_ALLOCATE_BUFFER; 936 dod_buf.Pointer = NULL; 937 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf); 938 if (ACPI_FAILURE(status)) { 939 if (status != AE_NOT_FOUND) 940 printf("can't evaluate %s._DOD - %s\n", 941 acpi_name(handle), AcpiFormatException(status)); 942 argset.count = -1; 943 goto out; 944 } 945 res = (ACPI_OBJECT *)dod_buf.Pointer; 946 if (!ACPI_PKG_VALID(res, 1)) { 947 printf("evaluation of %s._DOD makes no sense\n", 948 acpi_name(handle)); 949 argset.count = -1; 950 goto out; 951 } 952 if (callback == NULL) { 953 argset.count = res->Package.Count; 954 goto out; 955 } 956 argset.callback = callback; 957 argset.context = context; 958 argset.dod_pkg = res; 959 argset.count = 0; 960 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1, 961 vid_enum_outputs_subr, NULL, &argset, NULL); 962 if (ACPI_FAILURE(status)) 963 printf("failed walking down %s - %s\n", 964 acpi_name(handle), AcpiFormatException(status)); 965 out: 966 if (dod_buf.Pointer != NULL) 967 AcpiOsFree(dod_buf.Pointer); 968 return (argset.count); 969 } 970 971 static int 972 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp) 973 { 974 ACPI_STATUS status; 975 ACPI_BUFFER bcl_buf; 976 ACPI_OBJECT *res; 977 int num, i, n, *levels; 978 979 bcl_buf.Length = ACPI_ALLOCATE_BUFFER; 980 bcl_buf.Pointer = NULL; 981 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf); 982 if (ACPI_FAILURE(status)) { 983 if (status != AE_NOT_FOUND) 984 printf("can't evaluate %s._BCL - %s\n", 985 acpi_name(handle), AcpiFormatException(status)); 986 goto out; 987 } 988 res = (ACPI_OBJECT *)bcl_buf.Pointer; 989 if (!ACPI_PKG_VALID(res, 2)) { 990 printf("evaluation of %s._BCL makes no sense\n", 991 acpi_name(handle)); 992 goto out; 993 } 994 num = res->Package.Count; 995 if (num < 2 || levelp == NULL) 996 goto out; 997 levels = AcpiOsAllocate(num * sizeof(*levels)); 998 if (levels == NULL) 999 goto out; 1000 for (i = 0, n = 0; i < num; i++) 1001 if (acpi_PkgInt32(res, i, &levels[n]) == 0) 1002 n++; 1003 if (n < 2) { 1004 AcpiOsFree(levels); 1005 goto out; 1006 } 1007 *levelp = levels; 1008 return (n); 1009 1010 out: 1011 if (bcl_buf.Pointer != NULL) 1012 AcpiOsFree(bcl_buf.Pointer); 1013 return (0); 1014 } 1015 1016 static int 1017 vo_get_brightness(ACPI_HANDLE handle) 1018 { 1019 UINT32 level; 1020 ACPI_STATUS status; 1021 1022 ACPI_SERIAL_ASSERT(video_output); 1023 status = acpi_GetInteger(handle, "_BQC", &level); 1024 if (ACPI_FAILURE(status)) { 1025 printf("can't evaluate %s._BQC - %s\n", acpi_name(handle), 1026 AcpiFormatException(status)); 1027 return (-1); 1028 } 1029 if (level > 100) 1030 return (-1); 1031 1032 return (level); 1033 } 1034 1035 static void 1036 vo_set_brightness(ACPI_HANDLE handle, int level) 1037 { 1038 ACPI_STATUS status; 1039 1040 ACPI_SERIAL_ASSERT(video_output); 1041 status = acpi_SetInteger(handle, "_BCM", level); 1042 if (ACPI_FAILURE(status)) 1043 printf("can't evaluate %s._BCM - %s\n", 1044 acpi_name(handle), AcpiFormatException(status)); 1045 } 1046 1047 static UINT32 1048 vo_get_device_status(ACPI_HANDLE handle) 1049 { 1050 UINT32 dcs; 1051 ACPI_STATUS status; 1052 1053 ACPI_SERIAL_ASSERT(video_output); 1054 dcs = 0; 1055 status = acpi_GetInteger(handle, "_DCS", &dcs); 1056 if (ACPI_FAILURE(status)) 1057 printf("can't evaluate %s._DCS - %s\n", 1058 acpi_name(handle), AcpiFormatException(status)); 1059 1060 return (dcs); 1061 } 1062 1063 static UINT32 1064 vo_get_graphics_state(ACPI_HANDLE handle) 1065 { 1066 UINT32 dgs; 1067 ACPI_STATUS status; 1068 1069 dgs = 0; 1070 status = acpi_GetInteger(handle, "_DGS", &dgs); 1071 if (ACPI_FAILURE(status)) 1072 printf("can't evaluate %s._DGS - %s\n", 1073 acpi_name(handle), AcpiFormatException(status)); 1074 1075 return (dgs); 1076 } 1077 1078 static void 1079 vo_set_device_state(ACPI_HANDLE handle, UINT32 state) 1080 { 1081 ACPI_STATUS status; 1082 1083 ACPI_SERIAL_ASSERT(video_output); 1084 status = acpi_SetInteger(handle, "_DSS", state); 1085 if (ACPI_FAILURE(status)) 1086 printf("can't evaluate %s._DSS - %s\n", 1087 acpi_name(handle), AcpiFormatException(status)); 1088 } 1089