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>
33 #define _COMPONENT ACPI_BUS_COMPONENT
34 ACPI_MODULE_NAME("utils");
36 /* --------------------------------------------------------------------------
37 Object Evaluation Helpers
38 -------------------------------------------------------------------------- */
40 acpi_util_eval_error(acpi_handle h
, acpi_string p
, acpi_status s
)
42 #ifdef ACPI_DEBUG_OUTPUT
43 char prefix
[80] = {'\0'};
44 struct acpi_buffer buffer
= {sizeof(prefix
), prefix
};
45 acpi_get_name(h
, ACPI_FULL_PATHNAME
, &buffer
);
46 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Evaluate [%s.%s]: %s\n",
47 (char *) prefix
, p
, acpi_format_exception(s
)));
54 acpi_extract_package(union acpi_object
*package
,
55 struct acpi_buffer
*format
, struct acpi_buffer
*buffer
)
57 u32 size_required
= 0;
59 char *format_string
= NULL
;
66 if (!package
|| (package
->type
!= ACPI_TYPE_PACKAGE
)
67 || (package
->package
.count
< 1)) {
68 printk(KERN_WARNING PREFIX
"Invalid package argument\n");
69 return AE_BAD_PARAMETER
;
72 if (!format
|| !format
->pointer
|| (format
->length
< 1)) {
73 printk(KERN_WARNING PREFIX
"Invalid format argument\n");
74 return AE_BAD_PARAMETER
;
78 printk(KERN_WARNING PREFIX
"Invalid buffer argument\n");
79 return AE_BAD_PARAMETER
;
82 format_count
= (format
->length
/ sizeof(char)) - 1;
83 if (format_count
> package
->package
.count
) {
84 printk(KERN_WARNING PREFIX
"Format specifies more objects [%d]"
85 " than exist in package [%d].\n",
86 format_count
, package
->package
.count
);
90 format_string
= format
->pointer
;
93 * Calculate size_required.
95 for (i
= 0; i
< format_count
; i
++) {
97 union acpi_object
*element
= &(package
->package
.elements
[i
]);
103 switch (element
->type
) {
105 case ACPI_TYPE_INTEGER
:
106 switch (format_string
[i
]) {
108 size_required
+= sizeof(acpi_integer
);
109 tail_offset
+= sizeof(acpi_integer
);
113 sizeof(char *) + sizeof(acpi_integer
) +
115 tail_offset
+= sizeof(char *);
118 printk(KERN_WARNING PREFIX
"Invalid package element"
119 " [%d]: got number, expecing"
121 i
, format_string
[i
]);
127 case ACPI_TYPE_STRING
:
128 case ACPI_TYPE_BUFFER
:
129 switch (format_string
[i
]) {
133 (element
->string
.length
* sizeof(char)) +
135 tail_offset
+= sizeof(char *);
140 (element
->buffer
.length
* sizeof(u8
));
141 tail_offset
+= sizeof(u8
*);
144 printk(KERN_WARNING PREFIX
"Invalid package element"
145 " [%d] got string/buffer,"
147 i
, format_string
[i
]);
153 case ACPI_TYPE_PACKAGE
:
155 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
156 "Found unsupported element at index=%d\n",
158 /* TBD: handle nested packages... */
165 * Validate output buffer.
167 if (buffer
->length
< size_required
) {
168 buffer
->length
= size_required
;
169 return AE_BUFFER_OVERFLOW
;
170 } else if (buffer
->length
!= size_required
|| !buffer
->pointer
) {
171 return AE_BAD_PARAMETER
;
174 head
= buffer
->pointer
;
175 tail
= buffer
->pointer
+ tail_offset
;
178 * Extract package data.
180 for (i
= 0; i
< format_count
; i
++) {
183 union acpi_object
*element
= &(package
->package
.elements
[i
]);
189 switch (element
->type
) {
191 case ACPI_TYPE_INTEGER
:
192 switch (format_string
[i
]) {
194 *((acpi_integer
*) head
) =
195 element
->integer
.value
;
196 head
+= sizeof(acpi_integer
);
199 pointer
= (u8
**) head
;
201 *((acpi_integer
*) tail
) =
202 element
->integer
.value
;
203 head
+= sizeof(acpi_integer
*);
204 tail
+= sizeof(acpi_integer
);
205 /* NULL terminate string */
207 tail
+= sizeof(char);
210 /* Should never get here */
215 case ACPI_TYPE_STRING
:
216 case ACPI_TYPE_BUFFER
:
217 switch (format_string
[i
]) {
219 pointer
= (u8
**) head
;
221 memcpy(tail
, element
->string
.pointer
,
222 element
->string
.length
);
223 head
+= sizeof(char *);
224 tail
+= element
->string
.length
* sizeof(char);
225 /* NULL terminate string */
227 tail
+= sizeof(char);
230 pointer
= (u8
**) head
;
232 memcpy(tail
, element
->buffer
.pointer
,
233 element
->buffer
.length
);
234 head
+= sizeof(u8
*);
235 tail
+= element
->buffer
.length
* sizeof(u8
);
238 /* Should never get here */
243 case ACPI_TYPE_PACKAGE
:
244 /* TBD: handle nested packages... */
246 /* Should never get here */
254 EXPORT_SYMBOL(acpi_extract_package
);
257 acpi_evaluate_integer(acpi_handle handle
,
258 acpi_string pathname
,
259 struct acpi_object_list
*arguments
, unsigned long long *data
)
261 acpi_status status
= AE_OK
;
262 union acpi_object element
;
263 struct acpi_buffer buffer
= { 0, NULL
};
266 return AE_BAD_PARAMETER
;
268 buffer
.length
= sizeof(union acpi_object
);
269 buffer
.pointer
= &element
;
270 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
271 if (ACPI_FAILURE(status
)) {
272 acpi_util_eval_error(handle
, pathname
, status
);
276 if (element
.type
!= ACPI_TYPE_INTEGER
) {
277 acpi_util_eval_error(handle
, pathname
, AE_BAD_DATA
);
281 *data
= element
.integer
.value
;
283 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%llu]\n", *data
));
288 EXPORT_SYMBOL(acpi_evaluate_integer
);
292 acpi_evaluate_string(acpi_handle handle
,
293 acpi_string pathname
,
294 acpi_object_list
* arguments
, acpi_string
* data
)
296 acpi_status status
= AE_OK
;
297 acpi_object
*element
= NULL
;
298 acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
302 return AE_BAD_PARAMETER
;
304 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
305 if (ACPI_FAILURE(status
)) {
306 acpi_util_eval_error(handle
, pathname
, status
);
310 element
= (acpi_object
*) buffer
.pointer
;
312 if ((element
->type
!= ACPI_TYPE_STRING
)
313 || (element
->type
!= ACPI_TYPE_BUFFER
)
314 || !element
->string
.length
) {
315 acpi_util_eval_error(handle
, pathname
, AE_BAD_DATA
);
319 *data
= kzalloc(element
->string
.length
+ 1, GFP_KERNEL
);
321 printk(KERN_ERR PREFIX
"Memory allocation\n");
325 memcpy(*data
, element
->string
.pointer
, element
->string
.length
);
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%s]\n", *data
));
329 kfree(buffer
.pointer
);
336 acpi_evaluate_reference(acpi_handle handle
,
337 acpi_string pathname
,
338 struct acpi_object_list
*arguments
,
339 struct acpi_handle_list
*list
)
341 acpi_status status
= AE_OK
;
342 union acpi_object
*package
= NULL
;
343 union acpi_object
*element
= NULL
;
344 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
349 return AE_BAD_PARAMETER
;
352 /* Evaluate object. */
354 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
355 if (ACPI_FAILURE(status
))
358 package
= buffer
.pointer
;
360 if ((buffer
.length
== 0) || !package
) {
361 printk(KERN_ERR PREFIX
"No return object (len %X ptr %p)\n",
362 (unsigned)buffer
.length
, package
);
363 status
= AE_BAD_DATA
;
364 acpi_util_eval_error(handle
, pathname
, status
);
367 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
368 printk(KERN_ERR PREFIX
"Expecting a [Package], found type %X\n",
370 status
= AE_BAD_DATA
;
371 acpi_util_eval_error(handle
, pathname
, status
);
374 if (!package
->package
.count
) {
375 printk(KERN_ERR PREFIX
"[Package] has zero elements (%p)\n",
377 status
= AE_BAD_DATA
;
378 acpi_util_eval_error(handle
, pathname
, status
);
382 if (package
->package
.count
> ACPI_MAX_HANDLES
) {
385 list
->count
= package
->package
.count
;
387 /* Extract package data. */
389 for (i
= 0; i
< list
->count
; i
++) {
391 element
= &(package
->package
.elements
[i
]);
393 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
394 status
= AE_BAD_DATA
;
395 printk(KERN_ERR PREFIX
396 "Expecting a [Reference] package element, found type %X\n",
398 acpi_util_eval_error(handle
, pathname
, status
);
402 if (!element
->reference
.handle
) {
403 printk(KERN_WARNING PREFIX
"Invalid reference in"
404 " package %s\n", pathname
);
405 status
= AE_NULL_ENTRY
;
408 /* Get the acpi_handle. */
410 list
->handles
[i
] = element
->reference
.handle
;
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found reference [%p]\n",
416 if (ACPI_FAILURE(status
)) {
418 //kfree(list->handles);
421 kfree(buffer
.pointer
);
426 EXPORT_SYMBOL(acpi_evaluate_reference
);