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,
59 u32 len, u32 type, bool chain)
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);
73 trb = &dwc->ep0_trb[dep->trb_enqueue];
78 trb->bpl = lower_32_bits(buf_dma);
79 trb->bph = upper_32_bits(buf_dma);
83 trb->ctrl |= (DWC3_TRB_CTRL_HWO
84 | DWC3_TRB_CTRL_ISP_IMI);
87 trb->ctrl |= DWC3_TRB_CTRL_CHN;
89 trb->ctrl |= (DWC3_TRB_CTRL_IOC
95 memset(¶ms, 0, sizeof(params));
96 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
97 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
99 trace_dwc3_prepare_trb(dep, trb);
101 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, ¶ms);
103 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
108 dep->flags |= DWC3_EP_BUSY;
109 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
110 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
115 static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
116 struct dwc3_request *req)
118 struct dwc3 *dwc = dep->dwc;
120 req->request.actual = 0;
121 req->request.status = -EINPROGRESS;
122 req->epnum = dep->number;
124 list_add_tail(&req->list, &dep->pending_list);
127 * Gadget driver might not be quick enough to queue a request
128 * before we get a Transfer Not Ready event on this endpoint.
130 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
131 * flag is set, it's telling us that as soon as Gadget queues the
132 * required request, we should kick the transfer here because the
133 * IRQ we were waiting for is long gone.
135 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
138 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
140 if (dwc->ep0state != EP0_DATA_PHASE) {
141 dev_WARN(dwc->dev, "Unexpected pending request\n");
145 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
147 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
154 * In case gadget driver asked us to delay the STATUS phase,
157 if (dwc->delayed_status) {
160 direction = !dwc->ep0_expect_in;
161 dwc->delayed_status = false;
162 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
164 if (dwc->ep0state == EP0_STATUS_PHASE)
165 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
167 dwc3_trace(trace_dwc3_ep0,
168 "too early for delayed status");
174 * Unfortunately we have uncovered a limitation wrt the Data Phase.
176 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
177 * come before issueing Start Transfer command, but if we do, we will
178 * miss situations where the host starts another SETUP phase instead of
179 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
180 * Layer Compliance Suite.
182 * The problem surfaces due to the fact that in case of back-to-back
183 * SETUP packets there will be no XferNotReady(DATA) generated and we
184 * will be stuck waiting for XferNotReady(DATA) forever.
186 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
187 * it tells us to start Data Phase right away. It also mentions that if
188 * we receive a SETUP phase instead of the DATA phase, core will issue
189 * XferComplete for the DATA phase, before actually initiating it in
190 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
191 * can only be used to print some debugging logs, as the core expects
192 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
193 * just so it completes right away, without transferring anything and,
194 * only then, we can go back to the SETUP phase.
196 * Because of this scenario, SNPS decided to change the programming
197 * model of control transfers and support on-demand transfers only for
198 * the STATUS phase. To fix the issue we have now, we will always wait
199 * for gadget driver to queue the DATA phase's struct usb_request, then
200 * start it right away.
202 * If we're actually in a 2-stage transfer, we will wait for
203 * XferNotReady(STATUS).
205 if (dwc->three_stage_setup) {
208 direction = dwc->ep0_expect_in;
209 dwc->ep0state = EP0_DATA_PHASE;
211 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
213 dep->flags &= ~DWC3_EP0_DIR_IN;
219 int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
222 struct dwc3_request *req = to_dwc3_request(request);
223 struct dwc3_ep *dep = to_dwc3_ep(ep);
224 struct dwc3 *dwc = dep->dwc;
230 spin_lock_irqsave(&dwc->lock, flags);
231 if (!dep->endpoint.desc) {
232 dwc3_trace(trace_dwc3_ep0,
233 "trying to queue request %p to disabled %s",
239 /* we share one TRB for ep0/1 */
240 if (!list_empty(&dep->pending_list)) {
245 dwc3_trace(trace_dwc3_ep0,
246 "queueing request %p to %s length %d state '%s'",
247 request, dep->name, request->length,
248 dwc3_ep0_state_string(dwc->ep0state));
250 ret = __dwc3_gadget_ep0_queue(dep, req);
253 spin_unlock_irqrestore(&dwc->lock, flags);
258 static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
262 /* reinitialize physical ep1 */
264 dep->flags = DWC3_EP_ENABLED;
266 /* stall is always issued on EP0 */
268 __dwc3_gadget_ep_set_halt(dep, 1, false);
269 dep->flags = DWC3_EP_ENABLED;
270 dwc->delayed_status = false;
272 if (!list_empty(&dep->pending_list)) {
273 struct dwc3_request *req;
275 req = next_request(&dep->pending_list);
276 dwc3_gadget_giveback(dep, req, -ECONNRESET);
279 dwc->ep0state = EP0_SETUP_PHASE;
280 dwc3_ep0_out_start(dwc);
283 int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
285 struct dwc3_ep *dep = to_dwc3_ep(ep);
286 struct dwc3 *dwc = dep->dwc;
288 dwc3_ep0_stall_and_restart(dwc);
293 int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
295 struct dwc3_ep *dep = to_dwc3_ep(ep);
296 struct dwc3 *dwc = dep->dwc;
300 spin_lock_irqsave(&dwc->lock, flags);
301 ret = __dwc3_gadget_ep0_set_halt(ep, value);
302 spin_unlock_irqrestore(&dwc->lock, flags);
307 void dwc3_ep0_out_start(struct dwc3 *dwc)
311 ret = dwc3_ep0_start_trans(dwc, 0, dwc->ctrl_req_addr, 8,
312 DWC3_TRBCTL_CONTROL_SETUP, false);
316 static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
319 u32 windex = le16_to_cpu(wIndex_le);
322 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
323 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
326 dep = dwc->eps[epnum];
327 if (dep->flags & DWC3_EP_ENABLED)
333 static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
339 static int dwc3_ep0_handle_status(struct dwc3 *dwc,
340 struct usb_ctrlrequest *ctrl)
346 __le16 *response_pkt;
348 recip = ctrl->bRequestType & USB_RECIP_MASK;
350 case USB_RECIP_DEVICE:
352 * LTM will be set once we know how to set this in HW.
354 usb_status |= dwc->gadget.is_selfpowered;
356 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
357 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
358 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
359 if (reg & DWC3_DCTL_INITU1ENA)
360 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
361 if (reg & DWC3_DCTL_INITU2ENA)
362 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
367 case USB_RECIP_INTERFACE:
369 * Function Remote Wake Capable D0
370 * Function Remote Wakeup D1
374 case USB_RECIP_ENDPOINT:
375 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
379 if (dep->flags & DWC3_EP_STALL)
380 usb_status = 1 << USB_ENDPOINT_HALT;
386 response_pkt = (__le16 *) dwc->setup_buf;
387 *response_pkt = cpu_to_le16(usb_status);
390 dwc->ep0_usb_req.dep = dep;
391 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
392 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
393 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
395 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
398 static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
399 struct usb_ctrlrequest *ctrl, int set)
407 enum usb_device_state state;
409 wValue = le16_to_cpu(ctrl->wValue);
410 wIndex = le16_to_cpu(ctrl->wIndex);
411 recip = ctrl->bRequestType & USB_RECIP_MASK;
412 state = dwc->gadget.state;
415 case USB_RECIP_DEVICE:
418 case USB_DEVICE_REMOTE_WAKEUP:
421 * 9.4.1 says only only for SS, in AddressState only for
422 * default control pipe
424 case USB_DEVICE_U1_ENABLE:
425 if (state != USB_STATE_CONFIGURED)
427 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
428 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
431 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
433 reg |= DWC3_DCTL_INITU1ENA;
435 reg &= ~DWC3_DCTL_INITU1ENA;
436 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
439 case USB_DEVICE_U2_ENABLE:
440 if (state != USB_STATE_CONFIGURED)
442 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
443 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
446 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
448 reg |= DWC3_DCTL_INITU2ENA;
450 reg &= ~DWC3_DCTL_INITU2ENA;
451 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
454 case USB_DEVICE_LTM_ENABLE:
457 case USB_DEVICE_TEST_MODE:
458 if ((wIndex & 0xff) != 0)
463 switch (wIndex >> 8) {
469 dwc->test_mode_nr = wIndex >> 8;
470 dwc->test_mode = true;
481 case USB_RECIP_INTERFACE:
483 case USB_INTRF_FUNC_SUSPEND:
484 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
485 /* XXX enable Low power suspend */
487 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
488 /* XXX enable remote wakeup */
496 case USB_RECIP_ENDPOINT:
498 case USB_ENDPOINT_HALT:
499 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
502 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
504 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
520 static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
522 enum usb_device_state state = dwc->gadget.state;
526 addr = le16_to_cpu(ctrl->wValue);
528 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
532 if (state == USB_STATE_CONFIGURED) {
533 dwc3_trace(trace_dwc3_ep0,
534 "trying to set address when configured");
538 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
539 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
540 reg |= DWC3_DCFG_DEVADDR(addr);
541 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
544 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
546 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
551 static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
555 spin_unlock(&dwc->lock);
556 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
557 spin_lock(&dwc->lock);
561 static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
563 enum usb_device_state state = dwc->gadget.state;
568 cfg = le16_to_cpu(ctrl->wValue);
571 case USB_STATE_DEFAULT:
574 case USB_STATE_ADDRESS:
575 ret = dwc3_ep0_delegate_req(dwc, ctrl);
576 /* if the cfg matches and the cfg is non zero */
577 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
580 * only change state if set_config has already
581 * been processed. If gadget driver returns
582 * USB_GADGET_DELAYED_STATUS, we will wait
583 * to change the state on the next usb_ep_queue()
586 usb_gadget_set_state(&dwc->gadget,
587 USB_STATE_CONFIGURED);
590 * Enable transition to U1/U2 state when
591 * nothing is pending from application.
593 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
594 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
595 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
599 case USB_STATE_CONFIGURED:
600 ret = dwc3_ep0_delegate_req(dwc, ctrl);
602 usb_gadget_set_state(&dwc->gadget,
611 static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
613 struct dwc3_ep *dep = to_dwc3_ep(ep);
614 struct dwc3 *dwc = dep->dwc;
628 memcpy(&timing, req->buf, sizeof(timing));
630 dwc->u1sel = timing.u1sel;
631 dwc->u1pel = timing.u1pel;
632 dwc->u2sel = le16_to_cpu(timing.u2sel);
633 dwc->u2pel = le16_to_cpu(timing.u2pel);
635 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
636 if (reg & DWC3_DCTL_INITU2ENA)
638 if (reg & DWC3_DCTL_INITU1ENA)
642 * According to Synopsys Databook, if parameter is
643 * greater than 125, a value of zero should be
644 * programmed in the register.
649 /* now that we have the time, issue DGCMD Set Sel */
650 ret = dwc3_send_gadget_generic_command(dwc,
651 DWC3_DGCMD_SET_PERIODIC_PAR, param);
655 static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
658 enum usb_device_state state = dwc->gadget.state;
662 if (state == USB_STATE_DEFAULT)
665 wValue = le16_to_cpu(ctrl->wValue);
666 wLength = le16_to_cpu(ctrl->wLength);
669 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
675 * To handle Set SEL we need to receive 6 bytes from Host. So let's
676 * queue a usb_request for 6 bytes.
678 * Remember, though, this controller can't handle non-wMaxPacketSize
679 * aligned transfers on the OUT direction, so we queue a request for
680 * wMaxPacketSize instead.
683 dwc->ep0_usb_req.dep = dep;
684 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
685 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
686 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
688 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
691 static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
697 wValue = le16_to_cpu(ctrl->wValue);
698 wLength = le16_to_cpu(ctrl->wLength);
699 wIndex = le16_to_cpu(ctrl->wIndex);
701 if (wIndex || wLength)
705 * REVISIT It's unclear from Databook what to do with this
706 * value. For now, just cache it.
708 dwc->isoch_delay = wValue;
713 static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
717 switch (ctrl->bRequest) {
718 case USB_REQ_GET_STATUS:
719 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
720 ret = dwc3_ep0_handle_status(dwc, ctrl);
722 case USB_REQ_CLEAR_FEATURE:
723 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
724 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
726 case USB_REQ_SET_FEATURE:
727 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
728 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
730 case USB_REQ_SET_ADDRESS:
731 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
732 ret = dwc3_ep0_set_address(dwc, ctrl);
734 case USB_REQ_SET_CONFIGURATION:
735 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
736 ret = dwc3_ep0_set_config(dwc, ctrl);
738 case USB_REQ_SET_SEL:
739 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
740 ret = dwc3_ep0_set_sel(dwc, ctrl);
742 case USB_REQ_SET_ISOCH_DELAY:
743 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
744 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
747 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
748 ret = dwc3_ep0_delegate_req(dwc, ctrl);
755 static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
756 const struct dwc3_event_depevt *event)
758 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
762 if (!dwc->gadget_driver)
765 trace_dwc3_ctrl_req(ctrl);
767 len = le16_to_cpu(ctrl->wLength);
769 dwc->three_stage_setup = false;
770 dwc->ep0_expect_in = false;
771 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
773 dwc->three_stage_setup = true;
774 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
775 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
778 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
779 ret = dwc3_ep0_std_request(dwc, ctrl);
781 ret = dwc3_ep0_delegate_req(dwc, ctrl);
783 if (ret == USB_GADGET_DELAYED_STATUS)
784 dwc->delayed_status = true;
788 dwc3_ep0_stall_and_restart(dwc);
791 static void dwc3_ep0_complete_data(struct dwc3 *dwc,
792 const struct dwc3_event_depevt *event)
794 struct dwc3_request *r = NULL;
795 struct usb_request *ur;
796 struct dwc3_trb *trb;
798 unsigned transfer_size = 0;
800 unsigned remaining_ur_length;
807 epnum = event->endpoint_number;
810 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
814 trace_dwc3_complete_trb(ep0, trb);
816 r = next_request(&ep0->pending_list);
820 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
821 if (status == DWC3_TRBSTS_SETUP_PENDING) {
822 dwc->setup_packet_pending = true;
824 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
827 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
834 remaining_ur_length = ur->length;
836 length = trb->size & DWC3_TRB_SIZE_MASK;
838 maxp = ep0->endpoint.maxpacket;
840 if (dwc->ep0_bounced) {
842 * Handle the first TRB before handling the bounce buffer if
843 * the request length is greater than the bounce buffer size
845 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
846 transfer_size = ALIGN(ur->length - maxp, maxp);
847 transferred = transfer_size - length;
848 buf = (u8 *)buf + transferred;
849 ur->actual += transferred;
850 remaining_ur_length -= transferred;
853 length = trb->size & DWC3_TRB_SIZE_MASK;
855 ep0->trb_enqueue = 0;
858 transfer_size = roundup((ur->length - transfer_size),
861 transferred = min_t(u32, remaining_ur_length,
862 transfer_size - length);
863 memcpy(buf, dwc->ep0_bounce, transferred);
865 transferred = ur->length - length;
868 ur->actual += transferred;
870 if ((epnum & 1) && ur->actual < ur->length) {
871 /* for some reason we did not get everything out */
873 dwc3_ep0_stall_and_restart(dwc);
875 dwc3_gadget_giveback(ep0, r, 0);
877 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
878 ur->length && ur->zero) {
881 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
883 ret = dwc3_ep0_start_trans(dwc, epnum,
884 dwc->ctrl_req_addr, 0,
885 DWC3_TRBCTL_CONTROL_DATA, false);
891 static void dwc3_ep0_complete_status(struct dwc3 *dwc,
892 const struct dwc3_event_depevt *event)
894 struct dwc3_request *r;
896 struct dwc3_trb *trb;
902 trace_dwc3_complete_trb(dep, trb);
904 if (!list_empty(&dep->pending_list)) {
905 r = next_request(&dep->pending_list);
907 dwc3_gadget_giveback(dep, r, 0);
910 if (dwc->test_mode) {
913 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
915 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
917 dwc3_ep0_stall_and_restart(dwc);
922 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
923 if (status == DWC3_TRBSTS_SETUP_PENDING) {
924 dwc->setup_packet_pending = true;
925 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
928 dwc->ep0state = EP0_SETUP_PHASE;
929 dwc3_ep0_out_start(dwc);
932 static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
933 const struct dwc3_event_depevt *event)
935 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
937 dep->flags &= ~DWC3_EP_BUSY;
938 dep->resource_index = 0;
939 dwc->setup_packet_pending = false;
941 switch (dwc->ep0state) {
942 case EP0_SETUP_PHASE:
943 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
944 dwc3_ep0_inspect_setup(dwc, event);
948 dwc3_trace(trace_dwc3_ep0, "Data Phase");
949 dwc3_ep0_complete_data(dwc, event);
952 case EP0_STATUS_PHASE:
953 dwc3_trace(trace_dwc3_ep0, "Status Phase");
954 dwc3_ep0_complete_status(dwc, event);
957 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
961 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
962 struct dwc3_ep *dep, struct dwc3_request *req)
966 req->direction = !!dep->number;
968 if (req->request.length == 0) {
969 ret = dwc3_ep0_start_trans(dwc, dep->number,
970 dwc->ctrl_req_addr, 0,
971 DWC3_TRBCTL_CONTROL_DATA, false);
972 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
973 && (dep->number == 0)) {
974 u32 transfer_size = 0;
977 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
980 dwc3_trace(trace_dwc3_ep0, "failed to map request");
984 maxpacket = dep->endpoint.maxpacket;
986 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
987 transfer_size = ALIGN(req->request.length - maxpacket,
989 ret = dwc3_ep0_start_trans(dwc, dep->number,
992 DWC3_TRBCTL_CONTROL_DATA,
996 transfer_size = roundup((req->request.length - transfer_size),
999 dwc->ep0_bounced = true;
1001 ret = dwc3_ep0_start_trans(dwc, dep->number,
1002 dwc->ep0_bounce_addr, transfer_size,
1003 DWC3_TRBCTL_CONTROL_DATA, false);
1005 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1008 dwc3_trace(trace_dwc3_ep0, "failed to map request");
1012 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
1013 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1020 static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
1022 struct dwc3 *dwc = dep->dwc;
1025 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1026 : DWC3_TRBCTL_CONTROL_STATUS2;
1028 return dwc3_ep0_start_trans(dwc, dep->number,
1029 dwc->ctrl_req_addr, 0, type, false);
1032 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1034 WARN_ON(dwc3_ep0_start_control_status(dep));
1037 static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1038 const struct dwc3_event_depevt *event)
1040 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1042 __dwc3_ep0_do_control_status(dwc, dep);
1045 static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1047 struct dwc3_gadget_ep_cmd_params params;
1051 if (!dep->resource_index)
1054 cmd = DWC3_DEPCMD_ENDTRANSFER;
1055 cmd |= DWC3_DEPCMD_CMDIOC;
1056 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1057 memset(¶ms, 0, sizeof(params));
1058 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1060 dep->resource_index = 0;
1063 static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1064 const struct dwc3_event_depevt *event)
1066 switch (event->status) {
1067 case DEPEVT_STATUS_CONTROL_DATA:
1068 dwc3_trace(trace_dwc3_ep0, "Control Data");
1071 * We already have a DATA transfer in the controller's cache,
1072 * if we receive a XferNotReady(DATA) we will ignore it, unless
1073 * it's for the wrong direction.
1075 * In that case, we must issue END_TRANSFER command to the Data
1076 * Phase we already have started and issue SetStall on the
1079 if (dwc->ep0_expect_in != event->endpoint_number) {
1080 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1082 dwc3_trace(trace_dwc3_ep0,
1083 "Wrong direction for Data phase");
1084 dwc3_ep0_end_control_data(dwc, dep);
1085 dwc3_ep0_stall_and_restart(dwc);
1091 case DEPEVT_STATUS_CONTROL_STATUS:
1092 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1095 dwc3_trace(trace_dwc3_ep0, "Control Status");
1097 dwc->ep0state = EP0_STATUS_PHASE;
1099 if (dwc->delayed_status) {
1100 WARN_ON_ONCE(event->endpoint_number != 1);
1101 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
1105 dwc3_ep0_do_control_status(dwc, event);
1109 void dwc3_ep0_interrupt(struct dwc3 *dwc,
1110 const struct dwc3_event_depevt *event)
1112 dwc3_trace(trace_dwc3_ep0, "%s: state '%s'",
1113 dwc3_ep_event_string(event),
1114 dwc3_ep0_state_string(dwc->ep0state));
1116 switch (event->endpoint_event) {
1117 case DWC3_DEPEVT_XFERCOMPLETE:
1118 dwc3_ep0_xfer_complete(dwc, event);
1121 case DWC3_DEPEVT_XFERNOTREADY:
1122 dwc3_ep0_xfernotready(dwc, event);
1125 case DWC3_DEPEVT_XFERINPROGRESS:
1126 case DWC3_DEPEVT_RXTXFIFOEVT:
1127 case DWC3_DEPEVT_STREAMEVT:
1128 case DWC3_DEPEVT_EPCMDCMPLT: