File:  [DragonFly] / src / sys / dev / acpica / Attic / acpi.c
Revision 1.5: download - view: text, annotated - select for diffs
Thu May 13 23:49:14 2004 UTC (10 years, 4 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
device switch 1/many: Remove d_autoq, add d_clone (where d_autoq was).

d_autoq was used to allow the device port dispatch to mix old-style synchronous
calls with new style messaging calls within a particular device.  It was never
used for that purpose.

d_clone will be more fully implemented as work continues.  We are going to
install d_port in the dev_t (struct specinfo) structure itself and d_clone
will be needed to allow devices to 'revector' the port on a minor-number
by minor-number basis, in particular allowing minor numbers to be directly
dispatched to distinct threads.  This is something we will be needing later
on.

    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:  *	$FreeBSD: src/sys/dev/acpica/acpi.c,v 1.95.2.1 2003/08/22 20:49:20 jhb Exp $
   30:  *      $DragonFly: src/sys/dev/acpica/acpi.c,v 1.5 2004/05/13 23:49:14 dillon Exp $ 
   31:  */
   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/bus.h>
   40: #include <sys/conf.h>
   41: #include <sys/ioccom.h>
   42: #include <sys/reboot.h>
   43: #include <sys/sysctl.h>
   44: #include <sys/ctype.h>
   45: #include <sys/linker.h>
   46: #include <sys/power.h>
   47: 
   48: #include <machine/clock.h>
   49: #include <machine/resource.h>
   50: 
   51: #include <bus/isa/isavar.h>
   52: 
   53: #include "acpi.h"
   54: 
   55: #include <dev/acpica/acpica_support.h>
   56: 
   57: #include <dev/acpica/acpivar.h>
   58: #include <dev/acpica/acpiio.h>
   59: 
   60: MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices");
   61: 
   62: /*
   63:  * Hooks for the ACPI CA debugging infrastructure
   64:  */
   65: #define _COMPONENT	ACPI_BUS
   66: ACPI_MODULE_NAME("ACPI")
   67: 
   68: /*
   69:  * Character device 
   70:  */
   71: 
   72: static d_open_t		acpiopen;
   73: static d_close_t	acpiclose;
   74: static d_ioctl_t	acpiioctl;
   75: 
   76: #define CDEV_MAJOR 152
   77: static struct cdevsw acpi_cdevsw = {
   78: 
   79:        .d_name    = "acpi",
   80:        .d_maj     = CDEV_MAJOR,
   81:        .d_flags   = 0,
   82:        .d_port    = NULL,
   83:        .d_clone   = NULL,
   84:        .old_open  = acpiopen,
   85:        .old_close = acpiclose,
   86:        .old_ioctl = acpiioctl,
   87: /*
   88: 	.d_open =       acpiopen,
   89:        .d_close =      acpiclose,
   90:        .d_ioctl =      acpiioctl,
   91: */
   92: 
   93: 
   94: };
   95: 
   96: static const char* sleep_state_names[] = {
   97:     "S0", "S1", "S2", "S3", "S4", "S5", "NONE"};
   98: 
   99: /* this has to be static, as the softc is gone when we need it */
  100: static int acpi_off_state = ACPI_STATE_S5;
  101: 
  102: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
  103: struct mtx	acpi_mutex;
  104: #endif
  105: 
  106: static int	acpi_modevent(struct module *mod, int event, void *junk);
  107: static void	acpi_identify(driver_t *driver, device_t parent);
  108: static int	acpi_probe(device_t dev);
  109: static int	acpi_attach(device_t dev);
  110: static device_t	acpi_add_child(device_t bus, int order, const char *name, int unit);
  111: static int	acpi_print_child(device_t bus, device_t child);
  112: static int	acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result);
  113: static int	acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value);
  114: static int	acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start,
  115: 				  u_long count);
  116: static int	acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp,
  117: 				  u_long *countp);
  118: static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
  119: 					    u_long start, u_long end, u_long count, u_int flags);
  120: static int	acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r);
  121: static u_int32_t acpi_isa_get_logicalid(device_t dev);
  122: static u_int32_t acpi_isa_get_compatid(device_t dev);
  123: static int	acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids);
  124: 
  125: static void	acpi_probe_children(device_t bus);
  126: static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status);
  127: 
  128: static void	acpi_shutdown_pre_sync(void *arg, int howto);
  129: static void	acpi_shutdown_final(void *arg, int howto);
  130: 
  131: static void	acpi_enable_fixed_events(struct acpi_softc *sc);
  132: 
  133: static void	acpi_system_eventhandler_sleep(void *arg, int state);
  134: static void	acpi_system_eventhandler_wakeup(void *arg, int state);
  135: static int	acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
  136: static int	acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS);
  137: 
  138: static int	acpi_pm_func(u_long cmd, void *arg, ...);
  139: 
  140: static device_method_t acpi_methods[] = {
  141:     /* Device interface */
  142:     DEVMETHOD(device_identify,		acpi_identify),
  143:     DEVMETHOD(device_probe,		acpi_probe),
  144:     DEVMETHOD(device_attach,		acpi_attach),
  145:     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
  146:     DEVMETHOD(device_suspend,		bus_generic_suspend),
  147:     DEVMETHOD(device_resume,		bus_generic_resume),
  148: 
  149:     /* Bus interface */
  150:     DEVMETHOD(bus_add_child,		acpi_add_child),
  151:     DEVMETHOD(bus_print_child,		acpi_print_child),
  152:     DEVMETHOD(bus_read_ivar,		acpi_read_ivar),
  153:     DEVMETHOD(bus_write_ivar,		acpi_write_ivar),
  154:     DEVMETHOD(bus_set_resource,		acpi_set_resource),
  155:     DEVMETHOD(bus_get_resource,		acpi_get_resource),
  156:     DEVMETHOD(bus_alloc_resource,	acpi_alloc_resource),
  157:     DEVMETHOD(bus_release_resource,	acpi_release_resource),
  158:     DEVMETHOD(bus_driver_added,		bus_generic_driver_added),
  159:     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
  160:     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
  161:     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
  162:     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
  163: 
  164:     /* ISA emulation */
  165:     DEVMETHOD(isa_pnp_probe,		acpi_isa_pnp_probe),
  166: 
  167:     {0, 0}
  168: };
  169: 
  170: static driver_t acpi_driver = {
  171:     "acpi",
  172:     acpi_methods,
  173:     sizeof(struct acpi_softc),
  174: };
  175: 
  176: static devclass_t acpi_devclass;
  177: DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0);
  178: MODULE_VERSION(acpi, 100);
  179: 
  180: SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, "");
  181: SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, "");
  182: static int acpi_ca_version = ACPI_CA_VERSION;
  183: SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, "");
  184: 
  185: /*
  186:  * ACPI can only be loaded as a module by the loader; activating it after
  187:  * system bootstrap time is not useful, and can be fatal to the system.
  188:  * It also cannot be unloaded, since the entire system bus heirarchy hangs off it.
  189:  */
  190: static int
  191: acpi_modevent(struct module *mod, int event, void *junk)
  192: {
  193:     switch(event) {
  194:     case MOD_LOAD:
  195: 	if (!cold) {
  196: 	    printf("The ACPI driver cannot be loaded after boot.\n");
  197: 	    return(EPERM);
  198: 	}
  199: 	break;
  200:     case MOD_UNLOAD:
  201: 	if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI)
  202: 	    return(EBUSY);
  203: 	break;
  204:     default:
  205: 	break;
  206:     }
  207:     return(0);
  208: }
  209: 
  210: /*
  211:  * Detect ACPI, perform early initialisation
  212:  */
  213: static void
  214: acpi_identify(driver_t *driver, device_t parent)
  215: {
  216:     device_t			child;
  217:     int				error;
  218: #ifdef ACPI_DEBUGGER
  219:     char			*debugpoint;
  220: #endif
  221: 
  222:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  223: 
  224:     if (!cold)
  225: 	return_VOID;
  226: 
  227:     /*
  228:      * Check that we haven't been disabled with a hint.
  229:      */
  230:     if (!resource_int_value("acpi", 0, "disabled", &error) &&
  231: 	(error != 0))
  232: 	return_VOID;
  233: 
  234:     /*
  235:      * Make sure we're not being doubly invoked.
  236:      */
  237:     if (device_find_child(parent, "acpi", 0) != NULL)
  238: 	return_VOID;
  239: 
  240: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
  241:     /* initialise the ACPI mutex */
  242:     mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF);
  243: #endif
  244: 
  245:     /*
  246:      * Start up the ACPI CA subsystem.
  247:      */
  248: #ifdef ACPI_DEBUGGER
  249:     debugpoint = getenv("debug.acpi.debugger");
  250:     if (debugpoint) {
  251: 	if (!strcmp(debugpoint, "init"))
  252: 	    acpi_EnterDebugger();
  253:         freeenv(debugpoint);
  254:     }
  255: #endif
  256:     if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) {
  257: 	printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error));
  258: 	return_VOID;
  259:     }
  260: #ifdef ACPI_DEBUGGER
  261:     debugpoint = getenv("debug.acpi.debugger");
  262:     if (debugpoint) {
  263: 	if (!strcmp(debugpoint, "tables"))
  264: 	    acpi_EnterDebugger();
  265:         freeenv(debugpoint);
  266:     }
  267: #endif
  268: 
  269:     if (ACPI_FAILURE(error = AcpiLoadTables())) {
  270: 	printf("ACPI: table load failed: %s\n", AcpiFormatException(error));
  271: 	return_VOID;
  272:     }
  273:     
  274:     /*
  275:      * Attach the actual ACPI device.
  276:      */
  277:     if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) {
  278: 	    device_printf(parent, "ACPI: could not attach\n");
  279: 	    return_VOID;
  280:     }
  281: }
  282: 
  283: /*
  284:  * Fetch some descriptive data from ACPI to put in our attach message
  285:  */
  286: static int
  287: acpi_probe(device_t dev)
  288: {
  289:     ACPI_TABLE_HEADER	th;
  290:     char		buf[20];
  291:     ACPI_STATUS		status;
  292:     int			error;
  293:     ACPI_LOCK_DECL;
  294: 
  295:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  296: 
  297:     if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
  298:         power_pm_get_type() != POWER_PM_TYPE_ACPI) {
  299: 	device_printf(dev, "Other PM system enabled.\n");
  300: 	return_VALUE(ENXIO);
  301:     }
  302: 
  303:     ACPI_LOCK;
  304: 
  305:     if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) {
  306: 	device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status));
  307: 	error = ENXIO;
  308:     } else {
  309: 	sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId);
  310: 	device_set_desc_copy(dev, buf);
  311: 	error = 0;
  312:     }
  313:     ACPI_UNLOCK;
  314:     return_VALUE(error);
  315: }
  316: 
  317: static int
  318: acpi_attach(device_t dev)
  319: {
  320:     struct acpi_softc	*sc;
  321:     ACPI_STATUS		status;
  322:     int			error;
  323:     UINT32		flags;
  324:     char		*env;
  325: #ifdef ACPI_DEBUGGER
  326:     char		*debugpoint;
  327: #endif
  328:     ACPI_LOCK_DECL;
  329: 
  330:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  331:     ACPI_LOCK;
  332:     sc = device_get_softc(dev);
  333:     bzero(sc, sizeof(*sc));
  334:     sc->acpi_dev = dev;
  335: 
  336: #ifdef ACPI_DEBUGGER
  337:     debugpoint = getenv("debug.acpi.debugger");
  338:     if (debugpoint) {
  339: 	if (!strcmp(debugpoint, "spaces"))
  340: 	    acpi_EnterDebugger();
  341:         freeenv(debugpoint);
  342:     }
  343: #endif
  344: 
  345:     /*
  346:      * Install the default address space handlers.
  347:      */
  348:     error = ENXIO;
  349:     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  350: 						ACPI_ADR_SPACE_SYSTEM_MEMORY,
  351: 						ACPI_DEFAULT_HANDLER,
  352: 						NULL, NULL))) {
  353: 	device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status));
  354: 	goto out;
  355:     }
  356:     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  357: 						ACPI_ADR_SPACE_SYSTEM_IO,
  358: 						ACPI_DEFAULT_HANDLER,
  359: 						NULL, NULL))) {
  360: 	device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status));
  361: 	goto out;
  362:     }
  363:     if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
  364: 						ACPI_ADR_SPACE_PCI_CONFIG,
  365: 						ACPI_DEFAULT_HANDLER,
  366: 						NULL, NULL))) {
  367: 	device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status));
  368: 	goto out;
  369:     }
  370: 
  371:     /*
  372:      * Bring ACPI fully online.
  373:      *
  374:      * Note that some systems (specifically, those with namespace evaluation issues
  375:      * that require the avoidance of parts of the namespace) must avoid running _INI
  376:      * and _STA on everything, as well as dodging the final object init pass.
  377:      *
  378:      * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT).
  379:      *
  380:      * XXX We should arrange for the object init pass after we have attached all our 
  381:      *     child devices, but on many systems it works here.
  382:      */
  383: #ifdef ACPI_DEBUGGER
  384:     debugpoint = getenv("debug.acpi.debugger");
  385:     if (debugpoint) {
  386: 	if (!strcmp(debugpoint, "enable"))
  387: 	    acpi_EnterDebugger();
  388:         freeenv(debugpoint);
  389:     }
  390: #endif
  391:     flags = 0;
  392:     if (testenv("debug.acpi.avoid"))
  393: 	flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
  394:     if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
  395: 	device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status));
  396: 	goto out;
  397:     }
  398: 
  399:     if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) {
  400: 	device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status));
  401: 	goto out;
  402:     }
  403: 
  404:     /*
  405:      * Setup our sysctl tree.
  406:      *
  407:      * XXX: This doesn't check to make sure that none of these fail.
  408:      */
  409:     sysctl_ctx_init(&sc->acpi_sysctl_ctx);
  410:     sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx,
  411: 			       SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
  412: 			       device_get_name(dev), CTLFLAG_RD, 0, "");
  413:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  414: 	OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD,
  415: 	0, 0, acpi_supported_sleep_state_sysctl, "A", "");
  416:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  417: 	OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW,
  418: 	&sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
  419:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  420: 	OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW,
  421: 	&sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", "");
  422:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  423: 	OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW,
  424: 	&sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", "");
  425:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  426: 	OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW,
  427: 	&sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", "");
  428:     SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  429: 	OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW,
  430: 	&sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", "");
  431:     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  432: 	OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW,
  433: 	&sc->acpi_sleep_delay, 0, "sleep delay");
  434:     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  435: 	OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW,
  436: 	&sc->acpi_s4bios, 0, "S4BIOS mode");
  437:     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  438: 	OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW,
  439: 	&sc->acpi_verbose, 0, "verbose mode");
  440:     SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
  441: 		   OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW,
  442: 		   &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff");
  443:     sc->acpi_disable_on_poweroff = 1;
  444:     sc->acpi_sleep_delay = 0;
  445:     sc->acpi_s4bios = 1;
  446:     if (bootverbose)
  447: 	sc->acpi_verbose = 1;
  448:     if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) {
  449: 	sc->acpi_verbose = 1;
  450: 	freeenv(env);
  451:     }
  452: 
  453:     /*
  454:      * Dispatch the default sleep state to devices.
  455:      * TBD: should be configured from userland policy manager.
  456:      */
  457:     sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX;
  458:     sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX;
  459:     sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX;
  460:     sc->acpi_standby_sx = ACPI_STATE_S1;
  461:     sc->acpi_suspend_sx = ACPI_STATE_S3;
  462: 
  463:     acpi_enable_fixed_events(sc);
  464: 
  465:     /*
  466:      * Scan the namespace and attach/initialise children.
  467:      */
  468: #ifdef ACPI_DEBUGGER
  469:     debugpoint = getenv("debug.acpi.debugger");
  470:     if (debugpoint) {
  471: 	if (!strcmp(debugpoint, "probe"))
  472: 	    acpi_EnterDebugger();
  473: 	freeenv(debugpoint);
  474:     }
  475: #endif
  476: 
  477:     /*
  478:      * Register our shutdown handlers
  479:      */
  480:     EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST);
  481:     EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST);
  482: 
  483:     /*
  484:      * Register our acpi event handlers.
  485:      * XXX should be configurable eg. via userland policy manager.
  486:      */
  487:     EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST);
  488:     EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST);
  489: 
  490:     /*
  491:      * Flag our initial states.
  492:      */
  493:     sc->acpi_enabled = 1;
  494:     sc->acpi_sstate = ACPI_STATE_S0;
  495:     sc->acpi_sleep_disabled = 0;
  496: 
  497:     /*
  498:      * Create the control device
  499:      */
  500:     sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644,
  501: 	"acpi");
  502:     sc->acpi_dev_t->si_drv1 = sc;
  503: 
  504: #ifdef ACPI_DEBUGGER
  505:     debugpoint = getenv("debug.acpi.debugger");
  506:     if (debugpoint) {
  507: 	if (!strcmp(debugpoint, "running"))
  508: 	    acpi_EnterDebugger();
  509: 	freeenv(debugpoint);
  510:     }
  511: #endif
  512: 
  513: #ifdef ACPI_USE_THREADS
  514:     if ((error = acpi_task_thread_init())) {
  515: 	goto out;
  516:     }
  517: #endif
  518: 
  519:     if ((error = acpi_machdep_init(dev))) {
  520: 	goto out;
  521:     }
  522: 
  523:     /* Register ACPI again to pass the correct argument of pm_func. */
  524:     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc);
  525: 
  526:     if (!acpi_disabled("bus"))
  527: 	acpi_probe_children(dev);
  528: 
  529:     error = 0;
  530: 
  531:  out:
  532:     ACPI_UNLOCK;
  533:     return_VALUE(error);
  534: }
  535: 
  536: /*
  537:  * Handle a new device being added
  538:  */
  539: static device_t
  540: acpi_add_child(device_t bus, int order, const char *name, int unit)
  541: {
  542:     struct acpi_device	*ad;
  543:     device_t		child;
  544: 
  545:     ad = malloc(sizeof(*ad), M_ACPIDEV, M_INTWAIT | M_ZERO);
  546: 
  547:     resource_list_init(&ad->ad_rl);
  548:     
  549:     child = device_add_child_ordered(bus, order, name, unit);
  550:     if (child != NULL)
  551: 	device_set_ivars(child, ad);
  552:     return(child);
  553: }
  554: 
  555: static int
  556: acpi_print_child(device_t bus, device_t child)
  557: {
  558:     struct acpi_device		*adev = device_get_ivars(child);
  559:     struct resource_list	*rl = &adev->ad_rl;
  560:     int retval = 0;
  561: 
  562:     retval += bus_print_child_header(bus, child);
  563:     retval += resource_list_print_type(rl, "port",  SYS_RES_IOPORT, "%#lx");
  564:     retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
  565:     retval += resource_list_print_type(rl, "irq",   SYS_RES_IRQ,    "%ld");
  566:     retval += resource_list_print_type(rl, "drq",   SYS_RES_DRQ,    "%ld");
  567:     retval += bus_print_child_footer(bus, child);
  568: 
  569:     return(retval);
  570: }
  571: 
  572: 
  573: /*
  574:  * Handle per-device ivars
  575:  */
  576: static int
  577: acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
  578: {
  579:     struct acpi_device	*ad;
  580: 
  581:     if ((ad = device_get_ivars(child)) == NULL) {
  582: 	printf("device has no ivars\n");
  583: 	return(ENOENT);
  584:     }
  585: 
  586:     switch(index) {
  587: 	/* ACPI ivars */
  588:     case ACPI_IVAR_HANDLE:
  589: 	*(ACPI_HANDLE *)result = ad->ad_handle;
  590: 	break;
  591:     case ACPI_IVAR_MAGIC:
  592: 	*(int *)result = ad->ad_magic;
  593: 	break;
  594:     case ACPI_IVAR_PRIVATE:
  595: 	*(void **)result = ad->ad_private;
  596: 	break;
  597: 
  598: 	/* ISA compatibility */
  599:     case ISA_IVAR_VENDORID:
  600:     case ISA_IVAR_SERIAL:
  601:     case ISA_IVAR_COMPATID:
  602: 	*(int *)result = -1;
  603: 	break;
  604: 
  605:     case ISA_IVAR_LOGICALID:
  606: 	*(int *)result = acpi_isa_get_logicalid(child);
  607: 	break;
  608: 
  609:     default:
  610: 	return(ENOENT);
  611:     }
  612:     return(0);
  613: }
  614: 
  615: static int
  616: acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
  617: {
  618:     struct acpi_device	*ad;
  619: 
  620:     if ((ad = device_get_ivars(child)) == NULL) {
  621: 	printf("device has no ivars\n");
  622: 	return(ENOENT);
  623:     }
  624: 
  625:     switch(index) {
  626: 	/* ACPI ivars */
  627:     case ACPI_IVAR_HANDLE:
  628: 	ad->ad_handle = (ACPI_HANDLE)value;
  629: 	break;
  630:     case ACPI_IVAR_MAGIC:
  631: 	ad->ad_magic = (int )value;
  632: 	break;
  633:     case ACPI_IVAR_PRIVATE:
  634: 	ad->ad_private = (void *)value;
  635: 	break;
  636: 
  637:     default:
  638: 	panic("bad ivar write request (%d)", index);
  639: 	return(ENOENT);
  640:     }
  641:     return(0);
  642: }
  643: 
  644: /*
  645:  * Handle child resource allocation/removal
  646:  */
  647: static int
  648: acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
  649: {
  650:     struct acpi_device		*ad = device_get_ivars(child);
  651:     struct resource_list	*rl = &ad->ad_rl;
  652: 
  653:     resource_list_add(rl, type, rid, start, start + count -1, count);
  654: 
  655:     return(0);
  656: }
  657: 
  658: static int
  659: acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
  660: {
  661:     struct acpi_device		*ad = device_get_ivars(child);
  662:     struct resource_list	*rl = &ad->ad_rl;
  663:     struct resource_list_entry	*rle;
  664: 
  665:     rle = resource_list_find(rl, type, rid);
  666:     if (!rle)
  667: 	return(ENOENT);
  668: 	
  669:     if (startp)
  670: 	*startp = rle->start;
  671:     if (countp)
  672: 	*countp = rle->count;
  673: 
  674:     return(0);
  675: }
  676: 
  677: static struct resource *
  678: acpi_alloc_resource(device_t bus, device_t child, int type, int *rid,
  679: 		    u_long start, u_long end, u_long count, u_int flags)
  680: {
  681:     struct acpi_device *ad = device_get_ivars(child);
  682:     struct resource_list *rl = &ad->ad_rl;
  683: 
  684:     return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags));
  685: }
  686: 
  687: static int
  688: acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r)
  689: {
  690:     struct acpi_device *ad = device_get_ivars(child);
  691:     struct resource_list *rl = &ad->ad_rl;
  692: 
  693:     return(resource_list_release(rl, bus, child, type, rid, r));
  694: }
  695: 
  696: /*
  697:  * Handle ISA-like devices probing for a PnP ID to match.
  698:  */
  699: #define PNP_EISAID(s)				\
  700: 	((((s[0] - '@') & 0x1f) << 2)		\
  701: 	 | (((s[1] - '@') & 0x18) >> 3)		\
  702: 	 | (((s[1] - '@') & 0x07) << 13)	\
  703: 	 | (((s[2] - '@') & 0x1f) << 8)		\
  704: 	 | (PNP_HEXTONUM(s[4]) << 16)		\
  705: 	 | (PNP_HEXTONUM(s[3]) << 20)		\
  706: 	 | (PNP_HEXTONUM(s[6]) << 24)		\
  707: 	 | (PNP_HEXTONUM(s[5]) << 28))
  708: 
  709: static u_int32_t
  710: acpi_isa_get_logicalid(device_t dev)
  711: {
  712:     ACPI_HANDLE		h;
  713:     ACPI_DEVICE_INFO	devinfo;
  714:     ACPI_STATUS		error;
  715:     u_int32_t		pnpid;
  716:     ACPI_LOCK_DECL;
  717: 
  718:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  719: 
  720:     pnpid = 0;
  721:     ACPI_LOCK;
  722:     
  723:     /* fetch and validate the HID */
  724:     if ((h = acpi_get_handle(dev)) == NULL)
  725: 	goto out;
  726:     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
  727: 	goto out;
  728:     if (!(devinfo.Valid & ACPI_VALID_HID))
  729: 	goto out;
  730: 
  731:     pnpid = PNP_EISAID(devinfo.HardwareId);
  732: out:
  733:     ACPI_UNLOCK;
  734:     return_VALUE(pnpid);
  735: }
  736: 
  737: static u_int32_t
  738: acpi_isa_get_compatid(device_t dev)
  739: {
  740:     ACPI_HANDLE		h;
  741:     ACPI_DEVICE_INFO	devinfo;
  742:     ACPI_STATUS		error;
  743:     u_int32_t		pnpid;
  744:     ACPI_LOCK_DECL;
  745: 
  746:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  747: 
  748:     pnpid = 0;
  749:     ACPI_LOCK;
  750:     
  751:     /* fetch and validate the HID */
  752:     if ((h = acpi_get_handle(dev)) == NULL)
  753: 	goto out;
  754:     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
  755: 	goto out;
  756:     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid)))
  757: 	goto out;
  758: 
  759: out:
  760:     ACPI_UNLOCK;
  761:     return_VALUE(pnpid);
  762: }
  763: 
  764: 
  765: static int
  766: acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids)
  767: {
  768:     int			result;
  769:     u_int32_t		lid, cid;
  770: 
  771:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  772: 
  773:     /*
  774:      * ISA-style drivers attached to ACPI may persist and
  775:      * probe manually if we return ENOENT.  We never want
  776:      * that to happen, so don't ever return it.
  777:      */
  778:     result = ENXIO;
  779: 
  780:     /* scan the supplied IDs for a match */
  781:     lid = acpi_isa_get_logicalid(child);
  782:     cid = acpi_isa_get_compatid(child);
  783:     while (ids && ids->ip_id) {
  784: 	if (lid == ids->ip_id || cid == ids->ip_id) {
  785: 	    result = 0;
  786: 	    goto out;
  787: 	}
  788: 	ids++;
  789:     }
  790:  out:
  791:     return_VALUE(result);
  792: }
  793: 
  794: /*
  795:  * Scan relevant portions of the ACPI namespace and attach child devices.
  796:  *
  797:  * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 
  798:  * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec.
  799:  */
  800: static void
  801: acpi_probe_children(device_t bus)
  802: {
  803:     ACPI_HANDLE		parent;
  804:     static char		*scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL};
  805:     int			i;
  806: 
  807:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  808:     ACPI_ASSERTLOCK;
  809: 
  810:     /*
  811:      * Create any static children by calling device identify methods.
  812:      */
  813:     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n"));
  814:     bus_generic_probe(bus);
  815: 
  816:     /*
  817:      * Scan the namespace and insert placeholders for all the devices that
  818:      * we find.
  819:      *
  820:      * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because
  821:      * we want to create nodes for all devices, not just those that are currently
  822:      * present. (This assumes that we don't want to create/remove devices as they
  823:      * appear, which might be smarter.)
  824:      */
  825:     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n"));
  826:     for (i = 0; scopes[i] != NULL; i++)
  827: 	if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)))
  828: 	    AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL);
  829: 
  830:     /*
  831:      * Scan all of the child devices we have created and let them probe/attach.
  832:      */
  833:     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n"));
  834:     bus_generic_attach(bus);
  835: 
  836:     /*
  837:      * Some of these children may have attached others as part of their attach
  838:      * process (eg. the root PCI bus driver), so rescan.
  839:      */
  840:     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n"));
  841:     bus_generic_attach(bus);
  842: 
  843:     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n"));
  844:     return_VOID;
  845: }
  846: 
  847: /*
  848:  * Evaluate a child device and determine whether we might attach a device to
  849:  * it.
  850:  */
  851: static ACPI_STATUS
  852: acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status)
  853: {
  854:     ACPI_OBJECT_TYPE	type;
  855:     device_t		child, bus = (device_t)context;
  856: 
  857:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  858: 
  859:     /*
  860:      * Skip this device if we think we'll have trouble with it.
  861:      */
  862:     if (acpi_avoid(handle))
  863: 	return_ACPI_STATUS(AE_OK);
  864: 
  865:     if (ACPI_SUCCESS(AcpiGetType(handle, &type))) {
  866: 	switch(type) {
  867: 	case ACPI_TYPE_DEVICE:
  868: 	case ACPI_TYPE_PROCESSOR:
  869: 	case ACPI_TYPE_THERMAL:
  870: 	case ACPI_TYPE_POWER:
  871: 	    if (acpi_disabled("children"))
  872: 		break;
  873: 	    /* 
  874: 	     * Create a placeholder device for this node.  Sort the placeholder
  875: 	     * so that the probe/attach passes will run breadth-first.
  876: 	     */
  877: 	    ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle)));
  878: 	    child = BUS_ADD_CHILD(bus, level * 10, NULL, -1);
  879: 	    if (child == NULL)
  880: 		break;
  881: 	    acpi_set_handle(child, handle);
  882: 
  883: 	    /*
  884: 	     * Check that the device is present.  If it's not present,
  885: 	     * leave it disabled (so that we have a device_t attached to
  886: 	     * the handle, but we don't probe it).
  887: 	     */
  888: 	    if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) {
  889: 		device_disable(child);
  890: 		break;
  891: 	    }
  892: 
  893: 	    /*
  894: 	     * Get the device's resource settings and attach them.
  895: 	     * Note that if the device has _PRS but no _CRS, we need
  896: 	     * to decide when it's appropriate to try to configure the
  897: 	     * device.  Ignore the return value here; it's OK for the
  898: 	     * device not to have any resources.
  899: 	     */
  900: 	    acpi_parse_resources(child, handle, &acpi_res_parse_set);
  901: 
  902: 	    /* if we're debugging, probe/attach now rather than later */
  903: 	    ACPI_DEBUG_EXEC(device_probe_and_attach(child));
  904: 	    break;
  905: 	}
  906:     }
  907:     return_ACPI_STATUS(AE_OK);
  908: }
  909: 
  910: static void
  911: acpi_shutdown_pre_sync(void *arg, int howto)
  912: {
  913: 
  914:     struct acpi_softc *sc = arg;
  915: 
  916:     ACPI_ASSERTLOCK;
  917: 
  918:     /*
  919:      * Disable all ACPI events before soft off, otherwise the system
  920:      * will be turned on again on some laptops.
  921:      *
  922:      * XXX this should probably be restricted to masking some events just
  923:      *     before powering down, since we may still need ACPI during the
  924:      *     shutdown process.
  925:      */
  926:     if (sc->acpi_disable_on_poweroff)
  927: 	acpi_Disable(sc);
  928: }
  929: 
  930: static void
  931: acpi_shutdown_final(void *arg, int howto)
  932: {
  933:     ACPI_STATUS	status;
  934: 
  935:     ACPI_ASSERTLOCK;
  936: 
  937:     if (howto & RB_POWEROFF) {
  938: 	printf("Power system off using ACPI...\n");
  939: 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) {
  940: 	    printf("AcpiEnterSleepStatePrep failed - %s\n",
  941: 		   AcpiFormatException(status));
  942: 	    return;
  943: 	}
  944: 	if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) {
  945: 	    printf("ACPI power-off failed - %s\n", AcpiFormatException(status));
  946: 	} else {
  947: 	    DELAY(1000000);
  948: 	    printf("ACPI power-off failed - timeout\n");
  949: 	}
  950:     } else {
  951: 	printf("Terminate ACPI\n");
  952: 	AcpiTerminate();
  953:     }
  954: }
  955: 
  956: static void
  957: acpi_enable_fixed_events(struct acpi_softc *sc)
  958: {
  959:     static int	first_time = 1;
  960: #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n"
  961: 
  962:     ACPI_ASSERTLOCK;
  963: 
  964:     /* Enable and clear fixed events and install handlers. */
  965:     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) {
  966: 	AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0);
  967: 	AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED);
  968: 	AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
  969: 				     acpi_eventhandler_power_button_for_sleep, sc);
  970: 	if (first_time) {
  971: 	    device_printf(sc->acpi_dev, MSGFORMAT, "power");
  972: 	}
  973:     }
  974:     if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) {
  975: 	AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0);
  976: 	AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED);
  977: 	AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
  978: 				     acpi_eventhandler_sleep_button_for_sleep, sc);
  979: 	if (first_time) {
  980: 	    device_printf(sc->acpi_dev, MSGFORMAT, "sleep");
  981: 	}
  982:     }
  983: 
  984:     first_time = 0;
  985: }
  986: 
  987: /*
  988:  * Returns true if the device is actually present and should
  989:  * be attached to.  This requires the present, enabled, UI-visible 
  990:  * and diagnostics-passed bits to be set.
  991:  */
  992: BOOLEAN
  993: acpi_DeviceIsPresent(device_t dev)
  994: {
  995:     ACPI_HANDLE		h;
  996:     ACPI_DEVICE_INFO	devinfo;
  997:     ACPI_STATUS		error;
  998: 
  999:     ACPI_ASSERTLOCK;
 1000:     
 1001:     if ((h = acpi_get_handle(dev)) == NULL)
 1002: 	return(FALSE);
 1003:     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
 1004: 	return(FALSE);
 1005:     /* if no _STA method, must be present */
 1006:     if (!(devinfo.Valid & ACPI_VALID_STA))
 1007: 	return(TRUE);
 1008:     /* return true for 'present' and 'functioning' */
 1009:     if ((devinfo.CurrentStatus & 0x9) == 0x9)
 1010: 	return(TRUE);
 1011:     return(FALSE);
 1012: }
 1013: 
 1014: /*
 1015:  * Returns true if the battery is actually present and inserted.
 1016:  */
 1017: BOOLEAN
 1018: acpi_BatteryIsPresent(device_t dev)
 1019: {
 1020:     ACPI_HANDLE		h;
 1021:     ACPI_DEVICE_INFO	devinfo;
 1022:     ACPI_STATUS		error;
 1023: 
 1024:     ACPI_ASSERTLOCK;
 1025:     
 1026:     if ((h = acpi_get_handle(dev)) == NULL)
 1027: 	return(FALSE);
 1028:     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
 1029: 	return(FALSE);
 1030:     /* if no _STA method, must be present */
 1031:     if (!(devinfo.Valid & ACPI_VALID_STA))
 1032: 	return(TRUE);
 1033:     /* return true for 'present' and 'functioning' */
 1034:     if ((devinfo.CurrentStatus & 0x19) == 0x19)
 1035: 	return(TRUE);
 1036:     return(FALSE);
 1037: }
 1038: 
 1039: /*
 1040:  * Match a HID string against a device
 1041:  */
 1042: BOOLEAN
 1043: acpi_MatchHid(device_t dev, char *hid) 
 1044: {
 1045:     ACPI_HANDLE		h;
 1046:     ACPI_DEVICE_INFO	devinfo;
 1047:     ACPI_STATUS		error;
 1048:     int			cid;
 1049: 
 1050:     ACPI_ASSERTLOCK;
 1051: 
 1052:     if (hid == NULL)
 1053: 	return(FALSE);
 1054:     if ((h = acpi_get_handle(dev)) == NULL)
 1055: 	return(FALSE);
 1056:     if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo)))
 1057: 	return(FALSE);
 1058:     if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId))
 1059: 	return(TRUE);
 1060:     if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid)))
 1061: 	return(FALSE);
 1062:     if (cid == PNP_EISAID(hid))
 1063: 	return(TRUE);
 1064:     return(FALSE);
 1065: }
 1066: 
 1067: /*
 1068:  * Return the handle of a named object within our scope, ie. that of (parent)
 1069:  * or one if its parents.
 1070:  */
 1071: ACPI_STATUS
 1072: acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result)
 1073: {
 1074:     ACPI_HANDLE		r;
 1075:     ACPI_STATUS		status;
 1076: 
 1077:     ACPI_ASSERTLOCK;
 1078: 
 1079:     /* walk back up the tree to the root */
 1080:     for (;;) {
 1081: 	if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) {
 1082: 	    *result = r;
 1083: 	    return(AE_OK);
 1084: 	}
 1085: 	if (status != AE_NOT_FOUND)
 1086: 	    return(AE_OK);
 1087: 	if (ACPI_FAILURE(AcpiGetParent(parent, &r)))
 1088: 	    return(AE_NOT_FOUND);
 1089: 	parent = r;
 1090:     }
 1091: }
 1092: 
 1093: /*
 1094:  * Allocate a buffer with a preset data size.
 1095:  */
 1096: ACPI_BUFFER *
 1097: acpi_AllocBuffer(int size)
 1098: {
 1099:     ACPI_BUFFER	*buf;
 1100: 
 1101:     buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_INTWAIT);
 1102:     buf->Length = size;
 1103:     buf->Pointer = (void *)(buf + 1);
 1104:     return(buf);
 1105: }
 1106: 
 1107: /*
 1108:  * Evaluate a path that should return an integer.
 1109:  */
 1110: ACPI_STATUS
 1111: acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number)
 1112: {
 1113:     ACPI_STATUS	error;
 1114:     ACPI_BUFFER	buf;
 1115:     ACPI_OBJECT	param;
 1116: 
 1117:     ACPI_ASSERTLOCK;
 1118: 
 1119:     if (handle == NULL)
 1120: 	handle = ACPI_ROOT_OBJECT;
 1121: 
 1122:     /*
 1123:      * Assume that what we've been pointed at is an Integer object, or
 1124:      * a method that will return an Integer.
 1125:      */
 1126:     buf.Pointer = &param;
 1127:     buf.Length = sizeof(param);
 1128:     if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
 1129: 	if (param.Type == ACPI_TYPE_INTEGER) {
 1130: 	    *number = param.Integer.Value;
 1131: 	} else {
 1132: 	    error = AE_TYPE;
 1133: 	}
 1134:     }
 1135: 
 1136:     /* 
 1137:      * In some applications, a method that's expected to return an Integer
 1138:      * may instead return a Buffer (probably to simplify some internal
 1139:      * arithmetic).  We'll try to fetch whatever it is, and if it's a Buffer,
 1140:      * convert it into an Integer as best we can.
 1141:      *
 1142:      * This is a hack.
 1143:      */
 1144:     if (error == AE_BUFFER_OVERFLOW) {
 1145: 	if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) {
 1146: 	    error = AE_NO_MEMORY;
 1147: 	} else {
 1148: 	    if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) {
 1149: 		error = acpi_ConvertBufferToInteger(&buf, number);
 1150: 	    }
 1151: 	}
 1152: 	AcpiOsFree(buf.Pointer);
 1153:     }
 1154:     return(error);
 1155: }
 1156: 
 1157: ACPI_STATUS
 1158: acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number)
 1159: {
 1160:     ACPI_OBJECT	*p;
 1161:     int		i;
 1162: 
 1163:     p = (ACPI_OBJECT *)bufp->Pointer;
 1164:     if (p->Type == ACPI_TYPE_INTEGER) {
 1165: 	*number = p->Integer.Value;
 1166: 	return(AE_OK);
 1167:     }
 1168:     if (p->Type != ACPI_TYPE_BUFFER)
 1169: 	return(AE_TYPE);
 1170:     if (p->Buffer.Length > sizeof(int))
 1171: 	return(AE_BAD_DATA);
 1172:     *number = 0;
 1173:     for (i = 0; i < p->Buffer.Length; i++)
 1174: 	*number += (*(p->Buffer.Pointer + i) << (i * 8));
 1175:     return(AE_OK);
 1176: }
 1177: 
 1178: /*
 1179:  * Iterate over the elements of an a package object, calling the supplied
 1180:  * function for each element.
 1181:  *
 1182:  * XXX possible enhancement might be to abort traversal on error.
 1183:  */
 1184: ACPI_STATUS
 1185: acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg)
 1186: {
 1187:     ACPI_OBJECT	*comp;
 1188:     int		i;
 1189:     
 1190:     if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE))
 1191: 	return(AE_BAD_PARAMETER);
 1192: 
 1193:     /* iterate over components */
 1194:     for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++)
 1195: 	func(comp, arg);
 1196: 
 1197:     return(AE_OK);
 1198: }
 1199: 
 1200: /*
 1201:  * Find the (index)th resource object in a set.
 1202:  */
 1203: ACPI_STATUS
 1204: acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp)
 1205: {
 1206:     ACPI_RESOURCE	*rp;
 1207:     int			i;
 1208: 
 1209:     rp = (ACPI_RESOURCE *)buf->Pointer;
 1210:     i = index;
 1211:     while (i-- > 0) {
 1212: 	/* range check */	
 1213: 	if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
 1214: 	    return(AE_BAD_PARAMETER);
 1215: 	/* check for terminator */
 1216: 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
 1217: 	    (rp->Length == 0))
 1218: 	    return(AE_NOT_FOUND);
 1219: 	rp = ACPI_RESOURCE_NEXT(rp);
 1220:     }
 1221:     if (resp != NULL)
 1222: 	*resp = rp;
 1223:     return(AE_OK);
 1224: }
 1225: 
 1226: /*
 1227:  * Append an ACPI_RESOURCE to an ACPI_BUFFER.
 1228:  *
 1229:  * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER
 1230:  * provided to contain it.  If the ACPI_BUFFER is empty, allocate a sensible
 1231:  * backing block.  If the ACPI_RESOURCE is NULL, return an empty set of
 1232:  * resources.
 1233:  */
 1234: #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE	512
 1235: 
 1236: ACPI_STATUS
 1237: acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res)
 1238: {
 1239:     ACPI_RESOURCE	*rp;
 1240:     void		*newp;
 1241:     
 1242:     /*
 1243:      * Initialise the buffer if necessary.
 1244:      */
 1245:     if (buf->Pointer == NULL) {
 1246: 	buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE;
 1247: 	if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL)
 1248: 	    return(AE_NO_MEMORY);
 1249: 	rp = (ACPI_RESOURCE *)buf->Pointer;
 1250: 	rp->Id = ACPI_RSTYPE_END_TAG;
 1251: 	rp->Length = 0;
 1252:     }
 1253:     if (res == NULL)
 1254: 	return(AE_OK);
 1255:     
 1256:     /*
 1257:      * Scan the current buffer looking for the terminator.
 1258:      * This will either find the terminator or hit the end
 1259:      * of the buffer and return an error.
 1260:      */
 1261:     rp = (ACPI_RESOURCE *)buf->Pointer;
 1262:     for (;;) {
 1263: 	/* range check, don't go outside the buffer */
 1264: 	if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length))
 1265: 	    return(AE_BAD_PARAMETER);
 1266: 	if ((rp->Id == ACPI_RSTYPE_END_TAG) ||
 1267: 	    (rp->Length == 0)) {
 1268: 	    break;
 1269: 	}
 1270: 	rp = ACPI_RESOURCE_NEXT(rp);
 1271:     }
 1272: 
 1273:     /*
 1274:      * Check the size of the buffer and expand if required.
 1275:      *
 1276:      * Required size is:
 1277:      *	size of existing resources before terminator + 
 1278:      *	size of new resource and header +
 1279:      * 	size of terminator.
 1280:      *
 1281:      * Note that this loop should really only run once, unless
 1282:      * for some reason we are stuffing a *really* huge resource.
 1283:      */
 1284:     while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 
 1285: 	    res->Length + ACPI_RESOURCE_LENGTH_NO_DATA +
 1286: 	    ACPI_RESOURCE_LENGTH) >= buf->Length) {
 1287: 	if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL)
 1288: 	    return(AE_NO_MEMORY);
 1289: 	bcopy(buf->Pointer, newp, buf->Length);
 1290:         rp = (ACPI_RESOURCE *)((u_int8_t *)newp +
 1291: 			       ((u_int8_t *)rp - (u_int8_t *)buf->Pointer));
 1292: 	AcpiOsFree(buf->Pointer);
 1293: 	buf->Pointer = newp;
 1294: 	buf->Length += buf->Length;
 1295:     }
 1296:     
 1297:     /*
 1298:      * Insert the new resource.
 1299:      */
 1300:     bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA);
 1301:     
 1302:     /*
 1303:      * And add the terminator.
 1304:      */
 1305:     rp = ACPI_RESOURCE_NEXT(rp);
 1306:     rp->Id = ACPI_RSTYPE_END_TAG;
 1307:     rp->Length = 0;
 1308: 
 1309:     return(AE_OK);
 1310: }
 1311: 
 1312: /*
 1313:  * Set interrupt model.
 1314:  */
 1315: ACPI_STATUS
 1316: acpi_SetIntrModel(int model)
 1317: {
 1318: 	ACPI_OBJECT_LIST ArgList;
 1319: 	ACPI_OBJECT Arg;
 1320: 
 1321: 	Arg.Type = ACPI_TYPE_INTEGER;
 1322: 	Arg.Integer.Value = model;
 1323: 	ArgList.Count = 1;
 1324: 	ArgList.Pointer = &Arg;
 1325: 	return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL));
 1326: }
 1327: 
 1328: #define ACPI_MINIMUM_AWAKETIME	5
 1329: 
 1330: static void
 1331: acpi_sleep_enable(void *arg)
 1332: {
 1333:     ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0;
 1334: }
 1335: 
 1336: /*
 1337:  * Set the system sleep state
 1338:  *
 1339:  * Currently we only support S1 and S5
 1340:  */
 1341: ACPI_STATUS
 1342: acpi_SetSleepState(struct acpi_softc *sc, int state)
 1343: {
 1344:     ACPI_STATUS	status = AE_OK;
 1345:     UINT8	TypeA;
 1346:     UINT8	TypeB;
 1347: 
 1348:     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 1349:     ACPI_ASSERTLOCK;
 1350: 
 1351:     if (sc->acpi_sstate != ACPI_STATE_S0)
 1352: 	return_ACPI_STATUS(AE_BAD_PARAMETER);	/* avoid reentry */
 1353: 
 1354:     if (sc->acpi_sleep_disabled)
 1355: 	return_ACPI_STATUS(AE_OK);
 1356: 
 1357:     switch (state) {
 1358:     case ACPI_STATE_S0:	/* XXX only for testing */
 1359: 	if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
 1360: 	    device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
 1361: 	}
 1362: 	break;
 1363: 
 1364:     case ACPI_STATE_S1:
 1365:     case ACPI_STATE_S2:
 1366:     case ACPI_STATE_S3:
 1367:     case ACPI_STATE_S4:
 1368: 	if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) {
 1369: 	    device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status));
 1370: 	    break;
 1371: 	}
 1372: 
 1373: 	sc->acpi_sstate = state;
 1374: 	sc->acpi_sleep_disabled = 1;
 1375: 
 1376: 	/*
 1377: 	 * Inform all devices that we are going to sleep.
 1378: 	 */
 1379: 	if (DEVICE_SUSPEND(root_bus) != 0) {
 1380: 	    /*
 1381: 	     * Re-wake the system.
 1382: 	     *
 1383: 	     * XXX note that a better two-pass approach with a 'veto' pass
 1384: 	     *     followed by a "real thing" pass would be better, but the
 1385: 	     *     current bus interface does not provide for this.
 1386: 	     */
 1387: 	    DEVICE_RESUME(root_bus);
 1388: 	    return_ACPI_STATUS(AE_ERROR);
 1389: 	}
 1390: 
 1391: 	if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) {
 1392: 	    device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n",
 1393: 			  AcpiFormatException(status));
 1394: 	    break;
 1395: 	}
 1396: 
 1397: 	if (sc->acpi_sleep_delay > 0) {
 1398: 	    DELAY(sc->acpi_sleep_delay * 1000000);
 1399: 	}
 1400: 
 1401: 	if (state != ACPI_STATE_S1) {
 1402: 	    acpi_sleep_machdep(sc, state);
 1403: 
 1404: 	    /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */
 1405: 	    if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) {
 1406: 		AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
 1407: 	    }
 1408: 
 1409: 	    /* Re-enable ACPI hardware on wakeup from sleep state 4. */
 1410: 	    if (state == ACPI_STATE_S4) {
 1411: 		AcpiEnable();
 1412: 	    }
 1413: 	} else {
 1414: 	    if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) {
 1415: 		device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status));
 1416: 		break;
 1417: 	    }
 1418: 	}
 1419: 	AcpiLeaveSleepState((UINT8)state);
 1420: 	DEVICE_RESUME(root_bus);
 1421: 	sc->acpi_sstate = ACPI_STATE_S0;
 1422: 	acpi_enable_fixed_events(sc);
 1423: 	break;
 1424: 
 1425:     case ACPI_STATE_S5:
 1426: 	/*
 1427: 	 * Shut down cleanly and power off.  This will call us back through the
 1428: 	 * shutdown handlers.
 1429: 	 */
 1430: 	shutdown_nice(RB_POWEROFF);
 1431: 	break;
 1432: 
 1433:     default:
 1434: 	status = AE_BAD_PARAMETER;
 1435: 	break;
 1436:     }
 1437: 
 1438:     if (sc->acpi_sleep_disabled)
 1439: 	timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME);
 1440: 
 1441:     return_ACPI_STATUS(status);
 1442: }
 1443: 
 1444: /*
 1445:  * Enable/Disable ACPI
 1446:  */
 1447: ACPI_STATUS
 1448: acpi_Enable(struct acpi_softc *sc)
 1449: {
 1450:     ACPI_STATUS	status;
 1451:     u_int32_t	flags;
 1452: 
 1453:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1454:     ACPI_ASSERTLOCK;
 1455: 
 1456:     flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT |
 1457:             ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT;
 1458:     if (!sc->acpi_enabled) {
 1459: 	status = AcpiEnableSubsystem(flags);
 1460:     } else {
 1461: 	status = AE_OK;
 1462:     }
 1463:     if (status == AE_OK)
 1464: 	sc->acpi_enabled = 1;
 1465:     return_ACPI_STATUS(status);
 1466: }
 1467: 
 1468: ACPI_STATUS
 1469: acpi_Disable(struct acpi_softc *sc)
 1470: {
 1471:     ACPI_STATUS	status;
 1472: 
 1473:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1474:     ACPI_ASSERTLOCK;
 1475: 
 1476:     if (sc->acpi_enabled) {
 1477: 	status = AcpiDisable();
 1478:     } else {
 1479: 	status = AE_OK;
 1480:     }
 1481:     if (status == AE_OK)
 1482: 	sc->acpi_enabled = 0;
 1483:     return_ACPI_STATUS(status);
 1484: }
 1485: 
 1486: /*
 1487:  * ACPI Event Handlers
 1488:  */
 1489: 
 1490: /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */
 1491: 
 1492: static void
 1493: acpi_system_eventhandler_sleep(void *arg, int state)
 1494: {
 1495:     ACPI_LOCK_DECL;
 1496:     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 1497: 
 1498:     ACPI_LOCK;
 1499:     if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX)
 1500: 	acpi_SetSleepState((struct acpi_softc *)arg, state);
 1501:     ACPI_UNLOCK;
 1502:     return_VOID;
 1503: }
 1504: 
 1505: static void
 1506: acpi_system_eventhandler_wakeup(void *arg, int state)
 1507: {
 1508:     ACPI_LOCK_DECL;
 1509:     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state);
 1510: 
 1511:     /* Well, what to do? :-) */
 1512: 
 1513:     ACPI_LOCK;
 1514:     ACPI_UNLOCK;
 1515: 
 1516:     return_VOID;
 1517: }
 1518: 
 1519: /* 
 1520:  * ACPICA Event Handlers (FixedEvent, also called from button notify handler)
 1521:  */
 1522: UINT32
 1523: acpi_eventhandler_power_button_for_sleep(void *context)
 1524: {
 1525:     struct acpi_softc	*sc = (struct acpi_softc *)context;
 1526: 
 1527:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1528: 
 1529:     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx);
 1530: 
 1531:     return_VALUE(ACPI_INTERRUPT_HANDLED);
 1532: }
 1533: 
 1534: UINT32
 1535: acpi_eventhandler_power_button_for_wakeup(void *context)
 1536: {
 1537:     struct acpi_softc	*sc = (struct acpi_softc *)context;
 1538: 
 1539:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1540: 
 1541:     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx);
 1542: 
 1543:     return_VALUE(ACPI_INTERRUPT_HANDLED);
 1544: }
 1545: 
 1546: UINT32
 1547: acpi_eventhandler_sleep_button_for_sleep(void *context)
 1548: {
 1549:     struct acpi_softc	*sc = (struct acpi_softc *)context;
 1550: 
 1551:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1552: 
 1553:     EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx);
 1554: 
 1555:     return_VALUE(ACPI_INTERRUPT_HANDLED);
 1556: }
 1557: 
 1558: UINT32
 1559: acpi_eventhandler_sleep_button_for_wakeup(void *context)
 1560: {
 1561:     struct acpi_softc	*sc = (struct acpi_softc *)context;
 1562: 
 1563:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1564: 
 1565:     EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx);
 1566: 
 1567:     return_VALUE(ACPI_INTERRUPT_HANDLED);
 1568: }
 1569: 
 1570: /*
 1571:  * XXX This is kinda ugly, and should not be here.
 1572:  */
 1573: struct acpi_staticbuf {
 1574:     ACPI_BUFFER	buffer;
 1575:     char	data[512];
 1576: };
 1577: 
 1578: char *
 1579: acpi_name(ACPI_HANDLE handle)
 1580: {
 1581:     static struct acpi_staticbuf	buf;
 1582: 
 1583:     ACPI_ASSERTLOCK;
 1584: 
 1585:     buf.buffer.Length = 512;
 1586:     buf.buffer.Pointer = &buf.data[0];
 1587: 
 1588:     if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer)))
 1589: 	return(buf.buffer.Pointer);
 1590:     return("(unknown path)");
 1591: }
 1592: 
 1593: /*
 1594:  * Debugging/bug-avoidance.  Avoid trying to fetch info on various
 1595:  * parts of the namespace.
 1596:  */
 1597: int
 1598: acpi_avoid(ACPI_HANDLE handle)
 1599: {
 1600:     char	*cp, *env, *np;
 1601:     int		len;
 1602: 
 1603:     np = acpi_name(handle);
 1604:     if (*np == '\\')
 1605: 	np++;
 1606:     if ((env = getenv("debug.acpi.avoid")) == NULL)
 1607: 	return(0);
 1608: 
 1609:     /* scan the avoid list checking for a match */
 1610:     cp = env;
 1611:     for (;;) {
 1612: 	while ((*cp != 0) && isspace(*cp))
 1613: 	    cp++;
 1614: 	if (*cp == 0)
 1615: 	    break;
 1616: 	len = 0;
 1617: 	while ((cp[len] != 0) && !isspace(cp[len]))
 1618: 	    len++;
 1619: 	if (!strncmp(cp, np, len)) {
 1620: 	    freeenv(env);
 1621: 	    return(1);
 1622: 	}
 1623: 	cp += len;
 1624:     }
 1625:     freeenv(env);
 1626:     return(0);
 1627: }
 1628: 
 1629: /*
 1630:  * Debugging/bug-avoidance.  Disable ACPI subsystem components.
 1631:  */
 1632: int
 1633: acpi_disabled(char *subsys)
 1634: {
 1635:     char	*cp, *env;
 1636:     int		len;
 1637: 
 1638:     if ((env = getenv("debug.acpi.disable")) == NULL)
 1639: 	return(0);
 1640:     if (!strcmp(env, "all")) {
 1641: 	freeenv(env);
 1642: 	return(1);
 1643:     }
 1644: 
 1645:     /* scan the disable list checking for a match */
 1646:     cp = env;
 1647:     for (;;) {
 1648: 	while ((*cp != 0) && isspace(*cp))
 1649: 	    cp++;
 1650: 	if (*cp == 0)
 1651: 	    break;
 1652: 	len = 0;
 1653: 	while ((cp[len] != 0) && !isspace(cp[len]))
 1654: 	    len++;
 1655: 	if (!strncmp(cp, subsys, len)) {
 1656: 	    freeenv(env);
 1657: 	    return(1);
 1658: 	}
 1659: 	cp += len;
 1660:     }
 1661:     freeenv(env);
 1662:     return(0);
 1663: }
 1664: 
 1665: /*
 1666:  * Device wake capability enable/disable.
 1667:  */
 1668: void
 1669: acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable)
 1670: {
 1671:     ACPI_OBJECT_LIST		ArgList;
 1672:     ACPI_OBJECT			Arg;
 1673: 
 1674:     /*
 1675:      * TBD: All Power Resources referenced by elements 2 through N
 1676:      *      of the _PRW object are put into the ON state.
 1677:      */
 1678: 
 1679:     /*
 1680:      * enable/disable device wake function.
 1681:      */
 1682: 
 1683:     ArgList.Count = 1;
 1684:     ArgList.Pointer = &Arg;
 1685: 
 1686:     Arg.Type = ACPI_TYPE_INTEGER;
 1687:     Arg.Integer.Value = enable;
 1688: 
 1689:     (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL);
 1690: }
 1691: 
 1692: void
 1693: acpi_device_enable_wake_event(ACPI_HANDLE h)
 1694: {
 1695:     struct acpi_softc		*sc;
 1696:     ACPI_STATUS			status;
 1697:     ACPI_BUFFER			prw_buffer;
 1698:     ACPI_OBJECT			*res;
 1699: 
 1700:     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1701: 
 1702:     if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) {
 1703: 	return;
 1704:     }
 1705: 
 1706:     /*
 1707:      * _PRW object is only required for devices that have the ability
 1708:      * to wake the system from a system sleeping state.
 1709:      */
 1710:     prw_buffer.Length = ACPI_ALLOCATE_BUFFER;
 1711:     status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer);
 1712:     if (ACPI_FAILURE(status)) {
 1713: 	return;
 1714:     }
 1715: 
 1716:     res = (ACPI_OBJECT *)prw_buffer.Pointer;
 1717:     if (res == NULL) {
 1718: 	return;
 1719:     }
 1720: 
 1721:     if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) {
 1722: 	goto out;
 1723:     }
 1724: 
 1725:     /*
 1726:      * The element 1 of the _PRW object:
 1727:      * The lowest power system sleeping state that can be entered
 1728:      * while still providing wake functionality.
 1729:      * The sleeping state being entered must be greater or equal to
 1730:      * the power state declared in element 1 of the _PRW object.
 1731:      */
 1732:     if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) {
 1733: 	goto out;
 1734:     }
 1735: 
 1736:     if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) {
 1737: 	goto out;
 1738:     }
 1739: 
 1740:     /*
 1741:      * The element 0 of the _PRW object:
 1742:      */
 1743:     switch(res->Package.Elements[0].Type) {
 1744:     case ACPI_TYPE_INTEGER:
 1745: 	/* 
 1746: 	 * If the data type of this package element is numeric, then this
 1747: 	 * _PRW package element is the bit index in the GPEx_EN, in the
 1748: 	 * GPE blocks described in the FADT, of the enable bit that is
 1749: 	 * enabled for the wake event.
 1750: 	 */
 1751: 
 1752: 	status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value,
 1753: 				 ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE);
 1754: 	if (ACPI_FAILURE(status))
 1755:             printf("%s: EnableEvent Failed\n", __func__);
 1756: 	break;
 1757: 
 1758:     case ACPI_TYPE_PACKAGE:
 1759: 	/* XXX TBD */
 1760: 
 1761: 	/*
 1762: 	 * If the data type of this package element is a package, then this
 1763: 	 * _PRW package element is itself a package containing two
 1764: 	 * elements. The first is an object reference to the GPE Block
 1765: 	 * device that contains the GPE that will be triggered by the wake
 1766: 	 * event. The second element is numeric and it contains the bit
 1767: 	 * index in the GPEx_EN, in the GPE Block referenced by the
 1768: 	 * first element in the package, of the enable bit that is enabled for
 1769: 	 * the wake event.
 1770: 	 * For example, if this field is a package then it is of the form:
 1771: 	 * Package() {\_SB.PCI0.ISA.GPE, 2}
 1772: 	 */
 1773: 
 1774: 	break;
 1775: 
 1776:     default:
 1777: 	break;
 1778:     }
 1779: 
 1780: out:
 1781:     if (prw_buffer.Pointer != NULL)
 1782: 	AcpiOsFree(prw_buffer.Pointer);
 1783:     return;
 1784: }
 1785: 
 1786: /*
 1787:  * Control interface.
 1788:  *
 1789:  * We multiplex ioctls for all participating ACPI devices here.  Individual 
 1790:  * drivers wanting to be accessible via /dev/acpi should use the register/deregister
 1791:  * interface to make their handlers visible.
 1792:  */
 1793: struct acpi_ioctl_hook
 1794: {
 1795:     TAILQ_ENTRY(acpi_ioctl_hook)	link;
 1796:     u_long				cmd;
 1797:     int					(* fn)(u_long cmd, caddr_t addr, void *arg);
 1798:     void				*arg;
 1799: };
 1800: 
 1801: static TAILQ_HEAD(,acpi_ioctl_hook)	acpi_ioctl_hooks;
 1802: static int				acpi_ioctl_hooks_initted;
 1803: 
 1804: /*
 1805:  * Register an ioctl handler.
 1806:  */
 1807: int
 1808: acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg)
 1809: {
 1810:     struct acpi_ioctl_hook	*hp;
 1811: 
 1812:     hp = malloc(sizeof(*hp), M_ACPIDEV, M_INTWAIT);
 1813:     hp->cmd = cmd;
 1814:     hp->fn = fn;
 1815:     hp->arg = arg;
 1816:     if (acpi_ioctl_hooks_initted == 0) {
 1817: 	TAILQ_INIT(&acpi_ioctl_hooks);
 1818: 	acpi_ioctl_hooks_initted = 1;
 1819:     }
 1820:     TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link);
 1821:     return(0);
 1822: }
 1823: 
 1824: /*
 1825:  * Deregister an ioctl handler.
 1826:  */
 1827: void	
 1828: acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg))
 1829: {
 1830:     struct acpi_ioctl_hook	*hp;
 1831: 
 1832:     TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link)
 1833: 	if ((hp->cmd == cmd) && (hp->fn == fn))
 1834: 	    break;
 1835: 
 1836:     if (hp != NULL) {
 1837: 	TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link);
 1838: 	free(hp, M_ACPIDEV);
 1839:     }
 1840: }
 1841: 
 1842: static int
 1843: acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td)
 1844: {
 1845:     return(0);
 1846: }
 1847: 
 1848: static int
 1849: acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td)
 1850: {
 1851:     return(0);
 1852: }
 1853: 
 1854: static int
 1855: acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
 1856: {
 1857:     struct acpi_softc		*sc;
 1858:     struct acpi_ioctl_hook	*hp;
 1859:     int				error, xerror, state;
 1860:     ACPI_LOCK_DECL;
 1861: 
 1862:     ACPI_LOCK;
 1863: 
 1864:     error = state = 0;
 1865:     sc = dev->si_drv1;
 1866: 
 1867:     /*
 1868:      * Scan the list of registered ioctls, looking for handlers.
 1869:      */
 1870:     if (acpi_ioctl_hooks_initted) {
 1871: 	TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) {
 1872: 	    if (hp->cmd == cmd) {
 1873: 		xerror = hp->fn(cmd, addr, hp->arg);
 1874: 		if (xerror != 0)
 1875: 		    error = xerror;
 1876: 		goto out;
 1877: 	    }
 1878: 	}
 1879:     }
 1880: 
 1881:     /*
 1882:      * Core ioctls are  not permitted for non-writable user.
 1883:      * Currently, other ioctls just fetch information.
 1884:      * Not changing system behavior.
 1885:      */
 1886:     if(!(flag & FWRITE)){
 1887: 	    return EPERM;
 1888:     }
 1889: 
 1890:     /*
 1891:      * Core system ioctls.
 1892:      */
 1893:     switch (cmd) {
 1894:     case ACPIIO_ENABLE:
 1895: 	if (ACPI_FAILURE(acpi_Enable(sc)))
 1896: 	    error = ENXIO;
 1897: 	break;
 1898: 
 1899:     case ACPIIO_DISABLE:
 1900: 	if (ACPI_FAILURE(acpi_Disable(sc)))
 1901: 	    error = ENXIO;
 1902: 	break;
 1903: 
 1904:     case ACPIIO_SETSLPSTATE:
 1905: 	if (!sc->acpi_enabled) {
 1906: 	    error = ENXIO;
 1907: 	    break;
 1908: 	}
 1909: 	state = *(int *)addr;
 1910: 	if (state >= ACPI_STATE_S0  && state <= ACPI_S_STATES_MAX) {
 1911: 	    acpi_SetSleepState(sc, state);
 1912: 	} else {
 1913: 	    error = EINVAL;
 1914: 	}
 1915: 	break;
 1916: 
 1917:     default:
 1918: 	if (error == 0)
 1919: 	    error = EINVAL;
 1920: 	break;
 1921:     }
 1922: 
 1923: out:
 1924:     ACPI_UNLOCK;
 1925:     return(error);
 1926: }
 1927: 
 1928: static int
 1929: acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
 1930: {
 1931:     char sleep_state[4];
 1932:     char buf[16];
 1933:     int error;
 1934:     UINT8 state, TypeA, TypeB;
 1935: 
 1936:     buf[0] = '\0';
 1937:     for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) {
 1938: 	if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) {
 1939: 	    sprintf(sleep_state, "S%d ", state);
 1940: 	    strcat(buf, sleep_state);
 1941: 	}
 1942:     }
 1943:     error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
 1944:     return(error);
 1945: }
 1946: 
 1947: static int
 1948: acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS)
 1949: {
 1950:     char sleep_state[10];
 1951:     int error;
 1952:     u_int new_state, old_state;
 1953: 
 1954:     old_state = *(u_int *)oidp->oid_arg1;
 1955:     if (old_state > ACPI_S_STATES_MAX+1) {
 1956: 	strcpy(sleep_state, "unknown");
 1957:     } else {
 1958: 	bzero(sleep_state, sizeof(sleep_state));
 1959: 	strncpy(sleep_state, sleep_state_names[old_state],
 1960: 		sizeof(sleep_state_names[old_state]));
 1961:     }
 1962:     error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req);
 1963:     if (error == 0 && req->newptr != NULL) {
 1964: 	for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) {
 1965: 	    if (strncmp(sleep_state, sleep_state_names[new_state],
 1966: 			sizeof(sleep_state)) == 0)
 1967: 		break;
 1968: 	}
 1969: 	if (new_state <= ACPI_S_STATES_MAX+1) {
 1970: 	    if (new_state != old_state) {
 1971: 		*(u_int *)oidp->oid_arg1 = new_state;
 1972: 	    }
 1973: 	} else {
 1974: 	    error = EINVAL;
 1975: 	}
 1976:     }
 1977:     return(error);
 1978: }
 1979: 
 1980: #ifdef ACPI_DEBUG
 1981: /*
 1982:  * Support for parsing debug options from the kernel environment.
 1983:  *
 1984:  * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers
 1985:  * by specifying the names of the bits in the debug.acpi.layer and
 1986:  * debug.acpi.level environment variables.  Bits may be unset by 
 1987:  * prefixing the bit name with !.
 1988:  */
 1989: struct debugtag
 1990: {
 1991:     char	*name;
 1992:     UINT32	value;
 1993: };
 1994: 
 1995: static struct debugtag	dbg_layer[] = {
 1996:     {"ACPI_UTILITIES",		ACPI_UTILITIES},
 1997:     {"ACPI_HARDWARE",		ACPI_HARDWARE},
 1998:     {"ACPI_EVENTS",		ACPI_EVENTS},
 1999:     {"ACPI_TABLES",		ACPI_TABLES},
 2000:     {"ACPI_NAMESPACE",		ACPI_NAMESPACE},
 2001:     {"ACPI_PARSER",		ACPI_PARSER},
 2002:     {"ACPI_DISPATCHER",		ACPI_DISPATCHER},
 2003:     {"ACPI_EXECUTER",		ACPI_EXECUTER},
 2004:     {"ACPI_RESOURCES",		ACPI_RESOURCES},
 2005:     {"ACPI_CA_DEBUGGER",	ACPI_CA_DEBUGGER},
 2006:     {"ACPI_OS_SERVICES",	ACPI_OS_SERVICES},
 2007:     {"ACPI_CA_DISASSEMBLER",	ACPI_CA_DISASSEMBLER},
 2008: 
 2009:     {"ACPI_BUS",		ACPI_BUS},
 2010:     {"ACPI_SYSTEM",		ACPI_SYSTEM},
 2011:     {"ACPI_POWER",		ACPI_POWER},
 2012:     {"ACPI_EC", 		ACPI_EC},
 2013:     {"ACPI_AC_ADAPTER",		ACPI_AC_ADAPTER},
 2014:     {"ACPI_BATTERY",		ACPI_BATTERY},
 2015:     {"ACPI_BUTTON",		ACPI_BUTTON},
 2016:     {"ACPI_PROCESSOR",		ACPI_PROCESSOR},
 2017:     {"ACPI_THERMAL",		ACPI_THERMAL},
 2018:     {"ACPI_FAN",		ACPI_FAN},
 2019: 
 2020:     {"ACPI_ALL_DRIVERS",	ACPI_ALL_DRIVERS},
 2021:     {"ACPI_ALL_COMPONENTS",	ACPI_ALL_COMPONENTS},
 2022:     {NULL, 0}
 2023: };
 2024: 
 2025: static struct debugtag dbg_level[] = {
 2026:     {"ACPI_LV_ERROR",		ACPI_LV_ERROR},
 2027:     {"ACPI_LV_WARN",		ACPI_LV_WARN},
 2028:     {"ACPI_LV_INIT",		ACPI_LV_INIT},
 2029:     {"ACPI_LV_DEBUG_OBJECT",	ACPI_LV_DEBUG_OBJECT},
 2030:     {"ACPI_LV_INFO",		ACPI_LV_INFO},
 2031:     {"ACPI_LV_ALL_EXCEPTIONS",	ACPI_LV_ALL_EXCEPTIONS},
 2032: 
 2033:     /* Trace verbosity level 1 [Standard Trace Level] */
 2034:     {"ACPI_LV_PARSE",		ACPI_LV_PARSE},
 2035:     {"ACPI_LV_LOAD",		ACPI_LV_LOAD},
 2036:     {"ACPI_LV_DISPATCH",	ACPI_LV_DISPATCH},
 2037:     {"ACPI_LV_EXEC",		ACPI_LV_EXEC},
 2038:     {"ACPI_LV_NAMES",		ACPI_LV_NAMES},
 2039:     {"ACPI_LV_OPREGION",	ACPI_LV_OPREGION},
 2040:     {"ACPI_LV_BFIELD",		ACPI_LV_BFIELD},
 2041:     {"ACPI_LV_TABLES",		ACPI_LV_TABLES},
 2042:     {"ACPI_LV_VALUES",		ACPI_LV_VALUES},
 2043:     {"ACPI_LV_OBJECTS",		ACPI_LV_OBJECTS},
 2044:     {"ACPI_LV_RESOURCES",	ACPI_LV_RESOURCES},
 2045:     {"ACPI_LV_USER_REQUESTS",	ACPI_LV_USER_REQUESTS},
 2046:     {"ACPI_LV_PACKAGE",		ACPI_LV_PACKAGE},
 2047:     {"ACPI_LV_INIT_NAMES",	ACPI_LV_INIT_NAMES},
 2048:     {"ACPI_LV_VERBOSITY1",	ACPI_LV_VERBOSITY1},
 2049: 
 2050:     /* Trace verbosity level 2 [Function tracing and memory allocation] */
 2051:     {"ACPI_LV_ALLOCATIONS",	ACPI_LV_ALLOCATIONS},
 2052:     {"ACPI_LV_FUNCTIONS",	ACPI_LV_FUNCTIONS},
 2053:     {"ACPI_LV_OPTIMIZATIONS",	ACPI_LV_OPTIMIZATIONS},
 2054:     {"ACPI_LV_VERBOSITY2",	ACPI_LV_VERBOSITY2},
 2055:     {"ACPI_LV_ALL",		ACPI_LV_ALL},
 2056: 
 2057:     /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */
 2058:     {"ACPI_LV_MUTEX",		ACPI_LV_MUTEX},
 2059:     {"ACPI_LV_THREADS",		ACPI_LV_THREADS},
 2060:     {"ACPI_LV_IO",		ACPI_LV_IO},
 2061:     {"ACPI_LV_INTERRUPTS",	ACPI_LV_INTERRUPTS},
 2062:     {"ACPI_LV_VERBOSITY3",	ACPI_LV_VERBOSITY3},
 2063: 
 2064:     /* Exceptionally verbose output -- also used in the global "DebugLevel"  */
 2065:     {"ACPI_LV_AML_DISASSEMBLE",	ACPI_LV_AML_DISASSEMBLE},
 2066:     {"ACPI_LV_VERBOSE_INFO",	ACPI_LV_VERBOSE_INFO},
 2067:     {"ACPI_LV_FULL_TABLES",	ACPI_LV_FULL_TABLES},
 2068:     {"ACPI_LV_EVENTS",		ACPI_LV_EVENTS},
 2069:     {"ACPI_LV_VERBOSE",		ACPI_LV_VERBOSE},
 2070:     {NULL, 0}
 2071: };    
 2072: 
 2073: static void
 2074: acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag)
 2075: {
 2076:     char	*ep;
 2077:     int		i, l;
 2078:     int		set;
 2079: 
 2080:     while (*cp) {
 2081: 	if (isspace(*cp)) {
 2082: 	    cp++;
 2083: 	    continue;
 2084: 	}
 2085: 	ep = cp;
 2086: 	while (*ep && !isspace(*ep))
 2087: 	    ep++;
 2088: 	if (*cp == '!') {
 2089: 	    set = 0;
 2090: 	    cp++;
 2091: 	    if (cp == ep)
 2092: 		continue;
 2093: 	} else {
 2094: 	    set = 1;
 2095: 	}
 2096: 	l = ep - cp;
 2097: 	for (i = 0; tag[i].name != NULL; i++) {
 2098: 	    if (!strncmp(cp, tag[i].name, l)) {
 2099: 		if (set) {
 2100: 		    *flag |= tag[i].value;
 2101: 		} else {
 2102: 		    *flag &= ~tag[i].value;
 2103: 		}
 2104: 		printf("ACPI_DEBUG: set '%s'\n", tag[i].name);
 2105: 	    }
 2106: 	}
 2107: 	cp = ep;
 2108:     }
 2109: }
 2110: 
 2111: static void
 2112: acpi_set_debugging(void *junk)
 2113: {
 2114:     char	*cp;
 2115: 
 2116:     if (!cold)
 2117: 	return;
 2118: 
 2119:     AcpiDbgLayer = 0;
 2120:     AcpiDbgLevel = 0;
 2121:     if ((cp = getenv("debug.acpi.layer")) != NULL) {
 2122: 	acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer);
 2123: 	freeenv(cp);
 2124:     }
 2125:     if ((cp = getenv("debug.acpi.level")) != NULL) {
 2126: 	acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel);
 2127: 	freeenv(cp);
 2128:     }
 2129: 
 2130:     printf("ACPI debug layer 0x%x  debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel);
 2131: }
 2132: SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL);
 2133: #endif
 2134: 
 2135: static int
 2136: acpi_pm_func(u_long cmd, void *arg, ...)
 2137: {
 2138: 	int	state, acpi_state;
 2139: 	int	error;
 2140: 	struct	acpi_softc *sc;
 2141: 	__va_list	ap;
 2142: 
 2143: 	error = 0;
 2144: 	switch (cmd) {
 2145: 	case POWER_CMD_SUSPEND:
 2146: 		sc = (struct acpi_softc *)arg;
 2147: 		if (sc == NULL) {
 2148: 			error = EINVAL;
 2149: 			goto out;
 2150: 		}
 2151: 
 2152: 		__va_start(ap, arg);
 2153: 		state = __va_arg(ap, int);
 2154: 		__va_end(ap);	
 2155: 
 2156: 		switch (state) {
 2157: 		case POWER_SLEEP_STATE_STANDBY:
 2158: 			acpi_state = sc->acpi_standby_sx;
 2159: 			break;
 2160: 		case POWER_SLEEP_STATE_SUSPEND:
 2161: 			acpi_state = sc->acpi_suspend_sx;
 2162: 			break;
 2163: 		case POWER_SLEEP_STATE_HIBERNATE:
 2164: 			acpi_state = ACPI_STATE_S4;
 2165: 			break;
 2166: 		default:
 2167: 			error = EINVAL;
 2168: 			goto out;
 2169: 		}
 2170: 
 2171: 		acpi_SetSleepState(sc, acpi_state);
 2172: 		break;
 2173: 
 2174: 	default:
 2175: 		error = EINVAL;
 2176: 		goto out;
 2177: 	}
 2178: 
 2179: out:
 2180: 	return (error);
 2181: }
 2182: 
 2183: static void
 2184: acpi_pm_register(void *arg)
 2185: {
 2186:     int	error;
 2187: 
 2188:     if (!cold)
 2189: 	return;
 2190: 
 2191:     if (!resource_int_value("acpi", 0, "disabled", &error) &&
 2192:        (error != 0))
 2193: 		return;
 2194: 
 2195:     power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL);
 2196: }
 2197: 
 2198: SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0);
 2199: