initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / acpi / namespace / nsobject.c
blob6eb3e86b82ef95832cc55af5818f2908b341b54f
1 /*******************************************************************************
3 * Module Name: nsobject - Utilities for objects attached to namespace
4 * table entries
6 ******************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2004, R. Byron Moore
10 * All rights reserved.
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.
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.
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.
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsobject")
54 /*******************************************************************************
56 * FUNCTION: acpi_ns_attach_object
58 * PARAMETERS: Node - Parent Node
59 * Object - Object to be attached
60 * Type - Type of object, or ACPI_TYPE_ANY if not
61 * known
63 * DESCRIPTION: Record the given object as the value associated with the
64 * name whose acpi_handle is passed. If Object is NULL
65 * and Type is ACPI_TYPE_ANY, set the name as having no value.
66 * Note: Future may require that the Node->Flags field be passed
67 * as a parameter.
69 * MUTEX: Assumes namespace is locked
71 ******************************************************************************/
73 acpi_status
74 acpi_ns_attach_object (
75 struct acpi_namespace_node *node,
76 union acpi_operand_object *object,
77 acpi_object_type type)
79 union acpi_operand_object *obj_desc;
80 union acpi_operand_object *last_obj_desc;
81 acpi_object_type object_type = ACPI_TYPE_ANY;
84 ACPI_FUNCTION_TRACE ("ns_attach_object");
88 * Parameter validation
90 if (!node) {
91 /* Invalid handle */
93 ACPI_REPORT_ERROR (("ns_attach_object: Null named_obj handle\n"));
94 return_ACPI_STATUS (AE_BAD_PARAMETER);
97 if (!object && (ACPI_TYPE_ANY != type)) {
98 /* Null object */
100 ACPI_REPORT_ERROR (("ns_attach_object: Null object, but type not ACPI_TYPE_ANY\n"));
101 return_ACPI_STATUS (AE_BAD_PARAMETER);
104 if (ACPI_GET_DESCRIPTOR_TYPE (node) != ACPI_DESC_TYPE_NAMED) {
105 /* Not a name handle */
107 ACPI_REPORT_ERROR (("ns_attach_object: Invalid handle %p [%s]\n",
108 node, acpi_ut_get_descriptor_name (node)));
109 return_ACPI_STATUS (AE_BAD_PARAMETER);
112 /* Check if this object is already attached */
114 if (node->object == object) {
115 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Obj %p already installed in name_obj %p\n",
116 object, node));
118 return_ACPI_STATUS (AE_OK);
121 /* If null object, we will just install it */
123 if (!object) {
124 obj_desc = NULL;
125 object_type = ACPI_TYPE_ANY;
129 * If the source object is a namespace Node with an attached object,
130 * we will use that (attached) object
132 else if ((ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) &&
133 ((struct acpi_namespace_node *) object)->object) {
135 * Value passed is a name handle and that name has a
136 * non-null value. Use that name's value and type.
138 obj_desc = ((struct acpi_namespace_node *) object)->object;
139 object_type = ((struct acpi_namespace_node *) object)->type;
143 * Otherwise, we will use the parameter object, but we must type
144 * it first
146 else {
147 obj_desc = (union acpi_operand_object *) object;
149 /* Use the given type */
151 object_type = type;
154 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
155 obj_desc, node, acpi_ut_get_node_name (node)));
157 /* Detach an existing attached object if present */
159 if (node->object) {
160 acpi_ns_detach_object (node);
163 if (obj_desc) {
165 * Must increment the new value's reference count
166 * (if it is an internal object)
168 acpi_ut_add_reference (obj_desc);
171 * Handle objects with multiple descriptors - walk
172 * to the end of the descriptor list
174 last_obj_desc = obj_desc;
175 while (last_obj_desc->common.next_object) {
176 last_obj_desc = last_obj_desc->common.next_object;
179 /* Install the object at the front of the object list */
181 last_obj_desc->common.next_object = node->object;
184 node->type = (u8) object_type;
185 node->object = obj_desc;
187 return_ACPI_STATUS (AE_OK);
191 /*******************************************************************************
193 * FUNCTION: acpi_ns_detach_object
195 * PARAMETERS: Node - An node whose object will be detached
197 * RETURN: None.
199 * DESCRIPTION: Detach/delete an object associated with a namespace node.
200 * if the object is an allocated object, it is freed.
201 * Otherwise, the field is simply cleared.
203 ******************************************************************************/
205 void
206 acpi_ns_detach_object (
207 struct acpi_namespace_node *node)
209 union acpi_operand_object *obj_desc;
212 ACPI_FUNCTION_TRACE ("ns_detach_object");
215 obj_desc = node->object;
217 if (!obj_desc ||
218 (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA)) {
219 return_VOID;
222 /* Clear the entry in all cases */
224 node->object = NULL;
225 if (ACPI_GET_DESCRIPTOR_TYPE (obj_desc) == ACPI_DESC_TYPE_OPERAND) {
226 node->object = obj_desc->common.next_object;
227 if (node->object &&
228 (ACPI_GET_OBJECT_TYPE (node->object) != ACPI_TYPE_LOCAL_DATA)) {
229 node->object = node->object->common.next_object;
233 /* Reset the node type to untyped */
235 node->type = ACPI_TYPE_ANY;
237 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
238 node, acpi_ut_get_node_name (node), obj_desc));
240 /* Remove one reference on the object (and all subobjects) */
242 acpi_ut_remove_reference (obj_desc);
243 return_VOID;
247 /*******************************************************************************
249 * FUNCTION: acpi_ns_get_attached_object
251 * PARAMETERS: Node - Parent Node to be examined
253 * RETURN: Current value of the object field from the Node whose
254 * handle is passed
256 * DESCRIPTION: Obtain the object attached to a namespace node.
258 ******************************************************************************/
260 union acpi_operand_object *
261 acpi_ns_get_attached_object (
262 struct acpi_namespace_node *node)
264 ACPI_FUNCTION_TRACE_PTR ("ns_get_attached_object", node);
267 if (!node) {
268 ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Null Node ptr\n"));
269 return_PTR (NULL);
272 if (!node->object ||
273 ((ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_OPERAND) &&
274 (ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_NAMED)) ||
275 (ACPI_GET_OBJECT_TYPE (node->object) == ACPI_TYPE_LOCAL_DATA)) {
276 return_PTR (NULL);
279 return_PTR (node->object);
283 /*******************************************************************************
285 * FUNCTION: acpi_ns_get_secondary_object
287 * PARAMETERS: Node - Parent Node to be examined
289 * RETURN: Current value of the object field from the Node whose
290 * handle is passed.
292 * DESCRIPTION: Obtain a secondary object associated with a namespace node.
294 ******************************************************************************/
296 union acpi_operand_object *
297 acpi_ns_get_secondary_object (
298 union acpi_operand_object *obj_desc)
300 ACPI_FUNCTION_TRACE_PTR ("ns_get_secondary_object", obj_desc);
303 if ((!obj_desc) ||
304 (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) ||
305 (!obj_desc->common.next_object) ||
306 (ACPI_GET_OBJECT_TYPE (obj_desc->common.next_object) == ACPI_TYPE_LOCAL_DATA)) {
307 return_PTR (NULL);
310 return_PTR (obj_desc->common.next_object);
314 /*******************************************************************************
316 * FUNCTION: acpi_ns_attach_data
318 * PARAMETERS: Node - Namespace node
319 * Handler - Handler to be associated with the data
320 * Data - Data to be attached
322 * RETURN: Status
324 * DESCRIPTION: Low-level attach data. Create and attach a Data object.
326 ******************************************************************************/
328 acpi_status
329 acpi_ns_attach_data (
330 struct acpi_namespace_node *node,
331 acpi_object_handler handler,
332 void *data)
334 union acpi_operand_object *prev_obj_desc;
335 union acpi_operand_object *obj_desc;
336 union acpi_operand_object *data_desc;
339 /* We only allow one attachment per handler */
341 prev_obj_desc = NULL;
342 obj_desc = node->object;
343 while (obj_desc) {
344 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
345 (obj_desc->data.handler == handler)) {
346 return (AE_ALREADY_EXISTS);
349 prev_obj_desc = obj_desc;
350 obj_desc = obj_desc->common.next_object;
353 /* Create an internal object for the data */
355 data_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_DATA);
356 if (!data_desc) {
357 return (AE_NO_MEMORY);
360 data_desc->data.handler = handler;
361 data_desc->data.pointer = data;
363 /* Install the data object */
365 if (prev_obj_desc) {
366 prev_obj_desc->common.next_object = data_desc;
368 else {
369 node->object = data_desc;
372 return (AE_OK);
376 /*******************************************************************************
378 * FUNCTION: acpi_ns_detach_data
380 * PARAMETERS: Node - Namespace node
381 * Handler - Handler associated with the data
383 * RETURN: Status
385 * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
386 * is responsible for the actual data.
388 ******************************************************************************/
390 acpi_status
391 acpi_ns_detach_data (
392 struct acpi_namespace_node *node,
393 acpi_object_handler handler)
395 union acpi_operand_object *obj_desc;
396 union acpi_operand_object *prev_obj_desc;
399 prev_obj_desc = NULL;
400 obj_desc = node->object;
401 while (obj_desc) {
402 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
403 (obj_desc->data.handler == handler)) {
404 if (prev_obj_desc) {
405 prev_obj_desc->common.next_object = obj_desc->common.next_object;
407 else {
408 node->object = obj_desc->common.next_object;
411 acpi_ut_remove_reference (obj_desc);
412 return (AE_OK);
415 prev_obj_desc = obj_desc;
416 obj_desc = obj_desc->common.next_object;
419 return (AE_NOT_FOUND);
423 /*******************************************************************************
425 * FUNCTION: acpi_ns_get_attached_data
427 * PARAMETERS: Node - Namespace node
428 * Handler - Handler associated with the data
429 * Data - Where the data is returned
431 * RETURN: Status
433 * DESCRIPTION: Low level interface to obtain data previously associated with
434 * a namespace node.
436 ******************************************************************************/
438 acpi_status
439 acpi_ns_get_attached_data (
440 struct acpi_namespace_node *node,
441 acpi_object_handler handler,
442 void **data)
444 union acpi_operand_object *obj_desc;
447 obj_desc = node->object;
448 while (obj_desc) {
449 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
450 (obj_desc->data.handler == handler)) {
451 *data = obj_desc->data.pointer;
452 return (AE_OK);
455 obj_desc = obj_desc->common.next_object;
458 return (AE_NOT_FOUND);