xref: /freebsd/sys/dev/acpica/acpi_video.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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  *	$FreeBSD$
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 <dev/acpica/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 	STAILQ_HEAD(, acpi_video_output) vid_outputs;
66 	eventhandler_tag	vid_pwr_evh;
67 };
68 
69 /* interfaces */
70 static int	acpi_video_modevent(struct module*, int, void *);
71 static int	acpi_video_probe(device_t);
72 static int	acpi_video_attach(device_t);
73 static int	acpi_video_detach(device_t);
74 static int	acpi_video_shutdown(device_t);
75 static void	acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
76 static void	acpi_video_power_profile(void *);
77 static void	acpi_video_bind_outputs(struct acpi_video_softc *);
78 static struct acpi_video_output
79 		*acpi_video_vo_init(UINT32);
80 static void	acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
81 static void	acpi_video_vo_destroy(struct acpi_video_output *);
82 static int	acpi_video_vo_check_level(struct acpi_video_output *, int);
83 static int	acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
84 static int	acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
85 static int	acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
86 static int	acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
87 
88 /* operations */
89 static int	vid_check_requirements(ACPI_HANDLE);
90 static void	vid_set_switch_policy(ACPI_HANDLE, UINT32);
91 static int	vid_enum_outputs(ACPI_HANDLE,
92 				 void(*)(ACPI_HANDLE, UINT32, void *), void *);
93 static int	vo_query_brightness_levels(ACPI_HANDLE, int **);
94 static void	vo_set_brightness(ACPI_HANDLE, int);
95 static UINT32	vo_get_device_status(ACPI_HANDLE);
96 static UINT32	vo_query_graphics_state(ACPI_HANDLE);
97 static void	vo_set_device_state(ACPI_HANDLE, UINT32);
98 
99 /* events */
100 #define VID_NOTIFY_SWITCHED	0x80
101 #define VID_NOTIFY_REPROBE	0x81
102 
103 /* _DOS (Enable/Disable Output Switching) argument bits */
104 #define DOS_SWITCH_MASK		((UINT32)3)
105 #define DOS_SWITCH_BY_OSPM	((UINT32)0)
106 #define DOS_SWITCH_BY_BIOS	((UINT32)1)
107 #define DOS_SWITCH_LOCKED	((UINT32)2)
108 #define DOS_BRIGHTNESS_BY_BIOS	((UINT32)1 << 2)
109 
110 /* _DOD and subdev's _ADR */
111 #define DOD_DEVID_MASK		((UINT32)0xffff)
112 #define DOD_DEVID_MONITOR	((UINT32)0x0100)
113 #define DOD_DEVID_PANEL		((UINT32)0x0110)
114 #define DOD_DEVID_TV		((UINT32)0x0200)
115 #define DOD_BIOS		((UINT32)1 << 16)
116 #define DOD_NONVGA		((UINT32)1 << 17)
117 #define DOD_HEAD_ID_SHIFT	18
118 #define DOD_HEAD_ID_BITS	3
119 #define DOD_HEAD_ID_MASK \
120 		((((UINT32)1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
121 
122 /* _BCL related constants */
123 #define BCL_FULLPOWER		0
124 #define BCL_ECONOMY		1
125 
126 /* _DCS (Device Currrent Status) value bits and masks. */
127 #define DCS_EXISTS		((UINT32)1 << 0)
128 #define DCS_ACTIVE		((UINT32)1 << 1)
129 #define DCS_READY		((UINT32)1 << 2)
130 #define DCS_FUNCTIONAL		((UINT32)1 << 3)
131 #define DCS_ATTACHED		((UINT32)1 << 4)
132 
133 /* _DSS (Device Set Status) argument bits and masks. */
134 #define DSS_INACTIVE		((UINT32)0)
135 #define DSS_ACTIVE		((UINT32)1 << 0)
136 #define DSS_ACTIVITY		((UINT32)1 << 0)
137 #define DSS_SETNEXT		((UINT32)1 << 30)
138 #define DSS_COMMIT		((UINT32)1 << 31)
139 
140 static device_method_t acpi_video_methods[] = {
141 	DEVMETHOD(device_probe, acpi_video_probe),
142 	DEVMETHOD(device_attach, acpi_video_attach),
143 	DEVMETHOD(device_detach, acpi_video_detach),
144 	DEVMETHOD(device_shutdown, acpi_video_shutdown),
145 	{ 0, 0 }
146 };
147 
148 static driver_t acpi_video_driver = {
149 	"acpi_video",
150 	acpi_video_methods,
151 	sizeof(struct acpi_video_softc),
152 };
153 
154 static devclass_t acpi_video_devclass;
155 
156 DRIVER_MODULE(acpi_video, acpi, acpi_video_driver, acpi_video_devclass,
157 	      acpi_video_modevent, NULL);
158 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
159 
160 struct sysctl_ctx_list	acpi_video_sysctl_ctx;
161 struct sysctl_oid	*acpi_video_sysctl_tree;
162 
163 static struct acpi_video_output_queue
164 		lcd_units, crt_units, tv_units, other_units;
165 
166 MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
167 
168 static int
169 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
170 {
171 	int err = 0;
172 
173 	switch (evt) {
174 	case MOD_LOAD:
175 		acpi_video_sysctl_tree = NULL;
176 		sysctl_ctx_init(&acpi_video_sysctl_ctx);
177 		STAILQ_INIT(&lcd_units);
178 		STAILQ_INIT(&crt_units);
179 		STAILQ_INIT(&tv_units);
180 		STAILQ_INIT(&other_units);
181 		break;
182 	case MOD_UNLOAD:
183 		acpi_video_sysctl_tree = NULL;
184 		sysctl_ctx_free(&acpi_video_sysctl_ctx);
185 		break;
186 	default:
187 		err = EINVAL;
188 	}
189 
190 	return (err);
191 }
192 
193 static int
194 acpi_video_probe(device_t dev)
195 {
196 	int err = ENXIO;
197 	ACPI_HANDLE handle;
198 	ACPI_LOCK_DECL;
199 
200 	ACPI_LOCK;
201 	handle = acpi_get_handle(dev);
202 	if (acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
203 	    !acpi_disabled("video") &&
204 	    vid_check_requirements(handle)) {
205 		device_set_desc(dev, "ACPI video extension");
206 		err = 0;
207 	}
208 	ACPI_UNLOCK;
209 
210 	return (err);
211 }
212 
213 static int
214 acpi_video_attach(device_t dev)
215 {
216 	struct acpi_softc *acpi_sc;
217 	struct acpi_video_softc *sc;
218 	ACPI_LOCK_DECL;
219 
220 	sc = device_get_softc(dev);
221 	ACPI_LOCK;
222 
223 	acpi_sc = acpi_device_get_parent_softc(dev);
224 	if (acpi_video_sysctl_tree == NULL && acpi_sc != NULL) {
225 		acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
226 				    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
227 				    OID_AUTO, "video", CTLFLAG_RD, 0,
228 				    "video extension control");
229 	}
230 
231 	sc->device = dev;
232 	sc->handle = acpi_get_handle(dev);
233 	STAILQ_INIT(&sc->vid_outputs);
234 
235 	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
236 				 acpi_video_notify_handler, sc);
237 	sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
238 				 acpi_video_power_profile, sc, 0);
239 
240 	acpi_video_bind_outputs(sc);
241 	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM);
242 
243 	ACPI_UNLOCK;
244 	acpi_video_power_profile(sc);
245 
246 	return (0);
247 }
248 
249 static int
250 acpi_video_detach(device_t dev)
251 {
252 	struct acpi_video_softc *sc;
253 	struct acpi_video_output *vo, *vn;
254 	ACPI_LOCK_DECL;
255 
256 	sc = device_get_softc(dev);
257 	ACPI_LOCK;
258 
259 	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
260 	EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
261 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
262 				acpi_video_notify_handler);
263 
264 	for (vo = STAILQ_FIRST(&sc->vid_outputs); vo != NULL; vo = vn) {
265 		vn = STAILQ_NEXT(vo, vo_next);
266 		acpi_video_vo_destroy(vo);
267 	}
268 
269 	ACPI_UNLOCK;
270 	return (0);
271 }
272 
273 static int
274 acpi_video_shutdown(device_t dev)
275 {
276 	struct acpi_video_softc *sc;
277 	ACPI_LOCK_DECL;
278 
279 	sc = device_get_softc(dev);
280 	ACPI_LOCK;
281 	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
282 	ACPI_UNLOCK;
283 
284 	return (0);
285 }
286 
287 static void
288 acpi_video_notify_handler(ACPI_HANDLE handle __unused, UINT32 notify, void *context)
289 {
290 	struct acpi_video_softc *sc;
291 	struct acpi_video_output *vo;
292 	ACPI_HANDLE lasthand = NULL;
293 	UINT32 dcs, dss, dss_p = 0;
294 
295 	ACPI_ASSERTLOCK;
296 	sc = context;
297 
298 	switch (notify) {
299 	case VID_NOTIFY_SWITCHED:
300 		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
301 			dss = vo_query_graphics_state(vo->handle);
302 			dcs = vo_get_device_status(vo->handle);
303 			if (!(dcs & DCS_READY))
304 				dss = DSS_INACTIVE;
305 			if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
306 			    (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
307 				if (lasthand != NULL)
308 					vo_set_device_state(lasthand, dss_p);
309 				dss_p = dss;
310 				lasthand = vo->handle;
311 			}
312 		}
313 		if (lasthand != NULL)
314 			vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
315 		break;
316 	case VID_NOTIFY_REPROBE:
317 		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
318 			vo->handle = NULL;
319 		acpi_video_bind_outputs(sc);
320 		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
321 			if (vo->handle == NULL) {
322 				STAILQ_REMOVE(&sc->vid_outputs, vo,
323 					      acpi_video_output, vo_next);
324 				acpi_video_vo_destroy(vo);
325 			}
326 		}
327 		break;
328 	default:
329 		device_printf(sc->device,
330 			      "unknown notify event 0x%x\n", notify);
331 	}
332 }
333 
334 static void
335 acpi_video_power_profile(void *context)
336 {
337 	int state;
338 	struct acpi_video_softc *sc;
339 	struct acpi_video_output *vo;
340 	ACPI_LOCK_DECL;
341 
342 	sc = context;
343 	state = power_profile_get_state();
344 	if (state != POWER_PROFILE_PERFORMANCE &&
345 	    state != POWER_PROFILE_ECONOMY)
346 		return;
347 
348 	ACPI_LOCK;
349 	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
350 		if (vo->vo_levels != NULL && vo->vo_brightness == -1)
351 			vo_set_brightness(vo->handle,
352 					  state == POWER_PROFILE_ECONOMY
353 					  ? vo->vo_economy : vo->vo_fullpower);
354 	}
355 	ACPI_UNLOCK;
356 }
357 
358 static void
359 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
360 {
361 	struct acpi_video_softc *sc;
362 	struct acpi_video_output *vo;
363 
364 	sc = context;
365 
366 	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
367 		if (vo->adr == adr) {
368 			acpi_video_vo_bind(vo, handle);
369 			return;
370 		}
371 	}
372 	vo = acpi_video_vo_init(adr);
373 	if (vo != NULL) {
374 		acpi_video_vo_bind(vo, handle);
375 		STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
376 	}
377 }
378 
379 static void
380 acpi_video_bind_outputs(struct acpi_video_softc *sc)
381 {
382 	ACPI_ASSERTLOCK;
383 
384 	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
385 }
386 
387 static struct acpi_video_output *
388 acpi_video_vo_init(UINT32 adr)
389 {
390 	struct acpi_video_output *vn, *vo, *vp;
391 	int n, x;
392 	char name[64], env[128];
393 	const char *type, *desc;
394 	struct acpi_video_output_queue *voqh;
395 
396 	switch (adr & DOD_DEVID_MASK) {
397 	case DOD_DEVID_MONITOR:
398 		desc = "CRT monitor";
399 		type = "crt";
400 		voqh = &crt_units;
401 		break;
402 	case DOD_DEVID_PANEL:
403 		desc = "LCD panel";
404 		type = "lcd";
405 		voqh = &lcd_units;
406 		break;
407 	case DOD_DEVID_TV:
408 		desc = "TV";
409 		type = "tv";
410 		voqh = &tv_units;
411 		break;
412 	default:
413 		desc = "unknown output";
414 		type = "out";
415 		voqh = &other_units;
416 	}
417 
418 	n = 0;
419 	vn = vp = NULL;
420 	/* XXX - needs locking for protecting STAILQ xxx_units. */
421 	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
422 		if (vn->vo_unit.num != n)
423 			break;
424 		vp = vn;
425 		n++;
426 	}
427 
428 	snprintf(name, 64, "%s%d", type, n);
429 
430 	vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
431 	if (vo != NULL) {
432 		vo->handle = NULL;
433 		vo->adr = adr;
434 		vo->vo_unit.num = n;
435 		vo->vo_brightness = -1;
436 		vo->vo_fullpower = -1;	/* TODO: override with tunables */
437 		vo->vo_economy = -1;
438 		vo->vo_numlevels = 0;
439 		vo->vo_levels = NULL;
440 		snprintf(env, 128, "hw.acpi.video.%s.fullpower", name);
441 		if (getenv_int(env, &x))
442 			vo->vo_fullpower = x;
443 		snprintf(env, 128, "hw.acpi.video.%s.economy", name);
444 		if (getenv_int(env, &x))
445 			vo->vo_economy = x;
446 
447 		sysctl_ctx_init(&vo->vo_sysctl_ctx);
448 		if (vp != NULL)
449 			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
450 		else
451 			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
452 		if (acpi_video_sysctl_tree != NULL)
453 			vo->vo_sysctl_tree =
454 			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
455 					    SYSCTL_CHILDREN(acpi_video_sysctl_tree),
456 					    OID_AUTO, name,
457 					    CTLFLAG_RD, 0, desc);
458 		if (vo->vo_sysctl_tree != NULL) {
459 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
460 					SYSCTL_CHILDREN(vo->vo_sysctl_tree),
461 					OID_AUTO, "active",
462 					CTLTYPE_INT|CTLFLAG_RW,	vo, 0,
463 					acpi_video_vo_active_sysctl, "I",
464 					"current activity of this device");
465 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
466 					SYSCTL_CHILDREN(vo->vo_sysctl_tree),
467 					OID_AUTO, "brightness",
468 					CTLTYPE_INT|CTLFLAG_RW, vo, 0,
469 					acpi_video_vo_bright_sysctl, "I",
470 					"current brightness level");
471 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
472 					SYSCTL_CHILDREN(vo->vo_sysctl_tree),
473 					OID_AUTO, "fullpower",
474 					CTLTYPE_INT|CTLFLAG_RW, vo,
475 					POWER_PROFILE_PERFORMANCE,
476 					acpi_video_vo_presets_sysctl, "I",
477 					"preset level for full power mode");
478 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
479 					SYSCTL_CHILDREN(vo->vo_sysctl_tree),
480 					OID_AUTO, "economy",
481 					CTLTYPE_INT|CTLFLAG_RW, vo,
482 					POWER_PROFILE_ECONOMY,
483 					acpi_video_vo_presets_sysctl, "I",
484 					"preset level for economy mode");
485 			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
486 					SYSCTL_CHILDREN(vo->vo_sysctl_tree),
487 					OID_AUTO, "levels",
488 					CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
489 					acpi_video_vo_levels_sysctl, "I",
490 					"supported brightness levels");
491 		} else
492 			printf("%s: sysctl node creation failed\n", type);
493 	} else
494 		printf("%s: softc allocation failed\n", type);
495 
496 	/* XXX unlock here - needs locking for protecting STAILQ xxx_units. */
497 
498 	if (bootverbose) {
499 		printf("found %s(%x)", desc,
500 		       (unsigned int)(adr & DOD_DEVID_MASK));
501 		if (adr & DOD_BIOS)
502 			printf(", detectable by BIOS");
503 		if (adr & DOD_NONVGA)
504 			printf(" (not a VGA output)");
505 		printf(", head #%d\n",
506 		       (int)((adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT));
507 	}
508 	return vo;
509 }
510 
511 static void
512 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
513 {
514 	ACPI_ASSERTLOCK;
515 
516 	if (vo->vo_levels != NULL)
517 		AcpiOsFree(vo->vo_levels);
518 	vo->handle = handle;
519 	vo->vo_numlevels
520 		= vo_query_brightness_levels(handle, &vo->vo_levels);
521 	if (vo->vo_numlevels >= 2) {
522 		if (vo->vo_fullpower == -1
523 		    || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
524 			/* XXX - can't deal with rebinding... */
525 			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
526 		if (vo->vo_economy == -1
527 		    || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
528 			/* XXX - see above. */
529 			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
530 	}
531 }
532 
533 static void
534 acpi_video_vo_destroy(struct acpi_video_output *vo)
535 {
536 	struct acpi_video_output_queue *voqh;
537 
538 	ACPI_ASSERTLOCK;
539 
540 	if (vo->vo_sysctl_tree != NULL) {
541 		vo->vo_sysctl_tree = NULL;
542 		sysctl_ctx_free(&vo->vo_sysctl_ctx);
543 	}
544 	if (vo->vo_levels != NULL)
545 		AcpiOsFree(vo->vo_levels);
546 
547 	switch (vo->adr & DOD_DEVID_MASK) {
548 	case DOD_DEVID_MONITOR:
549 		voqh = &crt_units;
550 		break;
551 	case DOD_DEVID_PANEL:
552 		voqh = &lcd_units;
553 		break;
554 	case DOD_DEVID_TV:
555 		voqh = &tv_units;
556 		break;
557 	default:
558 		voqh = &other_units;
559 	}
560 	/* XXX - needs locking for protecting STAILQ xxx_units. */
561 	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
562 	free(vo, M_ACPIVIDEO);
563 }
564 
565 static int
566 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
567 {
568 	int i;
569 
570 	if (vo->vo_levels == NULL)
571 		return (ENODEV);
572 	for (i = 0; i < vo->vo_numlevels; i++)
573 		if (vo->vo_levels[i] == level)
574 			return (0);
575 	return (EINVAL);
576 }
577 
578 /* ARGSUSED */
579 static int
580 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
581 {
582 	struct acpi_video_output *vo;
583 	int state, err;
584 	ACPI_LOCK_DECL;
585 
586 	ACPI_LOCK;
587 	vo = (struct acpi_video_output *)arg1;
588 	if (vo->handle == NULL) {
589 		err = ENXIO;
590 		goto out;
591 	}
592 	state = vo_get_device_status(vo->handle) & DCS_ACTIVE? 1 : 0;
593 	err = sysctl_handle_int(oidp, &state, 0, req);
594 	if (err != 0 || req->newptr == NULL)
595 		goto out;
596 	vo_set_device_state(vo->handle,
597 			    DSS_COMMIT | (state? DSS_ACTIVE : DSS_INACTIVE));
598 out:
599 	ACPI_UNLOCK;
600 	return (err);
601 }
602 
603 /* ARGSUSED */
604 static int
605 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
606 {
607 	struct acpi_video_output *vo;
608 	int level, preset, err;
609 	ACPI_LOCK_DECL;
610 
611 	ACPI_LOCK;
612 	vo = (struct acpi_video_output *)arg1;
613 	if (vo->handle == NULL) {
614 		err = ENXIO;
615 		goto out;
616 	}
617 	if (vo->vo_levels == NULL) {
618 		err = ENODEV;
619 		goto out;
620 	}
621 
622 	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY
623 		  ? vo->vo_economy
624 		  : vo->vo_fullpower);
625 	level = vo->vo_brightness;
626 	if (level == -1)
627 		level = preset;
628 
629 	err = sysctl_handle_int(oidp, &level, 0, req);
630 	if (err != 0 || req->newptr == NULL)
631 		goto out;
632 	if (level < -1 || level > 100) {
633 		err = EINVAL;
634 		goto out;
635 	}
636 
637 	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
638 		goto out;
639 	vo->vo_brightness = level;
640 	vo_set_brightness(vo->handle, level == -1? preset : level);
641 out:
642 	ACPI_UNLOCK;
643 	return (err);
644 }
645 
646 static int
647 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
648 {
649 	struct acpi_video_output *vo;
650 	int level, *preset, err = 0;
651 	ACPI_LOCK_DECL;
652 
653 	ACPI_LOCK;
654 	vo = (struct acpi_video_output *)arg1;
655 	if (vo->handle == NULL) {
656 		err = ENXIO;
657 		goto out;
658 	}
659 	if (vo->vo_levels == NULL) {
660 		err = ENODEV;
661 		goto out;
662 	}
663 	preset = (arg2 == POWER_PROFILE_ECONOMY
664 		  ? &vo->vo_economy
665 		  : &vo->vo_fullpower);
666 	level = *preset;
667 	err = sysctl_handle_int(oidp, &level, 0, req);
668 	if (err != 0 || req->newptr == NULL)
669 		goto out;
670 	if (level < -1 || level > 100) {
671 		err = EINVAL;
672 		goto out;
673 	}
674 	if (level == -1)
675 		level = vo->vo_levels
676 			[arg2 == POWER_PROFILE_ECONOMY
677 			 ? BCL_ECONOMY : BCL_FULLPOWER];
678 	else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
679 		goto out;
680 
681 	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
682 		vo_set_brightness(vo->handle, level);
683 	*preset = level;
684 out:
685 	ACPI_UNLOCK;
686 	return (err);
687 }
688 
689 /* ARGSUSED */
690 static int
691 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
692 {
693 	struct acpi_video_output *vo;
694 	int err;
695 	ACPI_LOCK_DECL;
696 
697 	ACPI_LOCK;
698 	vo = (struct acpi_video_output *)arg1;
699 	if (vo->vo_levels == NULL) {
700 		err = ENODEV;
701 		goto out;
702 	}
703 	if (req->newptr != NULL) {
704 		err = EPERM;
705 		goto out;
706 	}
707 	err = sysctl_handle_opaque(oidp, vo->vo_levels,
708 				vo->vo_numlevels * sizeof *vo->vo_levels, req);
709 out:
710 	ACPI_UNLOCK;
711 	return (err);
712 }
713 
714 static int
715 vid_check_requirements(ACPI_HANDLE handle)
716 {
717 	ACPI_HANDLE h_dod, h_dos;
718 	ACPI_OBJECT_TYPE t_dos;
719 
720 	ACPI_ASSERTLOCK;
721 
722 	/* check for _DOD, _DOS methods */
723 	return (ACPI_SUCCESS(AcpiGetHandle(handle, "_DOD", &h_dod))
724 	    && ACPI_SUCCESS(AcpiGetHandle(handle, "_DOS", &h_dos))
725 	    && ACPI_SUCCESS(AcpiGetType(h_dos, &t_dos))
726 	    && t_dos == ACPI_TYPE_METHOD);
727 }
728 
729 static void
730 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
731 {
732 	ACPI_STATUS status;
733 
734 	ACPI_ASSERTLOCK;
735 
736 	status = acpi_SetInteger(handle, "_DOS", policy);
737 	if (ACPI_FAILURE(status))
738 		printf("can't evaluate %s._DOS - %s\n",
739 		       acpi_name(handle), AcpiFormatException(status));
740 }
741 
742 struct enum_callback_arg {
743 	void (*callback)(ACPI_HANDLE, UINT32, void *);
744 	void *context;
745 	ACPI_OBJECT *dod_pkg;
746 };
747 
748 static ACPI_STATUS
749 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
750 		      void *context, void **retp)
751 {
752 	ACPI_STATUS status;
753 	ACPI_OBJECT *tmp;
754 	UINT32 adr;
755 	struct enum_callback_arg *argset;
756 	size_t i;
757 
758 	argset = context;
759 	status = acpi_GetInteger(handle, "_ADR", &adr);
760 	if (ACPI_SUCCESS(status)) {
761 		for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
762 			tmp = &argset->dod_pkg->Package.Elements[i];
763 			if (tmp != NULL && tmp->Type == ACPI_TYPE_INTEGER &&
764 			    (tmp->Integer.Value & DOD_DEVID_MASK) == adr) {
765 				argset->callback(handle, tmp->Integer.Value,
766 						 argset->context);
767 				(**(int**)retp)++;
768 			}
769 		}
770 	}
771 
772 	return (AE_OK);
773 }
774 
775 static int
776 vid_enum_outputs(ACPI_HANDLE handle,
777 		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
778 {
779 	ACPI_STATUS status;
780 	ACPI_BUFFER dod_buf;
781 	ACPI_OBJECT *res;
782 	int num = 0;
783 	void *pnum;
784 	struct enum_callback_arg argset;
785 
786 	ACPI_ASSERTLOCK;
787 	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
788 	dod_buf.Pointer = NULL;
789 	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
790 	if (ACPI_FAILURE(status)) {
791 		if (status != AE_NOT_FOUND)
792 			printf("can't evaluate %s._DOD - %s\n",
793 			       acpi_name(handle), AcpiFormatException(status));
794 		num = -1;
795 		goto out;
796 	}
797 	res = (ACPI_OBJECT *)dod_buf.Pointer;
798 	if (res == NULL || res->Type != ACPI_TYPE_PACKAGE) {
799 		printf("evaluation of %s._DOD makes no sense\n",
800 		       acpi_name(handle));
801 		num = -1;
802 		goto out;
803 	}
804 	if (callback == NULL) {
805 		num = res->Package.Count;
806 		goto out;
807 	}
808 	argset.callback = callback;
809 	argset.context  = context;
810 	argset.dod_pkg  = res;
811 	pnum = &num;
812 	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
813 				   vid_enum_outputs_subr, &argset,
814 				   &pnum);
815 	if (ACPI_FAILURE(status))
816 		printf("failed walking down %s - %s\n",
817 		       acpi_name(handle), AcpiFormatException(status));
818 out:
819 	if (dod_buf.Pointer != NULL)
820 		AcpiOsFree(dod_buf.Pointer);
821 	return (num);
822 }
823 
824 static int
825 vo_query_brightness_levels(ACPI_HANDLE handle, int **levelp)
826 {
827 	ACPI_STATUS status;
828 	ACPI_BUFFER bcl_buf;
829 	ACPI_OBJECT *res, *tmp;
830 	int num = 0, i, n, *levels;
831 
832 	ACPI_ASSERTLOCK;
833 	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
834 	bcl_buf.Pointer = NULL;
835 	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
836 	if (ACPI_FAILURE(status)) {
837 		if (status != AE_NOT_FOUND)
838 			printf("can't evaluate %s._BCL - %s\n",
839 			       acpi_name(handle), AcpiFormatException(status));
840 		num = -1;
841 		goto out;
842 	}
843 	res = (ACPI_OBJECT *)bcl_buf.Pointer;
844 	if (res == NULL || res->Type != ACPI_TYPE_PACKAGE ||
845 	    res->Package.Count < 2) {
846 		printf("evaluation of %s._BCL makes no sense\n",
847 		       acpi_name(handle));
848 		num = -1;
849 		goto out;
850 	}
851 	num = res->Package.Count;
852 	if (levelp == NULL)
853 		goto out;
854 	levels = AcpiOsAllocate(num * sizeof *levels);
855 	if (levels == NULL) {
856 		num = -1;
857 		goto out;
858 	}
859 	for (i = 0, n = 0; i < num; i++) {
860 		tmp = &res->Package.Elements[i];
861 		if (tmp != NULL && tmp->Type == ACPI_TYPE_INTEGER)
862 			levels[n++] = tmp->Integer.Value;
863 	}
864 	if (n < 2) {
865 		num = -1;
866 		AcpiOsFree(levels);
867 	} else {
868 		num = n;
869 		*levelp = levels;
870 	}
871 out:
872 	if (bcl_buf.Pointer != NULL)
873 		AcpiOsFree(bcl_buf.Pointer);
874 
875 	return (num);
876 }
877 
878 static void
879 vo_set_brightness(ACPI_HANDLE handle, int level)
880 {
881 	ACPI_STATUS status;
882 
883 	ACPI_ASSERTLOCK;
884 
885 	status = acpi_SetInteger(handle, "_BCM", level);
886 	if (ACPI_FAILURE(status))
887 		printf("can't evaluate %s._BCM - %s\n",
888 		       acpi_name(handle), AcpiFormatException(status));
889 }
890 
891 static UINT32
892 vo_get_device_status(ACPI_HANDLE handle)
893 {
894 	UINT32 dcs = 0;
895 	ACPI_STATUS status;
896 
897 	ACPI_ASSERTLOCK;
898 	status = acpi_GetInteger(handle, "_DCS", &dcs);
899 	if (ACPI_FAILURE(status))
900 		printf("can't evaluate %s._DCS - %s\n",
901 		       acpi_name(handle), AcpiFormatException(status));
902 
903 	return (dcs);
904 }
905 
906 static UINT32
907 vo_query_graphics_state(ACPI_HANDLE handle)
908 {
909 	UINT32 dgs = 0;
910 	ACPI_STATUS status;
911 
912 	ACPI_ASSERTLOCK;
913 	status = acpi_GetInteger(handle, "_DGS", &dgs);
914 	if (ACPI_FAILURE(status))
915 		printf("can't evaluate %s._DGS - %s\n",
916 		       acpi_name(handle), AcpiFormatException(status));
917 
918 	return (dgs);
919 }
920 
921 static void
922 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
923 {
924 	ACPI_STATUS status;
925 
926 	ACPI_ASSERTLOCK;
927 
928 	status = acpi_SetInteger(handle, "_DSS", state);
929 	if (ACPI_FAILURE(status))
930 		printf("can't evaluate %s._DSS - %s\n",
931 		       acpi_name(handle), AcpiFormatException(status));
932 }
933