Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $) |
| 3 | * |
| 4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * ACPI power-managed devices may be controlled in two ways: |
| 28 | * 1. via "Device Specific (D-State) Control" |
| 29 | * 2. via "Power Resource Control". |
| 30 | * This module is used to manage devices relying on Power Resource Control. |
| 31 | * |
| 32 | * An ACPI "power resource object" describes a software controllable power |
| 33 | * plane, clock plane, or other resource used by a power managed device. |
| 34 | * A device may rely on multiple power resources, and a power resource |
| 35 | * may be shared by multiple devices. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 43 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <acpi/acpi_bus.h> |
| 45 | #include <acpi/acpi_drivers.h> |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 46 | #include "sleep.h" |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 47 | #include "internal.h" |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 48 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 49 | #define PREFIX "ACPI: " |
| 50 | |
Bjorn Helgaas | 89595b8 | 2008-11-07 16:57:45 -0700 | [diff] [blame] | 51 | #define _COMPONENT ACPI_POWER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 52 | ACPI_MODULE_NAME("power"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define ACPI_POWER_CLASS "power_resource" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_POWER_DEVICE_NAME "Power Resource" |
| 55 | #define ACPI_POWER_FILE_INFO "info" |
| 56 | #define ACPI_POWER_FILE_STATUS "state" |
| 57 | #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 |
| 58 | #define ACPI_POWER_RESOURCE_STATE_ON 0x01 |
| 59 | #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 60 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 61 | static int acpi_power_add(struct acpi_device *device); |
| 62 | static int acpi_power_remove(struct acpi_device *device, int type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Márton Németh | c97adf9 | 2010-01-10 17:15:36 +0100 | [diff] [blame] | 64 | static const struct acpi_device_id power_device_ids[] = { |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 65 | {ACPI_POWER_HID, 0}, |
| 66 | {"", 0}, |
| 67 | }; |
| 68 | MODULE_DEVICE_TABLE(acpi, power_device_ids); |
| 69 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 71 | static int acpi_power_resume(struct device *dev); |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 72 | #endif |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 73 | static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume); |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static struct acpi_driver acpi_power_driver = { |
Len Brown | c2b6705 | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 76 | .name = "power", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 77 | .class = ACPI_POWER_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 78 | .ids = power_device_ids, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 79 | .ops = { |
| 80 | .add = acpi_power_add, |
| 81 | .remove = acpi_power_remove, |
| 82 | }, |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 83 | .drv.pm = &acpi_power_pm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 86 | struct acpi_power_dependent_device { |
| 87 | struct list_head node; |
| 88 | struct acpi_device *adev; |
| 89 | struct work_struct work; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 92 | struct acpi_power_resource { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 93 | struct acpi_device *device; |
| 94 | struct list_head dependent; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | acpi_bus_id name; |
| 96 | u32 system_level; |
| 97 | u32 order; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 98 | unsigned int ref_count; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 99 | struct mutex resource_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 102 | static struct list_head acpi_power_resource_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* -------------------------------------------------------------------------- |
| 105 | Power Resource Management |
| 106 | -------------------------------------------------------------------------- */ |
| 107 | |
| 108 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 109 | acpi_power_get_context(acpi_handle handle, |
| 110 | struct acpi_power_resource **resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 112 | int result = 0; |
| 113 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | if (!resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 117 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | result = acpi_bus_get_device(handle, &device); |
| 120 | if (result) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 121 | printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 122 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 125 | *resource = acpi_driver_data(device); |
Li Zefan | a815ab8 | 2008-04-18 13:27:29 -0700 | [diff] [blame] | 126 | if (!*resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 127 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 132 | static int acpi_power_get_state(acpi_handle handle, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 134 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 135 | unsigned long long sta = 0; |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 136 | char node_name[5]; |
| 137 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 140 | if (!handle || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 141 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 143 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 145 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 147 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
| 148 | ACPI_POWER_RESOURCE_STATE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 150 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 153 | node_name, |
Zhao Yakui | b1b57fb | 2008-10-27 16:04:53 +0800 | [diff] [blame] | 154 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 156 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 159 | static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 161 | int cur_state; |
| 162 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | if (!list || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 165 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | /* The state of the list is 'on' IFF all resources are 'on'. */ |
| 168 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 169 | for (i = 0; i < list->count; i++) { |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 170 | struct acpi_power_resource *resource; |
| 171 | acpi_handle handle = list->handles[i]; |
| 172 | int result; |
| 173 | |
| 174 | result = acpi_power_get_context(handle, &resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 176 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 178 | mutex_lock(&resource->resource_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 180 | result = acpi_power_get_state(handle, &cur_state); |
| 181 | |
| 182 | mutex_unlock(&resource->resource_lock); |
| 183 | |
| 184 | if (result) |
| 185 | return result; |
| 186 | |
| 187 | if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | |
| 191 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 192 | cur_state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 194 | *state = cur_state; |
| 195 | |
| 196 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 199 | static void acpi_power_resume_dependent(struct work_struct *work) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 200 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 201 | struct acpi_power_dependent_device *dep; |
| 202 | struct acpi_device_physical_node *pn; |
| 203 | struct acpi_device *adev; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 204 | int state; |
| 205 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 206 | dep = container_of(work, struct acpi_power_dependent_device, work); |
| 207 | adev = dep->adev; |
| 208 | if (acpi_power_get_inferred_state(adev, &state)) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 209 | return; |
| 210 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 211 | if (state > ACPI_STATE_D0) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 212 | return; |
| 213 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 214 | mutex_lock(&adev->physical_node_lock); |
| 215 | |
| 216 | list_for_each_entry(pn, &adev->physical_node_list, node) |
| 217 | pm_request_resume(pn->dev); |
| 218 | |
| 219 | list_for_each_entry(pn, &adev->power_dependent, node) |
| 220 | pm_request_resume(pn->dev); |
| 221 | |
| 222 | mutex_unlock(&adev->physical_node_lock); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 225 | static int __acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 227 | acpi_status status = AE_OK; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 228 | |
Patrick Mochel | 5fbc19e | 2006-05-19 16:54:43 -0400 | [diff] [blame] | 229 | status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 231 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* Update the power resource's _device_ power state */ |
Patrick Mochel | 4159857 | 2006-05-19 16:54:40 -0400 | [diff] [blame] | 234 | resource->device->power.state = ACPI_STATE_D0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 236 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 237 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 238 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 239 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 242 | static int acpi_power_on(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Bjorn Helgaas | bdf43bb | 2009-05-21 17:28:53 -0600 | [diff] [blame] | 244 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | struct acpi_power_resource *resource = NULL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | result = acpi_power_get_context(handle, &resource); |
| 248 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 249 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 251 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 252 | |
| 253 | if (resource->ref_count++) { |
| 254 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 255 | "Power resource [%s] already on", |
| 256 | resource->name)); |
| 257 | } else { |
| 258 | result = __acpi_power_on(resource); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 259 | if (result) { |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 260 | resource->ref_count--; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 261 | } else { |
| 262 | struct acpi_power_dependent_device *dep; |
| 263 | |
| 264 | list_for_each_entry(dep, &resource->dependent, node) |
| 265 | schedule_work(&dep->work); |
| 266 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 269 | mutex_unlock(&resource->resource_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 271 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Rafael J. Wysocki | 36237fa | 2011-01-06 23:38:04 +0100 | [diff] [blame] | 274 | static int acpi_power_off(acpi_handle handle) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 275 | { |
| 276 | int result = 0; |
| 277 | acpi_status status = AE_OK; |
| 278 | struct acpi_power_resource *resource = NULL; |
| 279 | |
| 280 | result = acpi_power_get_context(handle, &resource); |
| 281 | if (result) |
| 282 | return result; |
| 283 | |
| 284 | mutex_lock(&resource->resource_lock); |
| 285 | |
| 286 | if (!resource->ref_count) { |
| 287 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 288 | "Power resource [%s] already off", |
| 289 | resource->name)); |
| 290 | goto unlock; |
| 291 | } |
| 292 | |
| 293 | if (--resource->ref_count) { |
| 294 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 295 | "Power resource [%s] still in use\n", |
| 296 | resource->name)); |
| 297 | goto unlock; |
| 298 | } |
| 299 | |
| 300 | status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL); |
| 301 | if (ACPI_FAILURE(status)) { |
| 302 | result = -ENODEV; |
| 303 | } else { |
| 304 | /* Update the power resource's _device_ power state */ |
| 305 | resource->device->power.state = ACPI_STATE_D3; |
| 306 | |
| 307 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 308 | "Power resource [%s] turned off\n", |
| 309 | resource->name)); |
| 310 | } |
| 311 | |
| 312 | unlock: |
| 313 | mutex_unlock(&resource->resource_lock); |
| 314 | |
| 315 | return result; |
| 316 | } |
| 317 | |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 318 | static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res) |
| 319 | { |
| 320 | int i; |
| 321 | |
| 322 | for (i = num_res - 1; i >= 0 ; i--) |
Rafael J. Wysocki | 36237fa | 2011-01-06 23:38:04 +0100 | [diff] [blame] | 323 | acpi_power_off(list->handles[i]); |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void acpi_power_off_list(struct acpi_handle_list *list) |
| 327 | { |
| 328 | __acpi_power_off_list(list, list->count); |
| 329 | } |
| 330 | |
| 331 | static int acpi_power_on_list(struct acpi_handle_list *list) |
| 332 | { |
| 333 | int result = 0; |
| 334 | int i; |
| 335 | |
| 336 | for (i = 0; i < list->count; i++) { |
| 337 | result = acpi_power_on(list->handles[i]); |
| 338 | if (result) { |
| 339 | __acpi_power_off_list(list, i); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return result; |
| 345 | } |
| 346 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 347 | static void acpi_power_add_dependent(acpi_handle rhandle, |
| 348 | struct acpi_device *adev) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 349 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 350 | struct acpi_power_dependent_device *dep; |
| 351 | struct acpi_power_resource *resource; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 352 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 353 | if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 354 | return; |
| 355 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 356 | mutex_lock(&resource->resource_lock); |
| 357 | |
| 358 | list_for_each_entry(dep, &resource->dependent, node) |
| 359 | if (dep->adev == adev) |
| 360 | goto out; |
| 361 | |
| 362 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 363 | if (!dep) |
| 364 | goto out; |
| 365 | |
| 366 | dep->adev = adev; |
| 367 | INIT_WORK(&dep->work, acpi_power_resume_dependent); |
| 368 | list_add_tail(&dep->node, &resource->dependent); |
| 369 | |
| 370 | out: |
| 371 | mutex_unlock(&resource->resource_lock); |
| 372 | } |
| 373 | |
| 374 | static void acpi_power_remove_dependent(acpi_handle rhandle, |
| 375 | struct acpi_device *adev) |
| 376 | { |
| 377 | struct acpi_power_dependent_device *dep; |
| 378 | struct acpi_power_resource *resource; |
| 379 | struct work_struct *work = NULL; |
| 380 | |
| 381 | if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource)) |
| 382 | return; |
| 383 | |
| 384 | mutex_lock(&resource->resource_lock); |
| 385 | |
| 386 | list_for_each_entry(dep, &resource->dependent, node) |
| 387 | if (dep->adev == adev) { |
| 388 | list_del(&dep->node); |
| 389 | work = &dep->work; |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | mutex_unlock(&resource->resource_lock); |
| 394 | |
| 395 | if (work) { |
| 396 | cancel_work_sync(work); |
| 397 | kfree(dep); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void acpi_power_add_remove_device(struct acpi_device *adev, bool add) |
| 402 | { |
| 403 | if (adev->power.flags.power_resources) { |
| 404 | struct acpi_device_power_state *ps; |
| 405 | int j; |
| 406 | |
| 407 | ps = &adev->power.states[ACPI_STATE_D0]; |
| 408 | for (j = 0; j < ps->resources.count; j++) { |
| 409 | acpi_handle rhandle = ps->resources.handles[j]; |
| 410 | |
| 411 | if (add) |
| 412 | acpi_power_add_dependent(rhandle, adev); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 413 | else |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 414 | acpi_power_remove_dependent(rhandle, adev); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 415 | } |
| 416 | } |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 417 | } |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 418 | |
| 419 | /* -------------------------------------------------------------------------- |
| 420 | Device Power Management |
| 421 | -------------------------------------------------------------------------- */ |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 422 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 423 | /** |
| 424 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in |
| 425 | * ACPI 3.0) _PSW (Power State Wake) |
| 426 | * @dev: Device to handle. |
| 427 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. |
| 428 | * @sleep_state: Target sleep state of the system. |
| 429 | * @dev_state: Target power state of the device. |
| 430 | * |
| 431 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 432 | * State Wake) for the device, if present. On failure reset the device's |
| 433 | * wakeup.flags.valid flag. |
| 434 | * |
| 435 | * RETURN VALUE: |
| 436 | * 0 if either _DSW or _PSW has been successfully executed |
| 437 | * 0 if neither _DSW nor _PSW has been found |
| 438 | * -ENODEV if the execution of either _DSW or _PSW has failed |
| 439 | */ |
| 440 | int acpi_device_sleep_wake(struct acpi_device *dev, |
| 441 | int enable, int sleep_state, int dev_state) |
| 442 | { |
| 443 | union acpi_object in_arg[3]; |
| 444 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 445 | acpi_status status = AE_OK; |
| 446 | |
| 447 | /* |
| 448 | * Try to execute _DSW first. |
| 449 | * |
| 450 | * Three agruments are needed for the _DSW object: |
| 451 | * Argument 0: enable/disable the wake capabilities |
| 452 | * Argument 1: target system state |
| 453 | * Argument 2: target device state |
| 454 | * When _DSW object is called to disable the wake capabilities, maybe |
| 455 | * the first argument is filled. The values of the other two agruments |
| 456 | * are meaningless. |
| 457 | */ |
| 458 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 459 | in_arg[0].integer.value = enable; |
| 460 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 461 | in_arg[1].integer.value = sleep_state; |
| 462 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 463 | in_arg[2].integer.value = dev_state; |
| 464 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); |
| 465 | if (ACPI_SUCCESS(status)) { |
| 466 | return 0; |
| 467 | } else if (status != AE_NOT_FOUND) { |
| 468 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); |
| 469 | dev->wakeup.flags.valid = 0; |
| 470 | return -ENODEV; |
| 471 | } |
| 472 | |
| 473 | /* Execute _PSW */ |
| 474 | arg_list.count = 1; |
| 475 | in_arg[0].integer.value = enable; |
| 476 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); |
| 477 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { |
| 478 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); |
| 479 | dev->wakeup.flags.valid = 0; |
| 480 | return -ENODEV; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | /* |
| 487 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): |
| 488 | * 1. Power on the power resources required for the wakeup device |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 489 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 490 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | */ |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 492 | int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 494 | int i, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 497 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 499 | mutex_lock(&acpi_device_lock); |
| 500 | |
| 501 | if (dev->wakeup.prepare_count++) |
| 502 | goto out; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | /* Open power resource */ |
| 505 | for (i = 0; i < dev->wakeup.resources.count; i++) { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 506 | int ret = acpi_power_on(dev->wakeup.resources.handles[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if (ret) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 508 | printk(KERN_ERR PREFIX "Transition power state\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | dev->wakeup.flags.valid = 0; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 510 | err = -ENODEV; |
| 511 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 515 | /* |
| 516 | * Passing 3 as the third argument below means the device may be placed |
| 517 | * in arbitrary power state afterwards. |
| 518 | */ |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 519 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 520 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 521 | err_out: |
| 522 | if (err) |
| 523 | dev->wakeup.prepare_count = 0; |
| 524 | |
| 525 | out: |
| 526 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 527 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Shutdown a wakeup device, counterpart of above method |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 532 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 533 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | * 2. Shutdown down the power resources |
| 535 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 536 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 538 | int i, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 541 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 543 | mutex_lock(&acpi_device_lock); |
| 544 | |
| 545 | if (--dev->wakeup.prepare_count > 0) |
| 546 | goto out; |
| 547 | |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 548 | /* |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 549 | * Executing the code below even if prepare_count is already zero when |
| 550 | * the function is called may be useful, for example for initialisation. |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 551 | */ |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 552 | if (dev->wakeup.prepare_count < 0) |
| 553 | dev->wakeup.prepare_count = 0; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 554 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 555 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
| 556 | if (err) |
| 557 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | /* Close power resource */ |
| 560 | for (i = 0; i < dev->wakeup.resources.count; i++) { |
Rafael J. Wysocki | 36237fa | 2011-01-06 23:38:04 +0100 | [diff] [blame] | 561 | int ret = acpi_power_off(dev->wakeup.resources.handles[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | if (ret) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 563 | printk(KERN_ERR PREFIX "Transition power state\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | dev->wakeup.flags.valid = 0; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 565 | err = -ENODEV; |
| 566 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 570 | out: |
| 571 | mutex_unlock(&acpi_device_lock); |
| 572 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 575 | int acpi_power_get_inferred_state(struct acpi_device *device, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 577 | int result = 0; |
| 578 | struct acpi_handle_list *list = NULL; |
| 579 | int list_state = 0; |
| 580 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 582 | if (!device || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 583 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* |
| 586 | * We know a device's inferred power state when all the resources |
| 587 | * required for a given D-state are 'on'. |
| 588 | */ |
Rafael J. Wysocki | 38c92ff | 2012-05-20 13:58:00 +0200 | [diff] [blame] | 589 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | list = &device->power.states[i].resources; |
| 591 | if (list->count < 1) |
| 592 | continue; |
| 593 | |
| 594 | result = acpi_power_get_list_state(list, &list_state); |
| 595 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 596 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 599 | *state = i; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 600 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 604 | *state = ACPI_STATE_D3; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 605 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Rafael J. Wysocki | 30d3df4 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 608 | int acpi_power_on_resources(struct acpi_device *device, int state) |
| 609 | { |
| 610 | if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3) |
| 611 | return -EINVAL; |
| 612 | |
| 613 | return acpi_power_on_list(&device->power.states[state].resources); |
| 614 | } |
| 615 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 616 | int acpi_power_transition(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 618 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 620 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 621 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Rafael J. Wysocki | 212967c | 2010-11-25 00:02:36 +0100 | [diff] [blame] | 623 | if (device->power.state == state) |
| 624 | return 0; |
| 625 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 626 | if ((device->power.state < ACPI_STATE_D0) |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 627 | || (device->power.state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 628 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | /* TBD: Resources must be ordered. */ |
| 631 | |
| 632 | /* |
| 633 | * First we reference all power resources required in the target list |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 634 | * (e.g. so the device doesn't lose power while transitioning). Then, |
| 635 | * we dereference all power resources used in the current list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | */ |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 637 | if (state < ACPI_STATE_D3_COLD) |
| 638 | result = acpi_power_on_list( |
| 639 | &device->power.states[state].resources); |
| 640 | |
| 641 | if (!result && device->power.state < ACPI_STATE_D3_COLD) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 642 | acpi_power_off_list( |
| 643 | &device->power.states[device->power.state].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 645 | /* We shouldn't change the state unless the above operations succeed. */ |
| 646 | device->power.state = result ? ACPI_STATE_UNKNOWN : state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 648 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* -------------------------------------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | Driver Interface |
| 653 | -------------------------------------------------------------------------- */ |
| 654 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 655 | static int acpi_power_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 657 | int result = 0, state; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 658 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | struct acpi_power_resource *resource = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 660 | union acpi_object acpi_object; |
| 661 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 665 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 667 | resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (!resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 669 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Patrick Mochel | 4159857 | 2006-05-19 16:54:40 -0400 | [diff] [blame] | 671 | resource->device = device; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 672 | mutex_init(&resource->resource_lock); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame^] | 673 | INIT_LIST_HEAD(&resource->dependent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | strcpy(resource->name, device->pnp.bus_id); |
| 675 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
| 676 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 677 | device->driver_data = resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
| 679 | /* Evalute the object to get the system level and resource order. */ |
Patrick Mochel | 5fbc19e | 2006-05-19 16:54:43 -0400 | [diff] [blame] | 680 | status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (ACPI_FAILURE(status)) { |
| 682 | result = -ENODEV; |
| 683 | goto end; |
| 684 | } |
| 685 | resource->system_level = acpi_object.power_resource.system_level; |
| 686 | resource->order = acpi_object.power_resource.resource_order; |
| 687 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 688 | result = acpi_power_get_state(device->handle, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (result) |
| 690 | goto end; |
| 691 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 692 | switch (state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | case ACPI_POWER_RESOURCE_STATE_ON: |
| 694 | device->power.state = ACPI_STATE_D0; |
| 695 | break; |
| 696 | case ACPI_POWER_RESOURCE_STATE_OFF: |
| 697 | device->power.state = ACPI_STATE_D3; |
| 698 | break; |
| 699 | default: |
| 700 | device->power.state = ACPI_STATE_UNKNOWN; |
| 701 | break; |
| 702 | } |
| 703 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 704 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 705 | acpi_device_bid(device), state ? "on" : "off"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 706 | |
| 707 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (result) |
| 709 | kfree(resource); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 710 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 711 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 714 | static int acpi_power_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 716 | struct acpi_power_resource *resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 718 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 719 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 721 | resource = acpi_driver_data(device); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 722 | if (!resource) |
| 723 | return -EINVAL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | kfree(resource); |
| 726 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 727 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 730 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 731 | static int acpi_power_resume(struct device *dev) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 732 | { |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 733 | int result = 0, state; |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 734 | struct acpi_device *device; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 735 | struct acpi_power_resource *resource; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 736 | |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 737 | if (!dev) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 738 | return -EINVAL; |
| 739 | |
Rafael J. Wysocki | e579e2d | 2012-06-27 23:26:59 +0200 | [diff] [blame] | 740 | device = to_acpi_device(dev); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 741 | resource = acpi_driver_data(device); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 742 | if (!resource) |
| 743 | return -EINVAL; |
| 744 | |
| 745 | mutex_lock(&resource->resource_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 746 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 747 | result = acpi_power_get_state(device->handle, &state); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 748 | if (result) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 749 | goto unlock; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 750 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 751 | if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count) |
| 752 | result = __acpi_power_on(resource); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 753 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 754 | unlock: |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 755 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 756 | |
| 757 | return result; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 758 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 759 | #endif |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 760 | |
Bjorn Helgaas | 4451537 | 2009-03-24 16:49:53 -0600 | [diff] [blame] | 761 | int __init acpi_power_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | INIT_LIST_HEAD(&acpi_power_resource_list); |
Zhang Rui | 06af7eb | 2010-07-15 10:46:38 +0800 | [diff] [blame] | 764 | return acpi_bus_register_driver(&acpi_power_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |