Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / acpi / utils.c
blob1ce2047c3804d828fd16404838154af9e9bec8be
1 /*
2 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
34 #define _COMPONENT ACPI_BUS_COMPONENT
35 ACPI_MODULE_NAME ("acpi_utils")
38 /* --------------------------------------------------------------------------
39 Object Evaluation Helpers
40 -------------------------------------------------------------------------- */
42 #ifdef ACPI_DEBUG_OUTPUT
43 #define acpi_util_eval_error(h,p,s) {\
44 char prefix[80] = {'\0'};\
45 struct acpi_buffer buffer = {sizeof(prefix), prefix};\
46 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
47 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",\
48 (char *) prefix, p, acpi_format_exception(s))); }
49 #else
50 #define acpi_util_eval_error(h,p,s)
51 #endif
54 acpi_status
55 acpi_extract_package (
56 union acpi_object *package,
57 struct acpi_buffer *format,
58 struct acpi_buffer *buffer)
60 u32 size_required = 0;
61 u32 tail_offset = 0;
62 char *format_string = NULL;
63 u32 format_count = 0;
64 u32 i = 0;
65 u8 *head = NULL;
66 u8 *tail = NULL;
68 ACPI_FUNCTION_TRACE("acpi_extract_package");
70 if (!package || (package->type != ACPI_TYPE_PACKAGE) || (package->package.count < 1)) {
71 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'package' argument\n"));
72 return_ACPI_STATUS(AE_BAD_PARAMETER);
75 if (!format || !format->pointer || (format->length < 1)) {
76 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'format' argument\n"));
77 return_ACPI_STATUS(AE_BAD_PARAMETER);
80 if (!buffer) {
81 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'buffer' argument\n"));
82 return_ACPI_STATUS(AE_BAD_PARAMETER);
85 format_count = (format->length/sizeof(char)) - 1;
86 if (format_count > package->package.count) {
87 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Format specifies more objects [%d] than exist in package [%d].", format_count, package->package.count));
88 return_ACPI_STATUS(AE_BAD_DATA);
91 format_string = (char*)format->pointer;
94 * Calculate size_required.
96 for (i=0; i<format_count; i++) {
98 union acpi_object *element = &(package->package.elements[i]);
100 if (!element) {
101 return_ACPI_STATUS(AE_BAD_DATA);
104 switch (element->type) {
106 case ACPI_TYPE_INTEGER:
107 switch (format_string[i]) {
108 case 'N':
109 size_required += sizeof(acpi_integer);
110 tail_offset += sizeof(acpi_integer);
111 break;
112 case 'S':
113 size_required += sizeof(char*) + sizeof(acpi_integer) + sizeof(char);
114 tail_offset += sizeof(char*);
115 break;
116 default:
117 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d]: got number, expecing [%c].\n", i, format_string[i]));
118 return_ACPI_STATUS(AE_BAD_DATA);
119 break;
121 break;
123 case ACPI_TYPE_STRING:
124 case ACPI_TYPE_BUFFER:
125 switch (format_string[i]) {
126 case 'S':
127 size_required += sizeof(char*) + (element->string.length * sizeof(char)) + sizeof(char);
128 tail_offset += sizeof(char*);
129 break;
130 case 'B':
131 size_required += sizeof(u8*) + (element->buffer.length * sizeof(u8));
132 tail_offset += sizeof(u8*);
133 break;
134 default:
135 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d] got string/buffer, expecing [%c].\n", i, format_string[i]));
136 return_ACPI_STATUS(AE_BAD_DATA);
137 break;
139 break;
141 case ACPI_TYPE_PACKAGE:
142 default:
143 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unsupported element at index=%d\n", i));
144 /* TBD: handle nested packages... */
145 return_ACPI_STATUS(AE_SUPPORT);
146 break;
151 * Validate output buffer.
153 if (buffer->length < size_required) {
154 buffer->length = size_required;
155 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
157 else if (buffer->length != size_required || !buffer->pointer) {
158 return_ACPI_STATUS(AE_BAD_PARAMETER);
161 head = buffer->pointer;
162 tail = buffer->pointer + tail_offset;
165 * Extract package data.
167 for (i=0; i<format_count; i++) {
169 u8 **pointer = NULL;
170 union acpi_object *element = &(package->package.elements[i]);
172 if (!element) {
173 return_ACPI_STATUS(AE_BAD_DATA);
176 switch (element->type) {
178 case ACPI_TYPE_INTEGER:
179 switch (format_string[i]) {
180 case 'N':
181 *((acpi_integer*)head) = element->integer.value;
182 head += sizeof(acpi_integer);
183 break;
184 case 'S':
185 pointer = (u8**)head;
186 *pointer = tail;
187 *((acpi_integer*)tail) = element->integer.value;
188 head += sizeof(acpi_integer*);
189 tail += sizeof(acpi_integer);
190 /* NULL terminate string */
191 *tail = (char)0;
192 tail += sizeof(char);
193 break;
194 default:
195 /* Should never get here */
196 break;
198 break;
200 case ACPI_TYPE_STRING:
201 case ACPI_TYPE_BUFFER:
202 switch (format_string[i]) {
203 case 'S':
204 pointer = (u8**)head;
205 *pointer = tail;
206 memcpy(tail, element->string.pointer, element->string.length);
207 head += sizeof(char*);
208 tail += element->string.length * sizeof(char);
209 /* NULL terminate string */
210 *tail = (char)0;
211 tail += sizeof(char);
212 break;
213 case 'B':
214 pointer = (u8**)head;
215 *pointer = tail;
216 memcpy(tail, element->buffer.pointer, element->buffer.length);
217 head += sizeof(u8*);
218 tail += element->buffer.length * sizeof(u8);
219 break;
220 default:
221 /* Should never get here */
222 break;
224 break;
226 case ACPI_TYPE_PACKAGE:
227 /* TBD: handle nested packages... */
228 default:
229 /* Should never get here */
230 break;
234 return_ACPI_STATUS(AE_OK);
236 EXPORT_SYMBOL(acpi_extract_package);
239 acpi_status
240 acpi_evaluate_integer (
241 acpi_handle handle,
242 acpi_string pathname,
243 struct acpi_object_list *arguments,
244 unsigned long *data)
246 acpi_status status = AE_OK;
247 union acpi_object *element;
248 struct acpi_buffer buffer = {0,NULL};
250 ACPI_FUNCTION_TRACE("acpi_evaluate_integer");
252 if (!data)
253 return_ACPI_STATUS(AE_BAD_PARAMETER);
255 element = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
256 if(!element)
257 return_ACPI_STATUS(AE_NO_MEMORY);
259 memset(element, 0, sizeof(union acpi_object));
260 buffer.length = sizeof(union acpi_object);
261 buffer.pointer = element;
262 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
263 if (ACPI_FAILURE(status)) {
264 acpi_util_eval_error(handle, pathname, status);
265 return_ACPI_STATUS(status);
268 if (element->type != ACPI_TYPE_INTEGER) {
269 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
270 return_ACPI_STATUS(AE_BAD_DATA);
273 *data = element->integer.value;
274 kfree(element);
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data));
278 return_ACPI_STATUS(AE_OK);
280 EXPORT_SYMBOL(acpi_evaluate_integer);
283 #if 0
284 acpi_status
285 acpi_evaluate_string (
286 acpi_handle handle,
287 acpi_string pathname,
288 acpi_object_list *arguments,
289 acpi_string *data)
291 acpi_status status = AE_OK;
292 acpi_object *element = NULL;
293 acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
295 ACPI_FUNCTION_TRACE("acpi_evaluate_string");
297 if (!data)
298 return_ACPI_STATUS(AE_BAD_PARAMETER);
300 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
301 if (ACPI_FAILURE(status)) {
302 acpi_util_eval_error(handle, pathname, status);
303 return_ACPI_STATUS(status);
306 element = (acpi_object *) buffer.pointer;
308 if ((element->type != ACPI_TYPE_STRING)
309 || (element->type != ACPI_TYPE_BUFFER)
310 || !element->string.length) {
311 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
312 return_ACPI_STATUS(AE_BAD_DATA);
315 *data = kmalloc(element->string.length + 1, GFP_KERNEL);
316 if (!data) {
317 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
318 return_VALUE(-ENOMEM);
320 memset(*data, 0, element->string.length + 1);
322 memcpy(*data, element->string.pointer, element->string.length);
324 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));
326 acpi_os_free(buffer.pointer);
328 return_ACPI_STATUS(AE_OK);
330 #endif
333 acpi_status
334 acpi_evaluate_reference (
335 acpi_handle handle,
336 acpi_string pathname,
337 struct acpi_object_list *arguments,
338 struct acpi_handle_list *list)
340 acpi_status status = AE_OK;
341 union acpi_object *package = NULL;
342 union acpi_object *element = NULL;
343 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
344 u32 i = 0;
346 ACPI_FUNCTION_TRACE("acpi_evaluate_reference");
348 if (!list) {
349 return_ACPI_STATUS(AE_BAD_PARAMETER);
352 /* Evaluate object. */
354 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
355 if (ACPI_FAILURE(status))
356 goto end;
358 package = (union acpi_object *) buffer.pointer;
360 if ((buffer.length == 0) || !package) {
361 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
362 "No return object (len %X ptr %p)\n",
363 (unsigned)buffer.length, package));
364 status = AE_BAD_DATA;
365 acpi_util_eval_error(handle, pathname, status);
366 goto end;
368 if (package->type != ACPI_TYPE_PACKAGE) {
369 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
370 "Expecting a [Package], found type %X\n",
371 package->type));
372 status = AE_BAD_DATA;
373 acpi_util_eval_error(handle, pathname, status);
374 goto end;
376 if (!package->package.count) {
377 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
378 "[Package] has zero elements (%p)\n",
379 package));
380 status = AE_BAD_DATA;
381 acpi_util_eval_error(handle, pathname, status);
382 goto end;
385 if (package->package.count > ACPI_MAX_HANDLES) {
386 return_ACPI_STATUS(AE_NO_MEMORY);
388 list->count = package->package.count;
390 /* Extract package data. */
392 for (i = 0; i < list->count; i++) {
394 element = &(package->package.elements[i]);
396 if (element->type != ACPI_TYPE_ANY) {
397 status = AE_BAD_DATA;
398 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
399 "Expecting a [Reference] package element, found type %X\n",
400 element->type));
401 acpi_util_eval_error(handle, pathname, status);
402 break;
405 /* Get the acpi_handle. */
407 list->handles[i] = element->reference.handle;
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
409 list->handles[i]));
412 end:
413 if (ACPI_FAILURE(status)) {
414 list->count = 0;
415 //kfree(list->handles);
418 acpi_os_free(buffer.pointer);
420 return_ACPI_STATUS(status);
422 EXPORT_SYMBOL(acpi_evaluate_reference);