blob: 1c33051045f32e95ca5ebe7c0ab8f824803ff00c [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * ep0.c - DesignWare USB3 DRD Controller Endpoint 0 Handling
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * 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.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * 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.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
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>
25#include <linux/io.h>
26#include <linux/list.h>
27#include <linux/dma-mapping.h>
28
29#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h>
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010031#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030032
33#include "core.h"
Felipe Balbi80977dc2014-08-19 16:37:22 -050034#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030035#include "gadget.h"
36#include "io.h"
37
Felipe Balbi788a23f2012-05-21 14:22:41 +030038static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
Felipe Balbia0807882012-05-04 13:03:54 +030039static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
40 struct dwc3_ep *dep, struct dwc3_request *req);
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +010041
Felipe Balbi72246da2011-08-19 18:10:58 +030042static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
43{
44 switch (state) {
45 case EP0_UNCONNECTED:
46 return "Unconnected";
Felipe Balbic7fcdeb2011-08-27 22:28:36 +030047 case EP0_SETUP_PHASE:
48 return "Setup Phase";
49 case EP0_DATA_PHASE:
50 return "Data Phase";
51 case EP0_STATUS_PHASE:
52 return "Status Phase";
Felipe Balbi72246da2011-08-19 18:10:58 +030053 default:
54 return "UNKNOWN";
55 }
56}
57
Felipe Balbi5f7fd4d2016-12-20 13:57:32 +020058static void dwc3_ep0_prepare_one_trb(struct dwc3 *dwc, u8 epnum,
59 dma_addr_t buf_dma, u32 len, u32 type, bool chain)
Felipe Balbi72246da2011-08-19 18:10:58 +030060{
Felipe Balbif6bafc62012-02-06 11:04:53 +020061 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +030062 struct dwc3_ep *dep;
63
Felipe Balbi72246da2011-08-19 18:10:58 +030064 dep = dwc->eps[epnum];
65
Felipe Balbi53fd8812016-04-04 15:33:41 +030066 trb = &dwc->ep0_trb[dep->trb_enqueue];
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053067
68 if (chain)
Felipe Balbi53fd8812016-04-04 15:33:41 +030069 dep->trb_enqueue++;
Felipe Balbi72246da2011-08-19 18:10:58 +030070
Felipe Balbif6bafc62012-02-06 11:04:53 +020071 trb->bpl = lower_32_bits(buf_dma);
72 trb->bph = upper_32_bits(buf_dma);
73 trb->size = len;
74 trb->ctrl = type;
Felipe Balbi72246da2011-08-19 18:10:58 +030075
Felipe Balbif6bafc62012-02-06 11:04:53 +020076 trb->ctrl |= (DWC3_TRB_CTRL_HWO
Felipe Balbif6bafc62012-02-06 11:04:53 +020077 | DWC3_TRB_CTRL_ISP_IMI);
Felipe Balbi72246da2011-08-19 18:10:58 +030078
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053079 if (chain)
80 trb->ctrl |= DWC3_TRB_CTRL_CHN;
81 else
82 trb->ctrl |= (DWC3_TRB_CTRL_IOC
83 | DWC3_TRB_CTRL_LST);
84
Felipe Balbi5f7fd4d2016-12-20 13:57:32 +020085 trace_dwc3_prepare_trb(dep, trb);
86}
87
Felipe Balbic0083092016-12-20 14:08:48 +020088static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum)
Felipe Balbi5f7fd4d2016-12-20 13:57:32 +020089{
90 struct dwc3_gadget_ep_cmd_params params;
91 struct dwc3_ep *dep;
92 int ret;
93
94 dep = dwc->eps[epnum];
95 if (dep->flags & DWC3_EP_BUSY) {
96 dwc3_trace(trace_dwc3_ep0, "%s still busy", dep->name);
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053097 return 0;
Felipe Balbi5f7fd4d2016-12-20 13:57:32 +020098 }
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +053099
Felipe Balbi72246da2011-08-19 18:10:58 +0300100 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300101 params.param0 = upper_32_bits(dwc->ep0_trb_addr);
102 params.param1 = lower_32_bits(dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300103
Felipe Balbi2cd47182016-04-12 16:42:43 +0300104 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_STARTTRANSFER, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300105 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500106 dwc3_trace(trace_dwc3_ep0, "%s STARTTRANSFER failed",
107 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300108 return ret;
109 }
110
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300111 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi2eb88012016-04-12 16:53:39 +0300112 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300113 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
114
Felipe Balbi72246da2011-08-19 18:10:58 +0300115 return 0;
116}
117
118static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
119 struct dwc3_request *req)
120{
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100121 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300122
123 req->request.actual = 0;
124 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +0300125 req->epnum = dep->number;
126
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200127 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300128
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300129 /*
130 * Gadget driver might not be quick enough to queue a request
131 * before we get a Transfer Not Ready event on this endpoint.
132 *
133 * In that case, we will set DWC3_EP_PENDING_REQUEST. When that
134 * flag is set, it's telling us that as soon as Gadget queues the
135 * required request, we should kick the transfer here because the
136 * IRQ we were waiting for is long gone.
137 */
138 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300139 unsigned direction;
Felipe Balbia6829702011-08-27 22:18:09 +0300140
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300141 direction = !!(dep->flags & DWC3_EP0_DIR_IN);
Felipe Balbia6829702011-08-27 22:18:09 +0300142
Felipe Balbi68d8a782011-12-29 06:32:29 +0200143 if (dwc->ep0state != EP0_DATA_PHASE) {
144 dev_WARN(dwc->dev, "Unexpected pending request\n");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300145 return 0;
146 }
Felipe Balbia6829702011-08-27 22:18:09 +0300147
Felipe Balbia0807882012-05-04 13:03:54 +0300148 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
149
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300150 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
151 DWC3_EP0_DIR_IN);
Felipe Balbid9b33c62012-07-19 08:51:13 +0300152
153 return 0;
154 }
155
156 /*
157 * In case gadget driver asked us to delay the STATUS phase,
158 * handle it here.
159 */
160 if (dwc->delayed_status) {
Felipe Balbi7125d582012-07-19 21:05:08 +0300161 unsigned direction;
162
163 direction = !dwc->ep0_expect_in;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100164 dwc->delayed_status = false;
Felipe Balbi7c812902013-07-22 12:41:47 +0300165 usb_gadget_set_state(&dwc->gadget, USB_STATE_CONFIGURED);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200166
167 if (dwc->ep0state == EP0_STATUS_PHASE)
Felipe Balbi7125d582012-07-19 21:05:08 +0300168 __dwc3_ep0_do_control_status(dwc, dwc->eps[direction]);
Felipe Balbi68d3e662011-12-08 13:56:27 +0200169 else
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500170 dwc3_trace(trace_dwc3_ep0,
171 "too early for delayed status");
Felipe Balbid9b33c62012-07-19 08:51:13 +0300172
173 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300174 }
175
Felipe Balbifca88922012-07-19 09:05:35 +0300176 /*
177 * Unfortunately we have uncovered a limitation wrt the Data Phase.
178 *
179 * Section 9.4 says we can wait for the XferNotReady(DATA) event to
180 * come before issueing Start Transfer command, but if we do, we will
181 * miss situations where the host starts another SETUP phase instead of
182 * the DATA phase. Such cases happen at least on TD.7.6 of the Link
183 * Layer Compliance Suite.
184 *
185 * The problem surfaces due to the fact that in case of back-to-back
186 * SETUP packets there will be no XferNotReady(DATA) generated and we
187 * will be stuck waiting for XferNotReady(DATA) forever.
188 *
189 * By looking at tables 9-13 and 9-14 of the Databook, we can see that
190 * it tells us to start Data Phase right away. It also mentions that if
191 * we receive a SETUP phase instead of the DATA phase, core will issue
192 * XferComplete for the DATA phase, before actually initiating it in
193 * the wire, with the TRB's status set to "SETUP_PENDING". Such status
194 * can only be used to print some debugging logs, as the core expects
195 * us to go through to the STATUS phase and start a CONTROL_STATUS TRB,
196 * just so it completes right away, without transferring anything and,
197 * only then, we can go back to the SETUP phase.
198 *
199 * Because of this scenario, SNPS decided to change the programming
200 * model of control transfers and support on-demand transfers only for
201 * the STATUS phase. To fix the issue we have now, we will always wait
202 * for gadget driver to queue the DATA phase's struct usb_request, then
203 * start it right away.
204 *
205 * If we're actually in a 2-stage transfer, we will wait for
206 * XferNotReady(STATUS).
207 */
208 if (dwc->three_stage_setup) {
209 unsigned direction;
210
211 direction = dwc->ep0_expect_in;
212 dwc->ep0state = EP0_DATA_PHASE;
213
214 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
215
216 dep->flags &= ~DWC3_EP0_DIR_IN;
217 }
218
Felipe Balbi35f75692012-07-19 08:49:01 +0300219 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300220}
221
222int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
223 gfp_t gfp_flags)
224{
225 struct dwc3_request *req = to_dwc3_request(request);
226 struct dwc3_ep *dep = to_dwc3_ep(ep);
227 struct dwc3 *dwc = dep->dwc;
228
229 unsigned long flags;
230
231 int ret;
Hemant Kumare4f27812017-01-27 18:31:22 -0800232 enum dwc3_link_state link_state;
233 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300234
Felipe Balbi72246da2011-08-19 18:10:58 +0300235 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200236 if (!dep->endpoint.desc) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500237 dwc3_trace(trace_dwc3_ep0,
238 "trying to queue request %p to disabled %s",
Felipe Balbi72246da2011-08-19 18:10:58 +0300239 request, dep->name);
240 ret = -ESHUTDOWN;
241 goto out;
242 }
243
244 /* we share one TRB for ep0/1 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200245 if (!list_empty(&dep->pending_list)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 ret = -EBUSY;
247 goto out;
248 }
249
Hemant Kumare4f27812017-01-27 18:31:22 -0800250 /* if link stats is in L1 initiate remote wakeup before queuing req */
251 if (dwc->speed != DWC3_DSTS_SUPERSPEED) {
252 link_state = dwc3_get_link_state(dwc);
253 /* in HS this link state is same as L1 */
254 if (link_state == DWC3_LINK_STATE_U2) {
255 dwc->l1_remote_wakeup_cnt++;
256 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
257 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
258 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
259 }
260 }
261
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500262 dwc3_trace(trace_dwc3_ep0,
263 "queueing request %p to %s length %d state '%s'",
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 request, dep->name, request->length,
265 dwc3_ep0_state_string(dwc->ep0state));
266
267 ret = __dwc3_gadget_ep0_queue(dep, req);
268
269out:
270 spin_unlock_irqrestore(&dwc->lock, flags);
271
272 return ret;
273}
274
275static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
276{
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300277 struct dwc3_ep *dep;
278
279 /* reinitialize physical ep1 */
280 dep = dwc->eps[1];
281 dep->flags = DWC3_EP_ENABLED;
Felipe Balbid7422202011-09-08 18:17:12 +0300282
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 /* stall is always issued on EP0 */
Felipe Balbi2dfe37d2012-07-23 09:07:41 +0300284 dep = dwc->eps[0];
Felipe Balbi7a608552014-09-24 14:19:52 -0500285 __dwc3_gadget_ep_set_halt(dep, 1, false);
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200286 dep->flags = DWC3_EP_ENABLED;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100287 dwc->delayed_status = false;
Felipe Balbid7422202011-09-08 18:17:12 +0300288
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200289 if (!list_empty(&dep->pending_list)) {
Felipe Balbid7422202011-09-08 18:17:12 +0300290 struct dwc3_request *req;
291
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200292 req = next_request(&dep->pending_list);
Felipe Balbid7422202011-09-08 18:17:12 +0300293 dwc3_gadget_giveback(dep, req, -ECONNRESET);
294 }
295
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300296 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300297 dwc3_ep0_out_start(dwc);
298}
299
Felipe Balbi33fb6912014-09-24 10:46:46 -0500300int __dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
Pratyush Anand08f0d962012-06-25 22:40:43 +0530301{
302 struct dwc3_ep *dep = to_dwc3_ep(ep);
303 struct dwc3 *dwc = dep->dwc;
304
Mayank Rana08e41922017-03-02 15:25:48 -0800305 dbg_event(dep->number, "EP0STAL", value);
Pratyush Anand08f0d962012-06-25 22:40:43 +0530306 dwc3_ep0_stall_and_restart(dwc);
307
308 return 0;
309}
310
Felipe Balbi33fb6912014-09-24 10:46:46 -0500311int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
312{
313 struct dwc3_ep *dep = to_dwc3_ep(ep);
314 struct dwc3 *dwc = dep->dwc;
315 unsigned long flags;
316 int ret;
317
318 spin_lock_irqsave(&dwc->lock, flags);
319 ret = __dwc3_gadget_ep0_set_halt(ep, value);
320 spin_unlock_irqrestore(&dwc->lock, flags);
321
322 return ret;
323}
324
Felipe Balbi72246da2011-08-19 18:10:58 +0300325void dwc3_ep0_out_start(struct dwc3 *dwc)
326{
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 int ret;
328
Felipe Balbic0083092016-12-20 14:08:48 +0200329 dwc3_ep0_prepare_one_trb(dwc, 0, dwc->ctrl_req_addr, 8,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +0530330 DWC3_TRBCTL_CONTROL_SETUP, false);
Felipe Balbic0083092016-12-20 14:08:48 +0200331 ret = dwc3_ep0_start_trans(dwc, 0);
Mayank Rana08e41922017-03-02 15:25:48 -0800332 if (WARN_ON(ret < 0))
333 dbg_event(dwc->eps[0]->number, "EOUTSTART", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300334}
335
Felipe Balbi72246da2011-08-19 18:10:58 +0300336static struct dwc3_ep *dwc3_wIndex_to_dep(struct dwc3 *dwc, __le16 wIndex_le)
337{
338 struct dwc3_ep *dep;
339 u32 windex = le16_to_cpu(wIndex_le);
340 u32 epnum;
341
342 epnum = (windex & USB_ENDPOINT_NUMBER_MASK) << 1;
343 if ((windex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
344 epnum |= 1;
345
346 dep = dwc->eps[epnum];
347 if (dep->flags & DWC3_EP_ENABLED)
348 return dep;
349
350 return NULL;
351}
352
Sebastian Andrzej Siewior8ee62702011-10-18 19:13:29 +0200353static void dwc3_ep0_status_cmpl(struct usb_ep *ep, struct usb_request *req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300354{
Felipe Balbi72246da2011-08-19 18:10:58 +0300355}
Felipe Balbi72246da2011-08-19 18:10:58 +0300356/*
357 * ch 9.4.5
358 */
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200359static int dwc3_ep0_handle_status(struct dwc3 *dwc,
360 struct usb_ctrlrequest *ctrl)
Felipe Balbi72246da2011-08-19 18:10:58 +0300361{
362 struct dwc3_ep *dep;
363 u32 recip;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200364 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 u16 usb_status = 0;
366 __le16 *response_pkt;
367
368 recip = ctrl->bRequestType & USB_RECIP_MASK;
369 switch (recip) {
370 case USB_RECIP_DEVICE:
371 /*
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200372 * LTM will be set once we know how to set this in HW.
Felipe Balbi72246da2011-08-19 18:10:58 +0300373 */
Peter Chenbcdea502015-01-28 16:32:40 +0800374 usb_status |= dwc->gadget.is_selfpowered;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200375
John Younee5cd412016-02-05 17:08:45 -0800376 if ((dwc->speed == DWC3_DSTS_SUPERSPEED) ||
377 (dwc->speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200378 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
379 if (reg & DWC3_DCTL_INITU1ENA)
380 usb_status |= 1 << USB_DEV_STAT_U1_ENABLED;
381 if (reg & DWC3_DCTL_INITU2ENA)
382 usb_status |= 1 << USB_DEV_STAT_U2_ENABLED;
Danny Segalc7f53052014-05-27 12:22:38 +0300383 } else {
384 usb_status |= dwc->gadget.remote_wakeup <<
385 USB_DEVICE_REMOTE_WAKEUP;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200386 }
387
Felipe Balbi72246da2011-08-19 18:10:58 +0300388 break;
389
390 case USB_RECIP_INTERFACE:
391 /*
392 * Function Remote Wake Capable D0
393 * Function Remote Wakeup D1
394 */
395 break;
396
397 case USB_RECIP_ENDPOINT:
398 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
399 if (!dep)
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200400 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300401
402 if (dep->flags & DWC3_EP_STALL)
403 usb_status = 1 << USB_ENDPOINT_HALT;
404 break;
405 default:
406 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700407 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300408
409 response_pkt = (__le16 *) dwc->setup_buf;
410 *response_pkt = cpu_to_le16(usb_status);
Felipe Balbie2617792011-11-29 10:35:47 +0200411
412 dep = dwc->eps[0];
413 dwc->ep0_usb_req.dep = dep;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100414 dwc->ep0_usb_req.request.length = sizeof(*response_pkt);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200415 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
Sebastian Andrzej Siewiore0ce0b02011-11-25 12:03:46 +0100416 dwc->ep0_usb_req.request.complete = dwc3_ep0_status_cmpl;
Felipe Balbie2617792011-11-29 10:35:47 +0200417
418 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419}
420
421static int dwc3_ep0_handle_feature(struct dwc3 *dwc,
422 struct usb_ctrlrequest *ctrl, int set)
423{
424 struct dwc3_ep *dep;
425 u32 recip;
426 u32 wValue;
427 u32 wIndex;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200428 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 int ret;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200430 enum usb_device_state state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300431
432 wValue = le16_to_cpu(ctrl->wValue);
433 wIndex = le16_to_cpu(ctrl->wIndex);
434 recip = ctrl->bRequestType & USB_RECIP_MASK;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200435 state = dwc->gadget.state;
436
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 switch (recip) {
438 case USB_RECIP_DEVICE:
439
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200440 switch (wValue) {
441 case USB_DEVICE_REMOTE_WAKEUP:
Danny Segalc7f53052014-05-27 12:22:38 +0300442 pr_debug("%s(): remote wakeup :%s\n", __func__,
443 (set ? "enabled" : "disabled"));
444 dwc->gadget.remote_wakeup = set;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200445 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446 /*
447 * 9.4.1 says only only for SS, in AddressState only for
448 * default control pipe
449 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300450 case USB_DEVICE_U1_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200451 if (state != USB_STATE_CONFIGURED)
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800453 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
454 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +0300455 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200457 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
458 if (set)
459 reg |= DWC3_DCTL_INITU1ENA;
460 else
461 reg &= ~DWC3_DCTL_INITU1ENA;
462 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300463 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200464
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 case USB_DEVICE_U2_ENABLE:
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200466 if (state != USB_STATE_CONFIGURED)
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200467 return -EINVAL;
John Younee5cd412016-02-05 17:08:45 -0800468 if ((dwc->speed != DWC3_DSTS_SUPERSPEED) &&
469 (dwc->speed != DWC3_DSTS_SUPERSPEED_PLUS))
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200470 return -EINVAL;
471
472 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
473 if (set)
474 reg |= DWC3_DCTL_INITU2ENA;
475 else
476 reg &= ~DWC3_DCTL_INITU2ENA;
477 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 break;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200479
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 case USB_DEVICE_LTM_ENABLE:
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +0200481 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300482
483 case USB_DEVICE_TEST_MODE:
484 if ((wIndex & 0xff) != 0)
485 return -EINVAL;
486 if (!set)
487 return -EINVAL;
488
Fei Yangae411412016-04-20 09:08:43 +0300489 switch (wIndex >> 8) {
490 case TEST_J:
491 case TEST_K:
492 case TEST_SE0_NAK:
493 case TEST_PACKET:
494 case TEST_FORCE_EN:
495 dwc->test_mode_nr = wIndex >> 8;
496 dwc->test_mode = true;
497 break;
498 default:
499 return -EINVAL;
500 }
Gerard Cauvyecb07792012-03-16 16:20:10 +0200501 break;
502 default:
503 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300504 }
505 break;
506
507 case USB_RECIP_INTERFACE:
508 switch (wValue) {
509 case USB_INTRF_FUNC_SUSPEND:
510 if (wIndex & USB_INTRF_FUNC_SUSPEND_LP)
511 /* XXX enable Low power suspend */
512 ;
513 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
514 /* XXX enable remote wakeup */
515 ;
516 break;
517 default:
518 return -EINVAL;
519 }
520 break;
521
522 case USB_RECIP_ENDPOINT:
523 switch (wValue) {
524 case USB_ENDPOINT_HALT:
John Youn958b9fa2016-05-23 11:32:38 -0700525 dep = dwc3_wIndex_to_dep(dwc, ctrl->wIndex);
Felipe Balbi72246da2011-08-19 18:10:58 +0300526 if (!dep)
527 return -EINVAL;
Alan Sterna535d812013-11-01 12:05:12 -0400528 if (set == 0 && (dep->flags & DWC3_EP_WEDGE))
529 break;
Felipe Balbi7a608552014-09-24 14:19:52 -0500530 ret = __dwc3_gadget_ep_set_halt(dep, set, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 if (ret)
532 return -EINVAL;
533 break;
534 default:
535 return -EINVAL;
536 }
537 break;
538
539 default:
540 return -EINVAL;
Joe Perches2b84f922013-10-08 16:01:37 -0700541 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 return 0;
544}
545
546static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
547{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200548 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549 u32 addr;
550 u32 reg;
551
552 addr = le16_to_cpu(ctrl->wValue);
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300553 if (addr > 127) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500554 dwc3_trace(trace_dwc3_ep0, "invalid device address %d", addr);
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 return -EINVAL;
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300556 }
557
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200558 if (state == USB_STATE_CONFIGURED) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500559 dwc3_trace(trace_dwc3_ep0,
560 "trying to set address when configured");
Felipe Balbif96a6ec2011-10-15 21:37:35 +0300561 return -EINVAL;
562 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Felipe Balbi26460212011-09-30 10:58:36 +0300564 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
565 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
566 reg |= DWC3_DCFG_DEVADDR(addr);
567 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +0300568
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200569 if (addr)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200570 usb_gadget_set_state(&dwc->gadget, USB_STATE_ADDRESS);
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200571 else
Felipe Balbi14cd5922011-12-19 13:01:52 +0200572 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
Felipe Balbi26460212011-09-30 10:58:36 +0300574 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300575}
576
577static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
578{
579 int ret;
580
581 spin_unlock(&dwc->lock);
582 ret = dwc->gadget_driver->setup(&dwc->gadget, ctrl);
583 spin_lock(&dwc->lock);
584 return ret;
585}
586
587static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
588{
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200589 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 u32 cfg;
Mayank Ranaac1200c2017-04-25 13:48:46 -0700591 int ret, num;
Pratyush Anande274a312012-07-02 10:21:54 +0530592 u32 reg;
Mayank Ranaac1200c2017-04-25 13:48:46 -0700593 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300594
595 cfg = le16_to_cpu(ctrl->wValue);
596
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200597 switch (state) {
598 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300600
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200601 case USB_STATE_ADDRESS:
Mayank Ranaac1200c2017-04-25 13:48:46 -0700602 /* Read ep0IN related TXFIFO size */
603 dwc->last_fifo_depth = (dwc3_readl(dwc->regs,
604 DWC3_GTXFIFOSIZ(0)) & 0xFFFF);
605 /* Clear existing allocated TXFIFO for all IN eps except ep0 */
606 for (num = 0; num < dwc->num_in_eps; num++) {
607 dep = dwc->eps[(num << 1) | 1];
608 if (num) {
609 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), 0);
610 dep->fifo_depth = 0;
611 } else {
612 dep->fifo_depth = dwc->last_fifo_depth;
613 }
614
615 dev_dbg(dwc->dev, "%s(): %s dep->fifo_depth:%x\n",
616 __func__, dep->name, dep->fifo_depth);
617 dbg_event(0xFF, "fifo_reset", dep->number);
618 }
619
Felipe Balbi72246da2011-08-19 18:10:58 +0300620 ret = dwc3_ep0_delegate_req(dwc, ctrl);
621 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200622 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300623
624 /*
625 * only change state if set_config has already
626 * been processed. If gadget driver returns
627 * USB_GADGET_DELAYED_STATUS, we will wait
628 * to change the state on the next usb_ep_queue()
629 */
630 if (ret == 0)
631 usb_gadget_set_state(&dwc->gadget,
632 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200633
Pratyush Anande274a312012-07-02 10:21:54 +0530634 /*
635 * Enable transition to U1/U2 state when
636 * nothing is pending from application.
637 */
638 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
639 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
640 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200641 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 break;
643
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200644 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300645 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300646 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200647 usb_gadget_set_state(&dwc->gadget,
648 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100650 default:
651 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100653 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300654}
655
Felipe Balbi865e09e2012-04-24 16:19:49 +0300656static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
657{
658 struct dwc3_ep *dep = to_dwc3_ep(ep);
659 struct dwc3 *dwc = dep->dwc;
660
661 u32 param = 0;
662 u32 reg;
663
664 struct timing {
665 u8 u1sel;
666 u8 u1pel;
John Youn501058e2016-05-23 11:32:40 -0700667 __le16 u2sel;
668 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300669 } __packed timing;
670
671 int ret;
672
673 memcpy(&timing, req->buf, sizeof(timing));
674
675 dwc->u1sel = timing.u1sel;
676 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300677 dwc->u2sel = le16_to_cpu(timing.u2sel);
678 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300679
680 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
681 if (reg & DWC3_DCTL_INITU2ENA)
682 param = dwc->u2pel;
683 if (reg & DWC3_DCTL_INITU1ENA)
684 param = dwc->u1pel;
685
686 /*
687 * According to Synopsys Databook, if parameter is
688 * greater than 125, a value of zero should be
689 * programmed in the register.
690 */
691 if (param > 125)
692 param = 0;
693
694 /* now that we have the time, issue DGCMD Set Sel */
695 ret = dwc3_send_gadget_generic_command(dwc,
696 DWC3_DGCMD_SET_PERIODIC_PAR, param);
Mayank Rana08e41922017-03-02 15:25:48 -0800697 if (WARN_ON(ret < 0))
698 dbg_event(dep->number, "ESET_SELCMPL", ret);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300699}
700
701static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
702{
703 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200704 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300705 u16 wLength;
706 u16 wValue;
707
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200708 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300709 return -EINVAL;
710
711 wValue = le16_to_cpu(ctrl->wValue);
712 wLength = le16_to_cpu(ctrl->wLength);
713
714 if (wLength != 6) {
715 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
716 wLength);
717 return -EINVAL;
718 }
719
720 /*
721 * To handle Set SEL we need to receive 6 bytes from Host. So let's
722 * queue a usb_request for 6 bytes.
723 *
724 * Remember, though, this controller can't handle non-wMaxPacketSize
725 * aligned transfers on the OUT direction, so we queue a request for
726 * wMaxPacketSize instead.
727 */
728 dep = dwc->eps[0];
729 dwc->ep0_usb_req.dep = dep;
730 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
731 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
732 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
733
734 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
735}
736
Felipe Balbic12a0d82012-04-25 10:45:05 +0300737static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
738{
739 u16 wLength;
740 u16 wValue;
741 u16 wIndex;
742
743 wValue = le16_to_cpu(ctrl->wValue);
744 wLength = le16_to_cpu(ctrl->wLength);
745 wIndex = le16_to_cpu(ctrl->wIndex);
746
747 if (wIndex || wLength)
748 return -EINVAL;
749
750 /*
751 * REVISIT It's unclear from Databook what to do with this
752 * value. For now, just cache it.
753 */
754 dwc->isoch_delay = wValue;
755
756 return 0;
757}
758
Felipe Balbi72246da2011-08-19 18:10:58 +0300759static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
760{
761 int ret;
762
763 switch (ctrl->bRequest) {
764 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500765 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300766 ret = dwc3_ep0_handle_status(dwc, ctrl);
767 break;
768 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500769 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300770 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
771 break;
772 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500773 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
775 break;
776 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500777 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 ret = dwc3_ep0_set_address(dwc, ctrl);
779 break;
780 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500781 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 ret = dwc3_ep0_set_config(dwc, ctrl);
783 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300784 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500785 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300786 ret = dwc3_ep0_set_sel(dwc, ctrl);
787 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300788 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500789 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300790 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
791 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300792 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500793 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 ret = dwc3_ep0_delegate_req(dwc, ctrl);
795 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700796 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300797
798 return ret;
799}
800
801static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
802 const struct dwc3_event_depevt *event)
803{
804 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300805 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 u32 len;
807
808 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300809 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300810
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500811 trace_dwc3_ctrl_req(ctrl);
812
Felipe Balbi72246da2011-08-19 18:10:58 +0300813 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300814 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300815 dwc->three_stage_setup = false;
816 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300817 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
818 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300819 dwc->three_stage_setup = true;
820 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300821 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
822 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300823
Mayank Rana08e41922017-03-02 15:25:48 -0800824 dbg_setup(0x00, ctrl);
Felipe Balbi72246da2011-08-19 18:10:58 +0300825 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
826 ret = dwc3_ep0_std_request(dwc, ctrl);
827 else
828 ret = dwc3_ep0_delegate_req(dwc, ctrl);
829
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100830 if (ret == USB_GADGET_DELAYED_STATUS)
831 dwc->delayed_status = true;
832
Felipe Balbief21ede2012-05-31 10:29:49 +0300833out:
Mayank Ranad223be42017-06-07 11:54:08 -0700834 /*
835 * Don't try to halt ep0 if ret is -ESHUTDOWN.
836 * ret as -ESHUTDOWN suggests that setup packet related response
837 * is available but queueing of ep0 is failed. Possibly ep0 is
838 * already disabled.
839 */
840 if (ret < 0 && ret != -ESHUTDOWN) {
Mayank Rana08e41922017-03-02 15:25:48 -0800841 dbg_event(0x0, "ERRSTAL", ret);
Felipe Balbief21ede2012-05-31 10:29:49 +0300842 dwc3_ep0_stall_and_restart(dwc);
Mayank Rana08e41922017-03-02 15:25:48 -0800843 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300844}
845
846static void dwc3_ep0_complete_data(struct dwc3 *dwc,
847 const struct dwc3_event_depevt *event)
848{
849 struct dwc3_request *r = NULL;
850 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200851 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200852 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530853 unsigned transfer_size = 0;
854 unsigned maxp;
855 unsigned remaining_ur_length;
856 void *buf;
857 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300858 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200859 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 u8 epnum;
861
862 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200863 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300864
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300865 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
866
Felipe Balbif6bafc62012-02-06 11:04:53 +0200867 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300868
Felipe Balbiccb072d2014-10-01 12:20:29 -0500869 trace_dwc3_complete_trb(ep0, trb);
870
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200871 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600872 if (!r)
873 return;
874
Felipe Balbifca88922012-07-19 09:05:35 +0300875 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
876 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600877 dwc->setup_packet_pending = true;
878
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500879 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300880
Hemant Kumar951134b2017-09-07 18:13:36 -0700881 dbg_event(0x0, "SETUPPEND", status);
Felipe Balbifca88922012-07-19 09:05:35 +0300882 }
883
Felipe Balbi6856d302014-09-30 11:43:20 -0500884 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530885 buf = ur->buf;
886 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500887
Felipe Balbif6bafc62012-02-06 11:04:53 +0200888 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530890 maxp = ep0->endpoint.maxpacket;
891
Felipe Balbia6829702011-08-27 22:18:09 +0300892 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530893 /*
894 * Handle the first TRB before handling the bounce buffer if
895 * the request length is greater than the bounce buffer size
896 */
897 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
898 transfer_size = ALIGN(ur->length - maxp, maxp);
899 transferred = transfer_size - length;
900 buf = (u8 *)buf + transferred;
901 ur->actual += transferred;
902 remaining_ur_length -= transferred;
903
904 trb++;
905 length = trb->size & DWC3_TRB_SIZE_MASK;
906
Felipe Balbi53fd8812016-04-04 15:33:41 +0300907 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530908 }
909
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530910 transfer_size = roundup((ur->length - transfer_size),
911 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530912
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530913 transferred = min_t(u32, remaining_ur_length,
914 transfer_size - length);
915 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300916 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200917 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300918 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300919
Felipe Balbicd423dd2012-03-21 11:44:00 +0200920 ur->actual += transferred;
921
Felipe Balbi72246da2011-08-19 18:10:58 +0300922 if ((epnum & 1) && ur->actual < ur->length) {
923 /* for some reason we did not get everything out */
Mayank Rana08e41922017-03-02 15:25:48 -0800924 dbg_event(epnum, "INDATSTAL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300925 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500927 dwc3_gadget_giveback(ep0, r, 0);
928
929 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
930 ur->length && ur->zero) {
931 int ret;
932
933 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
934
Felipe Balbic0083092016-12-20 14:08:48 +0200935 dwc3_ep0_prepare_one_trb(dwc, epnum, dwc->ctrl_req_addr,
936 0, DWC3_TRBCTL_CONTROL_DATA, false);
937 ret = dwc3_ep0_start_trans(dwc, epnum);
Mayank Rana08e41922017-03-02 15:25:48 -0800938 if (WARN_ON(ret < 0))
939 dbg_event(epnum, "ECTRL_DATA", ret);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500940 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300941 }
942}
943
Felipe Balbi85a78102012-05-31 12:32:37 +0300944static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 const struct dwc3_event_depevt *event)
946{
947 struct dwc3_request *r;
948 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300949 struct dwc3_trb *trb;
950 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300951
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300952 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300953 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300954
Felipe Balbiccb072d2014-10-01 12:20:29 -0500955 trace_dwc3_complete_trb(dep, trb);
956
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200957 if (!list_empty(&dep->pending_list)) {
958 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300959
960 dwc3_gadget_giveback(dep, r, 0);
961 }
962
Gerard Cauvy3b637362012-02-10 12:21:18 +0200963 if (dwc->test_mode) {
964 int ret;
965
966 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
967 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500968 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200969 dwc->test_mode_nr);
Mayank Rana08e41922017-03-02 15:25:48 -0800970 dbg_event(0x00, "INVALTEST", ret);
Gerard Cauvy3b637362012-02-10 12:21:18 +0200971 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300972 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200973 }
974 }
975
Felipe Balbifca88922012-07-19 09:05:35 +0300976 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbib5d335e2015-11-16 16:20:34 -0600977 if (status == DWC3_TRBSTS_SETUP_PENDING) {
978 dwc->setup_packet_pending = true;
Felipe Balbidab716c2014-09-24 11:22:23 -0500979 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbib5d335e2015-11-16 16:20:34 -0600980 }
Felipe Balbifca88922012-07-19 09:05:35 +0300981
Mayank Rana08e41922017-03-02 15:25:48 -0800982 dbg_print(dep->number, "DONE", status, "STATUS");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300983 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 dwc3_ep0_out_start(dwc);
985}
986
987static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
988 const struct dwc3_event_depevt *event)
989{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300990 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
991
992 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300993 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300994 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300995
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300997 case EP0_SETUP_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500998 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 dwc3_ep0_inspect_setup(dwc, event);
1000 break;
1001
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001002 case EP0_DATA_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001003 dwc3_trace(trace_dwc3_ep0, "Data Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 dwc3_ep0_complete_data(dwc, event);
1005 break;
1006
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001007 case EP0_STATUS_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001008 dwc3_trace(trace_dwc3_ep0, "Status Phase");
Felipe Balbi85a78102012-05-31 12:32:37 +03001009 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001010 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001011 default:
1012 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +03001013 }
1014}
1015
Felipe Balbia0807882012-05-04 13:03:54 +03001016static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
1017 struct dwc3_ep *dep, struct dwc3_request *req)
1018{
1019 int ret;
1020
1021 req->direction = !!dep->number;
1022
1023 if (req->request.length == 0) {
Felipe Balbic0083092016-12-20 14:08:48 +02001024 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Felipe Balbia0807882012-05-04 13:03:54 +03001025 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301026 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001027 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbic74c6d42012-05-04 13:08:22 +03001028 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +03001029 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301030 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +02001031 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +03001032
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301033 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1034 &req->request, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001035 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001036 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001037 return;
1038 }
1039
Andrew Mortonc390b032013-03-08 09:42:50 +02001040 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301041
1042 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
1043 transfer_size = ALIGN(req->request.length - maxpacket,
1044 maxpacket);
Felipe Balbic0083092016-12-20 14:08:48 +02001045 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301046 req->request.dma,
1047 transfer_size,
1048 DWC3_TRBCTL_CONTROL_DATA,
1049 true);
1050 }
1051
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301052 transfer_size = roundup((req->request.length - transfer_size),
1053 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +03001054
1055 dwc->ep0_bounced = true;
1056
Felipe Balbic0083092016-12-20 14:08:48 +02001057 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Felipe Balbia0807882012-05-04 13:03:54 +03001058 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301059 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001060 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001061 } else {
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301062 ret = usb_gadget_map_request_by_dev(dwc->sysdev,
1063 &req->request, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001064 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001065 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001066 return;
1067 }
1068
Felipe Balbic0083092016-12-20 14:08:48 +02001069 dwc3_ep0_prepare_one_trb(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301070 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1071 false);
Felipe Balbic0083092016-12-20 14:08:48 +02001072 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001073 }
1074
1075 WARN_ON(ret < 0);
Mayank Rana08e41922017-03-02 15:25:48 -08001076 dbg_queue(dep->number, &req->request, ret);
Felipe Balbia0807882012-05-04 13:03:54 +03001077}
1078
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001079static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001080{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001081 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001082 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001083
1084 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1085 : DWC3_TRBCTL_CONTROL_STATUS2;
1086
Felipe Balbic0083092016-12-20 14:08:48 +02001087 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301088 dwc->ctrl_req_addr, 0, type, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001089 return dwc3_ep0_start_trans(dwc, dep->number);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001090}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001091
Felipe Balbi788a23f2012-05-21 14:22:41 +03001092static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001093{
Mayank Rana08e41922017-03-02 15:25:48 -08001094 int ret;
1095
Mayank Rana08e41922017-03-02 15:25:48 -08001096 ret = dwc3_ep0_start_control_status(dep);
1097 if (WARN_ON_ONCE(ret))
1098 dbg_event(dep->number, "ECTRLSTATUS", ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001099}
1100
Felipe Balbi788a23f2012-05-21 14:22:41 +03001101static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1102 const struct dwc3_event_depevt *event)
1103{
1104 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1105
1106 __dwc3_ep0_do_control_status(dwc, dep);
1107}
1108
Felipe Balbi2e3db062012-07-19 09:26:59 +03001109static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1110{
1111 struct dwc3_gadget_ep_cmd_params params;
1112 u32 cmd;
1113 int ret;
1114
1115 if (!dep->resource_index)
1116 return;
1117
1118 cmd = DWC3_DEPCMD_ENDTRANSFER;
1119 cmd |= DWC3_DEPCMD_CMDIOC;
1120 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1121 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001122 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Mayank Rana08e41922017-03-02 15:25:48 -08001123 if (ret) {
1124 dev_dbg(dwc->dev, "%s: send ep cmd ENDTRANSFER failed",
1125 dep->name);
1126 dbg_event(dep->number, "EENDXFER", ret);
1127 }
Felipe Balbi2e3db062012-07-19 09:26:59 +03001128 dep->resource_index = 0;
1129}
1130
Felipe Balbi72246da2011-08-19 18:10:58 +03001131static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1132 const struct dwc3_event_depevt *event)
1133{
Mayank Rana0c667b42017-02-09 11:56:51 -08001134 u8 epnum;
1135 struct dwc3_ep *dep;
1136
1137 epnum = event->endpoint_number;
1138 dep = dwc->eps[epnum];
1139
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001140 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001141 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001142 dwc3_trace(trace_dwc3_ep0, "Control Data");
Mayank Rana0c667b42017-02-09 11:56:51 -08001143 dep->dbg_ep_events.control_data++;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001144
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001145 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001146 * We already have a DATA transfer in the controller's cache,
1147 * if we receive a XferNotReady(DATA) we will ignore it, unless
1148 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001149 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001150 * In that case, we must issue END_TRANSFER command to the Data
1151 * Phase we already have started and issue SetStall on the
1152 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001153 */
1154 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001155 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1156
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001157 dwc3_trace(trace_dwc3_ep0,
1158 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001159 dwc3_ep0_end_control_data(dwc, dep);
Mayank Rana08e41922017-03-02 15:25:48 -08001160 dbg_event(epnum, "WRONGDR", 0);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001161 dwc3_ep0_stall_and_restart(dwc);
1162 return;
1163 }
1164
Felipe Balbi72246da2011-08-19 18:10:58 +03001165 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001166
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001167 case DEPEVT_STATUS_CONTROL_STATUS:
Mayank Rana0c667b42017-02-09 11:56:51 -08001168 dep->dbg_ep_events.control_status++;
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001169 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1170 return;
1171
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001172 dwc3_trace(trace_dwc3_ep0, "Control Status");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001173
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001174 dwc->ep0state = EP0_STATUS_PHASE;
1175
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001176 if (dwc->delayed_status) {
Mayank Rana08e41922017-03-02 15:25:48 -08001177 if (event->endpoint_number != 1)
1178 dbg_event(epnum, "EEPNUM", event->status);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001179 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001180 return;
1181 }
1182
Felipe Balbi788a23f2012-05-21 14:22:41 +03001183 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001184 }
1185}
1186
1187void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001188 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001189{
Mayank Rana0c667b42017-02-09 11:56:51 -08001190 struct dwc3_ep *dep;
1191 u8 epnum = event->endpoint_number;
1192
Felipe Balbif75cacc2016-05-23 11:10:08 +03001193 dwc3_trace(trace_dwc3_ep0, "%s: state '%s'",
1194 dwc3_ep_event_string(event),
Felipe Balbi72246da2011-08-19 18:10:58 +03001195 dwc3_ep0_state_string(dwc->ep0state));
1196
Mayank Rana0c667b42017-02-09 11:56:51 -08001197 dep = dwc->eps[epnum];
Felipe Balbi72246da2011-08-19 18:10:58 +03001198 switch (event->endpoint_event) {
1199 case DWC3_DEPEVT_XFERCOMPLETE:
1200 dwc3_ep0_xfer_complete(dwc, event);
Mayank Rana0c667b42017-02-09 11:56:51 -08001201 dep->dbg_ep_events.xfercomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001202 break;
1203
1204 case DWC3_DEPEVT_XFERNOTREADY:
1205 dwc3_ep0_xfernotready(dwc, event);
Mayank Rana0c667b42017-02-09 11:56:51 -08001206 dep->dbg_ep_events.xfernotready++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001207 break;
1208
1209 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08001210 dep->dbg_ep_events.xferinprogress++;
1211 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001212 case DWC3_DEPEVT_RXTXFIFOEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001213 dep->dbg_ep_events.rxtxfifoevent++;
1214 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001215 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001216 dep->dbg_ep_events.streamevent++;
1217 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001218 case DWC3_DEPEVT_EPCMDCMPLT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001219 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001220 break;
1221 }
1222}