vserver 1.9.5.x5
[linux-2.6.git] / drivers / acpi / utilities / utmisc.c
1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47
48
49 #define _COMPONENT          ACPI_UTILITIES
50          ACPI_MODULE_NAME    ("utmisc")
51
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_ut_print_string
56  *
57  * PARAMETERS:  String          - Null terminated ASCII string
58  *
59  * RETURN:      None
60  *
61  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
62  *              sequences.
63  *
64  ******************************************************************************/
65
66 void
67 acpi_ut_print_string (
68         char                            *string,
69         u8                              max_length)
70 {
71         u32                             i;
72
73
74         if (!string) {
75                 acpi_os_printf ("<\"NULL STRING PTR\">");
76                 return;
77         }
78
79         acpi_os_printf ("\"");
80         for (i = 0; string[i] && (i < max_length); i++) {
81                 /* Escape sequences */
82
83                 switch (string[i]) {
84                 case 0x07:
85                         acpi_os_printf ("\\a");      /* BELL */
86                         break;
87
88                 case 0x08:
89                         acpi_os_printf ("\\b");     /* BACKSPACE */
90                         break;
91
92                 case 0x0C:
93                         acpi_os_printf ("\\f");     /* FORMFEED */
94                         break;
95
96                 case 0x0A:
97                         acpi_os_printf ("\\n");     /* LINEFEED */
98                         break;
99
100                 case 0x0D:
101                         acpi_os_printf ("\\r");     /* CARRIAGE RETURN*/
102                         break;
103
104                 case 0x09:
105                         acpi_os_printf ("\\t");     /* HORIZONTAL TAB */
106                         break;
107
108                 case 0x0B:
109                         acpi_os_printf ("\\v");     /* VERTICAL TAB */
110                         break;
111
112                 case '\'':                      /* Single Quote */
113                 case '\"':                      /* Double Quote */
114                 case '\\':                      /* Backslash */
115                         acpi_os_printf ("\\%c", (int) string[i]);
116                         break;
117
118                 default:
119
120                         /* Check for printable character or hex escape */
121
122                         if (ACPI_IS_PRINT (string[i]))
123                         {
124                                 /* This is a normal character */
125
126                                 acpi_os_printf ("%c", (int) string[i]);
127                         }
128                         else
129                         {
130                                 /* All others will be Hex escapes */
131
132                                 acpi_os_printf ("\\x%2.2X", (s32) string[i]);
133                         }
134                         break;
135                 }
136         }
137         acpi_os_printf ("\"");
138
139         if (i == max_length && string[i]) {
140                 acpi_os_printf ("...");
141         }
142 }
143
144
145 /*******************************************************************************
146  *
147  * FUNCTION:    acpi_ut_dword_byte_swap
148  *
149  * PARAMETERS:  Value           - Value to be converted
150  *
151  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
152  *
153  ******************************************************************************/
154
155 u32
156 acpi_ut_dword_byte_swap (
157         u32                             value)
158 {
159         union {
160                 u32                         value;
161                 u8                          bytes[4];
162         } out;
163
164         union {
165                 u32                         value;
166                 u8                          bytes[4];
167         } in;
168
169
170         ACPI_FUNCTION_ENTRY ();
171
172
173         in.value = value;
174
175         out.bytes[0] = in.bytes[3];
176         out.bytes[1] = in.bytes[2];
177         out.bytes[2] = in.bytes[1];
178         out.bytes[3] = in.bytes[0];
179
180         return (out.value);
181 }
182
183
184 /*******************************************************************************
185  *
186  * FUNCTION:    acpi_ut_set_integer_width
187  *
188  * PARAMETERS:  Revision            From DSDT header
189  *
190  * RETURN:      None
191  *
192  * DESCRIPTION: Set the global integer bit width based upon the revision
193  *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
194  *              For Revision 2 and above, Integers are 64 bits.  Yes, this
195  *              makes a difference.
196  *
197  ******************************************************************************/
198
199 void
200 acpi_ut_set_integer_width (
201         u8                              revision)
202 {
203
204         if (revision <= 1) {
205                 acpi_gbl_integer_bit_width = 32;
206                 acpi_gbl_integer_nybble_width = 8;
207                 acpi_gbl_integer_byte_width = 4;
208         }
209         else {
210                 acpi_gbl_integer_bit_width = 64;
211                 acpi_gbl_integer_nybble_width = 16;
212                 acpi_gbl_integer_byte_width = 8;
213         }
214 }
215
216
217 #ifdef ACPI_DEBUG_OUTPUT
218 /*******************************************************************************
219  *
220  * FUNCTION:    acpi_ut_display_init_pathname
221  *
222  * PARAMETERS:  obj_handle          - Handle whose pathname will be displayed
223  *              Path                - Additional path string to be appended.
224  *                                      (NULL if no extra path)
225  *
226  * RETURN:      acpi_status
227  *
228  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
229  *
230  ******************************************************************************/
231
232 void
233 acpi_ut_display_init_pathname (
234         u8                              type,
235         struct acpi_namespace_node      *obj_handle,
236         char                            *path)
237 {
238         acpi_status                     status;
239         struct acpi_buffer              buffer;
240
241
242         ACPI_FUNCTION_ENTRY ();
243
244
245         /* Only print the path if the appropriate debug level is enabled */
246
247         if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
248                 return;
249         }
250
251         /* Get the full pathname to the node */
252
253         buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
254         status = acpi_ns_handle_to_pathname (obj_handle, &buffer);
255         if (ACPI_FAILURE (status)) {
256                 return;
257         }
258
259         /* Print what we're doing */
260
261         switch (type) {
262         case ACPI_TYPE_METHOD:
263                 acpi_os_printf ("Executing  ");
264                 break;
265
266         default:
267                 acpi_os_printf ("Initializing ");
268                 break;
269         }
270
271         /* Print the object type and pathname */
272
273         acpi_os_printf ("%-12s %s", acpi_ut_get_type_name (type), (char *) buffer.pointer);
274
275         /* Extra path is used to append names like _STA, _INI, etc. */
276
277         if (path) {
278                 acpi_os_printf (".%s", path);
279         }
280         acpi_os_printf ("\n");
281
282         ACPI_MEM_FREE (buffer.pointer);
283 }
284 #endif
285
286
287 /*******************************************************************************
288  *
289  * FUNCTION:    acpi_ut_valid_acpi_name
290  *
291  * PARAMETERS:  Character           - The character to be examined
292  *
293  * RETURN:      1 if Character may appear in a name, else 0
294  *
295  * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
296  *              1) Upper case alpha
297  *              2) numeric
298  *              3) underscore
299  *
300  ******************************************************************************/
301
302 u8
303 acpi_ut_valid_acpi_name (
304         u32                             name)
305 {
306         char                            *name_ptr = (char *) &name;
307         char                            character;
308         acpi_native_uint                i;
309
310
311         ACPI_FUNCTION_ENTRY ();
312
313
314         for (i = 0; i < ACPI_NAME_SIZE; i++) {
315                 character = *name_ptr;
316                 name_ptr++;
317
318                 if (!((character == '_') ||
319                           (character >= 'A' && character <= 'Z') ||
320                           (character >= '0' && character <= '9'))) {
321                         return (FALSE);
322                 }
323         }
324
325         return (TRUE);
326 }
327
328
329 /*******************************************************************************
330  *
331  * FUNCTION:    acpi_ut_valid_acpi_character
332  *
333  * PARAMETERS:  Character           - The character to be examined
334  *
335  * RETURN:      1 if Character may appear in a name, else 0
336  *
337  * DESCRIPTION: Check for a printable character
338  *
339  ******************************************************************************/
340
341 u8
342 acpi_ut_valid_acpi_character (
343         char                            character)
344 {
345
346         ACPI_FUNCTION_ENTRY ();
347
348         return ((u8)   ((character == '_') ||
349                            (character >= 'A' && character <= 'Z') ||
350                            (character >= '0' && character <= '9')));
351 }
352
353
354 /*******************************************************************************
355  *
356  * FUNCTION:    acpi_ut_strtoul64
357  *
358  * PARAMETERS:  String          - Null terminated string
359  *              Base            - Radix of the string: 10, 16, or ACPI_ANY_BASE
360  *              ret_integer     - Where the converted integer is returned
361  *
362  * RETURN:      Status and Converted value
363  *
364  * DESCRIPTION: Convert a string into an unsigned value.
365  *              NOTE: Does not support Octal strings, not needed.
366  *
367  ******************************************************************************/
368
369 acpi_status
370 acpi_ut_strtoul64 (
371         char                            *string,
372         u32                             base,
373         acpi_integer                    *ret_integer)
374 {
375         u32                             this_digit;
376         acpi_integer                    return_value = 0;
377         acpi_integer                    quotient;
378
379
380         ACPI_FUNCTION_TRACE ("ut_stroul64");
381
382
383         switch (base) {
384         case ACPI_ANY_BASE:
385         case 10:
386         case 16:
387                 break;
388
389         default:
390                 /* Invalid Base */
391                 return_ACPI_STATUS (AE_BAD_PARAMETER);
392         }
393
394         /* Skip over any white space in the buffer */
395
396         while (ACPI_IS_SPACE (*string) || *string == '\t') {
397                 ++string;
398         }
399
400         /*
401          * If the input parameter Base is zero, then we need to
402          * determine if it is decimal or hexadecimal:
403          */
404         if (base == 0) {
405                 if ((*string == '0') &&
406                         (ACPI_TOLOWER (*(++string)) == 'x')) {
407                         base = 16;
408                         ++string;
409                 }
410                 else {
411                         base = 10;
412                 }
413         }
414
415         /*
416          * For hexadecimal base, skip over the leading
417          * 0 or 0x, if they are present.
418          */
419         if (base == 16 &&
420                 *string == '0' &&
421                 ACPI_TOLOWER (*(++string)) == 'x') {
422                 string++;
423         }
424
425         /* Any string left? */
426
427         if (!(*string)) {
428                 goto error_exit;
429         }
430
431         /* Main loop: convert the string to a 64-bit integer */
432
433         while (*string) {
434                 if (ACPI_IS_DIGIT (*string)) {
435                         /* Convert ASCII 0-9 to Decimal value */
436
437                         this_digit = ((u8) *string) - '0';
438                 }
439                 else {
440                         this_digit = (u8) ACPI_TOUPPER (*string);
441                         if (ACPI_IS_UPPER ((char) this_digit)) {
442                                 /* Convert ASCII Hex char to value */
443
444                                 this_digit = this_digit - 'A' + 10;
445                         }
446                         else {
447                                 goto error_exit;
448                         }
449                 }
450
451                 /* Check to see if digit is out of range */
452
453                 if (this_digit >= base) {
454                         goto error_exit;
455                 }
456
457                 /* Divide the digit into the correct position */
458
459                 (void) acpi_ut_short_divide ((ACPI_INTEGER_MAX - (acpi_integer) this_digit),
460                                  base, &quotient, NULL);
461                 if (return_value > quotient) {
462                         goto error_exit;
463                 }
464
465                 return_value *= base;
466                 return_value += this_digit;
467                 ++string;
468         }
469
470         *ret_integer = return_value;
471         return_ACPI_STATUS (AE_OK);
472
473
474 error_exit:
475         /* Base was set/validated above */
476
477         if (base == 10) {
478                 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
479         }
480         else {
481                 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
482         }
483 }
484
485
486 /*******************************************************************************
487  *
488  * FUNCTION:    acpi_ut_strupr
489  *
490  * PARAMETERS:  src_string      - The source string to convert to
491  *
492  * RETURN:      src_string
493  *
494  * DESCRIPTION: Convert string to uppercase
495  *
496  ******************************************************************************/
497 #ifdef ACPI_FUTURE_USAGE
498 char *
499 acpi_ut_strupr (
500         char                            *src_string)
501 {
502         char                            *string;
503
504
505         ACPI_FUNCTION_ENTRY ();
506
507
508         /* Walk entire string, uppercasing the letters */
509
510         for (string = src_string; *string; ) {
511                 *string = (char) ACPI_TOUPPER (*string);
512                 string++;
513         }
514
515         return (src_string);
516 }
517 #endif  /*  ACPI_FUTURE_USAGE  */
518
519
520 /*******************************************************************************
521  *
522  * FUNCTION:    acpi_ut_mutex_initialize
523  *
524  * PARAMETERS:  None.
525  *
526  * RETURN:      Status
527  *
528  * DESCRIPTION: Create the system mutex objects.
529  *
530  ******************************************************************************/
531
532 acpi_status
533 acpi_ut_mutex_initialize (
534         void)
535 {
536         u32                             i;
537         acpi_status                     status;
538
539
540         ACPI_FUNCTION_TRACE ("ut_mutex_initialize");
541
542
543         /*
544          * Create each of the predefined mutex objects
545          */
546         for (i = 0; i < NUM_MUTEX; i++) {
547                 status = acpi_ut_create_mutex (i);
548                 if (ACPI_FAILURE (status)) {
549                         return_ACPI_STATUS (status);
550                 }
551         }
552
553         status = acpi_os_create_lock (&acpi_gbl_gpe_lock);
554         return_ACPI_STATUS (status);
555 }
556
557
558 /*******************************************************************************
559  *
560  * FUNCTION:    acpi_ut_mutex_terminate
561  *
562  * PARAMETERS:  None.
563  *
564  * RETURN:      None.
565  *
566  * DESCRIPTION: Delete all of the system mutex objects.
567  *
568  ******************************************************************************/
569
570 void
571 acpi_ut_mutex_terminate (
572         void)
573 {
574         u32                             i;
575
576
577         ACPI_FUNCTION_TRACE ("ut_mutex_terminate");
578
579
580         /*
581          * Delete each predefined mutex object
582          */
583         for (i = 0; i < NUM_MUTEX; i++) {
584                 (void) acpi_ut_delete_mutex (i);
585         }
586
587         acpi_os_delete_lock (acpi_gbl_gpe_lock);
588         return_VOID;
589 }
590
591
592 /*******************************************************************************
593  *
594  * FUNCTION:    acpi_ut_create_mutex
595  *
596  * PARAMETERS:  mutex_iD        - ID of the mutex to be created
597  *
598  * RETURN:      Status
599  *
600  * DESCRIPTION: Create a mutex object.
601  *
602  ******************************************************************************/
603
604 acpi_status
605 acpi_ut_create_mutex (
606         acpi_mutex_handle               mutex_id)
607 {
608         acpi_status                     status = AE_OK;
609
610
611         ACPI_FUNCTION_TRACE_U32 ("ut_create_mutex", mutex_id);
612
613
614         if (mutex_id > MAX_MUTEX) {
615                 return_ACPI_STATUS (AE_BAD_PARAMETER);
616         }
617
618         if (!acpi_gbl_mutex_info[mutex_id].mutex) {
619                 status = acpi_os_create_semaphore (1, 1,
620                                   &acpi_gbl_mutex_info[mutex_id].mutex);
621                 acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
622                 acpi_gbl_mutex_info[mutex_id].use_count = 0;
623         }
624
625         return_ACPI_STATUS (status);
626 }
627
628
629 /*******************************************************************************
630  *
631  * FUNCTION:    acpi_ut_delete_mutex
632  *
633  * PARAMETERS:  mutex_iD        - ID of the mutex to be deleted
634  *
635  * RETURN:      Status
636  *
637  * DESCRIPTION: Delete a mutex object.
638  *
639  ******************************************************************************/
640
641 acpi_status
642 acpi_ut_delete_mutex (
643         acpi_mutex_handle               mutex_id)
644 {
645         acpi_status                     status;
646
647
648         ACPI_FUNCTION_TRACE_U32 ("ut_delete_mutex", mutex_id);
649
650
651         if (mutex_id > MAX_MUTEX) {
652                 return_ACPI_STATUS (AE_BAD_PARAMETER);
653         }
654
655         status = acpi_os_delete_semaphore (acpi_gbl_mutex_info[mutex_id].mutex);
656
657         acpi_gbl_mutex_info[mutex_id].mutex = NULL;
658         acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
659
660         return_ACPI_STATUS (status);
661 }
662
663
664 /*******************************************************************************
665  *
666  * FUNCTION:    acpi_ut_acquire_mutex
667  *
668  * PARAMETERS:  mutex_iD        - ID of the mutex to be acquired
669  *
670  * RETURN:      Status
671  *
672  * DESCRIPTION: Acquire a mutex object.
673  *
674  ******************************************************************************/
675
676 acpi_status
677 acpi_ut_acquire_mutex (
678         acpi_mutex_handle               mutex_id)
679 {
680         acpi_status                     status;
681         u32                             this_thread_id;
682
683
684         ACPI_FUNCTION_NAME ("ut_acquire_mutex");
685
686
687         if (mutex_id > MAX_MUTEX) {
688                 return (AE_BAD_PARAMETER);
689         }
690
691         this_thread_id = acpi_os_get_thread_id ();
692
693 #ifdef ACPI_MUTEX_DEBUG
694         {
695                 u32                             i;
696                 /*
697                  * Mutex debug code, for internal debugging only.
698                  *
699                  * Deadlock prevention.  Check if this thread owns any mutexes of value
700                  * greater than or equal to this one.  If so, the thread has violated
701                  * the mutex ordering rule.  This indicates a coding error somewhere in
702                  * the ACPI subsystem code.
703                  */
704                 for (i = mutex_id; i < MAX_MUTEX; i++) {
705                         if (acpi_gbl_mutex_info[i].owner_id == this_thread_id) {
706                                 if (i == mutex_id) {
707                                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
708                                                         "Mutex [%s] already acquired by this thread [%X]\n",
709                                                         acpi_ut_get_mutex_name (mutex_id), this_thread_id));
710
711                                         return (AE_ALREADY_ACQUIRED);
712                                 }
713
714                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
715                                                 "Invalid acquire order: Thread %X owns [%s], wants [%s]\n",
716                                                 this_thread_id, acpi_ut_get_mutex_name (i),
717                                                 acpi_ut_get_mutex_name (mutex_id)));
718
719                                 return (AE_ACQUIRE_DEADLOCK);
720                         }
721                 }
722         }
723 #endif
724
725         ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
726                          "Thread %X attempting to acquire Mutex [%s]\n",
727                          this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
728
729         status = acpi_os_wait_semaphore (acpi_gbl_mutex_info[mutex_id].mutex,
730                            1, ACPI_WAIT_FOREVER);
731         if (ACPI_SUCCESS (status)) {
732                 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X acquired Mutex [%s]\n",
733                                  this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
734
735                 acpi_gbl_mutex_info[mutex_id].use_count++;
736                 acpi_gbl_mutex_info[mutex_id].owner_id = this_thread_id;
737         }
738         else {
739                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not acquire Mutex [%s] %s\n",
740                                  this_thread_id, acpi_ut_get_mutex_name (mutex_id),
741                                  acpi_format_exception (status)));
742         }
743
744         return (status);
745 }
746
747
748 /*******************************************************************************
749  *
750  * FUNCTION:    acpi_ut_release_mutex
751  *
752  * PARAMETERS:  mutex_iD        - ID of the mutex to be released
753  *
754  * RETURN:      Status
755  *
756  * DESCRIPTION: Release a mutex object.
757  *
758  ******************************************************************************/
759
760 acpi_status
761 acpi_ut_release_mutex (
762         acpi_mutex_handle               mutex_id)
763 {
764         acpi_status                     status;
765         u32                             i;
766         u32                             this_thread_id;
767
768
769         ACPI_FUNCTION_NAME ("ut_release_mutex");
770
771
772         this_thread_id = acpi_os_get_thread_id ();
773         ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
774                 "Thread %X releasing Mutex [%s]\n", this_thread_id,
775                 acpi_ut_get_mutex_name (mutex_id)));
776
777         if (mutex_id > MAX_MUTEX) {
778                 return (AE_BAD_PARAMETER);
779         }
780
781         /*
782          * Mutex must be acquired in order to release it!
783          */
784         if (acpi_gbl_mutex_info[mutex_id].owner_id == ACPI_MUTEX_NOT_ACQUIRED) {
785                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
786                                 "Mutex [%s] is not acquired, cannot release\n",
787                                 acpi_ut_get_mutex_name (mutex_id)));
788
789                 return (AE_NOT_ACQUIRED);
790         }
791
792         /*
793          * Deadlock prevention.  Check if this thread owns any mutexes of value
794          * greater than this one.  If so, the thread has violated the mutex
795          * ordering rule.  This indicates a coding error somewhere in
796          * the ACPI subsystem code.
797          */
798         for (i = mutex_id; i < MAX_MUTEX; i++) {
799                 if (acpi_gbl_mutex_info[i].owner_id == this_thread_id) {
800                         if (i == mutex_id) {
801                                 continue;
802                         }
803
804                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
805                                         "Invalid release order: owns [%s], releasing [%s]\n",
806                                         acpi_ut_get_mutex_name (i), acpi_ut_get_mutex_name (mutex_id)));
807
808                         return (AE_RELEASE_DEADLOCK);
809                 }
810         }
811
812         /* Mark unlocked FIRST */
813
814         acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
815
816         status = acpi_os_signal_semaphore (acpi_gbl_mutex_info[mutex_id].mutex, 1);
817
818         if (ACPI_FAILURE (status)) {
819                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Thread %X could not release Mutex [%s] %s\n",
820                                  this_thread_id, acpi_ut_get_mutex_name (mutex_id),
821                                  acpi_format_exception (status)));
822         }
823         else {
824                 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X released Mutex [%s]\n",
825                                  this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
826         }
827
828         return (status);
829 }
830
831
832 /*******************************************************************************
833  *
834  * FUNCTION:    acpi_ut_create_update_state_and_push
835  *
836  * PARAMETERS:  *Object         - Object to be added to the new state
837  *              Action          - Increment/Decrement
838  *              state_list      - List the state will be added to
839  *
840  * RETURN:      None
841  *
842  * DESCRIPTION: Create a new state and push it
843  *
844  ******************************************************************************/
845
846 acpi_status
847 acpi_ut_create_update_state_and_push (
848         union acpi_operand_object       *object,
849         u16                             action,
850         union acpi_generic_state        **state_list)
851 {
852         union acpi_generic_state         *state;
853
854
855         ACPI_FUNCTION_ENTRY ();
856
857
858         /* Ignore null objects; these are expected */
859
860         if (!object) {
861                 return (AE_OK);
862         }
863
864         state = acpi_ut_create_update_state (object, action);
865         if (!state) {
866                 return (AE_NO_MEMORY);
867         }
868
869         acpi_ut_push_generic_state (state_list, state);
870         return (AE_OK);
871 }
872
873
874 /*******************************************************************************
875  *
876  * FUNCTION:    acpi_ut_create_pkg_state_and_push
877  *
878  * PARAMETERS:  *Object         - Object to be added to the new state
879  *              Action          - Increment/Decrement
880  *              state_list      - List the state will be added to
881  *
882  * RETURN:      None
883  *
884  * DESCRIPTION: Create a new state and push it
885  *
886  ******************************************************************************/
887 #ifdef ACPI_FUTURE_USAGE
888 acpi_status
889 acpi_ut_create_pkg_state_and_push (
890         void                            *internal_object,
891         void                            *external_object,
892         u16                             index,
893         union acpi_generic_state        **state_list)
894 {
895         union acpi_generic_state         *state;
896
897
898         ACPI_FUNCTION_ENTRY ();
899
900
901         state = acpi_ut_create_pkg_state (internal_object, external_object, index);
902         if (!state) {
903                 return (AE_NO_MEMORY);
904         }
905
906         acpi_ut_push_generic_state (state_list, state);
907         return (AE_OK);
908 }
909 #endif  /*  ACPI_FUTURE_USAGE  */
910
911 /*******************************************************************************
912  *
913  * FUNCTION:    acpi_ut_push_generic_state
914  *
915  * PARAMETERS:  list_head           - Head of the state stack
916  *              State               - State object to push
917  *
918  * RETURN:      Status
919  *
920  * DESCRIPTION: Push a state object onto a state stack
921  *
922  ******************************************************************************/
923
924 void
925 acpi_ut_push_generic_state (
926         union acpi_generic_state        **list_head,
927         union acpi_generic_state        *state)
928 {
929         ACPI_FUNCTION_TRACE ("ut_push_generic_state");
930
931
932         /* Push the state object onto the front of the list (stack) */
933
934         state->common.next = *list_head;
935         *list_head = state;
936
937         return_VOID;
938 }
939
940
941 /*******************************************************************************
942  *
943  * FUNCTION:    acpi_ut_pop_generic_state
944  *
945  * PARAMETERS:  list_head           - Head of the state stack
946  *
947  * RETURN:      Status
948  *
949  * DESCRIPTION: Pop a state object from a state stack
950  *
951  ******************************************************************************/
952
953 union acpi_generic_state *
954 acpi_ut_pop_generic_state (
955         union acpi_generic_state        **list_head)
956 {
957         union acpi_generic_state        *state;
958
959
960         ACPI_FUNCTION_TRACE ("ut_pop_generic_state");
961
962
963         /* Remove the state object at the head of the list (stack) */
964
965         state = *list_head;
966         if (state) {
967                 /* Update the list head */
968
969                 *list_head = state->common.next;
970         }
971
972         return_PTR (state);
973 }
974
975
976 /*******************************************************************************
977  *
978  * FUNCTION:    acpi_ut_create_generic_state
979  *
980  * PARAMETERS:  None
981  *
982  * RETURN:      Status
983  *
984  * DESCRIPTION: Create a generic state object.  Attempt to obtain one from
985  *              the global state cache;  If none available, create a new one.
986  *
987  ******************************************************************************/
988
989 union acpi_generic_state *
990 acpi_ut_create_generic_state (void)
991 {
992         union acpi_generic_state        *state;
993
994
995         ACPI_FUNCTION_ENTRY ();
996
997
998         state = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_STATE);
999
1000         /* Initialize */
1001
1002         if (state) {
1003                 state->common.data_type = ACPI_DESC_TYPE_STATE;
1004         }
1005
1006         return (state);
1007 }
1008
1009
1010 /*******************************************************************************
1011  *
1012  * FUNCTION:    acpi_ut_create_thread_state
1013  *
1014  * PARAMETERS:  None
1015  *
1016  * RETURN:      Thread State
1017  *
1018  * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
1019  *              to track per-thread info during method execution
1020  *
1021  ******************************************************************************/
1022
1023 struct acpi_thread_state *
1024 acpi_ut_create_thread_state (
1025         void)
1026 {
1027         union acpi_generic_state        *state;
1028
1029
1030         ACPI_FUNCTION_TRACE ("ut_create_thread_state");
1031
1032
1033         /* Create the generic state object */
1034
1035         state = acpi_ut_create_generic_state ();
1036         if (!state) {
1037                 return_PTR (NULL);
1038         }
1039
1040         /* Init fields specific to the update struct */
1041
1042         state->common.data_type = ACPI_DESC_TYPE_STATE_THREAD;
1043         state->thread.thread_id = acpi_os_get_thread_id ();
1044
1045         return_PTR ((struct acpi_thread_state *) state);
1046 }
1047
1048
1049 /*******************************************************************************
1050  *
1051  * FUNCTION:    acpi_ut_create_update_state
1052  *
1053  * PARAMETERS:  Object              - Initial Object to be installed in the
1054  *                                    state
1055  *              Action              - Update action to be performed
1056  *
1057  * RETURN:      Status
1058  *
1059  * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
1060  *              to update reference counts and delete complex objects such
1061  *              as packages.
1062  *
1063  ******************************************************************************/
1064
1065 union acpi_generic_state *
1066 acpi_ut_create_update_state (
1067         union acpi_operand_object       *object,
1068         u16                             action)
1069 {
1070         union acpi_generic_state        *state;
1071
1072
1073         ACPI_FUNCTION_TRACE_PTR ("ut_create_update_state", object);
1074
1075
1076         /* Create the generic state object */
1077
1078         state = acpi_ut_create_generic_state ();
1079         if (!state) {
1080                 return_PTR (NULL);
1081         }
1082
1083         /* Init fields specific to the update struct */
1084
1085         state->common.data_type = ACPI_DESC_TYPE_STATE_UPDATE;
1086         state->update.object = object;
1087         state->update.value  = action;
1088
1089         return_PTR (state);
1090 }
1091
1092
1093 /*******************************************************************************
1094  *
1095  * FUNCTION:    acpi_ut_create_pkg_state
1096  *
1097  * PARAMETERS:  Object              - Initial Object to be installed in the
1098  *                                    state
1099  *              Action              - Update action to be performed
1100  *
1101  * RETURN:      Status
1102  *
1103  * DESCRIPTION: Create a "Package State"
1104  *
1105  ******************************************************************************/
1106
1107 union acpi_generic_state *
1108 acpi_ut_create_pkg_state (
1109         void                            *internal_object,
1110         void                            *external_object,
1111         u16                             index)
1112 {
1113         union acpi_generic_state        *state;
1114
1115
1116         ACPI_FUNCTION_TRACE_PTR ("ut_create_pkg_state", internal_object);
1117
1118
1119         /* Create the generic state object */
1120
1121         state = acpi_ut_create_generic_state ();
1122         if (!state) {
1123                 return_PTR (NULL);
1124         }
1125
1126         /* Init fields specific to the update struct */
1127
1128         state->common.data_type = ACPI_DESC_TYPE_STATE_PACKAGE;
1129         state->pkg.source_object = (union acpi_operand_object *) internal_object;
1130         state->pkg.dest_object  = external_object;
1131         state->pkg.index        = index;
1132         state->pkg.num_packages = 1;
1133
1134         return_PTR (state);
1135 }
1136
1137
1138 /*******************************************************************************
1139  *
1140  * FUNCTION:    acpi_ut_create_control_state
1141  *
1142  * PARAMETERS:  None
1143  *
1144  * RETURN:      Status
1145  *
1146  * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
1147  *              to support nested IF/WHILE constructs in the AML.
1148  *
1149  ******************************************************************************/
1150
1151 union acpi_generic_state *
1152 acpi_ut_create_control_state (
1153         void)
1154 {
1155         union acpi_generic_state        *state;
1156
1157
1158         ACPI_FUNCTION_TRACE ("ut_create_control_state");
1159
1160
1161         /* Create the generic state object */
1162
1163         state = acpi_ut_create_generic_state ();
1164         if (!state) {
1165                 return_PTR (NULL);
1166         }
1167
1168         /* Init fields specific to the control struct */
1169
1170         state->common.data_type = ACPI_DESC_TYPE_STATE_CONTROL;
1171         state->common.state     = ACPI_CONTROL_CONDITIONAL_EXECUTING;
1172
1173         return_PTR (state);
1174 }
1175
1176
1177 /*******************************************************************************
1178  *
1179  * FUNCTION:    acpi_ut_delete_generic_state
1180  *
1181  * PARAMETERS:  State               - The state object to be deleted
1182  *
1183  * RETURN:      Status
1184  *
1185  * DESCRIPTION: Put a state object back into the global state cache.  The object
1186  *              is not actually freed at this time.
1187  *
1188  ******************************************************************************/
1189
1190 void
1191 acpi_ut_delete_generic_state (
1192         union acpi_generic_state        *state)
1193 {
1194         ACPI_FUNCTION_TRACE ("ut_delete_generic_state");
1195
1196
1197         acpi_ut_release_to_cache (ACPI_MEM_LIST_STATE, state);
1198         return_VOID;
1199 }
1200
1201
1202 #ifdef ACPI_ENABLE_OBJECT_CACHE
1203 /*******************************************************************************
1204  *
1205  * FUNCTION:    acpi_ut_delete_generic_state_cache
1206  *
1207  * PARAMETERS:  None
1208  *
1209  * RETURN:      Status
1210  *
1211  * DESCRIPTION: Purge the global state object cache.  Used during subsystem
1212  *              termination.
1213  *
1214  ******************************************************************************/
1215
1216 void
1217 acpi_ut_delete_generic_state_cache (
1218         void)
1219 {
1220         ACPI_FUNCTION_TRACE ("ut_delete_generic_state_cache");
1221
1222
1223         acpi_ut_delete_generic_cache (ACPI_MEM_LIST_STATE);
1224         return_VOID;
1225 }
1226 #endif
1227
1228
1229 /*******************************************************************************
1230  *
1231  * FUNCTION:    acpi_ut_walk_package_tree
1232  *
1233  * PARAMETERS:  obj_desc        - The Package object on which to resolve refs
1234  *
1235  * RETURN:      Status
1236  *
1237  * DESCRIPTION: Walk through a package
1238  *
1239  ******************************************************************************/
1240
1241 acpi_status
1242 acpi_ut_walk_package_tree (
1243         union acpi_operand_object       *source_object,
1244         void                            *target_object,
1245         acpi_pkg_callback               walk_callback,
1246         void                            *context)
1247 {
1248         acpi_status                     status = AE_OK;
1249         union acpi_generic_state        *state_list = NULL;
1250         union acpi_generic_state        *state;
1251         u32                             this_index;
1252         union acpi_operand_object       *this_source_obj;
1253
1254
1255         ACPI_FUNCTION_TRACE ("ut_walk_package_tree");
1256
1257
1258         state = acpi_ut_create_pkg_state (source_object, target_object, 0);
1259         if (!state) {
1260                 return_ACPI_STATUS (AE_NO_MEMORY);
1261         }
1262
1263         while (state) {
1264                 /* Get one element of the package */
1265
1266                 this_index    = state->pkg.index;
1267                 this_source_obj = (union acpi_operand_object *)
1268                                   state->pkg.source_object->package.elements[this_index];
1269
1270                 /*
1271                  * Check for:
1272                  * 1) An uninitialized package element.  It is completely
1273                  *    legal to declare a package and leave it uninitialized
1274                  * 2) Not an internal object - can be a namespace node instead
1275                  * 3) Any type other than a package.  Packages are handled in else
1276                  *    case below.
1277                  */
1278                 if ((!this_source_obj) ||
1279                         (ACPI_GET_DESCRIPTOR_TYPE (this_source_obj) != ACPI_DESC_TYPE_OPERAND) ||
1280                         (ACPI_GET_OBJECT_TYPE (this_source_obj) != ACPI_TYPE_PACKAGE)) {
1281                         status = walk_callback (ACPI_COPY_TYPE_SIMPLE, this_source_obj,
1282                                          state, context);
1283                         if (ACPI_FAILURE (status)) {
1284                                 return_ACPI_STATUS (status);
1285                         }
1286
1287                         state->pkg.index++;
1288                         while (state->pkg.index >= state->pkg.source_object->package.count) {
1289                                 /*
1290                                  * We've handled all of the objects at this level,  This means
1291                                  * that we have just completed a package.  That package may
1292                                  * have contained one or more packages itself.
1293                                  *
1294                                  * Delete this state and pop the previous state (package).
1295                                  */
1296                                 acpi_ut_delete_generic_state (state);
1297                                 state = acpi_ut_pop_generic_state (&state_list);
1298
1299                                 /* Finished when there are no more states */
1300
1301                                 if (!state) {
1302                                         /*
1303                                          * We have handled all of the objects in the top level
1304                                          * package just add the length of the package objects
1305                                          * and exit
1306                                          */
1307                                         return_ACPI_STATUS (AE_OK);
1308                                 }
1309
1310                                 /*
1311                                  * Go back up a level and move the index past the just
1312                                  * completed package object.
1313                                  */
1314                                 state->pkg.index++;
1315                         }
1316                 }
1317                 else {
1318                         /* This is a subobject of type package */
1319
1320                         status = walk_callback (ACPI_COPY_TYPE_PACKAGE, this_source_obj,
1321                                           state, context);
1322                         if (ACPI_FAILURE (status)) {
1323                                 return_ACPI_STATUS (status);
1324                         }
1325
1326                         /*
1327                          * Push the current state and create a new one
1328                          * The callback above returned a new target package object.
1329                          */
1330                         acpi_ut_push_generic_state (&state_list, state);
1331                         state = acpi_ut_create_pkg_state (this_source_obj,
1332                                            state->pkg.this_target_obj, 0);
1333                         if (!state) {
1334                                 return_ACPI_STATUS (AE_NO_MEMORY);
1335                         }
1336                 }
1337         }
1338
1339         /* We should never get here */
1340
1341         return_ACPI_STATUS (AE_AML_INTERNAL);
1342 }
1343
1344
1345 /*******************************************************************************
1346  *
1347  * FUNCTION:    acpi_ut_generate_checksum
1348  *
1349  * PARAMETERS:  Buffer          - Buffer to be scanned
1350  *              Length          - number of bytes to examine
1351  *
1352  * RETURN:      checksum
1353  *
1354  * DESCRIPTION: Generate a checksum on a raw buffer
1355  *
1356  ******************************************************************************/
1357
1358 u8
1359 acpi_ut_generate_checksum (
1360         u8                              *buffer,
1361         u32                             length)
1362 {
1363         u32                             i;
1364         signed char                     sum = 0;
1365
1366
1367         for (i = 0; i < length; i++) {
1368                 sum = (signed char) (sum + buffer[i]);
1369         }
1370
1371         return ((u8) (0 - sum));
1372 }
1373
1374
1375 /*******************************************************************************
1376  *
1377  * FUNCTION:    acpi_ut_get_resource_end_tag
1378  *
1379  * PARAMETERS:  obj_desc        - The resource template buffer object
1380  *
1381  * RETURN:      Pointer to the end tag
1382  *
1383  * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
1384  *
1385  ******************************************************************************/
1386
1387
1388 u8 *
1389 acpi_ut_get_resource_end_tag (
1390         union acpi_operand_object       *obj_desc)
1391 {
1392         u8                              buffer_byte;
1393         u8                              *buffer;
1394         u8                              *end_buffer;
1395
1396
1397         buffer    = obj_desc->buffer.pointer;
1398         end_buffer = buffer + obj_desc->buffer.length;
1399
1400         while (buffer < end_buffer) {
1401                 buffer_byte = *buffer;
1402                 if (buffer_byte & ACPI_RDESC_TYPE_MASK) {
1403                         /* Large Descriptor - Length is next 2 bytes */
1404
1405                         buffer += ((*(buffer+1) | (*(buffer+2) << 8)) + 3);
1406                 }
1407                 else {
1408                         /* Small Descriptor.  End Tag will be found here */
1409
1410                         if ((buffer_byte & ACPI_RDESC_SMALL_MASK) == ACPI_RDESC_TYPE_END_TAG) {
1411                                 /* Found the end tag descriptor, all done. */
1412
1413                                 return (buffer);
1414                         }
1415
1416                         /* Length is in the header */
1417
1418                         buffer += ((buffer_byte & 0x07) + 1);
1419                 }
1420         }
1421
1422         /* End tag not found */
1423
1424         return (NULL);
1425 }
1426
1427
1428 /*******************************************************************************
1429  *
1430  * FUNCTION:    acpi_ut_report_error
1431  *
1432  * PARAMETERS:  module_name         - Caller's module name (for error output)
1433  *              line_number         - Caller's line number (for error output)
1434  *              component_id        - Caller's component ID (for error output)
1435  *              Message             - Error message to use on failure
1436  *
1437  * RETURN:      None
1438  *
1439  * DESCRIPTION: Print error message
1440  *
1441  ******************************************************************************/
1442
1443 void
1444 acpi_ut_report_error (
1445         char                            *module_name,
1446         u32                             line_number,
1447         u32                             component_id)
1448 {
1449
1450
1451         acpi_os_printf ("%8s-%04d: *** Error: ", module_name, line_number);
1452 }
1453
1454
1455 /*******************************************************************************
1456  *
1457  * FUNCTION:    acpi_ut_report_warning
1458  *
1459  * PARAMETERS:  module_name         - Caller's module name (for error output)
1460  *              line_number         - Caller's line number (for error output)
1461  *              component_id        - Caller's component ID (for error output)
1462  *              Message             - Error message to use on failure
1463  *
1464  * RETURN:      None
1465  *
1466  * DESCRIPTION: Print warning message
1467  *
1468  ******************************************************************************/
1469
1470 void
1471 acpi_ut_report_warning (
1472         char                            *module_name,
1473         u32                             line_number,
1474         u32                             component_id)
1475 {
1476
1477         acpi_os_printf ("%8s-%04d: *** Warning: ", module_name, line_number);
1478 }
1479
1480
1481 /*******************************************************************************
1482  *
1483  * FUNCTION:    acpi_ut_report_info
1484  *
1485  * PARAMETERS:  module_name         - Caller's module name (for error output)
1486  *              line_number         - Caller's line number (for error output)
1487  *              component_id        - Caller's component ID (for error output)
1488  *              Message             - Error message to use on failure
1489  *
1490  * RETURN:      None
1491  *
1492  * DESCRIPTION: Print information message
1493  *
1494  ******************************************************************************/
1495
1496 void
1497 acpi_ut_report_info (
1498         char                            *module_name,
1499         u32                             line_number,
1500         u32                             component_id)
1501 {
1502
1503         acpi_os_printf ("%8s-%04d: *** Info: ", module_name, line_number);
1504 }
1505
1506