utmisc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmisc - common utility procedures
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <linux/module.h>
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #define _COMPONENT ACPI_UTILITIES
  47. ACPI_MODULE_NAME("utmisc")
  48. /*
  49. * Common suffix for messages
  50. */
  51. #define ACPI_COMMON_MSG_SUFFIX \
  52. acpi_os_printf(" (%8.8X/%s-%u)\n", ACPI_CA_VERSION, module_name, line_number)
  53. /*******************************************************************************
  54. *
  55. * FUNCTION: acpi_ut_validate_exception
  56. *
  57. * PARAMETERS: Status - The acpi_status code to be formatted
  58. *
  59. * RETURN: A string containing the exception text. NULL if exception is
  60. * not valid.
  61. *
  62. * DESCRIPTION: This function validates and translates an ACPI exception into
  63. * an ASCII string.
  64. *
  65. ******************************************************************************/
  66. const char *acpi_ut_validate_exception(acpi_status status)
  67. {
  68. u32 sub_status;
  69. const char *exception = NULL;
  70. ACPI_FUNCTION_ENTRY();
  71. /*
  72. * Status is composed of two parts, a "type" and an actual code
  73. */
  74. sub_status = (status & ~AE_CODE_MASK);
  75. switch (status & AE_CODE_MASK) {
  76. case AE_CODE_ENVIRONMENTAL:
  77. if (sub_status <= AE_CODE_ENV_MAX) {
  78. exception = acpi_gbl_exception_names_env[sub_status];
  79. }
  80. break;
  81. case AE_CODE_PROGRAMMER:
  82. if (sub_status <= AE_CODE_PGM_MAX) {
  83. exception = acpi_gbl_exception_names_pgm[sub_status];
  84. }
  85. break;
  86. case AE_CODE_ACPI_TABLES:
  87. if (sub_status <= AE_CODE_TBL_MAX) {
  88. exception = acpi_gbl_exception_names_tbl[sub_status];
  89. }
  90. break;
  91. case AE_CODE_AML:
  92. if (sub_status <= AE_CODE_AML_MAX) {
  93. exception = acpi_gbl_exception_names_aml[sub_status];
  94. }
  95. break;
  96. case AE_CODE_CONTROL:
  97. if (sub_status <= AE_CODE_CTRL_MAX) {
  98. exception = acpi_gbl_exception_names_ctrl[sub_status];
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. return (ACPI_CAST_PTR(const char, exception));
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_is_pci_root_bridge
  109. *
  110. * PARAMETERS: Id - The HID/CID in string format
  111. *
  112. * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
  113. *
  114. * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
  115. *
  116. ******************************************************************************/
  117. u8 acpi_ut_is_pci_root_bridge(char *id)
  118. {
  119. /*
  120. * Check if this is a PCI root bridge.
  121. * ACPI 3.0+: check for a PCI Express root also.
  122. */
  123. if (!(ACPI_STRCMP(id,
  124. PCI_ROOT_HID_STRING)) ||
  125. !(ACPI_STRCMP(id, PCI_EXPRESS_ROOT_HID_STRING))) {
  126. return (TRUE);
  127. }
  128. return (FALSE);
  129. }
  130. /*******************************************************************************
  131. *
  132. * FUNCTION: acpi_ut_is_aml_table
  133. *
  134. * PARAMETERS: Table - An ACPI table
  135. *
  136. * RETURN: TRUE if table contains executable AML; FALSE otherwise
  137. *
  138. * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
  139. * Currently, these are DSDT,SSDT,PSDT. All other table types are
  140. * data tables that do not contain AML code.
  141. *
  142. ******************************************************************************/
  143. u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
  144. {
  145. /* These are the only tables that contain executable AML */
  146. if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
  147. ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
  148. ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
  149. return (TRUE);
  150. }
  151. return (FALSE);
  152. }
  153. /*******************************************************************************
  154. *
  155. * FUNCTION: acpi_ut_allocate_owner_id
  156. *
  157. * PARAMETERS: owner_id - Where the new owner ID is returned
  158. *
  159. * RETURN: Status
  160. *
  161. * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
  162. * track objects created by the table or method, to be deleted
  163. * when the method exits or the table is unloaded.
  164. *
  165. ******************************************************************************/
  166. acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
  167. {
  168. u32 i;
  169. u32 j;
  170. u32 k;
  171. acpi_status status;
  172. ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
  173. /* Guard against multiple allocations of ID to the same location */
  174. if (*owner_id) {
  175. ACPI_ERROR((AE_INFO, "Owner ID [0x%2.2X] already exists",
  176. *owner_id));
  177. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  178. }
  179. /* Mutex for the global ID mask */
  180. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  181. if (ACPI_FAILURE(status)) {
  182. return_ACPI_STATUS(status);
  183. }
  184. /*
  185. * Find a free owner ID, cycle through all possible IDs on repeated
  186. * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
  187. * to be scanned twice.
  188. */
  189. for (i = 0, j = acpi_gbl_last_owner_id_index;
  190. i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
  191. if (j >= ACPI_NUM_OWNERID_MASKS) {
  192. j = 0; /* Wraparound to start of mask array */
  193. }
  194. for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
  195. if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
  196. /* There are no free IDs in this mask */
  197. break;
  198. }
  199. if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
  200. /*
  201. * Found a free ID. The actual ID is the bit index plus one,
  202. * making zero an invalid Owner ID. Save this as the last ID
  203. * allocated and update the global ID mask.
  204. */
  205. acpi_gbl_owner_id_mask[j] |= (1 << k);
  206. acpi_gbl_last_owner_id_index = (u8) j;
  207. acpi_gbl_next_owner_id_offset = (u8) (k + 1);
  208. /*
  209. * Construct encoded ID from the index and bit position
  210. *
  211. * Note: Last [j].k (bit 255) is never used and is marked
  212. * permanently allocated (prevents +1 overflow)
  213. */
  214. *owner_id =
  215. (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
  216. ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
  217. "Allocated OwnerId: %2.2X\n",
  218. (unsigned int)*owner_id));
  219. goto exit;
  220. }
  221. }
  222. acpi_gbl_next_owner_id_offset = 0;
  223. }
  224. /*
  225. * All owner_ids have been allocated. This typically should
  226. * not happen since the IDs are reused after deallocation. The IDs are
  227. * allocated upon table load (one per table) and method execution, and
  228. * they are released when a table is unloaded or a method completes
  229. * execution.
  230. *
  231. * If this error happens, there may be very deep nesting of invoked control
  232. * methods, or there may be a bug where the IDs are not released.
  233. */
  234. status = AE_OWNER_ID_LIMIT;
  235. ACPI_ERROR((AE_INFO,
  236. "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
  237. exit:
  238. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  239. return_ACPI_STATUS(status);
  240. }
  241. /*******************************************************************************
  242. *
  243. * FUNCTION: acpi_ut_release_owner_id
  244. *
  245. * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
  246. *
  247. * RETURN: None. No error is returned because we are either exiting a
  248. * control method or unloading a table. Either way, we would
  249. * ignore any error anyway.
  250. *
  251. * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
  252. *
  253. ******************************************************************************/
  254. void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
  255. {
  256. acpi_owner_id owner_id = *owner_id_ptr;
  257. acpi_status status;
  258. u32 index;
  259. u32 bit;
  260. ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
  261. /* Always clear the input owner_id (zero is an invalid ID) */
  262. *owner_id_ptr = 0;
  263. /* Zero is not a valid owner_iD */
  264. if (owner_id == 0) {
  265. ACPI_ERROR((AE_INFO, "Invalid OwnerId: 0x%2.2X", owner_id));
  266. return_VOID;
  267. }
  268. /* Mutex for the global ID mask */
  269. status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
  270. if (ACPI_FAILURE(status)) {
  271. return_VOID;
  272. }
  273. /* Normalize the ID to zero */
  274. owner_id--;
  275. /* Decode ID to index/offset pair */
  276. index = ACPI_DIV_32(owner_id);
  277. bit = 1 << ACPI_MOD_32(owner_id);
  278. /* Free the owner ID only if it is valid */
  279. if (acpi_gbl_owner_id_mask[index] & bit) {
  280. acpi_gbl_owner_id_mask[index] ^= bit;
  281. } else {
  282. ACPI_ERROR((AE_INFO,
  283. "Release of non-allocated OwnerId: 0x%2.2X",
  284. owner_id + 1));
  285. }
  286. (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
  287. return_VOID;
  288. }
  289. /*******************************************************************************
  290. *
  291. * FUNCTION: acpi_ut_strupr (strupr)
  292. *
  293. * PARAMETERS: src_string - The source string to convert
  294. *
  295. * RETURN: None
  296. *
  297. * DESCRIPTION: Convert string to uppercase
  298. *
  299. * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
  300. *
  301. ******************************************************************************/
  302. void acpi_ut_strupr(char *src_string)
  303. {
  304. char *string;
  305. ACPI_FUNCTION_ENTRY();
  306. if (!src_string) {
  307. return;
  308. }
  309. /* Walk entire string, uppercasing the letters */
  310. for (string = src_string; *string; string++) {
  311. *string = (char)ACPI_TOUPPER(*string);
  312. }
  313. return;
  314. }
  315. /*******************************************************************************
  316. *
  317. * FUNCTION: acpi_ut_print_string
  318. *
  319. * PARAMETERS: String - Null terminated ASCII string
  320. * max_length - Maximum output length
  321. *
  322. * RETURN: None
  323. *
  324. * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
  325. * sequences.
  326. *
  327. ******************************************************************************/
  328. void acpi_ut_print_string(char *string, u8 max_length)
  329. {
  330. u32 i;
  331. if (!string) {
  332. acpi_os_printf("<\"NULL STRING PTR\">");
  333. return;
  334. }
  335. acpi_os_printf("\"");
  336. for (i = 0; string[i] && (i < max_length); i++) {
  337. /* Escape sequences */
  338. switch (string[i]) {
  339. case 0x07:
  340. acpi_os_printf("\\a"); /* BELL */
  341. break;
  342. case 0x08:
  343. acpi_os_printf("\\b"); /* BACKSPACE */
  344. break;
  345. case 0x0C:
  346. acpi_os_printf("\\f"); /* FORMFEED */
  347. break;
  348. case 0x0A:
  349. acpi_os_printf("\\n"); /* LINEFEED */
  350. break;
  351. case 0x0D:
  352. acpi_os_printf("\\r"); /* CARRIAGE RETURN */
  353. break;
  354. case 0x09:
  355. acpi_os_printf("\\t"); /* HORIZONTAL TAB */
  356. break;
  357. case 0x0B:
  358. acpi_os_printf("\\v"); /* VERTICAL TAB */
  359. break;
  360. case '\'': /* Single Quote */
  361. case '\"': /* Double Quote */
  362. case '\\': /* Backslash */
  363. acpi_os_printf("\\%c", (int)string[i]);
  364. break;
  365. default:
  366. /* Check for printable character or hex escape */
  367. if (ACPI_IS_PRINT(string[i])) {
  368. /* This is a normal character */
  369. acpi_os_printf("%c", (int)string[i]);
  370. } else {
  371. /* All others will be Hex escapes */
  372. acpi_os_printf("\\x%2.2X", (s32) string[i]);
  373. }
  374. break;
  375. }
  376. }
  377. acpi_os_printf("\"");
  378. if (i == max_length && string[i]) {
  379. acpi_os_printf("...");
  380. }
  381. }
  382. /*******************************************************************************
  383. *
  384. * FUNCTION: acpi_ut_dword_byte_swap
  385. *
  386. * PARAMETERS: Value - Value to be converted
  387. *
  388. * RETURN: u32 integer with bytes swapped
  389. *
  390. * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
  391. *
  392. ******************************************************************************/
  393. u32 acpi_ut_dword_byte_swap(u32 value)
  394. {
  395. union {
  396. u32 value;
  397. u8 bytes[4];
  398. } out;
  399. union {
  400. u32 value;
  401. u8 bytes[4];
  402. } in;
  403. ACPI_FUNCTION_ENTRY();
  404. in.value = value;
  405. out.bytes[0] = in.bytes[3];
  406. out.bytes[1] = in.bytes[2];
  407. out.bytes[2] = in.bytes[1];
  408. out.bytes[3] = in.bytes[0];
  409. return (out.value);
  410. }
  411. /*******************************************************************************
  412. *
  413. * FUNCTION: acpi_ut_set_integer_width
  414. *
  415. * PARAMETERS: Revision From DSDT header
  416. *
  417. * RETURN: None
  418. *
  419. * DESCRIPTION: Set the global integer bit width based upon the revision
  420. * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
  421. * For Revision 2 and above, Integers are 64 bits. Yes, this
  422. * makes a difference.
  423. *
  424. ******************************************************************************/
  425. void acpi_ut_set_integer_width(u8 revision)
  426. {
  427. if (revision < 2) {
  428. /* 32-bit case */
  429. acpi_gbl_integer_bit_width = 32;
  430. acpi_gbl_integer_nybble_width = 8;
  431. acpi_gbl_integer_byte_width = 4;
  432. } else {
  433. /* 64-bit case (ACPI 2.0+) */
  434. acpi_gbl_integer_bit_width = 64;
  435. acpi_gbl_integer_nybble_width = 16;
  436. acpi_gbl_integer_byte_width = 8;
  437. }
  438. }
  439. #ifdef ACPI_DEBUG_OUTPUT
  440. /*******************************************************************************
  441. *
  442. * FUNCTION: acpi_ut_display_init_pathname
  443. *
  444. * PARAMETERS: Type - Object type of the node
  445. * obj_handle - Handle whose pathname will be displayed
  446. * Path - Additional path string to be appended.
  447. * (NULL if no extra path)
  448. *
  449. * RETURN: acpi_status
  450. *
  451. * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
  452. *
  453. ******************************************************************************/
  454. void
  455. acpi_ut_display_init_pathname(u8 type,
  456. struct acpi_namespace_node *obj_handle,
  457. char *path)
  458. {
  459. acpi_status status;
  460. struct acpi_buffer buffer;
  461. ACPI_FUNCTION_ENTRY();
  462. /* Only print the path if the appropriate debug level is enabled */
  463. if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
  464. return;
  465. }
  466. /* Get the full pathname to the node */
  467. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  468. status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
  469. if (ACPI_FAILURE(status)) {
  470. return;
  471. }
  472. /* Print what we're doing */
  473. switch (type) {
  474. case ACPI_TYPE_METHOD:
  475. acpi_os_printf("Executing ");
  476. break;
  477. default:
  478. acpi_os_printf("Initializing ");
  479. break;
  480. }
  481. /* Print the object type and pathname */
  482. acpi_os_printf("%-12s %s",
  483. acpi_ut_get_type_name(type), (char *)buffer.pointer);
  484. /* Extra path is used to append names like _STA, _INI, etc. */
  485. if (path) {
  486. acpi_os_printf(".%s", path);
  487. }
  488. acpi_os_printf("\n");
  489. ACPI_FREE(buffer.pointer);
  490. }
  491. #endif
  492. /*******************************************************************************
  493. *
  494. * FUNCTION: acpi_ut_valid_acpi_char
  495. *
  496. * PARAMETERS: Char - The character to be examined
  497. * Position - Byte position (0-3)
  498. *
  499. * RETURN: TRUE if the character is valid, FALSE otherwise
  500. *
  501. * DESCRIPTION: Check for a valid ACPI character. Must be one of:
  502. * 1) Upper case alpha
  503. * 2) numeric
  504. * 3) underscore
  505. *
  506. * We allow a '!' as the last character because of the ASF! table
  507. *
  508. ******************************************************************************/
  509. u8 acpi_ut_valid_acpi_char(char character, u32 position)
  510. {
  511. if (!((character >= 'A' && character <= 'Z') ||
  512. (character >= '0' && character <= '9') || (character == '_'))) {
  513. /* Allow a '!' in the last position */
  514. if (character == '!' && position == 3) {
  515. return (TRUE);
  516. }
  517. return (FALSE);
  518. }
  519. return (TRUE);
  520. }
  521. /*******************************************************************************
  522. *
  523. * FUNCTION: acpi_ut_valid_acpi_name
  524. *
  525. * PARAMETERS: Name - The name to be examined
  526. *
  527. * RETURN: TRUE if the name is valid, FALSE otherwise
  528. *
  529. * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
  530. * 1) Upper case alpha
  531. * 2) numeric
  532. * 3) underscore
  533. *
  534. ******************************************************************************/
  535. u8 acpi_ut_valid_acpi_name(u32 name)
  536. {
  537. u32 i;
  538. ACPI_FUNCTION_ENTRY();
  539. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  540. if (!acpi_ut_valid_acpi_char
  541. ((ACPI_CAST_PTR(char, &name))[i], i)) {
  542. return (FALSE);
  543. }
  544. }
  545. return (TRUE);
  546. }
  547. /*******************************************************************************
  548. *
  549. * FUNCTION: acpi_ut_repair_name
  550. *
  551. * PARAMETERS: Name - The ACPI name to be repaired
  552. *
  553. * RETURN: Repaired version of the name
  554. *
  555. * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
  556. * return the new name.
  557. *
  558. ******************************************************************************/
  559. acpi_name acpi_ut_repair_name(char *name)
  560. {
  561. u32 i;
  562. char new_name[ACPI_NAME_SIZE];
  563. for (i = 0; i < ACPI_NAME_SIZE; i++) {
  564. new_name[i] = name[i];
  565. /*
  566. * Replace a bad character with something printable, yet technically
  567. * still invalid. This prevents any collisions with existing "good"
  568. * names in the namespace.
  569. */
  570. if (!acpi_ut_valid_acpi_char(name[i], i)) {
  571. new_name[i] = '*';
  572. }
  573. }
  574. return (*(u32 *) new_name);
  575. }
  576. /*******************************************************************************
  577. *
  578. * FUNCTION: acpi_ut_strtoul64
  579. *
  580. * PARAMETERS: String - Null terminated string
  581. * Base - Radix of the string: 16 or ACPI_ANY_BASE;
  582. * ACPI_ANY_BASE means 'in behalf of to_integer'
  583. * ret_integer - Where the converted integer is returned
  584. *
  585. * RETURN: Status and Converted value
  586. *
  587. * DESCRIPTION: Convert a string into an unsigned value. Performs either a
  588. * 32-bit or 64-bit conversion, depending on the current mode
  589. * of the interpreter.
  590. * NOTE: Does not support Octal strings, not needed.
  591. *
  592. ******************************************************************************/
  593. acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 * ret_integer)
  594. {
  595. u32 this_digit = 0;
  596. u64 return_value = 0;
  597. u64 quotient;
  598. u64 dividend;
  599. u32 to_integer_op = (base == ACPI_ANY_BASE);
  600. u32 mode32 = (acpi_gbl_integer_byte_width == 4);
  601. u8 valid_digits = 0;
  602. u8 sign_of0x = 0;
  603. u8 term = 0;
  604. ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
  605. switch (base) {
  606. case ACPI_ANY_BASE:
  607. case 16:
  608. break;
  609. default:
  610. /* Invalid Base */
  611. return_ACPI_STATUS(AE_BAD_PARAMETER);
  612. }
  613. if (!string) {
  614. goto error_exit;
  615. }
  616. /* Skip over any white space in the buffer */
  617. while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
  618. string++;
  619. }
  620. if (to_integer_op) {
  621. /*
  622. * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
  623. * We need to determine if it is decimal or hexadecimal.
  624. */
  625. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  626. sign_of0x = 1;
  627. base = 16;
  628. /* Skip over the leading '0x' */
  629. string += 2;
  630. } else {
  631. base = 10;
  632. }
  633. }
  634. /* Any string left? Check that '0x' is not followed by white space. */
  635. if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
  636. if (to_integer_op) {
  637. goto error_exit;
  638. } else {
  639. goto all_done;
  640. }
  641. }
  642. /*
  643. * Perform a 32-bit or 64-bit conversion, depending upon the current
  644. * execution mode of the interpreter
  645. */
  646. dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  647. /* Main loop: convert the string to a 32- or 64-bit integer */
  648. while (*string) {
  649. if (ACPI_IS_DIGIT(*string)) {
  650. /* Convert ASCII 0-9 to Decimal value */
  651. this_digit = ((u8) * string) - '0';
  652. } else if (base == 10) {
  653. /* Digit is out of range; possible in to_integer case only */
  654. term = 1;
  655. } else {
  656. this_digit = (u8) ACPI_TOUPPER(*string);
  657. if (ACPI_IS_XDIGIT((char)this_digit)) {
  658. /* Convert ASCII Hex char to value */
  659. this_digit = this_digit - 'A' + 10;
  660. } else {
  661. term = 1;
  662. }
  663. }
  664. if (term) {
  665. if (to_integer_op) {
  666. goto error_exit;
  667. } else {
  668. break;
  669. }
  670. } else if ((valid_digits == 0) && (this_digit == 0)
  671. && !sign_of0x) {
  672. /* Skip zeros */
  673. string++;
  674. continue;
  675. }
  676. valid_digits++;
  677. if (sign_of0x && ((valid_digits > 16)
  678. || ((valid_digits > 8) && mode32))) {
  679. /*
  680. * This is to_integer operation case.
  681. * No any restrictions for string-to-integer conversion,
  682. * see ACPI spec.
  683. */
  684. goto error_exit;
  685. }
  686. /* Divide the digit into the correct position */
  687. (void)acpi_ut_short_divide((dividend - (u64) this_digit),
  688. base, &quotient, NULL);
  689. if (return_value > quotient) {
  690. if (to_integer_op) {
  691. goto error_exit;
  692. } else {
  693. break;
  694. }
  695. }
  696. return_value *= base;
  697. return_value += this_digit;
  698. string++;
  699. }
  700. /* All done, normal exit */
  701. all_done:
  702. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  703. ACPI_FORMAT_UINT64(return_value)));
  704. *ret_integer = return_value;
  705. return_ACPI_STATUS(AE_OK);
  706. error_exit:
  707. /* Base was set/validated above */
  708. if (base == 10) {
  709. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  710. } else {
  711. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  712. }
  713. }
  714. /*******************************************************************************
  715. *
  716. * FUNCTION: acpi_ut_create_update_state_and_push
  717. *
  718. * PARAMETERS: Object - Object to be added to the new state
  719. * Action - Increment/Decrement
  720. * state_list - List the state will be added to
  721. *
  722. * RETURN: Status
  723. *
  724. * DESCRIPTION: Create a new state and push it
  725. *
  726. ******************************************************************************/
  727. acpi_status
  728. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  729. u16 action,
  730. union acpi_generic_state **state_list)
  731. {
  732. union acpi_generic_state *state;
  733. ACPI_FUNCTION_ENTRY();
  734. /* Ignore null objects; these are expected */
  735. if (!object) {
  736. return (AE_OK);
  737. }
  738. state = acpi_ut_create_update_state(object, action);
  739. if (!state) {
  740. return (AE_NO_MEMORY);
  741. }
  742. acpi_ut_push_generic_state(state_list, state);
  743. return (AE_OK);
  744. }
  745. /*******************************************************************************
  746. *
  747. * FUNCTION: acpi_ut_walk_package_tree
  748. *
  749. * PARAMETERS: source_object - The package to walk
  750. * target_object - Target object (if package is being copied)
  751. * walk_callback - Called once for each package element
  752. * Context - Passed to the callback function
  753. *
  754. * RETURN: Status
  755. *
  756. * DESCRIPTION: Walk through a package
  757. *
  758. ******************************************************************************/
  759. acpi_status
  760. acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
  761. void *target_object,
  762. acpi_pkg_callback walk_callback, void *context)
  763. {
  764. acpi_status status = AE_OK;
  765. union acpi_generic_state *state_list = NULL;
  766. union acpi_generic_state *state;
  767. u32 this_index;
  768. union acpi_operand_object *this_source_obj;
  769. ACPI_FUNCTION_TRACE(ut_walk_package_tree);
  770. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  771. if (!state) {
  772. return_ACPI_STATUS(AE_NO_MEMORY);
  773. }
  774. while (state) {
  775. /* Get one element of the package */
  776. this_index = state->pkg.index;
  777. this_source_obj = (union acpi_operand_object *)
  778. state->pkg.source_object->package.elements[this_index];
  779. /*
  780. * Check for:
  781. * 1) An uninitialized package element. It is completely
  782. * legal to declare a package and leave it uninitialized
  783. * 2) Not an internal object - can be a namespace node instead
  784. * 3) Any type other than a package. Packages are handled in else
  785. * case below.
  786. */
  787. if ((!this_source_obj) ||
  788. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  789. ACPI_DESC_TYPE_OPERAND)
  790. || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
  791. status =
  792. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  793. this_source_obj, state, context);
  794. if (ACPI_FAILURE(status)) {
  795. return_ACPI_STATUS(status);
  796. }
  797. state->pkg.index++;
  798. while (state->pkg.index >=
  799. state->pkg.source_object->package.count) {
  800. /*
  801. * We've handled all of the objects at this level, This means
  802. * that we have just completed a package. That package may
  803. * have contained one or more packages itself.
  804. *
  805. * Delete this state and pop the previous state (package).
  806. */
  807. acpi_ut_delete_generic_state(state);
  808. state = acpi_ut_pop_generic_state(&state_list);
  809. /* Finished when there are no more states */
  810. if (!state) {
  811. /*
  812. * We have handled all of the objects in the top level
  813. * package just add the length of the package objects
  814. * and exit
  815. */
  816. return_ACPI_STATUS(AE_OK);
  817. }
  818. /*
  819. * Go back up a level and move the index past the just
  820. * completed package object.
  821. */
  822. state->pkg.index++;
  823. }
  824. } else {
  825. /* This is a subobject of type package */
  826. status =
  827. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  828. this_source_obj, state, context);
  829. if (ACPI_FAILURE(status)) {
  830. return_ACPI_STATUS(status);
  831. }
  832. /*
  833. * Push the current state and create a new one
  834. * The callback above returned a new target package object.
  835. */
  836. acpi_ut_push_generic_state(&state_list, state);
  837. state = acpi_ut_create_pkg_state(this_source_obj,
  838. state->pkg.
  839. this_target_obj, 0);
  840. if (!state) {
  841. /* Free any stacked Update State objects */
  842. while (state_list) {
  843. state =
  844. acpi_ut_pop_generic_state
  845. (&state_list);
  846. acpi_ut_delete_generic_state(state);
  847. }
  848. return_ACPI_STATUS(AE_NO_MEMORY);
  849. }
  850. }
  851. }
  852. /* We should never get here */
  853. return_ACPI_STATUS(AE_AML_INTERNAL);
  854. }
  855. /*******************************************************************************
  856. *
  857. * FUNCTION: acpi_error, acpi_exception, acpi_warning, acpi_info
  858. *
  859. * PARAMETERS: module_name - Caller's module name (for error output)
  860. * line_number - Caller's line number (for error output)
  861. * Format - Printf format string + additional args
  862. *
  863. * RETURN: None
  864. *
  865. * DESCRIPTION: Print message with module/line/version info
  866. *
  867. ******************************************************************************/
  868. void ACPI_INTERNAL_VAR_XFACE
  869. acpi_error(const char *module_name, u32 line_number, const char *format, ...)
  870. {
  871. va_list args;
  872. acpi_os_printf("ACPI Error: ");
  873. va_start(args, format);
  874. acpi_os_vprintf(format, args);
  875. ACPI_COMMON_MSG_SUFFIX;
  876. va_end(args);
  877. }
  878. void ACPI_INTERNAL_VAR_XFACE
  879. acpi_exception(const char *module_name,
  880. u32 line_number, acpi_status status, const char *format, ...)
  881. {
  882. va_list args;
  883. acpi_os_printf("ACPI Exception: %s, ", acpi_format_exception(status));
  884. va_start(args, format);
  885. acpi_os_vprintf(format, args);
  886. ACPI_COMMON_MSG_SUFFIX;
  887. va_end(args);
  888. }
  889. void ACPI_INTERNAL_VAR_XFACE
  890. acpi_warning(const char *module_name, u32 line_number, const char *format, ...)
  891. {
  892. va_list args;
  893. acpi_os_printf("ACPI Warning: ");
  894. va_start(args, format);
  895. acpi_os_vprintf(format, args);
  896. ACPI_COMMON_MSG_SUFFIX;
  897. va_end(args);
  898. }
  899. void ACPI_INTERNAL_VAR_XFACE
  900. acpi_info(const char *module_name, u32 line_number, const char *format, ...)
  901. {
  902. va_list args;
  903. acpi_os_printf("ACPI: ");
  904. va_start(args, format);
  905. acpi_os_vprintf(format, args);
  906. acpi_os_printf("\n");
  907. va_end(args);
  908. }
  909. ACPI_EXPORT_SYMBOL(acpi_error)
  910. ACPI_EXPORT_SYMBOL(acpi_exception)
  911. ACPI_EXPORT_SYMBOL(acpi_warning)
  912. ACPI_EXPORT_SYMBOL(acpi_info)
  913. /*******************************************************************************
  914. *
  915. * FUNCTION: acpi_ut_predefined_warning
  916. *
  917. * PARAMETERS: module_name - Caller's module name (for error output)
  918. * line_number - Caller's line number (for error output)
  919. * Pathname - Full pathname to the node
  920. * node_flags - From Namespace node for the method/object
  921. * Format - Printf format string + additional args
  922. *
  923. * RETURN: None
  924. *
  925. * DESCRIPTION: Warnings for the predefined validation module. Messages are
  926. * only emitted the first time a problem with a particular
  927. * method/object is detected. This prevents a flood of error
  928. * messages for methods that are repeatedly evaluated.
  929. *
  930. ******************************************************************************/
  931. void ACPI_INTERNAL_VAR_XFACE
  932. acpi_ut_predefined_warning(const char *module_name,
  933. u32 line_number,
  934. char *pathname,
  935. u8 node_flags, const char *format, ...)
  936. {
  937. va_list args;
  938. /*
  939. * Warning messages for this method/object will be disabled after the
  940. * first time a validation fails or an object is successfully repaired.
  941. */
  942. if (node_flags & ANOBJ_EVALUATED) {
  943. return;
  944. }
  945. acpi_os_printf("ACPI Warning for %s: ", pathname);
  946. va_start(args, format);
  947. acpi_os_vprintf(format, args);
  948. ACPI_COMMON_MSG_SUFFIX;
  949. va_end(args);
  950. }
  951. /*******************************************************************************
  952. *
  953. * FUNCTION: acpi_ut_predefined_info
  954. *
  955. * PARAMETERS: module_name - Caller's module name (for error output)
  956. * line_number - Caller's line number (for error output)
  957. * Pathname - Full pathname to the node
  958. * node_flags - From Namespace node for the method/object
  959. * Format - Printf format string + additional args
  960. *
  961. * RETURN: None
  962. *
  963. * DESCRIPTION: Info messages for the predefined validation module. Messages
  964. * are only emitted the first time a problem with a particular
  965. * method/object is detected. This prevents a flood of
  966. * messages for methods that are repeatedly evaluated.
  967. *
  968. ******************************************************************************/
  969. void ACPI_INTERNAL_VAR_XFACE
  970. acpi_ut_predefined_info(const char *module_name,
  971. u32 line_number,
  972. char *pathname, u8 node_flags, const char *format, ...)
  973. {
  974. va_list args;
  975. /*
  976. * Warning messages for this method/object will be disabled after the
  977. * first time a validation fails or an object is successfully repaired.
  978. */
  979. if (node_flags & ANOBJ_EVALUATED) {
  980. return;
  981. }
  982. acpi_os_printf("ACPI Info for %s: ", pathname);
  983. va_start(args, format);
  984. acpi_os_vprintf(format, args);
  985. ACPI_COMMON_MSG_SUFFIX;
  986. va_end(args);
  987. }