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>
36 #define _COMPONENT ACPI_BUS_COMPONENT
37 ACPI_MODULE_NAME("utils");
39 /* --------------------------------------------------------------------------
40 Object Evaluation Helpers
41 -------------------------------------------------------------------------- */
43 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
45 #ifdef ACPI_DEBUG_OUTPUT
46 char prefix[80] = {'\0'};
47 struct acpi_buffer buffer = {sizeof(prefix), prefix};
48 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
49 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
50 (char *) prefix, p, acpi_format_exception(s)));
57 acpi_extract_package(union acpi_object *package,
58 struct acpi_buffer *format, struct acpi_buffer *buffer)
60 u32 size_required = 0;
62 char *format_string = NULL;
69 if (!package || (package->type != ACPI_TYPE_PACKAGE)
70 || (package->package.count < 1)) {
71 printk(KERN_WARNING PREFIX "Invalid package argument\n");
72 return AE_BAD_PARAMETER;
75 if (!format || !format->pointer || (format->length < 1)) {
76 printk(KERN_WARNING PREFIX "Invalid format argument\n");
77 return AE_BAD_PARAMETER;
81 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
82 return AE_BAD_PARAMETER;
85 format_count = (format->length / sizeof(char)) - 1;
86 if (format_count > package->package.count) {
87 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
88 " than exist in package [%d].\n",
89 format_count, package->package.count);
93 format_string = format->pointer;
96 * Calculate size_required.
98 for (i = 0; i < format_count; i++) {
100 union acpi_object *element = &(package->package.elements[i]);
106 switch (element->type) {
108 case ACPI_TYPE_INTEGER:
109 switch (format_string[i]) {
111 size_required += sizeof(u64);
112 tail_offset += sizeof(u64);
116 sizeof(char *) + sizeof(u64) +
118 tail_offset += sizeof(char *);
121 printk(KERN_WARNING PREFIX "Invalid package element"
122 " [%d]: got number, expecting"
124 i, format_string[i]);
130 case ACPI_TYPE_STRING:
131 case ACPI_TYPE_BUFFER:
132 switch (format_string[i]) {
136 (element->string.length * sizeof(char)) +
138 tail_offset += sizeof(char *);
143 (element->buffer.length * sizeof(u8));
144 tail_offset += sizeof(u8 *);
147 printk(KERN_WARNING PREFIX "Invalid package element"
148 " [%d] got string/buffer,"
150 i, format_string[i]);
156 case ACPI_TYPE_PACKAGE:
158 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
159 "Found unsupported element at index=%d\n",
161 /* TBD: handle nested packages... */
168 * Validate output buffer.
170 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
171 buffer->pointer = ACPI_ALLOCATE(size_required);
172 if (!buffer->pointer)
174 buffer->length = size_required;
175 memset(buffer->pointer, 0, size_required);
177 if (buffer->length < size_required) {
178 buffer->length = size_required;
179 return AE_BUFFER_OVERFLOW;
180 } else if (buffer->length != size_required ||
182 return AE_BAD_PARAMETER;
186 head = buffer->pointer;
187 tail = buffer->pointer + tail_offset;
190 * Extract package data.
192 for (i = 0; i < format_count; i++) {
195 union acpi_object *element = &(package->package.elements[i]);
201 switch (element->type) {
203 case ACPI_TYPE_INTEGER:
204 switch (format_string[i]) {
207 element->integer.value;
211 pointer = (u8 **) head;
214 element->integer.value;
215 head += sizeof(u64 *);
217 /* NULL terminate string */
219 tail += sizeof(char);
222 /* Should never get here */
227 case ACPI_TYPE_STRING:
228 case ACPI_TYPE_BUFFER:
229 switch (format_string[i]) {
231 pointer = (u8 **) head;
233 memcpy(tail, element->string.pointer,
234 element->string.length);
235 head += sizeof(char *);
236 tail += element->string.length * sizeof(char);
237 /* NULL terminate string */
239 tail += sizeof(char);
242 pointer = (u8 **) head;
244 memcpy(tail, element->buffer.pointer,
245 element->buffer.length);
246 head += sizeof(u8 *);
247 tail += element->buffer.length * sizeof(u8);
250 /* Should never get here */
255 case ACPI_TYPE_PACKAGE:
256 /* TBD: handle nested packages... */
258 /* Should never get here */
266 EXPORT_SYMBOL(acpi_extract_package);
269 acpi_evaluate_integer(acpi_handle handle,
270 acpi_string pathname,
271 struct acpi_object_list *arguments, unsigned long long *data)
273 acpi_status status = AE_OK;
274 union acpi_object element;
275 struct acpi_buffer buffer = { 0, NULL };
278 return AE_BAD_PARAMETER;
280 buffer.length = sizeof(union acpi_object);
281 buffer.pointer = &element;
282 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
283 if (ACPI_FAILURE(status)) {
284 acpi_util_eval_error(handle, pathname, status);
288 if (element.type != ACPI_TYPE_INTEGER) {
289 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
293 *data = element.integer.value;
295 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
300 EXPORT_SYMBOL(acpi_evaluate_integer);
303 acpi_evaluate_reference(acpi_handle handle,
304 acpi_string pathname,
305 struct acpi_object_list *arguments,
306 struct acpi_handle_list *list)
308 acpi_status status = AE_OK;
309 union acpi_object *package = NULL;
310 union acpi_object *element = NULL;
311 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
316 return AE_BAD_PARAMETER;
319 /* Evaluate object. */
321 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
322 if (ACPI_FAILURE(status))
325 package = buffer.pointer;
327 if ((buffer.length == 0) || !package) {
328 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
329 (unsigned)buffer.length, package);
330 status = AE_BAD_DATA;
331 acpi_util_eval_error(handle, pathname, status);
334 if (package->type != ACPI_TYPE_PACKAGE) {
335 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
337 status = AE_BAD_DATA;
338 acpi_util_eval_error(handle, pathname, status);
341 if (!package->package.count) {
342 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
344 status = AE_BAD_DATA;
345 acpi_util_eval_error(handle, pathname, status);
349 if (package->package.count > ACPI_MAX_HANDLES) {
352 list->count = package->package.count;
354 /* Extract package data. */
356 for (i = 0; i < list->count; i++) {
358 element = &(package->package.elements[i]);
360 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
361 status = AE_BAD_DATA;
362 printk(KERN_ERR PREFIX
363 "Expecting a [Reference] package element, found type %X\n",
365 acpi_util_eval_error(handle, pathname, status);
369 if (!element->reference.handle) {
370 printk(KERN_WARNING PREFIX "Invalid reference in"
371 " package %s\n", pathname);
372 status = AE_NULL_ENTRY;
375 /* Get the acpi_handle. */
377 list->handles[i] = element->reference.handle;
378 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
383 if (ACPI_FAILURE(status)) {
385 //kfree(list->handles);
388 kfree(buffer.pointer);
393 EXPORT_SYMBOL(acpi_evaluate_reference);
396 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
399 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
400 union acpi_object *output;
402 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
404 if (ACPI_FAILURE(status))
407 output = buffer.pointer;
409 if (!output || output->type != ACPI_TYPE_PACKAGE
410 || !output->package.count
411 || output->package.elements[0].type != ACPI_TYPE_BUFFER
412 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
417 status = acpi_decode_pld_buffer(
418 output->package.elements[0].buffer.pointer,
419 output->package.elements[0].buffer.length,
423 kfree(buffer.pointer);
426 EXPORT_SYMBOL(acpi_get_physical_device_location);
429 * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
430 * @handle: ACPI device handle
431 * @source_event: source event code
432 * @status_code: status code
433 * @status_buf: optional detailed information (NULL if none)
435 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
436 * must call this function when evaluating _OST for hotplug operations.
437 * When the platform does not support _OST, this function has no effect.
440 acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
441 u32 status_code, struct acpi_buffer *status_buf)
443 #ifdef ACPI_HOTPLUG_OST
444 union acpi_object params[3] = {
445 {.type = ACPI_TYPE_INTEGER,},
446 {.type = ACPI_TYPE_INTEGER,},
447 {.type = ACPI_TYPE_BUFFER,}
449 struct acpi_object_list arg_list = {3, params};
452 params[0].integer.value = source_event;
453 params[1].integer.value = status_code;
454 if (status_buf != NULL) {
455 params[2].buffer.pointer = status_buf->pointer;
456 params[2].buffer.length = status_buf->length;
458 params[2].buffer.pointer = NULL;
459 params[2].buffer.length = 0;
462 status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
468 EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
471 * acpi_handle_printk: Print message with ACPI prefix and object path
473 * This function is called through acpi_handle_<level> macros and prints
474 * a message with ACPI prefix and object path. This function acquires
475 * the global namespace mutex to obtain an object path. In interrupt
476 * context, it shows the object path as <n/a>.
479 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
481 struct va_format vaf;
483 struct acpi_buffer buffer = {
484 .length = ACPI_ALLOCATE_BUFFER,
493 if (in_interrupt() ||
494 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
497 path = buffer.pointer;
499 printk("%sACPI: %s: %pV", level, path, &vaf);
502 kfree(buffer.pointer);
504 EXPORT_SYMBOL(acpi_handle_printk);
507 * acpi_has_method: Check whether @handle has a method named @name
508 * @handle: ACPI device handle
509 * @name: name of object or method
511 * Check whether @handle has a method named @name.
513 bool acpi_has_method(acpi_handle handle, char *name)
517 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
519 EXPORT_SYMBOL(acpi_has_method);
521 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
524 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
525 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
527 obj.integer.value = arg;
529 return acpi_evaluate_object(handle, method, &arg_list, NULL);
531 EXPORT_SYMBOL(acpi_execute_simple_method);
534 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
535 * @handle: ACPI device handle
537 * Evaluate device's _EJ0 method for hotplug operations.
539 acpi_status acpi_evaluate_ej0(acpi_handle handle)
543 status = acpi_execute_simple_method(handle, "_EJ0", 1);
544 if (status == AE_NOT_FOUND)
545 acpi_handle_warn(handle, "No _EJ0 support for device\n");
546 else if (ACPI_FAILURE(status))
547 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
553 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
554 * @handle: ACPI device handle
555 * @lock: lock device if non-zero, otherwise unlock device
557 * Evaluate device's _LCK method if present to lock/unlock device
559 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
563 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
564 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
566 acpi_handle_warn(handle,
567 "Locking device failed (0x%x)\n", status);
569 acpi_handle_warn(handle,
570 "Unlocking device failed (0x%x)\n", status);
577 * acpi_evaluate_dsm - evaluate device's _DSM method
578 * @handle: ACPI device handle
579 * @uuid: UUID of requested functions, should be 16 bytes
580 * @rev: revision number of requested function
581 * @func: requested function number
582 * @argv4: the function specific parameter
584 * Evaluate device's _DSM method with specified UUID, revision id and
585 * function number. Caller needs to free the returned object.
587 * Though ACPI defines the fourth parameter for _DSM should be a package,
588 * some old BIOSes do expect a buffer or an integer etc.
591 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
592 union acpi_object *argv4)
595 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
596 union acpi_object params[4];
597 struct acpi_object_list input = {
602 params[0].type = ACPI_TYPE_BUFFER;
603 params[0].buffer.length = 16;
604 params[0].buffer.pointer = (char *)uuid;
605 params[1].type = ACPI_TYPE_INTEGER;
606 params[1].integer.value = rev;
607 params[2].type = ACPI_TYPE_INTEGER;
608 params[2].integer.value = func;
612 params[3].type = ACPI_TYPE_PACKAGE;
613 params[3].package.count = 0;
614 params[3].package.elements = NULL;
617 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
618 if (ACPI_SUCCESS(ret))
619 return (union acpi_object *)buf.pointer;
621 if (ret != AE_NOT_FOUND)
622 acpi_handle_warn(handle,
623 "failed to evaluate _DSM (0x%x)\n", ret);
627 EXPORT_SYMBOL(acpi_evaluate_dsm);
630 * acpi_check_dsm - check if _DSM method supports requested functions.
631 * @handle: ACPI device handle
632 * @uuid: UUID of requested functions, should be 16 bytes at least
633 * @rev: revision number of requested functions
634 * @funcs: bitmap of requested functions
635 * @exclude: excluding special value, used to support i915 and nouveau
637 * Evaluate device's _DSM method to check whether it supports requested
638 * functions. Currently only support 64 functions at maximum, should be
641 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
645 union acpi_object *obj;
650 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
654 /* For compatibility, old BIOSes may return an integer */
655 if (obj->type == ACPI_TYPE_INTEGER)
656 mask = obj->integer.value;
657 else if (obj->type == ACPI_TYPE_BUFFER)
658 for (i = 0; i < obj->buffer.length && i < 8; i++)
659 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
663 * Bit 0 indicates whether there's support for any functions other than
664 * function 0 for the specified UUID and revision.
666 if ((mask & 0x1) && (mask & funcs) == funcs)
671 EXPORT_SYMBOL(acpi_check_dsm);