utils.c 10.3 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 28 29 30 31 32
/*
 *  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>
#include <linux/init.h>
#include <linux/types.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

33 34
#include "internal.h"

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

/* --------------------------------------------------------------------------
                            Object Evaluation Helpers
   -------------------------------------------------------------------------- */
41 42 43
static void
acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
{
L
Linus Torvalds 已提交
44
#ifdef ACPI_DEBUG_OUTPUT
45 46 47 48 49
	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 已提交
50
#else
51
	return;
L
Linus Torvalds 已提交
52
#endif
53 54
}

L
Linus Torvalds 已提交
55
acpi_status
L
Len Brown 已提交
56 57
acpi_extract_package(union acpi_object *package,
		     struct acpi_buffer *format, struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
58
{
L
Len Brown 已提交
59 60 61 62 63 64 65
	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 已提交
66 67


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

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

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

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

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

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

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

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

		switch (element->type) {

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

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

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

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

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

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

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

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

		switch (element->type) {

		case ACPI_TYPE_INTEGER:
			switch (format_string[i]) {
			case 'N':
L
Len Brown 已提交
196 197
				*((acpi_integer *) head) =
				    element->integer.value;
L
Linus Torvalds 已提交
198 199 200
				head += sizeof(acpi_integer);
				break;
			case 'S':
L
Len Brown 已提交
201
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
202
				*pointer = tail;
L
Len Brown 已提交
203 204 205
				*((acpi_integer *) tail) =
				    element->integer.value;
				head += sizeof(acpi_integer *);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
				tail += sizeof(acpi_integer);
				/* 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 已提交
221
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
222
				*pointer = tail;
L
Len Brown 已提交
223 224 225
				memcpy(tail, element->string.pointer,
				       element->string.length);
				head += sizeof(char *);
L
Linus Torvalds 已提交
226 227 228 229 230 231
				tail += element->string.length * sizeof(char);
				/* NULL terminate string */
				*tail = (char)0;
				tail += sizeof(char);
				break;
			case 'B':
L
Len Brown 已提交
232
				pointer = (u8 **) head;
L
Linus Torvalds 已提交
233
				*pointer = tail;
L
Len Brown 已提交
234 235 236
				memcpy(tail, element->buffer.pointer,
				       element->buffer.length);
				head += sizeof(u8 *);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
				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;
		}
	}

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

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

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

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

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

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

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

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

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

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

#if 0
acpi_status
L
Len Brown 已提交
294 295 296
acpi_evaluate_string(acpi_handle handle,
		     acpi_string pathname,
		     acpi_object_list * arguments, acpi_string * data)
L
Linus Torvalds 已提交
297
{
L
Len Brown 已提交
298 299 300
	acpi_status status = AE_OK;
	acpi_object *element = NULL;
	acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
301 302 303


	if (!data)
304
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
305 306 307 308

	status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
	if (ACPI_FAILURE(status)) {
		acpi_util_eval_error(handle, pathname, status);
309
		return status;
L
Linus Torvalds 已提交
310 311 312 313
	}

	element = (acpi_object *) buffer.pointer;

L
Len Brown 已提交
314 315 316
	if ((element->type != ACPI_TYPE_STRING)
	    || (element->type != ACPI_TYPE_BUFFER)
	    || !element->string.length) {
L
Linus Torvalds 已提交
317
		acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
318
		return AE_BAD_DATA;
L
Linus Torvalds 已提交
319 320
	}

321
	*data = kzalloc(element->string.length + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
322
	if (!data) {
323
		printk(KERN_ERR PREFIX "Memory allocation\n");
324
		return -ENOMEM;
L
Linus Torvalds 已提交
325 326 327 328 329 330
	}

	memcpy(*data, element->string.pointer, element->string.length);

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));

331
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
332

333
	return AE_OK;
L
Linus Torvalds 已提交
334 335 336 337
}
#endif

acpi_status
L
Len Brown 已提交
338 339 340 341
acpi_evaluate_reference(acpi_handle handle,
			acpi_string pathname,
			struct acpi_object_list *arguments,
			struct acpi_handle_list *list)
L
Linus Torvalds 已提交
342
{
L
Len Brown 已提交
343 344 345 346 347
	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 已提交
348 349 350


	if (!list) {
351
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359
	}

	/* Evaluate object. */

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

360
	package = buffer.pointer;
L
Linus Torvalds 已提交
361 362

	if ((buffer.length == 0) || !package) {
363 364
		printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
			    (unsigned)buffer.length, package);
L
Linus Torvalds 已提交
365 366 367 368 369
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}
	if (package->type != ACPI_TYPE_PACKAGE) {
370 371
		printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
			    package->type);
L
Linus Torvalds 已提交
372 373 374 375 376
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}
	if (!package->package.count) {
377 378
		printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
			    package);
L
Linus Torvalds 已提交
379 380 381 382 383 384
		status = AE_BAD_DATA;
		acpi_util_eval_error(handle, pathname, status);
		goto end;
	}

	if (package->package.count > ACPI_MAX_HANDLES) {
385
		return AE_NO_MEMORY;
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394
	}
	list->count = package->package.count;

	/* Extract package data. */

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

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

395
		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
L
Linus Torvalds 已提交
396
			status = AE_BAD_DATA;
397 398 399
			printk(KERN_ERR PREFIX
				    "Expecting a [Reference] package element, found type %X\n",
				    element->type);
L
Linus Torvalds 已提交
400 401 402 403
			acpi_util_eval_error(handle, pathname, status);
			break;
		}

404 405 406 407 408 409
		if (!element->reference.handle) {
			printk(KERN_WARNING PREFIX "Invalid reference in"
			       " package %s\n", pathname);
			status = AE_NULL_ENTRY;
			break;
		}
L
Linus Torvalds 已提交
410 411 412 413
		/* Get the  acpi_handle. */

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

L
Len Brown 已提交
417
      end:
L
Linus Torvalds 已提交
418 419 420 421 422
	if (ACPI_FAILURE(status)) {
		list->count = 0;
		//kfree(list->handles);
	}

423
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
424

425
	return status;
L
Linus Torvalds 已提交
426 427
}

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