debug.c 6.7 KB

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