blob: 33c6cf7ff4675b896df5c44920eeab2ddd8f7f41 [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 *
Bob Mooreba494be2012-07-12 09:40:10 +080058 * PARAMETERS: pathname - File pathname string to be converted
Bob Moorec6e17332012-05-22 16:26:53 +080059 *
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 Moore15b8dd52009-06-29 13:39:29 +080085 * FUNCTION: acpi_ut_is_pci_root_bridge
86 *
Bob Mooreba494be2012-07-12 09:40:10 +080087 * PARAMETERS: id - The HID/CID in string format
Bob Moore15b8dd52009-06-29 13:39:29 +080088 *
89 * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
90 *
91 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
92 *
93 ******************************************************************************/
94
95u8 acpi_ut_is_pci_root_bridge(char *id)
96{
97
98 /*
99 * Check if this is a PCI root bridge.
100 * ACPI 3.0+: check for a PCI Express root also.
101 */
102 if (!(ACPI_STRCMP(id,
103 PCI_ROOT_HID_STRING)) ||
104 !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
105 return (TRUE);
106 }
107
108 return (FALSE);
109}
110
111/*******************************************************************************
112 *
Bob Moore793c2382006-03-31 00:00:00 -0500113 * FUNCTION: acpi_ut_is_aml_table
114 *
Bob Mooreba494be2012-07-12 09:40:10 +0800115 * PARAMETERS: table - An ACPI table
Bob Moore793c2382006-03-31 00:00:00 -0500116 *
117 * RETURN: TRUE if table contains executable AML; FALSE otherwise
118 *
119 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
120 * Currently, these are DSDT,SSDT,PSDT. All other table types are
121 * data tables that do not contain AML code.
122 *
123 ******************************************************************************/
Bob Moore84fb2c92007-02-02 19:48:19 +0300124
Bob Moore793c2382006-03-31 00:00:00 -0500125u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
126{
127
Bob Mooref6dd9222006-07-07 20:44:38 -0400128 /* These are the only tables that contain executable AML */
Bob Moore793c2382006-03-31 00:00:00 -0500129
Bob Mooref3d2e782007-02-02 19:48:18 +0300130 if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
131 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
132 ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
Bob Moore793c2382006-03-31 00:00:00 -0500133 return (TRUE);
134 }
135
136 return (FALSE);
137}
138
139/*******************************************************************************
140 *
Robert Mooref9f46012005-07-08 00:00:00 -0400141 * FUNCTION: acpi_ut_allocate_owner_id
142 *
143 * PARAMETERS: owner_id - Where the new owner ID is returned
144 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700145 * RETURN: Status
146 *
147 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
148 * track objects created by the table or method, to be deleted
149 * when the method exits or the table is unloaded.
Robert Mooref9f46012005-07-08 00:00:00 -0400150 *
151 ******************************************************************************/
Bob Moore793c2382006-03-31 00:00:00 -0500152
Len Brown4be44fc2005-08-05 00:44:28 -0400153acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
Robert Mooref9f46012005-07-08 00:00:00 -0400154{
Bob Moore67a119f2008-06-10 13:42:13 +0800155 u32 i;
156 u32 j;
157 u32 k;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 acpi_status status;
Robert Mooref9f46012005-07-08 00:00:00 -0400159
Bob Mooreb229cf92006-04-21 17:15:00 -0400160 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400161
Robert Mooreaff8c272005-09-02 17:24:17 -0400162 /* Guard against multiple allocations of ID to the same location */
163
164 if (*owner_id) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800165 ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500166 *owner_id));
Robert Mooreaff8c272005-09-02 17:24:17 -0400167 return_ACPI_STATUS(AE_ALREADY_EXISTS);
168 }
169
Robert Moore0c9938c2005-07-29 15:15:00 -0700170 /* Mutex for the global ID mask */
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
173 if (ACPI_FAILURE(status)) {
174 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400175 }
176
Bob Moorec51a4de2005-11-17 13:07:00 -0500177 /*
178 * Find a free owner ID, cycle through all possible IDs on repeated
Bob Moore28f55eb2005-12-02 18:27:00 -0500179 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
180 * to be scanned twice.
Bob Moorec51a4de2005-11-17 13:07:00 -0500181 */
Bob Moore28f55eb2005-12-02 18:27:00 -0500182 for (i = 0, j = acpi_gbl_last_owner_id_index;
183 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
184 if (j >= ACPI_NUM_OWNERID_MASKS) {
185 j = 0; /* Wraparound to start of mask array */
Bob Moorec51a4de2005-11-17 13:07:00 -0500186 }
Robert Mooref9f46012005-07-08 00:00:00 -0400187
Bob Moore28f55eb2005-12-02 18:27:00 -0500188 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
189 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400190
Bob Moore28f55eb2005-12-02 18:27:00 -0500191 /* There are no free IDs in this mask */
Bob Moorec51a4de2005-11-17 13:07:00 -0500192
Bob Moore28f55eb2005-12-02 18:27:00 -0500193 break;
194 }
Bob Moorea18ecf42005-08-15 03:42:00 -0800195
Bob Moore28f55eb2005-12-02 18:27:00 -0500196 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
197 /*
198 * Found a free ID. The actual ID is the bit index plus one,
199 * making zero an invalid Owner ID. Save this as the last ID
200 * allocated and update the global ID mask.
201 */
202 acpi_gbl_owner_id_mask[j] |= (1 << k);
203
204 acpi_gbl_last_owner_id_index = (u8) j;
205 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
206
207 /*
208 * Construct encoded ID from the index and bit position
209 *
210 * Note: Last [j].k (bit 255) is never used and is marked
211 * permanently allocated (prevents +1 overflow)
212 */
213 *owner_id =
214 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
215
216 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400217 "Allocated OwnerId: %2.2X\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500218 (unsigned int)*owner_id));
219 goto exit;
220 }
Robert Mooref9f46012005-07-08 00:00:00 -0400221 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500222
223 acpi_gbl_next_owner_id_offset = 0;
Robert Mooref9f46012005-07-08 00:00:00 -0400224 }
225
226 /*
Bob Moorec51a4de2005-11-17 13:07:00 -0500227 * All owner_ids have been allocated. This typically should
Robert Mooref9f46012005-07-08 00:00:00 -0400228 * not happen since the IDs are reused after deallocation. The IDs are
229 * allocated upon table load (one per table) and method execution, and
230 * they are released when a table is unloaded or a method completes
231 * execution.
Bob Moorec51a4de2005-11-17 13:07:00 -0500232 *
233 * If this error happens, there may be very deep nesting of invoked control
234 * methods, or there may be a bug where the IDs are not released.
Robert Mooref9f46012005-07-08 00:00:00 -0400235 */
236 status = AE_OWNER_ID_LIMIT;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500237 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400238 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
Robert Mooref9f46012005-07-08 00:00:00 -0400239
Len Brown4be44fc2005-08-05 00:44:28 -0400240 exit:
241 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
242 return_ACPI_STATUS(status);
Robert Mooref9f46012005-07-08 00:00:00 -0400243}
244
Robert Mooref9f46012005-07-08 00:00:00 -0400245/*******************************************************************************
246 *
247 * FUNCTION: acpi_ut_release_owner_id
248 *
Bob Mooreba494be2012-07-12 09:40:10 +0800249 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
Robert Mooref9f46012005-07-08 00:00:00 -0400250 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700251 * RETURN: None. No error is returned because we are either exiting a
252 * control method or unloading a table. Either way, we would
253 * ignore any error anyway.
254 *
Bob Moore28f55eb2005-12-02 18:27:00 -0500255 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
Robert Mooref9f46012005-07-08 00:00:00 -0400256 *
257 ******************************************************************************/
258
Len Brown4be44fc2005-08-05 00:44:28 -0400259void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
Robert Mooref9f46012005-07-08 00:00:00 -0400260{
Len Brown4be44fc2005-08-05 00:44:28 -0400261 acpi_owner_id owner_id = *owner_id_ptr;
262 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +0800263 u32 index;
Bob Moore28f55eb2005-12-02 18:27:00 -0500264 u32 bit;
Robert Mooref9f46012005-07-08 00:00:00 -0400265
Bob Mooreb229cf92006-04-21 17:15:00 -0400266 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400267
Robert Moore0c9938c2005-07-29 15:15:00 -0700268 /* Always clear the input owner_id (zero is an invalid ID) */
269
270 *owner_id_ptr = 0;
271
Bob Mooreba494be2012-07-12 09:40:10 +0800272 /* Zero is not a valid owner_ID */
Robert Moore0c9938c2005-07-29 15:15:00 -0700273
Bob Mooredefba1d2005-12-16 17:05:00 -0500274 if (owner_id == 0) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800275 ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
Robert Moore0c9938c2005-07-29 15:15:00 -0700276 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400277 }
278
Robert Moore0c9938c2005-07-29 15:15:00 -0700279 /* Mutex for the global ID mask */
280
Len Brown4be44fc2005-08-05 00:44:28 -0400281 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
282 if (ACPI_FAILURE(status)) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700283 return_VOID;
284 }
285
Robert Mooreaff8c272005-09-02 17:24:17 -0400286 /* Normalize the ID to zero */
287
288 owner_id--;
Robert Moore0c9938c2005-07-29 15:15:00 -0700289
Bob Moore28f55eb2005-12-02 18:27:00 -0500290 /* Decode ID to index/offset pair */
291
292 index = ACPI_DIV_32(owner_id);
293 bit = 1 << ACPI_MOD_32(owner_id);
294
Robert Moore0c9938c2005-07-29 15:15:00 -0700295 /* Free the owner ID only if it is valid */
Robert Mooref9f46012005-07-08 00:00:00 -0400296
Bob Moore28f55eb2005-12-02 18:27:00 -0500297 if (acpi_gbl_owner_id_mask[index] & bit) {
298 acpi_gbl_owner_id_mask[index] ^= bit;
299 } else {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500300 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800301 "Release of non-allocated OwnerId: 0x%2.2X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500302 owner_id + 1));
Robert Mooref9f46012005-07-08 00:00:00 -0400303 }
Robert Mooref9f46012005-07-08 00:00:00 -0400304
Len Brown4be44fc2005-08-05 00:44:28 -0400305 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
Robert Moore0c9938c2005-07-29 15:15:00 -0700306 return_VOID;
Robert Mooref9f46012005-07-08 00:00:00 -0400307}
308
Robert Mooref9f46012005-07-08 00:00:00 -0400309/*******************************************************************************
310 *
Robert Moore44f6c012005-04-18 22:49:35 -0400311 * FUNCTION: acpi_ut_strupr (strupr)
312 *
313 * PARAMETERS: src_string - The source string to convert
314 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700315 * RETURN: None
Robert Moore44f6c012005-04-18 22:49:35 -0400316 *
317 * DESCRIPTION: Convert string to uppercase
318 *
319 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
320 *
321 ******************************************************************************/
322
Len Brown4be44fc2005-08-05 00:44:28 -0400323void acpi_ut_strupr(char *src_string)
Robert Moore44f6c012005-04-18 22:49:35 -0400324{
Len Brown4be44fc2005-08-05 00:44:28 -0400325 char *string;
Robert Moore44f6c012005-04-18 22:49:35 -0400326
Len Brown4be44fc2005-08-05 00:44:28 -0400327 ACPI_FUNCTION_ENTRY();
Robert Moore44f6c012005-04-18 22:49:35 -0400328
Robert Moore73459f72005-06-24 00:00:00 -0400329 if (!src_string) {
Robert Moore0c9938c2005-07-29 15:15:00 -0700330 return;
Robert Moore73459f72005-06-24 00:00:00 -0400331 }
332
Robert Moore44f6c012005-04-18 22:49:35 -0400333 /* Walk entire string, uppercasing the letters */
334
335 for (string = src_string; *string; string++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400336 *string = (char)ACPI_TOUPPER(*string);
Robert Moore44f6c012005-04-18 22:49:35 -0400337 }
338
Robert Moore0c9938c2005-07-29 15:15:00 -0700339 return;
Robert Moore44f6c012005-04-18 22:49:35 -0400340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*******************************************************************************
343 *
344 * FUNCTION: acpi_ut_print_string
345 *
Bob Mooreba494be2012-07-12 09:40:10 +0800346 * PARAMETERS: string - Null terminated ASCII string
Robert Moore44f6c012005-04-18 22:49:35 -0400347 * max_length - Maximum output length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
349 * RETURN: None
350 *
351 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
352 * sequences.
353 *
354 ******************************************************************************/
355
Len Brown4be44fc2005-08-05 00:44:28 -0400356void acpi_ut_print_string(char *string, u8 max_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Len Brown4be44fc2005-08-05 00:44:28 -0400358 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400361 acpi_os_printf("<\"NULL STRING PTR\">");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
363 }
364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 for (i = 0; string[i] && (i < max_length); i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Escape sequences */
369
370 switch (string[i]) {
371 case 0x07:
Len Brown4be44fc2005-08-05 00:44:28 -0400372 acpi_os_printf("\\a"); /* BELL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374
375 case 0x08:
Len Brown4be44fc2005-08-05 00:44:28 -0400376 acpi_os_printf("\\b"); /* BACKSPACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378
379 case 0x0C:
Len Brown4be44fc2005-08-05 00:44:28 -0400380 acpi_os_printf("\\f"); /* FORMFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382
383 case 0x0A:
Len Brown4be44fc2005-08-05 00:44:28 -0400384 acpi_os_printf("\\n"); /* LINEFEED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386
387 case 0x0D:
Len Brown4be44fc2005-08-05 00:44:28 -0400388 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390
391 case 0x09:
Len Brown4be44fc2005-08-05 00:44:28 -0400392 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394
395 case 0x0B:
Len Brown4be44fc2005-08-05 00:44:28 -0400396 acpi_os_printf("\\v"); /* VERTICAL TAB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398
Len Brown4be44fc2005-08-05 00:44:28 -0400399 case '\'': /* Single Quote */
400 case '\"': /* Double Quote */
401 case '\\': /* Backslash */
402 acpi_os_printf("\\%c", (int)string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404
405 default:
406
407 /* Check for printable character or hex escape */
408
Len Brown4be44fc2005-08-05 00:44:28 -0400409 if (ACPI_IS_PRINT(string[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* This is a normal character */
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412 acpi_os_printf("%c", (int)string[i]);
413 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* All others will be Hex escapes */
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 acpi_os_printf("\\x%2.2X", (s32) string[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 break;
419 }
420 }
Len Brown4be44fc2005-08-05 00:44:28 -0400421 acpi_os_printf("\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (i == max_length && string[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -0400424 acpi_os_printf("...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*******************************************************************************
429 *
430 * FUNCTION: acpi_ut_dword_byte_swap
431 *
Bob Mooreba494be2012-07-12 09:40:10 +0800432 * PARAMETERS: value - Value to be converted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *
Robert Moore44f6c012005-04-18 22:49:35 -0400434 * RETURN: u32 integer with bytes swapped
435 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
437 *
438 ******************************************************************************/
439
Len Brown4be44fc2005-08-05 00:44:28 -0400440u32 acpi_ut_dword_byte_swap(u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400443 u32 value;
444 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 union {
Len Brown4be44fc2005-08-05 00:44:28 -0400447 u32 value;
448 u8 bytes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 } in;
450
Len Brown4be44fc2005-08-05 00:44:28 -0400451 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 in.value = value;
454
455 out.bytes[0] = in.bytes[3];
456 out.bytes[1] = in.bytes[2];
457 out.bytes[2] = in.bytes[1];
458 out.bytes[3] = in.bytes[0];
459
460 return (out.value);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*******************************************************************************
464 *
465 * FUNCTION: acpi_ut_set_integer_width
466 *
467 * PARAMETERS: Revision From DSDT header
468 *
469 * RETURN: None
470 *
471 * DESCRIPTION: Set the global integer bit width based upon the revision
472 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
473 * For Revision 2 and above, Integers are 64 bits. Yes, this
474 * makes a difference.
475 *
476 ******************************************************************************/
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478void acpi_ut_set_integer_width(u8 revision)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480
Bob Mooref3d2e782007-02-02 19:48:18 +0300481 if (revision < 2) {
Bob Mooref6dd9222006-07-07 20:44:38 -0400482
483 /* 32-bit case */
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 acpi_gbl_integer_bit_width = 32;
486 acpi_gbl_integer_nybble_width = 8;
487 acpi_gbl_integer_byte_width = 4;
Len Brown4be44fc2005-08-05 00:44:28 -0400488 } else {
Bob Mooref6dd9222006-07-07 20:44:38 -0400489 /* 64-bit case (ACPI 2.0+) */
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 acpi_gbl_integer_bit_width = 64;
492 acpi_gbl_integer_nybble_width = 16;
493 acpi_gbl_integer_byte_width = 8;
494 }
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#ifdef ACPI_DEBUG_OUTPUT
498/*******************************************************************************
499 *
500 * FUNCTION: acpi_ut_display_init_pathname
501 *
Bob Mooreba494be2012-07-12 09:40:10 +0800502 * PARAMETERS: type - Object type of the node
Robert Moore44f6c012005-04-18 22:49:35 -0400503 * obj_handle - Handle whose pathname will be displayed
Bob Mooreba494be2012-07-12 09:40:10 +0800504 * path - Additional path string to be appended.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * (NULL if no extra path)
506 *
507 * RETURN: acpi_status
508 *
509 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
510 *
511 ******************************************************************************/
512
513void
Len Brown4be44fc2005-08-05 00:44:28 -0400514acpi_ut_display_init_pathname(u8 type,
515 struct acpi_namespace_node *obj_handle,
516 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Len Brown4be44fc2005-08-05 00:44:28 -0400518 acpi_status status;
519 struct acpi_buffer buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Len Brown4be44fc2005-08-05 00:44:28 -0400521 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Only print the path if the appropriate debug level is enabled */
524
525 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
526 return;
527 }
528
529 /* Get the full pathname to the node */
530
531 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
Len Brown4be44fc2005-08-05 00:44:28 -0400532 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
533 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return;
535 }
536
537 /* Print what we're doing */
538
539 switch (type) {
540 case ACPI_TYPE_METHOD:
Len Brown4be44fc2005-08-05 00:44:28 -0400541 acpi_os_printf("Executing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400545 acpi_os_printf("Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547 }
548
549 /* Print the object type and pathname */
550
Len Brown4be44fc2005-08-05 00:44:28 -0400551 acpi_os_printf("%-12s %s",
552 acpi_ut_get_type_name(type), (char *)buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Extra path is used to append names like _STA, _INI, etc. */
555
556 if (path) {
Len Brown4be44fc2005-08-05 00:44:28 -0400557 acpi_os_printf(".%s", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Len Brown4be44fc2005-08-05 00:44:28 -0400559 acpi_os_printf("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Bob Moore83135242006-10-03 00:00:00 -0400561 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563#endif
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*******************************************************************************
566 *
Bob Moore793c2382006-03-31 00:00:00 -0500567 * FUNCTION: acpi_ut_valid_acpi_char
568 *
Bob Mooreba494be2012-07-12 09:40:10 +0800569 * PARAMETERS: char - The character to be examined
570 * position - Byte position (0-3)
Bob Moore793c2382006-03-31 00:00:00 -0500571 *
572 * RETURN: TRUE if the character is valid, FALSE otherwise
573 *
574 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
575 * 1) Upper case alpha
576 * 2) numeric
577 * 3) underscore
578 *
579 * We allow a '!' as the last character because of the ASF! table
580 *
581 ******************************************************************************/
582
Bob Moore67a119f2008-06-10 13:42:13 +0800583u8 acpi_ut_valid_acpi_char(char character, u32 position)
Bob Moore793c2382006-03-31 00:00:00 -0500584{
585
586 if (!((character >= 'A' && character <= 'Z') ||
587 (character >= '0' && character <= '9') || (character == '_'))) {
588
589 /* Allow a '!' in the last position */
590
591 if (character == '!' && position == 3) {
592 return (TRUE);
593 }
594
595 return (FALSE);
596 }
597
598 return (TRUE);
599}
600
601/*******************************************************************************
602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * FUNCTION: acpi_ut_valid_acpi_name
604 *
Bob Mooreba494be2012-07-12 09:40:10 +0800605 * PARAMETERS: name - The name to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *
Robert Moore44f6c012005-04-18 22:49:35 -0400607 * RETURN: TRUE if the name is valid, FALSE otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 *
609 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
610 * 1) Upper case alpha
611 * 2) numeric
612 * 3) underscore
613 *
614 ******************************************************************************/
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616u8 acpi_ut_valid_acpi_name(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Bob Moore67a119f2008-06-10 13:42:13 +0800618 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Len Brown4be44fc2005-08-05 00:44:28 -0400620 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore793c2382006-03-31 00:00:00 -0500623 if (!acpi_ut_valid_acpi_char
624 ((ACPI_CAST_PTR(char, &name))[i], i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return (FALSE);
626 }
627 }
628
629 return (TRUE);
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*******************************************************************************
633 *
Bob Moore793c2382006-03-31 00:00:00 -0500634 * FUNCTION: acpi_ut_repair_name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
Bob Mooreba494be2012-07-12 09:40:10 +0800636 * PARAMETERS: name - The ACPI name to be repaired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 *
Bob Moore793c2382006-03-31 00:00:00 -0500638 * RETURN: Repaired version of the name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *
Bob Moore793c2382006-03-31 00:00:00 -0500640 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
641 * return the new name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 ******************************************************************************/
644
Bob Moore3d81b232007-02-02 19:48:19 +0300645acpi_name acpi_ut_repair_name(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Bob Moore67a119f2008-06-10 13:42:13 +0800647 u32 i;
Bob Moore3d81b232007-02-02 19:48:19 +0300648 char new_name[ACPI_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Bob Moore793c2382006-03-31 00:00:00 -0500650 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore3d81b232007-02-02 19:48:19 +0300651 new_name[i] = name[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Bob Moore793c2382006-03-31 00:00:00 -0500653 /*
654 * Replace a bad character with something printable, yet technically
655 * still invalid. This prevents any collisions with existing "good"
656 * names in the namespace.
657 */
Bob Moore3d81b232007-02-02 19:48:19 +0300658 if (!acpi_ut_valid_acpi_char(name[i], i)) {
Bob Moore793c2382006-03-31 00:00:00 -0500659 new_name[i] = '*';
660 }
661 }
662
Bob Moore3d81b232007-02-02 19:48:19 +0300663 return (*(u32 *) new_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*******************************************************************************
667 *
668 * FUNCTION: acpi_ut_strtoul64
669 *
Bob Mooreba494be2012-07-12 09:40:10 +0800670 * PARAMETERS: string - Null terminated string
671 * base - Radix of the string: 16 or ACPI_ANY_BASE;
Bob Moore41195322006-05-26 16:36:00 -0400672 * ACPI_ANY_BASE means 'in behalf of to_integer'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * ret_integer - Where the converted integer is returned
674 *
675 * RETURN: Status and Converted value
676 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400677 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
678 * 32-bit or 64-bit conversion, depending on the current mode
679 * of the interpreter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * NOTE: Does not support Octal strings, not needed.
681 *
682 ******************************************************************************/
683
Bob Moore5df7e6c2010-01-21 10:06:32 +0800684acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Len Brown4be44fc2005-08-05 00:44:28 -0400686 u32 this_digit = 0;
Bob Moore5df7e6c2010-01-21 10:06:32 +0800687 u64 return_value = 0;
688 u64 quotient;
689 u64 dividend;
Bob Moore41195322006-05-26 16:36:00 -0400690 u32 to_integer_op = (base == ACPI_ANY_BASE);
691 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
692 u8 valid_digits = 0;
693 u8 sign_of0x = 0;
694 u8 term = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Bob Mooref6dd9222006-07-07 20:44:38 -0400696 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 switch (base) {
699 case ACPI_ANY_BASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 case 16:
701 break;
702
703 default:
704 /* Invalid Base */
Len Brown4be44fc2005-08-05 00:44:28 -0400705 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Bob Moore41195322006-05-26 16:36:00 -0400708 if (!string) {
709 goto error_exit;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Skip over any white space in the buffer */
713
Bob Moore41195322006-05-26 16:36:00 -0400714 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 string++;
716 }
717
Bob Moore41195322006-05-26 16:36:00 -0400718 if (to_integer_op) {
719 /*
Bob Mooreba494be2012-07-12 09:40:10 +0800720 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
Bob Moore41195322006-05-26 16:36:00 -0400721 * We need to determine if it is decimal or hexadecimal.
722 */
Len Brown4be44fc2005-08-05 00:44:28 -0400723 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
Bob Moore41195322006-05-26 16:36:00 -0400724 sign_of0x = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 base = 16;
Bob Moore41195322006-05-26 16:36:00 -0400726
727 /* Skip over the leading '0x' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 string += 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400729 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 base = 10;
731 }
732 }
733
Bob Moore41195322006-05-26 16:36:00 -0400734 /* Any string left? Check that '0x' is not followed by white space. */
735
736 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
737 if (to_integer_op) {
738 goto error_exit;
739 } else {
740 goto all_done;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Bob Mooref6dd9222006-07-07 20:44:38 -0400744 /*
745 * Perform a 32-bit or 64-bit conversion, depending upon the current
746 * execution mode of the interpreter
747 */
Bob Moore41195322006-05-26 16:36:00 -0400748 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Bob Mooref6dd9222006-07-07 20:44:38 -0400750 /* Main loop: convert the string to a 32- or 64-bit integer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 while (*string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400753 if (ACPI_IS_DIGIT(*string)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Convert ASCII 0-9 to Decimal value */
756
Len Brown4be44fc2005-08-05 00:44:28 -0400757 this_digit = ((u8) * string) - '0';
Bob Moore41195322006-05-26 16:36:00 -0400758 } else if (base == 10) {
759
760 /* Digit is out of range; possible in to_integer case only */
761
762 term = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400763 } else {
Len Brown4be44fc2005-08-05 00:44:28 -0400764 this_digit = (u8) ACPI_TOUPPER(*string);
765 if (ACPI_IS_XDIGIT((char)this_digit)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Convert ASCII Hex char to value */
768
769 this_digit = this_digit - 'A' + 10;
Len Brown4be44fc2005-08-05 00:44:28 -0400770 } else {
Bob Moore41195322006-05-26 16:36:00 -0400771 term = 1;
772 }
773 }
774
775 if (term) {
776 if (to_integer_op) {
777 goto error_exit;
778 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 }
Bob Moore41195322006-05-26 16:36:00 -0400781 } else if ((valid_digits == 0) && (this_digit == 0)
782 && !sign_of0x) {
783
784 /* Skip zeros */
785 string++;
786 continue;
787 }
788
789 valid_digits++;
790
Len Brownfd350942007-05-09 23:34:35 -0400791 if (sign_of0x && ((valid_digits > 16)
792 || ((valid_digits > 8) && mode32))) {
Bob Moore41195322006-05-26 16:36:00 -0400793 /*
794 * This is to_integer operation case.
795 * No any restrictions for string-to-integer conversion,
796 * see ACPI spec.
797 */
798 goto error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801 /* Divide the digit into the correct position */
802
Bob Moore5df7e6c2010-01-21 10:06:32 +0800803 (void)acpi_ut_short_divide((dividend - (u64) this_digit),
804 base, &quotient, NULL);
Bob Moore41195322006-05-26 16:36:00 -0400805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (return_value > quotient) {
Bob Moore41195322006-05-26 16:36:00 -0400807 if (to_integer_op) {
808 goto error_exit;
809 } else {
810 break;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 return_value *= base;
815 return_value += this_digit;
816 string++;
817 }
818
819 /* All done, normal exit */
820
Bob Moore41195322006-05-26 16:36:00 -0400821 all_done:
822
Bob Mooref6dd9222006-07-07 20:44:38 -0400823 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
824 ACPI_FORMAT_UINT64(return_value)));
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *ret_integer = return_value;
Len Brown4be44fc2005-08-05 00:44:28 -0400827 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Len Brown4be44fc2005-08-05 00:44:28 -0400829 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* Base was set/validated above */
831
832 if (base == 10) {
Len Brown4be44fc2005-08-05 00:44:28 -0400833 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
834 } else {
835 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839/*******************************************************************************
840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * FUNCTION: acpi_ut_create_update_state_and_push
842 *
Bob Mooreba494be2012-07-12 09:40:10 +0800843 * PARAMETERS: object - Object to be added to the new state
844 * action - Increment/Decrement
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * state_list - List the state will be added to
846 *
Robert Moore44f6c012005-04-18 22:49:35 -0400847 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 *
849 * DESCRIPTION: Create a new state and push it
850 *
851 ******************************************************************************/
852
853acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400854acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
855 u16 action,
856 union acpi_generic_state **state_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Len Brown4be44fc2005-08-05 00:44:28 -0400858 union acpi_generic_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Len Brown4be44fc2005-08-05 00:44:28 -0400860 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* Ignore null objects; these are expected */
863
864 if (!object) {
865 return (AE_OK);
866 }
867
Len Brown4be44fc2005-08-05 00:44:28 -0400868 state = acpi_ut_create_update_state(object, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!state) {
870 return (AE_NO_MEMORY);
871 }
872
Len Brown4be44fc2005-08-05 00:44:28 -0400873 acpi_ut_push_generic_state(state_list, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return (AE_OK);
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*******************************************************************************
878 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * FUNCTION: acpi_ut_walk_package_tree
880 *
Robert Moore44f6c012005-04-18 22:49:35 -0400881 * PARAMETERS: source_object - The package to walk
882 * target_object - Target object (if package is being copied)
883 * walk_callback - Called once for each package element
Bob Mooreba494be2012-07-12 09:40:10 +0800884 * context - Passed to the callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *
886 * RETURN: Status
887 *
888 * DESCRIPTION: Walk through a package
889 *
890 ******************************************************************************/
891
892acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400893acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
894 void *target_object,
895 acpi_pkg_callback walk_callback, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Len Brown4be44fc2005-08-05 00:44:28 -0400897 acpi_status status = AE_OK;
898 union acpi_generic_state *state_list = NULL;
899 union acpi_generic_state *state;
900 u32 this_index;
901 union acpi_operand_object *this_source_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Bob Mooreb229cf92006-04-21 17:15:00 -0400903 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Len Brown4be44fc2005-08-05 00:44:28 -0400905 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400907 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
910 while (state) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /* Get one element of the package */
913
Len Brown4be44fc2005-08-05 00:44:28 -0400914 this_index = state->pkg.index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 this_source_obj = (union acpi_operand_object *)
Len Brown4be44fc2005-08-05 00:44:28 -0400916 state->pkg.source_object->package.elements[this_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
919 * Check for:
920 * 1) An uninitialized package element. It is completely
921 * legal to declare a package and leave it uninitialized
922 * 2) Not an internal object - can be a namespace node instead
923 * 3) Any type other than a package. Packages are handled in else
924 * case below.
925 */
926 if ((!this_source_obj) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400927 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
928 ACPI_DESC_TYPE_OPERAND)
Bob Moore3371c192009-02-18 14:44:03 +0800929 || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400930 status =
931 walk_callback(ACPI_COPY_TYPE_SIMPLE,
932 this_source_obj, state, context);
933 if (ACPI_FAILURE(status)) {
934 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 state->pkg.index++;
Len Brown4be44fc2005-08-05 00:44:28 -0400938 while (state->pkg.index >=
939 state->pkg.source_object->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * We've handled all of the objects at this level, This means
942 * that we have just completed a package. That package may
943 * have contained one or more packages itself.
944 *
945 * Delete this state and pop the previous state (package).
946 */
Len Brown4be44fc2005-08-05 00:44:28 -0400947 acpi_ut_delete_generic_state(state);
948 state = acpi_ut_pop_generic_state(&state_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* Finished when there are no more states */
951
952 if (!state) {
953 /*
954 * We have handled all of the objects in the top level
955 * package just add the length of the package objects
956 * and exit
957 */
Len Brown4be44fc2005-08-05 00:44:28 -0400958 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
961 /*
962 * Go back up a level and move the index past the just
963 * completed package object.
964 */
965 state->pkg.index++;
966 }
Len Brown4be44fc2005-08-05 00:44:28 -0400967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* This is a subobject of type package */
969
Len Brown4be44fc2005-08-05 00:44:28 -0400970 status =
971 walk_callback(ACPI_COPY_TYPE_PACKAGE,
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 /*
978 * Push the current state and create a new one
979 * The callback above returned a new target package object.
980 */
Len Brown4be44fc2005-08-05 00:44:28 -0400981 acpi_ut_push_generic_state(&state_list, state);
982 state = acpi_ut_create_pkg_state(this_source_obj,
983 state->pkg.
984 this_target_obj, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (!state) {
Lin Mingcf058bd2008-09-27 11:29:57 +0800986
987 /* Free any stacked Update State objects */
988
989 while (state_list) {
990 state =
991 acpi_ut_pop_generic_state
992 (&state_list);
993 acpi_ut_delete_generic_state(state);
994 }
Len Brown4be44fc2005-08-05 00:44:28 -0400995 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998 }
999
1000 /* We should never get here */
1001
Len Brown4be44fc2005-08-05 00:44:28 -04001002 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}