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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/hardirq.h>
28 #include <linux/acpi.h>
29 #include <linux/dynamic_debug.h>
34 #define _COMPONENT ACPI_BUS_COMPONENT
35 ACPI_MODULE_NAME("utils");
37 /* --------------------------------------------------------------------------
38 Object Evaluation Helpers
39 -------------------------------------------------------------------------- */
41 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
43 #ifdef ACPI_DEBUG_OUTPUT
44 char prefix[80] = {'\0'};
45 struct acpi_buffer buffer = {sizeof(prefix), prefix};
46 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
47 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
48 (char *) prefix, p, acpi_format_exception(s)));
55 acpi_extract_package(union acpi_object *package,
56 struct acpi_buffer *format, struct acpi_buffer *buffer)
58 u32 size_required = 0;
60 char *format_string = NULL;
67 if (!package || (package->type != ACPI_TYPE_PACKAGE)
68 || (package->package.count < 1)) {
69 printk(KERN_WARNING PREFIX "Invalid package argument\n");
70 return AE_BAD_PARAMETER;
73 if (!format || !format->pointer || (format->length < 1)) {
74 printk(KERN_WARNING PREFIX "Invalid format argument\n");
75 return AE_BAD_PARAMETER;
79 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
80 return AE_BAD_PARAMETER;
83 format_count = (format->length / sizeof(char)) - 1;
84 if (format_count > package->package.count) {
85 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
86 " than exist in package [%d].\n",
87 format_count, package->package.count);
91 format_string = format->pointer;
94 * Calculate size_required.
96 for (i = 0; i < format_count; i++) {
98 union acpi_object *element = &(package->package.elements[i]);
100 switch (element->type) {
102 case ACPI_TYPE_INTEGER:
103 switch (format_string[i]) {
105 size_required += sizeof(u64);
106 tail_offset += sizeof(u64);
110 sizeof(char *) + sizeof(u64) +
112 tail_offset += sizeof(char *);
115 printk(KERN_WARNING PREFIX "Invalid package element"
116 " [%d]: got number, expecting"
118 i, format_string[i]);
124 case ACPI_TYPE_STRING:
125 case ACPI_TYPE_BUFFER:
126 switch (format_string[i]) {
130 (element->string.length * sizeof(char)) +
132 tail_offset += sizeof(char *);
136 sizeof(u8 *) + element->buffer.length;
137 tail_offset += sizeof(u8 *);
140 printk(KERN_WARNING PREFIX "Invalid package element"
141 " [%d] got string/buffer,"
143 i, format_string[i]);
148 case ACPI_TYPE_LOCAL_REFERENCE:
149 switch (format_string[i]) {
151 size_required += sizeof(void *);
152 tail_offset += sizeof(void *);
155 printk(KERN_WARNING PREFIX "Invalid package element"
156 " [%d] got reference,"
158 i, format_string[i]);
164 case ACPI_TYPE_PACKAGE:
166 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
167 "Found unsupported element at index=%d\n",
169 /* TBD: handle nested packages... */
176 * Validate output buffer.
178 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
179 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
180 if (!buffer->pointer)
182 buffer->length = size_required;
184 if (buffer->length < size_required) {
185 buffer->length = size_required;
186 return AE_BUFFER_OVERFLOW;
187 } else if (buffer->length != size_required ||
189 return AE_BAD_PARAMETER;
193 head = buffer->pointer;
194 tail = buffer->pointer + tail_offset;
197 * Extract package data.
199 for (i = 0; i < format_count; i++) {
202 union acpi_object *element = &(package->package.elements[i]);
208 switch (element->type) {
210 case ACPI_TYPE_INTEGER:
211 switch (format_string[i]) {
214 element->integer.value;
218 pointer = (u8 **) head;
221 element->integer.value;
222 head += sizeof(u64 *);
224 /* NULL terminate string */
226 tail += sizeof(char);
229 /* Should never get here */
234 case ACPI_TYPE_STRING:
235 case ACPI_TYPE_BUFFER:
236 switch (format_string[i]) {
238 pointer = (u8 **) head;
240 memcpy(tail, element->string.pointer,
241 element->string.length);
242 head += sizeof(char *);
243 tail += element->string.length * sizeof(char);
244 /* NULL terminate string */
246 tail += sizeof(char);
249 pointer = (u8 **) head;
251 memcpy(tail, element->buffer.pointer,
252 element->buffer.length);
253 head += sizeof(u8 *);
254 tail += element->buffer.length;
257 /* Should never get here */
261 case ACPI_TYPE_LOCAL_REFERENCE:
262 switch (format_string[i]) {
265 (void *)element->reference.handle;
266 head += sizeof(void *);
269 /* Should never get here */
273 case ACPI_TYPE_PACKAGE:
274 /* TBD: handle nested packages... */
276 /* Should never get here */
284 EXPORT_SYMBOL(acpi_extract_package);
287 acpi_evaluate_integer(acpi_handle handle,
288 acpi_string pathname,
289 struct acpi_object_list *arguments, unsigned long long *data)
291 acpi_status status = AE_OK;
292 union acpi_object element;
293 struct acpi_buffer buffer = { 0, NULL };
296 return AE_BAD_PARAMETER;
298 buffer.length = sizeof(union acpi_object);
299 buffer.pointer = &element;
300 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
301 if (ACPI_FAILURE(status)) {
302 acpi_util_eval_error(handle, pathname, status);
306 if (element.type != ACPI_TYPE_INTEGER) {
307 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
311 *data = element.integer.value;
313 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
318 EXPORT_SYMBOL(acpi_evaluate_integer);
321 acpi_evaluate_reference(acpi_handle handle,
322 acpi_string pathname,
323 struct acpi_object_list *arguments,
324 struct acpi_handle_list *list)
326 acpi_status status = AE_OK;
327 union acpi_object *package = NULL;
328 union acpi_object *element = NULL;
329 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334 return AE_BAD_PARAMETER;
337 /* Evaluate object. */
339 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
340 if (ACPI_FAILURE(status))
343 package = buffer.pointer;
345 if ((buffer.length == 0) || !package) {
346 status = AE_BAD_DATA;
347 acpi_util_eval_error(handle, pathname, status);
350 if (package->type != ACPI_TYPE_PACKAGE) {
351 status = AE_BAD_DATA;
352 acpi_util_eval_error(handle, pathname, status);
355 if (!package->package.count) {
356 status = AE_BAD_DATA;
357 acpi_util_eval_error(handle, pathname, status);
361 if (package->package.count > ACPI_MAX_HANDLES) {
364 list->count = package->package.count;
366 /* Extract package data. */
368 for (i = 0; i < list->count; i++) {
370 element = &(package->package.elements[i]);
372 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
373 status = AE_BAD_DATA;
374 acpi_util_eval_error(handle, pathname, status);
378 if (!element->reference.handle) {
379 status = AE_NULL_ENTRY;
380 acpi_util_eval_error(handle, pathname, status);
383 /* Get the acpi_handle. */
385 list->handles[i] = element->reference.handle;
386 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
391 if (ACPI_FAILURE(status)) {
393 //kfree(list->handles);
396 kfree(buffer.pointer);
401 EXPORT_SYMBOL(acpi_evaluate_reference);
404 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
407 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
408 union acpi_object *output;
410 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
412 if (ACPI_FAILURE(status))
415 output = buffer.pointer;
417 if (!output || output->type != ACPI_TYPE_PACKAGE
418 || !output->package.count
419 || output->package.elements[0].type != ACPI_TYPE_BUFFER
420 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
425 status = acpi_decode_pld_buffer(
426 output->package.elements[0].buffer.pointer,
427 output->package.elements[0].buffer.length,
431 kfree(buffer.pointer);
434 EXPORT_SYMBOL(acpi_get_physical_device_location);
437 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
438 * @handle: ACPI device handle
439 * @source_event: source event code
440 * @status_code: status code
441 * @status_buf: optional detailed information (NULL if none)
443 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
444 * must call this function when evaluating _OST for hotplug operations.
445 * When the platform does not support _OST, this function has no effect.
448 acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
449 struct acpi_buffer *status_buf)
451 union acpi_object params[3] = {
452 {.type = ACPI_TYPE_INTEGER,},
453 {.type = ACPI_TYPE_INTEGER,},
454 {.type = ACPI_TYPE_BUFFER,}
456 struct acpi_object_list arg_list = {3, params};
458 params[0].integer.value = source_event;
459 params[1].integer.value = status_code;
460 if (status_buf != NULL) {
461 params[2].buffer.pointer = status_buf->pointer;
462 params[2].buffer.length = status_buf->length;
464 params[2].buffer.pointer = NULL;
465 params[2].buffer.length = 0;
468 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
470 EXPORT_SYMBOL(acpi_evaluate_ost);
473 * acpi_handle_path: Return the object path of handle
475 * Caller must free the returned buffer
477 static char *acpi_handle_path(acpi_handle handle)
479 struct acpi_buffer buffer = {
480 .length = ACPI_ALLOCATE_BUFFER,
484 if (in_interrupt() ||
485 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
487 return buffer.pointer;
491 * acpi_handle_printk: Print message with ACPI prefix and object path
493 * This function is called through acpi_handle_<level> macros and prints
494 * a message with ACPI prefix and object path. This function acquires
495 * the global namespace mutex to obtain an object path. In interrupt
496 * context, it shows the object path as <n/a>.
499 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
501 struct va_format vaf;
509 path = acpi_handle_path(handle);
510 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
515 EXPORT_SYMBOL(acpi_handle_printk);
517 #if defined(CONFIG_DYNAMIC_DEBUG)
519 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
521 * This function is called through acpi_handle_debug macro and debug
522 * prints a message with ACPI prefix and object path. This function
523 * acquires the global namespace mutex to obtain an object path. In
524 * interrupt context, it shows the object path as <n/a>.
527 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
528 const char *fmt, ...)
530 struct va_format vaf;
538 path = acpi_handle_path(handle);
539 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
544 EXPORT_SYMBOL(__acpi_handle_debug);
548 * acpi_has_method: Check whether @handle has a method named @name
549 * @handle: ACPI device handle
550 * @name: name of object or method
552 * Check whether @handle has a method named @name.
554 bool acpi_has_method(acpi_handle handle, char *name)
558 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
560 EXPORT_SYMBOL(acpi_has_method);
562 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
565 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
566 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
568 obj.integer.value = arg;
570 return acpi_evaluate_object(handle, method, &arg_list, NULL);
572 EXPORT_SYMBOL(acpi_execute_simple_method);
575 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
576 * @handle: ACPI device handle
578 * Evaluate device's _EJ0 method for hotplug operations.
580 acpi_status acpi_evaluate_ej0(acpi_handle handle)
584 status = acpi_execute_simple_method(handle, "_EJ0", 1);
585 if (status == AE_NOT_FOUND)
586 acpi_handle_warn(handle, "No _EJ0 support for device\n");
587 else if (ACPI_FAILURE(status))
588 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
594 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
595 * @handle: ACPI device handle
596 * @lock: lock device if non-zero, otherwise unlock device
598 * Evaluate device's _LCK method if present to lock/unlock device
600 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
604 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
605 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
607 acpi_handle_warn(handle,
608 "Locking device failed (0x%x)\n", status);
610 acpi_handle_warn(handle,
611 "Unlocking device failed (0x%x)\n", status);
618 * acpi_evaluate_dsm - evaluate device's _DSM method
619 * @handle: ACPI device handle
620 * @uuid: UUID of requested functions, should be 16 bytes
621 * @rev: revision number of requested function
622 * @func: requested function number
623 * @argv4: the function specific parameter
625 * Evaluate device's _DSM method with specified UUID, revision id and
626 * function number. Caller needs to free the returned object.
628 * Though ACPI defines the fourth parameter for _DSM should be a package,
629 * some old BIOSes do expect a buffer or an integer etc.
632 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
633 union acpi_object *argv4)
636 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
637 union acpi_object params[4];
638 struct acpi_object_list input = {
643 params[0].type = ACPI_TYPE_BUFFER;
644 params[0].buffer.length = 16;
645 params[0].buffer.pointer = (char *)uuid;
646 params[1].type = ACPI_TYPE_INTEGER;
647 params[1].integer.value = rev;
648 params[2].type = ACPI_TYPE_INTEGER;
649 params[2].integer.value = func;
653 params[3].type = ACPI_TYPE_PACKAGE;
654 params[3].package.count = 0;
655 params[3].package.elements = NULL;
658 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
659 if (ACPI_SUCCESS(ret))
660 return (union acpi_object *)buf.pointer;
662 if (ret != AE_NOT_FOUND)
663 acpi_handle_warn(handle,
664 "failed to evaluate _DSM (0x%x)\n", ret);
668 EXPORT_SYMBOL(acpi_evaluate_dsm);
671 * acpi_check_dsm - check if _DSM method supports requested functions.
672 * @handle: ACPI device handle
673 * @uuid: UUID of requested functions, should be 16 bytes at least
674 * @rev: revision number of requested functions
675 * @funcs: bitmap of requested functions
677 * Evaluate device's _DSM method to check whether it supports requested
678 * functions. Currently only support 64 functions at maximum, should be
681 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
685 union acpi_object *obj;
690 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
694 /* For compatibility, old BIOSes may return an integer */
695 if (obj->type == ACPI_TYPE_INTEGER)
696 mask = obj->integer.value;
697 else if (obj->type == ACPI_TYPE_BUFFER)
698 for (i = 0; i < obj->buffer.length && i < 8; i++)
699 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
703 * Bit 0 indicates whether there's support for any functions other than
704 * function 0 for the specified UUID and revision.
706 if ((mask & 0x1) && (mask & funcs) == funcs)
711 EXPORT_SYMBOL(acpi_check_dsm);
714 * acpi_dev_present - Detect presence of a given ACPI device in the system.
715 * @hid: Hardware ID of the device.
717 * Return %true if the device was present at the moment of invocation.
718 * Note that if the device is pluggable, it may since have disappeared.
720 * For this function to work, acpi_bus_scan() must have been executed
721 * which happens in the subsys_initcall() subsection. Hence, do not
722 * call from a subsys_initcall() or earlier (use acpi_get_devices()
723 * instead). Calling from module_init() is fine (which is synonymous
724 * with device_initcall()).
726 bool acpi_dev_present(const char *hid)
728 struct acpi_device_bus_id *acpi_device_bus_id;
731 mutex_lock(&acpi_device_lock);
732 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
733 if (!strcmp(acpi_device_bus_id->bus_id, hid)) {
737 mutex_unlock(&acpi_device_lock);
741 EXPORT_SYMBOL(acpi_dev_present);
744 * acpi_backlight= handling, this is done here rather then in video_detect.c
745 * because __setup cannot be used in modules.
747 char acpi_video_backlight_string[16];
748 EXPORT_SYMBOL(acpi_video_backlight_string);
750 static int __init acpi_backlight(char *str)
752 strlcpy(acpi_video_backlight_string, str,
753 sizeof(acpi_video_backlight_string));
756 __setup("acpi_backlight=", acpi_backlight);