From: Janusz Dziedzic
> Sent: 24 January 2017 12:06
> On 23 January 2017 at 13:20, Mathias Nyman
> <[email protected]> wrote:
> > From: Felipe Balbi <[email protected]>
> >
> > If we just provide a helper to convert completion code to string, we can
> > combine all debugging messages into a single print.
> >
> > [keep the old debug messages, for warn and grep -Mathias]
> > Signed-off-by: Felipe Balbi <[email protected]>
> > Signed-off-by: Mathias Nyman <[email protected]>
> > ---
> > drivers/usb/host/xhci.h | 80
> > +++++++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 80 insertions(+)
> >
> > diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
> > index aa63e38..ebdd920 100644
> > --- a/drivers/usb/host/xhci.h
> > +++ b/drivers/usb/host/xhci.h
> > @@ -1097,6 +1097,86 @@ struct xhci_transfer_event {
> > #define COMP_SECONDARY_BANDWIDTH_ERROR 35
> > #define COMP_SPLIT_TRANSACTION_ERROR 36
> >
> > +static inline const char *xhci_trb_comp_code_string(u8 status)
> > +{
>
> BTW, maybe in the future we should use enum for status
> and next:
>
> #define C2S(x) case x: return #x
> static inline const char *xhci_trb_comp_code_string(enum
> xhci_trb_comp_code code)
> {
> switch(code) {
> C2S(COMP_INVALID);
> C2S(COMP_SUCCESS);
> ...
> default:
> return "Unknown!!";
> }
> }
> #undef C2S
A more interesting 'trick' is to get cpp to expand a macro in different ways.
So you have (if I type it correctly):
#define XHCI_ERRS(x) \
x(COMP_SUCCESS), 0, "success") \
... \
x(COMP_SECONDARY_BANDWIDTH_ERROR, 35, "secondary bandwidth error") \
x(COMP_SPLIT_TRANSACTION_ERROR, 36, "split transaction error") \
#define X(tag, val, str) tag = val,
enum (XHCI_ERRS(X) COMP_NUM_ERRS);
#undef X
const char * xhci_trb_comp_code_string(unsigned int code)
{
#define X(tag, val, str) [tag] = str,
static const char *const errmsg[] = { XHCI_ERRS(X) };
#undef X
if (code < ARRAY_SIZE(errmsg))
return errmsg[code];
...
}
/* or */
const char * xhci_trb_comp_code_string(unsigned int code)
{
#define X(tag, val, str) case tag: return str;
switch (code) {
XHCI_ERRS(X);
default:
...
#undef X
This all has the advantage of keeping the error number and message text
on the same line of the header file.
David