nsobject.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsobject - Utilities for objects attached to namespace
  4. * table entries
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2005, R. Byron Moore
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <acpi/acpi.h>
  44. #include <acpi/acnamesp.h>
  45. #define _COMPONENT ACPI_NAMESPACE
  46. ACPI_MODULE_NAME ("nsobject")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ns_attach_object
  50. *
  51. * PARAMETERS: Node - Parent Node
  52. * Object - Object to be attached
  53. * Type - Type of object, or ACPI_TYPE_ANY if not
  54. * known
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Record the given object as the value associated with the
  59. * name whose acpi_handle is passed. If Object is NULL
  60. * and Type is ACPI_TYPE_ANY, set the name as having no value.
  61. * Note: Future may require that the Node->Flags field be passed
  62. * as a parameter.
  63. *
  64. * MUTEX: Assumes namespace is locked
  65. *
  66. ******************************************************************************/
  67. acpi_status
  68. acpi_ns_attach_object (
  69. struct acpi_namespace_node *node,
  70. union acpi_operand_object *object,
  71. acpi_object_type type)
  72. {
  73. union acpi_operand_object *obj_desc;
  74. union acpi_operand_object *last_obj_desc;
  75. acpi_object_type object_type = ACPI_TYPE_ANY;
  76. ACPI_FUNCTION_TRACE ("ns_attach_object");
  77. /*
  78. * Parameter validation
  79. */
  80. if (!node) {
  81. /* Invalid handle */
  82. ACPI_REPORT_ERROR (("ns_attach_object: Null named_obj handle\n"));
  83. return_ACPI_STATUS (AE_BAD_PARAMETER);
  84. }
  85. if (!object && (ACPI_TYPE_ANY != type)) {
  86. /* Null object */
  87. ACPI_REPORT_ERROR ((
  88. "ns_attach_object: Null object, but type not ACPI_TYPE_ANY\n"));
  89. return_ACPI_STATUS (AE_BAD_PARAMETER);
  90. }
  91. if (ACPI_GET_DESCRIPTOR_TYPE (node) != ACPI_DESC_TYPE_NAMED) {
  92. /* Not a name handle */
  93. ACPI_REPORT_ERROR (("ns_attach_object: Invalid handle %p [%s]\n",
  94. node, acpi_ut_get_descriptor_name (node)));
  95. return_ACPI_STATUS (AE_BAD_PARAMETER);
  96. }
  97. /* Check if this object is already attached */
  98. if (node->object == object) {
  99. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  100. "Obj %p already installed in name_obj %p\n",
  101. object, node));
  102. return_ACPI_STATUS (AE_OK);
  103. }
  104. /* If null object, we will just install it */
  105. if (!object) {
  106. obj_desc = NULL;
  107. object_type = ACPI_TYPE_ANY;
  108. }
  109. /*
  110. * If the source object is a namespace Node with an attached object,
  111. * we will use that (attached) object
  112. */
  113. else if ((ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) &&
  114. ((struct acpi_namespace_node *) object)->object) {
  115. /*
  116. * Value passed is a name handle and that name has a
  117. * non-null value. Use that name's value and type.
  118. */
  119. obj_desc = ((struct acpi_namespace_node *) object)->object;
  120. object_type = ((struct acpi_namespace_node *) object)->type;
  121. }
  122. /*
  123. * Otherwise, we will use the parameter object, but we must type
  124. * it first
  125. */
  126. else {
  127. obj_desc = (union acpi_operand_object *) object;
  128. /* Use the given type */
  129. object_type = type;
  130. }
  131. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
  132. obj_desc, node, acpi_ut_get_node_name (node)));
  133. /* Detach an existing attached object if present */
  134. if (node->object) {
  135. acpi_ns_detach_object (node);
  136. }
  137. if (obj_desc) {
  138. /*
  139. * Must increment the new value's reference count
  140. * (if it is an internal object)
  141. */
  142. acpi_ut_add_reference (obj_desc);
  143. /*
  144. * Handle objects with multiple descriptors - walk
  145. * to the end of the descriptor list
  146. */
  147. last_obj_desc = obj_desc;
  148. while (last_obj_desc->common.next_object) {
  149. last_obj_desc = last_obj_desc->common.next_object;
  150. }
  151. /* Install the object at the front of the object list */
  152. last_obj_desc->common.next_object = node->object;
  153. }
  154. node->type = (u8) object_type;
  155. node->object = obj_desc;
  156. return_ACPI_STATUS (AE_OK);
  157. }
  158. /*******************************************************************************
  159. *
  160. * FUNCTION: acpi_ns_detach_object
  161. *
  162. * PARAMETERS: Node - A Namespace node whose object will be detached
  163. *
  164. * RETURN: None.
  165. *
  166. * DESCRIPTION: Detach/delete an object associated with a namespace node.
  167. * if the object is an allocated object, it is freed.
  168. * Otherwise, the field is simply cleared.
  169. *
  170. ******************************************************************************/
  171. void
  172. acpi_ns_detach_object (
  173. struct acpi_namespace_node *node)
  174. {
  175. union acpi_operand_object *obj_desc;
  176. ACPI_FUNCTION_TRACE ("ns_detach_object");
  177. obj_desc = node->object;
  178. if (!obj_desc ||
  179. (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA)) {
  180. return_VOID;
  181. }
  182. /* Clear the entry in all cases */
  183. node->object = NULL;
  184. if (ACPI_GET_DESCRIPTOR_TYPE (obj_desc) == ACPI_DESC_TYPE_OPERAND) {
  185. node->object = obj_desc->common.next_object;
  186. if (node->object &&
  187. (ACPI_GET_OBJECT_TYPE (node->object) != ACPI_TYPE_LOCAL_DATA)) {
  188. node->object = node->object->common.next_object;
  189. }
  190. }
  191. /* Reset the node type to untyped */
  192. node->type = ACPI_TYPE_ANY;
  193. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
  194. node, acpi_ut_get_node_name (node), obj_desc));
  195. /* Remove one reference on the object (and all subobjects) */
  196. acpi_ut_remove_reference (obj_desc);
  197. return_VOID;
  198. }
  199. /*******************************************************************************
  200. *
  201. * FUNCTION: acpi_ns_get_attached_object
  202. *
  203. * PARAMETERS: Node - Namespace node
  204. *
  205. * RETURN: Current value of the object field from the Node whose
  206. * handle is passed
  207. *
  208. * DESCRIPTION: Obtain the object attached to a namespace node.
  209. *
  210. ******************************************************************************/
  211. union acpi_operand_object *
  212. acpi_ns_get_attached_object (
  213. struct acpi_namespace_node *node)
  214. {
  215. ACPI_FUNCTION_TRACE_PTR ("ns_get_attached_object", node);
  216. if (!node) {
  217. ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Null Node ptr\n"));
  218. return_PTR (NULL);
  219. }
  220. if (!node->object ||
  221. ((ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_OPERAND) &&
  222. (ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_NAMED)) ||
  223. (ACPI_GET_OBJECT_TYPE (node->object) == ACPI_TYPE_LOCAL_DATA)) {
  224. return_PTR (NULL);
  225. }
  226. return_PTR (node->object);
  227. }
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ns_get_secondary_object
  231. *
  232. * PARAMETERS: Node - Namespace node
  233. *
  234. * RETURN: Current value of the object field from the Node whose
  235. * handle is passed.
  236. *
  237. * DESCRIPTION: Obtain a secondary object associated with a namespace node.
  238. *
  239. ******************************************************************************/
  240. union acpi_operand_object *
  241. acpi_ns_get_secondary_object (
  242. union acpi_operand_object *obj_desc)
  243. {
  244. ACPI_FUNCTION_TRACE_PTR ("ns_get_secondary_object", obj_desc);
  245. if ((!obj_desc) ||
  246. (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) ||
  247. (!obj_desc->common.next_object) ||
  248. (ACPI_GET_OBJECT_TYPE (obj_desc->common.next_object) == ACPI_TYPE_LOCAL_DATA)) {
  249. return_PTR (NULL);
  250. }
  251. return_PTR (obj_desc->common.next_object);
  252. }
  253. /*******************************************************************************
  254. *
  255. * FUNCTION: acpi_ns_attach_data
  256. *
  257. * PARAMETERS: Node - Namespace node
  258. * Handler - Handler to be associated with the data
  259. * Data - Data to be attached
  260. *
  261. * RETURN: Status
  262. *
  263. * DESCRIPTION: Low-level attach data. Create and attach a Data object.
  264. *
  265. ******************************************************************************/
  266. acpi_status
  267. acpi_ns_attach_data (
  268. struct acpi_namespace_node *node,
  269. acpi_object_handler handler,
  270. void *data)
  271. {
  272. union acpi_operand_object *prev_obj_desc;
  273. union acpi_operand_object *obj_desc;
  274. union acpi_operand_object *data_desc;
  275. /* We only allow one attachment per handler */
  276. prev_obj_desc = NULL;
  277. obj_desc = node->object;
  278. while (obj_desc) {
  279. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  280. (obj_desc->data.handler == handler)) {
  281. return (AE_ALREADY_EXISTS);
  282. }
  283. prev_obj_desc = obj_desc;
  284. obj_desc = obj_desc->common.next_object;
  285. }
  286. /* Create an internal object for the data */
  287. data_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_DATA);
  288. if (!data_desc) {
  289. return (AE_NO_MEMORY);
  290. }
  291. data_desc->data.handler = handler;
  292. data_desc->data.pointer = data;
  293. /* Install the data object */
  294. if (prev_obj_desc) {
  295. prev_obj_desc->common.next_object = data_desc;
  296. }
  297. else {
  298. node->object = data_desc;
  299. }
  300. return (AE_OK);
  301. }
  302. /*******************************************************************************
  303. *
  304. * FUNCTION: acpi_ns_detach_data
  305. *
  306. * PARAMETERS: Node - Namespace node
  307. * Handler - Handler associated with the data
  308. *
  309. * RETURN: Status
  310. *
  311. * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
  312. * is responsible for the actual data.
  313. *
  314. ******************************************************************************/
  315. acpi_status
  316. acpi_ns_detach_data (
  317. struct acpi_namespace_node *node,
  318. acpi_object_handler handler)
  319. {
  320. union acpi_operand_object *obj_desc;
  321. union acpi_operand_object *prev_obj_desc;
  322. prev_obj_desc = NULL;
  323. obj_desc = node->object;
  324. while (obj_desc) {
  325. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  326. (obj_desc->data.handler == handler)) {
  327. if (prev_obj_desc) {
  328. prev_obj_desc->common.next_object = obj_desc->common.next_object;
  329. }
  330. else {
  331. node->object = obj_desc->common.next_object;
  332. }
  333. acpi_ut_remove_reference (obj_desc);
  334. return (AE_OK);
  335. }
  336. prev_obj_desc = obj_desc;
  337. obj_desc = obj_desc->common.next_object;
  338. }
  339. return (AE_NOT_FOUND);
  340. }
  341. /*******************************************************************************
  342. *
  343. * FUNCTION: acpi_ns_get_attached_data
  344. *
  345. * PARAMETERS: Node - Namespace node
  346. * Handler - Handler associated with the data
  347. * Data - Where the data is returned
  348. *
  349. * RETURN: Status
  350. *
  351. * DESCRIPTION: Low level interface to obtain data previously associated with
  352. * a namespace node.
  353. *
  354. ******************************************************************************/
  355. acpi_status
  356. acpi_ns_get_attached_data (
  357. struct acpi_namespace_node *node,
  358. acpi_object_handler handler,
  359. void **data)
  360. {
  361. union acpi_operand_object *obj_desc;
  362. obj_desc = node->object;
  363. while (obj_desc) {
  364. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  365. (obj_desc->data.handler == handler)) {
  366. *data = obj_desc->data.pointer;
  367. return (AE_OK);
  368. }
  369. obj_desc = obj_desc->common.next_object;
  370. }
  371. return (AE_NOT_FOUND);
  372. }