usb: dwc3: gadget: skip Set/Clear Halt when invalid
[pandora-kernel.git] / drivers / usb / dwc3 / gadget.c
index 25dbd86..33fe3d3 100644 (file)
@@ -171,6 +171,8 @@ int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
                if (!(reg & DWC3_DEPCMD_CMDACT)) {
                        dev_vdbg(dwc->dev, "Command Complete --> %d\n",
                                        DWC3_DEPCMD_STATUS(reg));
+                       if (DWC3_DEPCMD_STATUS(reg))
+                               return -EINVAL;
                        return 0;
                }
 
@@ -227,24 +229,66 @@ static void dwc3_free_trb_pool(struct dwc3_ep *dep)
        dep->trb_pool_dma = 0;
 }
 
+static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
+
+/**
+ * dwc3_gadget_start_config - Configure EP resources
+ * @dwc: pointer to our controller context structure
+ * @dep: endpoint that is being enabled
+ *
+ * The assignment of transfer resources cannot perfectly follow the
+ * data book due to the fact that the controller driver does not have
+ * all knowledge of the configuration in advance. It is given this
+ * information piecemeal by the composite gadget framework after every
+ * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
+ * programming model in this scenario can cause errors. For two
+ * reasons:
+ *
+ * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
+ * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
+ * multiple interfaces.
+ *
+ * 2) The databook does not mention doing more DEPXFERCFG for new
+ * endpoint on alt setting (8.1.6).
+ *
+ * The following simplified method is used instead:
+ *
+ * All hardware endpoints can be assigned a transfer resource and this
+ * setting will stay persistent until either a core reset or
+ * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
+ * do DEPXFERCFG for every hardware endpoint as well. We are
+ * guaranteed that there are as many transfer resources as endpoints.
+ *
+ * This function is called for each endpoint when it is being enabled
+ * but is triggered only when called for EP0-out, which always happens
+ * first, and which should only happen in one of the above conditions.
+ */
 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
 {
        struct dwc3_gadget_ep_cmd_params params;
        u32                     cmd;
+       int                     i;
+       int                     ret;
+
+       if (dep->number)
+               return 0;
 
        memset(&params, 0x00, sizeof(params));
+       cmd = DWC3_DEPCMD_DEPSTARTCFG;
 
-       if (dep->number != 1) {
-               cmd = DWC3_DEPCMD_DEPSTARTCFG;
-               /* XferRscIdx == 0 for ep0 and 2 for the remaining */
-               if (dep->number > 1) {
-                       if (dwc->start_config_issued)
-                               return 0;
-                       dwc->start_config_issued = true;
-                       cmd |= DWC3_DEPCMD_PARAM(2);
-               }
+       ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
+       if (ret)
+               return ret;
+
+       for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
+               struct dwc3_ep *dep = dwc->eps[i];
+
+               if (!dep)
+                       continue;
 
-               return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
+               ret = dwc3_gadget_set_xfer_resource(dwc, dep);
+               if (ret)
+                       return ret;
        }
 
        return 0;
@@ -338,10 +382,6 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
                struct dwc3_trb_hw      *trb_link_hw;
                struct dwc3_trb         trb_link;
 
-               ret = dwc3_gadget_set_xfer_resource(dwc, dep);
-               if (ret)
-                       return ret;
-
                dep->desc = desc;
                dep->type = usb_endpoint_type(desc);
                dep->flags |= DWC3_EP_ENABLED;
@@ -449,16 +489,16 @@ static int dwc3_gadget_ep_enable(struct usb_ep *ep,
 
        switch (usb_endpoint_type(desc)) {
        case USB_ENDPOINT_XFER_CONTROL:
-               strncat(dep->name, "-control", sizeof(dep->name));
+               strlcat(dep->name, "-control", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_ISOC:
-               strncat(dep->name, "-isoc", sizeof(dep->name));
+               strlcat(dep->name, "-isoc", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_BULK:
-               strncat(dep->name, "-bulk", sizeof(dep->name));
+               strlcat(dep->name, "-bulk", sizeof(dep->name));
                break;
        case USB_ENDPOINT_XFER_INT:
-               strncat(dep->name, "-int", sizeof(dep->name));
+               strlcat(dep->name, "-int", sizeof(dep->name));
                break;
        default:
                dev_err(dwc->dev, "invalid endpoint transfer type\n");
@@ -885,6 +925,9 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
        memset(&params, 0x00, sizeof(params));
 
        if (value) {
+               if (dep->flags & DWC3_EP_STALL)
+                       return 0;
+
                if (dep->number == 0 || dep->number == 1) {
                        /*
                         * Whenever EP0 is stalled, we will restart
@@ -903,7 +946,7 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
                else
                        dep->flags |= DWC3_EP_STALL;
        } else {
-               if (dep->flags & DWC3_EP_WEDGE)
+               if (!(dep->flags & DWC3_EP_STALL))
                        return 0;
 
                ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
@@ -913,7 +956,7 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
                                        value ? "set" : "clear",
                                        dep->name);
                else
-                       dep->flags &= ~DWC3_EP_STALL;
+                       dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
        }
 
        return ret;
@@ -1186,8 +1229,6 @@ static int dwc3_gadget_start(struct usb_gadget *g,
        reg |= DWC3_DCFG_SUPERSPEED;
        dwc3_writel(dwc->regs, DWC3_DCFG, reg);
 
-       dwc->start_config_issued = false;
-
        /* Start with SuperSpeed Default */
        dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
 
@@ -1217,6 +1258,7 @@ err1:
        __dwc3_gadget_ep_disable(dwc->eps[0]);
 
 err0:
+       dwc->gadget_driver = NULL;
        spin_unlock_irqrestore(&dwc->lock, flags);
 
        return ret;
@@ -1277,6 +1319,7 @@ static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
 
                if (epnum == 0 || epnum == 1) {
                        dep->endpoint.maxpacket = 512;
+                       dep->endpoint.maxburst = 1;
                        dep->endpoint.ops = &dwc3_gadget_ep0_ops;
                        if (!epnum)
                                dwc->gadget.ep0 = &dep->endpoint;
@@ -1309,10 +1352,19 @@ static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
 
        for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
                dep = dwc->eps[epnum];
-               dwc3_free_trb_pool(dep);
-
-               if (epnum != 0 && epnum != 1)
+               /*
+                * Physical endpoints 0 and 1 are special; they form the
+                * bi-directional USB endpoint 0.
+                *
+                * For those two physical endpoints, we don't allocate a TRB
+                * pool nor do we add them the endpoints list. Due to that, we
+                * shouldn't do these two operations otherwise we would end up
+                * with all sorts of bugs when removing dwc3.ko.
+                */
+               if (epnum != 0 && epnum != 1) {
+                       dwc3_free_trb_pool(dep);
                        list_del(&dep->endpoint.ep_list);
+               }
 
                kfree(dep);
        }
@@ -1405,7 +1457,7 @@ static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
                struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
 {
-       u32 uf;
+       u32 uf, mask;
 
        if (list_empty(&dep->request_list)) {
                dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
@@ -1413,16 +1465,10 @@ static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
                return;
        }
 
-       if (event->parameters) {
-               u32 mask;
-
-               mask = ~(dep->interval - 1);
-               uf = event->parameters & mask;
-               /* 4 micro frames in the future */
-               uf += dep->interval * 4;
-       } else {
-               uf = 0;
-       }
+       mask = ~(dep->interval - 1);
+       uf = event->parameters & mask;
+       /* 4 micro frames in the future */
+       uf += dep->interval * 4;
 
        __dwc3_gadget_kick_transfer(dep, uf, 1);
 }
@@ -1636,7 +1682,6 @@ static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
 
        dwc3_stop_active_transfers(dwc);
        dwc3_disconnect_gadget(dwc);
-       dwc->start_config_issued = false;
 
        dwc->gadget.speed = USB_SPEED_UNKNOWN;
 }
@@ -1688,7 +1733,6 @@ static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
 
        dwc3_stop_active_transfers(dwc);
        dwc3_clear_stall_all_ep(dwc);
-       dwc->start_config_issued = false;
 
        /* Reset device address to zero */
        reg = dwc3_readl(dwc->regs, DWC3_DCFG);