Merge with 2.4.0-test3-pre4.
[linux-2.6/linux-mips.git] / drivers / acpi / namespace / nseval.c
blobea3e15621007e5e3a9af8df189b32f45604752bb
2 /******************************************************************************
4 * Module Name: nseval - Object evaluation interfaces -- includes control
5 * method lookup and execution.
7 *****************************************************************************/
9 /*
10 * Copyright (C) 2000 R. Byron Moore
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "acpi.h"
29 #include "amlcode.h"
30 #include "parser.h"
31 #include "interp.h"
32 #include "namesp.h"
35 #define _COMPONENT NAMESPACE
36 MODULE_NAME ("nseval");
39 /****************************************************************************
41 * FUNCTION: Acpi_ns_evaluate_relative
43 * PARAMETERS: Rel_obj_entry - NTE of the relative containing object
44 * *Pathname - Name of method to execute, If NULL, the
45 * handle is the object to execute
46 * **Params - List of parameters to pass to the method,
47 * terminated by NULL. Params itself may be
48 * NULL if no parameters are being passed.
49 * *Return_object - Where to put method's return value (if
50 * any). If NULL, no value is returned.
52 * RETURN: Status
54 * DESCRIPTION: Find and execute the requested method using the handle as a
55 * scope
57 * MUTEX: Locks Namespace
59 ****************************************************************************/
61 ACPI_STATUS
62 acpi_ns_evaluate_relative (
63 ACPI_NAMED_OBJECT *handle,
64 char *pathname,
65 ACPI_OBJECT_INTERNAL **params,
66 ACPI_OBJECT_INTERNAL **return_object)
68 ACPI_NAMED_OBJECT *rel_obj_entry;
69 ACPI_STATUS status;
70 ACPI_NAMED_OBJECT *obj_entry = NULL;
71 char *internal_path = NULL;
72 ACPI_GENERIC_STATE scope_info;
76 * Must have a valid object handle
78 if (!handle) {
79 return (AE_BAD_PARAMETER);
82 /* Build an internal name string for the method */
84 status = acpi_ns_internalize_name (pathname, &internal_path);
85 if (ACPI_FAILURE (status)) {
86 return (status);
89 /* Get the prefix handle and NTE */
91 acpi_cm_acquire_mutex (ACPI_MTX_NAMESPACE);
93 rel_obj_entry = acpi_ns_convert_handle_to_entry (handle);
94 if (!rel_obj_entry) {
95 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
96 status = AE_BAD_PARAMETER;
97 goto cleanup;
100 /* Lookup the name in the namespace */
102 scope_info.scope.name_table = rel_obj_entry->child_table;
103 status = acpi_ns_lookup (&scope_info, internal_path, ACPI_TYPE_ANY,
104 IMODE_EXECUTE,
105 NS_NO_UPSEARCH, NULL,
106 &obj_entry);
108 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
110 if (status != AE_OK) {
111 goto cleanup;
115 * Now that we have a handle to the object, we can attempt
116 * to evaluate it.
119 status = acpi_ns_evaluate_by_handle (obj_entry, params, return_object);
121 cleanup:
123 /* Cleanup */
125 acpi_cm_free (internal_path);
127 return (status);
131 /****************************************************************************
133 * FUNCTION: Acpi_ns_evaluate_by_name
135 * PARAMETERS: Pathname - Fully qualified pathname to the object
136 * *Return_object - Where to put method's return value (if
137 * any). If NULL, no value is returned.
138 * **Params - List of parameters to pass to the method,
139 * terminated by NULL. Params itself may be
140 * NULL if no parameters are being passed.
142 * RETURN: Status
144 * DESCRIPTION: Find and execute the requested method passing the given
145 * parameters
147 * MUTEX: Locks Namespace
149 ****************************************************************************/
151 ACPI_STATUS
152 acpi_ns_evaluate_by_name (
153 char *pathname,
154 ACPI_OBJECT_INTERNAL **params,
155 ACPI_OBJECT_INTERNAL **return_object)
157 ACPI_STATUS status;
158 ACPI_NAMED_OBJECT *obj_entry = NULL;
159 char *internal_path = NULL;
162 /* Build an internal name string for the method */
164 if (pathname[0] != '\\' || pathname[1] != '/') {
165 status = acpi_ns_internalize_name (pathname, &internal_path);
166 if (ACPI_FAILURE (status)) {
167 return (status);
171 acpi_cm_acquire_mutex (ACPI_MTX_NAMESPACE);
173 /* Lookup the name in the namespace */
175 status = acpi_ns_lookup (NULL, internal_path, ACPI_TYPE_ANY,
176 IMODE_EXECUTE,
177 NS_NO_UPSEARCH, NULL,
178 &obj_entry);
180 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
182 if (status != AE_OK) {
183 goto cleanup;
187 * Now that we have a handle to the object, we can attempt
188 * to evaluate it.
191 status = acpi_ns_evaluate_by_handle (obj_entry, params, return_object);
194 cleanup:
196 /* Cleanup */
198 if (internal_path) {
199 acpi_cm_free (internal_path);
202 return (status);
206 /****************************************************************************
208 * FUNCTION: Acpi_ns_evaluate_by_handle
210 * PARAMETERS: Obj_entry - NTE of method to execute
211 * *Return_object - Where to put method's return value (if
212 * any). If NULL, no value is returned.
213 * **Params - List of parameters to pass to the method,
214 * terminated by NULL. Params itself may be
215 * NULL if no parameters are being passed.
217 * RETURN: Status
219 * DESCRIPTION: Execute the requested method passing the given parameters
221 * MUTEX: Locks Namespace
223 ****************************************************************************/
225 ACPI_STATUS
226 acpi_ns_evaluate_by_handle (
227 ACPI_NAMED_OBJECT *handle,
228 ACPI_OBJECT_INTERNAL **params,
229 ACPI_OBJECT_INTERNAL **return_object)
231 ACPI_NAMED_OBJECT *obj_entry;
232 ACPI_STATUS status;
233 ACPI_OBJECT_INTERNAL *local_return_object;
236 /* Check if namespace has been initialized */
238 if (!acpi_gbl_root_object->child_table) {
239 return (AE_NO_NAMESPACE);
242 /* Parameter Validation */
244 if (!handle) {
245 return (AE_BAD_PARAMETER);
248 if (return_object) {
249 /* Initialize the return value to an invalid object */
251 *return_object = NULL;
254 /* Get the prefix handle and NTE */
256 acpi_cm_acquire_mutex (ACPI_MTX_NAMESPACE);
258 obj_entry = acpi_ns_convert_handle_to_entry (handle);
259 if (!obj_entry) {
260 status = AE_BAD_PARAMETER;
261 goto unlock_and_exit;
266 * Two major cases here:
267 * 1) The object is an actual control method -- execute it.
268 * 2) The object is not a method -- just return it's current
269 * value
271 * In both cases, the namespace is unlocked by the
272 * Acpi_ns* procedure
275 if (acpi_ns_get_type (obj_entry) == ACPI_TYPE_METHOD) {
277 * Case 1) We have an actual control method to execute
280 status = acpi_ns_execute_control_method (obj_entry,
281 params,
282 &local_return_object);
285 else {
287 * Case 2) Object is NOT a method, just return its
288 * current value
291 status = acpi_ns_get_object_value (obj_entry,
292 &local_return_object);
297 * Check if there is a return value on the stack that must
298 * be dealt with
301 if (status == AE_CTRL_RETURN_VALUE) {
303 * If the Method returned a value and the caller
304 * provided a place to store a returned value, Copy
305 * the returned value to the object descriptor provided
306 * by the caller.
309 if (return_object) {
311 * Valid return object, copy the pointer to
312 * the returned object
315 *return_object = local_return_object;
319 /* Map AE_RETURN_VALUE to AE_OK, we are done with it */
321 if (status == AE_CTRL_RETURN_VALUE) {
322 status = AE_OK;
327 * Namespace was unlocked by the handling Acpi_ns* function,
328 * so we just return
331 return (status);
334 unlock_and_exit:
336 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
337 return (status);
341 /****************************************************************************
343 * FUNCTION: Acpi_ns_execute_control_method
345 * PARAMETERS: Method_entry - The Nte of the object/method
346 * **Params - List of parameters to pass to the method,
347 * terminated by NULL. Params itself may be
348 * NULL if no parameters are being passed.
350 * RETURN: Status
352 * DESCRIPTION: Execute the requested method passing the given parameters
354 * MUTEX: Assumes namespace is locked
356 ****************************************************************************/
358 ACPI_STATUS
359 acpi_ns_execute_control_method (
360 ACPI_NAMED_OBJECT *method_entry,
361 ACPI_OBJECT_INTERNAL **params,
362 ACPI_OBJECT_INTERNAL **return_obj_desc)
364 ACPI_STATUS status;
365 ACPI_OBJECT_INTERNAL *obj_desc;
368 /* Verify that there is a method associated with this object */
370 obj_desc = acpi_ns_get_attached_object ((ACPI_HANDLE) method_entry);
371 if (!obj_desc) {
372 return (AE_ERROR);
376 * Valid method, Set the current scope to that of the Method,
377 * and execute it.
382 * Unlock the namespace before execution. This allows namespace access
383 * via the external Acpi* interfaces while a method is being executed.
384 * However, any namespace deletion must acquire both the namespace and
385 * interpter locks to ensure that no thread is using the portion of the
386 * namespace that is being deleted.
389 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
392 * Excecute the method via the interpreter
394 status = acpi_aml_execute_method (method_entry, params, return_obj_desc);
396 return (status);
400 /****************************************************************************
402 * FUNCTION: Acpi_ns_get_object_value
404 * PARAMETERS: Object_entry - The Nte of the object
406 * RETURN: Status
408 * DESCRIPTION: Return the current value of the object
410 * MUTEX: Assumes namespace is locked
412 ****************************************************************************/
414 ACPI_STATUS
415 acpi_ns_get_object_value (
416 ACPI_NAMED_OBJECT *object_entry,
417 ACPI_OBJECT_INTERNAL **return_obj_desc)
419 ACPI_STATUS status = AE_OK;
420 ACPI_OBJECT_INTERNAL *obj_desc;
421 ACPI_OBJECT_INTERNAL *val_desc;
425 * We take the value from certain objects directly
428 if ((object_entry->type == ACPI_TYPE_PROCESSOR) ||
429 (object_entry->type == ACPI_TYPE_POWER))
433 * Create a Reference object to contain the object
435 obj_desc = acpi_cm_create_internal_object (object_entry->type);
436 if (!obj_desc) {
437 status = AE_NO_MEMORY;
438 goto unlock_and_exit;
442 * Get the attached object
445 val_desc = acpi_ns_get_attached_object (object_entry);
446 if (!val_desc) {
447 status = AE_NULL_OBJECT;
448 goto unlock_and_exit;
452 * Just copy from the original to the return object
455 MEMCPY (&obj_desc->common.first_non_common_byte,
456 &val_desc->common.first_non_common_byte,
457 (sizeof(ACPI_OBJECT_COMMON) -
458 sizeof(obj_desc->common.first_non_common_byte)));
463 * Other objects require a reference object wrapper which we
464 * then attempt to resolve.
466 else {
467 /* Create an Reference object to contain the object */
469 obj_desc = acpi_cm_create_internal_object (INTERNAL_TYPE_REFERENCE);
470 if (!obj_desc) {
471 status = AE_NO_MEMORY;
472 goto unlock_and_exit;
475 /* Construct a descriptor pointing to the name */
477 obj_desc->reference.op_code = (u8) AML_NAME_OP;
478 obj_desc->reference.object = (void *) object_entry;
481 * Use Acpi_aml_resolve_to_value() to get the associated value.
482 * The call to Acpi_aml_resolve_to_value causes
483 * Obj_desc (allocated above) to always be deleted.
486 status = acpi_aml_resolve_to_value (&obj_desc);
490 * If Acpi_aml_resolve_to_value() succeeded, the return value was
491 * placed in Obj_desc.
494 if (status == AE_OK) {
495 status = AE_CTRL_RETURN_VALUE;
497 *return_obj_desc = obj_desc;
501 unlock_and_exit:
503 /* Unlock the namespace */
505 acpi_cm_release_mutex (ACPI_MTX_NAMESPACE);
506 return (status);