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