1 /*******************************************************************************
3 * Module Name: utmisc - common utility procedures
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmisc")
50 /*******************************************************************************
52 * FUNCTION: acpi_ut_allocate_owner_id
54 * PARAMETERS: owner_id - Where the new owner ID is returned
58 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
59 * track objects created by the table or method, to be deleted
60 * when the method exits or the table is unloaded.
62 ******************************************************************************/
63 acpi_status
acpi_ut_allocate_owner_id(acpi_owner_id
* owner_id
)
68 ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
70 /* Guard against multiple allocations of ID to the same location */
73 ACPI_REPORT_ERROR(("Owner ID [%2.2X] already exists\n",
75 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
78 /* Mutex for the global ID mask */
80 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
81 if (ACPI_FAILURE(status
)) {
82 return_ACPI_STATUS(status
);
85 /* Find a free owner ID */
87 for (i
= 0; i
< 32; i
++) {
88 if (!(acpi_gbl_owner_id_mask
& (1 << i
))) {
89 ACPI_DEBUG_PRINT((ACPI_DB_VALUES
,
90 "Current owner_id mask: %8.8X New ID: %2.2X\n",
91 acpi_gbl_owner_id_mask
,
92 (unsigned int)(i
+ 1)));
94 acpi_gbl_owner_id_mask
|= (1 << i
);
95 *owner_id
= (acpi_owner_id
) (i
+ 1);
101 * If we are here, all owner_ids have been allocated. This probably should
102 * not happen since the IDs are reused after deallocation. The IDs are
103 * allocated upon table load (one per table) and method execution, and
104 * they are released when a table is unloaded or a method completes
108 status
= AE_OWNER_ID_LIMIT
;
109 ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
112 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
113 return_ACPI_STATUS(status
);
116 /*******************************************************************************
118 * FUNCTION: acpi_ut_release_owner_id
120 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
122 * RETURN: None. No error is returned because we are either exiting a
123 * control method or unloading a table. Either way, we would
124 * ignore any error anyway.
126 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 32
128 ******************************************************************************/
130 void acpi_ut_release_owner_id(acpi_owner_id
* owner_id_ptr
)
132 acpi_owner_id owner_id
= *owner_id_ptr
;
135 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id
);
137 /* Always clear the input owner_id (zero is an invalid ID) */
141 /* Zero is not a valid owner_iD */
143 if ((owner_id
== 0) || (owner_id
> 32)) {
144 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id
));
148 /* Mutex for the global ID mask */
150 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
151 if (ACPI_FAILURE(status
)) {
155 /* Normalize the ID to zero */
159 /* Free the owner ID only if it is valid */
161 if (acpi_gbl_owner_id_mask
& (1 << owner_id
)) {
162 acpi_gbl_owner_id_mask
^= (1 << owner_id
);
165 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
169 /*******************************************************************************
171 * FUNCTION: acpi_ut_strupr (strupr)
173 * PARAMETERS: src_string - The source string to convert
177 * DESCRIPTION: Convert string to uppercase
179 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
181 ******************************************************************************/
183 void acpi_ut_strupr(char *src_string
)
187 ACPI_FUNCTION_ENTRY();
193 /* Walk entire string, uppercasing the letters */
195 for (string
= src_string
; *string
; string
++) {
196 *string
= (char)ACPI_TOUPPER(*string
);
202 /*******************************************************************************
204 * FUNCTION: acpi_ut_print_string
206 * PARAMETERS: String - Null terminated ASCII string
207 * max_length - Maximum output length
211 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
214 ******************************************************************************/
216 void acpi_ut_print_string(char *string
, u8 max_length
)
221 acpi_os_printf("<\"NULL STRING PTR\">");
225 acpi_os_printf("\"");
226 for (i
= 0; string
[i
] && (i
< max_length
); i
++) {
227 /* Escape sequences */
231 acpi_os_printf("\\a"); /* BELL */
235 acpi_os_printf("\\b"); /* BACKSPACE */
239 acpi_os_printf("\\f"); /* FORMFEED */
243 acpi_os_printf("\\n"); /* LINEFEED */
247 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
251 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
255 acpi_os_printf("\\v"); /* VERTICAL TAB */
258 case '\'': /* Single Quote */
259 case '\"': /* Double Quote */
260 case '\\': /* Backslash */
261 acpi_os_printf("\\%c", (int)string
[i
]);
266 /* Check for printable character or hex escape */
268 if (ACPI_IS_PRINT(string
[i
])) {
269 /* This is a normal character */
271 acpi_os_printf("%c", (int)string
[i
]);
273 /* All others will be Hex escapes */
275 acpi_os_printf("\\x%2.2X", (s32
) string
[i
]);
280 acpi_os_printf("\"");
282 if (i
== max_length
&& string
[i
]) {
283 acpi_os_printf("...");
287 /*******************************************************************************
289 * FUNCTION: acpi_ut_dword_byte_swap
291 * PARAMETERS: Value - Value to be converted
293 * RETURN: u32 integer with bytes swapped
295 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
297 ******************************************************************************/
299 u32
acpi_ut_dword_byte_swap(u32 value
)
310 ACPI_FUNCTION_ENTRY();
314 out
.bytes
[0] = in
.bytes
[3];
315 out
.bytes
[1] = in
.bytes
[2];
316 out
.bytes
[2] = in
.bytes
[1];
317 out
.bytes
[3] = in
.bytes
[0];
322 /*******************************************************************************
324 * FUNCTION: acpi_ut_set_integer_width
326 * PARAMETERS: Revision From DSDT header
330 * DESCRIPTION: Set the global integer bit width based upon the revision
331 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
332 * For Revision 2 and above, Integers are 64 bits. Yes, this
333 * makes a difference.
335 ******************************************************************************/
337 void acpi_ut_set_integer_width(u8 revision
)
341 acpi_gbl_integer_bit_width
= 32;
342 acpi_gbl_integer_nybble_width
= 8;
343 acpi_gbl_integer_byte_width
= 4;
345 acpi_gbl_integer_bit_width
= 64;
346 acpi_gbl_integer_nybble_width
= 16;
347 acpi_gbl_integer_byte_width
= 8;
351 #ifdef ACPI_DEBUG_OUTPUT
352 /*******************************************************************************
354 * FUNCTION: acpi_ut_display_init_pathname
356 * PARAMETERS: Type - Object type of the node
357 * obj_handle - Handle whose pathname will be displayed
358 * Path - Additional path string to be appended.
359 * (NULL if no extra path)
361 * RETURN: acpi_status
363 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
365 ******************************************************************************/
368 acpi_ut_display_init_pathname(u8 type
,
369 struct acpi_namespace_node
*obj_handle
,
373 struct acpi_buffer buffer
;
375 ACPI_FUNCTION_ENTRY();
377 /* Only print the path if the appropriate debug level is enabled */
379 if (!(acpi_dbg_level
& ACPI_LV_INIT_NAMES
)) {
383 /* Get the full pathname to the node */
385 buffer
.length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
386 status
= acpi_ns_handle_to_pathname(obj_handle
, &buffer
);
387 if (ACPI_FAILURE(status
)) {
391 /* Print what we're doing */
394 case ACPI_TYPE_METHOD
:
395 acpi_os_printf("Executing ");
399 acpi_os_printf("Initializing ");
403 /* Print the object type and pathname */
405 acpi_os_printf("%-12s %s",
406 acpi_ut_get_type_name(type
), (char *)buffer
.pointer
);
408 /* Extra path is used to append names like _STA, _INI, etc. */
411 acpi_os_printf(".%s", path
);
413 acpi_os_printf("\n");
415 ACPI_MEM_FREE(buffer
.pointer
);
419 /*******************************************************************************
421 * FUNCTION: acpi_ut_valid_acpi_name
423 * PARAMETERS: Name - The name to be examined
425 * RETURN: TRUE if the name is valid, FALSE otherwise
427 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
428 * 1) Upper case alpha
432 ******************************************************************************/
434 u8
acpi_ut_valid_acpi_name(u32 name
)
436 char *name_ptr
= (char *)&name
;
440 ACPI_FUNCTION_ENTRY();
442 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++) {
443 character
= *name_ptr
;
446 if (!((character
== '_') ||
447 (character
>= 'A' && character
<= 'Z') ||
448 (character
>= '0' && character
<= '9'))) {
456 /*******************************************************************************
458 * FUNCTION: acpi_ut_valid_acpi_character
460 * PARAMETERS: Character - The character to be examined
462 * RETURN: 1 if Character may appear in a name, else 0
464 * DESCRIPTION: Check for a printable character
466 ******************************************************************************/
468 u8
acpi_ut_valid_acpi_character(char character
)
471 ACPI_FUNCTION_ENTRY();
473 return ((u8
) ((character
== '_') ||
474 (character
>= 'A' && character
<= 'Z') ||
475 (character
>= '0' && character
<= '9')));
478 /*******************************************************************************
480 * FUNCTION: acpi_ut_strtoul64
482 * PARAMETERS: String - Null terminated string
483 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
484 * ret_integer - Where the converted integer is returned
486 * RETURN: Status and Converted value
488 * DESCRIPTION: Convert a string into an unsigned value.
489 * NOTE: Does not support Octal strings, not needed.
491 ******************************************************************************/
494 acpi_ut_strtoul64(char *string
, u32 base
, acpi_integer
* ret_integer
)
497 acpi_integer return_value
= 0;
498 acpi_integer quotient
;
500 ACPI_FUNCTION_TRACE("ut_stroul64");
502 if ((!string
) || !(*string
)) {
514 return_ACPI_STATUS(AE_BAD_PARAMETER
);
517 /* Skip over any white space in the buffer */
519 while (ACPI_IS_SPACE(*string
) || *string
== '\t') {
524 * If the input parameter Base is zero, then we need to
525 * determine if it is decimal or hexadecimal:
528 if ((*string
== '0') && (ACPI_TOLOWER(*(string
+ 1)) == 'x')) {
537 * For hexadecimal base, skip over the leading
538 * 0 or 0x, if they are present.
541 (*string
== '0') && (ACPI_TOLOWER(*(string
+ 1)) == 'x')) {
545 /* Any string left? */
551 /* Main loop: convert the string to a 64-bit integer */
554 if (ACPI_IS_DIGIT(*string
)) {
555 /* Convert ASCII 0-9 to Decimal value */
557 this_digit
= ((u8
) * string
) - '0';
560 /* Digit is out of range */
565 this_digit
= (u8
) ACPI_TOUPPER(*string
);
566 if (ACPI_IS_XDIGIT((char)this_digit
)) {
567 /* Convert ASCII Hex char to value */
569 this_digit
= this_digit
- 'A' + 10;
572 * We allow non-hex chars, just stop now, same as end-of-string.
573 * See ACPI spec, string-to-integer conversion.
579 /* Divide the digit into the correct position */
582 acpi_ut_short_divide((ACPI_INTEGER_MAX
-
583 (acpi_integer
) this_digit
), base
,
585 if (return_value
> quotient
) {
589 return_value
*= base
;
590 return_value
+= this_digit
;
594 /* All done, normal exit */
596 *ret_integer
= return_value
;
597 return_ACPI_STATUS(AE_OK
);
600 /* Base was set/validated above */
603 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT
);
605 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT
);
609 /*******************************************************************************
611 * FUNCTION: acpi_ut_create_update_state_and_push
613 * PARAMETERS: Object - Object to be added to the new state
614 * Action - Increment/Decrement
615 * state_list - List the state will be added to
619 * DESCRIPTION: Create a new state and push it
621 ******************************************************************************/
624 acpi_ut_create_update_state_and_push(union acpi_operand_object
*object
,
626 union acpi_generic_state
**state_list
)
628 union acpi_generic_state
*state
;
630 ACPI_FUNCTION_ENTRY();
632 /* Ignore null objects; these are expected */
638 state
= acpi_ut_create_update_state(object
, action
);
640 return (AE_NO_MEMORY
);
643 acpi_ut_push_generic_state(state_list
, state
);
647 /*******************************************************************************
649 * FUNCTION: acpi_ut_walk_package_tree
651 * PARAMETERS: source_object - The package to walk
652 * target_object - Target object (if package is being copied)
653 * walk_callback - Called once for each package element
654 * Context - Passed to the callback function
658 * DESCRIPTION: Walk through a package
660 ******************************************************************************/
663 acpi_ut_walk_package_tree(union acpi_operand_object
* source_object
,
665 acpi_pkg_callback walk_callback
, void *context
)
667 acpi_status status
= AE_OK
;
668 union acpi_generic_state
*state_list
= NULL
;
669 union acpi_generic_state
*state
;
671 union acpi_operand_object
*this_source_obj
;
673 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
675 state
= acpi_ut_create_pkg_state(source_object
, target_object
, 0);
677 return_ACPI_STATUS(AE_NO_MEMORY
);
681 /* Get one element of the package */
683 this_index
= state
->pkg
.index
;
684 this_source_obj
= (union acpi_operand_object
*)
685 state
->pkg
.source_object
->package
.elements
[this_index
];
689 * 1) An uninitialized package element. It is completely
690 * legal to declare a package and leave it uninitialized
691 * 2) Not an internal object - can be a namespace node instead
692 * 3) Any type other than a package. Packages are handled in else
695 if ((!this_source_obj
) ||
696 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj
) !=
697 ACPI_DESC_TYPE_OPERAND
)
698 || (ACPI_GET_OBJECT_TYPE(this_source_obj
) !=
699 ACPI_TYPE_PACKAGE
)) {
701 walk_callback(ACPI_COPY_TYPE_SIMPLE
,
702 this_source_obj
, state
, context
);
703 if (ACPI_FAILURE(status
)) {
704 return_ACPI_STATUS(status
);
708 while (state
->pkg
.index
>=
709 state
->pkg
.source_object
->package
.count
) {
711 * We've handled all of the objects at this level, This means
712 * that we have just completed a package. That package may
713 * have contained one or more packages itself.
715 * Delete this state and pop the previous state (package).
717 acpi_ut_delete_generic_state(state
);
718 state
= acpi_ut_pop_generic_state(&state_list
);
720 /* Finished when there are no more states */
724 * We have handled all of the objects in the top level
725 * package just add the length of the package objects
728 return_ACPI_STATUS(AE_OK
);
732 * Go back up a level and move the index past the just
733 * completed package object.
738 /* This is a subobject of type package */
741 walk_callback(ACPI_COPY_TYPE_PACKAGE
,
742 this_source_obj
, state
, context
);
743 if (ACPI_FAILURE(status
)) {
744 return_ACPI_STATUS(status
);
748 * Push the current state and create a new one
749 * The callback above returned a new target package object.
751 acpi_ut_push_generic_state(&state_list
, state
);
752 state
= acpi_ut_create_pkg_state(this_source_obj
,
756 return_ACPI_STATUS(AE_NO_MEMORY
);
761 /* We should never get here */
763 return_ACPI_STATUS(AE_AML_INTERNAL
);
766 /*******************************************************************************
768 * FUNCTION: acpi_ut_generate_checksum
770 * PARAMETERS: Buffer - Buffer to be scanned
771 * Length - number of bytes to examine
773 * RETURN: The generated checksum
775 * DESCRIPTION: Generate a checksum on a raw buffer
777 ******************************************************************************/
779 u8
acpi_ut_generate_checksum(u8
* buffer
, u32 length
)
784 for (i
= 0; i
< length
; i
++) {
785 sum
= (signed char)(sum
+ buffer
[i
]);
788 return ((u8
) (0 - sum
));
791 /*******************************************************************************
793 * FUNCTION: acpi_ut_get_resource_end_tag
795 * PARAMETERS: obj_desc - The resource template buffer object
797 * RETURN: Pointer to the end tag
799 * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
801 ******************************************************************************/
803 u8
*acpi_ut_get_resource_end_tag(union acpi_operand_object
* obj_desc
)
809 buffer
= obj_desc
->buffer
.pointer
;
810 end_buffer
= buffer
+ obj_desc
->buffer
.length
;
812 while (buffer
< end_buffer
) {
813 buffer_byte
= *buffer
;
814 if (buffer_byte
& ACPI_RDESC_TYPE_MASK
) {
815 /* Large Descriptor - Length is next 2 bytes */
817 buffer
+= ((*(buffer
+ 1) | (*(buffer
+ 2) << 8)) + 3);
819 /* Small Descriptor. End Tag will be found here */
821 if ((buffer_byte
& ACPI_RDESC_SMALL_MASK
) ==
822 ACPI_RDESC_TYPE_END_TAG
) {
823 /* Found the end tag descriptor, all done. */
828 /* Length is in the header */
830 buffer
+= ((buffer_byte
& 0x07) + 1);
834 /* End tag not found */
839 /*******************************************************************************
841 * FUNCTION: acpi_ut_report_error
843 * PARAMETERS: module_name - Caller's module name (for error output)
844 * line_number - Caller's line number (for error output)
845 * component_id - Caller's component ID (for error output)
849 * DESCRIPTION: Print error message
851 ******************************************************************************/
853 void acpi_ut_report_error(char *module_name
, u32 line_number
, u32 component_id
)
856 acpi_os_printf("%8s-%04d: *** Error: ", module_name
, line_number
);
859 /*******************************************************************************
861 * FUNCTION: acpi_ut_report_warning
863 * PARAMETERS: module_name - Caller's module name (for error output)
864 * line_number - Caller's line number (for error output)
865 * component_id - Caller's component ID (for error output)
869 * DESCRIPTION: Print warning message
871 ******************************************************************************/
874 acpi_ut_report_warning(char *module_name
, u32 line_number
, u32 component_id
)
877 acpi_os_printf("%8s-%04d: *** Warning: ", module_name
, line_number
);
880 /*******************************************************************************
882 * FUNCTION: acpi_ut_report_info
884 * PARAMETERS: module_name - Caller's module name (for error output)
885 * line_number - Caller's line number (for error output)
886 * component_id - Caller's component ID (for error output)
890 * DESCRIPTION: Print information message
892 ******************************************************************************/
894 void acpi_ut_report_info(char *module_name
, u32 line_number
, u32 component_id
)
897 acpi_os_printf("%8s-%04d: *** Info: ", module_name
, line_number
);