1 /*******************************************************************************
3 * Module Name: utmisc - common utility procedures
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2006, 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)
70 ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
72 /* Guard against multiple allocations of ID to the same location */
75 ACPI_ERROR((AE_INFO, "Owner ID [%2.2X] already exists",
77 return_ACPI_STATUS(AE_ALREADY_EXISTS);
80 /* Mutex for the global ID mask */
82 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
83 if (ACPI_FAILURE(status)) {
84 return_ACPI_STATUS(status);
88 * Find a free owner ID, cycle through all possible IDs on repeated
89 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
90 * to be scanned twice.
92 for (i = 0, j = acpi_gbl_last_owner_id_index;
93 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
94 if (j >= ACPI_NUM_OWNERID_MASKS) {
95 j = 0; /* Wraparound to start of mask array */
98 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
99 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
101 /* There are no free IDs in this mask */
106 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
108 * Found a free ID. The actual ID is the bit index plus one,
109 * making zero an invalid Owner ID. Save this as the last ID
110 * allocated and update the global ID mask.
112 acpi_gbl_owner_id_mask[j] |= (1 << k);
114 acpi_gbl_last_owner_id_index = (u8) j;
115 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
118 * Construct encoded ID from the index and bit position
120 * Note: Last [j].k (bit 255) is never used and is marked
121 * permanently allocated (prevents +1 overflow)
124 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
126 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
127 "Allocated owner_id: %2.2X\n",
128 (unsigned int)*owner_id));
133 acpi_gbl_next_owner_id_offset = 0;
137 * All owner_ids have been allocated. This typically should
138 * not happen since the IDs are reused after deallocation. The IDs are
139 * allocated upon table load (one per table) and method execution, and
140 * they are released when a table is unloaded or a method completes
143 * If this error happens, there may be very deep nesting of invoked control
144 * methods, or there may be a bug where the IDs are not released.
146 status = AE_OWNER_ID_LIMIT;
148 "Could not allocate new owner_id (255 max), AE_OWNER_ID_LIMIT"));
151 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
152 return_ACPI_STATUS(status);
155 /*******************************************************************************
157 * FUNCTION: acpi_ut_release_owner_id
159 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
161 * RETURN: None. No error is returned because we are either exiting a
162 * control method or unloading a table. Either way, we would
163 * ignore any error anyway.
165 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
167 ******************************************************************************/
169 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
171 acpi_owner_id owner_id = *owner_id_ptr;
173 acpi_native_uint index;
176 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
178 /* Always clear the input owner_id (zero is an invalid ID) */
182 /* Zero is not a valid owner_iD */
185 ACPI_ERROR((AE_INFO, "Invalid owner_id: %2.2X", owner_id));
189 /* Mutex for the global ID mask */
191 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
192 if (ACPI_FAILURE(status)) {
196 /* Normalize the ID to zero */
200 /* Decode ID to index/offset pair */
202 index = ACPI_DIV_32(owner_id);
203 bit = 1 << ACPI_MOD_32(owner_id);
205 /* Free the owner ID only if it is valid */
207 if (acpi_gbl_owner_id_mask[index] & bit) {
208 acpi_gbl_owner_id_mask[index] ^= bit;
211 "Release of non-allocated owner_id: %2.2X",
215 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
219 /*******************************************************************************
221 * FUNCTION: acpi_ut_strupr (strupr)
223 * PARAMETERS: src_string - The source string to convert
227 * DESCRIPTION: Convert string to uppercase
229 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
231 ******************************************************************************/
233 void acpi_ut_strupr(char *src_string)
237 ACPI_FUNCTION_ENTRY();
243 /* Walk entire string, uppercasing the letters */
245 for (string = src_string; *string; string++) {
246 *string = (char)ACPI_TOUPPER(*string);
252 /*******************************************************************************
254 * FUNCTION: acpi_ut_print_string
256 * PARAMETERS: String - Null terminated ASCII string
257 * max_length - Maximum output length
261 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
264 ******************************************************************************/
266 void acpi_ut_print_string(char *string, u8 max_length)
271 acpi_os_printf("<\"NULL STRING PTR\">");
275 acpi_os_printf("\"");
276 for (i = 0; string[i] && (i < max_length); i++) {
278 /* Escape sequences */
282 acpi_os_printf("\\a"); /* BELL */
286 acpi_os_printf("\\b"); /* BACKSPACE */
290 acpi_os_printf("\\f"); /* FORMFEED */
294 acpi_os_printf("\\n"); /* LINEFEED */
298 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
302 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
306 acpi_os_printf("\\v"); /* VERTICAL TAB */
309 case '\'': /* Single Quote */
310 case '\"': /* Double Quote */
311 case '\\': /* Backslash */
312 acpi_os_printf("\\%c", (int)string[i]);
317 /* Check for printable character or hex escape */
319 if (ACPI_IS_PRINT(string[i])) {
320 /* This is a normal character */
322 acpi_os_printf("%c", (int)string[i]);
324 /* All others will be Hex escapes */
326 acpi_os_printf("\\x%2.2X", (s32) string[i]);
331 acpi_os_printf("\"");
333 if (i == max_length && string[i]) {
334 acpi_os_printf("...");
338 /*******************************************************************************
340 * FUNCTION: acpi_ut_dword_byte_swap
342 * PARAMETERS: Value - Value to be converted
344 * RETURN: u32 integer with bytes swapped
346 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
348 ******************************************************************************/
350 u32 acpi_ut_dword_byte_swap(u32 value)
361 ACPI_FUNCTION_ENTRY();
365 out.bytes[0] = in.bytes[3];
366 out.bytes[1] = in.bytes[2];
367 out.bytes[2] = in.bytes[1];
368 out.bytes[3] = in.bytes[0];
373 /*******************************************************************************
375 * FUNCTION: acpi_ut_set_integer_width
377 * PARAMETERS: Revision From DSDT header
381 * DESCRIPTION: Set the global integer bit width based upon the revision
382 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
383 * For Revision 2 and above, Integers are 64 bits. Yes, this
384 * makes a difference.
386 ******************************************************************************/
388 void acpi_ut_set_integer_width(u8 revision)
392 acpi_gbl_integer_bit_width = 32;
393 acpi_gbl_integer_nybble_width = 8;
394 acpi_gbl_integer_byte_width = 4;
396 acpi_gbl_integer_bit_width = 64;
397 acpi_gbl_integer_nybble_width = 16;
398 acpi_gbl_integer_byte_width = 8;
402 #ifdef ACPI_DEBUG_OUTPUT
403 /*******************************************************************************
405 * FUNCTION: acpi_ut_display_init_pathname
407 * PARAMETERS: Type - Object type of the node
408 * obj_handle - Handle whose pathname will be displayed
409 * Path - Additional path string to be appended.
410 * (NULL if no extra path)
412 * RETURN: acpi_status
414 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
416 ******************************************************************************/
419 acpi_ut_display_init_pathname(u8 type,
420 struct acpi_namespace_node *obj_handle,
424 struct acpi_buffer buffer;
426 ACPI_FUNCTION_ENTRY();
428 /* Only print the path if the appropriate debug level is enabled */
430 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
434 /* Get the full pathname to the node */
436 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
437 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
438 if (ACPI_FAILURE(status)) {
442 /* Print what we're doing */
445 case ACPI_TYPE_METHOD:
446 acpi_os_printf("Executing ");
450 acpi_os_printf("Initializing ");
454 /* Print the object type and pathname */
456 acpi_os_printf("%-12s %s",
457 acpi_ut_get_type_name(type), (char *)buffer.pointer);
459 /* Extra path is used to append names like _STA, _INI, etc. */
462 acpi_os_printf(".%s", path);
464 acpi_os_printf("\n");
466 ACPI_FREE(buffer.pointer);
470 /*******************************************************************************
472 * FUNCTION: acpi_ut_valid_acpi_name
474 * PARAMETERS: Name - The name to be examined
476 * RETURN: TRUE if the name is valid, FALSE otherwise
478 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
479 * 1) Upper case alpha
483 ******************************************************************************/
485 u8 acpi_ut_valid_acpi_name(u32 name)
487 char *name_ptr = (char *)&name;
491 ACPI_FUNCTION_ENTRY();
493 for (i = 0; i < ACPI_NAME_SIZE; i++) {
494 character = *name_ptr;
497 if (!((character == '_') ||
498 (character >= 'A' && character <= 'Z') ||
499 (character >= '0' && character <= '9'))) {
507 /*******************************************************************************
509 * FUNCTION: acpi_ut_valid_acpi_character
511 * PARAMETERS: Character - The character to be examined
513 * RETURN: 1 if Character may appear in a name, else 0
515 * DESCRIPTION: Check for a printable character
517 ******************************************************************************/
519 u8 acpi_ut_valid_acpi_character(char character)
522 ACPI_FUNCTION_ENTRY();
524 return ((u8) ((character == '_') ||
525 (character >= 'A' && character <= 'Z') ||
526 (character >= '0' && character <= '9')));
529 /*******************************************************************************
531 * FUNCTION: acpi_ut_strtoul64
533 * PARAMETERS: String - Null terminated string
534 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
535 * ret_integer - Where the converted integer is returned
537 * RETURN: Status and Converted value
539 * DESCRIPTION: Convert a string into an unsigned value.
540 * NOTE: Does not support Octal strings, not needed.
542 ******************************************************************************/
545 acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
548 acpi_integer return_value = 0;
549 acpi_integer quotient;
551 ACPI_FUNCTION_TRACE("ut_stroul64");
553 if ((!string) || !(*string)) {
565 return_ACPI_STATUS(AE_BAD_PARAMETER);
568 /* Skip over any white space in the buffer */
570 while (ACPI_IS_SPACE(*string) || *string == '\t') {
575 * If the input parameter Base is zero, then we need to
576 * determine if it is decimal or hexadecimal:
579 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
588 * For hexadecimal base, skip over the leading
589 * 0 or 0x, if they are present.
592 (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
596 /* Any string left? */
602 /* Main loop: convert the string to a 64-bit integer */
605 if (ACPI_IS_DIGIT(*string)) {
607 /* Convert ASCII 0-9 to Decimal value */
609 this_digit = ((u8) * string) - '0';
613 /* Digit is out of range */
618 this_digit = (u8) ACPI_TOUPPER(*string);
619 if (ACPI_IS_XDIGIT((char)this_digit)) {
621 /* Convert ASCII Hex char to value */
623 this_digit = this_digit - 'A' + 10;
626 * We allow non-hex chars, just stop now, same as end-of-string.
627 * See ACPI spec, string-to-integer conversion.
633 /* Divide the digit into the correct position */
636 acpi_ut_short_divide((ACPI_INTEGER_MAX -
637 (acpi_integer) this_digit), base,
639 if (return_value > quotient) {
643 return_value *= base;
644 return_value += this_digit;
648 /* All done, normal exit */
650 *ret_integer = return_value;
651 return_ACPI_STATUS(AE_OK);
654 /* Base was set/validated above */
657 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
659 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
663 /*******************************************************************************
665 * FUNCTION: acpi_ut_create_update_state_and_push
667 * PARAMETERS: Object - Object to be added to the new state
668 * Action - Increment/Decrement
669 * state_list - List the state will be added to
673 * DESCRIPTION: Create a new state and push it
675 ******************************************************************************/
678 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
680 union acpi_generic_state **state_list)
682 union acpi_generic_state *state;
684 ACPI_FUNCTION_ENTRY();
686 /* Ignore null objects; these are expected */
692 state = acpi_ut_create_update_state(object, action);
694 return (AE_NO_MEMORY);
697 acpi_ut_push_generic_state(state_list, state);
701 /*******************************************************************************
703 * FUNCTION: acpi_ut_walk_package_tree
705 * PARAMETERS: source_object - The package to walk
706 * target_object - Target object (if package is being copied)
707 * walk_callback - Called once for each package element
708 * Context - Passed to the callback function
712 * DESCRIPTION: Walk through a package
714 ******************************************************************************/
717 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
719 acpi_pkg_callback walk_callback, void *context)
721 acpi_status status = AE_OK;
722 union acpi_generic_state *state_list = NULL;
723 union acpi_generic_state *state;
725 union acpi_operand_object *this_source_obj;
727 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
729 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
731 return_ACPI_STATUS(AE_NO_MEMORY);
736 /* Get one element of the package */
738 this_index = state->pkg.index;
739 this_source_obj = (union acpi_operand_object *)
740 state->pkg.source_object->package.elements[this_index];
744 * 1) An uninitialized package element. It is completely
745 * legal to declare a package and leave it uninitialized
746 * 2) Not an internal object - can be a namespace node instead
747 * 3) Any type other than a package. Packages are handled in else
750 if ((!this_source_obj) ||
751 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
752 ACPI_DESC_TYPE_OPERAND)
753 || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
754 ACPI_TYPE_PACKAGE)) {
756 walk_callback(ACPI_COPY_TYPE_SIMPLE,
757 this_source_obj, state, context);
758 if (ACPI_FAILURE(status)) {
759 return_ACPI_STATUS(status);
763 while (state->pkg.index >=
764 state->pkg.source_object->package.count) {
766 * We've handled all of the objects at this level, This means
767 * that we have just completed a package. That package may
768 * have contained one or more packages itself.
770 * Delete this state and pop the previous state (package).
772 acpi_ut_delete_generic_state(state);
773 state = acpi_ut_pop_generic_state(&state_list);
775 /* Finished when there are no more states */
779 * We have handled all of the objects in the top level
780 * package just add the length of the package objects
783 return_ACPI_STATUS(AE_OK);
787 * Go back up a level and move the index past the just
788 * completed package object.
793 /* This is a subobject of type package */
796 walk_callback(ACPI_COPY_TYPE_PACKAGE,
797 this_source_obj, state, context);
798 if (ACPI_FAILURE(status)) {
799 return_ACPI_STATUS(status);
803 * Push the current state and create a new one
804 * The callback above returned a new target package object.
806 acpi_ut_push_generic_state(&state_list, state);
807 state = acpi_ut_create_pkg_state(this_source_obj,
811 return_ACPI_STATUS(AE_NO_MEMORY);
816 /* We should never get here */
818 return_ACPI_STATUS(AE_AML_INTERNAL);
821 /*******************************************************************************
823 * FUNCTION: acpi_ut_error, acpi_ut_warning, acpi_ut_info
825 * PARAMETERS: module_name - Caller's module name (for error output)
826 * line_number - Caller's line number (for error output)
827 * Format - Printf format string + additional args
831 * DESCRIPTION: Print message with module/line/version info
833 ******************************************************************************/
835 void ACPI_INTERNAL_VAR_XFACE
836 acpi_ut_error(char *module_name, u32 line_number, char *format, ...)
840 acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
842 va_start(args, format);
843 acpi_os_vprintf(format, args);
844 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
847 void ACPI_INTERNAL_VAR_XFACE
848 acpi_ut_exception(char *module_name,
849 u32 line_number, acpi_status status, char *format, ...)
853 acpi_os_printf("ACPI Exception (%s-%04d): %s, ", module_name,
854 line_number, acpi_format_exception(status));
856 va_start(args, format);
857 acpi_os_vprintf(format, args);
858 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
861 void ACPI_INTERNAL_VAR_XFACE
862 acpi_ut_warning(char *module_name, u32 line_number, char *format, ...)
866 acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
868 va_start(args, format);
869 acpi_os_vprintf(format, args);
870 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
873 void ACPI_INTERNAL_VAR_XFACE
874 acpi_ut_info(char *module_name, u32 line_number, char *format, ...)
878 acpi_os_printf("ACPI (%s-%04d): ", module_name, line_number);
880 va_start(args, format);
881 acpi_os_vprintf(format, args);
882 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);