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