debug.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * Common USB debugging functions
  4. *
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Felipe Balbi <balbi@ti.com>,
  8. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  9. */
  10. #ifndef __LINUX_USB_COMMON_DEBUG
  11. #define __LINUX_USB_COMMON_DEBUG
  12. #include <linux/usb/ch9.h>
  13. static void usb_decode_get_status(__u8 bRequestType, __u16 wIndex,
  14. __u16 wLength, char *str, size_t size)
  15. {
  16. switch (bRequestType & USB_RECIP_MASK) {
  17. case USB_RECIP_INTERFACE:
  18. snprintf(str, size,
  19. "Get Interface Status(Intf = %d, Length = %d)",
  20. wIndex, wLength);
  21. break;
  22. case USB_RECIP_ENDPOINT:
  23. snprintf(str, size, "Get Endpoint Status(ep%d%s)",
  24. wIndex & ~USB_DIR_IN,
  25. wIndex & USB_DIR_IN ? "in" : "out");
  26. break;
  27. }
  28. }
  29. static const char *usb_decode_device_feature(u16 wValue)
  30. {
  31. switch (wValue) {
  32. case USB_DEVICE_SELF_POWERED:
  33. return "Self Powered";
  34. case USB_DEVICE_REMOTE_WAKEUP:
  35. return "Remote Wakeup";
  36. case USB_DEVICE_TEST_MODE:
  37. return "Test Mode";
  38. case USB_DEVICE_U1_ENABLE:
  39. return "U1 Enable";
  40. case USB_DEVICE_U2_ENABLE:
  41. return "U2 Enable";
  42. case USB_DEVICE_LTM_ENABLE:
  43. return "LTM Enable";
  44. default:
  45. return "UNKNOWN";
  46. }
  47. }
  48. static const char *usb_decode_test_mode(u16 wIndex)
  49. {
  50. switch (wIndex) {
  51. case TEST_J:
  52. return ": TEST_J";
  53. case TEST_K:
  54. return ": TEST_K";
  55. case TEST_SE0_NAK:
  56. return ": TEST_SE0_NAK";
  57. case TEST_PACKET:
  58. return ": TEST_PACKET";
  59. case TEST_FORCE_EN:
  60. return ": TEST_FORCE_EN";
  61. default:
  62. return ": UNKNOWN";
  63. }
  64. }
  65. static void usb_decode_set_clear_feature(__u8 bRequestType,
  66. __u8 bRequest, __u16 wValue,
  67. __u16 wIndex, char *str, size_t size)
  68. {
  69. switch (bRequestType & USB_RECIP_MASK) {
  70. case USB_RECIP_DEVICE:
  71. snprintf(str, size, "%s Device Feature(%s%s)",
  72. bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
  73. usb_decode_device_feature(wValue),
  74. wValue == USB_DEVICE_TEST_MODE ?
  75. usb_decode_test_mode(wIndex) : "");
  76. break;
  77. case USB_RECIP_INTERFACE:
  78. snprintf(str, size, "%s Interface Feature(%s)",
  79. bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
  80. wValue == USB_INTRF_FUNC_SUSPEND ?
  81. "Function Suspend" : "UNKNOWN");
  82. break;
  83. case USB_RECIP_ENDPOINT:
  84. snprintf(str, size, "%s Endpoint Feature(%s ep%d%s)",
  85. bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
  86. wValue == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN",
  87. wIndex & ~USB_DIR_IN,
  88. wIndex & USB_DIR_IN ? "in" : "out");
  89. break;
  90. }
  91. }
  92. static void usb_decode_set_address(__u16 wValue, char *str, size_t size)
  93. {
  94. snprintf(str, size, "Set Address(Addr = %02x)", wValue);
  95. }
  96. static void usb_decode_get_set_descriptor(__u8 bRequestType, __u8 bRequest,
  97. __u16 wValue, __u16 wIndex,
  98. __u16 wLength, char *str, size_t size)
  99. {
  100. char *s;
  101. switch (wValue >> 8) {
  102. case USB_DT_DEVICE:
  103. s = "Device";
  104. break;
  105. case USB_DT_CONFIG:
  106. s = "Configuration";
  107. break;
  108. case USB_DT_STRING:
  109. s = "String";
  110. break;
  111. case USB_DT_INTERFACE:
  112. s = "Interface";
  113. break;
  114. case USB_DT_ENDPOINT:
  115. s = "Endpoint";
  116. break;
  117. case USB_DT_DEVICE_QUALIFIER:
  118. s = "Device Qualifier";
  119. break;
  120. case USB_DT_OTHER_SPEED_CONFIG:
  121. s = "Other Speed Config";
  122. break;
  123. case USB_DT_INTERFACE_POWER:
  124. s = "Interface Power";
  125. break;
  126. case USB_DT_OTG:
  127. s = "OTG";
  128. break;
  129. case USB_DT_DEBUG:
  130. s = "Debug";
  131. break;
  132. case USB_DT_INTERFACE_ASSOCIATION:
  133. s = "Interface Association";
  134. break;
  135. case USB_DT_BOS:
  136. s = "BOS";
  137. break;
  138. case USB_DT_DEVICE_CAPABILITY:
  139. s = "Device Capability";
  140. break;
  141. case USB_DT_PIPE_USAGE:
  142. s = "Pipe Usage";
  143. break;
  144. case USB_DT_SS_ENDPOINT_COMP:
  145. s = "SS Endpoint Companion";
  146. break;
  147. case USB_DT_SSP_ISOC_ENDPOINT_COMP:
  148. s = "SSP Isochronous Endpoint Companion";
  149. break;
  150. default:
  151. s = "UNKNOWN";
  152. break;
  153. }
  154. snprintf(str, size, "%s %s Descriptor(Index = %d, Length = %d)",
  155. bRequest == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set",
  156. s, wValue & 0xff, wLength);
  157. }
  158. static void usb_decode_get_configuration(__u16 wLength, char *str, size_t size)
  159. {
  160. snprintf(str, size, "Get Configuration(Length = %d)", wLength);
  161. }
  162. static void usb_decode_set_configuration(__u8 wValue, char *str, size_t size)
  163. {
  164. snprintf(str, size, "Set Configuration(Config = %d)", wValue);
  165. }
  166. static void usb_decode_get_intf(__u16 wIndex, __u16 wLength, char *str,
  167. size_t size)
  168. {
  169. snprintf(str, size, "Get Interface(Intf = %d, Length = %d)",
  170. wIndex, wLength);
  171. }
  172. static void usb_decode_set_intf(__u8 wValue, __u16 wIndex, char *str,
  173. size_t size)
  174. {
  175. snprintf(str, size, "Set Interface(Intf = %d, Alt.Setting = %d)",
  176. wIndex, wValue);
  177. }
  178. static void usb_decode_synch_frame(__u16 wIndex, __u16 wLength,
  179. char *str, size_t size)
  180. {
  181. snprintf(str, size, "Synch Frame(Endpoint = %d, Length = %d)",
  182. wIndex, wLength);
  183. }
  184. static void usb_decode_set_sel(__u16 wLength, char *str, size_t size)
  185. {
  186. snprintf(str, size, "Set SEL(Length = %d)", wLength);
  187. }
  188. static void usb_decode_set_isoch_delay(__u8 wValue, char *str, size_t size)
  189. {
  190. snprintf(str, size, "Set Isochronous Delay(Delay = %d ns)", wValue);
  191. }
  192. /**
  193. * usb_decode_ctrl - returns a string representation of ctrl request
  194. */
  195. const char *usb_decode_ctrl(char *str, size_t size, __u8 bRequestType,
  196. __u8 bRequest, __u16 wValue, __u16 wIndex,
  197. __u16 wLength)
  198. {
  199. switch (bRequest) {
  200. case USB_REQ_GET_STATUS:
  201. usb_decode_get_status(bRequestType, wIndex, wLength, str, size);
  202. break;
  203. case USB_REQ_CLEAR_FEATURE:
  204. case USB_REQ_SET_FEATURE:
  205. usb_decode_set_clear_feature(bRequestType, bRequest, wValue,
  206. wIndex, str, size);
  207. break;
  208. case USB_REQ_SET_ADDRESS:
  209. usb_decode_set_address(wValue, str, size);
  210. break;
  211. case USB_REQ_GET_DESCRIPTOR:
  212. case USB_REQ_SET_DESCRIPTOR:
  213. usb_decode_get_set_descriptor(bRequestType, bRequest, wValue,
  214. wIndex, wLength, str, size);
  215. break;
  216. case USB_REQ_GET_CONFIGURATION:
  217. usb_decode_get_configuration(wLength, str, size);
  218. break;
  219. case USB_REQ_SET_CONFIGURATION:
  220. usb_decode_set_configuration(wValue, str, size);
  221. break;
  222. case USB_REQ_GET_INTERFACE:
  223. usb_decode_get_intf(wIndex, wLength, str, size);
  224. break;
  225. case USB_REQ_SET_INTERFACE:
  226. usb_decode_set_intf(wValue, wIndex, str, size);
  227. break;
  228. case USB_REQ_SYNCH_FRAME:
  229. usb_decode_synch_frame(wIndex, wLength, str, size);
  230. break;
  231. case USB_REQ_SET_SEL:
  232. usb_decode_set_sel(wLength, str, size);
  233. break;
  234. case USB_REQ_SET_ISOCH_DELAY:
  235. usb_decode_set_isoch_delay(wValue, str, size);
  236. break;
  237. default:
  238. snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
  239. bRequestType, bRequest,
  240. (u8)(cpu_to_le16(wValue) & 0xff),
  241. (u8)(cpu_to_le16(wValue) >> 8),
  242. (u8)(cpu_to_le16(wIndex) & 0xff),
  243. (u8)(cpu_to_le16(wIndex) >> 8),
  244. (u8)(cpu_to_le16(wLength) & 0xff),
  245. (u8)(cpu_to_le16(wLength) >> 8));
  246. }
  247. return str;
  248. }
  249. EXPORT_SYMBOL_GPL(usb_decode_ctrl);
  250. #endif /* __LINUX_USB_COMMON_DEBUG */