nsutils.c 26 KB

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