usb: dwc3: trace: pass trace buffer size to decoding functions

Instead of assuming that our buffer is big enough, let's pass the
buffer size around so printing functions can make sure they won't
overflow the buffer.

Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
This commit is contained in:
Felipe Balbi 2019-02-04 15:43:38 +02:00
parent 3aec99154d
commit 7790b3556f
2 changed files with 80 additions and 69 deletions

View File

@ -193,65 +193,69 @@ static inline const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
* dwc3_gadget_event_string - returns event name * dwc3_gadget_event_string - returns event name
* @event: the event code * @event: the event code
*/ */
static inline const char * static inline const char *dwc3_gadget_event_string(char *str, size_t size,
dwc3_gadget_event_string(char *str, const struct dwc3_event_devt *event) const struct dwc3_event_devt *event)
{ {
enum dwc3_link_state state = event->event_info & DWC3_LINK_STATE_MASK; enum dwc3_link_state state = event->event_info & DWC3_LINK_STATE_MASK;
switch (event->type) { switch (event->type) {
case DWC3_DEVICE_EVENT_DISCONNECT: case DWC3_DEVICE_EVENT_DISCONNECT:
sprintf(str, "Disconnect: [%s]", snprintf(str, size, "Disconnect: [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_RESET: case DWC3_DEVICE_EVENT_RESET:
sprintf(str, "Reset [%s]", dwc3_gadget_link_string(state)); snprintf(str, size, "Reset [%s]",
dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_CONNECT_DONE: case DWC3_DEVICE_EVENT_CONNECT_DONE:
sprintf(str, "Connection Done [%s]", snprintf(str, size, "Connection Done [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
sprintf(str, "Link Change [%s]", snprintf(str, size, "Link Change [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_WAKEUP: case DWC3_DEVICE_EVENT_WAKEUP:
sprintf(str, "WakeUp [%s]", dwc3_gadget_link_string(state)); snprintf(str, size, "WakeUp [%s]",
dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_EOPF: case DWC3_DEVICE_EVENT_EOPF:
sprintf(str, "End-Of-Frame [%s]", snprintf(str, size, "End-Of-Frame [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_SOF: case DWC3_DEVICE_EVENT_SOF:
sprintf(str, "Start-Of-Frame [%s]", snprintf(str, size, "Start-Of-Frame [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_ERRATIC_ERROR: case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
sprintf(str, "Erratic Error [%s]", snprintf(str, size, "Erratic Error [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_CMD_CMPL: case DWC3_DEVICE_EVENT_CMD_CMPL:
sprintf(str, "Command Complete [%s]", snprintf(str, size, "Command Complete [%s]",
dwc3_gadget_link_string(state)); dwc3_gadget_link_string(state));
break; break;
case DWC3_DEVICE_EVENT_OVERFLOW: case DWC3_DEVICE_EVENT_OVERFLOW:
sprintf(str, "Overflow [%s]", dwc3_gadget_link_string(state)); snprintf(str, size, "Overflow [%s]",
dwc3_gadget_link_string(state));
break; break;
default: default:
sprintf(str, "UNKNOWN"); snprintf(str, size, "UNKNOWN");
} }
return str; return str;
} }
static inline void dwc3_decode_get_status(__u8 t, __u16 i, __u16 l, char *str) static inline void dwc3_decode_get_status(__u8 t, __u16 i, __u16 l, char *str,
size_t size)
{ {
switch (t & USB_RECIP_MASK) { switch (t & USB_RECIP_MASK) {
case USB_RECIP_INTERFACE: case USB_RECIP_INTERFACE:
sprintf(str, "Get Interface Status(Intf = %d, Length = %d)", snprintf(str, size, "Get Interface Status(Intf = %d, Length = %d)",
i, l); i, l);
break; break;
case USB_RECIP_ENDPOINT: case USB_RECIP_ENDPOINT:
sprintf(str, "Get Endpoint Status(ep%d%s)", snprintf(str, size, "Get Endpoint Status(ep%d%s)",
i & ~USB_DIR_IN, i & ~USB_DIR_IN,
i & USB_DIR_IN ? "in" : "out"); i & USB_DIR_IN ? "in" : "out");
break; break;
@ -259,11 +263,11 @@ static inline void dwc3_decode_get_status(__u8 t, __u16 i, __u16 l, char *str)
} }
static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v, static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v,
__u16 i, char *str) __u16 i, char *str, size_t size)
{ {
switch (t & USB_RECIP_MASK) { switch (t & USB_RECIP_MASK) {
case USB_RECIP_DEVICE: case USB_RECIP_DEVICE:
sprintf(str, "%s Device Feature(%s%s)", snprintf(str, size, "%s Device Feature(%s%s)",
b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
({char *s; ({char *s;
switch (v) { switch (v) {
@ -311,13 +315,13 @@ static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v,
} s; }) : ""); } s; }) : "");
break; break;
case USB_RECIP_INTERFACE: case USB_RECIP_INTERFACE:
sprintf(str, "%s Interface Feature(%s)", snprintf(str, size, "%s Interface Feature(%s)",
b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
v == USB_INTRF_FUNC_SUSPEND ? v == USB_INTRF_FUNC_SUSPEND ?
"Function Suspend" : "UNKNOWN"); "Function Suspend" : "UNKNOWN");
break; break;
case USB_RECIP_ENDPOINT: case USB_RECIP_ENDPOINT:
sprintf(str, "%s Endpoint Feature(%s ep%d%s)", snprintf(str, size, "%s Endpoint Feature(%s ep%d%s)",
b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set", b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
v == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN", v == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN",
i & ~USB_DIR_IN, i & ~USB_DIR_IN,
@ -326,15 +330,15 @@ static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v,
} }
} }
static inline void dwc3_decode_set_address(__u16 v, char *str) static inline void dwc3_decode_set_address(__u16 v, char *str, size_t size)
{ {
sprintf(str, "Set Address(Addr = %02x)", v); snprintf(str, size, "Set Address(Addr = %02x)", v);
} }
static inline void dwc3_decode_get_set_descriptor(__u8 t, __u8 b, __u16 v, static inline void dwc3_decode_get_set_descriptor(__u8 t, __u8 b, __u16 v,
__u16 i, __u16 l, char *str) __u16 i, __u16 l, char *str, size_t size)
{ {
sprintf(str, "%s %s Descriptor(Index = %d, Length = %d)", snprintf(str, size, "%s %s Descriptor(Index = %d, Length = %d)",
b == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set", b == USB_REQ_GET_DESCRIPTOR ? "Get" : "Set",
({ char *s; ({ char *s;
switch (v >> 8) { switch (v >> 8) {
@ -393,87 +397,92 @@ static inline void dwc3_decode_get_set_descriptor(__u8 t, __u8 b, __u16 v,
} }
static inline void dwc3_decode_get_configuration(__u16 l, char *str) static inline void dwc3_decode_get_configuration(__u16 l, char *str,
size_t size)
{ {
sprintf(str, "Get Configuration(Length = %d)", l); snprintf(str, size, "Get Configuration(Length = %d)", l);
} }
static inline void dwc3_decode_set_configuration(__u8 v, char *str) static inline void dwc3_decode_set_configuration(__u8 v, char *str, size_t size)
{ {
sprintf(str, "Set Configuration(Config = %d)", v); snprintf(str, size, "Set Configuration(Config = %d)", v);
} }
static inline void dwc3_decode_get_intf(__u16 i, __u16 l, char *str) static inline void dwc3_decode_get_intf(__u16 i, __u16 l, char *str,
size_t size)
{ {
sprintf(str, "Get Interface(Intf = %d, Length = %d)", i, l); snprintf(str, size, "Get Interface(Intf = %d, Length = %d)", i, l);
} }
static inline void dwc3_decode_set_intf(__u8 v, __u16 i, char *str) static inline void dwc3_decode_set_intf(__u8 v, __u16 i, char *str, size_t size)
{ {
sprintf(str, "Set Interface(Intf = %d, Alt.Setting = %d)", i, v); snprintf(str, size, "Set Interface(Intf = %d, Alt.Setting = %d)", i, v);
} }
static inline void dwc3_decode_synch_frame(__u16 i, __u16 l, char *str) static inline void dwc3_decode_synch_frame(__u16 i, __u16 l, char *str,
size_t size)
{ {
sprintf(str, "Synch Frame(Endpoint = %d, Length = %d)", i, l); snprintf(str, size, "Synch Frame(Endpoint = %d, Length = %d)", i, l);
} }
static inline void dwc3_decode_set_sel(__u16 l, char *str) static inline void dwc3_decode_set_sel(__u16 l, char *str, size_t size)
{ {
sprintf(str, "Set SEL(Length = %d)", l); snprintf(str, size, "Set SEL(Length = %d)", l);
} }
static inline void dwc3_decode_set_isoch_delay(__u8 v, char *str) static inline void dwc3_decode_set_isoch_delay(__u8 v, char *str, size_t size)
{ {
sprintf(str, "Set Isochronous Delay(Delay = %d ns)", v); snprintf(str, size, "Set Isochronous Delay(Delay = %d ns)", v);
} }
/** /**
* dwc3_decode_ctrl - returns a string represetion of ctrl request * dwc3_decode_ctrl - returns a string represetion of ctrl request
*/ */
static inline const char *dwc3_decode_ctrl(char *str, __u8 bRequestType, static inline const char *dwc3_decode_ctrl(char *str, size_t size,
__u8 bRequest, __u16 wValue, __u16 wIndex, __u16 wLength) __u8 bRequestType, __u8 bRequest, __u16 wValue, __u16 wIndex,
__u16 wLength)
{ {
switch (bRequest) { switch (bRequest) {
case USB_REQ_GET_STATUS: case USB_REQ_GET_STATUS:
dwc3_decode_get_status(bRequestType, wIndex, wLength, str); dwc3_decode_get_status(bRequestType, wIndex, wLength, str,
size);
break; break;
case USB_REQ_CLEAR_FEATURE: case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE: case USB_REQ_SET_FEATURE:
dwc3_decode_set_clear_feature(bRequestType, bRequest, wValue, dwc3_decode_set_clear_feature(bRequestType, bRequest, wValue,
wIndex, str); wIndex, str, size);
break; break;
case USB_REQ_SET_ADDRESS: case USB_REQ_SET_ADDRESS:
dwc3_decode_set_address(wValue, str); dwc3_decode_set_address(wValue, str, size);
break; break;
case USB_REQ_GET_DESCRIPTOR: case USB_REQ_GET_DESCRIPTOR:
case USB_REQ_SET_DESCRIPTOR: case USB_REQ_SET_DESCRIPTOR:
dwc3_decode_get_set_descriptor(bRequestType, bRequest, wValue, dwc3_decode_get_set_descriptor(bRequestType, bRequest, wValue,
wIndex, wLength, str); wIndex, wLength, str, size);
break; break;
case USB_REQ_GET_CONFIGURATION: case USB_REQ_GET_CONFIGURATION:
dwc3_decode_get_configuration(wLength, str); dwc3_decode_get_configuration(wLength, str, size);
break; break;
case USB_REQ_SET_CONFIGURATION: case USB_REQ_SET_CONFIGURATION:
dwc3_decode_set_configuration(wValue, str); dwc3_decode_set_configuration(wValue, str, size);
break; break;
case USB_REQ_GET_INTERFACE: case USB_REQ_GET_INTERFACE:
dwc3_decode_get_intf(wIndex, wLength, str); dwc3_decode_get_intf(wIndex, wLength, str, size);
break; break;
case USB_REQ_SET_INTERFACE: case USB_REQ_SET_INTERFACE:
dwc3_decode_set_intf(wValue, wIndex, str); dwc3_decode_set_intf(wValue, wIndex, str, size);
break; break;
case USB_REQ_SYNCH_FRAME: case USB_REQ_SYNCH_FRAME:
dwc3_decode_synch_frame(wIndex, wLength, str); dwc3_decode_synch_frame(wIndex, wLength, str, size);
break; break;
case USB_REQ_SET_SEL: case USB_REQ_SET_SEL:
dwc3_decode_set_sel(wLength, str); dwc3_decode_set_sel(wLength, str, size);
break; break;
case USB_REQ_SET_ISOCH_DELAY: case USB_REQ_SET_ISOCH_DELAY:
dwc3_decode_set_isoch_delay(wValue, str); dwc3_decode_set_isoch_delay(wValue, str, size);
break; break;
default: default:
sprintf(str, "%02x %02x %02x %02x %02x %02x %02x %02x", snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
bRequestType, bRequest, bRequestType, bRequest,
cpu_to_le16(wValue) & 0xff, cpu_to_le16(wValue) & 0xff,
cpu_to_le16(wValue) >> 8, cpu_to_le16(wValue) >> 8,
@ -490,16 +499,15 @@ static inline const char *dwc3_decode_ctrl(char *str, __u8 bRequestType,
* dwc3_ep_event_string - returns event name * dwc3_ep_event_string - returns event name
* @event: then event code * @event: then event code
*/ */
static inline const char * static inline const char *dwc3_ep_event_string(char *str, size_t size,
dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event, const struct dwc3_event_depevt *event, u32 ep0state)
u32 ep0state)
{ {
u8 epnum = event->endpoint_number; u8 epnum = event->endpoint_number;
size_t len; size_t len;
int status; int status;
int ret; int ret;
ret = sprintf(str, "ep%d%s: ", epnum >> 1, ret = snprintf(str, size, "ep%d%s: ", epnum >> 1,
(epnum & 1) ? "in" : "out"); (epnum & 1) ? "in" : "out");
if (ret < 0) if (ret < 0)
return "UNKNOWN"; return "UNKNOWN";
@ -509,7 +517,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
switch (event->endpoint_event) { switch (event->endpoint_event) {
case DWC3_DEPEVT_XFERCOMPLETE: case DWC3_DEPEVT_XFERCOMPLETE:
len = strlen(str); len = strlen(str);
sprintf(str + len, "Transfer Complete (%c%c%c)", snprintf(str + len, size - len, "Transfer Complete (%c%c%c)",
status & DEPEVT_STATUS_SHORT ? 'S' : 's', status & DEPEVT_STATUS_SHORT ? 'S' : 's',
status & DEPEVT_STATUS_IOC ? 'I' : 'i', status & DEPEVT_STATUS_IOC ? 'I' : 'i',
status & DEPEVT_STATUS_LST ? 'L' : 'l'); status & DEPEVT_STATUS_LST ? 'L' : 'l');
@ -517,12 +525,13 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
len = strlen(str); len = strlen(str);
if (epnum <= 1) if (epnum <= 1)
sprintf(str + len, " [%s]", dwc3_ep0_state_string(ep0state)); snprintf(str + len, size - len, " [%s]",
dwc3_ep0_state_string(ep0state));
break; break;
case DWC3_DEPEVT_XFERINPROGRESS: case DWC3_DEPEVT_XFERINPROGRESS:
len = strlen(str); len = strlen(str);
sprintf(str + len, "Transfer In Progress [%d] (%c%c%c)", snprintf(str + len, size - len, "Transfer In Progress [%d] (%c%c%c)",
event->parameters, event->parameters,
status & DEPEVT_STATUS_SHORT ? 'S' : 's', status & DEPEVT_STATUS_SHORT ? 'S' : 's',
status & DEPEVT_STATUS_IOC ? 'I' : 'i', status & DEPEVT_STATUS_IOC ? 'I' : 'i',
@ -531,7 +540,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
case DWC3_DEPEVT_XFERNOTREADY: case DWC3_DEPEVT_XFERNOTREADY:
len = strlen(str); len = strlen(str);
sprintf(str + len, "Transfer Not Ready [%d]%s", snprintf(str + len, size - len, "Transfer Not Ready [%d]%s",
event->parameters, event->parameters,
status & DEPEVT_STATUS_TRANSFER_ACTIVE ? status & DEPEVT_STATUS_TRANSFER_ACTIVE ?
" (Active)" : " (Not Active)"); " (Active)" : " (Not Active)");
@ -557,7 +566,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
switch (status) { switch (status) {
case DEPEVT_STREAMEVT_FOUND: case DEPEVT_STREAMEVT_FOUND:
sprintf(str + ret, " Stream %d Found", snprintf(str + ret, size - ret, " Stream %d Found",
event->parameters); event->parameters);
break; break;
case DEPEVT_STREAMEVT_NOTFOUND: case DEPEVT_STREAMEVT_NOTFOUND:
@ -571,7 +580,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
strcat(str, "Endpoint Command Complete"); strcat(str, "Endpoint Command Complete");
break; break;
default: default:
sprintf(str, "UNKNOWN"); snprintf(str, size, "UNKNOWN");
} }
return str; return str;
@ -611,14 +620,15 @@ static inline const char *dwc3_gadget_event_type_string(u8 event)
} }
} }
static inline const char *dwc3_decode_event(char *str, u32 event, u32 ep0state) static inline const char *dwc3_decode_event(char *str, size_t size, u32 event,
u32 ep0state)
{ {
const union dwc3_event evt = (union dwc3_event) event; const union dwc3_event evt = (union dwc3_event) event;
if (evt.type.is_devspec) if (evt.type.is_devspec)
return dwc3_gadget_event_string(str, &evt.devt); return dwc3_gadget_event_string(str, size, &evt.devt);
else else
return dwc3_ep_event_string(str, &evt.depevt, ep0state); return dwc3_ep_event_string(str, size, &evt.depevt, ep0state);
} }
static inline const char *dwc3_ep_cmd_status_string(int status) static inline const char *dwc3_ep_cmd_status_string(int status)

View File

@ -59,8 +59,8 @@ DECLARE_EVENT_CLASS(dwc3_log_event,
__entry->ep0state = dwc->ep0state; __entry->ep0state = dwc->ep0state;
), ),
TP_printk("event (%08x): %s", __entry->event, TP_printk("event (%08x): %s", __entry->event,
dwc3_decode_event(__get_str(str), __entry->event, dwc3_decode_event(__get_str(str), DWC3_MSG_MAX,
__entry->ep0state)) __entry->event, __entry->ep0state))
); );
DEFINE_EVENT(dwc3_log_event, dwc3_event, DEFINE_EVENT(dwc3_log_event, dwc3_event,
@ -86,7 +86,8 @@ DECLARE_EVENT_CLASS(dwc3_log_ctrl,
__entry->wIndex = le16_to_cpu(ctrl->wIndex); __entry->wIndex = le16_to_cpu(ctrl->wIndex);
__entry->wLength = le16_to_cpu(ctrl->wLength); __entry->wLength = le16_to_cpu(ctrl->wLength);
), ),
TP_printk("%s", dwc3_decode_ctrl(__get_str(str), __entry->bRequestType, TP_printk("%s", dwc3_decode_ctrl(__get_str(str), DWC3_MSG_MAX,
__entry->bRequestType,
__entry->bRequest, __entry->wValue, __entry->bRequest, __entry->wValue,
__entry->wIndex, __entry->wLength) __entry->wIndex, __entry->wLength)
) )