blob: fec225d1b6b74520f6b132c2bcefd5dcb600b484 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_POWER_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("acpi_power")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#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 Brown4be44fc2005-08-05 00:44:28 -040058static int acpi_power_add(struct acpi_device *device);
59static int acpi_power_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int acpi_power_open_fs(struct inode *inode, struct file *file);
61
62static struct acpi_driver acpi_power_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040063 .name = ACPI_POWER_DRIVER_NAME,
64 .class = ACPI_POWER_CLASS,
65 .ids = ACPI_POWER_HID,
66 .ops = {
67 .add = acpi_power_add,
68 .remove = acpi_power_remove,
69 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Len Brown4be44fc2005-08-05 00:44:28 -040072struct acpi_power_resource {
Patrick Mochel41598572006-05-19 16:54:40 -040073 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040074 acpi_bus_id name;
75 u32 system_level;
76 u32 order;
77 int state;
78 int references;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Len Brown4be44fc2005-08-05 00:44:28 -040081static struct list_head acpi_power_resource_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Arjan van de Vend7508032006-07-04 13:06:00 -040083static const struct file_operations acpi_power_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040084 .open = acpi_power_open_fs,
85 .read = seq_read,
86 .llseek = seq_lseek,
87 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90/* --------------------------------------------------------------------------
91 Power Resource Management
92 -------------------------------------------------------------------------- */
93
94static int
Len Brown4be44fc2005-08-05 00:44:28 -040095acpi_power_get_context(acpi_handle handle,
96 struct acpi_power_resource **resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Len Brown4be44fc2005-08-05 00:44:28 -040098 int result = 0;
99 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400103 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 result = acpi_bus_get_device(handle, &device);
106 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400107 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
Patrick Mocheld550d982006-06-27 00:41:40 -0400108 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Len Brown4be44fc2005-08-05 00:44:28 -0400111 *resource = (struct acpi_power_resource *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick Mocheld550d982006-06-27 00:41:40 -0400115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Len Brown4be44fc2005-08-05 00:44:28 -0400118static int acpi_power_get_state(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Len Brown4be44fc2005-08-05 00:44:28 -0400120 acpi_status status = AE_OK;
121 unsigned long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400127 status = acpi_evaluate_integer(resource->device->handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400129 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (sta & 0x01)
132 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
133 else
134 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
135
136 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400137 resource->name, resource->state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Patrick Mocheld550d982006-06-27 00:41:40 -0400139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Len Brown4be44fc2005-08-05 00:44:28 -0400142static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Len Brown4be44fc2005-08-05 00:44:28 -0400144 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* The state of the list is 'on' IFF all resources are 'on'. */
153
Len Brown4be44fc2005-08-05 00:44:28 -0400154 for (i = 0; i < list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 result = acpi_power_get_context(list->handles[i], &resource);
156 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400157 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 result = acpi_power_get_state(resource);
159 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400160 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 *state = resource->state;
163
164 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
165 break;
166 }
167
168 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400169 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Len Brown4be44fc2005-08-05 00:44:28 -0400174static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Len Brown4be44fc2005-08-05 00:44:28 -0400176 int result = 0;
177 acpi_status status = AE_OK;
178 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct acpi_power_resource *resource = NULL;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 result = acpi_power_get_context(handle, &resource);
183 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400184 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 resource->references++;
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188 if ((resource->references > 1)
189 || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400191 resource->name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400195 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400197 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 result = acpi_power_get_state(resource);
200 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* Update the power resource's _device_ power state */
Patrick Mochel41598572006-05-19 16:54:40 -0400206 device = resource->device;
207 resource->device->power.state = ACPI_STATE_D0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400210 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Patrick Mocheld550d982006-06-27 00:41:40 -0400212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215static int acpi_power_off_device(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Len Brown4be44fc2005-08-05 00:44:28 -0400217 int result = 0;
218 acpi_status status = AE_OK;
219 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct acpi_power_resource *resource = NULL;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 result = acpi_power_get_context(handle, &resource);
224 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (resource->references)
228 resource->references--;
229
230 if (resource->references) {
Len Brown4be44fc2005-08-05 00:44:28 -0400231 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
232 "Resource [%s] is still in use, dereferencing\n",
233 device->pnp.bus_id));
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
238 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400239 device->pnp.bus_id));
Patrick Mocheld550d982006-06-27 00:41:40 -0400240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400243 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400245 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 result = acpi_power_get_state(resource);
248 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Update the power resource's _device_ power state */
Patrick Mochel41598572006-05-19 16:54:40 -0400254 device = resource->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 device->power.state = ACPI_STATE_D3;
256
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400258 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
265 * 1. Power on the power resources required for the wakeup device
266 * 2. Enable _PSW (power state wake) for the device if present
267 */
Len Brown4be44fc2005-08-05 00:44:28 -0400268int acpi_enable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Len Brown4be44fc2005-08-05 00:44:28 -0400270 union acpi_object arg = { ACPI_TYPE_INTEGER };
271 struct acpi_object_list arg_list = { 1, &arg };
272 acpi_status status = AE_OK;
273 int i;
274 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!dev || !dev->wakeup.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400277 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 arg.integer.value = 1;
280 /* Open power resource */
281 for (i = 0; i < dev->wakeup.resources.count; i++) {
282 ret = acpi_power_on(dev->wakeup.resources.handles[i]);
283 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400284 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
289
290 /* Execute PSW */
291 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
292 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Len Brown64684632006-06-26 23:41:38 -0400293 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 dev->wakeup.flags.valid = 0;
295 ret = -1;
296 }
297
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
302 * Shutdown a wakeup device, counterpart of above method
303 * 1. Disable _PSW (power state wake)
304 * 2. Shutdown down the power resources
305 */
Len Brown4be44fc2005-08-05 00:44:28 -0400306int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Len Brown4be44fc2005-08-05 00:44:28 -0400308 union acpi_object arg = { ACPI_TYPE_INTEGER };
309 struct acpi_object_list arg_list = { 1, &arg };
310 acpi_status status = AE_OK;
311 int i;
312 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (!dev || !dev->wakeup.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Len Brown4be44fc2005-08-05 00:44:28 -0400318 arg.integer.value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Execute PSW */
320 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
321 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Len Brown64684632006-06-26 23:41:38 -0400322 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 /* Close power resource */
328 for (i = 0; i < dev->wakeup.resources.count; i++) {
329 ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
330 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400331 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400333 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335 }
336
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/* --------------------------------------------------------------------------
341 Device Power Management
342 -------------------------------------------------------------------------- */
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344int acpi_power_get_inferred_state(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Len Brown4be44fc2005-08-05 00:44:28 -0400346 int result = 0;
347 struct acpi_handle_list *list = NULL;
348 int list_state = 0;
349 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 device->power.state = ACPI_STATE_UNKNOWN;
356
357 /*
358 * We know a device's inferred power state when all the resources
359 * required for a given D-state are 'on'.
360 */
Len Brown4be44fc2005-08-05 00:44:28 -0400361 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 list = &device->power.states[i].resources;
363 if (list->count < 1)
364 continue;
365
366 result = acpi_power_get_list_state(list, &list_state);
367 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400368 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
371 device->power.state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 }
375
376 device->power.state = ACPI_STATE_D3;
377
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Len Brown4be44fc2005-08-05 00:44:28 -0400381int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Len Brown4be44fc2005-08-05 00:44:28 -0400383 int result = 0;
384 struct acpi_handle_list *cl = NULL; /* Current Resources */
385 struct acpi_handle_list *tl = NULL; /* Target Resources */
386 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 if ((device->power.state < ACPI_STATE_D0)
393 || (device->power.state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 cl = &device->power.states[device->power.state].resources;
397 tl = &device->power.states[state].resources;
398
399 device->power.state = ACPI_STATE_UNKNOWN;
400
401 if (!cl->count && !tl->count) {
402 result = -ENODEV;
403 goto end;
404 }
405
406 /* TBD: Resources must be ordered. */
407
408 /*
409 * First we reference all power resources required in the target list
410 * (e.g. so the device doesn't lose power while transitioning).
411 */
Len Brown4be44fc2005-08-05 00:44:28 -0400412 for (i = 0; i < tl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 result = acpi_power_on(tl->handles[i]);
414 if (result)
415 goto end;
416 }
417
418 /*
419 * Then we dereference all power resources used in the current list.
420 */
Len Brown4be44fc2005-08-05 00:44:28 -0400421 for (i = 0; i < cl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 result = acpi_power_off_device(cl->handles[i]);
423 if (result)
424 goto end;
425 }
426
427 /* We shouldn't change the state till all above operations succeed */
428 device->power.state = state;
Len Brown4be44fc2005-08-05 00:44:28 -0400429 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (result)
Len Browncece9292006-06-26 23:04:31 -0400431 printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
432 device->pnp.bus_id, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Patrick Mocheld550d982006-06-27 00:41:40 -0400434 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/* --------------------------------------------------------------------------
438 FS Interface (/proc)
439 -------------------------------------------------------------------------- */
440
Len Brown4be44fc2005-08-05 00:44:28 -0400441static struct proc_dir_entry *acpi_power_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443static int acpi_power_seq_show(struct seq_file *seq, void *offset)
444{
445 struct acpi_power_resource *resource = NULL;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 resource = (struct acpi_power_resource *)seq->private;
449
450 if (!resource)
451 goto end;
452
453 seq_puts(seq, "state: ");
454 switch (resource->state) {
455 case ACPI_POWER_RESOURCE_STATE_ON:
456 seq_puts(seq, "on\n");
457 break;
458 case ACPI_POWER_RESOURCE_STATE_OFF:
459 seq_puts(seq, "off\n");
460 break;
461 default:
462 seq_puts(seq, "unknown\n");
463 break;
464 }
465
466 seq_printf(seq, "system level: S%d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400467 "order: %d\n"
468 "reference count: %d\n",
469 resource->system_level,
470 resource->order, resource->references);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static int acpi_power_open_fs(struct inode *inode, struct file *file)
477{
478 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
479}
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481static int acpi_power_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown4be44fc2005-08-05 00:44:28 -0400483 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (!acpi_device_dir(device)) {
490 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400491 acpi_power_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
496 /* 'status' [R] */
497 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400498 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400500 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 else {
502 entry->proc_fops = &acpi_power_fops;
503 entry->data = acpi_driver_data(device);
504 }
505
Patrick Mocheld550d982006-06-27 00:41:40 -0400506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Len Brown4be44fc2005-08-05 00:44:28 -0400509static int acpi_power_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if (acpi_device_dir(device)) {
513 remove_proc_entry(ACPI_POWER_FILE_STATUS,
514 acpi_device_dir(device));
515 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
516 acpi_device_dir(device) = NULL;
517 }
518
Patrick Mocheld550d982006-06-27 00:41:40 -0400519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/* --------------------------------------------------------------------------
523 Driver Interface
524 -------------------------------------------------------------------------- */
525
Len Brown4be44fc2005-08-05 00:44:28 -0400526static int acpi_power_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Len Brown4be44fc2005-08-05 00:44:28 -0400528 int result = 0;
529 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400531 union acpi_object acpi_object;
532 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
539 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400540 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 memset(resource, 0, sizeof(struct acpi_power_resource));
542
Patrick Mochel41598572006-05-19 16:54:40 -0400543 resource->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 strcpy(resource->name, device->pnp.bus_id);
545 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
546 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
547 acpi_driver_data(device) = resource;
548
549 /* Evalute the object to get the system level and resource order. */
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400550 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (ACPI_FAILURE(status)) {
552 result = -ENODEV;
553 goto end;
554 }
555 resource->system_level = acpi_object.power_resource.system_level;
556 resource->order = acpi_object.power_resource.resource_order;
557
558 result = acpi_power_get_state(resource);
559 if (result)
560 goto end;
561
562 switch (resource->state) {
563 case ACPI_POWER_RESOURCE_STATE_ON:
564 device->power.state = ACPI_STATE_D0;
565 break;
566 case ACPI_POWER_RESOURCE_STATE_OFF:
567 device->power.state = ACPI_STATE_D3;
568 break;
569 default:
570 device->power.state = ACPI_STATE_UNKNOWN;
571 break;
572 }
573
574 result = acpi_power_add_fs(device);
575 if (result)
576 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Len Brown4be44fc2005-08-05 00:44:28 -0400578 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
579 acpi_device_bid(device), resource->state ? "on" : "off");
580
581 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (result)
583 kfree(resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400584
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Len Brown4be44fc2005-08-05 00:44:28 -0400588static int acpi_power_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct acpi_power_resource *resource = NULL;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Len Brown4be44fc2005-08-05 00:44:28 -0400596 resource = (struct acpi_power_resource *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 acpi_power_remove_fs(device);
599
600 kfree(resource);
601
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Len Brown4be44fc2005-08-05 00:44:28 -0400605static int __init acpi_power_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Len Brown4be44fc2005-08-05 00:44:28 -0400607 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 INIT_LIST_HEAD(&acpi_power_resource_list);
614
615 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
616 if (!acpi_power_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 result = acpi_bus_register_driver(&acpi_power_driver);
620 if (result < 0) {
621 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400622 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628subsys_initcall(acpi_power_init);