blob: 8b932033d29b631d274345cb0eeb972b47664312 [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 Balbi74674cb2016-04-13 16:44:39 +03001848 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001849
Felipe Balbi73815282015-01-27 13:48:14 -06001850 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001851
Felipe Balbi72246da2011-08-19 18:10:58 +03001852 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001853 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301854 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001855 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1856 if (!epnum)
1857 dwc->gadget.ep0 = &dep->endpoint;
1858 } else {
1859 int ret;
1860
Robert Baldygae117e742013-12-13 12:23:38 +01001861 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001862 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1864 list_add_tail(&dep->endpoint.ep_list,
1865 &dwc->gadget.ep_list);
1866
1867 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001868 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001869 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001870 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001871
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001872 if (epnum == 0 || epnum == 1) {
1873 dep->endpoint.caps.type_control = true;
1874 } else {
1875 dep->endpoint.caps.type_iso = true;
1876 dep->endpoint.caps.type_bulk = true;
1877 dep->endpoint.caps.type_int = true;
1878 }
1879
1880 dep->endpoint.caps.dir_in = !!direction;
1881 dep->endpoint.caps.dir_out = !direction;
1882
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001883 INIT_LIST_HEAD(&dep->pending_list);
1884 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 }
1886
1887 return 0;
1888}
1889
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001890static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1891{
1892 int ret;
1893
1894 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1895
1896 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1897 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001898 dwc3_trace(trace_dwc3_gadget,
1899 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001900 return ret;
1901 }
1902
1903 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1904 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001905 dwc3_trace(trace_dwc3_gadget,
1906 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001907 return ret;
1908 }
1909
1910 return 0;
1911}
1912
Felipe Balbi72246da2011-08-19 18:10:58 +03001913static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1914{
1915 struct dwc3_ep *dep;
1916 u8 epnum;
1917
1918 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1919 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001920 if (!dep)
1921 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301922 /*
1923 * Physical endpoints 0 and 1 are special; they form the
1924 * bi-directional USB endpoint 0.
1925 *
1926 * For those two physical endpoints, we don't allocate a TRB
1927 * pool nor do we add them the endpoints list. Due to that, we
1928 * shouldn't do these two operations otherwise we would end up
1929 * with all sorts of bugs when removing dwc3.ko.
1930 */
1931 if (epnum != 0 && epnum != 1) {
1932 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001933 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301934 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001935
1936 kfree(dep);
1937 }
1938}
1939
Felipe Balbi72246da2011-08-19 18:10:58 +03001940/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001941
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301942static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1943 struct dwc3_request *req, struct dwc3_trb *trb,
1944 const struct dwc3_event_depevt *event, int status)
1945{
1946 unsigned int count;
1947 unsigned int s_pkt = 0;
1948 unsigned int trb_status;
1949
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001950 trace_dwc3_complete_trb(dep, trb);
1951
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301952 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1953 /*
1954 * We continue despite the error. There is not much we
1955 * can do. If we don't clean it up we loop forever. If
1956 * we skip the TRB then it gets overwritten after a
1957 * while since we use them in a ring buffer. A BUG()
1958 * would help. Lets hope that if this occurs, someone
1959 * fixes the root cause instead of looking away :)
1960 */
1961 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1962 dep->name, trb);
1963 count = trb->size & DWC3_TRB_SIZE_MASK;
1964
1965 if (dep->direction) {
1966 if (count) {
1967 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1968 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001969 dwc3_trace(trace_dwc3_gadget,
1970 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301971 dep->name);
1972 /*
1973 * If missed isoc occurred and there is
1974 * no request queued then issue END
1975 * TRANSFER, so that core generates
1976 * next xfernotready and we will issue
1977 * a fresh START TRANSFER.
1978 * If there are still queued request
1979 * then wait, do not issue either END
1980 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001981 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301982 * giveback.If any future queued request
1983 * is successfully transferred then we
1984 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001985 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301986 */
1987 dep->flags |= DWC3_EP_MISSED_ISOC;
1988 } else {
1989 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1990 dep->name);
1991 status = -ECONNRESET;
1992 }
1993 } else {
1994 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1995 }
1996 } else {
1997 if (count && (event->status & DEPEVT_STATUS_SHORT))
1998 s_pkt = 1;
1999 }
2000
2001 /*
2002 * We assume here we will always receive the entire data block
2003 * which we should receive. Meaning, if we program RX to
2004 * receive 4K but we receive only 2K, we assume that's all we
2005 * should receive and we simply bounce the request back to the
2006 * gadget driver for further processing.
2007 */
2008 req->request.actual += req->request.length - count;
2009 if (s_pkt)
2010 return 1;
2011 if ((event->status & DEPEVT_STATUS_LST) &&
2012 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2013 DWC3_TRB_CTRL_HWO)))
2014 return 1;
2015 if ((event->status & DEPEVT_STATUS_IOC) &&
2016 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2017 return 1;
2018 return 0;
2019}
2020
Felipe Balbi72246da2011-08-19 18:10:58 +03002021static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2022 const struct dwc3_event_depevt *event, int status)
2023{
2024 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002025 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302026 unsigned int slot;
2027 unsigned int i;
2028 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002029
2030 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002031 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002032 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002033 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002034
Ville Syrjäläd115d702015-08-31 19:48:28 +03002035 i = 0;
2036 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002037 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002038 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002039 slot++;
2040 slot %= DWC3_TRB_NUM;
2041 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002042
Ville Syrjäläd115d702015-08-31 19:48:28 +03002043 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2044 event, status);
2045 if (ret)
2046 break;
2047 } while (++i < req->request.num_mapped_sgs);
2048
2049 dwc3_gadget_giveback(dep, req, status);
2050
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302051 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002052 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002053 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002054
Felipe Balbi4cb42212016-05-18 12:37:21 +03002055 /*
2056 * Our endpoint might get disabled by another thread during
2057 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2058 * early on so DWC3_EP_BUSY flag gets cleared
2059 */
2060 if (!dep->endpoint.desc)
2061 return 1;
2062
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302063 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002064 list_empty(&dep->started_list)) {
2065 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302066 /*
2067 * If there is no entry in request list then do
2068 * not issue END TRANSFER now. Just set PENDING
2069 * flag, so that END TRANSFER is issued when an
2070 * entry is added into request list.
2071 */
2072 dep->flags = DWC3_EP_PENDING_REQUEST;
2073 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002074 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302075 dep->flags = DWC3_EP_ENABLED;
2076 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302077 return 1;
2078 }
2079
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002080 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2081 if ((event->status & DEPEVT_STATUS_IOC) &&
2082 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2083 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002084 return 1;
2085}
2086
2087static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002088 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002089{
2090 unsigned status = 0;
2091 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002092 u32 is_xfer_complete;
2093
2094 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002095
2096 if (event->status & DEPEVT_STATUS_BUSERR)
2097 status = -ECONNRESET;
2098
Paul Zimmerman1d046792012-02-15 18:56:56 -08002099 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002100 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002101 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002102 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002103
2104 /*
2105 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2106 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2107 */
2108 if (dwc->revision < DWC3_REVISION_183A) {
2109 u32 reg;
2110 int i;
2111
2112 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002113 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002114
2115 if (!(dep->flags & DWC3_EP_ENABLED))
2116 continue;
2117
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002118 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002119 return;
2120 }
2121
2122 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2123 reg |= dwc->u1u2;
2124 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2125
2126 dwc->u1u2 = 0;
2127 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002128
Felipe Balbi4cb42212016-05-18 12:37:21 +03002129 /*
2130 * Our endpoint might get disabled by another thread during
2131 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2132 * early on so DWC3_EP_BUSY flag gets cleared
2133 */
2134 if (!dep->endpoint.desc)
2135 return;
2136
Felipe Balbie6e709b2015-09-28 15:16:56 -05002137 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002138 int ret;
2139
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002140 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002141 if (!ret || ret == -EBUSY)
2142 return;
2143 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002144}
2145
Felipe Balbi72246da2011-08-19 18:10:58 +03002146static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2147 const struct dwc3_event_depevt *event)
2148{
2149 struct dwc3_ep *dep;
2150 u8 epnum = event->endpoint_number;
2151
2152 dep = dwc->eps[epnum];
2153
Felipe Balbi3336abb2012-06-06 09:19:35 +03002154 if (!(dep->flags & DWC3_EP_ENABLED))
2155 return;
2156
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 if (epnum == 0 || epnum == 1) {
2158 dwc3_ep0_interrupt(dwc, event);
2159 return;
2160 }
2161
2162 switch (event->endpoint_event) {
2163 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002164 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002165
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002166 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002167 dwc3_trace(trace_dwc3_gadget,
2168 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002169 dep->name);
2170 return;
2171 }
2172
Jingoo Han029d97f2014-07-04 15:00:51 +09002173 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002174 break;
2175 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002176 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002177 break;
2178 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002179 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002180 dwc3_gadget_start_isoc(dwc, dep, event);
2181 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002182 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002183 int ret;
2184
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002185 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2186
Felipe Balbi73815282015-01-27 13:48:14 -06002187 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002188 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 : "Transfer Not Active");
2190
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002191 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002192 if (!ret || ret == -EBUSY)
2193 return;
2194
Felipe Balbiec5e7952015-11-16 16:04:13 -06002195 dwc3_trace(trace_dwc3_gadget,
2196 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 dep->name);
2198 }
2199
2200 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002201 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002202 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002203 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2204 dep->name);
2205 return;
2206 }
2207
2208 switch (event->status) {
2209 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002210 dwc3_trace(trace_dwc3_gadget,
2211 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002212 event->parameters);
2213
2214 break;
2215 case DEPEVT_STREAMEVT_NOTFOUND:
2216 /* FALLTHROUGH */
2217 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002218 dwc3_trace(trace_dwc3_gadget,
2219 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002220 }
2221 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002222 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002223 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002224 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002225 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002226 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 break;
2228 }
2229}
2230
2231static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2232{
2233 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2234 spin_unlock(&dwc->lock);
2235 dwc->gadget_driver->disconnect(&dwc->gadget);
2236 spin_lock(&dwc->lock);
2237 }
2238}
2239
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002240static void dwc3_suspend_gadget(struct dwc3 *dwc)
2241{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002242 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002243 spin_unlock(&dwc->lock);
2244 dwc->gadget_driver->suspend(&dwc->gadget);
2245 spin_lock(&dwc->lock);
2246 }
2247}
2248
2249static void dwc3_resume_gadget(struct dwc3 *dwc)
2250{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002251 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002252 spin_unlock(&dwc->lock);
2253 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002254 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002255 }
2256}
2257
2258static void dwc3_reset_gadget(struct dwc3 *dwc)
2259{
2260 if (!dwc->gadget_driver)
2261 return;
2262
2263 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2264 spin_unlock(&dwc->lock);
2265 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002266 spin_lock(&dwc->lock);
2267 }
2268}
2269
Paul Zimmermanb992e682012-04-27 14:17:35 +03002270static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002271{
2272 struct dwc3_ep *dep;
2273 struct dwc3_gadget_ep_cmd_params params;
2274 u32 cmd;
2275 int ret;
2276
2277 dep = dwc->eps[epnum];
2278
Felipe Balbib4996a82012-06-06 12:04:13 +03002279 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302280 return;
2281
Pratyush Anand57911502012-07-06 15:19:10 +05302282 /*
2283 * NOTICE: We are violating what the Databook says about the
2284 * EndTransfer command. Ideally we would _always_ wait for the
2285 * EndTransfer Command Completion IRQ, but that's causing too
2286 * much trouble synchronizing between us and gadget driver.
2287 *
2288 * We have discussed this with the IP Provider and it was
2289 * suggested to giveback all requests here, but give HW some
2290 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002291 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302292 *
2293 * Note also that a similar handling was tested by Synopsys
2294 * (thanks a lot Paul) and nothing bad has come out of it.
2295 * In short, what we're doing is:
2296 *
2297 * - Issue EndTransfer WITH CMDIOC bit set
2298 * - Wait 100us
2299 */
2300
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302301 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002302 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2303 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002304 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302305 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002306 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302307 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002308 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002309 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302310 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002311}
2312
2313static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2314{
2315 u32 epnum;
2316
2317 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2318 struct dwc3_ep *dep;
2319
2320 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002321 if (!dep)
2322 continue;
2323
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 if (!(dep->flags & DWC3_EP_ENABLED))
2325 continue;
2326
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002327 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002328 }
2329}
2330
2331static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2332{
2333 u32 epnum;
2334
2335 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2336 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002337 int ret;
2338
2339 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002340 if (!dep)
2341 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002342
2343 if (!(dep->flags & DWC3_EP_STALL))
2344 continue;
2345
2346 dep->flags &= ~DWC3_EP_STALL;
2347
John Youn50c763f2016-05-31 17:49:56 -07002348 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002349 WARN_ON_ONCE(ret);
2350 }
2351}
2352
2353static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2354{
Felipe Balbic4430a22012-05-24 10:30:01 +03002355 int reg;
2356
Felipe Balbi72246da2011-08-19 18:10:58 +03002357 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2358 reg &= ~DWC3_DCTL_INITU1ENA;
2359 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2360
2361 reg &= ~DWC3_DCTL_INITU2ENA;
2362 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002363
Felipe Balbi72246da2011-08-19 18:10:58 +03002364 dwc3_disconnect_gadget(dwc);
2365
2366 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002367 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002368 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002369
2370 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002371}
2372
Felipe Balbi72246da2011-08-19 18:10:58 +03002373static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2374{
2375 u32 reg;
2376
Felipe Balbifc8bb912016-05-16 13:14:48 +03002377 dwc->connected = true;
2378
Felipe Balbidf62df52011-10-14 15:11:49 +03002379 /*
2380 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2381 * would cause a missing Disconnect Event if there's a
2382 * pending Setup Packet in the FIFO.
2383 *
2384 * There's no suggested workaround on the official Bug
2385 * report, which states that "unless the driver/application
2386 * is doing any special handling of a disconnect event,
2387 * there is no functional issue".
2388 *
2389 * Unfortunately, it turns out that we _do_ some special
2390 * handling of a disconnect event, namely complete all
2391 * pending transfers, notify gadget driver of the
2392 * disconnection, and so on.
2393 *
2394 * Our suggested workaround is to follow the Disconnect
2395 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002396 * flag. Such flag gets set whenever we have a SETUP_PENDING
2397 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002398 * same endpoint.
2399 *
2400 * Refers to:
2401 *
2402 * STAR#9000466709: RTL: Device : Disconnect event not
2403 * generated if setup packet pending in FIFO
2404 */
2405 if (dwc->revision < DWC3_REVISION_188A) {
2406 if (dwc->setup_packet_pending)
2407 dwc3_gadget_disconnect_interrupt(dwc);
2408 }
2409
Felipe Balbi8e744752014-11-06 14:27:53 +08002410 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002411
2412 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2413 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2414 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002415 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002416
2417 dwc3_stop_active_transfers(dwc);
2418 dwc3_clear_stall_all_ep(dwc);
2419
2420 /* Reset device address to zero */
2421 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2422 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2423 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002424}
2425
2426static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2427{
2428 u32 reg;
2429 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2430
2431 /*
2432 * We change the clock only at SS but I dunno why I would want to do
2433 * this. Maybe it becomes part of the power saving plan.
2434 */
2435
John Younee5cd412016-02-05 17:08:45 -08002436 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2437 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 return;
2439
2440 /*
2441 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2442 * each time on Connect Done.
2443 */
2444 if (!usb30_clock)
2445 return;
2446
2447 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2448 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2449 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2450}
2451
Felipe Balbi72246da2011-08-19 18:10:58 +03002452static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2453{
Felipe Balbi72246da2011-08-19 18:10:58 +03002454 struct dwc3_ep *dep;
2455 int ret;
2456 u32 reg;
2457 u8 speed;
2458
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2460 speed = reg & DWC3_DSTS_CONNECTSPD;
2461 dwc->speed = speed;
2462
2463 dwc3_update_ram_clk_sel(dwc, speed);
2464
2465 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002466 case DWC3_DCFG_SUPERSPEED_PLUS:
2467 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2468 dwc->gadget.ep0->maxpacket = 512;
2469 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2470 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002471 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002472 /*
2473 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2474 * would cause a missing USB3 Reset event.
2475 *
2476 * In such situations, we should force a USB3 Reset
2477 * event by calling our dwc3_gadget_reset_interrupt()
2478 * routine.
2479 *
2480 * Refers to:
2481 *
2482 * STAR#9000483510: RTL: SS : USB3 reset event may
2483 * not be generated always when the link enters poll
2484 */
2485 if (dwc->revision < DWC3_REVISION_190A)
2486 dwc3_gadget_reset_interrupt(dwc);
2487
Felipe Balbi72246da2011-08-19 18:10:58 +03002488 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2489 dwc->gadget.ep0->maxpacket = 512;
2490 dwc->gadget.speed = USB_SPEED_SUPER;
2491 break;
2492 case DWC3_DCFG_HIGHSPEED:
2493 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2494 dwc->gadget.ep0->maxpacket = 64;
2495 dwc->gadget.speed = USB_SPEED_HIGH;
2496 break;
2497 case DWC3_DCFG_FULLSPEED2:
2498 case DWC3_DCFG_FULLSPEED1:
2499 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2500 dwc->gadget.ep0->maxpacket = 64;
2501 dwc->gadget.speed = USB_SPEED_FULL;
2502 break;
2503 case DWC3_DCFG_LOWSPEED:
2504 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2505 dwc->gadget.ep0->maxpacket = 8;
2506 dwc->gadget.speed = USB_SPEED_LOW;
2507 break;
2508 }
2509
Pratyush Anand2b758352013-01-14 15:59:31 +05302510 /* Enable USB2 LPM Capability */
2511
John Younee5cd412016-02-05 17:08:45 -08002512 if ((dwc->revision > DWC3_REVISION_194A) &&
2513 (speed != DWC3_DCFG_SUPERSPEED) &&
2514 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302515 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2516 reg |= DWC3_DCFG_LPM_CAP;
2517 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2518
2519 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2520 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2521
Huang Rui460d0982014-10-31 11:11:18 +08002522 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302523
Huang Rui80caf7d2014-10-28 19:54:26 +08002524 /*
2525 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2526 * DCFG.LPMCap is set, core responses with an ACK and the
2527 * BESL value in the LPM token is less than or equal to LPM
2528 * NYET threshold.
2529 */
2530 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2531 && dwc->has_lpm_erratum,
2532 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2533
2534 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2535 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2536
Pratyush Anand2b758352013-01-14 15:59:31 +05302537 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002538 } else {
2539 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2540 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2541 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302542 }
2543
Felipe Balbi72246da2011-08-19 18:10:58 +03002544 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002545 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2546 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002547 if (ret) {
2548 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2549 return;
2550 }
2551
2552 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002553 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2554 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002555 if (ret) {
2556 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2557 return;
2558 }
2559
2560 /*
2561 * Configure PHY via GUSB3PIPECTLn if required.
2562 *
2563 * Update GTXFIFOSIZn
2564 *
2565 * In both cases reset values should be sufficient.
2566 */
2567}
2568
2569static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2570{
Felipe Balbi72246da2011-08-19 18:10:58 +03002571 /*
2572 * TODO take core out of low power mode when that's
2573 * implemented.
2574 */
2575
Jiebing Liad14d4e2014-12-11 13:26:29 +08002576 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2577 spin_unlock(&dwc->lock);
2578 dwc->gadget_driver->resume(&dwc->gadget);
2579 spin_lock(&dwc->lock);
2580 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002581}
2582
2583static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2584 unsigned int evtinfo)
2585{
Felipe Balbifae2b902011-10-14 13:00:30 +03002586 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002587 unsigned int pwropt;
2588
2589 /*
2590 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2591 * Hibernation mode enabled which would show up when device detects
2592 * host-initiated U3 exit.
2593 *
2594 * In that case, device will generate a Link State Change Interrupt
2595 * from U3 to RESUME which is only necessary if Hibernation is
2596 * configured in.
2597 *
2598 * There are no functional changes due to such spurious event and we
2599 * just need to ignore it.
2600 *
2601 * Refers to:
2602 *
2603 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2604 * operational mode
2605 */
2606 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2607 if ((dwc->revision < DWC3_REVISION_250A) &&
2608 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2609 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2610 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002611 dwc3_trace(trace_dwc3_gadget,
2612 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002613 return;
2614 }
2615 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002616
2617 /*
2618 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2619 * on the link partner, the USB session might do multiple entry/exit
2620 * of low power states before a transfer takes place.
2621 *
2622 * Due to this problem, we might experience lower throughput. The
2623 * suggested workaround is to disable DCTL[12:9] bits if we're
2624 * transitioning from U1/U2 to U0 and enable those bits again
2625 * after a transfer completes and there are no pending transfers
2626 * on any of the enabled endpoints.
2627 *
2628 * This is the first half of that workaround.
2629 *
2630 * Refers to:
2631 *
2632 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2633 * core send LGO_Ux entering U0
2634 */
2635 if (dwc->revision < DWC3_REVISION_183A) {
2636 if (next == DWC3_LINK_STATE_U0) {
2637 u32 u1u2;
2638 u32 reg;
2639
2640 switch (dwc->link_state) {
2641 case DWC3_LINK_STATE_U1:
2642 case DWC3_LINK_STATE_U2:
2643 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2644 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2645 | DWC3_DCTL_ACCEPTU2ENA
2646 | DWC3_DCTL_INITU1ENA
2647 | DWC3_DCTL_ACCEPTU1ENA);
2648
2649 if (!dwc->u1u2)
2650 dwc->u1u2 = reg & u1u2;
2651
2652 reg &= ~u1u2;
2653
2654 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2655 break;
2656 default:
2657 /* do nothing */
2658 break;
2659 }
2660 }
2661 }
2662
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002663 switch (next) {
2664 case DWC3_LINK_STATE_U1:
2665 if (dwc->speed == USB_SPEED_SUPER)
2666 dwc3_suspend_gadget(dwc);
2667 break;
2668 case DWC3_LINK_STATE_U2:
2669 case DWC3_LINK_STATE_U3:
2670 dwc3_suspend_gadget(dwc);
2671 break;
2672 case DWC3_LINK_STATE_RESUME:
2673 dwc3_resume_gadget(dwc);
2674 break;
2675 default:
2676 /* do nothing */
2677 break;
2678 }
2679
Felipe Balbie57ebc12014-04-22 13:20:12 -05002680 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002681}
2682
Felipe Balbie1dadd32014-02-25 14:47:54 -06002683static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2684 unsigned int evtinfo)
2685{
2686 unsigned int is_ss = evtinfo & BIT(4);
2687
2688 /**
2689 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2690 * have a known issue which can cause USB CV TD.9.23 to fail
2691 * randomly.
2692 *
2693 * Because of this issue, core could generate bogus hibernation
2694 * events which SW needs to ignore.
2695 *
2696 * Refers to:
2697 *
2698 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2699 * Device Fallback from SuperSpeed
2700 */
2701 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2702 return;
2703
2704 /* enter hibernation here */
2705}
2706
Felipe Balbi72246da2011-08-19 18:10:58 +03002707static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2708 const struct dwc3_event_devt *event)
2709{
2710 switch (event->type) {
2711 case DWC3_DEVICE_EVENT_DISCONNECT:
2712 dwc3_gadget_disconnect_interrupt(dwc);
2713 break;
2714 case DWC3_DEVICE_EVENT_RESET:
2715 dwc3_gadget_reset_interrupt(dwc);
2716 break;
2717 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2718 dwc3_gadget_conndone_interrupt(dwc);
2719 break;
2720 case DWC3_DEVICE_EVENT_WAKEUP:
2721 dwc3_gadget_wakeup_interrupt(dwc);
2722 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002723 case DWC3_DEVICE_EVENT_HIBER_REQ:
2724 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2725 "unexpected hibernation event\n"))
2726 break;
2727
2728 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2729 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2731 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2732 break;
2733 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002734 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002735 break;
2736 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002737 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002738 break;
2739 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002740 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002741 break;
2742 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002743 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002744 break;
2745 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002746 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002747 break;
2748 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002749 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002750 }
2751}
2752
2753static void dwc3_process_event_entry(struct dwc3 *dwc,
2754 const union dwc3_event *event)
2755{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002756 trace_dwc3_event(event->raw);
2757
Felipe Balbi72246da2011-08-19 18:10:58 +03002758 /* Endpoint IRQ, handle it and return early */
2759 if (event->type.is_devspec == 0) {
2760 /* depevt */
2761 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2762 }
2763
2764 switch (event->type.type) {
2765 case DWC3_EVENT_TYPE_DEV:
2766 dwc3_gadget_interrupt(dwc, &event->devt);
2767 break;
2768 /* REVISIT what to do with Carkit and I2C events ? */
2769 default:
2770 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2771 }
2772}
2773
Felipe Balbidea520a2016-03-30 09:39:34 +03002774static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002775{
Felipe Balbidea520a2016-03-30 09:39:34 +03002776 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002777 irqreturn_t ret = IRQ_NONE;
2778 int left;
2779 u32 reg;
2780
Felipe Balbif42f2442013-06-12 21:25:08 +03002781 left = evt->count;
2782
2783 if (!(evt->flags & DWC3_EVENT_PENDING))
2784 return IRQ_NONE;
2785
2786 while (left > 0) {
2787 union dwc3_event event;
2788
2789 event.raw = *(u32 *) (evt->buf + evt->lpos);
2790
2791 dwc3_process_event_entry(dwc, &event);
2792
2793 /*
2794 * FIXME we wrap around correctly to the next entry as
2795 * almost all entries are 4 bytes in size. There is one
2796 * entry which has 12 bytes which is a regular entry
2797 * followed by 8 bytes data. ATM I don't know how
2798 * things are organized if we get next to the a
2799 * boundary so I worry about that once we try to handle
2800 * that.
2801 */
2802 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2803 left -= 4;
2804
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002805 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002806 }
2807
2808 evt->count = 0;
2809 evt->flags &= ~DWC3_EVENT_PENDING;
2810 ret = IRQ_HANDLED;
2811
2812 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002813 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002814 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002815 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002816
2817 return ret;
2818}
2819
Felipe Balbidea520a2016-03-30 09:39:34 +03002820static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002821{
Felipe Balbidea520a2016-03-30 09:39:34 +03002822 struct dwc3_event_buffer *evt = _evt;
2823 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002824 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002825 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002826
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002827 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002828 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002829 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002830
2831 return ret;
2832}
2833
Felipe Balbidea520a2016-03-30 09:39:34 +03002834static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002835{
Felipe Balbidea520a2016-03-30 09:39:34 +03002836 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002837 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002838 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002839
Felipe Balbifc8bb912016-05-16 13:14:48 +03002840 if (pm_runtime_suspended(dwc->dev)) {
2841 pm_runtime_get(dwc->dev);
2842 disable_irq_nosync(dwc->irq_gadget);
2843 dwc->pending_events = true;
2844 return IRQ_HANDLED;
2845 }
2846
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002847 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002848 count &= DWC3_GEVNTCOUNT_MASK;
2849 if (!count)
2850 return IRQ_NONE;
2851
Felipe Balbib15a7622011-06-30 16:57:15 +03002852 evt->count = count;
2853 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002854
Felipe Balbie8adfc32013-06-12 21:11:14 +03002855 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002856 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002857 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002858 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002859
Felipe Balbib15a7622011-06-30 16:57:15 +03002860 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002861}
2862
Felipe Balbidea520a2016-03-30 09:39:34 +03002863static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002864{
Felipe Balbidea520a2016-03-30 09:39:34 +03002865 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002866
Felipe Balbidea520a2016-03-30 09:39:34 +03002867 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002868}
2869
2870/**
2871 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002872 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002873 *
2874 * Returns 0 on success otherwise negative errno.
2875 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002876int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002877{
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002879
2880 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2881 &dwc->ctrl_req_addr, GFP_KERNEL);
2882 if (!dwc->ctrl_req) {
2883 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2884 ret = -ENOMEM;
2885 goto err0;
2886 }
2887
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302888 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 &dwc->ep0_trb_addr, GFP_KERNEL);
2890 if (!dwc->ep0_trb) {
2891 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2892 ret = -ENOMEM;
2893 goto err1;
2894 }
2895
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002896 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002897 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002898 ret = -ENOMEM;
2899 goto err2;
2900 }
2901
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002902 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002903 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2904 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002905 if (!dwc->ep0_bounce) {
2906 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2907 ret = -ENOMEM;
2908 goto err3;
2909 }
2910
Felipe Balbi04c03d12015-12-02 10:06:45 -06002911 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2912 if (!dwc->zlp_buf) {
2913 ret = -ENOMEM;
2914 goto err4;
2915 }
2916
Felipe Balbi72246da2011-08-19 18:10:58 +03002917 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002918 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002919 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002920 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002921 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002922
2923 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002924 * FIXME We might be setting max_speed to <SUPER, however versions
2925 * <2.20a of dwc3 have an issue with metastability (documented
2926 * elsewhere in this driver) which tells us we can't set max speed to
2927 * anything lower than SUPER.
2928 *
2929 * Because gadget.max_speed is only used by composite.c and function
2930 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2931 * to happen so we avoid sending SuperSpeed Capability descriptor
2932 * together with our BOS descriptor as that could confuse host into
2933 * thinking we can handle super speed.
2934 *
2935 * Note that, in fact, we won't even support GetBOS requests when speed
2936 * is less than super speed because we don't have means, yet, to tell
2937 * composite.c that we are USB 2.0 + LPM ECN.
2938 */
2939 if (dwc->revision < DWC3_REVISION_220A)
2940 dwc3_trace(trace_dwc3_gadget,
2941 "Changing max_speed on rev %08x\n",
2942 dwc->revision);
2943
2944 dwc->gadget.max_speed = dwc->maximum_speed;
2945
2946 /*
David Cohena4b9d942013-12-09 15:55:38 -08002947 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2948 * on ep out.
2949 */
2950 dwc->gadget.quirk_ep_out_aligned_size = true;
2951
2952 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002953 * REVISIT: Here we should clear all pending IRQs to be
2954 * sure we're starting from a well known location.
2955 */
2956
2957 ret = dwc3_gadget_init_endpoints(dwc);
2958 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002959 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002960
Felipe Balbi72246da2011-08-19 18:10:58 +03002961 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2962 if (ret) {
2963 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002964 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002965 }
2966
2967 return 0;
2968
Felipe Balbi04c03d12015-12-02 10:06:45 -06002969err5:
2970 kfree(dwc->zlp_buf);
2971
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002972err4:
David Cohene1f80462013-09-11 17:42:47 -07002973 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002974 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2975 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002976
Felipe Balbi72246da2011-08-19 18:10:58 +03002977err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002978 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002979
2980err2:
2981 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2982 dwc->ep0_trb, dwc->ep0_trb_addr);
2983
2984err1:
2985 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2986 dwc->ctrl_req, dwc->ctrl_req_addr);
2987
2988err0:
2989 return ret;
2990}
2991
Felipe Balbi7415f172012-04-30 14:56:33 +03002992/* -------------------------------------------------------------------------- */
2993
Felipe Balbi72246da2011-08-19 18:10:58 +03002994void dwc3_gadget_exit(struct dwc3 *dwc)
2995{
Felipe Balbi72246da2011-08-19 18:10:58 +03002996 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002997
Felipe Balbi72246da2011-08-19 18:10:58 +03002998 dwc3_gadget_free_endpoints(dwc);
2999
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003000 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3001 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003002
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003003 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003004 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003005
3006 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3007 dwc->ep0_trb, dwc->ep0_trb_addr);
3008
3009 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3010 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003011}
Felipe Balbi7415f172012-04-30 14:56:33 +03003012
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003013int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003014{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003015 int ret;
3016
Roger Quadros9772b472016-04-12 11:33:29 +03003017 if (!dwc->gadget_driver)
3018 return 0;
3019
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003020 ret = dwc3_gadget_run_stop(dwc, false, false);
3021 if (ret < 0)
3022 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003023
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003024 dwc3_disconnect_gadget(dwc);
3025 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003026
3027 return 0;
3028}
3029
3030int dwc3_gadget_resume(struct dwc3 *dwc)
3031{
Felipe Balbi7415f172012-04-30 14:56:33 +03003032 int ret;
3033
Roger Quadros9772b472016-04-12 11:33:29 +03003034 if (!dwc->gadget_driver)
3035 return 0;
3036
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003037 ret = __dwc3_gadget_start(dwc);
3038 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003039 goto err0;
3040
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003041 ret = dwc3_gadget_run_stop(dwc, true, false);
3042 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003043 goto err1;
3044
Felipe Balbi7415f172012-04-30 14:56:33 +03003045 return 0;
3046
3047err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003048 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003049
3050err0:
3051 return ret;
3052}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003053
3054void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3055{
3056 if (dwc->pending_events) {
3057 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3058 dwc->pending_events = false;
3059 enable_irq(dwc->irq_gadget);
3060 }
3061}