blob: 0dedf8a799f454a896677f9e474bdc6397f94412 [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
Wei Yongjun9cffd15d2018-03-29 02:20:10 +0000169static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300170 struct dwc3_request *req, int status)
171{
172 struct dwc3 *dwc = dep->dwc;
173
174 req->started = false;
175 list_del(&req->list);
176 req->remaining = 0;
177
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
180
181 if (req->trb)
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
183 &req->request, req->direction);
184
185 req->trb = NULL;
186 trace_dwc3_gadget_giveback(req);
187
188 if (dep->number > 1)
189 pm_runtime_put(dwc->dev);
190}
191
Felipe Balbibfad65e2017-04-19 14:59:27 +0300192/**
193 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
194 * @dep: The endpoint to whom the request belongs to
195 * @req: The request we're giving back
196 * @status: completion code for the request
197 *
198 * Must be called with controller's lock held and interrupts disabled. This
199 * function will unmap @req and call its ->complete() callback to notify upper
200 * layers that it has completed.
201 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300202void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
203 int status)
204{
205 struct dwc3 *dwc = dep->dwc;
206
Felipe Balbic91815b2018-03-26 13:14:47 +0300207 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300208
209 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 spin_lock(&dwc->lock);
212}
213
Felipe Balbibfad65e2017-04-19 14:59:27 +0300214/**
215 * dwc3_send_gadget_generic_command - issue a generic command for the controller
216 * @dwc: pointer to the controller context
217 * @cmd: the command to be issued
218 * @param: command parameter
219 *
220 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
221 * and wait for its completion.
222 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500223int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300224{
225 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300226 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300227 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300228 u32 reg;
229
230 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
231 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
232
233 do {
234 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
235 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300236 status = DWC3_DGCMD_STATUS(reg);
237 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300238 ret = -EINVAL;
239 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300240 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100241 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300242
243 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300244 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300245 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 }
247
Felipe Balbi71f7e702016-05-23 14:16:19 +0300248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
249
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300251}
252
Felipe Balbic36d8e92016-04-04 12:46:33 +0300253static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
254
Felipe Balbibfad65e2017-04-19 14:59:27 +0300255/**
256 * dwc3_send_gadget_ep_cmd - issue an endpoint command
257 * @dep: the endpoint to which the command is going to be issued
258 * @cmd: the command to be issued
259 * @params: parameters to the command
260 *
261 * Caller should handle locking. This function will issue @cmd with given
262 * @params to @dep and wait for its completion.
263 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300264int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
Felipe Balbi8897a762016-09-22 10:56:08 +0300267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300268 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000269 u32 timeout = 1000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 u32 reg;
271
Felipe Balbi0933df12016-05-23 14:02:33 +0300272 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300273 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300274 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300276 /*
277 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
278 * we're issuing an endpoint command, we must check if
279 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
280 *
281 * We will also set SUSPHY bit to what it was before returning as stated
282 * by the same section on Synopsys databook.
283 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300284 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
285 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
286 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
287 susphy = true;
288 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
289 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
290 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300291 }
292
Felipe Balbi59999142016-09-22 12:25:28 +0300293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300294 int needs_wakeup;
295
296 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
297 dwc->link_state == DWC3_LINK_STATE_U2 ||
298 dwc->link_state == DWC3_LINK_STATE_U3);
299
300 if (unlikely(needs_wakeup)) {
301 ret = __dwc3_gadget_wakeup(dwc);
302 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
303 ret);
304 }
305 }
306
Felipe Balbi2eb88012016-04-12 16:53:39 +0300307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300310
Felipe Balbi8897a762016-09-22 10:56:08 +0300311 /*
312 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
313 * not relying on XferNotReady, we can make use of a special "No
314 * Response Update Transfer" command where we should clear both CmdAct
315 * and CmdIOC bits.
316 *
317 * With this, we don't need to wait for command completion and can
318 * straight away issue further commands to the endpoint.
319 *
320 * NOTICE: We're making an assumption that control endpoints will never
321 * make use of Update Transfer command. This is a safe assumption
322 * because we can never have more than one request at a time with
323 * Control Endpoints. If anybody changes that assumption, this chunk
324 * needs to be updated accordingly.
325 */
326 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
327 !usb_endpoint_xfer_isoc(desc))
328 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
329 else
330 cmd |= DWC3_DEPCMD_CMDACT;
331
332 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000337
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000338 switch (cmd_status) {
339 case 0:
340 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300341 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000342 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000343 ret = -EINVAL;
344 break;
345 case DEPEVT_TRANSFER_BUS_EXPIRY:
346 /*
347 * SW issues START TRANSFER command to
348 * isochronous ep with future frame interval. If
349 * future interval time has already passed when
350 * core receives the command, it will respond
351 * with an error status of 'Bus Expiry'.
352 *
353 * Instead of always returning -EINVAL, let's
354 * give a hint to the gadget driver that this is
355 * the case by returning -EAGAIN.
356 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000357 ret = -EAGAIN;
358 break;
359 default:
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
361 }
362
Felipe Balbic0ca3242016-04-04 09:11:51 +0300363 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300365 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366
Felipe Balbif6bb2252016-05-23 13:53:34 +0300367 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300368 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300369 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300370 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300371
Felipe Balbi0933df12016-05-23 14:02:33 +0300372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
373
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300374 if (ret == 0) {
375 switch (DWC3_DEPCMD_CMD(cmd)) {
376 case DWC3_DEPCMD_STARTTRANSFER:
377 dep->flags |= DWC3_EP_TRANSFER_STARTED;
378 break;
379 case DWC3_DEPCMD_ENDTRANSFER:
380 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
381 break;
382 default:
383 /* nothing */
384 break;
385 }
386 }
387
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300388 if (unlikely(susphy)) {
389 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
392 }
393
Felipe Balbic0ca3242016-04-04 09:11:51 +0300394 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395}
396
John Youn50c763f2016-05-31 17:49:56 -0700397static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400 struct dwc3_gadget_ep_cmd_params params;
401 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
402
403 /*
404 * As of core revision 2.60a the recommended programming model
405 * is to set the ClearPendIN bit when issuing a Clear Stall EP
406 * command for IN endpoints. This is to prevent an issue where
407 * some (non-compliant) hosts may not send ACK TPs for pending
408 * IN transfers due to a mishandled error condition. Synopsys
409 * STAR 9000614252.
410 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
414
415 memset(&params, 0, sizeof(params));
416
Felipe Balbi2cd47182016-04-12 16:42:43 +0300417 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700418}
419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200421 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300422{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300423 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
425 return dep->trb_pool_dma + offset;
426}
427
428static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
429{
430 struct dwc3 *dwc = dep->dwc;
431
432 if (dep->trb_pool)
433 return 0;
434
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
437 &dep->trb_pool_dma, GFP_KERNEL);
438 if (!dep->trb_pool) {
439 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
440 dep->name);
441 return -ENOMEM;
442 }
443
444 return 0;
445}
446
447static void dwc3_free_trb_pool(struct dwc3_ep *dep)
448{
449 struct dwc3 *dwc = dep->dwc;
450
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 dep->trb_pool, dep->trb_pool_dma);
453
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
456}
457
John Younc4509602016-02-16 20:10:53 -0800458static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
459
460/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300461 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800462 * @dwc: pointer to our controller context structure
463 * @dep: endpoint that is being enabled
464 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300465 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
466 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800467 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300468 * The assignment of transfer resources cannot perfectly follow the data book
469 * due to the fact that the controller driver does not have all knowledge of the
470 * configuration in advance. It is given this information piecemeal by the
471 * composite gadget framework after every SET_CONFIGURATION and
472 * SET_INTERFACE. Trying to follow the databook programming model in this
473 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800474 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300475 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
476 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
477 * incorrect in the scenario of multiple interfaces.
478 *
479 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800480 * endpoint on alt setting (8.1.6).
481 *
482 * The following simplified method is used instead:
483 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300484 * All hardware endpoints can be assigned a transfer resource and this setting
485 * will stay persistent until either a core reset or hibernation. So whenever we
486 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
487 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800488 * guaranteed that there are as many transfer resources as endpoints.
489 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300490 * This function is called for each endpoint when it is being enabled but is
491 * triggered only when called for EP0-out, which always happens first, and which
492 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800493 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300494static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
495{
496 struct dwc3_gadget_ep_cmd_params params;
497 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800498 int i;
499 int ret;
500
501 if (dep->number)
502 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
504 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800505 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
Felipe Balbi2cd47182016-04-12 16:42:43 +0300507 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800508 if (ret)
509 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300510
John Younc4509602016-02-16 20:10:53 -0800511 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
512 struct dwc3_ep *dep = dwc->eps[i];
513
514 if (!dep)
515 continue;
516
517 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
518 if (ret)
519 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 }
521
522 return 0;
523}
524
525static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300526 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300527{
John Youn39ebb052016-11-09 16:36:28 -0800528 const struct usb_ss_ep_comp_descriptor *comp_desc;
529 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530 struct dwc3_gadget_ep_cmd_params params;
531
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300532 if (dev_WARN_ONCE(dwc->dev, modify && restore,
533 "Can't modify and restore\n"))
534 return -EINVAL;
535
John Youn39ebb052016-11-09 16:36:28 -0800536 comp_desc = dep->endpoint.comp_desc;
537 desc = dep->endpoint.desc;
538
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 memset(&params, 0x00, sizeof(params));
540
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300541 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900542 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
543
544 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800545 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300546 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300547 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900548 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300550 if (modify) {
551 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
552 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600553 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
554 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300555 } else {
556 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600557 }
558
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300559 if (usb_endpoint_xfer_control(desc))
560 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300561
562 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
563 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200565 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300566 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
567 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300568 dep->stream_capable = true;
569 }
570
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500571 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300572 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
574 /*
575 * We are doing 1:1 mapping for endpoints, meaning
576 * Physical Endpoints 2 maps to Logical Endpoint 2 and
577 * so on. We consider the direction bit as part of the physical
578 * endpoint number. So USB endpoint 0x81 is 0x03.
579 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300580 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
582 /*
583 * We must use the lower 16 TX FIFOs even though
584 * HW might have more
585 */
586 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300587 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
589 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 dep->interval = 1 << (desc->bInterval - 1);
592 }
593
Felipe Balbi2cd47182016-04-12 16:42:43 +0300594 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300595}
596
597static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
598{
599 struct dwc3_gadget_ep_cmd_params params;
600
601 memset(&params, 0x00, sizeof(params));
602
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300603 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300604
Felipe Balbi2cd47182016-04-12 16:42:43 +0300605 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
606 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300607}
608
609/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300610 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 * @dep: endpoint to be initialized
Felipe Balbibfad65e2017-04-19 14:59:27 +0300612 * @modify: if true, modify existing endpoint configuration
613 * @restore: if true, restore endpoint configuration from scratch buffer
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300615 * Caller should take care of locking. Execute all necessary commands to
616 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 */
618static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300619 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300620{
John Youn39ebb052016-11-09 16:36:28 -0800621 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800623
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300625 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
628 ret = dwc3_gadget_start_config(dwc, dep);
629 if (ret)
630 return ret;
631 }
632
John Youn39ebb052016-11-09 16:36:28 -0800633 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 if (ret)
635 return ret;
636
637 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200638 struct dwc3_trb *trb_st_hw;
639 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300640
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 dep->type = usb_endpoint_type(desc);
642 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800643 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
645 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
646 reg |= DWC3_DALEPENA_EP(dep->number);
647 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
648
Baolin Wang76a638f2016-10-31 19:38:36 +0800649 init_waitqueue_head(&dep->wait_end_transfer);
650
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300651 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200652 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300653
John Youn0d257442016-05-19 17:26:08 -0700654 /* Initialize the TRB ring */
655 dep->trb_dequeue = 0;
656 dep->trb_enqueue = 0;
657 memset(dep->trb_pool, 0,
658 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
659
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300660 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 trb_st_hw = &dep->trb_pool[0];
662
Felipe Balbif6bafc62012-02-06 11:04:53 +0200663 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200664 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
665 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
666 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
667 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 }
669
Felipe Balbia97ea992016-09-29 16:28:56 +0300670 /*
671 * Issue StartTransfer here with no-op TRB so we can always rely on No
672 * Response Update Transfer command.
673 */
674 if (usb_endpoint_xfer_bulk(desc)) {
675 struct dwc3_gadget_ep_cmd_params params;
676 struct dwc3_trb *trb;
677 dma_addr_t trb_dma;
678 u32 cmd;
679
680 memset(&params, 0, sizeof(params));
681 trb = &dep->trb_pool[0];
682 trb_dma = dwc3_trb_dma_offset(dep, trb);
683
684 params.param0 = upper_32_bits(trb_dma);
685 params.param1 = lower_32_bits(trb_dma);
686
687 cmd = DWC3_DEPCMD_STARTTRANSFER;
688
689 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
690 if (ret < 0)
691 return ret;
692
693 dep->flags |= DWC3_EP_BUSY;
694
695 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
696 WARN_ON_ONCE(!dep->resource_index);
697 }
698
Felipe Balbi2870e502016-11-03 13:53:29 +0200699
700out:
701 trace_dwc3_gadget_ep_enable(dep);
702
Felipe Balbi72246da2011-08-19 18:10:58 +0300703 return 0;
704}
705
Paul Zimmermanb992e682012-04-27 14:17:35 +0300706static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200707static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300708{
709 struct dwc3_request *req;
710
Felipe Balbi0e146022016-06-21 10:32:02 +0300711 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300712
Felipe Balbi0e146022016-06-21 10:32:02 +0300713 /* - giveback all requests to gadget driver */
714 while (!list_empty(&dep->started_list)) {
715 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200716
Felipe Balbi0e146022016-06-21 10:32:02 +0300717 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200718 }
719
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200720 while (!list_empty(&dep->pending_list)) {
721 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300722
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200723 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300724 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300725}
726
727/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300728 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 * @dep: the endpoint to disable
730 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300731 * This function undoes what __dwc3_gadget_ep_enable did and also removes
732 * requests which are currently being processed by the hardware and those which
733 * are not yet scheduled.
734 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200735 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300736 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300737static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
738{
739 struct dwc3 *dwc = dep->dwc;
740 u32 reg;
741
Felipe Balbi2870e502016-11-03 13:53:29 +0200742 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300745
Felipe Balbi687ef982014-04-16 10:30:33 -0500746 /* make sure HW endpoint isn't stalled */
747 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500748 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500749
Felipe Balbi72246da2011-08-19 18:10:58 +0300750 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
751 reg &= ~DWC3_DALEPENA_EP(dep->number);
752 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
753
Felipe Balbi879631a2011-09-30 10:58:47 +0300754 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800756 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300757
John Youn39ebb052016-11-09 16:36:28 -0800758 /* Clear out the ep descriptors for non-ep0 */
759 if (dep->number > 1) {
760 dep->endpoint.comp_desc = NULL;
761 dep->endpoint.desc = NULL;
762 }
763
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 return 0;
765}
766
767/* -------------------------------------------------------------------------- */
768
769static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
770 const struct usb_endpoint_descriptor *desc)
771{
772 return -EINVAL;
773}
774
775static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
776{
777 return -EINVAL;
778}
779
780/* -------------------------------------------------------------------------- */
781
782static int dwc3_gadget_ep_enable(struct usb_ep *ep,
783 const struct usb_endpoint_descriptor *desc)
784{
785 struct dwc3_ep *dep;
786 struct dwc3 *dwc;
787 unsigned long flags;
788 int ret;
789
790 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
791 pr_debug("dwc3: invalid parameters\n");
792 return -EINVAL;
793 }
794
795 if (!desc->wMaxPacketSize) {
796 pr_debug("dwc3: missing wMaxPacketSize\n");
797 return -EINVAL;
798 }
799
800 dep = to_dwc3_ep(ep);
801 dwc = dep->dwc;
802
Felipe Balbi95ca9612015-12-10 13:08:20 -0600803 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
804 "%s is already enabled\n",
805 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300806 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300807
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800809 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300810 spin_unlock_irqrestore(&dwc->lock, flags);
811
812 return ret;
813}
814
815static int dwc3_gadget_ep_disable(struct usb_ep *ep)
816{
817 struct dwc3_ep *dep;
818 struct dwc3 *dwc;
819 unsigned long flags;
820 int ret;
821
822 if (!ep) {
823 pr_debug("dwc3: invalid parameters\n");
824 return -EINVAL;
825 }
826
827 dep = to_dwc3_ep(ep);
828 dwc = dep->dwc;
829
Felipe Balbi95ca9612015-12-10 13:08:20 -0600830 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
831 "%s is already disabled\n",
832 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300833 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300834
Felipe Balbi72246da2011-08-19 18:10:58 +0300835 spin_lock_irqsave(&dwc->lock, flags);
836 ret = __dwc3_gadget_ep_disable(dep);
837 spin_unlock_irqrestore(&dwc->lock, flags);
838
839 return ret;
840}
841
842static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
843 gfp_t gfp_flags)
844{
845 struct dwc3_request *req;
846 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300847
848 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900849 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300851
852 req->epnum = dep->number;
853 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854
Felipe Balbi68d34c82016-05-30 13:34:58 +0300855 dep->allocated_requests++;
856
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500857 trace_dwc3_alloc_request(req);
858
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 return &req->request;
860}
861
862static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
863 struct usb_request *request)
864{
865 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300866 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300867
Felipe Balbi68d34c82016-05-30 13:34:58 +0300868 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500869 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300870 kfree(req);
871}
872
Felipe Balbi2c78c022016-08-12 13:13:10 +0300873static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
874
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200875static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
876 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
877 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200878{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300879 struct dwc3 *dwc = dep->dwc;
880 struct usb_gadget *gadget = &dwc->gadget;
881 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200882
Felipe Balbief966b92016-04-05 13:09:51 +0300883 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530884
Felipe Balbif6bafc62012-02-06 11:04:53 +0200885 trb->size = DWC3_TRB_SIZE_LENGTH(length);
886 trb->bpl = lower_32_bits(dma);
887 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200888
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200889 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200890 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200891 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200892 break;
893
894 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300895 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530896 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300897
Manu Gautam40d829f2017-07-19 17:07:10 +0530898 /*
899 * USB Specification 2.0 Section 5.9.2 states that: "If
900 * there is only a single transaction in the microframe,
901 * only a DATA0 data packet PID is used. If there are
902 * two transactions per microframe, DATA1 is used for
903 * the first transaction data packet and DATA0 is used
904 * for the second transaction data packet. If there are
905 * three transactions per microframe, DATA2 is used for
906 * the first transaction data packet, DATA1 is used for
907 * the second, and DATA0 is used for the third."
908 *
909 * IOW, we should satisfy the following cases:
910 *
911 * 1) length <= maxpacket
912 * - DATA0
913 *
914 * 2) maxpacket < length <= (2 * maxpacket)
915 * - DATA1, DATA0
916 *
917 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
918 * - DATA2, DATA1, DATA0
919 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300920 if (speed == USB_SPEED_HIGH) {
921 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530922 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530923 unsigned int maxp = usb_endpoint_maxp(ep->desc);
924
925 if (length <= (2 * maxp))
926 mult--;
927
928 if (length <= maxp)
929 mult--;
930
931 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300932 }
933 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530934 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300935 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200936
937 /* always enable Interrupt on Missed ISOC */
938 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200939 break;
940
941 case USB_ENDPOINT_XFER_BULK:
942 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200943 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200944 break;
945 default:
946 /*
947 * This is only possible with faulty memory because we
948 * checked it already :)
949 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300950 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
951 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200952 }
953
Felipe Balbica4d44e2016-03-10 13:53:27 +0200954 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300955 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300956 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600957
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200958 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300959 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
960 }
961
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200962 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300963 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300964 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200965
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530966 if (chain)
967 trb->ctrl |= DWC3_TRB_CTRL_CHN;
968
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200969 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200970 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200971
972 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500973
974 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200975}
976
John Youn361572b2016-05-19 17:26:17 -0700977/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200978 * dwc3_prepare_one_trb - setup one TRB from one request
979 * @dep: endpoint for which this request is prepared
980 * @req: dwc3_request pointer
981 * @chain: should this TRB be chained to the next?
982 * @node: only for isochronous endpoints. First TRB needs different type.
983 */
984static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
985 struct dwc3_request *req, unsigned chain, unsigned node)
986{
987 struct dwc3_trb *trb;
988 unsigned length = req->request.length;
989 unsigned stream_id = req->request.stream_id;
990 unsigned short_not_ok = req->request.short_not_ok;
991 unsigned no_interrupt = req->request.no_interrupt;
992 dma_addr_t dma = req->request.dma;
993
994 trb = &dep->trb_pool[dep->trb_enqueue];
995
996 if (!req->trb) {
997 dwc3_gadget_move_started_request(req);
998 req->trb = trb;
999 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1000 dep->queued_requests++;
1001 }
1002
1003 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1004 stream_id, short_not_ok, no_interrupt);
1005}
1006
1007/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001008 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -07001009 * @dep: The endpoint with the TRB ring
1010 * @index: The index of the current TRB in the ring
1011 *
1012 * Returns the TRB prior to the one pointed to by the index. If the
1013 * index is 0, we will wrap backwards, skip the link TRB, and return
1014 * the one just before that.
1015 */
1016static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1017{
Felipe Balbi45438a02016-08-11 12:26:59 +03001018 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001019
Felipe Balbi45438a02016-08-11 12:26:59 +03001020 if (!tmp)
1021 tmp = DWC3_TRB_NUM - 1;
1022
1023 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001024}
1025
Felipe Balbic4233572016-05-12 14:08:34 +03001026static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1027{
1028 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001029 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001030
1031 /*
1032 * If enqueue & dequeue are equal than it is either full or empty.
1033 *
1034 * One way to know for sure is if the TRB right before us has HWO bit
1035 * set or not. If it has, then we're definitely full and can't fit any
1036 * more transfers in our ring.
1037 */
1038 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001039 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001040 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001041 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001042
1043 return DWC3_TRB_NUM - 1;
1044 }
1045
John Youn9d7aba72016-08-26 18:43:01 -07001046 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001047 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001048
John Youn9d7aba72016-08-26 18:43:01 -07001049 if (dep->trb_dequeue < dep->trb_enqueue)
1050 trbs_left--;
1051
John Youn32db3d92016-05-19 17:26:12 -07001052 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001053}
1054
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001055static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001056 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001057{
Felipe Balbi1f512112016-08-12 13:17:27 +03001058 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001059 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 int i;
1061
Felipe Balbi1f512112016-08-12 13:17:27 +03001062 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001063 unsigned int length = req->request.length;
1064 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1065 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001066 unsigned chain = true;
1067
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001068 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001069 chain = false;
1070
Felipe Balbic6267a52017-01-05 14:58:46 +02001071 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1072 struct dwc3 *dwc = dep->dwc;
1073 struct dwc3_trb *trb;
1074
1075 req->unaligned = true;
1076
1077 /* prepare normal TRB */
1078 dwc3_prepare_one_trb(dep, req, true, i);
1079
1080 /* Now prepare one extra TRB to align transfer size */
1081 trb = &dep->trb_pool[dep->trb_enqueue];
1082 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1083 maxp - rem, false, 0,
1084 req->request.stream_id,
1085 req->request.short_not_ok,
1086 req->request.no_interrupt);
1087 } else {
1088 dwc3_prepare_one_trb(dep, req, chain, i);
1089 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001090
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001091 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001092 break;
1093 }
1094}
1095
1096static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001097 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001098{
Felipe Balbic6267a52017-01-05 14:58:46 +02001099 unsigned int length = req->request.length;
1100 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1101 unsigned int rem = length % maxp;
1102
1103 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1104 struct dwc3 *dwc = dep->dwc;
1105 struct dwc3_trb *trb;
1106
1107 req->unaligned = true;
1108
1109 /* prepare normal TRB */
1110 dwc3_prepare_one_trb(dep, req, true, 0);
1111
1112 /* Now prepare one extra TRB to align transfer size */
1113 trb = &dep->trb_pool[dep->trb_enqueue];
1114 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1115 false, 0, req->request.stream_id,
1116 req->request.short_not_ok,
1117 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001118 } else if (req->request.zero && req->request.length &&
1119 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1120 struct dwc3 *dwc = dep->dwc;
1121 struct dwc3_trb *trb;
1122
1123 req->zero = true;
1124
1125 /* prepare normal TRB */
1126 dwc3_prepare_one_trb(dep, req, true, 0);
1127
1128 /* Now prepare one extra TRB to handle ZLP */
1129 trb = &dep->trb_pool[dep->trb_enqueue];
1130 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1131 false, 0, req->request.stream_id,
1132 req->request.short_not_ok,
1133 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001134 } else {
1135 dwc3_prepare_one_trb(dep, req, false, 0);
1136 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001137}
1138
Felipe Balbi72246da2011-08-19 18:10:58 +03001139/*
1140 * dwc3_prepare_trbs - setup TRBs from requests
1141 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001142 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001143 * The function goes through the requests list and sets up TRBs for the
1144 * transfers. The function returns once there are no more TRBs available or
1145 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001146 */
Felipe Balbic4233572016-05-12 14:08:34 +03001147static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001148{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001149 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001150
1151 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1152
Felipe Balbid86c5a62016-10-25 13:48:52 +03001153 /*
1154 * We can get in a situation where there's a request in the started list
1155 * but there weren't enough TRBs to fully kick it in the first time
1156 * around, so it has been waiting for more TRBs to be freed up.
1157 *
1158 * In that case, we should check if we have a request with pending_sgs
1159 * in the started list and prepare TRBs for that request first,
1160 * otherwise we will prepare TRBs completely out of order and that will
1161 * break things.
1162 */
1163 list_for_each_entry(req, &dep->started_list, list) {
1164 if (req->num_pending_sgs > 0)
1165 dwc3_prepare_one_trb_sg(dep, req);
1166
1167 if (!dwc3_calc_trbs_left(dep))
1168 return;
1169 }
1170
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001171 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001172 struct dwc3 *dwc = dep->dwc;
1173 int ret;
1174
1175 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1176 dep->direction);
1177 if (ret)
1178 return;
1179
1180 req->sg = req->request.sg;
1181 req->num_pending_sgs = req->request.num_mapped_sgs;
1182
Felipe Balbi1f512112016-08-12 13:17:27 +03001183 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001184 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001185 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001186 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001187
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001188 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001189 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001190 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001191}
1192
Felipe Balbi7fdca762017-09-05 14:41:34 +03001193static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001194{
1195 struct dwc3_gadget_ep_cmd_params params;
1196 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001197 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001198 int ret;
1199 u32 cmd;
1200
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001201 if (!dwc3_calc_trbs_left(dep))
1202 return 0;
1203
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001204 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001205
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001206 dwc3_prepare_trbs(dep);
1207 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001208 if (!req) {
1209 dep->flags |= DWC3_EP_PENDING_REQUEST;
1210 return 0;
1211 }
1212
1213 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001214
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001215 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301216 params.param0 = upper_32_bits(req->trb_dma);
1217 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001218 cmd = DWC3_DEPCMD_STARTTRANSFER;
1219
1220 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1221 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301222 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001223 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1224 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301225 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001226
Felipe Balbi2cd47182016-04-12 16:42:43 +03001227 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001229 /*
1230 * FIXME we need to iterate over the list of requests
1231 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001232 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001233 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001234 if (req->trb)
1235 memset(req->trb, 0, sizeof(struct dwc3_trb));
Janusz Dziedzic8ab89da2016-11-09 11:01:31 +01001236 dep->queued_requests--;
Felipe Balbic91815b2018-03-26 13:14:47 +03001237 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001238 return ret;
1239 }
1240
1241 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001242
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001243 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001244 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001245 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001246 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001247
Felipe Balbi72246da2011-08-19 18:10:58 +03001248 return 0;
1249}
1250
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001251static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1252{
1253 u32 reg;
1254
1255 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1256 return DWC3_DSTS_SOFFN(reg);
1257}
1258
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301259static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1260 struct dwc3_ep *dep, u32 cur_uf)
1261{
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001262 if (list_empty(&dep->pending_list)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001263 dev_info(dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001264 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301265 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301266 return;
1267 }
1268
John Younaf771d72017-01-26 11:58:40 -08001269 /*
1270 * Schedule the first trb for one interval in the future or at
1271 * least 4 microframes.
1272 */
Felipe Balbi502a37b2017-09-05 14:36:13 +03001273 dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001274 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301275}
1276
1277static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1278 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1279{
1280 u32 cur_uf, mask;
1281
1282 mask = ~(dep->interval - 1);
1283 cur_uf = event->parameters & mask;
1284
1285 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1286}
1287
Felipe Balbi72246da2011-08-19 18:10:58 +03001288static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1289{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001290 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001291
Felipe Balbibb423982015-11-16 15:31:21 -06001292 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001293 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1294 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001295 return -ESHUTDOWN;
1296 }
1297
Felipe Balbi04fb3652017-05-17 15:57:45 +03001298 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1299 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001300 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001301
Felipe Balbifc8bb912016-05-16 13:14:48 +03001302 pm_runtime_get(dwc->dev);
1303
Felipe Balbi72246da2011-08-19 18:10:58 +03001304 req->request.actual = 0;
1305 req->request.status = -EINPROGRESS;
1306 req->direction = dep->direction;
1307 req->epnum = dep->number;
1308
Felipe Balbife84f522015-09-01 09:01:38 -05001309 trace_dwc3_ep_queue(req);
1310
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001311 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001312
Felipe Balbid889c232016-09-29 15:44:29 +03001313 /*
1314 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1315 * wait for a XferNotReady event so we will know what's the current
1316 * (micro-)frame number.
1317 *
1318 * Without this trick, we are very, very likely gonna get Bus Expiry
1319 * errors which will force us issue EndTransfer command.
1320 */
1321 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001322 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1323 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1324 dwc3_stop_active_transfer(dwc, dep->number, true);
1325 dep->flags = DWC3_EP_ENABLED;
1326 } else {
1327 u32 cur_uf;
1328
1329 cur_uf = __dwc3_gadget_get_frame(dwc);
1330 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzic87aba102016-11-09 11:01:34 +01001331 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001332 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001333 return 0;
Felipe Balbi08a36b52016-08-11 14:27:52 +03001334 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001335
1336 if ((dep->flags & DWC3_EP_BUSY) &&
Felipe Balbi64e01082017-09-05 14:32:55 +03001337 !(dep->flags & DWC3_EP_MISSED_ISOC))
1338 goto out;
Roger Quadrosf1d68262017-04-21 15:58:08 +03001339
Felipe Balbi64e01082017-09-05 14:32:55 +03001340 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001341 }
1342
Roger Quadrosf1d68262017-04-21 15:58:08 +03001343out:
Felipe Balbi7fdca762017-09-05 14:41:34 +03001344 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001345}
1346
1347static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1348 gfp_t gfp_flags)
1349{
1350 struct dwc3_request *req = to_dwc3_request(request);
1351 struct dwc3_ep *dep = to_dwc3_ep(ep);
1352 struct dwc3 *dwc = dep->dwc;
1353
1354 unsigned long flags;
1355
1356 int ret;
1357
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001358 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 ret = __dwc3_gadget_ep_queue(dep, req);
1360 spin_unlock_irqrestore(&dwc->lock, flags);
1361
1362 return ret;
1363}
1364
1365static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1366 struct usb_request *request)
1367{
1368 struct dwc3_request *req = to_dwc3_request(request);
1369 struct dwc3_request *r = NULL;
1370
1371 struct dwc3_ep *dep = to_dwc3_ep(ep);
1372 struct dwc3 *dwc = dep->dwc;
1373
1374 unsigned long flags;
1375 int ret = 0;
1376
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001377 trace_dwc3_ep_dequeue(req);
1378
Felipe Balbi72246da2011-08-19 18:10:58 +03001379 spin_lock_irqsave(&dwc->lock, flags);
1380
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001381 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001382 if (r == req)
1383 break;
1384 }
1385
1386 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001387 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001388 if (r == req)
1389 break;
1390 }
1391 if (r == req) {
1392 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001393 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001394
1395 /*
1396 * If request was already started, this means we had to
1397 * stop the transfer. With that we also need to ignore
1398 * all TRBs used by the request, however TRBs can only
1399 * be modified after completion of END_TRANSFER
1400 * command. So what we do here is that we wait for
1401 * END_TRANSFER completion and only after that, we jump
1402 * over TRBs by clearing HWO and incrementing dequeue
1403 * pointer.
1404 *
1405 * Note that we have 2 possible types of transfers here:
1406 *
1407 * i) Linear buffer request
1408 * ii) SG-list based request
1409 *
1410 * SG-list based requests will have r->num_pending_sgs
1411 * set to a valid number (> 0). Linear requests,
1412 * normally use a single TRB.
1413 *
1414 * For each of these two cases, if r->unaligned flag is
1415 * set, one extra TRB has been used to align transfer
1416 * size to wMaxPacketSize.
1417 *
1418 * All of these cases need to be taken into
1419 * consideration so we don't mess up our TRB ring
1420 * pointers.
1421 */
1422 wait_event_lock_irq(dep->wait_end_transfer,
1423 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1424 dwc->lock);
1425
1426 if (!r->trb)
Mayank Rana96bd39d2018-03-23 10:05:33 -07001427 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001428
1429 if (r->num_pending_sgs) {
1430 struct dwc3_trb *trb;
1431 int i = 0;
1432
1433 for (i = 0; i < r->num_pending_sgs; i++) {
1434 trb = r->trb + i;
1435 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1436 dwc3_ep_inc_deq(dep);
1437 }
1438
Felipe Balbid6e5a542017-04-07 16:34:38 +03001439 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001440 trb = r->trb + r->num_pending_sgs + 1;
1441 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1442 dwc3_ep_inc_deq(dep);
1443 }
1444 } else {
1445 struct dwc3_trb *trb = r->trb;
1446
1447 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1448 dwc3_ep_inc_deq(dep);
1449
Felipe Balbid6e5a542017-04-07 16:34:38 +03001450 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001451 trb = r->trb + 1;
1452 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1453 dwc3_ep_inc_deq(dep);
1454 }
1455 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301456 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001457 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001458 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001459 request, ep->name);
1460 ret = -EINVAL;
1461 goto out0;
1462 }
1463
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301464out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001465 /* giveback the request */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001466 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1468
1469out0:
1470 spin_unlock_irqrestore(&dwc->lock, flags);
1471
1472 return ret;
1473}
1474
Felipe Balbi7a608552014-09-24 14:19:52 -05001475int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001476{
1477 struct dwc3_gadget_ep_cmd_params params;
1478 struct dwc3 *dwc = dep->dwc;
1479 int ret;
1480
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001481 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1482 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1483 return -EINVAL;
1484 }
1485
Felipe Balbi72246da2011-08-19 18:10:58 +03001486 memset(&params, 0x00, sizeof(params));
1487
1488 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001489 struct dwc3_trb *trb;
1490
1491 unsigned transfer_in_flight;
1492 unsigned started;
1493
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001494 if (dep->flags & DWC3_EP_STALL)
1495 return 0;
1496
Felipe Balbi69450c42016-05-30 13:37:02 +03001497 if (dep->number > 1)
1498 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1499 else
1500 trb = &dwc->ep0_trb[dep->trb_enqueue];
1501
1502 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1503 started = !list_empty(&dep->started_list);
1504
1505 if (!protocol && ((dep->direction && transfer_in_flight) ||
1506 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001507 return -EAGAIN;
1508 }
1509
Felipe Balbi2cd47182016-04-12 16:42:43 +03001510 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1511 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001512 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001513 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 dep->name);
1515 else
1516 dep->flags |= DWC3_EP_STALL;
1517 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001518 if (!(dep->flags & DWC3_EP_STALL))
1519 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001520
John Youn50c763f2016-05-31 17:49:56 -07001521 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001522 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001523 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 dep->name);
1525 else
Alan Sterna535d812013-11-01 12:05:12 -04001526 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001527 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001528
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 return ret;
1530}
1531
1532static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1533{
1534 struct dwc3_ep *dep = to_dwc3_ep(ep);
1535 struct dwc3 *dwc = dep->dwc;
1536
1537 unsigned long flags;
1538
1539 int ret;
1540
1541 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001542 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001543 spin_unlock_irqrestore(&dwc->lock, flags);
1544
1545 return ret;
1546}
1547
1548static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1549{
1550 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001551 struct dwc3 *dwc = dep->dwc;
1552 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001553 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001554
Paul Zimmerman249a4562012-02-24 17:32:16 -08001555 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001556 dep->flags |= DWC3_EP_WEDGE;
1557
Pratyush Anand08f0d962012-06-25 22:40:43 +05301558 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001559 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301560 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001561 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001562 spin_unlock_irqrestore(&dwc->lock, flags);
1563
1564 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001565}
1566
1567/* -------------------------------------------------------------------------- */
1568
1569static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1570 .bLength = USB_DT_ENDPOINT_SIZE,
1571 .bDescriptorType = USB_DT_ENDPOINT,
1572 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1573};
1574
1575static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1576 .enable = dwc3_gadget_ep0_enable,
1577 .disable = dwc3_gadget_ep0_disable,
1578 .alloc_request = dwc3_gadget_ep_alloc_request,
1579 .free_request = dwc3_gadget_ep_free_request,
1580 .queue = dwc3_gadget_ep0_queue,
1581 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301582 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001583 .set_wedge = dwc3_gadget_ep_set_wedge,
1584};
1585
1586static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1587 .enable = dwc3_gadget_ep_enable,
1588 .disable = dwc3_gadget_ep_disable,
1589 .alloc_request = dwc3_gadget_ep_alloc_request,
1590 .free_request = dwc3_gadget_ep_free_request,
1591 .queue = dwc3_gadget_ep_queue,
1592 .dequeue = dwc3_gadget_ep_dequeue,
1593 .set_halt = dwc3_gadget_ep_set_halt,
1594 .set_wedge = dwc3_gadget_ep_set_wedge,
1595};
1596
1597/* -------------------------------------------------------------------------- */
1598
1599static int dwc3_gadget_get_frame(struct usb_gadget *g)
1600{
1601 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001602
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001603 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001604}
1605
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001606static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001607{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001608 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001609
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001610 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001611 u32 reg;
1612
Felipe Balbi72246da2011-08-19 18:10:58 +03001613 u8 link_state;
1614 u8 speed;
1615
Felipe Balbi72246da2011-08-19 18:10:58 +03001616 /*
1617 * According to the Databook Remote wakeup request should
1618 * be issued only when the device is in early suspend state.
1619 *
1620 * We can check that via USB Link State bits in DSTS register.
1621 */
1622 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1623
1624 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001625 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001626 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001627 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001628
1629 link_state = DWC3_DSTS_USBLNKST(reg);
1630
1631 switch (link_state) {
1632 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1633 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1634 break;
1635 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001636 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 }
1638
Felipe Balbi8598bde2012-01-02 18:55:57 +02001639 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1640 if (ret < 0) {
1641 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001642 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001643 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001644
Paul Zimmerman802fde92012-04-27 13:10:52 +03001645 /* Recent versions do this automatically */
1646 if (dwc->revision < DWC3_REVISION_194A) {
1647 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001648 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001649 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1650 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1651 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001652
Paul Zimmerman1d046792012-02-15 18:56:56 -08001653 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001654 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001655
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001656 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001657 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1658
1659 /* in HS, means ON */
1660 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1661 break;
1662 }
1663
1664 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1665 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001666 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001667 }
1668
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001669 return 0;
1670}
1671
1672static int dwc3_gadget_wakeup(struct usb_gadget *g)
1673{
1674 struct dwc3 *dwc = gadget_to_dwc(g);
1675 unsigned long flags;
1676 int ret;
1677
1678 spin_lock_irqsave(&dwc->lock, flags);
1679 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001680 spin_unlock_irqrestore(&dwc->lock, flags);
1681
1682 return ret;
1683}
1684
1685static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1686 int is_selfpowered)
1687{
1688 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001689 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001690
Paul Zimmerman249a4562012-02-24 17:32:16 -08001691 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001692 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001693 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001694
1695 return 0;
1696}
1697
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001698static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001699{
1700 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001701 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001702
Felipe Balbifc8bb912016-05-16 13:14:48 +03001703 if (pm_runtime_suspended(dwc->dev))
1704 return 0;
1705
Felipe Balbi72246da2011-08-19 18:10:58 +03001706 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001707 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001708 if (dwc->revision <= DWC3_REVISION_187A) {
1709 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1710 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1711 }
1712
1713 if (dwc->revision >= DWC3_REVISION_194A)
1714 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1715 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001716
1717 if (dwc->has_hibernation)
1718 reg |= DWC3_DCTL_KEEP_CONNECT;
1719
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001720 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001721 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001723
1724 if (dwc->has_hibernation && !suspend)
1725 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1726
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001727 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001728 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001729
1730 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1731
1732 do {
1733 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001734 reg &= DWC3_DSTS_DEVCTRLHLT;
1735 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001736
1737 if (!timeout)
1738 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001739
Pratyush Anand6f17f742012-07-02 10:21:55 +05301740 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741}
1742
1743static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1744{
1745 struct dwc3 *dwc = gadget_to_dwc(g);
1746 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301747 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001748
1749 is_on = !!is_on;
1750
Baolin Wangbb014732016-10-14 17:11:33 +08001751 /*
1752 * Per databook, when we want to stop the gadget, if a control transfer
1753 * is still in process, complete it and get the core into setup phase.
1754 */
1755 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1756 reinit_completion(&dwc->ep0_in_setup);
1757
1758 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1759 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1760 if (ret == 0) {
1761 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1762 return -ETIMEDOUT;
1763 }
1764 }
1765
Felipe Balbi72246da2011-08-19 18:10:58 +03001766 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001767 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 spin_unlock_irqrestore(&dwc->lock, flags);
1769
Pratyush Anand6f17f742012-07-02 10:21:55 +05301770 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001771}
1772
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001773static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1774{
1775 u32 reg;
1776
1777 /* Enable all but Start and End of Frame IRQs */
1778 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1779 DWC3_DEVTEN_EVNTOVERFLOWEN |
1780 DWC3_DEVTEN_CMDCMPLTEN |
1781 DWC3_DEVTEN_ERRTICERREN |
1782 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001783 DWC3_DEVTEN_CONNECTDONEEN |
1784 DWC3_DEVTEN_USBRSTEN |
1785 DWC3_DEVTEN_DISCONNEVTEN);
1786
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001787 if (dwc->revision < DWC3_REVISION_250A)
1788 reg |= DWC3_DEVTEN_ULSTCNGEN;
1789
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001790 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1791}
1792
1793static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1794{
1795 /* mask all interrupts */
1796 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1797}
1798
1799static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001800static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001801
Felipe Balbi4e994722016-05-13 14:09:59 +03001802/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001803 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1804 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001805 *
1806 * The following looks like complex but it's actually very simple. In order to
1807 * calculate the number of packets we can burst at once on OUT transfers, we're
1808 * gonna use RxFIFO size.
1809 *
1810 * To calculate RxFIFO size we need two numbers:
1811 * MDWIDTH = size, in bits, of the internal memory bus
1812 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1813 *
1814 * Given these two numbers, the formula is simple:
1815 *
1816 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1817 *
1818 * 24 bytes is for 3x SETUP packets
1819 * 16 bytes is a clock domain crossing tolerance
1820 *
1821 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1822 */
1823static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1824{
1825 u32 ram2_depth;
1826 u32 mdwidth;
1827 u32 nump;
1828 u32 reg;
1829
1830 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1831 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1832
1833 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1834 nump = min_t(u32, nump, 16);
1835
1836 /* update NumP */
1837 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1838 reg &= ~DWC3_DCFG_NUMP_MASK;
1839 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1840 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1841}
1842
Felipe Balbid7be2952016-05-04 15:49:37 +03001843static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001844{
Felipe Balbi72246da2011-08-19 18:10:58 +03001845 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 int ret = 0;
1847 u32 reg;
1848
John Youncf40b862016-11-14 12:32:43 -08001849 /*
1850 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1851 * the core supports IMOD, disable it.
1852 */
1853 if (dwc->imod_interval) {
1854 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1855 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1856 } else if (dwc3_has_imod(dwc)) {
1857 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1858 }
1859
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001860 /*
1861 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1862 * field instead of letting dwc3 itself calculate that automatically.
1863 *
1864 * This way, we maximize the chances that we'll be able to get several
1865 * bursts of data without going through any sort of endpoint throttling.
1866 */
1867 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001868 if (dwc3_is_usb31(dwc))
1869 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1870 else
1871 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1872
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001873 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1874
Felipe Balbi4e994722016-05-13 14:09:59 +03001875 dwc3_gadget_setup_nump(dwc);
1876
Felipe Balbi72246da2011-08-19 18:10:58 +03001877 /* Start with SuperSpeed Default */
1878 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1879
1880 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001881 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001882 if (ret) {
1883 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001884 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 }
1886
1887 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001888 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 if (ret) {
1890 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001891 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001892 }
1893
1894 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001895 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001896 dwc3_ep0_out_start(dwc);
1897
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001898 dwc3_gadget_enable_irq(dwc);
1899
Felipe Balbid7be2952016-05-04 15:49:37 +03001900 return 0;
1901
1902err1:
1903 __dwc3_gadget_ep_disable(dwc->eps[0]);
1904
1905err0:
1906 return ret;
1907}
1908
1909static int dwc3_gadget_start(struct usb_gadget *g,
1910 struct usb_gadget_driver *driver)
1911{
1912 struct dwc3 *dwc = gadget_to_dwc(g);
1913 unsigned long flags;
1914 int ret = 0;
1915 int irq;
1916
Roger Quadros9522def2016-06-10 14:48:38 +03001917 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001918 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1919 IRQF_SHARED, "dwc3", dwc->ev_buf);
1920 if (ret) {
1921 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1922 irq, ret);
1923 goto err0;
1924 }
1925
1926 spin_lock_irqsave(&dwc->lock, flags);
1927 if (dwc->gadget_driver) {
1928 dev_err(dwc->dev, "%s is already bound to %s\n",
1929 dwc->gadget.name,
1930 dwc->gadget_driver->driver.name);
1931 ret = -EBUSY;
1932 goto err1;
1933 }
1934
1935 dwc->gadget_driver = driver;
1936
Felipe Balbifc8bb912016-05-16 13:14:48 +03001937 if (pm_runtime_active(dwc->dev))
1938 __dwc3_gadget_start(dwc);
1939
Felipe Balbi72246da2011-08-19 18:10:58 +03001940 spin_unlock_irqrestore(&dwc->lock, flags);
1941
1942 return 0;
1943
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001944err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001945 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001946 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001947
1948err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001949 return ret;
1950}
1951
Felipe Balbid7be2952016-05-04 15:49:37 +03001952static void __dwc3_gadget_stop(struct dwc3 *dwc)
1953{
1954 dwc3_gadget_disable_irq(dwc);
1955 __dwc3_gadget_ep_disable(dwc->eps[0]);
1956 __dwc3_gadget_ep_disable(dwc->eps[1]);
1957}
1958
Felipe Balbi22835b82014-10-17 12:05:12 -05001959static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001960{
1961 struct dwc3 *dwc = gadget_to_dwc(g);
1962 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001963 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02001964 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001965
1966 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001967
1968 if (pm_runtime_suspended(dwc->dev))
1969 goto out;
1970
Felipe Balbid7be2952016-05-04 15:49:37 +03001971 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001972
1973 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1974 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02001975 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08001976
1977 if (!dep)
1978 continue;
1979
1980 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1981 continue;
1982
Roger Quadros498f0472018-03-09 14:47:04 +02001983 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
1984 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1985 dwc->lock, msecs_to_jiffies(5));
1986
1987 if (ret <= 0) {
1988 /* Timed out or interrupted! There's nothing much
1989 * we can do so we just log here and print which
1990 * endpoints timed out at the end.
1991 */
1992 tmo_eps |= 1 << epnum;
1993 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
1994 }
1995 }
1996
1997 if (tmo_eps) {
1998 dev_err(dwc->dev,
1999 "end transfer timed out on endpoints 0x%x [bitmap]\n",
2000 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08002001 }
2002
2003out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002004 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002005 spin_unlock_irqrestore(&dwc->lock, flags);
2006
Felipe Balbi3f308d12016-05-16 14:17:06 +03002007 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002008
Felipe Balbi72246da2011-08-19 18:10:58 +03002009 return 0;
2010}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002011
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002012static void dwc3_gadget_set_speed(struct usb_gadget *g,
2013 enum usb_device_speed speed)
2014{
2015 struct dwc3 *dwc = gadget_to_dwc(g);
2016 unsigned long flags;
2017 u32 reg;
2018
2019 spin_lock_irqsave(&dwc->lock, flags);
2020 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2021 reg &= ~(DWC3_DCFG_SPEED_MASK);
2022
2023 /*
2024 * WORKAROUND: DWC3 revision < 2.20a have an issue
2025 * which would cause metastability state on Run/Stop
2026 * bit if we try to force the IP to USB2-only mode.
2027 *
2028 * Because of that, we cannot configure the IP to any
2029 * speed other than the SuperSpeed
2030 *
2031 * Refers to:
2032 *
2033 * STAR#9000525659: Clock Domain Crossing on DCTL in
2034 * USB 2.0 Mode
2035 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002036 if (dwc->revision < DWC3_REVISION_220A &&
2037 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002038 reg |= DWC3_DCFG_SUPERSPEED;
2039 } else {
2040 switch (speed) {
2041 case USB_SPEED_LOW:
2042 reg |= DWC3_DCFG_LOWSPEED;
2043 break;
2044 case USB_SPEED_FULL:
2045 reg |= DWC3_DCFG_FULLSPEED;
2046 break;
2047 case USB_SPEED_HIGH:
2048 reg |= DWC3_DCFG_HIGHSPEED;
2049 break;
2050 case USB_SPEED_SUPER:
2051 reg |= DWC3_DCFG_SUPERSPEED;
2052 break;
2053 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002054 if (dwc3_is_usb31(dwc))
2055 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2056 else
2057 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002058 break;
2059 default:
2060 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2061
2062 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2063 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2064 else
2065 reg |= DWC3_DCFG_SUPERSPEED;
2066 }
2067 }
2068 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2069
2070 spin_unlock_irqrestore(&dwc->lock, flags);
2071}
2072
Felipe Balbi72246da2011-08-19 18:10:58 +03002073static const struct usb_gadget_ops dwc3_gadget_ops = {
2074 .get_frame = dwc3_gadget_get_frame,
2075 .wakeup = dwc3_gadget_wakeup,
2076 .set_selfpowered = dwc3_gadget_set_selfpowered,
2077 .pullup = dwc3_gadget_pullup,
2078 .udc_start = dwc3_gadget_start,
2079 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002080 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002081};
2082
2083/* -------------------------------------------------------------------------- */
2084
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002085static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
Felipe Balbi72246da2011-08-19 18:10:58 +03002086{
2087 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002088 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002090 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2091
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002092 for (epnum = 0; epnum < total; epnum++) {
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002093 bool direction = epnum & 1;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002094 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002095
Felipe Balbi72246da2011-08-19 18:10:58 +03002096 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002097 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002098 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002099
2100 dep->dwc = dwc;
2101 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002102 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002103 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002104 dwc->eps[epnum] = dep;
2105
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002106 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002107 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002108
Felipe Balbi72246da2011-08-19 18:10:58 +03002109 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002110
2111 if (!(dep->number > 1)) {
2112 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2113 dep->endpoint.comp_desc = NULL;
2114 }
2115
Felipe Balbi74674cb2016-04-13 16:44:39 +03002116 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002117
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002118 if (num == 0) {
Robert Baldygae117e742013-12-13 12:23:38 +01002119 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302120 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002121 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002122 if (!direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002123 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002124 } else if (direction) {
2125 int mdwidth;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002126 int kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002127 int size;
2128 int ret;
Felipe Balbi28781782017-01-23 18:01:59 +02002129
2130 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2131 /* MDWIDTH is represented in bits, we need it in bytes */
2132 mdwidth /= 8;
2133
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002134 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
Thinh Nguyend548a612018-03-16 15:34:00 -07002135 if (dwc3_is_usb31(dwc))
2136 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2137 else
2138 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
Felipe Balbi28781782017-01-23 18:01:59 +02002139
2140 /* FIFO Depth is in MDWDITH bytes. Multiply */
2141 size *= mdwidth;
2142
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002143 kbytes = size / 1024;
2144 if (kbytes == 0)
2145 kbytes = 1;
Felipe Balbi28781782017-01-23 18:01:59 +02002146
2147 /*
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002148 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
Felipe Balbi28781782017-01-23 18:01:59 +02002149 * internal overhead. We don't really know how these are used,
2150 * but documentation say it exists.
2151 */
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002152 size -= mdwidth * (kbytes + 1);
2153 size /= kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002154
2155 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2156
2157 dep->endpoint.max_streams = 15;
2158 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2159 list_add_tail(&dep->endpoint.ep_list,
2160 &dwc->gadget.ep_list);
2161
2162 ret = dwc3_alloc_trb_pool(dep);
2163 if (ret)
2164 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002165 } else {
2166 int ret;
2167
Robert Baldygae117e742013-12-13 12:23:38 +01002168 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002169 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002170 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2171 list_add_tail(&dep->endpoint.ep_list,
2172 &dwc->gadget.ep_list);
2173
2174 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002175 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002176 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002177 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002178
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002179 if (num == 0) {
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002180 dep->endpoint.caps.type_control = true;
2181 } else {
2182 dep->endpoint.caps.type_iso = true;
2183 dep->endpoint.caps.type_bulk = true;
2184 dep->endpoint.caps.type_int = true;
2185 }
2186
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002187 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002188 dep->endpoint.caps.dir_out = !direction;
2189
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002190 INIT_LIST_HEAD(&dep->pending_list);
2191 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002192 }
2193
2194 return 0;
2195}
2196
2197static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2198{
2199 struct dwc3_ep *dep;
2200 u8 epnum;
2201
2202 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2203 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002204 if (!dep)
2205 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302206 /*
2207 * Physical endpoints 0 and 1 are special; they form the
2208 * bi-directional USB endpoint 0.
2209 *
2210 * For those two physical endpoints, we don't allocate a TRB
2211 * pool nor do we add them the endpoints list. Due to that, we
2212 * shouldn't do these two operations otherwise we would end up
2213 * with all sorts of bugs when removing dwc3.ko.
2214 */
2215 if (epnum != 0 && epnum != 1) {
2216 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002217 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302218 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002219
2220 kfree(dep);
2221 }
2222}
2223
Felipe Balbi72246da2011-08-19 18:10:58 +03002224/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002225
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302226static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2227 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002228 const struct dwc3_event_depevt *event, int status,
2229 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302230{
2231 unsigned int count;
2232 unsigned int s_pkt = 0;
2233 unsigned int trb_status;
2234
Felipe Balbidc55c672016-08-12 13:20:32 +03002235 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002236
2237 if (req->trb == trb)
2238 dep->queued_requests--;
2239
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002240 trace_dwc3_complete_trb(dep, trb);
2241
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002242 /*
2243 * If we're in the middle of series of chained TRBs and we
2244 * receive a short transfer along the way, DWC3 will skip
2245 * through all TRBs including the last TRB in the chain (the
2246 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2247 * bit and SW has to do it manually.
2248 *
2249 * We're going to do that here to avoid problems of HW trying
2250 * to use bogus TRBs for transfers.
2251 */
2252 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2253 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2254
Felipe Balbic6267a52017-01-05 14:58:46 +02002255 /*
2256 * If we're dealing with unaligned size OUT transfer, we will be left
2257 * with one TRB pending in the ring. We need to manually clear HWO bit
2258 * from that TRB.
2259 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002260 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002261 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2262 return 1;
2263 }
2264
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302265 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002266 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302267
Felipe Balbi35b27192017-03-08 13:56:37 +02002268 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2269 return 1;
2270
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302271 if (dep->direction) {
2272 if (count) {
2273 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2274 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302275 /*
2276 * If missed isoc occurred and there is
2277 * no request queued then issue END
2278 * TRANSFER, so that core generates
2279 * next xfernotready and we will issue
2280 * a fresh START TRANSFER.
2281 * If there are still queued request
2282 * then wait, do not issue either END
2283 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002284 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302285 * giveback.If any future queued request
2286 * is successfully transferred then we
2287 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002288 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302289 */
2290 dep->flags |= DWC3_EP_MISSED_ISOC;
2291 } else {
2292 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2293 dep->name);
2294 status = -ECONNRESET;
2295 }
2296 } else {
2297 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2298 }
2299 } else {
2300 if (count && (event->status & DEPEVT_STATUS_SHORT))
2301 s_pkt = 1;
2302 }
2303
Felipe Balbi7c705df2016-08-10 12:35:30 +03002304 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302305 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002306
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302307 if ((event->status & DEPEVT_STATUS_IOC) &&
2308 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2309 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002310
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302311 return 0;
2312}
2313
Felipe Balbi72246da2011-08-19 18:10:58 +03002314static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2315 const struct dwc3_event_depevt *event, int status)
2316{
Felipe Balbi31162af2016-08-11 14:38:37 +03002317 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002318 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002319 bool ioc = false;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002320 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002321
Felipe Balbi31162af2016-08-11 14:38:37 +03002322 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002323 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002324 int chain;
2325
Felipe Balbi1f512112016-08-12 13:17:27 +03002326 length = req->request.length;
2327 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002328 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002329 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002330 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002331 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002332 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002333
Felipe Balbi1f512112016-08-12 13:17:27 +03002334 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002335 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002336
Felipe Balbi7282c4e2016-10-25 13:50:46 +03002337 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2338 break;
2339
Felipe Balbi1f512112016-08-12 13:17:27 +03002340 req->sg = sg_next(s);
2341 req->num_pending_sgs--;
2342
Felipe Balbi31162af2016-08-11 14:38:37 +03002343 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2344 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002345 if (ret)
2346 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002347 }
2348 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002349 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002350 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002351 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002352 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002353
Felipe Balbid6e5a542017-04-07 16:34:38 +03002354 if (req->unaligned || req->zero) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002355 trb = &dep->trb_pool[dep->trb_dequeue];
2356 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2357 event, status, false);
2358 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002359 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002360 }
2361
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002362 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002363
Felipe Balbiff377ae2016-10-25 13:54:00 +03002364 if ((req->request.actual < length) && req->num_pending_sgs)
Felipe Balbi7fdca762017-09-05 14:41:34 +03002365 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi1f512112016-08-12 13:17:27 +03002366
Ville Syrjäläd115d702015-08-31 19:48:28 +03002367 dwc3_gadget_giveback(dep, req, status);
2368
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002369 if (ret) {
2370 if ((event->status & DEPEVT_STATUS_IOC) &&
2371 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2372 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002374 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002375 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002376
Felipe Balbi4cb42212016-05-18 12:37:21 +03002377 /*
2378 * Our endpoint might get disabled by another thread during
2379 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2380 * early on so DWC3_EP_BUSY flag gets cleared
2381 */
2382 if (!dep->endpoint.desc)
2383 return 1;
2384
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302385 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002386 list_empty(&dep->started_list)) {
2387 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302388 /*
2389 * If there is no entry in request list then do
2390 * not issue END TRANSFER now. Just set PENDING
2391 * flag, so that END TRANSFER is issued when an
2392 * entry is added into request list.
2393 */
2394 dep->flags = DWC3_EP_PENDING_REQUEST;
2395 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002396 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302397 dep->flags = DWC3_EP_ENABLED;
2398 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302399 return 1;
2400 }
2401
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002402 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2403 return 0;
2404
Felipe Balbi72246da2011-08-19 18:10:58 +03002405 return 1;
2406}
2407
2408static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002409 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002410{
2411 unsigned status = 0;
2412 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002413 u32 is_xfer_complete;
2414
2415 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002416
2417 if (event->status & DEPEVT_STATUS_BUSERR)
2418 status = -ECONNRESET;
2419
Paul Zimmerman1d046792012-02-15 18:56:56 -08002420 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002421 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002422 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002423 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002424
2425 /*
2426 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2427 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2428 */
2429 if (dwc->revision < DWC3_REVISION_183A) {
2430 u32 reg;
2431 int i;
2432
2433 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002434 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002435
2436 if (!(dep->flags & DWC3_EP_ENABLED))
2437 continue;
2438
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002439 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002440 return;
2441 }
2442
2443 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2444 reg |= dwc->u1u2;
2445 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2446
2447 dwc->u1u2 = 0;
2448 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002449
Felipe Balbi4cb42212016-05-18 12:37:21 +03002450 /*
2451 * Our endpoint might get disabled by another thread during
2452 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2453 * early on so DWC3_EP_BUSY flag gets cleared
2454 */
2455 if (!dep->endpoint.desc)
2456 return;
2457
Felipe Balbi7fdca762017-09-05 14:41:34 +03002458 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
2459 __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002460}
2461
Felipe Balbi72246da2011-08-19 18:10:58 +03002462static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2463 const struct dwc3_event_depevt *event)
2464{
2465 struct dwc3_ep *dep;
2466 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002467 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002468
2469 dep = dwc->eps[epnum];
2470
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002471 if (!(dep->flags & DWC3_EP_ENABLED)) {
2472 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2473 return;
2474
2475 /* Handle only EPCMDCMPLT when EP disabled */
2476 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2477 return;
2478 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002479
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 if (epnum == 0 || epnum == 1) {
2481 dwc3_ep0_interrupt(dwc, event);
2482 return;
2483 }
2484
2485 switch (event->endpoint_event) {
2486 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002487 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002488
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002489 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002490 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002491 return;
2492 }
2493
Jingoo Han029d97f2014-07-04 15:00:51 +09002494 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002495 break;
2496 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002497 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002498 break;
2499 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi7fdca762017-09-05 14:41:34 +03002500 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +03002501 dwc3_gadget_start_isoc(dwc, dep, event);
Felipe Balbi7fdca762017-09-05 14:41:34 +03002502 else
2503 __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002504
2505 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002506 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002507 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002508 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2509 dep->name);
2510 return;
2511 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002512 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002513 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002514 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2515
2516 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2517 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2518 wake_up(&dep->wait_end_transfer);
2519 }
2520 break;
2521 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002522 break;
2523 }
2524}
2525
2526static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2527{
2528 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2529 spin_unlock(&dwc->lock);
2530 dwc->gadget_driver->disconnect(&dwc->gadget);
2531 spin_lock(&dwc->lock);
2532 }
2533}
2534
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002535static void dwc3_suspend_gadget(struct dwc3 *dwc)
2536{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002537 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002538 spin_unlock(&dwc->lock);
2539 dwc->gadget_driver->suspend(&dwc->gadget);
2540 spin_lock(&dwc->lock);
2541 }
2542}
2543
2544static void dwc3_resume_gadget(struct dwc3 *dwc)
2545{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002546 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002547 spin_unlock(&dwc->lock);
2548 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002549 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002550 }
2551}
2552
2553static void dwc3_reset_gadget(struct dwc3 *dwc)
2554{
2555 if (!dwc->gadget_driver)
2556 return;
2557
2558 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2559 spin_unlock(&dwc->lock);
2560 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002561 spin_lock(&dwc->lock);
2562 }
2563}
2564
Paul Zimmermanb992e682012-04-27 14:17:35 +03002565static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002566{
2567 struct dwc3_ep *dep;
2568 struct dwc3_gadget_ep_cmd_params params;
2569 u32 cmd;
2570 int ret;
2571
2572 dep = dwc->eps[epnum];
2573
Baolin Wang76a638f2016-10-31 19:38:36 +08002574 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2575 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302576 return;
2577
Pratyush Anand57911502012-07-06 15:19:10 +05302578 /*
2579 * NOTICE: We are violating what the Databook says about the
2580 * EndTransfer command. Ideally we would _always_ wait for the
2581 * EndTransfer Command Completion IRQ, but that's causing too
2582 * much trouble synchronizing between us and gadget driver.
2583 *
2584 * We have discussed this with the IP Provider and it was
2585 * suggested to giveback all requests here, but give HW some
2586 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002587 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302588 *
2589 * Note also that a similar handling was tested by Synopsys
2590 * (thanks a lot Paul) and nothing bad has come out of it.
2591 * In short, what we're doing is:
2592 *
2593 * - Issue EndTransfer WITH CMDIOC bit set
2594 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002595 *
2596 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2597 * supports a mode to work around the above limitation. The
2598 * software can poll the CMDACT bit in the DEPCMD register
2599 * after issuing a EndTransfer command. This mode is enabled
2600 * by writing GUCTL2[14]. This polling is already done in the
2601 * dwc3_send_gadget_ep_cmd() function so if the mode is
2602 * enabled, the EndTransfer command will have completed upon
2603 * returning from this function and we don't need to delay for
2604 * 100us.
2605 *
2606 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302607 */
2608
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302609 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002610 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2611 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002612 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302613 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002614 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302615 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002616 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002617 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002618
Baolin Wang76a638f2016-10-31 19:38:36 +08002619 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2620 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002621 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002622 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002623}
2624
Felipe Balbi72246da2011-08-19 18:10:58 +03002625static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2626{
2627 u32 epnum;
2628
2629 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2630 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002631 int ret;
2632
2633 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002634 if (!dep)
2635 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002636
2637 if (!(dep->flags & DWC3_EP_STALL))
2638 continue;
2639
2640 dep->flags &= ~DWC3_EP_STALL;
2641
John Youn50c763f2016-05-31 17:49:56 -07002642 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002643 WARN_ON_ONCE(ret);
2644 }
2645}
2646
2647static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2648{
Felipe Balbic4430a22012-05-24 10:30:01 +03002649 int reg;
2650
Felipe Balbi72246da2011-08-19 18:10:58 +03002651 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2652 reg &= ~DWC3_DCTL_INITU1ENA;
2653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2654
2655 reg &= ~DWC3_DCTL_INITU2ENA;
2656 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002657
Felipe Balbi72246da2011-08-19 18:10:58 +03002658 dwc3_disconnect_gadget(dwc);
2659
2660 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002661 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002662 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002663
2664 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002665}
2666
Felipe Balbi72246da2011-08-19 18:10:58 +03002667static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2668{
2669 u32 reg;
2670
Felipe Balbifc8bb912016-05-16 13:14:48 +03002671 dwc->connected = true;
2672
Felipe Balbidf62df52011-10-14 15:11:49 +03002673 /*
2674 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2675 * would cause a missing Disconnect Event if there's a
2676 * pending Setup Packet in the FIFO.
2677 *
2678 * There's no suggested workaround on the official Bug
2679 * report, which states that "unless the driver/application
2680 * is doing any special handling of a disconnect event,
2681 * there is no functional issue".
2682 *
2683 * Unfortunately, it turns out that we _do_ some special
2684 * handling of a disconnect event, namely complete all
2685 * pending transfers, notify gadget driver of the
2686 * disconnection, and so on.
2687 *
2688 * Our suggested workaround is to follow the Disconnect
2689 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002690 * flag. Such flag gets set whenever we have a SETUP_PENDING
2691 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002692 * same endpoint.
2693 *
2694 * Refers to:
2695 *
2696 * STAR#9000466709: RTL: Device : Disconnect event not
2697 * generated if setup packet pending in FIFO
2698 */
2699 if (dwc->revision < DWC3_REVISION_188A) {
2700 if (dwc->setup_packet_pending)
2701 dwc3_gadget_disconnect_interrupt(dwc);
2702 }
2703
Felipe Balbi8e744752014-11-06 14:27:53 +08002704 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002705
2706 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2707 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2708 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002709 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 dwc3_clear_stall_all_ep(dwc);
2711
2712 /* Reset device address to zero */
2713 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2714 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2715 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002716}
2717
Felipe Balbi72246da2011-08-19 18:10:58 +03002718static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2719{
Felipe Balbi72246da2011-08-19 18:10:58 +03002720 struct dwc3_ep *dep;
2721 int ret;
2722 u32 reg;
2723 u8 speed;
2724
Felipe Balbi72246da2011-08-19 18:10:58 +03002725 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2726 speed = reg & DWC3_DSTS_CONNECTSPD;
2727 dwc->speed = speed;
2728
John Youn5fb6fda2016-11-10 17:23:25 -08002729 /*
2730 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2731 * each time on Connect Done.
2732 *
2733 * Currently we always use the reset value. If any platform
2734 * wants to set this to a different value, we need to add a
2735 * setting and update GCTL.RAMCLKSEL here.
2736 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002737
2738 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002739 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002740 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2741 dwc->gadget.ep0->maxpacket = 512;
2742 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2743 break;
John Youn2da9ad72016-05-20 16:34:26 -07002744 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002745 /*
2746 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2747 * would cause a missing USB3 Reset event.
2748 *
2749 * In such situations, we should force a USB3 Reset
2750 * event by calling our dwc3_gadget_reset_interrupt()
2751 * routine.
2752 *
2753 * Refers to:
2754 *
2755 * STAR#9000483510: RTL: SS : USB3 reset event may
2756 * not be generated always when the link enters poll
2757 */
2758 if (dwc->revision < DWC3_REVISION_190A)
2759 dwc3_gadget_reset_interrupt(dwc);
2760
Felipe Balbi72246da2011-08-19 18:10:58 +03002761 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2762 dwc->gadget.ep0->maxpacket = 512;
2763 dwc->gadget.speed = USB_SPEED_SUPER;
2764 break;
John Youn2da9ad72016-05-20 16:34:26 -07002765 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002766 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2767 dwc->gadget.ep0->maxpacket = 64;
2768 dwc->gadget.speed = USB_SPEED_HIGH;
2769 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002770 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002771 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2772 dwc->gadget.ep0->maxpacket = 64;
2773 dwc->gadget.speed = USB_SPEED_FULL;
2774 break;
John Youn2da9ad72016-05-20 16:34:26 -07002775 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002776 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2777 dwc->gadget.ep0->maxpacket = 8;
2778 dwc->gadget.speed = USB_SPEED_LOW;
2779 break;
2780 }
2781
Thinh Nguyen61800262018-01-12 18:18:05 -08002782 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2783
Pratyush Anand2b758352013-01-14 15:59:31 +05302784 /* Enable USB2 LPM Capability */
2785
John Younee5cd412016-02-05 17:08:45 -08002786 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002787 (speed != DWC3_DSTS_SUPERSPEED) &&
2788 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302789 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2790 reg |= DWC3_DCFG_LPM_CAP;
2791 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2792
2793 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2794 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2795
Huang Rui460d0982014-10-31 11:11:18 +08002796 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302797
Huang Rui80caf7d2014-10-28 19:54:26 +08002798 /*
2799 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2800 * DCFG.LPMCap is set, core responses with an ACK and the
2801 * BESL value in the LPM token is less than or equal to LPM
2802 * NYET threshold.
2803 */
2804 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2805 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002806 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002807
2808 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2809 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2810
Pratyush Anand2b758352013-01-14 15:59:31 +05302811 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002812 } else {
2813 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2814 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2815 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302816 }
2817
Felipe Balbi72246da2011-08-19 18:10:58 +03002818 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002819 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002820 if (ret) {
2821 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2822 return;
2823 }
2824
2825 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002826 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002827 if (ret) {
2828 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2829 return;
2830 }
2831
2832 /*
2833 * Configure PHY via GUSB3PIPECTLn if required.
2834 *
2835 * Update GTXFIFOSIZn
2836 *
2837 * In both cases reset values should be sufficient.
2838 */
2839}
2840
2841static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2842{
Felipe Balbi72246da2011-08-19 18:10:58 +03002843 /*
2844 * TODO take core out of low power mode when that's
2845 * implemented.
2846 */
2847
Jiebing Liad14d4e2014-12-11 13:26:29 +08002848 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2849 spin_unlock(&dwc->lock);
2850 dwc->gadget_driver->resume(&dwc->gadget);
2851 spin_lock(&dwc->lock);
2852 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002853}
2854
2855static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2856 unsigned int evtinfo)
2857{
Felipe Balbifae2b902011-10-14 13:00:30 +03002858 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002859 unsigned int pwropt;
2860
2861 /*
2862 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2863 * Hibernation mode enabled which would show up when device detects
2864 * host-initiated U3 exit.
2865 *
2866 * In that case, device will generate a Link State Change Interrupt
2867 * from U3 to RESUME which is only necessary if Hibernation is
2868 * configured in.
2869 *
2870 * There are no functional changes due to such spurious event and we
2871 * just need to ignore it.
2872 *
2873 * Refers to:
2874 *
2875 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2876 * operational mode
2877 */
2878 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2879 if ((dwc->revision < DWC3_REVISION_250A) &&
2880 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2881 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2882 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002883 return;
2884 }
2885 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002886
2887 /*
2888 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2889 * on the link partner, the USB session might do multiple entry/exit
2890 * of low power states before a transfer takes place.
2891 *
2892 * Due to this problem, we might experience lower throughput. The
2893 * suggested workaround is to disable DCTL[12:9] bits if we're
2894 * transitioning from U1/U2 to U0 and enable those bits again
2895 * after a transfer completes and there are no pending transfers
2896 * on any of the enabled endpoints.
2897 *
2898 * This is the first half of that workaround.
2899 *
2900 * Refers to:
2901 *
2902 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2903 * core send LGO_Ux entering U0
2904 */
2905 if (dwc->revision < DWC3_REVISION_183A) {
2906 if (next == DWC3_LINK_STATE_U0) {
2907 u32 u1u2;
2908 u32 reg;
2909
2910 switch (dwc->link_state) {
2911 case DWC3_LINK_STATE_U1:
2912 case DWC3_LINK_STATE_U2:
2913 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2914 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2915 | DWC3_DCTL_ACCEPTU2ENA
2916 | DWC3_DCTL_INITU1ENA
2917 | DWC3_DCTL_ACCEPTU1ENA);
2918
2919 if (!dwc->u1u2)
2920 dwc->u1u2 = reg & u1u2;
2921
2922 reg &= ~u1u2;
2923
2924 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2925 break;
2926 default:
2927 /* do nothing */
2928 break;
2929 }
2930 }
2931 }
2932
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002933 switch (next) {
2934 case DWC3_LINK_STATE_U1:
2935 if (dwc->speed == USB_SPEED_SUPER)
2936 dwc3_suspend_gadget(dwc);
2937 break;
2938 case DWC3_LINK_STATE_U2:
2939 case DWC3_LINK_STATE_U3:
2940 dwc3_suspend_gadget(dwc);
2941 break;
2942 case DWC3_LINK_STATE_RESUME:
2943 dwc3_resume_gadget(dwc);
2944 break;
2945 default:
2946 /* do nothing */
2947 break;
2948 }
2949
Felipe Balbie57ebc12014-04-22 13:20:12 -05002950 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002951}
2952
Baolin Wang72704f82016-05-16 16:43:53 +08002953static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2954 unsigned int evtinfo)
2955{
2956 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2957
2958 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2959 dwc3_suspend_gadget(dwc);
2960
2961 dwc->link_state = next;
2962}
2963
Felipe Balbie1dadd32014-02-25 14:47:54 -06002964static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2965 unsigned int evtinfo)
2966{
2967 unsigned int is_ss = evtinfo & BIT(4);
2968
Felipe Balbibfad65e2017-04-19 14:59:27 +03002969 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002970 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2971 * have a known issue which can cause USB CV TD.9.23 to fail
2972 * randomly.
2973 *
2974 * Because of this issue, core could generate bogus hibernation
2975 * events which SW needs to ignore.
2976 *
2977 * Refers to:
2978 *
2979 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2980 * Device Fallback from SuperSpeed
2981 */
2982 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2983 return;
2984
2985 /* enter hibernation here */
2986}
2987
Felipe Balbi72246da2011-08-19 18:10:58 +03002988static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2989 const struct dwc3_event_devt *event)
2990{
2991 switch (event->type) {
2992 case DWC3_DEVICE_EVENT_DISCONNECT:
2993 dwc3_gadget_disconnect_interrupt(dwc);
2994 break;
2995 case DWC3_DEVICE_EVENT_RESET:
2996 dwc3_gadget_reset_interrupt(dwc);
2997 break;
2998 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2999 dwc3_gadget_conndone_interrupt(dwc);
3000 break;
3001 case DWC3_DEVICE_EVENT_WAKEUP:
3002 dwc3_gadget_wakeup_interrupt(dwc);
3003 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003004 case DWC3_DEVICE_EVENT_HIBER_REQ:
3005 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3006 "unexpected hibernation event\n"))
3007 break;
3008
3009 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3010 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003011 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3012 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3013 break;
3014 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003015 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003016 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08003017 /*
3018 * Ignore suspend event until the gadget enters into
3019 * USB_STATE_CONFIGURED state.
3020 */
3021 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3022 dwc3_gadget_suspend_interrupt(dwc,
3023 event->event_info);
3024 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003025 break;
3026 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003027 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003028 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003029 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003030 break;
3031 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003032 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003033 }
3034}
3035
3036static void dwc3_process_event_entry(struct dwc3 *dwc,
3037 const union dwc3_event *event)
3038{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003039 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003040
Felipe Balbidfc5e802017-04-26 13:44:51 +03003041 if (!event->type.is_devspec)
3042 dwc3_endpoint_interrupt(dwc, &event->depevt);
3043 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003044 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003045 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003046 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003047}
3048
Felipe Balbidea520a2016-03-30 09:39:34 +03003049static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003050{
Felipe Balbidea520a2016-03-30 09:39:34 +03003051 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003052 irqreturn_t ret = IRQ_NONE;
3053 int left;
3054 u32 reg;
3055
Felipe Balbif42f2442013-06-12 21:25:08 +03003056 left = evt->count;
3057
3058 if (!(evt->flags & DWC3_EVENT_PENDING))
3059 return IRQ_NONE;
3060
3061 while (left > 0) {
3062 union dwc3_event event;
3063
John Younebbb2d52016-11-15 13:07:02 +02003064 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003065
3066 dwc3_process_event_entry(dwc, &event);
3067
3068 /*
3069 * FIXME we wrap around correctly to the next entry as
3070 * almost all entries are 4 bytes in size. There is one
3071 * entry which has 12 bytes which is a regular entry
3072 * followed by 8 bytes data. ATM I don't know how
3073 * things are organized if we get next to the a
3074 * boundary so I worry about that once we try to handle
3075 * that.
3076 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003077 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003078 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003079 }
3080
3081 evt->count = 0;
3082 evt->flags &= ~DWC3_EVENT_PENDING;
3083 ret = IRQ_HANDLED;
3084
3085 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003086 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003087 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003088 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003089
John Youncf40b862016-11-14 12:32:43 -08003090 if (dwc->imod_interval) {
3091 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3092 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3093 }
3094
Felipe Balbif42f2442013-06-12 21:25:08 +03003095 return ret;
3096}
3097
Felipe Balbidea520a2016-03-30 09:39:34 +03003098static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003099{
Felipe Balbidea520a2016-03-30 09:39:34 +03003100 struct dwc3_event_buffer *evt = _evt;
3101 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003102 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003103 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003104
Felipe Balbie5f68b42015-10-12 13:25:44 -05003105 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003106 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05003107 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003108
3109 return ret;
3110}
3111
Felipe Balbidea520a2016-03-30 09:39:34 +03003112static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003113{
Felipe Balbidea520a2016-03-30 09:39:34 +03003114 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003115 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003116 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003117 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003118
Felipe Balbifc8bb912016-05-16 13:14:48 +03003119 if (pm_runtime_suspended(dwc->dev)) {
3120 pm_runtime_get(dwc->dev);
3121 disable_irq_nosync(dwc->irq_gadget);
3122 dwc->pending_events = true;
3123 return IRQ_HANDLED;
3124 }
3125
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003126 /*
3127 * With PCIe legacy interrupt, test shows that top-half irq handler can
3128 * be called again after HW interrupt deassertion. Check if bottom-half
3129 * irq event handler completes before caching new event to prevent
3130 * losing events.
3131 */
3132 if (evt->flags & DWC3_EVENT_PENDING)
3133 return IRQ_HANDLED;
3134
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003135 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003136 count &= DWC3_GEVNTCOUNT_MASK;
3137 if (!count)
3138 return IRQ_NONE;
3139
Felipe Balbib15a7622011-06-30 16:57:15 +03003140 evt->count = count;
3141 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003142
Felipe Balbie8adfc32013-06-12 21:11:14 +03003143 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003144 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003145 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003146 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003147
John Younebbb2d52016-11-15 13:07:02 +02003148 amount = min(count, evt->length - evt->lpos);
3149 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3150
3151 if (amount < count)
3152 memcpy(evt->cache, evt->buf, count - amount);
3153
John Youn65aca322016-11-15 13:08:59 +02003154 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3155
Felipe Balbib15a7622011-06-30 16:57:15 +03003156 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003157}
3158
Felipe Balbidea520a2016-03-30 09:39:34 +03003159static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003160{
Felipe Balbidea520a2016-03-30 09:39:34 +03003161 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003162
Felipe Balbidea520a2016-03-30 09:39:34 +03003163 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003164}
3165
Felipe Balbi6db38122016-10-03 11:27:01 +03003166static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3167{
3168 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3169 int irq;
3170
3171 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3172 if (irq > 0)
3173 goto out;
3174
3175 if (irq == -EPROBE_DEFER)
3176 goto out;
3177
3178 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3179 if (irq > 0)
3180 goto out;
3181
3182 if (irq == -EPROBE_DEFER)
3183 goto out;
3184
3185 irq = platform_get_irq(dwc3_pdev, 0);
3186 if (irq > 0)
3187 goto out;
3188
3189 if (irq != -EPROBE_DEFER)
3190 dev_err(dwc->dev, "missing peripheral IRQ\n");
3191
3192 if (!irq)
3193 irq = -EINVAL;
3194
3195out:
3196 return irq;
3197}
3198
Felipe Balbi72246da2011-08-19 18:10:58 +03003199/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003200 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003201 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003202 *
3203 * Returns 0 on success otherwise negative errno.
3204 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003205int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003206{
Felipe Balbi6db38122016-10-03 11:27:01 +03003207 int ret;
3208 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003209
Felipe Balbi6db38122016-10-03 11:27:01 +03003210 irq = dwc3_gadget_get_irq(dwc);
3211 if (irq < 0) {
3212 ret = irq;
3213 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003214 }
3215
3216 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003217
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303218 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3219 sizeof(*dwc->ep0_trb) * 2,
3220 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003221 if (!dwc->ep0_trb) {
3222 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3223 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003224 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003225 }
3226
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003227 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003228 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003229 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003230 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003231 }
3232
Felipe Balbi905dc042017-01-05 14:46:52 +02003233 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3234 &dwc->bounce_addr, GFP_KERNEL);
3235 if (!dwc->bounce) {
3236 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003237 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003238 }
3239
Baolin Wangbb014732016-10-14 17:11:33 +08003240 init_completion(&dwc->ep0_in_setup);
3241
Felipe Balbi72246da2011-08-19 18:10:58 +03003242 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003243 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003244 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003245 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003246 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003247
3248 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003249 * FIXME We might be setting max_speed to <SUPER, however versions
3250 * <2.20a of dwc3 have an issue with metastability (documented
3251 * elsewhere in this driver) which tells us we can't set max speed to
3252 * anything lower than SUPER.
3253 *
3254 * Because gadget.max_speed is only used by composite.c and function
3255 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3256 * to happen so we avoid sending SuperSpeed Capability descriptor
3257 * together with our BOS descriptor as that could confuse host into
3258 * thinking we can handle super speed.
3259 *
3260 * Note that, in fact, we won't even support GetBOS requests when speed
3261 * is less than super speed because we don't have means, yet, to tell
3262 * composite.c that we are USB 2.0 + LPM ECN.
3263 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003264 if (dwc->revision < DWC3_REVISION_220A &&
3265 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003266 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003267 dwc->revision);
3268
3269 dwc->gadget.max_speed = dwc->maximum_speed;
3270
3271 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003272 * REVISIT: Here we should clear all pending IRQs to be
3273 * sure we're starting from a well known location.
3274 */
3275
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003276 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003277 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003278 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003279
Felipe Balbi72246da2011-08-19 18:10:58 +03003280 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3281 if (ret) {
3282 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003283 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003284 }
3285
3286 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003287
3288err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003289 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003290
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003291err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003292 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3293 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003294
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003295err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003296 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003297
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003298err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303299 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003300 dwc->ep0_trb, dwc->ep0_trb_addr);
3301
Felipe Balbi72246da2011-08-19 18:10:58 +03003302err0:
3303 return ret;
3304}
3305
Felipe Balbi7415f172012-04-30 14:56:33 +03003306/* -------------------------------------------------------------------------- */
3307
Felipe Balbi72246da2011-08-19 18:10:58 +03003308void dwc3_gadget_exit(struct dwc3 *dwc)
3309{
Felipe Balbi72246da2011-08-19 18:10:58 +03003310 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003311 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003312 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003313 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003314 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303315 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003316 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003317}
Felipe Balbi7415f172012-04-30 14:56:33 +03003318
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003319int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003320{
Roger Quadros9772b472016-04-12 11:33:29 +03003321 if (!dwc->gadget_driver)
3322 return 0;
3323
Roger Quadros1551e352017-02-15 14:16:26 +02003324 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003325 dwc3_disconnect_gadget(dwc);
3326 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003327
3328 return 0;
3329}
3330
3331int dwc3_gadget_resume(struct dwc3 *dwc)
3332{
Felipe Balbi7415f172012-04-30 14:56:33 +03003333 int ret;
3334
Roger Quadros9772b472016-04-12 11:33:29 +03003335 if (!dwc->gadget_driver)
3336 return 0;
3337
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003338 ret = __dwc3_gadget_start(dwc);
3339 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003340 goto err0;
3341
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003342 ret = dwc3_gadget_run_stop(dwc, true, false);
3343 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003344 goto err1;
3345
Felipe Balbi7415f172012-04-30 14:56:33 +03003346 return 0;
3347
3348err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003349 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003350
3351err0:
3352 return ret;
3353}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003354
3355void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3356{
3357 if (dwc->pending_events) {
3358 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3359 dwc->pending_events = false;
3360 enable_irq(dwc->irq_gadget);
3361 }
3362}