nsxfname.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /******************************************************************************
  2. *
  3. * Module Name: nsxfname - Public interfaces to the ACPI subsystem
  4. * ACPI Namespace oriented interfaces
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2012, 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 "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 acpica_device_id *dest,
  53. struct acpica_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_ns_valid_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 (!ACPI_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. struct acpi_namespace_node *node;
  139. /* Parameter validation */
  140. if (name_type > ACPI_NAME_TYPE_MAX) {
  141. return (AE_BAD_PARAMETER);
  142. }
  143. status = acpi_ut_validate_buffer(buffer);
  144. if (ACPI_FAILURE(status)) {
  145. return (status);
  146. }
  147. if (name_type == ACPI_FULL_PATHNAME) {
  148. /* Get the full pathname (From the namespace root) */
  149. status = acpi_ns_handle_to_pathname(handle, buffer);
  150. return (status);
  151. }
  152. /*
  153. * Wants the single segment ACPI name.
  154. * Validate handle and convert to a namespace Node
  155. */
  156. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  157. if (ACPI_FAILURE(status)) {
  158. return (status);
  159. }
  160. node = acpi_ns_validate_handle(handle);
  161. if (!node) {
  162. status = AE_BAD_PARAMETER;
  163. goto unlock_and_exit;
  164. }
  165. /* Validate/Allocate/Clear caller buffer */
  166. status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
  167. if (ACPI_FAILURE(status)) {
  168. goto unlock_and_exit;
  169. }
  170. /* Just copy the ACPI name from the Node and zero terminate it */
  171. ACPI_STRNCPY(buffer->pointer, acpi_ut_get_node_name(node),
  172. ACPI_NAME_SIZE);
  173. ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
  174. status = AE_OK;
  175. unlock_and_exit:
  176. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  177. return (status);
  178. }
  179. ACPI_EXPORT_SYMBOL(acpi_get_name)
  180. /******************************************************************************
  181. *
  182. * FUNCTION: acpi_ns_copy_device_id
  183. *
  184. * PARAMETERS: Dest - Pointer to the destination DEVICE_ID
  185. * Source - Pointer to the source DEVICE_ID
  186. * string_area - Pointer to where to copy the dest string
  187. *
  188. * RETURN: Pointer to the next string area
  189. *
  190. * DESCRIPTION: Copy a single DEVICE_ID, including the string data.
  191. *
  192. ******************************************************************************/
  193. static char *acpi_ns_copy_device_id(struct acpica_device_id *dest,
  194. struct acpica_device_id *source,
  195. char *string_area)
  196. {
  197. /* Create the destination DEVICE_ID */
  198. dest->string = string_area;
  199. dest->length = source->length;
  200. /* Copy actual string and return a pointer to the next string area */
  201. ACPI_MEMCPY(string_area, source->string, source->length);
  202. return (string_area + source->length);
  203. }
  204. /******************************************************************************
  205. *
  206. * FUNCTION: acpi_get_object_info
  207. *
  208. * PARAMETERS: Handle - Object Handle
  209. * return_buffer - Where the info is returned
  210. *
  211. * RETURN: Status
  212. *
  213. * DESCRIPTION: Returns information about an object as gleaned from the
  214. * namespace node and possibly by running several standard
  215. * control methods (Such as in the case of a device.)
  216. *
  217. * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
  218. * _ADR, _sx_w, and _sx_d methods.
  219. *
  220. * Note: Allocates the return buffer, must be freed by the caller.
  221. *
  222. ******************************************************************************/
  223. acpi_status
  224. acpi_get_object_info(acpi_handle handle,
  225. struct acpi_device_info **return_buffer)
  226. {
  227. struct acpi_namespace_node *node;
  228. struct acpi_device_info *info;
  229. struct acpica_device_id_list *cid_list = NULL;
  230. struct acpica_device_id *hid = NULL;
  231. struct acpica_device_id *uid = NULL;
  232. char *next_id_string;
  233. acpi_object_type type;
  234. acpi_name name;
  235. u8 param_count = 0;
  236. u8 valid = 0;
  237. u32 info_size;
  238. u32 i;
  239. acpi_status status;
  240. /* Parameter validation */
  241. if (!handle || !return_buffer) {
  242. return (AE_BAD_PARAMETER);
  243. }
  244. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  245. if (ACPI_FAILURE(status)) {
  246. goto cleanup;
  247. }
  248. node = acpi_ns_validate_handle(handle);
  249. if (!node) {
  250. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  251. return (AE_BAD_PARAMETER);
  252. }
  253. /* Get the namespace node data while the namespace is locked */
  254. info_size = sizeof(struct acpi_device_info);
  255. type = node->type;
  256. name = node->name.integer;
  257. if (node->type == ACPI_TYPE_METHOD) {
  258. param_count = node->object->method.param_count;
  259. }
  260. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  261. if (ACPI_FAILURE(status)) {
  262. return (status);
  263. }
  264. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  265. /*
  266. * Get extra info for ACPI Device/Processor objects only:
  267. * Run the Device _HID, _UID, and _CID methods.
  268. *
  269. * Note: none of these methods are required, so they may or may
  270. * not be present for this device. The Info->Valid bitfield is used
  271. * to indicate which methods were found and run successfully.
  272. */
  273. /* Execute the Device._HID method */
  274. status = acpi_ut_execute_HID(node, &hid);
  275. if (ACPI_SUCCESS(status)) {
  276. info_size += hid->length;
  277. valid |= ACPI_VALID_HID;
  278. }
  279. /* Execute the Device._UID method */
  280. status = acpi_ut_execute_UID(node, &uid);
  281. if (ACPI_SUCCESS(status)) {
  282. info_size += uid->length;
  283. valid |= ACPI_VALID_UID;
  284. }
  285. /* Execute the Device._CID method */
  286. status = acpi_ut_execute_CID(node, &cid_list);
  287. if (ACPI_SUCCESS(status)) {
  288. /* Add size of CID strings and CID pointer array */
  289. info_size +=
  290. (cid_list->list_size -
  291. sizeof(struct acpica_device_id_list));
  292. valid |= ACPI_VALID_CID;
  293. }
  294. }
  295. /*
  296. * Now that we have the variable-length data, we can allocate the
  297. * return buffer
  298. */
  299. info = ACPI_ALLOCATE_ZEROED(info_size);
  300. if (!info) {
  301. status = AE_NO_MEMORY;
  302. goto cleanup;
  303. }
  304. /* Get the fixed-length data */
  305. if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
  306. /*
  307. * Get extra info for ACPI Device/Processor objects only:
  308. * Run the _STA, _ADR and, sx_w, and _sx_d methods.
  309. *
  310. * Note: none of these methods are required, so they may or may
  311. * not be present for this device. The Info->Valid bitfield is used
  312. * to indicate which methods were found and run successfully.
  313. */
  314. /* Execute the Device._STA method */
  315. status = acpi_ut_execute_STA(node, &info->current_status);
  316. if (ACPI_SUCCESS(status)) {
  317. valid |= ACPI_VALID_STA;
  318. }
  319. /* Execute the Device._ADR method */
  320. status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
  321. &info->address);
  322. if (ACPI_SUCCESS(status)) {
  323. valid |= ACPI_VALID_ADR;
  324. }
  325. /* Execute the Device._sx_w methods */
  326. status = acpi_ut_execute_power_methods(node,
  327. acpi_gbl_lowest_dstate_names,
  328. ACPI_NUM_sx_w_METHODS,
  329. info->lowest_dstates);
  330. if (ACPI_SUCCESS(status)) {
  331. valid |= ACPI_VALID_SXWS;
  332. }
  333. /* Execute the Device._sx_d methods */
  334. status = acpi_ut_execute_power_methods(node,
  335. acpi_gbl_highest_dstate_names,
  336. ACPI_NUM_sx_d_METHODS,
  337. info->highest_dstates);
  338. if (ACPI_SUCCESS(status)) {
  339. valid |= ACPI_VALID_SXDS;
  340. }
  341. }
  342. /*
  343. * Create a pointer to the string area of the return buffer.
  344. * Point to the end of the base struct acpi_device_info structure.
  345. */
  346. next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
  347. if (cid_list) {
  348. /* Point past the CID DEVICE_ID array */
  349. next_id_string +=
  350. ((acpi_size) cid_list->count *
  351. sizeof(struct acpica_device_id));
  352. }
  353. /*
  354. * Copy the HID, UID, and CIDs to the return buffer. The variable-length
  355. * strings are copied to the reserved area at the end of the buffer.
  356. *
  357. * For HID and CID, check if the ID is a PCI Root Bridge.
  358. */
  359. if (hid) {
  360. next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
  361. hid, next_id_string);
  362. if (acpi_ut_is_pci_root_bridge(hid->string)) {
  363. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  364. }
  365. }
  366. if (uid) {
  367. next_id_string = acpi_ns_copy_device_id(&info->unique_id,
  368. uid, next_id_string);
  369. }
  370. if (cid_list) {
  371. info->compatible_id_list.count = cid_list->count;
  372. info->compatible_id_list.list_size = cid_list->list_size;
  373. /* Copy each CID */
  374. for (i = 0; i < cid_list->count; i++) {
  375. next_id_string =
  376. acpi_ns_copy_device_id(&info->compatible_id_list.
  377. ids[i], &cid_list->ids[i],
  378. next_id_string);
  379. if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
  380. info->flags |= ACPI_PCI_ROOT_BRIDGE;
  381. }
  382. }
  383. }
  384. /* Copy the fixed-length data */
  385. info->info_size = info_size;
  386. info->type = type;
  387. info->name = name;
  388. info->param_count = param_count;
  389. info->valid = valid;
  390. *return_buffer = info;
  391. status = AE_OK;
  392. cleanup:
  393. if (hid) {
  394. ACPI_FREE(hid);
  395. }
  396. if (uid) {
  397. ACPI_FREE(uid);
  398. }
  399. if (cid_list) {
  400. ACPI_FREE(cid_list);
  401. }
  402. return (status);
  403. }
  404. ACPI_EXPORT_SYMBOL(acpi_get_object_info)
  405. /******************************************************************************
  406. *
  407. * FUNCTION: acpi_install_method
  408. *
  409. * PARAMETERS: Buffer - An ACPI table containing one control method
  410. *
  411. * RETURN: Status
  412. *
  413. * DESCRIPTION: Install a control method into the namespace. If the method
  414. * name already exists in the namespace, it is overwritten. The
  415. * input buffer must contain a valid DSDT or SSDT containing a
  416. * single control method.
  417. *
  418. ******************************************************************************/
  419. acpi_status acpi_install_method(u8 *buffer)
  420. {
  421. struct acpi_table_header *table =
  422. ACPI_CAST_PTR(struct acpi_table_header, buffer);
  423. u8 *aml_buffer;
  424. u8 *aml_start;
  425. char *path;
  426. struct acpi_namespace_node *node;
  427. union acpi_operand_object *method_obj;
  428. struct acpi_parse_state parser_state;
  429. u32 aml_length;
  430. u16 opcode;
  431. u8 method_flags;
  432. acpi_status status;
  433. /* Parameter validation */
  434. if (!buffer) {
  435. return AE_BAD_PARAMETER;
  436. }
  437. /* Table must be a DSDT or SSDT */
  438. if (!ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) &&
  439. !ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
  440. return AE_BAD_HEADER;
  441. }
  442. /* First AML opcode in the table must be a control method */
  443. parser_state.aml = buffer + sizeof(struct acpi_table_header);
  444. opcode = acpi_ps_peek_opcode(&parser_state);
  445. if (opcode != AML_METHOD_OP) {
  446. return AE_BAD_PARAMETER;
  447. }
  448. /* Extract method information from the raw AML */
  449. parser_state.aml += acpi_ps_get_opcode_size(opcode);
  450. parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
  451. path = acpi_ps_get_next_namestring(&parser_state);
  452. method_flags = *parser_state.aml++;
  453. aml_start = parser_state.aml;
  454. aml_length = ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
  455. /*
  456. * Allocate resources up-front. We don't want to have to delete a new
  457. * node from the namespace if we cannot allocate memory.
  458. */
  459. aml_buffer = ACPI_ALLOCATE(aml_length);
  460. if (!aml_buffer) {
  461. return AE_NO_MEMORY;
  462. }
  463. method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
  464. if (!method_obj) {
  465. ACPI_FREE(aml_buffer);
  466. return AE_NO_MEMORY;
  467. }
  468. /* Lock namespace for acpi_ns_lookup, we may be creating a new node */
  469. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  470. if (ACPI_FAILURE(status)) {
  471. goto error_exit;
  472. }
  473. /* The lookup either returns an existing node or creates a new one */
  474. status =
  475. acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
  476. ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
  477. NULL, &node);
  478. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  479. if (ACPI_FAILURE(status)) { /* ns_lookup */
  480. if (status != AE_ALREADY_EXISTS) {
  481. goto error_exit;
  482. }
  483. /* Node existed previously, make sure it is a method node */
  484. if (node->type != ACPI_TYPE_METHOD) {
  485. status = AE_TYPE;
  486. goto error_exit;
  487. }
  488. }
  489. /* Copy the method AML to the local buffer */
  490. ACPI_MEMCPY(aml_buffer, aml_start, aml_length);
  491. /* Initialize the method object with the new method's information */
  492. method_obj->method.aml_start = aml_buffer;
  493. method_obj->method.aml_length = aml_length;
  494. method_obj->method.param_count = (u8)
  495. (method_flags & AML_METHOD_ARG_COUNT);
  496. if (method_flags & AML_METHOD_SERIALIZED) {
  497. method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
  498. method_obj->method.sync_level = (u8)
  499. ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
  500. }
  501. /*
  502. * Now that it is complete, we can attach the new method object to
  503. * the method Node (detaches/deletes any existing object)
  504. */
  505. status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
  506. /*
  507. * Flag indicates AML buffer is dynamic, must be deleted later.
  508. * Must be set only after attach above.
  509. */
  510. node->flags |= ANOBJ_ALLOCATED_BUFFER;
  511. /* Remove local reference to the method object */
  512. acpi_ut_remove_reference(method_obj);
  513. return status;
  514. error_exit:
  515. ACPI_FREE(aml_buffer);
  516. ACPI_FREE(method_obj);
  517. return status;
  518. }
  519. ACPI_EXPORT_SYMBOL(acpi_install_method)