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