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