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