aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/acpica/acpi_video.c
blob: ada6b1e5e93c2873b2f7f828231d9c899aac8f9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
/*-
 * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: acpi_vid.c,v 1.4 2003/10/13 10:07:36 taku Exp $
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/power.h>
#include <sys/queue.h>
#include <sys/sysctl.h>

#include <contrib/dev/acpica/include/acpi.h>

#include <dev/acpica/acpivar.h>

/* ACPI video extension driver. */
struct acpi_video_output {
	ACPI_HANDLE	handle;
	UINT32		adr;
	STAILQ_ENTRY(acpi_video_output) vo_next;
	struct {
		int	num;
		STAILQ_ENTRY(acpi_video_output) next;
	} vo_unit;
	int		vo_brightness;
	int		vo_fullpower;
	int		vo_economy;
	int		vo_numlevels;
	int		*vo_levels;
	struct sysctl_ctx_list vo_sysctl_ctx;
	struct sysctl_oid *vo_sysctl_tree;
};

STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);

struct acpi_video_softc {
	device_t		device;
	ACPI_HANDLE		handle;
	struct acpi_video_output_queue vid_outputs;
	eventhandler_tag	vid_pwr_evh;
};

/* interfaces */
static int	acpi_video_modevent(struct module*, int, void *);
static void	acpi_video_identify(driver_t *driver, device_t parent);
static int	acpi_video_probe(device_t);
static int	acpi_video_attach(device_t);
static int	acpi_video_detach(device_t);
static int	acpi_video_shutdown(device_t);
static void	acpi_video_notify_handler(ACPI_HANDLE, UINT32, void *);
static void	acpi_video_power_profile(void *);
static void	acpi_video_bind_outputs(struct acpi_video_softc *);
static struct acpi_video_output *acpi_video_vo_init(UINT32);
static void	acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
static void	acpi_video_vo_destroy(struct acpi_video_output *);
static int	acpi_video_vo_check_level(struct acpi_video_output *, int);
static void	acpi_video_vo_notify_handler(ACPI_HANDLE, UINT32, void *);
static int	acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
static int	acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
static int	acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
static int	acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);

/* operations */
static void	vid_set_switch_policy(ACPI_HANDLE, UINT32);
static int	vid_enum_outputs(ACPI_HANDLE,
		    void(*)(ACPI_HANDLE, UINT32, void *), void *);
static int	vo_get_brightness_levels(ACPI_HANDLE, int **);
static int	vo_get_brightness(ACPI_HANDLE);
static void	vo_set_brightness(ACPI_HANDLE, int);
static UINT32	vo_get_device_status(ACPI_HANDLE);
static UINT32	vo_get_graphics_state(ACPI_HANDLE);
static void	vo_set_device_state(ACPI_HANDLE, UINT32);

/* events */
#define VID_NOTIFY_SWITCHED	0x80
#define VID_NOTIFY_REPROBE	0x81
#define	VID_NOTIFY_INC_BRN	0x86
#define	VID_NOTIFY_DEC_BRN	0x87

/* _DOS (Enable/Disable Output Switching) argument bits */
#define DOS_SWITCH_MASK		3
#define DOS_SWITCH_BY_OSPM	0
#define DOS_SWITCH_BY_BIOS	1
#define DOS_SWITCH_LOCKED	2
#define DOS_BRIGHTNESS_BY_BIOS	(1 << 2)

/* _DOD and subdev's _ADR */
#define DOD_DEVID_MASK		0x0f00
#define DOD_DEVID_MASK_FULL	0xffff
#define DOD_DEVID_MASK_DISPIDX	0x000f
#define DOD_DEVID_MASK_DISPPORT	0x00f0
#define DOD_DEVID_MONITOR	0x0100
#define DOD_DEVID_LCD		0x0110
#define DOD_DEVID_TV		0x0200
#define DOD_DEVID_EXT		0x0300
#define DOD_DEVID_INTDFP	0x0400
#define DOD_BIOS		(1 << 16)
#define DOD_NONVGA		(1 << 17)
#define DOD_HEAD_ID_SHIFT	18
#define DOD_HEAD_ID_BITS	3
#define DOD_HEAD_ID_MASK \
		(((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
#define DOD_DEVID_SCHEME_STD	(1 << 31)

/* _BCL related constants */
#define BCL_FULLPOWER		0
#define BCL_ECONOMY		1

/* _DCS (Device Currrent Status) value bits and masks. */
#define DCS_EXISTS		(1 << 0)
#define DCS_ACTIVE		(1 << 1)
#define DCS_READY		(1 << 2)
#define DCS_FUNCTIONAL		(1 << 3)
#define DCS_ATTACHED		(1 << 4)

/* _DSS (Device Set Status) argument bits and masks. */
#define DSS_INACTIVE		0
#define DSS_ACTIVE		(1 << 0)
#define DSS_SETNEXT		(1 << 30)
#define DSS_COMMIT		(1 << 31)

static device_method_t acpi_video_methods[] = {
	DEVMETHOD(device_identify, acpi_video_identify),
	DEVMETHOD(device_probe, acpi_video_probe),
	DEVMETHOD(device_attach, acpi_video_attach),
	DEVMETHOD(device_detach, acpi_video_detach),
	DEVMETHOD(device_shutdown, acpi_video_shutdown),
	{ 0, 0 }
};

static driver_t acpi_video_driver = {
	"acpi_video",
	acpi_video_methods,
	sizeof(struct acpi_video_softc),
};

static devclass_t acpi_video_devclass;

DRIVER_MODULE(acpi_video, vgapci, acpi_video_driver, acpi_video_devclass,
	      acpi_video_modevent, NULL);
MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);

static struct sysctl_ctx_list	acpi_video_sysctl_ctx;
static struct sysctl_oid	*acpi_video_sysctl_tree;
static struct acpi_video_output_queue crt_units, tv_units,
    ext_units, lcd_units, other_units;

/*
 * The 'video' lock protects the hierarchy of video output devices
 * (the video "bus").  The 'video_output' lock protects per-output
 * data is equivalent to a softc lock for each video output.
 */
ACPI_SERIAL_DECL(video, "ACPI video");
ACPI_SERIAL_DECL(video_output, "ACPI video output");
MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");

static int
acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
{
	int err;

	err = 0;
	switch (evt) {
	case MOD_LOAD:
		sysctl_ctx_init(&acpi_video_sysctl_ctx);
		STAILQ_INIT(&crt_units);
		STAILQ_INIT(&tv_units);
		STAILQ_INIT(&ext_units);
		STAILQ_INIT(&lcd_units);
		STAILQ_INIT(&other_units);
		break;
	case MOD_UNLOAD:
		sysctl_ctx_free(&acpi_video_sysctl_ctx);
		acpi_video_sysctl_tree = NULL;
		break;
	default:
		err = EINVAL;
	}

	return (err);
}

static void
acpi_video_identify(driver_t *driver, device_t parent)
{

	if (device_find_child(parent, "acpi_video", -1) == NULL)
		device_add_child(parent, "acpi_video", -1);
}

static int
acpi_video_probe(device_t dev)
{
	ACPI_HANDLE devh, h;
	ACPI_OBJECT_TYPE t_dos;

	devh = acpi_get_handle(dev);
	if (acpi_disabled("video") ||
	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
	    ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
	    ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
	    t_dos != ACPI_TYPE_METHOD)
		return (ENXIO);

	device_set_desc(dev, "ACPI video extension");
	return (0);
}

static int
acpi_video_attach(device_t dev)
{
	struct acpi_softc *acpi_sc;
	struct acpi_video_softc *sc;

	sc = device_get_softc(dev);

	acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
	if (acpi_sc == NULL)
		return (ENXIO);
	ACPI_SERIAL_BEGIN(video);
	if (acpi_video_sysctl_tree == NULL) {
		acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
				    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
				    OID_AUTO, "video", CTLFLAG_RD, 0,
				    "video extension control");
	}
	ACPI_SERIAL_END(video);

	sc->device = dev;
	sc->handle = acpi_get_handle(dev);
	STAILQ_INIT(&sc->vid_outputs);

	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
				 acpi_video_notify_handler, sc);
	sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
				 acpi_video_power_profile, sc, 0);

	ACPI_SERIAL_BEGIN(video);
	acpi_video_bind_outputs(sc);
	ACPI_SERIAL_END(video);

	/*
	 * Notify the BIOS that we want to switch both active outputs and
	 * brightness levels.
	 */
	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
	    DOS_BRIGHTNESS_BY_BIOS);

	acpi_video_power_profile(sc);

	return (0);
}

static int
acpi_video_detach(device_t dev)
{
	struct acpi_video_softc *sc;
	struct acpi_video_output *vo, *vn;

	sc = device_get_softc(dev);

	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
	EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
				acpi_video_notify_handler);

	ACPI_SERIAL_BEGIN(video);
	STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vn) {
		acpi_video_vo_destroy(vo);
	}
	ACPI_SERIAL_END(video);

	return (0);
}

static int
acpi_video_shutdown(device_t dev)
{
	struct acpi_video_softc *sc;

	sc = device_get_softc(dev);
	vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);

	return (0);
}

static void
acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
{
	struct acpi_video_softc *sc;
	struct acpi_video_output *vo, *vo_tmp;
	ACPI_HANDLE lasthand;
	UINT32 dcs, dss, dss_p;

	sc = (struct acpi_video_softc *)context;

	switch (notify) {
	case VID_NOTIFY_SWITCHED:
		dss_p = 0;
		lasthand = NULL;
		ACPI_SERIAL_BEGIN(video);
		ACPI_SERIAL_BEGIN(video_output);
		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
			dss = vo_get_graphics_state(vo->handle);
			dcs = vo_get_device_status(vo->handle);
			if (!(dcs & DCS_READY))
				dss = DSS_INACTIVE;
			if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
			    (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
				if (lasthand != NULL)
					vo_set_device_state(lasthand, dss_p);
				dss_p = dss;
				lasthand = vo->handle;
			}
		}
		if (lasthand != NULL)
			vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
		ACPI_SERIAL_END(video_output);
		ACPI_SERIAL_END(video);
		break;
	case VID_NOTIFY_REPROBE:
		ACPI_SERIAL_BEGIN(video);
		STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
			vo->handle = NULL;
		acpi_video_bind_outputs(sc);
		STAILQ_FOREACH_SAFE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
			if (vo->handle == NULL) {
				STAILQ_REMOVE(&sc->vid_outputs, vo,
				    acpi_video_output, vo_next);
				acpi_video_vo_destroy(vo);
			}
		}
		ACPI_SERIAL_END(video);
		break;
	default:
		device_printf(sc->device, "unknown notify event 0x%x\n",
		    notify);
	}
}

static void
acpi_video_power_profile(void *context)
{
	int state;
	struct acpi_video_softc *sc;
	struct acpi_video_output *vo;

	sc = context;
	state = power_profile_get_state();
	if (state != POWER_PROFILE_PERFORMANCE &&
	    state != POWER_PROFILE_ECONOMY)
		return;

	ACPI_SERIAL_BEGIN(video);
	ACPI_SERIAL_BEGIN(video_output);
	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
		if (vo->vo_levels != NULL && vo->vo_brightness == -1)
			vo_set_brightness(vo->handle,
			    state == POWER_PROFILE_ECONOMY ?
			    vo->vo_economy : vo->vo_fullpower);
	}
	ACPI_SERIAL_END(video_output);
	ACPI_SERIAL_END(video);
}

static void
acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
{
	struct acpi_video_softc *sc;
	struct acpi_video_output *vo;

	ACPI_SERIAL_ASSERT(video);
	sc = context;

	STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
		if (vo->adr == adr) {
			acpi_video_vo_bind(vo, handle);
			return;
		}
	}
	vo = acpi_video_vo_init(adr);
	if (vo != NULL) {
		acpi_video_vo_bind(vo, handle);
		STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
	}
}

static void
acpi_video_bind_outputs(struct acpi_video_softc *sc)
{

	ACPI_SERIAL_ASSERT(video);
	vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
}

static struct acpi_video_output *
acpi_video_vo_init(UINT32 adr)
{
	struct acpi_video_output *vn, *vo, *vp;
	int n, x;
	int display_index;
	int display_port;
	char name[8], env[32];
	const char *type, *desc;
	struct acpi_video_output_queue *voqh;

	ACPI_SERIAL_ASSERT(video);
	display_index = adr & DOD_DEVID_MASK_DISPIDX;
	display_port = (adr & DOD_DEVID_MASK_DISPPORT) >> 4;

	switch (adr & DOD_DEVID_MASK) {
	case DOD_DEVID_MONITOR:
		if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
			/* DOD_DEVID_LCD is a common, backward compatible ID */
			desc = "Internal/Integrated Digital Flat Panel";
			type = "lcd";
			voqh = &lcd_units;
		} else {
			desc = "VGA CRT or VESA Compatible Analog Monitor";
			type = "crt";
			voqh = &crt_units;
		}
		break;
	case DOD_DEVID_TV:
		desc = "TV/HDTV or Analog-Video Monitor";
		type = "tv";
		voqh = &tv_units;
		break;
	case DOD_DEVID_EXT:
		desc = "External Digital Monitor";
		type = "ext";
		voqh = &ext_units;
		break;
	case DOD_DEVID_INTDFP:
		desc = "Internal/Integrated Digital Flat Panel";
		type = "lcd";
		voqh = &lcd_units;
		break;
	default:
		desc = "unknown output";
		type = "out";
		voqh = &other_units;
	}

	n = 0;
	vn = vp = NULL;
	STAILQ_FOREACH(vn, voqh, vo_unit.next) {
		if (vn->vo_unit.num != n)
			break;
		vp = vn;
		n++;
	}

	snprintf(name, sizeof(name), "%s%d", type, n);

	vo = malloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
	if (vo != NULL) {
		vo->handle = NULL;
		vo->adr = adr;
		vo->vo_unit.num = n;
		vo->vo_brightness = -1;
		vo->vo_fullpower = -1;	/* TODO: override with tunables */
		vo->vo_economy = -1;
		vo->vo_numlevels = 0;
		vo->vo_levels = NULL;
		snprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
		if (getenv_int(env, &x))
			vo->vo_fullpower = x;
		snprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
		if (getenv_int(env, &x))
			vo->vo_economy = x;

		sysctl_ctx_init(&vo->vo_sysctl_ctx);
		if (vp != NULL)
			STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
		else
			STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
		if (acpi_video_sysctl_tree != NULL)
			vo->vo_sysctl_tree =
			    SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
				SYSCTL_CHILDREN(acpi_video_sysctl_tree),
				OID_AUTO, name, CTLFLAG_RD, 0, desc);
		if (vo->vo_sysctl_tree != NULL) {
			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
			    OID_AUTO, "active",
			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
			    acpi_video_vo_active_sysctl, "I",
			    "current activity of this device");
			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
			    OID_AUTO, "brightness",
			    CTLTYPE_INT|CTLFLAG_RW, vo, 0,
			    acpi_video_vo_bright_sysctl, "I",
			    "current brightness level");
			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
			    OID_AUTO, "fullpower",
			    CTLTYPE_INT|CTLFLAG_RW, vo,
			    POWER_PROFILE_PERFORMANCE,
			    acpi_video_vo_presets_sysctl, "I",
			    "preset level for full power mode");
			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
			    OID_AUTO, "economy",
			    CTLTYPE_INT|CTLFLAG_RW, vo,
			    POWER_PROFILE_ECONOMY,
			    acpi_video_vo_presets_sysctl, "I",
			    "preset level for economy mode");
			SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
			    SYSCTL_CHILDREN(vo->vo_sysctl_tree),
			    OID_AUTO, "levels",
			    CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
			    acpi_video_vo_levels_sysctl, "I",
			    "supported brightness levels");
		} else
			printf("%s: sysctl node creation failed\n", type);
	} else
		printf("%s: softc allocation failed\n", type);

	if (bootverbose) {
		printf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
		printf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
		printf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
		if (adr & DOD_BIOS)
			printf(", detectable by BIOS");
		if (adr & DOD_NONVGA)
			printf(" (Non-VGA output device whose power "
			    "is related to the VGA device)");
		printf(", head #%d\n",
			(adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
	}
	return (vo);
}

static void
acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
{

	ACPI_SERIAL_BEGIN(video_output);
	if (vo->vo_levels != NULL)
		AcpiOsFree(vo->vo_levels);
	vo->handle = handle;
	vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
	if (vo->vo_numlevels >= 2) {
		if (vo->vo_fullpower == -1
		    || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
			/* XXX - can't deal with rebinding... */
			vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
		if (vo->vo_economy == -1
		    || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
			/* XXX - see above. */
			vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
	}
	if (vo->vo_levels != NULL)
	    AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
		acpi_video_vo_notify_handler, vo);
	ACPI_SERIAL_END(video_output);
}

static void
acpi_video_vo_destroy(struct acpi_video_output *vo)
{
	struct acpi_video_output_queue *voqh;

	ACPI_SERIAL_ASSERT(video);
	if (vo->vo_sysctl_tree != NULL) {
		vo->vo_sysctl_tree = NULL;
		sysctl_ctx_free(&vo->vo_sysctl_ctx);
	}
	if (vo->vo_levels != NULL) {
		AcpiRemoveNotifyHandler(vo->handle, ACPI_DEVICE_NOTIFY,
		    acpi_video_vo_notify_handler);
		AcpiOsFree(vo->vo_levels);
	}

	switch (vo->adr & DOD_DEVID_MASK) {
	case DOD_DEVID_MONITOR:
		voqh = &crt_units;
		break;
	case DOD_DEVID_TV:
		voqh = &tv_units;
		break;
	case DOD_DEVID_EXT:
		voqh = &ext_units;
		break;
	case DOD_DEVID_INTDFP:
		voqh = &lcd_units;
		break;
	default:
		voqh = &other_units;
	}
	STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
	free(vo, M_ACPIVIDEO);
}

static int
acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
{
	int i;

	ACPI_SERIAL_ASSERT(video_output);
	if (vo->vo_levels == NULL)
		return (ENODEV);
	for (i = 0; i < vo->vo_numlevels; i++)
		if (vo->vo_levels[i] == level)
			return (0);
	return (EINVAL);
}

static void
acpi_video_vo_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
{
	struct acpi_video_output *vo;
	int i, j, level, new_level;

	vo = context;
	ACPI_SERIAL_BEGIN(video_output);
	if (vo->handle == NULL) {
		ACPI_SERIAL_END(video_output);
		return;
	}

	switch (notify) {
	case VID_NOTIFY_INC_BRN:
	case VID_NOTIFY_DEC_BRN:
		if (vo->vo_levels == NULL)
			break;
		level = vo_get_brightness(vo->handle);
		if (level < 0)
			break;
		new_level = level;
		for (i = 0; i < vo->vo_numlevels; i++) {
			j = vo->vo_levels[i];
			if (notify == VID_NOTIFY_INC_BRN) {
				if (j > level &&
				    (j < new_level || level == new_level))
					new_level = j;
			} else {
				if (j < level &&
				    (j > new_level || level == new_level))
					new_level = j;
			}
		}
		if (new_level != level) {
			vo_set_brightness(vo->handle, new_level);
			vo->vo_brightness = new_level;
		}
		break;
	default:
		printf("%s: unknown notify event 0x%x\n",
		    acpi_name(vo->handle), notify);
	}
	ACPI_SERIAL_END(video_output);
}

/* ARGSUSED */
static int
acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct acpi_video_output *vo;
	int state, err;

	vo = (struct acpi_video_output *)arg1;
	if (vo->handle == NULL)
		return (ENXIO);
	ACPI_SERIAL_BEGIN(video_output);
	state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
	err = sysctl_handle_int(oidp, &state, 0, req);
	if (err != 0 || req->newptr == NULL)
		goto out;
	vo_set_device_state(vo->handle,
	    DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
out:
	ACPI_SERIAL_END(video_output);
	return (err);
}

/* ARGSUSED */
static int
acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct acpi_video_output *vo;
	int level, preset, err;

	vo = (struct acpi_video_output *)arg1;
	ACPI_SERIAL_BEGIN(video_output);
	if (vo->handle == NULL) {
		err = ENXIO;
		goto out;
	}
	if (vo->vo_levels == NULL) {
		err = ENODEV;
		goto out;
	}

	preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
		  vo->vo_economy : vo->vo_fullpower;
	level = vo->vo_brightness;
	if (level == -1)
		level = preset;

	err = sysctl_handle_int(oidp, &level, 0, req);
	if (err != 0 || req->newptr == NULL)
		goto out;
	if (level < -1 || level > 100) {
		err = EINVAL;
		goto out;
	}

	if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
		goto out;
	vo->vo_brightness = level;
	vo_set_brightness(vo->handle, (level == -1) ? preset : level);

out:
	ACPI_SERIAL_END(video_output);
	return (err);
}

static int
acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct acpi_video_output *vo;
	int i, level, *preset, err;

	err = 0;
	vo = (struct acpi_video_output *)arg1;
	ACPI_SERIAL_BEGIN(video_output);
	if (vo->handle == NULL) {
		err = ENXIO;
		goto out;
	}
	if (vo->vo_levels == NULL) {
		err = ENODEV;
		goto out;
	}
	preset = (arg2 == POWER_PROFILE_ECONOMY) ?
		  &vo->vo_economy : &vo->vo_fullpower;
	level = *preset;
	err = sysctl_handle_int(oidp, &level, 0, req);
	if (err != 0 || req->newptr == NULL)
		goto out;
	if (level < -1 || level > 100) {
		err = EINVAL;
		goto out;
	}
	if (level == -1) {
		i = (arg2 == POWER_PROFILE_ECONOMY) ?
		    BCL_ECONOMY : BCL_FULLPOWER;
		level = vo->vo_levels[i];
	} else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
		goto out;

	if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
		vo_set_brightness(vo->handle, level);
	*preset = level;

out:
	ACPI_SERIAL_END(video_output);
	return (err);
}

/* ARGSUSED */
static int
acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct acpi_video_output *vo;
	int err;

	vo = (struct acpi_video_output *)arg1;
	ACPI_SERIAL_BEGIN(video_output);
	if (vo->vo_levels == NULL) {
		err = ENODEV;
		goto out;
	}
	if (req->newptr != NULL) {
		err = EPERM;
		goto out;
	}
	err = sysctl_handle_opaque(oidp, vo->vo_levels,
	    vo->vo_numlevels * sizeof(*vo->vo_levels), req);

out:
	ACPI_SERIAL_END(video_output);
	return (err);
}

static void
vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
{
	ACPI_STATUS status;

	status = acpi_SetInteger(handle, "_DOS", policy);
	if (ACPI_FAILURE(status))
		printf("can't evaluate %s._DOS - %s\n",
		       acpi_name(handle), AcpiFormatException(status));
}

struct enum_callback_arg {
	void (*callback)(ACPI_HANDLE, UINT32, void *);
	void *context;
	ACPI_OBJECT *dod_pkg;
	int count;
};

static ACPI_STATUS
vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
		      void *context, void **retp __unused)
{
	ACPI_STATUS status;
	UINT32 adr, val;
	struct enum_callback_arg *argset;
	size_t i;

	ACPI_SERIAL_ASSERT(video);
	argset = context;
	status = acpi_GetInteger(handle, "_ADR", &adr);
	if (ACPI_FAILURE(status))
		return (AE_OK);

	for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
		if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
		    (val & DOD_DEVID_MASK_FULL) == adr) {
			argset->callback(handle, val, argset->context);
			argset->count++;
		}
	}

	return (AE_OK);
}

static int
vid_enum_outputs(ACPI_HANDLE handle,
		 void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
{
	ACPI_STATUS status;
	ACPI_BUFFER dod_buf;
	ACPI_OBJECT *res;
	struct enum_callback_arg argset;

	ACPI_SERIAL_ASSERT(video);
	dod_buf.Length = ACPI_ALLOCATE_BUFFER;
	dod_buf.Pointer = NULL;
	status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND)
			printf("can't evaluate %s._DOD - %s\n",
			       acpi_name(handle), AcpiFormatException(status));
		argset.count = -1;
		goto out;
	}
	res = (ACPI_OBJECT *)dod_buf.Pointer;
	if (!ACPI_PKG_VALID(res, 1)) {
		printf("evaluation of %s._DOD makes no sense\n",
		       acpi_name(handle));
		argset.count = -1;
		goto out;
	}
	if (callback == NULL) {
		argset.count = res->Package.Count;
		goto out;
	}
	argset.callback = callback;
	argset.context  = context;
	argset.dod_pkg  = res;
	argset.count    = 0;
	status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
	    vid_enum_outputs_subr, NULL, &argset, NULL);
	if (ACPI_FAILURE(status))
		printf("failed walking down %s - %s\n",
		       acpi_name(handle), AcpiFormatException(status));
out:
	if (dod_buf.Pointer != NULL)
		AcpiOsFree(dod_buf.Pointer);
	return (argset.count);
}

static int
vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
{
	ACPI_STATUS status;
	ACPI_BUFFER bcl_buf;
	ACPI_OBJECT *res;
	int num, i, n, *levels;

	num = 0;
	bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
	bcl_buf.Pointer = NULL;
	status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND)
			printf("can't evaluate %s._BCL - %s\n",
			       acpi_name(handle), AcpiFormatException(status));
		num = -1;
		goto out;
	}
	res = (ACPI_OBJECT *)bcl_buf.Pointer;
	if (!ACPI_PKG_VALID(res, 2)) {
		printf("evaluation of %s._BCL makes no sense\n",
		       acpi_name(handle));
		num = -1;
		goto out;
	}
	num = res->Package.Count;
	if (levelp == NULL)
		goto out;
	levels = AcpiOsAllocate(num * sizeof(*levels));
	if (levels == NULL) {
		num = -1;
		goto out;
	}
	for (i = 0, n = 0; i < num; i++)
		if (acpi_PkgInt32(res, i, &levels[n]) == 0)
			n++;
	if (n < 2) {
		num = -1;
		AcpiOsFree(levels);
	} else {
		num = n;
		*levelp = levels;
	}
out:
	if (bcl_buf.Pointer != NULL)
		AcpiOsFree(bcl_buf.Pointer);

	return (num);
}

static int
vo_get_brightness(ACPI_HANDLE handle)
{
	UINT32 level;
	ACPI_STATUS status;

	ACPI_SERIAL_ASSERT(video_output);
	status = acpi_GetInteger(handle, "_BQC", &level);
	if (ACPI_FAILURE(status)) {
		printf("can't evaluate %s._BQC - %s\n", acpi_name(handle),
		    AcpiFormatException(status));
		return (-1);
	}
	if (level > 100)
		return (-1);

	return (level);
}

static void
vo_set_brightness(ACPI_HANDLE handle, int level)
{
	ACPI_STATUS status;

	ACPI_SERIAL_ASSERT(video_output);
	status = acpi_SetInteger(handle, "_BCM", level);
	if (ACPI_FAILURE(status))
		printf("can't evaluate %s._BCM - %s\n",
		       acpi_name(handle), AcpiFormatException(status));
}

static UINT32
vo_get_device_status(ACPI_HANDLE handle)
{
	UINT32 dcs;
	ACPI_STATUS status;

	ACPI_SERIAL_ASSERT(video_output);
	dcs = 0;
	status = acpi_GetInteger(handle, "_DCS", &dcs);
	if (ACPI_FAILURE(status))
		printf("can't evaluate %s._DCS - %s\n",
		       acpi_name(handle), AcpiFormatException(status));

	return (dcs);
}

static UINT32
vo_get_graphics_state(ACPI_HANDLE handle)
{
	UINT32 dgs;
	ACPI_STATUS status;

	dgs = 0;
	status = acpi_GetInteger(handle, "_DGS", &dgs);
	if (ACPI_FAILURE(status))
		printf("can't evaluate %s._DGS - %s\n",
		       acpi_name(handle), AcpiFormatException(status));

	return (dgs);
}

static void
vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
{
	ACPI_STATUS status;

	ACPI_SERIAL_ASSERT(video_output);
	status = acpi_SetInteger(handle, "_DSS", state);
	if (ACPI_FAILURE(status))
		printf("can't evaluate %s._DSS - %s\n",
		       acpi_name(handle), AcpiFormatException(status));
}