Merge from vendor branch INTEL_ACPICA:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / utilities / utmisc.c
1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *              $Revision: 1.150 $
5  *
6  ******************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
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.
21  *
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
28  *
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;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
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.
54  *
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.
66  *
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.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
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.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
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.
90  *
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.
99  *
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.
114  *
115  *****************************************************************************/
116
117
118 #define __UTMISC_C__
119
120 #include "acpi.h"
121 #include "acnamesp.h"
122
123
124 #define _COMPONENT          ACPI_UTILITIES
125         ACPI_MODULE_NAME    ("utmisc")
126
127
128 /*******************************************************************************
129  *
130  * FUNCTION:    AcpiUtValidateException
131  *
132  * PARAMETERS:  Status       - The ACPI_STATUS code to be formatted
133  *
134  * RETURN:      A string containing the exception text. NULL if exception is
135  *              not valid.
136  *
137  * DESCRIPTION: This function validates and translates an ACPI exception into
138  *              an ASCII string.
139  *
140  ******************************************************************************/
141
142 const char *
143 AcpiUtValidateException (
144     ACPI_STATUS             Status)
145 {
146     ACPI_STATUS             SubStatus;
147     const char              *Exception = NULL;
148
149
150     ACPI_FUNCTION_ENTRY ();
151
152
153     /*
154      * Status is composed of two parts, a "type" and an actual code
155      */
156     SubStatus = (Status & ~AE_CODE_MASK);
157
158     switch (Status & AE_CODE_MASK)
159     {
160     case AE_CODE_ENVIRONMENTAL:
161
162         if (SubStatus <= AE_CODE_ENV_MAX)
163         {
164             Exception = AcpiGbl_ExceptionNames_Env [SubStatus];
165         }
166         break;
167
168     case AE_CODE_PROGRAMMER:
169
170         if (SubStatus <= AE_CODE_PGM_MAX)
171         {
172             Exception = AcpiGbl_ExceptionNames_Pgm [SubStatus -1];
173         }
174         break;
175
176     case AE_CODE_ACPI_TABLES:
177
178         if (SubStatus <= AE_CODE_TBL_MAX)
179         {
180             Exception = AcpiGbl_ExceptionNames_Tbl [SubStatus -1];
181         }
182         break;
183
184     case AE_CODE_AML:
185
186         if (SubStatus <= AE_CODE_AML_MAX)
187         {
188             Exception = AcpiGbl_ExceptionNames_Aml [SubStatus -1];
189         }
190         break;
191
192     case AE_CODE_CONTROL:
193
194         if (SubStatus <= AE_CODE_CTRL_MAX)
195         {
196             Exception = AcpiGbl_ExceptionNames_Ctrl [SubStatus -1];
197         }
198         break;
199
200     default:
201         break;
202     }
203
204     return (ACPI_CAST_PTR (const char, Exception));
205 }
206
207
208 /*******************************************************************************
209  *
210  * FUNCTION:    AcpiUtIsAmlTable
211  *
212  * PARAMETERS:  Table               - An ACPI table
213  *
214  * RETURN:      TRUE if table contains executable AML; FALSE otherwise
215  *
216  * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
217  *              Currently, these are DSDT,SSDT,PSDT. All other table types are
218  *              data tables that do not contain AML code.
219  *
220  ******************************************************************************/
221
222 BOOLEAN
223 AcpiUtIsAmlTable (
224     ACPI_TABLE_HEADER       *Table)
225 {
226
227     /* These are the only tables that contain executable AML */
228
229     if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) ||
230         ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_PSDT) ||
231         ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
232     {
233         return (TRUE);
234     }
235
236     return (FALSE);
237 }
238
239
240 /*******************************************************************************
241  *
242  * FUNCTION:    AcpiUtAllocateOwnerId
243  *
244  * PARAMETERS:  OwnerId         - Where the new owner ID is returned
245  *
246  * RETURN:      Status
247  *
248  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
249  *              track objects created by the table or method, to be deleted
250  *              when the method exits or the table is unloaded.
251  *
252  ******************************************************************************/
253
254 ACPI_STATUS
255 AcpiUtAllocateOwnerId (
256     ACPI_OWNER_ID           *OwnerId)
257 {
258     ACPI_NATIVE_UINT        i;
259     ACPI_NATIVE_UINT        j;
260     ACPI_NATIVE_UINT        k;
261     ACPI_STATUS             Status;
262
263
264     ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
265
266
267     /* Guard against multiple allocations of ID to the same location */
268
269     if (*OwnerId)
270     {
271         ACPI_ERROR ((AE_INFO, "Owner ID [%2.2X] already exists", *OwnerId));
272         return_ACPI_STATUS (AE_ALREADY_EXISTS);
273     }
274
275     /* Mutex for the global ID mask */
276
277     Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
278     if (ACPI_FAILURE (Status))
279     {
280         return_ACPI_STATUS (Status);
281     }
282
283     /*
284      * Find a free owner ID, cycle through all possible IDs on repeated
285      * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
286      * to be scanned twice.
287      */
288     for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
289          i < (ACPI_NUM_OWNERID_MASKS + 1);
290          i++, j++)
291     {
292         if (j >= ACPI_NUM_OWNERID_MASKS)
293         {
294             j = 0;  /* Wraparound to start of mask array */
295         }
296
297         for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
298         {
299             if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
300             {
301                 /* There are no free IDs in this mask */
302
303                 break;
304             }
305
306             if (!(AcpiGbl_OwnerIdMask[j] & (1 << k)))
307             {
308                 /*
309                  * Found a free ID. The actual ID is the bit index plus one,
310                  * making zero an invalid Owner ID. Save this as the last ID
311                  * allocated and update the global ID mask.
312                  */
313                 AcpiGbl_OwnerIdMask[j] |= (1 << k);
314
315                 AcpiGbl_LastOwnerIdIndex = (UINT8) j;
316                 AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
317
318                 /*
319                  * Construct encoded ID from the index and bit position
320                  *
321                  * Note: Last [j].k (bit 255) is never used and is marked
322                  * permanently allocated (prevents +1 overflow)
323                  */
324                 *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
325
326                 ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
327                     "Allocated OwnerId: %2.2X\n", (unsigned int) *OwnerId));
328                 goto Exit;
329             }
330         }
331
332         AcpiGbl_NextOwnerIdOffset = 0;
333     }
334
335     /*
336      * All OwnerIds have been allocated. This typically should
337      * not happen since the IDs are reused after deallocation. The IDs are
338      * allocated upon table load (one per table) and method execution, and
339      * they are released when a table is unloaded or a method completes
340      * execution.
341      *
342      * If this error happens, there may be very deep nesting of invoked control
343      * methods, or there may be a bug where the IDs are not released.
344      */
345     Status = AE_OWNER_ID_LIMIT;
346     ACPI_ERROR ((AE_INFO,
347         "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
348
349 Exit:
350     (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
351     return_ACPI_STATUS (Status);
352 }
353
354
355 /*******************************************************************************
356  *
357  * FUNCTION:    AcpiUtReleaseOwnerId
358  *
359  * PARAMETERS:  OwnerIdPtr          - Pointer to a previously allocated OwnerID
360  *
361  * RETURN:      None. No error is returned because we are either exiting a
362  *              control method or unloading a table. Either way, we would
363  *              ignore any error anyway.
364  *
365  * DESCRIPTION: Release a table or method owner ID.  Valid IDs are 1 - 255
366  *
367  ******************************************************************************/
368
369 void
370 AcpiUtReleaseOwnerId (
371     ACPI_OWNER_ID           *OwnerIdPtr)
372 {
373     ACPI_OWNER_ID           OwnerId = *OwnerIdPtr;
374     ACPI_STATUS             Status;
375     ACPI_NATIVE_UINT        Index;
376     UINT32                  Bit;
377
378
379     ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
380
381
382     /* Always clear the input OwnerId (zero is an invalid ID) */
383
384     *OwnerIdPtr = 0;
385
386     /* Zero is not a valid OwnerID */
387
388     if (OwnerId == 0)
389     {
390         ACPI_ERROR ((AE_INFO, "Invalid OwnerId: %2.2X", OwnerId));
391         return_VOID;
392     }
393
394     /* Mutex for the global ID mask */
395
396     Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
397     if (ACPI_FAILURE (Status))
398     {
399         return_VOID;
400     }
401
402     /* Normalize the ID to zero */
403
404     OwnerId--;
405
406     /* Decode ID to index/offset pair */
407
408     Index = ACPI_DIV_32 (OwnerId);
409     Bit = 1 << ACPI_MOD_32 (OwnerId);
410
411     /* Free the owner ID only if it is valid */
412
413     if (AcpiGbl_OwnerIdMask[Index] & Bit)
414     {
415         AcpiGbl_OwnerIdMask[Index] ^= Bit;
416     }
417     else
418     {
419         ACPI_ERROR ((AE_INFO,
420             "Release of non-allocated OwnerId: %2.2X", OwnerId + 1));
421     }
422
423     (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
424     return_VOID;
425 }
426
427
428 /*******************************************************************************
429  *
430  * FUNCTION:    AcpiUtStrupr (strupr)
431  *
432  * PARAMETERS:  SrcString       - The source string to convert
433  *
434  * RETURN:      None
435  *
436  * DESCRIPTION: Convert string to uppercase
437  *
438  * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
439  *
440  ******************************************************************************/
441
442 void
443 AcpiUtStrupr (
444     char                    *SrcString)
445 {
446     char                    *String;
447
448
449     ACPI_FUNCTION_ENTRY ();
450
451
452     if (!SrcString)
453     {
454         return;
455     }
456
457     /* Walk entire string, uppercasing the letters */
458
459     for (String = SrcString; *String; String++)
460     {
461         *String = (char) ACPI_TOUPPER (*String);
462     }
463
464     return;
465 }
466
467
468 /*******************************************************************************
469  *
470  * FUNCTION:    AcpiUtPrintString
471  *
472  * PARAMETERS:  String          - Null terminated ASCII string
473  *              MaxLength       - Maximum output length
474  *
475  * RETURN:      None
476  *
477  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
478  *              sequences.
479  *
480  ******************************************************************************/
481
482 void
483 AcpiUtPrintString (
484     char                    *String,
485     UINT8                   MaxLength)
486 {
487     UINT32                  i;
488
489
490     if (!String)
491     {
492         AcpiOsPrintf ("<\"NULL STRING PTR\">");
493         return;
494     }
495
496     AcpiOsPrintf ("\"");
497     for (i = 0; String[i] && (i < MaxLength); i++)
498     {
499         /* Escape sequences */
500
501         switch (String[i])
502         {
503         case 0x07:
504             AcpiOsPrintf ("\\a");       /* BELL */
505             break;
506
507         case 0x08:
508             AcpiOsPrintf ("\\b");       /* BACKSPACE */
509             break;
510
511         case 0x0C:
512             AcpiOsPrintf ("\\f");       /* FORMFEED */
513             break;
514
515         case 0x0A:
516             AcpiOsPrintf ("\\n");       /* LINEFEED */
517             break;
518
519         case 0x0D:
520             AcpiOsPrintf ("\\r");       /* CARRIAGE RETURN*/
521             break;
522
523         case 0x09:
524             AcpiOsPrintf ("\\t");       /* HORIZONTAL TAB */
525             break;
526
527         case 0x0B:
528             AcpiOsPrintf ("\\v");       /* VERTICAL TAB */
529             break;
530
531         case '\'':                      /* Single Quote */
532         case '\"':                      /* Double Quote */
533         case '\\':                      /* Backslash */
534             AcpiOsPrintf ("\\%c", (int) String[i]);
535             break;
536
537         default:
538
539             /* Check for printable character or hex escape */
540
541             if (ACPI_IS_PRINT (String[i]))
542             {
543                 /* This is a normal character */
544
545                 AcpiOsPrintf ("%c", (int) String[i]);
546             }
547             else
548             {
549                 /* All others will be Hex escapes */
550
551                 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
552             }
553             break;
554         }
555     }
556     AcpiOsPrintf ("\"");
557
558     if (i == MaxLength && String[i])
559     {
560         AcpiOsPrintf ("...");
561     }
562 }
563
564
565 /*******************************************************************************
566  *
567  * FUNCTION:    AcpiUtDwordByteSwap
568  *
569  * PARAMETERS:  Value           - Value to be converted
570  *
571  * RETURN:      UINT32 integer with bytes swapped
572  *
573  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
574  *
575  ******************************************************************************/
576
577 UINT32
578 AcpiUtDwordByteSwap (
579     UINT32                  Value)
580 {
581     union
582     {
583         UINT32              Value;
584         UINT8               Bytes[4];
585     } Out;
586     union
587     {
588         UINT32              Value;
589         UINT8               Bytes[4];
590     } In;
591
592
593     ACPI_FUNCTION_ENTRY ();
594
595
596     In.Value = Value;
597
598     Out.Bytes[0] = In.Bytes[3];
599     Out.Bytes[1] = In.Bytes[2];
600     Out.Bytes[2] = In.Bytes[1];
601     Out.Bytes[3] = In.Bytes[0];
602
603     return (Out.Value);
604 }
605
606
607 /*******************************************************************************
608  *
609  * FUNCTION:    AcpiUtSetIntegerWidth
610  *
611  * PARAMETERS:  Revision            From DSDT header
612  *
613  * RETURN:      None
614  *
615  * DESCRIPTION: Set the global integer bit width based upon the revision
616  *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
617  *              For Revision 2 and above, Integers are 64 bits.  Yes, this
618  *              makes a difference.
619  *
620  ******************************************************************************/
621
622 void
623 AcpiUtSetIntegerWidth (
624     UINT8                   Revision)
625 {
626
627     if (Revision < 2)
628     {
629         /* 32-bit case */
630
631         AcpiGbl_IntegerBitWidth    = 32;
632         AcpiGbl_IntegerNybbleWidth = 8;
633         AcpiGbl_IntegerByteWidth   = 4;
634     }
635     else
636     {
637         /* 64-bit case (ACPI 2.0+) */
638
639         AcpiGbl_IntegerBitWidth    = 64;
640         AcpiGbl_IntegerNybbleWidth = 16;
641         AcpiGbl_IntegerByteWidth   = 8;
642     }
643 }
644
645
646 #ifdef ACPI_DEBUG_OUTPUT
647 /*******************************************************************************
648  *
649  * FUNCTION:    AcpiUtDisplayInitPathname
650  *
651  * PARAMETERS:  Type                - Object type of the node
652  *              ObjHandle           - Handle whose pathname will be displayed
653  *              Path                - Additional path string to be appended.
654  *                                      (NULL if no extra path)
655  *
656  * RETURN:      ACPI_STATUS
657  *
658  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
659  *
660  ******************************************************************************/
661
662 void
663 AcpiUtDisplayInitPathname (
664     UINT8                   Type,
665     ACPI_NAMESPACE_NODE     *ObjHandle,
666     char                    *Path)
667 {
668     ACPI_STATUS             Status;
669     ACPI_BUFFER             Buffer;
670
671
672     ACPI_FUNCTION_ENTRY ();
673
674
675     /* Only print the path if the appropriate debug level is enabled */
676
677     if (!(AcpiDbgLevel & ACPI_LV_INIT_NAMES))
678     {
679         return;
680     }
681
682     /* Get the full pathname to the node */
683
684     Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
685     Status = AcpiNsHandleToPathname (ObjHandle, &Buffer);
686     if (ACPI_FAILURE (Status))
687     {
688         return;
689     }
690
691     /* Print what we're doing */
692
693     switch (Type)
694     {
695     case ACPI_TYPE_METHOD:
696         AcpiOsPrintf ("Executing    ");
697         break;
698
699     default:
700         AcpiOsPrintf ("Initializing ");
701         break;
702     }
703
704     /* Print the object type and pathname */
705
706     AcpiOsPrintf ("%-12s  %s",
707         AcpiUtGetTypeName (Type), (char *) Buffer.Pointer);
708
709     /* Extra path is used to append names like _STA, _INI, etc. */
710
711     if (Path)
712     {
713         AcpiOsPrintf (".%s", Path);
714     }
715     AcpiOsPrintf ("\n");
716
717     ACPI_FREE (Buffer.Pointer);
718 }
719 #endif
720
721
722 /*******************************************************************************
723  *
724  * FUNCTION:    AcpiUtValidAcpiChar
725  *
726  * PARAMETERS:  Char            - The character to be examined
727  *              Position        - Byte position (0-3)
728  *
729  * RETURN:      TRUE if the character is valid, FALSE otherwise
730  *
731  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
732  *              1) Upper case alpha
733  *              2) numeric
734  *              3) underscore
735  *
736  *              We allow a '!' as the last character because of the ASF! table
737  *
738  ******************************************************************************/
739
740 BOOLEAN
741 AcpiUtValidAcpiChar (
742     char                    Character,
743     ACPI_NATIVE_UINT        Position)
744 {
745
746     if (!((Character >= 'A' && Character <= 'Z') ||
747           (Character >= '0' && Character <= '9') ||
748           (Character == '_')))
749     {
750         /* Allow a '!' in the last position */
751
752         if (Character == '!' && Position == 3)
753         {
754             return (TRUE);
755         }
756
757         return (FALSE);
758     }
759
760     return (TRUE);
761 }
762
763
764 /*******************************************************************************
765  *
766  * FUNCTION:    AcpiUtValidAcpiName
767  *
768  * PARAMETERS:  Name            - The name to be examined
769  *
770  * RETURN:      TRUE if the name is valid, FALSE otherwise
771  *
772  * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
773  *              1) Upper case alpha
774  *              2) numeric
775  *              3) underscore
776  *
777  ******************************************************************************/
778
779 BOOLEAN
780 AcpiUtValidAcpiName (
781     UINT32                  Name)
782 {
783     ACPI_NATIVE_UINT        i;
784
785
786     ACPI_FUNCTION_ENTRY ();
787
788
789     for (i = 0; i < ACPI_NAME_SIZE; i++)
790     {
791         if (!AcpiUtValidAcpiChar ((ACPI_CAST_PTR (char, &Name))[i], i))
792         {
793             return (FALSE);
794         }
795     }
796
797     return (TRUE);
798 }
799
800
801 /*******************************************************************************
802  *
803  * FUNCTION:    AcpiUtRepairName
804  *
805  * PARAMETERS:  Name            - The ACPI name to be repaired
806  *
807  * RETURN:      Repaired version of the name
808  *
809  * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
810  *              return the new name.
811  *
812  ******************************************************************************/
813
814 ACPI_NAME
815 AcpiUtRepairName (
816     char                    *Name)
817 {
818     ACPI_NATIVE_UINT        i;
819     char                    NewName[ACPI_NAME_SIZE];
820
821
822     for (i = 0; i < ACPI_NAME_SIZE; i++)
823     {
824         NewName[i] = Name[i];
825
826         /*
827          * Replace a bad character with something printable, yet technically
828          * still invalid. This prevents any collisions with existing "good"
829          * names in the namespace.
830          */
831         if (!AcpiUtValidAcpiChar (Name[i], i))
832         {
833             NewName[i] = '*';
834         }
835     }
836
837     return (*(UINT32 *) NewName);
838 }
839
840
841 /*******************************************************************************
842  *
843  * FUNCTION:    AcpiUtStrtoul64
844  *
845  * PARAMETERS:  String          - Null terminated string
846  *              Base            - Radix of the string: 16 or ACPI_ANY_BASE;
847  *                                ACPI_ANY_BASE means 'in behalf of ToInteger'
848  *              RetInteger      - Where the converted integer is returned
849  *
850  * RETURN:      Status and Converted value
851  *
852  * DESCRIPTION: Convert a string into an unsigned value. Performs either a
853  *              32-bit or 64-bit conversion, depending on the current mode
854  *              of the interpreter.
855  *              NOTE: Does not support Octal strings, not needed.
856  *
857  ******************************************************************************/
858
859 ACPI_STATUS
860 AcpiUtStrtoul64 (
861     char                    *String,
862     UINT32                  Base,
863     ACPI_INTEGER            *RetInteger)
864 {
865     UINT32                  ThisDigit = 0;
866     ACPI_INTEGER            ReturnValue = 0;
867     ACPI_INTEGER            Quotient;
868     ACPI_INTEGER            Dividend;
869     UINT32                  ToIntegerOp = (Base == ACPI_ANY_BASE);
870     UINT32                  Mode32 = (AcpiGbl_IntegerByteWidth == 4);
871     UINT8                   ValidDigits = 0;
872     UINT8                   SignOf0x = 0;
873     UINT8                   Term = 0;
874
875
876     ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
877
878
879     switch (Base)
880     {
881     case ACPI_ANY_BASE:
882     case 16:
883         break;
884
885     default:
886         /* Invalid Base */
887         return_ACPI_STATUS (AE_BAD_PARAMETER);
888     }
889
890     if (!String)
891     {
892         goto ErrorExit;
893     }
894
895     /* Skip over any white space in the buffer */
896
897     while ((*String) && (ACPI_IS_SPACE (*String) || *String == '\t'))
898     {
899         String++;
900     }
901
902     if (ToIntegerOp)
903     {
904         /*
905          * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
906          * We need to determine if it is decimal or hexadecimal.
907          */
908         if ((*String == '0') && (ACPI_TOLOWER (*(String + 1)) == 'x'))
909         {
910             SignOf0x = 1;
911             Base = 16;
912
913             /* Skip over the leading '0x' */
914             String += 2;
915         }
916         else
917         {
918             Base = 10;
919         }
920     }
921
922     /* Any string left? Check that '0x' is not followed by white space. */
923
924     if (!(*String) || ACPI_IS_SPACE (*String) || *String == '\t')
925     {
926         if (ToIntegerOp)
927         {
928             goto ErrorExit;
929         }
930         else
931         {
932             goto AllDone;
933         }
934     }
935
936     /*
937      * Perform a 32-bit or 64-bit conversion, depending upon the current
938      * execution mode of the interpreter
939      */
940     Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
941
942     /* Main loop: convert the string to a 32- or 64-bit integer */
943
944     while (*String)
945     {
946         if (ACPI_IS_DIGIT (*String))
947         {
948             /* Convert ASCII 0-9 to Decimal value */
949
950             ThisDigit = ((UINT8) *String) - '0';
951         }
952         else if (Base == 10)
953         {
954             /* Digit is out of range; possible in ToInteger case only */
955
956             Term = 1;
957         }
958         else
959         {
960             ThisDigit = (UINT8) ACPI_TOUPPER (*String);
961             if (ACPI_IS_XDIGIT ((char) ThisDigit))
962             {
963                 /* Convert ASCII Hex char to value */
964
965                 ThisDigit = ThisDigit - 'A' + 10;
966             }
967             else
968             {
969                 Term = 1;
970             }
971         }
972
973         if (Term)
974         {
975             if (ToIntegerOp)
976             {
977                 goto ErrorExit;
978             }
979             else
980             {
981                 break;
982             }
983         }
984         else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
985         {
986             /* Skip zeros */
987             String++;
988             continue;
989         }
990
991         ValidDigits++;
992
993         if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
994         {
995             /*
996              * This is ToInteger operation case.
997              * No any restrictions for string-to-integer conversion,
998              * see ACPI spec.
999              */
1000             goto ErrorExit;
1001         }
1002
1003         /* Divide the digit into the correct position */
1004
1005         (void) AcpiUtShortDivide ((Dividend - (ACPI_INTEGER) ThisDigit),
1006                     Base, &Quotient, NULL);
1007
1008         if (ReturnValue > Quotient)
1009         {
1010             if (ToIntegerOp)
1011             {
1012                 goto ErrorExit;
1013             }
1014             else
1015             {
1016                 break;
1017             }
1018         }
1019
1020         ReturnValue *= Base;
1021         ReturnValue += ThisDigit;
1022         String++;
1023     }
1024
1025     /* All done, normal exit */
1026
1027 AllDone:
1028
1029     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
1030         ACPI_FORMAT_UINT64 (ReturnValue)));
1031
1032     *RetInteger = ReturnValue;
1033     return_ACPI_STATUS (AE_OK);
1034
1035
1036 ErrorExit:
1037     /* Base was set/validated above */
1038
1039     if (Base == 10)
1040     {
1041         return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
1042     }
1043     else
1044     {
1045         return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
1046     }
1047 }
1048
1049
1050 /*******************************************************************************
1051  *
1052  * FUNCTION:    AcpiUtCreateUpdateStateAndPush
1053  *
1054  * PARAMETERS:  Object          - Object to be added to the new state
1055  *              Action          - Increment/Decrement
1056  *              StateList       - List the state will be added to
1057  *
1058  * RETURN:      Status
1059  *
1060  * DESCRIPTION: Create a new state and push it
1061  *
1062  ******************************************************************************/
1063
1064 ACPI_STATUS
1065 AcpiUtCreateUpdateStateAndPush (
1066     ACPI_OPERAND_OBJECT     *Object,
1067     UINT16                  Action,
1068     ACPI_GENERIC_STATE      **StateList)
1069 {
1070     ACPI_GENERIC_STATE       *State;
1071
1072
1073     ACPI_FUNCTION_ENTRY ();
1074
1075
1076     /* Ignore null objects; these are expected */
1077
1078     if (!Object)
1079     {
1080         return (AE_OK);
1081     }
1082
1083     State = AcpiUtCreateUpdateState (Object, Action);
1084     if (!State)
1085     {
1086         return (AE_NO_MEMORY);
1087     }
1088
1089     AcpiUtPushGenericState (StateList, State);
1090     return (AE_OK);
1091 }
1092
1093
1094 /*******************************************************************************
1095  *
1096  * FUNCTION:    AcpiUtWalkPackageTree
1097  *
1098  * PARAMETERS:  SourceObject        - The package to walk
1099  *              TargetObject        - Target object (if package is being copied)
1100  *              WalkCallback        - Called once for each package element
1101  *              Context             - Passed to the callback function
1102  *
1103  * RETURN:      Status
1104  *
1105  * DESCRIPTION: Walk through a package
1106  *
1107  ******************************************************************************/
1108
1109 ACPI_STATUS
1110 AcpiUtWalkPackageTree (
1111     ACPI_OPERAND_OBJECT     *SourceObject,
1112     void                    *TargetObject,
1113     ACPI_PKG_CALLBACK       WalkCallback,
1114     void                    *Context)
1115 {
1116     ACPI_STATUS             Status = AE_OK;
1117     ACPI_GENERIC_STATE      *StateList = NULL;
1118     ACPI_GENERIC_STATE      *State;
1119     UINT32                  ThisIndex;
1120     ACPI_OPERAND_OBJECT     *ThisSourceObj;
1121
1122
1123     ACPI_FUNCTION_TRACE (UtWalkPackageTree);
1124
1125
1126     State = AcpiUtCreatePkgState (SourceObject, TargetObject, 0);
1127     if (!State)
1128     {
1129         return_ACPI_STATUS (AE_NO_MEMORY);
1130     }
1131
1132     while (State)
1133     {
1134         /* Get one element of the package */
1135
1136         ThisIndex     = State->Pkg.Index;
1137         ThisSourceObj = (ACPI_OPERAND_OBJECT *)
1138                         State->Pkg.SourceObject->Package.Elements[ThisIndex];
1139
1140         /*
1141          * Check for:
1142          * 1) An uninitialized package element.  It is completely
1143          *    legal to declare a package and leave it uninitialized
1144          * 2) Not an internal object - can be a namespace node instead
1145          * 3) Any type other than a package.  Packages are handled in else
1146          *    case below.
1147          */
1148         if ((!ThisSourceObj) ||
1149             (ACPI_GET_DESCRIPTOR_TYPE (ThisSourceObj) != ACPI_DESC_TYPE_OPERAND) ||
1150             (ACPI_GET_OBJECT_TYPE (ThisSourceObj) != ACPI_TYPE_PACKAGE))
1151         {
1152             Status = WalkCallback (ACPI_COPY_TYPE_SIMPLE, ThisSourceObj,
1153                                     State, Context);
1154             if (ACPI_FAILURE (Status))
1155             {
1156                 return_ACPI_STATUS (Status);
1157             }
1158
1159             State->Pkg.Index++;
1160             while (State->Pkg.Index >= State->Pkg.SourceObject->Package.Count)
1161             {
1162                 /*
1163                  * We've handled all of the objects at this level,  This means
1164                  * that we have just completed a package.  That package may
1165                  * have contained one or more packages itself.
1166                  *
1167                  * Delete this state and pop the previous state (package).
1168                  */
1169                 AcpiUtDeleteGenericState (State);
1170                 State = AcpiUtPopGenericState (&StateList);
1171
1172                 /* Finished when there are no more states */
1173
1174                 if (!State)
1175                 {
1176                     /*
1177                      * We have handled all of the objects in the top level
1178                      * package just add the length of the package objects
1179                      * and exit
1180                      */
1181                     return_ACPI_STATUS (AE_OK);
1182                 }
1183
1184                 /*
1185                  * Go back up a level and move the index past the just
1186                  * completed package object.
1187                  */
1188                 State->Pkg.Index++;
1189             }
1190         }
1191         else
1192         {
1193             /* This is a subobject of type package */
1194
1195             Status = WalkCallback (ACPI_COPY_TYPE_PACKAGE, ThisSourceObj,
1196                                         State, Context);
1197             if (ACPI_FAILURE (Status))
1198             {
1199                 return_ACPI_STATUS (Status);
1200             }
1201
1202             /*
1203              * Push the current state and create a new one
1204              * The callback above returned a new target package object.
1205              */
1206             AcpiUtPushGenericState (&StateList, State);
1207             State = AcpiUtCreatePkgState (ThisSourceObj,
1208                                             State->Pkg.ThisTargetObj, 0);
1209             if (!State)
1210             {
1211                 return_ACPI_STATUS (AE_NO_MEMORY);
1212             }
1213         }
1214     }
1215
1216     /* We should never get here */
1217
1218     return_ACPI_STATUS (AE_AML_INTERNAL);
1219 }
1220
1221
1222 /*******************************************************************************
1223  *
1224  * FUNCTION:    AcpiUtError, AcpiUtWarning, AcpiUtInfo
1225  *
1226  * PARAMETERS:  ModuleName          - Caller's module name (for error output)
1227  *              LineNumber          - Caller's line number (for error output)
1228  *              Format              - Printf format string + additional args
1229  *
1230  * RETURN:      None
1231  *
1232  * DESCRIPTION: Print message with module/line/version info
1233  *
1234  ******************************************************************************/
1235
1236 void  ACPI_INTERNAL_VAR_XFACE
1237 AcpiUtError (
1238     char                    *ModuleName,
1239     UINT32                  LineNumber,
1240     char                    *Format,
1241     ...)
1242 {
1243     va_list                 args;
1244
1245
1246     AcpiOsPrintf ("ACPI Error (%s-%04d): ", ModuleName, LineNumber);
1247
1248     va_start (args, Format);
1249     AcpiOsVprintf (Format, args);
1250     AcpiOsPrintf (" [%X]\n", ACPI_CA_VERSION);
1251 }
1252
1253 void  ACPI_INTERNAL_VAR_XFACE
1254 AcpiUtException (
1255     char                    *ModuleName,
1256     UINT32                  LineNumber,
1257     ACPI_STATUS             Status,
1258     char                    *Format,
1259     ...)
1260 {
1261     va_list                 args;
1262
1263
1264     AcpiOsPrintf ("ACPI Exception (%s-%04d): %s, ", ModuleName, LineNumber,
1265         AcpiFormatException (Status));
1266
1267     va_start (args, Format);
1268     AcpiOsVprintf (Format, args);
1269     AcpiOsPrintf (" [%X]\n", ACPI_CA_VERSION);
1270 }
1271
1272 void  ACPI_INTERNAL_VAR_XFACE
1273 AcpiUtWarning (
1274     char                    *ModuleName,
1275     UINT32                  LineNumber,
1276     char                    *Format,
1277     ...)
1278 {
1279     va_list                 args;
1280
1281
1282     AcpiOsPrintf ("ACPI Warning (%s-%04d): ", ModuleName, LineNumber);
1283
1284     va_start (args, Format);
1285     AcpiOsVprintf (Format, args);
1286     AcpiOsPrintf (" [%X]\n", ACPI_CA_VERSION);
1287 }
1288
1289 void  ACPI_INTERNAL_VAR_XFACE
1290 AcpiUtInfo (
1291     char                    *ModuleName,
1292     UINT32                  LineNumber,
1293     char                    *Format,
1294     ...)
1295 {
1296     va_list                 args;
1297
1298
1299     /*
1300      * Removed ModuleName, LineNumber, and acpica version, not needed
1301      * for info output
1302      */
1303     AcpiOsPrintf ("ACPI: ");
1304
1305     va_start (args, Format);
1306     AcpiOsVprintf (Format, args);
1307     AcpiOsPrintf ("\n");
1308 }
1309