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>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/power.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
42 #include <contrib/dev/acpica/include/acpi.h>
44 #include <dev/acpica/acpivar.h>
46 /* ACPI video extension driver. */
47 struct acpi_video_output {
50 STAILQ_ENTRY(acpi_video_output) vo_next;
53 STAILQ_ENTRY(acpi_video_output) next;
55 int vo_hasbqc; /* Query method is present. */
56 int vo_level; /* Cached level when !vo_hasbqc. */
62 struct sysctl_ctx_list vo_sysctl_ctx;
63 struct sysctl_oid *vo_sysctl_tree;
66 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
68 struct acpi_video_softc {
71 struct acpi_video_output_queue vid_outputs;
72 eventhandler_tag vid_pwr_evh;
76 static int acpi_video_modevent(struct module*, int, void *);
77 static void acpi_video_identify(driver_t *driver, device_t parent);
78 static int acpi_video_probe(device_t);
79 static int acpi_video_attach(device_t);
80 static int acpi_video_detach(device_t);
81 static int acpi_video_resume(device_t);
82 static int acpi_video_shutdown(device_t);
83 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
84 static void acpi_video_power_profile(void *);
85 static void acpi_video_bind_outputs(struct acpi_video_softc *);
86 static struct acpi_video_output *acpi_video_vo_init(UINT32);
87 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
88 static void acpi_video_vo_destroy(struct acpi_video_output *);
89 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
90 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
91 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
92 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
93 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
94 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
97 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
98 static int vid_enum_outputs(ACPI_HANDLE,
99 void(*)(ACPI_HANDLE, UINT32, void *), void *);
100 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
101 static int vo_get_brightness(struct acpi_video_output *);
102 static void vo_set_brightness(struct acpi_video_output *, int);
103 static UINT32 vo_get_device_status(ACPI_HANDLE);
104 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
105 static void vo_set_device_state(ACPI_HANDLE, UINT32);
108 #define VID_NOTIFY_SWITCHED 0x80
109 #define VID_NOTIFY_REPROBE 0x81
110 #define VID_NOTIFY_CYCLE_BRN 0x85
111 #define VID_NOTIFY_INC_BRN 0x86
112 #define VID_NOTIFY_DEC_BRN 0x87
113 #define VID_NOTIFY_ZERO_BRN 0x88
115 /* _DOS (Enable/Disable Output Switching) argument bits */
116 #define DOS_SWITCH_MASK 3
117 #define DOS_SWITCH_BY_OSPM 0
118 #define DOS_SWITCH_BY_BIOS 1
119 #define DOS_SWITCH_LOCKED 2
120 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
122 /* _DOD and subdev's _ADR */
123 #define DOD_DEVID_MASK 0x0f00
124 #define DOD_DEVID_MASK_FULL 0xffff
125 #define DOD_DEVID_MASK_DISPIDX 0x000f
126 #define DOD_DEVID_MASK_DISPPORT 0x00f0
127 #define DOD_DEVID_MONITOR 0x0100
128 #define DOD_DEVID_LCD 0x0110
129 #define DOD_DEVID_TV 0x0200
130 #define DOD_DEVID_EXT 0x0300
131 #define DOD_DEVID_INTDFP 0x0400
132 #define DOD_BIOS (1 << 16)
133 #define DOD_NONVGA (1 << 17)
134 #define DOD_HEAD_ID_SHIFT 18
135 #define DOD_HEAD_ID_BITS 3
136 #define DOD_HEAD_ID_MASK \
137 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
138 #define DOD_DEVID_SCHEME_STD (1U << 31)
140 /* _BCL related constants */
141 #define BCL_FULLPOWER 0
142 #define BCL_ECONOMY 1
144 /* _DCS (Device Currrent Status) value bits and masks. */
145 #define DCS_EXISTS (1 << 0)
146 #define DCS_ACTIVE (1 << 1)
147 #define DCS_READY (1 << 2)
148 #define DCS_FUNCTIONAL (1 << 3)
149 #define DCS_ATTACHED (1 << 4)
151 /* _DSS (Device Set Status) argument bits and masks. */
152 #define DSS_INACTIVE 0
153 #define DSS_ACTIVE (1 << 0)
154 #define DSS_SETNEXT (1 << 30)
155 #define DSS_COMMIT (1U << 31)
157 static device_method_t acpi_video_methods[] = {
158 DEVMETHOD(device_identify, acpi_video_identify),
159 DEVMETHOD(device_probe, acpi_video_probe),
160 DEVMETHOD(device_attach, acpi_video_attach),
161 DEVMETHOD(device_detach, acpi_video_detach),
162 DEVMETHOD(device_resume, acpi_video_resume),
163 DEVMETHOD(device_shutdown, acpi_video_shutdown),
167 static driver_t acpi_video_driver = {
170 sizeof(struct acpi_video_softc),
173 static devclass_t acpi_video_devclass;
175 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
176 acpi_video_modevent, NULL);
177 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
179 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
180 static struct sysctl_oid *acpi_video_sysctl_tree;
181 static struct acpi_video_output_queue crt_units, tv_units,
182 ext_units, lcd_units, other_units;
185 * The 'video' lock protects the hierarchy of video output devices
186 * (the video "bus"). The 'video_output' lock protects per-output
187 * data is equivalent to a softc lock for each video output.
189 ACPI_SERIAL_DECL(video, "ACPI video");
190 ACPI_SERIAL_DECL(video_output, "ACPI video output");
191 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
194 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
201 sysctl_ctx_init(&acpi_video_sysctl_ctx);
202 STAILQ_INIT(&crt_units);
203 STAILQ_INIT(&tv_units);
204 STAILQ_INIT(&ext_units);
205 STAILQ_INIT(&lcd_units);
206 STAILQ_INIT(&other_units);
209 sysctl_ctx_free(&acpi_video_sysctl_ctx);
210 acpi_video_sysctl_tree = NULL;
220 acpi_video_identify(driver_t *driver, device_t parent)
223 if (device_find_child(parent, "acpi_video", -1) == NULL)
224 device_add_child(parent, "acpi_video", -1);
228 acpi_video_probe(device_t dev)
231 ACPI_OBJECT_TYPE t_dos;
233 devh = acpi_get_handle(dev);
234 if (acpi_disabled("video") ||
235 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
236 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
237 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
238 t_dos != ACPI_TYPE_METHOD)
241 device_set_desc(dev, "ACPI video extension");
246 acpi_video_attach(device_t dev)
248 struct acpi_softc *acpi_sc;
249 struct acpi_video_softc *sc;
251 sc = device_get_softc(dev);
253 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
256 ACPI_SERIAL_BEGIN(video);
257 if (acpi_video_sysctl_tree == NULL) {
258 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
259 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
260 "video", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
261 "video extension control");
263 ACPI_SERIAL_END(video);
266 sc->handle = acpi_get_handle(dev);
267 STAILQ_INIT(&sc->vid_outputs);
269 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
270 acpi_video_notify_handler, sc);
271 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
272 acpi_video_power_profile, sc, 0);
274 ACPI_SERIAL_BEGIN(video);
275 acpi_video_bind_outputs(sc);
276 ACPI_SERIAL_END(video);
279 * Notify the BIOS that we want to switch both active outputs and
282 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
283 DOS_BRIGHTNESS_BY_OSPM);
285 acpi_video_power_profile(sc);
291 acpi_video_detach(device_t dev)
293 struct acpi_video_softc *sc;
294 struct acpi_video_output *vo, *vn;
296 sc = device_get_softc(dev);
298 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
299 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
300 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
301 acpi_video_notify_handler);
303 ACPI_SERIAL_BEGIN(video);
304 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
305 acpi_video_vo_destroy(vo);
307 ACPI_SERIAL_END(video);
313 acpi_video_resume(device_t dev)
315 struct acpi_video_softc *sc;
316 struct acpi_video_output *vo, *vn;
319 sc = device_get_softc(dev);
321 /* Restore brightness level */
322 ACPI_SERIAL_BEGIN(video);
323 ACPI_SERIAL_BEGIN(video_output);
324 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
325 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
326 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
329 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
332 level = vo_get_brightness(vo);
334 vo_set_brightness(vo, level);
336 ACPI_SERIAL_END(video_output);
337 ACPI_SERIAL_END(video);
343 acpi_video_shutdown(device_t dev)
345 struct acpi_video_softc *sc;
347 sc = device_get_softc(dev);
348 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
354 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
356 struct acpi_video_softc *sc;
357 struct acpi_video_output *vo, *vo_tmp;
358 ACPI_HANDLE lasthand;
359 UINT32 dcs, dss, dss_p;
361 sc = (struct acpi_video_softc *)context;
364 case VID_NOTIFY_SWITCHED:
367 ACPI_SERIAL_BEGIN(video);
368 ACPI_SERIAL_BEGIN(video_output);
369 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
370 dss = vo_get_graphics_state(vo->handle);
371 dcs = vo_get_device_status(vo->handle);
372 if (!(dcs & DCS_READY))
374 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
375 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
376 if (lasthand != NULL)
377 vo_set_device_state(lasthand, dss_p);
379 lasthand = vo->handle;
382 if (lasthand != NULL)
383 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
384 ACPI_SERIAL_END(video_output);
385 ACPI_SERIAL_END(video);
387 case VID_NOTIFY_REPROBE:
388 ACPI_SERIAL_BEGIN(video);
389 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
391 acpi_video_bind_outputs(sc);
392 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
393 if (vo->handle == NULL) {
394 STAILQ_REMOVE(&sc->vid_outputs, vo,
395 acpi_video_output, vo_next);
396 acpi_video_vo_destroy(vo);
399 ACPI_SERIAL_END(video);
402 device_printf(sc->device, "unknown notify event 0x%x\n",
408 acpi_video_power_profile(void *context)
411 struct acpi_video_softc *sc;
412 struct acpi_video_output *vo;
415 state = power_profile_get_state();
416 if (state != POWER_PROFILE_PERFORMANCE &&
417 state != POWER_PROFILE_ECONOMY)
420 ACPI_SERIAL_BEGIN(video);
421 ACPI_SERIAL_BEGIN(video_output);
422 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
423 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
424 vo_set_brightness(vo,
425 state == POWER_PROFILE_ECONOMY ?
426 vo->vo_economy : vo->vo_fullpower);
428 ACPI_SERIAL_END(video_output);
429 ACPI_SERIAL_END(video);
433 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
435 struct acpi_video_softc *sc;
436 struct acpi_video_output *vo;
438 ACPI_SERIAL_ASSERT(video);
441 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
442 if (vo->adr == adr) {
443 acpi_video_vo_bind(vo, handle);
447 vo = acpi_video_vo_init(adr);
449 acpi_video_vo_bind(vo, handle);
450 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
455 acpi_video_bind_outputs(struct acpi_video_softc *sc)
458 ACPI_SERIAL_ASSERT(video);
459 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
462 static struct acpi_video_output *
463 acpi_video_vo_init(UINT32 adr)
465 struct acpi_video_output *vn, *vo, *vp;
467 char name[8], env[32];
468 const char *type, *desc;
469 struct acpi_video_output_queue *voqh;
471 ACPI_SERIAL_ASSERT(video);
473 switch (adr & DOD_DEVID_MASK) {
474 case DOD_DEVID_MONITOR:
475 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
476 /* DOD_DEVID_LCD is a common, backward compatible ID */
477 desc = "Internal/Integrated Digital Flat Panel";
481 desc = "VGA CRT or VESA Compatible Analog Monitor";
487 desc = "TV/HDTV or Analog-Video Monitor";
492 desc = "External Digital Monitor";
496 case DOD_DEVID_INTDFP:
497 desc = "Internal/Integrated Digital Flat Panel";
502 desc = "unknown output";
509 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
510 if (vn->vo_unit.num != n)
516 snprintf(name, sizeof(name), "%s%d", type, n);
518 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
525 vo->vo_brightness = -1;
526 vo->vo_fullpower = -1; /* TODO: override with tunables */
528 vo->vo_numlevels = 0;
529 vo->vo_levels = NULL;
530 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
531 if (getenv_int(env, &x))
532 vo->vo_fullpower = x;
533 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
534 if (getenv_int(env, &x))
537 sysctl_ctx_init(&vo->vo_sysctl_ctx);
539 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
541 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
542 if (acpi_video_sysctl_tree != NULL)
544 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
545 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
546 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE,
548 if (vo->vo_sysctl_tree != NULL) {
549 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
550 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
552 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
553 0, acpi_video_vo_active_sysctl, "I",
554 "current activity of this device");
555 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
556 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
557 OID_AUTO, "brightness",
558 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
559 0, acpi_video_vo_bright_sysctl, "I",
560 "current brightness level");
561 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
562 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
563 OID_AUTO, "fullpower",
564 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
565 POWER_PROFILE_PERFORMANCE,
566 acpi_video_vo_presets_sysctl, "I",
567 "preset level for full power mode");
568 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
569 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
571 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
572 POWER_PROFILE_ECONOMY,
573 acpi_video_vo_presets_sysctl, "I",
574 "preset level for economy mode");
575 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
576 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
578 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, vo,
579 0, acpi_video_vo_levels_sysctl, "I",
580 "supported brightness levels");
582 printf("%s: sysctl node creation failed\n", type);
584 printf("%s: softc allocation failed\n", type);
587 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
588 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
589 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
591 printf(", detectable by BIOS");
592 if (adr & DOD_NONVGA)
593 printf(" (Non-VGA output device whose power "
594 "is related to the VGA device)");
595 printf(", head #%d\n",
596 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
602 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
605 ACPI_SERIAL_BEGIN(video_output);
606 if (vo->vo_levels != NULL) {
607 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
608 acpi_video_vo_notify_handler);
609 AcpiOsFree(vo->vo_levels);
610 vo->vo_levels = NULL;
613 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
614 if (vo->vo_numlevels >= 2) {
615 if (vo->vo_fullpower == -1 ||
616 acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) {
617 /* XXX - can't deal with rebinding... */
618 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
620 if (vo->vo_economy == -1 ||
621 acpi_video_vo_check_level(vo, vo->vo_economy) != 0) {
622 /* XXX - see above. */
623 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
625 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
626 acpi_video_vo_notify_handler, vo);
628 ACPI_SERIAL_END(video_output);
632 acpi_video_vo_destroy(struct acpi_video_output *vo)
634 struct acpi_video_output_queue *voqh;
636 ACPI_SERIAL_ASSERT(video);
637 if (vo->vo_sysctl_tree != NULL) {
638 vo->vo_sysctl_tree = NULL;
639 sysctl_ctx_free(&vo->vo_sysctl_ctx);
641 if (vo->vo_levels != NULL) {
642 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
643 acpi_video_vo_notify_handler);
644 AcpiOsFree(vo->vo_levels);
647 switch (vo->adr & DOD_DEVID_MASK) {
648 case DOD_DEVID_MONITOR:
649 if ((vo->adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD)
660 case DOD_DEVID_INTDFP:
666 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
667 free(vo, M_ACPIVIDEO);
671 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
675 ACPI_SERIAL_ASSERT(video_output);
676 if (vo->vo_levels == NULL)
678 for (i = 0; i < vo->vo_numlevels; i++)
679 if (vo->vo_levels[i] == level)
685 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
687 struct acpi_video_output *vo;
688 int i, j, level, new_level;
691 ACPI_SERIAL_BEGIN(video_output);
692 if (vo->handle != handle)
696 case VID_NOTIFY_CYCLE_BRN:
697 if (vo->vo_numlevels <= 3)
700 case VID_NOTIFY_INC_BRN:
701 case VID_NOTIFY_DEC_BRN:
702 case VID_NOTIFY_ZERO_BRN:
703 if (vo->vo_levels == NULL)
705 level = vo_get_brightness(vo);
710 printf("unknown notify event 0x%x from %s\n",
711 notify, acpi_name(handle));
717 case VID_NOTIFY_CYCLE_BRN:
718 for (i = 2; i < vo->vo_numlevels; i++)
719 if (vo->vo_levels[i] == level) {
720 new_level = vo->vo_numlevels > i + 1 ?
721 vo->vo_levels[i + 1] : vo->vo_levels[2];
725 case VID_NOTIFY_INC_BRN:
726 case VID_NOTIFY_DEC_BRN:
727 for (i = 0; i < vo->vo_numlevels; i++) {
728 j = vo->vo_levels[i];
729 if (notify == VID_NOTIFY_INC_BRN) {
731 (j < new_level || level == new_level))
735 (j > new_level || level == new_level))
740 case VID_NOTIFY_ZERO_BRN:
741 for (i = 0; i < vo->vo_numlevels; i++)
742 if (vo->vo_levels[i] == 0) {
748 if (new_level != level) {
749 vo_set_brightness(vo, new_level);
750 vo->vo_brightness = new_level;
754 ACPI_SERIAL_END(video_output);
759 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
761 struct acpi_video_output *vo;
764 vo = (struct acpi_video_output *)arg1;
765 if (vo->handle == NULL)
767 ACPI_SERIAL_BEGIN(video_output);
768 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
769 err = sysctl_handle_int(oidp, &state, 0, req);
770 if (err != 0 || req->newptr == NULL)
772 vo_set_device_state(vo->handle,
773 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
775 ACPI_SERIAL_END(video_output);
781 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
783 struct acpi_video_output *vo;
784 int level, preset, err;
786 vo = (struct acpi_video_output *)arg1;
787 ACPI_SERIAL_BEGIN(video_output);
788 if (vo->handle == NULL) {
792 if (vo->vo_levels == NULL) {
797 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
798 vo->vo_economy : vo->vo_fullpower;
799 level = vo->vo_brightness;
803 err = sysctl_handle_int(oidp, &level, 0, req);
804 if (err != 0 || req->newptr == NULL)
806 if (level < -1 || level > 100) {
811 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
813 vo->vo_brightness = level;
814 vo_set_brightness(vo, (level == -1) ? preset : level);
817 ACPI_SERIAL_END(video_output);
822 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
824 struct acpi_video_output *vo;
825 int i, level, *preset, err;
827 vo = (struct acpi_video_output *)arg1;
828 ACPI_SERIAL_BEGIN(video_output);
829 if (vo->handle == NULL) {
833 if (vo->vo_levels == NULL) {
837 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
838 &vo->vo_economy : &vo->vo_fullpower;
840 err = sysctl_handle_int(oidp, &level, 0, req);
841 if (err != 0 || req->newptr == NULL)
843 if (level < -1 || level > 100) {
848 i = (arg2 == POWER_PROFILE_ECONOMY) ?
849 BCL_ECONOMY : BCL_FULLPOWER;
850 level = vo->vo_levels[i];
851 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
854 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
855 vo_set_brightness(vo, level);
859 ACPI_SERIAL_END(video_output);
865 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
867 struct acpi_video_output *vo;
870 vo = (struct acpi_video_output *)arg1;
871 ACPI_SERIAL_BEGIN(video_output);
872 if (vo->vo_levels == NULL) {
876 if (req->newptr != NULL) {
880 err = sysctl_handle_opaque(oidp, vo->vo_levels,
881 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
884 ACPI_SERIAL_END(video_output);
889 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
893 status = acpi_SetInteger(handle, "_DOS", policy);
894 if (ACPI_FAILURE(status))
895 printf("can't evaluate %s._DOS - %s\n",
896 acpi_name(handle), AcpiFormatException(status));
899 struct enum_callback_arg {
900 void (*callback)(ACPI_HANDLE, UINT32, void *);
902 ACPI_OBJECT *dod_pkg;
907 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
908 void *context, void **retp __unused)
912 struct enum_callback_arg *argset;
915 ACPI_SERIAL_ASSERT(video);
917 status = acpi_GetInteger(handle, "_ADR", &adr);
918 if (ACPI_FAILURE(status))
921 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
922 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
923 (val & DOD_DEVID_MASK_FULL) ==
924 (adr & DOD_DEVID_MASK_FULL)) {
925 argset->callback(handle, val, argset->context);
934 vid_enum_outputs(ACPI_HANDLE handle,
935 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
940 struct enum_callback_arg argset;
942 ACPI_SERIAL_ASSERT(video);
943 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
944 dod_buf.Pointer = NULL;
945 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
946 if (ACPI_FAILURE(status)) {
947 if (status != AE_NOT_FOUND)
948 printf("can't evaluate %s._DOD - %s\n",
949 acpi_name(handle), AcpiFormatException(status));
953 res = (ACPI_OBJECT *)dod_buf.Pointer;
954 if (!ACPI_PKG_VALID(res, 1)) {
955 printf("evaluation of %s._DOD makes no sense\n",
960 if (callback == NULL) {
961 argset.count = res->Package.Count;
964 argset.callback = callback;
965 argset.context = context;
966 argset.dod_pkg = res;
968 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
969 vid_enum_outputs_subr, NULL, &argset, NULL);
970 if (ACPI_FAILURE(status))
971 printf("failed walking down %s - %s\n",
972 acpi_name(handle), AcpiFormatException(status));
974 if (dod_buf.Pointer != NULL)
975 AcpiOsFree(dod_buf.Pointer);
976 return (argset.count);
980 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
985 int num, i, n, *levels;
987 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
988 bcl_buf.Pointer = NULL;
989 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
990 if (ACPI_FAILURE(status)) {
991 if (status != AE_NOT_FOUND)
992 printf("can't evaluate %s._BCL - %s\n",
993 acpi_name(handle), AcpiFormatException(status));
996 res = (ACPI_OBJECT *)bcl_buf.Pointer;
997 if (!ACPI_PKG_VALID(res, 2)) {
998 printf("evaluation of %s._BCL makes no sense\n",
1002 num = res->Package.Count;
1003 if (num < 2 || levelp == NULL)
1005 levels = AcpiOsAllocate(num * sizeof(*levels));
1008 for (i = 0, n = 0; i < num; i++)
1009 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1019 if (bcl_buf.Pointer != NULL)
1020 AcpiOsFree(bcl_buf.Pointer);
1025 vo_get_bqc(struct acpi_video_output *vo, UINT32 *level)
1029 switch (vo->vo_hasbqc) {
1032 status = acpi_GetInteger(vo->handle, "_BQC", level);
1033 if (vo->vo_hasbqc == 1)
1035 vo->vo_hasbqc = status != AE_NOT_FOUND;
1036 if (vo->vo_hasbqc == 1)
1040 KASSERT(vo->vo_hasbqc == 0,
1041 ("bad vo_hasbqc state %d", vo->vo_hasbqc));
1042 *level = vo->vo_level;
1049 vo_get_brightness(struct acpi_video_output *vo)
1054 ACPI_SERIAL_ASSERT(video_output);
1055 status = vo_get_bqc(vo, &level);
1056 if (ACPI_FAILURE(status)) {
1057 printf("can't evaluate %s._BQC - %s\n", acpi_name(vo->handle),
1058 AcpiFormatException(status));
1068 vo_set_brightness(struct acpi_video_output *vo, int level)
1070 char notify_buf[16];
1073 ACPI_SERIAL_ASSERT(video_output);
1074 status = acpi_SetInteger(vo->handle, "_BCM", level);
1075 if (ACPI_FAILURE(status)) {
1076 printf("can't evaluate %s._BCM - %s\n",
1077 acpi_name(vo->handle), AcpiFormatException(status));
1079 vo->vo_level = level;
1081 snprintf(notify_buf, sizeof(notify_buf), "notify=%d", level);
1082 devctl_notify("ACPI", "Video", "brightness", notify_buf);
1086 vo_get_device_status(ACPI_HANDLE handle)
1091 ACPI_SERIAL_ASSERT(video_output);
1093 status = acpi_GetInteger(handle, "_DCS", &dcs);
1094 if (ACPI_FAILURE(status)) {
1096 * If the method is missing, assume that the device is always
1099 if (status != AE_NOT_FOUND) {
1100 printf("can't evaluate %s._DCS - %s\n",
1101 acpi_name(handle), AcpiFormatException(status));
1111 vo_get_graphics_state(ACPI_HANDLE handle)
1117 status = acpi_GetInteger(handle, "_DGS", &dgs);
1118 if (ACPI_FAILURE(status)) {
1120 * If the method is missing, assume that the device is always
1123 if (status != AE_NOT_FOUND) {
1124 printf("can't evaluate %s._DGS - %s\n",
1125 acpi_name(handle), AcpiFormatException(status));
1135 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1139 ACPI_SERIAL_ASSERT(video_output);
1140 status = acpi_SetInteger(handle, "_DSS", state);
1141 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
1142 printf("can't evaluate %s._DSS - %s\n",
1143 acpi_name(handle), AcpiFormatException(status));