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