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