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