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