a5da4efab063df4aa64e313ee7efa55e74731ff8
[linux-2.6.git] / drivers / acpi / utils.c
1 /*
2  *  acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
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 <acpi/acpi_bus.h>
31 #include <acpi/acpi_drivers.h>
32
33 #define _COMPONENT              ACPI_BUS_COMPONENT
34 ACPI_MODULE_NAME("acpi_utils")
35
36 /* --------------------------------------------------------------------------
37                             Object Evaluation Helpers
38    -------------------------------------------------------------------------- */
39 #ifdef ACPI_DEBUG_OUTPUT
40 #define acpi_util_eval_error(h,p,s) {\
41         char prefix[80] = {'\0'};\
42         struct acpi_buffer buffer = {sizeof(prefix), prefix};\
43         acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
44         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",\
45                 (char *) prefix, p, acpi_format_exception(s))); }
46 #else
47 #define acpi_util_eval_error(h,p,s)
48 #endif
49 acpi_status
50 acpi_extract_package(union acpi_object *package,
51                      struct acpi_buffer *format, struct acpi_buffer *buffer)
52 {
53         u32 size_required = 0;
54         u32 tail_offset = 0;
55         char *format_string = NULL;
56         u32 format_count = 0;
57         u32 i = 0;
58         u8 *head = NULL;
59         u8 *tail = NULL;
60
61         ACPI_FUNCTION_TRACE("acpi_extract_package");
62
63         if (!package || (package->type != ACPI_TYPE_PACKAGE)
64             || (package->package.count < 1)) {
65                 printk(KERN_WARNING PREFIX "Invalid package argument\n");
66                 return_ACPI_STATUS(AE_BAD_PARAMETER);
67         }
68
69         if (!format || !format->pointer || (format->length < 1)) {
70                 printk(KERN_WARNING PREFIX "Invalid format argument\n");
71                 return_ACPI_STATUS(AE_BAD_PARAMETER);
72         }
73
74         if (!buffer) {
75                 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
76                 return_ACPI_STATUS(AE_BAD_PARAMETER);
77         }
78
79         format_count = (format->length / sizeof(char)) - 1;
80         if (format_count > package->package.count) {
81                 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
82                               " than exist in package [%d].\n",
83                               format_count, package->package.count);
84                 return_ACPI_STATUS(AE_BAD_DATA);
85         }
86
87         format_string = (char *)format->pointer;
88
89         /*
90          * Calculate size_required.
91          */
92         for (i = 0; i < format_count; i++) {
93
94                 union acpi_object *element = &(package->package.elements[i]);
95
96                 if (!element) {
97                         return_ACPI_STATUS(AE_BAD_DATA);
98                 }
99
100                 switch (element->type) {
101
102                 case ACPI_TYPE_INTEGER:
103                         switch (format_string[i]) {
104                         case 'N':
105                                 size_required += sizeof(acpi_integer);
106                                 tail_offset += sizeof(acpi_integer);
107                                 break;
108                         case 'S':
109                                 size_required +=
110                                     sizeof(char *) + sizeof(acpi_integer) +
111                                     sizeof(char);
112                                 tail_offset += sizeof(char *);
113                                 break;
114                         default:
115                                 printk(KERN_WARNING PREFIX "Invalid package element"
116                                               " [%d]: got number, expecing"
117                                               " [%c]\n",
118                                               i, format_string[i]);
119                                 return_ACPI_STATUS(AE_BAD_DATA);
120                                 break;
121                         }
122                         break;
123
124                 case ACPI_TYPE_STRING:
125                 case ACPI_TYPE_BUFFER:
126                         switch (format_string[i]) {
127                         case 'S':
128                                 size_required +=
129                                     sizeof(char *) +
130                                     (element->string.length * sizeof(char)) +
131                                     sizeof(char);
132                                 tail_offset += sizeof(char *);
133                                 break;
134                         case 'B':
135                                 size_required +=
136                                     sizeof(u8 *) +
137                                     (element->buffer.length * sizeof(u8));
138                                 tail_offset += sizeof(u8 *);
139                                 break;
140                         default:
141                                 printk(KERN_WARNING PREFIX "Invalid package element"
142                                               " [%d] got string/buffer,"
143                                               " expecing [%c]\n",
144                                               i, format_string[i]);
145                                 return_ACPI_STATUS(AE_BAD_DATA);
146                                 break;
147                         }
148                         break;
149
150                 case ACPI_TYPE_PACKAGE:
151                 default:
152                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
153                                           "Found unsupported element at index=%d\n",
154                                           i));
155                         /* TBD: handle nested packages... */
156                         return_ACPI_STATUS(AE_SUPPORT);
157                         break;
158                 }
159         }
160
161         /*
162          * Validate output buffer.
163          */
164         if (buffer->length < size_required) {
165                 buffer->length = size_required;
166                 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
167         } else if (buffer->length != size_required || !buffer->pointer) {
168                 return_ACPI_STATUS(AE_BAD_PARAMETER);
169         }
170
171         head = buffer->pointer;
172         tail = buffer->pointer + tail_offset;
173
174         /*
175          * Extract package data.
176          */
177         for (i = 0; i < format_count; i++) {
178
179                 u8 **pointer = NULL;
180                 union acpi_object *element = &(package->package.elements[i]);
181
182                 if (!element) {
183                         return_ACPI_STATUS(AE_BAD_DATA);
184                 }
185
186                 switch (element->type) {
187
188                 case ACPI_TYPE_INTEGER:
189                         switch (format_string[i]) {
190                         case 'N':
191                                 *((acpi_integer *) head) =
192                                     element->integer.value;
193                                 head += sizeof(acpi_integer);
194                                 break;
195                         case 'S':
196                                 pointer = (u8 **) head;
197                                 *pointer = tail;
198                                 *((acpi_integer *) tail) =
199                                     element->integer.value;
200                                 head += sizeof(acpi_integer *);
201                                 tail += sizeof(acpi_integer);
202                                 /* NULL terminate string */
203                                 *tail = (char)0;
204                                 tail += sizeof(char);
205                                 break;
206                         default:
207                                 /* Should never get here */
208                                 break;
209                         }
210                         break;
211
212                 case ACPI_TYPE_STRING:
213                 case ACPI_TYPE_BUFFER:
214                         switch (format_string[i]) {
215                         case 'S':
216                                 pointer = (u8 **) head;
217                                 *pointer = tail;
218                                 memcpy(tail, element->string.pointer,
219                                        element->string.length);
220                                 head += sizeof(char *);
221                                 tail += element->string.length * sizeof(char);
222                                 /* NULL terminate string */
223                                 *tail = (char)0;
224                                 tail += sizeof(char);
225                                 break;
226                         case 'B':
227                                 pointer = (u8 **) head;
228                                 *pointer = tail;
229                                 memcpy(tail, element->buffer.pointer,
230                                        element->buffer.length);
231                                 head += sizeof(u8 *);
232                                 tail += element->buffer.length * sizeof(u8);
233                                 break;
234                         default:
235                                 /* Should never get here */
236                                 break;
237                         }
238                         break;
239
240                 case ACPI_TYPE_PACKAGE:
241                         /* TBD: handle nested packages... */
242                 default:
243                         /* Should never get here */
244                         break;
245                 }
246         }
247
248         return_ACPI_STATUS(AE_OK);
249 }
250
251 EXPORT_SYMBOL(acpi_extract_package);
252
253 acpi_status
254 acpi_evaluate_integer(acpi_handle handle,
255                       acpi_string pathname,
256                       struct acpi_object_list *arguments, unsigned long *data)
257 {
258         acpi_status status = AE_OK;
259         union acpi_object *element;
260         struct acpi_buffer buffer = { 0, NULL };
261
262         ACPI_FUNCTION_TRACE("acpi_evaluate_integer");
263
264         if (!data)
265                 return_ACPI_STATUS(AE_BAD_PARAMETER);
266
267         element = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
268         if (!element)
269                 return_ACPI_STATUS(AE_NO_MEMORY);
270
271         memset(element, 0, sizeof(union acpi_object));
272         buffer.length = sizeof(union acpi_object);
273         buffer.pointer = element;
274         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
275         if (ACPI_FAILURE(status)) {
276                 acpi_util_eval_error(handle, pathname, status);
277                 kfree(element);
278                 return_ACPI_STATUS(status);
279         }
280
281         if (element->type != ACPI_TYPE_INTEGER) {
282                 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
283                 kfree(element);
284                 return_ACPI_STATUS(AE_BAD_DATA);
285         }
286
287         *data = element->integer.value;
288         kfree(element);
289
290         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data));
291
292         return_ACPI_STATUS(AE_OK);
293 }
294
295 EXPORT_SYMBOL(acpi_evaluate_integer);
296
297 #if 0
298 acpi_status
299 acpi_evaluate_string(acpi_handle handle,
300                      acpi_string pathname,
301                      acpi_object_list * arguments, acpi_string * data)
302 {
303         acpi_status status = AE_OK;
304         acpi_object *element = NULL;
305         acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
306
307         ACPI_FUNCTION_TRACE("acpi_evaluate_string");
308
309         if (!data)
310                 return_ACPI_STATUS(AE_BAD_PARAMETER);
311
312         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
313         if (ACPI_FAILURE(status)) {
314                 acpi_util_eval_error(handle, pathname, status);
315                 return_ACPI_STATUS(status);
316         }
317
318         element = (acpi_object *) buffer.pointer;
319
320         if ((element->type != ACPI_TYPE_STRING)
321             || (element->type != ACPI_TYPE_BUFFER)
322             || !element->string.length) {
323                 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
324                 return_ACPI_STATUS(AE_BAD_DATA);
325         }
326
327         *data = kmalloc(element->string.length + 1, GFP_KERNEL);
328         if (!data) {
329                 printk(KERN_ERR PREFIX "Memory allocation\n");
330                 return_VALUE(-ENOMEM);
331         }
332         memset(*data, 0, element->string.length + 1);
333
334         memcpy(*data, element->string.pointer, element->string.length);
335
336         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));
337
338         acpi_os_free(buffer.pointer);
339
340         return_ACPI_STATUS(AE_OK);
341 }
342 #endif
343
344 acpi_status
345 acpi_evaluate_reference(acpi_handle handle,
346                         acpi_string pathname,
347                         struct acpi_object_list *arguments,
348                         struct acpi_handle_list *list)
349 {
350         acpi_status status = AE_OK;
351         union acpi_object *package = NULL;
352         union acpi_object *element = NULL;
353         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
354         u32 i = 0;
355
356         ACPI_FUNCTION_TRACE("acpi_evaluate_reference");
357
358         if (!list) {
359                 return_ACPI_STATUS(AE_BAD_PARAMETER);
360         }
361
362         /* Evaluate object. */
363
364         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
365         if (ACPI_FAILURE(status))
366                 goto end;
367
368         package = (union acpi_object *)buffer.pointer;
369
370         if ((buffer.length == 0) || !package) {
371                 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
372                             (unsigned)buffer.length, package);
373                 status = AE_BAD_DATA;
374                 acpi_util_eval_error(handle, pathname, status);
375                 goto end;
376         }
377         if (package->type != ACPI_TYPE_PACKAGE) {
378                 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
379                             package->type);
380                 status = AE_BAD_DATA;
381                 acpi_util_eval_error(handle, pathname, status);
382                 goto end;
383         }
384         if (!package->package.count) {
385                 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
386                             package);
387                 status = AE_BAD_DATA;
388                 acpi_util_eval_error(handle, pathname, status);
389                 goto end;
390         }
391
392         if (package->package.count > ACPI_MAX_HANDLES) {
393                 return_ACPI_STATUS(AE_NO_MEMORY);
394         }
395         list->count = package->package.count;
396
397         /* Extract package data. */
398
399         for (i = 0; i < list->count; i++) {
400
401                 element = &(package->package.elements[i]);
402
403                 if (element->type != ACPI_TYPE_ANY) {
404                         status = AE_BAD_DATA;
405                         printk(KERN_ERR PREFIX
406                                     "Expecting a [Reference] package element, found type %X\n",
407                                     element->type);
408                         acpi_util_eval_error(handle, pathname, status);
409                         break;
410                 }
411
412                 /* Get the  acpi_handle. */
413
414                 list->handles[i] = element->reference.handle;
415                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
416                                   list->handles[i]));
417         }
418
419       end:
420         if (ACPI_FAILURE(status)) {
421                 list->count = 0;
422                 //kfree(list->handles);
423         }
424
425         acpi_os_free(buffer.pointer);
426
427         return_ACPI_STATUS(status);
428 }
429
430 EXPORT_SYMBOL(acpi_evaluate_reference);