nsxfeval.c 28 KB

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