]> err.no Git - linux-2.6/blob - drivers/acpi/utilities/utmisc.c
bb658777fa8849125096f0c8f8378cc7bc391fe9
[linux-2.6] / 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                             this_thread_id;
791
792
793         ACPI_FUNCTION_NAME ("ut_release_mutex");
794
795
796         this_thread_id = acpi_os_get_thread_id ();
797         ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
798                 "Thread %X releasing Mutex [%s]\n", this_thread_id,
799                 acpi_ut_get_mutex_name (mutex_id)));
800
801         if (mutex_id > MAX_MUTEX) {
802                 return (AE_BAD_PARAMETER);
803         }
804
805         /*
806          * Mutex must be acquired in order to release it!
807          */
808         if (acpi_gbl_mutex_info[mutex_id].owner_id == ACPI_MUTEX_NOT_ACQUIRED) {
809                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
810                         "Mutex [%s] is not acquired, cannot release\n",
811                         acpi_ut_get_mutex_name (mutex_id)));
812
813                 return (AE_NOT_ACQUIRED);
814         }
815
816 #ifdef ACPI_MUTEX_DEBUG
817         {
818                 u32                             i;
819                 /*
820                  * Mutex debug code, for internal debugging only.
821                  *
822                  * Deadlock prevention.  Check if this thread owns any mutexes of value
823                  * greater than this one.  If so, the thread has violated the mutex
824                  * ordering rule.  This indicates a coding error somewhere in
825                  * the ACPI subsystem code.
826                  */
827                 for (i = mutex_id; i < MAX_MUTEX; i++) {
828                         if (acpi_gbl_mutex_info[i].owner_id == this_thread_id) {
829                                 if (i == mutex_id) {
830                                         continue;
831                                 }
832
833                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
834                                         "Invalid release order: owns [%s], releasing [%s]\n",
835                                         acpi_ut_get_mutex_name (i), acpi_ut_get_mutex_name (mutex_id)));
836
837                                 return (AE_RELEASE_DEADLOCK);
838                         }
839                 }
840         }
841 #endif
842
843         /* Mark unlocked FIRST */
844
845         acpi_gbl_mutex_info[mutex_id].owner_id = ACPI_MUTEX_NOT_ACQUIRED;
846
847         status = acpi_os_signal_semaphore (acpi_gbl_mutex_info[mutex_id].mutex, 1);
848
849         if (ACPI_FAILURE (status)) {
850                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
851                         "Thread %X could not release Mutex [%s] %s\n",
852                         this_thread_id, acpi_ut_get_mutex_name (mutex_id),
853                         acpi_format_exception (status)));
854         }
855         else {
856                 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X released Mutex [%s]\n",
857                         this_thread_id, acpi_ut_get_mutex_name (mutex_id)));
858         }
859
860         return (status);
861 }
862
863
864 /*******************************************************************************
865  *
866  * FUNCTION:    acpi_ut_create_update_state_and_push
867  *
868  * PARAMETERS:  Object          - Object to be added to the new state
869  *              Action          - Increment/Decrement
870  *              state_list      - List the state will be added to
871  *
872  * RETURN:      Status
873  *
874  * DESCRIPTION: Create a new state and push it
875  *
876  ******************************************************************************/
877
878 acpi_status
879 acpi_ut_create_update_state_and_push (
880         union acpi_operand_object       *object,
881         u16                             action,
882         union acpi_generic_state        **state_list)
883 {
884         union acpi_generic_state         *state;
885
886
887         ACPI_FUNCTION_ENTRY ();
888
889
890         /* Ignore null objects; these are expected */
891
892         if (!object) {
893                 return (AE_OK);
894         }
895
896         state = acpi_ut_create_update_state (object, action);
897         if (!state) {
898                 return (AE_NO_MEMORY);
899         }
900
901         acpi_ut_push_generic_state (state_list, state);
902         return (AE_OK);
903 }
904
905
906 /*******************************************************************************
907  *
908  * FUNCTION:    acpi_ut_create_pkg_state_and_push
909  *
910  * PARAMETERS:  Object          - Object to be added to the new state
911  *              Action          - Increment/Decrement
912  *              state_list      - List the state will be added to
913  *
914  * RETURN:      Status
915  *
916  * DESCRIPTION: Create a new state and push it
917  *
918  ******************************************************************************/
919
920 #ifdef ACPI_FUTURE_USAGE
921 acpi_status
922 acpi_ut_create_pkg_state_and_push (
923         void                            *internal_object,
924         void                            *external_object,
925         u16                             index,
926         union acpi_generic_state        **state_list)
927 {
928         union acpi_generic_state         *state;
929
930
931         ACPI_FUNCTION_ENTRY ();
932
933
934         state = acpi_ut_create_pkg_state (internal_object, external_object, index);
935         if (!state) {
936                 return (AE_NO_MEMORY);
937         }
938
939         acpi_ut_push_generic_state (state_list, state);
940         return (AE_OK);
941 }
942 #endif  /*  ACPI_FUTURE_USAGE  */
943
944 /*******************************************************************************
945  *
946  * FUNCTION:    acpi_ut_push_generic_state
947  *
948  * PARAMETERS:  list_head           - Head of the state stack
949  *              State               - State object to push
950  *
951  * RETURN:      None
952  *
953  * DESCRIPTION: Push a state object onto a state stack
954  *
955  ******************************************************************************/
956
957 void
958 acpi_ut_push_generic_state (
959         union acpi_generic_state        **list_head,
960         union acpi_generic_state        *state)
961 {
962         ACPI_FUNCTION_TRACE ("ut_push_generic_state");
963
964
965         /* Push the state object onto the front of the list (stack) */
966
967         state->common.next = *list_head;
968         *list_head = state;
969
970         return_VOID;
971 }
972
973
974 /*******************************************************************************
975  *
976  * FUNCTION:    acpi_ut_pop_generic_state
977  *
978  * PARAMETERS:  list_head           - Head of the state stack
979  *
980  * RETURN:      The popped state object
981  *
982  * DESCRIPTION: Pop a state object from a state stack
983  *
984  ******************************************************************************/
985
986 union acpi_generic_state *
987 acpi_ut_pop_generic_state (
988         union acpi_generic_state        **list_head)
989 {
990         union acpi_generic_state        *state;
991
992
993         ACPI_FUNCTION_TRACE ("ut_pop_generic_state");
994
995
996         /* Remove the state object at the head of the list (stack) */
997
998         state = *list_head;
999         if (state) {
1000                 /* Update the list head */
1001
1002                 *list_head = state->common.next;
1003         }
1004
1005         return_PTR (state);
1006 }
1007
1008
1009 /*******************************************************************************
1010  *
1011  * FUNCTION:    acpi_ut_create_generic_state
1012  *
1013  * PARAMETERS:  None
1014  *
1015  * RETURN:      The new state object. NULL on failure.
1016  *
1017  * DESCRIPTION: Create a generic state object.  Attempt to obtain one from
1018  *              the global state cache;  If none available, create a new one.
1019  *
1020  ******************************************************************************/
1021
1022 union acpi_generic_state *
1023 acpi_ut_create_generic_state (
1024         void)
1025 {
1026         union acpi_generic_state        *state;
1027
1028
1029         ACPI_FUNCTION_ENTRY ();
1030
1031
1032         state = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_STATE);
1033
1034         /* Initialize */
1035
1036         if (state) {
1037                 state->common.data_type = ACPI_DESC_TYPE_STATE;
1038         }
1039
1040         return (state);
1041 }
1042
1043
1044 /*******************************************************************************
1045  *
1046  * FUNCTION:    acpi_ut_create_thread_state
1047  *
1048  * PARAMETERS:  None
1049  *
1050  * RETURN:      New Thread State. NULL on failure
1051  *
1052  * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
1053  *              to track per-thread info during method execution
1054  *
1055  ******************************************************************************/
1056
1057 struct acpi_thread_state *
1058 acpi_ut_create_thread_state (
1059         void)
1060 {
1061         union acpi_generic_state        *state;
1062
1063
1064         ACPI_FUNCTION_TRACE ("ut_create_thread_state");
1065
1066
1067         /* Create the generic state object */
1068
1069         state = acpi_ut_create_generic_state ();
1070         if (!state) {
1071                 return_PTR (NULL);
1072         }
1073
1074         /* Init fields specific to the update struct */
1075
1076         state->common.data_type = ACPI_DESC_TYPE_STATE_THREAD;
1077         state->thread.thread_id = acpi_os_get_thread_id ();
1078
1079         return_PTR ((struct acpi_thread_state *) state);
1080 }
1081
1082
1083 /*******************************************************************************
1084  *
1085  * FUNCTION:    acpi_ut_create_update_state
1086  *
1087  * PARAMETERS:  Object          - Initial Object to be installed in the state
1088  *              Action          - Update action to be performed
1089  *
1090  * RETURN:      New state object, null on failure
1091  *
1092  * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
1093  *              to update reference counts and delete complex objects such
1094  *              as packages.
1095  *
1096  ******************************************************************************/
1097
1098 union acpi_generic_state *
1099 acpi_ut_create_update_state (
1100         union acpi_operand_object       *object,
1101         u16                             action)
1102 {
1103         union acpi_generic_state        *state;
1104
1105
1106         ACPI_FUNCTION_TRACE_PTR ("ut_create_update_state", object);
1107
1108
1109         /* Create the generic state object */
1110
1111         state = acpi_ut_create_generic_state ();
1112         if (!state) {
1113                 return_PTR (NULL);
1114         }
1115
1116         /* Init fields specific to the update struct */
1117
1118         state->common.data_type = ACPI_DESC_TYPE_STATE_UPDATE;
1119         state->update.object = object;
1120         state->update.value  = action;
1121
1122         return_PTR (state);
1123 }
1124
1125
1126 /*******************************************************************************
1127  *
1128  * FUNCTION:    acpi_ut_create_pkg_state
1129  *
1130  * PARAMETERS:  Object          - Initial Object to be installed in the state
1131  *              Action          - Update action to be performed
1132  *
1133  * RETURN:      New state object, null on failure
1134  *
1135  * DESCRIPTION: Create a "Package State"
1136  *
1137  ******************************************************************************/
1138
1139 union acpi_generic_state *
1140 acpi_ut_create_pkg_state (
1141         void                            *internal_object,
1142         void                            *external_object,
1143         u16                             index)
1144 {
1145         union acpi_generic_state        *state;
1146
1147
1148         ACPI_FUNCTION_TRACE_PTR ("ut_create_pkg_state", internal_object);
1149
1150
1151         /* Create the generic state object */
1152
1153         state = acpi_ut_create_generic_state ();
1154         if (!state) {
1155                 return_PTR (NULL);
1156         }
1157
1158         /* Init fields specific to the update struct */
1159
1160         state->common.data_type = ACPI_DESC_TYPE_STATE_PACKAGE;
1161         state->pkg.source_object = (union acpi_operand_object *) internal_object;
1162         state->pkg.dest_object  = external_object;
1163         state->pkg.index        = index;
1164         state->pkg.num_packages = 1;
1165
1166         return_PTR (state);
1167 }
1168
1169
1170 /*******************************************************************************
1171  *
1172  * FUNCTION:    acpi_ut_create_control_state
1173  *
1174  * PARAMETERS:  None
1175  *
1176  * RETURN:      New state object, null on failure
1177  *
1178  * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
1179  *              to support nested IF/WHILE constructs in the AML.
1180  *
1181  ******************************************************************************/
1182
1183 union acpi_generic_state *
1184 acpi_ut_create_control_state (
1185         void)
1186 {
1187         union acpi_generic_state        *state;
1188
1189
1190         ACPI_FUNCTION_TRACE ("ut_create_control_state");
1191
1192
1193         /* Create the generic state object */
1194
1195         state = acpi_ut_create_generic_state ();
1196         if (!state) {
1197                 return_PTR (NULL);
1198         }
1199
1200         /* Init fields specific to the control struct */
1201
1202         state->common.data_type = ACPI_DESC_TYPE_STATE_CONTROL;
1203         state->common.state     = ACPI_CONTROL_CONDITIONAL_EXECUTING;
1204
1205         return_PTR (state);
1206 }
1207
1208
1209 /*******************************************************************************
1210  *
1211  * FUNCTION:    acpi_ut_delete_generic_state
1212  *
1213  * PARAMETERS:  State               - The state object to be deleted
1214  *
1215  * RETURN:      None
1216  *
1217  * DESCRIPTION: Put a state object back into the global state cache.  The object
1218  *              is not actually freed at this time.
1219  *
1220  ******************************************************************************/
1221
1222 void
1223 acpi_ut_delete_generic_state (
1224         union acpi_generic_state        *state)
1225 {
1226         ACPI_FUNCTION_TRACE ("ut_delete_generic_state");
1227
1228
1229         acpi_ut_release_to_cache (ACPI_MEM_LIST_STATE, state);
1230         return_VOID;
1231 }
1232
1233
1234 #ifdef ACPI_ENABLE_OBJECT_CACHE
1235 /*******************************************************************************
1236  *
1237  * FUNCTION:    acpi_ut_delete_generic_state_cache
1238  *
1239  * PARAMETERS:  None
1240  *
1241  * RETURN:      None
1242  *
1243  * DESCRIPTION: Purge the global state object cache.  Used during subsystem
1244  *              termination.
1245  *
1246  ******************************************************************************/
1247
1248 void
1249 acpi_ut_delete_generic_state_cache (
1250         void)
1251 {
1252         ACPI_FUNCTION_TRACE ("ut_delete_generic_state_cache");
1253
1254
1255         acpi_ut_delete_generic_cache (ACPI_MEM_LIST_STATE);
1256         return_VOID;
1257 }
1258 #endif
1259
1260
1261 /*******************************************************************************
1262  *
1263  * FUNCTION:    acpi_ut_walk_package_tree
1264  *
1265  * PARAMETERS:  source_object       - The package to walk
1266  *              target_object       - Target object (if package is being copied)
1267  *              walk_callback       - Called once for each package element
1268  *              Context             - Passed to the callback function
1269  *
1270  * RETURN:      Status
1271  *
1272  * DESCRIPTION: Walk through a package
1273  *
1274  ******************************************************************************/
1275
1276 acpi_status
1277 acpi_ut_walk_package_tree (
1278         union acpi_operand_object       *source_object,
1279         void                            *target_object,
1280         acpi_pkg_callback               walk_callback,
1281         void                            *context)
1282 {
1283         acpi_status                     status = AE_OK;
1284         union acpi_generic_state        *state_list = NULL;
1285         union acpi_generic_state        *state;
1286         u32                             this_index;
1287         union acpi_operand_object       *this_source_obj;
1288
1289
1290         ACPI_FUNCTION_TRACE ("ut_walk_package_tree");
1291
1292
1293         state = acpi_ut_create_pkg_state (source_object, target_object, 0);
1294         if (!state) {
1295                 return_ACPI_STATUS (AE_NO_MEMORY);
1296         }
1297
1298         while (state) {
1299                 /* Get one element of the package */
1300
1301                 this_index    = state->pkg.index;
1302                 this_source_obj = (union acpi_operand_object *)
1303                                   state->pkg.source_object->package.elements[this_index];
1304
1305                 /*
1306                  * Check for:
1307                  * 1) An uninitialized package element.  It is completely
1308                  *    legal to declare a package and leave it uninitialized
1309                  * 2) Not an internal object - can be a namespace node instead
1310                  * 3) Any type other than a package.  Packages are handled in else
1311                  *    case below.
1312                  */
1313                 if ((!this_source_obj) ||
1314                         (ACPI_GET_DESCRIPTOR_TYPE (this_source_obj) != ACPI_DESC_TYPE_OPERAND) ||
1315                         (ACPI_GET_OBJECT_TYPE (this_source_obj) != ACPI_TYPE_PACKAGE)) {
1316                         status = walk_callback (ACPI_COPY_TYPE_SIMPLE, this_source_obj,
1317                                          state, context);
1318                         if (ACPI_FAILURE (status)) {
1319                                 return_ACPI_STATUS (status);
1320                         }
1321
1322                         state->pkg.index++;
1323                         while (state->pkg.index >= state->pkg.source_object->package.count) {
1324                                 /*
1325                                  * We've handled all of the objects at this level,  This means
1326                                  * that we have just completed a package.  That package may
1327                                  * have contained one or more packages itself.
1328                                  *
1329                                  * Delete this state and pop the previous state (package).
1330                                  */
1331                                 acpi_ut_delete_generic_state (state);
1332                                 state = acpi_ut_pop_generic_state (&state_list);
1333
1334                                 /* Finished when there are no more states */
1335
1336                                 if (!state) {
1337                                         /*
1338                                          * We have handled all of the objects in the top level
1339                                          * package just add the length of the package objects
1340                                          * and exit
1341                                          */
1342                                         return_ACPI_STATUS (AE_OK);
1343                                 }
1344
1345                                 /*
1346                                  * Go back up a level and move the index past the just
1347                                  * completed package object.
1348                                  */
1349                                 state->pkg.index++;
1350                         }
1351                 }
1352                 else {
1353                         /* This is a subobject of type package */
1354
1355                         status = walk_callback (ACPI_COPY_TYPE_PACKAGE, this_source_obj,
1356                                           state, context);
1357                         if (ACPI_FAILURE (status)) {
1358                                 return_ACPI_STATUS (status);
1359                         }
1360
1361                         /*
1362                          * Push the current state and create a new one
1363                          * The callback above returned a new target package object.
1364                          */
1365                         acpi_ut_push_generic_state (&state_list, state);
1366                         state = acpi_ut_create_pkg_state (this_source_obj,
1367                                            state->pkg.this_target_obj, 0);
1368                         if (!state) {
1369                                 return_ACPI_STATUS (AE_NO_MEMORY);
1370                         }
1371                 }
1372         }
1373
1374         /* We should never get here */
1375
1376         return_ACPI_STATUS (AE_AML_INTERNAL);
1377 }
1378
1379
1380 /*******************************************************************************
1381  *
1382  * FUNCTION:    acpi_ut_generate_checksum
1383  *
1384  * PARAMETERS:  Buffer          - Buffer to be scanned
1385  *              Length          - number of bytes to examine
1386  *
1387  * RETURN:      The generated checksum
1388  *
1389  * DESCRIPTION: Generate a checksum on a raw buffer
1390  *
1391  ******************************************************************************/
1392
1393 u8
1394 acpi_ut_generate_checksum (
1395         u8                              *buffer,
1396         u32                             length)
1397 {
1398         u32                             i;
1399         signed char                     sum = 0;
1400
1401
1402         for (i = 0; i < length; i++) {
1403                 sum = (signed char) (sum + buffer[i]);
1404         }
1405
1406         return ((u8) (0 - sum));
1407 }
1408
1409
1410 /*******************************************************************************
1411  *
1412  * FUNCTION:    acpi_ut_get_resource_end_tag
1413  *
1414  * PARAMETERS:  obj_desc        - The resource template buffer object
1415  *
1416  * RETURN:      Pointer to the end tag
1417  *
1418  * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
1419  *
1420  ******************************************************************************/
1421
1422
1423 u8 *
1424 acpi_ut_get_resource_end_tag (
1425         union acpi_operand_object       *obj_desc)
1426 {
1427         u8                              buffer_byte;
1428         u8                              *buffer;
1429         u8                              *end_buffer;
1430
1431
1432         buffer    = obj_desc->buffer.pointer;
1433         end_buffer = buffer + obj_desc->buffer.length;
1434
1435         while (buffer < end_buffer) {
1436                 buffer_byte = *buffer;
1437                 if (buffer_byte & ACPI_RDESC_TYPE_MASK) {
1438                         /* Large Descriptor - Length is next 2 bytes */
1439
1440                         buffer += ((*(buffer+1) | (*(buffer+2) << 8)) + 3);
1441                 }
1442                 else {
1443                         /* Small Descriptor.  End Tag will be found here */
1444
1445                         if ((buffer_byte & ACPI_RDESC_SMALL_MASK) == ACPI_RDESC_TYPE_END_TAG) {
1446                                 /* Found the end tag descriptor, all done. */
1447
1448                                 return (buffer);
1449                         }
1450
1451                         /* Length is in the header */
1452
1453                         buffer += ((buffer_byte & 0x07) + 1);
1454                 }
1455         }
1456
1457         /* End tag not found */
1458
1459         return (NULL);
1460 }
1461
1462
1463 /*******************************************************************************
1464  *
1465  * FUNCTION:    acpi_ut_report_error
1466  *
1467  * PARAMETERS:  module_name         - Caller's module name (for error output)
1468  *              line_number         - Caller's line number (for error output)
1469  *              component_id        - Caller's component ID (for error output)
1470  *
1471  * RETURN:      None
1472  *
1473  * DESCRIPTION: Print error message
1474  *
1475  ******************************************************************************/
1476
1477 void
1478 acpi_ut_report_error (
1479         char                            *module_name,
1480         u32                             line_number,
1481         u32                             component_id)
1482 {
1483
1484         acpi_os_printf ("%8s-%04d: *** Error: ", module_name, line_number);
1485 }
1486
1487
1488 /*******************************************************************************
1489  *
1490  * FUNCTION:    acpi_ut_report_warning
1491  *
1492  * PARAMETERS:  module_name         - Caller's module name (for error output)
1493  *              line_number         - Caller's line number (for error output)
1494  *              component_id        - Caller's component ID (for error output)
1495  *
1496  * RETURN:      None
1497  *
1498  * DESCRIPTION: Print warning message
1499  *
1500  ******************************************************************************/
1501
1502 void
1503 acpi_ut_report_warning (
1504         char                            *module_name,
1505         u32                             line_number,
1506         u32                             component_id)
1507 {
1508
1509         acpi_os_printf ("%8s-%04d: *** Warning: ", module_name, line_number);
1510 }
1511
1512
1513 /*******************************************************************************
1514  *
1515  * FUNCTION:    acpi_ut_report_info
1516  *
1517  * PARAMETERS:  module_name         - Caller's module name (for error output)
1518  *              line_number         - Caller's line number (for error output)
1519  *              component_id        - Caller's component ID (for error output)
1520  *
1521  * RETURN:      None
1522  *
1523  * DESCRIPTION: Print information message
1524  *
1525  ******************************************************************************/
1526
1527 void
1528 acpi_ut_report_info (
1529         char                            *module_name,
1530         u32                             line_number,
1531         u32                             component_id)
1532 {
1533
1534         acpi_os_printf ("%8s-%04d: *** Info: ", module_name, line_number);
1535 }
1536
1537