usb: dwc3: gadget: increase readability of dwc3_gadget_init_endpoints()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Mon, 12 Jun 2017 12:11:25 +0000 (15:11 +0300)
committerFelipe Balbi <felipe.balbi@linux.intel.com>
Tue, 13 Jun 2017 10:21:08 +0000 (13:21 +0300)
The commit 47d3946ea220

usb: dwc3: refactor gadget endpoint count calculation

refactored dwc3_gadget_init_endpoints() and in particular changed in or
out endpoint numbering to be through. It's not always convenient and
makes code a slightly harder to read.

Introduce a new temporary variable to make it easier to understand what
is going on inside the function.

While doing that, rename local variables as follows:
u8 num -> u8 total
int num -> int kbytes

Replace implicit direction check via epnum with explicit use of
direction variable.

While here, replace %d to %u when compounding endpoint name since we are
using unsigned type.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
drivers/usb/dwc3/gadget.c

index 01cd7ddc9981b56d4e53ef889efd5069ca6ad582..7fd4b22f4e977c582cbd36654810fef12892c12e 100644 (file)
@@ -2027,15 +2027,16 @@ static const struct usb_gadget_ops dwc3_gadget_ops = {
 
 /* -------------------------------------------------------------------------- */
 
-static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
+static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
 {
        struct dwc3_ep                  *dep;
        u8                              epnum;
 
        INIT_LIST_HEAD(&dwc->gadget.ep_list);
 
-       for (epnum = 0; epnum < num; epnum++) {
+       for (epnum = 0; epnum < total; epnum++) {
                bool                    direction = epnum & 1;
+               u8                      num = epnum >> 1;
 
                dep = kzalloc(sizeof(*dep), GFP_KERNEL);
                if (!dep)
@@ -2047,7 +2048,7 @@ static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
                dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
                dwc->eps[epnum] = dep;
 
-               snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
+               snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
                                direction ? "in" : "out");
 
                dep->endpoint.name = dep->name;
@@ -2059,39 +2060,39 @@ static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
 
                spin_lock_init(&dep->lock);
 
-               if (epnum == 0 || epnum == 1) {
+               if (num == 0) {
                        usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
                        dep->endpoint.maxburst = 1;
                        dep->endpoint.ops = &dwc3_gadget_ep0_ops;
-                       if (!epnum)
+                       if (!direction)
                                dwc->gadget.ep0 = &dep->endpoint;
                } else if (direction) {
                        int mdwidth;
+                       int kbytes;
                        int size;
                        int ret;
-                       int num;
 
                        mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
                        /* MDWIDTH is represented in bits, we need it in bytes */
                        mdwidth /= 8;
 
-                       size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(epnum >> 1));
+                       size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
                        size = DWC3_GTXFIFOSIZ_TXFDEF(size);
 
                        /* FIFO Depth is in MDWDITH bytes. Multiply */
                        size *= mdwidth;
 
-                       num = size / 1024;
-                       if (num == 0)
-                               num = 1;
+                       kbytes = size / 1024;
+                       if (kbytes == 0)
+                               kbytes = 1;
 
                        /*
-                        * FIFO sizes account an extra MDWIDTH * (num + 1) bytes for
+                        * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
                         * internal overhead. We don't really know how these are used,
                         * but documentation say it exists.
                         */
-                       size -= mdwidth * (num + 1);
-                       size /= num;
+                       size -= mdwidth * (kbytes + 1);
+                       size /= kbytes;
 
                        usb_ep_set_maxpacket_limit(&dep->endpoint, size);
 
@@ -2117,7 +2118,7 @@ static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
                                return ret;
                }
 
-               if (epnum == 0 || epnum == 1) {
+               if (num == 0) {
                        dep->endpoint.caps.type_control = true;
                } else {
                        dep->endpoint.caps.type_iso = true;