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  * $FreeBSD: head/sys/dev/acpica/acpi_video.c 258780 2013-11-30 22:17:27Z eadler $
27  *	$Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/power.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 
39 #include "acpi.h"
40 #include "acpivar.h"
41 
42 /* ACPI video extension driver. */
43 struct acpi_video_output {
44 	ACPI_HANDLE	handle;
45 	UINT32		adr;
46 	STAILQ_ENTRY(acpi_video_output) vo_next;
47 	struct {
48 		int	num;
49 		STAILQ_ENTRY(acpi_video_output) next;
50 	} vo_unit;
51 	int		vo_brightness;
52 	int		vo_fullpower;
53 	int		vo_economy;
54 	int		vo_numlevels;
55 	int		*vo_levels;
56 	struct sysctl_ctx_list vo_sysctl_ctx;
57 	struct sysctl_oid *vo_sysctl_tree;
58 };
59 
60 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
61 
62 struct acpi_video_softc {
63 	device_t		device;
64 	ACPI_HANDLE		handle;
65 	struct acpi_video_output_queue vid_outputs;
66 	eventhandler_tag	vid_pwr_evh;
67 };
68 
69 /* interfaces */
70 static int	acpi_video_modevent(struct module*, int, void *);
71 static void	acpi_video_identify(driver_t *driver, device_t parent);
72 static int	acpi_video_probe(device_t);
73 static int	acpi_video_attach(device_t);
74 static int	acpi_video_detach(device_t);
75 static int	acpi_video_resume(device_t);
76 static int	acpi_video_shutdown(device_t);
77 static void	acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
78 static void	acpi_video_power_profile(void *);
79 static void	acpi_video_bind_outputs(struct acpi_video_softc *);
80 static struct acpi_video_output *acpi_video_vo_init(UINT32);
81 static void	acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
82 static void	acpi_video_vo_destroy(struct acpi_video_output *);
83 static int	acpi_video_vo_check_level(struct acpi_video_output *, int);
84 static void	acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
85 static int	acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
86 static int	acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
87 static int	acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
88 static int	acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
89 
90 /* operations */
91 static void	vid_set_switch_policy(ACPI_HANDLE, UINT32);
92 static int	vid_enum_outputs(ACPI_HANDLE,
93 		    void(*)(ACPI_HANDLE, UINT32, void *), void *);
94 static int	vo_get_brightness_levels(ACPI_HANDLE, int **);
95 static int	vo_get_brightness(ACPI_HANDLE);
96 static void	vo_set_brightness(ACPI_HANDLE, int);
97 static UINT32	vo_get_device_status(ACPI_HANDLE);
98 static UINT32	vo_get_graphics_state(ACPI_HANDLE);
99 static void	vo_set_device_state(ACPI_HANDLE, UINT32);
100 
101 /* events */
102 #define VID_NOTIFY_SWITCHED	0x80
103 #define VID_NOTIFY_REPROBE	0x81
104 #define	VID_NOTIFY_CYCLE_BRN	0x85
105 #define	VID_NOTIFY_INC_BRN	0x86
106 #define	VID_NOTIFY_DEC_BRN	0x87
107 #define	VID_NOTIFY_ZERO_BRN	0x88
108 
109 /* _DOS (Enable/Disable Output Switching) argument bits */
110 #define DOS_SWITCH_MASK		3
111 #define DOS_SWITCH_BY_OSPM	0
112 #define DOS_SWITCH_BY_BIOS	1
113 #define DOS_SWITCH_LOCKED	2
114 #define	DOS_BRIGHTNESS_BY_OSPM	(1 << 2)
115 
116 /* _DOD and subdev's _ADR */
117 #define DOD_DEVID_MASK		0x0f00
118 #define DOD_DEVID_MASK_FULL	0xffff
119 #define DOD_DEVID_MASK_DISPIDX	0x000f
120 #define DOD_DEVID_MASK_DISPPORT	0x00f0
121 #define DOD_DEVID_MONITOR	0x0100
122 #define DOD_DEVID_LCD		0x0110
123 #define DOD_DEVID_TV		0x0200
124 #define DOD_DEVID_EXT		0x0300
125 #define DOD_DEVID_INTDFP	0x0400
126 #define DOD_BIOS		(1 << 16)
127 #define DOD_NONVGA		(1 << 17)
128 #define DOD_HEAD_ID_SHIFT	18
129 #define DOD_HEAD_ID_BITS	3
130 #define DOD_HEAD_ID_MASK \
131 		(((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
132 #define DOD_DEVID_SCHEME_STD	(1 << 31)
133 
134 /* _BCL related constants */
135 #define BCL_FULLPOWER		0
136 #define BCL_ECONOMY		1
137 
138 /* _DCS (Device Currrent Status) value bits and masks. */
139 #define DCS_EXISTS		(1 << 0)
140 #define DCS_ACTIVE		(1 << 1)
141 #define DCS_READY		(1 << 2)
142 #define DCS_FUNCTIONAL		(1 << 3)
143 #define DCS_ATTACHED		(1 << 4)
144 
145 /* _DSS (Device Set Status) argument bits and masks. */
146 #define DSS_INACTIVE		0
147 #define DSS_ACTIVE		(1 << 0)
148 #define DSS_SETNEXT		(1 << 30)
149 #define DSS_COMMIT		(1 << 31)
150 
151 static device_method_t acpi_video_methods[] = {
152 	DEVMETHOD(device_identify, acpi_video_identify),
153 	DEVMETHOD(device_probe, acpi_video_probe),
154 	DEVMETHOD(device_attach, acpi_video_attach),
155 	DEVMETHOD(device_detach, acpi_video_detach),
156 	DEVMETHOD(device_resume, acpi_video_resume),
157 	DEVMETHOD(device_shutdown, acpi_video_shutdown),
158 
159 	DEVMETHOD_END
160 };
161 
162 static driver_t acpi_video_driver = {
163 	"acpi_video",
164 	acpi_video_methods,
165 	sizeof(struct acpi_video_softc),
166 };
167 
168 static devclass_t acpi_video_devclass;
169 
170 DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
171 	      acpi_video_modevent, NULL);
172 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
173 
174 static struct sysctl_ctx_list	acpi_video_sysctl_ctx;
175 static struct sysctl_oid	*acpi_video_sysctl_tree;
176 static struct acpi_video_output_queue crt_units, tv_units,
177     ext_units, lcd_units, other_units;
178 
179 /*
180  * The 'video' lock protects the hierarchy of video output devices
181  * (the video "bus").  The 'video_output' lock protects per-output
182  * data is equivalent to a softc lock for each video output.
183  */
184 ACPI_SERIAL_DECL(video, "ACPI video");
185 ACPI_SERIAL_DECL(video_output, "ACPI video output");
186 static MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
187 
188 static int
189 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
190 {
191 	int err;
192 
193 	err = 0;
194 	switch (evt) {
195 	case MOD_LOAD:
196 		sysctl_ctx_init(&acpi_video_sysctl_ctx);
197 		STAILQ_INIT(&crt_units);
198 		STAILQ_INIT(&tv_units);
199 		STAILQ_INIT(&ext_units);
200 		STAILQ_INIT(&lcd_units);
201 		STAILQ_INIT(&other_units);
202 		break;
203 	case MOD_UNLOAD:
204 		sysctl_ctx_free(&acpi_video_sysctl_ctx);
205 		acpi_video_sysctl_tree = NULL;
206 		break;
207 	default:
208 		err = EINVAL;
209 	}
210 
211 	return (err);
212 }
213 
214 static void
215 acpi_video_identify(driver_t *driver, device_t parent)
216 {
217 
218 	if (device_find_child(parent, "acpi_video", -1) == NULL)
219 		device_add_child(parent, "acpi_video", -1);
220 }
221 
222 static int
223 acpi_video_probe(device_t dev)
224 {
225 	ACPI_HANDLE devh, h;
226 	ACPI_OBJECT_TYPE t_dos;
227 
228 	devh = acpi_get_handle(dev);
229 	if (acpi_disabled("video") ||
230 	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
231 	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
232 	    ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
233 	    t_dos != ACPI_TYPE_METHOD)
234 		return (ENXIO);
235 
236 	device_set_desc(dev, "ACPI video extension");
237 	return (0);
238 }
239 
240 static int
241 acpi_video_attach(device_t dev)
242 {
243 	struct acpi_softc *acpi_sc;
244 	struct acpi_video_softc *sc;
245 
246 	sc = device_get_softc(dev);
247 
248 	acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
249 	if (acpi_sc == NULL)
250 		return (ENXIO);
251 	ACPI_SERIAL_INIT(video);
252 	ACPI_SERIAL_INIT(video_output);
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 
287 static int
288 acpi_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_MUTABLE(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 
309 static int
310 acpi_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_MUTABLE(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 
339 static int
340 acpi_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 
350 static void
351 acpi_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_MUTABLE(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 
404 static void
405 acpi_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 
429 static void
430 acpi_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 
451 static void
452 acpi_video_bind_outputs(struct acpi_video_softc *sc)
453 {
454 	ACPI_SERIAL_ASSERT(video);
455 	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
456 }
457 
458 static struct acpi_video_output *
459 acpi_video_vo_init(UINT32 adr)
460 {
461 	struct acpi_video_output *vn, *vo, *vp;
462 	int n, x;
463 	char name[8], env[32];
464 	const char *type, *desc;
465 	struct acpi_video_output_queue *voqh;
466 
467 	ACPI_SERIAL_ASSERT(video);
468 
469 	switch (adr & DOD_DEVID_MASK) {
470 	case DOD_DEVID_MONITOR:
471 		if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
472 			/* DOD_DEVID_LCD is a common, backward compatible ID */
473 			desc = "Internal/Integrated Digital Flat Panel";
474 			type = "lcd";
475 			voqh = &lcd_units;
476 		} else {
477 			desc = "VGA CRT or VESA Compatible Analog Monitor";
478 			type = "crt";
479 			voqh = &crt_units;
480 		}
481 		break;
482 	case DOD_DEVID_TV:
483 		desc = "TV/HDTV or Analog-Video Monitor";
484 		type = "tv";
485 		voqh = &tv_units;
486 		break;
487 	case DOD_DEVID_EXT:
488 		desc = "External Digital Monitor";
489 		type = "ext";
490 		voqh = &ext_units;
491 		break;
492 	case DOD_DEVID_INTDFP:
493 		desc = "Internal/Integrated Digital Flat Panel";
494 		type = "lcd";
495 		voqh = &lcd_units;
496 		break;
497 	default:
498 		desc = "unknown output";
499 		type = "out";
500 		voqh = &other_units;
501 	}
502 
503 	n = 0;
504 	vp = NULL;
505 	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
506 		if (vn->vo_unit.num != n)
507 			break;
508 		vp = vn;
509 		n++;
510 	}
511 
512 	ksnprintf(name, sizeof(name), "%s%d", type, n);
513 
514 	vo = kmalloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
515 	if (vo != NULL) {
516 		vo->handle = NULL;
517 		vo->adr = adr;
518 		vo->vo_unit.num = n;
519 		vo->vo_brightness = -1;
520 		vo->vo_fullpower = -1;	/* TODO: override with tunables */
521 		vo->vo_economy = -1;
522 		vo->vo_numlevels = 0;
523 		vo->vo_levels = NULL;
524 		ksnprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
525 		if (kgetenv_int(env, &x))
526 			vo->vo_fullpower = x;
527 		ksnprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
528 		if (kgetenv_int(env, &x))
529 			vo->vo_economy = x;
530 
531 		sysctl_ctx_init(&vo->vo_sysctl_ctx);
532 		if (vp != NULL)
533 			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
534 		else
535 			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
536 		if (acpi_video_sysctl_tree != NULL)
537 			vo->vo_sysctl_tree =
538 			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
539 				SYSCTL_CHILDREN(acpi_video_sysctl_tree),
540 				OID_AUTO, name, CTLFLAG_RD, 0, desc);
541 		if (vo->vo_sysctl_tree != NULL) {
542 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
543 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
544 			    OID_AUTO, "active",
545 			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
546 			    acpi_video_vo_active_sysctl, "I",
547 			    "current activity of this device");
548 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
549 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
550 			    OID_AUTO, "brightness",
551 			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
552 			    acpi_video_vo_bright_sysctl, "I",
553 			    "current brightness level");
554 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
555 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
556 			    OID_AUTO, "fullpower",
557 			    CTLTYPE_INT|CTLFLAG_RW, vo,
558 			    POWER_PROFILE_PERFORMANCE,
559 			    acpi_video_vo_presets_sysctl, "I",
560 			    "preset level for full power mode");
561 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
562 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
563 			    OID_AUTO, "economy",
564 			    CTLTYPE_INT|CTLFLAG_RW, vo,
565 			    POWER_PROFILE_ECONOMY,
566 			    acpi_video_vo_presets_sysctl, "I",
567 			    "preset level for economy mode");
568 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
569 			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
570 			    OID_AUTO, "levels",
571 			    CTLTYPE_INT|CTLFLAG_RD, vo, 0,
572 			    acpi_video_vo_levels_sysctl, "I",
573 			    "supported brightness levels");
574 		} else
575 			kprintf("%s: sysctl node creation failed\n", type);
576 	} else
577 		kprintf("%s: softc allocation failed\n", type);
578 
579 	if (bootverbose) {
580 		kprintf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
581 		kprintf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
582 		kprintf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
583 		if (adr & DOD_BIOS)
584 			kprintf(", detectable by BIOS");
585 		if (adr & DOD_NONVGA)
586 			kprintf(" (Non-VGA output device whose power "
587 			    "is related to the VGA device)");
588 		kprintf(", head #%d\n",
589 			(adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
590 	}
591 	return (vo);
592 }
593 
594 static void
595 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
596 {
597 	ACPI_SERIAL_BEGIN(video_output);
598 	if (vo->vo_levels != NULL)
599 		AcpiOsFree(vo->vo_levels);
600 	vo->handle = handle;
601 	vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
602 	if (vo->vo_numlevels >= 2) {
603 		if (vo->vo_fullpower == -1
604 		    || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
605 			/* XXX - can't deal with rebinding... */
606 			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
607 		if (vo->vo_economy == -1
608 		    || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
609 			/* XXX - see above. */
610 			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
611 	}
612 	if (vo->vo_levels != NULL)
613 		AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
614 		    acpi_video_vo_notify_handler, vo);
615 	ACPI_SERIAL_END(video_output);
616 }
617 
618 static void
619 acpi_video_vo_destroy(struct acpi_video_output *vo)
620 {
621 	struct acpi_video_output_queue *voqh;
622 
623 	ACPI_SERIAL_ASSERT(video);
624 	if (vo->vo_sysctl_tree != NULL) {
625 		vo->vo_sysctl_tree = NULL;
626 		sysctl_ctx_free(&vo->vo_sysctl_ctx);
627 	}
628 	if (vo->vo_levels != NULL) {
629 		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
630 		    acpi_video_vo_notify_handler);
631 		AcpiOsFree(vo->vo_levels);
632 	}
633 
634 	switch (vo->adr & DOD_DEVID_MASK) {
635 	case DOD_DEVID_MONITOR:
636 		voqh = &crt_units;
637 		break;
638 	case DOD_DEVID_TV:
639 		voqh = &tv_units;
640 		break;
641 	case DOD_DEVID_EXT:
642 		voqh = &ext_units;
643 		break;
644 	case DOD_DEVID_INTDFP:
645 		voqh = &lcd_units;
646 		break;
647 	default:
648 		voqh = &other_units;
649 	}
650 	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
651 	kfree(vo, M_ACPIVIDEO);
652 }
653 
654 static int
655 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
656 {
657 	int i;
658 
659 	ACPI_SERIAL_ASSERT(video_output);
660 	if (vo->vo_levels == NULL)
661 		return (ENODEV);
662 	for (i = 0; i < vo->vo_numlevels; i++)
663 		if (vo->vo_levels[i] == level)
664 			return (0);
665 	return (EINVAL);
666 }
667 
668 static void
669 acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
670 {
671 	struct acpi_video_output *vo;
672 	int i, j, level, new_level;
673 
674 	vo = context;
675 	ACPI_SERIAL_BEGIN(video_output);
676 	if (vo->handle != handle)
677 		goto out;
678 
679 	switch (notify) {
680 	case VID_NOTIFY_CYCLE_BRN:
681 		if (vo->vo_numlevels <= 3)
682 			goto out;
683 		/* FALLTHROUGH */
684 	case VID_NOTIFY_INC_BRN:
685 	case VID_NOTIFY_DEC_BRN:
686 	case VID_NOTIFY_ZERO_BRN:
687 		if (vo->vo_levels == NULL)
688 			goto out;
689 		level = vo_get_brightness(handle);
690 		if (level < 0)
691 			goto out;
692 		break;
693 	default:
694 		kprintf("unknown notify event 0x%x from %s\n",
695 		    notify, acpi_name(handle));
696 		goto out;
697 	}
698 
699 	new_level = level;
700 	switch (notify) {
701 	case VID_NOTIFY_CYCLE_BRN:
702 		for (i = 2; i < vo->vo_numlevels; i++)
703 			if (vo->vo_levels[i] == level) {
704 				new_level = vo->vo_numlevels > i + 1 ?
705 				     vo->vo_levels[i + 1] : vo->vo_levels[2];
706 				break;
707 			}
708 		break;
709 	case VID_NOTIFY_INC_BRN:
710 	case VID_NOTIFY_DEC_BRN:
711 		for (i = 0; i < vo->vo_numlevels; i++) {
712 			j = vo->vo_levels[i];
713 			if (notify == VID_NOTIFY_INC_BRN) {
714 				if (j > level &&
715 				    (j < new_level || level == new_level))
716 					new_level = j;
717 			} else {
718 				if (j < level &&
719 				    (j > new_level || level == new_level))
720 					new_level = j;
721 			}
722 		}
723 		break;
724 	case VID_NOTIFY_ZERO_BRN:
725 		for (i = 0; i < vo->vo_numlevels; i++)
726 			if (vo->vo_levels[i] == 0) {
727 				new_level = 0;
728 				break;
729 			}
730 		break;
731 	}
732 	if (new_level != level) {
733 		vo_set_brightness(handle, new_level);
734 		vo->vo_brightness = new_level;
735 	}
736 
737 out:
738 	ACPI_SERIAL_END(video_output);
739 }
740 
741 /* ARGSUSED */
742 static int
743 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
744 {
745 	struct acpi_video_output *vo;
746 	int state, err;
747 
748 	vo = (struct acpi_video_output *)arg1;
749 	if (vo->handle == NULL)
750 		return (ENXIO);
751 	ACPI_SERIAL_BEGIN(video_output);
752 	state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
753 	err = sysctl_handle_int(oidp, &state, 0, req);
754 	if (err != 0 || req->newptr == NULL)
755 		goto out;
756 	vo_set_device_state(vo->handle,
757 	    DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
758 out:
759 	ACPI_SERIAL_END(video_output);
760 	return (err);
761 }
762 
763 /* ARGSUSED */
764 static int
765 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
766 {
767 	struct acpi_video_output *vo;
768 	int level, preset, err;
769 
770 	vo = (struct acpi_video_output *)arg1;
771 	ACPI_SERIAL_BEGIN(video_output);
772 	if (vo->handle == NULL) {
773 		err = ENXIO;
774 		goto out;
775 	}
776 	if (vo->vo_levels == NULL) {
777 		err = ENODEV;
778 		goto out;
779 	}
780 
781 	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
782 		  vo->vo_economy : vo->vo_fullpower;
783 	level = vo->vo_brightness;
784 	if (level == -1)
785 		level = preset;
786 
787 	err = sysctl_handle_int(oidp, &level, 0, req);
788 	if (err != 0 || req->newptr == NULL)
789 		goto out;
790 	if (level < -1 || level > 100) {
791 		err = EINVAL;
792 		goto out;
793 	}
794 
795 	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
796 		goto out;
797 	vo->vo_brightness = level;
798 	vo_set_brightness(vo->handle, (level == -1) ? preset : level);
799 
800 out:
801 	ACPI_SERIAL_END(video_output);
802 	return (err);
803 }
804 
805 static int
806 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
807 {
808 	struct acpi_video_output *vo;
809 	int i, level, *preset, err;
810 
811 	vo = (struct acpi_video_output *)arg1;
812 	ACPI_SERIAL_BEGIN(video_output);
813 	if (vo->handle == NULL) {
814 		err = ENXIO;
815 		goto out;
816 	}
817 	if (vo->vo_levels == NULL) {
818 		err = ENODEV;
819 		goto out;
820 	}
821 	preset = (arg2 == POWER_PROFILE_ECONOMY) ?
822 		  &vo->vo_economy : &vo->vo_fullpower;
823 	level = *preset;
824 	err = sysctl_handle_int(oidp, &level, 0, req);
825 	if (err != 0 || req->newptr == NULL)
826 		goto out;
827 	if (level < -1 || level > 100) {
828 		err = EINVAL;
829 		goto out;
830 	}
831 	if (level == -1) {
832 		i = (arg2 == POWER_PROFILE_ECONOMY) ?
833 		    BCL_ECONOMY : BCL_FULLPOWER;
834 		level = vo->vo_levels[i];
835 	} else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
836 		goto out;
837 
838 	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
839 		vo_set_brightness(vo->handle, level);
840 	*preset = level;
841 
842 out:
843 	ACPI_SERIAL_END(video_output);
844 	return (err);
845 }
846 
847 /* ARGSUSED */
848 static int
849 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
850 {
851 	struct acpi_video_output *vo;
852 	int err;
853 
854 	vo = (struct acpi_video_output *)arg1;
855 	ACPI_SERIAL_BEGIN(video_output);
856 	if (vo->vo_levels == NULL) {
857 		err = ENODEV;
858 		goto out;
859 	}
860 	if (req->newptr != NULL) {
861 		err = EPERM;
862 		goto out;
863 	}
864 	err = sysctl_handle_opaque(oidp, vo->vo_levels,
865 	    vo->vo_numlevels * sizeof(*vo->vo_levels), req);
866 
867 out:
868 	ACPI_SERIAL_END(video_output);
869 	return (err);
870 }
871 
872 static void
873 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
874 {
875 	ACPI_STATUS status;
876 
877 	status = acpi_SetInteger(handle, "_DOS", policy);
878 	if (ACPI_FAILURE(status))
879 		kprintf("can't evaluate %s._DOS - %s\n",
880 		       acpi_name(handle), AcpiFormatException(status));
881 }
882 
883 struct enum_callback_arg {
884 	void (*callback)(ACPI_HANDLE, UINT32, void *);
885 	void *context;
886 	ACPI_OBJECT *dod_pkg;
887 	int count;
888 };
889 
890 static ACPI_STATUS
891 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
892 		      void *context, void **retp __unused)
893 {
894 	ACPI_STATUS status;
895 	UINT32 adr, val;
896 	struct enum_callback_arg *argset;
897 	size_t i;
898 
899 	ACPI_SERIAL_ASSERT(video);
900 	argset = context;
901 	status = acpi_GetInteger(handle, "_ADR", &adr);
902 	if (ACPI_FAILURE(status))
903 		return (AE_OK);
904 
905 	for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
906 		if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
907 		    (val & DOD_DEVID_MASK_FULL) ==
908 		    (adr & DOD_DEVID_MASK_FULL)) {
909 			argset->callback(handle, val, argset->context);
910 			argset->count++;
911 		}
912 	}
913 
914 	return (AE_OK);
915 }
916 
917 static int
918 vid_enum_outputs(ACPI_HANDLE handle,
919 		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
920 {
921 	ACPI_STATUS status;
922 	ACPI_BUFFER dod_buf;
923 	ACPI_OBJECT *res;
924 	struct enum_callback_arg argset;
925 
926 	ACPI_SERIAL_ASSERT(video);
927 	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
928 	dod_buf.Pointer = NULL;
929 	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
930 	if (ACPI_FAILURE(status)) {
931 		if (status != AE_NOT_FOUND)
932 			kprintf("can't evaluate %s._DOD - %s\n",
933 			       acpi_name(handle), AcpiFormatException(status));
934 		argset.count = -1;
935 		goto out;
936 	}
937 	res = (ACPI_OBJECT *)dod_buf.Pointer;
938 	if (!ACPI_PKG_VALID(res, 1)) {
939 		kprintf("evaluation of %s._DOD makes no sense\n",
940 		       acpi_name(handle));
941 		argset.count = -1;
942 		goto out;
943 	}
944 	if (callback == NULL) {
945 		argset.count = res->Package.Count;
946 		goto out;
947 	}
948 	argset.callback = callback;
949 	argset.context  = context;
950 	argset.dod_pkg  = res;
951 	argset.count    = 0;
952 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
953 	    vid_enum_outputs_subr, NULL, &argset, NULL);
954 	if (ACPI_FAILURE(status))
955 		kprintf("failed walking down %s - %s\n",
956 		       acpi_name(handle), AcpiFormatException(status));
957 out:
958 	if (dod_buf.Pointer != NULL)
959 		AcpiOsFree(dod_buf.Pointer);
960 	return (argset.count);
961 }
962 
963 static int
964 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
965 {
966 	ACPI_STATUS status;
967 	ACPI_BUFFER bcl_buf;
968 	ACPI_OBJECT *res;
969 	int num, i, n, *levels;
970 
971 	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
972 	bcl_buf.Pointer = NULL;
973 	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
974 	if (ACPI_FAILURE(status)) {
975 		if (status != AE_NOT_FOUND)
976 			kprintf("can't evaluate %s._BCL - %s\n",
977 			       acpi_name(handle), AcpiFormatException(status));
978 		goto out;
979 	}
980 	res = (ACPI_OBJECT *)bcl_buf.Pointer;
981 	if (!ACPI_PKG_VALID(res, 2)) {
982 		kprintf("evaluation of %s._BCL makes no sense\n",
983 		       acpi_name(handle));
984 		goto out;
985 	}
986 	num = res->Package.Count;
987 	if (num < 2 || levelp == NULL)
988 		goto out;
989 	levels = AcpiOsAllocate(num * sizeof(*levels));
990 	if (levels == NULL)
991 		goto out;
992 	for (i = 0, n = 0; i < num; i++)
993 		if (acpi_PkgInt32(res, i, &levels[n]) == 0)
994 			n++;
995 	if (n < 2) {
996 		AcpiOsFree(levels);
997 		goto out;
998 	}
999 	*levelp = levels;
1000 	return (n);
1001 
1002 out:
1003 	if (bcl_buf.Pointer != NULL)
1004 		AcpiOsFree(bcl_buf.Pointer);
1005 	return (0);
1006 }
1007 
1008 static int
1009 vo_get_brightness(ACPI_HANDLE handle)
1010 {
1011 	UINT32 level;
1012 	ACPI_STATUS status;
1013 
1014 	ACPI_SERIAL_ASSERT(video_output);
1015 	status = acpi_GetInteger(handle, "_BQC", &level);
1016 	if (ACPI_FAILURE(status)) {
1017 		kprintf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
1018 		    AcpiFormatException(status));
1019 		return (-1);
1020 	}
1021 	if (level > 100)
1022 		return (-1);
1023 
1024 	return (level);
1025 }
1026 
1027 static void
1028 vo_set_brightness(ACPI_HANDLE handle, int level)
1029 {
1030 	ACPI_STATUS status;
1031 
1032 	ACPI_SERIAL_ASSERT(video_output);
1033 	status = acpi_SetInteger(handle, "_BCM", level);
1034 	if (ACPI_FAILURE(status))
1035 		kprintf("can't evaluate %s._BCM - %s\n",
1036 		       acpi_name(handle), AcpiFormatException(status));
1037 }
1038 
1039 static UINT32
1040 vo_get_device_status(ACPI_HANDLE handle)
1041 {
1042 	UINT32 dcs;
1043 	ACPI_STATUS status;
1044 
1045 	ACPI_SERIAL_ASSERT(video_output);
1046 	dcs = 0;
1047 	status = acpi_GetInteger(handle, "_DCS", &dcs);
1048 	if (ACPI_FAILURE(status))
1049 		kprintf("can't evaluate %s._DCS - %s\n",
1050 		       acpi_name(handle), AcpiFormatException(status));
1051 
1052 	return (dcs);
1053 }
1054 
1055 static UINT32
1056 vo_get_graphics_state(ACPI_HANDLE handle)
1057 {
1058 	UINT32 dgs;
1059 	ACPI_STATUS status;
1060 
1061 	dgs = 0;
1062 	status = acpi_GetInteger(handle, "_DGS", &dgs);
1063 	if (ACPI_FAILURE(status))
1064 		kprintf("can't evaluate %s._DGS - %s\n",
1065 		       acpi_name(handle), AcpiFormatException(status));
1066 
1067 	return (dgs);
1068 }
1069 
1070 static void
1071 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
1072 {
1073 	ACPI_STATUS status;
1074 
1075 	ACPI_SERIAL_ASSERT(video_output);
1076 	status = acpi_SetInteger(handle, "_DSS", state);
1077 	if (ACPI_FAILURE(status))
1078 		kprintf("can't evaluate %s._DSS - %s\n",
1079 		       acpi_name(handle), AcpiFormatException(status));
1080 }
1081