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> |
| 42 | #include <linux/proc_fs.h> |
| 43 | #include <linux/seq_file.h> |
| 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" |
| 47 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 48 | #define PREFIX "ACPI: " |
| 49 | |
Bjorn Helgaas | 89595b8 | 2008-11-07 16:57:45 -0700 | [diff] [blame] | 50 | #define _COMPONENT ACPI_POWER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 51 | ACPI_MODULE_NAME("power"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define ACPI_POWER_CLASS "power_resource" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define ACPI_POWER_DEVICE_NAME "Power Resource" |
| 54 | #define ACPI_POWER_FILE_INFO "info" |
| 55 | #define ACPI_POWER_FILE_STATUS "state" |
| 56 | #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 |
| 57 | #define ACPI_POWER_RESOURCE_STATE_ON 0x01 |
| 58 | #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 59 | |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 60 | int acpi_power_nocheck; |
| 61 | module_param_named(power_nocheck, acpi_power_nocheck, bool, 000); |
| 62 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 63 | static int acpi_power_add(struct acpi_device *device); |
| 64 | static int acpi_power_remove(struct acpi_device *device, int type); |
Len Brown | e8363f3 | 2007-02-16 02:05:39 -0500 | [diff] [blame] | 65 | static int acpi_power_resume(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static int acpi_power_open_fs(struct inode *inode, struct file *file); |
| 67 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 68 | static struct acpi_device_id power_device_ids[] = { |
| 69 | {ACPI_POWER_HID, 0}, |
| 70 | {"", 0}, |
| 71 | }; |
| 72 | MODULE_DEVICE_TABLE(acpi, power_device_ids); |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static struct acpi_driver acpi_power_driver = { |
Len Brown | c2b6705b | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 75 | .name = "power", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | .class = ACPI_POWER_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 77 | .ids = power_device_ids, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 78 | .ops = { |
| 79 | .add = acpi_power_add, |
| 80 | .remove = acpi_power_remove, |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 81 | .resume = acpi_power_resume, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 82 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 85 | struct acpi_power_reference { |
| 86 | struct list_head node; |
| 87 | struct acpi_device *device; |
| 88 | }; |
| 89 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 90 | struct acpi_power_resource { |
Patrick Mochel | 4159857 | 2006-05-19 16:54:40 -0400 | [diff] [blame] | 91 | struct acpi_device * device; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 92 | acpi_bus_id name; |
| 93 | u32 system_level; |
| 94 | u32 order; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 95 | struct mutex resource_lock; |
| 96 | struct list_head reference; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 99 | static struct list_head acpi_power_resource_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 101 | static const struct file_operations acpi_power_fops = { |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 102 | .owner = THIS_MODULE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 103 | .open = acpi_power_open_fs, |
| 104 | .read = seq_read, |
| 105 | .llseek = seq_lseek, |
| 106 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* -------------------------------------------------------------------------- |
| 110 | Power Resource Management |
| 111 | -------------------------------------------------------------------------- */ |
| 112 | |
| 113 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 114 | acpi_power_get_context(acpi_handle handle, |
| 115 | struct acpi_power_resource **resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 117 | int result = 0; |
| 118 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | if (!resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 122 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | result = acpi_bus_get_device(handle, &device); |
| 125 | if (result) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 126 | printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 127 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 130 | *resource = acpi_driver_data(device); |
Li Zefan | a815ab8 | 2008-04-18 13:27:29 -0700 | [diff] [blame] | 131 | if (!*resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 132 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 134 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 137 | static int acpi_power_get_state(acpi_handle handle, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 139 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 140 | unsigned long long sta = 0; |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 141 | char node_name[5]; |
| 142 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 145 | if (!handle || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 146 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 148 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 150 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 152 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
| 153 | ACPI_POWER_RESOURCE_STATE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 155 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 158 | node_name, |
Zhao Yakui | b1b57fb | 2008-10-27 16:04:53 +0800 | [diff] [blame] | 159 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 161 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 164 | 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] | 165 | { |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 166 | int result = 0, state1; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 167 | u32 i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | if (!list || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 171 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* The state of the list is 'on' IFF all resources are 'on'. */ |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 174 | /* */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 176 | for (i = 0; i < list->count; i++) { |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 177 | /* |
| 178 | * The state of the power resource can be obtained by |
| 179 | * using the ACPI handle. In such case it is unnecessary to |
| 180 | * get the Power resource first and then get its state again. |
| 181 | */ |
| 182 | result = acpi_power_get_state(list->handles[i], &state1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 184 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 186 | *state = state1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | if (*state != ACPI_POWER_RESOURCE_STATE_ON) |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 193 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 195 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 198 | static int acpi_power_on(acpi_handle handle, struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Bjorn Helgaas | bdf43bb | 2009-05-21 17:28:53 -0600 | [diff] [blame] | 200 | int result = 0; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 201 | int found = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 202 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct acpi_power_resource *resource = NULL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 204 | struct list_head *node, *next; |
| 205 | struct acpi_power_reference *ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | result = acpi_power_get_context(handle, &resource); |
| 209 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 210 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 212 | mutex_lock(&resource->resource_lock); |
| 213 | list_for_each_safe(node, next, &resource->reference) { |
| 214 | ref = container_of(node, struct acpi_power_reference, node); |
| 215 | if (dev->handle == ref->device->handle) { |
| 216 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n", |
| 217 | dev->pnp.bus_id, resource->name)); |
| 218 | found = 1; |
| 219 | break; |
| 220 | } |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 223 | if (!found) { |
| 224 | ref = kmalloc(sizeof (struct acpi_power_reference), |
| 225 | irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL); |
| 226 | if (!ref) { |
| 227 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n")); |
| 228 | mutex_unlock(&resource->resource_lock); |
| 229 | return -ENOMEM; |
| 230 | } |
| 231 | list_add_tail(&ref->node, &resource->reference); |
| 232 | ref->device = dev; |
| 233 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n", |
| 234 | dev->pnp.bus_id, resource->name)); |
| 235 | } |
| 236 | mutex_unlock(&resource->resource_lock); |
| 237 | |
Patrick Mochel | 5fbc19e | 2006-05-19 16:54:43 -0400 | [diff] [blame] | 238 | status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 240 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* Update the power resource's _device_ power state */ |
Patrick Mochel | 4159857 | 2006-05-19 16:54:40 -0400 | [diff] [blame] | 243 | resource->device->power.state = ACPI_STATE_D0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | resource->name)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 247 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 250 | static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
Bjorn Helgaas | bdf43bb | 2009-05-21 17:28:53 -0600 | [diff] [blame] | 252 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 253 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct acpi_power_resource *resource = NULL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 255 | struct list_head *node, *next; |
| 256 | struct acpi_power_reference *ref; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | result = acpi_power_get_context(handle, &resource); |
| 260 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 261 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 263 | mutex_lock(&resource->resource_lock); |
| 264 | list_for_each_safe(node, next, &resource->reference) { |
| 265 | ref = container_of(node, struct acpi_power_reference, node); |
| 266 | if (dev->handle == ref->device->handle) { |
| 267 | list_del(&ref->node); |
| 268 | kfree(ref); |
| 269 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n", |
| 270 | dev->pnp.bus_id, resource->name)); |
| 271 | break; |
| 272 | } |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 275 | if (!list_empty(&resource->reference)) { |
| 276 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n", |
| 277 | resource->name)); |
| 278 | mutex_unlock(&resource->resource_lock); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 279 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 281 | mutex_unlock(&resource->resource_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Patrick Mochel | 5fbc19e | 2006-05-19 16:54:43 -0400 | [diff] [blame] | 283 | status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 285 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /* Update the power resource's _device_ power state */ |
Dmitry Torokhov | 786f18c | 2006-08-23 23:18:06 -0400 | [diff] [blame] | 288 | resource->device->power.state = ACPI_STATE_D3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 291 | resource->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 293 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 296 | /** |
| 297 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in |
| 298 | * ACPI 3.0) _PSW (Power State Wake) |
| 299 | * @dev: Device to handle. |
| 300 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. |
| 301 | * @sleep_state: Target sleep state of the system. |
| 302 | * @dev_state: Target power state of the device. |
| 303 | * |
| 304 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 305 | * State Wake) for the device, if present. On failure reset the device's |
| 306 | * wakeup.flags.valid flag. |
| 307 | * |
| 308 | * RETURN VALUE: |
| 309 | * 0 if either _DSW or _PSW has been successfully executed |
| 310 | * 0 if neither _DSW nor _PSW has been found |
| 311 | * -ENODEV if the execution of either _DSW or _PSW has failed |
| 312 | */ |
| 313 | int acpi_device_sleep_wake(struct acpi_device *dev, |
| 314 | int enable, int sleep_state, int dev_state) |
| 315 | { |
| 316 | union acpi_object in_arg[3]; |
| 317 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 318 | acpi_status status = AE_OK; |
| 319 | |
| 320 | /* |
| 321 | * Try to execute _DSW first. |
| 322 | * |
| 323 | * Three agruments are needed for the _DSW object: |
| 324 | * Argument 0: enable/disable the wake capabilities |
| 325 | * Argument 1: target system state |
| 326 | * Argument 2: target device state |
| 327 | * When _DSW object is called to disable the wake capabilities, maybe |
| 328 | * the first argument is filled. The values of the other two agruments |
| 329 | * are meaningless. |
| 330 | */ |
| 331 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 332 | in_arg[0].integer.value = enable; |
| 333 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 334 | in_arg[1].integer.value = sleep_state; |
| 335 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 336 | in_arg[2].integer.value = dev_state; |
| 337 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); |
| 338 | if (ACPI_SUCCESS(status)) { |
| 339 | return 0; |
| 340 | } else if (status != AE_NOT_FOUND) { |
| 341 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); |
| 342 | dev->wakeup.flags.valid = 0; |
| 343 | return -ENODEV; |
| 344 | } |
| 345 | |
| 346 | /* Execute _PSW */ |
| 347 | arg_list.count = 1; |
| 348 | in_arg[0].integer.value = enable; |
| 349 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); |
| 350 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { |
| 351 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); |
| 352 | dev->wakeup.flags.valid = 0; |
| 353 | return -ENODEV; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): |
| 361 | * 1. Power on the power resources required for the wakeup device |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 362 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 363 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | */ |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 365 | 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] | 366 | { |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 367 | int i, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 370 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 372 | mutex_lock(&acpi_device_lock); |
| 373 | |
| 374 | if (dev->wakeup.prepare_count++) |
| 375 | goto out; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* Open power resource */ |
| 378 | for (i = 0; i < dev->wakeup.resources.count; i++) { |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 379 | int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (ret) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 381 | printk(KERN_ERR PREFIX "Transition power state\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | dev->wakeup.flags.valid = 0; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 383 | err = -ENODEV; |
| 384 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 388 | /* |
| 389 | * Passing 3 as the third argument below means the device may be placed |
| 390 | * in arbitrary power state afterwards. |
| 391 | */ |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 392 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 393 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 394 | err_out: |
| 395 | if (err) |
| 396 | dev->wakeup.prepare_count = 0; |
| 397 | |
| 398 | out: |
| 399 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 400 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Shutdown a wakeup device, counterpart of above method |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 405 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 406 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * 2. Shutdown down the power resources |
| 408 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 409 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 411 | int i, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 414 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 416 | mutex_lock(&acpi_device_lock); |
| 417 | |
| 418 | if (--dev->wakeup.prepare_count > 0) |
| 419 | goto out; |
| 420 | |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 421 | /* |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 422 | * Executing the code below even if prepare_count is already zero when |
| 423 | * the function is called may be useful, for example for initialisation. |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 424 | */ |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 425 | if (dev->wakeup.prepare_count < 0) |
| 426 | dev->wakeup.prepare_count = 0; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 427 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 428 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
| 429 | if (err) |
| 430 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* Close power resource */ |
| 433 | for (i = 0; i < dev->wakeup.resources.count; i++) { |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 434 | int ret = acpi_power_off_device( |
| 435 | dev->wakeup.resources.handles[i], dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (ret) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 437 | printk(KERN_ERR PREFIX "Transition power state\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | dev->wakeup.flags.valid = 0; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 439 | err = -ENODEV; |
| 440 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 444 | out: |
| 445 | mutex_unlock(&acpi_device_lock); |
| 446 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* -------------------------------------------------------------------------- |
| 450 | Device Power Management |
| 451 | -------------------------------------------------------------------------- */ |
| 452 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 453 | int acpi_power_get_inferred_state(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 455 | int result = 0; |
| 456 | struct acpi_handle_list *list = NULL; |
| 457 | int list_state = 0; |
| 458 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 462 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | device->power.state = ACPI_STATE_UNKNOWN; |
| 465 | |
| 466 | /* |
| 467 | * We know a device's inferred power state when all the resources |
| 468 | * required for a given D-state are 'on'. |
| 469 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 470 | for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | list = &device->power.states[i].resources; |
| 472 | if (list->count < 1) |
| 473 | continue; |
| 474 | |
| 475 | result = acpi_power_get_list_state(list, &list_state); |
| 476 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 477 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { |
| 480 | device->power.state = i; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 481 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | device->power.state = ACPI_STATE_D3; |
| 486 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 487 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 490 | int acpi_power_transition(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 492 | int result = 0; |
| 493 | struct acpi_handle_list *cl = NULL; /* Current Resources */ |
| 494 | struct acpi_handle_list *tl = NULL; /* Target Resources */ |
| 495 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 499 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 501 | if ((device->power.state < ACPI_STATE_D0) |
| 502 | || (device->power.state > ACPI_STATE_D3)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 503 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | cl = &device->power.states[device->power.state].resources; |
| 506 | tl = &device->power.states[state].resources; |
| 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | /* TBD: Resources must be ordered. */ |
| 509 | |
| 510 | /* |
| 511 | * First we reference all power resources required in the target list |
| 512 | * (e.g. so the device doesn't lose power while transitioning). |
| 513 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 514 | for (i = 0; i < tl->count; i++) { |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 515 | result = acpi_power_on(tl->handles[i], device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (result) |
| 517 | goto end; |
| 518 | } |
| 519 | |
Konstantin Karasyov | b1028c5 | 2007-02-16 02:23:07 -0500 | [diff] [blame] | 520 | if (device->power.state == state) { |
| 521 | goto end; |
| 522 | } |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* |
| 525 | * Then we dereference all power resources used in the current list. |
| 526 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 527 | for (i = 0; i < cl->count; i++) { |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 528 | result = acpi_power_off_device(cl->handles[i], device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (result) |
| 530 | goto end; |
| 531 | } |
| 532 | |
Konstantin Karasyov | 7292576 | 2007-02-21 02:05:58 -0500 | [diff] [blame] | 533 | end: |
Miguel Botón | e76d5f7 | 2008-02-04 23:31:19 -0800 | [diff] [blame] | 534 | if (result) |
Konstantin Karasyov | 7292576 | 2007-02-21 02:05:58 -0500 | [diff] [blame] | 535 | device->power.state = ACPI_STATE_UNKNOWN; |
Miguel Botón | e76d5f7 | 2008-02-04 23:31:19 -0800 | [diff] [blame] | 536 | else { |
Konstantin Karasyov | 7292576 | 2007-02-21 02:05:58 -0500 | [diff] [blame] | 537 | /* We shouldn't change the state till all above operations succeed */ |
| 538 | device->power.state = state; |
| 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 541 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | /* -------------------------------------------------------------------------- |
| 545 | FS Interface (/proc) |
| 546 | -------------------------------------------------------------------------- */ |
| 547 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 548 | static struct proc_dir_entry *acpi_power_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | static int acpi_power_seq_show(struct seq_file *seq, void *offset) |
| 551 | { |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 552 | int count = 0; |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 553 | int result = 0, state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | struct acpi_power_resource *resource = NULL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 555 | struct list_head *node, *next; |
| 556 | struct acpi_power_reference *ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 559 | resource = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (!resource) |
| 562 | goto end; |
| 563 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 564 | result = acpi_power_get_state(resource->device->handle, &state); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 565 | if (result) |
| 566 | goto end; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | seq_puts(seq, "state: "); |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 569 | switch (state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | case ACPI_POWER_RESOURCE_STATE_ON: |
| 571 | seq_puts(seq, "on\n"); |
| 572 | break; |
| 573 | case ACPI_POWER_RESOURCE_STATE_OFF: |
| 574 | seq_puts(seq, "off\n"); |
| 575 | break; |
| 576 | default: |
| 577 | seq_puts(seq, "unknown\n"); |
| 578 | break; |
| 579 | } |
| 580 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 581 | mutex_lock(&resource->resource_lock); |
| 582 | list_for_each_safe(node, next, &resource->reference) { |
| 583 | ref = container_of(node, struct acpi_power_reference, node); |
| 584 | count++; |
| 585 | } |
| 586 | mutex_unlock(&resource->resource_lock); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | seq_printf(seq, "system level: S%d\n" |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 589 | "order: %d\n" |
| 590 | "reference count: %d\n", |
| 591 | resource->system_level, |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 592 | resource->order, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 594 | end: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 595 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static int acpi_power_open_fs(struct inode *inode, struct file *file) |
| 599 | { |
| 600 | return single_open(file, acpi_power_seq_show, PDE(inode)->data); |
| 601 | } |
| 602 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 603 | static int acpi_power_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 605 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 609 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | if (!acpi_device_dir(device)) { |
| 612 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 613 | acpi_power_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 615 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* 'status' [R] */ |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 619 | entry = proc_create_data(ACPI_POWER_FILE_STATUS, |
| 620 | S_IRUGO, acpi_device_dir(device), |
| 621 | &acpi_power_fops, acpi_driver_data(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 623 | return -EIO; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 624 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 627 | static int acpi_power_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | if (acpi_device_dir(device)) { |
| 631 | remove_proc_entry(ACPI_POWER_FILE_STATUS, |
| 632 | acpi_device_dir(device)); |
| 633 | remove_proc_entry(acpi_device_bid(device), acpi_power_dir); |
| 634 | acpi_device_dir(device) = NULL; |
| 635 | } |
| 636 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 637 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | /* -------------------------------------------------------------------------- |
| 641 | Driver Interface |
| 642 | -------------------------------------------------------------------------- */ |
| 643 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 644 | static int acpi_power_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 646 | int result = 0, state; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 647 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | struct acpi_power_resource *resource = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 649 | union acpi_object acpi_object; |
| 650 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; |
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 | |
| 653 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 654 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 656 | resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (!resource) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 658 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Patrick Mochel | 4159857 | 2006-05-19 16:54:40 -0400 | [diff] [blame] | 660 | resource->device = device; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 661 | mutex_init(&resource->resource_lock); |
| 662 | INIT_LIST_HEAD(&resource->reference); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | strcpy(resource->name, device->pnp.bus_id); |
| 664 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
| 665 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 666 | device->driver_data = resource; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | /* Evalute the object to get the system level and resource order. */ |
Patrick Mochel | 5fbc19e | 2006-05-19 16:54:43 -0400 | [diff] [blame] | 669 | status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (ACPI_FAILURE(status)) { |
| 671 | result = -ENODEV; |
| 672 | goto end; |
| 673 | } |
| 674 | resource->system_level = acpi_object.power_resource.system_level; |
| 675 | resource->order = acpi_object.power_resource.resource_order; |
| 676 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 677 | result = acpi_power_get_state(device->handle, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (result) |
| 679 | goto end; |
| 680 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 681 | switch (state) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | case ACPI_POWER_RESOURCE_STATE_ON: |
| 683 | device->power.state = ACPI_STATE_D0; |
| 684 | break; |
| 685 | case ACPI_POWER_RESOURCE_STATE_OFF: |
| 686 | device->power.state = ACPI_STATE_D3; |
| 687 | break; |
| 688 | default: |
| 689 | device->power.state = ACPI_STATE_UNKNOWN; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | result = acpi_power_add_fs(device); |
| 694 | if (result) |
| 695 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 697 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 698 | acpi_device_bid(device), state ? "on" : "off"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 699 | |
| 700 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (result) |
| 702 | kfree(resource); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 703 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 704 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 707 | static int acpi_power_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
| 709 | struct acpi_power_resource *resource = NULL; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 710 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
| 713 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 714 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 716 | resource = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | acpi_power_remove_fs(device); |
| 719 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 720 | mutex_lock(&resource->resource_lock); |
| 721 | list_for_each_safe(node, next, &resource->reference) { |
| 722 | struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node); |
| 723 | list_del(&ref->node); |
| 724 | kfree(ref); |
| 725 | } |
| 726 | mutex_unlock(&resource->resource_lock); |
| 727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | kfree(resource); |
| 729 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 730 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Len Brown | e8363f3 | 2007-02-16 02:05:39 -0500 | [diff] [blame] | 733 | static int acpi_power_resume(struct acpi_device *device) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 734 | { |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 735 | int result = 0, state; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 736 | struct acpi_power_resource *resource = NULL; |
| 737 | struct acpi_power_reference *ref; |
| 738 | |
| 739 | if (!device || !acpi_driver_data(device)) |
| 740 | return -EINVAL; |
| 741 | |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 742 | resource = acpi_driver_data(device); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 743 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 744 | result = acpi_power_get_state(device->handle, &state); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 745 | if (result) |
| 746 | return result; |
| 747 | |
| 748 | mutex_lock(&resource->resource_lock); |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 749 | if (state == ACPI_POWER_RESOURCE_STATE_OFF && |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 750 | !list_empty(&resource->reference)) { |
| 751 | ref = container_of(resource->reference.next, struct acpi_power_reference, node); |
| 752 | mutex_unlock(&resource->resource_lock); |
| 753 | result = acpi_power_on(device->handle, ref->device); |
| 754 | return result; |
| 755 | } |
| 756 | |
| 757 | mutex_unlock(&resource->resource_lock); |
| 758 | return 0; |
| 759 | } |
| 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 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 763 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | INIT_LIST_HEAD(&acpi_power_resource_list); |
| 766 | |
| 767 | acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir); |
| 768 | if (!acpi_power_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 769 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | result = acpi_bus_register_driver(&acpi_power_driver); |
| 772 | if (result < 0) { |
| 773 | remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 774 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 777 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |