blob: 1431a88437afe682b3838193af8ec3e6cb1961ff [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020030/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030031 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020032 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
34 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030035 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037 */
38int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
39{
40 u32 reg;
41
42 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
43 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
44
45 switch (mode) {
46 case TEST_J:
47 case TEST_K:
48 case TEST_SE0_NAK:
49 case TEST_PACKET:
50 case TEST_FORCE_EN:
51 reg |= mode << 1;
52 break;
53 default:
54 return -EINVAL;
55 }
56
57 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
58
59 return 0;
60}
61
Felipe Balbi8598bde2012-01-02 18:55:57 +020062/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030063 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030064 * @dwc: pointer to our context structure
65 *
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
68 */
69int dwc3_gadget_get_link_state(struct dwc3 *dwc)
70{
71 u32 reg;
72
73 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
74
75 return DWC3_DSTS_USBLNKST(reg);
76}
77
78/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030079 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020080 * @dwc: pointer to our context structure
81 * @state: the state to put link into
82 *
83 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080084 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020085 */
86int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
87{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080088 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020089 u32 reg;
90
Paul Zimmerman802fde92012-04-27 13:10:52 +030091 /*
92 * Wait until device controller is ready. Only applies to 1.94a and
93 * later RTL.
94 */
95 if (dwc->revision >= DWC3_REVISION_194A) {
96 while (--retries) {
97 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
98 if (reg & DWC3_DSTS_DCNRD)
99 udelay(5);
100 else
101 break;
102 }
103
104 if (retries <= 0)
105 return -ETIMEDOUT;
106 }
107
Felipe Balbi8598bde2012-01-02 18:55:57 +0200108 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
109 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
110
111 /* set requested state */
112 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
113 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
114
Paul Zimmerman802fde92012-04-27 13:10:52 +0300115 /*
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
118 */
119 if (dwc->revision >= DWC3_REVISION_194A)
120 return 0;
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300123 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200124 while (--retries) {
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
126
Felipe Balbi8598bde2012-01-02 18:55:57 +0200127 if (DWC3_DSTS_USBLNKST(reg) == state)
128 return 0;
129
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800130 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 }
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 return -ETIMEDOUT;
134}
135
John Youndca01192016-05-19 17:26:05 -0700136/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700139 *
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
143 */
144static void dwc3_ep_inc_trb(u8 *index)
145{
146 (*index)++;
147 if (*index == (DWC3_TRB_NUM - 1))
148 *index = 0;
149}
150
Felipe Balbibfad65e2017-04-19 14:59:27 +0300151/**
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
154 */
Felipe Balbief966b92016-04-05 13:09:51 +0300155static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200156{
John Youndca01192016-05-19 17:26:05 -0700157 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbibfad65e2017-04-19 14:59:27 +0300160/**
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
Felipe Balbief966b92016-04-05 13:09:51 +0300164static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
165{
John Youndca01192016-05-19 17:26:05 -0700166 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167}
168
Felipe Balbibfad65e2017-04-19 14:59:27 +0300169/**
170 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
171 * @dep: The endpoint to whom the request belongs to
172 * @req: The request we're giving back
173 * @status: completion code for the request
174 *
175 * Must be called with controller's lock held and interrupts disabled. This
176 * function will unmap @req and call its ->complete() callback to notify upper
177 * layers that it has completed.
178 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300179void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
180 int status)
181{
182 struct dwc3 *dwc = dep->dwc;
183
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300184 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300185 list_del(&req->list);
Felipe Balbie62c5bc52016-10-25 13:47:21 +0300186 req->remaining = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Jack Pham4a71fcb2017-06-29 00:53:31 -0700191 if (req->trb)
192 usb_gadget_unmap_request_by_dev(dwc->sysdev,
193 &req->request, req->direction);
194
195 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300202
203 if (dep->number > 1)
204 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300205}
206
Felipe Balbibfad65e2017-04-19 14:59:27 +0300207/**
208 * dwc3_send_gadget_generic_command - issue a generic command for the controller
209 * @dwc: pointer to the controller context
210 * @cmd: the command to be issued
211 * @param: command parameter
212 *
213 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
214 * and wait for its completion.
215 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500216int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300217{
218 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300219 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300220 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300221 u32 reg;
222
223 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
224 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
225
226 do {
227 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
228 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300229 status = DWC3_DGCMD_STATUS(reg);
230 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300231 ret = -EINVAL;
232 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300233 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100234 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235
236 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300237 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300238 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300239 }
240
Felipe Balbi71f7e702016-05-23 14:16:19 +0300241 trace_dwc3_gadget_generic_cmd(cmd, param, status);
242
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300243 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300244}
245
Felipe Balbic36d8e92016-04-04 12:46:33 +0300246static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
247
Felipe Balbibfad65e2017-04-19 14:59:27 +0300248/**
249 * dwc3_send_gadget_ep_cmd - issue an endpoint command
250 * @dep: the endpoint to which the command is going to be issued
251 * @cmd: the command to be issued
252 * @params: parameters to the command
253 *
254 * Caller should handle locking. This function will issue @cmd with given
255 * @params to @dep and wait for its completion.
256 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300257int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
258 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300259{
Felipe Balbi8897a762016-09-22 10:56:08 +0300260 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300261 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000262 u32 timeout = 1000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300263 u32 reg;
264
Felipe Balbi0933df12016-05-23 14:02:33 +0300265 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300266 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300267 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300268
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300269 /*
270 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
271 * we're issuing an endpoint command, we must check if
272 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
273 *
274 * We will also set SUSPHY bit to what it was before returning as stated
275 * by the same section on Synopsys databook.
276 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300277 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
278 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
279 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
280 susphy = true;
281 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
282 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
283 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 }
285
Felipe Balbi59999142016-09-22 12:25:28 +0300286 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300287 int needs_wakeup;
288
289 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
290 dwc->link_state == DWC3_LINK_STATE_U2 ||
291 dwc->link_state == DWC3_LINK_STATE_U3);
292
293 if (unlikely(needs_wakeup)) {
294 ret = __dwc3_gadget_wakeup(dwc);
295 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
296 ret);
297 }
298 }
299
Felipe Balbi2eb88012016-04-12 16:53:39 +0300300 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
301 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
302 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300303
Felipe Balbi8897a762016-09-22 10:56:08 +0300304 /*
305 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
306 * not relying on XferNotReady, we can make use of a special "No
307 * Response Update Transfer" command where we should clear both CmdAct
308 * and CmdIOC bits.
309 *
310 * With this, we don't need to wait for command completion and can
311 * straight away issue further commands to the endpoint.
312 *
313 * NOTICE: We're making an assumption that control endpoints will never
314 * make use of Update Transfer command. This is a safe assumption
315 * because we can never have more than one request at a time with
316 * Control Endpoints. If anybody changes that assumption, this chunk
317 * needs to be updated accordingly.
318 */
319 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
320 !usb_endpoint_xfer_isoc(desc))
321 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
322 else
323 cmd |= DWC3_DEPCMD_CMDACT;
324
325 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300327 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300328 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300329 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000330
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000331 switch (cmd_status) {
332 case 0:
333 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000335 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000336 ret = -EINVAL;
337 break;
338 case DEPEVT_TRANSFER_BUS_EXPIRY:
339 /*
340 * SW issues START TRANSFER command to
341 * isochronous ep with future frame interval. If
342 * future interval time has already passed when
343 * core receives the command, it will respond
344 * with an error status of 'Bus Expiry'.
345 *
346 * Instead of always returning -EINVAL, let's
347 * give a hint to the gadget driver that this is
348 * the case by returning -EAGAIN.
349 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000350 ret = -EAGAIN;
351 break;
352 default:
353 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
354 }
355
Felipe Balbic0ca3242016-04-04 09:11:51 +0300356 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300357 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300358 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300359
Felipe Balbif6bb2252016-05-23 13:53:34 +0300360 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300361 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300362 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300363 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300364
Felipe Balbi0933df12016-05-23 14:02:33 +0300365 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
366
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300367 if (ret == 0) {
368 switch (DWC3_DEPCMD_CMD(cmd)) {
369 case DWC3_DEPCMD_STARTTRANSFER:
370 dep->flags |= DWC3_EP_TRANSFER_STARTED;
371 break;
372 case DWC3_DEPCMD_ENDTRANSFER:
373 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
374 break;
375 default:
376 /* nothing */
377 break;
378 }
379 }
380
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300381 if (unlikely(susphy)) {
382 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
383 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
384 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
385 }
386
Felipe Balbic0ca3242016-04-04 09:11:51 +0300387 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300388}
389
John Youn50c763f2016-05-31 17:49:56 -0700390static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
391{
392 struct dwc3 *dwc = dep->dwc;
393 struct dwc3_gadget_ep_cmd_params params;
394 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
395
396 /*
397 * As of core revision 2.60a the recommended programming model
398 * is to set the ClearPendIN bit when issuing a Clear Stall EP
399 * command for IN endpoints. This is to prevent an issue where
400 * some (non-compliant) hosts may not send ACK TPs for pending
401 * IN transfers due to a mishandled error condition. Synopsys
402 * STAR 9000614252.
403 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800404 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
405 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700406 cmd |= DWC3_DEPCMD_CLEARPENDIN;
407
408 memset(&params, 0, sizeof(params));
409
Felipe Balbi2cd47182016-04-12 16:42:43 +0300410 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700411}
412
Felipe Balbi72246da2011-08-19 18:10:58 +0300413static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200414 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300415{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300416 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300417
418 return dep->trb_pool_dma + offset;
419}
420
421static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
422{
423 struct dwc3 *dwc = dep->dwc;
424
425 if (dep->trb_pool)
426 return 0;
427
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530428 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
430 &dep->trb_pool_dma, GFP_KERNEL);
431 if (!dep->trb_pool) {
432 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
433 dep->name);
434 return -ENOMEM;
435 }
436
437 return 0;
438}
439
440static void dwc3_free_trb_pool(struct dwc3_ep *dep)
441{
442 struct dwc3 *dwc = dep->dwc;
443
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530444 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300445 dep->trb_pool, dep->trb_pool_dma);
446
447 dep->trb_pool = NULL;
448 dep->trb_pool_dma = 0;
449}
450
John Younc4509602016-02-16 20:10:53 -0800451static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
452
453/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300454 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800455 * @dwc: pointer to our controller context structure
456 * @dep: endpoint that is being enabled
457 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300458 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
459 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800460 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300461 * The assignment of transfer resources cannot perfectly follow the data book
462 * due to the fact that the controller driver does not have all knowledge of the
463 * configuration in advance. It is given this information piecemeal by the
464 * composite gadget framework after every SET_CONFIGURATION and
465 * SET_INTERFACE. Trying to follow the databook programming model in this
466 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800467 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300468 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
469 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
470 * incorrect in the scenario of multiple interfaces.
471 *
472 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800473 * endpoint on alt setting (8.1.6).
474 *
475 * The following simplified method is used instead:
476 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300477 * All hardware endpoints can be assigned a transfer resource and this setting
478 * will stay persistent until either a core reset or hibernation. So whenever we
479 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
480 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800481 * guaranteed that there are as many transfer resources as endpoints.
482 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300483 * This function is called for each endpoint when it is being enabled but is
484 * triggered only when called for EP0-out, which always happens first, and which
485 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800486 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300487static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
488{
489 struct dwc3_gadget_ep_cmd_params params;
490 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800491 int i;
492 int ret;
493
494 if (dep->number)
495 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300496
497 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800498 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Felipe Balbi2cd47182016-04-12 16:42:43 +0300500 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800501 if (ret)
502 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
John Younc4509602016-02-16 20:10:53 -0800504 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
505 struct dwc3_ep *dep = dwc->eps[i];
506
507 if (!dep)
508 continue;
509
510 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
511 if (ret)
512 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300513 }
514
515 return 0;
516}
517
518static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300519 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300520{
John Youn39ebb052016-11-09 16:36:28 -0800521 const struct usb_ss_ep_comp_descriptor *comp_desc;
522 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 struct dwc3_gadget_ep_cmd_params params;
524
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300525 if (dev_WARN_ONCE(dwc->dev, modify && restore,
526 "Can't modify and restore\n"))
527 return -EINVAL;
528
John Youn39ebb052016-11-09 16:36:28 -0800529 comp_desc = dep->endpoint.comp_desc;
530 desc = dep->endpoint.desc;
531
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 memset(&params, 0x00, sizeof(params));
533
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300534 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900535 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
536
537 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800538 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300539 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300540 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900541 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300543 if (modify) {
544 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
545 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600546 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
547 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300548 } else {
549 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600550 }
551
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300552 if (usb_endpoint_xfer_control(desc))
553 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300554
555 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
556 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200558 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300559 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
560 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300561 dep->stream_capable = true;
562 }
563
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500564 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300565 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
567 /*
568 * We are doing 1:1 mapping for endpoints, meaning
569 * Physical Endpoints 2 maps to Logical Endpoint 2 and
570 * so on. We consider the direction bit as part of the physical
571 * endpoint number. So USB endpoint 0x81 is 0x03.
572 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300573 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300574
575 /*
576 * We must use the lower 16 TX FIFOs even though
577 * HW might have more
578 */
579 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300580 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
582 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 dep->interval = 1 << (desc->bInterval - 1);
585 }
586
Felipe Balbi2cd47182016-04-12 16:42:43 +0300587 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588}
589
590static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
591{
592 struct dwc3_gadget_ep_cmd_params params;
593
594 memset(&params, 0x00, sizeof(params));
595
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300596 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300597
Felipe Balbi2cd47182016-04-12 16:42:43 +0300598 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
599 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300600}
601
602/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300603 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 * @dep: endpoint to be initialized
Felipe Balbibfad65e2017-04-19 14:59:27 +0300605 * @modify: if true, modify existing endpoint configuration
606 * @restore: if true, restore endpoint configuration from scratch buffer
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300608 * Caller should take care of locking. Execute all necessary commands to
609 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 */
611static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300612 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300613{
John Youn39ebb052016-11-09 16:36:28 -0800614 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800616
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300618 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300619
620 if (!(dep->flags & DWC3_EP_ENABLED)) {
621 ret = dwc3_gadget_start_config(dwc, dep);
622 if (ret)
623 return ret;
624 }
625
John Youn39ebb052016-11-09 16:36:28 -0800626 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300627 if (ret)
628 return ret;
629
630 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200631 struct dwc3_trb *trb_st_hw;
632 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 dep->type = usb_endpoint_type(desc);
635 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800636 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300637
638 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
639 reg |= DWC3_DALEPENA_EP(dep->number);
640 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
641
Baolin Wang76a638f2016-10-31 19:38:36 +0800642 init_waitqueue_head(&dep->wait_end_transfer);
643
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300644 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200645 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300646
John Youn0d257442016-05-19 17:26:08 -0700647 /* Initialize the TRB ring */
648 dep->trb_dequeue = 0;
649 dep->trb_enqueue = 0;
650 memset(dep->trb_pool, 0,
651 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
652
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300653 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300654 trb_st_hw = &dep->trb_pool[0];
655
Felipe Balbif6bafc62012-02-06 11:04:53 +0200656 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200657 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
658 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
659 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
660 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 }
662
Felipe Balbia97ea992016-09-29 16:28:56 +0300663 /*
664 * Issue StartTransfer here with no-op TRB so we can always rely on No
665 * Response Update Transfer command.
666 */
667 if (usb_endpoint_xfer_bulk(desc)) {
668 struct dwc3_gadget_ep_cmd_params params;
669 struct dwc3_trb *trb;
670 dma_addr_t trb_dma;
671 u32 cmd;
672
673 memset(&params, 0, sizeof(params));
674 trb = &dep->trb_pool[0];
675 trb_dma = dwc3_trb_dma_offset(dep, trb);
676
677 params.param0 = upper_32_bits(trb_dma);
678 params.param1 = lower_32_bits(trb_dma);
679
680 cmd = DWC3_DEPCMD_STARTTRANSFER;
681
682 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
683 if (ret < 0)
684 return ret;
685
686 dep->flags |= DWC3_EP_BUSY;
687
688 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
689 WARN_ON_ONCE(!dep->resource_index);
690 }
691
Felipe Balbi2870e502016-11-03 13:53:29 +0200692
693out:
694 trace_dwc3_gadget_ep_enable(dep);
695
Felipe Balbi72246da2011-08-19 18:10:58 +0300696 return 0;
697}
698
Paul Zimmermanb992e682012-04-27 14:17:35 +0300699static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200700static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300701{
702 struct dwc3_request *req;
703
Felipe Balbi0e146022016-06-21 10:32:02 +0300704 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300705
Felipe Balbi0e146022016-06-21 10:32:02 +0300706 /* - giveback all requests to gadget driver */
707 while (!list_empty(&dep->started_list)) {
708 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200709
Felipe Balbi0e146022016-06-21 10:32:02 +0300710 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200711 }
712
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200713 while (!list_empty(&dep->pending_list)) {
714 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200716 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300717 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300718}
719
720/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300721 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 * @dep: the endpoint to disable
723 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300724 * This function undoes what __dwc3_gadget_ep_enable did and also removes
725 * requests which are currently being processed by the hardware and those which
726 * are not yet scheduled.
727 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200728 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300730static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
731{
732 struct dwc3 *dwc = dep->dwc;
733 u32 reg;
734
Felipe Balbi2870e502016-11-03 13:53:29 +0200735 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500736
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200737 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300738
Felipe Balbi687ef982014-04-16 10:30:33 -0500739 /* make sure HW endpoint isn't stalled */
740 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500741 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500742
Felipe Balbi72246da2011-08-19 18:10:58 +0300743 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
744 reg &= ~DWC3_DALEPENA_EP(dep->number);
745 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
746
Felipe Balbi879631a2011-09-30 10:58:47 +0300747 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300748 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800749 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300750
John Youn39ebb052016-11-09 16:36:28 -0800751 /* Clear out the ep descriptors for non-ep0 */
752 if (dep->number > 1) {
753 dep->endpoint.comp_desc = NULL;
754 dep->endpoint.desc = NULL;
755 }
756
Felipe Balbi72246da2011-08-19 18:10:58 +0300757 return 0;
758}
759
760/* -------------------------------------------------------------------------- */
761
762static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
763 const struct usb_endpoint_descriptor *desc)
764{
765 return -EINVAL;
766}
767
768static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
769{
770 return -EINVAL;
771}
772
773/* -------------------------------------------------------------------------- */
774
775static int dwc3_gadget_ep_enable(struct usb_ep *ep,
776 const struct usb_endpoint_descriptor *desc)
777{
778 struct dwc3_ep *dep;
779 struct dwc3 *dwc;
780 unsigned long flags;
781 int ret;
782
783 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
784 pr_debug("dwc3: invalid parameters\n");
785 return -EINVAL;
786 }
787
788 if (!desc->wMaxPacketSize) {
789 pr_debug("dwc3: missing wMaxPacketSize\n");
790 return -EINVAL;
791 }
792
793 dep = to_dwc3_ep(ep);
794 dwc = dep->dwc;
795
Felipe Balbi95ca9612015-12-10 13:08:20 -0600796 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
797 "%s is already enabled\n",
798 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300799 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300800
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800802 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300803 spin_unlock_irqrestore(&dwc->lock, flags);
804
805 return ret;
806}
807
808static int dwc3_gadget_ep_disable(struct usb_ep *ep)
809{
810 struct dwc3_ep *dep;
811 struct dwc3 *dwc;
812 unsigned long flags;
813 int ret;
814
815 if (!ep) {
816 pr_debug("dwc3: invalid parameters\n");
817 return -EINVAL;
818 }
819
820 dep = to_dwc3_ep(ep);
821 dwc = dep->dwc;
822
Felipe Balbi95ca9612015-12-10 13:08:20 -0600823 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
824 "%s is already disabled\n",
825 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300826 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300827
Felipe Balbi72246da2011-08-19 18:10:58 +0300828 spin_lock_irqsave(&dwc->lock, flags);
829 ret = __dwc3_gadget_ep_disable(dep);
830 spin_unlock_irqrestore(&dwc->lock, flags);
831
832 return ret;
833}
834
835static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
836 gfp_t gfp_flags)
837{
838 struct dwc3_request *req;
839 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300840
841 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900842 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844
845 req->epnum = dep->number;
846 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300847
Felipe Balbi68d34c82016-05-30 13:34:58 +0300848 dep->allocated_requests++;
849
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500850 trace_dwc3_alloc_request(req);
851
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 return &req->request;
853}
854
855static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
856 struct usb_request *request)
857{
858 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300859 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300860
Felipe Balbi68d34c82016-05-30 13:34:58 +0300861 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500862 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300863 kfree(req);
864}
865
Felipe Balbi2c78c022016-08-12 13:13:10 +0300866static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
867
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200868static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
869 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
870 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200871{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300872 struct dwc3 *dwc = dep->dwc;
873 struct usb_gadget *gadget = &dwc->gadget;
874 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200875
Felipe Balbief966b92016-04-05 13:09:51 +0300876 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530877
Felipe Balbif6bafc62012-02-06 11:04:53 +0200878 trb->size = DWC3_TRB_SIZE_LENGTH(length);
879 trb->bpl = lower_32_bits(dma);
880 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200881
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200882 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200883 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200884 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200885 break;
886
887 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300888 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530889 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300890
Manu Gautam40d829f2017-07-19 17:07:10 +0530891 /*
892 * USB Specification 2.0 Section 5.9.2 states that: "If
893 * there is only a single transaction in the microframe,
894 * only a DATA0 data packet PID is used. If there are
895 * two transactions per microframe, DATA1 is used for
896 * the first transaction data packet and DATA0 is used
897 * for the second transaction data packet. If there are
898 * three transactions per microframe, DATA2 is used for
899 * the first transaction data packet, DATA1 is used for
900 * the second, and DATA0 is used for the third."
901 *
902 * IOW, we should satisfy the following cases:
903 *
904 * 1) length <= maxpacket
905 * - DATA0
906 *
907 * 2) maxpacket < length <= (2 * maxpacket)
908 * - DATA1, DATA0
909 *
910 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
911 * - DATA2, DATA1, DATA0
912 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300913 if (speed == USB_SPEED_HIGH) {
914 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530915 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530916 unsigned int maxp = usb_endpoint_maxp(ep->desc);
917
918 if (length <= (2 * maxp))
919 mult--;
920
921 if (length <= maxp)
922 mult--;
923
924 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300925 }
926 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530927 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300928 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200929
930 /* always enable Interrupt on Missed ISOC */
931 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200932 break;
933
934 case USB_ENDPOINT_XFER_BULK:
935 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200936 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200937 break;
938 default:
939 /*
940 * This is only possible with faulty memory because we
941 * checked it already :)
942 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300943 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
944 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200945 }
946
Felipe Balbica4d44e2016-03-10 13:53:27 +0200947 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300948 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300949 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600950
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200951 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300952 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
953 }
954
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200955 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300956 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300957 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200958
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530959 if (chain)
960 trb->ctrl |= DWC3_TRB_CTRL_CHN;
961
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200962 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200963 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200964
965 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500966
967 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200968}
969
John Youn361572b2016-05-19 17:26:17 -0700970/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200971 * dwc3_prepare_one_trb - setup one TRB from one request
972 * @dep: endpoint for which this request is prepared
973 * @req: dwc3_request pointer
974 * @chain: should this TRB be chained to the next?
975 * @node: only for isochronous endpoints. First TRB needs different type.
976 */
977static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
978 struct dwc3_request *req, unsigned chain, unsigned node)
979{
980 struct dwc3_trb *trb;
981 unsigned length = req->request.length;
982 unsigned stream_id = req->request.stream_id;
983 unsigned short_not_ok = req->request.short_not_ok;
984 unsigned no_interrupt = req->request.no_interrupt;
985 dma_addr_t dma = req->request.dma;
986
987 trb = &dep->trb_pool[dep->trb_enqueue];
988
989 if (!req->trb) {
990 dwc3_gadget_move_started_request(req);
991 req->trb = trb;
992 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
993 dep->queued_requests++;
994 }
995
996 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
997 stream_id, short_not_ok, no_interrupt);
998}
999
1000/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001001 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -07001002 * @dep: The endpoint with the TRB ring
1003 * @index: The index of the current TRB in the ring
1004 *
1005 * Returns the TRB prior to the one pointed to by the index. If the
1006 * index is 0, we will wrap backwards, skip the link TRB, and return
1007 * the one just before that.
1008 */
1009static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1010{
Felipe Balbi45438a02016-08-11 12:26:59 +03001011 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001012
Felipe Balbi45438a02016-08-11 12:26:59 +03001013 if (!tmp)
1014 tmp = DWC3_TRB_NUM - 1;
1015
1016 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001017}
1018
Felipe Balbic4233572016-05-12 14:08:34 +03001019static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1020{
1021 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001022 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001023
1024 /*
1025 * If enqueue & dequeue are equal than it is either full or empty.
1026 *
1027 * One way to know for sure is if the TRB right before us has HWO bit
1028 * set or not. If it has, then we're definitely full and can't fit any
1029 * more transfers in our ring.
1030 */
1031 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001032 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001033 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001034 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001035
1036 return DWC3_TRB_NUM - 1;
1037 }
1038
John Youn9d7aba72016-08-26 18:43:01 -07001039 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001040 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001041
John Youn9d7aba72016-08-26 18:43:01 -07001042 if (dep->trb_dequeue < dep->trb_enqueue)
1043 trbs_left--;
1044
John Youn32db3d92016-05-19 17:26:12 -07001045 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001046}
1047
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001048static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001049 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001050{
Felipe Balbi1f512112016-08-12 13:17:27 +03001051 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001052 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001053 int i;
1054
Felipe Balbi1f512112016-08-12 13:17:27 +03001055 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001056 unsigned int length = req->request.length;
1057 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1058 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001059 unsigned chain = true;
1060
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001061 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001062 chain = false;
1063
Felipe Balbic6267a52017-01-05 14:58:46 +02001064 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1065 struct dwc3 *dwc = dep->dwc;
1066 struct dwc3_trb *trb;
1067
1068 req->unaligned = true;
1069
1070 /* prepare normal TRB */
1071 dwc3_prepare_one_trb(dep, req, true, i);
1072
1073 /* Now prepare one extra TRB to align transfer size */
1074 trb = &dep->trb_pool[dep->trb_enqueue];
1075 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1076 maxp - rem, false, 0,
1077 req->request.stream_id,
1078 req->request.short_not_ok,
1079 req->request.no_interrupt);
1080 } else {
1081 dwc3_prepare_one_trb(dep, req, chain, i);
1082 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001083
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001084 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001085 break;
1086 }
1087}
1088
1089static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001090 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001091{
Felipe Balbic6267a52017-01-05 14:58:46 +02001092 unsigned int length = req->request.length;
1093 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1094 unsigned int rem = length % maxp;
1095
1096 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1097 struct dwc3 *dwc = dep->dwc;
1098 struct dwc3_trb *trb;
1099
1100 req->unaligned = true;
1101
1102 /* prepare normal TRB */
1103 dwc3_prepare_one_trb(dep, req, true, 0);
1104
1105 /* Now prepare one extra TRB to align transfer size */
1106 trb = &dep->trb_pool[dep->trb_enqueue];
1107 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1108 false, 0, req->request.stream_id,
1109 req->request.short_not_ok,
1110 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001111 } else if (req->request.zero && req->request.length &&
1112 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1113 struct dwc3 *dwc = dep->dwc;
1114 struct dwc3_trb *trb;
1115
1116 req->zero = true;
1117
1118 /* prepare normal TRB */
1119 dwc3_prepare_one_trb(dep, req, true, 0);
1120
1121 /* Now prepare one extra TRB to handle ZLP */
1122 trb = &dep->trb_pool[dep->trb_enqueue];
1123 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1124 false, 0, req->request.stream_id,
1125 req->request.short_not_ok,
1126 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001127 } else {
1128 dwc3_prepare_one_trb(dep, req, false, 0);
1129 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001130}
1131
Felipe Balbi72246da2011-08-19 18:10:58 +03001132/*
1133 * dwc3_prepare_trbs - setup TRBs from requests
1134 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001135 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001136 * The function goes through the requests list and sets up TRBs for the
1137 * transfers. The function returns once there are no more TRBs available or
1138 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001139 */
Felipe Balbic4233572016-05-12 14:08:34 +03001140static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001141{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001142 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001143
1144 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1145
Felipe Balbid86c5a62016-10-25 13:48:52 +03001146 /*
1147 * We can get in a situation where there's a request in the started list
1148 * but there weren't enough TRBs to fully kick it in the first time
1149 * around, so it has been waiting for more TRBs to be freed up.
1150 *
1151 * In that case, we should check if we have a request with pending_sgs
1152 * in the started list and prepare TRBs for that request first,
1153 * otherwise we will prepare TRBs completely out of order and that will
1154 * break things.
1155 */
1156 list_for_each_entry(req, &dep->started_list, list) {
1157 if (req->num_pending_sgs > 0)
1158 dwc3_prepare_one_trb_sg(dep, req);
1159
1160 if (!dwc3_calc_trbs_left(dep))
1161 return;
1162 }
1163
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001164 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001165 struct dwc3 *dwc = dep->dwc;
1166 int ret;
1167
1168 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1169 dep->direction);
1170 if (ret)
1171 return;
1172
1173 req->sg = req->request.sg;
1174 req->num_pending_sgs = req->request.num_mapped_sgs;
1175
Felipe Balbi1f512112016-08-12 13:17:27 +03001176 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001177 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001178 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001179 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001180
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001181 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001182 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001183 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001184}
1185
Felipe Balbi7fdca762017-09-05 14:41:34 +03001186static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001187{
1188 struct dwc3_gadget_ep_cmd_params params;
1189 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001190 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001191 int ret;
1192 u32 cmd;
1193
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001194 if (!dwc3_calc_trbs_left(dep))
1195 return 0;
1196
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001197 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001198
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001199 dwc3_prepare_trbs(dep);
1200 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001201 if (!req) {
1202 dep->flags |= DWC3_EP_PENDING_REQUEST;
1203 return 0;
1204 }
1205
1206 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001207
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001208 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301209 params.param0 = upper_32_bits(req->trb_dma);
1210 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001211 cmd = DWC3_DEPCMD_STARTTRANSFER;
1212
1213 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1214 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301215 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001216 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1217 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301218 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001219
Felipe Balbi2cd47182016-04-12 16:42:43 +03001220 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001221 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001222 /*
1223 * FIXME we need to iterate over the list of requests
1224 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001225 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001226 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001227 if (req->trb)
1228 memset(req->trb, 0, sizeof(struct dwc3_trb));
Janusz Dziedzic8ab89da2016-11-09 11:01:31 +01001229 dep->queued_requests--;
Felipe Balbi15b8d932016-09-22 10:59:12 +03001230 dwc3_gadget_giveback(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001231 return ret;
1232 }
1233
1234 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001235
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001236 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001237 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001238 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001239 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001240
Felipe Balbi72246da2011-08-19 18:10:58 +03001241 return 0;
1242}
1243
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001244static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1245{
1246 u32 reg;
1247
1248 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1249 return DWC3_DSTS_SOFFN(reg);
1250}
1251
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301252static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1253 struct dwc3_ep *dep, u32 cur_uf)
1254{
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001255 if (list_empty(&dep->pending_list)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001256 dev_info(dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001257 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301258 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301259 return;
1260 }
1261
John Younaf771d72017-01-26 11:58:40 -08001262 /*
1263 * Schedule the first trb for one interval in the future or at
1264 * least 4 microframes.
1265 */
Felipe Balbi502a37b2017-09-05 14:36:13 +03001266 dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001267 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301268}
1269
1270static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1271 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1272{
1273 u32 cur_uf, mask;
1274
1275 mask = ~(dep->interval - 1);
1276 cur_uf = event->parameters & mask;
1277
1278 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1279}
1280
Felipe Balbi72246da2011-08-19 18:10:58 +03001281static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1282{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001283 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001284
Felipe Balbibb423982015-11-16 15:31:21 -06001285 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001286 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1287 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001288 return -ESHUTDOWN;
1289 }
1290
Felipe Balbi04fb3652017-05-17 15:57:45 +03001291 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1292 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001293 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001294
Felipe Balbifc8bb912016-05-16 13:14:48 +03001295 pm_runtime_get(dwc->dev);
1296
Felipe Balbi72246da2011-08-19 18:10:58 +03001297 req->request.actual = 0;
1298 req->request.status = -EINPROGRESS;
1299 req->direction = dep->direction;
1300 req->epnum = dep->number;
1301
Felipe Balbife84f522015-09-01 09:01:38 -05001302 trace_dwc3_ep_queue(req);
1303
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001304 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001305
Felipe Balbid889c232016-09-29 15:44:29 +03001306 /*
1307 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1308 * wait for a XferNotReady event so we will know what's the current
1309 * (micro-)frame number.
1310 *
1311 * Without this trick, we are very, very likely gonna get Bus Expiry
1312 * errors which will force us issue EndTransfer command.
1313 */
1314 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001315 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1316 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1317 dwc3_stop_active_transfer(dwc, dep->number, true);
1318 dep->flags = DWC3_EP_ENABLED;
1319 } else {
1320 u32 cur_uf;
1321
1322 cur_uf = __dwc3_gadget_get_frame(dwc);
1323 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzic87aba102016-11-09 11:01:34 +01001324 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001325 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001326 return 0;
Felipe Balbi08a36b52016-08-11 14:27:52 +03001327 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001328
1329 if ((dep->flags & DWC3_EP_BUSY) &&
Felipe Balbi64e01082017-09-05 14:32:55 +03001330 !(dep->flags & DWC3_EP_MISSED_ISOC))
1331 goto out;
Roger Quadrosf1d68262017-04-21 15:58:08 +03001332
Felipe Balbi64e01082017-09-05 14:32:55 +03001333 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001334 }
1335
Roger Quadrosf1d68262017-04-21 15:58:08 +03001336out:
Felipe Balbi7fdca762017-09-05 14:41:34 +03001337 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001338}
1339
1340static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1341 gfp_t gfp_flags)
1342{
1343 struct dwc3_request *req = to_dwc3_request(request);
1344 struct dwc3_ep *dep = to_dwc3_ep(ep);
1345 struct dwc3 *dwc = dep->dwc;
1346
1347 unsigned long flags;
1348
1349 int ret;
1350
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001351 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 ret = __dwc3_gadget_ep_queue(dep, req);
1353 spin_unlock_irqrestore(&dwc->lock, flags);
1354
1355 return ret;
1356}
1357
1358static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1359 struct usb_request *request)
1360{
1361 struct dwc3_request *req = to_dwc3_request(request);
1362 struct dwc3_request *r = NULL;
1363
1364 struct dwc3_ep *dep = to_dwc3_ep(ep);
1365 struct dwc3 *dwc = dep->dwc;
1366
1367 unsigned long flags;
1368 int ret = 0;
1369
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001370 trace_dwc3_ep_dequeue(req);
1371
Felipe Balbi72246da2011-08-19 18:10:58 +03001372 spin_lock_irqsave(&dwc->lock, flags);
1373
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001374 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 if (r == req)
1376 break;
1377 }
1378
1379 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001380 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001381 if (r == req)
1382 break;
1383 }
1384 if (r == req) {
1385 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001386 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001387
1388 /*
1389 * If request was already started, this means we had to
1390 * stop the transfer. With that we also need to ignore
1391 * all TRBs used by the request, however TRBs can only
1392 * be modified after completion of END_TRANSFER
1393 * command. So what we do here is that we wait for
1394 * END_TRANSFER completion and only after that, we jump
1395 * over TRBs by clearing HWO and incrementing dequeue
1396 * pointer.
1397 *
1398 * Note that we have 2 possible types of transfers here:
1399 *
1400 * i) Linear buffer request
1401 * ii) SG-list based request
1402 *
1403 * SG-list based requests will have r->num_pending_sgs
1404 * set to a valid number (> 0). Linear requests,
1405 * normally use a single TRB.
1406 *
1407 * For each of these two cases, if r->unaligned flag is
1408 * set, one extra TRB has been used to align transfer
1409 * size to wMaxPacketSize.
1410 *
1411 * All of these cases need to be taken into
1412 * consideration so we don't mess up our TRB ring
1413 * pointers.
1414 */
1415 wait_event_lock_irq(dep->wait_end_transfer,
1416 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1417 dwc->lock);
1418
1419 if (!r->trb)
1420 goto out1;
1421
1422 if (r->num_pending_sgs) {
1423 struct dwc3_trb *trb;
1424 int i = 0;
1425
1426 for (i = 0; i < r->num_pending_sgs; i++) {
1427 trb = r->trb + i;
1428 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1429 dwc3_ep_inc_deq(dep);
1430 }
1431
Felipe Balbid6e5a542017-04-07 16:34:38 +03001432 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001433 trb = r->trb + r->num_pending_sgs + 1;
1434 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1435 dwc3_ep_inc_deq(dep);
1436 }
1437 } else {
1438 struct dwc3_trb *trb = r->trb;
1439
1440 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1441 dwc3_ep_inc_deq(dep);
1442
Felipe Balbid6e5a542017-04-07 16:34:38 +03001443 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001444 trb = r->trb + 1;
1445 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1446 dwc3_ep_inc_deq(dep);
1447 }
1448 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301449 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001451 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 request, ep->name);
1453 ret = -EINVAL;
1454 goto out0;
1455 }
1456
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301457out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001458 /* giveback the request */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001459 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1461
1462out0:
1463 spin_unlock_irqrestore(&dwc->lock, flags);
1464
1465 return ret;
1466}
1467
Felipe Balbi7a608552014-09-24 14:19:52 -05001468int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001469{
1470 struct dwc3_gadget_ep_cmd_params params;
1471 struct dwc3 *dwc = dep->dwc;
1472 int ret;
1473
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001474 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1475 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1476 return -EINVAL;
1477 }
1478
Felipe Balbi72246da2011-08-19 18:10:58 +03001479 memset(&params, 0x00, sizeof(params));
1480
1481 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001482 struct dwc3_trb *trb;
1483
1484 unsigned transfer_in_flight;
1485 unsigned started;
1486
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001487 if (dep->flags & DWC3_EP_STALL)
1488 return 0;
1489
Felipe Balbi69450c42016-05-30 13:37:02 +03001490 if (dep->number > 1)
1491 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1492 else
1493 trb = &dwc->ep0_trb[dep->trb_enqueue];
1494
1495 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1496 started = !list_empty(&dep->started_list);
1497
1498 if (!protocol && ((dep->direction && transfer_in_flight) ||
1499 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001500 return -EAGAIN;
1501 }
1502
Felipe Balbi2cd47182016-04-12 16:42:43 +03001503 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1504 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001505 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001506 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001507 dep->name);
1508 else
1509 dep->flags |= DWC3_EP_STALL;
1510 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001511 if (!(dep->flags & DWC3_EP_STALL))
1512 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001513
John Youn50c763f2016-05-31 17:49:56 -07001514 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001516 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001517 dep->name);
1518 else
Alan Sterna535d812013-11-01 12:05:12 -04001519 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001520 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001521
Felipe Balbi72246da2011-08-19 18:10:58 +03001522 return ret;
1523}
1524
1525static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1526{
1527 struct dwc3_ep *dep = to_dwc3_ep(ep);
1528 struct dwc3 *dwc = dep->dwc;
1529
1530 unsigned long flags;
1531
1532 int ret;
1533
1534 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001535 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 spin_unlock_irqrestore(&dwc->lock, flags);
1537
1538 return ret;
1539}
1540
1541static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1542{
1543 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001544 struct dwc3 *dwc = dep->dwc;
1545 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001546 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001547
Paul Zimmerman249a4562012-02-24 17:32:16 -08001548 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001549 dep->flags |= DWC3_EP_WEDGE;
1550
Pratyush Anand08f0d962012-06-25 22:40:43 +05301551 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001552 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301553 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001554 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001555 spin_unlock_irqrestore(&dwc->lock, flags);
1556
1557 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558}
1559
1560/* -------------------------------------------------------------------------- */
1561
1562static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1563 .bLength = USB_DT_ENDPOINT_SIZE,
1564 .bDescriptorType = USB_DT_ENDPOINT,
1565 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1566};
1567
1568static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1569 .enable = dwc3_gadget_ep0_enable,
1570 .disable = dwc3_gadget_ep0_disable,
1571 .alloc_request = dwc3_gadget_ep_alloc_request,
1572 .free_request = dwc3_gadget_ep_free_request,
1573 .queue = dwc3_gadget_ep0_queue,
1574 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301575 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001576 .set_wedge = dwc3_gadget_ep_set_wedge,
1577};
1578
1579static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1580 .enable = dwc3_gadget_ep_enable,
1581 .disable = dwc3_gadget_ep_disable,
1582 .alloc_request = dwc3_gadget_ep_alloc_request,
1583 .free_request = dwc3_gadget_ep_free_request,
1584 .queue = dwc3_gadget_ep_queue,
1585 .dequeue = dwc3_gadget_ep_dequeue,
1586 .set_halt = dwc3_gadget_ep_set_halt,
1587 .set_wedge = dwc3_gadget_ep_set_wedge,
1588};
1589
1590/* -------------------------------------------------------------------------- */
1591
1592static int dwc3_gadget_get_frame(struct usb_gadget *g)
1593{
1594 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001595
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001596 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001597}
1598
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001599static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001600{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001601 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001602
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001603 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001604 u32 reg;
1605
Felipe Balbi72246da2011-08-19 18:10:58 +03001606 u8 link_state;
1607 u8 speed;
1608
Felipe Balbi72246da2011-08-19 18:10:58 +03001609 /*
1610 * According to the Databook Remote wakeup request should
1611 * be issued only when the device is in early suspend state.
1612 *
1613 * We can check that via USB Link State bits in DSTS register.
1614 */
1615 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1616
1617 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001618 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001619 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001620 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001621
1622 link_state = DWC3_DSTS_USBLNKST(reg);
1623
1624 switch (link_state) {
1625 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1626 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1627 break;
1628 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001629 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 }
1631
Felipe Balbi8598bde2012-01-02 18:55:57 +02001632 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1633 if (ret < 0) {
1634 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001635 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001636 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001637
Paul Zimmerman802fde92012-04-27 13:10:52 +03001638 /* Recent versions do this automatically */
1639 if (dwc->revision < DWC3_REVISION_194A) {
1640 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001641 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001642 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1643 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1644 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001645
Paul Zimmerman1d046792012-02-15 18:56:56 -08001646 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001647 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001648
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001649 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1651
1652 /* in HS, means ON */
1653 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1654 break;
1655 }
1656
1657 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1658 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001659 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 }
1661
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001662 return 0;
1663}
1664
1665static int dwc3_gadget_wakeup(struct usb_gadget *g)
1666{
1667 struct dwc3 *dwc = gadget_to_dwc(g);
1668 unsigned long flags;
1669 int ret;
1670
1671 spin_lock_irqsave(&dwc->lock, flags);
1672 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 spin_unlock_irqrestore(&dwc->lock, flags);
1674
1675 return ret;
1676}
1677
1678static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1679 int is_selfpowered)
1680{
1681 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001682 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001683
Paul Zimmerman249a4562012-02-24 17:32:16 -08001684 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001685 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001686 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001687
1688 return 0;
1689}
1690
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001691static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001692{
1693 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001694 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001695
Felipe Balbifc8bb912016-05-16 13:14:48 +03001696 if (pm_runtime_suspended(dwc->dev))
1697 return 0;
1698
Felipe Balbi72246da2011-08-19 18:10:58 +03001699 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001700 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001701 if (dwc->revision <= DWC3_REVISION_187A) {
1702 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1703 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1704 }
1705
1706 if (dwc->revision >= DWC3_REVISION_194A)
1707 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1708 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001709
1710 if (dwc->has_hibernation)
1711 reg |= DWC3_DCTL_KEEP_CONNECT;
1712
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001713 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001714 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001716
1717 if (dwc->has_hibernation && !suspend)
1718 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1719
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001720 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001721 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001722
1723 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1724
1725 do {
1726 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001727 reg &= DWC3_DSTS_DEVCTRLHLT;
1728 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001729
1730 if (!timeout)
1731 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001732
Pratyush Anand6f17f742012-07-02 10:21:55 +05301733 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001734}
1735
1736static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1737{
1738 struct dwc3 *dwc = gadget_to_dwc(g);
1739 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301740 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741
1742 is_on = !!is_on;
1743
Baolin Wangbb014732016-10-14 17:11:33 +08001744 /*
1745 * Per databook, when we want to stop the gadget, if a control transfer
1746 * is still in process, complete it and get the core into setup phase.
1747 */
1748 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1749 reinit_completion(&dwc->ep0_in_setup);
1750
1751 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1752 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1753 if (ret == 0) {
1754 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1755 return -ETIMEDOUT;
1756 }
1757 }
1758
Felipe Balbi72246da2011-08-19 18:10:58 +03001759 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001760 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001761 spin_unlock_irqrestore(&dwc->lock, flags);
1762
Pratyush Anand6f17f742012-07-02 10:21:55 +05301763 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001764}
1765
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001766static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1767{
1768 u32 reg;
1769
1770 /* Enable all but Start and End of Frame IRQs */
1771 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1772 DWC3_DEVTEN_EVNTOVERFLOWEN |
1773 DWC3_DEVTEN_CMDCMPLTEN |
1774 DWC3_DEVTEN_ERRTICERREN |
1775 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001776 DWC3_DEVTEN_CONNECTDONEEN |
1777 DWC3_DEVTEN_USBRSTEN |
1778 DWC3_DEVTEN_DISCONNEVTEN);
1779
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001780 if (dwc->revision < DWC3_REVISION_250A)
1781 reg |= DWC3_DEVTEN_ULSTCNGEN;
1782
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001783 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1784}
1785
1786static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1787{
1788 /* mask all interrupts */
1789 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1790}
1791
1792static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001793static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001794
Felipe Balbi4e994722016-05-13 14:09:59 +03001795/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001796 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1797 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001798 *
1799 * The following looks like complex but it's actually very simple. In order to
1800 * calculate the number of packets we can burst at once on OUT transfers, we're
1801 * gonna use RxFIFO size.
1802 *
1803 * To calculate RxFIFO size we need two numbers:
1804 * MDWIDTH = size, in bits, of the internal memory bus
1805 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1806 *
1807 * Given these two numbers, the formula is simple:
1808 *
1809 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1810 *
1811 * 24 bytes is for 3x SETUP packets
1812 * 16 bytes is a clock domain crossing tolerance
1813 *
1814 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1815 */
1816static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1817{
1818 u32 ram2_depth;
1819 u32 mdwidth;
1820 u32 nump;
1821 u32 reg;
1822
1823 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1824 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1825
1826 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1827 nump = min_t(u32, nump, 16);
1828
1829 /* update NumP */
1830 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1831 reg &= ~DWC3_DCFG_NUMP_MASK;
1832 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1833 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1834}
1835
Felipe Balbid7be2952016-05-04 15:49:37 +03001836static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001837{
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001839 int ret = 0;
1840 u32 reg;
1841
John Youncf40b862016-11-14 12:32:43 -08001842 /*
1843 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1844 * the core supports IMOD, disable it.
1845 */
1846 if (dwc->imod_interval) {
1847 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1848 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1849 } else if (dwc3_has_imod(dwc)) {
1850 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1851 }
1852
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001853 /*
1854 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1855 * field instead of letting dwc3 itself calculate that automatically.
1856 *
1857 * This way, we maximize the chances that we'll be able to get several
1858 * bursts of data without going through any sort of endpoint throttling.
1859 */
1860 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1861 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1862 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1863
Felipe Balbi4e994722016-05-13 14:09:59 +03001864 dwc3_gadget_setup_nump(dwc);
1865
Felipe Balbi72246da2011-08-19 18:10:58 +03001866 /* Start with SuperSpeed Default */
1867 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1868
1869 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001870 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001871 if (ret) {
1872 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001873 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001874 }
1875
1876 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001877 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 if (ret) {
1879 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001880 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001881 }
1882
1883 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001884 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 dwc3_ep0_out_start(dwc);
1886
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001887 dwc3_gadget_enable_irq(dwc);
1888
Felipe Balbid7be2952016-05-04 15:49:37 +03001889 return 0;
1890
1891err1:
1892 __dwc3_gadget_ep_disable(dwc->eps[0]);
1893
1894err0:
1895 return ret;
1896}
1897
1898static int dwc3_gadget_start(struct usb_gadget *g,
1899 struct usb_gadget_driver *driver)
1900{
1901 struct dwc3 *dwc = gadget_to_dwc(g);
1902 unsigned long flags;
1903 int ret = 0;
1904 int irq;
1905
Roger Quadros9522def2016-06-10 14:48:38 +03001906 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001907 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1908 IRQF_SHARED, "dwc3", dwc->ev_buf);
1909 if (ret) {
1910 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1911 irq, ret);
1912 goto err0;
1913 }
1914
1915 spin_lock_irqsave(&dwc->lock, flags);
1916 if (dwc->gadget_driver) {
1917 dev_err(dwc->dev, "%s is already bound to %s\n",
1918 dwc->gadget.name,
1919 dwc->gadget_driver->driver.name);
1920 ret = -EBUSY;
1921 goto err1;
1922 }
1923
1924 dwc->gadget_driver = driver;
1925
Felipe Balbifc8bb912016-05-16 13:14:48 +03001926 if (pm_runtime_active(dwc->dev))
1927 __dwc3_gadget_start(dwc);
1928
Felipe Balbi72246da2011-08-19 18:10:58 +03001929 spin_unlock_irqrestore(&dwc->lock, flags);
1930
1931 return 0;
1932
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001933err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001934 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001935 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001936
1937err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001938 return ret;
1939}
1940
Felipe Balbid7be2952016-05-04 15:49:37 +03001941static void __dwc3_gadget_stop(struct dwc3 *dwc)
1942{
1943 dwc3_gadget_disable_irq(dwc);
1944 __dwc3_gadget_ep_disable(dwc->eps[0]);
1945 __dwc3_gadget_ep_disable(dwc->eps[1]);
1946}
1947
Felipe Balbi22835b82014-10-17 12:05:12 -05001948static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001949{
1950 struct dwc3 *dwc = gadget_to_dwc(g);
1951 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001952 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02001953 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001954
1955 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001956
1957 if (pm_runtime_suspended(dwc->dev))
1958 goto out;
1959
Felipe Balbid7be2952016-05-04 15:49:37 +03001960 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001961
1962 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1963 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02001964 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08001965
1966 if (!dep)
1967 continue;
1968
1969 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1970 continue;
1971
Roger Quadros498f0472018-03-09 14:47:04 +02001972 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
1973 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1974 dwc->lock, msecs_to_jiffies(5));
1975
1976 if (ret <= 0) {
1977 /* Timed out or interrupted! There's nothing much
1978 * we can do so we just log here and print which
1979 * endpoints timed out at the end.
1980 */
1981 tmo_eps |= 1 << epnum;
1982 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
1983 }
1984 }
1985
1986 if (tmo_eps) {
1987 dev_err(dwc->dev,
1988 "end transfer timed out on endpoints 0x%x [bitmap]\n",
1989 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08001990 }
1991
1992out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001993 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001994 spin_unlock_irqrestore(&dwc->lock, flags);
1995
Felipe Balbi3f308d12016-05-16 14:17:06 +03001996 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001997
Felipe Balbi72246da2011-08-19 18:10:58 +03001998 return 0;
1999}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002000
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002001static void dwc3_gadget_set_speed(struct usb_gadget *g,
2002 enum usb_device_speed speed)
2003{
2004 struct dwc3 *dwc = gadget_to_dwc(g);
2005 unsigned long flags;
2006 u32 reg;
2007
2008 spin_lock_irqsave(&dwc->lock, flags);
2009 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2010 reg &= ~(DWC3_DCFG_SPEED_MASK);
2011
2012 /*
2013 * WORKAROUND: DWC3 revision < 2.20a have an issue
2014 * which would cause metastability state on Run/Stop
2015 * bit if we try to force the IP to USB2-only mode.
2016 *
2017 * Because of that, we cannot configure the IP to any
2018 * speed other than the SuperSpeed
2019 *
2020 * Refers to:
2021 *
2022 * STAR#9000525659: Clock Domain Crossing on DCTL in
2023 * USB 2.0 Mode
2024 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002025 if (dwc->revision < DWC3_REVISION_220A &&
2026 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002027 reg |= DWC3_DCFG_SUPERSPEED;
2028 } else {
2029 switch (speed) {
2030 case USB_SPEED_LOW:
2031 reg |= DWC3_DCFG_LOWSPEED;
2032 break;
2033 case USB_SPEED_FULL:
2034 reg |= DWC3_DCFG_FULLSPEED;
2035 break;
2036 case USB_SPEED_HIGH:
2037 reg |= DWC3_DCFG_HIGHSPEED;
2038 break;
2039 case USB_SPEED_SUPER:
2040 reg |= DWC3_DCFG_SUPERSPEED;
2041 break;
2042 case USB_SPEED_SUPER_PLUS:
2043 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2044 break;
2045 default:
2046 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2047
2048 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2049 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2050 else
2051 reg |= DWC3_DCFG_SUPERSPEED;
2052 }
2053 }
2054 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2055
2056 spin_unlock_irqrestore(&dwc->lock, flags);
2057}
2058
Felipe Balbi72246da2011-08-19 18:10:58 +03002059static const struct usb_gadget_ops dwc3_gadget_ops = {
2060 .get_frame = dwc3_gadget_get_frame,
2061 .wakeup = dwc3_gadget_wakeup,
2062 .set_selfpowered = dwc3_gadget_set_selfpowered,
2063 .pullup = dwc3_gadget_pullup,
2064 .udc_start = dwc3_gadget_start,
2065 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002066 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002067};
2068
2069/* -------------------------------------------------------------------------- */
2070
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002071static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
Felipe Balbi72246da2011-08-19 18:10:58 +03002072{
2073 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002074 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002075
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002076 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2077
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002078 for (epnum = 0; epnum < total; epnum++) {
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002079 bool direction = epnum & 1;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002080 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002081
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002083 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002084 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002085
2086 dep->dwc = dwc;
2087 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002088 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002089 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002090 dwc->eps[epnum] = dep;
2091
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002092 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002093 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002094
Felipe Balbi72246da2011-08-19 18:10:58 +03002095 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002096
2097 if (!(dep->number > 1)) {
2098 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2099 dep->endpoint.comp_desc = NULL;
2100 }
2101
Felipe Balbi74674cb2016-04-13 16:44:39 +03002102 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002103
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002104 if (num == 0) {
Robert Baldygae117e742013-12-13 12:23:38 +01002105 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302106 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002107 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002108 if (!direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002109 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002110 } else if (direction) {
2111 int mdwidth;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002112 int kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002113 int size;
2114 int ret;
Felipe Balbi28781782017-01-23 18:01:59 +02002115
2116 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2117 /* MDWIDTH is represented in bits, we need it in bytes */
2118 mdwidth /= 8;
2119
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002120 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
Thinh Nguyend548a612018-03-16 15:34:00 -07002121 if (dwc3_is_usb31(dwc))
2122 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2123 else
2124 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
Felipe Balbi28781782017-01-23 18:01:59 +02002125
2126 /* FIFO Depth is in MDWDITH bytes. Multiply */
2127 size *= mdwidth;
2128
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002129 kbytes = size / 1024;
2130 if (kbytes == 0)
2131 kbytes = 1;
Felipe Balbi28781782017-01-23 18:01:59 +02002132
2133 /*
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002134 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
Felipe Balbi28781782017-01-23 18:01:59 +02002135 * internal overhead. We don't really know how these are used,
2136 * but documentation say it exists.
2137 */
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002138 size -= mdwidth * (kbytes + 1);
2139 size /= kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002140
2141 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2142
2143 dep->endpoint.max_streams = 15;
2144 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2145 list_add_tail(&dep->endpoint.ep_list,
2146 &dwc->gadget.ep_list);
2147
2148 ret = dwc3_alloc_trb_pool(dep);
2149 if (ret)
2150 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 } else {
2152 int ret;
2153
Robert Baldygae117e742013-12-13 12:23:38 +01002154 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002155 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2157 list_add_tail(&dep->endpoint.ep_list,
2158 &dwc->gadget.ep_list);
2159
2160 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002161 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002162 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002163 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002164
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002165 if (num == 0) {
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002166 dep->endpoint.caps.type_control = true;
2167 } else {
2168 dep->endpoint.caps.type_iso = true;
2169 dep->endpoint.caps.type_bulk = true;
2170 dep->endpoint.caps.type_int = true;
2171 }
2172
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002173 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002174 dep->endpoint.caps.dir_out = !direction;
2175
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002176 INIT_LIST_HEAD(&dep->pending_list);
2177 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002178 }
2179
2180 return 0;
2181}
2182
2183static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2184{
2185 struct dwc3_ep *dep;
2186 u8 epnum;
2187
2188 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2189 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002190 if (!dep)
2191 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302192 /*
2193 * Physical endpoints 0 and 1 are special; they form the
2194 * bi-directional USB endpoint 0.
2195 *
2196 * For those two physical endpoints, we don't allocate a TRB
2197 * pool nor do we add them the endpoints list. Due to that, we
2198 * shouldn't do these two operations otherwise we would end up
2199 * with all sorts of bugs when removing dwc3.ko.
2200 */
2201 if (epnum != 0 && epnum != 1) {
2202 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002203 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302204 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002205
2206 kfree(dep);
2207 }
2208}
2209
Felipe Balbi72246da2011-08-19 18:10:58 +03002210/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002211
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302212static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2213 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002214 const struct dwc3_event_depevt *event, int status,
2215 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302216{
2217 unsigned int count;
2218 unsigned int s_pkt = 0;
2219 unsigned int trb_status;
2220
Felipe Balbidc55c672016-08-12 13:20:32 +03002221 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002222
2223 if (req->trb == trb)
2224 dep->queued_requests--;
2225
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002226 trace_dwc3_complete_trb(dep, trb);
2227
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002228 /*
2229 * If we're in the middle of series of chained TRBs and we
2230 * receive a short transfer along the way, DWC3 will skip
2231 * through all TRBs including the last TRB in the chain (the
2232 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2233 * bit and SW has to do it manually.
2234 *
2235 * We're going to do that here to avoid problems of HW trying
2236 * to use bogus TRBs for transfers.
2237 */
2238 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2239 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2240
Felipe Balbic6267a52017-01-05 14:58:46 +02002241 /*
2242 * If we're dealing with unaligned size OUT transfer, we will be left
2243 * with one TRB pending in the ring. We need to manually clear HWO bit
2244 * from that TRB.
2245 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002246 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002247 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2248 return 1;
2249 }
2250
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302251 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002252 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302253
Felipe Balbi35b27192017-03-08 13:56:37 +02002254 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2255 return 1;
2256
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302257 if (dep->direction) {
2258 if (count) {
2259 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2260 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302261 /*
2262 * If missed isoc occurred and there is
2263 * no request queued then issue END
2264 * TRANSFER, so that core generates
2265 * next xfernotready and we will issue
2266 * a fresh START TRANSFER.
2267 * If there are still queued request
2268 * then wait, do not issue either END
2269 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002270 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302271 * giveback.If any future queued request
2272 * is successfully transferred then we
2273 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002274 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302275 */
2276 dep->flags |= DWC3_EP_MISSED_ISOC;
2277 } else {
2278 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2279 dep->name);
2280 status = -ECONNRESET;
2281 }
2282 } else {
2283 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2284 }
2285 } else {
2286 if (count && (event->status & DEPEVT_STATUS_SHORT))
2287 s_pkt = 1;
2288 }
2289
Felipe Balbi7c705df2016-08-10 12:35:30 +03002290 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302291 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002292
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302293 if ((event->status & DEPEVT_STATUS_IOC) &&
2294 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2295 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002296
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302297 return 0;
2298}
2299
Felipe Balbi72246da2011-08-19 18:10:58 +03002300static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2301 const struct dwc3_event_depevt *event, int status)
2302{
Felipe Balbi31162af2016-08-11 14:38:37 +03002303 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002304 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002305 bool ioc = false;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002306 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002307
Felipe Balbi31162af2016-08-11 14:38:37 +03002308 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002309 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002310 int chain;
2311
Felipe Balbi1f512112016-08-12 13:17:27 +03002312 length = req->request.length;
2313 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002314 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002315 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002316 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002317 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002318 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002319
Felipe Balbi1f512112016-08-12 13:17:27 +03002320 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002321 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002322
Felipe Balbi7282c4e2016-10-25 13:50:46 +03002323 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2324 break;
2325
Felipe Balbi1f512112016-08-12 13:17:27 +03002326 req->sg = sg_next(s);
2327 req->num_pending_sgs--;
2328
Felipe Balbi31162af2016-08-11 14:38:37 +03002329 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2330 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002331 if (ret)
2332 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002333 }
2334 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002335 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002336 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002337 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002338 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002339
Felipe Balbid6e5a542017-04-07 16:34:38 +03002340 if (req->unaligned || req->zero) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002341 trb = &dep->trb_pool[dep->trb_dequeue];
2342 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2343 event, status, false);
2344 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002345 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002346 }
2347
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002348 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002349
Felipe Balbiff377ae2016-10-25 13:54:00 +03002350 if ((req->request.actual < length) && req->num_pending_sgs)
Felipe Balbi7fdca762017-09-05 14:41:34 +03002351 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi1f512112016-08-12 13:17:27 +03002352
Ville Syrjäläd115d702015-08-31 19:48:28 +03002353 dwc3_gadget_giveback(dep, req, status);
2354
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002355 if (ret) {
2356 if ((event->status & DEPEVT_STATUS_IOC) &&
2357 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2358 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002360 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002361 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002362
Felipe Balbi4cb42212016-05-18 12:37:21 +03002363 /*
2364 * Our endpoint might get disabled by another thread during
2365 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2366 * early on so DWC3_EP_BUSY flag gets cleared
2367 */
2368 if (!dep->endpoint.desc)
2369 return 1;
2370
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302371 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002372 list_empty(&dep->started_list)) {
2373 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302374 /*
2375 * If there is no entry in request list then do
2376 * not issue END TRANSFER now. Just set PENDING
2377 * flag, so that END TRANSFER is issued when an
2378 * entry is added into request list.
2379 */
2380 dep->flags = DWC3_EP_PENDING_REQUEST;
2381 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002382 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302383 dep->flags = DWC3_EP_ENABLED;
2384 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302385 return 1;
2386 }
2387
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002388 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2389 return 0;
2390
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 return 1;
2392}
2393
2394static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002395 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002396{
2397 unsigned status = 0;
2398 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002399 u32 is_xfer_complete;
2400
2401 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002402
2403 if (event->status & DEPEVT_STATUS_BUSERR)
2404 status = -ECONNRESET;
2405
Paul Zimmerman1d046792012-02-15 18:56:56 -08002406 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002407 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002408 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002409 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002410
2411 /*
2412 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2413 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2414 */
2415 if (dwc->revision < DWC3_REVISION_183A) {
2416 u32 reg;
2417 int i;
2418
2419 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002420 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002421
2422 if (!(dep->flags & DWC3_EP_ENABLED))
2423 continue;
2424
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002425 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002426 return;
2427 }
2428
2429 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2430 reg |= dwc->u1u2;
2431 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2432
2433 dwc->u1u2 = 0;
2434 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002435
Felipe Balbi4cb42212016-05-18 12:37:21 +03002436 /*
2437 * Our endpoint might get disabled by another thread during
2438 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2439 * early on so DWC3_EP_BUSY flag gets cleared
2440 */
2441 if (!dep->endpoint.desc)
2442 return;
2443
Felipe Balbi7fdca762017-09-05 14:41:34 +03002444 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
2445 __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002446}
2447
Felipe Balbi72246da2011-08-19 18:10:58 +03002448static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2449 const struct dwc3_event_depevt *event)
2450{
2451 struct dwc3_ep *dep;
2452 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002453 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002454
2455 dep = dwc->eps[epnum];
2456
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002457 if (!(dep->flags & DWC3_EP_ENABLED)) {
2458 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2459 return;
2460
2461 /* Handle only EPCMDCMPLT when EP disabled */
2462 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2463 return;
2464 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002465
Felipe Balbi72246da2011-08-19 18:10:58 +03002466 if (epnum == 0 || epnum == 1) {
2467 dwc3_ep0_interrupt(dwc, event);
2468 return;
2469 }
2470
2471 switch (event->endpoint_event) {
2472 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002473 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002474
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002475 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002476 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002477 return;
2478 }
2479
Jingoo Han029d97f2014-07-04 15:00:51 +09002480 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002481 break;
2482 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002483 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002484 break;
2485 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi7fdca762017-09-05 14:41:34 +03002486 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 dwc3_gadget_start_isoc(dwc, dep, event);
Felipe Balbi7fdca762017-09-05 14:41:34 +03002488 else
2489 __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002490
2491 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002492 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002493 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002494 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2495 dep->name);
2496 return;
2497 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002498 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002499 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002500 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2501
2502 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2503 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2504 wake_up(&dep->wait_end_transfer);
2505 }
2506 break;
2507 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 break;
2509 }
2510}
2511
2512static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2513{
2514 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2515 spin_unlock(&dwc->lock);
2516 dwc->gadget_driver->disconnect(&dwc->gadget);
2517 spin_lock(&dwc->lock);
2518 }
2519}
2520
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002521static void dwc3_suspend_gadget(struct dwc3 *dwc)
2522{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002523 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002524 spin_unlock(&dwc->lock);
2525 dwc->gadget_driver->suspend(&dwc->gadget);
2526 spin_lock(&dwc->lock);
2527 }
2528}
2529
2530static void dwc3_resume_gadget(struct dwc3 *dwc)
2531{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002532 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002533 spin_unlock(&dwc->lock);
2534 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002535 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002536 }
2537}
2538
2539static void dwc3_reset_gadget(struct dwc3 *dwc)
2540{
2541 if (!dwc->gadget_driver)
2542 return;
2543
2544 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2545 spin_unlock(&dwc->lock);
2546 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002547 spin_lock(&dwc->lock);
2548 }
2549}
2550
Paul Zimmermanb992e682012-04-27 14:17:35 +03002551static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002552{
2553 struct dwc3_ep *dep;
2554 struct dwc3_gadget_ep_cmd_params params;
2555 u32 cmd;
2556 int ret;
2557
2558 dep = dwc->eps[epnum];
2559
Baolin Wang76a638f2016-10-31 19:38:36 +08002560 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2561 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302562 return;
2563
Pratyush Anand57911502012-07-06 15:19:10 +05302564 /*
2565 * NOTICE: We are violating what the Databook says about the
2566 * EndTransfer command. Ideally we would _always_ wait for the
2567 * EndTransfer Command Completion IRQ, but that's causing too
2568 * much trouble synchronizing between us and gadget driver.
2569 *
2570 * We have discussed this with the IP Provider and it was
2571 * suggested to giveback all requests here, but give HW some
2572 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002573 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302574 *
2575 * Note also that a similar handling was tested by Synopsys
2576 * (thanks a lot Paul) and nothing bad has come out of it.
2577 * In short, what we're doing is:
2578 *
2579 * - Issue EndTransfer WITH CMDIOC bit set
2580 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002581 *
2582 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2583 * supports a mode to work around the above limitation. The
2584 * software can poll the CMDACT bit in the DEPCMD register
2585 * after issuing a EndTransfer command. This mode is enabled
2586 * by writing GUCTL2[14]. This polling is already done in the
2587 * dwc3_send_gadget_ep_cmd() function so if the mode is
2588 * enabled, the EndTransfer command will have completed upon
2589 * returning from this function and we don't need to delay for
2590 * 100us.
2591 *
2592 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302593 */
2594
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302595 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002596 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2597 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002598 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302599 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002600 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302601 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002602 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002603 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002604
Baolin Wang76a638f2016-10-31 19:38:36 +08002605 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2606 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002607 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002608 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002609}
2610
Felipe Balbi72246da2011-08-19 18:10:58 +03002611static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2612{
2613 u32 epnum;
2614
2615 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2616 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002617 int ret;
2618
2619 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002620 if (!dep)
2621 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002622
2623 if (!(dep->flags & DWC3_EP_STALL))
2624 continue;
2625
2626 dep->flags &= ~DWC3_EP_STALL;
2627
John Youn50c763f2016-05-31 17:49:56 -07002628 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002629 WARN_ON_ONCE(ret);
2630 }
2631}
2632
2633static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2634{
Felipe Balbic4430a22012-05-24 10:30:01 +03002635 int reg;
2636
Felipe Balbi72246da2011-08-19 18:10:58 +03002637 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2638 reg &= ~DWC3_DCTL_INITU1ENA;
2639 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2640
2641 reg &= ~DWC3_DCTL_INITU2ENA;
2642 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002643
Felipe Balbi72246da2011-08-19 18:10:58 +03002644 dwc3_disconnect_gadget(dwc);
2645
2646 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002647 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002648 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002649
2650 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002651}
2652
Felipe Balbi72246da2011-08-19 18:10:58 +03002653static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2654{
2655 u32 reg;
2656
Felipe Balbifc8bb912016-05-16 13:14:48 +03002657 dwc->connected = true;
2658
Felipe Balbidf62df52011-10-14 15:11:49 +03002659 /*
2660 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2661 * would cause a missing Disconnect Event if there's a
2662 * pending Setup Packet in the FIFO.
2663 *
2664 * There's no suggested workaround on the official Bug
2665 * report, which states that "unless the driver/application
2666 * is doing any special handling of a disconnect event,
2667 * there is no functional issue".
2668 *
2669 * Unfortunately, it turns out that we _do_ some special
2670 * handling of a disconnect event, namely complete all
2671 * pending transfers, notify gadget driver of the
2672 * disconnection, and so on.
2673 *
2674 * Our suggested workaround is to follow the Disconnect
2675 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002676 * flag. Such flag gets set whenever we have a SETUP_PENDING
2677 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002678 * same endpoint.
2679 *
2680 * Refers to:
2681 *
2682 * STAR#9000466709: RTL: Device : Disconnect event not
2683 * generated if setup packet pending in FIFO
2684 */
2685 if (dwc->revision < DWC3_REVISION_188A) {
2686 if (dwc->setup_packet_pending)
2687 dwc3_gadget_disconnect_interrupt(dwc);
2688 }
2689
Felipe Balbi8e744752014-11-06 14:27:53 +08002690 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002691
2692 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2693 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2694 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002695 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002696 dwc3_clear_stall_all_ep(dwc);
2697
2698 /* Reset device address to zero */
2699 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2700 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2701 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002702}
2703
Felipe Balbi72246da2011-08-19 18:10:58 +03002704static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2705{
Felipe Balbi72246da2011-08-19 18:10:58 +03002706 struct dwc3_ep *dep;
2707 int ret;
2708 u32 reg;
2709 u8 speed;
2710
Felipe Balbi72246da2011-08-19 18:10:58 +03002711 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2712 speed = reg & DWC3_DSTS_CONNECTSPD;
2713 dwc->speed = speed;
2714
John Youn5fb6fda2016-11-10 17:23:25 -08002715 /*
2716 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2717 * each time on Connect Done.
2718 *
2719 * Currently we always use the reset value. If any platform
2720 * wants to set this to a different value, we need to add a
2721 * setting and update GCTL.RAMCLKSEL here.
2722 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002723
2724 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002725 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002726 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2727 dwc->gadget.ep0->maxpacket = 512;
2728 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2729 break;
John Youn2da9ad72016-05-20 16:34:26 -07002730 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002731 /*
2732 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2733 * would cause a missing USB3 Reset event.
2734 *
2735 * In such situations, we should force a USB3 Reset
2736 * event by calling our dwc3_gadget_reset_interrupt()
2737 * routine.
2738 *
2739 * Refers to:
2740 *
2741 * STAR#9000483510: RTL: SS : USB3 reset event may
2742 * not be generated always when the link enters poll
2743 */
2744 if (dwc->revision < DWC3_REVISION_190A)
2745 dwc3_gadget_reset_interrupt(dwc);
2746
Felipe Balbi72246da2011-08-19 18:10:58 +03002747 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2748 dwc->gadget.ep0->maxpacket = 512;
2749 dwc->gadget.speed = USB_SPEED_SUPER;
2750 break;
John Youn2da9ad72016-05-20 16:34:26 -07002751 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002752 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2753 dwc->gadget.ep0->maxpacket = 64;
2754 dwc->gadget.speed = USB_SPEED_HIGH;
2755 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002756 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2758 dwc->gadget.ep0->maxpacket = 64;
2759 dwc->gadget.speed = USB_SPEED_FULL;
2760 break;
John Youn2da9ad72016-05-20 16:34:26 -07002761 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002762 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2763 dwc->gadget.ep0->maxpacket = 8;
2764 dwc->gadget.speed = USB_SPEED_LOW;
2765 break;
2766 }
2767
Thinh Nguyen61800262018-01-12 18:18:05 -08002768 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2769
Pratyush Anand2b758352013-01-14 15:59:31 +05302770 /* Enable USB2 LPM Capability */
2771
John Younee5cd412016-02-05 17:08:45 -08002772 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002773 (speed != DWC3_DSTS_SUPERSPEED) &&
2774 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302775 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2776 reg |= DWC3_DCFG_LPM_CAP;
2777 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2778
2779 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2780 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2781
Huang Rui460d0982014-10-31 11:11:18 +08002782 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302783
Huang Rui80caf7d2014-10-28 19:54:26 +08002784 /*
2785 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2786 * DCFG.LPMCap is set, core responses with an ACK and the
2787 * BESL value in the LPM token is less than or equal to LPM
2788 * NYET threshold.
2789 */
2790 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2791 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002792 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002793
2794 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2795 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2796
Pratyush Anand2b758352013-01-14 15:59:31 +05302797 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002798 } else {
2799 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2800 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2801 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302802 }
2803
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002805 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002806 if (ret) {
2807 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2808 return;
2809 }
2810
2811 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002812 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002813 if (ret) {
2814 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2815 return;
2816 }
2817
2818 /*
2819 * Configure PHY via GUSB3PIPECTLn if required.
2820 *
2821 * Update GTXFIFOSIZn
2822 *
2823 * In both cases reset values should be sufficient.
2824 */
2825}
2826
2827static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2828{
Felipe Balbi72246da2011-08-19 18:10:58 +03002829 /*
2830 * TODO take core out of low power mode when that's
2831 * implemented.
2832 */
2833
Jiebing Liad14d4e2014-12-11 13:26:29 +08002834 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2835 spin_unlock(&dwc->lock);
2836 dwc->gadget_driver->resume(&dwc->gadget);
2837 spin_lock(&dwc->lock);
2838 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002839}
2840
2841static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2842 unsigned int evtinfo)
2843{
Felipe Balbifae2b902011-10-14 13:00:30 +03002844 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002845 unsigned int pwropt;
2846
2847 /*
2848 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2849 * Hibernation mode enabled which would show up when device detects
2850 * host-initiated U3 exit.
2851 *
2852 * In that case, device will generate a Link State Change Interrupt
2853 * from U3 to RESUME which is only necessary if Hibernation is
2854 * configured in.
2855 *
2856 * There are no functional changes due to such spurious event and we
2857 * just need to ignore it.
2858 *
2859 * Refers to:
2860 *
2861 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2862 * operational mode
2863 */
2864 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2865 if ((dwc->revision < DWC3_REVISION_250A) &&
2866 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2867 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2868 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002869 return;
2870 }
2871 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002872
2873 /*
2874 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2875 * on the link partner, the USB session might do multiple entry/exit
2876 * of low power states before a transfer takes place.
2877 *
2878 * Due to this problem, we might experience lower throughput. The
2879 * suggested workaround is to disable DCTL[12:9] bits if we're
2880 * transitioning from U1/U2 to U0 and enable those bits again
2881 * after a transfer completes and there are no pending transfers
2882 * on any of the enabled endpoints.
2883 *
2884 * This is the first half of that workaround.
2885 *
2886 * Refers to:
2887 *
2888 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2889 * core send LGO_Ux entering U0
2890 */
2891 if (dwc->revision < DWC3_REVISION_183A) {
2892 if (next == DWC3_LINK_STATE_U0) {
2893 u32 u1u2;
2894 u32 reg;
2895
2896 switch (dwc->link_state) {
2897 case DWC3_LINK_STATE_U1:
2898 case DWC3_LINK_STATE_U2:
2899 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2900 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2901 | DWC3_DCTL_ACCEPTU2ENA
2902 | DWC3_DCTL_INITU1ENA
2903 | DWC3_DCTL_ACCEPTU1ENA);
2904
2905 if (!dwc->u1u2)
2906 dwc->u1u2 = reg & u1u2;
2907
2908 reg &= ~u1u2;
2909
2910 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2911 break;
2912 default:
2913 /* do nothing */
2914 break;
2915 }
2916 }
2917 }
2918
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002919 switch (next) {
2920 case DWC3_LINK_STATE_U1:
2921 if (dwc->speed == USB_SPEED_SUPER)
2922 dwc3_suspend_gadget(dwc);
2923 break;
2924 case DWC3_LINK_STATE_U2:
2925 case DWC3_LINK_STATE_U3:
2926 dwc3_suspend_gadget(dwc);
2927 break;
2928 case DWC3_LINK_STATE_RESUME:
2929 dwc3_resume_gadget(dwc);
2930 break;
2931 default:
2932 /* do nothing */
2933 break;
2934 }
2935
Felipe Balbie57ebc12014-04-22 13:20:12 -05002936 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002937}
2938
Baolin Wang72704f82016-05-16 16:43:53 +08002939static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2940 unsigned int evtinfo)
2941{
2942 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2943
2944 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2945 dwc3_suspend_gadget(dwc);
2946
2947 dwc->link_state = next;
2948}
2949
Felipe Balbie1dadd32014-02-25 14:47:54 -06002950static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2951 unsigned int evtinfo)
2952{
2953 unsigned int is_ss = evtinfo & BIT(4);
2954
Felipe Balbibfad65e2017-04-19 14:59:27 +03002955 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002956 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2957 * have a known issue which can cause USB CV TD.9.23 to fail
2958 * randomly.
2959 *
2960 * Because of this issue, core could generate bogus hibernation
2961 * events which SW needs to ignore.
2962 *
2963 * Refers to:
2964 *
2965 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2966 * Device Fallback from SuperSpeed
2967 */
2968 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2969 return;
2970
2971 /* enter hibernation here */
2972}
2973
Felipe Balbi72246da2011-08-19 18:10:58 +03002974static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2975 const struct dwc3_event_devt *event)
2976{
2977 switch (event->type) {
2978 case DWC3_DEVICE_EVENT_DISCONNECT:
2979 dwc3_gadget_disconnect_interrupt(dwc);
2980 break;
2981 case DWC3_DEVICE_EVENT_RESET:
2982 dwc3_gadget_reset_interrupt(dwc);
2983 break;
2984 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2985 dwc3_gadget_conndone_interrupt(dwc);
2986 break;
2987 case DWC3_DEVICE_EVENT_WAKEUP:
2988 dwc3_gadget_wakeup_interrupt(dwc);
2989 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002990 case DWC3_DEVICE_EVENT_HIBER_REQ:
2991 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2992 "unexpected hibernation event\n"))
2993 break;
2994
2995 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2996 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002997 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2998 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2999 break;
3000 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003001 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003002 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08003003 /*
3004 * Ignore suspend event until the gadget enters into
3005 * USB_STATE_CONFIGURED state.
3006 */
3007 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3008 dwc3_gadget_suspend_interrupt(dwc,
3009 event->event_info);
3010 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003011 break;
3012 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003013 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003014 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003015 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003016 break;
3017 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003018 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003019 }
3020}
3021
3022static void dwc3_process_event_entry(struct dwc3 *dwc,
3023 const union dwc3_event *event)
3024{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003025 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003026
Felipe Balbidfc5e802017-04-26 13:44:51 +03003027 if (!event->type.is_devspec)
3028 dwc3_endpoint_interrupt(dwc, &event->depevt);
3029 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003030 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003031 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003032 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003033}
3034
Felipe Balbidea520a2016-03-30 09:39:34 +03003035static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003036{
Felipe Balbidea520a2016-03-30 09:39:34 +03003037 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003038 irqreturn_t ret = IRQ_NONE;
3039 int left;
3040 u32 reg;
3041
Felipe Balbif42f2442013-06-12 21:25:08 +03003042 left = evt->count;
3043
3044 if (!(evt->flags & DWC3_EVENT_PENDING))
3045 return IRQ_NONE;
3046
3047 while (left > 0) {
3048 union dwc3_event event;
3049
John Younebbb2d52016-11-15 13:07:02 +02003050 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003051
3052 dwc3_process_event_entry(dwc, &event);
3053
3054 /*
3055 * FIXME we wrap around correctly to the next entry as
3056 * almost all entries are 4 bytes in size. There is one
3057 * entry which has 12 bytes which is a regular entry
3058 * followed by 8 bytes data. ATM I don't know how
3059 * things are organized if we get next to the a
3060 * boundary so I worry about that once we try to handle
3061 * that.
3062 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003063 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003064 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003065 }
3066
3067 evt->count = 0;
3068 evt->flags &= ~DWC3_EVENT_PENDING;
3069 ret = IRQ_HANDLED;
3070
3071 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003072 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003073 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003074 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003075
John Youncf40b862016-11-14 12:32:43 -08003076 if (dwc->imod_interval) {
3077 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3078 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3079 }
3080
Felipe Balbif42f2442013-06-12 21:25:08 +03003081 return ret;
3082}
3083
Felipe Balbidea520a2016-03-30 09:39:34 +03003084static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003085{
Felipe Balbidea520a2016-03-30 09:39:34 +03003086 struct dwc3_event_buffer *evt = _evt;
3087 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003088 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003089 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003090
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003091 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003092 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003093 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003094
3095 return ret;
3096}
3097
Felipe Balbidea520a2016-03-30 09:39:34 +03003098static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003099{
Felipe Balbidea520a2016-03-30 09:39:34 +03003100 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003101 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003102 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003103 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003104
Felipe Balbifc8bb912016-05-16 13:14:48 +03003105 if (pm_runtime_suspended(dwc->dev)) {
3106 pm_runtime_get(dwc->dev);
3107 disable_irq_nosync(dwc->irq_gadget);
3108 dwc->pending_events = true;
3109 return IRQ_HANDLED;
3110 }
3111
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003112 /*
3113 * With PCIe legacy interrupt, test shows that top-half irq handler can
3114 * be called again after HW interrupt deassertion. Check if bottom-half
3115 * irq event handler completes before caching new event to prevent
3116 * losing events.
3117 */
3118 if (evt->flags & DWC3_EVENT_PENDING)
3119 return IRQ_HANDLED;
3120
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003121 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003122 count &= DWC3_GEVNTCOUNT_MASK;
3123 if (!count)
3124 return IRQ_NONE;
3125
Felipe Balbib15a7622011-06-30 16:57:15 +03003126 evt->count = count;
3127 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003128
Felipe Balbie8adfc32013-06-12 21:11:14 +03003129 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003130 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003131 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003132 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003133
John Younebbb2d52016-11-15 13:07:02 +02003134 amount = min(count, evt->length - evt->lpos);
3135 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3136
3137 if (amount < count)
3138 memcpy(evt->cache, evt->buf, count - amount);
3139
John Youn65aca322016-11-15 13:08:59 +02003140 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3141
Felipe Balbib15a7622011-06-30 16:57:15 +03003142 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003143}
3144
Felipe Balbidea520a2016-03-30 09:39:34 +03003145static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003146{
Felipe Balbidea520a2016-03-30 09:39:34 +03003147 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003148
Felipe Balbidea520a2016-03-30 09:39:34 +03003149 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003150}
3151
Felipe Balbi6db38122016-10-03 11:27:01 +03003152static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3153{
3154 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3155 int irq;
3156
3157 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3158 if (irq > 0)
3159 goto out;
3160
3161 if (irq == -EPROBE_DEFER)
3162 goto out;
3163
3164 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3165 if (irq > 0)
3166 goto out;
3167
3168 if (irq == -EPROBE_DEFER)
3169 goto out;
3170
3171 irq = platform_get_irq(dwc3_pdev, 0);
3172 if (irq > 0)
3173 goto out;
3174
3175 if (irq != -EPROBE_DEFER)
3176 dev_err(dwc->dev, "missing peripheral IRQ\n");
3177
3178 if (!irq)
3179 irq = -EINVAL;
3180
3181out:
3182 return irq;
3183}
3184
Felipe Balbi72246da2011-08-19 18:10:58 +03003185/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003186 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003187 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003188 *
3189 * Returns 0 on success otherwise negative errno.
3190 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003191int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003192{
Felipe Balbi6db38122016-10-03 11:27:01 +03003193 int ret;
3194 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003195
Felipe Balbi6db38122016-10-03 11:27:01 +03003196 irq = dwc3_gadget_get_irq(dwc);
3197 if (irq < 0) {
3198 ret = irq;
3199 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003200 }
3201
3202 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003203
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303204 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3205 sizeof(*dwc->ep0_trb) * 2,
3206 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 if (!dwc->ep0_trb) {
3208 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3209 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003210 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003211 }
3212
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003213 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003214 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003215 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003216 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003217 }
3218
Felipe Balbi905dc042017-01-05 14:46:52 +02003219 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3220 &dwc->bounce_addr, GFP_KERNEL);
3221 if (!dwc->bounce) {
3222 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003223 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003224 }
3225
Baolin Wangbb014732016-10-14 17:11:33 +08003226 init_completion(&dwc->ep0_in_setup);
3227
Felipe Balbi72246da2011-08-19 18:10:58 +03003228 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003229 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003230 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003231 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003232 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003233
3234 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003235 * FIXME We might be setting max_speed to <SUPER, however versions
3236 * <2.20a of dwc3 have an issue with metastability (documented
3237 * elsewhere in this driver) which tells us we can't set max speed to
3238 * anything lower than SUPER.
3239 *
3240 * Because gadget.max_speed is only used by composite.c and function
3241 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3242 * to happen so we avoid sending SuperSpeed Capability descriptor
3243 * together with our BOS descriptor as that could confuse host into
3244 * thinking we can handle super speed.
3245 *
3246 * Note that, in fact, we won't even support GetBOS requests when speed
3247 * is less than super speed because we don't have means, yet, to tell
3248 * composite.c that we are USB 2.0 + LPM ECN.
3249 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003250 if (dwc->revision < DWC3_REVISION_220A &&
3251 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003252 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003253 dwc->revision);
3254
3255 dwc->gadget.max_speed = dwc->maximum_speed;
3256
3257 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003258 * REVISIT: Here we should clear all pending IRQs to be
3259 * sure we're starting from a well known location.
3260 */
3261
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003262 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003263 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003264 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003265
Felipe Balbi72246da2011-08-19 18:10:58 +03003266 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3267 if (ret) {
3268 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003269 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003270 }
3271
3272 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003273
3274err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003275 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003276
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003277err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003278 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3279 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003280
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003281err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003282 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003283
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003284err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303285 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003286 dwc->ep0_trb, dwc->ep0_trb_addr);
3287
Felipe Balbi72246da2011-08-19 18:10:58 +03003288err0:
3289 return ret;
3290}
3291
Felipe Balbi7415f172012-04-30 14:56:33 +03003292/* -------------------------------------------------------------------------- */
3293
Felipe Balbi72246da2011-08-19 18:10:58 +03003294void dwc3_gadget_exit(struct dwc3 *dwc)
3295{
Felipe Balbi72246da2011-08-19 18:10:58 +03003296 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003297 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003298 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003299 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003300 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303301 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003302 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003303}
Felipe Balbi7415f172012-04-30 14:56:33 +03003304
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003305int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003306{
Roger Quadros9772b472016-04-12 11:33:29 +03003307 if (!dwc->gadget_driver)
3308 return 0;
3309
Roger Quadros1551e352017-02-15 14:16:26 +02003310 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003311 dwc3_disconnect_gadget(dwc);
3312 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003313
3314 return 0;
3315}
3316
3317int dwc3_gadget_resume(struct dwc3 *dwc)
3318{
Felipe Balbi7415f172012-04-30 14:56:33 +03003319 int ret;
3320
Roger Quadros9772b472016-04-12 11:33:29 +03003321 if (!dwc->gadget_driver)
3322 return 0;
3323
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003324 ret = __dwc3_gadget_start(dwc);
3325 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003326 goto err0;
3327
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003328 ret = dwc3_gadget_run_stop(dwc, true, false);
3329 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003330 goto err1;
3331
Felipe Balbi7415f172012-04-30 14:56:33 +03003332 return 0;
3333
3334err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003335 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003336
3337err0:
3338 return ret;
3339}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003340
3341void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3342{
3343 if (dwc->pending_events) {
3344 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3345 dwc->pending_events = false;
3346 enable_irq(dwc->irq_gadget);
3347 }
3348}