utids.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utids - support for device Ids - HID, UID, CID, SUB, CLS
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #define _COMPONENT ACPI_UTILITIES
  13. ACPI_MODULE_NAME("utids")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ut_execute_HID
  17. *
  18. * PARAMETERS: device_node - Node for the device
  19. * return_id - Where the string HID is returned
  20. *
  21. * RETURN: Status
  22. *
  23. * DESCRIPTION: Executes the _HID control method that returns the hardware
  24. * ID of the device. The HID is either an 32-bit encoded EISAID
  25. * Integer or a String. A string is always returned. An EISAID
  26. * is converted to a string.
  27. *
  28. * NOTE: Internal function, no parameter validation
  29. *
  30. ******************************************************************************/
  31. acpi_status
  32. acpi_ut_execute_HID(struct acpi_namespace_node *device_node,
  33. struct acpi_pnp_device_id **return_id)
  34. {
  35. union acpi_operand_object *obj_desc;
  36. struct acpi_pnp_device_id *hid;
  37. u32 length;
  38. acpi_status status;
  39. ACPI_FUNCTION_TRACE(ut_execute_HID);
  40. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__HID,
  41. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING,
  42. &obj_desc);
  43. if (ACPI_FAILURE(status)) {
  44. return_ACPI_STATUS(status);
  45. }
  46. /* Get the size of the String to be returned, includes null terminator */
  47. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  48. length = ACPI_EISAID_STRING_SIZE;
  49. } else {
  50. length = obj_desc->string.length + 1;
  51. }
  52. /* Allocate a buffer for the HID */
  53. hid =
  54. ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pnp_device_id) +
  55. (acpi_size)length);
  56. if (!hid) {
  57. status = AE_NO_MEMORY;
  58. goto cleanup;
  59. }
  60. /* Area for the string starts after PNP_DEVICE_ID struct */
  61. hid->string =
  62. ACPI_ADD_PTR(char, hid, sizeof(struct acpi_pnp_device_id));
  63. /* Convert EISAID to a string or simply copy existing string */
  64. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  65. acpi_ex_eisa_id_to_string(hid->string, obj_desc->integer.value);
  66. } else {
  67. strcpy(hid->string, obj_desc->string.pointer);
  68. }
  69. hid->length = length;
  70. *return_id = hid;
  71. cleanup:
  72. /* On exit, we must delete the return object */
  73. acpi_ut_remove_reference(obj_desc);
  74. return_ACPI_STATUS(status);
  75. }
  76. /*******************************************************************************
  77. *
  78. * FUNCTION: acpi_ut_execute_UID
  79. *
  80. * PARAMETERS: device_node - Node for the device
  81. * return_id - Where the string UID is returned
  82. *
  83. * RETURN: Status
  84. *
  85. * DESCRIPTION: Executes the _UID control method that returns the unique
  86. * ID of the device. The UID is either a 64-bit Integer (NOT an
  87. * EISAID) or a string. Always returns a string. A 64-bit integer
  88. * is converted to a decimal string.
  89. *
  90. * NOTE: Internal function, no parameter validation
  91. *
  92. ******************************************************************************/
  93. acpi_status
  94. acpi_ut_execute_UID(struct acpi_namespace_node *device_node,
  95. struct acpi_pnp_device_id **return_id)
  96. {
  97. union acpi_operand_object *obj_desc;
  98. struct acpi_pnp_device_id *uid;
  99. u32 length;
  100. acpi_status status;
  101. ACPI_FUNCTION_TRACE(ut_execute_UID);
  102. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__UID,
  103. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING,
  104. &obj_desc);
  105. if (ACPI_FAILURE(status)) {
  106. return_ACPI_STATUS(status);
  107. }
  108. /* Get the size of the String to be returned, includes null terminator */
  109. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  110. length = ACPI_MAX64_DECIMAL_DIGITS + 1;
  111. } else {
  112. length = obj_desc->string.length + 1;
  113. }
  114. /* Allocate a buffer for the UID */
  115. uid =
  116. ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pnp_device_id) +
  117. (acpi_size)length);
  118. if (!uid) {
  119. status = AE_NO_MEMORY;
  120. goto cleanup;
  121. }
  122. /* Area for the string starts after PNP_DEVICE_ID struct */
  123. uid->string =
  124. ACPI_ADD_PTR(char, uid, sizeof(struct acpi_pnp_device_id));
  125. /* Convert an Integer to string, or just copy an existing string */
  126. if (obj_desc->common.type == ACPI_TYPE_INTEGER) {
  127. acpi_ex_integer_to_string(uid->string, obj_desc->integer.value);
  128. } else {
  129. strcpy(uid->string, obj_desc->string.pointer);
  130. }
  131. uid->length = length;
  132. *return_id = uid;
  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_CID
  141. *
  142. * PARAMETERS: device_node - Node for the device
  143. * return_cid_list - Where the CID list is returned
  144. *
  145. * RETURN: Status, list of CID strings
  146. *
  147. * DESCRIPTION: Executes the _CID control method that returns one or more
  148. * compatible hardware IDs for the device.
  149. *
  150. * NOTE: Internal function, no parameter validation
  151. *
  152. * A _CID method can return either a single compatible ID or a package of
  153. * compatible IDs. Each compatible ID can be one of the following:
  154. * 1) Integer (32 bit compressed EISA ID) or
  155. * 2) String (PCI ID format, e.g. "PCI\VEN_vvvv&DEV_dddd&SUBSYS_ssssssss")
  156. *
  157. * The Integer CIDs are converted to string format by this function.
  158. *
  159. ******************************************************************************/
  160. acpi_status
  161. acpi_ut_execute_CID(struct acpi_namespace_node *device_node,
  162. struct acpi_pnp_device_id_list **return_cid_list)
  163. {
  164. union acpi_operand_object **cid_objects;
  165. union acpi_operand_object *obj_desc;
  166. struct acpi_pnp_device_id_list *cid_list;
  167. char *next_id_string;
  168. u32 string_area_size;
  169. u32 length;
  170. u32 cid_list_size;
  171. acpi_status status;
  172. u32 count;
  173. u32 i;
  174. ACPI_FUNCTION_TRACE(ut_execute_CID);
  175. /* Evaluate the _CID method for this device */
  176. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__CID,
  177. ACPI_BTYPE_INTEGER | ACPI_BTYPE_STRING
  178. | ACPI_BTYPE_PACKAGE, &obj_desc);
  179. if (ACPI_FAILURE(status)) {
  180. return_ACPI_STATUS(status);
  181. }
  182. /*
  183. * Get the count and size of the returned _CIDs. _CID can return either
  184. * a Package of Integers/Strings or a single Integer or String.
  185. * Note: This section also validates that all CID elements are of the
  186. * correct type (Integer or String).
  187. */
  188. if (obj_desc->common.type == ACPI_TYPE_PACKAGE) {
  189. count = obj_desc->package.count;
  190. cid_objects = obj_desc->package.elements;
  191. } else { /* Single Integer or String CID */
  192. count = 1;
  193. cid_objects = &obj_desc;
  194. }
  195. string_area_size = 0;
  196. for (i = 0; i < count; i++) {
  197. /* String lengths include null terminator */
  198. switch (cid_objects[i]->common.type) {
  199. case ACPI_TYPE_INTEGER:
  200. string_area_size += ACPI_EISAID_STRING_SIZE;
  201. break;
  202. case ACPI_TYPE_STRING:
  203. string_area_size += cid_objects[i]->string.length + 1;
  204. break;
  205. default:
  206. status = AE_TYPE;
  207. goto cleanup;
  208. }
  209. }
  210. /*
  211. * Now that we know the length of the CIDs, allocate return buffer:
  212. * 1) Size of the base structure +
  213. * 2) Size of the CID PNP_DEVICE_ID array +
  214. * 3) Size of the actual CID strings
  215. */
  216. cid_list_size = sizeof(struct acpi_pnp_device_id_list) +
  217. (count * sizeof(struct acpi_pnp_device_id)) + string_area_size;
  218. cid_list = ACPI_ALLOCATE_ZEROED(cid_list_size);
  219. if (!cid_list) {
  220. status = AE_NO_MEMORY;
  221. goto cleanup;
  222. }
  223. /* Area for CID strings starts after the CID PNP_DEVICE_ID array */
  224. next_id_string = ACPI_CAST_PTR(char, cid_list->ids) +
  225. ((acpi_size)count * sizeof(struct acpi_pnp_device_id));
  226. /* Copy/convert the CIDs to the return buffer */
  227. for (i = 0; i < count; i++) {
  228. if (cid_objects[i]->common.type == ACPI_TYPE_INTEGER) {
  229. /* Convert the Integer (EISAID) CID to a string */
  230. acpi_ex_eisa_id_to_string(next_id_string,
  231. cid_objects[i]->integer.
  232. value);
  233. length = ACPI_EISAID_STRING_SIZE;
  234. } else { /* ACPI_TYPE_STRING */
  235. /* Copy the String CID from the returned object */
  236. strcpy(next_id_string, cid_objects[i]->string.pointer);
  237. length = cid_objects[i]->string.length + 1;
  238. }
  239. cid_list->ids[i].string = next_id_string;
  240. cid_list->ids[i].length = length;
  241. next_id_string += length;
  242. }
  243. /* Finish the CID list */
  244. cid_list->count = count;
  245. cid_list->list_size = cid_list_size;
  246. *return_cid_list = cid_list;
  247. cleanup:
  248. /* On exit, we must delete the _CID return object */
  249. acpi_ut_remove_reference(obj_desc);
  250. return_ACPI_STATUS(status);
  251. }
  252. /*******************************************************************************
  253. *
  254. * FUNCTION: acpi_ut_execute_CLS
  255. *
  256. * PARAMETERS: device_node - Node for the device
  257. * return_id - Where the _CLS is returned
  258. *
  259. * RETURN: Status
  260. *
  261. * DESCRIPTION: Executes the _CLS control method that returns PCI-defined
  262. * class code of the device. The _CLS value is always a package
  263. * containing PCI class information as a list of integers.
  264. * The returned string has format "BBSSPP", where:
  265. * BB = Base-class code
  266. * SS = Sub-class code
  267. * PP = Programming Interface code
  268. *
  269. ******************************************************************************/
  270. acpi_status
  271. acpi_ut_execute_CLS(struct acpi_namespace_node *device_node,
  272. struct acpi_pnp_device_id **return_id)
  273. {
  274. union acpi_operand_object *obj_desc;
  275. union acpi_operand_object **cls_objects;
  276. u32 count;
  277. struct acpi_pnp_device_id *cls;
  278. u32 length;
  279. acpi_status status;
  280. u8 class_code[3] = { 0, 0, 0 };
  281. ACPI_FUNCTION_TRACE(ut_execute_CLS);
  282. status = acpi_ut_evaluate_object(device_node, METHOD_NAME__CLS,
  283. ACPI_BTYPE_PACKAGE, &obj_desc);
  284. if (ACPI_FAILURE(status)) {
  285. return_ACPI_STATUS(status);
  286. }
  287. /* Get the size of the String to be returned, includes null terminator */
  288. length = ACPI_PCICLS_STRING_SIZE;
  289. cls_objects = obj_desc->package.elements;
  290. count = obj_desc->package.count;
  291. if (obj_desc->common.type == ACPI_TYPE_PACKAGE) {
  292. if (count > 0
  293. && cls_objects[0]->common.type == ACPI_TYPE_INTEGER) {
  294. class_code[0] = (u8)cls_objects[0]->integer.value;
  295. }
  296. if (count > 1
  297. && cls_objects[1]->common.type == ACPI_TYPE_INTEGER) {
  298. class_code[1] = (u8)cls_objects[1]->integer.value;
  299. }
  300. if (count > 2
  301. && cls_objects[2]->common.type == ACPI_TYPE_INTEGER) {
  302. class_code[2] = (u8)cls_objects[2]->integer.value;
  303. }
  304. }
  305. /* Allocate a buffer for the CLS */
  306. cls =
  307. ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pnp_device_id) +
  308. (acpi_size)length);
  309. if (!cls) {
  310. status = AE_NO_MEMORY;
  311. goto cleanup;
  312. }
  313. /* Area for the string starts after PNP_DEVICE_ID struct */
  314. cls->string =
  315. ACPI_ADD_PTR(char, cls, sizeof(struct acpi_pnp_device_id));
  316. /* Simply copy existing string */
  317. acpi_ex_pci_cls_to_string(cls->string, class_code);
  318. cls->length = length;
  319. *return_id = cls;
  320. cleanup:
  321. /* On exit, we must delete the return object */
  322. acpi_ut_remove_reference(obj_desc);
  323. return_ACPI_STATUS(status);
  324. }