checkdirs() was being passed the wrong mount point, resulting in a panic
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050309 / utilities / utmisc.c
blobd79649635c8e17e113d99eb581ad4b547030337b
1 /*******************************************************************************
3 * Module Name: utmisc - common utility procedures
4 * $Revision: 108 $
6 ******************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #define __UTMISC_C__
120 #include "acpi.h"
121 #include "acnamesp.h"
124 #define _COMPONENT ACPI_UTILITIES
125 ACPI_MODULE_NAME ("utmisc")
128 /*******************************************************************************
130 * FUNCTION: AcpiUtPrintString
132 * PARAMETERS: String - Null terminated ASCII string
134 * RETURN: None
136 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
137 * sequences.
139 ******************************************************************************/
141 void
142 AcpiUtPrintString (
143 char *String,
144 UINT8 MaxLength)
146 UINT32 i;
149 if (!String)
151 AcpiOsPrintf ("<\"NULL STRING PTR\">");
152 return;
155 AcpiOsPrintf ("\"");
156 for (i = 0; String[i] && (i < MaxLength); i++)
158 /* Escape sequences */
160 switch (String[i])
162 case 0x07:
163 AcpiOsPrintf ("\\a"); /* BELL */
164 break;
166 case 0x08:
167 AcpiOsPrintf ("\\b"); /* BACKSPACE */
168 break;
170 case 0x0C:
171 AcpiOsPrintf ("\\f"); /* FORMFEED */
172 break;
174 case 0x0A:
175 AcpiOsPrintf ("\\n"); /* LINEFEED */
176 break;
178 case 0x0D:
179 AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
180 break;
182 case 0x09:
183 AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
184 break;
186 case 0x0B:
187 AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
188 break;
190 case '\'': /* Single Quote */
191 case '\"': /* Double Quote */
192 case '\\': /* Backslash */
193 AcpiOsPrintf ("\\%c", (int) String[i]);
194 break;
196 default:
198 /* Check for printable character or hex escape */
200 if (ACPI_IS_PRINT (String[i]))
202 /* This is a normal character */
204 AcpiOsPrintf ("%c", (int) String[i]);
206 else
208 /* All others will be Hex escapes */
210 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
212 break;
215 AcpiOsPrintf ("\"");
217 if (i == MaxLength && String[i])
219 AcpiOsPrintf ("...");
224 /*******************************************************************************
226 * FUNCTION: AcpiUtDwordByteSwap
228 * PARAMETERS: Value - Value to be converted
230 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
232 ******************************************************************************/
234 UINT32
235 AcpiUtDwordByteSwap (
236 UINT32 Value)
238 union
240 UINT32 Value;
241 UINT8 Bytes[4];
242 } Out;
244 union
246 UINT32 Value;
247 UINT8 Bytes[4];
248 } In;
251 ACPI_FUNCTION_ENTRY ();
254 In.Value = Value;
256 Out.Bytes[0] = In.Bytes[3];
257 Out.Bytes[1] = In.Bytes[2];
258 Out.Bytes[2] = In.Bytes[1];
259 Out.Bytes[3] = In.Bytes[0];
261 return (Out.Value);
265 /*******************************************************************************
267 * FUNCTION: AcpiUtSetIntegerWidth
269 * PARAMETERS: Revision From DSDT header
271 * RETURN: None
273 * DESCRIPTION: Set the global integer bit width based upon the revision
274 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
275 * For Revision 2 and above, Integers are 64 bits. Yes, this
276 * makes a difference.
278 ******************************************************************************/
280 void
281 AcpiUtSetIntegerWidth (
282 UINT8 Revision)
285 if (Revision <= 1)
287 AcpiGbl_IntegerBitWidth = 32;
288 AcpiGbl_IntegerNybbleWidth = 8;
289 AcpiGbl_IntegerByteWidth = 4;
291 else
293 AcpiGbl_IntegerBitWidth = 64;
294 AcpiGbl_IntegerNybbleWidth = 16;
295 AcpiGbl_IntegerByteWidth = 8;
300 #ifdef ACPI_DEBUG_OUTPUT
301 /*******************************************************************************
303 * FUNCTION: AcpiUtDisplayInitPathname
305 * PARAMETERS: ObjHandle - Handle whose pathname will be displayed
306 * Path - Additional path string to be appended.
307 * (NULL if no extra path)
309 * RETURN: ACPI_STATUS
311 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
313 ******************************************************************************/
315 void
316 AcpiUtDisplayInitPathname (
317 UINT8 Type,
318 ACPI_NAMESPACE_NODE *ObjHandle,
319 char *Path)
321 ACPI_STATUS Status;
322 ACPI_BUFFER Buffer;
325 ACPI_FUNCTION_ENTRY ();
328 /* Only print the path if the appropriate debug level is enabled */
330 if (!(AcpiDbgLevel & ACPI_LV_INIT_NAMES))
332 return;
335 /* Get the full pathname to the node */
337 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
338 Status = AcpiNsHandleToPathname (ObjHandle, &Buffer);
339 if (ACPI_FAILURE (Status))
341 return;
344 /* Print what we're doing */
346 switch (Type)
348 case ACPI_TYPE_METHOD:
349 AcpiOsPrintf ("Executing ");
350 break;
352 default:
353 AcpiOsPrintf ("Initializing ");
354 break;
357 /* Print the object type and pathname */
359 AcpiOsPrintf ("%-12s %s", AcpiUtGetTypeName (Type), (char *) Buffer.Pointer);
361 /* Extra path is used to append names like _STA, _INI, etc. */
363 if (Path)
365 AcpiOsPrintf (".%s", Path);
367 AcpiOsPrintf ("\n");
369 ACPI_MEM_FREE (Buffer.Pointer);
371 #endif
374 /*******************************************************************************
376 * FUNCTION: AcpiUtValidAcpiName
378 * PARAMETERS: Character - The character to be examined
380 * RETURN: 1 if Character may appear in a name, else 0
382 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
383 * 1) Upper case alpha
384 * 2) numeric
385 * 3) underscore
387 ******************************************************************************/
389 BOOLEAN
390 AcpiUtValidAcpiName (
391 UINT32 Name)
393 char *NamePtr = (char *) &Name;
394 char Character;
395 ACPI_NATIVE_UINT i;
398 ACPI_FUNCTION_ENTRY ();
401 for (i = 0; i < ACPI_NAME_SIZE; i++)
403 Character = *NamePtr;
404 NamePtr++;
406 if (!((Character == '_') ||
407 (Character >= 'A' && Character <= 'Z') ||
408 (Character >= '0' && Character <= '9')))
410 return (FALSE);
414 return (TRUE);
418 /*******************************************************************************
420 * FUNCTION: AcpiUtValidAcpiCharacter
422 * PARAMETERS: Character - The character to be examined
424 * RETURN: 1 if Character may appear in a name, else 0
426 * DESCRIPTION: Check for a printable character
428 ******************************************************************************/
430 BOOLEAN
431 AcpiUtValidAcpiCharacter (
432 char Character)
435 ACPI_FUNCTION_ENTRY ();
437 return ((BOOLEAN) ((Character == '_') ||
438 (Character >= 'A' && Character <= 'Z') ||
439 (Character >= '0' && Character <= '9')));
443 /*******************************************************************************
445 * FUNCTION: AcpiUtStrtoul64
447 * PARAMETERS: String - Null terminated string
448 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
449 * RetInteger - Where the converted integer is returned
451 * RETURN: Status and Converted value
453 * DESCRIPTION: Convert a string into an unsigned value.
454 * NOTE: Does not support Octal strings, not needed.
456 ******************************************************************************/
458 ACPI_STATUS
459 AcpiUtStrtoul64 (
460 char *String,
461 UINT32 Base,
462 ACPI_INTEGER *RetInteger)
464 UINT32 ThisDigit = 0;
465 ACPI_INTEGER ReturnValue = 0;
466 ACPI_INTEGER Quotient;
469 ACPI_FUNCTION_TRACE ("UtStroul64");
472 if ((!String) || !(*String))
474 goto ErrorExit;
477 switch (Base)
479 case ACPI_ANY_BASE:
480 case 10:
481 case 16:
482 break;
484 default:
485 /* Invalid Base */
486 return_ACPI_STATUS (AE_BAD_PARAMETER);
489 /* Skip over any white space in the buffer */
491 while (ACPI_IS_SPACE (*String) || *String == '\t')
493 String++;
497 * If the input parameter Base is zero, then we need to
498 * determine if it is decimal or hexadecimal:
500 if (Base == 0)
502 if ((*String == '0') &&
503 (ACPI_TOLOWER (*(String + 1)) == 'x'))
505 Base = 16;
506 String += 2;
508 else
510 Base = 10;
515 * For hexadecimal base, skip over the leading
516 * 0 or 0x, if they are present.
518 if ((Base == 16) &&
519 (*String == '0') &&
520 (ACPI_TOLOWER (*(String + 1)) == 'x'))
522 String += 2;
525 /* Any string left? */
527 if (!(*String))
529 goto ErrorExit;
532 /* Main loop: convert the string to a 64-bit integer */
534 while (*String)
536 if (ACPI_IS_DIGIT (*String))
538 /* Convert ASCII 0-9 to Decimal value */
540 ThisDigit = ((UINT8) *String) - '0';
542 else
544 if (Base == 10)
546 /* Digit is out of range */
548 goto ErrorExit;
551 ThisDigit = (UINT8) ACPI_TOUPPER (*String);
552 if (ACPI_IS_XDIGIT ((char) ThisDigit))
554 /* Convert ASCII Hex char to value */
556 ThisDigit = ThisDigit - 'A' + 10;
558 else
561 * We allow non-hex chars, just stop now, same as end-of-string.
562 * See ACPI spec, string-to-integer conversion.
564 break;
568 /* Divide the digit into the correct position */
570 (void) AcpiUtShortDivide ((ACPI_INTEGER_MAX - (ACPI_INTEGER) ThisDigit),
571 Base, &Quotient, NULL);
572 if (ReturnValue > Quotient)
574 goto ErrorExit;
577 ReturnValue *= Base;
578 ReturnValue += ThisDigit;
579 String++;
582 /* All done, normal exit */
584 *RetInteger = ReturnValue;
585 return_ACPI_STATUS (AE_OK);
588 ErrorExit:
589 /* Base was set/validated above */
591 if (Base == 10)
593 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
595 else
597 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
602 /*******************************************************************************
604 * FUNCTION: AcpiUtStrupr
606 * PARAMETERS: SrcString - The source string to convert to
608 * RETURN: SrcString
610 * DESCRIPTION: Convert string to uppercase
612 ******************************************************************************/
614 char *
615 AcpiUtStrupr (
616 char *SrcString)
618 char *String;
621 ACPI_FUNCTION_ENTRY ();
624 /* Walk entire string, uppercasing the letters */
626 for (String = SrcString; *String; )
628 *String = (char) ACPI_TOUPPER (*String);
629 String++;
632 return (SrcString);
636 /*******************************************************************************
638 * FUNCTION: AcpiUtMutexInitialize
640 * PARAMETERS: None.
642 * RETURN: Status
644 * DESCRIPTION: Create the system mutex objects.
646 ******************************************************************************/
648 ACPI_STATUS
649 AcpiUtMutexInitialize (
650 void)
652 UINT32 i;
653 ACPI_STATUS Status;
656 ACPI_FUNCTION_TRACE ("UtMutexInitialize");
660 * Create each of the predefined mutex objects
662 for (i = 0; i < NUM_MUTEX; i++)
664 Status = AcpiUtCreateMutex (i);
665 if (ACPI_FAILURE (Status))
667 return_ACPI_STATUS (Status);
671 Status = AcpiOsCreateLock (&AcpiGbl_GpeLock);
672 return_ACPI_STATUS (Status);
676 /*******************************************************************************
678 * FUNCTION: AcpiUtMutexTerminate
680 * PARAMETERS: None.
682 * RETURN: None.
684 * DESCRIPTION: Delete all of the system mutex objects.
686 ******************************************************************************/
688 void
689 AcpiUtMutexTerminate (
690 void)
692 UINT32 i;
695 ACPI_FUNCTION_TRACE ("UtMutexTerminate");
699 * Delete each predefined mutex object
701 for (i = 0; i < NUM_MUTEX; i++)
703 (void) AcpiUtDeleteMutex (i);
706 AcpiOsDeleteLock (AcpiGbl_GpeLock);
707 return_VOID;
711 /*******************************************************************************
713 * FUNCTION: AcpiUtCreateMutex
715 * PARAMETERS: MutexID - ID of the mutex to be created
717 * RETURN: Status
719 * DESCRIPTION: Create a mutex object.
721 ******************************************************************************/
723 ACPI_STATUS
724 AcpiUtCreateMutex (
725 ACPI_MUTEX_HANDLE MutexId)
727 ACPI_STATUS Status = AE_OK;
730 ACPI_FUNCTION_TRACE_U32 ("UtCreateMutex", MutexId);
733 if (MutexId > MAX_MUTEX)
735 return_ACPI_STATUS (AE_BAD_PARAMETER);
738 if (!AcpiGbl_MutexInfo[MutexId].Mutex)
740 Status = AcpiOsCreateSemaphore (1, 1,
741 &AcpiGbl_MutexInfo[MutexId].Mutex);
742 AcpiGbl_MutexInfo[MutexId].OwnerId = ACPI_MUTEX_NOT_ACQUIRED;
743 AcpiGbl_MutexInfo[MutexId].UseCount = 0;
746 return_ACPI_STATUS (Status);
750 /*******************************************************************************
752 * FUNCTION: AcpiUtDeleteMutex
754 * PARAMETERS: MutexID - ID of the mutex to be deleted
756 * RETURN: Status
758 * DESCRIPTION: Delete a mutex object.
760 ******************************************************************************/
762 ACPI_STATUS
763 AcpiUtDeleteMutex (
764 ACPI_MUTEX_HANDLE MutexId)
766 ACPI_STATUS Status;
769 ACPI_FUNCTION_TRACE_U32 ("UtDeleteMutex", MutexId);
772 if (MutexId > MAX_MUTEX)
774 return_ACPI_STATUS (AE_BAD_PARAMETER);
777 Status = AcpiOsDeleteSemaphore (AcpiGbl_MutexInfo[MutexId].Mutex);
779 AcpiGbl_MutexInfo[MutexId].Mutex = NULL;
780 AcpiGbl_MutexInfo[MutexId].OwnerId = ACPI_MUTEX_NOT_ACQUIRED;
782 return_ACPI_STATUS (Status);
786 /*******************************************************************************
788 * FUNCTION: AcpiUtAcquireMutex
790 * PARAMETERS: MutexID - ID of the mutex to be acquired
792 * RETURN: Status
794 * DESCRIPTION: Acquire a mutex object.
796 ******************************************************************************/
798 ACPI_STATUS
799 AcpiUtAcquireMutex (
800 ACPI_MUTEX_HANDLE MutexId)
802 ACPI_STATUS Status;
803 UINT32 ThisThreadId;
806 ACPI_FUNCTION_NAME ("UtAcquireMutex");
809 if (MutexId > MAX_MUTEX)
811 return (AE_BAD_PARAMETER);
814 ThisThreadId = AcpiOsGetThreadId ();
816 #ifdef ACPI_MUTEX_DEBUG
818 UINT32 i;
820 * Mutex debug code, for internal debugging only.
822 * Deadlock prevention. Check if this thread owns any mutexes of value
823 * greater than or equal to this one. If so, the thread has violated
824 * the mutex ordering rule. This indicates a coding error somewhere in
825 * the ACPI subsystem code.
827 for (i = MutexId; i < MAX_MUTEX; i++)
829 if (AcpiGbl_MutexInfo[i].OwnerId == ThisThreadId)
831 if (i == MutexId)
833 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
834 "Mutex [%s] already acquired by this thread [%X]\n",
835 AcpiUtGetMutexName (MutexId), ThisThreadId));
837 return (AE_ALREADY_ACQUIRED);
840 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
841 "Invalid acquire order: Thread %X owns [%s], wants [%s]\n",
842 ThisThreadId, AcpiUtGetMutexName (i),
843 AcpiUtGetMutexName (MutexId)));
845 return (AE_ACQUIRE_DEADLOCK);
849 #endif
851 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
852 "Thread %X attempting to acquire Mutex [%s]\n",
853 ThisThreadId, AcpiUtGetMutexName (MutexId)));
855 Status = AcpiOsWaitSemaphore (AcpiGbl_MutexInfo[MutexId].Mutex,
856 1, ACPI_WAIT_FOREVER);
857 if (ACPI_SUCCESS (Status))
859 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X acquired Mutex [%s]\n",
860 ThisThreadId, AcpiUtGetMutexName (MutexId)));
862 AcpiGbl_MutexInfo[MutexId].UseCount++;
863 AcpiGbl_MutexInfo[MutexId].OwnerId = ThisThreadId;
865 else
867 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not acquire Mutex [%s] %s\n",
868 ThisThreadId, AcpiUtGetMutexName (MutexId),
869 AcpiFormatException (Status)));
872 return (Status);
876 /*******************************************************************************
878 * FUNCTION: AcpiUtReleaseMutex
880 * PARAMETERS: MutexID - ID of the mutex to be released
882 * RETURN: Status
884 * DESCRIPTION: Release a mutex object.
886 ******************************************************************************/
888 ACPI_STATUS
889 AcpiUtReleaseMutex (
890 ACPI_MUTEX_HANDLE MutexId)
892 ACPI_STATUS Status;
893 UINT32 i;
894 UINT32 ThisThreadId;
897 ACPI_FUNCTION_NAME ("UtReleaseMutex");
900 ThisThreadId = AcpiOsGetThreadId ();
901 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
902 "Thread %X releasing Mutex [%s]\n", ThisThreadId,
903 AcpiUtGetMutexName (MutexId)));
905 if (MutexId > MAX_MUTEX)
907 return (AE_BAD_PARAMETER);
911 * Mutex must be acquired in order to release it!
913 if (AcpiGbl_MutexInfo[MutexId].OwnerId == ACPI_MUTEX_NOT_ACQUIRED)
915 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
916 "Mutex [%s] is not acquired, cannot release\n",
917 AcpiUtGetMutexName (MutexId)));
919 return (AE_NOT_ACQUIRED);
923 * Deadlock prevention. Check if this thread owns any mutexes of value
924 * greater than this one. If so, the thread has violated the mutex
925 * ordering rule. This indicates a coding error somewhere in
926 * the ACPI subsystem code.
928 for (i = MutexId; i < MAX_MUTEX; i++)
930 if (AcpiGbl_MutexInfo[i].OwnerId == ThisThreadId)
932 if (i == MutexId)
934 continue;
937 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
938 "Invalid release order: owns [%s], releasing [%s]\n",
939 AcpiUtGetMutexName (i), AcpiUtGetMutexName (MutexId)));
941 return (AE_RELEASE_DEADLOCK);
945 /* Mark unlocked FIRST */
947 AcpiGbl_MutexInfo[MutexId].OwnerId = ACPI_MUTEX_NOT_ACQUIRED;
949 Status = AcpiOsSignalSemaphore (AcpiGbl_MutexInfo[MutexId].Mutex, 1);
951 if (ACPI_FAILURE (Status))
953 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not release Mutex [%s] %s\n",
954 ThisThreadId, AcpiUtGetMutexName (MutexId),
955 AcpiFormatException (Status)));
957 else
959 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X released Mutex [%s]\n",
960 ThisThreadId, AcpiUtGetMutexName (MutexId)));
963 return (Status);
967 /*******************************************************************************
969 * FUNCTION: AcpiUtCreateUpdateStateAndPush
971 * PARAMETERS: *Object - Object to be added to the new state
972 * Action - Increment/Decrement
973 * StateList - List the state will be added to
975 * RETURN: None
977 * DESCRIPTION: Create a new state and push it
979 ******************************************************************************/
981 ACPI_STATUS
982 AcpiUtCreateUpdateStateAndPush (
983 ACPI_OPERAND_OBJECT *Object,
984 UINT16 Action,
985 ACPI_GENERIC_STATE **StateList)
987 ACPI_GENERIC_STATE *State;
990 ACPI_FUNCTION_ENTRY ();
993 /* Ignore null objects; these are expected */
995 if (!Object)
997 return (AE_OK);
1000 State = AcpiUtCreateUpdateState (Object, Action);
1001 if (!State)
1003 return (AE_NO_MEMORY);
1006 AcpiUtPushGenericState (StateList, State);
1007 return (AE_OK);
1011 /*******************************************************************************
1013 * FUNCTION: AcpiUtCreatePkgStateAndPush
1015 * PARAMETERS: *Object - Object to be added to the new state
1016 * Action - Increment/Decrement
1017 * StateList - List the state will be added to
1019 * RETURN: None
1021 * DESCRIPTION: Create a new state and push it
1023 ******************************************************************************/
1025 ACPI_STATUS
1026 AcpiUtCreatePkgStateAndPush (
1027 void *InternalObject,
1028 void *ExternalObject,
1029 UINT16 Index,
1030 ACPI_GENERIC_STATE **StateList)
1032 ACPI_GENERIC_STATE *State;
1035 ACPI_FUNCTION_ENTRY ();
1038 State = AcpiUtCreatePkgState (InternalObject, ExternalObject, Index);
1039 if (!State)
1041 return (AE_NO_MEMORY);
1044 AcpiUtPushGenericState (StateList, State);
1045 return (AE_OK);
1049 /*******************************************************************************
1051 * FUNCTION: AcpiUtPushGenericState
1053 * PARAMETERS: ListHead - Head of the state stack
1054 * State - State object to push
1056 * RETURN: Status
1058 * DESCRIPTION: Push a state object onto a state stack
1060 ******************************************************************************/
1062 void
1063 AcpiUtPushGenericState (
1064 ACPI_GENERIC_STATE **ListHead,
1065 ACPI_GENERIC_STATE *State)
1067 ACPI_FUNCTION_TRACE ("UtPushGenericState");
1070 /* Push the state object onto the front of the list (stack) */
1072 State->Common.Next = *ListHead;
1073 *ListHead = State;
1075 return_VOID;
1079 /*******************************************************************************
1081 * FUNCTION: AcpiUtPopGenericState
1083 * PARAMETERS: ListHead - Head of the state stack
1085 * RETURN: Status
1087 * DESCRIPTION: Pop a state object from a state stack
1089 ******************************************************************************/
1091 ACPI_GENERIC_STATE *
1092 AcpiUtPopGenericState (
1093 ACPI_GENERIC_STATE **ListHead)
1095 ACPI_GENERIC_STATE *State;
1098 ACPI_FUNCTION_TRACE ("UtPopGenericState");
1101 /* Remove the state object at the head of the list (stack) */
1103 State = *ListHead;
1104 if (State)
1106 /* Update the list head */
1108 *ListHead = State->Common.Next;
1111 return_PTR (State);
1115 /*******************************************************************************
1117 * FUNCTION: AcpiUtCreateGenericState
1119 * PARAMETERS: None
1121 * RETURN: Status
1123 * DESCRIPTION: Create a generic state object. Attempt to obtain one from
1124 * the global state cache; If none available, create a new one.
1126 ******************************************************************************/
1128 ACPI_GENERIC_STATE *
1129 AcpiUtCreateGenericState (void)
1131 ACPI_GENERIC_STATE *State;
1134 ACPI_FUNCTION_ENTRY ();
1137 State = AcpiUtAcquireFromCache (ACPI_MEM_LIST_STATE);
1139 /* Initialize */
1141 if (State)
1143 State->Common.DataType = ACPI_DESC_TYPE_STATE;
1146 return (State);
1150 /*******************************************************************************
1152 * FUNCTION: AcpiUtCreateThreadState
1154 * PARAMETERS: None
1156 * RETURN: Thread State
1158 * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
1159 * to track per-thread info during method execution
1161 ******************************************************************************/
1163 ACPI_THREAD_STATE *
1164 AcpiUtCreateThreadState (
1165 void)
1167 ACPI_GENERIC_STATE *State;
1170 ACPI_FUNCTION_TRACE ("UtCreateThreadState");
1173 /* Create the generic state object */
1175 State = AcpiUtCreateGenericState ();
1176 if (!State)
1178 return_PTR (NULL);
1181 /* Init fields specific to the update struct */
1183 State->Common.DataType = ACPI_DESC_TYPE_STATE_THREAD;
1184 State->Thread.ThreadId = AcpiOsGetThreadId ();
1186 return_PTR ((ACPI_THREAD_STATE *) State);
1190 /*******************************************************************************
1192 * FUNCTION: AcpiUtCreateUpdateState
1194 * PARAMETERS: Object - Initial Object to be installed in the
1195 * state
1196 * Action - Update action to be performed
1198 * RETURN: Status
1200 * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
1201 * to update reference counts and delete complex objects such
1202 * as packages.
1204 ******************************************************************************/
1206 ACPI_GENERIC_STATE *
1207 AcpiUtCreateUpdateState (
1208 ACPI_OPERAND_OBJECT *Object,
1209 UINT16 Action)
1211 ACPI_GENERIC_STATE *State;
1214 ACPI_FUNCTION_TRACE_PTR ("UtCreateUpdateState", Object);
1217 /* Create the generic state object */
1219 State = AcpiUtCreateGenericState ();
1220 if (!State)
1222 return_PTR (NULL);
1225 /* Init fields specific to the update struct */
1227 State->Common.DataType = ACPI_DESC_TYPE_STATE_UPDATE;
1228 State->Update.Object = Object;
1229 State->Update.Value = Action;
1231 return_PTR (State);
1235 /*******************************************************************************
1237 * FUNCTION: AcpiUtCreatePkgState
1239 * PARAMETERS: Object - Initial Object to be installed in the
1240 * state
1241 * Action - Update action to be performed
1243 * RETURN: Status
1245 * DESCRIPTION: Create a "Package State"
1247 ******************************************************************************/
1249 ACPI_GENERIC_STATE *
1250 AcpiUtCreatePkgState (
1251 void *InternalObject,
1252 void *ExternalObject,
1253 UINT16 Index)
1255 ACPI_GENERIC_STATE *State;
1258 ACPI_FUNCTION_TRACE_PTR ("UtCreatePkgState", InternalObject);
1261 /* Create the generic state object */
1263 State = AcpiUtCreateGenericState ();
1264 if (!State)
1266 return_PTR (NULL);
1269 /* Init fields specific to the update struct */
1271 State->Common.DataType = ACPI_DESC_TYPE_STATE_PACKAGE;
1272 State->Pkg.SourceObject = (ACPI_OPERAND_OBJECT *) InternalObject;
1273 State->Pkg.DestObject = ExternalObject;
1274 State->Pkg.Index = Index;
1275 State->Pkg.NumPackages = 1;
1277 return_PTR (State);
1281 /*******************************************************************************
1283 * FUNCTION: AcpiUtCreateControlState
1285 * PARAMETERS: None
1287 * RETURN: Status
1289 * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
1290 * to support nested IF/WHILE constructs in the AML.
1292 ******************************************************************************/
1294 ACPI_GENERIC_STATE *
1295 AcpiUtCreateControlState (
1296 void)
1298 ACPI_GENERIC_STATE *State;
1301 ACPI_FUNCTION_TRACE ("UtCreateControlState");
1304 /* Create the generic state object */
1306 State = AcpiUtCreateGenericState ();
1307 if (!State)
1309 return_PTR (NULL);
1312 /* Init fields specific to the control struct */
1314 State->Common.DataType = ACPI_DESC_TYPE_STATE_CONTROL;
1315 State->Common.State = ACPI_CONTROL_CONDITIONAL_EXECUTING;
1317 return_PTR (State);
1321 /*******************************************************************************
1323 * FUNCTION: AcpiUtDeleteGenericState
1325 * PARAMETERS: State - The state object to be deleted
1327 * RETURN: Status
1329 * DESCRIPTION: Put a state object back into the global state cache. The object
1330 * is not actually freed at this time.
1332 ******************************************************************************/
1334 void
1335 AcpiUtDeleteGenericState (
1336 ACPI_GENERIC_STATE *State)
1338 ACPI_FUNCTION_TRACE ("UtDeleteGenericState");
1341 AcpiUtReleaseToCache (ACPI_MEM_LIST_STATE, State);
1342 return_VOID;
1346 #ifdef ACPI_ENABLE_OBJECT_CACHE
1347 /*******************************************************************************
1349 * FUNCTION: AcpiUtDeleteGenericStateCache
1351 * PARAMETERS: None
1353 * RETURN: Status
1355 * DESCRIPTION: Purge the global state object cache. Used during subsystem
1356 * termination.
1358 ******************************************************************************/
1360 void
1361 AcpiUtDeleteGenericStateCache (
1362 void)
1364 ACPI_FUNCTION_TRACE ("UtDeleteGenericStateCache");
1367 AcpiUtDeleteGenericCache (ACPI_MEM_LIST_STATE);
1368 return_VOID;
1370 #endif
1373 /*******************************************************************************
1375 * FUNCTION: AcpiUtWalkPackageTree
1377 * PARAMETERS: ObjDesc - The Package object on which to resolve refs
1379 * RETURN: Status
1381 * DESCRIPTION: Walk through a package
1383 ******************************************************************************/
1385 ACPI_STATUS
1386 AcpiUtWalkPackageTree (
1387 ACPI_OPERAND_OBJECT *SourceObject,
1388 void *TargetObject,
1389 ACPI_PKG_CALLBACK WalkCallback,
1390 void *Context)
1392 ACPI_STATUS Status = AE_OK;
1393 ACPI_GENERIC_STATE *StateList = NULL;
1394 ACPI_GENERIC_STATE *State;
1395 UINT32 ThisIndex;
1396 ACPI_OPERAND_OBJECT *ThisSourceObj;
1399 ACPI_FUNCTION_TRACE ("UtWalkPackageTree");
1402 State = AcpiUtCreatePkgState (SourceObject, TargetObject, 0);
1403 if (!State)
1405 return_ACPI_STATUS (AE_NO_MEMORY);
1408 while (State)
1410 /* Get one element of the package */
1412 ThisIndex = State->Pkg.Index;
1413 ThisSourceObj = (ACPI_OPERAND_OBJECT *)
1414 State->Pkg.SourceObject->Package.Elements[ThisIndex];
1417 * Check for:
1418 * 1) An uninitialized package element. It is completely
1419 * legal to declare a package and leave it uninitialized
1420 * 2) Not an internal object - can be a namespace node instead
1421 * 3) Any type other than a package. Packages are handled in else
1422 * case below.
1424 if ((!ThisSourceObj) ||
1425 (ACPI_GET_DESCRIPTOR_TYPE (ThisSourceObj) != ACPI_DESC_TYPE_OPERAND) ||
1426 (ACPI_GET_OBJECT_TYPE (ThisSourceObj) != ACPI_TYPE_PACKAGE))
1428 Status = WalkCallback (ACPI_COPY_TYPE_SIMPLE, ThisSourceObj,
1429 State, Context);
1430 if (ACPI_FAILURE (Status))
1432 return_ACPI_STATUS (Status);
1435 State->Pkg.Index++;
1436 while (State->Pkg.Index >= State->Pkg.SourceObject->Package.Count)
1439 * We've handled all of the objects at this level, This means
1440 * that we have just completed a package. That package may
1441 * have contained one or more packages itself.
1443 * Delete this state and pop the previous state (package).
1445 AcpiUtDeleteGenericState (State);
1446 State = AcpiUtPopGenericState (&StateList);
1448 /* Finished when there are no more states */
1450 if (!State)
1453 * We have handled all of the objects in the top level
1454 * package just add the length of the package objects
1455 * and exit
1457 return_ACPI_STATUS (AE_OK);
1461 * Go back up a level and move the index past the just
1462 * completed package object.
1464 State->Pkg.Index++;
1467 else
1469 /* This is a subobject of type package */
1471 Status = WalkCallback (ACPI_COPY_TYPE_PACKAGE, ThisSourceObj,
1472 State, Context);
1473 if (ACPI_FAILURE (Status))
1475 return_ACPI_STATUS (Status);
1479 * Push the current state and create a new one
1480 * The callback above returned a new target package object.
1482 AcpiUtPushGenericState (&StateList, State);
1483 State = AcpiUtCreatePkgState (ThisSourceObj,
1484 State->Pkg.ThisTargetObj, 0);
1485 if (!State)
1487 return_ACPI_STATUS (AE_NO_MEMORY);
1492 /* We should never get here */
1494 return_ACPI_STATUS (AE_AML_INTERNAL);
1498 /*******************************************************************************
1500 * FUNCTION: AcpiUtGenerateChecksum
1502 * PARAMETERS: Buffer - Buffer to be scanned
1503 * Length - number of bytes to examine
1505 * RETURN: checksum
1507 * DESCRIPTION: Generate a checksum on a raw buffer
1509 ******************************************************************************/
1511 UINT8
1512 AcpiUtGenerateChecksum (
1513 UINT8 *Buffer,
1514 UINT32 Length)
1516 UINT32 i;
1517 signed char Sum = 0;
1520 for (i = 0; i < Length; i++)
1522 Sum = (signed char) (Sum + Buffer[i]);
1525 return ((UINT8) (0 - Sum));
1529 /*******************************************************************************
1531 * FUNCTION: AcpiUtGetResourceEndTag
1533 * PARAMETERS: ObjDesc - The resource template buffer object
1535 * RETURN: Pointer to the end tag
1537 * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
1539 ******************************************************************************/
1542 UINT8 *
1543 AcpiUtGetResourceEndTag (
1544 ACPI_OPERAND_OBJECT *ObjDesc)
1546 UINT8 BufferByte;
1547 UINT8 *Buffer;
1548 UINT8 *EndBuffer;
1551 Buffer = ObjDesc->Buffer.Pointer;
1552 EndBuffer = Buffer + ObjDesc->Buffer.Length;
1554 while (Buffer < EndBuffer)
1556 BufferByte = *Buffer;
1557 if (BufferByte & ACPI_RDESC_TYPE_MASK)
1559 /* Large Descriptor - Length is next 2 bytes */
1561 Buffer += ((*(Buffer+1) | (*(Buffer+2) << 8)) + 3);
1563 else
1565 /* Small Descriptor. End Tag will be found here */
1567 if ((BufferByte & ACPI_RDESC_SMALL_MASK) == ACPI_RDESC_TYPE_END_TAG)
1569 /* Found the end tag descriptor, all done. */
1571 return (Buffer);
1574 /* Length is in the header */
1576 Buffer += ((BufferByte & 0x07) + 1);
1580 /* End tag not found */
1582 return (NULL);
1586 /*******************************************************************************
1588 * FUNCTION: AcpiUtReportError
1590 * PARAMETERS: ModuleName - Caller's module name (for error output)
1591 * LineNumber - Caller's line number (for error output)
1592 * ComponentId - Caller's component ID (for error output)
1593 * Message - Error message to use on failure
1595 * RETURN: None
1597 * DESCRIPTION: Print error message
1599 ******************************************************************************/
1601 void
1602 AcpiUtReportError (
1603 char *ModuleName,
1604 UINT32 LineNumber,
1605 UINT32 ComponentId)
1609 AcpiOsPrintf ("%8s-%04d: *** Error: ", ModuleName, LineNumber);
1613 /*******************************************************************************
1615 * FUNCTION: AcpiUtReportWarning
1617 * PARAMETERS: ModuleName - Caller's module name (for error output)
1618 * LineNumber - Caller's line number (for error output)
1619 * ComponentId - Caller's component ID (for error output)
1620 * Message - Error message to use on failure
1622 * RETURN: None
1624 * DESCRIPTION: Print warning message
1626 ******************************************************************************/
1628 void
1629 AcpiUtReportWarning (
1630 char *ModuleName,
1631 UINT32 LineNumber,
1632 UINT32 ComponentId)
1635 AcpiOsPrintf ("%8s-%04d: *** Warning: ", ModuleName, LineNumber);
1639 /*******************************************************************************
1641 * FUNCTION: AcpiUtReportInfo
1643 * PARAMETERS: ModuleName - Caller's module name (for error output)
1644 * LineNumber - Caller's line number (for error output)
1645 * ComponentId - Caller's component ID (for error output)
1646 * Message - Error message to use on failure
1648 * RETURN: None
1650 * DESCRIPTION: Print information message
1652 ******************************************************************************/
1654 void
1655 AcpiUtReportInfo (
1656 char *ModuleName,
1657 UINT32 LineNumber,
1658 UINT32 ComponentId)
1661 AcpiOsPrintf ("%8s-%04d: *** Info: ", ModuleName, LineNumber);