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>
33 #define _COMPONENT ACPI_BUS_COMPONENT
34 ACPI_MODULE_NAME("utils");
36 /* --------------------------------------------------------------------------
37 Object Evaluation Helpers
38 -------------------------------------------------------------------------- */
40 acpi_util_eval_error(acpi_handle h
, acpi_string p
, acpi_status s
)
42 #ifdef ACPI_DEBUG_OUTPUT
43 char prefix
[80] = {'\0'};
44 struct acpi_buffer buffer
= {sizeof(prefix
), prefix
};
45 acpi_get_name(h
, ACPI_FULL_PATHNAME
, &buffer
);
46 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Evaluate [%s.%s]: %s\n",
47 (char *) prefix
, p
, acpi_format_exception(s
)));
54 acpi_extract_package(union acpi_object
*package
,
55 struct acpi_buffer
*format
, struct acpi_buffer
*buffer
)
57 u32 size_required
= 0;
59 char *format_string
= NULL
;
66 if (!package
|| (package
->type
!= ACPI_TYPE_PACKAGE
)
67 || (package
->package
.count
< 1)) {
68 printk(KERN_WARNING PREFIX
"Invalid package argument\n");
69 return AE_BAD_PARAMETER
;
72 if (!format
|| !format
->pointer
|| (format
->length
< 1)) {
73 printk(KERN_WARNING PREFIX
"Invalid format argument\n");
74 return AE_BAD_PARAMETER
;
78 printk(KERN_WARNING PREFIX
"Invalid buffer argument\n");
79 return AE_BAD_PARAMETER
;
82 format_count
= (format
->length
/ sizeof(char)) - 1;
83 if (format_count
> package
->package
.count
) {
84 printk(KERN_WARNING PREFIX
"Format specifies more objects [%d]"
85 " than exist in package [%d].\n",
86 format_count
, package
->package
.count
);
90 format_string
= format
->pointer
;
93 * Calculate size_required.
95 for (i
= 0; i
< format_count
; i
++) {
97 union acpi_object
*element
= &(package
->package
.elements
[i
]);
99 switch (element
->type
) {
101 case ACPI_TYPE_INTEGER
:
102 switch (format_string
[i
]) {
104 size_required
+= sizeof(u64
);
105 tail_offset
+= sizeof(u64
);
109 sizeof(char *) + sizeof(u64
) +
111 tail_offset
+= sizeof(char *);
114 printk(KERN_WARNING PREFIX
"Invalid package element"
115 " [%d]: got number, expecting"
117 i
, format_string
[i
]);
123 case ACPI_TYPE_STRING
:
124 case ACPI_TYPE_BUFFER
:
125 switch (format_string
[i
]) {
129 (element
->string
.length
* sizeof(char)) +
131 tail_offset
+= sizeof(char *);
135 sizeof(u8
*) + element
->buffer
.length
;
136 tail_offset
+= sizeof(u8
*);
139 printk(KERN_WARNING PREFIX
"Invalid package element"
140 " [%d] got string/buffer,"
142 i
, format_string
[i
]);
147 case ACPI_TYPE_LOCAL_REFERENCE
:
148 switch (format_string
[i
]) {
150 size_required
+= sizeof(void *);
151 tail_offset
+= sizeof(void *);
154 printk(KERN_WARNING PREFIX
"Invalid package element"
155 " [%d] got reference,"
157 i
, format_string
[i
]);
163 case ACPI_TYPE_PACKAGE
:
165 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
166 "Found unsupported element at index=%d\n",
168 /* TBD: handle nested packages... */
175 * Validate output buffer.
177 if (buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
178 buffer
->pointer
= ACPI_ALLOCATE_ZEROED(size_required
);
179 if (!buffer
->pointer
)
181 buffer
->length
= size_required
;
183 if (buffer
->length
< size_required
) {
184 buffer
->length
= size_required
;
185 return AE_BUFFER_OVERFLOW
;
186 } else if (buffer
->length
!= size_required
||
188 return AE_BAD_PARAMETER
;
192 head
= buffer
->pointer
;
193 tail
= buffer
->pointer
+ tail_offset
;
196 * Extract package data.
198 for (i
= 0; i
< format_count
; i
++) {
201 union acpi_object
*element
= &(package
->package
.elements
[i
]);
207 switch (element
->type
) {
209 case ACPI_TYPE_INTEGER
:
210 switch (format_string
[i
]) {
213 element
->integer
.value
;
217 pointer
= (u8
**) head
;
220 element
->integer
.value
;
221 head
+= sizeof(u64
*);
223 /* NULL terminate string */
225 tail
+= sizeof(char);
228 /* Should never get here */
233 case ACPI_TYPE_STRING
:
234 case ACPI_TYPE_BUFFER
:
235 switch (format_string
[i
]) {
237 pointer
= (u8
**) head
;
239 memcpy(tail
, element
->string
.pointer
,
240 element
->string
.length
);
241 head
+= sizeof(char *);
242 tail
+= element
->string
.length
* sizeof(char);
243 /* NULL terminate string */
245 tail
+= sizeof(char);
248 pointer
= (u8
**) head
;
250 memcpy(tail
, element
->buffer
.pointer
,
251 element
->buffer
.length
);
252 head
+= sizeof(u8
*);
253 tail
+= element
->buffer
.length
;
256 /* Should never get here */
260 case ACPI_TYPE_LOCAL_REFERENCE
:
261 switch (format_string
[i
]) {
264 (void *)element
->reference
.handle
;
265 head
+= sizeof(void *);
268 /* Should never get here */
272 case ACPI_TYPE_PACKAGE
:
273 /* TBD: handle nested packages... */
275 /* Should never get here */
283 EXPORT_SYMBOL(acpi_extract_package
);
286 acpi_evaluate_integer(acpi_handle handle
,
287 acpi_string pathname
,
288 struct acpi_object_list
*arguments
, unsigned long long *data
)
290 acpi_status status
= AE_OK
;
291 union acpi_object element
;
292 struct acpi_buffer buffer
= { 0, NULL
};
295 return AE_BAD_PARAMETER
;
297 buffer
.length
= sizeof(union acpi_object
);
298 buffer
.pointer
= &element
;
299 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
300 if (ACPI_FAILURE(status
)) {
301 acpi_util_eval_error(handle
, pathname
, status
);
305 if (element
.type
!= ACPI_TYPE_INTEGER
) {
306 acpi_util_eval_error(handle
, pathname
, AE_BAD_DATA
);
310 *data
= element
.integer
.value
;
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%llu]\n", *data
));
317 EXPORT_SYMBOL(acpi_evaluate_integer
);
320 acpi_evaluate_reference(acpi_handle handle
,
321 acpi_string pathname
,
322 struct acpi_object_list
*arguments
,
323 struct acpi_handle_list
*list
)
325 acpi_status status
= AE_OK
;
326 union acpi_object
*package
= NULL
;
327 union acpi_object
*element
= NULL
;
328 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
333 return AE_BAD_PARAMETER
;
336 /* Evaluate object. */
338 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
339 if (ACPI_FAILURE(status
))
342 package
= buffer
.pointer
;
344 if ((buffer
.length
== 0) || !package
) {
345 status
= AE_BAD_DATA
;
346 acpi_util_eval_error(handle
, pathname
, status
);
349 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
350 status
= AE_BAD_DATA
;
351 acpi_util_eval_error(handle
, pathname
, status
);
354 if (!package
->package
.count
) {
355 status
= AE_BAD_DATA
;
356 acpi_util_eval_error(handle
, pathname
, status
);
360 if (package
->package
.count
> ACPI_MAX_HANDLES
) {
363 list
->count
= package
->package
.count
;
365 /* Extract package data. */
367 for (i
= 0; i
< list
->count
; i
++) {
369 element
= &(package
->package
.elements
[i
]);
371 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
372 status
= AE_BAD_DATA
;
373 acpi_util_eval_error(handle
, pathname
, status
);
377 if (!element
->reference
.handle
) {
378 status
= AE_NULL_ENTRY
;
379 acpi_util_eval_error(handle
, pathname
, status
);
382 /* Get the acpi_handle. */
384 list
->handles
[i
] = element
->reference
.handle
;
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found reference [%p]\n",
390 if (ACPI_FAILURE(status
)) {
392 //kfree(list->handles);
395 kfree(buffer
.pointer
);
400 EXPORT_SYMBOL(acpi_evaluate_reference
);
403 acpi_get_physical_device_location(acpi_handle handle
, struct acpi_pld_info
**pld
)
406 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
407 union acpi_object
*output
;
409 status
= acpi_evaluate_object(handle
, "_PLD", NULL
, &buffer
);
411 if (ACPI_FAILURE(status
))
414 output
= buffer
.pointer
;
416 if (!output
|| output
->type
!= ACPI_TYPE_PACKAGE
417 || !output
->package
.count
418 || output
->package
.elements
[0].type
!= ACPI_TYPE_BUFFER
419 || output
->package
.elements
[0].buffer
.length
< ACPI_PLD_REV1_BUFFER_SIZE
) {
424 status
= acpi_decode_pld_buffer(
425 output
->package
.elements
[0].buffer
.pointer
,
426 output
->package
.elements
[0].buffer
.length
,
430 kfree(buffer
.pointer
);
433 EXPORT_SYMBOL(acpi_get_physical_device_location
);
436 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
437 * @handle: ACPI device handle
438 * @source_event: source event code
439 * @status_code: status code
440 * @status_buf: optional detailed information (NULL if none)
442 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
443 * must call this function when evaluating _OST for hotplug operations.
444 * When the platform does not support _OST, this function has no effect.
447 acpi_evaluate_ost(acpi_handle handle
, u32 source_event
, u32 status_code
,
448 struct acpi_buffer
*status_buf
)
450 union acpi_object params
[3] = {
451 {.type
= ACPI_TYPE_INTEGER
,},
452 {.type
= ACPI_TYPE_INTEGER
,},
453 {.type
= ACPI_TYPE_BUFFER
,}
455 struct acpi_object_list arg_list
= {3, params
};
457 params
[0].integer
.value
= source_event
;
458 params
[1].integer
.value
= status_code
;
459 if (status_buf
!= NULL
) {
460 params
[2].buffer
.pointer
= status_buf
->pointer
;
461 params
[2].buffer
.length
= status_buf
->length
;
463 params
[2].buffer
.pointer
= NULL
;
464 params
[2].buffer
.length
= 0;
467 return acpi_evaluate_object(handle
, "_OST", &arg_list
, NULL
);
469 EXPORT_SYMBOL(acpi_evaluate_ost
);
472 * acpi_handle_path: Return the object path of handle
474 * Caller must free the returned buffer
476 static char *acpi_handle_path(acpi_handle handle
)
478 struct acpi_buffer buffer
= {
479 .length
= ACPI_ALLOCATE_BUFFER
,
483 if (in_interrupt() ||
484 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
) != AE_OK
)
486 return buffer
.pointer
;
490 * acpi_handle_printk: Print message with ACPI prefix and object path
492 * This function is called through acpi_handle_<level> macros and prints
493 * a message with ACPI prefix and object path. This function acquires
494 * the global namespace mutex to obtain an object path. In interrupt
495 * context, it shows the object path as <n/a>.
498 acpi_handle_printk(const char *level
, acpi_handle handle
, const char *fmt
, ...)
500 struct va_format vaf
;
508 path
= acpi_handle_path(handle
);
509 printk("%sACPI: %s: %pV", level
, path
? path
: "<n/a>" , &vaf
);
514 EXPORT_SYMBOL(acpi_handle_printk
);
516 #if defined(CONFIG_DYNAMIC_DEBUG)
518 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
520 * This function is called through acpi_handle_debug macro and debug
521 * prints a message with ACPI prefix and object path. This function
522 * acquires the global namespace mutex to obtain an object path. In
523 * interrupt context, it shows the object path as <n/a>.
526 __acpi_handle_debug(struct _ddebug
*descriptor
, acpi_handle handle
,
527 const char *fmt
, ...)
529 struct va_format vaf
;
537 path
= acpi_handle_path(handle
);
538 __dynamic_pr_debug(descriptor
, "ACPI: %s: %pV", path
? path
: "<n/a>", &vaf
);
543 EXPORT_SYMBOL(__acpi_handle_debug
);
547 * acpi_has_method: Check whether @handle has a method named @name
548 * @handle: ACPI device handle
549 * @name: name of object or method
551 * Check whether @handle has a method named @name.
553 bool acpi_has_method(acpi_handle handle
, char *name
)
557 return ACPI_SUCCESS(acpi_get_handle(handle
, name
, &tmp
));
559 EXPORT_SYMBOL(acpi_has_method
);
561 acpi_status
acpi_execute_simple_method(acpi_handle handle
, char *method
,
564 union acpi_object obj
= { .type
= ACPI_TYPE_INTEGER
};
565 struct acpi_object_list arg_list
= { .count
= 1, .pointer
= &obj
, };
567 obj
.integer
.value
= arg
;
569 return acpi_evaluate_object(handle
, method
, &arg_list
, NULL
);
571 EXPORT_SYMBOL(acpi_execute_simple_method
);
574 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
575 * @handle: ACPI device handle
577 * Evaluate device's _EJ0 method for hotplug operations.
579 acpi_status
acpi_evaluate_ej0(acpi_handle handle
)
583 status
= acpi_execute_simple_method(handle
, "_EJ0", 1);
584 if (status
== AE_NOT_FOUND
)
585 acpi_handle_warn(handle
, "No _EJ0 support for device\n");
586 else if (ACPI_FAILURE(status
))
587 acpi_handle_warn(handle
, "Eject failed (0x%x)\n", status
);
593 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
594 * @handle: ACPI device handle
595 * @lock: lock device if non-zero, otherwise unlock device
597 * Evaluate device's _LCK method if present to lock/unlock device
599 acpi_status
acpi_evaluate_lck(acpi_handle handle
, int lock
)
603 status
= acpi_execute_simple_method(handle
, "_LCK", !!lock
);
604 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
606 acpi_handle_warn(handle
,
607 "Locking device failed (0x%x)\n", status
);
609 acpi_handle_warn(handle
,
610 "Unlocking device failed (0x%x)\n", status
);
617 * acpi_evaluate_dsm - evaluate device's _DSM method
618 * @handle: ACPI device handle
619 * @uuid: UUID of requested functions, should be 16 bytes
620 * @rev: revision number of requested function
621 * @func: requested function number
622 * @argv4: the function specific parameter
624 * Evaluate device's _DSM method with specified UUID, revision id and
625 * function number. Caller needs to free the returned object.
627 * Though ACPI defines the fourth parameter for _DSM should be a package,
628 * some old BIOSes do expect a buffer or an integer etc.
631 acpi_evaluate_dsm(acpi_handle handle
, const u8
*uuid
, int rev
, int func
,
632 union acpi_object
*argv4
)
635 struct acpi_buffer buf
= {ACPI_ALLOCATE_BUFFER
, NULL
};
636 union acpi_object params
[4];
637 struct acpi_object_list input
= {
642 params
[0].type
= ACPI_TYPE_BUFFER
;
643 params
[0].buffer
.length
= 16;
644 params
[0].buffer
.pointer
= (char *)uuid
;
645 params
[1].type
= ACPI_TYPE_INTEGER
;
646 params
[1].integer
.value
= rev
;
647 params
[2].type
= ACPI_TYPE_INTEGER
;
648 params
[2].integer
.value
= func
;
652 params
[3].type
= ACPI_TYPE_PACKAGE
;
653 params
[3].package
.count
= 0;
654 params
[3].package
.elements
= NULL
;
657 ret
= acpi_evaluate_object(handle
, "_DSM", &input
, &buf
);
658 if (ACPI_SUCCESS(ret
))
659 return (union acpi_object
*)buf
.pointer
;
661 if (ret
!= AE_NOT_FOUND
)
662 acpi_handle_warn(handle
,
663 "failed to evaluate _DSM (0x%x)\n", ret
);
667 EXPORT_SYMBOL(acpi_evaluate_dsm
);
670 * acpi_check_dsm - check if _DSM method supports requested functions.
671 * @handle: ACPI device handle
672 * @uuid: UUID of requested functions, should be 16 bytes at least
673 * @rev: revision number of requested functions
674 * @funcs: bitmap of requested functions
676 * Evaluate device's _DSM method to check whether it supports requested
677 * functions. Currently only support 64 functions at maximum, should be
680 bool acpi_check_dsm(acpi_handle handle
, const u8
*uuid
, int rev
, u64 funcs
)
684 union acpi_object
*obj
;
689 obj
= acpi_evaluate_dsm(handle
, uuid
, rev
, 0, NULL
);
693 /* For compatibility, old BIOSes may return an integer */
694 if (obj
->type
== ACPI_TYPE_INTEGER
)
695 mask
= obj
->integer
.value
;
696 else if (obj
->type
== ACPI_TYPE_BUFFER
)
697 for (i
= 0; i
< obj
->buffer
.length
&& i
< 8; i
++)
698 mask
|= (((u8
)obj
->buffer
.pointer
[i
]) << (i
* 8));
702 * Bit 0 indicates whether there's support for any functions other than
703 * function 0 for the specified UUID and revision.
705 if ((mask
& 0x1) && (mask
& funcs
) == funcs
)
710 EXPORT_SYMBOL(acpi_check_dsm
);
713 * acpi_backlight= handling, this is done here rather then in video_detect.c
714 * because __setup cannot be used in modules.
716 char acpi_video_backlight_string
[16];
717 EXPORT_SYMBOL(acpi_video_backlight_string
);
719 static int __init
acpi_backlight(char *str
)
721 strlcpy(acpi_video_backlight_string
, str
,
722 sizeof(acpi_video_backlight_string
));
725 __setup("acpi_backlight=", acpi_backlight
);