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