89111cd28ed86d0ca441ccba2769af5771c614a2
[cascardo/linux.git] / drivers / acpi / power.c
1 /*
2  *  acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 /*
27  * ACPI power-managed devices may be controlled in two ways:
28  * 1. via "Device Specific (D-State) Control"
29  * 2. via "Power Resource Control".
30  * This module is used to manage devices relying on Power Resource Control.
31  * 
32  * An ACPI "power resource object" describes a software controllable power
33  * plane, clock plane, or other resource used by a power managed device.
34  * A device may rely on multiple power resources, and a power resource
35  * may be shared by multiple devices.
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/types.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
46
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
57
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);
64
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);
69
70 static struct acpi_device_id power_device_ids[] = {
71         {ACPI_POWER_HID, 0},
72         {"", 0},
73 };
74 MODULE_DEVICE_TABLE(acpi, power_device_ids);
75
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,
84                 },
85 };
86
87 struct acpi_power_reference {
88         struct list_head node;
89         struct acpi_device *device;
90 };
91
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;
99 };
100
101 static struct list_head acpi_power_resource_list;
102
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,
109 };
110
111 /* --------------------------------------------------------------------------
112                              Power Resource Management
113    -------------------------------------------------------------------------- */
114
115 static int
116 acpi_power_get_context(acpi_handle handle,
117                        struct acpi_power_resource **resource)
118 {
119         int result = 0;
120         struct acpi_device *device = NULL;
121
122
123         if (!resource)
124                 return -ENODEV;
125
126         result = acpi_bus_get_device(handle, &device);
127         if (result) {
128                 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
129                 return result;
130         }
131
132         *resource = acpi_driver_data(device);
133         if (!*resource)
134                 return -ENODEV;
135
136         return 0;
137 }
138
139 static int acpi_power_get_state(acpi_handle handle, int *state)
140 {
141         acpi_status status = AE_OK;
142         unsigned long long sta = 0;
143
144
145         if (!handle || !state)
146                 return -EINVAL;
147
148         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
149         if (ACPI_FAILURE(status))
150                 return -ENODEV;
151
152         *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
153                               ACPI_POWER_RESOURCE_STATE_OFF;
154
155         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
156                           acpi_ut_get_node_name(handle),
157                                 *state ? "on" : "off"));
158
159         return 0;
160 }
161
162 static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
163 {
164         int result = 0, state1;
165         u32 i = 0;
166
167
168         if (!list || !state)
169                 return -EINVAL;
170
171         /* The state of the list is 'on' IFF all resources are 'on'. */
172         /* */
173
174         for (i = 0; i < list->count; i++) {
175                 /*
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.
179                  */
180                 result = acpi_power_get_state(list->handles[i], &state1);
181                 if (result)
182                         return result;
183
184                 *state = state1;
185
186                 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
187                         break;
188         }
189
190         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
191                           *state ? "on" : "off"));
192
193         return result;
194 }
195
196 static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
197 {
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;
204
205
206         result = acpi_power_get_context(handle, &resource);
207         if (result)
208                 return result;
209
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;
218                 }
219         }
220
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;
228                 }
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));
233         }
234         mutex_unlock(&resource->resource_lock);
235
236         status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
237         if (ACPI_FAILURE(status))
238                 return -ENODEV;
239
240         if (!acpi_power_nocheck) {
241                 /*
242                  * If acpi_power_nocheck is set, it is unnecessary to check
243                  * the power state after power transition.
244                  */
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;
251         }
252         /* Update the power resource's _device_ power state */
253         resource->device->power.state = ACPI_STATE_D0;
254
255         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
256                           resource->name));
257         return 0;
258 }
259
260 static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
261 {
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;
267
268
269         result = acpi_power_get_context(handle, &resource);
270         if (result)
271                 return result;
272
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;
282                 }
283         }
284
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;
290         }
291         mutex_unlock(&resource->resource_lock);
292
293         status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
294         if (ACPI_FAILURE(status))
295                 return -ENODEV;
296
297         if (!acpi_power_nocheck) {
298                 /*
299                  * If acpi_power_nocheck is set, it is unnecessary to check
300                  * the power state after power transition.
301                  */
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;
307         }
308
309         /* Update the power resource's _device_ power state */
310         resource->device->power.state = ACPI_STATE_D3;
311
312         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
313                           resource->name));
314
315         return 0;
316 }
317
318 /**
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.
325  *
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.
329  *
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
334  */
335 int acpi_device_sleep_wake(struct acpi_device *dev,
336                            int enable, int sleep_state, int dev_state)
337 {
338         union acpi_object in_arg[3];
339         struct acpi_object_list arg_list = { 3, in_arg };
340         acpi_status status = AE_OK;
341
342         /*
343          * Try to execute _DSW first.
344          *
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.
352          */
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;
366         }
367
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;
376         }
377
378         return 0;
379 }
380
381 /*
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
386  */
387 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
388 {
389         int i, err;
390
391         if (!dev || !dev->wakeup.flags.valid)
392                 return -EINVAL;
393
394         /*
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
397          */
398         if (dev->wakeup.flags.prepared)
399                 return 0;
400
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;
408                 }
409         }
410
411         /*
412          * Passing 3 as the third argument below means the device may be placed
413          * in arbitrary power state afterwards.
414          */
415         err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
416         if (!err)
417                 dev->wakeup.flags.prepared = 1;
418
419         return err;
420 }
421
422 /*
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
427  */
428 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
429 {
430         int i, ret;
431
432         if (!dev || !dev->wakeup.flags.valid)
433                 return -EINVAL;
434
435         /*
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
438          */
439         if (!dev->wakeup.flags.prepared)
440                 return 0;
441
442         dev->wakeup.flags.prepared = 0;
443
444         ret = acpi_device_sleep_wake(dev, 0, 0, 0);
445         if (ret)
446                 return ret;
447
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;
455                 }
456         }
457
458         return ret;
459 }
460
461 /* --------------------------------------------------------------------------
462                              Device Power Management
463    -------------------------------------------------------------------------- */
464
465 int acpi_power_get_inferred_state(struct acpi_device *device)
466 {
467         int result = 0;
468         struct acpi_handle_list *list = NULL;
469         int list_state = 0;
470         int i = 0;
471
472
473         if (!device)
474                 return -EINVAL;
475
476         device->power.state = ACPI_STATE_UNKNOWN;
477
478         /*
479          * We know a device's inferred power state when all the resources
480          * required for a given D-state are 'on'.
481          */
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;
486
487                 result = acpi_power_get_list_state(list, &list_state);
488                 if (result)
489                         return result;
490
491                 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
492                         device->power.state = i;
493                         return 0;
494                 }
495         }
496
497         device->power.state = ACPI_STATE_D3;
498
499         return 0;
500 }
501
502 int acpi_power_transition(struct acpi_device *device, int state)
503 {
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;
508
509
510         if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
511                 return -EINVAL;
512
513         if ((device->power.state < ACPI_STATE_D0)
514             || (device->power.state > ACPI_STATE_D3))
515                 return -ENODEV;
516
517         cl = &device->power.states[device->power.state].resources;
518         tl = &device->power.states[state].resources;
519
520         /* TBD: Resources must be ordered. */
521
522         /*
523          * First we reference all power resources required in the target list
524          * (e.g. so the device doesn't lose power while transitioning).
525          */
526         for (i = 0; i < tl->count; i++) {
527                 result = acpi_power_on(tl->handles[i], device);
528                 if (result)
529                         goto end;
530         }
531
532         if (device->power.state == state) {
533                 goto end;
534         }
535
536         /*
537          * Then we dereference all power resources used in the current list.
538          */
539         for (i = 0; i < cl->count; i++) {
540                 result = acpi_power_off_device(cl->handles[i], device);
541                 if (result)
542                         goto end;
543         }
544
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;
551         }
552
553         return result;
554 }
555
556 /* --------------------------------------------------------------------------
557                               FS Interface (/proc)
558    -------------------------------------------------------------------------- */
559
560 static struct proc_dir_entry *acpi_power_dir;
561
562 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
563 {
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;
569
570
571         resource = seq->private;
572
573         if (!resource)
574                 goto end;
575
576         result = acpi_power_get_state(resource->device->handle, &state);
577         if (result)
578                 goto end;
579
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;
591         }
592
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++;
597         }
598         mutex_unlock(&resource->resource_lock);
599
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);
605
606       end:
607         return 0;
608 }
609
610 static int acpi_power_open_fs(struct inode *inode, struct file *file)
611 {
612         return single_open(file, acpi_power_seq_show, PDE(inode)->data);
613 }
614
615 static int acpi_power_add_fs(struct acpi_device *device)
616 {
617         struct proc_dir_entry *entry = NULL;
618
619
620         if (!device)
621                 return -EINVAL;
622
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;
628         }
629
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;
637 }
638
639 static int acpi_power_remove_fs(struct acpi_device *device)
640 {
641
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;
647         }
648
649         return 0;
650 }
651
652 /* --------------------------------------------------------------------------
653                                 Driver Interface
654    -------------------------------------------------------------------------- */
655
656 static int acpi_power_add(struct acpi_device *device)
657 {
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 };
663
664
665         if (!device)
666                 return -EINVAL;
667
668         resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
669         if (!resource)
670                 return -ENOMEM;
671
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;
679
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;
685         }
686         resource->system_level = acpi_object.power_resource.system_level;
687         resource->order = acpi_object.power_resource.resource_order;
688
689         result = acpi_power_get_state(device->handle, &state);
690         if (result)
691                 goto end;
692
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;
703         }
704
705         result = acpi_power_add_fs(device);
706         if (result)
707                 goto end;
708
709         printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
710                acpi_device_bid(device), state ? "on" : "off");
711
712       end:
713         if (result)
714                 kfree(resource);
715
716         return result;
717 }
718
719 static int acpi_power_remove(struct acpi_device *device, int type)
720 {
721         struct acpi_power_resource *resource = NULL;
722         struct list_head *node, *next;
723
724
725         if (!device || !acpi_driver_data(device))
726                 return -EINVAL;
727
728         resource = acpi_driver_data(device);
729
730         acpi_power_remove_fs(device);
731
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);
737         }
738         mutex_unlock(&resource->resource_lock);
739
740         kfree(resource);
741
742         return 0;
743 }
744
745 static int acpi_power_resume(struct acpi_device *device)
746 {
747         int result = 0, state;
748         struct acpi_power_resource *resource = NULL;
749         struct acpi_power_reference *ref;
750
751         if (!device || !acpi_driver_data(device))
752                 return -EINVAL;
753
754         resource = acpi_driver_data(device);
755
756         result = acpi_power_get_state(device->handle, &state);
757         if (result)
758                 return result;
759
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;
767         }
768
769         mutex_unlock(&resource->resource_lock);
770         return 0;
771 }
772
773 static int __init acpi_power_init(void)
774 {
775         int result = 0;
776
777
778         if (acpi_disabled)
779                 return 0;
780
781         INIT_LIST_HEAD(&acpi_power_resource_list);
782
783         acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
784         if (!acpi_power_dir)
785                 return -ENODEV;
786
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;
791         }
792
793         return 0;
794 }
795
796 subsys_initcall(acpi_power_init);