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

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
    3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
    4  * Copyright (c) 2000, 2001 Michael Smith
    5  * Copyright (c) 2000 BSDi
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/5.4/sys/dev/acpica/acpi.c 143003 2005-03-02 09:26:43Z obrien $");
   32 
   33 #include "opt_acpi.h"
   34 #include <sys/param.h>
   35 #include <sys/kernel.h>
   36 #include <sys/proc.h>
   37 #include <sys/fcntl.h>
   38 #include <sys/malloc.h>
   39 #include <sys/module.h>
   40 #include <sys/bus.h>
   41 #include <sys/conf.h>
   42 #include <sys/ioccom.h>
   43 #include <sys/reboot.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/ctype.h>
   46 #include <sys/linker.h>
   47 #include <sys/power.h>
   48 #include <sys/sbuf.h>
   49 #include <sys/smp.h>
   50 
   51 #include <machine/clock.h>
   52 #include <machine/resource.h>
   53 #include <machine/bus.h>
   54 #include <sys/rman.h>
   55 #include <isa/isavar.h>
   56 #include <isa/pnpvar.h>
   57 
   58 #include "acpi.h"
   59 #include <dev/acpica/acpivar.h>
   60 #include <dev/acpica/acpiio.h>
   61 #include <contrib/dev/acpica/acnamesp.h>
   62 
   63 #include "pci_if.h"
   64 #include <dev/pci/pcivar.h>
   65 #include <dev/pci/pci_private.h>
   66 
   67 MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
   68 
   69 /* Hooks for the ACPI CA debugging infrastructure */
   70 #define _COMPONENT      ACPI_BUS
   71 ACPI_MODULE_NAME("ACPI")
   72 
   73 static d_open_t         acpiopen;
   74 static d_close_t        acpiclose;
   75 static d_ioctl_t        acpiioctl;
   76 
   77 static struct cdevsw acpi_cdevsw = {
   78         .d_version =    D_VERSION,
   79         .d_open =       acpiopen,
   80         .d_close =      acpiclose,
   81         .d_ioctl =      acpiioctl,
   82         .d_name =       "acpi",
   83 };
   84 
   85 /* Global mutex for locking access to the ACPI subsystem. */
   86 struct mtx      acpi_mutex;
   87 
   88 /* Bitmap of device quirks. */
   89 int             acpi_quirks;
   90 
   91 static int      acpi_modevent(struct module *mod, int event, void *junk);
   92 static void     acpi_identify(driver_t *driver, device_t parent);
   93 static int      acpi_probe(device_t dev);
   94 static int      acpi_attach(device_t dev);
   95 static int      acpi_suspend(device_t dev);
   96 static int      acpi_resume(device_t dev);
   97 static int      acpi_shutdown(device_t dev);
   98 static device_t acpi_add_child(device_t bus, int order, const char *name,
   99                         int unit);
  100 static int      acpi_print_child(device_t bus, device_t child);
  101 static void     acpi_probe_nomatch(device_t bus, device_t child);
  102 static void     acpi_driver_added(device_t dev, driver_t *driver);
  103 static int      acpi_read_ivar(device_t dev, device_t child, int index,
  104                         uintptr_t *result);
  105 static int      acpi_write_ivar(device_t dev, device_t child, int index,
  106                         uintptr_t value);
  107 static struct resource_list *acpi_get_rlist(device_t dev, device_t child);
  108 static int      acpi_sysres_alloc(device_t dev);
  109 static struct resource_list_entry *acpi_sysres_find(device_t dev, int type,
  110                     u_long addr);
  111 static struct resource *acpi_alloc_resource(device_t bus, device_t child,
  112                         int type, int *rid, u_long start, u_long end,
  113                         u_long count, u_int flags);
  114 static int      acpi_release_resource(device_t bus, device_t child, int type,
  115                         int rid, struct resource *r);
  116 static uint32_t acpi_isa_get_logicalid(device_t dev);
  117 static int      acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count);
  118 static char     *acpi_device_id_probe(device_t bus, device_t dev, char **ids);
  119 static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev,
  120                     ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters,
  121                     ACPI_BUFFER *ret);
  122 static int      acpi_device_pwr_for_sleep(device_t bus, device_t dev,
  123                     int *dstate);
  124 static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level,
  125                     void *context, void **retval);
  126 static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev,
  127                     int max_depth, acpi_scan_cb_t user_fn, void *arg);
  128 static int      acpi_set_powerstate_method(device_t bus, device_t child,
  129                     int state);
  130 static int      acpi_isa_pnp_probe(device_t bus, device_t child,
  131                     struct isa_pnp_id *ids);
  132 static void     acpi_probe_children(device_t bus);
  133 static int      acpi_probe_order(ACPI_HANDLE handle, int *order);
  134 static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level,
  135                     void *context, void **status);
  136 static BOOLEAN  acpi_MatchHid(ACPI_HANDLE h, const char *hid);
  137 static void     acpi_shutdown_final(void *arg, int howto);
  138 static void     acpi_enable_fixed_events(struct acpi_softc *sc);
  139 static int      acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate);
  140 static int      acpi_wake_run_prep(ACPI_HANDLE handle, int sstate);
  141 static int      acpi_wake_prep_walk(int sstate);
  142 static int      acpi_wake_sysctl_walk(device_t dev);
  143 static int      acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS);
  144 static void     acpi_system_eventhandler_sleep(void *arg, int state);
  145 static void     acpi_system_eventhandler_wakeup(void *arg, int state);
  146 static int      acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
  147 static int      acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
  148 static int      acpi_pm_func(u_long cmd, void *arg, ...);
  149 static int      acpi_child_location_str_method(device_t acdev, device_t child,
  150                                                char *buf, size_t buflen);
  151 static int      acpi_child_pnpinfo_str_method(device_t acdev, device_t child,
  152                                               char *buf, size_t buflen);
  153 
  154 static device_method_t acpi_methods[] = {
  155     /* Device interface */
  156     DEVMETHOD(device_identify,          acpi_identify),
  157     DEVMETHOD(device_probe,             acpi_probe),
  158     DEVMETHOD(device_attach,            acpi_attach),
  159     DEVMETHOD(device_shutdown,          acpi_shutdown),
  160     DEVMETHOD(device_detach,            bus_generic_detach),
  161     DEVMETHOD(device_suspend,           acpi_suspend),
  162     DEVMETHOD(device_resume,            acpi_resume),
  163 
  164     /* Bus interface */
  165     DEVMETHOD(bus_add_child,            acpi_add_child),
  166     DEVMETHOD(bus_print_child,          acpi_print_child),
  167     DEVMETHOD(bus_probe_nomatch,        acpi_probe_nomatch),
  168     DEVMETHOD(bus_driver_added,         acpi_driver_added),
  169     DEVMETHOD(bus_read_ivar,            acpi_read_ivar),
  170     DEVMETHOD(bus_write_ivar,           acpi_write_ivar),
  171     DEVMETHOD(bus_get_resource_list,    acpi_get_rlist),
  172     DEVMETHOD(bus_set_resource,         bus_generic_rl_set_resource),
  173     DEVMETHOD(bus_get_resource,         bus_generic_rl_get_resource),
  174     DEVMETHOD(bus_alloc_resource,       acpi_alloc_resource),
  175     DEVMETHOD(bus_release_resource,     acpi_release_resource),
  176     DEVMETHOD(bus_child_pnpinfo_str,    acpi_child_pnpinfo_str_method),
  177     DEVMETHOD(bus_child_location_str,   acpi_child_location_str_method),
  178     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
  179     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
  180     DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
  181     DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
  182 
  183     /* ACPI bus */
  184     DEVMETHOD(acpi_id_probe,            acpi_device_id_probe),
  185     DEVMETHOD(acpi_evaluate_object,     acpi_device_eval_obj),
  186     DEVMETHOD(acpi_pwr_for_sleep,       acpi_device_pwr_for_sleep),
  187     DEVMETHOD(acpi_scan_children,       acpi_device_scan_children),
  188 
  189     /* PCI emulation */
  190     DEVMETHOD(pci_set_powerstate,       acpi_set_powerstate_method),
  191 
  192     /* ISA emulation */
  193     DEVMETHOD(isa_pnp_probe,            acpi_isa_pnp_probe),
  194 
  195     {0, 0}
  196 };
  197 
  198 static driver_t acpi_driver = {
  199     "acpi",
  200     acpi_methods,
  201     sizeof(struct acpi_softc),
  202 };
  203 
  204 static devclass_t acpi_devclass;
  205 DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
  206 MODULE_VERSION(acpi, 1);
  207 
  208 ACPI_SERIAL_DECL(acpi, "ACPI root bus");
  209 
  210 /* Local pools for managing system resources for ACPI child devices. */
  211 static struct rman acpi_rman_io, acpi_rman_mem;
  212 
  213 #define ACPI_MINIMUM_AWAKETIME  5
  214 
  215 static const char* sleep_state_names[] = {
  216     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
  217 
  218 SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging");
  219 static char acpi_ca_version[12];
  220 SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD,
  221               acpi_ca_version, 0, "Version of Intel ACPI-CA");
  222 
  223 /*
  224  * Allow override of whether methods execute in parallel or not.
  225  * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS"
  226  * errors for AML that really can't handle parallel method execution.
  227  * It is off by default since this breaks recursive methods and
  228  * some IBMs use such code.
  229  */
  230 static int acpi_serialize_methods;
  231 TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods);
  232 
  233 /* Power devices off and on in suspend and resume.  XXX Remove once tested. */
  234 static int acpi_do_powerstate = 1;
  235 TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate);
  236 SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW,
  237     &acpi_do_powerstate, 1, "Turn off devices when suspending.");
  238 
  239 /* Allow users to override quirks. */
  240 TUNABLE_INT("debug.acpi.quirks", &acpi_quirks);
  241 
  242 /*
  243  * ACPI can only be loaded as a module by the loader; activating it after
  244  * system bootstrap time is not useful, and can be fatal to the system.
  245  * It also cannot be unloaded, since the entire system bus heirarchy hangs
  246  * off it.
  247  */
  248 static int
  249 acpi_modevent(struct module *mod, int event, void *junk)
  250 {
  251     switch (event) {
  252     case MOD_LOAD:
  253         if (!cold) {
  254             printf("The ACPI driver cannot be loaded after boot.\n");
  255             return (EPERM);
  256         }
  257         break;
  258     case MOD_UNLOAD:
  259         if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
  260             return (EBUSY);
  261         break;
  262     default:
  263         break;
  264     }
  265     return (0);
  266 }
  267 
  268 /*
  269  * Perform early initialization.
  270  */
  271 ACPI_STATUS
  272 acpi_Startup(void)
  273 {
  274     static int started = 0;
  275     int error, val;
  276 
  277     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  278 
  279     /* Only run the startup code once.  The MADT driver also calls this. */
  280     if (started)
  281         return_VALUE (0);
  282     started = 1;
  283 
  284     /* Initialise the ACPI mutex */
  285     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
  286 
  287     /*
  288      * Set the globals from our tunables.  This is needed because ACPI-CA
  289      * uses UINT8 for some values and we have no tunable_byte.
  290      */
  291     AcpiGbl_AllMethodsSerialized = acpi_serialize_methods;
  292     AcpiGbl_EnableInterpreterSlack = TRUE;
  293 
  294     /* Start up the ACPI CA subsystem. */
  295     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
  296         printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
  297         return_VALUE (error);
  298     }
  299 
  300     if (ACPI_FAILURE(error = AcpiLoadTables())) {
  301         printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
  302         AcpiTerminate();
  303         return_VALUE (error);
  304     }
  305 
  306     /* Set up any quirks we have for this system. */
  307     if (acpi_quirks == 0)
  308         acpi_table_quirks(&acpi_quirks);
  309 
  310     /* If the user manually set the disabled hint to 0, force-enable ACPI. */
  311     if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0)
  312         acpi_quirks &= ~ACPI_Q_BROKEN;
  313     if (acpi_quirks & ACPI_Q_BROKEN) {
  314         printf("ACPI disabled by blacklist.  Contact your BIOS vendor.\n");
  315         AcpiTerminate();
  316         return_VALUE (AE_ERROR);
  317     }
  318 
  319     return_VALUE (AE_OK);
  320 }
  321 
  322 /*
  323  * Detect ACPI, perform early initialisation
  324  */
  325 static void
  326 acpi_identify(driver_t *driver, device_t parent)
  327 {
  328     device_t    child;
  329 
  330     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  331 
  332     if (!cold)
  333         return_VOID;
  334 
  335     /* Check that we haven't been disabled with a hint. */
  336     if (resource_disabled("acpi", 0))
  337         return_VOID;
  338 
  339     /* Make sure we're not being doubly invoked. */
  340     if (device_find_child(parent, "acpi", 0) != NULL)
  341         return_VOID;
  342 
  343     /* Initialize ACPI-CA. */
  344     if (ACPI_FAILURE(acpi_Startup()))
  345         return_VOID;
  346 
  347     snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION);
  348 
  349     /* Attach the actual ACPI device. */
  350     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
  351         device_printf(parent, "device_identify failed\n");
  352         return_VOID;
  353     }
  354 }
  355 
  356 /*
  357  * Fetch some descriptive data from ACPI to put in our attach message.
  358  */
  359 static int
  360 acpi_probe(device_t dev)
  361 {
  362     ACPI_TABLE_HEADER   th;
  363     char                buf[20];
  364     int                 error;
  365     struct sbuf         sb;
  366     ACPI_STATUS         status;
  367 
  368     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  369 
  370     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
  371         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
  372         device_printf(dev, "probe failed, other PM system enabled.\n");
  373         return_VALUE (ENXIO);
  374     }
  375 
  376     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
  377         device_printf(dev, "couldn't get XSDT header: %s\n",
  378                       AcpiFormatException(status));
  379         error = ENXIO;
  380     } else {
  381         sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
  382         sbuf_bcat(&sb, th.OemId, 6);
  383         sbuf_trim(&sb);
  384         sbuf_putc(&sb, ' ');
  385         sbuf_bcat(&sb, th.OemTableId, 8);
  386         sbuf_trim(&sb);
  387         sbuf_finish(&sb);
  388         device_set_desc_copy(dev, sbuf_data(&sb));
  389         sbuf_delete(&sb);
  390         error = 0;
  391     }
  392 
  393     return_VALUE (error);
  394 }
  395 
  396 static int
  397 acpi_attach(device_t dev)
  398 {
  399     struct acpi_softc   *sc;
  400     ACPI_STATUS         status;
  401     int                 error, state;
  402     UINT32              flags;
  403     UINT8               TypeA, TypeB;
  404     char                *env;
  405 
  406     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  407 
  408     sc = device_get_softc(dev);
  409     sc->acpi_dev = dev;
  410 
  411     /* Initialize resource manager. */
  412     acpi_rman_io.rm_type = RMAN_ARRAY;
  413     acpi_rman_io.rm_start = 0;
  414     acpi_rman_io.rm_end = 0xffff;
  415     acpi_rman_io.rm_descr = "I/O ports";
  416     if (rman_init(&acpi_rman_io) != 0)
  417         panic("acpi rman_init IO ports failed");
  418     acpi_rman_mem.rm_type = RMAN_ARRAY;
  419     acpi_rman_mem.rm_start = 0;
  420     acpi_rman_mem.rm_end = ~0ul;
  421     acpi_rman_mem.rm_descr = "I/O memory addresses";
  422     if (rman_init(&acpi_rman_mem) != 0)
  423         panic("acpi rman_init memory failed");
  424 
  425     /* Install the default address space handlers. */
  426     error = ENXIO;
  427     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  428                 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
  429     if (ACPI_FAILURE(status)) {
  430         device_printf(dev, "Could not initialise SystemMemory handler: %s\n",
  431                       AcpiFormatException(status));
  432         goto out;
  433     }
  434     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  435                 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
  436     if (ACPI_FAILURE(status)) {
  437         device_printf(dev, "Could not initialise SystemIO handler: %s\n",
  438                       AcpiFormatException(status));
  439         goto out;
  440     }
  441     status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  442                 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
  443     if (ACPI_FAILURE(status)) {
  444         device_printf(dev, "could not initialise PciConfig handler: %s\n",
  445                       AcpiFormatException(status));
  446         goto out;
  447     }
  448 
  449     /*
  450      * Note that some systems (specifically, those with namespace evaluation
  451      * issues that require the avoidance of parts of the namespace) must
  452      * avoid running _INI and _STA on everything, as well as dodging the final
  453      * object init pass.
  454      *
  455      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
  456      *
  457      * XXX We should arrange for the object init pass after we have attached
  458      *     all our child devices, but on many systems it works here.
  459      */
  460     flags = 0;
  461     if (testenv("debug.acpi.avoid"))
  462         flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
  463 
  464     /* Bring the hardware and basic handlers online. */
  465     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
  466         device_printf(dev, "Could not enable ACPI: %s\n",
  467                       AcpiFormatException(status));
  468         goto out;
  469     }
  470 
  471     /*
  472      * Call the ECDT probe function to provide EC functionality before
  473      * the namespace has been evaluated.
  474      */
  475     acpi_ec_ecdt_probe(dev);
  476 
  477     /* Bring device objects and regions online. */
  478     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
  479         device_printf(dev, "Could not initialize ACPI objects: %s\n",
  480                       AcpiFormatException(status));
  481         goto out;
  482     }
  483 
  484     /*
  485      * Setup our sysctl tree.
  486      *
  487      * XXX: This doesn't check to make sure that none of these fail.
  488      */
  489     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
  490     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
  491                                SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
  492                                device_get_name(dev), CTLFLAG_RD, 0, "");
  493     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  494         OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
  495         0, 0, acpi_supported_sleep_state_sysctl, "A", "");
  496     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  497         OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
  498         &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
  499     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  500         OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
  501         &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
  502     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  503         OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
  504         &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
  505     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  506         OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
  507         &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
  508     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  509         OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
  510         &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
  511     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  512         OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
  513         &sc->acpi_sleep_delay, 0, "sleep delay");
  514     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  515         OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
  516         &sc->acpi_s4bios, 0, "S4BIOS mode");
  517     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  518         OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
  519         &sc->acpi_verbose, 0, "verbose mode");
  520 
  521     /*
  522      * Default to 1 second before sleeping to give some machines time to
  523      * stabilize.
  524      */
  525     sc->acpi_sleep_delay = 1;
  526     if (bootverbose)
  527         sc->acpi_verbose = 1;
  528     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "")) {
  529         sc->acpi_verbose = 1;
  530         freeenv(env);
  531     }
  532 
  533     /* Only enable S4BIOS by default if the FACS says it is available. */
  534     if (AcpiGbl_FACS->S4Bios_f != 0)
  535         sc->acpi_s4bios = 1;
  536 
  537     /*
  538      * Dispatch the default sleep state to devices.  The lid switch is set
  539      * to NONE by default to avoid surprising users.
  540      */
  541     sc->acpi_power_button_sx = ACPI_STATE_S5;
  542     sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1;
  543     sc->acpi_standby_sx = ACPI_STATE_S1;
  544     sc->acpi_suspend_sx = ACPI_STATE_S3;
  545 
  546     /* Pick the first valid sleep state for the sleep button default. */
  547     sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1;
  548     for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++)
  549         if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
  550             sc->acpi_sleep_button_sx = state;
  551             break;
  552         }
  553 
  554     acpi_enable_fixed_events(sc);
  555 
  556     /*
  557      * Scan the namespace and attach/initialise children.
  558      */
  559 
  560     /* Register our shutdown handler. */
  561     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc,
  562         SHUTDOWN_PRI_LAST);
  563 
  564     /*
  565      * Register our acpi event handlers.
  566      * XXX should be configurable eg. via userland policy manager.
  567      */
  568     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep,
  569         sc, ACPI_EVENT_PRI_LAST);
  570     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup,
  571         sc, ACPI_EVENT_PRI_LAST);
  572 
  573     /* Flag our initial states. */
  574     sc->acpi_enabled = 1;
  575     sc->acpi_sstate = ACPI_STATE_S0;
  576     sc->acpi_sleep_disabled = 0;
  577 
  578     /* Create the control device */
  579     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
  580                               "acpi");
  581     sc->acpi_dev_t->si_drv1 = sc;
  582 
  583     if ((error = acpi_task_thread_init()))
  584         goto out;
  585 
  586     if ((error = acpi_machdep_init(dev)))
  587         goto out;
  588 
  589     /* Register ACPI again to pass the correct argument of pm_func. */
  590     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
  591 
  592     if (!acpi_disabled("bus"))
  593         acpi_probe_children(dev);
  594 
  595     error = 0;
  596 
  597  out:
  598     return_VALUE (error);
  599 }
  600 
  601 static int
  602 acpi_suspend(device_t dev)
  603 {
  604     struct acpi_softc *sc;
  605     device_t child, *devlist;
  606     int error, i, numdevs, pstate;
  607 
  608     GIANT_REQUIRED;
  609 
  610     /* First give child devices a chance to suspend. */
  611     error = bus_generic_suspend(dev);
  612     if (error)
  613         return (error);
  614 
  615     /*
  616      * Now, set them into the appropriate power state, usually D3.  If the
  617      * device has an _SxD method for the next sleep state, use that power
  618      * state instead.
  619      */
  620     sc = device_get_softc(dev);
  621     device_get_children(dev, &devlist, &numdevs);
  622     for (i = 0; i < numdevs; i++) {
  623         /* If the device is not attached, we've powered it down elsewhere. */
  624         child = devlist[i];
  625         if (!device_is_attached(child))
  626             continue;
  627 
  628         /*
  629          * Default to D3 for all sleep states.  The _SxD method is optional
  630          * so set the powerstate even if it's absent.
  631          */
  632         pstate = PCI_POWERSTATE_D3;
  633         error = acpi_device_pwr_for_sleep(device_get_parent(child),
  634             child, &pstate);
  635         if ((error == 0 || error == ESRCH) && acpi_do_powerstate)
  636             pci_set_powerstate(child, pstate);
  637     }
  638     free(devlist, M_TEMP);
  639     error = 0;
  640 
  641     return (error);
  642 }
  643 
  644 static int
  645 acpi_resume(device_t dev)
  646 {
  647     ACPI_HANDLE handle;
  648     int i, numdevs;
  649     device_t child, *devlist;
  650 
  651     GIANT_REQUIRED;
  652 
  653     /*
  654      * Put all devices in D0 before resuming them.  Call _S0D on each one
  655      * since some systems expect this.
  656      */
  657     device_get_children(dev, &devlist, &numdevs);
  658     for (i = 0; i < numdevs; i++) {
  659         child = devlist[i];
  660         handle = acpi_get_handle(child);
  661         if (handle)
  662             AcpiEvaluateObject(handle, "_S0D", NULL, NULL);
  663         if (device_is_attached(child) && acpi_do_powerstate)
  664             pci_set_powerstate(child, PCI_POWERSTATE_D0);
  665     }
  666     free(devlist, M_TEMP);
  667 
  668     return (bus_generic_resume(dev));
  669 }
  670 
  671 static int
  672 acpi_shutdown(device_t dev)
  673 {
  674 
  675     GIANT_REQUIRED;
  676 
  677     /* Allow children to shutdown first. */
  678     bus_generic_shutdown(dev);
  679 
  680     /*
  681      * Enable any GPEs that are able to power-on the system (i.e., RTC).
  682      * Also, disable any that are not valid for this state (most).
  683      */
  684     acpi_wake_prep_walk(ACPI_STATE_S5);
  685 
  686     return (0);
  687 }
  688 
  689 /*
  690  * Handle a new device being added
  691  */
  692 static device_t
  693 acpi_add_child(device_t bus, int order, const char *name, int unit)
  694 {
  695     struct acpi_device  *ad;
  696     device_t            child;
  697 
  698     if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL)
  699         return (NULL);
  700 
  701     resource_list_init(&ad->ad_rl);
  702 
  703     child = device_add_child_ordered(bus, order, name, unit);
  704     if (child != NULL)
  705         device_set_ivars(child, ad);
  706     return (child);
  707 }
  708 
  709 static int
  710 acpi_print_child(device_t bus, device_t child)
  711 {
  712     struct acpi_device   *adev = device_get_ivars(child);
  713     struct resource_list *rl = &adev->ad_rl;
  714     int retval = 0;
  715 
  716     retval += bus_print_child_header(bus, child);
  717     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
  718     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
  719     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
  720     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
  721     if (device_get_flags(child))
  722         retval += printf(" flags %#x", device_get_flags(child));
  723     retval += bus_print_child_footer(bus, child);
  724 
  725     return (retval);
  726 }
  727 
  728 /*
  729  * If this device is an ACPI child but no one claimed it, attempt
  730  * to power it off.  We'll power it back up when a driver is added.
  731  *
  732  * XXX Disabled for now since many necessary devices (like fdc and
  733  * ATA) don't claim the devices we created for them but still expect
  734  * them to be powered up.
  735  */
  736 static void
  737 acpi_probe_nomatch(device_t bus, device_t child)
  738 {
  739 
  740     /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
  741 }
  742 
  743 /*
  744  * If a new driver has a chance to probe a child, first power it up.
  745  *
  746  * XXX Disabled for now (see acpi_probe_nomatch for details).
  747  */
  748 static void
  749 acpi_driver_added(device_t dev, driver_t *driver)
  750 {
  751     device_t child, *devlist;
  752     int i, numdevs;
  753 
  754     DEVICE_IDENTIFY(driver, dev);
  755     device_get_children(dev, &devlist, &numdevs);
  756     for (i = 0; i < numdevs; i++) {
  757         child = devlist[i];
  758         if (device_get_state(child) == DS_NOTPRESENT) {
  759             /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
  760             if (device_probe_and_attach(child) != 0)
  761                 ; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
  762         }
  763     }
  764     free(devlist, M_TEMP);
  765 }
  766 
  767 /* Location hint for devctl(8) */
  768 static int
  769 acpi_child_location_str_method(device_t cbdev, device_t child, char *buf,
  770     size_t buflen)
  771 {
  772     struct acpi_device *dinfo = device_get_ivars(child);
  773 
  774     if (dinfo->ad_handle)
  775         snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle));
  776     else
  777         snprintf(buf, buflen, "unknown");
  778     return (0);
  779 }
  780 
  781 /* PnP information for devctl(8) */
  782 static int
  783 acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf,
  784     size_t buflen)
  785 {
  786     ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 
  787     ACPI_DEVICE_INFO *adinfo;
  788     struct acpi_device *dinfo = device_get_ivars(child);
  789     char *end;
  790     int error;
  791 
  792     error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf);
  793     adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer;
  794     if (error)
  795         snprintf(buf, buflen, "unknown");
  796     else
  797         snprintf(buf, buflen, "_HID=%s _UID=%lu",
  798                  (adinfo->Valid & ACPI_VALID_HID) ?
  799                  adinfo->HardwareId.Value : "none",
  800                  (adinfo->Valid & ACPI_VALID_UID) ?
  801                  strtoul(adinfo->UniqueId.Value, &end, 10) : 0);
  802     if (adinfo)
  803         AcpiOsFree(adinfo);
  804 
  805     return (0);
  806 }
  807 
  808 /*
  809  * Handle per-device ivars
  810  */
  811 static int
  812 acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
  813 {
  814     struct acpi_device  *ad;
  815 
  816     if ((ad = device_get_ivars(child)) == NULL) {
  817         printf("device has no ivars\n");
  818         return (ENOENT);
  819     }
  820 
  821     /* ACPI and ISA compatibility ivars */
  822     switch(index) {
  823     case ACPI_IVAR_HANDLE:
  824         *(ACPI_HANDLE *)result = ad->ad_handle;
  825         break;
  826     case ACPI_IVAR_MAGIC:
  827         *(int *)result = ad->ad_magic;
  828         break;
  829     case ACPI_IVAR_PRIVATE:
  830         *(void **)result = ad->ad_private;
  831         break;
  832     case ACPI_IVAR_FLAGS:
  833         *(int *)result = ad->ad_flags;
  834         break;
  835     case ISA_IVAR_VENDORID:
  836     case ISA_IVAR_SERIAL:
  837     case ISA_IVAR_COMPATID:
  838         *(int *)result = -1;
  839         break;
  840     case ISA_IVAR_LOGICALID:
  841         *(int *)result = acpi_isa_get_logicalid(child);
  842         break;
  843     default:
  844         return (ENOENT);
  845     }
  846 
  847     return (0);
  848 }
  849 
  850 static int
  851 acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
  852 {
  853     struct acpi_device  *ad;
  854 
  855     if ((ad = device_get_ivars(child)) == NULL) {
  856         printf("device has no ivars\n");
  857         return (ENOENT);
  858     }
  859 
  860     switch(index) {
  861     case ACPI_IVAR_HANDLE:
  862         ad->ad_handle = (ACPI_HANDLE)value;
  863         break;
  864     case ACPI_IVAR_MAGIC:
  865         ad->ad_magic = (int)value;
  866         break;
  867     case ACPI_IVAR_PRIVATE:
  868         ad->ad_private = (void *)value;
  869         break;
  870     case ACPI_IVAR_FLAGS:
  871         ad->ad_flags = (int)value;
  872         break;
  873     default:
  874         panic("bad ivar write request (%d)", index);
  875         return (ENOENT);
  876     }
  877 
  878     return (0);
  879 }
  880 
  881 /*
  882  * Handle child resource allocation/removal
  883  */
  884 static struct resource_list *
  885 acpi_get_rlist(device_t dev, device_t child)
  886 {
  887     struct acpi_device          *ad;
  888 
  889     ad = device_get_ivars(child);
  890     return (&ad->ad_rl);
  891 }
  892 
  893 /*
  894  * Pre-allocate/manage all memory and IO resources.  Since rman can't handle
  895  * duplicates, we merge any in the sysresource attach routine.
  896  */
  897 static int
  898 acpi_sysres_alloc(device_t dev)
  899 {
  900     struct resource *res;
  901     struct resource_list *rl;
  902     struct resource_list_entry *rle;
  903     struct rman *rm;
  904 
  905     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
  906     SLIST_FOREACH(rle, rl, link) {
  907         if (rle->res != NULL) {
  908             device_printf(dev, "duplicate resource for %lx\n", rle->start);
  909             continue;
  910         }
  911 
  912         /* Only memory and IO resources are valid here. */
  913         switch (rle->type) {
  914         case SYS_RES_IOPORT:
  915             rm = &acpi_rman_io;
  916             break;
  917         case SYS_RES_MEMORY:
  918             rm = &acpi_rman_mem;
  919             break;
  920         default:
  921             continue;
  922         }
  923 
  924         /* Pre-allocate resource and add to our rman pool. */
  925         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type,
  926             &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0);
  927         if (res != NULL) {
  928             rman_manage_region(rm, rman_get_start(res), rman_get_end(res));
  929             rle->res = res;
  930         } else
  931             device_printf(dev, "reservation of %lx, %lx (%d) failed\n",
  932                 rle->start, rle->count, rle->type);
  933     }
  934     return (0);
  935 }
  936 
  937 /* Find if we manage a given resource. */
  938 static struct resource_list_entry *
  939 acpi_sysres_find(device_t dev, int type, u_long addr)
  940 {
  941     struct resource_list *rl;
  942     struct resource_list_entry *rle;
  943 
  944     ACPI_SERIAL_ASSERT(acpi);
  945 
  946     /* We only consider IO and memory resources for our pool. */
  947     rle = NULL;
  948     if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
  949         goto out;
  950 
  951     rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
  952     SLIST_FOREACH(rle, rl, link) {
  953         if (type == rle->type && addr >= rle->start &&
  954             addr < rle->start + rle->count)
  955             break;
  956     }
  957 
  958 out:
  959     return (rle);
  960 }
  961 
  962 static struct resource *
  963 acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
  964     u_long start, u_long end, u_long count, u_int flags)
  965 {
  966     ACPI_RESOURCE ares;
  967     struct acpi_device *ad = device_get_ivars(child);
  968     struct resource_list *rl = &ad->ad_rl;
  969     struct resource_list_entry *rle;
  970     struct resource *res;
  971     struct rman *rm;
  972 
  973     res = NULL;
  974     ACPI_SERIAL_BEGIN(acpi);
  975 
  976     /*
  977      * If this is an allocation of the "default" range for a given RID, and
  978      * we know what the resources for this device are (i.e., they're on the
  979      * child's resource list), use those start/end values.
  980      */
  981     if (start == 0UL && end == ~0UL) {
  982         rle = resource_list_find(rl, type, *rid);
  983         if (rle == NULL)
  984             goto out;
  985         start = rle->start;
  986         end = rle->end;
  987         count = rle->count;
  988     }
  989 
  990     /* If we don't manage this address, pass the request up to the parent. */
  991     rle = acpi_sysres_find(bus, type, start);
  992     if (rle == NULL) {
  993         res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid,
  994             start, end, count, flags);
  995     } else {
  996 
  997         /* We only handle memory and IO resources through rman. */
  998         switch (type) {
  999         case SYS_RES_IOPORT:
 1000             rm = &acpi_rman_io;
 1001             break;
 1002         case SYS_RES_MEMORY:
 1003             rm = &acpi_rman_mem;
 1004             break;
 1005         default:
 1006             panic("acpi_alloc_resource: invalid res type %d", type);
 1007         }
 1008 
 1009         /* If we do know it, allocate it from the local pool. */
 1010         res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
 1011             child);
 1012         if (res == NULL)
 1013             goto out;
 1014 
 1015         /* Copy the bus tag and handle from the pre-allocated resource. */
 1016         rman_set_bustag(res, rman_get_bustag(rle->res));
 1017         rman_set_bushandle(res, rman_get_start(res));
 1018 
 1019         /* If requested, activate the resource using the parent's method. */
 1020         if (flags & RF_ACTIVE)
 1021             if (bus_activate_resource(child, type, *rid, res) != 0) {
 1022                 rman_release_resource(res);
 1023                 res = NULL;
 1024                 goto out;
 1025             }
 1026     }
 1027 
 1028     if (res != NULL && device_get_parent(child) == bus)
 1029         switch (type) {
 1030         case SYS_RES_IRQ:
 1031             /*
 1032              * Since bus_config_intr() takes immediate effect, we cannot
 1033              * configure the interrupt associated with a device when we
 1034              * parse the resources but have to defer it until a driver
 1035              * actually allocates the interrupt via bus_alloc_resource().
 1036              *
 1037              * XXX: Should we handle the lookup failing?
 1038              */
 1039             if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares)))
 1040                 acpi_config_intr(child, &ares);
 1041             break;
 1042         }
 1043 
 1044 out:
 1045     ACPI_SERIAL_END(acpi);
 1046     return (res);
 1047 }
 1048 
 1049 static int
 1050 acpi_release_resource(device_t bus, device_t child, int type, int rid,
 1051     struct resource *r)
 1052 {
 1053     int ret;
 1054 
 1055     ACPI_SERIAL_BEGIN(acpi);
 1056 
 1057     /*
 1058      * If we know about this address, deactivate it and release it to the
 1059      * local pool.  If we don't, pass this request up to the parent.
 1060      */
 1061     if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) {
 1062         if (rman_get_flags(r) & RF_ACTIVE) {
 1063             ret = bus_deactivate_resource(child, type, rid, r);
 1064             if (ret != 0)
 1065                 goto out;
 1066         }
 1067         ret = rman_release_resource(r);
 1068     } else
 1069         ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r);
 1070 
 1071 out:
 1072     ACPI_SERIAL_END(acpi);
 1073     return (ret);
 1074 }
 1075 
 1076 /* Allocate an IO port or memory resource, given its GAS. */
 1077 int
 1078 acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas,
 1079     struct resource **res)
 1080 {
 1081     int error, res_type;
 1082 
 1083     error = ENOMEM;
 1084     if (type == NULL || rid == NULL || gas == NULL || res == NULL)
 1085         return (EINVAL);
 1086 
 1087     /* We only support memory and IO spaces. */
 1088     switch (gas->AddressSpaceId) {
 1089     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
 1090         res_type = SYS_RES_MEMORY;
 1091         break;
 1092     case ACPI_ADR_SPACE_SYSTEM_IO:
 1093         res_type = SYS_RES_IOPORT;
 1094         break;
 1095     default:
 1096         return (EOPNOTSUPP);
 1097     }
 1098 
 1099     /*
 1100      * If the register width is less than 8, assume the BIOS author means
 1101      * it is a bit field and just allocate a byte.
 1102      */
 1103     if (gas->RegisterBitWidth && gas->RegisterBitWidth < 8)
 1104         gas->RegisterBitWidth = 8;
 1105 
 1106     /* Validate the address after we're sure we support the space. */
 1107     if (!ACPI_VALID_ADDRESS(gas->Address) || gas->RegisterBitWidth == 0)
 1108         return (EINVAL);
 1109 
 1110     bus_set_resource(dev, res_type, *rid, gas->Address,
 1111         gas->RegisterBitWidth / 8);
 1112     *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE);
 1113     if (*res != NULL) {
 1114         *type = res_type;
 1115         error = 0;
 1116     }
 1117     return (error);
 1118 }
 1119 
 1120 /* Probe _HID and _CID for compatible ISA PNP ids. */
 1121 static uint32_t
 1122 acpi_isa_get_logicalid(device_t dev)
 1123 {
 1124     ACPI_DEVICE_INFO    *devinfo;
 1125     ACPI_BUFFER         buf;
 1126     ACPI_HANDLE         h;
 1127     ACPI_STATUS         error;
 1128     u_int32_t           pnpid;
 1129 
 1130     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1131 
 1132     pnpid = 0;
 1133     buf.Pointer = NULL;
 1134     buf.Length = ACPI_ALLOCATE_BUFFER;
 1135 
 1136     /* Fetch and validate the HID. */
 1137     if ((h = acpi_get_handle(dev)) == NULL)
 1138         goto out;
 1139     error = AcpiGetObjectInfo(h, &buf);
 1140     if (ACPI_FAILURE(error))
 1141         goto out;
 1142     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
 1143 
 1144     if ((devinfo->Valid & ACPI_VALID_HID) != 0)
 1145         pnpid = PNP_EISAID(devinfo->HardwareId.Value);
 1146 
 1147 out:
 1148     if (buf.Pointer != NULL)
 1149         AcpiOsFree(buf.Pointer);
 1150     return_VALUE (pnpid);
 1151 }
 1152 
 1153 static int
 1154 acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count)
 1155 {
 1156     ACPI_DEVICE_INFO    *devinfo;
 1157     ACPI_BUFFER         buf;
 1158     ACPI_HANDLE         h;
 1159     ACPI_STATUS         error;
 1160     uint32_t            *pnpid;
 1161     int                 valid, i;
 1162 
 1163     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1164 
 1165     pnpid = cids;
 1166     valid = 0;
 1167     buf.Pointer = NULL;
 1168     buf.Length = ACPI_ALLOCATE_BUFFER;
 1169 
 1170     /* Fetch and validate the CID */
 1171     if ((h = acpi_get_handle(dev)) == NULL)
 1172         goto out;
 1173     error = AcpiGetObjectInfo(h, &buf);
 1174     if (ACPI_FAILURE(error))
 1175         goto out;
 1176     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
 1177     if ((devinfo->Valid & ACPI_VALID_CID) == 0)
 1178         goto out;
 1179 
 1180     if (devinfo->CompatibilityId.Count < count)
 1181         count = devinfo->CompatibilityId.Count;
 1182     for (i = 0; i < count; i++) {
 1183         if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0)
 1184             continue;
 1185         *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value);
 1186         valid++;
 1187     }
 1188 
 1189 out:
 1190     if (buf.Pointer != NULL)
 1191         AcpiOsFree(buf.Pointer);
 1192     return_VALUE (valid);
 1193 }
 1194 
 1195 static char *
 1196 acpi_device_id_probe(device_t bus, device_t dev, char **ids) 
 1197 {
 1198     ACPI_HANDLE h;
 1199     int i;
 1200 
 1201     h = acpi_get_handle(dev);
 1202     if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE)
 1203         return (NULL);
 1204 
 1205     /* Try to match one of the array of IDs with a HID or CID. */
 1206     for (i = 0; ids[i] != NULL; i++) {
 1207         if (acpi_MatchHid(h, ids[i]))
 1208             return (ids[i]);
 1209     }
 1210     return (NULL);
 1211 }
 1212 
 1213 static ACPI_STATUS
 1214 acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname,
 1215     ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret)
 1216 {
 1217     ACPI_HANDLE h;
 1218 
 1219     if (dev == NULL)
 1220         h = ACPI_ROOT_OBJECT;
 1221     else if ((h = acpi_get_handle(dev)) == NULL)
 1222         return (AE_BAD_PARAMETER);
 1223     return (AcpiEvaluateObject(h, pathname, parameters, ret));
 1224 }
 1225 
 1226 static int
 1227 acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate)
 1228 {
 1229     struct acpi_softc *sc;
 1230     ACPI_HANDLE handle;
 1231     ACPI_STATUS status;
 1232     char sxd[8];
 1233     int error;
 1234 
 1235     sc = device_get_softc(bus);
 1236     handle = acpi_get_handle(dev);
 1237 
 1238     /*
 1239      * XXX If we find these devices, don't try to power them down.
 1240      * The serial and IRDA ports on my T23 hang the system when
 1241      * set to D3 and it appears that such legacy devices may
 1242      * need special handling in their drivers.
 1243      */
 1244     if (handle == NULL ||
 1245         acpi_MatchHid(handle, "PNP0500") ||
 1246         acpi_MatchHid(handle, "PNP0501") ||
 1247         acpi_MatchHid(handle, "PNP0502") ||
 1248         acpi_MatchHid(handle, "PNP0510") ||
 1249         acpi_MatchHid(handle, "PNP0511"))
 1250         return (ENXIO);
 1251 
 1252     /*
 1253      * Override next state with the value from _SxD, if present.  If no
 1254      * dstate argument was provided, don't fetch the return value.
 1255      */
 1256     snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate);
 1257     if (dstate)
 1258         status = acpi_GetInteger(handle, sxd, dstate);
 1259     else
 1260         status = AcpiEvaluateObject(handle, sxd, NULL, NULL);
 1261 
 1262     switch (status) {
 1263     case AE_OK:
 1264         error = 0;
 1265         break;
 1266     case AE_NOT_FOUND:
 1267         error = ESRCH;
 1268         break;
 1269     default:
 1270         error = ENXIO;
 1271         break;
 1272     }
 1273 
 1274     return (error);
 1275 }
 1276 
 1277 /* Callback arg for our implementation of walking the namespace. */
 1278 struct acpi_device_scan_ctx {
 1279     acpi_scan_cb_t      user_fn;
 1280     void                *arg;
 1281     ACPI_HANDLE         parent;
 1282 };
 1283 
 1284 static ACPI_STATUS
 1285 acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval)
 1286 {
 1287     struct acpi_device_scan_ctx *ctx;
 1288     device_t dev, old_dev;
 1289     ACPI_STATUS status;
 1290     ACPI_OBJECT_TYPE type;
 1291 
 1292     /*
 1293      * Skip this device if we think we'll have trouble with it or it is
 1294      * the parent where the scan began.
 1295      */
 1296     ctx = (struct acpi_device_scan_ctx *)arg;
 1297     if (acpi_avoid(h) || h == ctx->parent)
 1298         return (AE_OK);
 1299 
 1300     /* If this is not a valid device type (e.g., a method), skip it. */
 1301     if (ACPI_FAILURE(AcpiGetType(h, &type)))
 1302         return (AE_OK);
 1303     if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR &&
 1304         type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER)
 1305         return (AE_OK);
 1306 
 1307     /*
 1308      * Call the user function with the current device.  If it is unchanged
 1309      * afterwards, return.  Otherwise, we update the handle to the new dev.
 1310      */
 1311     old_dev = acpi_get_device(h);
 1312     dev = old_dev;
 1313     status = ctx->user_fn(h, &dev, level, ctx->arg);
 1314     if (ACPI_FAILURE(status) || old_dev == dev)
 1315         return (status);
 1316 
 1317     /* Remove the old child and its connection to the handle. */
 1318     if (old_dev != NULL) {
 1319         device_delete_child(device_get_parent(old_dev), old_dev);
 1320         AcpiDetachData(h, acpi_fake_objhandler);
 1321     }
 1322 
 1323     /* Recreate the handle association if the user created a device. */
 1324     if (dev != NULL)
 1325         AcpiAttachData(h, acpi_fake_objhandler, dev);
 1326 
 1327     return (AE_OK);
 1328 }
 1329 
 1330 static ACPI_STATUS
 1331 acpi_device_scan_children(device_t bus, device_t dev, int max_depth,
 1332     acpi_scan_cb_t user_fn, void *arg)
 1333 {
 1334     ACPI_HANDLE h;
 1335     struct acpi_device_scan_ctx ctx;
 1336 
 1337     if (acpi_disabled("children"))
 1338         return (AE_OK);
 1339 
 1340     if (dev == NULL)
 1341         h = ACPI_ROOT_OBJECT;
 1342     else if ((h = acpi_get_handle(dev)) == NULL)
 1343         return (AE_BAD_PARAMETER);
 1344     ctx.user_fn = user_fn;
 1345     ctx.arg = arg;
 1346     ctx.parent = h;
 1347     return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth,
 1348         acpi_device_scan_cb, &ctx, NULL));
 1349 }
 1350 
 1351 /*
 1352  * Even though ACPI devices are not PCI, we use the PCI approach for setting
 1353  * device power states since it's close enough to ACPI.
 1354  */
 1355 static int
 1356 acpi_set_powerstate_method(device_t bus, device_t child, int state)
 1357 {
 1358     ACPI_HANDLE h;
 1359     ACPI_STATUS status;
 1360     int error;
 1361 
 1362     error = 0;
 1363     h = acpi_get_handle(child);
 1364     if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
 1365         return (EINVAL);
 1366     if (h == NULL)
 1367         return (0);
 1368 
 1369     /* Ignore errors if the power methods aren't present. */
 1370     status = acpi_pwr_switch_consumer(h, state);
 1371     if (ACPI_FAILURE(status) && status != AE_NOT_FOUND
 1372         && status != AE_BAD_PARAMETER)
 1373         device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n",
 1374             state, acpi_name(h), AcpiFormatException(status));
 1375 
 1376     return (error);
 1377 }
 1378 
 1379 static int
 1380 acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
 1381 {
 1382     int                 result, cid_count, i;
 1383     uint32_t            lid, cids[8];
 1384 
 1385     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1386 
 1387     /*
 1388      * ISA-style drivers attached to ACPI may persist and
 1389      * probe manually if we return ENOENT.  We never want
 1390      * that to happen, so don't ever return it.
 1391      */
 1392     result = ENXIO;
 1393 
 1394     /* Scan the supplied IDs for a match */
 1395     lid = acpi_isa_get_logicalid(child);
 1396     cid_count = acpi_isa_get_compatid(child, cids, 8);
 1397     while (ids && ids->ip_id) {
 1398         if (lid == ids->ip_id) {
 1399             result = 0;
 1400             goto out;
 1401         }
 1402         for (i = 0; i < cid_count; i++) {
 1403             if (cids[i] == ids->ip_id) {
 1404                 result = 0;
 1405                 goto out;
 1406             }
 1407         }
 1408         ids++;
 1409     }
 1410 
 1411  out:
 1412     if (result == 0 && ids->ip_desc)
 1413         device_set_desc(child, ids->ip_desc);
 1414 
 1415     return_VALUE (result);
 1416 }
 1417 
 1418 /*
 1419  * Scan all of the ACPI namespace and attach child devices.
 1420  *
 1421  * We should only expect to find devices in the \_PR, \_TZ, \_SI, and
 1422  * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec.
 1423  * However, in violation of the spec, some systems place their PCI link
 1424  * devices in \, so we have to walk the whole namespace.  We check the
 1425  * type of namespace nodes, so this should be ok.
 1426  */
 1427 static void
 1428 acpi_probe_children(device_t bus)
 1429 {
 1430 
 1431     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1432 
 1433     /*
 1434      * Scan the namespace and insert placeholders for all the devices that
 1435      * we find.  We also probe/attach any early devices.
 1436      *
 1437      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
 1438      * we want to create nodes for all devices, not just those that are
 1439      * currently present. (This assumes that we don't want to create/remove
 1440      * devices as they appear, which might be smarter.)
 1441      */
 1442     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
 1443     AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child,
 1444         bus, NULL);
 1445 
 1446     /* Pre-allocate resources for our rman from any sysresource devices. */
 1447     acpi_sysres_alloc(bus);
 1448 
 1449     /* Create any static children by calling device identify methods. */
 1450     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
 1451     bus_generic_probe(bus);
 1452 
 1453     /* Probe/attach all children, created staticly and from the namespace. */
 1454     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
 1455     bus_generic_attach(bus);
 1456 
 1457     /*
 1458      * Some of these children may have attached others as part of their attach
 1459      * process (eg. the root PCI bus driver), so rescan.
 1460      */
 1461     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
 1462     bus_generic_attach(bus);
 1463 
 1464     /* Attach wake sysctls. */
 1465     acpi_wake_sysctl_walk(bus);
 1466 
 1467     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
 1468     return_VOID;
 1469 }
 1470 
 1471 /*
 1472  * Determine the probe order for a given device and return non-zero if it
 1473  * should be attached immediately.
 1474  */
 1475 static int
 1476 acpi_probe_order(ACPI_HANDLE handle, int *order)
 1477 {
 1478     int ret;
 1479 
 1480     /*
 1481      * 1. I/O port and memory system resource holders
 1482      * 2. Embedded controllers (to handle early accesses)
 1483      */
 1484     ret = 0;
 1485     if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) {
 1486         *order = 1;
 1487         ret = 1;
 1488     } else if (acpi_MatchHid(handle, "PNP0C09")) {
 1489         *order = 2;
 1490         ret = 1;
 1491     }
 1492 
 1493     return (ret);
 1494 }
 1495 
 1496 /*
 1497  * Evaluate a child device and determine whether we might attach a device to
 1498  * it.
 1499  */
 1500 static ACPI_STATUS
 1501 acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
 1502 {
 1503     ACPI_OBJECT_TYPE type;
 1504     device_t bus, child;
 1505     int order, probe_now;
 1506     char *handle_str, **search;
 1507     static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL};
 1508 
 1509     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1510 
 1511     /* Skip this device if we think we'll have trouble with it. */
 1512     if (acpi_avoid(handle))
 1513         return_ACPI_STATUS (AE_OK);
 1514 
 1515     bus = (device_t)context;
 1516     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
 1517         switch (type) {
 1518         case ACPI_TYPE_DEVICE:
 1519         case ACPI_TYPE_PROCESSOR:
 1520         case ACPI_TYPE_THERMAL:
 1521         case ACPI_TYPE_POWER:
 1522             if (acpi_disabled("children"))
 1523                 break;
 1524 
 1525             /*
 1526              * Since we scan from \, be sure to skip system scope objects.
 1527              * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?)
 1528              */
 1529             handle_str = acpi_name(handle);
 1530             for (search = scopes; *search != NULL; search++) {
 1531                 if (strcmp(handle_str, *search) == 0)
 1532                     break;
 1533             }
 1534             if (*search != NULL)
 1535                 break;
 1536 
 1537             /* 
 1538              * Create a placeholder device for this node.  Sort the placeholder
 1539              * so that the probe/attach passes will run breadth-first.  Orders
 1540              * less than 10 are reserved for special objects (i.e., system
 1541              * resources).  Larger values are used for all other devices.
 1542              */
 1543             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str));
 1544             order = (level + 1) * 10;
 1545             probe_now = acpi_probe_order(handle, &order);
 1546             child = BUS_ADD_CHILD(bus, order, NULL, -1);
 1547             if (child == NULL)
 1548                 break;
 1549 
 1550             /* Associate the handle with the device_t and vice versa. */
 1551             acpi_set_handle(child, handle);
 1552             AcpiAttachData(handle, acpi_fake_objhandler, child);
 1553 
 1554             /*
 1555              * Check that the device is present.  If it's not present,
 1556              * leave it disabled (so that we have a device_t attached to
 1557              * the handle, but we don't probe it).
 1558              */
 1559             if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) {
 1560                 device_disable(child);
 1561                 break;
 1562             }
 1563 
 1564             /*
 1565              * Get the device's resource settings and attach them.
 1566              * Note that if the device has _PRS but no _CRS, we need
 1567              * to decide when it's appropriate to try to configure the
 1568              * device.  Ignore the return value here; it's OK for the
 1569              * device not to have any resources.
 1570              */
 1571             acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL);
 1572 
 1573             /* If order was overridden, probe/attach now rather than later. */
 1574             if (probe_now)
 1575                 device_probe_and_attach(child);
 1576             break;
 1577         }
 1578     }
 1579 
 1580     return_ACPI_STATUS (AE_OK);
 1581 }
 1582 
 1583 /*
 1584  * AcpiAttachData() requires an object handler but never uses it.  This is a
 1585  * placeholder object handler so we can store a device_t in an ACPI_HANDLE.
 1586  */
 1587 void
 1588 acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data)
 1589 {
 1590 }
 1591 
 1592 static void
 1593 acpi_shutdown_final(void *arg, int howto)
 1594 {
 1595     ACPI_STATUS status;
 1596 
 1597     /*
 1598      * XXX Shutdown code should only run on the BSP (cpuid 0).
 1599      * Some chipsets do not power off the system correctly if called from
 1600      * an AP.
 1601      */
 1602     if ((howto & RB_POWEROFF) != 0) {
 1603         status = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
 1604         if (ACPI_FAILURE(status)) {
 1605             printf("AcpiEnterSleepStatePrep failed - %s\n",
 1606                    AcpiFormatException(status));
 1607             return;
 1608         }
 1609         printf("Powering system off using ACPI\n");
 1610         ACPI_DISABLE_IRQS();
 1611         status = AcpiEnterSleepState(ACPI_STATE_S5);
 1612         if (ACPI_FAILURE(status)) {
 1613             printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
 1614         } else {
 1615             DELAY(1000000);
 1616             printf("ACPI power-off failed - timeout\n");
 1617         }
 1618     } else if (panicstr == NULL) {
 1619         printf("Shutting down ACPI\n");
 1620         AcpiTerminate();
 1621     }
 1622 }
 1623 
 1624 static void
 1625 acpi_enable_fixed_events(struct acpi_softc *sc)
 1626 {
 1627     static int  first_time = 1;
 1628 
 1629     /* Enable and clear fixed events and install handlers. */
 1630     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
 1631         AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
 1632         AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
 1633                                      acpi_event_power_button_sleep, sc);
 1634         if (first_time)
 1635             device_printf(sc->acpi_dev, "Power Button (fixed)\n");
 1636     }
 1637     if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
 1638         AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
 1639         AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
 1640                                      acpi_event_sleep_button_sleep, sc);
 1641         if (first_time)
 1642             device_printf(sc->acpi_dev, "Sleep Button (fixed)\n");
 1643     }
 1644 
 1645     first_time = 0;
 1646 }
 1647 
 1648 /*
 1649  * Returns true if the device is actually present and should
 1650  * be attached to.  This requires the present, enabled, UI-visible 
 1651  * and diagnostics-passed bits to be set.
 1652  */
 1653 BOOLEAN
 1654 acpi_DeviceIsPresent(device_t dev)
 1655 {
 1656     ACPI_DEVICE_INFO    *devinfo;
 1657     ACPI_HANDLE         h;
 1658     ACPI_BUFFER         buf;
 1659     ACPI_STATUS         error;
 1660     int                 ret;
 1661 
 1662     ret = FALSE;
 1663     if ((h = acpi_get_handle(dev)) == NULL)
 1664         return (FALSE);
 1665     buf.Pointer = NULL;
 1666     buf.Length = ACPI_ALLOCATE_BUFFER;
 1667     error = AcpiGetObjectInfo(h, &buf);
 1668     if (ACPI_FAILURE(error))
 1669         return (FALSE);
 1670     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
 1671 
 1672     /* If no _STA method, must be present */
 1673     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
 1674         ret = TRUE;
 1675 
 1676     /* Return true for 'present' and 'functioning' */
 1677     if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
 1678         ret = TRUE;
 1679 
 1680     AcpiOsFree(buf.Pointer);
 1681     return (ret);
 1682 }
 1683 
 1684 /*
 1685  * Returns true if the battery is actually present and inserted.
 1686  */
 1687 BOOLEAN
 1688 acpi_BatteryIsPresent(device_t dev)
 1689 {
 1690     ACPI_DEVICE_INFO    *devinfo;
 1691     ACPI_HANDLE         h;
 1692     ACPI_BUFFER         buf;
 1693     ACPI_STATUS         error;
 1694     int                 ret;
 1695 
 1696     ret = FALSE;
 1697     if ((h = acpi_get_handle(dev)) == NULL)
 1698         return (FALSE);
 1699     buf.Pointer = NULL;
 1700     buf.Length = ACPI_ALLOCATE_BUFFER;
 1701     error = AcpiGetObjectInfo(h, &buf);
 1702     if (ACPI_FAILURE(error))
 1703         return (FALSE);
 1704     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
 1705 
 1706     /* If no _STA method, must be present */
 1707     if ((devinfo->Valid & ACPI_VALID_STA) == 0)
 1708         ret = TRUE;
 1709 
 1710     /* Return true for 'present', 'battery present', and 'functioning' */
 1711     if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
 1712         ret = TRUE;
 1713 
 1714     AcpiOsFree(buf.Pointer);
 1715     return (ret);
 1716 }
 1717 
 1718 /*
 1719  * Match a HID string against a handle
 1720  */
 1721 static BOOLEAN
 1722 acpi_MatchHid(ACPI_HANDLE h, const char *hid) 
 1723 {
 1724     ACPI_DEVICE_INFO    *devinfo;
 1725     ACPI_BUFFER         buf;
 1726     ACPI_STATUS         error;
 1727     int                 ret, i;
 1728 
 1729     ret = FALSE;
 1730     if (hid == NULL || h == NULL)
 1731         return (ret);
 1732     buf.Pointer = NULL;
 1733     buf.Length = ACPI_ALLOCATE_BUFFER;
 1734     error = AcpiGetObjectInfo(h, &buf);
 1735     if (ACPI_FAILURE(error))
 1736         return (ret);
 1737     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
 1738 
 1739     if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
 1740         strcmp(hid, devinfo->HardwareId.Value) == 0)
 1741             ret = TRUE;
 1742     else if ((devinfo->Valid & ACPI_VALID_CID) != 0) {
 1743         for (i = 0; i < devinfo->CompatibilityId.Count; i++) {
 1744             if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) {
 1745                 ret = TRUE;
 1746                 break;
 1747             }
 1748         }
 1749     }
 1750 
 1751     AcpiOsFree(buf.Pointer);
 1752     return (ret);
 1753 }
 1754 
 1755 /*
 1756  * Return the handle of a named object within our scope, ie. that of (parent)
 1757  * or one if its parents.
 1758  */
 1759 ACPI_STATUS
 1760 acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
 1761 {
 1762     ACPI_HANDLE         r;
 1763     ACPI_STATUS         status;
 1764 
 1765     /* Walk back up the tree to the root */
 1766     for (;;) {
 1767         status = AcpiGetHandle(parent, path, &r);
 1768         if (ACPI_SUCCESS(status)) {
 1769             *result = r;
 1770             return (AE_OK);
 1771         }
 1772         /* XXX Return error here? */
 1773         if (status != AE_NOT_FOUND)
 1774             return (AE_OK);
 1775         if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
 1776             return (AE_NOT_FOUND);
 1777         parent = r;
 1778     }
 1779 }
 1780 
 1781 /* Find the difference between two PM tick counts. */
 1782 uint32_t
 1783 acpi_TimerDelta(uint32_t end, uint32_t start)
 1784 {
 1785     uint32_t delta;
 1786 
 1787     if (end >= start)
 1788         delta = end - start;
 1789     else if (AcpiGbl_FADT->TmrValExt == 0)
 1790         delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF;
 1791     else
 1792         delta = ((0xFFFFFFFF - start) + end + 1);
 1793     return (delta);
 1794 }
 1795 
 1796 /*
 1797  * Allocate a buffer with a preset data size.
 1798  */
 1799 ACPI_BUFFER *
 1800 acpi_AllocBuffer(int size)
 1801 {
 1802     ACPI_BUFFER *buf;
 1803 
 1804     if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL)
 1805         return (NULL);
 1806     buf->Length = size;
 1807     buf->Pointer = (void *)(buf + 1);
 1808     return (buf);
 1809 }
 1810 
 1811 ACPI_STATUS
 1812 acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number)
 1813 {
 1814     ACPI_OBJECT arg1;
 1815     ACPI_OBJECT_LIST args;
 1816 
 1817     arg1.Type = ACPI_TYPE_INTEGER;
 1818     arg1.Integer.Value = number;
 1819     args.Count = 1;
 1820     args.Pointer = &arg1;
 1821 
 1822     return (AcpiEvaluateObject(handle, path, &args, NULL));
 1823 }
 1824 
 1825 /*
 1826  * Evaluate a path that should return an integer.
 1827  */
 1828 ACPI_STATUS
 1829 acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number)
 1830 {
 1831     ACPI_STATUS status;
 1832     ACPI_BUFFER buf;
 1833     ACPI_OBJECT param;
 1834 
 1835     if (handle == NULL)
 1836         handle = ACPI_ROOT_OBJECT;
 1837 
 1838     /*
 1839      * Assume that what we've been pointed at is an Integer object, or
 1840      * a method that will return an Integer.
 1841      */
 1842     buf.Pointer = &param;
 1843     buf.Length = sizeof(param);
 1844     status = AcpiEvaluateObject(handle, path, NULL, &buf);
 1845     if (ACPI_SUCCESS(status)) {
 1846         if (param.Type == ACPI_TYPE_INTEGER)
 1847             *number = param.Integer.Value;
 1848         else
 1849             status = AE_TYPE;
 1850     }
 1851 
 1852     /* 
 1853      * In some applications, a method that's expected to return an Integer
 1854      * may instead return a Buffer (probably to simplify some internal
 1855      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
 1856      * convert it into an Integer as best we can.
 1857      *
 1858      * This is a hack.
 1859      */
 1860     if (status == AE_BUFFER_OVERFLOW) {
 1861         if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
 1862             status = AE_NO_MEMORY;
 1863         } else {
 1864             status = AcpiEvaluateObject(handle, path, NULL, &buf);
 1865             if (ACPI_SUCCESS(status))
 1866                 status = acpi_ConvertBufferToInteger(&buf, number);
 1867             AcpiOsFree(buf.Pointer);
 1868         }
 1869     }
 1870     return (status);
 1871 }
 1872 
 1873 ACPI_STATUS
 1874 acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number)
 1875 {
 1876     ACPI_OBJECT *p;
 1877     UINT8       *val;
 1878     int         i;
 1879 
 1880     p = (ACPI_OBJECT *)bufp->Pointer;
 1881     if (p->Type == ACPI_TYPE_INTEGER) {
 1882         *number = p->Integer.Value;
 1883         return (AE_OK);
 1884     }
 1885     if (p->Type != ACPI_TYPE_BUFFER)
 1886         return (AE_TYPE);
 1887     if (p->Buffer.Length > sizeof(int))
 1888         return (AE_BAD_DATA);
 1889 
 1890     *number = 0;
 1891     val = p->Buffer.Pointer;
 1892     for (i = 0; i < p->Buffer.Length; i++)
 1893         *number += val[i] << (i * 8);
 1894     return (AE_OK);
 1895 }
 1896 
 1897 /*
 1898  * Iterate over the elements of an a package object, calling the supplied
 1899  * function for each element.
 1900  *
 1901  * XXX possible enhancement might be to abort traversal on error.
 1902  */
 1903 ACPI_STATUS
 1904 acpi_ForeachPackageObject(ACPI_OBJECT *pkg,
 1905         void (*func)(ACPI_OBJECT *comp, void *arg), void *arg)
 1906 {
 1907     ACPI_OBJECT *comp;
 1908     int         i;
 1909 
 1910     if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
 1911         return (AE_BAD_PARAMETER);
 1912 
 1913     /* Iterate over components */
 1914     i = 0;
 1915     comp = pkg->Package.Elements;
 1916     for (; i < pkg->Package.Count; i++, comp++)
 1917         func(comp, arg);
 1918 
 1919     return (AE_OK);
 1920 }
 1921 
 1922 /*
 1923  * Find the (index)th resource object in a set.
 1924  */
 1925 ACPI_STATUS
 1926 acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
 1927 {
 1928     ACPI_RESOURCE       *rp;
 1929     int                 i;
 1930 
 1931     rp = (ACPI_RESOURCE *)buf->Pointer;
 1932     i = index;
 1933     while (i-- > 0) {
 1934         /* Range check */
 1935         if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
 1936             return (AE_BAD_PARAMETER);
 1937 
 1938         /* Check for terminator */
 1939         if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
 1940             return (AE_NOT_FOUND);
 1941         rp = ACPI_NEXT_RESOURCE(rp);
 1942     }
 1943     if (resp != NULL)
 1944         *resp = rp;
 1945 
 1946     return (AE_OK);
 1947 }
 1948 
 1949 /*
 1950  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
 1951  *
 1952  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
 1953  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
 1954  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
 1955  * resources.
 1956  */
 1957 #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE       512
 1958 
 1959 ACPI_STATUS
 1960 acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
 1961 {
 1962     ACPI_RESOURCE       *rp;
 1963     void                *newp;
 1964 
 1965     /* Initialise the buffer if necessary. */
 1966     if (buf->Pointer == NULL) {
 1967         buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
 1968         if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
 1969             return (AE_NO_MEMORY);
 1970         rp = (ACPI_RESOURCE *)buf->Pointer;
 1971         rp->Id = ACPI_RSTYPE_END_TAG;
 1972         rp->Length = 0;
 1973     }
 1974     if (res == NULL)
 1975         return (AE_OK);
 1976 
 1977     /*
 1978      * Scan the current buffer looking for the terminator.
 1979      * This will either find the terminator or hit the end
 1980      * of the buffer and return an error.
 1981      */
 1982     rp = (ACPI_RESOURCE *)buf->Pointer;
 1983     for (;;) {
 1984         /* Range check, don't go outside the buffer */
 1985         if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
 1986             return (AE_BAD_PARAMETER);
 1987         if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0)
 1988             break;
 1989         rp = ACPI_NEXT_RESOURCE(rp);
 1990     }
 1991 
 1992     /*
 1993      * Check the size of the buffer and expand if required.
 1994      *
 1995      * Required size is:
 1996      *  size of existing resources before terminator + 
 1997      *  size of new resource and header +
 1998      *  size of terminator.
 1999      *
 2000      * Note that this loop should really only run once, unless
 2001      * for some reason we are stuffing a *really* huge resource.
 2002      */
 2003     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 
 2004             res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
 2005             ACPI_RESOURCE_LENGTH) >= buf->Length) {
 2006         if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
 2007             return (AE_NO_MEMORY);
 2008         bcopy(buf->Pointer, newp, buf->Length);
 2009         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
 2010                                ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
 2011         AcpiOsFree(buf->Pointer);
 2012         buf->Pointer = newp;
 2013         buf->Length += buf->Length;
 2014     }
 2015 
 2016     /* Insert the new resource. */
 2017     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
 2018 
 2019     /* And add the terminator. */
 2020     rp = ACPI_NEXT_RESOURCE(rp);
 2021     rp->Id = ACPI_RSTYPE_END_TAG;
 2022     rp->Length = 0;
 2023 
 2024     return (AE_OK);
 2025 }
 2026 
 2027 /*
 2028  * Set interrupt model.
 2029  */
 2030 ACPI_STATUS
 2031 acpi_SetIntrModel(int model)
 2032 {
 2033 
 2034     return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model));
 2035 }
 2036 
 2037 static void
 2038 acpi_sleep_enable(void *arg)
 2039 {
 2040 
 2041     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
 2042 }
 2043 
 2044 enum acpi_sleep_state {
 2045     ACPI_SS_NONE,
 2046     ACPI_SS_GPE_SET,
 2047     ACPI_SS_DEV_SUSPEND,
 2048     ACPI_SS_SLP_PREP,
 2049     ACPI_SS_SLEPT,
 2050 };
 2051 
 2052 /*
 2053  * Set the system sleep state
 2054  *
 2055  * Currently we support S1-S5 but S4 is only S4BIOS
 2056  */
 2057 ACPI_STATUS
 2058 acpi_SetSleepState(struct acpi_softc *sc, int state)
 2059 {
 2060     ACPI_STATUS status;
 2061     UINT8       TypeA;
 2062     UINT8       TypeB;
 2063     enum acpi_sleep_state slp_state;
 2064 
 2065     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 2066 
 2067     status = AE_OK;
 2068     ACPI_LOCK(acpi);
 2069     if (sc->acpi_sleep_disabled) {
 2070         if (sc->acpi_sstate != ACPI_STATE_S0)
 2071             status = AE_ERROR;
 2072         ACPI_UNLOCK(acpi);
 2073         printf("acpi: suspend request ignored (not ready yet)\n");
 2074         return (status);
 2075     }
 2076     sc->acpi_sleep_disabled = 1;
 2077     ACPI_UNLOCK(acpi);
 2078 
 2079     /*
 2080      * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
 2081      * drivers need this.
 2082      */
 2083     mtx_lock(&Giant);
 2084     slp_state = ACPI_SS_NONE;
 2085     switch (state) {
 2086     case ACPI_STATE_S1:
 2087     case ACPI_STATE_S2:
 2088     case ACPI_STATE_S3:
 2089     case ACPI_STATE_S4:
 2090         status = AcpiGetSleepTypeData(state, &TypeA, &TypeB);
 2091         if (status == AE_NOT_FOUND) {
 2092             device_printf(sc->acpi_dev,
 2093                           "Sleep state S%d not supported by BIOS\n", state);
 2094             break;
 2095         } else if (ACPI_FAILURE(status)) {
 2096             device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n",
 2097                           AcpiFormatException(status));
 2098             break;
 2099         }
 2100 
 2101         sc->acpi_sstate = state;
 2102 
 2103         /* Enable any GPEs as appropriate and requested by the user. */
 2104         acpi_wake_prep_walk(state);
 2105         slp_state = ACPI_SS_GPE_SET;
 2106 
 2107         /*
 2108          * Inform all devices that we are going to sleep.  If at least one
 2109          * device fails, DEVICE_SUSPEND() automatically resumes the tree.
 2110          *
 2111          * XXX Note that a better two-pass approach with a 'veto' pass
 2112          * followed by a "real thing" pass would be better, but the current
 2113          * bus interface does not provide for this.
 2114          */
 2115         if (DEVICE_SUSPEND(root_bus) != 0) {
 2116             device_printf(sc->acpi_dev, "device_suspend failed\n");
 2117             break;
 2118         }
 2119         slp_state = ACPI_SS_DEV_SUSPEND;
 2120 
 2121         status = AcpiEnterSleepStatePrep(state);
 2122         if (ACPI_FAILURE(status)) {
 2123             device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
 2124                           AcpiFormatException(status));
 2125             break;
 2126         }
 2127         slp_state = ACPI_SS_SLP_PREP;
 2128 
 2129         if (sc->acpi_sleep_delay > 0)
 2130             DELAY(sc->acpi_sleep_delay * 1000000);
 2131 
 2132         if (state != ACPI_STATE_S1) {
 2133             acpi_sleep_machdep(sc, state);
 2134 
 2135             /* Re-enable ACPI hardware on wakeup from sleep state 4. */
 2136             if (state == ACPI_STATE_S4)
 2137                 AcpiEnable();
 2138         } else {
 2139             ACPI_DISABLE_IRQS();
 2140             status = AcpiEnterSleepState(state);
 2141             if (ACPI_FAILURE(status)) {
 2142                 device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n",
 2143                               AcpiFormatException(status));
 2144                 break;
 2145             }
 2146         }
 2147         slp_state = ACPI_SS_SLEPT;
 2148         break;
 2149     case ACPI_STATE_S5:
 2150         /*
 2151          * Shut down cleanly and power off.  This will call us back through the
 2152          * shutdown handlers.
 2153          */
 2154         shutdown_nice(RB_POWEROFF);
 2155         break;
 2156     case ACPI_STATE_S0:
 2157     default:
 2158         status = AE_BAD_PARAMETER;
 2159         break;
 2160     }
 2161 
 2162     /*
 2163      * Back out state according to how far along we got in the suspend
 2164      * process.  This handles both the error and success cases.
 2165      */
 2166     if (slp_state >= ACPI_SS_GPE_SET) {
 2167         acpi_wake_prep_walk(state);
 2168         sc->acpi_sstate = ACPI_STATE_S0;
 2169     }
 2170     if (slp_state >= ACPI_SS_SLP_PREP)
 2171         AcpiLeaveSleepState(state);
 2172     if (slp_state >= ACPI_SS_DEV_SUSPEND)
 2173         DEVICE_RESUME(root_bus);
 2174     if (slp_state >= ACPI_SS_SLEPT)
 2175         acpi_enable_fixed_events(sc);
 2176 
 2177     /* Allow another sleep request after a while. */
 2178     if (state != ACPI_STATE_S5)
 2179         timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
 2180 
 2181     mtx_unlock(&Giant);
 2182     return_ACPI_STATUS (status);
 2183 }
 2184 
 2185 /* Initialize a device's wake GPE. */
 2186 int
 2187 acpi_wake_init(device_t dev, int type)
 2188 {
 2189     struct acpi_prw_data prw;
 2190 
 2191     /* Evaluate _PRW to find the GPE. */
 2192     if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0)
 2193         return (ENXIO);
 2194 
 2195     /* Set the requested type for the GPE (runtime, wake, or both). */
 2196     if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) {
 2197         device_printf(dev, "set GPE type failed\n");
 2198         return (ENXIO);
 2199     }
 2200 
 2201     return (0);
 2202 }
 2203 
 2204 /* Enable or disable the device's wake GPE. */
 2205 int
 2206 acpi_wake_set_enable(device_t dev, int enable)
 2207 {
 2208     struct acpi_prw_data prw;
 2209     ACPI_HANDLE handle;
 2210     ACPI_STATUS status;
 2211     int flags;
 2212 
 2213     /* Make sure the device supports waking the system and get the GPE. */
 2214     handle = acpi_get_handle(dev);
 2215     if (acpi_parse_prw(handle, &prw) != 0)
 2216         return (ENXIO);
 2217 
 2218     flags = acpi_get_flags(dev);
 2219     if (enable) {
 2220         status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
 2221         if (ACPI_FAILURE(status)) {
 2222             device_printf(dev, "enable wake failed\n");
 2223             return (ENXIO);
 2224         }
 2225         acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED);
 2226     } else {
 2227         status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
 2228         if (ACPI_FAILURE(status)) {
 2229             device_printf(dev, "disable wake failed\n");
 2230             return (ENXIO);
 2231         }
 2232         acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED);
 2233     }
 2234 
 2235     return (0);
 2236 }
 2237 
 2238 static int
 2239 acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate)
 2240 {
 2241     struct acpi_prw_data prw;
 2242     device_t dev;
 2243 
 2244     /* Check that this is a wake-capable device and get its GPE. */
 2245     if (acpi_parse_prw(handle, &prw) != 0)
 2246         return (ENXIO);
 2247     dev = acpi_get_device(handle);
 2248 
 2249     /*
 2250      * The destination sleep state must be less than (i.e., higher power)
 2251      * or equal to the value specified by _PRW.  If this GPE cannot be
 2252      * enabled for the next sleep state, then disable it.  If it can and
 2253      * the user requested it be enabled, turn on any required power resources
 2254      * and set _PSW.
 2255      */
 2256     if (sstate > prw.lowest_wake) {
 2257         AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
 2258         if (bootverbose)
 2259             device_printf(dev, "wake_prep disabled wake for %s (S%d)\n",
 2260                 acpi_name(handle), sstate);
 2261     } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) {
 2262         acpi_pwr_wake_enable(handle, 1);
 2263         acpi_SetInteger(handle, "_PSW", 1);
 2264         if (bootverbose)
 2265             device_printf(dev, "wake_prep enabled for %s (S%d)\n",
 2266                 acpi_name(handle), sstate);
 2267     }
 2268 
 2269     return (0);
 2270 }
 2271 
 2272 static int
 2273 acpi_wake_run_prep(ACPI_HANDLE handle, int sstate)
 2274 {
 2275     struct acpi_prw_data prw;
 2276     device_t dev;
 2277 
 2278     /*
 2279      * Check that this is a wake-capable device and get its GPE.  Return
 2280      * now if the user didn't enable this device for wake.
 2281      */
 2282     if (acpi_parse_prw(handle, &prw) != 0)
 2283         return (ENXIO);
 2284     dev = acpi_get_device(handle);
 2285     if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0)
 2286         return (0);
 2287 
 2288     /*
 2289      * If this GPE couldn't be enabled for the previous sleep state, it was
 2290      * disabled before going to sleep so re-enable it.  If it was enabled,
 2291      * clear _PSW and turn off any power resources it used.
 2292      */
 2293     if (sstate > prw.lowest_wake) {
 2294         AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR);
 2295         if (bootverbose)
 2296             device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle));
 2297     } else {
 2298         acpi_SetInteger(handle, "_PSW", 0);
 2299         acpi_pwr_wake_enable(handle, 0);
 2300         if (bootverbose)
 2301             device_printf(dev, "run_prep cleaned up for %s\n",
 2302                 acpi_name(handle));
 2303     }
 2304 
 2305     return (0);
 2306 }
 2307 
 2308 static ACPI_STATUS
 2309 acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
 2310 {
 2311     int sstate;
 2312 
 2313     /* If suspending, run the sleep prep function, otherwise wake. */
 2314     sstate = *(int *)context;
 2315     if (AcpiGbl_SystemAwakeAndRunning)
 2316         acpi_wake_sleep_prep(handle, sstate);
 2317     else
 2318         acpi_wake_run_prep(handle, sstate);
 2319     return (AE_OK);
 2320 }
 2321 
 2322 /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */
 2323 static int
 2324 acpi_wake_prep_walk(int sstate)
 2325 {
 2326     ACPI_HANDLE sb_handle;
 2327 
 2328     if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle)))
 2329         AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100,
 2330             acpi_wake_prep, &sstate, NULL);
 2331     return (0);
 2332 }
 2333 
 2334 /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */
 2335 static int
 2336 acpi_wake_sysctl_walk(device_t dev)
 2337 {
 2338     int error, i, numdevs;
 2339     device_t *devlist;
 2340     device_t child;
 2341     ACPI_STATUS status;
 2342 
 2343     error = device_get_children(dev, &devlist, &numdevs);
 2344     if (error != 0 || numdevs == 0)
 2345         return (error);
 2346     for (i = 0; i < numdevs; i++) {
 2347         child = devlist[i];
 2348         acpi_wake_sysctl_walk(child);
 2349         if (!device_is_attached(child))
 2350             continue;
 2351         status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL);
 2352         if (ACPI_SUCCESS(status)) {
 2353             SYSCTL_ADD_PROC(device_get_sysctl_ctx(child),
 2354                 SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO,
 2355                 "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0,
 2356                 acpi_wake_set_sysctl, "I", "Device set to wake the system");
 2357         }
 2358     }
 2359     free(devlist, M_TEMP);
 2360 
 2361     return (0);
 2362 }
 2363 
 2364 /* Enable or disable wake from userland. */
 2365 static int
 2366 acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS)
 2367 {
 2368     int enable, error;
 2369     device_t dev;
 2370 
 2371     dev = (device_t)arg1;
 2372     enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0;
 2373 
 2374     error = sysctl_handle_int(oidp, &enable, 0, req);
 2375     if (error != 0 || req->newptr == NULL)
 2376         return (error);
 2377     if (enable != 0 && enable != 1)
 2378         return (EINVAL);
 2379 
 2380     return (acpi_wake_set_enable(dev, enable));
 2381 }
 2382 
 2383 /* Parse a device's _PRW into a structure. */
 2384 int
 2385 acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw)
 2386 {
 2387     ACPI_STATUS                 status;
 2388     ACPI_BUFFER                 prw_buffer;
 2389     ACPI_OBJECT                 *res, *res2;
 2390     int                         error, i, power_count;
 2391 
 2392     if (h == NULL || prw == NULL)
 2393         return (EINVAL);
 2394 
 2395     /*
 2396      * The _PRW object (7.2.9) is only required for devices that have the
 2397      * ability to wake the system from a sleeping state.
 2398      */
 2399     error = EINVAL;
 2400     prw_buffer.Pointer = NULL;
 2401     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
 2402     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
 2403     if (ACPI_FAILURE(status))
 2404         return (ENOENT);
 2405     res = (ACPI_OBJECT *)prw_buffer.Pointer;
 2406     if (res == NULL)
 2407         return (ENOENT);
 2408     if (!ACPI_PKG_VALID(res, 2))
 2409         goto out;
 2410 
 2411     /*
 2412      * Element 1 of the _PRW object:
 2413      * The lowest power system sleeping state that can be entered while still
 2414      * providing wake functionality.  The sleeping state being entered must
 2415      * be less than (i.e., higher power) or equal to this value.
 2416      */
 2417     if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0)
 2418         goto out;
 2419 
 2420     /*
 2421      * Element 0 of the _PRW object:
 2422      */
 2423     switch (res->Package.Elements[0].Type) {
 2424     case ACPI_TYPE_INTEGER:
 2425         /*
 2426          * If the data type of this package element is numeric, then this
 2427          * _PRW package element is the bit index in the GPEx_EN, in the
 2428          * GPE blocks described in the FADT, of the enable bit that is
 2429          * enabled for the wake event.
 2430          */
 2431         prw->gpe_handle = NULL;
 2432         prw->gpe_bit = res->Package.Elements[0].Integer.Value;
 2433         error = 0;
 2434         break;
 2435     case ACPI_TYPE_PACKAGE:
 2436         /*
 2437          * If the data type of this package element is a package, then this
 2438          * _PRW package element is itself a package containing two
 2439          * elements.  The first is an object reference to the GPE Block
 2440          * device that contains the GPE that will be triggered by the wake
 2441          * event.  The second element is numeric and it contains the bit
 2442          * index in the GPEx_EN, in the GPE Block referenced by the
 2443          * first element in the package, of the enable bit that is enabled for
 2444          * the wake event.
 2445          *
 2446          * For example, if this field is a package then it is of the form:
 2447          * Package() {\_SB.PCI0.ISA.GPE, 2}
 2448          */
 2449         res2 = &res->Package.Elements[0];
 2450         if (!ACPI_PKG_VALID(res2, 2))
 2451             goto out;
 2452         prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]);
 2453         if (prw->gpe_handle == NULL)
 2454             goto out;
 2455         if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0)
 2456             goto out;
 2457         error = 0;
 2458         break;
 2459     default:
 2460         goto out;
 2461     }
 2462 
 2463     /* Elements 2 to N of the _PRW object are power resources. */
 2464     power_count = res->Package.Count - 2;
 2465     if (power_count > ACPI_PRW_MAX_POWERRES) {
 2466         printf("ACPI device %s has too many power resources\n", acpi_name(h));
 2467         power_count = 0;
 2468     }
 2469     prw->power_res_count = power_count;
 2470     for (i = 0; i < power_count; i++)
 2471         prw->power_res[i] = res->Package.Elements[i];
 2472 
 2473 out:
 2474     if (prw_buffer.Pointer != NULL)
 2475         AcpiOsFree(prw_buffer.Pointer);
 2476     return (error);
 2477 }
 2478 
 2479 /*
 2480  * ACPI Event Handlers
 2481  */
 2482 
 2483 /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
 2484 
 2485 static void
 2486 acpi_system_eventhandler_sleep(void *arg, int state)
 2487 {
 2488 
 2489     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 2490 
 2491     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
 2492         acpi_SetSleepState((struct acpi_softc *)arg, state);
 2493 
 2494     return_VOID;
 2495 }
 2496 
 2497 static void
 2498 acpi_system_eventhandler_wakeup(void *arg, int state)
 2499 {
 2500 
 2501     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 2502 
 2503     /* Currently, nothing to do for wakeup. */
 2504 
 2505     return_VOID;
 2506 }
 2507 
 2508 /* 
 2509  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
 2510  */
 2511 UINT32
 2512 acpi_event_power_button_sleep(void *context)
 2513 {
 2514     struct acpi_softc   *sc = (struct acpi_softc *)context;
 2515 
 2516     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 2517 
 2518     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
 2519 
 2520     return_VALUE (ACPI_INTERRUPT_HANDLED);
 2521 }
 2522 
 2523 UINT32
 2524 acpi_event_power_button_wake(void *context)
 2525 {
 2526     struct acpi_softc   *sc = (struct acpi_softc *)context;
 2527 
 2528     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 2529 
 2530     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
 2531 
 2532     return_VALUE (ACPI_INTERRUPT_HANDLED);
 2533 }
 2534 
 2535 UINT32
 2536 acpi_event_sleep_button_sleep(void *context)
 2537 {
 2538     struct acpi_softc   *sc = (struct acpi_softc *)context;
 2539 
 2540     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 2541 
 2542     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
 2543 
 2544     return_VALUE (ACPI_INTERRUPT_HANDLED);
 2545 }
 2546 
 2547 UINT32
 2548 acpi_event_sleep_button_wake(void *context)
 2549 {
 2550     struct acpi_softc   *sc = (struct acpi_softc *)context;
 2551 
 2552     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 2553 
 2554     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
 2555 
 2556     return_VALUE (ACPI_INTERRUPT_HANDLED);
 2557 }
 2558 
 2559 /*
 2560  * XXX This static buffer is suboptimal.  There is no locking so only
 2561  * use this for single-threaded callers.
 2562  */
 2563 char *
 2564 acpi_name(ACPI_HANDLE handle)
 2565 {
 2566     ACPI_BUFFER buf;
 2567     static char data[256];
 2568 
 2569     buf.Length = sizeof(data);
 2570     buf.Pointer = data;
 2571 
 2572     if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf)))
 2573         return (data);
 2574     return ("(unknown)");
 2575 }
 2576 
 2577 /*
 2578  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
 2579  * parts of the namespace.
 2580  */
 2581 int
 2582 acpi_avoid(ACPI_HANDLE handle)
 2583 {
 2584     char        *cp, *env, *np;
 2585     int         len;
 2586 
 2587     np = acpi_name(handle);
 2588     if (*np == '\\')
 2589         np++;
 2590     if ((env = getenv("debug.acpi.avoid")) == NULL)
 2591         return (0);
 2592 
 2593     /* Scan the avoid list checking for a match */
 2594     cp = env;
 2595     for (;;) {
 2596         while (*cp != 0 && isspace(*cp))
 2597             cp++;
 2598         if (*cp == 0)
 2599             break;
 2600         len = 0;
 2601         while (cp[len] != 0 && !isspace(cp[len]))
 2602             len++;
 2603         if (!strncmp(cp, np, len)) {
 2604             freeenv(env);
 2605             return(1);
 2606         }
 2607         cp += len;
 2608     }
 2609     freeenv(env);
 2610 
 2611     return (0);
 2612 }
 2613 
 2614 /*
 2615  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
 2616  */
 2617 int
 2618 acpi_disabled(char *subsys)
 2619 {
 2620     char        *cp, *env;
 2621     int         len;
 2622 
 2623     if ((env = getenv("debug.acpi.disabled")) == NULL)
 2624         return (0);
 2625     if (strcmp(env, "all") == 0) {
 2626         freeenv(env);
 2627         return (1);
 2628     }
 2629 
 2630     /* Scan the disable list, checking for a match. */
 2631     cp = env;
 2632     for (;;) {
 2633         while (*cp != '\0' && isspace(*cp))
 2634             cp++;
 2635         if (*cp == '\0')
 2636             break;
 2637         len = 0;
 2638         while (cp[len] != '\0' && !isspace(cp[len]))
 2639             len++;
 2640         if (strncmp(cp, subsys, len) == 0) {
 2641             freeenv(env);
 2642             return (1);
 2643         }
 2644         cp += len;
 2645     }
 2646     freeenv(env);
 2647 
 2648     return (0);
 2649 }
 2650 
 2651 /*
 2652  * Control interface.
 2653  *
 2654  * We multiplex ioctls for all participating ACPI devices here.  Individual 
 2655  * drivers wanting to be accessible via /dev/acpi should use the
 2656  * register/deregister interface to make their handlers visible.
 2657  */
 2658 struct acpi_ioctl_hook
 2659 {
 2660     TAILQ_ENTRY(acpi_ioctl_hook) link;
 2661     u_long                       cmd;
 2662     acpi_ioctl_fn                fn;
 2663     void                         *arg;
 2664 };
 2665 
 2666 static TAILQ_HEAD(,acpi_ioctl_hook)     acpi_ioctl_hooks;
 2667 static int                              acpi_ioctl_hooks_initted;
 2668 
 2669 int
 2670 acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg)
 2671 {
 2672     struct acpi_ioctl_hook      *hp;
 2673 
 2674     if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL)
 2675         return (ENOMEM);
 2676     hp->cmd = cmd;
 2677     hp->fn = fn;
 2678     hp->arg = arg;
 2679 
 2680     ACPI_LOCK(acpi);
 2681     if (acpi_ioctl_hooks_initted == 0) {
 2682         TAILQ_INIT(&acpi_ioctl_hooks);
 2683         acpi_ioctl_hooks_initted = 1;
 2684     }
 2685     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
 2686     ACPI_UNLOCK(acpi);
 2687 
 2688     return (0);
 2689 }
 2690 
 2691 void
 2692 acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn)
 2693 {
 2694     struct acpi_ioctl_hook      *hp;
 2695 
 2696     ACPI_LOCK(acpi);
 2697     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
 2698         if (hp->cmd == cmd && hp->fn == fn)
 2699             break;
 2700 
 2701     if (hp != NULL) {
 2702         TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
 2703         free(hp, M_ACPIDEV);
 2704     }
 2705     ACPI_UNLOCK(acpi);
 2706 }
 2707 
 2708 static int
 2709 acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td)
 2710 {
 2711     return (0);
 2712 }
 2713 
 2714 static int
 2715 acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td)
 2716 {
 2717     return (0);
 2718 }
 2719 
 2720 static int
 2721 acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
 2722 {
 2723     struct acpi_softc           *sc;
 2724     struct acpi_ioctl_hook      *hp;
 2725     int                         error, state;
 2726 
 2727     error = 0;
 2728     hp = NULL;
 2729     sc = dev->si_drv1;
 2730 
 2731     /*
 2732      * Scan the list of registered ioctls, looking for handlers.
 2733      */
 2734     ACPI_LOCK(acpi);
 2735     if (acpi_ioctl_hooks_initted)
 2736         TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
 2737             if (hp->cmd == cmd)
 2738                 break;
 2739         }
 2740     ACPI_UNLOCK(acpi);
 2741     if (hp)
 2742         return (hp->fn(cmd, addr, hp->arg));
 2743 
 2744     /*
 2745      * Core ioctls are not permitted for non-writable user.
 2746      * Currently, other ioctls just fetch information.
 2747      * Not changing system behavior.
 2748      */
 2749     if ((flag & FWRITE) == 0)
 2750         return (EPERM);
 2751 
 2752     /* Core system ioctls. */
 2753     switch (cmd) {
 2754     case ACPIIO_SETSLPSTATE:
 2755         error = EINVAL;
 2756         state = *(int *)addr;
 2757         if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
 2758             if (ACPI_SUCCESS(acpi_SetSleepState(sc, state)))
 2759                 error = 0;
 2760         break;
 2761     default:
 2762         error = ENXIO;
 2763         break;
 2764     }
 2765 
 2766     return (error);
 2767 }
 2768 
 2769 static int
 2770 acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
 2771 {
 2772     int error;
 2773     struct sbuf sb;
 2774     UINT8 state, TypeA, TypeB;
 2775 
 2776     sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND);
 2777     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++)
 2778         if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB)))
 2779             sbuf_printf(&sb, "S%d ", state);
 2780     sbuf_trim(&sb);
 2781     sbuf_finish(&sb);
 2782     error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
 2783     sbuf_delete(&sb);
 2784     return (error);
 2785 }
 2786 
 2787 static int
 2788 acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
 2789 {
 2790     char sleep_state[10];
 2791     int error;
 2792     u_int new_state, old_state;
 2793 
 2794     old_state = *(u_int *)oidp->oid_arg1;
 2795     if (old_state > ACPI_S_STATES_MAX + 1)
 2796         strlcpy(sleep_state, "unknown", sizeof(sleep_state));
 2797     else
 2798         strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state));
 2799     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
 2800     if (error == 0 && req->newptr != NULL) {
 2801         new_state = ACPI_STATE_S0;
 2802         for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++)
 2803             if (strcmp(sleep_state, sleep_state_names[new_state]) == 0)
 2804                 break;
 2805         if (new_state <= ACPI_S_STATES_MAX + 1) {
 2806             if (new_state != old_state)
 2807                 *(u_int *)oidp->oid_arg1 = new_state;
 2808         } else
 2809             error = EINVAL;
 2810     }
 2811 
 2812     return (error);
 2813 }
 2814 
 2815 /* Inform devctl(4) when we receive a Notify. */
 2816 void
 2817 acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify)
 2818 {
 2819     char                notify_buf[16];
 2820     ACPI_BUFFER         handle_buf;
 2821     ACPI_STATUS         status;
 2822 
 2823     if (subsystem == NULL)
 2824         return;
 2825 
 2826     handle_buf.Pointer = NULL;
 2827     handle_buf.Length = ACPI_ALLOCATE_BUFFER;
 2828     status = AcpiNsHandleToPathname(h, &handle_buf);
 2829     if (ACPI_FAILURE(status))
 2830         return;
 2831     snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify);
 2832     devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf);
 2833     AcpiOsFree(handle_buf.Pointer);
 2834 }
 2835 
 2836 #ifdef ACPI_DEBUG
 2837 /*
 2838  * Support for parsing debug options from the kernel environment.
 2839  *
 2840  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
 2841  * by specifying the names of the bits in the debug.acpi.layer and
 2842  * debug.acpi.level environment variables.  Bits may be unset by 
 2843  * prefixing the bit name with !.
 2844  */
 2845 struct debugtag
 2846 {
 2847     char        *name;
 2848     UINT32      value;
 2849 };
 2850 
 2851 static struct debugtag  dbg_layer[] = {
 2852     {"ACPI_UTILITIES",          ACPI_UTILITIES},
 2853     {"ACPI_HARDWARE",           ACPI_HARDWARE},
 2854     {"ACPI_EVENTS",             ACPI_EVENTS},
 2855     {"ACPI_TABLES",             ACPI_TABLES},
 2856     {"ACPI_NAMESPACE",          ACPI_NAMESPACE},
 2857     {"ACPI_PARSER",             ACPI_PARSER},
 2858     {"ACPI_DISPATCHER",         ACPI_DISPATCHER},
 2859     {"ACPI_EXECUTER",           ACPI_EXECUTER},
 2860     {"ACPI_RESOURCES",          ACPI_RESOURCES},
 2861     {"ACPI_CA_DEBUGGER",        ACPI_CA_DEBUGGER},
 2862     {"ACPI_OS_SERVICES",        ACPI_OS_SERVICES},
 2863     {"ACPI_CA_DISASSEMBLER",    ACPI_CA_DISASSEMBLER},
 2864     {"ACPI_ALL_COMPONENTS",     ACPI_ALL_COMPONENTS},
 2865 
 2866     {"ACPI_AC_ADAPTER",         ACPI_AC_ADAPTER},
 2867     {"ACPI_BATTERY",            ACPI_BATTERY},
 2868     {"ACPI_BUS",                ACPI_BUS},
 2869     {"ACPI_BUTTON",             ACPI_BUTTON},
 2870     {"ACPI_EC",                 ACPI_EC},
 2871     {"ACPI_FAN",                ACPI_FAN},
 2872     {"ACPI_POWERRES",           ACPI_POWERRES},
 2873     {"ACPI_PROCESSOR",          ACPI_PROCESSOR},
 2874     {"ACPI_THERMAL",            ACPI_THERMAL},
 2875     {"ACPI_TIMER",              ACPI_TIMER},
 2876     {"ACPI_ALL_DRIVERS",        ACPI_ALL_DRIVERS},
 2877     {NULL, 0}
 2878 };
 2879 
 2880 static struct debugtag dbg_level[] = {
 2881     {"ACPI_LV_ERROR",           ACPI_LV_ERROR},
 2882     {"ACPI_LV_WARN",            ACPI_LV_WARN},
 2883     {"ACPI_LV_INIT",            ACPI_LV_INIT},
 2884     {"ACPI_LV_DEBUG_OBJECT",    ACPI_LV_DEBUG_OBJECT},
 2885     {"ACPI_LV_INFO",            ACPI_LV_INFO},
 2886     {"ACPI_LV_ALL_EXCEPTIONS",  ACPI_LV_ALL_EXCEPTIONS},
 2887 
 2888     /* Trace verbosity level 1 [Standard Trace Level] */
 2889     {"ACPI_LV_INIT_NAMES",      ACPI_LV_INIT_NAMES},
 2890     {"ACPI_LV_PARSE",           ACPI_LV_PARSE},
 2891     {"ACPI_LV_LOAD",            ACPI_LV_LOAD},
 2892     {"ACPI_LV_DISPATCH",        ACPI_LV_DISPATCH},
 2893     {"ACPI_LV_EXEC",            ACPI_LV_EXEC},
 2894     {"ACPI_LV_NAMES",           ACPI_LV_NAMES},
 2895     {"ACPI_LV_OPREGION",        ACPI_LV_OPREGION},
 2896     {"ACPI_LV_BFIELD",          ACPI_LV_BFIELD},
 2897     {"ACPI_LV_TABLES",          ACPI_LV_TABLES},
 2898     {"ACPI_LV_VALUES",          ACPI_LV_VALUES},
 2899     {"ACPI_LV_OBJECTS",         ACPI_LV_OBJECTS},
 2900     {"ACPI_LV_RESOURCES",       ACPI_LV_RESOURCES},
 2901     {"ACPI_LV_USER_REQUESTS",   ACPI_LV_USER_REQUESTS},
 2902     {"ACPI_LV_PACKAGE",         ACPI_LV_PACKAGE},
 2903     {"ACPI_LV_VERBOSITY1",      ACPI_LV_VERBOSITY1},
 2904 
 2905     /* Trace verbosity level 2 [Function tracing and memory allocation] */
 2906     {"ACPI_LV_ALLOCATIONS",     ACPI_LV_ALLOCATIONS},
 2907     {"ACPI_LV_FUNCTIONS",       ACPI_LV_FUNCTIONS},
 2908     {"ACPI_LV_OPTIMIZATIONS",   ACPI_LV_OPTIMIZATIONS},
 2909     {"ACPI_LV_VERBOSITY2",      ACPI_LV_VERBOSITY2},
 2910     {"ACPI_LV_ALL",             ACPI_LV_ALL},
 2911 
 2912     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
 2913     {"ACPI_LV_MUTEX",           ACPI_LV_MUTEX},
 2914     {"ACPI_LV_THREADS",         ACPI_LV_THREADS},
 2915     {"ACPI_LV_IO",              ACPI_LV_IO},
 2916     {"ACPI_LV_INTERRUPTS",      ACPI_LV_INTERRUPTS},
 2917     {"ACPI_LV_VERBOSITY3",      ACPI_LV_VERBOSITY3},
 2918 
 2919     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
 2920     {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE},
 2921     {"ACPI_LV_VERBOSE_INFO",    ACPI_LV_VERBOSE_INFO},
 2922     {"ACPI_LV_FULL_TABLES",     ACPI_LV_FULL_TABLES},
 2923     {"ACPI_LV_EVENTS",          ACPI_LV_EVENTS},
 2924     {"ACPI_LV_VERBOSE",         ACPI_LV_VERBOSE},
 2925     {NULL, 0}
 2926 };    
 2927 
 2928 static void
 2929 acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
 2930 {
 2931     char        *ep;
 2932     int         i, l;
 2933     int         set;
 2934 
 2935     while (*cp) {
 2936         if (isspace(*cp)) {
 2937             cp++;
 2938             continue;
 2939         }
 2940         ep = cp;
 2941         while (*ep && !isspace(*ep))
 2942             ep++;
 2943         if (*cp == '!') {
 2944             set = 0;
 2945             cp++;
 2946             if (cp == ep)
 2947                 continue;
 2948         } else {
 2949             set = 1;
 2950         }
 2951         l = ep - cp;
 2952         for (i = 0; tag[i].name != NULL; i++) {
 2953             if (!strncmp(cp, tag[i].name, l)) {
 2954                 if (set)
 2955                     *flag |= tag[i].value;
 2956                 else
 2957                     *flag &= ~tag[i].value;
 2958             }
 2959         }
 2960         cp = ep;
 2961     }
 2962 }
 2963 
 2964 static void
 2965 acpi_set_debugging(void *junk)
 2966 {
 2967     char        *layer, *level;
 2968 
 2969     if (cold) {
 2970         AcpiDbgLayer = 0;
 2971         AcpiDbgLevel = 0;
 2972     }
 2973 
 2974     layer = getenv("debug.acpi.layer");
 2975     level = getenv("debug.acpi.level");
 2976     if (layer == NULL && level == NULL)
 2977         return;
 2978 
 2979     printf("ACPI set debug");
 2980     if (layer != NULL) {
 2981         if (strcmp("NONE", layer) != 0)
 2982             printf(" layer '%s'", layer);
 2983         acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer);
 2984         freeenv(layer);
 2985     }
 2986     if (level != NULL) {
 2987         if (strcmp("NONE", level) != 0)
 2988             printf(" level '%s'", level);
 2989         acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel);
 2990         freeenv(level);
 2991     }
 2992     printf("\n");
 2993 }
 2994 
 2995 SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging,
 2996         NULL);
 2997 
 2998 static int
 2999 acpi_debug_sysctl(SYSCTL_HANDLER_ARGS)
 3000 {
 3001     int          error, *dbg;
 3002     struct       debugtag *tag;
 3003     struct       sbuf sb;
 3004 
 3005     if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
 3006         return (ENOMEM);
 3007     if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) {
 3008         tag = &dbg_layer[0];
 3009         dbg = &AcpiDbgLayer;
 3010     } else {
 3011         tag = &dbg_level[0];
 3012         dbg = &AcpiDbgLevel;
 3013     }
 3014 
 3015     /* Get old values if this is a get request. */
 3016     ACPI_SERIAL_BEGIN(acpi);
 3017     if (*dbg == 0) {
 3018         sbuf_cpy(&sb, "NONE");
 3019     } else if (req->newptr == NULL) {
 3020         for (; tag->name != NULL; tag++) {
 3021             if ((*dbg & tag->value) == tag->value)
 3022                 sbuf_printf(&sb, "%s ", tag->name);
 3023         }
 3024     }
 3025     sbuf_trim(&sb);
 3026     sbuf_finish(&sb);
 3027 
 3028     /* Copy out the old values to the user. */
 3029     error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
 3030     sbuf_delete(&sb);
 3031 
 3032     /* If the user is setting a string, parse it. */
 3033     if (error == 0 && req->newptr != NULL) {
 3034         *dbg = 0;
 3035         setenv((char *)oidp->oid_arg1, (char *)req->newptr);
 3036         acpi_set_debugging(NULL);
 3037     }
 3038     ACPI_SERIAL_END(acpi);
 3039 
 3040     return (error);
 3041 }
 3042 
 3043 SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING,
 3044             "debug.acpi.layer", 0, acpi_debug_sysctl, "A", "");
 3045 SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING,
 3046             "debug.acpi.level", 0, acpi_debug_sysctl, "A", "");
 3047 #endif /* ACPI_DEBUG */
 3048 
 3049 static int
 3050 acpi_pm_func(u_long cmd, void *arg, ...)
 3051 {
 3052         int     state, acpi_state;
 3053         int     error;
 3054         struct  acpi_softc *sc;
 3055         va_list ap;
 3056 
 3057         error = 0;
 3058         switch (cmd) {
 3059         case POWER_CMD_SUSPEND:
 3060                 sc = (struct acpi_softc *)arg;
 3061                 if (sc == NULL) {
 3062                         error = EINVAL;
 3063                         goto out;
 3064                 }
 3065 
 3066                 va_start(ap, arg);
 3067                 state = va_arg(ap, int);
 3068                 va_end(ap);
 3069 
 3070                 switch (state) {
 3071                 case POWER_SLEEP_STATE_STANDBY:
 3072                         acpi_state = sc->acpi_standby_sx;
 3073                         break;
 3074                 case POWER_SLEEP_STATE_SUSPEND:
 3075                         acpi_state = sc->acpi_suspend_sx;
 3076                         break;
 3077                 case POWER_SLEEP_STATE_HIBERNATE:
 3078                         acpi_state = ACPI_STATE_S4;
 3079                         break;
 3080                 default:
 3081                         error = EINVAL;
 3082                         goto out;
 3083                 }
 3084 
 3085                 acpi_SetSleepState(sc, acpi_state);
 3086                 break;
 3087         default:
 3088                 error = EINVAL;
 3089                 goto out;
 3090         }
 3091 
 3092 out:
 3093         return (error);
 3094 }
 3095 
 3096 static void
 3097 acpi_pm_register(void *arg)
 3098 {
 3099     if (!cold || resource_disabled("acpi", 0))
 3100         return;
 3101 
 3102     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
 3103 }
 3104 
 3105 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);

Cache object: 441ced14f8934261c7ea257b2d7ecced


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