utids.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /******************************************************************************
  2. *
  3. * Module Name: utids - support for device IDs - HID, UID, CID
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2009, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acinterp.h"
  45. #define _COMPONENT ACPI_UTILITIES
  46. ACPI_MODULE_NAME("utids")
  47. /* Local prototypes */
  48. static void acpi_ut_copy_id_string(char *destination, char *source);
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ut_copy_id_string
  52. *
  53. * PARAMETERS: Destination - Where to copy the string
  54. * Source - Source string
  55. *
  56. * RETURN: None
  57. *
  58. * DESCRIPTION: Copies an ID string for the _HID, _CID, and _UID methods.
  59. * Performs removal of a leading asterisk if present -- workaround
  60. * for a known issue on a bunch of machines.
  61. *
  62. ******************************************************************************/
  63. static void acpi_ut_copy_id_string(char *destination, char *source)
  64. {
  65. /*
  66. * Workaround for ID strings that have a leading asterisk. This construct
  67. * is not allowed by the ACPI specification (ID strings must be
  68. * alphanumeric), but enough existing machines have this embedded in their
  69. * ID strings that the following code is useful.
  70. */
  71. if (*source == '*') {
  72. source++;
  73. }
  74. /* Do the actual copy */
  75. ACPI_STRCPY(destination, source);
  76. }
  77. /*******************************************************************************
  78. *
  79. * FUNCTION: acpi_ut_execute_HID
  80. *
  81. * PARAMETERS: device_node - Node for the device
  82. * return_id - Where the string HID is returned
  83. *
  84. * RETURN: Status
  85. *
  86. * DESCRIPTION: Executes the _HID control method that returns the hardware
  87. * ID of the device. The HID is either an 32-bit encoded EISAID
  88. * Integer or a String. A string is always returned. An EISAID
  89. * is converted to a string.
  90. *
  91. * NOTE: Internal function, no parameter validation
  92. *
  93. ******************************************************************************/
  94. acpi_status
  95. acpi_ut_execute_HID(struct acpi_namespace_node *device_node,
  96. struct acpica_device_id **return_id)
  97. {
  98. union acpi_operand_object *obj_desc;
  99. struct acpica_device_id *hid;
  100. u32 length;
  101. acpi_status status;
  102. ACPI_FUNCTION_TRACE(ut_execute_HID);
  103. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__HID,
  104. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING,
  105. &obj_desc);
  106. if (ACPI_FAILURE(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. /* Get the size of the String to be returned, includes null terminator */
  110. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  111. length = ACPI_EISAID_STRING_SIZE;
  112. } else {
  113. length = obj_desc->string.length + 1;
  114. }
  115. /* Allocate a buffer for the HID */
  116. hid =
  117. ACPI_ALLOCATE_ZEROED(sizeof(struct acpica_device_id) +
  118. (acpi_size) length);
  119. if (!hid) {
  120. status = AE_NO_MEMORY;
  121. goto cleanup;
  122. }
  123. /* Area for the string starts after DEVICE_ID struct */
  124. hid->string = ACPI_ADD_PTR(char, hid, sizeof(struct acpica_device_id));
  125. /* Convert EISAID to a string or simply copy existing string */
  126. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  127. acpi_ex_eisa_id_to_string(hid->string, obj_desc->integer.value);
  128. } else {
  129. acpi_ut_copy_id_string(hid->string, obj_desc->string.pointer);
  130. }
  131. hid->length = length;
  132. *return_id = hid;
  133. cleanup:
  134. /* On exit, we must delete the return object */
  135. acpi_ut_remove_reference(obj_desc);
  136. return_ACPI_STATUS(status);
  137. }
  138. /*******************************************************************************
  139. *
  140. * FUNCTION: acpi_ut_execute_UID
  141. *
  142. * PARAMETERS: device_node - Node for the device
  143. * return_id - Where the string UID is returned
  144. *
  145. * RETURN: Status
  146. *
  147. * DESCRIPTION: Executes the _UID control method that returns the unique
  148. * ID of the device. The UID is either a 64-bit Integer (NOT an
  149. * EISAID) or a string. Always returns a string. A 64-bit integer
  150. * is converted to a decimal string.
  151. *
  152. * NOTE: Internal function, no parameter validation
  153. *
  154. ******************************************************************************/
  155. acpi_status
  156. acpi_ut_execute_UID(struct acpi_namespace_node *device_node,
  157. struct acpica_device_id **return_id)
  158. {
  159. union acpi_operand_object *obj_desc;
  160. struct acpica_device_id *uid;
  161. u32 length;
  162. acpi_status status;
  163. ACPI_FUNCTION_TRACE(ut_execute_UID);
  164. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__UID,
  165. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING,
  166. &obj_desc);
  167. if (ACPI_FAILURE(status)) {
  168. return_ACPI_STATUS(status);
  169. }
  170. /* Get the size of the String to be returned, includes null terminator */
  171. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  172. length = ACPI_MAX64_DECIMAL_DIGITS + 1;
  173. } else {
  174. length = obj_desc->string.length + 1;
  175. }
  176. /* Allocate a buffer for the UID */
  177. uid =
  178. ACPI_ALLOCATE_ZEROED(sizeof(struct acpica_device_id) +
  179. (acpi_size) length);
  180. if (!uid) {
  181. status = AE_NO_MEMORY;
  182. goto cleanup;
  183. }
  184. /* Area for the string starts after DEVICE_ID struct */
  185. uid->string = ACPI_ADD_PTR(char, uid, sizeof(struct acpica_device_id));
  186. /* Convert an Integer to string, or just copy an existing string */
  187. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  188. acpi_ex_integer_to_string(uid->string, obj_desc->integer.value);
  189. } else {
  190. acpi_ut_copy_id_string(uid->string, obj_desc->string.pointer);
  191. }
  192. uid->length = length;
  193. *return_id = uid;
  194. cleanup:
  195. /* On exit, we must delete the return object */
  196. acpi_ut_remove_reference(obj_desc);
  197. return_ACPI_STATUS(status);
  198. }
  199. /*******************************************************************************
  200. *
  201. * FUNCTION: acpi_ut_execute_CID
  202. *
  203. * PARAMETERS: device_node - Node for the device
  204. * return_cid_list - Where the CID list is returned
  205. *
  206. * RETURN: Status, list of CID strings
  207. *
  208. * DESCRIPTION: Executes the _CID control method that returns one or more
  209. * compatible hardware IDs for the device.
  210. *
  211. * NOTE: Internal function, no parameter validation
  212. *
  213. * A _CID method can return either a single compatible ID or a package of
  214. * compatible IDs. Each compatible ID can be one of the following:
  215. * 1) Integer (32 bit compressed EISA ID) or
  216. * 2) String (PCI ID format, e.g. "PCI\VEN_vvvv&DEV_dddd&SUBSYS_ssssssss")
  217. *
  218. * The Integer CIDs are converted to string format by this function.
  219. *
  220. ******************************************************************************/
  221. acpi_status
  222. acpi_ut_execute_CID(struct acpi_namespace_node *device_node,
  223. struct acpica_device_id_list **return_cid_list)
  224. {
  225. union acpi_operand_object **cid_objects;
  226. union acpi_operand_object *obj_desc;
  227. struct acpica_device_id_list *cid_list;
  228. char *next_id_string;
  229. u32 string_area_size;
  230. u32 length;
  231. u32 cid_list_size;
  232. acpi_status status;
  233. u32 count;
  234. u32 i;
  235. ACPI_FUNCTION_TRACE(ut_execute_CID);
  236. /* Evaluate the _CID method for this device */
  237. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__CID,
  238. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING
  239. | ACPI_BTYPE_PACKAGE, &obj_desc);
  240. if (ACPI_FAILURE(status)) {
  241. return_ACPI_STATUS(status);
  242. }
  243. /*
  244. * Get the count and size of the returned _CIDs. _CID can return either
  245. * a Package of Integers/Strings or a single Integer or String.
  246. * Note: This section also validates that all CID elements are of the
  247. * correct type (Integer or String).
  248. */
  249. if (obj_desc->common.type == ACPI_TYPE_PACKAGE) {
  250. count = obj_desc->package.count;
  251. cid_objects = obj_desc->package.elements;
  252. } else { /* Single Integer or String CID */
  253. count = 1;
  254. cid_objects = &obj_desc;
  255. }
  256. string_area_size = 0;
  257. for (i = 0; i < count; i++) {
  258. /* String lengths include null terminator */
  259. switch (cid_objects[i]->common.type) {
  260. case ACPI_TYPE_INTEGER:
  261. string_area_size += ACPI_EISAID_STRING_SIZE;
  262. break;
  263. case ACPI_TYPE_STRING:
  264. string_area_size += cid_objects[i]->string.length + 1;
  265. break;
  266. default:
  267. status = AE_TYPE;
  268. goto cleanup;
  269. }
  270. }
  271. /*
  272. * Now that we know the length of the CIDs, allocate return buffer:
  273. * 1) Size of the base structure +
  274. * 2) Size of the CID DEVICE_ID array +
  275. * 3) Size of the actual CID strings
  276. */
  277. cid_list_size = sizeof(struct acpica_device_id_list) +
  278. ((count - 1) * sizeof(struct acpica_device_id)) + string_area_size;
  279. cid_list = ACPI_ALLOCATE_ZEROED(cid_list_size);
  280. if (!cid_list) {
  281. status = AE_NO_MEMORY;
  282. goto cleanup;
  283. }
  284. /* Area for CID strings starts after the CID DEVICE_ID array */
  285. next_id_string = ACPI_CAST_PTR(char, cid_list->ids) +
  286. ((acpi_size) count * sizeof(struct acpica_device_id));
  287. /* Copy/convert the CIDs to the return buffer */
  288. for (i = 0; i < count; i++) {
  289. if (cid_objects[i]->common.type == ACPI_TYPE_INTEGER) {
  290. /* Convert the Integer (EISAID) CID to a string */
  291. acpi_ex_eisa_id_to_string(next_id_string,
  292. cid_objects[i]->integer.
  293. value);
  294. length = ACPI_EISAID_STRING_SIZE;
  295. } else { /* ACPI_TYPE_STRING */
  296. /* Copy the String CID from the returned object */
  297. acpi_ut_copy_id_string(next_id_string,
  298. cid_objects[i]->string.pointer);
  299. length = cid_objects[i]->string.length + 1;
  300. }
  301. cid_list->ids[i].string = next_id_string;
  302. cid_list->ids[i].length = length;
  303. next_id_string += length;
  304. }
  305. /* Finish the CID list */
  306. cid_list->count = count;
  307. cid_list->list_size = cid_list_size;
  308. *return_cid_list = cid_list;
  309. cleanup:
  310. /* On exit, we must delete the _CID return object */
  311. acpi_ut_remove_reference(obj_desc);
  312. return_ACPI_STATUS(status);
  313. }