]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/dwc3/gadget.c
usb: dwc3: increase maximum number of TRBs per endpoint
[mirror_ubuntu-artful-kernel.git] / drivers / usb / dwc3 / gadget.c
CommitLineData
72246da4
FB
1/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
72246da4
FB
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
5945f789
FB
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
72246da4 12 *
5945f789
FB
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
72246da4
FB
17 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
80977dc9 33#include "debug.h"
72246da4
FB
34#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
04a9bfcd
FB
38/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
911f1f88
PZ
71/**
72 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
8598bde7
FB
87/**
88 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
aee63e3c 93 * return 0 on success or -ETIMEDOUT.
8598bde7
FB
94 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
aee63e3c 97 int retries = 10000;
8598bde7
FB
98 u32 reg;
99
802fde98
PZ
100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
8598bde7
FB
117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
802fde98
PZ
124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
8598bde7 131 /* wait for a change in DSTS */
aed430e5 132 retries = 10000;
8598bde7
FB
133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
8598bde7
FB
136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
aee63e3c 139 udelay(5);
8598bde7
FB
140 }
141
73815280
FB
142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
8598bde7
FB
144
145 return -ETIMEDOUT;
146}
147
72246da4
FB
148void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
149 int status)
150{
151 struct dwc3 *dwc = dep->dwc;
e5ba5ec8 152 int i;
72246da4
FB
153
154 if (req->queued) {
e5ba5ec8
PA
155 i = 0;
156 do {
eeb720fb 157 dep->busy_slot++;
e5ba5ec8
PA
158 /*
159 * Skip LINK TRB. We can't use req->trb and check for
160 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
161 * just completed (not the LINK TRB).
162 */
163 if (((dep->busy_slot & DWC3_TRB_MASK) ==
164 DWC3_TRB_NUM- 1) &&
16e78db7 165 usb_endpoint_xfer_isoc(dep->endpoint.desc))
e5ba5ec8
PA
166 dep->busy_slot++;
167 } while(++i < req->request.num_mapped_sgs);
c9fda7d6 168 req->queued = false;
72246da4
FB
169 }
170 list_del(&req->list);
eeb720fb 171 req->trb = NULL;
72246da4
FB
172
173 if (req->request.status == -EINPROGRESS)
174 req->request.status = status;
175
0416e494
PA
176 if (dwc->ep0_bounced && dep->number == 0)
177 dwc->ep0_bounced = false;
178 else
179 usb_gadget_unmap_request(&dwc->gadget, &req->request,
180 req->direction);
72246da4 181
2c4cbe6e 182 trace_dwc3_gadget_giveback(req);
72246da4
FB
183
184 spin_unlock(&dwc->lock);
304f7e5e 185 usb_gadget_giveback_request(&dep->endpoint, &req->request);
72246da4
FB
186 spin_lock(&dwc->lock);
187}
188
3ece0ec4 189int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
b09bb642
FB
190{
191 u32 timeout = 500;
192 u32 reg;
193
2c4cbe6e 194 trace_dwc3_gadget_generic_cmd(cmd, param);
427c3df6 195
b09bb642
FB
196 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
197 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
198
199 do {
200 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
201 if (!(reg & DWC3_DGCMD_CMDACT)) {
73815280
FB
202 dwc3_trace(trace_dwc3_gadget,
203 "Command Complete --> %d",
b09bb642 204 DWC3_DGCMD_STATUS(reg));
891b1dc0
SSB
205 if (DWC3_DGCMD_STATUS(reg))
206 return -EINVAL;
b09bb642
FB
207 return 0;
208 }
209
210 /*
211 * We can't sleep here, because it's also called from
212 * interrupt context.
213 */
214 timeout--;
73815280
FB
215 if (!timeout) {
216 dwc3_trace(trace_dwc3_gadget,
217 "Command Timed Out");
b09bb642 218 return -ETIMEDOUT;
73815280 219 }
b09bb642
FB
220 udelay(1);
221 } while (1);
222}
223
72246da4
FB
224int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
225 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
226{
227 struct dwc3_ep *dep = dwc->eps[ep];
61d58242 228 u32 timeout = 500;
72246da4
FB
229 u32 reg;
230
2c4cbe6e 231 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
72246da4 232
dc1c70a7
FB
233 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
234 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
235 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
72246da4
FB
236
237 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
238 do {
239 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
240 if (!(reg & DWC3_DEPCMD_CMDACT)) {
73815280
FB
241 dwc3_trace(trace_dwc3_gadget,
242 "Command Complete --> %d",
164f6e14 243 DWC3_DEPCMD_STATUS(reg));
76e838c9
SSB
244 if (DWC3_DEPCMD_STATUS(reg))
245 return -EINVAL;
72246da4
FB
246 return 0;
247 }
248
249 /*
72246da4
FB
250 * We can't sleep here, because it is also called from
251 * interrupt context.
252 */
253 timeout--;
73815280
FB
254 if (!timeout) {
255 dwc3_trace(trace_dwc3_gadget,
256 "Command Timed Out");
72246da4 257 return -ETIMEDOUT;
73815280 258 }
72246da4 259
61d58242 260 udelay(1);
72246da4
FB
261 } while (1);
262}
263
264static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
f6bafc6a 265 struct dwc3_trb *trb)
72246da4 266{
c439ef87 267 u32 offset = (char *) trb - (char *) dep->trb_pool;
72246da4
FB
268
269 return dep->trb_pool_dma + offset;
270}
271
272static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
273{
274 struct dwc3 *dwc = dep->dwc;
275
276 if (dep->trb_pool)
277 return 0;
278
72246da4
FB
279 dep->trb_pool = dma_alloc_coherent(dwc->dev,
280 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
281 &dep->trb_pool_dma, GFP_KERNEL);
282 if (!dep->trb_pool) {
283 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
284 dep->name);
285 return -ENOMEM;
286 }
287
288 return 0;
289}
290
291static void dwc3_free_trb_pool(struct dwc3_ep *dep)
292{
293 struct dwc3 *dwc = dep->dwc;
294
295 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
296 dep->trb_pool, dep->trb_pool_dma);
297
298 dep->trb_pool = NULL;
299 dep->trb_pool_dma = 0;
300}
301
c4509601
JY
302static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
303
304/**
305 * dwc3_gadget_start_config - Configure EP resources
306 * @dwc: pointer to our controller context structure
307 * @dep: endpoint that is being enabled
308 *
309 * The assignment of transfer resources cannot perfectly follow the
310 * data book due to the fact that the controller driver does not have
311 * all knowledge of the configuration in advance. It is given this
312 * information piecemeal by the composite gadget framework after every
313 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
314 * programming model in this scenario can cause errors. For two
315 * reasons:
316 *
317 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
318 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
319 * multiple interfaces.
320 *
321 * 2) The databook does not mention doing more DEPXFERCFG for new
322 * endpoint on alt setting (8.1.6).
323 *
324 * The following simplified method is used instead:
325 *
326 * All hardware endpoints can be assigned a transfer resource and this
327 * setting will stay persistent until either a core reset or
328 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
329 * do DEPXFERCFG for every hardware endpoint as well. We are
330 * guaranteed that there are as many transfer resources as endpoints.
331 *
332 * This function is called for each endpoint when it is being enabled
333 * but is triggered only when called for EP0-out, which always happens
334 * first, and which should only happen in one of the above conditions.
335 */
72246da4
FB
336static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
337{
338 struct dwc3_gadget_ep_cmd_params params;
339 u32 cmd;
c4509601
JY
340 int i;
341 int ret;
342
343 if (dep->number)
344 return 0;
72246da4
FB
345
346 memset(&params, 0x00, sizeof(params));
c4509601 347 cmd = DWC3_DEPCMD_DEPSTARTCFG;
72246da4 348
c4509601
JY
349 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
350 if (ret)
351 return ret;
352
353 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
354 struct dwc3_ep *dep = dwc->eps[i];
72246da4 355
c4509601
JY
356 if (!dep)
357 continue;
358
359 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
360 if (ret)
361 return ret;
72246da4
FB
362 }
363
364 return 0;
365}
366
367static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
c90bfaec 368 const struct usb_endpoint_descriptor *desc,
4b345c9a 369 const struct usb_ss_ep_comp_descriptor *comp_desc,
265b70a7 370 bool ignore, bool restore)
72246da4
FB
371{
372 struct dwc3_gadget_ep_cmd_params params;
373
374 memset(&params, 0x00, sizeof(params));
375
dc1c70a7 376 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
d2e9a13a
CP
377 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
378
379 /* Burst size is only needed in SuperSpeed mode */
ee5cd41c 380 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
d2e9a13a
CP
381 u32 burst = dep->endpoint.maxburst - 1;
382
383 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
384 }
72246da4 385
4b345c9a
FB
386 if (ignore)
387 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
388
265b70a7
PZ
389 if (restore) {
390 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
391 params.param2 |= dep->saved_state;
392 }
393
dc1c70a7
FB
394 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
395 | DWC3_DEPCFG_XFER_NOT_READY_EN;
72246da4 396
18b7ede5 397 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
dc1c70a7
FB
398 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
399 | DWC3_DEPCFG_STREAM_EVENT_EN;
879631aa
FB
400 dep->stream_capable = true;
401 }
402
0b93a4c8 403 if (!usb_endpoint_xfer_control(desc))
dc1c70a7 404 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
72246da4
FB
405
406 /*
407 * We are doing 1:1 mapping for endpoints, meaning
408 * Physical Endpoints 2 maps to Logical Endpoint 2 and
409 * so on. We consider the direction bit as part of the physical
410 * endpoint number. So USB endpoint 0x81 is 0x03.
411 */
dc1c70a7 412 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
72246da4
FB
413
414 /*
415 * We must use the lower 16 TX FIFOs even though
416 * HW might have more
417 */
418 if (dep->direction)
dc1c70a7 419 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
72246da4
FB
420
421 if (desc->bInterval) {
dc1c70a7 422 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
72246da4
FB
423 dep->interval = 1 << (desc->bInterval - 1);
424 }
425
426 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
427 DWC3_DEPCMD_SETEPCONFIG, &params);
428}
429
430static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
431{
432 struct dwc3_gadget_ep_cmd_params params;
433
434 memset(&params, 0x00, sizeof(params));
435
dc1c70a7 436 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
72246da4
FB
437
438 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
439 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
440}
441
442/**
443 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
444 * @dep: endpoint to be initialized
445 * @desc: USB Endpoint Descriptor
446 *
447 * Caller should take care of locking
448 */
449static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
c90bfaec 450 const struct usb_endpoint_descriptor *desc,
4b345c9a 451 const struct usb_ss_ep_comp_descriptor *comp_desc,
265b70a7 452 bool ignore, bool restore)
72246da4
FB
453{
454 struct dwc3 *dwc = dep->dwc;
455 u32 reg;
b09e99ee 456 int ret;
72246da4 457
73815280 458 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
ff62d6b6 459
72246da4
FB
460 if (!(dep->flags & DWC3_EP_ENABLED)) {
461 ret = dwc3_gadget_start_config(dwc, dep);
462 if (ret)
463 return ret;
464 }
465
265b70a7
PZ
466 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
467 restore);
72246da4
FB
468 if (ret)
469 return ret;
470
471 if (!(dep->flags & DWC3_EP_ENABLED)) {
f6bafc6a
FB
472 struct dwc3_trb *trb_st_hw;
473 struct dwc3_trb *trb_link;
72246da4 474
16e78db7 475 dep->endpoint.desc = desc;
c90bfaec 476 dep->comp_desc = comp_desc;
72246da4
FB
477 dep->type = usb_endpoint_type(desc);
478 dep->flags |= DWC3_EP_ENABLED;
479
480 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
481 reg |= DWC3_DALEPENA_EP(dep->number);
482 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
483
484 if (!usb_endpoint_xfer_isoc(desc))
e901aa15 485 goto out;
72246da4 486
1d046793 487 /* Link TRB for ISOC. The HWO bit is never reset */
72246da4
FB
488 trb_st_hw = &dep->trb_pool[0];
489
f6bafc6a 490 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
1200a82a 491 memset(trb_link, 0, sizeof(*trb_link));
72246da4 492
f6bafc6a
FB
493 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
494 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
495 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
496 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
72246da4
FB
497 }
498
e901aa15 499out:
aa739974
FB
500 switch (usb_endpoint_type(desc)) {
501 case USB_ENDPOINT_XFER_CONTROL:
e901aa15 502 /* don't change name */
aa739974
FB
503 break;
504 case USB_ENDPOINT_XFER_ISOC:
505 strlcat(dep->name, "-isoc", sizeof(dep->name));
506 break;
507 case USB_ENDPOINT_XFER_BULK:
508 strlcat(dep->name, "-bulk", sizeof(dep->name));
509 break;
510 case USB_ENDPOINT_XFER_INT:
511 strlcat(dep->name, "-int", sizeof(dep->name));
512 break;
513 default:
514 dev_err(dwc->dev, "invalid endpoint transfer type\n");
515 }
516
72246da4
FB
517 return 0;
518}
519
b992e681 520static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
624407f9 521static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
72246da4
FB
522{
523 struct dwc3_request *req;
524
ea53b882 525 if (!list_empty(&dep->req_queued)) {
b992e681 526 dwc3_stop_active_transfer(dwc, dep->number, true);
624407f9 527
57911504 528 /* - giveback all requests to gadget driver */
1591633e
PA
529 while (!list_empty(&dep->req_queued)) {
530 req = next_request(&dep->req_queued);
531
532 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
533 }
ea53b882
FB
534 }
535
72246da4
FB
536 while (!list_empty(&dep->request_list)) {
537 req = next_request(&dep->request_list);
538
624407f9 539 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
72246da4 540 }
72246da4
FB
541}
542
543/**
544 * __dwc3_gadget_ep_disable - Disables a HW endpoint
545 * @dep: the endpoint to disable
546 *
624407f9
SAS
547 * This function also removes requests which are currently processed ny the
548 * hardware and those which are not yet scheduled.
549 * Caller should take care of locking.
72246da4 550 */
72246da4
FB
551static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
552{
553 struct dwc3 *dwc = dep->dwc;
554 u32 reg;
555
7eaeac5c
FB
556 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
557
624407f9 558 dwc3_remove_requests(dwc, dep);
72246da4 559
687ef981
FB
560 /* make sure HW endpoint isn't stalled */
561 if (dep->flags & DWC3_EP_STALL)
7a608559 562 __dwc3_gadget_ep_set_halt(dep, 0, false);
687ef981 563
72246da4
FB
564 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
565 reg &= ~DWC3_DALEPENA_EP(dep->number);
566 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
567
879631aa 568 dep->stream_capable = false;
f9c56cdd 569 dep->endpoint.desc = NULL;
c90bfaec 570 dep->comp_desc = NULL;
72246da4 571 dep->type = 0;
879631aa 572 dep->flags = 0;
72246da4 573
aa739974
FB
574 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
575 dep->number >> 1,
576 (dep->number & 1) ? "in" : "out");
577
72246da4
FB
578 return 0;
579}
580
581/* -------------------------------------------------------------------------- */
582
583static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
584 const struct usb_endpoint_descriptor *desc)
585{
586 return -EINVAL;
587}
588
589static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
590{
591 return -EINVAL;
592}
593
594/* -------------------------------------------------------------------------- */
595
596static int dwc3_gadget_ep_enable(struct usb_ep *ep,
597 const struct usb_endpoint_descriptor *desc)
598{
599 struct dwc3_ep *dep;
600 struct dwc3 *dwc;
601 unsigned long flags;
602 int ret;
603
604 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
605 pr_debug("dwc3: invalid parameters\n");
606 return -EINVAL;
607 }
608
609 if (!desc->wMaxPacketSize) {
610 pr_debug("dwc3: missing wMaxPacketSize\n");
611 return -EINVAL;
612 }
613
614 dep = to_dwc3_ep(ep);
615 dwc = dep->dwc;
616
95ca961c
FB
617 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
618 "%s is already enabled\n",
619 dep->name))
c6f83f38 620 return 0;
c6f83f38 621
72246da4 622 spin_lock_irqsave(&dwc->lock, flags);
265b70a7 623 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
72246da4
FB
624 spin_unlock_irqrestore(&dwc->lock, flags);
625
626 return ret;
627}
628
629static int dwc3_gadget_ep_disable(struct usb_ep *ep)
630{
631 struct dwc3_ep *dep;
632 struct dwc3 *dwc;
633 unsigned long flags;
634 int ret;
635
636 if (!ep) {
637 pr_debug("dwc3: invalid parameters\n");
638 return -EINVAL;
639 }
640
641 dep = to_dwc3_ep(ep);
642 dwc = dep->dwc;
643
95ca961c
FB
644 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
645 "%s is already disabled\n",
646 dep->name))
72246da4 647 return 0;
72246da4 648
72246da4
FB
649 spin_lock_irqsave(&dwc->lock, flags);
650 ret = __dwc3_gadget_ep_disable(dep);
651 spin_unlock_irqrestore(&dwc->lock, flags);
652
653 return ret;
654}
655
656static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
657 gfp_t gfp_flags)
658{
659 struct dwc3_request *req;
660 struct dwc3_ep *dep = to_dwc3_ep(ep);
72246da4
FB
661
662 req = kzalloc(sizeof(*req), gfp_flags);
734d5a53 663 if (!req)
72246da4 664 return NULL;
72246da4
FB
665
666 req->epnum = dep->number;
667 req->dep = dep;
72246da4 668
2c4cbe6e
FB
669 trace_dwc3_alloc_request(req);
670
72246da4
FB
671 return &req->request;
672}
673
674static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
675 struct usb_request *request)
676{
677 struct dwc3_request *req = to_dwc3_request(request);
678
2c4cbe6e 679 trace_dwc3_free_request(req);
72246da4
FB
680 kfree(req);
681}
682
c71fc37c
FB
683/**
684 * dwc3_prepare_one_trb - setup one TRB from one request
685 * @dep: endpoint for which this request is prepared
686 * @req: dwc3_request pointer
687 */
68e823e2 688static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
eeb720fb 689 struct dwc3_request *req, dma_addr_t dma,
e5ba5ec8 690 unsigned length, unsigned last, unsigned chain, unsigned node)
c71fc37c 691{
f6bafc6a 692 struct dwc3_trb *trb;
c71fc37c 693
73815280 694 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
eeb720fb
FB
695 dep->name, req, (unsigned long long) dma,
696 length, last ? " last" : "",
697 chain ? " chain" : "");
698
915e202a
PA
699
700 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
c71fc37c 701
eeb720fb
FB
702 if (!req->trb) {
703 dwc3_gadget_move_request_queued(req);
f6bafc6a
FB
704 req->trb = trb;
705 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
e5ba5ec8 706 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
eeb720fb 707 }
c71fc37c 708
e5ba5ec8 709 dep->free_slot++;
5cd8c48d
ZJC
710 /* Skip the LINK-TRB on ISOC */
711 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
712 usb_endpoint_xfer_isoc(dep->endpoint.desc))
713 dep->free_slot++;
e5ba5ec8 714
f6bafc6a
FB
715 trb->size = DWC3_TRB_SIZE_LENGTH(length);
716 trb->bpl = lower_32_bits(dma);
717 trb->bph = upper_32_bits(dma);
c71fc37c 718
16e78db7 719 switch (usb_endpoint_type(dep->endpoint.desc)) {
c71fc37c 720 case USB_ENDPOINT_XFER_CONTROL:
f6bafc6a 721 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
c71fc37c
FB
722 break;
723
724 case USB_ENDPOINT_XFER_ISOC:
e5ba5ec8
PA
725 if (!node)
726 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
727 else
728 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
ca4d44ea
FB
729
730 /* always enable Interrupt on Missed ISOC */
731 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
c71fc37c
FB
732 break;
733
734 case USB_ENDPOINT_XFER_BULK:
735 case USB_ENDPOINT_XFER_INT:
f6bafc6a 736 trb->ctrl = DWC3_TRBCTL_NORMAL;
c71fc37c
FB
737 break;
738 default:
739 /*
740 * This is only possible with faulty memory because we
741 * checked it already :)
742 */
743 BUG();
744 }
745
ca4d44ea
FB
746 /* always enable Continue on Short Packet */
747 trb->ctrl |= DWC3_TRB_CTRL_CSP;
f3af3651 748
ca4d44ea
FB
749 if (!req->request.no_interrupt)
750 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
751
752 if (last)
e5ba5ec8 753 trb->ctrl |= DWC3_TRB_CTRL_LST;
c71fc37c 754
e5ba5ec8
PA
755 if (chain)
756 trb->ctrl |= DWC3_TRB_CTRL_CHN;
757
16e78db7 758 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
f6bafc6a 759 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
c71fc37c 760
f6bafc6a 761 trb->ctrl |= DWC3_TRB_CTRL_HWO;
2c4cbe6e
FB
762
763 trace_dwc3_prepare_trb(dep, trb);
c71fc37c
FB
764}
765
72246da4
FB
766/*
767 * dwc3_prepare_trbs - setup TRBs from requests
768 * @dep: endpoint for which requests are being prepared
769 * @starting: true if the endpoint is idle and no requests are queued.
770 *
1d046793
PZ
771 * The function goes through the requests list and sets up TRBs for the
772 * transfers. The function returns once there are no more TRBs available or
773 * it runs out of requests.
72246da4 774 */
68e823e2 775static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
72246da4 776{
68e823e2 777 struct dwc3_request *req, *n;
72246da4 778 u32 trbs_left;
8d62cd65 779 u32 max;
c71fc37c 780 unsigned int last_one = 0;
72246da4
FB
781
782 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
783
784 /* the first request must not be queued */
785 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
c71fc37c 786
8d62cd65 787 /* Can't wrap around on a non-isoc EP since there's no link TRB */
16e78db7 788 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
8d62cd65
PZ
789 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
790 if (trbs_left > max)
791 trbs_left = max;
792 }
793
72246da4 794 /*
1d046793
PZ
795 * If busy & slot are equal than it is either full or empty. If we are
796 * starting to process requests then we are empty. Otherwise we are
72246da4
FB
797 * full and don't do anything
798 */
799 if (!trbs_left) {
800 if (!starting)
68e823e2 801 return;
72246da4
FB
802 trbs_left = DWC3_TRB_NUM;
803 /*
804 * In case we start from scratch, we queue the ISOC requests
805 * starting from slot 1. This is done because we use ring
806 * buffer and have no LST bit to stop us. Instead, we place
1d046793 807 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
72246da4
FB
808 * after the first request so we start at slot 1 and have
809 * 7 requests proceed before we hit the first IOC.
810 * Other transfer types don't use the ring buffer and are
811 * processed from the first TRB until the last one. Since we
812 * don't wrap around we have to start at the beginning.
813 */
16e78db7 814 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
815 dep->busy_slot = 1;
816 dep->free_slot = 1;
817 } else {
818 dep->busy_slot = 0;
819 dep->free_slot = 0;
820 }
821 }
822
823 /* The last TRB is a link TRB, not used for xfer */
16e78db7 824 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
68e823e2 825 return;
72246da4
FB
826
827 list_for_each_entry_safe(req, n, &dep->request_list, list) {
eeb720fb
FB
828 unsigned length;
829 dma_addr_t dma;
e5ba5ec8 830 last_one = false;
72246da4 831
eeb720fb
FB
832 if (req->request.num_mapped_sgs > 0) {
833 struct usb_request *request = &req->request;
834 struct scatterlist *sg = request->sg;
835 struct scatterlist *s;
836 int i;
72246da4 837
eeb720fb
FB
838 for_each_sg(sg, s, request->num_mapped_sgs, i) {
839 unsigned chain = true;
72246da4 840
eeb720fb
FB
841 length = sg_dma_len(s);
842 dma = sg_dma_address(s);
72246da4 843
1d046793
PZ
844 if (i == (request->num_mapped_sgs - 1) ||
845 sg_is_last(s)) {
ec512fb8 846 if (list_empty(&dep->request_list))
e5ba5ec8 847 last_one = true;
eeb720fb
FB
848 chain = false;
849 }
72246da4 850
eeb720fb
FB
851 trbs_left--;
852 if (!trbs_left)
853 last_one = true;
72246da4 854
eeb720fb
FB
855 if (last_one)
856 chain = false;
72246da4 857
eeb720fb 858 dwc3_prepare_one_trb(dep, req, dma, length,
e5ba5ec8 859 last_one, chain, i);
72246da4 860
eeb720fb
FB
861 if (last_one)
862 break;
863 }
39e60635
AV
864
865 if (last_one)
866 break;
72246da4 867 } else {
eeb720fb
FB
868 dma = req->request.dma;
869 length = req->request.length;
870 trbs_left--;
72246da4 871
eeb720fb
FB
872 if (!trbs_left)
873 last_one = 1;
879631aa 874
eeb720fb
FB
875 /* Is this the last request? */
876 if (list_is_last(&req->list, &dep->request_list))
877 last_one = 1;
72246da4 878
eeb720fb 879 dwc3_prepare_one_trb(dep, req, dma, length,
e5ba5ec8 880 last_one, false, 0);
72246da4 881
eeb720fb
FB
882 if (last_one)
883 break;
72246da4 884 }
72246da4 885 }
72246da4
FB
886}
887
888static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
889 int start_new)
890{
891 struct dwc3_gadget_ep_cmd_params params;
892 struct dwc3_request *req;
893 struct dwc3 *dwc = dep->dwc;
894 int ret;
895 u32 cmd;
896
897 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
73815280 898 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
72246da4
FB
899 return -EBUSY;
900 }
72246da4
FB
901
902 /*
903 * If we are getting here after a short-out-packet we don't enqueue any
904 * new requests as we try to set the IOC bit only on the last request.
905 */
906 if (start_new) {
907 if (list_empty(&dep->req_queued))
908 dwc3_prepare_trbs(dep, start_new);
909
910 /* req points to the first request which will be sent */
911 req = next_request(&dep->req_queued);
912 } else {
68e823e2
FB
913 dwc3_prepare_trbs(dep, start_new);
914
72246da4 915 /*
1d046793 916 * req points to the first request where HWO changed from 0 to 1
72246da4 917 */
68e823e2 918 req = next_request(&dep->req_queued);
72246da4
FB
919 }
920 if (!req) {
921 dep->flags |= DWC3_EP_PENDING_REQUEST;
922 return 0;
923 }
924
925 memset(&params, 0, sizeof(params));
72246da4 926
1877d6c9
PA
927 if (start_new) {
928 params.param0 = upper_32_bits(req->trb_dma);
929 params.param1 = lower_32_bits(req->trb_dma);
72246da4 930 cmd = DWC3_DEPCMD_STARTTRANSFER;
1877d6c9 931 } else {
72246da4 932 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1877d6c9 933 }
72246da4
FB
934
935 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
936 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
937 if (ret < 0) {
72246da4
FB
938 /*
939 * FIXME we need to iterate over the list of requests
940 * here and stop, unmap, free and del each of the linked
1d046793 941 * requests instead of what we do now.
72246da4 942 */
0fc9a1be
FB
943 usb_gadget_unmap_request(&dwc->gadget, &req->request,
944 req->direction);
72246da4
FB
945 list_del(&req->list);
946 return ret;
947 }
948
949 dep->flags |= DWC3_EP_BUSY;
25b8ff68 950
f898ae09 951 if (start_new) {
b4996a86 952 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
f898ae09 953 dep->number);
b4996a86 954 WARN_ON_ONCE(!dep->resource_index);
f898ae09 955 }
25b8ff68 956
72246da4
FB
957 return 0;
958}
959
d6d6ec7b
PA
960static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
961 struct dwc3_ep *dep, u32 cur_uf)
962{
963 u32 uf;
964
965 if (list_empty(&dep->request_list)) {
73815280
FB
966 dwc3_trace(trace_dwc3_gadget,
967 "ISOC ep %s run out for requests",
968 dep->name);
f4a53c55 969 dep->flags |= DWC3_EP_PENDING_REQUEST;
d6d6ec7b
PA
970 return;
971 }
972
973 /* 4 micro frames in the future */
974 uf = cur_uf + dep->interval * 4;
975
976 __dwc3_gadget_kick_transfer(dep, uf, 1);
977}
978
979static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
980 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
981{
982 u32 cur_uf, mask;
983
984 mask = ~(dep->interval - 1);
985 cur_uf = event->parameters & mask;
986
987 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
988}
989
72246da4
FB
990static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
991{
0fc9a1be
FB
992 struct dwc3 *dwc = dep->dwc;
993 int ret;
994
bb423984 995 if (!dep->endpoint.desc) {
ec5e795c
FB
996 dwc3_trace(trace_dwc3_gadget,
997 "trying to queue request %p to disabled %s\n",
bb423984
FB
998 &req->request, dep->endpoint.name);
999 return -ESHUTDOWN;
1000 }
1001
1002 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1003 &req->request, req->dep->name)) {
ec5e795c
FB
1004 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1005 &req->request, req->dep->name);
bb423984
FB
1006 return -EINVAL;
1007 }
1008
72246da4
FB
1009 req->request.actual = 0;
1010 req->request.status = -EINPROGRESS;
1011 req->direction = dep->direction;
1012 req->epnum = dep->number;
1013
fe84f522
FB
1014 trace_dwc3_ep_queue(req);
1015
72246da4
FB
1016 /*
1017 * We only add to our list of requests now and
1018 * start consuming the list once we get XferNotReady
1019 * IRQ.
1020 *
1021 * That way, we avoid doing anything that we don't need
1022 * to do now and defer it until the point we receive a
1023 * particular token from the Host side.
1024 *
1025 * This will also avoid Host cancelling URBs due to too
1d046793 1026 * many NAKs.
72246da4 1027 */
0fc9a1be
FB
1028 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1029 dep->direction);
1030 if (ret)
1031 return ret;
1032
72246da4
FB
1033 list_add_tail(&req->list, &dep->request_list);
1034
1d6a3918
FB
1035 /*
1036 * If there are no pending requests and the endpoint isn't already
1037 * busy, we will just start the request straight away.
1038 *
1039 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1040 * little bit faster.
1041 */
1042 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
62e345ae 1043 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
1d6a3918
FB
1044 !(dep->flags & DWC3_EP_BUSY)) {
1045 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
a8f32817 1046 goto out;
1d6a3918
FB
1047 }
1048
72246da4 1049 /*
b511e5e7 1050 * There are a few special cases:
72246da4 1051 *
f898ae09
PZ
1052 * 1. XferNotReady with empty list of requests. We need to kick the
1053 * transfer here in that situation, otherwise we will be NAKing
1054 * forever. If we get XferNotReady before gadget driver has a
1055 * chance to queue a request, we will ACK the IRQ but won't be
1056 * able to receive the data until the next request is queued.
1057 * The following code is handling exactly that.
72246da4 1058 *
72246da4
FB
1059 */
1060 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
f4a53c55
PA
1061 /*
1062 * If xfernotready is already elapsed and it is a case
1063 * of isoc transfer, then issue END TRANSFER, so that
1064 * you can receive xfernotready again and can have
1065 * notion of current microframe.
1066 */
1067 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
cdc359dd 1068 if (list_empty(&dep->req_queued)) {
b992e681 1069 dwc3_stop_active_transfer(dwc, dep->number, true);
cdc359dd
PA
1070 dep->flags = DWC3_EP_ENABLED;
1071 }
f4a53c55
PA
1072 return 0;
1073 }
1074
b511e5e7 1075 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
89185916
FB
1076 if (!ret)
1077 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1078
a8f32817 1079 goto out;
b511e5e7 1080 }
72246da4 1081
b511e5e7
FB
1082 /*
1083 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1084 * kick the transfer here after queuing a request, otherwise the
1085 * core may not see the modified TRB(s).
1086 */
1087 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
79c9046e
PA
1088 (dep->flags & DWC3_EP_BUSY) &&
1089 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
b4996a86
FB
1090 WARN_ON_ONCE(!dep->resource_index);
1091 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
b511e5e7 1092 false);
a8f32817 1093 goto out;
a0925324 1094 }
72246da4 1095
b997ada5
FB
1096 /*
1097 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1098 * right away, otherwise host will not know we have streams to be
1099 * handled.
1100 */
a8f32817 1101 if (dep->stream_capable)
b997ada5 1102 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
b997ada5 1103
a8f32817
FB
1104out:
1105 if (ret && ret != -EBUSY)
ec5e795c
FB
1106 dwc3_trace(trace_dwc3_gadget,
1107 "%s: failed to kick transfers\n",
a8f32817
FB
1108 dep->name);
1109 if (ret == -EBUSY)
1110 ret = 0;
1111
1112 return ret;
72246da4
FB
1113}
1114
04c03d10
FB
1115static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1116 struct usb_request *request)
1117{
1118 dwc3_gadget_ep_free_request(ep, request);
1119}
1120
1121static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1122{
1123 struct dwc3_request *req;
1124 struct usb_request *request;
1125 struct usb_ep *ep = &dep->endpoint;
1126
1127 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1128 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1129 if (!request)
1130 return -ENOMEM;
1131
1132 request->length = 0;
1133 request->buf = dwc->zlp_buf;
1134 request->complete = __dwc3_gadget_ep_zlp_complete;
1135
1136 req = to_dwc3_request(request);
1137
1138 return __dwc3_gadget_ep_queue(dep, req);
1139}
1140
72246da4
FB
1141static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1142 gfp_t gfp_flags)
1143{
1144 struct dwc3_request *req = to_dwc3_request(request);
1145 struct dwc3_ep *dep = to_dwc3_ep(ep);
1146 struct dwc3 *dwc = dep->dwc;
1147
1148 unsigned long flags;
1149
1150 int ret;
1151
fdee4eba 1152 spin_lock_irqsave(&dwc->lock, flags);
72246da4 1153 ret = __dwc3_gadget_ep_queue(dep, req);
04c03d10
FB
1154
1155 /*
1156 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1157 * setting request->zero, instead of doing magic, we will just queue an
1158 * extra usb_request ourselves so that it gets handled the same way as
1159 * any other request.
1160 */
d9261898
JY
1161 if (ret == 0 && request->zero && request->length &&
1162 (request->length % ep->maxpacket == 0))
04c03d10
FB
1163 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1164
72246da4
FB
1165 spin_unlock_irqrestore(&dwc->lock, flags);
1166
1167 return ret;
1168}
1169
1170static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1171 struct usb_request *request)
1172{
1173 struct dwc3_request *req = to_dwc3_request(request);
1174 struct dwc3_request *r = NULL;
1175
1176 struct dwc3_ep *dep = to_dwc3_ep(ep);
1177 struct dwc3 *dwc = dep->dwc;
1178
1179 unsigned long flags;
1180 int ret = 0;
1181
2c4cbe6e
FB
1182 trace_dwc3_ep_dequeue(req);
1183
72246da4
FB
1184 spin_lock_irqsave(&dwc->lock, flags);
1185
1186 list_for_each_entry(r, &dep->request_list, list) {
1187 if (r == req)
1188 break;
1189 }
1190
1191 if (r != req) {
1192 list_for_each_entry(r, &dep->req_queued, list) {
1193 if (r == req)
1194 break;
1195 }
1196 if (r == req) {
1197 /* wait until it is processed */
b992e681 1198 dwc3_stop_active_transfer(dwc, dep->number, true);
e8d4e8be 1199 goto out1;
72246da4
FB
1200 }
1201 dev_err(dwc->dev, "request %p was not queued to %s\n",
1202 request, ep->name);
1203 ret = -EINVAL;
1204 goto out0;
1205 }
1206
e8d4e8be 1207out1:
72246da4
FB
1208 /* giveback the request */
1209 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1210
1211out0:
1212 spin_unlock_irqrestore(&dwc->lock, flags);
1213
1214 return ret;
1215}
1216
7a608559 1217int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
72246da4
FB
1218{
1219 struct dwc3_gadget_ep_cmd_params params;
1220 struct dwc3 *dwc = dep->dwc;
1221 int ret;
1222
5ad02fb8
FB
1223 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1224 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1225 return -EINVAL;
1226 }
1227
72246da4
FB
1228 memset(&params, 0x00, sizeof(params));
1229
1230 if (value) {
7a608559
FB
1231 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1232 (!list_empty(&dep->req_queued) ||
1233 !list_empty(&dep->request_list)))) {
ec5e795c
FB
1234 dwc3_trace(trace_dwc3_gadget,
1235 "%s: pending request, cannot halt\n",
7a608559
FB
1236 dep->name);
1237 return -EAGAIN;
1238 }
1239
72246da4
FB
1240 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1241 DWC3_DEPCMD_SETSTALL, &params);
1242 if (ret)
3f89204b 1243 dev_err(dwc->dev, "failed to set STALL on %s\n",
72246da4
FB
1244 dep->name);
1245 else
1246 dep->flags |= DWC3_EP_STALL;
1247 } else {
1248 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1249 DWC3_DEPCMD_CLEARSTALL, &params);
1250 if (ret)
3f89204b 1251 dev_err(dwc->dev, "failed to clear STALL on %s\n",
72246da4
FB
1252 dep->name);
1253 else
a535d81c 1254 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
72246da4 1255 }
5275455a 1256
72246da4
FB
1257 return ret;
1258}
1259
1260static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1261{
1262 struct dwc3_ep *dep = to_dwc3_ep(ep);
1263 struct dwc3 *dwc = dep->dwc;
1264
1265 unsigned long flags;
1266
1267 int ret;
1268
1269 spin_lock_irqsave(&dwc->lock, flags);
7a608559 1270 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
72246da4
FB
1271 spin_unlock_irqrestore(&dwc->lock, flags);
1272
1273 return ret;
1274}
1275
1276static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1277{
1278 struct dwc3_ep *dep = to_dwc3_ep(ep);
249a4569
PZ
1279 struct dwc3 *dwc = dep->dwc;
1280 unsigned long flags;
95aa4e8d 1281 int ret;
72246da4 1282
249a4569 1283 spin_lock_irqsave(&dwc->lock, flags);
72246da4
FB
1284 dep->flags |= DWC3_EP_WEDGE;
1285
08f0d966 1286 if (dep->number == 0 || dep->number == 1)
95aa4e8d 1287 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
08f0d966 1288 else
7a608559 1289 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
95aa4e8d
FB
1290 spin_unlock_irqrestore(&dwc->lock, flags);
1291
1292 return ret;
72246da4
FB
1293}
1294
1295/* -------------------------------------------------------------------------- */
1296
1297static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1298 .bLength = USB_DT_ENDPOINT_SIZE,
1299 .bDescriptorType = USB_DT_ENDPOINT,
1300 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1301};
1302
1303static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1304 .enable = dwc3_gadget_ep0_enable,
1305 .disable = dwc3_gadget_ep0_disable,
1306 .alloc_request = dwc3_gadget_ep_alloc_request,
1307 .free_request = dwc3_gadget_ep_free_request,
1308 .queue = dwc3_gadget_ep0_queue,
1309 .dequeue = dwc3_gadget_ep_dequeue,
08f0d966 1310 .set_halt = dwc3_gadget_ep0_set_halt,
72246da4
FB
1311 .set_wedge = dwc3_gadget_ep_set_wedge,
1312};
1313
1314static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1315 .enable = dwc3_gadget_ep_enable,
1316 .disable = dwc3_gadget_ep_disable,
1317 .alloc_request = dwc3_gadget_ep_alloc_request,
1318 .free_request = dwc3_gadget_ep_free_request,
1319 .queue = dwc3_gadget_ep_queue,
1320 .dequeue = dwc3_gadget_ep_dequeue,
1321 .set_halt = dwc3_gadget_ep_set_halt,
1322 .set_wedge = dwc3_gadget_ep_set_wedge,
1323};
1324
1325/* -------------------------------------------------------------------------- */
1326
1327static int dwc3_gadget_get_frame(struct usb_gadget *g)
1328{
1329 struct dwc3 *dwc = gadget_to_dwc(g);
1330 u32 reg;
1331
1332 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1333 return DWC3_DSTS_SOFFN(reg);
1334}
1335
1336static int dwc3_gadget_wakeup(struct usb_gadget *g)
1337{
1338 struct dwc3 *dwc = gadget_to_dwc(g);
1339
1340 unsigned long timeout;
1341 unsigned long flags;
1342
1343 u32 reg;
1344
1345 int ret = 0;
1346
1347 u8 link_state;
1348 u8 speed;
1349
1350 spin_lock_irqsave(&dwc->lock, flags);
1351
1352 /*
1353 * According to the Databook Remote wakeup request should
1354 * be issued only when the device is in early suspend state.
1355 *
1356 * We can check that via USB Link State bits in DSTS register.
1357 */
1358 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1359
1360 speed = reg & DWC3_DSTS_CONNECTSPD;
ee5cd41c
JY
1361 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1362 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
ec5e795c 1363 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
72246da4
FB
1364 ret = -EINVAL;
1365 goto out;
1366 }
1367
1368 link_state = DWC3_DSTS_USBLNKST(reg);
1369
1370 switch (link_state) {
1371 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1372 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1373 break;
1374 default:
ec5e795c
FB
1375 dwc3_trace(trace_dwc3_gadget,
1376 "can't wakeup from '%s'\n",
1377 dwc3_gadget_link_string(link_state));
72246da4
FB
1378 ret = -EINVAL;
1379 goto out;
1380 }
1381
8598bde7
FB
1382 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1383 if (ret < 0) {
1384 dev_err(dwc->dev, "failed to put link in Recovery\n");
1385 goto out;
1386 }
72246da4 1387
802fde98
PZ
1388 /* Recent versions do this automatically */
1389 if (dwc->revision < DWC3_REVISION_194A) {
1390 /* write zeroes to Link Change Request */
fcc023c7 1391 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
802fde98
PZ
1392 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1393 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1394 }
72246da4 1395
1d046793 1396 /* poll until Link State changes to ON */
72246da4
FB
1397 timeout = jiffies + msecs_to_jiffies(100);
1398
1d046793 1399 while (!time_after(jiffies, timeout)) {
72246da4
FB
1400 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1401
1402 /* in HS, means ON */
1403 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1404 break;
1405 }
1406
1407 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1408 dev_err(dwc->dev, "failed to send remote wakeup\n");
1409 ret = -EINVAL;
1410 }
1411
1412out:
1413 spin_unlock_irqrestore(&dwc->lock, flags);
1414
1415 return ret;
1416}
1417
1418static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1419 int is_selfpowered)
1420{
1421 struct dwc3 *dwc = gadget_to_dwc(g);
249a4569 1422 unsigned long flags;
72246da4 1423
249a4569 1424 spin_lock_irqsave(&dwc->lock, flags);
bcdea503 1425 g->is_selfpowered = !!is_selfpowered;
249a4569 1426 spin_unlock_irqrestore(&dwc->lock, flags);
72246da4
FB
1427
1428 return 0;
1429}
1430
7b2a0368 1431static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
72246da4
FB
1432{
1433 u32 reg;
61d58242 1434 u32 timeout = 500;
72246da4
FB
1435
1436 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
8db7ed15 1437 if (is_on) {
802fde98
PZ
1438 if (dwc->revision <= DWC3_REVISION_187A) {
1439 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1440 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1441 }
1442
1443 if (dwc->revision >= DWC3_REVISION_194A)
1444 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1445 reg |= DWC3_DCTL_RUN_STOP;
7b2a0368
FB
1446
1447 if (dwc->has_hibernation)
1448 reg |= DWC3_DCTL_KEEP_CONNECT;
1449
9fcb3bd8 1450 dwc->pullups_connected = true;
8db7ed15 1451 } else {
72246da4 1452 reg &= ~DWC3_DCTL_RUN_STOP;
7b2a0368
FB
1453
1454 if (dwc->has_hibernation && !suspend)
1455 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1456
9fcb3bd8 1457 dwc->pullups_connected = false;
8db7ed15 1458 }
72246da4
FB
1459
1460 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1461
1462 do {
1463 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1464 if (is_on) {
1465 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1466 break;
1467 } else {
1468 if (reg & DWC3_DSTS_DEVCTRLHLT)
1469 break;
1470 }
72246da4
FB
1471 timeout--;
1472 if (!timeout)
6f17f74b 1473 return -ETIMEDOUT;
61d58242 1474 udelay(1);
72246da4
FB
1475 } while (1);
1476
73815280 1477 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
72246da4
FB
1478 dwc->gadget_driver
1479 ? dwc->gadget_driver->function : "no-function",
1480 is_on ? "connect" : "disconnect");
6f17f74b
PA
1481
1482 return 0;
72246da4
FB
1483}
1484
1485static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1486{
1487 struct dwc3 *dwc = gadget_to_dwc(g);
1488 unsigned long flags;
6f17f74b 1489 int ret;
72246da4
FB
1490
1491 is_on = !!is_on;
1492
1493 spin_lock_irqsave(&dwc->lock, flags);
7b2a0368 1494 ret = dwc3_gadget_run_stop(dwc, is_on, false);
72246da4
FB
1495 spin_unlock_irqrestore(&dwc->lock, flags);
1496
6f17f74b 1497 return ret;
72246da4
FB
1498}
1499
8698e2ac
FB
1500static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1501{
1502 u32 reg;
1503
1504 /* Enable all but Start and End of Frame IRQs */
1505 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1506 DWC3_DEVTEN_EVNTOVERFLOWEN |
1507 DWC3_DEVTEN_CMDCMPLTEN |
1508 DWC3_DEVTEN_ERRTICERREN |
1509 DWC3_DEVTEN_WKUPEVTEN |
1510 DWC3_DEVTEN_ULSTCNGEN |
1511 DWC3_DEVTEN_CONNECTDONEEN |
1512 DWC3_DEVTEN_USBRSTEN |
1513 DWC3_DEVTEN_DISCONNEVTEN);
1514
1515 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1516}
1517
1518static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1519{
1520 /* mask all interrupts */
1521 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1522}
1523
1524static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
b15a762f 1525static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
8698e2ac 1526
72246da4
FB
1527static int dwc3_gadget_start(struct usb_gadget *g,
1528 struct usb_gadget_driver *driver)
1529{
1530 struct dwc3 *dwc = gadget_to_dwc(g);
1531 struct dwc3_ep *dep;
1532 unsigned long flags;
1533 int ret = 0;
8698e2ac 1534 int irq;
72246da4
FB
1535 u32 reg;
1536
b0d7ffd4
FB
1537 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1538 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
e8adfc30 1539 IRQF_SHARED, "dwc3", dwc);
b0d7ffd4
FB
1540 if (ret) {
1541 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1542 irq, ret);
1543 goto err0;
1544 }
1545
72246da4
FB
1546 spin_lock_irqsave(&dwc->lock, flags);
1547
1548 if (dwc->gadget_driver) {
1549 dev_err(dwc->dev, "%s is already bound to %s\n",
1550 dwc->gadget.name,
1551 dwc->gadget_driver->driver.name);
1552 ret = -EBUSY;
b0d7ffd4 1553 goto err1;
72246da4
FB
1554 }
1555
1556 dwc->gadget_driver = driver;
72246da4 1557
72246da4
FB
1558 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1559 reg &= ~(DWC3_DCFG_SPEED_MASK);
07e7f47b
FB
1560
1561 /**
1562 * WORKAROUND: DWC3 revision < 2.20a have an issue
1563 * which would cause metastability state on Run/Stop
1564 * bit if we try to force the IP to USB2-only mode.
1565 *
1566 * Because of that, we cannot configure the IP to any
1567 * speed other than the SuperSpeed
1568 *
1569 * Refers to:
1570 *
1571 * STAR#9000525659: Clock Domain Crossing on DCTL in
1572 * USB 2.0 Mode
1573 */
f7e846f0 1574 if (dwc->revision < DWC3_REVISION_220A) {
07e7f47b 1575 reg |= DWC3_DCFG_SUPERSPEED;
f7e846f0
FB
1576 } else {
1577 switch (dwc->maximum_speed) {
1578 case USB_SPEED_LOW:
1579 reg |= DWC3_DSTS_LOWSPEED;
1580 break;
1581 case USB_SPEED_FULL:
1582 reg |= DWC3_DSTS_FULLSPEED1;
1583 break;
1584 case USB_SPEED_HIGH:
1585 reg |= DWC3_DSTS_HIGHSPEED;
1586 break;
7580862b
JY
1587 case USB_SPEED_SUPER_PLUS:
1588 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1589 break;
f7e846f0 1590 default:
77966eb8
JY
1591 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1592 dwc->maximum_speed);
1593 /* fall through */
1594 case USB_SPEED_SUPER:
1595 reg |= DWC3_DCFG_SUPERSPEED;
1596 break;
f7e846f0
FB
1597 }
1598 }
72246da4
FB
1599 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1600
1601 /* Start with SuperSpeed Default */
1602 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1603
1604 dep = dwc->eps[0];
265b70a7
PZ
1605 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1606 false);
72246da4
FB
1607 if (ret) {
1608 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
b0d7ffd4 1609 goto err2;
72246da4
FB
1610 }
1611
1612 dep = dwc->eps[1];
265b70a7
PZ
1613 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1614 false);
72246da4
FB
1615 if (ret) {
1616 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
b0d7ffd4 1617 goto err3;
72246da4
FB
1618 }
1619
1620 /* begin to receive SETUP packets */
c7fcdeb2 1621 dwc->ep0state = EP0_SETUP_PHASE;
72246da4
FB
1622 dwc3_ep0_out_start(dwc);
1623
8698e2ac
FB
1624 dwc3_gadget_enable_irq(dwc);
1625
72246da4
FB
1626 spin_unlock_irqrestore(&dwc->lock, flags);
1627
1628 return 0;
1629
b0d7ffd4 1630err3:
72246da4
FB
1631 __dwc3_gadget_ep_disable(dwc->eps[0]);
1632
b0d7ffd4 1633err2:
cdcedd69 1634 dwc->gadget_driver = NULL;
b0d7ffd4
FB
1635
1636err1:
72246da4
FB
1637 spin_unlock_irqrestore(&dwc->lock, flags);
1638
b0d7ffd4
FB
1639 free_irq(irq, dwc);
1640
1641err0:
72246da4
FB
1642 return ret;
1643}
1644
22835b80 1645static int dwc3_gadget_stop(struct usb_gadget *g)
72246da4
FB
1646{
1647 struct dwc3 *dwc = gadget_to_dwc(g);
1648 unsigned long flags;
8698e2ac 1649 int irq;
72246da4
FB
1650
1651 spin_lock_irqsave(&dwc->lock, flags);
1652
8698e2ac 1653 dwc3_gadget_disable_irq(dwc);
72246da4
FB
1654 __dwc3_gadget_ep_disable(dwc->eps[0]);
1655 __dwc3_gadget_ep_disable(dwc->eps[1]);
1656
1657 dwc->gadget_driver = NULL;
72246da4
FB
1658
1659 spin_unlock_irqrestore(&dwc->lock, flags);
1660
b0d7ffd4
FB
1661 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1662 free_irq(irq, dwc);
1663
72246da4
FB
1664 return 0;
1665}
802fde98 1666
72246da4
FB
1667static const struct usb_gadget_ops dwc3_gadget_ops = {
1668 .get_frame = dwc3_gadget_get_frame,
1669 .wakeup = dwc3_gadget_wakeup,
1670 .set_selfpowered = dwc3_gadget_set_selfpowered,
1671 .pullup = dwc3_gadget_pullup,
1672 .udc_start = dwc3_gadget_start,
1673 .udc_stop = dwc3_gadget_stop,
1674};
1675
1676/* -------------------------------------------------------------------------- */
1677
6a1e3ef4
FB
1678static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1679 u8 num, u32 direction)
72246da4
FB
1680{
1681 struct dwc3_ep *dep;
6a1e3ef4 1682 u8 i;
72246da4 1683
6a1e3ef4
FB
1684 for (i = 0; i < num; i++) {
1685 u8 epnum = (i << 1) | (!!direction);
72246da4 1686
72246da4 1687 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
734d5a53 1688 if (!dep)
72246da4 1689 return -ENOMEM;
72246da4
FB
1690
1691 dep->dwc = dwc;
1692 dep->number = epnum;
9aa62ae4 1693 dep->direction = !!direction;
72246da4
FB
1694 dwc->eps[epnum] = dep;
1695
1696 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1697 (epnum & 1) ? "in" : "out");
6a1e3ef4 1698
72246da4 1699 dep->endpoint.name = dep->name;
72246da4 1700
73815280 1701 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
653df35e 1702
72246da4 1703 if (epnum == 0 || epnum == 1) {
e117e742 1704 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
6048e4c6 1705 dep->endpoint.maxburst = 1;
72246da4
FB
1706 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1707 if (!epnum)
1708 dwc->gadget.ep0 = &dep->endpoint;
1709 } else {
1710 int ret;
1711
e117e742 1712 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
12d36c16 1713 dep->endpoint.max_streams = 15;
72246da4
FB
1714 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1715 list_add_tail(&dep->endpoint.ep_list,
1716 &dwc->gadget.ep_list);
1717
1718 ret = dwc3_alloc_trb_pool(dep);
25b8ff68 1719 if (ret)
72246da4 1720 return ret;
72246da4 1721 }
25b8ff68 1722
a474d3b7
RB
1723 if (epnum == 0 || epnum == 1) {
1724 dep->endpoint.caps.type_control = true;
1725 } else {
1726 dep->endpoint.caps.type_iso = true;
1727 dep->endpoint.caps.type_bulk = true;
1728 dep->endpoint.caps.type_int = true;
1729 }
1730
1731 dep->endpoint.caps.dir_in = !!direction;
1732 dep->endpoint.caps.dir_out = !direction;
1733
72246da4
FB
1734 INIT_LIST_HEAD(&dep->request_list);
1735 INIT_LIST_HEAD(&dep->req_queued);
1736 }
1737
1738 return 0;
1739}
1740
6a1e3ef4
FB
1741static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1742{
1743 int ret;
1744
1745 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1746
1747 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1748 if (ret < 0) {
73815280
FB
1749 dwc3_trace(trace_dwc3_gadget,
1750 "failed to allocate OUT endpoints");
6a1e3ef4
FB
1751 return ret;
1752 }
1753
1754 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1755 if (ret < 0) {
73815280
FB
1756 dwc3_trace(trace_dwc3_gadget,
1757 "failed to allocate IN endpoints");
6a1e3ef4
FB
1758 return ret;
1759 }
1760
1761 return 0;
1762}
1763
72246da4
FB
1764static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1765{
1766 struct dwc3_ep *dep;
1767 u8 epnum;
1768
1769 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1770 dep = dwc->eps[epnum];
6a1e3ef4
FB
1771 if (!dep)
1772 continue;
5bf8fae3
GC
1773 /*
1774 * Physical endpoints 0 and 1 are special; they form the
1775 * bi-directional USB endpoint 0.
1776 *
1777 * For those two physical endpoints, we don't allocate a TRB
1778 * pool nor do we add them the endpoints list. Due to that, we
1779 * shouldn't do these two operations otherwise we would end up
1780 * with all sorts of bugs when removing dwc3.ko.
1781 */
1782 if (epnum != 0 && epnum != 1) {
1783 dwc3_free_trb_pool(dep);
72246da4 1784 list_del(&dep->endpoint.ep_list);
5bf8fae3 1785 }
72246da4
FB
1786
1787 kfree(dep);
1788 }
1789}
1790
72246da4 1791/* -------------------------------------------------------------------------- */
e5caff68 1792
e5ba5ec8
PA
1793static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1794 struct dwc3_request *req, struct dwc3_trb *trb,
72246da4
FB
1795 const struct dwc3_event_depevt *event, int status)
1796{
72246da4
FB
1797 unsigned int count;
1798 unsigned int s_pkt = 0;
d6d6ec7b 1799 unsigned int trb_status;
72246da4 1800
2c4cbe6e
FB
1801 trace_dwc3_complete_trb(dep, trb);
1802
e5ba5ec8
PA
1803 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1804 /*
1805 * We continue despite the error. There is not much we
1806 * can do. If we don't clean it up we loop forever. If
1807 * we skip the TRB then it gets overwritten after a
1808 * while since we use them in a ring buffer. A BUG()
1809 * would help. Lets hope that if this occurs, someone
1810 * fixes the root cause instead of looking away :)
1811 */
1812 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1813 dep->name, trb);
1814 count = trb->size & DWC3_TRB_SIZE_MASK;
1815
1816 if (dep->direction) {
1817 if (count) {
1818 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1819 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
ec5e795c
FB
1820 dwc3_trace(trace_dwc3_gadget,
1821 "%s: incomplete IN transfer\n",
e5ba5ec8
PA
1822 dep->name);
1823 /*
1824 * If missed isoc occurred and there is
1825 * no request queued then issue END
1826 * TRANSFER, so that core generates
1827 * next xfernotready and we will issue
1828 * a fresh START TRANSFER.
1829 * If there are still queued request
1830 * then wait, do not issue either END
1831 * or UPDATE TRANSFER, just attach next
1832 * request in request_list during
1833 * giveback.If any future queued request
1834 * is successfully transferred then we
1835 * will issue UPDATE TRANSFER for all
1836 * request in the request_list.
1837 */
1838 dep->flags |= DWC3_EP_MISSED_ISOC;
1839 } else {
1840 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1841 dep->name);
1842 status = -ECONNRESET;
1843 }
1844 } else {
1845 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1846 }
1847 } else {
1848 if (count && (event->status & DEPEVT_STATUS_SHORT))
1849 s_pkt = 1;
1850 }
1851
1852 /*
1853 * We assume here we will always receive the entire data block
1854 * which we should receive. Meaning, if we program RX to
1855 * receive 4K but we receive only 2K, we assume that's all we
1856 * should receive and we simply bounce the request back to the
1857 * gadget driver for further processing.
1858 */
1859 req->request.actual += req->request.length - count;
1860 if (s_pkt)
1861 return 1;
1862 if ((event->status & DEPEVT_STATUS_LST) &&
1863 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1864 DWC3_TRB_CTRL_HWO)))
1865 return 1;
1866 if ((event->status & DEPEVT_STATUS_IOC) &&
1867 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1868 return 1;
1869 return 0;
1870}
1871
1872static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1873 const struct dwc3_event_depevt *event, int status)
1874{
1875 struct dwc3_request *req;
1876 struct dwc3_trb *trb;
1877 unsigned int slot;
1878 unsigned int i;
1879 int ret;
1880
72246da4 1881 do {
d115d705 1882 req = next_request(&dep->req_queued);
ac7bdcc1 1883 if (WARN_ON_ONCE(!req))
d115d705 1884 return 1;
ac7bdcc1 1885
d115d705
VS
1886 i = 0;
1887 do {
1888 slot = req->start_slot + i;
1889 if ((slot == DWC3_TRB_NUM - 1) &&
e5ba5ec8 1890 usb_endpoint_xfer_isoc(dep->endpoint.desc))
d115d705
VS
1891 slot++;
1892 slot %= DWC3_TRB_NUM;
1893 trb = &dep->trb_pool[slot];
1894
1895 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1896 event, status);
1897 if (ret)
1898 break;
1899 } while (++i < req->request.num_mapped_sgs);
1900
1901 dwc3_gadget_giveback(dep, req, status);
e5ba5ec8
PA
1902
1903 if (ret)
72246da4 1904 break;
d115d705 1905 } while (1);
72246da4 1906
cdc359dd
PA
1907 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1908 list_empty(&dep->req_queued)) {
1909 if (list_empty(&dep->request_list)) {
1910 /*
1911 * If there is no entry in request list then do
1912 * not issue END TRANSFER now. Just set PENDING
1913 * flag, so that END TRANSFER is issued when an
1914 * entry is added into request list.
1915 */
1916 dep->flags = DWC3_EP_PENDING_REQUEST;
1917 } else {
b992e681 1918 dwc3_stop_active_transfer(dwc, dep->number, true);
cdc359dd
PA
1919 dep->flags = DWC3_EP_ENABLED;
1920 }
7efea86c
PA
1921 return 1;
1922 }
1923
72246da4
FB
1924 return 1;
1925}
1926
1927static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
029d97ff 1928 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
72246da4
FB
1929{
1930 unsigned status = 0;
1931 int clean_busy;
e18b7975
FB
1932 u32 is_xfer_complete;
1933
1934 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
72246da4
FB
1935
1936 if (event->status & DEPEVT_STATUS_BUSERR)
1937 status = -ECONNRESET;
1938
1d046793 1939 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
e18b7975
FB
1940 if (clean_busy && (is_xfer_complete ||
1941 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
72246da4 1942 dep->flags &= ~DWC3_EP_BUSY;
fae2b904
FB
1943
1944 /*
1945 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1946 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1947 */
1948 if (dwc->revision < DWC3_REVISION_183A) {
1949 u32 reg;
1950 int i;
1951
1952 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
348e026f 1953 dep = dwc->eps[i];
fae2b904
FB
1954
1955 if (!(dep->flags & DWC3_EP_ENABLED))
1956 continue;
1957
1958 if (!list_empty(&dep->req_queued))
1959 return;
1960 }
1961
1962 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1963 reg |= dwc->u1u2;
1964 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1965
1966 dwc->u1u2 = 0;
1967 }
8a1a9c9e 1968
e6e709b7 1969 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
8a1a9c9e
FB
1970 int ret;
1971
e6e709b7 1972 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
8a1a9c9e
FB
1973 if (!ret || ret == -EBUSY)
1974 return;
1975 }
72246da4
FB
1976}
1977
72246da4
FB
1978static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1979 const struct dwc3_event_depevt *event)
1980{
1981 struct dwc3_ep *dep;
1982 u8 epnum = event->endpoint_number;
1983
1984 dep = dwc->eps[epnum];
1985
3336abb5
FB
1986 if (!(dep->flags & DWC3_EP_ENABLED))
1987 return;
1988
72246da4
FB
1989 if (epnum == 0 || epnum == 1) {
1990 dwc3_ep0_interrupt(dwc, event);
1991 return;
1992 }
1993
1994 switch (event->endpoint_event) {
1995 case DWC3_DEPEVT_XFERCOMPLETE:
b4996a86 1996 dep->resource_index = 0;
c2df85ca 1997
16e78db7 1998 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
ec5e795c
FB
1999 dwc3_trace(trace_dwc3_gadget,
2000 "%s is an Isochronous endpoint\n",
72246da4
FB
2001 dep->name);
2002 return;
2003 }
2004
029d97ff 2005 dwc3_endpoint_transfer_complete(dwc, dep, event);
72246da4
FB
2006 break;
2007 case DWC3_DEPEVT_XFERINPROGRESS:
029d97ff 2008 dwc3_endpoint_transfer_complete(dwc, dep, event);
72246da4
FB
2009 break;
2010 case DWC3_DEPEVT_XFERNOTREADY:
16e78db7 2011 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
2012 dwc3_gadget_start_isoc(dwc, dep, event);
2013 } else {
6bb4fe12 2014 int active;
72246da4
FB
2015 int ret;
2016
6bb4fe12
FB
2017 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2018
73815280 2019 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
6bb4fe12 2020 dep->name, active ? "Transfer Active"
72246da4
FB
2021 : "Transfer Not Active");
2022
6bb4fe12 2023 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
72246da4
FB
2024 if (!ret || ret == -EBUSY)
2025 return;
2026
ec5e795c
FB
2027 dwc3_trace(trace_dwc3_gadget,
2028 "%s: failed to kick transfers\n",
72246da4
FB
2029 dep->name);
2030 }
2031
879631aa
FB
2032 break;
2033 case DWC3_DEPEVT_STREAMEVT:
16e78db7 2034 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
879631aa
FB
2035 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2036 dep->name);
2037 return;
2038 }
2039
2040 switch (event->status) {
2041 case DEPEVT_STREAMEVT_FOUND:
73815280
FB
2042 dwc3_trace(trace_dwc3_gadget,
2043 "Stream %d found and started",
879631aa
FB
2044 event->parameters);
2045
2046 break;
2047 case DEPEVT_STREAMEVT_NOTFOUND:
2048 /* FALLTHROUGH */
2049 default:
ec5e795c
FB
2050 dwc3_trace(trace_dwc3_gadget,
2051 "unable to find suitable stream\n");
879631aa 2052 }
72246da4
FB
2053 break;
2054 case DWC3_DEPEVT_RXTXFIFOEVT:
ec5e795c 2055 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
72246da4 2056 break;
72246da4 2057 case DWC3_DEPEVT_EPCMDCMPLT:
73815280 2058 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
72246da4
FB
2059 break;
2060 }
2061}
2062
2063static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2064{
2065 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2066 spin_unlock(&dwc->lock);
2067 dwc->gadget_driver->disconnect(&dwc->gadget);
2068 spin_lock(&dwc->lock);
2069 }
2070}
2071
bc5ba2e0
FB
2072static void dwc3_suspend_gadget(struct dwc3 *dwc)
2073{
73a30bfc 2074 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
bc5ba2e0
FB
2075 spin_unlock(&dwc->lock);
2076 dwc->gadget_driver->suspend(&dwc->gadget);
2077 spin_lock(&dwc->lock);
2078 }
2079}
2080
2081static void dwc3_resume_gadget(struct dwc3 *dwc)
2082{
73a30bfc 2083 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
bc5ba2e0
FB
2084 spin_unlock(&dwc->lock);
2085 dwc->gadget_driver->resume(&dwc->gadget);
5c7b3b02 2086 spin_lock(&dwc->lock);
8e74475b
FB
2087 }
2088}
2089
2090static void dwc3_reset_gadget(struct dwc3 *dwc)
2091{
2092 if (!dwc->gadget_driver)
2093 return;
2094
2095 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2096 spin_unlock(&dwc->lock);
2097 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
bc5ba2e0
FB
2098 spin_lock(&dwc->lock);
2099 }
2100}
2101
b992e681 2102static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
72246da4
FB
2103{
2104 struct dwc3_ep *dep;
2105 struct dwc3_gadget_ep_cmd_params params;
2106 u32 cmd;
2107 int ret;
2108
2109 dep = dwc->eps[epnum];
2110
b4996a86 2111 if (!dep->resource_index)
3daf74d7
PA
2112 return;
2113
57911504
PA
2114 /*
2115 * NOTICE: We are violating what the Databook says about the
2116 * EndTransfer command. Ideally we would _always_ wait for the
2117 * EndTransfer Command Completion IRQ, but that's causing too
2118 * much trouble synchronizing between us and gadget driver.
2119 *
2120 * We have discussed this with the IP Provider and it was
2121 * suggested to giveback all requests here, but give HW some
2122 * extra time to synchronize with the interconnect. We're using
dc93b41a 2123 * an arbitrary 100us delay for that.
57911504
PA
2124 *
2125 * Note also that a similar handling was tested by Synopsys
2126 * (thanks a lot Paul) and nothing bad has come out of it.
2127 * In short, what we're doing is:
2128 *
2129 * - Issue EndTransfer WITH CMDIOC bit set
2130 * - Wait 100us
2131 */
2132
3daf74d7 2133 cmd = DWC3_DEPCMD_ENDTRANSFER;
b992e681
PZ
2134 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2135 cmd |= DWC3_DEPCMD_CMDIOC;
b4996a86 2136 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3daf74d7
PA
2137 memset(&params, 0, sizeof(params));
2138 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2139 WARN_ON_ONCE(ret);
b4996a86 2140 dep->resource_index = 0;
041d81f4 2141 dep->flags &= ~DWC3_EP_BUSY;
57911504 2142 udelay(100);
72246da4
FB
2143}
2144
2145static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2146{
2147 u32 epnum;
2148
2149 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2150 struct dwc3_ep *dep;
2151
2152 dep = dwc->eps[epnum];
6a1e3ef4
FB
2153 if (!dep)
2154 continue;
2155
72246da4
FB
2156 if (!(dep->flags & DWC3_EP_ENABLED))
2157 continue;
2158
624407f9 2159 dwc3_remove_requests(dwc, dep);
72246da4
FB
2160 }
2161}
2162
2163static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2164{
2165 u32 epnum;
2166
2167 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2168 struct dwc3_ep *dep;
2169 struct dwc3_gadget_ep_cmd_params params;
2170 int ret;
2171
2172 dep = dwc->eps[epnum];
6a1e3ef4
FB
2173 if (!dep)
2174 continue;
72246da4
FB
2175
2176 if (!(dep->flags & DWC3_EP_STALL))
2177 continue;
2178
2179 dep->flags &= ~DWC3_EP_STALL;
2180
2181 memset(&params, 0, sizeof(params));
2182 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2183 DWC3_DEPCMD_CLEARSTALL, &params);
2184 WARN_ON_ONCE(ret);
2185 }
2186}
2187
2188static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2189{
c4430a26
FB
2190 int reg;
2191
72246da4
FB
2192 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2193 reg &= ~DWC3_DCTL_INITU1ENA;
2194 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2195
2196 reg &= ~DWC3_DCTL_INITU2ENA;
2197 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72246da4 2198
72246da4
FB
2199 dwc3_disconnect_gadget(dwc);
2200
2201 dwc->gadget.speed = USB_SPEED_UNKNOWN;
df62df56 2202 dwc->setup_packet_pending = false;
06a374ed 2203 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
72246da4
FB
2204}
2205
72246da4
FB
2206static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2207{
2208 u32 reg;
2209
df62df56
FB
2210 /*
2211 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2212 * would cause a missing Disconnect Event if there's a
2213 * pending Setup Packet in the FIFO.
2214 *
2215 * There's no suggested workaround on the official Bug
2216 * report, which states that "unless the driver/application
2217 * is doing any special handling of a disconnect event,
2218 * there is no functional issue".
2219 *
2220 * Unfortunately, it turns out that we _do_ some special
2221 * handling of a disconnect event, namely complete all
2222 * pending transfers, notify gadget driver of the
2223 * disconnection, and so on.
2224 *
2225 * Our suggested workaround is to follow the Disconnect
2226 * Event steps here, instead, based on a setup_packet_pending
b5d335e5
FB
2227 * flag. Such flag gets set whenever we have a SETUP_PENDING
2228 * status for EP0 TRBs and gets cleared on XferComplete for the
df62df56
FB
2229 * same endpoint.
2230 *
2231 * Refers to:
2232 *
2233 * STAR#9000466709: RTL: Device : Disconnect event not
2234 * generated if setup packet pending in FIFO
2235 */
2236 if (dwc->revision < DWC3_REVISION_188A) {
2237 if (dwc->setup_packet_pending)
2238 dwc3_gadget_disconnect_interrupt(dwc);
2239 }
2240
8e74475b 2241 dwc3_reset_gadget(dwc);
72246da4
FB
2242
2243 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2244 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2245 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3b637367 2246 dwc->test_mode = false;
72246da4
FB
2247
2248 dwc3_stop_active_transfers(dwc);
2249 dwc3_clear_stall_all_ep(dwc);
2250
2251 /* Reset device address to zero */
2252 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2253 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2254 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
72246da4
FB
2255}
2256
2257static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2258{
2259 u32 reg;
2260 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2261
2262 /*
2263 * We change the clock only at SS but I dunno why I would want to do
2264 * this. Maybe it becomes part of the power saving plan.
2265 */
2266
ee5cd41c
JY
2267 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2268 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
72246da4
FB
2269 return;
2270
2271 /*
2272 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2273 * each time on Connect Done.
2274 */
2275 if (!usb30_clock)
2276 return;
2277
2278 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2279 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2280 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2281}
2282
72246da4
FB
2283static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2284{
72246da4
FB
2285 struct dwc3_ep *dep;
2286 int ret;
2287 u32 reg;
2288 u8 speed;
2289
72246da4
FB
2290 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2291 speed = reg & DWC3_DSTS_CONNECTSPD;
2292 dwc->speed = speed;
2293
2294 dwc3_update_ram_clk_sel(dwc, speed);
2295
2296 switch (speed) {
7580862b
JY
2297 case DWC3_DCFG_SUPERSPEED_PLUS:
2298 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2299 dwc->gadget.ep0->maxpacket = 512;
2300 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2301 break;
72246da4 2302 case DWC3_DCFG_SUPERSPEED:
05870c5b
FB
2303 /*
2304 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2305 * would cause a missing USB3 Reset event.
2306 *
2307 * In such situations, we should force a USB3 Reset
2308 * event by calling our dwc3_gadget_reset_interrupt()
2309 * routine.
2310 *
2311 * Refers to:
2312 *
2313 * STAR#9000483510: RTL: SS : USB3 reset event may
2314 * not be generated always when the link enters poll
2315 */
2316 if (dwc->revision < DWC3_REVISION_190A)
2317 dwc3_gadget_reset_interrupt(dwc);
2318
72246da4
FB
2319 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2320 dwc->gadget.ep0->maxpacket = 512;
2321 dwc->gadget.speed = USB_SPEED_SUPER;
2322 break;
2323 case DWC3_DCFG_HIGHSPEED:
2324 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2325 dwc->gadget.ep0->maxpacket = 64;
2326 dwc->gadget.speed = USB_SPEED_HIGH;
2327 break;
2328 case DWC3_DCFG_FULLSPEED2:
2329 case DWC3_DCFG_FULLSPEED1:
2330 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2331 dwc->gadget.ep0->maxpacket = 64;
2332 dwc->gadget.speed = USB_SPEED_FULL;
2333 break;
2334 case DWC3_DCFG_LOWSPEED:
2335 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2336 dwc->gadget.ep0->maxpacket = 8;
2337 dwc->gadget.speed = USB_SPEED_LOW;
2338 break;
2339 }
2340
2b758350
PA
2341 /* Enable USB2 LPM Capability */
2342
ee5cd41c
JY
2343 if ((dwc->revision > DWC3_REVISION_194A) &&
2344 (speed != DWC3_DCFG_SUPERSPEED) &&
2345 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
2b758350
PA
2346 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2347 reg |= DWC3_DCFG_LPM_CAP;
2348 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2349
2350 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2351 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2352
460d098c 2353 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2b758350 2354
80caf7d2
HR
2355 /*
2356 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2357 * DCFG.LPMCap is set, core responses with an ACK and the
2358 * BESL value in the LPM token is less than or equal to LPM
2359 * NYET threshold.
2360 */
2361 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2362 && dwc->has_lpm_erratum,
2363 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2364
2365 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2366 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2367
356363bf
FB
2368 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2369 } else {
2370 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2371 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2b758350
PA
2372 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2373 }
2374
72246da4 2375 dep = dwc->eps[0];
265b70a7
PZ
2376 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2377 false);
72246da4
FB
2378 if (ret) {
2379 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2380 return;
2381 }
2382
2383 dep = dwc->eps[1];
265b70a7
PZ
2384 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2385 false);
72246da4
FB
2386 if (ret) {
2387 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2388 return;
2389 }
2390
2391 /*
2392 * Configure PHY via GUSB3PIPECTLn if required.
2393 *
2394 * Update GTXFIFOSIZn
2395 *
2396 * In both cases reset values should be sufficient.
2397 */
2398}
2399
2400static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2401{
72246da4
FB
2402 /*
2403 * TODO take core out of low power mode when that's
2404 * implemented.
2405 */
2406
ad14d4e0
JL
2407 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2408 spin_unlock(&dwc->lock);
2409 dwc->gadget_driver->resume(&dwc->gadget);
2410 spin_lock(&dwc->lock);
2411 }
72246da4
FB
2412}
2413
2414static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2415 unsigned int evtinfo)
2416{
fae2b904 2417 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
0b0cc1cd
FB
2418 unsigned int pwropt;
2419
2420 /*
2421 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2422 * Hibernation mode enabled which would show up when device detects
2423 * host-initiated U3 exit.
2424 *
2425 * In that case, device will generate a Link State Change Interrupt
2426 * from U3 to RESUME which is only necessary if Hibernation is
2427 * configured in.
2428 *
2429 * There are no functional changes due to such spurious event and we
2430 * just need to ignore it.
2431 *
2432 * Refers to:
2433 *
2434 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2435 * operational mode
2436 */
2437 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2438 if ((dwc->revision < DWC3_REVISION_250A) &&
2439 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2440 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2441 (next == DWC3_LINK_STATE_RESUME)) {
73815280
FB
2442 dwc3_trace(trace_dwc3_gadget,
2443 "ignoring transition U3 -> Resume");
0b0cc1cd
FB
2444 return;
2445 }
2446 }
fae2b904
FB
2447
2448 /*
2449 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2450 * on the link partner, the USB session might do multiple entry/exit
2451 * of low power states before a transfer takes place.
2452 *
2453 * Due to this problem, we might experience lower throughput. The
2454 * suggested workaround is to disable DCTL[12:9] bits if we're
2455 * transitioning from U1/U2 to U0 and enable those bits again
2456 * after a transfer completes and there are no pending transfers
2457 * on any of the enabled endpoints.
2458 *
2459 * This is the first half of that workaround.
2460 *
2461 * Refers to:
2462 *
2463 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2464 * core send LGO_Ux entering U0
2465 */
2466 if (dwc->revision < DWC3_REVISION_183A) {
2467 if (next == DWC3_LINK_STATE_U0) {
2468 u32 u1u2;
2469 u32 reg;
2470
2471 switch (dwc->link_state) {
2472 case DWC3_LINK_STATE_U1:
2473 case DWC3_LINK_STATE_U2:
2474 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2475 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2476 | DWC3_DCTL_ACCEPTU2ENA
2477 | DWC3_DCTL_INITU1ENA
2478 | DWC3_DCTL_ACCEPTU1ENA);
2479
2480 if (!dwc->u1u2)
2481 dwc->u1u2 = reg & u1u2;
2482
2483 reg &= ~u1u2;
2484
2485 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2486 break;
2487 default:
2488 /* do nothing */
2489 break;
2490 }
2491 }
2492 }
2493
bc5ba2e0
FB
2494 switch (next) {
2495 case DWC3_LINK_STATE_U1:
2496 if (dwc->speed == USB_SPEED_SUPER)
2497 dwc3_suspend_gadget(dwc);
2498 break;
2499 case DWC3_LINK_STATE_U2:
2500 case DWC3_LINK_STATE_U3:
2501 dwc3_suspend_gadget(dwc);
2502 break;
2503 case DWC3_LINK_STATE_RESUME:
2504 dwc3_resume_gadget(dwc);
2505 break;
2506 default:
2507 /* do nothing */
2508 break;
2509 }
2510
e57ebc1d 2511 dwc->link_state = next;
72246da4
FB
2512}
2513
e1dadd3b
FB
2514static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2515 unsigned int evtinfo)
2516{
2517 unsigned int is_ss = evtinfo & BIT(4);
2518
2519 /**
2520 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2521 * have a known issue which can cause USB CV TD.9.23 to fail
2522 * randomly.
2523 *
2524 * Because of this issue, core could generate bogus hibernation
2525 * events which SW needs to ignore.
2526 *
2527 * Refers to:
2528 *
2529 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2530 * Device Fallback from SuperSpeed
2531 */
2532 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2533 return;
2534
2535 /* enter hibernation here */
2536}
2537
72246da4
FB
2538static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2539 const struct dwc3_event_devt *event)
2540{
2541 switch (event->type) {
2542 case DWC3_DEVICE_EVENT_DISCONNECT:
2543 dwc3_gadget_disconnect_interrupt(dwc);
2544 break;
2545 case DWC3_DEVICE_EVENT_RESET:
2546 dwc3_gadget_reset_interrupt(dwc);
2547 break;
2548 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2549 dwc3_gadget_conndone_interrupt(dwc);
2550 break;
2551 case DWC3_DEVICE_EVENT_WAKEUP:
2552 dwc3_gadget_wakeup_interrupt(dwc);
2553 break;
e1dadd3b
FB
2554 case DWC3_DEVICE_EVENT_HIBER_REQ:
2555 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2556 "unexpected hibernation event\n"))
2557 break;
2558
2559 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2560 break;
72246da4
FB
2561 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2562 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2563 break;
2564 case DWC3_DEVICE_EVENT_EOPF:
73815280 2565 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
72246da4
FB
2566 break;
2567 case DWC3_DEVICE_EVENT_SOF:
73815280 2568 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
72246da4
FB
2569 break;
2570 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
73815280 2571 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
72246da4
FB
2572 break;
2573 case DWC3_DEVICE_EVENT_CMD_CMPL:
73815280 2574 dwc3_trace(trace_dwc3_gadget, "Command Complete");
72246da4
FB
2575 break;
2576 case DWC3_DEVICE_EVENT_OVERFLOW:
73815280 2577 dwc3_trace(trace_dwc3_gadget, "Overflow");
72246da4
FB
2578 break;
2579 default:
e9f2aa87 2580 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
72246da4
FB
2581 }
2582}
2583
2584static void dwc3_process_event_entry(struct dwc3 *dwc,
2585 const union dwc3_event *event)
2586{
2c4cbe6e
FB
2587 trace_dwc3_event(event->raw);
2588
72246da4
FB
2589 /* Endpoint IRQ, handle it and return early */
2590 if (event->type.is_devspec == 0) {
2591 /* depevt */
2592 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2593 }
2594
2595 switch (event->type.type) {
2596 case DWC3_EVENT_TYPE_DEV:
2597 dwc3_gadget_interrupt(dwc, &event->devt);
2598 break;
2599 /* REVISIT what to do with Carkit and I2C events ? */
2600 default:
2601 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2602 }
2603}
2604
f42f2447 2605static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
b15a762f 2606{
f42f2447 2607 struct dwc3_event_buffer *evt;
b15a762f 2608 irqreturn_t ret = IRQ_NONE;
f42f2447 2609 int left;
e8adfc30 2610 u32 reg;
b15a762f 2611
f42f2447
FB
2612 evt = dwc->ev_buffs[buf];
2613 left = evt->count;
b15a762f 2614
f42f2447
FB
2615 if (!(evt->flags & DWC3_EVENT_PENDING))
2616 return IRQ_NONE;
b15a762f 2617
f42f2447
FB
2618 while (left > 0) {
2619 union dwc3_event event;
b15a762f 2620
f42f2447 2621 event.raw = *(u32 *) (evt->buf + evt->lpos);
b15a762f 2622
f42f2447 2623 dwc3_process_event_entry(dwc, &event);
b15a762f 2624
f42f2447
FB
2625 /*
2626 * FIXME we wrap around correctly to the next entry as
2627 * almost all entries are 4 bytes in size. There is one
2628 * entry which has 12 bytes which is a regular entry
2629 * followed by 8 bytes data. ATM I don't know how
2630 * things are organized if we get next to the a
2631 * boundary so I worry about that once we try to handle
2632 * that.
2633 */
2634 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2635 left -= 4;
b15a762f 2636
f42f2447
FB
2637 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2638 }
b15a762f 2639
f42f2447
FB
2640 evt->count = 0;
2641 evt->flags &= ~DWC3_EVENT_PENDING;
2642 ret = IRQ_HANDLED;
b15a762f 2643
f42f2447
FB
2644 /* Unmask interrupt */
2645 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2646 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2647 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
b15a762f 2648
f42f2447
FB
2649 return ret;
2650}
e8adfc30 2651
f42f2447
FB
2652static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2653{
2654 struct dwc3 *dwc = _dwc;
e5f68b4a 2655 unsigned long flags;
f42f2447
FB
2656 irqreturn_t ret = IRQ_NONE;
2657 int i;
2658
e5f68b4a 2659 spin_lock_irqsave(&dwc->lock, flags);
f42f2447
FB
2660
2661 for (i = 0; i < dwc->num_event_buffers; i++)
2662 ret |= dwc3_process_event_buf(dwc, i);
b15a762f 2663
e5f68b4a 2664 spin_unlock_irqrestore(&dwc->lock, flags);
b15a762f
FB
2665
2666 return ret;
2667}
2668
7f97aa98 2669static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
72246da4
FB
2670{
2671 struct dwc3_event_buffer *evt;
72246da4 2672 u32 count;
e8adfc30 2673 u32 reg;
72246da4 2674
b15a762f
FB
2675 evt = dwc->ev_buffs[buf];
2676
72246da4
FB
2677 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2678 count &= DWC3_GEVNTCOUNT_MASK;
2679 if (!count)
2680 return IRQ_NONE;
2681
b15a762f
FB
2682 evt->count = count;
2683 evt->flags |= DWC3_EVENT_PENDING;
72246da4 2684
e8adfc30
FB
2685 /* Mask interrupt */
2686 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2687 reg |= DWC3_GEVNTSIZ_INTMASK;
2688 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2689
b15a762f 2690 return IRQ_WAKE_THREAD;
72246da4
FB
2691}
2692
2693static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2694{
2695 struct dwc3 *dwc = _dwc;
2696 int i;
2697 irqreturn_t ret = IRQ_NONE;
2698
9f622b2a 2699 for (i = 0; i < dwc->num_event_buffers; i++) {
72246da4
FB
2700 irqreturn_t status;
2701
7f97aa98 2702 status = dwc3_check_event_buf(dwc, i);
b15a762f 2703 if (status == IRQ_WAKE_THREAD)
72246da4
FB
2704 ret = status;
2705 }
2706
72246da4
FB
2707 return ret;
2708}
2709
2710/**
2711 * dwc3_gadget_init - Initializes gadget related registers
1d046793 2712 * @dwc: pointer to our controller context structure
72246da4
FB
2713 *
2714 * Returns 0 on success otherwise negative errno.
2715 */
41ac7b3a 2716int dwc3_gadget_init(struct dwc3 *dwc)
72246da4 2717{
72246da4 2718 int ret;
72246da4
FB
2719
2720 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2721 &dwc->ctrl_req_addr, GFP_KERNEL);
2722 if (!dwc->ctrl_req) {
2723 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2724 ret = -ENOMEM;
2725 goto err0;
2726 }
2727
2abd9d5f 2728 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
72246da4
FB
2729 &dwc->ep0_trb_addr, GFP_KERNEL);
2730 if (!dwc->ep0_trb) {
2731 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2732 ret = -ENOMEM;
2733 goto err1;
2734 }
2735
3ef35faf 2736 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
72246da4 2737 if (!dwc->setup_buf) {
72246da4
FB
2738 ret = -ENOMEM;
2739 goto err2;
2740 }
2741
5812b1c2 2742 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
3ef35faf
FB
2743 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2744 GFP_KERNEL);
5812b1c2
FB
2745 if (!dwc->ep0_bounce) {
2746 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2747 ret = -ENOMEM;
2748 goto err3;
2749 }
2750
04c03d10
FB
2751 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2752 if (!dwc->zlp_buf) {
2753 ret = -ENOMEM;
2754 goto err4;
2755 }
2756
72246da4 2757 dwc->gadget.ops = &dwc3_gadget_ops;
72246da4 2758 dwc->gadget.speed = USB_SPEED_UNKNOWN;
eeb720fb 2759 dwc->gadget.sg_supported = true;
72246da4 2760 dwc->gadget.name = "dwc3-gadget";
6a4290cc 2761 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
72246da4 2762
b9e51b2b
BM
2763 /*
2764 * FIXME We might be setting max_speed to <SUPER, however versions
2765 * <2.20a of dwc3 have an issue with metastability (documented
2766 * elsewhere in this driver) which tells us we can't set max speed to
2767 * anything lower than SUPER.
2768 *
2769 * Because gadget.max_speed is only used by composite.c and function
2770 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2771 * to happen so we avoid sending SuperSpeed Capability descriptor
2772 * together with our BOS descriptor as that could confuse host into
2773 * thinking we can handle super speed.
2774 *
2775 * Note that, in fact, we won't even support GetBOS requests when speed
2776 * is less than super speed because we don't have means, yet, to tell
2777 * composite.c that we are USB 2.0 + LPM ECN.
2778 */
2779 if (dwc->revision < DWC3_REVISION_220A)
2780 dwc3_trace(trace_dwc3_gadget,
2781 "Changing max_speed on rev %08x\n",
2782 dwc->revision);
2783
2784 dwc->gadget.max_speed = dwc->maximum_speed;
2785
a4b9d94b
DC
2786 /*
2787 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2788 * on ep out.
2789 */
2790 dwc->gadget.quirk_ep_out_aligned_size = true;
2791
72246da4
FB
2792 /*
2793 * REVISIT: Here we should clear all pending IRQs to be
2794 * sure we're starting from a well known location.
2795 */
2796
2797 ret = dwc3_gadget_init_endpoints(dwc);
2798 if (ret)
04c03d10 2799 goto err5;
72246da4 2800
72246da4
FB
2801 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2802 if (ret) {
2803 dev_err(dwc->dev, "failed to register udc\n");
04c03d10 2804 goto err5;
72246da4
FB
2805 }
2806
2807 return 0;
2808
04c03d10
FB
2809err5:
2810 kfree(dwc->zlp_buf);
2811
5812b1c2 2812err4:
e1f80467 2813 dwc3_gadget_free_endpoints(dwc);
3ef35faf
FB
2814 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2815 dwc->ep0_bounce, dwc->ep0_bounce_addr);
5812b1c2 2816
72246da4 2817err3:
0fc9a1be 2818 kfree(dwc->setup_buf);
72246da4
FB
2819
2820err2:
2821 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2822 dwc->ep0_trb, dwc->ep0_trb_addr);
2823
2824err1:
2825 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2826 dwc->ctrl_req, dwc->ctrl_req_addr);
2827
2828err0:
2829 return ret;
2830}
2831
7415f17c
FB
2832/* -------------------------------------------------------------------------- */
2833
72246da4
FB
2834void dwc3_gadget_exit(struct dwc3 *dwc)
2835{
72246da4 2836 usb_del_gadget_udc(&dwc->gadget);
72246da4 2837
72246da4
FB
2838 dwc3_gadget_free_endpoints(dwc);
2839
3ef35faf
FB
2840 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2841 dwc->ep0_bounce, dwc->ep0_bounce_addr);
5812b1c2 2842
0fc9a1be 2843 kfree(dwc->setup_buf);
04c03d10 2844 kfree(dwc->zlp_buf);
72246da4
FB
2845
2846 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2847 dwc->ep0_trb, dwc->ep0_trb_addr);
2848
2849 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2850 dwc->ctrl_req, dwc->ctrl_req_addr);
72246da4 2851}
7415f17c 2852
0b0231aa 2853int dwc3_gadget_suspend(struct dwc3 *dwc)
7415f17c 2854{
7b2a0368 2855 if (dwc->pullups_connected) {
7415f17c 2856 dwc3_gadget_disable_irq(dwc);
7b2a0368
FB
2857 dwc3_gadget_run_stop(dwc, true, true);
2858 }
7415f17c 2859
7415f17c
FB
2860 __dwc3_gadget_ep_disable(dwc->eps[0]);
2861 __dwc3_gadget_ep_disable(dwc->eps[1]);
2862
2863 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2864
2865 return 0;
2866}
2867
2868int dwc3_gadget_resume(struct dwc3 *dwc)
2869{
2870 struct dwc3_ep *dep;
2871 int ret;
2872
2873 /* Start with SuperSpeed Default */
2874 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2875
2876 dep = dwc->eps[0];
265b70a7
PZ
2877 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2878 false);
7415f17c
FB
2879 if (ret)
2880 goto err0;
2881
2882 dep = dwc->eps[1];
265b70a7
PZ
2883 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2884 false);
7415f17c
FB
2885 if (ret)
2886 goto err1;
2887
2888 /* begin to receive SETUP packets */
2889 dwc->ep0state = EP0_SETUP_PHASE;
2890 dwc3_ep0_out_start(dwc);
2891
2892 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2893
0b0231aa
FB
2894 if (dwc->pullups_connected) {
2895 dwc3_gadget_enable_irq(dwc);
2896 dwc3_gadget_run_stop(dwc, true, false);
2897 }
2898
7415f17c
FB
2899 return 0;
2900
2901err1:
2902 __dwc3_gadget_ep_disable(dwc->eps[0]);
2903
2904err0:
2905 return ret;
2906}