2 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/hardirq.h>
32 #include <linux/acpi.h>
33 #include <linux/dynamic_debug.h>
37 #define _COMPONENT ACPI_BUS_COMPONENT
38 ACPI_MODULE_NAME("utils");
40 /* --------------------------------------------------------------------------
41 Object Evaluation Helpers
42 -------------------------------------------------------------------------- */
44 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
46 #ifdef ACPI_DEBUG_OUTPUT
47 char prefix[80] = {'\0'};
48 struct acpi_buffer buffer = {sizeof(prefix), prefix};
49 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
50 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
51 (char *) prefix, p, acpi_format_exception(s)));
58 acpi_extract_package(union acpi_object *package,
59 struct acpi_buffer *format, struct acpi_buffer *buffer)
61 u32 size_required = 0;
63 char *format_string = NULL;
70 if (!package || (package->type != ACPI_TYPE_PACKAGE)
71 || (package->package.count < 1)) {
72 printk(KERN_WARNING PREFIX "Invalid package argument\n");
73 return AE_BAD_PARAMETER;
76 if (!format || !format->pointer || (format->length < 1)) {
77 printk(KERN_WARNING PREFIX "Invalid format argument\n");
78 return AE_BAD_PARAMETER;
82 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
83 return AE_BAD_PARAMETER;
86 format_count = (format->length / sizeof(char)) - 1;
87 if (format_count > package->package.count) {
88 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
89 " than exist in package [%d].\n",
90 format_count, package->package.count);
94 format_string = format->pointer;
97 * Calculate size_required.
99 for (i = 0; i < format_count; i++) {
101 union acpi_object *element = &(package->package.elements[i]);
103 switch (element->type) {
105 case ACPI_TYPE_INTEGER:
106 switch (format_string[i]) {
108 size_required += sizeof(u64);
109 tail_offset += sizeof(u64);
113 sizeof(char *) + sizeof(u64) +
115 tail_offset += sizeof(char *);
118 printk(KERN_WARNING PREFIX "Invalid package element"
119 " [%d]: got number, expecting"
121 i, format_string[i]);
127 case ACPI_TYPE_STRING:
128 case ACPI_TYPE_BUFFER:
129 switch (format_string[i]) {
133 (element->string.length * sizeof(char)) +
135 tail_offset += sizeof(char *);
139 sizeof(u8 *) + element->buffer.length;
140 tail_offset += sizeof(u8 *);
143 printk(KERN_WARNING PREFIX "Invalid package element"
144 " [%d] got string/buffer,"
146 i, format_string[i]);
151 case ACPI_TYPE_LOCAL_REFERENCE:
152 switch (format_string[i]) {
154 size_required += sizeof(void *);
155 tail_offset += sizeof(void *);
158 printk(KERN_WARNING PREFIX "Invalid package element"
159 " [%d] got reference,"
161 i, format_string[i]);
167 case ACPI_TYPE_PACKAGE:
169 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
170 "Found unsupported element at index=%d\n",
172 /* TBD: handle nested packages... */
179 * Validate output buffer.
181 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
182 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
183 if (!buffer->pointer)
185 buffer->length = size_required;
187 if (buffer->length < size_required) {
188 buffer->length = size_required;
189 return AE_BUFFER_OVERFLOW;
190 } else if (buffer->length != size_required ||
192 return AE_BAD_PARAMETER;
196 head = buffer->pointer;
197 tail = buffer->pointer + tail_offset;
200 * Extract package data.
202 for (i = 0; i < format_count; i++) {
205 union acpi_object *element = &(package->package.elements[i]);
211 switch (element->type) {
213 case ACPI_TYPE_INTEGER:
214 switch (format_string[i]) {
217 element->integer.value;
221 pointer = (u8 **) head;
224 element->integer.value;
225 head += sizeof(u64 *);
227 /* NULL terminate string */
229 tail += sizeof(char);
232 /* Should never get here */
237 case ACPI_TYPE_STRING:
238 case ACPI_TYPE_BUFFER:
239 switch (format_string[i]) {
241 pointer = (u8 **) head;
243 memcpy(tail, element->string.pointer,
244 element->string.length);
245 head += sizeof(char *);
246 tail += element->string.length * sizeof(char);
247 /* NULL terminate string */
249 tail += sizeof(char);
252 pointer = (u8 **) head;
254 memcpy(tail, element->buffer.pointer,
255 element->buffer.length);
256 head += sizeof(u8 *);
257 tail += element->buffer.length;
260 /* Should never get here */
264 case ACPI_TYPE_LOCAL_REFERENCE:
265 switch (format_string[i]) {
268 (void *)element->reference.handle;
269 head += sizeof(void *);
272 /* Should never get here */
276 case ACPI_TYPE_PACKAGE:
277 /* TBD: handle nested packages... */
279 /* Should never get here */
287 EXPORT_SYMBOL(acpi_extract_package);
290 acpi_evaluate_integer(acpi_handle handle,
291 acpi_string pathname,
292 struct acpi_object_list *arguments, unsigned long long *data)
294 acpi_status status = AE_OK;
295 union acpi_object element;
296 struct acpi_buffer buffer = { 0, NULL };
299 return AE_BAD_PARAMETER;
301 buffer.length = sizeof(union acpi_object);
302 buffer.pointer = &element;
303 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
304 if (ACPI_FAILURE(status)) {
305 acpi_util_eval_error(handle, pathname, status);
309 if (element.type != ACPI_TYPE_INTEGER) {
310 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
314 *data = element.integer.value;
316 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
321 EXPORT_SYMBOL(acpi_evaluate_integer);
324 acpi_evaluate_reference(acpi_handle handle,
325 acpi_string pathname,
326 struct acpi_object_list *arguments,
327 struct acpi_handle_list *list)
329 acpi_status status = AE_OK;
330 union acpi_object *package = NULL;
331 union acpi_object *element = NULL;
332 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
337 return AE_BAD_PARAMETER;
340 /* Evaluate object. */
342 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
343 if (ACPI_FAILURE(status))
346 package = buffer.pointer;
348 if ((buffer.length == 0) || !package) {
349 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
350 (unsigned)buffer.length, package);
351 status = AE_BAD_DATA;
352 acpi_util_eval_error(handle, pathname, status);
355 if (package->type != ACPI_TYPE_PACKAGE) {
356 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
358 status = AE_BAD_DATA;
359 acpi_util_eval_error(handle, pathname, status);
362 if (!package->package.count) {
363 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
365 status = AE_BAD_DATA;
366 acpi_util_eval_error(handle, pathname, status);
370 if (package->package.count > ACPI_MAX_HANDLES) {
373 list->count = package->package.count;
375 /* Extract package data. */
377 for (i = 0; i < list->count; i++) {
379 element = &(package->package.elements[i]);
381 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
382 status = AE_BAD_DATA;
383 printk(KERN_ERR PREFIX
384 "Expecting a [Reference] package element, found type %X\n",
386 acpi_util_eval_error(handle, pathname, status);
390 if (!element->reference.handle) {
391 printk(KERN_WARNING PREFIX "Invalid reference in"
392 " package %s\n", pathname);
393 status = AE_NULL_ENTRY;
396 /* Get the acpi_handle. */
398 list->handles[i] = element->reference.handle;
399 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
404 if (ACPI_FAILURE(status)) {
406 //kfree(list->handles);
409 kfree(buffer.pointer);
414 EXPORT_SYMBOL(acpi_evaluate_reference);
417 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
420 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
421 union acpi_object *output;
423 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
425 if (ACPI_FAILURE(status))
428 output = buffer.pointer;
430 if (!output || output->type != ACPI_TYPE_PACKAGE
431 || !output->package.count
432 || output->package.elements[0].type != ACPI_TYPE_BUFFER
433 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
438 status = acpi_decode_pld_buffer(
439 output->package.elements[0].buffer.pointer,
440 output->package.elements[0].buffer.length,
444 kfree(buffer.pointer);
447 EXPORT_SYMBOL(acpi_get_physical_device_location);
450 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
451 * @handle: ACPI device handle
452 * @source_event: source event code
453 * @status_code: status code
454 * @status_buf: optional detailed information (NULL if none)
456 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
457 * must call this function when evaluating _OST for hotplug operations.
458 * When the platform does not support _OST, this function has no effect.
461 acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
462 struct acpi_buffer *status_buf)
464 union acpi_object params[3] = {
465 {.type = ACPI_TYPE_INTEGER,},
466 {.type = ACPI_TYPE_INTEGER,},
467 {.type = ACPI_TYPE_BUFFER,}
469 struct acpi_object_list arg_list = {3, params};
471 params[0].integer.value = source_event;
472 params[1].integer.value = status_code;
473 if (status_buf != NULL) {
474 params[2].buffer.pointer = status_buf->pointer;
475 params[2].buffer.length = status_buf->length;
477 params[2].buffer.pointer = NULL;
478 params[2].buffer.length = 0;
481 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
483 EXPORT_SYMBOL(acpi_evaluate_ost);
486 * acpi_handle_path: Return the object path of handle
488 * Caller must free the returned buffer
490 static char *acpi_handle_path(acpi_handle handle)
492 struct acpi_buffer buffer = {
493 .length = ACPI_ALLOCATE_BUFFER,
497 if (in_interrupt() ||
498 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
500 return buffer.pointer;
504 * acpi_handle_printk: Print message with ACPI prefix and object path
506 * This function is called through acpi_handle_<level> macros and prints
507 * a message with ACPI prefix and object path. This function acquires
508 * the global namespace mutex to obtain an object path. In interrupt
509 * context, it shows the object path as <n/a>.
512 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
514 struct va_format vaf;
522 path = acpi_handle_path(handle);
523 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
528 EXPORT_SYMBOL(acpi_handle_printk);
530 #if defined(CONFIG_DYNAMIC_DEBUG)
532 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
534 * This function is called through acpi_handle_debug macro and debug
535 * prints a message with ACPI prefix and object path. This function
536 * acquires the global namespace mutex to obtain an object path. In
537 * interrupt context, it shows the object path as <n/a>.
540 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
541 const char *fmt, ...)
543 struct va_format vaf;
551 path = acpi_handle_path(handle);
552 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
557 EXPORT_SYMBOL(__acpi_handle_debug);
561 * acpi_has_method: Check whether @handle has a method named @name
562 * @handle: ACPI device handle
563 * @name: name of object or method
565 * Check whether @handle has a method named @name.
567 bool acpi_has_method(acpi_handle handle, char *name)
571 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
573 EXPORT_SYMBOL(acpi_has_method);
575 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
578 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
579 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
581 obj.integer.value = arg;
583 return acpi_evaluate_object(handle, method, &arg_list, NULL);
585 EXPORT_SYMBOL(acpi_execute_simple_method);
588 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
589 * @handle: ACPI device handle
591 * Evaluate device's _EJ0 method for hotplug operations.
593 acpi_status acpi_evaluate_ej0(acpi_handle handle)
597 status = acpi_execute_simple_method(handle, "_EJ0", 1);
598 if (status == AE_NOT_FOUND)
599 acpi_handle_warn(handle, "No _EJ0 support for device\n");
600 else if (ACPI_FAILURE(status))
601 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
607 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
608 * @handle: ACPI device handle
609 * @lock: lock device if non-zero, otherwise unlock device
611 * Evaluate device's _LCK method if present to lock/unlock device
613 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
617 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
618 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
620 acpi_handle_warn(handle,
621 "Locking device failed (0x%x)\n", status);
623 acpi_handle_warn(handle,
624 "Unlocking device failed (0x%x)\n", status);
631 * acpi_evaluate_dsm - evaluate device's _DSM method
632 * @handle: ACPI device handle
633 * @uuid: UUID of requested functions, should be 16 bytes
634 * @rev: revision number of requested function
635 * @func: requested function number
636 * @argv4: the function specific parameter
638 * Evaluate device's _DSM method with specified UUID, revision id and
639 * function number. Caller needs to free the returned object.
641 * Though ACPI defines the fourth parameter for _DSM should be a package,
642 * some old BIOSes do expect a buffer or an integer etc.
645 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
646 union acpi_object *argv4)
649 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
650 union acpi_object params[4];
651 struct acpi_object_list input = {
656 params[0].type = ACPI_TYPE_BUFFER;
657 params[0].buffer.length = 16;
658 params[0].buffer.pointer = (char *)uuid;
659 params[1].type = ACPI_TYPE_INTEGER;
660 params[1].integer.value = rev;
661 params[2].type = ACPI_TYPE_INTEGER;
662 params[2].integer.value = func;
666 params[3].type = ACPI_TYPE_PACKAGE;
667 params[3].package.count = 0;
668 params[3].package.elements = NULL;
671 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
672 if (ACPI_SUCCESS(ret))
673 return (union acpi_object *)buf.pointer;
675 if (ret != AE_NOT_FOUND)
676 acpi_handle_warn(handle,
677 "failed to evaluate _DSM (0x%x)\n", ret);
681 EXPORT_SYMBOL(acpi_evaluate_dsm);
684 * acpi_check_dsm - check if _DSM method supports requested functions.
685 * @handle: ACPI device handle
686 * @uuid: UUID of requested functions, should be 16 bytes at least
687 * @rev: revision number of requested functions
688 * @funcs: bitmap of requested functions
690 * Evaluate device's _DSM method to check whether it supports requested
691 * functions. Currently only support 64 functions at maximum, should be
694 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
698 union acpi_object *obj;
703 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
707 /* For compatibility, old BIOSes may return an integer */
708 if (obj->type == ACPI_TYPE_INTEGER)
709 mask = obj->integer.value;
710 else if (obj->type == ACPI_TYPE_BUFFER)
711 for (i = 0; i < obj->buffer.length && i < 8; i++)
712 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
716 * Bit 0 indicates whether there's support for any functions other than
717 * function 0 for the specified UUID and revision.
719 if ((mask & 0x1) && (mask & funcs) == funcs)
724 EXPORT_SYMBOL(acpi_check_dsm);