2 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
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 *);
140 (element->buffer.length * sizeof(u8));
141 tail_offset += sizeof(u8 *);
144 printk(KERN_WARNING PREFIX "Invalid package element"
145 " [%d] got string/buffer,"
147 i, format_string[i]);
152 case ACPI_TYPE_LOCAL_REFERENCE:
153 switch (format_string[i]) {
155 size_required += sizeof(void *);
156 tail_offset += sizeof(void *);
159 printk(KERN_WARNING PREFIX "Invalid package element"
160 " [%d] got reference,"
162 i, format_string[i]);
168 case ACPI_TYPE_PACKAGE:
170 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
171 "Found unsupported element at index=%d\n",
173 /* TBD: handle nested packages... */
180 * Validate output buffer.
182 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
183 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
184 if (!buffer->pointer)
186 buffer->length = size_required;
188 if (buffer->length < size_required) {
189 buffer->length = size_required;
190 return AE_BUFFER_OVERFLOW;
191 } else if (buffer->length != size_required ||
193 return AE_BAD_PARAMETER;
197 head = buffer->pointer;
198 tail = buffer->pointer + tail_offset;
201 * Extract package data.
203 for (i = 0; i < format_count; i++) {
206 union acpi_object *element = &(package->package.elements[i]);
212 switch (element->type) {
214 case ACPI_TYPE_INTEGER:
215 switch (format_string[i]) {
218 element->integer.value;
222 pointer = (u8 **) head;
225 element->integer.value;
226 head += sizeof(u64 *);
228 /* NULL terminate string */
230 tail += sizeof(char);
233 /* Should never get here */
238 case ACPI_TYPE_STRING:
239 case ACPI_TYPE_BUFFER:
240 switch (format_string[i]) {
242 pointer = (u8 **) head;
244 memcpy(tail, element->string.pointer,
245 element->string.length);
246 head += sizeof(char *);
247 tail += element->string.length * sizeof(char);
248 /* NULL terminate string */
250 tail += sizeof(char);
253 pointer = (u8 **) head;
255 memcpy(tail, element->buffer.pointer,
256 element->buffer.length);
257 head += sizeof(u8 *);
258 tail += element->buffer.length * sizeof(u8);
261 /* Should never get here */
265 case ACPI_TYPE_LOCAL_REFERENCE:
266 switch (format_string[i]) {
269 (void *)element->reference.handle;
270 head += sizeof(void *);
273 /* Should never get here */
277 case ACPI_TYPE_PACKAGE:
278 /* TBD: handle nested packages... */
280 /* Should never get here */
288 EXPORT_SYMBOL(acpi_extract_package);
291 acpi_evaluate_integer(acpi_handle handle,
292 acpi_string pathname,
293 struct acpi_object_list *arguments, unsigned long long *data)
295 acpi_status status = AE_OK;
296 union acpi_object element;
297 struct acpi_buffer buffer = { 0, NULL };
300 return AE_BAD_PARAMETER;
302 buffer.length = sizeof(union acpi_object);
303 buffer.pointer = &element;
304 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
305 if (ACPI_FAILURE(status)) {
306 acpi_util_eval_error(handle, pathname, status);
310 if (element.type != ACPI_TYPE_INTEGER) {
311 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
315 *data = element.integer.value;
317 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
322 EXPORT_SYMBOL(acpi_evaluate_integer);
325 acpi_evaluate_reference(acpi_handle handle,
326 acpi_string pathname,
327 struct acpi_object_list *arguments,
328 struct acpi_handle_list *list)
330 acpi_status status = AE_OK;
331 union acpi_object *package = NULL;
332 union acpi_object *element = NULL;
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
338 return AE_BAD_PARAMETER;
341 /* Evaluate object. */
343 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
344 if (ACPI_FAILURE(status))
347 package = buffer.pointer;
349 if ((buffer.length == 0) || !package) {
350 status = AE_BAD_DATA;
351 acpi_util_eval_error(handle, pathname, status);
354 if (package->type != ACPI_TYPE_PACKAGE) {
355 status = AE_BAD_DATA;
356 acpi_util_eval_error(handle, pathname, status);
359 if (!package->package.count) {
360 status = AE_BAD_DATA;
361 acpi_util_eval_error(handle, pathname, status);
365 if (package->package.count > ACPI_MAX_HANDLES) {
368 list->count = package->package.count;
370 /* Extract package data. */
372 for (i = 0; i < list->count; i++) {
374 element = &(package->package.elements[i]);
376 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
377 status = AE_BAD_DATA;
378 acpi_util_eval_error(handle, pathname, status);
382 if (!element->reference.handle) {
383 status = AE_NULL_ENTRY;
384 acpi_util_eval_error(handle, pathname, status);
387 /* Get the acpi_handle. */
389 list->handles[i] = element->reference.handle;
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
395 if (ACPI_FAILURE(status)) {
397 //kfree(list->handles);
400 kfree(buffer.pointer);
405 EXPORT_SYMBOL(acpi_evaluate_reference);
408 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
411 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
412 union acpi_object *output;
414 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
416 if (ACPI_FAILURE(status))
419 output = buffer.pointer;
421 if (!output || output->type != ACPI_TYPE_PACKAGE
422 || !output->package.count
423 || output->package.elements[0].type != ACPI_TYPE_BUFFER
424 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
429 status = acpi_decode_pld_buffer(
430 output->package.elements[0].buffer.pointer,
431 output->package.elements[0].buffer.length,
435 kfree(buffer.pointer);
438 EXPORT_SYMBOL(acpi_get_physical_device_location);
441 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
442 * @handle: ACPI device handle
443 * @source_event: source event code
444 * @status_code: status code
445 * @status_buf: optional detailed information (NULL if none)
447 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
448 * must call this function when evaluating _OST for hotplug operations.
449 * When the platform does not support _OST, this function has no effect.
452 acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
453 struct acpi_buffer *status_buf)
455 union acpi_object params[3] = {
456 {.type = ACPI_TYPE_INTEGER,},
457 {.type = ACPI_TYPE_INTEGER,},
458 {.type = ACPI_TYPE_BUFFER,}
460 struct acpi_object_list arg_list = {3, params};
462 params[0].integer.value = source_event;
463 params[1].integer.value = status_code;
464 if (status_buf != NULL) {
465 params[2].buffer.pointer = status_buf->pointer;
466 params[2].buffer.length = status_buf->length;
468 params[2].buffer.pointer = NULL;
469 params[2].buffer.length = 0;
472 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
474 EXPORT_SYMBOL(acpi_evaluate_ost);
477 * acpi_handle_path: Return the object path of handle
479 * Caller must free the returned buffer
481 static char *acpi_handle_path(acpi_handle handle)
483 struct acpi_buffer buffer = {
484 .length = ACPI_ALLOCATE_BUFFER,
488 if (in_interrupt() ||
489 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
491 return buffer.pointer;
495 * acpi_handle_printk: Print message with ACPI prefix and object path
497 * This function is called through acpi_handle_<level> macros and prints
498 * a message with ACPI prefix and object path. This function acquires
499 * the global namespace mutex to obtain an object path. In interrupt
500 * context, it shows the object path as <n/a>.
503 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
505 struct va_format vaf;
513 path = acpi_handle_path(handle);
514 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
519 EXPORT_SYMBOL(acpi_handle_printk);
521 #if defined(CONFIG_DYNAMIC_DEBUG)
523 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
525 * This function is called through acpi_handle_debug macro and debug
526 * prints a message with ACPI prefix and object path. This function
527 * acquires the global namespace mutex to obtain an object path. In
528 * interrupt context, it shows the object path as <n/a>.
531 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
532 const char *fmt, ...)
534 struct va_format vaf;
542 path = acpi_handle_path(handle);
543 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
548 EXPORT_SYMBOL(__acpi_handle_debug);
552 * acpi_has_method: Check whether @handle has a method named @name
553 * @handle: ACPI device handle
554 * @name: name of object or method
556 * Check whether @handle has a method named @name.
558 bool acpi_has_method(acpi_handle handle, char *name)
562 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
564 EXPORT_SYMBOL(acpi_has_method);
566 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
569 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
570 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
572 obj.integer.value = arg;
574 return acpi_evaluate_object(handle, method, &arg_list, NULL);
576 EXPORT_SYMBOL(acpi_execute_simple_method);
579 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
580 * @handle: ACPI device handle
582 * Evaluate device's _EJ0 method for hotplug operations.
584 acpi_status acpi_evaluate_ej0(acpi_handle handle)
588 status = acpi_execute_simple_method(handle, "_EJ0", 1);
589 if (status == AE_NOT_FOUND)
590 acpi_handle_warn(handle, "No _EJ0 support for device\n");
591 else if (ACPI_FAILURE(status))
592 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
598 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
599 * @handle: ACPI device handle
600 * @lock: lock device if non-zero, otherwise unlock device
602 * Evaluate device's _LCK method if present to lock/unlock device
604 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
608 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
609 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
611 acpi_handle_warn(handle,
612 "Locking device failed (0x%x)\n", status);
614 acpi_handle_warn(handle,
615 "Unlocking device failed (0x%x)\n", status);
622 * acpi_evaluate_dsm - evaluate device's _DSM method
623 * @handle: ACPI device handle
624 * @uuid: UUID of requested functions, should be 16 bytes
625 * @rev: revision number of requested function
626 * @func: requested function number
627 * @argv4: the function specific parameter
629 * Evaluate device's _DSM method with specified UUID, revision id and
630 * function number. Caller needs to free the returned object.
632 * Though ACPI defines the fourth parameter for _DSM should be a package,
633 * some old BIOSes do expect a buffer or an integer etc.
636 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
637 union acpi_object *argv4)
640 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
641 union acpi_object params[4];
642 struct acpi_object_list input = {
647 params[0].type = ACPI_TYPE_BUFFER;
648 params[0].buffer.length = 16;
649 params[0].buffer.pointer = (char *)uuid;
650 params[1].type = ACPI_TYPE_INTEGER;
651 params[1].integer.value = rev;
652 params[2].type = ACPI_TYPE_INTEGER;
653 params[2].integer.value = func;
657 params[3].type = ACPI_TYPE_PACKAGE;
658 params[3].package.count = 0;
659 params[3].package.elements = NULL;
662 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
663 if (ACPI_SUCCESS(ret))
664 return (union acpi_object *)buf.pointer;
666 if (ret != AE_NOT_FOUND)
667 acpi_handle_warn(handle,
668 "failed to evaluate _DSM (0x%x)\n", ret);
672 EXPORT_SYMBOL(acpi_evaluate_dsm);
675 * acpi_check_dsm - check if _DSM method supports requested functions.
676 * @handle: ACPI device handle
677 * @uuid: UUID of requested functions, should be 16 bytes at least
678 * @rev: revision number of requested functions
679 * @funcs: bitmap of requested functions
681 * Evaluate device's _DSM method to check whether it supports requested
682 * functions. Currently only support 64 functions at maximum, should be
685 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
689 union acpi_object *obj;
694 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
698 /* For compatibility, old BIOSes may return an integer */
699 if (obj->type == ACPI_TYPE_INTEGER)
700 mask = obj->integer.value;
701 else if (obj->type == ACPI_TYPE_BUFFER)
702 for (i = 0; i < obj->buffer.length && i < 8; i++)
703 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
707 * Bit 0 indicates whether there's support for any functions other than
708 * function 0 for the specified UUID and revision.
710 if ((mask & 0x1) && (mask & funcs) == funcs)
715 EXPORT_SYMBOL(acpi_check_dsm);