blob: 3c4fd485431784039399e92a3e6ed28ffc1f6f28 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
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/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
John Youndca01192016-05-19 17:26:05 -0700148/**
149 * dwc3_ep_inc_trb() - Increment a TRB index.
150 * @index - Pointer to the TRB index to increment.
151 *
152 * The index should never point to the link TRB. After incrementing,
153 * if it is point to the link TRB, wrap around to the beginning. The
154 * link TRB is always at the last TRB entry.
155 */
156static void dwc3_ep_inc_trb(u8 *index)
157{
158 (*index)++;
159 if (*index == (DWC3_TRB_NUM - 1))
160 *index = 0;
161}
162
Felipe Balbief966b92016-04-05 13:09:51 +0300163static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200164{
John Youndca01192016-05-19 17:26:05 -0700165 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300166}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167
Felipe Balbief966b92016-04-05 13:09:51 +0300168static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
169{
John Youndca01192016-05-19 17:26:05 -0700170 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200171}
172
Felipe Balbi72246da2011-08-19 18:10:58 +0300173void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
174 int status)
175{
176 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530177 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300178
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200179 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530180 i = 0;
181 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300182 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530183 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200184 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300185 }
186 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200187 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300188
189 if (req->request.status == -EINPROGRESS)
190 req->request.status = status;
191
Pratyush Anand0416e492012-08-10 13:42:16 +0530192 if (dwc->ep0_bounced && dep->number == 0)
193 dwc->ep0_bounced = false;
194 else
195 usb_gadget_unmap_request(&dwc->gadget, &req->request,
196 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300197
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500198 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300199
200 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200201 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300202 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300203
204 if (dep->number > 1)
205 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300206}
207
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500208int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300209{
210 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300211 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300212 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300213 u32 reg;
214
215 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
216 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
217
218 do {
219 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
220 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300221 status = DWC3_DGCMD_STATUS(reg);
222 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300223 ret = -EINVAL;
224 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300225 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300226 } while (timeout--);
227
228 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300229 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300230 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300231 }
232
Felipe Balbi71f7e702016-05-23 14:16:19 +0300233 trace_dwc3_gadget_generic_cmd(cmd, param, status);
234
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236}
237
Felipe Balbic36d8e92016-04-04 12:46:33 +0300238static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
239
Felipe Balbi2cd47182016-04-12 16:42:43 +0300240int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
241 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300242{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300243 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200244 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300245 u32 reg;
246
Felipe Balbi0933df12016-05-23 14:02:33 +0300247 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300249 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300251 /*
252 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
253 * we're issuing an endpoint command, we must check if
254 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
255 *
256 * We will also set SUSPHY bit to what it was before returning as stated
257 * by the same section on Synopsys databook.
258 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300259 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
260 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
261 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
262 susphy = true;
263 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
264 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
265 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300266 }
267
Felipe Balbic36d8e92016-04-04 12:46:33 +0300268 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
269 int needs_wakeup;
270
271 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
272 dwc->link_state == DWC3_LINK_STATE_U2 ||
273 dwc->link_state == DWC3_LINK_STATE_U3);
274
275 if (unlikely(needs_wakeup)) {
276 ret = __dwc3_gadget_wakeup(dwc);
277 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
278 ret);
279 }
280 }
281
Felipe Balbi2eb88012016-04-12 16:53:39 +0300282 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
283 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
284 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
Felipe Balbi2eb88012016-04-12 16:53:39 +0300286 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300288 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300290 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000291
Felipe Balbi73815282015-01-27 13:48:14 -0600292 dwc3_trace(trace_dwc3_gadget,
293 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294 cmd_status);
295
296 switch (cmd_status) {
297 case 0:
298 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300299 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000300 case DEPEVT_TRANSFER_NO_RESOURCE:
Felipe Balbiba159842016-05-23 13:50:29 +0300301 dwc3_trace(trace_dwc3_gadget, "no resource available");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000302 ret = -EINVAL;
303 break;
304 case DEPEVT_TRANSFER_BUS_EXPIRY:
305 /*
306 * SW issues START TRANSFER command to
307 * isochronous ep with future frame interval. If
308 * future interval time has already passed when
309 * core receives the command, it will respond
310 * with an error status of 'Bus Expiry'.
311 *
312 * Instead of always returning -EINVAL, let's
313 * give a hint to the gadget driver that this is
314 * the case by returning -EAGAIN.
315 */
Felipe Balbiba159842016-05-23 13:50:29 +0300316 dwc3_trace(trace_dwc3_gadget, "bus expiry");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000317 ret = -EAGAIN;
318 break;
319 default:
320 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
321 }
322
Felipe Balbic0ca3242016-04-04 09:11:51 +0300323 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300325 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326
Felipe Balbif6bb2252016-05-23 13:53:34 +0300327 if (timeout == 0) {
328 dwc3_trace(trace_dwc3_gadget,
329 "Command Timed Out");
330 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300331 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300332 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300333
Felipe Balbi0933df12016-05-23 14:02:33 +0300334 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
335
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300336 if (unlikely(susphy)) {
337 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
338 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
339 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
340 }
341
Felipe Balbic0ca3242016-04-04 09:11:51 +0300342 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300343}
344
John Youn50c763f2016-05-31 17:49:56 -0700345static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
346{
347 struct dwc3 *dwc = dep->dwc;
348 struct dwc3_gadget_ep_cmd_params params;
349 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
350
351 /*
352 * As of core revision 2.60a the recommended programming model
353 * is to set the ClearPendIN bit when issuing a Clear Stall EP
354 * command for IN endpoints. This is to prevent an issue where
355 * some (non-compliant) hosts may not send ACK TPs for pending
356 * IN transfers due to a mishandled error condition. Synopsys
357 * STAR 9000614252.
358 */
359 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
360 cmd |= DWC3_DEPCMD_CLEARPENDIN;
361
362 memset(&params, 0, sizeof(params));
363
Felipe Balbi2cd47182016-04-12 16:42:43 +0300364 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700365}
366
Felipe Balbi72246da2011-08-19 18:10:58 +0300367static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200368 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300369{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300370 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300371
372 return dep->trb_pool_dma + offset;
373}
374
375static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
376{
377 struct dwc3 *dwc = dep->dwc;
378
379 if (dep->trb_pool)
380 return 0;
381
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 dep->trb_pool = dma_alloc_coherent(dwc->dev,
383 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
384 &dep->trb_pool_dma, GFP_KERNEL);
385 if (!dep->trb_pool) {
386 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
387 dep->name);
388 return -ENOMEM;
389 }
390
391 return 0;
392}
393
394static void dwc3_free_trb_pool(struct dwc3_ep *dep)
395{
396 struct dwc3 *dwc = dep->dwc;
397
398 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
399 dep->trb_pool, dep->trb_pool_dma);
400
401 dep->trb_pool = NULL;
402 dep->trb_pool_dma = 0;
403}
404
John Younc4509602016-02-16 20:10:53 -0800405static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
406
407/**
408 * dwc3_gadget_start_config - Configure EP resources
409 * @dwc: pointer to our controller context structure
410 * @dep: endpoint that is being enabled
411 *
412 * The assignment of transfer resources cannot perfectly follow the
413 * data book due to the fact that the controller driver does not have
414 * all knowledge of the configuration in advance. It is given this
415 * information piecemeal by the composite gadget framework after every
416 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
417 * programming model in this scenario can cause errors. For two
418 * reasons:
419 *
420 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
421 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
422 * multiple interfaces.
423 *
424 * 2) The databook does not mention doing more DEPXFERCFG for new
425 * endpoint on alt setting (8.1.6).
426 *
427 * The following simplified method is used instead:
428 *
429 * All hardware endpoints can be assigned a transfer resource and this
430 * setting will stay persistent until either a core reset or
431 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
432 * do DEPXFERCFG for every hardware endpoint as well. We are
433 * guaranteed that there are as many transfer resources as endpoints.
434 *
435 * This function is called for each endpoint when it is being enabled
436 * but is triggered only when called for EP0-out, which always happens
437 * first, and which should only happen in one of the above conditions.
438 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300439static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
440{
441 struct dwc3_gadget_ep_cmd_params params;
442 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800443 int i;
444 int ret;
445
446 if (dep->number)
447 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300448
449 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800450 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
Felipe Balbi2cd47182016-04-12 16:42:43 +0300452 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800453 if (ret)
454 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
John Younc4509602016-02-16 20:10:53 -0800456 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
457 struct dwc3_ep *dep = dwc->eps[i];
458
459 if (!dep)
460 continue;
461
462 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
463 if (ret)
464 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 }
466
467 return 0;
468}
469
470static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200471 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300472 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600473 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300474{
475 struct dwc3_gadget_ep_cmd_params params;
476
477 memset(&params, 0x00, sizeof(params));
478
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300479 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900480 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
481
482 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800483 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300484 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300485 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900486 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300487
Felipe Balbi4b345c92012-07-16 14:08:16 +0300488 if (ignore)
489 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
490
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600491 if (restore) {
492 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
493 params.param2 |= dep->saved_state;
494 }
495
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300496 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
497 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300498
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200499 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300500 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
501 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300502 dep->stream_capable = true;
503 }
504
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500505 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300506 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300507
508 /*
509 * We are doing 1:1 mapping for endpoints, meaning
510 * Physical Endpoints 2 maps to Logical Endpoint 2 and
511 * so on. We consider the direction bit as part of the physical
512 * endpoint number. So USB endpoint 0x81 is 0x03.
513 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300514 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300515
516 /*
517 * We must use the lower 16 TX FIFOs even though
518 * HW might have more
519 */
520 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300521 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300522
523 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300524 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 dep->interval = 1 << (desc->bInterval - 1);
526 }
527
Felipe Balbi2cd47182016-04-12 16:42:43 +0300528 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300529}
530
531static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
532{
533 struct dwc3_gadget_ep_cmd_params params;
534
535 memset(&params, 0x00, sizeof(params));
536
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300537 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300538
Felipe Balbi2cd47182016-04-12 16:42:43 +0300539 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
540 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300541}
542
543/**
544 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
545 * @dep: endpoint to be initialized
546 * @desc: USB Endpoint Descriptor
547 *
548 * Caller should take care of locking
549 */
550static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200551 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300552 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600553 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300554{
555 struct dwc3 *dwc = dep->dwc;
556 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300557 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300558
Felipe Balbi73815282015-01-27 13:48:14 -0600559 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300560
Felipe Balbi72246da2011-08-19 18:10:58 +0300561 if (!(dep->flags & DWC3_EP_ENABLED)) {
562 ret = dwc3_gadget_start_config(dwc, dep);
563 if (ret)
564 return ret;
565 }
566
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600567 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
568 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300569 if (ret)
570 return ret;
571
572 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200573 struct dwc3_trb *trb_st_hw;
574 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300575
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200576 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200577 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 dep->type = usb_endpoint_type(desc);
579 dep->flags |= DWC3_EP_ENABLED;
580
581 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
582 reg |= DWC3_DALEPENA_EP(dep->number);
583 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
584
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300585 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300586 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
John Youn0d257442016-05-19 17:26:08 -0700588 /* Initialize the TRB ring */
589 dep->trb_dequeue = 0;
590 dep->trb_enqueue = 0;
591 memset(dep->trb_pool, 0,
592 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
593
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300594 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 trb_st_hw = &dep->trb_pool[0];
596
Felipe Balbif6bafc62012-02-06 11:04:53 +0200597 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200598 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
599 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
600 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
601 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300602 }
603
604 return 0;
605}
606
Paul Zimmermanb992e682012-04-27 14:17:35 +0300607static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200608static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300609{
610 struct dwc3_request *req;
611
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200612 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300613 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200614
Pratyush Anand57911502012-07-06 15:19:10 +0530615 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200616 while (!list_empty(&dep->started_list)) {
617 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530618
619 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
620 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200621 }
622
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200623 while (!list_empty(&dep->pending_list)) {
624 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300625
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200626 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300627 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300628}
629
630/**
631 * __dwc3_gadget_ep_disable - Disables a HW endpoint
632 * @dep: the endpoint to disable
633 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200634 * This function also removes requests which are currently processed ny the
635 * hardware and those which are not yet scheduled.
636 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300638static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
639{
640 struct dwc3 *dwc = dep->dwc;
641 u32 reg;
642
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500643 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
644
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200645 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300646
Felipe Balbi687ef982014-04-16 10:30:33 -0500647 /* make sure HW endpoint isn't stalled */
648 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500649 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500650
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
652 reg &= ~DWC3_DALEPENA_EP(dep->number);
653 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
654
Felipe Balbi879631a2011-09-30 10:58:47 +0300655 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200656 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200657 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300659 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300660
661 return 0;
662}
663
664/* -------------------------------------------------------------------------- */
665
666static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
667 const struct usb_endpoint_descriptor *desc)
668{
669 return -EINVAL;
670}
671
672static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
673{
674 return -EINVAL;
675}
676
677/* -------------------------------------------------------------------------- */
678
679static int dwc3_gadget_ep_enable(struct usb_ep *ep,
680 const struct usb_endpoint_descriptor *desc)
681{
682 struct dwc3_ep *dep;
683 struct dwc3 *dwc;
684 unsigned long flags;
685 int ret;
686
687 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
688 pr_debug("dwc3: invalid parameters\n");
689 return -EINVAL;
690 }
691
692 if (!desc->wMaxPacketSize) {
693 pr_debug("dwc3: missing wMaxPacketSize\n");
694 return -EINVAL;
695 }
696
697 dep = to_dwc3_ep(ep);
698 dwc = dep->dwc;
699
Felipe Balbi95ca9612015-12-10 13:08:20 -0600700 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
701 "%s is already enabled\n",
702 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300703 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300704
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600706 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300707 spin_unlock_irqrestore(&dwc->lock, flags);
708
709 return ret;
710}
711
712static int dwc3_gadget_ep_disable(struct usb_ep *ep)
713{
714 struct dwc3_ep *dep;
715 struct dwc3 *dwc;
716 unsigned long flags;
717 int ret;
718
719 if (!ep) {
720 pr_debug("dwc3: invalid parameters\n");
721 return -EINVAL;
722 }
723
724 dep = to_dwc3_ep(ep);
725 dwc = dep->dwc;
726
Felipe Balbi95ca9612015-12-10 13:08:20 -0600727 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
728 "%s is already disabled\n",
729 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300731
Felipe Balbi72246da2011-08-19 18:10:58 +0300732 spin_lock_irqsave(&dwc->lock, flags);
733 ret = __dwc3_gadget_ep_disable(dep);
734 spin_unlock_irqrestore(&dwc->lock, flags);
735
736 return ret;
737}
738
739static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
740 gfp_t gfp_flags)
741{
742 struct dwc3_request *req;
743 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
745 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900746 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300747 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300748
749 req->epnum = dep->number;
750 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300751
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500752 trace_dwc3_alloc_request(req);
753
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 return &req->request;
755}
756
757static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
758 struct usb_request *request)
759{
760 struct dwc3_request *req = to_dwc3_request(request);
761
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500762 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 kfree(req);
764}
765
Felipe Balbic71fc372011-11-22 11:37:34 +0200766/**
767 * dwc3_prepare_one_trb - setup one TRB from one request
768 * @dep: endpoint for which this request is prepared
769 * @req: dwc3_request pointer
770 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200771static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200772 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530773 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200774{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200775 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200776
Felipe Balbi73815282015-01-27 13:48:14 -0600777 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200778 dep->name, req, (unsigned long long) dma,
779 length, last ? " last" : "",
780 chain ? " chain" : "");
781
Pratyush Anand915e2022013-01-14 15:59:35 +0530782
Felipe Balbi4faf7552016-04-05 13:14:31 +0300783 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200784
Felipe Balbieeb720f2011-11-28 12:46:59 +0200785 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200786 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200787 req->trb = trb;
788 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300789 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200790 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200791
Felipe Balbief966b92016-04-05 13:09:51 +0300792 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530793
Felipe Balbif6bafc62012-02-06 11:04:53 +0200794 trb->size = DWC3_TRB_SIZE_LENGTH(length);
795 trb->bpl = lower_32_bits(dma);
796 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200797
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200798 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200799 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200800 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200801 break;
802
803 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530804 if (!node)
805 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
806 else
807 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200808
809 /* always enable Interrupt on Missed ISOC */
810 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200811 break;
812
813 case USB_ENDPOINT_XFER_BULK:
814 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200815 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200816 break;
817 default:
818 /*
819 * This is only possible with faulty memory because we
820 * checked it already :)
821 */
822 BUG();
823 }
824
Felipe Balbica4d44e2016-03-10 13:53:27 +0200825 /* always enable Continue on Short Packet */
826 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600827
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300828 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200829 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
830
831 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530832 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200833
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530834 if (chain)
835 trb->ctrl |= DWC3_TRB_CTRL_CHN;
836
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200837 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200838 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
839
840 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500841
842 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200843}
844
Felipe Balbic4233572016-05-12 14:08:34 +0300845static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
846{
847 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700848 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300849
850 /*
851 * If enqueue & dequeue are equal than it is either full or empty.
852 *
853 * One way to know for sure is if the TRB right before us has HWO bit
854 * set or not. If it has, then we're definitely full and can't fit any
855 * more transfers in our ring.
856 */
857 if (dep->trb_enqueue == dep->trb_dequeue) {
858 /* If we're full, enqueue/dequeue are > 0 */
859 if (dep->trb_enqueue) {
860 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
861 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
862 return 0;
863 }
864
865 return DWC3_TRB_NUM - 1;
866 }
867
John Youn32db3d92016-05-19 17:26:12 -0700868 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
869 trbs_left %= DWC3_TRB_NUM;
870
John Youn7d0a0382016-05-19 17:26:15 -0700871 if (dep->trb_dequeue < dep->trb_enqueue)
872 trbs_left--;
873
John Youn32db3d92016-05-19 17:26:12 -0700874 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300875}
876
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300877static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
878 struct dwc3_request *req, unsigned int trbs_left)
879{
880 struct usb_request *request = &req->request;
881 struct scatterlist *sg = request->sg;
882 struct scatterlist *s;
883 unsigned int last = false;
884 unsigned int length;
885 dma_addr_t dma;
886 int i;
887
888 for_each_sg(sg, s, request->num_mapped_sgs, i) {
889 unsigned chain = true;
890
891 length = sg_dma_len(s);
892 dma = sg_dma_address(s);
893
894 if (sg_is_last(s)) {
895 if (list_is_last(&req->list, &dep->pending_list))
896 last = true;
897
898 chain = false;
899 }
900
901 if (!trbs_left)
902 last = true;
903
904 if (last)
905 chain = false;
906
907 dwc3_prepare_one_trb(dep, req, dma, length,
908 last, chain, i);
909
910 if (last)
911 break;
912 }
913}
914
915static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
916 struct dwc3_request *req, unsigned int trbs_left)
917{
918 unsigned int last = false;
919 unsigned int length;
920 dma_addr_t dma;
921
922 dma = req->request.dma;
923 length = req->request.length;
924
925 if (!trbs_left)
926 last = true;
927
928 /* Is this the last request? */
929 if (list_is_last(&req->list, &dep->pending_list))
930 last = true;
931
932 dwc3_prepare_one_trb(dep, req, dma, length,
933 last, false, 0);
934}
935
Felipe Balbi72246da2011-08-19 18:10:58 +0300936/*
937 * dwc3_prepare_trbs - setup TRBs from requests
938 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800940 * The function goes through the requests list and sets up TRBs for the
941 * transfers. The function returns once there are no more TRBs available or
942 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300943 */
Felipe Balbic4233572016-05-12 14:08:34 +0300944static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300945{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200946 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 u32 trbs_left;
948
949 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
950
Felipe Balbic4233572016-05-12 14:08:34 +0300951 trbs_left = dwc3_calc_trbs_left(dep);
John Youn89bc8562016-05-19 17:26:10 -0700952 if (!trbs_left)
953 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300954
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200955 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300956 if (req->request.num_mapped_sgs > 0)
957 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
958 else
959 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300960
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300961 if (!trbs_left)
962 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300963 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300964}
965
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300966static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300967{
968 struct dwc3_gadget_ep_cmd_params params;
969 struct dwc3_request *req;
970 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300971 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 int ret;
973 u32 cmd;
974
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300975 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300977 dwc3_prepare_trbs(dep);
978 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300979 if (!req) {
980 dep->flags |= DWC3_EP_PENDING_REQUEST;
981 return 0;
982 }
983
984 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300985
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300986 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530987 params.param0 = upper_32_bits(req->trb_dma);
988 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530990 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530992 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300993
994 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +0300995 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 /*
998 * FIXME we need to iterate over the list of requests
999 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001000 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001001 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001002 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1003 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 list_del(&req->list);
1005 return ret;
1006 }
1007
1008 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001009
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001010 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001011 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001012 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001013 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001014
Felipe Balbi72246da2011-08-19 18:10:58 +03001015 return 0;
1016}
1017
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301018static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1019 struct dwc3_ep *dep, u32 cur_uf)
1020{
1021 u32 uf;
1022
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001023 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001024 dwc3_trace(trace_dwc3_gadget,
1025 "ISOC ep %s run out for requests",
1026 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301027 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301028 return;
1029 }
1030
1031 /* 4 micro frames in the future */
1032 uf = cur_uf + dep->interval * 4;
1033
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001034 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301035}
1036
1037static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1038 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1039{
1040 u32 cur_uf, mask;
1041
1042 mask = ~(dep->interval - 1);
1043 cur_uf = event->parameters & mask;
1044
1045 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1046}
1047
Felipe Balbi72246da2011-08-19 18:10:58 +03001048static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1049{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001050 struct dwc3 *dwc = dep->dwc;
1051 int ret;
1052
Felipe Balbibb423982015-11-16 15:31:21 -06001053 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001054 dwc3_trace(trace_dwc3_gadget,
1055 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001056 &req->request, dep->endpoint.name);
1057 return -ESHUTDOWN;
1058 }
1059
1060 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1061 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001062 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1063 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001064 return -EINVAL;
1065 }
1066
Felipe Balbifc8bb912016-05-16 13:14:48 +03001067 pm_runtime_get(dwc->dev);
1068
Felipe Balbi72246da2011-08-19 18:10:58 +03001069 req->request.actual = 0;
1070 req->request.status = -EINPROGRESS;
1071 req->direction = dep->direction;
1072 req->epnum = dep->number;
1073
Felipe Balbife84f522015-09-01 09:01:38 -05001074 trace_dwc3_ep_queue(req);
1075
Felipe Balbi72246da2011-08-19 18:10:58 +03001076 /*
1077 * We only add to our list of requests now and
1078 * start consuming the list once we get XferNotReady
1079 * IRQ.
1080 *
1081 * That way, we avoid doing anything that we don't need
1082 * to do now and defer it until the point we receive a
1083 * particular token from the Host side.
1084 *
1085 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001086 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001087 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001088 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1089 dep->direction);
1090 if (ret)
1091 return ret;
1092
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001093 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001094
1095 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001096 * If there are no pending requests and the endpoint isn't already
1097 * busy, we will just start the request straight away.
1098 *
1099 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1100 * little bit faster.
1101 */
1102 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001103 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001104 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001105 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001106 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001107 }
1108
1109 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001110 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001111 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001112 * 1. XferNotReady with empty list of requests. We need to kick the
1113 * transfer here in that situation, otherwise we will be NAKing
1114 * forever. If we get XferNotReady before gadget driver has a
1115 * chance to queue a request, we will ACK the IRQ but won't be
1116 * able to receive the data until the next request is queued.
1117 * The following code is handling exactly that.
1118 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001119 */
1120 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301121 /*
1122 * If xfernotready is already elapsed and it is a case
1123 * of isoc transfer, then issue END TRANSFER, so that
1124 * you can receive xfernotready again and can have
1125 * notion of current microframe.
1126 */
1127 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001128 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001129 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301130 dep->flags = DWC3_EP_ENABLED;
1131 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301132 return 0;
1133 }
1134
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001135 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001136 if (!ret)
1137 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1138
Felipe Balbia8f32812015-09-16 10:40:07 -05001139 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001140 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001141
Felipe Balbib511e5e2012-06-06 12:00:50 +03001142 /*
1143 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1144 * kick the transfer here after queuing a request, otherwise the
1145 * core may not see the modified TRB(s).
1146 */
1147 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301148 (dep->flags & DWC3_EP_BUSY) &&
1149 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001150 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001151 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001152 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001153 }
1154
Felipe Balbib997ada2012-07-26 13:26:50 +03001155 /*
1156 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1157 * right away, otherwise host will not know we have streams to be
1158 * handled.
1159 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001160 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001161 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001162
Felipe Balbia8f32812015-09-16 10:40:07 -05001163out:
1164 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001165 dwc3_trace(trace_dwc3_gadget,
1166 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001167 dep->name);
1168 if (ret == -EBUSY)
1169 ret = 0;
1170
1171 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001172}
1173
Felipe Balbi04c03d12015-12-02 10:06:45 -06001174static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1175 struct usb_request *request)
1176{
1177 dwc3_gadget_ep_free_request(ep, request);
1178}
1179
1180static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1181{
1182 struct dwc3_request *req;
1183 struct usb_request *request;
1184 struct usb_ep *ep = &dep->endpoint;
1185
1186 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1187 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1188 if (!request)
1189 return -ENOMEM;
1190
1191 request->length = 0;
1192 request->buf = dwc->zlp_buf;
1193 request->complete = __dwc3_gadget_ep_zlp_complete;
1194
1195 req = to_dwc3_request(request);
1196
1197 return __dwc3_gadget_ep_queue(dep, req);
1198}
1199
Felipe Balbi72246da2011-08-19 18:10:58 +03001200static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1201 gfp_t gfp_flags)
1202{
1203 struct dwc3_request *req = to_dwc3_request(request);
1204 struct dwc3_ep *dep = to_dwc3_ep(ep);
1205 struct dwc3 *dwc = dep->dwc;
1206
1207 unsigned long flags;
1208
1209 int ret;
1210
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001211 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001212 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001213
1214 /*
1215 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1216 * setting request->zero, instead of doing magic, we will just queue an
1217 * extra usb_request ourselves so that it gets handled the same way as
1218 * any other request.
1219 */
John Yound92618982015-12-22 12:23:20 -08001220 if (ret == 0 && request->zero && request->length &&
1221 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001222 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1223
Felipe Balbi72246da2011-08-19 18:10:58 +03001224 spin_unlock_irqrestore(&dwc->lock, flags);
1225
1226 return ret;
1227}
1228
1229static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1230 struct usb_request *request)
1231{
1232 struct dwc3_request *req = to_dwc3_request(request);
1233 struct dwc3_request *r = NULL;
1234
1235 struct dwc3_ep *dep = to_dwc3_ep(ep);
1236 struct dwc3 *dwc = dep->dwc;
1237
1238 unsigned long flags;
1239 int ret = 0;
1240
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001241 trace_dwc3_ep_dequeue(req);
1242
Felipe Balbi72246da2011-08-19 18:10:58 +03001243 spin_lock_irqsave(&dwc->lock, flags);
1244
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001245 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001246 if (r == req)
1247 break;
1248 }
1249
1250 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001251 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 if (r == req)
1253 break;
1254 }
1255 if (r == req) {
1256 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001257 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301258 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001259 }
1260 dev_err(dwc->dev, "request %p was not queued to %s\n",
1261 request, ep->name);
1262 ret = -EINVAL;
1263 goto out0;
1264 }
1265
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301266out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001267 /* giveback the request */
1268 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1269
1270out0:
1271 spin_unlock_irqrestore(&dwc->lock, flags);
1272
1273 return ret;
1274}
1275
Felipe Balbi7a608552014-09-24 14:19:52 -05001276int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001277{
1278 struct dwc3_gadget_ep_cmd_params params;
1279 struct dwc3 *dwc = dep->dwc;
1280 int ret;
1281
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001282 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1283 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1284 return -EINVAL;
1285 }
1286
Felipe Balbi72246da2011-08-19 18:10:58 +03001287 memset(&params, 0x00, sizeof(params));
1288
1289 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001290 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001291 (!list_empty(&dep->started_list) ||
1292 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001293 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001294 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001295 dep->name);
1296 return -EAGAIN;
1297 }
1298
Felipe Balbi2cd47182016-04-12 16:42:43 +03001299 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1300 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001301 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001302 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001303 dep->name);
1304 else
1305 dep->flags |= DWC3_EP_STALL;
1306 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001307
John Youn50c763f2016-05-31 17:49:56 -07001308 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001309 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001310 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001311 dep->name);
1312 else
Alan Sterna535d812013-11-01 12:05:12 -04001313 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001315
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 return ret;
1317}
1318
1319static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1320{
1321 struct dwc3_ep *dep = to_dwc3_ep(ep);
1322 struct dwc3 *dwc = dep->dwc;
1323
1324 unsigned long flags;
1325
1326 int ret;
1327
1328 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001329 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001330 spin_unlock_irqrestore(&dwc->lock, flags);
1331
1332 return ret;
1333}
1334
1335static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1336{
1337 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001338 struct dwc3 *dwc = dep->dwc;
1339 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001340 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001341
Paul Zimmerman249a4562012-02-24 17:32:16 -08001342 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001343 dep->flags |= DWC3_EP_WEDGE;
1344
Pratyush Anand08f0d962012-06-25 22:40:43 +05301345 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001346 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301347 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001348 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001349 spin_unlock_irqrestore(&dwc->lock, flags);
1350
1351 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001352}
1353
1354/* -------------------------------------------------------------------------- */
1355
1356static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1357 .bLength = USB_DT_ENDPOINT_SIZE,
1358 .bDescriptorType = USB_DT_ENDPOINT,
1359 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1360};
1361
1362static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1363 .enable = dwc3_gadget_ep0_enable,
1364 .disable = dwc3_gadget_ep0_disable,
1365 .alloc_request = dwc3_gadget_ep_alloc_request,
1366 .free_request = dwc3_gadget_ep_free_request,
1367 .queue = dwc3_gadget_ep0_queue,
1368 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301369 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001370 .set_wedge = dwc3_gadget_ep_set_wedge,
1371};
1372
1373static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1374 .enable = dwc3_gadget_ep_enable,
1375 .disable = dwc3_gadget_ep_disable,
1376 .alloc_request = dwc3_gadget_ep_alloc_request,
1377 .free_request = dwc3_gadget_ep_free_request,
1378 .queue = dwc3_gadget_ep_queue,
1379 .dequeue = dwc3_gadget_ep_dequeue,
1380 .set_halt = dwc3_gadget_ep_set_halt,
1381 .set_wedge = dwc3_gadget_ep_set_wedge,
1382};
1383
1384/* -------------------------------------------------------------------------- */
1385
1386static int dwc3_gadget_get_frame(struct usb_gadget *g)
1387{
1388 struct dwc3 *dwc = gadget_to_dwc(g);
1389 u32 reg;
1390
1391 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1392 return DWC3_DSTS_SOFFN(reg);
1393}
1394
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001395static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001396{
Felipe Balbi72246da2011-08-19 18:10:58 +03001397 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001398
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001399 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001400 u32 reg;
1401
Felipe Balbi72246da2011-08-19 18:10:58 +03001402 u8 link_state;
1403 u8 speed;
1404
Felipe Balbi72246da2011-08-19 18:10:58 +03001405 /*
1406 * According to the Databook Remote wakeup request should
1407 * be issued only when the device is in early suspend state.
1408 *
1409 * We can check that via USB Link State bits in DSTS register.
1410 */
1411 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1412
1413 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001414 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1415 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001416 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001417 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001418 }
1419
1420 link_state = DWC3_DSTS_USBLNKST(reg);
1421
1422 switch (link_state) {
1423 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1424 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1425 break;
1426 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001427 dwc3_trace(trace_dwc3_gadget,
1428 "can't wakeup from '%s'\n",
1429 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001430 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001431 }
1432
Felipe Balbi8598bde2012-01-02 18:55:57 +02001433 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1434 if (ret < 0) {
1435 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001436 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001437 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001438
Paul Zimmerman802fde92012-04-27 13:10:52 +03001439 /* Recent versions do this automatically */
1440 if (dwc->revision < DWC3_REVISION_194A) {
1441 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001442 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001443 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1444 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1445 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001446
Paul Zimmerman1d046792012-02-15 18:56:56 -08001447 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001448 timeout = jiffies + msecs_to_jiffies(100);
1449
Paul Zimmerman1d046792012-02-15 18:56:56 -08001450 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1452
1453 /* in HS, means ON */
1454 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1455 break;
1456 }
1457
1458 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1459 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001460 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 }
1462
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001463 return 0;
1464}
1465
1466static int dwc3_gadget_wakeup(struct usb_gadget *g)
1467{
1468 struct dwc3 *dwc = gadget_to_dwc(g);
1469 unsigned long flags;
1470 int ret;
1471
1472 spin_lock_irqsave(&dwc->lock, flags);
1473 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 spin_unlock_irqrestore(&dwc->lock, flags);
1475
1476 return ret;
1477}
1478
1479static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1480 int is_selfpowered)
1481{
1482 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001483 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001484
Paul Zimmerman249a4562012-02-24 17:32:16 -08001485 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001486 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001487 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001488
1489 return 0;
1490}
1491
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001492static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001493{
1494 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001495 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001496
Felipe Balbifc8bb912016-05-16 13:14:48 +03001497 if (pm_runtime_suspended(dwc->dev))
1498 return 0;
1499
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001501 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001502 if (dwc->revision <= DWC3_REVISION_187A) {
1503 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1504 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1505 }
1506
1507 if (dwc->revision >= DWC3_REVISION_194A)
1508 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1509 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001510
1511 if (dwc->has_hibernation)
1512 reg |= DWC3_DCTL_KEEP_CONNECT;
1513
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001514 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001515 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001517
1518 if (dwc->has_hibernation && !suspend)
1519 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1520
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001521 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001522 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001523
1524 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1525
1526 do {
1527 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1528 if (is_on) {
1529 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1530 break;
1531 } else {
1532 if (reg & DWC3_DSTS_DEVCTRLHLT)
1533 break;
1534 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 timeout--;
1536 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301537 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001538 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001539 } while (1);
1540
Felipe Balbi73815282015-01-27 13:48:14 -06001541 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001542 dwc->gadget_driver
1543 ? dwc->gadget_driver->function : "no-function",
1544 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301545
1546 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001547}
1548
1549static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1550{
1551 struct dwc3 *dwc = gadget_to_dwc(g);
1552 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301553 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001554
1555 is_on = !!is_on;
1556
1557 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001558 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001559 spin_unlock_irqrestore(&dwc->lock, flags);
1560
Pratyush Anand6f17f742012-07-02 10:21:55 +05301561 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001562}
1563
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001564static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1565{
1566 u32 reg;
1567
1568 /* Enable all but Start and End of Frame IRQs */
1569 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1570 DWC3_DEVTEN_EVNTOVERFLOWEN |
1571 DWC3_DEVTEN_CMDCMPLTEN |
1572 DWC3_DEVTEN_ERRTICERREN |
1573 DWC3_DEVTEN_WKUPEVTEN |
1574 DWC3_DEVTEN_ULSTCNGEN |
1575 DWC3_DEVTEN_CONNECTDONEEN |
1576 DWC3_DEVTEN_USBRSTEN |
1577 DWC3_DEVTEN_DISCONNEVTEN);
1578
1579 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1580}
1581
1582static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1583{
1584 /* mask all interrupts */
1585 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1586}
1587
1588static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001589static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001590
Felipe Balbi4e994722016-05-13 14:09:59 +03001591/**
1592 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1593 * dwc: pointer to our context structure
1594 *
1595 * The following looks like complex but it's actually very simple. In order to
1596 * calculate the number of packets we can burst at once on OUT transfers, we're
1597 * gonna use RxFIFO size.
1598 *
1599 * To calculate RxFIFO size we need two numbers:
1600 * MDWIDTH = size, in bits, of the internal memory bus
1601 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1602 *
1603 * Given these two numbers, the formula is simple:
1604 *
1605 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1606 *
1607 * 24 bytes is for 3x SETUP packets
1608 * 16 bytes is a clock domain crossing tolerance
1609 *
1610 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1611 */
1612static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1613{
1614 u32 ram2_depth;
1615 u32 mdwidth;
1616 u32 nump;
1617 u32 reg;
1618
1619 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1620 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1621
1622 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1623 nump = min_t(u32, nump, 16);
1624
1625 /* update NumP */
1626 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1627 reg &= ~DWC3_DCFG_NUMP_MASK;
1628 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1629 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1630}
1631
Felipe Balbid7be2952016-05-04 15:49:37 +03001632static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001633{
Felipe Balbi72246da2011-08-19 18:10:58 +03001634 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001635 int ret = 0;
1636 u32 reg;
1637
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1639 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001640
1641 /**
1642 * WORKAROUND: DWC3 revision < 2.20a have an issue
1643 * which would cause metastability state on Run/Stop
1644 * bit if we try to force the IP to USB2-only mode.
1645 *
1646 * Because of that, we cannot configure the IP to any
1647 * speed other than the SuperSpeed
1648 *
1649 * Refers to:
1650 *
1651 * STAR#9000525659: Clock Domain Crossing on DCTL in
1652 * USB 2.0 Mode
1653 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001654 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001655 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001656 } else {
1657 switch (dwc->maximum_speed) {
1658 case USB_SPEED_LOW:
1659 reg |= DWC3_DSTS_LOWSPEED;
1660 break;
1661 case USB_SPEED_FULL:
1662 reg |= DWC3_DSTS_FULLSPEED1;
1663 break;
1664 case USB_SPEED_HIGH:
1665 reg |= DWC3_DSTS_HIGHSPEED;
1666 break;
John Youn75808622016-02-05 17:09:13 -08001667 case USB_SPEED_SUPER_PLUS:
1668 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1669 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001670 default:
John Youn77966eb2016-02-19 17:31:01 -08001671 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1672 dwc->maximum_speed);
1673 /* fall through */
1674 case USB_SPEED_SUPER:
1675 reg |= DWC3_DCFG_SUPERSPEED;
1676 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001677 }
1678 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001679 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1680
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001681 /*
1682 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1683 * field instead of letting dwc3 itself calculate that automatically.
1684 *
1685 * This way, we maximize the chances that we'll be able to get several
1686 * bursts of data without going through any sort of endpoint throttling.
1687 */
1688 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1689 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1690 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1691
Felipe Balbi4e994722016-05-13 14:09:59 +03001692 dwc3_gadget_setup_nump(dwc);
1693
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 /* Start with SuperSpeed Default */
1695 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1696
1697 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001698 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1699 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001700 if (ret) {
1701 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001702 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001703 }
1704
1705 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001706 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1707 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001708 if (ret) {
1709 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001710 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001711 }
1712
1713 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001714 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 dwc3_ep0_out_start(dwc);
1716
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001717 dwc3_gadget_enable_irq(dwc);
1718
Felipe Balbid7be2952016-05-04 15:49:37 +03001719 return 0;
1720
1721err1:
1722 __dwc3_gadget_ep_disable(dwc->eps[0]);
1723
1724err0:
1725 return ret;
1726}
1727
1728static int dwc3_gadget_start(struct usb_gadget *g,
1729 struct usb_gadget_driver *driver)
1730{
1731 struct dwc3 *dwc = gadget_to_dwc(g);
1732 unsigned long flags;
1733 int ret = 0;
1734 int irq;
1735
1736 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1737 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1738 IRQF_SHARED, "dwc3", dwc->ev_buf);
1739 if (ret) {
1740 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1741 irq, ret);
1742 goto err0;
1743 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001744 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001745
1746 spin_lock_irqsave(&dwc->lock, flags);
1747 if (dwc->gadget_driver) {
1748 dev_err(dwc->dev, "%s is already bound to %s\n",
1749 dwc->gadget.name,
1750 dwc->gadget_driver->driver.name);
1751 ret = -EBUSY;
1752 goto err1;
1753 }
1754
1755 dwc->gadget_driver = driver;
1756
Felipe Balbifc8bb912016-05-16 13:14:48 +03001757 if (pm_runtime_active(dwc->dev))
1758 __dwc3_gadget_start(dwc);
1759
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 spin_unlock_irqrestore(&dwc->lock, flags);
1761
1762 return 0;
1763
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001764err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001766 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001767
1768err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001769 return ret;
1770}
1771
Felipe Balbid7be2952016-05-04 15:49:37 +03001772static void __dwc3_gadget_stop(struct dwc3 *dwc)
1773{
1774 dwc3_gadget_disable_irq(dwc);
1775 __dwc3_gadget_ep_disable(dwc->eps[0]);
1776 __dwc3_gadget_ep_disable(dwc->eps[1]);
1777}
1778
Felipe Balbi22835b82014-10-17 12:05:12 -05001779static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001780{
1781 struct dwc3 *dwc = gadget_to_dwc(g);
1782 unsigned long flags;
1783
1784 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001785 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001787 spin_unlock_irqrestore(&dwc->lock, flags);
1788
Felipe Balbi3f308d12016-05-16 14:17:06 +03001789 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001790
Felipe Balbi72246da2011-08-19 18:10:58 +03001791 return 0;
1792}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001793
Felipe Balbi72246da2011-08-19 18:10:58 +03001794static const struct usb_gadget_ops dwc3_gadget_ops = {
1795 .get_frame = dwc3_gadget_get_frame,
1796 .wakeup = dwc3_gadget_wakeup,
1797 .set_selfpowered = dwc3_gadget_set_selfpowered,
1798 .pullup = dwc3_gadget_pullup,
1799 .udc_start = dwc3_gadget_start,
1800 .udc_stop = dwc3_gadget_stop,
1801};
1802
1803/* -------------------------------------------------------------------------- */
1804
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001805static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1806 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001807{
1808 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001809 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001810
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001811 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07001812 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001813
Felipe Balbi72246da2011-08-19 18:10:58 +03001814 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001815 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001816 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001817
1818 dep->dwc = dwc;
1819 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001820 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001821 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001822 dwc->eps[epnum] = dep;
1823
1824 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1825 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001826
Felipe Balbi72246da2011-08-19 18:10:58 +03001827 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03001828 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001829
Felipe Balbi73815282015-01-27 13:48:14 -06001830 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001831
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001833 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301834 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1836 if (!epnum)
1837 dwc->gadget.ep0 = &dep->endpoint;
1838 } else {
1839 int ret;
1840
Robert Baldygae117e742013-12-13 12:23:38 +01001841 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001842 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001843 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1844 list_add_tail(&dep->endpoint.ep_list,
1845 &dwc->gadget.ep_list);
1846
1847 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001848 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001849 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001850 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001851
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001852 if (epnum == 0 || epnum == 1) {
1853 dep->endpoint.caps.type_control = true;
1854 } else {
1855 dep->endpoint.caps.type_iso = true;
1856 dep->endpoint.caps.type_bulk = true;
1857 dep->endpoint.caps.type_int = true;
1858 }
1859
1860 dep->endpoint.caps.dir_in = !!direction;
1861 dep->endpoint.caps.dir_out = !direction;
1862
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001863 INIT_LIST_HEAD(&dep->pending_list);
1864 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001865 }
1866
1867 return 0;
1868}
1869
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001870static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1871{
1872 int ret;
1873
1874 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1875
1876 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1877 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001878 dwc3_trace(trace_dwc3_gadget,
1879 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001880 return ret;
1881 }
1882
1883 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1884 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001885 dwc3_trace(trace_dwc3_gadget,
1886 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001887 return ret;
1888 }
1889
1890 return 0;
1891}
1892
Felipe Balbi72246da2011-08-19 18:10:58 +03001893static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1894{
1895 struct dwc3_ep *dep;
1896 u8 epnum;
1897
1898 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1899 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001900 if (!dep)
1901 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301902 /*
1903 * Physical endpoints 0 and 1 are special; they form the
1904 * bi-directional USB endpoint 0.
1905 *
1906 * For those two physical endpoints, we don't allocate a TRB
1907 * pool nor do we add them the endpoints list. Due to that, we
1908 * shouldn't do these two operations otherwise we would end up
1909 * with all sorts of bugs when removing dwc3.ko.
1910 */
1911 if (epnum != 0 && epnum != 1) {
1912 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001913 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301914 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001915
1916 kfree(dep);
1917 }
1918}
1919
Felipe Balbi72246da2011-08-19 18:10:58 +03001920/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001921
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301922static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1923 struct dwc3_request *req, struct dwc3_trb *trb,
1924 const struct dwc3_event_depevt *event, int status)
1925{
1926 unsigned int count;
1927 unsigned int s_pkt = 0;
1928 unsigned int trb_status;
1929
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001930 trace_dwc3_complete_trb(dep, trb);
1931
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301932 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1933 /*
1934 * We continue despite the error. There is not much we
1935 * can do. If we don't clean it up we loop forever. If
1936 * we skip the TRB then it gets overwritten after a
1937 * while since we use them in a ring buffer. A BUG()
1938 * would help. Lets hope that if this occurs, someone
1939 * fixes the root cause instead of looking away :)
1940 */
1941 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1942 dep->name, trb);
1943 count = trb->size & DWC3_TRB_SIZE_MASK;
1944
1945 if (dep->direction) {
1946 if (count) {
1947 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1948 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001949 dwc3_trace(trace_dwc3_gadget,
1950 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301951 dep->name);
1952 /*
1953 * If missed isoc occurred and there is
1954 * no request queued then issue END
1955 * TRANSFER, so that core generates
1956 * next xfernotready and we will issue
1957 * a fresh START TRANSFER.
1958 * If there are still queued request
1959 * then wait, do not issue either END
1960 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001961 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301962 * giveback.If any future queued request
1963 * is successfully transferred then we
1964 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001965 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301966 */
1967 dep->flags |= DWC3_EP_MISSED_ISOC;
1968 } else {
1969 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1970 dep->name);
1971 status = -ECONNRESET;
1972 }
1973 } else {
1974 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1975 }
1976 } else {
1977 if (count && (event->status & DEPEVT_STATUS_SHORT))
1978 s_pkt = 1;
1979 }
1980
1981 /*
1982 * We assume here we will always receive the entire data block
1983 * which we should receive. Meaning, if we program RX to
1984 * receive 4K but we receive only 2K, we assume that's all we
1985 * should receive and we simply bounce the request back to the
1986 * gadget driver for further processing.
1987 */
1988 req->request.actual += req->request.length - count;
1989 if (s_pkt)
1990 return 1;
1991 if ((event->status & DEPEVT_STATUS_LST) &&
1992 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1993 DWC3_TRB_CTRL_HWO)))
1994 return 1;
1995 if ((event->status & DEPEVT_STATUS_IOC) &&
1996 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1997 return 1;
1998 return 0;
1999}
2000
Felipe Balbi72246da2011-08-19 18:10:58 +03002001static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2002 const struct dwc3_event_depevt *event, int status)
2003{
2004 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002005 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302006 unsigned int slot;
2007 unsigned int i;
2008 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002009
2010 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002011 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002012 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002013 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002014
Ville Syrjäläd115d702015-08-31 19:48:28 +03002015 i = 0;
2016 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002017 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002018 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002019 slot++;
2020 slot %= DWC3_TRB_NUM;
2021 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002022
Ville Syrjäläd115d702015-08-31 19:48:28 +03002023 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2024 event, status);
2025 if (ret)
2026 break;
2027 } while (++i < req->request.num_mapped_sgs);
2028
2029 dwc3_gadget_giveback(dep, req, status);
2030
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302031 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002032 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002033 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002034
Felipe Balbi4cb42212016-05-18 12:37:21 +03002035 /*
2036 * Our endpoint might get disabled by another thread during
2037 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2038 * early on so DWC3_EP_BUSY flag gets cleared
2039 */
2040 if (!dep->endpoint.desc)
2041 return 1;
2042
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302043 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002044 list_empty(&dep->started_list)) {
2045 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302046 /*
2047 * If there is no entry in request list then do
2048 * not issue END TRANSFER now. Just set PENDING
2049 * flag, so that END TRANSFER is issued when an
2050 * entry is added into request list.
2051 */
2052 dep->flags = DWC3_EP_PENDING_REQUEST;
2053 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002054 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302055 dep->flags = DWC3_EP_ENABLED;
2056 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302057 return 1;
2058 }
2059
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002060 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2061 if ((event->status & DEPEVT_STATUS_IOC) &&
2062 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2063 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002064 return 1;
2065}
2066
2067static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002068 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002069{
2070 unsigned status = 0;
2071 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002072 u32 is_xfer_complete;
2073
2074 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002075
2076 if (event->status & DEPEVT_STATUS_BUSERR)
2077 status = -ECONNRESET;
2078
Paul Zimmerman1d046792012-02-15 18:56:56 -08002079 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002080 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002081 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002083
2084 /*
2085 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2086 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2087 */
2088 if (dwc->revision < DWC3_REVISION_183A) {
2089 u32 reg;
2090 int i;
2091
2092 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002093 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002094
2095 if (!(dep->flags & DWC3_EP_ENABLED))
2096 continue;
2097
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002098 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002099 return;
2100 }
2101
2102 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2103 reg |= dwc->u1u2;
2104 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2105
2106 dwc->u1u2 = 0;
2107 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002108
Felipe Balbi4cb42212016-05-18 12:37:21 +03002109 /*
2110 * Our endpoint might get disabled by another thread during
2111 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2112 * early on so DWC3_EP_BUSY flag gets cleared
2113 */
2114 if (!dep->endpoint.desc)
2115 return;
2116
Felipe Balbie6e709b2015-09-28 15:16:56 -05002117 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002118 int ret;
2119
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002120 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002121 if (!ret || ret == -EBUSY)
2122 return;
2123 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002124}
2125
Felipe Balbi72246da2011-08-19 18:10:58 +03002126static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2127 const struct dwc3_event_depevt *event)
2128{
2129 struct dwc3_ep *dep;
2130 u8 epnum = event->endpoint_number;
2131
2132 dep = dwc->eps[epnum];
2133
Felipe Balbi3336abb2012-06-06 09:19:35 +03002134 if (!(dep->flags & DWC3_EP_ENABLED))
2135 return;
2136
Felipe Balbi72246da2011-08-19 18:10:58 +03002137 if (epnum == 0 || epnum == 1) {
2138 dwc3_ep0_interrupt(dwc, event);
2139 return;
2140 }
2141
2142 switch (event->endpoint_event) {
2143 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002144 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002145
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002146 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002147 dwc3_trace(trace_dwc3_gadget,
2148 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002149 dep->name);
2150 return;
2151 }
2152
Jingoo Han029d97f2014-07-04 15:00:51 +09002153 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002154 break;
2155 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002156 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 break;
2158 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002159 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 dwc3_gadget_start_isoc(dwc, dep, event);
2161 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002162 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002163 int ret;
2164
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002165 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2166
Felipe Balbi73815282015-01-27 13:48:14 -06002167 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002168 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002169 : "Transfer Not Active");
2170
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002171 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002172 if (!ret || ret == -EBUSY)
2173 return;
2174
Felipe Balbiec5e7952015-11-16 16:04:13 -06002175 dwc3_trace(trace_dwc3_gadget,
2176 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002177 dep->name);
2178 }
2179
2180 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002181 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002182 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002183 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2184 dep->name);
2185 return;
2186 }
2187
2188 switch (event->status) {
2189 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002190 dwc3_trace(trace_dwc3_gadget,
2191 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002192 event->parameters);
2193
2194 break;
2195 case DEPEVT_STREAMEVT_NOTFOUND:
2196 /* FALLTHROUGH */
2197 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002198 dwc3_trace(trace_dwc3_gadget,
2199 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002200 }
2201 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002202 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002203 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002204 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002205 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002206 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002207 break;
2208 }
2209}
2210
2211static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2212{
2213 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2214 spin_unlock(&dwc->lock);
2215 dwc->gadget_driver->disconnect(&dwc->gadget);
2216 spin_lock(&dwc->lock);
2217 }
2218}
2219
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002220static void dwc3_suspend_gadget(struct dwc3 *dwc)
2221{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002222 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002223 spin_unlock(&dwc->lock);
2224 dwc->gadget_driver->suspend(&dwc->gadget);
2225 spin_lock(&dwc->lock);
2226 }
2227}
2228
2229static void dwc3_resume_gadget(struct dwc3 *dwc)
2230{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002231 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002232 spin_unlock(&dwc->lock);
2233 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002234 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002235 }
2236}
2237
2238static void dwc3_reset_gadget(struct dwc3 *dwc)
2239{
2240 if (!dwc->gadget_driver)
2241 return;
2242
2243 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2244 spin_unlock(&dwc->lock);
2245 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002246 spin_lock(&dwc->lock);
2247 }
2248}
2249
Paul Zimmermanb992e682012-04-27 14:17:35 +03002250static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002251{
2252 struct dwc3_ep *dep;
2253 struct dwc3_gadget_ep_cmd_params params;
2254 u32 cmd;
2255 int ret;
2256
2257 dep = dwc->eps[epnum];
2258
Felipe Balbib4996a82012-06-06 12:04:13 +03002259 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302260 return;
2261
Pratyush Anand57911502012-07-06 15:19:10 +05302262 /*
2263 * NOTICE: We are violating what the Databook says about the
2264 * EndTransfer command. Ideally we would _always_ wait for the
2265 * EndTransfer Command Completion IRQ, but that's causing too
2266 * much trouble synchronizing between us and gadget driver.
2267 *
2268 * We have discussed this with the IP Provider and it was
2269 * suggested to giveback all requests here, but give HW some
2270 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002271 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302272 *
2273 * Note also that a similar handling was tested by Synopsys
2274 * (thanks a lot Paul) and nothing bad has come out of it.
2275 * In short, what we're doing is:
2276 *
2277 * - Issue EndTransfer WITH CMDIOC bit set
2278 * - Wait 100us
2279 */
2280
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302281 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002282 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2283 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002284 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302285 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002286 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302287 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002288 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002289 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302290 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002291}
2292
2293static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2294{
2295 u32 epnum;
2296
2297 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2298 struct dwc3_ep *dep;
2299
2300 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002301 if (!dep)
2302 continue;
2303
Felipe Balbi72246da2011-08-19 18:10:58 +03002304 if (!(dep->flags & DWC3_EP_ENABLED))
2305 continue;
2306
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002307 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002308 }
2309}
2310
2311static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2312{
2313 u32 epnum;
2314
2315 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2316 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002317 int ret;
2318
2319 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002320 if (!dep)
2321 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002322
2323 if (!(dep->flags & DWC3_EP_STALL))
2324 continue;
2325
2326 dep->flags &= ~DWC3_EP_STALL;
2327
John Youn50c763f2016-05-31 17:49:56 -07002328 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002329 WARN_ON_ONCE(ret);
2330 }
2331}
2332
2333static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2334{
Felipe Balbic4430a22012-05-24 10:30:01 +03002335 int reg;
2336
Felipe Balbi72246da2011-08-19 18:10:58 +03002337 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2338 reg &= ~DWC3_DCTL_INITU1ENA;
2339 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2340
2341 reg &= ~DWC3_DCTL_INITU2ENA;
2342 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002343
Felipe Balbi72246da2011-08-19 18:10:58 +03002344 dwc3_disconnect_gadget(dwc);
2345
2346 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002347 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002348 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002349
2350 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002351}
2352
Felipe Balbi72246da2011-08-19 18:10:58 +03002353static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2354{
2355 u32 reg;
2356
Felipe Balbifc8bb912016-05-16 13:14:48 +03002357 dwc->connected = true;
2358
Felipe Balbidf62df52011-10-14 15:11:49 +03002359 /*
2360 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2361 * would cause a missing Disconnect Event if there's a
2362 * pending Setup Packet in the FIFO.
2363 *
2364 * There's no suggested workaround on the official Bug
2365 * report, which states that "unless the driver/application
2366 * is doing any special handling of a disconnect event,
2367 * there is no functional issue".
2368 *
2369 * Unfortunately, it turns out that we _do_ some special
2370 * handling of a disconnect event, namely complete all
2371 * pending transfers, notify gadget driver of the
2372 * disconnection, and so on.
2373 *
2374 * Our suggested workaround is to follow the Disconnect
2375 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002376 * flag. Such flag gets set whenever we have a SETUP_PENDING
2377 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002378 * same endpoint.
2379 *
2380 * Refers to:
2381 *
2382 * STAR#9000466709: RTL: Device : Disconnect event not
2383 * generated if setup packet pending in FIFO
2384 */
2385 if (dwc->revision < DWC3_REVISION_188A) {
2386 if (dwc->setup_packet_pending)
2387 dwc3_gadget_disconnect_interrupt(dwc);
2388 }
2389
Felipe Balbi8e744752014-11-06 14:27:53 +08002390 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002391
2392 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2393 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2394 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002395 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002396
2397 dwc3_stop_active_transfers(dwc);
2398 dwc3_clear_stall_all_ep(dwc);
2399
2400 /* Reset device address to zero */
2401 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2402 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2403 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002404}
2405
2406static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2407{
2408 u32 reg;
2409 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2410
2411 /*
2412 * We change the clock only at SS but I dunno why I would want to do
2413 * this. Maybe it becomes part of the power saving plan.
2414 */
2415
John Younee5cd412016-02-05 17:08:45 -08002416 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2417 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002418 return;
2419
2420 /*
2421 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2422 * each time on Connect Done.
2423 */
2424 if (!usb30_clock)
2425 return;
2426
2427 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2428 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2429 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2430}
2431
Felipe Balbi72246da2011-08-19 18:10:58 +03002432static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2433{
Felipe Balbi72246da2011-08-19 18:10:58 +03002434 struct dwc3_ep *dep;
2435 int ret;
2436 u32 reg;
2437 u8 speed;
2438
Felipe Balbi72246da2011-08-19 18:10:58 +03002439 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2440 speed = reg & DWC3_DSTS_CONNECTSPD;
2441 dwc->speed = speed;
2442
2443 dwc3_update_ram_clk_sel(dwc, speed);
2444
2445 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002446 case DWC3_DCFG_SUPERSPEED_PLUS:
2447 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2448 dwc->gadget.ep0->maxpacket = 512;
2449 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2450 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002451 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002452 /*
2453 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2454 * would cause a missing USB3 Reset event.
2455 *
2456 * In such situations, we should force a USB3 Reset
2457 * event by calling our dwc3_gadget_reset_interrupt()
2458 * routine.
2459 *
2460 * Refers to:
2461 *
2462 * STAR#9000483510: RTL: SS : USB3 reset event may
2463 * not be generated always when the link enters poll
2464 */
2465 if (dwc->revision < DWC3_REVISION_190A)
2466 dwc3_gadget_reset_interrupt(dwc);
2467
Felipe Balbi72246da2011-08-19 18:10:58 +03002468 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2469 dwc->gadget.ep0->maxpacket = 512;
2470 dwc->gadget.speed = USB_SPEED_SUPER;
2471 break;
2472 case DWC3_DCFG_HIGHSPEED:
2473 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2474 dwc->gadget.ep0->maxpacket = 64;
2475 dwc->gadget.speed = USB_SPEED_HIGH;
2476 break;
2477 case DWC3_DCFG_FULLSPEED2:
2478 case DWC3_DCFG_FULLSPEED1:
2479 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2480 dwc->gadget.ep0->maxpacket = 64;
2481 dwc->gadget.speed = USB_SPEED_FULL;
2482 break;
2483 case DWC3_DCFG_LOWSPEED:
2484 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2485 dwc->gadget.ep0->maxpacket = 8;
2486 dwc->gadget.speed = USB_SPEED_LOW;
2487 break;
2488 }
2489
Pratyush Anand2b758352013-01-14 15:59:31 +05302490 /* Enable USB2 LPM Capability */
2491
John Younee5cd412016-02-05 17:08:45 -08002492 if ((dwc->revision > DWC3_REVISION_194A) &&
2493 (speed != DWC3_DCFG_SUPERSPEED) &&
2494 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302495 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2496 reg |= DWC3_DCFG_LPM_CAP;
2497 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2498
2499 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2500 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2501
Huang Rui460d0982014-10-31 11:11:18 +08002502 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302503
Huang Rui80caf7d2014-10-28 19:54:26 +08002504 /*
2505 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2506 * DCFG.LPMCap is set, core responses with an ACK and the
2507 * BESL value in the LPM token is less than or equal to LPM
2508 * NYET threshold.
2509 */
2510 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2511 && dwc->has_lpm_erratum,
2512 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2513
2514 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2515 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2516
Pratyush Anand2b758352013-01-14 15:59:31 +05302517 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002518 } else {
2519 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2520 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2521 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302522 }
2523
Felipe Balbi72246da2011-08-19 18:10:58 +03002524 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002525 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2526 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002527 if (ret) {
2528 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2529 return;
2530 }
2531
2532 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002533 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2534 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002535 if (ret) {
2536 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2537 return;
2538 }
2539
2540 /*
2541 * Configure PHY via GUSB3PIPECTLn if required.
2542 *
2543 * Update GTXFIFOSIZn
2544 *
2545 * In both cases reset values should be sufficient.
2546 */
2547}
2548
2549static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2550{
Felipe Balbi72246da2011-08-19 18:10:58 +03002551 /*
2552 * TODO take core out of low power mode when that's
2553 * implemented.
2554 */
2555
Jiebing Liad14d4e2014-12-11 13:26:29 +08002556 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2557 spin_unlock(&dwc->lock);
2558 dwc->gadget_driver->resume(&dwc->gadget);
2559 spin_lock(&dwc->lock);
2560 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002561}
2562
2563static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2564 unsigned int evtinfo)
2565{
Felipe Balbifae2b902011-10-14 13:00:30 +03002566 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002567 unsigned int pwropt;
2568
2569 /*
2570 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2571 * Hibernation mode enabled which would show up when device detects
2572 * host-initiated U3 exit.
2573 *
2574 * In that case, device will generate a Link State Change Interrupt
2575 * from U3 to RESUME which is only necessary if Hibernation is
2576 * configured in.
2577 *
2578 * There are no functional changes due to such spurious event and we
2579 * just need to ignore it.
2580 *
2581 * Refers to:
2582 *
2583 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2584 * operational mode
2585 */
2586 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2587 if ((dwc->revision < DWC3_REVISION_250A) &&
2588 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2589 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2590 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002591 dwc3_trace(trace_dwc3_gadget,
2592 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002593 return;
2594 }
2595 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002596
2597 /*
2598 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2599 * on the link partner, the USB session might do multiple entry/exit
2600 * of low power states before a transfer takes place.
2601 *
2602 * Due to this problem, we might experience lower throughput. The
2603 * suggested workaround is to disable DCTL[12:9] bits if we're
2604 * transitioning from U1/U2 to U0 and enable those bits again
2605 * after a transfer completes and there are no pending transfers
2606 * on any of the enabled endpoints.
2607 *
2608 * This is the first half of that workaround.
2609 *
2610 * Refers to:
2611 *
2612 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2613 * core send LGO_Ux entering U0
2614 */
2615 if (dwc->revision < DWC3_REVISION_183A) {
2616 if (next == DWC3_LINK_STATE_U0) {
2617 u32 u1u2;
2618 u32 reg;
2619
2620 switch (dwc->link_state) {
2621 case DWC3_LINK_STATE_U1:
2622 case DWC3_LINK_STATE_U2:
2623 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2624 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2625 | DWC3_DCTL_ACCEPTU2ENA
2626 | DWC3_DCTL_INITU1ENA
2627 | DWC3_DCTL_ACCEPTU1ENA);
2628
2629 if (!dwc->u1u2)
2630 dwc->u1u2 = reg & u1u2;
2631
2632 reg &= ~u1u2;
2633
2634 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2635 break;
2636 default:
2637 /* do nothing */
2638 break;
2639 }
2640 }
2641 }
2642
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002643 switch (next) {
2644 case DWC3_LINK_STATE_U1:
2645 if (dwc->speed == USB_SPEED_SUPER)
2646 dwc3_suspend_gadget(dwc);
2647 break;
2648 case DWC3_LINK_STATE_U2:
2649 case DWC3_LINK_STATE_U3:
2650 dwc3_suspend_gadget(dwc);
2651 break;
2652 case DWC3_LINK_STATE_RESUME:
2653 dwc3_resume_gadget(dwc);
2654 break;
2655 default:
2656 /* do nothing */
2657 break;
2658 }
2659
Felipe Balbie57ebc12014-04-22 13:20:12 -05002660 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002661}
2662
Felipe Balbie1dadd32014-02-25 14:47:54 -06002663static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2664 unsigned int evtinfo)
2665{
2666 unsigned int is_ss = evtinfo & BIT(4);
2667
2668 /**
2669 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2670 * have a known issue which can cause USB CV TD.9.23 to fail
2671 * randomly.
2672 *
2673 * Because of this issue, core could generate bogus hibernation
2674 * events which SW needs to ignore.
2675 *
2676 * Refers to:
2677 *
2678 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2679 * Device Fallback from SuperSpeed
2680 */
2681 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2682 return;
2683
2684 /* enter hibernation here */
2685}
2686
Felipe Balbi72246da2011-08-19 18:10:58 +03002687static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2688 const struct dwc3_event_devt *event)
2689{
2690 switch (event->type) {
2691 case DWC3_DEVICE_EVENT_DISCONNECT:
2692 dwc3_gadget_disconnect_interrupt(dwc);
2693 break;
2694 case DWC3_DEVICE_EVENT_RESET:
2695 dwc3_gadget_reset_interrupt(dwc);
2696 break;
2697 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2698 dwc3_gadget_conndone_interrupt(dwc);
2699 break;
2700 case DWC3_DEVICE_EVENT_WAKEUP:
2701 dwc3_gadget_wakeup_interrupt(dwc);
2702 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002703 case DWC3_DEVICE_EVENT_HIBER_REQ:
2704 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2705 "unexpected hibernation event\n"))
2706 break;
2707
2708 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2709 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2711 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2712 break;
2713 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002714 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002715 break;
2716 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002717 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002718 break;
2719 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002720 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002721 break;
2722 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002723 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002724 break;
2725 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002726 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002727 break;
2728 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002729 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 }
2731}
2732
2733static void dwc3_process_event_entry(struct dwc3 *dwc,
2734 const union dwc3_event *event)
2735{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002736 trace_dwc3_event(event->raw);
2737
Felipe Balbi72246da2011-08-19 18:10:58 +03002738 /* Endpoint IRQ, handle it and return early */
2739 if (event->type.is_devspec == 0) {
2740 /* depevt */
2741 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2742 }
2743
2744 switch (event->type.type) {
2745 case DWC3_EVENT_TYPE_DEV:
2746 dwc3_gadget_interrupt(dwc, &event->devt);
2747 break;
2748 /* REVISIT what to do with Carkit and I2C events ? */
2749 default:
2750 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2751 }
2752}
2753
Felipe Balbidea520a2016-03-30 09:39:34 +03002754static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002755{
Felipe Balbidea520a2016-03-30 09:39:34 +03002756 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002757 irqreturn_t ret = IRQ_NONE;
2758 int left;
2759 u32 reg;
2760
Felipe Balbif42f2442013-06-12 21:25:08 +03002761 left = evt->count;
2762
2763 if (!(evt->flags & DWC3_EVENT_PENDING))
2764 return IRQ_NONE;
2765
2766 while (left > 0) {
2767 union dwc3_event event;
2768
2769 event.raw = *(u32 *) (evt->buf + evt->lpos);
2770
2771 dwc3_process_event_entry(dwc, &event);
2772
2773 /*
2774 * FIXME we wrap around correctly to the next entry as
2775 * almost all entries are 4 bytes in size. There is one
2776 * entry which has 12 bytes which is a regular entry
2777 * followed by 8 bytes data. ATM I don't know how
2778 * things are organized if we get next to the a
2779 * boundary so I worry about that once we try to handle
2780 * that.
2781 */
2782 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2783 left -= 4;
2784
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002785 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002786 }
2787
2788 evt->count = 0;
2789 evt->flags &= ~DWC3_EVENT_PENDING;
2790 ret = IRQ_HANDLED;
2791
2792 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002793 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002794 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002795 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002796
2797 return ret;
2798}
2799
Felipe Balbidea520a2016-03-30 09:39:34 +03002800static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002801{
Felipe Balbidea520a2016-03-30 09:39:34 +03002802 struct dwc3_event_buffer *evt = _evt;
2803 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002804 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002805 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002806
Felipe Balbie5f68b42015-10-12 13:25:44 -05002807 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002808 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002809 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002810
2811 return ret;
2812}
2813
Felipe Balbidea520a2016-03-30 09:39:34 +03002814static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002815{
Felipe Balbidea520a2016-03-30 09:39:34 +03002816 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002817 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002818 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002819
Felipe Balbifc8bb912016-05-16 13:14:48 +03002820 if (pm_runtime_suspended(dwc->dev)) {
2821 pm_runtime_get(dwc->dev);
2822 disable_irq_nosync(dwc->irq_gadget);
2823 dwc->pending_events = true;
2824 return IRQ_HANDLED;
2825 }
2826
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002827 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002828 count &= DWC3_GEVNTCOUNT_MASK;
2829 if (!count)
2830 return IRQ_NONE;
2831
Felipe Balbib15a7622011-06-30 16:57:15 +03002832 evt->count = count;
2833 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002834
Felipe Balbie8adfc32013-06-12 21:11:14 +03002835 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002836 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002837 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002838 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002839
Felipe Balbib15a7622011-06-30 16:57:15 +03002840 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002841}
2842
Felipe Balbidea520a2016-03-30 09:39:34 +03002843static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002844{
Felipe Balbidea520a2016-03-30 09:39:34 +03002845 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002846
Felipe Balbidea520a2016-03-30 09:39:34 +03002847 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002848}
2849
2850/**
2851 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002852 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002853 *
2854 * Returns 0 on success otherwise negative errno.
2855 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002856int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002857{
Felipe Balbi72246da2011-08-19 18:10:58 +03002858 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002859
2860 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2861 &dwc->ctrl_req_addr, GFP_KERNEL);
2862 if (!dwc->ctrl_req) {
2863 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2864 ret = -ENOMEM;
2865 goto err0;
2866 }
2867
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302868 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002869 &dwc->ep0_trb_addr, GFP_KERNEL);
2870 if (!dwc->ep0_trb) {
2871 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2872 ret = -ENOMEM;
2873 goto err1;
2874 }
2875
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002876 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002877 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 ret = -ENOMEM;
2879 goto err2;
2880 }
2881
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002882 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002883 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2884 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002885 if (!dwc->ep0_bounce) {
2886 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2887 ret = -ENOMEM;
2888 goto err3;
2889 }
2890
Felipe Balbi04c03d12015-12-02 10:06:45 -06002891 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2892 if (!dwc->zlp_buf) {
2893 ret = -ENOMEM;
2894 goto err4;
2895 }
2896
Felipe Balbi72246da2011-08-19 18:10:58 +03002897 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002898 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002899 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002900 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002901 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002902
2903 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002904 * FIXME We might be setting max_speed to <SUPER, however versions
2905 * <2.20a of dwc3 have an issue with metastability (documented
2906 * elsewhere in this driver) which tells us we can't set max speed to
2907 * anything lower than SUPER.
2908 *
2909 * Because gadget.max_speed is only used by composite.c and function
2910 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2911 * to happen so we avoid sending SuperSpeed Capability descriptor
2912 * together with our BOS descriptor as that could confuse host into
2913 * thinking we can handle super speed.
2914 *
2915 * Note that, in fact, we won't even support GetBOS requests when speed
2916 * is less than super speed because we don't have means, yet, to tell
2917 * composite.c that we are USB 2.0 + LPM ECN.
2918 */
2919 if (dwc->revision < DWC3_REVISION_220A)
2920 dwc3_trace(trace_dwc3_gadget,
2921 "Changing max_speed on rev %08x\n",
2922 dwc->revision);
2923
2924 dwc->gadget.max_speed = dwc->maximum_speed;
2925
2926 /*
David Cohena4b9d942013-12-09 15:55:38 -08002927 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2928 * on ep out.
2929 */
2930 dwc->gadget.quirk_ep_out_aligned_size = true;
2931
2932 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002933 * REVISIT: Here we should clear all pending IRQs to be
2934 * sure we're starting from a well known location.
2935 */
2936
2937 ret = dwc3_gadget_init_endpoints(dwc);
2938 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002939 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002940
Felipe Balbi72246da2011-08-19 18:10:58 +03002941 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2942 if (ret) {
2943 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002944 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002945 }
2946
2947 return 0;
2948
Felipe Balbi04c03d12015-12-02 10:06:45 -06002949err5:
2950 kfree(dwc->zlp_buf);
2951
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002952err4:
David Cohene1f80462013-09-11 17:42:47 -07002953 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002954 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2955 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002956
Felipe Balbi72246da2011-08-19 18:10:58 +03002957err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002958 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002959
2960err2:
2961 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2962 dwc->ep0_trb, dwc->ep0_trb_addr);
2963
2964err1:
2965 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2966 dwc->ctrl_req, dwc->ctrl_req_addr);
2967
2968err0:
2969 return ret;
2970}
2971
Felipe Balbi7415f172012-04-30 14:56:33 +03002972/* -------------------------------------------------------------------------- */
2973
Felipe Balbi72246da2011-08-19 18:10:58 +03002974void dwc3_gadget_exit(struct dwc3 *dwc)
2975{
Felipe Balbi72246da2011-08-19 18:10:58 +03002976 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002977
Felipe Balbi72246da2011-08-19 18:10:58 +03002978 dwc3_gadget_free_endpoints(dwc);
2979
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002980 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2981 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002982
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002983 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002984 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002985
2986 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2987 dwc->ep0_trb, dwc->ep0_trb_addr);
2988
2989 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2990 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002991}
Felipe Balbi7415f172012-04-30 14:56:33 +03002992
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002993int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002994{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002995 int ret;
2996
Roger Quadros9772b472016-04-12 11:33:29 +03002997 if (!dwc->gadget_driver)
2998 return 0;
2999
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003000 ret = dwc3_gadget_run_stop(dwc, false, false);
3001 if (ret < 0)
3002 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003003
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003004 dwc3_disconnect_gadget(dwc);
3005 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003006
3007 return 0;
3008}
3009
3010int dwc3_gadget_resume(struct dwc3 *dwc)
3011{
Felipe Balbi7415f172012-04-30 14:56:33 +03003012 int ret;
3013
Roger Quadros9772b472016-04-12 11:33:29 +03003014 if (!dwc->gadget_driver)
3015 return 0;
3016
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003017 ret = __dwc3_gadget_start(dwc);
3018 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003019 goto err0;
3020
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003021 ret = dwc3_gadget_run_stop(dwc, true, false);
3022 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003023 goto err1;
3024
Felipe Balbi7415f172012-04-30 14:56:33 +03003025 return 0;
3026
3027err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003028 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003029
3030err0:
3031 return ret;
3032}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003033
3034void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3035{
3036 if (dwc->pending_events) {
3037 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3038 dwc->pending_events = false;
3039 enable_irq(dwc->irq_gadget);
3040 }
3041}