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