blob: 609acd7b1520e4dd36e66229b815c013101158be [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
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200149{
Felipe Balbief966b92016-04-05 13:09:51 +0300150 dep->trb_enqueue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300151 dep->trb_enqueue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300152}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200153
Felipe Balbief966b92016-04-05 13:09:51 +0300154static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
155{
156 dep->trb_dequeue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300157 dep->trb_dequeue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static int dwc3_ep_is_last_trb(unsigned int index)
161{
Felipe Balbi4faf7552016-04-05 13:14:31 +0300162 return index == DWC3_TRB_NUM - 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200163}
164
Felipe Balbi72246da2011-08-19 18:10:58 +0300165void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
166 int status)
167{
168 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530169 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200171 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530172 i = 0;
173 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300174 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530175 /*
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
179 */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300180 if (dwc3_ep_is_last_trb(dep->trb_dequeue))
Felipe Balbief966b92016-04-05 13:09:51 +0300181 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530182 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200183 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 }
185 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200186 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Pratyush Anand0416e492012-08-10 13:42:16 +0530191 if (dwc->ep0_bounced && dep->number == 0)
192 dwc->ep0_bounced = false;
193 else
194 usb_gadget_unmap_request(&dwc->gadget, &req->request,
195 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300202
203 if (dep->number > 1)
204 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300205}
206
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500207int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300208{
209 u32 timeout = 500;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300210 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300211 u32 reg;
212
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500213 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500214
Felipe Balbib09bb642012-04-24 16:19:11 +0300215 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 Balbi73815282015-01-27 13:48:14 -0600221 dwc3_trace(trace_dwc3_gadget,
222 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300223 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530224 if (DWC3_DGCMD_STATUS(reg))
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300225 ret = -EINVAL;
226 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300227 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300228 } while (timeout--);
229
230 if (!timeout) {
231 dwc3_trace(trace_dwc3_gadget,
232 "Command Timed Out");
233 ret = -ETIMEDOUT;
234 }
235
236 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300237}
238
Felipe Balbic36d8e92016-04-04 12:46:33 +0300239static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
240
Felipe Balbi2cd47182016-04-12 16:42:43 +0300241int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
242 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300243{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300244 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200245 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 u32 reg;
247
Felipe Balbi0933df12016-05-23 14:02:33 +0300248 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300249 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300250 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300251
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300252 /*
253 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
254 * we're issuing an endpoint command, we must check if
255 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
256 *
257 * We will also set SUSPHY bit to what it was before returning as stated
258 * by the same section on Synopsys databook.
259 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300260 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
261 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
262 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
263 susphy = true;
264 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
265 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
266 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300267 }
268
Felipe Balbic36d8e92016-04-04 12:46:33 +0300269 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
270 int needs_wakeup;
271
272 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
273 dwc->link_state == DWC3_LINK_STATE_U2 ||
274 dwc->link_state == DWC3_LINK_STATE_U3);
275
276 if (unlikely(needs_wakeup)) {
277 ret = __dwc3_gadget_wakeup(dwc);
278 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
279 ret);
280 }
281 }
282
Felipe Balbi2eb88012016-04-12 16:53:39 +0300283 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
284 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
285 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300286
Felipe Balbi2eb88012016-04-12 16:53:39 +0300287 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300288 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300289 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300291 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000292
Felipe Balbi73815282015-01-27 13:48:14 -0600293 dwc3_trace(trace_dwc3_gadget,
294 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000295 cmd_status);
296
297 switch (cmd_status) {
298 case 0:
299 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300300 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000301 case DEPEVT_TRANSFER_NO_RESOURCE:
Felipe Balbiba159842016-05-23 13:50:29 +0300302 dwc3_trace(trace_dwc3_gadget, "no resource available");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000303 ret = -EINVAL;
304 break;
305 case DEPEVT_TRANSFER_BUS_EXPIRY:
306 /*
307 * SW issues START TRANSFER command to
308 * isochronous ep with future frame interval. If
309 * future interval time has already passed when
310 * core receives the command, it will respond
311 * with an error status of 'Bus Expiry'.
312 *
313 * Instead of always returning -EINVAL, let's
314 * give a hint to the gadget driver that this is
315 * the case by returning -EAGAIN.
316 */
Felipe Balbiba159842016-05-23 13:50:29 +0300317 dwc3_trace(trace_dwc3_gadget, "bus expiry");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000318 ret = -EAGAIN;
319 break;
320 default:
321 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
322 }
323
Felipe Balbic0ca3242016-04-04 09:11:51 +0300324 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300325 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300326 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
Felipe Balbif6bb2252016-05-23 13:53:34 +0300328 if (timeout == 0) {
329 dwc3_trace(trace_dwc3_gadget,
330 "Command Timed Out");
331 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300332 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300333 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334
Felipe Balbi0933df12016-05-23 14:02:33 +0300335 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
336
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300337 if (unlikely(susphy)) {
338 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
339 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
340 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
341 }
342
Felipe Balbic0ca3242016-04-04 09:11:51 +0300343 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300344}
345
John Youn50c763f2016-05-31 17:49:56 -0700346static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
347{
348 struct dwc3 *dwc = dep->dwc;
349 struct dwc3_gadget_ep_cmd_params params;
350 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
351
352 /*
353 * As of core revision 2.60a the recommended programming model
354 * is to set the ClearPendIN bit when issuing a Clear Stall EP
355 * command for IN endpoints. This is to prevent an issue where
356 * some (non-compliant) hosts may not send ACK TPs for pending
357 * IN transfers due to a mishandled error condition. Synopsys
358 * STAR 9000614252.
359 */
360 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
361 cmd |= DWC3_DEPCMD_CLEARPENDIN;
362
363 memset(&params, 0, sizeof(params));
364
Felipe Balbi2cd47182016-04-12 16:42:43 +0300365 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700366}
367
Felipe Balbi72246da2011-08-19 18:10:58 +0300368static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200369 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300370{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300371 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300372
373 return dep->trb_pool_dma + offset;
374}
375
376static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
377{
378 struct dwc3 *dwc = dep->dwc;
379
380 if (dep->trb_pool)
381 return 0;
382
Felipe Balbi72246da2011-08-19 18:10:58 +0300383 dep->trb_pool = dma_alloc_coherent(dwc->dev,
384 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
385 &dep->trb_pool_dma, GFP_KERNEL);
386 if (!dep->trb_pool) {
387 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
388 dep->name);
389 return -ENOMEM;
390 }
391
392 return 0;
393}
394
395static void dwc3_free_trb_pool(struct dwc3_ep *dep)
396{
397 struct dwc3 *dwc = dep->dwc;
398
399 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
400 dep->trb_pool, dep->trb_pool_dma);
401
402 dep->trb_pool = NULL;
403 dep->trb_pool_dma = 0;
404}
405
John Younc4509602016-02-16 20:10:53 -0800406static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
407
408/**
409 * dwc3_gadget_start_config - Configure EP resources
410 * @dwc: pointer to our controller context structure
411 * @dep: endpoint that is being enabled
412 *
413 * The assignment of transfer resources cannot perfectly follow the
414 * data book due to the fact that the controller driver does not have
415 * all knowledge of the configuration in advance. It is given this
416 * information piecemeal by the composite gadget framework after every
417 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
418 * programming model in this scenario can cause errors. For two
419 * reasons:
420 *
421 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
422 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
423 * multiple interfaces.
424 *
425 * 2) The databook does not mention doing more DEPXFERCFG for new
426 * endpoint on alt setting (8.1.6).
427 *
428 * The following simplified method is used instead:
429 *
430 * All hardware endpoints can be assigned a transfer resource and this
431 * setting will stay persistent until either a core reset or
432 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
433 * do DEPXFERCFG for every hardware endpoint as well. We are
434 * guaranteed that there are as many transfer resources as endpoints.
435 *
436 * This function is called for each endpoint when it is being enabled
437 * but is triggered only when called for EP0-out, which always happens
438 * first, and which should only happen in one of the above conditions.
439 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300440static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
441{
442 struct dwc3_gadget_ep_cmd_params params;
443 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800444 int i;
445 int ret;
446
447 if (dep->number)
448 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449
450 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800451 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300452
Felipe Balbi2cd47182016-04-12 16:42:43 +0300453 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800454 if (ret)
455 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
John Younc4509602016-02-16 20:10:53 -0800457 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
458 struct dwc3_ep *dep = dwc->eps[i];
459
460 if (!dep)
461 continue;
462
463 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
464 if (ret)
465 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 }
467
468 return 0;
469}
470
471static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200472 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300473 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600474 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300475{
476 struct dwc3_gadget_ep_cmd_params params;
477
478 memset(&params, 0x00, sizeof(params));
479
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300480 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900481 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
482
483 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800484 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300485 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300486 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900487 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300488
Felipe Balbi4b345c92012-07-16 14:08:16 +0300489 if (ignore)
490 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
491
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600492 if (restore) {
493 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
494 params.param2 |= dep->saved_state;
495 }
496
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300497 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
498 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200500 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300501 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
502 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300503 dep->stream_capable = true;
504 }
505
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500506 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300507 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300508
509 /*
510 * We are doing 1:1 mapping for endpoints, meaning
511 * Physical Endpoints 2 maps to Logical Endpoint 2 and
512 * so on. We consider the direction bit as part of the physical
513 * endpoint number. So USB endpoint 0x81 is 0x03.
514 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300515 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300516
517 /*
518 * We must use the lower 16 TX FIFOs even though
519 * HW might have more
520 */
521 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300522 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300525 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300526 dep->interval = 1 << (desc->bInterval - 1);
527 }
528
Felipe Balbi2cd47182016-04-12 16:42:43 +0300529 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300530}
531
532static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
533{
534 struct dwc3_gadget_ep_cmd_params params;
535
536 memset(&params, 0x00, sizeof(params));
537
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300538 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300539
Felipe Balbi2cd47182016-04-12 16:42:43 +0300540 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
541 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300542}
543
544/**
545 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
546 * @dep: endpoint to be initialized
547 * @desc: USB Endpoint Descriptor
548 *
549 * Caller should take care of locking
550 */
551static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200552 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300553 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600554 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300555{
556 struct dwc3 *dwc = dep->dwc;
557 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300558 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
Felipe Balbi73815282015-01-27 13:48:14 -0600560 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300561
Felipe Balbi72246da2011-08-19 18:10:58 +0300562 if (!(dep->flags & DWC3_EP_ENABLED)) {
563 ret = dwc3_gadget_start_config(dwc, dep);
564 if (ret)
565 return ret;
566 }
567
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600568 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
569 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 if (ret)
571 return ret;
572
573 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200574 struct dwc3_trb *trb_st_hw;
575 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300576
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200577 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200578 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579 dep->type = usb_endpoint_type(desc);
580 dep->flags |= DWC3_EP_ENABLED;
581
582 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
583 reg |= DWC3_DALEPENA_EP(dep->number);
584 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
585
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300586 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300587 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300589 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 trb_st_hw = &dep->trb_pool[0];
591
Felipe Balbif6bafc62012-02-06 11:04:53 +0200592 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700593 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300594
Felipe Balbif6bafc62012-02-06 11:04:53 +0200595 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
596 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
597 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
598 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 }
600
601 return 0;
602}
603
Paul Zimmermanb992e682012-04-27 14:17:35 +0300604static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200605static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300606{
607 struct dwc3_request *req;
608
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200609 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300610 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200611
Pratyush Anand57911502012-07-06 15:19:10 +0530612 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200613 while (!list_empty(&dep->started_list)) {
614 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530615
616 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
617 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200618 }
619
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200620 while (!list_empty(&dep->pending_list)) {
621 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300622
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200623 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300625}
626
627/**
628 * __dwc3_gadget_ep_disable - Disables a HW endpoint
629 * @dep: the endpoint to disable
630 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200631 * This function also removes requests which are currently processed ny the
632 * hardware and those which are not yet scheduled.
633 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300635static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
636{
637 struct dwc3 *dwc = dep->dwc;
638 u32 reg;
639
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500640 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
641
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200642 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300643
Felipe Balbi687ef982014-04-16 10:30:33 -0500644 /* make sure HW endpoint isn't stalled */
645 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500646 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500647
Felipe Balbi72246da2011-08-19 18:10:58 +0300648 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
649 reg &= ~DWC3_DALEPENA_EP(dep->number);
650 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
651
Felipe Balbi879631a2011-09-30 10:58:47 +0300652 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200653 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200654 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300655 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300656 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657
658 return 0;
659}
660
661/* -------------------------------------------------------------------------- */
662
663static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
664 const struct usb_endpoint_descriptor *desc)
665{
666 return -EINVAL;
667}
668
669static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
670{
671 return -EINVAL;
672}
673
674/* -------------------------------------------------------------------------- */
675
676static int dwc3_gadget_ep_enable(struct usb_ep *ep,
677 const struct usb_endpoint_descriptor *desc)
678{
679 struct dwc3_ep *dep;
680 struct dwc3 *dwc;
681 unsigned long flags;
682 int ret;
683
684 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
685 pr_debug("dwc3: invalid parameters\n");
686 return -EINVAL;
687 }
688
689 if (!desc->wMaxPacketSize) {
690 pr_debug("dwc3: missing wMaxPacketSize\n");
691 return -EINVAL;
692 }
693
694 dep = to_dwc3_ep(ep);
695 dwc = dep->dwc;
696
Felipe Balbi95ca9612015-12-10 13:08:20 -0600697 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
698 "%s is already enabled\n",
699 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300700 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300701
Felipe Balbi72246da2011-08-19 18:10:58 +0300702 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600703 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 spin_unlock_irqrestore(&dwc->lock, flags);
705
706 return ret;
707}
708
709static int dwc3_gadget_ep_disable(struct usb_ep *ep)
710{
711 struct dwc3_ep *dep;
712 struct dwc3 *dwc;
713 unsigned long flags;
714 int ret;
715
716 if (!ep) {
717 pr_debug("dwc3: invalid parameters\n");
718 return -EINVAL;
719 }
720
721 dep = to_dwc3_ep(ep);
722 dwc = dep->dwc;
723
Felipe Balbi95ca9612015-12-10 13:08:20 -0600724 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
725 "%s is already disabled\n",
726 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300728
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 spin_lock_irqsave(&dwc->lock, flags);
730 ret = __dwc3_gadget_ep_disable(dep);
731 spin_unlock_irqrestore(&dwc->lock, flags);
732
733 return ret;
734}
735
736static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
737 gfp_t gfp_flags)
738{
739 struct dwc3_request *req;
740 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300741
742 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900743 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300744 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300745
746 req->epnum = dep->number;
747 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300748
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500749 trace_dwc3_alloc_request(req);
750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 return &req->request;
752}
753
754static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
755 struct usb_request *request)
756{
757 struct dwc3_request *req = to_dwc3_request(request);
758
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500759 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 kfree(req);
761}
762
Felipe Balbic71fc372011-11-22 11:37:34 +0200763/**
764 * dwc3_prepare_one_trb - setup one TRB from one request
765 * @dep: endpoint for which this request is prepared
766 * @req: dwc3_request pointer
767 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200768static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200769 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530770 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200771{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200772 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200773
Felipe Balbi73815282015-01-27 13:48:14 -0600774 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200775 dep->name, req, (unsigned long long) dma,
776 length, last ? " last" : "",
777 chain ? " chain" : "");
778
Pratyush Anand915e2022013-01-14 15:59:35 +0530779
Felipe Balbi4faf7552016-04-05 13:14:31 +0300780 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200781
Felipe Balbieeb720f2011-11-28 12:46:59 +0200782 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200783 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200784 req->trb = trb;
785 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300786 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200787 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200788
Felipe Balbief966b92016-04-05 13:09:51 +0300789 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300790 /* Skip the LINK-TRB */
791 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
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;
848
849 /*
850 * If enqueue & dequeue are equal than it is either full or empty.
851 *
852 * One way to know for sure is if the TRB right before us has HWO bit
853 * set or not. If it has, then we're definitely full and can't fit any
854 * more transfers in our ring.
855 */
856 if (dep->trb_enqueue == dep->trb_dequeue) {
857 /* If we're full, enqueue/dequeue are > 0 */
858 if (dep->trb_enqueue) {
859 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
860 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
861 return 0;
862 }
863
864 return DWC3_TRB_NUM - 1;
865 }
866
867 return dep->trb_dequeue - dep->trb_enqueue;
868}
869
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300870static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
871 struct dwc3_request *req, unsigned int trbs_left)
872{
873 struct usb_request *request = &req->request;
874 struct scatterlist *sg = request->sg;
875 struct scatterlist *s;
876 unsigned int last = false;
877 unsigned int length;
878 dma_addr_t dma;
879 int i;
880
881 for_each_sg(sg, s, request->num_mapped_sgs, i) {
882 unsigned chain = true;
883
884 length = sg_dma_len(s);
885 dma = sg_dma_address(s);
886
887 if (sg_is_last(s)) {
888 if (list_is_last(&req->list, &dep->pending_list))
889 last = true;
890
891 chain = false;
892 }
893
894 if (!trbs_left)
895 last = true;
896
897 if (last)
898 chain = false;
899
900 dwc3_prepare_one_trb(dep, req, dma, length,
901 last, chain, i);
902
903 if (last)
904 break;
905 }
906}
907
908static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
909 struct dwc3_request *req, unsigned int trbs_left)
910{
911 unsigned int last = false;
912 unsigned int length;
913 dma_addr_t dma;
914
915 dma = req->request.dma;
916 length = req->request.length;
917
918 if (!trbs_left)
919 last = true;
920
921 /* Is this the last request? */
922 if (list_is_last(&req->list, &dep->pending_list))
923 last = true;
924
925 dwc3_prepare_one_trb(dep, req, dma, length,
926 last, false, 0);
927}
928
Felipe Balbi72246da2011-08-19 18:10:58 +0300929/*
930 * dwc3_prepare_trbs - setup TRBs from requests
931 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300932 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800933 * The function goes through the requests list and sets up TRBs for the
934 * transfers. The function returns once there are no more TRBs available or
935 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300936 */
Felipe Balbic4233572016-05-12 14:08:34 +0300937static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300938{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200939 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300940 u32 trbs_left;
941
942 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
943
Felipe Balbic4233572016-05-12 14:08:34 +0300944 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300945
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200946 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300947 if (req->request.num_mapped_sgs > 0)
948 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
949 else
950 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300951
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300952 if (!trbs_left)
953 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300954 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300955}
956
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300957static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300958{
959 struct dwc3_gadget_ep_cmd_params params;
960 struct dwc3_request *req;
961 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300962 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300963 int ret;
964 u32 cmd;
965
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300966 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300967
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300968 dwc3_prepare_trbs(dep);
969 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 if (!req) {
971 dep->flags |= DWC3_EP_PENDING_REQUEST;
972 return 0;
973 }
974
975 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300977 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530978 params.param0 = upper_32_bits(req->trb_dma);
979 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530981 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300982 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530983 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300984
985 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +0300986 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300988 /*
989 * FIXME we need to iterate over the list of requests
990 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800991 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200993 usb_gadget_unmap_request(&dwc->gadget, &req->request,
994 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 list_del(&req->list);
996 return ret;
997 }
998
999 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001000
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001001 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001002 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001003 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001004 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001005
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 return 0;
1007}
1008
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301009static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1010 struct dwc3_ep *dep, u32 cur_uf)
1011{
1012 u32 uf;
1013
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001014 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001015 dwc3_trace(trace_dwc3_gadget,
1016 "ISOC ep %s run out for requests",
1017 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301018 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301019 return;
1020 }
1021
1022 /* 4 micro frames in the future */
1023 uf = cur_uf + dep->interval * 4;
1024
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001025 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301026}
1027
1028static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1029 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1030{
1031 u32 cur_uf, mask;
1032
1033 mask = ~(dep->interval - 1);
1034 cur_uf = event->parameters & mask;
1035
1036 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1037}
1038
Felipe Balbi72246da2011-08-19 18:10:58 +03001039static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1040{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001041 struct dwc3 *dwc = dep->dwc;
1042 int ret;
1043
Felipe Balbibb423982015-11-16 15:31:21 -06001044 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001045 dwc3_trace(trace_dwc3_gadget,
1046 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001047 &req->request, dep->endpoint.name);
1048 return -ESHUTDOWN;
1049 }
1050
1051 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1052 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001053 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1054 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001055 return -EINVAL;
1056 }
1057
Felipe Balbifc8bb912016-05-16 13:14:48 +03001058 pm_runtime_get(dwc->dev);
1059
Felipe Balbi72246da2011-08-19 18:10:58 +03001060 req->request.actual = 0;
1061 req->request.status = -EINPROGRESS;
1062 req->direction = dep->direction;
1063 req->epnum = dep->number;
1064
Felipe Balbife84f522015-09-01 09:01:38 -05001065 trace_dwc3_ep_queue(req);
1066
Felipe Balbi72246da2011-08-19 18:10:58 +03001067 /*
1068 * We only add to our list of requests now and
1069 * start consuming the list once we get XferNotReady
1070 * IRQ.
1071 *
1072 * That way, we avoid doing anything that we don't need
1073 * to do now and defer it until the point we receive a
1074 * particular token from the Host side.
1075 *
1076 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001077 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001078 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001079 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1080 dep->direction);
1081 if (ret)
1082 return ret;
1083
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001084 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001085
1086 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001087 * If there are no pending requests and the endpoint isn't already
1088 * busy, we will just start the request straight away.
1089 *
1090 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1091 * little bit faster.
1092 */
1093 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001094 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001095 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001096 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001097 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001098 }
1099
1100 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001101 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001102 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001103 * 1. XferNotReady with empty list of requests. We need to kick the
1104 * transfer here in that situation, otherwise we will be NAKing
1105 * forever. If we get XferNotReady before gadget driver has a
1106 * chance to queue a request, we will ACK the IRQ but won't be
1107 * able to receive the data until the next request is queued.
1108 * The following code is handling exactly that.
1109 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 */
1111 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301112 /*
1113 * If xfernotready is already elapsed and it is a case
1114 * of isoc transfer, then issue END TRANSFER, so that
1115 * you can receive xfernotready again and can have
1116 * notion of current microframe.
1117 */
1118 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001119 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001120 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301121 dep->flags = DWC3_EP_ENABLED;
1122 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301123 return 0;
1124 }
1125
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001126 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001127 if (!ret)
1128 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1129
Felipe Balbia8f32812015-09-16 10:40:07 -05001130 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001131 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001132
Felipe Balbib511e5e2012-06-06 12:00:50 +03001133 /*
1134 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1135 * kick the transfer here after queuing a request, otherwise the
1136 * core may not see the modified TRB(s).
1137 */
1138 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301139 (dep->flags & DWC3_EP_BUSY) &&
1140 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001141 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001142 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001143 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001144 }
1145
Felipe Balbib997ada2012-07-26 13:26:50 +03001146 /*
1147 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1148 * right away, otherwise host will not know we have streams to be
1149 * handled.
1150 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001151 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001152 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001153
Felipe Balbia8f32812015-09-16 10:40:07 -05001154out:
1155 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001156 dwc3_trace(trace_dwc3_gadget,
1157 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001158 dep->name);
1159 if (ret == -EBUSY)
1160 ret = 0;
1161
1162 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001163}
1164
Felipe Balbi04c03d12015-12-02 10:06:45 -06001165static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1166 struct usb_request *request)
1167{
1168 dwc3_gadget_ep_free_request(ep, request);
1169}
1170
1171static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1172{
1173 struct dwc3_request *req;
1174 struct usb_request *request;
1175 struct usb_ep *ep = &dep->endpoint;
1176
1177 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1178 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1179 if (!request)
1180 return -ENOMEM;
1181
1182 request->length = 0;
1183 request->buf = dwc->zlp_buf;
1184 request->complete = __dwc3_gadget_ep_zlp_complete;
1185
1186 req = to_dwc3_request(request);
1187
1188 return __dwc3_gadget_ep_queue(dep, req);
1189}
1190
Felipe Balbi72246da2011-08-19 18:10:58 +03001191static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1192 gfp_t gfp_flags)
1193{
1194 struct dwc3_request *req = to_dwc3_request(request);
1195 struct dwc3_ep *dep = to_dwc3_ep(ep);
1196 struct dwc3 *dwc = dep->dwc;
1197
1198 unsigned long flags;
1199
1200 int ret;
1201
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001202 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001203 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001204
1205 /*
1206 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1207 * setting request->zero, instead of doing magic, we will just queue an
1208 * extra usb_request ourselves so that it gets handled the same way as
1209 * any other request.
1210 */
John Yound92618982015-12-22 12:23:20 -08001211 if (ret == 0 && request->zero && request->length &&
1212 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001213 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1214
Felipe Balbi72246da2011-08-19 18:10:58 +03001215 spin_unlock_irqrestore(&dwc->lock, flags);
1216
1217 return ret;
1218}
1219
1220static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1221 struct usb_request *request)
1222{
1223 struct dwc3_request *req = to_dwc3_request(request);
1224 struct dwc3_request *r = NULL;
1225
1226 struct dwc3_ep *dep = to_dwc3_ep(ep);
1227 struct dwc3 *dwc = dep->dwc;
1228
1229 unsigned long flags;
1230 int ret = 0;
1231
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001232 trace_dwc3_ep_dequeue(req);
1233
Felipe Balbi72246da2011-08-19 18:10:58 +03001234 spin_lock_irqsave(&dwc->lock, flags);
1235
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001236 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001237 if (r == req)
1238 break;
1239 }
1240
1241 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001242 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001243 if (r == req)
1244 break;
1245 }
1246 if (r == req) {
1247 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001248 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301249 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 }
1251 dev_err(dwc->dev, "request %p was not queued to %s\n",
1252 request, ep->name);
1253 ret = -EINVAL;
1254 goto out0;
1255 }
1256
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301257out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001258 /* giveback the request */
1259 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1260
1261out0:
1262 spin_unlock_irqrestore(&dwc->lock, flags);
1263
1264 return ret;
1265}
1266
Felipe Balbi7a608552014-09-24 14:19:52 -05001267int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001268{
1269 struct dwc3_gadget_ep_cmd_params params;
1270 struct dwc3 *dwc = dep->dwc;
1271 int ret;
1272
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001273 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1274 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1275 return -EINVAL;
1276 }
1277
Felipe Balbi72246da2011-08-19 18:10:58 +03001278 memset(&params, 0x00, sizeof(params));
1279
1280 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001281 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001282 (!list_empty(&dep->started_list) ||
1283 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001284 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001285 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001286 dep->name);
1287 return -EAGAIN;
1288 }
1289
Felipe Balbi2cd47182016-04-12 16:42:43 +03001290 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1291 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001292 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001293 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001294 dep->name);
1295 else
1296 dep->flags |= DWC3_EP_STALL;
1297 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001298
John Youn50c763f2016-05-31 17:49:56 -07001299 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001300 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001301 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 dep->name);
1303 else
Alan Sterna535d812013-11-01 12:05:12 -04001304 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001305 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001306
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 return ret;
1308}
1309
1310static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1311{
1312 struct dwc3_ep *dep = to_dwc3_ep(ep);
1313 struct dwc3 *dwc = dep->dwc;
1314
1315 unsigned long flags;
1316
1317 int ret;
1318
1319 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001320 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001321 spin_unlock_irqrestore(&dwc->lock, flags);
1322
1323 return ret;
1324}
1325
1326static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1327{
1328 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001329 struct dwc3 *dwc = dep->dwc;
1330 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001331 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001332
Paul Zimmerman249a4562012-02-24 17:32:16 -08001333 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 dep->flags |= DWC3_EP_WEDGE;
1335
Pratyush Anand08f0d962012-06-25 22:40:43 +05301336 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001337 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301338 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001339 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001340 spin_unlock_irqrestore(&dwc->lock, flags);
1341
1342 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001343}
1344
1345/* -------------------------------------------------------------------------- */
1346
1347static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1348 .bLength = USB_DT_ENDPOINT_SIZE,
1349 .bDescriptorType = USB_DT_ENDPOINT,
1350 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1351};
1352
1353static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1354 .enable = dwc3_gadget_ep0_enable,
1355 .disable = dwc3_gadget_ep0_disable,
1356 .alloc_request = dwc3_gadget_ep_alloc_request,
1357 .free_request = dwc3_gadget_ep_free_request,
1358 .queue = dwc3_gadget_ep0_queue,
1359 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301360 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001361 .set_wedge = dwc3_gadget_ep_set_wedge,
1362};
1363
1364static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1365 .enable = dwc3_gadget_ep_enable,
1366 .disable = dwc3_gadget_ep_disable,
1367 .alloc_request = dwc3_gadget_ep_alloc_request,
1368 .free_request = dwc3_gadget_ep_free_request,
1369 .queue = dwc3_gadget_ep_queue,
1370 .dequeue = dwc3_gadget_ep_dequeue,
1371 .set_halt = dwc3_gadget_ep_set_halt,
1372 .set_wedge = dwc3_gadget_ep_set_wedge,
1373};
1374
1375/* -------------------------------------------------------------------------- */
1376
1377static int dwc3_gadget_get_frame(struct usb_gadget *g)
1378{
1379 struct dwc3 *dwc = gadget_to_dwc(g);
1380 u32 reg;
1381
1382 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1383 return DWC3_DSTS_SOFFN(reg);
1384}
1385
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001386static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001387{
Felipe Balbi72246da2011-08-19 18:10:58 +03001388 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001389
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001390 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001391 u32 reg;
1392
Felipe Balbi72246da2011-08-19 18:10:58 +03001393 u8 link_state;
1394 u8 speed;
1395
Felipe Balbi72246da2011-08-19 18:10:58 +03001396 /*
1397 * According to the Databook Remote wakeup request should
1398 * be issued only when the device is in early suspend state.
1399 *
1400 * We can check that via USB Link State bits in DSTS register.
1401 */
1402 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1403
1404 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001405 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1406 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001407 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001408 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001409 }
1410
1411 link_state = DWC3_DSTS_USBLNKST(reg);
1412
1413 switch (link_state) {
1414 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1415 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1416 break;
1417 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001418 dwc3_trace(trace_dwc3_gadget,
1419 "can't wakeup from '%s'\n",
1420 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001421 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 }
1423
Felipe Balbi8598bde2012-01-02 18:55:57 +02001424 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1425 if (ret < 0) {
1426 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001427 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001428 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001429
Paul Zimmerman802fde92012-04-27 13:10:52 +03001430 /* Recent versions do this automatically */
1431 if (dwc->revision < DWC3_REVISION_194A) {
1432 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001433 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001434 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1435 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1436 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001437
Paul Zimmerman1d046792012-02-15 18:56:56 -08001438 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001439 timeout = jiffies + msecs_to_jiffies(100);
1440
Paul Zimmerman1d046792012-02-15 18:56:56 -08001441 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001442 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1443
1444 /* in HS, means ON */
1445 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1446 break;
1447 }
1448
1449 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1450 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001451 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 }
1453
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001454 return 0;
1455}
1456
1457static int dwc3_gadget_wakeup(struct usb_gadget *g)
1458{
1459 struct dwc3 *dwc = gadget_to_dwc(g);
1460 unsigned long flags;
1461 int ret;
1462
1463 spin_lock_irqsave(&dwc->lock, flags);
1464 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001465 spin_unlock_irqrestore(&dwc->lock, flags);
1466
1467 return ret;
1468}
1469
1470static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1471 int is_selfpowered)
1472{
1473 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001474 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001475
Paul Zimmerman249a4562012-02-24 17:32:16 -08001476 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001477 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001478 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001479
1480 return 0;
1481}
1482
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001483static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001484{
1485 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001486 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001487
Felipe Balbifc8bb912016-05-16 13:14:48 +03001488 if (pm_runtime_suspended(dwc->dev))
1489 return 0;
1490
Felipe Balbi72246da2011-08-19 18:10:58 +03001491 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001492 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001493 if (dwc->revision <= DWC3_REVISION_187A) {
1494 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1495 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1496 }
1497
1498 if (dwc->revision >= DWC3_REVISION_194A)
1499 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1500 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001501
1502 if (dwc->has_hibernation)
1503 reg |= DWC3_DCTL_KEEP_CONNECT;
1504
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001505 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001506 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001507 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001508
1509 if (dwc->has_hibernation && !suspend)
1510 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1511
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001512 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001513 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001514
1515 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1516
1517 do {
1518 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1519 if (is_on) {
1520 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1521 break;
1522 } else {
1523 if (reg & DWC3_DSTS_DEVCTRLHLT)
1524 break;
1525 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 timeout--;
1527 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301528 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001529 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 } while (1);
1531
Felipe Balbi73815282015-01-27 13:48:14 -06001532 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001533 dwc->gadget_driver
1534 ? dwc->gadget_driver->function : "no-function",
1535 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301536
1537 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001538}
1539
1540static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1541{
1542 struct dwc3 *dwc = gadget_to_dwc(g);
1543 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301544 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001545
1546 is_on = !!is_on;
1547
1548 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001549 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001550 spin_unlock_irqrestore(&dwc->lock, flags);
1551
Pratyush Anand6f17f742012-07-02 10:21:55 +05301552 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001553}
1554
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001555static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1556{
1557 u32 reg;
1558
1559 /* Enable all but Start and End of Frame IRQs */
1560 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1561 DWC3_DEVTEN_EVNTOVERFLOWEN |
1562 DWC3_DEVTEN_CMDCMPLTEN |
1563 DWC3_DEVTEN_ERRTICERREN |
1564 DWC3_DEVTEN_WKUPEVTEN |
1565 DWC3_DEVTEN_ULSTCNGEN |
1566 DWC3_DEVTEN_CONNECTDONEEN |
1567 DWC3_DEVTEN_USBRSTEN |
1568 DWC3_DEVTEN_DISCONNEVTEN);
1569
1570 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1571}
1572
1573static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1574{
1575 /* mask all interrupts */
1576 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1577}
1578
1579static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001580static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001581
Felipe Balbi4e994722016-05-13 14:09:59 +03001582/**
1583 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1584 * dwc: pointer to our context structure
1585 *
1586 * The following looks like complex but it's actually very simple. In order to
1587 * calculate the number of packets we can burst at once on OUT transfers, we're
1588 * gonna use RxFIFO size.
1589 *
1590 * To calculate RxFIFO size we need two numbers:
1591 * MDWIDTH = size, in bits, of the internal memory bus
1592 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1593 *
1594 * Given these two numbers, the formula is simple:
1595 *
1596 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1597 *
1598 * 24 bytes is for 3x SETUP packets
1599 * 16 bytes is a clock domain crossing tolerance
1600 *
1601 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1602 */
1603static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1604{
1605 u32 ram2_depth;
1606 u32 mdwidth;
1607 u32 nump;
1608 u32 reg;
1609
1610 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1611 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1612
1613 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1614 nump = min_t(u32, nump, 16);
1615
1616 /* update NumP */
1617 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1618 reg &= ~DWC3_DCFG_NUMP_MASK;
1619 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1620 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1621}
1622
Felipe Balbid7be2952016-05-04 15:49:37 +03001623static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001624{
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001626 int ret = 0;
1627 u32 reg;
1628
Felipe Balbi72246da2011-08-19 18:10:58 +03001629 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1630 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001631
1632 /**
1633 * WORKAROUND: DWC3 revision < 2.20a have an issue
1634 * which would cause metastability state on Run/Stop
1635 * bit if we try to force the IP to USB2-only mode.
1636 *
1637 * Because of that, we cannot configure the IP to any
1638 * speed other than the SuperSpeed
1639 *
1640 * Refers to:
1641 *
1642 * STAR#9000525659: Clock Domain Crossing on DCTL in
1643 * USB 2.0 Mode
1644 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001645 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001646 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001647 } else {
1648 switch (dwc->maximum_speed) {
1649 case USB_SPEED_LOW:
1650 reg |= DWC3_DSTS_LOWSPEED;
1651 break;
1652 case USB_SPEED_FULL:
1653 reg |= DWC3_DSTS_FULLSPEED1;
1654 break;
1655 case USB_SPEED_HIGH:
1656 reg |= DWC3_DSTS_HIGHSPEED;
1657 break;
John Youn75808622016-02-05 17:09:13 -08001658 case USB_SPEED_SUPER_PLUS:
1659 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1660 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001661 default:
John Youn77966eb2016-02-19 17:31:01 -08001662 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1663 dwc->maximum_speed);
1664 /* fall through */
1665 case USB_SPEED_SUPER:
1666 reg |= DWC3_DCFG_SUPERSPEED;
1667 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001668 }
1669 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001670 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1671
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001672 /*
1673 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1674 * field instead of letting dwc3 itself calculate that automatically.
1675 *
1676 * This way, we maximize the chances that we'll be able to get several
1677 * bursts of data without going through any sort of endpoint throttling.
1678 */
1679 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1680 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1681 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1682
Felipe Balbi4e994722016-05-13 14:09:59 +03001683 dwc3_gadget_setup_nump(dwc);
1684
Felipe Balbi72246da2011-08-19 18:10:58 +03001685 /* Start with SuperSpeed Default */
1686 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1687
1688 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001689 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1690 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001691 if (ret) {
1692 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001693 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 }
1695
1696 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001697 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1698 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001699 if (ret) {
1700 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001701 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 }
1703
1704 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001705 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001706 dwc3_ep0_out_start(dwc);
1707
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001708 dwc3_gadget_enable_irq(dwc);
1709
Felipe Balbid7be2952016-05-04 15:49:37 +03001710 return 0;
1711
1712err1:
1713 __dwc3_gadget_ep_disable(dwc->eps[0]);
1714
1715err0:
1716 return ret;
1717}
1718
1719static int dwc3_gadget_start(struct usb_gadget *g,
1720 struct usb_gadget_driver *driver)
1721{
1722 struct dwc3 *dwc = gadget_to_dwc(g);
1723 unsigned long flags;
1724 int ret = 0;
1725 int irq;
1726
1727 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1728 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1729 IRQF_SHARED, "dwc3", dwc->ev_buf);
1730 if (ret) {
1731 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1732 irq, ret);
1733 goto err0;
1734 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001735 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001736
1737 spin_lock_irqsave(&dwc->lock, flags);
1738 if (dwc->gadget_driver) {
1739 dev_err(dwc->dev, "%s is already bound to %s\n",
1740 dwc->gadget.name,
1741 dwc->gadget_driver->driver.name);
1742 ret = -EBUSY;
1743 goto err1;
1744 }
1745
1746 dwc->gadget_driver = driver;
1747
Felipe Balbifc8bb912016-05-16 13:14:48 +03001748 if (pm_runtime_active(dwc->dev))
1749 __dwc3_gadget_start(dwc);
1750
Felipe Balbi72246da2011-08-19 18:10:58 +03001751 spin_unlock_irqrestore(&dwc->lock, flags);
1752
1753 return 0;
1754
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001755err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001756 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001757 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001758
1759err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 return ret;
1761}
1762
Felipe Balbid7be2952016-05-04 15:49:37 +03001763static void __dwc3_gadget_stop(struct dwc3 *dwc)
1764{
1765 dwc3_gadget_disable_irq(dwc);
1766 __dwc3_gadget_ep_disable(dwc->eps[0]);
1767 __dwc3_gadget_ep_disable(dwc->eps[1]);
1768}
1769
Felipe Balbi22835b82014-10-17 12:05:12 -05001770static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001771{
1772 struct dwc3 *dwc = gadget_to_dwc(g);
1773 unsigned long flags;
1774
1775 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001776 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001777 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001778 spin_unlock_irqrestore(&dwc->lock, flags);
1779
Felipe Balbi3f308d12016-05-16 14:17:06 +03001780 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001781
Felipe Balbi72246da2011-08-19 18:10:58 +03001782 return 0;
1783}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001784
Felipe Balbi72246da2011-08-19 18:10:58 +03001785static const struct usb_gadget_ops dwc3_gadget_ops = {
1786 .get_frame = dwc3_gadget_get_frame,
1787 .wakeup = dwc3_gadget_wakeup,
1788 .set_selfpowered = dwc3_gadget_set_selfpowered,
1789 .pullup = dwc3_gadget_pullup,
1790 .udc_start = dwc3_gadget_start,
1791 .udc_stop = dwc3_gadget_stop,
1792};
1793
1794/* -------------------------------------------------------------------------- */
1795
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001796static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1797 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001798{
1799 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001800 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001801
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001802 for (i = 0; i < num; i++) {
1803 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001804
Felipe Balbi72246da2011-08-19 18:10:58 +03001805 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001806 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001808
1809 dep->dwc = dwc;
1810 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001811 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001812 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001813 dwc->eps[epnum] = dep;
1814
1815 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1816 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001817
Felipe Balbi72246da2011-08-19 18:10:58 +03001818 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03001819 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001820
Felipe Balbi73815282015-01-27 13:48:14 -06001821 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001822
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001824 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301825 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1827 if (!epnum)
1828 dwc->gadget.ep0 = &dep->endpoint;
1829 } else {
1830 int ret;
1831
Robert Baldygae117e742013-12-13 12:23:38 +01001832 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001833 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001834 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1835 list_add_tail(&dep->endpoint.ep_list,
1836 &dwc->gadget.ep_list);
1837
1838 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001839 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001840 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001841 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001842
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001843 if (epnum == 0 || epnum == 1) {
1844 dep->endpoint.caps.type_control = true;
1845 } else {
1846 dep->endpoint.caps.type_iso = true;
1847 dep->endpoint.caps.type_bulk = true;
1848 dep->endpoint.caps.type_int = true;
1849 }
1850
1851 dep->endpoint.caps.dir_in = !!direction;
1852 dep->endpoint.caps.dir_out = !direction;
1853
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001854 INIT_LIST_HEAD(&dep->pending_list);
1855 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001856 }
1857
1858 return 0;
1859}
1860
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001861static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1862{
1863 int ret;
1864
1865 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1866
1867 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1868 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001869 dwc3_trace(trace_dwc3_gadget,
1870 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001871 return ret;
1872 }
1873
1874 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1875 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001876 dwc3_trace(trace_dwc3_gadget,
1877 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001878 return ret;
1879 }
1880
1881 return 0;
1882}
1883
Felipe Balbi72246da2011-08-19 18:10:58 +03001884static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1885{
1886 struct dwc3_ep *dep;
1887 u8 epnum;
1888
1889 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1890 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001891 if (!dep)
1892 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301893 /*
1894 * Physical endpoints 0 and 1 are special; they form the
1895 * bi-directional USB endpoint 0.
1896 *
1897 * For those two physical endpoints, we don't allocate a TRB
1898 * pool nor do we add them the endpoints list. Due to that, we
1899 * shouldn't do these two operations otherwise we would end up
1900 * with all sorts of bugs when removing dwc3.ko.
1901 */
1902 if (epnum != 0 && epnum != 1) {
1903 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001904 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301905 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001906
1907 kfree(dep);
1908 }
1909}
1910
Felipe Balbi72246da2011-08-19 18:10:58 +03001911/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001912
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301913static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1914 struct dwc3_request *req, struct dwc3_trb *trb,
1915 const struct dwc3_event_depevt *event, int status)
1916{
1917 unsigned int count;
1918 unsigned int s_pkt = 0;
1919 unsigned int trb_status;
1920
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001921 trace_dwc3_complete_trb(dep, trb);
1922
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301923 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1924 /*
1925 * We continue despite the error. There is not much we
1926 * can do. If we don't clean it up we loop forever. If
1927 * we skip the TRB then it gets overwritten after a
1928 * while since we use them in a ring buffer. A BUG()
1929 * would help. Lets hope that if this occurs, someone
1930 * fixes the root cause instead of looking away :)
1931 */
1932 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1933 dep->name, trb);
1934 count = trb->size & DWC3_TRB_SIZE_MASK;
1935
1936 if (dep->direction) {
1937 if (count) {
1938 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1939 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001940 dwc3_trace(trace_dwc3_gadget,
1941 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301942 dep->name);
1943 /*
1944 * If missed isoc occurred and there is
1945 * no request queued then issue END
1946 * TRANSFER, so that core generates
1947 * next xfernotready and we will issue
1948 * a fresh START TRANSFER.
1949 * If there are still queued request
1950 * then wait, do not issue either END
1951 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001952 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301953 * giveback.If any future queued request
1954 * is successfully transferred then we
1955 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001956 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301957 */
1958 dep->flags |= DWC3_EP_MISSED_ISOC;
1959 } else {
1960 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1961 dep->name);
1962 status = -ECONNRESET;
1963 }
1964 } else {
1965 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1966 }
1967 } else {
1968 if (count && (event->status & DEPEVT_STATUS_SHORT))
1969 s_pkt = 1;
1970 }
1971
1972 /*
1973 * We assume here we will always receive the entire data block
1974 * which we should receive. Meaning, if we program RX to
1975 * receive 4K but we receive only 2K, we assume that's all we
1976 * should receive and we simply bounce the request back to the
1977 * gadget driver for further processing.
1978 */
1979 req->request.actual += req->request.length - count;
1980 if (s_pkt)
1981 return 1;
1982 if ((event->status & DEPEVT_STATUS_LST) &&
1983 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1984 DWC3_TRB_CTRL_HWO)))
1985 return 1;
1986 if ((event->status & DEPEVT_STATUS_IOC) &&
1987 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1988 return 1;
1989 return 0;
1990}
1991
Felipe Balbi72246da2011-08-19 18:10:58 +03001992static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1993 const struct dwc3_event_depevt *event, int status)
1994{
1995 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001996 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301997 unsigned int slot;
1998 unsigned int i;
1999 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002000
2001 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002002 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002003 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002004 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002005
Ville Syrjäläd115d702015-08-31 19:48:28 +03002006 i = 0;
2007 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002008 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002009 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002010 slot++;
2011 slot %= DWC3_TRB_NUM;
2012 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002013
Ville Syrjäläd115d702015-08-31 19:48:28 +03002014 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2015 event, status);
2016 if (ret)
2017 break;
2018 } while (++i < req->request.num_mapped_sgs);
2019
2020 dwc3_gadget_giveback(dep, req, status);
2021
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302022 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002023 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002024 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002025
Felipe Balbi4cb42212016-05-18 12:37:21 +03002026 /*
2027 * Our endpoint might get disabled by another thread during
2028 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2029 * early on so DWC3_EP_BUSY flag gets cleared
2030 */
2031 if (!dep->endpoint.desc)
2032 return 1;
2033
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302034 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002035 list_empty(&dep->started_list)) {
2036 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302037 /*
2038 * If there is no entry in request list then do
2039 * not issue END TRANSFER now. Just set PENDING
2040 * flag, so that END TRANSFER is issued when an
2041 * entry is added into request list.
2042 */
2043 dep->flags = DWC3_EP_PENDING_REQUEST;
2044 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002045 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302046 dep->flags = DWC3_EP_ENABLED;
2047 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302048 return 1;
2049 }
2050
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002051 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2052 if ((event->status & DEPEVT_STATUS_IOC) &&
2053 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2054 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002055 return 1;
2056}
2057
2058static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002059 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002060{
2061 unsigned status = 0;
2062 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002063 u32 is_xfer_complete;
2064
2065 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002066
2067 if (event->status & DEPEVT_STATUS_BUSERR)
2068 status = -ECONNRESET;
2069
Paul Zimmerman1d046792012-02-15 18:56:56 -08002070 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002071 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002072 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002073 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002074
2075 /*
2076 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2077 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2078 */
2079 if (dwc->revision < DWC3_REVISION_183A) {
2080 u32 reg;
2081 int i;
2082
2083 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002084 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002085
2086 if (!(dep->flags & DWC3_EP_ENABLED))
2087 continue;
2088
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002089 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002090 return;
2091 }
2092
2093 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2094 reg |= dwc->u1u2;
2095 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2096
2097 dwc->u1u2 = 0;
2098 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002099
Felipe Balbi4cb42212016-05-18 12:37:21 +03002100 /*
2101 * Our endpoint might get disabled by another thread during
2102 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2103 * early on so DWC3_EP_BUSY flag gets cleared
2104 */
2105 if (!dep->endpoint.desc)
2106 return;
2107
Felipe Balbie6e709b2015-09-28 15:16:56 -05002108 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002109 int ret;
2110
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002111 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002112 if (!ret || ret == -EBUSY)
2113 return;
2114 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002115}
2116
Felipe Balbi72246da2011-08-19 18:10:58 +03002117static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2118 const struct dwc3_event_depevt *event)
2119{
2120 struct dwc3_ep *dep;
2121 u8 epnum = event->endpoint_number;
2122
2123 dep = dwc->eps[epnum];
2124
Felipe Balbi3336abb2012-06-06 09:19:35 +03002125 if (!(dep->flags & DWC3_EP_ENABLED))
2126 return;
2127
Felipe Balbi72246da2011-08-19 18:10:58 +03002128 if (epnum == 0 || epnum == 1) {
2129 dwc3_ep0_interrupt(dwc, event);
2130 return;
2131 }
2132
2133 switch (event->endpoint_event) {
2134 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002135 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002136
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002137 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002138 dwc3_trace(trace_dwc3_gadget,
2139 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002140 dep->name);
2141 return;
2142 }
2143
Jingoo Han029d97f2014-07-04 15:00:51 +09002144 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002145 break;
2146 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002147 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002148 break;
2149 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002150 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 dwc3_gadget_start_isoc(dwc, dep, event);
2152 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002153 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002154 int ret;
2155
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002156 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2157
Felipe Balbi73815282015-01-27 13:48:14 -06002158 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002159 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 : "Transfer Not Active");
2161
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002162 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002163 if (!ret || ret == -EBUSY)
2164 return;
2165
Felipe Balbiec5e7952015-11-16 16:04:13 -06002166 dwc3_trace(trace_dwc3_gadget,
2167 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002168 dep->name);
2169 }
2170
2171 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002172 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002173 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002174 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2175 dep->name);
2176 return;
2177 }
2178
2179 switch (event->status) {
2180 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002181 dwc3_trace(trace_dwc3_gadget,
2182 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002183 event->parameters);
2184
2185 break;
2186 case DEPEVT_STREAMEVT_NOTFOUND:
2187 /* FALLTHROUGH */
2188 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002189 dwc3_trace(trace_dwc3_gadget,
2190 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002191 }
2192 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002194 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002196 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002197 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002198 break;
2199 }
2200}
2201
2202static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2203{
2204 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2205 spin_unlock(&dwc->lock);
2206 dwc->gadget_driver->disconnect(&dwc->gadget);
2207 spin_lock(&dwc->lock);
2208 }
2209}
2210
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002211static void dwc3_suspend_gadget(struct dwc3 *dwc)
2212{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002213 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002214 spin_unlock(&dwc->lock);
2215 dwc->gadget_driver->suspend(&dwc->gadget);
2216 spin_lock(&dwc->lock);
2217 }
2218}
2219
2220static void dwc3_resume_gadget(struct dwc3 *dwc)
2221{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002222 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002223 spin_unlock(&dwc->lock);
2224 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002225 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002226 }
2227}
2228
2229static void dwc3_reset_gadget(struct dwc3 *dwc)
2230{
2231 if (!dwc->gadget_driver)
2232 return;
2233
2234 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2235 spin_unlock(&dwc->lock);
2236 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002237 spin_lock(&dwc->lock);
2238 }
2239}
2240
Paul Zimmermanb992e682012-04-27 14:17:35 +03002241static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002242{
2243 struct dwc3_ep *dep;
2244 struct dwc3_gadget_ep_cmd_params params;
2245 u32 cmd;
2246 int ret;
2247
2248 dep = dwc->eps[epnum];
2249
Felipe Balbib4996a82012-06-06 12:04:13 +03002250 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302251 return;
2252
Pratyush Anand57911502012-07-06 15:19:10 +05302253 /*
2254 * NOTICE: We are violating what the Databook says about the
2255 * EndTransfer command. Ideally we would _always_ wait for the
2256 * EndTransfer Command Completion IRQ, but that's causing too
2257 * much trouble synchronizing between us and gadget driver.
2258 *
2259 * We have discussed this with the IP Provider and it was
2260 * suggested to giveback all requests here, but give HW some
2261 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002262 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302263 *
2264 * Note also that a similar handling was tested by Synopsys
2265 * (thanks a lot Paul) and nothing bad has come out of it.
2266 * In short, what we're doing is:
2267 *
2268 * - Issue EndTransfer WITH CMDIOC bit set
2269 * - Wait 100us
2270 */
2271
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302272 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002273 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2274 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002275 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302276 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002277 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302278 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002279 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002280 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302281 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002282}
2283
2284static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2285{
2286 u32 epnum;
2287
2288 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2289 struct dwc3_ep *dep;
2290
2291 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002292 if (!dep)
2293 continue;
2294
Felipe Balbi72246da2011-08-19 18:10:58 +03002295 if (!(dep->flags & DWC3_EP_ENABLED))
2296 continue;
2297
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002298 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002299 }
2300}
2301
2302static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2303{
2304 u32 epnum;
2305
2306 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2307 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002308 int ret;
2309
2310 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002311 if (!dep)
2312 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002313
2314 if (!(dep->flags & DWC3_EP_STALL))
2315 continue;
2316
2317 dep->flags &= ~DWC3_EP_STALL;
2318
John Youn50c763f2016-05-31 17:49:56 -07002319 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002320 WARN_ON_ONCE(ret);
2321 }
2322}
2323
2324static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2325{
Felipe Balbic4430a22012-05-24 10:30:01 +03002326 int reg;
2327
Felipe Balbi72246da2011-08-19 18:10:58 +03002328 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2329 reg &= ~DWC3_DCTL_INITU1ENA;
2330 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2331
2332 reg &= ~DWC3_DCTL_INITU2ENA;
2333 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002334
Felipe Balbi72246da2011-08-19 18:10:58 +03002335 dwc3_disconnect_gadget(dwc);
2336
2337 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002338 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002339 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002340
2341 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002342}
2343
Felipe Balbi72246da2011-08-19 18:10:58 +03002344static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2345{
2346 u32 reg;
2347
Felipe Balbifc8bb912016-05-16 13:14:48 +03002348 dwc->connected = true;
2349
Felipe Balbidf62df52011-10-14 15:11:49 +03002350 /*
2351 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2352 * would cause a missing Disconnect Event if there's a
2353 * pending Setup Packet in the FIFO.
2354 *
2355 * There's no suggested workaround on the official Bug
2356 * report, which states that "unless the driver/application
2357 * is doing any special handling of a disconnect event,
2358 * there is no functional issue".
2359 *
2360 * Unfortunately, it turns out that we _do_ some special
2361 * handling of a disconnect event, namely complete all
2362 * pending transfers, notify gadget driver of the
2363 * disconnection, and so on.
2364 *
2365 * Our suggested workaround is to follow the Disconnect
2366 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002367 * flag. Such flag gets set whenever we have a SETUP_PENDING
2368 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002369 * same endpoint.
2370 *
2371 * Refers to:
2372 *
2373 * STAR#9000466709: RTL: Device : Disconnect event not
2374 * generated if setup packet pending in FIFO
2375 */
2376 if (dwc->revision < DWC3_REVISION_188A) {
2377 if (dwc->setup_packet_pending)
2378 dwc3_gadget_disconnect_interrupt(dwc);
2379 }
2380
Felipe Balbi8e744752014-11-06 14:27:53 +08002381 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002382
2383 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2384 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2385 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002386 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002387
2388 dwc3_stop_active_transfers(dwc);
2389 dwc3_clear_stall_all_ep(dwc);
2390
2391 /* Reset device address to zero */
2392 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2393 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2394 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002395}
2396
2397static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2398{
2399 u32 reg;
2400 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2401
2402 /*
2403 * We change the clock only at SS but I dunno why I would want to do
2404 * this. Maybe it becomes part of the power saving plan.
2405 */
2406
John Younee5cd412016-02-05 17:08:45 -08002407 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2408 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002409 return;
2410
2411 /*
2412 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2413 * each time on Connect Done.
2414 */
2415 if (!usb30_clock)
2416 return;
2417
2418 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2419 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2420 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2421}
2422
Felipe Balbi72246da2011-08-19 18:10:58 +03002423static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2424{
Felipe Balbi72246da2011-08-19 18:10:58 +03002425 struct dwc3_ep *dep;
2426 int ret;
2427 u32 reg;
2428 u8 speed;
2429
Felipe Balbi72246da2011-08-19 18:10:58 +03002430 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2431 speed = reg & DWC3_DSTS_CONNECTSPD;
2432 dwc->speed = speed;
2433
2434 dwc3_update_ram_clk_sel(dwc, speed);
2435
2436 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002437 case DWC3_DCFG_SUPERSPEED_PLUS:
2438 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2439 dwc->gadget.ep0->maxpacket = 512;
2440 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2441 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002442 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002443 /*
2444 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2445 * would cause a missing USB3 Reset event.
2446 *
2447 * In such situations, we should force a USB3 Reset
2448 * event by calling our dwc3_gadget_reset_interrupt()
2449 * routine.
2450 *
2451 * Refers to:
2452 *
2453 * STAR#9000483510: RTL: SS : USB3 reset event may
2454 * not be generated always when the link enters poll
2455 */
2456 if (dwc->revision < DWC3_REVISION_190A)
2457 dwc3_gadget_reset_interrupt(dwc);
2458
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2460 dwc->gadget.ep0->maxpacket = 512;
2461 dwc->gadget.speed = USB_SPEED_SUPER;
2462 break;
2463 case DWC3_DCFG_HIGHSPEED:
2464 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2465 dwc->gadget.ep0->maxpacket = 64;
2466 dwc->gadget.speed = USB_SPEED_HIGH;
2467 break;
2468 case DWC3_DCFG_FULLSPEED2:
2469 case DWC3_DCFG_FULLSPEED1:
2470 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2471 dwc->gadget.ep0->maxpacket = 64;
2472 dwc->gadget.speed = USB_SPEED_FULL;
2473 break;
2474 case DWC3_DCFG_LOWSPEED:
2475 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2476 dwc->gadget.ep0->maxpacket = 8;
2477 dwc->gadget.speed = USB_SPEED_LOW;
2478 break;
2479 }
2480
Pratyush Anand2b758352013-01-14 15:59:31 +05302481 /* Enable USB2 LPM Capability */
2482
John Younee5cd412016-02-05 17:08:45 -08002483 if ((dwc->revision > DWC3_REVISION_194A) &&
2484 (speed != DWC3_DCFG_SUPERSPEED) &&
2485 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302486 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2487 reg |= DWC3_DCFG_LPM_CAP;
2488 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2489
2490 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2491 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2492
Huang Rui460d0982014-10-31 11:11:18 +08002493 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302494
Huang Rui80caf7d2014-10-28 19:54:26 +08002495 /*
2496 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2497 * DCFG.LPMCap is set, core responses with an ACK and the
2498 * BESL value in the LPM token is less than or equal to LPM
2499 * NYET threshold.
2500 */
2501 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2502 && dwc->has_lpm_erratum,
2503 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2504
2505 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2506 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2507
Pratyush Anand2b758352013-01-14 15:59:31 +05302508 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002509 } else {
2510 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2511 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2512 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302513 }
2514
Felipe Balbi72246da2011-08-19 18:10:58 +03002515 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002516 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2517 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002518 if (ret) {
2519 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2520 return;
2521 }
2522
2523 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002524 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2525 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002526 if (ret) {
2527 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2528 return;
2529 }
2530
2531 /*
2532 * Configure PHY via GUSB3PIPECTLn if required.
2533 *
2534 * Update GTXFIFOSIZn
2535 *
2536 * In both cases reset values should be sufficient.
2537 */
2538}
2539
2540static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2541{
Felipe Balbi72246da2011-08-19 18:10:58 +03002542 /*
2543 * TODO take core out of low power mode when that's
2544 * implemented.
2545 */
2546
Jiebing Liad14d4e2014-12-11 13:26:29 +08002547 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2548 spin_unlock(&dwc->lock);
2549 dwc->gadget_driver->resume(&dwc->gadget);
2550 spin_lock(&dwc->lock);
2551 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002552}
2553
2554static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2555 unsigned int evtinfo)
2556{
Felipe Balbifae2b902011-10-14 13:00:30 +03002557 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002558 unsigned int pwropt;
2559
2560 /*
2561 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2562 * Hibernation mode enabled which would show up when device detects
2563 * host-initiated U3 exit.
2564 *
2565 * In that case, device will generate a Link State Change Interrupt
2566 * from U3 to RESUME which is only necessary if Hibernation is
2567 * configured in.
2568 *
2569 * There are no functional changes due to such spurious event and we
2570 * just need to ignore it.
2571 *
2572 * Refers to:
2573 *
2574 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2575 * operational mode
2576 */
2577 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2578 if ((dwc->revision < DWC3_REVISION_250A) &&
2579 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2580 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2581 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002582 dwc3_trace(trace_dwc3_gadget,
2583 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002584 return;
2585 }
2586 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002587
2588 /*
2589 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2590 * on the link partner, the USB session might do multiple entry/exit
2591 * of low power states before a transfer takes place.
2592 *
2593 * Due to this problem, we might experience lower throughput. The
2594 * suggested workaround is to disable DCTL[12:9] bits if we're
2595 * transitioning from U1/U2 to U0 and enable those bits again
2596 * after a transfer completes and there are no pending transfers
2597 * on any of the enabled endpoints.
2598 *
2599 * This is the first half of that workaround.
2600 *
2601 * Refers to:
2602 *
2603 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2604 * core send LGO_Ux entering U0
2605 */
2606 if (dwc->revision < DWC3_REVISION_183A) {
2607 if (next == DWC3_LINK_STATE_U0) {
2608 u32 u1u2;
2609 u32 reg;
2610
2611 switch (dwc->link_state) {
2612 case DWC3_LINK_STATE_U1:
2613 case DWC3_LINK_STATE_U2:
2614 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2615 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2616 | DWC3_DCTL_ACCEPTU2ENA
2617 | DWC3_DCTL_INITU1ENA
2618 | DWC3_DCTL_ACCEPTU1ENA);
2619
2620 if (!dwc->u1u2)
2621 dwc->u1u2 = reg & u1u2;
2622
2623 reg &= ~u1u2;
2624
2625 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2626 break;
2627 default:
2628 /* do nothing */
2629 break;
2630 }
2631 }
2632 }
2633
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002634 switch (next) {
2635 case DWC3_LINK_STATE_U1:
2636 if (dwc->speed == USB_SPEED_SUPER)
2637 dwc3_suspend_gadget(dwc);
2638 break;
2639 case DWC3_LINK_STATE_U2:
2640 case DWC3_LINK_STATE_U3:
2641 dwc3_suspend_gadget(dwc);
2642 break;
2643 case DWC3_LINK_STATE_RESUME:
2644 dwc3_resume_gadget(dwc);
2645 break;
2646 default:
2647 /* do nothing */
2648 break;
2649 }
2650
Felipe Balbie57ebc12014-04-22 13:20:12 -05002651 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002652}
2653
Felipe Balbie1dadd32014-02-25 14:47:54 -06002654static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2655 unsigned int evtinfo)
2656{
2657 unsigned int is_ss = evtinfo & BIT(4);
2658
2659 /**
2660 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2661 * have a known issue which can cause USB CV TD.9.23 to fail
2662 * randomly.
2663 *
2664 * Because of this issue, core could generate bogus hibernation
2665 * events which SW needs to ignore.
2666 *
2667 * Refers to:
2668 *
2669 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2670 * Device Fallback from SuperSpeed
2671 */
2672 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2673 return;
2674
2675 /* enter hibernation here */
2676}
2677
Felipe Balbi72246da2011-08-19 18:10:58 +03002678static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2679 const struct dwc3_event_devt *event)
2680{
2681 switch (event->type) {
2682 case DWC3_DEVICE_EVENT_DISCONNECT:
2683 dwc3_gadget_disconnect_interrupt(dwc);
2684 break;
2685 case DWC3_DEVICE_EVENT_RESET:
2686 dwc3_gadget_reset_interrupt(dwc);
2687 break;
2688 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2689 dwc3_gadget_conndone_interrupt(dwc);
2690 break;
2691 case DWC3_DEVICE_EVENT_WAKEUP:
2692 dwc3_gadget_wakeup_interrupt(dwc);
2693 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002694 case DWC3_DEVICE_EVENT_HIBER_REQ:
2695 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2696 "unexpected hibernation event\n"))
2697 break;
2698
2699 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2700 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2702 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2703 break;
2704 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002705 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002706 break;
2707 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002708 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002709 break;
2710 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002711 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002712 break;
2713 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002714 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002715 break;
2716 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002717 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002718 break;
2719 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002720 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002721 }
2722}
2723
2724static void dwc3_process_event_entry(struct dwc3 *dwc,
2725 const union dwc3_event *event)
2726{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002727 trace_dwc3_event(event->raw);
2728
Felipe Balbi72246da2011-08-19 18:10:58 +03002729 /* Endpoint IRQ, handle it and return early */
2730 if (event->type.is_devspec == 0) {
2731 /* depevt */
2732 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2733 }
2734
2735 switch (event->type.type) {
2736 case DWC3_EVENT_TYPE_DEV:
2737 dwc3_gadget_interrupt(dwc, &event->devt);
2738 break;
2739 /* REVISIT what to do with Carkit and I2C events ? */
2740 default:
2741 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2742 }
2743}
2744
Felipe Balbidea520a2016-03-30 09:39:34 +03002745static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002746{
Felipe Balbidea520a2016-03-30 09:39:34 +03002747 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002748 irqreturn_t ret = IRQ_NONE;
2749 int left;
2750 u32 reg;
2751
Felipe Balbif42f2442013-06-12 21:25:08 +03002752 left = evt->count;
2753
2754 if (!(evt->flags & DWC3_EVENT_PENDING))
2755 return IRQ_NONE;
2756
2757 while (left > 0) {
2758 union dwc3_event event;
2759
2760 event.raw = *(u32 *) (evt->buf + evt->lpos);
2761
2762 dwc3_process_event_entry(dwc, &event);
2763
2764 /*
2765 * FIXME we wrap around correctly to the next entry as
2766 * almost all entries are 4 bytes in size. There is one
2767 * entry which has 12 bytes which is a regular entry
2768 * followed by 8 bytes data. ATM I don't know how
2769 * things are organized if we get next to the a
2770 * boundary so I worry about that once we try to handle
2771 * that.
2772 */
2773 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2774 left -= 4;
2775
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002776 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002777 }
2778
2779 evt->count = 0;
2780 evt->flags &= ~DWC3_EVENT_PENDING;
2781 ret = IRQ_HANDLED;
2782
2783 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002784 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002785 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002786 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002787
2788 return ret;
2789}
2790
Felipe Balbidea520a2016-03-30 09:39:34 +03002791static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002792{
Felipe Balbidea520a2016-03-30 09:39:34 +03002793 struct dwc3_event_buffer *evt = _evt;
2794 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002795 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002796 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002797
Felipe Balbie5f68b42015-10-12 13:25:44 -05002798 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002799 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002800 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002801
2802 return ret;
2803}
2804
Felipe Balbidea520a2016-03-30 09:39:34 +03002805static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002806{
Felipe Balbidea520a2016-03-30 09:39:34 +03002807 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002808 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002809 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002810
Felipe Balbifc8bb912016-05-16 13:14:48 +03002811 if (pm_runtime_suspended(dwc->dev)) {
2812 pm_runtime_get(dwc->dev);
2813 disable_irq_nosync(dwc->irq_gadget);
2814 dwc->pending_events = true;
2815 return IRQ_HANDLED;
2816 }
2817
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002818 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002819 count &= DWC3_GEVNTCOUNT_MASK;
2820 if (!count)
2821 return IRQ_NONE;
2822
Felipe Balbib15a7622011-06-30 16:57:15 +03002823 evt->count = count;
2824 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002825
Felipe Balbie8adfc32013-06-12 21:11:14 +03002826 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002827 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002828 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002829 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002830
Felipe Balbib15a7622011-06-30 16:57:15 +03002831 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002832}
2833
Felipe Balbidea520a2016-03-30 09:39:34 +03002834static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002835{
Felipe Balbidea520a2016-03-30 09:39:34 +03002836 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002837
Felipe Balbidea520a2016-03-30 09:39:34 +03002838 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002839}
2840
2841/**
2842 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002843 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002844 *
2845 * Returns 0 on success otherwise negative errno.
2846 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002847int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002848{
Felipe Balbi72246da2011-08-19 18:10:58 +03002849 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002850
2851 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2852 &dwc->ctrl_req_addr, GFP_KERNEL);
2853 if (!dwc->ctrl_req) {
2854 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2855 ret = -ENOMEM;
2856 goto err0;
2857 }
2858
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302859 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002860 &dwc->ep0_trb_addr, GFP_KERNEL);
2861 if (!dwc->ep0_trb) {
2862 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2863 ret = -ENOMEM;
2864 goto err1;
2865 }
2866
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002867 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002868 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002869 ret = -ENOMEM;
2870 goto err2;
2871 }
2872
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002873 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002874 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2875 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002876 if (!dwc->ep0_bounce) {
2877 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2878 ret = -ENOMEM;
2879 goto err3;
2880 }
2881
Felipe Balbi04c03d12015-12-02 10:06:45 -06002882 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2883 if (!dwc->zlp_buf) {
2884 ret = -ENOMEM;
2885 goto err4;
2886 }
2887
Felipe Balbi72246da2011-08-19 18:10:58 +03002888 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002890 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002891 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002892 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002893
2894 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002895 * FIXME We might be setting max_speed to <SUPER, however versions
2896 * <2.20a of dwc3 have an issue with metastability (documented
2897 * elsewhere in this driver) which tells us we can't set max speed to
2898 * anything lower than SUPER.
2899 *
2900 * Because gadget.max_speed is only used by composite.c and function
2901 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2902 * to happen so we avoid sending SuperSpeed Capability descriptor
2903 * together with our BOS descriptor as that could confuse host into
2904 * thinking we can handle super speed.
2905 *
2906 * Note that, in fact, we won't even support GetBOS requests when speed
2907 * is less than super speed because we don't have means, yet, to tell
2908 * composite.c that we are USB 2.0 + LPM ECN.
2909 */
2910 if (dwc->revision < DWC3_REVISION_220A)
2911 dwc3_trace(trace_dwc3_gadget,
2912 "Changing max_speed on rev %08x\n",
2913 dwc->revision);
2914
2915 dwc->gadget.max_speed = dwc->maximum_speed;
2916
2917 /*
David Cohena4b9d942013-12-09 15:55:38 -08002918 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2919 * on ep out.
2920 */
2921 dwc->gadget.quirk_ep_out_aligned_size = true;
2922
2923 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002924 * REVISIT: Here we should clear all pending IRQs to be
2925 * sure we're starting from a well known location.
2926 */
2927
2928 ret = dwc3_gadget_init_endpoints(dwc);
2929 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002930 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002931
Felipe Balbi72246da2011-08-19 18:10:58 +03002932 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2933 if (ret) {
2934 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002935 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002936 }
2937
2938 return 0;
2939
Felipe Balbi04c03d12015-12-02 10:06:45 -06002940err5:
2941 kfree(dwc->zlp_buf);
2942
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002943err4:
David Cohene1f80462013-09-11 17:42:47 -07002944 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002945 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2946 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002947
Felipe Balbi72246da2011-08-19 18:10:58 +03002948err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002949 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002950
2951err2:
2952 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2953 dwc->ep0_trb, dwc->ep0_trb_addr);
2954
2955err1:
2956 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2957 dwc->ctrl_req, dwc->ctrl_req_addr);
2958
2959err0:
2960 return ret;
2961}
2962
Felipe Balbi7415f172012-04-30 14:56:33 +03002963/* -------------------------------------------------------------------------- */
2964
Felipe Balbi72246da2011-08-19 18:10:58 +03002965void dwc3_gadget_exit(struct dwc3 *dwc)
2966{
Felipe Balbi72246da2011-08-19 18:10:58 +03002967 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002968
Felipe Balbi72246da2011-08-19 18:10:58 +03002969 dwc3_gadget_free_endpoints(dwc);
2970
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002971 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2972 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002973
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002974 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002975 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002976
2977 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2978 dwc->ep0_trb, dwc->ep0_trb_addr);
2979
2980 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2981 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002982}
Felipe Balbi7415f172012-04-30 14:56:33 +03002983
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002984int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002985{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002986 int ret;
2987
Roger Quadros9772b472016-04-12 11:33:29 +03002988 if (!dwc->gadget_driver)
2989 return 0;
2990
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002991 ret = dwc3_gadget_run_stop(dwc, false, false);
2992 if (ret < 0)
2993 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002994
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002995 dwc3_disconnect_gadget(dwc);
2996 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002997
2998 return 0;
2999}
3000
3001int dwc3_gadget_resume(struct dwc3 *dwc)
3002{
Felipe Balbi7415f172012-04-30 14:56:33 +03003003 int ret;
3004
Roger Quadros9772b472016-04-12 11:33:29 +03003005 if (!dwc->gadget_driver)
3006 return 0;
3007
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003008 ret = __dwc3_gadget_start(dwc);
3009 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003010 goto err0;
3011
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003012 ret = dwc3_gadget_run_stop(dwc, true, false);
3013 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003014 goto err1;
3015
Felipe Balbi7415f172012-04-30 14:56:33 +03003016 return 0;
3017
3018err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003019 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003020
3021err0:
3022 return ret;
3023}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003024
3025void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3026{
3027 if (dwc->pending_events) {
3028 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3029 dwc->pending_events = false;
3030 enable_irq(dwc->irq_gadget);
3031 }
3032}