acpi_video.c revision 203810
1/*-
2 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 *	$Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_video.c 203810 2010-02-13 02:24:23Z jkim $");
31
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <sys/power.h>
38#include <sys/queue.h>
39#include <sys/sysctl.h>
40
41#include <contrib/dev/acpica/include/acpi.h>
42
43#include <dev/acpica/acpivar.h>
44
45/* ACPI video extension driver. */
46struct acpi_video_output {
47	ACPI_HANDLE	handle;
48	UINT32		adr;
49	STAILQ_ENTRY(acpi_video_output) vo_next;
50	struct {
51		int	num;
52		STAILQ_ENTRY(acpi_video_output) next;
53	} vo_unit;
54	int		vo_brightness;
55	int		vo_fullpower;
56	int		vo_economy;
57	int		vo_numlevels;
58	int		*vo_levels;
59	struct sysctl_ctx_list vo_sysctl_ctx;
60	struct sysctl_oid *vo_sysctl_tree;
61};
62
63STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
64
65struct acpi_video_softc {
66	device_t		device;
67	ACPI_HANDLE		handle;
68	struct acpi_video_output_queue vid_outputs;
69	eventhandler_tag	vid_pwr_evh;
70};
71
72/* interfaces */
73static int	acpi_video_modevent(struct module*, int, void *);
74static void	acpi_video_identify(driver_t *driver, device_t parent);
75static int	acpi_video_probe(device_t);
76static int	acpi_video_attach(device_t);
77static int	acpi_video_detach(device_t);
78static int	acpi_video_shutdown(device_t);
79static void	acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
80static void	acpi_video_power_profile(void *);
81static void	acpi_video_bind_outputs(struct acpi_video_softc *);
82static struct acpi_video_output *acpi_video_vo_init(UINT32);
83static void	acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
84static void	acpi_video_vo_destroy(struct acpi_video_output *);
85static int	acpi_video_vo_check_level(struct acpi_video_output *, int);
86static void	acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
87static int	acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
88static int	acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
89static int	acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
90static int	acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
91
92/* operations */
93static void	vid_set_switch_policy(ACPI_HANDLE, UINT32);
94static int	vid_enum_outputs(ACPI_HANDLE,
95		    void(*)(ACPI_HANDLE, UINT32, void *), void *);
96static int	vo_get_brightness_levels(ACPI_HANDLE, int **);
97static int	vo_get_brightness(ACPI_HANDLE);
98static void	vo_set_brightness(ACPI_HANDLE, int);
99static UINT32	vo_get_device_status(ACPI_HANDLE);
100static UINT32	vo_get_graphics_state(ACPI_HANDLE);
101static void	vo_set_device_state(ACPI_HANDLE, UINT32);
102
103/* events */
104#define VID_NOTIFY_SWITCHED	0x80
105#define VID_NOTIFY_REPROBE	0x81
106#define	VID_NOTIFY_INC_BRN	0x86
107#define	VID_NOTIFY_DEC_BRN	0x87
108
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_BIOS	(1 << 2)
115
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)
133
134/* _BCL related constants */
135#define BCL_FULLPOWER		0
136#define BCL_ECONOMY		1
137
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)
144
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)
150
151static 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_shutdown, acpi_video_shutdown),
157	{ 0, 0 }
158};
159
160static driver_t acpi_video_driver = {
161	"acpi_video",
162	acpi_video_methods,
163	sizeof(struct acpi_video_softc),
164};
165
166static devclass_t acpi_video_devclass;
167
168DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
169	      acpi_video_modevent, NULL);
170MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
171
172static struct sysctl_ctx_list	acpi_video_sysctl_ctx;
173static struct sysctl_oid	*acpi_video_sysctl_tree;
174static struct acpi_video_output_queue crt_units, tv_units,
175    ext_units, lcd_units, other_units;
176
177/*
178 * The 'video' lock protects the hierarchy of video output devices
179 * (the video "bus").  The 'video_output' lock protects per-output
180 * data is equivalent to a softc lock for each video output.
181 */
182ACPI_SERIAL_DECL(video, "ACPI video");
183ACPI_SERIAL_DECL(video_output, "ACPI video output");
184MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
185
186static int
187acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
188{
189	int err;
190
191	err = 0;
192	switch (evt) {
193	case MOD_LOAD:
194		sysctl_ctx_init(&acpi_video_sysctl_ctx);
195		STAILQ_INIT(&crt_units);
196		STAILQ_INIT(&tv_units);
197		STAILQ_INIT(&ext_units);
198		STAILQ_INIT(&lcd_units);
199		STAILQ_INIT(&other_units);
200		break;
201	case MOD_UNLOAD:
202		sysctl_ctx_free(&acpi_video_sysctl_ctx);
203		acpi_video_sysctl_tree = NULL;
204		break;
205	default:
206		err = EINVAL;
207	}
208
209	return (err);
210}
211
212static void
213acpi_video_identify(driver_t *driver, device_t parent)
214{
215
216	if (device_find_child(parent, "acpi_video", -1) == NULL)
217		device_add_child(parent, "acpi_video", -1);
218}
219
220static int
221acpi_video_probe(device_t dev)
222{
223	ACPI_HANDLE devh, h;
224	ACPI_OBJECT_TYPE t_dos;
225
226	devh = acpi_get_handle(dev);
227	if (acpi_disabled("video") ||
228	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
229	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
230	    ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
231	    t_dos != ACPI_TYPE_METHOD)
232		return (ENXIO);
233
234	device_set_desc(dev, "ACPI video extension");
235	return (0);
236}
237
238static int
239acpi_video_attach(device_t dev)
240{
241	struct acpi_softc *acpi_sc;
242	struct acpi_video_softc *sc;
243
244	sc = device_get_softc(dev);
245
246	acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
247	if (acpi_sc == NULL)
248		return (ENXIO);
249	ACPI_SERIAL_BEGIN(video);
250	if (acpi_video_sysctl_tree == NULL) {
251		acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
252				    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
253				    OID_AUTO, "video", CTLFLAG_RD, 0,
254				    "video extension control");
255	}
256	ACPI_SERIAL_END(video);
257
258	sc->device = dev;
259	sc->handle = acpi_get_handle(dev);
260	STAILQ_INIT(&sc->vid_outputs);
261
262	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
263				 acpi_video_notify_handler, sc);
264	sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
265				 acpi_video_power_profile, sc, 0);
266
267	ACPI_SERIAL_BEGIN(video);
268	acpi_video_bind_outputs(sc);
269	ACPI_SERIAL_END(video);
270
271	/*
272	 * Notify the BIOS that we want to switch both active outputs and
273	 * brightness levels.
274	 */
275	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
276	    DOS_BRIGHTNESS_BY_BIOS);
277
278	acpi_video_power_profile(sc);
279
280	return (0);
281}
282
283static int
284acpi_video_detach(device_t dev)
285{
286	struct acpi_video_softc *sc;
287	struct acpi_video_output *vo, *vn;
288
289	sc = device_get_softc(dev);
290
291	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
292	EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
293	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
294				acpi_video_notify_handler);
295
296	ACPI_SERIAL_BEGIN(video);
297	STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
298		acpi_video_vo_destroy(vo);
299	}
300	ACPI_SERIAL_END(video);
301
302	return (0);
303}
304
305static int
306acpi_video_shutdown(device_t dev)
307{
308	struct acpi_video_softc *sc;
309
310	sc = device_get_softc(dev);
311	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
312
313	return (0);
314}
315
316static void
317acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
318{
319	struct acpi_video_softc *sc;
320	struct acpi_video_output *vo, *vo_tmp;
321	ACPI_HANDLE lasthand;
322	UINT32 dcs, dss, dss_p;
323
324	sc = (struct acpi_video_softc *)context;
325
326	switch (notify) {
327	case VID_NOTIFY_SWITCHED:
328		dss_p = 0;
329		lasthand = NULL;
330		ACPI_SERIAL_BEGIN(video);
331		ACPI_SERIAL_BEGIN(video_output);
332		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
333			dss = vo_get_graphics_state(vo->handle);
334			dcs = vo_get_device_status(vo->handle);
335			if (!(dcs & DCS_READY))
336				dss = DSS_INACTIVE;
337			if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
338			    (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
339				if (lasthand != NULL)
340					vo_set_device_state(lasthand, dss_p);
341				dss_p = dss;
342				lasthand = vo->handle;
343			}
344		}
345		if (lasthand != NULL)
346			vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
347		ACPI_SERIAL_END(video_output);
348		ACPI_SERIAL_END(video);
349		break;
350	case VID_NOTIFY_REPROBE:
351		ACPI_SERIAL_BEGIN(video);
352		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
353			vo->handle = NULL;
354		acpi_video_bind_outputs(sc);
355		STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
356			if (vo->handle == NULL) {
357				STAILQ_REMOVE(&sc->vid_outputs, vo,
358				    acpi_video_output, vo_next);
359				acpi_video_vo_destroy(vo);
360			}
361		}
362		ACPI_SERIAL_END(video);
363		break;
364	default:
365		device_printf(sc->device, "unknown notify event 0x%x\n",
366		    notify);
367	}
368}
369
370static void
371acpi_video_power_profile(void *context)
372{
373	int state;
374	struct acpi_video_softc *sc;
375	struct acpi_video_output *vo;
376
377	sc = context;
378	state = power_profile_get_state();
379	if (state != POWER_PROFILE_PERFORMANCE &&
380	    state != POWER_PROFILE_ECONOMY)
381		return;
382
383	ACPI_SERIAL_BEGIN(video);
384	ACPI_SERIAL_BEGIN(video_output);
385	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
386		if (vo->vo_levels != NULL && vo->vo_brightness == -1)
387			vo_set_brightness(vo->handle,
388			    state == POWER_PROFILE_ECONOMY ?
389			    vo->vo_economy : vo->vo_fullpower);
390	}
391	ACPI_SERIAL_END(video_output);
392	ACPI_SERIAL_END(video);
393}
394
395static void
396acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
397{
398	struct acpi_video_softc *sc;
399	struct acpi_video_output *vo;
400
401	ACPI_SERIAL_ASSERT(video);
402	sc = context;
403
404	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
405		if (vo->adr == adr) {
406			acpi_video_vo_bind(vo, handle);
407			return;
408		}
409	}
410	vo = acpi_video_vo_init(adr);
411	if (vo != NULL) {
412		acpi_video_vo_bind(vo, handle);
413		STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
414	}
415}
416
417static void
418acpi_video_bind_outputs(struct acpi_video_softc *sc)
419{
420
421	ACPI_SERIAL_ASSERT(video);
422	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
423}
424
425static struct acpi_video_output *
426acpi_video_vo_init(UINT32 adr)
427{
428	struct acpi_video_output *vn, *vo, *vp;
429	int n, x;
430	int display_index;
431	int display_port;
432	char name[8], env[32];
433	const char *type, *desc;
434	struct acpi_video_output_queue *voqh;
435
436	ACPI_SERIAL_ASSERT(video);
437	display_index = adr & DOD_DEVID_MASK_DISPIDX;
438	display_port = (adr & DOD_DEVID_MASK_DISPPORT) >> 4;
439
440	switch (adr & DOD_DEVID_MASK) {
441	case DOD_DEVID_MONITOR:
442		if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
443			/* DOD_DEVID_LCD is a common, backward compatible ID */
444			desc = "Internal/Integrated Digital Flat Panel";
445			type = "lcd";
446			voqh = &lcd_units;
447		} else {
448			desc = "VGA CRT or VESA Compatible Analog Monitor";
449			type = "crt";
450			voqh = &crt_units;
451		}
452		break;
453	case DOD_DEVID_TV:
454		desc = "TV/HDTV or Analog-Video Monitor";
455		type = "tv";
456		voqh = &tv_units;
457		break;
458	case DOD_DEVID_EXT:
459		desc = "External Digital Monitor";
460		type = "ext";
461		voqh = &ext_units;
462		break;
463	case DOD_DEVID_INTDFP:
464		desc = "Internal/Integrated Digital Flat Panel";
465		type = "lcd";
466		voqh = &lcd_units;
467		break;
468	default:
469		desc = "unknown output";
470		type = "out";
471		voqh = &other_units;
472	}
473
474	n = 0;
475	vn = vp = NULL;
476	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
477		if (vn->vo_unit.num != n)
478			break;
479		vp = vn;
480		n++;
481	}
482
483	snprintf(name, sizeof(name), "%s%d", type, n);
484
485	vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
486	if (vo != NULL) {
487		vo->handle = NULL;
488		vo->adr = adr;
489		vo->vo_unit.num = n;
490		vo->vo_brightness = -1;
491		vo->vo_fullpower = -1;	/* TODO: override with tunables */
492		vo->vo_economy = -1;
493		vo->vo_numlevels = 0;
494		vo->vo_levels = NULL;
495		snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
496		if (getenv_int(env, &x))
497			vo->vo_fullpower = x;
498		snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
499		if (getenv_int(env, &x))
500			vo->vo_economy = x;
501
502		sysctl_ctx_init(&vo->vo_sysctl_ctx);
503		if (vp != NULL)
504			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
505		else
506			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
507		if (acpi_video_sysctl_tree != NULL)
508			vo->vo_sysctl_tree =
509			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
510				SYSCTL_CHILDREN(acpi_video_sysctl_tree),
511				OID_AUTO, name, CTLFLAG_RD, 0, desc);
512		if (vo->vo_sysctl_tree != NULL) {
513			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
514			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
515			    OID_AUTO, "active",
516			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
517			    acpi_video_vo_active_sysctl, "I",
518			    "current activity of this device");
519			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
520			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
521			    OID_AUTO, "brightness",
522			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
523			    acpi_video_vo_bright_sysctl, "I",
524			    "current brightness level");
525			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
526			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
527			    OID_AUTO, "fullpower",
528			    CTLTYPE_INT|CTLFLAG_RW, vo,
529			    POWER_PROFILE_PERFORMANCE,
530			    acpi_video_vo_presets_sysctl, "I",
531			    "preset level for full power mode");
532			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
533			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
534			    OID_AUTO, "economy",
535			    CTLTYPE_INT|CTLFLAG_RW, vo,
536			    POWER_PROFILE_ECONOMY,
537			    acpi_video_vo_presets_sysctl, "I",
538			    "preset level for economy mode");
539			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
540			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
541			    OID_AUTO, "levels",
542			    CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
543			    acpi_video_vo_levels_sysctl, "I",
544			    "supported brightness levels");
545		} else
546			printf("%s: sysctl node creation failed\n", type);
547	} else
548		printf("%s: softc allocation failed\n", type);
549
550	if (bootverbose) {
551		printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
552		printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
553		printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
554		if (adr & DOD_BIOS)
555			printf(", detectable by BIOS");
556		if (adr & DOD_NONVGA)
557			printf(" (Non-VGA output device whose power "
558			    "is related to the VGA device)");
559		printf(", head #%d\n",
560			(adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
561	}
562	return (vo);
563}
564
565static void
566acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
567{
568
569	ACPI_SERIAL_BEGIN(video_output);
570	if (vo->vo_levels != NULL)
571		AcpiOsFree(vo->vo_levels);
572	vo->handle = handle;
573	vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
574	if (vo->vo_numlevels >= 2) {
575		if (vo->vo_fullpower == -1
576		    || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
577			/* XXX - can't deal with rebinding... */
578			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
579		if (vo->vo_economy == -1
580		    || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
581			/* XXX - see above. */
582			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
583	}
584	if (vo->vo_levels != NULL)
585	    AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
586		acpi_video_vo_notify_handler, vo);
587	ACPI_SERIAL_END(video_output);
588}
589
590static void
591acpi_video_vo_destroy(struct acpi_video_output *vo)
592{
593	struct acpi_video_output_queue *voqh;
594
595	ACPI_SERIAL_ASSERT(video);
596	if (vo->vo_sysctl_tree != NULL) {
597		vo->vo_sysctl_tree = NULL;
598		sysctl_ctx_free(&vo->vo_sysctl_ctx);
599	}
600	if (vo->vo_levels != NULL) {
601		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
602		    acpi_video_vo_notify_handler);
603		AcpiOsFree(vo->vo_levels);
604	}
605
606	switch (vo->adr & DOD_DEVID_MASK) {
607	case DOD_DEVID_MONITOR:
608		voqh = &crt_units;
609		break;
610	case DOD_DEVID_TV:
611		voqh = &tv_units;
612		break;
613	case DOD_DEVID_EXT:
614		voqh = &ext_units;
615		break;
616	case DOD_DEVID_INTDFP:
617		voqh = &lcd_units;
618		break;
619	default:
620		voqh = &other_units;
621	}
622	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
623	free(vo, M_ACPIVIDEO);
624}
625
626static int
627acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
628{
629	int i;
630
631	ACPI_SERIAL_ASSERT(video_output);
632	if (vo->vo_levels == NULL)
633		return (ENODEV);
634	for (i = 0; i < vo->vo_numlevels; i++)
635		if (vo->vo_levels[i] == level)
636			return (0);
637	return (EINVAL);
638}
639
640static void
641acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
642{
643	struct acpi_video_output *vo;
644	int i, j, level, new_level;
645
646	vo = context;
647	ACPI_SERIAL_BEGIN(video_output);
648	if (vo->handle == NULL) {
649		ACPI_SERIAL_END(video_output);
650		return;
651	}
652
653	switch (notify) {
654	case VID_NOTIFY_INC_BRN:
655	case VID_NOTIFY_DEC_BRN:
656		if (vo->vo_levels == NULL)
657			break;
658		level = vo_get_brightness(vo->handle);
659		if (level < 0)
660			break;
661		new_level = level;
662		for (i = 0; i < vo->vo_numlevels; i++) {
663			j = vo->vo_levels[i];
664			if (notify == VID_NOTIFY_INC_BRN) {
665				if (j > level &&
666				    (j < new_level || level == new_level))
667					new_level = j;
668			} else {
669				if (j < level &&
670				    (j > new_level || level == new_level))
671					new_level = j;
672			}
673		}
674		if (new_level != level) {
675			vo_set_brightness(vo->handle, new_level);
676			vo->vo_brightness = new_level;
677		}
678		break;
679	default:
680		printf("%s: unknown notify event 0x%x\n",
681		    acpi_name(vo->handle), notify);
682	}
683	ACPI_SERIAL_END(video_output);
684}
685
686/* ARGSUSED */
687static int
688acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
689{
690	struct acpi_video_output *vo;
691	int state, err;
692
693	vo = (struct acpi_video_output *)arg1;
694	if (vo->handle == NULL)
695		return (ENXIO);
696	ACPI_SERIAL_BEGIN(video_output);
697	state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
698	err = sysctl_handle_int(oidp, &state, 0, req);
699	if (err != 0 || req->newptr == NULL)
700		goto out;
701	vo_set_device_state(vo->handle,
702	    DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
703out:
704	ACPI_SERIAL_END(video_output);
705	return (err);
706}
707
708/* ARGSUSED */
709static int
710acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
711{
712	struct acpi_video_output *vo;
713	int level, preset, err;
714
715	vo = (struct acpi_video_output *)arg1;
716	ACPI_SERIAL_BEGIN(video_output);
717	if (vo->handle == NULL) {
718		err = ENXIO;
719		goto out;
720	}
721	if (vo->vo_levels == NULL) {
722		err = ENODEV;
723		goto out;
724	}
725
726	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
727		  vo->vo_economy : vo->vo_fullpower;
728	level = vo->vo_brightness;
729	if (level == -1)
730		level = preset;
731
732	err = sysctl_handle_int(oidp, &level, 0, req);
733	if (err != 0 || req->newptr == NULL)
734		goto out;
735	if (level < -1 || level > 100) {
736		err = EINVAL;
737		goto out;
738	}
739
740	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
741		goto out;
742	vo->vo_brightness = level;
743	vo_set_brightness(vo->handle, (level == -1) ? preset : level);
744
745out:
746	ACPI_SERIAL_END(video_output);
747	return (err);
748}
749
750static int
751acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
752{
753	struct acpi_video_output *vo;
754	int i, level, *preset, err;
755
756	err = 0;
757	vo = (struct acpi_video_output *)arg1;
758	ACPI_SERIAL_BEGIN(video_output);
759	if (vo->handle == NULL) {
760		err = ENXIO;
761		goto out;
762	}
763	if (vo->vo_levels == NULL) {
764		err = ENODEV;
765		goto out;
766	}
767	preset = (arg2 == POWER_PROFILE_ECONOMY) ?
768		  &vo->vo_economy : &vo->vo_fullpower;
769	level = *preset;
770	err = sysctl_handle_int(oidp, &level, 0, req);
771	if (err != 0 || req->newptr == NULL)
772		goto out;
773	if (level < -1 || level > 100) {
774		err = EINVAL;
775		goto out;
776	}
777	if (level == -1) {
778		i = (arg2 == POWER_PROFILE_ECONOMY) ?
779		    BCL_ECONOMY : BCL_FULLPOWER;
780		level = vo->vo_levels[i];
781	} else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
782		goto out;
783
784	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
785		vo_set_brightness(vo->handle, level);
786	*preset = level;
787
788out:
789	ACPI_SERIAL_END(video_output);
790	return (err);
791}
792
793/* ARGSUSED */
794static int
795acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
796{
797	struct acpi_video_output *vo;
798	int err;
799
800	vo = (struct acpi_video_output *)arg1;
801	ACPI_SERIAL_BEGIN(video_output);
802	if (vo->vo_levels == NULL) {
803		err = ENODEV;
804		goto out;
805	}
806	if (req->newptr != NULL) {
807		err = EPERM;
808		goto out;
809	}
810	err = sysctl_handle_opaque(oidp, vo->vo_levels,
811	    vo->vo_numlevels * sizeof(*vo->vo_levels), req);
812
813out:
814	ACPI_SERIAL_END(video_output);
815	return (err);
816}
817
818static void
819vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
820{
821	ACPI_STATUS status;
822
823	status = acpi_SetInteger(handle, "_DOS", policy);
824	if (ACPI_FAILURE(status))
825		printf("can't evaluate %s._DOS - %s\n",
826		       acpi_name(handle), AcpiFormatException(status));
827}
828
829struct enum_callback_arg {
830	void (*callback)(ACPI_HANDLE, UINT32, void *);
831	void *context;
832	ACPI_OBJECT *dod_pkg;
833	int count;
834};
835
836static ACPI_STATUS
837vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
838		      void *context, void **retp __unused)
839{
840	ACPI_STATUS status;
841	UINT32 adr, val;
842	struct enum_callback_arg *argset;
843	size_t i;
844
845	ACPI_SERIAL_ASSERT(video);
846	argset = context;
847	status = acpi_GetInteger(handle, "_ADR", &adr);
848	if (ACPI_FAILURE(status))
849		return (AE_OK);
850
851	for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
852		if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
853		    (val & DOD_DEVID_MASK_FULL) == adr) {
854			argset->callback(handle, val, argset->context);
855			argset->count++;
856		}
857	}
858
859	return (AE_OK);
860}
861
862static int
863vid_enum_outputs(ACPI_HANDLE handle,
864		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
865{
866	ACPI_STATUS status;
867	ACPI_BUFFER dod_buf;
868	ACPI_OBJECT *res;
869	struct enum_callback_arg argset;
870
871	ACPI_SERIAL_ASSERT(video);
872	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
873	dod_buf.Pointer = NULL;
874	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
875	if (ACPI_FAILURE(status)) {
876		if (status != AE_NOT_FOUND)
877			printf("can't evaluate %s._DOD - %s\n",
878			       acpi_name(handle), AcpiFormatException(status));
879		argset.count = -1;
880		goto out;
881	}
882	res = (ACPI_OBJECT *)dod_buf.Pointer;
883	if (!ACPI_PKG_VALID(res, 1)) {
884		printf("evaluation of %s._DOD makes no sense\n",
885		       acpi_name(handle));
886		argset.count = -1;
887		goto out;
888	}
889	if (callback == NULL) {
890		argset.count = res->Package.Count;
891		goto out;
892	}
893	argset.callback = callback;
894	argset.context  = context;
895	argset.dod_pkg  = res;
896	argset.count    = 0;
897	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
898	    vid_enum_outputs_subr, NULL, &argset, NULL);
899	if (ACPI_FAILURE(status))
900		printf("failed walking down %s - %s\n",
901		       acpi_name(handle), AcpiFormatException(status));
902out:
903	if (dod_buf.Pointer != NULL)
904		AcpiOsFree(dod_buf.Pointer);
905	return (argset.count);
906}
907
908static int
909vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
910{
911	ACPI_STATUS status;
912	ACPI_BUFFER bcl_buf;
913	ACPI_OBJECT *res;
914	int num, i, n, *levels;
915
916	num = 0;
917	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
918	bcl_buf.Pointer = NULL;
919	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
920	if (ACPI_FAILURE(status)) {
921		if (status != AE_NOT_FOUND)
922			printf("can't evaluate %s._BCL - %s\n",
923			       acpi_name(handle), AcpiFormatException(status));
924		num = -1;
925		goto out;
926	}
927	res = (ACPI_OBJECT *)bcl_buf.Pointer;
928	if (!ACPI_PKG_VALID(res, 2)) {
929		printf("evaluation of %s._BCL makes no sense\n",
930		       acpi_name(handle));
931		num = -1;
932		goto out;
933	}
934	num = res->Package.Count;
935	if (levelp == NULL)
936		goto out;
937	levels = AcpiOsAllocate(num * sizeof(*levels));
938	if (levels == NULL) {
939		num = -1;
940		goto out;
941	}
942	for (i = 0, n = 0; i < num; i++)
943		if (acpi_PkgInt32(res, i, &levels[n]) == 0)
944			n++;
945	if (n < 2) {
946		num = -1;
947		AcpiOsFree(levels);
948	} else {
949		num = n;
950		*levelp = levels;
951	}
952out:
953	if (bcl_buf.Pointer != NULL)
954		AcpiOsFree(bcl_buf.Pointer);
955
956	return (num);
957}
958
959static int
960vo_get_brightness(ACPI_HANDLE handle)
961{
962	UINT32 level;
963	ACPI_STATUS status;
964
965	ACPI_SERIAL_ASSERT(video_output);
966	status = acpi_GetInteger(handle, "_BQC", &level);
967	if (ACPI_FAILURE(status)) {
968		printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
969		    AcpiFormatException(status));
970		return (-1);
971	}
972	if (level > 100)
973		return (-1);
974
975	return (level);
976}
977
978static void
979vo_set_brightness(ACPI_HANDLE handle, int level)
980{
981	ACPI_STATUS status;
982
983	ACPI_SERIAL_ASSERT(video_output);
984	status = acpi_SetInteger(handle, "_BCM", level);
985	if (ACPI_FAILURE(status))
986		printf("can't evaluate %s._BCM - %s\n",
987		       acpi_name(handle), AcpiFormatException(status));
988}
989
990static UINT32
991vo_get_device_status(ACPI_HANDLE handle)
992{
993	UINT32 dcs;
994	ACPI_STATUS status;
995
996	ACPI_SERIAL_ASSERT(video_output);
997	dcs = 0;
998	status = acpi_GetInteger(handle, "_DCS", &dcs);
999	if (ACPI_FAILURE(status))
1000		printf("can't evaluate %s._DCS - %s\n",
1001		       acpi_name(handle), AcpiFormatException(status));
1002
1003	return (dcs);
1004}
1005
1006static UINT32
1007vo_get_graphics_state(ACPI_HANDLE handle)
1008{
1009	UINT32 dgs;
1010	ACPI_STATUS status;
1011
1012	dgs = 0;
1013	status = acpi_GetInteger(handle, "_DGS", &dgs);
1014	if (ACPI_FAILURE(status))
1015		printf("can't evaluate %s._DGS - %s\n",
1016		       acpi_name(handle), AcpiFormatException(status));
1017
1018	return (dgs);
1019}
1020
1021static void
1022vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1023{
1024	ACPI_STATUS status;
1025
1026	ACPI_SERIAL_ASSERT(video_output);
1027	status = acpi_SetInteger(handle, "_DSS", state);
1028	if (ACPI_FAILURE(status))
1029		printf("can't evaluate %s._DSS - %s\n",
1030		       acpi_name(handle), AcpiFormatException(status));
1031}
1032