Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.10
      1  1.10  tshiozak /*	$NetBSD: acpi.c,v 1.10 2002/06/18 08:09:21 tshiozak 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.10  tshiozak __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.10 2002/06/18 08:09:21 tshiozak 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.10  tshiozak #include <machine/acpi_machdep.h>
     57  1.10  tshiozak 
     58   1.1   thorpej #ifdef ENABLE_DEBUGGER
     59   1.1   thorpej #define	ACPI_DBGR_INIT		0x01
     60   1.1   thorpej #define	ACPI_DBGR_TABLES	0x02
     61   1.1   thorpej #define	ACPI_DBGR_ENABLE	0x04
     62   1.1   thorpej #define	ACPI_DBGR_PROBE		0x08
     63   1.1   thorpej #define	ACPI_DBGR_RUNNING	0x10
     64   1.1   thorpej 
     65   1.1   thorpej int	acpi_dbgr = 0x00;
     66   1.1   thorpej #endif
     67   1.1   thorpej 
     68   1.1   thorpej int	acpi_match(struct device *, struct cfdata *, void *);
     69   1.1   thorpej void	acpi_attach(struct device *, struct device *, void *);
     70   1.1   thorpej 
     71   1.1   thorpej int	acpi_print(void *aux, const char *);
     72   1.1   thorpej 
     73   1.1   thorpej extern struct cfdriver acpi_cd;
     74   1.1   thorpej 
     75   1.1   thorpej struct cfattach acpi_ca = {
     76   1.1   thorpej 	sizeof(struct acpi_softc), acpi_match, acpi_attach,
     77   1.1   thorpej };
     78   1.1   thorpej 
     79   1.1   thorpej /*
     80   1.1   thorpej  * This is a flag we set when the ACPI subsystem is active.  Machine
     81   1.1   thorpej  * dependent code may wish to skip other steps (such as attaching
     82   1.1   thorpej  * subsystems that ACPI supercedes) when ACPI is active.
     83   1.1   thorpej  */
     84   1.1   thorpej int	acpi_active;
     85   1.1   thorpej 
     86   1.1   thorpej /*
     87   1.1   thorpej  * Pointer to the ACPI subsystem's state.  There can be only
     88   1.1   thorpej  * one ACPI instance.
     89   1.1   thorpej  */
     90   1.1   thorpej struct acpi_softc *acpi_softc;
     91   1.1   thorpej 
     92   1.1   thorpej void		acpi_shutdown(void *);
     93   1.1   thorpej ACPI_STATUS	acpi_disable(struct acpi_softc *sc);
     94   1.1   thorpej void		acpi_build_tree(struct acpi_softc *);
     95   1.1   thorpej ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
     96   1.1   thorpej 
     97   1.1   thorpej void		acpi_enable_fixed_events(struct acpi_softc *);
     98   1.1   thorpej 
     99   1.1   thorpej /*
    100   1.1   thorpej  * acpi_probe:
    101   1.1   thorpej  *
    102   1.1   thorpej  *	Probe for ACPI support.  This is called by the
    103   1.1   thorpej  *	machine-dependent ACPI front-end.  All of the
    104   1.1   thorpej  *	actual work is done by ACPICA.
    105   1.1   thorpej  *
    106   1.1   thorpej  *	NOTE: This is not an autoconfiguration interface function.
    107   1.1   thorpej  */
    108   1.1   thorpej int
    109   1.1   thorpej acpi_probe(void)
    110   1.1   thorpej {
    111   1.1   thorpej 	static int beenhere;
    112   1.1   thorpej 	ACPI_STATUS rv;
    113   1.1   thorpej 
    114   1.1   thorpej 	if (beenhere != 0)
    115   1.1   thorpej 		panic("acpi_probe: ACPI has already been probed");
    116   1.1   thorpej 	beenhere = 1;
    117   1.1   thorpej 
    118   1.1   thorpej 	/*
    119   1.1   thorpej 	 * Start up ACPICA.
    120   1.1   thorpej 	 */
    121   1.1   thorpej #ifdef ENABLE_DEBUGGER
    122   1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_INIT)
    123   1.1   thorpej 		acpi_osd_debugger();
    124   1.1   thorpej #endif
    125   1.1   thorpej 
    126   1.1   thorpej 	rv = AcpiInitializeSubsystem();
    127   1.1   thorpej 	if (rv != AE_OK) {
    128   1.1   thorpej 		printf("ACPI: unable to initialize ACPICA: %d\n", rv);
    129   1.1   thorpej 		return (0);
    130   1.1   thorpej 	}
    131   1.1   thorpej 
    132   1.1   thorpej #ifdef ENABLE_DEBUGGER
    133   1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    134   1.1   thorpej 		acpi_osd_debugger();
    135   1.1   thorpej #endif
    136   1.1   thorpej 
    137   1.1   thorpej 	rv = AcpiLoadTables();
    138   1.1   thorpej 	if (rv != AE_OK) {
    139   1.1   thorpej 		printf("ACPI: unable to load tables: %d\n", rv);
    140   1.1   thorpej 		return (0);
    141   1.1   thorpej 	}
    142   1.1   thorpej 
    143   1.1   thorpej 	/*
    144   1.1   thorpej 	 * Looks like we have ACPI!
    145   1.1   thorpej 	 */
    146   1.1   thorpej 
    147   1.1   thorpej 	return (1);
    148   1.1   thorpej }
    149   1.1   thorpej 
    150   1.1   thorpej /*
    151   1.1   thorpej  * acpi_match:
    152   1.1   thorpej  *
    153   1.1   thorpej  *	Autoconfiguration `match' routine.
    154   1.1   thorpej  */
    155   1.1   thorpej int
    156   1.1   thorpej acpi_match(struct device *parent, struct cfdata *match, void *aux)
    157   1.1   thorpej {
    158   1.1   thorpej 	struct acpibus_attach_args *aa = aux;
    159   1.1   thorpej 
    160   1.1   thorpej 	if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0)
    161   1.1   thorpej 		return (0);
    162   1.1   thorpej 
    163   1.1   thorpej 	/*
    164   1.1   thorpej 	 * XXX Check other locators?  Hard to know -- machine
    165   1.1   thorpej 	 * dependent code has already checked for the presence
    166   1.1   thorpej 	 * of ACPI by calling acpi_probe(), so I suppose we
    167   1.1   thorpej 	 * don't really have to do anything else.
    168   1.1   thorpej 	 */
    169   1.1   thorpej 	return (1);
    170   1.1   thorpej }
    171   1.1   thorpej 
    172   1.1   thorpej /*
    173   1.1   thorpej  * acpi_attach:
    174   1.1   thorpej  *
    175   1.1   thorpej  *	Autoconfiguration `attach' routine.  Finish initializing
    176   1.1   thorpej  *	ACPICA (some initialization was done in acpi_probe(),
    177   1.1   thorpej  *	which was required to check for the presence of ACPI),
    178   1.1   thorpej  *	and enable the ACPI subsystem.
    179   1.1   thorpej  */
    180   1.1   thorpej void
    181   1.1   thorpej acpi_attach(struct device *parent, struct device *self, void *aux)
    182   1.1   thorpej {
    183   1.1   thorpej 	struct acpi_softc *sc = (void *) self;
    184   1.1   thorpej 	struct acpibus_attach_args *aa = aux;
    185   1.1   thorpej 	ACPI_STATUS rv;
    186   1.1   thorpej 
    187   1.1   thorpej 	printf("\n");
    188   1.1   thorpej 
    189   1.1   thorpej 	if (acpi_softc != NULL)
    190   1.1   thorpej 		panic("acpi_attach: ACPI has already been attached");
    191   1.1   thorpej 
    192   1.1   thorpej 	sc->sc_iot = aa->aa_iot;
    193   1.1   thorpej 	sc->sc_memt = aa->aa_memt;
    194   1.1   thorpej 	sc->sc_pc = aa->aa_pc;
    195   1.1   thorpej 	sc->sc_pciflags = aa->aa_pciflags;
    196   1.1   thorpej 
    197   1.1   thorpej 	acpi_softc = sc;
    198   1.1   thorpej 
    199   1.1   thorpej 	/*
    200   1.1   thorpej 	 * Install the default address space handlers.
    201   1.1   thorpej 	 */
    202   1.1   thorpej 
    203   1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    204   1.1   thorpej 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    205   1.1   thorpej 	if (rv != AE_OK) {
    206   1.1   thorpej 		printf("%s: unable to install SYSTEM MEMORY handler: %d\n",
    207   1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    208   1.1   thorpej 		return;
    209   1.1   thorpej 	}
    210   1.1   thorpej 
    211   1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    212   1.1   thorpej 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    213   1.1   thorpej 	if (rv != AE_OK) {
    214   1.1   thorpej 		printf("%s: unable to install SYSTEM IO handler: %d\n",
    215   1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    216   1.1   thorpej 		return;
    217   1.1   thorpej 	}
    218   1.1   thorpej 
    219   1.1   thorpej 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    220   1.1   thorpej 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    221   1.1   thorpej 	if (rv != AE_OK) {
    222   1.1   thorpej 		printf("%s: unable to install PCI CONFIG handler: %d\n",
    223   1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    224   1.1   thorpej 		return;
    225   1.1   thorpej 	}
    226   1.1   thorpej 
    227   1.1   thorpej 	/*
    228   1.1   thorpej 	 * Bring ACPI on-line.
    229   1.1   thorpej 	 *
    230   1.1   thorpej 	 * Note that we request that _STA (device init) and _INI (object init)
    231   1.1   thorpej 	 * methods not be run.
    232   1.1   thorpej 	 *
    233   1.1   thorpej 	 * XXX We need to arrange for the object init pass after we have
    234   1.1   thorpej 	 * XXX attached all of our children.
    235   1.1   thorpej 	 */
    236   1.1   thorpej #ifdef ENABLE_DEBUGGER
    237   1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    238   1.1   thorpej 		acpi_osd_debugger();
    239   1.1   thorpej #endif
    240   1.1   thorpej 	rv = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
    241   1.1   thorpej 	if (rv != AE_OK) {
    242   1.1   thorpej 		printf("%s: unable to enable ACPI: %d\n",
    243   1.1   thorpej 		    sc->sc_dev.dv_xname, rv);
    244   1.1   thorpej 		return;
    245   1.1   thorpej 	}
    246   1.1   thorpej 	acpi_active = 1;
    247   1.1   thorpej 
    248   1.1   thorpej 	/*
    249   1.1   thorpej 	 * Set up the default sleep state to enter when various
    250   1.1   thorpej 	 * switches are activated.
    251   1.1   thorpej 	 */
    252   1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_POWERBUTTON] = ACPI_STATE_S5;
    253   1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_SLEEPBUTTON] = ACPI_STATE_S1;
    254   1.1   thorpej 	sc->sc_switch_sleep[ACPI_SWITCH_LID]	     = ACPI_STATE_S1;
    255   1.1   thorpej 
    256   1.1   thorpej 	/* Our current state is "awake". */
    257   1.1   thorpej 	sc->sc_sleepstate = ACPI_STATE_S0;
    258   1.1   thorpej 
    259   1.9   kanaoka 	/* Show SCI interrupt. */
    260   1.9   kanaoka 	if (AcpiGbl_FADT != NULL)
    261   1.9   kanaoka 		printf("%s: SCI interrupting at irq %d\n",
    262   1.9   kanaoka 			sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    263   1.1   thorpej 	/*
    264   1.1   thorpej 	 * Check for fixed-hardware features.
    265   1.1   thorpej 	 */
    266   1.1   thorpej 	acpi_enable_fixed_events(sc);
    267   1.1   thorpej 
    268   1.1   thorpej 	/*
    269   1.1   thorpej 	 * Scan the namespace and build our device tree.
    270   1.1   thorpej 	 */
    271   1.1   thorpej #ifdef ENABLE_DEBUGGER
    272   1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    273   1.1   thorpej 		acpi_osd_debugger();
    274   1.1   thorpej #endif
    275   1.1   thorpej 	acpi_build_tree(sc);
    276   1.1   thorpej 
    277   1.1   thorpej 	/*
    278   1.1   thorpej 	 * Register a shutdown hook that disables certain ACPI
    279   1.1   thorpej 	 * events that might happen and confuse us while we're
    280   1.1   thorpej 	 * trying to shut down.
    281   1.1   thorpej 	 */
    282   1.1   thorpej 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    283   1.1   thorpej 	if (sc->sc_sdhook == NULL)
    284   1.1   thorpej 		printf("%s: WARNING: unable to register shutdown hook\n",
    285   1.1   thorpej 		    sc->sc_dev.dv_xname);
    286   1.1   thorpej 
    287   1.1   thorpej #ifdef ENABLE_DEBUGGER
    288   1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    289   1.1   thorpej 		acpi_osd_debugger();
    290   1.1   thorpej #endif
    291   1.1   thorpej }
    292   1.1   thorpej 
    293   1.1   thorpej /*
    294   1.1   thorpej  * acpi_shutdown:
    295   1.1   thorpej  *
    296   1.1   thorpej  *	Shutdown hook for ACPI -- disable some events that
    297   1.1   thorpej  *	might confuse us.
    298   1.1   thorpej  */
    299   1.1   thorpej void
    300   1.1   thorpej acpi_shutdown(void *arg)
    301   1.1   thorpej {
    302   1.1   thorpej 	struct acpi_softc *sc = arg;
    303   1.1   thorpej 
    304   1.1   thorpej 	if (acpi_disable(sc) != AE_OK)
    305   1.1   thorpej 		printf("%s: WARNING: unable to disable ACPI\n",
    306   1.1   thorpej 		    sc->sc_dev.dv_xname);
    307   1.1   thorpej }
    308   1.1   thorpej 
    309   1.1   thorpej /*
    310   1.1   thorpej  * acpi_disable:
    311   1.1   thorpej  *
    312   1.1   thorpej  *	Disable ACPI.
    313   1.1   thorpej  */
    314   1.1   thorpej ACPI_STATUS
    315   1.1   thorpej acpi_disable(struct acpi_softc *sc)
    316   1.1   thorpej {
    317   1.1   thorpej 	ACPI_STATUS rv = AE_OK;
    318   1.1   thorpej 
    319   1.1   thorpej 	if (acpi_active) {
    320   1.1   thorpej 		rv = AcpiDisable();
    321   1.1   thorpej 		if (rv == AE_OK)
    322   1.1   thorpej 			acpi_active = 0;
    323   1.1   thorpej 	}
    324   1.1   thorpej 	return (rv);
    325   1.1   thorpej }
    326   1.1   thorpej 
    327   1.1   thorpej struct acpi_make_devnode_state {
    328   1.1   thorpej 	struct acpi_softc *softc;
    329   1.1   thorpej 	struct acpi_scope *scope;
    330   1.1   thorpej };
    331   1.1   thorpej 
    332   1.1   thorpej /*
    333   1.1   thorpej  * acpi_build_tree:
    334   1.1   thorpej  *
    335   1.1   thorpej  *	Scan relevant portions of the ACPI namespace and attach
    336   1.1   thorpej  *	child devices.
    337   1.1   thorpej  */
    338   1.1   thorpej void
    339   1.1   thorpej acpi_build_tree(struct acpi_softc *sc)
    340   1.1   thorpej {
    341   1.1   thorpej 	static const char *scopes[] = {
    342   1.1   thorpej 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    343   1.1   thorpej 		"\\_SB_",	/* system bus namespace */
    344   1.1   thorpej 		"\\_SI_",	/* system idicator namespace */
    345   1.1   thorpej 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    346   1.1   thorpej 		NULL,
    347   1.1   thorpej 	};
    348   1.1   thorpej 	struct acpi_attach_args aa;
    349   1.1   thorpej 	struct acpi_make_devnode_state state;
    350   1.1   thorpej 	struct acpi_scope *as;
    351   1.1   thorpej 	struct acpi_devnode *ad;
    352   1.1   thorpej 	ACPI_HANDLE parent;
    353   1.1   thorpej 	int i;
    354   1.1   thorpej 
    355   1.1   thorpej 	TAILQ_INIT(&sc->sc_scopes);
    356   1.1   thorpej 
    357   1.1   thorpej 	state.softc = sc;
    358   1.1   thorpej 
    359   1.1   thorpej 	/*
    360   1.1   thorpej 	 * Scan the namespace and build our tree.
    361   1.1   thorpej 	 */
    362   1.1   thorpej 	for (i = 0; scopes[i] != NULL; i++) {
    363   1.1   thorpej 		as = malloc(sizeof(*as), M_DEVBUF, M_WAITOK);
    364   1.1   thorpej 		as->as_name = scopes[i];
    365   1.1   thorpej 		TAILQ_INIT(&as->as_devnodes);
    366   1.1   thorpej 
    367   1.1   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    368   1.1   thorpej 
    369   1.1   thorpej 		state.scope = as;
    370   1.1   thorpej 
    371   1.1   thorpej 		if (AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i],
    372   1.1   thorpej 		    &parent) == AE_OK) {
    373   1.1   thorpej 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    374   1.1   thorpej 			    acpi_make_devnode, &state, NULL);
    375   1.1   thorpej 		}
    376   1.1   thorpej 
    377   1.1   thorpej 		/* Now, for this namespace, try and attach the devices. */
    378   1.1   thorpej 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    379   1.1   thorpej 			aa.aa_node = ad;
    380   1.1   thorpej 			aa.aa_iot = sc->sc_iot;
    381   1.1   thorpej 			aa.aa_memt = sc->sc_memt;
    382   1.1   thorpej 			aa.aa_pc = sc->sc_pc;
    383   1.1   thorpej 			aa.aa_pciflags = sc->sc_pciflags;
    384   1.1   thorpej 
    385   1.1   thorpej 			/*
    386   1.1   thorpej 			 * XXX We only attach devices which are:
    387   1.1   thorpej 			 *
    388   1.1   thorpej 			 *	- present
    389   1.1   thorpej 			 *	- enabled
    390   1.1   thorpej 			 *	- functioning properly
    391   1.1   thorpej 			 *
    392   1.1   thorpej 			 * However, if enabled, it's decoding resources,
    393   1.1   thorpej 			 * so we should claim them, if possible.  Requires
    394   1.1   thorpej 			 * changes to bus_space(9).
    395   1.1   thorpej 			 */
    396   1.1   thorpej 			if ((ad->ad_devinfo.CurrentStatus &
    397   1.1   thorpej 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    398   1.9   kanaoka 			      ACPI_STA_DEV_OK)) !=
    399   1.1   thorpej 			    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    400   1.9   kanaoka 			     ACPI_STA_DEV_OK))
    401   1.1   thorpej 				continue;
    402   1.1   thorpej 
    403   1.1   thorpej 			/*
    404   1.1   thorpej 			 * XXX Same problem as above...
    405   1.1   thorpej 			 */
    406   1.1   thorpej 			if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
    407   1.1   thorpej 				continue;
    408   1.1   thorpej 
    409   1.1   thorpej 			ad->ad_device = config_found(&sc->sc_dev,
    410   1.1   thorpej 			    &aa, acpi_print);
    411   1.1   thorpej 		}
    412   1.1   thorpej 	}
    413   1.1   thorpej }
    414   1.1   thorpej 
    415   1.1   thorpej /*
    416   1.1   thorpej  * acpi_make_devnode:
    417   1.1   thorpej  *
    418   1.1   thorpej  *	Make an ACPI devnode.
    419   1.1   thorpej  */
    420   1.1   thorpej ACPI_STATUS
    421   1.1   thorpej acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    422   1.1   thorpej     void **status)
    423   1.1   thorpej {
    424   1.1   thorpej 	struct acpi_make_devnode_state *state = context;
    425   1.4   thorpej #ifdef ACPI_DEBUG
    426   1.1   thorpej 	struct acpi_softc *sc = state->softc;
    427   1.4   thorpej #endif
    428   1.1   thorpej 	struct acpi_scope *as = state->scope;
    429   1.1   thorpej 	struct acpi_devnode *ad;
    430   1.1   thorpej 	ACPI_OBJECT_TYPE type;
    431   1.1   thorpej 	ACPI_STATUS rv;
    432   1.1   thorpej 
    433   1.1   thorpej 	if (AcpiGetType(handle, &type) == AE_OK) {
    434   1.1   thorpej 		switch (type) {
    435   1.1   thorpej 		case ACPI_TYPE_DEVICE:
    436   1.1   thorpej 		case ACPI_TYPE_PROCESSOR:
    437   1.1   thorpej 		case ACPI_TYPE_THERMAL:
    438   1.1   thorpej 		case ACPI_TYPE_POWER:
    439   1.6   tsutsui 			ad = malloc(sizeof(*ad), M_DEVBUF, M_NOWAIT|M_ZERO);
    440   1.1   thorpej 			if (ad == NULL)
    441   1.1   thorpej 				return (AE_NO_MEMORY);
    442   1.1   thorpej 
    443   1.1   thorpej 			ad->ad_handle = handle;
    444   1.1   thorpej 			ad->ad_level = level;
    445   1.1   thorpej 			ad->ad_scope = as;
    446   1.1   thorpej 			ad->ad_type = type;
    447   1.1   thorpej 
    448   1.1   thorpej 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    449   1.1   thorpej 
    450   1.1   thorpej 			rv = AcpiGetObjectInfo(handle, &ad->ad_devinfo);
    451   1.1   thorpej 			if (rv != AE_OK)
    452   1.1   thorpej 				goto out;
    453   1.1   thorpej 
    454   1.1   thorpej 			if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
    455   1.1   thorpej 				goto out;
    456   1.1   thorpej 
    457   1.1   thorpej #ifdef ACPI_DEBUG
    458   1.1   thorpej 			printf("%s: HID %s found in scope %s level %d\n",
    459   1.1   thorpej 			    sc->sc_dev.dv_xname, ad->ad_devinfo.HardwareId,
    460   1.1   thorpej 			    as->as_name, ad->ad_level);
    461   1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_UID)
    462   1.1   thorpej 				printf("       UID %s\n",
    463   1.1   thorpej 				    ad->ad_devinfo.UniqueId);
    464   1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_ADR)
    465   1.1   thorpej 				printf("       ADR 0x%016qx\n",
    466   1.1   thorpej 				    ad->ad_devinfo.Address);
    467   1.1   thorpej 			if (ad->ad_devinfo.Valid & ACPI_VALID_STA)
    468   1.1   thorpej 				printf("       STA 0x%08x\n",
    469   1.1   thorpej 				    ad->ad_devinfo.CurrentStatus);
    470   1.1   thorpej #endif
    471   1.1   thorpej 		}
    472   1.1   thorpej 	}
    473   1.1   thorpej  out:
    474   1.1   thorpej 	return (AE_OK);
    475   1.1   thorpej }
    476   1.1   thorpej 
    477   1.1   thorpej /*
    478   1.1   thorpej  * acpi_print:
    479   1.1   thorpej  *
    480   1.1   thorpej  *	Autoconfiguration print routine.
    481   1.1   thorpej  */
    482   1.1   thorpej int
    483   1.1   thorpej acpi_print(void *aux, const char *pnp)
    484   1.1   thorpej {
    485   1.1   thorpej 	struct acpi_attach_args *aa = aux;
    486   1.7  sommerfe #if 0
    487   1.4   thorpej 	char *str;
    488   1.7  sommerfe #endif
    489   1.1   thorpej 
    490   1.4   thorpej 	if (pnp) {
    491   1.4   thorpej 		printf("%s ", aa->aa_node->ad_devinfo.HardwareId);
    492   1.7  sommerfe #if 0 /* Not until we fix acpi_eval_string */
    493   1.4   thorpej 		if (acpi_eval_string(aa->aa_node->ad_handle,
    494   1.4   thorpej 		    "_STR", &str) == AE_OK) {
    495   1.4   thorpej 			printf("[%s] ", str);
    496   1.4   thorpej 			AcpiOsFree(str);
    497   1.4   thorpej 		}
    498   1.7  sommerfe #endif
    499   1.4   thorpej 		printf("at %s", pnp);
    500   1.4   thorpej 	}
    501   1.1   thorpej 
    502   1.1   thorpej 	return (UNCONF);
    503   1.1   thorpej }
    504   1.1   thorpej 
    505   1.1   thorpej /*****************************************************************************
    506   1.1   thorpej  * ACPI fixed-hardware feature handlers
    507   1.1   thorpej  *****************************************************************************/
    508   1.1   thorpej 
    509   1.1   thorpej UINT32		acpi_fixed_power_button_handler(void *);
    510   1.1   thorpej UINT32		acpi_fixed_sleep_button_handler(void *);
    511   1.1   thorpej 
    512   1.1   thorpej /*
    513   1.1   thorpej  * acpi_enable_fixed_events:
    514   1.1   thorpej  *
    515   1.1   thorpej  *	Enable any fixed-hardware feature handlers.
    516   1.1   thorpej  */
    517   1.1   thorpej void
    518   1.1   thorpej acpi_enable_fixed_events(struct acpi_softc *sc)
    519   1.1   thorpej {
    520   1.1   thorpej 	static int beenhere;
    521   1.1   thorpej 	ACPI_STATUS rv;
    522   1.1   thorpej 
    523   1.1   thorpej 	/*
    524   1.1   thorpej 	 * Check for fixed-hardware buttons.
    525   1.1   thorpej 	 */
    526   1.1   thorpej 
    527   1.1   thorpej 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    528   1.1   thorpej 		if (beenhere == 0)
    529   1.1   thorpej 			printf("%s: fixed-feature power button present\n",
    530   1.1   thorpej 			    sc->sc_dev.dv_xname);
    531   1.1   thorpej 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
    532   1.1   thorpej 		    acpi_fixed_power_button_handler, sc);
    533   1.1   thorpej 		if (rv != AE_OK)
    534   1.1   thorpej 			printf("%s: unable to install handler for fixed "
    535   1.1   thorpej 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    536   1.1   thorpej 	}
    537   1.1   thorpej 
    538   1.1   thorpej 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    539   1.1   thorpej 		if (beenhere == 0)
    540   1.1   thorpej 			printf("%s: fixed-feature sleep button present\n",
    541   1.1   thorpej 			    sc->sc_dev.dv_xname);
    542   1.1   thorpej 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
    543   1.1   thorpej 		    acpi_fixed_sleep_button_handler, sc);
    544   1.1   thorpej 		if (rv != AE_OK)
    545   1.1   thorpej 			printf("%s: unable to install handler for fixed "
    546   1.1   thorpej 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    547   1.1   thorpej 	}
    548   1.1   thorpej 
    549   1.1   thorpej 	beenhere = 1;
    550   1.1   thorpej }
    551   1.1   thorpej 
    552   1.1   thorpej /*
    553   1.1   thorpej  * acpi_fixed_power_button_handler:
    554   1.1   thorpej  *
    555   1.1   thorpej  *	Fixed event handler for the power button.
    556   1.1   thorpej  */
    557   1.1   thorpej UINT32
    558   1.1   thorpej acpi_fixed_power_button_handler(void *context)
    559   1.1   thorpej {
    560   1.1   thorpej 	struct acpi_softc *sc = context;
    561   1.1   thorpej 
    562   1.1   thorpej 	/* XXX XXX XXX */
    563   1.1   thorpej 
    564   1.1   thorpej 	printf("%s: fixed power button pressed\n", sc->sc_dev.dv_xname);
    565   1.1   thorpej 
    566   1.8   thorpej 	return (ACPI_INTERRUPT_HANDLED);
    567   1.1   thorpej }
    568   1.1   thorpej 
    569   1.1   thorpej /*
    570   1.1   thorpej  * acpi_fixed_sleep_button_handler:
    571   1.1   thorpej  *
    572   1.1   thorpej  *	Fixed event handler for the sleep button.
    573   1.1   thorpej  */
    574   1.1   thorpej UINT32
    575   1.1   thorpej acpi_fixed_sleep_button_handler(void *context)
    576   1.1   thorpej {
    577   1.1   thorpej 	struct acpi_softc *sc = context;
    578   1.1   thorpej 
    579   1.1   thorpej 	/* XXX XXX XXX */
    580   1.1   thorpej 
    581   1.1   thorpej 	printf("%s: fixed sleep button pressed\n", sc->sc_dev.dv_xname);
    582   1.1   thorpej 
    583   1.8   thorpej 	return (ACPI_INTERRUPT_HANDLED);
    584   1.1   thorpej }
    585   1.1   thorpej 
    586   1.1   thorpej /*****************************************************************************
    587   1.1   thorpej  * ACPI utility routines.
    588   1.1   thorpej  *****************************************************************************/
    589   1.1   thorpej 
    590   1.2   thorpej /*
    591   1.2   thorpej  * acpi_eval_integer:
    592   1.2   thorpej  *
    593   1.2   thorpej  *	Evaluate an integer object.
    594   1.2   thorpej  */
    595   1.1   thorpej ACPI_STATUS
    596   1.1   thorpej acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
    597   1.1   thorpej {
    598   1.1   thorpej 	ACPI_STATUS rv;
    599   1.1   thorpej 	ACPI_BUFFER buf;
    600   1.1   thorpej 	ACPI_OBJECT param;
    601   1.1   thorpej 
    602   1.1   thorpej 	if (handle == NULL)
    603   1.1   thorpej 		handle = ACPI_ROOT_OBJECT;
    604   1.1   thorpej 
    605   1.1   thorpej 	buf.Pointer = &param;
    606   1.1   thorpej 	buf.Length = sizeof(param);
    607   1.1   thorpej 
    608   1.1   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    609   1.1   thorpej 	if (rv == AE_OK) {
    610   1.1   thorpej 		if (param.Type == ACPI_TYPE_INTEGER)
    611   1.1   thorpej 			*valp = param.Integer.Value;
    612   1.1   thorpej 		else
    613   1.1   thorpej 			rv = AE_TYPE;
    614   1.1   thorpej 	}
    615   1.1   thorpej 
    616   1.4   thorpej 	return (rv);
    617   1.4   thorpej }
    618   1.4   thorpej 
    619   1.7  sommerfe #if 0
    620   1.4   thorpej /*
    621   1.4   thorpej  * acpi_eval_string:
    622   1.4   thorpej  *
    623   1.7  sommerfe  *	Evaluate a (Unicode) string object.
    624   1.7  sommerfe  * XXX current API may leak memory, so don't use this.
    625   1.4   thorpej  */
    626   1.4   thorpej ACPI_STATUS
    627   1.4   thorpej acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
    628   1.4   thorpej {
    629   1.4   thorpej 	ACPI_STATUS rv;
    630   1.4   thorpej 	ACPI_BUFFER buf;
    631   1.7  sommerfe 	ACPI_OBJECT *param;
    632   1.4   thorpej 
    633   1.4   thorpej 	if (handle == NULL)
    634   1.4   thorpej 		handle = ACPI_ROOT_OBJECT;
    635   1.4   thorpej 
    636   1.4   thorpej 	buf.Pointer = NULL;
    637   1.4   thorpej 	buf.Length = 0;
    638   1.4   thorpej 
    639   1.4   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    640   1.4   thorpej 	if (rv != AE_BUFFER_OVERFLOW)
    641   1.4   thorpej 		return (rv);
    642   1.4   thorpej 
    643   1.4   thorpej 	buf.Pointer = AcpiOsAllocate(buf.Length);
    644   1.4   thorpej 	if (buf.Pointer == NULL)
    645   1.4   thorpej 		return (AE_NO_MEMORY);
    646   1.4   thorpej 
    647   1.4   thorpej 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    648   1.7  sommerfe 	param = (ACPI_OBJECT *)buf.Pointer;
    649   1.4   thorpej 	if (rv == AE_OK) {
    650   1.7  sommerfe 		if (param->Type == ACPI_TYPE_STRING) {
    651   1.7  sommerfe 			/* XXX may leak buf.Pointer!! */
    652   1.7  sommerfe 			*stringp = param->String.Pointer;
    653   1.4   thorpej 			return (AE_OK);
    654   1.4   thorpej 		}
    655   1.4   thorpej 		rv = AE_TYPE;
    656   1.4   thorpej 	}
    657   1.4   thorpej 
    658   1.4   thorpej 	AcpiOsFree(buf.Pointer);
    659   1.7  sommerfe 	return (rv);
    660   1.7  sommerfe }
    661   1.7  sommerfe #endif
    662   1.7  sommerfe 
    663   1.7  sommerfe 
    664   1.7  sommerfe /*
    665   1.7  sommerfe  * acpi_eval_struct:
    666   1.7  sommerfe  *
    667   1.7  sommerfe  *	Evaluate a more complex structure.  Caller must free buf.Pointer.
    668   1.7  sommerfe  */
    669   1.7  sommerfe ACPI_STATUS
    670   1.7  sommerfe acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
    671   1.7  sommerfe {
    672   1.7  sommerfe 	ACPI_STATUS rv;
    673   1.7  sommerfe 
    674   1.7  sommerfe 	if (handle == NULL)
    675   1.7  sommerfe 		handle = ACPI_ROOT_OBJECT;
    676   1.7  sommerfe 
    677   1.7  sommerfe 	bufp->Pointer = NULL;
    678   1.7  sommerfe 	bufp->Length = 0;
    679   1.7  sommerfe 
    680   1.7  sommerfe 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    681   1.7  sommerfe 	if (rv != AE_BUFFER_OVERFLOW)
    682   1.7  sommerfe 		return (rv);
    683   1.7  sommerfe 
    684   1.7  sommerfe 	bufp->Pointer = AcpiOsAllocate(bufp->Length);
    685   1.7  sommerfe 	if (bufp->Pointer == NULL)
    686   1.7  sommerfe 		return (AE_NO_MEMORY);
    687   1.7  sommerfe 
    688   1.7  sommerfe 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    689   1.7  sommerfe 
    690   1.1   thorpej 	return (rv);
    691   1.2   thorpej }
    692   1.2   thorpej 
    693   1.2   thorpej /*
    694   1.2   thorpej  * acpi_get:
    695   1.2   thorpej  *
    696   1.2   thorpej  *	Fetch data info the specified (empty) ACPI buffer.
    697   1.2   thorpej  */
    698   1.2   thorpej ACPI_STATUS
    699   1.2   thorpej acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    700   1.2   thorpej     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    701   1.2   thorpej {
    702   1.2   thorpej 	ACPI_STATUS rv;
    703   1.2   thorpej 
    704   1.2   thorpej 	buf->Pointer = NULL;
    705   1.2   thorpej 	buf->Length = 0;
    706   1.2   thorpej 
    707   1.2   thorpej 	rv = (*getit)(handle, buf);
    708   1.2   thorpej 	if (rv != AE_BUFFER_OVERFLOW)
    709   1.2   thorpej 		return (rv);
    710   1.2   thorpej 
    711   1.8   thorpej 	buf->Pointer = AcpiOsAllocate(buf->Length);
    712   1.2   thorpej 	if (buf->Pointer == NULL)
    713   1.2   thorpej 		return (AE_NO_MEMORY);
    714   1.8   thorpej 	memset(buf->Pointer, 0, buf->Length);
    715   1.2   thorpej 
    716   1.2   thorpej 	return ((*getit)(handle, buf));
    717  1.10  tshiozak }
    718  1.10  tshiozak 
    719  1.10  tshiozak 
    720  1.10  tshiozak /*****************************************************************************
    721  1.10  tshiozak  * ACPI sleep support.
    722  1.10  tshiozak  *****************************************************************************/
    723  1.10  tshiozak 
    724  1.10  tshiozak static int
    725  1.10  tshiozak is_available_state(struct acpi_softc *sc, int state)
    726  1.10  tshiozak {
    727  1.10  tshiozak 	UINT8 type_a, type_b;
    728  1.10  tshiozak 
    729  1.10  tshiozak 	return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
    730  1.10  tshiozak 						  &type_a, &type_b)));
    731  1.10  tshiozak }
    732  1.10  tshiozak 
    733  1.10  tshiozak /*
    734  1.10  tshiozak  * acpi_enter_sleep_state:
    735  1.10  tshiozak  *
    736  1.10  tshiozak  *	enter to the specified sleep state.
    737  1.10  tshiozak  */
    738  1.10  tshiozak 
    739  1.10  tshiozak ACPI_STATUS
    740  1.10  tshiozak acpi_enter_sleep_state(struct acpi_softc *sc, int state)
    741  1.10  tshiozak {
    742  1.10  tshiozak 	int s;
    743  1.10  tshiozak 	ACPI_STATUS ret = AE_OK;
    744  1.10  tshiozak 
    745  1.10  tshiozak 	switch (state) {
    746  1.10  tshiozak 	case ACPI_STATE_S0:
    747  1.10  tshiozak 		break;
    748  1.10  tshiozak 	case ACPI_STATE_S1:
    749  1.10  tshiozak 	case ACPI_STATE_S2:
    750  1.10  tshiozak 	case ACPI_STATE_S3:
    751  1.10  tshiozak 	case ACPI_STATE_S4:
    752  1.10  tshiozak 		if (!is_available_state(sc, state)) {
    753  1.10  tshiozak 			printf("acpi: cannot enter the sleep state (%d).\n",
    754  1.10  tshiozak 			       state);
    755  1.10  tshiozak 			break;
    756  1.10  tshiozak 		}
    757  1.10  tshiozak 		ret = AcpiEnterSleepStatePrep(state);
    758  1.10  tshiozak 		if (ACPI_FAILURE(ret)) {
    759  1.10  tshiozak 			printf("acpi: failed preparing to sleep (%s)\n",
    760  1.10  tshiozak 			       AcpiFormatException(ret));
    761  1.10  tshiozak 			break;
    762  1.10  tshiozak 		}
    763  1.10  tshiozak 		if (state==ACPI_STATE_S1) {
    764  1.10  tshiozak 			/* just enter the state */
    765  1.10  tshiozak 			AcpiEnterSleepState((UINT8)state);
    766  1.10  tshiozak 			AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
    767  1.10  tshiozak 		} else {
    768  1.10  tshiozak 			/* XXX: powerhooks(9) framework is too poor to
    769  1.10  tshiozak 			 * support ACPI sleep state...
    770  1.10  tshiozak 			 */
    771  1.10  tshiozak 			dopowerhooks(PWR_SOFTSUSPEND);
    772  1.10  tshiozak 			s = splhigh();
    773  1.10  tshiozak 			dopowerhooks(PWR_SUSPEND);
    774  1.10  tshiozak 			acpi_md_sleep(state);
    775  1.10  tshiozak 			dopowerhooks(PWR_RESUME);
    776  1.10  tshiozak 			splx(s);
    777  1.10  tshiozak 			dopowerhooks(PWR_SOFTRESUME);
    778  1.10  tshiozak 			if (state==ACPI_STATE_S4)
    779  1.10  tshiozak 				AcpiEnable();
    780  1.10  tshiozak 		}
    781  1.10  tshiozak 		AcpiLeaveSleepState((UINT8)state);
    782  1.10  tshiozak 		break;
    783  1.10  tshiozak 	case ACPI_STATE_S5:
    784  1.10  tshiozak 		AcpiEnterSleepStatePrep(ACPI_STATE_S5);
    785  1.10  tshiozak 		AcpiEnterSleepState(ACPI_STATE_S5);
    786  1.10  tshiozak 		printf("WARNING: powerdown failed!\n");
    787  1.10  tshiozak 		break;
    788  1.10  tshiozak 	}
    789  1.10  tshiozak 
    790  1.10  tshiozak 	return_ACPI_STATUS(ret);
    791   1.1   thorpej }
    792