2 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/power.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
42 #include <contrib/dev/acpica/include/acpi.h>
44 #include <dev/acpica/acpivar.h>
46 /* ACPI video extension driver. */
47 struct acpi_video_output {
50 STAILQ_ENTRY(acpi_video_output) vo_next;
53 STAILQ_ENTRY(acpi_video_output) next;
60 struct sysctl_ctx_list vo_sysctl_ctx;
61 struct sysctl_oid *vo_sysctl_tree;
64 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
66 struct acpi_video_softc {
69 struct acpi_video_output_queue vid_outputs;
70 eventhandler_tag vid_pwr_evh;
74 static int acpi_video_modevent(struct module*, int, void *);
75 static void acpi_video_identify(driver_t *driver, device_t parent);
76 static int acpi_video_probe(device_t);
77 static int acpi_video_attach(device_t);
78 static int acpi_video_detach(device_t);
79 static int acpi_video_resume(device_t);
80 static int acpi_video_shutdown(device_t);
81 static void acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
82 static void acpi_video_power_profile(void *);
83 static void acpi_video_bind_outputs(struct acpi_video_softc *);
84 static struct acpi_video_output *acpi_video_vo_init(UINT32);
85 static void acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
86 static void acpi_video_vo_destroy(struct acpi_video_output *);
87 static int acpi_video_vo_check_level(struct acpi_video_output *, int);
88 static void acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
89 static int acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
90 static int acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
91 static int acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
92 static int acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
95 static void vid_set_switch_policy(ACPI_HANDLE, UINT32);
96 static int vid_enum_outputs(ACPI_HANDLE,
97 void(*)(ACPI_HANDLE, UINT32, void *), void *);
98 static int vo_get_brightness_levels(ACPI_HANDLE, int **);
99 static int vo_get_brightness(ACPI_HANDLE);
100 static void vo_set_brightness(ACPI_HANDLE, int);
101 static UINT32 vo_get_device_status(ACPI_HANDLE);
102 static UINT32 vo_get_graphics_state(ACPI_HANDLE);
103 static void vo_set_device_state(ACPI_HANDLE, UINT32);
106 #define VID_NOTIFY_SWITCHED 0x80
107 #define VID_NOTIFY_REPROBE 0x81
108 #define VID_NOTIFY_CYCLE_BRN 0x85
109 #define VID_NOTIFY_INC_BRN 0x86
110 #define VID_NOTIFY_DEC_BRN 0x87
111 #define VID_NOTIFY_ZERO_BRN 0x88
113 /* _DOS (Enable/Disable Output Switching) argument bits */
114 #define DOS_SWITCH_MASK 3
115 #define DOS_SWITCH_BY_OSPM 0
116 #define DOS_SWITCH_BY_BIOS 1
117 #define DOS_SWITCH_LOCKED 2
118 #define DOS_BRIGHTNESS_BY_OSPM (1 << 2)
120 /* _DOD and subdev's _ADR */
121 #define DOD_DEVID_MASK 0x0f00
122 #define DOD_DEVID_MASK_FULL 0xffff
123 #define DOD_DEVID_MASK_DISPIDX 0x000f
124 #define DOD_DEVID_MASK_DISPPORT 0x00f0
125 #define DOD_DEVID_MONITOR 0x0100
126 #define DOD_DEVID_LCD 0x0110
127 #define DOD_DEVID_TV 0x0200
128 #define DOD_DEVID_EXT 0x0300
129 #define DOD_DEVID_INTDFP 0x0400
130 #define DOD_BIOS (1 << 16)
131 #define DOD_NONVGA (1 << 17)
132 #define DOD_HEAD_ID_SHIFT 18
133 #define DOD_HEAD_ID_BITS 3
134 #define DOD_HEAD_ID_MASK \
135 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
136 #define DOD_DEVID_SCHEME_STD (1U << 31)
138 /* _BCL related constants */
139 #define BCL_FULLPOWER 0
140 #define BCL_ECONOMY 1
142 /* _DCS (Device Currrent Status) value bits and masks. */
143 #define DCS_EXISTS (1 << 0)
144 #define DCS_ACTIVE (1 << 1)
145 #define DCS_READY (1 << 2)
146 #define DCS_FUNCTIONAL (1 << 3)
147 #define DCS_ATTACHED (1 << 4)
149 /* _DSS (Device Set Status) argument bits and masks. */
150 #define DSS_INACTIVE 0
151 #define DSS_ACTIVE (1 << 0)
152 #define DSS_SETNEXT (1 << 30)
153 #define DSS_COMMIT (1U << 31)
155 static device_method_t acpi_video_methods[] = {
156 DEVMETHOD(device_identify, acpi_video_identify),
157 DEVMETHOD(device_probe, acpi_video_probe),
158 DEVMETHOD(device_attach, acpi_video_attach),
159 DEVMETHOD(device_detach, acpi_video_detach),
160 DEVMETHOD(device_resume, acpi_video_resume),
161 DEVMETHOD(device_shutdown, acpi_video_shutdown),
165 static driver_t acpi_video_driver = {
168 sizeof(struct acpi_video_softc),
171 static devclass_t acpi_video_devclass;
173 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
174 acpi_video_modevent, NULL);
175 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
177 static struct sysctl_ctx_list acpi_video_sysctl_ctx;
178 static struct sysctl_oid *acpi_video_sysctl_tree;
179 static struct acpi_video_output_queue crt_units, tv_units,
180 ext_units, lcd_units, other_units;
183 * The 'video' lock protects the hierarchy of video output devices
184 * (the video "bus"). The 'video_output' lock protects per-output
185 * data is equivalent to a softc lock for each video output.
187 ACPI_SERIAL_DECL(video, "ACPI video");
188 ACPI_SERIAL_DECL(video_output, "ACPI video output");
189 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
192 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
199 sysctl_ctx_init(&acpi_video_sysctl_ctx);
200 STAILQ_INIT(&crt_units);
201 STAILQ_INIT(&tv_units);
202 STAILQ_INIT(&ext_units);
203 STAILQ_INIT(&lcd_units);
204 STAILQ_INIT(&other_units);
207 sysctl_ctx_free(&acpi_video_sysctl_ctx);
208 acpi_video_sysctl_tree = NULL;
218 acpi_video_identify(driver_t *driver, device_t parent)
221 if (device_find_child(parent, "acpi_video", -1) == NULL)
222 device_add_child(parent, "acpi_video", -1);
226 acpi_video_probe(device_t dev)
229 ACPI_OBJECT_TYPE t_dos;
231 devh = acpi_get_handle(dev);
232 if (acpi_disabled("video") ||
233 ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
234 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
235 ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
236 t_dos != ACPI_TYPE_METHOD)
239 device_set_desc(dev, "ACPI video extension");
244 acpi_video_attach(device_t dev)
246 struct acpi_softc *acpi_sc;
247 struct acpi_video_softc *sc;
249 sc = device_get_softc(dev);
251 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
254 ACPI_SERIAL_BEGIN(video);
255 if (acpi_video_sysctl_tree == NULL) {
256 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
257 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
258 OID_AUTO, "video", CTLFLAG_RD, 0,
259 "video extension control");
261 ACPI_SERIAL_END(video);
264 sc->handle = acpi_get_handle(dev);
265 STAILQ_INIT(&sc->vid_outputs);
267 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
268 acpi_video_notify_handler, sc);
269 sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
270 acpi_video_power_profile, sc, 0);
272 ACPI_SERIAL_BEGIN(video);
273 acpi_video_bind_outputs(sc);
274 ACPI_SERIAL_END(video);
277 * Notify the BIOS that we want to switch both active outputs and
280 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
281 DOS_BRIGHTNESS_BY_OSPM);
283 acpi_video_power_profile(sc);
289 acpi_video_detach(device_t dev)
291 struct acpi_video_softc *sc;
292 struct acpi_video_output *vo, *vn;
294 sc = device_get_softc(dev);
296 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
297 EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
298 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
299 acpi_video_notify_handler);
301 ACPI_SERIAL_BEGIN(video);
302 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
303 acpi_video_vo_destroy(vo);
305 ACPI_SERIAL_END(video);
311 acpi_video_resume(device_t dev)
313 struct acpi_video_softc *sc;
314 struct acpi_video_output *vo, *vn;
317 sc = device_get_softc(dev);
319 /* Restore brightness level */
320 ACPI_SERIAL_BEGIN(video);
321 ACPI_SERIAL_BEGIN(video_output);
322 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
323 if ((vo->adr & DOD_DEVID_MASK_FULL) != DOD_DEVID_LCD &&
324 (vo->adr & DOD_DEVID_MASK) != DOD_DEVID_INTDFP)
327 if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
330 level = vo_get_brightness(vo->handle);
332 vo_set_brightness(vo->handle, level);
334 ACPI_SERIAL_END(video_output);
335 ACPI_SERIAL_END(video);
341 acpi_video_shutdown(device_t dev)
343 struct acpi_video_softc *sc;
345 sc = device_get_softc(dev);
346 vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
352 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
354 struct acpi_video_softc *sc;
355 struct acpi_video_output *vo, *vo_tmp;
356 ACPI_HANDLE lasthand;
357 UINT32 dcs, dss, dss_p;
359 sc = (struct acpi_video_softc *)context;
362 case VID_NOTIFY_SWITCHED:
365 ACPI_SERIAL_BEGIN(video);
366 ACPI_SERIAL_BEGIN(video_output);
367 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
368 dss = vo_get_graphics_state(vo->handle);
369 dcs = vo_get_device_status(vo->handle);
370 if (!(dcs & DCS_READY))
372 if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
373 (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
374 if (lasthand != NULL)
375 vo_set_device_state(lasthand, dss_p);
377 lasthand = vo->handle;
380 if (lasthand != NULL)
381 vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
382 ACPI_SERIAL_END(video_output);
383 ACPI_SERIAL_END(video);
385 case VID_NOTIFY_REPROBE:
386 ACPI_SERIAL_BEGIN(video);
387 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
389 acpi_video_bind_outputs(sc);
390 STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
391 if (vo->handle == NULL) {
392 STAILQ_REMOVE(&sc->vid_outputs, vo,
393 acpi_video_output, vo_next);
394 acpi_video_vo_destroy(vo);
397 ACPI_SERIAL_END(video);
400 device_printf(sc->device, "unknown notify event 0x%x\n",
406 acpi_video_power_profile(void *context)
409 struct acpi_video_softc *sc;
410 struct acpi_video_output *vo;
413 state = power_profile_get_state();
414 if (state != POWER_PROFILE_PERFORMANCE &&
415 state != POWER_PROFILE_ECONOMY)
418 ACPI_SERIAL_BEGIN(video);
419 ACPI_SERIAL_BEGIN(video_output);
420 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
421 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
422 vo_set_brightness(vo->handle,
423 state == POWER_PROFILE_ECONOMY ?
424 vo->vo_economy : vo->vo_fullpower);
426 ACPI_SERIAL_END(video_output);
427 ACPI_SERIAL_END(video);
431 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
433 struct acpi_video_softc *sc;
434 struct acpi_video_output *vo;
436 ACPI_SERIAL_ASSERT(video);
439 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
440 if (vo->adr == adr) {
441 acpi_video_vo_bind(vo, handle);
445 vo = acpi_video_vo_init(adr);
447 acpi_video_vo_bind(vo, handle);
448 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
453 acpi_video_bind_outputs(struct acpi_video_softc *sc)
456 ACPI_SERIAL_ASSERT(video);
457 vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
460 static struct acpi_video_output *
461 acpi_video_vo_init(UINT32 adr)
463 struct acpi_video_output *vn, *vo, *vp;
465 char name[8], env[32];
466 const char *type, *desc;
467 struct acpi_video_output_queue *voqh;
469 ACPI_SERIAL_ASSERT(video);
471 switch (adr & DOD_DEVID_MASK) {
472 case DOD_DEVID_MONITOR:
473 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
474 /* DOD_DEVID_LCD is a common, backward compatible ID */
475 desc = "Internal/Integrated Digital Flat Panel";
479 desc = "VGA CRT or VESA Compatible Analog Monitor";
485 desc = "TV/HDTV or Analog-Video Monitor";
490 desc = "External Digital Monitor";
494 case DOD_DEVID_INTDFP:
495 desc = "Internal/Integrated Digital Flat Panel";
500 desc = "unknown output";
507 STAILQ_FOREACH(vn, voqh, vo_unit.next) {
508 if (vn->vo_unit.num != n)
514 snprintf(name, sizeof(name), "%s%d", type, n);
516 vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
521 vo->vo_brightness = -1;
522 vo->vo_fullpower = -1; /* TODO: override with tunables */
524 vo->vo_numlevels = 0;
525 vo->vo_levels = NULL;
526 snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
527 if (getenv_int(env, &x))
528 vo->vo_fullpower = x;
529 snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
530 if (getenv_int(env, &x))
533 sysctl_ctx_init(&vo->vo_sysctl_ctx);
535 STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
537 STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
538 if (acpi_video_sysctl_tree != NULL)
540 SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
541 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
542 OID_AUTO, name, CTLFLAG_RD, 0, desc);
543 if (vo->vo_sysctl_tree != NULL) {
544 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
545 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
547 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
548 acpi_video_vo_active_sysctl, "I",
549 "current activity of this device");
550 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
551 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
552 OID_AUTO, "brightness",
553 CTLTYPE_INT|CTLFLAG_RW, vo, 0,
554 acpi_video_vo_bright_sysctl, "I",
555 "current brightness level");
556 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
557 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
558 OID_AUTO, "fullpower",
559 CTLTYPE_INT|CTLFLAG_RW, vo,
560 POWER_PROFILE_PERFORMANCE,
561 acpi_video_vo_presets_sysctl, "I",
562 "preset level for full power mode");
563 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
564 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
566 CTLTYPE_INT|CTLFLAG_RW, vo,
567 POWER_PROFILE_ECONOMY,
568 acpi_video_vo_presets_sysctl, "I",
569 "preset level for economy mode");
570 SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
571 SYSCTL_CHILDREN(vo->vo_sysctl_tree),
573 CTLTYPE_INT | CTLFLAG_RD, vo, 0,
574 acpi_video_vo_levels_sysctl, "I",
575 "supported brightness levels");
577 printf("%s: sysctl node creation failed\n", type);
579 printf("%s: softc allocation failed\n", type);
582 printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
583 printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
584 printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
586 printf(", detectable by BIOS");
587 if (adr & DOD_NONVGA)
588 printf(" (Non-VGA output device whose power "
589 "is related to the VGA device)");
590 printf(", head #%d\n",
591 (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
597 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
600 ACPI_SERIAL_BEGIN(video_output);
601 if (vo->vo_levels != NULL) {
602 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
603 acpi_video_vo_notify_handler);
604 AcpiOsFree(vo->vo_levels);
605 vo->vo_levels = NULL;
608 vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
609 if (vo->vo_numlevels >= 2) {
610 if (vo->vo_fullpower == -1 ||
611 acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) {
612 /* XXX - can't deal with rebinding... */
613 vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
615 if (vo->vo_economy == -1 ||
616 acpi_video_vo_check_level(vo, vo->vo_economy) != 0) {
617 /* XXX - see above. */
618 vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
620 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
621 acpi_video_vo_notify_handler, vo);
623 ACPI_SERIAL_END(video_output);
627 acpi_video_vo_destroy(struct acpi_video_output *vo)
629 struct acpi_video_output_queue *voqh;
631 ACPI_SERIAL_ASSERT(video);
632 if (vo->vo_sysctl_tree != NULL) {
633 vo->vo_sysctl_tree = NULL;
634 sysctl_ctx_free(&vo->vo_sysctl_ctx);
636 if (vo->vo_levels != NULL) {
637 AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
638 acpi_video_vo_notify_handler);
639 AcpiOsFree(vo->vo_levels);
642 switch (vo->adr & DOD_DEVID_MASK) {
643 case DOD_DEVID_MONITOR:
652 case DOD_DEVID_INTDFP:
658 STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
659 free(vo, M_ACPIVIDEO);
663 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
667 ACPI_SERIAL_ASSERT(video_output);
668 if (vo->vo_levels == NULL)
670 for (i = 0; i < vo->vo_numlevels; i++)
671 if (vo->vo_levels[i] == level)
677 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
679 struct acpi_video_output *vo;
680 int i, j, level, new_level;
683 ACPI_SERIAL_BEGIN(video_output);
684 if (vo->handle != handle)
688 case VID_NOTIFY_CYCLE_BRN:
689 if (vo->vo_numlevels <= 3)
692 case VID_NOTIFY_INC_BRN:
693 case VID_NOTIFY_DEC_BRN:
694 case VID_NOTIFY_ZERO_BRN:
695 if (vo->vo_levels == NULL)
697 level = vo_get_brightness(handle);
702 printf("unknown notify event 0x%x from %s\n",
703 notify, acpi_name(handle));
709 case VID_NOTIFY_CYCLE_BRN:
710 for (i = 2; i < vo->vo_numlevels; i++)
711 if (vo->vo_levels[i] == level) {
712 new_level = vo->vo_numlevels > i + 1 ?
713 vo->vo_levels[i + 1] : vo->vo_levels[2];
717 case VID_NOTIFY_INC_BRN:
718 case VID_NOTIFY_DEC_BRN:
719 for (i = 0; i < vo->vo_numlevels; i++) {
720 j = vo->vo_levels[i];
721 if (notify == VID_NOTIFY_INC_BRN) {
723 (j < new_level || level == new_level))
727 (j > new_level || level == new_level))
732 case VID_NOTIFY_ZERO_BRN:
733 for (i = 0; i < vo->vo_numlevels; i++)
734 if (vo->vo_levels[i] == 0) {
740 if (new_level != level) {
741 vo_set_brightness(handle, new_level);
742 vo->vo_brightness = new_level;
746 ACPI_SERIAL_END(video_output);
751 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
753 struct acpi_video_output *vo;
756 vo = (struct acpi_video_output *)arg1;
757 if (vo->handle == NULL)
759 ACPI_SERIAL_BEGIN(video_output);
760 state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
761 err = sysctl_handle_int(oidp, &state, 0, req);
762 if (err != 0 || req->newptr == NULL)
764 vo_set_device_state(vo->handle,
765 DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
767 ACPI_SERIAL_END(video_output);
773 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
775 struct acpi_video_output *vo;
776 int level, preset, err;
778 vo = (struct acpi_video_output *)arg1;
779 ACPI_SERIAL_BEGIN(video_output);
780 if (vo->handle == NULL) {
784 if (vo->vo_levels == NULL) {
789 preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
790 vo->vo_economy : vo->vo_fullpower;
791 level = vo->vo_brightness;
795 err = sysctl_handle_int(oidp, &level, 0, req);
796 if (err != 0 || req->newptr == NULL)
798 if (level < -1 || level > 100) {
803 if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
805 vo->vo_brightness = level;
806 vo_set_brightness(vo->handle, (level == -1) ? preset : level);
809 ACPI_SERIAL_END(video_output);
814 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
816 struct acpi_video_output *vo;
817 int i, level, *preset, err;
819 vo = (struct acpi_video_output *)arg1;
820 ACPI_SERIAL_BEGIN(video_output);
821 if (vo->handle == NULL) {
825 if (vo->vo_levels == NULL) {
829 preset = (arg2 == POWER_PROFILE_ECONOMY) ?
830 &vo->vo_economy : &vo->vo_fullpower;
832 err = sysctl_handle_int(oidp, &level, 0, req);
833 if (err != 0 || req->newptr == NULL)
835 if (level < -1 || level > 100) {
840 i = (arg2 == POWER_PROFILE_ECONOMY) ?
841 BCL_ECONOMY : BCL_FULLPOWER;
842 level = vo->vo_levels[i];
843 } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
846 if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
847 vo_set_brightness(vo->handle, level);
851 ACPI_SERIAL_END(video_output);
857 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
859 struct acpi_video_output *vo;
862 vo = (struct acpi_video_output *)arg1;
863 ACPI_SERIAL_BEGIN(video_output);
864 if (vo->vo_levels == NULL) {
868 if (req->newptr != NULL) {
872 err = sysctl_handle_opaque(oidp, vo->vo_levels,
873 vo->vo_numlevels * sizeof(*vo->vo_levels), req);
876 ACPI_SERIAL_END(video_output);
881 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
885 status = acpi_SetInteger(handle, "_DOS", policy);
886 if (ACPI_FAILURE(status))
887 printf("can't evaluate %s._DOS - %s\n",
888 acpi_name(handle), AcpiFormatException(status));
891 struct enum_callback_arg {
892 void (*callback)(ACPI_HANDLE, UINT32, void *);
894 ACPI_OBJECT *dod_pkg;
899 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
900 void *context, void **retp __unused)
904 struct enum_callback_arg *argset;
907 ACPI_SERIAL_ASSERT(video);
909 status = acpi_GetInteger(handle, "_ADR", &adr);
910 if (ACPI_FAILURE(status))
913 for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
914 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
915 (val & DOD_DEVID_MASK_FULL) ==
916 (adr & DOD_DEVID_MASK_FULL)) {
917 argset->callback(handle, val, argset->context);
926 vid_enum_outputs(ACPI_HANDLE handle,
927 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
932 struct enum_callback_arg argset;
934 ACPI_SERIAL_ASSERT(video);
935 dod_buf.Length = ACPI_ALLOCATE_BUFFER;
936 dod_buf.Pointer = NULL;
937 status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
938 if (ACPI_FAILURE(status)) {
939 if (status != AE_NOT_FOUND)
940 printf("can't evaluate %s._DOD - %s\n",
941 acpi_name(handle), AcpiFormatException(status));
945 res = (ACPI_OBJECT *)dod_buf.Pointer;
946 if (!ACPI_PKG_VALID(res, 1)) {
947 printf("evaluation of %s._DOD makes no sense\n",
952 if (callback == NULL) {
953 argset.count = res->Package.Count;
956 argset.callback = callback;
957 argset.context = context;
958 argset.dod_pkg = res;
960 status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
961 vid_enum_outputs_subr, NULL, &argset, NULL);
962 if (ACPI_FAILURE(status))
963 printf("failed walking down %s - %s\n",
964 acpi_name(handle), AcpiFormatException(status));
966 if (dod_buf.Pointer != NULL)
967 AcpiOsFree(dod_buf.Pointer);
968 return (argset.count);
972 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
977 int num, i, n, *levels;
979 bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
980 bcl_buf.Pointer = NULL;
981 status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
982 if (ACPI_FAILURE(status)) {
983 if (status != AE_NOT_FOUND)
984 printf("can't evaluate %s._BCL - %s\n",
985 acpi_name(handle), AcpiFormatException(status));
988 res = (ACPI_OBJECT *)bcl_buf.Pointer;
989 if (!ACPI_PKG_VALID(res, 2)) {
990 printf("evaluation of %s._BCL makes no sense\n",
994 num = res->Package.Count;
995 if (num < 2 || levelp == NULL)
997 levels = AcpiOsAllocate(num * sizeof(*levels));
1000 for (i = 0, n = 0; i < num; i++)
1001 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1011 if (bcl_buf.Pointer != NULL)
1012 AcpiOsFree(bcl_buf.Pointer);
1017 vo_get_brightness(ACPI_HANDLE handle)
1022 ACPI_SERIAL_ASSERT(video_output);
1023 status = acpi_GetInteger(handle, "_BQC", &level);
1024 if (ACPI_FAILURE(status)) {
1025 printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
1026 AcpiFormatException(status));
1036 vo_set_brightness(ACPI_HANDLE handle, int level)
1040 ACPI_SERIAL_ASSERT(video_output);
1041 status = acpi_SetInteger(handle, "_BCM", level);
1042 if (ACPI_FAILURE(status))
1043 printf("can't evaluate %s._BCM - %s\n",
1044 acpi_name(handle), AcpiFormatException(status));
1048 vo_get_device_status(ACPI_HANDLE handle)
1053 ACPI_SERIAL_ASSERT(video_output);
1055 status = acpi_GetInteger(handle, "_DCS", &dcs);
1056 if (ACPI_FAILURE(status))
1057 printf("can't evaluate %s._DCS - %s\n",
1058 acpi_name(handle), AcpiFormatException(status));
1064 vo_get_graphics_state(ACPI_HANDLE handle)
1070 status = acpi_GetInteger(handle, "_DGS", &dgs);
1071 if (ACPI_FAILURE(status))
1072 printf("can't evaluate %s._DGS - %s\n",
1073 acpi_name(handle), AcpiFormatException(status));
1079 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1083 ACPI_SERIAL_ASSERT(video_output);
1084 status = acpi_SetInteger(handle, "_DSS", state);
1085 if (ACPI_FAILURE(status))
1086 printf("can't evaluate %s._DSS - %s\n",
1087 acpi_name(handle), AcpiFormatException(status));