utmisc.c 25 KB

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