nsxfeval.c 29 KB

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