utils.c 10.5 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 33
/*
 *  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>

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

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

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


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

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

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

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

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

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

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

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

		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 已提交
112 113 114 115
				size_required +=
				    sizeof(char *) + sizeof(acpi_integer) +
				    sizeof(char);
				tail_offset += sizeof(char *);
L
Linus Torvalds 已提交
116 117
				break;
			default:
118
				printk(KERN_WARNING PREFIX "Invalid package element"
119
					      " [%d]: got number, expecing"
120 121
					      " [%c]\n",
					      i, format_string[i]);
122
				return AE_BAD_DATA;
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130
				break;
			}
			break;

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

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

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

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

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

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

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

		switch (element->type) {

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

251
	return AE_OK;
L
Linus Torvalds 已提交
252 253
}

L
Len Brown 已提交
254
EXPORT_SYMBOL(acpi_extract_package);
L
Linus Torvalds 已提交
255 256

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


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

269
	element = kzalloc(sizeof(union acpi_object), irqs_disabled() ? GFP_ATOMIC: GFP_KERNEL);
L
Len Brown 已提交
270
	if (!element)
271
		return AE_NO_MEMORY;
L
Linus Torvalds 已提交
272 273 274 275 276 277

	buffer.length = sizeof(union acpi_object);
	buffer.pointer = element;
	status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
	if (ACPI_FAILURE(status)) {
		acpi_util_eval_error(handle, pathname, status);
278
		kfree(element);
279
		return status;
L
Linus Torvalds 已提交
280 281 282 283
	}

	if (element->type != ACPI_TYPE_INTEGER) {
		acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
284
		kfree(element);
285
		return AE_BAD_DATA;
L
Linus Torvalds 已提交
286 287 288 289 290
	}

	*data = element->integer.value;
	kfree(element);

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

293
	return AE_OK;
L
Linus Torvalds 已提交
294 295
}

L
Len Brown 已提交
296
EXPORT_SYMBOL(acpi_evaluate_integer);
L
Linus Torvalds 已提交
297 298 299

#if 0
acpi_status
L
Len Brown 已提交
300 301 302
acpi_evaluate_string(acpi_handle handle,
		     acpi_string pathname,
		     acpi_object_list * arguments, acpi_string * data)
L
Linus Torvalds 已提交
303
{
L
Len Brown 已提交
304 305 306
	acpi_status status = AE_OK;
	acpi_object *element = NULL;
	acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
307 308 309


	if (!data)
310
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
311 312 313 314

	status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
	if (ACPI_FAILURE(status)) {
		acpi_util_eval_error(handle, pathname, status);
315
		return status;
L
Linus Torvalds 已提交
316 317 318 319
	}

	element = (acpi_object *) buffer.pointer;

L
Len Brown 已提交
320 321 322
	if ((element->type != ACPI_TYPE_STRING)
	    || (element->type != ACPI_TYPE_BUFFER)
	    || !element->string.length) {
L
Linus Torvalds 已提交
323
		acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
324
		return AE_BAD_DATA;
L
Linus Torvalds 已提交
325 326
	}

327
	*data = kzalloc(element->string.length + 1, GFP_KERNEL);
L
Linus Torvalds 已提交
328
	if (!data) {
329
		printk(KERN_ERR PREFIX "Memory allocation\n");
330
		return -ENOMEM;
L
Linus Torvalds 已提交
331 332 333 334 335 336
	}

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

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

337
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
338

339
	return AE_OK;
L
Linus Torvalds 已提交
340 341 342 343
}
#endif

acpi_status
L
Len Brown 已提交
344 345 346 347
acpi_evaluate_reference(acpi_handle handle,
			acpi_string pathname,
			struct acpi_object_list *arguments,
			struct acpi_handle_list *list)
L
Linus Torvalds 已提交
348
{
L
Len Brown 已提交
349 350 351 352 353
	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 已提交
354 355 356


	if (!list) {
357
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365
	}

	/* Evaluate object. */

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

366
	package = buffer.pointer;
L
Linus Torvalds 已提交
367 368

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

	if (package->package.count > ACPI_MAX_HANDLES) {
391
		return AE_NO_MEMORY;
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400
	}
	list->count = package->package.count;

	/* Extract package data. */

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

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

401
		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
L
Linus Torvalds 已提交
402
			status = AE_BAD_DATA;
403 404 405
			printk(KERN_ERR PREFIX
				    "Expecting a [Reference] package element, found type %X\n",
				    element->type);
L
Linus Torvalds 已提交
406 407 408 409
			acpi_util_eval_error(handle, pathname, status);
			break;
		}

410 411 412 413 414 415
		if (!element->reference.handle) {
			printk(KERN_WARNING PREFIX "Invalid reference in"
			       " package %s\n", pathname);
			status = AE_NULL_ENTRY;
			break;
		}
L
Linus Torvalds 已提交
416 417 418 419
		/* Get the  acpi_handle. */

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

L
Len Brown 已提交
423
      end:
L
Linus Torvalds 已提交
424 425 426 427 428
	if (ACPI_FAILURE(status)) {
		list->count = 0;
		//kfree(list->handles);
	}

429
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
430

431
	return status;
L
Linus Torvalds 已提交
432 433
}

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