pstree.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /******************************************************************************
  2. *
  3. * Module Name: pstree - Parser op tree manipulation/traversal/search
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acparser.h"
  45. #include "amlcode.h"
  46. #define _COMPONENT ACPI_PARSER
  47. ACPI_MODULE_NAME("pstree")
  48. /* Local prototypes */
  49. #ifdef ACPI_OBSOLETE_FUNCTIONS
  50. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op);
  51. #endif
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ps_get_arg
  55. *
  56. * PARAMETERS: op - Get an argument for this op
  57. * argn - Nth argument to get
  58. *
  59. * RETURN: The argument (as an Op object). NULL if argument does not exist
  60. *
  61. * DESCRIPTION: Get the specified op's argument.
  62. *
  63. ******************************************************************************/
  64. union acpi_parse_object *acpi_ps_get_arg(union acpi_parse_object *op, u32 argn)
  65. {
  66. union acpi_parse_object *arg = NULL;
  67. const struct acpi_opcode_info *op_info;
  68. ACPI_FUNCTION_ENTRY();
  69. /*
  70. if (Op->Common.aml_opcode == AML_INT_CONNECTION_OP)
  71. {
  72. return (Op->Common.Value.Arg);
  73. }
  74. */
  75. /* Get the info structure for this opcode */
  76. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  77. if (op_info->class == AML_CLASS_UNKNOWN) {
  78. /* Invalid opcode or ASCII character */
  79. return (NULL);
  80. }
  81. /* Check if this opcode requires argument sub-objects */
  82. if (!(op_info->flags & AML_HAS_ARGS)) {
  83. /* Has no linked argument objects */
  84. return (NULL);
  85. }
  86. /* Get the requested argument object */
  87. arg = op->common.value.arg;
  88. while (arg && argn) {
  89. argn--;
  90. arg = arg->common.next;
  91. }
  92. return (arg);
  93. }
  94. /*******************************************************************************
  95. *
  96. * FUNCTION: acpi_ps_append_arg
  97. *
  98. * PARAMETERS: op - Append an argument to this Op.
  99. * arg - Argument Op to append
  100. *
  101. * RETURN: None.
  102. *
  103. * DESCRIPTION: Append an argument to an op's argument list (a NULL arg is OK)
  104. *
  105. ******************************************************************************/
  106. void
  107. acpi_ps_append_arg(union acpi_parse_object *op, union acpi_parse_object *arg)
  108. {
  109. union acpi_parse_object *prev_arg;
  110. const struct acpi_opcode_info *op_info;
  111. ACPI_FUNCTION_ENTRY();
  112. if (!op) {
  113. return;
  114. }
  115. /* Get the info structure for this opcode */
  116. op_info = acpi_ps_get_opcode_info(op->common.aml_opcode);
  117. if (op_info->class == AML_CLASS_UNKNOWN) {
  118. /* Invalid opcode */
  119. ACPI_ERROR((AE_INFO, "Invalid AML Opcode: 0x%2.2X",
  120. op->common.aml_opcode));
  121. return;
  122. }
  123. /* Check if this opcode requires argument sub-objects */
  124. if (!(op_info->flags & AML_HAS_ARGS)) {
  125. /* Has no linked argument objects */
  126. return;
  127. }
  128. /* Append the argument to the linked argument list */
  129. if (op->common.value.arg) {
  130. /* Append to existing argument list */
  131. prev_arg = op->common.value.arg;
  132. while (prev_arg->common.next) {
  133. prev_arg = prev_arg->common.next;
  134. }
  135. prev_arg->common.next = arg;
  136. } else {
  137. /* No argument list, this will be the first argument */
  138. op->common.value.arg = arg;
  139. }
  140. /* Set the parent in this arg and any args linked after it */
  141. while (arg) {
  142. arg->common.parent = op;
  143. arg = arg->common.next;
  144. op->common.arg_list_length++;
  145. }
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_ps_get_depth_next
  150. *
  151. * PARAMETERS: origin - Root of subtree to search
  152. * op - Last (previous) Op that was found
  153. *
  154. * RETURN: Next Op found in the search.
  155. *
  156. * DESCRIPTION: Get next op in tree (walking the tree in depth-first order)
  157. * Return NULL when reaching "origin" or when walking up from root
  158. *
  159. ******************************************************************************/
  160. union acpi_parse_object *acpi_ps_get_depth_next(union acpi_parse_object *origin,
  161. union acpi_parse_object *op)
  162. {
  163. union acpi_parse_object *next = NULL;
  164. union acpi_parse_object *parent;
  165. union acpi_parse_object *arg;
  166. ACPI_FUNCTION_ENTRY();
  167. if (!op) {
  168. return (NULL);
  169. }
  170. /* Look for an argument or child */
  171. next = acpi_ps_get_arg(op, 0);
  172. if (next) {
  173. return (next);
  174. }
  175. /* Look for a sibling */
  176. next = op->common.next;
  177. if (next) {
  178. return (next);
  179. }
  180. /* Look for a sibling of parent */
  181. parent = op->common.parent;
  182. while (parent) {
  183. arg = acpi_ps_get_arg(parent, 0);
  184. while (arg && (arg != origin) && (arg != op)) {
  185. arg = arg->common.next;
  186. }
  187. if (arg == origin) {
  188. /* Reached parent of origin, end search */
  189. return (NULL);
  190. }
  191. if (parent->common.next) {
  192. /* Found sibling of parent */
  193. return (parent->common.next);
  194. }
  195. op = parent;
  196. parent = parent->common.parent;
  197. }
  198. return (next);
  199. }
  200. #ifdef ACPI_OBSOLETE_FUNCTIONS
  201. /*******************************************************************************
  202. *
  203. * FUNCTION: acpi_ps_get_child
  204. *
  205. * PARAMETERS: op - Get the child of this Op
  206. *
  207. * RETURN: Child Op, Null if none is found.
  208. *
  209. * DESCRIPTION: Get op's children or NULL if none
  210. *
  211. ******************************************************************************/
  212. union acpi_parse_object *acpi_ps_get_child(union acpi_parse_object *op)
  213. {
  214. union acpi_parse_object *child = NULL;
  215. ACPI_FUNCTION_ENTRY();
  216. switch (op->common.aml_opcode) {
  217. case AML_SCOPE_OP:
  218. case AML_ELSE_OP:
  219. case AML_DEVICE_OP:
  220. case AML_THERMAL_ZONE_OP:
  221. case AML_INT_METHODCALL_OP:
  222. child = acpi_ps_get_arg(op, 0);
  223. break;
  224. case AML_BUFFER_OP:
  225. case AML_PACKAGE_OP:
  226. case AML_METHOD_OP:
  227. case AML_IF_OP:
  228. case AML_WHILE_OP:
  229. case AML_FIELD_OP:
  230. child = acpi_ps_get_arg(op, 1);
  231. break;
  232. case AML_POWER_RES_OP:
  233. case AML_INDEX_FIELD_OP:
  234. child = acpi_ps_get_arg(op, 2);
  235. break;
  236. case AML_PROCESSOR_OP:
  237. case AML_BANK_FIELD_OP:
  238. child = acpi_ps_get_arg(op, 3);
  239. break;
  240. default:
  241. /* All others have no children */
  242. break;
  243. }
  244. return (child);
  245. }
  246. #endif