1 // SPDX-License-Identifier: GPL-2.0
3 * Common USB debugging functions
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
11 #include <linux/kernel.h>
12 #include <linux/usb/ch9.h>
14 static void usb_decode_get_status(__u8 bRequestType, __u16 wIndex,
15 __u16 wLength, char *str, size_t size)
17 switch (bRequestType & USB_RECIP_MASK) {
18 case USB_RECIP_DEVICE:
19 snprintf(str, size, "Get Device Status(Length = %d)", wLength);
21 case USB_RECIP_INTERFACE:
23 "Get Interface Status(Intf = %d, Length = %d)",
26 case USB_RECIP_ENDPOINT:
27 snprintf(str, size, "Get Endpoint Status(ep%d%s)",
29 wIndex & USB_DIR_IN ? "in" : "out");
34 static const char *usb_decode_device_feature(u16 wValue)
37 case USB_DEVICE_SELF_POWERED:
38 return "Self Powered";
39 case USB_DEVICE_REMOTE_WAKEUP:
40 return "Remote Wakeup";
41 case USB_DEVICE_TEST_MODE:
43 case USB_DEVICE_U1_ENABLE:
45 case USB_DEVICE_U2_ENABLE:
47 case USB_DEVICE_LTM_ENABLE:
54 static const char *usb_decode_test_mode(u16 wIndex)
61 case USB_TEST_SE0_NAK:
62 return ": TEST_SE0_NAK";
64 return ": TEST_PACKET";
65 case USB_TEST_FORCE_ENABLE:
66 return ": TEST_FORCE_EN";
72 static void usb_decode_set_clear_feature(__u8 bRequestType,
73 __u8 bRequest, __u16 wValue,
74 __u16 wIndex, char *str, size_t size)
76 switch (bRequestType & USB_RECIP_MASK) {
77 case USB_RECIP_DEVICE:
78 snprintf(str, size, "%s Device Feature(%s%s)",
79 bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
80 usb_decode_device_feature(wValue),
81 wValue == USB_DEVICE_TEST_MODE ?
82 usb_decode_test_mode(wIndex) : "");
84 case USB_RECIP_INTERFACE:
85 snprintf(str, size, "%s Interface Feature(%s)",
86 bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
87 wValue == USB_INTRF_FUNC_SUSPEND ?
88 "Function Suspend" : "UNKNOWN");
90 case USB_RECIP_ENDPOINT:
91 snprintf(str, size, "%s Endpoint Feature(%s ep%d%s)",
92 bRequest == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
93 wValue == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN",
95 wIndex & USB_DIR_IN ? "in" : "out");
100 static void usb_decode_set_address(__u16 wValue, char *str, size_t size)
102 snprintf(str, size, "Set Address(Addr = %02x)", wValue);
105 static void usb_decode_get_set_descriptor(__u8 bRequestType, __u8 bRequest,
106 __u16 wValue, __u16 wIndex,
107 __u16 wLength, char *str, size_t size)
111 switch (wValue >> 8) {
121 case USB_DT_INTERFACE:
124 case USB_DT_ENDPOINT:
127 case USB_DT_DEVICE_QUALIFIER:
128 s = "Device Qualifier";
130 case USB_DT_OTHER_SPEED_CONFIG:
131 s = "Other Speed Config";
133 case USB_DT_INTERFACE_POWER:
134 s = "Interface Power";
142 case USB_DT_INTERFACE_ASSOCIATION:
143 s = "Interface Association";
148 case USB_DT_DEVICE_CAPABILITY:
149 s = "Device Capability";
151 case USB_DT_PIPE_USAGE:
154 case USB_DT_SS_ENDPOINT_COMP:
155 s = "SS Endpoint Companion";
157 case USB_DT_SSP_ISOC_ENDPOINT_COMP:
158 s = "SSP Isochronous Endpoint Companion";
165 snprintf(str, size, "%s %s Descriptor(Index = %d, Length = %d)",
166 bRequest == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set",
167 s, wValue & 0xff, wLength);
170 static void usb_decode_get_configuration(__u16 wLength, char *str, size_t size)
172 snprintf(str, size, "Get Configuration(Length = %d)", wLength);
175 static void usb_decode_set_configuration(__u8 wValue, char *str, size_t size)
177 snprintf(str, size, "Set Configuration(Config = %d)", wValue);
180 static void usb_decode_get_intf(__u16 wIndex, __u16 wLength, char *str,
183 snprintf(str, size, "Get Interface(Intf = %d, Length = %d)",
187 static void usb_decode_set_intf(__u8 wValue, __u16 wIndex, char *str,
190 snprintf(str, size, "Set Interface(Intf = %d, Alt.Setting = %d)",
194 static void usb_decode_synch_frame(__u16 wIndex, __u16 wLength,
195 char *str, size_t size)
197 snprintf(str, size, "Synch Frame(Endpoint = %d, Length = %d)",
201 static void usb_decode_set_sel(__u16 wLength, char *str, size_t size)
203 snprintf(str, size, "Set SEL(Length = %d)", wLength);
206 static void usb_decode_set_isoch_delay(__u8 wValue, char *str, size_t size)
208 snprintf(str, size, "Set Isochronous Delay(Delay = %d ns)", wValue);
212 * usb_decode_ctrl - Returns human readable representation of control request.
213 * @str: buffer to return a human-readable representation of control request.
214 * This buffer should have about 200 bytes.
215 * @size: size of str buffer.
216 * @bRequestType: matches the USB bmRequestType field
217 * @bRequest: matches the USB bRequest field
218 * @wValue: matches the USB wValue field (CPU byte order)
219 * @wIndex: matches the USB wIndex field (CPU byte order)
220 * @wLength: matches the USB wLength field (CPU byte order)
222 * Function returns decoded, formatted and human-readable description of
223 * control request packet.
225 * The usage scenario for this is for tracepoints, so function as a return
226 * use the same value as in parameters. This approach allows to use this
227 * function in TP_printk
229 * Important: wValue, wIndex, wLength parameters before invoking this function
230 * should be processed by le16_to_cpu macro.
232 const char *usb_decode_ctrl(char *str, size_t size, __u8 bRequestType,
233 __u8 bRequest, __u16 wValue, __u16 wIndex,
237 case USB_REQ_GET_STATUS:
238 usb_decode_get_status(bRequestType, wIndex, wLength, str, size);
240 case USB_REQ_CLEAR_FEATURE:
241 case USB_REQ_SET_FEATURE:
242 usb_decode_set_clear_feature(bRequestType, bRequest, wValue,
245 case USB_REQ_SET_ADDRESS:
246 usb_decode_set_address(wValue, str, size);
248 case USB_REQ_GET_DESCRIPTOR:
249 case USB_REQ_SET_DESCRIPTOR:
250 usb_decode_get_set_descriptor(bRequestType, bRequest, wValue,
251 wIndex, wLength, str, size);
253 case USB_REQ_GET_CONFIGURATION:
254 usb_decode_get_configuration(wLength, str, size);
256 case USB_REQ_SET_CONFIGURATION:
257 usb_decode_set_configuration(wValue, str, size);
259 case USB_REQ_GET_INTERFACE:
260 usb_decode_get_intf(wIndex, wLength, str, size);
262 case USB_REQ_SET_INTERFACE:
263 usb_decode_set_intf(wValue, wIndex, str, size);
265 case USB_REQ_SYNCH_FRAME:
266 usb_decode_synch_frame(wIndex, wLength, str, size);
268 case USB_REQ_SET_SEL:
269 usb_decode_set_sel(wLength, str, size);
271 case USB_REQ_SET_ISOCH_DELAY:
272 usb_decode_set_isoch_delay(wValue, str, size);
275 snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
276 bRequestType, bRequest,
277 (u8)(cpu_to_le16(wValue) & 0xff),
278 (u8)(cpu_to_le16(wValue) >> 8),
279 (u8)(cpu_to_le16(wIndex) & 0xff),
280 (u8)(cpu_to_le16(wIndex) >> 8),
281 (u8)(cpu_to_le16(wLength) & 0xff),
282 (u8)(cpu_to_le16(wLength) >> 8));
287 EXPORT_SYMBOL_GPL(usb_decode_ctrl);