2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
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 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/interrupt.h>
26 #include <linux/list.h>
27 #include <linux/dma-mapping.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
31 #include <linux/usb/composite.h>
38 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
39 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
40 struct dwc3_ep *dep, struct dwc3_request *req);
42 static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
51 case EP0_STATUS_PHASE:
52 return "Status Phase";
58 static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
61 struct dwc3_gadget_ep_cmd_params params;
67 dep = dwc->eps[epnum];
68 if (dep->flags & DWC3_EP_BUSY) {
69 dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name);
75 trb->bpl = lower_32_bits(buf_dma);
76 trb->bph = upper_32_bits(buf_dma);
80 trb->ctrl |= (DWC3_TRB_CTRL_HWO
83 | DWC3_TRB_CTRL_ISP_IMI);
85 memset(¶ms, 0, sizeof(params));
86 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
87 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
89 trace_dwc3_prepare_trb(dep, trb);
91 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
92 DWC3_DEPCMD_STARTTRANSFER, ¶ms);
94 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
99 dep->flags |= DWC3_EP_BUSY;
100 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
103 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
108 static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
109 struct dwc3_request *req)
111 struct dwc3 *dwc = dep->dwc;
113 req->request.actual = 0;
114 req->request.status = -EINPROGRESS;
115 req->epnum = dep->number;
117 list_add_tail(&req->list, &dep->request_list);
120 * Gadget driver might not be quick enough to queue a request
121 * before we get a Transfer Not Ready event on this endpoint.
123 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
124 * flag is set, it's telling us that as soon as Gadget queues the
125 * required request, we should kick the transfer here because the
126 * IRQ we were waiting for is long gone.
128 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
131 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
133 if (dwc->ep0state != EP0_DATA_PHASE) {
134 dev_WARN(dwc->dev, "Unexpected pending request\n");
138 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
140 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
147 * In case gadget driver asked us to delay the STATUS phase,
150 if (dwc->delayed_status) {
153 direction = !dwc->ep0_expect_in;
154 dwc->delayed_status = false;
155 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
157 if (dwc->ep0state == EP0_STATUS_PHASE)
158 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
160 dwc3_trace(trace_dwc3_ep0,
161 "too early for delayed status");
167 * Unfortunately we have uncovered a limitation wrt the Data Phase.
169 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
170 * come before issueing Start Transfer command, but if we do, we will
171 * miss situations where the host starts another SETUP phase instead of
172 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
173 * Layer Compliance Suite.
175 * The problem surfaces due to the fact that in case of back-to-back
176 * SETUP packets there will be no XferNotReady(DATA) generated and we
177 * will be stuck waiting for XferNotReady(DATA) forever.
179 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
180 * it tells us to start Data Phase right away. It also mentions that if
181 * we receive a SETUP phase instead of the DATA phase, core will issue
182 * XferComplete for the DATA phase, before actually initiating it in
183 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
184 * can only be used to print some debugging logs, as the core expects
185 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
186 * just so it completes right away, without transferring anything and,
187 * only then, we can go back to the SETUP phase.
189 * Because of this scenario, SNPS decided to change the programming
190 * model of control transfers and support on-demand transfers only for
191 * the STATUS phase. To fix the issue we have now, we will always wait
192 * for gadget driver to queue the DATA phase's struct usb_request, then
193 * start it right away.
195 * If we're actually in a 2-stage transfer, we will wait for
196 * XferNotReady(STATUS).
198 if (dwc->three_stage_setup) {
201 direction = dwc->ep0_expect_in;
202 dwc->ep0state = EP0_DATA_PHASE;
204 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
206 dep->flags &= ~DWC3_EP0_DIR_IN;
212 int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
215 struct dwc3_request *req = to_dwc3_request(request);
216 struct dwc3_ep *dep = to_dwc3_ep(ep);
217 struct dwc3 *dwc = dep->dwc;
223 spin_lock_irqsave(&dwc->lock, flags);
224 if (!dep->endpoint.desc) {
225 dwc3_trace(trace_dwc3_ep0,
226 "trying to queue request %p to disabled %s",
232 /* we share one TRB for ep0/1 */
233 if (!list_empty(&dep->request_list)) {
238 dwc3_trace(trace_dwc3_ep0,
239 "queueing request %p to %s length %d state '%s'",
240 request, dep->name, request->length,
241 dwc3_ep0_state_string(dwc->ep0state));
243 ret = __dwc3_gadget_ep0_queue(dep, req);
246 spin_unlock_irqrestore(&dwc->lock, flags);
251 static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
255 /* reinitialize physical ep1 */
257 dep->flags = DWC3_EP_ENABLED;
259 /* stall is always issued on EP0 */
261 __dwc3_gadget_ep_set_halt(dep, 1, false);
262 dep->flags = DWC3_EP_ENABLED;
263 dwc->delayed_status = false;
265 if (!list_empty(&dep->request_list)) {
266 struct dwc3_request *req;
268 req = next_request(&dep->request_list);
269 dwc3_gadget_giveback(dep, req, -ECONNRESET);
272 dwc->ep0state = EP0_SETUP_PHASE;
273 dwc3_ep0_out_start(dwc);
276 int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
278 struct dwc3_ep *dep = to_dwc3_ep(ep);
279 struct dwc3 *dwc = dep->dwc;
281 dwc3_ep0_stall_and_restart(dwc);
286 int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
288 struct dwc3_ep *dep = to_dwc3_ep(ep);
289 struct dwc3 *dwc = dep->dwc;
293 spin_lock_irqsave(&dwc->lock, flags);
294 ret = __dwc3_gadget_ep0_set_halt(ep, value);
295 spin_unlock_irqrestore(&dwc->lock, flags);
300 void dwc3_ep0_out_start(struct dwc3 *dwc)
304 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
305 DWC3_TRBCTL_CONTROL_SETUP);
309 static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
312 u32 windex = le16_to_cpu(wIndex_le);
315 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
316 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
319 dep = dwc->eps[epnum];
320 if (dep->flags & DWC3_EP_ENABLED)
326 static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
332 static int dwc3_ep0_handle_status(struct dwc3 *dwc,
333 struct usb_ctrlrequest *ctrl)
339 __le16 *response_pkt;
341 recip = ctrl->bRequestType & USB_RECIP_MASK;
343 case USB_RECIP_DEVICE:
345 * LTM will be set once we know how to set this in HW.
347 usb_status |= dwc->gadget.is_selfpowered;
349 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
350 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
351 if (reg & DWC3_DCTL_INITU1ENA)
352 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
353 if (reg & DWC3_DCTL_INITU2ENA)
354 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
359 case USB_RECIP_INTERFACE:
361 * Function Remote Wake Capable D0
362 * Function Remote Wakeup D1
366 case USB_RECIP_ENDPOINT:
367 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
371 if (dep->flags & DWC3_EP_STALL)
372 usb_status = 1 << USB_ENDPOINT_HALT;
378 response_pkt = (__le16 *) dwc->setup_buf;
379 *response_pkt = cpu_to_le16(usb_status);
382 dwc->ep0_usb_req.dep = dep;
383 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
384 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
385 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
387 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
390 static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
391 struct usb_ctrlrequest *ctrl, int set)
399 enum usb_device_state state;
401 wValue = le16_to_cpu(ctrl->wValue);
402 wIndex = le16_to_cpu(ctrl->wIndex);
403 recip = ctrl->bRequestType & USB_RECIP_MASK;
404 state = dwc->gadget.state;
407 case USB_RECIP_DEVICE:
410 case USB_DEVICE_REMOTE_WAKEUP:
413 * 9.4.1 says only only for SS, in AddressState only for
414 * default control pipe
416 case USB_DEVICE_U1_ENABLE:
417 if (state != USB_STATE_CONFIGURED)
419 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
422 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
424 reg |= DWC3_DCTL_INITU1ENA;
426 reg &= ~DWC3_DCTL_INITU1ENA;
427 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
430 case USB_DEVICE_U2_ENABLE:
431 if (state != USB_STATE_CONFIGURED)
433 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
436 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
438 reg |= DWC3_DCTL_INITU2ENA;
440 reg &= ~DWC3_DCTL_INITU2ENA;
441 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
444 case USB_DEVICE_LTM_ENABLE:
447 case USB_DEVICE_TEST_MODE:
448 if ((wIndex & 0xff) != 0)
453 dwc->test_mode_nr = wIndex >> 8;
454 dwc->test_mode = true;
461 case USB_RECIP_INTERFACE:
463 case USB_INTRF_FUNC_SUSPEND:
464 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
465 /* XXX enable Low power suspend */
467 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
468 /* XXX enable remote wakeup */
476 case USB_RECIP_ENDPOINT:
478 case USB_ENDPOINT_HALT:
479 dep = dwc3_wIndex_to_dep(dwc, wIndex);
482 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
484 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
500 static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
502 enum usb_device_state state = dwc->gadget.state;
506 addr = le16_to_cpu(ctrl->wValue);
508 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
512 if (state == USB_STATE_CONFIGURED) {
513 dwc3_trace(trace_dwc3_ep0,
514 "trying to set address when configured");
518 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
519 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
520 reg |= DWC3_DCFG_DEVADDR(addr);
521 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
524 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
526 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
531 static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
535 spin_unlock(&dwc->lock);
536 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
537 spin_lock(&dwc->lock);
541 static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
543 enum usb_device_state state = dwc->gadget.state;
548 dwc->start_config_issued = false;
549 cfg = le16_to_cpu(ctrl->wValue);
552 case USB_STATE_DEFAULT:
555 case USB_STATE_ADDRESS:
556 ret = dwc3_ep0_delegate_req(dwc, ctrl);
557 /* if the cfg matches and the cfg is non zero */
558 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
561 * only change state if set_config has already
562 * been processed. If gadget driver returns
563 * USB_GADGET_DELAYED_STATUS, we will wait
564 * to change the state on the next usb_ep_queue()
567 usb_gadget_set_state(&dwc->gadget,
568 USB_STATE_CONFIGURED);
571 * Enable transition to U1/U2 state when
572 * nothing is pending from application.
574 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
575 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
576 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
578 dwc->resize_fifos = true;
579 dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
583 case USB_STATE_CONFIGURED:
584 ret = dwc3_ep0_delegate_req(dwc, ctrl);
586 usb_gadget_set_state(&dwc->gadget,
595 static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
597 struct dwc3_ep *dep = to_dwc3_ep(ep);
598 struct dwc3 *dwc = dep->dwc;
612 memcpy(&timing, req->buf, sizeof(timing));
614 dwc->u1sel = timing.u1sel;
615 dwc->u1pel = timing.u1pel;
616 dwc->u2sel = le16_to_cpu(timing.u2sel);
617 dwc->u2pel = le16_to_cpu(timing.u2pel);
619 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
620 if (reg & DWC3_DCTL_INITU2ENA)
622 if (reg & DWC3_DCTL_INITU1ENA)
626 * According to Synopsys Databook, if parameter is
627 * greater than 125, a value of zero should be
628 * programmed in the register.
633 /* now that we have the time, issue DGCMD Set Sel */
634 ret = dwc3_send_gadget_generic_command(dwc,
635 DWC3_DGCMD_SET_PERIODIC_PAR, param);
639 static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
642 enum usb_device_state state = dwc->gadget.state;
646 if (state == USB_STATE_DEFAULT)
649 wValue = le16_to_cpu(ctrl->wValue);
650 wLength = le16_to_cpu(ctrl->wLength);
653 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
659 * To handle Set SEL we need to receive 6 bytes from Host. So let's
660 * queue a usb_request for 6 bytes.
662 * Remember, though, this controller can't handle non-wMaxPacketSize
663 * aligned transfers on the OUT direction, so we queue a request for
664 * wMaxPacketSize instead.
667 dwc->ep0_usb_req.dep = dep;
668 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
669 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
670 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
672 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
675 static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
681 wValue = le16_to_cpu(ctrl->wValue);
682 wLength = le16_to_cpu(ctrl->wLength);
683 wIndex = le16_to_cpu(ctrl->wIndex);
685 if (wIndex || wLength)
689 * REVISIT It's unclear from Databook what to do with this
690 * value. For now, just cache it.
692 dwc->isoch_delay = wValue;
697 static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
701 switch (ctrl->bRequest) {
702 case USB_REQ_GET_STATUS:
703 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
704 ret = dwc3_ep0_handle_status(dwc, ctrl);
706 case USB_REQ_CLEAR_FEATURE:
707 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
708 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
710 case USB_REQ_SET_FEATURE:
711 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
712 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
714 case USB_REQ_SET_ADDRESS:
715 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
716 ret = dwc3_ep0_set_address(dwc, ctrl);
718 case USB_REQ_SET_CONFIGURATION:
719 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
720 ret = dwc3_ep0_set_config(dwc, ctrl);
722 case USB_REQ_SET_SEL:
723 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
724 ret = dwc3_ep0_set_sel(dwc, ctrl);
726 case USB_REQ_SET_ISOCH_DELAY:
727 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
728 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
731 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
732 ret = dwc3_ep0_delegate_req(dwc, ctrl);
739 static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
740 const struct dwc3_event_depevt *event)
742 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
746 if (!dwc->gadget_driver)
749 trace_dwc3_ctrl_req(ctrl);
751 len = le16_to_cpu(ctrl->wLength);
753 dwc->three_stage_setup = false;
754 dwc->ep0_expect_in = false;
755 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
757 dwc->three_stage_setup = true;
758 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
759 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
762 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
763 ret = dwc3_ep0_std_request(dwc, ctrl);
765 ret = dwc3_ep0_delegate_req(dwc, ctrl);
767 if (ret == USB_GADGET_DELAYED_STATUS)
768 dwc->delayed_status = true;
772 dwc3_ep0_stall_and_restart(dwc);
775 static void dwc3_ep0_complete_data(struct dwc3 *dwc,
776 const struct dwc3_event_depevt *event)
778 struct dwc3_request *r = NULL;
779 struct usb_request *ur;
780 struct dwc3_trb *trb;
787 epnum = event->endpoint_number;
790 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
794 trace_dwc3_complete_trb(ep0, trb);
796 r = next_request(&ep0->request_list);
800 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
801 if (status == DWC3_TRBSTS_SETUP_PENDING) {
802 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
805 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
812 length = trb->size & DWC3_TRB_SIZE_MASK;
814 if (dwc->ep0_bounced) {
815 unsigned transfer_size = ur->length;
816 unsigned maxp = ep0->endpoint.maxpacket;
818 transfer_size += (maxp - (transfer_size % maxp));
819 transferred = min_t(u32, ur->length,
820 transfer_size - length);
821 memcpy(ur->buf, dwc->ep0_bounce, transferred);
823 transferred = ur->length - length;
826 ur->actual += transferred;
828 if ((epnum & 1) && ur->actual < ur->length) {
829 /* for some reason we did not get everything out */
831 dwc3_ep0_stall_and_restart(dwc);
833 dwc3_gadget_giveback(ep0, r, 0);
835 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
836 ur->length && ur->zero) {
839 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
841 ret = dwc3_ep0_start_trans(dwc, epnum,
842 dwc->ctrl_req_addr, 0,
843 DWC3_TRBCTL_CONTROL_DATA);
849 static void dwc3_ep0_complete_status(struct dwc3 *dwc,
850 const struct dwc3_event_depevt *event)
852 struct dwc3_request *r;
854 struct dwc3_trb *trb;
860 trace_dwc3_complete_trb(dep, trb);
862 if (!list_empty(&dep->request_list)) {
863 r = next_request(&dep->request_list);
865 dwc3_gadget_giveback(dep, r, 0);
868 if (dwc->test_mode) {
871 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
873 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
875 dwc3_ep0_stall_and_restart(dwc);
880 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
881 if (status == DWC3_TRBSTS_SETUP_PENDING)
882 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
884 dwc->ep0state = EP0_SETUP_PHASE;
885 dwc3_ep0_out_start(dwc);
888 static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
889 const struct dwc3_event_depevt *event)
891 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
893 dep->flags &= ~DWC3_EP_BUSY;
894 dep->resource_index = 0;
895 dwc->setup_packet_pending = false;
897 switch (dwc->ep0state) {
898 case EP0_SETUP_PHASE:
899 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
900 dwc3_ep0_inspect_setup(dwc, event);
904 dwc3_trace(trace_dwc3_ep0, "Data Phase");
905 dwc3_ep0_complete_data(dwc, event);
908 case EP0_STATUS_PHASE:
909 dwc3_trace(trace_dwc3_ep0, "Status Phase");
910 dwc3_ep0_complete_status(dwc, event);
913 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
917 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
918 struct dwc3_ep *dep, struct dwc3_request *req)
922 req->direction = !!dep->number;
924 if (req->request.length == 0) {
925 ret = dwc3_ep0_start_trans(dwc, dep->number,
926 dwc->ctrl_req_addr, 0,
927 DWC3_TRBCTL_CONTROL_DATA);
928 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
929 && (dep->number == 0)) {
933 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
936 dev_dbg(dwc->dev, "failed to map request\n");
940 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
942 maxpacket = dep->endpoint.maxpacket;
943 transfer_size = roundup(req->request.length, maxpacket);
945 dwc->ep0_bounced = true;
948 * REVISIT in case request length is bigger than
949 * DWC3_EP0_BOUNCE_SIZE we will need two chained
950 * TRBs to handle the transfer.
952 ret = dwc3_ep0_start_trans(dwc, dep->number,
953 dwc->ep0_bounce_addr, transfer_size,
954 DWC3_TRBCTL_CONTROL_DATA);
956 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
959 dev_dbg(dwc->dev, "failed to map request\n");
963 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
964 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
970 static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
972 struct dwc3 *dwc = dep->dwc;
975 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
976 : DWC3_TRBCTL_CONTROL_STATUS2;
978 return dwc3_ep0_start_trans(dwc, dep->number,
979 dwc->ctrl_req_addr, 0, type);
982 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
984 if (dwc->resize_fifos) {
985 dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
986 dwc3_gadget_resize_tx_fifos(dwc);
987 dwc->resize_fifos = 0;
990 WARN_ON(dwc3_ep0_start_control_status(dep));
993 static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
994 const struct dwc3_event_depevt *event)
996 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
998 __dwc3_ep0_do_control_status(dwc, dep);
1001 static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1003 struct dwc3_gadget_ep_cmd_params params;
1007 if (!dep->resource_index)
1010 cmd = DWC3_DEPCMD_ENDTRANSFER;
1011 cmd |= DWC3_DEPCMD_CMDIOC;
1012 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1013 memset(¶ms, 0, sizeof(params));
1014 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1016 dep->resource_index = 0;
1019 static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1020 const struct dwc3_event_depevt *event)
1022 dwc->setup_packet_pending = true;
1024 switch (event->status) {
1025 case DEPEVT_STATUS_CONTROL_DATA:
1026 dwc3_trace(trace_dwc3_ep0, "Control Data");
1029 * We already have a DATA transfer in the controller's cache,
1030 * if we receive a XferNotReady(DATA) we will ignore it, unless
1031 * it's for the wrong direction.
1033 * In that case, we must issue END_TRANSFER command to the Data
1034 * Phase we already have started and issue SetStall on the
1037 if (dwc->ep0_expect_in != event->endpoint_number) {
1038 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1040 dwc3_trace(trace_dwc3_ep0,
1041 "Wrong direction for Data phase");
1042 dwc3_ep0_end_control_data(dwc, dep);
1043 dwc3_ep0_stall_and_restart(dwc);
1049 case DEPEVT_STATUS_CONTROL_STATUS:
1050 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1053 dwc3_trace(trace_dwc3_ep0, "Control Status");
1055 dwc->ep0state = EP0_STATUS_PHASE;
1057 if (dwc->delayed_status) {
1058 WARN_ON_ONCE(event->endpoint_number != 1);
1059 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
1063 dwc3_ep0_do_control_status(dwc, event);
1067 void dwc3_ep0_interrupt(struct dwc3 *dwc,
1068 const struct dwc3_event_depevt *event)
1070 u8 epnum = event->endpoint_number;
1072 dwc3_trace(trace_dwc3_ep0, "%s while ep%d%s in state '%s'",
1073 dwc3_ep_event_string(event->endpoint_event),
1074 epnum >> 1, (epnum & 1) ? "in" : "out",
1075 dwc3_ep0_state_string(dwc->ep0state));
1077 switch (event->endpoint_event) {
1078 case DWC3_DEPEVT_XFERCOMPLETE:
1079 dwc3_ep0_xfer_complete(dwc, event);
1082 case DWC3_DEPEVT_XFERNOTREADY:
1083 dwc3_ep0_xfernotready(dwc, event);
1086 case DWC3_DEPEVT_XFERINPROGRESS:
1087 case DWC3_DEPEVT_RXTXFIFOEVT:
1088 case DWC3_DEPEVT_STREAMEVT:
1089 case DWC3_DEPEVT_EPCMDCMPLT: