183 184 /* sort the resource into the list */ 185 status = AE_OK; 186 srp = TAILQ_FIRST(&acpi_powerresources); 187 if ((srp == NULL) || (rp->ap_order < srp->ap_order)) { 188 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 189 goto done; 190 } 191 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) 192 if (rp->ap_order < srp->ap_order) { 193 TAILQ_INSERT_BEFORE(srp, rp, ap_link); 194 goto done; 195 } 196 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 197 198 done: 199 DEBUG_PRINT(TRACE_OBJECTS, ("registered power resource %s\n", acpi_name(res))); 200 out: 201 if (obj != NULL) 202 AcpiOsFree(obj); 203 if ((status != AE_OK) && (rp != NULL)) 204 free(rp, M_ACPIPWR); 205 return_ACPI_STATUS(status); 206} 207 208/* 209 * Deregister a power resource. 210 */ 211static ACPI_STATUS 212acpi_pwr_deregister_resource(ACPI_HANDLE res) 213{ 214 struct acpi_powerresource *rp; 215 216 FUNCTION_TRACE(__func__); 217 218 rp = NULL; 219 220 /* find the resource */ 221 if ((rp = acpi_pwr_find_resource(res)) == NULL) 222 return_ACPI_STATUS(AE_BAD_PARAMETER); 223 224 /* check that there are no consumers referencing this resource */ 225 if (TAILQ_FIRST(&rp->ap_references) != NULL) 226 return_ACPI_STATUS(AE_BAD_PARAMETER); 227 228 /* pull it off the list and free it */ 229 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 230 free(rp, M_ACPIPWR); 231 232 DEBUG_PRINT(TRACE_OBJECTS, ("deregistered power resource %s\n", acpi_name(res))); 233 234 return_ACPI_STATUS(AE_OK); 235} 236 237/* 238 * Register a power consumer. 239 * 240 * It's OK to call this if we already know about the consumer. 241 */ 242static ACPI_STATUS 243acpi_pwr_register_consumer(ACPI_HANDLE consumer) 244{ 245 struct acpi_powerconsumer *pc; 246 247 FUNCTION_TRACE(__func__); 248 249 /* check to see whether we know about this consumer already */ 250 if ((pc = acpi_pwr_find_consumer(consumer)) != NULL) 251 return_ACPI_STATUS(AE_OK); 252 253 /* allocate a new power consumer */ 254 if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 255 return_ACPI_STATUS(AE_NO_MEMORY); 256 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 257 TAILQ_INIT(&pc->ac_references); 258 pc->ac_consumer = consumer; 259 260 pc->ac_state = ACPI_STATE_UNKNOWN; /* XXX we should try to find its current state */ 261 262 DEBUG_PRINT(TRACE_OBJECTS, ("registered power consumer %s\n", acpi_name(consumer))); 263 264 return_ACPI_STATUS(AE_OK); 265} 266 267/* 268 * Deregister a power consumer. 269 * 270 * This should only be done once the consumer has been powered off. 271 * (XXX is this correct? Check once implemented) 272 */ 273static ACPI_STATUS 274acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 275{ 276 struct acpi_powerconsumer *pc; 277 278 FUNCTION_TRACE(__func__); 279 280 /* find the consumer */ 281 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) 282 return_ACPI_STATUS(AE_BAD_PARAMETER); 283 284 /* make sure the consumer's not referencing anything right now */ 285 if (TAILQ_FIRST(&pc->ac_references) != NULL) 286 return_ACPI_STATUS(AE_BAD_PARAMETER); 287 288 /* pull the consumer off the list and free it */ 289 TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); 290 291 DEBUG_PRINT(TRACE_OBJECTS, ("deregistered power consumer %s\n", acpi_name(consumer))); 292 293 return_ACPI_STATUS(AE_OK); 294} 295 296/* 297 * Set a power consumer to a particular power state. 298 */ 299ACPI_STATUS 300acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) 301{ 302 struct acpi_powerconsumer *pc; 303 struct acpi_powerreference *pr; 304 ACPI_HANDLE method_handle, reslist_handle; 305 ACPI_BUFFER reslist_buffer; 306 ACPI_OBJECT *reslist_object; 307 ACPI_STATUS status; 308 char *method_name, *reslist_name; 309 int res_changed; 310 311 FUNCTION_TRACE(__func__); 312 313 /* find the consumer */ 314 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 315 if ((status = acpi_pwr_register_consumer(consumer)) != AE_OK) 316 return_ACPI_STATUS(status); 317 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 318 return_ACPI_STATUS(AE_ERROR); /* something very wrong */ 319 } 320 } 321 322 /* check for valid transitions */ 323 if ((pc->ac_state == ACPI_STATE_D3) && (state != ACPI_STATE_D0)) 324 return_ACPI_STATUS(AE_BAD_PARAMETER); /* can only go to D0 from D3 */ 325 326 /* find transition mechanism(s) */ 327 switch(state) { 328 case ACPI_STATE_D0: 329 method_name = "_PS0"; 330 reslist_name = "_PR0"; 331 break; 332 case ACPI_STATE_D1: 333 method_name = "_PS1"; 334 reslist_name = "_PR1"; 335 break; 336 case ACPI_STATE_D2: 337 method_name = "_PS2"; 338 reslist_name = "_PR2"; 339 break; 340 case ACPI_STATE_D3: 341 method_name = "_PS3"; 342 reslist_name = "_PR3"; 343 break; 344 default: 345 return_ACPI_STATUS(AE_BAD_PARAMETER); 346 } 347 DEBUG_PRINT(TRACE_OBJECTS, ("setup to switch %s D%d -> D%d\n", 348 acpi_name(consumer), pc->ac_state, state)); 349 350 /* 351 * Verify that this state is supported, ie. one of method or 352 * reslist must be present. We need to do this before we go 353 * dereferencing resources (since we might be trying to go to 354 * a state we don't support). 355 * 356 * Note that if any states are supported, the device has to 357 * support D0 and D3. It's never an error to try to go to 358 * D0. 359 */ 360 if (AcpiGetHandle(consumer, method_name, &method_handle) != AE_OK) 361 method_handle = NULL; 362 if (AcpiGetHandle(consumer, reslist_name, &reslist_handle) != AE_OK) 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 DEBUG_PRINT(TRACE_OBJECTS, ("attempt to set unsupported state D%d\n", 370 state)); 371 return_ACPI_STATUS(AE_BAD_PARAMETER); 372 } 373 374 /* 375 * Check that we can actually fetch the list of power resources 376 */ 377 if (reslist_handle != NULL) { 378 if ((status = acpi_EvaluateIntoBuffer(reslist_handle, NULL, NULL, &reslist_buffer)) != AE_OK) { 379 DEBUG_PRINT(TRACE_OBJECTS, ("can't evaluate resource list %s\n", 380 acpi_name(reslist_handle))); 381 return_ACPI_STATUS(status); 382 } 383 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 384 if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 385 DEBUG_PRINT(TRACE_OBJECTS, ("resource list is not ACPI_TYPE_PACKAGE (%d)\n", 386 reslist_object->Type)); 387 return_ACPI_STATUS(AE_TYPE); 388 } 389 } else { 390 reslist_object = NULL; 391 } 392 393 /* 394 * Now we are ready to switch, so kill off any current power resource references. 395 */ 396 res_changed = 0; 397 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 398 res_changed = 1; 399 DEBUG_PRINT(TRACE_OBJECTS, ("removing reference to %s\n", acpi_name(pr->ar_resource->ap_resource))); 400 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 401 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 402 free(pr, M_ACPIPWR); 403 } 404 405 /* 406 * Add new power resource references, if we have any. Traverse the 407 * package that we got from evaluating reslist_handle, and look up each 408 * of the resources that are referenced. 409 */ 410 if (reslist_object != NULL) { 411 DEBUG_PRINT(TRACE_OBJECTS, ("referencing %d new resources\n", 412 reslist_object->Package.Count)); 413 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, pc); 414 res_changed = 1; 415 } 416 417 /* 418 * If we changed anything in the resource list, we need to run a switch 419 * pass now. 420 */ 421 if ((status = acpi_pwr_switch_power()) != AE_OK) { 422 DEBUG_PRINT(TRACE_OBJECTS, ("failed to correctly switch resources to move %s to D%d\n", 423 acpi_name(consumer), state)); 424 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */ 425 } 426 427 /* invoke power state switch method (if present) */ 428 if (method_handle != NULL) { 429 DEBUG_PRINT(TRACE_OBJECTS, ("invoking state transition method %s\n", 430 acpi_name(method_handle))); 431 if ((status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL)) != AE_OK) 432 pc->ac_state = ACPI_STATE_UNKNOWN; 433 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */ 434 } 435 436 /* transition was successful */ 437 pc->ac_state = state; 438 return_ACPI_STATUS(AE_OK); 439} 440 441/* 442 * Called to create a reference between a power consumer and a power resource 443 * identified in the object. 444 */ 445static void 446acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 447{ 448 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 449 struct acpi_powerreference *pr; 450 struct acpi_powerresource *rp; 451 ACPI_HANDLE res; 452 ACPI_STATUS status; 453 454 FUNCTION_TRACE(__func__); 455 456 /* check the object type */ 457 if (obj->Type != ACPI_TYPE_STRING) { 458 DEBUG_PRINT(TRACE_OBJECTS, ("don't know how to create a power reference to object type %d\n", 459 obj->Type)); 460 return_VOID; 461 } 462 463 DEBUG_PRINT(TRACE_OBJECTS, ("building reference from %s to %s\n", 464 acpi_name(pc->ac_consumer), obj->String.Pointer)); 465 466 /* get the handle of the resource */ 467 if (ACPI_FAILURE(status = AcpiGetHandle(NULL, obj->String.Pointer, &res))) { 468 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't find power resource %s\n", 469 obj->String.Pointer)); 470 return_VOID; 471 } 472 473 /* create/look up the resource */ 474 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 475 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't register power resource %s - %s\n", 476 obj->String.Pointer, acpi_strerror(status))); 477 return_VOID; 478 } 479 if ((rp = acpi_pwr_find_resource(res)) == NULL) { 480 DEBUG_PRINT(TRACE_OBJECTS, ("power resource list corrupted\n")); 481 return_VOID; 482 } 483 DEBUG_PRINT(TRACE_OBJECTS, ("found power resource %s\n", acpi_name(rp->ap_resource))); 484 485 /* create a reference between the consumer and resource */ 486 if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 487 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't allocate memory for a power consumer reference\n")); 488 return_VOID; 489 } 490 pr->ar_consumer = pc; 491 pr->ar_resource = rp; 492 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 493 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 494 495 return_VOID; 496} 497 498 499/* 500 * Switch power resources to conform to the desired state. 501 * 502 * Consumers may have modified the power resource list in an arbitrary 503 * fashion; we sweep it in sequence order. 504 */ 505static ACPI_STATUS 506acpi_pwr_switch_power(void) 507{ 508 struct acpi_powerresource *rp; 509 ACPI_STATUS status; 510 int cur; 511 512 FUNCTION_TRACE(__func__); 513 514 /* 515 * Sweep the list forwards turning things on. 516 */ 517 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 518 if (TAILQ_FIRST(&rp->ap_references) == NULL) { 519 DEBUG_PRINT(TRACE_OBJECTS, ("%s has no references, not turning on\n", 520 acpi_name(rp->ap_resource))); 521 continue; 522 } 523 524 /* we could cache this if we trusted it not to change under us */ 525 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) { 526 DEBUG_PRINT(TRACE_OBJECTS, ("can't get status of %s - %d\n", 527 acpi_name(rp->ap_resource), status)); 528 continue; /* XXX is this correct? Always switch if in doubt? */ 529 } 530 531 /* 532 * Switch if required. Note that we ignore the result of the switch 533 * effort; we don't know what to do if it fails, so checking wouldn't 534 * help much. 535 */ 536 if (cur != ACPI_PWR_ON) { 537 if (ACPI_FAILURE(status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL))) { 538 DEBUG_PRINT(TRACE_OBJECTS, ("failed to switch %s on - %s\n", 539 acpi_name(rp->ap_resource), acpi_strerror(status))); 540 } else { 541 DEBUG_PRINT(TRACE_OBJECTS, ("switched %s on\n", acpi_name(rp->ap_resource))); 542 } 543 } else { 544 DEBUG_PRINT(TRACE_OBJECTS, ("%s is already on\n", acpi_name(rp->ap_resource))); 545 } 546 } 547 548 /* 549 * Sweep the list backwards turning things off. 550 */ 551 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, ap_link) { 552 if (TAILQ_FIRST(&rp->ap_references) != NULL) { 553 DEBUG_PRINT(TRACE_OBJECTS, ("%s has references, not turning off\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 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) { 560 DEBUG_PRINT(TRACE_OBJECTS, ("can't get status of %s - %d\n", 561 acpi_name(rp->ap_resource), status)); 562 continue; /* XXX is this correct? Always switch if in doubt? */ 563 } 564 565 /* 566 * Switch if required. Note that we ignore the result of the switch 567 * effort; we don't know what to do if it fails, so checking wouldn't 568 * help much. 569 */ 570 if (cur != ACPI_PWR_OFF) { 571 if (ACPI_FAILURE(status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL))) { 572 DEBUG_PRINT(TRACE_OBJECTS, ("failed to switch %s off - %s\n", 573 acpi_name(rp->ap_resource), acpi_strerror(status))); 574 } else { 575 DEBUG_PRINT(TRACE_OBJECTS, ("switched %s off\n", acpi_name(rp->ap_resource))); 576 } 577 } else { 578 DEBUG_PRINT(TRACE_OBJECTS, ("%s is already off\n", acpi_name(rp->ap_resource))); 579 } 580 } 581 return_ACPI_STATUS(AE_OK); 582} 583 584/* 585 * Find a power resource's control structure. 586 */ 587static struct acpi_powerresource * 588acpi_pwr_find_resource(ACPI_HANDLE res) 589{ 590 struct acpi_powerresource *rp; 591 592 FUNCTION_TRACE(__func__); 593 594 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) 595 if (rp->ap_resource == res) 596 break; 597 return_VALUE(rp); 598} 599 600/* 601 * Find a power consumer's control structure. 602 */ 603static struct acpi_powerconsumer * 604acpi_pwr_find_consumer(ACPI_HANDLE consumer) 605{ 606 struct acpi_powerconsumer *pc; 607 608 FUNCTION_TRACE(__func__); 609 610 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) 611 if (pc->ac_consumer == consumer) 612 break; 613 return_VALUE(pc); 614} 615
| 181 182 /* sort the resource into the list */ 183 status = AE_OK; 184 srp = TAILQ_FIRST(&acpi_powerresources); 185 if ((srp == NULL) || (rp->ap_order < srp->ap_order)) { 186 TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link); 187 goto done; 188 } 189 TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) 190 if (rp->ap_order < srp->ap_order) { 191 TAILQ_INSERT_BEFORE(srp, rp, ap_link); 192 goto done; 193 } 194 TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link); 195 196 done: 197 DEBUG_PRINT(TRACE_OBJECTS, ("registered power resource %s\n", acpi_name(res))); 198 out: 199 if (obj != NULL) 200 AcpiOsFree(obj); 201 if ((status != AE_OK) && (rp != NULL)) 202 free(rp, M_ACPIPWR); 203 return_ACPI_STATUS(status); 204} 205 206/* 207 * Deregister a power resource. 208 */ 209static ACPI_STATUS 210acpi_pwr_deregister_resource(ACPI_HANDLE res) 211{ 212 struct acpi_powerresource *rp; 213 214 FUNCTION_TRACE(__func__); 215 216 rp = NULL; 217 218 /* find the resource */ 219 if ((rp = acpi_pwr_find_resource(res)) == NULL) 220 return_ACPI_STATUS(AE_BAD_PARAMETER); 221 222 /* check that there are no consumers referencing this resource */ 223 if (TAILQ_FIRST(&rp->ap_references) != NULL) 224 return_ACPI_STATUS(AE_BAD_PARAMETER); 225 226 /* pull it off the list and free it */ 227 TAILQ_REMOVE(&acpi_powerresources, rp, ap_link); 228 free(rp, M_ACPIPWR); 229 230 DEBUG_PRINT(TRACE_OBJECTS, ("deregistered power resource %s\n", acpi_name(res))); 231 232 return_ACPI_STATUS(AE_OK); 233} 234 235/* 236 * Register a power consumer. 237 * 238 * It's OK to call this if we already know about the consumer. 239 */ 240static ACPI_STATUS 241acpi_pwr_register_consumer(ACPI_HANDLE consumer) 242{ 243 struct acpi_powerconsumer *pc; 244 245 FUNCTION_TRACE(__func__); 246 247 /* check to see whether we know about this consumer already */ 248 if ((pc = acpi_pwr_find_consumer(consumer)) != NULL) 249 return_ACPI_STATUS(AE_OK); 250 251 /* allocate a new power consumer */ 252 if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) 253 return_ACPI_STATUS(AE_NO_MEMORY); 254 TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); 255 TAILQ_INIT(&pc->ac_references); 256 pc->ac_consumer = consumer; 257 258 pc->ac_state = ACPI_STATE_UNKNOWN; /* XXX we should try to find its current state */ 259 260 DEBUG_PRINT(TRACE_OBJECTS, ("registered power consumer %s\n", acpi_name(consumer))); 261 262 return_ACPI_STATUS(AE_OK); 263} 264 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 */ 271static ACPI_STATUS 272acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) 273{ 274 struct acpi_powerconsumer *pc; 275 276 FUNCTION_TRACE(__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 DEBUG_PRINT(TRACE_OBJECTS, ("deregistered power consumer %s\n", acpi_name(consumer))); 290 291 return_ACPI_STATUS(AE_OK); 292} 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; 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 FUNCTION_TRACE(__func__); 310 311 /* find the consumer */ 312 if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) { 313 if ((status = acpi_pwr_register_consumer(consumer)) != AE_OK) 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 DEBUG_PRINT(TRACE_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 if (AcpiGetHandle(consumer, method_name, &method_handle) != AE_OK) 359 method_handle = NULL; 360 if (AcpiGetHandle(consumer, reslist_name, &reslist_handle) != AE_OK) 361 reslist_handle = NULL; 362 if ((reslist_handle == NULL) && (method_handle == NULL)) { 363 if (state == ACPI_STATE_D0) { 364 pc->ac_state = ACPI_STATE_D0; 365 return_ACPI_STATUS(AE_OK); 366 } 367 DEBUG_PRINT(TRACE_OBJECTS, ("attempt to set unsupported state D%d\n", 368 state)); 369 return_ACPI_STATUS(AE_BAD_PARAMETER); 370 } 371 372 /* 373 * Check that we can actually fetch the list of power resources 374 */ 375 if (reslist_handle != NULL) { 376 if ((status = acpi_EvaluateIntoBuffer(reslist_handle, NULL, NULL, &reslist_buffer)) != AE_OK) { 377 DEBUG_PRINT(TRACE_OBJECTS, ("can't evaluate resource list %s\n", 378 acpi_name(reslist_handle))); 379 return_ACPI_STATUS(status); 380 } 381 reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer; 382 if (reslist_object->Type != ACPI_TYPE_PACKAGE) { 383 DEBUG_PRINT(TRACE_OBJECTS, ("resource list is not ACPI_TYPE_PACKAGE (%d)\n", 384 reslist_object->Type)); 385 return_ACPI_STATUS(AE_TYPE); 386 } 387 } else { 388 reslist_object = NULL; 389 } 390 391 /* 392 * Now we are ready to switch, so kill off any current power resource references. 393 */ 394 res_changed = 0; 395 while((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) { 396 res_changed = 1; 397 DEBUG_PRINT(TRACE_OBJECTS, ("removing reference to %s\n", acpi_name(pr->ar_resource->ap_resource))); 398 TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink); 399 TAILQ_REMOVE(&pc->ac_references, pr, ar_clink); 400 free(pr, M_ACPIPWR); 401 } 402 403 /* 404 * Add new power resource references, if we have any. Traverse the 405 * package that we got from evaluating reslist_handle, and look up each 406 * of the resources that are referenced. 407 */ 408 if (reslist_object != NULL) { 409 DEBUG_PRINT(TRACE_OBJECTS, ("referencing %d new resources\n", 410 reslist_object->Package.Count)); 411 acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource, pc); 412 res_changed = 1; 413 } 414 415 /* 416 * If we changed anything in the resource list, we need to run a switch 417 * pass now. 418 */ 419 if ((status = acpi_pwr_switch_power()) != AE_OK) { 420 DEBUG_PRINT(TRACE_OBJECTS, ("failed to correctly switch resources to move %s to D%d\n", 421 acpi_name(consumer), state)); 422 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */ 423 } 424 425 /* invoke power state switch method (if present) */ 426 if (method_handle != NULL) { 427 DEBUG_PRINT(TRACE_OBJECTS, ("invoking state transition method %s\n", 428 acpi_name(method_handle))); 429 if ((status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL)) != AE_OK) 430 pc->ac_state = ACPI_STATE_UNKNOWN; 431 return_ACPI_STATUS(status); /* XXX is this appropriate? Should we return to previous state? */ 432 } 433 434 /* transition was successful */ 435 pc->ac_state = state; 436 return_ACPI_STATUS(AE_OK); 437} 438 439/* 440 * Called to create a reference between a power consumer and a power resource 441 * identified in the object. 442 */ 443static void 444acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg) 445{ 446 struct acpi_powerconsumer *pc = (struct acpi_powerconsumer *)arg; 447 struct acpi_powerreference *pr; 448 struct acpi_powerresource *rp; 449 ACPI_HANDLE res; 450 ACPI_STATUS status; 451 452 FUNCTION_TRACE(__func__); 453 454 /* check the object type */ 455 if (obj->Type != ACPI_TYPE_STRING) { 456 DEBUG_PRINT(TRACE_OBJECTS, ("don't know how to create a power reference to object type %d\n", 457 obj->Type)); 458 return_VOID; 459 } 460 461 DEBUG_PRINT(TRACE_OBJECTS, ("building reference from %s to %s\n", 462 acpi_name(pc->ac_consumer), obj->String.Pointer)); 463 464 /* get the handle of the resource */ 465 if (ACPI_FAILURE(status = AcpiGetHandle(NULL, obj->String.Pointer, &res))) { 466 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't find power resource %s\n", 467 obj->String.Pointer)); 468 return_VOID; 469 } 470 471 /* create/look up the resource */ 472 if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) { 473 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't register power resource %s - %s\n", 474 obj->String.Pointer, acpi_strerror(status))); 475 return_VOID; 476 } 477 if ((rp = acpi_pwr_find_resource(res)) == NULL) { 478 DEBUG_PRINT(TRACE_OBJECTS, ("power resource list corrupted\n")); 479 return_VOID; 480 } 481 DEBUG_PRINT(TRACE_OBJECTS, ("found power resource %s\n", acpi_name(rp->ap_resource))); 482 483 /* create a reference between the consumer and resource */ 484 if ((pr = malloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) { 485 DEBUG_PRINT(TRACE_OBJECTS, ("couldn't allocate memory for a power consumer reference\n")); 486 return_VOID; 487 } 488 pr->ar_consumer = pc; 489 pr->ar_resource = rp; 490 TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink); 491 TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink); 492 493 return_VOID; 494} 495 496 497/* 498 * Switch power resources to conform to the desired state. 499 * 500 * Consumers may have modified the power resource list in an arbitrary 501 * fashion; we sweep it in sequence order. 502 */ 503static ACPI_STATUS 504acpi_pwr_switch_power(void) 505{ 506 struct acpi_powerresource *rp; 507 ACPI_STATUS status; 508 int cur; 509 510 FUNCTION_TRACE(__func__); 511 512 /* 513 * Sweep the list forwards turning things on. 514 */ 515 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) { 516 if (TAILQ_FIRST(&rp->ap_references) == NULL) { 517 DEBUG_PRINT(TRACE_OBJECTS, ("%s has no references, not turning on\n", 518 acpi_name(rp->ap_resource))); 519 continue; 520 } 521 522 /* we could cache this if we trusted it not to change under us */ 523 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) { 524 DEBUG_PRINT(TRACE_OBJECTS, ("can't get status of %s - %d\n", 525 acpi_name(rp->ap_resource), status)); 526 continue; /* XXX is this correct? Always switch if in doubt? */ 527 } 528 529 /* 530 * Switch if required. Note that we ignore the result of the switch 531 * effort; we don't know what to do if it fails, so checking wouldn't 532 * help much. 533 */ 534 if (cur != ACPI_PWR_ON) { 535 if (ACPI_FAILURE(status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL))) { 536 DEBUG_PRINT(TRACE_OBJECTS, ("failed to switch %s on - %s\n", 537 acpi_name(rp->ap_resource), acpi_strerror(status))); 538 } else { 539 DEBUG_PRINT(TRACE_OBJECTS, ("switched %s on\n", acpi_name(rp->ap_resource))); 540 } 541 } else { 542 DEBUG_PRINT(TRACE_OBJECTS, ("%s is already on\n", acpi_name(rp->ap_resource))); 543 } 544 } 545 546 /* 547 * Sweep the list backwards turning things off. 548 */ 549 TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list, ap_link) { 550 if (TAILQ_FIRST(&rp->ap_references) != NULL) { 551 DEBUG_PRINT(TRACE_OBJECTS, ("%s has references, not turning off\n", 552 acpi_name(rp->ap_resource))); 553 continue; 554 } 555 556 /* we could cache this if we trusted it not to change under us */ 557 if ((status = acpi_EvaluateInteger(rp->ap_resource, "_STA", &cur)) != AE_OK) { 558 DEBUG_PRINT(TRACE_OBJECTS, ("can't get status of %s - %d\n", 559 acpi_name(rp->ap_resource), status)); 560 continue; /* XXX is this correct? Always switch if in doubt? */ 561 } 562 563 /* 564 * Switch if required. Note that we ignore the result of the switch 565 * effort; we don't know what to do if it fails, so checking wouldn't 566 * help much. 567 */ 568 if (cur != ACPI_PWR_OFF) { 569 if (ACPI_FAILURE(status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL))) { 570 DEBUG_PRINT(TRACE_OBJECTS, ("failed to switch %s off - %s\n", 571 acpi_name(rp->ap_resource), acpi_strerror(status))); 572 } else { 573 DEBUG_PRINT(TRACE_OBJECTS, ("switched %s off\n", acpi_name(rp->ap_resource))); 574 } 575 } else { 576 DEBUG_PRINT(TRACE_OBJECTS, ("%s is already off\n", acpi_name(rp->ap_resource))); 577 } 578 } 579 return_ACPI_STATUS(AE_OK); 580} 581 582/* 583 * Find a power resource's control structure. 584 */ 585static struct acpi_powerresource * 586acpi_pwr_find_resource(ACPI_HANDLE res) 587{ 588 struct acpi_powerresource *rp; 589 590 FUNCTION_TRACE(__func__); 591 592 TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) 593 if (rp->ap_resource == res) 594 break; 595 return_VALUE(rp); 596} 597 598/* 599 * Find a power consumer's control structure. 600 */ 601static struct acpi_powerconsumer * 602acpi_pwr_find_consumer(ACPI_HANDLE consumer) 603{ 604 struct acpi_powerconsumer *pc; 605 606 FUNCTION_TRACE(__func__); 607 608 TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) 609 if (pc->ac_consumer == consumer) 610 break; 611 return_VALUE(pc); 612} 613
|