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