debug.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common USB debugging functions
  4. *
  5. * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://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 USB_TEST_J:
  53. return ": TEST_J";
  54. case USB_TEST_K:
  55. return ": TEST_K";
  56. case USB_TEST_SE0_NAK:
  57. return ": TEST_SE0_NAK";
  58. case USB_TEST_PACKET:
  59. return ": TEST_PACKET";
  60. case USB_TEST_FORCE_ENABLE:
  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 human readable representation of control request.
  195. * @str: buffer to return a human-readable representation of control request.
  196. * This buffer should have about 200 bytes.
  197. * @size: size of str buffer.
  198. * @bRequestType: matches the USB bmRequestType field
  199. * @bRequest: matches the USB bRequest field
  200. * @wValue: matches the USB wValue field (CPU byte order)
  201. * @wIndex: matches the USB wIndex field (CPU byte order)
  202. * @wLength: matches the USB wLength field (CPU byte order)
  203. *
  204. * Function returns decoded, formatted and human-readable description of
  205. * control request packet.
  206. *
  207. * The usage scenario for this is for tracepoints, so function as a return
  208. * use the same value as in parameters. This approach allows to use this
  209. * function in TP_printk
  210. *
  211. * Important: wValue, wIndex, wLength parameters before invoking this function
  212. * should be processed by le16_to_cpu macro.
  213. */
  214. const char *usb_decode_ctrl(char *str, size_t size, __u8 bRequestType,
  215. __u8 bRequest, __u16 wValue, __u16 wIndex,
  216. __u16 wLength)
  217. {
  218. switch (bRequest) {
  219. case USB_REQ_GET_STATUS:
  220. usb_decode_get_status(bRequestType, wIndex, wLength, str, size);
  221. break;
  222. case USB_REQ_CLEAR_FEATURE:
  223. case USB_REQ_SET_FEATURE:
  224. usb_decode_set_clear_feature(bRequestType, bRequest, wValue,
  225. wIndex, str, size);
  226. break;
  227. case USB_REQ_SET_ADDRESS:
  228. usb_decode_set_address(wValue, str, size);
  229. break;
  230. case USB_REQ_GET_DESCRIPTOR:
  231. case USB_REQ_SET_DESCRIPTOR:
  232. usb_decode_get_set_descriptor(bRequestType, bRequest, wValue,
  233. wIndex, wLength, str, size);
  234. break;
  235. case USB_REQ_GET_CONFIGURATION:
  236. usb_decode_get_configuration(wLength, str, size);
  237. break;
  238. case USB_REQ_SET_CONFIGURATION:
  239. usb_decode_set_configuration(wValue, str, size);
  240. break;
  241. case USB_REQ_GET_INTERFACE:
  242. usb_decode_get_intf(wIndex, wLength, str, size);
  243. break;
  244. case USB_REQ_SET_INTERFACE:
  245. usb_decode_set_intf(wValue, wIndex, str, size);
  246. break;
  247. case USB_REQ_SYNCH_FRAME:
  248. usb_decode_synch_frame(wIndex, wLength, str, size);
  249. break;
  250. case USB_REQ_SET_SEL:
  251. usb_decode_set_sel(wLength, str, size);
  252. break;
  253. case USB_REQ_SET_ISOCH_DELAY:
  254. usb_decode_set_isoch_delay(wValue, str, size);
  255. break;
  256. default:
  257. snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
  258. bRequestType, bRequest,
  259. (u8)(cpu_to_le16(wValue) & 0xff),
  260. (u8)(cpu_to_le16(wValue) >> 8),
  261. (u8)(cpu_to_le16(wIndex) & 0xff),
  262. (u8)(cpu_to_le16(wIndex) >> 8),
  263. (u8)(cpu_to_le16(wLength) & 0xff),
  264. (u8)(cpu_to_le16(wLength) >> 8));
  265. }
  266. return str;
  267. }
  268. EXPORT_SYMBOL_GPL(usb_decode_ctrl);