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