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