/*-
* Copyright (c) 2009, 2010, 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jukka Ruohonen.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 2001 Michael Smith
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Power resources should be ordered.
*
* These *should* be enabled from low values to high
* values and disabled from high values to low values.
*/
TAILQ_FOREACH(tmp, &res_head, res_list) {
/*
* Remove all references in each resource.
*/
TAILQ_FOREACH(res, &res_head, res_list)
(void)acpi_power_res_deref(res, ad->ad_handle);
}
/*
* Get the D-state of an ACPI device node.
*/
bool
acpi_power_get(ACPI_HANDLE hdl, int *state)
{
struct acpi_devnode *ad = acpi_match_node(hdl);
ACPI_STATUS rv;
if (ad == NULL)
return false;
/*
* As _PSC may be broken, first try to
* retrieve the power state indirectly
* via power resources.
*/
rv = acpi_power_get_indirect(ad);
if (ACPI_FAILURE(rv))
rv = acpi_power_get_direct(ad);
/*
* The device is in a given D-state if all resources are on.
* To derive this, evaluate all elements in each _PRx package
* (x = 0 ... 3) and break if the noted condition becomes true.
*/
for (ad->ad_state = ACPI_STATE_D3, i = 0; i < ACPI_STATE_D3; i++) {
pkg = acpi_power_pkg_get(ad->ad_handle, i);
if (pkg == NULL)
continue;
/*
* For each element in the _PRx package, evaluate _STA
* and return AE_OK only if all power resources are on.
*/
rv = acpi_foreach_package_object(pkg, acpi_power_res_sta, ad);
if (ACPI_FAILURE(rv) && rv != AE_CTRL_FALSE)
goto out;
if (ACPI_SUCCESS(rv)) {
ad->ad_state = i;
goto out;
}
ACPI_FREE(pkg); pkg = NULL;
}
KASSERT(ad->ad_state == ACPI_STATE_D3);
return AE_OK;
out:
ACPI_FREE(pkg);
return rv;
}
/*
* Set the D-state of an ACPI device node.
*/
bool
acpi_power_set(ACPI_HANDLE hdl, int state)
{
struct acpi_devnode *ad = acpi_match_node(hdl);
ACPI_STATUS rv;
char path[5];
int old;
if (ad == NULL)
return false;
if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) {
rv = AE_BAD_PARAMETER;
goto fail;
}
/*
* It is only possible to go to D0 ("on") from D3 ("off").
*/
if (ad->ad_state == ACPI_STATE_D3 && state != ACPI_STATE_D0) {
rv = AE_BAD_PARAMETER;
goto fail;
}
/*
* As noted in ACPI 4.0 (appendix A.2.1), the bus power state
* should never be lower than the highest state of one of its
* devices. Consequently, we cannot set the state to a lower
* (i.e. higher power) state than the parent device's state.
*/
if ((ad->ad_parent != NULL) &&
(ad->ad_parent->ad_flags & ACPI_DEVICE_POWER) != 0) {
/*
* We first sweep through the resources required for the target
* state, turning things on and building references. After this
* we dereference the resources required for the current state,
* turning the resources off as we go.
*/
rv = acpi_power_switch(ad, state, true);
if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
goto fail;
rv = acpi_power_switch(ad, ad->ad_state, false);
if (ACPI_FAILURE(rv) && rv != AE_CTRL_CONTINUE)
goto fail;
/*
* Last but not least, invoke the power state switch method,
* if available. Because some systems use only _PSx for the
* power state transitions, we do this even if there is no _PRx.
*/
(void)snprintf(path, sizeof(path), "_PS%d", state);
(void)AcpiEvaluateObject(ad->ad_handle, path, NULL, NULL);
ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s turned from "
"D%d to D%d\n", ad->ad_name, old, state));
ad->ad_state = state;
return true;
fail:
ad->ad_state = ACPI_STATE_ERROR;
ACPI_DEBUG_PRINT((ACPI_DB_INFO, "failed to set power state to D%d "
"for %s: %s\n", state, ad->ad_name, AcpiFormatException(rv)));
/*
* For each element in the _PRx package, fetch
* the reference handle, search for this handle
* from the power resource queue, and turn the
* resource behind the handle on or off.
*/
pkg = acpi_power_pkg_get(ad->ad_handle, state);
if (pkg == NULL)
return AE_CTRL_CONTINUE;
n = pkg->Package.Count;
for (i = 0; i < n; i++) {
elm = &pkg->Package.Elements[i];
rv = acpi_eval_reference_handle(elm, &hdl);
/*
* Search for the resource.
*/
res = acpi_power_res_get(hdl);
if (res == NULL)
return AE_NOT_FOUND;
if (ref == NULL)
return AE_BAD_PARAMETER;
/*
* Adjust the reference counting. This is
* necessary since a single power resource
* can be shared by multiple devices.
*/
if (on) {
rv = acpi_power_res_ref(res, ref);
str = "_ON";
} else {
rv = acpi_power_res_deref(res, ref);
str = "_OFF";
}
if (ACPI_FAILURE(rv))
return rv;
/*
* Turn the resource on or off.
*/
return AcpiEvaluateObject(res->res_handle, str, NULL, NULL);
}
/*
* If the array remains to be non-empty,
* something else is using the resource
* and hence it can not be turned off.
*/
mutex_exit(&res->res_mutex);