Home | History | Annotate | Line # | Download | only in acpi
acpi_power.c revision 1.3
      1 /* $NetBSD: acpi_power.c,v 1.3 2010/04/23 15:37:01 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2001 Michael Smith
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: acpi_power.c,v 1.3 2010/04/23 15:37:01 jruoho Exp $");
     60 
     61 #include <sys/param.h>
     62 #include <sys/kmem.h>
     63 #include <sys/mutex.h>
     64 
     65 #include <dev/acpi/acpireg.h>
     66 #include <dev/acpi/acpivar.h>
     67 #include <dev/acpi/acpi_power.h>
     68 
     69 #define _COMPONENT			ACPI_BUS_COMPONENT
     70 ACPI_MODULE_NAME			("acpi_power")
     71 
     72 /*
     73  * References.
     74  */
     75 struct acpi_power_ref {
     76 	ACPI_HANDLE			ref_handle;
     77 
     78 	SIMPLEQ_ENTRY(acpi_power_ref)	ref_list;
     79 };
     80 
     81 /*
     82  * Resources.
     83  */
     84 struct acpi_power_res {
     85 	ACPI_HANDLE			res_handle;
     86 	ACPI_INTEGER			res_level;
     87 	ACPI_INTEGER			res_order;
     88 	char				res_name[5];
     89 	kmutex_t			res_mutex;
     90 
     91 	TAILQ_ENTRY(acpi_power_res)	res_list;
     92 	SIMPLEQ_HEAD(, acpi_power_ref)	ref_head;
     93 };
     94 
     95 static TAILQ_HEAD(, acpi_power_res) res_head =
     96 	TAILQ_HEAD_INITIALIZER(res_head);
     97 
     98 static struct acpi_power_res	*acpi_power_res_init(ACPI_HANDLE);
     99 static struct acpi_power_res	*acpi_power_res_get(ACPI_HANDLE);
    100 
    101 #if 0
    102 static ACPI_STATUS	 acpi_power_get_direct(struct acpi_devnode *);
    103 #endif
    104 
    105 static ACPI_STATUS	 acpi_power_get_indirect(struct acpi_devnode *);
    106 static ACPI_STATUS	 acpi_power_switch(struct acpi_devnode *,
    107 						   int, bool);
    108 static ACPI_STATUS	 acpi_power_res_on(ACPI_OBJECT *, void *);
    109 static ACPI_STATUS	 acpi_power_res_off(ACPI_OBJECT *, void *);
    110 static ACPI_STATUS	 acpi_power_res_ref(struct acpi_power_res *,
    111 					    ACPI_HANDLE);
    112 static ACPI_STATUS	 acpi_power_res_deref(struct acpi_power_res *,
    113 					      ACPI_HANDLE);
    114 static ACPI_STATUS	 acpi_power_res_sta(ACPI_OBJECT *, void *);
    115 
    116 static ACPI_OBJECT	*acpi_power_pkg_get(ACPI_HANDLE, int);
    117 static const char	*acpi_xname(ACPI_HANDLE);
    118 
    119 void
    120 acpi_power_res_add(struct acpi_devnode *ad)
    121 {
    122 	struct acpi_power_res *res;
    123 
    124 	KASSERT(ad != NULL && ad->ad_root != NULL);
    125 	KASSERT(ad->ad_devinfo->Type == ACPI_TYPE_POWER);
    126 
    127 	res = acpi_power_res_init(ad->ad_handle);
    128 
    129 	if (res == NULL)
    130 		aprint_error_dev(ad->ad_root, "failed to "
    131 		    "add power resource %s\n", ad->ad_name);
    132 }
    133 
    134 static struct acpi_power_res *
    135 acpi_power_res_init(ACPI_HANDLE hdl)
    136 {
    137 	struct acpi_power_res *tmp = NULL;
    138 	struct acpi_power_res *res = NULL;
    139 	ACPI_OBJECT *obj;
    140 	ACPI_BUFFER buf;
    141 	ACPI_STATUS rv;
    142 
    143 	rv = acpi_eval_struct(hdl, NULL, &buf);
    144 
    145 	if (ACPI_FAILURE(rv))
    146 		goto out;
    147 
    148 	obj = buf.Pointer;
    149 
    150 	if (obj->Type != ACPI_TYPE_POWER) {
    151 		rv = AE_TYPE;
    152 		goto out;
    153 	}
    154 
    155 	res = kmem_zalloc(sizeof(*res), KM_SLEEP);
    156 
    157 	if (res == NULL) {
    158 		rv = AE_NO_MEMORY;
    159 		goto out;
    160 	}
    161 
    162 	res->res_handle = hdl;
    163 	res->res_level = obj->PowerResource.SystemLevel;
    164 	res->res_order = obj->PowerResource.ResourceOrder;
    165 
    166 	(void)strlcpy(res->res_name,
    167 	    acpi_xname(hdl), sizeof(res->res_name));
    168 
    169 	SIMPLEQ_INIT(&res->ref_head);
    170 	mutex_init(&res->res_mutex, MUTEX_DEFAULT, IPL_NONE);
    171 
    172 	/*
    173 	 * Power resources should be ordered.
    174 	 *
    175 	 * These *should* be enabled from low values to high
    176 	 * values and disabled from high values to low values.
    177 	 */
    178 	TAILQ_FOREACH(tmp, &res_head, res_list) {
    179 
    180 		if (res->res_order < tmp->res_order) {
    181 			TAILQ_INSERT_BEFORE(tmp, res, res_list);
    182 			break;
    183 		}
    184 	}
    185 
    186 	if (tmp == NULL)
    187 		TAILQ_INSERT_TAIL(&res_head, res, res_list);
    188 
    189 out:
    190 	if (buf.Pointer != NULL)
    191 		ACPI_FREE(buf.Pointer);
    192 
    193 	return res;
    194 }
    195 
    196 static struct acpi_power_res *
    197 acpi_power_res_get(ACPI_HANDLE hdl)
    198 {
    199 	struct acpi_power_res *res;
    200 
    201 	TAILQ_FOREACH(res, &res_head, res_list) {
    202 
    203 		if (res->res_handle == hdl)
    204 			return res;
    205 	}
    206 
    207 	return acpi_power_res_init(hdl);
    208 }
    209 
    210 bool
    211 acpi_power_register(struct acpi_devnode *ad)
    212 {
    213 
    214 	KASSERT(ad != NULL && ad->ad_root != NULL);
    215 
    216 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0)
    217 		return false;
    218 
    219 	return true;
    220 }
    221 
    222 void
    223 acpi_power_deregister(struct acpi_devnode *ad)
    224 {
    225 	struct acpi_power_res *res;
    226 
    227 	KASSERT(ad != NULL && ad->ad_root != NULL);
    228 
    229 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0)
    230 		return;
    231 
    232 	/*
    233 	 * Remove all references in each resource.
    234 	 */
    235 	TAILQ_FOREACH(res, &res_head, res_list)
    236 	    (void)acpi_power_res_deref(res, ad->ad_handle);
    237 }
    238 
    239 /*
    240  * Get the D-state of an ACPI device node.
    241  */
    242 bool
    243 acpi_power_get(struct acpi_devnode *ad, int *state)
    244 {
    245 	ACPI_STATUS rv;
    246 
    247 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0) {
    248 		rv = AE_SUPPORT;
    249 		goto fail;
    250 	}
    251 
    252 	rv = acpi_power_get_indirect(ad);
    253 
    254 	if (ACPI_FAILURE(rv))
    255 		goto fail;
    256 
    257 	KASSERT(ad->ad_state != ACPI_STATE_ERROR);
    258 
    259 	if (ad->ad_state < ACPI_STATE_D0 || ad->ad_state > ACPI_STATE_D3) {
    260 		rv = AE_BAD_VALUE;
    261 		goto fail;
    262 	}
    263 
    264 	if (state != NULL)
    265 		*state = ad->ad_state;
    266 
    267 	return true;
    268 
    269 fail:
    270 	ad->ad_state = ACPI_STATE_ERROR;
    271 
    272 	if (state != NULL)
    273 		*state = ad->ad_state;
    274 
    275 	aprint_error_dev(ad->ad_root, "failed to get power state "
    276 	    "for %s: %s\n", ad->ad_name, AcpiFormatException(rv));
    277 
    278 	return false;
    279 }
    280 
    281 #if 0
    282 /*
    283  * Unfortunately, comparable to _STA, there are systems
    284  * where the convenient _PSC is implemented incorrectly.
    285  */
    286 static ACPI_STATUS
    287 acpi_power_get_direct(struct acpi_devnode *ad)
    288 {
    289 	ACPI_INTEGER val = 0;
    290 	ACPI_STATUS rv;
    291 
    292 	rv = acpi_eval_integer(ad->ad_handle, "_PSC", &val);
    293 
    294 	KDASSERT((uint64_t)val < INT_MAX);
    295 
    296 	ad->ad_state = (int)val;
    297 
    298 	return rv;
    299 }
    300 #endif
    301 
    302 static ACPI_STATUS
    303 acpi_power_get_indirect(struct acpi_devnode *ad)
    304 {
    305 	ACPI_OBJECT *pkg;
    306 	ACPI_STATUS rv;
    307 	int i;
    308 
    309 	CTASSERT(ACPI_STATE_D0 == 0 && ACPI_STATE_D1 == 1);
    310 	CTASSERT(ACPI_STATE_D2 == 2 && ACPI_STATE_D3 == 3);
    311 
    312 	/*
    313 	 * The device is in a given D-state if all resources are on.
    314 	 * To derive this, evaluate all elements in each _PRx package
    315 	 * (x = 0 ... 3) and break if the noted condition becomes true.
    316 	 */
    317 	for (ad->ad_state = ACPI_STATE_D3, i = 0; i < ACPI_STATE_D3; i++) {
    318 
    319 		pkg = acpi_power_pkg_get(ad->ad_handle, i);
    320 
    321 		if (pkg == NULL)
    322 			continue;
    323 
    324 		/*
    325 		 * For each element in the _PRx package, evaluate _STA
    326 		 * and return AE_OK only if all power resources are on.
    327 		 */
    328 		rv = acpi_foreach_package_object(pkg, acpi_power_res_sta, ad);
    329 
    330 		if (ACPI_FAILURE(rv) && rv != AE_CTRL_TERMINATE)
    331 			goto out;
    332 
    333 		if (ACPI_SUCCESS(rv)) {
    334 			ad->ad_state = i;
    335 			goto out;
    336 		}
    337 
    338 		ACPI_FREE(pkg); pkg = NULL;
    339 	}
    340 
    341 	KASSERT(ad->ad_state == ACPI_STATE_D3);
    342 
    343 	return AE_OK;
    344 
    345 out:
    346 	ACPI_FREE(pkg);
    347 
    348 	return rv;
    349 }
    350 
    351 /*
    352  * Set the D-state of an ACPI device node.
    353  */
    354 bool
    355 acpi_power_set(struct acpi_devnode *ad, int state)
    356 {
    357 	ACPI_STATUS rv;
    358 	char path[5];
    359 	int old;
    360 
    361 	KASSERT(ad != NULL && ad->ad_root != NULL);
    362 
    363 	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) {
    364 		rv = AE_BAD_PARAMETER;
    365 		goto fail;
    366 	}
    367 
    368 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0) {
    369 		rv = AE_SUPPORT;
    370 		goto fail;
    371 	}
    372 
    373 	if (acpi_power_get(ad, &old) != true) {
    374 		rv = AE_NOT_FOUND;
    375 		goto fail;
    376 	}
    377 
    378 	KASSERT(ad->ad_state == old);
    379 	KASSERT(ad->ad_state != ACPI_STATE_ERROR);
    380 
    381 	if (ad->ad_state == state) {
    382 		rv = AE_ALREADY_EXISTS;
    383 		goto fail;
    384 	}
    385 
    386 	/*
    387 	 * It is only possible to go to D0 ("on") from D3 ("off").
    388 	 */
    389 	if (ad->ad_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) {
    390 		rv = AE_BAD_PARAMETER;
    391 		goto fail;
    392 	}
    393 
    394 	/*
    395 	 * We first sweep through the resources required
    396 	 * for the target state, turning things on and
    397 	 * building references. After this we dereference
    398 	 * the resources required for the current state,
    399 	 * turning the resources off as we go.
    400 	 */
    401 	rv = acpi_power_switch(ad, state, true);
    402 
    403 	if (ACPI_FAILURE(rv))
    404 		goto fail;
    405 
    406 	rv = acpi_power_switch(ad, ad->ad_state, false);
    407 
    408 	if (ACPI_FAILURE(rv))
    409 		goto fail;
    410 
    411 	ad->ad_state = state;
    412 
    413 	/*
    414 	 * Last but not least, invoke the power
    415 	 * state switch method, if available.
    416 	 */
    417 	(void)snprintf(path, sizeof(path), "_PS%d", state);
    418 	(void)AcpiEvaluateObject(ad->ad_handle, path, NULL, NULL);
    419 
    420 	aprint_debug_dev(ad->ad_root, "%s turned from "
    421 	    "D%d to D%d\n", ad->ad_name, old, state);
    422 
    423 	return true;
    424 
    425 fail:
    426 	ad->ad_state = ACPI_STATE_ERROR;
    427 
    428 	aprint_error_dev(ad->ad_root, "failed to set power state to D%d "
    429 	    "for %s: %s\n", state, ad->ad_name, AcpiFormatException(rv));
    430 
    431 	return false;
    432 }
    433 
    434 /*
    435  * Set the D-state of an ACPI device node from a handle.
    436  */
    437 bool
    438 acpi_power_set_from_handle(ACPI_HANDLE hdl, int state)
    439 {
    440 	struct acpi_softc *sc = acpi_softc; /* XXX. */
    441 	struct acpi_devnode *ad;
    442 
    443 	if (sc == NULL)
    444 		return false;
    445 
    446 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    447 
    448 		if (ad->ad_handle == hdl)
    449 			return acpi_power_set(ad, state);
    450 	}
    451 
    452 	aprint_error_dev(sc->sc_dev, "%s: failed to "
    453 	    "find node %s\n", __func__, acpi_xname(hdl));
    454 
    455 	return false;
    456 }
    457 
    458 static ACPI_STATUS
    459 acpi_power_switch(struct acpi_devnode *ad, int state, bool on)
    460 {
    461 	ACPI_OBJECT *pkg;
    462 	ACPI_STATUS rv;
    463 
    464 	pkg = acpi_power_pkg_get(ad->ad_handle, state);
    465 
    466 	if (pkg == NULL)
    467 		return AE_NOT_EXIST;
    468 
    469 	if (on != false)
    470 		rv = acpi_foreach_package_object(pkg, acpi_power_res_on, ad);
    471 	else
    472 		rv = acpi_foreach_package_object(pkg, acpi_power_res_off, ad);
    473 
    474 	ACPI_FREE(pkg);
    475 
    476 	return rv;
    477 }
    478 
    479 static ACPI_STATUS
    480 acpi_power_res_on(ACPI_OBJECT *elm, void *arg)
    481 {
    482 	struct acpi_devnode *ad = arg;
    483 	struct acpi_power_res *res;
    484 	ACPI_HANDLE hdl;
    485 	ACPI_STATUS rv;
    486 
    487 	/*
    488 	 * For each element in the _PRx package, first
    489 	 * fetch the reference handle and then search
    490 	 * for this handle from the power resource list.
    491 	 */
    492 	rv = acpi_eval_reference_handle(elm, &hdl);
    493 
    494 	if (ACPI_FAILURE(rv))
    495 		return rv;
    496 
    497 	res = acpi_power_res_get(hdl);
    498 
    499 	if (res == NULL)
    500 		return AE_NOT_FOUND;
    501 
    502 	/*
    503 	 * Reference the resource.
    504 	 */
    505 	rv = acpi_power_res_ref(res, ad->ad_handle);
    506 
    507 	if (ACPI_FAILURE(rv))
    508 		return rv;
    509 
    510 	/*
    511 	 * Turn the resource on.
    512 	 */
    513 	return AcpiEvaluateObject(res->res_handle, "_ON", NULL, NULL);
    514 }
    515 
    516 static ACPI_STATUS
    517 acpi_power_res_off(ACPI_OBJECT *elm, void *arg)
    518 {
    519 	struct acpi_devnode *ad = arg;
    520 	struct acpi_power_res *res;
    521 	ACPI_HANDLE hdl;
    522 	ACPI_STATUS rv;
    523 
    524 	rv = acpi_eval_reference_handle(elm, &hdl);
    525 
    526 	if (ACPI_FAILURE(rv))
    527 		return rv;
    528 
    529 	res = acpi_power_res_get(hdl);
    530 
    531 	if (res == NULL)
    532 		return AE_NOT_FOUND;
    533 
    534 	rv = acpi_power_res_deref(res, ad->ad_handle);
    535 
    536 	if (ACPI_FAILURE(rv))
    537 		return rv;
    538 
    539 	return AcpiEvaluateObject(res->res_handle, "_OFF", NULL, NULL);
    540 }
    541 
    542 static ACPI_STATUS
    543 acpi_power_res_ref(struct acpi_power_res *res, ACPI_HANDLE hdl)
    544 {
    545 	struct acpi_power_ref *ref, *tmp;
    546 
    547 	ref = kmem_zalloc(sizeof(*ref), KM_SLEEP);
    548 
    549 	if (ref == NULL)
    550 		return AE_NO_MEMORY;
    551 
    552 	mutex_enter(&res->res_mutex);
    553 
    554 	SIMPLEQ_FOREACH(tmp, &res->ref_head, ref_list) {
    555 
    556 		if (tmp->ref_handle == hdl)
    557 			goto out;
    558 	}
    559 
    560 	ref->ref_handle = hdl;
    561 	SIMPLEQ_INSERT_TAIL(&res->ref_head, ref, ref_list);
    562 	mutex_exit(&res->res_mutex);
    563 
    564 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: %s referenced "
    565 		"by %s?\n", __func__, res->res_name, acpi_xname(hdl)));
    566 
    567 	return AE_OK;
    568 
    569 out:
    570 	mutex_exit(&res->res_mutex);
    571 	kmem_free(ref, sizeof(*ref));
    572 
    573 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: %s already referenced "
    574 		"by %s?\n", __func__, res->res_name, acpi_xname(hdl)));
    575 
    576 	return AE_OK;
    577 }
    578 
    579 static ACPI_STATUS
    580 acpi_power_res_deref(struct acpi_power_res *res, ACPI_HANDLE hdl)
    581 {
    582 	struct acpi_power_ref *ref;
    583 
    584 	mutex_enter(&res->res_mutex);
    585 
    586 	if (SIMPLEQ_EMPTY(&res->ref_head) != 0) {
    587 		mutex_exit(&res->res_mutex);
    588 		return AE_OK;
    589 	}
    590 
    591 	SIMPLEQ_FOREACH(ref, &res->ref_head, ref_list) {
    592 
    593 		if (ref->ref_handle == hdl) {
    594 			SIMPLEQ_REMOVE(&res->ref_head,
    595 			    ref, acpi_power_ref, ref_list);
    596 			mutex_exit(&res->res_mutex);
    597 			kmem_free(ref, sizeof(*ref));
    598 			mutex_enter(&res->res_mutex);
    599 			break;
    600 		}
    601 	}
    602 
    603 	/*
    604 	 * If the queue remains non-empty,
    605 	 * something else is using the resource
    606 	 * and hence it can not be turned off.
    607 	 */
    608 	if (SIMPLEQ_EMPTY(&res->ref_head) == 0) {
    609 		mutex_exit(&res->res_mutex);
    610 		return AE_ABORT_METHOD;
    611 	}
    612 
    613 	mutex_exit(&res->res_mutex);
    614 
    615 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: %s dereferenced "
    616 		"by %s?\n", __func__, res->res_name, acpi_xname(hdl)));
    617 
    618 	return AE_OK;
    619 }
    620 
    621 static ACPI_STATUS
    622 acpi_power_res_sta(ACPI_OBJECT *elm, void *arg)
    623 {
    624 	ACPI_INTEGER val;
    625 	ACPI_HANDLE hdl;
    626 	ACPI_STATUS rv;
    627 
    628 	rv = acpi_eval_reference_handle(elm, &hdl);
    629 
    630 	if (ACPI_FAILURE(rv))
    631 		goto fail;
    632 
    633 	rv = acpi_eval_integer(hdl, "_STA", &val);
    634 
    635 	if (ACPI_FAILURE(rv))
    636 		goto fail;
    637 
    638 	KDASSERT((uint64_t)val < INT_MAX);
    639 
    640 	if ((int)val != ACPI_STA_POW_ON && (int)val != ACPI_STA_POW_OFF)
    641 		return AE_BAD_VALUE;
    642 
    643 	if ((int)val != ACPI_STA_POW_ON)
    644 		return AE_CTRL_TERMINATE; /* XXX: Not an error. */
    645 
    646 	return AE_OK;
    647 
    648 fail:
    649 	if (rv == AE_CTRL_TERMINATE)
    650 		rv = AE_ERROR;
    651 
    652 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "%s: failed to "
    653 		"evaluate _STA for %s: %s\n", __func__,
    654 		acpi_xname(hdl), AcpiFormatException(rv)));
    655 
    656 	return rv;
    657 }
    658 
    659 static ACPI_OBJECT *
    660 acpi_power_pkg_get(ACPI_HANDLE hdl, int state)
    661 {
    662 	char path[5] = "_PR?";
    663 	ACPI_OBJECT *obj;
    664 	ACPI_BUFFER buf;
    665 	ACPI_STATUS rv;
    666 
    667 	path[3] = '0' + state;
    668 
    669 	rv = acpi_eval_struct(hdl, path, &buf);
    670 
    671 	if (ACPI_FAILURE(rv))
    672 		goto fail;
    673 
    674 	if (buf.Length == 0) {
    675 		rv = AE_LIMIT;
    676 		goto fail;
    677 	}
    678 
    679 	obj = buf.Pointer;
    680 
    681 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    682 		rv = AE_TYPE;
    683 		goto fail;
    684 	}
    685 
    686 	if (obj->Package.Count == 0) {
    687 		rv = AE_LIMIT;
    688 		goto fail;
    689 	}
    690 
    691 	return obj;
    692 
    693 fail:
    694 	if (buf.Pointer != NULL)
    695 		ACPI_FREE(buf.Pointer);
    696 
    697 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "%s: failed to "
    698 		"evaluate %s for %s: %s\n", __func__, path,
    699 		acpi_xname(hdl), AcpiFormatException(rv)));
    700 
    701 	return NULL;
    702 }
    703 
    704 /*
    705  * XXX: Move this to acpi_util.c by refactoring
    706  *	acpi_name() to optionally return a single name.
    707  */
    708 static const char *
    709 acpi_xname(ACPI_HANDLE hdl)
    710 {
    711 	static char str[5];
    712 	ACPI_BUFFER buf;
    713 	ACPI_STATUS rv;
    714 
    715 	buf.Pointer = str;
    716 	buf.Length = sizeof(str);
    717 
    718 	rv = AcpiGetName(hdl, ACPI_SINGLE_NAME, &buf);
    719 
    720 	if (ACPI_FAILURE(rv))
    721 		return "????";
    722 
    723 	return str;
    724 }
    725