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