blob: e827be36ee8db99b4b8d648389cb7cc40df4064a [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 Brownf52fd662007-02-12 22:42:12 -050034ACPI_MODULE_NAME("utils");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* --------------------------------------------------------------------------
37 Object Evaluation Helpers
38 -------------------------------------------------------------------------- */
Harvey Harrison4fd7f512008-02-15 17:07:19 -080039static void
40acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
41{
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef ACPI_DEBUG_OUTPUT
Harvey Harrison4fd7f512008-02-15 17:07:19 -080043 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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#else
Harvey Harrison4fd7f512008-02-15 17:07:19 -080049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#endif
Harvey Harrison4fd7f512008-02-15 17:07:19 -080051}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040054acpi_extract_package(union acpi_object *package,
55 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Len Brown4be44fc2005-08-05 00:44:28 -040057 u32 size_required = 0;
58 u32 tail_offset = 0;
59 char *format_string = NULL;
60 u32 format_count = 0;
61 u32 i = 0;
62 u8 *head = NULL;
63 u8 *tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Len Brown4be44fc2005-08-05 00:44:28 -040066 if (!package || (package->type != ACPI_TYPE_PACKAGE)
67 || (package->package.count < 1)) {
Len Browncece9292006-06-26 23:04:31 -040068 printk(KERN_WARNING PREFIX "Invalid package argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040069 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 if (!format || !format->pointer || (format->length < 1)) {
Len Browncece9292006-06-26 23:04:31 -040073 printk(KERN_WARNING PREFIX "Invalid format argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040074 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
77 if (!buffer) {
Len Browncece9292006-06-26 23:04:31 -040078 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040079 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Len Brown4be44fc2005-08-05 00:44:28 -040082 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (format_count > package->package.count) {
Len Browncece9292006-06-26 23:04:31 -040084 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
85 " than exist in package [%d].\n",
86 format_count, package->package.count);
Patrick Mocheld550d982006-06-27 00:41:40 -040087 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
Jan Engelhardt50dd0962006-10-01 00:28:50 +020090 format_string = format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /*
93 * Calculate size_required.
94 */
Len Brown4be44fc2005-08-05 00:44:28 -040095 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 union acpi_object *element = &(package->package.elements[i]);
98
99 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400100 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
103 switch (element->type) {
104
105 case ACPI_TYPE_INTEGER:
106 switch (format_string[i]) {
107 case 'N':
108 size_required += sizeof(acpi_integer);
109 tail_offset += sizeof(acpi_integer);
110 break;
111 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400112 size_required +=
113 sizeof(char *) + sizeof(acpi_integer) +
114 sizeof(char);
115 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117 default:
Len Browncece9292006-06-26 23:04:31 -0400118 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400119 " [%d]: got number, expecing"
Len Browncece9292006-06-26 23:04:31 -0400120 " [%c]\n",
121 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
124 }
125 break;
126
127 case ACPI_TYPE_STRING:
128 case ACPI_TYPE_BUFFER:
129 switch (format_string[i]) {
130 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400131 size_required +=
132 sizeof(char *) +
133 (element->string.length * sizeof(char)) +
134 sizeof(char);
135 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
137 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400138 size_required +=
139 sizeof(u8 *) +
140 (element->buffer.length * sizeof(u8));
141 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143 default:
Len Browncece9292006-06-26 23:04:31 -0400144 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400145 " [%d] got string/buffer,"
Len Browncece9292006-06-26 23:04:31 -0400146 " expecing [%c]\n",
147 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150 }
151 break;
152
153 case ACPI_TYPE_PACKAGE:
154 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400155 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
156 "Found unsupported element at index=%d\n",
157 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400159 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 }
162 }
163
164 /*
165 * Validate output buffer.
166 */
167 if (buffer->length < size_required) {
168 buffer->length = size_required;
Patrick Mocheld550d982006-06-27 00:41:40 -0400169 return AE_BUFFER_OVERFLOW;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 } else if (buffer->length != size_required || !buffer->pointer) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 head = buffer->pointer;
175 tail = buffer->pointer + tail_offset;
176
177 /*
178 * Extract package data.
179 */
Len Brown4be44fc2005-08-05 00:44:28 -0400180 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 u8 **pointer = NULL;
183 union acpi_object *element = &(package->package.elements[i]);
184
185 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 switch (element->type) {
190
191 case ACPI_TYPE_INTEGER:
192 switch (format_string[i]) {
193 case 'N':
Len Brown4be44fc2005-08-05 00:44:28 -0400194 *((acpi_integer *) head) =
195 element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 head += sizeof(acpi_integer);
197 break;
198 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400199 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400201 *((acpi_integer *) tail) =
202 element->integer.value;
203 head += sizeof(acpi_integer *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 tail += sizeof(acpi_integer);
205 /* NULL terminate string */
206 *tail = (char)0;
207 tail += sizeof(char);
208 break;
209 default:
210 /* Should never get here */
211 break;
212 }
213 break;
214
215 case ACPI_TYPE_STRING:
216 case ACPI_TYPE_BUFFER:
217 switch (format_string[i]) {
218 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400219 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400221 memcpy(tail, element->string.pointer,
222 element->string.length);
223 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 tail += element->string.length * sizeof(char);
225 /* NULL terminate string */
226 *tail = (char)0;
227 tail += sizeof(char);
228 break;
229 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400230 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400232 memcpy(tail, element->buffer.pointer,
233 element->buffer.length);
234 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 tail += element->buffer.length * sizeof(u8);
236 break;
237 default:
238 /* Should never get here */
239 break;
240 }
241 break;
242
243 case ACPI_TYPE_PACKAGE:
244 /* TBD: handle nested packages... */
245 default:
246 /* Should never get here */
247 break;
248 }
249 }
250
Patrick Mocheld550d982006-06-27 00:41:40 -0400251 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Len Brown4be44fc2005-08-05 00:44:28 -0400253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254EXPORT_SYMBOL(acpi_extract_package);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400257acpi_evaluate_integer(acpi_handle handle,
258 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400259 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Len Brown4be44fc2005-08-05 00:44:28 -0400261 acpi_status status = AE_OK;
262 union acpi_object *element;
263 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Burman Yan36bcbec2006-12-19 12:56:11 -0800269 element = kzalloc(sizeof(union acpi_object), irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
Len Brown4be44fc2005-08-05 00:44:28 -0400270 if (!element)
Patrick Mocheld550d982006-06-27 00:41:40 -0400271 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 buffer.length = sizeof(union acpi_object);
274 buffer.pointer = element;
275 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
276 if (ACPI_FAILURE(status)) {
277 acpi_util_eval_error(handle, pathname, status);
Vasily Averin64385f22006-04-27 05:25:00 -0400278 kfree(element);
Patrick Mocheld550d982006-06-27 00:41:40 -0400279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
282 if (element->type != ACPI_TYPE_INTEGER) {
283 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Vasily Averin64385f22006-04-27 05:25:00 -0400284 kfree(element);
Patrick Mocheld550d982006-06-27 00:41:40 -0400285 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
288 *data = element->integer.value;
289 kfree(element);
290
Matthew Wilcox27663c52008-10-10 02:22:59 -0400291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Len Brown4be44fc2005-08-05 00:44:28 -0400296EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298#if 0
299acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400300acpi_evaluate_string(acpi_handle handle,
301 acpi_string pathname,
302 acpi_object_list * arguments, acpi_string * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Len Brown4be44fc2005-08-05 00:44:28 -0400304 acpi_status status = AE_OK;
305 acpi_object *element = NULL;
306 acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
313 if (ACPI_FAILURE(status)) {
314 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 element = (acpi_object *) buffer.pointer;
319
Len Brown4be44fc2005-08-05 00:44:28 -0400320 if ((element->type != ACPI_TYPE_STRING)
321 || (element->type != ACPI_TYPE_BUFFER)
322 || !element->string.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Burman Yan36bcbec2006-12-19 12:56:11 -0800327 *data = kzalloc(element->string.length + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!data) {
Len Brown64684632006-06-26 23:41:38 -0400329 printk(KERN_ERR PREFIX "Memory allocation\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400330 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 memcpy(*data, element->string.pointer, element->string.length);
334
335 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));
336
Len Brown02438d82006-06-30 03:19:10 -0400337 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Patrick Mocheld550d982006-06-27 00:41:40 -0400339 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341#endif
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400344acpi_evaluate_reference(acpi_handle handle,
345 acpi_string pathname,
346 struct acpi_object_list *arguments,
347 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Len Brown4be44fc2005-08-05 00:44:28 -0400349 acpi_status status = AE_OK;
350 union acpi_object *package = NULL;
351 union acpi_object *element = NULL;
352 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
353 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 /* Evaluate object. */
361
362 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
363 if (ACPI_FAILURE(status))
364 goto end;
365
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200366 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400369 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
370 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 status = AE_BAD_DATA;
372 acpi_util_eval_error(handle, pathname, status);
373 goto end;
374 }
375 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400376 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
377 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 status = AE_BAD_DATA;
379 acpi_util_eval_error(handle, pathname, status);
380 goto end;
381 }
382 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400383 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
384 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 status = AE_BAD_DATA;
386 acpi_util_eval_error(handle, pathname, status);
387 goto end;
388 }
389
390 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400391 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 list->count = package->package.count;
394
395 /* Extract package data. */
396
397 for (i = 0; i < list->count; i++) {
398
399 element = &(package->package.elements[i]);
400
Bob Moorecd0b2242008-04-10 19:06:43 +0400401 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 status = AE_BAD_DATA;
Len Brown64684632006-06-26 23:41:38 -0400403 printk(KERN_ERR PREFIX
404 "Expecting a [Reference] package element, found type %X\n",
405 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 acpi_util_eval_error(handle, pathname, status);
407 break;
408 }
409
Thomas Renningerb6a16382008-03-12 01:06:24 +0100410 if (!element->reference.handle) {
411 printk(KERN_WARNING PREFIX "Invalid reference in"
412 " package %s\n", pathname);
413 status = AE_NULL_ENTRY;
414 break;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* Get the acpi_handle. */
417
418 list->handles[i] = element->reference.handle;
419 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400420 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (ACPI_FAILURE(status)) {
425 list->count = 0;
426 //kfree(list->handles);
427 }
428
Len Brown02438d82006-06-30 03:19:10 -0400429 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Len Brown4be44fc2005-08-05 00:44:28 -0400434EXPORT_SYMBOL(acpi_evaluate_reference);