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