Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.101.16.17
      1 /*	$NetBSD: acpi.c,v 1.101.16.17 2007/10/02 21:44:10 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum of By Noon Software, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright 2001, 2003 Wasabi Systems, Inc.
     41  * All rights reserved.
     42  *
     43  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *	This product includes software developed for the NetBSD Project by
     56  *	Wasabi Systems, Inc.
     57  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     58  *    or promote products derived from this software without specific prior
     59  *    written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     63  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     64  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     65  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     68  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     69  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     70  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     71  * POSSIBILITY OF SUCH DAMAGE.
     72  */
     73 
     74 /*
     75  * Autoconfiguration support for the Intel ACPI Component Architecture
     76  * ACPI reference implementation.
     77  */
     78 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.101.16.17 2007/10/02 21:44:10 joerg Exp $");
     81 
     82 #include "opt_acpi.h"
     83 #include "opt_pcifixup.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/device.h>
     88 #include <sys/malloc.h>
     89 #include <sys/mutex.h>
     90 #include <sys/kernel.h>
     91 #include <sys/proc.h>
     92 #include <sys/sysctl.h>
     93 
     94 #include <dev/acpi/acpica.h>
     95 #include <dev/acpi/acpireg.h>
     96 #include <dev/acpi/acpivar.h>
     97 #include <dev/acpi/acpi_osd.h>
     98 #include <dev/acpi/acpi_timer.h>
     99 #ifdef ACPIVERBOSE
    100 #include <dev/acpi/acpidevs_data.h>
    101 #endif
    102 
    103 #if defined(ACPI_PCI_FIXUP)
    104 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
    105 #endif
    106 
    107 #ifdef PCI_INTR_FIXUP_DISABLED
    108 #include <dev/pci/pcidevs.h>
    109 #endif
    110 
    111 MALLOC_DECLARE(M_ACPI);
    112 
    113 #include <machine/acpi_machdep.h>
    114 
    115 #ifdef ACPI_DEBUGGER
    116 #define	ACPI_DBGR_INIT		0x01
    117 #define	ACPI_DBGR_TABLES	0x02
    118 #define	ACPI_DBGR_ENABLE	0x04
    119 #define	ACPI_DBGR_PROBE		0x08
    120 #define	ACPI_DBGR_RUNNING	0x10
    121 
    122 static int acpi_dbgr = 0x00;
    123 #endif
    124 
    125 static int	acpi_match(struct device *, struct cfdata *, void *);
    126 static void	acpi_attach(struct device *, struct device *, void *);
    127 
    128 static int	acpi_print(void *aux, const char *);
    129 
    130 static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
    131 
    132 extern struct cfdriver acpi_cd;
    133 
    134 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
    135     acpi_match, acpi_attach, NULL, NULL);
    136 
    137 /*
    138  * This is a flag we set when the ACPI subsystem is active.  Machine
    139  * dependent code may wish to skip other steps (such as attaching
    140  * subsystems that ACPI supercedes) when ACPI is active.
    141  */
    142 int	acpi_active;
    143 int	acpi_force_load;
    144 
    145 /*
    146  * Pointer to the ACPI subsystem's state.  There can be only
    147  * one ACPI instance.
    148  */
    149 struct acpi_softc *acpi_softc;
    150 
    151 /*
    152  * Locking stuff.
    153  */
    154 static kmutex_t acpi_slock;
    155 static int acpi_locked;
    156 extern kmutex_t acpi_interrupt_list_mtx;
    157 
    158 /*
    159  * sysctl-related information
    160  */
    161 
    162 static int acpi_node = CTL_EOL;
    163 static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
    164 static int acpi_sleepstate = ACPI_STATE_S0;
    165 
    166 /*
    167  * Prototypes.
    168  */
    169 static void		acpi_shutdown(void *);
    170 static void		acpi_build_tree(struct acpi_softc *);
    171 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    172 
    173 static void		acpi_enable_fixed_events(struct acpi_softc *);
    174 
    175 /*
    176  * acpi_probe:
    177  *
    178  *	Probe for ACPI support.  This is called by the
    179  *	machine-dependent ACPI front-end.  All of the
    180  *	actual work is done by ACPICA.
    181  *
    182  *	NOTE: This is not an autoconfiguration interface function.
    183  */
    184 int
    185 acpi_probe(void)
    186 {
    187 	static int beenhere;
    188 	ACPI_STATUS rv;
    189 
    190 	if (beenhere != 0)
    191 		panic("acpi_probe: ACPI has already been probed");
    192 	beenhere = 1;
    193 
    194 	mutex_init(&acpi_slock, MUTEX_DRIVER, IPL_NONE);
    195 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DRIVER, IPL_NONE);
    196 	acpi_locked = 0;
    197 
    198 	/*
    199 	 * Start up ACPICA.
    200 	 */
    201 #ifdef ACPI_DEBUGGER
    202 	if (acpi_dbgr & ACPI_DBGR_INIT)
    203 		acpi_osd_debugger();
    204 #endif
    205 
    206 	rv = AcpiInitializeSubsystem();
    207 	if (ACPI_FAILURE(rv)) {
    208 		printf("ACPI: unable to initialize ACPICA: %s\n",
    209 		    AcpiFormatException(rv));
    210 		return 0;
    211 	}
    212 
    213 #ifdef ACPI_DEBUGGER
    214 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    215 		acpi_osd_debugger();
    216 #endif
    217 
    218 	rv = AcpiLoadTables();
    219 	if (ACPI_FAILURE(rv)) {
    220 		printf("ACPI: unable to load tables: %s\n",
    221 		    AcpiFormatException(rv));
    222 		return 0;
    223 	}
    224 
    225 	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    226 		printf("ACPI: BIOS implementation in listed as broken:\n");
    227 		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    228 		       "AslId <%4.4s,%08x>\n",
    229 			AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    230 		        AcpiGbl_XSDT->OemRevision,
    231 			AcpiGbl_XSDT->AslCompilerId,
    232 		        AcpiGbl_XSDT->AslCompilerRevision);
    233 		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
    234 		return 0;
    235 	}
    236 
    237 	/* Install the default address space handlers. */
    238 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    239 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    240 	if (ACPI_FAILURE(rv)) {
    241 		printf("ACPI: unable to initialise SystemMemory handler: %s\n",
    242 		    AcpiFormatException(rv));
    243 		return 0;
    244 	}
    245 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    246 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    247 	if (ACPI_FAILURE(rv)) {
    248 		printf("ACPI: unable to initialise SystemIO handler: %s\n",
    249 		     AcpiFormatException(rv));
    250 		return 0;
    251 	}
    252 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    253 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    254 	if (ACPI_FAILURE(rv)) {
    255 		printf("ACPI: unabled to initialise PciConfig handler: %s\n",
    256 		    AcpiFormatException(rv));
    257 		return 0;
    258 	}
    259 
    260 	/*
    261 	 * Looks like we have ACPI!
    262 	 */
    263 
    264 	return 1;
    265 }
    266 
    267 static int
    268 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    269 {
    270 	struct cfattach *ca;
    271 
    272 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    273 	return (ca == &acpi_ca);
    274 }
    275 
    276 int
    277 acpi_check(device_t parent, const char *ifattr)
    278 {
    279 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    280 }
    281 
    282 ACPI_STATUS
    283 acpi_OsGetRootPointer(UINT32 Flags, ACPI_POINTER *PhysicalAddress)
    284 {
    285 	ACPI_STATUS rv;
    286 
    287 	/*
    288 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
    289 	 *
    290 	 * IA-64: Use the EFI.
    291 	 *
    292 	 * We let MD code handle this since there are multiple
    293 	 * ways to do it.
    294 	 */
    295 
    296 	rv = acpi_md_OsGetRootPointer(Flags, PhysicalAddress);
    297 
    298 	if (acpi_root_pointer == 0 && ACPI_SUCCESS(rv))
    299 		acpi_root_pointer =
    300 		    (uint64_t)PhysicalAddress->Pointer.Physical;
    301 
    302 	return rv;
    303 }
    304 
    305 /*
    306  * acpi_match:
    307  *
    308  *	Autoconfiguration `match' routine.
    309  */
    310 static int
    311 acpi_match(struct device *parent, struct cfdata *match,
    312     void *aux)
    313 {
    314 	/*
    315 	 * XXX Check other locators?  Hard to know -- machine
    316 	 * dependent code has already checked for the presence
    317 	 * of ACPI by calling acpi_probe(), so I suppose we
    318 	 * don't really have to do anything else.
    319 	 */
    320 	return 1;
    321 }
    322 
    323 /*
    324  * acpi_attach:
    325  *
    326  *	Autoconfiguration `attach' routine.  Finish initializing
    327  *	ACPICA (some initialization was done in acpi_probe(),
    328  *	which was required to check for the presence of ACPI),
    329  *	and enable the ACPI subsystem.
    330  */
    331 static void
    332 acpi_attach(struct device *parent, struct device *self, void *aux)
    333 {
    334 	struct acpi_softc *sc = (void *) self;
    335 	struct acpibus_attach_args *aa = aux;
    336 	ACPI_STATUS rv;
    337 
    338 	aprint_naive(": Advanced Configuration and Power Interface\n");
    339 	aprint_normal(": Advanced Configuration and Power Interface\n");
    340 
    341 	if (acpi_softc != NULL)
    342 		panic("acpi_attach: ACPI has already been attached");
    343 
    344 	sysmon_power_settype("acpi");
    345 
    346 	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
    347 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    348 
    349 	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    350 	    sc->sc_dev.dv_xname,
    351 	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    352 	    AcpiGbl_XSDT->OemRevision,
    353 	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
    354 
    355 	sc->sc_quirks = acpi_find_quirks();
    356 
    357 	sc->sc_iot = aa->aa_iot;
    358 	sc->sc_memt = aa->aa_memt;
    359 	sc->sc_pc = aa->aa_pc;
    360 	sc->sc_pciflags = aa->aa_pciflags;
    361 	sc->sc_ic = aa->aa_ic;
    362 
    363 	acpi_softc = sc;
    364 
    365 	/*
    366 	 * Register null power management handler
    367 	 */
    368 	(void)pnp_register(self, pnp_generic_power);
    369 
    370 	/*
    371 	 * Bring ACPI on-line.
    372 	 */
    373 #ifdef ACPI_DEBUGGER
    374 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    375 		acpi_osd_debugger();
    376 #endif
    377 
    378 #define ACPI_ENABLE_PHASE1 \
    379     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
    380 #define ACPI_ENABLE_PHASE2 \
    381     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
    382      ACPI_NO_ADDRESS_SPACE_INIT)
    383 
    384 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
    385 	if (ACPI_FAILURE(rv)) {
    386 		aprint_error("%s: unable to enable ACPI: %s\n",
    387 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    388 		return;
    389 	}
    390 
    391 	acpi_md_callback();
    392 
    393 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
    394 	if (ACPI_FAILURE(rv)) {
    395 		aprint_error("%s: unable to enable ACPI: %s\n",
    396 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    397 		return;
    398 	}
    399 
    400 	/* early EC handler initialization if ECDT table is available */
    401 	config_found_ia(&sc->sc_dev, "acpiecdtbus", NULL, NULL);
    402 
    403 	rv = AcpiInitializeObjects(0);
    404 	if (ACPI_FAILURE(rv)) {
    405 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    406 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    407 		return;
    408 	}
    409 	acpi_active = 1;
    410 
    411 	/* Our current state is "awake". */
    412 	sc->sc_sleepstate = ACPI_STATE_S0;
    413 
    414 	/* Show SCI interrupt. */
    415 	if (AcpiGbl_FADT != NULL)
    416 		aprint_verbose("%s: SCI interrupting at int %d\n",
    417 		    sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    418 
    419 	/*
    420 	 * Check for fixed-hardware features.
    421 	 */
    422 	acpi_enable_fixed_events(sc);
    423 	acpitimer_init();
    424 
    425 	/*
    426 	 * Scan the namespace and build our device tree.
    427 	 */
    428 #ifdef ACPI_DEBUGGER
    429 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    430 		acpi_osd_debugger();
    431 #endif
    432 	acpi_build_tree(sc);
    433 
    434 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    435 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    436 		    CTLFLAG_IMMEDIATE,
    437 		    CTLTYPE_QUAD, "root", NULL, NULL,
    438 		    acpi_root_pointer, NULL, 0,
    439 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    440 	}
    441 
    442 
    443 	/*
    444 	 * Register a shutdown hook that disables certain ACPI
    445 	 * events that might happen and confuse us while we're
    446 	 * trying to shut down.
    447 	 */
    448 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    449 	if (sc->sc_sdhook == NULL)
    450 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    451 		    sc->sc_dev.dv_xname);
    452 
    453 #ifdef ACPI_DEBUGGER
    454 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    455 		acpi_osd_debugger();
    456 #endif
    457 }
    458 
    459 /*
    460  * acpi_shutdown:
    461  *
    462  *	Shutdown hook for ACPI -- disable some events that
    463  *	might confuse us.
    464  */
    465 static void
    466 acpi_shutdown(void *arg)
    467 {
    468 	/* nothing */
    469 }
    470 
    471 #if 0
    472 /*
    473  * acpi_disable:
    474  *
    475  *	Disable ACPI.
    476  */
    477 static ACPI_STATUS
    478 acpi_disable(struct acpi_softc *sc)
    479 {
    480 	ACPI_STATUS rv = AE_OK;
    481 
    482 	if (acpi_active) {
    483 		rv = AcpiDisable();
    484 		if (ACPI_SUCCESS(rv))
    485 			acpi_active = 0;
    486 	}
    487 	return rv;
    488 }
    489 #endif
    490 
    491 struct acpi_make_devnode_state {
    492 	struct acpi_softc *softc;
    493 	struct acpi_scope *scope;
    494 };
    495 
    496 /*
    497  * acpi_build_tree:
    498  *
    499  *	Scan relevant portions of the ACPI namespace and attach
    500  *	child devices.
    501  */
    502 static void
    503 acpi_build_tree(struct acpi_softc *sc)
    504 {
    505 	static const char *scopes[] = {
    506 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    507 		"\\_SB_",	/* system bus namespace */
    508 		"\\_SI_",	/* system indicator namespace */
    509 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    510 		NULL,
    511 	};
    512 	struct acpi_attach_args aa;
    513 	struct acpi_make_devnode_state state;
    514 	struct acpi_scope *as;
    515 	struct acpi_devnode *ad;
    516 	ACPI_HANDLE parent;
    517 	ACPI_STATUS rv;
    518 	int i;
    519 
    520 	TAILQ_INIT(&sc->sc_scopes);
    521 
    522 	state.softc = sc;
    523 
    524 	/*
    525 	 * Scan the namespace and build our tree.
    526 	 */
    527 	for (i = 0; scopes[i] != NULL; i++) {
    528 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    529 		as->as_name = scopes[i];
    530 		TAILQ_INIT(&as->as_devnodes);
    531 
    532 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    533 
    534 		state.scope = as;
    535 
    536 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    537 		    &parent);
    538 		if (ACPI_SUCCESS(rv)) {
    539 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    540 			    acpi_make_devnode, &state, NULL);
    541 		}
    542 
    543 		/* Now, for this namespace, try and attach the devices. */
    544 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    545 			aa.aa_node = ad;
    546 			aa.aa_iot = sc->sc_iot;
    547 			aa.aa_memt = sc->sc_memt;
    548 			aa.aa_pc = sc->sc_pc;
    549 			aa.aa_pciflags = sc->sc_pciflags;
    550 			aa.aa_ic = sc->sc_ic;
    551 
    552 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    553 				/*
    554 				 * XXX We only attach devices which are:
    555 				 *
    556 				 *	- present
    557 				 *	- enabled
    558 				 *	- functioning properly
    559 				 *
    560 				 * However, if enabled, it's decoding resources,
    561 				 * so we should claim them, if possible.
    562 				 * Requires changes to bus_space(9).
    563 				 */
    564 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    565 				    ACPI_VALID_STA &&
    566 				    (ad->ad_devinfo->CurrentStatus &
    567 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    568 				      ACPI_STA_DEV_OK)) !=
    569 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    570 				     ACPI_STA_DEV_OK))
    571 					continue;
    572 			}
    573 
    574 			/*
    575 			 * XXX Same problem as above...
    576 			 */
    577 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    578 				continue;
    579 
    580 			ad->ad_device = config_found_ia(&sc->sc_dev,
    581 			    "acpinodebus", &aa, acpi_print);
    582 		}
    583 	}
    584 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    585 }
    586 
    587 #ifdef ACPI_ACTIVATE_DEV
    588 static void
    589 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    590 {
    591 	ACPI_STATUS rv;
    592 	ACPI_BUFFER buf;
    593 
    594 	buf.Pointer = NULL;
    595 	buf.Length = ACPI_ALLOCATE_BUFFER;
    596 
    597 #ifdef ACPI_DEBUG
    598 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    599 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    600 #endif
    601 
    602 	rv = acpi_allocate_resources(handle);
    603 	if (ACPI_FAILURE(rv)) {
    604 		aprint_error("acpi: activate failed for %s\n",
    605 		       (*di)->HardwareId.Value);
    606 	} else {
    607 		aprint_verbose("acpi: activated %s\n",
    608 		    (*di)->HardwareId.Value);
    609 	}
    610 
    611 	(void)AcpiGetObjectInfo(handle, &buf);
    612 	AcpiOsFree(*di);
    613 	*di = buf.Pointer;
    614 
    615 #ifdef ACPI_DEBUG
    616 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    617 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    618 #endif
    619 }
    620 #endif /* ACPI_ACTIVATE_DEV */
    621 
    622 /*
    623  * acpi_make_devnode:
    624  *
    625  *	Make an ACPI devnode.
    626  */
    627 static ACPI_STATUS
    628 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    629     void **status)
    630 {
    631 	struct acpi_make_devnode_state *state = context;
    632 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    633 	struct acpi_softc *sc = state->softc;
    634 #endif
    635 	struct acpi_scope *as = state->scope;
    636 	struct acpi_devnode *ad;
    637 	ACPI_OBJECT_TYPE type;
    638 	ACPI_BUFFER buf;
    639 	ACPI_DEVICE_INFO *devinfo;
    640 	ACPI_STATUS rv;
    641 	ACPI_NAME_UNION *anu;
    642 	int i, clear = 0;
    643 
    644 	rv = AcpiGetType(handle, &type);
    645 	if (ACPI_SUCCESS(rv)) {
    646 		buf.Pointer = NULL;
    647 		buf.Length = ACPI_ALLOCATE_BUFFER;
    648 		rv = AcpiGetObjectInfo(handle, &buf);
    649 		if (ACPI_FAILURE(rv)) {
    650 #ifdef ACPI_DEBUG
    651 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    652 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    653 #endif
    654 			goto out; /* XXX why return OK */
    655 		}
    656 
    657 		devinfo = buf.Pointer;
    658 
    659 		switch (type) {
    660 		case ACPI_TYPE_DEVICE:
    661 #ifdef ACPI_ACTIVATE_DEV
    662 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    663 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    664 			    (devinfo->CurrentStatus &
    665 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    666 			    ACPI_STA_DEV_PRESENT)
    667 				acpi_activate_device(handle, &devinfo);
    668 
    669 			/* FALLTHROUGH */
    670 #endif
    671 
    672 		case ACPI_TYPE_PROCESSOR:
    673 		case ACPI_TYPE_THERMAL:
    674 		case ACPI_TYPE_POWER:
    675 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    676 			if (ad == NULL)
    677 				return AE_NO_MEMORY;
    678 
    679 			ad->ad_devinfo = devinfo;
    680 			ad->ad_handle = handle;
    681 			ad->ad_level = level;
    682 			ad->ad_scope = as;
    683 			ad->ad_type = type;
    684 
    685 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    686 			ad->ad_name[4] = '\0';
    687 			for (i = 3, clear = 0; i >= 0; i--) {
    688 				if (!clear && anu->Ascii[i] == '_')
    689 					ad->ad_name[i] = '\0';
    690 				else {
    691 					ad->ad_name[i] = anu->Ascii[i];
    692 					clear = 1;
    693 				}
    694 			}
    695 			if (ad->ad_name[0] == '\0')
    696 				ad->ad_name[0] = '_';
    697 
    698 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    699 
    700 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    701 				goto out;
    702 
    703 #ifdef ACPI_EXTRA_DEBUG
    704 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    705 			    sc->sc_dev.dv_xname,
    706 			    ad->ad_devinfo->HardwareId.Value,
    707 			    as->as_name, ad->ad_level);
    708 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    709 				aprint_normal("       UID %s\n",
    710 				    ad->ad_devinfo->UniqueId.Value);
    711 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    712 				aprint_normal("       ADR 0x%016qx\n",
    713 				    ad->ad_devinfo->Address);
    714 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    715 				aprint_normal("       STA 0x%08x\n",
    716 				    ad->ad_devinfo->CurrentStatus);
    717 #endif
    718 		}
    719 	}
    720  out:
    721 	return AE_OK;
    722 }
    723 
    724 /*
    725  * acpi_print:
    726  *
    727  *	Autoconfiguration print routine for ACPI node bus.
    728  */
    729 static int
    730 acpi_print(void *aux, const char *pnp)
    731 {
    732 	struct acpi_attach_args *aa = aux;
    733 	ACPI_STATUS rv;
    734 
    735 	if (pnp) {
    736 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    737 			char *pnpstr =
    738 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    739 			char *str;
    740 
    741 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    742 			    pnpstr);
    743 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    744 			    "_STR", &str);
    745 			if (ACPI_SUCCESS(rv)) {
    746 				aprint_normal("[%s] ", str);
    747 				AcpiOsFree(str);
    748 			}
    749 #ifdef ACPIVERBOSE
    750 			else {
    751 				int i;
    752 
    753 				for (i = 0; i < sizeof(acpi_knowndevs) /
    754 				    sizeof(acpi_knowndevs[0]); i++) {
    755 					if (strcmp(acpi_knowndevs[i].pnp,
    756 					    pnpstr) == 0) {
    757 						aprint_normal("[%s] ",
    758 						    acpi_knowndevs[i].str);
    759 					}
    760 				}
    761 			}
    762 
    763 #endif
    764 			aprint_normal("at %s", pnp);
    765 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    766 			aprint_normal("%s (ACPI Object Type '%s' "
    767 			    "[0x%02x]) ", aa->aa_node->ad_name,
    768 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    769 			     aa->aa_node->ad_devinfo->Type);
    770 			aprint_normal("at %s", pnp);
    771 		} else
    772 			return 0;
    773 	} else {
    774 		aprint_normal(" (%s", aa->aa_node->ad_name);
    775 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    776 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    777 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    778 				const char *uid;
    779 
    780 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    781 				if (uid[0] == '\0')
    782 					uid = "<null>";
    783 				aprint_normal("-%s", uid);
    784 			}
    785 		}
    786 		aprint_normal(")");
    787 	}
    788 
    789 	return UNCONF;
    790 }
    791 
    792 /*****************************************************************************
    793  * ACPI fixed-hardware feature handlers
    794  *****************************************************************************/
    795 
    796 static UINT32	acpi_fixed_button_handler(void *);
    797 static void	acpi_fixed_button_pressed(void *);
    798 
    799 /*
    800  * acpi_enable_fixed_events:
    801  *
    802  *	Enable any fixed-hardware feature handlers.
    803  */
    804 static void
    805 acpi_enable_fixed_events(struct acpi_softc *sc)
    806 {
    807 	static int beenhere;
    808 	ACPI_STATUS rv;
    809 
    810 	KASSERT(beenhere == 0);
    811 	beenhere = 1;
    812 
    813 	/*
    814 	 * Check for fixed-hardware buttons.
    815 	 */
    816 
    817 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    818 		aprint_verbose("%s: fixed-feature power button present\n",
    819 		    sc->sc_dev.dv_xname);
    820 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    821 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    822 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    823 			aprint_error("%s: unable to register fixed power "
    824 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    825 		} else {
    826 			rv = AcpiInstallFixedEventHandler(
    827 			    ACPI_EVENT_POWER_BUTTON,
    828 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    829 			if (ACPI_FAILURE(rv)) {
    830 				aprint_error("%s: unable to install handler "
    831 				    "for fixed power button: %s\n",
    832 				    sc->sc_dev.dv_xname,
    833 				    AcpiFormatException(rv));
    834 			}
    835 		}
    836 	}
    837 
    838 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    839 		aprint_verbose("%s: fixed-feature sleep button present\n",
    840 		    sc->sc_dev.dv_xname);
    841 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    842 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    843 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    844 			aprint_error("%s: unable to register fixed sleep "
    845 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    846 		} else {
    847 			rv = AcpiInstallFixedEventHandler(
    848 			    ACPI_EVENT_SLEEP_BUTTON,
    849 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    850 			if (ACPI_FAILURE(rv)) {
    851 				aprint_error("%s: unable to install handler "
    852 				    "for fixed sleep button: %s\n",
    853 				    sc->sc_dev.dv_xname,
    854 				    AcpiFormatException(rv));
    855 			}
    856 		}
    857 	}
    858 }
    859 
    860 /*
    861  * acpi_fixed_button_handler:
    862  *
    863  *	Event handler for the fixed buttons.
    864  */
    865 static UINT32
    866 acpi_fixed_button_handler(void *context)
    867 {
    868 	struct sysmon_pswitch *smpsw = context;
    869 	int rv;
    870 
    871 #ifdef ACPI_BUT_DEBUG
    872 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    873 #endif
    874 
    875 	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    876 	    acpi_fixed_button_pressed, smpsw);
    877 	if (ACPI_FAILURE(rv))
    878 		printf("%s: WARNING: unable to queue fixed button pressed "
    879 		    "callback: %s\n", smpsw->smpsw_name,
    880 		    AcpiFormatException(rv));
    881 
    882 	return ACPI_INTERRUPT_HANDLED;
    883 }
    884 
    885 /*
    886  * acpi_fixed_button_pressed:
    887  *
    888  *	Deal with a fixed button being pressed.
    889  */
    890 static void
    891 acpi_fixed_button_pressed(void *context)
    892 {
    893 	struct sysmon_pswitch *smpsw = context;
    894 
    895 #ifdef ACPI_BUT_DEBUG
    896 	printf("%s: fixed button pressed, calling sysmon\n",
    897 	    smpsw->smpsw_name);
    898 #endif
    899 
    900 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    901 }
    902 
    903 /*****************************************************************************
    904  * ACPI utility routines.
    905  *****************************************************************************/
    906 
    907 /*
    908  * acpi_eval_integer:
    909  *
    910  *	Evaluate an integer object.
    911  */
    912 ACPI_STATUS
    913 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    914 {
    915 	ACPI_STATUS rv;
    916 	ACPI_BUFFER buf;
    917 	ACPI_OBJECT param;
    918 
    919 	if (handle == NULL)
    920 		handle = ACPI_ROOT_OBJECT;
    921 
    922 	buf.Pointer = &param;
    923 	buf.Length = sizeof(param);
    924 
    925 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    926 	if (ACPI_SUCCESS(rv))
    927 		*valp = param.Integer.Value;
    928 
    929 	return rv;
    930 }
    931 
    932 /*
    933  * acpi_eval_string:
    934  *
    935  *	Evaluate a (Unicode) string object.
    936  */
    937 ACPI_STATUS
    938 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    939 {
    940 	ACPI_STATUS rv;
    941 	ACPI_BUFFER buf;
    942 
    943 	if (handle == NULL)
    944 		handle = ACPI_ROOT_OBJECT;
    945 
    946 	buf.Pointer = NULL;
    947 	buf.Length = ACPI_ALLOCATE_BUFFER;
    948 
    949 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    950 	if (ACPI_SUCCESS(rv)) {
    951 		ACPI_OBJECT *param = buf.Pointer;
    952 		const char *ptr = param->String.Pointer;
    953 		size_t len = param->String.Length;
    954 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    955 			rv = AE_NO_MEMORY;
    956 		else
    957 			(void)memcpy(*stringp, ptr, len);
    958 		AcpiOsFree(param);
    959 	}
    960 
    961 	return rv;
    962 }
    963 
    964 
    965 /*
    966  * acpi_eval_struct:
    967  *
    968  *	Evaluate a more complex structure.
    969  *	Caller must free buf.Pointer by AcpiOsFree().
    970  */
    971 ACPI_STATUS
    972 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
    973 {
    974 	ACPI_STATUS rv;
    975 
    976 	if (handle == NULL)
    977 		handle = ACPI_ROOT_OBJECT;
    978 
    979 	bufp->Pointer = NULL;
    980 	bufp->Length = ACPI_ALLOCATE_BUFFER;
    981 
    982 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    983 
    984 	return rv;
    985 }
    986 
    987 /*
    988  * acpi_foreach_package_object:
    989  *
    990  *	Iterate over all objects in a in a packages and pass then all
    991  *	to a function. If the called function returns non AE_OK, the
    992  *	iteration is stopped and that value is returned.
    993  */
    994 
    995 ACPI_STATUS
    996 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    997     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
    998     void *arg)
    999 {
   1000 	ACPI_STATUS rv = AE_OK;
   1001 	int i;
   1002 
   1003 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
   1004 		return AE_BAD_PARAMETER;
   1005 
   1006 	for (i = 0; i < pkg->Package.Count; i++) {
   1007 		rv = (*func)(&pkg->Package.Elements[i], arg);
   1008 		if (ACPI_FAILURE(rv))
   1009 			break;
   1010 	}
   1011 
   1012 	return rv;
   1013 }
   1014 
   1015 const char *
   1016 acpi_name(ACPI_HANDLE handle)
   1017 {
   1018 	static char buffer[80];
   1019 	ACPI_BUFFER buf;
   1020 	ACPI_STATUS rv;
   1021 
   1022 	buf.Length = sizeof(buffer);
   1023 	buf.Pointer = buffer;
   1024 
   1025 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1026 	if (ACPI_FAILURE(rv))
   1027 		return "(unknown acpi path)";
   1028 	return buffer;
   1029 }
   1030 
   1031 /*
   1032  * acpi_get:
   1033  *
   1034  *	Fetch data info the specified (empty) ACPI buffer.
   1035  *	Caller must free buf.Pointer by AcpiOsFree().
   1036  */
   1037 ACPI_STATUS
   1038 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1039     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1040 {
   1041 	buf->Pointer = NULL;
   1042 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1043 
   1044 	return (*getit)(handle, buf);
   1045 }
   1046 
   1047 
   1048 /*
   1049  * acpi_match_hid
   1050  *
   1051  *	Match given ids against _HID and _CIDs
   1052  */
   1053 int
   1054 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1055 {
   1056 	int i;
   1057 
   1058 	while (*ids) {
   1059 		if (ad->Valid & ACPI_VALID_HID) {
   1060 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1061 				return 1;
   1062 		}
   1063 
   1064 		if (ad->Valid & ACPI_VALID_CID) {
   1065 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1066 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1067 					return 1;
   1068 			}
   1069 		}
   1070 		ids++;
   1071 	}
   1072 
   1073 	return 0;
   1074 }
   1075 
   1076 /*
   1077  * acpi_set_wake_gpe
   1078  *
   1079  *	Set GPE as both Runtime and Wake
   1080  */
   1081 void
   1082 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1083 {
   1084 	ACPI_BUFFER buf;
   1085 	ACPI_STATUS rv;
   1086 	ACPI_OBJECT *p, *elt;
   1087 
   1088 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1089 	if (ACPI_FAILURE(rv))
   1090 		return;			/* just ignore */
   1091 
   1092 	p = buf.Pointer;
   1093 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1094 		goto out;		/* just ignore */
   1095 
   1096 	elt = p->Package.Elements;
   1097 
   1098 	/* TBD: package support */
   1099 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1100 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1101 
   1102  out:
   1103 	AcpiOsFree(buf.Pointer);
   1104 }
   1105 
   1106 
   1107 /*****************************************************************************
   1108  * ACPI sleep support.
   1109  *****************************************************************************/
   1110 
   1111 static int
   1112 is_available_state(struct acpi_softc *sc, int state)
   1113 {
   1114 	UINT8 type_a, type_b;
   1115 
   1116 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1117 				&type_a, &type_b));
   1118 }
   1119 
   1120 /*
   1121  * acpi_enter_sleep_state:
   1122  *
   1123  *	enter to the specified sleep state.
   1124  */
   1125 
   1126 ACPI_STATUS
   1127 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1128 {
   1129 	int err, s;
   1130 	ACPI_STATUS ret = AE_OK;
   1131 
   1132 	if (state == acpi_sleepstate)
   1133 		return AE_OK;
   1134 
   1135 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1136 
   1137 	switch (state) {
   1138 	case ACPI_STATE_S0:
   1139 		break;
   1140 	case ACPI_STATE_S1:
   1141 	case ACPI_STATE_S2:
   1142 	case ACPI_STATE_S3:
   1143 	case ACPI_STATE_S4:
   1144 		if (!is_available_state(sc, state)) {
   1145 			aprint_error("%s: cannot enter the sleep state (%d).\n",
   1146 			    sc->sc_dev.dv_xname, state);
   1147 			break;
   1148 		}
   1149 
   1150 		if (state != ACPI_STATE_S1)
   1151 			pnp_global_transition(PNP_STATE_D3);
   1152 
   1153 		ret = AcpiEnterSleepStatePrep(state);
   1154 		if (ACPI_FAILURE(ret)) {
   1155 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1156 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1157 			break;
   1158 		}
   1159 
   1160 		DELAY(1000000);
   1161 		acpi_sleepstate = state;
   1162 		if (state == ACPI_STATE_S1) {
   1163 			/* just enter the state */
   1164 			acpi_md_OsDisableInterrupt();
   1165 			AcpiEnterSleepState((UINT8)state);
   1166 			AcpiLeaveSleepState((UINT8)state);
   1167 		} else {
   1168 			s = splhigh();
   1169 			err = acpi_md_sleep(state);
   1170 			if (err == 0 && state == ACPI_STATE_S3)
   1171 				AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
   1172 			splx(s);
   1173 			if (state == ACPI_STATE_S4)
   1174 				AcpiEnable();
   1175 			AcpiLeaveSleepState((UINT8)state);
   1176 			pnp_global_transition(PNP_STATE_D0);
   1177 		}
   1178 
   1179 		break;
   1180 	case ACPI_STATE_S5:
   1181 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1182 		if (ACPI_FAILURE(ret)) {
   1183 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1184 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1185 			break;
   1186 		}
   1187 		DELAY(1000000);
   1188 		acpi_sleepstate = state;
   1189 		acpi_md_OsDisableInterrupt();
   1190 		AcpiEnterSleepState(ACPI_STATE_S5);
   1191 		aprint_error("%s: WARNING powerdown failed!\n",
   1192 		    sc->sc_dev.dv_xname);
   1193 		break;
   1194 	}
   1195 
   1196 	acpi_sleepstate = ACPI_STATE_S0;
   1197 	return ret;
   1198 }
   1199 
   1200 #if defined(ACPI_ACTIVATE_DEV)
   1201 /* XXX This very incomplete */
   1202 ACPI_STATUS
   1203 acpi_allocate_resources(ACPI_HANDLE handle)
   1204 {
   1205 	ACPI_BUFFER bufp, bufc, bufn;
   1206 	ACPI_RESOURCE *resp, *resc, *resn;
   1207 	ACPI_RESOURCE_IRQ *irq;
   1208 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1209 	ACPI_STATUS rv;
   1210 	uint delta;
   1211 
   1212 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1213 	if (ACPI_FAILURE(rv))
   1214 		goto out;
   1215 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1216 	if (ACPI_FAILURE(rv)) {
   1217 		goto out1;
   1218 	}
   1219 
   1220 	bufn.Length = 1000;
   1221 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1222 	resp = bufp.Pointer;
   1223 	resc = bufc.Pointer;
   1224 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1225 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1226 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1227 			resp = ACPI_NEXT_RESOURCE(resp);
   1228 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1229 			break;
   1230 		/* Found identical Id */
   1231 		resn->Type = resc->Type;
   1232 		switch (resc->Type) {
   1233 		case ACPI_RESOURCE_TYPE_IRQ:
   1234 			memcpy(&resn->Data, &resp->Data,
   1235 			       sizeof(ACPI_RESOURCE_IRQ));
   1236 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1237 			irq->Interrupts[0] =
   1238 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1239 			        Interrupts[irq->InterruptCount-1];
   1240 			irq->InterruptCount = 1;
   1241 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1242 			break;
   1243 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1244 			memcpy(&resn->Data, &resp->Data,
   1245 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1246 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1247 #if 0
   1248 			/*
   1249 			 * XXX not duplicating the interrupt logic above
   1250 			 * because its not clear what it accomplishes.
   1251 			 */
   1252 			xirq->Interrupts[0] =
   1253 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1254 			    Interrupts[irq->NumberOfInterrupts-1];
   1255 			xirq->NumberOfInterrupts = 1;
   1256 #endif
   1257 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1258 			break;
   1259 		case ACPI_RESOURCE_TYPE_IO:
   1260 			memcpy(&resn->Data, &resp->Data,
   1261 			       sizeof(ACPI_RESOURCE_IO));
   1262 			resn->Length = resp->Length;
   1263 			break;
   1264 		default:
   1265 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1266 			rv = AE_BAD_DATA;
   1267 			goto out2;
   1268 		}
   1269 		resc = ACPI_NEXT_RESOURCE(resc);
   1270 		resn = ACPI_NEXT_RESOURCE(resn);
   1271 		resp = ACPI_NEXT_RESOURCE(resp);
   1272 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1273 		if (delta >=
   1274 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1275 			bufn.Length *= 2;
   1276 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1277 					       M_ACPI, M_WAITOK);
   1278 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1279 		}
   1280 	}
   1281 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1282 		printf("acpi_allocate_resources: resc not exhausted\n");
   1283 		rv = AE_BAD_DATA;
   1284 		goto out3;
   1285 	}
   1286 
   1287 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1288 	rv = AcpiSetCurrentResources(handle, &bufn);
   1289 	if (ACPI_FAILURE(rv)) {
   1290 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1291 		       AcpiFormatException(rv));
   1292 	}
   1293 
   1294 out3:
   1295 	free(bufn.Pointer, M_ACPI);
   1296 out2:
   1297 	AcpiOsFree(bufc.Pointer);
   1298 out1:
   1299 	AcpiOsFree(bufp.Pointer);
   1300 out:
   1301 	return rv;
   1302 }
   1303 #endif /* ACPI_ACTIVATE_DEV */
   1304 
   1305 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1306 {
   1307 	const struct sysctlnode *node;
   1308 	const struct sysctlnode *ssnode;
   1309 
   1310 	if (sysctl_createv(clog, 0, NULL, NULL,
   1311 	    CTLFLAG_PERMANENT,
   1312 	    CTLTYPE_NODE, "hw", NULL,
   1313 	    NULL, 0, NULL, 0,
   1314 	    CTL_HW, CTL_EOL) != 0)
   1315 		return;
   1316 
   1317 	if (sysctl_createv(clog, 0, NULL, &node,
   1318 	    CTLFLAG_PERMANENT,
   1319 	    CTLTYPE_NODE, "acpi", NULL,
   1320 	    NULL, 0, NULL, 0,
   1321 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1322 		return;
   1323 
   1324 	acpi_node = node->sysctl_num;
   1325 
   1326 	/* ACPI sleepstate sysctl */
   1327 	if (sysctl_createv(NULL, 0, NULL, &node,
   1328 	    CTLFLAG_PERMANENT,
   1329 	    CTLTYPE_NODE, "machdep", NULL,
   1330 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1331 		return;
   1332 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1333 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1334 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1335 	    CTL_EOL) != 0)
   1336 		return;
   1337 }
   1338 
   1339 static int
   1340 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1341 {
   1342 	int error, t;
   1343 	struct sysctlnode node;
   1344 
   1345 	node = *rnode;
   1346 	t = acpi_sleepstate;
   1347 	node.sysctl_data = &t;
   1348 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1349 	if (error || newp == NULL)
   1350 		return error;
   1351 
   1352 	if (acpi_softc == NULL)
   1353 		return ENOSYS;
   1354 
   1355 	acpi_enter_sleep_state(acpi_softc, t);
   1356 
   1357 	return 0;
   1358 }
   1359