blob: f148b11f6e4d8bb8863d27509f606cfd3dce8b57 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200149{
Felipe Balbief966b92016-04-05 13:09:51 +0300150 dep->trb_enqueue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300151 dep->trb_enqueue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300152}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200153
Felipe Balbief966b92016-04-05 13:09:51 +0300154static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
155{
156 dep->trb_dequeue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300157 dep->trb_dequeue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static int dwc3_ep_is_last_trb(unsigned int index)
161{
Felipe Balbi4faf7552016-04-05 13:14:31 +0300162 return index == DWC3_TRB_NUM - 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200163}
164
Felipe Balbi72246da2011-08-19 18:10:58 +0300165void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
166 int status)
167{
168 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530169 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200171 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530172 i = 0;
173 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300174 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530175 /*
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
179 */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300180 if (dwc3_ep_is_last_trb(dep->trb_dequeue))
Felipe Balbief966b92016-04-05 13:09:51 +0300181 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530182 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200183 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 }
185 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200186 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Pratyush Anand0416e492012-08-10 13:42:16 +0530191 if (dwc->ep0_bounced && dep->number == 0)
192 dwc->ep0_bounced = false;
193 else
194 usb_gadget_unmap_request(&dwc->gadget, &req->request,
195 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300202
203 if (dep->number > 1)
204 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300205}
206
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500207int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300208{
209 u32 timeout = 500;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300210 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300211 u32 reg;
212
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500213 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500214
Felipe Balbib09bb642012-04-24 16:19:11 +0300215 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
216 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
217
218 do {
219 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
220 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600221 dwc3_trace(trace_dwc3_gadget,
222 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300223 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530224 if (DWC3_DGCMD_STATUS(reg))
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300225 ret = -EINVAL;
226 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300227 }
228
Felipe Balbib09bb642012-04-24 16:19:11 +0300229 udelay(1);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300230 } while (timeout--);
231
232 if (!timeout) {
233 dwc3_trace(trace_dwc3_gadget,
234 "Command Timed Out");
235 ret = -ETIMEDOUT;
236 }
237
238 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300239}
240
Felipe Balbic36d8e92016-04-04 12:46:33 +0300241static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
242
Felipe Balbi2cd47182016-04-12 16:42:43 +0300243int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
244 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300245{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300246 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200247 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300248 u32 reg;
249
Felipe Balbi0933df12016-05-23 14:02:33 +0300250 int cmd_status = 0;
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 Balbi2b0f11d2016-04-04 09:19:17 +0300254 /*
255 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
256 * we're issuing an endpoint command, we must check if
257 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
258 *
259 * We will also set SUSPHY bit to what it was before returning as stated
260 * by the same section on Synopsys databook.
261 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300262 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
263 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
264 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
265 susphy = true;
266 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
267 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
268 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300269 }
270
Felipe Balbic36d8e92016-04-04 12:46:33 +0300271 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
272 int needs_wakeup;
273
274 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
275 dwc->link_state == DWC3_LINK_STATE_U2 ||
276 dwc->link_state == DWC3_LINK_STATE_U3);
277
278 if (unlikely(needs_wakeup)) {
279 ret = __dwc3_gadget_wakeup(dwc);
280 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
281 ret);
282 }
283 }
284
Felipe Balbi2eb88012016-04-12 16:53:39 +0300285 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
286 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
287 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300288
Felipe Balbi2eb88012016-04-12 16:53:39 +0300289 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300291 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300293 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294
Felipe Balbi73815282015-01-27 13:48:14 -0600295 dwc3_trace(trace_dwc3_gadget,
296 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000297 cmd_status);
298
299 switch (cmd_status) {
300 case 0:
301 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300302 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000303 case DEPEVT_TRANSFER_NO_RESOURCE:
Felipe Balbiba159842016-05-23 13:50:29 +0300304 dwc3_trace(trace_dwc3_gadget, "no resource available");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000305 ret = -EINVAL;
306 break;
307 case DEPEVT_TRANSFER_BUS_EXPIRY:
308 /*
309 * SW issues START TRANSFER command to
310 * isochronous ep with future frame interval. If
311 * future interval time has already passed when
312 * core receives the command, it will respond
313 * with an error status of 'Bus Expiry'.
314 *
315 * Instead of always returning -EINVAL, let's
316 * give a hint to the gadget driver that this is
317 * the case by returning -EAGAIN.
318 */
Felipe Balbiba159842016-05-23 13:50:29 +0300319 dwc3_trace(trace_dwc3_gadget, "bus expiry");
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000320 ret = -EAGAIN;
321 break;
322 default:
323 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
324 }
325
Felipe Balbic0ca3242016-04-04 09:11:51 +0300326 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300328 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300329
Felipe Balbif6bb2252016-05-23 13:53:34 +0300330 if (timeout == 0) {
331 dwc3_trace(trace_dwc3_gadget,
332 "Command Timed Out");
333 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300334 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300335 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300336
Felipe Balbi0933df12016-05-23 14:02:33 +0300337 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
338
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300339 if (unlikely(susphy)) {
340 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
341 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
342 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
343 }
344
Felipe Balbic0ca3242016-04-04 09:11:51 +0300345 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300346}
347
John Youn50c763f2016-05-31 17:49:56 -0700348static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
349{
350 struct dwc3 *dwc = dep->dwc;
351 struct dwc3_gadget_ep_cmd_params params;
352 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
353
354 /*
355 * As of core revision 2.60a the recommended programming model
356 * is to set the ClearPendIN bit when issuing a Clear Stall EP
357 * command for IN endpoints. This is to prevent an issue where
358 * some (non-compliant) hosts may not send ACK TPs for pending
359 * IN transfers due to a mishandled error condition. Synopsys
360 * STAR 9000614252.
361 */
362 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
363 cmd |= DWC3_DEPCMD_CLEARPENDIN;
364
365 memset(&params, 0, sizeof(params));
366
Felipe Balbi2cd47182016-04-12 16:42:43 +0300367 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700368}
369
Felipe Balbi72246da2011-08-19 18:10:58 +0300370static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200371 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300372{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300373 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
375 return dep->trb_pool_dma + offset;
376}
377
378static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
379{
380 struct dwc3 *dwc = dep->dwc;
381
382 if (dep->trb_pool)
383 return 0;
384
Felipe Balbi72246da2011-08-19 18:10:58 +0300385 dep->trb_pool = dma_alloc_coherent(dwc->dev,
386 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
387 &dep->trb_pool_dma, GFP_KERNEL);
388 if (!dep->trb_pool) {
389 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
390 dep->name);
391 return -ENOMEM;
392 }
393
394 return 0;
395}
396
397static void dwc3_free_trb_pool(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400
401 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
402 dep->trb_pool, dep->trb_pool_dma);
403
404 dep->trb_pool = NULL;
405 dep->trb_pool_dma = 0;
406}
407
John Younc4509602016-02-16 20:10:53 -0800408static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
409
410/**
411 * dwc3_gadget_start_config - Configure EP resources
412 * @dwc: pointer to our controller context structure
413 * @dep: endpoint that is being enabled
414 *
415 * The assignment of transfer resources cannot perfectly follow the
416 * data book due to the fact that the controller driver does not have
417 * all knowledge of the configuration in advance. It is given this
418 * information piecemeal by the composite gadget framework after every
419 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
420 * programming model in this scenario can cause errors. For two
421 * reasons:
422 *
423 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
424 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
425 * multiple interfaces.
426 *
427 * 2) The databook does not mention doing more DEPXFERCFG for new
428 * endpoint on alt setting (8.1.6).
429 *
430 * The following simplified method is used instead:
431 *
432 * All hardware endpoints can be assigned a transfer resource and this
433 * setting will stay persistent until either a core reset or
434 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
435 * do DEPXFERCFG for every hardware endpoint as well. We are
436 * guaranteed that there are as many transfer resources as endpoints.
437 *
438 * This function is called for each endpoint when it is being enabled
439 * but is triggered only when called for EP0-out, which always happens
440 * first, and which should only happen in one of the above conditions.
441 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300442static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
443{
444 struct dwc3_gadget_ep_cmd_params params;
445 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800446 int i;
447 int ret;
448
449 if (dep->number)
450 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
452 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800453 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300454
Felipe Balbi2cd47182016-04-12 16:42:43 +0300455 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800456 if (ret)
457 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300458
John Younc4509602016-02-16 20:10:53 -0800459 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
460 struct dwc3_ep *dep = dwc->eps[i];
461
462 if (!dep)
463 continue;
464
465 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
466 if (ret)
467 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 }
469
470 return 0;
471}
472
473static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200474 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300475 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600476 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300477{
478 struct dwc3_gadget_ep_cmd_params params;
479
480 memset(&params, 0x00, sizeof(params));
481
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300482 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900483 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
484
485 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800486 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300487 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300488 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900489 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300490
Felipe Balbi4b345c92012-07-16 14:08:16 +0300491 if (ignore)
492 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
493
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600494 if (restore) {
495 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
496 params.param2 |= dep->saved_state;
497 }
498
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300499 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
500 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300501
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200502 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300503 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
504 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300505 dep->stream_capable = true;
506 }
507
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500508 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300509 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300510
511 /*
512 * We are doing 1:1 mapping for endpoints, meaning
513 * Physical Endpoints 2 maps to Logical Endpoint 2 and
514 * so on. We consider the direction bit as part of the physical
515 * endpoint number. So USB endpoint 0x81 is 0x03.
516 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300517 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300518
519 /*
520 * We must use the lower 16 TX FIFOs even though
521 * HW might have more
522 */
523 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300524 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300525
526 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300527 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 dep->interval = 1 << (desc->bInterval - 1);
529 }
530
Felipe Balbi2cd47182016-04-12 16:42:43 +0300531 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300532}
533
534static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
535{
536 struct dwc3_gadget_ep_cmd_params params;
537
538 memset(&params, 0x00, sizeof(params));
539
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300540 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
Felipe Balbi2cd47182016-04-12 16:42:43 +0300542 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
543 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300544}
545
546/**
547 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
548 * @dep: endpoint to be initialized
549 * @desc: USB Endpoint Descriptor
550 *
551 * Caller should take care of locking
552 */
553static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200554 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300555 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600556 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300557{
558 struct dwc3 *dwc = dep->dwc;
559 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300560 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300561
Felipe Balbi73815282015-01-27 13:48:14 -0600562 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300563
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 if (!(dep->flags & DWC3_EP_ENABLED)) {
565 ret = dwc3_gadget_start_config(dwc, dep);
566 if (ret)
567 return ret;
568 }
569
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600570 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
571 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 if (ret)
573 return ret;
574
575 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200576 struct dwc3_trb *trb_st_hw;
577 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300578
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200579 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200580 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 dep->type = usb_endpoint_type(desc);
582 dep->flags |= DWC3_EP_ENABLED;
583
584 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
585 reg |= DWC3_DALEPENA_EP(dep->number);
586 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
587
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300588 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300589 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300591 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 trb_st_hw = &dep->trb_pool[0];
593
Felipe Balbif6bafc62012-02-06 11:04:53 +0200594 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700595 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Felipe Balbif6bafc62012-02-06 11:04:53 +0200597 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
598 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
599 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
600 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 }
602
603 return 0;
604}
605
Paul Zimmermanb992e682012-04-27 14:17:35 +0300606static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200607static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300608{
609 struct dwc3_request *req;
610
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200611 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300612 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200613
Pratyush Anand57911502012-07-06 15:19:10 +0530614 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200615 while (!list_empty(&dep->started_list)) {
616 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530617
618 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
619 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200620 }
621
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200622 while (!list_empty(&dep->pending_list)) {
623 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300624
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200625 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300627}
628
629/**
630 * __dwc3_gadget_ep_disable - Disables a HW endpoint
631 * @dep: the endpoint to disable
632 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200633 * This function also removes requests which are currently processed ny the
634 * hardware and those which are not yet scheduled.
635 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300637static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
638{
639 struct dwc3 *dwc = dep->dwc;
640 u32 reg;
641
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500642 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
643
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200644 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
Felipe Balbi687ef982014-04-16 10:30:33 -0500646 /* make sure HW endpoint isn't stalled */
647 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500648 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500649
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
651 reg &= ~DWC3_DALEPENA_EP(dep->number);
652 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
653
Felipe Balbi879631a2011-09-30 10:58:47 +0300654 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200655 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200656 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300658 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300659
660 return 0;
661}
662
663/* -------------------------------------------------------------------------- */
664
665static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
666 const struct usb_endpoint_descriptor *desc)
667{
668 return -EINVAL;
669}
670
671static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
672{
673 return -EINVAL;
674}
675
676/* -------------------------------------------------------------------------- */
677
678static int dwc3_gadget_ep_enable(struct usb_ep *ep,
679 const struct usb_endpoint_descriptor *desc)
680{
681 struct dwc3_ep *dep;
682 struct dwc3 *dwc;
683 unsigned long flags;
684 int ret;
685
686 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
687 pr_debug("dwc3: invalid parameters\n");
688 return -EINVAL;
689 }
690
691 if (!desc->wMaxPacketSize) {
692 pr_debug("dwc3: missing wMaxPacketSize\n");
693 return -EINVAL;
694 }
695
696 dep = to_dwc3_ep(ep);
697 dwc = dep->dwc;
698
Felipe Balbi95ca9612015-12-10 13:08:20 -0600699 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
700 "%s is already enabled\n",
701 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300702 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300703
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600705 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300706 spin_unlock_irqrestore(&dwc->lock, flags);
707
708 return ret;
709}
710
711static int dwc3_gadget_ep_disable(struct usb_ep *ep)
712{
713 struct dwc3_ep *dep;
714 struct dwc3 *dwc;
715 unsigned long flags;
716 int ret;
717
718 if (!ep) {
719 pr_debug("dwc3: invalid parameters\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 disabled\n",
728 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300730
Felipe Balbi72246da2011-08-19 18:10:58 +0300731 spin_lock_irqsave(&dwc->lock, flags);
732 ret = __dwc3_gadget_ep_disable(dep);
733 spin_unlock_irqrestore(&dwc->lock, flags);
734
735 return ret;
736}
737
738static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
739 gfp_t gfp_flags)
740{
741 struct dwc3_request *req;
742 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
744 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900745 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300747
748 req->epnum = dep->number;
749 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300750
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500751 trace_dwc3_alloc_request(req);
752
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 return &req->request;
754}
755
756static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
757 struct usb_request *request)
758{
759 struct dwc3_request *req = to_dwc3_request(request);
760
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500761 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300762 kfree(req);
763}
764
Felipe Balbic71fc372011-11-22 11:37:34 +0200765/**
766 * dwc3_prepare_one_trb - setup one TRB from one request
767 * @dep: endpoint for which this request is prepared
768 * @req: dwc3_request pointer
769 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200770static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200771 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530772 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200773{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200774 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200775
Felipe Balbi73815282015-01-27 13:48:14 -0600776 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200777 dep->name, req, (unsigned long long) dma,
778 length, last ? " last" : "",
779 chain ? " chain" : "");
780
Pratyush Anand915e2022013-01-14 15:59:35 +0530781
Felipe Balbi4faf7552016-04-05 13:14:31 +0300782 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200783
Felipe Balbieeb720f2011-11-28 12:46:59 +0200784 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200785 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200786 req->trb = trb;
787 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300788 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200789 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200790
Felipe Balbief966b92016-04-05 13:09:51 +0300791 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300792 /* Skip the LINK-TRB */
793 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300794 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530795
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 trb->size = DWC3_TRB_SIZE_LENGTH(length);
797 trb->bpl = lower_32_bits(dma);
798 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200799
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200800 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200801 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200802 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200803 break;
804
805 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530806 if (!node)
807 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
808 else
809 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200810
811 /* always enable Interrupt on Missed ISOC */
812 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200813 break;
814
815 case USB_ENDPOINT_XFER_BULK:
816 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200817 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200818 break;
819 default:
820 /*
821 * This is only possible with faulty memory because we
822 * checked it already :)
823 */
824 BUG();
825 }
826
Felipe Balbica4d44e2016-03-10 13:53:27 +0200827 /* always enable Continue on Short Packet */
828 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600829
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300830 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200831 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
832
833 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530834 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200835
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530836 if (chain)
837 trb->ctrl |= DWC3_TRB_CTRL_CHN;
838
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200839 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200840 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
841
842 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500843
844 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200845}
846
Felipe Balbic4233572016-05-12 14:08:34 +0300847static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
848{
849 struct dwc3_trb *tmp;
850
851 /*
852 * If enqueue & dequeue are equal than it is either full or empty.
853 *
854 * One way to know for sure is if the TRB right before us has HWO bit
855 * set or not. If it has, then we're definitely full and can't fit any
856 * more transfers in our ring.
857 */
858 if (dep->trb_enqueue == dep->trb_dequeue) {
859 /* If we're full, enqueue/dequeue are > 0 */
860 if (dep->trb_enqueue) {
861 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
862 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
863 return 0;
864 }
865
866 return DWC3_TRB_NUM - 1;
867 }
868
869 return dep->trb_dequeue - dep->trb_enqueue;
870}
871
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300872static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
873 struct dwc3_request *req, unsigned int trbs_left)
874{
875 struct usb_request *request = &req->request;
876 struct scatterlist *sg = request->sg;
877 struct scatterlist *s;
878 unsigned int last = false;
879 unsigned int length;
880 dma_addr_t dma;
881 int i;
882
883 for_each_sg(sg, s, request->num_mapped_sgs, i) {
884 unsigned chain = true;
885
886 length = sg_dma_len(s);
887 dma = sg_dma_address(s);
888
889 if (sg_is_last(s)) {
890 if (list_is_last(&req->list, &dep->pending_list))
891 last = true;
892
893 chain = false;
894 }
895
896 if (!trbs_left)
897 last = true;
898
899 if (last)
900 chain = false;
901
902 dwc3_prepare_one_trb(dep, req, dma, length,
903 last, chain, i);
904
905 if (last)
906 break;
907 }
908}
909
910static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
911 struct dwc3_request *req, unsigned int trbs_left)
912{
913 unsigned int last = false;
914 unsigned int length;
915 dma_addr_t dma;
916
917 dma = req->request.dma;
918 length = req->request.length;
919
920 if (!trbs_left)
921 last = true;
922
923 /* Is this the last request? */
924 if (list_is_last(&req->list, &dep->pending_list))
925 last = true;
926
927 dwc3_prepare_one_trb(dep, req, dma, length,
928 last, false, 0);
929}
930
Felipe Balbi72246da2011-08-19 18:10:58 +0300931/*
932 * dwc3_prepare_trbs - setup TRBs from requests
933 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300934 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800935 * The function goes through the requests list and sets up TRBs for the
936 * transfers. The function returns once there are no more TRBs available or
937 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300938 */
Felipe Balbic4233572016-05-12 14:08:34 +0300939static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300940{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200941 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300942 u32 trbs_left;
943
944 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
945
Felipe Balbic4233572016-05-12 14:08:34 +0300946 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300947
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200948 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300949 if (req->request.num_mapped_sgs > 0)
950 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
951 else
952 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300953
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300954 if (!trbs_left)
955 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300956 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300957}
958
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300959static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300960{
961 struct dwc3_gadget_ep_cmd_params params;
962 struct dwc3_request *req;
963 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300964 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 int ret;
966 u32 cmd;
967
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300968 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300969
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300970 dwc3_prepare_trbs(dep);
971 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 if (!req) {
973 dep->flags |= DWC3_EP_PENDING_REQUEST;
974 return 0;
975 }
976
977 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300978
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300979 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530980 params.param0 = upper_32_bits(req->trb_dma);
981 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300982 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530983 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530985 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300986
987 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +0300988 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 /*
991 * FIXME we need to iterate over the list of requests
992 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800993 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200995 usb_gadget_unmap_request(&dwc->gadget, &req->request,
996 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 list_del(&req->list);
998 return ret;
999 }
1000
1001 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001002
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001003 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001004 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001005 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001006 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001007
Felipe Balbi72246da2011-08-19 18:10:58 +03001008 return 0;
1009}
1010
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301011static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1012 struct dwc3_ep *dep, u32 cur_uf)
1013{
1014 u32 uf;
1015
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001016 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001017 dwc3_trace(trace_dwc3_gadget,
1018 "ISOC ep %s run out for requests",
1019 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301020 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301021 return;
1022 }
1023
1024 /* 4 micro frames in the future */
1025 uf = cur_uf + dep->interval * 4;
1026
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001027 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301028}
1029
1030static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1031 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1032{
1033 u32 cur_uf, mask;
1034
1035 mask = ~(dep->interval - 1);
1036 cur_uf = event->parameters & mask;
1037
1038 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1039}
1040
Felipe Balbi72246da2011-08-19 18:10:58 +03001041static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1042{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001043 struct dwc3 *dwc = dep->dwc;
1044 int ret;
1045
Felipe Balbibb423982015-11-16 15:31:21 -06001046 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001047 dwc3_trace(trace_dwc3_gadget,
1048 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001049 &req->request, dep->endpoint.name);
1050 return -ESHUTDOWN;
1051 }
1052
1053 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1054 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001055 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1056 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001057 return -EINVAL;
1058 }
1059
Felipe Balbifc8bb912016-05-16 13:14:48 +03001060 pm_runtime_get(dwc->dev);
1061
Felipe Balbi72246da2011-08-19 18:10:58 +03001062 req->request.actual = 0;
1063 req->request.status = -EINPROGRESS;
1064 req->direction = dep->direction;
1065 req->epnum = dep->number;
1066
Felipe Balbife84f522015-09-01 09:01:38 -05001067 trace_dwc3_ep_queue(req);
1068
Felipe Balbi72246da2011-08-19 18:10:58 +03001069 /*
1070 * We only add to our list of requests now and
1071 * start consuming the list once we get XferNotReady
1072 * IRQ.
1073 *
1074 * That way, we avoid doing anything that we don't need
1075 * to do now and defer it until the point we receive a
1076 * particular token from the Host side.
1077 *
1078 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001079 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001080 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001081 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1082 dep->direction);
1083 if (ret)
1084 return ret;
1085
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001086 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001087
1088 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001089 * If there are no pending requests and the endpoint isn't already
1090 * busy, we will just start the request straight away.
1091 *
1092 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1093 * little bit faster.
1094 */
1095 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001096 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001097 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001098 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001099 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001100 }
1101
1102 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001103 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001104 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001105 * 1. XferNotReady with empty list of requests. We need to kick the
1106 * transfer here in that situation, otherwise we will be NAKing
1107 * forever. If we get XferNotReady before gadget driver has a
1108 * chance to queue a request, we will ACK the IRQ but won't be
1109 * able to receive the data until the next request is queued.
1110 * The following code is handling exactly that.
1111 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001112 */
1113 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301114 /*
1115 * If xfernotready is already elapsed and it is a case
1116 * of isoc transfer, then issue END TRANSFER, so that
1117 * you can receive xfernotready again and can have
1118 * notion of current microframe.
1119 */
1120 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001121 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001122 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301123 dep->flags = DWC3_EP_ENABLED;
1124 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301125 return 0;
1126 }
1127
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001128 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001129 if (!ret)
1130 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1131
Felipe Balbia8f32812015-09-16 10:40:07 -05001132 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001133 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001134
Felipe Balbib511e5e2012-06-06 12:00:50 +03001135 /*
1136 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1137 * kick the transfer here after queuing a request, otherwise the
1138 * core may not see the modified TRB(s).
1139 */
1140 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301141 (dep->flags & DWC3_EP_BUSY) &&
1142 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001143 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001144 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001145 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001146 }
1147
Felipe Balbib997ada2012-07-26 13:26:50 +03001148 /*
1149 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1150 * right away, otherwise host will not know we have streams to be
1151 * handled.
1152 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001153 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001154 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001155
Felipe Balbia8f32812015-09-16 10:40:07 -05001156out:
1157 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001158 dwc3_trace(trace_dwc3_gadget,
1159 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001160 dep->name);
1161 if (ret == -EBUSY)
1162 ret = 0;
1163
1164 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001165}
1166
Felipe Balbi04c03d12015-12-02 10:06:45 -06001167static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1168 struct usb_request *request)
1169{
1170 dwc3_gadget_ep_free_request(ep, request);
1171}
1172
1173static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1174{
1175 struct dwc3_request *req;
1176 struct usb_request *request;
1177 struct usb_ep *ep = &dep->endpoint;
1178
1179 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1180 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1181 if (!request)
1182 return -ENOMEM;
1183
1184 request->length = 0;
1185 request->buf = dwc->zlp_buf;
1186 request->complete = __dwc3_gadget_ep_zlp_complete;
1187
1188 req = to_dwc3_request(request);
1189
1190 return __dwc3_gadget_ep_queue(dep, req);
1191}
1192
Felipe Balbi72246da2011-08-19 18:10:58 +03001193static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1194 gfp_t gfp_flags)
1195{
1196 struct dwc3_request *req = to_dwc3_request(request);
1197 struct dwc3_ep *dep = to_dwc3_ep(ep);
1198 struct dwc3 *dwc = dep->dwc;
1199
1200 unsigned long flags;
1201
1202 int ret;
1203
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001204 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001205 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001206
1207 /*
1208 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1209 * setting request->zero, instead of doing magic, we will just queue an
1210 * extra usb_request ourselves so that it gets handled the same way as
1211 * any other request.
1212 */
John Yound92618982015-12-22 12:23:20 -08001213 if (ret == 0 && request->zero && request->length &&
1214 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001215 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1216
Felipe Balbi72246da2011-08-19 18:10:58 +03001217 spin_unlock_irqrestore(&dwc->lock, flags);
1218
1219 return ret;
1220}
1221
1222static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1223 struct usb_request *request)
1224{
1225 struct dwc3_request *req = to_dwc3_request(request);
1226 struct dwc3_request *r = NULL;
1227
1228 struct dwc3_ep *dep = to_dwc3_ep(ep);
1229 struct dwc3 *dwc = dep->dwc;
1230
1231 unsigned long flags;
1232 int ret = 0;
1233
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001234 trace_dwc3_ep_dequeue(req);
1235
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 spin_lock_irqsave(&dwc->lock, flags);
1237
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001238 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001239 if (r == req)
1240 break;
1241 }
1242
1243 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001244 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001245 if (r == req)
1246 break;
1247 }
1248 if (r == req) {
1249 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001250 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301251 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 }
1253 dev_err(dwc->dev, "request %p was not queued to %s\n",
1254 request, ep->name);
1255 ret = -EINVAL;
1256 goto out0;
1257 }
1258
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301259out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001260 /* giveback the request */
1261 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1262
1263out0:
1264 spin_unlock_irqrestore(&dwc->lock, flags);
1265
1266 return ret;
1267}
1268
Felipe Balbi7a608552014-09-24 14:19:52 -05001269int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001270{
1271 struct dwc3_gadget_ep_cmd_params params;
1272 struct dwc3 *dwc = dep->dwc;
1273 int ret;
1274
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001275 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1276 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1277 return -EINVAL;
1278 }
1279
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 memset(&params, 0x00, sizeof(params));
1281
1282 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001283 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001284 (!list_empty(&dep->started_list) ||
1285 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001286 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001287 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001288 dep->name);
1289 return -EAGAIN;
1290 }
1291
Felipe Balbi2cd47182016-04-12 16:42:43 +03001292 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1293 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001294 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001295 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001296 dep->name);
1297 else
1298 dep->flags |= DWC3_EP_STALL;
1299 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001300
John Youn50c763f2016-05-31 17:49:56 -07001301 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001303 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001304 dep->name);
1305 else
Alan Sterna535d812013-11-01 12:05:12 -04001306 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001308
Felipe Balbi72246da2011-08-19 18:10:58 +03001309 return ret;
1310}
1311
1312static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1313{
1314 struct dwc3_ep *dep = to_dwc3_ep(ep);
1315 struct dwc3 *dwc = dep->dwc;
1316
1317 unsigned long flags;
1318
1319 int ret;
1320
1321 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001322 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 spin_unlock_irqrestore(&dwc->lock, flags);
1324
1325 return ret;
1326}
1327
1328static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1329{
1330 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001331 struct dwc3 *dwc = dep->dwc;
1332 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001333 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001334
Paul Zimmerman249a4562012-02-24 17:32:16 -08001335 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 dep->flags |= DWC3_EP_WEDGE;
1337
Pratyush Anand08f0d962012-06-25 22:40:43 +05301338 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001339 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301340 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001341 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001342 spin_unlock_irqrestore(&dwc->lock, flags);
1343
1344 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001345}
1346
1347/* -------------------------------------------------------------------------- */
1348
1349static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1350 .bLength = USB_DT_ENDPOINT_SIZE,
1351 .bDescriptorType = USB_DT_ENDPOINT,
1352 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1353};
1354
1355static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1356 .enable = dwc3_gadget_ep0_enable,
1357 .disable = dwc3_gadget_ep0_disable,
1358 .alloc_request = dwc3_gadget_ep_alloc_request,
1359 .free_request = dwc3_gadget_ep_free_request,
1360 .queue = dwc3_gadget_ep0_queue,
1361 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301362 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 .set_wedge = dwc3_gadget_ep_set_wedge,
1364};
1365
1366static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1367 .enable = dwc3_gadget_ep_enable,
1368 .disable = dwc3_gadget_ep_disable,
1369 .alloc_request = dwc3_gadget_ep_alloc_request,
1370 .free_request = dwc3_gadget_ep_free_request,
1371 .queue = dwc3_gadget_ep_queue,
1372 .dequeue = dwc3_gadget_ep_dequeue,
1373 .set_halt = dwc3_gadget_ep_set_halt,
1374 .set_wedge = dwc3_gadget_ep_set_wedge,
1375};
1376
1377/* -------------------------------------------------------------------------- */
1378
1379static int dwc3_gadget_get_frame(struct usb_gadget *g)
1380{
1381 struct dwc3 *dwc = gadget_to_dwc(g);
1382 u32 reg;
1383
1384 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1385 return DWC3_DSTS_SOFFN(reg);
1386}
1387
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001388static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001389{
Felipe Balbi72246da2011-08-19 18:10:58 +03001390 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001391
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001392 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001393 u32 reg;
1394
Felipe Balbi72246da2011-08-19 18:10:58 +03001395 u8 link_state;
1396 u8 speed;
1397
Felipe Balbi72246da2011-08-19 18:10:58 +03001398 /*
1399 * According to the Databook Remote wakeup request should
1400 * be issued only when the device is in early suspend state.
1401 *
1402 * We can check that via USB Link State bits in DSTS register.
1403 */
1404 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1405
1406 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001407 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1408 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001409 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001410 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 }
1412
1413 link_state = DWC3_DSTS_USBLNKST(reg);
1414
1415 switch (link_state) {
1416 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1417 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1418 break;
1419 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001420 dwc3_trace(trace_dwc3_gadget,
1421 "can't wakeup from '%s'\n",
1422 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001423 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 }
1425
Felipe Balbi8598bde2012-01-02 18:55:57 +02001426 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1427 if (ret < 0) {
1428 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001429 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001430 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001431
Paul Zimmerman802fde92012-04-27 13:10:52 +03001432 /* Recent versions do this automatically */
1433 if (dwc->revision < DWC3_REVISION_194A) {
1434 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001435 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001436 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1437 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1438 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001439
Paul Zimmerman1d046792012-02-15 18:56:56 -08001440 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001441 timeout = jiffies + msecs_to_jiffies(100);
1442
Paul Zimmerman1d046792012-02-15 18:56:56 -08001443 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001444 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1445
1446 /* in HS, means ON */
1447 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1448 break;
1449 }
1450
1451 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1452 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001453 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001454 }
1455
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001456 return 0;
1457}
1458
1459static int dwc3_gadget_wakeup(struct usb_gadget *g)
1460{
1461 struct dwc3 *dwc = gadget_to_dwc(g);
1462 unsigned long flags;
1463 int ret;
1464
1465 spin_lock_irqsave(&dwc->lock, flags);
1466 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 spin_unlock_irqrestore(&dwc->lock, flags);
1468
1469 return ret;
1470}
1471
1472static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1473 int is_selfpowered)
1474{
1475 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001476 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001477
Paul Zimmerman249a4562012-02-24 17:32:16 -08001478 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001479 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001480 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001481
1482 return 0;
1483}
1484
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001485static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001486{
1487 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001488 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001489
Felipe Balbifc8bb912016-05-16 13:14:48 +03001490 if (pm_runtime_suspended(dwc->dev))
1491 return 0;
1492
Felipe Balbi72246da2011-08-19 18:10:58 +03001493 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001494 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001495 if (dwc->revision <= DWC3_REVISION_187A) {
1496 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1497 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1498 }
1499
1500 if (dwc->revision >= DWC3_REVISION_194A)
1501 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1502 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001503
1504 if (dwc->has_hibernation)
1505 reg |= DWC3_DCTL_KEEP_CONNECT;
1506
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001507 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001508 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001510
1511 if (dwc->has_hibernation && !suspend)
1512 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1513
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001514 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001515 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001516
1517 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1518
1519 do {
1520 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1521 if (is_on) {
1522 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1523 break;
1524 } else {
1525 if (reg & DWC3_DSTS_DEVCTRLHLT)
1526 break;
1527 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001528 timeout--;
1529 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301530 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001531 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001532 } while (1);
1533
Felipe Balbi73815282015-01-27 13:48:14 -06001534 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 dwc->gadget_driver
1536 ? dwc->gadget_driver->function : "no-function",
1537 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301538
1539 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001540}
1541
1542static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1543{
1544 struct dwc3 *dwc = gadget_to_dwc(g);
1545 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301546 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001547
1548 is_on = !!is_on;
1549
1550 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001551 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001552 spin_unlock_irqrestore(&dwc->lock, flags);
1553
Pratyush Anand6f17f742012-07-02 10:21:55 +05301554 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001555}
1556
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001557static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1558{
1559 u32 reg;
1560
1561 /* Enable all but Start and End of Frame IRQs */
1562 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1563 DWC3_DEVTEN_EVNTOVERFLOWEN |
1564 DWC3_DEVTEN_CMDCMPLTEN |
1565 DWC3_DEVTEN_ERRTICERREN |
1566 DWC3_DEVTEN_WKUPEVTEN |
1567 DWC3_DEVTEN_ULSTCNGEN |
1568 DWC3_DEVTEN_CONNECTDONEEN |
1569 DWC3_DEVTEN_USBRSTEN |
1570 DWC3_DEVTEN_DISCONNEVTEN);
1571
1572 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1573}
1574
1575static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1576{
1577 /* mask all interrupts */
1578 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1579}
1580
1581static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001582static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001583
Felipe Balbi4e994722016-05-13 14:09:59 +03001584/**
1585 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1586 * dwc: pointer to our context structure
1587 *
1588 * The following looks like complex but it's actually very simple. In order to
1589 * calculate the number of packets we can burst at once on OUT transfers, we're
1590 * gonna use RxFIFO size.
1591 *
1592 * To calculate RxFIFO size we need two numbers:
1593 * MDWIDTH = size, in bits, of the internal memory bus
1594 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1595 *
1596 * Given these two numbers, the formula is simple:
1597 *
1598 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1599 *
1600 * 24 bytes is for 3x SETUP packets
1601 * 16 bytes is a clock domain crossing tolerance
1602 *
1603 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1604 */
1605static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1606{
1607 u32 ram2_depth;
1608 u32 mdwidth;
1609 u32 nump;
1610 u32 reg;
1611
1612 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1613 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1614
1615 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1616 nump = min_t(u32, nump, 16);
1617
1618 /* update NumP */
1619 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1620 reg &= ~DWC3_DCFG_NUMP_MASK;
1621 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1622 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1623}
1624
Felipe Balbid7be2952016-05-04 15:49:37 +03001625static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001626{
Felipe Balbi72246da2011-08-19 18:10:58 +03001627 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001628 int ret = 0;
1629 u32 reg;
1630
Felipe Balbi72246da2011-08-19 18:10:58 +03001631 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1632 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001633
1634 /**
1635 * WORKAROUND: DWC3 revision < 2.20a have an issue
1636 * which would cause metastability state on Run/Stop
1637 * bit if we try to force the IP to USB2-only mode.
1638 *
1639 * Because of that, we cannot configure the IP to any
1640 * speed other than the SuperSpeed
1641 *
1642 * Refers to:
1643 *
1644 * STAR#9000525659: Clock Domain Crossing on DCTL in
1645 * USB 2.0 Mode
1646 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001647 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001648 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001649 } else {
1650 switch (dwc->maximum_speed) {
1651 case USB_SPEED_LOW:
1652 reg |= DWC3_DSTS_LOWSPEED;
1653 break;
1654 case USB_SPEED_FULL:
1655 reg |= DWC3_DSTS_FULLSPEED1;
1656 break;
1657 case USB_SPEED_HIGH:
1658 reg |= DWC3_DSTS_HIGHSPEED;
1659 break;
John Youn75808622016-02-05 17:09:13 -08001660 case USB_SPEED_SUPER_PLUS:
1661 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1662 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001663 default:
John Youn77966eb2016-02-19 17:31:01 -08001664 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1665 dwc->maximum_speed);
1666 /* fall through */
1667 case USB_SPEED_SUPER:
1668 reg |= DWC3_DCFG_SUPERSPEED;
1669 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001670 }
1671 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001672 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1673
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001674 /*
1675 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1676 * field instead of letting dwc3 itself calculate that automatically.
1677 *
1678 * This way, we maximize the chances that we'll be able to get several
1679 * bursts of data without going through any sort of endpoint throttling.
1680 */
1681 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1682 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1683 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1684
Felipe Balbi4e994722016-05-13 14:09:59 +03001685 dwc3_gadget_setup_nump(dwc);
1686
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 /* Start with SuperSpeed Default */
1688 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1689
1690 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001691 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1692 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 if (ret) {
1694 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001695 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001696 }
1697
1698 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001699 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1700 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001701 if (ret) {
1702 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001703 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001704 }
1705
1706 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001707 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001708 dwc3_ep0_out_start(dwc);
1709
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001710 dwc3_gadget_enable_irq(dwc);
1711
Felipe Balbid7be2952016-05-04 15:49:37 +03001712 return 0;
1713
1714err1:
1715 __dwc3_gadget_ep_disable(dwc->eps[0]);
1716
1717err0:
1718 return ret;
1719}
1720
1721static int dwc3_gadget_start(struct usb_gadget *g,
1722 struct usb_gadget_driver *driver)
1723{
1724 struct dwc3 *dwc = gadget_to_dwc(g);
1725 unsigned long flags;
1726 int ret = 0;
1727 int irq;
1728
1729 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1730 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1731 IRQF_SHARED, "dwc3", dwc->ev_buf);
1732 if (ret) {
1733 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1734 irq, ret);
1735 goto err0;
1736 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001737 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001738
1739 spin_lock_irqsave(&dwc->lock, flags);
1740 if (dwc->gadget_driver) {
1741 dev_err(dwc->dev, "%s is already bound to %s\n",
1742 dwc->gadget.name,
1743 dwc->gadget_driver->driver.name);
1744 ret = -EBUSY;
1745 goto err1;
1746 }
1747
1748 dwc->gadget_driver = driver;
1749
Felipe Balbifc8bb912016-05-16 13:14:48 +03001750 if (pm_runtime_active(dwc->dev))
1751 __dwc3_gadget_start(dwc);
1752
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 spin_unlock_irqrestore(&dwc->lock, flags);
1754
1755 return 0;
1756
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001757err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001759 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001760
1761err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001762 return ret;
1763}
1764
Felipe Balbid7be2952016-05-04 15:49:37 +03001765static void __dwc3_gadget_stop(struct dwc3 *dwc)
1766{
1767 dwc3_gadget_disable_irq(dwc);
1768 __dwc3_gadget_ep_disable(dwc->eps[0]);
1769 __dwc3_gadget_ep_disable(dwc->eps[1]);
1770}
1771
Felipe Balbi22835b82014-10-17 12:05:12 -05001772static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001773{
1774 struct dwc3 *dwc = gadget_to_dwc(g);
1775 unsigned long flags;
1776
1777 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001778 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001779 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 spin_unlock_irqrestore(&dwc->lock, flags);
1781
Felipe Balbi3f308d12016-05-16 14:17:06 +03001782 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001783
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 return 0;
1785}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001786
Felipe Balbi72246da2011-08-19 18:10:58 +03001787static const struct usb_gadget_ops dwc3_gadget_ops = {
1788 .get_frame = dwc3_gadget_get_frame,
1789 .wakeup = dwc3_gadget_wakeup,
1790 .set_selfpowered = dwc3_gadget_set_selfpowered,
1791 .pullup = dwc3_gadget_pullup,
1792 .udc_start = dwc3_gadget_start,
1793 .udc_stop = dwc3_gadget_stop,
1794};
1795
1796/* -------------------------------------------------------------------------- */
1797
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001798static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1799 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001800{
1801 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001802 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001803
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001804 for (i = 0; i < num; i++) {
1805 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001806
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001808 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001809 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001810
1811 dep->dwc = dwc;
1812 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001813 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001814 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001815 dwc->eps[epnum] = dep;
1816
1817 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1818 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001819
Felipe Balbi72246da2011-08-19 18:10:58 +03001820 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03001821 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001822
Felipe Balbi73815282015-01-27 13:48:14 -06001823 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001824
Felipe Balbi72246da2011-08-19 18:10:58 +03001825 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001826 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301827 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001828 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1829 if (!epnum)
1830 dwc->gadget.ep0 = &dep->endpoint;
1831 } else {
1832 int ret;
1833
Robert Baldygae117e742013-12-13 12:23:38 +01001834 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001835 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1837 list_add_tail(&dep->endpoint.ep_list,
1838 &dwc->gadget.ep_list);
1839
1840 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001841 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001842 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001843 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001844
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001845 if (epnum == 0 || epnum == 1) {
1846 dep->endpoint.caps.type_control = true;
1847 } else {
1848 dep->endpoint.caps.type_iso = true;
1849 dep->endpoint.caps.type_bulk = true;
1850 dep->endpoint.caps.type_int = true;
1851 }
1852
1853 dep->endpoint.caps.dir_in = !!direction;
1854 dep->endpoint.caps.dir_out = !direction;
1855
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001856 INIT_LIST_HEAD(&dep->pending_list);
1857 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 }
1859
1860 return 0;
1861}
1862
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001863static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1864{
1865 int ret;
1866
1867 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1868
1869 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1870 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001871 dwc3_trace(trace_dwc3_gadget,
1872 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001873 return ret;
1874 }
1875
1876 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1877 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001878 dwc3_trace(trace_dwc3_gadget,
1879 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001880 return ret;
1881 }
1882
1883 return 0;
1884}
1885
Felipe Balbi72246da2011-08-19 18:10:58 +03001886static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1887{
1888 struct dwc3_ep *dep;
1889 u8 epnum;
1890
1891 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1892 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001893 if (!dep)
1894 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301895 /*
1896 * Physical endpoints 0 and 1 are special; they form the
1897 * bi-directional USB endpoint 0.
1898 *
1899 * For those two physical endpoints, we don't allocate a TRB
1900 * pool nor do we add them the endpoints list. Due to that, we
1901 * shouldn't do these two operations otherwise we would end up
1902 * with all sorts of bugs when removing dwc3.ko.
1903 */
1904 if (epnum != 0 && epnum != 1) {
1905 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001906 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301907 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001908
1909 kfree(dep);
1910 }
1911}
1912
Felipe Balbi72246da2011-08-19 18:10:58 +03001913/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001914
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301915static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1916 struct dwc3_request *req, struct dwc3_trb *trb,
1917 const struct dwc3_event_depevt *event, int status)
1918{
1919 unsigned int count;
1920 unsigned int s_pkt = 0;
1921 unsigned int trb_status;
1922
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001923 trace_dwc3_complete_trb(dep, trb);
1924
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301925 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1926 /*
1927 * We continue despite the error. There is not much we
1928 * can do. If we don't clean it up we loop forever. If
1929 * we skip the TRB then it gets overwritten after a
1930 * while since we use them in a ring buffer. A BUG()
1931 * would help. Lets hope that if this occurs, someone
1932 * fixes the root cause instead of looking away :)
1933 */
1934 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1935 dep->name, trb);
1936 count = trb->size & DWC3_TRB_SIZE_MASK;
1937
1938 if (dep->direction) {
1939 if (count) {
1940 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1941 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001942 dwc3_trace(trace_dwc3_gadget,
1943 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301944 dep->name);
1945 /*
1946 * If missed isoc occurred and there is
1947 * no request queued then issue END
1948 * TRANSFER, so that core generates
1949 * next xfernotready and we will issue
1950 * a fresh START TRANSFER.
1951 * If there are still queued request
1952 * then wait, do not issue either END
1953 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001954 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301955 * giveback.If any future queued request
1956 * is successfully transferred then we
1957 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001958 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301959 */
1960 dep->flags |= DWC3_EP_MISSED_ISOC;
1961 } else {
1962 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1963 dep->name);
1964 status = -ECONNRESET;
1965 }
1966 } else {
1967 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1968 }
1969 } else {
1970 if (count && (event->status & DEPEVT_STATUS_SHORT))
1971 s_pkt = 1;
1972 }
1973
1974 /*
1975 * We assume here we will always receive the entire data block
1976 * which we should receive. Meaning, if we program RX to
1977 * receive 4K but we receive only 2K, we assume that's all we
1978 * should receive and we simply bounce the request back to the
1979 * gadget driver for further processing.
1980 */
1981 req->request.actual += req->request.length - count;
1982 if (s_pkt)
1983 return 1;
1984 if ((event->status & DEPEVT_STATUS_LST) &&
1985 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1986 DWC3_TRB_CTRL_HWO)))
1987 return 1;
1988 if ((event->status & DEPEVT_STATUS_IOC) &&
1989 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1990 return 1;
1991 return 0;
1992}
1993
Felipe Balbi72246da2011-08-19 18:10:58 +03001994static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1995 const struct dwc3_event_depevt *event, int status)
1996{
1997 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001998 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301999 unsigned int slot;
2000 unsigned int i;
2001 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002002
2003 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002004 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002005 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002006 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002007
Ville Syrjäläd115d702015-08-31 19:48:28 +03002008 i = 0;
2009 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002010 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002011 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002012 slot++;
2013 slot %= DWC3_TRB_NUM;
2014 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002015
Ville Syrjäläd115d702015-08-31 19:48:28 +03002016 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2017 event, status);
2018 if (ret)
2019 break;
2020 } while (++i < req->request.num_mapped_sgs);
2021
2022 dwc3_gadget_giveback(dep, req, status);
2023
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302024 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002025 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002026 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002027
Felipe Balbi4cb42212016-05-18 12:37:21 +03002028 /*
2029 * Our endpoint might get disabled by another thread during
2030 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2031 * early on so DWC3_EP_BUSY flag gets cleared
2032 */
2033 if (!dep->endpoint.desc)
2034 return 1;
2035
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302036 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002037 list_empty(&dep->started_list)) {
2038 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302039 /*
2040 * If there is no entry in request list then do
2041 * not issue END TRANSFER now. Just set PENDING
2042 * flag, so that END TRANSFER is issued when an
2043 * entry is added into request list.
2044 */
2045 dep->flags = DWC3_EP_PENDING_REQUEST;
2046 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002047 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302048 dep->flags = DWC3_EP_ENABLED;
2049 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302050 return 1;
2051 }
2052
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002053 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2054 if ((event->status & DEPEVT_STATUS_IOC) &&
2055 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2056 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002057 return 1;
2058}
2059
2060static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002061 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002062{
2063 unsigned status = 0;
2064 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002065 u32 is_xfer_complete;
2066
2067 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002068
2069 if (event->status & DEPEVT_STATUS_BUSERR)
2070 status = -ECONNRESET;
2071
Paul Zimmerman1d046792012-02-15 18:56:56 -08002072 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002073 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002074 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002075 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002076
2077 /*
2078 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2079 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2080 */
2081 if (dwc->revision < DWC3_REVISION_183A) {
2082 u32 reg;
2083 int i;
2084
2085 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002086 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002087
2088 if (!(dep->flags & DWC3_EP_ENABLED))
2089 continue;
2090
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002091 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002092 return;
2093 }
2094
2095 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2096 reg |= dwc->u1u2;
2097 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2098
2099 dwc->u1u2 = 0;
2100 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002101
Felipe Balbi4cb42212016-05-18 12:37:21 +03002102 /*
2103 * Our endpoint might get disabled by another thread during
2104 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2105 * early on so DWC3_EP_BUSY flag gets cleared
2106 */
2107 if (!dep->endpoint.desc)
2108 return;
2109
Felipe Balbie6e709b2015-09-28 15:16:56 -05002110 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002111 int ret;
2112
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002113 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002114 if (!ret || ret == -EBUSY)
2115 return;
2116 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002117}
2118
Felipe Balbi72246da2011-08-19 18:10:58 +03002119static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2120 const struct dwc3_event_depevt *event)
2121{
2122 struct dwc3_ep *dep;
2123 u8 epnum = event->endpoint_number;
2124
2125 dep = dwc->eps[epnum];
2126
Felipe Balbi3336abb2012-06-06 09:19:35 +03002127 if (!(dep->flags & DWC3_EP_ENABLED))
2128 return;
2129
Felipe Balbi72246da2011-08-19 18:10:58 +03002130 if (epnum == 0 || epnum == 1) {
2131 dwc3_ep0_interrupt(dwc, event);
2132 return;
2133 }
2134
2135 switch (event->endpoint_event) {
2136 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002137 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002138
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002139 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002140 dwc3_trace(trace_dwc3_gadget,
2141 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002142 dep->name);
2143 return;
2144 }
2145
Jingoo Han029d97f2014-07-04 15:00:51 +09002146 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002147 break;
2148 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002149 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 break;
2151 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002152 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 dwc3_gadget_start_isoc(dwc, dep, event);
2154 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002155 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 int ret;
2157
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002158 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2159
Felipe Balbi73815282015-01-27 13:48:14 -06002160 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002161 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002162 : "Transfer Not Active");
2163
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002164 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002165 if (!ret || ret == -EBUSY)
2166 return;
2167
Felipe Balbiec5e7952015-11-16 16:04:13 -06002168 dwc3_trace(trace_dwc3_gadget,
2169 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002170 dep->name);
2171 }
2172
2173 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002174 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002175 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002176 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2177 dep->name);
2178 return;
2179 }
2180
2181 switch (event->status) {
2182 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002183 dwc3_trace(trace_dwc3_gadget,
2184 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002185 event->parameters);
2186
2187 break;
2188 case DEPEVT_STREAMEVT_NOTFOUND:
2189 /* FALLTHROUGH */
2190 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002191 dwc3_trace(trace_dwc3_gadget,
2192 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002193 }
2194 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002196 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002198 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002199 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002200 break;
2201 }
2202}
2203
2204static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2205{
2206 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2207 spin_unlock(&dwc->lock);
2208 dwc->gadget_driver->disconnect(&dwc->gadget);
2209 spin_lock(&dwc->lock);
2210 }
2211}
2212
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002213static void dwc3_suspend_gadget(struct dwc3 *dwc)
2214{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002215 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002216 spin_unlock(&dwc->lock);
2217 dwc->gadget_driver->suspend(&dwc->gadget);
2218 spin_lock(&dwc->lock);
2219 }
2220}
2221
2222static void dwc3_resume_gadget(struct dwc3 *dwc)
2223{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002224 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002225 spin_unlock(&dwc->lock);
2226 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002227 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002228 }
2229}
2230
2231static void dwc3_reset_gadget(struct dwc3 *dwc)
2232{
2233 if (!dwc->gadget_driver)
2234 return;
2235
2236 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2237 spin_unlock(&dwc->lock);
2238 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002239 spin_lock(&dwc->lock);
2240 }
2241}
2242
Paul Zimmermanb992e682012-04-27 14:17:35 +03002243static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002244{
2245 struct dwc3_ep *dep;
2246 struct dwc3_gadget_ep_cmd_params params;
2247 u32 cmd;
2248 int ret;
2249
2250 dep = dwc->eps[epnum];
2251
Felipe Balbib4996a82012-06-06 12:04:13 +03002252 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302253 return;
2254
Pratyush Anand57911502012-07-06 15:19:10 +05302255 /*
2256 * NOTICE: We are violating what the Databook says about the
2257 * EndTransfer command. Ideally we would _always_ wait for the
2258 * EndTransfer Command Completion IRQ, but that's causing too
2259 * much trouble synchronizing between us and gadget driver.
2260 *
2261 * We have discussed this with the IP Provider and it was
2262 * suggested to giveback all requests here, but give HW some
2263 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002264 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302265 *
2266 * Note also that a similar handling was tested by Synopsys
2267 * (thanks a lot Paul) and nothing bad has come out of it.
2268 * In short, what we're doing is:
2269 *
2270 * - Issue EndTransfer WITH CMDIOC bit set
2271 * - Wait 100us
2272 */
2273
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302274 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002275 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2276 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002277 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302278 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002279 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302280 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002281 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002282 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302283 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002284}
2285
2286static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2287{
2288 u32 epnum;
2289
2290 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2291 struct dwc3_ep *dep;
2292
2293 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002294 if (!dep)
2295 continue;
2296
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 if (!(dep->flags & DWC3_EP_ENABLED))
2298 continue;
2299
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002300 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002301 }
2302}
2303
2304static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2305{
2306 u32 epnum;
2307
2308 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2309 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002310 int ret;
2311
2312 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002313 if (!dep)
2314 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002315
2316 if (!(dep->flags & DWC3_EP_STALL))
2317 continue;
2318
2319 dep->flags &= ~DWC3_EP_STALL;
2320
John Youn50c763f2016-05-31 17:49:56 -07002321 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002322 WARN_ON_ONCE(ret);
2323 }
2324}
2325
2326static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2327{
Felipe Balbic4430a22012-05-24 10:30:01 +03002328 int reg;
2329
Felipe Balbi72246da2011-08-19 18:10:58 +03002330 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2331 reg &= ~DWC3_DCTL_INITU1ENA;
2332 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2333
2334 reg &= ~DWC3_DCTL_INITU2ENA;
2335 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002336
Felipe Balbi72246da2011-08-19 18:10:58 +03002337 dwc3_disconnect_gadget(dwc);
2338
2339 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002340 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002341 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002342
2343 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002344}
2345
Felipe Balbi72246da2011-08-19 18:10:58 +03002346static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2347{
2348 u32 reg;
2349
Felipe Balbifc8bb912016-05-16 13:14:48 +03002350 dwc->connected = true;
2351
Felipe Balbidf62df52011-10-14 15:11:49 +03002352 /*
2353 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2354 * would cause a missing Disconnect Event if there's a
2355 * pending Setup Packet in the FIFO.
2356 *
2357 * There's no suggested workaround on the official Bug
2358 * report, which states that "unless the driver/application
2359 * is doing any special handling of a disconnect event,
2360 * there is no functional issue".
2361 *
2362 * Unfortunately, it turns out that we _do_ some special
2363 * handling of a disconnect event, namely complete all
2364 * pending transfers, notify gadget driver of the
2365 * disconnection, and so on.
2366 *
2367 * Our suggested workaround is to follow the Disconnect
2368 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002369 * flag. Such flag gets set whenever we have a SETUP_PENDING
2370 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002371 * same endpoint.
2372 *
2373 * Refers to:
2374 *
2375 * STAR#9000466709: RTL: Device : Disconnect event not
2376 * generated if setup packet pending in FIFO
2377 */
2378 if (dwc->revision < DWC3_REVISION_188A) {
2379 if (dwc->setup_packet_pending)
2380 dwc3_gadget_disconnect_interrupt(dwc);
2381 }
2382
Felipe Balbi8e744752014-11-06 14:27:53 +08002383 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002384
2385 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2386 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2387 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002388 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002389
2390 dwc3_stop_active_transfers(dwc);
2391 dwc3_clear_stall_all_ep(dwc);
2392
2393 /* Reset device address to zero */
2394 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2395 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2396 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002397}
2398
2399static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2400{
2401 u32 reg;
2402 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2403
2404 /*
2405 * We change the clock only at SS but I dunno why I would want to do
2406 * this. Maybe it becomes part of the power saving plan.
2407 */
2408
John Younee5cd412016-02-05 17:08:45 -08002409 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2410 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 return;
2412
2413 /*
2414 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2415 * each time on Connect Done.
2416 */
2417 if (!usb30_clock)
2418 return;
2419
2420 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2421 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2422 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2423}
2424
Felipe Balbi72246da2011-08-19 18:10:58 +03002425static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2426{
Felipe Balbi72246da2011-08-19 18:10:58 +03002427 struct dwc3_ep *dep;
2428 int ret;
2429 u32 reg;
2430 u8 speed;
2431
Felipe Balbi72246da2011-08-19 18:10:58 +03002432 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2433 speed = reg & DWC3_DSTS_CONNECTSPD;
2434 dwc->speed = speed;
2435
2436 dwc3_update_ram_clk_sel(dwc, speed);
2437
2438 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002439 case DWC3_DCFG_SUPERSPEED_PLUS:
2440 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2441 dwc->gadget.ep0->maxpacket = 512;
2442 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2443 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002444 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002445 /*
2446 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2447 * would cause a missing USB3 Reset event.
2448 *
2449 * In such situations, we should force a USB3 Reset
2450 * event by calling our dwc3_gadget_reset_interrupt()
2451 * routine.
2452 *
2453 * Refers to:
2454 *
2455 * STAR#9000483510: RTL: SS : USB3 reset event may
2456 * not be generated always when the link enters poll
2457 */
2458 if (dwc->revision < DWC3_REVISION_190A)
2459 dwc3_gadget_reset_interrupt(dwc);
2460
Felipe Balbi72246da2011-08-19 18:10:58 +03002461 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2462 dwc->gadget.ep0->maxpacket = 512;
2463 dwc->gadget.speed = USB_SPEED_SUPER;
2464 break;
2465 case DWC3_DCFG_HIGHSPEED:
2466 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2467 dwc->gadget.ep0->maxpacket = 64;
2468 dwc->gadget.speed = USB_SPEED_HIGH;
2469 break;
2470 case DWC3_DCFG_FULLSPEED2:
2471 case DWC3_DCFG_FULLSPEED1:
2472 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2473 dwc->gadget.ep0->maxpacket = 64;
2474 dwc->gadget.speed = USB_SPEED_FULL;
2475 break;
2476 case DWC3_DCFG_LOWSPEED:
2477 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2478 dwc->gadget.ep0->maxpacket = 8;
2479 dwc->gadget.speed = USB_SPEED_LOW;
2480 break;
2481 }
2482
Pratyush Anand2b758352013-01-14 15:59:31 +05302483 /* Enable USB2 LPM Capability */
2484
John Younee5cd412016-02-05 17:08:45 -08002485 if ((dwc->revision > DWC3_REVISION_194A) &&
2486 (speed != DWC3_DCFG_SUPERSPEED) &&
2487 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302488 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2489 reg |= DWC3_DCFG_LPM_CAP;
2490 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2491
2492 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2493 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2494
Huang Rui460d0982014-10-31 11:11:18 +08002495 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302496
Huang Rui80caf7d2014-10-28 19:54:26 +08002497 /*
2498 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2499 * DCFG.LPMCap is set, core responses with an ACK and the
2500 * BESL value in the LPM token is less than or equal to LPM
2501 * NYET threshold.
2502 */
2503 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2504 && dwc->has_lpm_erratum,
2505 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2506
2507 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2508 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2509
Pratyush Anand2b758352013-01-14 15:59:31 +05302510 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002511 } else {
2512 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2513 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2514 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302515 }
2516
Felipe Balbi72246da2011-08-19 18:10:58 +03002517 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002518 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2519 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002520 if (ret) {
2521 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2522 return;
2523 }
2524
2525 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002526 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2527 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002528 if (ret) {
2529 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2530 return;
2531 }
2532
2533 /*
2534 * Configure PHY via GUSB3PIPECTLn if required.
2535 *
2536 * Update GTXFIFOSIZn
2537 *
2538 * In both cases reset values should be sufficient.
2539 */
2540}
2541
2542static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2543{
Felipe Balbi72246da2011-08-19 18:10:58 +03002544 /*
2545 * TODO take core out of low power mode when that's
2546 * implemented.
2547 */
2548
Jiebing Liad14d4e2014-12-11 13:26:29 +08002549 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2550 spin_unlock(&dwc->lock);
2551 dwc->gadget_driver->resume(&dwc->gadget);
2552 spin_lock(&dwc->lock);
2553 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002554}
2555
2556static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2557 unsigned int evtinfo)
2558{
Felipe Balbifae2b902011-10-14 13:00:30 +03002559 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002560 unsigned int pwropt;
2561
2562 /*
2563 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2564 * Hibernation mode enabled which would show up when device detects
2565 * host-initiated U3 exit.
2566 *
2567 * In that case, device will generate a Link State Change Interrupt
2568 * from U3 to RESUME which is only necessary if Hibernation is
2569 * configured in.
2570 *
2571 * There are no functional changes due to such spurious event and we
2572 * just need to ignore it.
2573 *
2574 * Refers to:
2575 *
2576 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2577 * operational mode
2578 */
2579 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2580 if ((dwc->revision < DWC3_REVISION_250A) &&
2581 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2582 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2583 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002584 dwc3_trace(trace_dwc3_gadget,
2585 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002586 return;
2587 }
2588 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002589
2590 /*
2591 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2592 * on the link partner, the USB session might do multiple entry/exit
2593 * of low power states before a transfer takes place.
2594 *
2595 * Due to this problem, we might experience lower throughput. The
2596 * suggested workaround is to disable DCTL[12:9] bits if we're
2597 * transitioning from U1/U2 to U0 and enable those bits again
2598 * after a transfer completes and there are no pending transfers
2599 * on any of the enabled endpoints.
2600 *
2601 * This is the first half of that workaround.
2602 *
2603 * Refers to:
2604 *
2605 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2606 * core send LGO_Ux entering U0
2607 */
2608 if (dwc->revision < DWC3_REVISION_183A) {
2609 if (next == DWC3_LINK_STATE_U0) {
2610 u32 u1u2;
2611 u32 reg;
2612
2613 switch (dwc->link_state) {
2614 case DWC3_LINK_STATE_U1:
2615 case DWC3_LINK_STATE_U2:
2616 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2617 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2618 | DWC3_DCTL_ACCEPTU2ENA
2619 | DWC3_DCTL_INITU1ENA
2620 | DWC3_DCTL_ACCEPTU1ENA);
2621
2622 if (!dwc->u1u2)
2623 dwc->u1u2 = reg & u1u2;
2624
2625 reg &= ~u1u2;
2626
2627 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2628 break;
2629 default:
2630 /* do nothing */
2631 break;
2632 }
2633 }
2634 }
2635
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002636 switch (next) {
2637 case DWC3_LINK_STATE_U1:
2638 if (dwc->speed == USB_SPEED_SUPER)
2639 dwc3_suspend_gadget(dwc);
2640 break;
2641 case DWC3_LINK_STATE_U2:
2642 case DWC3_LINK_STATE_U3:
2643 dwc3_suspend_gadget(dwc);
2644 break;
2645 case DWC3_LINK_STATE_RESUME:
2646 dwc3_resume_gadget(dwc);
2647 break;
2648 default:
2649 /* do nothing */
2650 break;
2651 }
2652
Felipe Balbie57ebc12014-04-22 13:20:12 -05002653 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002654}
2655
Felipe Balbie1dadd32014-02-25 14:47:54 -06002656static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2657 unsigned int evtinfo)
2658{
2659 unsigned int is_ss = evtinfo & BIT(4);
2660
2661 /**
2662 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2663 * have a known issue which can cause USB CV TD.9.23 to fail
2664 * randomly.
2665 *
2666 * Because of this issue, core could generate bogus hibernation
2667 * events which SW needs to ignore.
2668 *
2669 * Refers to:
2670 *
2671 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2672 * Device Fallback from SuperSpeed
2673 */
2674 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2675 return;
2676
2677 /* enter hibernation here */
2678}
2679
Felipe Balbi72246da2011-08-19 18:10:58 +03002680static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2681 const struct dwc3_event_devt *event)
2682{
2683 switch (event->type) {
2684 case DWC3_DEVICE_EVENT_DISCONNECT:
2685 dwc3_gadget_disconnect_interrupt(dwc);
2686 break;
2687 case DWC3_DEVICE_EVENT_RESET:
2688 dwc3_gadget_reset_interrupt(dwc);
2689 break;
2690 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2691 dwc3_gadget_conndone_interrupt(dwc);
2692 break;
2693 case DWC3_DEVICE_EVENT_WAKEUP:
2694 dwc3_gadget_wakeup_interrupt(dwc);
2695 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002696 case DWC3_DEVICE_EVENT_HIBER_REQ:
2697 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2698 "unexpected hibernation event\n"))
2699 break;
2700
2701 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2702 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002703 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2704 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2705 break;
2706 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002707 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002708 break;
2709 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002710 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002711 break;
2712 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002713 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002714 break;
2715 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002716 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002717 break;
2718 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002719 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002720 break;
2721 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002722 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 }
2724}
2725
2726static void dwc3_process_event_entry(struct dwc3 *dwc,
2727 const union dwc3_event *event)
2728{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002729 trace_dwc3_event(event->raw);
2730
Felipe Balbi72246da2011-08-19 18:10:58 +03002731 /* Endpoint IRQ, handle it and return early */
2732 if (event->type.is_devspec == 0) {
2733 /* depevt */
2734 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2735 }
2736
2737 switch (event->type.type) {
2738 case DWC3_EVENT_TYPE_DEV:
2739 dwc3_gadget_interrupt(dwc, &event->devt);
2740 break;
2741 /* REVISIT what to do with Carkit and I2C events ? */
2742 default:
2743 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2744 }
2745}
2746
Felipe Balbidea520a2016-03-30 09:39:34 +03002747static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002748{
Felipe Balbidea520a2016-03-30 09:39:34 +03002749 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002750 irqreturn_t ret = IRQ_NONE;
2751 int left;
2752 u32 reg;
2753
Felipe Balbif42f2442013-06-12 21:25:08 +03002754 left = evt->count;
2755
2756 if (!(evt->flags & DWC3_EVENT_PENDING))
2757 return IRQ_NONE;
2758
2759 while (left > 0) {
2760 union dwc3_event event;
2761
2762 event.raw = *(u32 *) (evt->buf + evt->lpos);
2763
2764 dwc3_process_event_entry(dwc, &event);
2765
2766 /*
2767 * FIXME we wrap around correctly to the next entry as
2768 * almost all entries are 4 bytes in size. There is one
2769 * entry which has 12 bytes which is a regular entry
2770 * followed by 8 bytes data. ATM I don't know how
2771 * things are organized if we get next to the a
2772 * boundary so I worry about that once we try to handle
2773 * that.
2774 */
2775 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2776 left -= 4;
2777
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002778 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002779 }
2780
2781 evt->count = 0;
2782 evt->flags &= ~DWC3_EVENT_PENDING;
2783 ret = IRQ_HANDLED;
2784
2785 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002786 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002787 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002788 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002789
2790 return ret;
2791}
2792
Felipe Balbidea520a2016-03-30 09:39:34 +03002793static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002794{
Felipe Balbidea520a2016-03-30 09:39:34 +03002795 struct dwc3_event_buffer *evt = _evt;
2796 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002797 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002798 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002799
Felipe Balbie5f68b42015-10-12 13:25:44 -05002800 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002801 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002802 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002803
2804 return ret;
2805}
2806
Felipe Balbidea520a2016-03-30 09:39:34 +03002807static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002808{
Felipe Balbidea520a2016-03-30 09:39:34 +03002809 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002810 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002811 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002812
Felipe Balbifc8bb912016-05-16 13:14:48 +03002813 if (pm_runtime_suspended(dwc->dev)) {
2814 pm_runtime_get(dwc->dev);
2815 disable_irq_nosync(dwc->irq_gadget);
2816 dwc->pending_events = true;
2817 return IRQ_HANDLED;
2818 }
2819
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002820 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002821 count &= DWC3_GEVNTCOUNT_MASK;
2822 if (!count)
2823 return IRQ_NONE;
2824
Felipe Balbib15a7622011-06-30 16:57:15 +03002825 evt->count = count;
2826 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002827
Felipe Balbie8adfc32013-06-12 21:11:14 +03002828 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002829 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002830 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002831 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002832
Felipe Balbib15a7622011-06-30 16:57:15 +03002833 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002834}
2835
Felipe Balbidea520a2016-03-30 09:39:34 +03002836static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002837{
Felipe Balbidea520a2016-03-30 09:39:34 +03002838 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002839
Felipe Balbidea520a2016-03-30 09:39:34 +03002840 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002841}
2842
2843/**
2844 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002845 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002846 *
2847 * Returns 0 on success otherwise negative errno.
2848 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002849int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002850{
Felipe Balbi72246da2011-08-19 18:10:58 +03002851 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002852
2853 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2854 &dwc->ctrl_req_addr, GFP_KERNEL);
2855 if (!dwc->ctrl_req) {
2856 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2857 ret = -ENOMEM;
2858 goto err0;
2859 }
2860
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302861 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002862 &dwc->ep0_trb_addr, GFP_KERNEL);
2863 if (!dwc->ep0_trb) {
2864 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2865 ret = -ENOMEM;
2866 goto err1;
2867 }
2868
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002869 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002871 ret = -ENOMEM;
2872 goto err2;
2873 }
2874
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002875 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002876 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2877 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002878 if (!dwc->ep0_bounce) {
2879 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2880 ret = -ENOMEM;
2881 goto err3;
2882 }
2883
Felipe Balbi04c03d12015-12-02 10:06:45 -06002884 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2885 if (!dwc->zlp_buf) {
2886 ret = -ENOMEM;
2887 goto err4;
2888 }
2889
Felipe Balbi72246da2011-08-19 18:10:58 +03002890 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002891 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002892 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002893 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002894 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002895
2896 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002897 * FIXME We might be setting max_speed to <SUPER, however versions
2898 * <2.20a of dwc3 have an issue with metastability (documented
2899 * elsewhere in this driver) which tells us we can't set max speed to
2900 * anything lower than SUPER.
2901 *
2902 * Because gadget.max_speed is only used by composite.c and function
2903 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2904 * to happen so we avoid sending SuperSpeed Capability descriptor
2905 * together with our BOS descriptor as that could confuse host into
2906 * thinking we can handle super speed.
2907 *
2908 * Note that, in fact, we won't even support GetBOS requests when speed
2909 * is less than super speed because we don't have means, yet, to tell
2910 * composite.c that we are USB 2.0 + LPM ECN.
2911 */
2912 if (dwc->revision < DWC3_REVISION_220A)
2913 dwc3_trace(trace_dwc3_gadget,
2914 "Changing max_speed on rev %08x\n",
2915 dwc->revision);
2916
2917 dwc->gadget.max_speed = dwc->maximum_speed;
2918
2919 /*
David Cohena4b9d942013-12-09 15:55:38 -08002920 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2921 * on ep out.
2922 */
2923 dwc->gadget.quirk_ep_out_aligned_size = true;
2924
2925 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002926 * REVISIT: Here we should clear all pending IRQs to be
2927 * sure we're starting from a well known location.
2928 */
2929
2930 ret = dwc3_gadget_init_endpoints(dwc);
2931 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002932 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002933
Felipe Balbi72246da2011-08-19 18:10:58 +03002934 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2935 if (ret) {
2936 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002937 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002938 }
2939
2940 return 0;
2941
Felipe Balbi04c03d12015-12-02 10:06:45 -06002942err5:
2943 kfree(dwc->zlp_buf);
2944
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002945err4:
David Cohene1f80462013-09-11 17:42:47 -07002946 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002947 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2948 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002949
Felipe Balbi72246da2011-08-19 18:10:58 +03002950err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002951 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002952
2953err2:
2954 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2955 dwc->ep0_trb, dwc->ep0_trb_addr);
2956
2957err1:
2958 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2959 dwc->ctrl_req, dwc->ctrl_req_addr);
2960
2961err0:
2962 return ret;
2963}
2964
Felipe Balbi7415f172012-04-30 14:56:33 +03002965/* -------------------------------------------------------------------------- */
2966
Felipe Balbi72246da2011-08-19 18:10:58 +03002967void dwc3_gadget_exit(struct dwc3 *dwc)
2968{
Felipe Balbi72246da2011-08-19 18:10:58 +03002969 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002970
Felipe Balbi72246da2011-08-19 18:10:58 +03002971 dwc3_gadget_free_endpoints(dwc);
2972
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002973 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2974 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002975
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002976 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002977 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002978
2979 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2980 dwc->ep0_trb, dwc->ep0_trb_addr);
2981
2982 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2983 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002984}
Felipe Balbi7415f172012-04-30 14:56:33 +03002985
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002986int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002987{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002988 int ret;
2989
Roger Quadros9772b472016-04-12 11:33:29 +03002990 if (!dwc->gadget_driver)
2991 return 0;
2992
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002993 ret = dwc3_gadget_run_stop(dwc, false, false);
2994 if (ret < 0)
2995 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002996
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002997 dwc3_disconnect_gadget(dwc);
2998 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002999
3000 return 0;
3001}
3002
3003int dwc3_gadget_resume(struct dwc3 *dwc)
3004{
Felipe Balbi7415f172012-04-30 14:56:33 +03003005 int ret;
3006
Roger Quadros9772b472016-04-12 11:33:29 +03003007 if (!dwc->gadget_driver)
3008 return 0;
3009
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003010 ret = __dwc3_gadget_start(dwc);
3011 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003012 goto err0;
3013
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003014 ret = dwc3_gadget_run_stop(dwc, true, false);
3015 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003016 goto err1;
3017
Felipe Balbi7415f172012-04-30 14:56:33 +03003018 return 0;
3019
3020err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003021 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003022
3023err0:
3024 return ret;
3025}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003026
3027void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3028{
3029 if (dwc->pending_events) {
3030 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3031 dwc->pending_events = false;
3032 enable_irq(dwc->irq_gadget);
3033 }
3034}