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