The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi_video/acpi_video.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b6e8ff3ffd016a759ce722b26ebac14d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.