[ACPI] ACPICA 20051021
[powerpc.git] / drivers / acpi / utilities / utmisc.c
1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
46 #include <acpi/amlresrc.h>
47
48 #define _COMPONENT          ACPI_UTILITIES
49 ACPI_MODULE_NAME("utmisc")
50
51 /*******************************************************************************
52  *
53  * FUNCTION:    acpi_ut_allocate_owner_id
54  *
55  * PARAMETERS:  owner_id        - Where the new owner ID is returned
56  *
57  * RETURN:      Status
58  *
59  * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
60  *              track objects created by the table or method, to be deleted
61  *              when the method exits or the table is unloaded.
62  *
63  ******************************************************************************/
64 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
65 {
66         acpi_native_uint i;
67         acpi_status status;
68
69         ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
70
71         /* Guard against multiple allocations of ID to the same location */
72
73         if (*owner_id) {
74                 ACPI_REPORT_ERROR(("Owner ID [%2.2X] already exists\n",
75                                    *owner_id));
76                 return_ACPI_STATUS(AE_ALREADY_EXISTS);
77         }
78
79         /* Mutex for the global ID mask */
80
81         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
82         if (ACPI_FAILURE(status)) {
83                 return_ACPI_STATUS(status);
84         }
85
86         /* Find a free owner ID */
87
88         for (i = 0; i < 32; i++) {
89                 if (!(acpi_gbl_owner_id_mask & (1 << i))) {
90                         ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
91                                           "Current owner_id mask: %8.8X New ID: %2.2X\n",
92                                           acpi_gbl_owner_id_mask,
93                                           (unsigned int)(i + 1)));
94
95                         acpi_gbl_owner_id_mask |= (1 << i);
96                         *owner_id = (acpi_owner_id) (i + 1);
97                         goto exit;
98                 }
99         }
100
101         /*
102          * If we are here, all owner_ids have been allocated. This probably should
103          * not happen since the IDs are reused after deallocation. The IDs are
104          * allocated upon table load (one per table) and method execution, and
105          * they are released when a table is unloaded or a method completes
106          * execution.
107          */
108         *owner_id = 0;
109         status = AE_OWNER_ID_LIMIT;
110         ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
111
112       exit:
113         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
114         return_ACPI_STATUS(status);
115 }
116
117 /*******************************************************************************
118  *
119  * FUNCTION:    acpi_ut_release_owner_id
120  *
121  * PARAMETERS:  owner_id_ptr        - Pointer to a previously allocated owner_iD
122  *
123  * RETURN:      None. No error is returned because we are either exiting a
124  *              control method or unloading a table. Either way, we would
125  *              ignore any error anyway.
126  *
127  * DESCRIPTION: Release a table or method owner ID.  Valid IDs are 1 - 32
128  *
129  ******************************************************************************/
130
131 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
132 {
133         acpi_owner_id owner_id = *owner_id_ptr;
134         acpi_status status;
135
136         ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
137
138         /* Always clear the input owner_id (zero is an invalid ID) */
139
140         *owner_id_ptr = 0;
141
142         /* Zero is not a valid owner_iD */
143
144         if ((owner_id == 0) || (owner_id > 32)) {
145                 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id));
146                 return_VOID;
147         }
148
149         /* Mutex for the global ID mask */
150
151         status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
152         if (ACPI_FAILURE(status)) {
153                 return_VOID;
154         }
155
156         /* Normalize the ID to zero */
157
158         owner_id--;
159
160         /* Free the owner ID only if it is valid */
161
162         if (acpi_gbl_owner_id_mask & (1 << owner_id)) {
163                 acpi_gbl_owner_id_mask ^= (1 << owner_id);
164         }
165
166         (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
167         return_VOID;
168 }
169
170 /*******************************************************************************
171  *
172  * FUNCTION:    acpi_ut_strupr (strupr)
173  *
174  * PARAMETERS:  src_string      - The source string to convert
175  *
176  * RETURN:      None
177  *
178  * DESCRIPTION: Convert string to uppercase
179  *
180  * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
181  *
182  ******************************************************************************/
183
184 void acpi_ut_strupr(char *src_string)
185 {
186         char *string;
187
188         ACPI_FUNCTION_ENTRY();
189
190         if (!src_string) {
191                 return;
192         }
193
194         /* Walk entire string, uppercasing the letters */
195
196         for (string = src_string; *string; string++) {
197                 *string = (char)ACPI_TOUPPER(*string);
198         }
199
200         return;
201 }
202
203 /*******************************************************************************
204  *
205  * FUNCTION:    acpi_ut_print_string
206  *
207  * PARAMETERS:  String          - Null terminated ASCII string
208  *              max_length      - Maximum output length
209  *
210  * RETURN:      None
211  *
212  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
213  *              sequences.
214  *
215  ******************************************************************************/
216
217 void acpi_ut_print_string(char *string, u8 max_length)
218 {
219         u32 i;
220
221         if (!string) {
222                 acpi_os_printf("<\"NULL STRING PTR\">");
223                 return;
224         }
225
226         acpi_os_printf("\"");
227         for (i = 0; string[i] && (i < max_length); i++) {
228                 /* Escape sequences */
229
230                 switch (string[i]) {
231                 case 0x07:
232                         acpi_os_printf("\\a");  /* BELL */
233                         break;
234
235                 case 0x08:
236                         acpi_os_printf("\\b");  /* BACKSPACE */
237                         break;
238
239                 case 0x0C:
240                         acpi_os_printf("\\f");  /* FORMFEED */
241                         break;
242
243                 case 0x0A:
244                         acpi_os_printf("\\n");  /* LINEFEED */
245                         break;
246
247                 case 0x0D:
248                         acpi_os_printf("\\r");  /* CARRIAGE RETURN */
249                         break;
250
251                 case 0x09:
252                         acpi_os_printf("\\t");  /* HORIZONTAL TAB */
253                         break;
254
255                 case 0x0B:
256                         acpi_os_printf("\\v");  /* VERTICAL TAB */
257                         break;
258
259                 case '\'':      /* Single Quote */
260                 case '\"':      /* Double Quote */
261                 case '\\':      /* Backslash */
262                         acpi_os_printf("\\%c", (int)string[i]);
263                         break;
264
265                 default:
266
267                         /* Check for printable character or hex escape */
268
269                         if (ACPI_IS_PRINT(string[i])) {
270                                 /* This is a normal character */
271
272                                 acpi_os_printf("%c", (int)string[i]);
273                         } else {
274                                 /* All others will be Hex escapes */
275
276                                 acpi_os_printf("\\x%2.2X", (s32) string[i]);
277                         }
278                         break;
279                 }
280         }
281         acpi_os_printf("\"");
282
283         if (i == max_length && string[i]) {
284                 acpi_os_printf("...");
285         }
286 }
287
288 /*******************************************************************************
289  *
290  * FUNCTION:    acpi_ut_dword_byte_swap
291  *
292  * PARAMETERS:  Value           - Value to be converted
293  *
294  * RETURN:      u32 integer with bytes swapped
295  *
296  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
297  *
298  ******************************************************************************/
299
300 u32 acpi_ut_dword_byte_swap(u32 value)
301 {
302         union {
303                 u32 value;
304                 u8 bytes[4];
305         } out;
306         union {
307                 u32 value;
308                 u8 bytes[4];
309         } in;
310
311         ACPI_FUNCTION_ENTRY();
312
313         in.value = value;
314
315         out.bytes[0] = in.bytes[3];
316         out.bytes[1] = in.bytes[2];
317         out.bytes[2] = in.bytes[1];
318         out.bytes[3] = in.bytes[0];
319
320         return (out.value);
321 }
322
323 /*******************************************************************************
324  *
325  * FUNCTION:    acpi_ut_set_integer_width
326  *
327  * PARAMETERS:  Revision            From DSDT header
328  *
329  * RETURN:      None
330  *
331  * DESCRIPTION: Set the global integer bit width based upon the revision
332  *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
333  *              For Revision 2 and above, Integers are 64 bits.  Yes, this
334  *              makes a difference.
335  *
336  ******************************************************************************/
337
338 void acpi_ut_set_integer_width(u8 revision)
339 {
340
341         if (revision <= 1) {
342                 acpi_gbl_integer_bit_width = 32;
343                 acpi_gbl_integer_nybble_width = 8;
344                 acpi_gbl_integer_byte_width = 4;
345         } else {
346                 acpi_gbl_integer_bit_width = 64;
347                 acpi_gbl_integer_nybble_width = 16;
348                 acpi_gbl_integer_byte_width = 8;
349         }
350 }
351
352 #ifdef ACPI_DEBUG_OUTPUT
353 /*******************************************************************************
354  *
355  * FUNCTION:    acpi_ut_display_init_pathname
356  *
357  * PARAMETERS:  Type                - Object type of the node
358  *              obj_handle          - Handle whose pathname will be displayed
359  *              Path                - Additional path string to be appended.
360  *                                      (NULL if no extra path)
361  *
362  * RETURN:      acpi_status
363  *
364  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
365  *
366  ******************************************************************************/
367
368 void
369 acpi_ut_display_init_pathname(u8 type,
370                               struct acpi_namespace_node *obj_handle,
371                               char *path)
372 {
373         acpi_status status;
374         struct acpi_buffer buffer;
375
376         ACPI_FUNCTION_ENTRY();
377
378         /* Only print the path if the appropriate debug level is enabled */
379
380         if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
381                 return;
382         }
383
384         /* Get the full pathname to the node */
385
386         buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
387         status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
388         if (ACPI_FAILURE(status)) {
389                 return;
390         }
391
392         /* Print what we're doing */
393
394         switch (type) {
395         case ACPI_TYPE_METHOD:
396                 acpi_os_printf("Executing  ");
397                 break;
398
399         default:
400                 acpi_os_printf("Initializing ");
401                 break;
402         }
403
404         /* Print the object type and pathname */
405
406         acpi_os_printf("%-12s %s",
407                        acpi_ut_get_type_name(type), (char *)buffer.pointer);
408
409         /* Extra path is used to append names like _STA, _INI, etc. */
410
411         if (path) {
412                 acpi_os_printf(".%s", path);
413         }
414         acpi_os_printf("\n");
415
416         ACPI_MEM_FREE(buffer.pointer);
417 }
418 #endif
419
420 /*******************************************************************************
421  *
422  * FUNCTION:    acpi_ut_valid_acpi_name
423  *
424  * PARAMETERS:  Name            - The name to be examined
425  *
426  * RETURN:      TRUE if the name is valid, FALSE otherwise
427  *
428  * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
429  *              1) Upper case alpha
430  *              2) numeric
431  *              3) underscore
432  *
433  ******************************************************************************/
434
435 u8 acpi_ut_valid_acpi_name(u32 name)
436 {
437         char *name_ptr = (char *)&name;
438         char character;
439         acpi_native_uint i;
440
441         ACPI_FUNCTION_ENTRY();
442
443         for (i = 0; i < ACPI_NAME_SIZE; i++) {
444                 character = *name_ptr;
445                 name_ptr++;
446
447                 if (!((character == '_') ||
448                       (character >= 'A' && character <= 'Z') ||
449                       (character >= '0' && character <= '9'))) {
450                         return (FALSE);
451                 }
452         }
453
454         return (TRUE);
455 }
456
457 /*******************************************************************************
458  *
459  * FUNCTION:    acpi_ut_valid_acpi_character
460  *
461  * PARAMETERS:  Character           - The character to be examined
462  *
463  * RETURN:      1 if Character may appear in a name, else 0
464  *
465  * DESCRIPTION: Check for a printable character
466  *
467  ******************************************************************************/
468
469 u8 acpi_ut_valid_acpi_character(char character)
470 {
471
472         ACPI_FUNCTION_ENTRY();
473
474         return ((u8) ((character == '_') ||
475                       (character >= 'A' && character <= 'Z') ||
476                       (character >= '0' && character <= '9')));
477 }
478
479 /*******************************************************************************
480  *
481  * FUNCTION:    acpi_ut_strtoul64
482  *
483  * PARAMETERS:  String          - Null terminated string
484  *              Base            - Radix of the string: 10, 16, or ACPI_ANY_BASE
485  *              ret_integer     - Where the converted integer is returned
486  *
487  * RETURN:      Status and Converted value
488  *
489  * DESCRIPTION: Convert a string into an unsigned value.
490  *              NOTE: Does not support Octal strings, not needed.
491  *
492  ******************************************************************************/
493
494 acpi_status
495 acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
496 {
497         u32 this_digit = 0;
498         acpi_integer return_value = 0;
499         acpi_integer quotient;
500
501         ACPI_FUNCTION_TRACE("ut_stroul64");
502
503         if ((!string) || !(*string)) {
504                 goto error_exit;
505         }
506
507         switch (base) {
508         case ACPI_ANY_BASE:
509         case 10:
510         case 16:
511                 break;
512
513         default:
514                 /* Invalid Base */
515                 return_ACPI_STATUS(AE_BAD_PARAMETER);
516         }
517
518         /* Skip over any white space in the buffer */
519
520         while (ACPI_IS_SPACE(*string) || *string == '\t') {
521                 string++;
522         }
523
524         /*
525          * If the input parameter Base is zero, then we need to
526          * determine if it is decimal or hexadecimal:
527          */
528         if (base == 0) {
529                 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
530                         base = 16;
531                         string += 2;
532                 } else {
533                         base = 10;
534                 }
535         }
536
537         /*
538          * For hexadecimal base, skip over the leading
539          * 0 or 0x, if they are present.
540          */
541         if ((base == 16) &&
542             (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
543                 string += 2;
544         }
545
546         /* Any string left? */
547
548         if (!(*string)) {
549                 goto error_exit;
550         }
551
552         /* Main loop: convert the string to a 64-bit integer */
553
554         while (*string) {
555                 if (ACPI_IS_DIGIT(*string)) {
556                         /* Convert ASCII 0-9 to Decimal value */
557
558                         this_digit = ((u8) * string) - '0';
559                 } else {
560                         if (base == 10) {
561                                 /* Digit is out of range */
562
563                                 goto error_exit;
564                         }
565
566                         this_digit = (u8) ACPI_TOUPPER(*string);
567                         if (ACPI_IS_XDIGIT((char)this_digit)) {
568                                 /* Convert ASCII Hex char to value */
569
570                                 this_digit = this_digit - 'A' + 10;
571                         } else {
572                                 /*
573                                  * We allow non-hex chars, just stop now, same as end-of-string.
574                                  * See ACPI spec, string-to-integer conversion.
575                                  */
576                                 break;
577                         }
578                 }
579
580                 /* Divide the digit into the correct position */
581
582                 (void)
583                     acpi_ut_short_divide((ACPI_INTEGER_MAX -
584                                           (acpi_integer) this_digit), base,
585                                          &quotient, NULL);
586                 if (return_value > quotient) {
587                         goto error_exit;
588                 }
589
590                 return_value *= base;
591                 return_value += this_digit;
592                 string++;
593         }
594
595         /* All done, normal exit */
596
597         *ret_integer = return_value;
598         return_ACPI_STATUS(AE_OK);
599
600       error_exit:
601         /* Base was set/validated above */
602
603         if (base == 10) {
604                 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
605         } else {
606                 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
607         }
608 }
609
610 /*******************************************************************************
611  *
612  * FUNCTION:    acpi_ut_create_update_state_and_push
613  *
614  * PARAMETERS:  Object          - Object to be added to the new state
615  *              Action          - Increment/Decrement
616  *              state_list      - List the state will be added to
617  *
618  * RETURN:      Status
619  *
620  * DESCRIPTION: Create a new state and push it
621  *
622  ******************************************************************************/
623
624 acpi_status
625 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
626                                      u16 action,
627                                      union acpi_generic_state **state_list)
628 {
629         union acpi_generic_state *state;
630
631         ACPI_FUNCTION_ENTRY();
632
633         /* Ignore null objects; these are expected */
634
635         if (!object) {
636                 return (AE_OK);
637         }
638
639         state = acpi_ut_create_update_state(object, action);
640         if (!state) {
641                 return (AE_NO_MEMORY);
642         }
643
644         acpi_ut_push_generic_state(state_list, state);
645         return (AE_OK);
646 }
647
648 /*******************************************************************************
649  *
650  * FUNCTION:    acpi_ut_walk_package_tree
651  *
652  * PARAMETERS:  source_object       - The package to walk
653  *              target_object       - Target object (if package is being copied)
654  *              walk_callback       - Called once for each package element
655  *              Context             - Passed to the callback function
656  *
657  * RETURN:      Status
658  *
659  * DESCRIPTION: Walk through a package
660  *
661  ******************************************************************************/
662
663 acpi_status
664 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
665                           void *target_object,
666                           acpi_pkg_callback walk_callback, void *context)
667 {
668         acpi_status status = AE_OK;
669         union acpi_generic_state *state_list = NULL;
670         union acpi_generic_state *state;
671         u32 this_index;
672         union acpi_operand_object *this_source_obj;
673
674         ACPI_FUNCTION_TRACE("ut_walk_package_tree");
675
676         state = acpi_ut_create_pkg_state(source_object, target_object, 0);
677         if (!state) {
678                 return_ACPI_STATUS(AE_NO_MEMORY);
679         }
680
681         while (state) {
682                 /* Get one element of the package */
683
684                 this_index = state->pkg.index;
685                 this_source_obj = (union acpi_operand_object *)
686                     state->pkg.source_object->package.elements[this_index];
687
688                 /*
689                  * Check for:
690                  * 1) An uninitialized package element.  It is completely
691                  *    legal to declare a package and leave it uninitialized
692                  * 2) Not an internal object - can be a namespace node instead
693                  * 3) Any type other than a package.  Packages are handled in else
694                  *    case below.
695                  */
696                 if ((!this_source_obj) ||
697                     (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
698                      ACPI_DESC_TYPE_OPERAND)
699                     || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
700                         ACPI_TYPE_PACKAGE)) {
701                         status =
702                             walk_callback(ACPI_COPY_TYPE_SIMPLE,
703                                           this_source_obj, state, context);
704                         if (ACPI_FAILURE(status)) {
705                                 return_ACPI_STATUS(status);
706                         }
707
708                         state->pkg.index++;
709                         while (state->pkg.index >=
710                                state->pkg.source_object->package.count) {
711                                 /*
712                                  * We've handled all of the objects at this level,  This means
713                                  * that we have just completed a package.  That package may
714                                  * have contained one or more packages itself.
715                                  *
716                                  * Delete this state and pop the previous state (package).
717                                  */
718                                 acpi_ut_delete_generic_state(state);
719                                 state = acpi_ut_pop_generic_state(&state_list);
720
721                                 /* Finished when there are no more states */
722
723                                 if (!state) {
724                                         /*
725                                          * We have handled all of the objects in the top level
726                                          * package just add the length of the package objects
727                                          * and exit
728                                          */
729                                         return_ACPI_STATUS(AE_OK);
730                                 }
731
732                                 /*
733                                  * Go back up a level and move the index past the just
734                                  * completed package object.
735                                  */
736                                 state->pkg.index++;
737                         }
738                 } else {
739                         /* This is a subobject of type package */
740
741                         status =
742                             walk_callback(ACPI_COPY_TYPE_PACKAGE,
743                                           this_source_obj, state, context);
744                         if (ACPI_FAILURE(status)) {
745                                 return_ACPI_STATUS(status);
746                         }
747
748                         /*
749                          * Push the current state and create a new one
750                          * The callback above returned a new target package object.
751                          */
752                         acpi_ut_push_generic_state(&state_list, state);
753                         state = acpi_ut_create_pkg_state(this_source_obj,
754                                                          state->pkg.
755                                                          this_target_obj, 0);
756                         if (!state) {
757                                 return_ACPI_STATUS(AE_NO_MEMORY);
758                         }
759                 }
760         }
761
762         /* We should never get here */
763
764         return_ACPI_STATUS(AE_AML_INTERNAL);
765 }
766
767 /*******************************************************************************
768  *
769  * FUNCTION:    acpi_ut_generate_checksum
770  *
771  * PARAMETERS:  Buffer          - Buffer to be scanned
772  *              Length          - number of bytes to examine
773  *
774  * RETURN:      The generated checksum
775  *
776  * DESCRIPTION: Generate a checksum on a raw buffer
777  *
778  ******************************************************************************/
779
780 u8 acpi_ut_generate_checksum(u8 * buffer, u32 length)
781 {
782         u32 i;
783         signed char sum = 0;
784
785         for (i = 0; i < length; i++) {
786                 sum = (signed char)(sum + buffer[i]);
787         }
788
789         return ((u8) (0 - sum));
790 }
791
792 /*******************************************************************************
793  *
794  * FUNCTION:    acpi_ut_get_resource_type
795  *
796  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
797  *
798  * RETURN:      The Resource Type with no extraneous bits (except the
799  *              Large/Small descriptor bit -- this is left alone)
800  *
801  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
802  *              a resource descriptor.
803  *
804  ******************************************************************************/
805
806 u8 acpi_ut_get_resource_type(void *aml)
807 {
808         ACPI_FUNCTION_ENTRY();
809
810         /*
811          * Byte 0 contains the descriptor name (Resource Type)
812          * Determine if this is a small or large resource
813          */
814         if (*((u8 *) aml) & ACPI_RESOURCE_NAME_LARGE) {
815                 /* Large Resource Type -- bits 6:0 contain the name */
816
817                 return (*((u8 *) aml));
818         } else {
819                 /* Small Resource Type -- bits 6:3 contain the name */
820
821                 return ((u8) (*((u8 *) aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
822         }
823 }
824
825 /*******************************************************************************
826  *
827  * FUNCTION:    acpi_ut_get_resource_length
828  *
829  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
830  *
831  * RETURN:      Byte Length
832  *
833  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
834  *              definition, this does not include the size of the descriptor
835  *              header or the length field itself.
836  *
837  ******************************************************************************/
838
839 u16 acpi_ut_get_resource_length(void *aml)
840 {
841         u16 resource_length;
842
843         ACPI_FUNCTION_ENTRY();
844
845         /*
846          * Byte 0 contains the descriptor name (Resource Type)
847          * Determine if this is a small or large resource
848          */
849         if (*((u8 *) aml) & ACPI_RESOURCE_NAME_LARGE) {
850                 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
851
852                 ACPI_MOVE_16_TO_16(&resource_length, &((u8 *) aml)[1]);
853
854         } else {
855                 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
856
857                 resource_length = (u16) (*((u8 *) aml) &
858                                          ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
859         }
860
861         return (resource_length);
862 }
863
864 /*******************************************************************************
865  *
866  * FUNCTION:    acpi_ut_get_descriptor_length
867  *
868  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
869  *
870  * RETURN:      Byte length
871  *
872  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
873  *              length of the descriptor header and the length field itself.
874  *              Used to walk descriptor lists.
875  *
876  ******************************************************************************/
877
878 u32 acpi_ut_get_descriptor_length(void *aml)
879 {
880         u32 descriptor_length;
881
882         ACPI_FUNCTION_ENTRY();
883
884         /* First get the Resource Length (Does not include header length) */
885
886         descriptor_length = acpi_ut_get_resource_length(aml);
887
888         /* Determine if this is a small or large resource */
889
890         if (*((u8 *) aml) & ACPI_RESOURCE_NAME_LARGE) {
891                 descriptor_length += sizeof(struct aml_resource_large_header);
892         } else {
893                 descriptor_length += sizeof(struct aml_resource_small_header);
894         }
895
896         return (descriptor_length);
897 }
898
899 /*******************************************************************************
900  *
901  * FUNCTION:    acpi_ut_get_resource_end_tag
902  *
903  * PARAMETERS:  obj_desc        - The resource template buffer object
904  *
905  * RETURN:      Pointer to the end tag
906  *
907  * DESCRIPTION: Find the END_TAG resource descriptor in an AML resource template
908  *
909  ******************************************************************************/
910
911 u8 *acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc)
912 {
913         u8 *aml;
914         u8 *end_aml;
915
916         aml = obj_desc->buffer.pointer;
917         end_aml = aml + obj_desc->buffer.length;
918
919         /* Walk the resource template, one descriptor per loop */
920
921         while (aml < end_aml) {
922                 if (acpi_ut_get_resource_type(aml) ==
923                     ACPI_RESOURCE_NAME_END_TAG) {
924                         /* Found the end_tag descriptor, all done */
925
926                         return (aml);
927                 }
928
929                 /* Point to the next resource descriptor */
930
931                 aml += acpi_ut_get_resource_length(aml);
932         }
933
934         /* End tag was not found */
935
936         return (NULL);
937 }
938
939 /*******************************************************************************
940  *
941  * FUNCTION:    acpi_ut_report_error
942  *
943  * PARAMETERS:  module_name         - Caller's module name (for error output)
944  *              line_number         - Caller's line number (for error output)
945  *              component_id        - Caller's component ID (for error output)
946  *
947  * RETURN:      None
948  *
949  * DESCRIPTION: Print error message
950  *
951  ******************************************************************************/
952
953 void acpi_ut_report_error(char *module_name, u32 line_number, u32 component_id)
954 {
955
956         acpi_os_printf("%8s-%04d: *** Error: ", module_name, line_number);
957 }
958
959 /*******************************************************************************
960  *
961  * FUNCTION:    acpi_ut_report_warning
962  *
963  * PARAMETERS:  module_name         - Caller's module name (for error output)
964  *              line_number         - Caller's line number (for error output)
965  *              component_id        - Caller's component ID (for error output)
966  *
967  * RETURN:      None
968  *
969  * DESCRIPTION: Print warning message
970  *
971  ******************************************************************************/
972
973 void
974 acpi_ut_report_warning(char *module_name, u32 line_number, u32 component_id)
975 {
976
977         acpi_os_printf("%8s-%04d: *** Warning: ", module_name, line_number);
978 }
979
980 /*******************************************************************************
981  *
982  * FUNCTION:    acpi_ut_report_info
983  *
984  * PARAMETERS:  module_name         - Caller's module name (for error output)
985  *              line_number         - Caller's line number (for error output)
986  *              component_id        - Caller's component ID (for error output)
987  *
988  * RETURN:      None
989  *
990  * DESCRIPTION: Print information message
991  *
992  ******************************************************************************/
993
994 void acpi_ut_report_info(char *module_name, u32 line_number, u32 component_id)
995 {
996
997         acpi_os_printf("%8s-%04d: *** Info: ", module_name, line_number);
998 }