blob: 6d9bd45af30a6c95b997978d6d6d02e68b0fcc3c [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/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05009 * Copyright (C) 2000 - 2006, R. Byron Moore
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")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Bob Moore83135242006-10-03 00:00:00 -040052#ifdef ACPI_FUTURE_USAGE
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*******************************************************************************
54 *
55 * FUNCTION: acpi_evaluate_object_typed
56 *
57 * PARAMETERS: Handle - Object handle (optional)
Robert Moore44f6c012005-04-18 22:49:35 -040058 * Pathname - Object pathname (optional)
59 * external_params - List of parameters to pass to method,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * terminated by NULL. May be NULL
61 * if no parameters are being passed.
Robert Moore44f6c012005-04-18 22:49:35 -040062 * return_buffer - Where to put method's return value (if
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * any). If NULL, no value is returned.
64 * return_type - Expected type of return object
65 *
66 * RETURN: Status
67 *
68 * DESCRIPTION: Find and evaluate the given object, passing the given
69 * parameters if necessary. One of "Handle" or "Pathname" must
70 * be valid (non-null)
71 *
72 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070073acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040074acpi_evaluate_object_typed(acpi_handle handle,
75 acpi_string pathname,
Bob Moore83135242006-10-03 00:00:00 -040076 struct acpi_object_list * external_params,
77 struct acpi_buffer * return_buffer,
Len Brown4be44fc2005-08-05 00:44:28 -040078 acpi_object_type return_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Len Brown4be44fc2005-08-05 00:44:28 -040080 acpi_status status;
81 u8 must_free = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Bob Mooreb229cf92006-04-21 17:15:00 -040083 ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* Return buffer must be valid */
86
87 if (!return_buffer) {
Len Brown4be44fc2005-08-05 00:44:28 -040088 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
91 if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
92 must_free = TRUE;
93 }
94
95 /* Evaluate the object */
96
Len Brown4be44fc2005-08-05 00:44:28 -040097 status =
98 acpi_evaluate_object(handle, pathname, external_params,
99 return_buffer);
100 if (ACPI_FAILURE(status)) {
101 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 /* Type ANY means "don't care" */
105
106 if (return_type == ACPI_TYPE_ANY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400107 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
110 if (return_buffer->length == 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Error because caller specifically asked for a return value */
113
Bob Mooreb8e4d892006-01-27 16:43:00 -0500114 ACPI_ERROR((AE_INFO, "No return value"));
Len Brown4be44fc2005-08-05 00:44:28 -0400115 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 /* Examine the object type returned from evaluate_object */
119
Len Brown4be44fc2005-08-05 00:44:28 -0400120 if (((union acpi_object *)return_buffer->pointer)->type == return_type) {
121 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 /* Return object type does not match requested type */
125
Bob Mooreb8e4d892006-01-27 16:43:00 -0500126 ACPI_ERROR((AE_INFO,
127 "Incorrect return type [%s] requested [%s]",
128 acpi_ut_get_type_name(((union acpi_object *)return_buffer->
129 pointer)->type),
130 acpi_ut_get_type_name(return_type)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (must_free) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136 acpi_os_free(return_buffer->pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return_buffer->pointer = NULL;
138 }
139
140 return_buffer->length = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 return_ACPI_STATUS(AE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Bob Moore83135242006-10-03 00:00:00 -0400143
144ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed)
Len Brown4be44fc2005-08-05 00:44:28 -0400145#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*******************************************************************************
148 *
149 * FUNCTION: acpi_evaluate_object
150 *
151 * PARAMETERS: Handle - Object handle (optional)
152 * Pathname - Object pathname (optional)
153 * external_params - List of parameters to pass to method,
154 * terminated by NULL. May be NULL
155 * if no parameters are being passed.
156 * return_buffer - Where to put method's return value (if
157 * any). If NULL, no value is returned.
158 *
159 * RETURN: Status
160 *
161 * DESCRIPTION: Find and evaluate the given object, passing the given
162 * parameters if necessary. One of "Handle" or "Pathname" must
163 * be valid (non-null)
164 *
165 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400167acpi_evaluate_object(acpi_handle handle,
168 acpi_string pathname,
169 struct acpi_object_list *external_params,
170 struct acpi_buffer *return_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_status status;
173 acpi_status status2;
Bob Moore41195322006-05-26 16:36:00 -0400174 struct acpi_evaluate_info *info;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 acpi_size buffer_space_needed;
176 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Bob Mooreb229cf92006-04-21 17:15:00 -0400178 ACPI_FUNCTION_TRACE(acpi_evaluate_object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Bob Moore41195322006-05-26 16:36:00 -0400180 /* Allocate and initialize the evaluation information block */
181
182 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
183 if (!info) {
184 return_ACPI_STATUS(AE_NO_MEMORY);
185 }
186
187 info->pathname = pathname;
188 info->parameter_type = ACPI_PARAM_ARGS;
189
190 /* Convert and validate the device handle */
191
192 info->prefix_node = acpi_ns_map_handle_to_node(handle);
193 if (!info->prefix_node) {
194 status = AE_BAD_PARAMETER;
195 goto cleanup;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
Bob Moore41195322006-05-26 16:36:00 -0400199 * If there are parameters to be passed to a control method, the external
200 * objects must all be converted to internal objects
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202 if (external_params && external_params->count) {
203 /*
204 * Allocate a new parameter block for the internal objects
205 * Add 1 to count to allow for null terminated internal list
206 */
Bob Moore41195322006-05-26 16:36:00 -0400207 info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size)
208 external_params->
209 count +
210 1) * sizeof(void *));
211 if (!info->parameters) {
212 status = AE_NO_MEMORY;
213 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Bob Moore41195322006-05-26 16:36:00 -0400216 /* Convert each external object in the list to an internal object */
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 for (i = 0; i < external_params->count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400219 status =
220 acpi_ut_copy_eobject_to_iobject(&external_params->
221 pointer[i],
Bob Moore41195322006-05-26 16:36:00 -0400222 &info->
Len Brown4be44fc2005-08-05 00:44:28 -0400223 parameters[i]);
224 if (ACPI_FAILURE(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400225 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 }
Bob Moore41195322006-05-26 16:36:00 -0400228 info->parameters[external_params->count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /*
232 * Three major cases:
233 * 1) Fully qualified pathname
234 * 2) No handle, not fully qualified pathname (error)
235 * 3) Valid handle
236 */
Len Brown4be44fc2005-08-05 00:44:28 -0400237 if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400238
239 /* The path is fully qualified, just evaluate by name */
240
Bob Moore41195322006-05-26 16:36:00 -0400241 info->prefix_node = NULL;
242 status = acpi_ns_evaluate(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400243 } else if (!handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
Bob Moore41195322006-05-26 16:36:00 -0400245 * A handle is optional iff a fully qualified pathname is specified.
246 * Since we've already handled fully qualified names above, this is
247 * an error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249 if (!pathname) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400250 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
251 "Both Handle and Pathname are NULL"));
Len Brown4be44fc2005-08-05 00:44:28 -0400252 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400253 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
254 "Null Handle with relative pathname [%s]",
255 pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
258 status = AE_BAD_PARAMETER;
Len Brown4be44fc2005-08-05 00:44:28 -0400259 } else {
Bob Moore41195322006-05-26 16:36:00 -0400260 /* We have a namespace a node and a possible relative path */
Bob Moore52fc0b02006-10-02 00:00:00 -0400261
Bob Moore41195322006-05-26 16:36:00 -0400262 status = acpi_ns_evaluate(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * If we are expecting a return value, and all went well above,
267 * copy the return value to an external object.
268 */
269 if (return_buffer) {
Bob Moore41195322006-05-26 16:36:00 -0400270 if (!info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return_buffer->length = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400272 } else {
Bob Moore41195322006-05-26 16:36:00 -0400273 if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400274 ACPI_DESC_TYPE_NAMED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /*
276 * If we received a NS Node as a return object, this means that
277 * the object we are evaluating has nothing interesting to
278 * return (such as a mutex, etc.) We return an error because
279 * these types are essentially unsupported by this interface.
280 * We don't check up front because this makes it easier to add
281 * support for various types at a later date if necessary.
282 */
283 status = AE_TYPE;
Bob Moore41195322006-05-26 16:36:00 -0400284 info->return_object = NULL; /* No need to delete a NS Node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return_buffer->length = 0;
286 }
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 if (ACPI_SUCCESS(status)) {
Bob Moore41195322006-05-26 16:36:00 -0400289
290 /* Get the size of the returned object */
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292 status =
Bob Moore41195322006-05-26 16:36:00 -0400293 acpi_ut_get_object_size(info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400294 &buffer_space_needed);
295 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* Validate/Allocate/Clear caller buffer */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 status =
300 acpi_ut_initialize_buffer
301 (return_buffer,
302 buffer_space_needed);
303 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
Bob Moore52fc0b02006-10-02 00:00:00 -0400305 * Caller's buffer is too small or a new one can't
306 * be allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
Len Brown4be44fc2005-08-05 00:44:28 -0400308 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
309 "Needed buffer size %X, %s\n",
310 (u32)
311 buffer_space_needed,
312 acpi_format_exception
313 (status)));
314 } else {
Bob Moore52fc0b02006-10-02 00:00:00 -0400315 /* We have enough space for the object, build it */
316
Len Brown4be44fc2005-08-05 00:44:28 -0400317 status =
318 acpi_ut_copy_iobject_to_eobject
Bob Moore41195322006-05-26 16:36:00 -0400319 (info->return_object,
Len Brown4be44fc2005-08-05 00:44:28 -0400320 return_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 }
323 }
324 }
325 }
326
Bob Moore41195322006-05-26 16:36:00 -0400327 if (info->return_object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /*
Bob Moore41195322006-05-26 16:36:00 -0400329 * Delete the internal return object. NOTE: Interpreter must be
330 * locked to avoid race condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Len Brown4be44fc2005-08-05 00:44:28 -0400332 status2 = acpi_ex_enter_interpreter();
333 if (ACPI_SUCCESS(status2)) {
Bob Moore41195322006-05-26 16:36:00 -0400334
335 /* Remove one reference on the return object (should delete it) */
336
337 acpi_ut_remove_reference(info->return_object);
Len Brown4be44fc2005-08-05 00:44:28 -0400338 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
341
Bob Moore41195322006-05-26 16:36:00 -0400342 cleanup:
343
Bob Moore52fc0b02006-10-02 00:00:00 -0400344 /* Free the input parameter list (if we created one) */
345
Bob Moore41195322006-05-26 16:36:00 -0400346 if (info->parameters) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* Free the allocated parameter block */
349
Bob Moore41195322006-05-26 16:36:00 -0400350 acpi_ut_delete_internal_object_list(info->parameters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Bob Moore41195322006-05-26 16:36:00 -0400353 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400354 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Bob Moore83135242006-10-03 00:00:00 -0400357ACPI_EXPORT_SYMBOL(acpi_evaluate_object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*******************************************************************************
360 *
361 * FUNCTION: acpi_walk_namespace
362 *
363 * PARAMETERS: Type - acpi_object_type to search for
364 * start_object - Handle in namespace where search begins
365 * max_depth - Depth to which search is to reach
366 * user_function - Called when an object of "Type" is found
367 * Context - Passed to user function
368 * return_value - Location where return value of
369 * user_function is put if terminated early
370 *
371 * RETURNS Return value from the user_function if terminated early.
372 * Otherwise, returns NULL.
373 *
374 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
375 * starting (and ending) at the object specified by start_handle.
376 * The user_function is called whenever an object that matches
377 * the type parameter is found. If the user function returns
378 * a non-zero value, the search is terminated immediately and this
379 * value is returned to the caller.
380 *
381 * The point of this procedure is to provide a generic namespace
382 * walk routine that can be called from multiple places to
383 * provide multiple services; the User Function can be tailored
384 * to each task, whether it is a print function, a compare
385 * function, etc.
386 *
387 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400389acpi_walk_namespace(acpi_object_type type,
390 acpi_handle start_object,
391 u32 max_depth,
392 acpi_walk_callback user_function,
393 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Len Brown4be44fc2005-08-05 00:44:28 -0400395 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Bob Mooreb229cf92006-04-21 17:15:00 -0400397 ACPI_FUNCTION_TRACE(acpi_walk_namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Parameter validation */
400
Bob Moore96db2552005-11-02 00:00:00 -0500401 if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400402 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 /*
406 * Lock the namespace around the walk.
407 * The namespace will be unlocked/locked around each call
408 * to the user function - since this function
409 * must be allowed to make Acpi calls itself.
410 */
Len Brown4be44fc2005-08-05 00:44:28 -0400411 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
412 if (ACPI_FAILURE(status)) {
413 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 status = acpi_ns_walk_namespace(type, start_object, max_depth,
417 ACPI_NS_WALK_UNLOCK,
418 user_function, context, return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Len Brown4be44fc2005-08-05 00:44:28 -0400420 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
421 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Bob Moore83135242006-10-03 00:00:00 -0400424ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426/*******************************************************************************
427 *
428 * FUNCTION: acpi_ns_get_device_callback
429 *
430 * PARAMETERS: Callback from acpi_get_device
431 *
432 * RETURN: Status
433 *
434 * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
435 * present devices, or if they specified a HID, it filters based
436 * on that.
437 *
438 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400440acpi_ns_get_device_callback(acpi_handle obj_handle,
441 u32 nesting_level,
442 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Len Brown4be44fc2005-08-05 00:44:28 -0400444 struct acpi_get_devices_info *info = context;
445 acpi_status status;
446 struct acpi_namespace_node *node;
447 u32 flags;
448 struct acpi_device_id hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct acpi_compatible_id_list *cid;
Len Brown4be44fc2005-08-05 00:44:28 -0400450 acpi_native_uint i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Len Brown4be44fc2005-08-05 00:44:28 -0400452 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
453 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return (status);
455 }
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457 node = acpi_ns_map_handle_to_node(obj_handle);
458 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
459 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return (status);
461 }
462
463 if (!node) {
464 return (AE_BAD_PARAMETER);
465 }
466
467 /* Run _STA to determine if device is present */
468
Len Brown4be44fc2005-08-05 00:44:28 -0400469 status = acpi_ut_execute_STA(node, &flags);
470 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return (AE_CTRL_DEPTH);
472 }
473
Bob Mooredefba1d2005-12-16 17:05:00 -0500474 if (!(flags & ACPI_STA_DEVICE_PRESENT)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400475
Bob Mooredefba1d2005-12-16 17:05:00 -0500476 /* Don't examine children of the device if not present */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 return (AE_CTRL_DEPTH);
479 }
480
481 /* Filter based on device HID & CID */
482
483 if (info->hid != NULL) {
Len Brown4be44fc2005-08-05 00:44:28 -0400484 status = acpi_ut_execute_HID(node, &hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (status == AE_NOT_FOUND) {
486 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400487 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return (AE_CTRL_DEPTH);
489 }
490
Len Brown4be44fc2005-08-05 00:44:28 -0400491 if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Get the list of Compatible IDs */
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495 status = acpi_ut_execute_CID(node, &cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (status == AE_NOT_FOUND) {
497 return (AE_OK);
Len Brown4be44fc2005-08-05 00:44:28 -0400498 } else if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return (AE_CTRL_DEPTH);
500 }
501
502 /* Walk the CID list */
503
504 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
507 acpi_compatible_id)) !=
508 0) {
Bob Moore83135242006-10-03 00:00:00 -0400509 ACPI_FREE(cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return (AE_OK);
511 }
512 }
Bob Moore83135242006-10-03 00:00:00 -0400513 ACPI_FREE(cid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 }
516
Len Brown4be44fc2005-08-05 00:44:28 -0400517 status = info->user_function(obj_handle, nesting_level, info->context,
518 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return (status);
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*******************************************************************************
523 *
524 * FUNCTION: acpi_get_devices
525 *
526 * PARAMETERS: HID - HID to search for. Can be NULL.
527 * user_function - Called when a matching object is found
528 * Context - Passed to user function
529 * return_value - Location where return value of
530 * user_function is put if terminated early
531 *
532 * RETURNS Return value from the user_function if terminated early.
533 * Otherwise, returns NULL.
534 *
535 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
536 * starting (and ending) at the object specified by start_handle.
537 * The user_function is called whenever an object of type
538 * Device is found. If the user function returns
539 * a non-zero value, the search is terminated immediately and this
540 * value is returned to the caller.
541 *
542 * This is a wrapper for walk_namespace, but the callback performs
543 * additional filtering. Please see acpi_get_device_callback.
544 *
545 ******************************************************************************/
546
547acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400548acpi_get_devices(char *HID,
549 acpi_walk_callback user_function,
550 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Len Brown4be44fc2005-08-05 00:44:28 -0400552 acpi_status status;
553 struct acpi_get_devices_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Bob Mooreb229cf92006-04-21 17:15:00 -0400555 ACPI_FUNCTION_TRACE(acpi_get_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Parameter validation */
558
559 if (!user_function) {
Len Brown4be44fc2005-08-05 00:44:28 -0400560 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563 /*
564 * We're going to call their callback from OUR callback, so we need
565 * to know what it is, and their context parameter.
566 */
Bob Moore52fc0b02006-10-02 00:00:00 -0400567 info.hid = HID;
Len Brown4be44fc2005-08-05 00:44:28 -0400568 info.context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 info.user_function = user_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /*
572 * Lock the namespace around the walk.
573 * The namespace will be unlocked/locked around each call
574 * to the user function - since this function
575 * must be allowed to make Acpi calls itself.
576 */
Len Brown4be44fc2005-08-05 00:44:28 -0400577 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
578 if (ACPI_FAILURE(status)) {
579 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Bob Moore52fc0b02006-10-02 00:00:00 -0400582 status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
583 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
Len Brown4be44fc2005-08-05 00:44:28 -0400584 acpi_ns_get_device_callback, &info,
585 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Len Brown4be44fc2005-08-05 00:44:28 -0400587 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
588 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Bob Moore83135242006-10-03 00:00:00 -0400591ACPI_EXPORT_SYMBOL(acpi_get_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/*******************************************************************************
594 *
595 * FUNCTION: acpi_attach_data
596 *
597 * PARAMETERS: obj_handle - Namespace node
598 * Handler - Handler for this attachment
599 * Data - Pointer to data to be attached
600 *
601 * RETURN: Status
602 *
603 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
604 *
605 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400607acpi_attach_data(acpi_handle obj_handle,
608 acpi_object_handler handler, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Len Brown4be44fc2005-08-05 00:44:28 -0400610 struct acpi_namespace_node *node;
611 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* Parameter validation */
614
Len Brown4be44fc2005-08-05 00:44:28 -0400615 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return (AE_BAD_PARAMETER);
617 }
618
Len Brown4be44fc2005-08-05 00:44:28 -0400619 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
620 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return (status);
622 }
623
624 /* Convert and validate the handle */
625
Len Brown4be44fc2005-08-05 00:44:28 -0400626 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!node) {
628 status = AE_BAD_PARAMETER;
629 goto unlock_and_exit;
630 }
631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 status = acpi_ns_attach_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Len Brown4be44fc2005-08-05 00:44:28 -0400634 unlock_and_exit:
635 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return (status);
637}
638
Bob Moore83135242006-10-03 00:00:00 -0400639ACPI_EXPORT_SYMBOL(acpi_attach_data)
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*******************************************************************************
642 *
643 * FUNCTION: acpi_detach_data
644 *
645 * PARAMETERS: obj_handle - Namespace node handle
646 * Handler - Handler used in call to acpi_attach_data
647 *
648 * RETURN: Status
649 *
650 * DESCRIPTION: Remove data that was previously attached to a node.
651 *
652 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400654acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Len Brown4be44fc2005-08-05 00:44:28 -0400656 struct acpi_namespace_node *node;
657 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Parameter validation */
660
Len Brown4be44fc2005-08-05 00:44:28 -0400661 if (!obj_handle || !handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return (AE_BAD_PARAMETER);
663 }
664
Len Brown4be44fc2005-08-05 00:44:28 -0400665 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
666 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return (status);
668 }
669
670 /* Convert and validate the handle */
671
Len Brown4be44fc2005-08-05 00:44:28 -0400672 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!node) {
674 status = AE_BAD_PARAMETER;
675 goto unlock_and_exit;
676 }
677
Len Brown4be44fc2005-08-05 00:44:28 -0400678 status = acpi_ns_detach_data(node, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 unlock_and_exit:
681 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return (status);
683}
684
Bob Moore83135242006-10-03 00:00:00 -0400685ACPI_EXPORT_SYMBOL(acpi_detach_data)
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/*******************************************************************************
688 *
689 * FUNCTION: acpi_get_data
690 *
691 * PARAMETERS: obj_handle - Namespace node
692 * Handler - Handler used in call to attach_data
693 * Data - Where the data is returned
694 *
695 * RETURN: Status
696 *
697 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
698 *
699 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400701acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Len Brown4be44fc2005-08-05 00:44:28 -0400703 struct acpi_namespace_node *node;
704 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* Parameter validation */
707
Len Brown4be44fc2005-08-05 00:44:28 -0400708 if (!obj_handle || !handler || !data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return (AE_BAD_PARAMETER);
710 }
711
Len Brown4be44fc2005-08-05 00:44:28 -0400712 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
713 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return (status);
715 }
716
717 /* Convert and validate the handle */
718
Len Brown4be44fc2005-08-05 00:44:28 -0400719 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!node) {
721 status = AE_BAD_PARAMETER;
722 goto unlock_and_exit;
723 }
724
Len Brown4be44fc2005-08-05 00:44:28 -0400725 status = acpi_ns_get_attached_data(node, handler, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Len Brown4be44fc2005-08-05 00:44:28 -0400727 unlock_and_exit:
728 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return (status);
730}
Bob Moore83135242006-10-03 00:00:00 -0400731
732ACPI_EXPORT_SYMBOL(acpi_get_data)