1 /*******************************************************************************
2  *
3  * Module Name: nsobject - Utilities for objects attached to namespace
4  *                         table entries
5  *
6  ******************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2004, R. Byron Moore
10  * 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 
45 
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
48 
49 
50 #define _COMPONENT          ACPI_NAMESPACE
51 	 ACPI_MODULE_NAME    ("nsobject")
52 
53 
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ns_attach_object
57  *
58  * PARAMETERS:  Node                - Parent Node
59  *              Object              - Object to be attached
60  *              Type                - Type of object, or ACPI_TYPE_ANY if not
61  *                                    known
62  *
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.
68  *
69  * MUTEX:       Assumes namespace is locked
70  *
71  ******************************************************************************/
72 
73 acpi_status
acpi_ns_attach_object(struct acpi_namespace_node * node,union acpi_operand_object * object,acpi_object_type type)74 acpi_ns_attach_object (
75 	struct acpi_namespace_node      *node,
76 	union acpi_operand_object       *object,
77 	acpi_object_type                type)
78 {
79 	union acpi_operand_object       *obj_desc;
80 	union acpi_operand_object       *last_obj_desc;
81 	acpi_object_type                object_type = ACPI_TYPE_ANY;
82 
83 
84 	ACPI_FUNCTION_TRACE ("ns_attach_object");
85 
86 
87 	/*
88 	 * Parameter validation
89 	 */
90 	if (!node) {
91 		/* Invalid handle */
92 
93 		ACPI_REPORT_ERROR (("ns_attach_object: Null named_obj handle\n"));
94 		return_ACPI_STATUS (AE_BAD_PARAMETER);
95 	}
96 
97 	if (!object && (ACPI_TYPE_ANY != type)) {
98 		/* Null object */
99 
100 		ACPI_REPORT_ERROR (("ns_attach_object: Null object, but type not ACPI_TYPE_ANY\n"));
101 		return_ACPI_STATUS (AE_BAD_PARAMETER);
102 	}
103 
104 	if (ACPI_GET_DESCRIPTOR_TYPE (node) != ACPI_DESC_TYPE_NAMED) {
105 		/* Not a name handle */
106 
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);
110 	}
111 
112 	/* Check if this object is already attached */
113 
114 	if (node->object == object) {
115 		ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Obj %p already installed in name_obj %p\n",
116 			object, node));
117 
118 		return_ACPI_STATUS (AE_OK);
119 	}
120 
121 	/* If null object, we will just install it */
122 
123 	if (!object) {
124 		obj_desc   = NULL;
125 		object_type = ACPI_TYPE_ANY;
126 	}
127 
128 	/*
129 	 * If the source object is a namespace Node with an attached object,
130 	 * we will use that (attached) object
131 	 */
132 	else if ((ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) &&
133 			((struct acpi_namespace_node *) object)->object) {
134 		/*
135 		 * Value passed is a name handle and that name has a
136 		 * non-null value.  Use that name's value and type.
137 		 */
138 		obj_desc   = ((struct acpi_namespace_node *) object)->object;
139 		object_type = ((struct acpi_namespace_node *) object)->type;
140 	}
141 
142 	/*
143 	 * Otherwise, we will use the parameter object, but we must type
144 	 * it first
145 	 */
146 	else {
147 		obj_desc = (union acpi_operand_object   *) object;
148 
149 		/* Use the given type */
150 
151 		object_type = type;
152 	}
153 
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)));
156 
157 	/* Detach an existing attached object if present */
158 
159 	if (node->object) {
160 		acpi_ns_detach_object (node);
161 	}
162 
163 	if (obj_desc) {
164 		/*
165 		 * Must increment the new value's reference count
166 		 * (if it is an internal object)
167 		 */
168 		acpi_ut_add_reference (obj_desc);
169 
170 		/*
171 		 * Handle objects with multiple descriptors - walk
172 		 * to the end of the descriptor list
173 		 */
174 		last_obj_desc = obj_desc;
175 		while (last_obj_desc->common.next_object) {
176 			last_obj_desc = last_obj_desc->common.next_object;
177 		}
178 
179 		/* Install the object at the front of the object list */
180 
181 		last_obj_desc->common.next_object = node->object;
182 	}
183 
184 	node->type     = (u8) object_type;
185 	node->object   = obj_desc;
186 
187 	return_ACPI_STATUS (AE_OK);
188 }
189 
190 
191 /*******************************************************************************
192  *
193  * FUNCTION:    acpi_ns_detach_object
194  *
195  * PARAMETERS:  Node           - An node whose object will be detached
196  *
197  * RETURN:      None.
198  *
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.
202  *
203  ******************************************************************************/
204 
205 void
acpi_ns_detach_object(struct acpi_namespace_node * node)206 acpi_ns_detach_object (
207 	struct acpi_namespace_node      *node)
208 {
209 	union acpi_operand_object       *obj_desc;
210 
211 
212 	ACPI_FUNCTION_TRACE ("ns_detach_object");
213 
214 
215 	obj_desc = node->object;
216 
217 	if (!obj_desc ||
218 		(ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA)) {
219 		return_VOID;
220 	}
221 
222 	/* Clear the entry in all cases */
223 
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;
230 		}
231 	}
232 
233 	/* Reset the node type to untyped */
234 
235 	node->type = ACPI_TYPE_ANY;
236 
237 	ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
238 		node, acpi_ut_get_node_name (node), obj_desc));
239 
240 	/* Remove one reference on the object (and all subobjects) */
241 
242 	acpi_ut_remove_reference (obj_desc);
243 	return_VOID;
244 }
245 
246 
247 /*******************************************************************************
248  *
249  * FUNCTION:    acpi_ns_get_attached_object
250  *
251  * PARAMETERS:  Node             - Parent Node to be examined
252  *
253  * RETURN:      Current value of the object field from the Node whose
254  *              handle is passed
255  *
256  * DESCRIPTION: Obtain the object attached to a namespace node.
257  *
258  ******************************************************************************/
259 
260 union acpi_operand_object *
acpi_ns_get_attached_object(struct acpi_namespace_node * node)261 acpi_ns_get_attached_object (
262 	struct acpi_namespace_node      *node)
263 {
264 	ACPI_FUNCTION_TRACE_PTR ("ns_get_attached_object", node);
265 
266 
267 	if (!node) {
268 		ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Null Node ptr\n"));
269 		return_PTR (NULL);
270 	}
271 
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);
277 	}
278 
279 	return_PTR (node->object);
280 }
281 
282 
283 /*******************************************************************************
284  *
285  * FUNCTION:    acpi_ns_get_secondary_object
286  *
287  * PARAMETERS:  Node             - Parent Node to be examined
288  *
289  * RETURN:      Current value of the object field from the Node whose
290  *              handle is passed.
291  *
292  * DESCRIPTION: Obtain a secondary object associated with a namespace node.
293  *
294  ******************************************************************************/
295 
296 union acpi_operand_object *
acpi_ns_get_secondary_object(union acpi_operand_object * obj_desc)297 acpi_ns_get_secondary_object (
298 	union acpi_operand_object       *obj_desc)
299 {
300 	ACPI_FUNCTION_TRACE_PTR ("ns_get_secondary_object", obj_desc);
301 
302 
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);
308 	}
309 
310 	return_PTR (obj_desc->common.next_object);
311 }
312 
313 
314 /*******************************************************************************
315  *
316  * FUNCTION:    acpi_ns_attach_data
317  *
318  * PARAMETERS:  Node            - Namespace node
319  *              Handler         - Handler to be associated with the data
320  *              Data            - Data to be attached
321  *
322  * RETURN:      Status
323  *
324  * DESCRIPTION: Low-level attach data.  Create and attach a Data object.
325  *
326  ******************************************************************************/
327 
328 acpi_status
acpi_ns_attach_data(struct acpi_namespace_node * node,acpi_object_handler handler,void * data)329 acpi_ns_attach_data (
330 	struct acpi_namespace_node      *node,
331 	acpi_object_handler             handler,
332 	void                            *data)
333 {
334 	union acpi_operand_object       *prev_obj_desc;
335 	union acpi_operand_object       *obj_desc;
336 	union acpi_operand_object       *data_desc;
337 
338 
339 	/* We only allow one attachment per handler */
340 
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);
347 		}
348 
349 		prev_obj_desc = obj_desc;
350 		obj_desc = obj_desc->common.next_object;
351 	}
352 
353 	/* Create an internal object for the data */
354 
355 	data_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_DATA);
356 	if (!data_desc) {
357 		return (AE_NO_MEMORY);
358 	}
359 
360 	data_desc->data.handler = handler;
361 	data_desc->data.pointer = data;
362 
363 	/* Install the data object */
364 
365 	if (prev_obj_desc) {
366 		prev_obj_desc->common.next_object = data_desc;
367 	}
368 	else {
369 		node->object = data_desc;
370 	}
371 
372 	return (AE_OK);
373 }
374 
375 
376 /*******************************************************************************
377  *
378  * FUNCTION:    acpi_ns_detach_data
379  *
380  * PARAMETERS:  Node            - Namespace node
381  *              Handler         - Handler associated with the data
382  *
383  * RETURN:      Status
384  *
385  * DESCRIPTION: Low-level detach data.  Delete the data node, but the caller
386  *              is responsible for the actual data.
387  *
388  ******************************************************************************/
389 
390 acpi_status
acpi_ns_detach_data(struct acpi_namespace_node * node,acpi_object_handler handler)391 acpi_ns_detach_data (
392 	struct acpi_namespace_node      *node,
393 	acpi_object_handler             handler)
394 {
395 	union acpi_operand_object       *obj_desc;
396 	union acpi_operand_object       *prev_obj_desc;
397 
398 
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;
406 			}
407 			else {
408 				node->object = obj_desc->common.next_object;
409 			}
410 
411 			acpi_ut_remove_reference (obj_desc);
412 			return (AE_OK);
413 		}
414 
415 		prev_obj_desc = obj_desc;
416 		obj_desc = obj_desc->common.next_object;
417 	}
418 
419 	return (AE_NOT_FOUND);
420 }
421 
422 
423 /*******************************************************************************
424  *
425  * FUNCTION:    acpi_ns_get_attached_data
426  *
427  * PARAMETERS:  Node            - Namespace node
428  *              Handler         - Handler associated with the data
429  *              Data            - Where the data is returned
430  *
431  * RETURN:      Status
432  *
433  * DESCRIPTION: Low level interface to obtain data previously associated with
434  *              a namespace node.
435  *
436  ******************************************************************************/
437 
438 acpi_status
acpi_ns_get_attached_data(struct acpi_namespace_node * node,acpi_object_handler handler,void ** data)439 acpi_ns_get_attached_data (
440 	struct acpi_namespace_node      *node,
441 	acpi_object_handler             handler,
442 	void                            **data)
443 {
444 	union acpi_operand_object       *obj_desc;
445 
446 
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);
453 		}
454 
455 		obj_desc = obj_desc->common.next_object;
456 	}
457 
458 	return (AE_NOT_FOUND);
459 }
460 
461 
462