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 /* Mutex for the global ID mask */
72 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
73 if (ACPI_FAILURE(status)) {
74 return_ACPI_STATUS(status);
77 /* Find a free owner ID */
79 for (i = 0; i < 32; i++) {
80 if (!(acpi_gbl_owner_id_mask & (1 << i))) {
81 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
82 "Current owner_id mask: %8.8X New ID: %2.2X\n",
83 acpi_gbl_owner_id_mask, (i + 1)));
85 acpi_gbl_owner_id_mask |= (1 << i);
86 *owner_id = (acpi_owner_id) (i + 1);
92 * If we are here, all owner_ids have been allocated. This probably should
93 * not happen since the IDs are reused after deallocation. The IDs are
94 * allocated upon table load (one per table) and method execution, and
95 * they are released when a table is unloaded or a method completes
99 status = AE_OWNER_ID_LIMIT;
100 ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
103 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
104 return_ACPI_STATUS(status);
107 /*******************************************************************************
109 * FUNCTION: acpi_ut_release_owner_id
111 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
113 * RETURN: None. No error is returned because we are either exiting a
114 * control method or unloading a table. Either way, we would
115 * ignore any error anyway.
117 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 32
119 ******************************************************************************/
121 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
123 acpi_owner_id owner_id = *owner_id_ptr;
126 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
128 /* Always clear the input owner_id (zero is an invalid ID) */
132 /* Zero is not a valid owner_iD */
134 if ((owner_id == 0) || (owner_id > 32)) {
135 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id));
139 /* Mutex for the global ID mask */
141 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
142 if (ACPI_FAILURE(status)) {
146 owner_id--; /* Normalize to zero */
148 /* Free the owner ID only if it is valid */
150 if (acpi_gbl_owner_id_mask & (1 << owner_id)) {
151 acpi_gbl_owner_id_mask ^= (1 << owner_id);
154 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
158 /*******************************************************************************
160 * FUNCTION: acpi_ut_strupr (strupr)
162 * PARAMETERS: src_string - The source string to convert
166 * DESCRIPTION: Convert string to uppercase
168 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
170 ******************************************************************************/
172 void acpi_ut_strupr(char *src_string)
176 ACPI_FUNCTION_ENTRY();
182 /* Walk entire string, uppercasing the letters */
184 for (string = src_string; *string; string++) {
185 *string = (char)ACPI_TOUPPER(*string);
191 /*******************************************************************************
193 * FUNCTION: acpi_ut_print_string
195 * PARAMETERS: String - Null terminated ASCII string
196 * max_length - Maximum output length
200 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
203 ******************************************************************************/
205 void acpi_ut_print_string(char *string, u8 max_length)
210 acpi_os_printf("<\"NULL STRING PTR\">");
214 acpi_os_printf("\"");
215 for (i = 0; string[i] && (i < max_length); i++) {
216 /* Escape sequences */
220 acpi_os_printf("\\a"); /* BELL */
224 acpi_os_printf("\\b"); /* BACKSPACE */
228 acpi_os_printf("\\f"); /* FORMFEED */
232 acpi_os_printf("\\n"); /* LINEFEED */
236 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
240 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
244 acpi_os_printf("\\v"); /* VERTICAL TAB */
247 case '\'': /* Single Quote */
248 case '\"': /* Double Quote */
249 case '\\': /* Backslash */
250 acpi_os_printf("\\%c", (int)string[i]);
255 /* Check for printable character or hex escape */
257 if (ACPI_IS_PRINT(string[i])) {
258 /* This is a normal character */
260 acpi_os_printf("%c", (int)string[i]);
262 /* All others will be Hex escapes */
264 acpi_os_printf("\\x%2.2X", (s32) string[i]);
269 acpi_os_printf("\"");
271 if (i == max_length && string[i]) {
272 acpi_os_printf("...");
276 /*******************************************************************************
278 * FUNCTION: acpi_ut_dword_byte_swap
280 * PARAMETERS: Value - Value to be converted
282 * RETURN: u32 integer with bytes swapped
284 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
286 ******************************************************************************/
288 u32 acpi_ut_dword_byte_swap(u32 value)
299 ACPI_FUNCTION_ENTRY();
303 out.bytes[0] = in.bytes[3];
304 out.bytes[1] = in.bytes[2];
305 out.bytes[2] = in.bytes[1];
306 out.bytes[3] = in.bytes[0];
311 /*******************************************************************************
313 * FUNCTION: acpi_ut_set_integer_width
315 * PARAMETERS: Revision From DSDT header
319 * DESCRIPTION: Set the global integer bit width based upon the revision
320 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
321 * For Revision 2 and above, Integers are 64 bits. Yes, this
322 * makes a difference.
324 ******************************************************************************/
326 void acpi_ut_set_integer_width(u8 revision)
330 acpi_gbl_integer_bit_width = 32;
331 acpi_gbl_integer_nybble_width = 8;
332 acpi_gbl_integer_byte_width = 4;
334 acpi_gbl_integer_bit_width = 64;
335 acpi_gbl_integer_nybble_width = 16;
336 acpi_gbl_integer_byte_width = 8;
340 #ifdef ACPI_DEBUG_OUTPUT
341 /*******************************************************************************
343 * FUNCTION: acpi_ut_display_init_pathname
345 * PARAMETERS: Type - Object type of the node
346 * obj_handle - Handle whose pathname will be displayed
347 * Path - Additional path string to be appended.
348 * (NULL if no extra path)
350 * RETURN: acpi_status
352 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
354 ******************************************************************************/
357 acpi_ut_display_init_pathname(u8 type,
358 struct acpi_namespace_node *obj_handle,
362 struct acpi_buffer buffer;
364 ACPI_FUNCTION_ENTRY();
366 /* Only print the path if the appropriate debug level is enabled */
368 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
372 /* Get the full pathname to the node */
374 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
375 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
376 if (ACPI_FAILURE(status)) {
380 /* Print what we're doing */
383 case ACPI_TYPE_METHOD:
384 acpi_os_printf("Executing ");
388 acpi_os_printf("Initializing ");
392 /* Print the object type and pathname */
394 acpi_os_printf("%-12s %s",
395 acpi_ut_get_type_name(type), (char *)buffer.pointer);
397 /* Extra path is used to append names like _STA, _INI, etc. */
400 acpi_os_printf(".%s", path);
402 acpi_os_printf("\n");
404 ACPI_MEM_FREE(buffer.pointer);
408 /*******************************************************************************
410 * FUNCTION: acpi_ut_valid_acpi_name
412 * PARAMETERS: Name - The name to be examined
414 * RETURN: TRUE if the name is valid, FALSE otherwise
416 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
417 * 1) Upper case alpha
421 ******************************************************************************/
423 u8 acpi_ut_valid_acpi_name(u32 name)
425 char *name_ptr = (char *)&name;
429 ACPI_FUNCTION_ENTRY();
431 for (i = 0; i < ACPI_NAME_SIZE; i++) {
432 character = *name_ptr;
435 if (!((character == '_') ||
436 (character >= 'A' && character <= 'Z') ||
437 (character >= '0' && character <= '9'))) {
445 /*******************************************************************************
447 * FUNCTION: acpi_ut_valid_acpi_character
449 * PARAMETERS: Character - The character to be examined
451 * RETURN: 1 if Character may appear in a name, else 0
453 * DESCRIPTION: Check for a printable character
455 ******************************************************************************/
457 u8 acpi_ut_valid_acpi_character(char character)
460 ACPI_FUNCTION_ENTRY();
462 return ((u8) ((character == '_') ||
463 (character >= 'A' && character <= 'Z') ||
464 (character >= '0' && character <= '9')));
467 /*******************************************************************************
469 * FUNCTION: acpi_ut_strtoul64
471 * PARAMETERS: String - Null terminated string
472 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
473 * ret_integer - Where the converted integer is returned
475 * RETURN: Status and Converted value
477 * DESCRIPTION: Convert a string into an unsigned value.
478 * NOTE: Does not support Octal strings, not needed.
480 ******************************************************************************/
483 acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
486 acpi_integer return_value = 0;
487 acpi_integer quotient;
489 ACPI_FUNCTION_TRACE("ut_stroul64");
491 if ((!string) || !(*string)) {
503 return_ACPI_STATUS(AE_BAD_PARAMETER);
506 /* Skip over any white space in the buffer */
508 while (ACPI_IS_SPACE(*string) || *string == '\t') {
513 * If the input parameter Base is zero, then we need to
514 * determine if it is decimal or hexadecimal:
517 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
526 * For hexadecimal base, skip over the leading
527 * 0 or 0x, if they are present.
530 (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
534 /* Any string left? */
540 /* Main loop: convert the string to a 64-bit integer */
543 if (ACPI_IS_DIGIT(*string)) {
544 /* Convert ASCII 0-9 to Decimal value */
546 this_digit = ((u8) * string) - '0';
549 /* Digit is out of range */
554 this_digit = (u8) ACPI_TOUPPER(*string);
555 if (ACPI_IS_XDIGIT((char)this_digit)) {
556 /* Convert ASCII Hex char to value */
558 this_digit = this_digit - 'A' + 10;
561 * We allow non-hex chars, just stop now, same as end-of-string.
562 * See ACPI spec, string-to-integer conversion.
568 /* Divide the digit into the correct position */
571 acpi_ut_short_divide((ACPI_INTEGER_MAX -
572 (acpi_integer) this_digit), base,
574 if (return_value > quotient) {
578 return_value *= base;
579 return_value += this_digit;
583 /* All done, normal exit */
585 *ret_integer = return_value;
586 return_ACPI_STATUS(AE_OK);
589 /* Base was set/validated above */
592 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
594 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
598 /*******************************************************************************
600 * FUNCTION: acpi_ut_create_update_state_and_push
602 * PARAMETERS: Object - Object to be added to the new state
603 * Action - Increment/Decrement
604 * state_list - List the state will be added to
608 * DESCRIPTION: Create a new state and push it
610 ******************************************************************************/
613 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
615 union acpi_generic_state **state_list)
617 union acpi_generic_state *state;
619 ACPI_FUNCTION_ENTRY();
621 /* Ignore null objects; these are expected */
627 state = acpi_ut_create_update_state(object, action);
629 return (AE_NO_MEMORY);
632 acpi_ut_push_generic_state(state_list, state);
636 /*******************************************************************************
638 * FUNCTION: acpi_ut_walk_package_tree
640 * PARAMETERS: source_object - The package to walk
641 * target_object - Target object (if package is being copied)
642 * walk_callback - Called once for each package element
643 * Context - Passed to the callback function
647 * DESCRIPTION: Walk through a package
649 ******************************************************************************/
652 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
654 acpi_pkg_callback walk_callback, void *context)
656 acpi_status status = AE_OK;
657 union acpi_generic_state *state_list = NULL;
658 union acpi_generic_state *state;
660 union acpi_operand_object *this_source_obj;
662 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
664 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
666 return_ACPI_STATUS(AE_NO_MEMORY);
670 /* Get one element of the package */
672 this_index = state->pkg.index;
673 this_source_obj = (union acpi_operand_object *)
674 state->pkg.source_object->package.elements[this_index];
678 * 1) An uninitialized package element. It is completely
679 * legal to declare a package and leave it uninitialized
680 * 2) Not an internal object - can be a namespace node instead
681 * 3) Any type other than a package. Packages are handled in else
684 if ((!this_source_obj) ||
685 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
686 ACPI_DESC_TYPE_OPERAND)
687 || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
688 ACPI_TYPE_PACKAGE)) {
690 walk_callback(ACPI_COPY_TYPE_SIMPLE,
691 this_source_obj, state, context);
692 if (ACPI_FAILURE(status)) {
693 return_ACPI_STATUS(status);
697 while (state->pkg.index >=
698 state->pkg.source_object->package.count) {
700 * We've handled all of the objects at this level, This means
701 * that we have just completed a package. That package may
702 * have contained one or more packages itself.
704 * Delete this state and pop the previous state (package).
706 acpi_ut_delete_generic_state(state);
707 state = acpi_ut_pop_generic_state(&state_list);
709 /* Finished when there are no more states */
713 * We have handled all of the objects in the top level
714 * package just add the length of the package objects
717 return_ACPI_STATUS(AE_OK);
721 * Go back up a level and move the index past the just
722 * completed package object.
727 /* This is a subobject of type package */
730 walk_callback(ACPI_COPY_TYPE_PACKAGE,
731 this_source_obj, state, context);
732 if (ACPI_FAILURE(status)) {
733 return_ACPI_STATUS(status);
737 * Push the current state and create a new one
738 * The callback above returned a new target package object.
740 acpi_ut_push_generic_state(&state_list, state);
741 state = acpi_ut_create_pkg_state(this_source_obj,
745 return_ACPI_STATUS(AE_NO_MEMORY);
750 /* We should never get here */
752 return_ACPI_STATUS(AE_AML_INTERNAL);
755 /*******************************************************************************
757 * FUNCTION: acpi_ut_generate_checksum
759 * PARAMETERS: Buffer - Buffer to be scanned
760 * Length - number of bytes to examine
762 * RETURN: The generated checksum
764 * DESCRIPTION: Generate a checksum on a raw buffer
766 ******************************************************************************/
768 u8 acpi_ut_generate_checksum(u8 * buffer, u32 length)
773 for (i = 0; i < length; i++) {
774 sum = (signed char)(sum + buffer[i]);
777 return ((u8) (0 - sum));
780 /*******************************************************************************
782 * FUNCTION: acpi_ut_get_resource_end_tag
784 * PARAMETERS: obj_desc - The resource template buffer object
786 * RETURN: Pointer to the end tag
788 * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
790 ******************************************************************************/
792 u8 *acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc)
798 buffer = obj_desc->buffer.pointer;
799 end_buffer = buffer + obj_desc->buffer.length;
801 while (buffer < end_buffer) {
802 buffer_byte = *buffer;
803 if (buffer_byte & ACPI_RDESC_TYPE_MASK) {
804 /* Large Descriptor - Length is next 2 bytes */
806 buffer += ((*(buffer + 1) | (*(buffer + 2) << 8)) + 3);
808 /* Small Descriptor. End Tag will be found here */
810 if ((buffer_byte & ACPI_RDESC_SMALL_MASK) ==
811 ACPI_RDESC_TYPE_END_TAG) {
812 /* Found the end tag descriptor, all done. */
817 /* Length is in the header */
819 buffer += ((buffer_byte & 0x07) + 1);
823 /* End tag not found */
828 /*******************************************************************************
830 * FUNCTION: acpi_ut_report_error
832 * PARAMETERS: module_name - Caller's module name (for error output)
833 * line_number - Caller's line number (for error output)
834 * component_id - Caller's component ID (for error output)
838 * DESCRIPTION: Print error message
840 ******************************************************************************/
842 void acpi_ut_report_error(char *module_name, u32 line_number, u32 component_id)
845 acpi_os_printf("%8s-%04d: *** Error: ", module_name, line_number);
848 /*******************************************************************************
850 * FUNCTION: acpi_ut_report_warning
852 * PARAMETERS: module_name - Caller's module name (for error output)
853 * line_number - Caller's line number (for error output)
854 * component_id - Caller's component ID (for error output)
858 * DESCRIPTION: Print warning message
860 ******************************************************************************/
863 acpi_ut_report_warning(char *module_name, u32 line_number, u32 component_id)
866 acpi_os_printf("%8s-%04d: *** Warning: ", module_name, line_number);
869 /*******************************************************************************
871 * FUNCTION: acpi_ut_report_info
873 * PARAMETERS: module_name - Caller's module name (for error output)
874 * line_number - Caller's line number (for error output)
875 * component_id - Caller's component ID (for error output)
879 * DESCRIPTION: Print information message
881 ******************************************************************************/
883 void acpi_ut_report_info(char *module_name, u32 line_number, u32 component_id)
886 acpi_os_printf("%8s-%04d: *** Info: ", module_name, line_number);