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