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