nsxfeval.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
  4. * ACPI Object evaluation interfaces
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2017, Intel Corp.
  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. #define EXPORT_ACPI_INTERFACES
  44. #include <acpi/acpi.h>
  45. #include "accommon.h"
  46. #include "acnamesp.h"
  47. #include "acinterp.h"
  48. #define _COMPONENT ACPI_NAMESPACE
  49. ACPI_MODULE_NAME("nsxfeval")
  50. /* Local prototypes */
  51. static void acpi_ns_resolve_references(struct acpi_evaluate_info *info);
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_evaluate_object_typed
  55. *
  56. * PARAMETERS: handle - Object handle (optional)
  57. * pathname - Object pathname (optional)
  58. * external_params - List of parameters to pass to method,
  59. * terminated by NULL. May be NULL
  60. * if no parameters are being passed.
  61. * return_buffer - Where to put method's return value (if
  62. * any). If NULL, no value is returned.
  63. * return_type - Expected type of return object
  64. *
  65. * RETURN: Status
  66. *
  67. * DESCRIPTION: Find and evaluate the given object, passing the given
  68. * parameters if necessary. One of "Handle" or "Pathname" must
  69. * be valid (non-null)
  70. *
  71. ******************************************************************************/
  72. acpi_status
  73. acpi_evaluate_object_typed(acpi_handle handle,
  74. acpi_string pathname,
  75. struct acpi_object_list *external_params,
  76. struct acpi_buffer *return_buffer,
  77. acpi_object_type return_type)
  78. {
  79. acpi_status status;
  80. u8 free_buffer_on_error = FALSE;
  81. acpi_handle target_handle;
  82. char *full_pathname;
  83. ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed);
  84. /* Return buffer must be valid */
  85. if (!return_buffer) {
  86. return_ACPI_STATUS(AE_BAD_PARAMETER);
  87. }
  88. if (return_buffer->length == ACPI_ALLOCATE_BUFFER) {
  89. free_buffer_on_error = TRUE;
  90. }
  91. if (pathname) {
  92. status = acpi_get_handle(handle, pathname, &target_handle);
  93. if (ACPI_FAILURE(status)) {
  94. return_ACPI_STATUS(status);
  95. }
  96. } else {
  97. target_handle = handle;
  98. }
  99. full_pathname = acpi_ns_get_external_pathname(target_handle);
  100. if (!full_pathname) {
  101. return_ACPI_STATUS(AE_NO_MEMORY);
  102. }
  103. /* Evaluate the object */
  104. status = acpi_evaluate_object(target_handle, NULL, external_params,
  105. return_buffer);
  106. if (ACPI_FAILURE(status)) {
  107. goto exit;
  108. }
  109. /* Type ANY means "don't care about return value type" */
  110. if (return_type == ACPI_TYPE_ANY) {
  111. goto exit;
  112. }
  113. if (return_buffer->length == 0) {
  114. /* Error because caller specifically asked for a return value */
  115. ACPI_ERROR((AE_INFO, "%s did not return any object",
  116. full_pathname));
  117. status = AE_NULL_OBJECT;
  118. goto exit;
  119. }
  120. /* Examine the object type returned from evaluate_object */
  121. if (((union acpi_object *)return_buffer->pointer)->type == return_type) {
  122. goto exit;
  123. }
  124. /* Return object type does not match requested type */
  125. ACPI_ERROR((AE_INFO,
  126. "Incorrect return type from %s - received [%s], requested [%s]",
  127. full_pathname,
  128. acpi_ut_get_type_name(((union acpi_object *)return_buffer->
  129. pointer)->type),
  130. acpi_ut_get_type_name(return_type)));
  131. if (free_buffer_on_error) {
  132. /*
  133. * Free a buffer created via ACPI_ALLOCATE_BUFFER.
  134. * Note: We use acpi_os_free here because acpi_os_allocate was used
  135. * to allocate the buffer. This purposefully bypasses the
  136. * (optionally enabled) allocation tracking mechanism since we
  137. * only want to track internal allocations.
  138. */
  139. acpi_os_free(return_buffer->pointer);
  140. return_buffer->pointer = NULL;
  141. }
  142. return_buffer->length = 0;
  143. status = AE_TYPE;
  144. exit:
  145. ACPI_FREE(full_pathname);
  146. return_ACPI_STATUS(status);
  147. }
  148. ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed)
  149. /*******************************************************************************
  150. *
  151. * FUNCTION: acpi_evaluate_object
  152. *
  153. * PARAMETERS: handle - Object handle (optional)
  154. * pathname - Object pathname (optional)
  155. * external_params - List of parameters to pass to method,
  156. * terminated by NULL. May be NULL
  157. * if no parameters are being passed.
  158. * return_buffer - Where to put method's return value (if
  159. * any). If NULL, no value is returned.
  160. *
  161. * RETURN: Status
  162. *
  163. * DESCRIPTION: Find and evaluate the given object, passing the given
  164. * parameters if necessary. One of "Handle" or "Pathname" must
  165. * be valid (non-null)
  166. *
  167. ******************************************************************************/
  168. acpi_status
  169. acpi_evaluate_object(acpi_handle handle,
  170. acpi_string pathname,
  171. struct acpi_object_list *external_params,
  172. struct acpi_buffer *return_buffer)
  173. {
  174. acpi_status status;
  175. struct acpi_evaluate_info *info;
  176. acpi_size buffer_space_needed;
  177. u32 i;
  178. ACPI_FUNCTION_TRACE(acpi_evaluate_object);
  179. /* Allocate and initialize the evaluation information block */
  180. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  181. if (!info) {
  182. return_ACPI_STATUS(AE_NO_MEMORY);
  183. }
  184. /* Convert and validate the device handle */
  185. info->prefix_node = acpi_ns_validate_handle(handle);
  186. if (!info->prefix_node) {
  187. status = AE_BAD_PARAMETER;
  188. goto cleanup;
  189. }
  190. /*
  191. * Get the actual namespace node for the target object.
  192. * Handles these cases:
  193. *
  194. * 1) Null node, valid pathname from root (absolute path)
  195. * 2) Node and valid pathname (path relative to Node)
  196. * 3) Node, Null pathname
  197. */
  198. if ((pathname) && (ACPI_IS_ROOT_PREFIX(pathname[0]))) {
  199. /* The path is fully qualified, just evaluate by name */
  200. info->prefix_node = NULL;
  201. } else if (!handle) {
  202. /*
  203. * A handle is optional iff a fully qualified pathname is specified.
  204. * Since we've already handled fully qualified names above, this is
  205. * an error.
  206. */
  207. if (!pathname) {
  208. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  209. "Both Handle and Pathname are NULL"));
  210. } else {
  211. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  212. "Null Handle with relative pathname [%s]",
  213. pathname));
  214. }
  215. status = AE_BAD_PARAMETER;
  216. goto cleanup;
  217. }
  218. info->relative_pathname = pathname;
  219. /*
  220. * Convert all external objects passed as arguments to the
  221. * internal version(s).
  222. */
  223. if (external_params && external_params->count) {
  224. info->param_count = (u16)external_params->count;
  225. /* Warn on impossible argument count */
  226. if (info->param_count > ACPI_METHOD_NUM_ARGS) {
  227. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  228. ACPI_WARN_ALWAYS,
  229. "Excess arguments (%u) - using only %u",
  230. info->param_count,
  231. ACPI_METHOD_NUM_ARGS));
  232. info->param_count = ACPI_METHOD_NUM_ARGS;
  233. }
  234. /*
  235. * Allocate a new parameter block for the internal objects
  236. * Add 1 to count to allow for null terminated internal list
  237. */
  238. info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size)info->
  239. param_count +
  240. 1) * sizeof(void *));
  241. if (!info->parameters) {
  242. status = AE_NO_MEMORY;
  243. goto cleanup;
  244. }
  245. /* Convert each external object in the list to an internal object */
  246. for (i = 0; i < info->param_count; i++) {
  247. status =
  248. acpi_ut_copy_eobject_to_iobject(&external_params->
  249. pointer[i],
  250. &info->
  251. parameters[i]);
  252. if (ACPI_FAILURE(status)) {
  253. goto cleanup;
  254. }
  255. }
  256. info->parameters[info->param_count] = NULL;
  257. }
  258. #ifdef _FUTURE_FEATURE
  259. /*
  260. * Begin incoming argument count analysis. Check for too few args
  261. * and too many args.
  262. */
  263. switch (acpi_ns_get_type(info->node)) {
  264. case ACPI_TYPE_METHOD:
  265. /* Check incoming argument count against the method definition */
  266. if (info->obj_desc->method.param_count > info->param_count) {
  267. ACPI_ERROR((AE_INFO,
  268. "Insufficient arguments (%u) - %u are required",
  269. info->param_count,
  270. info->obj_desc->method.param_count));
  271. status = AE_MISSING_ARGUMENTS;
  272. goto cleanup;
  273. }
  274. else if (info->obj_desc->method.param_count < info->param_count) {
  275. ACPI_WARNING((AE_INFO,
  276. "Excess arguments (%u) - only %u are required",
  277. info->param_count,
  278. info->obj_desc->method.param_count));
  279. /* Just pass the required number of arguments */
  280. info->param_count = info->obj_desc->method.param_count;
  281. }
  282. /*
  283. * Any incoming external objects to be passed as arguments to the
  284. * method must be converted to internal objects
  285. */
  286. if (info->param_count) {
  287. /*
  288. * Allocate a new parameter block for the internal objects
  289. * Add 1 to count to allow for null terminated internal list
  290. */
  291. info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size)
  292. info->
  293. param_count +
  294. 1) *
  295. sizeof(void *));
  296. if (!info->parameters) {
  297. status = AE_NO_MEMORY;
  298. goto cleanup;
  299. }
  300. /* Convert each external object in the list to an internal object */
  301. for (i = 0; i < info->param_count; i++) {
  302. status =
  303. acpi_ut_copy_eobject_to_iobject
  304. (&external_params->pointer[i],
  305. &info->parameters[i]);
  306. if (ACPI_FAILURE(status)) {
  307. goto cleanup;
  308. }
  309. }
  310. info->parameters[info->param_count] = NULL;
  311. }
  312. break;
  313. default:
  314. /* Warn if arguments passed to an object that is not a method */
  315. if (info->param_count) {
  316. ACPI_WARNING((AE_INFO,
  317. "%u arguments were passed to a non-method ACPI object",
  318. info->param_count));
  319. }
  320. break;
  321. }
  322. #endif
  323. /* Now we can evaluate the object */
  324. status = acpi_ns_evaluate(info);
  325. /*
  326. * If we are expecting a return value, and all went well above,
  327. * copy the return value to an external object.
  328. */
  329. if (!return_buffer) {
  330. goto cleanup_return_object;
  331. }
  332. if (!info->return_object) {
  333. return_buffer->length = 0;
  334. goto cleanup;
  335. }
  336. if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) ==
  337. ACPI_DESC_TYPE_NAMED) {
  338. /*
  339. * If we received a NS Node as a return object, this means that
  340. * the object we are evaluating has nothing interesting to
  341. * return (such as a mutex, etc.) We return an error because
  342. * these types are essentially unsupported by this interface.
  343. * We don't check up front because this makes it easier to add
  344. * support for various types at a later date if necessary.
  345. */
  346. status = AE_TYPE;
  347. info->return_object = NULL; /* No need to delete a NS Node */
  348. return_buffer->length = 0;
  349. }
  350. if (ACPI_FAILURE(status)) {
  351. goto cleanup_return_object;
  352. }
  353. /* Dereference Index and ref_of references */
  354. acpi_ns_resolve_references(info);
  355. /* Get the size of the returned object */
  356. status = acpi_ut_get_object_size(info->return_object,
  357. &buffer_space_needed);
  358. if (ACPI_SUCCESS(status)) {
  359. /* Validate/Allocate/Clear caller buffer */
  360. status = acpi_ut_initialize_buffer(return_buffer,
  361. buffer_space_needed);
  362. if (ACPI_FAILURE(status)) {
  363. /*
  364. * Caller's buffer is too small or a new one can't
  365. * be allocated
  366. */
  367. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  368. "Needed buffer size %X, %s\n",
  369. (u32)buffer_space_needed,
  370. acpi_format_exception(status)));
  371. } else {
  372. /* We have enough space for the object, build it */
  373. status =
  374. acpi_ut_copy_iobject_to_eobject(info->return_object,
  375. return_buffer);
  376. }
  377. }
  378. cleanup_return_object:
  379. if (info->return_object) {
  380. /*
  381. * Delete the internal return object. NOTE: Interpreter must be
  382. * locked to avoid race condition.
  383. */
  384. acpi_ex_enter_interpreter();
  385. /* Remove one reference on the return object (should delete it) */
  386. acpi_ut_remove_reference(info->return_object);
  387. acpi_ex_exit_interpreter();
  388. }
  389. cleanup:
  390. /* Free the input parameter list (if we created one) */
  391. if (info->parameters) {
  392. /* Free the allocated parameter block */
  393. acpi_ut_delete_internal_object_list(info->parameters);
  394. }
  395. ACPI_FREE(info);
  396. return_ACPI_STATUS(status);
  397. }
  398. ACPI_EXPORT_SYMBOL(acpi_evaluate_object)
  399. /*******************************************************************************
  400. *
  401. * FUNCTION: acpi_ns_resolve_references
  402. *
  403. * PARAMETERS: info - Evaluation info block
  404. *
  405. * RETURN: Info->return_object is replaced with the dereferenced object
  406. *
  407. * DESCRIPTION: Dereference certain reference objects. Called before an
  408. * internal return object is converted to an external union acpi_object.
  409. *
  410. * Performs an automatic dereference of Index and ref_of reference objects.
  411. * These reference objects are not supported by the union acpi_object, so this is a
  412. * last resort effort to return something useful. Also, provides compatibility
  413. * with other ACPI implementations.
  414. *
  415. * NOTE: does not handle references within returned package objects or nested
  416. * references, but this support could be added later if found to be necessary.
  417. *
  418. ******************************************************************************/
  419. static void acpi_ns_resolve_references(struct acpi_evaluate_info *info)
  420. {
  421. union acpi_operand_object *obj_desc = NULL;
  422. struct acpi_namespace_node *node;
  423. /* We are interested in reference objects only */
  424. if ((info->return_object)->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
  425. return;
  426. }
  427. /*
  428. * Two types of references are supported - those created by Index and
  429. * ref_of operators. A name reference (AML_NAMEPATH_OP) can be converted
  430. * to a union acpi_object, so it is not dereferenced here. A ddb_handle
  431. * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to
  432. * a union acpi_object.
  433. */
  434. switch (info->return_object->reference.class) {
  435. case ACPI_REFCLASS_INDEX:
  436. obj_desc = *(info->return_object->reference.where);
  437. break;
  438. case ACPI_REFCLASS_REFOF:
  439. node = info->return_object->reference.object;
  440. if (node) {
  441. obj_desc = node->object;
  442. }
  443. break;
  444. default:
  445. return;
  446. }
  447. /* Replace the existing reference object */
  448. if (obj_desc) {
  449. acpi_ut_add_reference(obj_desc);
  450. acpi_ut_remove_reference(info->return_object);
  451. info->return_object = obj_desc;
  452. }
  453. return;
  454. }
  455. /*******************************************************************************
  456. *
  457. * FUNCTION: acpi_walk_namespace
  458. *
  459. * PARAMETERS: type - acpi_object_type to search for
  460. * start_object - Handle in namespace where search begins
  461. * max_depth - Depth to which search is to reach
  462. * descending_callback - Called during tree descent
  463. * when an object of "Type" is found
  464. * ascending_callback - Called during tree ascent
  465. * when an object of "Type" is found
  466. * context - Passed to user function(s) above
  467. * return_value - Location where return value of
  468. * user_function is put if terminated early
  469. *
  470. * RETURNS Return value from the user_function if terminated early.
  471. * Otherwise, returns NULL.
  472. *
  473. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  474. * starting (and ending) at the object specified by start_handle.
  475. * The callback function is called whenever an object that matches
  476. * the type parameter is found. If the callback function returns
  477. * a non-zero value, the search is terminated immediately and this
  478. * value is returned to the caller.
  479. *
  480. * The point of this procedure is to provide a generic namespace
  481. * walk routine that can be called from multiple places to
  482. * provide multiple services; the callback function(s) can be
  483. * tailored to each task, whether it is a print function,
  484. * a compare function, etc.
  485. *
  486. ******************************************************************************/
  487. acpi_status
  488. acpi_walk_namespace(acpi_object_type type,
  489. acpi_handle start_object,
  490. u32 max_depth,
  491. acpi_walk_callback descending_callback,
  492. acpi_walk_callback ascending_callback,
  493. void *context, void **return_value)
  494. {
  495. acpi_status status;
  496. ACPI_FUNCTION_TRACE(acpi_walk_namespace);
  497. /* Parameter validation */
  498. if ((type > ACPI_TYPE_LOCAL_MAX) ||
  499. (!max_depth) || (!descending_callback && !ascending_callback)) {
  500. return_ACPI_STATUS(AE_BAD_PARAMETER);
  501. }
  502. /*
  503. * Need to acquire the namespace reader lock to prevent interference
  504. * with any concurrent table unloads (which causes the deletion of
  505. * namespace objects). We cannot allow the deletion of a namespace node
  506. * while the user function is using it. The exception to this are the
  507. * nodes created and deleted during control method execution -- these
  508. * nodes are marked as temporary nodes and are ignored by the namespace
  509. * walk. Thus, control methods can be executed while holding the
  510. * namespace deletion lock (and the user function can execute control
  511. * methods.)
  512. */
  513. status = acpi_ut_acquire_read_lock(&acpi_gbl_namespace_rw_lock);
  514. if (ACPI_FAILURE(status)) {
  515. return_ACPI_STATUS(status);
  516. }
  517. /*
  518. * Lock the namespace around the walk. The namespace will be
  519. * unlocked/locked around each call to the user function - since the user
  520. * function must be allowed to make ACPICA calls itself (for example, it
  521. * will typically execute control methods during device enumeration.)
  522. */
  523. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  524. if (ACPI_FAILURE(status)) {
  525. goto unlock_and_exit;
  526. }
  527. /* Now we can validate the starting node */
  528. if (!acpi_ns_validate_handle(start_object)) {
  529. status = AE_BAD_PARAMETER;
  530. goto unlock_and_exit2;
  531. }
  532. status = acpi_ns_walk_namespace(type, start_object, max_depth,
  533. ACPI_NS_WALK_UNLOCK,
  534. descending_callback, ascending_callback,
  535. context, return_value);
  536. unlock_and_exit2:
  537. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  538. unlock_and_exit:
  539. (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock);
  540. return_ACPI_STATUS(status);
  541. }
  542. ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
  543. /*******************************************************************************
  544. *
  545. * FUNCTION: acpi_ns_get_device_callback
  546. *
  547. * PARAMETERS: Callback from acpi_get_device
  548. *
  549. * RETURN: Status
  550. *
  551. * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
  552. * present devices, or if they specified a HID, it filters based
  553. * on that.
  554. *
  555. ******************************************************************************/
  556. static acpi_status
  557. acpi_ns_get_device_callback(acpi_handle obj_handle,
  558. u32 nesting_level,
  559. void *context, void **return_value)
  560. {
  561. struct acpi_get_devices_info *info = context;
  562. acpi_status status;
  563. struct acpi_namespace_node *node;
  564. u32 flags;
  565. struct acpi_pnp_device_id *hid;
  566. struct acpi_pnp_device_id_list *cid;
  567. u32 i;
  568. u8 found;
  569. int no_match;
  570. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  571. if (ACPI_FAILURE(status)) {
  572. return (status);
  573. }
  574. node = acpi_ns_validate_handle(obj_handle);
  575. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  576. if (ACPI_FAILURE(status)) {
  577. return (status);
  578. }
  579. if (!node) {
  580. return (AE_BAD_PARAMETER);
  581. }
  582. /*
  583. * First, filter based on the device HID and CID.
  584. *
  585. * 01/2010: For this case where a specific HID is requested, we don't
  586. * want to run _STA until we have an actual HID match. Thus, we will
  587. * not unnecessarily execute _STA on devices for which the caller
  588. * doesn't care about. Previously, _STA was executed unconditionally
  589. * on all devices found here.
  590. *
  591. * A side-effect of this change is that now we will continue to search
  592. * for a matching HID even under device trees where the parent device
  593. * would have returned a _STA that indicates it is not present or
  594. * not functioning (thus aborting the search on that branch).
  595. */
  596. if (info->hid != NULL) {
  597. status = acpi_ut_execute_HID(node, &hid);
  598. if (status == AE_NOT_FOUND) {
  599. return (AE_OK);
  600. } else if (ACPI_FAILURE(status)) {
  601. return (AE_CTRL_DEPTH);
  602. }
  603. no_match = strcmp(hid->string, info->hid);
  604. ACPI_FREE(hid);
  605. if (no_match) {
  606. /*
  607. * HID does not match, attempt match within the
  608. * list of Compatible IDs (CIDs)
  609. */
  610. status = acpi_ut_execute_CID(node, &cid);
  611. if (status == AE_NOT_FOUND) {
  612. return (AE_OK);
  613. } else if (ACPI_FAILURE(status)) {
  614. return (AE_CTRL_DEPTH);
  615. }
  616. /* Walk the CID list */
  617. found = FALSE;
  618. for (i = 0; i < cid->count; i++) {
  619. if (strcmp(cid->ids[i].string, info->hid) == 0) {
  620. /* Found a matching CID */
  621. found = TRUE;
  622. break;
  623. }
  624. }
  625. ACPI_FREE(cid);
  626. if (!found) {
  627. return (AE_OK);
  628. }
  629. }
  630. }
  631. /* Run _STA to determine if device is present */
  632. status = acpi_ut_execute_STA(node, &flags);
  633. if (ACPI_FAILURE(status)) {
  634. return (AE_CTRL_DEPTH);
  635. }
  636. if (!(flags & ACPI_STA_DEVICE_PRESENT) &&
  637. !(flags & ACPI_STA_DEVICE_FUNCTIONING)) {
  638. /*
  639. * Don't examine the children of the device only when the
  640. * device is neither present nor functional. See ACPI spec,
  641. * description of _STA for more information.
  642. */
  643. return (AE_CTRL_DEPTH);
  644. }
  645. /* We have a valid device, invoke the user function */
  646. status = info->user_function(obj_handle, nesting_level,
  647. info->context, return_value);
  648. return (status);
  649. }
  650. /*******************************************************************************
  651. *
  652. * FUNCTION: acpi_get_devices
  653. *
  654. * PARAMETERS: HID - HID to search for. Can be NULL.
  655. * user_function - Called when a matching object is found
  656. * context - Passed to user function
  657. * return_value - Location where return value of
  658. * user_function is put if terminated early
  659. *
  660. * RETURNS Return value from the user_function if terminated early.
  661. * Otherwise, returns NULL.
  662. *
  663. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  664. * starting (and ending) at the object specified by start_handle.
  665. * The user_function is called whenever an object of type
  666. * Device is found. If the user function returns
  667. * a non-zero value, the search is terminated immediately and this
  668. * value is returned to the caller.
  669. *
  670. * This is a wrapper for walk_namespace, but the callback performs
  671. * additional filtering. Please see acpi_ns_get_device_callback.
  672. *
  673. ******************************************************************************/
  674. acpi_status
  675. acpi_get_devices(const char *HID,
  676. acpi_walk_callback user_function,
  677. void *context, void **return_value)
  678. {
  679. acpi_status status;
  680. struct acpi_get_devices_info info;
  681. ACPI_FUNCTION_TRACE(acpi_get_devices);
  682. /* Parameter validation */
  683. if (!user_function) {
  684. return_ACPI_STATUS(AE_BAD_PARAMETER);
  685. }
  686. /*
  687. * We're going to call their callback from OUR callback, so we need
  688. * to know what it is, and their context parameter.
  689. */
  690. info.hid = HID;
  691. info.context = context;
  692. info.user_function = user_function;
  693. /*
  694. * Lock the namespace around the walk.
  695. * The namespace will be unlocked/locked around each call
  696. * to the user function - since this function
  697. * must be allowed to make Acpi calls itself.
  698. */
  699. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  700. if (ACPI_FAILURE(status)) {
  701. return_ACPI_STATUS(status);
  702. }
  703. status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  704. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
  705. acpi_ns_get_device_callback, NULL,
  706. &info, return_value);
  707. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  708. return_ACPI_STATUS(status);
  709. }
  710. ACPI_EXPORT_SYMBOL(acpi_get_devices)
  711. /*******************************************************************************
  712. *
  713. * FUNCTION: acpi_attach_data
  714. *
  715. * PARAMETERS: obj_handle - Namespace node
  716. * handler - Handler for this attachment
  717. * data - Pointer to data to be attached
  718. *
  719. * RETURN: Status
  720. *
  721. * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
  722. *
  723. ******************************************************************************/
  724. acpi_status
  725. acpi_attach_data(acpi_handle obj_handle,
  726. acpi_object_handler handler, void *data)
  727. {
  728. struct acpi_namespace_node *node;
  729. acpi_status status;
  730. /* Parameter validation */
  731. if (!obj_handle || !handler || !data) {
  732. return (AE_BAD_PARAMETER);
  733. }
  734. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  735. if (ACPI_FAILURE(status)) {
  736. return (status);
  737. }
  738. /* Convert and validate the handle */
  739. node = acpi_ns_validate_handle(obj_handle);
  740. if (!node) {
  741. status = AE_BAD_PARAMETER;
  742. goto unlock_and_exit;
  743. }
  744. status = acpi_ns_attach_data(node, handler, data);
  745. unlock_and_exit:
  746. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  747. return (status);
  748. }
  749. ACPI_EXPORT_SYMBOL(acpi_attach_data)
  750. /*******************************************************************************
  751. *
  752. * FUNCTION: acpi_detach_data
  753. *
  754. * PARAMETERS: obj_handle - Namespace node handle
  755. * handler - Handler used in call to acpi_attach_data
  756. *
  757. * RETURN: Status
  758. *
  759. * DESCRIPTION: Remove data that was previously attached to a node.
  760. *
  761. ******************************************************************************/
  762. acpi_status
  763. acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
  764. {
  765. struct acpi_namespace_node *node;
  766. acpi_status status;
  767. /* Parameter validation */
  768. if (!obj_handle || !handler) {
  769. return (AE_BAD_PARAMETER);
  770. }
  771. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  772. if (ACPI_FAILURE(status)) {
  773. return (status);
  774. }
  775. /* Convert and validate the handle */
  776. node = acpi_ns_validate_handle(obj_handle);
  777. if (!node) {
  778. status = AE_BAD_PARAMETER;
  779. goto unlock_and_exit;
  780. }
  781. status = acpi_ns_detach_data(node, handler);
  782. unlock_and_exit:
  783. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  784. return (status);
  785. }
  786. ACPI_EXPORT_SYMBOL(acpi_detach_data)
  787. /*******************************************************************************
  788. *
  789. * FUNCTION: acpi_get_data_full
  790. *
  791. * PARAMETERS: obj_handle - Namespace node
  792. * handler - Handler used in call to attach_data
  793. * data - Where the data is returned
  794. * callback - function to execute before returning
  795. *
  796. * RETURN: Status
  797. *
  798. * DESCRIPTION: Retrieve data that was previously attached to a namespace node
  799. * and execute a callback before returning.
  800. *
  801. ******************************************************************************/
  802. acpi_status
  803. acpi_get_data_full(acpi_handle obj_handle, acpi_object_handler handler,
  804. void **data, void (*callback)(void *))
  805. {
  806. struct acpi_namespace_node *node;
  807. acpi_status status;
  808. /* Parameter validation */
  809. if (!obj_handle || !handler || !data) {
  810. return (AE_BAD_PARAMETER);
  811. }
  812. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  813. if (ACPI_FAILURE(status)) {
  814. return (status);
  815. }
  816. /* Convert and validate the handle */
  817. node = acpi_ns_validate_handle(obj_handle);
  818. if (!node) {
  819. status = AE_BAD_PARAMETER;
  820. goto unlock_and_exit;
  821. }
  822. status = acpi_ns_get_attached_data(node, handler, data);
  823. if (ACPI_SUCCESS(status) && callback) {
  824. callback(*data);
  825. }
  826. unlock_and_exit:
  827. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  828. return (status);
  829. }
  830. ACPI_EXPORT_SYMBOL(acpi_get_data_full)
  831. /*******************************************************************************
  832. *
  833. * FUNCTION: acpi_get_data
  834. *
  835. * PARAMETERS: obj_handle - Namespace node
  836. * handler - Handler used in call to attach_data
  837. * data - Where the data is returned
  838. *
  839. * RETURN: Status
  840. *
  841. * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
  842. *
  843. ******************************************************************************/
  844. acpi_status
  845. acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
  846. {
  847. return acpi_get_data_full(obj_handle, handler, data, NULL);
  848. }
  849. ACPI_EXPORT_SYMBOL(acpi_get_data)