1 /*******************************************************************************
3 * Module Name: nsobject - Utilities for objects attached to namespace
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2005, 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
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.
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
65 * DESCRIPTION: Record the given object as the value associated with the
66 * name whose acpi_handle is passed. If Object is NULL
67 * and Type is ACPI_TYPE_ANY, set the name as having no value.
68 * Note: Future may require that the Node->Flags field be passed
71 * MUTEX: Assumes namespace is locked
73 ******************************************************************************/
76 acpi_ns_attach_object (
77 struct acpi_namespace_node *node,
78 union acpi_operand_object *object,
79 acpi_object_type type)
81 union acpi_operand_object *obj_desc;
82 union acpi_operand_object *last_obj_desc;
83 acpi_object_type object_type = ACPI_TYPE_ANY;
86 ACPI_FUNCTION_TRACE ("ns_attach_object");
90 * Parameter validation
95 ACPI_REPORT_ERROR (("ns_attach_object: Null named_obj handle\n"));
96 return_ACPI_STATUS (AE_BAD_PARAMETER);
99 if (!object && (ACPI_TYPE_ANY != type)) {
103 "ns_attach_object: Null object, but type not ACPI_TYPE_ANY\n"));
104 return_ACPI_STATUS (AE_BAD_PARAMETER);
107 if (ACPI_GET_DESCRIPTOR_TYPE (node) != ACPI_DESC_TYPE_NAMED) {
108 /* Not a name handle */
110 ACPI_REPORT_ERROR (("ns_attach_object: Invalid handle %p [%s]\n",
111 node, acpi_ut_get_descriptor_name (node)));
112 return_ACPI_STATUS (AE_BAD_PARAMETER);
115 /* Check if this object is already attached */
117 if (node->object == object) {
118 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
119 "Obj %p already installed in name_obj %p\n",
122 return_ACPI_STATUS (AE_OK);
125 /* If null object, we will just install it */
129 object_type = ACPI_TYPE_ANY;
133 * If the source object is a namespace Node with an attached object,
134 * we will use that (attached) object
136 else if ((ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) &&
137 ((struct acpi_namespace_node *) object)->object) {
139 * Value passed is a name handle and that name has a
140 * non-null value. Use that name's value and type.
142 obj_desc = ((struct acpi_namespace_node *) object)->object;
143 object_type = ((struct acpi_namespace_node *) object)->type;
147 * Otherwise, we will use the parameter object, but we must type
151 obj_desc = (union acpi_operand_object *) object;
153 /* Use the given type */
158 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
159 obj_desc, node, acpi_ut_get_node_name (node)));
161 /* Detach an existing attached object if present */
164 acpi_ns_detach_object (node);
169 * Must increment the new value's reference count
170 * (if it is an internal object)
172 acpi_ut_add_reference (obj_desc);
175 * Handle objects with multiple descriptors - walk
176 * to the end of the descriptor list
178 last_obj_desc = obj_desc;
179 while (last_obj_desc->common.next_object) {
180 last_obj_desc = last_obj_desc->common.next_object;
183 /* Install the object at the front of the object list */
185 last_obj_desc->common.next_object = node->object;
188 node->type = (u8) object_type;
189 node->object = obj_desc;
191 return_ACPI_STATUS (AE_OK);
195 /*******************************************************************************
197 * FUNCTION: acpi_ns_detach_object
199 * PARAMETERS: Node - A Namespace node whose object will be detached
203 * DESCRIPTION: Detach/delete an object associated with a namespace node.
204 * if the object is an allocated object, it is freed.
205 * Otherwise, the field is simply cleared.
207 ******************************************************************************/
210 acpi_ns_detach_object (
211 struct acpi_namespace_node *node)
213 union acpi_operand_object *obj_desc;
216 ACPI_FUNCTION_TRACE ("ns_detach_object");
219 obj_desc = node->object;
222 (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA)) {
226 /* Clear the entry in all cases */
229 if (ACPI_GET_DESCRIPTOR_TYPE (obj_desc) == ACPI_DESC_TYPE_OPERAND) {
230 node->object = obj_desc->common.next_object;
232 (ACPI_GET_OBJECT_TYPE (node->object) != ACPI_TYPE_LOCAL_DATA)) {
233 node->object = node->object->common.next_object;
237 /* Reset the node type to untyped */
239 node->type = ACPI_TYPE_ANY;
241 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
242 node, acpi_ut_get_node_name (node), obj_desc));
244 /* Remove one reference on the object (and all subobjects) */
246 acpi_ut_remove_reference (obj_desc);
251 /*******************************************************************************
253 * FUNCTION: acpi_ns_get_attached_object
255 * PARAMETERS: Node - Namespace node
257 * RETURN: Current value of the object field from the Node whose
260 * DESCRIPTION: Obtain the object attached to a namespace node.
262 ******************************************************************************/
264 union acpi_operand_object *
265 acpi_ns_get_attached_object (
266 struct acpi_namespace_node *node)
268 ACPI_FUNCTION_TRACE_PTR ("ns_get_attached_object", node);
272 ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Null Node ptr\n"));
277 ((ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_OPERAND) &&
278 (ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_NAMED)) ||
279 (ACPI_GET_OBJECT_TYPE (node->object) == ACPI_TYPE_LOCAL_DATA)) {
283 return_PTR (node->object);
287 /*******************************************************************************
289 * FUNCTION: acpi_ns_get_secondary_object
291 * PARAMETERS: Node - Namespace node
293 * RETURN: Current value of the object field from the Node whose
296 * DESCRIPTION: Obtain a secondary object associated with a namespace node.
298 ******************************************************************************/
300 union acpi_operand_object *
301 acpi_ns_get_secondary_object (
302 union acpi_operand_object *obj_desc)
304 ACPI_FUNCTION_TRACE_PTR ("ns_get_secondary_object", obj_desc);
308 (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) ||
309 (!obj_desc->common.next_object) ||
310 (ACPI_GET_OBJECT_TYPE (obj_desc->common.next_object) == ACPI_TYPE_LOCAL_DATA)) {
314 return_PTR (obj_desc->common.next_object);
318 /*******************************************************************************
320 * FUNCTION: acpi_ns_attach_data
322 * PARAMETERS: Node - Namespace node
323 * Handler - Handler to be associated with the data
324 * Data - Data to be attached
328 * DESCRIPTION: Low-level attach data. Create and attach a Data object.
330 ******************************************************************************/
333 acpi_ns_attach_data (
334 struct acpi_namespace_node *node,
335 acpi_object_handler handler,
338 union acpi_operand_object *prev_obj_desc;
339 union acpi_operand_object *obj_desc;
340 union acpi_operand_object *data_desc;
343 /* We only allow one attachment per handler */
345 prev_obj_desc = NULL;
346 obj_desc = node->object;
348 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
349 (obj_desc->data.handler == handler)) {
350 return (AE_ALREADY_EXISTS);
353 prev_obj_desc = obj_desc;
354 obj_desc = obj_desc->common.next_object;
357 /* Create an internal object for the data */
359 data_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_DATA);
361 return (AE_NO_MEMORY);
364 data_desc->data.handler = handler;
365 data_desc->data.pointer = data;
367 /* Install the data object */
370 prev_obj_desc->common.next_object = data_desc;
373 node->object = data_desc;
380 /*******************************************************************************
382 * FUNCTION: acpi_ns_detach_data
384 * PARAMETERS: Node - Namespace node
385 * Handler - Handler associated with the data
389 * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
390 * is responsible for the actual data.
392 ******************************************************************************/
395 acpi_ns_detach_data (
396 struct acpi_namespace_node *node,
397 acpi_object_handler handler)
399 union acpi_operand_object *obj_desc;
400 union acpi_operand_object *prev_obj_desc;
403 prev_obj_desc = NULL;
404 obj_desc = node->object;
406 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
407 (obj_desc->data.handler == handler)) {
409 prev_obj_desc->common.next_object = obj_desc->common.next_object;
412 node->object = obj_desc->common.next_object;
415 acpi_ut_remove_reference (obj_desc);
419 prev_obj_desc = obj_desc;
420 obj_desc = obj_desc->common.next_object;
423 return (AE_NOT_FOUND);
427 /*******************************************************************************
429 * FUNCTION: acpi_ns_get_attached_data
431 * PARAMETERS: Node - Namespace node
432 * Handler - Handler associated with the data
433 * Data - Where the data is returned
437 * DESCRIPTION: Low level interface to obtain data previously associated with
440 ******************************************************************************/
443 acpi_ns_get_attached_data (
444 struct acpi_namespace_node *node,
445 acpi_object_handler handler,
448 union acpi_operand_object *obj_desc;
451 obj_desc = node->object;
453 if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
454 (obj_desc->data.handler == handler)) {
455 *data = obj_desc->data.pointer;
459 obj_desc = obj_desc->common.next_object;
462 return (AE_NOT_FOUND);