nsxfname.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /******************************************************************************
  2. *
  3. * Module Name: nsxfname - Public interfaces to the ACPI subsystem
  4. * ACPI Namespace oriented interfaces
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2016, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #define EXPORT_ACPI_INTERFACES
  44. #include <acpi/acpi.h>
  45. #include "accommon.h"
  46. #include "acnamesp.h"
  47. #include "acparser.h"
  48. #include "amlcode.h"
  49. #define _COMPONENT ACPI_NAMESPACE
  50. ACPI_MODULE_NAME("nsxfname")
  51. /* Local prototypes */
  52. static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
  53. struct acpi_pnp_device_id *source,
  54. char *string_area);
  55. /******************************************************************************
  56. *
  57. * FUNCTION: acpi_get_handle
  58. *
  59. * PARAMETERS: parent - Object to search under (search scope).
  60. * pathname - Pointer to an asciiz string containing the
  61. * name
  62. * ret_handle - Where the return handle is returned
  63. *
  64. * RETURN: Status
  65. *
  66. * DESCRIPTION: This routine will search for a caller specified name in the
  67. * name space. The caller can restrict the search region by
  68. * specifying a non NULL parent. The parent value is itself a
  69. * namespace handle.
  70. *
  71. ******************************************************************************/
  72. acpi_status
  73. acpi_get_handle(acpi_handle parent,
  74. acpi_string pathname, acpi_handle *ret_handle)
  75. {
  76. acpi_status status;
  77. struct acpi_namespace_node *node = NULL;
  78. struct acpi_namespace_node *prefix_node = NULL;
  79. ACPI_FUNCTION_ENTRY();
  80. /* Parameter Validation */
  81. if (!ret_handle || !pathname) {
  82. return (AE_BAD_PARAMETER);
  83. }
  84. /* Convert a parent handle to a prefix node */
  85. if (parent) {
  86. prefix_node = acpi_ns_validate_handle(parent);
  87. if (!prefix_node) {
  88. return (AE_BAD_PARAMETER);
  89. }
  90. }
  91. /*
  92. * Valid cases are:
  93. * 1) Fully qualified pathname
  94. * 2) Parent + Relative pathname
  95. *
  96. * Error for <null Parent + relative path>
  97. */
  98. if (ACPI_IS_ROOT_PREFIX(pathname[0])) {
  99. /* Pathname is fully qualified (starts with '\') */
  100. /* Special case for root-only, since we can't search for it */
  101. if (!strcmp(pathname, ACPI_NS_ROOT_PATH)) {
  102. *ret_handle =
  103. ACPI_CAST_PTR(acpi_handle, acpi_gbl_root_node);
  104. return (AE_OK);
  105. }
  106. } else if (!prefix_node) {
  107. /* Relative path with null prefix is disallowed */
  108. return (AE_BAD_PARAMETER);
  109. }
  110. /* Find the Node and convert to a handle */
  111. status =
  112. acpi_ns_get_node(prefix_node, pathname, ACPI_NS_NO_UPSEARCH, &node);
  113. if (ACPI_SUCCESS(status)) {
  114. *ret_handle = ACPI_CAST_PTR(acpi_handle, node);
  115. }
  116. return (status);
  117. }
  118. ACPI_EXPORT_SYMBOL(acpi_get_handle)
  119. /******************************************************************************
  120. *
  121. * FUNCTION: acpi_get_name
  122. *
  123. * PARAMETERS: handle - Handle to be converted to a pathname
  124. * name_type - Full pathname or single segment
  125. * buffer - Buffer for returned path
  126. *
  127. * RETURN: Pointer to a string containing the fully qualified Name.
  128. *
  129. * DESCRIPTION: This routine returns the fully qualified name associated with
  130. * the Handle parameter. This and the acpi_pathname_to_handle are
  131. * complementary functions.
  132. *
  133. ******************************************************************************/
  134. acpi_status
  135. acpi_get_name(acpi_handle handle, u32 name_type, struct acpi_buffer *buffer)
  136. {
  137. acpi_status status;
  138. /* Parameter validation */
  139. if (name_type > ACPI_NAME_TYPE_MAX) {
  140. return (AE_BAD_PARAMETER);
  141. }
  142. status = acpi_ut_validate_buffer(buffer);
  143. if (ACPI_FAILURE(status)) {
  144. return (status);
  145. }
  146. /*
  147. * Wants the single segment ACPI name.
  148. * Validate handle and convert to a namespace Node
  149. */
  150. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  151. if (ACPI_FAILURE(status)) {
  152. return (status);
  153. }
  154. if (name_type == ACPI_FULL_PATHNAME ||
  155. name_type == ACPI_FULL_PATHNAME_NO_TRAILING) {
  156. /* Get the full pathname (From the namespace root) */
  157. status = acpi_ns_handle_to_pathname(handle, buffer,
  158. name_type ==
  159. ACPI_FULL_PATHNAME ? FALSE :
  160. TRUE);
  161. } else {
  162. /* Get the single name */
  163. status = acpi_ns_handle_to_name(handle, buffer);
  164. }
  165. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  166. return (status);
  167. }
  168. ACPI_EXPORT_SYMBOL(acpi_get_name)
  169. /******************************************************************************
  170. *
  171. * FUNCTION: acpi_ns_copy_device_id
  172. *
  173. * PARAMETERS: dest - Pointer to the destination PNP_DEVICE_ID
  174. * source - Pointer to the source PNP_DEVICE_ID
  175. * string_area - Pointer to where to copy the dest string
  176. *
  177. * RETURN: Pointer to the next string area
  178. *
  179. * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
  180. *
  181. ******************************************************************************/
  182. static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
  183. struct acpi_pnp_device_id *source,
  184. char *string_area)
  185. {
  186. /* Create the destination PNP_DEVICE_ID */
  187. dest->string = string_area;
  188. dest->length = source->length;
  189. /* Copy actual string and return a pointer to the next string area */
  190. memcpy(string_area, source->string, source->length);
  191. return (string_area + source->length);
  192. }
  193. /******************************************************************************
  194. *
  195. * FUNCTION: acpi_get_object_info
  196. *
  197. * PARAMETERS: handle - Object Handle
  198. * return_buffer - Where the info is returned
  199. *
  200. * RETURN: Status
  201. *
  202. * DESCRIPTION: Returns information about an object as gleaned from the
  203. * namespace node and possibly by running several standard
  204. * control methods (Such as in the case of a device.)
  205. *
  206. * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
  207. * _CLS, _ADR, _sx_w, and _sx_d methods.
  208. *
  209. * Note: Allocates the return buffer, must be freed by the caller.
  210. *
  211. * Note: This interface is intended to be used during the initial device
  212. * discovery namespace traversal. Therefore, no complex methods can be
  213. * executed, especially those that access operation regions. Therefore, do
  214. * not add any additional methods that could cause problems in this area.
  215. * this was the fate of the _SUB method which was found to cause such
  216. * problems and was removed (11/2015).
  217. *
  218. ******************************************************************************/
  219. acpi_status
  220. acpi_get_object_info(acpi_handle handle,
  221. struct acpi_device_info **return_buffer)
  222. {
  223. struct acpi_namespace_node *node;
  224. struct acpi_device_info *info;
  225. struct acpi_pnp_device_id_list *cid_list = NULL;
  226. struct acpi_pnp_device_id *hid = NULL;
  227. struct acpi_pnp_device_id *uid = NULL;
  228. struct acpi_pnp_device_id *cls = NULL;
  229. char *next_id_string;
  230. acpi_object_type type;
  231. acpi_name name;
  232. u8 param_count = 0;
  233. u16 valid = 0;
  234. u32 info_size;
  235. u32 i;
  236. acpi_status status;
  237. /* Parameter validation */
  238. if (!handle || !return_buffer) {
  239. return (AE_BAD_PARAMETER);
  240. }
  241. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  242. if (ACPI_FAILURE(status)) {
  243. return (status);
  244. }
  245. node = acpi_ns_validate_handle(handle);
  246. if (!node) {
  247. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  248. return (AE_BAD_PARAMETER);
  249. }
  250. /* Get the namespace node data while the namespace is locked */
  251. info_size = sizeof(struct acpi_device_info);
  252. type = node->type;
  253. name = node->name.integer;
  254. if (node->type == ACPI_TYPE_METHOD) {
  255. param_count = node->object->method.param_count;
  256. }
  257. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  258. if (ACPI_FAILURE(status)) {
  259. return (status);
  260. }
  261. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  262. /*
  263. * Get extra info for ACPI Device/Processor objects only:
  264. * Run the Device _HID, _UID, _CLS, and _CID methods.
  265. *
  266. * Note: none of these methods are required, so they may or may
  267. * not be present for this device. The Info->Valid bitfield is used
  268. * to indicate which methods were found and run successfully.
  269. */
  270. /* Execute the Device._HID method */
  271. status = acpi_ut_execute_HID(node, &hid);
  272. if (ACPI_SUCCESS(status)) {
  273. info_size += hid->length;
  274. valid |= ACPI_VALID_HID;
  275. }
  276. /* Execute the Device._UID method */
  277. status = acpi_ut_execute_UID(node, &uid);
  278. if (ACPI_SUCCESS(status)) {
  279. info_size += uid->length;
  280. valid |= ACPI_VALID_UID;
  281. }
  282. /* Execute the Device._CID method */
  283. status = acpi_ut_execute_CID(node, &cid_list);
  284. if (ACPI_SUCCESS(status)) {
  285. /* Add size of CID strings and CID pointer array */
  286. info_size +=
  287. (cid_list->list_size -
  288. sizeof(struct acpi_pnp_device_id_list));
  289. valid |= ACPI_VALID_CID;
  290. }
  291. /* Execute the Device._CLS method */
  292. status = acpi_ut_execute_CLS(node, &cls);
  293. if (ACPI_SUCCESS(status)) {
  294. info_size += cls->length;
  295. valid |= ACPI_VALID_CLS;
  296. }
  297. }
  298. /*
  299. * Now that we have the variable-length data, we can allocate the
  300. * return buffer
  301. */
  302. info = ACPI_ALLOCATE_ZEROED(info_size);
  303. if (!info) {
  304. status = AE_NO_MEMORY;
  305. goto cleanup;
  306. }
  307. /* Get the fixed-length data */
  308. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  309. /*
  310. * Get extra info for ACPI Device/Processor objects only:
  311. * Run the _STA, _ADR and, sx_w, and _sx_d methods.
  312. *
  313. * Notes: none of these methods are required, so they may or may
  314. * not be present for this device. The Info->Valid bitfield is used
  315. * to indicate which methods were found and run successfully.
  316. *
  317. * For _STA, if the method does not exist, then (as per the ACPI
  318. * specification), the returned current_status flags will indicate
  319. * that the device is present/functional/enabled. Otherwise, the
  320. * current_status flags reflect the value returned from _STA.
  321. */
  322. /* Execute the Device._STA method */
  323. status = acpi_ut_execute_STA(node, &info->current_status);
  324. if (ACPI_SUCCESS(status)) {
  325. valid |= ACPI_VALID_STA;
  326. }
  327. /* Execute the Device._ADR method */
  328. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
  329. &info->address);
  330. if (ACPI_SUCCESS(status)) {
  331. valid |= ACPI_VALID_ADR;
  332. }
  333. /* Execute the Device._sx_w methods */
  334. status = acpi_ut_execute_power_methods(node,
  335. acpi_gbl_lowest_dstate_names,
  336. ACPI_NUM_sx_w_METHODS,
  337. info->lowest_dstates);
  338. if (ACPI_SUCCESS(status)) {
  339. valid |= ACPI_VALID_SXWS;
  340. }
  341. /* Execute the Device._sx_d methods */
  342. status = acpi_ut_execute_power_methods(node,
  343. acpi_gbl_highest_dstate_names,
  344. ACPI_NUM_sx_d_METHODS,
  345. info->highest_dstates);
  346. if (ACPI_SUCCESS(status)) {
  347. valid |= ACPI_VALID_SXDS;
  348. }
  349. }
  350. /*
  351. * Create a pointer to the string area of the return buffer.
  352. * Point to the end of the base struct acpi_device_info structure.
  353. */
  354. next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
  355. if (cid_list) {
  356. /* Point past the CID PNP_DEVICE_ID array */
  357. next_id_string +=
  358. ((acpi_size)cid_list->count *
  359. sizeof(struct acpi_pnp_device_id));
  360. }
  361. /*
  362. * Copy the HID, UID, and CIDs to the return buffer. The variable-length
  363. * strings are copied to the reserved area at the end of the buffer.
  364. *
  365. * For HID and CID, check if the ID is a PCI Root Bridge.
  366. */
  367. if (hid) {
  368. next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
  369. hid, next_id_string);
  370. if (acpi_ut_is_pci_root_bridge(hid->string)) {
  371. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  372. }
  373. }
  374. if (uid) {
  375. next_id_string = acpi_ns_copy_device_id(&info->unique_id,
  376. uid, next_id_string);
  377. }
  378. if (cid_list) {
  379. info->compatible_id_list.count = cid_list->count;
  380. info->compatible_id_list.list_size = cid_list->list_size;
  381. /* Copy each CID */
  382. for (i = 0; i < cid_list->count; i++) {
  383. next_id_string =
  384. acpi_ns_copy_device_id(&info->compatible_id_list.
  385. ids[i], &cid_list->ids[i],
  386. next_id_string);
  387. if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
  388. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  389. }
  390. }
  391. }
  392. if (cls) {
  393. next_id_string = acpi_ns_copy_device_id(&info->class_code,
  394. cls, next_id_string);
  395. }
  396. /* Copy the fixed-length data */
  397. info->info_size = info_size;
  398. info->type = type;
  399. info->name = name;
  400. info->param_count = param_count;
  401. info->valid = valid;
  402. *return_buffer = info;
  403. status = AE_OK;
  404. cleanup:
  405. if (hid) {
  406. ACPI_FREE(hid);
  407. }
  408. if (uid) {
  409. ACPI_FREE(uid);
  410. }
  411. if (cid_list) {
  412. ACPI_FREE(cid_list);
  413. }
  414. if (cls) {
  415. ACPI_FREE(cls);
  416. }
  417. return (status);
  418. }
  419. ACPI_EXPORT_SYMBOL(acpi_get_object_info)
  420. /******************************************************************************
  421. *
  422. * FUNCTION: acpi_install_method
  423. *
  424. * PARAMETERS: buffer - An ACPI table containing one control method
  425. *
  426. * RETURN: Status
  427. *
  428. * DESCRIPTION: Install a control method into the namespace. If the method
  429. * name already exists in the namespace, it is overwritten. The
  430. * input buffer must contain a valid DSDT or SSDT containing a
  431. * single control method.
  432. *
  433. ******************************************************************************/
  434. acpi_status acpi_install_method(u8 *buffer)
  435. {
  436. struct acpi_table_header *table =
  437. ACPI_CAST_PTR(struct acpi_table_header, buffer);
  438. u8 *aml_buffer;
  439. u8 *aml_start;
  440. char *path;
  441. struct acpi_namespace_node *node;
  442. union acpi_operand_object *method_obj;
  443. struct acpi_parse_state parser_state;
  444. u32 aml_length;
  445. u16 opcode;
  446. u8 method_flags;
  447. acpi_status status;
  448. /* Parameter validation */
  449. if (!buffer) {
  450. return (AE_BAD_PARAMETER);
  451. }
  452. /* Table must be a DSDT or SSDT */
  453. if (!ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) &&
  454. !ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
  455. return (AE_BAD_HEADER);
  456. }
  457. /* First AML opcode in the table must be a control method */
  458. parser_state.aml = buffer + sizeof(struct acpi_table_header);
  459. opcode = acpi_ps_peek_opcode(&parser_state);
  460. if (opcode != AML_METHOD_OP) {
  461. return (AE_BAD_PARAMETER);
  462. }
  463. /* Extract method information from the raw AML */
  464. parser_state.aml += acpi_ps_get_opcode_size(opcode);
  465. parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
  466. path = acpi_ps_get_next_namestring(&parser_state);
  467. method_flags = *parser_state.aml++;
  468. aml_start = parser_state.aml;
  469. aml_length = ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
  470. /*
  471. * Allocate resources up-front. We don't want to have to delete a new
  472. * node from the namespace if we cannot allocate memory.
  473. */
  474. aml_buffer = ACPI_ALLOCATE(aml_length);
  475. if (!aml_buffer) {
  476. return (AE_NO_MEMORY);
  477. }
  478. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  479. if (!method_obj) {
  480. ACPI_FREE(aml_buffer);
  481. return (AE_NO_MEMORY);
  482. }
  483. /* Lock namespace for acpi_ns_lookup, we may be creating a new node */
  484. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  485. if (ACPI_FAILURE(status)) {
  486. goto error_exit;
  487. }
  488. /* The lookup either returns an existing node or creates a new one */
  489. status =
  490. acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
  491. ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
  492. NULL, &node);
  493. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  494. if (ACPI_FAILURE(status)) { /* ns_lookup */
  495. if (status != AE_ALREADY_EXISTS) {
  496. goto error_exit;
  497. }
  498. /* Node existed previously, make sure it is a method node */
  499. if (node->type != ACPI_TYPE_METHOD) {
  500. status = AE_TYPE;
  501. goto error_exit;
  502. }
  503. }
  504. /* Copy the method AML to the local buffer */
  505. memcpy(aml_buffer, aml_start, aml_length);
  506. /* Initialize the method object with the new method's information */
  507. method_obj->method.aml_start = aml_buffer;
  508. method_obj->method.aml_length = aml_length;
  509. method_obj->method.param_count = (u8)
  510. (method_flags & AML_METHOD_ARG_COUNT);
  511. if (method_flags & AML_METHOD_SERIALIZED) {
  512. method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
  513. method_obj->method.sync_level = (u8)
  514. ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
  515. }
  516. /*
  517. * Now that it is complete, we can attach the new method object to
  518. * the method Node (detaches/deletes any existing object)
  519. */
  520. status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
  521. /*
  522. * Flag indicates AML buffer is dynamic, must be deleted later.
  523. * Must be set only after attach above.
  524. */
  525. node->flags |= ANOBJ_ALLOCATED_BUFFER;
  526. /* Remove local reference to the method object */
  527. acpi_ut_remove_reference(method_obj);
  528. return (status);
  529. error_exit:
  530. ACPI_FREE(aml_buffer);
  531. ACPI_FREE(method_obj);
  532. return (status);
  533. }
  534. ACPI_EXPORT_SYMBOL(acpi_install_method)