kernel: Move sys/dev/acpica5 to sys/dev/acpica.
[dragonfly.git] / sys / dev / acpica / acpi_video / acpi_video.c
1 /*-
2  * Copyright (c) 2002-2003 Taku YAMAMOTO <taku@cent.saitama-u.ac.jp>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/acpica/acpi_video.c,v 1.13 2006/08/10 13:18:02 bruno Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/power.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38
39 #include "acpi.h"
40 #include "acpivar.h"
41
42 /* ACPI video extension driver. */
43 struct acpi_video_output {
44         ACPI_HANDLE     handle;
45         UINT32          adr;
46         STAILQ_ENTRY(acpi_video_output) vo_next;
47         struct {
48                 int     num;
49                 STAILQ_ENTRY(acpi_video_output) next;
50         } vo_unit;
51         int             vo_brightness;
52         int             vo_fullpower;
53         int             vo_economy;
54         int             vo_numlevels;
55         int             *vo_levels;
56         struct sysctl_ctx_list vo_sysctl_ctx;
57         struct sysctl_oid *vo_sysctl_tree;
58 };
59
60 STAILQ_HEAD(acpi_video_output_queue, acpi_video_output);
61
62 struct acpi_video_softc {
63         device_t                device;
64         ACPI_HANDLE             handle;
65         struct acpi_video_output_queue vid_outputs;
66         eventhandler_tag        vid_pwr_evh;
67 };
68
69 /* interfaces */
70 static int      acpi_video_modevent(struct module*, int, void *);
71 static 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 *acpi_video_vo_init(UINT32);
79 static void     acpi_video_vo_bind(struct acpi_video_output *, ACPI_HANDLE);
80 static void     acpi_video_vo_destroy(struct acpi_video_output *);
81 static int      acpi_video_vo_check_level(struct acpi_video_output *, int);
82 static int      acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS);
83 static int      acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS);
84 static int      acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS);
85 static int      acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS);
86
87 /* operations */
88 static void     vid_set_switch_policy(ACPI_HANDLE, UINT32);
89 static int      vid_enum_outputs(ACPI_HANDLE,
90                     void(*)(ACPI_HANDLE, UINT32, void *), void *);
91 static int      vo_get_brightness_levels(ACPI_HANDLE, int **);
92 static void     vo_set_brightness(ACPI_HANDLE, int);
93 static UINT32   vo_get_device_status(ACPI_HANDLE);
94 static UINT32   vo_get_graphics_state(ACPI_HANDLE);
95 static void     vo_set_device_state(ACPI_HANDLE, UINT32);
96
97 /* events */
98 #define VID_NOTIFY_SWITCHED     0x80
99 #define VID_NOTIFY_REPROBE      0x81
100
101 /* _DOS (Enable/Disable Output Switching) argument bits */
102 #define DOS_SWITCH_MASK         3
103 #define DOS_SWITCH_BY_OSPM      0
104 #define DOS_SWITCH_BY_BIOS      1
105 #define DOS_SWITCH_LOCKED       2
106 #define DOS_BRIGHTNESS_BY_BIOS  (1 << 2)
107
108 /* _DOD and subdev's _ADR */
109 #define DOD_DEVID_MASK          0x0f00
110 #define DOD_DEVID_MASK_FULL     0xffff
111 #define DOD_DEVID_MASK_DISPIDX  0x000f
112 #define DOD_DEVID_MASK_DISPPORT 0x00f0
113 #define DOD_DEVID_MONITOR       0x0100
114 #define DOD_DEVID_LCD           0x0110
115 #define DOD_DEVID_TV            0x0200
116 #define DOD_DEVID_EXT           0x0300
117 #define DOD_DEVID_INTDFP        0x0400
118 #define DOD_BIOS                (1 << 16)
119 #define DOD_NONVGA              (1 << 17)
120 #define DOD_HEAD_ID_SHIFT       18
121 #define DOD_HEAD_ID_BITS        3
122 #define DOD_HEAD_ID_MASK \
123                 (((1 << DOD_HEAD_ID_BITS) - 1) << DOD_HEAD_ID_SHIFT)
124 #define DOD_DEVID_SCHEME_STD    (1 << 31)
125
126 /* _BCL related constants */
127 #define BCL_FULLPOWER           0
128 #define BCL_ECONOMY             1
129
130 /* _DCS (Device Currrent Status) value bits and masks. */
131 #define DCS_EXISTS              (1 << 0)
132 #define DCS_ACTIVE              (1 << 1)
133 #define DCS_READY               (1 << 2)
134 #define DCS_FUNCTIONAL          (1 << 3)
135 #define DCS_ATTACHED            (1 << 4)
136
137 /* _DSS (Device Set Status) argument bits and masks. */
138 #define DSS_INACTIVE            0
139 #define DSS_ACTIVE              (1 << 0)
140 #define DSS_SETNEXT             (1 << 30)
141 #define DSS_COMMIT              (1 << 31)
142
143 static device_method_t acpi_video_methods[] = {
144         DEVMETHOD(device_probe, acpi_video_probe),
145         DEVMETHOD(device_attach, acpi_video_attach),
146         DEVMETHOD(device_detach, acpi_video_detach),
147         DEVMETHOD(device_shutdown, acpi_video_shutdown),
148         { 0, 0 }
149 };
150
151 static driver_t acpi_video_driver = {
152         "acpi_video",
153         acpi_video_methods,
154         sizeof(struct acpi_video_softc),
155 };
156
157 static devclass_t acpi_video_devclass;
158
159 DRIVER_MODULE(acpi_video, acpi, acpi_video_driver, acpi_video_devclass,
160               acpi_video_modevent, NULL);
161 MODULE_DEPEND(acpi_video, acpi, 1, 1, 1);
162
163 static struct sysctl_ctx_list   acpi_video_sysctl_ctx;
164 static struct sysctl_oid        *acpi_video_sysctl_tree;
165 static struct acpi_video_output_queue crt_units, tv_units,
166     ext_units, lcd_units, other_units;
167 static struct lock avlock;
168
169 MALLOC_DEFINE(M_ACPIVIDEO, "acpivideo", "ACPI video extension");
170
171 static int
172 acpi_video_modevent(struct module *mod __unused, int evt, void *cookie __unused)
173 {
174         int err;
175
176         err = 0;
177         switch (evt) {
178         case MOD_LOAD:
179                 sysctl_ctx_init(&acpi_video_sysctl_ctx);
180                 STAILQ_INIT(&crt_units);
181                 STAILQ_INIT(&tv_units);
182                 STAILQ_INIT(&ext_units);
183                 STAILQ_INIT(&lcd_units);
184                 STAILQ_INIT(&other_units);
185                 break;
186         case MOD_UNLOAD:
187                 sysctl_ctx_free(&acpi_video_sysctl_ctx);
188                 acpi_video_sysctl_tree = NULL;
189                 break;
190         default:
191                 err = EINVAL;
192         }
193
194         return (err);
195 }
196
197 static int
198 acpi_video_probe(device_t dev)
199 {
200         ACPI_HANDLE devh, h;
201         ACPI_OBJECT_TYPE t_dos;
202
203         devh = acpi_get_handle(dev);
204         if (acpi_disabled("video") ||
205             ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
206             ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h)) ||
207             ACPI_FAILURE(AcpiGetType(h, &t_dos)) ||
208             t_dos != ACPI_TYPE_METHOD)
209                 return (ENXIO);
210
211         device_set_desc(dev, "ACPI video extension");
212         return (0);
213 }
214
215 static int
216 acpi_video_attach(device_t dev)
217 {
218         struct acpi_softc *acpi_sc;
219         struct acpi_video_softc *sc;
220
221         sc = device_get_softc(dev);
222
223         acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
224         if (acpi_sc == NULL)
225                 return (ENXIO);
226         if (acpi_video_sysctl_tree == NULL) {
227                 acpi_video_sysctl_tree = SYSCTL_ADD_NODE(&acpi_video_sysctl_ctx,
228                                     SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
229                                     OID_AUTO, "video", CTLFLAG_RD, 0,
230                                     "video extension control");
231         }
232
233         sc->device = dev;
234         sc->handle = acpi_get_handle(dev);
235         STAILQ_INIT(&sc->vid_outputs);
236
237         AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
238                                  acpi_video_notify_handler, sc);
239         sc->vid_pwr_evh = EVENTHANDLER_REGISTER(power_profile_change,
240                                  acpi_video_power_profile, sc, 0);
241
242         lockinit(&avlock, "acpi_video", 0, 0);
243         lockmgr(&avlock, LK_EXCLUSIVE);
244         acpi_video_bind_outputs(sc);
245         lockmgr(&avlock, LK_RELEASE);
246
247         /*
248          * Notify the BIOS that we want to switch both active outputs and
249          * brightness levels.
250          */
251         vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_OSPM |
252             DOS_BRIGHTNESS_BY_BIOS);
253
254         acpi_video_power_profile(sc);
255
256         return (0);
257 }
258
259 static int
260 acpi_video_detach(device_t dev)
261 {
262         struct acpi_video_softc *sc;
263         struct acpi_video_output *vo, *vn;
264
265         sc = device_get_softc(dev);
266
267         vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
268         EVENTHANDLER_DEREGISTER(power_profile_change, sc->vid_pwr_evh);
269         AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
270                                 acpi_video_notify_handler);
271
272         lockmgr(&avlock, LK_EXCLUSIVE);
273         for (vo = STAILQ_FIRST(&sc->vid_outputs); vo != NULL; vo = vn) {
274                 vn = STAILQ_NEXT(vo, vo_next);
275                 acpi_video_vo_destroy(vo);
276         }
277         lockmgr(&avlock, LK_RELEASE);
278
279         return (0);
280 }
281
282 static int
283 acpi_video_shutdown(device_t dev)
284 {
285         struct acpi_video_softc *sc;
286
287         sc = device_get_softc(dev);
288         vid_set_switch_policy(sc->handle, DOS_SWITCH_BY_BIOS);
289
290         return (0);
291 }
292
293 static void
294 acpi_video_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
295 {
296         struct acpi_video_softc *sc;
297         struct acpi_video_output *vo, *vo_tmp;
298         ACPI_HANDLE lasthand;
299         UINT32 dcs, dss, dss_p;
300
301         sc = (struct acpi_video_softc *)context;
302
303         switch (notify) {
304         case VID_NOTIFY_SWITCHED:
305                 dss_p = 0;
306                 lasthand = NULL;
307                 lockmgr(&avlock, LK_EXCLUSIVE);
308                 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
309                         dss = vo_get_graphics_state(vo->handle);
310                         dcs = vo_get_device_status(vo->handle);
311                         if (!(dcs & DCS_READY))
312                                 dss = DSS_INACTIVE;
313                         if (((dcs & DCS_ACTIVE) && dss == DSS_INACTIVE) ||
314                             (!(dcs & DCS_ACTIVE) && dss == DSS_ACTIVE)) {
315                                 if (lasthand != NULL)
316                                         vo_set_device_state(lasthand, dss_p);
317                                 dss_p = dss;
318                                 lasthand = vo->handle;
319                         }
320                 }
321                 if (lasthand != NULL)
322                         vo_set_device_state(lasthand, dss_p|DSS_COMMIT);
323                 lockmgr(&avlock, LK_RELEASE);
324                 break;
325         case VID_NOTIFY_REPROBE:
326                 lockmgr(&avlock, LK_EXCLUSIVE);
327                 STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next)
328                         vo->handle = NULL;
329                 acpi_video_bind_outputs(sc);
330                 STAILQ_FOREACH_MUTABLE(vo, &sc->vid_outputs, vo_next, vo_tmp) {
331                         if (vo->handle == NULL) {
332                                 STAILQ_REMOVE(&sc->vid_outputs, vo,
333                                     acpi_video_output, vo_next);
334                                 acpi_video_vo_destroy(vo);
335                         }
336                 }
337                 lockmgr(&avlock, LK_RELEASE);
338                 break;
339         default:
340                 device_printf(sc->device, "unknown notify event 0x%x\n",
341                     notify);
342         }
343 }
344
345 static void
346 acpi_video_power_profile(void *context)
347 {
348         int state;
349         struct acpi_video_softc *sc;
350         struct acpi_video_output *vo;
351
352         sc = context;
353         state = power_profile_get_state();
354         if (state != POWER_PROFILE_PERFORMANCE &&
355             state != POWER_PROFILE_ECONOMY)
356                 return;
357
358         lockmgr(&avlock, LK_EXCLUSIVE);
359         STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
360                 if (vo->vo_levels != NULL && vo->vo_brightness == -1)
361                         vo_set_brightness(vo->handle,
362                             state == POWER_PROFILE_ECONOMY ?
363                             vo->vo_economy : vo->vo_fullpower);
364         }
365         lockmgr(&avlock, LK_RELEASE);
366 }
367
368 static void
369 acpi_video_bind_outputs_subr(ACPI_HANDLE handle, UINT32 adr, void *context)
370 {
371         struct acpi_video_softc *sc;
372         struct acpi_video_output *vo;
373
374         sc = context;
375
376         STAILQ_FOREACH(vo, &sc->vid_outputs, vo_next) {
377                 if (vo->adr == adr) {
378                         acpi_video_vo_bind(vo, handle);
379                         return;
380                 }
381         }
382         vo = acpi_video_vo_init(adr);
383         if (vo != NULL) {
384                 acpi_video_vo_bind(vo, handle);
385                 STAILQ_INSERT_TAIL(&sc->vid_outputs, vo, vo_next);
386         }
387 }
388
389 static void
390 acpi_video_bind_outputs(struct acpi_video_softc *sc)
391 {
392
393         vid_enum_outputs(sc->handle, acpi_video_bind_outputs_subr, sc);
394 }
395
396 static struct acpi_video_output *
397 acpi_video_vo_init(UINT32 adr)
398 {
399         struct acpi_video_output *vn, *vo, *vp;
400         int n, x;
401         int display_index;
402         int display_port;
403         char name[8], env[32];
404         const char *type, *desc;
405         struct acpi_video_output_queue *voqh;
406
407         display_index = adr & DOD_DEVID_MASK_DISPIDX;
408         display_port = (adr & DOD_DEVID_MASK_DISPPORT) >> 4;
409
410         switch (adr & DOD_DEVID_MASK) {
411         case DOD_DEVID_MONITOR:
412                 if ((adr & DOD_DEVID_MASK_FULL) == DOD_DEVID_LCD) {
413                         /* DOD_DEVID_LCD is a common, backward compatible ID */
414                         desc = "Internal/Integrated Digital Flat Panel";
415                         type = "lcd";
416                         voqh = &lcd_units;
417                 } else {
418                         desc = "VGA CRT or VESA Compatible Analog Monitor";
419                         type = "crt";
420                         voqh = &crt_units;
421                 }
422                 break;
423         case DOD_DEVID_TV:
424                 desc = "TV/HDTV or Analog-Video Monitor";
425                 type = "tv";
426                 voqh = &tv_units;
427                 break;
428         case DOD_DEVID_EXT:
429                 desc = "External Digital Monitor";
430                 type = "ext";
431                 voqh = &ext_units;
432                 break;
433         case DOD_DEVID_INTDFP:
434                 desc = "Internal/Integrated Digital Flat Panel";
435                 type = "lcd";
436                 voqh = &lcd_units;
437                 break;
438         default:
439                 desc = "unknown output";
440                 type = "out";
441                 voqh = &other_units;
442         }
443
444         n = 0;
445         vn = vp = NULL;
446         STAILQ_FOREACH(vn, voqh, vo_unit.next) {
447                 if (vn->vo_unit.num != n)
448                         break;
449                 vp = vn;
450                 n++;
451         }
452
453         ksnprintf(name, sizeof(name), "%s%d", type, n);
454
455         vo = kmalloc(sizeof(*vo), M_ACPIVIDEO, M_NOWAIT);
456         if (vo != NULL) {
457                 vo->handle = NULL;
458                 vo->adr = adr;
459                 vo->vo_unit.num = n;
460                 vo->vo_brightness = -1;
461                 vo->vo_fullpower = -1;  /* TODO: override with tunables */
462                 vo->vo_economy = -1;
463                 vo->vo_numlevels = 0;
464                 vo->vo_levels = NULL;
465                 ksnprintf(env, sizeof(env), "hw.acpi.video.%s.fullpower", name);
466                 if (kgetenv_int(env, &x))
467                         vo->vo_fullpower = x;
468                 ksnprintf(env, sizeof(env), "hw.acpi.video.%s.economy", name);
469                 if (kgetenv_int(env, &x))
470                         vo->vo_economy = x;
471
472                 sysctl_ctx_init(&vo->vo_sysctl_ctx);
473                 if (vp != NULL)
474                         STAILQ_INSERT_AFTER(voqh, vp, vo, vo_unit.next);
475                 else
476                         STAILQ_INSERT_TAIL(voqh, vo, vo_unit.next);
477                 if (acpi_video_sysctl_tree != NULL)
478                         vo->vo_sysctl_tree =
479                             SYSCTL_ADD_NODE(&vo->vo_sysctl_ctx,
480                                 SYSCTL_CHILDREN(acpi_video_sysctl_tree),
481                                 OID_AUTO, name, CTLFLAG_RD, 0, desc);
482                 if (vo->vo_sysctl_tree != NULL) {
483                         SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
484                             SYSCTL_CHILDREN(vo->vo_sysctl_tree),
485                             OID_AUTO, "active",
486                             CTLTYPE_INT|CTLFLAG_RW, vo, 0,
487                             acpi_video_vo_active_sysctl, "I",
488                             "current activity of this device");
489                         SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
490                             SYSCTL_CHILDREN(vo->vo_sysctl_tree),
491                             OID_AUTO, "brightness",
492                             CTLTYPE_INT|CTLFLAG_RW, vo, 0,
493                             acpi_video_vo_bright_sysctl, "I",
494                             "current brightness level");
495                         SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
496                             SYSCTL_CHILDREN(vo->vo_sysctl_tree),
497                             OID_AUTO, "fullpower",
498                             CTLTYPE_INT|CTLFLAG_RW, vo,
499                             POWER_PROFILE_PERFORMANCE,
500                             acpi_video_vo_presets_sysctl, "I",
501                             "preset level for full power mode");
502                         SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
503                             SYSCTL_CHILDREN(vo->vo_sysctl_tree),
504                             OID_AUTO, "economy",
505                             CTLTYPE_INT|CTLFLAG_RW, vo,
506                             POWER_PROFILE_ECONOMY,
507                             acpi_video_vo_presets_sysctl, "I",
508                             "preset level for economy mode");
509                         SYSCTL_ADD_PROC(&vo->vo_sysctl_ctx,
510                             SYSCTL_CHILDREN(vo->vo_sysctl_tree),
511                             OID_AUTO, "levels",
512                             CTLTYPE_OPAQUE|CTLFLAG_RD, vo, 0,
513                             acpi_video_vo_levels_sysctl, "I",
514                             "supported brightness levels");
515                 } else
516                         kprintf("%s: sysctl node creation failed\n", type);
517         } else
518                 kprintf("%s: softc allocation failed\n", type);
519
520         if (bootverbose) {
521                 kprintf("found %s(%x)", desc, adr & DOD_DEVID_MASK_FULL);
522                 kprintf(", idx#%x", adr & DOD_DEVID_MASK_DISPIDX);
523                 kprintf(", port#%x", (adr & DOD_DEVID_MASK_DISPPORT) >> 4);
524                 if (adr & DOD_BIOS)
525                         kprintf(", detectable by BIOS");
526                 if (adr & DOD_NONVGA)
527                         kprintf(" (Non-VGA output device whose power "
528                             "is related to the VGA device)");
529                 kprintf(", head #%d\n",
530                         (adr & DOD_HEAD_ID_MASK) >> DOD_HEAD_ID_SHIFT);
531         }
532         return (vo);
533 }
534
535 static void
536 acpi_video_vo_bind(struct acpi_video_output *vo, ACPI_HANDLE handle)
537 {
538         if (vo->vo_levels != NULL)
539                 AcpiOsFree(vo->vo_levels);
540         vo->handle = handle;
541         vo->vo_numlevels = vo_get_brightness_levels(handle, &vo->vo_levels);
542         if (vo->vo_numlevels >= 2) {
543                 if (vo->vo_fullpower == -1
544                     || acpi_video_vo_check_level(vo, vo->vo_fullpower) != 0)
545                         /* XXX - can't deal with rebinding... */
546                         vo->vo_fullpower = vo->vo_levels[BCL_FULLPOWER];
547                 if (vo->vo_economy == -1
548                     || acpi_video_vo_check_level(vo, vo->vo_economy) != 0)
549                         /* XXX - see above. */
550                         vo->vo_economy = vo->vo_levels[BCL_ECONOMY];
551         }
552 }
553
554 static void
555 acpi_video_vo_destroy(struct acpi_video_output *vo)
556 {
557         struct acpi_video_output_queue *voqh;
558
559         if (vo->vo_sysctl_tree != NULL) {
560                 vo->vo_sysctl_tree = NULL;
561                 sysctl_ctx_free(&vo->vo_sysctl_ctx);
562         }
563         if (vo->vo_levels != NULL)
564                 AcpiOsFree(vo->vo_levels);
565
566         switch (vo->adr & DOD_DEVID_MASK) {
567         case DOD_DEVID_MONITOR:
568                 voqh = &crt_units;
569                 break;
570         case DOD_DEVID_TV:
571                 voqh = &tv_units;
572                 break;
573         case DOD_DEVID_EXT:
574                 voqh = &ext_units;
575                 break;
576         case DOD_DEVID_INTDFP:
577                 voqh = &lcd_units;
578                 break;
579         default:
580                 voqh = &other_units;
581         }
582         STAILQ_REMOVE(voqh, vo, acpi_video_output, vo_unit.next);
583         kfree(vo, M_ACPIVIDEO);
584 }
585
586 static int
587 acpi_video_vo_check_level(struct acpi_video_output *vo, int level)
588 {
589         int i;
590
591         if (vo->vo_levels == NULL)
592                 return (ENODEV);
593         for (i = 0; i < vo->vo_numlevels; i++)
594                 if (vo->vo_levels[i] == level)
595                         return (0);
596         return (EINVAL);
597 }
598
599 /* ARGSUSED */
600 static int
601 acpi_video_vo_active_sysctl(SYSCTL_HANDLER_ARGS)
602 {
603         struct acpi_video_output *vo;
604         int state, err;
605
606         vo = (struct acpi_video_output *)arg1;
607         if (vo->handle == NULL)
608                 return (ENXIO);
609         lockmgr(&avlock, LK_EXCLUSIVE);
610         state = (vo_get_device_status(vo->handle) & DCS_ACTIVE) ? 1 : 0;
611         err = sysctl_handle_int(oidp, &state, 0, req);
612         if (err != 0 || req->newptr == NULL)
613                 goto out;
614         vo_set_device_state(vo->handle,
615             DSS_COMMIT | (state ? DSS_ACTIVE : DSS_INACTIVE));
616 out:
617         lockmgr(&avlock, LK_RELEASE);
618         return (err);
619 }
620
621 /* ARGSUSED */
622 static int
623 acpi_video_vo_bright_sysctl(SYSCTL_HANDLER_ARGS)
624 {
625         struct acpi_video_output *vo;
626         int level, preset, err;
627
628         vo = (struct acpi_video_output *)arg1;
629         lockmgr(&avlock, LK_EXCLUSIVE);
630         if (vo->handle == NULL) {
631                 err = ENXIO;
632                 goto out;
633         }
634         if (vo->vo_levels == NULL) {
635                 err = ENODEV;
636                 goto out;
637         }
638
639         preset = (power_profile_get_state() == POWER_PROFILE_ECONOMY) ?
640                   vo->vo_economy : vo->vo_fullpower;
641         level = vo->vo_brightness;
642         if (level == -1)
643                 level = preset;
644
645         err = sysctl_handle_int(oidp, &level, 0, req);
646         if (err != 0 || req->newptr == NULL)
647                 goto out;
648         if (level < -1 || level > 100) {
649                 err = EINVAL;
650                 goto out;
651         }
652
653         if (level != -1 && (err = acpi_video_vo_check_level(vo, level)))
654                 goto out;
655         vo->vo_brightness = level;
656         vo_set_brightness(vo->handle, (level == -1) ? preset : level);
657
658 out:
659         lockmgr(&avlock, LK_RELEASE);
660         return (err);
661 }
662
663 static int
664 acpi_video_vo_presets_sysctl(SYSCTL_HANDLER_ARGS)
665 {
666         struct acpi_video_output *vo;
667         int i, level, *preset, err;
668
669         err = 0;
670         vo = (struct acpi_video_output *)arg1;
671         lockmgr(&avlock, LK_EXCLUSIVE);
672         if (vo->handle == NULL) {
673                 err = ENXIO;
674                 goto out;
675         }
676         if (vo->vo_levels == NULL) {
677                 err = ENODEV;
678                 goto out;
679         }
680         preset = (arg2 == POWER_PROFILE_ECONOMY) ?
681                   &vo->vo_economy : &vo->vo_fullpower;
682         level = *preset;
683         err = sysctl_handle_int(oidp, &level, 0, req);
684         if (err != 0 || req->newptr == NULL)
685                 goto out;
686         if (level < -1 || level > 100) {
687                 err = EINVAL;
688                 goto out;
689         }
690         if (level == -1) {
691                 i = (arg2 == POWER_PROFILE_ECONOMY) ?
692                     BCL_ECONOMY : BCL_FULLPOWER;
693                 level = vo->vo_levels[i];
694         } else if ((err = acpi_video_vo_check_level(vo, level)) != 0)
695                 goto out;
696
697         if (vo->vo_brightness == -1 && (power_profile_get_state() == arg2))
698                 vo_set_brightness(vo->handle, level);
699         *preset = level;
700
701 out:
702         lockmgr(&avlock, LK_RELEASE);
703         return (err);
704 }
705
706 /* ARGSUSED */
707 static int
708 acpi_video_vo_levels_sysctl(SYSCTL_HANDLER_ARGS)
709 {
710         struct acpi_video_output *vo;
711         int err;
712
713         vo = (struct acpi_video_output *)arg1;
714         lockmgr(&avlock, LK_EXCLUSIVE);
715         if (vo->vo_levels == NULL) {
716                 err = ENODEV;
717                 goto out;
718         }
719         if (req->newptr != NULL) {
720                 err = EPERM;
721                 goto out;
722         }
723         err = sysctl_handle_opaque(oidp, vo->vo_levels,
724             vo->vo_numlevels * sizeof(*vo->vo_levels), req);
725
726 out:
727         lockmgr(&avlock, LK_RELEASE);
728         return (err);
729 }
730
731 static void
732 vid_set_switch_policy(ACPI_HANDLE handle, UINT32 policy)
733 {
734         ACPI_STATUS status;
735
736         status = acpi_SetInteger(handle, "_DOS", policy);
737         if (ACPI_FAILURE(status))
738                 kprintf("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         int count;
747 };
748
749 static ACPI_STATUS
750 vid_enum_outputs_subr(ACPI_HANDLE handle, UINT32 level __unused,
751                       void *context, void **retp __unused)
752 {
753         ACPI_STATUS status;
754         UINT32 adr, val;
755         struct enum_callback_arg *argset;
756         size_t i;
757
758         argset = context;
759         status = acpi_GetInteger(handle, "_ADR", &adr);
760         if (ACPI_FAILURE(status))
761                 return (AE_OK);
762
763         for (i = 0; i < argset->dod_pkg->Package.Count; i++) {
764                 if (acpi_PkgInt32(argset->dod_pkg, i, &val) == 0 &&
765                     (val & DOD_DEVID_MASK_FULL) == adr) {
766                         argset->callback(handle, val, argset->context);
767                         argset->count++;
768                 }
769         }
770
771         return (AE_OK);
772 }
773
774 static int
775 vid_enum_outputs(ACPI_HANDLE handle,
776                  void (*callback)(ACPI_HANDLE, UINT32, void *), void *context)
777 {
778         ACPI_STATUS status;
779         ACPI_BUFFER dod_buf;
780         ACPI_OBJECT *res;
781         struct enum_callback_arg argset;
782
783         dod_buf.Length = ACPI_ALLOCATE_BUFFER;
784         dod_buf.Pointer = NULL;
785         status = AcpiEvaluateObject(handle, "_DOD", NULL, &dod_buf);
786         if (ACPI_FAILURE(status)) {
787                 if (status != AE_NOT_FOUND)
788                         kprintf("can't evaluate %s._DOD - %s\n",
789                                acpi_name(handle), AcpiFormatException(status));
790                 argset.count = -1;
791                 goto out;
792         }
793         res = (ACPI_OBJECT *)dod_buf.Pointer;
794         if (!ACPI_PKG_VALID(res, 1)) {
795                 kprintf("evaluation of %s._DOD makes no sense\n",
796                        acpi_name(handle));
797                 argset.count = -1;
798                 goto out;
799         }
800         if (callback == NULL) {
801                 argset.count = res->Package.Count;
802                 goto out;
803         }
804         argset.callback = callback;
805         argset.context  = context;
806         argset.dod_pkg  = res;
807         argset.count    = 0;
808         status = AcpiWalkNamespace(ACPI_TYPE_DEVICE, handle, 1,
809             vid_enum_outputs_subr, NULL, &argset, NULL);
810         if (ACPI_FAILURE(status))
811                 kprintf("failed walking down %s - %s\n",
812                        acpi_name(handle), AcpiFormatException(status));
813 out:
814         if (dod_buf.Pointer != NULL)
815                 AcpiOsFree(dod_buf.Pointer);
816         return (argset.count);
817 }
818
819 static int
820 vo_get_brightness_levels(ACPI_HANDLE handle, int **levelp)
821 {
822         ACPI_STATUS status;
823         ACPI_BUFFER bcl_buf;
824         ACPI_OBJECT *res;
825         int num, i, n, *levels;
826
827         num = 0;
828         bcl_buf.Length = ACPI_ALLOCATE_BUFFER;
829         bcl_buf.Pointer = NULL;
830         status = AcpiEvaluateObject(handle, "_BCL", NULL, &bcl_buf);
831         if (ACPI_FAILURE(status)) {
832                 if (status != AE_NOT_FOUND)
833                         kprintf("can't evaluate %s._BCL - %s\n",
834                                acpi_name(handle), AcpiFormatException(status));
835                 num = -1;
836                 goto out;
837         }
838         res = (ACPI_OBJECT *)bcl_buf.Pointer;
839         if (!ACPI_PKG_VALID(res, 2)) {
840                 kprintf("evaluation of %s._BCL makes no sense\n",
841                        acpi_name(handle));
842                 num = -1;
843                 goto out;
844         }
845         num = res->Package.Count;
846         if (levelp == NULL)
847                 goto out;
848         levels = AcpiOsAllocate(num * sizeof(*levels));
849         if (levels == NULL) {
850                 num = -1;
851                 goto out;
852         }
853         for (i = 0, n = 0; i < num; i++)
854                 if (acpi_PkgInt32(res, i, &levels[n]) == 0)
855                         n++;
856         if (n < 2) {
857                 num = -1;
858                 AcpiOsFree(levels);
859         } else {
860                 num = n;
861                 *levelp = levels;
862         }
863 out:
864         if (bcl_buf.Pointer != NULL)
865                 AcpiOsFree(bcl_buf.Pointer);
866
867         return (num);
868 }
869
870 static void
871 vo_set_brightness(ACPI_HANDLE handle, int level)
872 {
873         ACPI_STATUS status;
874
875         status = acpi_SetInteger(handle, "_BCM", level);
876         if (ACPI_FAILURE(status))
877                 kprintf("can't evaluate %s._BCM - %s\n",
878                        acpi_name(handle), AcpiFormatException(status));
879 }
880
881 static UINT32
882 vo_get_device_status(ACPI_HANDLE handle)
883 {
884         UINT32 dcs;
885         ACPI_STATUS status;
886
887         dcs = 0;
888         status = acpi_GetInteger(handle, "_DCS", &dcs);
889         if (ACPI_FAILURE(status))
890                 kprintf("can't evaluate %s._DCS - %s\n",
891                        acpi_name(handle), AcpiFormatException(status));
892
893         return (dcs);
894 }
895
896 static UINT32
897 vo_get_graphics_state(ACPI_HANDLE handle)
898 {
899         UINT32 dgs;
900         ACPI_STATUS status;
901
902         dgs = 0;
903         status = acpi_GetInteger(handle, "_DGS", &dgs);
904         if (ACPI_FAILURE(status))
905                 kprintf("can't evaluate %s._DGS - %s\n",
906                        acpi_name(handle), AcpiFormatException(status));
907
908         return (dgs);
909 }
910
911 static void
912 vo_set_device_state(ACPI_HANDLE handle, UINT32 state)
913 {
914         ACPI_STATUS status;
915
916         status = acpi_SetInteger(handle, "_DSS", state);
917         if (ACPI_FAILURE(status))
918                 kprintf("can't evaluate %s._DSS - %s\n",
919                        acpi_name(handle), AcpiFormatException(status));
920 }