blob: d0d84c43a9d4c547591a6e43ffb8b8857dc90a63 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040034ACPI_MODULE_NAME("acpi_utils")
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* --------------------------------------------------------------------------
37 Object Evaluation Helpers
38 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040050acpi_extract_package(union acpi_object *package,
51 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Len Brown4be44fc2005-08-05 00:44:28 -040053 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Len Brown4be44fc2005-08-05 00:44:28 -040062 if (!package || (package->type != ACPI_TYPE_PACKAGE)
63 || (package->package.count < 1)) {
Len Browncece9292006-06-26 23:04:31 -040064 printk(KERN_WARNING PREFIX "Invalid package argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040065 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67
68 if (!format || !format->pointer || (format->length < 1)) {
Len Browncece9292006-06-26 23:04:31 -040069 printk(KERN_WARNING PREFIX "Invalid format argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040070 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
73 if (!buffer) {
Len Browncece9292006-06-26 23:04:31 -040074 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040075 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Len Brown4be44fc2005-08-05 00:44:28 -040078 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (format_count > package->package.count) {
Len Browncece9292006-06-26 23:04:31 -040080 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
81 " than exist in package [%d].\n",
82 format_count, package->package.count);
Patrick Mocheld550d982006-06-27 00:41:40 -040083 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Len Brown4be44fc2005-08-05 00:44:28 -040086 format_string = (char *)format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /*
89 * Calculate size_required.
90 */
Len Brown4be44fc2005-08-05 00:44:28 -040091 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 union acpi_object *element = &(package->package.elements[i]);
94
95 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -040096 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
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':
Len Brown4be44fc2005-08-05 00:44:28 -0400108 size_required +=
109 sizeof(char *) + sizeof(acpi_integer) +
110 sizeof(char);
111 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 default:
Len Browncece9292006-06-26 23:04:31 -0400114 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400115 " [%d]: got number, expecing"
Len Browncece9292006-06-26 23:04:31 -0400116 " [%c]\n",
117 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400118 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120 }
121 break;
122
123 case ACPI_TYPE_STRING:
124 case ACPI_TYPE_BUFFER:
125 switch (format_string[i]) {
126 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400127 size_required +=
128 sizeof(char *) +
129 (element->string.length * sizeof(char)) +
130 sizeof(char);
131 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
133 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400134 size_required +=
135 sizeof(u8 *) +
136 (element->buffer.length * sizeof(u8));
137 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 default:
Len Browncece9292006-06-26 23:04:31 -0400140 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400141 " [%d] got string/buffer,"
Len Browncece9292006-06-26 23:04:31 -0400142 " expecing [%c]\n",
143 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400144 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 }
147 break;
148
149 case ACPI_TYPE_PACKAGE:
150 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400151 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
152 "Found unsupported element at index=%d\n",
153 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400155 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 }
158 }
159
160 /*
161 * Validate output buffer.
162 */
163 if (buffer->length < size_required) {
164 buffer->length = size_required;
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return AE_BUFFER_OVERFLOW;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 } else if (buffer->length != size_required || !buffer->pointer) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400167 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
170 head = buffer->pointer;
171 tail = buffer->pointer + tail_offset;
172
173 /*
174 * Extract package data.
175 */
Len Brown4be44fc2005-08-05 00:44:28 -0400176 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 u8 **pointer = NULL;
179 union acpi_object *element = &(package->package.elements[i]);
180
181 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400182 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
185 switch (element->type) {
186
187 case ACPI_TYPE_INTEGER:
188 switch (format_string[i]) {
189 case 'N':
Len Brown4be44fc2005-08-05 00:44:28 -0400190 *((acpi_integer *) head) =
191 element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 head += sizeof(acpi_integer);
193 break;
194 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400195 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400197 *((acpi_integer *) tail) =
198 element->integer.value;
199 head += sizeof(acpi_integer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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':
Len Brown4be44fc2005-08-05 00:44:28 -0400215 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400217 memcpy(tail, element->string.pointer,
218 element->string.length);
219 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 tail += element->string.length * sizeof(char);
221 /* NULL terminate string */
222 *tail = (char)0;
223 tail += sizeof(char);
224 break;
225 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400226 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400228 memcpy(tail, element->buffer.pointer,
229 element->buffer.length);
230 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 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
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
Len Brown4be44fc2005-08-05 00:44:28 -0400249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250EXPORT_SYMBOL(acpi_extract_package);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400253acpi_evaluate_integer(acpi_handle handle,
254 acpi_string pathname,
255 struct acpi_object_list *arguments, unsigned long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Len Brown4be44fc2005-08-05 00:44:28 -0400257 acpi_status status = AE_OK;
258 union acpi_object *element;
259 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Len Brownd68909f2006-08-16 19:16:58 -0400265 element = kmalloc(sizeof(union acpi_object), irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400266 if (!element)
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
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);
Vasily Averin64385f22006-04-27 05:25:00 -0400275 kfree(element);
Patrick Mocheld550d982006-06-27 00:41:40 -0400276 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 if (element->type != ACPI_TYPE_INTEGER) {
280 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Vasily Averin64385f22006-04-27 05:25:00 -0400281 kfree(element);
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 *data = element->integer.value;
286 kfree(element);
287
288 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data));
289
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Len Brown4be44fc2005-08-05 00:44:28 -0400293EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295#if 0
296acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400297acpi_evaluate_string(acpi_handle handle,
298 acpi_string pathname,
299 acpi_object_list * arguments, acpi_string * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Len Brown4be44fc2005-08-05 00:44:28 -0400301 acpi_status status = AE_OK;
302 acpi_object *element = NULL;
303 acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
310 if (ACPI_FAILURE(status)) {
311 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 element = (acpi_object *) buffer.pointer;
316
Len Brown4be44fc2005-08-05 00:44:28 -0400317 if ((element->type != ACPI_TYPE_STRING)
318 || (element->type != ACPI_TYPE_BUFFER)
319 || !element->string.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400321 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
324 *data = kmalloc(element->string.length + 1, GFP_KERNEL);
325 if (!data) {
Len Brown64684632006-06-26 23:41:38 -0400326 printk(KERN_ERR PREFIX "Memory allocation\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
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
Len Brown02438d82006-06-30 03:19:10 -0400335 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339#endif
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400342acpi_evaluate_reference(acpi_handle handle,
343 acpi_string pathname,
344 struct acpi_object_list *arguments,
345 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Len Brown4be44fc2005-08-05 00:44:28 -0400347 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 /* Evaluate object. */
359
360 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
361 if (ACPI_FAILURE(status))
362 goto end;
363
Len Brown4be44fc2005-08-05 00:44:28 -0400364 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400367 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
368 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 status = AE_BAD_DATA;
370 acpi_util_eval_error(handle, pathname, status);
371 goto end;
372 }
373 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400374 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
375 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 status = AE_BAD_DATA;
377 acpi_util_eval_error(handle, pathname, status);
378 goto end;
379 }
380 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400381 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
382 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 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) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
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;
Len Brown64684632006-06-26 23:41:38 -0400401 printk(KERN_ERR PREFIX
402 "Expecting a [Reference] package element, found type %X\n",
403 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 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",
Len Brown4be44fc2005-08-05 00:44:28 -0400412 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ACPI_FAILURE(status)) {
417 list->count = 0;
418 //kfree(list->handles);
419 }
420
Len Brown02438d82006-06-30 03:19:10 -0400421 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Patrick Mocheld550d982006-06-27 00:41:40 -0400423 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Len Brown4be44fc2005-08-05 00:44:28 -0400426EXPORT_SYMBOL(acpi_evaluate_reference);