blob: a8d549187c84262d5ae7ca9bd34ceee6d4a358dd [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;
185 info->parameter_type = ACPI_PARAM_ARGS;
186
187 /* Convert and validate the device handle */
188
189 info->prefix_node = acpi_ns_map_handle_to_node(handle);
190 if (!info->prefix_node) {
191 status = AE_BAD_PARAMETER;
192 goto cleanup;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
Bob Moore41195322006-05-26 16:36:00 -0400196 * If there are parameters to be passed to a control method, the external
197 * objects must all be converted to internal objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
199 if (external_params && external_params->count) {
200 /*
201 * Allocate a new parameter block for the internal objects
202 * Add 1 to count to allow for null terminated internal list
203 */
Bob Moore41195322006-05-26 16:36:00 -0400204 info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size)
205 external_params->
206 count +
207 1) * sizeof(void *));
208 if (!info->parameters) {
209 status = AE_NO_MEMORY;
210 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
Bob Moore41195322006-05-26 16:36:00 -0400213 /* Convert each external object in the list to an internal object */
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 for (i = 0; i < external_params->count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400216 status =
217 acpi_ut_copy_eobject_to_iobject(&external_params->
218 pointer[i],
Bob Moore41195322006-05-26 16:36:00 -0400219 &info->
Len Brown4be44fc2005-08-05 00:44:28 -0400220 parameters[i]);
221 if (ACPI_FAILURE(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400222 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
Bob Moore41195322006-05-26 16:36:00 -0400225 info->parameters[external_params->count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Three major cases:
230 * 1) Fully qualified pathname
231 * 2) No handle, not fully qualified pathname (error)
232 * 3) Valid handle
233 */
Len Brown4be44fc2005-08-05 00:44:28 -0400234 if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400235
236 /* The path is fully qualified, just evaluate by name */
237
Bob Moore41195322006-05-26 16:36:00 -0400238 info->prefix_node = NULL;
239 status = acpi_ns_evaluate(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400240 } else if (!handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
Bob Moore41195322006-05-26 16:36:00 -0400242 * A handle is optional iff a fully qualified pathname is specified.
243 * Since we've already handled fully qualified names above, this is
244 * an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246 if (!pathname) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
248 "Both Handle and Pathname are NULL"));
Len Brown4be44fc2005-08-05 00:44:28 -0400249 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400250 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
251 "Null Handle with relative pathname [%s]",
252 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 status = AE_BAD_PARAMETER;
Len Brown4be44fc2005-08-05 00:44:28 -0400256 } else {
Bob Moore41195322006-05-26 16:36:00 -0400257 /* We have a namespace a node and a possible relative path */
Bob Moore52fc0b02006-10-02 00:00:00 -0400258
Bob Moore41195322006-05-26 16:36:00 -0400259 status = acpi_ns_evaluate(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * If we are expecting a return value, and all went well above,
264 * copy the return value to an external object.
265 */
266 if (return_buffer) {
Bob Moore41195322006-05-26 16:36:00 -0400267 if (!info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return_buffer->length = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400269 } else {
Bob Moore41195322006-05-26 16:36:00 -0400270 if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400271 ACPI_DESC_TYPE_NAMED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /*
273 * If we received a NS Node as a return object, this means that
274 * the object we are evaluating has nothing interesting to
275 * return (such as a mutex, etc.) We return an error because
276 * these types are essentially unsupported by this interface.
277 * We don't check up front because this makes it easier to add
278 * support for various types at a later date if necessary.
279 */
280 status = AE_TYPE;
Bob Moore41195322006-05-26 16:36:00 -0400281 info->return_object = NULL; /* No need to delete a NS Node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return_buffer->length = 0;
283 }
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285 if (ACPI_SUCCESS(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400286
287 /* Get the size of the returned object */
288
Len Brown4be44fc2005-08-05 00:44:28 -0400289 status =
Bob Moore41195322006-05-26 16:36:00 -0400290 acpi_ut_get_object_size(info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400291 &buffer_space_needed);
292 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Validate/Allocate/Clear caller buffer */
295
Len Brown4be44fc2005-08-05 00:44:28 -0400296 status =
297 acpi_ut_initialize_buffer
298 (return_buffer,
299 buffer_space_needed);
300 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /*
Bob Moore52fc0b02006-10-02 00:00:00 -0400302 * Caller's buffer is too small or a new one can't
303 * be allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Len Brown4be44fc2005-08-05 00:44:28 -0400305 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
306 "Needed buffer size %X, %s\n",
307 (u32)
308 buffer_space_needed,
309 acpi_format_exception
310 (status)));
311 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400312 /* We have enough space for the object, build it */
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314 status =
315 acpi_ut_copy_iobject_to_eobject
Bob Moore41195322006-05-26 16:36:00 -0400316 (info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400317 return_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 }
320 }
321 }
322 }
323
Bob Moore41195322006-05-26 16:36:00 -0400324 if (info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
Bob Moore41195322006-05-26 16:36:00 -0400326 * Delete the internal return object. NOTE: Interpreter must be
327 * locked to avoid race condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Len Brown4d2acd92007-05-09 22:56:38 -0400329 acpi_ex_enter_interpreter();
Bob Moore41195322006-05-26 16:36:00 -0400330
Len Brown4d2acd92007-05-09 22:56:38 -0400331 /* Remove one reference on the return object (should delete it) */
Bob Moore41195322006-05-26 16:36:00 -0400332
Len Brown4d2acd92007-05-09 22:56:38 -0400333 acpi_ut_remove_reference(info->return_object);
334 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Bob Moore41195322006-05-26 16:36:00 -0400337 cleanup:
338
Bob Moore52fc0b02006-10-02 00:00:00 -0400339 /* Free the input parameter list (if we created one) */
340
Bob Moore41195322006-05-26 16:36:00 -0400341 if (info->parameters) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Free the allocated parameter block */
344
Bob Moore41195322006-05-26 16:36:00 -0400345 acpi_ut_delete_internal_object_list(info->parameters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Bob Moore41195322006-05-26 16:36:00 -0400348 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400349 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Bob Moore83135242006-10-03 00:00:00 -0400352ACPI_EXPORT_SYMBOL(acpi_evaluate_object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354/*******************************************************************************
355 *
356 * FUNCTION: acpi_walk_namespace
357 *
358 * PARAMETERS: Type - acpi_object_type to search for
359 * start_object - Handle in namespace where search begins
360 * max_depth - Depth to which search is to reach
361 * user_function - Called when an object of "Type" is found
362 * Context - Passed to user function
363 * return_value - Location where return value of
364 * user_function is put if terminated early
365 *
366 * RETURNS Return value from the user_function if terminated early.
367 * Otherwise, returns NULL.
368 *
369 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
370 * starting (and ending) at the object specified by start_handle.
371 * The user_function is called whenever an object that matches
372 * the type parameter is found. If the user function returns
373 * a non-zero value, the search is terminated immediately and this
374 * value is returned to the caller.
375 *
376 * The point of this procedure is to provide a generic namespace
377 * walk routine that can be called from multiple places to
378 * provide multiple services; the User Function can be tailored
379 * to each task, whether it is a print function, a compare
380 * function, etc.
381 *
382 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400384acpi_walk_namespace(acpi_object_type type,
385 acpi_handle start_object,
386 u32 max_depth,
387 acpi_walk_callback user_function,
388 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Len Brown4be44fc2005-08-05 00:44:28 -0400390 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bob Mooreb229cf92006-04-21 17:15:00 -0400392 ACPI_FUNCTION_TRACE(acpi_walk_namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* Parameter validation */
395
Bob Moore96db2552005-11-02 00:00:00 -0500396 if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400397 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 /*
401 * Lock the namespace around the walk.
402 * The namespace will be unlocked/locked around each call
403 * to the user function - since this function
404 * must be allowed to make Acpi calls itself.
405 */
Len Brown4be44fc2005-08-05 00:44:28 -0400406 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
407 if (ACPI_FAILURE(status)) {
408 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Len Brown4be44fc2005-08-05 00:44:28 -0400411 status = acpi_ns_walk_namespace(type, start_object, max_depth,
412 ACPI_NS_WALK_UNLOCK,
413 user_function, context, return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
416 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Bob Moore83135242006-10-03 00:00:00 -0400419ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421/*******************************************************************************
422 *
423 * FUNCTION: acpi_ns_get_device_callback
424 *
425 * PARAMETERS: Callback from acpi_get_device
426 *
427 * RETURN: Status
428 *
429 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
430 * present devices, or if they specified a HID, it filters based
431 * on that.
432 *
433 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400435acpi_ns_get_device_callback(acpi_handle obj_handle,
436 u32 nesting_level,
437 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Len Brown4be44fc2005-08-05 00:44:28 -0400439 struct acpi_get_devices_info *info = context;
440 acpi_status status;
441 struct acpi_namespace_node *node;
442 u32 flags;
Thomas Renninger8c8eb782007-07-23 14:43:32 +0200443 struct acpica_device_id hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct acpi_compatible_id_list *cid;
Len Brown4be44fc2005-08-05 00:44:28 -0400445 acpi_native_uint i;
Andrew Patterson02f8a852008-01-22 17:18:22 -0700446 int found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Len Brown4be44fc2005-08-05 00:44:28 -0400448 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
449 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return (status);
451 }
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 node = acpi_ns_map_handle_to_node(obj_handle);
454 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
455 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return (status);
457 }
458
459 if (!node) {
460 return (AE_BAD_PARAMETER);
461 }
462
463 /* Run _STA to determine if device is present */
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 status = acpi_ut_execute_STA(node, &flags);
466 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return (AE_CTRL_DEPTH);
468 }
469
Bob Moore549f4602008-04-10 19:06:41 +0400470 if (!(flags & ACPI_STA_DEVICE_PRESENT) &&
471 !(flags & ACPI_STA_DEVICE_FUNCTIONING)) {
472 /*
473 * Don't examine the children of the device only when the
474 * device is neither present nor functional. See ACPI spec,
475 * description of _STA for more information.
476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return (AE_CTRL_DEPTH);
478 }
479
480 /* Filter based on device HID & CID */
481
482 if (info->hid != NULL) {
Len Brown4be44fc2005-08-05 00:44:28 -0400483 status = acpi_ut_execute_HID(node, &hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (status == AE_NOT_FOUND) {
485 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400486 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return (AE_CTRL_DEPTH);
488 }
489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Get the list of Compatible IDs */
493
Len Brown4be44fc2005-08-05 00:44:28 -0400494 status = acpi_ut_execute_CID(node, &cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (status == AE_NOT_FOUND) {
496 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400497 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return (AE_CTRL_DEPTH);
499 }
500
501 /* Walk the CID list */
502
Andrew Patterson02f8a852008-01-22 17:18:22 -0700503 found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 for (i = 0; i < cid->count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400505 if (ACPI_STRNCMP(cid->id[i].value, info->hid,
506 sizeof(struct
Andrew Patterson02f8a852008-01-22 17:18:22 -0700507 acpi_compatible_id)) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400508 0) {
Andrew Patterson02f8a852008-01-22 17:18:22 -0700509 found = 1;
510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
Bob Moore83135242006-10-03 00:00:00 -0400513 ACPI_FREE(cid);
Andrew Patterson02f8a852008-01-22 17:18:22 -0700514 if (!found)
515 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518
Len Brown4be44fc2005-08-05 00:44:28 -0400519 status = info->user_function(obj_handle, nesting_level, info->context,
520 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return (status);
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*******************************************************************************
525 *
526 * FUNCTION: acpi_get_devices
527 *
528 * PARAMETERS: HID - HID to search for. Can be NULL.
529 * user_function - Called when a matching object is found
530 * Context - Passed to user function
531 * return_value - Location where return value of
532 * user_function is put if terminated early
533 *
534 * RETURNS Return value from the user_function if terminated early.
535 * Otherwise, returns NULL.
536 *
537 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
538 * starting (and ending) at the object specified by start_handle.
539 * The user_function is called whenever an object of type
540 * Device is found. If the user function returns
541 * a non-zero value, the search is terminated immediately and this
542 * value is returned to the caller.
543 *
544 * This is a wrapper for walk_namespace, but the callback performs
Bob Moore549f4602008-04-10 19:06:41 +0400545 * additional filtering. Please see acpi_ns_get_device_callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
547 ******************************************************************************/
548
549acpi_status
Al Viro70b30fb2007-08-21 16:18:20 +0100550acpi_get_devices(const char *HID,
Len Brown4be44fc2005-08-05 00:44:28 -0400551 acpi_walk_callback user_function,
552 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Len Brown4be44fc2005-08-05 00:44:28 -0400554 acpi_status status;
555 struct acpi_get_devices_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Bob Mooreb229cf92006-04-21 17:15:00 -0400557 ACPI_FUNCTION_TRACE(acpi_get_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Parameter validation */
560
561 if (!user_function) {
Len Brown4be44fc2005-08-05 00:44:28 -0400562 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 /*
566 * We're going to call their callback from OUR callback, so we need
567 * to know what it is, and their context parameter.
568 */
Bob Moore52fc0b02006-10-02 00:00:00 -0400569 info.hid = HID;
Len Brown4be44fc2005-08-05 00:44:28 -0400570 info.context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 info.user_function = user_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
574 * Lock the namespace around the walk.
575 * The namespace will be unlocked/locked around each call
576 * to the user function - since this function
577 * must be allowed to make Acpi calls itself.
578 */
Len Brown4be44fc2005-08-05 00:44:28 -0400579 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
580 if (ACPI_FAILURE(status)) {
581 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Bob Moore52fc0b02006-10-02 00:00:00 -0400584 status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
585 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
Len Brown4be44fc2005-08-05 00:44:28 -0400586 acpi_ns_get_device_callback, &info,
587 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Len Brown4be44fc2005-08-05 00:44:28 -0400589 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
590 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Bob Moore83135242006-10-03 00:00:00 -0400593ACPI_EXPORT_SYMBOL(acpi_get_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595/*******************************************************************************
596 *
597 * FUNCTION: acpi_attach_data
598 *
599 * PARAMETERS: obj_handle - Namespace node
600 * Handler - Handler for this attachment
601 * Data - Pointer to data to be attached
602 *
603 * RETURN: Status
604 *
605 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
606 *
607 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400609acpi_attach_data(acpi_handle obj_handle,
610 acpi_object_handler handler, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown4be44fc2005-08-05 00:44:28 -0400612 struct acpi_namespace_node *node;
613 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* Parameter validation */
616
Len Brown4be44fc2005-08-05 00:44:28 -0400617 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return (AE_BAD_PARAMETER);
619 }
620
Len Brown4be44fc2005-08-05 00:44:28 -0400621 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
622 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return (status);
624 }
625
626 /* Convert and validate the handle */
627
Len Brown4be44fc2005-08-05 00:44:28 -0400628 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!node) {
630 status = AE_BAD_PARAMETER;
631 goto unlock_and_exit;
632 }
633
Len Brown4be44fc2005-08-05 00:44:28 -0400634 status = acpi_ns_attach_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Len Brown4be44fc2005-08-05 00:44:28 -0400636 unlock_and_exit:
637 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return (status);
639}
640
Bob Moore83135242006-10-03 00:00:00 -0400641ACPI_EXPORT_SYMBOL(acpi_attach_data)
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*******************************************************************************
644 *
645 * FUNCTION: acpi_detach_data
646 *
647 * PARAMETERS: obj_handle - Namespace node handle
648 * Handler - Handler used in call to acpi_attach_data
649 *
650 * RETURN: Status
651 *
652 * DESCRIPTION: Remove data that was previously attached to a node.
653 *
654 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400656acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Len Brown4be44fc2005-08-05 00:44:28 -0400658 struct acpi_namespace_node *node;
659 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* Parameter validation */
662
Len Brown4be44fc2005-08-05 00:44:28 -0400663 if (!obj_handle || !handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return (AE_BAD_PARAMETER);
665 }
666
Len Brown4be44fc2005-08-05 00:44:28 -0400667 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
668 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return (status);
670 }
671
672 /* Convert and validate the handle */
673
Len Brown4be44fc2005-08-05 00:44:28 -0400674 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!node) {
676 status = AE_BAD_PARAMETER;
677 goto unlock_and_exit;
678 }
679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 status = acpi_ns_detach_data(node, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 unlock_and_exit:
683 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return (status);
685}
686
Bob Moore83135242006-10-03 00:00:00 -0400687ACPI_EXPORT_SYMBOL(acpi_detach_data)
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*******************************************************************************
690 *
691 * FUNCTION: acpi_get_data
692 *
693 * PARAMETERS: obj_handle - Namespace node
694 * Handler - Handler used in call to attach_data
695 * Data - Where the data is returned
696 *
697 * RETURN: Status
698 *
699 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
700 *
701 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400703acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Len Brown4be44fc2005-08-05 00:44:28 -0400705 struct acpi_namespace_node *node;
706 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* Parameter validation */
709
Len Brown4be44fc2005-08-05 00:44:28 -0400710 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return (AE_BAD_PARAMETER);
712 }
713
Len Brown4be44fc2005-08-05 00:44:28 -0400714 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
715 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return (status);
717 }
718
719 /* Convert and validate the handle */
720
Len Brown4be44fc2005-08-05 00:44:28 -0400721 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!node) {
723 status = AE_BAD_PARAMETER;
724 goto unlock_and_exit;
725 }
726
Len Brown4be44fc2005-08-05 00:44:28 -0400727 status = acpi_ns_get_attached_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Len Brown4be44fc2005-08-05 00:44:28 -0400729 unlock_and_exit:
730 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return (status);
732}
Bob Moore83135242006-10-03 00:00:00 -0400733
734ACPI_EXPORT_SYMBOL(acpi_get_data)