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