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