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.dma == DMA_ADDR_INVALID) {
69 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
70 req->request.length, req->direction
71 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
76 void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
78 struct dwc3 *dwc = req->dep->dwc;
80 if (req->request.length == 0) {
81 req->request.dma = DMA_ADDR_INVALID;
86 dma_unmap_single(dwc->dev, req->request.dma,
87 req->request.length, req->direction
88 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
90 req->request.dma = DMA_ADDR_INVALID;
94 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
97 struct dwc3 *dwc = dep->dwc;
102 * Skip LINK TRB. We can't use req->trb and check for
103 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
104 * completed (not the LINK TRB).
106 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
107 usb_endpoint_xfer_isoc(dep->desc))
110 list_del(&req->list);
112 if (req->request.status == -EINPROGRESS)
113 req->request.status = status;
115 dwc3_unmap_buffer_from_dma(req);
117 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
118 req, dep->name, req->request.actual,
119 req->request.length, status);
121 spin_unlock(&dwc->lock);
122 req->request.complete(&req->dep->endpoint, &req->request);
123 spin_lock(&dwc->lock);
126 static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
129 case DWC3_DEPCMD_DEPSTARTCFG:
130 return "Start New Configuration";
131 case DWC3_DEPCMD_ENDTRANSFER:
132 return "End Transfer";
133 case DWC3_DEPCMD_UPDATETRANSFER:
134 return "Update Transfer";
135 case DWC3_DEPCMD_STARTTRANSFER:
136 return "Start Transfer";
137 case DWC3_DEPCMD_CLEARSTALL:
138 return "Clear Stall";
139 case DWC3_DEPCMD_SETSTALL:
141 case DWC3_DEPCMD_GETSEQNUMBER:
142 return "Get Data Sequence Number";
143 case DWC3_DEPCMD_SETTRANSFRESOURCE:
144 return "Set Endpoint Transfer Resource";
145 case DWC3_DEPCMD_SETEPCONFIG:
146 return "Set Endpoint Configuration";
148 return "UNKNOWN command";
152 int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
153 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
155 struct dwc3_ep *dep = dwc->eps[ep];
159 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
161 dwc3_gadget_ep_cmd_string(cmd), params->param0,
162 params->param1, params->param2);
164 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
165 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
166 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
168 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
170 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
171 if (!(reg & DWC3_DEPCMD_CMDACT)) {
172 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
173 DWC3_DEPCMD_STATUS(reg));
174 if (DWC3_DEPCMD_STATUS(reg))
180 * We can't sleep here, because it is also called from
191 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
192 struct dwc3_trb_hw *trb)
194 u32 offset = (char *) trb - (char *) dep->trb_pool;
196 return dep->trb_pool_dma + offset;
199 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
201 struct dwc3 *dwc = dep->dwc;
206 if (dep->number == 0 || dep->number == 1)
209 dep->trb_pool = dma_alloc_coherent(dwc->dev,
210 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
211 &dep->trb_pool_dma, GFP_KERNEL);
212 if (!dep->trb_pool) {
213 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
221 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
223 struct dwc3 *dwc = dep->dwc;
225 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
226 dep->trb_pool, dep->trb_pool_dma);
228 dep->trb_pool = NULL;
229 dep->trb_pool_dma = 0;
232 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
235 * dwc3_gadget_start_config - Configure EP resources
236 * @dwc: pointer to our controller context structure
237 * @dep: endpoint that is being enabled
239 * The assignment of transfer resources cannot perfectly follow the
240 * data book due to the fact that the controller driver does not have
241 * all knowledge of the configuration in advance. It is given this
242 * information piecemeal by the composite gadget framework after every
243 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
244 * programming model in this scenario can cause errors. For two
247 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
248 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
249 * multiple interfaces.
251 * 2) The databook does not mention doing more DEPXFERCFG for new
252 * endpoint on alt setting (8.1.6).
254 * The following simplified method is used instead:
256 * All hardware endpoints can be assigned a transfer resource and this
257 * setting will stay persistent until either a core reset or
258 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
259 * do DEPXFERCFG for every hardware endpoint as well. We are
260 * guaranteed that there are as many transfer resources as endpoints.
262 * This function is called for each endpoint when it is being enabled
263 * but is triggered only when called for EP0-out, which always happens
264 * first, and which should only happen in one of the above conditions.
266 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
268 struct dwc3_gadget_ep_cmd_params params;
276 memset(¶ms, 0x00, sizeof(params));
277 cmd = DWC3_DEPCMD_DEPSTARTCFG;
279 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms);
283 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
284 struct dwc3_ep *dep = dwc->eps[i];
289 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
297 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
298 const struct usb_endpoint_descriptor *desc)
300 struct dwc3_gadget_ep_cmd_params params;
302 memset(¶ms, 0x00, sizeof(params));
304 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
305 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
306 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
308 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
309 | DWC3_DEPCFG_XFER_NOT_READY_EN;
311 if (usb_endpoint_xfer_bulk(desc) && dep->endpoint.max_streams) {
312 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
313 | DWC3_DEPCFG_STREAM_EVENT_EN;
314 dep->stream_capable = true;
317 if (usb_endpoint_xfer_isoc(desc))
318 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
321 * We are doing 1:1 mapping for endpoints, meaning
322 * Physical Endpoints 2 maps to Logical Endpoint 2 and
323 * so on. We consider the direction bit as part of the physical
324 * endpoint number. So USB endpoint 0x81 is 0x03.
326 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
329 * We must use the lower 16 TX FIFOs even though
333 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
335 if (desc->bInterval) {
336 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
337 dep->interval = 1 << (desc->bInterval - 1);
340 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
341 DWC3_DEPCMD_SETEPCONFIG, ¶ms);
344 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
346 struct dwc3_gadget_ep_cmd_params params;
348 memset(¶ms, 0x00, sizeof(params));
350 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
352 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
353 DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms);
357 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
358 * @dep: endpoint to be initialized
359 * @desc: USB Endpoint Descriptor
361 * Caller should take care of locking
363 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
364 const struct usb_endpoint_descriptor *desc)
366 struct dwc3 *dwc = dep->dwc;
370 if (!(dep->flags & DWC3_EP_ENABLED)) {
371 ret = dwc3_gadget_start_config(dwc, dep);
376 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
380 if (!(dep->flags & DWC3_EP_ENABLED)) {
381 struct dwc3_trb_hw *trb_st_hw;
382 struct dwc3_trb_hw *trb_link_hw;
383 struct dwc3_trb trb_link;
386 dep->type = usb_endpoint_type(desc);
387 dep->flags |= DWC3_EP_ENABLED;
389 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
390 reg |= DWC3_DALEPENA_EP(dep->number);
391 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
393 if (!usb_endpoint_xfer_isoc(desc))
396 memset(&trb_link, 0, sizeof(trb_link));
398 /* Link TRB for ISOC. The HWO but is never reset */
399 trb_st_hw = &dep->trb_pool[0];
401 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
402 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
405 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
406 dwc3_trb_to_hw(&trb_link, trb_link_hw);
412 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
413 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
415 struct dwc3_request *req;
417 if (!list_empty(&dep->req_queued))
418 dwc3_stop_active_transfer(dwc, dep->number);
420 while (!list_empty(&dep->request_list)) {
421 req = next_request(&dep->request_list);
423 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
428 * __dwc3_gadget_ep_disable - Disables a HW endpoint
429 * @dep: the endpoint to disable
431 * This function also removes requests which are currently processed ny the
432 * hardware and those which are not yet scheduled.
433 * Caller should take care of locking.
435 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
437 struct dwc3 *dwc = dep->dwc;
440 dwc3_remove_requests(dwc, dep);
442 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
443 reg &= ~DWC3_DALEPENA_EP(dep->number);
444 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
446 dep->stream_capable = false;
454 /* -------------------------------------------------------------------------- */
456 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
457 const struct usb_endpoint_descriptor *desc)
462 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
467 /* -------------------------------------------------------------------------- */
469 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
470 const struct usb_endpoint_descriptor *desc)
477 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
478 pr_debug("dwc3: invalid parameters\n");
482 if (!desc->wMaxPacketSize) {
483 pr_debug("dwc3: missing wMaxPacketSize\n");
487 dep = to_dwc3_ep(ep);
490 switch (usb_endpoint_type(desc)) {
491 case USB_ENDPOINT_XFER_CONTROL:
492 strlcat(dep->name, "-control", sizeof(dep->name));
494 case USB_ENDPOINT_XFER_ISOC:
495 strlcat(dep->name, "-isoc", sizeof(dep->name));
497 case USB_ENDPOINT_XFER_BULK:
498 strlcat(dep->name, "-bulk", sizeof(dep->name));
500 case USB_ENDPOINT_XFER_INT:
501 strlcat(dep->name, "-int", sizeof(dep->name));
504 dev_err(dwc->dev, "invalid endpoint transfer type\n");
507 if (dep->flags & DWC3_EP_ENABLED) {
508 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
513 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
515 spin_lock_irqsave(&dwc->lock, flags);
516 ret = __dwc3_gadget_ep_enable(dep, desc);
517 spin_unlock_irqrestore(&dwc->lock, flags);
522 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
530 pr_debug("dwc3: invalid parameters\n");
534 dep = to_dwc3_ep(ep);
537 if (!(dep->flags & DWC3_EP_ENABLED)) {
538 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
543 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
545 (dep->number & 1) ? "in" : "out");
547 spin_lock_irqsave(&dwc->lock, flags);
548 ret = __dwc3_gadget_ep_disable(dep);
549 spin_unlock_irqrestore(&dwc->lock, flags);
554 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
557 struct dwc3_request *req;
558 struct dwc3_ep *dep = to_dwc3_ep(ep);
559 struct dwc3 *dwc = dep->dwc;
561 req = kzalloc(sizeof(*req), gfp_flags);
563 dev_err(dwc->dev, "not enough memory\n");
567 req->epnum = dep->number;
569 req->request.dma = DMA_ADDR_INVALID;
571 return &req->request;
574 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
575 struct usb_request *request)
577 struct dwc3_request *req = to_dwc3_request(request);
583 * dwc3_prepare_trbs - setup TRBs from requests
584 * @dep: endpoint for which requests are being prepared
585 * @starting: true if the endpoint is idle and no requests are queued.
587 * The functions goes through the requests list and setups TRBs for the
588 * transfers. The functions returns once there are not more TRBs available or
589 * it run out of requests.
591 static struct dwc3_request *dwc3_prepare_trbs(struct dwc3_ep *dep,
594 struct dwc3_request *req, *n, *ret = NULL;
595 struct dwc3_trb_hw *trb_hw;
599 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
601 /* the first request must not be queued */
602 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
604 * if busy & slot are equal than it is either full or empty. If we are
605 * starting to proceed requests then we are empty. Otherwise we ar
606 * full and don't do anything
611 trbs_left = DWC3_TRB_NUM;
613 * In case we start from scratch, we queue the ISOC requests
614 * starting from slot 1. This is done because we use ring
615 * buffer and have no LST bit to stop us. Instead, we place
616 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
617 * after the first request so we start at slot 1 and have
618 * 7 requests proceed before we hit the first IOC.
619 * Other transfer types don't use the ring buffer and are
620 * processed from the first TRB until the last one. Since we
621 * don't wrap around we have to start at the beginning.
623 if (usb_endpoint_xfer_isoc(dep->desc)) {
632 /* The last TRB is a link TRB, not used for xfer */
633 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
636 list_for_each_entry_safe(req, n, &dep->request_list, list) {
637 unsigned int last_one = 0;
638 unsigned int cur_slot;
640 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
641 cur_slot = dep->free_slot;
644 /* Skip the LINK-TRB on ISOC */
645 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
646 usb_endpoint_xfer_isoc(dep->desc))
649 dwc3_gadget_move_request_queued(req);
650 memset(&trb, 0, sizeof(trb));
653 /* Is our TRB pool empty? */
656 /* Is this the last request? */
657 if (list_empty(&dep->request_list))
661 * FIXME we shouldn't need to set LST bit always but we are
662 * facing some weird problem with the Hardware where it doesn't
663 * complete even though it has been previously started.
665 * While we're debugging the problem, as a workaround to
666 * multiple TRBs handling, use only one TRB at a time.
674 trb.bplh = req->request.dma;
676 if (usb_endpoint_xfer_isoc(dep->desc)) {
683 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
684 trb.sid_sofn = req->request.stream_id;
686 switch (usb_endpoint_type(dep->desc)) {
687 case USB_ENDPOINT_XFER_CONTROL:
688 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
691 case USB_ENDPOINT_XFER_ISOC:
692 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
694 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
695 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
699 case USB_ENDPOINT_XFER_BULK:
700 case USB_ENDPOINT_XFER_INT:
701 trb.trbctl = DWC3_TRBCTL_NORMAL;
705 * This is only possible with faulty memory because we
706 * checked it already :)
711 trb.length = req->request.length;
714 dwc3_trb_to_hw(&trb, trb_hw);
715 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
724 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
727 struct dwc3_gadget_ep_cmd_params params;
728 struct dwc3_request *req;
729 struct dwc3 *dwc = dep->dwc;
733 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
734 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
737 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
740 * If we are getting here after a short-out-packet we don't enqueue any
741 * new requests as we try to set the IOC bit only on the last request.
744 if (list_empty(&dep->req_queued))
745 dwc3_prepare_trbs(dep, start_new);
747 /* req points to the first request which will be sent */
748 req = next_request(&dep->req_queued);
751 * req points to the first request where HWO changed
754 req = dwc3_prepare_trbs(dep, start_new);
757 dep->flags |= DWC3_EP_PENDING_REQUEST;
761 memset(¶ms, 0, sizeof(params));
762 params.param0 = upper_32_bits(req->trb_dma);
763 params.param1 = lower_32_bits(req->trb_dma);
766 cmd = DWC3_DEPCMD_STARTTRANSFER;
768 cmd = DWC3_DEPCMD_UPDATETRANSFER;
770 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
771 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
773 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
776 * FIXME we need to iterate over the list of requests
777 * here and stop, unmap, free and del each of the linked
778 * requests instead of we do now.
780 dwc3_unmap_buffer_from_dma(req);
781 list_del(&req->list);
785 dep->flags |= DWC3_EP_BUSY;
786 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
788 if (!dep->res_trans_idx)
789 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
793 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
795 req->request.actual = 0;
796 req->request.status = -EINPROGRESS;
797 req->direction = dep->direction;
798 req->epnum = dep->number;
801 * We only add to our list of requests now and
802 * start consuming the list once we get XferNotReady
805 * That way, we avoid doing anything that we don't need
806 * to do now and defer it until the point we receive a
807 * particular token from the Host side.
809 * This will also avoid Host cancelling URBs due to too
812 dwc3_map_buffer_to_dma(req);
813 list_add_tail(&req->list, &dep->request_list);
816 * There is one special case: XferNotReady with
817 * empty list of requests. We need to kick the
818 * transfer here in that situation, otherwise
819 * we will be NAKing forever.
821 * If we get XferNotReady before gadget driver
822 * has a chance to queue a request, we will ACK
823 * the IRQ but won't be able to receive the data
824 * until the next request is queued. The following
825 * code is handling exactly that.
827 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
832 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
833 dep->flags & DWC3_EP_BUSY)
836 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
837 if (ret && ret != -EBUSY) {
838 struct dwc3 *dwc = dep->dwc;
840 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
848 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
851 struct dwc3_request *req = to_dwc3_request(request);
852 struct dwc3_ep *dep = to_dwc3_ep(ep);
853 struct dwc3 *dwc = dep->dwc;
860 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
865 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
866 request, ep->name, request->length);
868 spin_lock_irqsave(&dwc->lock, flags);
869 ret = __dwc3_gadget_ep_queue(dep, req);
870 spin_unlock_irqrestore(&dwc->lock, flags);
875 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
876 struct usb_request *request)
878 struct dwc3_request *req = to_dwc3_request(request);
879 struct dwc3_request *r = NULL;
881 struct dwc3_ep *dep = to_dwc3_ep(ep);
882 struct dwc3 *dwc = dep->dwc;
887 spin_lock_irqsave(&dwc->lock, flags);
889 list_for_each_entry(r, &dep->request_list, list) {
895 list_for_each_entry(r, &dep->req_queued, list) {
900 /* wait until it is processed */
901 dwc3_stop_active_transfer(dwc, dep->number);
904 dev_err(dwc->dev, "request %p was not queued to %s\n",
910 /* giveback the request */
911 dwc3_gadget_giveback(dep, req, -ECONNRESET);
914 spin_unlock_irqrestore(&dwc->lock, flags);
919 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
921 struct dwc3_gadget_ep_cmd_params params;
922 struct dwc3 *dwc = dep->dwc;
925 memset(¶ms, 0x00, sizeof(params));
928 if (dep->flags & DWC3_EP_STALL)
931 if (dep->number == 0 || dep->number == 1) {
933 * Whenever EP0 is stalled, we will restart
934 * the state machine, thus moving back to
937 dwc->ep0state = EP0_SETUP_PHASE;
940 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
941 DWC3_DEPCMD_SETSTALL, ¶ms);
943 dev_err(dwc->dev, "failed to %s STALL on %s\n",
944 value ? "set" : "clear",
947 dep->flags |= DWC3_EP_STALL;
949 if (!(dep->flags & DWC3_EP_STALL))
952 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
953 DWC3_DEPCMD_CLEARSTALL, ¶ms);
955 dev_err(dwc->dev, "failed to %s STALL on %s\n",
956 value ? "set" : "clear",
959 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
965 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
967 struct dwc3_ep *dep = to_dwc3_ep(ep);
968 struct dwc3 *dwc = dep->dwc;
974 spin_lock_irqsave(&dwc->lock, flags);
976 if (usb_endpoint_xfer_isoc(dep->desc)) {
977 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
982 ret = __dwc3_gadget_ep_set_halt(dep, value);
984 spin_unlock_irqrestore(&dwc->lock, flags);
989 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
991 struct dwc3_ep *dep = to_dwc3_ep(ep);
993 dep->flags |= DWC3_EP_WEDGE;
995 return dwc3_gadget_ep_set_halt(ep, 1);
998 /* -------------------------------------------------------------------------- */
1000 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1001 .bLength = USB_DT_ENDPOINT_SIZE,
1002 .bDescriptorType = USB_DT_ENDPOINT,
1003 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1006 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1007 .enable = dwc3_gadget_ep0_enable,
1008 .disable = dwc3_gadget_ep0_disable,
1009 .alloc_request = dwc3_gadget_ep_alloc_request,
1010 .free_request = dwc3_gadget_ep_free_request,
1011 .queue = dwc3_gadget_ep0_queue,
1012 .dequeue = dwc3_gadget_ep_dequeue,
1013 .set_halt = dwc3_gadget_ep_set_halt,
1014 .set_wedge = dwc3_gadget_ep_set_wedge,
1017 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1018 .enable = dwc3_gadget_ep_enable,
1019 .disable = dwc3_gadget_ep_disable,
1020 .alloc_request = dwc3_gadget_ep_alloc_request,
1021 .free_request = dwc3_gadget_ep_free_request,
1022 .queue = dwc3_gadget_ep_queue,
1023 .dequeue = dwc3_gadget_ep_dequeue,
1024 .set_halt = dwc3_gadget_ep_set_halt,
1025 .set_wedge = dwc3_gadget_ep_set_wedge,
1028 /* -------------------------------------------------------------------------- */
1030 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1032 struct dwc3 *dwc = gadget_to_dwc(g);
1035 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1036 return DWC3_DSTS_SOFFN(reg);
1039 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1041 struct dwc3 *dwc = gadget_to_dwc(g);
1043 unsigned long timeout;
1044 unsigned long flags;
1053 spin_lock_irqsave(&dwc->lock, flags);
1056 * According to the Databook Remote wakeup request should
1057 * be issued only when the device is in early suspend state.
1059 * We can check that via USB Link State bits in DSTS register.
1061 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1063 speed = reg & DWC3_DSTS_CONNECTSPD;
1064 if (speed == DWC3_DSTS_SUPERSPEED) {
1065 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1070 link_state = DWC3_DSTS_USBLNKST(reg);
1072 switch (link_state) {
1073 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1074 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1077 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1083 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1086 * Switch link state to Recovery. In HS/FS/LS this means
1087 * RemoteWakeup Request
1089 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1090 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1092 /* wait for at least 2000us */
1093 usleep_range(2000, 2500);
1095 /* write zeroes to Link Change Request */
1096 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1097 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1099 /* pool until Link State change to ON */
1100 timeout = jiffies + msecs_to_jiffies(100);
1102 while (!(time_after(jiffies, timeout))) {
1103 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1105 /* in HS, means ON */
1106 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1110 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1111 dev_err(dwc->dev, "failed to send remote wakeup\n");
1116 spin_unlock_irqrestore(&dwc->lock, flags);
1121 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1124 struct dwc3 *dwc = gadget_to_dwc(g);
1126 dwc->is_selfpowered = !!is_selfpowered;
1131 static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1136 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1138 reg |= DWC3_DCTL_RUN_STOP;
1140 reg &= ~DWC3_DCTL_RUN_STOP;
1142 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1145 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1147 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1150 if (reg & DWC3_DSTS_DEVCTRLHLT)
1159 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1161 ? dwc->gadget_driver->function : "no-function",
1162 is_on ? "connect" : "disconnect");
1165 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1167 struct dwc3 *dwc = gadget_to_dwc(g);
1168 unsigned long flags;
1172 spin_lock_irqsave(&dwc->lock, flags);
1173 dwc3_gadget_run_stop(dwc, is_on);
1174 spin_unlock_irqrestore(&dwc->lock, flags);
1179 static int dwc3_gadget_start(struct usb_gadget *g,
1180 struct usb_gadget_driver *driver)
1182 struct dwc3 *dwc = gadget_to_dwc(g);
1183 struct dwc3_ep *dep;
1184 unsigned long flags;
1188 spin_lock_irqsave(&dwc->lock, flags);
1190 if (dwc->gadget_driver) {
1191 dev_err(dwc->dev, "%s is already bound to %s\n",
1193 dwc->gadget_driver->driver.name);
1198 dwc->gadget_driver = driver;
1199 dwc->gadget.dev.driver = &driver->driver;
1201 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1203 reg &= ~DWC3_GCTL_SCALEDOWN(3);
1204 reg &= ~DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG);
1205 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1206 reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
1208 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams0)) {
1209 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
1210 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
1213 dev_dbg(dwc->dev, "No power optimization available\n");
1217 * WORKAROUND: DWC3 revisions <1.90a have a bug
1218 * when The device fails to connect at SuperSpeed
1219 * and falls back to high-speed mode which causes
1220 * the device to enter in a Connect/Disconnect loop
1222 if (dwc->revision < DWC3_REVISION_190A)
1223 reg |= DWC3_GCTL_U2RSTECN;
1225 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1227 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1228 reg &= ~(DWC3_DCFG_SPEED_MASK);
1229 reg |= DWC3_DCFG_SUPERSPEED;
1230 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1232 /* Start with SuperSpeed Default */
1233 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1236 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1238 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1243 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1245 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1249 /* begin to receive SETUP packets */
1250 dwc->ep0state = EP0_SETUP_PHASE;
1251 dwc3_ep0_out_start(dwc);
1253 spin_unlock_irqrestore(&dwc->lock, flags);
1258 __dwc3_gadget_ep_disable(dwc->eps[0]);
1261 dwc->gadget_driver = NULL;
1262 spin_unlock_irqrestore(&dwc->lock, flags);
1267 static int dwc3_gadget_stop(struct usb_gadget *g,
1268 struct usb_gadget_driver *driver)
1270 struct dwc3 *dwc = gadget_to_dwc(g);
1271 unsigned long flags;
1273 spin_lock_irqsave(&dwc->lock, flags);
1275 __dwc3_gadget_ep_disable(dwc->eps[0]);
1276 __dwc3_gadget_ep_disable(dwc->eps[1]);
1278 dwc->gadget_driver = NULL;
1279 dwc->gadget.dev.driver = NULL;
1281 spin_unlock_irqrestore(&dwc->lock, flags);
1285 static const struct usb_gadget_ops dwc3_gadget_ops = {
1286 .get_frame = dwc3_gadget_get_frame,
1287 .wakeup = dwc3_gadget_wakeup,
1288 .set_selfpowered = dwc3_gadget_set_selfpowered,
1289 .pullup = dwc3_gadget_pullup,
1290 .udc_start = dwc3_gadget_start,
1291 .udc_stop = dwc3_gadget_stop,
1294 /* -------------------------------------------------------------------------- */
1296 static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1298 struct dwc3_ep *dep;
1301 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1303 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1304 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1306 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1312 dep->number = epnum;
1313 dwc->eps[epnum] = dep;
1315 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1316 (epnum & 1) ? "in" : "out");
1317 dep->endpoint.name = dep->name;
1318 dep->direction = (epnum & 1);
1320 if (epnum == 0 || epnum == 1) {
1321 dep->endpoint.maxpacket = 512;
1322 dep->endpoint.maxburst = 1;
1323 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1325 dwc->gadget.ep0 = &dep->endpoint;
1329 dep->endpoint.maxpacket = 1024;
1330 dep->endpoint.max_streams = 15;
1331 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1332 list_add_tail(&dep->endpoint.ep_list,
1333 &dwc->gadget.ep_list);
1335 ret = dwc3_alloc_trb_pool(dep);
1337 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1341 INIT_LIST_HEAD(&dep->request_list);
1342 INIT_LIST_HEAD(&dep->req_queued);
1348 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1350 struct dwc3_ep *dep;
1353 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1354 dep = dwc->eps[epnum];
1356 * Physical endpoints 0 and 1 are special; they form the
1357 * bi-directional USB endpoint 0.
1359 * For those two physical endpoints, we don't allocate a TRB
1360 * pool nor do we add them the endpoints list. Due to that, we
1361 * shouldn't do these two operations otherwise we would end up
1362 * with all sorts of bugs when removing dwc3.ko.
1364 if (epnum != 0 && epnum != 1) {
1365 dwc3_free_trb_pool(dep);
1366 list_del(&dep->endpoint.ep_list);
1373 static void dwc3_gadget_release(struct device *dev)
1375 dev_dbg(dev, "%s\n", __func__);
1378 /* -------------------------------------------------------------------------- */
1379 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1380 const struct dwc3_event_depevt *event, int status)
1382 struct dwc3_request *req;
1383 struct dwc3_trb trb;
1385 unsigned int s_pkt = 0;
1388 req = next_request(&dep->req_queued);
1392 dwc3_trb_to_nat(req->trb, &trb);
1394 if (trb.hwo && status != -ESHUTDOWN)
1396 * We continue despite the error. There is not much we
1397 * can do. If we don't clean in up we loop for ever. If
1398 * we skip the TRB than it gets overwritten reused after
1399 * a while since we use them in a ring buffer. a BUG()
1400 * would help. Lets hope that if this occures, someone
1401 * fixes the root cause instead of looking away :)
1403 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1404 dep->name, req->trb);
1407 if (dep->direction) {
1409 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1411 status = -ECONNRESET;
1414 if (count && (event->status & DEPEVT_STATUS_SHORT))
1419 * We assume here we will always receive the entire data block
1420 * which we should receive. Meaning, if we program RX to
1421 * receive 4K but we receive only 2K, we assume that's all we
1422 * should receive and we simply bounce the request back to the
1423 * gadget driver for further processing.
1425 req->request.actual += req->request.length - count;
1426 dwc3_gadget_giveback(dep, req, status);
1429 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1431 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1435 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1440 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1441 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1444 unsigned status = 0;
1447 if (event->status & DEPEVT_STATUS_BUSERR)
1448 status = -ECONNRESET;
1450 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1452 dep->flags &= ~DWC3_EP_BUSY;
1453 dep->res_trans_idx = 0;
1457 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1458 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1462 if (list_empty(&dep->request_list)) {
1463 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1468 mask = ~(dep->interval - 1);
1469 uf = event->parameters & mask;
1470 /* 4 micro frames in the future */
1471 uf += dep->interval * 4;
1473 __dwc3_gadget_kick_transfer(dep, uf, 1);
1476 static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1477 const struct dwc3_event_depevt *event)
1479 struct dwc3 *dwc = dep->dwc;
1480 struct dwc3_event_depevt mod_ev = *event;
1483 * We were asked to remove one requests. It is possible that this
1484 * request and a few other were started together and have the same
1485 * transfer index. Since we stopped the complete endpoint we don't
1486 * know how many requests were already completed (and not yet)
1487 * reported and how could be done (later). We purge them all until
1488 * the end of the list.
1490 mod_ev.status = DEPEVT_STATUS_LST;
1491 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1492 dep->flags &= ~DWC3_EP_BUSY;
1493 /* pending requets are ignored and are queued on XferNotReady */
1496 static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1497 const struct dwc3_event_depevt *event)
1499 u32 param = event->parameters;
1500 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1503 case DWC3_DEPCMD_ENDTRANSFER:
1504 dwc3_process_ep_cmd_complete(dep, event);
1506 case DWC3_DEPCMD_STARTTRANSFER:
1507 dep->res_trans_idx = param & 0x7f;
1510 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1511 __func__, cmd_type);
1516 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1517 const struct dwc3_event_depevt *event)
1519 struct dwc3_ep *dep;
1520 u8 epnum = event->endpoint_number;
1522 dep = dwc->eps[epnum];
1524 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1525 dwc3_ep_event_string(event->endpoint_event));
1527 if (epnum == 0 || epnum == 1) {
1528 dwc3_ep0_interrupt(dwc, event);
1532 switch (event->endpoint_event) {
1533 case DWC3_DEPEVT_XFERCOMPLETE:
1534 if (usb_endpoint_xfer_isoc(dep->desc)) {
1535 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1540 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1542 case DWC3_DEPEVT_XFERINPROGRESS:
1543 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1544 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1549 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1551 case DWC3_DEPEVT_XFERNOTREADY:
1552 if (usb_endpoint_xfer_isoc(dep->desc)) {
1553 dwc3_gadget_start_isoc(dwc, dep, event);
1557 dev_vdbg(dwc->dev, "%s: reason %s\n",
1558 dep->name, event->status
1560 : "Transfer Not Active");
1562 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1563 if (!ret || ret == -EBUSY)
1566 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1571 case DWC3_DEPEVT_STREAMEVT:
1572 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1573 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1578 switch (event->status) {
1579 case DEPEVT_STREAMEVT_FOUND:
1580 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1584 case DEPEVT_STREAMEVT_NOTFOUND:
1587 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1590 case DWC3_DEPEVT_RXTXFIFOEVT:
1591 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1593 case DWC3_DEPEVT_EPCMDCMPLT:
1594 dwc3_ep_cmd_compl(dep, event);
1599 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1601 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1602 spin_unlock(&dwc->lock);
1603 dwc->gadget_driver->disconnect(&dwc->gadget);
1604 spin_lock(&dwc->lock);
1608 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1610 struct dwc3_ep *dep;
1611 struct dwc3_gadget_ep_cmd_params params;
1615 dep = dwc->eps[epnum];
1617 WARN_ON(!dep->res_trans_idx);
1618 if (dep->res_trans_idx) {
1619 cmd = DWC3_DEPCMD_ENDTRANSFER;
1620 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1621 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1622 memset(¶ms, 0, sizeof(params));
1623 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1625 dep->res_trans_idx = 0;
1629 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1633 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1634 struct dwc3_ep *dep;
1636 dep = dwc->eps[epnum];
1637 if (!(dep->flags & DWC3_EP_ENABLED))
1640 dwc3_remove_requests(dwc, dep);
1644 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1648 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1649 struct dwc3_ep *dep;
1650 struct dwc3_gadget_ep_cmd_params params;
1653 dep = dwc->eps[epnum];
1655 if (!(dep->flags & DWC3_EP_STALL))
1658 dep->flags &= ~DWC3_EP_STALL;
1660 memset(¶ms, 0, sizeof(params));
1661 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1662 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1667 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1669 dev_vdbg(dwc->dev, "%s\n", __func__);
1672 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1673 enable it before we can disable it.
1675 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1676 reg &= ~DWC3_DCTL_INITU1ENA;
1677 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1679 reg &= ~DWC3_DCTL_INITU2ENA;
1680 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1683 dwc3_stop_active_transfers(dwc);
1684 dwc3_disconnect_gadget(dwc);
1686 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1689 static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1693 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1696 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1698 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1700 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1703 static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1707 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1710 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1712 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1714 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1717 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1721 dev_vdbg(dwc->dev, "%s\n", __func__);
1724 dwc3_gadget_usb2_phy_power(dwc, true);
1725 dwc3_gadget_usb3_phy_power(dwc, true);
1727 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1728 dwc3_disconnect_gadget(dwc);
1730 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1731 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1732 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1734 dwc3_stop_active_transfers(dwc);
1735 dwc3_clear_stall_all_ep(dwc);
1737 /* Reset device address to zero */
1738 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1739 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1740 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1743 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1746 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1749 * We change the clock only at SS but I dunno why I would want to do
1750 * this. Maybe it becomes part of the power saving plan.
1753 if (speed != DWC3_DSTS_SUPERSPEED)
1757 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1758 * each time on Connect Done.
1763 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1764 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1765 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1768 static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1771 case USB_SPEED_SUPER:
1772 dwc3_gadget_usb2_phy_power(dwc, false);
1774 case USB_SPEED_HIGH:
1775 case USB_SPEED_FULL:
1777 dwc3_gadget_usb3_phy_power(dwc, false);
1782 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1784 struct dwc3_gadget_ep_cmd_params params;
1785 struct dwc3_ep *dep;
1790 dev_vdbg(dwc->dev, "%s\n", __func__);
1792 memset(¶ms, 0x00, sizeof(params));
1794 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1795 speed = reg & DWC3_DSTS_CONNECTSPD;
1798 dwc3_update_ram_clk_sel(dwc, speed);
1801 case DWC3_DCFG_SUPERSPEED:
1802 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1803 dwc->gadget.ep0->maxpacket = 512;
1804 dwc->gadget.speed = USB_SPEED_SUPER;
1806 case DWC3_DCFG_HIGHSPEED:
1807 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1808 dwc->gadget.ep0->maxpacket = 64;
1809 dwc->gadget.speed = USB_SPEED_HIGH;
1811 case DWC3_DCFG_FULLSPEED2:
1812 case DWC3_DCFG_FULLSPEED1:
1813 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1814 dwc->gadget.ep0->maxpacket = 64;
1815 dwc->gadget.speed = USB_SPEED_FULL;
1817 case DWC3_DCFG_LOWSPEED:
1818 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1819 dwc->gadget.ep0->maxpacket = 8;
1820 dwc->gadget.speed = USB_SPEED_LOW;
1824 /* Disable unneded PHY */
1825 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1828 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1830 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1835 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1837 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1842 * Configure PHY via GUSB3PIPECTLn if required.
1844 * Update GTXFIFOSIZn
1846 * In both cases reset values should be sufficient.
1850 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1852 dev_vdbg(dwc->dev, "%s\n", __func__);
1855 * TODO take core out of low power mode when that's
1859 dwc->gadget_driver->resume(&dwc->gadget);
1862 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1863 unsigned int evtinfo)
1865 /* The fith bit says SuperSpeed yes or no. */
1866 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1868 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
1871 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1872 const struct dwc3_event_devt *event)
1874 switch (event->type) {
1875 case DWC3_DEVICE_EVENT_DISCONNECT:
1876 dwc3_gadget_disconnect_interrupt(dwc);
1878 case DWC3_DEVICE_EVENT_RESET:
1879 dwc3_gadget_reset_interrupt(dwc);
1881 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1882 dwc3_gadget_conndone_interrupt(dwc);
1884 case DWC3_DEVICE_EVENT_WAKEUP:
1885 dwc3_gadget_wakeup_interrupt(dwc);
1887 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1888 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1890 case DWC3_DEVICE_EVENT_EOPF:
1891 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1893 case DWC3_DEVICE_EVENT_SOF:
1894 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1896 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1897 dev_vdbg(dwc->dev, "Erratic Error\n");
1899 case DWC3_DEVICE_EVENT_CMD_CMPL:
1900 dev_vdbg(dwc->dev, "Command Complete\n");
1902 case DWC3_DEVICE_EVENT_OVERFLOW:
1903 dev_vdbg(dwc->dev, "Overflow\n");
1906 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1910 static void dwc3_process_event_entry(struct dwc3 *dwc,
1911 const union dwc3_event *event)
1913 /* Endpoint IRQ, handle it and return early */
1914 if (event->type.is_devspec == 0) {
1916 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1919 switch (event->type.type) {
1920 case DWC3_EVENT_TYPE_DEV:
1921 dwc3_gadget_interrupt(dwc, &event->devt);
1923 /* REVISIT what to do with Carkit and I2C events ? */
1925 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1929 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1931 struct dwc3_event_buffer *evt;
1935 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1936 count &= DWC3_GEVNTCOUNT_MASK;
1940 evt = dwc->ev_buffs[buf];
1944 union dwc3_event event;
1946 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1947 dwc3_process_event_entry(dwc, &event);
1949 * XXX we wrap around correctly to the next entry as almost all
1950 * entries are 4 bytes in size. There is one entry which has 12
1951 * bytes which is a regular entry followed by 8 bytes data. ATM
1952 * I don't know how things are organized if were get next to the
1953 * a boundary so I worry about that once we try to handle that.
1955 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1958 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1964 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1966 struct dwc3 *dwc = _dwc;
1968 irqreturn_t ret = IRQ_NONE;
1970 spin_lock(&dwc->lock);
1972 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1975 status = dwc3_process_event_buf(dwc, i);
1976 if (status == IRQ_HANDLED)
1980 spin_unlock(&dwc->lock);
1986 * dwc3_gadget_init - Initializes gadget related registers
1987 * @dwc: Pointer to out controller context structure
1989 * Returns 0 on success otherwise negative errno.
1991 int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1997 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1998 &dwc->ctrl_req_addr, GFP_KERNEL);
1999 if (!dwc->ctrl_req) {
2000 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2005 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2006 &dwc->ep0_trb_addr, GFP_KERNEL);
2007 if (!dwc->ep0_trb) {
2008 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2013 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
2014 sizeof(*dwc->setup_buf) * 2,
2015 &dwc->setup_buf_addr, GFP_KERNEL);
2016 if (!dwc->setup_buf) {
2017 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2022 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2023 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2024 if (!dwc->ep0_bounce) {
2025 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2030 dev_set_name(&dwc->gadget.dev, "gadget");
2032 dwc->gadget.ops = &dwc3_gadget_ops;
2033 dwc->gadget.is_dualspeed = true;
2034 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2035 dwc->gadget.dev.parent = dwc->dev;
2037 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2039 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2040 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2041 dwc->gadget.dev.release = dwc3_gadget_release;
2042 dwc->gadget.name = "dwc3-gadget";
2045 * REVISIT: Here we should clear all pending IRQs to be
2046 * sure we're starting from a well known location.
2049 ret = dwc3_gadget_init_endpoints(dwc);
2053 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2055 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2058 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2063 /* Enable all but Start and End of Frame IRQs */
2064 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2065 DWC3_DEVTEN_EVNTOVERFLOWEN |
2066 DWC3_DEVTEN_CMDCMPLTEN |
2067 DWC3_DEVTEN_ERRTICERREN |
2068 DWC3_DEVTEN_WKUPEVTEN |
2069 DWC3_DEVTEN_ULSTCNGEN |
2070 DWC3_DEVTEN_CONNECTDONEEN |
2071 DWC3_DEVTEN_USBRSTEN |
2072 DWC3_DEVTEN_DISCONNEVTEN);
2073 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2075 ret = device_register(&dwc->gadget.dev);
2077 dev_err(dwc->dev, "failed to register gadget device\n");
2078 put_device(&dwc->gadget.dev);
2082 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2084 dev_err(dwc->dev, "failed to register udc\n");
2091 device_unregister(&dwc->gadget.dev);
2094 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2098 dwc3_gadget_free_endpoints(dwc);
2101 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2102 dwc->ep0_bounce_addr);
2105 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2106 dwc->setup_buf, dwc->setup_buf_addr);
2109 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2110 dwc->ep0_trb, dwc->ep0_trb_addr);
2113 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2114 dwc->ctrl_req, dwc->ctrl_req_addr);
2120 void dwc3_gadget_exit(struct dwc3 *dwc)
2125 usb_del_gadget_udc(&dwc->gadget);
2126 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2128 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2131 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2132 __dwc3_gadget_ep_disable(dwc->eps[i]);
2134 dwc3_gadget_free_endpoints(dwc);
2136 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2137 dwc->ep0_bounce_addr);
2139 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2140 dwc->setup_buf, dwc->setup_buf_addr);
2142 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2143 dwc->ep0_trb, dwc->ep0_trb_addr);
2145 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2146 dwc->ctrl_req, dwc->ctrl_req_addr);
2148 device_unregister(&dwc->gadget.dev);