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
)
69 ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
71 /* Guard against multiple allocations of ID to the same location */
74 ACPI_REPORT_ERROR(("Owner ID [%2.2X] already exists\n",
76 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
79 /* Mutex for the global ID mask */
81 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
82 if (ACPI_FAILURE(status
)) {
83 return_ACPI_STATUS(status
);
87 * Find a free owner ID, cycle through all possible IDs on repeated
88 * allocations. Note: Index for next possible ID is equal to the value
89 * of the last allocated ID.
91 for (i
= 0, j
= acpi_gbl_last_owner_id
; i
< 32; i
++, j
++) {
93 j
= 0; /* Wraparound to ID start */
96 if (!(acpi_gbl_owner_id_mask
& (1 << j
))) {
98 * Found a free ID. The actual ID is the bit index plus one,
99 * making zero an invalid Owner ID. Save this as the last ID
100 * allocated and update the global ID mask.
102 acpi_gbl_last_owner_id
= (acpi_owner_id
) (j
+ 1);
103 *owner_id
= acpi_gbl_last_owner_id
;
105 ACPI_DEBUG_PRINT((ACPI_DB_VALUES
,
106 "Current owner_id mask: %8.8X New ID: %2.2X\n",
107 acpi_gbl_owner_id_mask
,
109 acpi_gbl_last_owner_id
));
111 acpi_gbl_owner_id_mask
|= (1 << j
);
117 * All owner_ids have been allocated. This typically should
118 * not happen since the IDs are reused after deallocation. The IDs are
119 * allocated upon table load (one per table) and method execution, and
120 * they are released when a table is unloaded or a method completes
123 * If this error happens, there may be very deep nesting of invoked control
124 * methods, or there may be a bug where the IDs are not released.
126 status
= AE_OWNER_ID_LIMIT
;
127 ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
130 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
131 return_ACPI_STATUS(status
);
134 /*******************************************************************************
136 * FUNCTION: acpi_ut_release_owner_id
138 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
140 * RETURN: None. No error is returned because we are either exiting a
141 * control method or unloading a table. Either way, we would
142 * ignore any error anyway.
144 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 32
146 ******************************************************************************/
148 void acpi_ut_release_owner_id(acpi_owner_id
* owner_id_ptr
)
150 acpi_owner_id owner_id
= *owner_id_ptr
;
153 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id
);
155 /* Always clear the input owner_id (zero is an invalid ID) */
159 /* Zero is not a valid owner_iD */
161 if ((owner_id
== 0) || (owner_id
> 32)) {
162 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id
));
166 /* Mutex for the global ID mask */
168 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
169 if (ACPI_FAILURE(status
)) {
173 /* Normalize the ID to zero */
177 /* Free the owner ID only if it is valid */
179 if (acpi_gbl_owner_id_mask
& (1 << owner_id
)) {
180 acpi_gbl_owner_id_mask
^= (1 << owner_id
);
183 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
187 /*******************************************************************************
189 * FUNCTION: acpi_ut_strupr (strupr)
191 * PARAMETERS: src_string - The source string to convert
195 * DESCRIPTION: Convert string to uppercase
197 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
199 ******************************************************************************/
201 void acpi_ut_strupr(char *src_string
)
205 ACPI_FUNCTION_ENTRY();
211 /* Walk entire string, uppercasing the letters */
213 for (string
= src_string
; *string
; string
++) {
214 *string
= (char)ACPI_TOUPPER(*string
);
220 /*******************************************************************************
222 * FUNCTION: acpi_ut_print_string
224 * PARAMETERS: String - Null terminated ASCII string
225 * max_length - Maximum output length
229 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
232 ******************************************************************************/
234 void acpi_ut_print_string(char *string
, u8 max_length
)
239 acpi_os_printf("<\"NULL STRING PTR\">");
243 acpi_os_printf("\"");
244 for (i
= 0; string
[i
] && (i
< max_length
); i
++) {
245 /* Escape sequences */
249 acpi_os_printf("\\a"); /* BELL */
253 acpi_os_printf("\\b"); /* BACKSPACE */
257 acpi_os_printf("\\f"); /* FORMFEED */
261 acpi_os_printf("\\n"); /* LINEFEED */
265 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
269 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
273 acpi_os_printf("\\v"); /* VERTICAL TAB */
276 case '\'': /* Single Quote */
277 case '\"': /* Double Quote */
278 case '\\': /* Backslash */
279 acpi_os_printf("\\%c", (int)string
[i
]);
284 /* Check for printable character or hex escape */
286 if (ACPI_IS_PRINT(string
[i
])) {
287 /* This is a normal character */
289 acpi_os_printf("%c", (int)string
[i
]);
291 /* All others will be Hex escapes */
293 acpi_os_printf("\\x%2.2X", (s32
) string
[i
]);
298 acpi_os_printf("\"");
300 if (i
== max_length
&& string
[i
]) {
301 acpi_os_printf("...");
305 /*******************************************************************************
307 * FUNCTION: acpi_ut_dword_byte_swap
309 * PARAMETERS: Value - Value to be converted
311 * RETURN: u32 integer with bytes swapped
313 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
315 ******************************************************************************/
317 u32
acpi_ut_dword_byte_swap(u32 value
)
328 ACPI_FUNCTION_ENTRY();
332 out
.bytes
[0] = in
.bytes
[3];
333 out
.bytes
[1] = in
.bytes
[2];
334 out
.bytes
[2] = in
.bytes
[1];
335 out
.bytes
[3] = in
.bytes
[0];
340 /*******************************************************************************
342 * FUNCTION: acpi_ut_set_integer_width
344 * PARAMETERS: Revision From DSDT header
348 * DESCRIPTION: Set the global integer bit width based upon the revision
349 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
350 * For Revision 2 and above, Integers are 64 bits. Yes, this
351 * makes a difference.
353 ******************************************************************************/
355 void acpi_ut_set_integer_width(u8 revision
)
359 acpi_gbl_integer_bit_width
= 32;
360 acpi_gbl_integer_nybble_width
= 8;
361 acpi_gbl_integer_byte_width
= 4;
363 acpi_gbl_integer_bit_width
= 64;
364 acpi_gbl_integer_nybble_width
= 16;
365 acpi_gbl_integer_byte_width
= 8;
369 #ifdef ACPI_DEBUG_OUTPUT
370 /*******************************************************************************
372 * FUNCTION: acpi_ut_display_init_pathname
374 * PARAMETERS: Type - Object type of the node
375 * obj_handle - Handle whose pathname will be displayed
376 * Path - Additional path string to be appended.
377 * (NULL if no extra path)
379 * RETURN: acpi_status
381 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
383 ******************************************************************************/
386 acpi_ut_display_init_pathname(u8 type
,
387 struct acpi_namespace_node
*obj_handle
,
391 struct acpi_buffer buffer
;
393 ACPI_FUNCTION_ENTRY();
395 /* Only print the path if the appropriate debug level is enabled */
397 if (!(acpi_dbg_level
& ACPI_LV_INIT_NAMES
)) {
401 /* Get the full pathname to the node */
403 buffer
.length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
404 status
= acpi_ns_handle_to_pathname(obj_handle
, &buffer
);
405 if (ACPI_FAILURE(status
)) {
409 /* Print what we're doing */
412 case ACPI_TYPE_METHOD
:
413 acpi_os_printf("Executing ");
417 acpi_os_printf("Initializing ");
421 /* Print the object type and pathname */
423 acpi_os_printf("%-12s %s",
424 acpi_ut_get_type_name(type
), (char *)buffer
.pointer
);
426 /* Extra path is used to append names like _STA, _INI, etc. */
429 acpi_os_printf(".%s", path
);
431 acpi_os_printf("\n");
433 ACPI_MEM_FREE(buffer
.pointer
);
437 /*******************************************************************************
439 * FUNCTION: acpi_ut_valid_acpi_name
441 * PARAMETERS: Name - The name to be examined
443 * RETURN: TRUE if the name is valid, FALSE otherwise
445 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
446 * 1) Upper case alpha
450 ******************************************************************************/
452 u8
acpi_ut_valid_acpi_name(u32 name
)
454 char *name_ptr
= (char *)&name
;
458 ACPI_FUNCTION_ENTRY();
460 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++) {
461 character
= *name_ptr
;
464 if (!((character
== '_') ||
465 (character
>= 'A' && character
<= 'Z') ||
466 (character
>= '0' && character
<= '9'))) {
474 /*******************************************************************************
476 * FUNCTION: acpi_ut_valid_acpi_character
478 * PARAMETERS: Character - The character to be examined
480 * RETURN: 1 if Character may appear in a name, else 0
482 * DESCRIPTION: Check for a printable character
484 ******************************************************************************/
486 u8
acpi_ut_valid_acpi_character(char character
)
489 ACPI_FUNCTION_ENTRY();
491 return ((u8
) ((character
== '_') ||
492 (character
>= 'A' && character
<= 'Z') ||
493 (character
>= '0' && character
<= '9')));
496 /*******************************************************************************
498 * FUNCTION: acpi_ut_strtoul64
500 * PARAMETERS: String - Null terminated string
501 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
502 * ret_integer - Where the converted integer is returned
504 * RETURN: Status and Converted value
506 * DESCRIPTION: Convert a string into an unsigned value.
507 * NOTE: Does not support Octal strings, not needed.
509 ******************************************************************************/
512 acpi_ut_strtoul64(char *string
, u32 base
, acpi_integer
* ret_integer
)
515 acpi_integer return_value
= 0;
516 acpi_integer quotient
;
518 ACPI_FUNCTION_TRACE("ut_stroul64");
520 if ((!string
) || !(*string
)) {
532 return_ACPI_STATUS(AE_BAD_PARAMETER
);
535 /* Skip over any white space in the buffer */
537 while (ACPI_IS_SPACE(*string
) || *string
== '\t') {
542 * If the input parameter Base is zero, then we need to
543 * determine if it is decimal or hexadecimal:
546 if ((*string
== '0') && (ACPI_TOLOWER(*(string
+ 1)) == 'x')) {
555 * For hexadecimal base, skip over the leading
556 * 0 or 0x, if they are present.
559 (*string
== '0') && (ACPI_TOLOWER(*(string
+ 1)) == 'x')) {
563 /* Any string left? */
569 /* Main loop: convert the string to a 64-bit integer */
572 if (ACPI_IS_DIGIT(*string
)) {
573 /* Convert ASCII 0-9 to Decimal value */
575 this_digit
= ((u8
) * string
) - '0';
578 /* Digit is out of range */
583 this_digit
= (u8
) ACPI_TOUPPER(*string
);
584 if (ACPI_IS_XDIGIT((char)this_digit
)) {
585 /* Convert ASCII Hex char to value */
587 this_digit
= this_digit
- 'A' + 10;
590 * We allow non-hex chars, just stop now, same as end-of-string.
591 * See ACPI spec, string-to-integer conversion.
597 /* Divide the digit into the correct position */
600 acpi_ut_short_divide((ACPI_INTEGER_MAX
-
601 (acpi_integer
) this_digit
), base
,
603 if (return_value
> quotient
) {
607 return_value
*= base
;
608 return_value
+= this_digit
;
612 /* All done, normal exit */
614 *ret_integer
= return_value
;
615 return_ACPI_STATUS(AE_OK
);
618 /* Base was set/validated above */
621 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT
);
623 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT
);
627 /*******************************************************************************
629 * FUNCTION: acpi_ut_create_update_state_and_push
631 * PARAMETERS: Object - Object to be added to the new state
632 * Action - Increment/Decrement
633 * state_list - List the state will be added to
637 * DESCRIPTION: Create a new state and push it
639 ******************************************************************************/
642 acpi_ut_create_update_state_and_push(union acpi_operand_object
*object
,
644 union acpi_generic_state
**state_list
)
646 union acpi_generic_state
*state
;
648 ACPI_FUNCTION_ENTRY();
650 /* Ignore null objects; these are expected */
656 state
= acpi_ut_create_update_state(object
, action
);
658 return (AE_NO_MEMORY
);
661 acpi_ut_push_generic_state(state_list
, state
);
665 /*******************************************************************************
667 * FUNCTION: acpi_ut_walk_package_tree
669 * PARAMETERS: source_object - The package to walk
670 * target_object - Target object (if package is being copied)
671 * walk_callback - Called once for each package element
672 * Context - Passed to the callback function
676 * DESCRIPTION: Walk through a package
678 ******************************************************************************/
681 acpi_ut_walk_package_tree(union acpi_operand_object
* source_object
,
683 acpi_pkg_callback walk_callback
, void *context
)
685 acpi_status status
= AE_OK
;
686 union acpi_generic_state
*state_list
= NULL
;
687 union acpi_generic_state
*state
;
689 union acpi_operand_object
*this_source_obj
;
691 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
693 state
= acpi_ut_create_pkg_state(source_object
, target_object
, 0);
695 return_ACPI_STATUS(AE_NO_MEMORY
);
699 /* Get one element of the package */
701 this_index
= state
->pkg
.index
;
702 this_source_obj
= (union acpi_operand_object
*)
703 state
->pkg
.source_object
->package
.elements
[this_index
];
707 * 1) An uninitialized package element. It is completely
708 * legal to declare a package and leave it uninitialized
709 * 2) Not an internal object - can be a namespace node instead
710 * 3) Any type other than a package. Packages are handled in else
713 if ((!this_source_obj
) ||
714 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj
) !=
715 ACPI_DESC_TYPE_OPERAND
)
716 || (ACPI_GET_OBJECT_TYPE(this_source_obj
) !=
717 ACPI_TYPE_PACKAGE
)) {
719 walk_callback(ACPI_COPY_TYPE_SIMPLE
,
720 this_source_obj
, state
, context
);
721 if (ACPI_FAILURE(status
)) {
722 return_ACPI_STATUS(status
);
726 while (state
->pkg
.index
>=
727 state
->pkg
.source_object
->package
.count
) {
729 * We've handled all of the objects at this level, This means
730 * that we have just completed a package. That package may
731 * have contained one or more packages itself.
733 * Delete this state and pop the previous state (package).
735 acpi_ut_delete_generic_state(state
);
736 state
= acpi_ut_pop_generic_state(&state_list
);
738 /* Finished when there are no more states */
742 * We have handled all of the objects in the top level
743 * package just add the length of the package objects
746 return_ACPI_STATUS(AE_OK
);
750 * Go back up a level and move the index past the just
751 * completed package object.
756 /* This is a subobject of type package */
759 walk_callback(ACPI_COPY_TYPE_PACKAGE
,
760 this_source_obj
, state
, context
);
761 if (ACPI_FAILURE(status
)) {
762 return_ACPI_STATUS(status
);
766 * Push the current state and create a new one
767 * The callback above returned a new target package object.
769 acpi_ut_push_generic_state(&state_list
, state
);
770 state
= acpi_ut_create_pkg_state(this_source_obj
,
774 return_ACPI_STATUS(AE_NO_MEMORY
);
779 /* We should never get here */
781 return_ACPI_STATUS(AE_AML_INTERNAL
);
784 /*******************************************************************************
786 * FUNCTION: acpi_ut_generate_checksum
788 * PARAMETERS: Buffer - Buffer to be scanned
789 * Length - number of bytes to examine
791 * RETURN: The generated checksum
793 * DESCRIPTION: Generate a checksum on a raw buffer
795 ******************************************************************************/
797 u8
acpi_ut_generate_checksum(u8
* buffer
, u32 length
)
802 for (i
= 0; i
< length
; i
++) {
803 sum
= (signed char)(sum
+ buffer
[i
]);
806 return ((u8
) (0 - sum
));
809 /*******************************************************************************
811 * FUNCTION: acpi_ut_report_error
813 * PARAMETERS: module_name - Caller's module name (for error output)
814 * line_number - Caller's line number (for error output)
815 * component_id - Caller's component ID (for error output)
819 * DESCRIPTION: Print error message
821 ******************************************************************************/
823 void acpi_ut_report_error(char *module_name
, u32 line_number
, u32 component_id
)
826 acpi_os_printf("%8s-%04d: *** Error: ", module_name
, line_number
);
829 /*******************************************************************************
831 * FUNCTION: acpi_ut_report_warning
833 * PARAMETERS: module_name - Caller's module name (for error output)
834 * line_number - Caller's line number (for error output)
835 * component_id - Caller's component ID (for error output)
839 * DESCRIPTION: Print warning message
841 ******************************************************************************/
844 acpi_ut_report_warning(char *module_name
, u32 line_number
, u32 component_id
)
847 acpi_os_printf("%8s-%04d: *** Warning: ", module_name
, line_number
);
850 /*******************************************************************************
852 * FUNCTION: acpi_ut_report_info
854 * PARAMETERS: module_name - Caller's module name (for error output)
855 * line_number - Caller's line number (for error output)
856 * component_id - Caller's component ID (for error output)
860 * DESCRIPTION: Print information message
862 ******************************************************************************/
864 void acpi_ut_report_info(char *module_name
, u32 line_number
, u32 component_id
)
867 acpi_os_printf("%8s-%04d: *** Info: ", module_name
, line_number
);