ACPI: fan: Delete the strict check in power transition
[linux-2.6/mini2440.git] / drivers / acpi / power.c
blob89111cd28ed86d0ca441ccba2769af5771c614a2
1 /*
2 * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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>
47 #define _COMPONENT ACPI_POWER_COMPONENT
48 ACPI_MODULE_NAME("power");
49 #define ACPI_POWER_COMPONENT 0x00800000
50 #define ACPI_POWER_CLASS "power_resource"
51 #define ACPI_POWER_DEVICE_NAME "Power Resource"
52 #define ACPI_POWER_FILE_INFO "info"
53 #define ACPI_POWER_FILE_STATUS "state"
54 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
55 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
56 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
58 #ifdef MODULE_PARAM_PREFIX
59 #undef MODULE_PARAM_PREFIX
60 #endif
61 #define MODULE_PARAM_PREFIX "acpi."
62 int acpi_power_nocheck;
63 module_param_named(power_nocheck, acpi_power_nocheck, bool, 000);
65 static int acpi_power_add(struct acpi_device *device);
66 static int acpi_power_remove(struct acpi_device *device, int type);
67 static int acpi_power_resume(struct acpi_device *device);
68 static int acpi_power_open_fs(struct inode *inode, struct file *file);
70 static struct acpi_device_id power_device_ids[] = {
71 {ACPI_POWER_HID, 0},
72 {"", 0},
74 MODULE_DEVICE_TABLE(acpi, power_device_ids);
76 static struct acpi_driver acpi_power_driver = {
77 .name = "power",
78 .class = ACPI_POWER_CLASS,
79 .ids = power_device_ids,
80 .ops = {
81 .add = acpi_power_add,
82 .remove = acpi_power_remove,
83 .resume = acpi_power_resume,
87 struct acpi_power_reference {
88 struct list_head node;
89 struct acpi_device *device;
92 struct acpi_power_resource {
93 struct acpi_device * device;
94 acpi_bus_id name;
95 u32 system_level;
96 u32 order;
97 struct mutex resource_lock;
98 struct list_head reference;
101 static struct list_head acpi_power_resource_list;
103 static const struct file_operations acpi_power_fops = {
104 .owner = THIS_MODULE,
105 .open = acpi_power_open_fs,
106 .read = seq_read,
107 .llseek = seq_lseek,
108 .release = single_release,
111 /* --------------------------------------------------------------------------
112 Power Resource Management
113 -------------------------------------------------------------------------- */
115 static int
116 acpi_power_get_context(acpi_handle handle,
117 struct acpi_power_resource **resource)
119 int result = 0;
120 struct acpi_device *device = NULL;
123 if (!resource)
124 return -ENODEV;
126 result = acpi_bus_get_device(handle, &device);
127 if (result) {
128 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
129 return result;
132 *resource = acpi_driver_data(device);
133 if (!*resource)
134 return -ENODEV;
136 return 0;
139 static int acpi_power_get_state(acpi_handle handle, int *state)
141 acpi_status status = AE_OK;
142 unsigned long long sta = 0;
145 if (!handle || !state)
146 return -EINVAL;
148 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
149 if (ACPI_FAILURE(status))
150 return -ENODEV;
152 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
153 ACPI_POWER_RESOURCE_STATE_OFF;
155 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
156 acpi_ut_get_node_name(handle),
157 *state ? "on" : "off"));
159 return 0;
162 static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
164 int result = 0, state1;
165 u32 i = 0;
168 if (!list || !state)
169 return -EINVAL;
171 /* The state of the list is 'on' IFF all resources are 'on'. */
172 /* */
174 for (i = 0; i < list->count; i++) {
176 * The state of the power resource can be obtained by
177 * using the ACPI handle. In such case it is unnecessary to
178 * get the Power resource first and then get its state again.
180 result = acpi_power_get_state(list->handles[i], &state1);
181 if (result)
182 return result;
184 *state = state1;
186 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
187 break;
190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
191 *state ? "on" : "off"));
193 return result;
196 static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
198 int result = 0, state;
199 int found = 0;
200 acpi_status status = AE_OK;
201 struct acpi_power_resource *resource = NULL;
202 struct list_head *node, *next;
203 struct acpi_power_reference *ref;
206 result = acpi_power_get_context(handle, &resource);
207 if (result)
208 return result;
210 mutex_lock(&resource->resource_lock);
211 list_for_each_safe(node, next, &resource->reference) {
212 ref = container_of(node, struct acpi_power_reference, node);
213 if (dev->handle == ref->device->handle) {
214 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
215 dev->pnp.bus_id, resource->name));
216 found = 1;
217 break;
221 if (!found) {
222 ref = kmalloc(sizeof (struct acpi_power_reference),
223 irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
224 if (!ref) {
225 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
226 mutex_unlock(&resource->resource_lock);
227 return -ENOMEM;
229 list_add_tail(&ref->node, &resource->reference);
230 ref->device = dev;
231 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
232 dev->pnp.bus_id, resource->name));
234 mutex_unlock(&resource->resource_lock);
236 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
237 if (ACPI_FAILURE(status))
238 return -ENODEV;
240 if (!acpi_power_nocheck) {
242 * If acpi_power_nocheck is set, it is unnecessary to check
243 * the power state after power transition.
245 result = acpi_power_get_state(resource->device->handle,
246 &state);
247 if (result)
248 return result;
249 if (state != ACPI_POWER_RESOURCE_STATE_ON)
250 return -ENOEXEC;
252 /* Update the power resource's _device_ power state */
253 resource->device->power.state = ACPI_STATE_D0;
255 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
256 resource->name));
257 return 0;
260 static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
262 int result = 0, state;
263 acpi_status status = AE_OK;
264 struct acpi_power_resource *resource = NULL;
265 struct list_head *node, *next;
266 struct acpi_power_reference *ref;
269 result = acpi_power_get_context(handle, &resource);
270 if (result)
271 return result;
273 mutex_lock(&resource->resource_lock);
274 list_for_each_safe(node, next, &resource->reference) {
275 ref = container_of(node, struct acpi_power_reference, node);
276 if (dev->handle == ref->device->handle) {
277 list_del(&ref->node);
278 kfree(ref);
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
280 dev->pnp.bus_id, resource->name));
281 break;
285 if (!list_empty(&resource->reference)) {
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
287 resource->name));
288 mutex_unlock(&resource->resource_lock);
289 return 0;
291 mutex_unlock(&resource->resource_lock);
293 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
294 if (ACPI_FAILURE(status))
295 return -ENODEV;
297 if (!acpi_power_nocheck) {
299 * If acpi_power_nocheck is set, it is unnecessary to check
300 * the power state after power transition.
302 result = acpi_power_get_state(handle, &state);
303 if (result)
304 return result;
305 if (state != ACPI_POWER_RESOURCE_STATE_OFF)
306 return -ENOEXEC;
309 /* Update the power resource's _device_ power state */
310 resource->device->power.state = ACPI_STATE_D3;
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
313 resource->name));
315 return 0;
319 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
320 * ACPI 3.0) _PSW (Power State Wake)
321 * @dev: Device to handle.
322 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
323 * @sleep_state: Target sleep state of the system.
324 * @dev_state: Target power state of the device.
326 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
327 * State Wake) for the device, if present. On failure reset the device's
328 * wakeup.flags.valid flag.
330 * RETURN VALUE:
331 * 0 if either _DSW or _PSW has been successfully executed
332 * 0 if neither _DSW nor _PSW has been found
333 * -ENODEV if the execution of either _DSW or _PSW has failed
335 int acpi_device_sleep_wake(struct acpi_device *dev,
336 int enable, int sleep_state, int dev_state)
338 union acpi_object in_arg[3];
339 struct acpi_object_list arg_list = { 3, in_arg };
340 acpi_status status = AE_OK;
343 * Try to execute _DSW first.
345 * Three agruments are needed for the _DSW object:
346 * Argument 0: enable/disable the wake capabilities
347 * Argument 1: target system state
348 * Argument 2: target device state
349 * When _DSW object is called to disable the wake capabilities, maybe
350 * the first argument is filled. The values of the other two agruments
351 * are meaningless.
353 in_arg[0].type = ACPI_TYPE_INTEGER;
354 in_arg[0].integer.value = enable;
355 in_arg[1].type = ACPI_TYPE_INTEGER;
356 in_arg[1].integer.value = sleep_state;
357 in_arg[2].type = ACPI_TYPE_INTEGER;
358 in_arg[2].integer.value = dev_state;
359 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
360 if (ACPI_SUCCESS(status)) {
361 return 0;
362 } else if (status != AE_NOT_FOUND) {
363 printk(KERN_ERR PREFIX "_DSW execution failed\n");
364 dev->wakeup.flags.valid = 0;
365 return -ENODEV;
368 /* Execute _PSW */
369 arg_list.count = 1;
370 in_arg[0].integer.value = enable;
371 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
372 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
373 printk(KERN_ERR PREFIX "_PSW execution failed\n");
374 dev->wakeup.flags.valid = 0;
375 return -ENODEV;
378 return 0;
382 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
383 * 1. Power on the power resources required for the wakeup device
384 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
385 * State Wake) for the device, if present
387 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
389 int i, err;
391 if (!dev || !dev->wakeup.flags.valid)
392 return -EINVAL;
395 * Do not execute the code below twice in a row without calling
396 * acpi_disable_wakeup_device_power() in between for the same device
398 if (dev->wakeup.flags.prepared)
399 return 0;
401 /* Open power resource */
402 for (i = 0; i < dev->wakeup.resources.count; i++) {
403 int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
404 if (ret) {
405 printk(KERN_ERR PREFIX "Transition power state\n");
406 dev->wakeup.flags.valid = 0;
407 return -ENODEV;
412 * Passing 3 as the third argument below means the device may be placed
413 * in arbitrary power state afterwards.
415 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
416 if (!err)
417 dev->wakeup.flags.prepared = 1;
419 return err;
423 * Shutdown a wakeup device, counterpart of above method
424 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
425 * State Wake) for the device, if present
426 * 2. Shutdown down the power resources
428 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
430 int i, ret;
432 if (!dev || !dev->wakeup.flags.valid)
433 return -EINVAL;
436 * Do not execute the code below twice in a row without calling
437 * acpi_enable_wakeup_device_power() in between for the same device
439 if (!dev->wakeup.flags.prepared)
440 return 0;
442 dev->wakeup.flags.prepared = 0;
444 ret = acpi_device_sleep_wake(dev, 0, 0, 0);
445 if (ret)
446 return ret;
448 /* Close power resource */
449 for (i = 0; i < dev->wakeup.resources.count; i++) {
450 ret = acpi_power_off_device(dev->wakeup.resources.handles[i], dev);
451 if (ret) {
452 printk(KERN_ERR PREFIX "Transition power state\n");
453 dev->wakeup.flags.valid = 0;
454 return -ENODEV;
458 return ret;
461 /* --------------------------------------------------------------------------
462 Device Power Management
463 -------------------------------------------------------------------------- */
465 int acpi_power_get_inferred_state(struct acpi_device *device)
467 int result = 0;
468 struct acpi_handle_list *list = NULL;
469 int list_state = 0;
470 int i = 0;
473 if (!device)
474 return -EINVAL;
476 device->power.state = ACPI_STATE_UNKNOWN;
479 * We know a device's inferred power state when all the resources
480 * required for a given D-state are 'on'.
482 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
483 list = &device->power.states[i].resources;
484 if (list->count < 1)
485 continue;
487 result = acpi_power_get_list_state(list, &list_state);
488 if (result)
489 return result;
491 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
492 device->power.state = i;
493 return 0;
497 device->power.state = ACPI_STATE_D3;
499 return 0;
502 int acpi_power_transition(struct acpi_device *device, int state)
504 int result = 0;
505 struct acpi_handle_list *cl = NULL; /* Current Resources */
506 struct acpi_handle_list *tl = NULL; /* Target Resources */
507 int i = 0;
510 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
511 return -EINVAL;
513 if ((device->power.state < ACPI_STATE_D0)
514 || (device->power.state > ACPI_STATE_D3))
515 return -ENODEV;
517 cl = &device->power.states[device->power.state].resources;
518 tl = &device->power.states[state].resources;
520 /* TBD: Resources must be ordered. */
523 * First we reference all power resources required in the target list
524 * (e.g. so the device doesn't lose power while transitioning).
526 for (i = 0; i < tl->count; i++) {
527 result = acpi_power_on(tl->handles[i], device);
528 if (result)
529 goto end;
532 if (device->power.state == state) {
533 goto end;
537 * Then we dereference all power resources used in the current list.
539 for (i = 0; i < cl->count; i++) {
540 result = acpi_power_off_device(cl->handles[i], device);
541 if (result)
542 goto end;
545 end:
546 if (result)
547 device->power.state = ACPI_STATE_UNKNOWN;
548 else {
549 /* We shouldn't change the state till all above operations succeed */
550 device->power.state = state;
553 return result;
556 /* --------------------------------------------------------------------------
557 FS Interface (/proc)
558 -------------------------------------------------------------------------- */
560 static struct proc_dir_entry *acpi_power_dir;
562 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
564 int count = 0;
565 int result = 0, state;
566 struct acpi_power_resource *resource = NULL;
567 struct list_head *node, *next;
568 struct acpi_power_reference *ref;
571 resource = seq->private;
573 if (!resource)
574 goto end;
576 result = acpi_power_get_state(resource->device->handle, &state);
577 if (result)
578 goto end;
580 seq_puts(seq, "state: ");
581 switch (state) {
582 case ACPI_POWER_RESOURCE_STATE_ON:
583 seq_puts(seq, "on\n");
584 break;
585 case ACPI_POWER_RESOURCE_STATE_OFF:
586 seq_puts(seq, "off\n");
587 break;
588 default:
589 seq_puts(seq, "unknown\n");
590 break;
593 mutex_lock(&resource->resource_lock);
594 list_for_each_safe(node, next, &resource->reference) {
595 ref = container_of(node, struct acpi_power_reference, node);
596 count++;
598 mutex_unlock(&resource->resource_lock);
600 seq_printf(seq, "system level: S%d\n"
601 "order: %d\n"
602 "reference count: %d\n",
603 resource->system_level,
604 resource->order, count);
606 end:
607 return 0;
610 static int acpi_power_open_fs(struct inode *inode, struct file *file)
612 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
615 static int acpi_power_add_fs(struct acpi_device *device)
617 struct proc_dir_entry *entry = NULL;
620 if (!device)
621 return -EINVAL;
623 if (!acpi_device_dir(device)) {
624 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
625 acpi_power_dir);
626 if (!acpi_device_dir(device))
627 return -ENODEV;
630 /* 'status' [R] */
631 entry = proc_create_data(ACPI_POWER_FILE_STATUS,
632 S_IRUGO, acpi_device_dir(device),
633 &acpi_power_fops, acpi_driver_data(device));
634 if (!entry)
635 return -EIO;
636 return 0;
639 static int acpi_power_remove_fs(struct acpi_device *device)
642 if (acpi_device_dir(device)) {
643 remove_proc_entry(ACPI_POWER_FILE_STATUS,
644 acpi_device_dir(device));
645 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
646 acpi_device_dir(device) = NULL;
649 return 0;
652 /* --------------------------------------------------------------------------
653 Driver Interface
654 -------------------------------------------------------------------------- */
656 static int acpi_power_add(struct acpi_device *device)
658 int result = 0, state;
659 acpi_status status = AE_OK;
660 struct acpi_power_resource *resource = NULL;
661 union acpi_object acpi_object;
662 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
665 if (!device)
666 return -EINVAL;
668 resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
669 if (!resource)
670 return -ENOMEM;
672 resource->device = device;
673 mutex_init(&resource->resource_lock);
674 INIT_LIST_HEAD(&resource->reference);
675 strcpy(resource->name, device->pnp.bus_id);
676 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
677 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
678 device->driver_data = resource;
680 /* Evalute the object to get the system level and resource order. */
681 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
682 if (ACPI_FAILURE(status)) {
683 result = -ENODEV;
684 goto end;
686 resource->system_level = acpi_object.power_resource.system_level;
687 resource->order = acpi_object.power_resource.resource_order;
689 result = acpi_power_get_state(device->handle, &state);
690 if (result)
691 goto end;
693 switch (state) {
694 case ACPI_POWER_RESOURCE_STATE_ON:
695 device->power.state = ACPI_STATE_D0;
696 break;
697 case ACPI_POWER_RESOURCE_STATE_OFF:
698 device->power.state = ACPI_STATE_D3;
699 break;
700 default:
701 device->power.state = ACPI_STATE_UNKNOWN;
702 break;
705 result = acpi_power_add_fs(device);
706 if (result)
707 goto end;
709 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
710 acpi_device_bid(device), state ? "on" : "off");
712 end:
713 if (result)
714 kfree(resource);
716 return result;
719 static int acpi_power_remove(struct acpi_device *device, int type)
721 struct acpi_power_resource *resource = NULL;
722 struct list_head *node, *next;
725 if (!device || !acpi_driver_data(device))
726 return -EINVAL;
728 resource = acpi_driver_data(device);
730 acpi_power_remove_fs(device);
732 mutex_lock(&resource->resource_lock);
733 list_for_each_safe(node, next, &resource->reference) {
734 struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
735 list_del(&ref->node);
736 kfree(ref);
738 mutex_unlock(&resource->resource_lock);
740 kfree(resource);
742 return 0;
745 static int acpi_power_resume(struct acpi_device *device)
747 int result = 0, state;
748 struct acpi_power_resource *resource = NULL;
749 struct acpi_power_reference *ref;
751 if (!device || !acpi_driver_data(device))
752 return -EINVAL;
754 resource = acpi_driver_data(device);
756 result = acpi_power_get_state(device->handle, &state);
757 if (result)
758 return result;
760 mutex_lock(&resource->resource_lock);
761 if (state == ACPI_POWER_RESOURCE_STATE_OFF &&
762 !list_empty(&resource->reference)) {
763 ref = container_of(resource->reference.next, struct acpi_power_reference, node);
764 mutex_unlock(&resource->resource_lock);
765 result = acpi_power_on(device->handle, ref->device);
766 return result;
769 mutex_unlock(&resource->resource_lock);
770 return 0;
773 static int __init acpi_power_init(void)
775 int result = 0;
778 if (acpi_disabled)
779 return 0;
781 INIT_LIST_HEAD(&acpi_power_resource_list);
783 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
784 if (!acpi_power_dir)
785 return -ENODEV;
787 result = acpi_bus_register_driver(&acpi_power_driver);
788 if (result < 0) {
789 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
790 return -ENODEV;
793 return 0;
796 subsys_initcall(acpi_power_init);