blob: 577a44e569092e2e108699133bfc8017dc814c48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: utmisc - common utility procedures
4 *
5 ******************************************************************************/
6
7/*
Bob Moore77848132012-01-12 13:27:23 +08008 * Copyright (C) 2000 - 2012, 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
Bob Moore26181bf2012-06-29 09:16:30 +080053#if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
Robert Moore44f6c012005-04-18 22:49:35 -040054/*******************************************************************************
55 *
Bob Moorec6e17332012-05-22 16:26:53 +080056 * FUNCTION: ut_convert_backslashes
57 *
58 * PARAMETERS: Pathname - File pathname string to be converted
59 *
60 * RETURN: Modifies the input Pathname
61 *
62 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
63 * the entire input file pathname string.
64 *
65 ******************************************************************************/
66void ut_convert_backslashes(char *pathname)
67{
68
69 if (!pathname) {
70 return;
71 }
72
73 while (*pathname) {
74 if (*pathname == '\\') {
75 *pathname = '/';
76 }
77
78 pathname++;
79 }
80}
Bob Moore26181bf2012-06-29 09:16:30 +080081#endif
Bob Moorec6e17332012-05-22 16:26:53 +080082
83/*******************************************************************************
84 *
Bob Moore84fb2c92007-02-02 19:48:19 +030085 * FUNCTION: acpi_ut_validate_exception
86 *
87 * PARAMETERS: Status - The acpi_status code to be formatted
88 *
89 * RETURN: A string containing the exception text. NULL if exception is
90 * not valid.
91 *
92 * DESCRIPTION: This function validates and translates an ACPI exception into
93 * an ASCII string.
94 *
95 ******************************************************************************/
Bob Moorec6e17332012-05-22 16:26:53 +080096
Bob Moore84fb2c92007-02-02 19:48:19 +030097const char *acpi_ut_validate_exception(acpi_status status)
98{
Bob Moore11f2a612008-06-10 12:53:01 +080099 u32 sub_status;
Bob Moore84fb2c92007-02-02 19:48:19 +0300100 const char *exception = NULL;
101
102 ACPI_FUNCTION_ENTRY();
103
104 /*
105 * Status is composed of two parts, a "type" and an actual code
106 */
107 sub_status = (status & ~AE_CODE_MASK);
108
109 switch (status & AE_CODE_MASK) {
110 case AE_CODE_ENVIRONMENTAL:
111
112 if (sub_status <= AE_CODE_ENV_MAX) {
113 exception = acpi_gbl_exception_names_env[sub_status];
114 }
115 break;
116
117 case AE_CODE_PROGRAMMER:
118
119 if (sub_status <= AE_CODE_PGM_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800120 exception = acpi_gbl_exception_names_pgm[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300121 }
122 break;
123
124 case AE_CODE_ACPI_TABLES:
125
126 if (sub_status <= AE_CODE_TBL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800127 exception = acpi_gbl_exception_names_tbl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300128 }
129 break;
130
131 case AE_CODE_AML:
132
133 if (sub_status <= AE_CODE_AML_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800134 exception = acpi_gbl_exception_names_aml[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300135 }
136 break;
137
138 case AE_CODE_CONTROL:
139
140 if (sub_status <= AE_CODE_CTRL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800141 exception = acpi_gbl_exception_names_ctrl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300142 }
143 break;
144
145 default:
146 break;
147 }
148
149 return (ACPI_CAST_PTR(const char, exception));
150}
151
152/*******************************************************************************
153 *
Bob Moore15b8dd52009-06-29 13:39:29 +0800154 * FUNCTION: acpi_ut_is_pci_root_bridge
155 *
156 * PARAMETERS: Id - The HID/CID in string format
157 *
158 * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
159 *
160 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
161 *
162 ******************************************************************************/
163
164u8 acpi_ut_is_pci_root_bridge(char *id)
165{
166
167 /*
168 * Check if this is a PCI root bridge.
169 * ACPI 3.0+: check for a PCI Express root also.
170 */
171 if (!(ACPI_STRCMP(id,
172 PCI_ROOT_HID_STRING)) ||
173 !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
174 return (TRUE);
175 }
176
177 return (FALSE);
178}
179
180/*******************************************************************************
181 *
Bob Moore793c2382006-03-31 00:00:00 -0500182 * FUNCTION: acpi_ut_is_aml_table
183 *
184 * PARAMETERS: Table - An ACPI table
185 *
186 * RETURN: TRUE if table contains executable AML; FALSE otherwise
187 *
188 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
189 * Currently, these are DSDT,SSDT,PSDT. All other table types are
190 * data tables that do not contain AML code.
191 *
192 ******************************************************************************/
Bob Moore84fb2c92007-02-02 19:48:19 +0300193
Bob Moore793c2382006-03-31 00:00:00 -0500194u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
195{
196
Bob Mooref6dd9222006-07-07 20:44:38 -0400197 /* These are the only tables that contain executable AML */
Bob Moore793c2382006-03-31 00:00:00 -0500198
Bob Mooref3d2e782007-02-02 19:48:18 +0300199 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
200 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
201 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
Bob Moore793c2382006-03-31 00:00:00 -0500202 return (TRUE);
203 }
204
205 return (FALSE);
206}
207
208/*******************************************************************************
209 *
Robert Mooref9f46012005-07-08 00:00:00 -0400210 * FUNCTION: acpi_ut_allocate_owner_id
211 *
212 * PARAMETERS: owner_id - Where the new owner ID is returned
213 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700214 * RETURN: Status
215 *
216 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
217 * track objects created by the table or method, to be deleted
218 * when the method exits or the table is unloaded.
Robert Mooref9f46012005-07-08 00:00:00 -0400219 *
220 ******************************************************************************/
Bob Moore793c2382006-03-31 00:00:00 -0500221
Len Brown4be44fc2005-08-05 00:44:28 -0400222acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
Robert Mooref9f46012005-07-08 00:00:00 -0400223{
Bob Moore67a119f2008-06-10 13:42:13 +0800224 u32 i;
225 u32 j;
226 u32 k;
Len Brown4be44fc2005-08-05 00:44:28 -0400227 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400228
Bob Mooreb229cf92006-04-21 17:15:00 -0400229 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400230
Robert Mooreaff8c272005-09-02 17:24:17 -0400231 /* Guard against multiple allocations of ID to the same location */
232
233 if (*owner_id) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800234 ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500235 *owner_id));
Robert Mooreaff8c272005-09-02 17:24:17 -0400236 return_ACPI_STATUS(AE_ALREADY_EXISTS);
237 }
238
Robert Moore0c9938c2005-07-29 15:15:00 -0700239 /* Mutex for the global ID mask */
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
242 if (ACPI_FAILURE(status)) {
243 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400244 }
245
Bob Moorec51a4de2005-11-17 13:07:00 -0500246 /*
247 * Find a free owner ID, cycle through all possible IDs on repeated
Bob Moore28f55eb2005-12-02 18:27:00 -0500248 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
249 * to be scanned twice.
Bob Moorec51a4de2005-11-17 13:07:00 -0500250 */
Bob Moore28f55eb2005-12-02 18:27:00 -0500251 for (i = 0, j = acpi_gbl_last_owner_id_index;
252 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
253 if (j >= ACPI_NUM_OWNERID_MASKS) {
254 j = 0; /* Wraparound to start of mask array */
Bob Moorec51a4de2005-11-17 13:07:00 -0500255 }
Robert Mooref9f46012005-07-08 00:00:00 -0400256
Bob Moore28f55eb2005-12-02 18:27:00 -0500257 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
258 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400259
Bob Moore28f55eb2005-12-02 18:27:00 -0500260 /* There are no free IDs in this mask */
Bob Moorec51a4de2005-11-17 13:07:00 -0500261
Bob Moore28f55eb2005-12-02 18:27:00 -0500262 break;
263 }
Bob Moorea18ecf42005-08-15 03:42:00 -0800264
Bob Moore28f55eb2005-12-02 18:27:00 -0500265 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
266 /*
267 * Found a free ID. The actual ID is the bit index plus one,
268 * making zero an invalid Owner ID. Save this as the last ID
269 * allocated and update the global ID mask.
270 */
271 acpi_gbl_owner_id_mask[j] |= (1 << k);
272
273 acpi_gbl_last_owner_id_index = (u8) j;
274 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
275
276 /*
277 * Construct encoded ID from the index and bit position
278 *
279 * Note: Last [j].k (bit 255) is never used and is marked
280 * permanently allocated (prevents +1 overflow)
281 */
282 *owner_id =
283 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
284
285 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400286 "Allocated OwnerId: %2.2X\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500287 (unsigned int)*owner_id));
288 goto exit;
289 }
Robert Mooref9f46012005-07-08 00:00:00 -0400290 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500291
292 acpi_gbl_next_owner_id_offset = 0;
Robert Mooref9f46012005-07-08 00:00:00 -0400293 }
294
295 /*
Bob Moorec51a4de2005-11-17 13:07:00 -0500296 * All owner_ids have been allocated. This typically should
Robert Mooref9f46012005-07-08 00:00:00 -0400297 * not happen since the IDs are reused after deallocation. The IDs are
298 * allocated upon table load (one per table) and method execution, and
299 * they are released when a table is unloaded or a method completes
300 * execution.
Bob Moorec51a4de2005-11-17 13:07:00 -0500301 *
302 * If this error happens, there may be very deep nesting of invoked control
303 * methods, or there may be a bug where the IDs are not released.
Robert Mooref9f46012005-07-08 00:00:00 -0400304 */
305 status = AE_OWNER_ID_LIMIT;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500306 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400307 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
Robert Mooref9f46012005-07-08 00:00:00 -0400308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 exit:
310 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
311 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400312}
313
Robert Mooref9f46012005-07-08 00:00:00 -0400314/*******************************************************************************
315 *
316 * FUNCTION: acpi_ut_release_owner_id
317 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700318 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
Robert Mooref9f46012005-07-08 00:00:00 -0400319 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700320 * RETURN: None. No error is returned because we are either exiting a
321 * control method or unloading a table. Either way, we would
322 * ignore any error anyway.
323 *
Bob Moore28f55eb2005-12-02 18:27:00 -0500324 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
Robert Mooref9f46012005-07-08 00:00:00 -0400325 *
326 ******************************************************************************/
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400329{
Len Brown4be44fc2005-08-05 00:44:28 -0400330 acpi_owner_id owner_id = *owner_id_ptr;
331 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +0800332 u32 index;
Bob Moore28f55eb2005-12-02 18:27:00 -0500333 u32 bit;
Robert Mooref9f46012005-07-08 00:00:00 -0400334
Bob Mooreb229cf92006-04-21 17:15:00 -0400335 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400336
Robert Moore0c9938c2005-07-29 15:15:00 -0700337 /* Always clear the input owner_id (zero is an invalid ID) */
338
339 *owner_id_ptr = 0;
340
341 /* Zero is not a valid owner_iD */
342
Bob Mooredefba1d2005-12-16 17:05:00 -0500343 if (owner_id == 0) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800344 ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700345 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400346 }
347
Robert Moore0c9938c2005-07-29 15:15:00 -0700348 /* Mutex for the global ID mask */
349
Len Brown4be44fc2005-08-05 00:44:28 -0400350 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
351 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700352 return_VOID;
353 }
354
Robert Mooreaff8c272005-09-02 17:24:17 -0400355 /* Normalize the ID to zero */
356
357 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700358
Bob Moore28f55eb2005-12-02 18:27:00 -0500359 /* Decode ID to index/offset pair */
360
361 index = ACPI_DIV_32(owner_id);
362 bit = 1 << ACPI_MOD_32(owner_id);
363
Robert Moore0c9938c2005-07-29 15:15:00 -0700364 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400365
Bob Moore28f55eb2005-12-02 18:27:00 -0500366 if (acpi_gbl_owner_id_mask[index] & bit) {
367 acpi_gbl_owner_id_mask[index] ^= bit;
368 } else {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500369 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800370 "Release of non-allocated OwnerId: 0x%2.2X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500371 owner_id + 1));
Robert Mooref9f46012005-07-08 00:00:00 -0400372 }
Robert Mooref9f46012005-07-08 00:00:00 -0400373
Len Brown4be44fc2005-08-05 00:44:28 -0400374 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700375 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400376}
377
Robert Mooref9f46012005-07-08 00:00:00 -0400378/*******************************************************************************
379 *
Robert Moore44f6c012005-04-18 22:49:35 -0400380 * FUNCTION: acpi_ut_strupr (strupr)
381 *
382 * PARAMETERS: src_string - The source string to convert
383 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700384 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400385 *
386 * DESCRIPTION: Convert string to uppercase
387 *
388 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
389 *
390 ******************************************************************************/
391
Len Brown4be44fc2005-08-05 00:44:28 -0400392void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400393{
Len Brown4be44fc2005-08-05 00:44:28 -0400394 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400397
Robert Moore73459f72005-06-24 00:00:00 -0400398 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700399 return;
Robert Moore73459f72005-06-24 00:00:00 -0400400 }
401
Robert Moore44f6c012005-04-18 22:49:35 -0400402 /* Walk entire string, uppercasing the letters */
403
404 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400405 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400406 }
407
Robert Moore0c9938c2005-07-29 15:15:00 -0700408 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*******************************************************************************
412 *
413 * FUNCTION: acpi_ut_print_string
414 *
415 * PARAMETERS: String - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400416 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * RETURN: None
419 *
420 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
421 * sequences.
422 *
423 ******************************************************************************/
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Len Brown4be44fc2005-08-05 00:44:28 -0400427 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400430 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return;
432 }
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 for (i = 0; string[i] && (i < max_length); i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Escape sequences */
438
439 switch (string[i]) {
440 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400441 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443
444 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400445 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
447
448 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400449 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451
452 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400453 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455
456 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400457 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459
460 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400461 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463
464 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400465 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467
Len Brown4be44fc2005-08-05 00:44:28 -0400468 case '\'': /* Single Quote */
469 case '\"': /* Double Quote */
470 case '\\': /* Backslash */
471 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473
474 default:
475
476 /* Check for printable character or hex escape */
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* This is a normal character */
480
Len Brown4be44fc2005-08-05 00:44:28 -0400481 acpi_os_printf("%c", (int)string[i]);
482 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* All others will be Hex escapes */
484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 break;
488 }
489 }
Len Brown4be44fc2005-08-05 00:44:28 -0400490 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400493 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*******************************************************************************
498 *
499 * FUNCTION: acpi_ut_dword_byte_swap
500 *
501 * PARAMETERS: Value - Value to be converted
502 *
Robert Moore44f6c012005-04-18 22:49:35 -0400503 * RETURN: u32 integer with bytes swapped
504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
506 *
507 ******************************************************************************/
508
Len Brown4be44fc2005-08-05 00:44:28 -0400509u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400512 u32 value;
513 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400516 u32 value;
517 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } in;
519
Len Brown4be44fc2005-08-05 00:44:28 -0400520 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 in.value = value;
523
524 out.bytes[0] = in.bytes[3];
525 out.bytes[1] = in.bytes[2];
526 out.bytes[2] = in.bytes[1];
527 out.bytes[3] = in.bytes[0];
528
529 return (out.value);
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*******************************************************************************
533 *
534 * FUNCTION: acpi_ut_set_integer_width
535 *
536 * PARAMETERS: Revision From DSDT header
537 *
538 * RETURN: None
539 *
540 * DESCRIPTION: Set the global integer bit width based upon the revision
541 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
542 * For Revision 2 and above, Integers are 64 bits. Yes, this
543 * makes a difference.
544 *
545 ******************************************************************************/
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549
Bob Mooref3d2e782007-02-02 19:48:18 +0300550 if (revision < 2) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400551
552 /* 32-bit case */
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 acpi_gbl_integer_bit_width = 32;
555 acpi_gbl_integer_nybble_width = 8;
556 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400557 } else {
Bob Mooref6dd9222006-07-07 20:44:38 -0400558 /* 64-bit case (ACPI 2.0+) */
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 acpi_gbl_integer_bit_width = 64;
561 acpi_gbl_integer_nybble_width = 16;
562 acpi_gbl_integer_byte_width = 8;
563 }
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#ifdef ACPI_DEBUG_OUTPUT
567/*******************************************************************************
568 *
569 * FUNCTION: acpi_ut_display_init_pathname
570 *
Robert Moore44f6c012005-04-18 22:49:35 -0400571 * PARAMETERS: Type - Object type of the node
572 * obj_handle - Handle whose pathname will be displayed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * Path - Additional path string to be appended.
574 * (NULL if no extra path)
575 *
576 * RETURN: acpi_status
577 *
578 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
579 *
580 ******************************************************************************/
581
582void
Len Brown4be44fc2005-08-05 00:44:28 -0400583acpi_ut_display_init_pathname(u8 type,
584 struct acpi_namespace_node *obj_handle,
585 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Len Brown4be44fc2005-08-05 00:44:28 -0400587 acpi_status status;
588 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Len Brown4be44fc2005-08-05 00:44:28 -0400590 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Only print the path if the appropriate debug level is enabled */
593
594 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
595 return;
596 }
597
598 /* Get the full pathname to the node */
599
600 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400601 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
602 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return;
604 }
605
606 /* Print what we're doing */
607
608 switch (type) {
609 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400610 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
612
613 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400614 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
616 }
617
618 /* Print the object type and pathname */
619
Len Brown4be44fc2005-08-05 00:44:28 -0400620 acpi_os_printf("%-12s %s",
621 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Extra path is used to append names like _STA, _INI, etc. */
624
625 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400626 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Len Brown4be44fc2005-08-05 00:44:28 -0400628 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Bob Moore83135242006-10-03 00:00:00 -0400630 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632#endif
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*******************************************************************************
635 *
Bob Moore793c2382006-03-31 00:00:00 -0500636 * FUNCTION: acpi_ut_valid_acpi_char
637 *
638 * PARAMETERS: Char - The character to be examined
Bob Mooref6dd9222006-07-07 20:44:38 -0400639 * Position - Byte position (0-3)
Bob Moore793c2382006-03-31 00:00:00 -0500640 *
641 * RETURN: TRUE if the character is valid, FALSE otherwise
642 *
643 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
644 * 1) Upper case alpha
645 * 2) numeric
646 * 3) underscore
647 *
648 * We allow a '!' as the last character because of the ASF! table
649 *
650 ******************************************************************************/
651
Bob Moore67a119f2008-06-10 13:42:13 +0800652u8 acpi_ut_valid_acpi_char(char character, u32 position)
Bob Moore793c2382006-03-31 00:00:00 -0500653{
654
655 if (!((character >= 'A' && character <= 'Z') ||
656 (character >= '0' && character <= '9') || (character == '_'))) {
657
658 /* Allow a '!' in the last position */
659
660 if (character == '!' && position == 3) {
661 return (TRUE);
662 }
663
664 return (FALSE);
665 }
666
667 return (TRUE);
668}
669
670/*******************************************************************************
671 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * FUNCTION: acpi_ut_valid_acpi_name
673 *
Robert Moore44f6c012005-04-18 22:49:35 -0400674 * PARAMETERS: Name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Robert Moore44f6c012005-04-18 22:49:35 -0400676 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
678 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
679 * 1) Upper case alpha
680 * 2) numeric
681 * 3) underscore
682 *
683 ******************************************************************************/
684
Len Brown4be44fc2005-08-05 00:44:28 -0400685u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Bob Moore67a119f2008-06-10 13:42:13 +0800687 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Len Brown4be44fc2005-08-05 00:44:28 -0400689 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore793c2382006-03-31 00:00:00 -0500692 if (!acpi_ut_valid_acpi_char
693 ((ACPI_CAST_PTR(char, &name))[i], i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return (FALSE);
695 }
696 }
697
698 return (TRUE);
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*******************************************************************************
702 *
Bob Moore793c2382006-03-31 00:00:00 -0500703 * FUNCTION: acpi_ut_repair_name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
Bob Moore793c2382006-03-31 00:00:00 -0500705 * PARAMETERS: Name - The ACPI name to be repaired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
Bob Moore793c2382006-03-31 00:00:00 -0500707 * RETURN: Repaired version of the name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
Bob Moore793c2382006-03-31 00:00:00 -0500709 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
710 * return the new name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
712 ******************************************************************************/
713
Bob Moore3d81b232007-02-02 19:48:19 +0300714acpi_name acpi_ut_repair_name(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Bob Moore67a119f2008-06-10 13:42:13 +0800716 u32 i;
Bob Moore3d81b232007-02-02 19:48:19 +0300717 char new_name[ACPI_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Bob Moore793c2382006-03-31 00:00:00 -0500719 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore3d81b232007-02-02 19:48:19 +0300720 new_name[i] = name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Bob Moore793c2382006-03-31 00:00:00 -0500722 /*
723 * Replace a bad character with something printable, yet technically
724 * still invalid. This prevents any collisions with existing "good"
725 * names in the namespace.
726 */
Bob Moore3d81b232007-02-02 19:48:19 +0300727 if (!acpi_ut_valid_acpi_char(name[i], i)) {
Bob Moore793c2382006-03-31 00:00:00 -0500728 new_name[i] = '*';
729 }
730 }
731
Bob Moore3d81b232007-02-02 19:48:19 +0300732 return (*(u32 *) new_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*******************************************************************************
736 *
737 * FUNCTION: acpi_ut_strtoul64
738 *
739 * PARAMETERS: String - Null terminated string
Bob Moore41195322006-05-26 16:36:00 -0400740 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
741 * ACPI_ANY_BASE means 'in behalf of to_integer'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * ret_integer - Where the converted integer is returned
743 *
744 * RETURN: Status and Converted value
745 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400746 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
747 * 32-bit or 64-bit conversion, depending on the current mode
748 * of the interpreter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * NOTE: Does not support Octal strings, not needed.
750 *
751 ******************************************************************************/
752
Bob Moore5df7e6c2010-01-21 10:06:32 +0800753acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Len Brown4be44fc2005-08-05 00:44:28 -0400755 u32 this_digit = 0;
Bob Moore5df7e6c2010-01-21 10:06:32 +0800756 u64 return_value = 0;
757 u64 quotient;
758 u64 dividend;
Bob Moore41195322006-05-26 16:36:00 -0400759 u32 to_integer_op = (base == ACPI_ANY_BASE);
760 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
761 u8 valid_digits = 0;
762 u8 sign_of0x = 0;
763 u8 term = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Bob Mooref6dd9222006-07-07 20:44:38 -0400765 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 switch (base) {
768 case ACPI_ANY_BASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 case 16:
770 break;
771
772 default:
773 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400774 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Bob Moore41195322006-05-26 16:36:00 -0400777 if (!string) {
778 goto error_exit;
779 }
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Skip over any white space in the buffer */
782
Bob Moore41195322006-05-26 16:36:00 -0400783 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 string++;
785 }
786
Bob Moore41195322006-05-26 16:36:00 -0400787 if (to_integer_op) {
788 /*
789 * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
790 * We need to determine if it is decimal or hexadecimal.
791 */
Len Brown4be44fc2005-08-05 00:44:28 -0400792 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Bob Moore41195322006-05-26 16:36:00 -0400793 sign_of0x = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 base = 16;
Bob Moore41195322006-05-26 16:36:00 -0400795
796 /* Skip over the leading '0x' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400798 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 base = 10;
800 }
801 }
802
Bob Moore41195322006-05-26 16:36:00 -0400803 /* Any string left? Check that '0x' is not followed by white space. */
804
805 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
806 if (to_integer_op) {
807 goto error_exit;
808 } else {
809 goto all_done;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Bob Mooref6dd9222006-07-07 20:44:38 -0400813 /*
814 * Perform a 32-bit or 64-bit conversion, depending upon the current
815 * execution mode of the interpreter
816 */
Bob Moore41195322006-05-26 16:36:00 -0400817 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Bob Mooref6dd9222006-07-07 20:44:38 -0400819 /* Main loop: convert the string to a 32- or 64-bit integer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400822 if (ACPI_IS_DIGIT(*string)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Convert ASCII 0-9 to Decimal value */
825
Len Brown4be44fc2005-08-05 00:44:28 -0400826 this_digit = ((u8) * string) - '0';
Bob Moore41195322006-05-26 16:36:00 -0400827 } else if (base == 10) {
828
829 /* Digit is out of range; possible in to_integer case only */
830
831 term = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400832 } else {
Len Brown4be44fc2005-08-05 00:44:28 -0400833 this_digit = (u8) ACPI_TOUPPER(*string);
834 if (ACPI_IS_XDIGIT((char)this_digit)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* Convert ASCII Hex char to value */
837
838 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400839 } else {
Bob Moore41195322006-05-26 16:36:00 -0400840 term = 1;
841 }
842 }
843
844 if (term) {
845 if (to_integer_op) {
846 goto error_exit;
847 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 break;
849 }
Bob Moore41195322006-05-26 16:36:00 -0400850 } else if ((valid_digits == 0) && (this_digit == 0)
851 && !sign_of0x) {
852
853 /* Skip zeros */
854 string++;
855 continue;
856 }
857
858 valid_digits++;
859
Len Brownfd350942007-05-09 23:34:35 -0400860 if (sign_of0x && ((valid_digits > 16)
861 || ((valid_digits > 8) && mode32))) {
Bob Moore41195322006-05-26 16:36:00 -0400862 /*
863 * This is to_integer operation case.
864 * No any restrictions for string-to-integer conversion,
865 * see ACPI spec.
866 */
867 goto error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 /* Divide the digit into the correct position */
871
Bob Moore5df7e6c2010-01-21 10:06:32 +0800872 (void)acpi_ut_short_divide((dividend - (u64) this_digit),
873 base, &quotient, NULL);
Bob Moore41195322006-05-26 16:36:00 -0400874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (return_value > quotient) {
Bob Moore41195322006-05-26 16:36:00 -0400876 if (to_integer_op) {
877 goto error_exit;
878 } else {
879 break;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
883 return_value *= base;
884 return_value += this_digit;
885 string++;
886 }
887
888 /* All done, normal exit */
889
Bob Moore41195322006-05-26 16:36:00 -0400890 all_done:
891
Bob Mooref6dd9222006-07-07 20:44:38 -0400892 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
893 ACPI_FORMAT_UINT64(return_value)));
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400896 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Len Brown4be44fc2005-08-05 00:44:28 -0400898 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Base was set/validated above */
900
901 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400902 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
903 } else {
904 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/*******************************************************************************
909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * FUNCTION: acpi_ut_create_update_state_and_push
911 *
Robert Moore44f6c012005-04-18 22:49:35 -0400912 * PARAMETERS: Object - Object to be added to the new state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * Action - Increment/Decrement
914 * state_list - List the state will be added to
915 *
Robert Moore44f6c012005-04-18 22:49:35 -0400916 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 * DESCRIPTION: Create a new state and push it
919 *
920 ******************************************************************************/
921
922acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400923acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
924 u16 action,
925 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Len Brown4be44fc2005-08-05 00:44:28 -0400927 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Len Brown4be44fc2005-08-05 00:44:28 -0400929 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Ignore null objects; these are expected */
932
933 if (!object) {
934 return (AE_OK);
935 }
936
Len Brown4be44fc2005-08-05 00:44:28 -0400937 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!state) {
939 return (AE_NO_MEMORY);
940 }
941
Len Brown4be44fc2005-08-05 00:44:28 -0400942 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return (AE_OK);
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*******************************************************************************
947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * FUNCTION: acpi_ut_walk_package_tree
949 *
Robert Moore44f6c012005-04-18 22:49:35 -0400950 * PARAMETERS: source_object - The package to walk
951 * target_object - Target object (if package is being copied)
952 * walk_callback - Called once for each package element
953 * Context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
955 * RETURN: Status
956 *
957 * DESCRIPTION: Walk through a package
958 *
959 ******************************************************************************/
960
961acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400962acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
963 void *target_object,
964 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Len Brown4be44fc2005-08-05 00:44:28 -0400966 acpi_status status = AE_OK;
967 union acpi_generic_state *state_list = NULL;
968 union acpi_generic_state *state;
969 u32 this_index;
970 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Bob Mooreb229cf92006-04-21 17:15:00 -0400972 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Len Brown4be44fc2005-08-05 00:44:28 -0400974 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400976 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
979 while (state) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* Get one element of the package */
982
Len Brown4be44fc2005-08-05 00:44:28 -0400983 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400985 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /*
988 * Check for:
989 * 1) An uninitialized package element. It is completely
990 * legal to declare a package and leave it uninitialized
991 * 2) Not an internal object - can be a namespace node instead
992 * 3) Any type other than a package. Packages are handled in else
993 * case below.
994 */
995 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400996 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
997 ACPI_DESC_TYPE_OPERAND)
Bob Moore3371c192009-02-18 14:44:03 +0800998 || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400999 status =
1000 walk_callback(ACPI_COPY_TYPE_SIMPLE,
1001 this_source_obj, state, context);
1002 if (ACPI_FAILURE(status)) {
1003 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005
1006 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -04001007 while (state->pkg.index >=
1008 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * We've handled all of the objects at this level, This means
1011 * that we have just completed a package. That package may
1012 * have contained one or more packages itself.
1013 *
1014 * Delete this state and pop the previous state (package).
1015 */
Len Brown4be44fc2005-08-05 00:44:28 -04001016 acpi_ut_delete_generic_state(state);
1017 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* Finished when there are no more states */
1020
1021 if (!state) {
1022 /*
1023 * We have handled all of the objects in the top level
1024 * package just add the length of the package objects
1025 * and exit
1026 */
Len Brown4be44fc2005-08-05 00:44:28 -04001027 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 /*
1031 * Go back up a level and move the index past the just
1032 * completed package object.
1033 */
1034 state->pkg.index++;
1035 }
Len Brown4be44fc2005-08-05 00:44:28 -04001036 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /* This is a subobject of type package */
1038
Len Brown4be44fc2005-08-05 00:44:28 -04001039 status =
1040 walk_callback(ACPI_COPY_TYPE_PACKAGE,
1041 this_source_obj, state, context);
1042 if (ACPI_FAILURE(status)) {
1043 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
1046 /*
1047 * Push the current state and create a new one
1048 * The callback above returned a new target package object.
1049 */
Len Brown4be44fc2005-08-05 00:44:28 -04001050 acpi_ut_push_generic_state(&state_list, state);
1051 state = acpi_ut_create_pkg_state(this_source_obj,
1052 state->pkg.
1053 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!state) {
Lin Mingcf058bd2008-09-27 11:29:57 +08001055
1056 /* Free any stacked Update State objects */
1057
1058 while (state_list) {
1059 state =
1060 acpi_ut_pop_generic_state
1061 (&state_list);
1062 acpi_ut_delete_generic_state(state);
1063 }
Len Brown4be44fc2005-08-05 00:44:28 -04001064 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 }
1067 }
1068
1069 /* We should never get here */
1070
Len Brown4be44fc2005-08-05 00:44:28 -04001071 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}