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