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