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 "opt_evdev.h"
34 #include <sys/param.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/power.h>
41 #include <sys/queue.h>
42 #include <sys/sysctl.h>
44 #include <contrib/dev/acpica/include/acpi.h>
46 #include <dev/acpica/acpivar.h>
49 #include <dev/evdev/input.h>
50 #include <dev/evdev/evdev.h>
53 /* ACPI video extension driver. */
54 struct acpi_video_output {
57 STAILQ_ENTRY(acpi_video_output) vo_next;
60 STAILQ_ENTRY(acpi_video_output) next;
62 int vo_hasbqc; /* Query method is present. */
63 int vo_level; /* Cached level when !vo_hasbqc. */
69 struct sysctl_ctx_list vo_sysctl_ctx;
70 struct sysctl_oid *vo_sysctl_tree;
72 struct evdev_dev *evdev;
76 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
78 struct acpi_video_softc {
81 struct acpi_video_output_queue vid_outputs;
82 eventhandler_tag vid_pwr_evh;
84 struct evdev_dev *evdev;
89 static int acpi_video_modevent(struct module*, int, void *);
90 static void acpi_video_identify(driver_t *driver, device_t parent);
91 static int acpi_video_probe(device_t);
92 static int acpi_video_attach(device_t);
93 static int acpi_video_detach(device_t);
94 static int acpi_video_resume(device_t);
95 static int acpi_video_shutdown(device_t);
96 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
97 static void acpi_video_power_profile(void *);
98 static void acpi_video_bind_outputs(struct acpi_video_softc *);
99 static struct acpi_video_output *acpi_video_vo_init(UINT32);
100 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
101 static void acpi_video_vo_destroy(struct acpi_video_output *);
102 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
103 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
104 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
105 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
106 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
107 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
110 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
111 static int vid_enum_outputs(ACPI_HANDLE,
112 void(*)(ACPI_HANDLE, UINT32, void *), void *);
113 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
114 static int vo_get_brightness(struct acpi_video_output *);
115 static void vo_set_brightness(struct acpi_video_output *, int);
116 static UINT32 vo_get_device_status(ACPI_HANDLE);
117 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
118 static void vo_set_device_state(ACPI_HANDLE, UINT32);
121 #define VID_NOTIFY_SWITCHED 0x80
122 #define VID_NOTIFY_REPROBE 0x81
123 #define VID_NOTIFY_CYCLE_OUT 0x82
124 #define VID_NOTIFY_NEXT_OUT 0x83
125 #define VID_NOTIFY_PREV_OUT 0x84
126 #define VID_NOTIFY_CYCLE_BRN 0x85
127 #define VID_NOTIFY_INC_BRN 0x86
128 #define VID_NOTIFY_DEC_BRN 0x87
129 #define VID_NOTIFY_ZERO_BRN 0x88
130 #define VID_NOTIFY_DISP_OFF 0x89
132 /* _DOS (Enable/Disable Output Switching) argument bits */
133 #define DOS_SWITCH_MASK 3
134 #define DOS_SWITCH_BY_OSPM 0
135 #define DOS_SWITCH_BY_BIOS 1
136 #define DOS_SWITCH_LOCKED 2
137 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
139 /* _DOD and subdev's _ADR */
140 #define DOD_DEVID_MASK 0x0f00
141 #define DOD_DEVID_MASK_FULL 0xffff
142 #define DOD_DEVID_MASK_DISPIDX 0x000f
143 #define DOD_DEVID_MASK_DISPPORT 0x00f0
144 #define DOD_DEVID_MONITOR 0x0100
145 #define DOD_DEVID_LCD 0x0110
146 #define DOD_DEVID_TV 0x0200
147 #define DOD_DEVID_EXT 0x0300
148 #define DOD_DEVID_INTDFP 0x0400
149 #define DOD_BIOS (1 << 16)
150 #define DOD_NONVGA (1 << 17)
151 #define DOD_HEAD_ID_SHIFT 18
152 #define DOD_HEAD_ID_BITS 3
153 #define DOD_HEAD_ID_MASK \
154 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
155 #define DOD_DEVID_SCHEME_STD (1U << 31)
157 /* _BCL related constants */
158 #define BCL_FULLPOWER 0
159 #define BCL_ECONOMY 1
161 /* _DCS (Device Currrent Status) value bits and masks. */
162 #define DCS_EXISTS (1 << 0)
163 #define DCS_ACTIVE (1 << 1)
164 #define DCS_READY (1 << 2)
165 #define DCS_FUNCTIONAL (1 << 3)
166 #define DCS_ATTACHED (1 << 4)
168 /* _DSS (Device Set Status) argument bits and masks. */
169 #define DSS_INACTIVE 0
170 #define DSS_ACTIVE (1 << 0)
171 #define DSS_SETNEXT (1 << 30)
172 #define DSS_COMMIT (1U << 31)
174 static device_method_t acpi_video_methods[] = {
175 DEVMETHOD(device_identify, acpi_video_identify),
176 DEVMETHOD(device_probe, acpi_video_probe),
177 DEVMETHOD(device_attach, acpi_video_attach),
178 DEVMETHOD(device_detach, acpi_video_detach),
179 DEVMETHOD(device_resume, acpi_video_resume),
180 DEVMETHOD(device_shutdown, acpi_video_shutdown),
184 static driver_t acpi_video_driver = {
187 sizeof(struct acpi_video_softc),
190 static devclass_t acpi_video_devclass;
192 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
193 acpi_video_modevent, NULL);
194 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
196 MODULE_DEPEND(acpi_video, evdev, 1, 1, 1);
199 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
200 static struct sysctl_oid *acpi_video_sysctl_tree;
201 static struct acpi_video_output_queue crt_units, tv_units,
202 ext_units, lcd_units, other_units;
205 * The 'video' lock protects the hierarchy of video output devices
206 * (the video "bus"). The 'video_output' lock protects per-output
207 * data is equivalent to a softc lock for each video output.
209 ACPI_SERIAL_DECL(video, "ACPI video");
210 ACPI_SERIAL_DECL(video_output, "ACPI video output");
211 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
214 static const struct {
217 } acpi_video_evdev_map[] = {
218 { VID_NOTIFY_SWITCHED, KEY_SWITCHVIDEOMODE },
219 { VID_NOTIFY_REPROBE, KEY_SWITCHVIDEOMODE },
220 { VID_NOTIFY_CYCLE_OUT, KEY_SWITCHVIDEOMODE },
221 { VID_NOTIFY_NEXT_OUT, KEY_VIDEO_NEXT },
222 { VID_NOTIFY_PREV_OUT, KEY_VIDEO_PREV },
223 { VID_NOTIFY_CYCLE_BRN, KEY_BRIGHTNESS_CYCLE },
224 { VID_NOTIFY_INC_BRN, KEY_BRIGHTNESSUP },
225 { VID_NOTIFY_DEC_BRN, KEY_BRIGHTNESSDOWN },
226 { VID_NOTIFY_ZERO_BRN, KEY_BRIGHTNESS_ZERO },
227 { VID_NOTIFY_DISP_OFF, KEY_DISPLAY_OFF },
231 acpi_video_push_evdev_event(struct evdev_dev *evdev, UINT32 notify)
236 /* Do not allow to execute 2 instances this routine concurently */
237 ACPI_SERIAL_ASSERT(video_output);
239 for (i = 0; i < nitems(acpi_video_evdev_map); i++) {
240 if (acpi_video_evdev_map[i].notify == notify) {
241 key = acpi_video_evdev_map[i].key;
242 evdev_push_key(evdev, key, 1);
244 evdev_push_key(evdev, key, 0);
253 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
260 sysctl_ctx_init(&acpi_video_sysctl_ctx);
261 STAILQ_INIT(&crt_units);
262 STAILQ_INIT(&tv_units);
263 STAILQ_INIT(&ext_units);
264 STAILQ_INIT(&lcd_units);
265 STAILQ_INIT(&other_units);
268 sysctl_ctx_free(&acpi_video_sysctl_ctx);
269 acpi_video_sysctl_tree = NULL;
279 acpi_video_identify(driver_t *driver, device_t parent)
282 if (device_find_child(parent, "acpi_video", -1) == NULL)
283 device_add_child(parent, "acpi_video", -1);
287 acpi_video_probe(device_t dev)
290 ACPI_OBJECT_TYPE t_dos;
292 devh = acpi_get_handle(dev);
293 if (acpi_disabled("video") ||
294 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
295 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
296 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
297 t_dos != ACPI_TYPE_METHOD)
300 device_set_desc(dev, "ACPI video extension");
305 acpi_video_attach(device_t dev)
307 struct acpi_softc *acpi_sc;
308 struct acpi_video_softc *sc;
313 sc = device_get_softc(dev);
315 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
320 sc->evdev = evdev_alloc();
321 evdev_set_name(sc->evdev, device_get_desc(dev));
322 evdev_set_phys(sc->evdev, device_get_nameunit(dev));
323 evdev_set_id(sc->evdev, BUS_HOST, 0, 0, 1);
324 evdev_support_event(sc->evdev, EV_SYN);
325 evdev_support_event(sc->evdev, EV_KEY);
326 for (i = 0; i < nitems(acpi_video_evdev_map); i++)
327 evdev_support_key(sc->evdev, acpi_video_evdev_map[i].key);
329 if (evdev_register(sc->evdev) != 0)
333 ACPI_SERIAL_BEGIN(video);
334 if (acpi_video_sysctl_tree == NULL) {
335 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
336 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
337 "video", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
338 "video extension control");
340 ACPI_SERIAL_END(video);
343 sc->handle = acpi_get_handle(dev);
344 STAILQ_INIT(&sc->vid_outputs);
346 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
347 acpi_video_notify_handler, sc);
348 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
349 acpi_video_power_profile, sc, 0);
351 ACPI_SERIAL_BEGIN(video);
352 acpi_video_bind_outputs(sc);
353 ACPI_SERIAL_END(video);
356 * Notify the BIOS that we want to switch both active outputs and
359 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
360 DOS_BRIGHTNESS_BY_OSPM);
362 acpi_video_power_profile(sc);
368 acpi_video_detach(device_t dev)
370 struct acpi_video_softc *sc;
371 struct acpi_video_output *vo, *vn;
373 sc = device_get_softc(dev);
375 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
376 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
377 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
378 acpi_video_notify_handler);
380 ACPI_SERIAL_BEGIN(video);
381 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
382 acpi_video_vo_destroy(vo);
384 ACPI_SERIAL_END(video);
387 evdev_free(sc->evdev);
394 acpi_video_resume(device_t dev)
396 struct acpi_video_softc *sc;
397 struct acpi_video_output *vo, *vn;
400 sc = device_get_softc(dev);
402 /* Restore brightness level */
403 ACPI_SERIAL_BEGIN(video);
404 ACPI_SERIAL_BEGIN(video_output);
405 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
406 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
407 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
410 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
413 level = vo_get_brightness(vo);
415 vo_set_brightness(vo, level);
417 ACPI_SERIAL_END(video_output);
418 ACPI_SERIAL_END(video);
424 acpi_video_shutdown(device_t dev)
426 struct acpi_video_softc *sc;
428 sc = device_get_softc(dev);
429 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
435 acpi_video_invoke_event_handler(void *context)
437 EVENTHANDLER_INVOKE(acpi_video_event, (int)(intptr_t)context);
441 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
443 struct acpi_video_softc *sc;
444 struct acpi_video_output *vo, *vo_tmp;
445 ACPI_HANDLE lasthand;
446 UINT32 dcs, dss, dss_p;
448 sc = (struct acpi_video_softc *)context;
451 case VID_NOTIFY_SWITCHED:
454 ACPI_SERIAL_BEGIN(video);
455 ACPI_SERIAL_BEGIN(video_output);
456 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
457 dss = vo_get_graphics_state(vo->handle);
458 dcs = vo_get_device_status(vo->handle);
459 if (!(dcs & DCS_READY))
461 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
462 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
463 if (lasthand != NULL)
464 vo_set_device_state(lasthand, dss_p);
466 lasthand = vo->handle;
469 if (lasthand != NULL)
470 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
471 ACPI_SERIAL_END(video_output);
472 ACPI_SERIAL_END(video);
474 case VID_NOTIFY_REPROBE:
475 ACPI_SERIAL_BEGIN(video);
476 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
478 acpi_video_bind_outputs(sc);
479 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
480 if (vo->handle == NULL) {
481 STAILQ_REMOVE(&sc->vid_outputs, vo,
482 acpi_video_output, vo_next);
483 acpi_video_vo_destroy(vo);
486 ACPI_SERIAL_END(video);
488 /* Next events should not appear if DOS_SWITCH_BY_OSPM policy is set */
489 case VID_NOTIFY_CYCLE_OUT:
490 case VID_NOTIFY_NEXT_OUT:
491 case VID_NOTIFY_PREV_OUT:
493 device_printf(sc->device, "unknown notify event 0x%x\n",
496 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_video_invoke_event_handler,
497 (void *)(uintptr_t)notify);
499 ACPI_SERIAL_BEGIN(video_output);
500 acpi_video_push_evdev_event(sc->evdev, notify);
501 ACPI_SERIAL_END(video_output);
506 acpi_video_power_profile(void *context)
509 struct acpi_video_softc *sc;
510 struct acpi_video_output *vo;
513 state = power_profile_get_state();
514 if (state != POWER_PROFILE_PERFORMANCE &&
515 state != POWER_PROFILE_ECONOMY)
518 ACPI_SERIAL_BEGIN(video);
519 ACPI_SERIAL_BEGIN(video_output);
520 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
521 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
522 vo_set_brightness(vo,
523 state == POWER_PROFILE_ECONOMY ?
524 vo->vo_economy : vo->vo_fullpower);
526 ACPI_SERIAL_END(video_output);
527 ACPI_SERIAL_END(video);
531 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
533 struct acpi_video_softc *sc;
534 struct acpi_video_output *vo;
536 ACPI_SERIAL_ASSERT(video);
539 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
540 if (vo->adr == adr) {
541 acpi_video_vo_bind(vo, handle);
545 vo = acpi_video_vo_init(adr);
548 vo->evdev = sc->evdev;
550 acpi_video_vo_bind(vo, handle);
551 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
556 acpi_video_bind_outputs(struct acpi_video_softc *sc)
559 ACPI_SERIAL_ASSERT(video);
560 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
563 static struct acpi_video_output *
564 acpi_video_vo_init(UINT32 adr)
566 struct acpi_video_output *vn, *vo, *vp;
568 char name[8], env[32];
569 const char *type, *desc;
570 struct acpi_video_output_queue *voqh;
572 ACPI_SERIAL_ASSERT(video);
574 switch (adr & DOD_DEVID_MASK) {
575 case DOD_DEVID_MONITOR:
576 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
577 /* DOD_DEVID_LCD is a common, backward compatible ID */
578 desc = "Internal/Integrated Digital Flat Panel";
582 desc = "VGA CRT or VESA Compatible Analog Monitor";
588 desc = "TV/HDTV or Analog-Video Monitor";
593 desc = "External Digital Monitor";
597 case DOD_DEVID_INTDFP:
598 desc = "Internal/Integrated Digital Flat Panel";
603 desc = "unknown output";
610 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
611 if (vn->vo_unit.num != n)
617 snprintf(name, sizeof(name), "%s%d", type, n);
619 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
626 vo->vo_brightness = -1;
627 vo->vo_fullpower = -1; /* TODO: override with tunables */
629 vo->vo_numlevels = 0;
630 vo->vo_levels = NULL;
631 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
632 if (getenv_int(env, &x))
633 vo->vo_fullpower = x;
634 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
635 if (getenv_int(env, &x))
638 sysctl_ctx_init(&vo->vo_sysctl_ctx);
640 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
642 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
643 if (acpi_video_sysctl_tree != NULL)
645 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
646 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
647 OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE,
649 if (vo->vo_sysctl_tree != NULL) {
650 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
651 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
653 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
654 0, acpi_video_vo_active_sysctl, "I",
655 "current activity of this device");
656 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
657 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
658 OID_AUTO, "brightness",
659 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
660 0, acpi_video_vo_bright_sysctl, "I",
661 "current brightness level");
662 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
663 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
664 OID_AUTO, "fullpower",
665 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
666 POWER_PROFILE_PERFORMANCE,
667 acpi_video_vo_presets_sysctl, "I",
668 "preset level for full power mode");
669 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
670 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
672 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
673 POWER_PROFILE_ECONOMY,
674 acpi_video_vo_presets_sysctl, "I",
675 "preset level for economy mode");
676 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
677 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
679 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, vo,
680 0, acpi_video_vo_levels_sysctl, "I",
681 "supported brightness levels");
683 printf("%s: sysctl node creation failed\n", type);
685 printf("%s: softc allocation failed\n", type);
688 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
689 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
690 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
692 printf(", detectable by BIOS");
693 if (adr & DOD_NONVGA)
694 printf(" (Non-VGA output device whose power "
695 "is related to the VGA device)");
696 printf(", head #%d\n",
697 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
703 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
706 ACPI_SERIAL_BEGIN(video_output);
707 if (vo->vo_levels != NULL) {
708 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
709 acpi_video_vo_notify_handler);
710 AcpiOsFree(vo->vo_levels);
711 vo->vo_levels = NULL;
714 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
715 if (vo->vo_numlevels >= 2) {
716 if (vo->vo_fullpower == -1 ||
717 acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) {
718 /* XXX - can't deal with rebinding... */
719 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
721 if (vo->vo_economy == -1 ||
722 acpi_video_vo_check_level(vo, vo->vo_economy) != 0) {
723 /* XXX - see above. */
724 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
726 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
727 acpi_video_vo_notify_handler, vo);
729 ACPI_SERIAL_END(video_output);
733 acpi_video_vo_destroy(struct acpi_video_output *vo)
735 struct acpi_video_output_queue *voqh;
737 ACPI_SERIAL_ASSERT(video);
738 if (vo->vo_sysctl_tree != NULL) {
739 vo->vo_sysctl_tree = NULL;
740 sysctl_ctx_free(&vo->vo_sysctl_ctx);
742 if (vo->vo_levels != NULL) {
743 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
744 acpi_video_vo_notify_handler);
745 AcpiOsFree(vo->vo_levels);
748 switch (vo->adr & DOD_DEVID_MASK) {
749 case DOD_DEVID_MONITOR:
750 if ((vo->adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD)
761 case DOD_DEVID_INTDFP:
767 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
768 free(vo, M_ACPIVIDEO);
772 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
776 ACPI_SERIAL_ASSERT(video_output);
777 if (vo->vo_levels == NULL)
779 for (i = 0; i < vo->vo_numlevels; i++)
780 if (vo->vo_levels[i] == level)
786 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
788 struct acpi_video_output *vo;
789 int i, j, level, new_level;
792 ACPI_SERIAL_BEGIN(video_output);
793 if (vo->handle != handle)
797 case VID_NOTIFY_CYCLE_BRN:
798 if (vo->vo_numlevels <= 3)
801 case VID_NOTIFY_INC_BRN:
802 case VID_NOTIFY_DEC_BRN:
803 case VID_NOTIFY_ZERO_BRN:
804 case VID_NOTIFY_DISP_OFF:
805 if (vo->vo_levels == NULL)
807 level = vo_get_brightness(vo);
812 printf("unknown notify event 0x%x from %s\n",
813 notify, acpi_name(handle));
819 case VID_NOTIFY_CYCLE_BRN:
820 for (i = 2; i < vo->vo_numlevels; i++)
821 if (vo->vo_levels[i] == level) {
822 new_level = vo->vo_numlevels > i + 1 ?
823 vo->vo_levels[i + 1] : vo->vo_levels[2];
827 case VID_NOTIFY_INC_BRN:
828 case VID_NOTIFY_DEC_BRN:
829 for (i = 0; i < vo->vo_numlevels; i++) {
830 j = vo->vo_levels[i];
831 if (notify == VID_NOTIFY_INC_BRN) {
833 (j < new_level || level == new_level))
837 (j > new_level || level == new_level))
842 case VID_NOTIFY_ZERO_BRN:
843 for (i = 0; i < vo->vo_numlevels; i++)
844 if (vo->vo_levels[i] == 0) {
849 case VID_NOTIFY_DISP_OFF:
850 acpi_pwr_switch_consumer(handle, ACPI_STATE_D3);
853 if (new_level != level) {
854 vo_set_brightness(vo, new_level);
855 vo->vo_brightness = new_level;
858 acpi_video_push_evdev_event(vo->evdev, notify);
862 ACPI_SERIAL_END(video_output);
864 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_video_invoke_event_handler,
865 (void *)(uintptr_t)notify);
870 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
872 struct acpi_video_output *vo;
875 vo = (struct acpi_video_output *)arg1;
876 if (vo->handle == NULL)
878 ACPI_SERIAL_BEGIN(video_output);
879 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
880 err = sysctl_handle_int(oidp, &state, 0, req);
881 if (err != 0 || req->newptr == NULL)
883 vo_set_device_state(vo->handle,
884 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
886 ACPI_SERIAL_END(video_output);
892 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
894 struct acpi_video_output *vo;
895 int level, preset, err;
897 vo = (struct acpi_video_output *)arg1;
898 ACPI_SERIAL_BEGIN(video_output);
899 if (vo->handle == NULL) {
903 if (vo->vo_levels == NULL) {
908 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
909 vo->vo_economy : vo->vo_fullpower;
910 level = vo->vo_brightness;
914 err = sysctl_handle_int(oidp, &level, 0, req);
915 if (err != 0 || req->newptr == NULL)
917 if (level < -1 || level > 100) {
922 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
924 vo->vo_brightness = level;
925 vo_set_brightness(vo, (level == -1) ? preset : level);
928 ACPI_SERIAL_END(video_output);
933 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
935 struct acpi_video_output *vo;
936 int i, level, *preset, err;
938 vo = (struct acpi_video_output *)arg1;
939 ACPI_SERIAL_BEGIN(video_output);
940 if (vo->handle == NULL) {
944 if (vo->vo_levels == NULL) {
948 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
949 &vo->vo_economy : &vo->vo_fullpower;
951 err = sysctl_handle_int(oidp, &level, 0, req);
952 if (err != 0 || req->newptr == NULL)
954 if (level < -1 || level > 100) {
959 i = (arg2 == POWER_PROFILE_ECONOMY) ?
960 BCL_ECONOMY : BCL_FULLPOWER;
961 level = vo->vo_levels[i];
962 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
965 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
966 vo_set_brightness(vo, level);
970 ACPI_SERIAL_END(video_output);
976 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
978 struct acpi_video_output *vo;
981 vo = (struct acpi_video_output *)arg1;
982 ACPI_SERIAL_BEGIN(video_output);
983 if (vo->vo_levels == NULL) {
987 if (req->newptr != NULL) {
991 err = sysctl_handle_opaque(oidp, vo->vo_levels,
992 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
995 ACPI_SERIAL_END(video_output);
1000 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
1004 status = acpi_SetInteger(handle, "_DOS", policy);
1005 if (ACPI_FAILURE(status))
1006 printf("can't evaluate %s._DOS - %s\n",
1007 acpi_name(handle), AcpiFormatException(status));
1010 struct enum_callback_arg {
1011 void (*callback)(ACPI_HANDLE, UINT32, void *);
1013 ACPI_OBJECT *dod_pkg;
1018 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
1019 void *context, void **retp __unused)
1023 struct enum_callback_arg *argset;
1026 ACPI_SERIAL_ASSERT(video);
1028 status = acpi_GetInteger(handle, "_ADR", &adr);
1029 if (ACPI_FAILURE(status))
1032 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
1033 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
1034 (val & DOD_DEVID_MASK_FULL) ==
1035 (adr & DOD_DEVID_MASK_FULL)) {
1036 argset->callback(handle, val, argset->context);
1045 vid_enum_outputs(ACPI_HANDLE handle,
1046 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
1049 ACPI_BUFFER dod_buf;
1051 struct enum_callback_arg argset;
1053 ACPI_SERIAL_ASSERT(video);
1054 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
1055 dod_buf.Pointer = NULL;
1056 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
1057 if (ACPI_FAILURE(status)) {
1058 if (status != AE_NOT_FOUND)
1059 printf("can't evaluate %s._DOD - %s\n",
1060 acpi_name(handle), AcpiFormatException(status));
1064 res = (ACPI_OBJECT *)dod_buf.Pointer;
1065 if (!ACPI_PKG_VALID(res, 1)) {
1066 printf("evaluation of %s._DOD makes no sense\n",
1071 if (callback == NULL) {
1072 argset.count = res->Package.Count;
1075 argset.callback = callback;
1076 argset.context = context;
1077 argset.dod_pkg = res;
1079 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
1080 vid_enum_outputs_subr, NULL, &argset, NULL);
1081 if (ACPI_FAILURE(status))
1082 printf("failed walking down %s - %s\n",
1083 acpi_name(handle), AcpiFormatException(status));
1085 if (dod_buf.Pointer != NULL)
1086 AcpiOsFree(dod_buf.Pointer);
1087 return (argset.count);
1091 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
1094 ACPI_BUFFER bcl_buf;
1096 int num, i, n, *levels;
1098 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
1099 bcl_buf.Pointer = NULL;
1100 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
1101 if (ACPI_FAILURE(status)) {
1102 if (status != AE_NOT_FOUND)
1103 printf("can't evaluate %s._BCL - %s\n",
1104 acpi_name(handle), AcpiFormatException(status));
1107 res = (ACPI_OBJECT *)bcl_buf.Pointer;
1108 if (!ACPI_PKG_VALID(res, 2)) {
1109 printf("evaluation of %s._BCL makes no sense\n",
1113 num = res->Package.Count;
1114 if (num < 2 || levelp == NULL)
1116 levels = AcpiOsAllocate(num * sizeof(*levels));
1119 for (i = 0, n = 0; i < num; i++)
1120 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1130 if (bcl_buf.Pointer != NULL)
1131 AcpiOsFree(bcl_buf.Pointer);
1136 vo_get_bqc(struct acpi_video_output *vo, UINT32 *level)
1140 switch (vo->vo_hasbqc) {
1143 status = acpi_GetInteger(vo->handle, "_BQC", level);
1144 if (vo->vo_hasbqc == 1)
1146 vo->vo_hasbqc = status != AE_NOT_FOUND;
1147 if (vo->vo_hasbqc == 1)
1151 KASSERT(vo->vo_hasbqc == 0,
1152 ("bad vo_hasbqc state %d", vo->vo_hasbqc));
1153 *level = vo->vo_level;
1160 vo_get_brightness(struct acpi_video_output *vo)
1165 ACPI_SERIAL_ASSERT(video_output);
1166 status = vo_get_bqc(vo, &level);
1167 if (ACPI_FAILURE(status)) {
1168 printf("can't evaluate %s._BQC - %s\n", acpi_name(vo->handle),
1169 AcpiFormatException(status));
1179 vo_set_brightness(struct acpi_video_output *vo, int level)
1181 char notify_buf[16];
1184 ACPI_SERIAL_ASSERT(video_output);
1185 status = acpi_SetInteger(vo->handle, "_BCM", level);
1186 if (ACPI_FAILURE(status)) {
1187 printf("can't evaluate %s._BCM - %s\n",
1188 acpi_name(vo->handle), AcpiFormatException(status));
1190 vo->vo_level = level;
1192 snprintf(notify_buf, sizeof(notify_buf), "notify=%d", level);
1193 devctl_notify("ACPI", "Video", "brightness", notify_buf);
1197 vo_get_device_status(ACPI_HANDLE handle)
1202 ACPI_SERIAL_ASSERT(video_output);
1204 status = acpi_GetInteger(handle, "_DCS", &dcs);
1205 if (ACPI_FAILURE(status)) {
1207 * If the method is missing, assume that the device is always
1210 if (status != AE_NOT_FOUND) {
1211 printf("can't evaluate %s._DCS - %s\n",
1212 acpi_name(handle), AcpiFormatException(status));
1222 vo_get_graphics_state(ACPI_HANDLE handle)
1228 status = acpi_GetInteger(handle, "_DGS", &dgs);
1229 if (ACPI_FAILURE(status)) {
1231 * If the method is missing, assume that the device is always
1234 if (status != AE_NOT_FOUND) {
1235 printf("can't evaluate %s._DGS - %s\n",
1236 acpi_name(handle), AcpiFormatException(status));
1246 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1250 ACPI_SERIAL_ASSERT(video_output);
1251 status = acpi_SetInteger(handle, "_DSS", state);
1252 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
1253 printf("can't evaluate %s._DSS - %s\n",
1254 acpi_name(handle), AcpiFormatException(status));