blob: 9cd65334ca750260d25c106c1da7260f8aa44997 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: utmisc - common utility procedures
4 *
5 ******************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Thomas Renningerbe63c922006-06-02 15:58:00 -040044#include <linux/module.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050047#include "accommon.h"
48#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("utmisc")
Robert Moore44f6c012005-04-18 22:49:35 -040052
53/*******************************************************************************
54 *
Bob Moore84fb2c92007-02-02 19:48:19 +030055 * FUNCTION: acpi_ut_validate_exception
56 *
57 * PARAMETERS: Status - The acpi_status code to be formatted
58 *
59 * RETURN: A string containing the exception text. NULL if exception is
60 * not valid.
61 *
62 * DESCRIPTION: This function validates and translates an ACPI exception into
63 * an ASCII string.
64 *
65 ******************************************************************************/
66const char *acpi_ut_validate_exception(acpi_status status)
67{
Bob Moore11f2a612008-06-10 12:53:01 +080068 u32 sub_status;
Bob Moore84fb2c92007-02-02 19:48:19 +030069 const char *exception = NULL;
70
71 ACPI_FUNCTION_ENTRY();
72
73 /*
74 * Status is composed of two parts, a "type" and an actual code
75 */
76 sub_status = (status & ~AE_CODE_MASK);
77
78 switch (status & AE_CODE_MASK) {
79 case AE_CODE_ENVIRONMENTAL:
80
81 if (sub_status <= AE_CODE_ENV_MAX) {
82 exception = acpi_gbl_exception_names_env[sub_status];
83 }
84 break;
85
86 case AE_CODE_PROGRAMMER:
87
88 if (sub_status <= AE_CODE_PGM_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +080089 exception = acpi_gbl_exception_names_pgm[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +030090 }
91 break;
92
93 case AE_CODE_ACPI_TABLES:
94
95 if (sub_status <= AE_CODE_TBL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +080096 exception = acpi_gbl_exception_names_tbl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +030097 }
98 break;
99
100 case AE_CODE_AML:
101
102 if (sub_status <= AE_CODE_AML_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800103 exception = acpi_gbl_exception_names_aml[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300104 }
105 break;
106
107 case AE_CODE_CONTROL:
108
109 if (sub_status <= AE_CODE_CTRL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800110 exception = acpi_gbl_exception_names_ctrl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300111 }
112 break;
113
114 default:
115 break;
116 }
117
118 return (ACPI_CAST_PTR(const char, exception));
119}
120
121/*******************************************************************************
122 *
Bob Moore15b8dd52009-06-29 13:39:29 +0800123 * FUNCTION: acpi_ut_is_pci_root_bridge
124 *
125 * PARAMETERS: Id - The HID/CID in string format
126 *
127 * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
128 *
129 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
130 *
131 ******************************************************************************/
132
133u8 acpi_ut_is_pci_root_bridge(char *id)
134{
135
136 /*
137 * Check if this is a PCI root bridge.
138 * ACPI 3.0+: check for a PCI Express root also.
139 */
140 if (!(ACPI_STRCMP(id,
141 PCI_ROOT_HID_STRING)) ||
142 !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
143 return (TRUE);
144 }
145
146 return (FALSE);
147}
148
149/*******************************************************************************
150 *
Bob Moore793c2382006-03-31 00:00:00 -0500151 * FUNCTION: acpi_ut_is_aml_table
152 *
153 * PARAMETERS: Table - An ACPI table
154 *
155 * RETURN: TRUE if table contains executable AML; FALSE otherwise
156 *
157 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
158 * Currently, these are DSDT,SSDT,PSDT. All other table types are
159 * data tables that do not contain AML code.
160 *
161 ******************************************************************************/
Bob Moore84fb2c92007-02-02 19:48:19 +0300162
Bob Moore793c2382006-03-31 00:00:00 -0500163u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
164{
165
Bob Mooref6dd9222006-07-07 20:44:38 -0400166 /* These are the only tables that contain executable AML */
Bob Moore793c2382006-03-31 00:00:00 -0500167
Bob Mooref3d2e782007-02-02 19:48:18 +0300168 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
169 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
170 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
Bob Moore793c2382006-03-31 00:00:00 -0500171 return (TRUE);
172 }
173
174 return (FALSE);
175}
176
177/*******************************************************************************
178 *
Robert Mooref9f46012005-07-08 00:00:00 -0400179 * FUNCTION: acpi_ut_allocate_owner_id
180 *
181 * PARAMETERS: owner_id - Where the new owner ID is returned
182 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700183 * RETURN: Status
184 *
185 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
186 * track objects created by the table or method, to be deleted
187 * when the method exits or the table is unloaded.
Robert Mooref9f46012005-07-08 00:00:00 -0400188 *
189 ******************************************************************************/
Bob Moore793c2382006-03-31 00:00:00 -0500190
Len Brown4be44fc2005-08-05 00:44:28 -0400191acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
Robert Mooref9f46012005-07-08 00:00:00 -0400192{
Bob Moore67a119f2008-06-10 13:42:13 +0800193 u32 i;
194 u32 j;
195 u32 k;
Len Brown4be44fc2005-08-05 00:44:28 -0400196 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400197
Bob Mooreb229cf92006-04-21 17:15:00 -0400198 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400199
Robert Mooreaff8c272005-09-02 17:24:17 -0400200 /* Guard against multiple allocations of ID to the same location */
201
202 if (*owner_id) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500203 ACPI_ERROR((AE_INFO, "Owner ID [%2.2X] already exists",
204 *owner_id));
Robert Mooreaff8c272005-09-02 17:24:17 -0400205 return_ACPI_STATUS(AE_ALREADY_EXISTS);
206 }
207
Robert Moore0c9938c2005-07-29 15:15:00 -0700208 /* Mutex for the global ID mask */
209
Len Brown4be44fc2005-08-05 00:44:28 -0400210 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
211 if (ACPI_FAILURE(status)) {
212 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400213 }
214
Bob Moorec51a4de2005-11-17 13:07:00 -0500215 /*
216 * Find a free owner ID, cycle through all possible IDs on repeated
Bob Moore28f55eb2005-12-02 18:27:00 -0500217 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
218 * to be scanned twice.
Bob Moorec51a4de2005-11-17 13:07:00 -0500219 */
Bob Moore28f55eb2005-12-02 18:27:00 -0500220 for (i = 0, j = acpi_gbl_last_owner_id_index;
221 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
222 if (j >= ACPI_NUM_OWNERID_MASKS) {
223 j = 0; /* Wraparound to start of mask array */
Bob Moorec51a4de2005-11-17 13:07:00 -0500224 }
Robert Mooref9f46012005-07-08 00:00:00 -0400225
Bob Moore28f55eb2005-12-02 18:27:00 -0500226 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
227 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400228
Bob Moore28f55eb2005-12-02 18:27:00 -0500229 /* There are no free IDs in this mask */
Bob Moorec51a4de2005-11-17 13:07:00 -0500230
Bob Moore28f55eb2005-12-02 18:27:00 -0500231 break;
232 }
Bob Moorea18ecf42005-08-15 03:42:00 -0800233
Bob Moore28f55eb2005-12-02 18:27:00 -0500234 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
235 /*
236 * Found a free ID. The actual ID is the bit index plus one,
237 * making zero an invalid Owner ID. Save this as the last ID
238 * allocated and update the global ID mask.
239 */
240 acpi_gbl_owner_id_mask[j] |= (1 << k);
241
242 acpi_gbl_last_owner_id_index = (u8) j;
243 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
244
245 /*
246 * Construct encoded ID from the index and bit position
247 *
248 * Note: Last [j].k (bit 255) is never used and is marked
249 * permanently allocated (prevents +1 overflow)
250 */
251 *owner_id =
252 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
253
254 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400255 "Allocated OwnerId: %2.2X\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500256 (unsigned int)*owner_id));
257 goto exit;
258 }
Robert Mooref9f46012005-07-08 00:00:00 -0400259 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500260
261 acpi_gbl_next_owner_id_offset = 0;
Robert Mooref9f46012005-07-08 00:00:00 -0400262 }
263
264 /*
Bob Moorec51a4de2005-11-17 13:07:00 -0500265 * All owner_ids have been allocated. This typically should
Robert Mooref9f46012005-07-08 00:00:00 -0400266 * not happen since the IDs are reused after deallocation. The IDs are
267 * allocated upon table load (one per table) and method execution, and
268 * they are released when a table is unloaded or a method completes
269 * execution.
Bob Moorec51a4de2005-11-17 13:07:00 -0500270 *
271 * If this error happens, there may be very deep nesting of invoked control
272 * methods, or there may be a bug where the IDs are not released.
Robert Mooref9f46012005-07-08 00:00:00 -0400273 */
274 status = AE_OWNER_ID_LIMIT;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500275 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400276 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
Robert Mooref9f46012005-07-08 00:00:00 -0400277
Len Brown4be44fc2005-08-05 00:44:28 -0400278 exit:
279 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
280 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400281}
282
Robert Mooref9f46012005-07-08 00:00:00 -0400283/*******************************************************************************
284 *
285 * FUNCTION: acpi_ut_release_owner_id
286 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700287 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
Robert Mooref9f46012005-07-08 00:00:00 -0400288 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700289 * RETURN: None. No error is returned because we are either exiting a
290 * control method or unloading a table. Either way, we would
291 * ignore any error anyway.
292 *
Bob Moore28f55eb2005-12-02 18:27:00 -0500293 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
Robert Mooref9f46012005-07-08 00:00:00 -0400294 *
295 ******************************************************************************/
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400298{
Len Brown4be44fc2005-08-05 00:44:28 -0400299 acpi_owner_id owner_id = *owner_id_ptr;
300 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +0800301 u32 index;
Bob Moore28f55eb2005-12-02 18:27:00 -0500302 u32 bit;
Robert Mooref9f46012005-07-08 00:00:00 -0400303
Bob Mooreb229cf92006-04-21 17:15:00 -0400304 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400305
Robert Moore0c9938c2005-07-29 15:15:00 -0700306 /* Always clear the input owner_id (zero is an invalid ID) */
307
308 *owner_id_ptr = 0;
309
310 /* Zero is not a valid owner_iD */
311
Bob Mooredefba1d2005-12-16 17:05:00 -0500312 if (owner_id == 0) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400313 ACPI_ERROR((AE_INFO, "Invalid OwnerId: %2.2X", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700314 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400315 }
316
Robert Moore0c9938c2005-07-29 15:15:00 -0700317 /* Mutex for the global ID mask */
318
Len Brown4be44fc2005-08-05 00:44:28 -0400319 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
320 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700321 return_VOID;
322 }
323
Robert Mooreaff8c272005-09-02 17:24:17 -0400324 /* Normalize the ID to zero */
325
326 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700327
Bob Moore28f55eb2005-12-02 18:27:00 -0500328 /* Decode ID to index/offset pair */
329
330 index = ACPI_DIV_32(owner_id);
331 bit = 1 << ACPI_MOD_32(owner_id);
332
Robert Moore0c9938c2005-07-29 15:15:00 -0700333 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400334
Bob Moore28f55eb2005-12-02 18:27:00 -0500335 if (acpi_gbl_owner_id_mask[index] & bit) {
336 acpi_gbl_owner_id_mask[index] ^= bit;
337 } else {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500338 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400339 "Release of non-allocated OwnerId: %2.2X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500340 owner_id + 1));
Robert Mooref9f46012005-07-08 00:00:00 -0400341 }
Robert Mooref9f46012005-07-08 00:00:00 -0400342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700344 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400345}
346
Robert Mooref9f46012005-07-08 00:00:00 -0400347/*******************************************************************************
348 *
Robert Moore44f6c012005-04-18 22:49:35 -0400349 * FUNCTION: acpi_ut_strupr (strupr)
350 *
351 * PARAMETERS: src_string - The source string to convert
352 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700353 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400354 *
355 * DESCRIPTION: Convert string to uppercase
356 *
357 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
358 *
359 ******************************************************************************/
360
Len Brown4be44fc2005-08-05 00:44:28 -0400361void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400362{
Len Brown4be44fc2005-08-05 00:44:28 -0400363 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400366
Robert Moore73459f72005-06-24 00:00:00 -0400367 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700368 return;
Robert Moore73459f72005-06-24 00:00:00 -0400369 }
370
Robert Moore44f6c012005-04-18 22:49:35 -0400371 /* Walk entire string, uppercasing the letters */
372
373 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400374 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400375 }
376
Robert Moore0c9938c2005-07-29 15:15:00 -0700377 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*******************************************************************************
381 *
382 * FUNCTION: acpi_ut_print_string
383 *
384 * PARAMETERS: String - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400385 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
387 * RETURN: None
388 *
389 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
390 * sequences.
391 *
392 ******************************************************************************/
393
Len Brown4be44fc2005-08-05 00:44:28 -0400394void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Len Brown4be44fc2005-08-05 00:44:28 -0400396 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400399 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return;
401 }
402
Len Brown4be44fc2005-08-05 00:44:28 -0400403 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 for (i = 0; string[i] && (i < max_length); i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Escape sequences */
407
408 switch (string[i]) {
409 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400410 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412
413 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400414 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416
417 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400418 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420
421 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400422 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424
425 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400426 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428
429 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400430 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432
433 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400434 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436
Len Brown4be44fc2005-08-05 00:44:28 -0400437 case '\'': /* Single Quote */
438 case '\"': /* Double Quote */
439 case '\\': /* Backslash */
440 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442
443 default:
444
445 /* Check for printable character or hex escape */
446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* This is a normal character */
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450 acpi_os_printf("%c", (int)string[i]);
451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /* All others will be Hex escapes */
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 break;
457 }
458 }
Len Brown4be44fc2005-08-05 00:44:28 -0400459 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400462 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/*******************************************************************************
467 *
468 * FUNCTION: acpi_ut_dword_byte_swap
469 *
470 * PARAMETERS: Value - Value to be converted
471 *
Robert Moore44f6c012005-04-18 22:49:35 -0400472 * RETURN: u32 integer with bytes swapped
473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
475 *
476 ******************************************************************************/
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400481 u32 value;
482 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400485 u32 value;
486 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } in;
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 in.value = value;
492
493 out.bytes[0] = in.bytes[3];
494 out.bytes[1] = in.bytes[2];
495 out.bytes[2] = in.bytes[1];
496 out.bytes[3] = in.bytes[0];
497
498 return (out.value);
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*******************************************************************************
502 *
503 * FUNCTION: acpi_ut_set_integer_width
504 *
505 * PARAMETERS: Revision From DSDT header
506 *
507 * RETURN: None
508 *
509 * DESCRIPTION: Set the global integer bit width based upon the revision
510 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
511 * For Revision 2 and above, Integers are 64 bits. Yes, this
512 * makes a difference.
513 *
514 ******************************************************************************/
515
Len Brown4be44fc2005-08-05 00:44:28 -0400516void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518
Bob Mooref3d2e782007-02-02 19:48:18 +0300519 if (revision < 2) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400520
521 /* 32-bit case */
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 acpi_gbl_integer_bit_width = 32;
524 acpi_gbl_integer_nybble_width = 8;
525 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400526 } else {
Bob Mooref6dd9222006-07-07 20:44:38 -0400527 /* 64-bit case (ACPI 2.0+) */
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 acpi_gbl_integer_bit_width = 64;
530 acpi_gbl_integer_nybble_width = 16;
531 acpi_gbl_integer_byte_width = 8;
532 }
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#ifdef ACPI_DEBUG_OUTPUT
536/*******************************************************************************
537 *
538 * FUNCTION: acpi_ut_display_init_pathname
539 *
Robert Moore44f6c012005-04-18 22:49:35 -0400540 * PARAMETERS: Type - Object type of the node
541 * obj_handle - Handle whose pathname will be displayed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * Path - Additional path string to be appended.
543 * (NULL if no extra path)
544 *
545 * RETURN: acpi_status
546 *
547 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
548 *
549 ******************************************************************************/
550
551void
Len Brown4be44fc2005-08-05 00:44:28 -0400552acpi_ut_display_init_pathname(u8 type,
553 struct acpi_namespace_node *obj_handle,
554 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Len Brown4be44fc2005-08-05 00:44:28 -0400556 acpi_status status;
557 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Len Brown4be44fc2005-08-05 00:44:28 -0400559 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* Only print the path if the appropriate debug level is enabled */
562
563 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
564 return;
565 }
566
567 /* Get the full pathname to the node */
568
569 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400570 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
571 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573 }
574
575 /* Print what we're doing */
576
577 switch (type) {
578 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400579 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
581
582 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400583 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585 }
586
587 /* Print the object type and pathname */
588
Len Brown4be44fc2005-08-05 00:44:28 -0400589 acpi_os_printf("%-12s %s",
590 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Extra path is used to append names like _STA, _INI, etc. */
593
594 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400595 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Len Brown4be44fc2005-08-05 00:44:28 -0400597 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Bob Moore83135242006-10-03 00:00:00 -0400599 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601#endif
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*******************************************************************************
604 *
Bob Moore793c2382006-03-31 00:00:00 -0500605 * FUNCTION: acpi_ut_valid_acpi_char
606 *
607 * PARAMETERS: Char - The character to be examined
Bob Mooref6dd9222006-07-07 20:44:38 -0400608 * Position - Byte position (0-3)
Bob Moore793c2382006-03-31 00:00:00 -0500609 *
610 * RETURN: TRUE if the character is valid, FALSE otherwise
611 *
612 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
613 * 1) Upper case alpha
614 * 2) numeric
615 * 3) underscore
616 *
617 * We allow a '!' as the last character because of the ASF! table
618 *
619 ******************************************************************************/
620
Bob Moore67a119f2008-06-10 13:42:13 +0800621u8 acpi_ut_valid_acpi_char(char character, u32 position)
Bob Moore793c2382006-03-31 00:00:00 -0500622{
623
624 if (!((character >= 'A' && character <= 'Z') ||
625 (character >= '0' && character <= '9') || (character == '_'))) {
626
627 /* Allow a '!' in the last position */
628
629 if (character == '!' && position == 3) {
630 return (TRUE);
631 }
632
633 return (FALSE);
634 }
635
636 return (TRUE);
637}
638
639/*******************************************************************************
640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * FUNCTION: acpi_ut_valid_acpi_name
642 *
Robert Moore44f6c012005-04-18 22:49:35 -0400643 * PARAMETERS: Name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
Robert Moore44f6c012005-04-18 22:49:35 -0400645 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
647 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
648 * 1) Upper case alpha
649 * 2) numeric
650 * 3) underscore
651 *
652 ******************************************************************************/
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Bob Moore67a119f2008-06-10 13:42:13 +0800656 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Len Brown4be44fc2005-08-05 00:44:28 -0400658 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore793c2382006-03-31 00:00:00 -0500661 if (!acpi_ut_valid_acpi_char
662 ((ACPI_CAST_PTR(char, &name))[i], i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return (FALSE);
664 }
665 }
666
667 return (TRUE);
668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*******************************************************************************
671 *
Bob Moore793c2382006-03-31 00:00:00 -0500672 * FUNCTION: acpi_ut_repair_name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
Bob Moore793c2382006-03-31 00:00:00 -0500674 * PARAMETERS: Name - The ACPI name to be repaired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Bob Moore793c2382006-03-31 00:00:00 -0500676 * RETURN: Repaired version of the name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
Bob Moore793c2382006-03-31 00:00:00 -0500678 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
679 * return the new name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *
681 ******************************************************************************/
682
Bob Moore3d81b232007-02-02 19:48:19 +0300683acpi_name acpi_ut_repair_name(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Bob Moore67a119f2008-06-10 13:42:13 +0800685 u32 i;
Bob Moore3d81b232007-02-02 19:48:19 +0300686 char new_name[ACPI_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Bob Moore793c2382006-03-31 00:00:00 -0500688 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore3d81b232007-02-02 19:48:19 +0300689 new_name[i] = name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Bob Moore793c2382006-03-31 00:00:00 -0500691 /*
692 * Replace a bad character with something printable, yet technically
693 * still invalid. This prevents any collisions with existing "good"
694 * names in the namespace.
695 */
Bob Moore3d81b232007-02-02 19:48:19 +0300696 if (!acpi_ut_valid_acpi_char(name[i], i)) {
Bob Moore793c2382006-03-31 00:00:00 -0500697 new_name[i] = '*';
698 }
699 }
700
Bob Moore3d81b232007-02-02 19:48:19 +0300701 return (*(u32 *) new_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*******************************************************************************
705 *
706 * FUNCTION: acpi_ut_strtoul64
707 *
708 * PARAMETERS: String - Null terminated string
Bob Moore41195322006-05-26 16:36:00 -0400709 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
710 * ACPI_ANY_BASE means 'in behalf of to_integer'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * ret_integer - Where the converted integer is returned
712 *
713 * RETURN: Status and Converted value
714 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400715 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
716 * 32-bit or 64-bit conversion, depending on the current mode
717 * of the interpreter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * NOTE: Does not support Octal strings, not needed.
719 *
720 ******************************************************************************/
721
722acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400723acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Len Brown4be44fc2005-08-05 00:44:28 -0400725 u32 this_digit = 0;
726 acpi_integer return_value = 0;
727 acpi_integer quotient;
Bob Moore41195322006-05-26 16:36:00 -0400728 acpi_integer dividend;
729 u32 to_integer_op = (base == ACPI_ANY_BASE);
730 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
731 u8 valid_digits = 0;
732 u8 sign_of0x = 0;
733 u8 term = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Bob Mooref6dd9222006-07-07 20:44:38 -0400735 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 switch (base) {
738 case ACPI_ANY_BASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 case 16:
740 break;
741
742 default:
743 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400744 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Bob Moore41195322006-05-26 16:36:00 -0400747 if (!string) {
748 goto error_exit;
749 }
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Skip over any white space in the buffer */
752
Bob Moore41195322006-05-26 16:36:00 -0400753 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 string++;
755 }
756
Bob Moore41195322006-05-26 16:36:00 -0400757 if (to_integer_op) {
758 /*
759 * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
760 * We need to determine if it is decimal or hexadecimal.
761 */
Len Brown4be44fc2005-08-05 00:44:28 -0400762 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Bob Moore41195322006-05-26 16:36:00 -0400763 sign_of0x = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 base = 16;
Bob Moore41195322006-05-26 16:36:00 -0400765
766 /* Skip over the leading '0x' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400768 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 base = 10;
770 }
771 }
772
Bob Moore41195322006-05-26 16:36:00 -0400773 /* Any string left? Check that '0x' is not followed by white space. */
774
775 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
776 if (to_integer_op) {
777 goto error_exit;
778 } else {
779 goto all_done;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Bob Mooref6dd9222006-07-07 20:44:38 -0400783 /*
784 * Perform a 32-bit or 64-bit conversion, depending upon the current
785 * execution mode of the interpreter
786 */
Bob Moore41195322006-05-26 16:36:00 -0400787 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Bob Mooref6dd9222006-07-07 20:44:38 -0400789 /* Main loop: convert the string to a 32- or 64-bit integer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400792 if (ACPI_IS_DIGIT(*string)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* Convert ASCII 0-9 to Decimal value */
795
Len Brown4be44fc2005-08-05 00:44:28 -0400796 this_digit = ((u8) * string) - '0';
Bob Moore41195322006-05-26 16:36:00 -0400797 } else if (base == 10) {
798
799 /* Digit is out of range; possible in to_integer case only */
800
801 term = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400802 } else {
Len Brown4be44fc2005-08-05 00:44:28 -0400803 this_digit = (u8) ACPI_TOUPPER(*string);
804 if (ACPI_IS_XDIGIT((char)this_digit)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* Convert ASCII Hex char to value */
807
808 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400809 } else {
Bob Moore41195322006-05-26 16:36:00 -0400810 term = 1;
811 }
812 }
813
814 if (term) {
815 if (to_integer_op) {
816 goto error_exit;
817 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 }
Bob Moore41195322006-05-26 16:36:00 -0400820 } else if ((valid_digits == 0) && (this_digit == 0)
821 && !sign_of0x) {
822
823 /* Skip zeros */
824 string++;
825 continue;
826 }
827
828 valid_digits++;
829
Len Brownfd350942007-05-09 23:34:35 -0400830 if (sign_of0x && ((valid_digits > 16)
831 || ((valid_digits > 8) && mode32))) {
Bob Moore41195322006-05-26 16:36:00 -0400832 /*
833 * This is to_integer operation case.
834 * No any restrictions for string-to-integer conversion,
835 * see ACPI spec.
836 */
837 goto error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 /* Divide the digit into the correct position */
841
Len Brown4be44fc2005-08-05 00:44:28 -0400842 (void)
Bob Moore41195322006-05-26 16:36:00 -0400843 acpi_ut_short_divide((dividend - (acpi_integer) this_digit),
844 base, &quotient, NULL);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (return_value > quotient) {
Bob Moore41195322006-05-26 16:36:00 -0400847 if (to_integer_op) {
848 goto error_exit;
849 } else {
850 break;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 return_value *= base;
855 return_value += this_digit;
856 string++;
857 }
858
859 /* All done, normal exit */
860
Bob Moore41195322006-05-26 16:36:00 -0400861 all_done:
862
Bob Mooref6dd9222006-07-07 20:44:38 -0400863 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
864 ACPI_FORMAT_UINT64(return_value)));
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400867 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Len Brown4be44fc2005-08-05 00:44:28 -0400869 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Base was set/validated above */
871
872 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400873 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
874 } else {
875 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*******************************************************************************
880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * FUNCTION: acpi_ut_create_update_state_and_push
882 *
Robert Moore44f6c012005-04-18 22:49:35 -0400883 * PARAMETERS: Object - Object to be added to the new state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * Action - Increment/Decrement
885 * state_list - List the state will be added to
886 *
Robert Moore44f6c012005-04-18 22:49:35 -0400887 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
889 * DESCRIPTION: Create a new state and push it
890 *
891 ******************************************************************************/
892
893acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400894acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
895 u16 action,
896 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Len Brown4be44fc2005-08-05 00:44:28 -0400898 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Len Brown4be44fc2005-08-05 00:44:28 -0400900 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Ignore null objects; these are expected */
903
904 if (!object) {
905 return (AE_OK);
906 }
907
Len Brown4be44fc2005-08-05 00:44:28 -0400908 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!state) {
910 return (AE_NO_MEMORY);
911 }
912
Len Brown4be44fc2005-08-05 00:44:28 -0400913 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return (AE_OK);
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*******************************************************************************
918 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * FUNCTION: acpi_ut_walk_package_tree
920 *
Robert Moore44f6c012005-04-18 22:49:35 -0400921 * PARAMETERS: source_object - The package to walk
922 * target_object - Target object (if package is being copied)
923 * walk_callback - Called once for each package element
924 * Context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *
926 * RETURN: Status
927 *
928 * DESCRIPTION: Walk through a package
929 *
930 ******************************************************************************/
931
932acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400933acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
934 void *target_object,
935 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Len Brown4be44fc2005-08-05 00:44:28 -0400937 acpi_status status = AE_OK;
938 union acpi_generic_state *state_list = NULL;
939 union acpi_generic_state *state;
940 u32 this_index;
941 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Bob Mooreb229cf92006-04-21 17:15:00 -0400943 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Len Brown4be44fc2005-08-05 00:44:28 -0400945 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400947 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 while (state) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Get one element of the package */
953
Len Brown4be44fc2005-08-05 00:44:28 -0400954 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400956 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /*
959 * Check for:
960 * 1) An uninitialized package element. It is completely
961 * legal to declare a package and leave it uninitialized
962 * 2) Not an internal object - can be a namespace node instead
963 * 3) Any type other than a package. Packages are handled in else
964 * case below.
965 */
966 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400967 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
968 ACPI_DESC_TYPE_OPERAND)
Bob Moore3371c192009-02-18 14:44:03 +0800969 || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400970 status =
971 walk_callback(ACPI_COPY_TYPE_SIMPLE,
972 this_source_obj, state, context);
973 if (ACPI_FAILURE(status)) {
974 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -0400978 while (state->pkg.index >=
979 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /*
981 * We've handled all of the objects at this level, This means
982 * that we have just completed a package. That package may
983 * have contained one or more packages itself.
984 *
985 * Delete this state and pop the previous state (package).
986 */
Len Brown4be44fc2005-08-05 00:44:28 -0400987 acpi_ut_delete_generic_state(state);
988 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* Finished when there are no more states */
991
992 if (!state) {
993 /*
994 * We have handled all of the objects in the top level
995 * package just add the length of the package objects
996 * and exit
997 */
Len Brown4be44fc2005-08-05 00:44:28 -0400998 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
1001 /*
1002 * Go back up a level and move the index past the just
1003 * completed package object.
1004 */
1005 state->pkg.index++;
1006 }
Len Brown4be44fc2005-08-05 00:44:28 -04001007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* This is a subobject of type package */
1009
Len Brown4be44fc2005-08-05 00:44:28 -04001010 status =
1011 walk_callback(ACPI_COPY_TYPE_PACKAGE,
1012 this_source_obj, state, context);
1013 if (ACPI_FAILURE(status)) {
1014 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017 /*
1018 * Push the current state and create a new one
1019 * The callback above returned a new target package object.
1020 */
Len Brown4be44fc2005-08-05 00:44:28 -04001021 acpi_ut_push_generic_state(&state_list, state);
1022 state = acpi_ut_create_pkg_state(this_source_obj,
1023 state->pkg.
1024 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (!state) {
Lin Mingcf058bd2008-09-27 11:29:57 +08001026
1027 /* Free any stacked Update State objects */
1028
1029 while (state_list) {
1030 state =
1031 acpi_ut_pop_generic_state
1032 (&state_list);
1033 acpi_ut_delete_generic_state(state);
1034 }
Len Brown4be44fc2005-08-05 00:44:28 -04001035 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 }
1038 }
1039
1040 /* We should never get here */
1041
Len Brown4be44fc2005-08-05 00:44:28 -04001042 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045/*******************************************************************************
1046 *
Bob Moore50df4d82008-12-31 03:01:23 +08001047 * FUNCTION: acpi_error, acpi_exception, acpi_warning, acpi_info
Bob Mooreb8e4d892006-01-27 16:43:00 -05001048 *
1049 * PARAMETERS: module_name - Caller's module name (for error output)
1050 * line_number - Caller's line number (for error output)
1051 * Format - Printf format string + additional args
1052 *
1053 * RETURN: None
1054 *
1055 * DESCRIPTION: Print message with module/line/version info
1056 *
1057 ******************************************************************************/
1058
1059void ACPI_INTERNAL_VAR_XFACE
Bob Moore50df4d82008-12-31 03:01:23 +08001060acpi_error(const char *module_name, u32 line_number, const char *format, ...)
Bob Mooreb8e4d892006-01-27 16:43:00 -05001061{
1062 va_list args;
1063
Bob Mooreb74be612009-04-22 10:20:23 +08001064 acpi_os_printf("ACPI Error: ");
Bob Mooreb8e4d892006-01-27 16:43:00 -05001065
1066 va_start(args, format);
1067 acpi_os_vprintf(format, args);
Bob Mooreb74be612009-04-22 10:20:23 +08001068 acpi_os_printf(" %8.8X %s-%u\n", ACPI_CA_VERSION, module_name,
1069 line_number);
Bob Moore507f0462008-04-10 19:06:42 +04001070 va_end(args);
Bob Mooreb8e4d892006-01-27 16:43:00 -05001071}
1072
1073void ACPI_INTERNAL_VAR_XFACE
Bob Moore50df4d82008-12-31 03:01:23 +08001074acpi_exception(const char *module_name,
1075 u32 line_number, acpi_status status, const char *format, ...)
Bob Mooreb8e4d892006-01-27 16:43:00 -05001076{
1077 va_list args;
1078
Bob Mooreb74be612009-04-22 10:20:23 +08001079 acpi_os_printf("ACPI Exception: %s, ", acpi_format_exception(status));
Bob Mooreb8e4d892006-01-27 16:43:00 -05001080
1081 va_start(args, format);
1082 acpi_os_vprintf(format, args);
Bob Mooreb74be612009-04-22 10:20:23 +08001083 acpi_os_printf(" %8.8X %s-%u\n", ACPI_CA_VERSION, module_name,
1084 line_number);
Len Brown3549dba2008-06-06 15:32:39 -04001085 va_end(args);
Bob Mooreb8e4d892006-01-27 16:43:00 -05001086}
Len Brownfd350942007-05-09 23:34:35 -04001087
Bob Mooreb8e4d892006-01-27 16:43:00 -05001088void ACPI_INTERNAL_VAR_XFACE
Bob Moore50df4d82008-12-31 03:01:23 +08001089acpi_warning(const char *module_name, u32 line_number, const char *format, ...)
Bob Mooreb8e4d892006-01-27 16:43:00 -05001090{
1091 va_list args;
1092
Bob Mooreb74be612009-04-22 10:20:23 +08001093 acpi_os_printf("ACPI Warning: ");
Bob Mooreb8e4d892006-01-27 16:43:00 -05001094
1095 va_start(args, format);
1096 acpi_os_vprintf(format, args);
Bob Mooreb74be612009-04-22 10:20:23 +08001097 acpi_os_printf(" %8.8X %s-%u\n", ACPI_CA_VERSION, module_name,
1098 line_number);
Bob Moore507f0462008-04-10 19:06:42 +04001099 va_end(args);
Bob Mooreb8e4d892006-01-27 16:43:00 -05001100}
Len Browncece9292006-06-26 23:04:31 -04001101
Bob Mooreb8e4d892006-01-27 16:43:00 -05001102void ACPI_INTERNAL_VAR_XFACE
Bob Moore50df4d82008-12-31 03:01:23 +08001103acpi_info(const char *module_name, u32 line_number, const char *format, ...)
Bob Mooreb8e4d892006-01-27 16:43:00 -05001104{
1105 va_list args;
1106
Bob Moorec5fc42a2007-02-02 19:48:19 +03001107 acpi_os_printf("ACPI: ");
Bob Mooreb8e4d892006-01-27 16:43:00 -05001108
1109 va_start(args, format);
1110 acpi_os_vprintf(format, args);
Bob Moorec5fc42a2007-02-02 19:48:19 +03001111 acpi_os_printf("\n");
Bob Moore507f0462008-04-10 19:06:42 +04001112 va_end(args);
Bob Mooreb8e4d892006-01-27 16:43:00 -05001113}
Bob Moore50df4d82008-12-31 03:01:23 +08001114
1115ACPI_EXPORT_SYMBOL(acpi_error)
1116ACPI_EXPORT_SYMBOL(acpi_exception)
1117ACPI_EXPORT_SYMBOL(acpi_warning)
1118ACPI_EXPORT_SYMBOL(acpi_info)