ACPI: un-export ACPI_ERROR() -- use printk(KERN_ERR...)
[pandora-kernel.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("acpi_power")
49 #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
58 static int acpi_power_add(struct acpi_device *device);
59 static int acpi_power_remove(struct acpi_device *device, int type);
60 static int acpi_power_open_fs(struct inode *inode, struct file *file);
61
62 static struct acpi_driver acpi_power_driver = {
63         .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                 },
70 };
71
72 struct 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;
79 };
80
81 static struct list_head acpi_power_resource_list;
82
83 static struct file_operations acpi_power_fops = {
84         .open = acpi_power_open_fs,
85         .read = seq_read,
86         .llseek = seq_lseek,
87         .release = single_release,
88 };
89
90 /* --------------------------------------------------------------------------
91                              Power Resource Management
92    -------------------------------------------------------------------------- */
93
94 static int
95 acpi_power_get_context(acpi_handle handle,
96                        struct acpi_power_resource **resource)
97 {
98         int result = 0;
99         struct acpi_device *device = NULL;
100
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                 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
109                 return_VALUE(result);
110         }
111
112         *resource = (struct acpi_power_resource *)acpi_driver_data(device);
113         if (!resource)
114                 return_VALUE(-ENODEV);
115
116         return_VALUE(0);
117 }
118
119 static int acpi_power_get_state(struct acpi_power_resource *resource)
120 {
121         acpi_status status = AE_OK;
122         unsigned long sta = 0;
123
124         ACPI_FUNCTION_TRACE("acpi_power_get_state");
125
126         if (!resource)
127                 return_VALUE(-EINVAL);
128
129         status = acpi_evaluate_integer(resource->handle, "_STA", NULL, &sta);
130         if (ACPI_FAILURE(status))
131                 return_VALUE(-ENODEV);
132
133         if (sta & 0x01)
134                 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
135         else
136                 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
137
138         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
139                           resource->name, resource->state ? "on" : "off"));
140
141         return_VALUE(0);
142 }
143
144 static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
145 {
146         int result = 0;
147         struct acpi_power_resource *resource = NULL;
148         u32 i = 0;
149
150         ACPI_FUNCTION_TRACE("acpi_power_get_list_state");
151
152         if (!list || !state)
153                 return_VALUE(-EINVAL);
154
155         /* The state of the list is 'on' IFF all resources are 'on'. */
156
157         for (i = 0; i < list->count; i++) {
158                 result = acpi_power_get_context(list->handles[i], &resource);
159                 if (result)
160                         return_VALUE(result);
161                 result = acpi_power_get_state(resource);
162                 if (result)
163                         return_VALUE(result);
164
165                 *state = resource->state;
166
167                 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
168                         break;
169         }
170
171         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
172                           *state ? "on" : "off"));
173
174         return_VALUE(result);
175 }
176
177 static int acpi_power_on(acpi_handle handle)
178 {
179         int result = 0;
180         acpi_status status = AE_OK;
181         struct acpi_device *device = NULL;
182         struct acpi_power_resource *resource = NULL;
183
184         ACPI_FUNCTION_TRACE("acpi_power_on");
185
186         result = acpi_power_get_context(handle, &resource);
187         if (result)
188                 return_VALUE(result);
189
190         resource->references++;
191
192         if ((resource->references > 1)
193             || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
194                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
195                                   resource->name));
196                 return_VALUE(0);
197         }
198
199         status = acpi_evaluate_object(resource->handle, "_ON", NULL, NULL);
200         if (ACPI_FAILURE(status))
201                 return_VALUE(-ENODEV);
202
203         result = acpi_power_get_state(resource);
204         if (result)
205                 return_VALUE(result);
206         if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
207                 return_VALUE(-ENOEXEC);
208
209         /* Update the power resource's _device_ power state */
210         result = acpi_bus_get_device(resource->handle, &device);
211         if (result)
212                 return_VALUE(result);
213         device->power.state = ACPI_STATE_D0;
214
215         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
216                           resource->name));
217
218         return_VALUE(0);
219 }
220
221 static int acpi_power_off_device(acpi_handle handle)
222 {
223         int result = 0;
224         acpi_status status = AE_OK;
225         struct acpi_device *device = NULL;
226         struct acpi_power_resource *resource = NULL;
227
228         ACPI_FUNCTION_TRACE("acpi_power_off_device");
229
230         result = acpi_power_get_context(handle, &resource);
231         if (result)
232                 return_VALUE(result);
233
234         if (resource->references)
235                 resource->references--;
236
237         if (resource->references) {
238                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
239                                   "Resource [%s] is still in use, dereferencing\n",
240                                   device->pnp.bus_id));
241                 return_VALUE(0);
242         }
243
244         if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
245                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
246                                   device->pnp.bus_id));
247                 return_VALUE(0);
248         }
249
250         status = acpi_evaluate_object(resource->handle, "_OFF", NULL, NULL);
251         if (ACPI_FAILURE(status))
252                 return_VALUE(-ENODEV);
253
254         result = acpi_power_get_state(resource);
255         if (result)
256                 return_VALUE(result);
257         if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
258                 return_VALUE(-ENOEXEC);
259
260         /* Update the power resource's _device_ power state */
261         result = acpi_bus_get_device(resource->handle, &device);
262         if (result)
263                 return_VALUE(result);
264         device->power.state = ACPI_STATE_D3;
265
266         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
267                           resource->name));
268
269         return_VALUE(0);
270 }
271
272 /*
273  * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
274  * 1. Power on the power resources required for the wakeup device 
275  * 2. Enable _PSW (power state wake) for the device if present
276  */
277 int acpi_enable_wakeup_device_power(struct acpi_device *dev)
278 {
279         union acpi_object arg = { ACPI_TYPE_INTEGER };
280         struct acpi_object_list arg_list = { 1, &arg };
281         acpi_status status = AE_OK;
282         int i;
283         int ret = 0;
284
285         ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_power");
286         if (!dev || !dev->wakeup.flags.valid)
287                 return_VALUE(-1);
288
289         arg.integer.value = 1;
290         /* Open power resource */
291         for (i = 0; i < dev->wakeup.resources.count; i++) {
292                 ret = acpi_power_on(dev->wakeup.resources.handles[i]);
293                 if (ret) {
294                         printk(KERN_ERR PREFIX "Transition power state\n");
295                         dev->wakeup.flags.valid = 0;
296                         return_VALUE(-1);
297                 }
298         }
299
300         /* Execute PSW */
301         status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
302         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
303                 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
304                 dev->wakeup.flags.valid = 0;
305                 ret = -1;
306         }
307
308         return_VALUE(ret);
309 }
310
311 /*
312  * Shutdown a wakeup device, counterpart of above method
313  * 1. Disable _PSW (power state wake)
314  * 2. Shutdown down the power resources
315  */
316 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
317 {
318         union acpi_object arg = { ACPI_TYPE_INTEGER };
319         struct acpi_object_list arg_list = { 1, &arg };
320         acpi_status status = AE_OK;
321         int i;
322         int ret = 0;
323
324         ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device_power");
325
326         if (!dev || !dev->wakeup.flags.valid)
327                 return_VALUE(-1);
328
329         arg.integer.value = 0;
330         /* Execute PSW */
331         status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
332         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
333                 printk(KERN_ERR PREFIX "Evaluate _PSW\n");
334                 dev->wakeup.flags.valid = 0;
335                 return_VALUE(-1);
336         }
337
338         /* Close power resource */
339         for (i = 0; i < dev->wakeup.resources.count; i++) {
340                 ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
341                 if (ret) {
342                         printk(KERN_ERR PREFIX "Transition power state\n");
343                         dev->wakeup.flags.valid = 0;
344                         return_VALUE(-1);
345                 }
346         }
347
348         return_VALUE(ret);
349 }
350
351 /* --------------------------------------------------------------------------
352                              Device Power Management
353    -------------------------------------------------------------------------- */
354
355 int acpi_power_get_inferred_state(struct acpi_device *device)
356 {
357         int result = 0;
358         struct acpi_handle_list *list = NULL;
359         int list_state = 0;
360         int i = 0;
361
362         ACPI_FUNCTION_TRACE("acpi_power_get_inferred_state");
363
364         if (!device)
365                 return_VALUE(-EINVAL);
366
367         device->power.state = ACPI_STATE_UNKNOWN;
368
369         /*
370          * We know a device's inferred power state when all the resources
371          * required for a given D-state are 'on'.
372          */
373         for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
374                 list = &device->power.states[i].resources;
375                 if (list->count < 1)
376                         continue;
377
378                 result = acpi_power_get_list_state(list, &list_state);
379                 if (result)
380                         return_VALUE(result);
381
382                 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
383                         device->power.state = i;
384                         return_VALUE(0);
385                 }
386         }
387
388         device->power.state = ACPI_STATE_D3;
389
390         return_VALUE(0);
391 }
392
393 int acpi_power_transition(struct acpi_device *device, int state)
394 {
395         int result = 0;
396         struct acpi_handle_list *cl = NULL;     /* Current Resources */
397         struct acpi_handle_list *tl = NULL;     /* Target Resources */
398         int i = 0;
399
400         ACPI_FUNCTION_TRACE("acpi_power_transition");
401
402         if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
403                 return_VALUE(-EINVAL);
404
405         if ((device->power.state < ACPI_STATE_D0)
406             || (device->power.state > ACPI_STATE_D3))
407                 return_VALUE(-ENODEV);
408
409         cl = &device->power.states[device->power.state].resources;
410         tl = &device->power.states[state].resources;
411
412         device->power.state = ACPI_STATE_UNKNOWN;
413
414         if (!cl->count && !tl->count) {
415                 result = -ENODEV;
416                 goto end;
417         }
418
419         /* TBD: Resources must be ordered. */
420
421         /*
422          * First we reference all power resources required in the target list
423          * (e.g. so the device doesn't lose power while transitioning).
424          */
425         for (i = 0; i < tl->count; i++) {
426                 result = acpi_power_on(tl->handles[i]);
427                 if (result)
428                         goto end;
429         }
430
431         /*
432          * Then we dereference all power resources used in the current list.
433          */
434         for (i = 0; i < cl->count; i++) {
435                 result = acpi_power_off_device(cl->handles[i]);
436                 if (result)
437                         goto end;
438         }
439
440         /* We shouldn't change the state till all above operations succeed */
441         device->power.state = state;
442       end:
443         if (result)
444                 printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
445                               device->pnp.bus_id, state);
446
447         return_VALUE(result);
448 }
449
450 /* --------------------------------------------------------------------------
451                               FS Interface (/proc)
452    -------------------------------------------------------------------------- */
453
454 static struct proc_dir_entry *acpi_power_dir;
455
456 static int acpi_power_seq_show(struct seq_file *seq, void *offset)
457 {
458         struct acpi_power_resource *resource = NULL;
459
460         ACPI_FUNCTION_TRACE("acpi_power_seq_show");
461
462         resource = (struct acpi_power_resource *)seq->private;
463
464         if (!resource)
465                 goto end;
466
467         seq_puts(seq, "state:                   ");
468         switch (resource->state) {
469         case ACPI_POWER_RESOURCE_STATE_ON:
470                 seq_puts(seq, "on\n");
471                 break;
472         case ACPI_POWER_RESOURCE_STATE_OFF:
473                 seq_puts(seq, "off\n");
474                 break;
475         default:
476                 seq_puts(seq, "unknown\n");
477                 break;
478         }
479
480         seq_printf(seq, "system level:            S%d\n"
481                    "order:                   %d\n"
482                    "reference count:         %d\n",
483                    resource->system_level,
484                    resource->order, resource->references);
485
486       end:
487         return_VALUE(0);
488 }
489
490 static int acpi_power_open_fs(struct inode *inode, struct file *file)
491 {
492         return single_open(file, acpi_power_seq_show, PDE(inode)->data);
493 }
494
495 static int acpi_power_add_fs(struct acpi_device *device)
496 {
497         struct proc_dir_entry *entry = NULL;
498
499         ACPI_FUNCTION_TRACE("acpi_power_add_fs");
500
501         if (!device)
502                 return_VALUE(-EINVAL);
503
504         if (!acpi_device_dir(device)) {
505                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
506                                                      acpi_power_dir);
507                 if (!acpi_device_dir(device))
508                         return_VALUE(-ENODEV);
509         }
510
511         /* 'status' [R] */
512         entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
513                                   S_IRUGO, acpi_device_dir(device));
514         if (!entry)
515                 return_VALUE(-EIO);
516         else {
517                 entry->proc_fops = &acpi_power_fops;
518                 entry->data = acpi_driver_data(device);
519         }
520
521         return_VALUE(0);
522 }
523
524 static int acpi_power_remove_fs(struct acpi_device *device)
525 {
526         ACPI_FUNCTION_TRACE("acpi_power_remove_fs");
527
528         if (acpi_device_dir(device)) {
529                 remove_proc_entry(ACPI_POWER_FILE_STATUS,
530                                   acpi_device_dir(device));
531                 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
532                 acpi_device_dir(device) = NULL;
533         }
534
535         return_VALUE(0);
536 }
537
538 /* --------------------------------------------------------------------------
539                                 Driver Interface
540    -------------------------------------------------------------------------- */
541
542 static int acpi_power_add(struct acpi_device *device)
543 {
544         int result = 0;
545         acpi_status status = AE_OK;
546         struct acpi_power_resource *resource = NULL;
547         union acpi_object acpi_object;
548         struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
549
550         ACPI_FUNCTION_TRACE("acpi_power_add");
551
552         if (!device)
553                 return_VALUE(-EINVAL);
554
555         resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
556         if (!resource)
557                 return_VALUE(-ENOMEM);
558         memset(resource, 0, sizeof(struct acpi_power_resource));
559
560         resource->handle = device->handle;
561         strcpy(resource->name, device->pnp.bus_id);
562         strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
563         strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
564         acpi_driver_data(device) = resource;
565
566         /* Evalute the object to get the system level and resource order. */
567         status = acpi_evaluate_object(resource->handle, NULL, NULL, &buffer);
568         if (ACPI_FAILURE(status)) {
569                 result = -ENODEV;
570                 goto end;
571         }
572         resource->system_level = acpi_object.power_resource.system_level;
573         resource->order = acpi_object.power_resource.resource_order;
574
575         result = acpi_power_get_state(resource);
576         if (result)
577                 goto end;
578
579         switch (resource->state) {
580         case ACPI_POWER_RESOURCE_STATE_ON:
581                 device->power.state = ACPI_STATE_D0;
582                 break;
583         case ACPI_POWER_RESOURCE_STATE_OFF:
584                 device->power.state = ACPI_STATE_D3;
585                 break;
586         default:
587                 device->power.state = ACPI_STATE_UNKNOWN;
588                 break;
589         }
590
591         result = acpi_power_add_fs(device);
592         if (result)
593                 goto end;
594
595         printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
596                acpi_device_bid(device), resource->state ? "on" : "off");
597
598       end:
599         if (result)
600                 kfree(resource);
601
602         return_VALUE(result);
603 }
604
605 static int acpi_power_remove(struct acpi_device *device, int type)
606 {
607         struct acpi_power_resource *resource = NULL;
608
609         ACPI_FUNCTION_TRACE("acpi_power_remove");
610
611         if (!device || !acpi_driver_data(device))
612                 return_VALUE(-EINVAL);
613
614         resource = (struct acpi_power_resource *)acpi_driver_data(device);
615
616         acpi_power_remove_fs(device);
617
618         kfree(resource);
619
620         return_VALUE(0);
621 }
622
623 static int __init acpi_power_init(void)
624 {
625         int result = 0;
626
627         ACPI_FUNCTION_TRACE("acpi_power_init");
628
629         if (acpi_disabled)
630                 return_VALUE(0);
631
632         INIT_LIST_HEAD(&acpi_power_resource_list);
633
634         acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
635         if (!acpi_power_dir)
636                 return_VALUE(-ENODEV);
637
638         result = acpi_bus_register_driver(&acpi_power_driver);
639         if (result < 0) {
640                 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
641                 return_VALUE(-ENODEV);
642         }
643
644         return_VALUE(0);
645 }
646
647 subsys_initcall(acpi_power_init);