utils.c 9.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
 *
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
28
#include <linux/slab.h>
L
Linus Torvalds 已提交
29 30 31 32 33
#include <linux/init.h>
#include <linux/types.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

34 35
#include "internal.h"

L
Linus Torvalds 已提交
36
#define _COMPONENT		ACPI_BUS_COMPONENT
37
ACPI_MODULE_NAME("utils");
L
Linus Torvalds 已提交
38 39 40 41

/* --------------------------------------------------------------------------
                            Object Evaluation Helpers
   -------------------------------------------------------------------------- */
42 43 44
static void
acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
{
L
Linus Torvalds 已提交
45
#ifdef ACPI_DEBUG_OUTPUT
46 47 48 49 50
	char prefix[80] = {'\0'};
	struct acpi_buffer buffer = {sizeof(prefix), prefix};
	acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
		(char *) prefix, p, acpi_format_exception(s)));
L
Linus Torvalds 已提交
51
#else
52
	return;
L
Linus Torvalds 已提交
53
#endif
54 55
}

L
Linus Torvalds 已提交
56
acpi_status
L
Len Brown 已提交
57 58
acpi_extract_package(union acpi_object *package,
		     struct acpi_buffer *format, struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
59
{
L
Len Brown 已提交
60 61 62 63 64 65 66
	u32 size_required = 0;
	u32 tail_offset = 0;
	char *format_string = NULL;
	u32 format_count = 0;
	u32 i = 0;
	u8 *head = NULL;
	u8 *tail = NULL;
L
Linus Torvalds 已提交
67 68


L
Len Brown 已提交
69 70
	if (!package || (package->type != ACPI_TYPE_PACKAGE)
	    || (package->package.count < 1)) {
71
		printk(KERN_WARNING PREFIX "Invalid package argument\n");
72
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
73 74 75
	}

	if (!format || !format->pointer || (format->length < 1)) {
76
		printk(KERN_WARNING PREFIX "Invalid format argument\n");
77
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
78 79 80
	}

	if (!buffer) {
81
		printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
82
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
83 84
	}

L
Len Brown 已提交
85
	format_count = (format->length / sizeof(char)) - 1;
L
Linus Torvalds 已提交
86
	if (format_count > package->package.count) {
87 88 89
		printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
			      " than exist in package [%d].\n",
			      format_count, package->package.count);
90
		return AE_BAD_DATA;
L
Linus Torvalds 已提交
91 92
	}

93
	format_string = format->pointer;
L
Linus Torvalds 已提交
94 95 96 97

	/*
	 * Calculate size_required.
	 */
L
Len Brown 已提交
98
	for (i = 0; i < format_count; i++) {
L
Linus Torvalds 已提交
99 100 101 102

		union acpi_object *element = &(package->package.elements[i]);

		if (!element) {
103
			return AE_BAD_DATA;
L
Linus Torvalds 已提交
104 105 106 107 108 109 110
		}

		switch (element->type) {

		case ACPI_TYPE_INTEGER:
			switch (format_string[i]) {
			case 'N':
L
Lin Ming 已提交
111 112
				size_required += sizeof(u64);
				tail_offset += sizeof(u64);
L
Linus Torvalds 已提交
113 114
				break;
			case 'S':
L
Len Brown 已提交
115
				size_required +=
L
Lin Ming 已提交
116
				    sizeof(char *) + sizeof(u64) +
L
Len Brown 已提交
117 118
				    sizeof(char);
				tail_offset += sizeof(char *);
L
Linus Torvalds 已提交
119 120
				break;
			default:
121
				printk(KERN_WARNING PREFIX "Invalid package element"
122
					      " [%d]: got number, expecing"
123 124
					      " [%c]\n",
					      i, format_string[i]);
125
				return AE_BAD_DATA;
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133
				break;
			}
			break;

		case ACPI_TYPE_STRING:
		case ACPI_TYPE_BUFFER:
			switch (format_string[i]) {
			case 'S':
L
Len Brown 已提交
134 135 136 137 138
				size_required +=
				    sizeof(char *) +
				    (element->string.length * sizeof(char)) +
				    sizeof(char);
				tail_offset += sizeof(char *);
L
Linus Torvalds 已提交
139 140
				break;
			case 'B':
L
Len Brown 已提交
141 142 143 144
				size_required +=
				    sizeof(u8 *) +
				    (element->buffer.length * sizeof(u8));
				tail_offset += sizeof(u8 *);
L
Linus Torvalds 已提交
145 146
				break;
			default:
147
				printk(KERN_WARNING PREFIX "Invalid package element"
148
					      " [%d] got string/buffer,"
149 150
					      " expecing [%c]\n",
					      i, format_string[i]);
151
				return AE_BAD_DATA;
L
Linus Torvalds 已提交
152 153 154 155 156 157
				break;
			}
			break;

		case ACPI_TYPE_PACKAGE:
		default:
L
Len Brown 已提交
158 159 160
			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
					  "Found unsupported element at index=%d\n",
					  i));
L
Linus Torvalds 已提交
161
			/* TBD: handle nested packages... */
162
			return AE_SUPPORT;
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171
			break;
		}
	}

	/*
	 * Validate output buffer.
	 */
	if (buffer->length < size_required) {
		buffer->length = size_required;
172
		return AE_BUFFER_OVERFLOW;
L
Len Brown 已提交
173
	} else if (buffer->length != size_required || !buffer->pointer) {
174
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182
	}

	head = buffer->pointer;
	tail = buffer->pointer + tail_offset;

	/*
	 * Extract package data.
	 */
L
Len Brown 已提交
183
	for (i = 0; i < format_count; i++) {
L
Linus Torvalds 已提交
184 185 186 187 188

		u8 **pointer = NULL;
		union acpi_object *element = &(package->package.elements[i]);

		if (!element) {
189
			return AE_BAD_DATA;
L
Linus Torvalds 已提交
190 191 192 193 194 195 196
		}

		switch (element->type) {

		case ACPI_TYPE_INTEGER:
			switch (format_string[i]) {
			case 'N':
L
Lin Ming 已提交
197
				*((u64 *) head) =
L
Len Brown 已提交
198
				    element->integer.value;
L
Lin Ming 已提交
199
				head += sizeof(u64);
L
Linus Torvalds 已提交
200 201
				break;
			case 'S':
L
Len Brown 已提交
202
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
203
				*pointer = tail;
L
Lin Ming 已提交
204
				*((u64 *) tail) =
L
Len Brown 已提交
205
				    element->integer.value;
L
Lin Ming 已提交
206 207
				head += sizeof(u64 *);
				tail += sizeof(u64);
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
				/* NULL terminate string */
				*tail = (char)0;
				tail += sizeof(char);
				break;
			default:
				/* Should never get here */
				break;
			}
			break;

		case ACPI_TYPE_STRING:
		case ACPI_TYPE_BUFFER:
			switch (format_string[i]) {
			case 'S':
L
Len Brown 已提交
222
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
223
				*pointer = tail;
L
Len Brown 已提交
224 225 226
				memcpy(tail, element->string.pointer,
				       element->string.length);
				head += sizeof(char *);
L
Linus Torvalds 已提交
227 228 229 230 231 232
				tail += element->string.length * sizeof(char);
				/* NULL terminate string */
				*tail = (char)0;
				tail += sizeof(char);
				break;
			case 'B':
L
Len Brown 已提交
233
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
234
				*pointer = tail;
L
Len Brown 已提交
235 236 237
				memcpy(tail, element->buffer.pointer,
				       element->buffer.length);
				head += sizeof(u8 *);
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
				tail += element->buffer.length * sizeof(u8);
				break;
			default:
				/* Should never get here */
				break;
			}
			break;

		case ACPI_TYPE_PACKAGE:
			/* TBD: handle nested packages... */
		default:
			/* Should never get here */
			break;
		}
	}

254
	return AE_OK;
L
Linus Torvalds 已提交
255 256
}

L
Len Brown 已提交
257
EXPORT_SYMBOL(acpi_extract_package);
L
Linus Torvalds 已提交
258 259

acpi_status
L
Len Brown 已提交
260 261
acpi_evaluate_integer(acpi_handle handle,
		      acpi_string pathname,
262
		      struct acpi_object_list *arguments, unsigned long long *data)
L
Linus Torvalds 已提交
263
{
L
Len Brown 已提交
264
	acpi_status status = AE_OK;
265
	union acpi_object element;
L
Len Brown 已提交
266
	struct acpi_buffer buffer = { 0, NULL };
L
Linus Torvalds 已提交
267 268

	if (!data)
269
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
270 271

	buffer.length = sizeof(union acpi_object);
272
	buffer.pointer = &element;
L
Linus Torvalds 已提交
273 274 275
	status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
	if (ACPI_FAILURE(status)) {
		acpi_util_eval_error(handle, pathname, status);
276
		return status;
L
Linus Torvalds 已提交
277 278
	}

279
	if (element.type != ACPI_TYPE_INTEGER) {
L
Linus Torvalds 已提交
280
		acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
281
		return AE_BAD_DATA;
L
Linus Torvalds 已提交
282 283
	}

284
	*data = element.integer.value;
L
Linus Torvalds 已提交
285

286
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
L
Linus Torvalds 已提交
287

288
	return AE_OK;
L
Linus Torvalds 已提交
289 290
}

L
Len Brown 已提交
291
EXPORT_SYMBOL(acpi_evaluate_integer);
L
Linus Torvalds 已提交
292 293

acpi_status
L
Len Brown 已提交
294 295 296 297
acpi_evaluate_reference(acpi_handle handle,
			acpi_string pathname,
			struct acpi_object_list *arguments,
			struct acpi_handle_list *list)
L
Linus Torvalds 已提交
298
{
L
Len Brown 已提交
299 300 301 302 303
	acpi_status status = AE_OK;
	union acpi_object *package = NULL;
	union acpi_object *element = NULL;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	u32 i = 0;
L
Linus Torvalds 已提交
304 305 306


	if (!list) {
307
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315
	}

	/* Evaluate object. */

	status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
	if (ACPI_FAILURE(status))
		goto end;

316
	package = buffer.pointer;
L
Linus Torvalds 已提交
317 318

	if ((buffer.length == 0) || !package) {
319 320
		printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
			    (unsigned)buffer.length, package);
L
Linus Torvalds 已提交
321 322 323 324 325
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}
	if (package->type != ACPI_TYPE_PACKAGE) {
326 327
		printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
			    package->type);
L
Linus Torvalds 已提交
328 329 330 331 332
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}
	if (!package->package.count) {
333 334
		printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
			    package);
L
Linus Torvalds 已提交
335 336 337 338 339 340
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}

	if (package->package.count > ACPI_MAX_HANDLES) {
341
		return AE_NO_MEMORY;
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350
	}
	list->count = package->package.count;

	/* Extract package data. */

	for (i = 0; i < list->count; i++) {

		element = &(package->package.elements[i]);

351
		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
L
Linus Torvalds 已提交
352
			status = AE_BAD_DATA;
353 354 355
			printk(KERN_ERR PREFIX
				    "Expecting a [Reference] package element, found type %X\n",
				    element->type);
L
Linus Torvalds 已提交
356 357 358 359
			acpi_util_eval_error(handle, pathname, status);
			break;
		}

360 361 362 363 364 365
		if (!element->reference.handle) {
			printk(KERN_WARNING PREFIX "Invalid reference in"
			       " package %s\n", pathname);
			status = AE_NULL_ENTRY;
			break;
		}
L
Linus Torvalds 已提交
366 367 368 369
		/* Get the  acpi_handle. */

		list->handles[i] = element->reference.handle;
		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
L
Len Brown 已提交
370
				  list->handles[i]));
L
Linus Torvalds 已提交
371 372
	}

L
Len Brown 已提交
373
      end:
L
Linus Torvalds 已提交
374 375 376 377 378
	if (ACPI_FAILURE(status)) {
		list->count = 0;
		//kfree(list->handles);
	}

379
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
380

381
	return status;
L
Linus Torvalds 已提交
382 383
}

L
Len Brown 已提交
384
EXPORT_SYMBOL(acpi_evaluate_reference);