resource manager: add optional resource id.
[dragonfly.git] / sys / dev / acpica5 / acpi_powerres.c
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2001 Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
f9d8cd12 26 * $FreeBSD: src/sys/dev/acpica/acpi_powerres.c,v 1.22 2004/04/14 17:58:19 njl Exp $
ba39e2e0 27 * $DragonFly: src/sys/dev/acpica5/acpi_powerres.c,v 1.5 2007/04/30 07:18:47 dillon Exp $
5ed44076
MD
28 */
29
30#include "opt_acpi.h"
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/bus.h>
35
36#include "acpi.h"
da42c799 37#include "accommon.h"
5ed44076
MD
38#include <dev/acpica5/acpivar.h>
39
40/*
41 * ACPI power resource management.
42 *
43 * Power resource behaviour is slightly complicated by the fact that
44 * a single power resource may provide power for more than one device.
45 * Thus, we must track the device(s) being powered by a given power
46 * resource, and only deactivate it when there are no powered devices.
47 *
48 * Note that this only manages resources for known devices. There is an
49 * ugly case where we may turn of power to a device which is in use because
50 * we don't know that it depends on a given resource. We should perhaps
51 * try to be smarter about this, but a more complete solution would involve
52 * scanning all of the ACPI namespace to find devices we're not currently
53 * aware of, and this raises questions about whether they should be left
54 * on, turned off, etc.
55 *
56 * XXX locking
57 */
58
59MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
60
61/* Hooks for the ACPI CA debugging infrastructure */
f9d8cd12 62#define _COMPONENT ACPI_POWERRES
5ed44076
MD
63ACPI_MODULE_NAME("POWERRES")
64
65/* Return values from _STA on a power resource */
66#define ACPI_PWR_OFF 0
67#define ACPI_PWR_ON 1
68
69/* A relationship between a power resource and a consumer. */
70struct acpi_powerreference {
71 struct acpi_powerconsumer *ar_consumer;
72 struct acpi_powerresource *ar_resource;
73 TAILQ_ENTRY(acpi_powerreference) ar_rlink; /* link on resource list */
74 TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */
75};
76
77/* A power-managed device. */
78struct acpi_powerconsumer {
79 /* Device which is powered */
80 ACPI_HANDLE ac_consumer;
81 int ac_state;
82 TAILQ_ENTRY(acpi_powerconsumer) ac_link;
83 TAILQ_HEAD(,acpi_powerreference) ac_references;
84};
85
86/* A power resource. */
87struct acpi_powerresource {
88 TAILQ_ENTRY(acpi_powerresource) ap_link;
89 TAILQ_HEAD(,acpi_powerreference) ap_references;
90 ACPI_HANDLE ap_resource;
91 ACPI_INTEGER ap_systemlevel;
92 ACPI_INTEGER ap_order;
93};
94
95static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource)
96 acpi_powerresources;
97static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer)
98 acpi_powerconsumers;
99
100static ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer);
f9d8cd12 101#ifdef notyet
5ed44076 102static ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer);
f9d8cd12 103#endif /* notyet */
5ed44076 104static ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res);
f9d8cd12 105#ifdef notyet
5ed44076 106static ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res);
f9d8cd12 107#endif /* notyet */
5ed44076
MD
108static void acpi_pwr_reference_resource(ACPI_OBJECT *obj,
109 void *arg);
110static ACPI_STATUS acpi_pwr_switch_power(void);
111static struct acpi_powerresource
112 *acpi_pwr_find_resource(ACPI_HANDLE res);
113static struct acpi_powerconsumer
114 *acpi_pwr_find_consumer(ACPI_HANDLE consumer);
115
116/* Initialise our lists. */
117static void
118acpi_pwr_init(void *junk)
119{
120 TAILQ_INIT(&acpi_powerresources);
121 TAILQ_INIT(&acpi_powerconsumers);
122}
ba39e2e0
MD
123SYSINIT(acpi_powerresource, SI_BOOT1_LOCK, SI_ORDER_ANY,
124 acpi_pwr_init, NULL);
5ed44076
MD
125
126/*
127 * Register a power resource.
128 *
129 * It's OK to call this if we already know about the resource.
130 */
131static ACPI_STATUS
132acpi_pwr_register_resource(ACPI_HANDLE res)
133{
134 ACPI_STATUS status;
135 ACPI_BUFFER buf;
136 ACPI_OBJECT *obj;
137 struct acpi_powerresource *rp, *srp;
138
139 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
140
141 rp = NULL;
142 buf.Pointer = NULL;
143
144 /* Look to see if we know about this resource */
145 if (acpi_pwr_find_resource(res) != NULL)
146 return_ACPI_STATUS (AE_OK); /* already know about it */
147
148 /* Allocate a new resource */
efda3bd0 149 rp = kmalloc(sizeof(*rp), M_ACPIPWR, M_INTWAIT | M_ZERO);
5ed44076
MD
150 TAILQ_INIT(&rp->ap_references);
151 rp->ap_resource = res;
152
153 /* Get the Power Resource object */
154 buf.Length = ACPI_ALLOCATE_BUFFER;
155 if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) {
156 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
157 goto out;
158 }
159 obj = buf.Pointer;
160 if (obj->Type != ACPI_TYPE_POWER) {
161 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
162 "questionable power resource object %s\n",
163 acpi_name(res)));
164 status = AE_TYPE;
165 goto out;
166 }
167 rp->ap_systemlevel = obj->PowerResource.SystemLevel;
168 rp->ap_order = obj->PowerResource.ResourceOrder;
169
170 /* Sort the resource into the list */
171 status = AE_OK;
172 srp = TAILQ_FIRST(&acpi_powerresources);
173 if (srp == NULL || rp->ap_order < srp->ap_order) {
174 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link);
175 goto done;
176 }
177 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) {
178 if (rp->ap_order < srp->ap_order) {
179 TAILQ_INSERT_BEFORE(srp, rp, ap_link);
180 goto done;
181 }
182 }
183 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
184
185 done:
186 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
187 "registered power resource %s\n", acpi_name(res)));
188
189 out:
190 if (buf.Pointer != NULL)
191 AcpiOsFree(buf.Pointer);
192 if (ACPI_FAILURE(status) && rp != NULL)
efda3bd0 193 kfree(rp, M_ACPIPWR);
5ed44076
MD
194 return_ACPI_STATUS (status);
195}
196
f9d8cd12 197#ifdef notyet
5ed44076
MD
198/*
199 * Deregister a power resource.
200 */
201static ACPI_STATUS
202acpi_pwr_deregister_resource(ACPI_HANDLE res)
203{
204 struct acpi_powerresource *rp;
205
206 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
207
208 rp = NULL;
209
210 /* Find the resource */
211 if ((rp = acpi_pwr_find_resource(res)) == NULL)
212 return_ACPI_STATUS (AE_BAD_PARAMETER);
213
214 /* Check that there are no consumers referencing this resource */
215 if (TAILQ_FIRST(&rp->ap_references) != NULL)
216 return_ACPI_STATUS (AE_BAD_PARAMETER);
217
218 /* Pull it off the list and free it */
219 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link);
efda3bd0 220 kfree(rp, M_ACPIPWR);
5ed44076
MD
221
222 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n",
223 acpi_name(res)));
224
225 return_ACPI_STATUS (AE_OK);
226}
f9d8cd12 227#endif /* notyet */
5ed44076
MD
228
229/*
230 * Register a power consumer.
231 *
232 * It's OK to call this if we already know about the consumer.
233 */
234static ACPI_STATUS
235acpi_pwr_register_consumer(ACPI_HANDLE consumer)
236{
237 struct acpi_powerconsumer *pc;
238
239 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
240
241 /* Check to see whether we know about this consumer already */
242 if ((pc = acpi_pwr_find_consumer(consumer)) != NULL)
243 return_ACPI_STATUS (AE_OK);
244
245 /* Allocate a new power consumer */
efda3bd0 246 pc = kmalloc(sizeof(*pc), M_ACPIPWR, M_INTWAIT);
5ed44076
MD
247 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link);
248 TAILQ_INIT(&pc->ac_references);
249 pc->ac_consumer = consumer;
250
251 /* XXX we should try to find its current state */
252 pc->ac_state = ACPI_STATE_UNKNOWN;
253
254 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n",
255 acpi_name(consumer)));
256
257 return_ACPI_STATUS (AE_OK);
258}
259
f9d8cd12 260#ifdef notyet
5ed44076
MD
261/*
262 * Deregister a power consumer.
263 *
264 * This should only be done once the consumer has been powered off.
265 * (XXX is this correct? Check once implemented)
266 */
267static ACPI_STATUS
268acpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
269{
270 struct acpi_powerconsumer *pc;
271
272 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
273
274 /* Find the consumer */
275 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
276 return_ACPI_STATUS (AE_BAD_PARAMETER);
277
278 /* Make sure the consumer's not referencing anything right now */
279 if (TAILQ_FIRST(&pc->ac_references) != NULL)
280 return_ACPI_STATUS (AE_BAD_PARAMETER);
281
282 /* Pull the consumer off the list and free it */
283 TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link);
284
285 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n",
286 acpi_name(consumer)));
287
288 return_ACPI_STATUS (AE_OK);
289}
f9d8cd12 290#endif /* notyet */
5ed44076
MD
291
292/*
293 * Set a power consumer to a particular power state.
294 */
295ACPI_STATUS
296acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state)
297{
298 struct acpi_powerconsumer *pc;
299 struct acpi_powerreference *pr;
300 ACPI_HANDLE method_handle, reslist_handle, pr0_handle;
301 ACPI_BUFFER reslist_buffer;
302 ACPI_OBJECT *reslist_object;
303 ACPI_STATUS status;
304 char *method_name, *reslist_name;
305 int res_changed;
306
307 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
308
f9d8cd12
MD
309 /* It's never ok to switch a non-existent consumer. */
310 if (consumer == NULL)
311 return_ACPI_STATUS (AE_NOT_FOUND);
312
5ed44076
MD
313 /* Find the consumer */
314 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
315 if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
316 return_ACPI_STATUS (status);
317 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
318 return_ACPI_STATUS (AE_ERROR); /* something very wrong */
319 }
320 }
321
322 /* Check for valid transitions */
323 if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
324 return_ACPI_STATUS (AE_BAD_PARAMETER); /* can only go to D0 from D3 */
325
326 /* Find transition mechanism(s) */
327 switch(state) {
328 case ACPI_STATE_D0:
329 method_name = "_PS0";
330 reslist_name = "_PR0";
331 break;
332 case ACPI_STATE_D1:
333 method_name = "_PS1";
334 reslist_name = "_PR1";
335 break;
336 case ACPI_STATE_D2:
337 method_name = "_PS2";
338 reslist_name = "_PR2";
339 break;
340 case ACPI_STATE_D3:
341 method_name = "_PS3";
342 reslist_name = "_PR3";
343 break;
344 default:
345 return_ACPI_STATUS (AE_BAD_PARAMETER);
346 }
347 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
348 acpi_name(consumer), pc->ac_state, state));
349
350 /*
351 * Verify that this state is supported, ie. one of method or
352 * reslist must be present. We need to do this before we go
353 * dereferencing resources (since we might be trying to go to
354 * a state we don't support).
355 *
356 * Note that if any states are supported, the device has to
357 * support D0 and D3. It's never an error to try to go to
358 * D0.
359 */
360 reslist_buffer.Pointer = NULL;
361 reslist_object = NULL;
362 if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
363 method_handle = NULL;
364 if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
365 reslist_handle = NULL;
366 if (reslist_handle == NULL && method_handle == NULL) {
367 if (state == ACPI_STATE_D0) {
368 pc->ac_state = ACPI_STATE_D0;
369 return_ACPI_STATUS (AE_OK);
370 }
371 if (state != ACPI_STATE_D3)
372 goto bad;
373
374 /* Turn off the resources listed in _PR0 to go to D3. */
375 if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle)))
376 goto bad;
377 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
378 status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
379 if (ACPI_FAILURE(status))
380 goto bad;
381 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
382 if (reslist_object->Type != ACPI_TYPE_PACKAGE ||
383 reslist_object->Package.Count == 0) {
384
385 goto bad;
386 }
387 AcpiOsFree(reslist_buffer.Pointer);
388 reslist_buffer.Pointer = NULL;
389 reslist_object = NULL;
390 }
391
392 /*
393 * Check that we can actually fetch the list of power resources
394 */
395 if (reslist_handle != NULL) {
396 reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
397 status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
398 &reslist_buffer);
399 if (ACPI_FAILURE(status)) {
400 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
401 "can't evaluate resource list %s\n",
402 acpi_name(reslist_handle)));
403 goto out;
404 }
405 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
406 if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
407 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
408 "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
409 reslist_object->Type));
410 status = AE_TYPE;
411 goto out;
412 }
413 }
414
415 /*
416 * Now we are ready to switch, so kill off any current power
417 * resource references.
418 */
419 res_changed = 0;
420 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
421 res_changed = 1;
422 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
423 acpi_name(pr->ar_resource->ap_resource)));
424 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
425 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
efda3bd0 426 kfree(pr, M_ACPIPWR);
5ed44076
MD
427 }
428
429 /*
430 * Add new power resource references, if we have any. Traverse the
431 * package that we got from evaluating reslist_handle, and look up each
432 * of the resources that are referenced.
433 */
434 if (reslist_object != NULL) {
435 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n",
436 reslist_object->Package.Count));
437 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
438 pc);
439 res_changed = 1;
440 }
441
442 /*
443 * If we changed anything in the resource list, we need to run a switch
444 * pass now.
445 */
446 if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
447 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
448 "failed to switch resources from %s to D%d\n",
449 acpi_name(consumer), state));
450
451 /* XXX is this appropriate? Should we return to previous state? */
452 goto out;
453 }
454
455 /* Invoke power state switch method (if present) */
456 if (method_handle != NULL) {
457 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
458 "invoking state transition method %s\n",
459 acpi_name(method_handle)));
460 status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
461 if (ACPI_FAILURE(status)) {
462 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
463 AcpiFormatException(status)));
464 pc->ac_state = ACPI_STATE_UNKNOWN;
465
466 /* XXX Should we return to previous state? */
467 goto out;
468 }
469 }
470
471 /* Transition was successful */
472 pc->ac_state = state;
473 return_ACPI_STATUS (AE_OK);
474
475 bad:
476 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
477 "attempt to set unsupported state D%d\n", state));
478 status = AE_BAD_PARAMETER;
479
480 out:
481 if (reslist_buffer.Pointer != NULL)
482 AcpiOsFree(reslist_buffer.Pointer);
483 return_ACPI_STATUS (status);
484}
485
486/*
487 * Called to create a reference between a power consumer and a power resource
488 * identified in the object.
489 */
490static void
491acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
492{
493 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg;
494 struct acpi_powerreference *pr;
495 struct acpi_powerresource *rp;
496 ACPI_HANDLE res;
497 ACPI_STATUS status;
498
499 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
500
f9d8cd12
MD
501 res = acpi_GetReference(NULL, obj);
502 if (res == NULL) {
5ed44076 503 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
f9d8cd12 504 "can't create a power reference for object type %d\n",
5ed44076
MD
505 obj->Type));
506 return_VOID;
507 }
508
509 /* Create/look up the resource */
510 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
511 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
512 "couldn't register power resource %s - %s\n",
513 obj->String.Pointer, AcpiFormatException(status)));
514 return_VOID;
515 }
516 if ((rp = acpi_pwr_find_resource(res)) == NULL) {
517 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
518 return_VOID;
519 }
520 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
521 acpi_name(rp->ap_resource)));
522
523 /* Create a reference between the consumer and resource */
efda3bd0 524 pr = kmalloc(sizeof(*pr), M_ACPIPWR, M_INTWAIT | M_ZERO);
5ed44076
MD
525 pr->ar_consumer = pc;
526 pr->ar_resource = rp;
527 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
528 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
529
530 return_VOID;
531}
532
533
534/*
535 * Switch power resources to conform to the desired state.
536 *
537 * Consumers may have modified the power resource list in an arbitrary
538 * fashion; we sweep it in sequence order.
539 */
540static ACPI_STATUS
541acpi_pwr_switch_power(void)
542{
543 struct acpi_powerresource *rp;
544 ACPI_STATUS status;
545 int cur;
546
547 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
548
549 /*
550 * Sweep the list forwards turning things on.
551 */
552 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
553 if (TAILQ_FIRST(&rp->ap_references) == NULL) {
554 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
555 "%s has no references, not turning on\n",
556 acpi_name(rp->ap_resource)));
557 continue;
558 }
559
560 /* We could cache this if we trusted it not to change under us */
f9d8cd12 561 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
5ed44076
MD
562 if (ACPI_FAILURE(status)) {
563 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
564 acpi_name(rp->ap_resource), status));
565
566 /* XXX is this correct? Always switch if in doubt? */
567 continue;
568 }
569
570 /*
571 * Switch if required. Note that we ignore the result of the switch
572 * effort; we don't know what to do if it fails, so checking wouldn't
573 * help much.
574 */
575 if (cur != ACPI_PWR_ON) {
576 status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
577 if (ACPI_FAILURE(status)) {
578 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
579 "failed to switch %s on - %s\n",
580 acpi_name(rp->ap_resource),
581 AcpiFormatException(status)));
582 } else {
583 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
584 acpi_name(rp->ap_resource)));
585 }
586 } else {
587 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
588 acpi_name(rp->ap_resource)));
589 }
590 }
591
592 /* Sweep the list backwards turning things off. */
593 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
594 ap_link) {
595
596 if (TAILQ_FIRST(&rp->ap_references) != NULL) {
597 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
598 "%s has references, not turning off\n",
599 acpi_name(rp->ap_resource)));
600 continue;
601 }
602
603 /* We could cache this if we trusted it not to change under us */
f9d8cd12 604 status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
5ed44076
MD
605 if (ACPI_FAILURE(status)) {
606 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
607 acpi_name(rp->ap_resource), status));
608 /* XXX is this correct? Always switch if in doubt? */
609 continue;
610 }
611
612 /*
613 * Switch if required. Note that we ignore the result of the switch
614 * effort; we don't know what to do if it fails, so checking wouldn't
615 * help much.
616 */
617 if (cur != ACPI_PWR_OFF) {
618 status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
619 if (ACPI_FAILURE(status)) {
620 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
621 "failed to switch %s off - %s\n",
622 acpi_name(rp->ap_resource),
623 AcpiFormatException(status)));
624 } else {
625 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
626 acpi_name(rp->ap_resource)));
627 }
628 } else {
629 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
630 acpi_name(rp->ap_resource)));
631 }
632 }
633
634 return_ACPI_STATUS (AE_OK);
635}
636
637/*
638 * Find a power resource's control structure.
639 */
640static struct acpi_powerresource *
641acpi_pwr_find_resource(ACPI_HANDLE res)
642{
643 struct acpi_powerresource *rp;
644
645 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
646
647 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
648 if (rp->ap_resource == res)
649 break;
650 }
651
652 return_PTR (rp);
653}
654
655/*
656 * Find a power consumer's control structure.
657 */
658static struct acpi_powerconsumer *
659acpi_pwr_find_consumer(ACPI_HANDLE consumer)
660{
661 struct acpi_powerconsumer *pc;
662
663 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
664
665 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
666 if (pc->ac_consumer == consumer)
667 break;
668 }
669
670 return_PTR (pc);
671}