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