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