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