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