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 | 60 | |
1da177e4 | 61 | |
4be44fcd LB |
62 | if (!package || (package->type != ACPI_TYPE_PACKAGE) |
63 | || (package->package.count < 1)) { | |
cece9296 | 64 | printk(KERN_WARNING PREFIX "Invalid package argument\n"); |
d550d98d | 65 | return AE_BAD_PARAMETER; |
1da177e4 LT |
66 | } |
67 | ||
68 | if (!format || !format->pointer || (format->length < 1)) { | |
cece9296 | 69 | printk(KERN_WARNING PREFIX "Invalid format argument\n"); |
d550d98d | 70 | return AE_BAD_PARAMETER; |
1da177e4 LT |
71 | } |
72 | ||
73 | if (!buffer) { | |
cece9296 | 74 | printk(KERN_WARNING PREFIX "Invalid buffer argument\n"); |
d550d98d | 75 | return AE_BAD_PARAMETER; |
1da177e4 LT |
76 | } |
77 | ||
4be44fcd | 78 | format_count = (format->length / sizeof(char)) - 1; |
1da177e4 | 79 | if (format_count > package->package.count) { |
cece9296 LB |
80 | printk(KERN_WARNING PREFIX "Format specifies more objects [%d]" |
81 | " than exist in package [%d].\n", | |
82 | format_count, package->package.count); | |
d550d98d | 83 | return AE_BAD_DATA; |
1da177e4 LT |
84 | } |
85 | ||
50dd0969 | 86 | format_string = format->pointer; |
1da177e4 LT |
87 | |
88 | /* | |
89 | * Calculate size_required. | |
90 | */ | |
4be44fcd | 91 | for (i = 0; i < format_count; i++) { |
1da177e4 LT |
92 | |
93 | union acpi_object *element = &(package->package.elements[i]); | |
94 | ||
95 | if (!element) { | |
d550d98d | 96 | return AE_BAD_DATA; |
1da177e4 LT |
97 | } |
98 | ||
99 | switch (element->type) { | |
100 | ||
101 | case ACPI_TYPE_INTEGER: | |
102 | switch (format_string[i]) { | |
103 | case 'N': | |
104 | size_required += sizeof(acpi_integer); | |
105 | tail_offset += sizeof(acpi_integer); | |
106 | break; | |
107 | case 'S': | |
4be44fcd LB |
108 | size_required += |
109 | sizeof(char *) + sizeof(acpi_integer) + | |
110 | sizeof(char); | |
111 | tail_offset += sizeof(char *); | |
1da177e4 LT |
112 | break; |
113 | default: | |
cece9296 | 114 | printk(KERN_WARNING PREFIX "Invalid package element" |
a6fc6720 | 115 | " [%d]: got number, expecing" |
cece9296 LB |
116 | " [%c]\n", |
117 | i, format_string[i]); | |
d550d98d | 118 | return AE_BAD_DATA; |
1da177e4 LT |
119 | break; |
120 | } | |
121 | break; | |
122 | ||
123 | case ACPI_TYPE_STRING: | |
124 | case ACPI_TYPE_BUFFER: | |
125 | switch (format_string[i]) { | |
126 | case 'S': | |
4be44fcd LB |
127 | size_required += |
128 | sizeof(char *) + | |
129 | (element->string.length * sizeof(char)) + | |
130 | sizeof(char); | |
131 | tail_offset += sizeof(char *); | |
1da177e4 LT |
132 | break; |
133 | case 'B': | |
4be44fcd LB |
134 | size_required += |
135 | sizeof(u8 *) + | |
136 | (element->buffer.length * sizeof(u8)); | |
137 | tail_offset += sizeof(u8 *); | |
1da177e4 LT |
138 | break; |
139 | default: | |
cece9296 | 140 | printk(KERN_WARNING PREFIX "Invalid package element" |
a6fc6720 | 141 | " [%d] got string/buffer," |
cece9296 LB |
142 | " expecing [%c]\n", |
143 | i, format_string[i]); | |
d550d98d | 144 | return AE_BAD_DATA; |
1da177e4 LT |
145 | break; |
146 | } | |
147 | break; | |
148 | ||
149 | case ACPI_TYPE_PACKAGE: | |
150 | default: | |
4be44fcd LB |
151 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
152 | "Found unsupported element at index=%d\n", | |
153 | i)); | |
1da177e4 | 154 | /* TBD: handle nested packages... */ |
d550d98d | 155 | return AE_SUPPORT; |
1da177e4 LT |
156 | break; |
157 | } | |
158 | } | |
159 | ||
160 | /* | |
161 | * Validate output buffer. | |
162 | */ | |
163 | if (buffer->length < size_required) { | |
164 | buffer->length = size_required; | |
d550d98d | 165 | return AE_BUFFER_OVERFLOW; |
4be44fcd | 166 | } else if (buffer->length != size_required || !buffer->pointer) { |
d550d98d | 167 | return AE_BAD_PARAMETER; |
1da177e4 LT |
168 | } |
169 | ||
170 | head = buffer->pointer; | |
171 | tail = buffer->pointer + tail_offset; | |
172 | ||
173 | /* | |
174 | * Extract package data. | |
175 | */ | |
4be44fcd | 176 | for (i = 0; i < format_count; i++) { |
1da177e4 LT |
177 | |
178 | u8 **pointer = NULL; | |
179 | union acpi_object *element = &(package->package.elements[i]); | |
180 | ||
181 | if (!element) { | |
d550d98d | 182 | return AE_BAD_DATA; |
1da177e4 LT |
183 | } |
184 | ||
185 | switch (element->type) { | |
186 | ||
187 | case ACPI_TYPE_INTEGER: | |
188 | switch (format_string[i]) { | |
189 | case 'N': | |
4be44fcd LB |
190 | *((acpi_integer *) head) = |
191 | element->integer.value; | |
1da177e4 LT |
192 | head += sizeof(acpi_integer); |
193 | break; | |
194 | case 'S': | |
4be44fcd | 195 | pointer = (u8 **) head; |
1da177e4 | 196 | *pointer = tail; |
4be44fcd LB |
197 | *((acpi_integer *) tail) = |
198 | element->integer.value; | |
199 | head += sizeof(acpi_integer *); | |
1da177e4 LT |
200 | tail += sizeof(acpi_integer); |
201 | /* NULL terminate string */ | |
202 | *tail = (char)0; | |
203 | tail += sizeof(char); | |
204 | break; | |
205 | default: | |
206 | /* Should never get here */ | |
207 | break; | |
208 | } | |
209 | break; | |
210 | ||
211 | case ACPI_TYPE_STRING: | |
212 | case ACPI_TYPE_BUFFER: | |
213 | switch (format_string[i]) { | |
214 | case 'S': | |
4be44fcd | 215 | pointer = (u8 **) head; |
1da177e4 | 216 | *pointer = tail; |
4be44fcd LB |
217 | memcpy(tail, element->string.pointer, |
218 | element->string.length); | |
219 | head += sizeof(char *); | |
1da177e4 LT |
220 | tail += element->string.length * sizeof(char); |
221 | /* NULL terminate string */ | |
222 | *tail = (char)0; | |
223 | tail += sizeof(char); | |
224 | break; | |
225 | case 'B': | |
4be44fcd | 226 | pointer = (u8 **) head; |
1da177e4 | 227 | *pointer = tail; |
4be44fcd LB |
228 | memcpy(tail, element->buffer.pointer, |
229 | element->buffer.length); | |
230 | head += sizeof(u8 *); | |
1da177e4 LT |
231 | tail += element->buffer.length * sizeof(u8); |
232 | break; | |
233 | default: | |
234 | /* Should never get here */ | |
235 | break; | |
236 | } | |
237 | break; | |
238 | ||
239 | case ACPI_TYPE_PACKAGE: | |
240 | /* TBD: handle nested packages... */ | |
241 | default: | |
242 | /* Should never get here */ | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
d550d98d | 247 | return AE_OK; |
1da177e4 | 248 | } |
1da177e4 | 249 | |
4be44fcd | 250 | EXPORT_SYMBOL(acpi_extract_package); |
1da177e4 LT |
251 | |
252 | acpi_status | |
4be44fcd LB |
253 | acpi_evaluate_integer(acpi_handle handle, |
254 | acpi_string pathname, | |
255 | struct acpi_object_list *arguments, unsigned long *data) | |
1da177e4 | 256 | { |
4be44fcd LB |
257 | acpi_status status = AE_OK; |
258 | union acpi_object *element; | |
259 | struct acpi_buffer buffer = { 0, NULL }; | |
1da177e4 | 260 | |
1da177e4 LT |
261 | |
262 | if (!data) | |
d550d98d | 263 | return AE_BAD_PARAMETER; |
1da177e4 | 264 | |
d68909f4 | 265 | element = kmalloc(sizeof(union acpi_object), irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL); |
4be44fcd | 266 | if (!element) |
d550d98d | 267 | return AE_NO_MEMORY; |
1da177e4 LT |
268 | |
269 | memset(element, 0, sizeof(union acpi_object)); | |
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); | |
64385f2f | 275 | kfree(element); |
d550d98d | 276 | return status; |
1da177e4 LT |
277 | } |
278 | ||
279 | if (element->type != ACPI_TYPE_INTEGER) { | |
280 | acpi_util_eval_error(handle, pathname, AE_BAD_DATA); | |
64385f2f | 281 | kfree(element); |
d550d98d | 282 | return AE_BAD_DATA; |
1da177e4 LT |
283 | } |
284 | ||
285 | *data = element->integer.value; | |
286 | kfree(element); | |
287 | ||
288 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data)); | |
289 | ||
d550d98d | 290 | return AE_OK; |
1da177e4 | 291 | } |
1da177e4 | 292 | |
4be44fcd | 293 | EXPORT_SYMBOL(acpi_evaluate_integer); |
1da177e4 LT |
294 | |
295 | #if 0 | |
296 | acpi_status | |
4be44fcd LB |
297 | acpi_evaluate_string(acpi_handle handle, |
298 | acpi_string pathname, | |
299 | acpi_object_list * arguments, acpi_string * data) | |
1da177e4 | 300 | { |
4be44fcd LB |
301 | acpi_status status = AE_OK; |
302 | acpi_object *element = NULL; | |
303 | acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 304 | |
1da177e4 LT |
305 | |
306 | if (!data) | |
d550d98d | 307 | return AE_BAD_PARAMETER; |
1da177e4 LT |
308 | |
309 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); | |
310 | if (ACPI_FAILURE(status)) { | |
311 | acpi_util_eval_error(handle, pathname, status); | |
d550d98d | 312 | return status; |
1da177e4 LT |
313 | } |
314 | ||
315 | element = (acpi_object *) buffer.pointer; | |
316 | ||
4be44fcd LB |
317 | if ((element->type != ACPI_TYPE_STRING) |
318 | || (element->type != ACPI_TYPE_BUFFER) | |
319 | || !element->string.length) { | |
1da177e4 | 320 | acpi_util_eval_error(handle, pathname, AE_BAD_DATA); |
d550d98d | 321 | return AE_BAD_DATA; |
1da177e4 LT |
322 | } |
323 | ||
324 | *data = kmalloc(element->string.length + 1, GFP_KERNEL); | |
325 | if (!data) { | |
6468463a | 326 | printk(KERN_ERR PREFIX "Memory allocation\n"); |
d550d98d | 327 | return -ENOMEM; |
1da177e4 LT |
328 | } |
329 | memset(*data, 0, element->string.length + 1); | |
330 | ||
331 | memcpy(*data, element->string.pointer, element->string.length); | |
332 | ||
333 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data)); | |
334 | ||
02438d87 | 335 | kfree(buffer.pointer); |
1da177e4 | 336 | |
d550d98d | 337 | return AE_OK; |
1da177e4 LT |
338 | } |
339 | #endif | |
340 | ||
1da177e4 | 341 | acpi_status |
4be44fcd LB |
342 | acpi_evaluate_reference(acpi_handle handle, |
343 | acpi_string pathname, | |
344 | struct acpi_object_list *arguments, | |
345 | struct acpi_handle_list *list) | |
1da177e4 | 346 | { |
4be44fcd LB |
347 | acpi_status status = AE_OK; |
348 | union acpi_object *package = NULL; | |
349 | union acpi_object *element = NULL; | |
350 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
351 | u32 i = 0; | |
1da177e4 | 352 | |
1da177e4 LT |
353 | |
354 | if (!list) { | |
d550d98d | 355 | return AE_BAD_PARAMETER; |
1da177e4 LT |
356 | } |
357 | ||
358 | /* Evaluate object. */ | |
359 | ||
360 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); | |
361 | if (ACPI_FAILURE(status)) | |
362 | goto end; | |
363 | ||
50dd0969 | 364 | package = buffer.pointer; |
1da177e4 LT |
365 | |
366 | if ((buffer.length == 0) || !package) { | |
6468463a LB |
367 | printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n", |
368 | (unsigned)buffer.length, package); | |
1da177e4 LT |
369 | status = AE_BAD_DATA; |
370 | acpi_util_eval_error(handle, pathname, status); | |
371 | goto end; | |
372 | } | |
373 | if (package->type != ACPI_TYPE_PACKAGE) { | |
6468463a LB |
374 | printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n", |
375 | package->type); | |
1da177e4 LT |
376 | status = AE_BAD_DATA; |
377 | acpi_util_eval_error(handle, pathname, status); | |
378 | goto end; | |
379 | } | |
380 | if (!package->package.count) { | |
6468463a LB |
381 | printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n", |
382 | package); | |
1da177e4 LT |
383 | status = AE_BAD_DATA; |
384 | acpi_util_eval_error(handle, pathname, status); | |
385 | goto end; | |
386 | } | |
387 | ||
388 | if (package->package.count > ACPI_MAX_HANDLES) { | |
d550d98d | 389 | return AE_NO_MEMORY; |
1da177e4 LT |
390 | } |
391 | list->count = package->package.count; | |
392 | ||
393 | /* Extract package data. */ | |
394 | ||
395 | for (i = 0; i < list->count; i++) { | |
396 | ||
397 | element = &(package->package.elements[i]); | |
398 | ||
399 | if (element->type != ACPI_TYPE_ANY) { | |
400 | status = AE_BAD_DATA; | |
6468463a LB |
401 | printk(KERN_ERR PREFIX |
402 | "Expecting a [Reference] package element, found type %X\n", | |
403 | element->type); | |
1da177e4 LT |
404 | acpi_util_eval_error(handle, pathname, status); |
405 | break; | |
406 | } | |
407 | ||
408 | /* Get the acpi_handle. */ | |
409 | ||
410 | list->handles[i] = element->reference.handle; | |
411 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n", | |
4be44fcd | 412 | list->handles[i])); |
1da177e4 LT |
413 | } |
414 | ||
4be44fcd | 415 | end: |
1da177e4 LT |
416 | if (ACPI_FAILURE(status)) { |
417 | list->count = 0; | |
418 | //kfree(list->handles); | |
419 | } | |
420 | ||
02438d87 | 421 | kfree(buffer.pointer); |
1da177e4 | 422 | |
d550d98d | 423 | return status; |
1da177e4 | 424 | } |
1da177e4 | 425 | |
4be44fcd | 426 | EXPORT_SYMBOL(acpi_evaluate_reference); |