blob: 3eb20bfda9d81a079c606d2e91ac85aa5dd8a859 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: nsobject - Utilities for objects attached to namespace
4 * table entries
5 *
6 ******************************************************************************/
7
8/*
Len Brown75a44ce2008-04-23 23:00:13 -04009 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("nsobject")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*******************************************************************************
53 *
54 * FUNCTION: acpi_ns_attach_object
55 *
56 * PARAMETERS: Node - Parent Node
57 * Object - Object to be attached
58 * Type - Type of object, or ACPI_TYPE_ANY if not
59 * known
60 *
Robert Moore44f6c012005-04-18 22:49:35 -040061 * RETURN: Status
62 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * 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 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070072acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040073acpi_ns_attach_object(struct acpi_namespace_node *node,
74 union acpi_operand_object *object, acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Len Brown4be44fc2005-08-05 00:44:28 -040076 union acpi_operand_object *obj_desc;
77 union acpi_operand_object *last_obj_desc;
78 acpi_object_type object_type = ACPI_TYPE_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bob Mooreb229cf92006-04-21 17:15:00 -040080 ACPI_FUNCTION_TRACE(ns_attach_object);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /*
83 * Parameter validation
84 */
85 if (!node) {
Bob Moore52fc0b02006-10-02 00:00:00 -040086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* Invalid handle */
88
Bob Mooreb229cf92006-04-21 17:15:00 -040089 ACPI_ERROR((AE_INFO, "Null NamedObj handle"));
Len Brown4be44fc2005-08-05 00:44:28 -040090 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 if (!object && (ACPI_TYPE_ANY != type)) {
Bob Moore52fc0b02006-10-02 00:00:00 -040094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* Null object */
96
Bob Mooreb8e4d892006-01-27 16:43:00 -050097 ACPI_ERROR((AE_INFO,
98 "Null object, but type not ACPI_TYPE_ANY"));
Len Brown4be44fc2005-08-05 00:44:28 -040099 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
Len Brown4be44fc2005-08-05 00:44:28 -0400102 if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* Not a name handle */
105
Bob Mooreb8e4d892006-01-27 16:43:00 -0500106 ACPI_ERROR((AE_INFO, "Invalid handle %p [%s]",
107 node, acpi_ut_get_descriptor_name(node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400108 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
111 /* Check if this object is already attached */
112
113 if (node->object == object) {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Bob Mooreb229cf92006-04-21 17:15:00 -0400115 "Obj %p already installed in NameObj %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400116 object, node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Len Brown4be44fc2005-08-05 00:44:28 -0400118 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
121 /* If null object, we will just install it */
122
123 if (!object) {
Len Brown4be44fc2005-08-05 00:44:28 -0400124 obj_desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400132 else if ((ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_NAMED) &&
133 ((struct acpi_namespace_node *)object)->object) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * Value passed is a name handle and that name has a
136 * non-null value. Use that name's value and type.
137 */
Len Brown4be44fc2005-08-05 00:44:28 -0400138 obj_desc = ((struct acpi_namespace_node *)object)->object;
139 object_type = ((struct acpi_namespace_node *)object)->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 /*
143 * Otherwise, we will use the parameter object, but we must type
144 * it first
145 */
146 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400147 obj_desc = (union acpi_operand_object *)object;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* Use the given type */
150
151 object_type = type;
152 }
153
Len Brown4be44fc2005-08-05 00:44:28 -0400154 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
155 obj_desc, node, acpi_ut_get_node_name(node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* Detach an existing attached object if present */
158
159 if (node->object) {
Len Brown4be44fc2005-08-05 00:44:28 -0400160 acpi_ns_detach_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 if (obj_desc) {
164 /*
165 * Must increment the new value's reference count
166 * (if it is an internal object)
167 */
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_ut_add_reference(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
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
Len Brown4be44fc2005-08-05 00:44:28 -0400184 node->type = (u8) object_type;
185 node->object = obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Len Brown4be44fc2005-08-05 00:44:28 -0400187 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*******************************************************************************
191 *
192 * FUNCTION: acpi_ns_detach_object
193 *
Robert Moore44f6c012005-04-18 22:49:35 -0400194 * PARAMETERS: Node - A Namespace node whose object will be detached
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * RETURN: None.
197 *
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.
201 *
202 ******************************************************************************/
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204void acpi_ns_detach_object(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Len Brown4be44fc2005-08-05 00:44:28 -0400206 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Bob Mooreb229cf92006-04-21 17:15:00 -0400208 ACPI_FUNCTION_TRACE(ns_detach_object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 obj_desc = node->object;
211
Bob Moore3371c192009-02-18 14:44:03 +0800212 if (!obj_desc || (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return_VOID;
214 }
215
216 /* Clear the entry in all cases */
217
218 node->object = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400219 if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) == ACPI_DESC_TYPE_OPERAND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 node->object = obj_desc->common.next_object;
221 if (node->object &&
Bob Moore3371c192009-02-18 14:44:03 +0800222 ((node->object)->common.type != ACPI_TYPE_LOCAL_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 node->object = node->object->common.next_object;
224 }
225 }
226
227 /* Reset the node type to untyped */
228
229 node->type = ACPI_TYPE_ANY;
230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
232 node, acpi_ut_get_node_name(node), obj_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Remove one reference on the object (and all subobjects) */
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 acpi_ut_remove_reference(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return_VOID;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*******************************************************************************
241 *
242 * FUNCTION: acpi_ns_get_attached_object
243 *
Robert Moore44f6c012005-04-18 22:49:35 -0400244 * PARAMETERS: Node - Namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * RETURN: Current value of the object field from the Node whose
247 * handle is passed
248 *
249 * DESCRIPTION: Obtain the object attached to a namespace node.
250 *
251 ******************************************************************************/
252
Len Brown4be44fc2005-08-05 00:44:28 -0400253union acpi_operand_object *acpi_ns_get_attached_object(struct
254 acpi_namespace_node
255 *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Bob Mooreb229cf92006-04-21 17:15:00 -0400257 ACPI_FUNCTION_TRACE_PTR(ns_get_attached_object, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (!node) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500260 ACPI_WARNING((AE_INFO, "Null Node ptr"));
Len Brown4be44fc2005-08-05 00:44:28 -0400261 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
264 if (!node->object ||
Len Brown4be44fc2005-08-05 00:44:28 -0400265 ((ACPI_GET_DESCRIPTOR_TYPE(node->object) != ACPI_DESC_TYPE_OPERAND)
266 && (ACPI_GET_DESCRIPTOR_TYPE(node->object) !=
267 ACPI_DESC_TYPE_NAMED))
Bob Moore3371c192009-02-18 14:44:03 +0800268 || ((node->object)->common.type == ACPI_TYPE_LOCAL_DATA)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400269 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272 return_PTR(node->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*******************************************************************************
276 *
277 * FUNCTION: acpi_ns_get_secondary_object
278 *
Robert Moore44f6c012005-04-18 22:49:35 -0400279 * PARAMETERS: Node - Namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * RETURN: Current value of the object field from the Node whose
282 * handle is passed.
283 *
284 * DESCRIPTION: Obtain a secondary object associated with a namespace node.
285 *
286 ******************************************************************************/
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288union acpi_operand_object *acpi_ns_get_secondary_object(union
289 acpi_operand_object
290 *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Bob Mooreb229cf92006-04-21 17:15:00 -0400292 ACPI_FUNCTION_TRACE_PTR(ns_get_secondary_object, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 if ((!obj_desc) ||
Bob Moore3371c192009-02-18 14:44:03 +0800295 (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400296 (!obj_desc->common.next_object) ||
Bob Moore3371c192009-02-18 14:44:03 +0800297 ((obj_desc->common.next_object)->common.type ==
Len Brown4be44fc2005-08-05 00:44:28 -0400298 ACPI_TYPE_LOCAL_DATA)) {
299 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 return_PTR(obj_desc->common.next_object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*******************************************************************************
306 *
307 * FUNCTION: acpi_ns_attach_data
308 *
309 * PARAMETERS: Node - Namespace node
310 * Handler - Handler to be associated with the data
311 * Data - Data to be attached
312 *
313 * RETURN: Status
314 *
315 * DESCRIPTION: Low-level attach data. Create and attach a Data object.
316 *
317 ******************************************************************************/
318
319acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400320acpi_ns_attach_data(struct acpi_namespace_node *node,
321 acpi_object_handler handler, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Len Brown4be44fc2005-08-05 00:44:28 -0400323 union acpi_operand_object *prev_obj_desc;
324 union acpi_operand_object *obj_desc;
325 union acpi_operand_object *data_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* We only allow one attachment per handler */
328
329 prev_obj_desc = NULL;
330 obj_desc = node->object;
331 while (obj_desc) {
Bob Moore3371c192009-02-18 14:44:03 +0800332 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400333 (obj_desc->data.handler == handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return (AE_ALREADY_EXISTS);
335 }
336
337 prev_obj_desc = obj_desc;
338 obj_desc = obj_desc->common.next_object;
339 }
340
341 /* Create an internal object for the data */
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 data_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!data_desc) {
345 return (AE_NO_MEMORY);
346 }
347
348 data_desc->data.handler = handler;
349 data_desc->data.pointer = data;
350
351 /* Install the data object */
352
353 if (prev_obj_desc) {
354 prev_obj_desc->common.next_object = data_desc;
Len Brown4be44fc2005-08-05 00:44:28 -0400355 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 node->object = data_desc;
357 }
358
359 return (AE_OK);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/*******************************************************************************
363 *
364 * FUNCTION: acpi_ns_detach_data
365 *
366 * PARAMETERS: Node - Namespace node
367 * Handler - Handler associated with the data
368 *
369 * RETURN: Status
370 *
371 * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
372 * is responsible for the actual data.
373 *
374 ******************************************************************************/
375
376acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400377acpi_ns_detach_data(struct acpi_namespace_node * node,
378 acpi_object_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Len Brown4be44fc2005-08-05 00:44:28 -0400380 union acpi_operand_object *obj_desc;
381 union acpi_operand_object *prev_obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 prev_obj_desc = NULL;
384 obj_desc = node->object;
385 while (obj_desc) {
Bob Moore3371c192009-02-18 14:44:03 +0800386 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400387 (obj_desc->data.handler == handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (prev_obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400389 prev_obj_desc->common.next_object =
390 obj_desc->common.next_object;
391 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 node->object = obj_desc->common.next_object;
393 }
394
Len Brown4be44fc2005-08-05 00:44:28 -0400395 acpi_ut_remove_reference(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return (AE_OK);
397 }
398
399 prev_obj_desc = obj_desc;
400 obj_desc = obj_desc->common.next_object;
401 }
402
403 return (AE_NOT_FOUND);
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*******************************************************************************
407 *
408 * FUNCTION: acpi_ns_get_attached_data
409 *
410 * PARAMETERS: Node - Namespace node
411 * Handler - Handler associated with the data
412 * Data - Where the data is returned
413 *
414 * RETURN: Status
415 *
416 * DESCRIPTION: Low level interface to obtain data previously associated with
417 * a namespace node.
418 *
419 ******************************************************************************/
420
421acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400422acpi_ns_get_attached_data(struct acpi_namespace_node * node,
423 acpi_object_handler handler, void **data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Len Brown4be44fc2005-08-05 00:44:28 -0400425 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 obj_desc = node->object;
428 while (obj_desc) {
Bob Moore3371c192009-02-18 14:44:03 +0800429 if ((obj_desc->common.type == ACPI_TYPE_LOCAL_DATA) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400430 (obj_desc->data.handler == handler)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *data = obj_desc->data.pointer;
432 return (AE_OK);
433 }
434
435 obj_desc = obj_desc->common.next_object;
436 }
437
438 return (AE_NOT_FOUND);
439}