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