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_mapped_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 (usb_ss_max_streams(comp_desc
) && usb_endpoint_xfer_bulk(desc
)) {
301 params
.param1
|= DWC3_DEPCFG_STREAM_CAPABLE
302 | DWC3_DEPCFG_STREAM_EVENT_EN
;
303 dep
->stream_capable
= true;
306 if (usb_endpoint_xfer_isoc(desc
))
307 params
.param1
|= DWC3_DEPCFG_XFER_IN_PROGRESS_EN
;
310 * We are doing 1:1 mapping for endpoints, meaning
311 * Physical Endpoints 2 maps to Logical Endpoint 2 and
312 * so on. We consider the direction bit as part of the physical
313 * endpoint number. So USB endpoint 0x81 is 0x03.
315 params
.param1
|= DWC3_DEPCFG_EP_NUMBER(dep
->number
);
318 * We must use the lower 16 TX FIFOs even though
322 params
.param0
|= DWC3_DEPCFG_FIFO_NUMBER(dep
->number
>> 1);
324 if (desc
->bInterval
) {
325 params
.param1
|= DWC3_DEPCFG_BINTERVAL_M1(desc
->bInterval
- 1);
326 dep
->interval
= 1 << (desc
->bInterval
- 1);
329 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
330 DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
333 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
335 struct dwc3_gadget_ep_cmd_params params
;
337 memset(¶ms
, 0x00, sizeof(params
));
339 params
.param0
= DWC3_DEPXFERCFG_NUM_XFER_RES(1);
341 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
342 DWC3_DEPCMD_SETTRANSFRESOURCE
, ¶ms
);
346 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
347 * @dep: endpoint to be initialized
348 * @desc: USB Endpoint Descriptor
350 * Caller should take care of locking
352 static int __dwc3_gadget_ep_enable(struct dwc3_ep
*dep
,
353 const struct usb_endpoint_descriptor
*desc
,
354 const struct usb_ss_ep_comp_descriptor
*comp_desc
)
356 struct dwc3
*dwc
= dep
->dwc
;
360 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
361 ret
= dwc3_gadget_start_config(dwc
, dep
);
366 ret
= dwc3_gadget_set_ep_config(dwc
, dep
, desc
, comp_desc
);
370 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
371 struct dwc3_trb_hw
*trb_st_hw
;
372 struct dwc3_trb_hw
*trb_link_hw
;
373 struct dwc3_trb trb_link
;
375 ret
= dwc3_gadget_set_xfer_resource(dwc
, dep
);
380 dep
->comp_desc
= comp_desc
;
381 dep
->type
= usb_endpoint_type(desc
);
382 dep
->flags
|= DWC3_EP_ENABLED
;
384 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
385 reg
|= DWC3_DALEPENA_EP(dep
->number
);
386 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
388 if (!usb_endpoint_xfer_isoc(desc
))
391 memset(&trb_link
, 0, sizeof(trb_link
));
393 /* Link TRB for ISOC. The HWO but is never reset */
394 trb_st_hw
= &dep
->trb_pool
[0];
396 trb_link
.bplh
= dwc3_trb_dma_offset(dep
, trb_st_hw
);
397 trb_link
.trbctl
= DWC3_TRBCTL_LINK_TRB
;
400 trb_link_hw
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
401 dwc3_trb_to_hw(&trb_link
, trb_link_hw
);
407 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
);
408 static void dwc3_remove_requests(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
410 struct dwc3_request
*req
;
412 if (!list_empty(&dep
->req_queued
))
413 dwc3_stop_active_transfer(dwc
, dep
->number
);
415 while (!list_empty(&dep
->request_list
)) {
416 req
= next_request(&dep
->request_list
);
418 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
423 * __dwc3_gadget_ep_disable - Disables a HW endpoint
424 * @dep: the endpoint to disable
426 * This function also removes requests which are currently processed ny the
427 * hardware and those which are not yet scheduled.
428 * Caller should take care of locking.
430 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
432 struct dwc3
*dwc
= dep
->dwc
;
435 dwc3_remove_requests(dwc
, dep
);
437 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
438 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
439 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
441 dep
->stream_capable
= false;
443 dep
->comp_desc
= NULL
;
450 /* -------------------------------------------------------------------------- */
452 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
453 const struct usb_endpoint_descriptor
*desc
)
458 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
463 /* -------------------------------------------------------------------------- */
465 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
466 const struct usb_endpoint_descriptor
*desc
)
473 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
474 pr_debug("dwc3: invalid parameters\n");
478 if (!desc
->wMaxPacketSize
) {
479 pr_debug("dwc3: missing wMaxPacketSize\n");
483 dep
= to_dwc3_ep(ep
);
486 switch (usb_endpoint_type(desc
)) {
487 case USB_ENDPOINT_XFER_CONTROL
:
488 strncat(dep
->name
, "-control", sizeof(dep
->name
));
490 case USB_ENDPOINT_XFER_ISOC
:
491 strncat(dep
->name
, "-isoc", sizeof(dep
->name
));
493 case USB_ENDPOINT_XFER_BULK
:
494 strncat(dep
->name
, "-bulk", sizeof(dep
->name
));
496 case USB_ENDPOINT_XFER_INT
:
497 strncat(dep
->name
, "-int", sizeof(dep
->name
));
500 dev_err(dwc
->dev
, "invalid endpoint transfer type\n");
503 if (dep
->flags
& DWC3_EP_ENABLED
) {
504 dev_WARN_ONCE(dwc
->dev
, true, "%s is already enabled\n",
509 dev_vdbg(dwc
->dev
, "Enabling %s\n", dep
->name
);
511 spin_lock_irqsave(&dwc
->lock
, flags
);
512 ret
= __dwc3_gadget_ep_enable(dep
, desc
, ep
->comp_desc
);
513 spin_unlock_irqrestore(&dwc
->lock
, flags
);
518 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
526 pr_debug("dwc3: invalid parameters\n");
530 dep
= to_dwc3_ep(ep
);
533 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
534 dev_WARN_ONCE(dwc
->dev
, true, "%s is already disabled\n",
539 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s",
541 (dep
->number
& 1) ? "in" : "out");
543 spin_lock_irqsave(&dwc
->lock
, flags
);
544 ret
= __dwc3_gadget_ep_disable(dep
);
545 spin_unlock_irqrestore(&dwc
->lock
, flags
);
550 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
553 struct dwc3_request
*req
;
554 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
555 struct dwc3
*dwc
= dep
->dwc
;
557 req
= kzalloc(sizeof(*req
), gfp_flags
);
559 dev_err(dwc
->dev
, "not enough memory\n");
563 req
->epnum
= dep
->number
;
565 req
->request
.dma
= DMA_ADDR_INVALID
;
567 return &req
->request
;
570 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
571 struct usb_request
*request
)
573 struct dwc3_request
*req
= to_dwc3_request(request
);
579 * dwc3_prepare_one_trb - setup one TRB from one request
580 * @dep: endpoint for which this request is prepared
581 * @req: dwc3_request pointer
583 static void dwc3_prepare_one_trb(struct dwc3_ep
*dep
,
584 struct dwc3_request
*req
, dma_addr_t dma
,
585 unsigned length
, unsigned last
, unsigned chain
)
587 struct dwc3
*dwc
= dep
->dwc
;
588 struct dwc3_trb_hw
*trb_hw
;
591 unsigned int cur_slot
;
593 dev_vdbg(dwc
->dev
, "%s: req %p dma %08llx length %d%s%s\n",
594 dep
->name
, req
, (unsigned long long) dma
,
595 length
, last
? " last" : "",
596 chain
? " chain" : "");
598 trb_hw
= &dep
->trb_pool
[dep
->free_slot
& DWC3_TRB_MASK
];
599 cur_slot
= dep
->free_slot
;
602 /* Skip the LINK-TRB on ISOC */
603 if (((cur_slot
& DWC3_TRB_MASK
) == DWC3_TRB_NUM
- 1) &&
604 usb_endpoint_xfer_isoc(dep
->desc
))
607 memset(&trb
, 0, sizeof(trb
));
609 dwc3_gadget_move_request_queued(req
);
611 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb_hw
);
614 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
622 if (usb_endpoint_xfer_bulk(dep
->desc
) && dep
->stream_capable
)
623 trb
.sid_sofn
= req
->request
.stream_id
;
625 switch (usb_endpoint_type(dep
->desc
)) {
626 case USB_ENDPOINT_XFER_CONTROL
:
627 trb
.trbctl
= DWC3_TRBCTL_CONTROL_SETUP
;
630 case USB_ENDPOINT_XFER_ISOC
:
631 trb
.trbctl
= DWC3_TRBCTL_ISOCHRONOUS_FIRST
;
633 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
634 if (!(cur_slot
% (DWC3_TRB_NUM
/ 4)))
638 case USB_ENDPOINT_XFER_BULK
:
639 case USB_ENDPOINT_XFER_INT
:
640 trb
.trbctl
= DWC3_TRBCTL_NORMAL
;
644 * This is only possible with faulty memory because we
645 * checked it already :)
654 dwc3_trb_to_hw(&trb
, trb_hw
);
658 * dwc3_prepare_trbs - setup TRBs from requests
659 * @dep: endpoint for which requests are being prepared
660 * @starting: true if the endpoint is idle and no requests are queued.
662 * The functions goes through the requests list and setups TRBs for the
663 * transfers. The functions returns once there are not more TRBs available or
664 * it run out of requests.
666 static void dwc3_prepare_trbs(struct dwc3_ep
*dep
, bool starting
)
668 struct dwc3_request
*req
, *n
;
670 unsigned int last_one
= 0;
672 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
674 /* the first request must not be queued */
675 trbs_left
= (dep
->busy_slot
- dep
->free_slot
) & DWC3_TRB_MASK
;
678 * if busy & slot are equal than it is either full or empty. If we are
679 * starting to proceed requests then we are empty. Otherwise we ar
680 * full and don't do anything
685 trbs_left
= DWC3_TRB_NUM
;
687 * In case we start from scratch, we queue the ISOC requests
688 * starting from slot 1. This is done because we use ring
689 * buffer and have no LST bit to stop us. Instead, we place
690 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
691 * after the first request so we start at slot 1 and have
692 * 7 requests proceed before we hit the first IOC.
693 * Other transfer types don't use the ring buffer and are
694 * processed from the first TRB until the last one. Since we
695 * don't wrap around we have to start at the beginning.
697 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
706 /* The last TRB is a link TRB, not used for xfer */
707 if ((trbs_left
<= 1) && usb_endpoint_xfer_isoc(dep
->desc
))
710 list_for_each_entry_safe(req
, n
, &dep
->request_list
, list
) {
714 if (req
->request
.num_mapped_sgs
> 0) {
715 struct usb_request
*request
= &req
->request
;
716 struct scatterlist
*sg
= request
->sg
;
717 struct scatterlist
*s
;
720 for_each_sg(sg
, s
, request
->num_mapped_sgs
, i
) {
721 unsigned chain
= true;
723 length
= sg_dma_len(s
);
724 dma
= sg_dma_address(s
);
726 if (i
== (request
->num_mapped_sgs
- 1)
739 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
746 dma
= req
->request
.dma
;
747 length
= req
->request
.length
;
753 /* Is this the last request? */
754 if (list_is_last(&req
->list
, &dep
->request_list
))
757 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
766 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
, u16 cmd_param
,
769 struct dwc3_gadget_ep_cmd_params params
;
770 struct dwc3_request
*req
;
771 struct dwc3
*dwc
= dep
->dwc
;
775 if (start_new
&& (dep
->flags
& DWC3_EP_BUSY
)) {
776 dev_vdbg(dwc
->dev
, "%s: endpoint busy\n", dep
->name
);
779 dep
->flags
&= ~DWC3_EP_PENDING_REQUEST
;
782 * If we are getting here after a short-out-packet we don't enqueue any
783 * new requests as we try to set the IOC bit only on the last request.
786 if (list_empty(&dep
->req_queued
))
787 dwc3_prepare_trbs(dep
, start_new
);
789 /* req points to the first request which will be sent */
790 req
= next_request(&dep
->req_queued
);
792 dwc3_prepare_trbs(dep
, start_new
);
795 * req points to the first request where HWO changed
798 req
= next_request(&dep
->req_queued
);
801 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
805 memset(¶ms
, 0, sizeof(params
));
806 params
.param0
= upper_32_bits(req
->trb_dma
);
807 params
.param1
= lower_32_bits(req
->trb_dma
);
810 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
812 cmd
= DWC3_DEPCMD_UPDATETRANSFER
;
814 cmd
|= DWC3_DEPCMD_PARAM(cmd_param
);
815 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
817 dev_dbg(dwc
->dev
, "failed to send STARTTRANSFER command\n");
820 * FIXME we need to iterate over the list of requests
821 * here and stop, unmap, free and del each of the linked
822 * requests instead of we do now.
824 dwc3_unmap_buffer_from_dma(req
);
825 list_del(&req
->list
);
829 dep
->flags
|= DWC3_EP_BUSY
;
830 dep
->res_trans_idx
= dwc3_gadget_ep_get_transfer_index(dwc
,
833 WARN_ON_ONCE(!dep
->res_trans_idx
);
838 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
840 req
->request
.actual
= 0;
841 req
->request
.status
= -EINPROGRESS
;
842 req
->direction
= dep
->direction
;
843 req
->epnum
= dep
->number
;
846 * We only add to our list of requests now and
847 * start consuming the list once we get XferNotReady
850 * That way, we avoid doing anything that we don't need
851 * to do now and defer it until the point we receive a
852 * particular token from the Host side.
854 * This will also avoid Host cancelling URBs due to too
857 dwc3_map_buffer_to_dma(req
);
858 list_add_tail(&req
->list
, &dep
->request_list
);
861 * There is one special case: XferNotReady with
862 * empty list of requests. We need to kick the
863 * transfer here in that situation, otherwise
864 * we will be NAKing forever.
866 * If we get XferNotReady before gadget driver
867 * has a chance to queue a request, we will ACK
868 * the IRQ but won't be able to receive the data
869 * until the next request is queued. The following
870 * code is handling exactly that.
872 if (dep
->flags
& DWC3_EP_PENDING_REQUEST
) {
877 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
878 dep
->flags
& DWC3_EP_BUSY
)
881 ret
= __dwc3_gadget_kick_transfer(dep
, 0, start_trans
);
882 if (ret
&& ret
!= -EBUSY
) {
883 struct dwc3
*dwc
= dep
->dwc
;
885 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
893 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
896 struct dwc3_request
*req
= to_dwc3_request(request
);
897 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
898 struct dwc3
*dwc
= dep
->dwc
;
905 dev_dbg(dwc
->dev
, "trying to queue request %p to disabled %s\n",
910 dev_vdbg(dwc
->dev
, "queing request %p to %s length %d\n",
911 request
, ep
->name
, request
->length
);
913 spin_lock_irqsave(&dwc
->lock
, flags
);
914 ret
= __dwc3_gadget_ep_queue(dep
, req
);
915 spin_unlock_irqrestore(&dwc
->lock
, flags
);
920 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
921 struct usb_request
*request
)
923 struct dwc3_request
*req
= to_dwc3_request(request
);
924 struct dwc3_request
*r
= NULL
;
926 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
927 struct dwc3
*dwc
= dep
->dwc
;
932 spin_lock_irqsave(&dwc
->lock
, flags
);
934 list_for_each_entry(r
, &dep
->request_list
, list
) {
940 list_for_each_entry(r
, &dep
->req_queued
, list
) {
945 /* wait until it is processed */
946 dwc3_stop_active_transfer(dwc
, dep
->number
);
949 dev_err(dwc
->dev
, "request %p was not queued to %s\n",
955 /* giveback the request */
956 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
959 spin_unlock_irqrestore(&dwc
->lock
, flags
);
964 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
)
966 struct dwc3_gadget_ep_cmd_params params
;
967 struct dwc3
*dwc
= dep
->dwc
;
970 memset(¶ms
, 0x00, sizeof(params
));
973 if (dep
->number
== 0 || dep
->number
== 1) {
975 * Whenever EP0 is stalled, we will restart
976 * the state machine, thus moving back to
979 dwc
->ep0state
= EP0_SETUP_PHASE
;
982 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
983 DWC3_DEPCMD_SETSTALL
, ¶ms
);
985 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
986 value
? "set" : "clear",
989 dep
->flags
|= DWC3_EP_STALL
;
991 if (dep
->flags
& DWC3_EP_WEDGE
)
994 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
995 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
997 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
998 value
? "set" : "clear",
1001 dep
->flags
&= ~DWC3_EP_STALL
;
1007 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
1009 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1010 struct dwc3
*dwc
= dep
->dwc
;
1012 unsigned long flags
;
1016 spin_lock_irqsave(&dwc
->lock
, flags
);
1018 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1019 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
1024 ret
= __dwc3_gadget_ep_set_halt(dep
, value
);
1026 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1031 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
1033 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1035 dep
->flags
|= DWC3_EP_WEDGE
;
1037 return dwc3_gadget_ep_set_halt(ep
, 1);
1040 /* -------------------------------------------------------------------------- */
1042 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
1043 .bLength
= USB_DT_ENDPOINT_SIZE
,
1044 .bDescriptorType
= USB_DT_ENDPOINT
,
1045 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1048 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
1049 .enable
= dwc3_gadget_ep0_enable
,
1050 .disable
= dwc3_gadget_ep0_disable
,
1051 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1052 .free_request
= dwc3_gadget_ep_free_request
,
1053 .queue
= dwc3_gadget_ep0_queue
,
1054 .dequeue
= dwc3_gadget_ep_dequeue
,
1055 .set_halt
= dwc3_gadget_ep_set_halt
,
1056 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1059 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
1060 .enable
= dwc3_gadget_ep_enable
,
1061 .disable
= dwc3_gadget_ep_disable
,
1062 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1063 .free_request
= dwc3_gadget_ep_free_request
,
1064 .queue
= dwc3_gadget_ep_queue
,
1065 .dequeue
= dwc3_gadget_ep_dequeue
,
1066 .set_halt
= dwc3_gadget_ep_set_halt
,
1067 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1070 /* -------------------------------------------------------------------------- */
1072 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
1074 struct dwc3
*dwc
= gadget_to_dwc(g
);
1077 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1078 return DWC3_DSTS_SOFFN(reg
);
1081 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
1083 struct dwc3
*dwc
= gadget_to_dwc(g
);
1085 unsigned long timeout
;
1086 unsigned long flags
;
1095 spin_lock_irqsave(&dwc
->lock
, flags
);
1098 * According to the Databook Remote wakeup request should
1099 * be issued only when the device is in early suspend state.
1101 * We can check that via USB Link State bits in DSTS register.
1103 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1105 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1106 if (speed
== DWC3_DSTS_SUPERSPEED
) {
1107 dev_dbg(dwc
->dev
, "no wakeup on SuperSpeed\n");
1112 link_state
= DWC3_DSTS_USBLNKST(reg
);
1114 switch (link_state
) {
1115 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1116 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1119 dev_dbg(dwc
->dev
, "can't wakeup from link state %d\n",
1125 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1128 * Switch link state to Recovery. In HS/FS/LS this means
1129 * RemoteWakeup Request
1131 reg
|= DWC3_DCTL_ULSTCHNG_RECOVERY
;
1132 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1134 /* wait for at least 2000us */
1135 usleep_range(2000, 2500);
1137 /* write zeroes to Link Change Request */
1138 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1139 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1141 /* pool until Link State change to ON */
1142 timeout
= jiffies
+ msecs_to_jiffies(100);
1144 while (!(time_after(jiffies
, timeout
))) {
1145 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1147 /* in HS, means ON */
1148 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1152 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1153 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1158 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1163 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1166 struct dwc3
*dwc
= gadget_to_dwc(g
);
1168 dwc
->is_selfpowered
= !!is_selfpowered
;
1173 static void dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
)
1178 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1180 reg
|= DWC3_DCTL_RUN_STOP
;
1182 reg
&= ~DWC3_DCTL_RUN_STOP
;
1184 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1187 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1189 if (!(reg
& DWC3_DSTS_DEVCTRLHLT
))
1192 if (reg
& DWC3_DSTS_DEVCTRLHLT
)
1201 dev_vdbg(dwc
->dev
, "gadget %s data soft-%s\n",
1203 ? dwc
->gadget_driver
->function
: "no-function",
1204 is_on
? "connect" : "disconnect");
1207 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1209 struct dwc3
*dwc
= gadget_to_dwc(g
);
1210 unsigned long flags
;
1214 spin_lock_irqsave(&dwc
->lock
, flags
);
1215 dwc3_gadget_run_stop(dwc
, is_on
);
1216 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1221 static int dwc3_gadget_start(struct usb_gadget
*g
,
1222 struct usb_gadget_driver
*driver
)
1224 struct dwc3
*dwc
= gadget_to_dwc(g
);
1225 struct dwc3_ep
*dep
;
1226 unsigned long flags
;
1230 spin_lock_irqsave(&dwc
->lock
, flags
);
1232 if (dwc
->gadget_driver
) {
1233 dev_err(dwc
->dev
, "%s is already bound to %s\n",
1235 dwc
->gadget_driver
->driver
.name
);
1240 dwc
->gadget_driver
= driver
;
1241 dwc
->gadget
.dev
.driver
= &driver
->driver
;
1243 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1244 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
1245 reg
|= dwc
->maximum_speed
;
1246 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1248 dwc
->start_config_issued
= false;
1250 /* Start with SuperSpeed Default */
1251 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1254 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1256 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1261 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1263 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1267 /* begin to receive SETUP packets */
1268 dwc
->ep0state
= EP0_SETUP_PHASE
;
1269 dwc3_ep0_out_start(dwc
);
1271 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1276 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1279 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1284 static int dwc3_gadget_stop(struct usb_gadget
*g
,
1285 struct usb_gadget_driver
*driver
)
1287 struct dwc3
*dwc
= gadget_to_dwc(g
);
1288 unsigned long flags
;
1290 spin_lock_irqsave(&dwc
->lock
, flags
);
1292 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1293 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
1295 dwc
->gadget_driver
= NULL
;
1296 dwc
->gadget
.dev
.driver
= NULL
;
1298 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1302 static const struct usb_gadget_ops dwc3_gadget_ops
= {
1303 .get_frame
= dwc3_gadget_get_frame
,
1304 .wakeup
= dwc3_gadget_wakeup
,
1305 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
1306 .pullup
= dwc3_gadget_pullup
,
1307 .udc_start
= dwc3_gadget_start
,
1308 .udc_stop
= dwc3_gadget_stop
,
1311 /* -------------------------------------------------------------------------- */
1313 static int __devinit
dwc3_gadget_init_endpoints(struct dwc3
*dwc
)
1315 struct dwc3_ep
*dep
;
1318 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
1320 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1321 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
1323 dev_err(dwc
->dev
, "can't allocate endpoint %d\n",
1329 dep
->number
= epnum
;
1330 dwc
->eps
[epnum
] = dep
;
1332 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s", epnum
>> 1,
1333 (epnum
& 1) ? "in" : "out");
1334 dep
->endpoint
.name
= dep
->name
;
1335 dep
->direction
= (epnum
& 1);
1337 if (epnum
== 0 || epnum
== 1) {
1338 dep
->endpoint
.maxpacket
= 512;
1339 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
1341 dwc
->gadget
.ep0
= &dep
->endpoint
;
1345 dep
->endpoint
.maxpacket
= 1024;
1346 dep
->endpoint
.max_streams
= 15;
1347 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
1348 list_add_tail(&dep
->endpoint
.ep_list
,
1349 &dwc
->gadget
.ep_list
);
1351 ret
= dwc3_alloc_trb_pool(dep
);
1356 INIT_LIST_HEAD(&dep
->request_list
);
1357 INIT_LIST_HEAD(&dep
->req_queued
);
1363 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
1365 struct dwc3_ep
*dep
;
1368 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1369 dep
= dwc
->eps
[epnum
];
1370 dwc3_free_trb_pool(dep
);
1372 if (epnum
!= 0 && epnum
!= 1)
1373 list_del(&dep
->endpoint
.ep_list
);
1379 static void dwc3_gadget_release(struct device
*dev
)
1381 dev_dbg(dev
, "%s\n", __func__
);
1384 /* -------------------------------------------------------------------------- */
1385 static int dwc3_cleanup_done_reqs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
1386 const struct dwc3_event_depevt
*event
, int status
)
1388 struct dwc3_request
*req
;
1389 struct dwc3_trb trb
;
1391 unsigned int s_pkt
= 0;
1394 req
= next_request(&dep
->req_queued
);
1400 dwc3_trb_to_nat(req
->trb
, &trb
);
1402 if (trb
.hwo
&& status
!= -ESHUTDOWN
)
1404 * We continue despite the error. There is not much we
1405 * can do. If we don't clean in up we loop for ever. If
1406 * we skip the TRB than it gets overwritten reused after
1407 * a while since we use them in a ring buffer. a BUG()
1408 * would help. Lets hope that if this occures, someone
1409 * fixes the root cause instead of looking away :)
1411 dev_err(dwc
->dev
, "%s's TRB (%p) still owned by HW\n",
1412 dep
->name
, req
->trb
);
1415 if (dep
->direction
) {
1417 dev_err(dwc
->dev
, "incomplete IN transfer %s\n",
1419 status
= -ECONNRESET
;
1422 if (count
&& (event
->status
& DEPEVT_STATUS_SHORT
))
1427 * We assume here we will always receive the entire data block
1428 * which we should receive. Meaning, if we program RX to
1429 * receive 4K but we receive only 2K, we assume that's all we
1430 * should receive and we simply bounce the request back to the
1431 * gadget driver for further processing.
1433 req
->request
.actual
+= req
->request
.length
- count
;
1434 dwc3_gadget_giveback(dep
, req
, status
);
1437 if ((event
->status
& DEPEVT_STATUS_LST
) && trb
.lst
)
1439 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1443 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1448 static void dwc3_endpoint_transfer_complete(struct dwc3
*dwc
,
1449 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
,
1452 unsigned status
= 0;
1455 if (event
->status
& DEPEVT_STATUS_BUSERR
)
1456 status
= -ECONNRESET
;
1458 clean_busy
= dwc3_cleanup_done_reqs(dwc
, dep
, event
, status
);
1460 dep
->flags
&= ~DWC3_EP_BUSY
;
1461 dep
->res_trans_idx
= 0;
1465 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1466 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1468 if (dwc
->revision
< DWC3_REVISION_183A
) {
1472 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
1473 struct dwc3_ep
*dep
= dwc
->eps
[i
];
1475 if (!(dep
->flags
& DWC3_EP_ENABLED
))
1478 if (!list_empty(&dep
->req_queued
))
1482 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1484 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1490 static void dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1491 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
1495 if (list_empty(&dep
->request_list
)) {
1496 dev_vdbg(dwc
->dev
, "ISOC ep %s run out for requests.\n",
1501 if (event
->parameters
) {
1504 mask
= ~(dep
->interval
- 1);
1505 uf
= event
->parameters
& mask
;
1506 /* 4 micro frames in the future */
1507 uf
+= dep
->interval
* 4;
1512 __dwc3_gadget_kick_transfer(dep
, uf
, 1);
1515 static void dwc3_process_ep_cmd_complete(struct dwc3_ep
*dep
,
1516 const struct dwc3_event_depevt
*event
)
1518 struct dwc3
*dwc
= dep
->dwc
;
1519 struct dwc3_event_depevt mod_ev
= *event
;
1522 * We were asked to remove one requests. It is possible that this
1523 * request and a few other were started together and have the same
1524 * transfer index. Since we stopped the complete endpoint we don't
1525 * know how many requests were already completed (and not yet)
1526 * reported and how could be done (later). We purge them all until
1527 * the end of the list.
1529 mod_ev
.status
= DEPEVT_STATUS_LST
;
1530 dwc3_cleanup_done_reqs(dwc
, dep
, &mod_ev
, -ESHUTDOWN
);
1531 dep
->flags
&= ~DWC3_EP_BUSY
;
1532 /* pending requets are ignored and are queued on XferNotReady */
1535 static void dwc3_ep_cmd_compl(struct dwc3_ep
*dep
,
1536 const struct dwc3_event_depevt
*event
)
1538 u32 param
= event
->parameters
;
1539 u32 cmd_type
= (param
>> 8) & ((1 << 5) - 1);
1542 case DWC3_DEPCMD_ENDTRANSFER
:
1543 dwc3_process_ep_cmd_complete(dep
, event
);
1545 case DWC3_DEPCMD_STARTTRANSFER
:
1546 dep
->res_trans_idx
= param
& 0x7f;
1549 printk(KERN_ERR
"%s() unknown /unexpected type: %d\n",
1550 __func__
, cmd_type
);
1555 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
1556 const struct dwc3_event_depevt
*event
)
1558 struct dwc3_ep
*dep
;
1559 u8 epnum
= event
->endpoint_number
;
1561 dep
= dwc
->eps
[epnum
];
1563 dev_vdbg(dwc
->dev
, "%s: %s\n", dep
->name
,
1564 dwc3_ep_event_string(event
->endpoint_event
));
1566 if (epnum
== 0 || epnum
== 1) {
1567 dwc3_ep0_interrupt(dwc
, event
);
1571 switch (event
->endpoint_event
) {
1572 case DWC3_DEPEVT_XFERCOMPLETE
:
1573 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1574 dev_dbg(dwc
->dev
, "%s is an Isochronous endpoint\n",
1579 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 1);
1581 case DWC3_DEPEVT_XFERINPROGRESS
:
1582 if (!usb_endpoint_xfer_isoc(dep
->desc
)) {
1583 dev_dbg(dwc
->dev
, "%s is not an Isochronous endpoint\n",
1588 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 0);
1590 case DWC3_DEPEVT_XFERNOTREADY
:
1591 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1592 dwc3_gadget_start_isoc(dwc
, dep
, event
);
1596 dev_vdbg(dwc
->dev
, "%s: reason %s\n",
1597 dep
->name
, event
->status
1599 : "Transfer Not Active");
1601 ret
= __dwc3_gadget_kick_transfer(dep
, 0, 1);
1602 if (!ret
|| ret
== -EBUSY
)
1605 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
1610 case DWC3_DEPEVT_STREAMEVT
:
1611 if (!usb_endpoint_xfer_bulk(dep
->desc
)) {
1612 dev_err(dwc
->dev
, "Stream event for non-Bulk %s\n",
1617 switch (event
->status
) {
1618 case DEPEVT_STREAMEVT_FOUND
:
1619 dev_vdbg(dwc
->dev
, "Stream %d found and started\n",
1623 case DEPEVT_STREAMEVT_NOTFOUND
:
1626 dev_dbg(dwc
->dev
, "Couldn't find suitable stream\n");
1629 case DWC3_DEPEVT_RXTXFIFOEVT
:
1630 dev_dbg(dwc
->dev
, "%s FIFO Overrun\n", dep
->name
);
1632 case DWC3_DEPEVT_EPCMDCMPLT
:
1633 dwc3_ep_cmd_compl(dep
, event
);
1638 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
1640 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
1641 spin_unlock(&dwc
->lock
);
1642 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
1643 spin_lock(&dwc
->lock
);
1647 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
)
1649 struct dwc3_ep
*dep
;
1650 struct dwc3_gadget_ep_cmd_params params
;
1654 dep
= dwc
->eps
[epnum
];
1656 WARN_ON(!dep
->res_trans_idx
);
1657 if (dep
->res_trans_idx
) {
1658 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
1659 cmd
|= DWC3_DEPCMD_HIPRI_FORCERM
| DWC3_DEPCMD_CMDIOC
;
1660 cmd
|= DWC3_DEPCMD_PARAM(dep
->res_trans_idx
);
1661 memset(¶ms
, 0, sizeof(params
));
1662 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
1664 dep
->res_trans_idx
= 0;
1668 static void dwc3_stop_active_transfers(struct dwc3
*dwc
)
1672 for (epnum
= 2; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1673 struct dwc3_ep
*dep
;
1675 dep
= dwc
->eps
[epnum
];
1676 if (!(dep
->flags
& DWC3_EP_ENABLED
))
1679 dwc3_remove_requests(dwc
, dep
);
1683 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
1687 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1688 struct dwc3_ep
*dep
;
1689 struct dwc3_gadget_ep_cmd_params params
;
1692 dep
= dwc
->eps
[epnum
];
1694 if (!(dep
->flags
& DWC3_EP_STALL
))
1697 dep
->flags
&= ~DWC3_EP_STALL
;
1699 memset(¶ms
, 0, sizeof(params
));
1700 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
1701 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
1706 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
1708 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1711 U1
/U2 is powersave optimization
. Skip it
for now
. Anyway we need to
1712 enable it before we can disable it
.
1714 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1715 reg
&= ~DWC3_DCTL_INITU1ENA
;
1716 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1718 reg
&= ~DWC3_DCTL_INITU2ENA
;
1719 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1722 dwc3_stop_active_transfers(dwc
);
1723 dwc3_disconnect_gadget(dwc
);
1724 dwc
->start_config_issued
= false;
1726 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1727 dwc
->setup_packet_pending
= false;
1730 static void dwc3_gadget_usb3_phy_power(struct dwc3
*dwc
, int on
)
1734 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(0));
1737 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
1739 reg
|= DWC3_GUSB3PIPECTL_SUSPHY
;
1741 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(0), reg
);
1744 static void dwc3_gadget_usb2_phy_power(struct dwc3
*dwc
, int on
)
1748 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
1751 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
1753 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
1755 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
1758 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
1762 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1765 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1766 * would cause a missing Disconnect Event if there's a
1767 * pending Setup Packet in the FIFO.
1769 * There's no suggested workaround on the official Bug
1770 * report, which states that "unless the driver/application
1771 * is doing any special handling of a disconnect event,
1772 * there is no functional issue".
1774 * Unfortunately, it turns out that we _do_ some special
1775 * handling of a disconnect event, namely complete all
1776 * pending transfers, notify gadget driver of the
1777 * disconnection, and so on.
1779 * Our suggested workaround is to follow the Disconnect
1780 * Event steps here, instead, based on a setup_packet_pending
1781 * flag. Such flag gets set whenever we have a XferNotReady
1782 * event on EP0 and gets cleared on XferComplete for the
1787 * STAR#9000466709: RTL: Device : Disconnect event not
1788 * generated if setup packet pending in FIFO
1790 if (dwc
->revision
< DWC3_REVISION_188A
) {
1791 if (dwc
->setup_packet_pending
)
1792 dwc3_gadget_disconnect_interrupt(dwc
);
1795 /* after reset -> Default State */
1796 dwc
->dev_state
= DWC3_DEFAULT_STATE
;
1799 dwc3_gadget_usb2_phy_power(dwc
, true);
1800 dwc3_gadget_usb3_phy_power(dwc
, true);
1802 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
1803 dwc3_disconnect_gadget(dwc
);
1805 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1806 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
1807 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1809 dwc3_stop_active_transfers(dwc
);
1810 dwc3_clear_stall_all_ep(dwc
);
1811 dwc
->start_config_issued
= false;
1813 /* Reset device address to zero */
1814 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1815 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
1816 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1819 static void dwc3_update_ram_clk_sel(struct dwc3
*dwc
, u32 speed
)
1822 u32 usb30_clock
= DWC3_GCTL_CLK_BUS
;
1825 * We change the clock only at SS but I dunno why I would want to do
1826 * this. Maybe it becomes part of the power saving plan.
1829 if (speed
!= DWC3_DSTS_SUPERSPEED
)
1833 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1834 * each time on Connect Done.
1839 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1840 reg
|= DWC3_GCTL_RAMCLKSEL(usb30_clock
);
1841 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1844 static void dwc3_gadget_disable_phy(struct dwc3
*dwc
, u8 speed
)
1847 case USB_SPEED_SUPER
:
1848 dwc3_gadget_usb2_phy_power(dwc
, false);
1850 case USB_SPEED_HIGH
:
1851 case USB_SPEED_FULL
:
1853 dwc3_gadget_usb3_phy_power(dwc
, false);
1858 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
1860 struct dwc3_gadget_ep_cmd_params params
;
1861 struct dwc3_ep
*dep
;
1866 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1868 memset(¶ms
, 0x00, sizeof(params
));
1870 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1871 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1874 dwc3_update_ram_clk_sel(dwc
, speed
);
1877 case DWC3_DCFG_SUPERSPEED
:
1879 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1880 * would cause a missing USB3 Reset event.
1882 * In such situations, we should force a USB3 Reset
1883 * event by calling our dwc3_gadget_reset_interrupt()
1888 * STAR#9000483510: RTL: SS : USB3 reset event may
1889 * not be generated always when the link enters poll
1891 if (dwc
->revision
< DWC3_REVISION_190A
)
1892 dwc3_gadget_reset_interrupt(dwc
);
1894 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1895 dwc
->gadget
.ep0
->maxpacket
= 512;
1896 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
1898 case DWC3_DCFG_HIGHSPEED
:
1899 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1900 dwc
->gadget
.ep0
->maxpacket
= 64;
1901 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
1903 case DWC3_DCFG_FULLSPEED2
:
1904 case DWC3_DCFG_FULLSPEED1
:
1905 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1906 dwc
->gadget
.ep0
->maxpacket
= 64;
1907 dwc
->gadget
.speed
= USB_SPEED_FULL
;
1909 case DWC3_DCFG_LOWSPEED
:
1910 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
1911 dwc
->gadget
.ep0
->maxpacket
= 8;
1912 dwc
->gadget
.speed
= USB_SPEED_LOW
;
1916 /* Disable unneded PHY */
1917 dwc3_gadget_disable_phy(dwc
, dwc
->gadget
.speed
);
1920 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1922 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1927 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
);
1929 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1934 * Configure PHY via GUSB3PIPECTLn if required.
1936 * Update GTXFIFOSIZn
1938 * In both cases reset values should be sufficient.
1942 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
1944 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1947 * TODO take core out of low power mode when that's
1951 dwc
->gadget_driver
->resume(&dwc
->gadget
);
1954 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
1955 unsigned int evtinfo
)
1957 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
1960 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
1961 * on the link partner, the USB session might do multiple entry/exit
1962 * of low power states before a transfer takes place.
1964 * Due to this problem, we might experience lower throughput. The
1965 * suggested workaround is to disable DCTL[12:9] bits if we're
1966 * transitioning from U1/U2 to U0 and enable those bits again
1967 * after a transfer completes and there are no pending transfers
1968 * on any of the enabled endpoints.
1970 * This is the first half of that workaround.
1974 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
1975 * core send LGO_Ux entering U0
1977 if (dwc
->revision
< DWC3_REVISION_183A
) {
1978 if (next
== DWC3_LINK_STATE_U0
) {
1982 switch (dwc
->link_state
) {
1983 case DWC3_LINK_STATE_U1
:
1984 case DWC3_LINK_STATE_U2
:
1985 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1986 u1u2
= reg
& (DWC3_DCTL_INITU2ENA
1987 | DWC3_DCTL_ACCEPTU2ENA
1988 | DWC3_DCTL_INITU1ENA
1989 | DWC3_DCTL_ACCEPTU1ENA
);
1992 dwc
->u1u2
= reg
& u1u2
;
1996 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2005 dwc
->link_state
= next
;
2007 dev_vdbg(dwc
->dev
, "%s link %d\n", __func__
, dwc
->link_state
);
2010 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
2011 const struct dwc3_event_devt
*event
)
2013 switch (event
->type
) {
2014 case DWC3_DEVICE_EVENT_DISCONNECT
:
2015 dwc3_gadget_disconnect_interrupt(dwc
);
2017 case DWC3_DEVICE_EVENT_RESET
:
2018 dwc3_gadget_reset_interrupt(dwc
);
2020 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
2021 dwc3_gadget_conndone_interrupt(dwc
);
2023 case DWC3_DEVICE_EVENT_WAKEUP
:
2024 dwc3_gadget_wakeup_interrupt(dwc
);
2026 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
2027 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
2029 case DWC3_DEVICE_EVENT_EOPF
:
2030 dev_vdbg(dwc
->dev
, "End of Periodic Frame\n");
2032 case DWC3_DEVICE_EVENT_SOF
:
2033 dev_vdbg(dwc
->dev
, "Start of Periodic Frame\n");
2035 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
2036 dev_vdbg(dwc
->dev
, "Erratic Error\n");
2038 case DWC3_DEVICE_EVENT_CMD_CMPL
:
2039 dev_vdbg(dwc
->dev
, "Command Complete\n");
2041 case DWC3_DEVICE_EVENT_OVERFLOW
:
2042 dev_vdbg(dwc
->dev
, "Overflow\n");
2045 dev_dbg(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
2049 static void dwc3_process_event_entry(struct dwc3
*dwc
,
2050 const union dwc3_event
*event
)
2052 /* Endpoint IRQ, handle it and return early */
2053 if (event
->type
.is_devspec
== 0) {
2055 return dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
2058 switch (event
->type
.type
) {
2059 case DWC3_EVENT_TYPE_DEV
:
2060 dwc3_gadget_interrupt(dwc
, &event
->devt
);
2062 /* REVISIT what to do with Carkit and I2C events ? */
2064 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
2068 static irqreturn_t
dwc3_process_event_buf(struct dwc3
*dwc
, u32 buf
)
2070 struct dwc3_event_buffer
*evt
;
2074 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(buf
));
2075 count
&= DWC3_GEVNTCOUNT_MASK
;
2079 evt
= dwc
->ev_buffs
[buf
];
2083 union dwc3_event event
;
2085 memcpy(&event
.raw
, (evt
->buf
+ evt
->lpos
), sizeof(event
.raw
));
2086 dwc3_process_event_entry(dwc
, &event
);
2088 * XXX we wrap around correctly to the next entry as almost all
2089 * entries are 4 bytes in size. There is one entry which has 12
2090 * bytes which is a regular entry followed by 8 bytes data. ATM
2091 * I don't know how things are organized if were get next to the
2092 * a boundary so I worry about that once we try to handle that.
2094 evt
->lpos
= (evt
->lpos
+ 4) % DWC3_EVENT_BUFFERS_SIZE
;
2097 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(buf
), 4);
2103 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
)
2105 struct dwc3
*dwc
= _dwc
;
2107 irqreturn_t ret
= IRQ_NONE
;
2109 spin_lock(&dwc
->lock
);
2111 for (i
= 0; i
< dwc
->num_event_buffers
; i
++) {
2114 status
= dwc3_process_event_buf(dwc
, i
);
2115 if (status
== IRQ_HANDLED
)
2119 spin_unlock(&dwc
->lock
);
2125 * dwc3_gadget_init - Initializes gadget related registers
2126 * @dwc: Pointer to out controller context structure
2128 * Returns 0 on success otherwise negative errno.
2130 int __devinit
dwc3_gadget_init(struct dwc3
*dwc
)
2136 dwc
->ctrl_req
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2137 &dwc
->ctrl_req_addr
, GFP_KERNEL
);
2138 if (!dwc
->ctrl_req
) {
2139 dev_err(dwc
->dev
, "failed to allocate ctrl request\n");
2144 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2145 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
2146 if (!dwc
->ep0_trb
) {
2147 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
2152 dwc
->setup_buf
= dma_alloc_coherent(dwc
->dev
,
2153 sizeof(*dwc
->setup_buf
) * 2,
2154 &dwc
->setup_buf_addr
, GFP_KERNEL
);
2155 if (!dwc
->setup_buf
) {
2156 dev_err(dwc
->dev
, "failed to allocate setup buffer\n");
2161 dwc
->ep0_bounce
= dma_alloc_coherent(dwc
->dev
,
2162 512, &dwc
->ep0_bounce_addr
, GFP_KERNEL
);
2163 if (!dwc
->ep0_bounce
) {
2164 dev_err(dwc
->dev
, "failed to allocate ep0 bounce buffer\n");
2169 dev_set_name(&dwc
->gadget
.dev
, "gadget");
2171 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
2172 dwc
->gadget
.max_speed
= USB_SPEED_SUPER
;
2173 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2174 dwc
->gadget
.dev
.parent
= dwc
->dev
;
2175 dwc
->gadget
.sg_supported
= true;
2177 dma_set_coherent_mask(&dwc
->gadget
.dev
, dwc
->dev
->coherent_dma_mask
);
2179 dwc
->gadget
.dev
.dma_parms
= dwc
->dev
->dma_parms
;
2180 dwc
->gadget
.dev
.dma_mask
= dwc
->dev
->dma_mask
;
2181 dwc
->gadget
.dev
.release
= dwc3_gadget_release
;
2182 dwc
->gadget
.name
= "dwc3-gadget";
2185 * REVISIT: Here we should clear all pending IRQs to be
2186 * sure we're starting from a well known location.
2189 ret
= dwc3_gadget_init_endpoints(dwc
);
2193 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
2195 ret
= request_irq(irq
, dwc3_interrupt
, IRQF_SHARED
,
2198 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
2203 /* Enable all but Start and End of Frame IRQs */
2204 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
2205 DWC3_DEVTEN_EVNTOVERFLOWEN
|
2206 DWC3_DEVTEN_CMDCMPLTEN
|
2207 DWC3_DEVTEN_ERRTICERREN
|
2208 DWC3_DEVTEN_WKUPEVTEN
|
2209 DWC3_DEVTEN_ULSTCNGEN
|
2210 DWC3_DEVTEN_CONNECTDONEEN
|
2211 DWC3_DEVTEN_USBRSTEN
|
2212 DWC3_DEVTEN_DISCONNEVTEN
);
2213 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
2215 ret
= device_register(&dwc
->gadget
.dev
);
2217 dev_err(dwc
->dev
, "failed to register gadget device\n");
2218 put_device(&dwc
->gadget
.dev
);
2222 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
2224 dev_err(dwc
->dev
, "failed to register udc\n");
2231 device_unregister(&dwc
->gadget
.dev
);
2234 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2238 dwc3_gadget_free_endpoints(dwc
);
2241 dma_free_coherent(dwc
->dev
, 512, dwc
->ep0_bounce
,
2242 dwc
->ep0_bounce_addr
);
2245 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2246 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2249 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2250 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2253 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2254 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2260 void dwc3_gadget_exit(struct dwc3
*dwc
)
2264 usb_del_gadget_udc(&dwc
->gadget
);
2265 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
2267 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2270 dwc3_gadget_free_endpoints(dwc
);
2272 dma_free_coherent(dwc
->dev
, 512, dwc
->ep0_bounce
,
2273 dwc
->ep0_bounce_addr
);
2275 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2276 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2278 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2279 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2281 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2282 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2284 device_unregister(&dwc
->gadget
.dev
);