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