2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/kernel.h>
40 #include <linux/delay.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/interrupt.h>
47 #include <linux/list.h>
48 #include <linux/dma-mapping.h>
50 #include <linux/usb/ch9.h>
51 #include <linux/usb/gadget.h>
57 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
60 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
61 * @dwc: pointer to our context structure
62 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
64 * Caller should take care of locking. This function will
65 * return 0 on success or -EINVAL if wrong Test Selector
68 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
72 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
73 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
87 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
93 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
94 * @dwc: pointer to our context structure
95 * @state: the state to put link into
97 * Caller should take care of locking. This function will
98 * return 0 on success or -EINVAL.
100 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
105 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
106 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
108 /* set requested state */
109 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
110 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
112 /* wait for a change in DSTS */
114 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
116 /* in HS, means ON */
117 if (DWC3_DSTS_USBLNKST(reg) == state)
123 dev_vdbg(dwc->dev, "link state change request timed out\n");
129 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
130 * @dwc: pointer to our context structure
132 * This function will a best effort FIFO allocation in order
133 * to improve FIFO usage and throughput, while still allowing
134 * us to enable as many endpoints as possible.
136 * Keep in mind that this operation will be highly dependent
137 * on the configured size for RAM1 - which contains TxFifo -,
138 * the amount of endpoints enabled on coreConsultant tool, and
139 * the width of the Master Bus.
141 * In the ideal world, we would always be able to satisfy the
142 * following equation:
144 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
145 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
147 * Unfortunately, due to many variables that's not always the case.
149 int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
151 int last_fifo_depth = 0;
157 if (!dwc->needs_fifo_resize)
160 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
161 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
163 /* MDWIDTH is represented in bits, we need it in bytes */
167 * FIXME For now we will only allocate 1 wMaxPacketSize space
168 * for each enabled endpoint, later patches will come to
169 * improve this algorithm so that we better use the internal
172 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
173 struct dwc3_ep *dep = dwc->eps[num];
174 int fifo_number = dep->number >> 1;
178 if (!(dep->number & 1))
181 if (!(dep->flags & DWC3_EP_ENABLED))
184 if (usb_endpoint_xfer_bulk(dep->desc)
185 || usb_endpoint_xfer_isoc(dep->desc))
189 * REVISIT: the following assumes we will always have enough
190 * space available on the FIFO RAM for all possible use cases.
191 * Make sure that's true somehow and change FIFO allocation
194 * If we have Bulk or Isochronous endpoints, we want
195 * them to be able to be very, very fast. So we're giving
196 * those endpoints a fifo_size which is enough for 3 full
199 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
202 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
204 fifo_size |= (last_fifo_depth << 16);
206 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
207 dep->name, last_fifo_depth, fifo_size & 0xffff);
209 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
212 last_fifo_depth += (fifo_size & 0xffff);
218 void dwc3_map_buffer_to_dma(struct dwc3_request *req)
220 struct dwc3 *dwc = req->dep->dwc;
222 if (req->request.length == 0) {
223 /* req->request.dma = dwc->setup_buf_addr; */
227 if (req->request.num_sgs) {
230 mapped = dma_map_sg(dwc->dev, req->request.sg,
231 req->request.num_sgs,
232 req->direction ? DMA_TO_DEVICE
235 dev_err(dwc->dev, "failed to map SGs\n");
239 req->request.num_mapped_sgs = mapped;
243 if (req->request.dma == DMA_ADDR_INVALID) {
244 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
245 req->request.length, req->direction
246 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
251 void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
253 struct dwc3 *dwc = req->dep->dwc;
255 if (req->request.length == 0) {
256 req->request.dma = DMA_ADDR_INVALID;
260 if (req->request.num_mapped_sgs) {
261 req->request.dma = DMA_ADDR_INVALID;
262 dma_unmap_sg(dwc->dev, req->request.sg,
263 req->request.num_mapped_sgs,
264 req->direction ? DMA_TO_DEVICE
267 req->request.num_mapped_sgs = 0;
272 dma_unmap_single(dwc->dev, req->request.dma,
273 req->request.length, req->direction
274 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
276 req->request.dma = DMA_ADDR_INVALID;
280 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
283 struct dwc3 *dwc = dep->dwc;
286 if (req->request.num_mapped_sgs)
287 dep->busy_slot += req->request.num_mapped_sgs;
292 * Skip LINK TRB. We can't use req->trb and check for
293 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
294 * completed (not the LINK TRB).
296 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
297 usb_endpoint_xfer_isoc(dep->desc))
300 list_del(&req->list);
303 if (req->request.status == -EINPROGRESS)
304 req->request.status = status;
306 dwc3_unmap_buffer_from_dma(req);
308 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
309 req, dep->name, req->request.actual,
310 req->request.length, status);
312 spin_unlock(&dwc->lock);
313 req->request.complete(&req->dep->endpoint, &req->request);
314 spin_lock(&dwc->lock);
317 static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
320 case DWC3_DEPCMD_DEPSTARTCFG:
321 return "Start New Configuration";
322 case DWC3_DEPCMD_ENDTRANSFER:
323 return "End Transfer";
324 case DWC3_DEPCMD_UPDATETRANSFER:
325 return "Update Transfer";
326 case DWC3_DEPCMD_STARTTRANSFER:
327 return "Start Transfer";
328 case DWC3_DEPCMD_CLEARSTALL:
329 return "Clear Stall";
330 case DWC3_DEPCMD_SETSTALL:
332 case DWC3_DEPCMD_GETSEQNUMBER:
333 return "Get Data Sequence Number";
334 case DWC3_DEPCMD_SETTRANSFRESOURCE:
335 return "Set Endpoint Transfer Resource";
336 case DWC3_DEPCMD_SETEPCONFIG:
337 return "Set Endpoint Configuration";
339 return "UNKNOWN command";
343 int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
344 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
346 struct dwc3_ep *dep = dwc->eps[ep];
350 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
352 dwc3_gadget_ep_cmd_string(cmd), params->param0,
353 params->param1, params->param2);
355 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
356 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
357 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
359 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
361 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
362 if (!(reg & DWC3_DEPCMD_CMDACT)) {
363 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
364 DWC3_DEPCMD_STATUS(reg));
369 * We can't sleep here, because it is also called from
380 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
381 struct dwc3_trb *trb)
383 u32 offset = (char *) trb - (char *) dep->trb_pool;
385 return dep->trb_pool_dma + offset;
388 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
390 struct dwc3 *dwc = dep->dwc;
395 if (dep->number == 0 || dep->number == 1)
398 dep->trb_pool = dma_alloc_coherent(dwc->dev,
399 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
400 &dep->trb_pool_dma, GFP_KERNEL);
401 if (!dep->trb_pool) {
402 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
410 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
412 struct dwc3 *dwc = dep->dwc;
414 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
415 dep->trb_pool, dep->trb_pool_dma);
417 dep->trb_pool = NULL;
418 dep->trb_pool_dma = 0;
421 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
423 struct dwc3_gadget_ep_cmd_params params;
426 memset(¶ms, 0x00, sizeof(params));
428 if (dep->number != 1) {
429 cmd = DWC3_DEPCMD_DEPSTARTCFG;
430 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
431 if (dep->number > 1) {
432 if (dwc->start_config_issued)
434 dwc->start_config_issued = true;
435 cmd |= DWC3_DEPCMD_PARAM(2);
438 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms);
444 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
445 const struct usb_endpoint_descriptor *desc,
446 const struct usb_ss_ep_comp_descriptor *comp_desc)
448 struct dwc3_gadget_ep_cmd_params params;
450 memset(¶ms, 0x00, sizeof(params));
452 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
453 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
454 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
456 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
457 | DWC3_DEPCFG_XFER_NOT_READY_EN;
459 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
460 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
461 | DWC3_DEPCFG_STREAM_EVENT_EN;
462 dep->stream_capable = true;
465 if (usb_endpoint_xfer_isoc(desc))
466 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
469 * We are doing 1:1 mapping for endpoints, meaning
470 * Physical Endpoints 2 maps to Logical Endpoint 2 and
471 * so on. We consider the direction bit as part of the physical
472 * endpoint number. So USB endpoint 0x81 is 0x03.
474 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
477 * We must use the lower 16 TX FIFOs even though
481 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
483 if (desc->bInterval) {
484 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
485 dep->interval = 1 << (desc->bInterval - 1);
488 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
489 DWC3_DEPCMD_SETEPCONFIG, ¶ms);
492 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
494 struct dwc3_gadget_ep_cmd_params params;
496 memset(¶ms, 0x00, sizeof(params));
498 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
500 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
501 DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms);
505 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
506 * @dep: endpoint to be initialized
507 * @desc: USB Endpoint Descriptor
509 * Caller should take care of locking
511 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
512 const struct usb_endpoint_descriptor *desc,
513 const struct usb_ss_ep_comp_descriptor *comp_desc)
515 struct dwc3 *dwc = dep->dwc;
519 if (!(dep->flags & DWC3_EP_ENABLED)) {
520 ret = dwc3_gadget_start_config(dwc, dep);
525 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
529 if (!(dep->flags & DWC3_EP_ENABLED)) {
530 struct dwc3_trb *trb_st_hw;
531 struct dwc3_trb *trb_link;
533 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
538 dep->comp_desc = comp_desc;
539 dep->type = usb_endpoint_type(desc);
540 dep->flags |= DWC3_EP_ENABLED;
542 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
543 reg |= DWC3_DALEPENA_EP(dep->number);
544 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
546 if (!usb_endpoint_xfer_isoc(desc))
549 memset(&trb_link, 0, sizeof(trb_link));
551 /* Link TRB for ISOC. The HWO but is never reset */
552 trb_st_hw = &dep->trb_pool[0];
554 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
556 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
557 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
558 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
559 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
565 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
566 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
568 struct dwc3_request *req;
570 if (!list_empty(&dep->req_queued))
571 dwc3_stop_active_transfer(dwc, dep->number);
573 while (!list_empty(&dep->request_list)) {
574 req = next_request(&dep->request_list);
576 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
581 * __dwc3_gadget_ep_disable - Disables a HW endpoint
582 * @dep: the endpoint to disable
584 * This function also removes requests which are currently processed ny the
585 * hardware and those which are not yet scheduled.
586 * Caller should take care of locking.
588 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
590 struct dwc3 *dwc = dep->dwc;
593 dwc3_remove_requests(dwc, dep);
595 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
596 reg &= ~DWC3_DALEPENA_EP(dep->number);
597 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
599 dep->stream_capable = false;
601 dep->comp_desc = NULL;
608 /* -------------------------------------------------------------------------- */
610 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
611 const struct usb_endpoint_descriptor *desc)
616 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
621 /* -------------------------------------------------------------------------- */
623 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
624 const struct usb_endpoint_descriptor *desc)
631 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
632 pr_debug("dwc3: invalid parameters\n");
636 if (!desc->wMaxPacketSize) {
637 pr_debug("dwc3: missing wMaxPacketSize\n");
641 dep = to_dwc3_ep(ep);
644 switch (usb_endpoint_type(desc)) {
645 case USB_ENDPOINT_XFER_CONTROL:
646 strncat(dep->name, "-control", sizeof(dep->name));
648 case USB_ENDPOINT_XFER_ISOC:
649 strncat(dep->name, "-isoc", sizeof(dep->name));
651 case USB_ENDPOINT_XFER_BULK:
652 strncat(dep->name, "-bulk", sizeof(dep->name));
654 case USB_ENDPOINT_XFER_INT:
655 strncat(dep->name, "-int", sizeof(dep->name));
658 dev_err(dwc->dev, "invalid endpoint transfer type\n");
661 if (dep->flags & DWC3_EP_ENABLED) {
662 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
667 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
669 spin_lock_irqsave(&dwc->lock, flags);
670 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
671 spin_unlock_irqrestore(&dwc->lock, flags);
676 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
684 pr_debug("dwc3: invalid parameters\n");
688 dep = to_dwc3_ep(ep);
691 if (!(dep->flags & DWC3_EP_ENABLED)) {
692 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
697 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
699 (dep->number & 1) ? "in" : "out");
701 spin_lock_irqsave(&dwc->lock, flags);
702 ret = __dwc3_gadget_ep_disable(dep);
703 spin_unlock_irqrestore(&dwc->lock, flags);
708 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
711 struct dwc3_request *req;
712 struct dwc3_ep *dep = to_dwc3_ep(ep);
713 struct dwc3 *dwc = dep->dwc;
715 req = kzalloc(sizeof(*req), gfp_flags);
717 dev_err(dwc->dev, "not enough memory\n");
721 req->epnum = dep->number;
723 req->request.dma = DMA_ADDR_INVALID;
725 return &req->request;
728 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
729 struct usb_request *request)
731 struct dwc3_request *req = to_dwc3_request(request);
737 * dwc3_prepare_one_trb - setup one TRB from one request
738 * @dep: endpoint for which this request is prepared
739 * @req: dwc3_request pointer
741 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
742 struct dwc3_request *req, dma_addr_t dma,
743 unsigned length, unsigned last, unsigned chain)
745 struct dwc3 *dwc = dep->dwc;
746 struct dwc3_trb *trb;
748 unsigned int cur_slot;
750 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
751 dep->name, req, (unsigned long long) dma,
752 length, last ? " last" : "",
753 chain ? " chain" : "");
755 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
756 cur_slot = dep->free_slot;
759 /* Skip the LINK-TRB on ISOC */
760 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
761 usb_endpoint_xfer_isoc(dep->desc))
765 dwc3_gadget_move_request_queued(req);
767 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
770 trb->size = DWC3_TRB_SIZE_LENGTH(length);
771 trb->bpl = lower_32_bits(dma);
772 trb->bph = upper_32_bits(dma);
774 switch (usb_endpoint_type(dep->desc)) {
775 case USB_ENDPOINT_XFER_CONTROL:
776 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
779 case USB_ENDPOINT_XFER_ISOC:
780 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
782 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
783 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
784 trb->ctrl |= DWC3_TRB_CTRL_IOC;
787 case USB_ENDPOINT_XFER_BULK:
788 case USB_ENDPOINT_XFER_INT:
789 trb->ctrl = DWC3_TRBCTL_NORMAL;
793 * This is only possible with faulty memory because we
794 * checked it already :)
799 if (usb_endpoint_xfer_isoc(dep->desc)) {
800 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
801 trb->ctrl |= DWC3_TRB_CTRL_CSP;
804 trb->ctrl |= DWC3_TRB_CTRL_CHN;
807 trb->ctrl |= DWC3_TRB_CTRL_LST;
810 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
811 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
813 trb->ctrl |= DWC3_TRB_CTRL_HWO;
817 * dwc3_prepare_trbs - setup TRBs from requests
818 * @dep: endpoint for which requests are being prepared
819 * @starting: true if the endpoint is idle and no requests are queued.
821 * The functions goes through the requests list and setups TRBs for the
822 * transfers. The functions returns once there are not more TRBs available or
823 * it run out of requests.
825 static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
827 struct dwc3_request *req, *n;
829 unsigned int last_one = 0;
831 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
833 /* the first request must not be queued */
834 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
837 * if busy & slot are equal than it is either full or empty. If we are
838 * starting to proceed requests then we are empty. Otherwise we ar
839 * full and don't do anything
844 trbs_left = DWC3_TRB_NUM;
846 * In case we start from scratch, we queue the ISOC requests
847 * starting from slot 1. This is done because we use ring
848 * buffer and have no LST bit to stop us. Instead, we place
849 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
850 * after the first request so we start at slot 1 and have
851 * 7 requests proceed before we hit the first IOC.
852 * Other transfer types don't use the ring buffer and are
853 * processed from the first TRB until the last one. Since we
854 * don't wrap around we have to start at the beginning.
856 if (usb_endpoint_xfer_isoc(dep->desc)) {
865 /* The last TRB is a link TRB, not used for xfer */
866 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
869 list_for_each_entry_safe(req, n, &dep->request_list, list) {
873 if (req->request.num_mapped_sgs > 0) {
874 struct usb_request *request = &req->request;
875 struct scatterlist *sg = request->sg;
876 struct scatterlist *s;
879 for_each_sg(sg, s, request->num_mapped_sgs, i) {
880 unsigned chain = true;
882 length = sg_dma_len(s);
883 dma = sg_dma_address(s);
885 if (i == (request->num_mapped_sgs - 1)
898 dwc3_prepare_one_trb(dep, req, dma, length,
905 dma = req->request.dma;
906 length = req->request.length;
912 /* Is this the last request? */
913 if (list_is_last(&req->list, &dep->request_list))
916 dwc3_prepare_one_trb(dep, req, dma, length,
925 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
928 struct dwc3_gadget_ep_cmd_params params;
929 struct dwc3_request *req;
930 struct dwc3 *dwc = dep->dwc;
934 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
935 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
938 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
941 * If we are getting here after a short-out-packet we don't enqueue any
942 * new requests as we try to set the IOC bit only on the last request.
945 if (list_empty(&dep->req_queued))
946 dwc3_prepare_trbs(dep, start_new);
948 /* req points to the first request which will be sent */
949 req = next_request(&dep->req_queued);
951 dwc3_prepare_trbs(dep, start_new);
954 * req points to the first request where HWO changed
957 req = next_request(&dep->req_queued);
960 dep->flags |= DWC3_EP_PENDING_REQUEST;
964 memset(¶ms, 0, sizeof(params));
965 params.param0 = upper_32_bits(req->trb_dma);
966 params.param1 = lower_32_bits(req->trb_dma);
969 cmd = DWC3_DEPCMD_STARTTRANSFER;
971 cmd = DWC3_DEPCMD_UPDATETRANSFER;
973 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
974 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
976 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
979 * FIXME we need to iterate over the list of requests
980 * here and stop, unmap, free and del each of the linked
981 * requests instead of we do now.
983 dwc3_unmap_buffer_from_dma(req);
984 list_del(&req->list);
988 dep->flags |= DWC3_EP_BUSY;
989 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
992 WARN_ON_ONCE(!dep->res_trans_idx);
997 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
999 req->request.actual = 0;
1000 req->request.status = -EINPROGRESS;
1001 req->direction = dep->direction;
1002 req->epnum = dep->number;
1005 * We only add to our list of requests now and
1006 * start consuming the list once we get XferNotReady
1009 * That way, we avoid doing anything that we don't need
1010 * to do now and defer it until the point we receive a
1011 * particular token from the Host side.
1013 * This will also avoid Host cancelling URBs due to too
1016 dwc3_map_buffer_to_dma(req);
1017 list_add_tail(&req->list, &dep->request_list);
1020 * There is one special case: XferNotReady with
1021 * empty list of requests. We need to kick the
1022 * transfer here in that situation, otherwise
1023 * we will be NAKing forever.
1025 * If we get XferNotReady before gadget driver
1026 * has a chance to queue a request, we will ACK
1027 * the IRQ but won't be able to receive the data
1028 * until the next request is queued. The following
1029 * code is handling exactly that.
1031 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1036 if (usb_endpoint_xfer_isoc(dep->desc) &&
1037 dep->flags & DWC3_EP_BUSY)
1040 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
1041 if (ret && ret != -EBUSY) {
1042 struct dwc3 *dwc = dep->dwc;
1044 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1052 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1055 struct dwc3_request *req = to_dwc3_request(request);
1056 struct dwc3_ep *dep = to_dwc3_ep(ep);
1057 struct dwc3 *dwc = dep->dwc;
1059 unsigned long flags;
1064 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1069 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1070 request, ep->name, request->length);
1072 spin_lock_irqsave(&dwc->lock, flags);
1073 ret = __dwc3_gadget_ep_queue(dep, req);
1074 spin_unlock_irqrestore(&dwc->lock, flags);
1079 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1080 struct usb_request *request)
1082 struct dwc3_request *req = to_dwc3_request(request);
1083 struct dwc3_request *r = NULL;
1085 struct dwc3_ep *dep = to_dwc3_ep(ep);
1086 struct dwc3 *dwc = dep->dwc;
1088 unsigned long flags;
1091 spin_lock_irqsave(&dwc->lock, flags);
1093 list_for_each_entry(r, &dep->request_list, list) {
1099 list_for_each_entry(r, &dep->req_queued, list) {
1104 /* wait until it is processed */
1105 dwc3_stop_active_transfer(dwc, dep->number);
1108 dev_err(dwc->dev, "request %p was not queued to %s\n",
1114 /* giveback the request */
1115 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1118 spin_unlock_irqrestore(&dwc->lock, flags);
1123 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1125 struct dwc3_gadget_ep_cmd_params params;
1126 struct dwc3 *dwc = dep->dwc;
1129 memset(¶ms, 0x00, sizeof(params));
1132 if (dep->number == 0 || dep->number == 1) {
1134 * Whenever EP0 is stalled, we will restart
1135 * the state machine, thus moving back to
1138 dwc->ep0state = EP0_SETUP_PHASE;
1141 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1142 DWC3_DEPCMD_SETSTALL, ¶ms);
1144 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1145 value ? "set" : "clear",
1148 dep->flags |= DWC3_EP_STALL;
1150 if (dep->flags & DWC3_EP_WEDGE)
1153 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1154 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1156 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1157 value ? "set" : "clear",
1160 dep->flags &= ~DWC3_EP_STALL;
1166 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1168 struct dwc3_ep *dep = to_dwc3_ep(ep);
1169 struct dwc3 *dwc = dep->dwc;
1171 unsigned long flags;
1175 spin_lock_irqsave(&dwc->lock, flags);
1177 if (usb_endpoint_xfer_isoc(dep->desc)) {
1178 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1183 ret = __dwc3_gadget_ep_set_halt(dep, value);
1185 spin_unlock_irqrestore(&dwc->lock, flags);
1190 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1192 struct dwc3_ep *dep = to_dwc3_ep(ep);
1194 dep->flags |= DWC3_EP_WEDGE;
1196 return dwc3_gadget_ep_set_halt(ep, 1);
1199 /* -------------------------------------------------------------------------- */
1201 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1202 .bLength = USB_DT_ENDPOINT_SIZE,
1203 .bDescriptorType = USB_DT_ENDPOINT,
1204 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1207 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1208 .enable = dwc3_gadget_ep0_enable,
1209 .disable = dwc3_gadget_ep0_disable,
1210 .alloc_request = dwc3_gadget_ep_alloc_request,
1211 .free_request = dwc3_gadget_ep_free_request,
1212 .queue = dwc3_gadget_ep0_queue,
1213 .dequeue = dwc3_gadget_ep_dequeue,
1214 .set_halt = dwc3_gadget_ep_set_halt,
1215 .set_wedge = dwc3_gadget_ep_set_wedge,
1218 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1219 .enable = dwc3_gadget_ep_enable,
1220 .disable = dwc3_gadget_ep_disable,
1221 .alloc_request = dwc3_gadget_ep_alloc_request,
1222 .free_request = dwc3_gadget_ep_free_request,
1223 .queue = dwc3_gadget_ep_queue,
1224 .dequeue = dwc3_gadget_ep_dequeue,
1225 .set_halt = dwc3_gadget_ep_set_halt,
1226 .set_wedge = dwc3_gadget_ep_set_wedge,
1229 /* -------------------------------------------------------------------------- */
1231 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1233 struct dwc3 *dwc = gadget_to_dwc(g);
1236 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1237 return DWC3_DSTS_SOFFN(reg);
1240 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1242 struct dwc3 *dwc = gadget_to_dwc(g);
1244 unsigned long timeout;
1245 unsigned long flags;
1254 spin_lock_irqsave(&dwc->lock, flags);
1257 * According to the Databook Remote wakeup request should
1258 * be issued only when the device is in early suspend state.
1260 * We can check that via USB Link State bits in DSTS register.
1262 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1264 speed = reg & DWC3_DSTS_CONNECTSPD;
1265 if (speed == DWC3_DSTS_SUPERSPEED) {
1266 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1271 link_state = DWC3_DSTS_USBLNKST(reg);
1273 switch (link_state) {
1274 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1275 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1278 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1284 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1286 dev_err(dwc->dev, "failed to put link in Recovery\n");
1290 /* write zeroes to Link Change Request */
1291 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1292 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1294 /* pool until Link State change to ON */
1295 timeout = jiffies + msecs_to_jiffies(100);
1297 while (!(time_after(jiffies, timeout))) {
1298 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1300 /* in HS, means ON */
1301 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1305 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1306 dev_err(dwc->dev, "failed to send remote wakeup\n");
1311 spin_unlock_irqrestore(&dwc->lock, flags);
1316 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1319 struct dwc3 *dwc = gadget_to_dwc(g);
1321 dwc->is_selfpowered = !!is_selfpowered;
1326 static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1331 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1333 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1334 reg |= (DWC3_DCTL_RUN_STOP
1335 | DWC3_DCTL_TRGTULST_RX_DET);
1337 reg &= ~DWC3_DCTL_RUN_STOP;
1340 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1343 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1345 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1348 if (reg & DWC3_DSTS_DEVCTRLHLT)
1357 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1359 ? dwc->gadget_driver->function : "no-function",
1360 is_on ? "connect" : "disconnect");
1363 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1365 struct dwc3 *dwc = gadget_to_dwc(g);
1366 unsigned long flags;
1370 spin_lock_irqsave(&dwc->lock, flags);
1371 dwc3_gadget_run_stop(dwc, is_on);
1372 spin_unlock_irqrestore(&dwc->lock, flags);
1377 static int dwc3_gadget_start(struct usb_gadget *g,
1378 struct usb_gadget_driver *driver)
1380 struct dwc3 *dwc = gadget_to_dwc(g);
1381 struct dwc3_ep *dep;
1382 unsigned long flags;
1386 spin_lock_irqsave(&dwc->lock, flags);
1388 if (dwc->gadget_driver) {
1389 dev_err(dwc->dev, "%s is already bound to %s\n",
1391 dwc->gadget_driver->driver.name);
1396 dwc->gadget_driver = driver;
1397 dwc->gadget.dev.driver = &driver->driver;
1399 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1400 reg &= ~(DWC3_DCFG_SPEED_MASK);
1401 reg |= dwc->maximum_speed;
1402 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1404 dwc->start_config_issued = false;
1406 /* Start with SuperSpeed Default */
1407 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1410 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
1412 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1417 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
1419 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1423 /* begin to receive SETUP packets */
1424 dwc->ep0state = EP0_SETUP_PHASE;
1425 dwc3_ep0_out_start(dwc);
1427 spin_unlock_irqrestore(&dwc->lock, flags);
1432 __dwc3_gadget_ep_disable(dwc->eps[0]);
1435 spin_unlock_irqrestore(&dwc->lock, flags);
1440 static int dwc3_gadget_stop(struct usb_gadget *g,
1441 struct usb_gadget_driver *driver)
1443 struct dwc3 *dwc = gadget_to_dwc(g);
1444 unsigned long flags;
1446 spin_lock_irqsave(&dwc->lock, flags);
1448 __dwc3_gadget_ep_disable(dwc->eps[0]);
1449 __dwc3_gadget_ep_disable(dwc->eps[1]);
1451 dwc->gadget_driver = NULL;
1452 dwc->gadget.dev.driver = NULL;
1454 spin_unlock_irqrestore(&dwc->lock, flags);
1458 static const struct usb_gadget_ops dwc3_gadget_ops = {
1459 .get_frame = dwc3_gadget_get_frame,
1460 .wakeup = dwc3_gadget_wakeup,
1461 .set_selfpowered = dwc3_gadget_set_selfpowered,
1462 .pullup = dwc3_gadget_pullup,
1463 .udc_start = dwc3_gadget_start,
1464 .udc_stop = dwc3_gadget_stop,
1467 /* -------------------------------------------------------------------------- */
1469 static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1471 struct dwc3_ep *dep;
1474 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1476 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1477 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1479 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1485 dep->number = epnum;
1486 dwc->eps[epnum] = dep;
1488 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1489 (epnum & 1) ? "in" : "out");
1490 dep->endpoint.name = dep->name;
1491 dep->direction = (epnum & 1);
1493 if (epnum == 0 || epnum == 1) {
1494 dep->endpoint.maxpacket = 512;
1495 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1497 dwc->gadget.ep0 = &dep->endpoint;
1501 dep->endpoint.maxpacket = 1024;
1502 dep->endpoint.max_streams = 15;
1503 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1504 list_add_tail(&dep->endpoint.ep_list,
1505 &dwc->gadget.ep_list);
1507 ret = dwc3_alloc_trb_pool(dep);
1512 INIT_LIST_HEAD(&dep->request_list);
1513 INIT_LIST_HEAD(&dep->req_queued);
1519 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1521 struct dwc3_ep *dep;
1524 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1525 dep = dwc->eps[epnum];
1526 dwc3_free_trb_pool(dep);
1528 if (epnum != 0 && epnum != 1)
1529 list_del(&dep->endpoint.ep_list);
1535 static void dwc3_gadget_release(struct device *dev)
1537 dev_dbg(dev, "%s\n", __func__);
1540 /* -------------------------------------------------------------------------- */
1541 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1542 const struct dwc3_event_depevt *event, int status)
1544 struct dwc3_request *req;
1545 struct dwc3_trb *trb;
1547 unsigned int s_pkt = 0;
1550 req = next_request(&dep->req_queued);
1558 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1560 * We continue despite the error. There is not much we
1561 * can do. If we don't clean in up we loop for ever. If
1562 * we skip the TRB than it gets overwritten reused after
1563 * a while since we use them in a ring buffer. a BUG()
1564 * would help. Lets hope that if this occures, someone
1565 * fixes the root cause instead of looking away :)
1567 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1568 dep->name, req->trb);
1569 count = trb->size & DWC3_TRB_SIZE_MASK;
1571 if (dep->direction) {
1573 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1575 status = -ECONNRESET;
1578 if (count && (event->status & DEPEVT_STATUS_SHORT))
1583 * We assume here we will always receive the entire data block
1584 * which we should receive. Meaning, if we program RX to
1585 * receive 4K but we receive only 2K, we assume that's all we
1586 * should receive and we simply bounce the request back to the
1587 * gadget driver for further processing.
1589 req->request.actual += req->request.length - count;
1590 dwc3_gadget_giveback(dep, req, status);
1593 if ((event->status & DEPEVT_STATUS_LST) &&
1594 (trb->ctrl & DWC3_TRB_CTRL_LST))
1596 if ((event->status & DEPEVT_STATUS_IOC) &&
1597 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1601 if ((event->status & DEPEVT_STATUS_IOC) &&
1602 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1607 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1608 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1611 unsigned status = 0;
1614 if (event->status & DEPEVT_STATUS_BUSERR)
1615 status = -ECONNRESET;
1617 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1619 dep->flags &= ~DWC3_EP_BUSY;
1620 dep->res_trans_idx = 0;
1624 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1625 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1627 if (dwc->revision < DWC3_REVISION_183A) {
1631 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1632 struct dwc3_ep *dep = dwc->eps[i];
1634 if (!(dep->flags & DWC3_EP_ENABLED))
1637 if (!list_empty(&dep->req_queued))
1641 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1643 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1649 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1650 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1654 if (list_empty(&dep->request_list)) {
1655 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1660 if (event->parameters) {
1663 mask = ~(dep->interval - 1);
1664 uf = event->parameters & mask;
1665 /* 4 micro frames in the future */
1666 uf += dep->interval * 4;
1671 __dwc3_gadget_kick_transfer(dep, uf, 1);
1674 static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1675 const struct dwc3_event_depevt *event)
1677 struct dwc3 *dwc = dep->dwc;
1678 struct dwc3_event_depevt mod_ev = *event;
1681 * We were asked to remove one requests. It is possible that this
1682 * request and a few other were started together and have the same
1683 * transfer index. Since we stopped the complete endpoint we don't
1684 * know how many requests were already completed (and not yet)
1685 * reported and how could be done (later). We purge them all until
1686 * the end of the list.
1688 mod_ev.status = DEPEVT_STATUS_LST;
1689 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1690 dep->flags &= ~DWC3_EP_BUSY;
1691 /* pending requets are ignored and are queued on XferNotReady */
1694 static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1695 const struct dwc3_event_depevt *event)
1697 u32 param = event->parameters;
1698 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1701 case DWC3_DEPCMD_ENDTRANSFER:
1702 dwc3_process_ep_cmd_complete(dep, event);
1704 case DWC3_DEPCMD_STARTTRANSFER:
1705 dep->res_trans_idx = param & 0x7f;
1708 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1709 __func__, cmd_type);
1714 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1715 const struct dwc3_event_depevt *event)
1717 struct dwc3_ep *dep;
1718 u8 epnum = event->endpoint_number;
1720 dep = dwc->eps[epnum];
1722 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1723 dwc3_ep_event_string(event->endpoint_event));
1725 if (epnum == 0 || epnum == 1) {
1726 dwc3_ep0_interrupt(dwc, event);
1730 switch (event->endpoint_event) {
1731 case DWC3_DEPEVT_XFERCOMPLETE:
1732 if (usb_endpoint_xfer_isoc(dep->desc)) {
1733 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1738 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1740 case DWC3_DEPEVT_XFERINPROGRESS:
1741 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1742 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1747 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1749 case DWC3_DEPEVT_XFERNOTREADY:
1750 if (usb_endpoint_xfer_isoc(dep->desc)) {
1751 dwc3_gadget_start_isoc(dwc, dep, event);
1755 dev_vdbg(dwc->dev, "%s: reason %s\n",
1756 dep->name, event->status &
1757 DEPEVT_STATUS_TRANSFER_ACTIVE
1759 : "Transfer Not Active");
1761 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1762 if (!ret || ret == -EBUSY)
1765 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1770 case DWC3_DEPEVT_STREAMEVT:
1771 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1772 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1777 switch (event->status) {
1778 case DEPEVT_STREAMEVT_FOUND:
1779 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1783 case DEPEVT_STREAMEVT_NOTFOUND:
1786 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1789 case DWC3_DEPEVT_RXTXFIFOEVT:
1790 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1792 case DWC3_DEPEVT_EPCMDCMPLT:
1793 dwc3_ep_cmd_compl(dep, event);
1798 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1800 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1801 spin_unlock(&dwc->lock);
1802 dwc->gadget_driver->disconnect(&dwc->gadget);
1803 spin_lock(&dwc->lock);
1807 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1809 struct dwc3_ep *dep;
1810 struct dwc3_gadget_ep_cmd_params params;
1814 dep = dwc->eps[epnum];
1816 WARN_ON(!dep->res_trans_idx);
1817 if (dep->res_trans_idx) {
1818 cmd = DWC3_DEPCMD_ENDTRANSFER;
1819 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1820 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1821 memset(¶ms, 0, sizeof(params));
1822 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1824 dep->res_trans_idx = 0;
1828 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1832 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1833 struct dwc3_ep *dep;
1835 dep = dwc->eps[epnum];
1836 if (!(dep->flags & DWC3_EP_ENABLED))
1839 dwc3_remove_requests(dwc, dep);
1843 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1847 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1848 struct dwc3_ep *dep;
1849 struct dwc3_gadget_ep_cmd_params params;
1852 dep = dwc->eps[epnum];
1854 if (!(dep->flags & DWC3_EP_STALL))
1857 dep->flags &= ~DWC3_EP_STALL;
1859 memset(¶ms, 0, sizeof(params));
1860 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1861 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1866 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1868 dev_vdbg(dwc->dev, "%s\n", __func__);
1871 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1872 enable it before we can disable it.
1874 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1875 reg &= ~DWC3_DCTL_INITU1ENA;
1876 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1878 reg &= ~DWC3_DCTL_INITU2ENA;
1879 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1882 dwc3_stop_active_transfers(dwc);
1883 dwc3_disconnect_gadget(dwc);
1884 dwc->start_config_issued = false;
1886 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1887 dwc->setup_packet_pending = false;
1890 static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1894 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1897 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1899 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1901 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1904 static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1908 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1911 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1913 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1915 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1918 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1922 dev_vdbg(dwc->dev, "%s\n", __func__);
1925 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1926 * would cause a missing Disconnect Event if there's a
1927 * pending Setup Packet in the FIFO.
1929 * There's no suggested workaround on the official Bug
1930 * report, which states that "unless the driver/application
1931 * is doing any special handling of a disconnect event,
1932 * there is no functional issue".
1934 * Unfortunately, it turns out that we _do_ some special
1935 * handling of a disconnect event, namely complete all
1936 * pending transfers, notify gadget driver of the
1937 * disconnection, and so on.
1939 * Our suggested workaround is to follow the Disconnect
1940 * Event steps here, instead, based on a setup_packet_pending
1941 * flag. Such flag gets set whenever we have a XferNotReady
1942 * event on EP0 and gets cleared on XferComplete for the
1947 * STAR#9000466709: RTL: Device : Disconnect event not
1948 * generated if setup packet pending in FIFO
1950 if (dwc->revision < DWC3_REVISION_188A) {
1951 if (dwc->setup_packet_pending)
1952 dwc3_gadget_disconnect_interrupt(dwc);
1955 /* after reset -> Default State */
1956 dwc->dev_state = DWC3_DEFAULT_STATE;
1959 dwc3_gadget_usb2_phy_power(dwc, true);
1960 dwc3_gadget_usb3_phy_power(dwc, true);
1962 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1963 dwc3_disconnect_gadget(dwc);
1965 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1966 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1967 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1968 dwc->test_mode = false;
1970 dwc3_stop_active_transfers(dwc);
1971 dwc3_clear_stall_all_ep(dwc);
1972 dwc->start_config_issued = false;
1974 /* Reset device address to zero */
1975 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1976 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1977 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1980 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1983 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1986 * We change the clock only at SS but I dunno why I would want to do
1987 * this. Maybe it becomes part of the power saving plan.
1990 if (speed != DWC3_DSTS_SUPERSPEED)
1994 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1995 * each time on Connect Done.
2000 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2001 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2002 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2005 static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
2008 case USB_SPEED_SUPER:
2009 dwc3_gadget_usb2_phy_power(dwc, false);
2011 case USB_SPEED_HIGH:
2012 case USB_SPEED_FULL:
2014 dwc3_gadget_usb3_phy_power(dwc, false);
2019 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2021 struct dwc3_gadget_ep_cmd_params params;
2022 struct dwc3_ep *dep;
2027 dev_vdbg(dwc->dev, "%s\n", __func__);
2029 memset(¶ms, 0x00, sizeof(params));
2031 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2032 speed = reg & DWC3_DSTS_CONNECTSPD;
2035 dwc3_update_ram_clk_sel(dwc, speed);
2038 case DWC3_DCFG_SUPERSPEED:
2040 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2041 * would cause a missing USB3 Reset event.
2043 * In such situations, we should force a USB3 Reset
2044 * event by calling our dwc3_gadget_reset_interrupt()
2049 * STAR#9000483510: RTL: SS : USB3 reset event may
2050 * not be generated always when the link enters poll
2052 if (dwc->revision < DWC3_REVISION_190A)
2053 dwc3_gadget_reset_interrupt(dwc);
2055 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2056 dwc->gadget.ep0->maxpacket = 512;
2057 dwc->gadget.speed = USB_SPEED_SUPER;
2059 case DWC3_DCFG_HIGHSPEED:
2060 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2061 dwc->gadget.ep0->maxpacket = 64;
2062 dwc->gadget.speed = USB_SPEED_HIGH;
2064 case DWC3_DCFG_FULLSPEED2:
2065 case DWC3_DCFG_FULLSPEED1:
2066 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2067 dwc->gadget.ep0->maxpacket = 64;
2068 dwc->gadget.speed = USB_SPEED_FULL;
2070 case DWC3_DCFG_LOWSPEED:
2071 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2072 dwc->gadget.ep0->maxpacket = 8;
2073 dwc->gadget.speed = USB_SPEED_LOW;
2077 /* Disable unneded PHY */
2078 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2081 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
2083 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2088 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
2090 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2095 * Configure PHY via GUSB3PIPECTLn if required.
2097 * Update GTXFIFOSIZn
2099 * In both cases reset values should be sufficient.
2103 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2105 dev_vdbg(dwc->dev, "%s\n", __func__);
2108 * TODO take core out of low power mode when that's
2112 dwc->gadget_driver->resume(&dwc->gadget);
2115 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2116 unsigned int evtinfo)
2118 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2121 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2122 * on the link partner, the USB session might do multiple entry/exit
2123 * of low power states before a transfer takes place.
2125 * Due to this problem, we might experience lower throughput. The
2126 * suggested workaround is to disable DCTL[12:9] bits if we're
2127 * transitioning from U1/U2 to U0 and enable those bits again
2128 * after a transfer completes and there are no pending transfers
2129 * on any of the enabled endpoints.
2131 * This is the first half of that workaround.
2135 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2136 * core send LGO_Ux entering U0
2138 if (dwc->revision < DWC3_REVISION_183A) {
2139 if (next == DWC3_LINK_STATE_U0) {
2143 switch (dwc->link_state) {
2144 case DWC3_LINK_STATE_U1:
2145 case DWC3_LINK_STATE_U2:
2146 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2147 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2148 | DWC3_DCTL_ACCEPTU2ENA
2149 | DWC3_DCTL_INITU1ENA
2150 | DWC3_DCTL_ACCEPTU1ENA);
2153 dwc->u1u2 = reg & u1u2;
2157 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2166 dwc->link_state = next;
2168 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
2171 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2172 const struct dwc3_event_devt *event)
2174 switch (event->type) {
2175 case DWC3_DEVICE_EVENT_DISCONNECT:
2176 dwc3_gadget_disconnect_interrupt(dwc);
2178 case DWC3_DEVICE_EVENT_RESET:
2179 dwc3_gadget_reset_interrupt(dwc);
2181 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2182 dwc3_gadget_conndone_interrupt(dwc);
2184 case DWC3_DEVICE_EVENT_WAKEUP:
2185 dwc3_gadget_wakeup_interrupt(dwc);
2187 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2188 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2190 case DWC3_DEVICE_EVENT_EOPF:
2191 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2193 case DWC3_DEVICE_EVENT_SOF:
2194 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2196 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2197 dev_vdbg(dwc->dev, "Erratic Error\n");
2199 case DWC3_DEVICE_EVENT_CMD_CMPL:
2200 dev_vdbg(dwc->dev, "Command Complete\n");
2202 case DWC3_DEVICE_EVENT_OVERFLOW:
2203 dev_vdbg(dwc->dev, "Overflow\n");
2206 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2210 static void dwc3_process_event_entry(struct dwc3 *dwc,
2211 const union dwc3_event *event)
2213 /* Endpoint IRQ, handle it and return early */
2214 if (event->type.is_devspec == 0) {
2216 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2219 switch (event->type.type) {
2220 case DWC3_EVENT_TYPE_DEV:
2221 dwc3_gadget_interrupt(dwc, &event->devt);
2223 /* REVISIT what to do with Carkit and I2C events ? */
2225 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2229 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2231 struct dwc3_event_buffer *evt;
2235 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2236 count &= DWC3_GEVNTCOUNT_MASK;
2240 evt = dwc->ev_buffs[buf];
2244 union dwc3_event event;
2246 event.raw = *(u32 *) (evt->buf + evt->lpos);
2248 dwc3_process_event_entry(dwc, &event);
2250 * XXX we wrap around correctly to the next entry as almost all
2251 * entries are 4 bytes in size. There is one entry which has 12
2252 * bytes which is a regular entry followed by 8 bytes data. ATM
2253 * I don't know how things are organized if were get next to the
2254 * a boundary so I worry about that once we try to handle that.
2256 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2259 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2265 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2267 struct dwc3 *dwc = _dwc;
2269 irqreturn_t ret = IRQ_NONE;
2271 spin_lock(&dwc->lock);
2273 for (i = 0; i < dwc->num_event_buffers; i++) {
2276 status = dwc3_process_event_buf(dwc, i);
2277 if (status == IRQ_HANDLED)
2281 spin_unlock(&dwc->lock);
2287 * dwc3_gadget_init - Initializes gadget related registers
2288 * @dwc: Pointer to out controller context structure
2290 * Returns 0 on success otherwise negative errno.
2292 int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2298 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2299 &dwc->ctrl_req_addr, GFP_KERNEL);
2300 if (!dwc->ctrl_req) {
2301 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2306 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2307 &dwc->ep0_trb_addr, GFP_KERNEL);
2308 if (!dwc->ep0_trb) {
2309 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2314 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
2315 sizeof(*dwc->setup_buf) * 2,
2316 &dwc->setup_buf_addr, GFP_KERNEL);
2317 if (!dwc->setup_buf) {
2318 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2323 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2324 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2325 if (!dwc->ep0_bounce) {
2326 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2331 dev_set_name(&dwc->gadget.dev, "gadget");
2333 dwc->gadget.ops = &dwc3_gadget_ops;
2334 dwc->gadget.max_speed = USB_SPEED_SUPER;
2335 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2336 dwc->gadget.dev.parent = dwc->dev;
2337 dwc->gadget.sg_supported = true;
2339 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2341 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2342 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2343 dwc->gadget.dev.release = dwc3_gadget_release;
2344 dwc->gadget.name = "dwc3-gadget";
2347 * REVISIT: Here we should clear all pending IRQs to be
2348 * sure we're starting from a well known location.
2351 ret = dwc3_gadget_init_endpoints(dwc);
2355 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2357 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2360 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2365 /* Enable all but Start and End of Frame IRQs */
2366 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2367 DWC3_DEVTEN_EVNTOVERFLOWEN |
2368 DWC3_DEVTEN_CMDCMPLTEN |
2369 DWC3_DEVTEN_ERRTICERREN |
2370 DWC3_DEVTEN_WKUPEVTEN |
2371 DWC3_DEVTEN_ULSTCNGEN |
2372 DWC3_DEVTEN_CONNECTDONEEN |
2373 DWC3_DEVTEN_USBRSTEN |
2374 DWC3_DEVTEN_DISCONNEVTEN);
2375 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2377 ret = device_register(&dwc->gadget.dev);
2379 dev_err(dwc->dev, "failed to register gadget device\n");
2380 put_device(&dwc->gadget.dev);
2384 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2386 dev_err(dwc->dev, "failed to register udc\n");
2393 device_unregister(&dwc->gadget.dev);
2396 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2400 dwc3_gadget_free_endpoints(dwc);
2403 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2404 dwc->ep0_bounce_addr);
2407 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2408 dwc->setup_buf, dwc->setup_buf_addr);
2411 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2412 dwc->ep0_trb, dwc->ep0_trb_addr);
2415 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2416 dwc->ctrl_req, dwc->ctrl_req_addr);
2422 void dwc3_gadget_exit(struct dwc3 *dwc)
2426 usb_del_gadget_udc(&dwc->gadget);
2427 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2429 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2432 dwc3_gadget_free_endpoints(dwc);
2434 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2435 dwc->ep0_bounce_addr);
2437 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2438 dwc->setup_buf, dwc->setup_buf_addr);
2440 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2441 dwc->ep0_trb, dwc->ep0_trb_addr);
2443 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2444 dwc->ctrl_req, dwc->ctrl_req_addr);
2446 device_unregister(&dwc->gadget.dev);