]> Git Repo - linux.git/blame - drivers/usb/dwc3/gadget.c
usb: dwc3: gadget: Correct handling of scattergather lists
[linux.git] / drivers / usb / dwc3 / gadget.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
bfad65ee 2/*
72246da4
FB
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
72246da4
FB
6 *
7 * Authors: Felipe Balbi <[email protected]>,
8 * Sebastian Andrzej Siewior <[email protected]>
72246da4
FB
9 */
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
80977dc9 25#include "debug.h"
72246da4
FB
26#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
04a9bfcd 30/**
bfad65ee 31 * dwc3_gadget_set_test_mode - enables usb2 test modes
04a9bfcd
FB
32 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
34 *
bfad65ee
FB
35 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
04a9bfcd
FB
37 */
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
911f1f88 62/**
bfad65ee 63 * dwc3_gadget_get_link_state - gets current state of usb link
911f1f88
PZ
64 * @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
8598bde7 78/**
bfad65ee 79 * dwc3_gadget_set_link_state - sets usb link to a particular state
8598bde7
FB
80 * @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
aee63e3c 84 * return 0 on success or -ETIMEDOUT.
8598bde7
FB
85 */
86int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
87{
aee63e3c 88 int retries = 10000;
8598bde7
FB
89 u32 reg;
90
802fde98
PZ
91 /*
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
8598bde7
FB
108 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
802fde98
PZ
115 /*
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
8598bde7 122 /* wait for a change in DSTS */
aed430e5 123 retries = 10000;
8598bde7
FB
124 while (--retries) {
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
126
8598bde7
FB
127 if (DWC3_DSTS_USBLNKST(reg) == state)
128 return 0;
129
aee63e3c 130 udelay(5);
8598bde7
FB
131 }
132
8598bde7
FB
133 return -ETIMEDOUT;
134}
135
dca0119c 136/**
bfad65ee
FB
137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
dca0119c
JY
139 *
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)
457e84b6 145{
dca0119c
JY
146 (*index)++;
147 if (*index == (DWC3_TRB_NUM - 1))
148 *index = 0;
ef966b9d 149}
457e84b6 150
bfad65ee
FB
151/**
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
154 */
dca0119c 155static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
ef966b9d 156{
dca0119c 157 dwc3_ep_inc_trb(&dep->trb_enqueue);
ef966b9d 158}
457e84b6 159
bfad65ee
FB
160/**
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
dca0119c 164static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
ef966b9d 165{
dca0119c 166 dwc3_ep_inc_trb(&dep->trb_dequeue);
457e84b6
FB
167}
168
c91815b5
FB
169void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
170 struct dwc3_request *req, int status)
72246da4
FB
171{
172 struct dwc3 *dwc = dep->dwc;
173
737f1ae2 174 req->started = false;
72246da4 175 list_del(&req->list);
e62c5bc5 176 req->remaining = 0;
72246da4
FB
177
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
180
4a71fcb8
JP
181 if (req->trb)
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
c91815b5 183 &req->request, req->direction);
4a71fcb8
JP
184
185 req->trb = NULL;
2c4cbe6e 186 trace_dwc3_gadget_giveback(req);
72246da4 187
c91815b5
FB
188 if (dep->number > 1)
189 pm_runtime_put(dwc->dev);
190}
191
192/**
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 */
202void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
203 int status)
204{
205 struct dwc3 *dwc = dep->dwc;
206
207 dwc3_gadget_del_and_unmap_request(dep, req, status);
208
72246da4 209 spin_unlock(&dwc->lock);
304f7e5e 210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
72246da4
FB
211 spin_lock(&dwc->lock);
212}
213
bfad65ee
FB
214/**
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 */
3ece0ec4 223int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
b09bb642
FB
224{
225 u32 timeout = 500;
71f7e702 226 int status = 0;
0fe886cd 227 int ret = 0;
b09bb642
FB
228 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)) {
71f7e702
FB
236 status = DWC3_DGCMD_STATUS(reg);
237 if (status)
0fe886cd
FB
238 ret = -EINVAL;
239 break;
b09bb642 240 }
e3aee486 241 } while (--timeout);
0fe886cd
FB
242
243 if (!timeout) {
0fe886cd 244 ret = -ETIMEDOUT;
71f7e702 245 status = -ETIMEDOUT;
0fe886cd
FB
246 }
247
71f7e702
FB
248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
249
0fe886cd 250 return ret;
b09bb642
FB
251}
252
c36d8e94
FB
253static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
254
bfad65ee
FB
255/**
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 */
2cd4718d
FB
264int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
72246da4 266{
8897a761 267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
2cd4718d 268 struct dwc3 *dwc = dep->dwc;
8722e095 269 u32 timeout = 1000;
72246da4
FB
270 u32 reg;
271
0933df15 272 int cmd_status = 0;
2b0f11df 273 int susphy = false;
c0ca324d 274 int ret = -EINVAL;
72246da4 275
2b0f11df
FB
276 /*
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 */
ab2a92e7
FB
284 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 }
2b0f11df
FB
291 }
292
5999914f 293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
c36d8e94
FB
294 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
2eb88016
FB
307 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);
72246da4 310
8897a761
FB
311 /*
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);
72246da4 333 do {
2eb88016 334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
72246da4 335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
0933df15 336 cmd_status = DWC3_DEPCMD_STATUS(reg);
7b9cc7a2 337
7b9cc7a2
KL
338 switch (cmd_status) {
339 case 0:
340 ret = 0;
341 break;
342 case DEPEVT_TRANSFER_NO_RESOURCE:
7b9cc7a2 343 ret = -EINVAL;
c0ca324d 344 break;
7b9cc7a2
KL
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 */
7b9cc7a2
KL
357 ret = -EAGAIN;
358 break;
359 default:
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
361 }
362
c0ca324d 363 break;
72246da4 364 }
f6bb225b 365 } while (--timeout);
72246da4 366
f6bb225b 367 if (timeout == 0) {
f6bb225b 368 ret = -ETIMEDOUT;
0933df15 369 cmd_status = -ETIMEDOUT;
f6bb225b 370 }
c0ca324d 371
0933df15
FB
372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
373
6cb2e4e3
FB
374 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
2b0f11df
FB
388 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
c0ca324d 394 return ret;
72246da4
FB
395}
396
50c763f8
JY
397static 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 */
5e6c88d2
LB
411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
50c763f8
JY
413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
414
415 memset(&params, 0, sizeof(params));
416
2cd4718d 417 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
50c763f8
JY
418}
419
72246da4 420static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
f6bafc6a 421 struct dwc3_trb *trb)
72246da4 422{
c439ef87 423 u32 offset = (char *) trb - (char *) dep->trb_pool;
72246da4
FB
424
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
d64ff406 435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
72246da4
FB
436 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
d64ff406 451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
72246da4
FB
452 dep->trb_pool, dep->trb_pool_dma);
453
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
456}
457
c4509601
JY
458static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
459
460/**
bfad65ee 461 * dwc3_gadget_start_config - configure ep resources
c4509601
JY
462 * @dwc: pointer to our controller context structure
463 * @dep: endpoint that is being enabled
464 *
bfad65ee
FB
465 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
466 * completion, it will set Transfer Resource for all available endpoints.
c4509601 467 *
bfad65ee
FB
468 * 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:
c4509601 474 *
bfad65ee
FB
475 * 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
c4509601
JY
480 * endpoint on alt setting (8.1.6).
481 *
482 * The following simplified method is used instead:
483 *
bfad65ee
FB
484 * 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
c4509601
JY
488 * guaranteed that there are as many transfer resources as endpoints.
489 *
bfad65ee
FB
490 * 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.
c4509601 493 */
72246da4
FB
494static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
495{
496 struct dwc3_gadget_ep_cmd_params params;
497 u32 cmd;
c4509601
JY
498 int i;
499 int ret;
500
501 if (dep->number)
502 return 0;
72246da4
FB
503
504 memset(&params, 0x00, sizeof(params));
c4509601 505 cmd = DWC3_DEPCMD_DEPSTARTCFG;
72246da4 506
2cd4718d 507 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
c4509601
JY
508 if (ret)
509 return ret;
510
511 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
512 struct dwc3_ep *dep = dwc->eps[i];
72246da4 513
c4509601
JY
514 if (!dep)
515 continue;
516
517 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
518 if (ret)
519 return ret;
72246da4
FB
520 }
521
522 return 0;
523}
524
525static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
21e64bf2 526 bool modify, bool restore)
72246da4 527{
39ebb05c
JY
528 const struct usb_ss_ep_comp_descriptor *comp_desc;
529 const struct usb_endpoint_descriptor *desc;
72246da4
FB
530 struct dwc3_gadget_ep_cmd_params params;
531
21e64bf2
FB
532 if (dev_WARN_ONCE(dwc->dev, modify && restore,
533 "Can't modify and restore\n"))
534 return -EINVAL;
535
39ebb05c
JY
536 comp_desc = dep->endpoint.comp_desc;
537 desc = dep->endpoint.desc;
538
72246da4
FB
539 memset(&params, 0x00, sizeof(params));
540
dc1c70a7 541 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
d2e9a13a
CP
542 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
543
544 /* Burst size is only needed in SuperSpeed mode */
ee5cd41c 545 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
676e3497 546 u32 burst = dep->endpoint.maxburst;
676e3497 547 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
d2e9a13a 548 }
72246da4 549
21e64bf2
FB
550 if (modify) {
551 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
552 } else if (restore) {
265b70a7
PZ
553 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
554 params.param2 |= dep->saved_state;
21e64bf2
FB
555 } else {
556 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
265b70a7
PZ
557 }
558
4bc48c97
FB
559 if (usb_endpoint_xfer_control(desc))
560 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
13fa2e69
FB
561
562 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
563 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
72246da4 564
18b7ede5 565 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
dc1c70a7
FB
566 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
567 | DWC3_DEPCFG_STREAM_EVENT_EN;
879631aa
FB
568 dep->stream_capable = true;
569 }
570
0b93a4c8 571 if (!usb_endpoint_xfer_control(desc))
dc1c70a7 572 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
72246da4
FB
573
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 */
dc1c70a7 580 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
72246da4
FB
581
582 /*
583 * We must use the lower 16 TX FIFOs even though
584 * HW might have more
585 */
586 if (dep->direction)
dc1c70a7 587 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
72246da4
FB
588
589 if (desc->bInterval) {
dc1c70a7 590 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
72246da4
FB
591 dep->interval = 1 << (desc->bInterval - 1);
592 }
593
2cd4718d 594 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
72246da4
FB
595}
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
dc1c70a7 603 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
72246da4 604
2cd4718d
FB
605 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
606 &params);
72246da4
FB
607}
608
609/**
bfad65ee 610 * __dwc3_gadget_ep_enable - initializes a hw endpoint
72246da4 611 * @dep: endpoint to be initialized
bfad65ee
FB
612 * @modify: if true, modify existing endpoint configuration
613 * @restore: if true, restore endpoint configuration from scratch buffer
72246da4 614 *
bfad65ee
FB
615 * 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.
72246da4
FB
617 */
618static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
21e64bf2 619 bool modify, bool restore)
72246da4 620{
39ebb05c 621 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
72246da4 622 struct dwc3 *dwc = dep->dwc;
39ebb05c 623
72246da4 624 u32 reg;
b09e99ee 625 int ret;
72246da4
FB
626
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
628 ret = dwc3_gadget_start_config(dwc, dep);
629 if (ret)
630 return ret;
631 }
632
39ebb05c 633 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
72246da4
FB
634 if (ret)
635 return ret;
636
637 if (!(dep->flags & DWC3_EP_ENABLED)) {
f6bafc6a
FB
638 struct dwc3_trb *trb_st_hw;
639 struct dwc3_trb *trb_link;
72246da4 640
72246da4
FB
641 dep->type = usb_endpoint_type(desc);
642 dep->flags |= DWC3_EP_ENABLED;
76a638f8 643 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
72246da4
FB
644
645 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
646 reg |= DWC3_DALEPENA_EP(dep->number);
647 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
648
76a638f8
BW
649 init_waitqueue_head(&dep->wait_end_transfer);
650
36b68aae 651 if (usb_endpoint_xfer_control(desc))
2870e501 652 goto out;
72246da4 653
0d25744a
JY
654 /* 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
36b68aae 660 /* Link TRB. The HWO bit is never reset */
72246da4
FB
661 trb_st_hw = &dep->trb_pool[0];
662
f6bafc6a 663 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
f6bafc6a
FB
664 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;
72246da4
FB
668 }
669
a97ea994
FB
670 /*
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
2870e501
FB
699
700out:
701 trace_dwc3_gadget_ep_enable(dep);
702
72246da4
FB
703 return 0;
704}
705
b992e681 706static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
624407f9 707static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
72246da4
FB
708{
709 struct dwc3_request *req;
710
0e146028 711 dwc3_stop_active_transfer(dwc, dep->number, true);
624407f9 712
0e146028
FB
713 /* - giveback all requests to gadget driver */
714 while (!list_empty(&dep->started_list)) {
715 req = next_request(&dep->started_list);
1591633e 716
0e146028 717 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
ea53b882
FB
718 }
719
aa3342c8
FB
720 while (!list_empty(&dep->pending_list)) {
721 req = next_request(&dep->pending_list);
72246da4 722
624407f9 723 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
72246da4 724 }
72246da4
FB
725}
726
727/**
bfad65ee 728 * __dwc3_gadget_ep_disable - disables a hw endpoint
72246da4
FB
729 * @dep: the endpoint to disable
730 *
bfad65ee
FB
731 * 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 *
624407f9 735 * Caller should take care of locking.
72246da4 736 */
72246da4
FB
737static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
738{
739 struct dwc3 *dwc = dep->dwc;
740 u32 reg;
741
2870e501 742 trace_dwc3_gadget_ep_disable(dep);
7eaeac5c 743
624407f9 744 dwc3_remove_requests(dwc, dep);
72246da4 745
687ef981
FB
746 /* make sure HW endpoint isn't stalled */
747 if (dep->flags & DWC3_EP_STALL)
7a608559 748 __dwc3_gadget_ep_set_halt(dep, 0, false);
687ef981 749
72246da4
FB
750 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
751 reg &= ~DWC3_DALEPENA_EP(dep->number);
752 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
753
879631aa 754 dep->stream_capable = false;
72246da4 755 dep->type = 0;
76a638f8 756 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
72246da4 757
39ebb05c
JY
758 /* 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
72246da4
FB
764 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
95ca961c
FB
803 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
804 "%s is already enabled\n",
805 dep->name))
c6f83f38 806 return 0;
c6f83f38 807
72246da4 808 spin_lock_irqsave(&dwc->lock, flags);
39ebb05c 809 ret = __dwc3_gadget_ep_enable(dep, false, false);
72246da4
FB
810 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
95ca961c
FB
830 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
831 "%s is already disabled\n",
832 dep->name))
72246da4 833 return 0;
72246da4 834
72246da4
FB
835 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);
72246da4
FB
847
848 req = kzalloc(sizeof(*req), gfp_flags);
734d5a53 849 if (!req)
72246da4 850 return NULL;
72246da4
FB
851
852 req->epnum = dep->number;
853 req->dep = dep;
72246da4 854
68d34c8a
FB
855 dep->allocated_requests++;
856
2c4cbe6e
FB
857 trace_dwc3_alloc_request(req);
858
72246da4
FB
859 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);
68d34c8a 866 struct dwc3_ep *dep = to_dwc3_ep(ep);
72246da4 867
68d34c8a 868 dep->allocated_requests--;
2c4cbe6e 869 trace_dwc3_free_request(req);
72246da4
FB
870 kfree(req);
871}
872
2c78c029
FB
873static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
874
e49d3cf4
FB
875static 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)
c71fc37c 878{
6b9018d4
FB
879 struct dwc3 *dwc = dep->dwc;
880 struct usb_gadget *gadget = &dwc->gadget;
881 enum usb_device_speed speed = gadget->speed;
c71fc37c 882
ef966b9d 883 dwc3_ep_inc_enq(dep);
e5ba5ec8 884
f6bafc6a
FB
885 trb->size = DWC3_TRB_SIZE_LENGTH(length);
886 trb->bpl = lower_32_bits(dma);
887 trb->bph = upper_32_bits(dma);
c71fc37c 888
16e78db7 889 switch (usb_endpoint_type(dep->endpoint.desc)) {
c71fc37c 890 case USB_ENDPOINT_XFER_CONTROL:
f6bafc6a 891 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
c71fc37c
FB
892 break;
893
894 case USB_ENDPOINT_XFER_ISOC:
6b9018d4 895 if (!node) {
e5ba5ec8 896 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
6b9018d4 897
40d829fb
MG
898 /*
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 */
6b9018d4
FB
920 if (speed == USB_SPEED_HIGH) {
921 struct usb_ep *ep = &dep->endpoint;
ec5bb87e 922 unsigned int mult = 2;
40d829fb
MG
923 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);
6b9018d4
FB
932 }
933 } else {
e5ba5ec8 934 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
6b9018d4 935 }
ca4d44ea
FB
936
937 /* always enable Interrupt on Missed ISOC */
938 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
c71fc37c
FB
939 break;
940
941 case USB_ENDPOINT_XFER_BULK:
942 case USB_ENDPOINT_XFER_INT:
f6bafc6a 943 trb->ctrl = DWC3_TRBCTL_NORMAL;
c71fc37c
FB
944 break;
945 default:
946 /*
947 * This is only possible with faulty memory because we
948 * checked it already :)
949 */
0a695d4c
FB
950 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
951 usb_endpoint_type(dep->endpoint.desc));
c71fc37c
FB
952 }
953
ca4d44ea 954 /* always enable Continue on Short Packet */
c9508c8c 955 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
58f29034 956 trb->ctrl |= DWC3_TRB_CTRL_CSP;
f3af3651 957
e49d3cf4 958 if (short_not_ok)
c9508c8c
FB
959 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
960 }
961
e49d3cf4 962 if ((!no_interrupt && !chain) ||
2c78c029 963 (dwc3_calc_trbs_left(dep) == 0))
c9508c8c 964 trb->ctrl |= DWC3_TRB_CTRL_IOC;
f3af3651 965
e5ba5ec8
PA
966 if (chain)
967 trb->ctrl |= DWC3_TRB_CTRL_CHN;
968
16e78db7 969 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
e49d3cf4 970 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
c71fc37c 971
f6bafc6a 972 trb->ctrl |= DWC3_TRB_CTRL_HWO;
2c4cbe6e
FB
973
974 trace_dwc3_prepare_trb(dep, trb);
c71fc37c
FB
975}
976
e49d3cf4
FB
977/**
978 * 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;
a31e63b6
AKV
988 unsigned int length;
989 dma_addr_t dma;
e49d3cf4
FB
990 unsigned stream_id = req->request.stream_id;
991 unsigned short_not_ok = req->request.short_not_ok;
992 unsigned no_interrupt = req->request.no_interrupt;
a31e63b6
AKV
993
994 if (req->request.num_sgs > 0) {
995 length = sg_dma_len(req->start_sg);
996 dma = sg_dma_address(req->start_sg);
997 } else {
998 length = req->request.length;
999 dma = req->request.dma;
1000 }
e49d3cf4
FB
1001
1002 trb = &dep->trb_pool[dep->trb_enqueue];
1003
1004 if (!req->trb) {
1005 dwc3_gadget_move_started_request(req);
1006 req->trb = trb;
1007 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1008 dep->queued_requests++;
1009 }
1010
1011 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1012 stream_id, short_not_ok, no_interrupt);
1013}
1014
361572b5 1015/**
bfad65ee 1016 * dwc3_ep_prev_trb - returns the previous TRB in the ring
361572b5
JY
1017 * @dep: The endpoint with the TRB ring
1018 * @index: The index of the current TRB in the ring
1019 *
1020 * Returns the TRB prior to the one pointed to by the index. If the
1021 * index is 0, we will wrap backwards, skip the link TRB, and return
1022 * the one just before that.
1023 */
1024static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1025{
45438a0c 1026 u8 tmp = index;
361572b5 1027
45438a0c
FB
1028 if (!tmp)
1029 tmp = DWC3_TRB_NUM - 1;
361572b5 1030
45438a0c 1031 return &dep->trb_pool[tmp - 1];
361572b5
JY
1032}
1033
c4233573
FB
1034static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1035{
1036 struct dwc3_trb *tmp;
32db3d94 1037 u8 trbs_left;
c4233573
FB
1038
1039 /*
1040 * If enqueue & dequeue are equal than it is either full or empty.
1041 *
1042 * One way to know for sure is if the TRB right before us has HWO bit
1043 * set or not. If it has, then we're definitely full and can't fit any
1044 * more transfers in our ring.
1045 */
1046 if (dep->trb_enqueue == dep->trb_dequeue) {
361572b5 1047 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
202adafe 1048 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
361572b5 1049 return 0;
c4233573
FB
1050
1051 return DWC3_TRB_NUM - 1;
1052 }
1053
9d7aba77 1054 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
3de2685f 1055 trbs_left &= (DWC3_TRB_NUM - 1);
32db3d94 1056
9d7aba77
JY
1057 if (dep->trb_dequeue < dep->trb_enqueue)
1058 trbs_left--;
1059
32db3d94 1060 return trbs_left;
c4233573
FB
1061}
1062
5ee85d89 1063static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
7ae7df49 1064 struct dwc3_request *req)
5ee85d89 1065{
a31e63b6 1066 struct scatterlist *sg = req->start_sg;
5ee85d89 1067 struct scatterlist *s;
5ee85d89
FB
1068 int i;
1069
1f512119 1070 for_each_sg(sg, s, req->num_pending_sgs, i) {
c6267a51
FB
1071 unsigned int length = req->request.length;
1072 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1073 unsigned int rem = length % maxp;
5ee85d89
FB
1074 unsigned chain = true;
1075
4bc48c97 1076 if (sg_is_last(s))
5ee85d89
FB
1077 chain = false;
1078
c6267a51
FB
1079 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1080 struct dwc3 *dwc = dep->dwc;
1081 struct dwc3_trb *trb;
1082
1083 req->unaligned = true;
1084
1085 /* prepare normal TRB */
1086 dwc3_prepare_one_trb(dep, req, true, i);
1087
1088 /* Now prepare one extra TRB to align transfer size */
1089 trb = &dep->trb_pool[dep->trb_enqueue];
1090 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1091 maxp - rem, false, 0,
1092 req->request.stream_id,
1093 req->request.short_not_ok,
1094 req->request.no_interrupt);
1095 } else {
1096 dwc3_prepare_one_trb(dep, req, chain, i);
1097 }
5ee85d89 1098
a31e63b6
AKV
1099 /*
1100 * There can be a situation where all sgs in sglist are not
1101 * queued because of insufficient trb number. To handle this
1102 * case, update start_sg to next sg to be queued, so that
1103 * we have free trbs we can continue queuing from where we
1104 * previously stopped
1105 */
1106 if (chain)
1107 req->start_sg = sg_next(s);
1108
7ae7df49 1109 if (!dwc3_calc_trbs_left(dep))
5ee85d89
FB
1110 break;
1111 }
1112}
1113
1114static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
7ae7df49 1115 struct dwc3_request *req)
5ee85d89 1116{
c6267a51
FB
1117 unsigned int length = req->request.length;
1118 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1119 unsigned int rem = length % maxp;
1120
1121 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1122 struct dwc3 *dwc = dep->dwc;
1123 struct dwc3_trb *trb;
1124
1125 req->unaligned = true;
1126
1127 /* prepare normal TRB */
1128 dwc3_prepare_one_trb(dep, req, true, 0);
1129
1130 /* Now prepare one extra TRB to align transfer size */
1131 trb = &dep->trb_pool[dep->trb_enqueue];
1132 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1133 false, 0, req->request.stream_id,
1134 req->request.short_not_ok,
1135 req->request.no_interrupt);
d6e5a549
FB
1136 } else if (req->request.zero && req->request.length &&
1137 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1138 struct dwc3 *dwc = dep->dwc;
1139 struct dwc3_trb *trb;
1140
1141 req->zero = true;
1142
1143 /* prepare normal TRB */
1144 dwc3_prepare_one_trb(dep, req, true, 0);
1145
1146 /* Now prepare one extra TRB to handle ZLP */
1147 trb = &dep->trb_pool[dep->trb_enqueue];
1148 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1149 false, 0, req->request.stream_id,
1150 req->request.short_not_ok,
1151 req->request.no_interrupt);
c6267a51
FB
1152 } else {
1153 dwc3_prepare_one_trb(dep, req, false, 0);
1154 }
5ee85d89
FB
1155}
1156
72246da4
FB
1157/*
1158 * dwc3_prepare_trbs - setup TRBs from requests
1159 * @dep: endpoint for which requests are being prepared
72246da4 1160 *
1d046793
PZ
1161 * The function goes through the requests list and sets up TRBs for the
1162 * transfers. The function returns once there are no more TRBs available or
1163 * it runs out of requests.
72246da4 1164 */
c4233573 1165static void dwc3_prepare_trbs(struct dwc3_ep *dep)
72246da4 1166{
68e823e2 1167 struct dwc3_request *req, *n;
72246da4
FB
1168
1169 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1170
d86c5a67
FB
1171 /*
1172 * We can get in a situation where there's a request in the started list
1173 * but there weren't enough TRBs to fully kick it in the first time
1174 * around, so it has been waiting for more TRBs to be freed up.
1175 *
1176 * In that case, we should check if we have a request with pending_sgs
1177 * in the started list and prepare TRBs for that request first,
1178 * otherwise we will prepare TRBs completely out of order and that will
1179 * break things.
1180 */
1181 list_for_each_entry(req, &dep->started_list, list) {
1182 if (req->num_pending_sgs > 0)
1183 dwc3_prepare_one_trb_sg(dep, req);
1184
1185 if (!dwc3_calc_trbs_left(dep))
1186 return;
1187 }
1188
aa3342c8 1189 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
cdb55b39
FB
1190 struct dwc3 *dwc = dep->dwc;
1191 int ret;
1192
1193 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1194 dep->direction);
1195 if (ret)
1196 return;
1197
1198 req->sg = req->request.sg;
a31e63b6 1199 req->start_sg = req->sg;
cdb55b39
FB
1200 req->num_pending_sgs = req->request.num_mapped_sgs;
1201
1f512119 1202 if (req->num_pending_sgs > 0)
7ae7df49 1203 dwc3_prepare_one_trb_sg(dep, req);
5ee85d89 1204 else
7ae7df49 1205 dwc3_prepare_one_trb_linear(dep, req);
72246da4 1206
7ae7df49 1207 if (!dwc3_calc_trbs_left(dep))
5ee85d89 1208 return;
72246da4 1209 }
72246da4
FB
1210}
1211
7fdca766 1212static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
72246da4
FB
1213{
1214 struct dwc3_gadget_ep_cmd_params params;
1215 struct dwc3_request *req;
4fae2e3e 1216 int starting;
72246da4
FB
1217 int ret;
1218 u32 cmd;
1219
ccb94ebf
FB
1220 if (!dwc3_calc_trbs_left(dep))
1221 return 0;
1222
4fae2e3e 1223 starting = !(dep->flags & DWC3_EP_BUSY);
72246da4 1224
4fae2e3e
FB
1225 dwc3_prepare_trbs(dep);
1226 req = next_request(&dep->started_list);
72246da4
FB
1227 if (!req) {
1228 dep->flags |= DWC3_EP_PENDING_REQUEST;
1229 return 0;
1230 }
1231
1232 memset(&params, 0, sizeof(params));
72246da4 1233
4fae2e3e 1234 if (starting) {
1877d6c9
PA
1235 params.param0 = upper_32_bits(req->trb_dma);
1236 params.param1 = lower_32_bits(req->trb_dma);
7fdca766
FB
1237 cmd = DWC3_DEPCMD_STARTTRANSFER;
1238
1239 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1240 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1877d6c9 1241 } else {
b6b1c6db
FB
1242 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1243 DWC3_DEPCMD_PARAM(dep->resource_index);
1877d6c9 1244 }
72246da4 1245
2cd4718d 1246 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
72246da4 1247 if (ret < 0) {
72246da4
FB
1248 /*
1249 * FIXME we need to iterate over the list of requests
1250 * here and stop, unmap, free and del each of the linked
1d046793 1251 * requests instead of what we do now.
72246da4 1252 */
ce3fc8b3
JD
1253 if (req->trb)
1254 memset(req->trb, 0, sizeof(struct dwc3_trb));
8ab89da4 1255 dep->queued_requests--;
c91815b5 1256 dwc3_gadget_del_and_unmap_request(dep, req, ret);
72246da4
FB
1257 return ret;
1258 }
1259
1260 dep->flags |= DWC3_EP_BUSY;
25b8ff68 1261
4fae2e3e 1262 if (starting) {
2eb88016 1263 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
b4996a86 1264 WARN_ON_ONCE(!dep->resource_index);
f898ae09 1265 }
25b8ff68 1266
72246da4
FB
1267 return 0;
1268}
1269
6cb2e4e3
FB
1270static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1271{
1272 u32 reg;
1273
1274 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1275 return DWC3_DSTS_SOFFN(reg);
1276}
1277
d6d6ec7b
PA
1278static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1279 struct dwc3_ep *dep, u32 cur_uf)
1280{
aa3342c8 1281 if (list_empty(&dep->pending_list)) {
5eb30ced 1282 dev_info(dwc->dev, "%s: ran out of requests\n",
73815280 1283 dep->name);
f4a53c55 1284 dep->flags |= DWC3_EP_PENDING_REQUEST;
d6d6ec7b
PA
1285 return;
1286 }
1287
af771d73
JY
1288 /*
1289 * Schedule the first trb for one interval in the future or at
1290 * least 4 microframes.
1291 */
502a37b9 1292 dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
7fdca766 1293 __dwc3_gadget_kick_transfer(dep);
d6d6ec7b
PA
1294}
1295
1296static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1297 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1298{
1299 u32 cur_uf, mask;
1300
1301 mask = ~(dep->interval - 1);
1302 cur_uf = event->parameters & mask;
1303
1304 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1305}
1306
72246da4
FB
1307static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1308{
0fc9a1be 1309 struct dwc3 *dwc = dep->dwc;
0fc9a1be 1310
bb423984 1311 if (!dep->endpoint.desc) {
5eb30ced
FB
1312 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1313 dep->name);
bb423984
FB
1314 return -ESHUTDOWN;
1315 }
1316
04fb365c
FB
1317 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1318 &req->request, req->dep->name))
bb423984 1319 return -EINVAL;
bb423984 1320
fc8bb91b
FB
1321 pm_runtime_get(dwc->dev);
1322
72246da4
FB
1323 req->request.actual = 0;
1324 req->request.status = -EINPROGRESS;
1325 req->direction = dep->direction;
1326 req->epnum = dep->number;
1327
fe84f522
FB
1328 trace_dwc3_ep_queue(req);
1329
aa3342c8 1330 list_add_tail(&req->list, &dep->pending_list);
72246da4 1331
d889c23c
FB
1332 /*
1333 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1334 * wait for a XferNotReady event so we will know what's the current
1335 * (micro-)frame number.
1336 *
1337 * Without this trick, we are very, very likely gonna get Bus Expiry
1338 * errors which will force us issue EndTransfer command.
1339 */
1340 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
6cb2e4e3
FB
1341 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1342 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1343 dwc3_stop_active_transfer(dwc, dep->number, true);
1344 dep->flags = DWC3_EP_ENABLED;
1345 } else {
1346 u32 cur_uf;
1347
1348 cur_uf = __dwc3_gadget_get_frame(dwc);
1349 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
87aba106 1350 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
6cb2e4e3 1351 }
f1d6826c 1352 return 0;
08a36b54 1353 }
f1d6826c
RQ
1354
1355 if ((dep->flags & DWC3_EP_BUSY) &&
64e01080
FB
1356 !(dep->flags & DWC3_EP_MISSED_ISOC))
1357 goto out;
72246da4 1358
594e121f 1359 return 0;
64e01080 1360 }
b997ada5 1361
f1d6826c 1362out:
7fdca766 1363 return __dwc3_gadget_kick_transfer(dep);
72246da4
FB
1364}
1365
1366static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1367 gfp_t gfp_flags)
1368{
1369 struct dwc3_request *req = to_dwc3_request(request);
1370 struct dwc3_ep *dep = to_dwc3_ep(ep);
1371 struct dwc3 *dwc = dep->dwc;
1372
1373 unsigned long flags;
1374
1375 int ret;
1376
fdee4eba 1377 spin_lock_irqsave(&dwc->lock, flags);
72246da4
FB
1378 ret = __dwc3_gadget_ep_queue(dep, req);
1379 spin_unlock_irqrestore(&dwc->lock, flags);
1380
1381 return ret;
1382}
1383
1384static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1385 struct usb_request *request)
1386{
1387 struct dwc3_request *req = to_dwc3_request(request);
1388 struct dwc3_request *r = NULL;
1389
1390 struct dwc3_ep *dep = to_dwc3_ep(ep);
1391 struct dwc3 *dwc = dep->dwc;
1392
1393 unsigned long flags;
1394 int ret = 0;
1395
2c4cbe6e
FB
1396 trace_dwc3_ep_dequeue(req);
1397
72246da4
FB
1398 spin_lock_irqsave(&dwc->lock, flags);
1399
aa3342c8 1400 list_for_each_entry(r, &dep->pending_list, list) {
72246da4
FB
1401 if (r == req)
1402 break;
1403 }
1404
1405 if (r != req) {
aa3342c8 1406 list_for_each_entry(r, &dep->started_list, list) {
72246da4
FB
1407 if (r == req)
1408 break;
1409 }
1410 if (r == req) {
1411 /* wait until it is processed */
b992e681 1412 dwc3_stop_active_transfer(dwc, dep->number, true);
cf3113d8
FB
1413
1414 /*
1415 * If request was already started, this means we had to
1416 * stop the transfer. With that we also need to ignore
1417 * all TRBs used by the request, however TRBs can only
1418 * be modified after completion of END_TRANSFER
1419 * command. So what we do here is that we wait for
1420 * END_TRANSFER completion and only after that, we jump
1421 * over TRBs by clearing HWO and incrementing dequeue
1422 * pointer.
1423 *
1424 * Note that we have 2 possible types of transfers here:
1425 *
1426 * i) Linear buffer request
1427 * ii) SG-list based request
1428 *
1429 * SG-list based requests will have r->num_pending_sgs
1430 * set to a valid number (> 0). Linear requests,
1431 * normally use a single TRB.
1432 *
1433 * For each of these two cases, if r->unaligned flag is
1434 * set, one extra TRB has been used to align transfer
1435 * size to wMaxPacketSize.
1436 *
1437 * All of these cases need to be taken into
1438 * consideration so we don't mess up our TRB ring
1439 * pointers.
1440 */
1441 wait_event_lock_irq(dep->wait_end_transfer,
1442 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1443 dwc->lock);
1444
1445 if (!r->trb)
1446 goto out1;
1447
1448 if (r->num_pending_sgs) {
1449 struct dwc3_trb *trb;
1450 int i = 0;
1451
1452 for (i = 0; i < r->num_pending_sgs; i++) {
1453 trb = r->trb + i;
1454 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1455 dwc3_ep_inc_deq(dep);
1456 }
1457
d6e5a549 1458 if (r->unaligned || r->zero) {
cf3113d8
FB
1459 trb = r->trb + r->num_pending_sgs + 1;
1460 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1461 dwc3_ep_inc_deq(dep);
1462 }
1463 } else {
1464 struct dwc3_trb *trb = r->trb;
1465
1466 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1467 dwc3_ep_inc_deq(dep);
1468
d6e5a549 1469 if (r->unaligned || r->zero) {
cf3113d8
FB
1470 trb = r->trb + 1;
1471 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1472 dwc3_ep_inc_deq(dep);
1473 }
1474 }
e8d4e8be 1475 goto out1;
72246da4 1476 }
04fb365c 1477 dev_err(dwc->dev, "request %pK was not queued to %s\n",
72246da4
FB
1478 request, ep->name);
1479 ret = -EINVAL;
1480 goto out0;
1481 }
1482
e8d4e8be 1483out1:
72246da4 1484 /* giveback the request */
cf3113d8 1485 dep->queued_requests--;
72246da4
FB
1486 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1487
1488out0:
1489 spin_unlock_irqrestore(&dwc->lock, flags);
1490
1491 return ret;
1492}
1493
7a608559 1494int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
72246da4
FB
1495{
1496 struct dwc3_gadget_ep_cmd_params params;
1497 struct dwc3 *dwc = dep->dwc;
1498 int ret;
1499
5ad02fb8
FB
1500 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1501 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1502 return -EINVAL;
1503 }
1504
72246da4
FB
1505 memset(&params, 0x00, sizeof(params));
1506
1507 if (value) {
69450c4d
FB
1508 struct dwc3_trb *trb;
1509
1510 unsigned transfer_in_flight;
1511 unsigned started;
1512
ffb80fc6
FB
1513 if (dep->flags & DWC3_EP_STALL)
1514 return 0;
1515
69450c4d
FB
1516 if (dep->number > 1)
1517 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1518 else
1519 trb = &dwc->ep0_trb[dep->trb_enqueue];
1520
1521 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1522 started = !list_empty(&dep->started_list);
1523
1524 if (!protocol && ((dep->direction && transfer_in_flight) ||
1525 (!dep->direction && started))) {
7a608559
FB
1526 return -EAGAIN;
1527 }
1528
2cd4718d
FB
1529 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1530 &params);
72246da4 1531 if (ret)
3f89204b 1532 dev_err(dwc->dev, "failed to set STALL on %s\n",
72246da4
FB
1533 dep->name);
1534 else
1535 dep->flags |= DWC3_EP_STALL;
1536 } else {
ffb80fc6
FB
1537 if (!(dep->flags & DWC3_EP_STALL))
1538 return 0;
2cd4718d 1539
50c763f8 1540 ret = dwc3_send_clear_stall_ep_cmd(dep);
72246da4 1541 if (ret)
3f89204b 1542 dev_err(dwc->dev, "failed to clear STALL on %s\n",
72246da4
FB
1543 dep->name);
1544 else
a535d81c 1545 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
72246da4 1546 }
5275455a 1547
72246da4
FB
1548 return ret;
1549}
1550
1551static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1552{
1553 struct dwc3_ep *dep = to_dwc3_ep(ep);
1554 struct dwc3 *dwc = dep->dwc;
1555
1556 unsigned long flags;
1557
1558 int ret;
1559
1560 spin_lock_irqsave(&dwc->lock, flags);
7a608559 1561 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
72246da4
FB
1562 spin_unlock_irqrestore(&dwc->lock, flags);
1563
1564 return ret;
1565}
1566
1567static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1568{
1569 struct dwc3_ep *dep = to_dwc3_ep(ep);
249a4569
PZ
1570 struct dwc3 *dwc = dep->dwc;
1571 unsigned long flags;
95aa4e8d 1572 int ret;
72246da4 1573
249a4569 1574 spin_lock_irqsave(&dwc->lock, flags);
72246da4
FB
1575 dep->flags |= DWC3_EP_WEDGE;
1576
08f0d966 1577 if (dep->number == 0 || dep->number == 1)
95aa4e8d 1578 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
08f0d966 1579 else
7a608559 1580 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
95aa4e8d
FB
1581 spin_unlock_irqrestore(&dwc->lock, flags);
1582
1583 return ret;
72246da4
FB
1584}
1585
1586/* -------------------------------------------------------------------------- */
1587
1588static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1589 .bLength = USB_DT_ENDPOINT_SIZE,
1590 .bDescriptorType = USB_DT_ENDPOINT,
1591 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1592};
1593
1594static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1595 .enable = dwc3_gadget_ep0_enable,
1596 .disable = dwc3_gadget_ep0_disable,
1597 .alloc_request = dwc3_gadget_ep_alloc_request,
1598 .free_request = dwc3_gadget_ep_free_request,
1599 .queue = dwc3_gadget_ep0_queue,
1600 .dequeue = dwc3_gadget_ep_dequeue,
08f0d966 1601 .set_halt = dwc3_gadget_ep0_set_halt,
72246da4
FB
1602 .set_wedge = dwc3_gadget_ep_set_wedge,
1603};
1604
1605static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1606 .enable = dwc3_gadget_ep_enable,
1607 .disable = dwc3_gadget_ep_disable,
1608 .alloc_request = dwc3_gadget_ep_alloc_request,
1609 .free_request = dwc3_gadget_ep_free_request,
1610 .queue = dwc3_gadget_ep_queue,
1611 .dequeue = dwc3_gadget_ep_dequeue,
1612 .set_halt = dwc3_gadget_ep_set_halt,
1613 .set_wedge = dwc3_gadget_ep_set_wedge,
1614};
1615
1616/* -------------------------------------------------------------------------- */
1617
1618static int dwc3_gadget_get_frame(struct usb_gadget *g)
1619{
1620 struct dwc3 *dwc = gadget_to_dwc(g);
72246da4 1621
6cb2e4e3 1622 return __dwc3_gadget_get_frame(dwc);
72246da4
FB
1623}
1624
218ef7b6 1625static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
72246da4 1626{
d6011f6f 1627 int retries;
72246da4 1628
218ef7b6 1629 int ret;
72246da4
FB
1630 u32 reg;
1631
72246da4
FB
1632 u8 link_state;
1633 u8 speed;
1634
72246da4
FB
1635 /*
1636 * According to the Databook Remote wakeup request should
1637 * be issued only when the device is in early suspend state.
1638 *
1639 * We can check that via USB Link State bits in DSTS register.
1640 */
1641 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1642
1643 speed = reg & DWC3_DSTS_CONNECTSPD;
ee5cd41c 1644 if ((speed == DWC3_DSTS_SUPERSPEED) ||
5eb30ced 1645 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
6b742899 1646 return 0;
72246da4
FB
1647
1648 link_state = DWC3_DSTS_USBLNKST(reg);
1649
1650 switch (link_state) {
1651 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1652 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1653 break;
1654 default:
218ef7b6 1655 return -EINVAL;
72246da4
FB
1656 }
1657
8598bde7
FB
1658 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1659 if (ret < 0) {
1660 dev_err(dwc->dev, "failed to put link in Recovery\n");
218ef7b6 1661 return ret;
8598bde7 1662 }
72246da4 1663
802fde98
PZ
1664 /* Recent versions do this automatically */
1665 if (dwc->revision < DWC3_REVISION_194A) {
1666 /* write zeroes to Link Change Request */
fcc023c7 1667 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
802fde98
PZ
1668 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1669 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1670 }
72246da4 1671
1d046793 1672 /* poll until Link State changes to ON */
d6011f6f 1673 retries = 20000;
72246da4 1674
d6011f6f 1675 while (retries--) {
72246da4
FB
1676 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1677
1678 /* in HS, means ON */
1679 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1680 break;
1681 }
1682
1683 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1684 dev_err(dwc->dev, "failed to send remote wakeup\n");
218ef7b6 1685 return -EINVAL;
72246da4
FB
1686 }
1687
218ef7b6
FB
1688 return 0;
1689}
1690
1691static int dwc3_gadget_wakeup(struct usb_gadget *g)
1692{
1693 struct dwc3 *dwc = gadget_to_dwc(g);
1694 unsigned long flags;
1695 int ret;
1696
1697 spin_lock_irqsave(&dwc->lock, flags);
1698 ret = __dwc3_gadget_wakeup(dwc);
72246da4
FB
1699 spin_unlock_irqrestore(&dwc->lock, flags);
1700
1701 return ret;
1702}
1703
1704static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1705 int is_selfpowered)
1706{
1707 struct dwc3 *dwc = gadget_to_dwc(g);
249a4569 1708 unsigned long flags;
72246da4 1709
249a4569 1710 spin_lock_irqsave(&dwc->lock, flags);
bcdea503 1711 g->is_selfpowered = !!is_selfpowered;
249a4569 1712 spin_unlock_irqrestore(&dwc->lock, flags);
72246da4
FB
1713
1714 return 0;
1715}
1716
7b2a0368 1717static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
72246da4
FB
1718{
1719 u32 reg;
61d58242 1720 u32 timeout = 500;
72246da4 1721
fc8bb91b
FB
1722 if (pm_runtime_suspended(dwc->dev))
1723 return 0;
1724
72246da4 1725 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
8db7ed15 1726 if (is_on) {
802fde98
PZ
1727 if (dwc->revision <= DWC3_REVISION_187A) {
1728 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1729 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1730 }
1731
1732 if (dwc->revision >= DWC3_REVISION_194A)
1733 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1734 reg |= DWC3_DCTL_RUN_STOP;
7b2a0368
FB
1735
1736 if (dwc->has_hibernation)
1737 reg |= DWC3_DCTL_KEEP_CONNECT;
1738
9fcb3bd8 1739 dwc->pullups_connected = true;
8db7ed15 1740 } else {
72246da4 1741 reg &= ~DWC3_DCTL_RUN_STOP;
7b2a0368
FB
1742
1743 if (dwc->has_hibernation && !suspend)
1744 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1745
9fcb3bd8 1746 dwc->pullups_connected = false;
8db7ed15 1747 }
72246da4
FB
1748
1749 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1750
1751 do {
1752 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
b6d4e16e
FB
1753 reg &= DWC3_DSTS_DEVCTRLHLT;
1754 } while (--timeout && !(!is_on ^ !reg));
f2df679b
FB
1755
1756 if (!timeout)
1757 return -ETIMEDOUT;
72246da4 1758
6f17f74b 1759 return 0;
72246da4
FB
1760}
1761
1762static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1763{
1764 struct dwc3 *dwc = gadget_to_dwc(g);
1765 unsigned long flags;
6f17f74b 1766 int ret;
72246da4
FB
1767
1768 is_on = !!is_on;
1769
bb014736
BW
1770 /*
1771 * Per databook, when we want to stop the gadget, if a control transfer
1772 * is still in process, complete it and get the core into setup phase.
1773 */
1774 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1775 reinit_completion(&dwc->ep0_in_setup);
1776
1777 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1778 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1779 if (ret == 0) {
1780 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1781 return -ETIMEDOUT;
1782 }
1783 }
1784
72246da4 1785 spin_lock_irqsave(&dwc->lock, flags);
7b2a0368 1786 ret = dwc3_gadget_run_stop(dwc, is_on, false);
72246da4
FB
1787 spin_unlock_irqrestore(&dwc->lock, flags);
1788
6f17f74b 1789 return ret;
72246da4
FB
1790}
1791
8698e2ac
FB
1792static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1793{
1794 u32 reg;
1795
1796 /* Enable all but Start and End of Frame IRQs */
1797 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1798 DWC3_DEVTEN_EVNTOVERFLOWEN |
1799 DWC3_DEVTEN_CMDCMPLTEN |
1800 DWC3_DEVTEN_ERRTICERREN |
1801 DWC3_DEVTEN_WKUPEVTEN |
8698e2ac
FB
1802 DWC3_DEVTEN_CONNECTDONEEN |
1803 DWC3_DEVTEN_USBRSTEN |
1804 DWC3_DEVTEN_DISCONNEVTEN);
1805
799e9dc8
FB
1806 if (dwc->revision < DWC3_REVISION_250A)
1807 reg |= DWC3_DEVTEN_ULSTCNGEN;
1808
8698e2ac
FB
1809 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1810}
1811
1812static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1813{
1814 /* mask all interrupts */
1815 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1816}
1817
1818static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
b15a762f 1819static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
8698e2ac 1820
4e99472b 1821/**
bfad65ee
FB
1822 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1823 * @dwc: pointer to our context structure
4e99472b
FB
1824 *
1825 * The following looks like complex but it's actually very simple. In order to
1826 * calculate the number of packets we can burst at once on OUT transfers, we're
1827 * gonna use RxFIFO size.
1828 *
1829 * To calculate RxFIFO size we need two numbers:
1830 * MDWIDTH = size, in bits, of the internal memory bus
1831 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1832 *
1833 * Given these two numbers, the formula is simple:
1834 *
1835 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1836 *
1837 * 24 bytes is for 3x SETUP packets
1838 * 16 bytes is a clock domain crossing tolerance
1839 *
1840 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1841 */
1842static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1843{
1844 u32 ram2_depth;
1845 u32 mdwidth;
1846 u32 nump;
1847 u32 reg;
1848
1849 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1850 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1851
1852 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1853 nump = min_t(u32, nump, 16);
1854
1855 /* update NumP */
1856 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1857 reg &= ~DWC3_DCFG_NUMP_MASK;
1858 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1859 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1860}
1861
d7be2952 1862static int __dwc3_gadget_start(struct dwc3 *dwc)
72246da4 1863{
72246da4 1864 struct dwc3_ep *dep;
72246da4
FB
1865 int ret = 0;
1866 u32 reg;
1867
cf40b86b
JY
1868 /*
1869 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1870 * the core supports IMOD, disable it.
1871 */
1872 if (dwc->imod_interval) {
1873 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1874 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1875 } else if (dwc3_has_imod(dwc)) {
1876 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1877 }
1878
2a58f9c1
FB
1879 /*
1880 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1881 * field instead of letting dwc3 itself calculate that automatically.
1882 *
1883 * This way, we maximize the chances that we'll be able to get several
1884 * bursts of data without going through any sort of endpoint throttling.
1885 */
1886 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
01b0e2cc
TN
1887 if (dwc3_is_usb31(dwc))
1888 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1889 else
1890 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1891
2a58f9c1
FB
1892 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1893
4e99472b
FB
1894 dwc3_gadget_setup_nump(dwc);
1895
72246da4
FB
1896 /* Start with SuperSpeed Default */
1897 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1898
1899 dep = dwc->eps[0];
39ebb05c 1900 ret = __dwc3_gadget_ep_enable(dep, false, false);
72246da4
FB
1901 if (ret) {
1902 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
d7be2952 1903 goto err0;
72246da4
FB
1904 }
1905
1906 dep = dwc->eps[1];
39ebb05c 1907 ret = __dwc3_gadget_ep_enable(dep, false, false);
72246da4
FB
1908 if (ret) {
1909 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
d7be2952 1910 goto err1;
72246da4
FB
1911 }
1912
1913 /* begin to receive SETUP packets */
c7fcdeb2 1914 dwc->ep0state = EP0_SETUP_PHASE;
72246da4
FB
1915 dwc3_ep0_out_start(dwc);
1916
8698e2ac
FB
1917 dwc3_gadget_enable_irq(dwc);
1918
72246da4
FB
1919 return 0;
1920
b0d7ffd4 1921err1:
d7be2952 1922 __dwc3_gadget_ep_disable(dwc->eps[0]);
b0d7ffd4
FB
1923
1924err0:
72246da4
FB
1925 return ret;
1926}
1927
d7be2952
FB
1928static int dwc3_gadget_start(struct usb_gadget *g,
1929 struct usb_gadget_driver *driver)
72246da4
FB
1930{
1931 struct dwc3 *dwc = gadget_to_dwc(g);
1932 unsigned long flags;
d7be2952 1933 int ret = 0;
8698e2ac 1934 int irq;
72246da4 1935
9522def4 1936 irq = dwc->irq_gadget;
d7be2952
FB
1937 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1938 IRQF_SHARED, "dwc3", dwc->ev_buf);
1939 if (ret) {
1940 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1941 irq, ret);
1942 goto err0;
1943 }
1944
72246da4 1945 spin_lock_irqsave(&dwc->lock, flags);
d7be2952
FB
1946 if (dwc->gadget_driver) {
1947 dev_err(dwc->dev, "%s is already bound to %s\n",
1948 dwc->gadget.name,
1949 dwc->gadget_driver->driver.name);
1950 ret = -EBUSY;
1951 goto err1;
1952 }
1953
1954 dwc->gadget_driver = driver;
1955
fc8bb91b
FB
1956 if (pm_runtime_active(dwc->dev))
1957 __dwc3_gadget_start(dwc);
1958
d7be2952
FB
1959 spin_unlock_irqrestore(&dwc->lock, flags);
1960
1961 return 0;
1962
1963err1:
1964 spin_unlock_irqrestore(&dwc->lock, flags);
1965 free_irq(irq, dwc);
1966
1967err0:
1968 return ret;
1969}
72246da4 1970
d7be2952
FB
1971static void __dwc3_gadget_stop(struct dwc3 *dwc)
1972{
8698e2ac 1973 dwc3_gadget_disable_irq(dwc);
72246da4
FB
1974 __dwc3_gadget_ep_disable(dwc->eps[0]);
1975 __dwc3_gadget_ep_disable(dwc->eps[1]);
d7be2952 1976}
72246da4 1977
d7be2952
FB
1978static int dwc3_gadget_stop(struct usb_gadget *g)
1979{
1980 struct dwc3 *dwc = gadget_to_dwc(g);
1981 unsigned long flags;
76a638f8 1982 int epnum;
498f0478 1983 u32 tmo_eps = 0;
72246da4 1984
d7be2952 1985 spin_lock_irqsave(&dwc->lock, flags);
76a638f8
BW
1986
1987 if (pm_runtime_suspended(dwc->dev))
1988 goto out;
1989
d7be2952 1990 __dwc3_gadget_stop(dwc);
76a638f8
BW
1991
1992 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1993 struct dwc3_ep *dep = dwc->eps[epnum];
498f0478 1994 int ret;
76a638f8
BW
1995
1996 if (!dep)
1997 continue;
1998
1999 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2000 continue;
2001
498f0478
RQ
2002 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
2003 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
2004 dwc->lock, msecs_to_jiffies(5));
2005
2006 if (ret <= 0) {
2007 /* Timed out or interrupted! There's nothing much
2008 * we can do so we just log here and print which
2009 * endpoints timed out at the end.
2010 */
2011 tmo_eps |= 1 << epnum;
2012 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
2013 }
2014 }
2015
2016 if (tmo_eps) {
2017 dev_err(dwc->dev,
2018 "end transfer timed out on endpoints 0x%x [bitmap]\n",
2019 tmo_eps);
76a638f8
BW
2020 }
2021
2022out:
d7be2952 2023 dwc->gadget_driver = NULL;
72246da4
FB
2024 spin_unlock_irqrestore(&dwc->lock, flags);
2025
3f308d17 2026 free_irq(dwc->irq_gadget, dwc->ev_buf);
b0d7ffd4 2027
72246da4
FB
2028 return 0;
2029}
802fde98 2030
7d8d0639
FB
2031static void dwc3_gadget_set_speed(struct usb_gadget *g,
2032 enum usb_device_speed speed)
2033{
2034 struct dwc3 *dwc = gadget_to_dwc(g);
2035 unsigned long flags;
2036 u32 reg;
2037
2038 spin_lock_irqsave(&dwc->lock, flags);
2039 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2040 reg &= ~(DWC3_DCFG_SPEED_MASK);
2041
2042 /*
2043 * WORKAROUND: DWC3 revision < 2.20a have an issue
2044 * which would cause metastability state on Run/Stop
2045 * bit if we try to force the IP to USB2-only mode.
2046 *
2047 * Because of that, we cannot configure the IP to any
2048 * speed other than the SuperSpeed
2049 *
2050 * Refers to:
2051 *
2052 * STAR#9000525659: Clock Domain Crossing on DCTL in
2053 * USB 2.0 Mode
2054 */
42bf02ec
RQ
2055 if (dwc->revision < DWC3_REVISION_220A &&
2056 !dwc->dis_metastability_quirk) {
7d8d0639
FB
2057 reg |= DWC3_DCFG_SUPERSPEED;
2058 } else {
2059 switch (speed) {
2060 case USB_SPEED_LOW:
2061 reg |= DWC3_DCFG_LOWSPEED;
2062 break;
2063 case USB_SPEED_FULL:
2064 reg |= DWC3_DCFG_FULLSPEED;
2065 break;
2066 case USB_SPEED_HIGH:
2067 reg |= DWC3_DCFG_HIGHSPEED;
2068 break;
2069 case USB_SPEED_SUPER:
2070 reg |= DWC3_DCFG_SUPERSPEED;
2071 break;
2072 case USB_SPEED_SUPER_PLUS:
2f3090c6
TN
2073 if (dwc3_is_usb31(dwc))
2074 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2075 else
2076 reg |= DWC3_DCFG_SUPERSPEED;
7d8d0639
FB
2077 break;
2078 default:
2079 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2080
2081 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2082 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2083 else
2084 reg |= DWC3_DCFG_SUPERSPEED;
2085 }
2086 }
2087 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2088
2089 spin_unlock_irqrestore(&dwc->lock, flags);
2090}
2091
72246da4
FB
2092static const struct usb_gadget_ops dwc3_gadget_ops = {
2093 .get_frame = dwc3_gadget_get_frame,
2094 .wakeup = dwc3_gadget_wakeup,
2095 .set_selfpowered = dwc3_gadget_set_selfpowered,
2096 .pullup = dwc3_gadget_pullup,
2097 .udc_start = dwc3_gadget_start,
2098 .udc_stop = dwc3_gadget_stop,
7d8d0639 2099 .udc_set_speed = dwc3_gadget_set_speed,
72246da4
FB
2100};
2101
2102/* -------------------------------------------------------------------------- */
2103
46b780d4 2104static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
72246da4
FB
2105{
2106 struct dwc3_ep *dep;
47d3946e 2107 u8 epnum;
72246da4 2108
f3bcfc7e
BD
2109 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2110
46b780d4 2111 for (epnum = 0; epnum < total; epnum++) {
47d3946e 2112 bool direction = epnum & 1;
46b780d4 2113 u8 num = epnum >> 1;
72246da4 2114
72246da4 2115 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
734d5a53 2116 if (!dep)
72246da4 2117 return -ENOMEM;
72246da4
FB
2118
2119 dep->dwc = dwc;
2120 dep->number = epnum;
47d3946e 2121 dep->direction = direction;
2eb88016 2122 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
72246da4
FB
2123 dwc->eps[epnum] = dep;
2124
46b780d4 2125 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
47d3946e 2126 direction ? "in" : "out");
6a1e3ef4 2127
72246da4 2128 dep->endpoint.name = dep->name;
39ebb05c
JY
2129
2130 if (!(dep->number > 1)) {
2131 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2132 dep->endpoint.comp_desc = NULL;
2133 }
2134
74674cbf 2135 spin_lock_init(&dep->lock);
72246da4 2136
46b780d4 2137 if (num == 0) {
e117e742 2138 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
6048e4c6 2139 dep->endpoint.maxburst = 1;
72246da4 2140 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
46b780d4 2141 if (!direction)
72246da4 2142 dwc->gadget.ep0 = &dep->endpoint;
28781789
FB
2143 } else if (direction) {
2144 int mdwidth;
46b780d4 2145 int kbytes;
28781789
FB
2146 int size;
2147 int ret;
28781789
FB
2148
2149 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2150 /* MDWIDTH is represented in bits, we need it in bytes */
2151 mdwidth /= 8;
2152
46b780d4 2153 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
d548a617
TN
2154 if (dwc3_is_usb31(dwc))
2155 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2156 else
2157 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
28781789
FB
2158
2159 /* FIFO Depth is in MDWDITH bytes. Multiply */
2160 size *= mdwidth;
2161
46b780d4
AS
2162 kbytes = size / 1024;
2163 if (kbytes == 0)
2164 kbytes = 1;
28781789
FB
2165
2166 /*
46b780d4 2167 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
28781789
FB
2168 * internal overhead. We don't really know how these are used,
2169 * but documentation say it exists.
2170 */
46b780d4
AS
2171 size -= mdwidth * (kbytes + 1);
2172 size /= kbytes;
28781789
FB
2173
2174 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2175
2176 dep->endpoint.max_streams = 15;
2177 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2178 list_add_tail(&dep->endpoint.ep_list,
2179 &dwc->gadget.ep_list);
2180
2181 ret = dwc3_alloc_trb_pool(dep);
2182 if (ret)
2183 return ret;
72246da4
FB
2184 } else {
2185 int ret;
2186
e117e742 2187 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
12d36c16 2188 dep->endpoint.max_streams = 15;
72246da4
FB
2189 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2190 list_add_tail(&dep->endpoint.ep_list,
2191 &dwc->gadget.ep_list);
2192
2193 ret = dwc3_alloc_trb_pool(dep);
25b8ff68 2194 if (ret)
72246da4 2195 return ret;
72246da4 2196 }
25b8ff68 2197
46b780d4 2198 if (num == 0) {
a474d3b7
RB
2199 dep->endpoint.caps.type_control = true;
2200 } else {
2201 dep->endpoint.caps.type_iso = true;
2202 dep->endpoint.caps.type_bulk = true;
2203 dep->endpoint.caps.type_int = true;
2204 }
2205
47d3946e 2206 dep->endpoint.caps.dir_in = direction;
a474d3b7
RB
2207 dep->endpoint.caps.dir_out = !direction;
2208
aa3342c8
FB
2209 INIT_LIST_HEAD(&dep->pending_list);
2210 INIT_LIST_HEAD(&dep->started_list);
72246da4
FB
2211 }
2212
2213 return 0;
2214}
2215
2216static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2217{
2218 struct dwc3_ep *dep;
2219 u8 epnum;
2220
2221 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2222 dep = dwc->eps[epnum];
6a1e3ef4
FB
2223 if (!dep)
2224 continue;
5bf8fae3
GC
2225 /*
2226 * Physical endpoints 0 and 1 are special; they form the
2227 * bi-directional USB endpoint 0.
2228 *
2229 * For those two physical endpoints, we don't allocate a TRB
2230 * pool nor do we add them the endpoints list. Due to that, we
2231 * shouldn't do these two operations otherwise we would end up
2232 * with all sorts of bugs when removing dwc3.ko.
2233 */
2234 if (epnum != 0 && epnum != 1) {
2235 dwc3_free_trb_pool(dep);
72246da4 2236 list_del(&dep->endpoint.ep_list);
5bf8fae3 2237 }
72246da4
FB
2238
2239 kfree(dep);
2240 }
2241}
2242
72246da4 2243/* -------------------------------------------------------------------------- */
e5caff68 2244
e5ba5ec8
PA
2245static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2246 struct dwc3_request *req, struct dwc3_trb *trb,
e5b36ae2
FB
2247 const struct dwc3_event_depevt *event, int status,
2248 int chain)
72246da4 2249{
72246da4
FB
2250 unsigned int count;
2251 unsigned int s_pkt = 0;
d6d6ec7b 2252 unsigned int trb_status;
72246da4 2253
dc55c67e 2254 dwc3_ep_inc_deq(dep);
a9c3ca5f
FB
2255
2256 if (req->trb == trb)
2257 dep->queued_requests--;
2258
2c4cbe6e
FB
2259 trace_dwc3_complete_trb(dep, trb);
2260
e5b36ae2
FB
2261 /*
2262 * If we're in the middle of series of chained TRBs and we
2263 * receive a short transfer along the way, DWC3 will skip
2264 * through all TRBs including the last TRB in the chain (the
2265 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2266 * bit and SW has to do it manually.
2267 *
2268 * We're going to do that here to avoid problems of HW trying
2269 * to use bogus TRBs for transfers.
2270 */
2271 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2272 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2273
c6267a51
FB
2274 /*
2275 * If we're dealing with unaligned size OUT transfer, we will be left
2276 * with one TRB pending in the ring. We need to manually clear HWO bit
2277 * from that TRB.
2278 */
d6e5a549 2279 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
c6267a51
FB
2280 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2281 return 1;
2282 }
2283
e5ba5ec8 2284 count = trb->size & DWC3_TRB_SIZE_MASK;
e62c5bc5 2285 req->remaining += count;
e5ba5ec8 2286
35b2719e
FB
2287 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2288 return 1;
2289
e5ba5ec8
PA
2290 if (dep->direction) {
2291 if (count) {
2292 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2293 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
e5ba5ec8
PA
2294 /*
2295 * If missed isoc occurred and there is
2296 * no request queued then issue END
2297 * TRANSFER, so that core generates
2298 * next xfernotready and we will issue
2299 * a fresh START TRANSFER.
2300 * If there are still queued request
2301 * then wait, do not issue either END
2302 * or UPDATE TRANSFER, just attach next
aa3342c8 2303 * request in pending_list during
e5ba5ec8
PA
2304 * giveback.If any future queued request
2305 * is successfully transferred then we
2306 * will issue UPDATE TRANSFER for all
aa3342c8 2307 * request in the pending_list.
e5ba5ec8
PA
2308 */
2309 dep->flags |= DWC3_EP_MISSED_ISOC;
2310 } else {
2311 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2312 dep->name);
2313 status = -ECONNRESET;
2314 }
2315 } else {
2316 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2317 }
2318 } else {
2319 if (count && (event->status & DEPEVT_STATUS_SHORT))
2320 s_pkt = 1;
2321 }
2322
7c705dfe 2323 if (s_pkt && !chain)
e5ba5ec8 2324 return 1;
f99f53f2 2325
e5ba5ec8
PA
2326 if ((event->status & DEPEVT_STATUS_IOC) &&
2327 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2328 return 1;
f99f53f2 2329
e5ba5ec8
PA
2330 return 0;
2331}
2332
2333static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2334 const struct dwc3_event_depevt *event, int status)
2335{
31162af4 2336 struct dwc3_request *req, *n;
e5ba5ec8 2337 struct dwc3_trb *trb;
d6e10bf2 2338 bool ioc = false;
e62c5bc5 2339 int ret = 0;
e5ba5ec8 2340
31162af4 2341 list_for_each_entry_safe(req, n, &dep->started_list, list) {
1f512119 2342 unsigned length;
e5b36ae2
FB
2343 int chain;
2344
1f512119
FB
2345 length = req->request.length;
2346 chain = req->num_pending_sgs > 0;
31162af4 2347 if (chain) {
1f512119 2348 struct scatterlist *sg = req->sg;
31162af4 2349 struct scatterlist *s;
1f512119 2350 unsigned int pending = req->num_pending_sgs;
31162af4 2351 unsigned int i;
c7de5734 2352
1f512119 2353 for_each_sg(sg, s, pending, i) {
31162af4 2354 trb = &dep->trb_pool[dep->trb_dequeue];
31162af4 2355
7282c4ef
FB
2356 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2357 break;
2358
1f512119
FB
2359 req->sg = sg_next(s);
2360 req->num_pending_sgs--;
2361
31162af4
FB
2362 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2363 event, status, chain);
1f512119
FB
2364 if (ret)
2365 break;
31162af4
FB
2366 }
2367 } else {
737f1ae2 2368 trb = &dep->trb_pool[dep->trb_dequeue];
d115d705 2369 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
e5b36ae2 2370 event, status, chain);
31162af4 2371 }
d115d705 2372
d6e5a549 2373 if (req->unaligned || req->zero) {
c6267a51
FB
2374 trb = &dep->trb_pool[dep->trb_dequeue];
2375 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2376 event, status, false);
2377 req->unaligned = false;
d6e5a549 2378 req->zero = false;
c6267a51
FB
2379 }
2380
e62c5bc5 2381 req->request.actual = length - req->remaining;
1f512119 2382
ff377ae4 2383 if ((req->request.actual < length) && req->num_pending_sgs)
7fdca766 2384 return __dwc3_gadget_kick_transfer(dep);
1f512119 2385
d115d705 2386 dwc3_gadget_giveback(dep, req, status);
e5ba5ec8 2387
d6e10bf2
AB
2388 if (ret) {
2389 if ((event->status & DEPEVT_STATUS_IOC) &&
2390 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2391 ioc = true;
72246da4 2392 break;
d6e10bf2 2393 }
31162af4 2394 }
72246da4 2395
4cb42217
FB
2396 /*
2397 * Our endpoint might get disabled by another thread during
2398 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2399 * early on so DWC3_EP_BUSY flag gets cleared
2400 */
2401 if (!dep->endpoint.desc)
2402 return 1;
2403
cdc359dd 2404 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
aa3342c8
FB
2405 list_empty(&dep->started_list)) {
2406 if (list_empty(&dep->pending_list)) {
cdc359dd
PA
2407 /*
2408 * If there is no entry in request list then do
2409 * not issue END TRANSFER now. Just set PENDING
2410 * flag, so that END TRANSFER is issued when an
2411 * entry is added into request list.
2412 */
2413 dep->flags = DWC3_EP_PENDING_REQUEST;
2414 } else {
b992e681 2415 dwc3_stop_active_transfer(dwc, dep->number, true);
cdc359dd
PA
2416 dep->flags = DWC3_EP_ENABLED;
2417 }
7efea86c
PA
2418 return 1;
2419 }
2420
d6e10bf2
AB
2421 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2422 return 0;
2423
72246da4
FB
2424 return 1;
2425}
2426
2427static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
029d97ff 2428 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
72246da4
FB
2429{
2430 unsigned status = 0;
2431 int clean_busy;
e18b7975
FB
2432 u32 is_xfer_complete;
2433
2434 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
72246da4
FB
2435
2436 if (event->status & DEPEVT_STATUS_BUSERR)
2437 status = -ECONNRESET;
2438
1d046793 2439 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
4cb42217 2440 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
e18b7975 2441 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
72246da4 2442 dep->flags &= ~DWC3_EP_BUSY;
fae2b904
FB
2443
2444 /*
2445 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2446 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2447 */
2448 if (dwc->revision < DWC3_REVISION_183A) {
2449 u32 reg;
2450 int i;
2451
2452 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
348e026f 2453 dep = dwc->eps[i];
fae2b904
FB
2454
2455 if (!(dep->flags & DWC3_EP_ENABLED))
2456 continue;
2457
aa3342c8 2458 if (!list_empty(&dep->started_list))
fae2b904
FB
2459 return;
2460 }
2461
2462 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2463 reg |= dwc->u1u2;
2464 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2465
2466 dwc->u1u2 = 0;
2467 }
8a1a9c9e 2468
4cb42217
FB
2469 /*
2470 * Our endpoint might get disabled by another thread during
2471 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2472 * early on so DWC3_EP_BUSY flag gets cleared
2473 */
2474 if (!dep->endpoint.desc)
2475 return;
2476
7fdca766
FB
2477 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
2478 __dwc3_gadget_kick_transfer(dep);
72246da4
FB
2479}
2480
72246da4
FB
2481static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2482 const struct dwc3_event_depevt *event)
2483{
2484 struct dwc3_ep *dep;
2485 u8 epnum = event->endpoint_number;
76a638f8 2486 u8 cmd;
72246da4
FB
2487
2488 dep = dwc->eps[epnum];
2489
d7fd41c6
JD
2490 if (!(dep->flags & DWC3_EP_ENABLED)) {
2491 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2492 return;
2493
2494 /* Handle only EPCMDCMPLT when EP disabled */
2495 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2496 return;
2497 }
3336abb5 2498
72246da4
FB
2499 if (epnum == 0 || epnum == 1) {
2500 dwc3_ep0_interrupt(dwc, event);
2501 return;
2502 }
2503
2504 switch (event->endpoint_event) {
2505 case DWC3_DEPEVT_XFERCOMPLETE:
b4996a86 2506 dep->resource_index = 0;
c2df85ca 2507
16e78db7 2508 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
8566cd1a 2509 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
72246da4
FB
2510 return;
2511 }
2512
029d97ff 2513 dwc3_endpoint_transfer_complete(dwc, dep, event);
72246da4
FB
2514 break;
2515 case DWC3_DEPEVT_XFERINPROGRESS:
029d97ff 2516 dwc3_endpoint_transfer_complete(dwc, dep, event);
72246da4
FB
2517 break;
2518 case DWC3_DEPEVT_XFERNOTREADY:
7fdca766 2519 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
72246da4 2520 dwc3_gadget_start_isoc(dwc, dep, event);
7fdca766
FB
2521 else
2522 __dwc3_gadget_kick_transfer(dep);
72246da4 2523
879631aa
FB
2524 break;
2525 case DWC3_DEPEVT_STREAMEVT:
16e78db7 2526 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
879631aa
FB
2527 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2528 dep->name);
2529 return;
2530 }
72246da4 2531 break;
72246da4 2532 case DWC3_DEPEVT_EPCMDCMPLT:
76a638f8
BW
2533 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2534
2535 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2536 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2537 wake_up(&dep->wait_end_transfer);
2538 }
2539 break;
2540 case DWC3_DEPEVT_RXTXFIFOEVT:
72246da4
FB
2541 break;
2542 }
2543}
2544
2545static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2546{
2547 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2548 spin_unlock(&dwc->lock);
2549 dwc->gadget_driver->disconnect(&dwc->gadget);
2550 spin_lock(&dwc->lock);
2551 }
2552}
2553
bc5ba2e0
FB
2554static void dwc3_suspend_gadget(struct dwc3 *dwc)
2555{
73a30bfc 2556 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
bc5ba2e0
FB
2557 spin_unlock(&dwc->lock);
2558 dwc->gadget_driver->suspend(&dwc->gadget);
2559 spin_lock(&dwc->lock);
2560 }
2561}
2562
2563static void dwc3_resume_gadget(struct dwc3 *dwc)
2564{
73a30bfc 2565 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
bc5ba2e0
FB
2566 spin_unlock(&dwc->lock);
2567 dwc->gadget_driver->resume(&dwc->gadget);
5c7b3b02 2568 spin_lock(&dwc->lock);
8e74475b
FB
2569 }
2570}
2571
2572static void dwc3_reset_gadget(struct dwc3 *dwc)
2573{
2574 if (!dwc->gadget_driver)
2575 return;
2576
2577 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2578 spin_unlock(&dwc->lock);
2579 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
bc5ba2e0
FB
2580 spin_lock(&dwc->lock);
2581 }
2582}
2583
b992e681 2584static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
72246da4
FB
2585{
2586 struct dwc3_ep *dep;
2587 struct dwc3_gadget_ep_cmd_params params;
2588 u32 cmd;
2589 int ret;
2590
2591 dep = dwc->eps[epnum];
2592
76a638f8
BW
2593 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2594 !dep->resource_index)
3daf74d7
PA
2595 return;
2596
57911504
PA
2597 /*
2598 * NOTICE: We are violating what the Databook says about the
2599 * EndTransfer command. Ideally we would _always_ wait for the
2600 * EndTransfer Command Completion IRQ, but that's causing too
2601 * much trouble synchronizing between us and gadget driver.
2602 *
2603 * We have discussed this with the IP Provider and it was
2604 * suggested to giveback all requests here, but give HW some
2605 * extra time to synchronize with the interconnect. We're using
dc93b41a 2606 * an arbitrary 100us delay for that.
57911504
PA
2607 *
2608 * Note also that a similar handling was tested by Synopsys
2609 * (thanks a lot Paul) and nothing bad has come out of it.
2610 * In short, what we're doing is:
2611 *
2612 * - Issue EndTransfer WITH CMDIOC bit set
2613 * - Wait 100us
06281d46
JY
2614 *
2615 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2616 * supports a mode to work around the above limitation. The
2617 * software can poll the CMDACT bit in the DEPCMD register
2618 * after issuing a EndTransfer command. This mode is enabled
2619 * by writing GUCTL2[14]. This polling is already done in the
2620 * dwc3_send_gadget_ep_cmd() function so if the mode is
2621 * enabled, the EndTransfer command will have completed upon
2622 * returning from this function and we don't need to delay for
2623 * 100us.
2624 *
2625 * This mode is NOT available on the DWC_usb31 IP.
57911504
PA
2626 */
2627
3daf74d7 2628 cmd = DWC3_DEPCMD_ENDTRANSFER;
b992e681
PZ
2629 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2630 cmd |= DWC3_DEPCMD_CMDIOC;
b4996a86 2631 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3daf74d7 2632 memset(&params, 0, sizeof(params));
2cd4718d 2633 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
3daf74d7 2634 WARN_ON_ONCE(ret);
b4996a86 2635 dep->resource_index = 0;
041d81f4 2636 dep->flags &= ~DWC3_EP_BUSY;
06281d46 2637
76a638f8
BW
2638 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2639 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
06281d46 2640 udelay(100);
76a638f8 2641 }
72246da4
FB
2642}
2643
72246da4
FB
2644static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2645{
2646 u32 epnum;
2647
2648 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2649 struct dwc3_ep *dep;
72246da4
FB
2650 int ret;
2651
2652 dep = dwc->eps[epnum];
6a1e3ef4
FB
2653 if (!dep)
2654 continue;
72246da4
FB
2655
2656 if (!(dep->flags & DWC3_EP_STALL))
2657 continue;
2658
2659 dep->flags &= ~DWC3_EP_STALL;
2660
50c763f8 2661 ret = dwc3_send_clear_stall_ep_cmd(dep);
72246da4
FB
2662 WARN_ON_ONCE(ret);
2663 }
2664}
2665
2666static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2667{
c4430a26
FB
2668 int reg;
2669
72246da4
FB
2670 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2671 reg &= ~DWC3_DCTL_INITU1ENA;
2672 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2673
2674 reg &= ~DWC3_DCTL_INITU2ENA;
2675 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72246da4 2676
72246da4
FB
2677 dwc3_disconnect_gadget(dwc);
2678
2679 dwc->gadget.speed = USB_SPEED_UNKNOWN;
df62df56 2680 dwc->setup_packet_pending = false;
06a374ed 2681 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
fc8bb91b
FB
2682
2683 dwc->connected = false;
72246da4
FB
2684}
2685
72246da4
FB
2686static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2687{
2688 u32 reg;
2689
fc8bb91b
FB
2690 dwc->connected = true;
2691
df62df56
FB
2692 /*
2693 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2694 * would cause a missing Disconnect Event if there's a
2695 * pending Setup Packet in the FIFO.
2696 *
2697 * There's no suggested workaround on the official Bug
2698 * report, which states that "unless the driver/application
2699 * is doing any special handling of a disconnect event,
2700 * there is no functional issue".
2701 *
2702 * Unfortunately, it turns out that we _do_ some special
2703 * handling of a disconnect event, namely complete all
2704 * pending transfers, notify gadget driver of the
2705 * disconnection, and so on.
2706 *
2707 * Our suggested workaround is to follow the Disconnect
2708 * Event steps here, instead, based on a setup_packet_pending
b5d335e5
FB
2709 * flag. Such flag gets set whenever we have a SETUP_PENDING
2710 * status for EP0 TRBs and gets cleared on XferComplete for the
df62df56
FB
2711 * same endpoint.
2712 *
2713 * Refers to:
2714 *
2715 * STAR#9000466709: RTL: Device : Disconnect event not
2716 * generated if setup packet pending in FIFO
2717 */
2718 if (dwc->revision < DWC3_REVISION_188A) {
2719 if (dwc->setup_packet_pending)
2720 dwc3_gadget_disconnect_interrupt(dwc);
2721 }
2722
8e74475b 2723 dwc3_reset_gadget(dwc);
72246da4
FB
2724
2725 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2726 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2727 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3b637367 2728 dwc->test_mode = false;
72246da4
FB
2729 dwc3_clear_stall_all_ep(dwc);
2730
2731 /* Reset device address to zero */
2732 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2733 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2734 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
72246da4
FB
2735}
2736
72246da4
FB
2737static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2738{
72246da4
FB
2739 struct dwc3_ep *dep;
2740 int ret;
2741 u32 reg;
2742 u8 speed;
2743
72246da4
FB
2744 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2745 speed = reg & DWC3_DSTS_CONNECTSPD;
2746 dwc->speed = speed;
2747
5fb6fdaf
JY
2748 /*
2749 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2750 * each time on Connect Done.
2751 *
2752 * Currently we always use the reset value. If any platform
2753 * wants to set this to a different value, we need to add a
2754 * setting and update GCTL.RAMCLKSEL here.
2755 */
72246da4
FB
2756
2757 switch (speed) {
2da9ad76 2758 case DWC3_DSTS_SUPERSPEED_PLUS:
7580862b
JY
2759 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2760 dwc->gadget.ep0->maxpacket = 512;
2761 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2762 break;
2da9ad76 2763 case DWC3_DSTS_SUPERSPEED:
05870c5b
FB
2764 /*
2765 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2766 * would cause a missing USB3 Reset event.
2767 *
2768 * In such situations, we should force a USB3 Reset
2769 * event by calling our dwc3_gadget_reset_interrupt()
2770 * routine.
2771 *
2772 * Refers to:
2773 *
2774 * STAR#9000483510: RTL: SS : USB3 reset event may
2775 * not be generated always when the link enters poll
2776 */
2777 if (dwc->revision < DWC3_REVISION_190A)
2778 dwc3_gadget_reset_interrupt(dwc);
2779
72246da4
FB
2780 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2781 dwc->gadget.ep0->maxpacket = 512;
2782 dwc->gadget.speed = USB_SPEED_SUPER;
2783 break;
2da9ad76 2784 case DWC3_DSTS_HIGHSPEED:
72246da4
FB
2785 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2786 dwc->gadget.ep0->maxpacket = 64;
2787 dwc->gadget.speed = USB_SPEED_HIGH;
2788 break;
9418ee15 2789 case DWC3_DSTS_FULLSPEED:
72246da4
FB
2790 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2791 dwc->gadget.ep0->maxpacket = 64;
2792 dwc->gadget.speed = USB_SPEED_FULL;
2793 break;
2da9ad76 2794 case DWC3_DSTS_LOWSPEED:
72246da4
FB
2795 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2796 dwc->gadget.ep0->maxpacket = 8;
2797 dwc->gadget.speed = USB_SPEED_LOW;
2798 break;
2799 }
2800
61800263
TN
2801 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2802
2b758350
PA
2803 /* Enable USB2 LPM Capability */
2804
ee5cd41c 2805 if ((dwc->revision > DWC3_REVISION_194A) &&
2da9ad76
JY
2806 (speed != DWC3_DSTS_SUPERSPEED) &&
2807 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2b758350
PA
2808 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2809 reg |= DWC3_DCFG_LPM_CAP;
2810 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2811
2812 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2813 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2814
460d098c 2815 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2b758350 2816
80caf7d2
HR
2817 /*
2818 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2819 * DCFG.LPMCap is set, core responses with an ACK and the
2820 * BESL value in the LPM token is less than or equal to LPM
2821 * NYET threshold.
2822 */
2823 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2824 && dwc->has_lpm_erratum,
9165dabb 2825 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
80caf7d2
HR
2826
2827 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2828 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2829
356363bf
FB
2830 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2831 } else {
2832 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2833 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2b758350
PA
2834 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2835 }
2836
72246da4 2837 dep = dwc->eps[0];
39ebb05c 2838 ret = __dwc3_gadget_ep_enable(dep, true, false);
72246da4
FB
2839 if (ret) {
2840 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2841 return;
2842 }
2843
2844 dep = dwc->eps[1];
39ebb05c 2845 ret = __dwc3_gadget_ep_enable(dep, true, false);
72246da4
FB
2846 if (ret) {
2847 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2848 return;
2849 }
2850
2851 /*
2852 * Configure PHY via GUSB3PIPECTLn if required.
2853 *
2854 * Update GTXFIFOSIZn
2855 *
2856 * In both cases reset values should be sufficient.
2857 */
2858}
2859
2860static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2861{
72246da4
FB
2862 /*
2863 * TODO take core out of low power mode when that's
2864 * implemented.
2865 */
2866
ad14d4e0
JL
2867 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2868 spin_unlock(&dwc->lock);
2869 dwc->gadget_driver->resume(&dwc->gadget);
2870 spin_lock(&dwc->lock);
2871 }
72246da4
FB
2872}
2873
2874static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2875 unsigned int evtinfo)
2876{
fae2b904 2877 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
0b0cc1cd
FB
2878 unsigned int pwropt;
2879
2880 /*
2881 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2882 * Hibernation mode enabled which would show up when device detects
2883 * host-initiated U3 exit.
2884 *
2885 * In that case, device will generate a Link State Change Interrupt
2886 * from U3 to RESUME which is only necessary if Hibernation is
2887 * configured in.
2888 *
2889 * There are no functional changes due to such spurious event and we
2890 * just need to ignore it.
2891 *
2892 * Refers to:
2893 *
2894 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2895 * operational mode
2896 */
2897 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2898 if ((dwc->revision < DWC3_REVISION_250A) &&
2899 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2900 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2901 (next == DWC3_LINK_STATE_RESUME)) {
0b0cc1cd
FB
2902 return;
2903 }
2904 }
fae2b904
FB
2905
2906 /*
2907 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2908 * on the link partner, the USB session might do multiple entry/exit
2909 * of low power states before a transfer takes place.
2910 *
2911 * Due to this problem, we might experience lower throughput. The
2912 * suggested workaround is to disable DCTL[12:9] bits if we're
2913 * transitioning from U1/U2 to U0 and enable those bits again
2914 * after a transfer completes and there are no pending transfers
2915 * on any of the enabled endpoints.
2916 *
2917 * This is the first half of that workaround.
2918 *
2919 * Refers to:
2920 *
2921 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2922 * core send LGO_Ux entering U0
2923 */
2924 if (dwc->revision < DWC3_REVISION_183A) {
2925 if (next == DWC3_LINK_STATE_U0) {
2926 u32 u1u2;
2927 u32 reg;
2928
2929 switch (dwc->link_state) {
2930 case DWC3_LINK_STATE_U1:
2931 case DWC3_LINK_STATE_U2:
2932 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2933 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2934 | DWC3_DCTL_ACCEPTU2ENA
2935 | DWC3_DCTL_INITU1ENA
2936 | DWC3_DCTL_ACCEPTU1ENA);
2937
2938 if (!dwc->u1u2)
2939 dwc->u1u2 = reg & u1u2;
2940
2941 reg &= ~u1u2;
2942
2943 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2944 break;
2945 default:
2946 /* do nothing */
2947 break;
2948 }
2949 }
2950 }
2951
bc5ba2e0
FB
2952 switch (next) {
2953 case DWC3_LINK_STATE_U1:
2954 if (dwc->speed == USB_SPEED_SUPER)
2955 dwc3_suspend_gadget(dwc);
2956 break;
2957 case DWC3_LINK_STATE_U2:
2958 case DWC3_LINK_STATE_U3:
2959 dwc3_suspend_gadget(dwc);
2960 break;
2961 case DWC3_LINK_STATE_RESUME:
2962 dwc3_resume_gadget(dwc);
2963 break;
2964 default:
2965 /* do nothing */
2966 break;
2967 }
2968
e57ebc1d 2969 dwc->link_state = next;
72246da4
FB
2970}
2971
72704f87
BW
2972static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2973 unsigned int evtinfo)
2974{
2975 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2976
2977 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2978 dwc3_suspend_gadget(dwc);
2979
2980 dwc->link_state = next;
2981}
2982
e1dadd3b
FB
2983static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2984 unsigned int evtinfo)
2985{
2986 unsigned int is_ss = evtinfo & BIT(4);
2987
bfad65ee 2988 /*
e1dadd3b
FB
2989 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2990 * have a known issue which can cause USB CV TD.9.23 to fail
2991 * randomly.
2992 *
2993 * Because of this issue, core could generate bogus hibernation
2994 * events which SW needs to ignore.
2995 *
2996 * Refers to:
2997 *
2998 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2999 * Device Fallback from SuperSpeed
3000 */
3001 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3002 return;
3003
3004 /* enter hibernation here */
3005}
3006
72246da4
FB
3007static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3008 const struct dwc3_event_devt *event)
3009{
3010 switch (event->type) {
3011 case DWC3_DEVICE_EVENT_DISCONNECT:
3012 dwc3_gadget_disconnect_interrupt(dwc);
3013 break;
3014 case DWC3_DEVICE_EVENT_RESET:
3015 dwc3_gadget_reset_interrupt(dwc);
3016 break;
3017 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3018 dwc3_gadget_conndone_interrupt(dwc);
3019 break;
3020 case DWC3_DEVICE_EVENT_WAKEUP:
3021 dwc3_gadget_wakeup_interrupt(dwc);
3022 break;
e1dadd3b
FB
3023 case DWC3_DEVICE_EVENT_HIBER_REQ:
3024 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3025 "unexpected hibernation event\n"))
3026 break;
3027
3028 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3029 break;
72246da4
FB
3030 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3031 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3032 break;
3033 case DWC3_DEVICE_EVENT_EOPF:
72704f87 3034 /* It changed to be suspend event for version 2.30a and above */
5eb30ced 3035 if (dwc->revision >= DWC3_REVISION_230A) {
72704f87
BW
3036 /*
3037 * Ignore suspend event until the gadget enters into
3038 * USB_STATE_CONFIGURED state.
3039 */
3040 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3041 dwc3_gadget_suspend_interrupt(dwc,
3042 event->event_info);
3043 }
72246da4
FB
3044 break;
3045 case DWC3_DEVICE_EVENT_SOF:
72246da4 3046 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
72246da4 3047 case DWC3_DEVICE_EVENT_CMD_CMPL:
72246da4 3048 case DWC3_DEVICE_EVENT_OVERFLOW:
72246da4
FB
3049 break;
3050 default:
e9f2aa87 3051 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
72246da4
FB
3052 }
3053}
3054
3055static void dwc3_process_event_entry(struct dwc3 *dwc,
3056 const union dwc3_event *event)
3057{
43c96be1 3058 trace_dwc3_event(event->raw, dwc);
2c4cbe6e 3059
dfc5e805
FB
3060 if (!event->type.is_devspec)
3061 dwc3_endpoint_interrupt(dwc, &event->depevt);
3062 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
72246da4 3063 dwc3_gadget_interrupt(dwc, &event->devt);
dfc5e805 3064 else
72246da4 3065 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
72246da4
FB
3066}
3067
dea520a4 3068static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
b15a762f 3069{
dea520a4 3070 struct dwc3 *dwc = evt->dwc;
b15a762f 3071 irqreturn_t ret = IRQ_NONE;
f42f2447 3072 int left;
e8adfc30 3073 u32 reg;
b15a762f 3074
f42f2447 3075 left = evt->count;
b15a762f 3076
f42f2447
FB
3077 if (!(evt->flags & DWC3_EVENT_PENDING))
3078 return IRQ_NONE;
b15a762f 3079
f42f2447
FB
3080 while (left > 0) {
3081 union dwc3_event event;
b15a762f 3082
ebbb2d59 3083 event.raw = *(u32 *) (evt->cache + evt->lpos);
b15a762f 3084
f42f2447 3085 dwc3_process_event_entry(dwc, &event);
b15a762f 3086
f42f2447
FB
3087 /*
3088 * FIXME we wrap around correctly to the next entry as
3089 * almost all entries are 4 bytes in size. There is one
3090 * entry which has 12 bytes which is a regular entry
3091 * followed by 8 bytes data. ATM I don't know how
3092 * things are organized if we get next to the a
3093 * boundary so I worry about that once we try to handle
3094 * that.
3095 */
caefe6c7 3096 evt->lpos = (evt->lpos + 4) % evt->length;
f42f2447 3097 left -= 4;
f42f2447 3098 }
b15a762f 3099
f42f2447
FB
3100 evt->count = 0;
3101 evt->flags &= ~DWC3_EVENT_PENDING;
3102 ret = IRQ_HANDLED;
b15a762f 3103
f42f2447 3104 /* Unmask interrupt */
660e9bde 3105 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
f42f2447 3106 reg &= ~DWC3_GEVNTSIZ_INTMASK;
660e9bde 3107 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
b15a762f 3108
cf40b86b
JY
3109 if (dwc->imod_interval) {
3110 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3111 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3112 }
3113
f42f2447
FB
3114 return ret;
3115}
e8adfc30 3116
dea520a4 3117static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
f42f2447 3118{
dea520a4
FB
3119 struct dwc3_event_buffer *evt = _evt;
3120 struct dwc3 *dwc = evt->dwc;
e5f68b4a 3121 unsigned long flags;
f42f2447 3122 irqreturn_t ret = IRQ_NONE;
f42f2447 3123
e5f68b4a 3124 spin_lock_irqsave(&dwc->lock, flags);
dea520a4 3125 ret = dwc3_process_event_buf(evt);
e5f68b4a 3126 spin_unlock_irqrestore(&dwc->lock, flags);
b15a762f
FB
3127
3128 return ret;
3129}
3130
dea520a4 3131static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
72246da4 3132{
dea520a4 3133 struct dwc3 *dwc = evt->dwc;
ebbb2d59 3134 u32 amount;
72246da4 3135 u32 count;
e8adfc30 3136 u32 reg;
72246da4 3137
fc8bb91b
FB
3138 if (pm_runtime_suspended(dwc->dev)) {
3139 pm_runtime_get(dwc->dev);
3140 disable_irq_nosync(dwc->irq_gadget);
3141 dwc->pending_events = true;
3142 return IRQ_HANDLED;
3143 }
3144
d325a1de
TN
3145 /*
3146 * With PCIe legacy interrupt, test shows that top-half irq handler can
3147 * be called again after HW interrupt deassertion. Check if bottom-half
3148 * irq event handler completes before caching new event to prevent
3149 * losing events.
3150 */
3151 if (evt->flags & DWC3_EVENT_PENDING)
3152 return IRQ_HANDLED;
3153
660e9bde 3154 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
72246da4
FB
3155 count &= DWC3_GEVNTCOUNT_MASK;
3156 if (!count)
3157 return IRQ_NONE;
3158
b15a762f
FB
3159 evt->count = count;
3160 evt->flags |= DWC3_EVENT_PENDING;
72246da4 3161
e8adfc30 3162 /* Mask interrupt */
660e9bde 3163 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
e8adfc30 3164 reg |= DWC3_GEVNTSIZ_INTMASK;
660e9bde 3165 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
e8adfc30 3166
ebbb2d59
JY
3167 amount = min(count, evt->length - evt->lpos);
3168 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3169
3170 if (amount < count)
3171 memcpy(evt->cache, evt->buf, count - amount);
3172
65aca320
JY
3173 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3174
b15a762f 3175 return IRQ_WAKE_THREAD;
72246da4
FB
3176}
3177
dea520a4 3178static irqreturn_t dwc3_interrupt(int irq, void *_evt)
72246da4 3179{
dea520a4 3180 struct dwc3_event_buffer *evt = _evt;
72246da4 3181
dea520a4 3182 return dwc3_check_event_buf(evt);
72246da4
FB
3183}
3184
6db3812e
FB
3185static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3186{
3187 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3188 int irq;
3189
3190 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3191 if (irq > 0)
3192 goto out;
3193
3194 if (irq == -EPROBE_DEFER)
3195 goto out;
3196
3197 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3198 if (irq > 0)
3199 goto out;
3200
3201 if (irq == -EPROBE_DEFER)
3202 goto out;
3203
3204 irq = platform_get_irq(dwc3_pdev, 0);
3205 if (irq > 0)
3206 goto out;
3207
3208 if (irq != -EPROBE_DEFER)
3209 dev_err(dwc->dev, "missing peripheral IRQ\n");
3210
3211 if (!irq)
3212 irq = -EINVAL;
3213
3214out:
3215 return irq;
3216}
3217
72246da4 3218/**
bfad65ee 3219 * dwc3_gadget_init - initializes gadget related registers
1d046793 3220 * @dwc: pointer to our controller context structure
72246da4
FB
3221 *
3222 * Returns 0 on success otherwise negative errno.
3223 */
41ac7b3a 3224int dwc3_gadget_init(struct dwc3 *dwc)
72246da4 3225{
6db3812e
FB
3226 int ret;
3227 int irq;
9522def4 3228
6db3812e
FB
3229 irq = dwc3_gadget_get_irq(dwc);
3230 if (irq < 0) {
3231 ret = irq;
3232 goto err0;
9522def4
RQ
3233 }
3234
3235 dwc->irq_gadget = irq;
72246da4 3236
d64ff406
AB
3237 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3238 sizeof(*dwc->ep0_trb) * 2,
3239 &dwc->ep0_trb_addr, GFP_KERNEL);
72246da4
FB
3240 if (!dwc->ep0_trb) {
3241 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3242 ret = -ENOMEM;
7d5e650a 3243 goto err0;
72246da4
FB
3244 }
3245
4199c5f8 3246 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
72246da4 3247 if (!dwc->setup_buf) {
72246da4 3248 ret = -ENOMEM;
7d5e650a 3249 goto err1;
72246da4
FB
3250 }
3251
905dc04e
FB
3252 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3253 &dwc->bounce_addr, GFP_KERNEL);
3254 if (!dwc->bounce) {
3255 ret = -ENOMEM;
d6e5a549 3256 goto err2;
905dc04e
FB
3257 }
3258
bb014736
BW
3259 init_completion(&dwc->ep0_in_setup);
3260
72246da4 3261 dwc->gadget.ops = &dwc3_gadget_ops;
72246da4 3262 dwc->gadget.speed = USB_SPEED_UNKNOWN;
eeb720fb 3263 dwc->gadget.sg_supported = true;
72246da4 3264 dwc->gadget.name = "dwc3-gadget";
6a4290cc 3265 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
72246da4 3266
b9e51b2b
BM
3267 /*
3268 * FIXME We might be setting max_speed to <SUPER, however versions
3269 * <2.20a of dwc3 have an issue with metastability (documented
3270 * elsewhere in this driver) which tells us we can't set max speed to
3271 * anything lower than SUPER.
3272 *
3273 * Because gadget.max_speed is only used by composite.c and function
3274 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3275 * to happen so we avoid sending SuperSpeed Capability descriptor
3276 * together with our BOS descriptor as that could confuse host into
3277 * thinking we can handle super speed.
3278 *
3279 * Note that, in fact, we won't even support GetBOS requests when speed
3280 * is less than super speed because we don't have means, yet, to tell
3281 * composite.c that we are USB 2.0 + LPM ECN.
3282 */
42bf02ec
RQ
3283 if (dwc->revision < DWC3_REVISION_220A &&
3284 !dwc->dis_metastability_quirk)
5eb30ced 3285 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
b9e51b2b
BM
3286 dwc->revision);
3287
3288 dwc->gadget.max_speed = dwc->maximum_speed;
3289
72246da4
FB
3290 /*
3291 * REVISIT: Here we should clear all pending IRQs to be
3292 * sure we're starting from a well known location.
3293 */
3294
f3bcfc7e 3295 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
72246da4 3296 if (ret)
d6e5a549 3297 goto err3;
72246da4 3298
72246da4
FB
3299 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3300 if (ret) {
3301 dev_err(dwc->dev, "failed to register udc\n");
d6e5a549 3302 goto err4;
72246da4
FB
3303 }
3304
3305 return 0;
3306
7d5e650a 3307err4:
d6e5a549 3308 dwc3_gadget_free_endpoints(dwc);
04c03d10 3309
7d5e650a 3310err3:
d6e5a549
FB
3311 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3312 dwc->bounce_addr);
5812b1c2 3313
7d5e650a 3314err2:
0fc9a1be 3315 kfree(dwc->setup_buf);
72246da4 3316
7d5e650a 3317err1:
d64ff406 3318 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
72246da4
FB
3319 dwc->ep0_trb, dwc->ep0_trb_addr);
3320
72246da4
FB
3321err0:
3322 return ret;
3323}
3324
7415f17c
FB
3325/* -------------------------------------------------------------------------- */
3326
72246da4
FB
3327void dwc3_gadget_exit(struct dwc3 *dwc)
3328{
72246da4 3329 usb_del_gadget_udc(&dwc->gadget);
72246da4 3330 dwc3_gadget_free_endpoints(dwc);
905dc04e 3331 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
d6e5a549 3332 dwc->bounce_addr);
0fc9a1be 3333 kfree(dwc->setup_buf);
d64ff406 3334 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
d6e5a549 3335 dwc->ep0_trb, dwc->ep0_trb_addr);
72246da4 3336}
7415f17c 3337
0b0231aa 3338int dwc3_gadget_suspend(struct dwc3 *dwc)
7415f17c 3339{
9772b47a
RQ
3340 if (!dwc->gadget_driver)
3341 return 0;
3342
1551e35e 3343 dwc3_gadget_run_stop(dwc, false, false);
9f8a67b6
FB
3344 dwc3_disconnect_gadget(dwc);
3345 __dwc3_gadget_stop(dwc);
7415f17c
FB
3346
3347 return 0;
3348}
3349
3350int dwc3_gadget_resume(struct dwc3 *dwc)
3351{
7415f17c
FB
3352 int ret;
3353
9772b47a
RQ
3354 if (!dwc->gadget_driver)
3355 return 0;
3356
9f8a67b6
FB
3357 ret = __dwc3_gadget_start(dwc);
3358 if (ret < 0)
7415f17c
FB
3359 goto err0;
3360
9f8a67b6
FB
3361 ret = dwc3_gadget_run_stop(dwc, true, false);
3362 if (ret < 0)
7415f17c
FB
3363 goto err1;
3364
7415f17c
FB
3365 return 0;
3366
3367err1:
9f8a67b6 3368 __dwc3_gadget_stop(dwc);
7415f17c
FB
3369
3370err0:
3371 return ret;
3372}
fc8bb91b
FB
3373
3374void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3375{
3376 if (dwc->pending_events) {
3377 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3378 dwc->pending_events = false;
3379 enable_irq(dwc->irq_gadget);
3380 }
3381}
This page took 1.218428 seconds and 4 git commands to generate.