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