fusb_debug.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fusb_debug.c
  15. * Date: 2022-02-11 13:33:11
  16. * LastEditTime: 2022-02-18 09:18:04
  17. * Description:  This files is for implmentation of USB debug utilities
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2022/2/7 init commit
  23. */
  24. #include "fdebug.h"
  25. #include "fusb_private.h"
  26. #define FUSB_DEBUG_TAG "FUSB_DEBUG"
  27. #define FUSB_ERROR(format, ...) FT_DEBUG_PRINT_E(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
  28. #define FUSB_WARN(format, ...) FT_DEBUG_PRINT_W(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
  29. #define FUSB_INFO(format, ...) FT_DEBUG_PRINT_I(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
  30. #define FUSB_DEBUG(format, ...) FT_DEBUG_PRINT_D(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
  31. static inline boolean FUsbIsValidStringIndex(u8 id)
  32. {
  33. return (0 != id) && (0xff != id);
  34. }
  35. /**
  36. * @name: FUsbDumpAllDescriptors
  37. * @msg: 打印USB设备的描述符信息(设备描述符,配置描述符和接口描述符)
  38. * @return {*}
  39. * @param {FUsbDev} *dev, USB设备实例,已完成初始化
  40. */
  41. void FUsbDumpAllDescriptors(FUsbDev *dev)
  42. {
  43. FError ret = FUSB_SUCCESS;
  44. if ((NULL == dev) || (NULL == dev->configuration))
  45. {
  46. return;
  47. }
  48. const FUsbDeviceDescriptor *dev_desc = NULL;
  49. const FUsbConfigurationDescriptor *config_desc = NULL;
  50. const FUsbInterfaceDescriptor *if_desc = NULL;
  51. FASSERT(dev->controller && dev->controller->usb);
  52. FUsb *instance = dev->controller->usb;
  53. u8 if_num = 0;
  54. u8 func_id = 0;
  55. FUsbConfigParser *parser = &dev->config_parser;
  56. dev_desc = dev->descriptor;
  57. config_desc = dev->configuration;
  58. /* init descriptor parser in each dump */
  59. ret = FUsbSetupConfigParser(dev, config_desc, config_desc->wTotalLength);
  60. FUsbSetupStringParser(dev);
  61. if (FUSB_SUCCESS != ret)
  62. {
  63. return;
  64. }
  65. if (FUsbIsValidStringIndex(dev_desc->iManufacturer))
  66. {
  67. ret = FUsbSearchStringDescriptor(instance, dev, dev_desc->iManufacturer);
  68. if (FUSB_SUCCESS == ret)
  69. {
  70. printf(" Manufacturer: %s\r\n", FUsbGetString(dev));
  71. }
  72. }
  73. if (FUsbIsValidStringIndex(dev_desc->iProduct))
  74. {
  75. ret = FUsbSearchStringDescriptor(instance, dev, dev_desc->iProduct);
  76. if (FUSB_SUCCESS == ret)
  77. {
  78. printf(" Product: %s\r\n", FUsbGetString(dev));
  79. }
  80. }
  81. if (FUsbIsValidStringIndex(dev_desc->iSerialNumber))
  82. {
  83. ret = FUsbSearchStringDescriptor(instance, dev, dev_desc->iSerialNumber);
  84. if (FUSB_SUCCESS == ret)
  85. {
  86. printf(" Serial No.: %s\r\n", FUsbGetString(dev));
  87. }
  88. }
  89. while (NULL != (if_desc = (const FUsbInterfaceDescriptor *)FUsbGetDescriptorFromParser(parser, FUSB_DESC_TYPE_INTERFACE)))
  90. {
  91. if (if_desc->bInterfaceNumber > if_num)
  92. {
  93. if_num = if_desc->bInterfaceNumber;
  94. }
  95. if (if_desc->bInterfaceNumber != if_num)
  96. {
  97. FUSB_INFO("Alternate setting %u ignored", if_desc->bInterfaceNumber);
  98. continue;
  99. }
  100. if (FUsbIsValidStringIndex(if_desc->iInterface))
  101. {
  102. ret = FUsbSearchStringDescriptor(instance, dev, if_desc->iInterface);
  103. if (FUSB_SUCCESS == ret)
  104. {
  105. printf(" Interface: %s\r\n", FUsbGetString(dev));
  106. }
  107. }
  108. }
  109. /* revoke descriptor parser after used */
  110. FUsbRevokeConfigParser(dev);
  111. FUsbRevokeStringParser(dev);
  112. return;
  113. }
  114. /**
  115. * @name: FUsbDumpDeviceDescriptor
  116. * @msg: 打印设备描述符信息
  117. * @return {*}
  118. * @param {FUsbDeviceDescriptor} *descriptor, 设备描述符
  119. */
  120. void FUsbDumpDeviceDescriptor(const FUsbDeviceDescriptor *descriptor)
  121. {
  122. if (NULL != descriptor)
  123. {
  124. FUSB_INFO("");
  125. FUSB_INFO("===Device Descriptor");
  126. FUSB_INFO(" bLength: %d", descriptor->bLength);
  127. FUSB_INFO(" bDescriptorType: %d", descriptor->bDescriptorType);
  128. FUSB_INFO(" bcdUSB: 0x%x", descriptor->bcdUSB);
  129. FUSB_INFO(" bDeviceClass: %d", descriptor->bDeviceClass);
  130. FUSB_INFO(" bDeviceSubClass: %d", descriptor->bDeviceSubClass);
  131. FUSB_INFO(" bDeviceProtocol: %d", descriptor->bDeviceProtocol);
  132. FUSB_INFO(" bMaxPacketSize0: %d", descriptor->bMaxPacketSize0);
  133. FUSB_INFO(" idVendor: 0x%x", descriptor->idVendor);
  134. FUSB_INFO(" idProduct: 0x%x", descriptor->idProduct);
  135. FUSB_INFO(" bcdDevice: 0x%x", descriptor->bcdDevice);
  136. FUSB_INFO(" iManufacturer: %d", descriptor->iManufacturer);
  137. FUSB_INFO(" iSerialNumber: %d", descriptor->iSerialNumber);
  138. FUSB_INFO(" bNumConfigurations: %d", descriptor->bNumConfigurations);
  139. FUSB_INFO("");
  140. }
  141. }
  142. /**
  143. * @name: FUsbDumpConfigDescriptor
  144. * @msg: 打印配置描述符信息
  145. * @return {*}
  146. * @param {FUsbConfigurationDescriptor} *descriptor, 配置描述符
  147. */
  148. void FUsbDumpConfigDescriptor(const FUsbConfigurationDescriptor *descriptor)
  149. {
  150. if (NULL != descriptor)
  151. {
  152. FUSB_INFO("");
  153. FUSB_INFO("===Configure Descriptor");
  154. FUSB_INFO(" bLength: %d", descriptor->bLength);
  155. FUSB_INFO(" bDescriptorType: %d", descriptor->bDescriptorType);
  156. FUSB_INFO(" wTotalLength: %d", descriptor->wTotalLength);
  157. FUSB_INFO(" bNumInterfaces: %d", descriptor->bNumInterfaces); /* 该配置下有多少个接口描述符 */
  158. FUSB_INFO(" bConfigurationValue: %d", descriptor->bConfigurationValue); /* 该配置描述符的配置号信息 */
  159. FUSB_INFO(" iConfiguration: %d", descriptor->iConfiguration);
  160. FUSB_INFO(" bmAttributes: 0x%x", descriptor->bmAttributes);
  161. FUSB_INFO(" remote-weakup: %s",
  162. (descriptor->bmAttributes & FUSB_CONFIG_DESC_ATTR_REMOTE_WEAKUP) ? "yes" : "no");
  163. FUSB_INFO(" self-power: %s",
  164. (descriptor->bmAttributes & FUSB_CONFIG_DESC_ATTR_SELF_POWER) ? "yes" : "no");
  165. FUSB_INFO(" usb1.0-compatible: %s",
  166. (descriptor->bmAttributes & FUSB_CONFIG_DESC_ATTR_USB1_COMPATIABLE) ? "yes" : "no");
  167. FUSB_INFO(" max power: %dmA", 2 * (descriptor->bMaxPower));
  168. FUSB_INFO(" ");
  169. }
  170. }
  171. /**
  172. * @name: FUsbDumpInterfaceDescriptor
  173. * @msg: 打印接口描述符信息
  174. * @return {*}
  175. * @param {FUsbInterfaceDescriptor} *descriptor, 接口描述符
  176. */
  177. void FUsbDumpInterfaceDescriptor(const FUsbInterfaceDescriptor *descriptor)
  178. {
  179. if (NULL != descriptor)
  180. {
  181. FUSB_INFO("");
  182. FUSB_INFO("===Interface Descriptor");
  183. FUSB_INFO(" bLength: %d", descriptor->bLength);
  184. FUSB_INFO(" bDescriptorType: %d", descriptor->bDescriptorType);
  185. FUSB_INFO(" bInterfaceNumber: %d", descriptor->bInterfaceNumber);
  186. FUSB_INFO(" bAlternateSetting: %d", descriptor->bAlternateSetting);
  187. FUSB_INFO(" bNumEndpoints: %d", descriptor->bNumEndpoints);
  188. FUSB_INFO(" bInterfaceClass: %d", descriptor->bInterfaceClass);
  189. FUSB_INFO(" bInterfaceSubClass: %d", descriptor->bInterfaceSubClass);
  190. FUSB_INFO(" bInterfaceProtocol: %d", descriptor->bInterfaceProtocol);
  191. FUSB_INFO(" iInterface: %d", descriptor->iInterface);
  192. FUSB_INFO(" ");
  193. }
  194. }
  195. /**
  196. * @name: FUsbDumpEndpointDescriptor
  197. * @msg: 打印端点描述符信息
  198. * @return {*}
  199. * @param {FUsbEndpointDescriptor} *descriptor, 端点描述符
  200. */
  201. void FUsbDumpEndpointDescriptor(const FUsbEndpointDescriptor *descriptor)
  202. {
  203. if (NULL != descriptor)
  204. {
  205. FUSB_INFO("");
  206. FUSB_INFO("===Endpoint Descriptor");
  207. FUSB_INFO(" bLength: %d", descriptor->bLength);
  208. FUSB_INFO(" bDescriptorType: %d", descriptor->bDescriptorType);
  209. FUSB_INFO(" bEndpointAddress: %d", descriptor->bEndpointAddress);
  210. FUSB_INFO(" ep num: %d", FUSB_EP_DESC_EP_NUM(descriptor->bEndpointAddress));
  211. FUSB_INFO(" ep dir: %s",
  212. (FUSB_EP_DESC_EP_DIR_IN & descriptor->bEndpointAddress) ? "IN" : "OUT");
  213. FUSB_INFO(" bmAttributes: 0x%x", descriptor->bmAttributes);
  214. FUSB_INFO(" trans type: %d ([0]-%s, [1]-%s, [2]-%s, [3]-%s)",
  215. FUSB_EP_DESC_TRANS_TYPE(descriptor->bmAttributes),
  216. "control", "isochronous", "bulk", "interrupt");
  217. FUSB_INFO(" wMaxPacketSize: %d", descriptor->wMaxPacketSize);
  218. FUSB_INFO(" bInterval: %d", descriptor->bInterval);
  219. FUSB_INFO(" ");
  220. }
  221. }