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 void dwc3_ep0_prepare_one_trb(struct dwc3_ep *dep,
43 dma_addr_t buf_dma, u32 len, u32 type, bool chain)
49 trb = &dwc->ep0_trb[dep->trb_enqueue];
54 trb->bpl = lower_32_bits(buf_dma);
55 trb->bph = upper_32_bits(buf_dma);
59 trb->ctrl |= (DWC3_TRB_CTRL_HWO
60 | DWC3_TRB_CTRL_ISP_IMI);
63 trb->ctrl |= DWC3_TRB_CTRL_CHN;
65 trb->ctrl |= (DWC3_TRB_CTRL_IOC
68 trace_dwc3_prepare_trb(dep, trb);
71 static int dwc3_ep0_start_trans(struct dwc3_ep *dep)
73 struct dwc3_gadget_ep_cmd_params params;
77 if (dep->flags & DWC3_EP_BUSY)
82 memset(¶ms, 0, sizeof(params));
83 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
84 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
86 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, ¶ms);
90 dep->flags |= DWC3_EP_BUSY;
91 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
92 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
97 static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
98 struct dwc3_request *req)
100 struct dwc3 *dwc = dep->dwc;
102 req->request.actual = 0;
103 req->request.status = -EINPROGRESS;
104 req->epnum = dep->number;
106 list_add_tail(&req->list, &dep->pending_list);
109 * Gadget driver might not be quick enough to queue a request
110 * before we get a Transfer Not Ready event on this endpoint.
112 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
113 * flag is set, it's telling us that as soon as Gadget queues the
114 * required request, we should kick the transfer here because the
115 * IRQ we were waiting for is long gone.
117 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
120 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
122 if (dwc->ep0state != EP0_DATA_PHASE) {
123 dev_WARN(dwc->dev, "Unexpected pending request\n");
127 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
129 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
136 * In case gadget driver asked us to delay the STATUS phase,
139 if (dwc->delayed_status) {
142 direction = !dwc->ep0_expect_in;
143 dwc->delayed_status = false;
144 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
146 if (dwc->ep0state == EP0_STATUS_PHASE)
147 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
153 * Unfortunately we have uncovered a limitation wrt the Data Phase.
155 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
156 * come before issueing Start Transfer command, but if we do, we will
157 * miss situations where the host starts another SETUP phase instead of
158 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
159 * Layer Compliance Suite.
161 * The problem surfaces due to the fact that in case of back-to-back
162 * SETUP packets there will be no XferNotReady(DATA) generated and we
163 * will be stuck waiting for XferNotReady(DATA) forever.
165 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
166 * it tells us to start Data Phase right away. It also mentions that if
167 * we receive a SETUP phase instead of the DATA phase, core will issue
168 * XferComplete for the DATA phase, before actually initiating it in
169 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
170 * can only be used to print some debugging logs, as the core expects
171 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
172 * just so it completes right away, without transferring anything and,
173 * only then, we can go back to the SETUP phase.
175 * Because of this scenario, SNPS decided to change the programming
176 * model of control transfers and support on-demand transfers only for
177 * the STATUS phase. To fix the issue we have now, we will always wait
178 * for gadget driver to queue the DATA phase's struct usb_request, then
179 * start it right away.
181 * If we're actually in a 2-stage transfer, we will wait for
182 * XferNotReady(STATUS).
184 if (dwc->three_stage_setup) {
187 direction = dwc->ep0_expect_in;
188 dwc->ep0state = EP0_DATA_PHASE;
190 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
192 dep->flags &= ~DWC3_EP0_DIR_IN;
198 int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
201 struct dwc3_request *req = to_dwc3_request(request);
202 struct dwc3_ep *dep = to_dwc3_ep(ep);
203 struct dwc3 *dwc = dep->dwc;
209 spin_lock_irqsave(&dwc->lock, flags);
210 if (!dep->endpoint.desc) {
211 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
217 /* we share one TRB for ep0/1 */
218 if (!list_empty(&dep->pending_list)) {
223 ret = __dwc3_gadget_ep0_queue(dep, req);
226 spin_unlock_irqrestore(&dwc->lock, flags);
231 static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
235 /* reinitialize physical ep1 */
237 dep->flags = DWC3_EP_ENABLED;
239 /* stall is always issued on EP0 */
241 __dwc3_gadget_ep_set_halt(dep, 1, false);
242 dep->flags = DWC3_EP_ENABLED;
243 dwc->delayed_status = false;
245 if (!list_empty(&dep->pending_list)) {
246 struct dwc3_request *req;
248 req = next_request(&dep->pending_list);
249 dwc3_gadget_giveback(dep, req, -ECONNRESET);
252 dwc->ep0state = EP0_SETUP_PHASE;
253 dwc3_ep0_out_start(dwc);
256 int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
258 struct dwc3_ep *dep = to_dwc3_ep(ep);
259 struct dwc3 *dwc = dep->dwc;
261 dwc3_ep0_stall_and_restart(dwc);
266 int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
268 struct dwc3_ep *dep = to_dwc3_ep(ep);
269 struct dwc3 *dwc = dep->dwc;
273 spin_lock_irqsave(&dwc->lock, flags);
274 ret = __dwc3_gadget_ep0_set_halt(ep, value);
275 spin_unlock_irqrestore(&dwc->lock, flags);
280 void dwc3_ep0_out_start(struct dwc3 *dwc)
285 complete(&dwc->ep0_in_setup);
288 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 8,
289 DWC3_TRBCTL_CONTROL_SETUP, false);
290 ret = dwc3_ep0_start_trans(dep);
294 static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
297 u32 windex = le16_to_cpu(wIndex_le);
300 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
301 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
304 dep = dwc->eps[epnum];
308 if (dep->flags & DWC3_EP_ENABLED)
314 static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
320 static int dwc3_ep0_handle_status(struct dwc3 *dwc,
321 struct usb_ctrlrequest *ctrl)
328 __le16 *response_pkt;
330 /* We don't support PTM_STATUS */
331 value = le16_to_cpu(ctrl->wValue);
335 recip = ctrl->bRequestType & USB_RECIP_MASK;
337 case USB_RECIP_DEVICE:
339 * LTM will be set once we know how to set this in HW.
341 usb_status |= dwc->gadget.is_selfpowered;
343 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
344 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
345 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
346 if (reg & DWC3_DCTL_INITU1ENA)
347 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
348 if (reg & DWC3_DCTL_INITU2ENA)
349 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
354 case USB_RECIP_INTERFACE:
356 * Function Remote Wake Capable D0
357 * Function Remote Wakeup D1
361 case USB_RECIP_ENDPOINT:
362 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
366 if (dep->flags & DWC3_EP_STALL)
367 usb_status = 1 << USB_ENDPOINT_HALT;
373 response_pkt = (__le16 *) dwc->setup_buf;
374 *response_pkt = cpu_to_le16(usb_status);
377 dwc->ep0_usb_req.dep = dep;
378 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
379 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
380 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
382 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
385 static int dwc3_ep0_handle_u1(struct dwc3 *dwc, enum usb_device_state state,
390 if (state != USB_STATE_CONFIGURED)
392 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
393 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
396 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
398 reg |= DWC3_DCTL_INITU1ENA;
400 reg &= ~DWC3_DCTL_INITU1ENA;
401 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
406 static int dwc3_ep0_handle_u2(struct dwc3 *dwc, enum usb_device_state state,
412 if (state != USB_STATE_CONFIGURED)
414 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
415 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
418 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
420 reg |= DWC3_DCTL_INITU2ENA;
422 reg &= ~DWC3_DCTL_INITU2ENA;
423 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
428 static int dwc3_ep0_handle_test(struct dwc3 *dwc, enum usb_device_state state,
431 if ((wIndex & 0xff) != 0)
436 switch (wIndex >> 8) {
442 dwc->test_mode_nr = wIndex >> 8;
443 dwc->test_mode = true;
452 static int dwc3_ep0_handle_device(struct dwc3 *dwc,
453 struct usb_ctrlrequest *ctrl, int set)
455 enum usb_device_state state;
460 wValue = le16_to_cpu(ctrl->wValue);
461 wIndex = le16_to_cpu(ctrl->wIndex);
462 state = dwc->gadget.state;
465 case USB_DEVICE_REMOTE_WAKEUP:
468 * 9.4.1 says only only for SS, in AddressState only for
469 * default control pipe
471 case USB_DEVICE_U1_ENABLE:
472 ret = dwc3_ep0_handle_u1(dwc, state, set);
474 case USB_DEVICE_U2_ENABLE:
475 ret = dwc3_ep0_handle_u2(dwc, state, set);
477 case USB_DEVICE_LTM_ENABLE:
480 case USB_DEVICE_TEST_MODE:
481 ret = dwc3_ep0_handle_test(dwc, state, wIndex, set);
490 static int dwc3_ep0_handle_intf(struct dwc3 *dwc,
491 struct usb_ctrlrequest *ctrl, int set)
493 enum usb_device_state state;
498 wValue = le16_to_cpu(ctrl->wValue);
499 wIndex = le16_to_cpu(ctrl->wIndex);
500 state = dwc->gadget.state;
503 case USB_INTRF_FUNC_SUSPEND:
505 * REVISIT: Ideally we would enable some low power mode here,
506 * however it's unclear what we should be doing here.
508 * For now, we're not doing anything, just making sure we return
509 * 0 so USB Command Verifier tests pass without any errors.
519 static int dwc3_ep0_handle_endpoint(struct dwc3 *dwc,
520 struct usb_ctrlrequest *ctrl, int set)
523 enum usb_device_state state;
528 wValue = le16_to_cpu(ctrl->wValue);
529 wIndex = le16_to_cpu(ctrl->wIndex);
530 state = dwc->gadget.state;
533 case USB_ENDPOINT_HALT:
534 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
538 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
541 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
552 static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
553 struct usb_ctrlrequest *ctrl, int set)
557 enum usb_device_state state;
559 recip = ctrl->bRequestType & USB_RECIP_MASK;
560 state = dwc->gadget.state;
563 case USB_RECIP_DEVICE:
564 ret = dwc3_ep0_handle_device(dwc, ctrl, set);
566 case USB_RECIP_INTERFACE:
567 ret = dwc3_ep0_handle_intf(dwc, ctrl, set);
569 case USB_RECIP_ENDPOINT:
570 ret = dwc3_ep0_handle_endpoint(dwc, ctrl, set);
579 static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
581 enum usb_device_state state = dwc->gadget.state;
585 addr = le16_to_cpu(ctrl->wValue);
587 dev_err(dwc->dev, "invalid device address %d\n", addr);
591 if (state == USB_STATE_CONFIGURED) {
592 dev_err(dwc->dev, "can't SetAddress() from Configured State\n");
596 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
597 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
598 reg |= DWC3_DCFG_DEVADDR(addr);
599 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
602 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
604 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
609 static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
613 spin_unlock(&dwc->lock);
614 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
615 spin_lock(&dwc->lock);
619 static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
621 enum usb_device_state state = dwc->gadget.state;
626 cfg = le16_to_cpu(ctrl->wValue);
629 case USB_STATE_DEFAULT:
632 case USB_STATE_ADDRESS:
633 ret = dwc3_ep0_delegate_req(dwc, ctrl);
634 /* if the cfg matches and the cfg is non zero */
635 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
638 * only change state if set_config has already
639 * been processed. If gadget driver returns
640 * USB_GADGET_DELAYED_STATUS, we will wait
641 * to change the state on the next usb_ep_queue()
644 usb_gadget_set_state(&dwc->gadget,
645 USB_STATE_CONFIGURED);
648 * Enable transition to U1/U2 state when
649 * nothing is pending from application.
651 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
652 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
657 case USB_STATE_CONFIGURED:
658 ret = dwc3_ep0_delegate_req(dwc, ctrl);
660 usb_gadget_set_state(&dwc->gadget,
669 static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
671 struct dwc3_ep *dep = to_dwc3_ep(ep);
672 struct dwc3 *dwc = dep->dwc;
686 memcpy(&timing, req->buf, sizeof(timing));
688 dwc->u1sel = timing.u1sel;
689 dwc->u1pel = timing.u1pel;
690 dwc->u2sel = le16_to_cpu(timing.u2sel);
691 dwc->u2pel = le16_to_cpu(timing.u2pel);
693 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
694 if (reg & DWC3_DCTL_INITU2ENA)
696 if (reg & DWC3_DCTL_INITU1ENA)
700 * According to Synopsys Databook, if parameter is
701 * greater than 125, a value of zero should be
702 * programmed in the register.
707 /* now that we have the time, issue DGCMD Set Sel */
708 ret = dwc3_send_gadget_generic_command(dwc,
709 DWC3_DGCMD_SET_PERIODIC_PAR, param);
713 static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
716 enum usb_device_state state = dwc->gadget.state;
720 if (state == USB_STATE_DEFAULT)
723 wValue = le16_to_cpu(ctrl->wValue);
724 wLength = le16_to_cpu(ctrl->wLength);
727 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
733 * To handle Set SEL we need to receive 6 bytes from Host. So let's
734 * queue a usb_request for 6 bytes.
736 * Remember, though, this controller can't handle non-wMaxPacketSize
737 * aligned transfers on the OUT direction, so we queue a request for
738 * wMaxPacketSize instead.
741 dwc->ep0_usb_req.dep = dep;
742 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
743 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
744 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
746 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
749 static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
755 wValue = le16_to_cpu(ctrl->wValue);
756 wLength = le16_to_cpu(ctrl->wLength);
757 wIndex = le16_to_cpu(ctrl->wIndex);
759 if (wIndex || wLength)
763 * REVISIT It's unclear from Databook what to do with this
764 * value. For now, just cache it.
766 dwc->isoch_delay = wValue;
771 static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
775 switch (ctrl->bRequest) {
776 case USB_REQ_GET_STATUS:
777 ret = dwc3_ep0_handle_status(dwc, ctrl);
779 case USB_REQ_CLEAR_FEATURE:
780 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
782 case USB_REQ_SET_FEATURE:
783 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
785 case USB_REQ_SET_ADDRESS:
786 ret = dwc3_ep0_set_address(dwc, ctrl);
788 case USB_REQ_SET_CONFIGURATION:
789 ret = dwc3_ep0_set_config(dwc, ctrl);
791 case USB_REQ_SET_SEL:
792 ret = dwc3_ep0_set_sel(dwc, ctrl);
794 case USB_REQ_SET_ISOCH_DELAY:
795 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
798 ret = dwc3_ep0_delegate_req(dwc, ctrl);
805 static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
806 const struct dwc3_event_depevt *event)
808 struct usb_ctrlrequest *ctrl = (void *) dwc->ep0_trb;
812 if (!dwc->gadget_driver)
815 trace_dwc3_ctrl_req(ctrl);
817 len = le16_to_cpu(ctrl->wLength);
819 dwc->three_stage_setup = false;
820 dwc->ep0_expect_in = false;
821 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
823 dwc->three_stage_setup = true;
824 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
825 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
828 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
829 ret = dwc3_ep0_std_request(dwc, ctrl);
831 ret = dwc3_ep0_delegate_req(dwc, ctrl);
833 if (ret == USB_GADGET_DELAYED_STATUS)
834 dwc->delayed_status = true;
838 dwc3_ep0_stall_and_restart(dwc);
841 static void dwc3_ep0_complete_data(struct dwc3 *dwc,
842 const struct dwc3_event_depevt *event)
844 struct dwc3_request *r = NULL;
845 struct usb_request *ur;
846 struct dwc3_trb *trb;
849 unsigned remaining_ur_length;
856 epnum = event->endpoint_number;
859 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
861 trace_dwc3_complete_trb(ep0, trb);
863 r = next_request(&ep0->pending_list);
867 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
868 if (status == DWC3_TRBSTS_SETUP_PENDING) {
869 dwc->setup_packet_pending = true;
871 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
878 remaining_ur_length = ur->length;
880 length = trb->size & DWC3_TRB_SIZE_MASK;
881 maxp = ep0->endpoint.maxpacket;
882 transferred = ur->length - length;
883 ur->actual += transferred;
885 if ((IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
886 ur->length && ur->zero) || dwc->ep0_bounced) {
888 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
889 trace_dwc3_complete_trb(ep0, trb);
892 dwc->eps[1]->trb_enqueue = 0;
894 dwc->eps[0]->trb_enqueue = 0;
896 dwc->ep0_bounced = false;
899 if ((epnum & 1) && ur->actual < ur->length)
900 dwc3_ep0_stall_and_restart(dwc);
902 dwc3_gadget_giveback(ep0, r, 0);
905 static void dwc3_ep0_complete_status(struct dwc3 *dwc,
906 const struct dwc3_event_depevt *event)
908 struct dwc3_request *r;
910 struct dwc3_trb *trb;
916 trace_dwc3_complete_trb(dep, trb);
918 if (!list_empty(&dep->pending_list)) {
919 r = next_request(&dep->pending_list);
921 dwc3_gadget_giveback(dep, r, 0);
924 if (dwc->test_mode) {
927 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
929 dev_err(dwc->dev, "invalid test #%d\n",
931 dwc3_ep0_stall_and_restart(dwc);
936 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
937 if (status == DWC3_TRBSTS_SETUP_PENDING)
938 dwc->setup_packet_pending = true;
940 dwc->ep0state = EP0_SETUP_PHASE;
941 dwc3_ep0_out_start(dwc);
944 static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
945 const struct dwc3_event_depevt *event)
947 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
949 dep->flags &= ~DWC3_EP_BUSY;
950 dep->resource_index = 0;
951 dwc->setup_packet_pending = false;
953 switch (dwc->ep0state) {
954 case EP0_SETUP_PHASE:
955 dwc3_ep0_inspect_setup(dwc, event);
959 dwc3_ep0_complete_data(dwc, event);
962 case EP0_STATUS_PHASE:
963 dwc3_ep0_complete_status(dwc, event);
966 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
970 static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
971 struct dwc3_ep *dep, struct dwc3_request *req)
973 unsigned int trb_length = 0;
976 req->direction = !!dep->number;
978 if (req->request.length == 0) {
980 trb_length = dep->endpoint.maxpacket;
982 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr, trb_length,
983 DWC3_TRBCTL_CONTROL_DATA, false);
984 ret = dwc3_ep0_start_trans(dep);
985 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
986 && (dep->number == 0)) {
990 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
991 &req->request, dep->number);
995 maxpacket = dep->endpoint.maxpacket;
996 rem = req->request.length % maxpacket;
997 dwc->ep0_bounced = true;
999 /* prepare normal TRB */
1000 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1001 req->request.length,
1002 DWC3_TRBCTL_CONTROL_DATA,
1005 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
1007 /* Now prepare one extra TRB to align transfer size */
1008 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1010 DWC3_TRBCTL_CONTROL_DATA,
1012 ret = dwc3_ep0_start_trans(dep);
1013 } else if (IS_ALIGNED(req->request.length, dep->endpoint.maxpacket) &&
1014 req->request.length && req->request.zero) {
1018 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1019 &req->request, dep->number);
1023 maxpacket = dep->endpoint.maxpacket;
1024 rem = req->request.length % maxpacket;
1026 /* prepare normal TRB */
1027 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1028 req->request.length,
1029 DWC3_TRBCTL_CONTROL_DATA,
1032 req->trb = &dwc->ep0_trb[dep->trb_enqueue - 1];
1034 if (!req->direction)
1035 trb_length = dep->endpoint.maxpacket;
1037 /* Now prepare one extra TRB to align transfer size */
1038 dwc3_ep0_prepare_one_trb(dep, dwc->bounce_addr,
1039 trb_length, DWC3_TRBCTL_CONTROL_DATA,
1041 ret = dwc3_ep0_start_trans(dep);
1043 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1044 &req->request, dep->number);
1048 dwc3_ep0_prepare_one_trb(dep, req->request.dma,
1049 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1052 req->trb = &dwc->ep0_trb[dep->trb_enqueue];
1054 ret = dwc3_ep0_start_trans(dep);
1060 static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
1062 struct dwc3 *dwc = dep->dwc;
1065 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1066 : DWC3_TRBCTL_CONTROL_STATUS2;
1068 dwc3_ep0_prepare_one_trb(dep, dwc->ep0_trb_addr, 0, type, false);
1069 return dwc3_ep0_start_trans(dep);
1072 static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
1074 WARN_ON(dwc3_ep0_start_control_status(dep));
1077 static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1078 const struct dwc3_event_depevt *event)
1080 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1082 __dwc3_ep0_do_control_status(dwc, dep);
1085 static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1087 struct dwc3_gadget_ep_cmd_params params;
1091 if (!dep->resource_index)
1094 cmd = DWC3_DEPCMD_ENDTRANSFER;
1095 cmd |= DWC3_DEPCMD_CMDIOC;
1096 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1097 memset(¶ms, 0, sizeof(params));
1098 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1100 dep->resource_index = 0;
1103 static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1104 const struct dwc3_event_depevt *event)
1106 switch (event->status) {
1107 case DEPEVT_STATUS_CONTROL_DATA:
1109 * We already have a DATA transfer in the controller's cache,
1110 * if we receive a XferNotReady(DATA) we will ignore it, unless
1111 * it's for the wrong direction.
1113 * In that case, we must issue END_TRANSFER command to the Data
1114 * Phase we already have started and issue SetStall on the
1117 if (dwc->ep0_expect_in != event->endpoint_number) {
1118 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1120 dev_err(dwc->dev, "unexpected direction for Data Phase\n");
1121 dwc3_ep0_end_control_data(dwc, dep);
1122 dwc3_ep0_stall_and_restart(dwc);
1128 case DEPEVT_STATUS_CONTROL_STATUS:
1129 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1132 dwc->ep0state = EP0_STATUS_PHASE;
1134 if (dwc->delayed_status) {
1135 struct dwc3_ep *dep = dwc->eps[0];
1137 WARN_ON_ONCE(event->endpoint_number != 1);
1139 * We should handle the delay STATUS phase here if the
1140 * request for handling delay STATUS has been queued
1143 if (!list_empty(&dep->pending_list)) {
1144 dwc->delayed_status = false;
1145 usb_gadget_set_state(&dwc->gadget,
1146 USB_STATE_CONFIGURED);
1147 dwc3_ep0_do_control_status(dwc, event);
1153 dwc3_ep0_do_control_status(dwc, event);
1157 void dwc3_ep0_interrupt(struct dwc3 *dwc,
1158 const struct dwc3_event_depevt *event)
1160 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1163 switch (event->endpoint_event) {
1164 case DWC3_DEPEVT_XFERCOMPLETE:
1165 dwc3_ep0_xfer_complete(dwc, event);
1168 case DWC3_DEPEVT_XFERNOTREADY:
1169 dwc3_ep0_xfernotready(dwc, event);
1172 case DWC3_DEPEVT_XFERINPROGRESS:
1173 case DWC3_DEPEVT_RXTXFIFOEVT:
1174 case DWC3_DEPEVT_STREAMEVT:
1176 case DWC3_DEPEVT_EPCMDCMPLT:
1177 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
1179 if (cmd == DWC3_DEPCMD_ENDTRANSFER)
1180 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;