Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.7.2.1
      1  1.7.2.1   gehenna /*	$NetBSD: acpi.c,v 1.7.2.1 2002/06/20 16:31:24 gehenna Exp $	*/
      2      1.1   thorpej 
      3      1.1   thorpej /*
      4      1.1   thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5      1.1   thorpej  * All rights reserved.
      6      1.1   thorpej  *
      7      1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8      1.1   thorpej  *
      9      1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10      1.1   thorpej  * modification, are permitted provided that the following conditions
     11      1.1   thorpej  * are met:
     12      1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13      1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14      1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16      1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17      1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18      1.1   thorpej  *    must display the following acknowledgement:
     19      1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20      1.1   thorpej  *	Wasabi Systems, Inc.
     21      1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22      1.1   thorpej  *    or promote products derived from this software without specific prior
     23      1.1   thorpej  *    written permission.
     24      1.1   thorpej  *
     25      1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26      1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29      1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1   thorpej  */
     37      1.1   thorpej 
     38      1.1   thorpej /*
     39      1.1   thorpej  * Autoconfiguration support for the Intel ACPI Component Architecture
     40      1.1   thorpej  * ACPI reference implementation.
     41      1.1   thorpej  */
     42      1.5     lukem 
     43      1.5     lukem #include <sys/cdefs.h>
     44  1.7.2.1   gehenna __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.7.2.1 2002/06/20 16:31:24 gehenna Exp $");
     45      1.1   thorpej 
     46      1.1   thorpej #include <sys/param.h>
     47      1.1   thorpej #include <sys/systm.h>
     48      1.1   thorpej #include <sys/device.h>
     49      1.1   thorpej #include <sys/malloc.h>
     50      1.1   thorpej 
     51      1.1   thorpej #include <dev/acpi/acpica.h>
     52      1.1   thorpej #include <dev/acpi/acpireg.h>
     53      1.1   thorpej #include <dev/acpi/acpivar.h>
     54      1.1   thorpej #include <dev/acpi/acpi_osd.h>
     55      1.1   thorpej 
     56      1.1   thorpej #ifdef ENABLE_DEBUGGER
     57      1.1   thorpej #define	ACPI_DBGR_INIT		0x01
     58      1.1   thorpej #define	ACPI_DBGR_TABLES	0x02
     59      1.1   thorpej #define	ACPI_DBGR_ENABLE	0x04
     60      1.1   thorpej #define	ACPI_DBGR_PROBE		0x08
     61      1.1   thorpej #define	ACPI_DBGR_RUNNING	0x10
     62      1.1   thorpej 
     63      1.1   thorpej int	acpi_dbgr = 0x00;
     64      1.1   thorpej #endif
     65      1.1   thorpej 
     66      1.1   thorpej int	acpi_match(struct device *, struct cfdata *, void *);
     67      1.1   thorpej void	acpi_attach(struct device *, struct device *, void *);
     68      1.1   thorpej 
     69      1.1   thorpej int	acpi_print(void *aux, const char *);
     70      1.1   thorpej 
     71      1.1   thorpej extern struct cfdriver acpi_cd;
     72      1.1   thorpej 
     73      1.1   thorpej struct cfattach acpi_ca = {
     74      1.1   thorpej 	sizeof(struct acpi_softc), acpi_match, acpi_attach,
     75      1.1   thorpej };
     76      1.1   thorpej 
     77      1.1   thorpej /*
     78      1.1   thorpej  * This is a flag we set when the ACPI subsystem is active.  Machine
     79      1.1   thorpej  * dependent code may wish to skip other steps (such as attaching
     80      1.1   thorpej  * subsystems that ACPI supercedes) when ACPI is active.
     81      1.1   thorpej  */
     82      1.1   thorpej int	acpi_active;
     83      1.1   thorpej 
     84      1.1   thorpej /*
     85      1.1   thorpej  * Pointer to the ACPI subsystem's state.  There can be only
     86      1.1   thorpej  * one ACPI instance.
     87      1.1   thorpej  */
     88      1.1   thorpej struct acpi_softc *acpi_softc;
     89      1.1   thorpej 
     90      1.1   thorpej void		acpi_shutdown(void *);
     91      1.1   thorpej ACPI_STATUS	acpi_disable(struct acpi_softc *sc);
     92      1.1   thorpej void		acpi_build_tree(struct acpi_softc *);
     93      1.1   thorpej ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
     94      1.1   thorpej 
     95      1.1   thorpej void		acpi_enable_fixed_events(struct acpi_softc *);
     96      1.1   thorpej 
     97      1.1   thorpej /*
     98      1.1   thorpej  * acpi_probe:
     99      1.1   thorpej  *
    100      1.1   thorpej  *	Probe for ACPI support.  This is called by the
    101      1.1   thorpej  *	machine-dependent ACPI front-end.  All of the
    102      1.1   thorpej  *	actual work is done by ACPICA.
    103      1.1   thorpej  *
    104      1.1   thorpej  *	NOTE: This is not an autoconfiguration interface function.
    105      1.1   thorpej  */
    106      1.1   thorpej int
    107      1.1   thorpej acpi_probe(void)
    108      1.1   thorpej {
    109      1.1   thorpej 	static int beenhere;
    110      1.1   thorpej 	ACPI_STATUS rv;
    111      1.1   thorpej 
    112      1.1   thorpej 	if (beenhere != 0)
    113      1.1   thorpej 		panic("acpi_probe: ACPI has already been probed");
    114      1.1   thorpej 	beenhere = 1;
    115      1.1   thorpej 
    116      1.1   thorpej 	/*
    117      1.1   thorpej 	 * Start up ACPICA.
    118      1.1   thorpej 	 */
    119      1.1   thorpej #ifdef ENABLE_DEBUGGER
    120      1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_INIT)
    121      1.1   thorpej 		acpi_osd_debugger();
    122      1.1   thorpej #endif
    123      1.1   thorpej 
    124      1.1   thorpej 	rv = AcpiInitializeSubsystem();
    125      1.1   thorpej 	if (rv != AE_OK) {
    126      1.1   thorpej 		printf("ACPI: unable to initialize ACPICA: %d\n", rv);
    127      1.1   thorpej 		return (0);
    128      1.1   thorpej 	}
    129      1.1   thorpej 
    130      1.1   thorpej #ifdef ENABLE_DEBUGGER
    131      1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    132      1.1   thorpej 		acpi_osd_debugger();
    133      1.1   thorpej #endif
    134      1.1   thorpej 
    135      1.1   thorpej 	rv = AcpiLoadTables();
    136      1.1   thorpej 	if (rv != AE_OK) {
    137      1.1   thorpej 		printf("ACPI: unable to load tables: %d\n", rv);
    138      1.1   thorpej 		return (0);
    139      1.1   thorpej 	}
    140      1.1   thorpej 
    141      1.1   thorpej 	/*
    142      1.1   thorpej 	 * Looks like we have ACPI!
    143      1.1   thorpej 	 */
    144      1.1   thorpej 
    145      1.1   thorpej 	return (1);
    146      1.1   thorpej }
    147      1.1   thorpej 
    148      1.1   thorpej /*
    149      1.1   thorpej  * acpi_match:
    150      1.1   thorpej  *
    151      1.1   thorpej  *	Autoconfiguration `match' routine.
    152      1.1   thorpej  */
    153      1.1   thorpej int
    154      1.1   thorpej acpi_match(struct device *parent, struct cfdata *match, void *aux)
    155      1.1   thorpej {
    156      1.1   thorpej 	struct acpibus_attach_args *aa = aux;
    157      1.1   thorpej 
    158      1.1   thorpej 	if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0)
    159      1.1   thorpej 		return (0);
    160      1.1   thorpej 
    161      1.1   thorpej 	/*
    162      1.1   thorpej 	 * XXX Check other locators?  Hard to know -- machine
    163      1.1   thorpej 	 * dependent code has already checked for the presence
    164      1.1   thorpej 	 * of ACPI by calling acpi_probe(), so I suppose we
    165      1.1   thorpej 	 * don't really have to do anything else.
    166      1.1   thorpej 	 */
    167      1.1   thorpej 	return (1);
    168      1.1   thorpej }
    169      1.1   thorpej 
    170      1.1   thorpej /*
    171      1.1   thorpej  * acpi_attach:
    172      1.1   thorpej  *
    173      1.1   thorpej  *	Autoconfiguration `attach' routine.  Finish initializing
    174      1.1   thorpej  *	ACPICA (some initialization was done in acpi_probe(),
    175      1.1   thorpej  *	which was required to check for the presence of ACPI),
    176      1.1   thorpej  *	and enable the ACPI subsystem.
    177      1.1   thorpej  */
    178      1.1   thorpej void
    179      1.1   thorpej acpi_attach(struct device *parent, struct device *self, void *aux)
    180      1.1   thorpej {
    181      1.1   thorpej 	struct acpi_softc *sc = (void *) self;
    182      1.1   thorpej 	struct acpibus_attach_args *aa = aux;
    183      1.1   thorpej 	ACPI_STATUS rv;
    184      1.1   thorpej 
    185      1.1   thorpej 	printf("\n");
    186      1.1   thorpej 
    187      1.1   thorpej 	if (acpi_softc != NULL)
    188      1.1   thorpej 		panic("acpi_attach: ACPI has already been attached");
    189      1.1   thorpej 
    190      1.1   thorpej 	sc->sc_iot = aa->aa_iot;
    191      1.1   thorpej 	sc->sc_memt = aa->aa_memt;
    192      1.1   thorpej 	sc->sc_pc = aa->aa_pc;
    193      1.1   thorpej 	sc->sc_pciflags = aa->aa_pciflags;
    194      1.1   thorpej 
    195      1.1   thorpej 	acpi_softc = sc;
    196      1.1   thorpej 
    197      1.1   thorpej 	/*
    198      1.1   thorpej 	 * Install the default address space handlers.
    199      1.1   thorpej 	 */
    200      1.1   thorpej 
    201      1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    202      1.1   thorpej 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    203      1.1   thorpej 	if (rv != AE_OK) {
    204      1.1   thorpej 		printf("%s: unable to install SYSTEM MEMORY handler: %d\n",
    205      1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    206      1.1   thorpej 		return;
    207      1.1   thorpej 	}
    208      1.1   thorpej 
    209      1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    210      1.1   thorpej 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    211      1.1   thorpej 	if (rv != AE_OK) {
    212      1.1   thorpej 		printf("%s: unable to install SYSTEM IO handler: %d\n",
    213      1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    214      1.1   thorpej 		return;
    215      1.1   thorpej 	}
    216      1.1   thorpej 
    217      1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    218      1.1   thorpej 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    219      1.1   thorpej 	if (rv != AE_OK) {
    220      1.1   thorpej 		printf("%s: unable to install PCI CONFIG handler: %d\n",
    221      1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    222      1.1   thorpej 		return;
    223      1.1   thorpej 	}
    224      1.1   thorpej 
    225      1.1   thorpej 	/*
    226      1.1   thorpej 	 * Bring ACPI on-line.
    227      1.1   thorpej 	 *
    228      1.1   thorpej 	 * Note that we request that _STA (device init) and _INI (object init)
    229      1.1   thorpej 	 * methods not be run.
    230      1.1   thorpej 	 *
    231      1.1   thorpej 	 * XXX We need to arrange for the object init pass after we have
    232      1.1   thorpej 	 * XXX attached all of our children.
    233      1.1   thorpej 	 */
    234      1.1   thorpej #ifdef ENABLE_DEBUGGER
    235      1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    236      1.1   thorpej 		acpi_osd_debugger();
    237      1.1   thorpej #endif
    238      1.1   thorpej 	rv = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
    239      1.1   thorpej 	if (rv != AE_OK) {
    240      1.1   thorpej 		printf("%s: unable to enable ACPI: %d\n",
    241      1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    242      1.1   thorpej 		return;
    243      1.1   thorpej 	}
    244      1.1   thorpej 	acpi_active = 1;
    245      1.1   thorpej 
    246      1.1   thorpej 	/*
    247      1.1   thorpej 	 * Set up the default sleep state to enter when various
    248      1.1   thorpej 	 * switches are activated.
    249      1.1   thorpej 	 */
    250      1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_POWERBUTTON] = ACPI_STATE_S5;
    251      1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_SLEEPBUTTON] = ACPI_STATE_S1;
    252      1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_LID]	     = ACPI_STATE_S1;
    253      1.1   thorpej 
    254      1.1   thorpej 	/* Our current state is "awake". */
    255      1.1   thorpej 	sc->sc_sleepstate = ACPI_STATE_S0;
    256      1.1   thorpej 
    257  1.7.2.1   gehenna 	/* Show SCI interrupt. */
    258  1.7.2.1   gehenna 	if (AcpiGbl_FADT != NULL)
    259  1.7.2.1   gehenna 		printf("%s: SCI interrupting at irq %d\n",
    260  1.7.2.1   gehenna 			sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    261      1.1   thorpej 	/*
    262      1.1   thorpej 	 * Check for fixed-hardware features.
    263      1.1   thorpej 	 */
    264      1.1   thorpej 	acpi_enable_fixed_events(sc);
    265      1.1   thorpej 
    266      1.1   thorpej 	/*
    267      1.1   thorpej 	 * Scan the namespace and build our device tree.
    268      1.1   thorpej 	 */
    269      1.1   thorpej #ifdef ENABLE_DEBUGGER
    270      1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    271      1.1   thorpej 		acpi_osd_debugger();
    272      1.1   thorpej #endif
    273      1.1   thorpej 	acpi_build_tree(sc);
    274      1.1   thorpej 
    275      1.1   thorpej 	/*
    276      1.1   thorpej 	 * Register a shutdown hook that disables certain ACPI
    277      1.1   thorpej 	 * events that might happen and confuse us while we're
    278      1.1   thorpej 	 * trying to shut down.
    279      1.1   thorpej 	 */
    280      1.1   thorpej 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    281      1.1   thorpej 	if (sc->sc_sdhook == NULL)
    282      1.1   thorpej 		printf("%s: WARNING: unable to register shutdown hook\n",
    283      1.1   thorpej 		    sc->sc_dev.dv_xname);
    284      1.1   thorpej 
    285      1.1   thorpej #ifdef ENABLE_DEBUGGER
    286      1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    287      1.1   thorpej 		acpi_osd_debugger();
    288      1.1   thorpej #endif
    289      1.1   thorpej }
    290      1.1   thorpej 
    291      1.1   thorpej /*
    292      1.1   thorpej  * acpi_shutdown:
    293      1.1   thorpej  *
    294      1.1   thorpej  *	Shutdown hook for ACPI -- disable some events that
    295      1.1   thorpej  *	might confuse us.
    296      1.1   thorpej  */
    297      1.1   thorpej void
    298      1.1   thorpej acpi_shutdown(void *arg)
    299      1.1   thorpej {
    300      1.1   thorpej 	struct acpi_softc *sc = arg;
    301      1.1   thorpej 
    302      1.1   thorpej 	if (acpi_disable(sc) != AE_OK)
    303      1.1   thorpej 		printf("%s: WARNING: unable to disable ACPI\n",
    304      1.1   thorpej 		    sc->sc_dev.dv_xname);
    305      1.1   thorpej }
    306      1.1   thorpej 
    307      1.1   thorpej /*
    308      1.1   thorpej  * acpi_disable:
    309      1.1   thorpej  *
    310      1.1   thorpej  *	Disable ACPI.
    311      1.1   thorpej  */
    312      1.1   thorpej ACPI_STATUS
    313      1.1   thorpej acpi_disable(struct acpi_softc *sc)
    314      1.1   thorpej {
    315      1.1   thorpej 	ACPI_STATUS rv = AE_OK;
    316      1.1   thorpej 
    317      1.1   thorpej 	if (acpi_active) {
    318      1.1   thorpej 		rv = AcpiDisable();
    319      1.1   thorpej 		if (rv == AE_OK)
    320      1.1   thorpej 			acpi_active = 0;
    321      1.1   thorpej 	}
    322      1.1   thorpej 	return (rv);
    323      1.1   thorpej }
    324      1.1   thorpej 
    325      1.1   thorpej struct acpi_make_devnode_state {
    326      1.1   thorpej 	struct acpi_softc *softc;
    327      1.1   thorpej 	struct acpi_scope *scope;
    328      1.1   thorpej };
    329      1.1   thorpej 
    330      1.1   thorpej /*
    331      1.1   thorpej  * acpi_build_tree:
    332      1.1   thorpej  *
    333      1.1   thorpej  *	Scan relevant portions of the ACPI namespace and attach
    334      1.1   thorpej  *	child devices.
    335      1.1   thorpej  */
    336      1.1   thorpej void
    337      1.1   thorpej acpi_build_tree(struct acpi_softc *sc)
    338      1.1   thorpej {
    339      1.1   thorpej 	static const char *scopes[] = {
    340      1.1   thorpej 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    341      1.1   thorpej 		"\\_SB_",	/* system bus namespace */
    342      1.1   thorpej 		"\\_SI_",	/* system idicator namespace */
    343      1.1   thorpej 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    344      1.1   thorpej 		NULL,
    345      1.1   thorpej 	};
    346      1.1   thorpej 	struct acpi_attach_args aa;
    347      1.1   thorpej 	struct acpi_make_devnode_state state;
    348      1.1   thorpej 	struct acpi_scope *as;
    349      1.1   thorpej 	struct acpi_devnode *ad;
    350      1.1   thorpej 	ACPI_HANDLE parent;
    351      1.1   thorpej 	int i;
    352      1.1   thorpej 
    353      1.1   thorpej 	TAILQ_INIT(&sc->sc_scopes);
    354      1.1   thorpej 
    355      1.1   thorpej 	state.softc = sc;
    356      1.1   thorpej 
    357      1.1   thorpej 	/*
    358      1.1   thorpej 	 * Scan the namespace and build our tree.
    359      1.1   thorpej 	 */
    360      1.1   thorpej 	for (i = 0; scopes[i] != NULL; i++) {
    361      1.1   thorpej 		as = malloc(sizeof(*as), M_DEVBUF, M_WAITOK);
    362      1.1   thorpej 		as->as_name = scopes[i];
    363      1.1   thorpej 		TAILQ_INIT(&as->as_devnodes);
    364      1.1   thorpej 
    365      1.1   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    366      1.1   thorpej 
    367      1.1   thorpej 		state.scope = as;
    368      1.1   thorpej 
    369      1.1   thorpej 		if (AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i],
    370      1.1   thorpej 		    &parent) == AE_OK) {
    371      1.1   thorpej 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    372      1.1   thorpej 			    acpi_make_devnode, &state, NULL);
    373      1.1   thorpej 		}
    374      1.1   thorpej 
    375      1.1   thorpej 		/* Now, for this namespace, try and attach the devices. */
    376      1.1   thorpej 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    377      1.1   thorpej 			aa.aa_node = ad;
    378      1.1   thorpej 			aa.aa_iot = sc->sc_iot;
    379      1.1   thorpej 			aa.aa_memt = sc->sc_memt;
    380      1.1   thorpej 			aa.aa_pc = sc->sc_pc;
    381      1.1   thorpej 			aa.aa_pciflags = sc->sc_pciflags;
    382      1.1   thorpej 
    383      1.1   thorpej 			/*
    384      1.1   thorpej 			 * XXX We only attach devices which are:
    385      1.1   thorpej 			 *
    386      1.1   thorpej 			 *	- present
    387      1.1   thorpej 			 *	- enabled
    388      1.1   thorpej 			 *	- functioning properly
    389      1.1   thorpej 			 *
    390      1.1   thorpej 			 * However, if enabled, it's decoding resources,
    391      1.1   thorpej 			 * so we should claim them, if possible.  Requires
    392      1.1   thorpej 			 * changes to bus_space(9).
    393      1.1   thorpej 			 */
    394      1.1   thorpej 			if ((ad->ad_devinfo.CurrentStatus &
    395      1.1   thorpej 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    396  1.7.2.1   gehenna 			      ACPI_STA_DEV_OK)) !=
    397      1.1   thorpej 			    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    398  1.7.2.1   gehenna 			     ACPI_STA_DEV_OK))
    399      1.1   thorpej 				continue;
    400      1.1   thorpej 
    401      1.1   thorpej 			/*
    402      1.1   thorpej 			 * XXX Same problem as above...
    403      1.1   thorpej 			 */
    404      1.1   thorpej 			if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
    405      1.1   thorpej 				continue;
    406      1.1   thorpej 
    407      1.1   thorpej 			ad->ad_device = config_found(&sc->sc_dev,
    408      1.1   thorpej 			    &aa, acpi_print);
    409      1.1   thorpej 		}
    410      1.1   thorpej 	}
    411      1.1   thorpej }
    412      1.1   thorpej 
    413      1.1   thorpej /*
    414      1.1   thorpej  * acpi_make_devnode:
    415      1.1   thorpej  *
    416      1.1   thorpej  *	Make an ACPI devnode.
    417      1.1   thorpej  */
    418      1.1   thorpej ACPI_STATUS
    419      1.1   thorpej acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    420      1.1   thorpej     void **status)
    421      1.1   thorpej {
    422      1.1   thorpej 	struct acpi_make_devnode_state *state = context;
    423      1.4   thorpej #ifdef ACPI_DEBUG
    424      1.1   thorpej 	struct acpi_softc *sc = state->softc;
    425      1.4   thorpej #endif
    426      1.1   thorpej 	struct acpi_scope *as = state->scope;
    427      1.1   thorpej 	struct acpi_devnode *ad;
    428      1.1   thorpej 	ACPI_OBJECT_TYPE type;
    429      1.1   thorpej 	ACPI_STATUS rv;
    430      1.1   thorpej 
    431      1.1   thorpej 	if (AcpiGetType(handle, &type) == AE_OK) {
    432      1.1   thorpej 		switch (type) {
    433      1.1   thorpej 		case ACPI_TYPE_DEVICE:
    434      1.1   thorpej 		case ACPI_TYPE_PROCESSOR:
    435      1.1   thorpej 		case ACPI_TYPE_THERMAL:
    436      1.1   thorpej 		case ACPI_TYPE_POWER:
    437      1.6   tsutsui 			ad = malloc(sizeof(*ad), M_DEVBUF, M_NOWAIT|M_ZERO);
    438      1.1   thorpej 			if (ad == NULL)
    439      1.1   thorpej 				return (AE_NO_MEMORY);
    440      1.1   thorpej 
    441      1.1   thorpej 			ad->ad_handle = handle;
    442      1.1   thorpej 			ad->ad_level = level;
    443      1.1   thorpej 			ad->ad_scope = as;
    444      1.1   thorpej 			ad->ad_type = type;
    445      1.1   thorpej 
    446      1.1   thorpej 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    447      1.1   thorpej 
    448      1.1   thorpej 			rv = AcpiGetObjectInfo(handle, &ad->ad_devinfo);
    449      1.1   thorpej 			if (rv != AE_OK)
    450      1.1   thorpej 				goto out;
    451      1.1   thorpej 
    452      1.1   thorpej 			if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
    453      1.1   thorpej 				goto out;
    454      1.1   thorpej 
    455      1.1   thorpej #ifdef ACPI_DEBUG
    456      1.1   thorpej 			printf("%s: HID %s found in scope %s level %d\n",
    457      1.1   thorpej 			    sc->sc_dev.dv_xname, ad->ad_devinfo.HardwareId,
    458      1.1   thorpej 			    as->as_name, ad->ad_level);
    459      1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_UID)
    460      1.1   thorpej 				printf("       UID %s\n",
    461      1.1   thorpej 				    ad->ad_devinfo.UniqueId);
    462      1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_ADR)
    463      1.1   thorpej 				printf("       ADR 0x%016qx\n",
    464      1.1   thorpej 				    ad->ad_devinfo.Address);
    465      1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_STA)
    466      1.1   thorpej 				printf("       STA 0x%08x\n",
    467      1.1   thorpej 				    ad->ad_devinfo.CurrentStatus);
    468      1.1   thorpej #endif
    469      1.1   thorpej 		}
    470      1.1   thorpej 	}
    471      1.1   thorpej  out:
    472      1.1   thorpej 	return (AE_OK);
    473      1.1   thorpej }
    474      1.1   thorpej 
    475      1.1   thorpej /*
    476      1.1   thorpej  * acpi_print:
    477      1.1   thorpej  *
    478      1.1   thorpej  *	Autoconfiguration print routine.
    479      1.1   thorpej  */
    480      1.1   thorpej int
    481      1.1   thorpej acpi_print(void *aux, const char *pnp)
    482      1.1   thorpej {
    483      1.1   thorpej 	struct acpi_attach_args *aa = aux;
    484      1.7  sommerfe #if 0
    485      1.4   thorpej 	char *str;
    486      1.7  sommerfe #endif
    487      1.1   thorpej 
    488      1.4   thorpej 	if (pnp) {
    489      1.4   thorpej 		printf("%s ", aa->aa_node->ad_devinfo.HardwareId);
    490      1.7  sommerfe #if 0 /* Not until we fix acpi_eval_string */
    491      1.4   thorpej 		if (acpi_eval_string(aa->aa_node->ad_handle,
    492      1.4   thorpej 		    "_STR", &str) == AE_OK) {
    493      1.4   thorpej 			printf("[%s] ", str);
    494      1.4   thorpej 			AcpiOsFree(str);
    495      1.4   thorpej 		}
    496      1.7  sommerfe #endif
    497      1.4   thorpej 		printf("at %s", pnp);
    498      1.4   thorpej 	}
    499      1.1   thorpej 
    500      1.1   thorpej 	return (UNCONF);
    501      1.1   thorpej }
    502      1.1   thorpej 
    503      1.1   thorpej /*****************************************************************************
    504      1.1   thorpej  * ACPI fixed-hardware feature handlers
    505      1.1   thorpej  *****************************************************************************/
    506      1.1   thorpej 
    507      1.1   thorpej UINT32		acpi_fixed_power_button_handler(void *);
    508      1.1   thorpej UINT32		acpi_fixed_sleep_button_handler(void *);
    509      1.1   thorpej 
    510      1.1   thorpej /*
    511      1.1   thorpej  * acpi_enable_fixed_events:
    512      1.1   thorpej  *
    513      1.1   thorpej  *	Enable any fixed-hardware feature handlers.
    514      1.1   thorpej  */
    515      1.1   thorpej void
    516      1.1   thorpej acpi_enable_fixed_events(struct acpi_softc *sc)
    517      1.1   thorpej {
    518      1.1   thorpej 	static int beenhere;
    519      1.1   thorpej 	ACPI_STATUS rv;
    520      1.1   thorpej 
    521      1.1   thorpej 	/*
    522      1.1   thorpej 	 * Check for fixed-hardware buttons.
    523      1.1   thorpej 	 */
    524      1.1   thorpej 
    525      1.1   thorpej 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    526      1.1   thorpej 		if (beenhere == 0)
    527      1.1   thorpej 			printf("%s: fixed-feature power button present\n",
    528      1.1   thorpej 			    sc->sc_dev.dv_xname);
    529      1.1   thorpej 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
    530      1.1   thorpej 		    acpi_fixed_power_button_handler, sc);
    531      1.1   thorpej 		if (rv != AE_OK)
    532      1.1   thorpej 			printf("%s: unable to install handler for fixed "
    533      1.1   thorpej 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    534      1.1   thorpej 	}
    535      1.1   thorpej 
    536      1.1   thorpej 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    537      1.1   thorpej 		if (beenhere == 0)
    538      1.1   thorpej 			printf("%s: fixed-feature sleep button present\n",
    539      1.1   thorpej 			    sc->sc_dev.dv_xname);
    540      1.1   thorpej 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
    541      1.1   thorpej 		    acpi_fixed_sleep_button_handler, sc);
    542      1.1   thorpej 		if (rv != AE_OK)
    543      1.1   thorpej 			printf("%s: unable to install handler for fixed "
    544      1.1   thorpej 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    545      1.1   thorpej 	}
    546      1.1   thorpej 
    547      1.1   thorpej 	beenhere = 1;
    548      1.1   thorpej }
    549      1.1   thorpej 
    550      1.1   thorpej /*
    551      1.1   thorpej  * acpi_fixed_power_button_handler:
    552      1.1   thorpej  *
    553      1.1   thorpej  *	Fixed event handler for the power button.
    554      1.1   thorpej  */
    555      1.1   thorpej UINT32
    556      1.1   thorpej acpi_fixed_power_button_handler(void *context)
    557      1.1   thorpej {
    558      1.1   thorpej 	struct acpi_softc *sc = context;
    559      1.1   thorpej 
    560      1.1   thorpej 	/* XXX XXX XXX */
    561      1.1   thorpej 
    562      1.1   thorpej 	printf("%s: fixed power button pressed\n", sc->sc_dev.dv_xname);
    563      1.1   thorpej 
    564  1.7.2.1   gehenna 	return (ACPI_INTERRUPT_HANDLED);
    565      1.1   thorpej }
    566      1.1   thorpej 
    567      1.1   thorpej /*
    568      1.1   thorpej  * acpi_fixed_sleep_button_handler:
    569      1.1   thorpej  *
    570      1.1   thorpej  *	Fixed event handler for the sleep button.
    571      1.1   thorpej  */
    572      1.1   thorpej UINT32
    573      1.1   thorpej acpi_fixed_sleep_button_handler(void *context)
    574      1.1   thorpej {
    575      1.1   thorpej 	struct acpi_softc *sc = context;
    576      1.1   thorpej 
    577      1.1   thorpej 	/* XXX XXX XXX */
    578      1.1   thorpej 
    579      1.1   thorpej 	printf("%s: fixed sleep button pressed\n", sc->sc_dev.dv_xname);
    580      1.1   thorpej 
    581  1.7.2.1   gehenna 	return (ACPI_INTERRUPT_HANDLED);
    582      1.1   thorpej }
    583      1.1   thorpej 
    584      1.1   thorpej /*****************************************************************************
    585      1.1   thorpej  * ACPI utility routines.
    586      1.1   thorpej  *****************************************************************************/
    587      1.1   thorpej 
    588      1.2   thorpej /*
    589      1.2   thorpej  * acpi_eval_integer:
    590      1.2   thorpej  *
    591      1.2   thorpej  *	Evaluate an integer object.
    592      1.2   thorpej  */
    593      1.1   thorpej ACPI_STATUS
    594      1.1   thorpej acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
    595      1.1   thorpej {
    596      1.1   thorpej 	ACPI_STATUS rv;
    597      1.1   thorpej 	ACPI_BUFFER buf;
    598      1.1   thorpej 	ACPI_OBJECT param;
    599      1.1   thorpej 
    600      1.1   thorpej 	if (handle == NULL)
    601      1.1   thorpej 		handle = ACPI_ROOT_OBJECT;
    602      1.1   thorpej 
    603      1.1   thorpej 	buf.Pointer = &param;
    604      1.1   thorpej 	buf.Length = sizeof(param);
    605      1.1   thorpej 
    606      1.1   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    607      1.1   thorpej 	if (rv == AE_OK) {
    608      1.1   thorpej 		if (param.Type == ACPI_TYPE_INTEGER)
    609      1.1   thorpej 			*valp = param.Integer.Value;
    610      1.1   thorpej 		else
    611      1.1   thorpej 			rv = AE_TYPE;
    612      1.1   thorpej 	}
    613      1.1   thorpej 
    614      1.4   thorpej 	return (rv);
    615      1.4   thorpej }
    616      1.4   thorpej 
    617      1.7  sommerfe #if 0
    618      1.4   thorpej /*
    619      1.4   thorpej  * acpi_eval_string:
    620      1.4   thorpej  *
    621      1.7  sommerfe  *	Evaluate a (Unicode) string object.
    622      1.7  sommerfe  * XXX current API may leak memory, so don't use this.
    623      1.4   thorpej  */
    624      1.4   thorpej ACPI_STATUS
    625      1.4   thorpej acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
    626      1.4   thorpej {
    627      1.4   thorpej 	ACPI_STATUS rv;
    628      1.4   thorpej 	ACPI_BUFFER buf;
    629      1.7  sommerfe 	ACPI_OBJECT *param;
    630      1.4   thorpej 
    631      1.4   thorpej 	if (handle == NULL)
    632      1.4   thorpej 		handle = ACPI_ROOT_OBJECT;
    633      1.4   thorpej 
    634      1.4   thorpej 	buf.Pointer = NULL;
    635      1.4   thorpej 	buf.Length = 0;
    636      1.4   thorpej 
    637      1.4   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    638      1.4   thorpej 	if (rv != AE_BUFFER_OVERFLOW)
    639      1.4   thorpej 		return (rv);
    640      1.4   thorpej 
    641      1.4   thorpej 	buf.Pointer = AcpiOsAllocate(buf.Length);
    642      1.4   thorpej 	if (buf.Pointer == NULL)
    643      1.4   thorpej 		return (AE_NO_MEMORY);
    644      1.4   thorpej 
    645      1.4   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    646      1.7  sommerfe 	param = (ACPI_OBJECT *)buf.Pointer;
    647      1.4   thorpej 	if (rv == AE_OK) {
    648      1.7  sommerfe 		if (param->Type == ACPI_TYPE_STRING) {
    649      1.7  sommerfe 			/* XXX may leak buf.Pointer!! */
    650      1.7  sommerfe 			*stringp = param->String.Pointer;
    651      1.4   thorpej 			return (AE_OK);
    652      1.4   thorpej 		}
    653      1.4   thorpej 		rv = AE_TYPE;
    654      1.4   thorpej 	}
    655      1.4   thorpej 
    656      1.4   thorpej 	AcpiOsFree(buf.Pointer);
    657      1.7  sommerfe 	return (rv);
    658      1.7  sommerfe }
    659      1.7  sommerfe #endif
    660      1.7  sommerfe 
    661      1.7  sommerfe 
    662      1.7  sommerfe /*
    663      1.7  sommerfe  * acpi_eval_struct:
    664      1.7  sommerfe  *
    665      1.7  sommerfe  *	Evaluate a more complex structure.  Caller must free buf.Pointer.
    666      1.7  sommerfe  */
    667      1.7  sommerfe ACPI_STATUS
    668      1.7  sommerfe acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
    669      1.7  sommerfe {
    670      1.7  sommerfe 	ACPI_STATUS rv;
    671      1.7  sommerfe 
    672      1.7  sommerfe 	if (handle == NULL)
    673      1.7  sommerfe 		handle = ACPI_ROOT_OBJECT;
    674      1.7  sommerfe 
    675      1.7  sommerfe 	bufp->Pointer = NULL;
    676      1.7  sommerfe 	bufp->Length = 0;
    677      1.7  sommerfe 
    678      1.7  sommerfe 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    679      1.7  sommerfe 	if (rv != AE_BUFFER_OVERFLOW)
    680      1.7  sommerfe 		return (rv);
    681      1.7  sommerfe 
    682      1.7  sommerfe 	bufp->Pointer = AcpiOsAllocate(bufp->Length);
    683      1.7  sommerfe 	if (bufp->Pointer == NULL)
    684      1.7  sommerfe 		return (AE_NO_MEMORY);
    685      1.7  sommerfe 
    686      1.7  sommerfe 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    687      1.7  sommerfe 
    688      1.1   thorpej 	return (rv);
    689      1.2   thorpej }
    690      1.2   thorpej 
    691      1.2   thorpej /*
    692      1.2   thorpej  * acpi_get:
    693      1.2   thorpej  *
    694      1.2   thorpej  *	Fetch data info the specified (empty) ACPI buffer.
    695      1.2   thorpej  */
    696      1.2   thorpej ACPI_STATUS
    697      1.2   thorpej acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    698      1.2   thorpej     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    699      1.2   thorpej {
    700      1.2   thorpej 	ACPI_STATUS rv;
    701      1.2   thorpej 
    702      1.2   thorpej 	buf->Pointer = NULL;
    703      1.2   thorpej 	buf->Length = 0;
    704      1.2   thorpej 
    705      1.2   thorpej 	rv = (*getit)(handle, buf);
    706      1.2   thorpej 	if (rv != AE_BUFFER_OVERFLOW)
    707      1.2   thorpej 		return (rv);
    708      1.2   thorpej 
    709  1.7.2.1   gehenna 	buf->Pointer = AcpiOsAllocate(buf->Length);
    710      1.2   thorpej 	if (buf->Pointer == NULL)
    711      1.2   thorpej 		return (AE_NO_MEMORY);
    712  1.7.2.1   gehenna 	memset(buf->Pointer, 0, buf->Length);
    713      1.2   thorpej 
    714      1.2   thorpej 	return ((*getit)(handle, buf));
    715      1.1   thorpej }
    716