blob: a5d3209f7be521b2c8758fda47ef7c1cff93dc11 [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);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020042/**
43 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
44 * @dwc: pointer to our context structure
45 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
46 *
47 * Caller should take care of locking. This function will
48 * return 0 on success or -EINVAL if wrong Test Selector
49 * is passed
50 */
51int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
52{
53 u32 reg;
54
55 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
56 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
57
58 switch (mode) {
59 case TEST_J:
60 case TEST_K:
61 case TEST_SE0_NAK:
62 case TEST_PACKET:
63 case TEST_FORCE_EN:
64 reg |= mode << 1;
65 break;
66 default:
67 return -EINVAL;
68 }
69
70 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
71
72 return 0;
73}
74
Felipe Balbi8598bde2012-01-02 18:55:57 +020075/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030076 * dwc3_gadget_get_link_state - Gets current state of USB Link
77 * @dwc: pointer to our context structure
78 *
79 * Caller should take care of locking. This function will
80 * return the link state on success (>= 0) or -ETIMEDOUT.
81 */
82int dwc3_gadget_get_link_state(struct dwc3 *dwc)
83{
84 u32 reg;
85
86 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
87
88 return DWC3_DSTS_USBLNKST(reg);
89}
90
91/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
Paul Zimmerman802fde92012-04-27 13:10:52 +0300104 /*
105 * Wait until device controller is ready. Only applies to 1.94a and
106 * later RTL.
107 */
108 if (dwc->revision >= DWC3_REVISION_194A) {
109 while (--retries) {
110 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
111 if (reg & DWC3_DSTS_DCNRD)
112 udelay(5);
113 else
114 break;
115 }
116
117 if (retries <= 0)
118 return -ETIMEDOUT;
119 }
120
Felipe Balbi8598bde2012-01-02 18:55:57 +0200121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
122 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
123
124 /* set requested state */
125 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
126 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
127
Paul Zimmerman802fde92012-04-27 13:10:52 +0300128 /*
129 * The following code is racy when called from dwc3_gadget_wakeup,
130 * and is not needed, at least on newer versions
131 */
132 if (dwc->revision >= DWC3_REVISION_194A)
133 return 0;
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300136 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 while (--retries) {
138 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
139
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 if (DWC3_DSTS_USBLNKST(reg) == state)
141 return 0;
142
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800143 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144 }
145
Felipe Balbi73815282015-01-27 13:48:14 -0600146 dwc3_trace(trace_dwc3_gadget,
147 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200148
149 return -ETIMEDOUT;
150}
151
John Youndca01192016-05-19 17:26:05 -0700152/**
153 * dwc3_ep_inc_trb() - Increment a TRB index.
154 * @index - Pointer to the TRB index to increment.
155 *
156 * The index should never point to the link TRB. After incrementing,
157 * if it is point to the link TRB, wrap around to the beginning. The
158 * link TRB is always at the last TRB entry.
159 */
160static void dwc3_ep_inc_trb(u8 *index)
161{
162 (*index)++;
163 if (*index == (DWC3_TRB_NUM - 1))
164 *index = 0;
165}
166
Felipe Balbief966b92016-04-05 13:09:51 +0300167static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200168{
John Youndca01192016-05-19 17:26:05 -0700169 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300170}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200171
Felipe Balbief966b92016-04-05 13:09:51 +0300172static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800177/*
178 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
179 * @dwc: pointer to our context structure
180 *
181 * This function will a best effort FIFO allocation in order
182 * to improve FIFO usage and throughput, while still allowing
183 * us to enable as many endpoints as possible.
184 *
185 * Keep in mind that this operation will be highly dependent
186 * on the configured size for RAM1 - which contains TxFifo -,
187 * the amount of endpoints enabled on coreConsultant tool, and
188 * the width of the Master Bus.
189 *
190 * In the ideal world, we would always be able to satisfy the
191 * following equation:
192 *
193 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
194 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
195 *
196 * Unfortunately, due to many variables that's not always the case.
197 */
198int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
199{
200 int last_fifo_depth = 0;
201 int ram1_depth;
202 int fifo_size;
203 int mdwidth;
204 int num;
Jack Phamdb943d62016-12-16 11:21:16 -0800205 int num_eps;
Mayank Ranacb44a0c2015-06-29 12:05:45 -0700206 int max_packet = 1024;
Jack Phamdb943d62016-12-16 11:21:16 -0800207 struct usb_composite_dev *cdev = get_gadget_data(&dwc->gadget);
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800208
Hemant Kumarf0ce2552016-12-16 11:27:01 -0800209 if (!(cdev && cdev->config) || !dwc->needs_fifo_resize)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800210 return 0;
211
Devdutt Patnaik520de142016-05-19 14:22:37 -0700212 num_eps = dwc->num_in_eps;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800213 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
214 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
215
216 /* MDWIDTH is represented in bits, we need it in bytes */
217 mdwidth >>= 3;
Mayank Ranaa846a412016-12-16 11:41:45 -0800218 last_fifo_depth = (dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)) & 0xFFFF);
219 dev_dbg(dwc->dev, "%s: num eps:%d max_packet:%d last_fifo_depth:%04x\n",
220 __func__, num_eps, max_packet, last_fifo_depth);
221
222 /* Don't resize ep0IN TxFIFO, start with ep1IN only. */
223 for (num = 1; num < num_eps; num++) {
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800224 /* bit0 indicates direction; 1 means IN ep */
225 struct dwc3_ep *dep = dwc->eps[(num << 1) | 1];
226 int mult = 1;
227 int tmp;
228
Mayank Ranacefab302016-12-16 11:59:14 -0800229 tmp = max_packet + mdwidth;
230 /*
231 * Interfaces like MBIM or ECM is having multiple data
232 * interfaces. SET_CONFIG() happens before set_alt with
233 * data interface 1 which results into calling this API
234 * before GSI endpoint enabled. This results no txfifo
235 * resize with GSI endpoint causing low throughput. Hence
236 * use mult as 3 for GSI IN endpoint always irrespective
237 * USB speed.
238 */
Mayank Ranaae5ca3c2016-09-21 12:21:01 -0700239 if (dep->endpoint.ep_type == EP_TYPE_GSI ||
240 dep->endpoint.endless)
Mayank Ranacefab302016-12-16 11:59:14 -0800241 mult = 3;
242
Jack Phamdb943d62016-12-16 11:21:16 -0800243 if (!(dep->flags & DWC3_EP_ENABLED)) {
Devdutt Patnaik520de142016-05-19 14:22:37 -0700244 dev_dbg(dwc->dev, "ep%dIn not enabled", num);
Jack Phamdb943d62016-12-16 11:21:16 -0800245 goto resize_fifo;
246 }
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800247
Jack Phamdb943d62016-12-16 11:21:16 -0800248 if (((dep->endpoint.maxburst > 1) &&
249 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800250 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
251 mult = 3;
252
Jack Phamdb943d62016-12-16 11:21:16 -0800253resize_fifo:
Mayank Ranacefab302016-12-16 11:59:14 -0800254 tmp *= mult;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800255 tmp += mdwidth;
256
257 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
258
259 fifo_size |= (last_fifo_depth << 16);
260
Mayank Ranaa846a412016-12-16 11:41:45 -0800261 dev_dbg(dwc->dev, "%s: Fifo Addr %04x Size %d",
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800262 dep->name, last_fifo_depth, fifo_size & 0xffff);
263
Jack Phamdb943d62016-12-16 11:21:16 -0800264 last_fifo_depth += (fifo_size & 0xffff);
265 if (dwc->tx_fifo_size &&
266 (last_fifo_depth >= dwc->tx_fifo_size)) {
267 /*
268 * Fifo size allocated exceeded available RAM size.
269 * Hence return error.
270 */
271 dev_err(dwc->dev, "Fifosize(%d) > available RAM(%d)\n",
272 last_fifo_depth, dwc->tx_fifo_size);
273 return -ENOMEM;
274 }
275
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800276 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size);
277
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800278 }
279
280 return 0;
281}
282
Felipe Balbi72246da2011-08-19 18:10:58 +0300283void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
284 int status)
285{
286 struct dwc3 *dwc = dep->dwc;
287
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300288 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200290 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300291
292 if (req->request.status == -EINPROGRESS)
293 req->request.status = status;
294
Felipe Balbi5af6f562016-12-20 14:14:40 +0200295 if (dwc->ep0_bounced && dep->number <= 1)
Pratyush Anand0416e492012-08-10 13:42:16 +0530296 dwc->ep0_bounced = false;
Felipe Balbi5af6f562016-12-20 14:14:40 +0200297
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530298 usb_gadget_unmap_request_by_dev(dwc->sysdev,
299 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300300
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500301 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300302
303 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200304 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300306
307 if (dep->number > 1)
308 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300309}
310
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500311int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300312{
313 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300314 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300315 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300316 u32 reg;
317
318 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
319 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
320
321 do {
322 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
323 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300324 status = DWC3_DGCMD_STATUS(reg);
325 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300326 ret = -EINVAL;
327 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300328 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300329 } while (timeout--);
330
331 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300332 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300333 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300334 }
335
Felipe Balbi71f7e702016-05-23 14:16:19 +0300336 trace_dwc3_gadget_generic_cmd(cmd, param, status);
337
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300338 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300339}
340
Felipe Balbic36d8e92016-04-04 12:46:33 +0300341static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
342
Felipe Balbi2cd47182016-04-12 16:42:43 +0300343int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
344 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300345{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300346 struct dwc3 *dwc = dep->dwc;
Hemant Kumar43874172016-08-25 16:17:48 -0700347 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300348 u32 reg;
349
Felipe Balbi0933df12016-05-23 14:02:33 +0300350 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300351 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300352 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300353
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300354 /*
355 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
356 * we're issuing an endpoint command, we must check if
357 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
358 *
359 * We will also set SUSPHY bit to what it was before returning as stated
360 * by the same section on Synopsys databook.
361 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300362 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
363 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
364 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
365 susphy = true;
366 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
367 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
368 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300369 }
370
Felipe Balbic36d8e92016-04-04 12:46:33 +0300371 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
372 int needs_wakeup;
373
374 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
375 dwc->link_state == DWC3_LINK_STATE_U2 ||
376 dwc->link_state == DWC3_LINK_STATE_U3);
377
378 if (unlikely(needs_wakeup)) {
379 ret = __dwc3_gadget_wakeup(dwc);
380 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
381 ret);
382 }
383 }
384
Felipe Balbi2eb88012016-04-12 16:53:39 +0300385 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
386 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
387 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300388
Felipe Balbi2eb88012016-04-12 16:53:39 +0300389 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300391 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300392 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300393 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000394
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000395 switch (cmd_status) {
396 case 0:
397 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300398 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000399 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000400 ret = -EINVAL;
401 break;
402 case DEPEVT_TRANSFER_BUS_EXPIRY:
403 /*
404 * SW issues START TRANSFER command to
405 * isochronous ep with future frame interval. If
406 * future interval time has already passed when
407 * core receives the command, it will respond
408 * with an error status of 'Bus Expiry'.
409 *
410 * Instead of always returning -EINVAL, let's
411 * give a hint to the gadget driver that this is
412 * the case by returning -EAGAIN.
413 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000414 ret = -EAGAIN;
415 break;
416 default:
417 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
418 }
419
Felipe Balbic0ca3242016-04-04 09:11:51 +0300420 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300422 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300423
Felipe Balbif6bb2252016-05-23 13:53:34 +0300424 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300425 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700426 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
427 dev_err(dwc->dev, "%s command timeout for %s\n",
428 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Hemant Kumar43874172016-08-25 16:17:48 -0700429 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
430 dwc->ep_cmd_timeout_cnt++;
431 dwc3_notify_event(dwc,
432 DWC3_CONTROLLER_RESTART_USB_SESSION);
433 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300434 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300435 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300436
Felipe Balbi0933df12016-05-23 14:02:33 +0300437 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
438
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300439 if (unlikely(susphy)) {
440 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
441 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
442 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
443 }
444
Felipe Balbic0ca3242016-04-04 09:11:51 +0300445 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446}
447
John Youn50c763f2016-05-31 17:49:56 -0700448static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
449{
450 struct dwc3 *dwc = dep->dwc;
451 struct dwc3_gadget_ep_cmd_params params;
452 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
453
454 /*
455 * As of core revision 2.60a the recommended programming model
456 * is to set the ClearPendIN bit when issuing a Clear Stall EP
457 * command for IN endpoints. This is to prevent an issue where
458 * some (non-compliant) hosts may not send ACK TPs for pending
459 * IN transfers due to a mishandled error condition. Synopsys
460 * STAR 9000614252.
461 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800462 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
463 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700464 cmd |= DWC3_DEPCMD_CLEARPENDIN;
465
466 memset(&params, 0, sizeof(params));
467
Felipe Balbi2cd47182016-04-12 16:42:43 +0300468 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700469}
470
Felipe Balbi72246da2011-08-19 18:10:58 +0300471static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
472{
473 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700474 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
476 if (dep->trb_pool)
477 return 0;
478
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530479 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700480 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300481 &dep->trb_pool_dma, GFP_KERNEL);
482 if (!dep->trb_pool) {
483 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
484 dep->name);
485 return -ENOMEM;
486 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700487 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300488
489 return 0;
490}
491
492static void dwc3_free_trb_pool(struct dwc3_ep *dep)
493{
494 struct dwc3 *dwc = dep->dwc;
495
Mayank Ranaa99689a2016-08-10 17:39:47 -0700496 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
497 if (dep->endpoint.ep_type == EP_TYPE_GSI)
498 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Mayank Rana4dd882c2016-10-05 09:43:05 -0700500 /*
501 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
502 * with HWO bit set from previous composition when update transfer cmd
503 * is issued.
504 */
505 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
506 memset(&dep->trb_pool[0], 0,
507 sizeof(struct dwc3_trb) * dep->num_trbs);
Mayank Rana558baca2017-02-17 11:46:38 -0800508 dbg_event(dep->number, "Clr_TRB", 0);
Mayank Rana4dd882c2016-10-05 09:43:05 -0700509 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
510
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530511 dma_free_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700512 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
513 dep->trb_pool, dep->trb_pool_dma);
514 dep->trb_pool = NULL;
515 dep->trb_pool_dma = 0;
516 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300517}
518
John Younc4509602016-02-16 20:10:53 -0800519static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
520
521/**
522 * dwc3_gadget_start_config - Configure EP resources
523 * @dwc: pointer to our controller context structure
524 * @dep: endpoint that is being enabled
525 *
526 * The assignment of transfer resources cannot perfectly follow the
527 * data book due to the fact that the controller driver does not have
528 * all knowledge of the configuration in advance. It is given this
529 * information piecemeal by the composite gadget framework after every
530 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
531 * programming model in this scenario can cause errors. For two
532 * reasons:
533 *
534 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
535 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
536 * multiple interfaces.
537 *
538 * 2) The databook does not mention doing more DEPXFERCFG for new
539 * endpoint on alt setting (8.1.6).
540 *
541 * The following simplified method is used instead:
542 *
543 * All hardware endpoints can be assigned a transfer resource and this
544 * setting will stay persistent until either a core reset or
545 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
546 * do DEPXFERCFG for every hardware endpoint as well. We are
547 * guaranteed that there are as many transfer resources as endpoints.
548 *
549 * This function is called for each endpoint when it is being enabled
550 * but is triggered only when called for EP0-out, which always happens
551 * first, and which should only happen in one of the above conditions.
552 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300553static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
554{
555 struct dwc3_gadget_ep_cmd_params params;
556 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800557 int i;
558 int ret;
559
560 if (dep->number)
561 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562
563 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800564 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300565
Felipe Balbi2cd47182016-04-12 16:42:43 +0300566 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800567 if (ret)
568 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300569
John Younc4509602016-02-16 20:10:53 -0800570 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
571 struct dwc3_ep *dep = dwc->eps[i];
572
573 if (!dep)
574 continue;
575
576 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
577 if (ret)
578 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579 }
580
581 return 0;
582}
583
584static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200585 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300586 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300587 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300588{
589 struct dwc3_gadget_ep_cmd_params params;
590
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300591 if (dev_WARN_ONCE(dwc->dev, modify && restore,
592 "Can't modify and restore\n"))
593 return -EINVAL;
594
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 memset(&params, 0x00, sizeof(params));
596
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300597 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900598 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
599
600 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800601 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300602 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300603 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900604 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300605
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300606 if (modify) {
607 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
608 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600609 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
610 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300611 } else {
612 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600613 }
614
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300615 if (usb_endpoint_xfer_control(desc))
616 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300617
618 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
619 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300620
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200621 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300622 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
623 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300624 dep->stream_capable = true;
625 }
626
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500627 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300628 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629
630 /*
631 * We are doing 1:1 mapping for endpoints, meaning
632 * Physical Endpoints 2 maps to Logical Endpoint 2 and
633 * so on. We consider the direction bit as part of the physical
634 * endpoint number. So USB endpoint 0x81 is 0x03.
635 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300636 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300637
638 /*
639 * We must use the lower 16 TX FIFOs even though
640 * HW might have more
641 */
642 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300643 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
645 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300646 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 dep->interval = 1 << (desc->bInterval - 1);
648 }
649
Felipe Balbi2cd47182016-04-12 16:42:43 +0300650 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300651}
652
653static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
654{
655 struct dwc3_gadget_ep_cmd_params params;
656
657 memset(&params, 0x00, sizeof(params));
658
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300659 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300660
Felipe Balbi2cd47182016-04-12 16:42:43 +0300661 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
662 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300663}
664
665/**
666 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
667 * @dep: endpoint to be initialized
668 * @desc: USB Endpoint Descriptor
669 *
670 * Caller should take care of locking
671 */
672static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200673 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300674 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300675 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300676{
677 struct dwc3 *dwc = dep->dwc;
678 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300679 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300680
Felipe Balbi73815282015-01-27 13:48:14 -0600681 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 if (!(dep->flags & DWC3_EP_ENABLED)) {
684 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700685 if (ret) {
686 dev_err(dwc->dev, "start_config() failed for %s\n",
687 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700689 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300690 }
691
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300692 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600693 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700694 if (ret) {
695 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300696 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700697 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300698
699 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200700 struct dwc3_trb *trb_st_hw;
701 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300702
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200703 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200704 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 dep->type = usb_endpoint_type(desc);
706 dep->flags |= DWC3_EP_ENABLED;
707
708 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
709 reg |= DWC3_DALEPENA_EP(dep->number);
710 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
711
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300712 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300713 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300714
John Youn0d257442016-05-19 17:26:08 -0700715 /* Initialize the TRB ring */
716 dep->trb_dequeue = 0;
717 dep->trb_enqueue = 0;
718 memset(dep->trb_pool, 0,
719 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
720
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300721 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 trb_st_hw = &dep->trb_pool[0];
723
Felipe Balbif6bafc62012-02-06 11:04:53 +0200724 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200725 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
726 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
727 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
728 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 }
730
731 return 0;
732}
733
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200734static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300735{
736 struct dwc3_request *req;
737
Felipe Balbi0e146022016-06-21 10:32:02 +0300738 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300739
Felipe Balbi0e146022016-06-21 10:32:02 +0300740 /* - giveback all requests to gadget driver */
741 while (!list_empty(&dep->started_list)) {
742 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743
Felipe Balbi0e146022016-06-21 10:32:02 +0300744 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200745 }
746
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200747 while (!list_empty(&dep->pending_list)) {
748 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300749
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200750 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300752}
753
754/**
755 * __dwc3_gadget_ep_disable - Disables a HW endpoint
756 * @dep: the endpoint to disable
757 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200758 * This function also removes requests which are currently processed ny the
759 * hardware and those which are not yet scheduled.
760 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300762static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
763{
764 struct dwc3 *dwc = dep->dwc;
765 u32 reg;
766
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500767 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
768
Mayank Ranaa99689a2016-08-10 17:39:47 -0700769 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
770 dwc3_remove_requests(dwc, dep);
771 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
772 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300773
Felipe Balbi687ef982014-04-16 10:30:33 -0500774 /* make sure HW endpoint isn't stalled */
775 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500776 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500777
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
779 reg &= ~DWC3_DALEPENA_EP(dep->number);
780 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
781
Felipe Balbi879631a2011-09-30 10:58:47 +0300782 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200783 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200784 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300785 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300786 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300787
Mayank Ranaa99689a2016-08-10 17:39:47 -0700788 /* Keep GSI ep names with "-gsi" suffix */
789 if (!strnstr(dep->name, "gsi", 10)) {
790 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
791 dep->number >> 1,
792 (dep->number & 1) ? "in" : "out");
793 }
794
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 return 0;
796}
797
798/* -------------------------------------------------------------------------- */
799
800static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
801 const struct usb_endpoint_descriptor *desc)
802{
803 return -EINVAL;
804}
805
806static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
807{
808 return -EINVAL;
809}
810
811/* -------------------------------------------------------------------------- */
812
813static int dwc3_gadget_ep_enable(struct usb_ep *ep,
814 const struct usb_endpoint_descriptor *desc)
815{
816 struct dwc3_ep *dep;
817 struct dwc3 *dwc;
818 unsigned long flags;
819 int ret;
820
821 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
822 pr_debug("dwc3: invalid parameters\n");
823 return -EINVAL;
824 }
825
826 if (!desc->wMaxPacketSize) {
827 pr_debug("dwc3: missing wMaxPacketSize\n");
828 return -EINVAL;
829 }
830
831 dep = to_dwc3_ep(ep);
832 dwc = dep->dwc;
833
Felipe Balbi95ca9612015-12-10 13:08:20 -0600834 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
835 "%s is already enabled\n",
836 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300837 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300838
Felipe Balbi72246da2011-08-19 18:10:58 +0300839 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600840 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Mayank Rana558baca2017-02-17 11:46:38 -0800841 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300842 spin_unlock_irqrestore(&dwc->lock, flags);
843
844 return ret;
845}
846
847static int dwc3_gadget_ep_disable(struct usb_ep *ep)
848{
849 struct dwc3_ep *dep;
850 struct dwc3 *dwc;
851 unsigned long flags;
852 int ret;
853
854 if (!ep) {
855 pr_debug("dwc3: invalid parameters\n");
856 return -EINVAL;
857 }
858
859 dep = to_dwc3_ep(ep);
860 dwc = dep->dwc;
861
Felipe Balbi95ca9612015-12-10 13:08:20 -0600862 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
863 "%s is already disabled\n",
864 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300865 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300866
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -0800867 /* Keep GSI ep names with "-gsi" suffix */
868 if (!strnstr(dep->name, "gsi", 10)) {
869 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
870 dep->number >> 1,
871 (dep->number & 1) ? "in" : "out");
872 }
873
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 spin_lock_irqsave(&dwc->lock, flags);
875 ret = __dwc3_gadget_ep_disable(dep);
Mayank Rana558baca2017-02-17 11:46:38 -0800876 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 spin_unlock_irqrestore(&dwc->lock, flags);
878
879 return ret;
880}
881
882static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
883 gfp_t gfp_flags)
884{
885 struct dwc3_request *req;
886 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300887
888 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900889 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300890 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300891
892 req->epnum = dep->number;
893 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbi68d34c82016-05-30 13:34:58 +0300895 dep->allocated_requests++;
896
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500897 trace_dwc3_alloc_request(req);
898
Felipe Balbi72246da2011-08-19 18:10:58 +0300899 return &req->request;
900}
901
902static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
903 struct usb_request *request)
904{
905 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300906 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300907
Felipe Balbi68d34c82016-05-30 13:34:58 +0300908 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500909 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 kfree(req);
911}
912
Felipe Balbi2c78c022016-08-12 13:13:10 +0300913static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
914
Felipe Balbic71fc372011-11-22 11:37:34 +0200915/**
916 * dwc3_prepare_one_trb - setup one TRB from one request
917 * @dep: endpoint for which this request is prepared
918 * @req: dwc3_request pointer
919 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200920static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200921 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300922 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200923{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200924 struct dwc3_trb *trb;
Felipe Balbi3666b622016-09-22 11:01:01 +0300925 struct dwc3 *dwc = dep->dwc;
926 struct usb_gadget *gadget = &dwc->gadget;
927 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200928
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300929 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200930 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300931 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530932
Felipe Balbi4faf7552016-04-05 13:14:31 +0300933 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200934
Felipe Balbieeb720f2011-11-28 12:46:59 +0200935 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200936 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200937 req->trb = trb;
938 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300939 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300940 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200941 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200942
Felipe Balbief966b92016-04-05 13:09:51 +0300943 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530944
Felipe Balbif6bafc62012-02-06 11:04:53 +0200945 trb->size = DWC3_TRB_SIZE_LENGTH(length);
946 trb->bpl = lower_32_bits(dma);
947 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200948
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200949 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200950 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200951 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200952 break;
953
954 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi3666b622016-09-22 11:01:01 +0300955 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530956 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi3666b622016-09-22 11:01:01 +0300957
958 if (speed == USB_SPEED_HIGH) {
959 struct usb_ep *ep = &dep->endpoint;
960 trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
961 }
962 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530963 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi3666b622016-09-22 11:01:01 +0300964 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200965
966 /* always enable Interrupt on Missed ISOC */
967 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200968 break;
969
970 case USB_ENDPOINT_XFER_BULK:
971 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200972 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200973 break;
974 default:
975 /*
976 * This is only possible with faulty memory because we
977 * checked it already :)
978 */
979 BUG();
980 }
981
Felipe Balbica4d44e2016-03-10 13:53:27 +0200982 /* always enable Continue on Short Packet */
983 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600984
Felipe Balbi2c78c022016-08-12 13:13:10 +0300985 if ((!req->request.no_interrupt && !chain) ||
986 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbica4d44e2016-03-10 13:53:27 +0200987 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
988
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530989 if (chain)
990 trb->ctrl |= DWC3_TRB_CTRL_CHN;
991
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200992 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200993 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
994
995 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500996
997 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200998}
999
John Youn361572b2016-05-19 17:26:17 -07001000/**
1001 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
1002 * @dep: The endpoint with the TRB ring
1003 * @index: The index of the current TRB in the ring
1004 *
1005 * Returns the TRB prior to the one pointed to by the index. If the
1006 * index is 0, we will wrap backwards, skip the link TRB, and return
1007 * the one just before that.
1008 */
1009static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1010{
Felipe Balbi45438a02016-08-11 12:26:59 +03001011 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001012
Felipe Balbi45438a02016-08-11 12:26:59 +03001013 if (!tmp)
1014 tmp = DWC3_TRB_NUM - 1;
1015
1016 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001017}
1018
Felipe Balbic4233572016-05-12 14:08:34 +03001019static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1020{
1021 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001022 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001023
1024 /*
1025 * If enqueue & dequeue are equal than it is either full or empty.
1026 *
1027 * One way to know for sure is if the TRB right before us has HWO bit
1028 * set or not. If it has, then we're definitely full and can't fit any
1029 * more transfers in our ring.
1030 */
1031 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001032 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1033 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
1034 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001035
1036 return DWC3_TRB_NUM - 1;
1037 }
1038
John Youn9d7aba72016-08-26 18:43:01 -07001039 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001040 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001041
John Youn9d7aba72016-08-26 18:43:01 -07001042 if (dep->trb_dequeue < dep->trb_enqueue)
1043 trbs_left--;
1044
John Youn32db3d92016-05-19 17:26:12 -07001045 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001046}
1047
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001048static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001049 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001050{
Felipe Balbi1f512112016-08-12 13:17:27 +03001051 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001052 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001053 unsigned int length;
1054 dma_addr_t dma;
1055 int i;
1056
Felipe Balbi1f512112016-08-12 13:17:27 +03001057 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001058 unsigned chain = true;
1059
1060 length = sg_dma_len(s);
1061 dma = sg_dma_address(s);
1062
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001063 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001064 chain = false;
1065
1066 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001067 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001068
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001069 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001070 break;
1071 }
1072}
1073
1074static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001075 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001076{
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001077 unsigned int length;
1078 dma_addr_t dma;
1079
1080 dma = req->request.dma;
1081 length = req->request.length;
1082
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001083 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001084 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001085}
1086
Felipe Balbi72246da2011-08-19 18:10:58 +03001087/*
1088 * dwc3_prepare_trbs - setup TRBs from requests
1089 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001090 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001091 * The function goes through the requests list and sets up TRBs for the
1092 * transfers. The function returns once there are no more TRBs available or
1093 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001094 */
Felipe Balbic4233572016-05-12 14:08:34 +03001095static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001096{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001097 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001098
1099 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1100
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001101 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001102 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001103
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001104 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001105 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001106 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001107 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001108 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001109
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001110 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001111 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001112 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001113}
1114
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001115static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001116{
1117 struct dwc3_gadget_ep_cmd_params params;
1118 struct dwc3_request *req;
1119 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001120 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001121 int ret;
1122 u32 cmd;
1123
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001124 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001125
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001126 dwc3_prepare_trbs(dep);
1127 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001128 if (!req) {
1129 dep->flags |= DWC3_EP_PENDING_REQUEST;
Mayank Rana558baca2017-02-17 11:46:38 -08001130 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001131 return 0;
1132 }
1133
1134 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001135
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001136 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301137 params.param0 = upper_32_bits(req->trb_dma);
1138 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001139 cmd = DWC3_DEPCMD_STARTTRANSFER |
1140 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301141 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001142 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1143 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301144 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001145
Felipe Balbi2cd47182016-04-12 16:42:43 +03001146 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001148 /*
1149 * FIXME we need to iterate over the list of requests
1150 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001151 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001152 */
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301153 usb_gadget_unmap_request_by_dev(dwc->sysdev,
1154 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001155 list_del(&req->list);
1156 return ret;
1157 }
1158
1159 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001160
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001161 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001162 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001163 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001164 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001165
Felipe Balbi72246da2011-08-19 18:10:58 +03001166 return 0;
1167}
1168
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301169static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1170 struct dwc3_ep *dep, u32 cur_uf)
1171{
1172 u32 uf;
Mayank Rana558baca2017-02-17 11:46:38 -08001173 int ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301174
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001175 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001176 dwc3_trace(trace_dwc3_gadget,
1177 "ISOC ep %s run out for requests",
1178 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301179 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301180 return;
1181 }
1182
1183 /* 4 micro frames in the future */
1184 uf = cur_uf + dep->interval * 4;
1185
Mayank Rana558baca2017-02-17 11:46:38 -08001186 ret = __dwc3_gadget_kick_transfer(dep, uf);
1187 if (ret < 0)
1188 dbg_event(dep->number, "ISOC QUEUE", ret);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301189}
1190
1191static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1192 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1193{
1194 u32 cur_uf, mask;
1195
1196 mask = ~(dep->interval - 1);
1197 cur_uf = event->parameters & mask;
1198
1199 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1200}
1201
Felipe Balbi72246da2011-08-19 18:10:58 +03001202static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1203{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001204 struct dwc3 *dwc = dep->dwc;
1205 int ret;
1206
Felipe Balbibb423982015-11-16 15:31:21 -06001207 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001208 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001209 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001210 &req->request, dep->endpoint.name);
1211 return -ESHUTDOWN;
1212 }
1213
1214 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1215 &req->request, req->dep->name)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001216 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001217 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001218 return -EINVAL;
1219 }
1220
Manu Gautamb40ef612013-02-11 15:53:34 +05301221 if (req->request.status == -EINPROGRESS) {
1222 ret = -EBUSY;
1223 dev_err(dwc->dev, "%s: %p request already in queue",
1224 dep->name, req);
1225 return ret;
1226 }
Felipe Balbifc8bb912016-05-16 13:14:48 +03001227
Manu Gautamb40ef612013-02-11 15:53:34 +05301228 pm_runtime_get(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001229 req->request.actual = 0;
1230 req->request.status = -EINPROGRESS;
1231 req->direction = dep->direction;
1232 req->epnum = dep->number;
1233
Felipe Balbife84f522015-09-01 09:01:38 -05001234 trace_dwc3_ep_queue(req);
1235
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301236 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1237 dep->direction);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001238 if (ret)
1239 return ret;
1240
Felipe Balbi1f512112016-08-12 13:17:27 +03001241 req->sg = req->request.sg;
1242 req->num_pending_sgs = req->request.num_mapped_sgs;
1243
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001244 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001245
Felipe Balbid889c232016-09-29 15:44:29 +03001246 /*
1247 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1248 * wait for a XferNotReady event so we will know what's the current
1249 * (micro-)frame number.
1250 *
1251 * Without this trick, we are very, very likely gonna get Bus Expiry
1252 * errors which will force us issue EndTransfer command.
1253 */
1254 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1255 if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1256 list_empty(&dep->started_list)) {
Felipe Balbi08a36b52016-08-11 14:27:52 +03001257 dwc3_stop_active_transfer(dwc, dep->number, true);
1258 dep->flags = DWC3_EP_ENABLED;
1259 }
1260 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001261 }
1262
Felipe Balbi594e1212016-08-24 14:38:10 +03001263 if (!dwc3_calc_trbs_left(dep))
1264 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001265
Felipe Balbi08a36b52016-08-11 14:27:52 +03001266 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001267 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001268 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001269 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001270 dep->name);
1271 if (ret == -EBUSY)
1272 ret = 0;
1273
1274 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001275}
1276
Mayank Ranaa99689a2016-08-10 17:39:47 -07001277static int dwc3_gadget_wakeup(struct usb_gadget *g)
1278{
1279 struct dwc3 *dwc = gadget_to_dwc(g);
1280
1281 schedule_work(&dwc->wakeup_work);
1282 return 0;
1283}
1284
Mayank Ranaa99689a2016-08-10 17:39:47 -07001285static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1286{
1287 if (atomic_read(&dwc->in_lpm) ||
1288 dwc->link_state == DWC3_LINK_STATE_U3)
1289 return true;
1290 return false;
1291}
1292
Felipe Balbi04c03d12015-12-02 10:06:45 -06001293static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1294 struct usb_request *request)
1295{
1296 dwc3_gadget_ep_free_request(ep, request);
1297}
1298
1299static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1300{
1301 struct dwc3_request *req;
1302 struct usb_request *request;
1303 struct usb_ep *ep = &dep->endpoint;
1304
Felipe Balbi60cfb372016-05-24 13:45:17 +03001305 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001306 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1307 if (!request)
1308 return -ENOMEM;
1309
1310 request->length = 0;
1311 request->buf = dwc->zlp_buf;
1312 request->complete = __dwc3_gadget_ep_zlp_complete;
1313
1314 req = to_dwc3_request(request);
1315
1316 return __dwc3_gadget_ep_queue(dep, req);
1317}
1318
Felipe Balbi72246da2011-08-19 18:10:58 +03001319static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1320 gfp_t gfp_flags)
1321{
1322 struct dwc3_request *req = to_dwc3_request(request);
1323 struct dwc3_ep *dep = to_dwc3_ep(ep);
1324 struct dwc3 *dwc = dep->dwc;
1325
1326 unsigned long flags;
1327
1328 int ret;
1329
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001330 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001331 if (!dep->endpoint.desc) {
1332 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1333 request, ep->name);
1334 ret = -ESHUTDOWN;
1335 goto out;
1336 }
1337
1338 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1339 request, req->dep->name)) {
1340 ret = -EINVAL;
1341 goto out;
1342 }
1343
1344 if (dwc3_gadget_is_suspended(dwc)) {
1345 if (dwc->gadget.remote_wakeup)
1346 dwc3_gadget_wakeup(&dwc->gadget);
1347 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1348 goto out;
1349 }
1350
Manu Gautam3df6a322017-03-06 16:24:59 -08001351 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1352 "trying to queue unaligned request (%d) with %s\n",
1353 request->length, ep->name);
1354
Felipe Balbi72246da2011-08-19 18:10:58 +03001355 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001356
1357 /*
1358 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1359 * setting request->zero, instead of doing magic, we will just queue an
1360 * extra usb_request ourselves so that it gets handled the same way as
1361 * any other request.
1362 */
John Yound92618982015-12-22 12:23:20 -08001363 if (ret == 0 && request->zero && request->length &&
1364 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001365 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1366
Mayank Ranaa99689a2016-08-10 17:39:47 -07001367out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 spin_unlock_irqrestore(&dwc->lock, flags);
1369
1370 return ret;
1371}
1372
1373static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1374 struct usb_request *request)
1375{
1376 struct dwc3_request *req = to_dwc3_request(request);
1377 struct dwc3_request *r = NULL;
1378
1379 struct dwc3_ep *dep = to_dwc3_ep(ep);
1380 struct dwc3 *dwc = dep->dwc;
1381
1382 unsigned long flags;
1383 int ret = 0;
1384
Mayank Ranaa99689a2016-08-10 17:39:47 -07001385 if (atomic_read(&dwc->in_lpm)) {
1386 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1387 return -EAGAIN;
1388 }
1389
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001390 trace_dwc3_ep_dequeue(req);
1391
Felipe Balbi72246da2011-08-19 18:10:58 +03001392 spin_lock_irqsave(&dwc->lock, flags);
1393
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001394 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001395 if (r == req)
1396 break;
1397 }
1398
1399 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001400 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001401 if (r == req)
1402 break;
1403 }
1404 if (r == req) {
1405 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001406 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301407 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001408 }
1409 dev_err(dwc->dev, "request %p was not queued to %s\n",
1410 request, ep->name);
1411 ret = -EINVAL;
1412 goto out0;
1413 }
1414
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301415out1:
Mayank Rana558baca2017-02-17 11:46:38 -08001416 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 /* giveback the request */
1418 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1419
1420out0:
1421 spin_unlock_irqrestore(&dwc->lock, flags);
1422
1423 return ret;
1424}
1425
Felipe Balbi7a608552014-09-24 14:19:52 -05001426int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001427{
1428 struct dwc3_gadget_ep_cmd_params params;
1429 struct dwc3 *dwc = dep->dwc;
1430 int ret;
1431
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001432 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1433 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1434 return -EINVAL;
1435 }
1436
Felipe Balbi72246da2011-08-19 18:10:58 +03001437 memset(&params, 0x00, sizeof(params));
Mayank Rana558baca2017-02-17 11:46:38 -08001438 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001439 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001440 struct dwc3_trb *trb;
1441
1442 unsigned transfer_in_flight;
1443 unsigned started;
1444
Felipe Balbi3ccf60e2017-01-19 13:38:42 +02001445 if (dep->flags & DWC3_EP_STALL)
1446 return 0;
1447
Felipe Balbi69450c42016-05-30 13:37:02 +03001448 if (dep->number > 1)
1449 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1450 else
1451 trb = &dwc->ep0_trb[dep->trb_enqueue];
1452
1453 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1454 started = !list_empty(&dep->started_list);
1455
1456 if (!protocol && ((dep->direction && transfer_in_flight) ||
1457 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001458 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001459 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001460 dep->name);
1461 return -EAGAIN;
1462 }
1463
Felipe Balbi2cd47182016-04-12 16:42:43 +03001464 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1465 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001467 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001468 dep->name);
1469 else
1470 dep->flags |= DWC3_EP_STALL;
1471 } else {
Felipe Balbi3ccf60e2017-01-19 13:38:42 +02001472 if (!(dep->flags & DWC3_EP_STALL))
1473 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001474
John Youn50c763f2016-05-31 17:49:56 -07001475 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001477 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001478 dep->name);
1479 else
Alan Sterna535d812013-11-01 12:05:12 -04001480 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001481 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001482
Felipe Balbi72246da2011-08-19 18:10:58 +03001483 return ret;
1484}
1485
1486static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1487{
1488 struct dwc3_ep *dep = to_dwc3_ep(ep);
1489 struct dwc3 *dwc = dep->dwc;
1490
1491 unsigned long flags;
1492
1493 int ret;
1494
Mayank Ranaa99689a2016-08-10 17:39:47 -07001495 if (!ep->desc) {
1496 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1497 return -EINVAL;
1498 }
1499
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001501 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001502 spin_unlock_irqrestore(&dwc->lock, flags);
1503
1504 return ret;
1505}
1506
1507static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1508{
1509 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001510 struct dwc3 *dwc = dep->dwc;
1511 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001512 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001513
Paul Zimmerman249a4562012-02-24 17:32:16 -08001514 spin_lock_irqsave(&dwc->lock, flags);
Mayank Rana558baca2017-02-17 11:46:38 -08001515 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 dep->flags |= DWC3_EP_WEDGE;
1517
Pratyush Anand08f0d962012-06-25 22:40:43 +05301518 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001519 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301520 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001521 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001522 spin_unlock_irqrestore(&dwc->lock, flags);
1523
1524 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001525}
1526
1527/* -------------------------------------------------------------------------- */
1528
1529static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1530 .bLength = USB_DT_ENDPOINT_SIZE,
1531 .bDescriptorType = USB_DT_ENDPOINT,
1532 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1533};
1534
1535static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1536 .enable = dwc3_gadget_ep0_enable,
1537 .disable = dwc3_gadget_ep0_disable,
1538 .alloc_request = dwc3_gadget_ep_alloc_request,
1539 .free_request = dwc3_gadget_ep_free_request,
1540 .queue = dwc3_gadget_ep0_queue,
1541 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301542 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001543 .set_wedge = dwc3_gadget_ep_set_wedge,
1544};
1545
1546static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1547 .enable = dwc3_gadget_ep_enable,
1548 .disable = dwc3_gadget_ep_disable,
1549 .alloc_request = dwc3_gadget_ep_alloc_request,
1550 .free_request = dwc3_gadget_ep_free_request,
1551 .queue = dwc3_gadget_ep_queue,
1552 .dequeue = dwc3_gadget_ep_dequeue,
1553 .set_halt = dwc3_gadget_ep_set_halt,
1554 .set_wedge = dwc3_gadget_ep_set_wedge,
1555};
1556
1557/* -------------------------------------------------------------------------- */
1558
1559static int dwc3_gadget_get_frame(struct usb_gadget *g)
1560{
1561 struct dwc3 *dwc = gadget_to_dwc(g);
1562 u32 reg;
1563
1564 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1565 return DWC3_DSTS_SOFFN(reg);
1566}
1567
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001568static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001569{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001570 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001571
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001572 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 u32 reg;
1574
Felipe Balbi72246da2011-08-19 18:10:58 +03001575 u8 link_state;
1576 u8 speed;
1577
Felipe Balbi72246da2011-08-19 18:10:58 +03001578 /*
1579 * According to the Databook Remote wakeup request should
1580 * be issued only when the device is in early suspend state.
1581 *
1582 * We can check that via USB Link State bits in DSTS register.
1583 */
1584 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1585
1586 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001587 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1588 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001589 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001590 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001591 }
1592
1593 link_state = DWC3_DSTS_USBLNKST(reg);
1594
1595 switch (link_state) {
1596 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1597 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1598 break;
1599 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001600 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001601 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001602 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001603 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001604 }
1605
Felipe Balbi8598bde2012-01-02 18:55:57 +02001606 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1607 if (ret < 0) {
1608 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001609 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001610 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001611
Paul Zimmerman802fde92012-04-27 13:10:52 +03001612 /* Recent versions do this automatically */
1613 if (dwc->revision < DWC3_REVISION_194A) {
1614 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001615 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001616 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1617 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1618 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001619
Paul Zimmerman1d046792012-02-15 18:56:56 -08001620 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001621 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001622
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001623 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001624 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1625
1626 /* in HS, means ON */
1627 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1628 break;
1629 }
1630
1631 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1632 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001633 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001634 }
1635
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001636 return 0;
1637}
1638
Mayank Ranaa99689a2016-08-10 17:39:47 -07001639#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1640#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1641
1642static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001643{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001644 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001645 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001646 static int retry_count;
1647
1648 dwc = container_of(w, struct dwc3, wakeup_work);
1649
1650 ret = pm_runtime_get_sync(dwc->dev);
1651 if (ret) {
1652 /* pm_runtime_get_sync returns -EACCES error between
1653 * late_suspend and early_resume, wait for system resume to
1654 * finish and queue work again
1655 */
1656 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1657 if (ret == -EACCES) {
1658 pm_runtime_put_noidle(dwc->dev);
1659 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1660 retry_count = 0;
1661 pr_err("pm_runtime_get_sync timed out\n");
1662 return;
1663 }
1664 msleep(DWC3_PM_RESUME_DELAY);
1665 retry_count++;
1666 schedule_work(&dwc->wakeup_work);
1667 return;
1668 }
1669 }
1670 retry_count = 0;
Mayank Rana558baca2017-02-17 11:46:38 -08001671 dbg_event(0xFF, "Gdgwake gsyn",
1672 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001673
1674 ret = dwc3_gadget_wakeup_int(dwc);
1675
1676 if (ret)
1677 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1678 else
1679 pr_debug("Remote wakeup succeeded.\n");
1680
1681 pm_runtime_put_noidle(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001682 dbg_event(0xFF, "Gdgwake put",
1683 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001684}
1685
1686static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1687{
1688 bool link_recover_only = false;
1689
1690 u32 reg;
1691 int ret = 0;
1692 u8 link_state;
1693 unsigned long flags;
1694
1695 pr_debug("%s(): Entry\n", __func__);
1696 disable_irq(dwc->irq);
1697 spin_lock_irqsave(&dwc->lock, flags);
1698 /*
1699 * According to the Databook Remote wakeup request should
1700 * be issued only when the device is in early suspend state.
1701 *
1702 * We can check that via USB Link State bits in DSTS register.
1703 */
1704 link_state = dwc3_get_link_state(dwc);
1705
1706 switch (link_state) {
1707 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1708 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1709 break;
1710 case DWC3_LINK_STATE_U1:
1711 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1712 link_recover_only = true;
1713 break;
1714 }
1715 /* Intentional fallthrough */
1716 default:
1717 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1718 link_state);
1719 ret = -EINVAL;
1720 goto out;
1721 }
1722
1723 /* Enable LINK STATUS change event */
1724 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1725 reg |= DWC3_DEVTEN_ULSTCNGEN;
1726 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1727 /*
1728 * memory barrier is required to make sure that required events
1729 * with core is enabled before performing RECOVERY mechnism.
1730 */
1731 mb();
1732
1733 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1734 if (ret < 0) {
1735 dev_err(dwc->dev, "failed to put link in Recovery\n");
1736 /* Disable LINK STATUS change */
1737 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1738 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1739 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1740 /* Required to complete this operation before returning */
1741 mb();
1742 goto out;
1743 }
1744
1745 /* Recent versions do this automatically */
1746 if (dwc->revision < DWC3_REVISION_194A) {
1747 /* write zeroes to Link Change Request */
1748 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1749 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1750 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1751 }
1752
1753 spin_unlock_irqrestore(&dwc->lock, flags);
1754 enable_irq(dwc->irq);
1755
1756 /*
1757 * Have bigger value (16 sec) for timeout since some host PCs driving
1758 * resume for very long time (e.g. 8 sec)
1759 */
1760 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1761 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1762 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1763 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001764
1765 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001766 /* Disable link status change event */
1767 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1768 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1769 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1770 /*
1771 * Complete this write before we go ahead and perform resume
1772 * as we don't need link status change notificaiton anymore.
1773 */
1774 mb();
1775
1776 if (!ret) {
1777 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1778 dwc->link_state);
1779 ret = -EINVAL;
1780 spin_unlock_irqrestore(&dwc->lock, flags);
1781 goto out1;
1782 } else {
1783 ret = 0;
1784 /*
1785 * If USB is disconnected OR received RESET from host,
1786 * don't perform resume
1787 */
1788 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1789 dwc->gadget.state == USB_STATE_DEFAULT)
1790 link_recover_only = true;
1791 }
1792
1793 /*
1794 * According to DWC3 databook, the controller does not
1795 * trigger a wakeup event when remote-wakeup is used.
1796 * Hence, after remote-wakeup sequence is complete, and
1797 * the device is back at U0 state, it is required that
1798 * the resume sequence is initiated by SW.
1799 */
1800 if (!link_recover_only)
1801 dwc3_gadget_wakeup_interrupt(dwc, true);
1802
Felipe Balbi72246da2011-08-19 18:10:58 +03001803 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001804 pr_debug("%s: Exit\n", __func__);
1805 return ret;
1806
1807out:
1808 spin_unlock_irqrestore(&dwc->lock, flags);
1809 enable_irq(dwc->irq);
1810
1811out1:
1812 return ret;
1813}
1814
1815static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1816{
1817 int ret = 0;
1818 struct dwc3 *dwc = gadget_to_dwc(g);
1819
1820 if (!g || (g->speed != USB_SPEED_SUPER))
1821 return -ENOTSUPP;
1822
1823 if (dwc3_gadget_is_suspended(dwc)) {
1824 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1825 dwc3_gadget_wakeup(&dwc->gadget);
1826 return -EAGAIN;
1827 }
1828
1829 if (dwc->revision < DWC3_REVISION_220A) {
1830 ret = dwc3_send_gadget_generic_command(dwc,
1831 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1832 } else {
1833 ret = dwc3_send_gadget_generic_command(dwc,
1834 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1835 }
1836
1837 if (ret)
1838 pr_err("Function wakeup HW command failed.\n");
1839 else
1840 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001841
1842 return ret;
1843}
1844
1845static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1846 int is_selfpowered)
1847{
1848 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001849 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001850
Paul Zimmerman249a4562012-02-24 17:32:16 -08001851 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001852 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001853 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001854
1855 return 0;
1856}
1857
Mayank Ranaa99689a2016-08-10 17:39:47 -07001858#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001859static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001860{
1861 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001862 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001863
Felipe Balbi72246da2011-08-19 18:10:58 +03001864 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001865 if (is_on) {
Mayank Rana558baca2017-02-17 11:46:38 -08001866 dbg_event(0xFF, "Pullup_enable", is_on);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001867 if (dwc->revision <= DWC3_REVISION_187A) {
1868 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1869 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1870 }
1871
1872 if (dwc->revision >= DWC3_REVISION_194A)
1873 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001874
1875 dwc3_event_buffers_setup(dwc);
1876 dwc3_gadget_restart(dwc);
1877 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001878 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001879
1880 if (dwc->has_hibernation)
1881 reg |= DWC3_DCTL_KEEP_CONNECT;
1882
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001883 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001884 } else {
Mayank Rana558baca2017-02-17 11:46:38 -08001885 dbg_event(0xFF, "Pullup_disable", is_on);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001886 dwc3_gadget_disable_irq(dwc);
1887 __dwc3_gadget_ep_disable(dwc->eps[0]);
1888 __dwc3_gadget_ep_disable(dwc->eps[1]);
1889
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001891
1892 if (dwc->has_hibernation && !suspend)
1893 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1894
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001895 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001896 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001897
1898 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1899
1900 do {
1901 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001902 reg &= DWC3_DSTS_DEVCTRLHLT;
1903 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001904
Mayank Ranaa99689a2016-08-10 17:39:47 -07001905 if (!timeout) {
1906 dev_err(dwc->dev, "failed to %s controller\n",
1907 is_on ? "start" : "stop");
Mayank Rana558baca2017-02-17 11:46:38 -08001908 if (is_on)
1909 dbg_event(0xFF, "STARTTOUT", reg);
1910 else
1911 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03001912 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001913 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001914
Felipe Balbi73815282015-01-27 13:48:14 -06001915 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001916 dwc->gadget_driver
1917 ? dwc->gadget_driver->function : "no-function",
1918 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301919
1920 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001921}
1922
Mayank Ranaa99689a2016-08-10 17:39:47 -07001923static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
1924{
1925 struct dwc3 *dwc = gadget_to_dwc(g);
1926
1927 dwc->vbus_draw = mA;
1928 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
Mayank Rana558baca2017-02-17 11:46:38 -08001929 dbg_event(0xFF, "currentDraw", mA);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001930 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT);
1931 return 0;
1932}
1933
Felipe Balbi72246da2011-08-19 18:10:58 +03001934static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1935{
1936 struct dwc3 *dwc = gadget_to_dwc(g);
1937 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301938 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001939
1940 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001941 dwc->softconnect = is_on;
1942 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
1943 /*
1944 * Need to wait for vbus_session(on) from otg driver or to
1945 * the udc_start.
1946 */
Mayank Rana558baca2017-02-17 11:46:38 -08001947 dbg_event(0xFF, "WaitPullup", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001948 return 0;
1949 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001950
Mayank Ranaa99689a2016-08-10 17:39:47 -07001951 pm_runtime_get_sync(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001952 dbg_event(0xFF, "Pullup gsync",
1953 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03001954 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001955 /*
1956 * If we are here after bus suspend notify otg state machine to
1957 * increment pm usage count of dwc to prevent pm_runtime_suspend
1958 * during enumeration.
1959 */
1960 dwc->b_suspend = false;
1961 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001962 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001963 spin_unlock_irqrestore(&dwc->lock, flags);
1964
Mayank Ranaa99689a2016-08-10 17:39:47 -07001965 pm_runtime_mark_last_busy(dwc->dev);
1966 pm_runtime_put_autosuspend(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001967 dbg_event(0xFF, "Pullup put",
1968 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05301969 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001970}
1971
Mayank Ranaa99689a2016-08-10 17:39:47 -07001972void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001973{
1974 u32 reg;
1975
Mayank Rana558baca2017-02-17 11:46:38 -08001976 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001977 /* Enable all but Start and End of Frame IRQs */
1978 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1979 DWC3_DEVTEN_EVNTOVERFLOWEN |
1980 DWC3_DEVTEN_CMDCMPLTEN |
1981 DWC3_DEVTEN_ERRTICERREN |
1982 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001983 DWC3_DEVTEN_CONNECTDONEEN |
1984 DWC3_DEVTEN_USBRSTEN |
1985 DWC3_DEVTEN_DISCONNEVTEN);
1986
Mayank Ranaa99689a2016-08-10 17:39:47 -07001987 /*
1988 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
1989 * else enable USB Link change event (BIT:3) for older version
1990 */
1991 if (dwc->revision < DWC3_REVISION_230A)
1992 reg |= DWC3_DEVTEN_ULSTCNGEN;
1993 else
1994 reg |= DWC3_DEVTEN_SUSPEND;
1995
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001996 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1997}
1998
Mayank Ranaa99689a2016-08-10 17:39:47 -07001999void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002000{
Mayank Rana558baca2017-02-17 11:46:38 -08002001 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002002 /* mask all interrupts */
2003 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2004}
2005
Felipe Balbib15a7622011-06-30 16:57:15 +03002006static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002007static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002008
Felipe Balbi4e994722016-05-13 14:09:59 +03002009/**
2010 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
2011 * dwc: pointer to our context structure
2012 *
2013 * The following looks like complex but it's actually very simple. In order to
2014 * calculate the number of packets we can burst at once on OUT transfers, we're
2015 * gonna use RxFIFO size.
2016 *
2017 * To calculate RxFIFO size we need two numbers:
2018 * MDWIDTH = size, in bits, of the internal memory bus
2019 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2020 *
2021 * Given these two numbers, the formula is simple:
2022 *
2023 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2024 *
2025 * 24 bytes is for 3x SETUP packets
2026 * 16 bytes is a clock domain crossing tolerance
2027 *
2028 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2029 */
2030static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2031{
2032 u32 ram2_depth;
2033 u32 mdwidth;
2034 u32 nump;
2035 u32 reg;
2036
2037 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2038 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2039
2040 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2041 nump = min_t(u32, nump, 16);
2042
2043 /* update NumP */
2044 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2045 reg &= ~DWC3_DCFG_NUMP_MASK;
2046 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2047 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2048}
2049
Mayank Ranaa99689a2016-08-10 17:39:47 -07002050static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2051{
2052 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2053 unsigned long flags;
2054
2055 if (!dwc->is_drd)
2056 return -EPERM;
2057
2058 is_active = !!is_active;
2059
Mayank Rana558baca2017-02-17 11:46:38 -08002060 dbg_event(0xFF, "VbusSess", is_active);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002061 spin_lock_irqsave(&dwc->lock, flags);
2062
2063 /* Mark that the vbus was powered */
2064 dwc->vbus_active = is_active;
2065
2066 /*
2067 * Check if upper level usb_gadget_driver was already registered with
2068 * this udc controller driver (if dwc3_gadget_start was called)
2069 */
2070 if (dwc->gadget_driver && dwc->softconnect) {
2071 if (dwc->vbus_active) {
2072 /*
2073 * Both vbus was activated by otg and pullup was
2074 * signaled by the gadget driver.
2075 */
2076 dwc3_gadget_run_stop(dwc, 1, false);
2077 } else {
2078 dwc3_gadget_run_stop(dwc, 0, false);
2079 }
2080 }
2081
2082 /*
2083 * Clearing run/stop bit might occur before disconnect event is seen.
2084 * Make sure to let gadget driver know in that case.
2085 */
2086 if (!dwc->vbus_active) {
2087 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2088 dwc3_gadget_disconnect_interrupt(dwc);
2089 }
2090
2091 spin_unlock_irqrestore(&dwc->lock, flags);
2092 return 0;
2093}
2094
Felipe Balbid7be2952016-05-04 15:49:37 +03002095static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002096{
Felipe Balbi72246da2011-08-19 18:10:58 +03002097 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002098 int ret = 0;
2099 u32 reg;
2100
Mayank Rana558baca2017-02-17 11:46:38 -08002101 dbg_event(0xFF, "__Gadgetstart", 0);
John Youn26cac202016-11-14 12:32:43 -08002102
2103 /*
2104 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2105 * the core supports IMOD, disable it.
2106 */
2107 if (dwc->imod_interval) {
2108 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2109 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2110 } else if (dwc3_has_imod(dwc)) {
2111 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2112 }
2113
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2115 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02002116
2117 /**
2118 * WORKAROUND: DWC3 revision < 2.20a have an issue
2119 * which would cause metastability state on Run/Stop
2120 * bit if we try to force the IP to USB2-only mode.
2121 *
2122 * Because of that, we cannot configure the IP to any
2123 * speed other than the SuperSpeed
2124 *
2125 * Refers to:
2126 *
2127 * STAR#9000525659: Clock Domain Crossing on DCTL in
2128 * USB 2.0 Mode
2129 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03002130 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02002131 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002132 } else {
2133 switch (dwc->maximum_speed) {
2134 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07002135 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002136 break;
2137 case USB_SPEED_FULL:
Roger Quadros5e3c2922017-01-03 14:32:09 +02002138 reg |= DWC3_DCFG_FULLSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002139 break;
2140 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07002141 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002142 break;
John Youn75808622016-02-05 17:09:13 -08002143 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07002144 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08002145 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002146 default:
John Youn77966eb2016-02-19 17:31:01 -08002147 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07002148 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08002149 /* fall through */
2150 case USB_SPEED_SUPER:
2151 reg |= DWC3_DCFG_SUPERSPEED;
2152 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002153 }
2154 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002155 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2156
Mayank Ranaa99689a2016-08-10 17:39:47 -07002157 /* Programs the number of outstanding pipelined transfer requests
2158 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002159 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07002160 if (dwc->revision >= DWC3_REVISION_270A) {
2161 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2162 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2163 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2164 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2165 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002166
Felipe Balbi4e994722016-05-13 14:09:59 +03002167 dwc3_gadget_setup_nump(dwc);
2168
Felipe Balbi72246da2011-08-19 18:10:58 +03002169 /* Start with SuperSpeed Default */
2170 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2171
Mayank Ranaa99689a2016-08-10 17:39:47 -07002172 dwc->delayed_status = false;
2173 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002174 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002175 dep->flags = 0;
2176 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002177 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2178 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002179 if (ret) {
2180 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002181 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002182 }
2183
2184 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002185 dep->flags = 0;
2186 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002187 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2188 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 if (ret) {
2190 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002191 __dwc3_gadget_ep_disable(dwc->eps[0]);
2192 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 }
2194
2195 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002196 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 dwc3_ep0_out_start(dwc);
2198
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002199 dwc3_gadget_enable_irq(dwc);
2200
Felipe Balbid7be2952016-05-04 15:49:37 +03002201 return ret;
2202}
2203
Mayank Ranaa99689a2016-08-10 17:39:47 -07002204/* Required gadget re-initialization before switching to gadget in OTG mode */
2205void dwc3_gadget_restart(struct dwc3 *dwc)
2206{
2207 __dwc3_gadget_start(dwc);
2208}
2209
Felipe Balbid7be2952016-05-04 15:49:37 +03002210static int dwc3_gadget_start(struct usb_gadget *g,
2211 struct usb_gadget_driver *driver)
2212{
2213 struct dwc3 *dwc = gadget_to_dwc(g);
2214 unsigned long flags;
2215 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002216
2217 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002218
Felipe Balbid7be2952016-05-04 15:49:37 +03002219 if (dwc->gadget_driver) {
2220 dev_err(dwc->dev, "%s is already bound to %s\n",
2221 dwc->gadget.name,
2222 dwc->gadget_driver->driver.name);
2223 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002224 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002225 }
2226
2227 dwc->gadget_driver = driver;
2228
Mayank Ranaa99689a2016-08-10 17:39:47 -07002229 /*
2230 * For DRD, this might get called by gadget driver during bootup
2231 * even though host mode might be active. Don't actually perform
2232 * device-specific initialization until device mode is activated.
2233 * In that case dwc3_gadget_restart() will handle it.
2234 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002235 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002236 return 0;
2237
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002238err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002239 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002240 return ret;
2241}
2242
Felipe Balbid7be2952016-05-04 15:49:37 +03002243static void __dwc3_gadget_stop(struct dwc3 *dwc)
2244{
Mayank Rana558baca2017-02-17 11:46:38 -08002245 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002246 dwc3_gadget_disable_irq(dwc);
2247 __dwc3_gadget_ep_disable(dwc->eps[0]);
2248 __dwc3_gadget_ep_disable(dwc->eps[1]);
2249}
2250
Felipe Balbi22835b82014-10-17 12:05:12 -05002251static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002252{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002253 struct dwc3 *dwc = gadget_to_dwc(g);
2254 unsigned long flags;
2255
Felipe Balbi72246da2011-08-19 18:10:58 +03002256 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002257 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002258 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002259
2260 return 0;
2261}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002262
Mayank Ranaa99689a2016-08-10 17:39:47 -07002263static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2264{
2265 struct dwc3 *dwc = gadget_to_dwc(g);
2266
Mayank Rana558baca2017-02-17 11:46:38 -08002267 dbg_event(0xFF, "RestartUSBSession", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002268 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION);
2269}
2270
Felipe Balbi72246da2011-08-19 18:10:58 +03002271static const struct usb_gadget_ops dwc3_gadget_ops = {
2272 .get_frame = dwc3_gadget_get_frame,
2273 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002274 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002275 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002276 .vbus_session = dwc3_gadget_vbus_session,
2277 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002278 .pullup = dwc3_gadget_pullup,
2279 .udc_start = dwc3_gadget_start,
2280 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002281 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002282};
2283
2284/* -------------------------------------------------------------------------- */
2285
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002286#define NUM_GSI_OUT_EPS 1
2287#define NUM_GSI_IN_EPS 2
2288
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002289static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2290 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002291{
2292 struct dwc3_ep *dep;
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002293 u8 i, gsi_ep_count, gsi_ep_index = 0;
2294
2295 gsi_ep_count = NUM_GSI_OUT_EPS + NUM_GSI_IN_EPS;
2296 /* OUT GSI EPs based on direction field */
2297 if (gsi_ep_count && !direction)
2298 gsi_ep_count = NUM_GSI_OUT_EPS;
2299 /* IN GSI EPs */
2300 else if (gsi_ep_count && direction)
2301 gsi_ep_count = NUM_GSI_IN_EPS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002302
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002303 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002304 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002305
Felipe Balbi72246da2011-08-19 18:10:58 +03002306 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002307 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002308 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002309
2310 dep->dwc = dwc;
2311 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002312 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002313 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002314 dwc->eps[epnum] = dep;
2315
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002316 /* Reserve EPs at the end for GSI based on gsi_ep_count */
2317 if ((gsi_ep_index < gsi_ep_count) &&
2318 (i > (num - 1 - gsi_ep_count))) {
2319 gsi_ep_index++;
2320 /* For GSI EPs, name eps as "gsi-epin" or "gsi-epout" */
2321 snprintf(dep->name, sizeof(dep->name), "%s",
2322 (epnum & 1) ? "gsi-epin" : "gsi-epout");
2323 /* Set ep type as GSI */
2324 dep->endpoint.ep_type = EP_TYPE_GSI;
2325 } else {
2326 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
2327 epnum >> 1, (epnum & 1) ? "in" : "out");
2328 }
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002329
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002330 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002332 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002333
Felipe Balbi73815282015-01-27 13:48:14 -06002334 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002335
Felipe Balbi72246da2011-08-19 18:10:58 +03002336 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002337 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302338 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002339 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2340 if (!epnum)
2341 dwc->gadget.ep0 = &dep->endpoint;
2342 } else {
2343 int ret;
2344
Robert Baldygae117e742013-12-13 12:23:38 +01002345 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002346 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2348 list_add_tail(&dep->endpoint.ep_list,
2349 &dwc->gadget.ep_list);
2350
2351 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002352 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002353 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002354 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002355
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002356 if (epnum == 0 || epnum == 1) {
2357 dep->endpoint.caps.type_control = true;
2358 } else {
2359 dep->endpoint.caps.type_iso = true;
2360 dep->endpoint.caps.type_bulk = true;
2361 dep->endpoint.caps.type_int = true;
2362 }
2363
2364 dep->endpoint.caps.dir_in = !!direction;
2365 dep->endpoint.caps.dir_out = !direction;
2366
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002367 INIT_LIST_HEAD(&dep->pending_list);
2368 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002369 }
2370
2371 return 0;
2372}
2373
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002374static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2375{
2376 int ret;
2377
2378 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2379
2380 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2381 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002382 dwc3_trace(trace_dwc3_gadget,
2383 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002384 return ret;
2385 }
2386
2387 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2388 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002389 dwc3_trace(trace_dwc3_gadget,
2390 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002391 return ret;
2392 }
2393
2394 return 0;
2395}
2396
Felipe Balbi72246da2011-08-19 18:10:58 +03002397static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2398{
2399 struct dwc3_ep *dep;
2400 u8 epnum;
2401
2402 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2403 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002404 if (!dep)
2405 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302406 /*
2407 * Physical endpoints 0 and 1 are special; they form the
2408 * bi-directional USB endpoint 0.
2409 *
2410 * For those two physical endpoints, we don't allocate a TRB
2411 * pool nor do we add them the endpoints list. Due to that, we
2412 * shouldn't do these two operations otherwise we would end up
2413 * with all sorts of bugs when removing dwc3.ko.
2414 */
2415 if (epnum != 0 && epnum != 1) {
2416 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002417 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302418 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002419
2420 kfree(dep);
2421 }
2422}
2423
Felipe Balbi72246da2011-08-19 18:10:58 +03002424/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002425
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302426static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2427 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002428 const struct dwc3_event_depevt *event, int status,
2429 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302430{
2431 unsigned int count;
2432 unsigned int s_pkt = 0;
2433 unsigned int trb_status;
2434
Felipe Balbidc55c672016-08-12 13:20:32 +03002435 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002436
2437 if (req->trb == trb)
2438 dep->queued_requests--;
2439
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002440 trace_dwc3_complete_trb(dep, trb);
2441
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002442 /*
2443 * If we're in the middle of series of chained TRBs and we
2444 * receive a short transfer along the way, DWC3 will skip
2445 * through all TRBs including the last TRB in the chain (the
2446 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2447 * bit and SW has to do it manually.
2448 *
2449 * We're going to do that here to avoid problems of HW trying
2450 * to use bogus TRBs for transfers.
2451 */
2452 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2453 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2454
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302455 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002456 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002457
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302458 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002459 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302460
2461 if (dep->direction) {
2462 if (count) {
2463 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2464 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002465 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002466 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302467 dep->name);
2468 /*
2469 * If missed isoc occurred and there is
2470 * no request queued then issue END
2471 * TRANSFER, so that core generates
2472 * next xfernotready and we will issue
2473 * a fresh START TRANSFER.
2474 * If there are still queued request
2475 * then wait, do not issue either END
2476 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002477 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302478 * giveback.If any future queued request
2479 * is successfully transferred then we
2480 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002481 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302482 */
2483 dep->flags |= DWC3_EP_MISSED_ISOC;
Mayank Rana558baca2017-02-17 11:46:38 -08002484 dbg_event(dep->number, "MISSED ISOC", status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302485 } else {
2486 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2487 dep->name);
2488 status = -ECONNRESET;
2489 }
2490 } else {
2491 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2492 }
2493 } else {
2494 if (count && (event->status & DEPEVT_STATUS_SHORT))
2495 s_pkt = 1;
2496 }
2497
Felipe Balbi7c705df2016-08-10 12:35:30 +03002498 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302499 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002500
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302501 if ((event->status & DEPEVT_STATUS_IOC) &&
2502 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2503 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002504
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302505 return 0;
2506}
2507
Felipe Balbi72246da2011-08-19 18:10:58 +03002508static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2509 const struct dwc3_event_depevt *event, int status)
2510{
Felipe Balbi31162af2016-08-11 14:38:37 +03002511 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002512 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002513 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302514 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002515
Felipe Balbi31162af2016-08-11 14:38:37 +03002516 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002517 unsigned length;
2518 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002519 int chain;
2520
Felipe Balbi1f512112016-08-12 13:17:27 +03002521 length = req->request.length;
2522 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002523 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002524 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002525 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002526 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002527 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002528
Felipe Balbi1f512112016-08-12 13:17:27 +03002529 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002530 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002531
Felipe Balbi1f512112016-08-12 13:17:27 +03002532 req->sg = sg_next(s);
2533 req->num_pending_sgs--;
2534
Felipe Balbi31162af2016-08-11 14:38:37 +03002535 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2536 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002537 if (ret)
2538 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002539 }
2540 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002541 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002542 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002543 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002544 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002545
Felipe Balbic7de5732016-07-29 03:17:58 +03002546 /*
2547 * We assume here we will always receive the entire data block
2548 * which we should receive. Meaning, if we program RX to
2549 * receive 4K but we receive only 2K, we assume that's all we
2550 * should receive and we simply bounce the request back to the
2551 * gadget driver for further processing.
2552 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002553 actual = length - req->request.actual;
2554 req->request.actual = actual;
2555
2556 if (ret && chain && (actual < length) && req->num_pending_sgs)
2557 return __dwc3_gadget_kick_transfer(dep, 0);
2558
Ville Syrjäläd115d702015-08-31 19:48:28 +03002559 dwc3_gadget_giveback(dep, req, status);
2560
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002561 if (ret) {
2562 if ((event->status & DEPEVT_STATUS_IOC) &&
2563 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2564 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002566 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002567 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002568
Felipe Balbi4cb42212016-05-18 12:37:21 +03002569 /*
2570 * Our endpoint might get disabled by another thread during
2571 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2572 * early on so DWC3_EP_BUSY flag gets cleared
2573 */
2574 if (!dep->endpoint.desc)
2575 return 1;
2576
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302577 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002578 list_empty(&dep->started_list)) {
2579 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302580 /*
2581 * If there is no entry in request list then do
2582 * not issue END TRANSFER now. Just set PENDING
2583 * flag, so that END TRANSFER is issued when an
2584 * entry is added into request list.
2585 */
2586 dep->flags = DWC3_EP_PENDING_REQUEST;
2587 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002588 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302589 dep->flags = DWC3_EP_ENABLED;
2590 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302591 return 1;
2592 }
2593
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002594 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2595 return 0;
2596
Felipe Balbi72246da2011-08-19 18:10:58 +03002597 return 1;
2598}
2599
2600static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002601 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002602{
2603 unsigned status = 0;
2604 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002605 u32 is_xfer_complete;
2606
2607 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002608
2609 if (event->status & DEPEVT_STATUS_BUSERR)
2610 status = -ECONNRESET;
2611
Paul Zimmerman1d046792012-02-15 18:56:56 -08002612 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002613 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002614 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002615 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002616
2617 /*
2618 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2619 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2620 */
2621 if (dwc->revision < DWC3_REVISION_183A) {
2622 u32 reg;
2623 int i;
2624
2625 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002626 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002627
2628 if (!(dep->flags & DWC3_EP_ENABLED))
2629 continue;
2630
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002631 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002632 return;
2633 }
2634
2635 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2636 reg |= dwc->u1u2;
2637 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2638
2639 dwc->u1u2 = 0;
2640 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002641
Felipe Balbi4cb42212016-05-18 12:37:21 +03002642 /*
2643 * Our endpoint might get disabled by another thread during
2644 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2645 * early on so DWC3_EP_BUSY flag gets cleared
2646 */
2647 if (!dep->endpoint.desc)
2648 return;
2649
Felipe Balbie6e709b2015-09-28 15:16:56 -05002650 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002651 int ret;
2652
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002653 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002654 if (!ret || ret == -EBUSY)
2655 return;
2656 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002657}
2658
Felipe Balbi72246da2011-08-19 18:10:58 +03002659static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2660 const struct dwc3_event_depevt *event)
2661{
2662 struct dwc3_ep *dep;
2663 u8 epnum = event->endpoint_number;
2664
2665 dep = dwc->eps[epnum];
2666
Felipe Balbi3336abb2012-06-06 09:19:35 +03002667 if (!(dep->flags & DWC3_EP_ENABLED))
2668 return;
2669
Felipe Balbi72246da2011-08-19 18:10:58 +03002670 if (epnum == 0 || epnum == 1) {
2671 dwc3_ep0_interrupt(dwc, event);
2672 return;
2673 }
2674
Mayank Rana0c667b42017-02-09 11:56:51 -08002675 dep->dbg_ep_events.total++;
2676
Felipe Balbi72246da2011-08-19 18:10:58 +03002677 switch (event->endpoint_event) {
2678 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002679 dep->resource_index = 0;
Mayank Rana0c667b42017-02-09 11:56:51 -08002680 dep->dbg_ep_events.xfercomplete++;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002681
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002682 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002683 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002684 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002685 dep->name);
2686 return;
2687 }
2688
Jingoo Han029d97f2014-07-04 15:00:51 +09002689 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002690 break;
2691 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08002692 dep->dbg_ep_events.xferinprogress++;
Jingoo Han029d97f2014-07-04 15:00:51 +09002693 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002694 break;
2695 case DWC3_DEPEVT_XFERNOTREADY:
Mayank Rana0c667b42017-02-09 11:56:51 -08002696 dep->dbg_ep_events.xfernotready++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002697 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002698 dwc3_gadget_start_isoc(dwc, dep, event);
2699 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002700 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 int ret;
2702
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002703 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2704
Felipe Balbi73815282015-01-27 13:48:14 -06002705 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002706 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 : "Transfer Not Active");
2708
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002709 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 if (!ret || ret == -EBUSY)
2711 return;
2712
Felipe Balbiec5e7952015-11-16 16:04:13 -06002713 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002714 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002715 dep->name);
2716 }
2717
2718 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002719 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08002720 dep->dbg_ep_events.streamevent++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002721 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002722 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2723 dep->name);
2724 return;
2725 }
2726
2727 switch (event->status) {
2728 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002729 dwc3_trace(trace_dwc3_gadget,
2730 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002731 event->parameters);
2732
2733 break;
2734 case DEPEVT_STREAMEVT_NOTFOUND:
2735 /* FALLTHROUGH */
2736 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002737 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002738 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002739 }
2740 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002741 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002742 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Mayank Rana0c667b42017-02-09 11:56:51 -08002743 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002744 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002745 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002746 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08002747 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002748 break;
2749 }
2750}
2751
2752static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2753{
2754 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2755 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002756 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 dwc->gadget_driver->disconnect(&dwc->gadget);
2758 spin_lock(&dwc->lock);
2759 }
2760}
2761
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002762static void dwc3_suspend_gadget(struct dwc3 *dwc)
2763{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002764 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002765 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002766 dbg_event(0xFF, "SUSPEND", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002767 dwc->gadget_driver->suspend(&dwc->gadget);
2768 spin_lock(&dwc->lock);
2769 }
2770}
2771
2772static void dwc3_resume_gadget(struct dwc3 *dwc)
2773{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002774 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002775 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002776 dbg_event(0xFF, "RESUME", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002777 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002778 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002779 }
2780}
2781
2782static void dwc3_reset_gadget(struct dwc3 *dwc)
2783{
2784 if (!dwc->gadget_driver)
2785 return;
2786
2787 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2788 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002789 dbg_event(0xFF, "UDC RESET", 0);
Felipe Balbi8e744752014-11-06 14:27:53 +08002790 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002791 spin_lock(&dwc->lock);
2792 }
2793}
2794
Mayank Ranaa99689a2016-08-10 17:39:47 -07002795void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002796{
2797 struct dwc3_ep *dep;
2798 struct dwc3_gadget_ep_cmd_params params;
2799 u32 cmd;
2800 int ret;
2801
2802 dep = dwc->eps[epnum];
2803
Felipe Balbib4996a82012-06-06 12:04:13 +03002804 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302805 return;
2806
Pratyush Anand57911502012-07-06 15:19:10 +05302807 /*
2808 * NOTICE: We are violating what the Databook says about the
2809 * EndTransfer command. Ideally we would _always_ wait for the
2810 * EndTransfer Command Completion IRQ, but that's causing too
2811 * much trouble synchronizing between us and gadget driver.
2812 *
2813 * We have discussed this with the IP Provider and it was
2814 * suggested to giveback all requests here, but give HW some
2815 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002816 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302817 *
2818 * Note also that a similar handling was tested by Synopsys
2819 * (thanks a lot Paul) and nothing bad has come out of it.
2820 * In short, what we're doing is:
2821 *
2822 * - Issue EndTransfer WITH CMDIOC bit set
2823 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002824 *
2825 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2826 * supports a mode to work around the above limitation. The
2827 * software can poll the CMDACT bit in the DEPCMD register
2828 * after issuing a EndTransfer command. This mode is enabled
2829 * by writing GUCTL2[14]. This polling is already done in the
2830 * dwc3_send_gadget_ep_cmd() function so if the mode is
2831 * enabled, the EndTransfer command will have completed upon
2832 * returning from this function and we don't need to delay for
2833 * 100us.
2834 *
2835 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302836 */
2837
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302838 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002839 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2840 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002841 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302842 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002843 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302844 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002845 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002846 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002847
2848 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2849 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002850}
2851
2852static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2853{
2854 u32 epnum;
2855
2856 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2857 struct dwc3_ep *dep;
2858
2859 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002860 if (!dep)
2861 continue;
2862
Felipe Balbi72246da2011-08-19 18:10:58 +03002863 if (!(dep->flags & DWC3_EP_ENABLED))
2864 continue;
2865
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002866 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002867 }
2868}
2869
2870static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2871{
2872 u32 epnum;
2873
2874 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2875 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002876 int ret;
2877
2878 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002879 if (!dep)
2880 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002881
2882 if (!(dep->flags & DWC3_EP_STALL))
2883 continue;
2884
2885 dep->flags &= ~DWC3_EP_STALL;
2886
John Youn50c763f2016-05-31 17:49:56 -07002887 ret = dwc3_send_clear_stall_ep_cmd(dep);
Mayank Rana558baca2017-02-17 11:46:38 -08002888 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 WARN_ON_ONCE(ret);
2890 }
2891}
2892
2893static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2894{
Felipe Balbic4430a22012-05-24 10:30:01 +03002895 int reg;
2896
Mayank Rana558baca2017-02-17 11:46:38 -08002897 dbg_event(0xFF, "DISCONNECT INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002898 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2899 dwc->b_suspend = false;
2900 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2901
Felipe Balbi72246da2011-08-19 18:10:58 +03002902 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2903 reg &= ~DWC3_DCTL_INITU1ENA;
2904 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2905
2906 reg &= ~DWC3_DCTL_INITU2ENA;
2907 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002908
Felipe Balbi72246da2011-08-19 18:10:58 +03002909 dwc3_disconnect_gadget(dwc);
2910
2911 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002912 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002913 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002914 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002915
2916 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002917 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002918}
2919
Felipe Balbi72246da2011-08-19 18:10:58 +03002920static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2921{
2922 u32 reg;
2923
Felipe Balbifc8bb912016-05-16 13:14:48 +03002924 dwc->connected = true;
2925
Felipe Balbidf62df52011-10-14 15:11:49 +03002926 /*
2927 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2928 * would cause a missing Disconnect Event if there's a
2929 * pending Setup Packet in the FIFO.
2930 *
2931 * There's no suggested workaround on the official Bug
2932 * report, which states that "unless the driver/application
2933 * is doing any special handling of a disconnect event,
2934 * there is no functional issue".
2935 *
2936 * Unfortunately, it turns out that we _do_ some special
2937 * handling of a disconnect event, namely complete all
2938 * pending transfers, notify gadget driver of the
2939 * disconnection, and so on.
2940 *
2941 * Our suggested workaround is to follow the Disconnect
2942 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002943 * flag. Such flag gets set whenever we have a SETUP_PENDING
2944 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002945 * same endpoint.
2946 *
2947 * Refers to:
2948 *
2949 * STAR#9000466709: RTL: Device : Disconnect event not
2950 * generated if setup packet pending in FIFO
2951 */
2952 if (dwc->revision < DWC3_REVISION_188A) {
2953 if (dwc->setup_packet_pending)
2954 dwc3_gadget_disconnect_interrupt(dwc);
2955 }
2956
Mayank Rana558baca2017-02-17 11:46:38 -08002957 dbg_event(0xFF, "BUS RESET", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002958 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2959 dwc->b_suspend = false;
2960 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2961
2962 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07002963 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002964
Felipe Balbi8e744752014-11-06 14:27:53 +08002965 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002966
2967 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2968 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2969 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002970 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002971
2972 dwc3_stop_active_transfers(dwc);
2973 dwc3_clear_stall_all_ep(dwc);
2974
Hemant Kumarf0ce2552016-12-16 11:27:01 -08002975 /* bus reset issued due to missing status stage of a control transfer */
2976 dwc->resize_fifos = 0;
2977
Felipe Balbi72246da2011-08-19 18:10:58 +03002978 /* Reset device address to zero */
2979 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2980 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2981 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002982
2983 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2984 dwc->link_state = DWC3_LINK_STATE_U0;
2985 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002986}
2987
2988static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2989{
2990 u32 reg;
2991 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2992
2993 /*
2994 * We change the clock only at SS but I dunno why I would want to do
2995 * this. Maybe it becomes part of the power saving plan.
2996 */
2997
John Younee5cd412016-02-05 17:08:45 -08002998 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2999 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03003000 return;
3001
3002 /*
3003 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3004 * each time on Connect Done.
3005 */
3006 if (!usb30_clock)
3007 return;
3008
3009 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3010 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
3011 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
3012}
3013
Felipe Balbi72246da2011-08-19 18:10:58 +03003014static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3015{
Felipe Balbi72246da2011-08-19 18:10:58 +03003016 struct dwc3_ep *dep;
3017 int ret;
3018 u32 reg;
3019 u8 speed;
3020
Mayank Rana558baca2017-02-17 11:46:38 -08003021 dbg_event(0xFF, "CONNECT DONE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03003022 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3023 speed = reg & DWC3_DSTS_CONNECTSPD;
3024 dwc->speed = speed;
3025
3026 dwc3_update_ram_clk_sel(dwc, speed);
3027
3028 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003029 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003030 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3031 dwc->gadget.ep0->maxpacket = 512;
3032 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3033 break;
John Youn2da9ad72016-05-20 16:34:26 -07003034 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003035 /*
3036 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3037 * would cause a missing USB3 Reset event.
3038 *
3039 * In such situations, we should force a USB3 Reset
3040 * event by calling our dwc3_gadget_reset_interrupt()
3041 * routine.
3042 *
3043 * Refers to:
3044 *
3045 * STAR#9000483510: RTL: SS : USB3 reset event may
3046 * not be generated always when the link enters poll
3047 */
3048 if (dwc->revision < DWC3_REVISION_190A)
3049 dwc3_gadget_reset_interrupt(dwc);
3050
Felipe Balbi72246da2011-08-19 18:10:58 +03003051 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3052 dwc->gadget.ep0->maxpacket = 512;
3053 dwc->gadget.speed = USB_SPEED_SUPER;
3054 break;
John Youn2da9ad72016-05-20 16:34:26 -07003055 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003056 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3057 dwc->gadget.ep0->maxpacket = 64;
3058 dwc->gadget.speed = USB_SPEED_HIGH;
3059 break;
Roger Quadros5e3c2922017-01-03 14:32:09 +02003060 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003061 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3062 dwc->gadget.ep0->maxpacket = 64;
3063 dwc->gadget.speed = USB_SPEED_FULL;
3064 break;
John Youn2da9ad72016-05-20 16:34:26 -07003065 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003066 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3067 dwc->gadget.ep0->maxpacket = 8;
3068 dwc->gadget.speed = USB_SPEED_LOW;
3069 break;
3070 }
3071
Pratyush Anand2b758352013-01-14 15:59:31 +05303072 /* Enable USB2 LPM Capability */
3073
John Younee5cd412016-02-05 17:08:45 -08003074 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003075 (speed != DWC3_DSTS_SUPERSPEED) &&
3076 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303077 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3078 reg |= DWC3_DCFG_LPM_CAP;
3079 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3080
3081 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3082 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3083
Huang Rui460d0982014-10-31 11:11:18 +08003084 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303085
Huang Rui80caf7d2014-10-28 19:54:26 +08003086 /*
3087 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3088 * DCFG.LPMCap is set, core responses with an ACK and the
3089 * BESL value in the LPM token is less than or equal to LPM
3090 * NYET threshold.
3091 */
3092 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3093 && dwc->has_lpm_erratum,
3094 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
3095
3096 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3097 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3098
Pratyush Anand2b758352013-01-14 15:59:31 +05303099 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003100 } else {
3101 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3102 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3103 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303104 }
3105
Mayank Ranaa99689a2016-08-10 17:39:47 -07003106
3107 /*
3108 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
3109 * suspend in P3 state and generates IN_P3 power event irq.
3110 */
3111 dwc3_usb3_phy_suspend(dwc, true);
3112
Felipe Balbi72246da2011-08-19 18:10:58 +03003113 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003114 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3115 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003116 if (ret) {
3117 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3118 return;
3119 }
3120
3121 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003122 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3123 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003124 if (ret) {
3125 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3126 return;
3127 }
3128
Mayank Ranaa99689a2016-08-10 17:39:47 -07003129 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +03003130 /*
3131 * Configure PHY via GUSB3PIPECTLn if required.
3132 *
3133 * Update GTXFIFOSIZn
3134 *
3135 * In both cases reset values should be sufficient.
3136 */
3137}
3138
Mayank Ranaa99689a2016-08-10 17:39:47 -07003139static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003140{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003141 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003142
Mayank Ranaa99689a2016-08-10 17:39:47 -07003143 dev_dbg(dwc->dev, "%s\n", __func__);
3144
Mayank Rana558baca2017-02-17 11:46:38 -08003145 dbg_event(0xFF, "WAKEUP", remote_wakeup);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003146 /*
3147 * Identify if it is called from wakeup_interrupt() context for bus
3148 * resume or as part of remote wakeup. And based on that check for
3149 * U3 state. as we need to handle case of L1 resume i.e. where we
3150 * don't want to perform resume.
3151 */
3152 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3153 perform_resume = false;
3154
3155 /* Only perform resume from L2 or Early Suspend states */
3156 if (perform_resume) {
3157
3158 /*
3159 * In case of remote wake up dwc3_gadget_wakeup_work()
3160 * is doing pm_runtime_get_sync().
3161 */
3162 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3163 dwc->b_suspend = false;
3164 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3165
3166 /*
3167 * set state to U0 as function level resume is trying to queue
3168 * notification over USB interrupt endpoint which would fail
3169 * due to state is not being updated.
3170 */
3171 dwc->link_state = DWC3_LINK_STATE_U0;
3172 dwc3_resume_gadget(dwc);
3173 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003174 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003175
3176 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003177}
3178
3179static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3180 unsigned int evtinfo)
3181{
Felipe Balbifae2b902011-10-14 13:00:30 +03003182 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003183 unsigned int pwropt;
3184
3185 /*
3186 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3187 * Hibernation mode enabled which would show up when device detects
3188 * host-initiated U3 exit.
3189 *
3190 * In that case, device will generate a Link State Change Interrupt
3191 * from U3 to RESUME which is only necessary if Hibernation is
3192 * configured in.
3193 *
3194 * There are no functional changes due to such spurious event and we
3195 * just need to ignore it.
3196 *
3197 * Refers to:
3198 *
3199 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3200 * operational mode
3201 */
3202 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3203 if ((dwc->revision < DWC3_REVISION_250A) &&
3204 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3205 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3206 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06003207 dwc3_trace(trace_dwc3_gadget,
3208 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003209 return;
3210 }
3211 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003212
3213 /*
3214 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3215 * on the link partner, the USB session might do multiple entry/exit
3216 * of low power states before a transfer takes place.
3217 *
3218 * Due to this problem, we might experience lower throughput. The
3219 * suggested workaround is to disable DCTL[12:9] bits if we're
3220 * transitioning from U1/U2 to U0 and enable those bits again
3221 * after a transfer completes and there are no pending transfers
3222 * on any of the enabled endpoints.
3223 *
3224 * This is the first half of that workaround.
3225 *
3226 * Refers to:
3227 *
3228 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3229 * core send LGO_Ux entering U0
3230 */
3231 if (dwc->revision < DWC3_REVISION_183A) {
3232 if (next == DWC3_LINK_STATE_U0) {
3233 u32 u1u2;
3234 u32 reg;
3235
3236 switch (dwc->link_state) {
3237 case DWC3_LINK_STATE_U1:
3238 case DWC3_LINK_STATE_U2:
3239 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3240 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3241 | DWC3_DCTL_ACCEPTU2ENA
3242 | DWC3_DCTL_INITU1ENA
3243 | DWC3_DCTL_ACCEPTU1ENA);
3244
3245 if (!dwc->u1u2)
3246 dwc->u1u2 = reg & u1u2;
3247
3248 reg &= ~u1u2;
3249
3250 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3251 break;
3252 default:
3253 /* do nothing */
3254 break;
3255 }
3256 }
3257 }
3258
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003259 switch (next) {
3260 case DWC3_LINK_STATE_U1:
3261 if (dwc->speed == USB_SPEED_SUPER)
3262 dwc3_suspend_gadget(dwc);
3263 break;
3264 case DWC3_LINK_STATE_U2:
3265 case DWC3_LINK_STATE_U3:
3266 dwc3_suspend_gadget(dwc);
3267 break;
3268 case DWC3_LINK_STATE_RESUME:
3269 dwc3_resume_gadget(dwc);
3270 break;
3271 default:
3272 /* do nothing */
3273 break;
3274 }
3275
Mayank Ranaa99689a2016-08-10 17:39:47 -07003276 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003277 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003278 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003279}
3280
Baolin Wang72704f82016-05-16 16:43:53 +08003281static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003282 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003283{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003284 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003285
Mayank Rana558baca2017-02-17 11:46:38 -08003286 dbg_event(0xFF, "SUSPEND INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003287 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3288
3289 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3290 /*
3291 * When first connecting the cable, even before the initial
3292 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3293 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3294 * event. In such a case, ignore.
3295 * Ignore suspend event until device side usb is not into
3296 * CONFIGURED state.
3297 */
3298 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3299 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3300 __func__, dwc->gadget.state);
3301 return;
3302 }
3303
Baolin Wang72704f82016-05-16 16:43:53 +08003304 dwc3_suspend_gadget(dwc);
3305
Mayank Ranaa99689a2016-08-10 17:39:47 -07003306 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3307 dwc->b_suspend = true;
3308 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3309 }
3310
Baolin Wang72704f82016-05-16 16:43:53 +08003311 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003312 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003313}
3314
Felipe Balbie1dadd32014-02-25 14:47:54 -06003315static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3316 unsigned int evtinfo)
3317{
3318 unsigned int is_ss = evtinfo & BIT(4);
3319
3320 /**
3321 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3322 * have a known issue which can cause USB CV TD.9.23 to fail
3323 * randomly.
3324 *
3325 * Because of this issue, core could generate bogus hibernation
3326 * events which SW needs to ignore.
3327 *
3328 * Refers to:
3329 *
3330 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3331 * Device Fallback from SuperSpeed
3332 */
3333 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3334 return;
3335
3336 /* enter hibernation here */
3337}
3338
Felipe Balbi72246da2011-08-19 18:10:58 +03003339static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3340 const struct dwc3_event_devt *event)
3341{
3342 switch (event->type) {
3343 case DWC3_DEVICE_EVENT_DISCONNECT:
3344 dwc3_gadget_disconnect_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003345 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003346 break;
3347 case DWC3_DEVICE_EVENT_RESET:
3348 dwc3_gadget_reset_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003349 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003350 break;
3351 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3352 dwc3_gadget_conndone_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003353 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003354 break;
3355 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003356 dwc3_gadget_wakeup_interrupt(dwc, false);
Mayank Rana0c667b42017-02-09 11:56:51 -08003357 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003358 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003359 case DWC3_DEVICE_EVENT_HIBER_REQ:
3360 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3361 "unexpected hibernation event\n"))
3362 break;
3363
3364 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3365 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003366 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3367 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Mayank Rana0c667b42017-02-09 11:56:51 -08003368 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003369 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003370 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003371 if (dwc->revision < DWC3_REVISION_230A) {
3372 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003373 dwc->dbg_gadget_events.eopf++;
Baolin Wang72704f82016-05-16 16:43:53 +08003374 } else {
3375 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
Mayank Rana558baca2017-02-17 11:46:38 -08003376 dbg_event(0xFF, "GAD SUS", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003377 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003378 /*
3379 * Ignore suspend event until the gadget enters into
3380 * USB_STATE_CONFIGURED state.
3381 */
3382 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3383 dwc3_gadget_suspend_interrupt(dwc,
3384 event->event_info);
3385 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003386 break;
3387 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003388 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003389 dwc->dbg_gadget_events.sof++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003390 break;
3391 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003392 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Mayank Rana558baca2017-02-17 11:46:38 -08003393 dbg_event(0xFF, "ERROR", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003394 dwc->dbg_gadget_events.erratic_error++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003395 break;
3396 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003397 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08003398 dwc->dbg_gadget_events.cmdcmplt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003399 break;
3400 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003401 dwc3_trace(trace_dwc3_gadget, "Overflow");
Mayank Rana0c667b42017-02-09 11:56:51 -08003402 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003403 break;
3404 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003405 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Mayank Rana0c667b42017-02-09 11:56:51 -08003406 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003407 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003408
3409 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003410}
3411
3412static void dwc3_process_event_entry(struct dwc3 *dwc,
3413 const union dwc3_event *event)
3414{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003415 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003416 /* skip event processing in absence of vbus */
3417 if (!dwc->vbus_active) {
3418 dev_err(dwc->dev, "SKIP EVT:%x", event->raw);
3419 return;
3420 }
3421
3422 /* If run/stop is cleared don't process any more events */
3423 if (!dwc->pullups_connected) {
3424 dev_err(dwc->dev, "SKIP_EVT_PULLUP:%x", event->raw);
3425 return;
3426 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003427
Felipe Balbi72246da2011-08-19 18:10:58 +03003428 /* Endpoint IRQ, handle it and return early */
3429 if (event->type.is_devspec == 0) {
3430 /* depevt */
3431 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3432 }
3433
3434 switch (event->type.type) {
3435 case DWC3_EVENT_TYPE_DEV:
3436 dwc3_gadget_interrupt(dwc, &event->devt);
3437 break;
3438 /* REVISIT what to do with Carkit and I2C events ? */
3439 default:
3440 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3441 }
3442}
3443
Mayank Ranaa99689a2016-08-10 17:39:47 -07003444static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003445{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003446 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003447 irqreturn_t ret = IRQ_NONE;
3448 int left;
3449 u32 reg;
3450
Mayank Ranaa99689a2016-08-10 17:39:47 -07003451 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003452 left = evt->count;
3453
3454 if (!(evt->flags & DWC3_EVENT_PENDING))
3455 return IRQ_NONE;
3456
3457 while (left > 0) {
3458 union dwc3_event event;
3459
3460 event.raw = *(u32 *) (evt->buf + evt->lpos);
3461
3462 dwc3_process_event_entry(dwc, &event);
3463
Mayank Ranaa99689a2016-08-10 17:39:47 -07003464 if (dwc->err_evt_seen) {
3465 /*
3466 * if erratic error, skip remaining events
3467 * while controller undergoes reset
3468 */
3469 evt->lpos = (evt->lpos + left) %
3470 DWC3_EVENT_BUFFERS_SIZE;
3471 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
3472 if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT))
3473 dwc->err_evt_seen = 0;
3474 break;
3475 }
3476
Felipe Balbif42f2442013-06-12 21:25:08 +03003477 /*
3478 * FIXME we wrap around correctly to the next entry as
3479 * almost all entries are 4 bytes in size. There is one
3480 * entry which has 12 bytes which is a regular entry
3481 * followed by 8 bytes data. ATM I don't know how
3482 * things are organized if we get next to the a
3483 * boundary so I worry about that once we try to handle
3484 * that.
3485 */
3486 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3487 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003488 }
3489
Mayank Ranaa99689a2016-08-10 17:39:47 -07003490 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003491 evt->count = 0;
3492 evt->flags &= ~DWC3_EVENT_PENDING;
3493 ret = IRQ_HANDLED;
3494
3495 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003496 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003497 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003498 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003499
John Youn26cac202016-11-14 12:32:43 -08003500 if (dwc->imod_interval)
3501 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0),
3502 DWC3_GEVNTCOUNT_EHB);
3503
Felipe Balbif42f2442013-06-12 21:25:08 +03003504 return ret;
3505}
3506
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003507void dwc3_bh_work(struct work_struct *w)
3508{
3509 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3510
3511 pm_runtime_get_sync(dwc->dev);
3512 dwc3_thread_interrupt(dwc->irq, dwc);
3513 pm_runtime_put(dwc->dev);
3514}
3515
Mayank Ranaa99689a2016-08-10 17:39:47 -07003516static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3517{
3518 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003519 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003520 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003521 unsigned int temp_time;
3522 ktime_t start_time;
3523
3524 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003525
Felipe Balbie5f68b42015-10-12 13:25:44 -05003526 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003527 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3528
3529 ret = dwc3_process_event_buf(dwc);
3530
Felipe Balbie5f68b42015-10-12 13:25:44 -05003531 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003532
Mayank Ranaa99689a2016-08-10 17:39:47 -07003533 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3534 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3535 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3536
Felipe Balbib15a7622011-06-30 16:57:15 +03003537 return ret;
3538}
3539
Mayank Ranaa99689a2016-08-10 17:39:47 -07003540static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003541{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003542 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003543 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003544 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003545
Mayank Ranaa99689a2016-08-10 17:39:47 -07003546 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003547
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003548 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003549 count &= DWC3_GEVNTCOUNT_MASK;
3550 if (!count)
3551 return IRQ_NONE;
3552
Mayank Ranaa99689a2016-08-10 17:39:47 -07003553 if (count > evt->length) {
3554 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
Mayank Rana558baca2017-02-17 11:46:38 -08003555 dbg_event(0xFF, "HUGE_EVCNT", count);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003556 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3557 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3558 return IRQ_HANDLED;
3559 }
3560
Felipe Balbib15a7622011-06-30 16:57:15 +03003561 evt->count = count;
3562 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003563
Felipe Balbie8adfc32013-06-12 21:11:14 +03003564 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003565 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003566 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003567 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003568
John Youn551d2902016-11-15 13:08:59 +02003569 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3570
Felipe Balbib15a7622011-06-30 16:57:15 +03003571 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003572}
3573
Mayank Ranaa99689a2016-08-10 17:39:47 -07003574irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003575{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003576 struct dwc3 *dwc = _dwc;
3577 irqreturn_t ret = IRQ_NONE;
3578 irqreturn_t status;
3579 unsigned int temp_cnt = 0;
3580 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003581
Mayank Ranaa99689a2016-08-10 17:39:47 -07003582 start_time = ktime_get();
3583 dwc->irq_cnt++;
3584
3585 /* controller reset is still pending */
3586 if (dwc->err_evt_seen)
3587 return IRQ_HANDLED;
3588
3589 status = dwc3_check_event_buf(dwc);
3590 if (status == IRQ_WAKE_THREAD)
3591 ret = status;
3592
3593 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3594 dwc->irq_completion_time[dwc->irq_dbg_index] =
3595 ktime_us_delta(ktime_get(), start_time);
3596 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3597 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3598
Hemant Kumar78c7c282016-08-09 12:28:55 -07003599 if (ret == IRQ_WAKE_THREAD)
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003600 queue_work(dwc->dwc_wq, &dwc->bh_work);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003601
3602 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003603}
3604
3605/**
3606 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003607 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003608 *
3609 * Returns 0 on success otherwise negative errno.
3610 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003611int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003612{
Roger Quadros9522def2016-06-10 14:48:38 +03003613 int ret, irq;
3614 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3615
3616 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3617 if (irq == -EPROBE_DEFER)
3618 return irq;
3619
3620 if (irq <= 0) {
3621 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3622 if (irq == -EPROBE_DEFER)
3623 return irq;
3624
3625 if (irq <= 0) {
3626 irq = platform_get_irq(dwc3_pdev, 0);
3627 if (irq <= 0) {
3628 if (irq != -EPROBE_DEFER) {
3629 dev_err(dwc->dev,
3630 "missing peripheral IRQ\n");
3631 }
3632 if (!irq)
3633 irq = -EINVAL;
3634 return irq;
3635 }
3636 }
3637 }
3638
3639 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003640
Mayank Ranaa99689a2016-08-10 17:39:47 -07003641 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3642
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303643 dwc->ctrl_req = dma_alloc_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003644 &dwc->ctrl_req_addr, GFP_KERNEL);
3645 if (!dwc->ctrl_req) {
3646 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3647 ret = -ENOMEM;
3648 goto err0;
3649 }
3650
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303651 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3652 sizeof(*dwc->ep0_trb) * 2,
3653 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003654 if (!dwc->ep0_trb) {
3655 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3656 ret = -ENOMEM;
3657 goto err1;
3658 }
3659
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003660 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003661 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003662 ret = -ENOMEM;
3663 goto err2;
3664 }
3665
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303666 dwc->ep0_bounce = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003667 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3668 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003669 if (!dwc->ep0_bounce) {
3670 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3671 ret = -ENOMEM;
3672 goto err3;
3673 }
3674
Felipe Balbi04c03d12015-12-02 10:06:45 -06003675 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3676 if (!dwc->zlp_buf) {
3677 ret = -ENOMEM;
3678 goto err4;
3679 }
3680
Felipe Balbi72246da2011-08-19 18:10:58 +03003681 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003682 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003683 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003684 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003685 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003686
3687 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003688 * FIXME We might be setting max_speed to <SUPER, however versions
3689 * <2.20a of dwc3 have an issue with metastability (documented
3690 * elsewhere in this driver) which tells us we can't set max speed to
3691 * anything lower than SUPER.
3692 *
3693 * Because gadget.max_speed is only used by composite.c and function
3694 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3695 * to happen so we avoid sending SuperSpeed Capability descriptor
3696 * together with our BOS descriptor as that could confuse host into
3697 * thinking we can handle super speed.
3698 *
3699 * Note that, in fact, we won't even support GetBOS requests when speed
3700 * is less than super speed because we don't have means, yet, to tell
3701 * composite.c that we are USB 2.0 + LPM ECN.
3702 */
3703 if (dwc->revision < DWC3_REVISION_220A)
3704 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003705 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003706 dwc->revision);
3707
3708 dwc->gadget.max_speed = dwc->maximum_speed;
3709
3710 /*
David Cohena4b9d942013-12-09 15:55:38 -08003711 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3712 * on ep out.
3713 */
3714 dwc->gadget.quirk_ep_out_aligned_size = true;
3715
3716 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003717 * REVISIT: Here we should clear all pending IRQs to be
3718 * sure we're starting from a well known location.
3719 */
3720
3721 ret = dwc3_gadget_init_endpoints(dwc);
3722 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003723 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003724
Felipe Balbi72246da2011-08-19 18:10:58 +03003725 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3726 if (ret) {
3727 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003728 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003729 }
3730
Mayank Ranaa99689a2016-08-10 17:39:47 -07003731 if (!dwc->is_drd) {
3732 pm_runtime_no_callbacks(&dwc->gadget.dev);
3733 pm_runtime_set_active(&dwc->gadget.dev);
3734 pm_runtime_enable(&dwc->gadget.dev);
3735 pm_runtime_get(&dwc->gadget.dev);
3736 }
3737
Felipe Balbi72246da2011-08-19 18:10:58 +03003738 return 0;
3739
Felipe Balbi04c03d12015-12-02 10:06:45 -06003740err5:
3741 kfree(dwc->zlp_buf);
3742
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003743err4:
David Cohene1f80462013-09-11 17:42:47 -07003744 dwc3_gadget_free_endpoints(dwc);
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303745 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003746 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003747
Felipe Balbi72246da2011-08-19 18:10:58 +03003748err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003749 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003750
3751err2:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303752 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003753 dwc->ep0_trb, dwc->ep0_trb_addr);
3754
3755err1:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303756 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003757 dwc->ctrl_req, dwc->ctrl_req_addr);
3758
3759err0:
3760 return ret;
3761}
3762
Felipe Balbi7415f172012-04-30 14:56:33 +03003763/* -------------------------------------------------------------------------- */
3764
Felipe Balbi72246da2011-08-19 18:10:58 +03003765void dwc3_gadget_exit(struct dwc3 *dwc)
3766{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003767 if (dwc->is_drd) {
3768 pm_runtime_put(&dwc->gadget.dev);
3769 pm_runtime_disable(&dwc->gadget.dev);
3770 }
3771
Felipe Balbi72246da2011-08-19 18:10:58 +03003772 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003773
Felipe Balbi72246da2011-08-19 18:10:58 +03003774 dwc3_gadget_free_endpoints(dwc);
3775
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303776 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003777 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003778
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003779 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003780 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003781
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303782 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003783 dwc->ep0_trb, dwc->ep0_trb_addr);
3784
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303785 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003786 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003787}
Felipe Balbi7415f172012-04-30 14:56:33 +03003788
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003789int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003790{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003791 int ret;
3792
Roger Quadros9772b472016-04-12 11:33:29 +03003793 if (!dwc->gadget_driver)
3794 return 0;
3795
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003796 ret = dwc3_gadget_run_stop(dwc, false, false);
3797 if (ret < 0)
3798 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003799
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003800 dwc3_disconnect_gadget(dwc);
3801 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003802
3803 return 0;
3804}
3805
3806int dwc3_gadget_resume(struct dwc3 *dwc)
3807{
Felipe Balbi7415f172012-04-30 14:56:33 +03003808 int ret;
3809
Roger Quadros9772b472016-04-12 11:33:29 +03003810 if (!dwc->gadget_driver)
3811 return 0;
3812
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003813 ret = __dwc3_gadget_start(dwc);
3814 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003815 goto err0;
3816
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003817 ret = dwc3_gadget_run_stop(dwc, true, false);
3818 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003819 goto err1;
3820
Felipe Balbi7415f172012-04-30 14:56:33 +03003821 return 0;
3822
3823err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003824 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003825
3826err0:
3827 return ret;
3828}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003829
3830void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3831{
3832 if (dwc->pending_events) {
3833 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3834 dwc->pending_events = false;
3835 enable_irq(dwc->irq_gadget);
3836 }
3837}