blob: 89efba7bf44948dab2567a38091f9be086f37183 [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;
Bob Moorec51a4de2005-11-17 13:07:00 -050066 acpi_native_uint j;
Len Brown4be44fc2005-08-05 00:44:28 -040067 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -040068
Len Brown4be44fc2005-08-05 00:44:28 -040069 ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
Robert Mooref9f46012005-07-08 00:00:00 -040070
Robert Mooreaff8c272005-09-02 17:24:17 -040071 /* 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
Robert Moore0c9938c2005-07-29 15:15:00 -070079 /* Mutex for the global ID mask */
80
Len Brown4be44fc2005-08-05 00:44:28 -040081 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
82 if (ACPI_FAILURE(status)) {
83 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -040084 }
85
Bob Moorec51a4de2005-11-17 13:07:00 -050086 /*
87 * Find a free owner ID, cycle through all possible IDs on repeated
88 * allocations. Note: Index for next possible ID is equal to the value
89 * of the last allocated ID.
90 */
91 for (i = 0, j = acpi_gbl_last_owner_id; i < 32; i++, j++) {
92 if (j >= 32) {
93 j = 0; /* Wraparound to ID start */
94 }
Robert Mooref9f46012005-07-08 00:00:00 -040095
Bob Moorec51a4de2005-11-17 13:07:00 -050096 if (!(acpi_gbl_owner_id_mask & (1 << j))) {
97 /*
98 * Found a free ID. The actual ID is the bit index plus one,
99 * making zero an invalid Owner ID. Save this as the last ID
100 * allocated and update the global ID mask.
101 */
102 acpi_gbl_last_owner_id = (acpi_owner_id) (j + 1);
103 *owner_id = acpi_gbl_last_owner_id;
104
Bob Moorea18ecf42005-08-15 03:42:00 -0800105 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
106 "Current owner_id mask: %8.8X New ID: %2.2X\n",
Robert Mooreaff8c272005-09-02 17:24:17 -0400107 acpi_gbl_owner_id_mask,
Bob Moorec51a4de2005-11-17 13:07:00 -0500108 (unsigned int)
109 acpi_gbl_last_owner_id));
Bob Moorea18ecf42005-08-15 03:42:00 -0800110
Bob Moorec51a4de2005-11-17 13:07:00 -0500111 acpi_gbl_owner_id_mask |= (1 << j);
Robert Mooref9f46012005-07-08 00:00:00 -0400112 goto exit;
113 }
114 }
115
116 /*
Bob Moorec51a4de2005-11-17 13:07:00 -0500117 * All owner_ids have been allocated. This typically should
Robert Mooref9f46012005-07-08 00:00:00 -0400118 * not happen since the IDs are reused after deallocation. The IDs are
119 * allocated upon table load (one per table) and method execution, and
120 * they are released when a table is unloaded or a method completes
121 * execution.
Bob Moorec51a4de2005-11-17 13:07:00 -0500122 *
123 * If this error happens, there may be very deep nesting of invoked control
124 * methods, or there may be a bug where the IDs are not released.
Robert Mooref9f46012005-07-08 00:00:00 -0400125 */
126 status = AE_OWNER_ID_LIMIT;
Len Brown4be44fc2005-08-05 00:44:28 -0400127 ACPI_REPORT_ERROR(("Could not allocate new owner_id (32 max), AE_OWNER_ID_LIMIT\n"));
Robert Mooref9f46012005-07-08 00:00:00 -0400128
Len Brown4be44fc2005-08-05 00:44:28 -0400129 exit:
130 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
131 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400132}
133
Robert Mooref9f46012005-07-08 00:00:00 -0400134/*******************************************************************************
135 *
136 * FUNCTION: acpi_ut_release_owner_id
137 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700138 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
Robert Mooref9f46012005-07-08 00:00:00 -0400139 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700140 * RETURN: None. No error is returned because we are either exiting a
141 * control method or unloading a table. Either way, we would
142 * ignore any error anyway.
143 *
144 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 32
Robert Mooref9f46012005-07-08 00:00:00 -0400145 *
146 ******************************************************************************/
147
Len Brown4be44fc2005-08-05 00:44:28 -0400148void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400149{
Len Brown4be44fc2005-08-05 00:44:28 -0400150 acpi_owner_id owner_id = *owner_id_ptr;
151 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400152
Bob Moorea18ecf42005-08-15 03:42:00 -0800153 ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400154
Robert Moore0c9938c2005-07-29 15:15:00 -0700155 /* Always clear the input owner_id (zero is an invalid ID) */
156
157 *owner_id_ptr = 0;
158
159 /* Zero is not a valid owner_iD */
160
161 if ((owner_id == 0) || (owner_id > 32)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400162 ACPI_REPORT_ERROR(("Invalid owner_id: %2.2X\n", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700163 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400164 }
165
Robert Moore0c9938c2005-07-29 15:15:00 -0700166 /* Mutex for the global ID mask */
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
169 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700170 return_VOID;
171 }
172
Robert Mooreaff8c272005-09-02 17:24:17 -0400173 /* Normalize the ID to zero */
174
175 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700176
177 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400178
179 if (acpi_gbl_owner_id_mask & (1 << owner_id)) {
180 acpi_gbl_owner_id_mask ^= (1 << owner_id);
181 }
Robert Mooref9f46012005-07-08 00:00:00 -0400182
Len Brown4be44fc2005-08-05 00:44:28 -0400183 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700184 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400185}
186
Robert Mooref9f46012005-07-08 00:00:00 -0400187/*******************************************************************************
188 *
Robert Moore44f6c012005-04-18 22:49:35 -0400189 * FUNCTION: acpi_ut_strupr (strupr)
190 *
191 * PARAMETERS: src_string - The source string to convert
192 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700193 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400194 *
195 * DESCRIPTION: Convert string to uppercase
196 *
197 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
198 *
199 ******************************************************************************/
200
Len Brown4be44fc2005-08-05 00:44:28 -0400201void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400202{
Len Brown4be44fc2005-08-05 00:44:28 -0400203 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400204
Len Brown4be44fc2005-08-05 00:44:28 -0400205 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400206
Robert Moore73459f72005-06-24 00:00:00 -0400207 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700208 return;
Robert Moore73459f72005-06-24 00:00:00 -0400209 }
210
Robert Moore44f6c012005-04-18 22:49:35 -0400211 /* Walk entire string, uppercasing the letters */
212
213 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400214 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400215 }
216
Robert Moore0c9938c2005-07-29 15:15:00 -0700217 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*******************************************************************************
221 *
222 * FUNCTION: acpi_ut_print_string
223 *
224 * PARAMETERS: String - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400225 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * RETURN: None
228 *
229 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
230 * sequences.
231 *
232 ******************************************************************************/
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Len Brown4be44fc2005-08-05 00:44:28 -0400236 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400239 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return;
241 }
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 for (i = 0; string[i] && (i < max_length); i++) {
245 /* Escape sequences */
246
247 switch (string[i]) {
248 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400249 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
252 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400253 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255
256 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400257 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259
260 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400261 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263
264 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267
268 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400269 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271
272 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400273 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276 case '\'': /* Single Quote */
277 case '\"': /* Double Quote */
278 case '\\': /* Backslash */
279 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281
282 default:
283
284 /* Check for printable character or hex escape */
285
Len Brown4be44fc2005-08-05 00:44:28 -0400286 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* This is a normal character */
288
Len Brown4be44fc2005-08-05 00:44:28 -0400289 acpi_os_printf("%c", (int)string[i]);
290 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* All others will be Hex escapes */
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 break;
296 }
297 }
Len Brown4be44fc2005-08-05 00:44:28 -0400298 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400301 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*******************************************************************************
306 *
307 * FUNCTION: acpi_ut_dword_byte_swap
308 *
309 * PARAMETERS: Value - Value to be converted
310 *
Robert Moore44f6c012005-04-18 22:49:35 -0400311 * RETURN: u32 integer with bytes swapped
312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
314 *
315 ******************************************************************************/
316
Len Brown4be44fc2005-08-05 00:44:28 -0400317u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400320 u32 value;
321 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400324 u32 value;
325 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 } in;
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 in.value = value;
331
332 out.bytes[0] = in.bytes[3];
333 out.bytes[1] = in.bytes[2];
334 out.bytes[2] = in.bytes[1];
335 out.bytes[3] = in.bytes[0];
336
337 return (out.value);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*******************************************************************************
341 *
342 * FUNCTION: acpi_ut_set_integer_width
343 *
344 * PARAMETERS: Revision From DSDT header
345 *
346 * RETURN: None
347 *
348 * DESCRIPTION: Set the global integer bit width based upon the revision
349 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
350 * For Revision 2 and above, Integers are 64 bits. Yes, this
351 * makes a difference.
352 *
353 ******************************************************************************/
354
Len Brown4be44fc2005-08-05 00:44:28 -0400355void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357
358 if (revision <= 1) {
359 acpi_gbl_integer_bit_width = 32;
360 acpi_gbl_integer_nybble_width = 8;
361 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400362 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 acpi_gbl_integer_bit_width = 64;
364 acpi_gbl_integer_nybble_width = 16;
365 acpi_gbl_integer_byte_width = 8;
366 }
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#ifdef ACPI_DEBUG_OUTPUT
370/*******************************************************************************
371 *
372 * FUNCTION: acpi_ut_display_init_pathname
373 *
Robert Moore44f6c012005-04-18 22:49:35 -0400374 * PARAMETERS: Type - Object type of the node
375 * obj_handle - Handle whose pathname will be displayed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Path - Additional path string to be appended.
377 * (NULL if no extra path)
378 *
379 * RETURN: acpi_status
380 *
381 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
382 *
383 ******************************************************************************/
384
385void
Len Brown4be44fc2005-08-05 00:44:28 -0400386acpi_ut_display_init_pathname(u8 type,
387 struct acpi_namespace_node *obj_handle,
388 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Len Brown4be44fc2005-08-05 00:44:28 -0400390 acpi_status status;
391 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Len Brown4be44fc2005-08-05 00:44:28 -0400393 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* Only print the path if the appropriate debug level is enabled */
396
397 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
398 return;
399 }
400
401 /* Get the full pathname to the node */
402
403 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400404 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
405 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return;
407 }
408
409 /* Print what we're doing */
410
411 switch (type) {
412 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400413 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415
416 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400417 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
420
421 /* Print the object type and pathname */
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423 acpi_os_printf("%-12s %s",
424 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Extra path is used to append names like _STA, _INI, etc. */
427
428 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400429 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Len Brown4be44fc2005-08-05 00:44:28 -0400431 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Len Brown4be44fc2005-08-05 00:44:28 -0400433 ACPI_MEM_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435#endif
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/*******************************************************************************
438 *
439 * FUNCTION: acpi_ut_valid_acpi_name
440 *
Robert Moore44f6c012005-04-18 22:49:35 -0400441 * PARAMETERS: Name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *
Robert Moore44f6c012005-04-18 22:49:35 -0400443 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *
445 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
446 * 1) Upper case alpha
447 * 2) numeric
448 * 3) underscore
449 *
450 ******************************************************************************/
451
Len Brown4be44fc2005-08-05 00:44:28 -0400452u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Len Brown4be44fc2005-08-05 00:44:28 -0400454 char *name_ptr = (char *)&name;
455 char character;
456 acpi_native_uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Len Brown4be44fc2005-08-05 00:44:28 -0400458 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 for (i = 0; i < ACPI_NAME_SIZE; i++) {
461 character = *name_ptr;
462 name_ptr++;
463
464 if (!((character == '_') ||
Len Brown4be44fc2005-08-05 00:44:28 -0400465 (character >= 'A' && character <= 'Z') ||
466 (character >= '0' && character <= '9'))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return (FALSE);
468 }
469 }
470
471 return (TRUE);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*******************************************************************************
475 *
476 * FUNCTION: acpi_ut_valid_acpi_character
477 *
478 * PARAMETERS: Character - The character to be examined
479 *
480 * RETURN: 1 if Character may appear in a name, else 0
481 *
482 * DESCRIPTION: Check for a printable character
483 *
484 ******************************************************************************/
485
Len Brown4be44fc2005-08-05 00:44:28 -0400486u8 acpi_ut_valid_acpi_character(char character)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Len Brown4be44fc2005-08-05 00:44:28 -0400491 return ((u8) ((character == '_') ||
492 (character >= 'A' && character <= 'Z') ||
493 (character >= '0' && character <= '9')));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*******************************************************************************
497 *
498 * FUNCTION: acpi_ut_strtoul64
499 *
500 * PARAMETERS: String - Null terminated string
501 * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
502 * ret_integer - Where the converted integer is returned
503 *
504 * RETURN: Status and Converted value
505 *
506 * DESCRIPTION: Convert a string into an unsigned value.
507 * NOTE: Does not support Octal strings, not needed.
508 *
509 ******************************************************************************/
510
511acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400512acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Len Brown4be44fc2005-08-05 00:44:28 -0400514 u32 this_digit = 0;
515 acpi_integer return_value = 0;
516 acpi_integer quotient;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Len Brown4be44fc2005-08-05 00:44:28 -0400518 ACPI_FUNCTION_TRACE("ut_stroul64");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if ((!string) || !(*string)) {
521 goto error_exit;
522 }
523
524 switch (base) {
525 case ACPI_ANY_BASE:
526 case 10:
527 case 16:
528 break;
529
530 default:
531 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400532 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 /* Skip over any white space in the buffer */
536
Len Brown4be44fc2005-08-05 00:44:28 -0400537 while (ACPI_IS_SPACE(*string) || *string == '\t') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 string++;
539 }
540
541 /*
542 * If the input parameter Base is zero, then we need to
543 * determine if it is decimal or hexadecimal:
544 */
545 if (base == 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400546 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 base = 16;
548 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 base = 10;
551 }
552 }
553
554 /*
555 * For hexadecimal base, skip over the leading
556 * 0 or 0x, if they are present.
557 */
558 if ((base == 16) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400559 (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 string += 2;
561 }
562
563 /* Any string left? */
564
565 if (!(*string)) {
566 goto error_exit;
567 }
568
569 /* Main loop: convert the string to a 64-bit integer */
570
571 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400572 if (ACPI_IS_DIGIT(*string)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* Convert ASCII 0-9 to Decimal value */
574
Len Brown4be44fc2005-08-05 00:44:28 -0400575 this_digit = ((u8) * string) - '0';
576 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (base == 10) {
578 /* Digit is out of range */
579
580 goto error_exit;
581 }
582
Len Brown4be44fc2005-08-05 00:44:28 -0400583 this_digit = (u8) ACPI_TOUPPER(*string);
584 if (ACPI_IS_XDIGIT((char)this_digit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Convert ASCII Hex char to value */
586
587 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400588 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
590 * We allow non-hex chars, just stop now, same as end-of-string.
591 * See ACPI spec, string-to-integer conversion.
592 */
593 break;
594 }
595 }
596
597 /* Divide the digit into the correct position */
598
Len Brown4be44fc2005-08-05 00:44:28 -0400599 (void)
600 acpi_ut_short_divide((ACPI_INTEGER_MAX -
601 (acpi_integer) this_digit), base,
602 &quotient, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (return_value > quotient) {
604 goto error_exit;
605 }
606
607 return_value *= base;
608 return_value += this_digit;
609 string++;
610 }
611
612 /* All done, normal exit */
613
614 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400615 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Len Brown4be44fc2005-08-05 00:44:28 -0400617 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Base was set/validated above */
619
620 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400621 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
622 } else {
623 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/*******************************************************************************
628 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * FUNCTION: acpi_ut_create_update_state_and_push
630 *
Robert Moore44f6c012005-04-18 22:49:35 -0400631 * PARAMETERS: Object - Object to be added to the new state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Action - Increment/Decrement
633 * state_list - List the state will be added to
634 *
Robert Moore44f6c012005-04-18 22:49:35 -0400635 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
637 * DESCRIPTION: Create a new state and push it
638 *
639 ******************************************************************************/
640
641acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400642acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
643 u16 action,
644 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Len Brown4be44fc2005-08-05 00:44:28 -0400646 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Len Brown4be44fc2005-08-05 00:44:28 -0400648 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Ignore null objects; these are expected */
651
652 if (!object) {
653 return (AE_OK);
654 }
655
Len Brown4be44fc2005-08-05 00:44:28 -0400656 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!state) {
658 return (AE_NO_MEMORY);
659 }
660
Len Brown4be44fc2005-08-05 00:44:28 -0400661 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return (AE_OK);
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*******************************************************************************
666 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * FUNCTION: acpi_ut_walk_package_tree
668 *
Robert Moore44f6c012005-04-18 22:49:35 -0400669 * PARAMETERS: source_object - The package to walk
670 * target_object - Target object (if package is being copied)
671 * walk_callback - Called once for each package element
672 * Context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
674 * RETURN: Status
675 *
676 * DESCRIPTION: Walk through a package
677 *
678 ******************************************************************************/
679
680acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400681acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
682 void *target_object,
683 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Len Brown4be44fc2005-08-05 00:44:28 -0400685 acpi_status status = AE_OK;
686 union acpi_generic_state *state_list = NULL;
687 union acpi_generic_state *state;
688 u32 this_index;
689 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Len Brown4be44fc2005-08-05 00:44:28 -0400691 ACPI_FUNCTION_TRACE("ut_walk_package_tree");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Len Brown4be44fc2005-08-05 00:44:28 -0400693 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400695 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 while (state) {
699 /* Get one element of the package */
700
Len Brown4be44fc2005-08-05 00:44:28 -0400701 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400703 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /*
706 * Check for:
707 * 1) An uninitialized package element. It is completely
708 * legal to declare a package and leave it uninitialized
709 * 2) Not an internal object - can be a namespace node instead
710 * 3) Any type other than a package. Packages are handled in else
711 * case below.
712 */
713 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400714 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
715 ACPI_DESC_TYPE_OPERAND)
716 || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
717 ACPI_TYPE_PACKAGE)) {
718 status =
719 walk_callback(ACPI_COPY_TYPE_SIMPLE,
720 this_source_obj, state, context);
721 if (ACPI_FAILURE(status)) {
722 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -0400726 while (state->pkg.index >=
727 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
729 * We've handled all of the objects at this level, This means
730 * that we have just completed a package. That package may
731 * have contained one or more packages itself.
732 *
733 * Delete this state and pop the previous state (package).
734 */
Len Brown4be44fc2005-08-05 00:44:28 -0400735 acpi_ut_delete_generic_state(state);
736 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* Finished when there are no more states */
739
740 if (!state) {
741 /*
742 * We have handled all of the objects in the top level
743 * package just add the length of the package objects
744 * and exit
745 */
Len Brown4be44fc2005-08-05 00:44:28 -0400746 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 /*
750 * Go back up a level and move the index past the just
751 * completed package object.
752 */
753 state->pkg.index++;
754 }
Len Brown4be44fc2005-08-05 00:44:28 -0400755 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* This is a subobject of type package */
757
Len Brown4be44fc2005-08-05 00:44:28 -0400758 status =
759 walk_callback(ACPI_COPY_TYPE_PACKAGE,
760 this_source_obj, state, context);
761 if (ACPI_FAILURE(status)) {
762 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 /*
766 * Push the current state and create a new one
767 * The callback above returned a new target package object.
768 */
Len Brown4be44fc2005-08-05 00:44:28 -0400769 acpi_ut_push_generic_state(&state_list, state);
770 state = acpi_ut_create_pkg_state(this_source_obj,
771 state->pkg.
772 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400774 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
777 }
778
779 /* We should never get here */
780
Len Brown4be44fc2005-08-05 00:44:28 -0400781 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*******************************************************************************
785 *
786 * FUNCTION: acpi_ut_generate_checksum
787 *
788 * PARAMETERS: Buffer - Buffer to be scanned
789 * Length - number of bytes to examine
790 *
Robert Moore44f6c012005-04-18 22:49:35 -0400791 * RETURN: The generated checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
793 * DESCRIPTION: Generate a checksum on a raw buffer
794 *
795 ******************************************************************************/
796
Len Brown4be44fc2005-08-05 00:44:28 -0400797u8 acpi_ut_generate_checksum(u8 * buffer, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Len Brown4be44fc2005-08-05 00:44:28 -0400799 u32 i;
800 signed char sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 for (i = 0; i < length; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400803 sum = (signed char)(sum + buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
806 return ((u8) (0 - sum));
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*******************************************************************************
810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * FUNCTION: acpi_ut_report_error
812 *
813 * PARAMETERS: module_name - Caller's module name (for error output)
814 * line_number - Caller's line number (for error output)
815 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
817 * RETURN: None
818 *
819 * DESCRIPTION: Print error message
820 *
821 ******************************************************************************/
822
Len Brown4be44fc2005-08-05 00:44:28 -0400823void acpi_ut_report_error(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825
Len Brown4be44fc2005-08-05 00:44:28 -0400826 acpi_os_printf("%8s-%04d: *** Error: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*******************************************************************************
830 *
831 * FUNCTION: acpi_ut_report_warning
832 *
833 * PARAMETERS: module_name - Caller's module name (for error output)
834 * line_number - Caller's line number (for error output)
835 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *
837 * RETURN: None
838 *
839 * DESCRIPTION: Print warning message
840 *
841 ******************************************************************************/
842
843void
Len Brown4be44fc2005-08-05 00:44:28 -0400844acpi_ut_report_warning(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846
Len Brown4be44fc2005-08-05 00:44:28 -0400847 acpi_os_printf("%8s-%04d: *** Warning: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*******************************************************************************
851 *
852 * FUNCTION: acpi_ut_report_info
853 *
854 * PARAMETERS: module_name - Caller's module name (for error output)
855 * line_number - Caller's line number (for error output)
856 * component_id - Caller's component ID (for error output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
858 * RETURN: None
859 *
860 * DESCRIPTION: Print information message
861 *
862 ******************************************************************************/
863
Len Brown4be44fc2005-08-05 00:44:28 -0400864void acpi_ut_report_info(char *module_name, u32 line_number, u32 component_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866
Len Brown4be44fc2005-08-05 00:44:28 -0400867 acpi_os_printf("%8s-%04d: *** Info: ", module_name, line_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}