2 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
37 #include <sys/power.h>
38 #include <sys/queue.h>
39 #include <sys/sysctl.h>
41 #include <contrib/dev/acpica/include/acpi.h>
43 #include <dev/acpica/acpivar.h>
45 /* ACPI video extension driver. */
46 struct acpi_video_output {
49 STAILQ_ENTRY(acpi_video_output) vo_next;
52 STAILQ_ENTRY(acpi_video_output) next;
59 struct sysctl_ctx_list vo_sysctl_ctx;
60 struct sysctl_oid *vo_sysctl_tree;
63 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
65 struct acpi_video_softc {
68 struct acpi_video_output_queue vid_outputs;
69 eventhandler_tag vid_pwr_evh;
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_resume(device_t);
79 static int acpi_video_shutdown(device_t);
80 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
81 static void acpi_video_power_profile(void *);
82 static void acpi_video_bind_outputs(struct acpi_video_softc *);
83 static struct acpi_video_output *acpi_video_vo_init(UINT32);
84 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
85 static void acpi_video_vo_destroy(struct acpi_video_output *);
86 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
87 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
88 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
89 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
90 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
91 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
94 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
95 static int vid_enum_outputs(ACPI_HANDLE,
96 void(*)(ACPI_HANDLE, UINT32, void *), void *);
97 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
98 static int vo_get_brightness(ACPI_HANDLE);
99 static void vo_set_brightness(ACPI_HANDLE, int);
100 static UINT32 vo_get_device_status(ACPI_HANDLE);
101 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
102 static void vo_set_device_state(ACPI_HANDLE, UINT32);
105 #define VID_NOTIFY_SWITCHED 0x80
106 #define VID_NOTIFY_REPROBE 0x81
107 #define VID_NOTIFY_CYCLE_BRN 0x85
108 #define VID_NOTIFY_INC_BRN 0x86
109 #define VID_NOTIFY_DEC_BRN 0x87
110 #define VID_NOTIFY_ZERO_BRN 0x88
112 /* _DOS (Enable/Disable Output Switching) argument bits */
113 #define DOS_SWITCH_MASK 3
114 #define DOS_SWITCH_BY_OSPM 0
115 #define DOS_SWITCH_BY_BIOS 1
116 #define DOS_SWITCH_LOCKED 2
117 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
119 /* _DOD and subdev's _ADR */
120 #define DOD_DEVID_MASK 0x0f00
121 #define DOD_DEVID_MASK_FULL 0xffff
122 #define DOD_DEVID_MASK_DISPIDX 0x000f
123 #define DOD_DEVID_MASK_DISPPORT 0x00f0
124 #define DOD_DEVID_MONITOR 0x0100
125 #define DOD_DEVID_LCD 0x0110
126 #define DOD_DEVID_TV 0x0200
127 #define DOD_DEVID_EXT 0x0300
128 #define DOD_DEVID_INTDFP 0x0400
129 #define DOD_BIOS (1 << 16)
130 #define DOD_NONVGA (1 << 17)
131 #define DOD_HEAD_ID_SHIFT 18
132 #define DOD_HEAD_ID_BITS 3
133 #define DOD_HEAD_ID_MASK \
134 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
135 #define DOD_DEVID_SCHEME_STD (1U << 31)
137 /* _BCL related constants */
138 #define BCL_FULLPOWER 0
139 #define BCL_ECONOMY 1
141 /* _DCS (Device Currrent Status) value bits and masks. */
142 #define DCS_EXISTS (1 << 0)
143 #define DCS_ACTIVE (1 << 1)
144 #define DCS_READY (1 << 2)
145 #define DCS_FUNCTIONAL (1 << 3)
146 #define DCS_ATTACHED (1 << 4)
148 /* _DSS (Device Set Status) argument bits and masks. */
149 #define DSS_INACTIVE 0
150 #define DSS_ACTIVE (1 << 0)
151 #define DSS_SETNEXT (1 << 30)
152 #define DSS_COMMIT (1U << 31)
154 static device_method_t acpi_video_methods[] = {
155 DEVMETHOD(device_identify, acpi_video_identify),
156 DEVMETHOD(device_probe, acpi_video_probe),
157 DEVMETHOD(device_attach, acpi_video_attach),
158 DEVMETHOD(device_detach, acpi_video_detach),
159 DEVMETHOD(device_resume, acpi_video_resume),
160 DEVMETHOD(device_shutdown, acpi_video_shutdown),
164 static driver_t acpi_video_driver = {
167 sizeof(struct acpi_video_softc),
170 static devclass_t acpi_video_devclass;
172 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
173 acpi_video_modevent, NULL);
174 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
176 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
177 static struct sysctl_oid *acpi_video_sysctl_tree;
178 static struct acpi_video_output_queue crt_units, tv_units,
179 ext_units, lcd_units, other_units;
182 * The 'video' lock protects the hierarchy of video output devices
183 * (the video "bus"). The 'video_output' lock protects per-output
184 * data is equivalent to a softc lock for each video output.
186 ACPI_SERIAL_DECL(video, "ACPI video");
187 ACPI_SERIAL_DECL(video_output, "ACPI video output");
188 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
191 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
198 sysctl_ctx_init(&acpi_video_sysctl_ctx);
199 STAILQ_INIT(&crt_units);
200 STAILQ_INIT(&tv_units);
201 STAILQ_INIT(&ext_units);
202 STAILQ_INIT(&lcd_units);
203 STAILQ_INIT(&other_units);
206 sysctl_ctx_free(&acpi_video_sysctl_ctx);
207 acpi_video_sysctl_tree = NULL;
217 acpi_video_identify(driver_t *driver, device_t parent)
220 if (device_find_child(parent, "acpi_video", -1) == NULL)
221 device_add_child(parent, "acpi_video", -1);
225 acpi_video_probe(device_t dev)
228 ACPI_OBJECT_TYPE t_dos;
230 devh = acpi_get_handle(dev);
231 if (acpi_disabled("video") ||
232 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
233 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
234 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
235 t_dos != ACPI_TYPE_METHOD)
238 device_set_desc(dev, "ACPI video extension");
243 acpi_video_attach(device_t dev)
245 struct acpi_softc *acpi_sc;
246 struct acpi_video_softc *sc;
248 sc = device_get_softc(dev);
250 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
253 ACPI_SERIAL_BEGIN(video);
254 if (acpi_video_sysctl_tree == NULL) {
255 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
256 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
257 OID_AUTO, "video", CTLFLAG_RD, 0,
258 "video extension control");
260 ACPI_SERIAL_END(video);
263 sc->handle = acpi_get_handle(dev);
264 STAILQ_INIT(&sc->vid_outputs);
266 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
267 acpi_video_notify_handler, sc);
268 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
269 acpi_video_power_profile, sc, 0);
271 ACPI_SERIAL_BEGIN(video);
272 acpi_video_bind_outputs(sc);
273 ACPI_SERIAL_END(video);
276 * Notify the BIOS that we want to switch both active outputs and
279 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
280 DOS_BRIGHTNESS_BY_OSPM);
282 acpi_video_power_profile(sc);
288 acpi_video_detach(device_t dev)
290 struct acpi_video_softc *sc;
291 struct acpi_video_output *vo, *vn;
293 sc = device_get_softc(dev);
295 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
296 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
297 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
298 acpi_video_notify_handler);
300 ACPI_SERIAL_BEGIN(video);
301 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
302 acpi_video_vo_destroy(vo);
304 ACPI_SERIAL_END(video);
310 acpi_video_resume(device_t dev)
312 struct acpi_video_softc *sc;
313 struct acpi_video_output *vo, *vn;
316 sc = device_get_softc(dev);
318 /* Restore brightness level */
319 ACPI_SERIAL_BEGIN(video);
320 ACPI_SERIAL_BEGIN(video_output);
321 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
322 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
323 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
326 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
329 level = vo_get_brightness(vo->handle);
331 vo_set_brightness(vo->handle, level);
333 ACPI_SERIAL_END(video_output);
334 ACPI_SERIAL_END(video);
340 acpi_video_shutdown(device_t dev)
342 struct acpi_video_softc *sc;
344 sc = device_get_softc(dev);
345 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
351 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
353 struct acpi_video_softc *sc;
354 struct acpi_video_output *vo, *vo_tmp;
355 ACPI_HANDLE lasthand;
356 UINT32 dcs, dss, dss_p;
358 sc = (struct acpi_video_softc *)context;
361 case VID_NOTIFY_SWITCHED:
364 ACPI_SERIAL_BEGIN(video);
365 ACPI_SERIAL_BEGIN(video_output);
366 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
367 dss = vo_get_graphics_state(vo->handle);
368 dcs = vo_get_device_status(vo->handle);
369 if (!(dcs & DCS_READY))
371 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
372 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
373 if (lasthand != NULL)
374 vo_set_device_state(lasthand, dss_p);
376 lasthand = vo->handle;
379 if (lasthand != NULL)
380 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
381 ACPI_SERIAL_END(video_output);
382 ACPI_SERIAL_END(video);
384 case VID_NOTIFY_REPROBE:
385 ACPI_SERIAL_BEGIN(video);
386 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
388 acpi_video_bind_outputs(sc);
389 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
390 if (vo->handle == NULL) {
391 STAILQ_REMOVE(&sc->vid_outputs, vo,
392 acpi_video_output, vo_next);
393 acpi_video_vo_destroy(vo);
396 ACPI_SERIAL_END(video);
399 device_printf(sc->device, "unknown notify event 0x%x\n",
405 acpi_video_power_profile(void *context)
408 struct acpi_video_softc *sc;
409 struct acpi_video_output *vo;
412 state = power_profile_get_state();
413 if (state != POWER_PROFILE_PERFORMANCE &&
414 state != POWER_PROFILE_ECONOMY)
417 ACPI_SERIAL_BEGIN(video);
418 ACPI_SERIAL_BEGIN(video_output);
419 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
420 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
421 vo_set_brightness(vo->handle,
422 state == POWER_PROFILE_ECONOMY ?
423 vo->vo_economy : vo->vo_fullpower);
425 ACPI_SERIAL_END(video_output);
426 ACPI_SERIAL_END(video);
430 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
432 struct acpi_video_softc *sc;
433 struct acpi_video_output *vo;
435 ACPI_SERIAL_ASSERT(video);
438 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
439 if (vo->adr == adr) {
440 acpi_video_vo_bind(vo, handle);
444 vo = acpi_video_vo_init(adr);
446 acpi_video_vo_bind(vo, handle);
447 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
452 acpi_video_bind_outputs(struct acpi_video_softc *sc)
455 ACPI_SERIAL_ASSERT(video);
456 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
459 static struct acpi_video_output *
460 acpi_video_vo_init(UINT32 adr)
462 struct acpi_video_output *vn, *vo, *vp;
464 char name[8], env[32];
465 const char *type, *desc;
466 struct acpi_video_output_queue *voqh;
468 ACPI_SERIAL_ASSERT(video);
470 switch (adr & DOD_DEVID_MASK) {
471 case DOD_DEVID_MONITOR:
472 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
473 /* DOD_DEVID_LCD is a common, backward compatible ID */
474 desc = "Internal/Integrated Digital Flat Panel";
478 desc = "VGA CRT or VESA Compatible Analog Monitor";
484 desc = "TV/HDTV or Analog-Video Monitor";
489 desc = "External Digital Monitor";
493 case DOD_DEVID_INTDFP:
494 desc = "Internal/Integrated Digital Flat Panel";
499 desc = "unknown output";
506 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
507 if (vn->vo_unit.num != n)
513 snprintf(name, sizeof(name), "%s%d", type, n);
515 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
520 vo->vo_brightness = -1;
521 vo->vo_fullpower = -1; /* TODO: override with tunables */
523 vo->vo_numlevels = 0;
524 vo->vo_levels = NULL;
525 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
526 if (getenv_int(env, &x))
527 vo->vo_fullpower = x;
528 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
529 if (getenv_int(env, &x))
532 sysctl_ctx_init(&vo->vo_sysctl_ctx);
534 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
536 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
537 if (acpi_video_sysctl_tree != NULL)
539 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
540 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
541 OID_AUTO, name, CTLFLAG_RD, 0, desc);
542 if (vo->vo_sysctl_tree != NULL) {
543 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
544 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
546 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
547 acpi_video_vo_active_sysctl, "I",
548 "current activity of this device");
549 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
550 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
551 OID_AUTO, "brightness",
552 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
553 acpi_video_vo_bright_sysctl, "I",
554 "current brightness level");
555 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
556 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
557 OID_AUTO, "fullpower",
558 CTLTYPE_INT|CTLFLAG_RW, vo,
559 POWER_PROFILE_PERFORMANCE,
560 acpi_video_vo_presets_sysctl, "I",
561 "preset level for full power mode");
562 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
563 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
565 CTLTYPE_INT|CTLFLAG_RW, vo,
566 POWER_PROFILE_ECONOMY,
567 acpi_video_vo_presets_sysctl, "I",
568 "preset level for economy mode");
569 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
570 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
572 CTLTYPE_INT | CTLFLAG_RD, vo, 0,
573 acpi_video_vo_levels_sysctl, "I",
574 "supported brightness levels");
576 printf("%s: sysctl node creation failed\n", type);
578 printf("%s: softc allocation failed\n", type);
581 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
582 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
583 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
585 printf(", detectable by BIOS");
586 if (adr & DOD_NONVGA)
587 printf(" (Non-VGA output device whose power "
588 "is related to the VGA device)");
589 printf(", head #%d\n",
590 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
596 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
599 ACPI_SERIAL_BEGIN(video_output);
600 if (vo->vo_levels != NULL)
601 AcpiOsFree(vo->vo_levels);
603 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
604 if (vo->vo_numlevels >= 2) {
605 if (vo->vo_fullpower == -1
606 || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
607 /* XXX - can't deal with rebinding... */
608 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
609 if (vo->vo_economy == -1
610 || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
611 /* XXX - see above. */
612 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
614 if (vo->vo_levels != NULL)
615 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
616 acpi_video_vo_notify_handler, vo);
617 ACPI_SERIAL_END(video_output);
621 acpi_video_vo_destroy(struct acpi_video_output *vo)
623 struct acpi_video_output_queue *voqh;
625 ACPI_SERIAL_ASSERT(video);
626 if (vo->vo_sysctl_tree != NULL) {
627 vo->vo_sysctl_tree = NULL;
628 sysctl_ctx_free(&vo->vo_sysctl_ctx);
630 if (vo->vo_levels != NULL) {
631 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
632 acpi_video_vo_notify_handler);
633 AcpiOsFree(vo->vo_levels);
636 switch (vo->adr & DOD_DEVID_MASK) {
637 case DOD_DEVID_MONITOR:
646 case DOD_DEVID_INTDFP:
652 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
653 free(vo, M_ACPIVIDEO);
657 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
661 ACPI_SERIAL_ASSERT(video_output);
662 if (vo->vo_levels == NULL)
664 for (i = 0; i < vo->vo_numlevels; i++)
665 if (vo->vo_levels[i] == level)
671 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
673 struct acpi_video_output *vo;
674 int i, j, level, new_level;
677 ACPI_SERIAL_BEGIN(video_output);
678 if (vo->handle != handle)
682 case VID_NOTIFY_CYCLE_BRN:
683 if (vo->vo_numlevels <= 3)
686 case VID_NOTIFY_INC_BRN:
687 case VID_NOTIFY_DEC_BRN:
688 case VID_NOTIFY_ZERO_BRN:
689 if (vo->vo_levels == NULL)
691 level = vo_get_brightness(handle);
696 printf("unknown notify event 0x%x from %s\n",
697 notify, acpi_name(handle));
703 case VID_NOTIFY_CYCLE_BRN:
704 for (i = 2; i < vo->vo_numlevels; i++)
705 if (vo->vo_levels[i] == level) {
706 new_level = vo->vo_numlevels > i + 1 ?
707 vo->vo_levels[i + 1] : vo->vo_levels[2];
711 case VID_NOTIFY_INC_BRN:
712 case VID_NOTIFY_DEC_BRN:
713 for (i = 0; i < vo->vo_numlevels; i++) {
714 j = vo->vo_levels[i];
715 if (notify == VID_NOTIFY_INC_BRN) {
717 (j < new_level || level == new_level))
721 (j > new_level || level == new_level))
726 case VID_NOTIFY_ZERO_BRN:
727 for (i = 0; i < vo->vo_numlevels; i++)
728 if (vo->vo_levels[i] == 0) {
734 if (new_level != level) {
735 vo_set_brightness(handle, new_level);
736 vo->vo_brightness = new_level;
740 ACPI_SERIAL_END(video_output);
745 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
747 struct acpi_video_output *vo;
750 vo = (struct acpi_video_output *)arg1;
751 if (vo->handle == NULL)
753 ACPI_SERIAL_BEGIN(video_output);
754 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
755 err = sysctl_handle_int(oidp, &state, 0, req);
756 if (err != 0 || req->newptr == NULL)
758 vo_set_device_state(vo->handle,
759 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
761 ACPI_SERIAL_END(video_output);
767 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
769 struct acpi_video_output *vo;
770 int level, preset, err;
772 vo = (struct acpi_video_output *)arg1;
773 ACPI_SERIAL_BEGIN(video_output);
774 if (vo->handle == NULL) {
778 if (vo->vo_levels == NULL) {
783 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
784 vo->vo_economy : vo->vo_fullpower;
785 level = vo->vo_brightness;
789 err = sysctl_handle_int(oidp, &level, 0, req);
790 if (err != 0 || req->newptr == NULL)
792 if (level < -1 || level > 100) {
797 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
799 vo->vo_brightness = level;
800 vo_set_brightness(vo->handle, (level == -1) ? preset : level);
803 ACPI_SERIAL_END(video_output);
808 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
810 struct acpi_video_output *vo;
811 int i, level, *preset, err;
813 vo = (struct acpi_video_output *)arg1;
814 ACPI_SERIAL_BEGIN(video_output);
815 if (vo->handle == NULL) {
819 if (vo->vo_levels == NULL) {
823 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
824 &vo->vo_economy : &vo->vo_fullpower;
826 err = sysctl_handle_int(oidp, &level, 0, req);
827 if (err != 0 || req->newptr == NULL)
829 if (level < -1 || level > 100) {
834 i = (arg2 == POWER_PROFILE_ECONOMY) ?
835 BCL_ECONOMY : BCL_FULLPOWER;
836 level = vo->vo_levels[i];
837 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
840 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
841 vo_set_brightness(vo->handle, level);
845 ACPI_SERIAL_END(video_output);
851 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
853 struct acpi_video_output *vo;
856 vo = (struct acpi_video_output *)arg1;
857 ACPI_SERIAL_BEGIN(video_output);
858 if (vo->vo_levels == NULL) {
862 if (req->newptr != NULL) {
866 err = sysctl_handle_opaque(oidp, vo->vo_levels,
867 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
870 ACPI_SERIAL_END(video_output);
875 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
879 status = acpi_SetInteger(handle, "_DOS", policy);
880 if (ACPI_FAILURE(status))
881 printf("can't evaluate %s._DOS - %s\n",
882 acpi_name(handle), AcpiFormatException(status));
885 struct enum_callback_arg {
886 void (*callback)(ACPI_HANDLE, UINT32, void *);
888 ACPI_OBJECT *dod_pkg;
893 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
894 void *context, void **retp __unused)
898 struct enum_callback_arg *argset;
901 ACPI_SERIAL_ASSERT(video);
903 status = acpi_GetInteger(handle, "_ADR", &adr);
904 if (ACPI_FAILURE(status))
907 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
908 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
909 (val & DOD_DEVID_MASK_FULL) ==
910 (adr & DOD_DEVID_MASK_FULL)) {
911 argset->callback(handle, val, argset->context);
920 vid_enum_outputs(ACPI_HANDLE handle,
921 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
926 struct enum_callback_arg argset;
928 ACPI_SERIAL_ASSERT(video);
929 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
930 dod_buf.Pointer = NULL;
931 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
932 if (ACPI_FAILURE(status)) {
933 if (status != AE_NOT_FOUND)
934 printf("can't evaluate %s._DOD - %s\n",
935 acpi_name(handle), AcpiFormatException(status));
939 res = (ACPI_OBJECT *)dod_buf.Pointer;
940 if (!ACPI_PKG_VALID(res, 1)) {
941 printf("evaluation of %s._DOD makes no sense\n",
946 if (callback == NULL) {
947 argset.count = res->Package.Count;
950 argset.callback = callback;
951 argset.context = context;
952 argset.dod_pkg = res;
954 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
955 vid_enum_outputs_subr, NULL, &argset, NULL);
956 if (ACPI_FAILURE(status))
957 printf("failed walking down %s - %s\n",
958 acpi_name(handle), AcpiFormatException(status));
960 if (dod_buf.Pointer != NULL)
961 AcpiOsFree(dod_buf.Pointer);
962 return (argset.count);
966 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
971 int num, i, n, *levels;
973 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
974 bcl_buf.Pointer = NULL;
975 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
976 if (ACPI_FAILURE(status)) {
977 if (status != AE_NOT_FOUND)
978 printf("can't evaluate %s._BCL - %s\n",
979 acpi_name(handle), AcpiFormatException(status));
982 res = (ACPI_OBJECT *)bcl_buf.Pointer;
983 if (!ACPI_PKG_VALID(res, 2)) {
984 printf("evaluation of %s._BCL makes no sense\n",
988 num = res->Package.Count;
989 if (num < 2 || levelp == NULL)
991 levels = AcpiOsAllocate(num * sizeof(*levels));
994 for (i = 0, n = 0; i < num; i++)
995 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1005 if (bcl_buf.Pointer != NULL)
1006 AcpiOsFree(bcl_buf.Pointer);
1011 vo_get_brightness(ACPI_HANDLE handle)
1016 ACPI_SERIAL_ASSERT(video_output);
1017 status = acpi_GetInteger(handle, "_BQC", &level);
1018 if (ACPI_FAILURE(status)) {
1019 printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
1020 AcpiFormatException(status));
1030 vo_set_brightness(ACPI_HANDLE handle, int level)
1034 ACPI_SERIAL_ASSERT(video_output);
1035 status = acpi_SetInteger(handle, "_BCM", level);
1036 if (ACPI_FAILURE(status))
1037 printf("can't evaluate %s._BCM - %s\n",
1038 acpi_name(handle), AcpiFormatException(status));
1042 vo_get_device_status(ACPI_HANDLE handle)
1047 ACPI_SERIAL_ASSERT(video_output);
1049 status = acpi_GetInteger(handle, "_DCS", &dcs);
1050 if (ACPI_FAILURE(status))
1051 printf("can't evaluate %s._DCS - %s\n",
1052 acpi_name(handle), AcpiFormatException(status));
1058 vo_get_graphics_state(ACPI_HANDLE handle)
1064 status = acpi_GetInteger(handle, "_DGS", &dgs);
1065 if (ACPI_FAILURE(status))
1066 printf("can't evaluate %s._DGS - %s\n",
1067 acpi_name(handle), AcpiFormatException(status));
1073 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1077 ACPI_SERIAL_ASSERT(video_output);
1078 status = acpi_SetInteger(handle, "_DSS", state);
1079 if (ACPI_FAILURE(status))
1080 printf("can't evaluate %s._DSS - %s\n",
1081 acpi_name(handle), AcpiFormatException(status));