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)
59 void dwc3_map_buffer_to_dma(struct dwc3_request
*req
)
61 struct dwc3
*dwc
= req
->dep
->dwc
;
63 if (req
->request
.length
== 0) {
64 /* req->request.dma = dwc->setup_buf_addr; */
68 if (req
->request
.num_sgs
) {
71 mapped
= dma_map_sg(dwc
->dev
, req
->request
.sg
,
73 req
->direction
? DMA_TO_DEVICE
76 dev_err(dwc
->dev
, "failed to map SGs\n");
80 req
->request
.num_mapped_sgs
= mapped
;
84 if (req
->request
.dma
== DMA_ADDR_INVALID
) {
85 req
->request
.dma
= dma_map_single(dwc
->dev
, req
->request
.buf
,
86 req
->request
.length
, req
->direction
87 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
92 void dwc3_unmap_buffer_from_dma(struct dwc3_request
*req
)
94 struct dwc3
*dwc
= req
->dep
->dwc
;
96 if (req
->request
.length
== 0) {
97 req
->request
.dma
= DMA_ADDR_INVALID
;
101 if (req
->request
.num_mapped_sgs
) {
102 req
->request
.dma
= DMA_ADDR_INVALID
;
103 dma_unmap_sg(dwc
->dev
, req
->request
.sg
,
104 req
->request
.num_sgs
,
105 req
->direction
? DMA_TO_DEVICE
108 req
->request
.num_mapped_sgs
= 0;
113 dma_unmap_single(dwc
->dev
, req
->request
.dma
,
114 req
->request
.length
, req
->direction
115 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
117 req
->request
.dma
= DMA_ADDR_INVALID
;
121 void dwc3_gadget_giveback(struct dwc3_ep
*dep
, struct dwc3_request
*req
,
124 struct dwc3
*dwc
= dep
->dwc
;
127 if (req
->request
.num_mapped_sgs
)
128 dep
->busy_slot
+= req
->request
.num_mapped_sgs
;
133 * Skip LINK TRB. We can't use req->trb and check for
134 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
135 * completed (not the LINK TRB).
137 if (((dep
->busy_slot
& DWC3_TRB_MASK
) == DWC3_TRB_NUM
- 1) &&
138 usb_endpoint_xfer_isoc(dep
->desc
))
141 list_del(&req
->list
);
144 if (req
->request
.status
== -EINPROGRESS
)
145 req
->request
.status
= status
;
147 dwc3_unmap_buffer_from_dma(req
);
149 dev_dbg(dwc
->dev
, "request %p from %s completed %d/%d ===> %d\n",
150 req
, dep
->name
, req
->request
.actual
,
151 req
->request
.length
, status
);
153 spin_unlock(&dwc
->lock
);
154 req
->request
.complete(&req
->dep
->endpoint
, &req
->request
);
155 spin_lock(&dwc
->lock
);
158 static const char *dwc3_gadget_ep_cmd_string(u8 cmd
)
161 case DWC3_DEPCMD_DEPSTARTCFG
:
162 return "Start New Configuration";
163 case DWC3_DEPCMD_ENDTRANSFER
:
164 return "End Transfer";
165 case DWC3_DEPCMD_UPDATETRANSFER
:
166 return "Update Transfer";
167 case DWC3_DEPCMD_STARTTRANSFER
:
168 return "Start Transfer";
169 case DWC3_DEPCMD_CLEARSTALL
:
170 return "Clear Stall";
171 case DWC3_DEPCMD_SETSTALL
:
173 case DWC3_DEPCMD_GETSEQNUMBER
:
174 return "Get Data Sequence Number";
175 case DWC3_DEPCMD_SETTRANSFRESOURCE
:
176 return "Set Endpoint Transfer Resource";
177 case DWC3_DEPCMD_SETEPCONFIG
:
178 return "Set Endpoint Configuration";
180 return "UNKNOWN command";
184 int dwc3_send_gadget_ep_cmd(struct dwc3
*dwc
, unsigned ep
,
185 unsigned cmd
, struct dwc3_gadget_ep_cmd_params
*params
)
187 struct dwc3_ep
*dep
= dwc
->eps
[ep
];
191 dev_vdbg(dwc
->dev
, "%s: cmd '%s' params %08x %08x %08x\n",
193 dwc3_gadget_ep_cmd_string(cmd
), params
->param0
,
194 params
->param1
, params
->param2
);
196 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR0(ep
), params
->param0
);
197 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR1(ep
), params
->param1
);
198 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR2(ep
), params
->param2
);
200 dwc3_writel(dwc
->regs
, DWC3_DEPCMD(ep
), cmd
| DWC3_DEPCMD_CMDACT
);
202 reg
= dwc3_readl(dwc
->regs
, DWC3_DEPCMD(ep
));
203 if (!(reg
& DWC3_DEPCMD_CMDACT
)) {
204 dev_vdbg(dwc
->dev
, "Command Complete --> %d\n",
205 DWC3_DEPCMD_STATUS(reg
));
210 * We can't sleep here, because it is also called from
221 static dma_addr_t
dwc3_trb_dma_offset(struct dwc3_ep
*dep
,
222 struct dwc3_trb_hw
*trb
)
224 u32 offset
= (char *) trb
- (char *) dep
->trb_pool
;
226 return dep
->trb_pool_dma
+ offset
;
229 static int dwc3_alloc_trb_pool(struct dwc3_ep
*dep
)
231 struct dwc3
*dwc
= dep
->dwc
;
236 if (dep
->number
== 0 || dep
->number
== 1)
239 dep
->trb_pool
= dma_alloc_coherent(dwc
->dev
,
240 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
241 &dep
->trb_pool_dma
, GFP_KERNEL
);
242 if (!dep
->trb_pool
) {
243 dev_err(dep
->dwc
->dev
, "failed to allocate trb pool for %s\n",
251 static void dwc3_free_trb_pool(struct dwc3_ep
*dep
)
253 struct dwc3
*dwc
= dep
->dwc
;
255 dma_free_coherent(dwc
->dev
, sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
256 dep
->trb_pool
, dep
->trb_pool_dma
);
258 dep
->trb_pool
= NULL
;
259 dep
->trb_pool_dma
= 0;
262 static int dwc3_gadget_start_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
264 struct dwc3_gadget_ep_cmd_params params
;
267 memset(¶ms
, 0x00, sizeof(params
));
269 if (dep
->number
!= 1) {
270 cmd
= DWC3_DEPCMD_DEPSTARTCFG
;
271 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
272 if (dep
->number
> 1) {
273 if (dwc
->start_config_issued
)
275 dwc
->start_config_issued
= true;
276 cmd
|= DWC3_DEPCMD_PARAM(2);
279 return dwc3_send_gadget_ep_cmd(dwc
, 0, cmd
, ¶ms
);
285 static int dwc3_gadget_set_ep_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
286 const struct usb_endpoint_descriptor
*desc
,
287 const struct usb_ss_ep_comp_descriptor
*comp_desc
)
289 struct dwc3_gadget_ep_cmd_params params
;
291 memset(¶ms
, 0x00, sizeof(params
));
293 params
.param0
= DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc
))
294 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc
))
295 | DWC3_DEPCFG_BURST_SIZE(dep
->endpoint
.maxburst
);
297 params
.param1
= DWC3_DEPCFG_XFER_COMPLETE_EN
298 | DWC3_DEPCFG_XFER_NOT_READY_EN
;
300 if (comp_desc
&& USB_SS_MAX_STREAMS(comp_desc
->bmAttributes
)
301 && usb_endpoint_xfer_bulk(desc
)) {
302 params
.param1
|= DWC3_DEPCFG_STREAM_CAPABLE
303 | DWC3_DEPCFG_STREAM_EVENT_EN
;
304 dep
->stream_capable
= true;
307 if (usb_endpoint_xfer_isoc(desc
))
308 params
.param1
|= DWC3_DEPCFG_XFER_IN_PROGRESS_EN
;
311 * We are doing 1:1 mapping for endpoints, meaning
312 * Physical Endpoints 2 maps to Logical Endpoint 2 and
313 * so on. We consider the direction bit as part of the physical
314 * endpoint number. So USB endpoint 0x81 is 0x03.
316 params
.param1
|= DWC3_DEPCFG_EP_NUMBER(dep
->number
);
319 * We must use the lower 16 TX FIFOs even though
323 params
.param0
|= DWC3_DEPCFG_FIFO_NUMBER(dep
->number
>> 1);
325 if (desc
->bInterval
) {
326 params
.param1
|= DWC3_DEPCFG_BINTERVAL_M1(desc
->bInterval
- 1);
327 dep
->interval
= 1 << (desc
->bInterval
- 1);
330 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
331 DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
334 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
336 struct dwc3_gadget_ep_cmd_params params
;
338 memset(¶ms
, 0x00, sizeof(params
));
340 params
.param0
= DWC3_DEPXFERCFG_NUM_XFER_RES(1);
342 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
343 DWC3_DEPCMD_SETTRANSFRESOURCE
, ¶ms
);
347 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
348 * @dep: endpoint to be initialized
349 * @desc: USB Endpoint Descriptor
351 * Caller should take care of locking
353 static int __dwc3_gadget_ep_enable(struct dwc3_ep
*dep
,
354 const struct usb_endpoint_descriptor
*desc
,
355 const struct usb_ss_ep_comp_descriptor
*comp_desc
)
357 struct dwc3
*dwc
= dep
->dwc
;
361 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
362 ret
= dwc3_gadget_start_config(dwc
, dep
);
367 ret
= dwc3_gadget_set_ep_config(dwc
, dep
, desc
, comp_desc
);
371 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
372 struct dwc3_trb_hw
*trb_st_hw
;
373 struct dwc3_trb_hw
*trb_link_hw
;
374 struct dwc3_trb trb_link
;
376 ret
= dwc3_gadget_set_xfer_resource(dwc
, dep
);
381 dep
->comp_desc
= comp_desc
;
382 dep
->type
= usb_endpoint_type(desc
);
383 dep
->flags
|= DWC3_EP_ENABLED
;
385 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
386 reg
|= DWC3_DALEPENA_EP(dep
->number
);
387 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
389 if (!usb_endpoint_xfer_isoc(desc
))
392 memset(&trb_link
, 0, sizeof(trb_link
));
394 /* Link TRB for ISOC. The HWO but is never reset */
395 trb_st_hw
= &dep
->trb_pool
[0];
397 trb_link
.bplh
= dwc3_trb_dma_offset(dep
, trb_st_hw
);
398 trb_link
.trbctl
= DWC3_TRBCTL_LINK_TRB
;
401 trb_link_hw
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
402 dwc3_trb_to_hw(&trb_link
, trb_link_hw
);
408 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
);
409 static void dwc3_remove_requests(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
411 struct dwc3_request
*req
;
413 if (!list_empty(&dep
->req_queued
))
414 dwc3_stop_active_transfer(dwc
, dep
->number
);
416 while (!list_empty(&dep
->request_list
)) {
417 req
= next_request(&dep
->request_list
);
419 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
424 * __dwc3_gadget_ep_disable - Disables a HW endpoint
425 * @dep: the endpoint to disable
427 * This function also removes requests which are currently processed ny the
428 * hardware and those which are not yet scheduled.
429 * Caller should take care of locking.
431 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
433 struct dwc3
*dwc
= dep
->dwc
;
436 dwc3_remove_requests(dwc
, dep
);
438 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
439 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
440 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
442 dep
->stream_capable
= false;
444 dep
->comp_desc
= NULL
;
451 /* -------------------------------------------------------------------------- */
453 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
454 const struct usb_endpoint_descriptor
*desc
)
459 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
464 /* -------------------------------------------------------------------------- */
466 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
467 const struct usb_endpoint_descriptor
*desc
)
474 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
475 pr_debug("dwc3: invalid parameters\n");
479 if (!desc
->wMaxPacketSize
) {
480 pr_debug("dwc3: missing wMaxPacketSize\n");
484 dep
= to_dwc3_ep(ep
);
487 switch (usb_endpoint_type(desc
)) {
488 case USB_ENDPOINT_XFER_CONTROL
:
489 strncat(dep
->name
, "-control", sizeof(dep
->name
));
491 case USB_ENDPOINT_XFER_ISOC
:
492 strncat(dep
->name
, "-isoc", sizeof(dep
->name
));
494 case USB_ENDPOINT_XFER_BULK
:
495 strncat(dep
->name
, "-bulk", sizeof(dep
->name
));
497 case USB_ENDPOINT_XFER_INT
:
498 strncat(dep
->name
, "-int", sizeof(dep
->name
));
501 dev_err(dwc
->dev
, "invalid endpoint transfer type\n");
504 if (dep
->flags
& DWC3_EP_ENABLED
) {
505 dev_WARN_ONCE(dwc
->dev
, true, "%s is already enabled\n",
510 dev_vdbg(dwc
->dev
, "Enabling %s\n", dep
->name
);
512 spin_lock_irqsave(&dwc
->lock
, flags
);
513 ret
= __dwc3_gadget_ep_enable(dep
, desc
, ep
->comp_desc
);
514 spin_unlock_irqrestore(&dwc
->lock
, flags
);
519 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
527 pr_debug("dwc3: invalid parameters\n");
531 dep
= to_dwc3_ep(ep
);
534 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
535 dev_WARN_ONCE(dwc
->dev
, true, "%s is already disabled\n",
540 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s",
542 (dep
->number
& 1) ? "in" : "out");
544 spin_lock_irqsave(&dwc
->lock
, flags
);
545 ret
= __dwc3_gadget_ep_disable(dep
);
546 spin_unlock_irqrestore(&dwc
->lock
, flags
);
551 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
554 struct dwc3_request
*req
;
555 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
556 struct dwc3
*dwc
= dep
->dwc
;
558 req
= kzalloc(sizeof(*req
), gfp_flags
);
560 dev_err(dwc
->dev
, "not enough memory\n");
564 req
->epnum
= dep
->number
;
566 req
->request
.dma
= DMA_ADDR_INVALID
;
568 return &req
->request
;
571 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
572 struct usb_request
*request
)
574 struct dwc3_request
*req
= to_dwc3_request(request
);
580 * dwc3_prepare_one_trb - setup one TRB from one request
581 * @dep: endpoint for which this request is prepared
582 * @req: dwc3_request pointer
584 static void dwc3_prepare_one_trb(struct dwc3_ep
*dep
,
585 struct dwc3_request
*req
, dma_addr_t dma
,
586 unsigned length
, unsigned last
, unsigned chain
)
588 struct dwc3
*dwc
= dep
->dwc
;
589 struct dwc3_trb_hw
*trb_hw
;
592 unsigned int cur_slot
;
594 dev_vdbg(dwc
->dev
, "%s: req %p dma %08llx length %d%s%s\n",
595 dep
->name
, req
, (unsigned long long) dma
,
596 length
, last
? " last" : "",
597 chain
? " chain" : "");
599 trb_hw
= &dep
->trb_pool
[dep
->free_slot
& DWC3_TRB_MASK
];
600 cur_slot
= dep
->free_slot
;
603 /* Skip the LINK-TRB on ISOC */
604 if (((cur_slot
& DWC3_TRB_MASK
) == DWC3_TRB_NUM
- 1) &&
605 usb_endpoint_xfer_isoc(dep
->desc
))
608 memset(&trb
, 0, sizeof(trb
));
610 dwc3_gadget_move_request_queued(req
);
612 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb_hw
);
615 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
623 if (usb_endpoint_xfer_bulk(dep
->desc
) && dep
->stream_capable
)
624 trb
.sid_sofn
= req
->request
.stream_id
;
626 switch (usb_endpoint_type(dep
->desc
)) {
627 case USB_ENDPOINT_XFER_CONTROL
:
628 trb
.trbctl
= DWC3_TRBCTL_CONTROL_SETUP
;
631 case USB_ENDPOINT_XFER_ISOC
:
632 trb
.trbctl
= DWC3_TRBCTL_ISOCHRONOUS_FIRST
;
634 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
635 if (!(cur_slot
% (DWC3_TRB_NUM
/ 4)))
639 case USB_ENDPOINT_XFER_BULK
:
640 case USB_ENDPOINT_XFER_INT
:
641 trb
.trbctl
= DWC3_TRBCTL_NORMAL
;
645 * This is only possible with faulty memory because we
646 * checked it already :)
655 dwc3_trb_to_hw(&trb
, trb_hw
);
659 * dwc3_prepare_trbs - setup TRBs from requests
660 * @dep: endpoint for which requests are being prepared
661 * @starting: true if the endpoint is idle and no requests are queued.
663 * The functions goes through the requests list and setups TRBs for the
664 * transfers. The functions returns once there are not more TRBs available or
665 * it run out of requests.
667 static void dwc3_prepare_trbs(struct dwc3_ep
*dep
, bool starting
)
669 struct dwc3_request
*req
, *n
;
671 unsigned int last_one
= 0;
673 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
675 /* the first request must not be queued */
676 trbs_left
= (dep
->busy_slot
- dep
->free_slot
) & DWC3_TRB_MASK
;
679 * if busy & slot are equal than it is either full or empty. If we are
680 * starting to proceed requests then we are empty. Otherwise we ar
681 * full and don't do anything
686 trbs_left
= DWC3_TRB_NUM
;
688 * In case we start from scratch, we queue the ISOC requests
689 * starting from slot 1. This is done because we use ring
690 * buffer and have no LST bit to stop us. Instead, we place
691 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
692 * after the first request so we start at slot 1 and have
693 * 7 requests proceed before we hit the first IOC.
694 * Other transfer types don't use the ring buffer and are
695 * processed from the first TRB until the last one. Since we
696 * don't wrap around we have to start at the beginning.
698 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
707 /* The last TRB is a link TRB, not used for xfer */
708 if ((trbs_left
<= 1) && usb_endpoint_xfer_isoc(dep
->desc
))
711 list_for_each_entry_safe(req
, n
, &dep
->request_list
, list
) {
715 if (req
->request
.num_mapped_sgs
> 0) {
716 struct usb_request
*request
= &req
->request
;
717 struct scatterlist
*sg
= request
->sg
;
718 struct scatterlist
*s
;
721 for_each_sg(sg
, s
, request
->num_mapped_sgs
, i
) {
722 unsigned chain
= true;
724 length
= sg_dma_len(s
);
725 dma
= sg_dma_address(s
);
727 if (i
== (request
->num_mapped_sgs
- 1)
740 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
747 dma
= req
->request
.dma
;
748 length
= req
->request
.length
;
754 /* Is this the last request? */
755 if (list_is_last(&req
->list
, &dep
->request_list
))
758 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
767 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
, u16 cmd_param
,
770 struct dwc3_gadget_ep_cmd_params params
;
771 struct dwc3_request
*req
;
772 struct dwc3
*dwc
= dep
->dwc
;
776 if (start_new
&& (dep
->flags
& DWC3_EP_BUSY
)) {
777 dev_vdbg(dwc
->dev
, "%s: endpoint busy\n", dep
->name
);
780 dep
->flags
&= ~DWC3_EP_PENDING_REQUEST
;
783 * If we are getting here after a short-out-packet we don't enqueue any
784 * new requests as we try to set the IOC bit only on the last request.
787 if (list_empty(&dep
->req_queued
))
788 dwc3_prepare_trbs(dep
, start_new
);
790 /* req points to the first request which will be sent */
791 req
= next_request(&dep
->req_queued
);
793 dwc3_prepare_trbs(dep
, start_new
);
796 * req points to the first request where HWO changed
799 req
= next_request(&dep
->req_queued
);
802 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
806 memset(¶ms
, 0, sizeof(params
));
807 params
.param0
= upper_32_bits(req
->trb_dma
);
808 params
.param1
= lower_32_bits(req
->trb_dma
);
811 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
813 cmd
= DWC3_DEPCMD_UPDATETRANSFER
;
815 cmd
|= DWC3_DEPCMD_PARAM(cmd_param
);
816 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
818 dev_dbg(dwc
->dev
, "failed to send STARTTRANSFER command\n");
821 * FIXME we need to iterate over the list of requests
822 * here and stop, unmap, free and del each of the linked
823 * requests instead of we do now.
825 dwc3_unmap_buffer_from_dma(req
);
826 list_del(&req
->list
);
830 dep
->flags
|= DWC3_EP_BUSY
;
831 dep
->res_trans_idx
= dwc3_gadget_ep_get_transfer_index(dwc
,
834 WARN_ON_ONCE(!dep
->res_trans_idx
);
839 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
841 req
->request
.actual
= 0;
842 req
->request
.status
= -EINPROGRESS
;
843 req
->direction
= dep
->direction
;
844 req
->epnum
= dep
->number
;
847 * We only add to our list of requests now and
848 * start consuming the list once we get XferNotReady
851 * That way, we avoid doing anything that we don't need
852 * to do now and defer it until the point we receive a
853 * particular token from the Host side.
855 * This will also avoid Host cancelling URBs due to too
858 dwc3_map_buffer_to_dma(req
);
859 list_add_tail(&req
->list
, &dep
->request_list
);
862 * There is one special case: XferNotReady with
863 * empty list of requests. We need to kick the
864 * transfer here in that situation, otherwise
865 * we will be NAKing forever.
867 * If we get XferNotReady before gadget driver
868 * has a chance to queue a request, we will ACK
869 * the IRQ but won't be able to receive the data
870 * until the next request is queued. The following
871 * code is handling exactly that.
873 if (dep
->flags
& DWC3_EP_PENDING_REQUEST
) {
878 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
879 dep
->flags
& DWC3_EP_BUSY
)
882 ret
= __dwc3_gadget_kick_transfer(dep
, 0, start_trans
);
883 if (ret
&& ret
!= -EBUSY
) {
884 struct dwc3
*dwc
= dep
->dwc
;
886 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
894 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
897 struct dwc3_request
*req
= to_dwc3_request(request
);
898 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
899 struct dwc3
*dwc
= dep
->dwc
;
906 dev_dbg(dwc
->dev
, "trying to queue request %p to disabled %s\n",
911 dev_vdbg(dwc
->dev
, "queing request %p to %s length %d\n",
912 request
, ep
->name
, request
->length
);
914 spin_lock_irqsave(&dwc
->lock
, flags
);
915 ret
= __dwc3_gadget_ep_queue(dep
, req
);
916 spin_unlock_irqrestore(&dwc
->lock
, flags
);
921 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
922 struct usb_request
*request
)
924 struct dwc3_request
*req
= to_dwc3_request(request
);
925 struct dwc3_request
*r
= NULL
;
927 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
928 struct dwc3
*dwc
= dep
->dwc
;
933 spin_lock_irqsave(&dwc
->lock
, flags
);
935 list_for_each_entry(r
, &dep
->request_list
, list
) {
941 list_for_each_entry(r
, &dep
->req_queued
, list
) {
946 /* wait until it is processed */
947 dwc3_stop_active_transfer(dwc
, dep
->number
);
950 dev_err(dwc
->dev
, "request %p was not queued to %s\n",
956 /* giveback the request */
957 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
960 spin_unlock_irqrestore(&dwc
->lock
, flags
);
965 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
)
967 struct dwc3_gadget_ep_cmd_params params
;
968 struct dwc3
*dwc
= dep
->dwc
;
971 memset(¶ms
, 0x00, sizeof(params
));
974 if (dep
->number
== 0 || dep
->number
== 1) {
976 * Whenever EP0 is stalled, we will restart
977 * the state machine, thus moving back to
980 dwc
->ep0state
= EP0_SETUP_PHASE
;
983 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
984 DWC3_DEPCMD_SETSTALL
, ¶ms
);
986 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
987 value
? "set" : "clear",
990 dep
->flags
|= DWC3_EP_STALL
;
992 if (dep
->flags
& DWC3_EP_WEDGE
)
995 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
996 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
998 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
999 value
? "set" : "clear",
1002 dep
->flags
&= ~DWC3_EP_STALL
;
1008 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
1010 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1011 struct dwc3
*dwc
= dep
->dwc
;
1013 unsigned long flags
;
1017 spin_lock_irqsave(&dwc
->lock
, flags
);
1019 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1020 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
1025 ret
= __dwc3_gadget_ep_set_halt(dep
, value
);
1027 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1032 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
1034 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1036 dep
->flags
|= DWC3_EP_WEDGE
;
1038 return dwc3_gadget_ep_set_halt(ep
, 1);
1041 /* -------------------------------------------------------------------------- */
1043 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
1044 .bLength
= USB_DT_ENDPOINT_SIZE
,
1045 .bDescriptorType
= USB_DT_ENDPOINT
,
1046 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1049 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
1050 .enable
= dwc3_gadget_ep0_enable
,
1051 .disable
= dwc3_gadget_ep0_disable
,
1052 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1053 .free_request
= dwc3_gadget_ep_free_request
,
1054 .queue
= dwc3_gadget_ep0_queue
,
1055 .dequeue
= dwc3_gadget_ep_dequeue
,
1056 .set_halt
= dwc3_gadget_ep_set_halt
,
1057 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1060 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
1061 .enable
= dwc3_gadget_ep_enable
,
1062 .disable
= dwc3_gadget_ep_disable
,
1063 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1064 .free_request
= dwc3_gadget_ep_free_request
,
1065 .queue
= dwc3_gadget_ep_queue
,
1066 .dequeue
= dwc3_gadget_ep_dequeue
,
1067 .set_halt
= dwc3_gadget_ep_set_halt
,
1068 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1071 /* -------------------------------------------------------------------------- */
1073 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
1075 struct dwc3
*dwc
= gadget_to_dwc(g
);
1078 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1079 return DWC3_DSTS_SOFFN(reg
);
1082 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
1084 struct dwc3
*dwc
= gadget_to_dwc(g
);
1086 unsigned long timeout
;
1087 unsigned long flags
;
1096 spin_lock_irqsave(&dwc
->lock
, flags
);
1099 * According to the Databook Remote wakeup request should
1100 * be issued only when the device is in early suspend state.
1102 * We can check that via USB Link State bits in DSTS register.
1104 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1106 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1107 if (speed
== DWC3_DSTS_SUPERSPEED
) {
1108 dev_dbg(dwc
->dev
, "no wakeup on SuperSpeed\n");
1113 link_state
= DWC3_DSTS_USBLNKST(reg
);
1115 switch (link_state
) {
1116 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1117 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1120 dev_dbg(dwc
->dev
, "can't wakeup from link state %d\n",
1126 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1129 * Switch link state to Recovery. In HS/FS/LS this means
1130 * RemoteWakeup Request
1132 reg
|= DWC3_DCTL_ULSTCHNG_RECOVERY
;
1133 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1135 /* wait for at least 2000us */
1136 usleep_range(2000, 2500);
1138 /* write zeroes to Link Change Request */
1139 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1140 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1142 /* pool until Link State change to ON */
1143 timeout
= jiffies
+ msecs_to_jiffies(100);
1145 while (!(time_after(jiffies
, timeout
))) {
1146 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1148 /* in HS, means ON */
1149 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1153 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1154 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1159 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1164 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1167 struct dwc3
*dwc
= gadget_to_dwc(g
);
1169 dwc
->is_selfpowered
= !!is_selfpowered
;
1174 static void dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
)
1179 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1181 reg
|= DWC3_DCTL_RUN_STOP
;
1183 reg
&= ~DWC3_DCTL_RUN_STOP
;
1185 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1188 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1190 if (!(reg
& DWC3_DSTS_DEVCTRLHLT
))
1193 if (reg
& DWC3_DSTS_DEVCTRLHLT
)
1202 dev_vdbg(dwc
->dev
, "gadget %s data soft-%s\n",
1204 ? dwc
->gadget_driver
->function
: "no-function",
1205 is_on
? "connect" : "disconnect");
1208 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1210 struct dwc3
*dwc
= gadget_to_dwc(g
);
1211 unsigned long flags
;
1215 spin_lock_irqsave(&dwc
->lock
, flags
);
1216 dwc3_gadget_run_stop(dwc
, is_on
);
1217 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1222 static int dwc3_gadget_start(struct usb_gadget
*g
,
1223 struct usb_gadget_driver
*driver
)
1225 struct dwc3
*dwc
= gadget_to_dwc(g
);
1226 struct dwc3_ep
*dep
;
1227 unsigned long flags
;
1231 spin_lock_irqsave(&dwc
->lock
, flags
);
1233 if (dwc
->gadget_driver
) {
1234 dev_err(dwc
->dev
, "%s is already bound to %s\n",
1236 dwc
->gadget_driver
->driver
.name
);
1241 dwc
->gadget_driver
= driver
;
1242 dwc
->gadget
.dev
.driver
= &driver
->driver
;
1244 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1245 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
1246 reg
|= dwc
->maximum_speed
;
1247 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1249 dwc
->start_config_issued
= false;
1251 /* Start with SuperSpeed Default */
1252 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1255 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1257 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1262 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1264 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1268 /* begin to receive SETUP packets */
1269 dwc
->ep0state
= EP0_SETUP_PHASE
;
1270 dwc3_ep0_out_start(dwc
);
1272 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1277 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1280 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1285 static int dwc3_gadget_stop(struct usb_gadget
*g
,
1286 struct usb_gadget_driver
*driver
)
1288 struct dwc3
*dwc
= gadget_to_dwc(g
);
1289 unsigned long flags
;
1291 spin_lock_irqsave(&dwc
->lock
, flags
);
1293 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1294 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
1296 dwc
->gadget_driver
= NULL
;
1297 dwc
->gadget
.dev
.driver
= NULL
;
1299 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1303 static const struct usb_gadget_ops dwc3_gadget_ops
= {
1304 .get_frame
= dwc3_gadget_get_frame
,
1305 .wakeup
= dwc3_gadget_wakeup
,
1306 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
1307 .pullup
= dwc3_gadget_pullup
,
1308 .udc_start
= dwc3_gadget_start
,
1309 .udc_stop
= dwc3_gadget_stop
,
1312 /* -------------------------------------------------------------------------- */
1314 static int __devinit
dwc3_gadget_init_endpoints(struct dwc3
*dwc
)
1316 struct dwc3_ep
*dep
;
1319 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
1321 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1322 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
1324 dev_err(dwc
->dev
, "can't allocate endpoint %d\n",
1330 dep
->number
= epnum
;
1331 dwc
->eps
[epnum
] = dep
;
1333 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s", epnum
>> 1,
1334 (epnum
& 1) ? "in" : "out");
1335 dep
->endpoint
.name
= dep
->name
;
1336 dep
->direction
= (epnum
& 1);
1338 if (epnum
== 0 || epnum
== 1) {
1339 dep
->endpoint
.maxpacket
= 512;
1340 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
1342 dwc
->gadget
.ep0
= &dep
->endpoint
;
1346 dep
->endpoint
.maxpacket
= 1024;
1347 dep
->endpoint
.max_streams
= 15;
1348 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
1349 list_add_tail(&dep
->endpoint
.ep_list
,
1350 &dwc
->gadget
.ep_list
);
1352 ret
= dwc3_alloc_trb_pool(dep
);
1357 INIT_LIST_HEAD(&dep
->request_list
);
1358 INIT_LIST_HEAD(&dep
->req_queued
);
1364 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
1366 struct dwc3_ep
*dep
;
1369 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1370 dep
= dwc
->eps
[epnum
];
1371 dwc3_free_trb_pool(dep
);
1373 if (epnum
!= 0 && epnum
!= 1)
1374 list_del(&dep
->endpoint
.ep_list
);
1380 static void dwc3_gadget_release(struct device
*dev
)
1382 dev_dbg(dev
, "%s\n", __func__
);
1385 /* -------------------------------------------------------------------------- */
1386 static int dwc3_cleanup_done_reqs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
1387 const struct dwc3_event_depevt
*event
, int status
)
1389 struct dwc3_request
*req
;
1390 struct dwc3_trb trb
;
1392 unsigned int s_pkt
= 0;
1395 req
= next_request(&dep
->req_queued
);
1401 dwc3_trb_to_nat(req
->trb
, &trb
);
1403 if (trb
.hwo
&& status
!= -ESHUTDOWN
)
1405 * We continue despite the error. There is not much we
1406 * can do. If we don't clean in up we loop for ever. If
1407 * we skip the TRB than it gets overwritten reused after
1408 * a while since we use them in a ring buffer. a BUG()
1409 * would help. Lets hope that if this occures, someone
1410 * fixes the root cause instead of looking away :)
1412 dev_err(dwc
->dev
, "%s's TRB (%p) still owned by HW\n",
1413 dep
->name
, req
->trb
);
1416 if (dep
->direction
) {
1418 dev_err(dwc
->dev
, "incomplete IN transfer %s\n",
1420 status
= -ECONNRESET
;
1423 if (count
&& (event
->status
& DEPEVT_STATUS_SHORT
))
1428 * We assume here we will always receive the entire data block
1429 * which we should receive. Meaning, if we program RX to
1430 * receive 4K but we receive only 2K, we assume that's all we
1431 * should receive and we simply bounce the request back to the
1432 * gadget driver for further processing.
1434 req
->request
.actual
+= req
->request
.length
- count
;
1435 dwc3_gadget_giveback(dep
, req
, status
);
1438 if ((event
->status
& DEPEVT_STATUS_LST
) && trb
.lst
)
1440 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1444 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1449 static void dwc3_endpoint_transfer_complete(struct dwc3
*dwc
,
1450 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
,
1453 unsigned status
= 0;
1456 if (event
->status
& DEPEVT_STATUS_BUSERR
)
1457 status
= -ECONNRESET
;
1459 clean_busy
= dwc3_cleanup_done_reqs(dwc
, dep
, event
, status
);
1461 dep
->flags
&= ~DWC3_EP_BUSY
;
1462 dep
->res_trans_idx
= 0;
1466 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1467 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1469 if (dwc
->revision
< DWC3_REVISION_183A
) {
1473 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
1474 struct dwc3_ep
*dep
= dwc
->eps
[i
];
1476 if (!(dep
->flags
& DWC3_EP_ENABLED
))
1479 if (!list_empty(&dep
->req_queued
))
1483 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1485 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1491 static void dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1492 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
1496 if (list_empty(&dep
->request_list
)) {
1497 dev_vdbg(dwc
->dev
, "ISOC ep %s run out for requests.\n",
1502 if (event
->parameters
) {
1505 mask
= ~(dep
->interval
- 1);
1506 uf
= event
->parameters
& mask
;
1507 /* 4 micro frames in the future */
1508 uf
+= dep
->interval
* 4;
1513 __dwc3_gadget_kick_transfer(dep
, uf
, 1);
1516 static void dwc3_process_ep_cmd_complete(struct dwc3_ep
*dep
,
1517 const struct dwc3_event_depevt
*event
)
1519 struct dwc3
*dwc
= dep
->dwc
;
1520 struct dwc3_event_depevt mod_ev
= *event
;
1523 * We were asked to remove one requests. It is possible that this
1524 * request and a few other were started together and have the same
1525 * transfer index. Since we stopped the complete endpoint we don't
1526 * know how many requests were already completed (and not yet)
1527 * reported and how could be done (later). We purge them all until
1528 * the end of the list.
1530 mod_ev
.status
= DEPEVT_STATUS_LST
;
1531 dwc3_cleanup_done_reqs(dwc
, dep
, &mod_ev
, -ESHUTDOWN
);
1532 dep
->flags
&= ~DWC3_EP_BUSY
;
1533 /* pending requets are ignored and are queued on XferNotReady */
1536 static void dwc3_ep_cmd_compl(struct dwc3_ep
*dep
,
1537 const struct dwc3_event_depevt
*event
)
1539 u32 param
= event
->parameters
;
1540 u32 cmd_type
= (param
>> 8) & ((1 << 5) - 1);
1543 case DWC3_DEPCMD_ENDTRANSFER
:
1544 dwc3_process_ep_cmd_complete(dep
, event
);
1546 case DWC3_DEPCMD_STARTTRANSFER
:
1547 dep
->res_trans_idx
= param
& 0x7f;
1550 printk(KERN_ERR
"%s() unknown /unexpected type: %d\n",
1551 __func__
, cmd_type
);
1556 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
1557 const struct dwc3_event_depevt
*event
)
1559 struct dwc3_ep
*dep
;
1560 u8 epnum
= event
->endpoint_number
;
1562 dep
= dwc
->eps
[epnum
];
1564 dev_vdbg(dwc
->dev
, "%s: %s\n", dep
->name
,
1565 dwc3_ep_event_string(event
->endpoint_event
));
1567 if (epnum
== 0 || epnum
== 1) {
1568 dwc3_ep0_interrupt(dwc
, event
);
1572 switch (event
->endpoint_event
) {
1573 case DWC3_DEPEVT_XFERCOMPLETE
:
1574 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1575 dev_dbg(dwc
->dev
, "%s is an Isochronous endpoint\n",
1580 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 1);
1582 case DWC3_DEPEVT_XFERINPROGRESS
:
1583 if (!usb_endpoint_xfer_isoc(dep
->desc
)) {
1584 dev_dbg(dwc
->dev
, "%s is not an Isochronous endpoint\n",
1589 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 0);
1591 case DWC3_DEPEVT_XFERNOTREADY
:
1592 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1593 dwc3_gadget_start_isoc(dwc
, dep
, event
);
1597 dev_vdbg(dwc
->dev
, "%s: reason %s\n",
1598 dep
->name
, event
->status
1600 : "Transfer Not Active");
1602 ret
= __dwc3_gadget_kick_transfer(dep
, 0, 1);
1603 if (!ret
|| ret
== -EBUSY
)
1606 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
1611 case DWC3_DEPEVT_STREAMEVT
:
1612 if (!usb_endpoint_xfer_bulk(dep
->desc
)) {
1613 dev_err(dwc
->dev
, "Stream event for non-Bulk %s\n",
1618 switch (event
->status
) {
1619 case DEPEVT_STREAMEVT_FOUND
:
1620 dev_vdbg(dwc
->dev
, "Stream %d found and started\n",
1624 case DEPEVT_STREAMEVT_NOTFOUND
:
1627 dev_dbg(dwc
->dev
, "Couldn't find suitable stream\n");
1630 case DWC3_DEPEVT_RXTXFIFOEVT
:
1631 dev_dbg(dwc
->dev
, "%s FIFO Overrun\n", dep
->name
);
1633 case DWC3_DEPEVT_EPCMDCMPLT
:
1634 dwc3_ep_cmd_compl(dep
, event
);
1639 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
1641 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
1642 spin_unlock(&dwc
->lock
);
1643 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
1644 spin_lock(&dwc
->lock
);
1648 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
)
1650 struct dwc3_ep
*dep
;
1651 struct dwc3_gadget_ep_cmd_params params
;
1655 dep
= dwc
->eps
[epnum
];
1657 WARN_ON(!dep
->res_trans_idx
);
1658 if (dep
->res_trans_idx
) {
1659 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
1660 cmd
|= DWC3_DEPCMD_HIPRI_FORCERM
| DWC3_DEPCMD_CMDIOC
;
1661 cmd
|= DWC3_DEPCMD_PARAM(dep
->res_trans_idx
);
1662 memset(¶ms
, 0, sizeof(params
));
1663 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
1665 dep
->res_trans_idx
= 0;
1669 static void dwc3_stop_active_transfers(struct dwc3
*dwc
)
1673 for (epnum
= 2; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1674 struct dwc3_ep
*dep
;
1676 dep
= dwc
->eps
[epnum
];
1677 if (!(dep
->flags
& DWC3_EP_ENABLED
))
1680 dwc3_remove_requests(dwc
, dep
);
1684 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
1688 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1689 struct dwc3_ep
*dep
;
1690 struct dwc3_gadget_ep_cmd_params params
;
1693 dep
= dwc
->eps
[epnum
];
1695 if (!(dep
->flags
& DWC3_EP_STALL
))
1698 dep
->flags
&= ~DWC3_EP_STALL
;
1700 memset(¶ms
, 0, sizeof(params
));
1701 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
1702 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
1707 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
1709 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1712 U1
/U2 is powersave optimization
. Skip it
for now
. Anyway we need to
1713 enable it before we can disable it
.
1715 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1716 reg
&= ~DWC3_DCTL_INITU1ENA
;
1717 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1719 reg
&= ~DWC3_DCTL_INITU2ENA
;
1720 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1723 dwc3_stop_active_transfers(dwc
);
1724 dwc3_disconnect_gadget(dwc
);
1725 dwc
->start_config_issued
= false;
1727 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1728 dwc
->setup_packet_pending
= false;
1731 static void dwc3_gadget_usb3_phy_power(struct dwc3
*dwc
, int on
)
1735 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(0));
1738 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
1740 reg
|= DWC3_GUSB3PIPECTL_SUSPHY
;
1742 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(0), reg
);
1745 static void dwc3_gadget_usb2_phy_power(struct dwc3
*dwc
, int on
)
1749 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
1752 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
1754 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
1756 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
1759 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
1763 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1766 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1767 * would cause a missing Disconnect Event if there's a
1768 * pending Setup Packet in the FIFO.
1770 * There's no suggested workaround on the official Bug
1771 * report, which states that "unless the driver/application
1772 * is doing any special handling of a disconnect event,
1773 * there is no functional issue".
1775 * Unfortunately, it turns out that we _do_ some special
1776 * handling of a disconnect event, namely complete all
1777 * pending transfers, notify gadget driver of the
1778 * disconnection, and so on.
1780 * Our suggested workaround is to follow the Disconnect
1781 * Event steps here, instead, based on a setup_packet_pending
1782 * flag. Such flag gets set whenever we have a XferNotReady
1783 * event on EP0 and gets cleared on XferComplete for the
1788 * STAR#9000466709: RTL: Device : Disconnect event not
1789 * generated if setup packet pending in FIFO
1791 if (dwc
->revision
< DWC3_REVISION_188A
) {
1792 if (dwc
->setup_packet_pending
)
1793 dwc3_gadget_disconnect_interrupt(dwc
);
1796 /* after reset -> Default State */
1797 dwc
->dev_state
= DWC3_DEFAULT_STATE
;
1800 dwc3_gadget_usb2_phy_power(dwc
, true);
1801 dwc3_gadget_usb3_phy_power(dwc
, true);
1803 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
1804 dwc3_disconnect_gadget(dwc
);
1806 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1807 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
1808 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1810 dwc3_stop_active_transfers(dwc
);
1811 dwc3_clear_stall_all_ep(dwc
);
1812 dwc
->start_config_issued
= false;
1814 /* Reset device address to zero */
1815 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1816 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
1817 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1820 static void dwc3_update_ram_clk_sel(struct dwc3
*dwc
, u32 speed
)
1823 u32 usb30_clock
= DWC3_GCTL_CLK_BUS
;
1826 * We change the clock only at SS but I dunno why I would want to do
1827 * this. Maybe it becomes part of the power saving plan.
1830 if (speed
!= DWC3_DSTS_SUPERSPEED
)
1834 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1835 * each time on Connect Done.
1840 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1841 reg
|= DWC3_GCTL_RAMCLKSEL(usb30_clock
);
1842 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1845 static void dwc3_gadget_disable_phy(struct dwc3
*dwc
, u8 speed
)
1848 case USB_SPEED_SUPER
:
1849 dwc3_gadget_usb2_phy_power(dwc
, false);
1851 case USB_SPEED_HIGH
:
1852 case USB_SPEED_FULL
:
1854 dwc3_gadget_usb3_phy_power(dwc
, false);
1859 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
1861 struct dwc3_gadget_ep_cmd_params params
;
1862 struct dwc3_ep
*dep
;
1867 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1869 memset(¶ms
, 0x00, sizeof(params
));
1871 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1872 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1875 dwc3_update_ram_clk_sel(dwc
, speed
);
1878 case DWC3_DCFG_SUPERSPEED
:
1880 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1881 * would cause a missing USB3 Reset event.
1883 * In such situations, we should force a USB3 Reset
1884 * event by calling our dwc3_gadget_reset_interrupt()
1889 * STAR#9000483510: RTL: SS : USB3 reset event may
1890 * not be generated always when the link enters poll
1892 if (dwc
->revision
< DWC3_REVISION_190A
)
1893 dwc3_gadget_reset_interrupt(dwc
);
1895 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1896 dwc
->gadget
.ep0
->maxpacket
= 512;
1897 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
1899 case DWC3_DCFG_HIGHSPEED
:
1900 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1901 dwc
->gadget
.ep0
->maxpacket
= 64;
1902 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
1904 case DWC3_DCFG_FULLSPEED2
:
1905 case DWC3_DCFG_FULLSPEED1
:
1906 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1907 dwc
->gadget
.ep0
->maxpacket
= 64;
1908 dwc
->gadget
.speed
= USB_SPEED_FULL
;
1910 case DWC3_DCFG_LOWSPEED
:
1911 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
1912 dwc
->gadget
.ep0
->maxpacket
= 8;
1913 dwc
->gadget
.speed
= USB_SPEED_LOW
;
1917 /* Disable unneded PHY */
1918 dwc3_gadget_disable_phy(dwc
, dwc
->gadget
.speed
);
1921 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1923 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1928 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1930 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1935 * Configure PHY via GUSB3PIPECTLn if required.
1937 * Update GTXFIFOSIZn
1939 * In both cases reset values should be sufficient.
1943 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
1945 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1948 * TODO take core out of low power mode when that's
1952 dwc
->gadget_driver
->resume(&dwc
->gadget
);
1955 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
1956 unsigned int evtinfo
)
1958 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
1961 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
1962 * on the link partner, the USB session might do multiple entry/exit
1963 * of low power states before a transfer takes place.
1965 * Due to this problem, we might experience lower throughput. The
1966 * suggested workaround is to disable DCTL[12:9] bits if we're
1967 * transitioning from U1/U2 to U0 and enable those bits again
1968 * after a transfer completes and there are no pending transfers
1969 * on any of the enabled endpoints.
1971 * This is the first half of that workaround.
1975 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
1976 * core send LGO_Ux entering U0
1978 if (dwc
->revision
< DWC3_REVISION_183A
) {
1979 if (next
== DWC3_LINK_STATE_U0
) {
1983 switch (dwc
->link_state
) {
1984 case DWC3_LINK_STATE_U1
:
1985 case DWC3_LINK_STATE_U2
:
1986 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1987 u1u2
= reg
& (DWC3_DCTL_INITU2ENA
1988 | DWC3_DCTL_ACCEPTU2ENA
1989 | DWC3_DCTL_INITU1ENA
1990 | DWC3_DCTL_ACCEPTU1ENA
);
1993 dwc
->u1u2
= reg
& u1u2
;
1997 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2006 dwc
->link_state
= next
;
2008 dev_vdbg(dwc
->dev
, "%s link %d\n", __func__
, dwc
->link_state
);
2011 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
2012 const struct dwc3_event_devt
*event
)
2014 switch (event
->type
) {
2015 case DWC3_DEVICE_EVENT_DISCONNECT
:
2016 dwc3_gadget_disconnect_interrupt(dwc
);
2018 case DWC3_DEVICE_EVENT_RESET
:
2019 dwc3_gadget_reset_interrupt(dwc
);
2021 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
2022 dwc3_gadget_conndone_interrupt(dwc
);
2024 case DWC3_DEVICE_EVENT_WAKEUP
:
2025 dwc3_gadget_wakeup_interrupt(dwc
);
2027 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
2028 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
2030 case DWC3_DEVICE_EVENT_EOPF
:
2031 dev_vdbg(dwc
->dev
, "End of Periodic Frame\n");
2033 case DWC3_DEVICE_EVENT_SOF
:
2034 dev_vdbg(dwc
->dev
, "Start of Periodic Frame\n");
2036 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
2037 dev_vdbg(dwc
->dev
, "Erratic Error\n");
2039 case DWC3_DEVICE_EVENT_CMD_CMPL
:
2040 dev_vdbg(dwc
->dev
, "Command Complete\n");
2042 case DWC3_DEVICE_EVENT_OVERFLOW
:
2043 dev_vdbg(dwc
->dev
, "Overflow\n");
2046 dev_dbg(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
2050 static void dwc3_process_event_entry(struct dwc3
*dwc
,
2051 const union dwc3_event
*event
)
2053 /* Endpoint IRQ, handle it and return early */
2054 if (event
->type
.is_devspec
== 0) {
2056 return dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
2059 switch (event
->type
.type
) {
2060 case DWC3_EVENT_TYPE_DEV
:
2061 dwc3_gadget_interrupt(dwc
, &event
->devt
);
2063 /* REVISIT what to do with Carkit and I2C events ? */
2065 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
2069 static irqreturn_t
dwc3_process_event_buf(struct dwc3
*dwc
, u32 buf
)
2071 struct dwc3_event_buffer
*evt
;
2075 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(buf
));
2076 count
&= DWC3_GEVNTCOUNT_MASK
;
2080 evt
= dwc
->ev_buffs
[buf
];
2084 union dwc3_event event
;
2086 memcpy(&event
.raw
, (evt
->buf
+ evt
->lpos
), sizeof(event
.raw
));
2087 dwc3_process_event_entry(dwc
, &event
);
2089 * XXX we wrap around correctly to the next entry as almost all
2090 * entries are 4 bytes in size. There is one entry which has 12
2091 * bytes which is a regular entry followed by 8 bytes data. ATM
2092 * I don't know how things are organized if were get next to the
2093 * a boundary so I worry about that once we try to handle that.
2095 evt
->lpos
= (evt
->lpos
+ 4) % DWC3_EVENT_BUFFERS_SIZE
;
2098 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(buf
), 4);
2104 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
)
2106 struct dwc3
*dwc
= _dwc
;
2108 irqreturn_t ret
= IRQ_NONE
;
2110 spin_lock(&dwc
->lock
);
2112 for (i
= 0; i
< dwc
->num_event_buffers
; i
++) {
2115 status
= dwc3_process_event_buf(dwc
, i
);
2116 if (status
== IRQ_HANDLED
)
2120 spin_unlock(&dwc
->lock
);
2126 * dwc3_gadget_init - Initializes gadget related registers
2127 * @dwc: Pointer to out controller context structure
2129 * Returns 0 on success otherwise negative errno.
2131 int __devinit
dwc3_gadget_init(struct dwc3
*dwc
)
2137 dwc
->ctrl_req
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2138 &dwc
->ctrl_req_addr
, GFP_KERNEL
);
2139 if (!dwc
->ctrl_req
) {
2140 dev_err(dwc
->dev
, "failed to allocate ctrl request\n");
2145 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2146 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
2147 if (!dwc
->ep0_trb
) {
2148 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
2153 dwc
->setup_buf
= dma_alloc_coherent(dwc
->dev
,
2154 sizeof(*dwc
->setup_buf
) * 2,
2155 &dwc
->setup_buf_addr
, GFP_KERNEL
);
2156 if (!dwc
->setup_buf
) {
2157 dev_err(dwc
->dev
, "failed to allocate setup buffer\n");
2162 dwc
->ep0_bounce
= dma_alloc_coherent(dwc
->dev
,
2163 512, &dwc
->ep0_bounce_addr
, GFP_KERNEL
);
2164 if (!dwc
->ep0_bounce
) {
2165 dev_err(dwc
->dev
, "failed to allocate ep0 bounce buffer\n");
2170 dev_set_name(&dwc
->gadget
.dev
, "gadget");
2172 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
2173 dwc
->gadget
.max_speed
= USB_SPEED_SUPER
;
2174 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2175 dwc
->gadget
.dev
.parent
= dwc
->dev
;
2176 dwc
->gadget
.sg_supported
= true;
2178 dma_set_coherent_mask(&dwc
->gadget
.dev
, dwc
->dev
->coherent_dma_mask
);
2180 dwc
->gadget
.dev
.dma_parms
= dwc
->dev
->dma_parms
;
2181 dwc
->gadget
.dev
.dma_mask
= dwc
->dev
->dma_mask
;
2182 dwc
->gadget
.dev
.release
= dwc3_gadget_release
;
2183 dwc
->gadget
.name
= "dwc3-gadget";
2186 * REVISIT: Here we should clear all pending IRQs to be
2187 * sure we're starting from a well known location.
2190 ret
= dwc3_gadget_init_endpoints(dwc
);
2194 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
2196 ret
= request_irq(irq
, dwc3_interrupt
, IRQF_SHARED
,
2199 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
2204 /* Enable all but Start and End of Frame IRQs */
2205 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
2206 DWC3_DEVTEN_EVNTOVERFLOWEN
|
2207 DWC3_DEVTEN_CMDCMPLTEN
|
2208 DWC3_DEVTEN_ERRTICERREN
|
2209 DWC3_DEVTEN_WKUPEVTEN
|
2210 DWC3_DEVTEN_ULSTCNGEN
|
2211 DWC3_DEVTEN_CONNECTDONEEN
|
2212 DWC3_DEVTEN_USBRSTEN
|
2213 DWC3_DEVTEN_DISCONNEVTEN
);
2214 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
2216 ret
= device_register(&dwc
->gadget
.dev
);
2218 dev_err(dwc
->dev
, "failed to register gadget device\n");
2219 put_device(&dwc
->gadget
.dev
);
2223 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
2225 dev_err(dwc
->dev
, "failed to register udc\n");
2232 device_unregister(&dwc
->gadget
.dev
);
2235 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2239 dwc3_gadget_free_endpoints(dwc
);
2242 dma_free_coherent(dwc
->dev
, 512, dwc
->ep0_bounce
,
2243 dwc
->ep0_bounce_addr
);
2246 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2247 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2250 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2251 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2254 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2255 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2261 void dwc3_gadget_exit(struct dwc3
*dwc
)
2265 usb_del_gadget_udc(&dwc
->gadget
);
2266 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
2268 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2271 dwc3_gadget_free_endpoints(dwc
);
2273 dma_free_coherent(dwc
->dev
, 512, dwc
->ep0_bounce
,
2274 dwc
->ep0_bounce_addr
);
2276 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2277 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2279 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2280 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2282 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2283 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2285 device_unregister(&dwc
->gadget
.dev
);