blob: ce093d4516baab48118274e7e0676686262ccc92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040034ACPI_MODULE_NAME("acpi_utils")
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* --------------------------------------------------------------------------
37 Object Evaluation Helpers
38 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040050acpi_extract_package(union acpi_object *package,
51 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Len Brown4be44fc2005-08-05 00:44:28 -040053 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 ACPI_FUNCTION_TRACE("acpi_extract_package");
62
Len Brown4be44fc2005-08-05 00:44:28 -040063 if (!package || (package->type != ACPI_TYPE_PACKAGE)
64 || (package->package.count < 1)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040065 ACPI_WARNING((AE_INFO, "Invalid package argument"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return_ACPI_STATUS(AE_BAD_PARAMETER);
67 }
68
69 if (!format || !format->pointer || (format->length < 1)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040070 ACPI_WARNING((AE_INFO, "Invalid format argument"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return_ACPI_STATUS(AE_BAD_PARAMETER);
72 }
73
74 if (!buffer) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040075 ACPI_WARNING((AE_INFO, "Invalid buffer argument"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return_ACPI_STATUS(AE_BAD_PARAMETER);
77 }
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (format_count > package->package.count) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040081 ACPI_WARNING((AE_INFO, "Format specifies more objects [%d]"
82 " than exist in package [%d].",
83 format_count, package->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return_ACPI_STATUS(AE_BAD_DATA);
85 }
86
Len Brown4be44fc2005-08-05 00:44:28 -040087 format_string = (char *)format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 /*
90 * Calculate size_required.
91 */
Len Brown4be44fc2005-08-05 00:44:28 -040092 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
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':
Len Brown4be44fc2005-08-05 00:44:28 -0400109 size_required +=
110 sizeof(char *) + sizeof(acpi_integer) +
111 sizeof(char);
112 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400115 ACPI_WARNING((AE_INFO, "Invalid package element"
116 " [%d]: got number, expecing"
117 " [%c]",
118 i, format_string[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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':
Len Brown4be44fc2005-08-05 00:44:28 -0400128 size_required +=
129 sizeof(char *) +
130 (element->string.length * sizeof(char)) +
131 sizeof(char);
132 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
134 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400135 size_required +=
136 sizeof(u8 *) +
137 (element->buffer.length * sizeof(u8));
138 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 default:
Thomas Renningera6fc6722006-06-26 23:58:43 -0400141 ACPI_WARNING((AE_INFO, "Invalid package element"
142 " [%d] got string/buffer,"
143 " expecing [%c]",
144 i, format_string[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return_ACPI_STATUS(AE_BAD_DATA);
146 break;
147 }
148 break;
149
150 case ACPI_TYPE_PACKAGE:
151 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400152 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
153 "Found unsupported element at index=%d\n",
154 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* 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);
Len Brown4be44fc2005-08-05 00:44:28 -0400167 } else if (buffer->length != size_required || !buffer->pointer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400177 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
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':
Len Brown4be44fc2005-08-05 00:44:28 -0400191 *((acpi_integer *) head) =
192 element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 head += sizeof(acpi_integer);
194 break;
195 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400196 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400198 *((acpi_integer *) tail) =
199 element->integer.value;
200 head += sizeof(acpi_integer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 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':
Len Brown4be44fc2005-08-05 00:44:28 -0400216 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400218 memcpy(tail, element->string.pointer,
219 element->string.length);
220 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 tail += element->string.length * sizeof(char);
222 /* NULL terminate string */
223 *tail = (char)0;
224 tail += sizeof(char);
225 break;
226 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400227 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400229 memcpy(tail, element->buffer.pointer,
230 element->buffer.length);
231 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 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}
Len Brown4be44fc2005-08-05 00:44:28 -0400250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251EXPORT_SYMBOL(acpi_extract_package);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400254acpi_evaluate_integer(acpi_handle handle,
255 acpi_string pathname,
256 struct acpi_object_list *arguments, unsigned long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Len Brown4be44fc2005-08-05 00:44:28 -0400258 acpi_status status = AE_OK;
259 union acpi_object *element;
260 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
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);
Len Brown4be44fc2005-08-05 00:44:28 -0400268 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 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);
Vasily Averin64385f22006-04-27 05:25:00 -0400277 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return_ACPI_STATUS(status);
279 }
280
281 if (element->type != ACPI_TYPE_INTEGER) {
282 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Vasily Averin64385f22006-04-27 05:25:00 -0400283 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Len Brown4be44fc2005-08-05 00:44:28 -0400295EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297#if 0
298acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400299acpi_evaluate_string(acpi_handle handle,
300 acpi_string pathname,
301 acpi_object_list * arguments, acpi_string * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Len Brown4be44fc2005-08-05 00:44:28 -0400303 acpi_status status = AE_OK;
304 acpi_object *element = NULL;
305 acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
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
Len Brown4be44fc2005-08-05 00:44:28 -0400320 if ((element->type != ACPI_TYPE_STRING)
321 || (element->type != ACPI_TYPE_BUFFER)
322 || !element->string.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 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) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400329 ACPI_ERROR((AE_INFO, "Memory allocation"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400345acpi_evaluate_reference(acpi_handle handle,
346 acpi_string pathname,
347 struct acpi_object_list *arguments,
348 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
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
Len Brown4be44fc2005-08-05 00:44:28 -0400368 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if ((buffer.length == 0) || !package) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400371 ACPI_ERROR((AE_INFO, "No return object (len %X ptr %p)",
372 (unsigned)buffer.length, package));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 status = AE_BAD_DATA;
374 acpi_util_eval_error(handle, pathname, status);
375 goto end;
376 }
377 if (package->type != ACPI_TYPE_PACKAGE) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400378 ACPI_ERROR((AE_INFO, "Expecting a [Package], found type %X",
379 package->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 status = AE_BAD_DATA;
381 acpi_util_eval_error(handle, pathname, status);
382 goto end;
383 }
384 if (!package->package.count) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400385 ACPI_ERROR((AE_INFO, "[Package] has zero elements (%p)",
386 package));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 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;
Thomas Renningera6fc6722006-06-26 23:58:43 -0400405 ACPI_ERROR((AE_INFO,
406 "Expecting a [Reference] package element, found type %X",
407 element->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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",
Len Brown4be44fc2005-08-05 00:44:28 -0400416 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Len Brown4be44fc2005-08-05 00:44:28 -0400419 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Len Brown4be44fc2005-08-05 00:44:28 -0400430EXPORT_SYMBOL(acpi_evaluate_reference);