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