/* -------------------------------------------------------------------------- */
 
-static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
+static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
+               u8 num, u32 direction)
 {
        struct dwc3_ep                  *dep;
-       u8                              epnum;
+       u8                              i;
 
-       INIT_LIST_HEAD(&dwc->gadget.ep_list);
+       for (i = 0; i < num; i++) {
+               u8 epnum = (i << 1) | (!!direction);
 
-       for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
                dep = kzalloc(sizeof(*dep), GFP_KERNEL);
                if (!dep) {
                        dev_err(dwc->dev, "can't allocate endpoint %d\n",
 
                snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
                                (epnum & 1) ? "in" : "out");
+
                dep->endpoint.name = dep->name;
                dep->direction = (epnum & 1);
 
        return 0;
 }
 
+static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
+{
+       int                             ret;
+
+       INIT_LIST_HEAD(&dwc->gadget.ep_list);
+
+       ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
+       if (ret < 0) {
+               dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
+               return ret;
+       }
+
+       ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
+       if (ret < 0) {
+               dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
+               return ret;
+       }
+
+       return 0;
+}
+
 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
 {
        struct dwc3_ep                  *dep;
 
        for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
                dep = dwc->eps[epnum];
+               if (!dep)
+                       continue;
+
                dwc3_free_trb_pool(dep);
 
                if (epnum != 0 && epnum != 1)
                struct dwc3_ep *dep;
 
                dep = dwc->eps[epnum];
+               if (!dep)
+                       continue;
+
                if (!(dep->flags & DWC3_EP_ENABLED))
                        continue;
 
                int ret;
 
                dep = dwc->eps[epnum];
+               if (!dep)
+                       continue;
 
                if (!(dep->flags & DWC3_EP_STALL))
                        continue;