1 /*******************************************************************************
3 * Module Name: nsobject - Utilities for objects attached to namespace
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2014, Intel Corp.
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.
45 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsobject")
52 /*******************************************************************************
54 * FUNCTION: acpi_ns_attach_object
56 * PARAMETERS: node - Parent Node
57 * object - Object to be attached
58 * type - Type of object, or ACPI_TYPE_ANY if not
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
69 * MUTEX: Assumes namespace is locked
71 ******************************************************************************/
73 acpi_ns_attach_object(struct acpi_namespace_node
*node
,
74 union acpi_operand_object
*object
, acpi_object_type type
)
76 union acpi_operand_object
*obj_desc
;
77 union acpi_operand_object
*last_obj_desc
;
78 acpi_object_type object_type
= ACPI_TYPE_ANY
;
80 ACPI_FUNCTION_TRACE(ns_attach_object
);
83 * Parameter validation
89 ACPI_ERROR((AE_INFO
, "Null NamedObj handle"));
90 return_ACPI_STATUS(AE_BAD_PARAMETER
);
93 if (!object
&& (ACPI_TYPE_ANY
!= type
)) {
98 "Null object, but type not ACPI_TYPE_ANY"));
99 return_ACPI_STATUS(AE_BAD_PARAMETER
);
102 if (ACPI_GET_DESCRIPTOR_TYPE(node
) != ACPI_DESC_TYPE_NAMED
) {
104 /* Not a name handle */
106 ACPI_ERROR((AE_INFO
, "Invalid handle %p [%s]",
107 node
, acpi_ut_get_descriptor_name(node
)));
108 return_ACPI_STATUS(AE_BAD_PARAMETER
);
111 /* Check if this object is already attached */
113 if (node
->object
== object
) {
114 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
115 "Obj %p already installed in NameObj %p\n",
118 return_ACPI_STATUS(AE_OK
);
121 /* If null object, we will just install it */
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
147 obj_desc
= (union acpi_operand_object
*)object
;
149 /* Use the given 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 */
160 acpi_ns_detach_object(node
);
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
);
190 /*******************************************************************************
192 * FUNCTION: acpi_ns_detach_object
194 * PARAMETERS: node - A Namespace node whose object will be detached
198 * DESCRIPTION: Detach/delete an object associated with a namespace node.
199 * if the object is an allocated object, it is freed.
200 * Otherwise, the field is simply cleared.
202 ******************************************************************************/
204 void acpi_ns_detach_object(struct acpi_namespace_node
*node
)
206 union acpi_operand_object
*obj_desc
;
208 ACPI_FUNCTION_TRACE(ns_detach_object
);
210 obj_desc
= node
->object
;
212 if (!obj_desc
|| (obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
)) {
216 if (node
->flags
& ANOBJ_ALLOCATED_BUFFER
) {
218 /* Free the dynamic aml buffer */
220 if (obj_desc
->common
.type
== ACPI_TYPE_METHOD
) {
221 ACPI_FREE(obj_desc
->method
.aml_start
);
225 /* Clear the Node entry in all cases */
228 if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc
) == ACPI_DESC_TYPE_OPERAND
) {
230 /* Unlink object from front of possible object list */
232 node
->object
= obj_desc
->common
.next_object
;
234 /* Handle possible 2-descriptor object */
237 (node
->object
->common
.type
!= ACPI_TYPE_LOCAL_DATA
)) {
238 node
->object
= node
->object
->common
.next_object
;
242 /* Reset the node type to untyped */
244 node
->type
= ACPI_TYPE_ANY
;
246 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
, "Node %p [%4.4s] Object %p\n",
247 node
, acpi_ut_get_node_name(node
), obj_desc
));
249 /* Remove one reference on the object (and all subobjects) */
251 acpi_ut_remove_reference(obj_desc
);
255 /*******************************************************************************
257 * FUNCTION: acpi_ns_get_attached_object
259 * PARAMETERS: node - Namespace node
261 * RETURN: Current value of the object field from the Node whose
264 * DESCRIPTION: Obtain the object attached to a namespace node.
266 ******************************************************************************/
268 union acpi_operand_object
*acpi_ns_get_attached_object(struct
272 ACPI_FUNCTION_TRACE_PTR(ns_get_attached_object
, node
);
275 ACPI_WARNING((AE_INFO
, "Null Node ptr"));
280 ((ACPI_GET_DESCRIPTOR_TYPE(node
->object
) != ACPI_DESC_TYPE_OPERAND
)
281 && (ACPI_GET_DESCRIPTOR_TYPE(node
->object
) !=
282 ACPI_DESC_TYPE_NAMED
))
283 || ((node
->object
)->common
.type
== ACPI_TYPE_LOCAL_DATA
)) {
287 return_PTR(node
->object
);
290 /*******************************************************************************
292 * FUNCTION: acpi_ns_get_secondary_object
294 * PARAMETERS: node - Namespace node
296 * RETURN: Current value of the object field from the Node whose
299 * DESCRIPTION: Obtain a secondary object associated with a namespace node.
301 ******************************************************************************/
303 union acpi_operand_object
*acpi_ns_get_secondary_object(union
307 ACPI_FUNCTION_TRACE_PTR(ns_get_secondary_object
, obj_desc
);
310 (obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
) ||
311 (!obj_desc
->common
.next_object
) ||
312 ((obj_desc
->common
.next_object
)->common
.type
==
313 ACPI_TYPE_LOCAL_DATA
)) {
317 return_PTR(obj_desc
->common
.next_object
);
320 /*******************************************************************************
322 * FUNCTION: acpi_ns_attach_data
324 * PARAMETERS: node - Namespace node
325 * handler - Handler to be associated with the data
326 * data - Data to be attached
330 * DESCRIPTION: Low-level attach data. Create and attach a Data object.
332 ******************************************************************************/
335 acpi_ns_attach_data(struct acpi_namespace_node
*node
,
336 acpi_object_handler handler
, void *data
)
338 union acpi_operand_object
*prev_obj_desc
;
339 union acpi_operand_object
*obj_desc
;
340 union acpi_operand_object
*data_desc
;
342 /* We only allow one attachment per handler */
344 prev_obj_desc
= NULL
;
345 obj_desc
= node
->object
;
347 if ((obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
) &&
348 (obj_desc
->data
.handler
== handler
)) {
349 return (AE_ALREADY_EXISTS
);
352 prev_obj_desc
= obj_desc
;
353 obj_desc
= obj_desc
->common
.next_object
;
356 /* Create an internal object for the data */
358 data_desc
= acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_DATA
);
360 return (AE_NO_MEMORY
);
363 data_desc
->data
.handler
= handler
;
364 data_desc
->data
.pointer
= data
;
366 /* Install the data object */
369 prev_obj_desc
->common
.next_object
= data_desc
;
371 node
->object
= data_desc
;
377 /*******************************************************************************
379 * FUNCTION: acpi_ns_detach_data
381 * PARAMETERS: node - Namespace node
382 * handler - Handler associated with the data
386 * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
387 * is responsible for the actual data.
389 ******************************************************************************/
392 acpi_ns_detach_data(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
;
398 prev_obj_desc
= NULL
;
399 obj_desc
= node
->object
;
401 if ((obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
) &&
402 (obj_desc
->data
.handler
== handler
)) {
404 prev_obj_desc
->common
.next_object
=
405 obj_desc
->common
.next_object
;
407 node
->object
= obj_desc
->common
.next_object
;
410 acpi_ut_remove_reference(obj_desc
);
414 prev_obj_desc
= obj_desc
;
415 obj_desc
= obj_desc
->common
.next_object
;
418 return (AE_NOT_FOUND
);
421 /*******************************************************************************
423 * FUNCTION: acpi_ns_get_attached_data
425 * PARAMETERS: node - Namespace node
426 * handler - Handler associated with the data
427 * data - Where the data is returned
431 * DESCRIPTION: Low level interface to obtain data previously associated with
434 ******************************************************************************/
437 acpi_ns_get_attached_data(struct acpi_namespace_node
* node
,
438 acpi_object_handler handler
, void **data
)
440 union acpi_operand_object
*obj_desc
;
442 obj_desc
= node
->object
;
444 if ((obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
) &&
445 (obj_desc
->data
.handler
== handler
)) {
446 *data
= obj_desc
->data
.pointer
;
450 obj_desc
= obj_desc
->common
.next_object
;
453 return (AE_NOT_FOUND
);