blob: e86f897767e8838cb9f10cfc060f218ce6786217 [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
53/*******************************************************************************
54 *
Bob Moorec6e17332012-05-22 16:26:53 +080055 * FUNCTION: ut_convert_backslashes
56 *
57 * PARAMETERS: Pathname - File pathname string to be converted
58 *
59 * RETURN: Modifies the input Pathname
60 *
61 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
62 * the entire input file pathname string.
63 *
64 ******************************************************************************/
65void ut_convert_backslashes(char *pathname)
66{
67
68 if (!pathname) {
69 return;
70 }
71
72 while (*pathname) {
73 if (*pathname == '\\') {
74 *pathname = '/';
75 }
76
77 pathname++;
78 }
79}
80
81/*******************************************************************************
82 *
Bob Moore84fb2c92007-02-02 19:48:19 +030083 * FUNCTION: acpi_ut_validate_exception
84 *
85 * PARAMETERS: Status - The acpi_status code to be formatted
86 *
87 * RETURN: A string containing the exception text. NULL if exception is
88 * not valid.
89 *
90 * DESCRIPTION: This function validates and translates an ACPI exception into
91 * an ASCII string.
92 *
93 ******************************************************************************/
Bob Moorec6e17332012-05-22 16:26:53 +080094
Bob Moore84fb2c92007-02-02 19:48:19 +030095const char *acpi_ut_validate_exception(acpi_status status)
96{
Bob Moore11f2a612008-06-10 12:53:01 +080097 u32 sub_status;
Bob Moore84fb2c92007-02-02 19:48:19 +030098 const char *exception = NULL;
99
100 ACPI_FUNCTION_ENTRY();
101
102 /*
103 * Status is composed of two parts, a "type" and an actual code
104 */
105 sub_status = (status & ~AE_CODE_MASK);
106
107 switch (status & AE_CODE_MASK) {
108 case AE_CODE_ENVIRONMENTAL:
109
110 if (sub_status <= AE_CODE_ENV_MAX) {
111 exception = acpi_gbl_exception_names_env[sub_status];
112 }
113 break;
114
115 case AE_CODE_PROGRAMMER:
116
117 if (sub_status <= AE_CODE_PGM_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800118 exception = acpi_gbl_exception_names_pgm[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300119 }
120 break;
121
122 case AE_CODE_ACPI_TABLES:
123
124 if (sub_status <= AE_CODE_TBL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800125 exception = acpi_gbl_exception_names_tbl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300126 }
127 break;
128
129 case AE_CODE_AML:
130
131 if (sub_status <= AE_CODE_AML_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800132 exception = acpi_gbl_exception_names_aml[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300133 }
134 break;
135
136 case AE_CODE_CONTROL:
137
138 if (sub_status <= AE_CODE_CTRL_MAX) {
Bob Moore11f2a612008-06-10 12:53:01 +0800139 exception = acpi_gbl_exception_names_ctrl[sub_status];
Bob Moore84fb2c92007-02-02 19:48:19 +0300140 }
141 break;
142
143 default:
144 break;
145 }
146
147 return (ACPI_CAST_PTR(const char, exception));
148}
149
150/*******************************************************************************
151 *
Bob Moore15b8dd52009-06-29 13:39:29 +0800152 * FUNCTION: acpi_ut_is_pci_root_bridge
153 *
154 * PARAMETERS: Id - The HID/CID in string format
155 *
156 * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
157 *
158 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
159 *
160 ******************************************************************************/
161
162u8 acpi_ut_is_pci_root_bridge(char *id)
163{
164
165 /*
166 * Check if this is a PCI root bridge.
167 * ACPI 3.0+: check for a PCI Express root also.
168 */
169 if (!(ACPI_STRCMP(id,
170 PCI_ROOT_HID_STRING)) ||
171 !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
172 return (TRUE);
173 }
174
175 return (FALSE);
176}
177
178/*******************************************************************************
179 *
Bob Moore793c2382006-03-31 00:00:00 -0500180 * FUNCTION: acpi_ut_is_aml_table
181 *
182 * PARAMETERS: Table - An ACPI table
183 *
184 * RETURN: TRUE if table contains executable AML; FALSE otherwise
185 *
186 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
187 * Currently, these are DSDT,SSDT,PSDT. All other table types are
188 * data tables that do not contain AML code.
189 *
190 ******************************************************************************/
Bob Moore84fb2c92007-02-02 19:48:19 +0300191
Bob Moore793c2382006-03-31 00:00:00 -0500192u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
193{
194
Bob Mooref6dd9222006-07-07 20:44:38 -0400195 /* These are the only tables that contain executable AML */
Bob Moore793c2382006-03-31 00:00:00 -0500196
Bob Mooref3d2e782007-02-02 19:48:18 +0300197 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
198 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
199 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
Bob Moore793c2382006-03-31 00:00:00 -0500200 return (TRUE);
201 }
202
203 return (FALSE);
204}
205
206/*******************************************************************************
207 *
Robert Mooref9f46012005-07-08 00:00:00 -0400208 * FUNCTION: acpi_ut_allocate_owner_id
209 *
210 * PARAMETERS: owner_id - Where the new owner ID is returned
211 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700212 * RETURN: Status
213 *
214 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
215 * track objects created by the table or method, to be deleted
216 * when the method exits or the table is unloaded.
Robert Mooref9f46012005-07-08 00:00:00 -0400217 *
218 ******************************************************************************/
Bob Moore793c2382006-03-31 00:00:00 -0500219
Len Brown4be44fc2005-08-05 00:44:28 -0400220acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
Robert Mooref9f46012005-07-08 00:00:00 -0400221{
Bob Moore67a119f2008-06-10 13:42:13 +0800222 u32 i;
223 u32 j;
224 u32 k;
Len Brown4be44fc2005-08-05 00:44:28 -0400225 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400226
Bob Mooreb229cf92006-04-21 17:15:00 -0400227 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400228
Robert Mooreaff8c272005-09-02 17:24:17 -0400229 /* Guard against multiple allocations of ID to the same location */
230
231 if (*owner_id) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800232 ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500233 *owner_id));
Robert Mooreaff8c272005-09-02 17:24:17 -0400234 return_ACPI_STATUS(AE_ALREADY_EXISTS);
235 }
236
Robert Moore0c9938c2005-07-29 15:15:00 -0700237 /* Mutex for the global ID mask */
238
Len Brown4be44fc2005-08-05 00:44:28 -0400239 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
240 if (ACPI_FAILURE(status)) {
241 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400242 }
243
Bob Moorec51a4de2005-11-17 13:07:00 -0500244 /*
245 * Find a free owner ID, cycle through all possible IDs on repeated
Bob Moore28f55eb2005-12-02 18:27:00 -0500246 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
247 * to be scanned twice.
Bob Moorec51a4de2005-11-17 13:07:00 -0500248 */
Bob Moore28f55eb2005-12-02 18:27:00 -0500249 for (i = 0, j = acpi_gbl_last_owner_id_index;
250 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
251 if (j >= ACPI_NUM_OWNERID_MASKS) {
252 j = 0; /* Wraparound to start of mask array */
Bob Moorec51a4de2005-11-17 13:07:00 -0500253 }
Robert Mooref9f46012005-07-08 00:00:00 -0400254
Bob Moore28f55eb2005-12-02 18:27:00 -0500255 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
256 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400257
Bob Moore28f55eb2005-12-02 18:27:00 -0500258 /* There are no free IDs in this mask */
Bob Moorec51a4de2005-11-17 13:07:00 -0500259
Bob Moore28f55eb2005-12-02 18:27:00 -0500260 break;
261 }
Bob Moorea18ecf42005-08-15 03:42:00 -0800262
Bob Moore28f55eb2005-12-02 18:27:00 -0500263 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
264 /*
265 * Found a free ID. The actual ID is the bit index plus one,
266 * making zero an invalid Owner ID. Save this as the last ID
267 * allocated and update the global ID mask.
268 */
269 acpi_gbl_owner_id_mask[j] |= (1 << k);
270
271 acpi_gbl_last_owner_id_index = (u8) j;
272 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
273
274 /*
275 * Construct encoded ID from the index and bit position
276 *
277 * Note: Last [j].k (bit 255) is never used and is marked
278 * permanently allocated (prevents +1 overflow)
279 */
280 *owner_id =
281 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
282
283 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400284 "Allocated OwnerId: %2.2X\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500285 (unsigned int)*owner_id));
286 goto exit;
287 }
Robert Mooref9f46012005-07-08 00:00:00 -0400288 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500289
290 acpi_gbl_next_owner_id_offset = 0;
Robert Mooref9f46012005-07-08 00:00:00 -0400291 }
292
293 /*
Bob Moorec51a4de2005-11-17 13:07:00 -0500294 * All owner_ids have been allocated. This typically should
Robert Mooref9f46012005-07-08 00:00:00 -0400295 * not happen since the IDs are reused after deallocation. The IDs are
296 * allocated upon table load (one per table) and method execution, and
297 * they are released when a table is unloaded or a method completes
298 * execution.
Bob Moorec51a4de2005-11-17 13:07:00 -0500299 *
300 * If this error happens, there may be very deep nesting of invoked control
301 * methods, or there may be a bug where the IDs are not released.
Robert Mooref9f46012005-07-08 00:00:00 -0400302 */
303 status = AE_OWNER_ID_LIMIT;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500304 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400305 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
Robert Mooref9f46012005-07-08 00:00:00 -0400306
Len Brown4be44fc2005-08-05 00:44:28 -0400307 exit:
308 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
309 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400310}
311
Robert Mooref9f46012005-07-08 00:00:00 -0400312/*******************************************************************************
313 *
314 * FUNCTION: acpi_ut_release_owner_id
315 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700316 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
Robert Mooref9f46012005-07-08 00:00:00 -0400317 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700318 * RETURN: None. No error is returned because we are either exiting a
319 * control method or unloading a table. Either way, we would
320 * ignore any error anyway.
321 *
Bob Moore28f55eb2005-12-02 18:27:00 -0500322 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
Robert Mooref9f46012005-07-08 00:00:00 -0400323 *
324 ******************************************************************************/
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400327{
Len Brown4be44fc2005-08-05 00:44:28 -0400328 acpi_owner_id owner_id = *owner_id_ptr;
329 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +0800330 u32 index;
Bob Moore28f55eb2005-12-02 18:27:00 -0500331 u32 bit;
Robert Mooref9f46012005-07-08 00:00:00 -0400332
Bob Mooreb229cf92006-04-21 17:15:00 -0400333 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400334
Robert Moore0c9938c2005-07-29 15:15:00 -0700335 /* Always clear the input owner_id (zero is an invalid ID) */
336
337 *owner_id_ptr = 0;
338
339 /* Zero is not a valid owner_iD */
340
Bob Mooredefba1d2005-12-16 17:05:00 -0500341 if (owner_id == 0) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800342 ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700343 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400344 }
345
Robert Moore0c9938c2005-07-29 15:15:00 -0700346 /* Mutex for the global ID mask */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
349 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700350 return_VOID;
351 }
352
Robert Mooreaff8c272005-09-02 17:24:17 -0400353 /* Normalize the ID to zero */
354
355 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700356
Bob Moore28f55eb2005-12-02 18:27:00 -0500357 /* Decode ID to index/offset pair */
358
359 index = ACPI_DIV_32(owner_id);
360 bit = 1 << ACPI_MOD_32(owner_id);
361
Robert Moore0c9938c2005-07-29 15:15:00 -0700362 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400363
Bob Moore28f55eb2005-12-02 18:27:00 -0500364 if (acpi_gbl_owner_id_mask[index] & bit) {
365 acpi_gbl_owner_id_mask[index] ^= bit;
366 } else {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500367 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800368 "Release of non-allocated OwnerId: 0x%2.2X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500369 owner_id + 1));
Robert Mooref9f46012005-07-08 00:00:00 -0400370 }
Robert Mooref9f46012005-07-08 00:00:00 -0400371
Len Brown4be44fc2005-08-05 00:44:28 -0400372 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700373 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400374}
375
Robert Mooref9f46012005-07-08 00:00:00 -0400376/*******************************************************************************
377 *
Robert Moore44f6c012005-04-18 22:49:35 -0400378 * FUNCTION: acpi_ut_strupr (strupr)
379 *
380 * PARAMETERS: src_string - The source string to convert
381 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700382 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400383 *
384 * DESCRIPTION: Convert string to uppercase
385 *
386 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
387 *
388 ******************************************************************************/
389
Len Brown4be44fc2005-08-05 00:44:28 -0400390void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400391{
Len Brown4be44fc2005-08-05 00:44:28 -0400392 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400395
Robert Moore73459f72005-06-24 00:00:00 -0400396 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700397 return;
Robert Moore73459f72005-06-24 00:00:00 -0400398 }
399
Robert Moore44f6c012005-04-18 22:49:35 -0400400 /* Walk entire string, uppercasing the letters */
401
402 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400403 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400404 }
405
Robert Moore0c9938c2005-07-29 15:15:00 -0700406 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*******************************************************************************
410 *
411 * FUNCTION: acpi_ut_print_string
412 *
413 * PARAMETERS: String - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400414 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
416 * RETURN: None
417 *
418 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
419 * sequences.
420 *
421 ******************************************************************************/
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Len Brown4be44fc2005-08-05 00:44:28 -0400425 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400428 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return;
430 }
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 for (i = 0; string[i] && (i < max_length); i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* Escape sequences */
436
437 switch (string[i]) {
438 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400439 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441
442 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400443 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445
446 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400447 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449
450 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400451 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453
454 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400455 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457
458 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400459 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461
462 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400463 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 case '\'': /* Single Quote */
467 case '\"': /* Double Quote */
468 case '\\': /* Backslash */
469 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471
472 default:
473
474 /* Check for printable character or hex escape */
475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* This is a normal character */
478
Len Brown4be44fc2005-08-05 00:44:28 -0400479 acpi_os_printf("%c", (int)string[i]);
480 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* All others will be Hex escapes */
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 break;
486 }
487 }
Len Brown4be44fc2005-08-05 00:44:28 -0400488 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400491 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*******************************************************************************
496 *
497 * FUNCTION: acpi_ut_dword_byte_swap
498 *
499 * PARAMETERS: Value - Value to be converted
500 *
Robert Moore44f6c012005-04-18 22:49:35 -0400501 * RETURN: u32 integer with bytes swapped
502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
504 *
505 ******************************************************************************/
506
Len Brown4be44fc2005-08-05 00:44:28 -0400507u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400510 u32 value;
511 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400514 u32 value;
515 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } in;
517
Len Brown4be44fc2005-08-05 00:44:28 -0400518 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 in.value = value;
521
522 out.bytes[0] = in.bytes[3];
523 out.bytes[1] = in.bytes[2];
524 out.bytes[2] = in.bytes[1];
525 out.bytes[3] = in.bytes[0];
526
527 return (out.value);
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530/*******************************************************************************
531 *
532 * FUNCTION: acpi_ut_set_integer_width
533 *
534 * PARAMETERS: Revision From DSDT header
535 *
536 * RETURN: None
537 *
538 * DESCRIPTION: Set the global integer bit width based upon the revision
539 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
540 * For Revision 2 and above, Integers are 64 bits. Yes, this
541 * makes a difference.
542 *
543 ******************************************************************************/
544
Len Brown4be44fc2005-08-05 00:44:28 -0400545void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547
Bob Mooref3d2e782007-02-02 19:48:18 +0300548 if (revision < 2) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400549
550 /* 32-bit case */
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 acpi_gbl_integer_bit_width = 32;
553 acpi_gbl_integer_nybble_width = 8;
554 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400555 } else {
Bob Mooref6dd9222006-07-07 20:44:38 -0400556 /* 64-bit case (ACPI 2.0+) */
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 acpi_gbl_integer_bit_width = 64;
559 acpi_gbl_integer_nybble_width = 16;
560 acpi_gbl_integer_byte_width = 8;
561 }
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#ifdef ACPI_DEBUG_OUTPUT
565/*******************************************************************************
566 *
567 * FUNCTION: acpi_ut_display_init_pathname
568 *
Robert Moore44f6c012005-04-18 22:49:35 -0400569 * PARAMETERS: Type - Object type of the node
570 * obj_handle - Handle whose pathname will be displayed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * Path - Additional path string to be appended.
572 * (NULL if no extra path)
573 *
574 * RETURN: acpi_status
575 *
576 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
577 *
578 ******************************************************************************/
579
580void
Len Brown4be44fc2005-08-05 00:44:28 -0400581acpi_ut_display_init_pathname(u8 type,
582 struct acpi_namespace_node *obj_handle,
583 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Len Brown4be44fc2005-08-05 00:44:28 -0400585 acpi_status status;
586 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Len Brown4be44fc2005-08-05 00:44:28 -0400588 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* Only print the path if the appropriate debug level is enabled */
591
592 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
593 return;
594 }
595
596 /* Get the full pathname to the node */
597
598 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400599 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
600 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return;
602 }
603
604 /* Print what we're doing */
605
606 switch (type) {
607 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400608 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 break;
610
611 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400612 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 }
615
616 /* Print the object type and pathname */
617
Len Brown4be44fc2005-08-05 00:44:28 -0400618 acpi_os_printf("%-12s %s",
619 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* Extra path is used to append names like _STA, _INI, etc. */
622
623 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400624 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Len Brown4be44fc2005-08-05 00:44:28 -0400626 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Bob Moore83135242006-10-03 00:00:00 -0400628 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630#endif
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*******************************************************************************
633 *
Bob Moore793c2382006-03-31 00:00:00 -0500634 * FUNCTION: acpi_ut_valid_acpi_char
635 *
636 * PARAMETERS: Char - The character to be examined
Bob Mooref6dd9222006-07-07 20:44:38 -0400637 * Position - Byte position (0-3)
Bob Moore793c2382006-03-31 00:00:00 -0500638 *
639 * RETURN: TRUE if the character is valid, FALSE otherwise
640 *
641 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
642 * 1) Upper case alpha
643 * 2) numeric
644 * 3) underscore
645 *
646 * We allow a '!' as the last character because of the ASF! table
647 *
648 ******************************************************************************/
649
Bob Moore67a119f2008-06-10 13:42:13 +0800650u8 acpi_ut_valid_acpi_char(char character, u32 position)
Bob Moore793c2382006-03-31 00:00:00 -0500651{
652
653 if (!((character >= 'A' && character <= 'Z') ||
654 (character >= '0' && character <= '9') || (character == '_'))) {
655
656 /* Allow a '!' in the last position */
657
658 if (character == '!' && position == 3) {
659 return (TRUE);
660 }
661
662 return (FALSE);
663 }
664
665 return (TRUE);
666}
667
668/*******************************************************************************
669 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * FUNCTION: acpi_ut_valid_acpi_name
671 *
Robert Moore44f6c012005-04-18 22:49:35 -0400672 * PARAMETERS: Name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
Robert Moore44f6c012005-04-18 22:49:35 -0400674 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
676 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
677 * 1) Upper case alpha
678 * 2) numeric
679 * 3) underscore
680 *
681 ******************************************************************************/
682
Len Brown4be44fc2005-08-05 00:44:28 -0400683u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Bob Moore67a119f2008-06-10 13:42:13 +0800685 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Len Brown4be44fc2005-08-05 00:44:28 -0400687 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore793c2382006-03-31 00:00:00 -0500690 if (!acpi_ut_valid_acpi_char
691 ((ACPI_CAST_PTR(char, &name))[i], i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return (FALSE);
693 }
694 }
695
696 return (TRUE);
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*******************************************************************************
700 *
Bob Moore793c2382006-03-31 00:00:00 -0500701 * FUNCTION: acpi_ut_repair_name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Bob Moore793c2382006-03-31 00:00:00 -0500703 * PARAMETERS: Name - The ACPI name to be repaired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
Bob Moore793c2382006-03-31 00:00:00 -0500705 * RETURN: Repaired version of the name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 *
Bob Moore793c2382006-03-31 00:00:00 -0500707 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
708 * return the new name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
710 ******************************************************************************/
711
Bob Moore3d81b232007-02-02 19:48:19 +0300712acpi_name acpi_ut_repair_name(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Bob Moore67a119f2008-06-10 13:42:13 +0800714 u32 i;
Bob Moore3d81b232007-02-02 19:48:19 +0300715 char new_name[ACPI_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Bob Moore793c2382006-03-31 00:00:00 -0500717 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore3d81b232007-02-02 19:48:19 +0300718 new_name[i] = name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Bob Moore793c2382006-03-31 00:00:00 -0500720 /*
721 * Replace a bad character with something printable, yet technically
722 * still invalid. This prevents any collisions with existing "good"
723 * names in the namespace.
724 */
Bob Moore3d81b232007-02-02 19:48:19 +0300725 if (!acpi_ut_valid_acpi_char(name[i], i)) {
Bob Moore793c2382006-03-31 00:00:00 -0500726 new_name[i] = '*';
727 }
728 }
729
Bob Moore3d81b232007-02-02 19:48:19 +0300730 return (*(u32 *) new_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*******************************************************************************
734 *
735 * FUNCTION: acpi_ut_strtoul64
736 *
737 * PARAMETERS: String - Null terminated string
Bob Moore41195322006-05-26 16:36:00 -0400738 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
739 * ACPI_ANY_BASE means 'in behalf of to_integer'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * ret_integer - Where the converted integer is returned
741 *
742 * RETURN: Status and Converted value
743 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400744 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
745 * 32-bit or 64-bit conversion, depending on the current mode
746 * of the interpreter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * NOTE: Does not support Octal strings, not needed.
748 *
749 ******************************************************************************/
750
Bob Moore5df7e6c2010-01-21 10:06:32 +0800751acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Len Brown4be44fc2005-08-05 00:44:28 -0400753 u32 this_digit = 0;
Bob Moore5df7e6c2010-01-21 10:06:32 +0800754 u64 return_value = 0;
755 u64 quotient;
756 u64 dividend;
Bob Moore41195322006-05-26 16:36:00 -0400757 u32 to_integer_op = (base == ACPI_ANY_BASE);
758 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
759 u8 valid_digits = 0;
760 u8 sign_of0x = 0;
761 u8 term = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Bob Mooref6dd9222006-07-07 20:44:38 -0400763 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (base) {
766 case ACPI_ANY_BASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case 16:
768 break;
769
770 default:
771 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400772 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Bob Moore41195322006-05-26 16:36:00 -0400775 if (!string) {
776 goto error_exit;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* Skip over any white space in the buffer */
780
Bob Moore41195322006-05-26 16:36:00 -0400781 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 string++;
783 }
784
Bob Moore41195322006-05-26 16:36:00 -0400785 if (to_integer_op) {
786 /*
787 * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
788 * We need to determine if it is decimal or hexadecimal.
789 */
Len Brown4be44fc2005-08-05 00:44:28 -0400790 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Bob Moore41195322006-05-26 16:36:00 -0400791 sign_of0x = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 base = 16;
Bob Moore41195322006-05-26 16:36:00 -0400793
794 /* Skip over the leading '0x' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400796 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 base = 10;
798 }
799 }
800
Bob Moore41195322006-05-26 16:36:00 -0400801 /* Any string left? Check that '0x' is not followed by white space. */
802
803 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
804 if (to_integer_op) {
805 goto error_exit;
806 } else {
807 goto all_done;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Bob Mooref6dd9222006-07-07 20:44:38 -0400811 /*
812 * Perform a 32-bit or 64-bit conversion, depending upon the current
813 * execution mode of the interpreter
814 */
Bob Moore41195322006-05-26 16:36:00 -0400815 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Bob Mooref6dd9222006-07-07 20:44:38 -0400817 /* Main loop: convert the string to a 32- or 64-bit integer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400820 if (ACPI_IS_DIGIT(*string)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Convert ASCII 0-9 to Decimal value */
823
Len Brown4be44fc2005-08-05 00:44:28 -0400824 this_digit = ((u8) * string) - '0';
Bob Moore41195322006-05-26 16:36:00 -0400825 } else if (base == 10) {
826
827 /* Digit is out of range; possible in to_integer case only */
828
829 term = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400830 } else {
Len Brown4be44fc2005-08-05 00:44:28 -0400831 this_digit = (u8) ACPI_TOUPPER(*string);
832 if (ACPI_IS_XDIGIT((char)this_digit)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* Convert ASCII Hex char to value */
835
836 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400837 } else {
Bob Moore41195322006-05-26 16:36:00 -0400838 term = 1;
839 }
840 }
841
842 if (term) {
843 if (to_integer_op) {
844 goto error_exit;
845 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
Bob Moore41195322006-05-26 16:36:00 -0400848 } else if ((valid_digits == 0) && (this_digit == 0)
849 && !sign_of0x) {
850
851 /* Skip zeros */
852 string++;
853 continue;
854 }
855
856 valid_digits++;
857
Len Brownfd350942007-05-09 23:34:35 -0400858 if (sign_of0x && ((valid_digits > 16)
859 || ((valid_digits > 8) && mode32))) {
Bob Moore41195322006-05-26 16:36:00 -0400860 /*
861 * This is to_integer operation case.
862 * No any restrictions for string-to-integer conversion,
863 * see ACPI spec.
864 */
865 goto error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
868 /* Divide the digit into the correct position */
869
Bob Moore5df7e6c2010-01-21 10:06:32 +0800870 (void)acpi_ut_short_divide((dividend - (u64) this_digit),
871 base, &quotient, NULL);
Bob Moore41195322006-05-26 16:36:00 -0400872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (return_value > quotient) {
Bob Moore41195322006-05-26 16:36:00 -0400874 if (to_integer_op) {
875 goto error_exit;
876 } else {
877 break;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 return_value *= base;
882 return_value += this_digit;
883 string++;
884 }
885
886 /* All done, normal exit */
887
Bob Moore41195322006-05-26 16:36:00 -0400888 all_done:
889
Bob Mooref6dd9222006-07-07 20:44:38 -0400890 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
891 ACPI_FORMAT_UINT64(return_value)));
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400894 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Len Brown4be44fc2005-08-05 00:44:28 -0400896 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* Base was set/validated above */
898
899 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400900 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
901 } else {
902 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/*******************************************************************************
907 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * FUNCTION: acpi_ut_create_update_state_and_push
909 *
Robert Moore44f6c012005-04-18 22:49:35 -0400910 * PARAMETERS: Object - Object to be added to the new state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Action - Increment/Decrement
912 * state_list - List the state will be added to
913 *
Robert Moore44f6c012005-04-18 22:49:35 -0400914 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
916 * DESCRIPTION: Create a new state and push it
917 *
918 ******************************************************************************/
919
920acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400921acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
922 u16 action,
923 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Len Brown4be44fc2005-08-05 00:44:28 -0400925 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Len Brown4be44fc2005-08-05 00:44:28 -0400927 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* Ignore null objects; these are expected */
930
931 if (!object) {
932 return (AE_OK);
933 }
934
Len Brown4be44fc2005-08-05 00:44:28 -0400935 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!state) {
937 return (AE_NO_MEMORY);
938 }
939
Len Brown4be44fc2005-08-05 00:44:28 -0400940 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return (AE_OK);
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*******************************************************************************
945 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * FUNCTION: acpi_ut_walk_package_tree
947 *
Robert Moore44f6c012005-04-18 22:49:35 -0400948 * PARAMETERS: source_object - The package to walk
949 * target_object - Target object (if package is being copied)
950 * walk_callback - Called once for each package element
951 * Context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
953 * RETURN: Status
954 *
955 * DESCRIPTION: Walk through a package
956 *
957 ******************************************************************************/
958
959acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400960acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
961 void *target_object,
962 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Len Brown4be44fc2005-08-05 00:44:28 -0400964 acpi_status status = AE_OK;
965 union acpi_generic_state *state_list = NULL;
966 union acpi_generic_state *state;
967 u32 this_index;
968 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Bob Mooreb229cf92006-04-21 17:15:00 -0400970 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Len Brown4be44fc2005-08-05 00:44:28 -0400972 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400974 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 while (state) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Get one element of the package */
980
Len Brown4be44fc2005-08-05 00:44:28 -0400981 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400983 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /*
986 * Check for:
987 * 1) An uninitialized package element. It is completely
988 * legal to declare a package and leave it uninitialized
989 * 2) Not an internal object - can be a namespace node instead
990 * 3) Any type other than a package. Packages are handled in else
991 * case below.
992 */
993 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400994 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
995 ACPI_DESC_TYPE_OPERAND)
Bob Moore3371c192009-02-18 14:44:03 +0800996 || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400997 status =
998 walk_callback(ACPI_COPY_TYPE_SIMPLE,
999 this_source_obj, state, context);
1000 if (ACPI_FAILURE(status)) {
1001 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
1004 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -04001005 while (state->pkg.index >=
1006 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * We've handled all of the objects at this level, This means
1009 * that we have just completed a package. That package may
1010 * have contained one or more packages itself.
1011 *
1012 * Delete this state and pop the previous state (package).
1013 */
Len Brown4be44fc2005-08-05 00:44:28 -04001014 acpi_ut_delete_generic_state(state);
1015 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Finished when there are no more states */
1018
1019 if (!state) {
1020 /*
1021 * We have handled all of the objects in the top level
1022 * package just add the length of the package objects
1023 * and exit
1024 */
Len Brown4be44fc2005-08-05 00:44:28 -04001025 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
1028 /*
1029 * Go back up a level and move the index past the just
1030 * completed package object.
1031 */
1032 state->pkg.index++;
1033 }
Len Brown4be44fc2005-08-05 00:44:28 -04001034 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* This is a subobject of type package */
1036
Len Brown4be44fc2005-08-05 00:44:28 -04001037 status =
1038 walk_callback(ACPI_COPY_TYPE_PACKAGE,
1039 this_source_obj, state, context);
1040 if (ACPI_FAILURE(status)) {
1041 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 /*
1045 * Push the current state and create a new one
1046 * The callback above returned a new target package object.
1047 */
Len Brown4be44fc2005-08-05 00:44:28 -04001048 acpi_ut_push_generic_state(&state_list, state);
1049 state = acpi_ut_create_pkg_state(this_source_obj,
1050 state->pkg.
1051 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!state) {
Lin Mingcf058bd2008-09-27 11:29:57 +08001053
1054 /* Free any stacked Update State objects */
1055
1056 while (state_list) {
1057 state =
1058 acpi_ut_pop_generic_state
1059 (&state_list);
1060 acpi_ut_delete_generic_state(state);
1061 }
Len Brown4be44fc2005-08-05 00:44:28 -04001062 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 }
1065 }
1066
1067 /* We should never get here */
1068
Len Brown4be44fc2005-08-05 00:44:28 -04001069 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}