utmisc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmisc - common utility procedures
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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 [%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: %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: %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
  594. acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
  595. {
  596. u32 this_digit = 0;
  597. acpi_integer return_value = 0;
  598. acpi_integer quotient;
  599. acpi_integer dividend;
  600. u32 to_integer_op = (base == ACPI_ANY_BASE);
  601. u32 mode32 = (acpi_gbl_integer_byte_width == 4);
  602. u8 valid_digits = 0;
  603. u8 sign_of0x = 0;
  604. u8 term = 0;
  605. ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
  606. switch (base) {
  607. case ACPI_ANY_BASE:
  608. case 16:
  609. break;
  610. default:
  611. /* Invalid Base */
  612. return_ACPI_STATUS(AE_BAD_PARAMETER);
  613. }
  614. if (!string) {
  615. goto error_exit;
  616. }
  617. /* Skip over any white space in the buffer */
  618. while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
  619. string++;
  620. }
  621. if (to_integer_op) {
  622. /*
  623. * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
  624. * We need to determine if it is decimal or hexadecimal.
  625. */
  626. if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
  627. sign_of0x = 1;
  628. base = 16;
  629. /* Skip over the leading '0x' */
  630. string += 2;
  631. } else {
  632. base = 10;
  633. }
  634. }
  635. /* Any string left? Check that '0x' is not followed by white space. */
  636. if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
  637. if (to_integer_op) {
  638. goto error_exit;
  639. } else {
  640. goto all_done;
  641. }
  642. }
  643. /*
  644. * Perform a 32-bit or 64-bit conversion, depending upon the current
  645. * execution mode of the interpreter
  646. */
  647. dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
  648. /* Main loop: convert the string to a 32- or 64-bit integer */
  649. while (*string) {
  650. if (ACPI_IS_DIGIT(*string)) {
  651. /* Convert ASCII 0-9 to Decimal value */
  652. this_digit = ((u8) * string) - '0';
  653. } else if (base == 10) {
  654. /* Digit is out of range; possible in to_integer case only */
  655. term = 1;
  656. } else {
  657. this_digit = (u8) ACPI_TOUPPER(*string);
  658. if (ACPI_IS_XDIGIT((char)this_digit)) {
  659. /* Convert ASCII Hex char to value */
  660. this_digit = this_digit - 'A' + 10;
  661. } else {
  662. term = 1;
  663. }
  664. }
  665. if (term) {
  666. if (to_integer_op) {
  667. goto error_exit;
  668. } else {
  669. break;
  670. }
  671. } else if ((valid_digits == 0) && (this_digit == 0)
  672. && !sign_of0x) {
  673. /* Skip zeros */
  674. string++;
  675. continue;
  676. }
  677. valid_digits++;
  678. if (sign_of0x && ((valid_digits > 16)
  679. || ((valid_digits > 8) && mode32))) {
  680. /*
  681. * This is to_integer operation case.
  682. * No any restrictions for string-to-integer conversion,
  683. * see ACPI spec.
  684. */
  685. goto error_exit;
  686. }
  687. /* Divide the digit into the correct position */
  688. (void)
  689. acpi_ut_short_divide((dividend - (acpi_integer) this_digit),
  690. base, &quotient, NULL);
  691. if (return_value > quotient) {
  692. if (to_integer_op) {
  693. goto error_exit;
  694. } else {
  695. break;
  696. }
  697. }
  698. return_value *= base;
  699. return_value += this_digit;
  700. string++;
  701. }
  702. /* All done, normal exit */
  703. all_done:
  704. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  705. ACPI_FORMAT_UINT64(return_value)));
  706. *ret_integer = return_value;
  707. return_ACPI_STATUS(AE_OK);
  708. error_exit:
  709. /* Base was set/validated above */
  710. if (base == 10) {
  711. return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
  712. } else {
  713. return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
  714. }
  715. }
  716. /*******************************************************************************
  717. *
  718. * FUNCTION: acpi_ut_create_update_state_and_push
  719. *
  720. * PARAMETERS: Object - Object to be added to the new state
  721. * Action - Increment/Decrement
  722. * state_list - List the state will be added to
  723. *
  724. * RETURN: Status
  725. *
  726. * DESCRIPTION: Create a new state and push it
  727. *
  728. ******************************************************************************/
  729. acpi_status
  730. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  731. u16 action,
  732. union acpi_generic_state **state_list)
  733. {
  734. union acpi_generic_state *state;
  735. ACPI_FUNCTION_ENTRY();
  736. /* Ignore null objects; these are expected */
  737. if (!object) {
  738. return (AE_OK);
  739. }
  740. state = acpi_ut_create_update_state(object, action);
  741. if (!state) {
  742. return (AE_NO_MEMORY);
  743. }
  744. acpi_ut_push_generic_state(state_list, state);
  745. return (AE_OK);
  746. }
  747. /*******************************************************************************
  748. *
  749. * FUNCTION: acpi_ut_walk_package_tree
  750. *
  751. * PARAMETERS: source_object - The package to walk
  752. * target_object - Target object (if package is being copied)
  753. * walk_callback - Called once for each package element
  754. * Context - Passed to the callback function
  755. *
  756. * RETURN: Status
  757. *
  758. * DESCRIPTION: Walk through a package
  759. *
  760. ******************************************************************************/
  761. acpi_status
  762. acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
  763. void *target_object,
  764. acpi_pkg_callback walk_callback, void *context)
  765. {
  766. acpi_status status = AE_OK;
  767. union acpi_generic_state *state_list = NULL;
  768. union acpi_generic_state *state;
  769. u32 this_index;
  770. union acpi_operand_object *this_source_obj;
  771. ACPI_FUNCTION_TRACE(ut_walk_package_tree);
  772. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  773. if (!state) {
  774. return_ACPI_STATUS(AE_NO_MEMORY);
  775. }
  776. while (state) {
  777. /* Get one element of the package */
  778. this_index = state->pkg.index;
  779. this_source_obj = (union acpi_operand_object *)
  780. state->pkg.source_object->package.elements[this_index];
  781. /*
  782. * Check for:
  783. * 1) An uninitialized package element. It is completely
  784. * legal to declare a package and leave it uninitialized
  785. * 2) Not an internal object - can be a namespace node instead
  786. * 3) Any type other than a package. Packages are handled in else
  787. * case below.
  788. */
  789. if ((!this_source_obj) ||
  790. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  791. ACPI_DESC_TYPE_OPERAND)
  792. || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
  793. status =
  794. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  795. this_source_obj, state, context);
  796. if (ACPI_FAILURE(status)) {
  797. return_ACPI_STATUS(status);
  798. }
  799. state->pkg.index++;
  800. while (state->pkg.index >=
  801. state->pkg.source_object->package.count) {
  802. /*
  803. * We've handled all of the objects at this level, This means
  804. * that we have just completed a package. That package may
  805. * have contained one or more packages itself.
  806. *
  807. * Delete this state and pop the previous state (package).
  808. */
  809. acpi_ut_delete_generic_state(state);
  810. state = acpi_ut_pop_generic_state(&state_list);
  811. /* Finished when there are no more states */
  812. if (!state) {
  813. /*
  814. * We have handled all of the objects in the top level
  815. * package just add the length of the package objects
  816. * and exit
  817. */
  818. return_ACPI_STATUS(AE_OK);
  819. }
  820. /*
  821. * Go back up a level and move the index past the just
  822. * completed package object.
  823. */
  824. state->pkg.index++;
  825. }
  826. } else {
  827. /* This is a subobject of type package */
  828. status =
  829. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  830. this_source_obj, state, context);
  831. if (ACPI_FAILURE(status)) {
  832. return_ACPI_STATUS(status);
  833. }
  834. /*
  835. * Push the current state and create a new one
  836. * The callback above returned a new target package object.
  837. */
  838. acpi_ut_push_generic_state(&state_list, state);
  839. state = acpi_ut_create_pkg_state(this_source_obj,
  840. state->pkg.
  841. this_target_obj, 0);
  842. if (!state) {
  843. /* Free any stacked Update State objects */
  844. while (state_list) {
  845. state =
  846. acpi_ut_pop_generic_state
  847. (&state_list);
  848. acpi_ut_delete_generic_state(state);
  849. }
  850. return_ACPI_STATUS(AE_NO_MEMORY);
  851. }
  852. }
  853. }
  854. /* We should never get here */
  855. return_ACPI_STATUS(AE_AML_INTERNAL);
  856. }
  857. /*******************************************************************************
  858. *
  859. * FUNCTION: acpi_error, acpi_exception, acpi_warning, acpi_info
  860. *
  861. * PARAMETERS: module_name - Caller's module name (for error output)
  862. * line_number - Caller's line number (for error output)
  863. * Format - Printf format string + additional args
  864. *
  865. * RETURN: None
  866. *
  867. * DESCRIPTION: Print message with module/line/version info
  868. *
  869. ******************************************************************************/
  870. void ACPI_INTERNAL_VAR_XFACE
  871. acpi_error(const char *module_name, u32 line_number, const char *format, ...)
  872. {
  873. va_list args;
  874. acpi_os_printf("ACPI Error: ");
  875. va_start(args, format);
  876. acpi_os_vprintf(format, args);
  877. ACPI_COMMON_MSG_SUFFIX;
  878. va_end(args);
  879. }
  880. void ACPI_INTERNAL_VAR_XFACE
  881. acpi_exception(const char *module_name,
  882. u32 line_number, acpi_status status, const char *format, ...)
  883. {
  884. va_list args;
  885. acpi_os_printf("ACPI Exception: %s, ", acpi_format_exception(status));
  886. va_start(args, format);
  887. acpi_os_vprintf(format, args);
  888. ACPI_COMMON_MSG_SUFFIX;
  889. va_end(args);
  890. }
  891. void ACPI_INTERNAL_VAR_XFACE
  892. acpi_warning(const char *module_name, u32 line_number, const char *format, ...)
  893. {
  894. va_list args;
  895. acpi_os_printf("ACPI Warning: ");
  896. va_start(args, format);
  897. acpi_os_vprintf(format, args);
  898. ACPI_COMMON_MSG_SUFFIX;
  899. va_end(args);
  900. }
  901. void ACPI_INTERNAL_VAR_XFACE
  902. acpi_info(const char *module_name, u32 line_number, const char *format, ...)
  903. {
  904. va_list args;
  905. acpi_os_printf("ACPI: ");
  906. va_start(args, format);
  907. acpi_os_vprintf(format, args);
  908. acpi_os_printf("\n");
  909. va_end(args);
  910. }
  911. ACPI_EXPORT_SYMBOL(acpi_error)
  912. ACPI_EXPORT_SYMBOL(acpi_exception)
  913. ACPI_EXPORT_SYMBOL(acpi_warning)
  914. ACPI_EXPORT_SYMBOL(acpi_info)
  915. /*******************************************************************************
  916. *
  917. * FUNCTION: acpi_ut_predefined_warning
  918. *
  919. * PARAMETERS: module_name - Caller's module name (for error output)
  920. * line_number - Caller's line number (for error output)
  921. * Pathname - Full pathname to the node
  922. * node_flags - From Namespace node for the method/object
  923. * Format - Printf format string + additional args
  924. *
  925. * RETURN: None
  926. *
  927. * DESCRIPTION: Warnings for the predefined validation module. Messages are
  928. * only emitted the first time a problem with a particular
  929. * method/object is detected. This prevents a flood of error
  930. * messages for methods that are repeatedly evaluated.
  931. *
  932. ******************************************************************************/
  933. void ACPI_INTERNAL_VAR_XFACE
  934. acpi_ut_predefined_warning(const char *module_name,
  935. u32 line_number,
  936. char *pathname,
  937. u8 node_flags, const char *format, ...)
  938. {
  939. va_list args;
  940. /*
  941. * Warning messages for this method/object will be disabled after the
  942. * first time a validation fails or an object is successfully repaired.
  943. */
  944. if (node_flags & ANOBJ_EVALUATED) {
  945. return;
  946. }
  947. acpi_os_printf("ACPI Warning for %s: ", pathname);
  948. va_start(args, format);
  949. acpi_os_vprintf(format, args);
  950. ACPI_COMMON_MSG_SUFFIX;
  951. va_end(args);
  952. }
  953. /*******************************************************************************
  954. *
  955. * FUNCTION: acpi_ut_predefined_info
  956. *
  957. * PARAMETERS: module_name - Caller's module name (for error output)
  958. * line_number - Caller's line number (for error output)
  959. * Pathname - Full pathname to the node
  960. * node_flags - From Namespace node for the method/object
  961. * Format - Printf format string + additional args
  962. *
  963. * RETURN: None
  964. *
  965. * DESCRIPTION: Info messages for the predefined validation module. Messages
  966. * are only emitted the first time a problem with a particular
  967. * method/object is detected. This prevents a flood of
  968. * messages for methods that are repeatedly evaluated.
  969. *
  970. ******************************************************************************/
  971. void ACPI_INTERNAL_VAR_XFACE
  972. acpi_ut_predefined_info(const char *module_name,
  973. u32 line_number,
  974. char *pathname, u8 node_flags, const char *format, ...)
  975. {
  976. va_list args;
  977. /*
  978. * Warning messages for this method/object will be disabled after the
  979. * first time a validation fails or an object is successfully repaired.
  980. */
  981. if (node_flags & ANOBJ_EVALUATED) {
  982. return;
  983. }
  984. acpi_os_printf("ACPI Info for %s: ", pathname);
  985. va_start(args, format);
  986. acpi_os_vprintf(format, args);
  987. ACPI_COMMON_MSG_SUFFIX;
  988. va_end(args);
  989. }