nsobject.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. * DESCRIPTION: Record the given object as the value associated with the
  57. * name whose acpi_handle is passed. If Object is NULL
  58. * and Type is ACPI_TYPE_ANY, set the name as having no value.
  59. * Note: Future may require that the Node->Flags field be passed
  60. * as a parameter.
  61. *
  62. * MUTEX: Assumes namespace is locked
  63. *
  64. ******************************************************************************/
  65. acpi_status
  66. acpi_ns_attach_object (
  67. struct acpi_namespace_node *node,
  68. union acpi_operand_object *object,
  69. acpi_object_type type)
  70. {
  71. union acpi_operand_object *obj_desc;
  72. union acpi_operand_object *last_obj_desc;
  73. acpi_object_type object_type = ACPI_TYPE_ANY;
  74. ACPI_FUNCTION_TRACE ("ns_attach_object");
  75. /*
  76. * Parameter validation
  77. */
  78. if (!node) {
  79. /* Invalid handle */
  80. ACPI_REPORT_ERROR (("ns_attach_object: Null named_obj handle\n"));
  81. return_ACPI_STATUS (AE_BAD_PARAMETER);
  82. }
  83. if (!object && (ACPI_TYPE_ANY != type)) {
  84. /* Null object */
  85. ACPI_REPORT_ERROR (("ns_attach_object: Null object, but type not ACPI_TYPE_ANY\n"));
  86. return_ACPI_STATUS (AE_BAD_PARAMETER);
  87. }
  88. if (ACPI_GET_DESCRIPTOR_TYPE (node) != ACPI_DESC_TYPE_NAMED) {
  89. /* Not a name handle */
  90. ACPI_REPORT_ERROR (("ns_attach_object: Invalid handle %p [%s]\n",
  91. node, acpi_ut_get_descriptor_name (node)));
  92. return_ACPI_STATUS (AE_BAD_PARAMETER);
  93. }
  94. /* Check if this object is already attached */
  95. if (node->object == object) {
  96. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Obj %p already installed in name_obj %p\n",
  97. object, node));
  98. return_ACPI_STATUS (AE_OK);
  99. }
  100. /* If null object, we will just install it */
  101. if (!object) {
  102. obj_desc = NULL;
  103. object_type = ACPI_TYPE_ANY;
  104. }
  105. /*
  106. * If the source object is a namespace Node with an attached object,
  107. * we will use that (attached) object
  108. */
  109. else if ((ACPI_GET_DESCRIPTOR_TYPE (object) == ACPI_DESC_TYPE_NAMED) &&
  110. ((struct acpi_namespace_node *) object)->object) {
  111. /*
  112. * Value passed is a name handle and that name has a
  113. * non-null value. Use that name's value and type.
  114. */
  115. obj_desc = ((struct acpi_namespace_node *) object)->object;
  116. object_type = ((struct acpi_namespace_node *) object)->type;
  117. }
  118. /*
  119. * Otherwise, we will use the parameter object, but we must type
  120. * it first
  121. */
  122. else {
  123. obj_desc = (union acpi_operand_object *) object;
  124. /* Use the given type */
  125. object_type = type;
  126. }
  127. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Installing %p into Node %p [%4.4s]\n",
  128. obj_desc, node, acpi_ut_get_node_name (node)));
  129. /* Detach an existing attached object if present */
  130. if (node->object) {
  131. acpi_ns_detach_object (node);
  132. }
  133. if (obj_desc) {
  134. /*
  135. * Must increment the new value's reference count
  136. * (if it is an internal object)
  137. */
  138. acpi_ut_add_reference (obj_desc);
  139. /*
  140. * Handle objects with multiple descriptors - walk
  141. * to the end of the descriptor list
  142. */
  143. last_obj_desc = obj_desc;
  144. while (last_obj_desc->common.next_object) {
  145. last_obj_desc = last_obj_desc->common.next_object;
  146. }
  147. /* Install the object at the front of the object list */
  148. last_obj_desc->common.next_object = node->object;
  149. }
  150. node->type = (u8) object_type;
  151. node->object = obj_desc;
  152. return_ACPI_STATUS (AE_OK);
  153. }
  154. /*******************************************************************************
  155. *
  156. * FUNCTION: acpi_ns_detach_object
  157. *
  158. * PARAMETERS: Node - An node whose object will be detached
  159. *
  160. * RETURN: None.
  161. *
  162. * DESCRIPTION: Detach/delete an object associated with a namespace node.
  163. * if the object is an allocated object, it is freed.
  164. * Otherwise, the field is simply cleared.
  165. *
  166. ******************************************************************************/
  167. void
  168. acpi_ns_detach_object (
  169. struct acpi_namespace_node *node)
  170. {
  171. union acpi_operand_object *obj_desc;
  172. ACPI_FUNCTION_TRACE ("ns_detach_object");
  173. obj_desc = node->object;
  174. if (!obj_desc ||
  175. (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA)) {
  176. return_VOID;
  177. }
  178. /* Clear the entry in all cases */
  179. node->object = NULL;
  180. if (ACPI_GET_DESCRIPTOR_TYPE (obj_desc) == ACPI_DESC_TYPE_OPERAND) {
  181. node->object = obj_desc->common.next_object;
  182. if (node->object &&
  183. (ACPI_GET_OBJECT_TYPE (node->object) != ACPI_TYPE_LOCAL_DATA)) {
  184. node->object = node->object->common.next_object;
  185. }
  186. }
  187. /* Reset the node type to untyped */
  188. node->type = ACPI_TYPE_ANY;
  189. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Node %p [%4.4s] Object %p\n",
  190. node, acpi_ut_get_node_name (node), obj_desc));
  191. /* Remove one reference on the object (and all subobjects) */
  192. acpi_ut_remove_reference (obj_desc);
  193. return_VOID;
  194. }
  195. /*******************************************************************************
  196. *
  197. * FUNCTION: acpi_ns_get_attached_object
  198. *
  199. * PARAMETERS: Node - Parent Node to be examined
  200. *
  201. * RETURN: Current value of the object field from the Node whose
  202. * handle is passed
  203. *
  204. * DESCRIPTION: Obtain the object attached to a namespace node.
  205. *
  206. ******************************************************************************/
  207. union acpi_operand_object *
  208. acpi_ns_get_attached_object (
  209. struct acpi_namespace_node *node)
  210. {
  211. ACPI_FUNCTION_TRACE_PTR ("ns_get_attached_object", node);
  212. if (!node) {
  213. ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Null Node ptr\n"));
  214. return_PTR (NULL);
  215. }
  216. if (!node->object ||
  217. ((ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_OPERAND) &&
  218. (ACPI_GET_DESCRIPTOR_TYPE (node->object) != ACPI_DESC_TYPE_NAMED)) ||
  219. (ACPI_GET_OBJECT_TYPE (node->object) == ACPI_TYPE_LOCAL_DATA)) {
  220. return_PTR (NULL);
  221. }
  222. return_PTR (node->object);
  223. }
  224. /*******************************************************************************
  225. *
  226. * FUNCTION: acpi_ns_get_secondary_object
  227. *
  228. * PARAMETERS: Node - Parent Node to be examined
  229. *
  230. * RETURN: Current value of the object field from the Node whose
  231. * handle is passed.
  232. *
  233. * DESCRIPTION: Obtain a secondary object associated with a namespace node.
  234. *
  235. ******************************************************************************/
  236. union acpi_operand_object *
  237. acpi_ns_get_secondary_object (
  238. union acpi_operand_object *obj_desc)
  239. {
  240. ACPI_FUNCTION_TRACE_PTR ("ns_get_secondary_object", obj_desc);
  241. if ((!obj_desc) ||
  242. (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) ||
  243. (!obj_desc->common.next_object) ||
  244. (ACPI_GET_OBJECT_TYPE (obj_desc->common.next_object) == ACPI_TYPE_LOCAL_DATA)) {
  245. return_PTR (NULL);
  246. }
  247. return_PTR (obj_desc->common.next_object);
  248. }
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: acpi_ns_attach_data
  252. *
  253. * PARAMETERS: Node - Namespace node
  254. * Handler - Handler to be associated with the data
  255. * Data - Data to be attached
  256. *
  257. * RETURN: Status
  258. *
  259. * DESCRIPTION: Low-level attach data. Create and attach a Data object.
  260. *
  261. ******************************************************************************/
  262. acpi_status
  263. acpi_ns_attach_data (
  264. struct acpi_namespace_node *node,
  265. acpi_object_handler handler,
  266. void *data)
  267. {
  268. union acpi_operand_object *prev_obj_desc;
  269. union acpi_operand_object *obj_desc;
  270. union acpi_operand_object *data_desc;
  271. /* We only allow one attachment per handler */
  272. prev_obj_desc = NULL;
  273. obj_desc = node->object;
  274. while (obj_desc) {
  275. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  276. (obj_desc->data.handler == handler)) {
  277. return (AE_ALREADY_EXISTS);
  278. }
  279. prev_obj_desc = obj_desc;
  280. obj_desc = obj_desc->common.next_object;
  281. }
  282. /* Create an internal object for the data */
  283. data_desc = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_DATA);
  284. if (!data_desc) {
  285. return (AE_NO_MEMORY);
  286. }
  287. data_desc->data.handler = handler;
  288. data_desc->data.pointer = data;
  289. /* Install the data object */
  290. if (prev_obj_desc) {
  291. prev_obj_desc->common.next_object = data_desc;
  292. }
  293. else {
  294. node->object = data_desc;
  295. }
  296. return (AE_OK);
  297. }
  298. /*******************************************************************************
  299. *
  300. * FUNCTION: acpi_ns_detach_data
  301. *
  302. * PARAMETERS: Node - Namespace node
  303. * Handler - Handler associated with the data
  304. *
  305. * RETURN: Status
  306. *
  307. * DESCRIPTION: Low-level detach data. Delete the data node, but the caller
  308. * is responsible for the actual data.
  309. *
  310. ******************************************************************************/
  311. acpi_status
  312. acpi_ns_detach_data (
  313. struct acpi_namespace_node *node,
  314. acpi_object_handler handler)
  315. {
  316. union acpi_operand_object *obj_desc;
  317. union acpi_operand_object *prev_obj_desc;
  318. prev_obj_desc = NULL;
  319. obj_desc = node->object;
  320. while (obj_desc) {
  321. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  322. (obj_desc->data.handler == handler)) {
  323. if (prev_obj_desc) {
  324. prev_obj_desc->common.next_object = obj_desc->common.next_object;
  325. }
  326. else {
  327. node->object = obj_desc->common.next_object;
  328. }
  329. acpi_ut_remove_reference (obj_desc);
  330. return (AE_OK);
  331. }
  332. prev_obj_desc = obj_desc;
  333. obj_desc = obj_desc->common.next_object;
  334. }
  335. return (AE_NOT_FOUND);
  336. }
  337. /*******************************************************************************
  338. *
  339. * FUNCTION: acpi_ns_get_attached_data
  340. *
  341. * PARAMETERS: Node - Namespace node
  342. * Handler - Handler associated with the data
  343. * Data - Where the data is returned
  344. *
  345. * RETURN: Status
  346. *
  347. * DESCRIPTION: Low level interface to obtain data previously associated with
  348. * a namespace node.
  349. *
  350. ******************************************************************************/
  351. acpi_status
  352. acpi_ns_get_attached_data (
  353. struct acpi_namespace_node *node,
  354. acpi_object_handler handler,
  355. void **data)
  356. {
  357. union acpi_operand_object *obj_desc;
  358. obj_desc = node->object;
  359. while (obj_desc) {
  360. if ((ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_LOCAL_DATA) &&
  361. (obj_desc->data.handler == handler)) {
  362. *data = obj_desc->data.pointer;
  363. return (AE_OK);
  364. }
  365. obj_desc = obj_desc->common.next_object;
  366. }
  367. return (AE_NOT_FOUND);
  368. }