1 // SPDX-License-Identifier: GPL-2.0
3 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
5 * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com
10 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/ep0.c) and ported
13 * commit c00552ebaf : Merge 3.18-rc7 into usb-next
18 #include <dm/device_compat.h>
19 #include <linux/bug.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/gadget.h>
25 #include <linux/usb/composite.h>
31 #include "linux-compat.h"
33 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
34 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
35 struct dwc3_ep *dep, struct dwc3_request *req);
37 static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
46 case EP0_STATUS_PHASE:
47 return "Status Phase";
53 static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
54 u32 len, u32 type, unsigned chain)
56 struct dwc3_gadget_ep_cmd_params params;
62 dep = dwc->eps[epnum];
63 if (dep->flags & DWC3_EP_BUSY) {
64 dev_vdbg(dwc->dev, "%s still busy", dep->name);
68 trb = &dwc->ep0_trb[dep->free_slot];
73 trb->bpl = lower_32_bits(buf_dma);
74 trb->bph = upper_32_bits(buf_dma);
78 trb->ctrl |= (DWC3_TRB_CTRL_HWO
79 | DWC3_TRB_CTRL_ISP_IMI);
82 trb->ctrl |= DWC3_TRB_CTRL_CHN;
84 trb->ctrl |= (DWC3_TRB_CTRL_IOC
87 dwc3_flush_cache((uintptr_t)buf_dma, len);
88 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
93 memset(¶ms, 0, sizeof(params));
94 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
95 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
97 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
98 DWC3_DEPCMD_STARTTRANSFER, ¶ms);
100 dev_dbg(dwc->dev, "%s STARTTRANSFER failed", dep->name);
104 dep->flags |= DWC3_EP_BUSY;
105 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
108 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
113 static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
114 struct dwc3_request *req)
116 struct dwc3 *dwc = dep->dwc;
118 req->request.actual = 0;
119 req->request.status = -EINPROGRESS;
120 req->epnum = dep->number;
122 list_add_tail(&req->list, &dep->request_list);
125 * Gadget driver might not be quick enough to queue a request
126 * before we get a Transfer Not Ready event on this endpoint.
128 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
129 * flag is set, it's telling us that as soon as Gadget queues the
130 * required request, we should kick the transfer here because the
131 * IRQ we were waiting for is long gone.
133 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
136 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
138 if (dwc->ep0state != EP0_DATA_PHASE) {
139 dev_WARN(dwc->dev, "Unexpected pending request\n");
143 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
145 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
152 * In case gadget driver asked us to delay the STATUS phase,
155 if (dwc->delayed_status) {
158 direction = !dwc->ep0_expect_in;
159 dwc->delayed_status = false;
160 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
162 if (dwc->ep0state == EP0_STATUS_PHASE)
163 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
165 dev_dbg(dwc->dev, "too early for delayed status");
171 * Unfortunately we have uncovered a limitation wrt the Data Phase.
173 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
174 * come before issueing Start Transfer command, but if we do, we will
175 * miss situations where the host starts another SETUP phase instead of
176 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
177 * Layer Compliance Suite.
179 * The problem surfaces due to the fact that in case of back-to-back
180 * SETUP packets there will be no XferNotReady(DATA) generated and we
181 * will be stuck waiting for XferNotReady(DATA) forever.
183 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
184 * it tells us to start Data Phase right away. It also mentions that if
185 * we receive a SETUP phase instead of the DATA phase, core will issue
186 * XferComplete for the DATA phase, before actually initiating it in
187 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
188 * can only be used to print some debugging logs, as the core expects
189 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
190 * just so it completes right away, without transferring anything and,
191 * only then, we can go back to the SETUP phase.
193 * Because of this scenario, SNPS decided to change the programming
194 * model of control transfers and support on-demand transfers only for
195 * the STATUS phase. To fix the issue we have now, we will always wait
196 * for gadget driver to queue the DATA phase's struct usb_request, then
197 * start it right away.
199 * If we're actually in a 2-stage transfer, we will wait for
200 * XferNotReady(STATUS).
202 if (dwc->three_stage_setup) {
205 direction = dwc->ep0_expect_in;
206 dwc->ep0state = EP0_DATA_PHASE;
208 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
210 dep->flags &= ~DWC3_EP0_DIR_IN;
216 int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
219 struct dwc3_request *req = to_dwc3_request(request);
220 struct dwc3_ep *dep = to_dwc3_ep(ep);
221 struct dwc3 *dwc = dep->dwc;
227 spin_lock_irqsave(&dwc->lock, flags);
228 if (!dep->endpoint.desc) {
229 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s",
235 /* we share one TRB for ep0/1 */
236 if (!list_empty(&dep->request_list)) {
241 dev_vdbg(dwc->dev, "queueing request %p to %s length %d state '%s'",
242 request, dep->name, request->length,
243 dwc3_ep0_state_string(dwc->ep0state));
245 ret = __dwc3_gadget_ep0_queue(dep, req);
248 spin_unlock_irqrestore(&dwc->lock, flags);
253 static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
257 /* reinitialize physical ep1 */
259 dep->flags = DWC3_EP_ENABLED;
261 /* stall is always issued on EP0 */
263 __dwc3_gadget_ep_set_halt(dep, 1, false);
264 dep->flags = DWC3_EP_ENABLED;
265 dwc->delayed_status = false;
267 if (!list_empty(&dep->request_list)) {
268 struct dwc3_request *req;
270 req = next_request(&dep->request_list);
271 dwc3_gadget_giveback(dep, req, -ECONNRESET);
274 dwc->ep0state = EP0_SETUP_PHASE;
275 dwc3_ep0_out_start(dwc);
278 int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
280 struct dwc3_ep *dep = to_dwc3_ep(ep);
281 struct dwc3 *dwc = dep->dwc;
283 dwc3_ep0_stall_and_restart(dwc);
288 int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
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, 0);
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->is_selfpowered << USB_DEVICE_SELF_POWERED;
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 dev_dbg(dwc->dev, "invalid device address %d", addr);
512 if (state == USB_STATE_CONFIGURED) {
513 dev_dbg(dwc->dev, "trying to set address when configured");
517 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
518 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
519 reg |= DWC3_DCFG_DEVADDR(addr);
520 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
523 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
525 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
530 static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
534 spin_unlock(&dwc->lock);
535 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
536 spin_lock(&dwc->lock);
540 static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
542 enum usb_device_state state = dwc->gadget.state;
547 dwc->start_config_issued = false;
548 cfg = le16_to_cpu(ctrl->wValue);
551 case USB_STATE_DEFAULT:
554 case USB_STATE_ADDRESS:
555 ret = dwc3_ep0_delegate_req(dwc, ctrl);
556 /* if the cfg matches and the cfg is non zero */
557 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
560 * only change state if set_config has already
561 * been processed. If gadget driver returns
562 * USB_GADGET_DELAYED_STATUS, we will wait
563 * to change the state on the next usb_ep_queue()
566 usb_gadget_set_state(&dwc->gadget,
567 USB_STATE_CONFIGURED);
570 * Enable transition to U1/U2 state when
571 * nothing is pending from application.
573 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
574 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
575 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
577 dwc->resize_fifos = true;
578 dev_dbg(dwc->dev, "resize FIFOs flag SET");
582 case USB_STATE_CONFIGURED:
583 ret = dwc3_ep0_delegate_req(dwc, ctrl);
585 usb_gadget_set_state(&dwc->gadget,
594 static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
596 struct dwc3_ep *dep = to_dwc3_ep(ep);
597 struct dwc3 *dwc = dep->dwc;
611 memcpy(&timing, req->buf, sizeof(timing));
613 dwc->u1sel = timing.u1sel;
614 dwc->u1pel = timing.u1pel;
615 dwc->u2sel = le16_to_cpu(timing.u2sel);
616 dwc->u2pel = le16_to_cpu(timing.u2pel);
618 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
619 if (reg & DWC3_DCTL_INITU2ENA)
621 if (reg & DWC3_DCTL_INITU1ENA)
625 * According to Synopsys Databook, if parameter is
626 * greater than 125, a value of zero should be
627 * programmed in the register.
632 /* now that we have the time, issue DGCMD Set Sel */
633 ret = dwc3_send_gadget_generic_command(dwc,
634 DWC3_DGCMD_SET_PERIODIC_PAR, param);
638 static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
641 enum usb_device_state state = dwc->gadget.state;
644 if (state == USB_STATE_DEFAULT)
647 wLength = le16_to_cpu(ctrl->wLength);
650 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
656 * To handle Set SEL we need to receive 6 bytes from Host. So let's
657 * queue a usb_request for 6 bytes.
659 * Remember, though, this controller can't handle non-wMaxPacketSize
660 * aligned transfers on the OUT direction, so we queue a request for
661 * wMaxPacketSize instead.
664 dwc->ep0_usb_req.dep = dep;
665 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
666 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
667 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
669 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
672 static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
678 wValue = le16_to_cpu(ctrl->wValue);
679 wLength = le16_to_cpu(ctrl->wLength);
680 wIndex = le16_to_cpu(ctrl->wIndex);
682 if (wIndex || wLength)
686 * REVISIT It's unclear from Databook what to do with this
687 * value. For now, just cache it.
689 dwc->isoch_delay = wValue;
694 static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
698 switch (ctrl->bRequest) {
699 case USB_REQ_GET_STATUS:
700 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS");
701 ret = dwc3_ep0_handle_status(dwc, ctrl);
703 case USB_REQ_CLEAR_FEATURE:
704 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE");
705 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
707 case USB_REQ_SET_FEATURE:
708 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE");
709 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
711 case USB_REQ_SET_ADDRESS:
712 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS");
713 ret = dwc3_ep0_set_address(dwc, ctrl);
715 case USB_REQ_SET_CONFIGURATION:
716 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION");
717 ret = dwc3_ep0_set_config(dwc, ctrl);
719 case USB_REQ_SET_SEL:
720 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL");
721 ret = dwc3_ep0_set_sel(dwc, ctrl);
723 case USB_REQ_SET_ISOCH_DELAY:
724 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY");
725 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
728 dev_vdbg(dwc->dev, "Forwarding to gadget driver");
729 ret = dwc3_ep0_delegate_req(dwc, ctrl);
736 static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
737 const struct dwc3_event_depevt *event)
739 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
743 if (!dwc->gadget_driver)
746 len = le16_to_cpu(ctrl->wLength);
748 dwc->three_stage_setup = false;
749 dwc->ep0_expect_in = false;
750 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
752 dwc->three_stage_setup = true;
753 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
754 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
757 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
758 ret = dwc3_ep0_std_request(dwc, ctrl);
760 ret = dwc3_ep0_delegate_req(dwc, ctrl);
762 if (ret == USB_GADGET_DELAYED_STATUS)
763 dwc->delayed_status = true;
767 dwc3_ep0_stall_and_restart(dwc);
770 static void dwc3_ep0_complete_data(struct dwc3 *dwc,
771 const struct dwc3_event_depevt *event)
773 struct dwc3_request *r = NULL;
774 struct usb_request *ur;
775 struct dwc3_trb *trb;
777 unsigned transfer_size = 0;
785 epnum = event->endpoint_number;
788 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
792 r = next_request(&ep0->request_list);
796 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
798 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
799 if (status == DWC3_TRBSTS_SETUP_PENDING) {
800 dev_dbg(dwc->dev, "Setup Pending received");
803 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
811 length = trb->size & DWC3_TRB_SIZE_MASK;
813 maxp = ep0->endpoint.maxpacket;
815 if (dwc->ep0_bounced) {
817 * Handle the first TRB before handling the bounce buffer if
818 * the request length is greater than the bounce buffer size.
820 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
821 transfer_size = (ur->length / maxp) * maxp;
822 transferred = transfer_size - length;
823 buf = (u8 *)buf + transferred;
824 ur->actual += transferred;
827 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
828 length = trb->size & DWC3_TRB_SIZE_MASK;
833 transfer_size = roundup((ur->length - transfer_size),
835 transferred = min_t(u32, ur->length - transferred,
836 transfer_size - length);
837 dwc3_flush_cache((uintptr_t)dwc->ep0_bounce, DWC3_EP0_BOUNCE_SIZE);
838 memcpy(buf, dwc->ep0_bounce, transferred);
840 transferred = ur->length - length;
843 ur->actual += transferred;
845 if ((epnum & 1) && ur->actual < ur->length) {
846 /* for some reason we did not get everything out */
848 dwc3_ep0_stall_and_restart(dwc);
850 dwc3_gadget_giveback(ep0, r, 0);
852 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
853 ur->length && ur->zero) {
856 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
858 ret = dwc3_ep0_start_trans(dwc, epnum,
859 dwc->ctrl_req_addr, 0,
860 DWC3_TRBCTL_CONTROL_DATA, 0);
866 static void dwc3_ep0_complete_status(struct dwc3 *dwc,
867 const struct dwc3_event_depevt *event)
869 struct dwc3_request *r;
871 struct dwc3_trb *trb;
877 if (!list_empty(&dep->request_list)) {
878 r = next_request(&dep->request_list);
880 dwc3_gadget_giveback(dep, r, 0);
883 if (dwc->test_mode) {
886 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
888 dev_dbg(dwc->dev, "Invalid Test #%d",
890 dwc3_ep0_stall_and_restart(dwc);
895 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
896 if (status == DWC3_TRBSTS_SETUP_PENDING)
897 dev_dbg(dwc->dev, "Setup Pending received");
899 dwc->ep0state = EP0_SETUP_PHASE;
900 dwc3_ep0_out_start(dwc);
903 static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
904 const struct dwc3_event_depevt *event)
906 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
908 dep->flags &= ~DWC3_EP_BUSY;
909 dep->resource_index = 0;
910 dwc->setup_packet_pending = false;
912 switch (dwc->ep0state) {
913 case EP0_SETUP_PHASE:
914 dev_vdbg(dwc->dev, "Setup Phase");
915 dwc3_ep0_inspect_setup(dwc, event);
919 dev_vdbg(dwc->dev, "Data Phase");
920 dwc3_ep0_complete_data(dwc, event);
923 case EP0_STATUS_PHASE:
924 dev_vdbg(dwc->dev, "Status Phase");
925 dwc3_ep0_complete_status(dwc, event);
928 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
932 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
933 struct dwc3_ep *dep, struct dwc3_request *req)
937 req->direction = !!dep->number;
939 if (req->request.length == 0) {
940 ret = dwc3_ep0_start_trans(dwc, dep->number,
941 dwc->ctrl_req_addr, 0,
942 DWC3_TRBCTL_CONTROL_DATA, 0);
943 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
944 (dep->number == 0)) {
945 u32 transfer_size = 0;
948 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
951 dev_dbg(dwc->dev, "failed to map request\n");
955 maxpacket = dep->endpoint.maxpacket;
956 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
957 transfer_size = (req->request.length / maxpacket) *
959 ret = dwc3_ep0_start_trans(dwc, dep->number,
962 DWC3_TRBCTL_CONTROL_DATA, 1);
965 transfer_size = roundup((req->request.length - transfer_size),
968 dwc->ep0_bounced = true;
971 * REVISIT in case request length is bigger than
972 * DWC3_EP0_BOUNCE_SIZE we will need two chained
973 * TRBs to handle the transfer.
975 ret = dwc3_ep0_start_trans(dwc, dep->number,
976 dwc->ep0_bounce_addr, transfer_size,
977 DWC3_TRBCTL_CONTROL_DATA, 0);
979 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
982 dev_dbg(dwc->dev, "failed to map request\n");
986 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
988 DWC3_TRBCTL_CONTROL_DATA, 0);
994 static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
996 struct dwc3 *dwc = dep->dwc;
999 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1000 : DWC3_TRBCTL_CONTROL_STATUS2;
1002 return dwc3_ep0_start_trans(dwc, dep->number,
1003 dwc->ctrl_req_addr, 0, type, 0);
1006 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1008 if (dwc->resize_fifos) {
1009 dev_dbg(dwc->dev, "Resizing FIFOs");
1010 dwc3_gadget_resize_tx_fifos(dwc);
1011 dwc->resize_fifos = 0;
1014 WARN_ON(dwc3_ep0_start_control_status(dep));
1017 static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1018 const struct dwc3_event_depevt *event)
1020 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1022 __dwc3_ep0_do_control_status(dwc, dep);
1025 static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1027 struct dwc3_gadget_ep_cmd_params params;
1031 if (!dep->resource_index)
1034 cmd = DWC3_DEPCMD_ENDTRANSFER;
1035 cmd |= DWC3_DEPCMD_CMDIOC;
1036 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1037 memset(¶ms, 0, sizeof(params));
1038 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1040 dep->resource_index = 0;
1043 static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1044 const struct dwc3_event_depevt *event)
1046 dwc->setup_packet_pending = true;
1048 switch (event->status) {
1049 case DEPEVT_STATUS_CONTROL_DATA:
1050 dev_vdbg(dwc->dev, "Control Data");
1053 * We already have a DATA transfer in the controller's cache,
1054 * if we receive a XferNotReady(DATA) we will ignore it, unless
1055 * it's for the wrong direction.
1057 * In that case, we must issue END_TRANSFER command to the Data
1058 * Phase we already have started and issue SetStall on the
1061 if (dwc->ep0_expect_in != event->endpoint_number) {
1062 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1064 dev_vdbg(dwc->dev, "Wrong direction for Data phase");
1065 dwc3_ep0_end_control_data(dwc, dep);
1066 dwc3_ep0_stall_and_restart(dwc);
1072 case DEPEVT_STATUS_CONTROL_STATUS:
1073 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1076 dev_vdbg(dwc->dev, "Control Status");
1078 dwc->ep0state = EP0_STATUS_PHASE;
1080 if (dwc->delayed_status) {
1081 WARN_ON_ONCE(event->endpoint_number != 1);
1082 dev_vdbg(dwc->dev, "Delayed Status");
1086 dwc3_ep0_do_control_status(dwc, event);
1090 void dwc3_ep0_interrupt(struct dwc3 *dwc,
1091 const struct dwc3_event_depevt *event)
1093 u8 epnum = event->endpoint_number;
1095 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'",
1096 dwc3_ep_event_string(event->endpoint_event),
1097 epnum >> 1, (epnum & 1) ? "in" : "out",
1098 dwc3_ep0_state_string(dwc->ep0state));
1100 switch (event->endpoint_event) {
1101 case DWC3_DEPEVT_XFERCOMPLETE:
1102 dwc3_ep0_xfer_complete(dwc, event);
1105 case DWC3_DEPEVT_XFERNOTREADY:
1106 dwc3_ep0_xfernotready(dwc, event);
1109 case DWC3_DEPEVT_XFERINPROGRESS:
1110 case DWC3_DEPEVT_RXTXFIFOEVT:
1111 case DWC3_DEPEVT_STREAMEVT:
1112 case DWC3_DEPEVT_EPCMDCMPLT: