blob: 27ed888a7bb33ee91f4c6e107812a95955db463d [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
Mayank Rana9ca186c2017-06-19 17:57:21 -0700167void 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
Mayank Rana9ca186c2017-06-19 17:57:21 -0700172void dwc3_ep_inc_deq(struct dwc3_ep *dep)
Felipe Balbief966b92016-04-05 13:09:51 +0300173{
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 */
Mayank Ranaac1200c2017-04-25 13:48:46 -0700198int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc, struct dwc3_ep *dep)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800199{
Mayank Ranaac1200c2017-04-25 13:48:46 -0700200 int fifo_size, mdwidth, max_packet = 1024;
201 int tmp, mult = 1;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800202
Mayank Ranaac1200c2017-04-25 13:48:46 -0700203 if (!dwc->needs_fifo_resize)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800204 return 0;
205
Mayank Ranaac1200c2017-04-25 13:48:46 -0700206 /* resize IN endpoints excepts ep0 */
207 if (!usb_endpoint_dir_in(dep->endpoint.desc) ||
208 dep->endpoint.ep_num == 0)
209 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800210
Mayank Ranaac1200c2017-04-25 13:48:46 -0700211 /* Don't resize already resized IN endpoint */
212 if (dep->fifo_depth) {
213 dev_dbg(dwc->dev, "%s fifo_depth:%d is already set\n",
214 dep->endpoint.name, dep->fifo_depth);
215 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800216 }
217
Mayank Ranaac1200c2017-04-25 13:48:46 -0700218 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
219 /* MDWIDTH is represented in bits, we need it in bytes */
220 mdwidth >>= 3;
221
222 if (dep->endpoint.ep_type == EP_TYPE_GSI || dep->endpoint.endless)
223 mult = 3;
224
225 if (((dep->endpoint.maxburst > 1) &&
226 usb_endpoint_xfer_bulk(dep->endpoint.desc))
227 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
228 mult = 3;
229
230 tmp = ((max_packet + mdwidth) * mult) + mdwidth;
231 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
232 dep->fifo_depth = fifo_size;
Ajay Agarwal8df9b9d2017-11-01 11:20:03 +0530233 fifo_size |= (dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)) & 0xffff0000)
234 + (dwc->last_fifo_depth << 16);
Mayank Ranaac1200c2017-04-25 13:48:46 -0700235 dwc->last_fifo_depth += (fifo_size & 0xffff);
236
237 dev_dbg(dwc->dev, "%s ep_num:%d last_fifo_depth:%04x fifo_depth:%d\n",
238 dep->endpoint.name, dep->endpoint.ep_num, dwc->last_fifo_depth,
239 dep->fifo_depth);
240
241 dbg_event(0xFF, "resize_fifo", dep->number);
242 dbg_event(0xFF, "fifo_depth", dep->fifo_depth);
243 /* Check fifo size allocation doesn't exceed available RAM size. */
244 if (dwc->tx_fifo_size &&
245 ((dwc->last_fifo_depth * mdwidth) >= dwc->tx_fifo_size)) {
246 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
247 (dwc->last_fifo_depth * mdwidth), dwc->tx_fifo_size,
248 dep->endpoint.name, fifo_size);
249 dwc->last_fifo_depth -= (fifo_size & 0xffff);
250 dep->fifo_depth = 0;
251 WARN_ON(1);
252 return -ENOMEM;
253 }
254
255 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->endpoint.ep_num),
256 fifo_size);
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800257 return 0;
258}
259
Felipe Balbi72246da2011-08-19 18:10:58 +0300260void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
261 int status)
262{
263 struct dwc3 *dwc = dep->dwc;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200264 unsigned int unmap_after_complete = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300265
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300266 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300267 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200268 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300269
270 if (req->request.status == -EINPROGRESS)
271 req->request.status = status;
272
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200273 /*
274 * NOTICE we don't want to unmap before calling ->complete() if we're
275 * dealing with a bounced ep0 request. If we unmap it here, we would end
276 * up overwritting the contents of req->buf and this could confuse the
277 * gadget driver.
278 */
279 if (dwc->ep0_bounced && dep->number <= 1) {
Pratyush Anand0416e492012-08-10 13:42:16 +0530280 dwc->ep0_bounced = false;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200281 unmap_after_complete = true;
282 } else {
Mayank Ranabd17b852017-08-25 10:38:30 -0700283 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200284 &req->request, req->direction);
285 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300286
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500287 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300288
289 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200290 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300291 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300292
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200293 if (unmap_after_complete)
Mayank Ranabd17b852017-08-25 10:38:30 -0700294 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200295 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300296}
297
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500298int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300299{
300 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300301 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300302 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300303 u32 reg;
304
305 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
306 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
307
308 do {
309 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
310 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300311 status = DWC3_DGCMD_STATUS(reg);
312 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300313 ret = -EINVAL;
314 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300315 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300316 } while (timeout--);
317
318 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300319 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300320 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300321 }
322
Felipe Balbi71f7e702016-05-23 14:16:19 +0300323 trace_dwc3_gadget_generic_cmd(cmd, param, status);
324
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300325 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300326}
327
Felipe Balbic36d8e92016-04-04 12:46:33 +0300328static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
329
Felipe Balbi2cd47182016-04-12 16:42:43 +0300330int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
331 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300332{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300333 struct dwc3 *dwc = dep->dwc;
Hemant Kumar43874172016-08-25 16:17:48 -0700334 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 u32 reg;
336
Felipe Balbi0933df12016-05-23 14:02:33 +0300337 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300338 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300339 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300340
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300341 /*
342 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
343 * we're issuing an endpoint command, we must check if
344 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
345 *
346 * We will also set SUSPHY bit to what it was before returning as stated
347 * by the same section on Synopsys databook.
348 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300349 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
350 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
351 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
352 susphy = true;
353 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
354 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
355 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300356 }
357
Felipe Balbia81d6b72016-09-22 12:25:28 +0300358 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300359 int needs_wakeup;
360
361 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
362 dwc->link_state == DWC3_LINK_STATE_U2 ||
363 dwc->link_state == DWC3_LINK_STATE_U3);
364
365 if (unlikely(needs_wakeup)) {
366 ret = __dwc3_gadget_wakeup(dwc);
367 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
368 ret);
369 }
370 }
371
Felipe Balbi2eb88012016-04-12 16:53:39 +0300372 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
373 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
374 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300375
Felipe Balbi2eb88012016-04-12 16:53:39 +0300376 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300378 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300379 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300380 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000381
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000382 switch (cmd_status) {
383 case 0:
384 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300385 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000386 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000387 ret = -EINVAL;
388 break;
389 case DEPEVT_TRANSFER_BUS_EXPIRY:
390 /*
391 * SW issues START TRANSFER command to
392 * isochronous ep with future frame interval. If
393 * future interval time has already passed when
394 * core receives the command, it will respond
395 * with an error status of 'Bus Expiry'.
396 *
397 * Instead of always returning -EINVAL, let's
398 * give a hint to the gadget driver that this is
399 * the case by returning -EAGAIN.
400 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000401 ret = -EAGAIN;
402 break;
403 default:
404 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
405 }
406
Felipe Balbic0ca3242016-04-04 09:11:51 +0300407 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300409 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300410
Felipe Balbif6bb2252016-05-23 13:53:34 +0300411 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300412 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700413 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
414 dev_err(dwc->dev, "%s command timeout for %s\n",
415 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Hemant Kumar43874172016-08-25 16:17:48 -0700416 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
417 dwc->ep_cmd_timeout_cnt++;
418 dwc3_notify_event(dwc,
419 DWC3_CONTROLLER_RESTART_USB_SESSION);
420 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300421 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300422 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300423
Felipe Balbi0933df12016-05-23 14:02:33 +0300424 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
425
Felipe Balbicf23b5b2016-10-21 13:07:09 +0300426 if (ret == 0) {
427 switch (DWC3_DEPCMD_CMD(cmd)) {
428 case DWC3_DEPCMD_STARTTRANSFER:
429 dep->flags |= DWC3_EP_TRANSFER_STARTED;
430 break;
431 case DWC3_DEPCMD_ENDTRANSFER:
432 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
433 break;
434 default:
435 /* nothing */
436 break;
437 }
438 }
439
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300440 if (unlikely(susphy)) {
441 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
442 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
443 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
444 }
445
Felipe Balbic0ca3242016-04-04 09:11:51 +0300446 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300447}
448
John Youn50c763f2016-05-31 17:49:56 -0700449static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
450{
451 struct dwc3 *dwc = dep->dwc;
452 struct dwc3_gadget_ep_cmd_params params;
453 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
454
455 /*
456 * As of core revision 2.60a the recommended programming model
457 * is to set the ClearPendIN bit when issuing a Clear Stall EP
458 * command for IN endpoints. This is to prevent an issue where
459 * some (non-compliant) hosts may not send ACK TPs for pending
460 * IN transfers due to a mishandled error condition. Synopsys
461 * STAR 9000614252.
462 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800463 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
464 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700465 cmd |= DWC3_DEPCMD_CLEARPENDIN;
466
467 memset(&params, 0, sizeof(params));
468
Felipe Balbi2cd47182016-04-12 16:42:43 +0300469 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700470}
471
Felipe Balbi72246da2011-08-19 18:10:58 +0300472static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
473{
474 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700475 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300476
477 if (dep->trb_pool)
478 return 0;
479
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530480 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700481 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300482 &dep->trb_pool_dma, GFP_KERNEL);
483 if (!dep->trb_pool) {
484 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
485 dep->name);
486 return -ENOMEM;
487 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700488 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300489
490 return 0;
491}
492
493static void dwc3_free_trb_pool(struct dwc3_ep *dep)
494{
495 struct dwc3 *dwc = dep->dwc;
496
Mayank Ranaa99689a2016-08-10 17:39:47 -0700497 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
498 if (dep->endpoint.ep_type == EP_TYPE_GSI)
499 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300500
Mayank Rana4dd882c2016-10-05 09:43:05 -0700501 /*
502 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
503 * with HWO bit set from previous composition when update transfer cmd
504 * is issued.
505 */
506 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
507 memset(&dep->trb_pool[0], 0,
508 sizeof(struct dwc3_trb) * dep->num_trbs);
Mayank Rana558baca2017-02-17 11:46:38 -0800509 dbg_event(dep->number, "Clr_TRB", 0);
Mayank Rana4dd882c2016-10-05 09:43:05 -0700510 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
511
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530512 dma_free_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700513 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
514 dep->trb_pool, dep->trb_pool_dma);
515 dep->trb_pool = NULL;
516 dep->trb_pool_dma = 0;
517 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300518}
519
John Younc4509602016-02-16 20:10:53 -0800520static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
521
522/**
523 * dwc3_gadget_start_config - Configure EP resources
524 * @dwc: pointer to our controller context structure
525 * @dep: endpoint that is being enabled
526 *
527 * The assignment of transfer resources cannot perfectly follow the
528 * data book due to the fact that the controller driver does not have
529 * all knowledge of the configuration in advance. It is given this
530 * information piecemeal by the composite gadget framework after every
531 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
532 * programming model in this scenario can cause errors. For two
533 * reasons:
534 *
535 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
536 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
537 * multiple interfaces.
538 *
539 * 2) The databook does not mention doing more DEPXFERCFG for new
540 * endpoint on alt setting (8.1.6).
541 *
542 * The following simplified method is used instead:
543 *
544 * All hardware endpoints can be assigned a transfer resource and this
545 * setting will stay persistent until either a core reset or
546 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
547 * do DEPXFERCFG for every hardware endpoint as well. We are
548 * guaranteed that there are as many transfer resources as endpoints.
549 *
550 * This function is called for each endpoint when it is being enabled
551 * but is triggered only when called for EP0-out, which always happens
552 * first, and which should only happen in one of the above conditions.
553 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300554static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
555{
556 struct dwc3_gadget_ep_cmd_params params;
557 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800558 int i;
559 int ret;
560
561 if (dep->number)
562 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
564 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800565 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
Felipe Balbi2cd47182016-04-12 16:42:43 +0300567 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800568 if (ret)
569 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
John Younc4509602016-02-16 20:10:53 -0800571 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
572 struct dwc3_ep *dep = dwc->eps[i];
573
574 if (!dep)
575 continue;
576
577 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
578 if (ret)
579 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300580 }
581
582 return 0;
583}
584
585static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200586 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300587 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300588 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300589{
590 struct dwc3_gadget_ep_cmd_params params;
591
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300592 if (dev_WARN_ONCE(dwc->dev, modify && restore,
593 "Can't modify and restore\n"))
594 return -EINVAL;
595
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 memset(&params, 0x00, sizeof(params));
597
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300598 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900599 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
600
601 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800602 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300603 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300604 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900605 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300607 if (modify) {
608 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
609 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600610 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
611 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300612 } else {
613 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600614 }
615
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300616 if (usb_endpoint_xfer_control(desc))
617 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300618
619 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
620 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200622 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300623 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
624 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300625 dep->stream_capable = true;
626 }
627
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500628 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300629 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300630
631 /*
632 * We are doing 1:1 mapping for endpoints, meaning
633 * Physical Endpoints 2 maps to Logical Endpoint 2 and
634 * so on. We consider the direction bit as part of the physical
635 * endpoint number. So USB endpoint 0x81 is 0x03.
636 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300637 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300638
639 /*
640 * We must use the lower 16 TX FIFOs even though
641 * HW might have more
642 */
643 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300644 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
646 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300647 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300648 dep->interval = 1 << (desc->bInterval - 1);
649 }
650
Felipe Balbi2cd47182016-04-12 16:42:43 +0300651 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300652}
653
654static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
655{
656 struct dwc3_gadget_ep_cmd_params params;
657
658 memset(&params, 0x00, sizeof(params));
659
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300660 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300661
Felipe Balbi2cd47182016-04-12 16:42:43 +0300662 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
663 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300664}
665
666/**
667 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
668 * @dep: endpoint to be initialized
669 * @desc: USB Endpoint Descriptor
670 *
671 * Caller should take care of locking
672 */
673static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200674 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300675 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300676 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300677{
678 struct dwc3 *dwc = dep->dwc;
679 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300680 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300681
Felipe Balbi73815282015-01-27 13:48:14 -0600682 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300683
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 if (!(dep->flags & DWC3_EP_ENABLED)) {
Mayank Ranaac1200c2017-04-25 13:48:46 -0700685 dep->endpoint.desc = desc;
686 dep->comp_desc = comp_desc;
687 dep->type = usb_endpoint_type(desc);
688 ret = dwc3_gadget_resize_tx_fifos(dwc, dep);
689 if (ret) {
690 dep->endpoint.desc = NULL;
691 dep->comp_desc = NULL;
692 dep->type = 0;
693 return ret;
694 }
695
Felipe Balbi72246da2011-08-19 18:10:58 +0300696 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700697 if (ret) {
698 dev_err(dwc->dev, "start_config() failed for %s\n",
699 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300700 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700701 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300702 }
703
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300704 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600705 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700706 if (ret) {
707 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700709 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300710
711 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200712 struct dwc3_trb *trb_st_hw;
713 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300714
Felipe Balbi72246da2011-08-19 18:10:58 +0300715 dep->flags |= DWC3_EP_ENABLED;
716
717 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
718 reg |= DWC3_DALEPENA_EP(dep->number);
719 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
720
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300721 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300722 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300723
John Youn0d257442016-05-19 17:26:08 -0700724 /* Initialize the TRB ring */
725 dep->trb_dequeue = 0;
726 dep->trb_enqueue = 0;
727 memset(dep->trb_pool, 0,
728 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
729
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300730 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300731 trb_st_hw = &dep->trb_pool[0];
732
Felipe Balbif6bafc62012-02-06 11:04:53 +0200733 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200734 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
735 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
736 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
737 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 }
739
740 return 0;
741}
742
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300744{
745 struct dwc3_request *req;
746
Felipe Balbi0e146022016-06-21 10:32:02 +0300747 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300748
Felipe Balbi0e146022016-06-21 10:32:02 +0300749 /* - giveback all requests to gadget driver */
750 while (!list_empty(&dep->started_list)) {
751 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200752
Felipe Balbi0e146022016-06-21 10:32:02 +0300753 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200754 }
755
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200756 while (!list_empty(&dep->pending_list)) {
757 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200759 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300761}
762
763/**
764 * __dwc3_gadget_ep_disable - Disables a HW endpoint
765 * @dep: the endpoint to disable
766 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200767 * This function also removes requests which are currently processed ny the
768 * hardware and those which are not yet scheduled.
769 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300770 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300771static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
772{
773 struct dwc3 *dwc = dep->dwc;
774 u32 reg;
775
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500776 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
777
Mayank Ranaa99689a2016-08-10 17:39:47 -0700778 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
779 dwc3_remove_requests(dwc, dep);
780 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
781 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300782
Felipe Balbi687ef982014-04-16 10:30:33 -0500783 /* make sure HW endpoint isn't stalled */
784 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500785 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500786
Felipe Balbi72246da2011-08-19 18:10:58 +0300787 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
788 reg &= ~DWC3_DALEPENA_EP(dep->number);
789 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
790
Felipe Balbi879631a2011-09-30 10:58:47 +0300791 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200792 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200793 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300795 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300796
Mayank Ranaa99689a2016-08-10 17:39:47 -0700797 /* Keep GSI ep names with "-gsi" suffix */
798 if (!strnstr(dep->name, "gsi", 10)) {
799 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
800 dep->number >> 1,
801 (dep->number & 1) ? "in" : "out");
802 }
803
Felipe Balbi72246da2011-08-19 18:10:58 +0300804 return 0;
805}
806
807/* -------------------------------------------------------------------------- */
808
809static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
810 const struct usb_endpoint_descriptor *desc)
811{
812 return -EINVAL;
813}
814
815static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
816{
817 return -EINVAL;
818}
819
820/* -------------------------------------------------------------------------- */
821
822static int dwc3_gadget_ep_enable(struct usb_ep *ep,
823 const struct usb_endpoint_descriptor *desc)
824{
825 struct dwc3_ep *dep;
826 struct dwc3 *dwc;
827 unsigned long flags;
828 int ret;
829
830 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
831 pr_debug("dwc3: invalid parameters\n");
832 return -EINVAL;
833 }
834
835 if (!desc->wMaxPacketSize) {
836 pr_debug("dwc3: missing wMaxPacketSize\n");
837 return -EINVAL;
838 }
839
840 dep = to_dwc3_ep(ep);
841 dwc = dep->dwc;
842
Felipe Balbi95ca9612015-12-10 13:08:20 -0600843 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
844 "%s is already enabled\n",
845 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300846 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300847
Felipe Balbi72246da2011-08-19 18:10:58 +0300848 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600849 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Mayank Rana558baca2017-02-17 11:46:38 -0800850 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 spin_unlock_irqrestore(&dwc->lock, flags);
852
853 return ret;
854}
855
856static int dwc3_gadget_ep_disable(struct usb_ep *ep)
857{
858 struct dwc3_ep *dep;
859 struct dwc3 *dwc;
860 unsigned long flags;
861 int ret;
862
863 if (!ep) {
864 pr_debug("dwc3: invalid parameters\n");
865 return -EINVAL;
866 }
867
868 dep = to_dwc3_ep(ep);
869 dwc = dep->dwc;
870
Felipe Balbi95ca9612015-12-10 13:08:20 -0600871 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
872 "%s is already disabled\n",
873 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300875
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -0800876 /* Keep GSI ep names with "-gsi" suffix */
877 if (!strnstr(dep->name, "gsi", 10)) {
878 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
879 dep->number >> 1,
880 (dep->number & 1) ? "in" : "out");
881 }
882
Felipe Balbi72246da2011-08-19 18:10:58 +0300883 spin_lock_irqsave(&dwc->lock, flags);
884 ret = __dwc3_gadget_ep_disable(dep);
Mayank Rana558baca2017-02-17 11:46:38 -0800885 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300886 spin_unlock_irqrestore(&dwc->lock, flags);
887
888 return ret;
889}
890
891static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
892 gfp_t gfp_flags)
893{
894 struct dwc3_request *req;
895 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300896
897 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900898 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300899 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
901 req->epnum = dep->number;
902 req->dep = dep;
Mayank Rana7877b272017-06-19 18:03:22 -0700903 req->request.dma = DMA_ERROR_CODE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300904
Felipe Balbi68d34c82016-05-30 13:34:58 +0300905 dep->allocated_requests++;
906
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500907 trace_dwc3_alloc_request(req);
908
Felipe Balbi72246da2011-08-19 18:10:58 +0300909 return &req->request;
910}
911
912static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
913 struct usb_request *request)
914{
915 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300916 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300917
Felipe Balbi68d34c82016-05-30 13:34:58 +0300918 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500919 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300920 kfree(req);
921}
922
Felipe Balbi2c78c022016-08-12 13:13:10 +0300923static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
924
Felipe Balbic71fc372011-11-22 11:37:34 +0200925/**
926 * dwc3_prepare_one_trb - setup one TRB from one request
927 * @dep: endpoint for which this request is prepared
928 * @req: dwc3_request pointer
929 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200930static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200931 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300932 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200933{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200934 struct dwc3_trb *trb;
Felipe Balbi3666b622016-09-22 11:01:01 +0300935 struct dwc3 *dwc = dep->dwc;
936 struct usb_gadget *gadget = &dwc->gadget;
937 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200938
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300939 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200940 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300941 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530942
Felipe Balbi4faf7552016-04-05 13:14:31 +0300943 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200944
Felipe Balbieeb720f2011-11-28 12:46:59 +0200945 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200946 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200947 req->trb = trb;
948 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300949 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300950 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200951 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200952
Felipe Balbief966b92016-04-05 13:09:51 +0300953 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530954
Felipe Balbif6bafc62012-02-06 11:04:53 +0200955 trb->size = DWC3_TRB_SIZE_LENGTH(length);
956 trb->bpl = lower_32_bits(dma);
957 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200958
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200959 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200960 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200961 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200962 break;
963
964 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi3666b622016-09-22 11:01:01 +0300965 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530966 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi3666b622016-09-22 11:01:01 +0300967
Manu Gautam480fd4f2017-07-19 17:07:10 +0530968 /*
969 * USB Specification 2.0 Section 5.9.2 states that: "If
970 * there is only a single transaction in the microframe,
971 * only a DATA0 data packet PID is used. If there are
972 * two transactions per microframe, DATA1 is used for
973 * the first transaction data packet and DATA0 is used
974 * for the second transaction data packet. If there are
975 * three transactions per microframe, DATA2 is used for
976 * the first transaction data packet, DATA1 is used for
977 * the second, and DATA0 is used for the third."
978 *
979 * IOW, we should satisfy the following cases:
980 *
981 * 1) length <= maxpacket
982 * - DATA0
983 *
984 * 2) maxpacket < length <= (2 * maxpacket)
985 * - DATA1, DATA0
986 *
987 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
988 * - DATA2, DATA1, DATA0
989 */
Felipe Balbi3666b622016-09-22 11:01:01 +0300990 if (speed == USB_SPEED_HIGH) {
991 struct usb_ep *ep = &dep->endpoint;
Manu Gautam480fd4f2017-07-19 17:07:10 +0530992 unsigned int mult = ep->mult - 1;
993 unsigned int maxp;
994
995 maxp = usb_endpoint_maxp(ep->desc) & 0x07ff;
996
997 if (length <= (2 * maxp))
998 mult--;
999
1000 if (length <= maxp)
1001 mult--;
1002
1003 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi3666b622016-09-22 11:01:01 +03001004 }
1005 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301006 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi3666b622016-09-22 11:01:01 +03001007 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001008
1009 /* always enable Interrupt on Missed ISOC */
1010 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001011 break;
1012
1013 case USB_ENDPOINT_XFER_BULK:
1014 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001015 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001016 break;
1017 default:
1018 /*
1019 * This is only possible with faulty memory because we
1020 * checked it already :)
1021 */
1022 BUG();
1023 }
1024
Felipe Balbica4d44e2016-03-10 13:53:27 +02001025 /* always enable Continue on Short Packet */
Felipe Balbi66f37a92016-10-05 14:26:23 +03001026 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi2e37cdd2016-10-06 17:10:39 +03001027 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001028
Felipe Balbi66f37a92016-10-05 14:26:23 +03001029 if (req->request.short_not_ok)
1030 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1031 }
1032
Felipe Balbi2c78c022016-08-12 13:13:10 +03001033 if ((!req->request.no_interrupt && !chain) ||
1034 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbi66f37a92016-10-05 14:26:23 +03001035 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001036
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301037 if (chain)
1038 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1039
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001040 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +02001041 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
1042
1043 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001044
1045 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001046}
1047
John Youn361572b2016-05-19 17:26:17 -07001048/**
1049 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
1050 * @dep: The endpoint with the TRB ring
1051 * @index: The index of the current TRB in the ring
1052 *
1053 * Returns the TRB prior to the one pointed to by the index. If the
1054 * index is 0, we will wrap backwards, skip the link TRB, and return
1055 * the one just before that.
1056 */
1057static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1058{
Felipe Balbi45438a02016-08-11 12:26:59 +03001059 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001060
Pratham Pratap2fcd96a2017-12-05 14:38:29 +05301061 if (!dep->trb_pool)
1062 return NULL;
1063
Felipe Balbi45438a02016-08-11 12:26:59 +03001064 if (!tmp)
1065 tmp = DWC3_TRB_NUM - 1;
1066
1067 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001068}
1069
Felipe Balbic4233572016-05-12 14:08:34 +03001070static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1071{
1072 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001073 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001074
1075 /*
1076 * If enqueue & dequeue are equal than it is either full or empty.
1077 *
1078 * One way to know for sure is if the TRB right before us has HWO bit
1079 * set or not. If it has, then we're definitely full and can't fit any
1080 * more transfers in our ring.
1081 */
1082 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001083 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1084 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
1085 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001086
1087 return DWC3_TRB_NUM - 1;
1088 }
1089
John Youn9d7aba72016-08-26 18:43:01 -07001090 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001091 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001092
John Youn9d7aba72016-08-26 18:43:01 -07001093 if (dep->trb_dequeue < dep->trb_enqueue)
1094 trbs_left--;
1095
John Youn32db3d92016-05-19 17:26:12 -07001096 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001097}
1098
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001099static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001100 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001101{
Felipe Balbi1f512112016-08-12 13:17:27 +03001102 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001103 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001104 unsigned int length;
1105 dma_addr_t dma;
1106 int i;
1107
Felipe Balbi1f512112016-08-12 13:17:27 +03001108 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001109 unsigned chain = true;
1110
1111 length = sg_dma_len(s);
1112 dma = sg_dma_address(s);
1113
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001114 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001115 chain = false;
1116
1117 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001118 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001119
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001120 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001121 break;
1122 }
1123}
1124
1125static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001126 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001127{
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001128 unsigned int length;
1129 dma_addr_t dma;
1130
1131 dma = req->request.dma;
1132 length = req->request.length;
1133
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001134 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001135 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001136}
1137
Felipe Balbi72246da2011-08-19 18:10:58 +03001138/*
1139 * dwc3_prepare_trbs - setup TRBs from requests
1140 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001141 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001142 * The function goes through the requests list and sets up TRBs for the
1143 * transfers. The function returns once there are no more TRBs available or
1144 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 */
Felipe Balbic4233572016-05-12 14:08:34 +03001146static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001147{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001148 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001149
1150 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1151
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001152 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001153 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001154
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001155 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001156 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001157 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001158 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001159 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001160
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001161 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001162 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001163 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001164}
1165
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001166static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001167{
1168 struct dwc3_gadget_ep_cmd_params params;
1169 struct dwc3_request *req;
1170 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001171 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001172 int ret;
1173 u32 cmd;
1174
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001175 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001176
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001177 dwc3_prepare_trbs(dep);
1178 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001179 if (!req) {
1180 dep->flags |= DWC3_EP_PENDING_REQUEST;
Mayank Rana558baca2017-02-17 11:46:38 -08001181 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001182 return 0;
1183 }
1184
1185 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001186
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001187 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301188 params.param0 = upper_32_bits(req->trb_dma);
1189 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001190 cmd = DWC3_DEPCMD_STARTTRANSFER |
1191 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301192 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001193 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1194 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301195 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001196
Felipe Balbi2cd47182016-04-12 16:42:43 +03001197 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001198 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 /*
1200 * FIXME we need to iterate over the list of requests
1201 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001202 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001203 */
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301204 usb_gadget_unmap_request_by_dev(dwc->sysdev,
1205 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001206 list_del(&req->list);
1207 return ret;
1208 }
1209
1210 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001211
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001212 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001213 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001214 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001215 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001216
Felipe Balbi72246da2011-08-19 18:10:58 +03001217 return 0;
1218}
1219
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001220static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1221{
1222 u32 reg;
1223
1224 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1225 return DWC3_DSTS_SOFFN(reg);
1226}
1227
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301228static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1229 struct dwc3_ep *dep, u32 cur_uf)
1230{
1231 u32 uf;
Mayank Rana558baca2017-02-17 11:46:38 -08001232 int ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301233
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001234 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001235 dwc3_trace(trace_dwc3_gadget,
1236 "ISOC ep %s run out for requests",
1237 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301238 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301239 return;
1240 }
1241
John Youn1819d0a2017-01-26 11:58:40 -08001242 /*
1243 * Schedule the first trb for one interval in the future or at
1244 * least 4 microframes.
1245 */
1246 uf = cur_uf + max_t(u32, 4, dep->interval);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301247
Mayank Rana558baca2017-02-17 11:46:38 -08001248 ret = __dwc3_gadget_kick_transfer(dep, uf);
1249 if (ret < 0)
1250 dbg_event(dep->number, "ISOC QUEUE", ret);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301251}
1252
1253static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1254 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1255{
1256 u32 cur_uf, mask;
1257
1258 mask = ~(dep->interval - 1);
1259 cur_uf = event->parameters & mask;
1260
1261 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1262}
1263
Felipe Balbi72246da2011-08-19 18:10:58 +03001264static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1265{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001266 struct dwc3 *dwc = dep->dwc;
1267 int ret;
1268
Felipe Balbibb423982015-11-16 15:31:21 -06001269 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001270 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001271 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001272 &req->request, dep->endpoint.name);
1273 return -ESHUTDOWN;
1274 }
1275
Felipe Balbi3272bad2017-05-17 15:57:45 +03001276 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001277 &req->request, req->dep->name)) {
Felipe Balbi3272bad2017-05-17 15:57:45 +03001278 dwc3_trace(trace_dwc3_gadget, "request %pK belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001279 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001280 return -EINVAL;
1281 }
1282
Manu Gautamb40ef612013-02-11 15:53:34 +05301283 if (req->request.status == -EINPROGRESS) {
1284 ret = -EBUSY;
1285 dev_err(dwc->dev, "%s: %p request already in queue",
1286 dep->name, req);
1287 return ret;
1288 }
Felipe Balbifc8bb912016-05-16 13:14:48 +03001289
Felipe Balbi72246da2011-08-19 18:10:58 +03001290 req->request.actual = 0;
1291 req->request.status = -EINPROGRESS;
1292 req->direction = dep->direction;
1293 req->epnum = dep->number;
1294
Felipe Balbife84f522015-09-01 09:01:38 -05001295 trace_dwc3_ep_queue(req);
1296
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301297 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1298 dep->direction);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001299 if (ret)
1300 return ret;
1301
Felipe Balbi1f512112016-08-12 13:17:27 +03001302 req->sg = req->request.sg;
1303 req->num_pending_sgs = req->request.num_mapped_sgs;
1304
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001305 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001306
Felipe Balbid889c232016-09-29 15:44:29 +03001307 /*
1308 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1309 * wait for a XferNotReady event so we will know what's the current
1310 * (micro-)frame number.
1311 *
1312 * Without this trick, we are very, very likely gonna get Bus Expiry
1313 * errors which will force us issue EndTransfer command.
1314 */
1315 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001316 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1317 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1318 dwc3_stop_active_transfer(dwc, dep->number, true);
1319 dep->flags = DWC3_EP_ENABLED;
1320 } else {
1321 u32 cur_uf;
1322
1323 cur_uf = __dwc3_gadget_get_frame(dwc);
1324 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzice7ab8972016-11-09 11:01:34 +01001325 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001326 }
Roger Quadros77d815d2017-04-21 15:58:08 +03001327 return 0;
Felipe Balbi08a36b52016-08-11 14:27:52 +03001328 }
Roger Quadros77d815d2017-04-21 15:58:08 +03001329
1330 if ((dep->flags & DWC3_EP_BUSY) &&
1331 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
1332 WARN_ON_ONCE(!dep->resource_index);
1333 ret = __dwc3_gadget_kick_transfer(dep,
1334 dep->resource_index);
1335 }
1336
1337 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001338 }
1339
Felipe Balbi594e1212016-08-24 14:38:10 +03001340 if (!dwc3_calc_trbs_left(dep))
1341 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001342
Felipe Balbi08a36b52016-08-11 14:27:52 +03001343 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001344 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001345 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001346 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001347 dep->name);
Roger Quadros77d815d2017-04-21 15:58:08 +03001348out:
Felipe Balbia8f32812015-09-16 10:40:07 -05001349 if (ret == -EBUSY)
1350 ret = 0;
1351
1352 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001353}
1354
Mayank Ranaa99689a2016-08-10 17:39:47 -07001355static int dwc3_gadget_wakeup(struct usb_gadget *g)
1356{
1357 struct dwc3 *dwc = gadget_to_dwc(g);
1358
1359 schedule_work(&dwc->wakeup_work);
1360 return 0;
1361}
1362
Mayank Ranaa99689a2016-08-10 17:39:47 -07001363static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1364{
1365 if (atomic_read(&dwc->in_lpm) ||
1366 dwc->link_state == DWC3_LINK_STATE_U3)
1367 return true;
1368 return false;
1369}
1370
Felipe Balbi04c03d12015-12-02 10:06:45 -06001371static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1372 struct usb_request *request)
1373{
1374 dwc3_gadget_ep_free_request(ep, request);
1375}
1376
1377static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1378{
1379 struct dwc3_request *req;
1380 struct usb_request *request;
1381 struct usb_ep *ep = &dep->endpoint;
1382
Felipe Balbi60cfb372016-05-24 13:45:17 +03001383 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001384 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1385 if (!request)
1386 return -ENOMEM;
1387
1388 request->length = 0;
1389 request->buf = dwc->zlp_buf;
1390 request->complete = __dwc3_gadget_ep_zlp_complete;
1391
1392 req = to_dwc3_request(request);
1393
1394 return __dwc3_gadget_ep_queue(dep, req);
1395}
1396
Felipe Balbi72246da2011-08-19 18:10:58 +03001397static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1398 gfp_t gfp_flags)
1399{
1400 struct dwc3_request *req = to_dwc3_request(request);
1401 struct dwc3_ep *dep = to_dwc3_ep(ep);
1402 struct dwc3 *dwc = dep->dwc;
1403
1404 unsigned long flags;
1405
1406 int ret;
1407
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001408 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001409 if (!dep->endpoint.desc) {
1410 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1411 request, ep->name);
1412 ret = -ESHUTDOWN;
1413 goto out;
1414 }
1415
1416 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1417 request, req->dep->name)) {
1418 ret = -EINVAL;
1419 goto out;
1420 }
1421
1422 if (dwc3_gadget_is_suspended(dwc)) {
1423 if (dwc->gadget.remote_wakeup)
1424 dwc3_gadget_wakeup(&dwc->gadget);
1425 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1426 goto out;
1427 }
1428
Manu Gautam3df6a322017-03-06 16:24:59 -08001429 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1430 "trying to queue unaligned request (%d) with %s\n",
1431 request->length, ep->name);
1432
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001434
1435 /*
1436 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1437 * setting request->zero, instead of doing magic, we will just queue an
1438 * extra usb_request ourselves so that it gets handled the same way as
1439 * any other request.
1440 */
John Yound92618982015-12-22 12:23:20 -08001441 if (ret == 0 && request->zero && request->length &&
1442 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001443 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1444
Mayank Ranaa99689a2016-08-10 17:39:47 -07001445out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 spin_unlock_irqrestore(&dwc->lock, flags);
1447
1448 return ret;
1449}
1450
1451static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1452 struct usb_request *request)
1453{
1454 struct dwc3_request *req = to_dwc3_request(request);
1455 struct dwc3_request *r = NULL;
1456
1457 struct dwc3_ep *dep = to_dwc3_ep(ep);
1458 struct dwc3 *dwc = dep->dwc;
1459
1460 unsigned long flags;
1461 int ret = 0;
1462
Mayank Ranaa99689a2016-08-10 17:39:47 -07001463 if (atomic_read(&dwc->in_lpm)) {
1464 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1465 return -EAGAIN;
1466 }
1467
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001468 trace_dwc3_ep_dequeue(req);
1469
Felipe Balbi72246da2011-08-19 18:10:58 +03001470 spin_lock_irqsave(&dwc->lock, flags);
1471
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001472 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001473 if (r == req)
1474 break;
1475 }
1476
1477 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001478 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001479 if (r == req)
1480 break;
1481 }
1482 if (r == req) {
1483 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001484 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbid1c93aa2017-03-08 13:56:37 -08001485
1486 /*
1487 * If request was already started, this means we had to
1488 * stop the transfer. With that we also need to ignore
1489 * all TRBs used by the request, however TRBs can only
1490 * be modified after completion of END_TRANSFER
1491 * command. So what we do here is that we wait for
1492 * END_TRANSFER completion and only after that, we jump
1493 * over TRBs by clearing HWO and incrementing dequeue
1494 * pointer.
1495 *
1496 * Note that we have 2 possible types of transfers here:
1497 *
1498 * i) Linear buffer request
1499 * ii) SG-list based request
1500 *
1501 * SG-list based requests will have r->num_pending_sgs
1502 * set to a valid number (> 0). Linear requests,
1503 * normally use a single TRB.
1504 *
1505 * All of these cases need to be taken into
1506 * consideration so we don't mess up our TRB ring
1507 * pointers.
1508 */
1509 if (!r->trb)
1510 goto out1;
1511
1512 if (r->num_pending_sgs) {
1513 struct dwc3_trb *trb;
1514 int i = 0;
1515
1516 for (i = 0; i < r->num_pending_sgs; i++) {
1517 trb = r->trb + i;
1518 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1519 dwc3_ep_inc_deq(dep);
1520 }
1521 } else {
1522 struct dwc3_trb *trb = r->trb;
1523
1524 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1525 dwc3_ep_inc_deq(dep);
1526 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301527 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001528 }
Felipe Balbi3272bad2017-05-17 15:57:45 +03001529 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 request, ep->name);
1531 ret = -EINVAL;
1532 goto out0;
1533 }
1534
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301535out1:
Mayank Rana558baca2017-02-17 11:46:38 -08001536 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001537 /* giveback the request */
Felipe Balbid1c93aa2017-03-08 13:56:37 -08001538 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1540
1541out0:
1542 spin_unlock_irqrestore(&dwc->lock, flags);
1543
1544 return ret;
1545}
1546
Felipe Balbi7a608552014-09-24 14:19:52 -05001547int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001548{
1549 struct dwc3_gadget_ep_cmd_params params;
1550 struct dwc3 *dwc = dep->dwc;
1551 int ret;
1552
Mayank Ranad223be42017-06-07 11:54:08 -07001553 if (!dep->endpoint.desc) {
1554 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1555 return -EINVAL;
1556 }
1557
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001558 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1559 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1560 return -EINVAL;
1561 }
1562
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 memset(&params, 0x00, sizeof(params));
Mayank Rana558baca2017-02-17 11:46:38 -08001564 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001565 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001566 struct dwc3_trb *trb;
1567
1568 unsigned transfer_in_flight;
1569 unsigned started;
1570
1571 if (dep->number > 1)
1572 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1573 else
1574 trb = &dwc->ep0_trb[dep->trb_enqueue];
1575
Pratham Pratap2fcd96a2017-12-05 14:38:29 +05301576 if (trb)
1577 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1578 else
1579 transfer_in_flight = false;
1580
Felipe Balbi69450c42016-05-30 13:37:02 +03001581 started = !list_empty(&dep->started_list);
1582
1583 if (!protocol && ((dep->direction && transfer_in_flight) ||
1584 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001585 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001586 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001587 dep->name);
1588 return -EAGAIN;
1589 }
1590
Felipe Balbi2cd47182016-04-12 16:42:43 +03001591 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1592 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001593 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001594 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 dep->name);
1596 else
1597 dep->flags |= DWC3_EP_STALL;
1598 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001599
John Youn50c763f2016-05-31 17:49:56 -07001600 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001601 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001602 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001603 dep->name);
1604 else
Alan Sterna535d812013-11-01 12:05:12 -04001605 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001606 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001607
Felipe Balbi72246da2011-08-19 18:10:58 +03001608 return ret;
1609}
1610
1611static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1612{
1613 struct dwc3_ep *dep = to_dwc3_ep(ep);
1614 struct dwc3 *dwc = dep->dwc;
1615
1616 unsigned long flags;
1617
1618 int ret;
1619
Mayank Ranaa99689a2016-08-10 17:39:47 -07001620 if (!ep->desc) {
1621 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1622 return -EINVAL;
1623 }
1624
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001626 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001627 spin_unlock_irqrestore(&dwc->lock, flags);
1628
1629 return ret;
1630}
1631
1632static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1633{
1634 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001635 struct dwc3 *dwc = dep->dwc;
1636 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001637 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001638
Paul Zimmerman249a4562012-02-24 17:32:16 -08001639 spin_lock_irqsave(&dwc->lock, flags);
Mayank Rana558baca2017-02-17 11:46:38 -08001640 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001641 dep->flags |= DWC3_EP_WEDGE;
1642
Pratyush Anand08f0d962012-06-25 22:40:43 +05301643 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001644 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301645 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001646 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001647 spin_unlock_irqrestore(&dwc->lock, flags);
1648
1649 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001650}
1651
1652/* -------------------------------------------------------------------------- */
1653
1654static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1655 .bLength = USB_DT_ENDPOINT_SIZE,
1656 .bDescriptorType = USB_DT_ENDPOINT,
1657 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1658};
1659
1660static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1661 .enable = dwc3_gadget_ep0_enable,
1662 .disable = dwc3_gadget_ep0_disable,
1663 .alloc_request = dwc3_gadget_ep_alloc_request,
1664 .free_request = dwc3_gadget_ep_free_request,
1665 .queue = dwc3_gadget_ep0_queue,
1666 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301667 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001668 .set_wedge = dwc3_gadget_ep_set_wedge,
1669};
1670
1671static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1672 .enable = dwc3_gadget_ep_enable,
1673 .disable = dwc3_gadget_ep_disable,
1674 .alloc_request = dwc3_gadget_ep_alloc_request,
1675 .free_request = dwc3_gadget_ep_free_request,
1676 .queue = dwc3_gadget_ep_queue,
1677 .dequeue = dwc3_gadget_ep_dequeue,
1678 .set_halt = dwc3_gadget_ep_set_halt,
1679 .set_wedge = dwc3_gadget_ep_set_wedge,
1680};
1681
1682/* -------------------------------------------------------------------------- */
1683
1684static int dwc3_gadget_get_frame(struct usb_gadget *g)
1685{
1686 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001687
Felipe Balbicf23b5b2016-10-21 13:07:09 +03001688 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001689}
1690
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001691static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001692{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001693 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001695 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001696 u32 reg;
1697
Felipe Balbi72246da2011-08-19 18:10:58 +03001698 u8 link_state;
1699 u8 speed;
1700
Felipe Balbi72246da2011-08-19 18:10:58 +03001701 /*
1702 * According to the Databook Remote wakeup request should
1703 * be issued only when the device is in early suspend state.
1704 *
1705 * We can check that via USB Link State bits in DSTS register.
1706 */
1707 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1708
1709 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001710 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1711 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001712 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001713 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 }
1715
1716 link_state = DWC3_DSTS_USBLNKST(reg);
1717
1718 switch (link_state) {
1719 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1720 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1721 break;
1722 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001723 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001724 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001725 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001726 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 }
1728
Felipe Balbi8598bde2012-01-02 18:55:57 +02001729 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1730 if (ret < 0) {
1731 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001732 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001733 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001734
Paul Zimmerman802fde92012-04-27 13:10:52 +03001735 /* Recent versions do this automatically */
1736 if (dwc->revision < DWC3_REVISION_194A) {
1737 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001738 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001739 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1740 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1741 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001742
Paul Zimmerman1d046792012-02-15 18:56:56 -08001743 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001744 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001745
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001746 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001747 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1748
1749 /* in HS, means ON */
1750 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1751 break;
1752 }
1753
1754 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1755 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001756 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001757 }
1758
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001759 return 0;
1760}
1761
Mayank Ranaa99689a2016-08-10 17:39:47 -07001762#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1763#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1764
1765static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001766{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001767 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001768 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001769 static int retry_count;
1770
1771 dwc = container_of(w, struct dwc3, wakeup_work);
1772
1773 ret = pm_runtime_get_sync(dwc->dev);
1774 if (ret) {
1775 /* pm_runtime_get_sync returns -EACCES error between
1776 * late_suspend and early_resume, wait for system resume to
1777 * finish and queue work again
1778 */
1779 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1780 if (ret == -EACCES) {
1781 pm_runtime_put_noidle(dwc->dev);
1782 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1783 retry_count = 0;
1784 pr_err("pm_runtime_get_sync timed out\n");
1785 return;
1786 }
1787 msleep(DWC3_PM_RESUME_DELAY);
1788 retry_count++;
1789 schedule_work(&dwc->wakeup_work);
1790 return;
1791 }
1792 }
1793 retry_count = 0;
Mayank Rana558baca2017-02-17 11:46:38 -08001794 dbg_event(0xFF, "Gdgwake gsyn",
1795 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001796
1797 ret = dwc3_gadget_wakeup_int(dwc);
1798
1799 if (ret)
1800 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1801 else
1802 pr_debug("Remote wakeup succeeded.\n");
1803
1804 pm_runtime_put_noidle(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001805 dbg_event(0xFF, "Gdgwake put",
1806 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001807}
1808
1809static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1810{
1811 bool link_recover_only = false;
1812
1813 u32 reg;
1814 int ret = 0;
1815 u8 link_state;
1816 unsigned long flags;
1817
1818 pr_debug("%s(): Entry\n", __func__);
1819 disable_irq(dwc->irq);
1820 spin_lock_irqsave(&dwc->lock, flags);
1821 /*
1822 * According to the Databook Remote wakeup request should
1823 * be issued only when the device is in early suspend state.
1824 *
1825 * We can check that via USB Link State bits in DSTS register.
1826 */
1827 link_state = dwc3_get_link_state(dwc);
1828
1829 switch (link_state) {
1830 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1831 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1832 break;
1833 case DWC3_LINK_STATE_U1:
1834 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1835 link_recover_only = true;
1836 break;
1837 }
1838 /* Intentional fallthrough */
1839 default:
1840 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1841 link_state);
1842 ret = -EINVAL;
1843 goto out;
1844 }
1845
1846 /* Enable LINK STATUS change event */
1847 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1848 reg |= DWC3_DEVTEN_ULSTCNGEN;
1849 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1850 /*
1851 * memory barrier is required to make sure that required events
1852 * with core is enabled before performing RECOVERY mechnism.
1853 */
1854 mb();
1855
1856 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1857 if (ret < 0) {
1858 dev_err(dwc->dev, "failed to put link in Recovery\n");
1859 /* Disable LINK STATUS change */
1860 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1861 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1862 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1863 /* Required to complete this operation before returning */
1864 mb();
1865 goto out;
1866 }
1867
1868 /* Recent versions do this automatically */
1869 if (dwc->revision < DWC3_REVISION_194A) {
1870 /* write zeroes to Link Change Request */
1871 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1872 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1873 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1874 }
1875
1876 spin_unlock_irqrestore(&dwc->lock, flags);
1877 enable_irq(dwc->irq);
1878
1879 /*
1880 * Have bigger value (16 sec) for timeout since some host PCs driving
1881 * resume for very long time (e.g. 8 sec)
1882 */
1883 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1884 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1885 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1886 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001887
1888 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001889 /* Disable link status change event */
1890 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1891 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1892 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1893 /*
1894 * Complete this write before we go ahead and perform resume
1895 * as we don't need link status change notificaiton anymore.
1896 */
1897 mb();
1898
1899 if (!ret) {
1900 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1901 dwc->link_state);
1902 ret = -EINVAL;
1903 spin_unlock_irqrestore(&dwc->lock, flags);
1904 goto out1;
1905 } else {
1906 ret = 0;
1907 /*
1908 * If USB is disconnected OR received RESET from host,
1909 * don't perform resume
1910 */
1911 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1912 dwc->gadget.state == USB_STATE_DEFAULT)
1913 link_recover_only = true;
1914 }
1915
1916 /*
1917 * According to DWC3 databook, the controller does not
1918 * trigger a wakeup event when remote-wakeup is used.
1919 * Hence, after remote-wakeup sequence is complete, and
1920 * the device is back at U0 state, it is required that
1921 * the resume sequence is initiated by SW.
1922 */
1923 if (!link_recover_only)
1924 dwc3_gadget_wakeup_interrupt(dwc, true);
1925
Felipe Balbi72246da2011-08-19 18:10:58 +03001926 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001927 pr_debug("%s: Exit\n", __func__);
1928 return ret;
1929
1930out:
1931 spin_unlock_irqrestore(&dwc->lock, flags);
1932 enable_irq(dwc->irq);
1933
1934out1:
1935 return ret;
1936}
1937
1938static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1939{
1940 int ret = 0;
1941 struct dwc3 *dwc = gadget_to_dwc(g);
1942
1943 if (!g || (g->speed != USB_SPEED_SUPER))
1944 return -ENOTSUPP;
1945
1946 if (dwc3_gadget_is_suspended(dwc)) {
1947 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1948 dwc3_gadget_wakeup(&dwc->gadget);
1949 return -EAGAIN;
1950 }
1951
1952 if (dwc->revision < DWC3_REVISION_220A) {
1953 ret = dwc3_send_gadget_generic_command(dwc,
1954 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1955 } else {
1956 ret = dwc3_send_gadget_generic_command(dwc,
1957 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1958 }
1959
1960 if (ret)
1961 pr_err("Function wakeup HW command failed.\n");
1962 else
1963 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001964
1965 return ret;
1966}
1967
1968static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1969 int is_selfpowered)
1970{
1971 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001972 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001973
Paul Zimmerman249a4562012-02-24 17:32:16 -08001974 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001975 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001976 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001977
1978 return 0;
1979}
1980
Mayank Ranaa99689a2016-08-10 17:39:47 -07001981#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001982static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001983{
1984 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001985 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001986
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001988 if (is_on) {
Mayank Rana558baca2017-02-17 11:46:38 -08001989 dbg_event(0xFF, "Pullup_enable", is_on);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001990 if (dwc->revision <= DWC3_REVISION_187A) {
1991 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1992 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1993 }
1994
1995 if (dwc->revision >= DWC3_REVISION_194A)
1996 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001997
1998 dwc3_event_buffers_setup(dwc);
1999 dwc3_gadget_restart(dwc);
2000 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002001 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002002
2003 if (dwc->has_hibernation)
2004 reg |= DWC3_DCTL_KEEP_CONNECT;
2005
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002006 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002007 } else {
Mayank Rana558baca2017-02-17 11:46:38 -08002008 dbg_event(0xFF, "Pullup_disable", is_on);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002009 dwc3_gadget_disable_irq(dwc);
2010 __dwc3_gadget_ep_disable(dwc->eps[0]);
2011 __dwc3_gadget_ep_disable(dwc->eps[1]);
2012
Felipe Balbi72246da2011-08-19 18:10:58 +03002013 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002014
2015 if (dwc->has_hibernation && !suspend)
2016 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2017
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002018 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002019 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002020
2021 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2022
2023 do {
2024 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002025 reg &= DWC3_DSTS_DEVCTRLHLT;
2026 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002027
Mayank Ranaa99689a2016-08-10 17:39:47 -07002028 if (!timeout) {
2029 dev_err(dwc->dev, "failed to %s controller\n",
2030 is_on ? "start" : "stop");
Mayank Rana558baca2017-02-17 11:46:38 -08002031 if (is_on)
2032 dbg_event(0xFF, "STARTTOUT", reg);
2033 else
2034 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03002035 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002036 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002037
Felipe Balbi73815282015-01-27 13:48:14 -06002038 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03002039 dwc->gadget_driver
2040 ? dwc->gadget_driver->function : "no-function",
2041 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05302042
2043 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002044}
2045
Mayank Ranaa99689a2016-08-10 17:39:47 -07002046static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2047{
2048 struct dwc3 *dwc = gadget_to_dwc(g);
2049
2050 dwc->vbus_draw = mA;
2051 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
Mayank Rana558baca2017-02-17 11:46:38 -08002052 dbg_event(0xFF, "currentDraw", mA);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002053 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT);
2054 return 0;
2055}
2056
Felipe Balbi72246da2011-08-19 18:10:58 +03002057static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2058{
2059 struct dwc3 *dwc = gadget_to_dwc(g);
2060 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302061 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002062
2063 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002064 dwc->softconnect = is_on;
2065 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
2066 /*
2067 * Need to wait for vbus_session(on) from otg driver or to
2068 * the udc_start.
2069 */
Mayank Rana558baca2017-02-17 11:46:38 -08002070 dbg_event(0xFF, "WaitPullup", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002071 return 0;
2072 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002073
Mayank Ranaa99689a2016-08-10 17:39:47 -07002074 pm_runtime_get_sync(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08002075 dbg_event(0xFF, "Pullup gsync",
2076 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03002077 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002078 /*
2079 * If we are here after bus suspend notify otg state machine to
2080 * increment pm usage count of dwc to prevent pm_runtime_suspend
2081 * during enumeration.
2082 */
2083 dwc->b_suspend = false;
2084 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002085 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002086 spin_unlock_irqrestore(&dwc->lock, flags);
2087
Mayank Ranaa99689a2016-08-10 17:39:47 -07002088 pm_runtime_mark_last_busy(dwc->dev);
2089 pm_runtime_put_autosuspend(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08002090 dbg_event(0xFF, "Pullup put",
2091 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302092 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002093}
2094
Mayank Ranaa99689a2016-08-10 17:39:47 -07002095void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002096{
2097 u32 reg;
2098
Mayank Rana558baca2017-02-17 11:46:38 -08002099 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002100 /* Enable all but Start and End of Frame IRQs */
2101 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2102 DWC3_DEVTEN_EVNTOVERFLOWEN |
2103 DWC3_DEVTEN_CMDCMPLTEN |
2104 DWC3_DEVTEN_ERRTICERREN |
2105 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002106 DWC3_DEVTEN_CONNECTDONEEN |
2107 DWC3_DEVTEN_USBRSTEN |
2108 DWC3_DEVTEN_DISCONNEVTEN);
2109
Mayank Ranaa99689a2016-08-10 17:39:47 -07002110 /*
2111 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
2112 * else enable USB Link change event (BIT:3) for older version
2113 */
2114 if (dwc->revision < DWC3_REVISION_230A)
2115 reg |= DWC3_DEVTEN_ULSTCNGEN;
2116 else
2117 reg |= DWC3_DEVTEN_SUSPEND;
2118
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002119 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2120}
2121
Mayank Ranaa99689a2016-08-10 17:39:47 -07002122void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002123{
Mayank Rana558baca2017-02-17 11:46:38 -08002124 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002125 /* mask all interrupts */
2126 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2127}
2128
Felipe Balbib15a7622011-06-30 16:57:15 +03002129static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002130static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002131
Felipe Balbi4e994722016-05-13 14:09:59 +03002132/**
2133 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
2134 * dwc: pointer to our context structure
2135 *
2136 * The following looks like complex but it's actually very simple. In order to
2137 * calculate the number of packets we can burst at once on OUT transfers, we're
2138 * gonna use RxFIFO size.
2139 *
2140 * To calculate RxFIFO size we need two numbers:
2141 * MDWIDTH = size, in bits, of the internal memory bus
2142 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2143 *
2144 * Given these two numbers, the formula is simple:
2145 *
2146 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2147 *
2148 * 24 bytes is for 3x SETUP packets
2149 * 16 bytes is a clock domain crossing tolerance
2150 *
2151 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2152 */
2153static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2154{
2155 u32 ram2_depth;
2156 u32 mdwidth;
2157 u32 nump;
2158 u32 reg;
2159
2160 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2161 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2162
2163 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2164 nump = min_t(u32, nump, 16);
2165
2166 /* update NumP */
2167 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2168 reg &= ~DWC3_DCFG_NUMP_MASK;
2169 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2170 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2171}
2172
Mayank Ranaa99689a2016-08-10 17:39:47 -07002173static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2174{
2175 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2176 unsigned long flags;
2177
2178 if (!dwc->is_drd)
2179 return -EPERM;
2180
2181 is_active = !!is_active;
2182
Mayank Rana558baca2017-02-17 11:46:38 -08002183 dbg_event(0xFF, "VbusSess", is_active);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002184 spin_lock_irqsave(&dwc->lock, flags);
2185
2186 /* Mark that the vbus was powered */
2187 dwc->vbus_active = is_active;
2188
2189 /*
2190 * Check if upper level usb_gadget_driver was already registered with
2191 * this udc controller driver (if dwc3_gadget_start was called)
2192 */
2193 if (dwc->gadget_driver && dwc->softconnect) {
2194 if (dwc->vbus_active) {
2195 /*
2196 * Both vbus was activated by otg and pullup was
2197 * signaled by the gadget driver.
2198 */
2199 dwc3_gadget_run_stop(dwc, 1, false);
2200 } else {
2201 dwc3_gadget_run_stop(dwc, 0, false);
2202 }
2203 }
2204
2205 /*
2206 * Clearing run/stop bit might occur before disconnect event is seen.
2207 * Make sure to let gadget driver know in that case.
2208 */
2209 if (!dwc->vbus_active) {
2210 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2211 dwc3_gadget_disconnect_interrupt(dwc);
2212 }
2213
2214 spin_unlock_irqrestore(&dwc->lock, flags);
2215 return 0;
2216}
2217
Felipe Balbid7be2952016-05-04 15:49:37 +03002218static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002219{
Felipe Balbi72246da2011-08-19 18:10:58 +03002220 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002221 int ret = 0;
2222 u32 reg;
2223
Mayank Rana558baca2017-02-17 11:46:38 -08002224 dbg_event(0xFF, "__Gadgetstart", 0);
John Youn26cac202016-11-14 12:32:43 -08002225
2226 /*
2227 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2228 * the core supports IMOD, disable it.
2229 */
2230 if (dwc->imod_interval) {
2231 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2232 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2233 } else if (dwc3_has_imod(dwc)) {
2234 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2235 }
2236
Felipe Balbi72246da2011-08-19 18:10:58 +03002237 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2238 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02002239
2240 /**
2241 * WORKAROUND: DWC3 revision < 2.20a have an issue
2242 * which would cause metastability state on Run/Stop
2243 * bit if we try to force the IP to USB2-only mode.
2244 *
2245 * Because of that, we cannot configure the IP to any
2246 * speed other than the SuperSpeed
2247 *
2248 * Refers to:
2249 *
2250 * STAR#9000525659: Clock Domain Crossing on DCTL in
2251 * USB 2.0 Mode
2252 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03002253 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02002254 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002255 } else {
2256 switch (dwc->maximum_speed) {
2257 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07002258 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002259 break;
2260 case USB_SPEED_FULL:
Roger Quadros5e3c2922017-01-03 14:32:09 +02002261 reg |= DWC3_DCFG_FULLSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002262 break;
2263 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07002264 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002265 break;
John Youn75808622016-02-05 17:09:13 -08002266 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07002267 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08002268 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002269 default:
John Youn77966eb2016-02-19 17:31:01 -08002270 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07002271 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08002272 /* fall through */
2273 case USB_SPEED_SUPER:
2274 reg |= DWC3_DCFG_SUPERSPEED;
2275 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002276 }
2277 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002278 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2279
Mayank Ranaa99689a2016-08-10 17:39:47 -07002280 /* Programs the number of outstanding pipelined transfer requests
2281 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002282 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07002283 if (dwc->revision >= DWC3_REVISION_270A) {
2284 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2285 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2286 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2287 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2288 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002289
Felipe Balbi4e994722016-05-13 14:09:59 +03002290 dwc3_gadget_setup_nump(dwc);
2291
Felipe Balbi72246da2011-08-19 18:10:58 +03002292 /* Start with SuperSpeed Default */
2293 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2294
Mayank Ranaa99689a2016-08-10 17:39:47 -07002295 dwc->delayed_status = false;
2296 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002298 dep->flags = 0;
2299 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002300 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2301 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002302 if (ret) {
2303 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002304 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002305 }
2306
2307 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002308 dep->flags = 0;
2309 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002310 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2311 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002312 if (ret) {
2313 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002314 __dwc3_gadget_ep_disable(dwc->eps[0]);
2315 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002316 }
2317
2318 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002319 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002320 dwc3_ep0_out_start(dwc);
2321
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002322 dwc3_gadget_enable_irq(dwc);
2323
Felipe Balbid7be2952016-05-04 15:49:37 +03002324 return ret;
2325}
2326
Mayank Ranaa99689a2016-08-10 17:39:47 -07002327/* Required gadget re-initialization before switching to gadget in OTG mode */
2328void dwc3_gadget_restart(struct dwc3 *dwc)
2329{
2330 __dwc3_gadget_start(dwc);
2331}
2332
Felipe Balbid7be2952016-05-04 15:49:37 +03002333static int dwc3_gadget_start(struct usb_gadget *g,
2334 struct usb_gadget_driver *driver)
2335{
2336 struct dwc3 *dwc = gadget_to_dwc(g);
2337 unsigned long flags;
2338 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002339
2340 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002341
Felipe Balbid7be2952016-05-04 15:49:37 +03002342 if (dwc->gadget_driver) {
2343 dev_err(dwc->dev, "%s is already bound to %s\n",
2344 dwc->gadget.name,
2345 dwc->gadget_driver->driver.name);
2346 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002347 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002348 }
2349
2350 dwc->gadget_driver = driver;
2351
Mayank Ranaa99689a2016-08-10 17:39:47 -07002352 /*
2353 * For DRD, this might get called by gadget driver during bootup
2354 * even though host mode might be active. Don't actually perform
2355 * device-specific initialization until device mode is activated.
2356 * In that case dwc3_gadget_restart() will handle it.
2357 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002358 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 return 0;
2360
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002361err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002362 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 return ret;
2364}
2365
Felipe Balbid7be2952016-05-04 15:49:37 +03002366static void __dwc3_gadget_stop(struct dwc3 *dwc)
2367{
Mayank Rana558baca2017-02-17 11:46:38 -08002368 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002369 dwc3_gadget_disable_irq(dwc);
2370 __dwc3_gadget_ep_disable(dwc->eps[0]);
2371 __dwc3_gadget_ep_disable(dwc->eps[1]);
2372}
2373
Felipe Balbi22835b82014-10-17 12:05:12 -05002374static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002375{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002376 struct dwc3 *dwc = gadget_to_dwc(g);
2377 unsigned long flags;
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002380 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002381 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002382
Mayank Ranaf0a45122017-11-13 16:56:40 -08002383 dbg_event(0xFF, "fwq_started", 0);
2384 flush_workqueue(dwc->dwc_wq);
2385 dbg_event(0xFF, "fwq_completed", 0);
2386
Felipe Balbi72246da2011-08-19 18:10:58 +03002387 return 0;
2388}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002389
Mayank Ranaa99689a2016-08-10 17:39:47 -07002390static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2391{
2392 struct dwc3 *dwc = gadget_to_dwc(g);
2393
Mayank Rana558baca2017-02-17 11:46:38 -08002394 dbg_event(0xFF, "RestartUSBSession", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002395 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION);
2396}
2397
Felipe Balbi72246da2011-08-19 18:10:58 +03002398static const struct usb_gadget_ops dwc3_gadget_ops = {
2399 .get_frame = dwc3_gadget_get_frame,
2400 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002401 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002402 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002403 .vbus_session = dwc3_gadget_vbus_session,
2404 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002405 .pullup = dwc3_gadget_pullup,
2406 .udc_start = dwc3_gadget_start,
2407 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002408 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002409};
2410
2411/* -------------------------------------------------------------------------- */
2412
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002413#define NUM_GSI_OUT_EPS 1
2414#define NUM_GSI_IN_EPS 2
2415
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002416static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2417 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002418{
2419 struct dwc3_ep *dep;
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002420 u8 i, gsi_ep_count, gsi_ep_index = 0;
2421
2422 gsi_ep_count = NUM_GSI_OUT_EPS + NUM_GSI_IN_EPS;
2423 /* OUT GSI EPs based on direction field */
2424 if (gsi_ep_count && !direction)
2425 gsi_ep_count = NUM_GSI_OUT_EPS;
2426 /* IN GSI EPs */
2427 else if (gsi_ep_count && direction)
2428 gsi_ep_count = NUM_GSI_IN_EPS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002429
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002430 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002431 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002432
Felipe Balbi72246da2011-08-19 18:10:58 +03002433 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002434 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002436
2437 dep->dwc = dwc;
2438 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002439 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002440 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002441 dwc->eps[epnum] = dep;
2442
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002443 /* Reserve EPs at the end for GSI based on gsi_ep_count */
2444 if ((gsi_ep_index < gsi_ep_count) &&
2445 (i > (num - 1 - gsi_ep_count))) {
2446 gsi_ep_index++;
2447 /* For GSI EPs, name eps as "gsi-epin" or "gsi-epout" */
2448 snprintf(dep->name, sizeof(dep->name), "%s",
2449 (epnum & 1) ? "gsi-epin" : "gsi-epout");
2450 /* Set ep type as GSI */
2451 dep->endpoint.ep_type = EP_TYPE_GSI;
2452 } else {
2453 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
2454 epnum >> 1, (epnum & 1) ? "in" : "out");
2455 }
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002456
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002457 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002458 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002459 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002460
Felipe Balbi73815282015-01-27 13:48:14 -06002461 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002462
Felipe Balbi72246da2011-08-19 18:10:58 +03002463 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002464 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302465 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002466 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2467 if (!epnum)
2468 dwc->gadget.ep0 = &dep->endpoint;
2469 } else {
2470 int ret;
2471
Robert Baldygae117e742013-12-13 12:23:38 +01002472 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002473 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002474 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2475 list_add_tail(&dep->endpoint.ep_list,
2476 &dwc->gadget.ep_list);
2477
2478 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002479 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002481 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002482
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002483 if (epnum == 0 || epnum == 1) {
2484 dep->endpoint.caps.type_control = true;
2485 } else {
2486 dep->endpoint.caps.type_iso = true;
2487 dep->endpoint.caps.type_bulk = true;
2488 dep->endpoint.caps.type_int = true;
2489 }
2490
2491 dep->endpoint.caps.dir_in = !!direction;
2492 dep->endpoint.caps.dir_out = !direction;
2493
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002494 INIT_LIST_HEAD(&dep->pending_list);
2495 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002496 }
2497
2498 return 0;
2499}
2500
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002501static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2502{
2503 int ret;
2504
2505 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2506
2507 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2508 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002509 dwc3_trace(trace_dwc3_gadget,
2510 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002511 return ret;
2512 }
2513
2514 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2515 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002516 dwc3_trace(trace_dwc3_gadget,
2517 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002518 return ret;
2519 }
2520
2521 return 0;
2522}
2523
Felipe Balbi72246da2011-08-19 18:10:58 +03002524static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2525{
2526 struct dwc3_ep *dep;
2527 u8 epnum;
2528
2529 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2530 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002531 if (!dep)
2532 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302533 /*
2534 * Physical endpoints 0 and 1 are special; they form the
2535 * bi-directional USB endpoint 0.
2536 *
2537 * For those two physical endpoints, we don't allocate a TRB
2538 * pool nor do we add them the endpoints list. Due to that, we
2539 * shouldn't do these two operations otherwise we would end up
2540 * with all sorts of bugs when removing dwc3.ko.
2541 */
2542 if (epnum != 0 && epnum != 1) {
2543 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002544 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302545 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002546
2547 kfree(dep);
2548 }
2549}
2550
Felipe Balbi72246da2011-08-19 18:10:58 +03002551/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002552
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302553static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2554 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002555 const struct dwc3_event_depevt *event, int status,
2556 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302557{
2558 unsigned int count;
2559 unsigned int s_pkt = 0;
2560 unsigned int trb_status;
2561
Felipe Balbidc55c672016-08-12 13:20:32 +03002562 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002563
2564 if (req->trb == trb)
2565 dep->queued_requests--;
2566
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002567 trace_dwc3_complete_trb(dep, trb);
2568
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002569 /*
2570 * If we're in the middle of series of chained TRBs and we
2571 * receive a short transfer along the way, DWC3 will skip
2572 * through all TRBs including the last TRB in the chain (the
2573 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2574 * bit and SW has to do it manually.
2575 *
2576 * We're going to do that here to avoid problems of HW trying
2577 * to use bogus TRBs for transfers.
2578 */
2579 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2580 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2581
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302582 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002583 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002584
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302585 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002586 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302587
2588 if (dep->direction) {
2589 if (count) {
2590 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2591 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002592 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002593 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302594 dep->name);
2595 /*
2596 * If missed isoc occurred and there is
2597 * no request queued then issue END
2598 * TRANSFER, so that core generates
2599 * next xfernotready and we will issue
2600 * a fresh START TRANSFER.
2601 * If there are still queued request
2602 * then wait, do not issue either END
2603 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002604 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302605 * giveback.If any future queued request
2606 * is successfully transferred then we
2607 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002608 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302609 */
2610 dep->flags |= DWC3_EP_MISSED_ISOC;
Mayank Rana558baca2017-02-17 11:46:38 -08002611 dbg_event(dep->number, "MISSED ISOC", status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302612 } else {
2613 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2614 dep->name);
2615 status = -ECONNRESET;
2616 }
2617 } else {
2618 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2619 }
2620 } else {
2621 if (count && (event->status & DEPEVT_STATUS_SHORT))
2622 s_pkt = 1;
2623 }
2624
Felipe Balbi7c705df2016-08-10 12:35:30 +03002625 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302626 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002627
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302628 if ((event->status & DEPEVT_STATUS_IOC) &&
2629 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2630 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002631
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302632 return 0;
2633}
2634
Felipe Balbi72246da2011-08-19 18:10:58 +03002635static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2636 const struct dwc3_event_depevt *event, int status)
2637{
Felipe Balbi31162af2016-08-11 14:38:37 +03002638 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002639 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002640 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302641 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002642
Felipe Balbi31162af2016-08-11 14:38:37 +03002643 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002644 unsigned length;
2645 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002646 int chain;
2647
Felipe Balbi1f512112016-08-12 13:17:27 +03002648 length = req->request.length;
2649 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002650 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002651 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002652 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002653 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002654 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002655
Felipe Balbi1f512112016-08-12 13:17:27 +03002656 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002657 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002658
Felipe Balbi1f512112016-08-12 13:17:27 +03002659 req->sg = sg_next(s);
2660 req->num_pending_sgs--;
2661
Felipe Balbi31162af2016-08-11 14:38:37 +03002662 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2663 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002664 if (ret)
2665 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002666 }
2667 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002668 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002669 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002670 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002671 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002672
Felipe Balbic7de5732016-07-29 03:17:58 +03002673 /*
2674 * We assume here we will always receive the entire data block
2675 * which we should receive. Meaning, if we program RX to
2676 * receive 4K but we receive only 2K, we assume that's all we
2677 * should receive and we simply bounce the request back to the
2678 * gadget driver for further processing.
2679 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002680 actual = length - req->request.actual;
2681 req->request.actual = actual;
2682
2683 if (ret && chain && (actual < length) && req->num_pending_sgs)
2684 return __dwc3_gadget_kick_transfer(dep, 0);
2685
Ville Syrjäläd115d702015-08-31 19:48:28 +03002686 dwc3_gadget_giveback(dep, req, status);
2687
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002688 if (ret) {
2689 if ((event->status & DEPEVT_STATUS_IOC) &&
2690 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2691 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002692 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002693 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002694 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002695
Felipe Balbi4cb42212016-05-18 12:37:21 +03002696 /*
2697 * Our endpoint might get disabled by another thread during
2698 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2699 * early on so DWC3_EP_BUSY flag gets cleared
2700 */
2701 if (!dep->endpoint.desc)
2702 return 1;
2703
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302704 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002705 list_empty(&dep->started_list)) {
2706 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302707 /*
2708 * If there is no entry in request list then do
2709 * not issue END TRANSFER now. Just set PENDING
2710 * flag, so that END TRANSFER is issued when an
2711 * entry is added into request list.
2712 */
2713 dep->flags = DWC3_EP_PENDING_REQUEST;
2714 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002715 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302716 dep->flags = DWC3_EP_ENABLED;
2717 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302718 return 1;
2719 }
2720
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002721 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2722 return 0;
2723
Felipe Balbi72246da2011-08-19 18:10:58 +03002724 return 1;
2725}
2726
2727static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002728 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002729{
2730 unsigned status = 0;
2731 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002732 u32 is_xfer_complete;
2733
2734 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002735
2736 if (event->status & DEPEVT_STATUS_BUSERR)
2737 status = -ECONNRESET;
2738
Paul Zimmerman1d046792012-02-15 18:56:56 -08002739 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002740 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002741 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002742 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002743
2744 /*
2745 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2746 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2747 */
2748 if (dwc->revision < DWC3_REVISION_183A) {
2749 u32 reg;
2750 int i;
2751
2752 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002753 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002754
2755 if (!(dep->flags & DWC3_EP_ENABLED))
2756 continue;
2757
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002758 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002759 return;
2760 }
2761
2762 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2763 reg |= dwc->u1u2;
2764 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2765
2766 dwc->u1u2 = 0;
2767 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002768
Felipe Balbi4cb42212016-05-18 12:37:21 +03002769 /*
2770 * Our endpoint might get disabled by another thread during
2771 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2772 * early on so DWC3_EP_BUSY flag gets cleared
2773 */
2774 if (!dep->endpoint.desc)
2775 return;
2776
Felipe Balbie6e709b2015-09-28 15:16:56 -05002777 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002778 int ret;
2779
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002780 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002781 if (!ret || ret == -EBUSY)
2782 return;
2783 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002784}
2785
Felipe Balbi72246da2011-08-19 18:10:58 +03002786static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2787 const struct dwc3_event_depevt *event)
2788{
2789 struct dwc3_ep *dep;
2790 u8 epnum = event->endpoint_number;
2791
2792 dep = dwc->eps[epnum];
2793
Felipe Balbi3336abb2012-06-06 09:19:35 +03002794 if (!(dep->flags & DWC3_EP_ENABLED))
2795 return;
2796
Felipe Balbi72246da2011-08-19 18:10:58 +03002797 if (epnum == 0 || epnum == 1) {
2798 dwc3_ep0_interrupt(dwc, event);
2799 return;
2800 }
2801
Mayank Rana0c667b42017-02-09 11:56:51 -08002802 dep->dbg_ep_events.total++;
2803
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 switch (event->endpoint_event) {
2805 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002806 dep->resource_index = 0;
Mayank Rana0c667b42017-02-09 11:56:51 -08002807 dep->dbg_ep_events.xfercomplete++;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002808
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002809 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002810 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002811 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002812 dep->name);
2813 return;
2814 }
2815
Jingoo Han029d97f2014-07-04 15:00:51 +09002816 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002817 break;
2818 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08002819 dep->dbg_ep_events.xferinprogress++;
Jingoo Han029d97f2014-07-04 15:00:51 +09002820 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002821 break;
2822 case DWC3_DEPEVT_XFERNOTREADY:
Mayank Rana0c667b42017-02-09 11:56:51 -08002823 dep->dbg_ep_events.xfernotready++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002824 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002825 dwc3_gadget_start_isoc(dwc, dep, event);
2826 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002827 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002828 int ret;
2829
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002830 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2831
Felipe Balbi73815282015-01-27 13:48:14 -06002832 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002833 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002834 : "Transfer Not Active");
2835
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002836 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002837 if (!ret || ret == -EBUSY)
2838 return;
2839
Felipe Balbiec5e7952015-11-16 16:04:13 -06002840 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002841 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002842 dep->name);
2843 }
2844
2845 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002846 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08002847 dep->dbg_ep_events.streamevent++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002848 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002849 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2850 dep->name);
2851 return;
2852 }
2853
2854 switch (event->status) {
2855 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002856 dwc3_trace(trace_dwc3_gadget,
2857 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002858 event->parameters);
2859
2860 break;
2861 case DEPEVT_STREAMEVT_NOTFOUND:
2862 /* FALLTHROUGH */
2863 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002864 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002865 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002866 }
2867 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002868 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002869 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Mayank Rana0c667b42017-02-09 11:56:51 -08002870 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002871 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002872 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002873 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08002874 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002875 break;
2876 }
2877}
2878
2879static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2880{
Mayank Rana8f847102017-11-15 09:57:15 -08002881 struct usb_gadget_driver *gadget_driver;
2882
Felipe Balbi72246da2011-08-19 18:10:58 +03002883 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
Mayank Rana8f847102017-11-15 09:57:15 -08002884 gadget_driver = dwc->gadget_driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002885 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002886 dbg_event(0xFF, "DISCONNECT", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002887 gadget_driver->disconnect(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002888 spin_lock(&dwc->lock);
2889 }
2890}
2891
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002892static void dwc3_suspend_gadget(struct dwc3 *dwc)
2893{
Mayank Rana8f847102017-11-15 09:57:15 -08002894 struct usb_gadget_driver *gadget_driver;
2895
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002896 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Mayank Rana8f847102017-11-15 09:57:15 -08002897 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002898 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002899 dbg_event(0xFF, "SUSPEND", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002900 gadget_driver->suspend(&dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002901 spin_lock(&dwc->lock);
2902 }
2903}
2904
2905static void dwc3_resume_gadget(struct dwc3 *dwc)
2906{
Mayank Rana8f847102017-11-15 09:57:15 -08002907 struct usb_gadget_driver *gadget_driver;
2908
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002909 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Mayank Rana8f847102017-11-15 09:57:15 -08002910 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002911 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002912 dbg_event(0xFF, "RESUME", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002913 gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002914 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002915 }
2916}
2917
2918static void dwc3_reset_gadget(struct dwc3 *dwc)
2919{
Mayank Rana8f847102017-11-15 09:57:15 -08002920 struct usb_gadget_driver *gadget_driver;
2921
Felipe Balbi8e744752014-11-06 14:27:53 +08002922 if (!dwc->gadget_driver)
2923 return;
2924
2925 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
Mayank Rana8f847102017-11-15 09:57:15 -08002926 gadget_driver = dwc->gadget_driver;
Felipe Balbi8e744752014-11-06 14:27:53 +08002927 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002928 dbg_event(0xFF, "UDC RESET", 0);
Mayank Rana8f847102017-11-15 09:57:15 -08002929 usb_gadget_udc_reset(&dwc->gadget, gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002930 spin_lock(&dwc->lock);
2931 }
2932}
2933
Mayank Ranaa99689a2016-08-10 17:39:47 -07002934void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002935{
2936 struct dwc3_ep *dep;
2937 struct dwc3_gadget_ep_cmd_params params;
2938 u32 cmd;
2939 int ret;
2940
2941 dep = dwc->eps[epnum];
2942
Felipe Balbib4996a82012-06-06 12:04:13 +03002943 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302944 return;
2945
Pratyush Anand57911502012-07-06 15:19:10 +05302946 /*
2947 * NOTICE: We are violating what the Databook says about the
2948 * EndTransfer command. Ideally we would _always_ wait for the
2949 * EndTransfer Command Completion IRQ, but that's causing too
2950 * much trouble synchronizing between us and gadget driver.
2951 *
2952 * We have discussed this with the IP Provider and it was
2953 * suggested to giveback all requests here, but give HW some
2954 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002955 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302956 *
2957 * Note also that a similar handling was tested by Synopsys
2958 * (thanks a lot Paul) and nothing bad has come out of it.
2959 * In short, what we're doing is:
2960 *
2961 * - Issue EndTransfer WITH CMDIOC bit set
2962 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002963 *
2964 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2965 * supports a mode to work around the above limitation. The
2966 * software can poll the CMDACT bit in the DEPCMD register
2967 * after issuing a EndTransfer command. This mode is enabled
2968 * by writing GUCTL2[14]. This polling is already done in the
2969 * dwc3_send_gadget_ep_cmd() function so if the mode is
2970 * enabled, the EndTransfer command will have completed upon
2971 * returning from this function and we don't need to delay for
2972 * 100us.
2973 *
2974 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302975 */
2976
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302977 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002978 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2979 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002980 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302981 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002982 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302983 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002984 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002985 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002986
2987 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2988 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002989}
2990
2991static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2992{
2993 u32 epnum;
2994
2995 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2996 struct dwc3_ep *dep;
2997
2998 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002999 if (!dep)
3000 continue;
3001
Felipe Balbi72246da2011-08-19 18:10:58 +03003002 if (!(dep->flags & DWC3_EP_ENABLED))
3003 continue;
3004
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02003005 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 }
3007}
3008
3009static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3010{
3011 u32 epnum;
3012
3013 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3014 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003015 int ret;
3016
3017 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003018 if (!dep)
3019 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003020
3021 if (!(dep->flags & DWC3_EP_STALL))
3022 continue;
3023
3024 dep->flags &= ~DWC3_EP_STALL;
3025
John Youn50c763f2016-05-31 17:49:56 -07003026 ret = dwc3_send_clear_stall_ep_cmd(dep);
Mayank Rana558baca2017-02-17 11:46:38 -08003027 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003028 WARN_ON_ONCE(ret);
3029 }
3030}
3031
3032static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3033{
Felipe Balbic4430a22012-05-24 10:30:01 +03003034 int reg;
3035
Mayank Rana558baca2017-02-17 11:46:38 -08003036 dbg_event(0xFF, "DISCONNECT INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003037 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3038 dwc->b_suspend = false;
3039 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3040
Felipe Balbi72246da2011-08-19 18:10:58 +03003041 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3042 reg &= ~DWC3_DCTL_INITU1ENA;
3043 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3044
3045 reg &= ~DWC3_DCTL_INITU2ENA;
3046 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003047
Felipe Balbi72246da2011-08-19 18:10:58 +03003048 dwc3_disconnect_gadget(dwc);
3049
3050 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003051 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003052 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05003053 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003054
3055 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003056 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003057}
3058
Felipe Balbi72246da2011-08-19 18:10:58 +03003059static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3060{
3061 u32 reg;
3062
Felipe Balbifc8bb912016-05-16 13:14:48 +03003063 dwc->connected = true;
3064
Felipe Balbidf62df52011-10-14 15:11:49 +03003065 /*
3066 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3067 * would cause a missing Disconnect Event if there's a
3068 * pending Setup Packet in the FIFO.
3069 *
3070 * There's no suggested workaround on the official Bug
3071 * report, which states that "unless the driver/application
3072 * is doing any special handling of a disconnect event,
3073 * there is no functional issue".
3074 *
3075 * Unfortunately, it turns out that we _do_ some special
3076 * handling of a disconnect event, namely complete all
3077 * pending transfers, notify gadget driver of the
3078 * disconnection, and so on.
3079 *
3080 * Our suggested workaround is to follow the Disconnect
3081 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003082 * flag. Such flag gets set whenever we have a SETUP_PENDING
3083 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003084 * same endpoint.
3085 *
3086 * Refers to:
3087 *
3088 * STAR#9000466709: RTL: Device : Disconnect event not
3089 * generated if setup packet pending in FIFO
3090 */
3091 if (dwc->revision < DWC3_REVISION_188A) {
3092 if (dwc->setup_packet_pending)
3093 dwc3_gadget_disconnect_interrupt(dwc);
3094 }
3095
Mayank Rana558baca2017-02-17 11:46:38 -08003096 dbg_event(0xFF, "BUS RESET", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003097 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3098 dwc->b_suspend = false;
3099 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3100
3101 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07003102 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003103
Felipe Balbi8e744752014-11-06 14:27:53 +08003104 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003105
3106 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3107 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003109 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003110
3111 dwc3_stop_active_transfers(dwc);
3112 dwc3_clear_stall_all_ep(dwc);
3113
3114 /* Reset device address to zero */
3115 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3116 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3117 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003118
3119 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3120 dwc->link_state = DWC3_LINK_STATE_U0;
3121 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003122}
3123
3124static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
3125{
3126 u32 reg;
3127 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
3128
3129 /*
3130 * We change the clock only at SS but I dunno why I would want to do
3131 * this. Maybe it becomes part of the power saving plan.
3132 */
3133
John Younee5cd412016-02-05 17:08:45 -08003134 if ((speed != DWC3_DSTS_SUPERSPEED) &&
3135 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03003136 return;
3137
3138 /*
3139 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3140 * each time on Connect Done.
3141 */
3142 if (!usb30_clock)
3143 return;
3144
3145 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3146 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
3147 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
3148}
3149
Felipe Balbi72246da2011-08-19 18:10:58 +03003150static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3151{
Felipe Balbi72246da2011-08-19 18:10:58 +03003152 struct dwc3_ep *dep;
3153 int ret;
3154 u32 reg;
3155 u8 speed;
3156
Mayank Rana558baca2017-02-17 11:46:38 -08003157 dbg_event(0xFF, "CONNECT DONE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03003158 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3159 speed = reg & DWC3_DSTS_CONNECTSPD;
3160 dwc->speed = speed;
3161
3162 dwc3_update_ram_clk_sel(dwc, speed);
3163
3164 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003165 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003166 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3167 dwc->gadget.ep0->maxpacket = 512;
3168 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3169 break;
John Youn2da9ad72016-05-20 16:34:26 -07003170 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003171 /*
3172 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3173 * would cause a missing USB3 Reset event.
3174 *
3175 * In such situations, we should force a USB3 Reset
3176 * event by calling our dwc3_gadget_reset_interrupt()
3177 * routine.
3178 *
3179 * Refers to:
3180 *
3181 * STAR#9000483510: RTL: SS : USB3 reset event may
3182 * not be generated always when the link enters poll
3183 */
3184 if (dwc->revision < DWC3_REVISION_190A)
3185 dwc3_gadget_reset_interrupt(dwc);
3186
Felipe Balbi72246da2011-08-19 18:10:58 +03003187 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3188 dwc->gadget.ep0->maxpacket = 512;
3189 dwc->gadget.speed = USB_SPEED_SUPER;
3190 break;
John Youn2da9ad72016-05-20 16:34:26 -07003191 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003192 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3193 dwc->gadget.ep0->maxpacket = 64;
3194 dwc->gadget.speed = USB_SPEED_HIGH;
3195 break;
Roger Quadros5e3c2922017-01-03 14:32:09 +02003196 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003197 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3198 dwc->gadget.ep0->maxpacket = 64;
3199 dwc->gadget.speed = USB_SPEED_FULL;
3200 break;
John Youn2da9ad72016-05-20 16:34:26 -07003201 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003202 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3203 dwc->gadget.ep0->maxpacket = 8;
3204 dwc->gadget.speed = USB_SPEED_LOW;
3205 break;
3206 }
3207
Pratyush Anand2b758352013-01-14 15:59:31 +05303208 /* Enable USB2 LPM Capability */
3209
John Younee5cd412016-02-05 17:08:45 -08003210 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003211 (speed != DWC3_DSTS_SUPERSPEED) &&
3212 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303213 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3214 reg |= DWC3_DCFG_LPM_CAP;
3215 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3216
3217 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3218 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3219
Huang Rui460d0982014-10-31 11:11:18 +08003220 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303221
Huang Rui80caf7d2014-10-28 19:54:26 +08003222 /*
3223 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3224 * DCFG.LPMCap is set, core responses with an ACK and the
3225 * BESL value in the LPM token is less than or equal to LPM
3226 * NYET threshold.
3227 */
3228 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3229 && dwc->has_lpm_erratum,
3230 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
3231
3232 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3233 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3234
Pratyush Anand2b758352013-01-14 15:59:31 +05303235 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003236 } else {
3237 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3238 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3239 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303240 }
3241
Mayank Ranaa99689a2016-08-10 17:39:47 -07003242
3243 /*
3244 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
3245 * suspend in P3 state and generates IN_P3 power event irq.
3246 */
3247 dwc3_usb3_phy_suspend(dwc, true);
3248
Felipe Balbi72246da2011-08-19 18:10:58 +03003249 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003250 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3251 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003252 if (ret) {
3253 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3254 return;
3255 }
3256
3257 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003258 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3259 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003260 if (ret) {
3261 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3262 return;
3263 }
3264
Mayank Ranaa99689a2016-08-10 17:39:47 -07003265 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +03003266 /*
3267 * Configure PHY via GUSB3PIPECTLn if required.
3268 *
3269 * Update GTXFIFOSIZn
3270 *
3271 * In both cases reset values should be sufficient.
3272 */
3273}
3274
Mayank Ranaa99689a2016-08-10 17:39:47 -07003275static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003276{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003277 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003278
Mayank Ranaa99689a2016-08-10 17:39:47 -07003279 dev_dbg(dwc->dev, "%s\n", __func__);
3280
Mayank Rana558baca2017-02-17 11:46:38 -08003281 dbg_event(0xFF, "WAKEUP", remote_wakeup);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003282 /*
3283 * Identify if it is called from wakeup_interrupt() context for bus
3284 * resume or as part of remote wakeup. And based on that check for
3285 * U3 state. as we need to handle case of L1 resume i.e. where we
3286 * don't want to perform resume.
3287 */
3288 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3289 perform_resume = false;
3290
3291 /* Only perform resume from L2 or Early Suspend states */
3292 if (perform_resume) {
3293
3294 /*
3295 * In case of remote wake up dwc3_gadget_wakeup_work()
3296 * is doing pm_runtime_get_sync().
3297 */
3298 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3299 dwc->b_suspend = false;
3300 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3301
3302 /*
3303 * set state to U0 as function level resume is trying to queue
3304 * notification over USB interrupt endpoint which would fail
3305 * due to state is not being updated.
3306 */
3307 dwc->link_state = DWC3_LINK_STATE_U0;
3308 dwc3_resume_gadget(dwc);
3309 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003310 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003311
3312 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003313}
3314
3315static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3316 unsigned int evtinfo)
3317{
Felipe Balbifae2b902011-10-14 13:00:30 +03003318 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003319 unsigned int pwropt;
3320
3321 /*
3322 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3323 * Hibernation mode enabled which would show up when device detects
3324 * host-initiated U3 exit.
3325 *
3326 * In that case, device will generate a Link State Change Interrupt
3327 * from U3 to RESUME which is only necessary if Hibernation is
3328 * configured in.
3329 *
3330 * There are no functional changes due to such spurious event and we
3331 * just need to ignore it.
3332 *
3333 * Refers to:
3334 *
3335 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3336 * operational mode
3337 */
3338 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3339 if ((dwc->revision < DWC3_REVISION_250A) &&
3340 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3341 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3342 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06003343 dwc3_trace(trace_dwc3_gadget,
3344 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003345 return;
3346 }
3347 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003348
3349 /*
3350 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3351 * on the link partner, the USB session might do multiple entry/exit
3352 * of low power states before a transfer takes place.
3353 *
3354 * Due to this problem, we might experience lower throughput. The
3355 * suggested workaround is to disable DCTL[12:9] bits if we're
3356 * transitioning from U1/U2 to U0 and enable those bits again
3357 * after a transfer completes and there are no pending transfers
3358 * on any of the enabled endpoints.
3359 *
3360 * This is the first half of that workaround.
3361 *
3362 * Refers to:
3363 *
3364 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3365 * core send LGO_Ux entering U0
3366 */
3367 if (dwc->revision < DWC3_REVISION_183A) {
3368 if (next == DWC3_LINK_STATE_U0) {
3369 u32 u1u2;
3370 u32 reg;
3371
3372 switch (dwc->link_state) {
3373 case DWC3_LINK_STATE_U1:
3374 case DWC3_LINK_STATE_U2:
3375 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3376 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3377 | DWC3_DCTL_ACCEPTU2ENA
3378 | DWC3_DCTL_INITU1ENA
3379 | DWC3_DCTL_ACCEPTU1ENA);
3380
3381 if (!dwc->u1u2)
3382 dwc->u1u2 = reg & u1u2;
3383
3384 reg &= ~u1u2;
3385
3386 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3387 break;
3388 default:
3389 /* do nothing */
3390 break;
3391 }
3392 }
3393 }
3394
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003395 switch (next) {
3396 case DWC3_LINK_STATE_U1:
3397 if (dwc->speed == USB_SPEED_SUPER)
3398 dwc3_suspend_gadget(dwc);
3399 break;
3400 case DWC3_LINK_STATE_U2:
3401 case DWC3_LINK_STATE_U3:
3402 dwc3_suspend_gadget(dwc);
3403 break;
3404 case DWC3_LINK_STATE_RESUME:
3405 dwc3_resume_gadget(dwc);
3406 break;
3407 default:
3408 /* do nothing */
3409 break;
3410 }
3411
Mayank Ranaa99689a2016-08-10 17:39:47 -07003412 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003413 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003414 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003415}
3416
Baolin Wang72704f82016-05-16 16:43:53 +08003417static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003418 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003419{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003420 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003421
Mayank Rana558baca2017-02-17 11:46:38 -08003422 dbg_event(0xFF, "SUSPEND INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003423 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3424
3425 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3426 /*
3427 * When first connecting the cable, even before the initial
3428 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3429 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3430 * event. In such a case, ignore.
3431 * Ignore suspend event until device side usb is not into
3432 * CONFIGURED state.
3433 */
3434 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3435 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3436 __func__, dwc->gadget.state);
3437 return;
3438 }
3439
Baolin Wang72704f82016-05-16 16:43:53 +08003440 dwc3_suspend_gadget(dwc);
3441
Mayank Ranaa99689a2016-08-10 17:39:47 -07003442 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3443 dwc->b_suspend = true;
3444 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3445 }
3446
Baolin Wang72704f82016-05-16 16:43:53 +08003447 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003448 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003449}
3450
Felipe Balbie1dadd32014-02-25 14:47:54 -06003451static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3452 unsigned int evtinfo)
3453{
3454 unsigned int is_ss = evtinfo & BIT(4);
3455
3456 /**
3457 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3458 * have a known issue which can cause USB CV TD.9.23 to fail
3459 * randomly.
3460 *
3461 * Because of this issue, core could generate bogus hibernation
3462 * events which SW needs to ignore.
3463 *
3464 * Refers to:
3465 *
3466 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3467 * Device Fallback from SuperSpeed
3468 */
3469 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3470 return;
3471
3472 /* enter hibernation here */
3473}
3474
Felipe Balbi72246da2011-08-19 18:10:58 +03003475static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3476 const struct dwc3_event_devt *event)
3477{
3478 switch (event->type) {
3479 case DWC3_DEVICE_EVENT_DISCONNECT:
3480 dwc3_gadget_disconnect_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003481 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003482 break;
3483 case DWC3_DEVICE_EVENT_RESET:
3484 dwc3_gadget_reset_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003485 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003486 break;
3487 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3488 dwc3_gadget_conndone_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003489 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003490 break;
3491 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003492 dwc3_gadget_wakeup_interrupt(dwc, false);
Mayank Rana0c667b42017-02-09 11:56:51 -08003493 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003494 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003495 case DWC3_DEVICE_EVENT_HIBER_REQ:
3496 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3497 "unexpected hibernation event\n"))
3498 break;
3499
3500 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3501 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003502 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3503 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Mayank Rana0c667b42017-02-09 11:56:51 -08003504 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003505 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003506 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003507 if (dwc->revision < DWC3_REVISION_230A) {
3508 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003509 dwc->dbg_gadget_events.eopf++;
Baolin Wang72704f82016-05-16 16:43:53 +08003510 } else {
3511 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
Mayank Rana558baca2017-02-17 11:46:38 -08003512 dbg_event(0xFF, "GAD SUS", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003513 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003514 /*
3515 * Ignore suspend event until the gadget enters into
3516 * USB_STATE_CONFIGURED state.
3517 */
3518 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3519 dwc3_gadget_suspend_interrupt(dwc,
3520 event->event_info);
3521 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003522 break;
3523 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003524 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003525 dwc->dbg_gadget_events.sof++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003526 break;
3527 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003528 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Mayank Rana558baca2017-02-17 11:46:38 -08003529 dbg_event(0xFF, "ERROR", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003530 dwc->dbg_gadget_events.erratic_error++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003531 break;
3532 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003533 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08003534 dwc->dbg_gadget_events.cmdcmplt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003535 break;
3536 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003537 dwc3_trace(trace_dwc3_gadget, "Overflow");
Mayank Rana0c667b42017-02-09 11:56:51 -08003538 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003539 break;
3540 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003541 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Mayank Rana0c667b42017-02-09 11:56:51 -08003542 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003543 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003544
3545 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003546}
3547
3548static void dwc3_process_event_entry(struct dwc3 *dwc,
3549 const union dwc3_event *event)
3550{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003551 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003552 /* skip event processing in absence of vbus */
3553 if (!dwc->vbus_active) {
Mayank Rana6300b452017-05-24 09:33:22 -07003554 dbg_event(0xFF, "SKIP_EVT", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003555 return;
3556 }
3557
3558 /* If run/stop is cleared don't process any more events */
3559 if (!dwc->pullups_connected) {
Mayank Rana6300b452017-05-24 09:33:22 -07003560 dbg_event(0xFF, "SKIP_EVT_PULLUP", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003561 return;
3562 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003563
Felipe Balbi72246da2011-08-19 18:10:58 +03003564 /* Endpoint IRQ, handle it and return early */
3565 if (event->type.is_devspec == 0) {
3566 /* depevt */
3567 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3568 }
3569
3570 switch (event->type.type) {
3571 case DWC3_EVENT_TYPE_DEV:
3572 dwc3_gadget_interrupt(dwc, &event->devt);
3573 break;
3574 /* REVISIT what to do with Carkit and I2C events ? */
3575 default:
3576 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3577 }
3578}
3579
Mayank Ranaa99689a2016-08-10 17:39:47 -07003580static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003581{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003582 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003583 irqreturn_t ret = IRQ_NONE;
3584 int left;
3585 u32 reg;
3586
Mayank Ranaa99689a2016-08-10 17:39:47 -07003587 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003588 left = evt->count;
3589
3590 if (!(evt->flags & DWC3_EVENT_PENDING))
3591 return IRQ_NONE;
3592
3593 while (left > 0) {
3594 union dwc3_event event;
3595
3596 event.raw = *(u32 *) (evt->buf + evt->lpos);
3597
3598 dwc3_process_event_entry(dwc, &event);
3599
Mayank Ranaa99689a2016-08-10 17:39:47 -07003600 if (dwc->err_evt_seen) {
3601 /*
3602 * if erratic error, skip remaining events
3603 * while controller undergoes reset
3604 */
3605 evt->lpos = (evt->lpos + left) %
3606 DWC3_EVENT_BUFFERS_SIZE;
3607 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
3608 if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT))
3609 dwc->err_evt_seen = 0;
3610 break;
3611 }
3612
Felipe Balbif42f2442013-06-12 21:25:08 +03003613 /*
3614 * FIXME we wrap around correctly to the next entry as
3615 * almost all entries are 4 bytes in size. There is one
3616 * entry which has 12 bytes which is a regular entry
3617 * followed by 8 bytes data. ATM I don't know how
3618 * things are organized if we get next to the a
3619 * boundary so I worry about that once we try to handle
3620 * that.
3621 */
3622 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3623 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003624 }
3625
Mayank Ranaa99689a2016-08-10 17:39:47 -07003626 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003627 evt->count = 0;
3628 evt->flags &= ~DWC3_EVENT_PENDING;
3629 ret = IRQ_HANDLED;
3630
3631 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003632 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003633 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003634 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003635
John Youn26cac202016-11-14 12:32:43 -08003636 if (dwc->imod_interval)
3637 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0),
3638 DWC3_GEVNTCOUNT_EHB);
3639
Felipe Balbif42f2442013-06-12 21:25:08 +03003640 return ret;
3641}
3642
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003643void dwc3_bh_work(struct work_struct *w)
3644{
3645 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3646
3647 pm_runtime_get_sync(dwc->dev);
3648 dwc3_thread_interrupt(dwc->irq, dwc);
3649 pm_runtime_put(dwc->dev);
3650}
3651
Mayank Ranaa99689a2016-08-10 17:39:47 -07003652static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3653{
3654 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003655 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003656 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003657 unsigned int temp_time;
3658 ktime_t start_time;
3659
3660 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003661
Felipe Balbie5f68b42015-10-12 13:25:44 -05003662 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003663 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3664
3665 ret = dwc3_process_event_buf(dwc);
3666
Felipe Balbie5f68b42015-10-12 13:25:44 -05003667 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003668
Mayank Ranaa99689a2016-08-10 17:39:47 -07003669 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3670 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3671 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3672
Felipe Balbib15a7622011-06-30 16:57:15 +03003673 return ret;
3674}
3675
Mayank Ranaa99689a2016-08-10 17:39:47 -07003676static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003677{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003678 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003679 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003680 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003681
Mayank Ranaa99689a2016-08-10 17:39:47 -07003682 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003683
Thinh Nguyenff9177b2017-05-11 17:26:47 -07003684 /*
3685 * With PCIe legacy interrupt, test shows that top-half irq handler can
3686 * be called again after HW interrupt deassertion. Check if bottom-half
3687 * irq event handler completes before caching new event to prevent
3688 * losing events.
3689 */
3690 if (evt->flags & DWC3_EVENT_PENDING)
3691 return IRQ_HANDLED;
3692
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003693 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003694 count &= DWC3_GEVNTCOUNT_MASK;
3695 if (!count)
3696 return IRQ_NONE;
3697
Mayank Ranaa99689a2016-08-10 17:39:47 -07003698 if (count > evt->length) {
3699 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
Mayank Rana558baca2017-02-17 11:46:38 -08003700 dbg_event(0xFF, "HUGE_EVCNT", count);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003701 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3702 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3703 return IRQ_HANDLED;
3704 }
3705
Felipe Balbib15a7622011-06-30 16:57:15 +03003706 evt->count = count;
3707 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003708
Felipe Balbie8adfc32013-06-12 21:11:14 +03003709 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003710 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003711 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003712 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003713
John Youn551d2902016-11-15 13:08:59 +02003714 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3715
Felipe Balbib15a7622011-06-30 16:57:15 +03003716 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003717}
3718
Mayank Ranaa99689a2016-08-10 17:39:47 -07003719irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003720{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003721 struct dwc3 *dwc = _dwc;
3722 irqreturn_t ret = IRQ_NONE;
3723 irqreturn_t status;
3724 unsigned int temp_cnt = 0;
3725 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003726
Mayank Ranaa99689a2016-08-10 17:39:47 -07003727 start_time = ktime_get();
3728 dwc->irq_cnt++;
3729
3730 /* controller reset is still pending */
3731 if (dwc->err_evt_seen)
3732 return IRQ_HANDLED;
3733
3734 status = dwc3_check_event_buf(dwc);
3735 if (status == IRQ_WAKE_THREAD)
3736 ret = status;
3737
3738 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3739 dwc->irq_completion_time[dwc->irq_dbg_index] =
3740 ktime_us_delta(ktime_get(), start_time);
3741 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3742 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3743
Hemant Kumar78c7c282016-08-09 12:28:55 -07003744 if (ret == IRQ_WAKE_THREAD)
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003745 queue_work(dwc->dwc_wq, &dwc->bh_work);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003746
3747 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003748}
3749
3750/**
3751 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003752 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003753 *
3754 * Returns 0 on success otherwise negative errno.
3755 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003756int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003757{
Roger Quadros9522def2016-06-10 14:48:38 +03003758 int ret, irq;
3759 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3760
3761 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3762 if (irq == -EPROBE_DEFER)
3763 return irq;
3764
3765 if (irq <= 0) {
3766 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3767 if (irq == -EPROBE_DEFER)
3768 return irq;
3769
3770 if (irq <= 0) {
3771 irq = platform_get_irq(dwc3_pdev, 0);
3772 if (irq <= 0) {
3773 if (irq != -EPROBE_DEFER) {
3774 dev_err(dwc->dev,
3775 "missing peripheral IRQ\n");
3776 }
3777 if (!irq)
3778 irq = -EINVAL;
3779 return irq;
3780 }
3781 }
3782 }
3783
3784 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003785
Mayank Ranaa99689a2016-08-10 17:39:47 -07003786 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3787
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303788 dwc->ctrl_req = dma_alloc_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003789 &dwc->ctrl_req_addr, GFP_KERNEL);
3790 if (!dwc->ctrl_req) {
3791 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3792 ret = -ENOMEM;
3793 goto err0;
3794 }
3795
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303796 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3797 sizeof(*dwc->ep0_trb) * 2,
3798 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003799 if (!dwc->ep0_trb) {
3800 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3801 ret = -ENOMEM;
3802 goto err1;
3803 }
3804
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003805 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003806 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003807 ret = -ENOMEM;
3808 goto err2;
3809 }
3810
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303811 dwc->ep0_bounce = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003812 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3813 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003814 if (!dwc->ep0_bounce) {
3815 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3816 ret = -ENOMEM;
3817 goto err3;
3818 }
3819
Felipe Balbi04c03d12015-12-02 10:06:45 -06003820 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3821 if (!dwc->zlp_buf) {
3822 ret = -ENOMEM;
3823 goto err4;
3824 }
3825
Felipe Balbi72246da2011-08-19 18:10:58 +03003826 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003827 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003828 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003829 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003830 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003831
3832 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003833 * FIXME We might be setting max_speed to <SUPER, however versions
3834 * <2.20a of dwc3 have an issue with metastability (documented
3835 * elsewhere in this driver) which tells us we can't set max speed to
3836 * anything lower than SUPER.
3837 *
3838 * Because gadget.max_speed is only used by composite.c and function
3839 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3840 * to happen so we avoid sending SuperSpeed Capability descriptor
3841 * together with our BOS descriptor as that could confuse host into
3842 * thinking we can handle super speed.
3843 *
3844 * Note that, in fact, we won't even support GetBOS requests when speed
3845 * is less than super speed because we don't have means, yet, to tell
3846 * composite.c that we are USB 2.0 + LPM ECN.
3847 */
3848 if (dwc->revision < DWC3_REVISION_220A)
3849 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003850 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003851 dwc->revision);
3852
3853 dwc->gadget.max_speed = dwc->maximum_speed;
3854
3855 /*
David Cohena4b9d942013-12-09 15:55:38 -08003856 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3857 * on ep out.
3858 */
3859 dwc->gadget.quirk_ep_out_aligned_size = true;
3860
3861 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003862 * REVISIT: Here we should clear all pending IRQs to be
3863 * sure we're starting from a well known location.
3864 */
3865
3866 ret = dwc3_gadget_init_endpoints(dwc);
3867 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003868 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003869
Felipe Balbi72246da2011-08-19 18:10:58 +03003870 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3871 if (ret) {
3872 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003873 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003874 }
3875
Mayank Ranaa99689a2016-08-10 17:39:47 -07003876 if (!dwc->is_drd) {
3877 pm_runtime_no_callbacks(&dwc->gadget.dev);
3878 pm_runtime_set_active(&dwc->gadget.dev);
3879 pm_runtime_enable(&dwc->gadget.dev);
3880 pm_runtime_get(&dwc->gadget.dev);
3881 }
3882
Felipe Balbi72246da2011-08-19 18:10:58 +03003883 return 0;
3884
Felipe Balbi04c03d12015-12-02 10:06:45 -06003885err5:
3886 kfree(dwc->zlp_buf);
3887
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003888err4:
David Cohene1f80462013-09-11 17:42:47 -07003889 dwc3_gadget_free_endpoints(dwc);
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303890 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003891 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003892
Felipe Balbi72246da2011-08-19 18:10:58 +03003893err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003894 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003895
3896err2:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303897 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003898 dwc->ep0_trb, dwc->ep0_trb_addr);
3899
3900err1:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303901 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003902 dwc->ctrl_req, dwc->ctrl_req_addr);
3903
3904err0:
3905 return ret;
3906}
3907
Felipe Balbi7415f172012-04-30 14:56:33 +03003908/* -------------------------------------------------------------------------- */
3909
Felipe Balbi72246da2011-08-19 18:10:58 +03003910void dwc3_gadget_exit(struct dwc3 *dwc)
3911{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003912 if (dwc->is_drd) {
3913 pm_runtime_put(&dwc->gadget.dev);
3914 pm_runtime_disable(&dwc->gadget.dev);
3915 }
3916
Felipe Balbi72246da2011-08-19 18:10:58 +03003917 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003918
Felipe Balbi72246da2011-08-19 18:10:58 +03003919 dwc3_gadget_free_endpoints(dwc);
3920
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303921 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003922 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003923
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003924 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003925 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003926
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303927 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003928 dwc->ep0_trb, dwc->ep0_trb_addr);
3929
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303930 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003931 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003932}
Felipe Balbi7415f172012-04-30 14:56:33 +03003933
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003934int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003935{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003936 int ret;
3937
Roger Quadros9772b472016-04-12 11:33:29 +03003938 if (!dwc->gadget_driver)
3939 return 0;
3940
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003941 ret = dwc3_gadget_run_stop(dwc, false, false);
3942 if (ret < 0)
3943 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003944
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003945 dwc3_disconnect_gadget(dwc);
3946 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003947
3948 return 0;
3949}
3950
3951int dwc3_gadget_resume(struct dwc3 *dwc)
3952{
Felipe Balbi7415f172012-04-30 14:56:33 +03003953 int ret;
3954
Roger Quadros9772b472016-04-12 11:33:29 +03003955 if (!dwc->gadget_driver)
3956 return 0;
3957
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003958 ret = __dwc3_gadget_start(dwc);
3959 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003960 goto err0;
3961
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003962 ret = dwc3_gadget_run_stop(dwc, true, false);
3963 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003964 goto err1;
3965
Felipe Balbi7415f172012-04-30 14:56:33 +03003966 return 0;
3967
3968err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003969 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003970
3971err0:
3972 return ret;
3973}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003974
3975void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3976{
3977 if (dwc->pending_events) {
3978 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3979 dwc->pending_events = false;
3980 enable_irq(dwc->irq_gadget);
3981 }
3982}