acpi_video.c revision 209064
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 209064 2010-06-11 19:53:42Z 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_CYCLE_BRN	0x85
107#define	VID_NOTIFY_INC_BRN	0x86
108#define	VID_NOTIFY_DEC_BRN	0x87
109#define	VID_NOTIFY_ZERO_BRN	0x88
110
111/* _DOS (Enable/Disable Output Switching) argument bits */
112#define	DOS_SWITCH_MASK		3
113#define	DOS_SWITCH_BY_OSPM	0
114#define	DOS_SWITCH_BY_BIOS	1
115#define	DOS_SWITCH_LOCKED	2
116#define	DOS_BRIGHTNESS_BY_OSPM	(1 << 2)
117
118/* _DOD and subdev's _ADR */
119#define	DOD_DEVID_MASK		0x0f00
120#define	DOD_DEVID_MASK_FULL	0xffff
121#define	DOD_DEVID_MASK_DISPIDX	0x000f
122#define	DOD_DEVID_MASK_DISPPORT	0x00f0
123#define	DOD_DEVID_MONITOR	0x0100
124#define	DOD_DEVID_LCD		0x0110
125#define	DOD_DEVID_TV		0x0200
126#define	DOD_DEVID_EXT		0x0300
127#define	DOD_DEVID_INTDFP	0x0400
128#define	DOD_BIOS		(1 << 16)
129#define	DOD_NONVGA		(1 << 17)
130#define	DOD_HEAD_ID_SHIFT	18
131#define	DOD_HEAD_ID_BITS	3
132#define	DOD_HEAD_ID_MASK \
133		(((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
134#define	DOD_DEVID_SCHEME_STD	(1 << 31)
135
136/* _BCL related constants */
137#define	BCL_FULLPOWER		0
138#define	BCL_ECONOMY		1
139
140/* _DCS (Device Currrent Status) value bits and masks. */
141#define	DCS_EXISTS		(1 << 0)
142#define	DCS_ACTIVE		(1 << 1)
143#define	DCS_READY		(1 << 2)
144#define	DCS_FUNCTIONAL		(1 << 3)
145#define	DCS_ATTACHED		(1 << 4)
146
147/* _DSS (Device Set Status) argument bits and masks. */
148#define	DSS_INACTIVE		0
149#define	DSS_ACTIVE		(1 << 0)
150#define	DSS_SETNEXT		(1 << 30)
151#define	DSS_COMMIT		(1 << 31)
152
153static device_method_t acpi_video_methods[] = {
154	DEVMETHOD(device_identify, acpi_video_identify),
155	DEVMETHOD(device_probe, acpi_video_probe),
156	DEVMETHOD(device_attach, acpi_video_attach),
157	DEVMETHOD(device_detach, acpi_video_detach),
158	DEVMETHOD(device_shutdown, acpi_video_shutdown),
159	{ 0, 0 }
160};
161
162static driver_t acpi_video_driver = {
163	"acpi_video",
164	acpi_video_methods,
165	sizeof(struct acpi_video_softc),
166};
167
168static devclass_t acpi_video_devclass;
169
170DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
171	      acpi_video_modevent, NULL);
172MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
173
174static struct sysctl_ctx_list	acpi_video_sysctl_ctx;
175static struct sysctl_oid	*acpi_video_sysctl_tree;
176static struct acpi_video_output_queue crt_units, tv_units,
177    ext_units, lcd_units, other_units;
178
179/*
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.
183 */
184ACPI_SERIAL_DECL(video, "ACPI video");
185ACPI_SERIAL_DECL(video_output, "ACPI video output");
186MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
187
188static int
189acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
190{
191	int err;
192
193	err = 0;
194	switch (evt) {
195	case MOD_LOAD:
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);
202		break;
203	case MOD_UNLOAD:
204		sysctl_ctx_free(&acpi_video_sysctl_ctx);
205		acpi_video_sysctl_tree = NULL;
206		break;
207	default:
208		err = EINVAL;
209	}
210
211	return (err);
212}
213
214static void
215acpi_video_identify(driver_t *driver, device_t parent)
216{
217
218	if (device_find_child(parent, "acpi_video", -1) == NULL)
219		device_add_child(parent, "acpi_video", -1);
220}
221
222static int
223acpi_video_probe(device_t dev)
224{
225	ACPI_HANDLE devh, h;
226	ACPI_OBJECT_TYPE t_dos;
227
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)
234		return (ENXIO);
235
236	device_set_desc(dev, "ACPI video extension");
237	return (0);
238}
239
240static int
241acpi_video_attach(device_t dev)
242{
243	struct acpi_softc *acpi_sc;
244	struct acpi_video_softc *sc;
245
246	sc = device_get_softc(dev);
247
248	acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
249	if (acpi_sc == NULL)
250		return (ENXIO);
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");
257	}
258	ACPI_SERIAL_END(video);
259
260	sc->device = dev;
261	sc->handle = acpi_get_handle(dev);
262	STAILQ_INIT(&sc->vid_outputs);
263
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);
268
269	ACPI_SERIAL_BEGIN(video);
270	acpi_video_bind_outputs(sc);
271	ACPI_SERIAL_END(video);
272
273	/*
274	 * Notify the BIOS that we want to switch both active outputs and
275	 * brightness levels.
276	 */
277	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
278	    DOS_BRIGHTNESS_BY_OSPM);
279
280	acpi_video_power_profile(sc);
281
282	return (0);
283}
284
285static int
286acpi_video_detach(device_t dev)
287{
288	struct acpi_video_softc *sc;
289	struct acpi_video_output *vo, *vn;
290
291	sc = device_get_softc(dev);
292
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);
297
298	ACPI_SERIAL_BEGIN(video);
299	STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
300		acpi_video_vo_destroy(vo);
301	}
302	ACPI_SERIAL_END(video);
303
304	return (0);
305}
306
307static int
308acpi_video_shutdown(device_t dev)
309{
310	struct acpi_video_softc *sc;
311
312	sc = device_get_softc(dev);
313	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
314
315	return (0);
316}
317
318static void
319acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
320{
321	struct acpi_video_softc *sc;
322	struct acpi_video_output *vo, *vo_tmp;
323	ACPI_HANDLE lasthand;
324	UINT32 dcs, dss, dss_p;
325
326	sc = (struct acpi_video_softc *)context;
327
328	switch (notify) {
329	case VID_NOTIFY_SWITCHED:
330		dss_p = 0;
331		lasthand = NULL;
332		ACPI_SERIAL_BEGIN(video);
333		ACPI_SERIAL_BEGIN(video_output);
334		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
335			dss = vo_get_graphics_state(vo->handle);
336			dcs = vo_get_device_status(vo->handle);
337			if (!(dcs & DCS_READY))
338				dss = DSS_INACTIVE;
339			if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
340			    (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
341				if (lasthand != NULL)
342					vo_set_device_state(lasthand, dss_p);
343				dss_p = dss;
344				lasthand = vo->handle;
345			}
346		}
347		if (lasthand != NULL)
348			vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
349		ACPI_SERIAL_END(video_output);
350		ACPI_SERIAL_END(video);
351		break;
352	case VID_NOTIFY_REPROBE:
353		ACPI_SERIAL_BEGIN(video);
354		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
355			vo->handle = NULL;
356		acpi_video_bind_outputs(sc);
357		STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
358			if (vo->handle == NULL) {
359				STAILQ_REMOVE(&sc->vid_outputs, vo,
360				    acpi_video_output, vo_next);
361				acpi_video_vo_destroy(vo);
362			}
363		}
364		ACPI_SERIAL_END(video);
365		break;
366	default:
367		device_printf(sc->device, "unknown notify event 0x%x\n",
368		    notify);
369	}
370}
371
372static void
373acpi_video_power_profile(void *context)
374{
375	int state;
376	struct acpi_video_softc *sc;
377	struct acpi_video_output *vo;
378
379	sc = context;
380	state = power_profile_get_state();
381	if (state != POWER_PROFILE_PERFORMANCE &&
382	    state != POWER_PROFILE_ECONOMY)
383		return;
384
385	ACPI_SERIAL_BEGIN(video);
386	ACPI_SERIAL_BEGIN(video_output);
387	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
388		if (vo->vo_levels != NULL && vo->vo_brightness == -1)
389			vo_set_brightness(vo->handle,
390			    state == POWER_PROFILE_ECONOMY ?
391			    vo->vo_economy : vo->vo_fullpower);
392	}
393	ACPI_SERIAL_END(video_output);
394	ACPI_SERIAL_END(video);
395}
396
397static void
398acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
399{
400	struct acpi_video_softc *sc;
401	struct acpi_video_output *vo;
402
403	ACPI_SERIAL_ASSERT(video);
404	sc = context;
405
406	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
407		if (vo->adr == adr) {
408			acpi_video_vo_bind(vo, handle);
409			return;
410		}
411	}
412	vo = acpi_video_vo_init(adr);
413	if (vo != NULL) {
414		acpi_video_vo_bind(vo, handle);
415		STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
416	}
417}
418
419static void
420acpi_video_bind_outputs(struct acpi_video_softc *sc)
421{
422
423	ACPI_SERIAL_ASSERT(video);
424	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
425}
426
427static struct acpi_video_output *
428acpi_video_vo_init(UINT32 adr)
429{
430	struct acpi_video_output *vn, *vo, *vp;
431	int n, x;
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
438	switch (adr & DOD_DEVID_MASK) {
439	case DOD_DEVID_MONITOR:
440		if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
441			/* DOD_DEVID_LCD is a common, backward compatible ID */
442			desc = "Internal/Integrated Digital Flat Panel";
443			type = "lcd";
444			voqh = &lcd_units;
445		} else {
446			desc = "VGA CRT or VESA Compatible Analog Monitor";
447			type = "crt";
448			voqh = &crt_units;
449		}
450		break;
451	case DOD_DEVID_TV:
452		desc = "TV/HDTV or Analog-Video Monitor";
453		type = "tv";
454		voqh = &tv_units;
455		break;
456	case DOD_DEVID_EXT:
457		desc = "External Digital Monitor";
458		type = "ext";
459		voqh = &ext_units;
460		break;
461	case DOD_DEVID_INTDFP:
462		desc = "Internal/Integrated Digital Flat Panel";
463		type = "lcd";
464		voqh = &lcd_units;
465		break;
466	default:
467		desc = "unknown output";
468		type = "out";
469		voqh = &other_units;
470	}
471
472	n = 0;
473	vp = NULL;
474	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
475		if (vn->vo_unit.num != n)
476			break;
477		vp = vn;
478		n++;
479	}
480
481	snprintf(name, sizeof(name), "%s%d", type, n);
482
483	vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
484	if (vo != NULL) {
485		vo->handle = NULL;
486		vo->adr = adr;
487		vo->vo_unit.num = n;
488		vo->vo_brightness = -1;
489		vo->vo_fullpower = -1;	/* TODO: override with tunables */
490		vo->vo_economy = -1;
491		vo->vo_numlevels = 0;
492		vo->vo_levels = NULL;
493		snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
494		if (getenv_int(env, &x))
495			vo->vo_fullpower = x;
496		snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
497		if (getenv_int(env, &x))
498			vo->vo_economy = x;
499
500		sysctl_ctx_init(&vo->vo_sysctl_ctx);
501		if (vp != NULL)
502			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
503		else
504			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
505		if (acpi_video_sysctl_tree != NULL)
506			vo->vo_sysctl_tree =
507			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
508				SYSCTL_CHILDREN(acpi_video_sysctl_tree),
509				OID_AUTO, name, CTLFLAG_RD, 0, desc);
510		if (vo->vo_sysctl_tree != NULL) {
511			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
512			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
513			    OID_AUTO, "active",
514			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
515			    acpi_video_vo_active_sysctl, "I",
516			    "current activity of this device");
517			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
518			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
519			    OID_AUTO, "brightness",
520			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
521			    acpi_video_vo_bright_sysctl, "I",
522			    "current brightness level");
523			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
524			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
525			    OID_AUTO, "fullpower",
526			    CTLTYPE_INT|CTLFLAG_RW, vo,
527			    POWER_PROFILE_PERFORMANCE,
528			    acpi_video_vo_presets_sysctl, "I",
529			    "preset level for full power mode");
530			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
531			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
532			    OID_AUTO, "economy",
533			    CTLTYPE_INT|CTLFLAG_RW, vo,
534			    POWER_PROFILE_ECONOMY,
535			    acpi_video_vo_presets_sysctl, "I",
536			    "preset level for economy mode");
537			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
538			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
539			    OID_AUTO, "levels",
540			    CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
541			    acpi_video_vo_levels_sysctl, "I",
542			    "supported brightness levels");
543		} else
544			printf("%s: sysctl node creation failed\n", type);
545	} else
546		printf("%s: softc allocation failed\n", type);
547
548	if (bootverbose) {
549		printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
550		printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
551		printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
552		if (adr & DOD_BIOS)
553			printf(", detectable by BIOS");
554		if (adr & DOD_NONVGA)
555			printf(" (Non-VGA output device whose power "
556			    "is related to the VGA device)");
557		printf(", head #%d\n",
558			(adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
559	}
560	return (vo);
561}
562
563static void
564acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
565{
566
567	ACPI_SERIAL_BEGIN(video_output);
568	if (vo->vo_levels != NULL)
569		AcpiOsFree(vo->vo_levels);
570	vo->handle = handle;
571	vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
572	if (vo->vo_numlevels >= 2) {
573		if (vo->vo_fullpower == -1
574		    || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
575			/* XXX - can't deal with rebinding... */
576			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
577		if (vo->vo_economy == -1
578		    || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
579			/* XXX - see above. */
580			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
581	}
582	if (vo->vo_levels != NULL)
583		AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
584		    acpi_video_vo_notify_handler, vo);
585	ACPI_SERIAL_END(video_output);
586}
587
588static void
589acpi_video_vo_destroy(struct acpi_video_output *vo)
590{
591	struct acpi_video_output_queue *voqh;
592
593	ACPI_SERIAL_ASSERT(video);
594	if (vo->vo_sysctl_tree != NULL) {
595		vo->vo_sysctl_tree = NULL;
596		sysctl_ctx_free(&vo->vo_sysctl_ctx);
597	}
598	if (vo->vo_levels != NULL) {
599		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
600		    acpi_video_vo_notify_handler);
601		AcpiOsFree(vo->vo_levels);
602	}
603
604	switch (vo->adr & DOD_DEVID_MASK) {
605	case DOD_DEVID_MONITOR:
606		voqh = &crt_units;
607		break;
608	case DOD_DEVID_TV:
609		voqh = &tv_units;
610		break;
611	case DOD_DEVID_EXT:
612		voqh = &ext_units;
613		break;
614	case DOD_DEVID_INTDFP:
615		voqh = &lcd_units;
616		break;
617	default:
618		voqh = &other_units;
619	}
620	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
621	free(vo, M_ACPIVIDEO);
622}
623
624static int
625acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
626{
627	int i;
628
629	ACPI_SERIAL_ASSERT(video_output);
630	if (vo->vo_levels == NULL)
631		return (ENODEV);
632	for (i = 0; i < vo->vo_numlevels; i++)
633		if (vo->vo_levels[i] == level)
634			return (0);
635	return (EINVAL);
636}
637
638static void
639acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
640{
641	struct acpi_video_output *vo;
642	int i, j, level, new_level;
643
644	vo = context;
645	ACPI_SERIAL_BEGIN(video_output);
646	if (vo->handle != handle)
647		goto out;
648
649	switch (notify) {
650	case VID_NOTIFY_CYCLE_BRN:
651		if (vo->vo_numlevels <= 3)
652			goto out;
653		/* FALLTHROUGH */
654	case VID_NOTIFY_INC_BRN:
655	case VID_NOTIFY_DEC_BRN:
656	case VID_NOTIFY_ZERO_BRN:
657		if (vo->vo_levels == NULL)
658			goto out;
659		level = vo_get_brightness(handle);
660		if (level < 0)
661			goto out;
662		break;
663	default:
664		printf("unknown notify event 0x%x from %s\n",
665		    notify, acpi_name(handle));
666		goto out;
667	}
668
669	new_level = level;
670	switch (notify) {
671	case VID_NOTIFY_CYCLE_BRN:
672		for (i = 2; i < vo->vo_numlevels; i++)
673			if (vo->vo_levels[i] == level) {
674				new_level = vo->vo_numlevels > i + 1 ?
675				     vo->vo_levels[i + 1] : vo->vo_levels[2];
676				break;
677			}
678		break;
679	case VID_NOTIFY_INC_BRN:
680	case VID_NOTIFY_DEC_BRN:
681		for (i = 0; i < vo->vo_numlevels; i++) {
682			j = vo->vo_levels[i];
683			if (notify == VID_NOTIFY_INC_BRN) {
684				if (j > level &&
685				    (j < new_level || level == new_level))
686					new_level = j;
687			} else {
688				if (j < level &&
689				    (j > new_level || level == new_level))
690					new_level = j;
691			}
692		}
693		break;
694	case VID_NOTIFY_ZERO_BRN:
695		for (i = 0; i < vo->vo_numlevels; i++)
696			if (vo->vo_levels[i] == 0) {
697				new_level = 0;
698				break;
699			}
700		break;
701	}
702	if (new_level != level) {
703		vo_set_brightness(handle, new_level);
704		vo->vo_brightness = new_level;
705	}
706
707out:
708	ACPI_SERIAL_END(video_output);
709}
710
711/* ARGSUSED */
712static int
713acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
714{
715	struct acpi_video_output *vo;
716	int state, err;
717
718	vo = (struct acpi_video_output *)arg1;
719	if (vo->handle == NULL)
720		return (ENXIO);
721	ACPI_SERIAL_BEGIN(video_output);
722	state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
723	err = sysctl_handle_int(oidp, &state, 0, req);
724	if (err != 0 || req->newptr == NULL)
725		goto out;
726	vo_set_device_state(vo->handle,
727	    DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
728out:
729	ACPI_SERIAL_END(video_output);
730	return (err);
731}
732
733/* ARGSUSED */
734static int
735acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
736{
737	struct acpi_video_output *vo;
738	int level, preset, err;
739
740	vo = (struct acpi_video_output *)arg1;
741	ACPI_SERIAL_BEGIN(video_output);
742	if (vo->handle == NULL) {
743		err = ENXIO;
744		goto out;
745	}
746	if (vo->vo_levels == NULL) {
747		err = ENODEV;
748		goto out;
749	}
750
751	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
752		  vo->vo_economy : vo->vo_fullpower;
753	level = vo->vo_brightness;
754	if (level == -1)
755		level = preset;
756
757	err = sysctl_handle_int(oidp, &level, 0, req);
758	if (err != 0 || req->newptr == NULL)
759		goto out;
760	if (level < -1 || level > 100) {
761		err = EINVAL;
762		goto out;
763	}
764
765	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
766		goto out;
767	vo->vo_brightness = level;
768	vo_set_brightness(vo->handle, (level == -1) ? preset : level);
769
770out:
771	ACPI_SERIAL_END(video_output);
772	return (err);
773}
774
775static int
776acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
777{
778	struct acpi_video_output *vo;
779	int i, level, *preset, err;
780
781	vo = (struct acpi_video_output *)arg1;
782	ACPI_SERIAL_BEGIN(video_output);
783	if (vo->handle == NULL) {
784		err = ENXIO;
785		goto out;
786	}
787	if (vo->vo_levels == NULL) {
788		err = ENODEV;
789		goto out;
790	}
791	preset = (arg2 == POWER_PROFILE_ECONOMY) ?
792		  &vo->vo_economy : &vo->vo_fullpower;
793	level = *preset;
794	err = sysctl_handle_int(oidp, &level, 0, req);
795	if (err != 0 || req->newptr == NULL)
796		goto out;
797	if (level < -1 || level > 100) {
798		err = EINVAL;
799		goto out;
800	}
801	if (level == -1) {
802		i = (arg2 == POWER_PROFILE_ECONOMY) ?
803		    BCL_ECONOMY : BCL_FULLPOWER;
804		level = vo->vo_levels[i];
805	} else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
806		goto out;
807
808	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
809		vo_set_brightness(vo->handle, level);
810	*preset = level;
811
812out:
813	ACPI_SERIAL_END(video_output);
814	return (err);
815}
816
817/* ARGSUSED */
818static int
819acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
820{
821	struct acpi_video_output *vo;
822	int err;
823
824	vo = (struct acpi_video_output *)arg1;
825	ACPI_SERIAL_BEGIN(video_output);
826	if (vo->vo_levels == NULL) {
827		err = ENODEV;
828		goto out;
829	}
830	if (req->newptr != NULL) {
831		err = EPERM;
832		goto out;
833	}
834	err = sysctl_handle_opaque(oidp, vo->vo_levels,
835	    vo->vo_numlevels * sizeof(*vo->vo_levels), req);
836
837out:
838	ACPI_SERIAL_END(video_output);
839	return (err);
840}
841
842static void
843vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
844{
845	ACPI_STATUS status;
846
847	status = acpi_SetInteger(handle, "_DOS", policy);
848	if (ACPI_FAILURE(status))
849		printf("can't evaluate %s._DOS - %s\n",
850		       acpi_name(handle), AcpiFormatException(status));
851}
852
853struct enum_callback_arg {
854	void (*callback)(ACPI_HANDLE, UINT32, void *);
855	void *context;
856	ACPI_OBJECT *dod_pkg;
857	int count;
858};
859
860static ACPI_STATUS
861vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
862		      void *context, void **retp __unused)
863{
864	ACPI_STATUS status;
865	UINT32 adr, val;
866	struct enum_callback_arg *argset;
867	size_t i;
868
869	ACPI_SERIAL_ASSERT(video);
870	argset = context;
871	status = acpi_GetInteger(handle, "_ADR", &adr);
872	if (ACPI_FAILURE(status))
873		return (AE_OK);
874
875	for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
876		if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
877		    (val & DOD_DEVID_MASK_FULL) == adr) {
878			argset->callback(handle, val, argset->context);
879			argset->count++;
880		}
881	}
882
883	return (AE_OK);
884}
885
886static int
887vid_enum_outputs(ACPI_HANDLE handle,
888		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
889{
890	ACPI_STATUS status;
891	ACPI_BUFFER dod_buf;
892	ACPI_OBJECT *res;
893	struct enum_callback_arg argset;
894
895	ACPI_SERIAL_ASSERT(video);
896	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
897	dod_buf.Pointer = NULL;
898	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
899	if (ACPI_FAILURE(status)) {
900		if (status != AE_NOT_FOUND)
901			printf("can't evaluate %s._DOD - %s\n",
902			       acpi_name(handle), AcpiFormatException(status));
903		argset.count = -1;
904		goto out;
905	}
906	res = (ACPI_OBJECT *)dod_buf.Pointer;
907	if (!ACPI_PKG_VALID(res, 1)) {
908		printf("evaluation of %s._DOD makes no sense\n",
909		       acpi_name(handle));
910		argset.count = -1;
911		goto out;
912	}
913	if (callback == NULL) {
914		argset.count = res->Package.Count;
915		goto out;
916	}
917	argset.callback = callback;
918	argset.context  = context;
919	argset.dod_pkg  = res;
920	argset.count    = 0;
921	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
922	    vid_enum_outputs_subr, NULL, &argset, NULL);
923	if (ACPI_FAILURE(status))
924		printf("failed walking down %s - %s\n",
925		       acpi_name(handle), AcpiFormatException(status));
926out:
927	if (dod_buf.Pointer != NULL)
928		AcpiOsFree(dod_buf.Pointer);
929	return (argset.count);
930}
931
932static int
933vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
934{
935	ACPI_STATUS status;
936	ACPI_BUFFER bcl_buf;
937	ACPI_OBJECT *res;
938	int num, i, n, *levels;
939
940	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
941	bcl_buf.Pointer = NULL;
942	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
943	if (ACPI_FAILURE(status)) {
944		if (status != AE_NOT_FOUND)
945			printf("can't evaluate %s._BCL - %s\n",
946			       acpi_name(handle), AcpiFormatException(status));
947		num = -1;
948		goto out;
949	}
950	res = (ACPI_OBJECT *)bcl_buf.Pointer;
951	if (!ACPI_PKG_VALID(res, 2)) {
952		printf("evaluation of %s._BCL makes no sense\n",
953		       acpi_name(handle));
954		num = -1;
955		goto out;
956	}
957	num = res->Package.Count;
958	if (levelp == NULL)
959		goto out;
960	levels = AcpiOsAllocate(num * sizeof(*levels));
961	if (levels == NULL) {
962		num = -1;
963		goto out;
964	}
965	for (i = 0, n = 0; i < num; i++)
966		if (acpi_PkgInt32(res, i, &levels[n]) == 0)
967			n++;
968	if (n < 2) {
969		num = -1;
970		AcpiOsFree(levels);
971	} else {
972		num = n;
973		*levelp = levels;
974	}
975out:
976	if (bcl_buf.Pointer != NULL)
977		AcpiOsFree(bcl_buf.Pointer);
978
979	return (num);
980}
981
982static int
983vo_get_brightness(ACPI_HANDLE handle)
984{
985	UINT32 level;
986	ACPI_STATUS status;
987
988	ACPI_SERIAL_ASSERT(video_output);
989	status = acpi_GetInteger(handle, "_BQC", &level);
990	if (ACPI_FAILURE(status)) {
991		printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
992		    AcpiFormatException(status));
993		return (-1);
994	}
995	if (level > 100)
996		return (-1);
997
998	return (level);
999}
1000
1001static void
1002vo_set_brightness(ACPI_HANDLE handle, int level)
1003{
1004	ACPI_STATUS status;
1005
1006	ACPI_SERIAL_ASSERT(video_output);
1007	status = acpi_SetInteger(handle, "_BCM", level);
1008	if (ACPI_FAILURE(status))
1009		printf("can't evaluate %s._BCM - %s\n",
1010		       acpi_name(handle), AcpiFormatException(status));
1011}
1012
1013static UINT32
1014vo_get_device_status(ACPI_HANDLE handle)
1015{
1016	UINT32 dcs;
1017	ACPI_STATUS status;
1018
1019	ACPI_SERIAL_ASSERT(video_output);
1020	dcs = 0;
1021	status = acpi_GetInteger(handle, "_DCS", &dcs);
1022	if (ACPI_FAILURE(status))
1023		printf("can't evaluate %s._DCS - %s\n",
1024		       acpi_name(handle), AcpiFormatException(status));
1025
1026	return (dcs);
1027}
1028
1029static UINT32
1030vo_get_graphics_state(ACPI_HANDLE handle)
1031{
1032	UINT32 dgs;
1033	ACPI_STATUS status;
1034
1035	dgs = 0;
1036	status = acpi_GetInteger(handle, "_DGS", &dgs);
1037	if (ACPI_FAILURE(status))
1038		printf("can't evaluate %s._DGS - %s\n",
1039		       acpi_name(handle), AcpiFormatException(status));
1040
1041	return (dgs);
1042}
1043
1044static void
1045vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1046{
1047	ACPI_STATUS status;
1048
1049	ACPI_SERIAL_ASSERT(video_output);
1050	status = acpi_SetInteger(handle, "_DSS", state);
1051	if (ACPI_FAILURE(status))
1052		printf("can't evaluate %s._DSS - %s\n",
1053		       acpi_name(handle), AcpiFormatException(status));
1054}
1055