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_query_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_query_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_ACTIVITY (1 << 0) 135 #define DSS_SETNEXT (1 << 30) 136 #define DSS_COMMIT (1 << 31) 137 138 static device_method_t acpi_video_methods[] = { 139 DEVMETHOD(device_probe, acpi_video_probe), 140 DEVMETHOD(device_attach, acpi_video_attach), 141 DEVMETHOD(device_detach, acpi_video_detach), 142 DEVMETHOD(device_shutdown, acpi_video_shutdown), 143 { 0, 0 } 144 }; 145 146 static driver_t acpi_video_driver = { 147 "acpi_video", 148 acpi_video_methods, 149 sizeof(struct acpi_video_softc), 150 }; 151 152 static devclass_t acpi_video_devclass; 153 154 DRIVER_MODULE(acpi_video, pci, acpi_video_driver, acpi_video_devclass, 155 acpi_video_modevent, NULL); 156 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1); 157 158 static struct sysctl_ctx_list acpi_video_sysctl_ctx; 159 static struct sysctl_oid *acpi_video_sysctl_tree; 160 static struct acpi_video_output_queue lcd_units, crt_units, tv_units, 161 other_units; 162 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 = 0; 169 170 switch (evt) { 171 case MOD_LOAD: 172 acpi_video_sysctl_tree = NULL; 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_video_bind_outputs(sc); 236 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM); 237 238 acpi_video_power_profile(sc); 239 240 return (0); 241 } 242 243 static int 244 acpi_video_detach(device_t dev) 245 { 246 struct acpi_video_softc *sc; 247 struct acpi_video_output *vo, *vn; 248 249 sc = device_get_softc(dev); 250 251 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS); 252 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh); 253 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 254 acpi_video_notify_handler); 255 256 for (vo = STAILQ_FIRST(&sc->vid_outputs); vo != NULL; vo = vn) { 257 vn = STAILQ_NEXT(vo, vo_next); 258 acpi_video_vo_destroy(vo); 259 } 260 261 return (0); 262 } 263 264 static int 265 acpi_video_shutdown(device_t dev) 266 { 267 struct acpi_video_softc *sc; 268 269 sc = device_get_softc(dev); 270 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS); 271 272 return (0); 273 } 274 275 static void 276 acpi_video_notify_handler(ACPI_HANDLE handle __unused, UINT32 notify, 277 void *context) 278 { 279 struct acpi_video_softc *sc; 280 struct acpi_video_output *vo, *vo_tmp; 281 ACPI_HANDLE lasthand = NULL; 282 UINT32 dcs, dss, dss_p = 0; 283 284 sc = context; 285 286 switch (notify) { 287 case VID_NOTIFY_SWITCHED: 288 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 289 dss = vo_query_graphics_state(vo->handle); 290 dcs = vo_get_device_status(vo->handle); 291 if (!(dcs & DCS_READY)) 292 dss = DSS_INACTIVE; 293 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) || 294 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) { 295 if (lasthand != NULL) 296 vo_set_device_state(lasthand, dss_p); 297 dss_p = dss; 298 lasthand = vo->handle; 299 } 300 } 301 if (lasthand != NULL) 302 vo_set_device_state(lasthand, dss_p|DSS_COMMIT); 303 break; 304 case VID_NOTIFY_REPROBE: 305 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) 306 vo->handle = NULL; 307 acpi_video_bind_outputs(sc); 308 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) { 309 if (vo->handle == NULL) { 310 STAILQ_REMOVE(&sc->vid_outputs, vo, 311 acpi_video_output, vo_next); 312 acpi_video_vo_destroy(vo); 313 } 314 } 315 break; 316 default: 317 device_printf(sc->device, 318 "unknown notify event 0x%x\n", notify); 319 } 320 } 321 322 static void 323 acpi_video_power_profile(void *context) 324 { 325 int state; 326 struct acpi_video_softc *sc; 327 struct acpi_video_output *vo; 328 329 sc = context; 330 state = power_profile_get_state(); 331 if (state != POWER_PROFILE_PERFORMANCE && 332 state != POWER_PROFILE_ECONOMY) 333 return; 334 335 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 336 if (vo->vo_levels != NULL && vo->vo_brightness == -1) 337 vo_set_brightness(vo->handle, 338 state == POWER_PROFILE_ECONOMY 339 ? vo->vo_economy : vo->vo_fullpower); 340 } 341 } 342 343 static void 344 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context) 345 { 346 struct acpi_video_softc *sc; 347 struct acpi_video_output *vo; 348 349 sc = context; 350 351 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) { 352 if (vo->adr == adr) { 353 acpi_video_vo_bind(vo, handle); 354 return; 355 } 356 } 357 vo = acpi_video_vo_init(adr); 358 if (vo != NULL) { 359 acpi_video_vo_bind(vo, handle); 360 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next); 361 } 362 } 363 364 static void 365 acpi_video_bind_outputs(struct acpi_video_softc *sc) 366 { 367 368 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc); 369 } 370 371 static struct acpi_video_output * 372 acpi_video_vo_init(UINT32 adr) 373 { 374 struct acpi_video_output *vn, *vo, *vp; 375 int n, x; 376 char name[64], env[128]; 377 const char *type, *desc; 378 struct acpi_video_output_queue *voqh; 379 380 switch (adr & DOD_DEVID_MASK) { 381 case DOD_DEVID_MONITOR: 382 desc = "CRT monitor"; 383 type = "crt"; 384 voqh = &crt_units; 385 break; 386 case DOD_DEVID_PANEL: 387 desc = "LCD panel"; 388 type = "lcd"; 389 voqh = &lcd_units; 390 break; 391 case DOD_DEVID_TV: 392 desc = "TV"; 393 type = "tv"; 394 voqh = &tv_units; 395 break; 396 default: 397 desc = "unknown output"; 398 type = "out"; 399 voqh = &other_units; 400 } 401 402 n = 0; 403 vn = vp = NULL; 404 /* XXX - needs locking for protecting STAILQ xxx_units. */ 405 STAILQ_FOREACH(vn, voqh, vo_unit.next) { 406 if (vn->vo_unit.num != n) 407 break; 408 vp = vn; 409 n++; 410 } 411 412 snprintf(name, sizeof(name), "%s%d", type, n); 413 414 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT); 415 if (vo != NULL) { 416 vo->handle = NULL; 417 vo->adr = adr; 418 vo->vo_unit.num = n; 419 vo->vo_brightness = -1; 420 vo->vo_fullpower = -1; /* TODO: override with tunables */ 421 vo->vo_economy = -1; 422 vo->vo_numlevels = 0; 423 vo->vo_levels = NULL; 424 snprintf(env, 128, "hw.acpi.video.%s.fullpower", name); 425 if (getenv_int(env, &x)) 426 vo->vo_fullpower = x; 427 snprintf(env, 128, "hw.acpi.video.%s.economy", name); 428 if (getenv_int(env, &x)) 429 vo->vo_economy = x; 430 431 sysctl_ctx_init(&vo->vo_sysctl_ctx); 432 if (vp != NULL) 433 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next); 434 else 435 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next); 436 if (acpi_video_sysctl_tree != NULL) 437 vo->vo_sysctl_tree = 438 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx, 439 SYSCTL_CHILDREN(acpi_video_sysctl_tree), 440 OID_AUTO, name, CTLFLAG_RD, 0, desc); 441 if (vo->vo_sysctl_tree != NULL) { 442 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 443 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 444 OID_AUTO, "active", 445 CTLTYPE_INT|CTLFLAG_RW, vo, 0, 446 acpi_video_vo_active_sysctl, "I", 447 "current activity of this device"); 448 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 449 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 450 OID_AUTO, "brightness", 451 CTLTYPE_INT|CTLFLAG_RW, vo, 0, 452 acpi_video_vo_bright_sysctl, "I", 453 "current brightness level"); 454 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 455 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 456 OID_AUTO, "fullpower", 457 CTLTYPE_INT|CTLFLAG_RW, vo, 458 POWER_PROFILE_PERFORMANCE, 459 acpi_video_vo_presets_sysctl, "I", 460 "preset level for full power mode"); 461 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 462 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 463 OID_AUTO, "economy", 464 CTLTYPE_INT|CTLFLAG_RW, vo, 465 POWER_PROFILE_ECONOMY, 466 acpi_video_vo_presets_sysctl, "I", 467 "preset level for economy mode"); 468 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx, 469 SYSCTL_CHILDREN(vo->vo_sysctl_tree), 470 OID_AUTO, "levels", 471 CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0, 472 acpi_video_vo_levels_sysctl, "I", 473 "supported brightness levels"); 474 } else 475 printf("%s: sysctl node creation failed\n", type); 476 } else 477 printf("%s: softc allocation failed\n", type); 478 479 /* XXX unlock here - needs locking for protecting STAILQ xxx_units. */ 480 481 if (bootverbose) { 482 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK); 483 if (adr & DOD_BIOS) 484 printf(", detectable by BIOS"); 485 if (adr & DOD_NONVGA) 486 printf(" (not a VGA output)"); 487 printf(", head #%d\n", 488 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT); 489 } 490 return (vo); 491 } 492 493 static void 494 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle) 495 { 496 497 if (vo->vo_levels != NULL) 498 AcpiOsFree(vo->vo_levels); 499 vo->handle = handle; 500 vo->vo_numlevels = vo_query_brightness_levels(handle, &vo->vo_levels); 501 if (vo->vo_numlevels >= 2) { 502 if (vo->vo_fullpower == -1 503 || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) 504 /* XXX - can't deal with rebinding... */ 505 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER]; 506 if (vo->vo_economy == -1 507 || acpi_video_vo_check_level(vo, vo->vo_economy) != 0) 508 /* XXX - see above. */ 509 vo->vo_economy = vo->vo_levels[BCL_ECONOMY]; 510 } 511 } 512 513 static void 514 acpi_video_vo_destroy(struct acpi_video_output *vo) 515 { 516 struct acpi_video_output_queue *voqh; 517 518 519 if (vo->vo_sysctl_tree != NULL) { 520 vo->vo_sysctl_tree = NULL; 521 sysctl_ctx_free(&vo->vo_sysctl_ctx); 522 } 523 if (vo->vo_levels != NULL) 524 AcpiOsFree(vo->vo_levels); 525 526 switch (vo->adr & DOD_DEVID_MASK) { 527 case DOD_DEVID_MONITOR: 528 voqh = &crt_units; 529 break; 530 case DOD_DEVID_PANEL: 531 voqh = &lcd_units; 532 break; 533 case DOD_DEVID_TV: 534 voqh = &tv_units; 535 break; 536 default: 537 voqh = &other_units; 538 } 539 /* XXX - needs locking for protecting STAILQ xxx_units. */ 540 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next); 541 free(vo, M_ACPIVIDEO); 542 } 543 544 static int 545 acpi_video_vo_check_level(struct acpi_video_output *vo, int level) 546 { 547 int i; 548 549 if (vo->vo_levels == NULL) 550 return (ENODEV); 551 for (i = 0; i < vo->vo_numlevels; i++) 552 if (vo->vo_levels[i] == level) 553 return (0); 554 return (EINVAL); 555 } 556 557 /* ARGSUSED */ 558 static int 559 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS) 560 { 561 struct acpi_video_output *vo; 562 int state, err; 563 564 vo = (struct acpi_video_output *)arg1; 565 if (vo->handle == NULL) { 566 err = ENXIO; 567 goto out; 568 } 569 state = vo_get_device_status(vo->handle) & DCS_ACTIVE? 1 : 0; 570 err = sysctl_handle_int(oidp, &state, 0, req); 571 if (err != 0 || req->newptr == NULL) 572 goto out; 573 vo_set_device_state(vo->handle, 574 DSS_COMMIT | (state? DSS_ACTIVE : DSS_INACTIVE)); 575 out: 576 return (err); 577 } 578 579 /* ARGSUSED */ 580 static int 581 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS) 582 { 583 struct acpi_video_output *vo; 584 int level, preset, err; 585 586 vo = (struct acpi_video_output *)arg1; 587 if (vo->handle == NULL) { 588 err = ENXIO; 589 goto out; 590 } 591 if (vo->vo_levels == NULL) { 592 err = ENODEV; 593 goto out; 594 } 595 596 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ? 597 vo->vo_economy : vo->vo_fullpower; 598 level = vo->vo_brightness; 599 if (level == -1) 600 level = preset; 601 602 err = sysctl_handle_int(oidp, &level, 0, req); 603 if (err != 0 || req->newptr == NULL) 604 goto out; 605 if (level < -1 || level > 100) { 606 err = EINVAL; 607 goto out; 608 } 609 610 if (level != -1 && (err = acpi_video_vo_check_level(vo, level))) 611 goto out; 612 vo->vo_brightness = level; 613 vo_set_brightness(vo->handle, level == -1? preset : level); 614 out: 615 return (err); 616 } 617 618 static int 619 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS) 620 { 621 struct acpi_video_output *vo; 622 int level, *preset, err = 0; 623 624 vo = (struct acpi_video_output *)arg1; 625 if (vo->handle == NULL) { 626 err = ENXIO; 627 goto out; 628 } 629 if (vo->vo_levels == NULL) { 630 err = ENODEV; 631 goto out; 632 } 633 preset = (arg2 == POWER_PROFILE_ECONOMY) ? 634 &vo->vo_economy : &vo->vo_fullpower; 635 level = *preset; 636 err = sysctl_handle_int(oidp, &level, 0, req); 637 if (err != 0 || req->newptr == NULL) 638 goto out; 639 if (level < -1 || level > 100) { 640 err = EINVAL; 641 goto out; 642 } 643 if (level == -1) 644 level = vo->vo_levels 645 [(arg2 == POWER_PROFILE_ECONOMY) ? 646 BCL_ECONOMY : BCL_FULLPOWER]; 647 else if ((err = acpi_video_vo_check_level(vo, level)) != 0) 648 goto out; 649 650 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2)) 651 vo_set_brightness(vo->handle, level); 652 *preset = level; 653 out: 654 return (err); 655 } 656 657 /* ARGSUSED */ 658 static int 659 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS) 660 { 661 struct acpi_video_output *vo; 662 int err; 663 664 vo = (struct acpi_video_output *)arg1; 665 if (vo->vo_levels == NULL) { 666 err = ENODEV; 667 goto out; 668 } 669 if (req->newptr != NULL) { 670 err = EPERM; 671 goto out; 672 } 673 err = sysctl_handle_opaque(oidp, vo->vo_levels, 674 vo->vo_numlevels * sizeof *vo->vo_levels, req); 675 out: 676 return (err); 677 } 678 679 static void 680 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy) 681 { 682 ACPI_STATUS status; 683 684 status = acpi_SetInteger(handle, "_DOS", policy); 685 if (ACPI_FAILURE(status)) 686 printf("can't evaluate %s._DOS - %s\n", 687 acpi_name(handle), AcpiFormatException(status)); 688 } 689 690 struct enum_callback_arg { 691 void (*callback)(ACPI_HANDLE, UINT32, void *); 692 void *context; 693 ACPI_OBJECT *dod_pkg; 694 int count; 695 }; 696 697 static ACPI_STATUS 698 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused, 699 void *context, void **retp __unused) 700 { 701 ACPI_STATUS status; 702 UINT32 adr, val; 703 struct enum_callback_arg *argset; 704 size_t i; 705 706 argset = context; 707 status = acpi_GetInteger(handle, "_ADR", &adr); 708 if (ACPI_FAILURE(status)) 709 return (AE_OK); 710 711 for (i = 0; i < argset->dod_pkg->Package.Count; i++) { 712 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 && 713 (val & DOD_DEVID_MASK) == adr) { 714 argset->callback(handle, val, argset->context); 715 argset->count++; 716 } 717 } 718 719 return (AE_OK); 720 } 721 722 static int 723 vid_enum_outputs(ACPI_HANDLE handle, 724 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context) 725 { 726 ACPI_STATUS status; 727 ACPI_BUFFER dod_buf; 728 ACPI_OBJECT *res; 729 struct enum_callback_arg argset; 730 731 dod_buf.Length = ACPI_ALLOCATE_BUFFER; 732 dod_buf.Pointer = NULL; 733 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf); 734 if (ACPI_FAILURE(status)) { 735 if (status != AE_NOT_FOUND) 736 printf("can't evaluate %s._DOD - %s\n", 737 acpi_name(handle), AcpiFormatException(status)); 738 argset.count = -1; 739 goto out; 740 } 741 res = (ACPI_OBJECT *)dod_buf.Pointer; 742 if (!ACPI_PKG_VALID(res, 1)) { 743 printf("evaluation of %s._DOD makes no sense\n", 744 acpi_name(handle)); 745 argset.count = -1; 746 goto out; 747 } 748 if (callback == NULL) { 749 argset.count = res->Package.Count; 750 goto out; 751 } 752 argset.callback = callback; 753 argset.context = context; 754 argset.dod_pkg = res; 755 argset.count = 0; 756 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1, 757 vid_enum_outputs_subr, &argset, NULL); 758 if (ACPI_FAILURE(status)) 759 printf("failed walking down %s - %s\n", 760 acpi_name(handle), AcpiFormatException(status)); 761 out: 762 if (dod_buf.Pointer != NULL) 763 AcpiOsFree(dod_buf.Pointer); 764 return (argset.count); 765 } 766 767 static int 768 vo_query_brightness_levels(ACPI_HANDLE handle, int **levelp) 769 { 770 ACPI_STATUS status; 771 ACPI_BUFFER bcl_buf; 772 ACPI_OBJECT *res; 773 int num = 0, i, n, *levels; 774 775 bcl_buf.Length = ACPI_ALLOCATE_BUFFER; 776 bcl_buf.Pointer = NULL; 777 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf); 778 if (ACPI_FAILURE(status)) { 779 if (status != AE_NOT_FOUND) 780 printf("can't evaluate %s._BCL - %s\n", 781 acpi_name(handle), AcpiFormatException(status)); 782 num = -1; 783 goto out; 784 } 785 res = (ACPI_OBJECT *)bcl_buf.Pointer; 786 if (!ACPI_PKG_VALID(res, 2)) { 787 printf("evaluation of %s._BCL makes no sense\n", 788 acpi_name(handle)); 789 num = -1; 790 goto out; 791 } 792 num = res->Package.Count; 793 if (levelp == NULL) 794 goto out; 795 levels = AcpiOsAllocate(num * sizeof(*levels)); 796 if (levels == NULL) { 797 num = -1; 798 goto out; 799 } 800 for (i = 0, n = 0; i < num; i++) 801 if (acpi_PkgInt32(res, i, &levels[n]) == 0) 802 n++; 803 if (n < 2) { 804 num = -1; 805 AcpiOsFree(levels); 806 } else { 807 num = n; 808 *levelp = levels; 809 } 810 out: 811 if (bcl_buf.Pointer != NULL) 812 AcpiOsFree(bcl_buf.Pointer); 813 814 return (num); 815 } 816 817 static void 818 vo_set_brightness(ACPI_HANDLE handle, int level) 819 { 820 ACPI_STATUS status; 821 822 status = acpi_SetInteger(handle, "_BCM", level); 823 if (ACPI_FAILURE(status)) 824 printf("can't evaluate %s._BCM - %s\n", 825 acpi_name(handle), AcpiFormatException(status)); 826 } 827 828 static UINT32 829 vo_get_device_status(ACPI_HANDLE handle) 830 { 831 UINT32 dcs = 0; 832 ACPI_STATUS status; 833 834 status = acpi_GetInteger(handle, "_DCS", &dcs); 835 if (ACPI_FAILURE(status)) 836 printf("can't evaluate %s._DCS - %s\n", 837 acpi_name(handle), AcpiFormatException(status)); 838 839 return (dcs); 840 } 841 842 static UINT32 843 vo_query_graphics_state(ACPI_HANDLE handle) 844 { 845 UINT32 dgs = 0; 846 ACPI_STATUS status; 847 848 status = acpi_GetInteger(handle, "_DGS", &dgs); 849 if (ACPI_FAILURE(status)) 850 printf("can't evaluate %s._DGS - %s\n", 851 acpi_name(handle), AcpiFormatException(status)); 852 853 return (dgs); 854 } 855 856 static void 857 vo_set_device_state(ACPI_HANDLE handle, UINT32 state) 858 { 859 ACPI_STATUS status; 860 861 status = acpi_SetInteger(handle, "_DSS", state); 862 if (ACPI_FAILURE(status)) 863 printf("can't evaluate %s._DSS - %s\n", 864 acpi_name(handle), AcpiFormatException(status)); 865 } 866