Home | History | Annotate | Line # | Download | only in acpi
acpi_power.c revision 1.24
      1 /* $NetBSD: acpi_power.c,v 1.24 2011/01/02 06:05:47 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.24 2011/01/02 06:05:47 jruoho Exp $");
     60 
     61 #include <sys/param.h>
     62 #include <sys/kmem.h>
     63 #include <sys/mutex.h>
     64 #include <sys/sysctl.h>
     65 
     66 #include <dev/acpi/acpireg.h>
     67 #include <dev/acpi/acpivar.h>
     68 #include <dev/acpi/acpi_pci.h>
     69 #include <dev/acpi/acpi_power.h>
     70 
     71 #define _COMPONENT			ACPI_BUS_COMPONENT
     72 ACPI_MODULE_NAME			("acpi_power")
     73 
     74 #define	ACPI_STA_POW_OFF		0x00
     75 #define	ACPI_STA_POW_ON			0x01
     76 
     77 /*
     78  * References.
     79  */
     80 struct acpi_power_ref {
     81 	ACPI_HANDLE			ref_handle;
     82 
     83 	SIMPLEQ_ENTRY(acpi_power_ref)	ref_list;
     84 };
     85 
     86 /*
     87  * Resources.
     88  */
     89 struct acpi_power_res {
     90 	ACPI_HANDLE			res_handle;
     91 	ACPI_INTEGER			res_level;
     92 	ACPI_INTEGER			res_order;
     93 	char				res_name[5];
     94 	kmutex_t			res_mutex;
     95 
     96 	TAILQ_ENTRY(acpi_power_res)	res_list;
     97 	SIMPLEQ_HEAD(, acpi_power_ref)	ref_head;
     98 };
     99 
    100 static TAILQ_HEAD(, acpi_power_res) res_head =
    101 	TAILQ_HEAD_INITIALIZER(res_head);
    102 
    103 static int32_t acpi_power_acpinode = CTL_EOL;
    104 static int32_t acpi_power_powernode = CTL_EOL;
    105 
    106 static struct acpi_power_res	*acpi_power_res_init(ACPI_HANDLE);
    107 static struct acpi_power_res	*acpi_power_res_get(ACPI_HANDLE);
    108 
    109 static ACPI_STATUS	 acpi_power_get_direct(struct acpi_devnode *);
    110 static ACPI_STATUS	 acpi_power_get_indirect(struct acpi_devnode *);
    111 static ACPI_STATUS	 acpi_power_switch(struct acpi_devnode *,
    112 						   int, bool);
    113 static ACPI_STATUS	 acpi_power_res_ref(struct acpi_power_res *,
    114 					    ACPI_HANDLE);
    115 static ACPI_STATUS	 acpi_power_res_deref(struct acpi_power_res *,
    116 					      ACPI_HANDLE);
    117 static ACPI_STATUS	 acpi_power_res_sta(ACPI_OBJECT *, void *);
    118 
    119 static ACPI_OBJECT	*acpi_power_pkg_get(ACPI_HANDLE, int);
    120 static int		 acpi_power_sysctl(SYSCTLFN_PROTO);
    121 static const char	*acpi_xname(ACPI_HANDLE);
    122 
    123 static struct acpi_power_res *
    124 acpi_power_res_init(ACPI_HANDLE hdl)
    125 {
    126 	struct acpi_power_res *tmp = NULL;
    127 	struct acpi_power_res *res = NULL;
    128 	ACPI_OBJECT *obj;
    129 	ACPI_BUFFER buf;
    130 	ACPI_STATUS rv;
    131 
    132 	rv = acpi_eval_struct(hdl, NULL, &buf);
    133 
    134 	if (ACPI_FAILURE(rv))
    135 		goto out;
    136 
    137 	obj = buf.Pointer;
    138 
    139 	if (obj->Type != ACPI_TYPE_POWER) {
    140 		rv = AE_TYPE;
    141 		goto out;
    142 	}
    143 
    144 	res = kmem_zalloc(sizeof(*res), KM_SLEEP);
    145 
    146 	if (res == NULL) {
    147 		rv = AE_NO_MEMORY;
    148 		goto out;
    149 	}
    150 
    151 	res->res_handle = hdl;
    152 	res->res_level = obj->PowerResource.SystemLevel;
    153 	res->res_order = obj->PowerResource.ResourceOrder;
    154 
    155 	(void)strlcpy(res->res_name,
    156 	    acpi_xname(hdl), sizeof(res->res_name));
    157 
    158 	SIMPLEQ_INIT(&res->ref_head);
    159 	mutex_init(&res->res_mutex, MUTEX_DEFAULT, IPL_NONE);
    160 
    161 	/*
    162 	 * Power resources should be ordered.
    163 	 *
    164 	 * These *should* be enabled from low values to high
    165 	 * values and disabled from high values to low values.
    166 	 */
    167 	TAILQ_FOREACH(tmp, &res_head, res_list) {
    168 
    169 		if (res->res_order < tmp->res_order) {
    170 			TAILQ_INSERT_BEFORE(tmp, res, res_list);
    171 			break;
    172 		}
    173 	}
    174 
    175 	if (tmp == NULL)
    176 		TAILQ_INSERT_TAIL(&res_head, res, res_list);
    177 
    178 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s added to the "
    179 		"power resource queue\n", res->res_name));
    180 
    181 out:
    182 	if (buf.Pointer != NULL)
    183 		ACPI_FREE(buf.Pointer);
    184 
    185 	return res;
    186 }
    187 
    188 static struct acpi_power_res *
    189 acpi_power_res_get(ACPI_HANDLE hdl)
    190 {
    191 	struct acpi_power_res *res;
    192 
    193 	TAILQ_FOREACH(res, &res_head, res_list) {
    194 
    195 		if (res->res_handle == hdl)
    196 			return res;
    197 	}
    198 
    199 	return acpi_power_res_init(hdl);
    200 }
    201 
    202 bool
    203 acpi_power_register(ACPI_HANDLE hdl)
    204 {
    205 	struct acpi_devnode *ad = acpi_get_node(hdl);
    206 
    207 	if (ad == NULL)
    208 		return false;
    209 
    210 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0)
    211 		return false;
    212 
    213 	return true;
    214 }
    215 
    216 void
    217 acpi_power_deregister(ACPI_HANDLE hdl)
    218 {
    219 	struct acpi_devnode *ad = acpi_get_node(hdl);
    220 	struct acpi_power_res *res;
    221 
    222 	if (ad == NULL)
    223 		return;
    224 
    225 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0)
    226 		return;
    227 
    228 	/*
    229 	 * Remove all references in each resource.
    230 	 */
    231 	TAILQ_FOREACH(res, &res_head, res_list)
    232 	    (void)acpi_power_res_deref(res, ad->ad_handle);
    233 }
    234 
    235 /*
    236  * Get the D-state of an ACPI device node.
    237  */
    238 bool
    239 acpi_power_get(ACPI_HANDLE hdl, int *state)
    240 {
    241 	struct acpi_devnode *ad = acpi_get_node(hdl);
    242 	ACPI_STATUS rv;
    243 
    244 	if (ad == NULL)
    245 		return false;
    246 
    247 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0) {
    248 		rv = AE_SUPPORT;
    249 		goto fail;
    250 	}
    251 
    252 	/*
    253 	 * Because the _PSC control method, like _STA,
    254 	 * is known to be implemented incorrectly in
    255 	 * some systems, we first try to retrieve the
    256 	 * power state indirectly via power resources.
    257 	 */
    258 	rv = acpi_power_get_indirect(ad);
    259 
    260 	if (ACPI_FAILURE(rv))
    261 		rv = acpi_power_get_direct(ad);
    262 
    263 	if (ACPI_FAILURE(rv))
    264 		goto fail;
    265 
    266 	KASSERT(ad->ad_state != ACPI_STATE_ERROR);
    267 
    268 	if (ad->ad_state < ACPI_STATE_D0 || ad->ad_state > ACPI_STATE_D3) {
    269 		rv = AE_BAD_VALUE;
    270 		goto fail;
    271 	}
    272 
    273 	if (state != NULL)
    274 		*state = ad->ad_state;
    275 
    276 	return true;
    277 
    278 fail:
    279 	ad->ad_state = ACPI_STATE_ERROR;
    280 
    281 	if (state != NULL)
    282 		*state = ad->ad_state;
    283 
    284 	aprint_error_dev(ad->ad_root, "failed to get power state "
    285 	    "for %s: %s\n", ad->ad_name, AcpiFormatException(rv));
    286 
    287 	return false;
    288 }
    289 
    290 static ACPI_STATUS
    291 acpi_power_get_direct(struct acpi_devnode *ad)
    292 {
    293 	ACPI_INTEGER val = 0;
    294 	ACPI_STATUS rv;
    295 
    296 	rv = acpi_eval_integer(ad->ad_handle, "_PSC", &val);
    297 
    298 	KDASSERT((uint64_t)val < INT_MAX);
    299 
    300 	ad->ad_state = (int)val;
    301 
    302 	return rv;
    303 }
    304 
    305 static ACPI_STATUS
    306 acpi_power_get_indirect(struct acpi_devnode *ad)
    307 {
    308 	ACPI_OBJECT *pkg;
    309 	ACPI_STATUS rv;
    310 	int i;
    311 
    312 	CTASSERT(ACPI_STATE_D0 == 0 && ACPI_STATE_D1 == 1);
    313 	CTASSERT(ACPI_STATE_D2 == 2 && ACPI_STATE_D3 == 3);
    314 
    315 	/*
    316 	 * The device is in a given D-state if all resources are on.
    317 	 * To derive this, evaluate all elements in each _PRx package
    318 	 * (x = 0 ... 3) and break if the noted condition becomes true.
    319 	 */
    320 	for (ad->ad_state = ACPI_STATE_D3, i = 0; i < ACPI_STATE_D3; i++) {
    321 
    322 		pkg = acpi_power_pkg_get(ad->ad_handle, i);
    323 
    324 		if (pkg == NULL)
    325 			continue;
    326 
    327 		/*
    328 		 * For each element in the _PRx package, evaluate _STA
    329 		 * and return AE_OK only if all power resources are on.
    330 		 */
    331 		rv = acpi_foreach_package_object(pkg, acpi_power_res_sta, ad);
    332 
    333 		if (ACPI_FAILURE(rv) && rv != AE_CTRL_FALSE)
    334 			goto out;
    335 
    336 		if (ACPI_SUCCESS(rv)) {
    337 			ad->ad_state = i;
    338 			goto out;
    339 		}
    340 
    341 		ACPI_FREE(pkg); pkg = NULL;
    342 	}
    343 
    344 	KASSERT(ad->ad_state == ACPI_STATE_D3);
    345 
    346 	return AE_OK;
    347 
    348 out:
    349 	ACPI_FREE(pkg);
    350 
    351 	return rv;
    352 }
    353 
    354 /*
    355  * Set the D-state of an ACPI device node.
    356  */
    357 bool
    358 acpi_power_set(ACPI_HANDLE hdl, int state)
    359 {
    360 	struct acpi_devnode *ad = acpi_get_node(hdl);
    361 	ACPI_STATUS rv;
    362 	char path[5];
    363 	int old;
    364 
    365 	if (ad == NULL)
    366 		return false;
    367 
    368 	if ((ad->ad_flags & ACPI_DEVICE_POWER) == 0) {
    369 		rv = AE_SUPPORT;
    370 		goto fail;
    371 	}
    372 
    373 	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) {
    374 		rv = AE_BAD_PARAMETER;
    375 		goto fail;
    376 	}
    377 
    378 	if (acpi_power_get(ad->ad_handle, &old) != true) {
    379 		rv = AE_NOT_FOUND;
    380 		goto fail;
    381 	}
    382 
    383 	KASSERT(ad->ad_state == old);
    384 	KASSERT(ad->ad_state != ACPI_STATE_ERROR);
    385 
    386 	if (ad->ad_state == state) {
    387 		rv = AE_ALREADY_EXISTS;
    388 		goto fail;
    389 	}
    390 
    391 	/*
    392 	 * It is only possible to go to D0 ("on") from D3 ("off").
    393 	 */
    394 	if (ad->ad_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) {
    395 		rv = AE_BAD_PARAMETER;
    396 		goto fail;
    397 	}
    398 
    399 	/*
    400 	 * As noted in ACPI 4.0 (appendix A.2.1), the bus power state
    401 	 * should never be lower than the highest state of one of its
    402 	 * devices. Consequently, we cannot set the state to a lower
    403 	 * (i.e. higher power) state than the parent device's state.
    404 	 */
    405 	if ((ad->ad_parent != NULL) &&
    406 	    (ad->ad_parent->ad_flags & ACPI_DEVICE_POWER) != 0) {
    407 
    408 		if (ad->ad_parent->ad_state > state) {
    409 			rv = AE_ABORT_METHOD;
    410 			goto fail;
    411 		}
    412 	}
    413 
    414 	/*
    415 	 * We first sweep through the resources required for the target
    416 	 * state, turning things on and building references. After this
    417 	 * we dereference the resources required for the current state,
    418 	 * turning the resources off as we go.
    419 	 */
    420 	rv = acpi_power_switch(ad, state, true);
    421 
    422 	if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
    423 		goto fail;
    424 
    425 	rv = acpi_power_switch(ad, ad->ad_state, false);
    426 
    427 	if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
    428 		goto fail;
    429 
    430 	/*
    431 	 * Last but not least, invoke the power state switch method,
    432 	 * if available. Because some systems use only _PSx for the
    433 	 * power state transitions, we do this even if there is no _PRx.
    434 	 */
    435 	(void)snprintf(path, sizeof(path), "_PS%d", state);
    436 	(void)AcpiEvaluateObject(ad->ad_handle, path, NULL, NULL);
    437 
    438 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s turned from "
    439 		"D%d to D%d\n", ad->ad_name, old, state));
    440 
    441 	ad->ad_state = state;
    442 
    443 	return true;
    444 
    445 fail:
    446 	ad->ad_state = ACPI_STATE_ERROR;
    447 
    448 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "failed to set power state to D%d "
    449 		"for %s: %s\n", state, ad->ad_name, AcpiFormatException(rv)));
    450 
    451 	return false;
    452 }
    453 
    454 static ACPI_STATUS
    455 acpi_power_switch(struct acpi_devnode *ad, int state, bool on)
    456 {
    457 	ACPI_OBJECT *elm, *pkg;
    458 	ACPI_STATUS rv = AE_OK;
    459 	ACPI_HANDLE hdl;
    460 	uint32_t i, n;
    461 
    462 	/*
    463 	 * For each element in the _PRx package, fetch
    464 	 * the reference handle, search for this handle
    465 	 * from the power resource queue, and turn the
    466 	 * resource behind the handle on or off.
    467 	 */
    468 	pkg = acpi_power_pkg_get(ad->ad_handle, state);
    469 
    470 	if (pkg == NULL)
    471 		return AE_CTRL_CONTINUE;
    472 
    473 	n = pkg->Package.Count;
    474 
    475 	for (i = 0; i < n; i++) {
    476 
    477 		elm = &pkg->Package.Elements[i];
    478 		rv = acpi_eval_reference_handle(elm, &hdl);
    479 
    480 		if (ACPI_FAILURE(rv))
    481 			continue;
    482 
    483 		(void)acpi_power_res(hdl, ad->ad_handle, on);
    484 	}
    485 
    486 	ACPI_FREE(pkg);
    487 
    488 	return rv;
    489 }
    490 
    491 ACPI_STATUS
    492 acpi_power_res(ACPI_HANDLE hdl, ACPI_HANDLE ref, bool on)
    493 {
    494 	struct acpi_power_res *res;
    495 	const char *str;
    496 	ACPI_STATUS rv;
    497 
    498 	/*
    499 	 * Search for the resource.
    500 	 */
    501 	res = acpi_power_res_get(hdl);
    502 
    503 	if (res == NULL)
    504 		return AE_NOT_FOUND;
    505 
    506 	/*
    507 	 * (De)reference the resource.
    508 	 */
    509 	switch (on) {
    510 
    511 	case true:
    512 		rv = acpi_power_res_ref(res, ref);
    513 		str = "_ON";
    514 		break;
    515 
    516 	case false:
    517 		rv = acpi_power_res_deref(res, ref);
    518 		str = "_OFF";
    519 		break;
    520 
    521 	default:
    522 		return AE_BAD_PARAMETER;
    523 	}
    524 
    525 	if (ACPI_FAILURE(rv))
    526 		return rv;
    527 
    528 	/*
    529 	 * Turn the resource on or off.
    530 	 */
    531 	return AcpiEvaluateObject(res->res_handle, str, NULL, NULL);
    532 }
    533 
    534 static ACPI_STATUS
    535 acpi_power_res_ref(struct acpi_power_res *res, ACPI_HANDLE hdl)
    536 {
    537 	struct acpi_power_ref *ref, *tmp;
    538 
    539 	ref = kmem_zalloc(sizeof(*ref), KM_SLEEP);
    540 
    541 	if (ref == NULL)
    542 		return AE_NO_MEMORY;
    543 
    544 	mutex_enter(&res->res_mutex);
    545 
    546 	SIMPLEQ_FOREACH(tmp, &res->ref_head, ref_list) {
    547 
    548 		if (tmp->ref_handle == hdl)
    549 			goto out;
    550 	}
    551 
    552 	ref->ref_handle = hdl;
    553 	SIMPLEQ_INSERT_TAIL(&res->ref_head, ref, ref_list);
    554 	mutex_exit(&res->res_mutex);
    555 
    556 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s referenced "
    557 		"by %s\n", res->res_name, acpi_xname(hdl)));
    558 
    559 	return AE_OK;
    560 
    561 out:
    562 	mutex_exit(&res->res_mutex);
    563 	kmem_free(ref, sizeof(*ref));
    564 
    565 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s already referenced "
    566 		"by %s?\n", res->res_name, acpi_xname(hdl)));
    567 
    568 	return AE_OK;
    569 }
    570 
    571 static ACPI_STATUS
    572 acpi_power_res_deref(struct acpi_power_res *res, ACPI_HANDLE hdl)
    573 {
    574 	struct acpi_power_ref *ref;
    575 
    576 	mutex_enter(&res->res_mutex);
    577 
    578 	if (SIMPLEQ_EMPTY(&res->ref_head) != 0) {
    579 		mutex_exit(&res->res_mutex);
    580 		return AE_OK;
    581 	}
    582 
    583 	SIMPLEQ_FOREACH(ref, &res->ref_head, ref_list) {
    584 
    585 		if (ref->ref_handle == hdl) {
    586 			SIMPLEQ_REMOVE(&res->ref_head,
    587 			    ref, acpi_power_ref, ref_list);
    588 			mutex_exit(&res->res_mutex);
    589 			kmem_free(ref, sizeof(*ref));
    590 			mutex_enter(&res->res_mutex);
    591 			break;
    592 		}
    593 	}
    594 
    595 	/*
    596 	 * If the queue remains non-empty,
    597 	 * something else is using the resource
    598 	 * and hence it can not be turned off.
    599 	 */
    600 	if (SIMPLEQ_EMPTY(&res->ref_head) == 0) {
    601 		mutex_exit(&res->res_mutex);
    602 		return AE_ABORT_METHOD;
    603 	}
    604 
    605 	mutex_exit(&res->res_mutex);
    606 
    607 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s dereferenced "
    608 		"by %s\n", res->res_name, acpi_xname(hdl)));
    609 
    610 	return AE_OK;
    611 }
    612 
    613 static ACPI_STATUS
    614 acpi_power_res_sta(ACPI_OBJECT *elm, void *arg)
    615 {
    616 	ACPI_INTEGER val;
    617 	ACPI_HANDLE hdl;
    618 	ACPI_STATUS rv;
    619 
    620 	rv = acpi_eval_reference_handle(elm, &hdl);
    621 
    622 	if (ACPI_FAILURE(rv))
    623 		goto fail;
    624 
    625 	rv = acpi_eval_integer(hdl, "_STA", &val);
    626 
    627 	if (ACPI_FAILURE(rv))
    628 		goto fail;
    629 
    630 	KDASSERT((uint64_t)val < INT_MAX);
    631 
    632 	if ((int)val != ACPI_STA_POW_ON && (int)val != ACPI_STA_POW_OFF)
    633 		return AE_BAD_VALUE;
    634 
    635 	if ((int)val != ACPI_STA_POW_ON)
    636 		return AE_CTRL_FALSE;		/* XXX: Not an error. */
    637 
    638 	return AE_OK;
    639 
    640 fail:
    641 	if (rv == AE_CTRL_FALSE)
    642 		rv = AE_ERROR;
    643 
    644 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "failed to evaluate _STA "
    645 		"for %s: %s\n", acpi_xname(hdl), AcpiFormatException(rv)));
    646 
    647 	return rv;
    648 }
    649 
    650 static ACPI_OBJECT *
    651 acpi_power_pkg_get(ACPI_HANDLE hdl, int state)
    652 {
    653 	char path[5] = "_PR?";
    654 	ACPI_OBJECT *obj;
    655 	ACPI_BUFFER buf;
    656 	ACPI_STATUS rv;
    657 
    658 	path[3] = '0' + state;
    659 
    660 	rv = acpi_eval_struct(hdl, path, &buf);
    661 
    662 	if (ACPI_FAILURE(rv))
    663 		goto fail;
    664 
    665 	if (buf.Length == 0) {
    666 		rv = AE_LIMIT;
    667 		goto fail;
    668 	}
    669 
    670 	obj = buf.Pointer;
    671 
    672 	if (obj->Type != ACPI_TYPE_PACKAGE) {
    673 		rv = AE_TYPE;
    674 		goto fail;
    675 	}
    676 
    677 	if (obj->Package.Count == 0) {
    678 		rv = AE_LIMIT;
    679 		goto fail;
    680 	}
    681 
    682 	return obj;
    683 
    684 fail:
    685 	if (buf.Pointer != NULL)
    686 		ACPI_FREE(buf.Pointer);
    687 
    688 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "failed to evaluate %s for "
    689 		"%s: %s\n", path, acpi_xname(hdl), AcpiFormatException(rv)));
    690 
    691 	return NULL;
    692 }
    693 
    694 SYSCTL_SETUP(sysctl_acpi_power_setup, "sysctl hw.acpi.power subtree setup")
    695 {
    696 	const struct sysctlnode *anode;
    697 	int err;
    698 
    699 	err = sysctl_createv(NULL, 0, NULL, &anode,
    700 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
    701 	    NULL, NULL, 0, NULL, 0,
    702 	    CTL_HW, CTL_EOL);
    703 
    704 	if (err != 0)
    705 		return;
    706 
    707 	err = sysctl_createv(NULL, 0, &anode, &anode,
    708 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
    709 	    NULL, NULL, 0, NULL, 0,
    710 	    CTL_CREATE, CTL_EOL);
    711 
    712 	if (err != 0)
    713 		return;
    714 
    715 	acpi_power_acpinode = anode->sysctl_num;
    716 
    717 	err = sysctl_createv(NULL, 0, &anode, &anode,
    718 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
    719 	    "power", SYSCTL_DESCR("ACPI device power states"),
    720 	    NULL, 0, NULL, 0,
    721 	    CTL_CREATE, CTL_EOL);
    722 
    723 	if (err != 0)
    724 		return;
    725 
    726 	acpi_power_powernode = anode->sysctl_num;
    727 }
    728 
    729 void
    730 acpi_power_add(struct acpi_devnode *ad)
    731 {
    732 	const char *str = NULL;
    733 	device_t dev;
    734 	int err;
    735 
    736 	KASSERT(ad != NULL && ad->ad_root != NULL);
    737 	KASSERT((ad->ad_flags & ACPI_DEVICE_POWER) != 0);
    738 
    739 	if (acpi_power_acpinode == CTL_EOL ||
    740 	    acpi_power_powernode == CTL_EOL)
    741 		return;
    742 
    743 	if (ad->ad_device != NULL)
    744 		str = device_xname(ad->ad_device);
    745 	else {
    746 		dev = acpi_pcidev_find_dev(ad);
    747 
    748 		if (dev != NULL)
    749 			str = device_xname(dev);
    750 	}
    751 
    752 	if (str == NULL)
    753 		return;
    754 
    755 	err = sysctl_createv(NULL, 0, NULL, NULL,
    756 	    CTLFLAG_READONLY, CTLTYPE_STRING, str,
    757 	    NULL, acpi_power_sysctl, 0, ad, 0, CTL_HW,
    758 	    acpi_power_acpinode, acpi_power_powernode,
    759 	    CTL_CREATE, CTL_EOL);
    760 
    761 	if (err != 0)
    762 		aprint_error_dev(ad->ad_root, "sysctl_createv"
    763 		    "(hw.acpi.power.%s) failed (err %d)\n", ad->ad_name, err);
    764 }
    765 
    766 static int
    767 acpi_power_sysctl(SYSCTLFN_ARGS)
    768 {
    769 	struct acpi_devnode *ad;
    770 	struct sysctlnode node;
    771 	int err, state;
    772 	char t[3];
    773 
    774 	node = *rnode;
    775 	ad = rnode->sysctl_data;
    776 
    777 	if (acpi_power_get(ad->ad_handle, &state) != true)
    778 		state = 0;
    779 
    780 	(void)memset(t, '\0', sizeof(t));
    781 	(void)snprintf(t, sizeof(t), "D%d", state);
    782 
    783 	node.sysctl_data = &t;
    784 
    785 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
    786 
    787 	if (err || newp == NULL)
    788 		return err;
    789 
    790 	return 0;
    791 }
    792 
    793 /*
    794  * XXX: Move this to acpi_util.c by refactoring
    795  *	acpi_name() to optionally return a single name.
    796  */
    797 static const char *
    798 acpi_xname(ACPI_HANDLE hdl)
    799 {
    800 	static char str[5];
    801 	ACPI_BUFFER buf;
    802 	ACPI_STATUS rv;
    803 
    804 	buf.Pointer = str;
    805 	buf.Length = sizeof(str);
    806 
    807 	rv = AcpiGetName(hdl, ACPI_SINGLE_NAME, &buf);
    808 
    809 	if (ACPI_FAILURE(rv))
    810 		return "????";
    811 
    812 	return str;
    813 }
    814