Skip to content

Commit

Permalink
usb: dwc3: trace: pass trace buffer size to decoding functions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Felipe Balbi committed Feb 4, 2019
1 parent 3aec991 commit 7790b35
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 69 deletions.
142 changes: 76 additions & 66 deletions drivers/usb/dwc3/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,77 +193,81 @@ static inline const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
* dwc3_gadget_event_string - returns event name
* @event: the event code
*/
static inline const char *
dwc3_gadget_event_string(char *str, const struct dwc3_event_devt *event)
static inline const char *dwc3_gadget_event_string(char *str, size_t size,
const struct dwc3_event_devt *event)
{
enum dwc3_link_state state = event->event_info & DWC3_LINK_STATE_MASK;

switch (event->type) {
case DWC3_DEVICE_EVENT_DISCONNECT:
sprintf(str, "Disconnect: [%s]",
snprintf(str, size, "Disconnect: [%s]",
dwc3_gadget_link_string(state));
break;
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;
case DWC3_DEVICE_EVENT_CONNECT_DONE:
sprintf(str, "Connection Done [%s]",
snprintf(str, size, "Connection Done [%s]",
dwc3_gadget_link_string(state));
break;
case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
sprintf(str, "Link Change [%s]",
snprintf(str, size, "Link Change [%s]",
dwc3_gadget_link_string(state));
break;
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;
case DWC3_DEVICE_EVENT_EOPF:
sprintf(str, "End-Of-Frame [%s]",
snprintf(str, size, "End-Of-Frame [%s]",
dwc3_gadget_link_string(state));
break;
case DWC3_DEVICE_EVENT_SOF:
sprintf(str, "Start-Of-Frame [%s]",
snprintf(str, size, "Start-Of-Frame [%s]",
dwc3_gadget_link_string(state));
break;
case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
sprintf(str, "Erratic Error [%s]",
snprintf(str, size, "Erratic Error [%s]",
dwc3_gadget_link_string(state));
break;
case DWC3_DEVICE_EVENT_CMD_CMPL:
sprintf(str, "Command Complete [%s]",
snprintf(str, size, "Command Complete [%s]",
dwc3_gadget_link_string(state));
break;
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;
default:
sprintf(str, "UNKNOWN");
snprintf(str, size, "UNKNOWN");
}

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) {
case USB_RECIP_INTERFACE:
sprintf(str, "Get Interface Status(Intf = %d, Length = %d)",
i, l);
snprintf(str, size, "Get Interface Status(Intf = %d, Length = %d)",
i, l);
break;
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 ? "in" : "out");
break;
}
}

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) {
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",
({char *s;
switch (v) {
Expand Down Expand Up @@ -311,13 +315,13 @@ static inline void dwc3_decode_set_clear_feature(__u8 t, __u8 b, __u16 v,
} s; }) : "");
break;
case USB_RECIP_INTERFACE:
sprintf(str, "%s Interface Feature(%s)",
snprintf(str, size, "%s Interface Feature(%s)",
b == USB_REQ_CLEAR_FEATURE ? "Clear" : "Set",
v == USB_INTRF_FUNC_SUSPEND ?
"Function Suspend" : "UNKNOWN");
break;
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",
v == USB_ENDPOINT_HALT ? "Halt" : "UNKNOWN",
i & ~USB_DIR_IN,
Expand All @@ -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,
__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",
({ char *s;
switch (v >> 8) {
Expand Down Expand Up @@ -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
*/
static inline const char *dwc3_decode_ctrl(char *str, __u8 bRequestType,
__u8 bRequest, __u16 wValue, __u16 wIndex, __u16 wLength)
static inline const char *dwc3_decode_ctrl(char *str, size_t size,
__u8 bRequestType, __u8 bRequest, __u16 wValue, __u16 wIndex,
__u16 wLength)
{
switch (bRequest) {
case USB_REQ_GET_STATUS:
dwc3_decode_get_status(bRequestType, wIndex, wLength, str);
dwc3_decode_get_status(bRequestType, wIndex, wLength, str,
size);
break;
case USB_REQ_CLEAR_FEATURE:
case USB_REQ_SET_FEATURE:
dwc3_decode_set_clear_feature(bRequestType, bRequest, wValue,
wIndex, str);
wIndex, str, size);
break;
case USB_REQ_SET_ADDRESS:
dwc3_decode_set_address(wValue, str);
dwc3_decode_set_address(wValue, str, size);
break;
case USB_REQ_GET_DESCRIPTOR:
case USB_REQ_SET_DESCRIPTOR:
dwc3_decode_get_set_descriptor(bRequestType, bRequest, wValue,
wIndex, wLength, str);
wIndex, wLength, str, size);
break;
case USB_REQ_GET_CONFIGURATION:
dwc3_decode_get_configuration(wLength, str);
dwc3_decode_get_configuration(wLength, str, size);
break;
case USB_REQ_SET_CONFIGURATION:
dwc3_decode_set_configuration(wValue, str);
dwc3_decode_set_configuration(wValue, str, size);
break;
case USB_REQ_GET_INTERFACE:
dwc3_decode_get_intf(wIndex, wLength, str);
dwc3_decode_get_intf(wIndex, wLength, str, size);
break;
case USB_REQ_SET_INTERFACE:
dwc3_decode_set_intf(wValue, wIndex, str);
dwc3_decode_set_intf(wValue, wIndex, str, size);
break;
case USB_REQ_SYNCH_FRAME:
dwc3_decode_synch_frame(wIndex, wLength, str);
dwc3_decode_synch_frame(wIndex, wLength, str, size);
break;
case USB_REQ_SET_SEL:
dwc3_decode_set_sel(wLength, str);
dwc3_decode_set_sel(wLength, str, size);
break;
case USB_REQ_SET_ISOCH_DELAY:
dwc3_decode_set_isoch_delay(wValue, str);
dwc3_decode_set_isoch_delay(wValue, str, size);
break;
default:
sprintf(str, "%02x %02x %02x %02x %02x %02x %02x %02x",
snprintf(str, size, "%02x %02x %02x %02x %02x %02x %02x %02x",
bRequestType, bRequest,
cpu_to_le16(wValue) & 0xff,
cpu_to_le16(wValue) >> 8,
Expand All @@ -490,16 +499,15 @@ static inline const char *dwc3_decode_ctrl(char *str, __u8 bRequestType,
* dwc3_ep_event_string - returns event name
* @event: then event code
*/
static inline const char *
dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
u32 ep0state)
static inline const char *dwc3_ep_event_string(char *str, size_t size,
const struct dwc3_event_depevt *event, u32 ep0state)
{
u8 epnum = event->endpoint_number;
size_t len;
int status;
int ret;

ret = sprintf(str, "ep%d%s: ", epnum >> 1,
ret = snprintf(str, size, "ep%d%s: ", epnum >> 1,
(epnum & 1) ? "in" : "out");
if (ret < 0)
return "UNKNOWN";
Expand All @@ -509,20 +517,21 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
switch (event->endpoint_event) {
case DWC3_DEPEVT_XFERCOMPLETE:
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_IOC ? 'I' : 'i',
status & DEPEVT_STATUS_LST ? 'L' : 'l');

len = strlen(str);

if (epnum <= 1)
sprintf(str + len, " [%s]", dwc3_ep0_state_string(ep0state));
snprintf(str + len, size - len, " [%s]",
dwc3_ep0_state_string(ep0state));
break;
case DWC3_DEPEVT_XFERINPROGRESS:
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,
status & DEPEVT_STATUS_SHORT ? 'S' : 's',
status & DEPEVT_STATUS_IOC ? 'I' : 'i',
Expand All @@ -531,7 +540,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
case DWC3_DEPEVT_XFERNOTREADY:
len = strlen(str);

sprintf(str + len, "Transfer Not Ready [%d]%s",
snprintf(str + len, size - len, "Transfer Not Ready [%d]%s",
event->parameters,
status & DEPEVT_STATUS_TRANSFER_ACTIVE ?
" (Active)" : " (Not Active)");
Expand All @@ -557,7 +566,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,

switch (status) {
case DEPEVT_STREAMEVT_FOUND:
sprintf(str + ret, " Stream %d Found",
snprintf(str + ret, size - ret, " Stream %d Found",
event->parameters);
break;
case DEPEVT_STREAMEVT_NOTFOUND:
Expand All @@ -571,7 +580,7 @@ dwc3_ep_event_string(char *str, const struct dwc3_event_depevt *event,
strcat(str, "Endpoint Command Complete");
break;
default:
sprintf(str, "UNKNOWN");
snprintf(str, size, "UNKNOWN");
}

return str;
Expand Down Expand Up @@ -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;

if (evt.type.is_devspec)
return dwc3_gadget_event_string(str, &evt.devt);
return dwc3_gadget_event_string(str, size, &evt.devt);
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)
Expand Down
7 changes: 4 additions & 3 deletions drivers/usb/dwc3/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ DECLARE_EVENT_CLASS(dwc3_log_event,
__entry->ep0state = dwc->ep0state;
),
TP_printk("event (%08x): %s", __entry->event,
dwc3_decode_event(__get_str(str), __entry->event,
__entry->ep0state))
dwc3_decode_event(__get_str(str), DWC3_MSG_MAX,
__entry->event, __entry->ep0state))
);

DEFINE_EVENT(dwc3_log_event, dwc3_event,
Expand All @@ -86,7 +86,8 @@ DECLARE_EVENT_CLASS(dwc3_log_ctrl,
__entry->wIndex = le16_to_cpu(ctrl->wIndex);
__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->wIndex, __entry->wLength)
)
Expand Down

0 comments on commit 7790b35

Please sign in to comment.