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

Cache object: 1c077f16503aebbc5699e33d940edfa3


[ 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.