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