blob: 0c5abc536c7ad1deb1f7db7e55c410ccf289a591 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acnamesp.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("utmisc")
Robert Moore44f6c012005-04-18 22:49:35 -040049
50/*******************************************************************************
51 *
Robert Mooref9f46012005-07-08 00:00:00 -040052 * FUNCTION: acpi_ut_allocate_owner_id
53 *
54 * PARAMETERS: owner_id - Where the new owner ID is returned
55 *
Robert Moore0c9938c2005-07-29 15:15:00 -070056 * RETURN: Status
57 *
58 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
59 * track objects created by the table or method, to be deleted
60 * when the method exits or the table is unloaded.
Robert Mooref9f46012005-07-08 00:00:00 -040061 *
62 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040063acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
Robert Mooref9f46012005-07-08 00:00:00 -040064{
Len Brown4be44fc2005-08-05 00:44:28 -040065 acpi_native_uint i;
66 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -040067
Len Brown4be44fc2005-08-05 00:44:28 -040068 ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
Robert Mooref9f46012005-07-08 00:00:00 -040069
Robert Mooreaff8c272005-09-02 17:24:17 -040070 /* Guard against multiple allocations of ID to the same location */
71
72 if (*owner_id) {
73 ACPI_REPORT_ERROR(("Owner ID [%2.2X] already exists\n",
74 *owner_id));
75 return_ACPI_STATUS(AE_ALREADY_EXISTS);
76 }
77
Robert Moore0c9938c2005-07-29 15:15:00 -070078 /* Mutex for the global ID mask */
79
Len Brown4be44fc2005-08-05 00:44:28 -040080 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
81 if (ACPI_FAILURE(status)) {
82 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -040083 }
84
85 /* Find a free owner ID */
86
87 for (i = 0; i < 32; i++) {
88 if (!(acpi_gbl_owner_id_mask & (1 << i))) {
Bob Moorea18ecf42005-08-15 03:42:00 -080089 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
90 "Current owner_id mask: %8.8X New ID: %2.2X\n",
Robert Mooreaff8c272005-09-02 17:24:17 -040091 acpi_gbl_owner_id_mask,
92 (unsigned int)(i + 1)));
Bob Moorea18ecf42005-08-15 03:42:00 -080093
Robert Mooref9f46012005-07-08 00:00:00 -040094 acpi_gbl_owner_id_mask |= (1 << i);
Robert Moore0c9938c2005-07-29 15:15:00 -070095 *owner_id = (acpi_owner_id) (i + 1);
Robert Mooref9f46012005-07-08 00:00:00 -040096 goto exit;
97 }
98 }
99
100 /*
101 * If we are here, all owner_ids have been allocated. This probably should
102 * not happen since the IDs are reused after deallocation. The IDs are
103 * allocated upon table load (one per table) and method execution, and
104 * they are released when a table is unloaded or a method completes
105 * execution.
106 */
Robert Moore0c9938c2005-07-29 15:15:00 -0700107 *owner_id = 0;
Robert Mooref9f46012005-07-08 00:00:00 -0400108 status = AE_OWNER_ID_LIMIT;
Len Brown4be44fc2005-08-05 00:44:28 -0400109 ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
Robert Mooref9f46012005-07-08 00:00:00 -0400110
Len Brown4be44fc2005-08-05 00:44:28 -0400111 exit:
112 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
113 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400114}
115
Robert Mooref9f46012005-07-08 00:00:00 -0400116/*******************************************************************************
117 *
118 * FUNCTION: acpi_ut_release_owner_id
119 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700120 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
Robert Mooref9f46012005-07-08 00:00:00 -0400121 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700122 * RETURN: None. No error is returned because we are either exiting a
123 * control method or unloading a table. Either way, we would
124 * ignore any error anyway.
125 *
126 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 32
Robert Mooref9f46012005-07-08 00:00:00 -0400127 *
128 ******************************************************************************/
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400131{
Len Brown4be44fc2005-08-05 00:44:28 -0400132 acpi_owner_id owner_id = *owner_id_ptr;
133 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400134
Bob Moorea18ecf42005-08-15 03:42:00 -0800135 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400136
Robert Moore0c9938c2005-07-29 15:15:00 -0700137 /* Always clear the input owner_id (zero is an invalid ID) */
138
139 *owner_id_ptr = 0;
140
141 /* Zero is not a valid owner_iD */
142
143 if ((owner_id == 0) || (owner_id > 32)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400144 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700145 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400146 }
147
Robert Moore0c9938c2005-07-29 15:15:00 -0700148 /* Mutex for the global ID mask */
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
151 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700152 return_VOID;
153 }
154
Robert Mooreaff8c272005-09-02 17:24:17 -0400155 /* Normalize the ID to zero */
156
157 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700158
159 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400160
161 if (acpi_gbl_owner_id_mask & (1 << owner_id)) {
162 acpi_gbl_owner_id_mask ^= (1 << owner_id);
163 }
Robert Mooref9f46012005-07-08 00:00:00 -0400164
Len Brown4be44fc2005-08-05 00:44:28 -0400165 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700166 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400167}
168
Robert Mooref9f46012005-07-08 00:00:00 -0400169/*******************************************************************************
170 *
Robert Moore44f6c012005-04-18 22:49:35 -0400171 * FUNCTION: acpi_ut_strupr (strupr)
172 *
173 * PARAMETERS: src_string - The source string to convert
174 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700175 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400176 *
177 * DESCRIPTION: Convert string to uppercase
178 *
179 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
180 *
181 ******************************************************************************/
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400186
Len Brown4be44fc2005-08-05 00:44:28 -0400187 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400188
Robert Moore73459f72005-06-24 00:00:00 -0400189 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700190 return;
Robert Moore73459f72005-06-24 00:00:00 -0400191 }
192
Robert Moore44f6c012005-04-18 22:49:35 -0400193 /* Walk entire string, uppercasing the letters */
194
195 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400197 }
198
Robert Moore0c9938c2005-07-29 15:15:00 -0700199 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*******************************************************************************
203 *
204 * FUNCTION: acpi_ut_print_string
205 *
206 * PARAMETERS: String - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400207 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * RETURN: None
210 *
211 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
212 * sequences.
213 *
214 ******************************************************************************/
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Len Brown4be44fc2005-08-05 00:44:28 -0400218 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400221 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return;
223 }
224
Len Brown4be44fc2005-08-05 00:44:28 -0400225 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 for (i = 0; string[i] && (i < max_length); i++) {
227 /* Escape sequences */
228
229 switch (string[i]) {
230 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400231 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
233
234 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400235 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237
238 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400239 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241
242 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245
246 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400247 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249
250 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400251 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
253
254 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400255 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258 case '\'': /* Single Quote */
259 case '\"': /* Double Quote */
260 case '\\': /* Backslash */
261 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263
264 default:
265
266 /* Check for printable character or hex escape */
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* This is a normal character */
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 acpi_os_printf("%c", (int)string[i]);
272 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* All others will be Hex escapes */
274
Len Brown4be44fc2005-08-05 00:44:28 -0400275 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 break;
278 }
279 }
Len Brown4be44fc2005-08-05 00:44:28 -0400280 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400283 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*******************************************************************************
288 *
289 * FUNCTION: acpi_ut_dword_byte_swap
290 *
291 * PARAMETERS: Value - Value to be converted
292 *
Robert Moore44f6c012005-04-18 22:49:35 -0400293 * RETURN: u32 integer with bytes swapped
294 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
296 *
297 ******************************************************************************/
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400302 u32 value;
303 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400306 u32 value;
307 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 } in;
309
Len Brown4be44fc2005-08-05 00:44:28 -0400310 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 in.value = value;
313
314 out.bytes[0] = in.bytes[3];
315 out.bytes[1] = in.bytes[2];
316 out.bytes[2] = in.bytes[1];
317 out.bytes[3] = in.bytes[0];
318
319 return (out.value);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*******************************************************************************
323 *
324 * FUNCTION: acpi_ut_set_integer_width
325 *
326 * PARAMETERS: Revision From DSDT header
327 *
328 * RETURN: None
329 *
330 * DESCRIPTION: Set the global integer bit width based upon the revision
331 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
332 * For Revision 2 and above, Integers are 64 bits. Yes, this
333 * makes a difference.
334 *
335 ******************************************************************************/
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339
340 if (revision <= 1) {
341 acpi_gbl_integer_bit_width = 32;
342 acpi_gbl_integer_nybble_width = 8;
343 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400344 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 acpi_gbl_integer_bit_width = 64;
346 acpi_gbl_integer_nybble_width = 16;
347 acpi_gbl_integer_byte_width = 8;
348 }
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#ifdef ACPI_DEBUG_OUTPUT
352/*******************************************************************************
353 *
354 * FUNCTION: acpi_ut_display_init_pathname
355 *
Robert Moore44f6c012005-04-18 22:49:35 -0400356 * PARAMETERS: Type - Object type of the node
357 * obj_handle - Handle whose pathname will be displayed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Path - Additional path string to be appended.
359 * (NULL if no extra path)
360 *
361 * RETURN: acpi_status
362 *
363 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
364 *
365 ******************************************************************************/
366
367void
Len Brown4be44fc2005-08-05 00:44:28 -0400368acpi_ut_display_init_pathname(u8 type,
369 struct acpi_namespace_node *obj_handle,
370 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Len Brown4be44fc2005-08-05 00:44:28 -0400372 acpi_status status;
373 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* Only print the path if the appropriate debug level is enabled */
378
379 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
380 return;
381 }
382
383 /* Get the full pathname to the node */
384
385 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400386 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
387 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return;
389 }
390
391 /* Print what we're doing */
392
393 switch (type) {
394 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400395 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397
398 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400399 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401 }
402
403 /* Print the object type and pathname */
404
Len Brown4be44fc2005-08-05 00:44:28 -0400405 acpi_os_printf("%-12s %s",
406 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Extra path is used to append names like _STA, _INI, etc. */
409
410 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400411 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Len Brown4be44fc2005-08-05 00:44:28 -0400413 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 ACPI_MEM_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417#endif
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*******************************************************************************
420 *
421 * FUNCTION: acpi_ut_valid_acpi_name
422 *
Robert Moore44f6c012005-04-18 22:49:35 -0400423 * PARAMETERS: Name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
Robert Moore44f6c012005-04-18 22:49:35 -0400425 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
427 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
428 * 1) Upper case alpha
429 * 2) numeric
430 * 3) underscore
431 *
432 ******************************************************************************/
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 char *name_ptr = (char *)&name;
437 char character;
438 acpi_native_uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Len Brown4be44fc2005-08-05 00:44:28 -0400440 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 for (i = 0; i < ACPI_NAME_SIZE; i++) {
443 character = *name_ptr;
444 name_ptr++;
445
446 if (!((character == '_') ||
Len Brown4be44fc2005-08-05 00:44:28 -0400447 (character >= 'A' && character <= 'Z') ||
448 (character >= '0' && character <= '9'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return (FALSE);
450 }
451 }
452
453 return (TRUE);
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*******************************************************************************
457 *
458 * FUNCTION: acpi_ut_valid_acpi_character
459 *
460 * PARAMETERS: Character - The character to be examined
461 *
462 * RETURN: 1 if Character may appear in a name, else 0
463 *
464 * DESCRIPTION: Check for a printable character
465 *
466 ******************************************************************************/
467
Len Brown4be44fc2005-08-05 00:44:28 -0400468u8 acpi_ut_valid_acpi_character(char character)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470
Len Brown4be44fc2005-08-05 00:44:28 -0400471 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 return ((u8) ((character == '_') ||
474 (character >= 'A' && character <= 'Z') ||
475 (character >= '0' && character <= '9')));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*******************************************************************************
479 *
480 * FUNCTION: acpi_ut_strtoul64
481 *
482 * PARAMETERS: String - Null terminated string
483 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
484 * ret_integer - Where the converted integer is returned
485 *
486 * RETURN: Status and Converted value
487 *
488 * DESCRIPTION: Convert a string into an unsigned value.
489 * NOTE: Does not support Octal strings, not needed.
490 *
491 ******************************************************************************/
492
493acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400494acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Len Brown4be44fc2005-08-05 00:44:28 -0400496 u32 this_digit = 0;
497 acpi_integer return_value = 0;
498 acpi_integer quotient;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Len Brown4be44fc2005-08-05 00:44:28 -0400500 ACPI_FUNCTION_TRACE("ut_stroul64");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if ((!string) || !(*string)) {
503 goto error_exit;
504 }
505
506 switch (base) {
507 case ACPI_ANY_BASE:
508 case 10:
509 case 16:
510 break;
511
512 default:
513 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400514 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 /* Skip over any white space in the buffer */
518
Len Brown4be44fc2005-08-05 00:44:28 -0400519 while (ACPI_IS_SPACE(*string) || *string == '\t') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 string++;
521 }
522
523 /*
524 * If the input parameter Base is zero, then we need to
525 * determine if it is decimal or hexadecimal:
526 */
527 if (base == 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400528 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 base = 16;
530 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 base = 10;
533 }
534 }
535
536 /*
537 * For hexadecimal base, skip over the leading
538 * 0 or 0x, if they are present.
539 */
540 if ((base == 16) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400541 (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 string += 2;
543 }
544
545 /* Any string left? */
546
547 if (!(*string)) {
548 goto error_exit;
549 }
550
551 /* Main loop: convert the string to a 64-bit integer */
552
553 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400554 if (ACPI_IS_DIGIT(*string)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Convert ASCII 0-9 to Decimal value */
556
Len Brown4be44fc2005-08-05 00:44:28 -0400557 this_digit = ((u8) * string) - '0';
558 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (base == 10) {
560 /* Digit is out of range */
561
562 goto error_exit;
563 }
564
Len Brown4be44fc2005-08-05 00:44:28 -0400565 this_digit = (u8) ACPI_TOUPPER(*string);
566 if (ACPI_IS_XDIGIT((char)this_digit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Convert ASCII Hex char to value */
568
569 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400570 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
572 * We allow non-hex chars, just stop now, same as end-of-string.
573 * See ACPI spec, string-to-integer conversion.
574 */
575 break;
576 }
577 }
578
579 /* Divide the digit into the correct position */
580
Len Brown4be44fc2005-08-05 00:44:28 -0400581 (void)
582 acpi_ut_short_divide((ACPI_INTEGER_MAX -
583 (acpi_integer) this_digit), base,
584 &quotient, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (return_value > quotient) {
586 goto error_exit;
587 }
588
589 return_value *= base;
590 return_value += this_digit;
591 string++;
592 }
593
594 /* All done, normal exit */
595
596 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400597 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Len Brown4be44fc2005-08-05 00:44:28 -0400599 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* Base was set/validated above */
601
602 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400603 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
604 } else {
605 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*******************************************************************************
610 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * FUNCTION: acpi_ut_create_update_state_and_push
612 *
Robert Moore44f6c012005-04-18 22:49:35 -0400613 * PARAMETERS: Object - Object to be added to the new state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * Action - Increment/Decrement
615 * state_list - List the state will be added to
616 *
Robert Moore44f6c012005-04-18 22:49:35 -0400617 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
619 * DESCRIPTION: Create a new state and push it
620 *
621 ******************************************************************************/
622
623acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400624acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
625 u16 action,
626 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Len Brown4be44fc2005-08-05 00:44:28 -0400628 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Len Brown4be44fc2005-08-05 00:44:28 -0400630 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* Ignore null objects; these are expected */
633
634 if (!object) {
635 return (AE_OK);
636 }
637
Len Brown4be44fc2005-08-05 00:44:28 -0400638 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!state) {
640 return (AE_NO_MEMORY);
641 }
642
Len Brown4be44fc2005-08-05 00:44:28 -0400643 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return (AE_OK);
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*******************************************************************************
648 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * FUNCTION: acpi_ut_walk_package_tree
650 *
Robert Moore44f6c012005-04-18 22:49:35 -0400651 * PARAMETERS: source_object - The package to walk
652 * target_object - Target object (if package is being copied)
653 * walk_callback - Called once for each package element
654 * Context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
656 * RETURN: Status
657 *
658 * DESCRIPTION: Walk through a package
659 *
660 ******************************************************************************/
661
662acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400663acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
664 void *target_object,
665 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Len Brown4be44fc2005-08-05 00:44:28 -0400667 acpi_status status = AE_OK;
668 union acpi_generic_state *state_list = NULL;
669 union acpi_generic_state *state;
670 u32 this_index;
671 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Len Brown4be44fc2005-08-05 00:44:28 -0400673 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Len Brown4be44fc2005-08-05 00:44:28 -0400675 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400677 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 while (state) {
681 /* Get one element of the package */
682
Len Brown4be44fc2005-08-05 00:44:28 -0400683 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400685 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /*
688 * Check for:
689 * 1) An uninitialized package element. It is completely
690 * legal to declare a package and leave it uninitialized
691 * 2) Not an internal object - can be a namespace node instead
692 * 3) Any type other than a package. Packages are handled in else
693 * case below.
694 */
695 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400696 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
697 ACPI_DESC_TYPE_OPERAND)
698 || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
699 ACPI_TYPE_PACKAGE)) {
700 status =
701 walk_callback(ACPI_COPY_TYPE_SIMPLE,
702 this_source_obj, state, context);
703 if (ACPI_FAILURE(status)) {
704 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -0400708 while (state->pkg.index >=
709 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * We've handled all of the objects at this level, This means
712 * that we have just completed a package. That package may
713 * have contained one or more packages itself.
714 *
715 * Delete this state and pop the previous state (package).
716 */
Len Brown4be44fc2005-08-05 00:44:28 -0400717 acpi_ut_delete_generic_state(state);
718 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Finished when there are no more states */
721
722 if (!state) {
723 /*
724 * We have handled all of the objects in the top level
725 * package just add the length of the package objects
726 * and exit
727 */
Len Brown4be44fc2005-08-05 00:44:28 -0400728 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 /*
732 * Go back up a level and move the index past the just
733 * completed package object.
734 */
735 state->pkg.index++;
736 }
Len Brown4be44fc2005-08-05 00:44:28 -0400737 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* This is a subobject of type package */
739
Len Brown4be44fc2005-08-05 00:44:28 -0400740 status =
741 walk_callback(ACPI_COPY_TYPE_PACKAGE,
742 this_source_obj, state, context);
743 if (ACPI_FAILURE(status)) {
744 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 /*
748 * Push the current state and create a new one
749 * The callback above returned a new target package object.
750 */
Len Brown4be44fc2005-08-05 00:44:28 -0400751 acpi_ut_push_generic_state(&state_list, state);
752 state = acpi_ut_create_pkg_state(this_source_obj,
753 state->pkg.
754 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400756 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 }
759 }
760
761 /* We should never get here */
762
Len Brown4be44fc2005-08-05 00:44:28 -0400763 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*******************************************************************************
767 *
768 * FUNCTION: acpi_ut_generate_checksum
769 *
770 * PARAMETERS: Buffer - Buffer to be scanned
771 * Length - number of bytes to examine
772 *
Robert Moore44f6c012005-04-18 22:49:35 -0400773 * RETURN: The generated checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * DESCRIPTION: Generate a checksum on a raw buffer
776 *
777 ******************************************************************************/
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779u8 acpi_ut_generate_checksum(u8 * buffer, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Len Brown4be44fc2005-08-05 00:44:28 -0400781 u32 i;
782 signed char sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 for (i = 0; i < length; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400785 sum = (signed char)(sum + buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 return ((u8) (0 - sum));
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*******************************************************************************
792 *
793 * FUNCTION: acpi_ut_get_resource_end_tag
794 *
795 * PARAMETERS: obj_desc - The resource template buffer object
796 *
797 * RETURN: Pointer to the end tag
798 *
799 * DESCRIPTION: Find the END_TAG resource descriptor in a resource template
800 *
801 ******************************************************************************/
802
Len Brown4be44fc2005-08-05 00:44:28 -0400803u8 *acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Len Brown4be44fc2005-08-05 00:44:28 -0400805 u8 buffer_byte;
806 u8 *buffer;
807 u8 *end_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Len Brown4be44fc2005-08-05 00:44:28 -0400809 buffer = obj_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 end_buffer = buffer + obj_desc->buffer.length;
811
812 while (buffer < end_buffer) {
813 buffer_byte = *buffer;
814 if (buffer_byte & ACPI_RDESC_TYPE_MASK) {
815 /* Large Descriptor - Length is next 2 bytes */
816
Len Brown4be44fc2005-08-05 00:44:28 -0400817 buffer += ((*(buffer + 1) | (*(buffer + 2) << 8)) + 3);
818 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Small Descriptor. End Tag will be found here */
820
Len Brown4be44fc2005-08-05 00:44:28 -0400821 if ((buffer_byte & ACPI_RDESC_SMALL_MASK) ==
822 ACPI_RDESC_TYPE_END_TAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Found the end tag descriptor, all done. */
824
825 return (buffer);
826 }
827
828 /* Length is in the header */
829
830 buffer += ((buffer_byte & 0x07) + 1);
831 }
832 }
833
834 /* End tag not found */
835
836 return (NULL);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/*******************************************************************************
840 *
841 * FUNCTION: acpi_ut_report_error
842 *
843 * PARAMETERS: module_name - Caller's module name (for error output)
844 * line_number - Caller's line number (for error output)
845 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
847 * RETURN: None
848 *
849 * DESCRIPTION: Print error message
850 *
851 ******************************************************************************/
852
Len Brown4be44fc2005-08-05 00:44:28 -0400853void acpi_ut_report_error(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855
Len Brown4be44fc2005-08-05 00:44:28 -0400856 acpi_os_printf("%8s-%04d: *** Error: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/*******************************************************************************
860 *
861 * FUNCTION: acpi_ut_report_warning
862 *
863 * PARAMETERS: module_name - Caller's module name (for error output)
864 * line_number - Caller's line number (for error output)
865 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
867 * RETURN: None
868 *
869 * DESCRIPTION: Print warning message
870 *
871 ******************************************************************************/
872
873void
Len Brown4be44fc2005-08-05 00:44:28 -0400874acpi_ut_report_warning(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876
Len Brown4be44fc2005-08-05 00:44:28 -0400877 acpi_os_printf("%8s-%04d: *** Warning: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*******************************************************************************
881 *
882 * FUNCTION: acpi_ut_report_info
883 *
884 * PARAMETERS: module_name - Caller's module name (for error output)
885 * line_number - Caller's line number (for error output)
886 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *
888 * RETURN: None
889 *
890 * DESCRIPTION: Print information message
891 *
892 ******************************************************************************/
893
Len Brown4be44fc2005-08-05 00:44:28 -0400894void acpi_ut_report_info(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896
Len Brown4be44fc2005-08-05 00:44:28 -0400897 acpi_os_printf("%8s-%04d: *** Info: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}