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