blob: 38be5865d95df30e382f21b2adb8d2b6837aa2f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
5 *
6 ******************************************************************************/
7
8/*
Len Brown75a44ce2008-04-23 23:00:13 -04009 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
46#include <acpi/acnamesp.h>
47#include <acpi/acinterp.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("nsxfeval")
Bob Moore83135242006-10-03 00:00:00 -040051#ifdef ACPI_FUTURE_USAGE
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*******************************************************************************
53 *
54 * FUNCTION: acpi_evaluate_object_typed
55 *
56 * PARAMETERS: Handle - Object handle (optional)
Robert Moore44f6c012005-04-18 22:49:35 -040057 * Pathname - Object pathname (optional)
58 * external_params - List of parameters to pass to method,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * terminated by NULL. May be NULL
60 * if no parameters are being passed.
Robert Moore44f6c012005-04-18 22:49:35 -040061 * return_buffer - Where to put method's return value (if
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * any). If NULL, no value is returned.
63 * return_type - Expected type of return object
64 *
65 * RETURN: Status
66 *
67 * DESCRIPTION: Find and evaluate the given object, passing the given
68 * parameters if necessary. One of "Handle" or "Pathname" must
69 * be valid (non-null)
70 *
71 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070072acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040073acpi_evaluate_object_typed(acpi_handle handle,
74 acpi_string pathname,
Len Brownfd350942007-05-09 23:34:35 -040075 struct acpi_object_list *external_params,
76 struct acpi_buffer *return_buffer,
Len Brown4be44fc2005-08-05 00:44:28 -040077 acpi_object_type return_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Len Brown4be44fc2005-08-05 00:44:28 -040079 acpi_status status;
80 u8 must_free = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Bob Mooreb229cf92006-04-21 17:15:00 -040082 ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /* Return buffer must be valid */
85
86 if (!return_buffer) {
Len Brown4be44fc2005-08-05 00:44:28 -040087 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
90 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
91 must_free = TRUE;
92 }
93
94 /* Evaluate the object */
95
Len Brown4be44fc2005-08-05 00:44:28 -040096 status =
97 acpi_evaluate_object(handle, pathname, external_params,
98 return_buffer);
99 if (ACPI_FAILURE(status)) {
100 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
103 /* Type ANY means "don't care" */
104
105 if (return_type == ACPI_TYPE_ANY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109 if (return_buffer->length == 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* Error because caller specifically asked for a return value */
112
Bob Mooreb8e4d892006-01-27 16:43:00 -0500113 ACPI_ERROR((AE_INFO, "No return value"));
Len Brown4be44fc2005-08-05 00:44:28 -0400114 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 /* Examine the object type returned from evaluate_object */
118
Len Brown4be44fc2005-08-05 00:44:28 -0400119 if (((union acpi_object *)return_buffer->pointer)->type == return_type) {
120 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 /* Return object type does not match requested type */
124
Bob Mooreb8e4d892006-01-27 16:43:00 -0500125 ACPI_ERROR((AE_INFO,
126 "Incorrect return type [%s] requested [%s]",
127 acpi_ut_get_type_name(((union acpi_object *)return_buffer->
128 pointer)->type),
129 acpi_ut_get_type_name(return_type)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (must_free) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
134
Len Brown02438d82006-06-30 03:19:10 -0400135 ACPI_FREE(return_buffer->pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return_buffer->pointer = NULL;
137 }
138
139 return_buffer->length = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 return_ACPI_STATUS(AE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Bob Moore83135242006-10-03 00:00:00 -0400142
143ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed)
Len Brown4be44fc2005-08-05 00:44:28 -0400144#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*******************************************************************************
146 *
147 * FUNCTION: acpi_evaluate_object
148 *
149 * PARAMETERS: Handle - Object handle (optional)
150 * Pathname - Object pathname (optional)
151 * external_params - List of parameters to pass to method,
152 * terminated by NULL. May be NULL
153 * if no parameters are being passed.
154 * return_buffer - Where to put method's return value (if
155 * any). If NULL, no value is returned.
156 *
157 * RETURN: Status
158 *
159 * DESCRIPTION: Find and evaluate the given object, passing the given
160 * parameters if necessary. One of "Handle" or "Pathname" must
161 * be valid (non-null)
162 *
163 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400165acpi_evaluate_object(acpi_handle handle,
166 acpi_string pathname,
167 struct acpi_object_list *external_params,
168 struct acpi_buffer *return_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Len Brown4be44fc2005-08-05 00:44:28 -0400170 acpi_status status;
Bob Moore41195322006-05-26 16:36:00 -0400171 struct acpi_evaluate_info *info;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_size buffer_space_needed;
173 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bob Mooreb229cf92006-04-21 17:15:00 -0400175 ACPI_FUNCTION_TRACE(acpi_evaluate_object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Bob Moore41195322006-05-26 16:36:00 -0400177 /* Allocate and initialize the evaluation information block */
178
179 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
180 if (!info) {
181 return_ACPI_STATUS(AE_NO_MEMORY);
182 }
183
184 info->pathname = pathname;
Bob Moore41195322006-05-26 16:36:00 -0400185
186 /* Convert and validate the device handle */
187
188 info->prefix_node = acpi_ns_map_handle_to_node(handle);
189 if (!info->prefix_node) {
190 status = AE_BAD_PARAMETER;
191 goto cleanup;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
Bob Moore41195322006-05-26 16:36:00 -0400195 * If there are parameters to be passed to a control method, the external
196 * objects must all be converted to internal objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198 if (external_params && external_params->count) {
199 /*
200 * Allocate a new parameter block for the internal objects
201 * Add 1 to count to allow for null terminated internal list
202 */
Bob Moore41195322006-05-26 16:36:00 -0400203 info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size)
204 external_params->
205 count +
206 1) * sizeof(void *));
207 if (!info->parameters) {
208 status = AE_NO_MEMORY;
209 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
Bob Moore41195322006-05-26 16:36:00 -0400212 /* Convert each external object in the list to an internal object */
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < external_params->count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400215 status =
216 acpi_ut_copy_eobject_to_iobject(&external_params->
217 pointer[i],
Bob Moore41195322006-05-26 16:36:00 -0400218 &info->
Len Brown4be44fc2005-08-05 00:44:28 -0400219 parameters[i]);
220 if (ACPI_FAILURE(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400221 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
Bob Moore41195322006-05-26 16:36:00 -0400224 info->parameters[external_params->count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
228 * Three major cases:
229 * 1) Fully qualified pathname
230 * 2) No handle, not fully qualified pathname (error)
231 * 3) Valid handle
232 */
Len Brown4be44fc2005-08-05 00:44:28 -0400233 if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400234
235 /* The path is fully qualified, just evaluate by name */
236
Bob Moore41195322006-05-26 16:36:00 -0400237 info->prefix_node = NULL;
238 status = acpi_ns_evaluate(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400239 } else if (!handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /*
Bob Moore41195322006-05-26 16:36:00 -0400241 * A handle is optional iff a fully qualified pathname is specified.
242 * Since we've already handled fully qualified names above, this is
243 * an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
245 if (!pathname) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400246 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
247 "Both Handle and Pathname are NULL"));
Len Brown4be44fc2005-08-05 00:44:28 -0400248 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400249 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
250 "Null Handle with relative pathname [%s]",
251 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 status = AE_BAD_PARAMETER;
Len Brown4be44fc2005-08-05 00:44:28 -0400255 } else {
Bob Moore41195322006-05-26 16:36:00 -0400256 /* We have a namespace a node and a possible relative path */
Bob Moore52fc0b02006-10-02 00:00:00 -0400257
Bob Moore41195322006-05-26 16:36:00 -0400258 status = acpi_ns_evaluate(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /*
262 * If we are expecting a return value, and all went well above,
263 * copy the return value to an external object.
264 */
265 if (return_buffer) {
Bob Moore41195322006-05-26 16:36:00 -0400266 if (!info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return_buffer->length = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400268 } else {
Bob Moore41195322006-05-26 16:36:00 -0400269 if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400270 ACPI_DESC_TYPE_NAMED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * If we received a NS Node as a return object, this means that
273 * the object we are evaluating has nothing interesting to
274 * return (such as a mutex, etc.) We return an error because
275 * these types are essentially unsupported by this interface.
276 * We don't check up front because this makes it easier to add
277 * support for various types at a later date if necessary.
278 */
279 status = AE_TYPE;
Bob Moore41195322006-05-26 16:36:00 -0400280 info->return_object = NULL; /* No need to delete a NS Node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return_buffer->length = 0;
282 }
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 if (ACPI_SUCCESS(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400285
286 /* Get the size of the returned object */
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 status =
Bob Moore41195322006-05-26 16:36:00 -0400289 acpi_ut_get_object_size(info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400290 &buffer_space_needed);
291 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Validate/Allocate/Clear caller buffer */
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295 status =
296 acpi_ut_initialize_buffer
297 (return_buffer,
298 buffer_space_needed);
299 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
Bob Moore52fc0b02006-10-02 00:00:00 -0400301 * Caller's buffer is too small or a new one can't
302 * be allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
Len Brown4be44fc2005-08-05 00:44:28 -0400304 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
305 "Needed buffer size %X, %s\n",
306 (u32)
307 buffer_space_needed,
308 acpi_format_exception
309 (status)));
310 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400311 /* We have enough space for the object, build it */
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313 status =
314 acpi_ut_copy_iobject_to_eobject
Bob Moore41195322006-05-26 16:36:00 -0400315 (info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400316 return_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
319 }
320 }
321 }
322
Bob Moore41195322006-05-26 16:36:00 -0400323 if (info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
Bob Moore41195322006-05-26 16:36:00 -0400325 * Delete the internal return object. NOTE: Interpreter must be
326 * locked to avoid race condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Len Brown4d2acd92007-05-09 22:56:38 -0400328 acpi_ex_enter_interpreter();
Bob Moore41195322006-05-26 16:36:00 -0400329
Len Brown4d2acd92007-05-09 22:56:38 -0400330 /* Remove one reference on the return object (should delete it) */
Bob Moore41195322006-05-26 16:36:00 -0400331
Len Brown4d2acd92007-05-09 22:56:38 -0400332 acpi_ut_remove_reference(info->return_object);
333 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Bob Moore41195322006-05-26 16:36:00 -0400336 cleanup:
337
Bob Moore52fc0b02006-10-02 00:00:00 -0400338 /* Free the input parameter list (if we created one) */
339
Bob Moore41195322006-05-26 16:36:00 -0400340 if (info->parameters) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Free the allocated parameter block */
343
Bob Moore41195322006-05-26 16:36:00 -0400344 acpi_ut_delete_internal_object_list(info->parameters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Bob Moore41195322006-05-26 16:36:00 -0400347 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400348 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Bob Moore83135242006-10-03 00:00:00 -0400351ACPI_EXPORT_SYMBOL(acpi_evaluate_object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/*******************************************************************************
354 *
355 * FUNCTION: acpi_walk_namespace
356 *
357 * PARAMETERS: Type - acpi_object_type to search for
358 * start_object - Handle in namespace where search begins
359 * max_depth - Depth to which search is to reach
360 * user_function - Called when an object of "Type" is found
361 * Context - Passed to user function
362 * return_value - Location where return value of
363 * user_function is put if terminated early
364 *
365 * RETURNS Return value from the user_function if terminated early.
366 * Otherwise, returns NULL.
367 *
368 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
369 * starting (and ending) at the object specified by start_handle.
370 * The user_function is called whenever an object that matches
371 * the type parameter is found. If the user function returns
372 * a non-zero value, the search is terminated immediately and this
373 * value is returned to the caller.
374 *
375 * The point of this procedure is to provide a generic namespace
376 * walk routine that can be called from multiple places to
377 * provide multiple services; the User Function can be tailored
378 * to each task, whether it is a print function, a compare
379 * function, etc.
380 *
381 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400383acpi_walk_namespace(acpi_object_type type,
384 acpi_handle start_object,
385 u32 max_depth,
386 acpi_walk_callback user_function,
387 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Len Brown4be44fc2005-08-05 00:44:28 -0400389 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Bob Mooreb229cf92006-04-21 17:15:00 -0400391 ACPI_FUNCTION_TRACE(acpi_walk_namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Parameter validation */
394
Bob Moore96db2552005-11-02 00:00:00 -0500395 if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400396 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /*
400 * Lock the namespace around the walk.
401 * The namespace will be unlocked/locked around each call
402 * to the user function - since this function
403 * must be allowed to make Acpi calls itself.
404 */
Len Brown4be44fc2005-08-05 00:44:28 -0400405 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
406 if (ACPI_FAILURE(status)) {
407 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 status = acpi_ns_walk_namespace(type, start_object, max_depth,
411 ACPI_NS_WALK_UNLOCK,
412 user_function, context, return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
415 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Bob Moore83135242006-10-03 00:00:00 -0400418ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/*******************************************************************************
421 *
422 * FUNCTION: acpi_ns_get_device_callback
423 *
424 * PARAMETERS: Callback from acpi_get_device
425 *
426 * RETURN: Status
427 *
428 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
429 * present devices, or if they specified a HID, it filters based
430 * on that.
431 *
432 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400434acpi_ns_get_device_callback(acpi_handle obj_handle,
435 u32 nesting_level,
436 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown4be44fc2005-08-05 00:44:28 -0400438 struct acpi_get_devices_info *info = context;
439 acpi_status status;
440 struct acpi_namespace_node *node;
441 u32 flags;
Thomas Renninger8c8eb782007-07-23 14:43:32 +0200442 struct acpica_device_id hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct acpi_compatible_id_list *cid;
Bob Moore67a119f2008-06-10 13:42:13 +0800444 u32 i;
Andrew Patterson02f8a852008-01-22 17:18:22 -0700445 int found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
448 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return (status);
450 }
451
Len Brown4be44fc2005-08-05 00:44:28 -0400452 node = acpi_ns_map_handle_to_node(obj_handle);
453 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
454 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return (status);
456 }
457
458 if (!node) {
459 return (AE_BAD_PARAMETER);
460 }
461
462 /* Run _STA to determine if device is present */
463
Len Brown4be44fc2005-08-05 00:44:28 -0400464 status = acpi_ut_execute_STA(node, &flags);
465 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return (AE_CTRL_DEPTH);
467 }
468
Bob Moore549f4602008-04-10 19:06:41 +0400469 if (!(flags & ACPI_STA_DEVICE_PRESENT) &&
470 !(flags & ACPI_STA_DEVICE_FUNCTIONING)) {
471 /*
472 * Don't examine the children of the device only when the
473 * device is neither present nor functional. See ACPI spec,
474 * description of _STA for more information.
475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return (AE_CTRL_DEPTH);
477 }
478
479 /* Filter based on device HID & CID */
480
481 if (info->hid != NULL) {
Len Brown4be44fc2005-08-05 00:44:28 -0400482 status = acpi_ut_execute_HID(node, &hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (status == AE_NOT_FOUND) {
484 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400485 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return (AE_CTRL_DEPTH);
487 }
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* Get the list of Compatible IDs */
492
Len Brown4be44fc2005-08-05 00:44:28 -0400493 status = acpi_ut_execute_CID(node, &cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (status == AE_NOT_FOUND) {
495 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400496 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return (AE_CTRL_DEPTH);
498 }
499
500 /* Walk the CID list */
501
Andrew Patterson02f8a852008-01-22 17:18:22 -0700502 found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 for (i = 0; i < cid->count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400504 if (ACPI_STRNCMP(cid->id[i].value, info->hid,
505 sizeof(struct
Andrew Patterson02f8a852008-01-22 17:18:22 -0700506 acpi_compatible_id)) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400507 0) {
Andrew Patterson02f8a852008-01-22 17:18:22 -0700508 found = 1;
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 }
Bob Moore83135242006-10-03 00:00:00 -0400512 ACPI_FREE(cid);
Andrew Patterson02f8a852008-01-22 17:18:22 -0700513 if (!found)
514 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 }
517
Len Brown4be44fc2005-08-05 00:44:28 -0400518 status = info->user_function(obj_handle, nesting_level, info->context,
519 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return (status);
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*******************************************************************************
524 *
525 * FUNCTION: acpi_get_devices
526 *
527 * PARAMETERS: HID - HID to search for. Can be NULL.
528 * user_function - Called when a matching object is found
529 * Context - Passed to user function
530 * return_value - Location where return value of
531 * user_function is put if terminated early
532 *
533 * RETURNS Return value from the user_function if terminated early.
534 * Otherwise, returns NULL.
535 *
536 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
537 * starting (and ending) at the object specified by start_handle.
538 * The user_function is called whenever an object of type
539 * Device is found. If the user function returns
540 * a non-zero value, the search is terminated immediately and this
541 * value is returned to the caller.
542 *
543 * This is a wrapper for walk_namespace, but the callback performs
Bob Moore549f4602008-04-10 19:06:41 +0400544 * additional filtering. Please see acpi_ns_get_device_callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
546 ******************************************************************************/
547
548acpi_status
Al Viro70b30fb2007-08-21 16:18:20 +0100549acpi_get_devices(const char *HID,
Len Brown4be44fc2005-08-05 00:44:28 -0400550 acpi_walk_callback user_function,
551 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Len Brown4be44fc2005-08-05 00:44:28 -0400553 acpi_status status;
554 struct acpi_get_devices_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Bob Mooreb229cf92006-04-21 17:15:00 -0400556 ACPI_FUNCTION_TRACE(acpi_get_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Parameter validation */
559
560 if (!user_function) {
Len Brown4be44fc2005-08-05 00:44:28 -0400561 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 /*
565 * We're going to call their callback from OUR callback, so we need
566 * to know what it is, and their context parameter.
567 */
Bob Moore52fc0b02006-10-02 00:00:00 -0400568 info.hid = HID;
Len Brown4be44fc2005-08-05 00:44:28 -0400569 info.context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 info.user_function = user_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
573 * Lock the namespace around the walk.
574 * The namespace will be unlocked/locked around each call
575 * to the user function - since this function
576 * must be allowed to make Acpi calls itself.
577 */
Len Brown4be44fc2005-08-05 00:44:28 -0400578 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
579 if (ACPI_FAILURE(status)) {
580 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
Bob Moore52fc0b02006-10-02 00:00:00 -0400583 status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
584 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
Len Brown4be44fc2005-08-05 00:44:28 -0400585 acpi_ns_get_device_callback, &info,
586 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Len Brown4be44fc2005-08-05 00:44:28 -0400588 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
589 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Bob Moore83135242006-10-03 00:00:00 -0400592ACPI_EXPORT_SYMBOL(acpi_get_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/*******************************************************************************
595 *
596 * FUNCTION: acpi_attach_data
597 *
598 * PARAMETERS: obj_handle - Namespace node
599 * Handler - Handler for this attachment
600 * Data - Pointer to data to be attached
601 *
602 * RETURN: Status
603 *
604 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
605 *
606 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400608acpi_attach_data(acpi_handle obj_handle,
609 acpi_object_handler handler, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Len Brown4be44fc2005-08-05 00:44:28 -0400611 struct acpi_namespace_node *node;
612 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* Parameter validation */
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return (AE_BAD_PARAMETER);
618 }
619
Len Brown4be44fc2005-08-05 00:44:28 -0400620 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
621 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return (status);
623 }
624
625 /* Convert and validate the handle */
626
Len Brown4be44fc2005-08-05 00:44:28 -0400627 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!node) {
629 status = AE_BAD_PARAMETER;
630 goto unlock_and_exit;
631 }
632
Len Brown4be44fc2005-08-05 00:44:28 -0400633 status = acpi_ns_attach_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Len Brown4be44fc2005-08-05 00:44:28 -0400635 unlock_and_exit:
636 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return (status);
638}
639
Bob Moore83135242006-10-03 00:00:00 -0400640ACPI_EXPORT_SYMBOL(acpi_attach_data)
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*******************************************************************************
643 *
644 * FUNCTION: acpi_detach_data
645 *
646 * PARAMETERS: obj_handle - Namespace node handle
647 * Handler - Handler used in call to acpi_attach_data
648 *
649 * RETURN: Status
650 *
651 * DESCRIPTION: Remove data that was previously attached to a node.
652 *
653 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400655acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Len Brown4be44fc2005-08-05 00:44:28 -0400657 struct acpi_namespace_node *node;
658 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Parameter validation */
661
Len Brown4be44fc2005-08-05 00:44:28 -0400662 if (!obj_handle || !handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return (AE_BAD_PARAMETER);
664 }
665
Len Brown4be44fc2005-08-05 00:44:28 -0400666 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
667 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return (status);
669 }
670
671 /* Convert and validate the handle */
672
Len Brown4be44fc2005-08-05 00:44:28 -0400673 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!node) {
675 status = AE_BAD_PARAMETER;
676 goto unlock_and_exit;
677 }
678
Len Brown4be44fc2005-08-05 00:44:28 -0400679 status = acpi_ns_detach_data(node, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Len Brown4be44fc2005-08-05 00:44:28 -0400681 unlock_and_exit:
682 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return (status);
684}
685
Bob Moore83135242006-10-03 00:00:00 -0400686ACPI_EXPORT_SYMBOL(acpi_detach_data)
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*******************************************************************************
689 *
690 * FUNCTION: acpi_get_data
691 *
692 * PARAMETERS: obj_handle - Namespace node
693 * Handler - Handler used in call to attach_data
694 * Data - Where the data is returned
695 *
696 * RETURN: Status
697 *
698 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
699 *
700 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400702acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Len Brown4be44fc2005-08-05 00:44:28 -0400704 struct acpi_namespace_node *node;
705 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* Parameter validation */
708
Len Brown4be44fc2005-08-05 00:44:28 -0400709 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return (AE_BAD_PARAMETER);
711 }
712
Len Brown4be44fc2005-08-05 00:44:28 -0400713 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
714 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return (status);
716 }
717
718 /* Convert and validate the handle */
719
Len Brown4be44fc2005-08-05 00:44:28 -0400720 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!node) {
722 status = AE_BAD_PARAMETER;
723 goto unlock_and_exit;
724 }
725
Len Brown4be44fc2005-08-05 00:44:28 -0400726 status = acpi_ns_get_attached_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Len Brown4be44fc2005-08-05 00:44:28 -0400728 unlock_and_exit:
729 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return (status);
731}
Bob Moore83135242006-10-03 00:00:00 -0400732
733ACPI_EXPORT_SYMBOL(acpi_get_data)