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;
54 int vo_hasbqc; /* Query method is present. */
55 int vo_level; /* Cached level when !vo_hasbqc. */
61 struct sysctl_ctx_list vo_sysctl_ctx;
62 struct sysctl_oid *vo_sysctl_tree;
65 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
67 struct acpi_video_softc {
70 struct acpi_video_output_queue vid_outputs;
71 eventhandler_tag vid_pwr_evh;
75 static int acpi_video_modevent(struct module*, int, void *);
76 static void acpi_video_identify(driver_t *driver, device_t parent);
77 static int acpi_video_probe(device_t);
78 static int acpi_video_attach(device_t);
79 static int acpi_video_detach(device_t);
80 static int acpi_video_resume(device_t);
81 static int acpi_video_shutdown(device_t);
82 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
83 static void acpi_video_power_profile(void *);
84 static void acpi_video_bind_outputs(struct acpi_video_softc *);
85 static struct acpi_video_output *acpi_video_vo_init(UINT32);
86 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
87 static void acpi_video_vo_destroy(struct acpi_video_output *);
88 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
89 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
90 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
91 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
92 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
93 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
96 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
97 static int vid_enum_outputs(ACPI_HANDLE,
98 void(*)(ACPI_HANDLE, UINT32, void *), void *);
99 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
100 static int vo_get_brightness(struct acpi_video_output *);
101 static void vo_set_brightness(struct acpi_video_output *, int);
102 static UINT32 vo_get_device_status(ACPI_HANDLE);
103 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
104 static void vo_set_device_state(ACPI_HANDLE, UINT32);
107 #define VID_NOTIFY_SWITCHED 0x80
108 #define VID_NOTIFY_REPROBE 0x81
109 #define VID_NOTIFY_CYCLE_BRN 0x85
110 #define VID_NOTIFY_INC_BRN 0x86
111 #define VID_NOTIFY_DEC_BRN 0x87
112 #define VID_NOTIFY_ZERO_BRN 0x88
114 /* _DOS (Enable/Disable Output Switching) argument bits */
115 #define DOS_SWITCH_MASK 3
116 #define DOS_SWITCH_BY_OSPM 0
117 #define DOS_SWITCH_BY_BIOS 1
118 #define DOS_SWITCH_LOCKED 2
119 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
121 /* _DOD and subdev's _ADR */
122 #define DOD_DEVID_MASK 0x0f00
123 #define DOD_DEVID_MASK_FULL 0xffff
124 #define DOD_DEVID_MASK_DISPIDX 0x000f
125 #define DOD_DEVID_MASK_DISPPORT 0x00f0
126 #define DOD_DEVID_MONITOR 0x0100
127 #define DOD_DEVID_LCD 0x0110
128 #define DOD_DEVID_TV 0x0200
129 #define DOD_DEVID_EXT 0x0300
130 #define DOD_DEVID_INTDFP 0x0400
131 #define DOD_BIOS (1 << 16)
132 #define DOD_NONVGA (1 << 17)
133 #define DOD_HEAD_ID_SHIFT 18
134 #define DOD_HEAD_ID_BITS 3
135 #define DOD_HEAD_ID_MASK \
136 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
137 #define DOD_DEVID_SCHEME_STD (1U << 31)
139 /* _BCL related constants */
140 #define BCL_FULLPOWER 0
141 #define BCL_ECONOMY 1
143 /* _DCS (Device Currrent Status) value bits and masks. */
144 #define DCS_EXISTS (1 << 0)
145 #define DCS_ACTIVE (1 << 1)
146 #define DCS_READY (1 << 2)
147 #define DCS_FUNCTIONAL (1 << 3)
148 #define DCS_ATTACHED (1 << 4)
150 /* _DSS (Device Set Status) argument bits and masks. */
151 #define DSS_INACTIVE 0
152 #define DSS_ACTIVE (1 << 0)
153 #define DSS_SETNEXT (1 << 30)
154 #define DSS_COMMIT (1U << 31)
156 static device_method_t acpi_video_methods[] = {
157 DEVMETHOD(device_identify, acpi_video_identify),
158 DEVMETHOD(device_probe, acpi_video_probe),
159 DEVMETHOD(device_attach, acpi_video_attach),
160 DEVMETHOD(device_detach, acpi_video_detach),
161 DEVMETHOD(device_resume, acpi_video_resume),
162 DEVMETHOD(device_shutdown, acpi_video_shutdown),
166 static driver_t acpi_video_driver = {
169 sizeof(struct acpi_video_softc),
172 static devclass_t acpi_video_devclass;
174 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
175 acpi_video_modevent, NULL);
176 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
178 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
179 static struct sysctl_oid *acpi_video_sysctl_tree;
180 static struct acpi_video_output_queue crt_units, tv_units,
181 ext_units, lcd_units, other_units;
184 * The 'video' lock protects the hierarchy of video output devices
185 * (the video "bus"). The 'video_output' lock protects per-output
186 * data is equivalent to a softc lock for each video output.
188 ACPI_SERIAL_DECL(video, "ACPI video");
189 ACPI_SERIAL_DECL(video_output, "ACPI video output");
190 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
193 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
200 sysctl_ctx_init(&acpi_video_sysctl_ctx);
201 STAILQ_INIT(&crt_units);
202 STAILQ_INIT(&tv_units);
203 STAILQ_INIT(&ext_units);
204 STAILQ_INIT(&lcd_units);
205 STAILQ_INIT(&other_units);
208 sysctl_ctx_free(&acpi_video_sysctl_ctx);
209 acpi_video_sysctl_tree = NULL;
219 acpi_video_identify(driver_t *driver, device_t parent)
222 if (device_find_child(parent, "acpi_video", -1) == NULL)
223 device_add_child(parent, "acpi_video", -1);
227 acpi_video_probe(device_t dev)
230 ACPI_OBJECT_TYPE t_dos;
232 devh = acpi_get_handle(dev);
233 if (acpi_disabled("video") ||
234 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
235 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
236 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
237 t_dos != ACPI_TYPE_METHOD)
240 device_set_desc(dev, "ACPI video extension");
245 acpi_video_attach(device_t dev)
247 struct acpi_softc *acpi_sc;
248 struct acpi_video_softc *sc;
250 sc = device_get_softc(dev);
252 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
255 ACPI_SERIAL_BEGIN(video);
256 if (acpi_video_sysctl_tree == NULL) {
257 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
258 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
259 OID_AUTO, "video", CTLFLAG_RD, 0,
260 "video extension control");
262 ACPI_SERIAL_END(video);
265 sc->handle = acpi_get_handle(dev);
266 STAILQ_INIT(&sc->vid_outputs);
268 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
269 acpi_video_notify_handler, sc);
270 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
271 acpi_video_power_profile, sc, 0);
273 ACPI_SERIAL_BEGIN(video);
274 acpi_video_bind_outputs(sc);
275 ACPI_SERIAL_END(video);
278 * Notify the BIOS that we want to switch both active outputs and
281 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
282 DOS_BRIGHTNESS_BY_OSPM);
284 acpi_video_power_profile(sc);
290 acpi_video_detach(device_t dev)
292 struct acpi_video_softc *sc;
293 struct acpi_video_output *vo, *vn;
295 sc = device_get_softc(dev);
297 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
298 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
299 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
300 acpi_video_notify_handler);
302 ACPI_SERIAL_BEGIN(video);
303 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
304 acpi_video_vo_destroy(vo);
306 ACPI_SERIAL_END(video);
312 acpi_video_resume(device_t dev)
314 struct acpi_video_softc *sc;
315 struct acpi_video_output *vo, *vn;
318 sc = device_get_softc(dev);
320 /* Restore brightness level */
321 ACPI_SERIAL_BEGIN(video);
322 ACPI_SERIAL_BEGIN(video_output);
323 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
324 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
325 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
328 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
331 level = vo_get_brightness(vo);
333 vo_set_brightness(vo, level);
335 ACPI_SERIAL_END(video_output);
336 ACPI_SERIAL_END(video);
342 acpi_video_shutdown(device_t dev)
344 struct acpi_video_softc *sc;
346 sc = device_get_softc(dev);
347 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
353 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
355 struct acpi_video_softc *sc;
356 struct acpi_video_output *vo, *vo_tmp;
357 ACPI_HANDLE lasthand;
358 UINT32 dcs, dss, dss_p;
360 sc = (struct acpi_video_softc *)context;
363 case VID_NOTIFY_SWITCHED:
366 ACPI_SERIAL_BEGIN(video);
367 ACPI_SERIAL_BEGIN(video_output);
368 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
369 dss = vo_get_graphics_state(vo->handle);
370 dcs = vo_get_device_status(vo->handle);
371 if (!(dcs & DCS_READY))
373 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
374 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
375 if (lasthand != NULL)
376 vo_set_device_state(lasthand, dss_p);
378 lasthand = vo->handle;
381 if (lasthand != NULL)
382 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
383 ACPI_SERIAL_END(video_output);
384 ACPI_SERIAL_END(video);
386 case VID_NOTIFY_REPROBE:
387 ACPI_SERIAL_BEGIN(video);
388 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
390 acpi_video_bind_outputs(sc);
391 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
392 if (vo->handle == NULL) {
393 STAILQ_REMOVE(&sc->vid_outputs, vo,
394 acpi_video_output, vo_next);
395 acpi_video_vo_destroy(vo);
398 ACPI_SERIAL_END(video);
401 device_printf(sc->device, "unknown notify event 0x%x\n",
407 acpi_video_power_profile(void *context)
410 struct acpi_video_softc *sc;
411 struct acpi_video_output *vo;
414 state = power_profile_get_state();
415 if (state != POWER_PROFILE_PERFORMANCE &&
416 state != POWER_PROFILE_ECONOMY)
419 ACPI_SERIAL_BEGIN(video);
420 ACPI_SERIAL_BEGIN(video_output);
421 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
422 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
423 vo_set_brightness(vo,
424 state == POWER_PROFILE_ECONOMY ?
425 vo->vo_economy : vo->vo_fullpower);
427 ACPI_SERIAL_END(video_output);
428 ACPI_SERIAL_END(video);
432 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
434 struct acpi_video_softc *sc;
435 struct acpi_video_output *vo;
437 ACPI_SERIAL_ASSERT(video);
440 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
441 if (vo->adr == adr) {
442 acpi_video_vo_bind(vo, handle);
446 vo = acpi_video_vo_init(adr);
448 acpi_video_vo_bind(vo, handle);
449 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
454 acpi_video_bind_outputs(struct acpi_video_softc *sc)
457 ACPI_SERIAL_ASSERT(video);
458 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
461 static struct acpi_video_output *
462 acpi_video_vo_init(UINT32 adr)
464 struct acpi_video_output *vn, *vo, *vp;
466 char name[8], env[32];
467 const char *type, *desc;
468 struct acpi_video_output_queue *voqh;
470 ACPI_SERIAL_ASSERT(video);
472 switch (adr & DOD_DEVID_MASK) {
473 case DOD_DEVID_MONITOR:
474 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
475 /* DOD_DEVID_LCD is a common, backward compatible ID */
476 desc = "Internal/Integrated Digital Flat Panel";
480 desc = "VGA CRT or VESA Compatible Analog Monitor";
486 desc = "TV/HDTV or Analog-Video Monitor";
491 desc = "External Digital Monitor";
495 case DOD_DEVID_INTDFP:
496 desc = "Internal/Integrated Digital Flat Panel";
501 desc = "unknown output";
508 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
509 if (vn->vo_unit.num != n)
515 snprintf(name, sizeof(name), "%s%d", type, n);
517 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
524 vo->vo_brightness = -1;
525 vo->vo_fullpower = -1; /* TODO: override with tunables */
527 vo->vo_numlevels = 0;
528 vo->vo_levels = NULL;
529 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
530 if (getenv_int(env, &x))
531 vo->vo_fullpower = x;
532 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
533 if (getenv_int(env, &x))
536 sysctl_ctx_init(&vo->vo_sysctl_ctx);
538 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
540 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
541 if (acpi_video_sysctl_tree != NULL)
543 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
544 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
545 OID_AUTO, name, CTLFLAG_RD, 0, desc);
546 if (vo->vo_sysctl_tree != NULL) {
547 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
548 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
550 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
551 acpi_video_vo_active_sysctl, "I",
552 "current activity of this device");
553 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
554 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
555 OID_AUTO, "brightness",
556 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
557 acpi_video_vo_bright_sysctl, "I",
558 "current brightness level");
559 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
560 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
561 OID_AUTO, "fullpower",
562 CTLTYPE_INT|CTLFLAG_RW, vo,
563 POWER_PROFILE_PERFORMANCE,
564 acpi_video_vo_presets_sysctl, "I",
565 "preset level for full power mode");
566 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
567 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
569 CTLTYPE_INT|CTLFLAG_RW, vo,
570 POWER_PROFILE_ECONOMY,
571 acpi_video_vo_presets_sysctl, "I",
572 "preset level for economy mode");
573 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
574 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
576 CTLTYPE_INT | CTLFLAG_RD, vo, 0,
577 acpi_video_vo_levels_sysctl, "I",
578 "supported brightness levels");
580 printf("%s: sysctl node creation failed\n", type);
582 printf("%s: softc allocation failed\n", type);
585 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
586 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
587 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
589 printf(", detectable by BIOS");
590 if (adr & DOD_NONVGA)
591 printf(" (Non-VGA output device whose power "
592 "is related to the VGA device)");
593 printf(", head #%d\n",
594 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
600 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
603 ACPI_SERIAL_BEGIN(video_output);
604 if (vo->vo_levels != NULL) {
605 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
606 acpi_video_vo_notify_handler);
607 AcpiOsFree(vo->vo_levels);
608 vo->vo_levels = NULL;
611 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
612 if (vo->vo_numlevels >= 2) {
613 if (vo->vo_fullpower == -1 ||
614 acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) {
615 /* XXX - can't deal with rebinding... */
616 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
618 if (vo->vo_economy == -1 ||
619 acpi_video_vo_check_level(vo, vo->vo_economy) != 0) {
620 /* XXX - see above. */
621 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
623 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
624 acpi_video_vo_notify_handler, vo);
626 ACPI_SERIAL_END(video_output);
630 acpi_video_vo_destroy(struct acpi_video_output *vo)
632 struct acpi_video_output_queue *voqh;
634 ACPI_SERIAL_ASSERT(video);
635 if (vo->vo_sysctl_tree != NULL) {
636 vo->vo_sysctl_tree = NULL;
637 sysctl_ctx_free(&vo->vo_sysctl_ctx);
639 if (vo->vo_levels != NULL) {
640 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
641 acpi_video_vo_notify_handler);
642 AcpiOsFree(vo->vo_levels);
645 switch (vo->adr & DOD_DEVID_MASK) {
646 case DOD_DEVID_MONITOR:
647 if ((vo->adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD)
658 case DOD_DEVID_INTDFP:
664 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
665 free(vo, M_ACPIVIDEO);
669 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
673 ACPI_SERIAL_ASSERT(video_output);
674 if (vo->vo_levels == NULL)
676 for (i = 0; i < vo->vo_numlevels; i++)
677 if (vo->vo_levels[i] == level)
683 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
685 struct acpi_video_output *vo;
686 int i, j, level, new_level;
689 ACPI_SERIAL_BEGIN(video_output);
690 if (vo->handle != handle)
694 case VID_NOTIFY_CYCLE_BRN:
695 if (vo->vo_numlevels <= 3)
698 case VID_NOTIFY_INC_BRN:
699 case VID_NOTIFY_DEC_BRN:
700 case VID_NOTIFY_ZERO_BRN:
701 if (vo->vo_levels == NULL)
703 level = vo_get_brightness(vo);
708 printf("unknown notify event 0x%x from %s\n",
709 notify, acpi_name(handle));
715 case VID_NOTIFY_CYCLE_BRN:
716 for (i = 2; i < vo->vo_numlevels; i++)
717 if (vo->vo_levels[i] == level) {
718 new_level = vo->vo_numlevels > i + 1 ?
719 vo->vo_levels[i + 1] : vo->vo_levels[2];
723 case VID_NOTIFY_INC_BRN:
724 case VID_NOTIFY_DEC_BRN:
725 for (i = 0; i < vo->vo_numlevels; i++) {
726 j = vo->vo_levels[i];
727 if (notify == VID_NOTIFY_INC_BRN) {
729 (j < new_level || level == new_level))
733 (j > new_level || level == new_level))
738 case VID_NOTIFY_ZERO_BRN:
739 for (i = 0; i < vo->vo_numlevels; i++)
740 if (vo->vo_levels[i] == 0) {
746 if (new_level != level) {
747 vo_set_brightness(vo, new_level);
748 vo->vo_brightness = new_level;
752 ACPI_SERIAL_END(video_output);
757 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
759 struct acpi_video_output *vo;
762 vo = (struct acpi_video_output *)arg1;
763 if (vo->handle == NULL)
765 ACPI_SERIAL_BEGIN(video_output);
766 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
767 err = sysctl_handle_int(oidp, &state, 0, req);
768 if (err != 0 || req->newptr == NULL)
770 vo_set_device_state(vo->handle,
771 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
773 ACPI_SERIAL_END(video_output);
779 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
781 struct acpi_video_output *vo;
782 int level, preset, err;
784 vo = (struct acpi_video_output *)arg1;
785 ACPI_SERIAL_BEGIN(video_output);
786 if (vo->handle == NULL) {
790 if (vo->vo_levels == NULL) {
795 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
796 vo->vo_economy : vo->vo_fullpower;
797 level = vo->vo_brightness;
801 err = sysctl_handle_int(oidp, &level, 0, req);
802 if (err != 0 || req->newptr == NULL)
804 if (level < -1 || level > 100) {
809 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
811 vo->vo_brightness = level;
812 vo_set_brightness(vo, (level == -1) ? preset : level);
815 ACPI_SERIAL_END(video_output);
820 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
822 struct acpi_video_output *vo;
823 int i, level, *preset, err;
825 vo = (struct acpi_video_output *)arg1;
826 ACPI_SERIAL_BEGIN(video_output);
827 if (vo->handle == NULL) {
831 if (vo->vo_levels == NULL) {
835 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
836 &vo->vo_economy : &vo->vo_fullpower;
838 err = sysctl_handle_int(oidp, &level, 0, req);
839 if (err != 0 || req->newptr == NULL)
841 if (level < -1 || level > 100) {
846 i = (arg2 == POWER_PROFILE_ECONOMY) ?
847 BCL_ECONOMY : BCL_FULLPOWER;
848 level = vo->vo_levels[i];
849 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
852 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
853 vo_set_brightness(vo, level);
857 ACPI_SERIAL_END(video_output);
863 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
865 struct acpi_video_output *vo;
868 vo = (struct acpi_video_output *)arg1;
869 ACPI_SERIAL_BEGIN(video_output);
870 if (vo->vo_levels == NULL) {
874 if (req->newptr != NULL) {
878 err = sysctl_handle_opaque(oidp, vo->vo_levels,
879 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
882 ACPI_SERIAL_END(video_output);
887 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
891 status = acpi_SetInteger(handle, "_DOS", policy);
892 if (ACPI_FAILURE(status))
893 printf("can't evaluate %s._DOS - %s\n",
894 acpi_name(handle), AcpiFormatException(status));
897 struct enum_callback_arg {
898 void (*callback)(ACPI_HANDLE, UINT32, void *);
900 ACPI_OBJECT *dod_pkg;
905 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
906 void *context, void **retp __unused)
910 struct enum_callback_arg *argset;
913 ACPI_SERIAL_ASSERT(video);
915 status = acpi_GetInteger(handle, "_ADR", &adr);
916 if (ACPI_FAILURE(status))
919 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
920 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
921 (val & DOD_DEVID_MASK_FULL) ==
922 (adr & DOD_DEVID_MASK_FULL)) {
923 argset->callback(handle, val, argset->context);
932 vid_enum_outputs(ACPI_HANDLE handle,
933 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
938 struct enum_callback_arg argset;
940 ACPI_SERIAL_ASSERT(video);
941 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
942 dod_buf.Pointer = NULL;
943 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
944 if (ACPI_FAILURE(status)) {
945 if (status != AE_NOT_FOUND)
946 printf("can't evaluate %s._DOD - %s\n",
947 acpi_name(handle), AcpiFormatException(status));
951 res = (ACPI_OBJECT *)dod_buf.Pointer;
952 if (!ACPI_PKG_VALID(res, 1)) {
953 printf("evaluation of %s._DOD makes no sense\n",
958 if (callback == NULL) {
959 argset.count = res->Package.Count;
962 argset.callback = callback;
963 argset.context = context;
964 argset.dod_pkg = res;
966 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
967 vid_enum_outputs_subr, NULL, &argset, NULL);
968 if (ACPI_FAILURE(status))
969 printf("failed walking down %s - %s\n",
970 acpi_name(handle), AcpiFormatException(status));
972 if (dod_buf.Pointer != NULL)
973 AcpiOsFree(dod_buf.Pointer);
974 return (argset.count);
978 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
983 int num, i, n, *levels;
985 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
986 bcl_buf.Pointer = NULL;
987 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
988 if (ACPI_FAILURE(status)) {
989 if (status != AE_NOT_FOUND)
990 printf("can't evaluate %s._BCL - %s\n",
991 acpi_name(handle), AcpiFormatException(status));
994 res = (ACPI_OBJECT *)bcl_buf.Pointer;
995 if (!ACPI_PKG_VALID(res, 2)) {
996 printf("evaluation of %s._BCL makes no sense\n",
1000 num = res->Package.Count;
1001 if (num < 2 || levelp == NULL)
1003 levels = AcpiOsAllocate(num * sizeof(*levels));
1006 for (i = 0, n = 0; i < num; i++)
1007 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1017 if (bcl_buf.Pointer != NULL)
1018 AcpiOsFree(bcl_buf.Pointer);
1023 vo_get_bqc(struct acpi_video_output *vo, UINT32 *level)
1027 switch (vo->vo_hasbqc) {
1030 status = acpi_GetInteger(vo->handle, "_BQC", level);
1031 if (vo->vo_hasbqc == 1)
1033 vo->vo_hasbqc = status != AE_NOT_FOUND;
1034 if (vo->vo_hasbqc == 1)
1038 KASSERT(vo->vo_hasbqc == 0,
1039 ("bad vo_hasbqc state %d", vo->vo_hasbqc));
1040 *level = vo->vo_level;
1047 vo_get_brightness(struct acpi_video_output *vo)
1052 ACPI_SERIAL_ASSERT(video_output);
1053 status = vo_get_bqc(vo, &level);
1054 if (ACPI_FAILURE(status)) {
1055 printf("can't evaluate %s._BQC - %s\n", acpi_name(vo->handle),
1056 AcpiFormatException(status));
1066 vo_set_brightness(struct acpi_video_output *vo, int level)
1068 char notify_buf[16];
1071 ACPI_SERIAL_ASSERT(video_output);
1072 status = acpi_SetInteger(vo->handle, "_BCM", level);
1073 if (ACPI_FAILURE(status)) {
1074 printf("can't evaluate %s._BCM - %s\n",
1075 acpi_name(vo->handle), AcpiFormatException(status));
1077 vo->vo_level = level;
1079 snprintf(notify_buf, sizeof(notify_buf), "notify=%d", level);
1080 devctl_notify("ACPI", "Video", "brightness", notify_buf);
1084 vo_get_device_status(ACPI_HANDLE handle)
1089 ACPI_SERIAL_ASSERT(video_output);
1091 status = acpi_GetInteger(handle, "_DCS", &dcs);
1092 if (ACPI_FAILURE(status)) {
1094 * If the method is missing, assume that the device is always
1097 if (status != AE_NOT_FOUND) {
1098 printf("can't evaluate %s._DCS - %s\n",
1099 acpi_name(handle), AcpiFormatException(status));
1109 vo_get_graphics_state(ACPI_HANDLE handle)
1115 status = acpi_GetInteger(handle, "_DGS", &dgs);
1116 if (ACPI_FAILURE(status)) {
1118 * If the method is missing, assume that the device is always
1121 if (status != AE_NOT_FOUND) {
1122 printf("can't evaluate %s._DGS - %s\n",
1123 acpi_name(handle), AcpiFormatException(status));
1133 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1137 ACPI_SERIAL_ASSERT(video_output);
1138 status = acpi_SetInteger(handle, "_DSS", state);
1139 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
1140 printf("can't evaluate %s._DSS - %s\n",
1141 acpi_name(handle), AcpiFormatException(status));