debug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. snprintf(str, size, "%s %s Descriptor(Index = %d, Length = %d)",
  101. bRequest == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set",
  102. ({ char *s;
  103. switch (wValue >> 8) {
  104. case USB_DT_DEVICE:
  105. s = "Device";
  106. break;
  107. case USB_DT_CONFIG:
  108. s = "Configuration";
  109. break;
  110. case USB_DT_STRING:
  111. s = "String";
  112. break;
  113. case USB_DT_INTERFACE:
  114. s = "Interface";
  115. break;
  116. case USB_DT_ENDPOINT:
  117. s = "Endpoint";
  118. break;
  119. case USB_DT_DEVICE_QUALIFIER:
  120. s = "Device Qualifier";
  121. break;
  122. case USB_DT_OTHER_SPEED_CONFIG:
  123. s = "Other Speed Config";
  124. break;
  125. case USB_DT_INTERFACE_POWER:
  126. s = "Interface Power";
  127. break;
  128. case USB_DT_OTG:
  129. s = "OTG";
  130. break;
  131. case USB_DT_DEBUG:
  132. s = "Debug";
  133. break;
  134. case USB_DT_INTERFACE_ASSOCIATION:
  135. s = "Interface Association";
  136. break;
  137. case USB_DT_BOS:
  138. s = "BOS";
  139. break;
  140. case USB_DT_DEVICE_CAPABILITY:
  141. s = "Device Capability";
  142. break;
  143. case USB_DT_PIPE_USAGE:
  144. s = "Pipe Usage";
  145. break;
  146. case USB_DT_SS_ENDPOINT_COMP:
  147. s = "SS Endpoint Companion";
  148. break;
  149. case USB_DT_SSP_ISOC_ENDPOINT_COMP:
  150. s = "SSP Isochronous Endpoint Companion";
  151. break;
  152. default:
  153. s = "UNKNOWN";
  154. break;
  155. } s; }), wValue & 0xff, wLength);
  156. }
  157. static void usb_decode_get_configuration(__u16 wLength, char *str, size_t size)
  158. {
  159. snprintf(str, size, "Get Configuration(Length = %d)", wLength);
  160. }
  161. static void usb_decode_set_configuration(__u8 wValue, char *str, size_t size)
  162. {
  163. snprintf(str, size, "Set Configuration(Config = %d)", wValue);
  164. }
  165. static void usb_decode_get_intf(__u16 wIndex, __u16 wLength, char *str,
  166. size_t size)
  167. {
  168. snprintf(str, size, "Get Interface(Intf = %d, Length = %d)",
  169. wIndex, wLength);
  170. }
  171. static void usb_decode_set_intf(__u8 wValue, __u16 wIndex, char *str,
  172. size_t size)
  173. {
  174. snprintf(str, size, "Set Interface(Intf = %d, Alt.Setting = %d)",
  175. wIndex, wValue);
  176. }
  177. static void usb_decode_synch_frame(__u16 wIndex, __u16 wLength,
  178. char *str, size_t size)
  179. {
  180. snprintf(str, size, "Synch Frame(Endpoint = %d, Length = %d)",
  181. wIndex, wLength);
  182. }
  183. static void usb_decode_set_sel(__u16 wLength, char *str, size_t size)
  184. {
  185. snprintf(str, size, "Set SEL(Length = %d)", wLength);
  186. }
  187. static void usb_decode_set_isoch_delay(__u8 wValue, char *str, size_t size)
  188. {
  189. snprintf(str, size, "Set Isochronous Delay(Delay = %d ns)", wValue);
  190. }
  191. /**
  192. * usb_decode_ctrl - returns a string representation of ctrl request
  193. */
  194. const char *usb_decode_ctrl(char *str, size_t size, __u8 bRequestType,
  195. __u8 bRequest, __u16 wValue, __u16 wIndex,
  196. __u16 wLength)
  197. {
  198. switch (bRequest) {
  199. case USB_REQ_GET_STATUS:
  200. usb_decode_get_status(bRequestType, wIndex, wLength, str, size);
  201. break;
  202. case USB_REQ_CLEAR_FEATURE:
  203. case USB_REQ_SET_FEATURE:
  204. usb_decode_set_clear_feature(bRequestType, bRequest, wValue,
  205. wIndex, str, size);
  206. break;
  207. case USB_REQ_SET_ADDRESS:
  208. usb_decode_set_address(wValue, str, size);
  209. break;
  210. case USB_REQ_GET_DESCRIPTOR:
  211. case USB_REQ_SET_DESCRIPTOR:
  212. usb_decode_get_set_descriptor(bRequestType, bRequest, wValue,
  213. wIndex, wLength, str, size);
  214. break;
  215. case USB_REQ_GET_CONFIGURATION:
  216. usb_decode_get_configuration(wLength, str, size);
  217. break;
  218. case USB_REQ_SET_CONFIGURATION:
  219. usb_decode_set_configuration(wValue, str, size);
  220. break;
  221. case USB_REQ_GET_INTERFACE:
  222. usb_decode_get_intf(wIndex, wLength, str, size);
  223. break;
  224. case USB_REQ_SET_INTERFACE:
  225. usb_decode_set_intf(wValue, wIndex, str, size);
  226. break;
  227. case USB_REQ_SYNCH_FRAME:
  228. usb_decode_synch_frame(wIndex, wLength, str, size);
  229. break;
  230. case USB_REQ_SET_SEL:
  231. usb_decode_set_sel(wLength, str, size);
  232. break;
  233. case USB_REQ_SET_ISOCH_DELAY:
  234. usb_decode_set_isoch_delay(wValue, str, size);
  235. break;
  236. default:
  237. snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
  238. bRequestType, bRequest,
  239. (u8)(cpu_to_le16(wValue) & 0xff),
  240. (u8)(cpu_to_le16(wValue) >> 8),
  241. (u8)(cpu_to_le16(wIndex) & 0xff),
  242. (u8)(cpu_to_le16(wIndex) >> 8),
  243. (u8)(cpu_to_le16(wLength) & 0xff),
  244. (u8)(cpu_to_le16(wLength) >> 8));
  245. }
  246. return str;
  247. }
  248. EXPORT_SYMBOL_GPL(usb_decode_ctrl);
  249. #endif /* __LINUX_USB_COMMON_DEBUG */