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