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_shutdown(device_t);
79 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
80 static void acpi_video_power_profile(void *);
81 static void acpi_video_bind_outputs(struct acpi_video_softc *);
82 static struct acpi_video_output *acpi_video_vo_init(UINT32);
83 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
84 static void acpi_video_vo_destroy(struct acpi_video_output *);
85 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
86 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
87 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
88 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
89 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
90 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
93 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
94 static int vid_enum_outputs(ACPI_HANDLE,
95 void(*)(ACPI_HANDLE, UINT32, void *), void *);
96 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
97 static int vo_get_brightness(ACPI_HANDLE);
98 static void vo_set_brightness(ACPI_HANDLE, int);
99 static UINT32 vo_get_device_status(ACPI_HANDLE);
100 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
101 static void vo_set_device_state(ACPI_HANDLE, UINT32);
104 #define VID_NOTIFY_SWITCHED 0x80
105 #define VID_NOTIFY_REPROBE 0x81
106 #define VID_NOTIFY_CYCLE_BRN 0x85
107 #define VID_NOTIFY_INC_BRN 0x86
108 #define VID_NOTIFY_DEC_BRN 0x87
109 #define VID_NOTIFY_ZERO_BRN 0x88
111 /* _DOS (Enable/Disable Output Switching) argument bits */
112 #define DOS_SWITCH_MASK 3
113 #define DOS_SWITCH_BY_OSPM 0
114 #define DOS_SWITCH_BY_BIOS 1
115 #define DOS_SWITCH_LOCKED 2
116 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
118 /* _DOD and subdev's _ADR */
119 #define DOD_DEVID_MASK 0x0f00
120 #define DOD_DEVID_MASK_FULL 0xffff
121 #define DOD_DEVID_MASK_DISPIDX 0x000f
122 #define DOD_DEVID_MASK_DISPPORT 0x00f0
123 #define DOD_DEVID_MONITOR 0x0100
124 #define DOD_DEVID_LCD 0x0110
125 #define DOD_DEVID_TV 0x0200
126 #define DOD_DEVID_EXT 0x0300
127 #define DOD_DEVID_INTDFP 0x0400
128 #define DOD_BIOS (1 << 16)
129 #define DOD_NONVGA (1 << 17)
130 #define DOD_HEAD_ID_SHIFT 18
131 #define DOD_HEAD_ID_BITS 3
132 #define DOD_HEAD_ID_MASK \
133 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
134 #define DOD_DEVID_SCHEME_STD (1 << 31)
136 /* _BCL related constants */
137 #define BCL_FULLPOWER 0
138 #define BCL_ECONOMY 1
140 /* _DCS (Device Currrent Status) value bits and masks. */
141 #define DCS_EXISTS (1 << 0)
142 #define DCS_ACTIVE (1 << 1)
143 #define DCS_READY (1 << 2)
144 #define DCS_FUNCTIONAL (1 << 3)
145 #define DCS_ATTACHED (1 << 4)
147 /* _DSS (Device Set Status) argument bits and masks. */
148 #define DSS_INACTIVE 0
149 #define DSS_ACTIVE (1 << 0)
150 #define DSS_SETNEXT (1 << 30)
151 #define DSS_COMMIT (1 << 31)
153 static device_method_t acpi_video_methods[] = {
154 DEVMETHOD(device_identify, acpi_video_identify),
155 DEVMETHOD(device_probe, acpi_video_probe),
156 DEVMETHOD(device_attach, acpi_video_attach),
157 DEVMETHOD(device_detach, acpi_video_detach),
158 DEVMETHOD(device_shutdown, acpi_video_shutdown),
162 static driver_t acpi_video_driver = {
165 sizeof(struct acpi_video_softc),
168 static devclass_t acpi_video_devclass;
170 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
171 acpi_video_modevent, NULL);
172 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
174 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
175 static struct sysctl_oid *acpi_video_sysctl_tree;
176 static struct acpi_video_output_queue crt_units, tv_units,
177 ext_units, lcd_units, other_units;
180 * The 'video' lock protects the hierarchy of video output devices
181 * (the video "bus"). The 'video_output' lock protects per-output
182 * data is equivalent to a softc lock for each video output.
184 ACPI_SERIAL_DECL(video, "ACPI video");
185 ACPI_SERIAL_DECL(video_output, "ACPI video output");
186 MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
189 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
196 sysctl_ctx_init(&acpi_video_sysctl_ctx);
197 STAILQ_INIT(&crt_units);
198 STAILQ_INIT(&tv_units);
199 STAILQ_INIT(&ext_units);
200 STAILQ_INIT(&lcd_units);
201 STAILQ_INIT(&other_units);
204 sysctl_ctx_free(&acpi_video_sysctl_ctx);
205 acpi_video_sysctl_tree = NULL;
215 acpi_video_identify(driver_t *driver, device_t parent)
218 if (device_find_child(parent, "acpi_video", -1) == NULL)
219 device_add_child(parent, "acpi_video", -1);
223 acpi_video_probe(device_t dev)
226 ACPI_OBJECT_TYPE t_dos;
228 devh = acpi_get_handle(dev);
229 if (acpi_disabled("video") ||
230 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
231 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
232 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
233 t_dos != ACPI_TYPE_METHOD)
236 device_set_desc(dev, "ACPI video extension");
241 acpi_video_attach(device_t dev)
243 struct acpi_softc *acpi_sc;
244 struct acpi_video_softc *sc;
246 sc = device_get_softc(dev);
248 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
251 ACPI_SERIAL_BEGIN(video);
252 if (acpi_video_sysctl_tree == NULL) {
253 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
254 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
255 OID_AUTO, "video", CTLFLAG_RD, 0,
256 "video extension control");
258 ACPI_SERIAL_END(video);
261 sc->handle = acpi_get_handle(dev);
262 STAILQ_INIT(&sc->vid_outputs);
264 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
265 acpi_video_notify_handler, sc);
266 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
267 acpi_video_power_profile, sc, 0);
269 ACPI_SERIAL_BEGIN(video);
270 acpi_video_bind_outputs(sc);
271 ACPI_SERIAL_END(video);
274 * Notify the BIOS that we want to switch both active outputs and
277 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
278 DOS_BRIGHTNESS_BY_OSPM);
280 acpi_video_power_profile(sc);
286 acpi_video_detach(device_t dev)
288 struct acpi_video_softc *sc;
289 struct acpi_video_output *vo, *vn;
291 sc = device_get_softc(dev);
293 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
294 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
295 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
296 acpi_video_notify_handler);
298 ACPI_SERIAL_BEGIN(video);
299 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
300 acpi_video_vo_destroy(vo);
302 ACPI_SERIAL_END(video);
308 acpi_video_shutdown(device_t dev)
310 struct acpi_video_softc *sc;
312 sc = device_get_softc(dev);
313 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
319 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
321 struct acpi_video_softc *sc;
322 struct acpi_video_output *vo, *vo_tmp;
323 ACPI_HANDLE lasthand;
324 UINT32 dcs, dss, dss_p;
326 sc = (struct acpi_video_softc *)context;
329 case VID_NOTIFY_SWITCHED:
332 ACPI_SERIAL_BEGIN(video);
333 ACPI_SERIAL_BEGIN(video_output);
334 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
335 dss = vo_get_graphics_state(vo->handle);
336 dcs = vo_get_device_status(vo->handle);
337 if (!(dcs & DCS_READY))
339 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
340 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
341 if (lasthand != NULL)
342 vo_set_device_state(lasthand, dss_p);
344 lasthand = vo->handle;
347 if (lasthand != NULL)
348 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
349 ACPI_SERIAL_END(video_output);
350 ACPI_SERIAL_END(video);
352 case VID_NOTIFY_REPROBE:
353 ACPI_SERIAL_BEGIN(video);
354 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
356 acpi_video_bind_outputs(sc);
357 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
358 if (vo->handle == NULL) {
359 STAILQ_REMOVE(&sc->vid_outputs, vo,
360 acpi_video_output, vo_next);
361 acpi_video_vo_destroy(vo);
364 ACPI_SERIAL_END(video);
367 device_printf(sc->device, "unknown notify event 0x%x\n",
373 acpi_video_power_profile(void *context)
376 struct acpi_video_softc *sc;
377 struct acpi_video_output *vo;
380 state = power_profile_get_state();
381 if (state != POWER_PROFILE_PERFORMANCE &&
382 state != POWER_PROFILE_ECONOMY)
385 ACPI_SERIAL_BEGIN(video);
386 ACPI_SERIAL_BEGIN(video_output);
387 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
388 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
389 vo_set_brightness(vo->handle,
390 state == POWER_PROFILE_ECONOMY ?
391 vo->vo_economy : vo->vo_fullpower);
393 ACPI_SERIAL_END(video_output);
394 ACPI_SERIAL_END(video);
398 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
400 struct acpi_video_softc *sc;
401 struct acpi_video_output *vo;
403 ACPI_SERIAL_ASSERT(video);
406 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
407 if (vo->adr == adr) {
408 acpi_video_vo_bind(vo, handle);
412 vo = acpi_video_vo_init(adr);
414 acpi_video_vo_bind(vo, handle);
415 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
420 acpi_video_bind_outputs(struct acpi_video_softc *sc)
423 ACPI_SERIAL_ASSERT(video);
424 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
427 static struct acpi_video_output *
428 acpi_video_vo_init(UINT32 adr)
430 struct acpi_video_output *vn, *vo, *vp;
434 char name[8], env[32];
435 const char *type, *desc;
436 struct acpi_video_output_queue *voqh;
438 ACPI_SERIAL_ASSERT(video);
439 display_index = adr & DOD_DEVID_MASK_DISPIDX;
440 display_port = (adr & DOD_DEVID_MASK_DISPPORT) >> 4;
442 switch (adr & DOD_DEVID_MASK) {
443 case DOD_DEVID_MONITOR:
444 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
445 /* DOD_DEVID_LCD is a common, backward compatible ID */
446 desc = "Internal/Integrated Digital Flat Panel";
450 desc = "VGA CRT or VESA Compatible Analog Monitor";
456 desc = "TV/HDTV or Analog-Video Monitor";
461 desc = "External Digital Monitor";
465 case DOD_DEVID_INTDFP:
466 desc = "Internal/Integrated Digital Flat Panel";
471 desc = "unknown output";
478 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
479 if (vn->vo_unit.num != n)
485 snprintf(name, sizeof(name), "%s%d", type, n);
487 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
492 vo->vo_brightness = -1;
493 vo->vo_fullpower = -1; /* TODO: override with tunables */
495 vo->vo_numlevels = 0;
496 vo->vo_levels = NULL;
497 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
498 if (getenv_int(env, &x))
499 vo->vo_fullpower = x;
500 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
501 if (getenv_int(env, &x))
504 sysctl_ctx_init(&vo->vo_sysctl_ctx);
506 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
508 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
509 if (acpi_video_sysctl_tree != NULL)
511 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
512 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
513 OID_AUTO, name, CTLFLAG_RD, 0, desc);
514 if (vo->vo_sysctl_tree != NULL) {
515 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
516 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
518 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
519 acpi_video_vo_active_sysctl, "I",
520 "current activity of this device");
521 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
522 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
523 OID_AUTO, "brightness",
524 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
525 acpi_video_vo_bright_sysctl, "I",
526 "current brightness level");
527 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
528 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
529 OID_AUTO, "fullpower",
530 CTLTYPE_INT|CTLFLAG_RW, vo,
531 POWER_PROFILE_PERFORMANCE,
532 acpi_video_vo_presets_sysctl, "I",
533 "preset level for full power mode");
534 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
535 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
537 CTLTYPE_INT|CTLFLAG_RW, vo,
538 POWER_PROFILE_ECONOMY,
539 acpi_video_vo_presets_sysctl, "I",
540 "preset level for economy mode");
541 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
542 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
544 CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
545 acpi_video_vo_levels_sysctl, "I",
546 "supported brightness levels");
548 printf("%s: sysctl node creation failed\n", type);
550 printf("%s: softc allocation failed\n", type);
553 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
554 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
555 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
557 printf(", detectable by BIOS");
558 if (adr & DOD_NONVGA)
559 printf(" (Non-VGA output device whose power "
560 "is related to the VGA device)");
561 printf(", head #%d\n",
562 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
568 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
571 ACPI_SERIAL_BEGIN(video_output);
572 if (vo->vo_levels != NULL)
573 AcpiOsFree(vo->vo_levels);
575 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
576 if (vo->vo_numlevels >= 2) {
577 if (vo->vo_fullpower == -1
578 || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
579 /* XXX - can't deal with rebinding... */
580 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
581 if (vo->vo_economy == -1
582 || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
583 /* XXX - see above. */
584 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
586 if (vo->vo_levels != NULL)
587 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
588 acpi_video_vo_notify_handler, vo);
589 ACPI_SERIAL_END(video_output);
593 acpi_video_vo_destroy(struct acpi_video_output *vo)
595 struct acpi_video_output_queue *voqh;
597 ACPI_SERIAL_ASSERT(video);
598 if (vo->vo_sysctl_tree != NULL) {
599 vo->vo_sysctl_tree = NULL;
600 sysctl_ctx_free(&vo->vo_sysctl_ctx);
602 if (vo->vo_levels != NULL) {
603 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
604 acpi_video_vo_notify_handler);
605 AcpiOsFree(vo->vo_levels);
608 switch (vo->adr & DOD_DEVID_MASK) {
609 case DOD_DEVID_MONITOR:
618 case DOD_DEVID_INTDFP:
624 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
625 free(vo, M_ACPIVIDEO);
629 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
633 ACPI_SERIAL_ASSERT(video_output);
634 if (vo->vo_levels == NULL)
636 for (i = 0; i < vo->vo_numlevels; i++)
637 if (vo->vo_levels[i] == level)
643 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
645 struct acpi_video_output *vo;
646 int i, j, level, new_level;
649 ACPI_SERIAL_BEGIN(video_output);
650 if (vo->handle != handle)
654 case VID_NOTIFY_CYCLE_BRN:
655 if (vo->vo_numlevels <= 3)
658 case VID_NOTIFY_INC_BRN:
659 case VID_NOTIFY_DEC_BRN:
660 case VID_NOTIFY_ZERO_BRN:
661 if (vo->vo_levels == NULL)
663 level = vo_get_brightness(handle);
668 printf("unknown notify event 0x%x from %s\n",
669 notify, acpi_name(handle));
675 case VID_NOTIFY_CYCLE_BRN:
676 for (i = 2; i < vo->vo_numlevels; i++)
677 if (vo->vo_levels[i] == level) {
678 new_level = vo->vo_numlevels > i + 1 ?
679 vo->vo_levels[i + 1] : vo->vo_levels[2];
683 case VID_NOTIFY_INC_BRN:
684 case VID_NOTIFY_DEC_BRN:
685 for (i = 0; i < vo->vo_numlevels; i++) {
686 j = vo->vo_levels[i];
687 if (notify == VID_NOTIFY_INC_BRN) {
689 (j < new_level || level == new_level))
693 (j > new_level || level == new_level))
698 case VID_NOTIFY_ZERO_BRN:
699 for (i = 0; i < vo->vo_numlevels; i++)
700 if (vo->vo_levels[i] == 0) {
706 if (new_level != level) {
707 vo_set_brightness(handle, new_level);
708 vo->vo_brightness = new_level;
712 ACPI_SERIAL_END(video_output);
717 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
719 struct acpi_video_output *vo;
722 vo = (struct acpi_video_output *)arg1;
723 if (vo->handle == NULL)
725 ACPI_SERIAL_BEGIN(video_output);
726 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
727 err = sysctl_handle_int(oidp, &state, 0, req);
728 if (err != 0 || req->newptr == NULL)
730 vo_set_device_state(vo->handle,
731 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
733 ACPI_SERIAL_END(video_output);
739 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
741 struct acpi_video_output *vo;
742 int level, preset, err;
744 vo = (struct acpi_video_output *)arg1;
745 ACPI_SERIAL_BEGIN(video_output);
746 if (vo->handle == NULL) {
750 if (vo->vo_levels == NULL) {
755 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
756 vo->vo_economy : vo->vo_fullpower;
757 level = vo->vo_brightness;
761 err = sysctl_handle_int(oidp, &level, 0, req);
762 if (err != 0 || req->newptr == NULL)
764 if (level < -1 || level > 100) {
769 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
771 vo->vo_brightness = level;
772 vo_set_brightness(vo->handle, (level == -1) ? preset : level);
775 ACPI_SERIAL_END(video_output);
780 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
782 struct acpi_video_output *vo;
783 int i, 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) {
796 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
797 &vo->vo_economy : &vo->vo_fullpower;
799 err = sysctl_handle_int(oidp, &level, 0, req);
800 if (err != 0 || req->newptr == NULL)
802 if (level < -1 || level > 100) {
807 i = (arg2 == POWER_PROFILE_ECONOMY) ?
808 BCL_ECONOMY : BCL_FULLPOWER;
809 level = vo->vo_levels[i];
810 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
813 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
814 vo_set_brightness(vo->handle, level);
818 ACPI_SERIAL_END(video_output);
824 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
826 struct acpi_video_output *vo;
829 vo = (struct acpi_video_output *)arg1;
830 ACPI_SERIAL_BEGIN(video_output);
831 if (vo->vo_levels == NULL) {
835 if (req->newptr != NULL) {
839 err = sysctl_handle_opaque(oidp, vo->vo_levels,
840 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
843 ACPI_SERIAL_END(video_output);
848 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
852 status = acpi_SetInteger(handle, "_DOS", policy);
853 if (ACPI_FAILURE(status))
854 printf("can't evaluate %s._DOS - %s\n",
855 acpi_name(handle), AcpiFormatException(status));
858 struct enum_callback_arg {
859 void (*callback)(ACPI_HANDLE, UINT32, void *);
861 ACPI_OBJECT *dod_pkg;
866 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
867 void *context, void **retp __unused)
871 struct enum_callback_arg *argset;
874 ACPI_SERIAL_ASSERT(video);
876 status = acpi_GetInteger(handle, "_ADR", &adr);
877 if (ACPI_FAILURE(status))
880 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
881 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
882 (val & DOD_DEVID_MASK_FULL) == adr) {
883 argset->callback(handle, val, argset->context);
892 vid_enum_outputs(ACPI_HANDLE handle,
893 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
898 struct enum_callback_arg argset;
900 ACPI_SERIAL_ASSERT(video);
901 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
902 dod_buf.Pointer = NULL;
903 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
904 if (ACPI_FAILURE(status)) {
905 if (status != AE_NOT_FOUND)
906 printf("can't evaluate %s._DOD - %s\n",
907 acpi_name(handle), AcpiFormatException(status));
911 res = (ACPI_OBJECT *)dod_buf.Pointer;
912 if (!ACPI_PKG_VALID(res, 1)) {
913 printf("evaluation of %s._DOD makes no sense\n",
918 if (callback == NULL) {
919 argset.count = res->Package.Count;
922 argset.callback = callback;
923 argset.context = context;
924 argset.dod_pkg = res;
926 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
927 vid_enum_outputs_subr, NULL, &argset, NULL);
928 if (ACPI_FAILURE(status))
929 printf("failed walking down %s - %s\n",
930 acpi_name(handle), AcpiFormatException(status));
932 if (dod_buf.Pointer != NULL)
933 AcpiOsFree(dod_buf.Pointer);
934 return (argset.count);
938 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
943 int num, i, n, *levels;
946 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
947 bcl_buf.Pointer = NULL;
948 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
949 if (ACPI_FAILURE(status)) {
950 if (status != AE_NOT_FOUND)
951 printf("can't evaluate %s._BCL - %s\n",
952 acpi_name(handle), AcpiFormatException(status));
956 res = (ACPI_OBJECT *)bcl_buf.Pointer;
957 if (!ACPI_PKG_VALID(res, 2)) {
958 printf("evaluation of %s._BCL makes no sense\n",
963 num = res->Package.Count;
966 levels = AcpiOsAllocate(num * sizeof(*levels));
967 if (levels == NULL) {
971 for (i = 0, n = 0; i < num; i++)
972 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
982 if (bcl_buf.Pointer != NULL)
983 AcpiOsFree(bcl_buf.Pointer);
989 vo_get_brightness(ACPI_HANDLE handle)
994 ACPI_SERIAL_ASSERT(video_output);
995 status = acpi_GetInteger(handle, "_BQC", &level);
996 if (ACPI_FAILURE(status)) {
997 printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
998 AcpiFormatException(status));
1008 vo_set_brightness(ACPI_HANDLE handle, int level)
1012 ACPI_SERIAL_ASSERT(video_output);
1013 status = acpi_SetInteger(handle, "_BCM", level);
1014 if (ACPI_FAILURE(status))
1015 printf("can't evaluate %s._BCM - %s\n",
1016 acpi_name(handle), AcpiFormatException(status));
1020 vo_get_device_status(ACPI_HANDLE handle)
1025 ACPI_SERIAL_ASSERT(video_output);
1027 status = acpi_GetInteger(handle, "_DCS", &dcs);
1028 if (ACPI_FAILURE(status))
1029 printf("can't evaluate %s._DCS - %s\n",
1030 acpi_name(handle), AcpiFormatException(status));
1036 vo_get_graphics_state(ACPI_HANDLE handle)
1042 status = acpi_GetInteger(handle, "_DGS", &dgs);
1043 if (ACPI_FAILURE(status))
1044 printf("can't evaluate %s._DGS - %s\n",
1045 acpi_name(handle), AcpiFormatException(status));
1051 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1055 ACPI_SERIAL_ASSERT(video_output);
1056 status = acpi_SetInteger(handle, "_DSS", state);
1057 if (ACPI_FAILURE(status))
1058 printf("can't evaluate %s._DSS - %s\n",
1059 acpi_name(handle), AcpiFormatException(status));