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>
35 #define _COMPONENT ACPI_BUS_COMPONENT
36 ACPI_MODULE_NAME("utils");
38 /* --------------------------------------------------------------------------
39 Object Evaluation Helpers
40 -------------------------------------------------------------------------- */
42 acpi_util_eval_error(acpi_handle h
, acpi_string p
, acpi_status s
)
44 #ifdef ACPI_DEBUG_OUTPUT
45 char prefix
[80] = {'\0'};
46 struct acpi_buffer buffer
= {sizeof(prefix
), prefix
};
47 acpi_get_name(h
, ACPI_FULL_PATHNAME
, &buffer
);
48 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Evaluate [%s.%s]: %s\n",
49 (char *) prefix
, p
, acpi_format_exception(s
)));
56 acpi_extract_package(union acpi_object
*package
,
57 struct acpi_buffer
*format
, struct acpi_buffer
*buffer
)
59 u32 size_required
= 0;
61 char *format_string
= NULL
;
68 if (!package
|| (package
->type
!= ACPI_TYPE_PACKAGE
)
69 || (package
->package
.count
< 1)) {
70 printk(KERN_WARNING PREFIX
"Invalid package argument\n");
71 return AE_BAD_PARAMETER
;
74 if (!format
|| !format
->pointer
|| (format
->length
< 1)) {
75 printk(KERN_WARNING PREFIX
"Invalid format argument\n");
76 return AE_BAD_PARAMETER
;
80 printk(KERN_WARNING PREFIX
"Invalid buffer argument\n");
81 return AE_BAD_PARAMETER
;
84 format_count
= (format
->length
/ sizeof(char)) - 1;
85 if (format_count
> package
->package
.count
) {
86 printk(KERN_WARNING PREFIX
"Format specifies more objects [%d]"
87 " than exist in package [%d].\n",
88 format_count
, package
->package
.count
);
92 format_string
= format
->pointer
;
95 * Calculate size_required.
97 for (i
= 0; i
< format_count
; i
++) {
99 union acpi_object
*element
= &(package
->package
.elements
[i
]);
105 switch (element
->type
) {
107 case ACPI_TYPE_INTEGER
:
108 switch (format_string
[i
]) {
110 size_required
+= sizeof(u64
);
111 tail_offset
+= sizeof(u64
);
115 sizeof(char *) + sizeof(u64
) +
117 tail_offset
+= sizeof(char *);
120 printk(KERN_WARNING PREFIX
"Invalid package element"
121 " [%d]: got number, expecing"
123 i
, format_string
[i
]);
129 case ACPI_TYPE_STRING
:
130 case ACPI_TYPE_BUFFER
:
131 switch (format_string
[i
]) {
135 (element
->string
.length
* sizeof(char)) +
137 tail_offset
+= sizeof(char *);
142 (element
->buffer
.length
* sizeof(u8
));
143 tail_offset
+= sizeof(u8
*);
146 printk(KERN_WARNING PREFIX
"Invalid package element"
147 " [%d] got string/buffer,"
149 i
, format_string
[i
]);
155 case ACPI_TYPE_PACKAGE
:
157 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
158 "Found unsupported element at index=%d\n",
160 /* TBD: handle nested packages... */
167 * Validate output buffer.
169 if (buffer
->length
< size_required
) {
170 buffer
->length
= size_required
;
171 return AE_BUFFER_OVERFLOW
;
172 } else if (buffer
->length
!= size_required
|| !buffer
->pointer
) {
173 return AE_BAD_PARAMETER
;
176 head
= buffer
->pointer
;
177 tail
= buffer
->pointer
+ tail_offset
;
180 * Extract package data.
182 for (i
= 0; i
< format_count
; i
++) {
185 union acpi_object
*element
= &(package
->package
.elements
[i
]);
191 switch (element
->type
) {
193 case ACPI_TYPE_INTEGER
:
194 switch (format_string
[i
]) {
197 element
->integer
.value
;
201 pointer
= (u8
**) head
;
204 element
->integer
.value
;
205 head
+= sizeof(u64
*);
207 /* NULL terminate string */
209 tail
+= sizeof(char);
212 /* Should never get here */
217 case ACPI_TYPE_STRING
:
218 case ACPI_TYPE_BUFFER
:
219 switch (format_string
[i
]) {
221 pointer
= (u8
**) head
;
223 memcpy(tail
, element
->string
.pointer
,
224 element
->string
.length
);
225 head
+= sizeof(char *);
226 tail
+= element
->string
.length
* sizeof(char);
227 /* NULL terminate string */
229 tail
+= sizeof(char);
232 pointer
= (u8
**) head
;
234 memcpy(tail
, element
->buffer
.pointer
,
235 element
->buffer
.length
);
236 head
+= sizeof(u8
*);
237 tail
+= element
->buffer
.length
* sizeof(u8
);
240 /* Should never get here */
245 case ACPI_TYPE_PACKAGE
:
246 /* TBD: handle nested packages... */
248 /* Should never get here */
256 EXPORT_SYMBOL(acpi_extract_package
);
259 acpi_evaluate_integer(acpi_handle handle
,
260 acpi_string pathname
,
261 struct acpi_object_list
*arguments
, unsigned long long *data
)
263 acpi_status status
= AE_OK
;
264 union acpi_object element
;
265 struct acpi_buffer buffer
= { 0, NULL
};
268 return AE_BAD_PARAMETER
;
270 buffer
.length
= sizeof(union acpi_object
);
271 buffer
.pointer
= &element
;
272 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
273 if (ACPI_FAILURE(status
)) {
274 acpi_util_eval_error(handle
, pathname
, status
);
278 if (element
.type
!= ACPI_TYPE_INTEGER
) {
279 acpi_util_eval_error(handle
, pathname
, AE_BAD_DATA
);
283 *data
= element
.integer
.value
;
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%llu]\n", *data
));
290 EXPORT_SYMBOL(acpi_evaluate_integer
);
293 acpi_evaluate_reference(acpi_handle handle
,
294 acpi_string pathname
,
295 struct acpi_object_list
*arguments
,
296 struct acpi_handle_list
*list
)
298 acpi_status status
= AE_OK
;
299 union acpi_object
*package
= NULL
;
300 union acpi_object
*element
= NULL
;
301 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
306 return AE_BAD_PARAMETER
;
309 /* Evaluate object. */
311 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
312 if (ACPI_FAILURE(status
))
315 package
= buffer
.pointer
;
317 if ((buffer
.length
== 0) || !package
) {
318 printk(KERN_ERR PREFIX
"No return object (len %X ptr %p)\n",
319 (unsigned)buffer
.length
, package
);
320 status
= AE_BAD_DATA
;
321 acpi_util_eval_error(handle
, pathname
, status
);
324 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
325 printk(KERN_ERR PREFIX
"Expecting a [Package], found type %X\n",
327 status
= AE_BAD_DATA
;
328 acpi_util_eval_error(handle
, pathname
, status
);
331 if (!package
->package
.count
) {
332 printk(KERN_ERR PREFIX
"[Package] has zero elements (%p)\n",
334 status
= AE_BAD_DATA
;
335 acpi_util_eval_error(handle
, pathname
, status
);
339 if (package
->package
.count
> ACPI_MAX_HANDLES
) {
342 list
->count
= package
->package
.count
;
344 /* Extract package data. */
346 for (i
= 0; i
< list
->count
; i
++) {
348 element
= &(package
->package
.elements
[i
]);
350 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
351 status
= AE_BAD_DATA
;
352 printk(KERN_ERR PREFIX
353 "Expecting a [Reference] package element, found type %X\n",
355 acpi_util_eval_error(handle
, pathname
, status
);
359 if (!element
->reference
.handle
) {
360 printk(KERN_WARNING PREFIX
"Invalid reference in"
361 " package %s\n", pathname
);
362 status
= AE_NULL_ENTRY
;
365 /* Get the acpi_handle. */
367 list
->handles
[i
] = element
->reference
.handle
;
368 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found reference [%p]\n",
373 if (ACPI_FAILURE(status
)) {
375 //kfree(list->handles);
378 kfree(buffer
.pointer
);
383 EXPORT_SYMBOL(acpi_evaluate_reference
);