| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /****************************************************************************** | 
 | 2 |  * | 
 | 3 |  * Module Name: nswalk - Functions for walking the ACPI namespace | 
 | 4 |  * | 
 | 5 |  *****************************************************************************/ | 
 | 6 |  | 
 | 7 | /* | 
| Len Brown | 75a44ce | 2008-04-23 23:00:13 -0400 | [diff] [blame] | 8 |  * Copyright (C) 2000 - 2008, Intel Corp. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 |  * All rights reserved. | 
 | 10 |  * | 
 | 11 |  * Redistribution and use in source and binary forms, with or without | 
 | 12 |  * modification, are permitted provided that the following conditions | 
 | 13 |  * are met: | 
 | 14 |  * 1. Redistributions of source code must retain the above copyright | 
 | 15 |  *    notice, this list of conditions, and the following disclaimer, | 
 | 16 |  *    without modification. | 
 | 17 |  * 2. Redistributions in binary form must reproduce at minimum a disclaimer | 
 | 18 |  *    substantially similar to the "NO WARRANTY" disclaimer below | 
 | 19 |  *    ("Disclaimer") and any redistribution must be conditioned upon | 
 | 20 |  *    including a substantially similar Disclaimer requirement for further | 
 | 21 |  *    binary redistribution. | 
 | 22 |  * 3. Neither the names of the above-listed copyright holders nor the names | 
 | 23 |  *    of any contributors may be used to endorse or promote products derived | 
 | 24 |  *    from this software without specific prior written permission. | 
 | 25 |  * | 
 | 26 |  * Alternatively, this software may be distributed under the terms of the | 
 | 27 |  * GNU General Public License ("GPL") version 2 as published by the Free | 
 | 28 |  * Software Foundation. | 
 | 29 |  * | 
 | 30 |  * NO WARRANTY | 
 | 31 |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
 | 32 |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
 | 33 |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR | 
 | 34 |  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
 | 35 |  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
 | 36 |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
 | 37 |  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
 | 38 |  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | 
 | 39 |  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | 
 | 40 |  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
 | 41 |  * POSSIBILITY OF SUCH DAMAGES. | 
 | 42 |  */ | 
 | 43 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <acpi/acpi.h> | 
| Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 45 | #include "accommon.h" | 
 | 46 | #include "acnamesp.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define _COMPONENT          ACPI_NAMESPACE | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 49 | ACPI_MODULE_NAME("nswalk") | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
 | 51 | /******************************************************************************* | 
 | 52 |  * | 
 | 53 |  * FUNCTION:    acpi_ns_get_next_node | 
 | 54 |  * | 
 | 55 |  * PARAMETERS:  Type                - Type of node to be searched for | 
 | 56 |  *              parent_node         - Parent node whose children we are | 
| Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 57 |  *                                    getting | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 |  *              child_node          - Previous child that was found. | 
 | 59 |  *                                    The NEXT child will be returned | 
 | 60 |  * | 
 | 61 |  * RETURN:      struct acpi_namespace_node - Pointer to the NEXT child or NULL if | 
 | 62 |  *                                    none is found. | 
 | 63 |  * | 
 | 64 |  * DESCRIPTION: Return the next peer node within the namespace.  If Handle | 
 | 65 |  *              is valid, Scope is ignored.  Otherwise, the first node | 
 | 66 |  *              within Scope is returned. | 
 | 67 |  * | 
 | 68 |  ******************************************************************************/ | 
| Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 69 | struct acpi_namespace_node *acpi_ns_get_next_node(acpi_object_type type, struct acpi_namespace_node | 
 | 70 | 						  *parent_node, struct acpi_namespace_node | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 71 | 						  *child_node) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | 	struct acpi_namespace_node *next_node = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 75 | 	ACPI_FUNCTION_ENTRY(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 |  | 
 | 77 | 	if (!child_node) { | 
| Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 78 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | 		/* It's really the parent's _scope_ that we want */ | 
 | 80 |  | 
| Bob Moore | a69c77c | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 81 | 		next_node = parent_node->child; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | 	} | 
 | 83 |  | 
 | 84 | 	else { | 
 | 85 | 		/* Start search at the NEXT node */ | 
 | 86 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 87 | 		next_node = acpi_ns_get_next_valid_node(child_node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 	} | 
 | 89 |  | 
 | 90 | 	/* If any type is OK, we are done */ | 
 | 91 |  | 
 | 92 | 	if (type == ACPI_TYPE_ANY) { | 
| Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 93 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 		/* next_node is NULL if we are at the end-of-list */ | 
 | 95 |  | 
 | 96 | 		return (next_node); | 
 | 97 | 	} | 
 | 98 |  | 
 | 99 | 	/* Must search for the node -- but within this scope only */ | 
 | 100 |  | 
 | 101 | 	while (next_node) { | 
| Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 102 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | 		/* If type matches, we are done */ | 
 | 104 |  | 
 | 105 | 		if (next_node->type == type) { | 
 | 106 | 			return (next_node); | 
 | 107 | 		} | 
 | 108 |  | 
 | 109 | 		/* Otherwise, move on to the next node */ | 
 | 110 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 111 | 		next_node = acpi_ns_get_next_valid_node(next_node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 	} | 
 | 113 |  | 
 | 114 | 	/* Not found */ | 
 | 115 |  | 
 | 116 | 	return (NULL); | 
 | 117 | } | 
 | 118 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /******************************************************************************* | 
 | 120 |  * | 
 | 121 |  * FUNCTION:    acpi_ns_walk_namespace | 
 | 122 |  * | 
 | 123 |  * PARAMETERS:  Type                - acpi_object_type to search for | 
 | 124 |  *              start_node          - Handle in namespace where search begins | 
 | 125 |  *              max_depth           - Depth to which search is to reach | 
| Bob Moore | d1fdda83 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 126 |  *              Flags               - Whether to unlock the NS before invoking | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 |  *                                    the callback routine | 
 | 128 |  *              user_function       - Called when an object of "Type" is found | 
 | 129 |  *              Context             - Passed to user function | 
 | 130 |  *              return_value        - from the user_function if terminated early. | 
 | 131 |  *                                    Otherwise, returns NULL. | 
 | 132 |  * RETURNS:     Status | 
 | 133 |  * | 
 | 134 |  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, | 
 | 135 |  *              starting (and ending) at the node specified by start_handle. | 
 | 136 |  *              The user_function is called whenever a node that matches | 
 | 137 |  *              the type parameter is found.  If the user function returns | 
| Bob Moore | d4913dc | 2009-03-06 10:05:18 +0800 | [diff] [blame] | 138 |  *              a non-zero value, the search is terminated immediately and | 
 | 139 |  *              this value is returned to the caller. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 |  * | 
 | 141 |  *              The point of this procedure is to provide a generic namespace | 
 | 142 |  *              walk routine that can be called from multiple places to | 
 | 143 |  *              provide multiple services;  the User Function can be tailored | 
 | 144 |  *              to each task, whether it is a print function, a compare | 
 | 145 |  *              function, etc. | 
 | 146 |  * | 
 | 147 |  ******************************************************************************/ | 
 | 148 |  | 
 | 149 | acpi_status | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 150 | acpi_ns_walk_namespace(acpi_object_type type, | 
 | 151 | 		       acpi_handle start_node, | 
 | 152 | 		       u32 max_depth, | 
| Bob Moore | d1fdda83 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 153 | 		       u32 flags, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | 		       acpi_walk_callback user_function, | 
 | 155 | 		       void *context, void **return_value) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 157 | 	acpi_status status; | 
 | 158 | 	acpi_status mutex_status; | 
 | 159 | 	struct acpi_namespace_node *child_node; | 
 | 160 | 	struct acpi_namespace_node *parent_node; | 
 | 161 | 	acpi_object_type child_type; | 
 | 162 | 	u32 level; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 |  | 
| Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 164 | 	ACPI_FUNCTION_TRACE(ns_walk_namespace); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 |  | 
 | 166 | 	/* Special case for the namespace Root Node */ | 
 | 167 |  | 
 | 168 | 	if (start_node == ACPI_ROOT_OBJECT) { | 
 | 169 | 		start_node = acpi_gbl_root_node; | 
 | 170 | 	} | 
 | 171 |  | 
 | 172 | 	/* Null child means "get first node" */ | 
 | 173 |  | 
 | 174 | 	parent_node = start_node; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 175 | 	child_node = NULL; | 
 | 176 | 	child_type = ACPI_TYPE_ANY; | 
 | 177 | 	level = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 |  | 
 | 179 | 	/* | 
 | 180 | 	 * Traverse the tree of nodes until we bubble back up to where we | 
 | 181 | 	 * started. When Level is zero, the loop is done because we have | 
 | 182 | 	 * bubbled up to (and passed) the original parent handle (start_entry) | 
 | 183 | 	 */ | 
 | 184 | 	while (level > 0) { | 
| Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 185 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | 		/* Get the next node in this scope.  Null if not found */ | 
 | 187 |  | 
 | 188 | 		status = AE_OK; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 189 | 		child_node = | 
 | 190 | 		    acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node, | 
 | 191 | 					  child_node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | 		if (child_node) { | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 193 |  | 
| Bob Moore | c101462 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 194 | 			/* Found next child, get the type if we are not searching for ANY */ | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 195 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | 			if (type != ACPI_TYPE_ANY) { | 
 | 197 | 				child_type = child_node->type; | 
 | 198 | 			} | 
 | 199 |  | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 200 | 			/* | 
| Bob Moore | c101462 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 201 | 			 * Ignore all temporary namespace nodes (created during control | 
 | 202 | 			 * method execution) unless told otherwise. These temporary nodes | 
| Bob Moore | d4913dc | 2009-03-06 10:05:18 +0800 | [diff] [blame] | 203 | 			 * can cause a race condition because they can be deleted during | 
 | 204 | 			 * the execution of the user function (if the namespace is | 
 | 205 | 			 * unlocked before invocation of the user function.) Only the | 
 | 206 | 			 * debugger namespace dump will examine the temporary nodes. | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 207 | 			 */ | 
| Bob Moore | c101462 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 208 | 			if ((child_node->flags & ANOBJ_TEMPORARY) && | 
 | 209 | 			    !(flags & ACPI_NS_WALK_TEMP_NODES)) { | 
 | 210 | 				status = AE_CTRL_DEPTH; | 
 | 211 | 			} | 
 | 212 |  | 
 | 213 | 			/* Type must match requested type */ | 
 | 214 |  | 
 | 215 | 			else if (child_type == type) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | 				/* | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 217 | 				 * Found a matching node, invoke the user callback function. | 
 | 218 | 				 * Unlock the namespace if flag is set. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | 				 */ | 
| Bob Moore | d1fdda83 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 220 | 				if (flags & ACPI_NS_WALK_UNLOCK) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 221 | 					mutex_status = | 
 | 222 | 					    acpi_ut_release_mutex | 
 | 223 | 					    (ACPI_MTX_NAMESPACE); | 
 | 224 | 					if (ACPI_FAILURE(mutex_status)) { | 
 | 225 | 						return_ACPI_STATUS | 
 | 226 | 						    (mutex_status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | 					} | 
 | 228 | 				} | 
 | 229 |  | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 230 | 				status = | 
 | 231 | 				    user_function(child_node, level, context, | 
 | 232 | 						  return_value); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 |  | 
| Bob Moore | d1fdda83 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 234 | 				if (flags & ACPI_NS_WALK_UNLOCK) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 235 | 					mutex_status = | 
 | 236 | 					    acpi_ut_acquire_mutex | 
 | 237 | 					    (ACPI_MTX_NAMESPACE); | 
 | 238 | 					if (ACPI_FAILURE(mutex_status)) { | 
 | 239 | 						return_ACPI_STATUS | 
 | 240 | 						    (mutex_status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | 					} | 
 | 242 | 				} | 
 | 243 |  | 
 | 244 | 				switch (status) { | 
 | 245 | 				case AE_OK: | 
 | 246 | 				case AE_CTRL_DEPTH: | 
 | 247 |  | 
 | 248 | 					/* Just keep going */ | 
 | 249 | 					break; | 
 | 250 |  | 
 | 251 | 				case AE_CTRL_TERMINATE: | 
 | 252 |  | 
 | 253 | 					/* Exit now, with OK status */ | 
 | 254 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 255 | 					return_ACPI_STATUS(AE_OK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 |  | 
 | 257 | 				default: | 
 | 258 |  | 
 | 259 | 					/* All others are valid exceptions */ | 
 | 260 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 261 | 					return_ACPI_STATUS(status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | 				} | 
 | 263 | 			} | 
 | 264 |  | 
 | 265 | 			/* | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 266 | 			 * Depth first search: Attempt to go down another level in the | 
 | 267 | 			 * namespace if we are allowed to.  Don't go any further if we have | 
 | 268 | 			 * reached the caller specified maximum depth or if the user | 
 | 269 | 			 * function has specified that the maximum depth has been reached. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | 			 */ | 
 | 271 | 			if ((level < max_depth) && (status != AE_CTRL_DEPTH)) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 272 | 				if (acpi_ns_get_next_node | 
 | 273 | 				    (ACPI_TYPE_ANY, child_node, NULL)) { | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 274 |  | 
 | 275 | 					/* There is at least one child of this node, visit it */ | 
 | 276 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 					level++; | 
 | 278 | 					parent_node = child_node; | 
 | 279 | 					child_node = NULL; | 
 | 280 | 				} | 
 | 281 | 			} | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 282 | 		} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 			/* | 
| Bob Moore | 3effba3 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 284 | 			 * No more children of this node (acpi_ns_get_next_node failed), go | 
 | 285 | 			 * back upwards in the namespace tree to the node's parent. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 			 */ | 
 | 287 | 			level--; | 
 | 288 | 			child_node = parent_node; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 289 | 			parent_node = acpi_ns_get_parent_node(parent_node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		} | 
 | 291 | 	} | 
 | 292 |  | 
 | 293 | 	/* Complete walk, not terminated by user function */ | 
 | 294 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 295 | 	return_ACPI_STATUS(AE_OK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |