blob: 70b3a665b8164dcf5c97c37b509daa6f357d65b3 [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>
Mayank Ranaa99689a2016-08-10 17:39:47 -070025#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/list.h>
29#include <linux/dma-mapping.h>
30
31#include <linux/usb/ch9.h>
Mayank Ranaa99689a2016-08-10 17:39:47 -070032#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030033#include <linux/usb/gadget.h>
34
Felipe Balbi80977dc2014-08-19 16:37:22 -050035#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030036#include "core.h"
37#include "gadget.h"
38#include "io.h"
39
Mayank Ranaa99689a2016-08-10 17:39:47 -070040static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup);
41static int dwc3_gadget_wakeup_int(struct dwc3 *dwc);
Mayank Rana9ce8d0f2018-02-22 12:04:42 -080042static void dwc3_stop_active_transfers(struct dwc3 *dwc);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020043/**
44 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
45 * @dwc: pointer to our context structure
46 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
47 *
48 * Caller should take care of locking. This function will
49 * return 0 on success or -EINVAL if wrong Test Selector
50 * is passed
51 */
52int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
53{
54 u32 reg;
55
56 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
57 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
58
59 switch (mode) {
60 case TEST_J:
61 case TEST_K:
62 case TEST_SE0_NAK:
63 case TEST_PACKET:
64 case TEST_FORCE_EN:
65 reg |= mode << 1;
66 break;
67 default:
68 return -EINVAL;
69 }
70
71 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72
73 return 0;
74}
75
Felipe Balbi8598bde2012-01-02 18:55:57 +020076/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030077 * dwc3_gadget_get_link_state - Gets current state of USB Link
78 * @dwc: pointer to our context structure
79 *
80 * Caller should take care of locking. This function will
81 * return the link state on success (>= 0) or -ETIMEDOUT.
82 */
83int dwc3_gadget_get_link_state(struct dwc3 *dwc)
84{
85 u32 reg;
86
87 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
88
89 return DWC3_DSTS_USBLNKST(reg);
90}
91
92/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020093 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
94 * @dwc: pointer to our context structure
95 * @state: the state to put link into
96 *
97 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080098 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020099 */
100int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
101{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800102 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200103 u32 reg;
104
Paul Zimmerman802fde92012-04-27 13:10:52 +0300105 /*
106 * Wait until device controller is ready. Only applies to 1.94a and
107 * later RTL.
108 */
109 if (dwc->revision >= DWC3_REVISION_194A) {
110 while (--retries) {
111 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
112 if (reg & DWC3_DSTS_DCNRD)
113 udelay(5);
114 else
115 break;
116 }
117
118 if (retries <= 0)
119 return -ETIMEDOUT;
120 }
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
123 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
124
125 /* set requested state */
126 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
127 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
128
Paul Zimmerman802fde92012-04-27 13:10:52 +0300129 /*
130 * The following code is racy when called from dwc3_gadget_wakeup,
131 * and is not needed, at least on newer versions
132 */
133 if (dwc->revision >= DWC3_REVISION_194A)
134 return 0;
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300137 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200138 while (--retries) {
139 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
140
Felipe Balbi8598bde2012-01-02 18:55:57 +0200141 if (DWC3_DSTS_USBLNKST(reg) == state)
142 return 0;
143
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800144 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200145 }
146
Felipe Balbi73815282015-01-27 13:48:14 -0600147 dwc3_trace(trace_dwc3_gadget,
148 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200149
150 return -ETIMEDOUT;
151}
152
John Youndca01192016-05-19 17:26:05 -0700153/**
154 * dwc3_ep_inc_trb() - Increment a TRB index.
155 * @index - Pointer to the TRB index to increment.
156 *
157 * The index should never point to the link TRB. After incrementing,
158 * if it is point to the link TRB, wrap around to the beginning. The
159 * link TRB is always at the last TRB entry.
160 */
161static void dwc3_ep_inc_trb(u8 *index)
162{
163 (*index)++;
164 if (*index == (DWC3_TRB_NUM - 1))
165 *index = 0;
166}
167
Mayank Rana9ca186c2017-06-19 17:57:21 -0700168void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200169{
John Youndca01192016-05-19 17:26:05 -0700170 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300171}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200172
Mayank Rana9ca186c2017-06-19 17:57:21 -0700173void dwc3_ep_inc_deq(struct dwc3_ep *dep)
Felipe Balbief966b92016-04-05 13:09:51 +0300174{
John Youndca01192016-05-19 17:26:05 -0700175 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200176}
177
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800178/*
179 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
180 * @dwc: pointer to our context structure
181 *
182 * This function will a best effort FIFO allocation in order
183 * to improve FIFO usage and throughput, while still allowing
184 * us to enable as many endpoints as possible.
185 *
186 * Keep in mind that this operation will be highly dependent
187 * on the configured size for RAM1 - which contains TxFifo -,
188 * the amount of endpoints enabled on coreConsultant tool, and
189 * the width of the Master Bus.
190 *
191 * In the ideal world, we would always be able to satisfy the
192 * following equation:
193 *
194 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
195 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
196 *
197 * Unfortunately, due to many variables that's not always the case.
198 */
Mayank Ranaac1200c2017-04-25 13:48:46 -0700199int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc, struct dwc3_ep *dep)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800200{
Mayank Ranaac1200c2017-04-25 13:48:46 -0700201 int fifo_size, mdwidth, max_packet = 1024;
202 int tmp, mult = 1;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800203
Mayank Ranaac1200c2017-04-25 13:48:46 -0700204 if (!dwc->needs_fifo_resize)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800205 return 0;
206
Mayank Ranaac1200c2017-04-25 13:48:46 -0700207 /* resize IN endpoints excepts ep0 */
208 if (!usb_endpoint_dir_in(dep->endpoint.desc) ||
209 dep->endpoint.ep_num == 0)
210 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800211
Mayank Ranaac1200c2017-04-25 13:48:46 -0700212 /* Don't resize already resized IN endpoint */
213 if (dep->fifo_depth) {
214 dev_dbg(dwc->dev, "%s fifo_depth:%d is already set\n",
215 dep->endpoint.name, dep->fifo_depth);
216 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800217 }
218
Mayank Ranaac1200c2017-04-25 13:48:46 -0700219 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
220 /* MDWIDTH is represented in bits, we need it in bytes */
221 mdwidth >>= 3;
222
223 if (dep->endpoint.ep_type == EP_TYPE_GSI || dep->endpoint.endless)
224 mult = 3;
225
226 if (((dep->endpoint.maxburst > 1) &&
227 usb_endpoint_xfer_bulk(dep->endpoint.desc))
228 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
229 mult = 3;
230
231 tmp = ((max_packet + mdwidth) * mult) + mdwidth;
232 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
233 dep->fifo_depth = fifo_size;
Ajay Agarwal8df9b9d2017-11-01 11:20:03 +0530234 fifo_size |= (dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)) & 0xffff0000)
235 + (dwc->last_fifo_depth << 16);
Mayank Ranaac1200c2017-04-25 13:48:46 -0700236 dwc->last_fifo_depth += (fifo_size & 0xffff);
237
238 dev_dbg(dwc->dev, "%s ep_num:%d last_fifo_depth:%04x fifo_depth:%d\n",
239 dep->endpoint.name, dep->endpoint.ep_num, dwc->last_fifo_depth,
240 dep->fifo_depth);
241
242 dbg_event(0xFF, "resize_fifo", dep->number);
243 dbg_event(0xFF, "fifo_depth", dep->fifo_depth);
244 /* Check fifo size allocation doesn't exceed available RAM size. */
245 if (dwc->tx_fifo_size &&
246 ((dwc->last_fifo_depth * mdwidth) >= dwc->tx_fifo_size)) {
247 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
248 (dwc->last_fifo_depth * mdwidth), dwc->tx_fifo_size,
249 dep->endpoint.name, fifo_size);
250 dwc->last_fifo_depth -= (fifo_size & 0xffff);
251 dep->fifo_depth = 0;
252 WARN_ON(1);
253 return -ENOMEM;
254 }
255
256 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->endpoint.ep_num),
257 fifo_size);
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800258 return 0;
259}
260
Felipe Balbi72246da2011-08-19 18:10:58 +0300261void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
262 int status)
263{
264 struct dwc3 *dwc = dep->dwc;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200265 unsigned int unmap_after_complete = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300266
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300267 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300268 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200269 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300270
271 if (req->request.status == -EINPROGRESS)
272 req->request.status = status;
273
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200274 /*
275 * NOTICE we don't want to unmap before calling ->complete() if we're
276 * dealing with a bounced ep0 request. If we unmap it here, we would end
277 * up overwritting the contents of req->buf and this could confuse the
278 * gadget driver.
279 */
280 if (dwc->ep0_bounced && dep->number <= 1) {
Pratyush Anand0416e492012-08-10 13:42:16 +0530281 dwc->ep0_bounced = false;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200282 unmap_after_complete = true;
283 } else {
Mayank Ranabd17b852017-08-25 10:38:30 -0700284 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200285 &req->request, req->direction);
286 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500288 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289
290 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200291 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300293
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200294 if (unmap_after_complete)
Mayank Ranabd17b852017-08-25 10:38:30 -0700295 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200296 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300297}
298
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500299int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300300{
301 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300302 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300303 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300304 u32 reg;
305
306 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
307 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
308
309 do {
310 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
311 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300312 status = DWC3_DGCMD_STATUS(reg);
313 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300314 ret = -EINVAL;
315 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300316 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300317 } while (timeout--);
318
319 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300320 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300321 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300322 }
323
Felipe Balbi71f7e702016-05-23 14:16:19 +0300324 trace_dwc3_gadget_generic_cmd(cmd, param, status);
325
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300326 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300327}
328
Felipe Balbic36d8e92016-04-04 12:46:33 +0300329static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
330
Felipe Balbi2cd47182016-04-12 16:42:43 +0300331int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
332 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300333{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300334 struct dwc3 *dwc = dep->dwc;
Hemant Kumar43874172016-08-25 16:17:48 -0700335 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 u32 reg;
337
Felipe Balbi0933df12016-05-23 14:02:33 +0300338 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300339 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300340 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300341
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300342 /*
343 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
344 * we're issuing an endpoint command, we must check if
345 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
346 *
347 * We will also set SUSPHY bit to what it was before returning as stated
348 * by the same section on Synopsys databook.
349 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300350 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
351 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
352 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
353 susphy = true;
354 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
355 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
356 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300357 }
358
Felipe Balbia81d6b72016-09-22 12:25:28 +0300359 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300360 int needs_wakeup;
361
362 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
363 dwc->link_state == DWC3_LINK_STATE_U2 ||
364 dwc->link_state == DWC3_LINK_STATE_U3);
365
366 if (unlikely(needs_wakeup)) {
367 ret = __dwc3_gadget_wakeup(dwc);
368 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
369 ret);
370 }
371 }
372
Felipe Balbi2eb88012016-04-12 16:53:39 +0300373 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
374 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
375 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
Felipe Balbi2eb88012016-04-12 16:53:39 +0300377 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300379 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300380 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300381 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000382
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000383 switch (cmd_status) {
384 case 0:
385 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300386 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000387 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000388 ret = -EINVAL;
389 break;
390 case DEPEVT_TRANSFER_BUS_EXPIRY:
391 /*
392 * SW issues START TRANSFER command to
393 * isochronous ep with future frame interval. If
394 * future interval time has already passed when
395 * core receives the command, it will respond
396 * with an error status of 'Bus Expiry'.
397 *
398 * Instead of always returning -EINVAL, let's
399 * give a hint to the gadget driver that this is
400 * the case by returning -EAGAIN.
401 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000402 ret = -EAGAIN;
403 break;
404 default:
405 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
406 }
407
Felipe Balbic0ca3242016-04-04 09:11:51 +0300408 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300409 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300410 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
Felipe Balbif6bb2252016-05-23 13:53:34 +0300412 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300413 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700414 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
415 dev_err(dwc->dev, "%s command timeout for %s\n",
416 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Chandana Kishori Chiluverub3f6d342018-04-30 12:25:44 +0530417 if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_ENDTRANSFER) {
Hemant Kumar43874172016-08-25 16:17:48 -0700418 dwc->ep_cmd_timeout_cnt++;
419 dwc3_notify_event(dwc,
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +0530420 DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Hemant Kumar43874172016-08-25 16:17:48 -0700421 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300422 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300423 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300424
Felipe Balbi0933df12016-05-23 14:02:33 +0300425 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
426
Felipe Balbicf23b5b2016-10-21 13:07:09 +0300427 if (ret == 0) {
428 switch (DWC3_DEPCMD_CMD(cmd)) {
429 case DWC3_DEPCMD_STARTTRANSFER:
430 dep->flags |= DWC3_EP_TRANSFER_STARTED;
431 break;
432 case DWC3_DEPCMD_ENDTRANSFER:
433 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
434 break;
435 default:
436 /* nothing */
437 break;
438 }
439 }
440
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300441 if (unlikely(susphy)) {
442 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
443 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
444 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
445 }
446
Felipe Balbic0ca3242016-04-04 09:11:51 +0300447 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300448}
449
John Youn50c763f2016-05-31 17:49:56 -0700450static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
451{
452 struct dwc3 *dwc = dep->dwc;
453 struct dwc3_gadget_ep_cmd_params params;
454 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
455
456 /*
457 * As of core revision 2.60a the recommended programming model
458 * is to set the ClearPendIN bit when issuing a Clear Stall EP
459 * command for IN endpoints. This is to prevent an issue where
460 * some (non-compliant) hosts may not send ACK TPs for pending
461 * IN transfers due to a mishandled error condition. Synopsys
462 * STAR 9000614252.
463 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800464 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
465 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700466 cmd |= DWC3_DEPCMD_CLEARPENDIN;
467
468 memset(&params, 0, sizeof(params));
469
Felipe Balbi2cd47182016-04-12 16:42:43 +0300470 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700471}
472
Felipe Balbi72246da2011-08-19 18:10:58 +0300473static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
474{
475 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700476 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300477
478 if (dep->trb_pool)
479 return 0;
480
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530481 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700482 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 &dep->trb_pool_dma, GFP_KERNEL);
484 if (!dep->trb_pool) {
485 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
486 dep->name);
487 return -ENOMEM;
488 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700489 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490
491 return 0;
492}
493
494static void dwc3_free_trb_pool(struct dwc3_ep *dep)
495{
496 struct dwc3 *dwc = dep->dwc;
497
Mayank Ranaa99689a2016-08-10 17:39:47 -0700498 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
499 if (dep->endpoint.ep_type == EP_TYPE_GSI)
500 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300501
Mayank Rana4dd882c2016-10-05 09:43:05 -0700502 /*
503 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
504 * with HWO bit set from previous composition when update transfer cmd
505 * is issued.
506 */
507 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
508 memset(&dep->trb_pool[0], 0,
509 sizeof(struct dwc3_trb) * dep->num_trbs);
Mayank Rana558baca2017-02-17 11:46:38 -0800510 dbg_event(dep->number, "Clr_TRB", 0);
Mayank Rana4dd882c2016-10-05 09:43:05 -0700511 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
512
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530513 dma_free_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700514 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
515 dep->trb_pool, dep->trb_pool_dma);
516 dep->trb_pool = NULL;
517 dep->trb_pool_dma = 0;
518 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300519}
520
John Younc4509602016-02-16 20:10:53 -0800521static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
522
523/**
524 * dwc3_gadget_start_config - Configure EP resources
525 * @dwc: pointer to our controller context structure
526 * @dep: endpoint that is being enabled
527 *
528 * The assignment of transfer resources cannot perfectly follow the
529 * data book due to the fact that the controller driver does not have
530 * all knowledge of the configuration in advance. It is given this
531 * information piecemeal by the composite gadget framework after every
532 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
533 * programming model in this scenario can cause errors. For two
534 * reasons:
535 *
536 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
537 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
538 * multiple interfaces.
539 *
540 * 2) The databook does not mention doing more DEPXFERCFG for new
541 * endpoint on alt setting (8.1.6).
542 *
543 * The following simplified method is used instead:
544 *
545 * All hardware endpoints can be assigned a transfer resource and this
546 * setting will stay persistent until either a core reset or
547 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
548 * do DEPXFERCFG for every hardware endpoint as well. We are
549 * guaranteed that there are as many transfer resources as endpoints.
550 *
551 * This function is called for each endpoint when it is being enabled
552 * but is triggered only when called for EP0-out, which always happens
553 * first, and which should only happen in one of the above conditions.
554 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300555static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
556{
557 struct dwc3_gadget_ep_cmd_params params;
558 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800559 int i;
560 int ret;
561
562 if (dep->number)
563 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564
565 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800566 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567
Felipe Balbi2cd47182016-04-12 16:42:43 +0300568 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800569 if (ret)
570 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300571
John Younc4509602016-02-16 20:10:53 -0800572 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
573 struct dwc3_ep *dep = dwc->eps[i];
574
575 if (!dep)
576 continue;
577
578 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
579 if (ret)
580 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 }
582
583 return 0;
584}
585
586static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200587 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300588 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300589 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300590{
591 struct dwc3_gadget_ep_cmd_params params;
592
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300593 if (dev_WARN_ONCE(dwc->dev, modify && restore,
594 "Can't modify and restore\n"))
595 return -EINVAL;
596
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 memset(&params, 0x00, sizeof(params));
598
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300599 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900600 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
601
602 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800603 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300604 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300605 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900606 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300607
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300608 if (modify) {
609 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
610 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600611 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
612 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300613 } else {
614 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600615 }
616
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300617 if (usb_endpoint_xfer_control(desc))
618 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300619
620 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
621 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200623 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300624 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
625 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300626 dep->stream_capable = true;
627 }
628
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500629 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300630 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300631
632 /*
633 * We are doing 1:1 mapping for endpoints, meaning
634 * Physical Endpoints 2 maps to Logical Endpoint 2 and
635 * so on. We consider the direction bit as part of the physical
636 * endpoint number. So USB endpoint 0x81 is 0x03.
637 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300638 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300639
640 /*
641 * We must use the lower 16 TX FIFOs even though
642 * HW might have more
643 */
644 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300645 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300646
647 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300648 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 dep->interval = 1 << (desc->bInterval - 1);
650 }
651
Felipe Balbi2cd47182016-04-12 16:42:43 +0300652 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300653}
654
655static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
656{
657 struct dwc3_gadget_ep_cmd_params params;
658
659 memset(&params, 0x00, sizeof(params));
660
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300661 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300662
Felipe Balbi2cd47182016-04-12 16:42:43 +0300663 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
664 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300665}
666
667/**
668 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
669 * @dep: endpoint to be initialized
670 * @desc: USB Endpoint Descriptor
671 *
672 * Caller should take care of locking
673 */
674static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200675 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300676 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300677 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300678{
679 struct dwc3 *dwc = dep->dwc;
680 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300681 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300682
Felipe Balbi73815282015-01-27 13:48:14 -0600683 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300684
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 if (!(dep->flags & DWC3_EP_ENABLED)) {
Mayank Ranaac1200c2017-04-25 13:48:46 -0700686 dep->endpoint.desc = desc;
687 dep->comp_desc = comp_desc;
688 dep->type = usb_endpoint_type(desc);
689 ret = dwc3_gadget_resize_tx_fifos(dwc, dep);
690 if (ret) {
691 dep->endpoint.desc = NULL;
692 dep->comp_desc = NULL;
693 dep->type = 0;
694 return ret;
695 }
696
Felipe Balbi72246da2011-08-19 18:10:58 +0300697 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700698 if (ret) {
699 dev_err(dwc->dev, "start_config() failed for %s\n",
700 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300701 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700702 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300703 }
704
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300705 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600706 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700707 if (ret) {
708 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300709 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700710 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300711
712 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200713 struct dwc3_trb *trb_st_hw;
714 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 dep->flags |= DWC3_EP_ENABLED;
717
718 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
719 reg |= DWC3_DALEPENA_EP(dep->number);
720 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
721
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300722 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300723 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300724
John Youn0d257442016-05-19 17:26:08 -0700725 /* Initialize the TRB ring */
726 dep->trb_dequeue = 0;
727 dep->trb_enqueue = 0;
728 memset(dep->trb_pool, 0,
729 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
730
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300731 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300732 trb_st_hw = &dep->trb_pool[0];
733
Felipe Balbif6bafc62012-02-06 11:04:53 +0200734 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200735 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
736 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
737 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
738 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300739 }
740
741 return 0;
742}
743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300745{
746 struct dwc3_request *req;
747
Felipe Balbi0e146022016-06-21 10:32:02 +0300748 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300749
Felipe Balbi0e146022016-06-21 10:32:02 +0300750 /* - giveback all requests to gadget driver */
751 while (!list_empty(&dep->started_list)) {
752 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200753
Felipe Balbi0e146022016-06-21 10:32:02 +0300754 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200755 }
756
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200757 while (!list_empty(&dep->pending_list)) {
758 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300759
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200760 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300762}
763
764/**
765 * __dwc3_gadget_ep_disable - Disables a HW endpoint
766 * @dep: the endpoint to disable
767 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200768 * This function also removes requests which are currently processed ny the
769 * hardware and those which are not yet scheduled.
770 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300771 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300772static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
773{
774 struct dwc3 *dwc = dep->dwc;
775 u32 reg;
776
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500777 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
778
Mayank Ranaa99689a2016-08-10 17:39:47 -0700779 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
780 dwc3_remove_requests(dwc, dep);
781 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
782 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300783
Felipe Balbi687ef982014-04-16 10:30:33 -0500784 /* make sure HW endpoint isn't stalled */
785 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500786 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500787
Felipe Balbi72246da2011-08-19 18:10:58 +0300788 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
789 reg &= ~DWC3_DALEPENA_EP(dep->number);
790 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
791
Felipe Balbi879631a2011-09-30 10:58:47 +0300792 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200793 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200794 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300796 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300797
Mayank Ranaa99689a2016-08-10 17:39:47 -0700798 /* Keep GSI ep names with "-gsi" suffix */
799 if (!strnstr(dep->name, "gsi", 10)) {
800 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
801 dep->number >> 1,
802 (dep->number & 1) ? "in" : "out");
803 }
804
Felipe Balbi72246da2011-08-19 18:10:58 +0300805 return 0;
806}
807
808/* -------------------------------------------------------------------------- */
809
810static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
811 const struct usb_endpoint_descriptor *desc)
812{
813 return -EINVAL;
814}
815
816static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
817{
818 return -EINVAL;
819}
820
821/* -------------------------------------------------------------------------- */
822
823static int dwc3_gadget_ep_enable(struct usb_ep *ep,
824 const struct usb_endpoint_descriptor *desc)
825{
826 struct dwc3_ep *dep;
827 struct dwc3 *dwc;
828 unsigned long flags;
829 int ret;
830
831 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
832 pr_debug("dwc3: invalid parameters\n");
833 return -EINVAL;
834 }
835
836 if (!desc->wMaxPacketSize) {
837 pr_debug("dwc3: missing wMaxPacketSize\n");
838 return -EINVAL;
839 }
840
841 dep = to_dwc3_ep(ep);
842 dwc = dep->dwc;
843
Felipe Balbi95ca9612015-12-10 13:08:20 -0600844 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
845 "%s is already enabled\n",
846 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300847 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300848
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600850 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Mayank Rana558baca2017-02-17 11:46:38 -0800851 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 spin_unlock_irqrestore(&dwc->lock, flags);
853
854 return ret;
855}
856
857static int dwc3_gadget_ep_disable(struct usb_ep *ep)
858{
859 struct dwc3_ep *dep;
860 struct dwc3 *dwc;
861 unsigned long flags;
862 int ret;
863
864 if (!ep) {
865 pr_debug("dwc3: invalid parameters\n");
866 return -EINVAL;
867 }
868
869 dep = to_dwc3_ep(ep);
870 dwc = dep->dwc;
871
Felipe Balbi95ca9612015-12-10 13:08:20 -0600872 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
873 "%s is already disabled\n",
874 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300875 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300876
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -0800877 /* Keep GSI ep names with "-gsi" suffix */
878 if (!strnstr(dep->name, "gsi", 10)) {
879 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
880 dep->number >> 1,
881 (dep->number & 1) ? "in" : "out");
882 }
883
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 spin_lock_irqsave(&dwc->lock, flags);
885 ret = __dwc3_gadget_ep_disable(dep);
Mayank Rana558baca2017-02-17 11:46:38 -0800886 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300887 spin_unlock_irqrestore(&dwc->lock, flags);
888
889 return ret;
890}
891
892static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
893 gfp_t gfp_flags)
894{
895 struct dwc3_request *req;
896 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300897
898 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900899 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300900 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
902 req->epnum = dep->number;
903 req->dep = dep;
Mayank Rana7877b272017-06-19 18:03:22 -0700904 req->request.dma = DMA_ERROR_CODE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300905
Felipe Balbi68d34c82016-05-30 13:34:58 +0300906 dep->allocated_requests++;
907
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500908 trace_dwc3_alloc_request(req);
909
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 return &req->request;
911}
912
913static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
914 struct usb_request *request)
915{
916 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300917 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Felipe Balbi68d34c82016-05-30 13:34:58 +0300919 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500920 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300921 kfree(req);
922}
923
Felipe Balbi2c78c022016-08-12 13:13:10 +0300924static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
925
Felipe Balbic71fc372011-11-22 11:37:34 +0200926/**
927 * dwc3_prepare_one_trb - setup one TRB from one request
928 * @dep: endpoint for which this request is prepared
929 * @req: dwc3_request pointer
930 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200931static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200932 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300933 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200934{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200935 struct dwc3_trb *trb;
Felipe Balbi3666b622016-09-22 11:01:01 +0300936 struct dwc3 *dwc = dep->dwc;
937 struct usb_gadget *gadget = &dwc->gadget;
938 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200939
Vamsi Krishna Samavedam1ed5d3f2016-11-03 17:15:41 -0700940 dwc3_trace(trace_dwc3_gadget, "%s: req %pK dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200941 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300942 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530943
Felipe Balbi4faf7552016-04-05 13:14:31 +0300944 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200945
Felipe Balbieeb720f2011-11-28 12:46:59 +0200946 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200947 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200948 req->trb = trb;
949 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300950 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300951 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200952 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200953
Felipe Balbief966b92016-04-05 13:09:51 +0300954 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530955
Felipe Balbif6bafc62012-02-06 11:04:53 +0200956 trb->size = DWC3_TRB_SIZE_LENGTH(length);
957 trb->bpl = lower_32_bits(dma);
958 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200959
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200960 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200961 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200962 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200963 break;
964
965 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi3666b622016-09-22 11:01:01 +0300966 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi3666b622016-09-22 11:01:01 +0300968
Manu Gautam480fd4f2017-07-19 17:07:10 +0530969 /*
970 * USB Specification 2.0 Section 5.9.2 states that: "If
971 * there is only a single transaction in the microframe,
972 * only a DATA0 data packet PID is used. If there are
973 * two transactions per microframe, DATA1 is used for
974 * the first transaction data packet and DATA0 is used
975 * for the second transaction data packet. If there are
976 * three transactions per microframe, DATA2 is used for
977 * the first transaction data packet, DATA1 is used for
978 * the second, and DATA0 is used for the third."
979 *
980 * IOW, we should satisfy the following cases:
981 *
982 * 1) length <= maxpacket
983 * - DATA0
984 *
985 * 2) maxpacket < length <= (2 * maxpacket)
986 * - DATA1, DATA0
987 *
988 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
989 * - DATA2, DATA1, DATA0
990 */
Felipe Balbi3666b622016-09-22 11:01:01 +0300991 if (speed == USB_SPEED_HIGH) {
992 struct usb_ep *ep = &dep->endpoint;
Sriharsha Allenki7441c2d2017-12-06 09:49:34 +0530993 unsigned int mult = 2;
Manu Gautam480fd4f2017-07-19 17:07:10 +0530994 unsigned int maxp;
995
996 maxp = usb_endpoint_maxp(ep->desc) & 0x07ff;
997
998 if (length <= (2 * maxp))
999 mult--;
1000
1001 if (length <= maxp)
1002 mult--;
1003
1004 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi3666b622016-09-22 11:01:01 +03001005 }
1006 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301007 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi3666b622016-09-22 11:01:01 +03001008 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001009
1010 /* always enable Interrupt on Missed ISOC */
1011 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001012 break;
1013
1014 case USB_ENDPOINT_XFER_BULK:
1015 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001016 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001017 break;
1018 default:
1019 /*
1020 * This is only possible with faulty memory because we
1021 * checked it already :)
1022 */
1023 BUG();
1024 }
1025
Felipe Balbica4d44e2016-03-10 13:53:27 +02001026 /* always enable Continue on Short Packet */
Felipe Balbi66f37a92016-10-05 14:26:23 +03001027 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi2e37cdd2016-10-06 17:10:39 +03001028 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001029
Felipe Balbi66f37a92016-10-05 14:26:23 +03001030 if (req->request.short_not_ok)
1031 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1032 }
1033
Felipe Balbi2c78c022016-08-12 13:13:10 +03001034 if ((!req->request.no_interrupt && !chain) ||
1035 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbi66f37a92016-10-05 14:26:23 +03001036 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001037
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301038 if (chain)
1039 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1040
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001041 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +02001042 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
1043
1044 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001045
1046 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001047}
1048
John Youn361572b2016-05-19 17:26:17 -07001049/**
1050 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
1051 * @dep: The endpoint with the TRB ring
1052 * @index: The index of the current TRB in the ring
1053 *
1054 * Returns the TRB prior to the one pointed to by the index. If the
1055 * index is 0, we will wrap backwards, skip the link TRB, and return
1056 * the one just before that.
1057 */
1058static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1059{
Felipe Balbi45438a02016-08-11 12:26:59 +03001060 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001061
Pratham Pratap2fcd96a2017-12-05 14:38:29 +05301062 if (!dep->trb_pool)
1063 return NULL;
1064
Felipe Balbi45438a02016-08-11 12:26:59 +03001065 if (!tmp)
1066 tmp = DWC3_TRB_NUM - 1;
1067
1068 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001069}
1070
Felipe Balbic4233572016-05-12 14:08:34 +03001071static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1072{
1073 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001074 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001075
1076 /*
1077 * If enqueue & dequeue are equal than it is either full or empty.
1078 *
1079 * One way to know for sure is if the TRB right before us has HWO bit
1080 * set or not. If it has, then we're definitely full and can't fit any
1081 * more transfers in our ring.
1082 */
1083 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001084 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Pratham Pratap4652d5a2018-03-15 10:31:20 +05301085 if (!tmp || tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001086 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001087
1088 return DWC3_TRB_NUM - 1;
1089 }
1090
John Youn9d7aba72016-08-26 18:43:01 -07001091 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001092 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001093
John Youn9d7aba72016-08-26 18:43:01 -07001094 if (dep->trb_dequeue < dep->trb_enqueue)
1095 trbs_left--;
1096
John Youn32db3d92016-05-19 17:26:12 -07001097 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001098}
1099
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001100static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001101 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001102{
Felipe Balbi1f512112016-08-12 13:17:27 +03001103 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001104 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001105 unsigned int length;
1106 dma_addr_t dma;
1107 int i;
1108
Felipe Balbi1f512112016-08-12 13:17:27 +03001109 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001110 unsigned chain = true;
1111
1112 length = sg_dma_len(s);
1113 dma = sg_dma_address(s);
1114
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001115 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001116 chain = false;
1117
1118 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001119 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001120
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001121 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001122 break;
1123 }
1124}
1125
1126static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001127 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001128{
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001129 unsigned int length;
1130 dma_addr_t dma;
1131
1132 dma = req->request.dma;
1133 length = req->request.length;
1134
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001135 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001136 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001137}
1138
Felipe Balbi72246da2011-08-19 18:10:58 +03001139/*
1140 * dwc3_prepare_trbs - setup TRBs from requests
1141 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001142 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001143 * The function goes through the requests list and sets up TRBs for the
1144 * transfers. The function returns once there are no more TRBs available or
1145 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001146 */
Felipe Balbic4233572016-05-12 14:08:34 +03001147static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001148{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001149 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001150
1151 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1152
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001153 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001154 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001155
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001156 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001157 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001158 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001159 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001160 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001161
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001162 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001163 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001164 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001165}
1166
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001167static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001168{
1169 struct dwc3_gadget_ep_cmd_params params;
Sriharsha Allenkie881b352017-11-20 20:42:11 +05301170 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001171 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001172 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001173 int ret;
1174 u32 cmd;
1175
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001176 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001177
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001178 dwc3_prepare_trbs(dep);
1179 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001180 if (!req) {
1181 dep->flags |= DWC3_EP_PENDING_REQUEST;
Mayank Rana558baca2017-02-17 11:46:38 -08001182 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001183 return 0;
1184 }
1185
1186 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001187
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001188 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301189 params.param0 = upper_32_bits(req->trb_dma);
1190 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001191 cmd = DWC3_DEPCMD_STARTTRANSFER |
1192 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301193 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001194 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1195 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301196 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001197
Felipe Balbi2cd47182016-04-12 16:42:43 +03001198 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 if (ret < 0) {
Sriharsha Allenkie881b352017-11-20 20:42:11 +05301200 if ((ret == -EAGAIN) && starting &&
1201 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1202 /* If bit13 in Command complete event is set, software
1203 * must issue ENDTRANDFER command and wait for
1204 * Xfernotready event to queue the requests again.
1205 */
1206 if (!dep->resource_index) {
1207 dep->resource_index =
1208 dwc3_gadget_ep_get_transfer_index(dep);
1209 WARN_ON_ONCE(!dep->resource_index);
1210 }
1211 dwc3_stop_active_transfer(dwc, dep->number, true);
1212
1213 list_for_each_entry_safe_reverse(req1, n,
1214 &dep->started_list, list) {
1215 req1->trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1216 req1->trb = NULL;
1217 dwc3_gadget_move_pending_list_front(req1);
1218 dwc3_ep_inc_deq(dep);
1219 }
1220
1221 return ret;
1222 }
1223
Felipe Balbi72246da2011-08-19 18:10:58 +03001224 /*
1225 * FIXME we need to iterate over the list of requests
1226 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001227 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 */
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301229 usb_gadget_unmap_request_by_dev(dwc->sysdev,
1230 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001231 list_del(&req->list);
1232 return ret;
1233 }
1234
1235 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001236
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001237 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001238 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001239 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001240 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001241
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 return 0;
1243}
1244
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001245static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1246{
1247 u32 reg;
1248
1249 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1250 return DWC3_DSTS_SOFFN(reg);
1251}
1252
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301253static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1254 struct dwc3_ep *dep, u32 cur_uf)
1255{
1256 u32 uf;
Mayank Rana558baca2017-02-17 11:46:38 -08001257 int ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301258
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001259 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001260 dwc3_trace(trace_dwc3_gadget,
1261 "ISOC ep %s run out for requests",
1262 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301263 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301264 return;
1265 }
1266
John Youn1819d0a2017-01-26 11:58:40 -08001267 /*
1268 * Schedule the first trb for one interval in the future or at
1269 * least 4 microframes.
1270 */
Sriharsha Allenki45c070702017-11-22 16:55:16 +05301271 uf = cur_uf + max_t(u32, 16, dep->interval);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301272
Mayank Rana558baca2017-02-17 11:46:38 -08001273 ret = __dwc3_gadget_kick_transfer(dep, uf);
1274 if (ret < 0)
1275 dbg_event(dep->number, "ISOC QUEUE", ret);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301276}
1277
1278static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1279 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1280{
1281 u32 cur_uf, mask;
1282
1283 mask = ~(dep->interval - 1);
1284 cur_uf = event->parameters & mask;
1285
1286 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1287}
1288
Felipe Balbi72246da2011-08-19 18:10:58 +03001289static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1290{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001291 struct dwc3 *dwc = dep->dwc;
1292 int ret;
1293
Felipe Balbibb423982015-11-16 15:31:21 -06001294 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001295 dwc3_trace(trace_dwc3_gadget,
Vamsi Krishna Samavedam1ed5d3f2016-11-03 17:15:41 -07001296 "trying to queue request %pK to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001297 &req->request, dep->endpoint.name);
1298 return -ESHUTDOWN;
1299 }
1300
Felipe Balbi3272bad2017-05-17 15:57:45 +03001301 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001302 &req->request, req->dep->name)) {
Felipe Balbi3272bad2017-05-17 15:57:45 +03001303 dwc3_trace(trace_dwc3_gadget, "request %pK belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001304 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001305 return -EINVAL;
1306 }
1307
Manu Gautamb40ef612013-02-11 15:53:34 +05301308 if (req->request.status == -EINPROGRESS) {
1309 ret = -EBUSY;
Pratham Pratap886ddda2018-04-19 12:26:48 +05301310 dev_err_ratelimited(dwc->dev, "%s: %pK request already in queue",
Manu Gautamb40ef612013-02-11 15:53:34 +05301311 dep->name, req);
1312 return ret;
1313 }
Felipe Balbifc8bb912016-05-16 13:14:48 +03001314
Felipe Balbi72246da2011-08-19 18:10:58 +03001315 req->request.actual = 0;
1316 req->request.status = -EINPROGRESS;
1317 req->direction = dep->direction;
1318 req->epnum = dep->number;
1319
Felipe Balbife84f522015-09-01 09:01:38 -05001320 trace_dwc3_ep_queue(req);
1321
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301322 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1323 dep->direction);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001324 if (ret)
1325 return ret;
1326
Felipe Balbi1f512112016-08-12 13:17:27 +03001327 req->sg = req->request.sg;
1328 req->num_pending_sgs = req->request.num_mapped_sgs;
1329
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001330 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001331
Felipe Balbid889c232016-09-29 15:44:29 +03001332 /*
1333 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1334 * wait for a XferNotReady event so we will know what's the current
1335 * (micro-)frame number.
1336 *
1337 * Without this trick, we are very, very likely gonna get Bus Expiry
1338 * errors which will force us issue EndTransfer command.
1339 */
1340 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001341 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1342 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1343 dwc3_stop_active_transfer(dwc, dep->number, true);
1344 dep->flags = DWC3_EP_ENABLED;
1345 } else {
1346 u32 cur_uf;
1347
1348 cur_uf = __dwc3_gadget_get_frame(dwc);
1349 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzice7ab8972016-11-09 11:01:34 +01001350 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001351 }
Roger Quadros77d815d2017-04-21 15:58:08 +03001352 return 0;
Felipe Balbi08a36b52016-08-11 14:27:52 +03001353 }
Roger Quadros77d815d2017-04-21 15:58:08 +03001354
1355 if ((dep->flags & DWC3_EP_BUSY) &&
1356 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
1357 WARN_ON_ONCE(!dep->resource_index);
1358 ret = __dwc3_gadget_kick_transfer(dep,
1359 dep->resource_index);
1360 }
1361
1362 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001363 }
1364
Felipe Balbi594e1212016-08-24 14:38:10 +03001365 if (!dwc3_calc_trbs_left(dep))
1366 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001367
Felipe Balbi08a36b52016-08-11 14:27:52 +03001368 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001369 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001370 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001371 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001372 dep->name);
Roger Quadros77d815d2017-04-21 15:58:08 +03001373out:
Felipe Balbia8f32812015-09-16 10:40:07 -05001374 if (ret == -EBUSY)
1375 ret = 0;
1376
1377 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001378}
1379
Mayank Ranaa99689a2016-08-10 17:39:47 -07001380static int dwc3_gadget_wakeup(struct usb_gadget *g)
1381{
1382 struct dwc3 *dwc = gadget_to_dwc(g);
1383
1384 schedule_work(&dwc->wakeup_work);
1385 return 0;
1386}
1387
Mayank Ranaa99689a2016-08-10 17:39:47 -07001388static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1389{
1390 if (atomic_read(&dwc->in_lpm) ||
1391 dwc->link_state == DWC3_LINK_STATE_U3)
1392 return true;
1393 return false;
1394}
1395
Felipe Balbi04c03d12015-12-02 10:06:45 -06001396static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1397 struct usb_request *request)
1398{
1399 dwc3_gadget_ep_free_request(ep, request);
1400}
1401
1402static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1403{
1404 struct dwc3_request *req;
1405 struct usb_request *request;
1406 struct usb_ep *ep = &dep->endpoint;
1407
Felipe Balbi60cfb372016-05-24 13:45:17 +03001408 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001409 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1410 if (!request)
1411 return -ENOMEM;
1412
1413 request->length = 0;
1414 request->buf = dwc->zlp_buf;
1415 request->complete = __dwc3_gadget_ep_zlp_complete;
1416
1417 req = to_dwc3_request(request);
1418
1419 return __dwc3_gadget_ep_queue(dep, req);
1420}
1421
Felipe Balbi72246da2011-08-19 18:10:58 +03001422static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1423 gfp_t gfp_flags)
1424{
1425 struct dwc3_request *req = to_dwc3_request(request);
1426 struct dwc3_ep *dep = to_dwc3_ep(ep);
1427 struct dwc3 *dwc = dep->dwc;
1428
1429 unsigned long flags;
1430
1431 int ret;
1432
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001433 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001434 if (!dep->endpoint.desc) {
Vamsi Krishna Samavedam1ed5d3f2016-11-03 17:15:41 -07001435 dev_dbg(dwc->dev, "trying to queue request %pK to disabled %s\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07001436 request, ep->name);
1437 ret = -ESHUTDOWN;
1438 goto out;
1439 }
1440
Vamsi Krishna Samavedam1ed5d3f2016-11-03 17:15:41 -07001441 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07001442 request, req->dep->name)) {
1443 ret = -EINVAL;
1444 goto out;
1445 }
1446
1447 if (dwc3_gadget_is_suspended(dwc)) {
1448 if (dwc->gadget.remote_wakeup)
1449 dwc3_gadget_wakeup(&dwc->gadget);
1450 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1451 goto out;
1452 }
1453
Manu Gautam3df6a322017-03-06 16:24:59 -08001454 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1455 "trying to queue unaligned request (%d) with %s\n",
1456 request->length, ep->name);
1457
Felipe Balbi72246da2011-08-19 18:10:58 +03001458 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001459
1460 /*
1461 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1462 * setting request->zero, instead of doing magic, we will just queue an
1463 * extra usb_request ourselves so that it gets handled the same way as
1464 * any other request.
1465 */
John Yound92618982015-12-22 12:23:20 -08001466 if (ret == 0 && request->zero && request->length &&
1467 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001468 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1469
Mayank Ranaa99689a2016-08-10 17:39:47 -07001470out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 spin_unlock_irqrestore(&dwc->lock, flags);
1472
1473 return ret;
1474}
1475
1476static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1477 struct usb_request *request)
1478{
1479 struct dwc3_request *req = to_dwc3_request(request);
1480 struct dwc3_request *r = NULL;
1481
1482 struct dwc3_ep *dep = to_dwc3_ep(ep);
1483 struct dwc3 *dwc = dep->dwc;
1484
1485 unsigned long flags;
1486 int ret = 0;
1487
Mayank Ranaa99689a2016-08-10 17:39:47 -07001488 if (atomic_read(&dwc->in_lpm)) {
1489 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1490 return -EAGAIN;
1491 }
1492
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001493 trace_dwc3_ep_dequeue(req);
1494
Felipe Balbi72246da2011-08-19 18:10:58 +03001495 spin_lock_irqsave(&dwc->lock, flags);
1496
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001497 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001498 if (r == req)
1499 break;
1500 }
1501
1502 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001503 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001504 if (r == req)
1505 break;
1506 }
1507 if (r == req) {
1508 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001509 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbid1c93aa2017-03-08 13:56:37 -08001510
1511 /*
1512 * If request was already started, this means we had to
1513 * stop the transfer. With that we also need to ignore
1514 * all TRBs used by the request, however TRBs can only
1515 * be modified after completion of END_TRANSFER
1516 * command. So what we do here is that we wait for
1517 * END_TRANSFER completion and only after that, we jump
1518 * over TRBs by clearing HWO and incrementing dequeue
1519 * pointer.
1520 *
1521 * Note that we have 2 possible types of transfers here:
1522 *
1523 * i) Linear buffer request
1524 * ii) SG-list based request
1525 *
1526 * SG-list based requests will have r->num_pending_sgs
1527 * set to a valid number (> 0). Linear requests,
1528 * normally use a single TRB.
1529 *
1530 * All of these cases need to be taken into
1531 * consideration so we don't mess up our TRB ring
1532 * pointers.
1533 */
1534 if (!r->trb)
1535 goto out1;
1536
1537 if (r->num_pending_sgs) {
1538 struct dwc3_trb *trb;
1539 int i = 0;
1540
1541 for (i = 0; i < r->num_pending_sgs; i++) {
1542 trb = r->trb + i;
1543 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1544 dwc3_ep_inc_deq(dep);
1545 }
1546 } else {
1547 struct dwc3_trb *trb = r->trb;
1548
1549 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1550 dwc3_ep_inc_deq(dep);
1551 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301552 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001553 }
Felipe Balbi3272bad2017-05-17 15:57:45 +03001554 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 request, ep->name);
1556 ret = -EINVAL;
1557 goto out0;
1558 }
1559
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301560out1:
Mayank Rana558baca2017-02-17 11:46:38 -08001561 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001562 /* giveback the request */
Felipe Balbid1c93aa2017-03-08 13:56:37 -08001563 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001564 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1565
1566out0:
1567 spin_unlock_irqrestore(&dwc->lock, flags);
1568
1569 return ret;
1570}
1571
Felipe Balbi7a608552014-09-24 14:19:52 -05001572int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001573{
1574 struct dwc3_gadget_ep_cmd_params params;
1575 struct dwc3 *dwc = dep->dwc;
1576 int ret;
1577
Mayank Ranad223be42017-06-07 11:54:08 -07001578 if (!dep->endpoint.desc) {
1579 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1580 return -EINVAL;
1581 }
1582
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001583 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1584 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1585 return -EINVAL;
1586 }
1587
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 memset(&params, 0x00, sizeof(params));
Mayank Rana558baca2017-02-17 11:46:38 -08001589 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001590 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001591 struct dwc3_trb *trb;
1592
1593 unsigned transfer_in_flight;
1594 unsigned started;
1595
1596 if (dep->number > 1)
1597 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1598 else
1599 trb = &dwc->ep0_trb[dep->trb_enqueue];
1600
Pratham Pratap2fcd96a2017-12-05 14:38:29 +05301601 if (trb)
1602 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1603 else
1604 transfer_in_flight = false;
1605
Felipe Balbi69450c42016-05-30 13:37:02 +03001606 started = !list_empty(&dep->started_list);
1607
1608 if (!protocol && ((dep->direction && transfer_in_flight) ||
1609 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001610 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001611 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001612 dep->name);
1613 return -EAGAIN;
1614 }
1615
Felipe Balbi2cd47182016-04-12 16:42:43 +03001616 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1617 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001618 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001619 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001620 dep->name);
1621 else
1622 dep->flags |= DWC3_EP_STALL;
1623 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001624
John Youn50c763f2016-05-31 17:49:56 -07001625 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001626 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001627 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001628 dep->name);
1629 else
Alan Sterna535d812013-11-01 12:05:12 -04001630 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001631 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001632
Felipe Balbi72246da2011-08-19 18:10:58 +03001633 return ret;
1634}
1635
1636static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1637{
1638 struct dwc3_ep *dep = to_dwc3_ep(ep);
1639 struct dwc3 *dwc = dep->dwc;
1640
1641 unsigned long flags;
1642
1643 int ret;
1644
Mayank Ranaa99689a2016-08-10 17:39:47 -07001645 if (!ep->desc) {
1646 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1647 return -EINVAL;
1648 }
1649
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001651 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 spin_unlock_irqrestore(&dwc->lock, flags);
1653
1654 return ret;
1655}
1656
1657static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1658{
1659 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001660 struct dwc3 *dwc = dep->dwc;
1661 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001662 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001663
Paul Zimmerman249a4562012-02-24 17:32:16 -08001664 spin_lock_irqsave(&dwc->lock, flags);
Mayank Rana558baca2017-02-17 11:46:38 -08001665 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001666 dep->flags |= DWC3_EP_WEDGE;
1667
Pratyush Anand08f0d962012-06-25 22:40:43 +05301668 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001669 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301670 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001671 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001672 spin_unlock_irqrestore(&dwc->lock, flags);
1673
1674 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001675}
1676
1677/* -------------------------------------------------------------------------- */
1678
1679static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1680 .bLength = USB_DT_ENDPOINT_SIZE,
1681 .bDescriptorType = USB_DT_ENDPOINT,
1682 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1683};
1684
1685static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1686 .enable = dwc3_gadget_ep0_enable,
1687 .disable = dwc3_gadget_ep0_disable,
1688 .alloc_request = dwc3_gadget_ep_alloc_request,
1689 .free_request = dwc3_gadget_ep_free_request,
1690 .queue = dwc3_gadget_ep0_queue,
1691 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301692 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 .set_wedge = dwc3_gadget_ep_set_wedge,
1694};
1695
1696static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1697 .enable = dwc3_gadget_ep_enable,
1698 .disable = dwc3_gadget_ep_disable,
1699 .alloc_request = dwc3_gadget_ep_alloc_request,
1700 .free_request = dwc3_gadget_ep_free_request,
1701 .queue = dwc3_gadget_ep_queue,
1702 .dequeue = dwc3_gadget_ep_dequeue,
1703 .set_halt = dwc3_gadget_ep_set_halt,
1704 .set_wedge = dwc3_gadget_ep_set_wedge,
1705};
1706
1707/* -------------------------------------------------------------------------- */
1708
1709static int dwc3_gadget_get_frame(struct usb_gadget *g)
1710{
1711 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001712
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001713 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001714}
1715
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001716static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001717{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001718 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001719
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001720 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 u32 reg;
1722
Felipe Balbi72246da2011-08-19 18:10:58 +03001723 u8 link_state;
1724 u8 speed;
1725
Felipe Balbi72246da2011-08-19 18:10:58 +03001726 /*
1727 * According to the Databook Remote wakeup request should
1728 * be issued only when the device is in early suspend state.
1729 *
1730 * We can check that via USB Link State bits in DSTS register.
1731 */
1732 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1733
1734 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001735 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1736 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001737 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001738 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001739 }
1740
1741 link_state = DWC3_DSTS_USBLNKST(reg);
1742
1743 switch (link_state) {
1744 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1745 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1746 break;
1747 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001748 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001749 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001750 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001751 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 }
1753
Felipe Balbi8598bde2012-01-02 18:55:57 +02001754 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1755 if (ret < 0) {
1756 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001757 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001758 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001759
Paul Zimmerman802fde92012-04-27 13:10:52 +03001760 /* Recent versions do this automatically */
1761 if (dwc->revision < DWC3_REVISION_194A) {
1762 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001763 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001764 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1765 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1766 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001767
Paul Zimmerman1d046792012-02-15 18:56:56 -08001768 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001769 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001770
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001771 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1773
1774 /* in HS, means ON */
1775 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1776 break;
1777 }
1778
1779 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1780 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001781 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001782 }
1783
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001784 return 0;
1785}
1786
Mayank Ranaa99689a2016-08-10 17:39:47 -07001787#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1788#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1789
1790static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001791{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001792 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001793 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001794 static int retry_count;
1795
1796 dwc = container_of(w, struct dwc3, wakeup_work);
1797
1798 ret = pm_runtime_get_sync(dwc->dev);
1799 if (ret) {
1800 /* pm_runtime_get_sync returns -EACCES error between
1801 * late_suspend and early_resume, wait for system resume to
1802 * finish and queue work again
1803 */
1804 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1805 if (ret == -EACCES) {
1806 pm_runtime_put_noidle(dwc->dev);
1807 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1808 retry_count = 0;
1809 pr_err("pm_runtime_get_sync timed out\n");
1810 return;
1811 }
1812 msleep(DWC3_PM_RESUME_DELAY);
1813 retry_count++;
1814 schedule_work(&dwc->wakeup_work);
1815 return;
1816 }
1817 }
1818 retry_count = 0;
Mayank Rana558baca2017-02-17 11:46:38 -08001819 dbg_event(0xFF, "Gdgwake gsyn",
1820 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001821
1822 ret = dwc3_gadget_wakeup_int(dwc);
1823
1824 if (ret)
1825 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1826 else
1827 pr_debug("Remote wakeup succeeded.\n");
1828
1829 pm_runtime_put_noidle(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001830 dbg_event(0xFF, "Gdgwake put",
1831 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001832}
1833
1834static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1835{
1836 bool link_recover_only = false;
1837
1838 u32 reg;
1839 int ret = 0;
1840 u8 link_state;
1841 unsigned long flags;
1842
1843 pr_debug("%s(): Entry\n", __func__);
1844 disable_irq(dwc->irq);
1845 spin_lock_irqsave(&dwc->lock, flags);
1846 /*
1847 * According to the Databook Remote wakeup request should
1848 * be issued only when the device is in early suspend state.
1849 *
1850 * We can check that via USB Link State bits in DSTS register.
1851 */
1852 link_state = dwc3_get_link_state(dwc);
1853
1854 switch (link_state) {
1855 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1856 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1857 break;
1858 case DWC3_LINK_STATE_U1:
1859 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1860 link_recover_only = true;
1861 break;
1862 }
1863 /* Intentional fallthrough */
1864 default:
1865 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1866 link_state);
1867 ret = -EINVAL;
1868 goto out;
1869 }
1870
1871 /* Enable LINK STATUS change event */
1872 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1873 reg |= DWC3_DEVTEN_ULSTCNGEN;
1874 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1875 /*
1876 * memory barrier is required to make sure that required events
1877 * with core is enabled before performing RECOVERY mechnism.
1878 */
1879 mb();
1880
1881 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1882 if (ret < 0) {
1883 dev_err(dwc->dev, "failed to put link in Recovery\n");
1884 /* Disable LINK STATUS change */
1885 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1886 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1887 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1888 /* Required to complete this operation before returning */
1889 mb();
1890 goto out;
1891 }
1892
1893 /* Recent versions do this automatically */
1894 if (dwc->revision < DWC3_REVISION_194A) {
1895 /* write zeroes to Link Change Request */
1896 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1897 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1898 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1899 }
1900
1901 spin_unlock_irqrestore(&dwc->lock, flags);
1902 enable_irq(dwc->irq);
1903
1904 /*
1905 * Have bigger value (16 sec) for timeout since some host PCs driving
1906 * resume for very long time (e.g. 8 sec)
1907 */
1908 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1909 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1910 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1911 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001912
1913 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001914 /* Disable link status change event */
1915 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1916 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1917 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1918 /*
1919 * Complete this write before we go ahead and perform resume
1920 * as we don't need link status change notificaiton anymore.
1921 */
1922 mb();
1923
1924 if (!ret) {
1925 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1926 dwc->link_state);
1927 ret = -EINVAL;
1928 spin_unlock_irqrestore(&dwc->lock, flags);
1929 goto out1;
1930 } else {
1931 ret = 0;
1932 /*
1933 * If USB is disconnected OR received RESET from host,
1934 * don't perform resume
1935 */
1936 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1937 dwc->gadget.state == USB_STATE_DEFAULT)
1938 link_recover_only = true;
1939 }
1940
1941 /*
1942 * According to DWC3 databook, the controller does not
1943 * trigger a wakeup event when remote-wakeup is used.
1944 * Hence, after remote-wakeup sequence is complete, and
1945 * the device is back at U0 state, it is required that
1946 * the resume sequence is initiated by SW.
1947 */
1948 if (!link_recover_only)
1949 dwc3_gadget_wakeup_interrupt(dwc, true);
1950
Felipe Balbi72246da2011-08-19 18:10:58 +03001951 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001952 pr_debug("%s: Exit\n", __func__);
1953 return ret;
1954
1955out:
1956 spin_unlock_irqrestore(&dwc->lock, flags);
1957 enable_irq(dwc->irq);
1958
1959out1:
1960 return ret;
1961}
1962
1963static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1964{
1965 int ret = 0;
1966 struct dwc3 *dwc = gadget_to_dwc(g);
1967
1968 if (!g || (g->speed != USB_SPEED_SUPER))
1969 return -ENOTSUPP;
1970
1971 if (dwc3_gadget_is_suspended(dwc)) {
1972 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1973 dwc3_gadget_wakeup(&dwc->gadget);
Sriharsha Allenkif857a142017-11-16 18:53:37 +05301974 return -EACCES;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001975 }
1976
1977 if (dwc->revision < DWC3_REVISION_220A) {
1978 ret = dwc3_send_gadget_generic_command(dwc,
1979 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1980 } else {
1981 ret = dwc3_send_gadget_generic_command(dwc,
1982 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1983 }
1984
1985 if (ret)
1986 pr_err("Function wakeup HW command failed.\n");
1987 else
1988 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001989
1990 return ret;
1991}
1992
1993static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1994 int is_selfpowered)
1995{
1996 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001997 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
Paul Zimmerman249a4562012-02-24 17:32:16 -08001999 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002000 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002001 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002002
2003 return 0;
2004}
2005
Mayank Ranaa99689a2016-08-10 17:39:47 -07002006#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002007static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002008{
2009 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002010 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03002011
Felipe Balbi72246da2011-08-19 18:10:58 +03002012 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002013 if (is_on) {
Mayank Rana558baca2017-02-17 11:46:38 -08002014 dbg_event(0xFF, "Pullup_enable", is_on);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002015 if (dwc->revision <= DWC3_REVISION_187A) {
2016 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2017 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2018 }
2019
2020 if (dwc->revision >= DWC3_REVISION_194A)
2021 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002022
2023 dwc3_event_buffers_setup(dwc);
2024 dwc3_gadget_restart(dwc);
2025 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002026 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002027
2028 if (dwc->has_hibernation)
2029 reg |= DWC3_DCTL_KEEP_CONNECT;
2030
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002031 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002032 } else {
Mayank Rana558baca2017-02-17 11:46:38 -08002033 dbg_event(0xFF, "Pullup_disable", is_on);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002034 dwc3_gadget_disable_irq(dwc);
2035 __dwc3_gadget_ep_disable(dwc->eps[0]);
2036 __dwc3_gadget_ep_disable(dwc->eps[1]);
2037
Mayank Rana9ce8d0f2018-02-22 12:04:42 -08002038 /*
2039 * According to dwc3 databook, it is must to remove any active
2040 * transfers before trying to stop USB device controller. Hence
2041 * call dwc3_stop_active_transfers() API before stopping USB
2042 * device controller.
2043 */
2044 dwc3_stop_active_transfers(dwc);
2045
Felipe Balbi72246da2011-08-19 18:10:58 +03002046 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002047
2048 if (dwc->has_hibernation && !suspend)
2049 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2050
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002051 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002052 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002053
2054 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2055
2056 do {
2057 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002058 reg &= DWC3_DSTS_DEVCTRLHLT;
2059 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002060
Mayank Ranaa99689a2016-08-10 17:39:47 -07002061 if (!timeout) {
2062 dev_err(dwc->dev, "failed to %s controller\n",
2063 is_on ? "start" : "stop");
Mayank Rana558baca2017-02-17 11:46:38 -08002064 if (is_on)
2065 dbg_event(0xFF, "STARTTOUT", reg);
2066 else
2067 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03002068 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002069 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002070
Felipe Balbi73815282015-01-27 13:48:14 -06002071 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03002072 dwc->gadget_driver
2073 ? dwc->gadget_driver->function : "no-function",
2074 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05302075
2076 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002077}
2078
Mayank Ranaa99689a2016-08-10 17:39:47 -07002079static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2080{
2081 struct dwc3 *dwc = gadget_to_dwc(g);
2082
2083 dwc->vbus_draw = mA;
2084 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
Mayank Rana558baca2017-02-17 11:46:38 -08002085 dbg_event(0xFF, "currentDraw", mA);
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05302086 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002087 return 0;
2088}
2089
Felipe Balbi72246da2011-08-19 18:10:58 +03002090static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2091{
2092 struct dwc3 *dwc = gadget_to_dwc(g);
2093 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302094 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002095
2096 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002097 dwc->softconnect = is_on;
2098 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
2099 /*
2100 * Need to wait for vbus_session(on) from otg driver or to
2101 * the udc_start.
2102 */
Mayank Rana558baca2017-02-17 11:46:38 -08002103 dbg_event(0xFF, "WaitPullup", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002104 return 0;
2105 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002106
Mayank Ranaa99689a2016-08-10 17:39:47 -07002107 pm_runtime_get_sync(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08002108 dbg_event(0xFF, "Pullup gsync",
2109 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03002110 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002111 /*
2112 * If we are here after bus suspend notify otg state machine to
2113 * increment pm usage count of dwc to prevent pm_runtime_suspend
2114 * during enumeration.
2115 */
2116 dwc->b_suspend = false;
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05302117 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
2118
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002119 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002120 spin_unlock_irqrestore(&dwc->lock, flags);
2121
Mayank Ranaa99689a2016-08-10 17:39:47 -07002122 pm_runtime_mark_last_busy(dwc->dev);
2123 pm_runtime_put_autosuspend(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08002124 dbg_event(0xFF, "Pullup put",
2125 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302126 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002127}
2128
Mayank Ranaa99689a2016-08-10 17:39:47 -07002129void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002130{
2131 u32 reg;
2132
Mayank Rana558baca2017-02-17 11:46:38 -08002133 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002134 /* Enable all but Start and End of Frame IRQs */
2135 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2136 DWC3_DEVTEN_EVNTOVERFLOWEN |
2137 DWC3_DEVTEN_CMDCMPLTEN |
2138 DWC3_DEVTEN_ERRTICERREN |
2139 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002140 DWC3_DEVTEN_CONNECTDONEEN |
2141 DWC3_DEVTEN_USBRSTEN |
2142 DWC3_DEVTEN_DISCONNEVTEN);
2143
Mayank Ranaa99689a2016-08-10 17:39:47 -07002144 /*
2145 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
2146 * else enable USB Link change event (BIT:3) for older version
2147 */
2148 if (dwc->revision < DWC3_REVISION_230A)
2149 reg |= DWC3_DEVTEN_ULSTCNGEN;
2150 else
2151 reg |= DWC3_DEVTEN_SUSPEND;
2152
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002153 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2154}
2155
Mayank Ranaa99689a2016-08-10 17:39:47 -07002156void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002157{
Mayank Rana558baca2017-02-17 11:46:38 -08002158 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002159 /* mask all interrupts */
2160 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2161}
2162
Felipe Balbib15a7622011-06-30 16:57:15 +03002163static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002164static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002165
Felipe Balbi4e994722016-05-13 14:09:59 +03002166/**
2167 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
2168 * dwc: pointer to our context structure
2169 *
2170 * The following looks like complex but it's actually very simple. In order to
2171 * calculate the number of packets we can burst at once on OUT transfers, we're
2172 * gonna use RxFIFO size.
2173 *
2174 * To calculate RxFIFO size we need two numbers:
2175 * MDWIDTH = size, in bits, of the internal memory bus
2176 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2177 *
2178 * Given these two numbers, the formula is simple:
2179 *
2180 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2181 *
2182 * 24 bytes is for 3x SETUP packets
2183 * 16 bytes is a clock domain crossing tolerance
2184 *
2185 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2186 */
2187static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2188{
2189 u32 ram2_depth;
2190 u32 mdwidth;
2191 u32 nump;
2192 u32 reg;
2193
2194 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2195 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2196
2197 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2198 nump = min_t(u32, nump, 16);
2199
2200 /* update NumP */
2201 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2202 reg &= ~DWC3_DCFG_NUMP_MASK;
2203 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2204 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2205}
2206
Mayank Ranaa99689a2016-08-10 17:39:47 -07002207static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2208{
2209 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2210 unsigned long flags;
2211
2212 if (!dwc->is_drd)
2213 return -EPERM;
2214
2215 is_active = !!is_active;
2216
Mayank Rana558baca2017-02-17 11:46:38 -08002217 dbg_event(0xFF, "VbusSess", is_active);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002218 spin_lock_irqsave(&dwc->lock, flags);
2219
2220 /* Mark that the vbus was powered */
2221 dwc->vbus_active = is_active;
2222
2223 /*
2224 * Check if upper level usb_gadget_driver was already registered with
2225 * this udc controller driver (if dwc3_gadget_start was called)
2226 */
2227 if (dwc->gadget_driver && dwc->softconnect) {
2228 if (dwc->vbus_active) {
2229 /*
2230 * Both vbus was activated by otg and pullup was
2231 * signaled by the gadget driver.
2232 */
2233 dwc3_gadget_run_stop(dwc, 1, false);
2234 } else {
2235 dwc3_gadget_run_stop(dwc, 0, false);
2236 }
2237 }
2238
2239 /*
2240 * Clearing run/stop bit might occur before disconnect event is seen.
2241 * Make sure to let gadget driver know in that case.
2242 */
2243 if (!dwc->vbus_active) {
2244 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2245 dwc3_gadget_disconnect_interrupt(dwc);
2246 }
2247
2248 spin_unlock_irqrestore(&dwc->lock, flags);
2249 return 0;
2250}
2251
Felipe Balbid7be2952016-05-04 15:49:37 +03002252static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002253{
Felipe Balbi72246da2011-08-19 18:10:58 +03002254 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002255 int ret = 0;
2256 u32 reg;
2257
Mayank Rana558baca2017-02-17 11:46:38 -08002258 dbg_event(0xFF, "__Gadgetstart", 0);
John Youn26cac202016-11-14 12:32:43 -08002259
2260 /*
2261 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2262 * the core supports IMOD, disable it.
2263 */
2264 if (dwc->imod_interval) {
2265 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2266 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2267 } else if (dwc3_has_imod(dwc)) {
2268 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2269 }
2270
Felipe Balbi72246da2011-08-19 18:10:58 +03002271 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2272 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02002273
2274 /**
2275 * WORKAROUND: DWC3 revision < 2.20a have an issue
2276 * which would cause metastability state on Run/Stop
2277 * bit if we try to force the IP to USB2-only mode.
2278 *
2279 * Because of that, we cannot configure the IP to any
2280 * speed other than the SuperSpeed
2281 *
2282 * Refers to:
2283 *
2284 * STAR#9000525659: Clock Domain Crossing on DCTL in
2285 * USB 2.0 Mode
2286 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03002287 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02002288 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002289 } else {
2290 switch (dwc->maximum_speed) {
2291 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07002292 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002293 break;
2294 case USB_SPEED_FULL:
Roger Quadros5e3c2922017-01-03 14:32:09 +02002295 reg |= DWC3_DCFG_FULLSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002296 break;
2297 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07002298 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002299 break;
John Youn75808622016-02-05 17:09:13 -08002300 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07002301 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08002302 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002303 default:
John Youn77966eb2016-02-19 17:31:01 -08002304 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07002305 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08002306 /* fall through */
2307 case USB_SPEED_SUPER:
2308 reg |= DWC3_DCFG_SUPERSPEED;
2309 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002310 }
2311 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002312 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2313
Mayank Ranaa99689a2016-08-10 17:39:47 -07002314 /* Programs the number of outstanding pipelined transfer requests
2315 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002316 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07002317 if (dwc->revision >= DWC3_REVISION_270A) {
2318 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2319 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2320 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2321 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2322 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002323
Felipe Balbi4e994722016-05-13 14:09:59 +03002324 dwc3_gadget_setup_nump(dwc);
2325
Felipe Balbi72246da2011-08-19 18:10:58 +03002326 /* Start with SuperSpeed Default */
2327 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2328
Mayank Ranaa99689a2016-08-10 17:39:47 -07002329 dwc->delayed_status = false;
2330 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002332 dep->flags = 0;
2333 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002334 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2335 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002336 if (ret) {
2337 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002338 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002339 }
2340
2341 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002342 dep->flags = 0;
2343 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002344 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2345 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002346 if (ret) {
2347 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002348 __dwc3_gadget_ep_disable(dwc->eps[0]);
2349 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002350 }
2351
2352 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002353 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002354 dwc3_ep0_out_start(dwc);
2355
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002356 dwc3_gadget_enable_irq(dwc);
2357
Felipe Balbid7be2952016-05-04 15:49:37 +03002358 return ret;
2359}
2360
Mayank Ranaa99689a2016-08-10 17:39:47 -07002361/* Required gadget re-initialization before switching to gadget in OTG mode */
2362void dwc3_gadget_restart(struct dwc3 *dwc)
2363{
2364 __dwc3_gadget_start(dwc);
2365}
2366
Felipe Balbid7be2952016-05-04 15:49:37 +03002367static int dwc3_gadget_start(struct usb_gadget *g,
2368 struct usb_gadget_driver *driver)
2369{
2370 struct dwc3 *dwc = gadget_to_dwc(g);
2371 unsigned long flags;
2372 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002373
2374 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002375
Felipe Balbid7be2952016-05-04 15:49:37 +03002376 if (dwc->gadget_driver) {
2377 dev_err(dwc->dev, "%s is already bound to %s\n",
2378 dwc->gadget.name,
2379 dwc->gadget_driver->driver.name);
2380 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002381 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002382 }
2383
2384 dwc->gadget_driver = driver;
2385
Mayank Ranaa99689a2016-08-10 17:39:47 -07002386 /*
2387 * For DRD, this might get called by gadget driver during bootup
2388 * even though host mode might be active. Don't actually perform
2389 * device-specific initialization until device mode is activated.
2390 * In that case dwc3_gadget_restart() will handle it.
2391 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002392 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002393 return 0;
2394
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002395err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002396 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002397 return ret;
2398}
2399
Felipe Balbid7be2952016-05-04 15:49:37 +03002400static void __dwc3_gadget_stop(struct dwc3 *dwc)
2401{
Mayank Rana558baca2017-02-17 11:46:38 -08002402 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002403 dwc3_gadget_disable_irq(dwc);
2404 __dwc3_gadget_ep_disable(dwc->eps[0]);
2405 __dwc3_gadget_ep_disable(dwc->eps[1]);
2406}
2407
Felipe Balbi22835b82014-10-17 12:05:12 -05002408static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002409{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002410 struct dwc3 *dwc = gadget_to_dwc(g);
2411 unsigned long flags;
2412
Felipe Balbi72246da2011-08-19 18:10:58 +03002413 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002414 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002415 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002416
Mayank Ranaf0a45122017-11-13 16:56:40 -08002417 dbg_event(0xFF, "fwq_started", 0);
2418 flush_workqueue(dwc->dwc_wq);
2419 dbg_event(0xFF, "fwq_completed", 0);
2420
Felipe Balbi72246da2011-08-19 18:10:58 +03002421 return 0;
2422}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002423
Mayank Ranaa99689a2016-08-10 17:39:47 -07002424static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2425{
2426 struct dwc3 *dwc = gadget_to_dwc(g);
2427
Mayank Rana558baca2017-02-17 11:46:38 -08002428 dbg_event(0xFF, "RestartUSBSession", 0);
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05302429 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002430}
2431
Felipe Balbi72246da2011-08-19 18:10:58 +03002432static const struct usb_gadget_ops dwc3_gadget_ops = {
2433 .get_frame = dwc3_gadget_get_frame,
2434 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002435 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002436 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002437 .vbus_session = dwc3_gadget_vbus_session,
2438 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002439 .pullup = dwc3_gadget_pullup,
2440 .udc_start = dwc3_gadget_start,
2441 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002442 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002443};
2444
2445/* -------------------------------------------------------------------------- */
2446
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002447#define NUM_GSI_OUT_EPS 1
2448#define NUM_GSI_IN_EPS 2
2449
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002450static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2451 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002452{
2453 struct dwc3_ep *dep;
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002454 u8 i, gsi_ep_count, gsi_ep_index = 0;
2455
2456 gsi_ep_count = NUM_GSI_OUT_EPS + NUM_GSI_IN_EPS;
2457 /* OUT GSI EPs based on direction field */
2458 if (gsi_ep_count && !direction)
2459 gsi_ep_count = NUM_GSI_OUT_EPS;
2460 /* IN GSI EPs */
2461 else if (gsi_ep_count && direction)
2462 gsi_ep_count = NUM_GSI_IN_EPS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002463
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002464 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002465 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002466
Felipe Balbi72246da2011-08-19 18:10:58 +03002467 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002468 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002469 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002470
2471 dep->dwc = dwc;
2472 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002473 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002474 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002475 dwc->eps[epnum] = dep;
2476
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002477 /* Reserve EPs at the end for GSI based on gsi_ep_count */
2478 if ((gsi_ep_index < gsi_ep_count) &&
2479 (i > (num - 1 - gsi_ep_count))) {
2480 gsi_ep_index++;
2481 /* For GSI EPs, name eps as "gsi-epin" or "gsi-epout" */
2482 snprintf(dep->name, sizeof(dep->name), "%s",
2483 (epnum & 1) ? "gsi-epin" : "gsi-epout");
2484 /* Set ep type as GSI */
2485 dep->endpoint.ep_type = EP_TYPE_GSI;
2486 } else {
2487 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
2488 epnum >> 1, (epnum & 1) ? "in" : "out");
2489 }
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002490
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002491 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002492 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002493 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002494
Felipe Balbi73815282015-01-27 13:48:14 -06002495 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002496
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002498 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302499 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002500 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2501 if (!epnum)
2502 dwc->gadget.ep0 = &dep->endpoint;
2503 } else {
2504 int ret;
2505
Robert Baldygae117e742013-12-13 12:23:38 +01002506 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002507 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2509 list_add_tail(&dep->endpoint.ep_list,
2510 &dwc->gadget.ep_list);
2511
2512 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002513 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002514 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002515 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002516
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002517 if (epnum == 0 || epnum == 1) {
2518 dep->endpoint.caps.type_control = true;
2519 } else {
2520 dep->endpoint.caps.type_iso = true;
2521 dep->endpoint.caps.type_bulk = true;
2522 dep->endpoint.caps.type_int = true;
2523 }
2524
2525 dep->endpoint.caps.dir_in = !!direction;
2526 dep->endpoint.caps.dir_out = !direction;
2527
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002528 INIT_LIST_HEAD(&dep->pending_list);
2529 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002530 }
2531
2532 return 0;
2533}
2534
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002535static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2536{
2537 int ret;
2538
2539 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2540
2541 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2542 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002543 dwc3_trace(trace_dwc3_gadget,
2544 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002545 return ret;
2546 }
2547
2548 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2549 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002550 dwc3_trace(trace_dwc3_gadget,
2551 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002552 return ret;
2553 }
2554
2555 return 0;
2556}
2557
Felipe Balbi72246da2011-08-19 18:10:58 +03002558static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2559{
2560 struct dwc3_ep *dep;
2561 u8 epnum;
2562
2563 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2564 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002565 if (!dep)
2566 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302567 /*
2568 * Physical endpoints 0 and 1 are special; they form the
2569 * bi-directional USB endpoint 0.
2570 *
2571 * For those two physical endpoints, we don't allocate a TRB
2572 * pool nor do we add them the endpoints list. Due to that, we
2573 * shouldn't do these two operations otherwise we would end up
2574 * with all sorts of bugs when removing dwc3.ko.
2575 */
2576 if (epnum != 0 && epnum != 1) {
2577 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002578 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302579 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002580
2581 kfree(dep);
2582 }
2583}
2584
Felipe Balbi72246da2011-08-19 18:10:58 +03002585/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002586
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302587static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2588 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002589 const struct dwc3_event_depevt *event, int status,
2590 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302591{
2592 unsigned int count;
2593 unsigned int s_pkt = 0;
2594 unsigned int trb_status;
2595
Felipe Balbidc55c672016-08-12 13:20:32 +03002596 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002597
2598 if (req->trb == trb)
2599 dep->queued_requests--;
2600
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002601 trace_dwc3_complete_trb(dep, trb);
2602
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002603 /*
2604 * If we're in the middle of series of chained TRBs and we
2605 * receive a short transfer along the way, DWC3 will skip
2606 * through all TRBs including the last TRB in the chain (the
2607 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2608 * bit and SW has to do it manually.
2609 *
2610 * We're going to do that here to avoid problems of HW trying
2611 * to use bogus TRBs for transfers.
2612 */
2613 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2614 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2615
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302616 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002617 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002618
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302619 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002620 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302621
2622 if (dep->direction) {
2623 if (count) {
2624 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2625 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002626 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002627 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302628 dep->name);
2629 /*
2630 * If missed isoc occurred and there is
2631 * no request queued then issue END
2632 * TRANSFER, so that core generates
2633 * next xfernotready and we will issue
2634 * a fresh START TRANSFER.
2635 * If there are still queued request
2636 * then wait, do not issue either END
2637 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002638 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302639 * giveback.If any future queued request
2640 * is successfully transferred then we
2641 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002642 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302643 */
2644 dep->flags |= DWC3_EP_MISSED_ISOC;
Mayank Rana558baca2017-02-17 11:46:38 -08002645 dbg_event(dep->number, "MISSED ISOC", status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302646 } else {
2647 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2648 dep->name);
2649 status = -ECONNRESET;
2650 }
2651 } else {
2652 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2653 }
2654 } else {
2655 if (count && (event->status & DEPEVT_STATUS_SHORT))
2656 s_pkt = 1;
2657 }
2658
Felipe Balbi7c705df2016-08-10 12:35:30 +03002659 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302660 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002661
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302662 if ((event->status & DEPEVT_STATUS_IOC) &&
2663 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2664 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002665
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302666 return 0;
2667}
2668
Felipe Balbi72246da2011-08-19 18:10:58 +03002669static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2670 const struct dwc3_event_depevt *event, int status)
2671{
Felipe Balbi31162af2016-08-11 14:38:37 +03002672 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002673 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002674 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302675 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002676
Felipe Balbi31162af2016-08-11 14:38:37 +03002677 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002678 unsigned length;
2679 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002680 int chain;
2681
Sriharsha Allenki0525b5c2017-11-22 16:57:46 +05302682 if (req->trb->ctrl & DWC3_TRB_CTRL_HWO)
2683 return 0;
2684
Felipe Balbi1f512112016-08-12 13:17:27 +03002685 length = req->request.length;
2686 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002687 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002688 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002689 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002690 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002691 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002692
Felipe Balbi1f512112016-08-12 13:17:27 +03002693 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002694 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002695
Felipe Balbi1f512112016-08-12 13:17:27 +03002696 req->sg = sg_next(s);
2697 req->num_pending_sgs--;
2698
Felipe Balbi31162af2016-08-11 14:38:37 +03002699 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2700 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002701 if (ret)
2702 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002703 }
2704 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002705 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002706 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002707 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002708 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002709
Felipe Balbic7de5732016-07-29 03:17:58 +03002710 /*
2711 * We assume here we will always receive the entire data block
2712 * which we should receive. Meaning, if we program RX to
2713 * receive 4K but we receive only 2K, we assume that's all we
2714 * should receive and we simply bounce the request back to the
2715 * gadget driver for further processing.
2716 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002717 actual = length - req->request.actual;
2718 req->request.actual = actual;
2719
2720 if (ret && chain && (actual < length) && req->num_pending_sgs)
2721 return __dwc3_gadget_kick_transfer(dep, 0);
2722
Ville Syrjäläd115d702015-08-31 19:48:28 +03002723 dwc3_gadget_giveback(dep, req, status);
2724
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002725 if (ret) {
2726 if ((event->status & DEPEVT_STATUS_IOC) &&
2727 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2728 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002729 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002730 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002731 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002732
Felipe Balbi4cb42212016-05-18 12:37:21 +03002733 /*
2734 * Our endpoint might get disabled by another thread during
2735 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2736 * early on so DWC3_EP_BUSY flag gets cleared
2737 */
2738 if (!dep->endpoint.desc)
2739 return 1;
2740
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302741 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002742 list_empty(&dep->started_list)) {
Sriharsha Allenkie881b352017-11-20 20:42:11 +05302743 if (list_empty(&dep->pending_list))
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302744 /*
2745 * If there is no entry in request list then do
2746 * not issue END TRANSFER now. Just set PENDING
2747 * flag, so that END TRANSFER is issued when an
2748 * entry is added into request list.
2749 */
Sriharsha Allenkie881b352017-11-20 20:42:11 +05302750 dep->flags |= DWC3_EP_PENDING_REQUEST;
2751 else
Paul Zimmermanb992e682012-04-27 14:17:35 +03002752 dwc3_stop_active_transfer(dwc, dep->number, true);
Sriharsha Allenkie881b352017-11-20 20:42:11 +05302753 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand7efea862013-01-14 15:59:32 +05302754 return 1;
2755 }
2756
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002757 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2758 return 0;
2759
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 return 1;
2761}
2762
2763static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002764 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002765{
2766 unsigned status = 0;
2767 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002768 u32 is_xfer_complete;
2769
2770 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002771
2772 if (event->status & DEPEVT_STATUS_BUSERR)
2773 status = -ECONNRESET;
2774
Paul Zimmerman1d046792012-02-15 18:56:56 -08002775 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002776 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002777 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002778 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002779
2780 /*
2781 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2782 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2783 */
2784 if (dwc->revision < DWC3_REVISION_183A) {
2785 u32 reg;
2786 int i;
2787
2788 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002789 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002790
2791 if (!(dep->flags & DWC3_EP_ENABLED))
2792 continue;
2793
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002794 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002795 return;
2796 }
2797
2798 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2799 reg |= dwc->u1u2;
2800 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2801
2802 dwc->u1u2 = 0;
2803 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002804
Felipe Balbi4cb42212016-05-18 12:37:21 +03002805 /*
2806 * Our endpoint might get disabled by another thread during
2807 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2808 * early on so DWC3_EP_BUSY flag gets cleared
2809 */
2810 if (!dep->endpoint.desc)
2811 return;
2812
Felipe Balbie6e709b2015-09-28 15:16:56 -05002813 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002814 int ret;
2815
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002816 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002817 if (!ret || ret == -EBUSY)
2818 return;
2819 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002820}
2821
Felipe Balbi72246da2011-08-19 18:10:58 +03002822static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2823 const struct dwc3_event_depevt *event)
2824{
2825 struct dwc3_ep *dep;
2826 u8 epnum = event->endpoint_number;
2827
2828 dep = dwc->eps[epnum];
2829
Felipe Balbi3336abb2012-06-06 09:19:35 +03002830 if (!(dep->flags & DWC3_EP_ENABLED))
2831 return;
2832
Felipe Balbi72246da2011-08-19 18:10:58 +03002833 if (epnum == 0 || epnum == 1) {
2834 dwc3_ep0_interrupt(dwc, event);
2835 return;
2836 }
2837
Mayank Rana0c667b42017-02-09 11:56:51 -08002838 dep->dbg_ep_events.total++;
2839
Felipe Balbi72246da2011-08-19 18:10:58 +03002840 switch (event->endpoint_event) {
2841 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002842 dep->resource_index = 0;
Mayank Rana0c667b42017-02-09 11:56:51 -08002843 dep->dbg_ep_events.xfercomplete++;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002844
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002845 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002846 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002847 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002848 dep->name);
2849 return;
2850 }
2851
Jingoo Han029d97f2014-07-04 15:00:51 +09002852 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002853 break;
2854 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08002855 dep->dbg_ep_events.xferinprogress++;
Jingoo Han029d97f2014-07-04 15:00:51 +09002856 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002857 break;
2858 case DWC3_DEPEVT_XFERNOTREADY:
Mayank Rana0c667b42017-02-09 11:56:51 -08002859 dep->dbg_ep_events.xfernotready++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002860 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002861 dwc3_gadget_start_isoc(dwc, dep, event);
2862 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002863 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002864 int ret;
2865
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002866 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2867
Felipe Balbi73815282015-01-27 13:48:14 -06002868 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002869 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 : "Transfer Not Active");
2871
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002872 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002873 if (!ret || ret == -EBUSY)
2874 return;
2875
Felipe Balbiec5e7952015-11-16 16:04:13 -06002876 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002877 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 dep->name);
2879 }
2880
2881 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002882 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08002883 dep->dbg_ep_events.streamevent++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002884 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002885 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2886 dep->name);
2887 return;
2888 }
2889
2890 switch (event->status) {
2891 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002892 dwc3_trace(trace_dwc3_gadget,
2893 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002894 event->parameters);
2895
2896 break;
2897 case DEPEVT_STREAMEVT_NOTFOUND:
2898 /* FALLTHROUGH */
2899 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002900 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002901 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002902 }
2903 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002904 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002905 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Mayank Rana0c667b42017-02-09 11:56:51 -08002906 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002907 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002908 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002909 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08002910 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002911 break;
2912 }
2913}
2914
2915static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2916{
Mayank Rana8f847102017-11-15 09:57:15 -08002917 struct usb_gadget_driver *gadget_driver;
2918
Felipe Balbi72246da2011-08-19 18:10:58 +03002919 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
Mayank Rana8f847102017-11-15 09:57:15 -08002920 gadget_driver = dwc->gadget_driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002921 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002922 dbg_event(0xFF, "DISCONNECT", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002923 gadget_driver->disconnect(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002924 spin_lock(&dwc->lock);
2925 }
2926}
2927
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002928static void dwc3_suspend_gadget(struct dwc3 *dwc)
2929{
Mayank Rana8f847102017-11-15 09:57:15 -08002930 struct usb_gadget_driver *gadget_driver;
2931
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002932 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Mayank Rana8f847102017-11-15 09:57:15 -08002933 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002934 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002935 dbg_event(0xFF, "SUSPEND", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002936 gadget_driver->suspend(&dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002937 spin_lock(&dwc->lock);
2938 }
2939}
2940
2941static void dwc3_resume_gadget(struct dwc3 *dwc)
2942{
Mayank Rana8f847102017-11-15 09:57:15 -08002943 struct usb_gadget_driver *gadget_driver;
2944
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002945 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Mayank Rana8f847102017-11-15 09:57:15 -08002946 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002947 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002948 dbg_event(0xFF, "RESUME", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002949 gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002950 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002951 }
2952}
2953
2954static void dwc3_reset_gadget(struct dwc3 *dwc)
2955{
Mayank Rana8f847102017-11-15 09:57:15 -08002956 struct usb_gadget_driver *gadget_driver;
2957
Felipe Balbi8e744752014-11-06 14:27:53 +08002958 if (!dwc->gadget_driver)
2959 return;
2960
2961 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
Mayank Rana8f847102017-11-15 09:57:15 -08002962 gadget_driver = dwc->gadget_driver;
Felipe Balbi8e744752014-11-06 14:27:53 +08002963 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002964 dbg_event(0xFF, "UDC RESET", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002965 usb_gadget_udc_reset(&dwc->gadget, gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002966 spin_lock(&dwc->lock);
2967 }
2968}
2969
Mayank Ranaa99689a2016-08-10 17:39:47 -07002970void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002971{
2972 struct dwc3_ep *dep;
2973 struct dwc3_gadget_ep_cmd_params params;
2974 u32 cmd;
2975 int ret;
2976
2977 dep = dwc->eps[epnum];
2978
Felipe Balbib4996a82012-06-06 12:04:13 +03002979 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302980 return;
2981
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05302982 if (dep->endpoint.endless)
2983 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
2984 dep->number);
2985
Pratyush Anand57911502012-07-06 15:19:10 +05302986 /*
2987 * NOTICE: We are violating what the Databook says about the
2988 * EndTransfer command. Ideally we would _always_ wait for the
2989 * EndTransfer Command Completion IRQ, but that's causing too
2990 * much trouble synchronizing between us and gadget driver.
2991 *
2992 * We have discussed this with the IP Provider and it was
2993 * suggested to giveback all requests here, but give HW some
2994 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002995 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302996 *
2997 * Note also that a similar handling was tested by Synopsys
2998 * (thanks a lot Paul) and nothing bad has come out of it.
2999 * In short, what we're doing is:
3000 *
3001 * - Issue EndTransfer WITH CMDIOC bit set
3002 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07003003 *
3004 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3005 * supports a mode to work around the above limitation. The
3006 * software can poll the CMDACT bit in the DEPCMD register
3007 * after issuing a EndTransfer command. This mode is enabled
3008 * by writing GUCTL2[14]. This polling is already done in the
3009 * dwc3_send_gadget_ep_cmd() function so if the mode is
3010 * enabled, the EndTransfer command will have completed upon
3011 * returning from this function and we don't need to delay for
3012 * 100us.
3013 *
3014 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303015 */
3016
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303017 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003018 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3019 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03003020 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303021 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003022 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303023 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003024 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03003025 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07003026
3027 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
3028 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03003029}
3030
3031static void dwc3_stop_active_transfers(struct dwc3 *dwc)
3032{
3033 u32 epnum;
3034
3035 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3036 struct dwc3_ep *dep;
3037
3038 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003039 if (!dep)
3040 continue;
3041
Felipe Balbi72246da2011-08-19 18:10:58 +03003042 if (!(dep->flags & DWC3_EP_ENABLED))
3043 continue;
3044
Vijayavardhan Vennapusa404c72d2018-07-17 17:33:16 +05303045 if (dep->endpoint.ep_type == EP_TYPE_GSI && dep->direction)
3046 dwc3_notify_event(dwc,
3047 DWC3_CONTROLLER_NOTIFY_CLEAR_DB, 0);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02003048 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003049 }
3050}
3051
3052static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3053{
3054 u32 epnum;
3055
3056 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3057 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003058 int ret;
3059
3060 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003061 if (!dep)
3062 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003063
3064 if (!(dep->flags & DWC3_EP_STALL))
3065 continue;
3066
3067 dep->flags &= ~DWC3_EP_STALL;
3068
John Youn50c763f2016-05-31 17:49:56 -07003069 ret = dwc3_send_clear_stall_ep_cmd(dep);
Mayank Rana558baca2017-02-17 11:46:38 -08003070 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003071 WARN_ON_ONCE(ret);
3072 }
3073}
3074
3075static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3076{
Felipe Balbic4430a22012-05-24 10:30:01 +03003077 int reg;
3078
Mayank Rana558baca2017-02-17 11:46:38 -08003079 dbg_event(0xFF, "DISCONNECT INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003080 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3081 dwc->b_suspend = false;
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303082 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003083
Felipe Balbi72246da2011-08-19 18:10:58 +03003084 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3085 reg &= ~DWC3_DCTL_INITU1ENA;
3086 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3087
3088 reg &= ~DWC3_DCTL_INITU2ENA;
3089 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003090
Felipe Balbi72246da2011-08-19 18:10:58 +03003091 dwc3_disconnect_gadget(dwc);
3092
3093 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003094 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003095 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05003096 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003097
3098 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003099 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003100}
3101
Felipe Balbi72246da2011-08-19 18:10:58 +03003102static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3103{
3104 u32 reg;
3105
Felipe Balbifc8bb912016-05-16 13:14:48 +03003106 dwc->connected = true;
3107
Felipe Balbidf62df52011-10-14 15:11:49 +03003108 /*
3109 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3110 * would cause a missing Disconnect Event if there's a
3111 * pending Setup Packet in the FIFO.
3112 *
3113 * There's no suggested workaround on the official Bug
3114 * report, which states that "unless the driver/application
3115 * is doing any special handling of a disconnect event,
3116 * there is no functional issue".
3117 *
3118 * Unfortunately, it turns out that we _do_ some special
3119 * handling of a disconnect event, namely complete all
3120 * pending transfers, notify gadget driver of the
3121 * disconnection, and so on.
3122 *
3123 * Our suggested workaround is to follow the Disconnect
3124 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003125 * flag. Such flag gets set whenever we have a SETUP_PENDING
3126 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003127 * same endpoint.
3128 *
3129 * Refers to:
3130 *
3131 * STAR#9000466709: RTL: Device : Disconnect event not
3132 * generated if setup packet pending in FIFO
3133 */
3134 if (dwc->revision < DWC3_REVISION_188A) {
3135 if (dwc->setup_packet_pending)
3136 dwc3_gadget_disconnect_interrupt(dwc);
3137 }
3138
Mayank Rana558baca2017-02-17 11:46:38 -08003139 dbg_event(0xFF, "BUS RESET", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003140 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3141 dwc->b_suspend = false;
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303142 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003143
3144 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07003145 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003146
Felipe Balbi8e744752014-11-06 14:27:53 +08003147 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003148
3149 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3150 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3151 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003152 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003153
Sriharsha Allenkif8d60db2017-11-16 17:00:09 +05303154 /*
3155 * From SNPS databook section 8.1.2
3156 * the EP0 should be in setup phase. So ensure
3157 * that EP0 is in setup phase by issuing a stall
3158 * and restart if EP0 is not in setup phase.
3159 */
Vijayavardhan Vennapusa5000f522018-03-13 15:01:14 +05303160 if (dwc->ep0state != EP0_SETUP_PHASE) {
3161 unsigned int dir;
3162
3163 dbg_event(0xFF, "CONTRPEND", dwc->ep0state);
3164 dir = !!dwc->ep0_expect_in;
3165 if (dwc->ep0state == EP0_DATA_PHASE)
3166 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
3167 else
3168 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
Sriharsha Allenkif8d60db2017-11-16 17:00:09 +05303169 dwc3_ep0_stall_and_restart(dwc);
Vijayavardhan Vennapusa5000f522018-03-13 15:01:14 +05303170 }
Sriharsha Allenkif8d60db2017-11-16 17:00:09 +05303171
Felipe Balbi72246da2011-08-19 18:10:58 +03003172 dwc3_stop_active_transfers(dwc);
3173 dwc3_clear_stall_all_ep(dwc);
3174
3175 /* Reset device address to zero */
3176 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3177 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3178 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003179
3180 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3181 dwc->link_state = DWC3_LINK_STATE_U0;
3182 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003183}
3184
3185static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
3186{
3187 u32 reg;
3188 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
3189
3190 /*
3191 * We change the clock only at SS but I dunno why I would want to do
3192 * this. Maybe it becomes part of the power saving plan.
3193 */
3194
John Younee5cd412016-02-05 17:08:45 -08003195 if ((speed != DWC3_DSTS_SUPERSPEED) &&
3196 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03003197 return;
3198
3199 /*
3200 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3201 * each time on Connect Done.
3202 */
3203 if (!usb30_clock)
3204 return;
3205
3206 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3207 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
3208 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
3209}
3210
Felipe Balbi72246da2011-08-19 18:10:58 +03003211static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3212{
Felipe Balbi72246da2011-08-19 18:10:58 +03003213 struct dwc3_ep *dep;
3214 int ret;
3215 u32 reg;
3216 u8 speed;
3217
Mayank Rana558baca2017-02-17 11:46:38 -08003218 dbg_event(0xFF, "CONNECT DONE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03003219 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3220 speed = reg & DWC3_DSTS_CONNECTSPD;
3221 dwc->speed = speed;
3222
3223 dwc3_update_ram_clk_sel(dwc, speed);
3224
3225 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003226 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003227 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3228 dwc->gadget.ep0->maxpacket = 512;
3229 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3230 break;
John Youn2da9ad72016-05-20 16:34:26 -07003231 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003232 /*
3233 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3234 * would cause a missing USB3 Reset event.
3235 *
3236 * In such situations, we should force a USB3 Reset
3237 * event by calling our dwc3_gadget_reset_interrupt()
3238 * routine.
3239 *
3240 * Refers to:
3241 *
3242 * STAR#9000483510: RTL: SS : USB3 reset event may
3243 * not be generated always when the link enters poll
3244 */
3245 if (dwc->revision < DWC3_REVISION_190A)
3246 dwc3_gadget_reset_interrupt(dwc);
3247
Felipe Balbi72246da2011-08-19 18:10:58 +03003248 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3249 dwc->gadget.ep0->maxpacket = 512;
3250 dwc->gadget.speed = USB_SPEED_SUPER;
3251 break;
John Youn2da9ad72016-05-20 16:34:26 -07003252 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003253 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3254 dwc->gadget.ep0->maxpacket = 64;
3255 dwc->gadget.speed = USB_SPEED_HIGH;
3256 break;
Roger Quadros5e3c2922017-01-03 14:32:09 +02003257 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003258 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3259 dwc->gadget.ep0->maxpacket = 64;
3260 dwc->gadget.speed = USB_SPEED_FULL;
3261 break;
John Youn2da9ad72016-05-20 16:34:26 -07003262 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003263 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3264 dwc->gadget.ep0->maxpacket = 8;
3265 dwc->gadget.speed = USB_SPEED_LOW;
3266 break;
3267 }
3268
Thinh Nguyen3c0cbbf2018-01-12 18:18:05 -08003269 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3270
Pratyush Anand2b758352013-01-14 15:59:31 +05303271 /* Enable USB2 LPM Capability */
3272
John Younee5cd412016-02-05 17:08:45 -08003273 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003274 (speed != DWC3_DSTS_SUPERSPEED) &&
3275 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303276 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3277 reg |= DWC3_DCFG_LPM_CAP;
3278 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3279
3280 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3281 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3282
Huang Rui460d0982014-10-31 11:11:18 +08003283 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303284
Huang Rui80caf7d2014-10-28 19:54:26 +08003285 /*
3286 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3287 * DCFG.LPMCap is set, core responses with an ACK and the
3288 * BESL value in the LPM token is less than or equal to LPM
3289 * NYET threshold.
3290 */
3291 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3292 && dwc->has_lpm_erratum,
3293 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
3294
3295 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3296 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3297
Pratyush Anand2b758352013-01-14 15:59:31 +05303298 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003299 } else {
3300 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3301 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3302 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303303 }
3304
Mayank Ranaa99689a2016-08-10 17:39:47 -07003305
3306 /*
3307 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
3308 * suspend in P3 state and generates IN_P3 power event irq.
3309 */
3310 dwc3_usb3_phy_suspend(dwc, true);
3311
Felipe Balbi72246da2011-08-19 18:10:58 +03003312 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003313 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3314 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003315 if (ret) {
3316 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3317 return;
3318 }
3319
3320 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003321 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3322 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003323 if (ret) {
3324 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3325 return;
3326 }
3327
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303328 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT, 0);
3329
Felipe Balbi72246da2011-08-19 18:10:58 +03003330 /*
3331 * Configure PHY via GUSB3PIPECTLn if required.
3332 *
3333 * Update GTXFIFOSIZn
3334 *
3335 * In both cases reset values should be sufficient.
3336 */
3337}
3338
Mayank Ranaa99689a2016-08-10 17:39:47 -07003339static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003340{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003341 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003342
Mayank Ranaa99689a2016-08-10 17:39:47 -07003343 dev_dbg(dwc->dev, "%s\n", __func__);
3344
Mayank Rana558baca2017-02-17 11:46:38 -08003345 dbg_event(0xFF, "WAKEUP", remote_wakeup);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003346 /*
3347 * Identify if it is called from wakeup_interrupt() context for bus
3348 * resume or as part of remote wakeup. And based on that check for
3349 * U3 state. as we need to handle case of L1 resume i.e. where we
3350 * don't want to perform resume.
3351 */
3352 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3353 perform_resume = false;
3354
3355 /* Only perform resume from L2 or Early Suspend states */
3356 if (perform_resume) {
3357
3358 /*
3359 * In case of remote wake up dwc3_gadget_wakeup_work()
Sriharsha Allenkif0201ec2018-09-06 15:51:52 +05303360 * is doing pm_runtime_get_sync(). But mark last wakeup
3361 * event here to prevent runtime_suspend happening before this
3362 * wakeup event is processed.
Mayank Ranaa99689a2016-08-10 17:39:47 -07003363 */
Sriharsha Allenkif0201ec2018-09-06 15:51:52 +05303364 pm_runtime_mark_last_busy(dwc->dev);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003365 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3366 dwc->b_suspend = false;
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303367 dwc3_notify_event(dwc,
3368 DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003369
3370 /*
3371 * set state to U0 as function level resume is trying to queue
3372 * notification over USB interrupt endpoint which would fail
3373 * due to state is not being updated.
3374 */
3375 dwc->link_state = DWC3_LINK_STATE_U0;
3376 dwc3_resume_gadget(dwc);
3377 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003378 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003379
3380 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003381}
3382
3383static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3384 unsigned int evtinfo)
3385{
Felipe Balbifae2b902011-10-14 13:00:30 +03003386 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003387 unsigned int pwropt;
3388
3389 /*
3390 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3391 * Hibernation mode enabled which would show up when device detects
3392 * host-initiated U3 exit.
3393 *
3394 * In that case, device will generate a Link State Change Interrupt
3395 * from U3 to RESUME which is only necessary if Hibernation is
3396 * configured in.
3397 *
3398 * There are no functional changes due to such spurious event and we
3399 * just need to ignore it.
3400 *
3401 * Refers to:
3402 *
3403 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3404 * operational mode
3405 */
3406 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3407 if ((dwc->revision < DWC3_REVISION_250A) &&
3408 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3409 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3410 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06003411 dwc3_trace(trace_dwc3_gadget,
3412 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003413 return;
3414 }
3415 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003416
3417 /*
3418 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3419 * on the link partner, the USB session might do multiple entry/exit
3420 * of low power states before a transfer takes place.
3421 *
3422 * Due to this problem, we might experience lower throughput. The
3423 * suggested workaround is to disable DCTL[12:9] bits if we're
3424 * transitioning from U1/U2 to U0 and enable those bits again
3425 * after a transfer completes and there are no pending transfers
3426 * on any of the enabled endpoints.
3427 *
3428 * This is the first half of that workaround.
3429 *
3430 * Refers to:
3431 *
3432 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3433 * core send LGO_Ux entering U0
3434 */
3435 if (dwc->revision < DWC3_REVISION_183A) {
3436 if (next == DWC3_LINK_STATE_U0) {
3437 u32 u1u2;
3438 u32 reg;
3439
3440 switch (dwc->link_state) {
3441 case DWC3_LINK_STATE_U1:
3442 case DWC3_LINK_STATE_U2:
3443 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3444 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3445 | DWC3_DCTL_ACCEPTU2ENA
3446 | DWC3_DCTL_INITU1ENA
3447 | DWC3_DCTL_ACCEPTU1ENA);
3448
3449 if (!dwc->u1u2)
3450 dwc->u1u2 = reg & u1u2;
3451
3452 reg &= ~u1u2;
3453
3454 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3455 break;
3456 default:
3457 /* do nothing */
3458 break;
3459 }
3460 }
3461 }
3462
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003463 switch (next) {
3464 case DWC3_LINK_STATE_U1:
3465 if (dwc->speed == USB_SPEED_SUPER)
3466 dwc3_suspend_gadget(dwc);
3467 break;
3468 case DWC3_LINK_STATE_U2:
3469 case DWC3_LINK_STATE_U3:
3470 dwc3_suspend_gadget(dwc);
3471 break;
3472 case DWC3_LINK_STATE_RESUME:
3473 dwc3_resume_gadget(dwc);
3474 break;
3475 default:
3476 /* do nothing */
3477 break;
3478 }
3479
Mayank Ranaa99689a2016-08-10 17:39:47 -07003480 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003481 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003482 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003483}
3484
Baolin Wang72704f82016-05-16 16:43:53 +08003485static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003486 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003487{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003488 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003489
Mayank Rana558baca2017-02-17 11:46:38 -08003490 dbg_event(0xFF, "SUSPEND INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003491 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3492
3493 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3494 /*
3495 * When first connecting the cable, even before the initial
3496 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3497 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3498 * event. In such a case, ignore.
3499 * Ignore suspend event until device side usb is not into
3500 * CONFIGURED state.
3501 */
3502 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3503 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3504 __func__, dwc->gadget.state);
3505 return;
3506 }
3507
Baolin Wang72704f82016-05-16 16:43:53 +08003508 dwc3_suspend_gadget(dwc);
3509
Mayank Ranaa99689a2016-08-10 17:39:47 -07003510 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3511 dwc->b_suspend = true;
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303512 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003513 }
3514
Baolin Wang72704f82016-05-16 16:43:53 +08003515 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003516 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003517}
3518
Felipe Balbie1dadd32014-02-25 14:47:54 -06003519static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3520 unsigned int evtinfo)
3521{
3522 unsigned int is_ss = evtinfo & BIT(4);
3523
3524 /**
3525 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3526 * have a known issue which can cause USB CV TD.9.23 to fail
3527 * randomly.
3528 *
3529 * Because of this issue, core could generate bogus hibernation
3530 * events which SW needs to ignore.
3531 *
3532 * Refers to:
3533 *
3534 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3535 * Device Fallback from SuperSpeed
3536 */
3537 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3538 return;
3539
3540 /* enter hibernation here */
3541}
3542
Felipe Balbi72246da2011-08-19 18:10:58 +03003543static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3544 const struct dwc3_event_devt *event)
3545{
3546 switch (event->type) {
3547 case DWC3_DEVICE_EVENT_DISCONNECT:
3548 dwc3_gadget_disconnect_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003549 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003550 break;
3551 case DWC3_DEVICE_EVENT_RESET:
3552 dwc3_gadget_reset_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003553 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003554 break;
3555 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3556 dwc3_gadget_conndone_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003557 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003558 break;
3559 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003560 dwc3_gadget_wakeup_interrupt(dwc, false);
Mayank Rana0c667b42017-02-09 11:56:51 -08003561 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003562 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003563 case DWC3_DEVICE_EVENT_HIBER_REQ:
3564 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3565 "unexpected hibernation event\n"))
3566 break;
3567
3568 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3569 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003570 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3571 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Mayank Rana0c667b42017-02-09 11:56:51 -08003572 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003573 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003574 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003575 if (dwc->revision < DWC3_REVISION_230A) {
3576 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003577 dwc->dbg_gadget_events.eopf++;
Baolin Wang72704f82016-05-16 16:43:53 +08003578 } else {
3579 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
Mayank Rana558baca2017-02-17 11:46:38 -08003580 dbg_event(0xFF, "GAD SUS", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003581 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003582 /*
3583 * Ignore suspend event until the gadget enters into
3584 * USB_STATE_CONFIGURED state.
3585 */
3586 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3587 dwc3_gadget_suspend_interrupt(dwc,
3588 event->event_info);
3589 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003590 break;
3591 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003592 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003593 dwc->dbg_gadget_events.sof++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003594 break;
3595 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003596 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Mayank Rana558baca2017-02-17 11:46:38 -08003597 dbg_event(0xFF, "ERROR", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003598 dwc->dbg_gadget_events.erratic_error++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003599 break;
3600 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003601 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08003602 dwc->dbg_gadget_events.cmdcmplt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003603 break;
3604 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003605 dwc3_trace(trace_dwc3_gadget, "Overflow");
Mayank Rana0c667b42017-02-09 11:56:51 -08003606 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003607 break;
3608 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003609 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Mayank Rana0c667b42017-02-09 11:56:51 -08003610 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003611 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003612
3613 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003614}
3615
3616static void dwc3_process_event_entry(struct dwc3 *dwc,
3617 const union dwc3_event *event)
3618{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003619 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003620 /* skip event processing in absence of vbus */
3621 if (!dwc->vbus_active) {
Mayank Rana6300b452017-05-24 09:33:22 -07003622 dbg_event(0xFF, "SKIP_EVT", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003623 return;
3624 }
3625
3626 /* If run/stop is cleared don't process any more events */
3627 if (!dwc->pullups_connected) {
Mayank Rana6300b452017-05-24 09:33:22 -07003628 dbg_event(0xFF, "SKIP_EVT_PULLUP", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003629 return;
3630 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003631
Felipe Balbi72246da2011-08-19 18:10:58 +03003632 /* Endpoint IRQ, handle it and return early */
3633 if (event->type.is_devspec == 0) {
3634 /* depevt */
Manu Gautamb6862ce2018-11-02 14:57:25 +05303635 /* If remote-wakeup attempt by device had failed, then core
3636 * wouldn't give wakeup event after resume. Handle that
3637 * here on ep event which indicates that bus is resumed.
3638 */
3639 if (dwc->b_suspend &&
3640 dwc3_get_link_state(dwc) == DWC3_LINK_STATE_U0)
3641 dwc3_gadget_wakeup_interrupt(dwc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003642 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3643 }
3644
3645 switch (event->type.type) {
3646 case DWC3_EVENT_TYPE_DEV:
3647 dwc3_gadget_interrupt(dwc, &event->devt);
3648 break;
3649 /* REVISIT what to do with Carkit and I2C events ? */
3650 default:
3651 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3652 }
3653}
3654
Mayank Ranaa99689a2016-08-10 17:39:47 -07003655static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003656{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003657 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003658 irqreturn_t ret = IRQ_NONE;
3659 int left;
3660 u32 reg;
3661
Mayank Ranaa99689a2016-08-10 17:39:47 -07003662 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003663 left = evt->count;
3664
3665 if (!(evt->flags & DWC3_EVENT_PENDING))
3666 return IRQ_NONE;
3667
3668 while (left > 0) {
3669 union dwc3_event event;
3670
3671 event.raw = *(u32 *) (evt->buf + evt->lpos);
3672
3673 dwc3_process_event_entry(dwc, &event);
3674
Mayank Ranaa99689a2016-08-10 17:39:47 -07003675 if (dwc->err_evt_seen) {
3676 /*
3677 * if erratic error, skip remaining events
3678 * while controller undergoes reset
3679 */
3680 evt->lpos = (evt->lpos + left) %
3681 DWC3_EVENT_BUFFERS_SIZE;
3682 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
Vijayavardhan Vennapusa61c8f612016-01-29 21:14:24 +05303683 if (dwc3_notify_event(dwc,
3684 DWC3_CONTROLLER_ERROR_EVENT, 0))
Mayank Ranaa99689a2016-08-10 17:39:47 -07003685 dwc->err_evt_seen = 0;
3686 break;
3687 }
3688
Felipe Balbif42f2442013-06-12 21:25:08 +03003689 /*
3690 * FIXME we wrap around correctly to the next entry as
3691 * almost all entries are 4 bytes in size. There is one
3692 * entry which has 12 bytes which is a regular entry
3693 * followed by 8 bytes data. ATM I don't know how
3694 * things are organized if we get next to the a
3695 * boundary so I worry about that once we try to handle
3696 * that.
3697 */
3698 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3699 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003700 }
3701
Mayank Ranaa99689a2016-08-10 17:39:47 -07003702 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003703 evt->count = 0;
3704 evt->flags &= ~DWC3_EVENT_PENDING;
3705 ret = IRQ_HANDLED;
3706
3707 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003708 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003709 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003710 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003711
John Youn26cac202016-11-14 12:32:43 -08003712 if (dwc->imod_interval)
3713 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0),
3714 DWC3_GEVNTCOUNT_EHB);
3715
Felipe Balbif42f2442013-06-12 21:25:08 +03003716 return ret;
3717}
3718
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003719void dwc3_bh_work(struct work_struct *w)
3720{
3721 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3722
3723 pm_runtime_get_sync(dwc->dev);
3724 dwc3_thread_interrupt(dwc->irq, dwc);
3725 pm_runtime_put(dwc->dev);
3726}
3727
Mayank Ranaa99689a2016-08-10 17:39:47 -07003728static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3729{
3730 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003731 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003732 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003733 unsigned int temp_time;
3734 ktime_t start_time;
3735
3736 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003737
Felipe Balbie5f68b42015-10-12 13:25:44 -05003738 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003739 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3740
3741 ret = dwc3_process_event_buf(dwc);
3742
Felipe Balbie5f68b42015-10-12 13:25:44 -05003743 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003744
Mayank Ranaa99689a2016-08-10 17:39:47 -07003745 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3746 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3747 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3748
Felipe Balbib15a7622011-06-30 16:57:15 +03003749 return ret;
3750}
3751
Mayank Ranaa99689a2016-08-10 17:39:47 -07003752static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003753{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003754 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003755 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003756 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003757
Mayank Ranaa99689a2016-08-10 17:39:47 -07003758 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003759
Thinh Nguyenff9177b2017-05-11 17:26:47 -07003760 /*
3761 * With PCIe legacy interrupt, test shows that top-half irq handler can
3762 * be called again after HW interrupt deassertion. Check if bottom-half
3763 * irq event handler completes before caching new event to prevent
3764 * losing events.
3765 */
3766 if (evt->flags & DWC3_EVENT_PENDING)
3767 return IRQ_HANDLED;
3768
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003769 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003770 count &= DWC3_GEVNTCOUNT_MASK;
3771 if (!count)
3772 return IRQ_NONE;
3773
Mayank Ranaa99689a2016-08-10 17:39:47 -07003774 if (count > evt->length) {
3775 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
Mayank Rana558baca2017-02-17 11:46:38 -08003776 dbg_event(0xFF, "HUGE_EVCNT", count);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003777 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3778 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3779 return IRQ_HANDLED;
3780 }
3781
Felipe Balbib15a7622011-06-30 16:57:15 +03003782 evt->count = count;
3783 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003784
Felipe Balbie8adfc32013-06-12 21:11:14 +03003785 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003786 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003787 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003788 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003789
John Youn551d2902016-11-15 13:08:59 +02003790 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3791
Felipe Balbib15a7622011-06-30 16:57:15 +03003792 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003793}
3794
Mayank Ranaa99689a2016-08-10 17:39:47 -07003795irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003796{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003797 struct dwc3 *dwc = _dwc;
3798 irqreturn_t ret = IRQ_NONE;
3799 irqreturn_t status;
3800 unsigned int temp_cnt = 0;
3801 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003802
Mayank Ranaa99689a2016-08-10 17:39:47 -07003803 start_time = ktime_get();
3804 dwc->irq_cnt++;
3805
3806 /* controller reset is still pending */
3807 if (dwc->err_evt_seen)
3808 return IRQ_HANDLED;
3809
3810 status = dwc3_check_event_buf(dwc);
3811 if (status == IRQ_WAKE_THREAD)
3812 ret = status;
3813
3814 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3815 dwc->irq_completion_time[dwc->irq_dbg_index] =
3816 ktime_us_delta(ktime_get(), start_time);
3817 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3818 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3819
Hemant Kumar78c7c282016-08-09 12:28:55 -07003820 if (ret == IRQ_WAKE_THREAD)
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003821 queue_work(dwc->dwc_wq, &dwc->bh_work);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003822
3823 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003824}
3825
3826/**
3827 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003828 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003829 *
3830 * Returns 0 on success otherwise negative errno.
3831 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003832int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003833{
Roger Quadros9522def2016-06-10 14:48:38 +03003834 int ret, irq;
3835 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3836
3837 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3838 if (irq == -EPROBE_DEFER)
3839 return irq;
3840
3841 if (irq <= 0) {
3842 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3843 if (irq == -EPROBE_DEFER)
3844 return irq;
3845
3846 if (irq <= 0) {
3847 irq = platform_get_irq(dwc3_pdev, 0);
3848 if (irq <= 0) {
3849 if (irq != -EPROBE_DEFER) {
3850 dev_err(dwc->dev,
3851 "missing peripheral IRQ\n");
3852 }
3853 if (!irq)
3854 irq = -EINVAL;
3855 return irq;
3856 }
3857 }
3858 }
3859
3860 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003861
Mayank Ranaa99689a2016-08-10 17:39:47 -07003862 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3863
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303864 dwc->ctrl_req = dma_alloc_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003865 &dwc->ctrl_req_addr, GFP_KERNEL);
3866 if (!dwc->ctrl_req) {
3867 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3868 ret = -ENOMEM;
3869 goto err0;
3870 }
3871
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303872 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3873 sizeof(*dwc->ep0_trb) * 2,
3874 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003875 if (!dwc->ep0_trb) {
3876 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3877 ret = -ENOMEM;
3878 goto err1;
3879 }
3880
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003881 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003882 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003883 ret = -ENOMEM;
3884 goto err2;
3885 }
3886
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303887 dwc->ep0_bounce = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003888 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3889 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003890 if (!dwc->ep0_bounce) {
3891 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3892 ret = -ENOMEM;
3893 goto err3;
3894 }
3895
Felipe Balbi04c03d12015-12-02 10:06:45 -06003896 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3897 if (!dwc->zlp_buf) {
3898 ret = -ENOMEM;
3899 goto err4;
3900 }
3901
Felipe Balbi72246da2011-08-19 18:10:58 +03003902 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003903 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003904 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003905 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003906 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Sriharsha Allenki972cdad2018-03-13 18:05:16 +05303907 dwc->gadget.l1_supported = !dwc->usb2_l1_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03003908
3909 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003910 * FIXME We might be setting max_speed to <SUPER, however versions
3911 * <2.20a of dwc3 have an issue with metastability (documented
3912 * elsewhere in this driver) which tells us we can't set max speed to
3913 * anything lower than SUPER.
3914 *
3915 * Because gadget.max_speed is only used by composite.c and function
3916 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3917 * to happen so we avoid sending SuperSpeed Capability descriptor
3918 * together with our BOS descriptor as that could confuse host into
3919 * thinking we can handle super speed.
3920 *
3921 * Note that, in fact, we won't even support GetBOS requests when speed
3922 * is less than super speed because we don't have means, yet, to tell
3923 * composite.c that we are USB 2.0 + LPM ECN.
3924 */
3925 if (dwc->revision < DWC3_REVISION_220A)
3926 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003927 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003928 dwc->revision);
3929
3930 dwc->gadget.max_speed = dwc->maximum_speed;
3931
3932 /*
David Cohena4b9d942013-12-09 15:55:38 -08003933 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3934 * on ep out.
3935 */
3936 dwc->gadget.quirk_ep_out_aligned_size = true;
3937
3938 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003939 * REVISIT: Here we should clear all pending IRQs to be
3940 * sure we're starting from a well known location.
3941 */
3942
3943 ret = dwc3_gadget_init_endpoints(dwc);
3944 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003945 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003946
Felipe Balbi72246da2011-08-19 18:10:58 +03003947 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3948 if (ret) {
3949 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003950 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003951 }
3952
Mayank Ranaa99689a2016-08-10 17:39:47 -07003953 if (!dwc->is_drd) {
3954 pm_runtime_no_callbacks(&dwc->gadget.dev);
3955 pm_runtime_set_active(&dwc->gadget.dev);
3956 pm_runtime_enable(&dwc->gadget.dev);
3957 pm_runtime_get(&dwc->gadget.dev);
3958 }
3959
Felipe Balbi72246da2011-08-19 18:10:58 +03003960 return 0;
3961
Felipe Balbi04c03d12015-12-02 10:06:45 -06003962err5:
3963 kfree(dwc->zlp_buf);
3964
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003965err4:
David Cohene1f80462013-09-11 17:42:47 -07003966 dwc3_gadget_free_endpoints(dwc);
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303967 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003968 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003969
Felipe Balbi72246da2011-08-19 18:10:58 +03003970err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003971 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003972
3973err2:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303974 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003975 dwc->ep0_trb, dwc->ep0_trb_addr);
3976
3977err1:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303978 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003979 dwc->ctrl_req, dwc->ctrl_req_addr);
3980
3981err0:
3982 return ret;
3983}
3984
Felipe Balbi7415f172012-04-30 14:56:33 +03003985/* -------------------------------------------------------------------------- */
3986
Felipe Balbi72246da2011-08-19 18:10:58 +03003987void dwc3_gadget_exit(struct dwc3 *dwc)
3988{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003989 if (dwc->is_drd) {
3990 pm_runtime_put(&dwc->gadget.dev);
3991 pm_runtime_disable(&dwc->gadget.dev);
3992 }
3993
Felipe Balbi72246da2011-08-19 18:10:58 +03003994 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003995
Felipe Balbi72246da2011-08-19 18:10:58 +03003996 dwc3_gadget_free_endpoints(dwc);
3997
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303998 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003999 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004000
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004001 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06004002 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004003
Arnd Bergmann42695fc2016-11-17 17:13:47 +05304004 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004005 dwc->ep0_trb, dwc->ep0_trb_addr);
4006
Arnd Bergmann42695fc2016-11-17 17:13:47 +05304007 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03004008 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004009}
Felipe Balbi7415f172012-04-30 14:56:33 +03004010
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004011int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004012{
Roger Quadros9772b472016-04-12 11:33:29 +03004013 if (!dwc->gadget_driver)
4014 return 0;
4015
Roger Quadros7abf66d2017-02-15 14:16:26 +02004016 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004017 dwc3_disconnect_gadget(dwc);
4018 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004019
4020 return 0;
4021}
4022
4023int dwc3_gadget_resume(struct dwc3 *dwc)
4024{
Felipe Balbi7415f172012-04-30 14:56:33 +03004025 int ret;
4026
Roger Quadros9772b472016-04-12 11:33:29 +03004027 if (!dwc->gadget_driver)
4028 return 0;
4029
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004030 ret = __dwc3_gadget_start(dwc);
4031 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004032 goto err0;
4033
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004034 ret = dwc3_gadget_run_stop(dwc, true, false);
4035 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004036 goto err1;
4037
Felipe Balbi7415f172012-04-30 14:56:33 +03004038 return 0;
4039
4040err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004041 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004042
4043err0:
4044 return ret;
4045}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004046
4047void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4048{
4049 if (dwc->pending_events) {
4050 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4051 dwc->pending_events = false;
4052 enable_irq(dwc->irq_gadget);
4053 }
4054}