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))); }
50 #define acpi_util_eval_error(h,p,s)
55 acpi_extract_package (
56 union acpi_object
*package
,
57 struct acpi_buffer
*format
,
58 struct acpi_buffer
*buffer
)
60 u32 size_required
= 0;
62 char *format_string
= 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
);
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
]);
101 return_ACPI_STATUS(AE_BAD_DATA
);
104 switch (element
->type
) {
106 case ACPI_TYPE_INTEGER
:
107 switch (format_string
[i
]) {
109 size_required
+= sizeof(acpi_integer
);
110 tail_offset
+= sizeof(acpi_integer
);
113 size_required
+= sizeof(char*) + sizeof(acpi_integer
) + sizeof(char);
114 tail_offset
+= sizeof(char*);
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
);
123 case ACPI_TYPE_STRING
:
124 case ACPI_TYPE_BUFFER
:
125 switch (format_string
[i
]) {
127 size_required
+= sizeof(char*) + (element
->string
.length
* sizeof(char)) + sizeof(char);
128 tail_offset
+= sizeof(char*);
131 size_required
+= sizeof(u8
*) + (element
->buffer
.length
* sizeof(u8
));
132 tail_offset
+= sizeof(u8
*);
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
);
141 case ACPI_TYPE_PACKAGE
:
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
);
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
++) {
170 union acpi_object
*element
= &(package
->package
.elements
[i
]);
173 return_ACPI_STATUS(AE_BAD_DATA
);
176 switch (element
->type
) {
178 case ACPI_TYPE_INTEGER
:
179 switch (format_string
[i
]) {
181 *((acpi_integer
*)head
) = element
->integer
.value
;
182 head
+= sizeof(acpi_integer
);
185 pointer
= (u8
**)head
;
187 *((acpi_integer
*)tail
) = element
->integer
.value
;
188 head
+= sizeof(acpi_integer
*);
189 tail
+= sizeof(acpi_integer
);
190 /* NULL terminate string */
192 tail
+= sizeof(char);
195 /* Should never get here */
200 case ACPI_TYPE_STRING
:
201 case ACPI_TYPE_BUFFER
:
202 switch (format_string
[i
]) {
204 pointer
= (u8
**)head
;
206 memcpy(tail
, element
->string
.pointer
, element
->string
.length
);
207 head
+= sizeof(char*);
208 tail
+= element
->string
.length
* sizeof(char);
209 /* NULL terminate string */
211 tail
+= sizeof(char);
214 pointer
= (u8
**)head
;
216 memcpy(tail
, element
->buffer
.pointer
, element
->buffer
.length
);
218 tail
+= element
->buffer
.length
* sizeof(u8
);
221 /* Should never get here */
226 case ACPI_TYPE_PACKAGE
:
227 /* TBD: handle nested packages... */
229 /* Should never get here */
234 return_ACPI_STATUS(AE_OK
);
236 EXPORT_SYMBOL(acpi_extract_package
);
240 acpi_evaluate_integer (
242 acpi_string pathname
,
243 struct acpi_object_list
*arguments
,
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");
253 return_ACPI_STATUS(AE_BAD_PARAMETER
);
255 element
= kmalloc(sizeof(union acpi_object
), GFP_KERNEL
);
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
;
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%lu]\n", *data
));
278 return_ACPI_STATUS(AE_OK
);
280 EXPORT_SYMBOL(acpi_evaluate_integer
);
285 acpi_evaluate_string (
287 acpi_string pathname
,
288 acpi_object_list
*arguments
,
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");
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
);
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
);
334 acpi_evaluate_reference (
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
};
346 ACPI_FUNCTION_TRACE("acpi_evaluate_reference");
349 return_ACPI_STATUS(AE_BAD_PARAMETER
);
352 /* Evaluate object. */
354 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
355 if (ACPI_FAILURE(status
))
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
);
368 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
369 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
370 "Expecting a [Package], found type %X\n",
372 status
= AE_BAD_DATA
;
373 acpi_util_eval_error(handle
, pathname
, status
);
376 if (!package
->package
.count
) {
377 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
378 "[Package] has zero elements (%p)\n",
380 status
= AE_BAD_DATA
;
381 acpi_util_eval_error(handle
, pathname
, status
);
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",
401 acpi_util_eval_error(handle
, pathname
, status
);
405 /* Get the acpi_handle. */
407 list
->handles
[i
] = element
->reference
.handle
;
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found reference [%p]\n",
413 if (ACPI_FAILURE(status
)) {
415 //kfree(list->handles);
418 acpi_os_free(buffer
.pointer
);
420 return_ACPI_STATUS(status
);
422 EXPORT_SYMBOL(acpi_evaluate_reference
);