blob: 62a5595ed8bc7551e7951b12dabb283662633b7f [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
101 ACPI_FUNCTION_TRACE("acpi_power_get_context");
102
103 if (!resource)
104 return_VALUE(-ENODEV);
105
106 result = acpi_bus_get_device(handle, &device);
107 if (result) {
108 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error getting context [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400109 handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return_VALUE(result);
111 }
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113 *resource = (struct acpi_power_resource *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!resource)
115 return_VALUE(-ENODEV);
116
117 return_VALUE(0);
118}
119
Len Brown4be44fc2005-08-05 00:44:28 -0400120static int acpi_power_get_state(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Len Brown4be44fc2005-08-05 00:44:28 -0400122 acpi_status status = AE_OK;
123 unsigned long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 ACPI_FUNCTION_TRACE("acpi_power_get_state");
126
127 if (!resource)
128 return_VALUE(-EINVAL);
129
130 status = acpi_evaluate_integer(resource->handle, "_STA", NULL, &sta);
131 if (ACPI_FAILURE(status))
132 return_VALUE(-ENODEV);
133
134 if (sta & 0x01)
135 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
136 else
137 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
138
139 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400140 resource->name, resource->state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return_VALUE(0);
143}
144
Len Brown4be44fc2005-08-05 00:44:28 -0400145static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Len Brown4be44fc2005-08-05 00:44:28 -0400147 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400149 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 ACPI_FUNCTION_TRACE("acpi_power_get_list_state");
152
153 if (!list || !state)
154 return_VALUE(-EINVAL);
155
156 /* The state of the list is 'on' IFF all resources are 'on'. */
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158 for (i = 0; i < list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 result = acpi_power_get_context(list->handles[i], &resource);
160 if (result)
161 return_VALUE(result);
162 result = acpi_power_get_state(resource);
163 if (result)
164 return_VALUE(result);
165
166 *state = resource->state;
167
168 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
169 break;
170 }
171
172 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400173 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return_VALUE(result);
176}
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Len Brown4be44fc2005-08-05 00:44:28 -0400180 int result = 0;
181 acpi_status status = AE_OK;
182 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct acpi_power_resource *resource = NULL;
184
185 ACPI_FUNCTION_TRACE("acpi_power_on");
186
187 result = acpi_power_get_context(handle, &resource);
188 if (result)
189 return_VALUE(result);
190
191 resource->references++;
192
Len Brown4be44fc2005-08-05 00:44:28 -0400193 if ((resource->references > 1)
194 || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400196 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return_VALUE(0);
198 }
199
200 status = acpi_evaluate_object(resource->handle, "_ON", NULL, NULL);
201 if (ACPI_FAILURE(status))
202 return_VALUE(-ENODEV);
203
204 result = acpi_power_get_state(resource);
205 if (result)
206 return_VALUE(result);
207 if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
208 return_VALUE(-ENOEXEC);
209
210 /* Update the power resource's _device_ power state */
211 result = acpi_bus_get_device(resource->handle, &device);
212 if (result)
213 return_VALUE(result);
214 device->power.state = ACPI_STATE_D0;
215
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400217 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 return_VALUE(0);
220}
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222static int acpi_power_off_device(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Len Brown4be44fc2005-08-05 00:44:28 -0400224 int result = 0;
225 acpi_status status = AE_OK;
226 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct acpi_power_resource *resource = NULL;
228
229 ACPI_FUNCTION_TRACE("acpi_power_off_device");
230
231 result = acpi_power_get_context(handle, &resource);
232 if (result)
233 return_VALUE(result);
234
235 if (resource->references)
236 resource->references--;
237
238 if (resource->references) {
Len Brown4be44fc2005-08-05 00:44:28 -0400239 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
240 "Resource [%s] is still in use, dereferencing\n",
241 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return_VALUE(0);
243 }
244
245 if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
246 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400247 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return_VALUE(0);
249 }
250
251 status = acpi_evaluate_object(resource->handle, "_OFF", NULL, NULL);
252 if (ACPI_FAILURE(status))
253 return_VALUE(-ENODEV);
254
255 result = acpi_power_get_state(resource);
256 if (result)
257 return_VALUE(result);
258 if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
259 return_VALUE(-ENOEXEC);
260
261 /* Update the power resource's _device_ power state */
262 result = acpi_bus_get_device(resource->handle, &device);
263 if (result)
264 return_VALUE(result);
265 device->power.state = ACPI_STATE_D3;
266
267 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400268 resource->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return_VALUE(0);
271}
272
273/*
274 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
275 * 1. Power on the power resources required for the wakeup device
276 * 2. Enable _PSW (power state wake) for the device if present
277 */
Len Brown4be44fc2005-08-05 00:44:28 -0400278int acpi_enable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Len Brown4be44fc2005-08-05 00:44:28 -0400280 union acpi_object arg = { ACPI_TYPE_INTEGER };
281 struct acpi_object_list arg_list = { 1, &arg };
282 acpi_status status = AE_OK;
283 int i;
284 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_power");
287 if (!dev || !dev->wakeup.flags.valid)
288 return_VALUE(-1);
289
290 arg.integer.value = 1;
291 /* Open power resource */
292 for (i = 0; i < dev->wakeup.resources.count; i++) {
293 ret = acpi_power_on(dev->wakeup.resources.handles[i]);
294 if (ret) {
Len Brown4be44fc2005-08-05 00:44:28 -0400295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
296 "Error transition power state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dev->wakeup.flags.valid = 0;
298 return_VALUE(-1);
299 }
300 }
301
302 /* Execute PSW */
303 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
304 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
305 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluate _PSW\n"));
306 dev->wakeup.flags.valid = 0;
307 ret = -1;
308 }
309
310 return_VALUE(ret);
311}
312
313/*
314 * Shutdown a wakeup device, counterpart of above method
315 * 1. Disable _PSW (power state wake)
316 * 2. Shutdown down the power resources
317 */
Len Brown4be44fc2005-08-05 00:44:28 -0400318int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Len Brown4be44fc2005-08-05 00:44:28 -0400320 union acpi_object arg = { ACPI_TYPE_INTEGER };
321 struct acpi_object_list arg_list = { 1, &arg };
322 acpi_status status = AE_OK;
323 int i;
324 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device_power");
327
328 if (!dev || !dev->wakeup.flags.valid)
329 return_VALUE(-1);
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 arg.integer.value = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Execute PSW */
333 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
334 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
335 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluate _PSW\n"));
336 dev->wakeup.flags.valid = 0;
337 return_VALUE(-1);
338 }
339
340 /* Close power resource */
341 for (i = 0; i < dev->wakeup.resources.count; i++) {
342 ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
343 if (ret) {
Len Brown4be44fc2005-08-05 00:44:28 -0400344 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
345 "Error transition power state\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev->wakeup.flags.valid = 0;
347 return_VALUE(-1);
348 }
349 }
350
351 return_VALUE(ret);
352}
353
354/* --------------------------------------------------------------------------
355 Device Power Management
356 -------------------------------------------------------------------------- */
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358int acpi_power_get_inferred_state(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Len Brown4be44fc2005-08-05 00:44:28 -0400360 int result = 0;
361 struct acpi_handle_list *list = NULL;
362 int list_state = 0;
363 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 ACPI_FUNCTION_TRACE("acpi_power_get_inferred_state");
366
367 if (!device)
368 return_VALUE(-EINVAL);
369
370 device->power.state = ACPI_STATE_UNKNOWN;
371
372 /*
373 * We know a device's inferred power state when all the resources
374 * required for a given D-state are 'on'.
375 */
Len Brown4be44fc2005-08-05 00:44:28 -0400376 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 list = &device->power.states[i].resources;
378 if (list->count < 1)
379 continue;
380
381 result = acpi_power_get_list_state(list, &list_state);
382 if (result)
383 return_VALUE(result);
384
385 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
386 device->power.state = i;
387 return_VALUE(0);
388 }
389 }
390
391 device->power.state = ACPI_STATE_D3;
392
393 return_VALUE(0);
394}
395
Len Brown4be44fc2005-08-05 00:44:28 -0400396int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Len Brown4be44fc2005-08-05 00:44:28 -0400398 int result = 0;
399 struct acpi_handle_list *cl = NULL; /* Current Resources */
400 struct acpi_handle_list *tl = NULL; /* Target Resources */
401 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 ACPI_FUNCTION_TRACE("acpi_power_transition");
404
405 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
406 return_VALUE(-EINVAL);
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408 if ((device->power.state < ACPI_STATE_D0)
409 || (device->power.state > ACPI_STATE_D3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return_VALUE(-ENODEV);
411
412 cl = &device->power.states[device->power.state].resources;
413 tl = &device->power.states[state].resources;
414
415 device->power.state = ACPI_STATE_UNKNOWN;
416
417 if (!cl->count && !tl->count) {
418 result = -ENODEV;
419 goto end;
420 }
421
422 /* TBD: Resources must be ordered. */
423
424 /*
425 * First we reference all power resources required in the target list
426 * (e.g. so the device doesn't lose power while transitioning).
427 */
Len Brown4be44fc2005-08-05 00:44:28 -0400428 for (i = 0; i < tl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 result = acpi_power_on(tl->handles[i]);
430 if (result)
431 goto end;
432 }
433
434 /*
435 * Then we dereference all power resources used in the current list.
436 */
Len Brown4be44fc2005-08-05 00:44:28 -0400437 for (i = 0; i < cl->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 result = acpi_power_off_device(cl->handles[i]);
439 if (result)
440 goto end;
441 }
442
443 /* We shouldn't change the state till all above operations succeed */
444 device->power.state = state;
Len Brown4be44fc2005-08-05 00:44:28 -0400445 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (result)
Len Brown4be44fc2005-08-05 00:44:28 -0400447 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
448 "Error transitioning device [%s] to D%d\n",
449 device->pnp.bus_id, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return_VALUE(result);
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* --------------------------------------------------------------------------
455 FS Interface (/proc)
456 -------------------------------------------------------------------------- */
457
Len Brown4be44fc2005-08-05 00:44:28 -0400458static struct proc_dir_entry *acpi_power_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460static int acpi_power_seq_show(struct seq_file *seq, void *offset)
461{
462 struct acpi_power_resource *resource = NULL;
463
464 ACPI_FUNCTION_TRACE("acpi_power_seq_show");
465
466 resource = (struct acpi_power_resource *)seq->private;
467
468 if (!resource)
469 goto end;
470
471 seq_puts(seq, "state: ");
472 switch (resource->state) {
473 case ACPI_POWER_RESOURCE_STATE_ON:
474 seq_puts(seq, "on\n");
475 break;
476 case ACPI_POWER_RESOURCE_STATE_OFF:
477 seq_puts(seq, "off\n");
478 break;
479 default:
480 seq_puts(seq, "unknown\n");
481 break;
482 }
483
484 seq_printf(seq, "system level: S%d\n"
Len Brown4be44fc2005-08-05 00:44:28 -0400485 "order: %d\n"
486 "reference count: %d\n",
487 resource->system_level,
488 resource->order, resource->references);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return_VALUE(0);
492}
493
494static int acpi_power_open_fs(struct inode *inode, struct file *file)
495{
496 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
497}
498
Len Brown4be44fc2005-08-05 00:44:28 -0400499static int acpi_power_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Len Brown4be44fc2005-08-05 00:44:28 -0400501 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 ACPI_FUNCTION_TRACE("acpi_power_add_fs");
504
505 if (!device)
506 return_VALUE(-EINVAL);
507
508 if (!acpi_device_dir(device)) {
509 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400510 acpi_power_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!acpi_device_dir(device))
512 return_VALUE(-ENODEV);
513 }
514
515 /* 'status' [R] */
516 entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400517 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!entry)
519 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
Len Brown4be44fc2005-08-05 00:44:28 -0400520 "Unable to create '%s' fs entry\n",
521 ACPI_POWER_FILE_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 else {
523 entry->proc_fops = &acpi_power_fops;
524 entry->data = acpi_driver_data(device);
525 }
526
527 return_VALUE(0);
528}
529
Len Brown4be44fc2005-08-05 00:44:28 -0400530static int acpi_power_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 ACPI_FUNCTION_TRACE("acpi_power_remove_fs");
533
534 if (acpi_device_dir(device)) {
535 remove_proc_entry(ACPI_POWER_FILE_STATUS,
536 acpi_device_dir(device));
537 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
538 acpi_device_dir(device) = NULL;
539 }
540
541 return_VALUE(0);
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/* --------------------------------------------------------------------------
545 Driver Interface
546 -------------------------------------------------------------------------- */
547
Len Brown4be44fc2005-08-05 00:44:28 -0400548static int acpi_power_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Len Brown4be44fc2005-08-05 00:44:28 -0400550 int result = 0;
551 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400553 union acpi_object acpi_object;
554 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 ACPI_FUNCTION_TRACE("acpi_power_add");
557
558 if (!device)
559 return_VALUE(-EINVAL);
560
561 resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
562 if (!resource)
563 return_VALUE(-ENOMEM);
564 memset(resource, 0, sizeof(struct acpi_power_resource));
565
566 resource->handle = device->handle;
567 strcpy(resource->name, device->pnp.bus_id);
568 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
569 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
570 acpi_driver_data(device) = resource;
571
572 /* Evalute the object to get the system level and resource order. */
573 status = acpi_evaluate_object(resource->handle, NULL, NULL, &buffer);
574 if (ACPI_FAILURE(status)) {
575 result = -ENODEV;
576 goto end;
577 }
578 resource->system_level = acpi_object.power_resource.system_level;
579 resource->order = acpi_object.power_resource.resource_order;
580
581 result = acpi_power_get_state(resource);
582 if (result)
583 goto end;
584
585 switch (resource->state) {
586 case ACPI_POWER_RESOURCE_STATE_ON:
587 device->power.state = ACPI_STATE_D0;
588 break;
589 case ACPI_POWER_RESOURCE_STATE_OFF:
590 device->power.state = ACPI_STATE_D3;
591 break;
592 default:
593 device->power.state = ACPI_STATE_UNKNOWN;
594 break;
595 }
596
597 result = acpi_power_add_fs(device);
598 if (result)
599 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Len Brown4be44fc2005-08-05 00:44:28 -0400601 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
602 acpi_device_bid(device), resource->state ? "on" : "off");
603
604 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (result)
606 kfree(resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return_VALUE(result);
609}
610
Len Brown4be44fc2005-08-05 00:44:28 -0400611static int acpi_power_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct acpi_power_resource *resource = NULL;
614
615 ACPI_FUNCTION_TRACE("acpi_power_remove");
616
617 if (!device || !acpi_driver_data(device))
618 return_VALUE(-EINVAL);
619
Len Brown4be44fc2005-08-05 00:44:28 -0400620 resource = (struct acpi_power_resource *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 acpi_power_remove_fs(device);
623
624 kfree(resource);
625
626 return_VALUE(0);
627}
628
Len Brown4be44fc2005-08-05 00:44:28 -0400629static int __init acpi_power_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Len Brown4be44fc2005-08-05 00:44:28 -0400631 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 ACPI_FUNCTION_TRACE("acpi_power_init");
634
635 if (acpi_disabled)
636 return_VALUE(0);
637
638 INIT_LIST_HEAD(&acpi_power_resource_list);
639
640 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
641 if (!acpi_power_dir)
642 return_VALUE(-ENODEV);
643
644 result = acpi_bus_register_driver(&acpi_power_driver);
645 if (result < 0) {
646 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
647 return_VALUE(-ENODEV);
648 }
649
650 return_VALUE(0);
651}
652
653subsys_initcall(acpi_power_init);