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