Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 | 33 | #define _COMPONENT ACPI_BUS_COMPONENT |
4be44fcd | 34 | ACPI_MODULE_NAME("acpi_utils") |
1da177e4 LT |
35 | |
36 | /* -------------------------------------------------------------------------- | |
37 | Object Evaluation Helpers | |
38 | -------------------------------------------------------------------------- */ | |
1da177e4 LT |
39 | #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 | |
1da177e4 | 49 | acpi_status |
4be44fcd LB |
50 | acpi_extract_package(union acpi_object *package, |
51 | struct acpi_buffer *format, struct acpi_buffer *buffer) | |
1da177e4 | 52 | { |
4be44fcd LB |
53 | 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; | |
1da177e4 LT |
60 | |
61 | ACPI_FUNCTION_TRACE("acpi_extract_package"); | |
62 | ||
4be44fcd LB |
63 | if (!package || (package->type != ACPI_TYPE_PACKAGE) |
64 | || (package->package.count < 1)) { | |
cece9296 | 65 | printk(KERN_WARNING PREFIX "Invalid package argument\n"); |
1da177e4 LT |
66 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
67 | } | |
68 | ||
69 | if (!format || !format->pointer || (format->length < 1)) { | |
cece9296 | 70 | printk(KERN_WARNING PREFIX "Invalid format argument\n"); |
1da177e4 LT |
71 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
72 | } | |
73 | ||
74 | if (!buffer) { | |
cece9296 | 75 | printk(KERN_WARNING PREFIX "Invalid buffer argument\n"); |
1da177e4 LT |
76 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
77 | } | |
78 | ||
4be44fcd | 79 | format_count = (format->length / sizeof(char)) - 1; |
1da177e4 | 80 | if (format_count > package->package.count) { |
cece9296 LB |
81 | printk(KERN_WARNING PREFIX "Format specifies more objects [%d]" |
82 | " than exist in package [%d].\n", | |
83 | format_count, package->package.count); | |
1da177e4 LT |
84 | return_ACPI_STATUS(AE_BAD_DATA); |
85 | } | |
86 | ||
4be44fcd | 87 | format_string = (char *)format->pointer; |
1da177e4 LT |
88 | |
89 | /* | |
90 | * Calculate size_required. | |
91 | */ | |
4be44fcd | 92 | for (i = 0; i < format_count; i++) { |
1da177e4 LT |
93 | |
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': | |
4be44fcd LB |
109 | size_required += |
110 | sizeof(char *) + sizeof(acpi_integer) + | |
111 | sizeof(char); | |
112 | tail_offset += sizeof(char *); | |
1da177e4 LT |
113 | break; |
114 | default: | |
cece9296 | 115 | printk(KERN_WARNING PREFIX "Invalid package element" |
a6fc6720 | 116 | " [%d]: got number, expecing" |
cece9296 LB |
117 | " [%c]\n", |
118 | i, format_string[i]); | |
1da177e4 LT |
119 | 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': | |
4be44fcd LB |
128 | size_required += |
129 | sizeof(char *) + | |
130 | (element->string.length * sizeof(char)) + | |
131 | sizeof(char); | |
132 | tail_offset += sizeof(char *); | |
1da177e4 LT |
133 | break; |
134 | case 'B': | |
4be44fcd LB |
135 | size_required += |
136 | sizeof(u8 *) + | |
137 | (element->buffer.length * sizeof(u8)); | |
138 | tail_offset += sizeof(u8 *); | |
1da177e4 LT |
139 | break; |
140 | default: | |
cece9296 | 141 | printk(KERN_WARNING PREFIX "Invalid package element" |
a6fc6720 | 142 | " [%d] got string/buffer," |
cece9296 LB |
143 | " expecing [%c]\n", |
144 | i, format_string[i]); | |
1da177e4 LT |
145 | return_ACPI_STATUS(AE_BAD_DATA); |
146 | break; | |
147 | } | |
148 | break; | |
149 | ||
150 | case ACPI_TYPE_PACKAGE: | |
151 | default: | |
4be44fcd LB |
152 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
153 | "Found unsupported element at index=%d\n", | |
154 | i)); | |
1da177e4 LT |
155 | /* 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); | |
4be44fcd | 167 | } else if (buffer->length != size_required || !buffer->pointer) { |
1da177e4 LT |
168 | 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 | */ | |
4be44fcd | 177 | for (i = 0; i < format_count; i++) { |
1da177e4 LT |
178 | |
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': | |
4be44fcd LB |
191 | *((acpi_integer *) head) = |
192 | element->integer.value; | |
1da177e4 LT |
193 | head += sizeof(acpi_integer); |
194 | break; | |
195 | case 'S': | |
4be44fcd | 196 | pointer = (u8 **) head; |
1da177e4 | 197 | *pointer = tail; |
4be44fcd LB |
198 | *((acpi_integer *) tail) = |
199 | element->integer.value; | |
200 | head += sizeof(acpi_integer *); | |
1da177e4 LT |
201 | 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': | |
4be44fcd | 216 | pointer = (u8 **) head; |
1da177e4 | 217 | *pointer = tail; |
4be44fcd LB |
218 | memcpy(tail, element->string.pointer, |
219 | element->string.length); | |
220 | head += sizeof(char *); | |
1da177e4 LT |
221 | tail += element->string.length * sizeof(char); |
222 | /* NULL terminate string */ | |
223 | *tail = (char)0; | |
224 | tail += sizeof(char); | |
225 | break; | |
226 | case 'B': | |
4be44fcd | 227 | pointer = (u8 **) head; |
1da177e4 | 228 | *pointer = tail; |
4be44fcd LB |
229 | memcpy(tail, element->buffer.pointer, |
230 | element->buffer.length); | |
231 | head += sizeof(u8 *); | |
1da177e4 LT |
232 | 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 | } | |
1da177e4 | 250 | |
4be44fcd | 251 | EXPORT_SYMBOL(acpi_extract_package); |
1da177e4 LT |
252 | |
253 | acpi_status | |
4be44fcd LB |
254 | acpi_evaluate_integer(acpi_handle handle, |
255 | acpi_string pathname, | |
256 | struct acpi_object_list *arguments, unsigned long *data) | |
1da177e4 | 257 | { |
4be44fcd LB |
258 | acpi_status status = AE_OK; |
259 | union acpi_object *element; | |
260 | struct acpi_buffer buffer = { 0, NULL }; | |
1da177e4 LT |
261 | |
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); | |
4be44fcd | 268 | if (!element) |
1da177e4 LT |
269 | 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); | |
64385f2f | 277 | kfree(element); |
1da177e4 LT |
278 | return_ACPI_STATUS(status); |
279 | } | |
280 | ||
281 | if (element->type != ACPI_TYPE_INTEGER) { | |
282 | acpi_util_eval_error(handle, pathname, AE_BAD_DATA); | |
64385f2f | 283 | kfree(element); |
1da177e4 LT |
284 | 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 | } | |
1da177e4 | 294 | |
4be44fcd | 295 | EXPORT_SYMBOL(acpi_evaluate_integer); |
1da177e4 LT |
296 | |
297 | #if 0 | |
298 | acpi_status | |
4be44fcd LB |
299 | acpi_evaluate_string(acpi_handle handle, |
300 | acpi_string pathname, | |
301 | acpi_object_list * arguments, acpi_string * data) | |
1da177e4 | 302 | { |
4be44fcd LB |
303 | acpi_status status = AE_OK; |
304 | acpi_object *element = NULL; | |
305 | acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 LT |
306 | |
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 | ||
4be44fcd LB |
320 | if ((element->type != ACPI_TYPE_STRING) |
321 | || (element->type != ACPI_TYPE_BUFFER) | |
322 | || !element->string.length) { | |
1da177e4 LT |
323 | 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) { | |
6468463a | 329 | printk(KERN_ERR PREFIX "Memory allocation\n"); |
1da177e4 LT |
330 | 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 | ||
1da177e4 | 344 | acpi_status |
4be44fcd LB |
345 | acpi_evaluate_reference(acpi_handle handle, |
346 | acpi_string pathname, | |
347 | struct acpi_object_list *arguments, | |
348 | struct acpi_handle_list *list) | |
1da177e4 | 349 | { |
4be44fcd LB |
350 | 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; | |
1da177e4 LT |
355 | |
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 | ||
4be44fcd | 368 | package = (union acpi_object *)buffer.pointer; |
1da177e4 LT |
369 | |
370 | if ((buffer.length == 0) || !package) { | |
6468463a LB |
371 | printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n", |
372 | (unsigned)buffer.length, package); | |
1da177e4 LT |
373 | status = AE_BAD_DATA; |
374 | acpi_util_eval_error(handle, pathname, status); | |
375 | goto end; | |
376 | } | |
377 | if (package->type != ACPI_TYPE_PACKAGE) { | |
6468463a LB |
378 | printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n", |
379 | package->type); | |
1da177e4 LT |
380 | status = AE_BAD_DATA; |
381 | acpi_util_eval_error(handle, pathname, status); | |
382 | goto end; | |
383 | } | |
384 | if (!package->package.count) { | |
6468463a LB |
385 | printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n", |
386 | package); | |
1da177e4 LT |
387 | 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; | |
6468463a LB |
405 | printk(KERN_ERR PREFIX |
406 | "Expecting a [Reference] package element, found type %X\n", | |
407 | element->type); | |
1da177e4 LT |
408 | 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", | |
4be44fcd | 416 | list->handles[i])); |
1da177e4 LT |
417 | } |
418 | ||
4be44fcd | 419 | end: |
1da177e4 LT |
420 | 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 | } | |
1da177e4 | 429 | |
4be44fcd | 430 | EXPORT_SYMBOL(acpi_evaluate_reference); |