nsxfeval.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
  4. * ACPI Object evaluation interfaces
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2015, 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. #if 0
  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. if (!info->return_object) {
  311. return_buffer->length = 0;
  312. } else {
  313. if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) ==
  314. ACPI_DESC_TYPE_NAMED) {
  315. /*
  316. * If we received a NS Node as a return object, this means that
  317. * the object we are evaluating has nothing interesting to
  318. * return (such as a mutex, etc.) We return an error because
  319. * these types are essentially unsupported by this interface.
  320. * We don't check up front because this makes it easier to add
  321. * support for various types at a later date if necessary.
  322. */
  323. status = AE_TYPE;
  324. info->return_object = NULL; /* No need to delete a NS Node */
  325. return_buffer->length = 0;
  326. }
  327. if (ACPI_SUCCESS(status)) {
  328. /* Dereference Index and ref_of references */
  329. acpi_ns_resolve_references(info);
  330. /* Get the size of the returned object */
  331. status =
  332. acpi_ut_get_object_size(info->return_object,
  333. &buffer_space_needed);
  334. if (ACPI_SUCCESS(status)) {
  335. /* Validate/Allocate/Clear caller buffer */
  336. status =
  337. acpi_ut_initialize_buffer
  338. (return_buffer,
  339. buffer_space_needed);
  340. if (ACPI_FAILURE(status)) {
  341. /*
  342. * Caller's buffer is too small or a new one can't
  343. * be allocated
  344. */
  345. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  346. "Needed buffer size %X, %s\n",
  347. (u32)
  348. buffer_space_needed,
  349. acpi_format_exception
  350. (status)));
  351. } else {
  352. /* We have enough space for the object, build it */
  353. status =
  354. acpi_ut_copy_iobject_to_eobject
  355. (info->return_object,
  356. return_buffer);
  357. }
  358. }
  359. }
  360. }
  361. }
  362. if (info->return_object) {
  363. /*
  364. * Delete the internal return object. NOTE: Interpreter must be
  365. * locked to avoid race condition.
  366. */
  367. acpi_ex_enter_interpreter();
  368. /* Remove one reference on the return object (should delete it) */
  369. acpi_ut_remove_reference(info->return_object);
  370. acpi_ex_exit_interpreter();
  371. }
  372. cleanup:
  373. /* Free the input parameter list (if we created one) */
  374. if (info->parameters) {
  375. /* Free the allocated parameter block */
  376. acpi_ut_delete_internal_object_list(info->parameters);
  377. }
  378. ACPI_FREE(info);
  379. return_ACPI_STATUS(status);
  380. }
  381. ACPI_EXPORT_SYMBOL(acpi_evaluate_object)
  382. /*******************************************************************************
  383. *
  384. * FUNCTION: acpi_ns_resolve_references
  385. *
  386. * PARAMETERS: info - Evaluation info block
  387. *
  388. * RETURN: Info->return_object is replaced with the dereferenced object
  389. *
  390. * DESCRIPTION: Dereference certain reference objects. Called before an
  391. * internal return object is converted to an external union acpi_object.
  392. *
  393. * Performs an automatic dereference of Index and ref_of reference objects.
  394. * These reference objects are not supported by the union acpi_object, so this is a
  395. * last resort effort to return something useful. Also, provides compatibility
  396. * with other ACPI implementations.
  397. *
  398. * NOTE: does not handle references within returned package objects or nested
  399. * references, but this support could be added later if found to be necessary.
  400. *
  401. ******************************************************************************/
  402. static void acpi_ns_resolve_references(struct acpi_evaluate_info *info)
  403. {
  404. union acpi_operand_object *obj_desc = NULL;
  405. struct acpi_namespace_node *node;
  406. /* We are interested in reference objects only */
  407. if ((info->return_object)->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
  408. return;
  409. }
  410. /*
  411. * Two types of references are supported - those created by Index and
  412. * ref_of operators. A name reference (AML_NAMEPATH_OP) can be converted
  413. * to an union acpi_object, so it is not dereferenced here. A ddb_handle
  414. * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to
  415. * an union acpi_object.
  416. */
  417. switch (info->return_object->reference.class) {
  418. case ACPI_REFCLASS_INDEX:
  419. obj_desc = *(info->return_object->reference.where);
  420. break;
  421. case ACPI_REFCLASS_REFOF:
  422. node = info->return_object->reference.object;
  423. if (node) {
  424. obj_desc = node->object;
  425. }
  426. break;
  427. default:
  428. return;
  429. }
  430. /* Replace the existing reference object */
  431. if (obj_desc) {
  432. acpi_ut_add_reference(obj_desc);
  433. acpi_ut_remove_reference(info->return_object);
  434. info->return_object = obj_desc;
  435. }
  436. return;
  437. }
  438. /*******************************************************************************
  439. *
  440. * FUNCTION: acpi_walk_namespace
  441. *
  442. * PARAMETERS: type - acpi_object_type to search for
  443. * start_object - Handle in namespace where search begins
  444. * max_depth - Depth to which search is to reach
  445. * descending_callback - Called during tree descent
  446. * when an object of "Type" is found
  447. * ascending_callback - Called during tree ascent
  448. * when an object of "Type" is found
  449. * context - Passed to user function(s) above
  450. * return_value - Location where return value of
  451. * user_function is put if terminated early
  452. *
  453. * RETURNS Return value from the user_function if terminated early.
  454. * Otherwise, returns NULL.
  455. *
  456. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  457. * starting (and ending) at the object specified by start_handle.
  458. * The callback function is called whenever an object that matches
  459. * the type parameter is found. If the callback function returns
  460. * a non-zero value, the search is terminated immediately and this
  461. * value is returned to the caller.
  462. *
  463. * The point of this procedure is to provide a generic namespace
  464. * walk routine that can be called from multiple places to
  465. * provide multiple services; the callback function(s) can be
  466. * tailored to each task, whether it is a print function,
  467. * a compare function, etc.
  468. *
  469. ******************************************************************************/
  470. acpi_status
  471. acpi_walk_namespace(acpi_object_type type,
  472. acpi_handle start_object,
  473. u32 max_depth,
  474. acpi_walk_callback descending_callback,
  475. acpi_walk_callback ascending_callback,
  476. void *context, void **return_value)
  477. {
  478. acpi_status status;
  479. ACPI_FUNCTION_TRACE(acpi_walk_namespace);
  480. /* Parameter validation */
  481. if ((type > ACPI_TYPE_LOCAL_MAX) ||
  482. (!max_depth) || (!descending_callback && !ascending_callback)) {
  483. return_ACPI_STATUS(AE_BAD_PARAMETER);
  484. }
  485. /*
  486. * Need to acquire the namespace reader lock to prevent interference
  487. * with any concurrent table unloads (which causes the deletion of
  488. * namespace objects). We cannot allow the deletion of a namespace node
  489. * while the user function is using it. The exception to this are the
  490. * nodes created and deleted during control method execution -- these
  491. * nodes are marked as temporary nodes and are ignored by the namespace
  492. * walk. Thus, control methods can be executed while holding the
  493. * namespace deletion lock (and the user function can execute control
  494. * methods.)
  495. */
  496. status = acpi_ut_acquire_read_lock(&acpi_gbl_namespace_rw_lock);
  497. if (ACPI_FAILURE(status)) {
  498. return_ACPI_STATUS(status);
  499. }
  500. /*
  501. * Lock the namespace around the walk. The namespace will be
  502. * unlocked/locked around each call to the user function - since the user
  503. * function must be allowed to make ACPICA calls itself (for example, it
  504. * will typically execute control methods during device enumeration.)
  505. */
  506. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  507. if (ACPI_FAILURE(status)) {
  508. goto unlock_and_exit;
  509. }
  510. /* Now we can validate the starting node */
  511. if (!acpi_ns_validate_handle(start_object)) {
  512. status = AE_BAD_PARAMETER;
  513. goto unlock_and_exit2;
  514. }
  515. status = acpi_ns_walk_namespace(type, start_object, max_depth,
  516. ACPI_NS_WALK_UNLOCK,
  517. descending_callback, ascending_callback,
  518. context, return_value);
  519. unlock_and_exit2:
  520. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  521. unlock_and_exit:
  522. (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock);
  523. return_ACPI_STATUS(status);
  524. }
  525. ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
  526. /*******************************************************************************
  527. *
  528. * FUNCTION: acpi_ns_get_device_callback
  529. *
  530. * PARAMETERS: Callback from acpi_get_device
  531. *
  532. * RETURN: Status
  533. *
  534. * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
  535. * present devices, or if they specified a HID, it filters based
  536. * on that.
  537. *
  538. ******************************************************************************/
  539. static acpi_status
  540. acpi_ns_get_device_callback(acpi_handle obj_handle,
  541. u32 nesting_level,
  542. void *context, void **return_value)
  543. {
  544. struct acpi_get_devices_info *info = context;
  545. acpi_status status;
  546. struct acpi_namespace_node *node;
  547. u32 flags;
  548. struct acpi_pnp_device_id *hid;
  549. struct acpi_pnp_device_id_list *cid;
  550. u32 i;
  551. u8 found;
  552. int no_match;
  553. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  554. if (ACPI_FAILURE(status)) {
  555. return (status);
  556. }
  557. node = acpi_ns_validate_handle(obj_handle);
  558. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  559. if (ACPI_FAILURE(status)) {
  560. return (status);
  561. }
  562. if (!node) {
  563. return (AE_BAD_PARAMETER);
  564. }
  565. /*
  566. * First, filter based on the device HID and CID.
  567. *
  568. * 01/2010: For this case where a specific HID is requested, we don't
  569. * want to run _STA until we have an actual HID match. Thus, we will
  570. * not unnecessarily execute _STA on devices for which the caller
  571. * doesn't care about. Previously, _STA was executed unconditionally
  572. * on all devices found here.
  573. *
  574. * A side-effect of this change is that now we will continue to search
  575. * for a matching HID even under device trees where the parent device
  576. * would have returned a _STA that indicates it is not present or
  577. * not functioning (thus aborting the search on that branch).
  578. */
  579. if (info->hid != NULL) {
  580. status = acpi_ut_execute_HID(node, &hid);
  581. if (status == AE_NOT_FOUND) {
  582. return (AE_OK);
  583. } else if (ACPI_FAILURE(status)) {
  584. return (AE_CTRL_DEPTH);
  585. }
  586. no_match = strcmp(hid->string, info->hid);
  587. ACPI_FREE(hid);
  588. if (no_match) {
  589. /*
  590. * HID does not match, attempt match within the
  591. * list of Compatible IDs (CIDs)
  592. */
  593. status = acpi_ut_execute_CID(node, &cid);
  594. if (status == AE_NOT_FOUND) {
  595. return (AE_OK);
  596. } else if (ACPI_FAILURE(status)) {
  597. return (AE_CTRL_DEPTH);
  598. }
  599. /* Walk the CID list */
  600. found = FALSE;
  601. for (i = 0; i < cid->count; i++) {
  602. if (strcmp(cid->ids[i].string, info->hid) == 0) {
  603. /* Found a matching CID */
  604. found = TRUE;
  605. break;
  606. }
  607. }
  608. ACPI_FREE(cid);
  609. if (!found) {
  610. return (AE_OK);
  611. }
  612. }
  613. }
  614. /* Run _STA to determine if device is present */
  615. status = acpi_ut_execute_STA(node, &flags);
  616. if (ACPI_FAILURE(status)) {
  617. return (AE_CTRL_DEPTH);
  618. }
  619. if (!(flags & ACPI_STA_DEVICE_PRESENT) &&
  620. !(flags & ACPI_STA_DEVICE_FUNCTIONING)) {
  621. /*
  622. * Don't examine the children of the device only when the
  623. * device is neither present nor functional. See ACPI spec,
  624. * description of _STA for more information.
  625. */
  626. return (AE_CTRL_DEPTH);
  627. }
  628. /* We have a valid device, invoke the user function */
  629. status = info->user_function(obj_handle, nesting_level, info->context,
  630. return_value);
  631. return (status);
  632. }
  633. /*******************************************************************************
  634. *
  635. * FUNCTION: acpi_get_devices
  636. *
  637. * PARAMETERS: HID - HID to search for. Can be NULL.
  638. * user_function - Called when a matching object is found
  639. * context - Passed to user function
  640. * return_value - Location where return value of
  641. * user_function is put if terminated early
  642. *
  643. * RETURNS Return value from the user_function if terminated early.
  644. * Otherwise, returns NULL.
  645. *
  646. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  647. * starting (and ending) at the object specified by start_handle.
  648. * The user_function is called whenever an object of type
  649. * Device is found. If the user function returns
  650. * a non-zero value, the search is terminated immediately and this
  651. * value is returned to the caller.
  652. *
  653. * This is a wrapper for walk_namespace, but the callback performs
  654. * additional filtering. Please see acpi_ns_get_device_callback.
  655. *
  656. ******************************************************************************/
  657. acpi_status
  658. acpi_get_devices(const char *HID,
  659. acpi_walk_callback user_function,
  660. void *context, void **return_value)
  661. {
  662. acpi_status status;
  663. struct acpi_get_devices_info info;
  664. ACPI_FUNCTION_TRACE(acpi_get_devices);
  665. /* Parameter validation */
  666. if (!user_function) {
  667. return_ACPI_STATUS(AE_BAD_PARAMETER);
  668. }
  669. /*
  670. * We're going to call their callback from OUR callback, so we need
  671. * to know what it is, and their context parameter.
  672. */
  673. info.hid = HID;
  674. info.context = context;
  675. info.user_function = user_function;
  676. /*
  677. * Lock the namespace around the walk.
  678. * The namespace will be unlocked/locked around each call
  679. * to the user function - since this function
  680. * must be allowed to make Acpi calls itself.
  681. */
  682. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  683. if (ACPI_FAILURE(status)) {
  684. return_ACPI_STATUS(status);
  685. }
  686. status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  687. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
  688. acpi_ns_get_device_callback, NULL,
  689. &info, return_value);
  690. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  691. return_ACPI_STATUS(status);
  692. }
  693. ACPI_EXPORT_SYMBOL(acpi_get_devices)
  694. /*******************************************************************************
  695. *
  696. * FUNCTION: acpi_attach_data
  697. *
  698. * PARAMETERS: obj_handle - Namespace node
  699. * handler - Handler for this attachment
  700. * data - Pointer to data to be attached
  701. *
  702. * RETURN: Status
  703. *
  704. * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
  705. *
  706. ******************************************************************************/
  707. acpi_status
  708. acpi_attach_data(acpi_handle obj_handle,
  709. acpi_object_handler handler, void *data)
  710. {
  711. struct acpi_namespace_node *node;
  712. acpi_status status;
  713. /* Parameter validation */
  714. if (!obj_handle || !handler || !data) {
  715. return (AE_BAD_PARAMETER);
  716. }
  717. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  718. if (ACPI_FAILURE(status)) {
  719. return (status);
  720. }
  721. /* Convert and validate the handle */
  722. node = acpi_ns_validate_handle(obj_handle);
  723. if (!node) {
  724. status = AE_BAD_PARAMETER;
  725. goto unlock_and_exit;
  726. }
  727. status = acpi_ns_attach_data(node, handler, data);
  728. unlock_and_exit:
  729. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  730. return (status);
  731. }
  732. ACPI_EXPORT_SYMBOL(acpi_attach_data)
  733. /*******************************************************************************
  734. *
  735. * FUNCTION: acpi_detach_data
  736. *
  737. * PARAMETERS: obj_handle - Namespace node handle
  738. * handler - Handler used in call to acpi_attach_data
  739. *
  740. * RETURN: Status
  741. *
  742. * DESCRIPTION: Remove data that was previously attached to a node.
  743. *
  744. ******************************************************************************/
  745. acpi_status
  746. acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
  747. {
  748. struct acpi_namespace_node *node;
  749. acpi_status status;
  750. /* Parameter validation */
  751. if (!obj_handle || !handler) {
  752. return (AE_BAD_PARAMETER);
  753. }
  754. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  755. if (ACPI_FAILURE(status)) {
  756. return (status);
  757. }
  758. /* Convert and validate the handle */
  759. node = acpi_ns_validate_handle(obj_handle);
  760. if (!node) {
  761. status = AE_BAD_PARAMETER;
  762. goto unlock_and_exit;
  763. }
  764. status = acpi_ns_detach_data(node, handler);
  765. unlock_and_exit:
  766. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  767. return (status);
  768. }
  769. ACPI_EXPORT_SYMBOL(acpi_detach_data)
  770. /*******************************************************************************
  771. *
  772. * FUNCTION: acpi_get_data_full
  773. *
  774. * PARAMETERS: obj_handle - Namespace node
  775. * handler - Handler used in call to attach_data
  776. * data - Where the data is returned
  777. * callback - function to execute before returning
  778. *
  779. * RETURN: Status
  780. *
  781. * DESCRIPTION: Retrieve data that was previously attached to a namespace node
  782. * and execute a callback before returning.
  783. *
  784. ******************************************************************************/
  785. acpi_status
  786. acpi_get_data_full(acpi_handle obj_handle, acpi_object_handler handler,
  787. void **data, void (*callback)(void *))
  788. {
  789. struct acpi_namespace_node *node;
  790. acpi_status status;
  791. /* Parameter validation */
  792. if (!obj_handle || !handler || !data) {
  793. return (AE_BAD_PARAMETER);
  794. }
  795. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  796. if (ACPI_FAILURE(status)) {
  797. return (status);
  798. }
  799. /* Convert and validate the handle */
  800. node = acpi_ns_validate_handle(obj_handle);
  801. if (!node) {
  802. status = AE_BAD_PARAMETER;
  803. goto unlock_and_exit;
  804. }
  805. status = acpi_ns_get_attached_data(node, handler, data);
  806. if (ACPI_SUCCESS(status) && callback) {
  807. callback(*data);
  808. }
  809. unlock_and_exit:
  810. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  811. return (status);
  812. }
  813. ACPI_EXPORT_SYMBOL(acpi_get_data_full)
  814. /*******************************************************************************
  815. *
  816. * FUNCTION: acpi_get_data
  817. *
  818. * PARAMETERS: obj_handle - Namespace node
  819. * handler - Handler used in call to attach_data
  820. * data - Where the data is returned
  821. *
  822. * RETURN: Status
  823. *
  824. * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
  825. *
  826. ******************************************************************************/
  827. acpi_status
  828. acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
  829. {
  830. return acpi_get_data_full(obj_handle, handler, data, NULL);
  831. }
  832. ACPI_EXPORT_SYMBOL(acpi_get_data)