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