ACPICA: create acpica/ directory
[linux-2.6] / drivers / acpi / acpica / utmisc.c
1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2008, Intel Corp.
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 #include <linux/module.h>
45
46 #include <acpi/acpi.h>
47 #include <acpi/accommon.h>
48 #include <acpi/acnamesp.h>
49
50 #define _COMPONENT          ACPI_UTILITIES
51 ACPI_MODULE_NAME("utmisc")
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_ut_validate_exception
56  *
57  * PARAMETERS:  Status       - The acpi_status code to be formatted
58  *
59  * RETURN:      A string containing the exception text. NULL if exception is
60  *              not valid.
61  *
62  * DESCRIPTION: This function validates and translates an ACPI exception into
63  *              an ASCII string.
64  *
65  ******************************************************************************/
66 const char *acpi_ut_validate_exception(acpi_status status)
67 {
68         u32 sub_status;
69         const char *exception = NULL;
70
71         ACPI_FUNCTION_ENTRY();
72
73         /*
74          * Status is composed of two parts, a "type" and an actual code
75          */
76         sub_status = (status & ~AE_CODE_MASK);
77
78         switch (status & AE_CODE_MASK) {
79         case AE_CODE_ENVIRONMENTAL:
80
81                 if (sub_status <= AE_CODE_ENV_MAX) {
82                         exception = acpi_gbl_exception_names_env[sub_status];
83                 }
84                 break;
85
86         case AE_CODE_PROGRAMMER:
87
88                 if (sub_status <= AE_CODE_PGM_MAX) {
89                         exception = acpi_gbl_exception_names_pgm[sub_status];
90                 }
91                 break;
92
93         case AE_CODE_ACPI_TABLES:
94
95                 if (sub_status <= AE_CODE_TBL_MAX) {
96                         exception = acpi_gbl_exception_names_tbl[sub_status];
97                 }
98                 break;
99
100         case AE_CODE_AML:
101
102                 if (sub_status <= AE_CODE_AML_MAX) {
103                         exception = acpi_gbl_exception_names_aml[sub_status];
104                 }
105                 break;
106
107         case AE_CODE_CONTROL:
108
109                 if (sub_status <= AE_CODE_CTRL_MAX) {
110                         exception = acpi_gbl_exception_names_ctrl[sub_status];
111                 }
112                 break;
113
114         default:
115                 break;
116         }
117
118         return (ACPI_CAST_PTR(const char, exception));
119 }
120
121 /*******************************************************************************
122  *
123  * FUNCTION:    acpi_ut_is_aml_table
124  *
125  * PARAMETERS:  Table               - An ACPI table
126  *
127  * RETURN:      TRUE if table contains executable AML; FALSE otherwise
128  *
129  * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
130  *              Currently, these are DSDT,SSDT,PSDT. All other table types are
131  *              data tables that do not contain AML code.
132  *
133  ******************************************************************************/
134
135 u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
136 {
137
138         /* These are the only tables that contain executable AML */
139
140         if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
141             ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
142             ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
143                 return (TRUE);
144         }
145
146         return (FALSE);
147 }
148
149 /*******************************************************************************
150  *
151  * FUNCTION:    acpi_ut_allocate_owner_id
152  *
153  * PARAMETERS:  owner_id        - Where the new owner ID is returned
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
158  *              track objects created by the table or method, to be deleted
159  *              when the method exits or the table is unloaded.
160  *
161  ******************************************************************************/
162
163 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
164 {
165         u32 i;
166         u32 j;
167         u32 k;
168         acpi_status status;
169
170         ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
171
172         /* Guard against multiple allocations of ID to the same location */
173
174         if (*owner_id) {
175                 ACPI_ERROR((AE_INFO, "Owner ID [%2.2X] already exists",
176                             *owner_id));
177                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
178         }
179
180         /* Mutex for the global ID mask */
181
182         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
183         if (ACPI_FAILURE(status)) {
184                 return_ACPI_STATUS(status);
185         }
186
187         /*
188          * Find a free owner ID, cycle through all possible IDs on repeated
189          * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
190          * to be scanned twice.
191          */
192         for (i = 0, j = acpi_gbl_last_owner_id_index;
193              i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
194                 if (j >= ACPI_NUM_OWNERID_MASKS) {
195                         j = 0;  /* Wraparound to start of mask array */
196                 }
197
198                 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
199                         if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
200
201                                 /* There are no free IDs in this mask */
202
203                                 break;
204                         }
205
206                         if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
207                                 /*
208                                  * Found a free ID. The actual ID is the bit index plus one,
209                                  * making zero an invalid Owner ID. Save this as the last ID
210                                  * allocated and update the global ID mask.
211                                  */
212                                 acpi_gbl_owner_id_mask[j] |= (1 << k);
213
214                                 acpi_gbl_last_owner_id_index = (u8) j;
215                                 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
216
217                                 /*
218                                  * Construct encoded ID from the index and bit position
219                                  *
220                                  * Note: Last [j].k (bit 255) is never used and is marked
221                                  * permanently allocated (prevents +1 overflow)
222                                  */
223                                 *owner_id =
224                                     (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
225
226                                 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
227                                                   "Allocated OwnerId: %2.2X\n",
228                                                   (unsigned int)*owner_id));
229                                 goto exit;
230                         }
231                 }
232
233                 acpi_gbl_next_owner_id_offset = 0;
234         }
235
236         /*
237          * All owner_ids have been allocated. This typically should
238          * not happen since the IDs are reused after deallocation. The IDs are
239          * allocated upon table load (one per table) and method execution, and
240          * they are released when a table is unloaded or a method completes
241          * execution.
242          *
243          * If this error happens, there may be very deep nesting of invoked control
244          * methods, or there may be a bug where the IDs are not released.
245          */
246         status = AE_OWNER_ID_LIMIT;
247         ACPI_ERROR((AE_INFO,
248                     "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
249
250       exit:
251         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
252         return_ACPI_STATUS(status);
253 }
254
255 /*******************************************************************************
256  *
257  * FUNCTION:    acpi_ut_release_owner_id
258  *
259  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_iD
260  *
261  * RETURN:      None. No error is returned because we are either exiting a
262  *              control method or unloading a table. Either way, we would
263  *              ignore any error anyway.
264  *
265  * DESCRIPTION: Release a table or method owner ID.  Valid IDs are 1 - 255
266  *
267  ******************************************************************************/
268
269 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
270 {
271         acpi_owner_id owner_id = *owner_id_ptr;
272         acpi_status status;
273         u32 index;
274         u32 bit;
275
276         ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
277
278         /* Always clear the input owner_id (zero is an invalid ID) */
279
280         *owner_id_ptr = 0;
281
282         /* Zero is not a valid owner_iD */
283
284         if (owner_id == 0) {
285                 ACPI_ERROR((AE_INFO, "Invalid OwnerId: %2.2X", owner_id));
286                 return_VOID;
287         }
288
289         /* Mutex for the global ID mask */
290
291         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
292         if (ACPI_FAILURE(status)) {
293                 return_VOID;
294         }
295
296         /* Normalize the ID to zero */
297
298         owner_id--;
299
300         /* Decode ID to index/offset pair */
301
302         index = ACPI_DIV_32(owner_id);
303         bit = 1 << ACPI_MOD_32(owner_id);
304
305         /* Free the owner ID only if it is valid */
306
307         if (acpi_gbl_owner_id_mask[index] & bit) {
308                 acpi_gbl_owner_id_mask[index] ^= bit;
309         } else {
310                 ACPI_ERROR((AE_INFO,
311                             "Release of non-allocated OwnerId: %2.2X",
312                             owner_id + 1));
313         }
314
315         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
316         return_VOID;
317 }
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ut_strupr (strupr)
322  *
323  * PARAMETERS:  src_string      - The source string to convert
324  *
325  * RETURN:      None
326  *
327  * DESCRIPTION: Convert string to uppercase
328  *
329  * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
330  *
331  ******************************************************************************/
332
333 void acpi_ut_strupr(char *src_string)
334 {
335         char *string;
336
337         ACPI_FUNCTION_ENTRY();
338
339         if (!src_string) {
340                 return;
341         }
342
343         /* Walk entire string, uppercasing the letters */
344
345         for (string = src_string; *string; string++) {
346                 *string = (char)ACPI_TOUPPER(*string);
347         }
348
349         return;
350 }
351
352 /*******************************************************************************
353  *
354  * FUNCTION:    acpi_ut_print_string
355  *
356  * PARAMETERS:  String          - Null terminated ASCII string
357  *              max_length      - Maximum output length
358  *
359  * RETURN:      None
360  *
361  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
362  *              sequences.
363  *
364  ******************************************************************************/
365
366 void acpi_ut_print_string(char *string, u8 max_length)
367 {
368         u32 i;
369
370         if (!string) {
371                 acpi_os_printf("<\"NULL STRING PTR\">");
372                 return;
373         }
374
375         acpi_os_printf("\"");
376         for (i = 0; string[i] && (i < max_length); i++) {
377
378                 /* Escape sequences */
379
380                 switch (string[i]) {
381                 case 0x07:
382                         acpi_os_printf("\\a");  /* BELL */
383                         break;
384
385                 case 0x08:
386                         acpi_os_printf("\\b");  /* BACKSPACE */
387                         break;
388
389                 case 0x0C:
390                         acpi_os_printf("\\f");  /* FORMFEED */
391                         break;
392
393                 case 0x0A:
394                         acpi_os_printf("\\n");  /* LINEFEED */
395                         break;
396
397                 case 0x0D:
398                         acpi_os_printf("\\r");  /* CARRIAGE RETURN */
399                         break;
400
401                 case 0x09:
402                         acpi_os_printf("\\t");  /* HORIZONTAL TAB */
403                         break;
404
405                 case 0x0B:
406                         acpi_os_printf("\\v");  /* VERTICAL TAB */
407                         break;
408
409                 case '\'':      /* Single Quote */
410                 case '\"':      /* Double Quote */
411                 case '\\':      /* Backslash */
412                         acpi_os_printf("\\%c", (int)string[i]);
413                         break;
414
415                 default:
416
417                         /* Check for printable character or hex escape */
418
419                         if (ACPI_IS_PRINT(string[i])) {
420                                 /* This is a normal character */
421
422                                 acpi_os_printf("%c", (int)string[i]);
423                         } else {
424                                 /* All others will be Hex escapes */
425
426                                 acpi_os_printf("\\x%2.2X", (s32) string[i]);
427                         }
428                         break;
429                 }
430         }
431         acpi_os_printf("\"");
432
433         if (i == max_length && string[i]) {
434                 acpi_os_printf("...");
435         }
436 }
437
438 /*******************************************************************************
439  *
440  * FUNCTION:    acpi_ut_dword_byte_swap
441  *
442  * PARAMETERS:  Value           - Value to be converted
443  *
444  * RETURN:      u32 integer with bytes swapped
445  *
446  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
447  *
448  ******************************************************************************/
449
450 u32 acpi_ut_dword_byte_swap(u32 value)
451 {
452         union {
453                 u32 value;
454                 u8 bytes[4];
455         } out;
456         union {
457                 u32 value;
458                 u8 bytes[4];
459         } in;
460
461         ACPI_FUNCTION_ENTRY();
462
463         in.value = value;
464
465         out.bytes[0] = in.bytes[3];
466         out.bytes[1] = in.bytes[2];
467         out.bytes[2] = in.bytes[1];
468         out.bytes[3] = in.bytes[0];
469
470         return (out.value);
471 }
472
473 /*******************************************************************************
474  *
475  * FUNCTION:    acpi_ut_set_integer_width
476  *
477  * PARAMETERS:  Revision            From DSDT header
478  *
479  * RETURN:      None
480  *
481  * DESCRIPTION: Set the global integer bit width based upon the revision
482  *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
483  *              For Revision 2 and above, Integers are 64 bits.  Yes, this
484  *              makes a difference.
485  *
486  ******************************************************************************/
487
488 void acpi_ut_set_integer_width(u8 revision)
489 {
490
491         if (revision < 2) {
492
493                 /* 32-bit case */
494
495                 acpi_gbl_integer_bit_width = 32;
496                 acpi_gbl_integer_nybble_width = 8;
497                 acpi_gbl_integer_byte_width = 4;
498         } else {
499                 /* 64-bit case (ACPI 2.0+) */
500
501                 acpi_gbl_integer_bit_width = 64;
502                 acpi_gbl_integer_nybble_width = 16;
503                 acpi_gbl_integer_byte_width = 8;
504         }
505 }
506
507 #ifdef ACPI_DEBUG_OUTPUT
508 /*******************************************************************************
509  *
510  * FUNCTION:    acpi_ut_display_init_pathname
511  *
512  * PARAMETERS:  Type                - Object type of the node
513  *              obj_handle          - Handle whose pathname will be displayed
514  *              Path                - Additional path string to be appended.
515  *                                      (NULL if no extra path)
516  *
517  * RETURN:      acpi_status
518  *
519  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
520  *
521  ******************************************************************************/
522
523 void
524 acpi_ut_display_init_pathname(u8 type,
525                               struct acpi_namespace_node *obj_handle,
526                               char *path)
527 {
528         acpi_status status;
529         struct acpi_buffer buffer;
530
531         ACPI_FUNCTION_ENTRY();
532
533         /* Only print the path if the appropriate debug level is enabled */
534
535         if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
536                 return;
537         }
538
539         /* Get the full pathname to the node */
540
541         buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
542         status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
543         if (ACPI_FAILURE(status)) {
544                 return;
545         }
546
547         /* Print what we're doing */
548
549         switch (type) {
550         case ACPI_TYPE_METHOD:
551                 acpi_os_printf("Executing  ");
552                 break;
553
554         default:
555                 acpi_os_printf("Initializing ");
556                 break;
557         }
558
559         /* Print the object type and pathname */
560
561         acpi_os_printf("%-12s %s",
562                        acpi_ut_get_type_name(type), (char *)buffer.pointer);
563
564         /* Extra path is used to append names like _STA, _INI, etc. */
565
566         if (path) {
567                 acpi_os_printf(".%s", path);
568         }
569         acpi_os_printf("\n");
570
571         ACPI_FREE(buffer.pointer);
572 }
573 #endif
574
575 /*******************************************************************************
576  *
577  * FUNCTION:    acpi_ut_valid_acpi_char
578  *
579  * PARAMETERS:  Char            - The character to be examined
580  *              Position        - Byte position (0-3)
581  *
582  * RETURN:      TRUE if the character is valid, FALSE otherwise
583  *
584  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
585  *              1) Upper case alpha
586  *              2) numeric
587  *              3) underscore
588  *
589  *              We allow a '!' as the last character because of the ASF! table
590  *
591  ******************************************************************************/
592
593 u8 acpi_ut_valid_acpi_char(char character, u32 position)
594 {
595
596         if (!((character >= 'A' && character <= 'Z') ||
597               (character >= '0' && character <= '9') || (character == '_'))) {
598
599                 /* Allow a '!' in the last position */
600
601                 if (character == '!' && position == 3) {
602                         return (TRUE);
603                 }
604
605                 return (FALSE);
606         }
607
608         return (TRUE);
609 }
610
611 /*******************************************************************************
612  *
613  * FUNCTION:    acpi_ut_valid_acpi_name
614  *
615  * PARAMETERS:  Name            - The name to be examined
616  *
617  * RETURN:      TRUE if the name is valid, FALSE otherwise
618  *
619  * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
620  *              1) Upper case alpha
621  *              2) numeric
622  *              3) underscore
623  *
624  ******************************************************************************/
625
626 u8 acpi_ut_valid_acpi_name(u32 name)
627 {
628         u32 i;
629
630         ACPI_FUNCTION_ENTRY();
631
632         for (i = 0; i < ACPI_NAME_SIZE; i++) {
633                 if (!acpi_ut_valid_acpi_char
634                     ((ACPI_CAST_PTR(char, &name))[i], i)) {
635                         return (FALSE);
636                 }
637         }
638
639         return (TRUE);
640 }
641
642 /*******************************************************************************
643  *
644  * FUNCTION:    acpi_ut_repair_name
645  *
646  * PARAMETERS:  Name            - The ACPI name to be repaired
647  *
648  * RETURN:      Repaired version of the name
649  *
650  * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
651  *              return the new name.
652  *
653  ******************************************************************************/
654
655 acpi_name acpi_ut_repair_name(char *name)
656 {
657        u32 i;
658         char new_name[ACPI_NAME_SIZE];
659
660         for (i = 0; i < ACPI_NAME_SIZE; i++) {
661                 new_name[i] = name[i];
662
663                 /*
664                  * Replace a bad character with something printable, yet technically
665                  * still invalid. This prevents any collisions with existing "good"
666                  * names in the namespace.
667                  */
668                 if (!acpi_ut_valid_acpi_char(name[i], i)) {
669                         new_name[i] = '*';
670                 }
671         }
672
673         return (*(u32 *) new_name);
674 }
675
676 /*******************************************************************************
677  *
678  * FUNCTION:    acpi_ut_strtoul64
679  *
680  * PARAMETERS:  String          - Null terminated string
681  *              Base            - Radix of the string: 16 or ACPI_ANY_BASE;
682  *                                ACPI_ANY_BASE means 'in behalf of to_integer'
683  *              ret_integer     - Where the converted integer is returned
684  *
685  * RETURN:      Status and Converted value
686  *
687  * DESCRIPTION: Convert a string into an unsigned value. Performs either a
688  *              32-bit or 64-bit conversion, depending on the current mode
689  *              of the interpreter.
690  *              NOTE: Does not support Octal strings, not needed.
691  *
692  ******************************************************************************/
693
694 acpi_status
695 acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
696 {
697         u32 this_digit = 0;
698         acpi_integer return_value = 0;
699         acpi_integer quotient;
700         acpi_integer dividend;
701         u32 to_integer_op = (base == ACPI_ANY_BASE);
702         u32 mode32 = (acpi_gbl_integer_byte_width == 4);
703         u8 valid_digits = 0;
704         u8 sign_of0x = 0;
705         u8 term = 0;
706
707         ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
708
709         switch (base) {
710         case ACPI_ANY_BASE:
711         case 16:
712                 break;
713
714         default:
715                 /* Invalid Base */
716                 return_ACPI_STATUS(AE_BAD_PARAMETER);
717         }
718
719         if (!string) {
720                 goto error_exit;
721         }
722
723         /* Skip over any white space in the buffer */
724
725         while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
726                 string++;
727         }
728
729         if (to_integer_op) {
730                 /*
731                  * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
732                  * We need to determine if it is decimal or hexadecimal.
733                  */
734                 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
735                         sign_of0x = 1;
736                         base = 16;
737
738                         /* Skip over the leading '0x' */
739                         string += 2;
740                 } else {
741                         base = 10;
742                 }
743         }
744
745         /* Any string left? Check that '0x' is not followed by white space. */
746
747         if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
748                 if (to_integer_op) {
749                         goto error_exit;
750                 } else {
751                         goto all_done;
752                 }
753         }
754
755         /*
756          * Perform a 32-bit or 64-bit conversion, depending upon the current
757          * execution mode of the interpreter
758          */
759         dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
760
761         /* Main loop: convert the string to a 32- or 64-bit integer */
762
763         while (*string) {
764                 if (ACPI_IS_DIGIT(*string)) {
765
766                         /* Convert ASCII 0-9 to Decimal value */
767
768                         this_digit = ((u8) * string) - '0';
769                 } else if (base == 10) {
770
771                         /* Digit is out of range; possible in to_integer case only */
772
773                         term = 1;
774                 } else {
775                         this_digit = (u8) ACPI_TOUPPER(*string);
776                         if (ACPI_IS_XDIGIT((char)this_digit)) {
777
778                                 /* Convert ASCII Hex char to value */
779
780                                 this_digit = this_digit - 'A' + 10;
781                         } else {
782                                 term = 1;
783                         }
784                 }
785
786                 if (term) {
787                         if (to_integer_op) {
788                                 goto error_exit;
789                         } else {
790                                 break;
791                         }
792                 } else if ((valid_digits == 0) && (this_digit == 0)
793                            && !sign_of0x) {
794
795                         /* Skip zeros */
796                         string++;
797                         continue;
798                 }
799
800                 valid_digits++;
801
802                 if (sign_of0x && ((valid_digits > 16)
803                                   || ((valid_digits > 8) && mode32))) {
804                         /*
805                          * This is to_integer operation case.
806                          * No any restrictions for string-to-integer conversion,
807                          * see ACPI spec.
808                          */
809                         goto error_exit;
810                 }
811
812                 /* Divide the digit into the correct position */
813
814                 (void)
815                     acpi_ut_short_divide((dividend - (acpi_integer) this_digit),
816                                          base, &quotient, NULL);
817
818                 if (return_value > quotient) {
819                         if (to_integer_op) {
820                                 goto error_exit;
821                         } else {
822                                 break;
823                         }
824                 }
825
826                 return_value *= base;
827                 return_value += this_digit;
828                 string++;
829         }
830
831         /* All done, normal exit */
832
833       all_done:
834
835         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
836                           ACPI_FORMAT_UINT64(return_value)));
837
838         *ret_integer = return_value;
839         return_ACPI_STATUS(AE_OK);
840
841       error_exit:
842         /* Base was set/validated above */
843
844         if (base == 10) {
845                 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
846         } else {
847                 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
848         }
849 }
850
851 /*******************************************************************************
852  *
853  * FUNCTION:    acpi_ut_create_update_state_and_push
854  *
855  * PARAMETERS:  Object          - Object to be added to the new state
856  *              Action          - Increment/Decrement
857  *              state_list      - List the state will be added to
858  *
859  * RETURN:      Status
860  *
861  * DESCRIPTION: Create a new state and push it
862  *
863  ******************************************************************************/
864
865 acpi_status
866 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
867                                      u16 action,
868                                      union acpi_generic_state **state_list)
869 {
870         union acpi_generic_state *state;
871
872         ACPI_FUNCTION_ENTRY();
873
874         /* Ignore null objects; these are expected */
875
876         if (!object) {
877                 return (AE_OK);
878         }
879
880         state = acpi_ut_create_update_state(object, action);
881         if (!state) {
882                 return (AE_NO_MEMORY);
883         }
884
885         acpi_ut_push_generic_state(state_list, state);
886         return (AE_OK);
887 }
888
889 /*******************************************************************************
890  *
891  * FUNCTION:    acpi_ut_walk_package_tree
892  *
893  * PARAMETERS:  source_object       - The package to walk
894  *              target_object       - Target object (if package is being copied)
895  *              walk_callback       - Called once for each package element
896  *              Context             - Passed to the callback function
897  *
898  * RETURN:      Status
899  *
900  * DESCRIPTION: Walk through a package
901  *
902  ******************************************************************************/
903
904 acpi_status
905 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
906                           void *target_object,
907                           acpi_pkg_callback walk_callback, void *context)
908 {
909         acpi_status status = AE_OK;
910         union acpi_generic_state *state_list = NULL;
911         union acpi_generic_state *state;
912         u32 this_index;
913         union acpi_operand_object *this_source_obj;
914
915         ACPI_FUNCTION_TRACE(ut_walk_package_tree);
916
917         state = acpi_ut_create_pkg_state(source_object, target_object, 0);
918         if (!state) {
919                 return_ACPI_STATUS(AE_NO_MEMORY);
920         }
921
922         while (state) {
923
924                 /* Get one element of the package */
925
926                 this_index = state->pkg.index;
927                 this_source_obj = (union acpi_operand_object *)
928                     state->pkg.source_object->package.elements[this_index];
929
930                 /*
931                  * Check for:
932                  * 1) An uninitialized package element.  It is completely
933                  *    legal to declare a package and leave it uninitialized
934                  * 2) Not an internal object - can be a namespace node instead
935                  * 3) Any type other than a package.  Packages are handled in else
936                  *    case below.
937                  */
938                 if ((!this_source_obj) ||
939                     (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
940                      ACPI_DESC_TYPE_OPERAND)
941                     || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
942                         ACPI_TYPE_PACKAGE)) {
943                         status =
944                             walk_callback(ACPI_COPY_TYPE_SIMPLE,
945                                           this_source_obj, state, context);
946                         if (ACPI_FAILURE(status)) {
947                                 return_ACPI_STATUS(status);
948                         }
949
950                         state->pkg.index++;
951                         while (state->pkg.index >=
952                                state->pkg.source_object->package.count) {
953                                 /*
954                                  * We've handled all of the objects at this level,  This means
955                                  * that we have just completed a package.  That package may
956                                  * have contained one or more packages itself.
957                                  *
958                                  * Delete this state and pop the previous state (package).
959                                  */
960                                 acpi_ut_delete_generic_state(state);
961                                 state = acpi_ut_pop_generic_state(&state_list);
962
963                                 /* Finished when there are no more states */
964
965                                 if (!state) {
966                                         /*
967                                          * We have handled all of the objects in the top level
968                                          * package just add the length of the package objects
969                                          * and exit
970                                          */
971                                         return_ACPI_STATUS(AE_OK);
972                                 }
973
974                                 /*
975                                  * Go back up a level and move the index past the just
976                                  * completed package object.
977                                  */
978                                 state->pkg.index++;
979                         }
980                 } else {
981                         /* This is a subobject of type package */
982
983                         status =
984                             walk_callback(ACPI_COPY_TYPE_PACKAGE,
985                                           this_source_obj, state, context);
986                         if (ACPI_FAILURE(status)) {
987                                 return_ACPI_STATUS(status);
988                         }
989
990                         /*
991                          * Push the current state and create a new one
992                          * The callback above returned a new target package object.
993                          */
994                         acpi_ut_push_generic_state(&state_list, state);
995                         state = acpi_ut_create_pkg_state(this_source_obj,
996                                                          state->pkg.
997                                                          this_target_obj, 0);
998                         if (!state) {
999
1000                                 /* Free any stacked Update State objects */
1001
1002                                 while (state_list) {
1003                                         state =
1004                                             acpi_ut_pop_generic_state
1005                                             (&state_list);
1006                                         acpi_ut_delete_generic_state(state);
1007                                 }
1008                                 return_ACPI_STATUS(AE_NO_MEMORY);
1009                         }
1010                 }
1011         }
1012
1013         /* We should never get here */
1014
1015         return_ACPI_STATUS(AE_AML_INTERNAL);
1016 }
1017
1018 /*******************************************************************************
1019  *
1020  * FUNCTION:    acpi_error, acpi_exception, acpi_warning, acpi_info
1021  *
1022  * PARAMETERS:  module_name         - Caller's module name (for error output)
1023  *              line_number         - Caller's line number (for error output)
1024  *              Format              - Printf format string + additional args
1025  *
1026  * RETURN:      None
1027  *
1028  * DESCRIPTION: Print message with module/line/version info
1029  *
1030  ******************************************************************************/
1031
1032 void ACPI_INTERNAL_VAR_XFACE
1033 acpi_error(const char *module_name, u32 line_number, const char *format, ...)
1034 {
1035         va_list args;
1036
1037         acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
1038
1039         va_start(args, format);
1040         acpi_os_vprintf(format, args);
1041         acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
1042         va_end(args);
1043 }
1044
1045 void ACPI_INTERNAL_VAR_XFACE
1046 acpi_exception(const char *module_name,
1047                u32 line_number, acpi_status status, const char *format, ...)
1048 {
1049         va_list args;
1050
1051         acpi_os_printf("ACPI Exception (%s-%04d): %s, ", module_name,
1052                        line_number, acpi_format_exception(status));
1053
1054         va_start(args, format);
1055         acpi_os_vprintf(format, args);
1056         acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
1057         va_end(args);
1058 }
1059
1060 void ACPI_INTERNAL_VAR_XFACE
1061 acpi_warning(const char *module_name, u32 line_number, const char *format, ...)
1062 {
1063         va_list args;
1064
1065         acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
1066
1067         va_start(args, format);
1068         acpi_os_vprintf(format, args);
1069         acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
1070         va_end(args);
1071 }
1072
1073 void ACPI_INTERNAL_VAR_XFACE
1074 acpi_info(const char *module_name, u32 line_number, const char *format, ...)
1075 {
1076         va_list args;
1077
1078         /*
1079          * Removed module_name, line_number, and acpica version, not needed
1080          * for info output
1081          */
1082         acpi_os_printf("ACPI: ");
1083
1084         va_start(args, format);
1085         acpi_os_vprintf(format, args);
1086         acpi_os_printf("\n");
1087         va_end(args);
1088 }
1089
1090 ACPI_EXPORT_SYMBOL(acpi_error)
1091 ACPI_EXPORT_SYMBOL(acpi_exception)
1092 ACPI_EXPORT_SYMBOL(acpi_warning)
1093 ACPI_EXPORT_SYMBOL(acpi_info)