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