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