nsxfeval.c 28 KB

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