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 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 39 40 41 42 43 44 45 46 47 48 49

/* --------------------------------------------------------------------------
                            Object Evaluation Helpers
   -------------------------------------------------------------------------- */
#ifdef ACPI_DEBUG_OUTPUT
#define acpi_util_eval_error(h,p,s) {\
	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))); }
#else
#define acpi_util_eval_error(h,p,s)
#endif
acpi_status
L
Len Brown 已提交
50 51
acpi_extract_package(union acpi_object *package,
		     struct acpi_buffer *format, struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
52
{
L
Len Brown 已提交
53 54 55 56 57 58 59
	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 已提交
60 61


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

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

	if (!buffer) {
74
		printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
75
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
76 77
	}

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

86
	format_string = format->pointer;
L
Linus Torvalds 已提交
87 88 89 90

	/*
	 * Calculate size_required.
	 */
L
Len Brown 已提交
91
	for (i = 0; i < format_count; i++) {
L
Linus Torvalds 已提交
92 93 94 95

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

		if (!element) {
96
			return AE_BAD_DATA;
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107
		}

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

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

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

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

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

	/*
	 * Extract package data.
	 */
L
Len Brown 已提交
176
	for (i = 0; i < format_count; i++) {
L
Linus Torvalds 已提交
177 178 179 180 181

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

		if (!element) {
182
			return AE_BAD_DATA;
L
Linus Torvalds 已提交
183 184 185 186 187 188 189
		}

		switch (element->type) {

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

247
	return AE_OK;
L
Linus Torvalds 已提交
248 249
}

L
Len Brown 已提交
250
EXPORT_SYMBOL(acpi_extract_package);
L
Linus Torvalds 已提交
251 252

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


	if (!data)
263
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
264

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

	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);
274
		kfree(element);
275
		return status;
L
Linus Torvalds 已提交
276 277 278 279
	}

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

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

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

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

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

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


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

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

	element = (acpi_object *) buffer.pointer;

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

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

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

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

333
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
334

335
	return AE_OK;
L
Linus Torvalds 已提交
336 337 338 339
}
#endif

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


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

	/* Evaluate object. */

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

362
	package = buffer.pointer;
L
Linus Torvalds 已提交
363 364

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

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

	/* Extract package data. */

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

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

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

		/* Get the  acpi_handle. */

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

L
Len Brown 已提交
413
      end:
L
Linus Torvalds 已提交
414 415 416 417 418
	if (ACPI_FAILURE(status)) {
		list->count = 0;
		//kfree(list->handles);
	}

419
	kfree(buffer.pointer);
L
Linus Torvalds 已提交
420

421
	return status;
L
Linus Torvalds 已提交
422 423
}

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