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