utmisc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmisc - common utility procedures
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/acnamesp.h>
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utmisc")
  46. /*******************************************************************************
  47. *
  48. * FUNCTION: acpi_ut_allocate_owner_id
  49. *
  50. * PARAMETERS: owner_id - Where the new owner ID is returned
  51. *
  52. * RETURN: Status
  53. *
  54. * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
  55. * track objects created by the table or method, to be deleted
  56. * when the method exits or the table is unloaded.
  57. *
  58. ******************************************************************************/
  59. acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
  60. {
  61. acpi_native_uint i;
  62. acpi_native_uint j;
  63. acpi_native_uint k;
  64. acpi_status status;
  65. ACPI_FUNCTION_TRACE("ut_allocate_owner_id");
  66. /* Guard against multiple allocations of ID to the same location */
  67. if (*owner_id) {
  68. ACPI_ERROR((AE_INFO, "Owner ID [%2.2X] already exists",
  69. *owner_id));
  70. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  71. }
  72. /* Mutex for the global ID mask */
  73. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  74. if (ACPI_FAILURE(status)) {
  75. return_ACPI_STATUS(status);
  76. }
  77. /*
  78. * Find a free owner ID, cycle through all possible IDs on repeated
  79. * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
  80. * to be scanned twice.
  81. */
  82. for (i = 0, j = acpi_gbl_last_owner_id_index;
  83. i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
  84. if (j >= ACPI_NUM_OWNERID_MASKS) {
  85. j = 0; /* Wraparound to start of mask array */
  86. }
  87. for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
  88. if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
  89. /* There are no free IDs in this mask */
  90. break;
  91. }
  92. if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
  93. /*
  94. * Found a free ID. The actual ID is the bit index plus one,
  95. * making zero an invalid Owner ID. Save this as the last ID
  96. * allocated and update the global ID mask.
  97. */
  98. acpi_gbl_owner_id_mask[j] |= (1 << k);
  99. acpi_gbl_last_owner_id_index = (u8) j;
  100. acpi_gbl_next_owner_id_offset = (u8) (k + 1);
  101. /*
  102. * Construct encoded ID from the index and bit position
  103. *
  104. * Note: Last [j].k (bit 255) is never used and is marked
  105. * permanently allocated (prevents +1 overflow)
  106. */
  107. *owner_id =
  108. (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
  109. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  110. "Allocated owner_id: %2.2X\n",
  111. (unsigned int)*owner_id));
  112. goto exit;
  113. }
  114. }
  115. acpi_gbl_next_owner_id_offset = 0;
  116. }
  117. /*
  118. * All owner_ids have been allocated. This typically should
  119. * not happen since the IDs are reused after deallocation. The IDs are
  120. * allocated upon table load (one per table) and method execution, and
  121. * they are released when a table is unloaded or a method completes
  122. * execution.
  123. *
  124. * If this error happens, there may be very deep nesting of invoked control
  125. * methods, or there may be a bug where the IDs are not released.
  126. */
  127. status = AE_OWNER_ID_LIMIT;
  128. ACPI_ERROR((AE_INFO,
  129. "Could not allocate new owner_id (255 max), AE_OWNER_ID_LIMIT"));
  130. exit:
  131. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  132. return_ACPI_STATUS(status);
  133. }
  134. /*******************************************************************************
  135. *
  136. * FUNCTION: acpi_ut_release_owner_id
  137. *
  138. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
  139. *
  140. * RETURN: None. No error is returned because we are either exiting a
  141. * control method or unloading a table. Either way, we would
  142. * ignore any error anyway.
  143. *
  144. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  145. *
  146. ******************************************************************************/
  147. void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
  148. {
  149. acpi_owner_id owner_id = *owner_id_ptr;
  150. acpi_status status;
  151. acpi_native_uint index;
  152. u32 bit;
  153. ACPI_FUNCTION_TRACE_U32("ut_release_owner_id", owner_id);
  154. /* Always clear the input owner_id (zero is an invalid ID) */
  155. *owner_id_ptr = 0;
  156. /* Zero is not a valid owner_iD */
  157. if (owner_id == 0) {
  158. ACPI_ERROR((AE_INFO, "Invalid owner_id: %2.2X", owner_id));
  159. return_VOID;
  160. }
  161. /* Mutex for the global ID mask */
  162. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  163. if (ACPI_FAILURE(status)) {
  164. return_VOID;
  165. }
  166. /* Normalize the ID to zero */
  167. owner_id--;
  168. /* Decode ID to index/offset pair */
  169. index = ACPI_DIV_32(owner_id);
  170. bit = 1 << ACPI_MOD_32(owner_id);
  171. /* Free the owner ID only if it is valid */
  172. if (acpi_gbl_owner_id_mask[index] & bit) {
  173. acpi_gbl_owner_id_mask[index] ^= bit;
  174. } else {
  175. ACPI_ERROR((AE_INFO,
  176. "Release of non-allocated owner_id: %2.2X",
  177. owner_id + 1));
  178. }
  179. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  180. return_VOID;
  181. }
  182. /*******************************************************************************
  183. *
  184. * FUNCTION: acpi_ut_strupr (strupr)
  185. *
  186. * PARAMETERS: src_string - The source string to convert
  187. *
  188. * RETURN: None
  189. *
  190. * DESCRIPTION: Convert string to uppercase
  191. *
  192. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  193. *
  194. ******************************************************************************/
  195. void acpi_ut_strupr(char *src_string)
  196. {
  197. char *string;
  198. ACPI_FUNCTION_ENTRY();
  199. if (!src_string) {
  200. return;
  201. }
  202. /* Walk entire string, uppercasing the letters */
  203. for (string = src_string; *string; string++) {
  204. *string = (char)ACPI_TOUPPER(*string);
  205. }
  206. return;
  207. }
  208. /*******************************************************************************
  209. *
  210. * FUNCTION: acpi_ut_print_string
  211. *
  212. * PARAMETERS: String - Null terminated ASCII string
  213. * max_length - Maximum output length
  214. *
  215. * RETURN: None
  216. *
  217. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  218. * sequences.
  219. *
  220. ******************************************************************************/
  221. void acpi_ut_print_string(char *string, u8 max_length)
  222. {
  223. u32 i;
  224. if (!string) {
  225. acpi_os_printf("<\"NULL STRING PTR\">");
  226. return;
  227. }
  228. acpi_os_printf("\"");
  229. for (i = 0; string[i] && (i < max_length); i++) {
  230. /* Escape sequences */
  231. switch (string[i]) {
  232. case 0x07:
  233. acpi_os_printf("\\a"); /* BELL */
  234. break;
  235. case 0x08:
  236. acpi_os_printf("\\b"); /* BACKSPACE */
  237. break;
  238. case 0x0C:
  239. acpi_os_printf("\\f"); /* FORMFEED */
  240. break;
  241. case 0x0A:
  242. acpi_os_printf("\\n"); /* LINEFEED */
  243. break;
  244. case 0x0D:
  245. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  246. break;
  247. case 0x09:
  248. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  249. break;
  250. case 0x0B:
  251. acpi_os_printf("\\v"); /* VERTICAL TAB */
  252. break;
  253. case '\'': /* Single Quote */
  254. case '\"': /* Double Quote */
  255. case '\\': /* Backslash */
  256. acpi_os_printf("\\%c", (int)string[i]);
  257. break;
  258. default:
  259. /* Check for printable character or hex escape */
  260. if (ACPI_IS_PRINT(string[i])) {
  261. /* This is a normal character */
  262. acpi_os_printf("%c", (int)string[i]);
  263. } else {
  264. /* All others will be Hex escapes */
  265. acpi_os_printf("\\x%2.2X", (s32) string[i]);
  266. }
  267. break;
  268. }
  269. }
  270. acpi_os_printf("\"");
  271. if (i == max_length && string[i]) {
  272. acpi_os_printf("...");
  273. }
  274. }
  275. /*******************************************************************************
  276. *
  277. * FUNCTION: acpi_ut_dword_byte_swap
  278. *
  279. * PARAMETERS: Value - Value to be converted
  280. *
  281. * RETURN: u32 integer with bytes swapped
  282. *
  283. * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
  284. *
  285. ******************************************************************************/
  286. u32 acpi_ut_dword_byte_swap(u32 value)
  287. {
  288. union {
  289. u32 value;
  290. u8 bytes[4];
  291. } out;
  292. union {
  293. u32 value;
  294. u8 bytes[4];
  295. } in;
  296. ACPI_FUNCTION_ENTRY();
  297. in.value = value;
  298. out.bytes[0] = in.bytes[3];
  299. out.bytes[1] = in.bytes[2];
  300. out.bytes[2] = in.bytes[1];
  301. out.bytes[3] = in.bytes[0];
  302. return (out.value);
  303. }
  304. /*******************************************************************************
  305. *
  306. * FUNCTION: acpi_ut_set_integer_width
  307. *
  308. * PARAMETERS: Revision From DSDT header
  309. *
  310. * RETURN: None
  311. *
  312. * DESCRIPTION: Set the global integer bit width based upon the revision
  313. * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
  314. * For Revision 2 and above, Integers are 64 bits. Yes, this
  315. * makes a difference.
  316. *
  317. ******************************************************************************/
  318. void acpi_ut_set_integer_width(u8 revision)
  319. {
  320. if (revision <= 1) {
  321. acpi_gbl_integer_bit_width = 32;
  322. acpi_gbl_integer_nybble_width = 8;
  323. acpi_gbl_integer_byte_width = 4;
  324. } else {
  325. acpi_gbl_integer_bit_width = 64;
  326. acpi_gbl_integer_nybble_width = 16;
  327. acpi_gbl_integer_byte_width = 8;
  328. }
  329. }
  330. #ifdef ACPI_DEBUG_OUTPUT
  331. /*******************************************************************************
  332. *
  333. * FUNCTION: acpi_ut_display_init_pathname
  334. *
  335. * PARAMETERS: Type - Object type of the node
  336. * obj_handle - Handle whose pathname will be displayed
  337. * Path - Additional path string to be appended.
  338. * (NULL if no extra path)
  339. *
  340. * RETURN: acpi_status
  341. *
  342. * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
  343. *
  344. ******************************************************************************/
  345. void
  346. acpi_ut_display_init_pathname(u8 type,
  347. struct acpi_namespace_node *obj_handle,
  348. char *path)
  349. {
  350. acpi_status status;
  351. struct acpi_buffer buffer;
  352. ACPI_FUNCTION_ENTRY();
  353. /* Only print the path if the appropriate debug level is enabled */
  354. if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
  355. return;
  356. }
  357. /* Get the full pathname to the node */
  358. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  359. status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
  360. if (ACPI_FAILURE(status)) {
  361. return;
  362. }
  363. /* Print what we're doing */
  364. switch (type) {
  365. case ACPI_TYPE_METHOD:
  366. acpi_os_printf("Executing ");
  367. break;
  368. default:
  369. acpi_os_printf("Initializing ");
  370. break;
  371. }
  372. /* Print the object type and pathname */
  373. acpi_os_printf("%-12s %s",
  374. acpi_ut_get_type_name(type), (char *)buffer.pointer);
  375. /* Extra path is used to append names like _STA, _INI, etc. */
  376. if (path) {
  377. acpi_os_printf(".%s", path);
  378. }
  379. acpi_os_printf("\n");
  380. ACPI_MEM_FREE(buffer.pointer);
  381. }
  382. #endif
  383. /*******************************************************************************
  384. *
  385. * FUNCTION: acpi_ut_valid_acpi_name
  386. *
  387. * PARAMETERS: Name - The name to be examined
  388. *
  389. * RETURN: TRUE if the name is valid, FALSE otherwise
  390. *
  391. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  392. * 1) Upper case alpha
  393. * 2) numeric
  394. * 3) underscore
  395. *
  396. ******************************************************************************/
  397. u8 acpi_ut_valid_acpi_name(u32 name)
  398. {
  399. char *name_ptr = (char *)&name;
  400. char character;
  401. acpi_native_uint i;
  402. ACPI_FUNCTION_ENTRY();
  403. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  404. character = *name_ptr;
  405. name_ptr++;
  406. if (!((character == '_') ||
  407. (character >= 'A' && character <= 'Z') ||
  408. (character >= '0' && character <= '9'))) {
  409. return (FALSE);
  410. }
  411. }
  412. return (TRUE);
  413. }
  414. /*******************************************************************************
  415. *
  416. * FUNCTION: acpi_ut_valid_acpi_character
  417. *
  418. * PARAMETERS: Character - The character to be examined
  419. *
  420. * RETURN: 1 if Character may appear in a name, else 0
  421. *
  422. * DESCRIPTION: Check for a printable character
  423. *
  424. ******************************************************************************/
  425. u8 acpi_ut_valid_acpi_character(char character)
  426. {
  427. ACPI_FUNCTION_ENTRY();
  428. return ((u8) ((character == '_') ||
  429. (character >= 'A' && character <= 'Z') ||
  430. (character >= '0' && character <= '9')));
  431. }
  432. /*******************************************************************************
  433. *
  434. * FUNCTION: acpi_ut_strtoul64
  435. *
  436. * PARAMETERS: String - Null terminated string
  437. * Base - Radix of the string: 10, 16, or ACPI_ANY_BASE
  438. * ret_integer - Where the converted integer is returned
  439. *
  440. * RETURN: Status and Converted value
  441. *
  442. * DESCRIPTION: Convert a string into an unsigned value.
  443. * NOTE: Does not support Octal strings, not needed.
  444. *
  445. ******************************************************************************/
  446. acpi_status
  447. acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
  448. {
  449. u32 this_digit = 0;
  450. acpi_integer return_value = 0;
  451. acpi_integer quotient;
  452. ACPI_FUNCTION_TRACE("ut_stroul64");
  453. if ((!string) || !(*string)) {
  454. goto error_exit;
  455. }
  456. switch (base) {
  457. case ACPI_ANY_BASE:
  458. case 10:
  459. case 16:
  460. break;
  461. default:
  462. /* Invalid Base */
  463. return_ACPI_STATUS(AE_BAD_PARAMETER);
  464. }
  465. /* Skip over any white space in the buffer */
  466. while (ACPI_IS_SPACE(*string) || *string == '\t') {
  467. string++;
  468. }
  469. /*
  470. * If the input parameter Base is zero, then we need to
  471. * determine if it is decimal or hexadecimal:
  472. */
  473. if (base == 0) {
  474. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  475. base = 16;
  476. string += 2;
  477. } else {
  478. base = 10;
  479. }
  480. }
  481. /*
  482. * For hexadecimal base, skip over the leading
  483. * 0 or 0x, if they are present.
  484. */
  485. if ((base == 16) &&
  486. (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  487. string += 2;
  488. }
  489. /* Any string left? */
  490. if (!(*string)) {
  491. goto error_exit;
  492. }
  493. /* Main loop: convert the string to a 64-bit integer */
  494. while (*string) {
  495. if (ACPI_IS_DIGIT(*string)) {
  496. /* Convert ASCII 0-9 to Decimal value */
  497. this_digit = ((u8) * string) - '0';
  498. } else {
  499. if (base == 10) {
  500. /* Digit is out of range */
  501. goto error_exit;
  502. }
  503. this_digit = (u8) ACPI_TOUPPER(*string);
  504. if (ACPI_IS_XDIGIT((char)this_digit)) {
  505. /* Convert ASCII Hex char to value */
  506. this_digit = this_digit - 'A' + 10;
  507. } else {
  508. /*
  509. * We allow non-hex chars, just stop now, same as end-of-string.
  510. * See ACPI spec, string-to-integer conversion.
  511. */
  512. break;
  513. }
  514. }
  515. /* Divide the digit into the correct position */
  516. (void)
  517. acpi_ut_short_divide((ACPI_INTEGER_MAX -
  518. (acpi_integer) this_digit), base,
  519. &quotient, NULL);
  520. if (return_value > quotient) {
  521. goto error_exit;
  522. }
  523. return_value *= base;
  524. return_value += this_digit;
  525. string++;
  526. }
  527. /* All done, normal exit */
  528. *ret_integer = return_value;
  529. return_ACPI_STATUS(AE_OK);
  530. error_exit:
  531. /* Base was set/validated above */
  532. if (base == 10) {
  533. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  534. } else {
  535. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  536. }
  537. }
  538. /*******************************************************************************
  539. *
  540. * FUNCTION: acpi_ut_create_update_state_and_push
  541. *
  542. * PARAMETERS: Object - Object to be added to the new state
  543. * Action - Increment/Decrement
  544. * state_list - List the state will be added to
  545. *
  546. * RETURN: Status
  547. *
  548. * DESCRIPTION: Create a new state and push it
  549. *
  550. ******************************************************************************/
  551. acpi_status
  552. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  553. u16 action,
  554. union acpi_generic_state **state_list)
  555. {
  556. union acpi_generic_state *state;
  557. ACPI_FUNCTION_ENTRY();
  558. /* Ignore null objects; these are expected */
  559. if (!object) {
  560. return (AE_OK);
  561. }
  562. state = acpi_ut_create_update_state(object, action);
  563. if (!state) {
  564. return (AE_NO_MEMORY);
  565. }
  566. acpi_ut_push_generic_state(state_list, state);
  567. return (AE_OK);
  568. }
  569. /*******************************************************************************
  570. *
  571. * FUNCTION: acpi_ut_walk_package_tree
  572. *
  573. * PARAMETERS: source_object - The package to walk
  574. * target_object - Target object (if package is being copied)
  575. * walk_callback - Called once for each package element
  576. * Context - Passed to the callback function
  577. *
  578. * RETURN: Status
  579. *
  580. * DESCRIPTION: Walk through a package
  581. *
  582. ******************************************************************************/
  583. acpi_status
  584. acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
  585. void *target_object,
  586. acpi_pkg_callback walk_callback, void *context)
  587. {
  588. acpi_status status = AE_OK;
  589. union acpi_generic_state *state_list = NULL;
  590. union acpi_generic_state *state;
  591. u32 this_index;
  592. union acpi_operand_object *this_source_obj;
  593. ACPI_FUNCTION_TRACE("ut_walk_package_tree");
  594. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  595. if (!state) {
  596. return_ACPI_STATUS(AE_NO_MEMORY);
  597. }
  598. while (state) {
  599. /* Get one element of the package */
  600. this_index = state->pkg.index;
  601. this_source_obj = (union acpi_operand_object *)
  602. state->pkg.source_object->package.elements[this_index];
  603. /*
  604. * Check for:
  605. * 1) An uninitialized package element. It is completely
  606. * legal to declare a package and leave it uninitialized
  607. * 2) Not an internal object - can be a namespace node instead
  608. * 3) Any type other than a package. Packages are handled in else
  609. * case below.
  610. */
  611. if ((!this_source_obj) ||
  612. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  613. ACPI_DESC_TYPE_OPERAND)
  614. || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
  615. ACPI_TYPE_PACKAGE)) {
  616. status =
  617. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  618. this_source_obj, state, context);
  619. if (ACPI_FAILURE(status)) {
  620. return_ACPI_STATUS(status);
  621. }
  622. state->pkg.index++;
  623. while (state->pkg.index >=
  624. state->pkg.source_object->package.count) {
  625. /*
  626. * We've handled all of the objects at this level, This means
  627. * that we have just completed a package. That package may
  628. * have contained one or more packages itself.
  629. *
  630. * Delete this state and pop the previous state (package).
  631. */
  632. acpi_ut_delete_generic_state(state);
  633. state = acpi_ut_pop_generic_state(&state_list);
  634. /* Finished when there are no more states */
  635. if (!state) {
  636. /*
  637. * We have handled all of the objects in the top level
  638. * package just add the length of the package objects
  639. * and exit
  640. */
  641. return_ACPI_STATUS(AE_OK);
  642. }
  643. /*
  644. * Go back up a level and move the index past the just
  645. * completed package object.
  646. */
  647. state->pkg.index++;
  648. }
  649. } else {
  650. /* This is a subobject of type package */
  651. status =
  652. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  653. this_source_obj, state, context);
  654. if (ACPI_FAILURE(status)) {
  655. return_ACPI_STATUS(status);
  656. }
  657. /*
  658. * Push the current state and create a new one
  659. * The callback above returned a new target package object.
  660. */
  661. acpi_ut_push_generic_state(&state_list, state);
  662. state = acpi_ut_create_pkg_state(this_source_obj,
  663. state->pkg.
  664. this_target_obj, 0);
  665. if (!state) {
  666. return_ACPI_STATUS(AE_NO_MEMORY);
  667. }
  668. }
  669. }
  670. /* We should never get here */
  671. return_ACPI_STATUS(AE_AML_INTERNAL);
  672. }
  673. /*******************************************************************************
  674. *
  675. * FUNCTION: acpi_ut_generate_checksum
  676. *
  677. * PARAMETERS: Buffer - Buffer to be scanned
  678. * Length - number of bytes to examine
  679. *
  680. * RETURN: The generated checksum
  681. *
  682. * DESCRIPTION: Generate a checksum on a raw buffer
  683. *
  684. ******************************************************************************/
  685. u8 acpi_ut_generate_checksum(u8 * buffer, u32 length)
  686. {
  687. u32 i;
  688. signed char sum = 0;
  689. for (i = 0; i < length; i++) {
  690. sum = (signed char)(sum + buffer[i]);
  691. }
  692. return ((u8) (0 - sum));
  693. }
  694. /*******************************************************************************
  695. *
  696. * FUNCTION: acpi_ut_error, acpi_ut_warning, acpi_ut_info
  697. *
  698. * PARAMETERS: module_name - Caller's module name (for error output)
  699. * line_number - Caller's line number (for error output)
  700. * Format - Printf format string + additional args
  701. *
  702. * RETURN: None
  703. *
  704. * DESCRIPTION: Print message with module/line/version info
  705. *
  706. ******************************************************************************/
  707. void ACPI_INTERNAL_VAR_XFACE
  708. acpi_ut_error(char *module_name, u32 line_number, char *format, ...)
  709. {
  710. va_list args;
  711. acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
  712. va_start(args, format);
  713. acpi_os_vprintf(format, args);
  714. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  715. }
  716. void ACPI_INTERNAL_VAR_XFACE
  717. acpi_ut_exception(char *module_name,
  718. u32 line_number, acpi_status status, char *format, ...)
  719. {
  720. va_list args;
  721. acpi_os_printf("ACPI Exception (%s-%04d): %s, ", module_name,
  722. line_number, acpi_format_exception(status));
  723. va_start(args, format);
  724. acpi_os_vprintf(format, args);
  725. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  726. }
  727. void ACPI_INTERNAL_VAR_XFACE
  728. acpi_ut_warning(char *module_name, u32 line_number, char *format, ...)
  729. {
  730. va_list args;
  731. acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
  732. va_start(args, format);
  733. acpi_os_vprintf(format, args);
  734. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  735. }
  736. void ACPI_INTERNAL_VAR_XFACE
  737. acpi_ut_info(char *module_name, u32 line_number, char *format, ...)
  738. {
  739. va_list args;
  740. acpi_os_printf("ACPI (%s-%04d): ", module_name, line_number);
  741. va_start(args, format);
  742. acpi_os_vprintf(format, args);
  743. acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
  744. }
  745. /*******************************************************************************
  746. *
  747. * FUNCTION: acpi_ut_report_error, Warning, Info
  748. *
  749. * PARAMETERS: module_name - Caller's module name (for error output)
  750. * line_number - Caller's line number (for error output)
  751. *
  752. * RETURN: None
  753. *
  754. * DESCRIPTION: Print error message
  755. *
  756. * Note: Legacy only, should be removed when no longer used by drivers.
  757. *
  758. ******************************************************************************/
  759. void acpi_ut_report_error(char *module_name, u32 line_number)
  760. {
  761. acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
  762. }
  763. void acpi_ut_report_warning(char *module_name, u32 line_number)
  764. {
  765. acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
  766. }
  767. void acpi_ut_report_info(char *module_name, u32 line_number)
  768. {
  769. acpi_os_printf("ACPI (%s-%04d): ", module_name, line_number);
  770. }