xref: /freebsd/sys/dev/acpica/acpi_video.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
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$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.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>
41 
42 #include <contrib/dev/acpica/include/acpi.h>
43 
44 #include <dev/acpica/acpivar.h>
45 
46 /* ACPI video extension driver. */
47 struct acpi_video_output {
48 	ACPI_HANDLE	handle;
49 	UINT32		adr;
50 	STAILQ_ENTRY(acpi_video_output) vo_next;
51 	struct {
52 		int	num;
53 		STAILQ_ENTRY(acpi_video_output) next;
54 	} vo_unit;
55 	int		vo_brightness;
56 	int		vo_fullpower;
57 	int		vo_economy;
58 	int		vo_numlevels;
59 	int		*vo_levels;
60 	struct sysctl_ctx_list vo_sysctl_ctx;
61 	struct sysctl_oid *vo_sysctl_tree;
62 };
63 
64 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
65 
66 struct acpi_video_softc {
67 	device_t		device;
68 	ACPI_HANDLE		handle;
69 	struct acpi_video_output_queue vid_outputs;
70 	eventhandler_tag	vid_pwr_evh;
71 };
72 
73 /* interfaces */
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);
93 
94 /* operations */
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);
104 
105 /* events */
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
112 
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)
119 
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)
137 
138 /* _BCL related constants */
139 #define	BCL_FULLPOWER		0
140 #define	BCL_ECONOMY		1
141 
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)
148 
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)
154 
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),
162 	{ 0, 0 }
163 };
164 
165 static driver_t acpi_video_driver = {
166 	"acpi_video",
167 	acpi_video_methods,
168 	sizeof(struct acpi_video_softc),
169 };
170 
171 static devclass_t acpi_video_devclass;
172 
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);
176 
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;
181 
182 /*
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.
186  */
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");
190 
191 static int
192 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
193 {
194 	int err;
195 
196 	err = 0;
197 	switch (evt) {
198 	case MOD_LOAD:
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);
205 		break;
206 	case MOD_UNLOAD:
207 		sysctl_ctx_free(&acpi_video_sysctl_ctx);
208 		acpi_video_sysctl_tree = NULL;
209 		break;
210 	default:
211 		err = EINVAL;
212 	}
213 
214 	return (err);
215 }
216 
217 static void
218 acpi_video_identify(driver_t *driver, device_t parent)
219 {
220 
221 	if (device_find_child(parent, "acpi_video", -1) == NULL)
222 		device_add_child(parent, "acpi_video", -1);
223 }
224 
225 static int
226 acpi_video_probe(device_t dev)
227 {
228 	ACPI_HANDLE devh, h;
229 	ACPI_OBJECT_TYPE t_dos;
230 
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)
237 		return (ENXIO);
238 
239 	device_set_desc(dev, "ACPI video extension");
240 	return (0);
241 }
242 
243 static int
244 acpi_video_attach(device_t dev)
245 {
246 	struct acpi_softc *acpi_sc;
247 	struct acpi_video_softc *sc;
248 
249 	sc = device_get_softc(dev);
250 
251 	acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
252 	if (acpi_sc == NULL)
253 		return (ENXIO);
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), OID_AUTO,
258 		    "video", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
259 		    "video extension control");
260 	}
261 	ACPI_SERIAL_END(video);
262 
263 	sc->device = dev;
264 	sc->handle = acpi_get_handle(dev);
265 	STAILQ_INIT(&sc->vid_outputs);
266 
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);
271 
272 	ACPI_SERIAL_BEGIN(video);
273 	acpi_video_bind_outputs(sc);
274 	ACPI_SERIAL_END(video);
275 
276 	/*
277 	 * Notify the BIOS that we want to switch both active outputs and
278 	 * brightness levels.
279 	 */
280 	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
281 	    DOS_BRIGHTNESS_BY_OSPM);
282 
283 	acpi_video_power_profile(sc);
284 
285 	return (0);
286 }
287 
288 static int
289 acpi_video_detach(device_t dev)
290 {
291 	struct acpi_video_softc *sc;
292 	struct acpi_video_output *vo, *vn;
293 
294 	sc = device_get_softc(dev);
295 
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);
300 
301 	ACPI_SERIAL_BEGIN(video);
302 	STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
303 		acpi_video_vo_destroy(vo);
304 	}
305 	ACPI_SERIAL_END(video);
306 
307 	return (0);
308 }
309 
310 static int
311 acpi_video_resume(device_t dev)
312 {
313 	struct acpi_video_softc *sc;
314 	struct acpi_video_output *vo, *vn;
315 	int level;
316 
317 	sc = device_get_softc(dev);
318 
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)
325 			continue;
326 
327 		if ((vo_get_device_status(vo->handle) & DCS_ACTIVE) == 0)
328 			continue;
329 
330 		level = vo_get_brightness(vo->handle);
331 		if (level != -1)
332 			vo_set_brightness(vo->handle, level);
333 	}
334 	ACPI_SERIAL_END(video_output);
335 	ACPI_SERIAL_END(video);
336 
337 	return (0);
338 }
339 
340 static int
341 acpi_video_shutdown(device_t dev)
342 {
343 	struct acpi_video_softc *sc;
344 
345 	sc = device_get_softc(dev);
346 	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
347 
348 	return (0);
349 }
350 
351 static void
352 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
353 {
354 	struct acpi_video_softc *sc;
355 	struct acpi_video_output *vo, *vo_tmp;
356 	ACPI_HANDLE lasthand;
357 	UINT32 dcs, dss, dss_p;
358 
359 	sc = (struct acpi_video_softc *)context;
360 
361 	switch (notify) {
362 	case VID_NOTIFY_SWITCHED:
363 		dss_p = 0;
364 		lasthand = NULL;
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))
371 				dss = DSS_INACTIVE;
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);
376 				dss_p = dss;
377 				lasthand = vo->handle;
378 			}
379 		}
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);
384 		break;
385 	case VID_NOTIFY_REPROBE:
386 		ACPI_SERIAL_BEGIN(video);
387 		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
388 			vo->handle = NULL;
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);
395 			}
396 		}
397 		ACPI_SERIAL_END(video);
398 		break;
399 	default:
400 		device_printf(sc->device, "unknown notify event 0x%x\n",
401 		    notify);
402 	}
403 }
404 
405 static void
406 acpi_video_power_profile(void *context)
407 {
408 	int state;
409 	struct acpi_video_softc *sc;
410 	struct acpi_video_output *vo;
411 
412 	sc = context;
413 	state = power_profile_get_state();
414 	if (state != POWER_PROFILE_PERFORMANCE &&
415 	    state != POWER_PROFILE_ECONOMY)
416 		return;
417 
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);
425 	}
426 	ACPI_SERIAL_END(video_output);
427 	ACPI_SERIAL_END(video);
428 }
429 
430 static void
431 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
432 {
433 	struct acpi_video_softc *sc;
434 	struct acpi_video_output *vo;
435 
436 	ACPI_SERIAL_ASSERT(video);
437 	sc = context;
438 
439 	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
440 		if (vo->adr == adr) {
441 			acpi_video_vo_bind(vo, handle);
442 			return;
443 		}
444 	}
445 	vo = acpi_video_vo_init(adr);
446 	if (vo != NULL) {
447 		acpi_video_vo_bind(vo, handle);
448 		STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
449 	}
450 }
451 
452 static void
453 acpi_video_bind_outputs(struct acpi_video_softc *sc)
454 {
455 
456 	ACPI_SERIAL_ASSERT(video);
457 	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
458 }
459 
460 static struct acpi_video_output *
461 acpi_video_vo_init(UINT32 adr)
462 {
463 	struct acpi_video_output *vn, *vo, *vp;
464 	int n, x;
465 	char name[8], env[32];
466 	const char *type, *desc;
467 	struct acpi_video_output_queue *voqh;
468 
469 	ACPI_SERIAL_ASSERT(video);
470 
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";
476 			type = "lcd";
477 			voqh = &lcd_units;
478 		} else {
479 			desc = "VGA CRT or VESA Compatible Analog Monitor";
480 			type = "crt";
481 			voqh = &crt_units;
482 		}
483 		break;
484 	case DOD_DEVID_TV:
485 		desc = "TV/HDTV or Analog-Video Monitor";
486 		type = "tv";
487 		voqh = &tv_units;
488 		break;
489 	case DOD_DEVID_EXT:
490 		desc = "External Digital Monitor";
491 		type = "ext";
492 		voqh = &ext_units;
493 		break;
494 	case DOD_DEVID_INTDFP:
495 		desc = "Internal/Integrated Digital Flat Panel";
496 		type = "lcd";
497 		voqh = &lcd_units;
498 		break;
499 	default:
500 		desc = "unknown output";
501 		type = "out";
502 		voqh = &other_units;
503 	}
504 
505 	n = 0;
506 	vp = NULL;
507 	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
508 		if (vn->vo_unit.num != n)
509 			break;
510 		vp = vn;
511 		n++;
512 	}
513 
514 	snprintf(name, sizeof(name), "%s%d", type, n);
515 
516 	vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
517 	if (vo != NULL) {
518 		vo->handle = NULL;
519 		vo->adr = adr;
520 		vo->vo_unit.num = n;
521 		vo->vo_brightness = -1;
522 		vo->vo_fullpower = -1;	/* TODO: override with tunables */
523 		vo->vo_economy = -1;
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))
531 			vo->vo_economy = x;
532 
533 		sysctl_ctx_init(&vo->vo_sysctl_ctx);
534 		if (vp != NULL)
535 			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
536 		else
537 			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
538 		if (acpi_video_sysctl_tree != NULL)
539 			vo->vo_sysctl_tree =
540 			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
541 				SYSCTL_CHILDREN(acpi_video_sysctl_tree),
542 				OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE,
543 				0, desc);
544 		if (vo->vo_sysctl_tree != NULL) {
545 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
546 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
547 			    OID_AUTO, "active",
548 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
549 			    0, acpi_video_vo_active_sysctl, "I",
550 			    "current activity of this device");
551 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
552 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
553 			    OID_AUTO, "brightness",
554 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
555 			    0, acpi_video_vo_bright_sysctl, "I",
556 			    "current brightness level");
557 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
558 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
559 			    OID_AUTO, "fullpower",
560 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
561 			    POWER_PROFILE_PERFORMANCE,
562 			    acpi_video_vo_presets_sysctl, "I",
563 			    "preset level for full power mode");
564 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
565 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
566 			    OID_AUTO, "economy",
567 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, vo,
568 			    POWER_PROFILE_ECONOMY,
569 			    acpi_video_vo_presets_sysctl, "I",
570 			    "preset level for economy mode");
571 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
572 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
573 			    OID_AUTO, "levels",
574 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, vo,
575 			    0, acpi_video_vo_levels_sysctl, "I",
576 			    "supported brightness levels");
577 		} else
578 			printf("%s: sysctl node creation failed\n", type);
579 	} else
580 		printf("%s: softc allocation failed\n", type);
581 
582 	if (bootverbose) {
583 		printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
584 		printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
585 		printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
586 		if (adr & DOD_BIOS)
587 			printf(", detectable by BIOS");
588 		if (adr & DOD_NONVGA)
589 			printf(" (Non-VGA output device whose power "
590 			    "is related to the VGA device)");
591 		printf(", head #%d\n",
592 			(adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
593 	}
594 	return (vo);
595 }
596 
597 static void
598 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
599 {
600 
601 	ACPI_SERIAL_BEGIN(video_output);
602 	if (vo->vo_levels != NULL) {
603 		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
604 		    acpi_video_vo_notify_handler);
605 		AcpiOsFree(vo->vo_levels);
606 		vo->vo_levels = NULL;
607 	}
608 	vo->handle = handle;
609 	vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
610 	if (vo->vo_numlevels >= 2) {
611 		if (vo->vo_fullpower == -1 ||
612 		    acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0) {
613 			/* XXX - can't deal with rebinding... */
614 			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
615 		}
616 		if (vo->vo_economy == -1 ||
617 		    acpi_video_vo_check_level(vo, vo->vo_economy) != 0) {
618 			/* XXX - see above. */
619 			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
620 		}
621 		AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
622 		    acpi_video_vo_notify_handler, vo);
623 	}
624 	ACPI_SERIAL_END(video_output);
625 }
626 
627 static void
628 acpi_video_vo_destroy(struct acpi_video_output *vo)
629 {
630 	struct acpi_video_output_queue *voqh;
631 
632 	ACPI_SERIAL_ASSERT(video);
633 	if (vo->vo_sysctl_tree != NULL) {
634 		vo->vo_sysctl_tree = NULL;
635 		sysctl_ctx_free(&vo->vo_sysctl_ctx);
636 	}
637 	if (vo->vo_levels != NULL) {
638 		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
639 		    acpi_video_vo_notify_handler);
640 		AcpiOsFree(vo->vo_levels);
641 	}
642 
643 	switch (vo->adr & DOD_DEVID_MASK) {
644 	case DOD_DEVID_MONITOR:
645 		voqh = &crt_units;
646 		break;
647 	case DOD_DEVID_TV:
648 		voqh = &tv_units;
649 		break;
650 	case DOD_DEVID_EXT:
651 		voqh = &ext_units;
652 		break;
653 	case DOD_DEVID_INTDFP:
654 		voqh = &lcd_units;
655 		break;
656 	default:
657 		voqh = &other_units;
658 	}
659 	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
660 	free(vo, M_ACPIVIDEO);
661 }
662 
663 static int
664 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
665 {
666 	int i;
667 
668 	ACPI_SERIAL_ASSERT(video_output);
669 	if (vo->vo_levels == NULL)
670 		return (ENODEV);
671 	for (i = 0; i < vo->vo_numlevels; i++)
672 		if (vo->vo_levels[i] == level)
673 			return (0);
674 	return (EINVAL);
675 }
676 
677 static void
678 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
679 {
680 	struct acpi_video_output *vo;
681 	int i, j, level, new_level;
682 
683 	vo = context;
684 	ACPI_SERIAL_BEGIN(video_output);
685 	if (vo->handle != handle)
686 		goto out;
687 
688 	switch (notify) {
689 	case VID_NOTIFY_CYCLE_BRN:
690 		if (vo->vo_numlevels <= 3)
691 			goto out;
692 		/* FALLTHROUGH */
693 	case VID_NOTIFY_INC_BRN:
694 	case VID_NOTIFY_DEC_BRN:
695 	case VID_NOTIFY_ZERO_BRN:
696 		if (vo->vo_levels == NULL)
697 			goto out;
698 		level = vo_get_brightness(handle);
699 		if (level < 0)
700 			goto out;
701 		break;
702 	default:
703 		printf("unknown notify event 0x%x from %s\n",
704 		    notify, acpi_name(handle));
705 		goto out;
706 	}
707 
708 	new_level = level;
709 	switch (notify) {
710 	case VID_NOTIFY_CYCLE_BRN:
711 		for (i = 2; i < vo->vo_numlevels; i++)
712 			if (vo->vo_levels[i] == level) {
713 				new_level = vo->vo_numlevels > i + 1 ?
714 				     vo->vo_levels[i + 1] : vo->vo_levels[2];
715 				break;
716 			}
717 		break;
718 	case VID_NOTIFY_INC_BRN:
719 	case VID_NOTIFY_DEC_BRN:
720 		for (i = 0; i < vo->vo_numlevels; i++) {
721 			j = vo->vo_levels[i];
722 			if (notify == VID_NOTIFY_INC_BRN) {
723 				if (j > level &&
724 				    (j < new_level || level == new_level))
725 					new_level = j;
726 			} else {
727 				if (j < level &&
728 				    (j > new_level || level == new_level))
729 					new_level = j;
730 			}
731 		}
732 		break;
733 	case VID_NOTIFY_ZERO_BRN:
734 		for (i = 0; i < vo->vo_numlevels; i++)
735 			if (vo->vo_levels[i] == 0) {
736 				new_level = 0;
737 				break;
738 			}
739 		break;
740 	}
741 	if (new_level != level) {
742 		vo_set_brightness(handle, new_level);
743 		vo->vo_brightness = new_level;
744 	}
745 
746 out:
747 	ACPI_SERIAL_END(video_output);
748 }
749 
750 /* ARGSUSED */
751 static int
752 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
753 {
754 	struct acpi_video_output *vo;
755 	int state, err;
756 
757 	vo = (struct acpi_video_output *)arg1;
758 	if (vo->handle == NULL)
759 		return (ENXIO);
760 	ACPI_SERIAL_BEGIN(video_output);
761 	state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
762 	err = sysctl_handle_int(oidp, &state, 0, req);
763 	if (err != 0 || req->newptr == NULL)
764 		goto out;
765 	vo_set_device_state(vo->handle,
766 	    DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
767 out:
768 	ACPI_SERIAL_END(video_output);
769 	return (err);
770 }
771 
772 /* ARGSUSED */
773 static int
774 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
775 {
776 	struct acpi_video_output *vo;
777 	int level, preset, err;
778 
779 	vo = (struct acpi_video_output *)arg1;
780 	ACPI_SERIAL_BEGIN(video_output);
781 	if (vo->handle == NULL) {
782 		err = ENXIO;
783 		goto out;
784 	}
785 	if (vo->vo_levels == NULL) {
786 		err = ENODEV;
787 		goto out;
788 	}
789 
790 	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
791 		  vo->vo_economy : vo->vo_fullpower;
792 	level = vo->vo_brightness;
793 	if (level == -1)
794 		level = preset;
795 
796 	err = sysctl_handle_int(oidp, &level, 0, req);
797 	if (err != 0 || req->newptr == NULL)
798 		goto out;
799 	if (level < -1 || level > 100) {
800 		err = EINVAL;
801 		goto out;
802 	}
803 
804 	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
805 		goto out;
806 	vo->vo_brightness = level;
807 	vo_set_brightness(vo->handle, (level == -1) ? preset : level);
808 
809 out:
810 	ACPI_SERIAL_END(video_output);
811 	return (err);
812 }
813 
814 static int
815 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
816 {
817 	struct acpi_video_output *vo;
818 	int i, level, *preset, err;
819 
820 	vo = (struct acpi_video_output *)arg1;
821 	ACPI_SERIAL_BEGIN(video_output);
822 	if (vo->handle == NULL) {
823 		err = ENXIO;
824 		goto out;
825 	}
826 	if (vo->vo_levels == NULL) {
827 		err = ENODEV;
828 		goto out;
829 	}
830 	preset = (arg2 == POWER_PROFILE_ECONOMY) ?
831 		  &vo->vo_economy : &vo->vo_fullpower;
832 	level = *preset;
833 	err = sysctl_handle_int(oidp, &level, 0, req);
834 	if (err != 0 || req->newptr == NULL)
835 		goto out;
836 	if (level < -1 || level > 100) {
837 		err = EINVAL;
838 		goto out;
839 	}
840 	if (level == -1) {
841 		i = (arg2 == POWER_PROFILE_ECONOMY) ?
842 		    BCL_ECONOMY : BCL_FULLPOWER;
843 		level = vo->vo_levels[i];
844 	} else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
845 		goto out;
846 
847 	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
848 		vo_set_brightness(vo->handle, level);
849 	*preset = level;
850 
851 out:
852 	ACPI_SERIAL_END(video_output);
853 	return (err);
854 }
855 
856 /* ARGSUSED */
857 static int
858 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
859 {
860 	struct acpi_video_output *vo;
861 	int err;
862 
863 	vo = (struct acpi_video_output *)arg1;
864 	ACPI_SERIAL_BEGIN(video_output);
865 	if (vo->vo_levels == NULL) {
866 		err = ENODEV;
867 		goto out;
868 	}
869 	if (req->newptr != NULL) {
870 		err = EPERM;
871 		goto out;
872 	}
873 	err = sysctl_handle_opaque(oidp, vo->vo_levels,
874 	    vo->vo_numlevels * sizeof(*vo->vo_levels), req);
875 
876 out:
877 	ACPI_SERIAL_END(video_output);
878 	return (err);
879 }
880 
881 static void
882 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
883 {
884 	ACPI_STATUS status;
885 
886 	status = acpi_SetInteger(handle, "_DOS", policy);
887 	if (ACPI_FAILURE(status))
888 		printf("can't evaluate %s._DOS - %s\n",
889 		       acpi_name(handle), AcpiFormatException(status));
890 }
891 
892 struct enum_callback_arg {
893 	void (*callback)(ACPI_HANDLE, UINT32, void *);
894 	void *context;
895 	ACPI_OBJECT *dod_pkg;
896 	int count;
897 };
898 
899 static ACPI_STATUS
900 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
901 		      void *context, void **retp __unused)
902 {
903 	ACPI_STATUS status;
904 	UINT32 adr, val;
905 	struct enum_callback_arg *argset;
906 	size_t i;
907 
908 	ACPI_SERIAL_ASSERT(video);
909 	argset = context;
910 	status = acpi_GetInteger(handle, "_ADR", &adr);
911 	if (ACPI_FAILURE(status))
912 		return (AE_OK);
913 
914 	for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
915 		if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
916 		    (val & DOD_DEVID_MASK_FULL) ==
917 		    (adr & DOD_DEVID_MASK_FULL)) {
918 			argset->callback(handle, val, argset->context);
919 			argset->count++;
920 		}
921 	}
922 
923 	return (AE_OK);
924 }
925 
926 static int
927 vid_enum_outputs(ACPI_HANDLE handle,
928 		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
929 {
930 	ACPI_STATUS status;
931 	ACPI_BUFFER dod_buf;
932 	ACPI_OBJECT *res;
933 	struct enum_callback_arg argset;
934 
935 	ACPI_SERIAL_ASSERT(video);
936 	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
937 	dod_buf.Pointer = NULL;
938 	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
939 	if (ACPI_FAILURE(status)) {
940 		if (status != AE_NOT_FOUND)
941 			printf("can't evaluate %s._DOD - %s\n",
942 			       acpi_name(handle), AcpiFormatException(status));
943 		argset.count = -1;
944 		goto out;
945 	}
946 	res = (ACPI_OBJECT *)dod_buf.Pointer;
947 	if (!ACPI_PKG_VALID(res, 1)) {
948 		printf("evaluation of %s._DOD makes no sense\n",
949 		       acpi_name(handle));
950 		argset.count = -1;
951 		goto out;
952 	}
953 	if (callback == NULL) {
954 		argset.count = res->Package.Count;
955 		goto out;
956 	}
957 	argset.callback = callback;
958 	argset.context  = context;
959 	argset.dod_pkg  = res;
960 	argset.count    = 0;
961 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
962 	    vid_enum_outputs_subr, NULL, &argset, NULL);
963 	if (ACPI_FAILURE(status))
964 		printf("failed walking down %s - %s\n",
965 		       acpi_name(handle), AcpiFormatException(status));
966 out:
967 	if (dod_buf.Pointer != NULL)
968 		AcpiOsFree(dod_buf.Pointer);
969 	return (argset.count);
970 }
971 
972 static int
973 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
974 {
975 	ACPI_STATUS status;
976 	ACPI_BUFFER bcl_buf;
977 	ACPI_OBJECT *res;
978 	int num, i, n, *levels;
979 
980 	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
981 	bcl_buf.Pointer = NULL;
982 	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
983 	if (ACPI_FAILURE(status)) {
984 		if (status != AE_NOT_FOUND)
985 			printf("can't evaluate %s._BCL - %s\n",
986 			       acpi_name(handle), AcpiFormatException(status));
987 		goto out;
988 	}
989 	res = (ACPI_OBJECT *)bcl_buf.Pointer;
990 	if (!ACPI_PKG_VALID(res, 2)) {
991 		printf("evaluation of %s._BCL makes no sense\n",
992 		       acpi_name(handle));
993 		goto out;
994 	}
995 	num = res->Package.Count;
996 	if (num < 2 || levelp == NULL)
997 		goto out;
998 	levels = AcpiOsAllocate(num * sizeof(*levels));
999 	if (levels == NULL)
1000 		goto out;
1001 	for (i = 0, n = 0; i < num; i++)
1002 		if (acpi_PkgInt32(res, i, &levels[n]) == 0)
1003 			n++;
1004 	if (n < 2) {
1005 		AcpiOsFree(levels);
1006 		goto out;
1007 	}
1008 	*levelp = levels;
1009 	return (n);
1010 
1011 out:
1012 	if (bcl_buf.Pointer != NULL)
1013 		AcpiOsFree(bcl_buf.Pointer);
1014 	return (0);
1015 }
1016 
1017 static int
1018 vo_get_brightness(ACPI_HANDLE handle)
1019 {
1020 	UINT32 level;
1021 	ACPI_STATUS status;
1022 
1023 	ACPI_SERIAL_ASSERT(video_output);
1024 	status = acpi_GetInteger(handle, "_BQC", &level);
1025 	if (ACPI_FAILURE(status)) {
1026 		printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
1027 		    AcpiFormatException(status));
1028 		return (-1);
1029 	}
1030 	if (level > 100)
1031 		return (-1);
1032 
1033 	return (level);
1034 }
1035 
1036 static void
1037 vo_set_brightness(ACPI_HANDLE handle, int level)
1038 {
1039 	ACPI_STATUS status;
1040 
1041 	ACPI_SERIAL_ASSERT(video_output);
1042 	status = acpi_SetInteger(handle, "_BCM", level);
1043 	if (ACPI_FAILURE(status))
1044 		printf("can't evaluate %s._BCM - %s\n",
1045 		       acpi_name(handle), AcpiFormatException(status));
1046 }
1047 
1048 static UINT32
1049 vo_get_device_status(ACPI_HANDLE handle)
1050 {
1051 	UINT32 dcs;
1052 	ACPI_STATUS status;
1053 
1054 	ACPI_SERIAL_ASSERT(video_output);
1055 	dcs = 0;
1056 	status = acpi_GetInteger(handle, "_DCS", &dcs);
1057 	if (ACPI_FAILURE(status))
1058 		printf("can't evaluate %s._DCS - %s\n",
1059 		       acpi_name(handle), AcpiFormatException(status));
1060 
1061 	return (dcs);
1062 }
1063 
1064 static UINT32
1065 vo_get_graphics_state(ACPI_HANDLE handle)
1066 {
1067 	UINT32 dgs;
1068 	ACPI_STATUS status;
1069 
1070 	dgs = 0;
1071 	status = acpi_GetInteger(handle, "_DGS", &dgs);
1072 	if (ACPI_FAILURE(status))
1073 		printf("can't evaluate %s._DGS - %s\n",
1074 		       acpi_name(handle), AcpiFormatException(status));
1075 
1076 	return (dgs);
1077 }
1078 
1079 static void
1080 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1081 {
1082 	ACPI_STATUS status;
1083 
1084 	ACPI_SERIAL_ASSERT(video_output);
1085 	status = acpi_SetInteger(handle, "_DSS", state);
1086 	if (ACPI_FAILURE(status))
1087 		printf("can't evaluate %s._DSS - %s\n",
1088 		       acpi_name(handle), AcpiFormatException(status));
1089 }
1090