blob: c2b29386caeee4c13a574b59853757bb8ab9d6ac [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;
591 int ret;
Pratyush Anande274a312012-07-02 10:21:54 +0530592 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +0300593
594 cfg = le16_to_cpu(ctrl->wValue);
595
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200596 switch (state) {
597 case USB_STATE_DEFAULT:
Felipe Balbi72246da2011-08-19 18:10:58 +0300598 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200600 case USB_STATE_ADDRESS:
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 ret = dwc3_ep0_delegate_req(dwc, ctrl);
602 /* if the cfg matches and the cfg is non zero */
Felipe Balbi457e84b2012-01-18 18:04:09 +0200603 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
Felipe Balbi7c812902013-07-22 12:41:47 +0300604
605 /*
606 * only change state if set_config has already
607 * been processed. If gadget driver returns
608 * USB_GADGET_DELAYED_STATUS, we will wait
609 * to change the state on the next usb_ep_queue()
610 */
611 if (ret == 0)
612 usb_gadget_set_state(&dwc->gadget,
613 USB_STATE_CONFIGURED);
Felipe Balbi14cd5922011-12-19 13:01:52 +0200614
Pratyush Anande274a312012-07-02 10:21:54 +0530615 /*
616 * Enable transition to U1/U2 state when
617 * nothing is pending from application.
618 */
619 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
620 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
621 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800622
623 dwc->resize_fifos = true;
624 dwc3_trace(trace_dwc3_ep0, "resize FIFOs flag SET");
Felipe Balbi457e84b2012-01-18 18:04:09 +0200625 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 break;
627
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200628 case USB_STATE_CONFIGURED:
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 ret = dwc3_ep0_delegate_req(dwc, ctrl);
Felipe Balbi7a42d832013-07-22 12:31:31 +0300630 if (!cfg && !ret)
Felipe Balbi14cd5922011-12-19 13:01:52 +0200631 usb_gadget_set_state(&dwc->gadget,
632 USB_STATE_ADDRESS);
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 break;
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100634 default:
635 ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 }
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100637 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300638}
639
Felipe Balbi865e09e2012-04-24 16:19:49 +0300640static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
641{
642 struct dwc3_ep *dep = to_dwc3_ep(ep);
643 struct dwc3 *dwc = dep->dwc;
644
645 u32 param = 0;
646 u32 reg;
647
648 struct timing {
649 u8 u1sel;
650 u8 u1pel;
John Youn501058e2016-05-23 11:32:40 -0700651 __le16 u2sel;
652 __le16 u2pel;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300653 } __packed timing;
654
655 int ret;
656
657 memcpy(&timing, req->buf, sizeof(timing));
658
659 dwc->u1sel = timing.u1sel;
660 dwc->u1pel = timing.u1pel;
Felipe Balbic8cf7af2012-05-31 11:00:28 +0300661 dwc->u2sel = le16_to_cpu(timing.u2sel);
662 dwc->u2pel = le16_to_cpu(timing.u2pel);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300663
664 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
665 if (reg & DWC3_DCTL_INITU2ENA)
666 param = dwc->u2pel;
667 if (reg & DWC3_DCTL_INITU1ENA)
668 param = dwc->u1pel;
669
670 /*
671 * According to Synopsys Databook, if parameter is
672 * greater than 125, a value of zero should be
673 * programmed in the register.
674 */
675 if (param > 125)
676 param = 0;
677
678 /* now that we have the time, issue DGCMD Set Sel */
679 ret = dwc3_send_gadget_generic_command(dwc,
680 DWC3_DGCMD_SET_PERIODIC_PAR, param);
Mayank Rana08e41922017-03-02 15:25:48 -0800681 if (WARN_ON(ret < 0))
682 dbg_event(dep->number, "ESET_SELCMPL", ret);
Felipe Balbi865e09e2012-04-24 16:19:49 +0300683}
684
685static int dwc3_ep0_set_sel(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
686{
687 struct dwc3_ep *dep;
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200688 enum usb_device_state state = dwc->gadget.state;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300689 u16 wLength;
690 u16 wValue;
691
Felipe Balbifdba5aa2013-01-25 11:28:19 +0200692 if (state == USB_STATE_DEFAULT)
Felipe Balbi865e09e2012-04-24 16:19:49 +0300693 return -EINVAL;
694
695 wValue = le16_to_cpu(ctrl->wValue);
696 wLength = le16_to_cpu(ctrl->wLength);
697
698 if (wLength != 6) {
699 dev_err(dwc->dev, "Set SEL should be 6 bytes, got %d\n",
700 wLength);
701 return -EINVAL;
702 }
703
704 /*
705 * To handle Set SEL we need to receive 6 bytes from Host. So let's
706 * queue a usb_request for 6 bytes.
707 *
708 * Remember, though, this controller can't handle non-wMaxPacketSize
709 * aligned transfers on the OUT direction, so we queue a request for
710 * wMaxPacketSize instead.
711 */
712 dep = dwc->eps[0];
713 dwc->ep0_usb_req.dep = dep;
714 dwc->ep0_usb_req.request.length = dep->endpoint.maxpacket;
715 dwc->ep0_usb_req.request.buf = dwc->setup_buf;
716 dwc->ep0_usb_req.request.complete = dwc3_ep0_set_sel_cmpl;
717
718 return __dwc3_gadget_ep0_queue(dep, &dwc->ep0_usb_req);
719}
720
Felipe Balbic12a0d82012-04-25 10:45:05 +0300721static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
722{
723 u16 wLength;
724 u16 wValue;
725 u16 wIndex;
726
727 wValue = le16_to_cpu(ctrl->wValue);
728 wLength = le16_to_cpu(ctrl->wLength);
729 wIndex = le16_to_cpu(ctrl->wIndex);
730
731 if (wIndex || wLength)
732 return -EINVAL;
733
734 /*
735 * REVISIT It's unclear from Databook what to do with this
736 * value. For now, just cache it.
737 */
738 dwc->isoch_delay = wValue;
739
740 return 0;
741}
742
Felipe Balbi72246da2011-08-19 18:10:58 +0300743static int dwc3_ep0_std_request(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
744{
745 int ret;
746
747 switch (ctrl->bRequest) {
748 case USB_REQ_GET_STATUS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500749 dwc3_trace(trace_dwc3_ep0, "USB_REQ_GET_STATUS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300750 ret = dwc3_ep0_handle_status(dwc, ctrl);
751 break;
752 case USB_REQ_CLEAR_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500753 dwc3_trace(trace_dwc3_ep0, "USB_REQ_CLEAR_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 ret = dwc3_ep0_handle_feature(dwc, ctrl, 0);
755 break;
756 case USB_REQ_SET_FEATURE:
Felipe Balbidab716c2014-09-24 11:22:23 -0500757 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_FEATURE");
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 ret = dwc3_ep0_handle_feature(dwc, ctrl, 1);
759 break;
760 case USB_REQ_SET_ADDRESS:
Felipe Balbidab716c2014-09-24 11:22:23 -0500761 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ADDRESS");
Felipe Balbi72246da2011-08-19 18:10:58 +0300762 ret = dwc3_ep0_set_address(dwc, ctrl);
763 break;
764 case USB_REQ_SET_CONFIGURATION:
Felipe Balbidab716c2014-09-24 11:22:23 -0500765 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_CONFIGURATION");
Felipe Balbi72246da2011-08-19 18:10:58 +0300766 ret = dwc3_ep0_set_config(dwc, ctrl);
767 break;
Felipe Balbi865e09e2012-04-24 16:19:49 +0300768 case USB_REQ_SET_SEL:
Felipe Balbidab716c2014-09-24 11:22:23 -0500769 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_SEL");
Felipe Balbi865e09e2012-04-24 16:19:49 +0300770 ret = dwc3_ep0_set_sel(dwc, ctrl);
771 break;
Felipe Balbic12a0d82012-04-25 10:45:05 +0300772 case USB_REQ_SET_ISOCH_DELAY:
Felipe Balbidab716c2014-09-24 11:22:23 -0500773 dwc3_trace(trace_dwc3_ep0, "USB_REQ_SET_ISOCH_DELAY");
Felipe Balbic12a0d82012-04-25 10:45:05 +0300774 ret = dwc3_ep0_set_isoch_delay(dwc, ctrl);
775 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300776 default:
Felipe Balbidab716c2014-09-24 11:22:23 -0500777 dwc3_trace(trace_dwc3_ep0, "Forwarding to gadget driver");
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 ret = dwc3_ep0_delegate_req(dwc, ctrl);
779 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700780 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
782 return ret;
783}
784
785static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
786 const struct dwc3_event_depevt *event)
787{
788 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
Felipe Balbief21ede2012-05-31 10:29:49 +0300789 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 u32 len;
791
792 if (!dwc->gadget_driver)
Felipe Balbief21ede2012-05-31 10:29:49 +0300793 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500795 trace_dwc3_ctrl_req(ctrl);
796
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 len = le16_to_cpu(ctrl->wLength);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300798 if (!len) {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300799 dwc->three_stage_setup = false;
800 dwc->ep0_expect_in = false;
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300801 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
802 } else {
Felipe Balbid95b09b2011-09-30 10:58:37 +0300803 dwc->three_stage_setup = true;
804 dwc->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300805 dwc->ep0_next_event = DWC3_EP0_NRDY_DATA;
806 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300807
Mayank Rana08e41922017-03-02 15:25:48 -0800808 dbg_setup(0x00, ctrl);
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
810 ret = dwc3_ep0_std_request(dwc, ctrl);
811 else
812 ret = dwc3_ep0_delegate_req(dwc, ctrl);
813
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +0100814 if (ret == USB_GADGET_DELAYED_STATUS)
815 dwc->delayed_status = true;
816
Felipe Balbief21ede2012-05-31 10:29:49 +0300817out:
Mayank Rana08e41922017-03-02 15:25:48 -0800818 if (ret < 0) {
819 dbg_event(0x0, "ERRSTAL", ret);
Felipe Balbief21ede2012-05-31 10:29:49 +0300820 dwc3_ep0_stall_and_restart(dwc);
Mayank Rana08e41922017-03-02 15:25:48 -0800821 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300822}
823
824static void dwc3_ep0_complete_data(struct dwc3 *dwc,
825 const struct dwc3_event_depevt *event)
826{
827 struct dwc3_request *r = NULL;
828 struct usb_request *ur;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200829 struct dwc3_trb *trb;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200830 struct dwc3_ep *ep0;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530831 unsigned transfer_size = 0;
832 unsigned maxp;
833 unsigned remaining_ur_length;
834 void *buf;
835 u32 transferred = 0;
Felipe Balbifca88922012-07-19 09:05:35 +0300836 u32 status;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200837 u32 length;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838 u8 epnum;
839
840 epnum = event->endpoint_number;
Sebastian Andrzej Siewiorc2da2ff2011-10-20 19:04:16 +0200841 ep0 = dwc->eps[0];
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Felipe Balbi1ddcb212011-08-30 15:52:17 +0300843 dwc->ep0_next_event = DWC3_EP0_NRDY_STATUS;
844
Felipe Balbif6bafc62012-02-06 11:04:53 +0200845 trb = dwc->ep0_trb;
Felipe Balbifca88922012-07-19 09:05:35 +0300846
Felipe Balbiccb072d2014-10-01 12:20:29 -0500847 trace_dwc3_complete_trb(ep0, trb);
848
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200849 r = next_request(&ep0->pending_list);
Felipe Balbi520fe762014-11-10 14:39:44 -0600850 if (!r)
851 return;
852
Felipe Balbifca88922012-07-19 09:05:35 +0300853 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
854 if (status == DWC3_TRBSTS_SETUP_PENDING) {
Felipe Balbib5d335e2015-11-16 16:20:34 -0600855 dwc->setup_packet_pending = true;
856
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500857 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbifca88922012-07-19 09:05:35 +0300858
859 if (r)
860 dwc3_gadget_giveback(ep0, r, -ECONNRESET);
861
862 return;
863 }
864
Felipe Balbi6856d302014-09-30 11:43:20 -0500865 ur = &r->request;
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530866 buf = ur->buf;
867 remaining_ur_length = ur->length;
Felipe Balbi6856d302014-09-30 11:43:20 -0500868
Felipe Balbif6bafc62012-02-06 11:04:53 +0200869 length = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +0300870
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530871 maxp = ep0->endpoint.maxpacket;
872
Felipe Balbia6829702011-08-27 22:18:09 +0300873 if (dwc->ep0_bounced) {
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530874 /*
875 * Handle the first TRB before handling the bounce buffer if
876 * the request length is greater than the bounce buffer size
877 */
878 if (ur->length > DWC3_EP0_BOUNCE_SIZE) {
879 transfer_size = ALIGN(ur->length - maxp, maxp);
880 transferred = transfer_size - length;
881 buf = (u8 *)buf + transferred;
882 ur->actual += transferred;
883 remaining_ur_length -= transferred;
884
885 trb++;
886 length = trb->size & DWC3_TRB_SIZE_MASK;
887
Felipe Balbi53fd8812016-04-04 15:33:41 +0300888 ep0->trb_enqueue = 0;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +0530889 }
890
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530891 transfer_size = roundup((ur->length - transfer_size),
892 maxp);
Kishon Vijay Abraham Ib2fb5b12015-07-27 12:25:27 +0530893
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +0530894 transferred = min_t(u32, remaining_ur_length,
895 transfer_size - length);
896 memcpy(buf, dwc->ep0_bounce, transferred);
Felipe Balbia6829702011-08-27 22:18:09 +0300897 } else {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200898 transferred = ur->length - length;
Felipe Balbia6829702011-08-27 22:18:09 +0300899 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
Felipe Balbicd423dd2012-03-21 11:44:00 +0200901 ur->actual += transferred;
902
Felipe Balbi72246da2011-08-19 18:10:58 +0300903 if ((epnum & 1) && ur->actual < ur->length) {
904 /* for some reason we did not get everything out */
Mayank Rana08e41922017-03-02 15:25:48 -0800905 dbg_event(epnum, "INDATSTAL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300906 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300907 } else {
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500908 dwc3_gadget_giveback(ep0, r, 0);
909
910 if (IS_ALIGNED(ur->length, ep0->endpoint.maxpacket) &&
911 ur->length && ur->zero) {
912 int ret;
913
914 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
915
Felipe Balbic0083092016-12-20 14:08:48 +0200916 dwc3_ep0_prepare_one_trb(dwc, epnum, dwc->ctrl_req_addr,
917 0, DWC3_TRBCTL_CONTROL_DATA, false);
918 ret = dwc3_ep0_start_trans(dwc, epnum);
Mayank Rana08e41922017-03-02 15:25:48 -0800919 if (WARN_ON(ret < 0))
920 dbg_event(epnum, "ECTRL_DATA", ret);
Felipe Balbi36f84ff2014-09-30 10:39:14 -0500921 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300922 }
923}
924
Felipe Balbi85a78102012-05-31 12:32:37 +0300925static void dwc3_ep0_complete_status(struct dwc3 *dwc,
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 const struct dwc3_event_depevt *event)
927{
928 struct dwc3_request *r;
929 struct dwc3_ep *dep;
Felipe Balbifca88922012-07-19 09:05:35 +0300930 struct dwc3_trb *trb;
931 u32 status;
Felipe Balbi72246da2011-08-19 18:10:58 +0300932
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300933 dep = dwc->eps[0];
Felipe Balbifca88922012-07-19 09:05:35 +0300934 trb = dwc->ep0_trb;
Felipe Balbi72246da2011-08-19 18:10:58 +0300935
Felipe Balbiccb072d2014-10-01 12:20:29 -0500936 trace_dwc3_complete_trb(dep, trb);
937
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200938 if (!list_empty(&dep->pending_list)) {
939 r = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300940
941 dwc3_gadget_giveback(dep, r, 0);
942 }
943
Gerard Cauvy3b637362012-02-10 12:21:18 +0200944 if (dwc->test_mode) {
945 int ret;
946
947 ret = dwc3_gadget_set_test_mode(dwc, dwc->test_mode_nr);
948 if (ret < 0) {
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500949 dwc3_trace(trace_dwc3_ep0, "Invalid Test #%d",
Gerard Cauvy3b637362012-02-10 12:21:18 +0200950 dwc->test_mode_nr);
Mayank Rana08e41922017-03-02 15:25:48 -0800951 dbg_event(0x00, "INVALTEST", ret);
Gerard Cauvy3b637362012-02-10 12:21:18 +0200952 dwc3_ep0_stall_and_restart(dwc);
Felipe Balbi5c81aba2012-06-25 19:30:49 +0300953 return;
Gerard Cauvy3b637362012-02-10 12:21:18 +0200954 }
955 }
956
Felipe Balbifca88922012-07-19 09:05:35 +0300957 status = DWC3_TRB_SIZE_TRBSTS(trb->size);
Felipe Balbib5d335e2015-11-16 16:20:34 -0600958 if (status == DWC3_TRBSTS_SETUP_PENDING) {
959 dwc->setup_packet_pending = true;
Felipe Balbidab716c2014-09-24 11:22:23 -0500960 dwc3_trace(trace_dwc3_ep0, "Setup Pending received");
Felipe Balbib5d335e2015-11-16 16:20:34 -0600961 }
Felipe Balbifca88922012-07-19 09:05:35 +0300962
Mayank Rana08e41922017-03-02 15:25:48 -0800963 dbg_print(dep->number, "DONE", status, "STATUS");
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300964 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 dwc3_ep0_out_start(dwc);
966}
967
968static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
969 const struct dwc3_event_depevt *event)
970{
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300971 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
972
973 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbib4996a82012-06-06 12:04:13 +0300974 dep->resource_index = 0;
Felipe Balbidf62df52011-10-14 15:11:49 +0300975 dwc->setup_packet_pending = false;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300976
Felipe Balbi72246da2011-08-19 18:10:58 +0300977 switch (dwc->ep0state) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300978 case EP0_SETUP_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500979 dwc3_trace(trace_dwc3_ep0, "Setup Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 dwc3_ep0_inspect_setup(dwc, event);
981 break;
982
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300983 case EP0_DATA_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500984 dwc3_trace(trace_dwc3_ep0, "Data Phase");
Felipe Balbi72246da2011-08-19 18:10:58 +0300985 dwc3_ep0_complete_data(dwc, event);
986 break;
987
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300988 case EP0_STATUS_PHASE:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500989 dwc3_trace(trace_dwc3_ep0, "Status Phase");
Felipe Balbi85a78102012-05-31 12:32:37 +0300990 dwc3_ep0_complete_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 break;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +0300992 default:
993 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 }
995}
996
Felipe Balbia0807882012-05-04 13:03:54 +0300997static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
998 struct dwc3_ep *dep, struct dwc3_request *req)
999{
1000 int ret;
1001
1002 req->direction = !!dep->number;
1003
1004 if (req->request.length == 0) {
Felipe Balbic0083092016-12-20 14:08:48 +02001005 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Felipe Balbia0807882012-05-04 13:03:54 +03001006 dwc->ctrl_req_addr, 0,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301007 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001008 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbic74c6d42012-05-04 13:08:22 +03001009 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
Felipe Balbia0807882012-05-04 13:03:54 +03001010 && (dep->number == 0)) {
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301011 u32 transfer_size = 0;
Andrew Mortonc390b032013-03-08 09:42:50 +02001012 u32 maxpacket;
Felipe Balbia0807882012-05-04 13:03:54 +03001013
1014 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1015 dep->number);
1016 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001017 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001018 return;
1019 }
1020
Andrew Mortonc390b032013-03-08 09:42:50 +02001021 maxpacket = dep->endpoint.maxpacket;
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301022
1023 if (req->request.length > DWC3_EP0_BOUNCE_SIZE) {
1024 transfer_size = ALIGN(req->request.length - maxpacket,
1025 maxpacket);
Felipe Balbic0083092016-12-20 14:08:48 +02001026 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Kishon Vijay Abraham Ic0bd5452015-07-27 12:25:32 +05301027 req->request.dma,
1028 transfer_size,
1029 DWC3_TRBCTL_CONTROL_DATA,
1030 true);
1031 }
1032
Kishon Vijay Abraham I8a344222015-07-27 12:25:29 +05301033 transfer_size = roundup((req->request.length - transfer_size),
1034 maxpacket);
Felipe Balbia0807882012-05-04 13:03:54 +03001035
1036 dwc->ep0_bounced = true;
1037
Felipe Balbic0083092016-12-20 14:08:48 +02001038 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Felipe Balbia0807882012-05-04 13:03:54 +03001039 dwc->ep0_bounce_addr, transfer_size,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301040 DWC3_TRBCTL_CONTROL_DATA, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001041 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001042 } else {
1043 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1044 dep->number);
1045 if (ret) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001046 dwc3_trace(trace_dwc3_ep0, "failed to map request");
Felipe Balbia0807882012-05-04 13:03:54 +03001047 return;
1048 }
1049
Felipe Balbic0083092016-12-20 14:08:48 +02001050 dwc3_ep0_prepare_one_trb(dwc, dep->number, req->request.dma,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301051 req->request.length, DWC3_TRBCTL_CONTROL_DATA,
1052 false);
Felipe Balbic0083092016-12-20 14:08:48 +02001053 ret = dwc3_ep0_start_trans(dwc, dep->number);
Felipe Balbia0807882012-05-04 13:03:54 +03001054 }
1055
1056 WARN_ON(ret < 0);
Mayank Rana08e41922017-03-02 15:25:48 -08001057 dbg_queue(dep->number, &req->request, ret);
Felipe Balbia0807882012-05-04 13:03:54 +03001058}
1059
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001060static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001061{
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001062 struct dwc3 *dwc = dep->dwc;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001063 u32 type;
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001064
1065 type = dwc->three_stage_setup ? DWC3_TRBCTL_CONTROL_STATUS3
1066 : DWC3_TRBCTL_CONTROL_STATUS2;
1067
Felipe Balbic0083092016-12-20 14:08:48 +02001068 dwc3_ep0_prepare_one_trb(dwc, dep->number,
Kishon Vijay Abraham I368ca112015-07-27 12:25:30 +05301069 dwc->ctrl_req_addr, 0, type, false);
Felipe Balbic0083092016-12-20 14:08:48 +02001070 return dwc3_ep0_start_trans(dwc, dep->number);
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001071}
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001072
Felipe Balbi788a23f2012-05-21 14:22:41 +03001073static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001074{
Mayank Rana08e41922017-03-02 15:25:48 -08001075 int ret;
1076
Mayank Ranaa8e4de62016-12-13 17:11:15 -08001077 if (dwc->resize_fifos) {
1078 dwc3_trace(trace_dwc3_ep0, "Resizing FIFOs");
1079 dwc3_gadget_resize_tx_fifos(dwc);
1080 dwc->resize_fifos = 0;
1081 }
1082
Mayank Rana08e41922017-03-02 15:25:48 -08001083 ret = dwc3_ep0_start_control_status(dep);
1084 if (WARN_ON_ONCE(ret))
1085 dbg_event(dep->number, "ECTRLSTATUS", ret);
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001086}
1087
Felipe Balbi788a23f2012-05-21 14:22:41 +03001088static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
1089 const struct dwc3_event_depevt *event)
1090{
1091 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
1092
1093 __dwc3_ep0_do_control_status(dwc, dep);
1094}
1095
Felipe Balbi2e3db062012-07-19 09:26:59 +03001096static void dwc3_ep0_end_control_data(struct dwc3 *dwc, struct dwc3_ep *dep)
1097{
1098 struct dwc3_gadget_ep_cmd_params params;
1099 u32 cmd;
1100 int ret;
1101
1102 if (!dep->resource_index)
1103 return;
1104
1105 cmd = DWC3_DEPCMD_ENDTRANSFER;
1106 cmd |= DWC3_DEPCMD_CMDIOC;
1107 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1108 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03001109 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Mayank Rana08e41922017-03-02 15:25:48 -08001110 if (ret) {
1111 dev_dbg(dwc->dev, "%s: send ep cmd ENDTRANSFER failed",
1112 dep->name);
1113 dbg_event(dep->number, "EENDXFER", ret);
1114 }
Felipe Balbi2e3db062012-07-19 09:26:59 +03001115 dep->resource_index = 0;
1116}
1117
Felipe Balbi72246da2011-08-19 18:10:58 +03001118static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
1119 const struct dwc3_event_depevt *event)
1120{
Mayank Rana0c667b42017-02-09 11:56:51 -08001121 u8 epnum;
1122 struct dwc3_ep *dep;
1123
1124 epnum = event->endpoint_number;
1125 dep = dwc->eps[epnum];
1126
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001127 switch (event->status) {
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001128 case DEPEVT_STATUS_CONTROL_DATA:
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001129 dwc3_trace(trace_dwc3_ep0, "Control Data");
Mayank Rana0c667b42017-02-09 11:56:51 -08001130 dep->dbg_ep_events.control_data++;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001131
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001132 /*
Felipe Balbi2e3db062012-07-19 09:26:59 +03001133 * We already have a DATA transfer in the controller's cache,
1134 * if we receive a XferNotReady(DATA) we will ignore it, unless
1135 * it's for the wrong direction.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001136 *
Felipe Balbi2e3db062012-07-19 09:26:59 +03001137 * In that case, we must issue END_TRANSFER command to the Data
1138 * Phase we already have started and issue SetStall on the
1139 * control endpoint.
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001140 */
1141 if (dwc->ep0_expect_in != event->endpoint_number) {
Felipe Balbi2e3db062012-07-19 09:26:59 +03001142 struct dwc3_ep *dep = dwc->eps[dwc->ep0_expect_in];
1143
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001144 dwc3_trace(trace_dwc3_ep0,
1145 "Wrong direction for Data phase");
Felipe Balbi2e3db062012-07-19 09:26:59 +03001146 dwc3_ep0_end_control_data(dwc, dep);
Mayank Rana08e41922017-03-02 15:25:48 -08001147 dbg_event(epnum, "WRONGDR", 0);
Felipe Balbi55f3fba2011-09-08 18:27:33 +03001148 dwc3_ep0_stall_and_restart(dwc);
1149 return;
1150 }
1151
Felipe Balbi72246da2011-08-19 18:10:58 +03001152 break;
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001153
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001154 case DEPEVT_STATUS_CONTROL_STATUS:
Mayank Rana0c667b42017-02-09 11:56:51 -08001155 dep->dbg_ep_events.control_status++;
Felipe Balbi77fa6df2012-07-23 09:09:32 +03001156 if (dwc->ep0_next_event != DWC3_EP0_NRDY_STATUS)
1157 return;
1158
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001159 dwc3_trace(trace_dwc3_ep0, "Control Status");
Felipe Balbi1ddcb212011-08-30 15:52:17 +03001160
Sebastian Andrzej Siewiorf0f2b2a2011-11-02 13:30:44 +01001161 dwc->ep0state = EP0_STATUS_PHASE;
1162
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001163 if (dwc->delayed_status) {
Mayank Rana08e41922017-03-02 15:25:48 -08001164 if (event->endpoint_number != 1)
1165 dbg_event(epnum, "EEPNUM", event->status);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001166 dwc3_trace(trace_dwc3_ep0, "Delayed Status");
Sebastian Andrzej Siewior5bdb1dc2011-11-02 13:30:45 +01001167 return;
1168 }
1169
Felipe Balbi788a23f2012-05-21 14:22:41 +03001170 dwc3_ep0_do_control_status(dwc, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03001171 }
1172}
1173
1174void dwc3_ep0_interrupt(struct dwc3 *dwc,
Felipe Balbi8becf272011-11-04 12:40:05 +02001175 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001176{
Mayank Rana0c667b42017-02-09 11:56:51 -08001177 struct dwc3_ep *dep;
1178 u8 epnum = event->endpoint_number;
1179
Felipe Balbif75cacc2016-05-23 11:10:08 +03001180 dwc3_trace(trace_dwc3_ep0, "%s: state '%s'",
1181 dwc3_ep_event_string(event),
Felipe Balbi72246da2011-08-19 18:10:58 +03001182 dwc3_ep0_state_string(dwc->ep0state));
1183
Mayank Rana0c667b42017-02-09 11:56:51 -08001184 dep = dwc->eps[epnum];
Felipe Balbi72246da2011-08-19 18:10:58 +03001185 switch (event->endpoint_event) {
1186 case DWC3_DEPEVT_XFERCOMPLETE:
1187 dwc3_ep0_xfer_complete(dwc, event);
Mayank Rana0c667b42017-02-09 11:56:51 -08001188 dep->dbg_ep_events.xfercomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001189 break;
1190
1191 case DWC3_DEPEVT_XFERNOTREADY:
1192 dwc3_ep0_xfernotready(dwc, event);
Mayank Rana0c667b42017-02-09 11:56:51 -08001193 dep->dbg_ep_events.xfernotready++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001194 break;
1195
1196 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08001197 dep->dbg_ep_events.xferinprogress++;
1198 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 case DWC3_DEPEVT_RXTXFIFOEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001200 dep->dbg_ep_events.rxtxfifoevent++;
1201 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001202 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001203 dep->dbg_ep_events.streamevent++;
1204 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001205 case DWC3_DEPEVT_EPCMDCMPLT:
Mayank Rana0c667b42017-02-09 11:56:51 -08001206 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03001207 break;
1208 }
1209}