nsutils.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /******************************************************************************
  2. *
  3. * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
  4. * parents and siblings and Scope manipulation
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2008, 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 <acpi/acnamesp.h>
  45. #include <acpi/amlcode.h>
  46. #include <acpi/actables.h>
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME("nsutils")
  49. /* Local prototypes */
  50. static u8 acpi_ns_valid_path_separator(char sep);
  51. #ifdef ACPI_OBSOLETE_FUNCTIONS
  52. acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node *node_to_search);
  53. #endif
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ns_report_error
  57. *
  58. * PARAMETERS: module_name - Caller's module name (for error output)
  59. * line_number - Caller's line number (for error output)
  60. * internal_name - Name or path of the namespace node
  61. * lookup_status - Exception code from NS lookup
  62. *
  63. * RETURN: None
  64. *
  65. * DESCRIPTION: Print warning message with full pathname
  66. *
  67. ******************************************************************************/
  68. void
  69. acpi_ns_report_error(char *module_name,
  70. u32 line_number,
  71. char *internal_name, acpi_status lookup_status)
  72. {
  73. acpi_status status;
  74. u32 bad_name;
  75. char *name = NULL;
  76. acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
  77. if (lookup_status == AE_BAD_CHARACTER) {
  78. /* There is a non-ascii character in the name */
  79. ACPI_MOVE_32_TO_32(&bad_name, internal_name);
  80. acpi_os_printf("[0x%4.4X] (NON-ASCII)", bad_name);
  81. } else {
  82. /* Convert path to external format */
  83. status = acpi_ns_externalize_name(ACPI_UINT32_MAX,
  84. internal_name, NULL, &name);
  85. /* Print target name */
  86. if (ACPI_SUCCESS(status)) {
  87. acpi_os_printf("[%s]", name);
  88. } else {
  89. acpi_os_printf("[COULD NOT EXTERNALIZE NAME]");
  90. }
  91. if (name) {
  92. ACPI_FREE(name);
  93. }
  94. }
  95. acpi_os_printf(" Namespace lookup failure, %s\n",
  96. acpi_format_exception(lookup_status));
  97. }
  98. /*******************************************************************************
  99. *
  100. * FUNCTION: acpi_ns_report_method_error
  101. *
  102. * PARAMETERS: module_name - Caller's module name (for error output)
  103. * line_number - Caller's line number (for error output)
  104. * Message - Error message to use on failure
  105. * prefix_node - Prefix relative to the path
  106. * Path - Path to the node (optional)
  107. * method_status - Execution status
  108. *
  109. * RETURN: None
  110. *
  111. * DESCRIPTION: Print warning message with full pathname
  112. *
  113. ******************************************************************************/
  114. void
  115. acpi_ns_report_method_error(char *module_name,
  116. u32 line_number,
  117. char *message,
  118. struct acpi_namespace_node *prefix_node,
  119. char *path, acpi_status method_status)
  120. {
  121. acpi_status status;
  122. struct acpi_namespace_node *node = prefix_node;
  123. acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
  124. if (path) {
  125. status =
  126. acpi_ns_get_node(prefix_node, path, ACPI_NS_NO_UPSEARCH,
  127. &node);
  128. if (ACPI_FAILURE(status)) {
  129. acpi_os_printf("[Could not get node by pathname]");
  130. }
  131. }
  132. acpi_ns_print_node_pathname(node, message);
  133. acpi_os_printf(", %s\n", acpi_format_exception(method_status));
  134. }
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_ns_print_node_pathname
  138. *
  139. * PARAMETERS: Node - Object
  140. * Message - Prefix message
  141. *
  142. * DESCRIPTION: Print an object's full namespace pathname
  143. * Manages allocation/freeing of a pathname buffer
  144. *
  145. ******************************************************************************/
  146. void
  147. acpi_ns_print_node_pathname(struct acpi_namespace_node *node, char *message)
  148. {
  149. struct acpi_buffer buffer;
  150. acpi_status status;
  151. if (!node) {
  152. acpi_os_printf("[NULL NAME]");
  153. return;
  154. }
  155. /* Convert handle to full pathname and print it (with supplied message) */
  156. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  157. status = acpi_ns_handle_to_pathname(node, &buffer);
  158. if (ACPI_SUCCESS(status)) {
  159. if (message) {
  160. acpi_os_printf("%s ", message);
  161. }
  162. acpi_os_printf("[%s] (Node %p)", (char *)buffer.pointer, node);
  163. ACPI_FREE(buffer.pointer);
  164. }
  165. }
  166. /*******************************************************************************
  167. *
  168. * FUNCTION: acpi_ns_valid_root_prefix
  169. *
  170. * PARAMETERS: Prefix - Character to be checked
  171. *
  172. * RETURN: TRUE if a valid prefix
  173. *
  174. * DESCRIPTION: Check if a character is a valid ACPI Root prefix
  175. *
  176. ******************************************************************************/
  177. u8 acpi_ns_valid_root_prefix(char prefix)
  178. {
  179. return ((u8) (prefix == '\\'));
  180. }
  181. /*******************************************************************************
  182. *
  183. * FUNCTION: acpi_ns_valid_path_separator
  184. *
  185. * PARAMETERS: Sep - Character to be checked
  186. *
  187. * RETURN: TRUE if a valid path separator
  188. *
  189. * DESCRIPTION: Check if a character is a valid ACPI path separator
  190. *
  191. ******************************************************************************/
  192. static u8 acpi_ns_valid_path_separator(char sep)
  193. {
  194. return ((u8) (sep == '.'));
  195. }
  196. /*******************************************************************************
  197. *
  198. * FUNCTION: acpi_ns_get_type
  199. *
  200. * PARAMETERS: Node - Parent Node to be examined
  201. *
  202. * RETURN: Type field from Node whose handle is passed
  203. *
  204. * DESCRIPTION: Return the type of a Namespace node
  205. *
  206. ******************************************************************************/
  207. acpi_object_type acpi_ns_get_type(struct acpi_namespace_node * node)
  208. {
  209. ACPI_FUNCTION_TRACE(ns_get_type);
  210. if (!node) {
  211. ACPI_WARNING((AE_INFO, "Null Node parameter"));
  212. return_UINT32(ACPI_TYPE_ANY);
  213. }
  214. return_UINT32((acpi_object_type) node->type);
  215. }
  216. /*******************************************************************************
  217. *
  218. * FUNCTION: acpi_ns_local
  219. *
  220. * PARAMETERS: Type - A namespace object type
  221. *
  222. * RETURN: LOCAL if names must be found locally in objects of the
  223. * passed type, 0 if enclosing scopes should be searched
  224. *
  225. * DESCRIPTION: Returns scope rule for the given object type.
  226. *
  227. ******************************************************************************/
  228. u32 acpi_ns_local(acpi_object_type type)
  229. {
  230. ACPI_FUNCTION_TRACE(ns_local);
  231. if (!acpi_ut_valid_object_type(type)) {
  232. /* Type code out of range */
  233. ACPI_WARNING((AE_INFO, "Invalid Object Type %X", type));
  234. return_UINT32(ACPI_NS_NORMAL);
  235. }
  236. return_UINT32((u32) acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
  237. }
  238. /*******************************************************************************
  239. *
  240. * FUNCTION: acpi_ns_get_internal_name_length
  241. *
  242. * PARAMETERS: Info - Info struct initialized with the
  243. * external name pointer.
  244. *
  245. * RETURN: None
  246. *
  247. * DESCRIPTION: Calculate the length of the internal (AML) namestring
  248. * corresponding to the external (ASL) namestring.
  249. *
  250. ******************************************************************************/
  251. void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
  252. {
  253. char *next_external_char;
  254. u32 i;
  255. ACPI_FUNCTION_ENTRY();
  256. next_external_char = info->external_name;
  257. info->num_carats = 0;
  258. info->num_segments = 0;
  259. info->fully_qualified = FALSE;
  260. /*
  261. * For the internal name, the required length is 4 bytes per segment, plus
  262. * 1 each for root_prefix, multi_name_prefix_op, segment count, trailing null
  263. * (which is not really needed, but no there's harm in putting it there)
  264. *
  265. * strlen() + 1 covers the first name_seg, which has no path separator
  266. */
  267. if (acpi_ns_valid_root_prefix(next_external_char[0])) {
  268. info->fully_qualified = TRUE;
  269. next_external_char++;
  270. } else {
  271. /*
  272. * Handle Carat prefixes
  273. */
  274. while (*next_external_char == '^') {
  275. info->num_carats++;
  276. next_external_char++;
  277. }
  278. }
  279. /*
  280. * Determine the number of ACPI name "segments" by counting the number of
  281. * path separators within the string. Start with one segment since the
  282. * segment count is [(# separators) + 1], and zero separators is ok.
  283. */
  284. if (*next_external_char) {
  285. info->num_segments = 1;
  286. for (i = 0; next_external_char[i]; i++) {
  287. if (acpi_ns_valid_path_separator(next_external_char[i])) {
  288. info->num_segments++;
  289. }
  290. }
  291. }
  292. info->length = (ACPI_NAME_SIZE * info->num_segments) +
  293. 4 + info->num_carats;
  294. info->next_external_char = next_external_char;
  295. }
  296. /*******************************************************************************
  297. *
  298. * FUNCTION: acpi_ns_build_internal_name
  299. *
  300. * PARAMETERS: Info - Info struct fully initialized
  301. *
  302. * RETURN: Status
  303. *
  304. * DESCRIPTION: Construct the internal (AML) namestring
  305. * corresponding to the external (ASL) namestring.
  306. *
  307. ******************************************************************************/
  308. acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
  309. {
  310. u32 num_segments = info->num_segments;
  311. char *internal_name = info->internal_name;
  312. char *external_name = info->next_external_char;
  313. char *result = NULL;
  314. u32 i;
  315. ACPI_FUNCTION_TRACE(ns_build_internal_name);
  316. /* Setup the correct prefixes, counts, and pointers */
  317. if (info->fully_qualified) {
  318. internal_name[0] = '\\';
  319. if (num_segments <= 1) {
  320. result = &internal_name[1];
  321. } else if (num_segments == 2) {
  322. internal_name[1] = AML_DUAL_NAME_PREFIX;
  323. result = &internal_name[2];
  324. } else {
  325. internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
  326. internal_name[2] = (char)num_segments;
  327. result = &internal_name[3];
  328. }
  329. } else {
  330. /*
  331. * Not fully qualified.
  332. * Handle Carats first, then append the name segments
  333. */
  334. i = 0;
  335. if (info->num_carats) {
  336. for (i = 0; i < info->num_carats; i++) {
  337. internal_name[i] = '^';
  338. }
  339. }
  340. if (num_segments <= 1) {
  341. result = &internal_name[i];
  342. } else if (num_segments == 2) {
  343. internal_name[i] = AML_DUAL_NAME_PREFIX;
  344. result = &internal_name[(acpi_size) i + 1];
  345. } else {
  346. internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
  347. internal_name[(acpi_size) i + 1] = (char)num_segments;
  348. result = &internal_name[(acpi_size) i + 2];
  349. }
  350. }
  351. /* Build the name (minus path separators) */
  352. for (; num_segments; num_segments--) {
  353. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  354. if (acpi_ns_valid_path_separator(*external_name) ||
  355. (*external_name == 0)) {
  356. /* Pad the segment with underscore(s) if segment is short */
  357. result[i] = '_';
  358. } else {
  359. /* Convert the character to uppercase and save it */
  360. result[i] =
  361. (char)ACPI_TOUPPER((int)*external_name);
  362. external_name++;
  363. }
  364. }
  365. /* Now we must have a path separator, or the pathname is bad */
  366. if (!acpi_ns_valid_path_separator(*external_name) &&
  367. (*external_name != 0)) {
  368. return_ACPI_STATUS(AE_BAD_PARAMETER);
  369. }
  370. /* Move on the next segment */
  371. external_name++;
  372. result += ACPI_NAME_SIZE;
  373. }
  374. /* Terminate the string */
  375. *result = 0;
  376. if (info->fully_qualified) {
  377. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  378. "Returning [%p] (abs) \"\\%s\"\n",
  379. internal_name, internal_name));
  380. } else {
  381. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
  382. internal_name, internal_name));
  383. }
  384. return_ACPI_STATUS(AE_OK);
  385. }
  386. /*******************************************************************************
  387. *
  388. * FUNCTION: acpi_ns_internalize_name
  389. *
  390. * PARAMETERS: *external_name - External representation of name
  391. * **Converted Name - Where to return the resulting
  392. * internal represention of the name
  393. *
  394. * RETURN: Status
  395. *
  396. * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
  397. * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
  398. *
  399. *******************************************************************************/
  400. acpi_status acpi_ns_internalize_name(char *external_name, char **converted_name)
  401. {
  402. char *internal_name;
  403. struct acpi_namestring_info info;
  404. acpi_status status;
  405. ACPI_FUNCTION_TRACE(ns_internalize_name);
  406. if ((!external_name) || (*external_name == 0) || (!converted_name)) {
  407. return_ACPI_STATUS(AE_BAD_PARAMETER);
  408. }
  409. /* Get the length of the new internal name */
  410. info.external_name = external_name;
  411. acpi_ns_get_internal_name_length(&info);
  412. /* We need a segment to store the internal name */
  413. internal_name = ACPI_ALLOCATE_ZEROED(info.length);
  414. if (!internal_name) {
  415. return_ACPI_STATUS(AE_NO_MEMORY);
  416. }
  417. /* Build the name */
  418. info.internal_name = internal_name;
  419. status = acpi_ns_build_internal_name(&info);
  420. if (ACPI_FAILURE(status)) {
  421. ACPI_FREE(internal_name);
  422. return_ACPI_STATUS(status);
  423. }
  424. *converted_name = internal_name;
  425. return_ACPI_STATUS(AE_OK);
  426. }
  427. /*******************************************************************************
  428. *
  429. * FUNCTION: acpi_ns_externalize_name
  430. *
  431. * PARAMETERS: internal_name_length - Lenth of the internal name below
  432. * internal_name - Internal representation of name
  433. * converted_name_length - Where the length is returned
  434. * converted_name - Where the resulting external name
  435. * is returned
  436. *
  437. * RETURN: Status
  438. *
  439. * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
  440. * to its external (printable) form (e.g. "\_PR_.CPU0")
  441. *
  442. ******************************************************************************/
  443. acpi_status
  444. acpi_ns_externalize_name(u32 internal_name_length,
  445. char *internal_name,
  446. u32 * converted_name_length, char **converted_name)
  447. {
  448. u32 names_index = 0;
  449. u32 num_segments = 0;
  450. u32 required_length;
  451. u32 prefix_length = 0;
  452. u32 i = 0;
  453. u32 j = 0;
  454. ACPI_FUNCTION_TRACE(ns_externalize_name);
  455. if (!internal_name_length || !internal_name || !converted_name) {
  456. return_ACPI_STATUS(AE_BAD_PARAMETER);
  457. }
  458. /*
  459. * Check for a prefix (one '\' | one or more '^').
  460. */
  461. switch (internal_name[0]) {
  462. case '\\':
  463. prefix_length = 1;
  464. break;
  465. case '^':
  466. for (i = 0; i < internal_name_length; i++) {
  467. if (internal_name[i] == '^') {
  468. prefix_length = i + 1;
  469. } else {
  470. break;
  471. }
  472. }
  473. if (i == internal_name_length) {
  474. prefix_length = i;
  475. }
  476. break;
  477. default:
  478. break;
  479. }
  480. /*
  481. * Check for object names. Note that there could be 0-255 of these
  482. * 4-byte elements.
  483. */
  484. if (prefix_length < internal_name_length) {
  485. switch (internal_name[prefix_length]) {
  486. case AML_MULTI_NAME_PREFIX_OP:
  487. /* <count> 4-byte names */
  488. names_index = prefix_length + 2;
  489. num_segments = (u8)
  490. internal_name[(acpi_size) prefix_length + 1];
  491. break;
  492. case AML_DUAL_NAME_PREFIX:
  493. /* Two 4-byte names */
  494. names_index = prefix_length + 1;
  495. num_segments = 2;
  496. break;
  497. case 0:
  498. /* null_name */
  499. names_index = 0;
  500. num_segments = 0;
  501. break;
  502. default:
  503. /* one 4-byte name */
  504. names_index = prefix_length;
  505. num_segments = 1;
  506. break;
  507. }
  508. }
  509. /*
  510. * Calculate the length of converted_name, which equals the length
  511. * of the prefix, length of all object names, length of any required
  512. * punctuation ('.') between object names, plus the NULL terminator.
  513. */
  514. required_length = prefix_length + (4 * num_segments) +
  515. ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
  516. /*
  517. * Check to see if we're still in bounds. If not, there's a problem
  518. * with internal_name (invalid format).
  519. */
  520. if (required_length > internal_name_length) {
  521. ACPI_ERROR((AE_INFO, "Invalid internal name"));
  522. return_ACPI_STATUS(AE_BAD_PATHNAME);
  523. }
  524. /*
  525. * Build converted_name
  526. */
  527. *converted_name = ACPI_ALLOCATE_ZEROED(required_length);
  528. if (!(*converted_name)) {
  529. return_ACPI_STATUS(AE_NO_MEMORY);
  530. }
  531. j = 0;
  532. for (i = 0; i < prefix_length; i++) {
  533. (*converted_name)[j++] = internal_name[i];
  534. }
  535. if (num_segments > 0) {
  536. for (i = 0; i < num_segments; i++) {
  537. if (i > 0) {
  538. (*converted_name)[j++] = '.';
  539. }
  540. (*converted_name)[j++] = internal_name[names_index++];
  541. (*converted_name)[j++] = internal_name[names_index++];
  542. (*converted_name)[j++] = internal_name[names_index++];
  543. (*converted_name)[j++] = internal_name[names_index++];
  544. }
  545. }
  546. if (converted_name_length) {
  547. *converted_name_length = (u32) required_length;
  548. }
  549. return_ACPI_STATUS(AE_OK);
  550. }
  551. /*******************************************************************************
  552. *
  553. * FUNCTION: acpi_ns_map_handle_to_node
  554. *
  555. * PARAMETERS: Handle - Handle to be converted to an Node
  556. *
  557. * RETURN: A Name table entry pointer
  558. *
  559. * DESCRIPTION: Convert a namespace handle to a real Node
  560. *
  561. * Note: Real integer handles would allow for more verification
  562. * and keep all pointers within this subsystem - however this introduces
  563. * more (and perhaps unnecessary) overhead.
  564. *
  565. ******************************************************************************/
  566. struct acpi_namespace_node *acpi_ns_map_handle_to_node(acpi_handle handle)
  567. {
  568. ACPI_FUNCTION_ENTRY();
  569. /*
  570. * Simple implementation
  571. */
  572. if ((!handle) || (handle == ACPI_ROOT_OBJECT)) {
  573. return (acpi_gbl_root_node);
  574. }
  575. /* We can at least attempt to verify the handle */
  576. if (ACPI_GET_DESCRIPTOR_TYPE(handle) != ACPI_DESC_TYPE_NAMED) {
  577. return (NULL);
  578. }
  579. return (ACPI_CAST_PTR(struct acpi_namespace_node, handle));
  580. }
  581. /*******************************************************************************
  582. *
  583. * FUNCTION: acpi_ns_convert_entry_to_handle
  584. *
  585. * PARAMETERS: Node - Node to be converted to a Handle
  586. *
  587. * RETURN: A user handle
  588. *
  589. * DESCRIPTION: Convert a real Node to a namespace handle
  590. *
  591. ******************************************************************************/
  592. acpi_handle acpi_ns_convert_entry_to_handle(struct acpi_namespace_node *node)
  593. {
  594. /*
  595. * Simple implementation for now;
  596. */
  597. return ((acpi_handle) node);
  598. /* Example future implementation ---------------------
  599. if (!Node)
  600. {
  601. return (NULL);
  602. }
  603. if (Node == acpi_gbl_root_node)
  604. {
  605. return (ACPI_ROOT_OBJECT);
  606. }
  607. return ((acpi_handle) Node);
  608. ------------------------------------------------------*/
  609. }
  610. /*******************************************************************************
  611. *
  612. * FUNCTION: acpi_ns_terminate
  613. *
  614. * PARAMETERS: none
  615. *
  616. * RETURN: none
  617. *
  618. * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
  619. *
  620. ******************************************************************************/
  621. void acpi_ns_terminate(void)
  622. {
  623. union acpi_operand_object *obj_desc;
  624. ACPI_FUNCTION_TRACE(ns_terminate);
  625. /*
  626. * 1) Free the entire namespace -- all nodes and objects
  627. *
  628. * Delete all object descriptors attached to namepsace nodes
  629. */
  630. acpi_ns_delete_namespace_subtree(acpi_gbl_root_node);
  631. /* Detach any objects attached to the root */
  632. obj_desc = acpi_ns_get_attached_object(acpi_gbl_root_node);
  633. if (obj_desc) {
  634. acpi_ns_detach_object(acpi_gbl_root_node);
  635. }
  636. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Namespace freed\n"));
  637. return_VOID;
  638. }
  639. /*******************************************************************************
  640. *
  641. * FUNCTION: acpi_ns_opens_scope
  642. *
  643. * PARAMETERS: Type - A valid namespace type
  644. *
  645. * RETURN: NEWSCOPE if the passed type "opens a name scope" according
  646. * to the ACPI specification, else 0
  647. *
  648. ******************************************************************************/
  649. u32 acpi_ns_opens_scope(acpi_object_type type)
  650. {
  651. ACPI_FUNCTION_TRACE_STR(ns_opens_scope, acpi_ut_get_type_name(type));
  652. if (!acpi_ut_valid_object_type(type)) {
  653. /* type code out of range */
  654. ACPI_WARNING((AE_INFO, "Invalid Object Type %X", type));
  655. return_UINT32(ACPI_NS_NORMAL);
  656. }
  657. return_UINT32(((u32) acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
  658. }
  659. /*******************************************************************************
  660. *
  661. * FUNCTION: acpi_ns_get_node
  662. *
  663. * PARAMETERS: *Pathname - Name to be found, in external (ASL) format. The
  664. * \ (backslash) and ^ (carat) prefixes, and the
  665. * . (period) to separate segments are supported.
  666. * prefix_node - Root of subtree to be searched, or NS_ALL for the
  667. * root of the name space. If Name is fully
  668. * qualified (first s8 is '\'), the passed value
  669. * of Scope will not be accessed.
  670. * Flags - Used to indicate whether to perform upsearch or
  671. * not.
  672. * return_node - Where the Node is returned
  673. *
  674. * DESCRIPTION: Look up a name relative to a given scope and return the
  675. * corresponding Node. NOTE: Scope can be null.
  676. *
  677. * MUTEX: Locks namespace
  678. *
  679. ******************************************************************************/
  680. acpi_status
  681. acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
  682. char *pathname,
  683. u32 flags, struct acpi_namespace_node **return_node)
  684. {
  685. union acpi_generic_state scope_info;
  686. acpi_status status;
  687. char *internal_path;
  688. ACPI_FUNCTION_TRACE_PTR(ns_get_node, pathname);
  689. if (!pathname) {
  690. *return_node = prefix_node;
  691. if (!prefix_node) {
  692. *return_node = acpi_gbl_root_node;
  693. }
  694. return_ACPI_STATUS(AE_OK);
  695. }
  696. /* Convert path to internal representation */
  697. status = acpi_ns_internalize_name(pathname, &internal_path);
  698. if (ACPI_FAILURE(status)) {
  699. return_ACPI_STATUS(status);
  700. }
  701. /* Must lock namespace during lookup */
  702. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  703. if (ACPI_FAILURE(status)) {
  704. goto cleanup;
  705. }
  706. /* Setup lookup scope (search starting point) */
  707. scope_info.scope.node = prefix_node;
  708. /* Lookup the name in the namespace */
  709. status = acpi_ns_lookup(&scope_info, internal_path, ACPI_TYPE_ANY,
  710. ACPI_IMODE_EXECUTE,
  711. (flags | ACPI_NS_DONT_OPEN_SCOPE), NULL,
  712. return_node);
  713. if (ACPI_FAILURE(status)) {
  714. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s, %s\n",
  715. pathname, acpi_format_exception(status)));
  716. }
  717. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  718. cleanup:
  719. ACPI_FREE(internal_path);
  720. return_ACPI_STATUS(status);
  721. }
  722. /*******************************************************************************
  723. *
  724. * FUNCTION: acpi_ns_get_parent_node
  725. *
  726. * PARAMETERS: Node - Current table entry
  727. *
  728. * RETURN: Parent entry of the given entry
  729. *
  730. * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
  731. *
  732. ******************************************************************************/
  733. struct acpi_namespace_node *acpi_ns_get_parent_node(struct acpi_namespace_node
  734. *node)
  735. {
  736. ACPI_FUNCTION_ENTRY();
  737. if (!node) {
  738. return (NULL);
  739. }
  740. /*
  741. * Walk to the end of this peer list. The last entry is marked with a flag
  742. * and the peer pointer is really a pointer back to the parent. This saves
  743. * putting a parent back pointer in each and every named object!
  744. */
  745. while (!(node->flags & ANOBJ_END_OF_PEER_LIST)) {
  746. node = node->peer;
  747. }
  748. return (node->peer);
  749. }
  750. /*******************************************************************************
  751. *
  752. * FUNCTION: acpi_ns_get_next_valid_node
  753. *
  754. * PARAMETERS: Node - Current table entry
  755. *
  756. * RETURN: Next valid Node in the linked node list. NULL if no more valid
  757. * nodes.
  758. *
  759. * DESCRIPTION: Find the next valid node within a name table.
  760. * Useful for implementing NULL-end-of-list loops.
  761. *
  762. ******************************************************************************/
  763. struct acpi_namespace_node *acpi_ns_get_next_valid_node(struct
  764. acpi_namespace_node
  765. *node)
  766. {
  767. /* If we are at the end of this peer list, return NULL */
  768. if (node->flags & ANOBJ_END_OF_PEER_LIST) {
  769. return NULL;
  770. }
  771. /* Otherwise just return the next peer */
  772. return (node->peer);
  773. }
  774. #ifdef ACPI_OBSOLETE_FUNCTIONS
  775. /*******************************************************************************
  776. *
  777. * FUNCTION: acpi_ns_find_parent_name
  778. *
  779. * PARAMETERS: *child_node - Named Obj whose name is to be found
  780. *
  781. * RETURN: The ACPI name
  782. *
  783. * DESCRIPTION: Search for the given obj in its parent scope and return the
  784. * name segment, or "????" if the parent name can't be found
  785. * (which "should not happen").
  786. *
  787. ******************************************************************************/
  788. acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node * child_node)
  789. {
  790. struct acpi_namespace_node *parent_node;
  791. ACPI_FUNCTION_TRACE(ns_find_parent_name);
  792. if (child_node) {
  793. /* Valid entry. Get the parent Node */
  794. parent_node = acpi_ns_get_parent_node(child_node);
  795. if (parent_node) {
  796. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  797. "Parent of %p [%4.4s] is %p [%4.4s]\n",
  798. child_node,
  799. acpi_ut_get_node_name(child_node),
  800. parent_node,
  801. acpi_ut_get_node_name(parent_node)));
  802. if (parent_node->name.integer) {
  803. return_VALUE((acpi_name) parent_node->name.
  804. integer);
  805. }
  806. }
  807. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  808. "Unable to find parent of %p (%4.4s)\n",
  809. child_node,
  810. acpi_ut_get_node_name(child_node)));
  811. }
  812. return_VALUE(ACPI_UNKNOWN_NAME);
  813. }
  814. #endif