1 // SPDX-License-Identifier: GPL-2.0
3 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://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
17 #include <dm/device_compat.h>
18 #include <linux/bug.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24 #include <linux/usb/composite.h>
30 #include "linux-compat.h"
32 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
33 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
34 struct dwc3_ep *dep, struct dwc3_request *req);
36 static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
45 case EP0_STATUS_PHASE:
46 return "Status Phase";
52 static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
53 u32 len, u32 type, unsigned chain)
55 struct dwc3_gadget_ep_cmd_params params;
61 dep = dwc->eps[epnum];
62 if (dep->flags & DWC3_EP_BUSY) {
63 dev_vdbg(dwc->dev, "%s still busy", dep->name);
67 trb = &dwc->ep0_trb[dep->free_slot];
72 trb->bpl = lower_32_bits(buf_dma);
73 trb->bph = upper_32_bits(buf_dma);
77 trb->ctrl |= (DWC3_TRB_CTRL_HWO
78 | DWC3_TRB_CTRL_ISP_IMI);
81 trb->ctrl |= DWC3_TRB_CTRL_CHN;
83 trb->ctrl |= (DWC3_TRB_CTRL_IOC
86 dwc3_flush_cache((uintptr_t)buf_dma, len);
87 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
92 memset(¶ms, 0, sizeof(params));
93 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
94 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
96 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
97 DWC3_DEPCMD_STARTTRANSFER, ¶ms);
99 dev_dbg(dwc->dev, "%s STARTTRANSFER failed", dep->name);
103 dep->flags |= DWC3_EP_BUSY;
104 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
107 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
112 static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
113 struct dwc3_request *req)
115 struct dwc3 *dwc = dep->dwc;
117 req->request.actual = 0;
118 req->request.status = -EINPROGRESS;
119 req->epnum = dep->number;
121 list_add_tail(&req->list, &dep->request_list);
124 * Gadget driver might not be quick enough to queue a request
125 * before we get a Transfer Not Ready event on this endpoint.
127 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
128 * flag is set, it's telling us that as soon as Gadget queues the
129 * required request, we should kick the transfer here because the
130 * IRQ we were waiting for is long gone.
132 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
135 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
137 if (dwc->ep0state != EP0_DATA_PHASE) {
138 dev_WARN(dwc->dev, "Unexpected pending request\n");
142 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
144 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
151 * In case gadget driver asked us to delay the STATUS phase,
154 if (dwc->delayed_status) {
157 direction = !dwc->ep0_expect_in;
158 dwc->delayed_status = false;
159 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
161 if (dwc->ep0state == EP0_STATUS_PHASE)
162 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
164 dev_dbg(dwc->dev, "too early for delayed status");
170 * Unfortunately we have uncovered a limitation wrt the Data Phase.
172 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
173 * come before issueing Start Transfer command, but if we do, we will
174 * miss situations where the host starts another SETUP phase instead of
175 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
176 * Layer Compliance Suite.
178 * The problem surfaces due to the fact that in case of back-to-back
179 * SETUP packets there will be no XferNotReady(DATA) generated and we
180 * will be stuck waiting for XferNotReady(DATA) forever.
182 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
183 * it tells us to start Data Phase right away. It also mentions that if
184 * we receive a SETUP phase instead of the DATA phase, core will issue
185 * XferComplete for the DATA phase, before actually initiating it in
186 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
187 * can only be used to print some debugging logs, as the core expects
188 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
189 * just so it completes right away, without transferring anything and,
190 * only then, we can go back to the SETUP phase.
192 * Because of this scenario, SNPS decided to change the programming
193 * model of control transfers and support on-demand transfers only for
194 * the STATUS phase. To fix the issue we have now, we will always wait
195 * for gadget driver to queue the DATA phase's struct usb_request, then
196 * start it right away.
198 * If we're actually in a 2-stage transfer, we will wait for
199 * XferNotReady(STATUS).
201 if (dwc->three_stage_setup) {
204 direction = dwc->ep0_expect_in;
205 dwc->ep0state = EP0_DATA_PHASE;
207 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
209 dep->flags &= ~DWC3_EP0_DIR_IN;
215 int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
218 struct dwc3_request *req = to_dwc3_request(request);
219 struct dwc3_ep *dep = to_dwc3_ep(ep);
220 struct dwc3 *dwc = dep->dwc;
226 spin_lock_irqsave(&dwc->lock, flags);
227 if (!dep->endpoint.desc) {
228 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s",
234 /* we share one TRB for ep0/1 */
235 if (!list_empty(&dep->request_list)) {
240 dev_vdbg(dwc->dev, "queueing request %p to %s length %d state '%s'",
241 request, dep->name, request->length,
242 dwc3_ep0_state_string(dwc->ep0state));
244 ret = __dwc3_gadget_ep0_queue(dep, req);
247 spin_unlock_irqrestore(&dwc->lock, flags);
252 static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
256 /* reinitialize physical ep1 */
258 dep->flags = DWC3_EP_ENABLED;
260 /* stall is always issued on EP0 */
262 __dwc3_gadget_ep_set_halt(dep, 1, false);
263 dep->flags = DWC3_EP_ENABLED;
264 dwc->delayed_status = false;
266 if (!list_empty(&dep->request_list)) {
267 struct dwc3_request *req;
269 req = next_request(&dep->request_list);
270 dwc3_gadget_giveback(dep, req, -ECONNRESET);
273 dwc->ep0state = EP0_SETUP_PHASE;
274 dwc3_ep0_out_start(dwc);
277 int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
279 struct dwc3_ep *dep = to_dwc3_ep(ep);
280 struct dwc3 *dwc = dep->dwc;
282 dwc3_ep0_stall_and_restart(dwc);
287 int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
292 spin_lock_irqsave(&dwc->lock, flags);
293 ret = __dwc3_gadget_ep0_set_halt(ep, value);
294 spin_unlock_irqrestore(&dwc->lock, flags);
299 void dwc3_ep0_out_start(struct dwc3 *dwc)
303 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
304 DWC3_TRBCTL_CONTROL_SETUP, 0);
308 static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
311 u32 windex = le16_to_cpu(wIndex_le);
314 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
315 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
318 dep = dwc->eps[epnum];
319 if (dep->flags & DWC3_EP_ENABLED)
325 static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
331 static int dwc3_ep0_handle_status(struct dwc3 *dwc,
332 struct usb_ctrlrequest *ctrl)
338 __le16 *response_pkt;
340 recip = ctrl->bRequestType & USB_RECIP_MASK;
342 case USB_RECIP_DEVICE:
344 * LTM will be set once we know how to set this in HW.
346 usb_status |= dwc->is_selfpowered << USB_DEVICE_SELF_POWERED;
348 if (dwc->speed == DWC3_DSTS_SUPERSPEED) {
349 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
350 if (reg & DWC3_DCTL_INITU1ENA)
351 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
352 if (reg & DWC3_DCTL_INITU2ENA)
353 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
358 case USB_RECIP_INTERFACE:
360 * Function Remote Wake Capable D0
361 * Function Remote Wakeup D1
365 case USB_RECIP_ENDPOINT:
366 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
370 if (dep->flags & DWC3_EP_STALL)
371 usb_status = 1 << USB_ENDPOINT_HALT;
377 response_pkt = (__le16 *) dwc->setup_buf;
378 *response_pkt = cpu_to_le16(usb_status);
381 dwc->ep0_usb_req.dep = dep;
382 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
383 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
384 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
386 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
389 static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
390 struct usb_ctrlrequest *ctrl, int set)
398 enum usb_device_state state;
400 wValue = le16_to_cpu(ctrl->wValue);
401 wIndex = le16_to_cpu(ctrl->wIndex);
402 recip = ctrl->bRequestType & USB_RECIP_MASK;
403 state = dwc->gadget.state;
406 case USB_RECIP_DEVICE:
409 case USB_DEVICE_REMOTE_WAKEUP:
412 * 9.4.1 says only only for SS, in AddressState only for
413 * default control pipe
415 case USB_DEVICE_U1_ENABLE:
416 if (state != USB_STATE_CONFIGURED)
418 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
421 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
423 reg |= DWC3_DCTL_INITU1ENA;
425 reg &= ~DWC3_DCTL_INITU1ENA;
426 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
429 case USB_DEVICE_U2_ENABLE:
430 if (state != USB_STATE_CONFIGURED)
432 if (dwc->speed != DWC3_DSTS_SUPERSPEED)
435 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
437 reg |= DWC3_DCTL_INITU2ENA;
439 reg &= ~DWC3_DCTL_INITU2ENA;
440 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
443 case USB_DEVICE_LTM_ENABLE:
446 case USB_DEVICE_TEST_MODE:
447 if ((wIndex & 0xff) != 0)
452 dwc->test_mode_nr = wIndex >> 8;
453 dwc->test_mode = true;
460 case USB_RECIP_INTERFACE:
462 case USB_INTRF_FUNC_SUSPEND:
463 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
464 /* XXX enable Low power suspend */
466 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
467 /* XXX enable remote wakeup */
475 case USB_RECIP_ENDPOINT:
477 case USB_ENDPOINT_HALT:
478 dep = dwc3_wIndex_to_dep(dwc, wIndex);
481 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
483 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
499 static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
501 enum usb_device_state state = dwc->gadget.state;
505 addr = le16_to_cpu(ctrl->wValue);
507 dev_dbg(dwc->dev, "invalid device address %d", addr);
511 if (state == USB_STATE_CONFIGURED) {
512 dev_dbg(dwc->dev, "trying to set address when configured");
516 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
517 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
518 reg |= DWC3_DCFG_DEVADDR(addr);
519 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
522 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
524 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
529 static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
533 spin_unlock(&dwc->lock);
534 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
535 spin_lock(&dwc->lock);
539 static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
541 enum usb_device_state state = dwc->gadget.state;
546 dwc->start_config_issued = false;
547 cfg = le16_to_cpu(ctrl->wValue);
550 case USB_STATE_DEFAULT:
553 case USB_STATE_ADDRESS:
554 ret = dwc3_ep0_delegate_req(dwc, ctrl);
555 /* if the cfg matches and the cfg is non zero */
556 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
559 * only change state if set_config has already
560 * been processed. If gadget driver returns
561 * USB_GADGET_DELAYED_STATUS, we will wait
562 * to change the state on the next usb_ep_queue()
565 usb_gadget_set_state(&dwc->gadget,
566 USB_STATE_CONFIGURED);
569 * Enable transition to U1/U2 state when
570 * nothing is pending from application.
572 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
573 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
574 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
576 dwc->resize_fifos = true;
577 dev_dbg(dwc->dev, "resize FIFOs flag SET");
581 case USB_STATE_CONFIGURED:
582 ret = dwc3_ep0_delegate_req(dwc, ctrl);
584 usb_gadget_set_state(&dwc->gadget,
593 static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
595 struct dwc3_ep *dep = to_dwc3_ep(ep);
596 struct dwc3 *dwc = dep->dwc;
610 memcpy(&timing, req->buf, sizeof(timing));
612 dwc->u1sel = timing.u1sel;
613 dwc->u1pel = timing.u1pel;
614 dwc->u2sel = le16_to_cpu(timing.u2sel);
615 dwc->u2pel = le16_to_cpu(timing.u2pel);
617 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
618 if (reg & DWC3_DCTL_INITU2ENA)
620 if (reg & DWC3_DCTL_INITU1ENA)
624 * According to Synopsys Databook, if parameter is
625 * greater than 125, a value of zero should be
626 * programmed in the register.
631 /* now that we have the time, issue DGCMD Set Sel */
632 ret = dwc3_send_gadget_generic_command(dwc,
633 DWC3_DGCMD_SET_PERIODIC_PAR, param);
637 static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
640 enum usb_device_state state = dwc->gadget.state;
643 if (state == USB_STATE_DEFAULT)
646 wLength = le16_to_cpu(ctrl->wLength);
649 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
655 * To handle Set SEL we need to receive 6 bytes from Host. So let's
656 * queue a usb_request for 6 bytes.
658 * Remember, though, this controller can't handle non-wMaxPacketSize
659 * aligned transfers on the OUT direction, so we queue a request for
660 * wMaxPacketSize instead.
663 dwc->ep0_usb_req.dep = dep;
664 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
665 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
666 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
668 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
671 static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
677 wValue = le16_to_cpu(ctrl->wValue);
678 wLength = le16_to_cpu(ctrl->wLength);
679 wIndex = le16_to_cpu(ctrl->wIndex);
681 if (wIndex || wLength)
685 * REVISIT It's unclear from Databook what to do with this
686 * value. For now, just cache it.
688 dwc->isoch_delay = wValue;
693 static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
697 switch (ctrl->bRequest) {
698 case USB_REQ_GET_STATUS:
699 dev_vdbg(dwc->dev, "USB_REQ_GET_STATUS");
700 ret = dwc3_ep0_handle_status(dwc, ctrl);
702 case USB_REQ_CLEAR_FEATURE:
703 dev_vdbg(dwc->dev, "USB_REQ_CLEAR_FEATURE");
704 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
706 case USB_REQ_SET_FEATURE:
707 dev_vdbg(dwc->dev, "USB_REQ_SET_FEATURE");
708 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
710 case USB_REQ_SET_ADDRESS:
711 dev_vdbg(dwc->dev, "USB_REQ_SET_ADDRESS");
712 ret = dwc3_ep0_set_address(dwc, ctrl);
714 case USB_REQ_SET_CONFIGURATION:
715 dev_vdbg(dwc->dev, "USB_REQ_SET_CONFIGURATION");
716 ret = dwc3_ep0_set_config(dwc, ctrl);
718 case USB_REQ_SET_SEL:
719 dev_vdbg(dwc->dev, "USB_REQ_SET_SEL");
720 ret = dwc3_ep0_set_sel(dwc, ctrl);
722 case USB_REQ_SET_ISOCH_DELAY:
723 dev_vdbg(dwc->dev, "USB_REQ_SET_ISOCH_DELAY");
724 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
727 dev_vdbg(dwc->dev, "Forwarding to gadget driver");
728 ret = dwc3_ep0_delegate_req(dwc, ctrl);
735 static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
736 const struct dwc3_event_depevt *event)
738 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
742 if (!dwc->gadget_driver)
745 len = le16_to_cpu(ctrl->wLength);
747 dwc->three_stage_setup = false;
748 dwc->ep0_expect_in = false;
749 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
751 dwc->three_stage_setup = true;
752 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
753 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
756 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
757 ret = dwc3_ep0_std_request(dwc, ctrl);
759 ret = dwc3_ep0_delegate_req(dwc, ctrl);
761 if (ret == USB_GADGET_DELAYED_STATUS)
762 dwc->delayed_status = true;
766 dwc3_ep0_stall_and_restart(dwc);
769 static void dwc3_ep0_complete_data(struct dwc3 *dwc,
770 const struct dwc3_event_depevt *event)
772 struct dwc3_request *r = NULL;
773 struct usb_request *ur;
774 struct dwc3_trb *trb;
776 unsigned transfer_size = 0;
784 epnum = event->endpoint_number;
787 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
791 r = next_request(&ep0->request_list);
795 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
797 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
798 if (status == DWC3_TRBSTS_SETUP_PENDING) {
799 dev_dbg(dwc->dev, "Setup Pending received");
802 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
810 length = trb->size & DWC3_TRB_SIZE_MASK;
812 maxp = ep0->endpoint.maxpacket;
814 if (dwc->ep0_bounced) {
816 * Handle the first TRB before handling the bounce buffer if
817 * the request length is greater than the bounce buffer size.
819 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
820 transfer_size = (ur->length / maxp) * maxp;
821 transferred = transfer_size - length;
822 buf = (u8 *)buf + transferred;
823 ur->actual += transferred;
826 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
827 length = trb->size & DWC3_TRB_SIZE_MASK;
832 transfer_size = roundup((ur->length - transfer_size),
834 transferred = min_t(u32, ur->length - transferred,
835 transfer_size - length);
836 dwc3_flush_cache((uintptr_t)dwc->ep0_bounce, DWC3_EP0_BOUNCE_SIZE);
837 memcpy(buf, dwc->ep0_bounce, transferred);
839 transferred = ur->length - length;
842 ur->actual += transferred;
844 if ((epnum & 1) && ur->actual < ur->length) {
845 /* for some reason we did not get everything out */
847 dwc3_ep0_stall_and_restart(dwc);
849 dwc3_gadget_giveback(ep0, r, 0);
851 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
852 ur->length && ur->zero) {
855 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
857 ret = dwc3_ep0_start_trans(dwc, epnum,
858 dwc->ctrl_req_addr, 0,
859 DWC3_TRBCTL_CONTROL_DATA, 0);
865 static void dwc3_ep0_complete_status(struct dwc3 *dwc,
866 const struct dwc3_event_depevt *event)
868 struct dwc3_request *r;
870 struct dwc3_trb *trb;
876 if (!list_empty(&dep->request_list)) {
877 r = next_request(&dep->request_list);
879 dwc3_gadget_giveback(dep, r, 0);
882 if (dwc->test_mode) {
885 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
887 dev_dbg(dwc->dev, "Invalid Test #%d",
889 dwc3_ep0_stall_and_restart(dwc);
894 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
895 if (status == DWC3_TRBSTS_SETUP_PENDING)
896 dev_dbg(dwc->dev, "Setup Pending received");
898 dwc->ep0state = EP0_SETUP_PHASE;
899 dwc3_ep0_out_start(dwc);
902 static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
903 const struct dwc3_event_depevt *event)
905 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
907 dep->flags &= ~DWC3_EP_BUSY;
908 dep->resource_index = 0;
909 dwc->setup_packet_pending = false;
911 switch (dwc->ep0state) {
912 case EP0_SETUP_PHASE:
913 dev_vdbg(dwc->dev, "Setup Phase");
914 dwc3_ep0_inspect_setup(dwc, event);
918 dev_vdbg(dwc->dev, "Data Phase");
919 dwc3_ep0_complete_data(dwc, event);
922 case EP0_STATUS_PHASE:
923 dev_vdbg(dwc->dev, "Status Phase");
924 dwc3_ep0_complete_status(dwc, event);
927 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
931 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
932 struct dwc3_ep *dep, struct dwc3_request *req)
936 req->direction = !!dep->number;
938 if (req->request.length == 0) {
939 ret = dwc3_ep0_start_trans(dwc, dep->number,
940 dwc->ctrl_req_addr, 0,
941 DWC3_TRBCTL_CONTROL_DATA, 0);
942 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
943 (dep->number == 0)) {
944 u32 transfer_size = 0;
947 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
950 dev_dbg(dwc->dev, "failed to map request\n");
954 maxpacket = dep->endpoint.maxpacket;
955 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
956 transfer_size = (req->request.length / maxpacket) *
958 ret = dwc3_ep0_start_trans(dwc, dep->number,
961 DWC3_TRBCTL_CONTROL_DATA, 1);
964 transfer_size = roundup((req->request.length - transfer_size),
967 dwc->ep0_bounced = true;
970 * REVISIT in case request length is bigger than
971 * DWC3_EP0_BOUNCE_SIZE we will need two chained
972 * TRBs to handle the transfer.
974 ret = dwc3_ep0_start_trans(dwc, dep->number,
975 dwc->ep0_bounce_addr, transfer_size,
976 DWC3_TRBCTL_CONTROL_DATA, 0);
978 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
981 dev_dbg(dwc->dev, "failed to map request\n");
985 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
987 DWC3_TRBCTL_CONTROL_DATA, 0);
993 static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
995 struct dwc3 *dwc = dep->dwc;
998 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
999 : DWC3_TRBCTL_CONTROL_STATUS2;
1001 return dwc3_ep0_start_trans(dwc, dep->number,
1002 dwc->ctrl_req_addr, 0, type, 0);
1005 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1007 if (dwc->resize_fifos) {
1008 dev_dbg(dwc->dev, "Resizing FIFOs");
1009 dwc3_gadget_resize_tx_fifos(dwc);
1010 dwc->resize_fifos = 0;
1013 WARN_ON(dwc3_ep0_start_control_status(dep));
1016 static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1017 const struct dwc3_event_depevt *event)
1019 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1021 __dwc3_ep0_do_control_status(dwc, dep);
1024 static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1026 struct dwc3_gadget_ep_cmd_params params;
1030 if (!dep->resource_index)
1033 cmd = DWC3_DEPCMD_ENDTRANSFER;
1034 cmd |= DWC3_DEPCMD_CMDIOC;
1035 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1036 memset(¶ms, 0, sizeof(params));
1037 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1039 dep->resource_index = 0;
1042 static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1043 const struct dwc3_event_depevt *event)
1045 dwc->setup_packet_pending = true;
1047 switch (event->status) {
1048 case DEPEVT_STATUS_CONTROL_DATA:
1049 dev_vdbg(dwc->dev, "Control Data");
1052 * We already have a DATA transfer in the controller's cache,
1053 * if we receive a XferNotReady(DATA) we will ignore it, unless
1054 * it's for the wrong direction.
1056 * In that case, we must issue END_TRANSFER command to the Data
1057 * Phase we already have started and issue SetStall on the
1060 if (dwc->ep0_expect_in != event->endpoint_number) {
1061 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1063 dev_vdbg(dwc->dev, "Wrong direction for Data phase");
1064 dwc3_ep0_end_control_data(dwc, dep);
1065 dwc3_ep0_stall_and_restart(dwc);
1071 case DEPEVT_STATUS_CONTROL_STATUS:
1072 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1075 dev_vdbg(dwc->dev, "Control Status");
1077 dwc->ep0state = EP0_STATUS_PHASE;
1079 if (dwc->delayed_status) {
1080 WARN_ON_ONCE(event->endpoint_number != 1);
1081 dev_vdbg(dwc->dev, "Delayed Status");
1085 dwc3_ep0_do_control_status(dwc, event);
1089 void dwc3_ep0_interrupt(struct dwc3 *dwc,
1090 const struct dwc3_event_depevt *event)
1092 u8 epnum = event->endpoint_number;
1094 dev_dbg(dwc->dev, "%s while ep%d%s in state '%s'",
1095 dwc3_ep_event_string(event->endpoint_event),
1096 epnum >> 1, (epnum & 1) ? "in" : "out",
1097 dwc3_ep0_state_string(dwc->ep0state));
1099 switch (event->endpoint_event) {
1100 case DWC3_DEPEVT_XFERCOMPLETE:
1101 dwc3_ep0_xfer_complete(dwc, event);
1104 case DWC3_DEPEVT_XFERNOTREADY:
1105 dwc3_ep0_xfernotready(dwc, event);
1108 case DWC3_DEPEVT_XFERINPROGRESS:
1109 case DWC3_DEPEVT_RXTXFIFOEVT:
1110 case DWC3_DEPEVT_STREAMEVT:
1111 case DWC3_DEPEVT_EPCMDCMPLT: