Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.101.16.7
      1 /*	$NetBSD: acpi.c,v 1.101.16.7 2007/08/14 22:25:07 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.7 2007/08/14 22:25:07 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 
    226 	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    227 		printf("ACPI: BIOS implementation in listed as broken:\n");
    228 		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    229 		       "AslId <%4.4s,%08x>\n",
    230 			AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    231 		        AcpiGbl_XSDT->OemRevision,
    232 			AcpiGbl_XSDT->AslCompilerId,
    233 		        AcpiGbl_XSDT->AslCompilerRevision);
    234 		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
    235 		return 0;
    236 	}
    237 
    238 	/*
    239 	 * Looks like we have ACPI!
    240 	 */
    241 
    242 	return 1;
    243 }
    244 
    245 static int
    246 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    247 {
    248 	struct cfattach *ca;
    249 
    250 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    251 	return (ca == &acpi_ca);
    252 }
    253 
    254 int
    255 acpi_check(device_t parent, const char *ifattr)
    256 {
    257 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    258 }
    259 
    260 ACPI_STATUS
    261 acpi_OsGetRootPointer(UINT32 Flags, ACPI_POINTER *PhysicalAddress)
    262 {
    263 	ACPI_STATUS rv;
    264 
    265 	/*
    266 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
    267 	 *
    268 	 * IA-64: Use the EFI.
    269 	 *
    270 	 * We let MD code handle this since there are multiple
    271 	 * ways to do it.
    272 	 */
    273 
    274 	rv = acpi_md_OsGetRootPointer(Flags, PhysicalAddress);
    275 
    276 	if (acpi_root_pointer == 0 && ACPI_SUCCESS(rv))
    277 		acpi_root_pointer =
    278 		    (uint64_t)PhysicalAddress->Pointer.Physical;
    279 
    280 	return rv;
    281 }
    282 
    283 /*
    284  * acpi_match:
    285  *
    286  *	Autoconfiguration `match' routine.
    287  */
    288 static int
    289 acpi_match(struct device *parent, struct cfdata *match,
    290     void *aux)
    291 {
    292 	/*
    293 	 * XXX Check other locators?  Hard to know -- machine
    294 	 * dependent code has already checked for the presence
    295 	 * of ACPI by calling acpi_probe(), so I suppose we
    296 	 * don't really have to do anything else.
    297 	 */
    298 	return 1;
    299 }
    300 
    301 /*
    302  * acpi_attach:
    303  *
    304  *	Autoconfiguration `attach' routine.  Finish initializing
    305  *	ACPICA (some initialization was done in acpi_probe(),
    306  *	which was required to check for the presence of ACPI),
    307  *	and enable the ACPI subsystem.
    308  */
    309 static void
    310 acpi_attach(struct device *parent, struct device *self, void *aux)
    311 {
    312 	struct acpi_softc *sc = (void *) self;
    313 	struct acpibus_attach_args *aa = aux;
    314 	ACPI_STATUS rv;
    315 
    316 	aprint_naive(": Advanced Configuration and Power Interface\n");
    317 	aprint_normal(": Advanced Configuration and Power Interface\n");
    318 
    319 	if (acpi_softc != NULL)
    320 		panic("acpi_attach: ACPI has already been attached");
    321 
    322 	sysmon_power_settype("acpi");
    323 
    324 	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
    325 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    326 
    327 	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    328 	    sc->sc_dev.dv_xname,
    329 	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    330 	    AcpiGbl_XSDT->OemRevision,
    331 	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
    332 
    333 	sc->sc_quirks = acpi_find_quirks();
    334 
    335 	sc->sc_iot = aa->aa_iot;
    336 	sc->sc_memt = aa->aa_memt;
    337 	sc->sc_pc = aa->aa_pc;
    338 	sc->sc_pciflags = aa->aa_pciflags;
    339 	sc->sc_ic = aa->aa_ic;
    340 
    341 	acpi_softc = sc;
    342 
    343 	/*
    344 	 * Register null power management handler
    345 	 */
    346 	(void)pnp_register(self, pnp_generic_power);
    347 
    348 	/*
    349 	 * Bring ACPI on-line.
    350 	 */
    351 #ifdef ACPI_DEBUGGER
    352 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    353 		acpi_osd_debugger();
    354 #endif
    355 
    356 	/* Show SCI interrupt. */
    357 	if (AcpiGbl_FADT != NULL)
    358 		aprint_verbose("%s: SCI interrupting at int %d\n",
    359 		    sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    360 
    361 	acpi_md_callback((struct device *)sc);
    362 
    363 	rv = AcpiEnableSubsystem(0);
    364 	if (ACPI_FAILURE(rv)) {
    365 		aprint_error("%s: unable to enable ACPI: %s\n",
    366 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    367 		return;
    368 	}
    369 
    370 	/* early EC handler initialization if ECDT table is available */
    371 #if NACPIEC > 0
    372 	acpiec_early_attach(&sc->sc_dev);
    373 #endif
    374 
    375 	rv = AcpiInitializeObjects(0);
    376 	if (ACPI_FAILURE(rv)) {
    377 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    378 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    379 		return;
    380 	}
    381 	acpi_active = 1;
    382 
    383 	/* Our current state is "awake". */
    384 	sc->sc_sleepstate = ACPI_STATE_S0;
    385 	/*
    386 	 * Check for fixed-hardware features.
    387 	 */
    388 	acpi_enable_fixed_events(sc);
    389 	acpitimer_init();
    390 
    391 	/*
    392 	 * Scan the namespace and build our device tree.
    393 	 */
    394 #ifdef ACPI_DEBUGGER
    395 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    396 		acpi_osd_debugger();
    397 #endif
    398 	acpi_build_tree(sc);
    399 
    400 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    401 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    402 		    CTLFLAG_IMMEDIATE,
    403 		    CTLTYPE_QUAD, "root", NULL, NULL,
    404 		    acpi_root_pointer, NULL, 0,
    405 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    406 	}
    407 
    408 
    409 	/*
    410 	 * Register a shutdown hook that disables certain ACPI
    411 	 * events that might happen and confuse us while we're
    412 	 * trying to shut down.
    413 	 */
    414 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    415 	if (sc->sc_sdhook == NULL)
    416 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    417 		    sc->sc_dev.dv_xname);
    418 
    419 #ifdef ACPI_DEBUGGER
    420 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    421 		acpi_osd_debugger();
    422 #endif
    423 }
    424 
    425 /*
    426  * acpi_shutdown:
    427  *
    428  *	Shutdown hook for ACPI -- disable some events that
    429  *	might confuse us.
    430  */
    431 static void
    432 acpi_shutdown(void *arg)
    433 {
    434 	/* nothing */
    435 }
    436 
    437 #if 0
    438 /*
    439  * acpi_disable:
    440  *
    441  *	Disable ACPI.
    442  */
    443 static ACPI_STATUS
    444 acpi_disable(struct acpi_softc *sc)
    445 {
    446 	ACPI_STATUS rv = AE_OK;
    447 
    448 	if (acpi_active) {
    449 		rv = AcpiDisable();
    450 		if (ACPI_SUCCESS(rv))
    451 			acpi_active = 0;
    452 	}
    453 	return rv;
    454 }
    455 #endif
    456 
    457 struct acpi_make_devnode_state {
    458 	struct acpi_softc *softc;
    459 	struct acpi_scope *scope;
    460 };
    461 
    462 /*
    463  * acpi_build_tree:
    464  *
    465  *	Scan relevant portions of the ACPI namespace and attach
    466  *	child devices.
    467  */
    468 static void
    469 acpi_build_tree(struct acpi_softc *sc)
    470 {
    471 	static const char *scopes[] = {
    472 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    473 		"\\_SB_",	/* system bus namespace */
    474 		"\\_SI_",	/* system indicator namespace */
    475 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    476 		NULL,
    477 	};
    478 	struct acpi_attach_args aa;
    479 	struct acpi_make_devnode_state state;
    480 	struct acpi_scope *as;
    481 	struct acpi_devnode *ad;
    482 	ACPI_HANDLE parent;
    483 	ACPI_STATUS rv;
    484 	int i;
    485 
    486 	TAILQ_INIT(&sc->sc_scopes);
    487 
    488 	state.softc = sc;
    489 
    490 	/*
    491 	 * Scan the namespace and build our tree.
    492 	 */
    493 	for (i = 0; scopes[i] != NULL; i++) {
    494 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    495 		as->as_name = scopes[i];
    496 		TAILQ_INIT(&as->as_devnodes);
    497 
    498 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    499 
    500 		state.scope = as;
    501 
    502 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    503 		    &parent);
    504 		if (ACPI_SUCCESS(rv)) {
    505 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    506 			    acpi_make_devnode, &state, NULL);
    507 		}
    508 
    509 		/* Now, for this namespace, try and attach the devices. */
    510 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    511 			aa.aa_node = ad;
    512 			aa.aa_iot = sc->sc_iot;
    513 			aa.aa_memt = sc->sc_memt;
    514 			aa.aa_pc = sc->sc_pc;
    515 			aa.aa_pciflags = sc->sc_pciflags;
    516 			aa.aa_ic = sc->sc_ic;
    517 
    518 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    519 				/*
    520 				 * XXX We only attach devices which are:
    521 				 *
    522 				 *	- present
    523 				 *	- enabled
    524 				 *	- functioning properly
    525 				 *
    526 				 * However, if enabled, it's decoding resources,
    527 				 * so we should claim them, if possible.
    528 				 * Requires changes to bus_space(9).
    529 				 */
    530 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    531 				    ACPI_VALID_STA &&
    532 				    (ad->ad_devinfo->CurrentStatus &
    533 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    534 				      ACPI_STA_DEV_OK)) !=
    535 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    536 				     ACPI_STA_DEV_OK))
    537 					continue;
    538 			}
    539 
    540 			ad->ad_device = config_found_ia(&sc->sc_dev,
    541 			    "acpinodebus", &aa, acpi_print);
    542 		}
    543 	}
    544 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    545 }
    546 
    547 #ifdef ACPI_ACTIVATE_DEV
    548 static void
    549 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    550 {
    551 	ACPI_STATUS rv;
    552 	ACPI_BUFFER buf;
    553 
    554 	buf.Pointer = NULL;
    555 	buf.Length = ACPI_ALLOCATE_BUFFER;
    556 
    557 #ifdef ACPI_DEBUG
    558 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    559 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    560 #endif
    561 
    562 	rv = acpi_allocate_resources(handle);
    563 	if (ACPI_FAILURE(rv)) {
    564 		aprint_error("acpi: activate failed for %s\n",
    565 		       (*di)->HardwareId.Value);
    566 	} else {
    567 		aprint_verbose("acpi: activated %s\n",
    568 		    (*di)->HardwareId.Value);
    569 	}
    570 
    571 	(void)AcpiGetObjectInfo(handle, &buf);
    572 	AcpiOsFree(*di);
    573 	*di = buf.Pointer;
    574 
    575 #ifdef ACPI_DEBUG
    576 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    577 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    578 #endif
    579 }
    580 #endif /* ACPI_ACTIVATE_DEV */
    581 
    582 /*
    583  * acpi_make_devnode:
    584  *
    585  *	Make an ACPI devnode.
    586  */
    587 static ACPI_STATUS
    588 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    589     void **status)
    590 {
    591 	struct acpi_make_devnode_state *state = context;
    592 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    593 	struct acpi_softc *sc = state->softc;
    594 #endif
    595 	struct acpi_scope *as = state->scope;
    596 	struct acpi_devnode *ad;
    597 	ACPI_OBJECT_TYPE type;
    598 	ACPI_BUFFER buf;
    599 	ACPI_DEVICE_INFO *devinfo;
    600 	ACPI_STATUS rv;
    601 	ACPI_NAME_UNION *anu;
    602 	int i, clear = 0;
    603 
    604 	rv = AcpiGetType(handle, &type);
    605 	if (ACPI_SUCCESS(rv)) {
    606 		buf.Pointer = NULL;
    607 		buf.Length = ACPI_ALLOCATE_BUFFER;
    608 		rv = AcpiGetObjectInfo(handle, &buf);
    609 		if (ACPI_FAILURE(rv)) {
    610 #ifdef ACPI_DEBUG
    611 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    612 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    613 #endif
    614 			goto out; /* XXX why return OK */
    615 		}
    616 
    617 		devinfo = buf.Pointer;
    618 
    619 		switch (type) {
    620 		case ACPI_TYPE_DEVICE:
    621 #ifdef ACPI_ACTIVATE_DEV
    622 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    623 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    624 			    (devinfo->CurrentStatus &
    625 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    626 			    ACPI_STA_DEV_PRESENT)
    627 				acpi_activate_device(handle, &devinfo);
    628 
    629 			/* FALLTHROUGH */
    630 #endif
    631 
    632 		case ACPI_TYPE_PROCESSOR:
    633 		case ACPI_TYPE_THERMAL:
    634 		case ACPI_TYPE_POWER:
    635 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    636 			if (ad == NULL)
    637 				return AE_NO_MEMORY;
    638 
    639 			ad->ad_devinfo = devinfo;
    640 			ad->ad_handle = handle;
    641 			ad->ad_level = level;
    642 			ad->ad_scope = as;
    643 			ad->ad_type = type;
    644 
    645 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    646 			ad->ad_name[4] = '\0';
    647 			for (i = 3, clear = 0; i >= 0; i--) {
    648 				if (!clear && anu->Ascii[i] == '_')
    649 					ad->ad_name[i] = '\0';
    650 				else {
    651 					ad->ad_name[i] = anu->Ascii[i];
    652 					clear = 1;
    653 				}
    654 			}
    655 			if (ad->ad_name[0] == '\0')
    656 				ad->ad_name[0] = '_';
    657 
    658 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    659 
    660 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    661 				goto out;
    662 
    663 #ifdef ACPI_EXTRA_DEBUG
    664 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    665 			    sc->sc_dev.dv_xname,
    666 			    ad->ad_devinfo->HardwareId.Value,
    667 			    as->as_name, ad->ad_level);
    668 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    669 				aprint_normal("       UID %s\n",
    670 				    ad->ad_devinfo->UniqueId.Value);
    671 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    672 				aprint_normal("       ADR 0x%016qx\n",
    673 				    ad->ad_devinfo->Address);
    674 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    675 				aprint_normal("       STA 0x%08x\n",
    676 				    ad->ad_devinfo->CurrentStatus);
    677 #endif
    678 		}
    679 	}
    680  out:
    681 	return AE_OK;
    682 }
    683 
    684 /*
    685  * acpi_print:
    686  *
    687  *	Autoconfiguration print routine for ACPI node bus.
    688  */
    689 static int
    690 acpi_print(void *aux, const char *pnp)
    691 {
    692 	struct acpi_attach_args *aa = aux;
    693 	ACPI_STATUS rv;
    694 
    695 	if (pnp) {
    696 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    697 			char *pnpstr =
    698 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    699 			char *str;
    700 
    701 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    702 			    pnpstr);
    703 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    704 			    "_STR", &str);
    705 			if (ACPI_SUCCESS(rv)) {
    706 				aprint_normal("[%s] ", str);
    707 				AcpiOsFree(str);
    708 			}
    709 #ifdef ACPIVERBOSE
    710 			else {
    711 				int i;
    712 
    713 				for (i = 0; i < sizeof(acpi_knowndevs) /
    714 				    sizeof(acpi_knowndevs[0]); i++) {
    715 					if (strcmp(acpi_knowndevs[i].pnp,
    716 					    pnpstr) == 0) {
    717 						aprint_normal("[%s] ",
    718 						    acpi_knowndevs[i].str);
    719 					}
    720 				}
    721 			}
    722 
    723 #endif
    724 			aprint_normal("at %s", pnp);
    725 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    726 			aprint_normal("%s (ACPI Object Type '%s' "
    727 			    "[0x%02x]) ", aa->aa_node->ad_name,
    728 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    729 			     aa->aa_node->ad_devinfo->Type);
    730 			aprint_normal("at %s", pnp);
    731 		} else
    732 			return 0;
    733 	} else {
    734 		aprint_normal(" (%s", aa->aa_node->ad_name);
    735 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    736 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    737 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    738 				const char *uid;
    739 
    740 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    741 				if (uid[0] == '\0')
    742 					uid = "<null>";
    743 				aprint_normal("-%s", uid);
    744 			}
    745 		}
    746 		aprint_normal(")");
    747 	}
    748 
    749 	return UNCONF;
    750 }
    751 
    752 /*****************************************************************************
    753  * ACPI fixed-hardware feature handlers
    754  *****************************************************************************/
    755 
    756 static UINT32	acpi_fixed_button_handler(void *);
    757 static void	acpi_fixed_button_pressed(void *);
    758 
    759 /*
    760  * acpi_enable_fixed_events:
    761  *
    762  *	Enable any fixed-hardware feature handlers.
    763  */
    764 static void
    765 acpi_enable_fixed_events(struct acpi_softc *sc)
    766 {
    767 	static int beenhere;
    768 	ACPI_STATUS rv;
    769 
    770 	KASSERT(beenhere == 0);
    771 	beenhere = 1;
    772 
    773 	/*
    774 	 * Check for fixed-hardware buttons.
    775 	 */
    776 
    777 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    778 		aprint_verbose("%s: fixed-feature power button present\n",
    779 		    sc->sc_dev.dv_xname);
    780 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    781 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    782 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    783 			aprint_error("%s: unable to register fixed power "
    784 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    785 		} else {
    786 			rv = AcpiInstallFixedEventHandler(
    787 			    ACPI_EVENT_POWER_BUTTON,
    788 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    789 			if (ACPI_FAILURE(rv)) {
    790 				aprint_error("%s: unable to install handler "
    791 				    "for fixed power button: %s\n",
    792 				    sc->sc_dev.dv_xname,
    793 				    AcpiFormatException(rv));
    794 			}
    795 		}
    796 	}
    797 
    798 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    799 		aprint_verbose("%s: fixed-feature sleep button present\n",
    800 		    sc->sc_dev.dv_xname);
    801 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    802 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    803 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    804 			aprint_error("%s: unable to register fixed sleep "
    805 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    806 		} else {
    807 			rv = AcpiInstallFixedEventHandler(
    808 			    ACPI_EVENT_SLEEP_BUTTON,
    809 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    810 			if (ACPI_FAILURE(rv)) {
    811 				aprint_error("%s: unable to install handler "
    812 				    "for fixed sleep button: %s\n",
    813 				    sc->sc_dev.dv_xname,
    814 				    AcpiFormatException(rv));
    815 			}
    816 		}
    817 	}
    818 }
    819 
    820 /*
    821  * acpi_fixed_button_handler:
    822  *
    823  *	Event handler for the fixed buttons.
    824  */
    825 static UINT32
    826 acpi_fixed_button_handler(void *context)
    827 {
    828 	struct sysmon_pswitch *smpsw = context;
    829 	int rv;
    830 
    831 #ifdef ACPI_BUT_DEBUG
    832 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    833 #endif
    834 
    835 	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    836 	    acpi_fixed_button_pressed, smpsw);
    837 	if (ACPI_FAILURE(rv))
    838 		printf("%s: WARNING: unable to queue fixed button pressed "
    839 		    "callback: %s\n", smpsw->smpsw_name,
    840 		    AcpiFormatException(rv));
    841 
    842 	return ACPI_INTERRUPT_HANDLED;
    843 }
    844 
    845 /*
    846  * acpi_fixed_button_pressed:
    847  *
    848  *	Deal with a fixed button being pressed.
    849  */
    850 static void
    851 acpi_fixed_button_pressed(void *context)
    852 {
    853 	struct sysmon_pswitch *smpsw = context;
    854 
    855 #ifdef ACPI_BUT_DEBUG
    856 	printf("%s: fixed button pressed, calling sysmon\n",
    857 	    smpsw->smpsw_name);
    858 #endif
    859 
    860 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    861 }
    862 
    863 /*****************************************************************************
    864  * ACPI utility routines.
    865  *****************************************************************************/
    866 
    867 /*
    868  * acpi_eval_integer:
    869  *
    870  *	Evaluate an integer object.
    871  */
    872 ACPI_STATUS
    873 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    874 {
    875 	ACPI_STATUS rv;
    876 	ACPI_BUFFER buf;
    877 	ACPI_OBJECT param;
    878 
    879 	if (handle == NULL)
    880 		handle = ACPI_ROOT_OBJECT;
    881 
    882 	buf.Pointer = &param;
    883 	buf.Length = sizeof(param);
    884 
    885 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    886 	if (ACPI_SUCCESS(rv))
    887 		*valp = param.Integer.Value;
    888 
    889 	return rv;
    890 }
    891 
    892 /*
    893  * acpi_eval_string:
    894  *
    895  *	Evaluate a (Unicode) string object.
    896  */
    897 ACPI_STATUS
    898 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    899 {
    900 	ACPI_STATUS rv;
    901 	ACPI_BUFFER buf;
    902 
    903 	if (handle == NULL)
    904 		handle = ACPI_ROOT_OBJECT;
    905 
    906 	buf.Pointer = NULL;
    907 	buf.Length = ACPI_ALLOCATE_BUFFER;
    908 
    909 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    910 	if (ACPI_SUCCESS(rv)) {
    911 		ACPI_OBJECT *param = buf.Pointer;
    912 		const char *ptr = param->String.Pointer;
    913 		size_t len = param->String.Length;
    914 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    915 			rv = AE_NO_MEMORY;
    916 		else
    917 			(void)memcpy(*stringp, ptr, len);
    918 		AcpiOsFree(param);
    919 	}
    920 
    921 	return rv;
    922 }
    923 
    924 
    925 /*
    926  * acpi_eval_struct:
    927  *
    928  *	Evaluate a more complex structure.
    929  *	Caller must free buf.Pointer by AcpiOsFree().
    930  */
    931 ACPI_STATUS
    932 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
    933 {
    934 	ACPI_STATUS rv;
    935 
    936 	if (handle == NULL)
    937 		handle = ACPI_ROOT_OBJECT;
    938 
    939 	bufp->Pointer = NULL;
    940 	bufp->Length = ACPI_ALLOCATE_BUFFER;
    941 
    942 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    943 
    944 	return rv;
    945 }
    946 
    947 /*
    948  * acpi_foreach_package_object:
    949  *
    950  *	Iterate over all objects in a in a packages and pass then all
    951  *	to a function. If the called function returns non AE_OK, the
    952  *	iteration is stopped and that value is returned.
    953  */
    954 
    955 ACPI_STATUS
    956 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    957     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
    958     void *arg)
    959 {
    960 	ACPI_STATUS rv = AE_OK;
    961 	int i;
    962 
    963 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
    964 		return AE_BAD_PARAMETER;
    965 
    966 	for (i = 0; i < pkg->Package.Count; i++) {
    967 		rv = (*func)(&pkg->Package.Elements[i], arg);
    968 		if (ACPI_FAILURE(rv))
    969 			break;
    970 	}
    971 
    972 	return rv;
    973 }
    974 
    975 const char *
    976 acpi_name(ACPI_HANDLE handle)
    977 {
    978 	static char buffer[80];
    979 	ACPI_BUFFER buf;
    980 	ACPI_STATUS rv;
    981 
    982 	buf.Length = sizeof(buffer);
    983 	buf.Pointer = buffer;
    984 
    985 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
    986 	if (ACPI_FAILURE(rv))
    987 		return "(unknown acpi path)";
    988 	return buffer;
    989 }
    990 
    991 /*
    992  * acpi_get:
    993  *
    994  *	Fetch data info the specified (empty) ACPI buffer.
    995  *	Caller must free buf.Pointer by AcpiOsFree().
    996  */
    997 ACPI_STATUS
    998 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    999     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1000 {
   1001 	buf->Pointer = NULL;
   1002 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1003 
   1004 	return (*getit)(handle, buf);
   1005 }
   1006 
   1007 
   1008 /*
   1009  * acpi_match_hid
   1010  *
   1011  *	Match given ids against _HID and _CIDs
   1012  */
   1013 int
   1014 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1015 {
   1016 	int i;
   1017 
   1018 	while (*ids) {
   1019 		if (ad->Valid & ACPI_VALID_HID) {
   1020 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1021 				return 1;
   1022 		}
   1023 
   1024 		if (ad->Valid & ACPI_VALID_CID) {
   1025 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1026 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1027 					return 1;
   1028 			}
   1029 		}
   1030 		ids++;
   1031 	}
   1032 
   1033 	return 0;
   1034 }
   1035 
   1036 /*
   1037  * acpi_set_wake_gpe
   1038  *
   1039  *	Set GPE as both Runtime and Wake
   1040  */
   1041 void
   1042 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1043 {
   1044 	ACPI_BUFFER buf;
   1045 	ACPI_STATUS rv;
   1046 	ACPI_OBJECT *p, *elt;
   1047 
   1048 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1049 	if (ACPI_FAILURE(rv))
   1050 		return;			/* just ignore */
   1051 
   1052 	p = buf.Pointer;
   1053 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1054 		goto out;		/* just ignore */
   1055 
   1056 	elt = p->Package.Elements;
   1057 
   1058 	/* TBD: package support */
   1059 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1060 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1061 
   1062  out:
   1063 	AcpiOsFree(buf.Pointer);
   1064 }
   1065 
   1066 
   1067 /*****************************************************************************
   1068  * ACPI sleep support.
   1069  *****************************************************************************/
   1070 
   1071 static int
   1072 is_available_state(struct acpi_softc *sc, int state)
   1073 {
   1074 	UINT8 type_a, type_b;
   1075 
   1076 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1077 				&type_a, &type_b));
   1078 }
   1079 
   1080 /*
   1081  * acpi_enter_sleep_state:
   1082  *
   1083  *	enter to the specified sleep state.
   1084  */
   1085 
   1086 ACPI_STATUS
   1087 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1088 {
   1089 	int s, err;
   1090 	ACPI_STATUS ret = AE_OK;
   1091 
   1092 	if (state == acpi_sleepstate)
   1093 		return AE_OK;
   1094 
   1095 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1096 
   1097 	switch (state) {
   1098 	case ACPI_STATE_S0:
   1099 		break;
   1100 	case ACPI_STATE_S1:
   1101 	case ACPI_STATE_S2:
   1102 	case ACPI_STATE_S3:
   1103 	case ACPI_STATE_S4:
   1104 		if (!is_available_state(sc, state)) {
   1105 			aprint_error("%s: cannot enter the sleep state (%d).\n",
   1106 			    sc->sc_dev.dv_xname, state);
   1107 			break;
   1108 		}
   1109 
   1110 		if (state != ACPI_STATE_S1)
   1111 			pnp_global_transition(PNP_STATE_D3);
   1112 
   1113 		ret = AcpiEnterSleepStatePrep(state);
   1114 		if (ACPI_FAILURE(ret)) {
   1115 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1116 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1117 			break;
   1118 		}
   1119 
   1120 		DELAY(1000000);
   1121 		acpi_sleepstate = state;
   1122 		if (state == ACPI_STATE_S1) {
   1123 			/* just enter the state */
   1124 			acpi_md_OsDisableInterrupt();
   1125 			AcpiEnterSleepState((UINT8)state);
   1126 		} else {
   1127 			s = splhigh();
   1128 			err = acpi_md_sleep(state);
   1129 			if (err == 0 && state == ACPI_STATE_S3)
   1130 				AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
   1131 			splx(s);
   1132 			if (state == ACPI_STATE_S4)
   1133 				AcpiEnable();
   1134 		}
   1135 		AcpiLeaveSleepState((UINT8)state);
   1136 		if (state != ACPI_STATE_S1)
   1137 			pnp_global_transition(PNP_STATE_D0);
   1138 
   1139 		break;
   1140 	case ACPI_STATE_S5:
   1141 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1142 		if (ACPI_FAILURE(ret)) {
   1143 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1144 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1145 			break;
   1146 		}
   1147 		DELAY(1000000);
   1148 		acpi_sleepstate = state;
   1149 		acpi_md_OsDisableInterrupt();
   1150 		AcpiEnterSleepState(ACPI_STATE_S5);
   1151 		aprint_error("%s: WARNING powerdown failed!\n",
   1152 		    sc->sc_dev.dv_xname);
   1153 		break;
   1154 	}
   1155 
   1156 	acpi_sleepstate = ACPI_STATE_S0;
   1157 	return ret;
   1158 }
   1159 
   1160 #if defined(ACPI_ACTIVATE_DEV)
   1161 /* XXX This very incomplete */
   1162 ACPI_STATUS
   1163 acpi_allocate_resources(ACPI_HANDLE handle)
   1164 {
   1165 	ACPI_BUFFER bufp, bufc, bufn;
   1166 	ACPI_RESOURCE *resp, *resc, *resn;
   1167 	ACPI_RESOURCE_IRQ *irq;
   1168 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1169 	ACPI_STATUS rv;
   1170 	uint delta;
   1171 
   1172 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1173 	if (ACPI_FAILURE(rv))
   1174 		goto out;
   1175 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1176 	if (ACPI_FAILURE(rv)) {
   1177 		goto out1;
   1178 	}
   1179 
   1180 	bufn.Length = 1000;
   1181 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1182 	resp = bufp.Pointer;
   1183 	resc = bufc.Pointer;
   1184 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1185 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1186 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1187 			resp = ACPI_NEXT_RESOURCE(resp);
   1188 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1189 			break;
   1190 		/* Found identical Id */
   1191 		resn->Type = resc->Type;
   1192 		switch (resc->Type) {
   1193 		case ACPI_RESOURCE_TYPE_IRQ:
   1194 			memcpy(&resn->Data, &resp->Data,
   1195 			       sizeof(ACPI_RESOURCE_IRQ));
   1196 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1197 			irq->Interrupts[0] =
   1198 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1199 			        Interrupts[irq->InterruptCount-1];
   1200 			irq->InterruptCount = 1;
   1201 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1202 			break;
   1203 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1204 			memcpy(&resn->Data, &resp->Data,
   1205 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1206 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1207 #if 0
   1208 			/*
   1209 			 * XXX not duplicating the interrupt logic above
   1210 			 * because its not clear what it accomplishes.
   1211 			 */
   1212 			xirq->Interrupts[0] =
   1213 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1214 			    Interrupts[irq->NumberOfInterrupts-1];
   1215 			xirq->NumberOfInterrupts = 1;
   1216 #endif
   1217 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1218 			break;
   1219 		case ACPI_RESOURCE_TYPE_IO:
   1220 			memcpy(&resn->Data, &resp->Data,
   1221 			       sizeof(ACPI_RESOURCE_IO));
   1222 			resn->Length = resp->Length;
   1223 			break;
   1224 		default:
   1225 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1226 			rv = AE_BAD_DATA;
   1227 			goto out2;
   1228 		}
   1229 		resc = ACPI_NEXT_RESOURCE(resc);
   1230 		resn = ACPI_NEXT_RESOURCE(resn);
   1231 		resp = ACPI_NEXT_RESOURCE(resp);
   1232 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1233 		if (delta >=
   1234 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1235 			bufn.Length *= 2;
   1236 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1237 					       M_ACPI, M_WAITOK);
   1238 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1239 		}
   1240 	}
   1241 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1242 		printf("acpi_allocate_resources: resc not exhausted\n");
   1243 		rv = AE_BAD_DATA;
   1244 		goto out3;
   1245 	}
   1246 
   1247 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1248 	rv = AcpiSetCurrentResources(handle, &bufn);
   1249 	if (ACPI_FAILURE(rv)) {
   1250 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1251 		       AcpiFormatException(rv));
   1252 	}
   1253 
   1254 out3:
   1255 	free(bufn.Pointer, M_ACPI);
   1256 out2:
   1257 	AcpiOsFree(bufc.Pointer);
   1258 out1:
   1259 	AcpiOsFree(bufp.Pointer);
   1260 out:
   1261 	return rv;
   1262 }
   1263 #endif /* ACPI_ACTIVATE_DEV */
   1264 
   1265 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1266 {
   1267 	const struct sysctlnode *node;
   1268 	const struct sysctlnode *ssnode;
   1269 
   1270 	if (sysctl_createv(clog, 0, NULL, NULL,
   1271 	    CTLFLAG_PERMANENT,
   1272 	    CTLTYPE_NODE, "hw", NULL,
   1273 	    NULL, 0, NULL, 0,
   1274 	    CTL_HW, CTL_EOL) != 0)
   1275 		return;
   1276 
   1277 	if (sysctl_createv(clog, 0, NULL, &node,
   1278 	    CTLFLAG_PERMANENT,
   1279 	    CTLTYPE_NODE, "acpi", NULL,
   1280 	    NULL, 0, NULL, 0,
   1281 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1282 		return;
   1283 
   1284 	acpi_node = node->sysctl_num;
   1285 
   1286 	/* ACPI sleepstate sysctl */
   1287 	if (sysctl_createv(NULL, 0, NULL, &node,
   1288 	    CTLFLAG_PERMANENT,
   1289 	    CTLTYPE_NODE, "machdep", NULL,
   1290 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1291 		return;
   1292 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1293 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1294 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1295 	    CTL_EOL) != 0)
   1296 		return;
   1297 }
   1298 
   1299 static int
   1300 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1301 {
   1302 	int error, t;
   1303 	struct sysctlnode node;
   1304 
   1305 	node = *rnode;
   1306 	t = acpi_sleepstate;
   1307 	node.sysctl_data = &t;
   1308 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1309 	if (error || newp == NULL)
   1310 		return error;
   1311 
   1312 	if (acpi_softc == NULL)
   1313 		return ENOSYS;
   1314 
   1315 	acpi_enter_sleep_state(acpi_softc, t);
   1316 
   1317 	return 0;
   1318 }
   1319