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 * $FreeBSD: head/sys/dev/acpica/acpi_video.c 258780 2013-11-30 22:17:27Z eadler $
27 * $Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
35 #include <sys/power.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
42 /* ACPI video extension driver. */
43 struct acpi_video_output {
46 STAILQ_ENTRY(acpi_video_output) vo_next;
49 STAILQ_ENTRY(acpi_video_output) next;
56 struct sysctl_ctx_list vo_sysctl_ctx;
57 struct sysctl_oid *vo_sysctl_tree;
60 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
62 struct acpi_video_softc {
65 struct acpi_video_output_queue vid_outputs;
66 eventhandler_tag vid_pwr_evh;
70 static int acpi_video_modevent(struct module*, int, void *);
71 static void acpi_video_identify(driver_t *driver, device_t parent);
72 static int acpi_video_probe(device_t);
73 static int acpi_video_attach(device_t);
74 static int acpi_video_detach(device_t);
75 static int acpi_video_resume(device_t);
76 static int acpi_video_shutdown(device_t);
77 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
78 static void acpi_video_power_profile(void *);
79 static void acpi_video_bind_outputs(struct acpi_video_softc *);
80 static struct acpi_video_output *acpi_video_vo_init(UINT32);
81 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
82 static void acpi_video_vo_destroy(struct acpi_video_output *);
83 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
84 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
85 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
86 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
87 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
88 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
91 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
92 static int vid_enum_outputs(ACPI_HANDLE,
93 void(*)(ACPI_HANDLE, UINT32, void *), void *);
94 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
95 static int vo_get_brightness(ACPI_HANDLE);
96 static void vo_set_brightness(ACPI_HANDLE, int);
97 static UINT32 vo_get_device_status(ACPI_HANDLE);
98 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
99 static void vo_set_device_state(ACPI_HANDLE, UINT32);
102 #define VID_NOTIFY_SWITCHED 0x80
103 #define VID_NOTIFY_REPROBE 0x81
104 #define VID_NOTIFY_CYCLE_BRN 0x85
105 #define VID_NOTIFY_INC_BRN 0x86
106 #define VID_NOTIFY_DEC_BRN 0x87
107 #define VID_NOTIFY_ZERO_BRN 0x88
109 /* _DOS (Enable/Disable Output Switching) argument bits */
110 #define DOS_SWITCH_MASK 3
111 #define DOS_SWITCH_BY_OSPM 0
112 #define DOS_SWITCH_BY_BIOS 1
113 #define DOS_SWITCH_LOCKED 2
114 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
116 /* _DOD and subdev's _ADR */
117 #define DOD_DEVID_MASK 0x0f00
118 #define DOD_DEVID_MASK_FULL 0xffff
119 #define DOD_DEVID_MASK_DISPIDX 0x000f
120 #define DOD_DEVID_MASK_DISPPORT 0x00f0
121 #define DOD_DEVID_MONITOR 0x0100
122 #define DOD_DEVID_LCD 0x0110
123 #define DOD_DEVID_TV 0x0200
124 #define DOD_DEVID_EXT 0x0300
125 #define DOD_DEVID_INTDFP 0x0400
126 #define DOD_BIOS (1 << 16)
127 #define DOD_NONVGA (1 << 17)
128 #define DOD_HEAD_ID_SHIFT 18
129 #define DOD_HEAD_ID_BITS 3
130 #define DOD_HEAD_ID_MASK \
131 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
132 #define DOD_DEVID_SCHEME_STD (1 << 31)
134 /* _BCL related constants */
135 #define BCL_FULLPOWER 0
136 #define BCL_ECONOMY 1
138 /* _DCS (Device Currrent Status) value bits and masks. */
139 #define DCS_EXISTS (1 << 0)
140 #define DCS_ACTIVE (1 << 1)
141 #define DCS_READY (1 << 2)
142 #define DCS_FUNCTIONAL (1 << 3)
143 #define DCS_ATTACHED (1 << 4)
145 /* _DSS (Device Set Status) argument bits and masks. */
146 #define DSS_INACTIVE 0
147 #define DSS_ACTIVE (1 << 0)
148 #define DSS_SETNEXT (1 << 30)
149 #define DSS_COMMIT (1 << 31)
151 static device_method_t acpi_video_methods[] = {
152 DEVMETHOD(device_identify, acpi_video_identify),
153 DEVMETHOD(device_probe, acpi_video_probe),
154 DEVMETHOD(device_attach, acpi_video_attach),
155 DEVMETHOD(device_detach, acpi_video_detach),
156 DEVMETHOD(device_resume, acpi_video_resume),
157 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 static 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_MUTABLE(vo, &sc->vid_outputs, vo_next, vn) {
300 acpi_video_vo_destroy(vo);
302 ACPI_SERIAL_END(video);
308 acpi_video_resume(device_t dev)
310 struct acpi_video_softc *sc;
311 struct acpi_video_output *vo, *vn;
314 sc = device_get_softc(dev);
316 /* Restore brightness level */
317 ACPI_SERIAL_BEGIN(video);
318 ACPI_SERIAL_BEGIN(video_output);
319 STAILQ_FOREACH_MUTABLE(vo, &sc->vid_outputs, vo_next, vn) {
320 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
321 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
324 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
327 level = vo_get_brightness(vo->handle);
329 vo_set_brightness(vo->handle, level);
331 ACPI_SERIAL_END(video_output);
332 ACPI_SERIAL_END(video);
338 acpi_video_shutdown(device_t dev)
340 struct acpi_video_softc *sc;
342 sc = device_get_softc(dev);
343 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
349 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
351 struct acpi_video_softc *sc;
352 struct acpi_video_output *vo, *vo_tmp;
353 ACPI_HANDLE lasthand;
354 UINT32 dcs, dss, dss_p;
356 sc = (struct acpi_video_softc *)context;
359 case VID_NOTIFY_SWITCHED:
362 ACPI_SERIAL_BEGIN(video);
363 ACPI_SERIAL_BEGIN(video_output);
364 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
365 dss = vo_get_graphics_state(vo->handle);
366 dcs = vo_get_device_status(vo->handle);
367 if (!(dcs & DCS_READY))
369 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
370 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
371 if (lasthand != NULL)
372 vo_set_device_state(lasthand, dss_p);
374 lasthand = vo->handle;
377 if (lasthand != NULL)
378 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
379 ACPI_SERIAL_END(video_output);
380 ACPI_SERIAL_END(video);
382 case VID_NOTIFY_REPROBE:
383 ACPI_SERIAL_BEGIN(video);
384 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
386 acpi_video_bind_outputs(sc);
387 STAILQ_FOREACH_MUTABLE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
388 if (vo->handle == NULL) {
389 STAILQ_REMOVE(&sc->vid_outputs, vo,
390 acpi_video_output, vo_next);
391 acpi_video_vo_destroy(vo);
394 ACPI_SERIAL_END(video);
397 device_printf(sc->device, "unknown notify event 0x%x\n",
403 acpi_video_power_profile(void *context)
406 struct acpi_video_softc *sc;
407 struct acpi_video_output *vo;
410 state = power_profile_get_state();
411 if (state != POWER_PROFILE_PERFORMANCE &&
412 state != POWER_PROFILE_ECONOMY)
415 ACPI_SERIAL_BEGIN(video);
416 ACPI_SERIAL_BEGIN(video_output);
417 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
418 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
419 vo_set_brightness(vo->handle,
420 state == POWER_PROFILE_ECONOMY ?
421 vo->vo_economy : vo->vo_fullpower);
423 ACPI_SERIAL_END(video_output);
424 ACPI_SERIAL_END(video);
428 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
430 struct acpi_video_softc *sc;
431 struct acpi_video_output *vo;
433 ACPI_SERIAL_ASSERT(video);
436 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
437 if (vo->adr == adr) {
438 acpi_video_vo_bind(vo, handle);
442 vo = acpi_video_vo_init(adr);
444 acpi_video_vo_bind(vo, handle);
445 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
450 acpi_video_bind_outputs(struct acpi_video_softc *sc)
452 ACPI_SERIAL_ASSERT(video);
453 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
456 static struct acpi_video_output *
457 acpi_video_vo_init(UINT32 adr)
459 struct acpi_video_output *vn, *vo, *vp;
461 char name[8], env[32];
462 const char *type, *desc;
463 struct acpi_video_output_queue *voqh;
465 ACPI_SERIAL_ASSERT(video);
467 switch (adr & DOD_DEVID_MASK) {
468 case DOD_DEVID_MONITOR:
469 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
470 /* DOD_DEVID_LCD is a common, backward compatible ID */
471 desc = "Internal/Integrated Digital Flat Panel";
475 desc = "VGA CRT or VESA Compatible Analog Monitor";
481 desc = "TV/HDTV or Analog-Video Monitor";
486 desc = "External Digital Monitor";
490 case DOD_DEVID_INTDFP:
491 desc = "Internal/Integrated Digital Flat Panel";
496 desc = "unknown output";
503 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
504 if (vn->vo_unit.num != n)
510 ksnprintf(name, sizeof(name), "%s%d", type, n);
512 vo = kmalloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
517 vo->vo_brightness = -1;
518 vo->vo_fullpower = -1; /* TODO: override with tunables */
520 vo->vo_numlevels = 0;
521 vo->vo_levels = NULL;
522 ksnprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
523 if (kgetenv_int(env, &x))
524 vo->vo_fullpower = x;
525 ksnprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
526 if (kgetenv_int(env, &x))
529 sysctl_ctx_init(&vo->vo_sysctl_ctx);
531 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
533 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
534 if (acpi_video_sysctl_tree != NULL)
536 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
537 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
538 OID_AUTO, name, CTLFLAG_RD, 0, desc);
539 if (vo->vo_sysctl_tree != NULL) {
540 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
541 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
543 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
544 acpi_video_vo_active_sysctl, "I",
545 "current activity of this device");
546 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
547 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
548 OID_AUTO, "brightness",
549 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
550 acpi_video_vo_bright_sysctl, "I",
551 "current brightness level");
552 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
553 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
554 OID_AUTO, "fullpower",
555 CTLTYPE_INT|CTLFLAG_RW, vo,
556 POWER_PROFILE_PERFORMANCE,
557 acpi_video_vo_presets_sysctl, "I",
558 "preset level for full power mode");
559 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
560 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
562 CTLTYPE_INT|CTLFLAG_RW, vo,
563 POWER_PROFILE_ECONOMY,
564 acpi_video_vo_presets_sysctl, "I",
565 "preset level for economy mode");
566 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
567 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
569 CTLTYPE_INT|CTLFLAG_RD, vo, 0,
570 acpi_video_vo_levels_sysctl, "I",
571 "supported brightness levels");
573 kprintf("%s: sysctl node creation failed\n", type);
575 kprintf("%s: softc allocation failed\n", type);
578 kprintf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
579 kprintf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
580 kprintf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
582 kprintf(", detectable by BIOS");
583 if (adr & DOD_NONVGA)
584 kprintf(" (Non-VGA output device whose power "
585 "is related to the VGA device)");
586 kprintf(", head #%d\n",
587 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
593 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
595 ACPI_SERIAL_BEGIN(video_output);
596 if (vo->vo_levels != NULL)
597 AcpiOsFree(vo->vo_levels);
599 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
600 if (vo->vo_numlevels >= 2) {
601 if (vo->vo_fullpower == -1
602 || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
603 /* XXX - can't deal with rebinding... */
604 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
605 if (vo->vo_economy == -1
606 || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
607 /* XXX - see above. */
608 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
610 if (vo->vo_levels != NULL)
611 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
612 acpi_video_vo_notify_handler, vo);
613 ACPI_SERIAL_END(video_output);
617 acpi_video_vo_destroy(struct acpi_video_output *vo)
619 struct acpi_video_output_queue *voqh;
621 ACPI_SERIAL_ASSERT(video);
622 if (vo->vo_sysctl_tree != NULL) {
623 vo->vo_sysctl_tree = NULL;
624 sysctl_ctx_free(&vo->vo_sysctl_ctx);
626 if (vo->vo_levels != NULL) {
627 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
628 acpi_video_vo_notify_handler);
629 AcpiOsFree(vo->vo_levels);
632 switch (vo->adr & DOD_DEVID_MASK) {
633 case DOD_DEVID_MONITOR:
642 case DOD_DEVID_INTDFP:
648 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
649 kfree(vo, M_ACPIVIDEO);
653 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
657 ACPI_SERIAL_ASSERT(video_output);
658 if (vo->vo_levels == NULL)
660 for (i = 0; i < vo->vo_numlevels; i++)
661 if (vo->vo_levels[i] == level)
667 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
669 struct acpi_video_output *vo;
670 int i, j, level, new_level;
673 ACPI_SERIAL_BEGIN(video_output);
674 if (vo->handle != handle)
678 case VID_NOTIFY_CYCLE_BRN:
679 if (vo->vo_numlevels <= 3)
682 case VID_NOTIFY_INC_BRN:
683 case VID_NOTIFY_DEC_BRN:
684 case VID_NOTIFY_ZERO_BRN:
685 if (vo->vo_levels == NULL)
687 level = vo_get_brightness(handle);
692 kprintf("unknown notify event 0x%x from %s\n",
693 notify, acpi_name(handle));
699 case VID_NOTIFY_CYCLE_BRN:
700 for (i = 2; i < vo->vo_numlevels; i++)
701 if (vo->vo_levels[i] == level) {
702 new_level = vo->vo_numlevels > i + 1 ?
703 vo->vo_levels[i + 1] : vo->vo_levels[2];
707 case VID_NOTIFY_INC_BRN:
708 case VID_NOTIFY_DEC_BRN:
709 for (i = 0; i < vo->vo_numlevels; i++) {
710 j = vo->vo_levels[i];
711 if (notify == VID_NOTIFY_INC_BRN) {
713 (j < new_level || level == new_level))
717 (j > new_level || level == new_level))
722 case VID_NOTIFY_ZERO_BRN:
723 for (i = 0; i < vo->vo_numlevels; i++)
724 if (vo->vo_levels[i] == 0) {
730 if (new_level != level) {
731 vo_set_brightness(handle, new_level);
732 vo->vo_brightness = new_level;
736 ACPI_SERIAL_END(video_output);
741 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
743 struct acpi_video_output *vo;
746 vo = (struct acpi_video_output *)arg1;
747 if (vo->handle == NULL)
749 ACPI_SERIAL_BEGIN(video_output);
750 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
751 err = sysctl_handle_int(oidp, &state, 0, req);
752 if (err != 0 || req->newptr == NULL)
754 vo_set_device_state(vo->handle,
755 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
757 ACPI_SERIAL_END(video_output);
763 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
765 struct acpi_video_output *vo;
766 int level, preset, err;
768 vo = (struct acpi_video_output *)arg1;
769 ACPI_SERIAL_BEGIN(video_output);
770 if (vo->handle == NULL) {
774 if (vo->vo_levels == NULL) {
779 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
780 vo->vo_economy : vo->vo_fullpower;
781 level = vo->vo_brightness;
785 err = sysctl_handle_int(oidp, &level, 0, req);
786 if (err != 0 || req->newptr == NULL)
788 if (level < -1 || level > 100) {
793 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
795 vo->vo_brightness = level;
796 vo_set_brightness(vo->handle, (level == -1) ? preset : level);
799 ACPI_SERIAL_END(video_output);
804 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
806 struct acpi_video_output *vo;
807 int i, level, *preset, err;
809 vo = (struct acpi_video_output *)arg1;
810 ACPI_SERIAL_BEGIN(video_output);
811 if (vo->handle == NULL) {
815 if (vo->vo_levels == NULL) {
819 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
820 &vo->vo_economy : &vo->vo_fullpower;
822 err = sysctl_handle_int(oidp, &level, 0, req);
823 if (err != 0 || req->newptr == NULL)
825 if (level < -1 || level > 100) {
830 i = (arg2 == POWER_PROFILE_ECONOMY) ?
831 BCL_ECONOMY : BCL_FULLPOWER;
832 level = vo->vo_levels[i];
833 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
836 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
837 vo_set_brightness(vo->handle, level);
841 ACPI_SERIAL_END(video_output);
847 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
849 struct acpi_video_output *vo;
852 vo = (struct acpi_video_output *)arg1;
853 ACPI_SERIAL_BEGIN(video_output);
854 if (vo->vo_levels == NULL) {
858 if (req->newptr != NULL) {
862 err = sysctl_handle_opaque(oidp, vo->vo_levels,
863 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
866 ACPI_SERIAL_END(video_output);
871 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
875 status = acpi_SetInteger(handle, "_DOS", policy);
876 if (ACPI_FAILURE(status))
877 kprintf("can't evaluate %s._DOS - %s\n",
878 acpi_name(handle), AcpiFormatException(status));
881 struct enum_callback_arg {
882 void (*callback)(ACPI_HANDLE, UINT32, void *);
884 ACPI_OBJECT *dod_pkg;
889 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
890 void *context, void **retp __unused)
894 struct enum_callback_arg *argset;
897 ACPI_SERIAL_ASSERT(video);
899 status = acpi_GetInteger(handle, "_ADR", &adr);
900 if (ACPI_FAILURE(status))
903 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
904 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
905 (val & DOD_DEVID_MASK_FULL) ==
906 (adr & DOD_DEVID_MASK_FULL)) {
907 argset->callback(handle, val, argset->context);
916 vid_enum_outputs(ACPI_HANDLE handle,
917 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
922 struct enum_callback_arg argset;
924 ACPI_SERIAL_ASSERT(video);
925 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
926 dod_buf.Pointer = NULL;
927 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
928 if (ACPI_FAILURE(status)) {
929 if (status != AE_NOT_FOUND)
930 kprintf("can't evaluate %s._DOD - %s\n",
931 acpi_name(handle), AcpiFormatException(status));
935 res = (ACPI_OBJECT *)dod_buf.Pointer;
936 if (!ACPI_PKG_VALID(res, 1)) {
937 kprintf("evaluation of %s._DOD makes no sense\n",
942 if (callback == NULL) {
943 argset.count = res->Package.Count;
946 argset.callback = callback;
947 argset.context = context;
948 argset.dod_pkg = res;
950 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
951 vid_enum_outputs_subr, NULL, &argset, NULL);
952 if (ACPI_FAILURE(status))
953 kprintf("failed walking down %s - %s\n",
954 acpi_name(handle), AcpiFormatException(status));
956 if (dod_buf.Pointer != NULL)
957 AcpiOsFree(dod_buf.Pointer);
958 return (argset.count);
962 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
967 int num, i, n, *levels;
969 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
970 bcl_buf.Pointer = NULL;
971 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
972 if (ACPI_FAILURE(status)) {
973 if (status != AE_NOT_FOUND)
974 kprintf("can't evaluate %s._BCL - %s\n",
975 acpi_name(handle), AcpiFormatException(status));
978 res = (ACPI_OBJECT *)bcl_buf.Pointer;
979 if (!ACPI_PKG_VALID(res, 2)) {
980 kprintf("evaluation of %s._BCL makes no sense\n",
984 num = res->Package.Count;
985 if (num < 2 || levelp == NULL)
987 levels = AcpiOsAllocate(num * sizeof(*levels));
990 for (i = 0, n = 0; i < num; i++)
991 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1001 if (bcl_buf.Pointer != NULL)
1002 AcpiOsFree(bcl_buf.Pointer);
1007 vo_get_brightness(ACPI_HANDLE handle)
1012 ACPI_SERIAL_ASSERT(video_output);
1013 status = acpi_GetInteger(handle, "_BQC", &level);
1014 if (ACPI_FAILURE(status)) {
1015 kprintf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
1016 AcpiFormatException(status));
1026 vo_set_brightness(ACPI_HANDLE handle, int level)
1030 ACPI_SERIAL_ASSERT(video_output);
1031 status = acpi_SetInteger(handle, "_BCM", level);
1032 if (ACPI_FAILURE(status))
1033 kprintf("can't evaluate %s._BCM - %s\n",
1034 acpi_name(handle), AcpiFormatException(status));
1038 vo_get_device_status(ACPI_HANDLE handle)
1043 ACPI_SERIAL_ASSERT(video_output);
1045 status = acpi_GetInteger(handle, "_DCS", &dcs);
1046 if (ACPI_FAILURE(status))
1047 kprintf("can't evaluate %s._DCS - %s\n",
1048 acpi_name(handle), AcpiFormatException(status));
1054 vo_get_graphics_state(ACPI_HANDLE handle)
1060 status = acpi_GetInteger(handle, "_DGS", &dgs);
1061 if (ACPI_FAILURE(status))
1062 kprintf("can't evaluate %s._DGS - %s\n",
1063 acpi_name(handle), AcpiFormatException(status));
1069 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1073 ACPI_SERIAL_ASSERT(video_output);
1074 status = acpi_SetInteger(handle, "_DSS", state);
1075 if (ACPI_FAILURE(status))
1076 kprintf("can't evaluate %s._DSS - %s\n",
1077 acpi_name(handle), AcpiFormatException(status));