nsxfeval.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. status = acpi_ns_walk_namespace(type, start_object, max_depth,
  506. ACPI_NS_WALK_UNLOCK,
  507. descending_callback, ascending_callback,
  508. context, return_value);
  509. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  510. unlock_and_exit:
  511. (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock);
  512. return_ACPI_STATUS(status);
  513. }
  514. ACPI_EXPORT_SYMBOL(acpi_walk_namespace)
  515. /*******************************************************************************
  516. *
  517. * FUNCTION: acpi_ns_get_device_callback
  518. *
  519. * PARAMETERS: Callback from acpi_get_device
  520. *
  521. * RETURN: Status
  522. *
  523. * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non-
  524. * present devices, or if they specified a HID, it filters based
  525. * on that.
  526. *
  527. ******************************************************************************/
  528. static acpi_status
  529. acpi_ns_get_device_callback(acpi_handle obj_handle,
  530. u32 nesting_level,
  531. void *context, void **return_value)
  532. {
  533. struct acpi_get_devices_info *info = context;
  534. acpi_status status;
  535. struct acpi_namespace_node *node;
  536. u32 flags;
  537. struct acpi_pnp_device_id *hid;
  538. struct acpi_pnp_device_id_list *cid;
  539. u32 i;
  540. u8 found;
  541. int no_match;
  542. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  543. if (ACPI_FAILURE(status)) {
  544. return (status);
  545. }
  546. node = acpi_ns_validate_handle(obj_handle);
  547. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  548. if (ACPI_FAILURE(status)) {
  549. return (status);
  550. }
  551. if (!node) {
  552. return (AE_BAD_PARAMETER);
  553. }
  554. /*
  555. * First, filter based on the device HID and CID.
  556. *
  557. * 01/2010: For this case where a specific HID is requested, we don't
  558. * want to run _STA until we have an actual HID match. Thus, we will
  559. * not unnecessarily execute _STA on devices for which the caller
  560. * doesn't care about. Previously, _STA was executed unconditionally
  561. * on all devices found here.
  562. *
  563. * A side-effect of this change is that now we will continue to search
  564. * for a matching HID even under device trees where the parent device
  565. * would have returned a _STA that indicates it is not present or
  566. * not functioning (thus aborting the search on that branch).
  567. */
  568. if (info->hid != NULL) {
  569. status = acpi_ut_execute_HID(node, &hid);
  570. if (status == AE_NOT_FOUND) {
  571. return (AE_OK);
  572. } else if (ACPI_FAILURE(status)) {
  573. return (AE_CTRL_DEPTH);
  574. }
  575. no_match = ACPI_STRCMP(hid->string, info->hid);
  576. ACPI_FREE(hid);
  577. if (no_match) {
  578. /*
  579. * HID does not match, attempt match within the
  580. * list of Compatible IDs (CIDs)
  581. */
  582. status = acpi_ut_execute_CID(node, &cid);
  583. if (status == AE_NOT_FOUND) {
  584. return (AE_OK);
  585. } else if (ACPI_FAILURE(status)) {
  586. return (AE_CTRL_DEPTH);
  587. }
  588. /* Walk the CID list */
  589. found = FALSE;
  590. for (i = 0; i < cid->count; i++) {
  591. if (ACPI_STRCMP(cid->ids[i].string, info->hid)
  592. == 0) {
  593. /* Found a matching CID */
  594. found = TRUE;
  595. break;
  596. }
  597. }
  598. ACPI_FREE(cid);
  599. if (!found) {
  600. return (AE_OK);
  601. }
  602. }
  603. }
  604. /* Run _STA to determine if device is present */
  605. status = acpi_ut_execute_STA(node, &flags);
  606. if (ACPI_FAILURE(status)) {
  607. return (AE_CTRL_DEPTH);
  608. }
  609. if (!(flags & ACPI_STA_DEVICE_PRESENT) &&
  610. !(flags & ACPI_STA_DEVICE_FUNCTIONING)) {
  611. /*
  612. * Don't examine the children of the device only when the
  613. * device is neither present nor functional. See ACPI spec,
  614. * description of _STA for more information.
  615. */
  616. return (AE_CTRL_DEPTH);
  617. }
  618. /* We have a valid device, invoke the user function */
  619. status = info->user_function(obj_handle, nesting_level, info->context,
  620. return_value);
  621. return (status);
  622. }
  623. /*******************************************************************************
  624. *
  625. * FUNCTION: acpi_get_devices
  626. *
  627. * PARAMETERS: HID - HID to search for. Can be NULL.
  628. * user_function - Called when a matching object is found
  629. * context - Passed to user function
  630. * return_value - Location where return value of
  631. * user_function is put if terminated early
  632. *
  633. * RETURNS Return value from the user_function if terminated early.
  634. * Otherwise, returns NULL.
  635. *
  636. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  637. * starting (and ending) at the object specified by start_handle.
  638. * The user_function is called whenever an object of type
  639. * Device is found. If the user function returns
  640. * a non-zero value, the search is terminated immediately and this
  641. * value is returned to the caller.
  642. *
  643. * This is a wrapper for walk_namespace, but the callback performs
  644. * additional filtering. Please see acpi_ns_get_device_callback.
  645. *
  646. ******************************************************************************/
  647. acpi_status
  648. acpi_get_devices(const char *HID,
  649. acpi_walk_callback user_function,
  650. void *context, void **return_value)
  651. {
  652. acpi_status status;
  653. struct acpi_get_devices_info info;
  654. ACPI_FUNCTION_TRACE(acpi_get_devices);
  655. /* Parameter validation */
  656. if (!user_function) {
  657. return_ACPI_STATUS(AE_BAD_PARAMETER);
  658. }
  659. /*
  660. * We're going to call their callback from OUR callback, so we need
  661. * to know what it is, and their context parameter.
  662. */
  663. info.hid = HID;
  664. info.context = context;
  665. info.user_function = user_function;
  666. /*
  667. * Lock the namespace around the walk.
  668. * The namespace will be unlocked/locked around each call
  669. * to the user function - since this function
  670. * must be allowed to make Acpi calls itself.
  671. */
  672. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  673. if (ACPI_FAILURE(status)) {
  674. return_ACPI_STATUS(status);
  675. }
  676. status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  677. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
  678. acpi_ns_get_device_callback, NULL,
  679. &info, return_value);
  680. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  681. return_ACPI_STATUS(status);
  682. }
  683. ACPI_EXPORT_SYMBOL(acpi_get_devices)
  684. /*******************************************************************************
  685. *
  686. * FUNCTION: acpi_attach_data
  687. *
  688. * PARAMETERS: obj_handle - Namespace node
  689. * handler - Handler for this attachment
  690. * data - Pointer to data to be attached
  691. *
  692. * RETURN: Status
  693. *
  694. * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
  695. *
  696. ******************************************************************************/
  697. acpi_status
  698. acpi_attach_data(acpi_handle obj_handle,
  699. acpi_object_handler handler, void *data)
  700. {
  701. struct acpi_namespace_node *node;
  702. acpi_status status;
  703. /* Parameter validation */
  704. if (!obj_handle || !handler || !data) {
  705. return (AE_BAD_PARAMETER);
  706. }
  707. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  708. if (ACPI_FAILURE(status)) {
  709. return (status);
  710. }
  711. /* Convert and validate the handle */
  712. node = acpi_ns_validate_handle(obj_handle);
  713. if (!node) {
  714. status = AE_BAD_PARAMETER;
  715. goto unlock_and_exit;
  716. }
  717. status = acpi_ns_attach_data(node, handler, data);
  718. unlock_and_exit:
  719. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  720. return (status);
  721. }
  722. ACPI_EXPORT_SYMBOL(acpi_attach_data)
  723. /*******************************************************************************
  724. *
  725. * FUNCTION: acpi_detach_data
  726. *
  727. * PARAMETERS: obj_handle - Namespace node handle
  728. * handler - Handler used in call to acpi_attach_data
  729. *
  730. * RETURN: Status
  731. *
  732. * DESCRIPTION: Remove data that was previously attached to a node.
  733. *
  734. ******************************************************************************/
  735. acpi_status
  736. acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler)
  737. {
  738. struct acpi_namespace_node *node;
  739. acpi_status status;
  740. /* Parameter validation */
  741. if (!obj_handle || !handler) {
  742. return (AE_BAD_PARAMETER);
  743. }
  744. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  745. if (ACPI_FAILURE(status)) {
  746. return (status);
  747. }
  748. /* Convert and validate the handle */
  749. node = acpi_ns_validate_handle(obj_handle);
  750. if (!node) {
  751. status = AE_BAD_PARAMETER;
  752. goto unlock_and_exit;
  753. }
  754. status = acpi_ns_detach_data(node, handler);
  755. unlock_and_exit:
  756. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  757. return (status);
  758. }
  759. ACPI_EXPORT_SYMBOL(acpi_detach_data)
  760. /*******************************************************************************
  761. *
  762. * FUNCTION: acpi_get_data
  763. *
  764. * PARAMETERS: obj_handle - Namespace node
  765. * handler - Handler used in call to attach_data
  766. * data - Where the data is returned
  767. *
  768. * RETURN: Status
  769. *
  770. * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
  771. *
  772. ******************************************************************************/
  773. acpi_status
  774. acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data)
  775. {
  776. struct acpi_namespace_node *node;
  777. acpi_status status;
  778. /* Parameter validation */
  779. if (!obj_handle || !handler || !data) {
  780. return (AE_BAD_PARAMETER);
  781. }
  782. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  783. if (ACPI_FAILURE(status)) {
  784. return (status);
  785. }
  786. /* Convert and validate the handle */
  787. node = acpi_ns_validate_handle(obj_handle);
  788. if (!node) {
  789. status = AE_BAD_PARAMETER;
  790. goto unlock_and_exit;
  791. }
  792. status = acpi_ns_get_attached_data(node, handler, data);
  793. unlock_and_exit:
  794. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  795. return (status);
  796. }
  797. ACPI_EXPORT_SYMBOL(acpi_get_data)