blob: c9a49f4747e675707e67aeeaa708fec550f2a5bf [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
Len Browna192a952009-07-28 16:45:54 -040033#include "internal.h"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050036ACPI_MODULE_NAME("utils");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* --------------------------------------------------------------------------
39 Object Evaluation Helpers
40 -------------------------------------------------------------------------- */
Harvey Harrison4fd7f512008-02-15 17:07:19 -080041static void
42acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
43{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef ACPI_DEBUG_OUTPUT
Harvey Harrison4fd7f512008-02-15 17:07:19 -080045 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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#else
Harvey Harrison4fd7f512008-02-15 17:07:19 -080051 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
Harvey Harrison4fd7f512008-02-15 17:07:19 -080053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040056acpi_extract_package(union acpi_object *package,
57 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Len Brown4be44fc2005-08-05 00:44:28 -040059 u32 size_required = 0;
60 u32 tail_offset = 0;
61 char *format_string = NULL;
62 u32 format_count = 0;
63 u32 i = 0;
64 u8 *head = NULL;
65 u8 *tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Len Brown4be44fc2005-08-05 00:44:28 -040068 if (!package || (package->type != ACPI_TYPE_PACKAGE)
69 || (package->package.count < 1)) {
Len Browncece9292006-06-26 23:04:31 -040070 printk(KERN_WARNING PREFIX "Invalid package argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040071 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
74 if (!format || !format->pointer || (format->length < 1)) {
Len Browncece9292006-06-26 23:04:31 -040075 printk(KERN_WARNING PREFIX "Invalid format argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040076 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
79 if (!buffer) {
Len Browncece9292006-06-26 23:04:31 -040080 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040081 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Len Brown4be44fc2005-08-05 00:44:28 -040084 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (format_count > package->package.count) {
Len Browncece9292006-06-26 23:04:31 -040086 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
87 " than exist in package [%d].\n",
88 format_count, package->package.count);
Patrick Mocheld550d982006-06-27 00:41:40 -040089 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Jan Engelhardt50dd0962006-10-01 00:28:50 +020092 format_string = format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /*
95 * Calculate size_required.
96 */
Len Brown4be44fc2005-08-05 00:44:28 -040097 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 union acpi_object *element = &(package->package.elements[i]);
100
101 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400102 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
105 switch (element->type) {
106
107 case ACPI_TYPE_INTEGER:
108 switch (format_string[i]) {
109 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800110 size_required += sizeof(u64);
111 tail_offset += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 break;
113 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400114 size_required +=
Lin Ming439913f2010-01-28 10:53:19 +0800115 sizeof(char *) + sizeof(u64) +
Len Brown4be44fc2005-08-05 00:44:28 -0400116 sizeof(char);
117 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 default:
Len Browncece9292006-06-26 23:04:31 -0400120 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400121 " [%d]: got number, expecing"
Len Browncece9292006-06-26 23:04:31 -0400122 " [%c]\n",
123 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126 }
127 break;
128
129 case ACPI_TYPE_STRING:
130 case ACPI_TYPE_BUFFER:
131 switch (format_string[i]) {
132 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400133 size_required +=
134 sizeof(char *) +
135 (element->string.length * sizeof(char)) +
136 sizeof(char);
137 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 break;
139 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400140 size_required +=
141 sizeof(u8 *) +
142 (element->buffer.length * sizeof(u8));
143 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
145 default:
Len Browncece9292006-06-26 23:04:31 -0400146 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400147 " [%d] got string/buffer,"
Len Browncece9292006-06-26 23:04:31 -0400148 " expecing [%c]\n",
149 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 break;
152 }
153 break;
154
155 case ACPI_TYPE_PACKAGE:
156 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400157 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
158 "Found unsupported element at index=%d\n",
159 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400161 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 }
164 }
165
166 /*
167 * Validate output buffer.
168 */
169 if (buffer->length < size_required) {
170 buffer->length = size_required;
Patrick Mocheld550d982006-06-27 00:41:40 -0400171 return AE_BUFFER_OVERFLOW;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 } else if (buffer->length != size_required || !buffer->pointer) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400173 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 head = buffer->pointer;
177 tail = buffer->pointer + tail_offset;
178
179 /*
180 * Extract package data.
181 */
Len Brown4be44fc2005-08-05 00:44:28 -0400182 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 u8 **pointer = NULL;
185 union acpi_object *element = &(package->package.elements[i]);
186
187 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400188 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 switch (element->type) {
192
193 case ACPI_TYPE_INTEGER:
194 switch (format_string[i]) {
195 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800196 *((u64 *) head) =
Len Brown4be44fc2005-08-05 00:44:28 -0400197 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800198 head += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400201 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *pointer = tail;
Lin Ming439913f2010-01-28 10:53:19 +0800203 *((u64 *) tail) =
Len Brown4be44fc2005-08-05 00:44:28 -0400204 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800205 head += sizeof(u64 *);
206 tail += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* NULL terminate string */
208 *tail = (char)0;
209 tail += sizeof(char);
210 break;
211 default:
212 /* Should never get here */
213 break;
214 }
215 break;
216
217 case ACPI_TYPE_STRING:
218 case ACPI_TYPE_BUFFER:
219 switch (format_string[i]) {
220 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400221 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400223 memcpy(tail, element->string.pointer,
224 element->string.length);
225 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 tail += element->string.length * sizeof(char);
227 /* NULL terminate string */
228 *tail = (char)0;
229 tail += sizeof(char);
230 break;
231 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400232 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400234 memcpy(tail, element->buffer.pointer,
235 element->buffer.length);
236 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 tail += element->buffer.length * sizeof(u8);
238 break;
239 default:
240 /* Should never get here */
241 break;
242 }
243 break;
244
245 case ACPI_TYPE_PACKAGE:
246 /* TBD: handle nested packages... */
247 default:
248 /* Should never get here */
249 break;
250 }
251 }
252
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
Len Brown4be44fc2005-08-05 00:44:28 -0400255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256EXPORT_SYMBOL(acpi_extract_package);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400259acpi_evaluate_integer(acpi_handle handle,
260 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400261 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Len Brown4be44fc2005-08-05 00:44:28 -0400263 acpi_status status = AE_OK;
Pavel Machek40599072008-11-25 12:05:08 +0100264 union acpi_object element;
Len Brown4be44fc2005-08-05 00:44:28 -0400265 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 buffer.length = sizeof(union acpi_object);
Pavel Machek40599072008-11-25 12:05:08 +0100271 buffer.pointer = &element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
273 if (ACPI_FAILURE(status)) {
274 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Pavel Machek40599072008-11-25 12:05:08 +0100278 if (element.type != ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Pavel Machek40599072008-11-25 12:05:08 +0100283 *data = element.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Matthew Wilcox27663c52008-10-10 02:22:59 -0400285 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Len Brown4be44fc2005-08-05 00:44:28 -0400290EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400293acpi_evaluate_reference(acpi_handle handle,
294 acpi_string pathname,
295 struct acpi_object_list *arguments,
296 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Len Brown4be44fc2005-08-05 00:44:28 -0400298 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 };
302 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 /* Evaluate object. */
310
311 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
312 if (ACPI_FAILURE(status))
313 goto end;
314
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200315 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400318 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
319 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 status = AE_BAD_DATA;
321 acpi_util_eval_error(handle, pathname, status);
322 goto end;
323 }
324 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400325 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
326 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 status = AE_BAD_DATA;
328 acpi_util_eval_error(handle, pathname, status);
329 goto end;
330 }
331 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400332 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
333 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 status = AE_BAD_DATA;
335 acpi_util_eval_error(handle, pathname, status);
336 goto end;
337 }
338
339 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400340 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 list->count = package->package.count;
343
344 /* Extract package data. */
345
346 for (i = 0; i < list->count; i++) {
347
348 element = &(package->package.elements[i]);
349
Bob Moorecd0b2242008-04-10 19:06:43 +0400350 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 status = AE_BAD_DATA;
Len Brown64684632006-06-26 23:41:38 -0400352 printk(KERN_ERR PREFIX
353 "Expecting a [Reference] package element, found type %X\n",
354 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 acpi_util_eval_error(handle, pathname, status);
356 break;
357 }
358
Thomas Renningerb6a16382008-03-12 01:06:24 +0100359 if (!element->reference.handle) {
360 printk(KERN_WARNING PREFIX "Invalid reference in"
361 " package %s\n", pathname);
362 status = AE_NULL_ENTRY;
363 break;
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* Get the acpi_handle. */
366
367 list->handles[i] = element->reference.handle;
368 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400369 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (ACPI_FAILURE(status)) {
374 list->count = 0;
375 //kfree(list->handles);
376 }
377
Len Brown02438d82006-06-30 03:19:10 -0400378 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Patrick Mocheld550d982006-06-27 00:41:40 -0400380 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Len Brown4be44fc2005-08-05 00:44:28 -0400383EXPORT_SYMBOL(acpi_evaluate_reference);