1/* $NetBSD: acpi_power.c,v 1.36 2022/05/31 20:28:57 mrg 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.36 2022/05/31 20:28:57 mrg 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
74ACPI_MODULE_NAME			("acpi_power")
75
76#define	ACPI_STA_POW_OFF		0x00
77#define	ACPI_STA_POW_ON			0x01
78
79struct 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
90static TAILQ_HEAD(, acpi_power_res) res_head =
91	TAILQ_HEAD_INITIALIZER(res_head);
92
93static int32_t acpi_power_acpinode = CTL_EOL;
94static int32_t acpi_power_powernode = CTL_EOL;
95
96static struct acpi_power_res	*acpi_power_res_init(ACPI_HANDLE);
97static struct acpi_power_res	*acpi_power_res_get(ACPI_HANDLE);
98
99static ACPI_STATUS	 acpi_power_get_direct(struct acpi_devnode *);
100static ACPI_STATUS	 acpi_power_get_indirect(struct acpi_devnode *);
101static ACPI_STATUS	 acpi_power_switch(struct acpi_devnode *,
102						   int, bool);
103static ACPI_STATUS	 acpi_power_res_ref(struct acpi_power_res *,
104					    ACPI_HANDLE);
105static ACPI_STATUS	 acpi_power_res_deref(struct acpi_power_res *,
106					      ACPI_HANDLE);
107static ACPI_STATUS	 acpi_power_res_sta(ACPI_OBJECT *, void *);
108
109static ACPI_OBJECT	*acpi_power_pkg_get(ACPI_HANDLE, int);
110static int		 acpi_power_sysctl(SYSCTLFN_PROTO);
111static const char	*acpi_xname(ACPI_HANDLE);
112
113static struct acpi_power_res *
114acpi_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
168out:
169	if (buf.Pointer != NULL)
170		ACPI_FREE(buf.Pointer);
171
172	return res;
173}
174
175static struct acpi_power_res *
176acpi_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
189bool
190acpi_power_register(ACPI_HANDLE hdl)
191{
192	return true;
193}
194
195void
196acpi_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 */
214bool
215acpi_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
248fail:
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
260static ACPI_STATUS
261acpi_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
275static ACPI_STATUS
276acpi_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 == 3);
284
285	/*
286	 * The device is in a given D-state if all resources are on.
287	 * To derive this, evaluate all elements in each _PRx package
288	 * (x = 0 ... 3) and break if the noted condition becomes true.
289	 */
290	for (ad->ad_state = ACPI_STATE_D3, i = 0; i < ACPI_STATE_D3; i++) {
291
292		pkg = acpi_power_pkg_get(ad->ad_handle, i);
293
294		if (pkg == NULL)
295			continue;
296
297		/*
298		 * For each element in the _PRx package, evaluate _STA
299		 * and return AE_OK only if all power resources are on.
300		 */
301		rv = acpi_foreach_package_object(pkg, acpi_power_res_sta, ad);
302
303		if (ACPI_FAILURE(rv) && rv != AE_CTRL_FALSE)
304			goto out;
305
306		if (ACPI_SUCCESS(rv)) {
307			ad->ad_state = i;
308			goto out;
309		}
310
311		ACPI_FREE(pkg); pkg = NULL;
312	}
313
314	KASSERT(ad->ad_state == ACPI_STATE_D3);
315
316	return AE_OK;
317
318out:
319	ACPI_FREE(pkg);
320
321	return rv;
322}
323
324/*
325 * Set the D-state of an ACPI device node.
326 */
327bool
328acpi_power_set(ACPI_HANDLE hdl, int state)
329{
330	struct acpi_devnode *ad = acpi_match_node(hdl);
331	ACPI_STATUS rv;
332	char path[5];
333	int old;
334
335	if (ad == NULL)
336		return false;
337
338	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) {
339		rv = AE_BAD_PARAMETER;
340		goto fail;
341	}
342
343	if (acpi_power_get(ad->ad_handle, &old) != true) {
344		rv = AE_NOT_FOUND;
345		goto fail;
346	}
347
348	KASSERT(ad->ad_state == old);
349	KASSERT(ad->ad_state != ACPI_STATE_ERROR);
350
351	if (ad->ad_state == state) {
352		rv = AE_ALREADY_EXISTS;
353		goto fail;
354	}
355
356	/*
357	 * It is only possible to go to D0 ("on") from D3 ("off").
358	 */
359	if (ad->ad_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) {
360		rv = AE_BAD_PARAMETER;
361		goto fail;
362	}
363
364	/*
365	 * As noted in ACPI 4.0 (appendix A.2.1), the bus power state
366	 * should never be lower than the highest state of one of its
367	 * devices. Consequently, we cannot set the state to a lower
368	 * (i.e. higher power) state than the parent device's state.
369	 */
370	if ((ad->ad_parent != NULL) &&
371	    (ad->ad_parent->ad_flags & ACPI_DEVICE_POWER) != 0) {
372
373		if (ad->ad_parent->ad_state > state) {
374			rv = AE_ABORT_METHOD;
375			goto fail;
376		}
377	}
378
379	/*
380	 * We first sweep through the resources required for the target
381	 * state, turning things on and building references. After this
382	 * we dereference the resources required for the current state,
383	 * turning the resources off as we go.
384	 */
385	rv = acpi_power_switch(ad, state, true);
386
387	if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
388		goto fail;
389
390	rv = acpi_power_switch(ad, ad->ad_state, false);
391
392	if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
393		goto fail;
394
395	/*
396	 * Last but not least, invoke the power state switch method,
397	 * if available. Because some systems use only _PSx for the
398	 * power state transitions, we do this even if there is no _PRx.
399	 */
400	(void)snprintf(path, sizeof(path), "_PS%d", state);
401	(void)AcpiEvaluateObject(ad->ad_handle, path, NULL, NULL);
402
403	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s turned from "
404		"D%d to D%d\n", ad->ad_name, old, state));
405
406	ad->ad_state = state;
407
408	return true;
409
410fail:
411	ad->ad_state = ACPI_STATE_ERROR;
412
413	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "failed to set power state to D%d "
414		"for %s: %s\n", state, ad->ad_name, AcpiFormatException(rv)));
415
416	return false;
417}
418
419static ACPI_STATUS
420acpi_power_switch(struct acpi_devnode *ad, int state, bool on)
421{
422	ACPI_OBJECT *elm, *pkg;
423	ACPI_STATUS rv = AE_OK;
424	ACPI_HANDLE hdl;
425	uint32_t i, n;
426
427	/*
428	 * For each element in the _PRx package, fetch
429	 * the reference handle, search for this handle
430	 * from the power resource queue, and turn the
431	 * resource behind the handle on or off.
432	 */
433	pkg = acpi_power_pkg_get(ad->ad_handle, state);
434
435	if (pkg == NULL)
436		return AE_CTRL_CONTINUE;
437
438	n = pkg->Package.Count;
439
440	for (i = 0; i < n; i++) {
441
442		elm = &pkg->Package.Elements[i];
443		rv = acpi_eval_reference_handle(elm, &hdl);
444
445		if (ACPI_FAILURE(rv))
446			continue;
447
448		(void)acpi_power_res(hdl, ad->ad_handle, on);
449	}
450
451	ACPI_FREE(pkg);
452
453	return rv;
454}
455
456ACPI_STATUS
457acpi_power_res(ACPI_HANDLE hdl, ACPI_HANDLE ref, bool on)
458{
459	struct acpi_power_res *res;
460	const char *str;
461	ACPI_STATUS rv;
462
463	/*
464	 * Search for the resource.
465	 */
466	res = acpi_power_res_get(hdl);
467
468	if (res == NULL)
469		return AE_NOT_FOUND;
470
471	if (ref == NULL)
472		return AE_BAD_PARAMETER;
473
474	/*
475	 * Adjust the reference counting. This is
476	 * necessary since a single power resource
477	 * can be shared by multiple devices.
478	 */
479	if (on) {
480		rv = acpi_power_res_ref(res, ref);
481		str = "_ON";
482	} else {
483		rv = acpi_power_res_deref(res, ref);
484		str = "_OFF";
485	}
486
487	if (ACPI_FAILURE(rv))
488		return rv;
489
490	/*
491	 * Turn the resource on or off.
492	 */
493	return AcpiEvaluateObject(res->res_handle, str, NULL, NULL);
494}
495
496static ACPI_STATUS
497acpi_power_res_ref(struct acpi_power_res *res, ACPI_HANDLE ref)
498{
499	size_t i, j = SIZE_MAX;
500
501	mutex_enter(&res->res_mutex);
502
503	for (i = 0; i < __arraycount(res->res_ref); i++) {
504
505		/*
506		 * Do not error out if the handle
507		 * has already been referenced.
508		 */
509		if (res->res_ref[i] == ref) {
510			mutex_exit(&res->res_mutex);
511			return AE_OK;
512		}
513
514		if (j == SIZE_MAX && res->res_ref[i] == NULL)
515			j = i;
516	}
517
518	if (j == SIZE_MAX) {
519		mutex_exit(&res->res_mutex);
520		return AE_LIMIT;
521	}
522
523	res->res_ref[j] = ref;
524	mutex_exit(&res->res_mutex);
525
526	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s referenced "
527		"by %s\n", res->res_name, acpi_xname(ref)));
528
529	return AE_OK;
530}
531
532static ACPI_STATUS
533acpi_power_res_deref(struct acpi_power_res *res, ACPI_HANDLE ref)
534{
535	size_t i;
536
537	mutex_enter(&res->res_mutex);
538
539	for (i = 0; i < __arraycount(res->res_ref); i++) {
540
541		if (res->res_ref[i] != ref)
542			continue;
543
544		res->res_ref[i] = NULL;
545		mutex_exit(&res->res_mutex);
546
547		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s dereferenced "
548			"by %s\n", res->res_name, acpi_xname(ref)));
549
550		return AE_OK;
551	}
552
553	/*
554	 * If the array remains to be non-empty,
555	 * something else is using the resource
556	 * and hence it can not be turned off.
557	 */
558	mutex_exit(&res->res_mutex);
559
560	return AE_ABORT_METHOD;
561}
562
563static ACPI_STATUS
564acpi_power_res_sta(ACPI_OBJECT *elm, void *arg)
565{
566	ACPI_INTEGER val;
567	ACPI_HANDLE hdl;
568	ACPI_STATUS rv;
569
570	rv = acpi_eval_reference_handle(elm, &hdl);
571
572	if (ACPI_FAILURE(rv))
573		goto fail;
574
575	rv = acpi_eval_integer(hdl, "_STA", &val);
576
577	if (ACPI_FAILURE(rv))
578		goto fail;
579
580	KDASSERT((uint64_t)val < INT_MAX);
581
582	if ((int)val != ACPI_STA_POW_ON && (int)val != ACPI_STA_POW_OFF)
583		return AE_BAD_VALUE;
584
585	if ((int)val != ACPI_STA_POW_ON)
586		return AE_CTRL_FALSE;		/* XXX: Not an error. */
587
588	return AE_OK;
589
590fail:
591	if (rv == AE_CTRL_FALSE)
592		rv = AE_ERROR;
593
594	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "failed to evaluate _STA "
595		"for %s: %s\n", acpi_xname(hdl), AcpiFormatException(rv)));
596
597	return rv;
598}
599
600static ACPI_OBJECT *
601acpi_power_pkg_get(ACPI_HANDLE hdl, int state)
602{
603	char path[5] = "_PR?";
604	ACPI_OBJECT *obj;
605	ACPI_BUFFER buf;
606	ACPI_STATUS rv;
607
608	path[3] = '0' + state;
609
610	rv = acpi_eval_struct(hdl, path, &buf);
611
612	if (ACPI_FAILURE(rv))
613		goto fail;
614
615	if (buf.Length == 0) {
616		rv = AE_LIMIT;
617		goto fail;
618	}
619
620	obj = buf.Pointer;
621
622	if (obj->Type != ACPI_TYPE_PACKAGE) {
623		rv = AE_TYPE;
624		goto fail;
625	}
626
627	if (obj->Package.Count == 0) {
628		rv = AE_LIMIT;
629		goto fail;
630	}
631
632	return obj;
633
634fail:
635	if (buf.Pointer != NULL)
636		ACPI_FREE(buf.Pointer);
637
638	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "failed to evaluate %s for "
639		"%s: %s\n", path, acpi_xname(hdl), AcpiFormatException(rv)));
640
641	return NULL;
642}
643
644SYSCTL_SETUP(sysctl_acpi_power_setup, "sysctl hw.acpi.power subtree setup")
645{
646	const struct sysctlnode *anode;
647	int err;
648
649	err = sysctl_createv(NULL, 0, NULL, &anode,
650	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
651	    NULL, NULL, 0, NULL, 0,
652	    CTL_HW, CTL_CREATE, CTL_EOL);
653
654	if (err != 0)
655		return;
656
657	acpi_power_acpinode = anode->sysctl_num;
658
659	err = sysctl_createv(NULL, 0, &anode, &anode,
660	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
661	    "power", SYSCTL_DESCR("ACPI device power states"),
662	    NULL, 0, NULL, 0,
663	    CTL_CREATE, CTL_EOL);
664
665	if (err != 0)
666		return;
667
668	acpi_power_powernode = anode->sysctl_num;
669}
670
671void
672acpi_power_add(struct acpi_devnode *ad)
673{
674	const char *str = NULL;
675	int err;
676
677	KASSERT(ad != NULL && ad->ad_root != NULL);
678	KASSERT((ad->ad_flags & ACPI_DEVICE_POWER) != 0);
679
680	if (acpi_power_acpinode == CTL_EOL ||
681	    acpi_power_powernode == CTL_EOL)
682		return;
683
684	if (ad->ad_device != NULL)
685		str = device_xname(ad->ad_device);
686#if NPCI > 0
687	else {
688		device_t dev = acpi_pcidev_find_dev(ad);
689
690		if (dev != NULL)
691			str = device_xname(dev);
692	}
693#endif
694
695	if (str == NULL)
696		return;
697
698	err = sysctl_createv(NULL, 0, NULL, NULL,
699	    CTLFLAG_READONLY, CTLTYPE_STRING, str,
700	    NULL, acpi_power_sysctl, 0, (void *)ad, 0, CTL_HW,
701	    acpi_power_acpinode, acpi_power_powernode,
702	    CTL_CREATE, CTL_EOL);
703
704	if (err != 0)
705		aprint_error_dev(ad->ad_root, "sysctl_createv"
706		    "(hw.acpi.power.%s) failed (err %d)\n", str, err);
707}
708
709static int
710acpi_power_sysctl(SYSCTLFN_ARGS)
711{
712	struct acpi_devnode *ad;
713	struct sysctlnode node;
714	int err, state;
715	char t[3];
716
717	node = *rnode;
718	ad = rnode->sysctl_data;
719
720	if (acpi_power_get(ad->ad_handle, &state) != true)
721		state = 0;
722
723	(void)memset(t, '\0', sizeof(t));
724	(void)snprintf(t, sizeof(t), "D%d", state);
725
726	node.sysctl_data = &t;
727
728	err = sysctl_lookup(SYSCTLFN_CALL(&node));
729
730	if (err || newp == NULL)
731		return err;
732
733	return 0;
734}
735
736/*
737 * XXX: Move this to acpi_util.c by refactoring
738 *	acpi_name() to optionally return a single name.
739 */
740static const char *
741acpi_xname(ACPI_HANDLE hdl)
742{
743	static char str[5];
744	ACPI_BUFFER buf;
745	ACPI_STATUS rv;
746
747	buf.Pointer = str;
748	buf.Length = sizeof(str);
749
750	rv = AcpiGetName(hdl, ACPI_SINGLE_NAME, &buf);
751
752	if (ACPI_FAILURE(rv))
753		return "????";
754
755	return str;
756}
757