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_dock.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) 2005-2006 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
    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: releng/8.1/sys/dev/acpica/acpi_dock.c 203544 2010-02-06 12:03:25Z avg $
   27  */
   28 
   29 #include "opt_acpi.h"
   30 #include <sys/param.h>
   31 #include <sys/bus.h>
   32 #include <sys/kernel.h>
   33 #include <sys/module.h>
   34 
   35 #include <contrib/dev/acpica/include/acpi.h>
   36 #include <contrib/dev/acpica/include/accommon.h>
   37 
   38 #include <dev/acpica/acpivar.h>
   39 #include <dev/acpica/acpiio.h>
   40 
   41 /* Hooks for the ACPI CA debugging infrastructure */
   42 #define _COMPONENT      ACPI_DOCK
   43 ACPI_MODULE_NAME("DOCK")
   44 
   45 /* For Docking status */
   46 #define ACPI_DOCK_STATUS_UNKNOWN        -1
   47 #define ACPI_DOCK_STATUS_UNDOCKED       0
   48 #define ACPI_DOCK_STATUS_DOCKED         1
   49 
   50 #define ACPI_DOCK_UNLOCK                0 /* Allow device to be ejected */
   51 #define ACPI_DOCK_LOCK                  1 /* Prevent dev from being removed */
   52 
   53 #define ACPI_DOCK_ISOLATE               0 /* Isolate from dock connector */
   54 #define ACPI_DOCK_CONNECT               1 /* Connect to dock */
   55 
   56 struct acpi_dock_softc {
   57         int             _sta;
   58         int             _bdn;
   59         int             _uid;
   60         int             status;
   61         struct sysctl_ctx_list  *sysctl_ctx;
   62         struct sysctl_oid       *sysctl_tree;
   63 };
   64 
   65 ACPI_SERIAL_DECL(dock, "ACPI Docking Station");
   66 
   67 /*
   68  * Utility functions
   69  */
   70 
   71 static void
   72 acpi_dock_get_info(device_t dev)
   73 {
   74         struct acpi_dock_softc *sc;
   75         ACPI_HANDLE     h;
   76 
   77         sc = device_get_softc(dev);
   78         h = acpi_get_handle(dev);
   79 
   80         if (ACPI_FAILURE(acpi_GetInteger(h, "_STA", &sc->_sta)))
   81                 sc->_sta = ACPI_DOCK_STATUS_UNKNOWN;
   82         if (ACPI_FAILURE(acpi_GetInteger(h, "_BDN", &sc->_bdn)))
   83                 sc->_bdn = ACPI_DOCK_STATUS_UNKNOWN;
   84         if (ACPI_FAILURE(acpi_GetInteger(h, "_UID", &sc->_uid)))
   85                 sc->_uid = ACPI_DOCK_STATUS_UNKNOWN;
   86         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
   87                     "_STA: %04x, _BDN: %04x, _UID: %04x\n", sc->_sta,
   88                     sc->_bdn, sc->_uid);
   89 }
   90 
   91 static int
   92 acpi_dock_execute_dck(device_t dev, int dock)
   93 {
   94         ACPI_HANDLE     h;
   95         ACPI_OBJECT     argobj;
   96         ACPI_OBJECT_LIST args;
   97         ACPI_BUFFER     buf;
   98         ACPI_OBJECT     retobj;
   99         ACPI_STATUS     status;
  100 
  101         h = acpi_get_handle(dev);
  102 
  103         argobj.Type = ACPI_TYPE_INTEGER;
  104         argobj.Integer.Value = dock;
  105         args.Count = 1;
  106         args.Pointer = &argobj;
  107         buf.Pointer = &retobj;
  108         buf.Length = sizeof(retobj);
  109         status = AcpiEvaluateObject(h, "_DCK", &args, &buf);
  110 
  111         /*
  112          * When _DCK is called with 0, OSPM will ignore the return value.
  113          */
  114         if (dock == ACPI_DOCK_ISOLATE)
  115                 return (0);
  116 
  117         /* If _DCK returned 1, the request succeeded. */
  118         if (ACPI_SUCCESS(status) && retobj.Type == ACPI_TYPE_INTEGER &&
  119             retobj.Integer.Value == 1)
  120                 return (0);
  121 
  122         return (-1);
  123 }
  124 
  125 /* Lock devices while docked to prevent surprise removal. */
  126 static void
  127 acpi_dock_execute_lck(device_t dev, int lock)
  128 {
  129         ACPI_HANDLE     h;
  130 
  131         h = acpi_get_handle(dev);
  132         acpi_SetInteger(h, "_LCK", lock);
  133 }
  134 
  135 /* Eject a device (i.e., motorized). */
  136 static int
  137 acpi_dock_execute_ejx(device_t dev, int eject, int state)
  138 {
  139         ACPI_HANDLE     h;
  140         ACPI_STATUS     status;
  141         char            ejx[5];
  142 
  143         h = acpi_get_handle(dev);
  144         snprintf(ejx, sizeof(ejx), "_EJ%d", state);
  145         status = acpi_SetInteger(h, ejx, eject);
  146         if (ACPI_SUCCESS(status))
  147                 return (0);
  148 
  149         return (-1);
  150 }
  151 
  152 /* Find dependent devices.  When their parent is removed, so are they. */
  153 static int
  154 acpi_dock_is_ejd_device(ACPI_HANDLE dock_handle, ACPI_HANDLE handle)
  155 {
  156         int             ret;
  157         ACPI_STATUS     ret_status;
  158         ACPI_BUFFER     ejd_buffer;
  159         ACPI_OBJECT     *obj;
  160 
  161         ret = 0;
  162 
  163         ejd_buffer.Pointer = NULL;
  164         ejd_buffer.Length = ACPI_ALLOCATE_BUFFER;
  165         ret_status = AcpiEvaluateObject(handle, "_EJD", NULL, &ejd_buffer);
  166         if (ACPI_FAILURE(ret_status))
  167                 goto out;
  168 
  169         obj = (ACPI_OBJECT *)ejd_buffer.Pointer;
  170         if (dock_handle == acpi_GetReference(NULL, obj))
  171                 ret = 1;
  172 
  173 out:
  174         if (ejd_buffer.Pointer != NULL)
  175                 AcpiOsFree(ejd_buffer.Pointer);
  176 
  177         return (ret);
  178 }
  179 
  180 /*
  181  * Docking functions
  182  */
  183 
  184 static void
  185 acpi_dock_attach_later(void *context)
  186 {
  187         device_t        dev;
  188 
  189         dev = (device_t)context;
  190 
  191         if (!device_is_enabled(dev))
  192                 device_enable(dev);
  193 
  194         mtx_lock(&Giant);
  195         device_probe_and_attach(dev);
  196         mtx_unlock(&Giant);
  197 }
  198 
  199 static ACPI_STATUS
  200 acpi_dock_insert_child(ACPI_HANDLE handle, UINT32 level, void *context,
  201     void **status)
  202 {
  203         device_t        dock_dev, dev;
  204         ACPI_HANDLE     dock_handle;
  205 
  206         dock_dev = (device_t)context;
  207         dock_handle = acpi_get_handle(dock_dev);
  208 
  209         if (!acpi_dock_is_ejd_device(dock_handle, handle))
  210                 goto out;
  211 
  212         ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
  213                     "inserting device for %s\n", acpi_name(handle));
  214 
  215 #if 0
  216         /*
  217          * If the system boot up w/o Docking, the devices under the dock
  218          * still un-initialized, also control methods such as _INI, _STA
  219          * are not executed.
  220          * Normal devices are initialized at booting by calling
  221          * AcpiInitializeObjects(), however the devices under the dock
  222          * need to be initialized here on the scheme of ACPICA.
  223          */
  224         ACPI_INIT_WALK_INFO     Info;
  225 
  226         AcpiNsWalkNamespace(ACPI_TYPE_ANY, handle,
  227             100, TRUE, AcpiNsInitOneDevice, NULL, &Info, NULL);
  228 #endif
  229 
  230         dev = acpi_get_device(handle);
  231         if (dev == NULL) {
  232                 device_printf(dock_dev, "error: %s has no associated device\n",
  233                     acpi_name(handle));
  234                 goto out;
  235         }
  236 
  237         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_dock_attach_later, dev);
  238 
  239 out:
  240         return (AE_OK);
  241 }
  242 
  243 static void
  244 acpi_dock_insert_children(device_t dev)
  245 {
  246         ACPI_STATUS     status;
  247         ACPI_HANDLE     sb_handle;
  248 
  249         status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
  250         if (ACPI_SUCCESS(status)) {
  251                 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
  252                     100, acpi_dock_insert_child, NULL, dev, NULL);
  253         }
  254 }
  255 
  256 static void
  257 acpi_dock_insert(device_t dev)
  258 {
  259         struct acpi_dock_softc *sc;
  260         ACPI_HANDLE     h;
  261 
  262         ACPI_SERIAL_ASSERT(dock);
  263 
  264         sc = device_get_softc(dev);
  265         h = acpi_get_handle(dev);
  266 
  267         if (sc->status == ACPI_DOCK_STATUS_UNDOCKED ||
  268             sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
  269                 acpi_dock_execute_lck(dev, ACPI_DOCK_LOCK);
  270                 if (acpi_dock_execute_dck(dev, ACPI_DOCK_CONNECT) != 0) {
  271                         device_printf(dev, "_DCK failed\n");
  272                         return;
  273                 }
  274 
  275                 if (!cold)
  276                         acpi_dock_insert_children(dev);
  277                 sc->status = ACPI_DOCK_STATUS_DOCKED;
  278         }
  279 }
  280 
  281 /*
  282  * Undock
  283  */
  284 
  285 static ACPI_STATUS
  286 acpi_dock_eject_child(ACPI_HANDLE handle, UINT32 level, void *context,
  287     void **status)
  288 {
  289         device_t        dock_dev, dev;
  290         ACPI_HANDLE     dock_handle;
  291 
  292         dock_dev = *(device_t *)context;
  293         dock_handle = acpi_get_handle(dock_dev);
  294 
  295         if (!acpi_dock_is_ejd_device(dock_handle, handle))
  296                 goto out;
  297 
  298         ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
  299             "ejecting device for %s\n", acpi_name(handle));
  300 
  301         dev = acpi_get_device(handle);
  302         if (dev != NULL && device_is_attached(dev)) {
  303                 mtx_lock(&Giant);
  304                 device_detach(dev);
  305                 mtx_unlock(&Giant);
  306         }
  307 
  308         acpi_SetInteger(handle, "_EJ0", 0);
  309 out:
  310         return (AE_OK);
  311 }
  312 
  313 static void
  314 acpi_dock_eject_children(device_t dev)
  315 {
  316         ACPI_HANDLE     sb_handle;
  317         ACPI_STATUS     status;
  318 
  319         status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
  320         if (ACPI_SUCCESS(status)) {
  321                 AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
  322                     100, acpi_dock_eject_child, NULL, &dev, NULL);
  323         }
  324 }
  325 
  326 static void
  327 acpi_dock_removal(device_t dev)
  328 {
  329         struct acpi_dock_softc *sc;
  330 
  331         ACPI_SERIAL_ASSERT(dock);
  332 
  333         sc = device_get_softc(dev);
  334         if (sc->status == ACPI_DOCK_STATUS_DOCKED ||
  335             sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
  336                 acpi_dock_eject_children(dev);
  337                 if (acpi_dock_execute_dck(dev, ACPI_DOCK_ISOLATE) != 0)
  338                         return;
  339 
  340                 acpi_dock_execute_lck(dev, ACPI_DOCK_UNLOCK);
  341 
  342                 if (acpi_dock_execute_ejx(dev, 1, 0) != 0) {
  343                         device_printf(dev, "_EJ0 failed\n");
  344                         return;
  345                 }
  346 
  347                 sc->status = ACPI_DOCK_STATUS_UNDOCKED;
  348         }
  349 
  350         acpi_dock_get_info(dev);
  351         if (sc->_sta != 0)
  352                 device_printf(dev, "mechanical failure (%#x).\n", sc->_sta);
  353 }
  354 
  355 /*
  356  * Device/Bus check
  357  */
  358 
  359 static void
  360 acpi_dock_device_check(device_t dev)
  361 {
  362         struct acpi_dock_softc *sc;
  363 
  364         ACPI_SERIAL_ASSERT(dock);
  365 
  366         sc = device_get_softc(dev);
  367         acpi_dock_get_info(dev);
  368 
  369         /*
  370          * If the _STA method indicates 'present' and 'functioning', the
  371          * system is docked.  If _STA does not exist for this device, it
  372          * is always present.
  373          */
  374         if (sc->_sta == ACPI_DOCK_STATUS_UNKNOWN ||
  375             ACPI_DEVICE_PRESENT(sc->_sta))
  376                 acpi_dock_insert(dev);
  377         else if (sc->_sta == 0)
  378                 acpi_dock_removal(dev);
  379 }
  380 
  381 /*
  382  * Notify Handler
  383  */
  384 
  385 static void
  386 acpi_dock_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
  387 {
  388         device_t        dev;
  389 
  390         dev = (device_t) context;
  391         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
  392                     "got notification %#x\n", notify);
  393 
  394         ACPI_SERIAL_BEGIN(dock);
  395         switch (notify) {
  396         case ACPI_NOTIFY_BUS_CHECK:
  397         case ACPI_NOTIFY_DEVICE_CHECK:
  398                 acpi_dock_device_check(dev);
  399                 break;
  400         case ACPI_NOTIFY_EJECT_REQUEST:
  401                 acpi_dock_removal(dev);
  402                 break;
  403         default:
  404                 device_printf(dev, "unknown notify %#x\n", notify);
  405                 break;
  406         }
  407         ACPI_SERIAL_END(dock);
  408 }
  409 
  410 static int
  411 acpi_dock_status_sysctl(SYSCTL_HANDLER_ARGS)
  412 {
  413         struct acpi_dock_softc *sc;
  414         device_t        dev;
  415         int             status, err;
  416 
  417         err = 0;
  418         dev = (device_t)arg1;
  419 
  420         sc = device_get_softc(dev);
  421         status = sc->status;
  422 
  423         ACPI_SERIAL_BEGIN(dock);
  424         err = sysctl_handle_int(oidp, &status, 0, req);
  425         if (err != 0 || req->newptr == NULL)
  426                 goto out;
  427 
  428         if (status != ACPI_DOCK_STATUS_UNDOCKED &&
  429             status != ACPI_DOCK_STATUS_DOCKED) {
  430                 err = EINVAL;
  431                 goto out;
  432         }
  433 
  434         if (status == sc->status)
  435                 goto out;
  436 
  437         switch (status) {
  438         case ACPI_DOCK_STATUS_UNDOCKED:
  439                 acpi_dock_removal(dev);
  440                 break;
  441         case ACPI_DOCK_STATUS_DOCKED:
  442                 acpi_dock_device_check(dev);
  443                 break;
  444         default:
  445                 err = EINVAL;
  446                 break;
  447         }
  448 out:
  449         ACPI_SERIAL_END(dock);
  450         return (err);
  451 }
  452 
  453 static int
  454 acpi_dock_probe(device_t dev)
  455 {
  456         ACPI_HANDLE     h, tmp;
  457 
  458         h = acpi_get_handle(dev);
  459         if (acpi_disabled("dock") ||
  460             ACPI_FAILURE(AcpiGetHandle(h, "_DCK", &tmp)))
  461                 return (ENXIO);
  462 
  463         device_set_desc(dev, "ACPI Docking Station");
  464 
  465         /*
  466          * XXX Somewhere else in the kernel panics on "sysctl kern" if we
  467          * return a negative value here (reprobe ok).
  468          */
  469         return (0);
  470 }
  471 
  472 static int
  473 acpi_dock_attach(device_t dev)
  474 {
  475         struct acpi_dock_softc *sc;
  476         ACPI_HANDLE     h;
  477 
  478         sc = device_get_softc(dev);
  479         h = acpi_get_handle(dev);
  480         if (sc == NULL || h == NULL)
  481                 return (ENXIO);
  482 
  483         sc->status = ACPI_DOCK_STATUS_UNKNOWN;
  484 
  485         AcpiEvaluateObject(h, "_INI", NULL, NULL);
  486 
  487         ACPI_SERIAL_BEGIN(dock);
  488 
  489         acpi_dock_device_check(dev);
  490 
  491         /* Get the sysctl tree */
  492         sc->sysctl_ctx = device_get_sysctl_ctx(dev);
  493         sc->sysctl_tree = device_get_sysctl_tree(dev);
  494 
  495         SYSCTL_ADD_INT(sc->sysctl_ctx,
  496                 SYSCTL_CHILDREN(sc->sysctl_tree),
  497                 OID_AUTO, "_sta", CTLFLAG_RD,
  498                 &sc->_sta, 0, "Dock _STA");
  499         SYSCTL_ADD_INT(sc->sysctl_ctx,
  500                 SYSCTL_CHILDREN(sc->sysctl_tree),
  501                 OID_AUTO, "_bdn", CTLFLAG_RD,
  502                 &sc->_bdn, 0, "Dock _BDN");
  503         SYSCTL_ADD_INT(sc->sysctl_ctx,
  504                 SYSCTL_CHILDREN(sc->sysctl_tree),
  505                 OID_AUTO, "_uid", CTLFLAG_RD,
  506                 &sc->_uid, 0, "Dock _UID");
  507         SYSCTL_ADD_PROC(sc->sysctl_ctx,
  508                 SYSCTL_CHILDREN(sc->sysctl_tree),
  509                 OID_AUTO, "status",
  510                 CTLTYPE_INT|CTLFLAG_RW, dev, 0,
  511                 acpi_dock_status_sysctl, "I",
  512                 "Dock/Undock operation");
  513 
  514         ACPI_SERIAL_END(dock);
  515 
  516         AcpiInstallNotifyHandler(h, ACPI_ALL_NOTIFY,
  517                                  acpi_dock_notify_handler, dev);
  518 
  519         return (0);
  520 }
  521 
  522 static device_method_t acpi_dock_methods[] = {
  523         /* Device interface */
  524         DEVMETHOD(device_probe, acpi_dock_probe),
  525         DEVMETHOD(device_attach, acpi_dock_attach),
  526 
  527         {0, 0}
  528 };
  529 
  530 static driver_t acpi_dock_driver = {
  531         "acpi_dock",
  532         acpi_dock_methods,
  533         sizeof(struct acpi_dock_softc),
  534 };
  535 
  536 static devclass_t acpi_dock_devclass;
  537 
  538 DRIVER_MODULE(acpi_dock, acpi, acpi_dock_driver, acpi_dock_devclass, 0, 0);
  539 MODULE_DEPEND(acpi_dock, acpi, 1, 1, 1);
  540 

Cache object: 634acdc7ffc596799262ff1ec46bf363


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