blob: 224f729f700e2e6b75f307de2b02c1af76ba70f2 [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 {
73 acpi_handle handle;
74 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
83static 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
127 status = acpi_evaluate_integer(resource->handle, "_STA", NULL, &sta);
128 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
195 status = acpi_evaluate_object(resource->handle, "_ON", NULL, NULL);
196 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 */
206 result = acpi_bus_get_device(resource->handle, &device);
207 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 device->power.state = ACPI_STATE_D0;
210
211 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400212 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217static int acpi_power_off_device(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Len Brown4be44fc2005-08-05 00:44:28 -0400219 int result = 0;
220 acpi_status status = AE_OK;
221 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct acpi_power_resource *resource = NULL;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 result = acpi_power_get_context(handle, &resource);
226 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (resource->references)
230 resource->references--;
231
232 if (resource->references) {
Len Brown4be44fc2005-08-05 00:44:28 -0400233 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
234 "Resource [%s] is still in use, dereferencing\n",
235 device->pnp.bus_id));
Patrick Mocheld550d982006-06-27 00:41:40 -0400236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
239 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400241 device->pnp.bus_id));
Patrick Mocheld550d982006-06-27 00:41:40 -0400242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 status = acpi_evaluate_object(resource->handle, "_OFF", NULL, NULL);
246 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 result = acpi_power_get_state(resource);
250 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* Update the power resource's _device_ power state */
256 result = acpi_bus_get_device(resource->handle, &device);
257 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 device->power.state = ACPI_STATE_D3;
260
261 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400262 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Patrick Mocheld550d982006-06-27 00:41:40 -0400264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
269 * 1. Power on the power resources required for the wakeup device
270 * 2. Enable _PSW (power state wake) for the device if present
271 */
Len Brown4be44fc2005-08-05 00:44:28 -0400272int acpi_enable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Len Brown4be44fc2005-08-05 00:44:28 -0400274 union acpi_object arg = { ACPI_TYPE_INTEGER };
275 struct acpi_object_list arg_list = { 1, &arg };
276 acpi_status status = AE_OK;
277 int i;
278 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!dev || !dev->wakeup.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 arg.integer.value = 1;
284 /* Open power resource */
285 for (i = 0; i < dev->wakeup.resources.count; i++) {
286 ret = acpi_power_on(dev->wakeup.resources.handles[i]);
287 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400288 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 }
293
294 /* Execute PSW */
295 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
296 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Len Brown64684632006-06-26 23:41:38 -0400297 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 dev->wakeup.flags.valid = 0;
299 ret = -1;
300 }
301
Patrick Mocheld550d982006-06-27 00:41:40 -0400302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * Shutdown a wakeup device, counterpart of above method
307 * 1. Disable _PSW (power state wake)
308 * 2. Shutdown down the power resources
309 */
Len Brown4be44fc2005-08-05 00:44:28 -0400310int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Len Brown4be44fc2005-08-05 00:44:28 -0400312 union acpi_object arg = { ACPI_TYPE_INTEGER };
313 struct acpi_object_list arg_list = { 1, &arg };
314 acpi_status status = AE_OK;
315 int i;
316 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!dev || !dev->wakeup.flags.valid)
Patrick Mocheld550d982006-06-27 00:41:40 -0400320 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Len Brown4be44fc2005-08-05 00:44:28 -0400322 arg.integer.value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* Execute PSW */
324 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
325 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Len Brown64684632006-06-26 23:41:38 -0400326 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400328 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 /* Close power resource */
332 for (i = 0; i < dev->wakeup.resources.count; i++) {
333 ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
334 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400335 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev->wakeup.flags.valid = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
340
Patrick Mocheld550d982006-06-27 00:41:40 -0400341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/* --------------------------------------------------------------------------
345 Device Power Management
346 -------------------------------------------------------------------------- */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348int acpi_power_get_inferred_state(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 int result = 0;
351 struct acpi_handle_list *list = NULL;
352 int list_state = 0;
353 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 device->power.state = ACPI_STATE_UNKNOWN;
360
361 /*
362 * We know a device's inferred power state when all the resources
363 * required for a given D-state are 'on'.
364 */
Len Brown4be44fc2005-08-05 00:44:28 -0400365 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 list = &device->power.states[i].resources;
367 if (list->count < 1)
368 continue;
369
370 result = acpi_power_get_list_state(list, &list_state);
371 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
375 device->power.state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 }
379
380 device->power.state = ACPI_STATE_D3;
381
Patrick Mocheld550d982006-06-27 00:41:40 -0400382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Len Brown4be44fc2005-08-05 00:44:28 -0400385int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Len Brown4be44fc2005-08-05 00:44:28 -0400387 int result = 0;
388 struct acpi_handle_list *cl = NULL; /* Current Resources */
389 struct acpi_handle_list *tl = NULL; /* Target Resources */
390 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 if ((device->power.state < ACPI_STATE_D0)
397 || (device->power.state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400398 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 cl = &device->power.states[device->power.state].resources;
401 tl = &device->power.states[state].resources;
402
403 device->power.state = ACPI_STATE_UNKNOWN;
404
405 if (!cl->count && !tl->count) {
406 result = -ENODEV;
407 goto end;
408 }
409
410 /* TBD: Resources must be ordered. */
411
412 /*
413 * First we reference all power resources required in the target list
414 * (e.g. so the device doesn't lose power while transitioning).
415 */
Len Brown4be44fc2005-08-05 00:44:28 -0400416 for (i = 0; i < tl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 result = acpi_power_on(tl->handles[i]);
418 if (result)
419 goto end;
420 }
421
422 /*
423 * Then we dereference all power resources used in the current list.
424 */
Len Brown4be44fc2005-08-05 00:44:28 -0400425 for (i = 0; i < cl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 result = acpi_power_off_device(cl->handles[i]);
427 if (result)
428 goto end;
429 }
430
431 /* We shouldn't change the state till all above operations succeed */
432 device->power.state = state;
Len Brown4be44fc2005-08-05 00:44:28 -0400433 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (result)
Len Browncece9292006-06-26 23:04:31 -0400435 printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
436 device->pnp.bus_id, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Patrick Mocheld550d982006-06-27 00:41:40 -0400438 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* --------------------------------------------------------------------------
442 FS Interface (/proc)
443 -------------------------------------------------------------------------- */
444
Len Brown4be44fc2005-08-05 00:44:28 -0400445static struct proc_dir_entry *acpi_power_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447static int acpi_power_seq_show(struct seq_file *seq, void *offset)
448{
449 struct acpi_power_resource *resource = NULL;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 resource = (struct acpi_power_resource *)seq->private;
453
454 if (!resource)
455 goto end;
456
457 seq_puts(seq, "state: ");
458 switch (resource->state) {
459 case ACPI_POWER_RESOURCE_STATE_ON:
460 seq_puts(seq, "on\n");
461 break;
462 case ACPI_POWER_RESOURCE_STATE_OFF:
463 seq_puts(seq, "off\n");
464 break;
465 default:
466 seq_puts(seq, "unknown\n");
467 break;
468 }
469
470 seq_printf(seq, "system level: S%d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400471 "order: %d\n"
472 "reference count: %d\n",
473 resource->system_level,
474 resource->order, resource->references);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480static int acpi_power_open_fs(struct inode *inode, struct file *file)
481{
482 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
483}
484
Len Brown4be44fc2005-08-05 00:44:28 -0400485static int acpi_power_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Len Brown4be44fc2005-08-05 00:44:28 -0400487 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (!acpi_device_dir(device)) {
494 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400495 acpi_power_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400497 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 /* 'status' [R] */
501 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400502 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400504 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 else {
506 entry->proc_fops = &acpi_power_fops;
507 entry->data = acpi_driver_data(device);
508 }
509
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Len Brown4be44fc2005-08-05 00:44:28 -0400513static int acpi_power_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (acpi_device_dir(device)) {
517 remove_proc_entry(ACPI_POWER_FILE_STATUS,
518 acpi_device_dir(device));
519 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
520 acpi_device_dir(device) = NULL;
521 }
522
Patrick Mocheld550d982006-06-27 00:41:40 -0400523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/* --------------------------------------------------------------------------
527 Driver Interface
528 -------------------------------------------------------------------------- */
529
Len Brown4be44fc2005-08-05 00:44:28 -0400530static int acpi_power_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Len Brown4be44fc2005-08-05 00:44:28 -0400532 int result = 0;
533 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400535 union acpi_object acpi_object;
536 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
543 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400544 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 memset(resource, 0, sizeof(struct acpi_power_resource));
546
547 resource->handle = device->handle;
548 strcpy(resource->name, device->pnp.bus_id);
549 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
550 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
551 acpi_driver_data(device) = resource;
552
553 /* Evalute the object to get the system level and resource order. */
554 status = acpi_evaluate_object(resource->handle, NULL, NULL, &buffer);
555 if (ACPI_FAILURE(status)) {
556 result = -ENODEV;
557 goto end;
558 }
559 resource->system_level = acpi_object.power_resource.system_level;
560 resource->order = acpi_object.power_resource.resource_order;
561
562 result = acpi_power_get_state(resource);
563 if (result)
564 goto end;
565
566 switch (resource->state) {
567 case ACPI_POWER_RESOURCE_STATE_ON:
568 device->power.state = ACPI_STATE_D0;
569 break;
570 case ACPI_POWER_RESOURCE_STATE_OFF:
571 device->power.state = ACPI_STATE_D3;
572 break;
573 default:
574 device->power.state = ACPI_STATE_UNKNOWN;
575 break;
576 }
577
578 result = acpi_power_add_fs(device);
579 if (result)
580 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Len Brown4be44fc2005-08-05 00:44:28 -0400582 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
583 acpi_device_bid(device), resource->state ? "on" : "off");
584
585 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (result)
587 kfree(resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400588
Patrick Mocheld550d982006-06-27 00:41:40 -0400589 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Len Brown4be44fc2005-08-05 00:44:28 -0400592static int acpi_power_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct acpi_power_resource *resource = NULL;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Len Brown4be44fc2005-08-05 00:44:28 -0400600 resource = (struct acpi_power_resource *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 acpi_power_remove_fs(device);
603
604 kfree(resource);
605
Patrick Mocheld550d982006-06-27 00:41:40 -0400606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Len Brown4be44fc2005-08-05 00:44:28 -0400609static int __init acpi_power_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Len Brown4be44fc2005-08-05 00:44:28 -0400611 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 INIT_LIST_HEAD(&acpi_power_resource_list);
618
619 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
620 if (!acpi_power_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 result = acpi_bus_register_driver(&acpi_power_driver);
624 if (result < 0) {
625 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632subsys_initcall(acpi_power_init);