blob: 194775184a58eed977ef465e7f73818c61ecf3f5 [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;
210 u32 reg;
211
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500212 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500213
Felipe Balbib09bb642012-04-24 16:19:11 +0300214 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
215 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
216
217 do {
218 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
219 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600220 dwc3_trace(trace_dwc3_gadget,
221 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300222 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530223 if (DWC3_DGCMD_STATUS(reg))
224 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300225 return 0;
226 }
227
228 /*
229 * We can't sleep here, because it's also called from
230 * interrupt context.
231 */
232 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600233 if (!timeout) {
234 dwc3_trace(trace_dwc3_gadget,
235 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300236 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600237 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300238 udelay(1);
239 } while (1);
240}
241
Felipe Balbic36d8e92016-04-04 12:46:33 +0300242static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
243
Felipe Balbi2cd47182016-04-12 16:42:43 +0300244int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
245 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300246{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300247 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200248 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300249 u32 reg;
250
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300251 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300252 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300253
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500254 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300255
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300256 /*
257 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
258 * we're issuing an endpoint command, we must check if
259 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
260 *
261 * We will also set SUSPHY bit to what it was before returning as stated
262 * by the same section on Synopsys databook.
263 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300264 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
265 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
266 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
267 susphy = true;
268 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
269 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
270 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300271 }
272
Felipe Balbic36d8e92016-04-04 12:46:33 +0300273 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
274 int needs_wakeup;
275
276 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
277 dwc->link_state == DWC3_LINK_STATE_U2 ||
278 dwc->link_state == DWC3_LINK_STATE_U3);
279
280 if (unlikely(needs_wakeup)) {
281 ret = __dwc3_gadget_wakeup(dwc);
282 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
283 ret);
284 }
285 }
286
Felipe Balbi2eb88012016-04-12 16:53:39 +0300287 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
288 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
289 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290
Felipe Balbi2eb88012016-04-12 16:53:39 +0300291 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300293 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000295 int cmd_status = DWC3_DEPCMD_STATUS(reg);
296
Felipe Balbi73815282015-01-27 13:48:14 -0600297 dwc3_trace(trace_dwc3_gadget,
298 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000299 cmd_status);
300
301 switch (cmd_status) {
302 case 0:
303 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300304 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000305 case DEPEVT_TRANSFER_NO_RESOURCE:
306 dwc3_trace(trace_dwc3_gadget, "%s: no resource available");
307 ret = -EINVAL;
308 break;
309 case DEPEVT_TRANSFER_BUS_EXPIRY:
310 /*
311 * SW issues START TRANSFER command to
312 * isochronous ep with future frame interval. If
313 * future interval time has already passed when
314 * core receives the command, it will respond
315 * with an error status of 'Bus Expiry'.
316 *
317 * Instead of always returning -EINVAL, let's
318 * give a hint to the gadget driver that this is
319 * the case by returning -EAGAIN.
320 */
321 dwc3_trace(trace_dwc3_gadget, "%s: bus expiry");
322 ret = -EAGAIN;
323 break;
324 default:
325 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
326 }
327
Felipe Balbic0ca3242016-04-04 09:11:51 +0300328 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300329 }
330
331 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300332 * We can't sleep here, because it is also called from
333 * interrupt context.
334 */
335 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600336 if (!timeout) {
337 dwc3_trace(trace_dwc3_gadget,
338 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300339 ret = -ETIMEDOUT;
340 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600341 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300343
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300344 if (unlikely(susphy)) {
345 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
346 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
347 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
348 }
349
Felipe Balbic0ca3242016-04-04 09:11:51 +0300350 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300351}
352
John Youn50c763f2016-05-31 17:49:56 -0700353static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
354{
355 struct dwc3 *dwc = dep->dwc;
356 struct dwc3_gadget_ep_cmd_params params;
357 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
358
359 /*
360 * As of core revision 2.60a the recommended programming model
361 * is to set the ClearPendIN bit when issuing a Clear Stall EP
362 * command for IN endpoints. This is to prevent an issue where
363 * some (non-compliant) hosts may not send ACK TPs for pending
364 * IN transfers due to a mishandled error condition. Synopsys
365 * STAR 9000614252.
366 */
367 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
368 cmd |= DWC3_DEPCMD_CLEARPENDIN;
369
370 memset(&params, 0, sizeof(params));
371
Felipe Balbi2cd47182016-04-12 16:42:43 +0300372 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700373}
374
Felipe Balbi72246da2011-08-19 18:10:58 +0300375static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200376 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300377{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300378 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379
380 return dep->trb_pool_dma + offset;
381}
382
383static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
384{
385 struct dwc3 *dwc = dep->dwc;
386
387 if (dep->trb_pool)
388 return 0;
389
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 dep->trb_pool = dma_alloc_coherent(dwc->dev,
391 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
392 &dep->trb_pool_dma, GFP_KERNEL);
393 if (!dep->trb_pool) {
394 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
395 dep->name);
396 return -ENOMEM;
397 }
398
399 return 0;
400}
401
402static void dwc3_free_trb_pool(struct dwc3_ep *dep)
403{
404 struct dwc3 *dwc = dep->dwc;
405
406 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
407 dep->trb_pool, dep->trb_pool_dma);
408
409 dep->trb_pool = NULL;
410 dep->trb_pool_dma = 0;
411}
412
John Younc4509602016-02-16 20:10:53 -0800413static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
414
415/**
416 * dwc3_gadget_start_config - Configure EP resources
417 * @dwc: pointer to our controller context structure
418 * @dep: endpoint that is being enabled
419 *
420 * The assignment of transfer resources cannot perfectly follow the
421 * data book due to the fact that the controller driver does not have
422 * all knowledge of the configuration in advance. It is given this
423 * information piecemeal by the composite gadget framework after every
424 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
425 * programming model in this scenario can cause errors. For two
426 * reasons:
427 *
428 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
429 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
430 * multiple interfaces.
431 *
432 * 2) The databook does not mention doing more DEPXFERCFG for new
433 * endpoint on alt setting (8.1.6).
434 *
435 * The following simplified method is used instead:
436 *
437 * All hardware endpoints can be assigned a transfer resource and this
438 * setting will stay persistent until either a core reset or
439 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
440 * do DEPXFERCFG for every hardware endpoint as well. We are
441 * guaranteed that there are as many transfer resources as endpoints.
442 *
443 * This function is called for each endpoint when it is being enabled
444 * but is triggered only when called for EP0-out, which always happens
445 * first, and which should only happen in one of the above conditions.
446 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300447static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
448{
449 struct dwc3_gadget_ep_cmd_params params;
450 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800451 int i;
452 int ret;
453
454 if (dep->number)
455 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
457 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800458 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300459
Felipe Balbi2cd47182016-04-12 16:42:43 +0300460 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800461 if (ret)
462 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300463
John Younc4509602016-02-16 20:10:53 -0800464 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
465 struct dwc3_ep *dep = dwc->eps[i];
466
467 if (!dep)
468 continue;
469
470 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
471 if (ret)
472 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
474
475 return 0;
476}
477
478static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200479 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300480 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600481 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300482{
483 struct dwc3_gadget_ep_cmd_params params;
484
485 memset(&params, 0x00, sizeof(params));
486
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300487 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900488 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
489
490 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800491 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300492 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300493 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900494 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300495
Felipe Balbi4b345c92012-07-16 14:08:16 +0300496 if (ignore)
497 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
498
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600499 if (restore) {
500 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
501 params.param2 |= dep->saved_state;
502 }
503
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300504 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
505 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200507 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300508 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
509 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300510 dep->stream_capable = true;
511 }
512
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500513 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300514 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300515
516 /*
517 * We are doing 1:1 mapping for endpoints, meaning
518 * Physical Endpoints 2 maps to Logical Endpoint 2 and
519 * so on. We consider the direction bit as part of the physical
520 * endpoint number. So USB endpoint 0x81 is 0x03.
521 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300522 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 /*
525 * We must use the lower 16 TX FIFOs even though
526 * HW might have more
527 */
528 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300529 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
531 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300532 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300533 dep->interval = 1 << (desc->bInterval - 1);
534 }
535
Felipe Balbi2cd47182016-04-12 16:42:43 +0300536 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300537}
538
539static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
540{
541 struct dwc3_gadget_ep_cmd_params params;
542
543 memset(&params, 0x00, sizeof(params));
544
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300545 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300546
Felipe Balbi2cd47182016-04-12 16:42:43 +0300547 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
548 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300549}
550
551/**
552 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
553 * @dep: endpoint to be initialized
554 * @desc: USB Endpoint Descriptor
555 *
556 * Caller should take care of locking
557 */
558static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200559 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300560 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600561 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300562{
563 struct dwc3 *dwc = dep->dwc;
564 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300565 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
Felipe Balbi73815282015-01-27 13:48:14 -0600567 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300568
Felipe Balbi72246da2011-08-19 18:10:58 +0300569 if (!(dep->flags & DWC3_EP_ENABLED)) {
570 ret = dwc3_gadget_start_config(dwc, dep);
571 if (ret)
572 return ret;
573 }
574
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600575 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
576 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 if (ret)
578 return ret;
579
580 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200581 struct dwc3_trb *trb_st_hw;
582 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300583
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200584 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200585 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 dep->type = usb_endpoint_type(desc);
587 dep->flags |= DWC3_EP_ENABLED;
588
589 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
590 reg |= DWC3_DALEPENA_EP(dep->number);
591 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
592
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300593 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200594 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300595
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300596 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 trb_st_hw = &dep->trb_pool[0];
598
Felipe Balbif6bafc62012-02-06 11:04:53 +0200599 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700600 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300601
Felipe Balbif6bafc62012-02-06 11:04:53 +0200602 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
603 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
604 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
605 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 }
607
Felipe Balbie901aa12016-03-16 14:01:37 +0200608out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500609 switch (usb_endpoint_type(desc)) {
610 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200611 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500612 break;
613 case USB_ENDPOINT_XFER_ISOC:
614 strlcat(dep->name, "-isoc", sizeof(dep->name));
615 break;
616 case USB_ENDPOINT_XFER_BULK:
617 strlcat(dep->name, "-bulk", sizeof(dep->name));
618 break;
619 case USB_ENDPOINT_XFER_INT:
620 strlcat(dep->name, "-int", sizeof(dep->name));
621 break;
622 default:
623 dev_err(dwc->dev, "invalid endpoint transfer type\n");
624 }
625
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 return 0;
627}
628
Paul Zimmermanb992e682012-04-27 14:17:35 +0300629static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200630static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300631{
632 struct dwc3_request *req;
633
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200634 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300635 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200636
Pratyush Anand57911502012-07-06 15:19:10 +0530637 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200638 while (!list_empty(&dep->started_list)) {
639 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530640
641 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
642 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200643 }
644
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200645 while (!list_empty(&dep->pending_list)) {
646 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300647
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200648 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300650}
651
652/**
653 * __dwc3_gadget_ep_disable - Disables a HW endpoint
654 * @dep: the endpoint to disable
655 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200656 * This function also removes requests which are currently processed ny the
657 * hardware and those which are not yet scheduled.
658 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300659 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300660static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
661{
662 struct dwc3 *dwc = dep->dwc;
663 u32 reg;
664
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500665 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
666
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200667 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300668
Felipe Balbi687ef982014-04-16 10:30:33 -0500669 /* make sure HW endpoint isn't stalled */
670 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500671 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500672
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
674 reg &= ~DWC3_DALEPENA_EP(dep->number);
675 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
676
Felipe Balbi879631a2011-09-30 10:58:47 +0300677 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200678 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200679 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300681 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300682
Felipe Balbiaa739972015-07-20 14:48:13 -0500683 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
684 dep->number >> 1,
685 (dep->number & 1) ? "in" : "out");
686
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 return 0;
688}
689
690/* -------------------------------------------------------------------------- */
691
692static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
693 const struct usb_endpoint_descriptor *desc)
694{
695 return -EINVAL;
696}
697
698static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
699{
700 return -EINVAL;
701}
702
703/* -------------------------------------------------------------------------- */
704
705static int dwc3_gadget_ep_enable(struct usb_ep *ep,
706 const struct usb_endpoint_descriptor *desc)
707{
708 struct dwc3_ep *dep;
709 struct dwc3 *dwc;
710 unsigned long flags;
711 int ret;
712
713 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
714 pr_debug("dwc3: invalid parameters\n");
715 return -EINVAL;
716 }
717
718 if (!desc->wMaxPacketSize) {
719 pr_debug("dwc3: missing wMaxPacketSize\n");
720 return -EINVAL;
721 }
722
723 dep = to_dwc3_ep(ep);
724 dwc = dep->dwc;
725
Felipe Balbi95ca9612015-12-10 13:08:20 -0600726 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
727 "%s is already enabled\n",
728 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300729 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300730
Felipe Balbi72246da2011-08-19 18:10:58 +0300731 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600732 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300733 spin_unlock_irqrestore(&dwc->lock, flags);
734
735 return ret;
736}
737
738static int dwc3_gadget_ep_disable(struct usb_ep *ep)
739{
740 struct dwc3_ep *dep;
741 struct dwc3 *dwc;
742 unsigned long flags;
743 int ret;
744
745 if (!ep) {
746 pr_debug("dwc3: invalid parameters\n");
747 return -EINVAL;
748 }
749
750 dep = to_dwc3_ep(ep);
751 dwc = dep->dwc;
752
Felipe Balbi95ca9612015-12-10 13:08:20 -0600753 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
754 "%s is already disabled\n",
755 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300757
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 spin_lock_irqsave(&dwc->lock, flags);
759 ret = __dwc3_gadget_ep_disable(dep);
760 spin_unlock_irqrestore(&dwc->lock, flags);
761
762 return ret;
763}
764
765static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
766 gfp_t gfp_flags)
767{
768 struct dwc3_request *req;
769 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300770
771 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900772 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300773 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300774
775 req->epnum = dep->number;
776 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300777
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500778 trace_dwc3_alloc_request(req);
779
Felipe Balbi72246da2011-08-19 18:10:58 +0300780 return &req->request;
781}
782
783static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
784 struct usb_request *request)
785{
786 struct dwc3_request *req = to_dwc3_request(request);
787
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500788 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 kfree(req);
790}
791
Felipe Balbic71fc372011-11-22 11:37:34 +0200792/**
793 * dwc3_prepare_one_trb - setup one TRB from one request
794 * @dep: endpoint for which this request is prepared
795 * @req: dwc3_request pointer
796 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200797static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200798 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530799 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200800{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200801 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200802
Felipe Balbi73815282015-01-27 13:48:14 -0600803 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200804 dep->name, req, (unsigned long long) dma,
805 length, last ? " last" : "",
806 chain ? " chain" : "");
807
Pratyush Anand915e2022013-01-14 15:59:35 +0530808
Felipe Balbi4faf7552016-04-05 13:14:31 +0300809 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200810
Felipe Balbieeb720f2011-11-28 12:46:59 +0200811 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200812 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200813 req->trb = trb;
814 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300815 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200816 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200817
Felipe Balbief966b92016-04-05 13:09:51 +0300818 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300819 /* Skip the LINK-TRB */
820 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300821 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530822
Felipe Balbif6bafc62012-02-06 11:04:53 +0200823 trb->size = DWC3_TRB_SIZE_LENGTH(length);
824 trb->bpl = lower_32_bits(dma);
825 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200826
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200827 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200828 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200829 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200830 break;
831
832 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530833 if (!node)
834 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
835 else
836 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200837
838 /* always enable Interrupt on Missed ISOC */
839 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200840 break;
841
842 case USB_ENDPOINT_XFER_BULK:
843 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200844 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200845 break;
846 default:
847 /*
848 * This is only possible with faulty memory because we
849 * checked it already :)
850 */
851 BUG();
852 }
853
Felipe Balbica4d44e2016-03-10 13:53:27 +0200854 /* always enable Continue on Short Packet */
855 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600856
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300857 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200858 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
859
860 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530861 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200862
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530863 if (chain)
864 trb->ctrl |= DWC3_TRB_CTRL_CHN;
865
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200866 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200867 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
868
869 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500870
871 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200872}
873
Felipe Balbic4233572016-05-12 14:08:34 +0300874static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
875{
876 struct dwc3_trb *tmp;
877
878 /*
879 * If enqueue & dequeue are equal than it is either full or empty.
880 *
881 * One way to know for sure is if the TRB right before us has HWO bit
882 * set or not. If it has, then we're definitely full and can't fit any
883 * more transfers in our ring.
884 */
885 if (dep->trb_enqueue == dep->trb_dequeue) {
886 /* If we're full, enqueue/dequeue are > 0 */
887 if (dep->trb_enqueue) {
888 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
889 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
890 return 0;
891 }
892
893 return DWC3_TRB_NUM - 1;
894 }
895
896 return dep->trb_dequeue - dep->trb_enqueue;
897}
898
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300899static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
900 struct dwc3_request *req, unsigned int trbs_left)
901{
902 struct usb_request *request = &req->request;
903 struct scatterlist *sg = request->sg;
904 struct scatterlist *s;
905 unsigned int last = false;
906 unsigned int length;
907 dma_addr_t dma;
908 int i;
909
910 for_each_sg(sg, s, request->num_mapped_sgs, i) {
911 unsigned chain = true;
912
913 length = sg_dma_len(s);
914 dma = sg_dma_address(s);
915
916 if (sg_is_last(s)) {
917 if (list_is_last(&req->list, &dep->pending_list))
918 last = true;
919
920 chain = false;
921 }
922
923 if (!trbs_left)
924 last = true;
925
926 if (last)
927 chain = false;
928
929 dwc3_prepare_one_trb(dep, req, dma, length,
930 last, chain, i);
931
932 if (last)
933 break;
934 }
935}
936
937static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
938 struct dwc3_request *req, unsigned int trbs_left)
939{
940 unsigned int last = false;
941 unsigned int length;
942 dma_addr_t dma;
943
944 dma = req->request.dma;
945 length = req->request.length;
946
947 if (!trbs_left)
948 last = true;
949
950 /* Is this the last request? */
951 if (list_is_last(&req->list, &dep->pending_list))
952 last = true;
953
954 dwc3_prepare_one_trb(dep, req, dma, length,
955 last, false, 0);
956}
957
Felipe Balbi72246da2011-08-19 18:10:58 +0300958/*
959 * dwc3_prepare_trbs - setup TRBs from requests
960 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300961 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800962 * The function goes through the requests list and sets up TRBs for the
963 * transfers. The function returns once there are no more TRBs available or
964 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 */
Felipe Balbic4233572016-05-12 14:08:34 +0300966static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300967{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200968 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300969 u32 trbs_left;
970
971 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
972
Felipe Balbic4233572016-05-12 14:08:34 +0300973 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300974
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200975 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300976 if (req->request.num_mapped_sgs > 0)
977 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
978 else
979 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300980
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300981 if (!trbs_left)
982 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300983 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300984}
985
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300986static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300987{
988 struct dwc3_gadget_ep_cmd_params params;
989 struct dwc3_request *req;
990 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300991 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 int ret;
993 u32 cmd;
994
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300995 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300996
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300997 dwc3_prepare_trbs(dep);
998 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 if (!req) {
1000 dep->flags |= DWC3_EP_PENDING_REQUEST;
1001 return 0;
1002 }
1003
1004 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001005
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001006 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301007 params.param0 = upper_32_bits(req->trb_dma);
1008 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001009 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301010 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001011 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301012 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001013
1014 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +03001015 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001017 /*
1018 * FIXME we need to iterate over the list of requests
1019 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001020 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001021 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001022 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1023 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 list_del(&req->list);
1025 return ret;
1026 }
1027
1028 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001029
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001030 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001031 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001032 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001033 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001034
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 return 0;
1036}
1037
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301038static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1039 struct dwc3_ep *dep, u32 cur_uf)
1040{
1041 u32 uf;
1042
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001043 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001044 dwc3_trace(trace_dwc3_gadget,
1045 "ISOC ep %s run out for requests",
1046 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301047 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301048 return;
1049 }
1050
1051 /* 4 micro frames in the future */
1052 uf = cur_uf + dep->interval * 4;
1053
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001054 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301055}
1056
1057static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1058 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1059{
1060 u32 cur_uf, mask;
1061
1062 mask = ~(dep->interval - 1);
1063 cur_uf = event->parameters & mask;
1064
1065 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1066}
1067
Felipe Balbi72246da2011-08-19 18:10:58 +03001068static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1069{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001070 struct dwc3 *dwc = dep->dwc;
1071 int ret;
1072
Felipe Balbibb423982015-11-16 15:31:21 -06001073 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001074 dwc3_trace(trace_dwc3_gadget,
1075 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001076 &req->request, dep->endpoint.name);
1077 return -ESHUTDOWN;
1078 }
1079
1080 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1081 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001082 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1083 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001084 return -EINVAL;
1085 }
1086
Felipe Balbifc8bb912016-05-16 13:14:48 +03001087 pm_runtime_get(dwc->dev);
1088
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 req->request.actual = 0;
1090 req->request.status = -EINPROGRESS;
1091 req->direction = dep->direction;
1092 req->epnum = dep->number;
1093
Felipe Balbife84f522015-09-01 09:01:38 -05001094 trace_dwc3_ep_queue(req);
1095
Felipe Balbi72246da2011-08-19 18:10:58 +03001096 /*
1097 * We only add to our list of requests now and
1098 * start consuming the list once we get XferNotReady
1099 * IRQ.
1100 *
1101 * That way, we avoid doing anything that we don't need
1102 * to do now and defer it until the point we receive a
1103 * particular token from the Host side.
1104 *
1105 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001106 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001107 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001108 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1109 dep->direction);
1110 if (ret)
1111 return ret;
1112
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001113 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001114
1115 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001116 * If there are no pending requests and the endpoint isn't already
1117 * busy, we will just start the request straight away.
1118 *
1119 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1120 * little bit faster.
1121 */
1122 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001123 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001124 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001125 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001126 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001127 }
1128
1129 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001130 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001131 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001132 * 1. XferNotReady with empty list of requests. We need to kick the
1133 * transfer here in that situation, otherwise we will be NAKing
1134 * forever. If we get XferNotReady before gadget driver has a
1135 * chance to queue a request, we will ACK the IRQ but won't be
1136 * able to receive the data until the next request is queued.
1137 * The following code is handling exactly that.
1138 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001139 */
1140 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301141 /*
1142 * If xfernotready is already elapsed and it is a case
1143 * of isoc transfer, then issue END TRANSFER, so that
1144 * you can receive xfernotready again and can have
1145 * notion of current microframe.
1146 */
1147 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001148 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001149 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301150 dep->flags = DWC3_EP_ENABLED;
1151 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301152 return 0;
1153 }
1154
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001155 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001156 if (!ret)
1157 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1158
Felipe Balbia8f32812015-09-16 10:40:07 -05001159 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001160 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001161
Felipe Balbib511e5e2012-06-06 12:00:50 +03001162 /*
1163 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1164 * kick the transfer here after queuing a request, otherwise the
1165 * core may not see the modified TRB(s).
1166 */
1167 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301168 (dep->flags & DWC3_EP_BUSY) &&
1169 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001170 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001171 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001172 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001173 }
1174
Felipe Balbib997ada2012-07-26 13:26:50 +03001175 /*
1176 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1177 * right away, otherwise host will not know we have streams to be
1178 * handled.
1179 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001180 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001181 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001182
Felipe Balbia8f32812015-09-16 10:40:07 -05001183out:
1184 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001185 dwc3_trace(trace_dwc3_gadget,
1186 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001187 dep->name);
1188 if (ret == -EBUSY)
1189 ret = 0;
1190
1191 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001192}
1193
Felipe Balbi04c03d12015-12-02 10:06:45 -06001194static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1195 struct usb_request *request)
1196{
1197 dwc3_gadget_ep_free_request(ep, request);
1198}
1199
1200static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1201{
1202 struct dwc3_request *req;
1203 struct usb_request *request;
1204 struct usb_ep *ep = &dep->endpoint;
1205
1206 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1207 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1208 if (!request)
1209 return -ENOMEM;
1210
1211 request->length = 0;
1212 request->buf = dwc->zlp_buf;
1213 request->complete = __dwc3_gadget_ep_zlp_complete;
1214
1215 req = to_dwc3_request(request);
1216
1217 return __dwc3_gadget_ep_queue(dep, req);
1218}
1219
Felipe Balbi72246da2011-08-19 18:10:58 +03001220static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1221 gfp_t gfp_flags)
1222{
1223 struct dwc3_request *req = to_dwc3_request(request);
1224 struct dwc3_ep *dep = to_dwc3_ep(ep);
1225 struct dwc3 *dwc = dep->dwc;
1226
1227 unsigned long flags;
1228
1229 int ret;
1230
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001231 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001233
1234 /*
1235 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1236 * setting request->zero, instead of doing magic, we will just queue an
1237 * extra usb_request ourselves so that it gets handled the same way as
1238 * any other request.
1239 */
John Yound92618982015-12-22 12:23:20 -08001240 if (ret == 0 && request->zero && request->length &&
1241 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001242 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1243
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 spin_unlock_irqrestore(&dwc->lock, flags);
1245
1246 return ret;
1247}
1248
1249static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1250 struct usb_request *request)
1251{
1252 struct dwc3_request *req = to_dwc3_request(request);
1253 struct dwc3_request *r = NULL;
1254
1255 struct dwc3_ep *dep = to_dwc3_ep(ep);
1256 struct dwc3 *dwc = dep->dwc;
1257
1258 unsigned long flags;
1259 int ret = 0;
1260
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001261 trace_dwc3_ep_dequeue(req);
1262
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 spin_lock_irqsave(&dwc->lock, flags);
1264
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001265 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 if (r == req)
1267 break;
1268 }
1269
1270 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001271 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 if (r == req)
1273 break;
1274 }
1275 if (r == req) {
1276 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001277 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301278 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001279 }
1280 dev_err(dwc->dev, "request %p was not queued to %s\n",
1281 request, ep->name);
1282 ret = -EINVAL;
1283 goto out0;
1284 }
1285
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301286out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001287 /* giveback the request */
1288 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1289
1290out0:
1291 spin_unlock_irqrestore(&dwc->lock, flags);
1292
1293 return ret;
1294}
1295
Felipe Balbi7a608552014-09-24 14:19:52 -05001296int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001297{
1298 struct dwc3_gadget_ep_cmd_params params;
1299 struct dwc3 *dwc = dep->dwc;
1300 int ret;
1301
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001302 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1303 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1304 return -EINVAL;
1305 }
1306
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 memset(&params, 0x00, sizeof(params));
1308
1309 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001310 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001311 (!list_empty(&dep->started_list) ||
1312 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001313 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001314 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001315 dep->name);
1316 return -EAGAIN;
1317 }
1318
Felipe Balbi2cd47182016-04-12 16:42:43 +03001319 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1320 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001321 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001322 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 dep->name);
1324 else
1325 dep->flags |= DWC3_EP_STALL;
1326 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001327
John Youn50c763f2016-05-31 17:49:56 -07001328 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001330 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001331 dep->name);
1332 else
Alan Sterna535d812013-11-01 12:05:12 -04001333 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001335
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 return ret;
1337}
1338
1339static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1340{
1341 struct dwc3_ep *dep = to_dwc3_ep(ep);
1342 struct dwc3 *dwc = dep->dwc;
1343
1344 unsigned long flags;
1345
1346 int ret;
1347
1348 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001349 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001350 spin_unlock_irqrestore(&dwc->lock, flags);
1351
1352 return ret;
1353}
1354
1355static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1356{
1357 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001358 struct dwc3 *dwc = dep->dwc;
1359 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001360 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001361
Paul Zimmerman249a4562012-02-24 17:32:16 -08001362 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 dep->flags |= DWC3_EP_WEDGE;
1364
Pratyush Anand08f0d962012-06-25 22:40:43 +05301365 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001366 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301367 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001368 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001369 spin_unlock_irqrestore(&dwc->lock, flags);
1370
1371 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001372}
1373
1374/* -------------------------------------------------------------------------- */
1375
1376static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1377 .bLength = USB_DT_ENDPOINT_SIZE,
1378 .bDescriptorType = USB_DT_ENDPOINT,
1379 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1380};
1381
1382static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1383 .enable = dwc3_gadget_ep0_enable,
1384 .disable = dwc3_gadget_ep0_disable,
1385 .alloc_request = dwc3_gadget_ep_alloc_request,
1386 .free_request = dwc3_gadget_ep_free_request,
1387 .queue = dwc3_gadget_ep0_queue,
1388 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301389 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001390 .set_wedge = dwc3_gadget_ep_set_wedge,
1391};
1392
1393static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1394 .enable = dwc3_gadget_ep_enable,
1395 .disable = dwc3_gadget_ep_disable,
1396 .alloc_request = dwc3_gadget_ep_alloc_request,
1397 .free_request = dwc3_gadget_ep_free_request,
1398 .queue = dwc3_gadget_ep_queue,
1399 .dequeue = dwc3_gadget_ep_dequeue,
1400 .set_halt = dwc3_gadget_ep_set_halt,
1401 .set_wedge = dwc3_gadget_ep_set_wedge,
1402};
1403
1404/* -------------------------------------------------------------------------- */
1405
1406static int dwc3_gadget_get_frame(struct usb_gadget *g)
1407{
1408 struct dwc3 *dwc = gadget_to_dwc(g);
1409 u32 reg;
1410
1411 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1412 return DWC3_DSTS_SOFFN(reg);
1413}
1414
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001415static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001416{
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001418
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001419 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001420 u32 reg;
1421
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 u8 link_state;
1423 u8 speed;
1424
Felipe Balbi72246da2011-08-19 18:10:58 +03001425 /*
1426 * According to the Databook Remote wakeup request should
1427 * be issued only when the device is in early suspend state.
1428 *
1429 * We can check that via USB Link State bits in DSTS register.
1430 */
1431 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1432
1433 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001434 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1435 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001436 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001437 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001438 }
1439
1440 link_state = DWC3_DSTS_USBLNKST(reg);
1441
1442 switch (link_state) {
1443 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1444 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1445 break;
1446 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001447 dwc3_trace(trace_dwc3_gadget,
1448 "can't wakeup from '%s'\n",
1449 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001450 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 }
1452
Felipe Balbi8598bde2012-01-02 18:55:57 +02001453 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1454 if (ret < 0) {
1455 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001456 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001457 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001458
Paul Zimmerman802fde92012-04-27 13:10:52 +03001459 /* Recent versions do this automatically */
1460 if (dwc->revision < DWC3_REVISION_194A) {
1461 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001462 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001463 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1464 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1465 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001466
Paul Zimmerman1d046792012-02-15 18:56:56 -08001467 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001468 timeout = jiffies + msecs_to_jiffies(100);
1469
Paul Zimmerman1d046792012-02-15 18:56:56 -08001470 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1472
1473 /* in HS, means ON */
1474 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1475 break;
1476 }
1477
1478 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1479 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001480 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001481 }
1482
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001483 return 0;
1484}
1485
1486static int dwc3_gadget_wakeup(struct usb_gadget *g)
1487{
1488 struct dwc3 *dwc = gadget_to_dwc(g);
1489 unsigned long flags;
1490 int ret;
1491
1492 spin_lock_irqsave(&dwc->lock, flags);
1493 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 spin_unlock_irqrestore(&dwc->lock, flags);
1495
1496 return ret;
1497}
1498
1499static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1500 int is_selfpowered)
1501{
1502 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001503 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001504
Paul Zimmerman249a4562012-02-24 17:32:16 -08001505 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001506 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001507 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001508
1509 return 0;
1510}
1511
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001512static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001513{
1514 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001515 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001516
Felipe Balbifc8bb912016-05-16 13:14:48 +03001517 if (pm_runtime_suspended(dwc->dev))
1518 return 0;
1519
Felipe Balbi72246da2011-08-19 18:10:58 +03001520 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001521 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001522 if (dwc->revision <= DWC3_REVISION_187A) {
1523 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1524 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1525 }
1526
1527 if (dwc->revision >= DWC3_REVISION_194A)
1528 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1529 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001530
1531 if (dwc->has_hibernation)
1532 reg |= DWC3_DCTL_KEEP_CONNECT;
1533
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001534 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001535 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001537
1538 if (dwc->has_hibernation && !suspend)
1539 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1540
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001541 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001542 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001543
1544 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1545
1546 do {
1547 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1548 if (is_on) {
1549 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1550 break;
1551 } else {
1552 if (reg & DWC3_DSTS_DEVCTRLHLT)
1553 break;
1554 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 timeout--;
1556 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301557 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001558 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001559 } while (1);
1560
Felipe Balbi73815282015-01-27 13:48:14 -06001561 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001562 dwc->gadget_driver
1563 ? dwc->gadget_driver->function : "no-function",
1564 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301565
1566 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001567}
1568
1569static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1570{
1571 struct dwc3 *dwc = gadget_to_dwc(g);
1572 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301573 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001574
1575 is_on = !!is_on;
1576
1577 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001578 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001579 spin_unlock_irqrestore(&dwc->lock, flags);
1580
Pratyush Anand6f17f742012-07-02 10:21:55 +05301581 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001582}
1583
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001584static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1585{
1586 u32 reg;
1587
1588 /* Enable all but Start and End of Frame IRQs */
1589 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1590 DWC3_DEVTEN_EVNTOVERFLOWEN |
1591 DWC3_DEVTEN_CMDCMPLTEN |
1592 DWC3_DEVTEN_ERRTICERREN |
1593 DWC3_DEVTEN_WKUPEVTEN |
1594 DWC3_DEVTEN_ULSTCNGEN |
1595 DWC3_DEVTEN_CONNECTDONEEN |
1596 DWC3_DEVTEN_USBRSTEN |
1597 DWC3_DEVTEN_DISCONNEVTEN);
1598
1599 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1600}
1601
1602static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1603{
1604 /* mask all interrupts */
1605 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1606}
1607
1608static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001609static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001610
Felipe Balbi4e994722016-05-13 14:09:59 +03001611/**
1612 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1613 * dwc: pointer to our context structure
1614 *
1615 * The following looks like complex but it's actually very simple. In order to
1616 * calculate the number of packets we can burst at once on OUT transfers, we're
1617 * gonna use RxFIFO size.
1618 *
1619 * To calculate RxFIFO size we need two numbers:
1620 * MDWIDTH = size, in bits, of the internal memory bus
1621 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1622 *
1623 * Given these two numbers, the formula is simple:
1624 *
1625 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1626 *
1627 * 24 bytes is for 3x SETUP packets
1628 * 16 bytes is a clock domain crossing tolerance
1629 *
1630 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1631 */
1632static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1633{
1634 u32 ram2_depth;
1635 u32 mdwidth;
1636 u32 nump;
1637 u32 reg;
1638
1639 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1640 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1641
1642 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1643 nump = min_t(u32, nump, 16);
1644
1645 /* update NumP */
1646 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1647 reg &= ~DWC3_DCFG_NUMP_MASK;
1648 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1649 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1650}
1651
Felipe Balbid7be2952016-05-04 15:49:37 +03001652static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001653{
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001655 int ret = 0;
1656 u32 reg;
1657
Felipe Balbi72246da2011-08-19 18:10:58 +03001658 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1659 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001660
1661 /**
1662 * WORKAROUND: DWC3 revision < 2.20a have an issue
1663 * which would cause metastability state on Run/Stop
1664 * bit if we try to force the IP to USB2-only mode.
1665 *
1666 * Because of that, we cannot configure the IP to any
1667 * speed other than the SuperSpeed
1668 *
1669 * Refers to:
1670 *
1671 * STAR#9000525659: Clock Domain Crossing on DCTL in
1672 * USB 2.0 Mode
1673 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001674 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001675 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001676 } else {
1677 switch (dwc->maximum_speed) {
1678 case USB_SPEED_LOW:
1679 reg |= DWC3_DSTS_LOWSPEED;
1680 break;
1681 case USB_SPEED_FULL:
1682 reg |= DWC3_DSTS_FULLSPEED1;
1683 break;
1684 case USB_SPEED_HIGH:
1685 reg |= DWC3_DSTS_HIGHSPEED;
1686 break;
John Youn75808622016-02-05 17:09:13 -08001687 case USB_SPEED_SUPER_PLUS:
1688 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1689 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001690 default:
John Youn77966eb2016-02-19 17:31:01 -08001691 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1692 dwc->maximum_speed);
1693 /* fall through */
1694 case USB_SPEED_SUPER:
1695 reg |= DWC3_DCFG_SUPERSPEED;
1696 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001697 }
1698 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001699 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1700
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001701 /*
1702 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1703 * field instead of letting dwc3 itself calculate that automatically.
1704 *
1705 * This way, we maximize the chances that we'll be able to get several
1706 * bursts of data without going through any sort of endpoint throttling.
1707 */
1708 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1709 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1710 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1711
Felipe Balbi4e994722016-05-13 14:09:59 +03001712 dwc3_gadget_setup_nump(dwc);
1713
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 /* Start with SuperSpeed Default */
1715 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1716
1717 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001718 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1719 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001720 if (ret) {
1721 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001722 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001723 }
1724
1725 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001726 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1727 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001728 if (ret) {
1729 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001730 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001731 }
1732
1733 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001734 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001735 dwc3_ep0_out_start(dwc);
1736
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001737 dwc3_gadget_enable_irq(dwc);
1738
Felipe Balbid7be2952016-05-04 15:49:37 +03001739 return 0;
1740
1741err1:
1742 __dwc3_gadget_ep_disable(dwc->eps[0]);
1743
1744err0:
1745 return ret;
1746}
1747
1748static int dwc3_gadget_start(struct usb_gadget *g,
1749 struct usb_gadget_driver *driver)
1750{
1751 struct dwc3 *dwc = gadget_to_dwc(g);
1752 unsigned long flags;
1753 int ret = 0;
1754 int irq;
1755
1756 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1757 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1758 IRQF_SHARED, "dwc3", dwc->ev_buf);
1759 if (ret) {
1760 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1761 irq, ret);
1762 goto err0;
1763 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001764 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001765
1766 spin_lock_irqsave(&dwc->lock, flags);
1767 if (dwc->gadget_driver) {
1768 dev_err(dwc->dev, "%s is already bound to %s\n",
1769 dwc->gadget.name,
1770 dwc->gadget_driver->driver.name);
1771 ret = -EBUSY;
1772 goto err1;
1773 }
1774
1775 dwc->gadget_driver = driver;
1776
Felipe Balbifc8bb912016-05-16 13:14:48 +03001777 if (pm_runtime_active(dwc->dev))
1778 __dwc3_gadget_start(dwc);
1779
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 spin_unlock_irqrestore(&dwc->lock, flags);
1781
1782 return 0;
1783
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001784err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001786 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001787
1788err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001789 return ret;
1790}
1791
Felipe Balbid7be2952016-05-04 15:49:37 +03001792static void __dwc3_gadget_stop(struct dwc3 *dwc)
1793{
1794 dwc3_gadget_disable_irq(dwc);
1795 __dwc3_gadget_ep_disable(dwc->eps[0]);
1796 __dwc3_gadget_ep_disable(dwc->eps[1]);
1797}
1798
Felipe Balbi22835b82014-10-17 12:05:12 -05001799static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001800{
1801 struct dwc3 *dwc = gadget_to_dwc(g);
1802 unsigned long flags;
1803
1804 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001805 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 spin_unlock_irqrestore(&dwc->lock, flags);
1808
Felipe Balbi3f308d12016-05-16 14:17:06 +03001809 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001810
Felipe Balbi72246da2011-08-19 18:10:58 +03001811 return 0;
1812}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001813
Felipe Balbi72246da2011-08-19 18:10:58 +03001814static const struct usb_gadget_ops dwc3_gadget_ops = {
1815 .get_frame = dwc3_gadget_get_frame,
1816 .wakeup = dwc3_gadget_wakeup,
1817 .set_selfpowered = dwc3_gadget_set_selfpowered,
1818 .pullup = dwc3_gadget_pullup,
1819 .udc_start = dwc3_gadget_start,
1820 .udc_stop = dwc3_gadget_stop,
1821};
1822
1823/* -------------------------------------------------------------------------- */
1824
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001825static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1826 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001827{
1828 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001829 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001830
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001831 for (i = 0; i < num; i++) {
1832 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001833
Felipe Balbi72246da2011-08-19 18:10:58 +03001834 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001835 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001837
1838 dep->dwc = dwc;
1839 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001840 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001841 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001842 dwc->eps[epnum] = dep;
1843
1844 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1845 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001846
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001848
Felipe Balbi73815282015-01-27 13:48:14 -06001849 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001850
Felipe Balbi72246da2011-08-19 18:10:58 +03001851 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001852 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301853 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001854 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1855 if (!epnum)
1856 dwc->gadget.ep0 = &dep->endpoint;
1857 } else {
1858 int ret;
1859
Robert Baldygae117e742013-12-13 12:23:38 +01001860 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001861 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1863 list_add_tail(&dep->endpoint.ep_list,
1864 &dwc->gadget.ep_list);
1865
1866 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001867 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001868 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001869 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001870
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001871 if (epnum == 0 || epnum == 1) {
1872 dep->endpoint.caps.type_control = true;
1873 } else {
1874 dep->endpoint.caps.type_iso = true;
1875 dep->endpoint.caps.type_bulk = true;
1876 dep->endpoint.caps.type_int = true;
1877 }
1878
1879 dep->endpoint.caps.dir_in = !!direction;
1880 dep->endpoint.caps.dir_out = !direction;
1881
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001882 INIT_LIST_HEAD(&dep->pending_list);
1883 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001884 }
1885
1886 return 0;
1887}
1888
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001889static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1890{
1891 int ret;
1892
1893 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1894
1895 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1896 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001897 dwc3_trace(trace_dwc3_gadget,
1898 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001899 return ret;
1900 }
1901
1902 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1903 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001904 dwc3_trace(trace_dwc3_gadget,
1905 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001906 return ret;
1907 }
1908
1909 return 0;
1910}
1911
Felipe Balbi72246da2011-08-19 18:10:58 +03001912static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1913{
1914 struct dwc3_ep *dep;
1915 u8 epnum;
1916
1917 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1918 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001919 if (!dep)
1920 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301921 /*
1922 * Physical endpoints 0 and 1 are special; they form the
1923 * bi-directional USB endpoint 0.
1924 *
1925 * For those two physical endpoints, we don't allocate a TRB
1926 * pool nor do we add them the endpoints list. Due to that, we
1927 * shouldn't do these two operations otherwise we would end up
1928 * with all sorts of bugs when removing dwc3.ko.
1929 */
1930 if (epnum != 0 && epnum != 1) {
1931 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001932 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301933 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001934
1935 kfree(dep);
1936 }
1937}
1938
Felipe Balbi72246da2011-08-19 18:10:58 +03001939/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001940
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301941static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1942 struct dwc3_request *req, struct dwc3_trb *trb,
1943 const struct dwc3_event_depevt *event, int status)
1944{
1945 unsigned int count;
1946 unsigned int s_pkt = 0;
1947 unsigned int trb_status;
1948
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001949 trace_dwc3_complete_trb(dep, trb);
1950
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301951 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1952 /*
1953 * We continue despite the error. There is not much we
1954 * can do. If we don't clean it up we loop forever. If
1955 * we skip the TRB then it gets overwritten after a
1956 * while since we use them in a ring buffer. A BUG()
1957 * would help. Lets hope that if this occurs, someone
1958 * fixes the root cause instead of looking away :)
1959 */
1960 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1961 dep->name, trb);
1962 count = trb->size & DWC3_TRB_SIZE_MASK;
1963
1964 if (dep->direction) {
1965 if (count) {
1966 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1967 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001968 dwc3_trace(trace_dwc3_gadget,
1969 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301970 dep->name);
1971 /*
1972 * If missed isoc occurred and there is
1973 * no request queued then issue END
1974 * TRANSFER, so that core generates
1975 * next xfernotready and we will issue
1976 * a fresh START TRANSFER.
1977 * If there are still queued request
1978 * then wait, do not issue either END
1979 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001980 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301981 * giveback.If any future queued request
1982 * is successfully transferred then we
1983 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001984 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301985 */
1986 dep->flags |= DWC3_EP_MISSED_ISOC;
1987 } else {
1988 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1989 dep->name);
1990 status = -ECONNRESET;
1991 }
1992 } else {
1993 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1994 }
1995 } else {
1996 if (count && (event->status & DEPEVT_STATUS_SHORT))
1997 s_pkt = 1;
1998 }
1999
2000 /*
2001 * We assume here we will always receive the entire data block
2002 * which we should receive. Meaning, if we program RX to
2003 * receive 4K but we receive only 2K, we assume that's all we
2004 * should receive and we simply bounce the request back to the
2005 * gadget driver for further processing.
2006 */
2007 req->request.actual += req->request.length - count;
2008 if (s_pkt)
2009 return 1;
2010 if ((event->status & DEPEVT_STATUS_LST) &&
2011 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2012 DWC3_TRB_CTRL_HWO)))
2013 return 1;
2014 if ((event->status & DEPEVT_STATUS_IOC) &&
2015 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2016 return 1;
2017 return 0;
2018}
2019
Felipe Balbi72246da2011-08-19 18:10:58 +03002020static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2021 const struct dwc3_event_depevt *event, int status)
2022{
2023 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002024 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302025 unsigned int slot;
2026 unsigned int i;
2027 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002028
2029 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002030 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002031 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002032 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002033
Ville Syrjäläd115d702015-08-31 19:48:28 +03002034 i = 0;
2035 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002036 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002037 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002038 slot++;
2039 slot %= DWC3_TRB_NUM;
2040 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002041
Ville Syrjäläd115d702015-08-31 19:48:28 +03002042 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2043 event, status);
2044 if (ret)
2045 break;
2046 } while (++i < req->request.num_mapped_sgs);
2047
2048 dwc3_gadget_giveback(dep, req, status);
2049
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302050 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002051 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002052 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002053
Felipe Balbi4cb42212016-05-18 12:37:21 +03002054 /*
2055 * Our endpoint might get disabled by another thread during
2056 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2057 * early on so DWC3_EP_BUSY flag gets cleared
2058 */
2059 if (!dep->endpoint.desc)
2060 return 1;
2061
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302062 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002063 list_empty(&dep->started_list)) {
2064 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302065 /*
2066 * If there is no entry in request list then do
2067 * not issue END TRANSFER now. Just set PENDING
2068 * flag, so that END TRANSFER is issued when an
2069 * entry is added into request list.
2070 */
2071 dep->flags = DWC3_EP_PENDING_REQUEST;
2072 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002073 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302074 dep->flags = DWC3_EP_ENABLED;
2075 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302076 return 1;
2077 }
2078
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002079 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2080 if ((event->status & DEPEVT_STATUS_IOC) &&
2081 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2082 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002083 return 1;
2084}
2085
2086static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002087 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002088{
2089 unsigned status = 0;
2090 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002091 u32 is_xfer_complete;
2092
2093 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002094
2095 if (event->status & DEPEVT_STATUS_BUSERR)
2096 status = -ECONNRESET;
2097
Paul Zimmerman1d046792012-02-15 18:56:56 -08002098 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002099 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002100 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002101 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002102
2103 /*
2104 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2105 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2106 */
2107 if (dwc->revision < DWC3_REVISION_183A) {
2108 u32 reg;
2109 int i;
2110
2111 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002112 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002113
2114 if (!(dep->flags & DWC3_EP_ENABLED))
2115 continue;
2116
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002117 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002118 return;
2119 }
2120
2121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2122 reg |= dwc->u1u2;
2123 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2124
2125 dwc->u1u2 = 0;
2126 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002127
Felipe Balbi4cb42212016-05-18 12:37:21 +03002128 /*
2129 * Our endpoint might get disabled by another thread during
2130 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2131 * early on so DWC3_EP_BUSY flag gets cleared
2132 */
2133 if (!dep->endpoint.desc)
2134 return;
2135
Felipe Balbie6e709b2015-09-28 15:16:56 -05002136 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002137 int ret;
2138
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002139 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002140 if (!ret || ret == -EBUSY)
2141 return;
2142 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002143}
2144
Felipe Balbi72246da2011-08-19 18:10:58 +03002145static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2146 const struct dwc3_event_depevt *event)
2147{
2148 struct dwc3_ep *dep;
2149 u8 epnum = event->endpoint_number;
2150
2151 dep = dwc->eps[epnum];
2152
Felipe Balbi3336abb2012-06-06 09:19:35 +03002153 if (!(dep->flags & DWC3_EP_ENABLED))
2154 return;
2155
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 if (epnum == 0 || epnum == 1) {
2157 dwc3_ep0_interrupt(dwc, event);
2158 return;
2159 }
2160
2161 switch (event->endpoint_event) {
2162 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002163 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002164
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002165 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002166 dwc3_trace(trace_dwc3_gadget,
2167 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002168 dep->name);
2169 return;
2170 }
2171
Jingoo Han029d97f2014-07-04 15:00:51 +09002172 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002173 break;
2174 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002175 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002176 break;
2177 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002178 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002179 dwc3_gadget_start_isoc(dwc, dep, event);
2180 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002181 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002182 int ret;
2183
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002184 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2185
Felipe Balbi73815282015-01-27 13:48:14 -06002186 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002187 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002188 : "Transfer Not Active");
2189
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002190 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191 if (!ret || ret == -EBUSY)
2192 return;
2193
Felipe Balbiec5e7952015-11-16 16:04:13 -06002194 dwc3_trace(trace_dwc3_gadget,
2195 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002196 dep->name);
2197 }
2198
2199 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002200 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002201 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002202 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2203 dep->name);
2204 return;
2205 }
2206
2207 switch (event->status) {
2208 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002209 dwc3_trace(trace_dwc3_gadget,
2210 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002211 event->parameters);
2212
2213 break;
2214 case DEPEVT_STREAMEVT_NOTFOUND:
2215 /* FALLTHROUGH */
2216 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002217 dwc3_trace(trace_dwc3_gadget,
2218 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002219 }
2220 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002221 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002222 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002223 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002224 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002225 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002226 break;
2227 }
2228}
2229
2230static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2231{
2232 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2233 spin_unlock(&dwc->lock);
2234 dwc->gadget_driver->disconnect(&dwc->gadget);
2235 spin_lock(&dwc->lock);
2236 }
2237}
2238
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002239static void dwc3_suspend_gadget(struct dwc3 *dwc)
2240{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002241 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002242 spin_unlock(&dwc->lock);
2243 dwc->gadget_driver->suspend(&dwc->gadget);
2244 spin_lock(&dwc->lock);
2245 }
2246}
2247
2248static void dwc3_resume_gadget(struct dwc3 *dwc)
2249{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002250 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002251 spin_unlock(&dwc->lock);
2252 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002253 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002254 }
2255}
2256
2257static void dwc3_reset_gadget(struct dwc3 *dwc)
2258{
2259 if (!dwc->gadget_driver)
2260 return;
2261
2262 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2263 spin_unlock(&dwc->lock);
2264 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002265 spin_lock(&dwc->lock);
2266 }
2267}
2268
Paul Zimmermanb992e682012-04-27 14:17:35 +03002269static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002270{
2271 struct dwc3_ep *dep;
2272 struct dwc3_gadget_ep_cmd_params params;
2273 u32 cmd;
2274 int ret;
2275
2276 dep = dwc->eps[epnum];
2277
Felipe Balbib4996a82012-06-06 12:04:13 +03002278 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302279 return;
2280
Pratyush Anand57911502012-07-06 15:19:10 +05302281 /*
2282 * NOTICE: We are violating what the Databook says about the
2283 * EndTransfer command. Ideally we would _always_ wait for the
2284 * EndTransfer Command Completion IRQ, but that's causing too
2285 * much trouble synchronizing between us and gadget driver.
2286 *
2287 * We have discussed this with the IP Provider and it was
2288 * suggested to giveback all requests here, but give HW some
2289 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002290 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302291 *
2292 * Note also that a similar handling was tested by Synopsys
2293 * (thanks a lot Paul) and nothing bad has come out of it.
2294 * In short, what we're doing is:
2295 *
2296 * - Issue EndTransfer WITH CMDIOC bit set
2297 * - Wait 100us
2298 */
2299
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302300 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002301 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2302 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002303 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302304 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002305 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302306 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002307 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002308 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302309 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002310}
2311
2312static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2313{
2314 u32 epnum;
2315
2316 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2317 struct dwc3_ep *dep;
2318
2319 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002320 if (!dep)
2321 continue;
2322
Felipe Balbi72246da2011-08-19 18:10:58 +03002323 if (!(dep->flags & DWC3_EP_ENABLED))
2324 continue;
2325
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002326 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002327 }
2328}
2329
2330static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2331{
2332 u32 epnum;
2333
2334 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2335 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002336 int ret;
2337
2338 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002339 if (!dep)
2340 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002341
2342 if (!(dep->flags & DWC3_EP_STALL))
2343 continue;
2344
2345 dep->flags &= ~DWC3_EP_STALL;
2346
John Youn50c763f2016-05-31 17:49:56 -07002347 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002348 WARN_ON_ONCE(ret);
2349 }
2350}
2351
2352static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2353{
Felipe Balbic4430a22012-05-24 10:30:01 +03002354 int reg;
2355
Felipe Balbi72246da2011-08-19 18:10:58 +03002356 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2357 reg &= ~DWC3_DCTL_INITU1ENA;
2358 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2359
2360 reg &= ~DWC3_DCTL_INITU2ENA;
2361 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002362
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 dwc3_disconnect_gadget(dwc);
2364
2365 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002366 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002367 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002368
2369 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002370}
2371
Felipe Balbi72246da2011-08-19 18:10:58 +03002372static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2373{
2374 u32 reg;
2375
Felipe Balbifc8bb912016-05-16 13:14:48 +03002376 dwc->connected = true;
2377
Felipe Balbidf62df52011-10-14 15:11:49 +03002378 /*
2379 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2380 * would cause a missing Disconnect Event if there's a
2381 * pending Setup Packet in the FIFO.
2382 *
2383 * There's no suggested workaround on the official Bug
2384 * report, which states that "unless the driver/application
2385 * is doing any special handling of a disconnect event,
2386 * there is no functional issue".
2387 *
2388 * Unfortunately, it turns out that we _do_ some special
2389 * handling of a disconnect event, namely complete all
2390 * pending transfers, notify gadget driver of the
2391 * disconnection, and so on.
2392 *
2393 * Our suggested workaround is to follow the Disconnect
2394 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002395 * flag. Such flag gets set whenever we have a SETUP_PENDING
2396 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002397 * same endpoint.
2398 *
2399 * Refers to:
2400 *
2401 * STAR#9000466709: RTL: Device : Disconnect event not
2402 * generated if setup packet pending in FIFO
2403 */
2404 if (dwc->revision < DWC3_REVISION_188A) {
2405 if (dwc->setup_packet_pending)
2406 dwc3_gadget_disconnect_interrupt(dwc);
2407 }
2408
Felipe Balbi8e744752014-11-06 14:27:53 +08002409 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002410
2411 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2412 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2413 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002414 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002415
2416 dwc3_stop_active_transfers(dwc);
2417 dwc3_clear_stall_all_ep(dwc);
2418
2419 /* Reset device address to zero */
2420 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2421 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2422 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002423}
2424
2425static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2426{
2427 u32 reg;
2428 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2429
2430 /*
2431 * We change the clock only at SS but I dunno why I would want to do
2432 * this. Maybe it becomes part of the power saving plan.
2433 */
2434
John Younee5cd412016-02-05 17:08:45 -08002435 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2436 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002437 return;
2438
2439 /*
2440 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2441 * each time on Connect Done.
2442 */
2443 if (!usb30_clock)
2444 return;
2445
2446 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2447 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2448 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2449}
2450
Felipe Balbi72246da2011-08-19 18:10:58 +03002451static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2452{
Felipe Balbi72246da2011-08-19 18:10:58 +03002453 struct dwc3_ep *dep;
2454 int ret;
2455 u32 reg;
2456 u8 speed;
2457
Felipe Balbi72246da2011-08-19 18:10:58 +03002458 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2459 speed = reg & DWC3_DSTS_CONNECTSPD;
2460 dwc->speed = speed;
2461
2462 dwc3_update_ram_clk_sel(dwc, speed);
2463
2464 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002465 case DWC3_DCFG_SUPERSPEED_PLUS:
2466 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2467 dwc->gadget.ep0->maxpacket = 512;
2468 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2469 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002470 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002471 /*
2472 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2473 * would cause a missing USB3 Reset event.
2474 *
2475 * In such situations, we should force a USB3 Reset
2476 * event by calling our dwc3_gadget_reset_interrupt()
2477 * routine.
2478 *
2479 * Refers to:
2480 *
2481 * STAR#9000483510: RTL: SS : USB3 reset event may
2482 * not be generated always when the link enters poll
2483 */
2484 if (dwc->revision < DWC3_REVISION_190A)
2485 dwc3_gadget_reset_interrupt(dwc);
2486
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2488 dwc->gadget.ep0->maxpacket = 512;
2489 dwc->gadget.speed = USB_SPEED_SUPER;
2490 break;
2491 case DWC3_DCFG_HIGHSPEED:
2492 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2493 dwc->gadget.ep0->maxpacket = 64;
2494 dwc->gadget.speed = USB_SPEED_HIGH;
2495 break;
2496 case DWC3_DCFG_FULLSPEED2:
2497 case DWC3_DCFG_FULLSPEED1:
2498 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2499 dwc->gadget.ep0->maxpacket = 64;
2500 dwc->gadget.speed = USB_SPEED_FULL;
2501 break;
2502 case DWC3_DCFG_LOWSPEED:
2503 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2504 dwc->gadget.ep0->maxpacket = 8;
2505 dwc->gadget.speed = USB_SPEED_LOW;
2506 break;
2507 }
2508
Pratyush Anand2b758352013-01-14 15:59:31 +05302509 /* Enable USB2 LPM Capability */
2510
John Younee5cd412016-02-05 17:08:45 -08002511 if ((dwc->revision > DWC3_REVISION_194A) &&
2512 (speed != DWC3_DCFG_SUPERSPEED) &&
2513 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302514 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2515 reg |= DWC3_DCFG_LPM_CAP;
2516 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2517
2518 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2519 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2520
Huang Rui460d0982014-10-31 11:11:18 +08002521 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302522
Huang Rui80caf7d2014-10-28 19:54:26 +08002523 /*
2524 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2525 * DCFG.LPMCap is set, core responses with an ACK and the
2526 * BESL value in the LPM token is less than or equal to LPM
2527 * NYET threshold.
2528 */
2529 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2530 && dwc->has_lpm_erratum,
2531 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2532
2533 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2534 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2535
Pratyush Anand2b758352013-01-14 15:59:31 +05302536 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002537 } else {
2538 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2539 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2540 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302541 }
2542
Felipe Balbi72246da2011-08-19 18:10:58 +03002543 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002544 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2545 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002546 if (ret) {
2547 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2548 return;
2549 }
2550
2551 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002552 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2553 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002554 if (ret) {
2555 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2556 return;
2557 }
2558
2559 /*
2560 * Configure PHY via GUSB3PIPECTLn if required.
2561 *
2562 * Update GTXFIFOSIZn
2563 *
2564 * In both cases reset values should be sufficient.
2565 */
2566}
2567
2568static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2569{
Felipe Balbi72246da2011-08-19 18:10:58 +03002570 /*
2571 * TODO take core out of low power mode when that's
2572 * implemented.
2573 */
2574
Jiebing Liad14d4e2014-12-11 13:26:29 +08002575 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2576 spin_unlock(&dwc->lock);
2577 dwc->gadget_driver->resume(&dwc->gadget);
2578 spin_lock(&dwc->lock);
2579 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002580}
2581
2582static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2583 unsigned int evtinfo)
2584{
Felipe Balbifae2b902011-10-14 13:00:30 +03002585 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002586 unsigned int pwropt;
2587
2588 /*
2589 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2590 * Hibernation mode enabled which would show up when device detects
2591 * host-initiated U3 exit.
2592 *
2593 * In that case, device will generate a Link State Change Interrupt
2594 * from U3 to RESUME which is only necessary if Hibernation is
2595 * configured in.
2596 *
2597 * There are no functional changes due to such spurious event and we
2598 * just need to ignore it.
2599 *
2600 * Refers to:
2601 *
2602 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2603 * operational mode
2604 */
2605 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2606 if ((dwc->revision < DWC3_REVISION_250A) &&
2607 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2608 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2609 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002610 dwc3_trace(trace_dwc3_gadget,
2611 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002612 return;
2613 }
2614 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002615
2616 /*
2617 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2618 * on the link partner, the USB session might do multiple entry/exit
2619 * of low power states before a transfer takes place.
2620 *
2621 * Due to this problem, we might experience lower throughput. The
2622 * suggested workaround is to disable DCTL[12:9] bits if we're
2623 * transitioning from U1/U2 to U0 and enable those bits again
2624 * after a transfer completes and there are no pending transfers
2625 * on any of the enabled endpoints.
2626 *
2627 * This is the first half of that workaround.
2628 *
2629 * Refers to:
2630 *
2631 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2632 * core send LGO_Ux entering U0
2633 */
2634 if (dwc->revision < DWC3_REVISION_183A) {
2635 if (next == DWC3_LINK_STATE_U0) {
2636 u32 u1u2;
2637 u32 reg;
2638
2639 switch (dwc->link_state) {
2640 case DWC3_LINK_STATE_U1:
2641 case DWC3_LINK_STATE_U2:
2642 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2643 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2644 | DWC3_DCTL_ACCEPTU2ENA
2645 | DWC3_DCTL_INITU1ENA
2646 | DWC3_DCTL_ACCEPTU1ENA);
2647
2648 if (!dwc->u1u2)
2649 dwc->u1u2 = reg & u1u2;
2650
2651 reg &= ~u1u2;
2652
2653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2654 break;
2655 default:
2656 /* do nothing */
2657 break;
2658 }
2659 }
2660 }
2661
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002662 switch (next) {
2663 case DWC3_LINK_STATE_U1:
2664 if (dwc->speed == USB_SPEED_SUPER)
2665 dwc3_suspend_gadget(dwc);
2666 break;
2667 case DWC3_LINK_STATE_U2:
2668 case DWC3_LINK_STATE_U3:
2669 dwc3_suspend_gadget(dwc);
2670 break;
2671 case DWC3_LINK_STATE_RESUME:
2672 dwc3_resume_gadget(dwc);
2673 break;
2674 default:
2675 /* do nothing */
2676 break;
2677 }
2678
Felipe Balbie57ebc12014-04-22 13:20:12 -05002679 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002680}
2681
Felipe Balbie1dadd32014-02-25 14:47:54 -06002682static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2683 unsigned int evtinfo)
2684{
2685 unsigned int is_ss = evtinfo & BIT(4);
2686
2687 /**
2688 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2689 * have a known issue which can cause USB CV TD.9.23 to fail
2690 * randomly.
2691 *
2692 * Because of this issue, core could generate bogus hibernation
2693 * events which SW needs to ignore.
2694 *
2695 * Refers to:
2696 *
2697 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2698 * Device Fallback from SuperSpeed
2699 */
2700 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2701 return;
2702
2703 /* enter hibernation here */
2704}
2705
Felipe Balbi72246da2011-08-19 18:10:58 +03002706static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2707 const struct dwc3_event_devt *event)
2708{
2709 switch (event->type) {
2710 case DWC3_DEVICE_EVENT_DISCONNECT:
2711 dwc3_gadget_disconnect_interrupt(dwc);
2712 break;
2713 case DWC3_DEVICE_EVENT_RESET:
2714 dwc3_gadget_reset_interrupt(dwc);
2715 break;
2716 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2717 dwc3_gadget_conndone_interrupt(dwc);
2718 break;
2719 case DWC3_DEVICE_EVENT_WAKEUP:
2720 dwc3_gadget_wakeup_interrupt(dwc);
2721 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002722 case DWC3_DEVICE_EVENT_HIBER_REQ:
2723 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2724 "unexpected hibernation event\n"))
2725 break;
2726
2727 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2728 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002729 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2730 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2731 break;
2732 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002733 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002734 break;
2735 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002736 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002737 break;
2738 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002739 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002740 break;
2741 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002742 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002743 break;
2744 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002745 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002746 break;
2747 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002748 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002749 }
2750}
2751
2752static void dwc3_process_event_entry(struct dwc3 *dwc,
2753 const union dwc3_event *event)
2754{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002755 trace_dwc3_event(event->raw);
2756
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 /* Endpoint IRQ, handle it and return early */
2758 if (event->type.is_devspec == 0) {
2759 /* depevt */
2760 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2761 }
2762
2763 switch (event->type.type) {
2764 case DWC3_EVENT_TYPE_DEV:
2765 dwc3_gadget_interrupt(dwc, &event->devt);
2766 break;
2767 /* REVISIT what to do with Carkit and I2C events ? */
2768 default:
2769 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2770 }
2771}
2772
Felipe Balbidea520a2016-03-30 09:39:34 +03002773static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002774{
Felipe Balbidea520a2016-03-30 09:39:34 +03002775 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002776 irqreturn_t ret = IRQ_NONE;
2777 int left;
2778 u32 reg;
2779
Felipe Balbif42f2442013-06-12 21:25:08 +03002780 left = evt->count;
2781
2782 if (!(evt->flags & DWC3_EVENT_PENDING))
2783 return IRQ_NONE;
2784
2785 while (left > 0) {
2786 union dwc3_event event;
2787
2788 event.raw = *(u32 *) (evt->buf + evt->lpos);
2789
2790 dwc3_process_event_entry(dwc, &event);
2791
2792 /*
2793 * FIXME we wrap around correctly to the next entry as
2794 * almost all entries are 4 bytes in size. There is one
2795 * entry which has 12 bytes which is a regular entry
2796 * followed by 8 bytes data. ATM I don't know how
2797 * things are organized if we get next to the a
2798 * boundary so I worry about that once we try to handle
2799 * that.
2800 */
2801 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2802 left -= 4;
2803
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002804 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002805 }
2806
2807 evt->count = 0;
2808 evt->flags &= ~DWC3_EVENT_PENDING;
2809 ret = IRQ_HANDLED;
2810
2811 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002812 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002813 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002814 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002815
2816 return ret;
2817}
2818
Felipe Balbidea520a2016-03-30 09:39:34 +03002819static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002820{
Felipe Balbidea520a2016-03-30 09:39:34 +03002821 struct dwc3_event_buffer *evt = _evt;
2822 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002823 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002824 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002825
Felipe Balbie5f68b42015-10-12 13:25:44 -05002826 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002827 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002828 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002829
2830 return ret;
2831}
2832
Felipe Balbidea520a2016-03-30 09:39:34 +03002833static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002834{
Felipe Balbidea520a2016-03-30 09:39:34 +03002835 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002837 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002838
Felipe Balbifc8bb912016-05-16 13:14:48 +03002839 if (pm_runtime_suspended(dwc->dev)) {
2840 pm_runtime_get(dwc->dev);
2841 disable_irq_nosync(dwc->irq_gadget);
2842 dwc->pending_events = true;
2843 return IRQ_HANDLED;
2844 }
2845
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002846 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002847 count &= DWC3_GEVNTCOUNT_MASK;
2848 if (!count)
2849 return IRQ_NONE;
2850
Felipe Balbib15a7622011-06-30 16:57:15 +03002851 evt->count = count;
2852 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002853
Felipe Balbie8adfc32013-06-12 21:11:14 +03002854 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002855 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002856 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002857 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002858
Felipe Balbib15a7622011-06-30 16:57:15 +03002859 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002860}
2861
Felipe Balbidea520a2016-03-30 09:39:34 +03002862static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002863{
Felipe Balbidea520a2016-03-30 09:39:34 +03002864 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002865
Felipe Balbidea520a2016-03-30 09:39:34 +03002866 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002867}
2868
2869/**
2870 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002871 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002872 *
2873 * Returns 0 on success otherwise negative errno.
2874 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002875int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002876{
Felipe Balbi72246da2011-08-19 18:10:58 +03002877 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002878
2879 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2880 &dwc->ctrl_req_addr, GFP_KERNEL);
2881 if (!dwc->ctrl_req) {
2882 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2883 ret = -ENOMEM;
2884 goto err0;
2885 }
2886
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302887 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002888 &dwc->ep0_trb_addr, GFP_KERNEL);
2889 if (!dwc->ep0_trb) {
2890 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2891 ret = -ENOMEM;
2892 goto err1;
2893 }
2894
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002895 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002896 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002897 ret = -ENOMEM;
2898 goto err2;
2899 }
2900
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002901 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002902 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2903 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002904 if (!dwc->ep0_bounce) {
2905 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2906 ret = -ENOMEM;
2907 goto err3;
2908 }
2909
Felipe Balbi04c03d12015-12-02 10:06:45 -06002910 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2911 if (!dwc->zlp_buf) {
2912 ret = -ENOMEM;
2913 goto err4;
2914 }
2915
Felipe Balbi72246da2011-08-19 18:10:58 +03002916 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002917 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002918 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002919 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002920 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002921
2922 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002923 * FIXME We might be setting max_speed to <SUPER, however versions
2924 * <2.20a of dwc3 have an issue with metastability (documented
2925 * elsewhere in this driver) which tells us we can't set max speed to
2926 * anything lower than SUPER.
2927 *
2928 * Because gadget.max_speed is only used by composite.c and function
2929 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2930 * to happen so we avoid sending SuperSpeed Capability descriptor
2931 * together with our BOS descriptor as that could confuse host into
2932 * thinking we can handle super speed.
2933 *
2934 * Note that, in fact, we won't even support GetBOS requests when speed
2935 * is less than super speed because we don't have means, yet, to tell
2936 * composite.c that we are USB 2.0 + LPM ECN.
2937 */
2938 if (dwc->revision < DWC3_REVISION_220A)
2939 dwc3_trace(trace_dwc3_gadget,
2940 "Changing max_speed on rev %08x\n",
2941 dwc->revision);
2942
2943 dwc->gadget.max_speed = dwc->maximum_speed;
2944
2945 /*
David Cohena4b9d942013-12-09 15:55:38 -08002946 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2947 * on ep out.
2948 */
2949 dwc->gadget.quirk_ep_out_aligned_size = true;
2950
2951 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002952 * REVISIT: Here we should clear all pending IRQs to be
2953 * sure we're starting from a well known location.
2954 */
2955
2956 ret = dwc3_gadget_init_endpoints(dwc);
2957 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002958 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002959
Felipe Balbi72246da2011-08-19 18:10:58 +03002960 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2961 if (ret) {
2962 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002963 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002964 }
2965
2966 return 0;
2967
Felipe Balbi04c03d12015-12-02 10:06:45 -06002968err5:
2969 kfree(dwc->zlp_buf);
2970
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002971err4:
David Cohene1f80462013-09-11 17:42:47 -07002972 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002973 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2974 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002975
Felipe Balbi72246da2011-08-19 18:10:58 +03002976err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002977 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002978
2979err2:
2980 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2981 dwc->ep0_trb, dwc->ep0_trb_addr);
2982
2983err1:
2984 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2985 dwc->ctrl_req, dwc->ctrl_req_addr);
2986
2987err0:
2988 return ret;
2989}
2990
Felipe Balbi7415f172012-04-30 14:56:33 +03002991/* -------------------------------------------------------------------------- */
2992
Felipe Balbi72246da2011-08-19 18:10:58 +03002993void dwc3_gadget_exit(struct dwc3 *dwc)
2994{
Felipe Balbi72246da2011-08-19 18:10:58 +03002995 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002996
Felipe Balbi72246da2011-08-19 18:10:58 +03002997 dwc3_gadget_free_endpoints(dwc);
2998
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002999 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3000 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003001
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003002 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003003 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003004
3005 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3006 dwc->ep0_trb, dwc->ep0_trb_addr);
3007
3008 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3009 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003010}
Felipe Balbi7415f172012-04-30 14:56:33 +03003011
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003012int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003013{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003014 int ret;
3015
Roger Quadros9772b472016-04-12 11:33:29 +03003016 if (!dwc->gadget_driver)
3017 return 0;
3018
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003019 ret = dwc3_gadget_run_stop(dwc, false, false);
3020 if (ret < 0)
3021 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003022
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003023 dwc3_disconnect_gadget(dwc);
3024 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003025
3026 return 0;
3027}
3028
3029int dwc3_gadget_resume(struct dwc3 *dwc)
3030{
Felipe Balbi7415f172012-04-30 14:56:33 +03003031 int ret;
3032
Roger Quadros9772b472016-04-12 11:33:29 +03003033 if (!dwc->gadget_driver)
3034 return 0;
3035
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003036 ret = __dwc3_gadget_start(dwc);
3037 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003038 goto err0;
3039
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003040 ret = dwc3_gadget_run_stop(dwc, true, false);
3041 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003042 goto err1;
3043
Felipe Balbi7415f172012-04-30 14:56:33 +03003044 return 0;
3045
3046err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003047 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003048
3049err0:
3050 return ret;
3051}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003052
3053void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3054{
3055 if (dwc->pending_events) {
3056 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3057 dwc->pending_events = false;
3058 enable_irq(dwc->irq_gadget);
3059 }
3060}