utids.c 13 KB

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