blob: a6e682a000fc7d424cb946be31c525d5801a05ae [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
25#include "debug.h"
26#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
30#define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \
31 & ~((d)->interval - 1))
32
33/**
34 * dwc3_gadget_set_test_mode - enables usb2 test modes
35 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
40 */
41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48 switch (mode) {
49 case TEST_J:
50 case TEST_K:
51 case TEST_SE0_NAK:
52 case TEST_PACKET:
53 case TEST_FORCE_EN:
54 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
60 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
61
62 return 0;
63}
64
65/**
66 * dwc3_gadget_get_link_state - gets current state of usb link
67 * @dwc: pointer to our context structure
68 *
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
71 */
72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78 return DWC3_DSTS_USBLNKST(reg);
79}
80
81/**
82 * dwc3_gadget_set_link_state - sets usb link to a particular state
83 * @dwc: pointer to our context structure
84 * @state: the state to put link into
85 *
86 * Caller should take care of locking. This function will
87 * return 0 on success or -ETIMEDOUT.
88 */
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90{
91 int retries = 10000;
92 u32 reg;
93
94 /*
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
97 */
98 if (dwc->revision >= DWC3_REVISION_194A) {
99 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
114 /* set requested state */
115 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
117
118 /*
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
121 */
122 if (dwc->revision >= DWC3_REVISION_194A)
123 return 0;
124
125 /* wait for a change in DSTS */
126 retries = 10000;
127 while (--retries) {
128 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
129
130 if (DWC3_DSTS_USBLNKST(reg) == state)
131 return 0;
132
133 udelay(5);
134 }
135
136 return -ETIMEDOUT;
137}
138
139/**
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
142 *
143 * The index should never point to the link TRB. After incrementing,
144 * if it is point to the link TRB, wrap around to the beginning. The
145 * link TRB is always at the last TRB entry.
146 */
147static void dwc3_ep_inc_trb(u8 *index)
148{
149 (*index)++;
150 if (*index == (DWC3_TRB_NUM - 1))
151 *index = 0;
152}
153
154/**
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
157 */
158static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
159{
160 dwc3_ep_inc_trb(&dep->trb_enqueue);
161}
162
163/**
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
166 */
167static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
168{
169 dwc3_ep_inc_trb(&dep->trb_dequeue);
170}
171
172static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173 struct dwc3_request *req, int status)
174{
175 struct dwc3 *dwc = dep->dwc;
176
177 req->started = false;
178 list_del(&req->list);
179 req->remaining = 0;
180 req->needs_extra_trb = false;
181
182 if (req->request.status == -EINPROGRESS)
183 req->request.status = status;
184
185 if (req->trb)
186 usb_gadget_unmap_request_by_dev(dwc->sysdev,
187 &req->request, req->direction);
188
189 req->trb = NULL;
190 trace_dwc3_gadget_giveback(req);
191
192 if (dep->number > 1)
193 pm_runtime_put(dwc->dev);
194}
195
196/**
197 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
198 * @dep: The endpoint to whom the request belongs to
199 * @req: The request we're giving back
200 * @status: completion code for the request
201 *
202 * Must be called with controller's lock held and interrupts disabled. This
203 * function will unmap @req and call its ->complete() callback to notify upper
204 * layers that it has completed.
205 */
206void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
207 int status)
208{
209 struct dwc3 *dwc = dep->dwc;
210
211 dwc3_gadget_del_and_unmap_request(dep, req, status);
212
213 spin_unlock(&dwc->lock);
214 usb_gadget_giveback_request(&dep->endpoint, &req->request);
215 spin_lock(&dwc->lock);
216}
217
218/**
219 * dwc3_send_gadget_generic_command - issue a generic command for the controller
220 * @dwc: pointer to the controller context
221 * @cmd: the command to be issued
222 * @param: command parameter
223 *
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
226 */
227int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
228{
229 u32 timeout = 500;
230 int status = 0;
231 int ret = 0;
232 u32 reg;
233
234 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
236
237 do {
238 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
239 if (!(reg & DWC3_DGCMD_CMDACT)) {
240 status = DWC3_DGCMD_STATUS(reg);
241 if (status)
242 ret = -EINVAL;
243 break;
244 }
245 } while (--timeout);
246
247 if (!timeout) {
248 ret = -ETIMEDOUT;
249 status = -ETIMEDOUT;
250 }
251
252 trace_dwc3_gadget_generic_cmd(cmd, param, status);
253
254 return ret;
255}
256
257static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
258
259/**
260 * dwc3_send_gadget_ep_cmd - issue an endpoint command
261 * @dep: the endpoint to which the command is going to be issued
262 * @cmd: the command to be issued
263 * @params: parameters to the command
264 *
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
267 */
268int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269 struct dwc3_gadget_ep_cmd_params *params)
270{
271 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
272 struct dwc3 *dwc = dep->dwc;
273 u32 timeout = 1000;
274 u32 saved_config = 0;
275 u32 reg;
276
277 int cmd_status = 0;
278 int ret = -EINVAL;
279
280 /*
281 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
282 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
283 * endpoint command.
284 *
285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
287 *
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
289 */
290 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
293 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
294 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
295 }
296
297 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
300 }
301
302 if (saved_config)
303 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
304 }
305
306 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
307 int needs_wakeup;
308
309 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
310 dwc->link_state == DWC3_LINK_STATE_U2 ||
311 dwc->link_state == DWC3_LINK_STATE_U3);
312
313 if (unlikely(needs_wakeup)) {
314 ret = __dwc3_gadget_wakeup(dwc);
315 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316 ret);
317 }
318 }
319
320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
322 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
323
324 /*
325 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
326 * not relying on XferNotReady, we can make use of a special "No
327 * Response Update Transfer" command where we should clear both CmdAct
328 * and CmdIOC bits.
329 *
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
332 *
333 * NOTICE: We're making an assumption that control endpoints will never
334 * make use of Update Transfer command. This is a safe assumption
335 * because we can never have more than one request at a time with
336 * Control Endpoints. If anybody changes that assumption, this chunk
337 * needs to be updated accordingly.
338 */
339 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
340 !usb_endpoint_xfer_isoc(desc))
341 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
342 else
343 cmd |= DWC3_DEPCMD_CMDACT;
344
345 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
346 do {
347 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
348 if (!(reg & DWC3_DEPCMD_CMDACT)) {
349 cmd_status = DWC3_DEPCMD_STATUS(reg);
350
351 switch (cmd_status) {
352 case 0:
353 ret = 0;
354 break;
355 case DEPEVT_TRANSFER_NO_RESOURCE:
356 ret = -EINVAL;
357 break;
358 case DEPEVT_TRANSFER_BUS_EXPIRY:
359 /*
360 * SW issues START TRANSFER command to
361 * isochronous ep with future frame interval. If
362 * future interval time has already passed when
363 * core receives the command, it will respond
364 * with an error status of 'Bus Expiry'.
365 *
366 * Instead of always returning -EINVAL, let's
367 * give a hint to the gadget driver that this is
368 * the case by returning -EAGAIN.
369 */
370 ret = -EAGAIN;
371 break;
372 default:
373 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
374 }
375
376 break;
377 }
378 } while (--timeout);
379
380 if (timeout == 0) {
381 ret = -ETIMEDOUT;
382 cmd_status = -ETIMEDOUT;
383 }
384
385 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
386
387 if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
388 dep->flags |= DWC3_EP_TRANSFER_STARTED;
389 dwc3_gadget_ep_get_transfer_index(dep);
390 }
391
392 if (saved_config) {
393 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
394 reg |= saved_config;
395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
396 }
397
398 return ret;
399}
400
401static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
402{
403 struct dwc3 *dwc = dep->dwc;
404 struct dwc3_gadget_ep_cmd_params params;
405 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
406
407 /*
408 * As of core revision 2.60a the recommended programming model
409 * is to set the ClearPendIN bit when issuing a Clear Stall EP
410 * command for IN endpoints. This is to prevent an issue where
411 * some (non-compliant) hosts may not send ACK TPs for pending
412 * IN transfers due to a mishandled error condition. Synopsys
413 * STAR 9000614252.
414 */
415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416 (dwc->gadget.speed >= USB_SPEED_SUPER))
417 cmd |= DWC3_DEPCMD_CLEARPENDIN;
418
419 memset(&params, 0, sizeof(params));
420
421 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
422}
423
424static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
425 struct dwc3_trb *trb)
426{
427 u32 offset = (char *) trb - (char *) dep->trb_pool;
428
429 return dep->trb_pool_dma + offset;
430}
431
432static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
433{
434 struct dwc3 *dwc = dep->dwc;
435
436 if (dep->trb_pool)
437 return 0;
438
439 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
440 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
441 &dep->trb_pool_dma, GFP_KERNEL);
442 if (!dep->trb_pool) {
443 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
444 dep->name);
445 return -ENOMEM;
446 }
447
448 return 0;
449}
450
451static void dwc3_free_trb_pool(struct dwc3_ep *dep)
452{
453 struct dwc3 *dwc = dep->dwc;
454
455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
456 dep->trb_pool, dep->trb_pool_dma);
457
458 dep->trb_pool = NULL;
459 dep->trb_pool_dma = 0;
460}
461
462static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
463{
464 struct dwc3_gadget_ep_cmd_params params;
465
466 memset(&params, 0x00, sizeof(params));
467
468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
469
470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471 &params);
472}
473
474/**
475 * dwc3_gadget_start_config - configure ep resources
476 * @dep: endpoint that is being enabled
477 *
478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
480 *
481 * The assignment of transfer resources cannot perfectly follow the data book
482 * due to the fact that the controller driver does not have all knowledge of the
483 * configuration in advance. It is given this information piecemeal by the
484 * composite gadget framework after every SET_CONFIGURATION and
485 * SET_INTERFACE. Trying to follow the databook programming model in this
486 * scenario can cause errors. For two reasons:
487 *
488 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
489 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
490 * incorrect in the scenario of multiple interfaces.
491 *
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493 * endpoint on alt setting (8.1.6).
494 *
495 * The following simplified method is used instead:
496 *
497 * All hardware endpoints can be assigned a transfer resource and this setting
498 * will stay persistent until either a core reset or hibernation. So whenever we
499 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
500 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
501 * guaranteed that there are as many transfer resources as endpoints.
502 *
503 * This function is called for each endpoint when it is being enabled but is
504 * triggered only when called for EP0-out, which always happens first, and which
505 * should only happen in one of the above conditions.
506 */
507static int dwc3_gadget_start_config(struct dwc3_ep *dep)
508{
509 struct dwc3_gadget_ep_cmd_params params;
510 struct dwc3 *dwc;
511 u32 cmd;
512 int i;
513 int ret;
514
515 if (dep->number)
516 return 0;
517
518 memset(&params, 0x00, sizeof(params));
519 cmd = DWC3_DEPCMD_DEPSTARTCFG;
520 dwc = dep->dwc;
521
522 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
523 if (ret)
524 return ret;
525
526 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527 struct dwc3_ep *dep = dwc->eps[i];
528
529 if (!dep)
530 continue;
531
532 ret = dwc3_gadget_set_xfer_resource(dep);
533 if (ret)
534 return ret;
535 }
536
537 return 0;
538}
539
540static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
541{
542 const struct usb_ss_ep_comp_descriptor *comp_desc;
543 const struct usb_endpoint_descriptor *desc;
544 struct dwc3_gadget_ep_cmd_params params;
545 struct dwc3 *dwc = dep->dwc;
546
547 comp_desc = dep->endpoint.comp_desc;
548 desc = dep->endpoint.desc;
549
550 memset(&params, 0x00, sizeof(params));
551
552 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
554
555 /* Burst size is only needed in SuperSpeed mode */
556 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
557 u32 burst = dep->endpoint.maxburst;
558 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
559 }
560
561 params.param0 |= action;
562 if (action == DWC3_DEPCFG_ACTION_RESTORE)
563 params.param2 |= dep->saved_state;
564
565 if (usb_endpoint_xfer_control(desc))
566 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
567
568 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
570
571 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
572 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
573 | DWC3_DEPCFG_STREAM_EVENT_EN;
574 dep->stream_capable = true;
575 }
576
577 if (!usb_endpoint_xfer_control(desc))
578 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
579
580 /*
581 * We are doing 1:1 mapping for endpoints, meaning
582 * Physical Endpoints 2 maps to Logical Endpoint 2 and
583 * so on. We consider the direction bit as part of the physical
584 * endpoint number. So USB endpoint 0x81 is 0x03.
585 */
586 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
587
588 /*
589 * We must use the lower 16 TX FIFOs even though
590 * HW might have more
591 */
592 if (dep->direction)
593 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
594
595 if (desc->bInterval) {
596 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
597 dep->interval = 1 << (desc->bInterval - 1);
598 }
599
600 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
601}
602
603/**
604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
605 * @dep: endpoint to be initialized
606 * @action: one of INIT, MODIFY or RESTORE
607 *
608 * Caller should take care of locking. Execute all necessary commands to
609 * initialize a HW endpoint so it can be used by a gadget driver.
610 */
611static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
612{
613 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
614 struct dwc3 *dwc = dep->dwc;
615
616 u32 reg;
617 int ret;
618
619 if (!(dep->flags & DWC3_EP_ENABLED)) {
620 ret = dwc3_gadget_start_config(dep);
621 if (ret)
622 return ret;
623 }
624
625 ret = dwc3_gadget_set_ep_config(dep, action);
626 if (ret)
627 return ret;
628
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
630 struct dwc3_trb *trb_st_hw;
631 struct dwc3_trb *trb_link;
632
633 dep->type = usb_endpoint_type(desc);
634 dep->flags |= DWC3_EP_ENABLED;
635 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
636
637 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
638 reg |= DWC3_DALEPENA_EP(dep->number);
639 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
640
641 if (usb_endpoint_xfer_control(desc))
642 goto out;
643
644 /* Initialize the TRB ring */
645 dep->trb_dequeue = 0;
646 dep->trb_enqueue = 0;
647 memset(dep->trb_pool, 0,
648 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
649
650 /* Link TRB. The HWO bit is never reset */
651 trb_st_hw = &dep->trb_pool[0];
652
653 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
654 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
656 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
657 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
658 }
659
660 /*
661 * Issue StartTransfer here with no-op TRB so we can always rely on No
662 * Response Update Transfer command.
663 */
664 if (usb_endpoint_xfer_bulk(desc) ||
665 usb_endpoint_xfer_int(desc)) {
666 struct dwc3_gadget_ep_cmd_params params;
667 struct dwc3_trb *trb;
668 dma_addr_t trb_dma;
669 u32 cmd;
670
671 memset(&params, 0, sizeof(params));
672 trb = &dep->trb_pool[0];
673 trb_dma = dwc3_trb_dma_offset(dep, trb);
674
675 params.param0 = upper_32_bits(trb_dma);
676 params.param1 = lower_32_bits(trb_dma);
677
678 cmd = DWC3_DEPCMD_STARTTRANSFER;
679
680 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
681 if (ret < 0)
682 return ret;
683 }
684
685out:
686 trace_dwc3_gadget_ep_enable(dep);
687
688 return 0;
689}
690
691static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
692static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
693{
694 struct dwc3_request *req;
695
696 dwc3_stop_active_transfer(dep, true);
697
698 /* - giveback all requests to gadget driver */
699 while (!list_empty(&dep->started_list)) {
700 req = next_request(&dep->started_list);
701
702 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
703 }
704
705 while (!list_empty(&dep->pending_list)) {
706 req = next_request(&dep->pending_list);
707
708 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
709 }
710
711 while (!list_empty(&dep->cancelled_list)) {
712 req = next_request(&dep->cancelled_list);
713
714 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
715 }
716}
717
718/**
719 * __dwc3_gadget_ep_disable - disables a hw endpoint
720 * @dep: the endpoint to disable
721 *
722 * This function undoes what __dwc3_gadget_ep_enable did and also removes
723 * requests which are currently being processed by the hardware and those which
724 * are not yet scheduled.
725 *
726 * Caller should take care of locking.
727 */
728static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
729{
730 struct dwc3 *dwc = dep->dwc;
731 u32 reg;
732
733 trace_dwc3_gadget_ep_disable(dep);
734
735 dwc3_remove_requests(dwc, dep);
736
737 /* make sure HW endpoint isn't stalled */
738 if (dep->flags & DWC3_EP_STALL)
739 __dwc3_gadget_ep_set_halt(dep, 0, false);
740
741 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
742 reg &= ~DWC3_DALEPENA_EP(dep->number);
743 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
744
745 dep->stream_capable = false;
746 dep->type = 0;
747 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
748
749 /* Clear out the ep descriptors for non-ep0 */
750 if (dep->number > 1) {
751 dep->endpoint.comp_desc = NULL;
752 dep->endpoint.desc = NULL;
753 }
754
755 return 0;
756}
757
758/* -------------------------------------------------------------------------- */
759
760static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
761 const struct usb_endpoint_descriptor *desc)
762{
763 return -EINVAL;
764}
765
766static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
767{
768 return -EINVAL;
769}
770
771/* -------------------------------------------------------------------------- */
772
773static int dwc3_gadget_ep_enable(struct usb_ep *ep,
774 const struct usb_endpoint_descriptor *desc)
775{
776 struct dwc3_ep *dep;
777 struct dwc3 *dwc;
778 unsigned long flags;
779 int ret;
780
781 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
782 pr_debug("dwc3: invalid parameters\n");
783 return -EINVAL;
784 }
785
786 if (!desc->wMaxPacketSize) {
787 pr_debug("dwc3: missing wMaxPacketSize\n");
788 return -EINVAL;
789 }
790
791 dep = to_dwc3_ep(ep);
792 dwc = dep->dwc;
793
794 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
795 "%s is already enabled\n",
796 dep->name))
797 return 0;
798
799 spin_lock_irqsave(&dwc->lock, flags);
800 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
801 spin_unlock_irqrestore(&dwc->lock, flags);
802
803 return ret;
804}
805
806static int dwc3_gadget_ep_disable(struct usb_ep *ep)
807{
808 struct dwc3_ep *dep;
809 struct dwc3 *dwc;
810 unsigned long flags;
811 int ret;
812
813 if (!ep) {
814 pr_debug("dwc3: invalid parameters\n");
815 return -EINVAL;
816 }
817
818 dep = to_dwc3_ep(ep);
819 dwc = dep->dwc;
820
821 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
822 "%s is already disabled\n",
823 dep->name))
824 return 0;
825
826 spin_lock_irqsave(&dwc->lock, flags);
827 ret = __dwc3_gadget_ep_disable(dep);
828 spin_unlock_irqrestore(&dwc->lock, flags);
829
830 return ret;
831}
832
833static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
834 gfp_t gfp_flags)
835{
836 struct dwc3_request *req;
837 struct dwc3_ep *dep = to_dwc3_ep(ep);
838
839 req = kzalloc(sizeof(*req), gfp_flags);
840 if (!req)
841 return NULL;
842
843 req->direction = dep->direction;
844 req->epnum = dep->number;
845 req->dep = dep;
846
847 trace_dwc3_alloc_request(req);
848
849 return &req->request;
850}
851
852static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
853 struct usb_request *request)
854{
855 struct dwc3_request *req = to_dwc3_request(request);
856
857 trace_dwc3_free_request(req);
858 kfree(req);
859}
860
861/**
862 * dwc3_ep_prev_trb - returns the previous TRB in the ring
863 * @dep: The endpoint with the TRB ring
864 * @index: The index of the current TRB in the ring
865 *
866 * Returns the TRB prior to the one pointed to by the index. If the
867 * index is 0, we will wrap backwards, skip the link TRB, and return
868 * the one just before that.
869 */
870static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
871{
872 u8 tmp = index;
873
874 if (!tmp)
875 tmp = DWC3_TRB_NUM - 1;
876
877 return &dep->trb_pool[tmp - 1];
878}
879
880static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
881{
882 struct dwc3_trb *tmp;
883 u8 trbs_left;
884
885 /*
886 * If enqueue & dequeue are equal than it is either full or empty.
887 *
888 * One way to know for sure is if the TRB right before us has HWO bit
889 * set or not. If it has, then we're definitely full and can't fit any
890 * more transfers in our ring.
891 */
892 if (dep->trb_enqueue == dep->trb_dequeue) {
893 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
894 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
895 return 0;
896
897 return DWC3_TRB_NUM - 1;
898 }
899
900 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
901 trbs_left &= (DWC3_TRB_NUM - 1);
902
903 if (dep->trb_dequeue < dep->trb_enqueue)
904 trbs_left--;
905
906 return trbs_left;
907}
908
909static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
910 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
911 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
912{
913 struct dwc3 *dwc = dep->dwc;
914 struct usb_gadget *gadget = &dwc->gadget;
915 enum usb_device_speed speed = gadget->speed;
916
917 trb->size = DWC3_TRB_SIZE_LENGTH(length);
918 trb->bpl = lower_32_bits(dma);
919 trb->bph = upper_32_bits(dma);
920
921 switch (usb_endpoint_type(dep->endpoint.desc)) {
922 case USB_ENDPOINT_XFER_CONTROL:
923 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
924 break;
925
926 case USB_ENDPOINT_XFER_ISOC:
927 if (!node) {
928 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
929
930 /*
931 * USB Specification 2.0 Section 5.9.2 states that: "If
932 * there is only a single transaction in the microframe,
933 * only a DATA0 data packet PID is used. If there are
934 * two transactions per microframe, DATA1 is used for
935 * the first transaction data packet and DATA0 is used
936 * for the second transaction data packet. If there are
937 * three transactions per microframe, DATA2 is used for
938 * the first transaction data packet, DATA1 is used for
939 * the second, and DATA0 is used for the third."
940 *
941 * IOW, we should satisfy the following cases:
942 *
943 * 1) length <= maxpacket
944 * - DATA0
945 *
946 * 2) maxpacket < length <= (2 * maxpacket)
947 * - DATA1, DATA0
948 *
949 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
950 * - DATA2, DATA1, DATA0
951 */
952 if (speed == USB_SPEED_HIGH) {
953 struct usb_ep *ep = &dep->endpoint;
954 unsigned int mult = 2;
955 unsigned int maxp = usb_endpoint_maxp(ep->desc);
956
957 if (length <= (2 * maxp))
958 mult--;
959
960 if (length <= maxp)
961 mult--;
962
963 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
964 }
965 } else {
966 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
967 }
968
969 /* always enable Interrupt on Missed ISOC */
970 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
971 break;
972
973 case USB_ENDPOINT_XFER_BULK:
974 case USB_ENDPOINT_XFER_INT:
975 trb->ctrl = DWC3_TRBCTL_NORMAL;
976 break;
977 default:
978 /*
979 * This is only possible with faulty memory because we
980 * checked it already :)
981 */
982 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
983 usb_endpoint_type(dep->endpoint.desc));
984 }
985
986 /*
987 * Enable Continue on Short Packet
988 * when endpoint is not a stream capable
989 */
990 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
991 if (!dep->stream_capable)
992 trb->ctrl |= DWC3_TRB_CTRL_CSP;
993
994 if (short_not_ok)
995 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
996 }
997
998 if ((!no_interrupt && !chain) ||
999 (dwc3_calc_trbs_left(dep) == 1))
1000 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1001
1002 if (chain)
1003 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1004
1005 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1006 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1007
1008 trb->ctrl |= DWC3_TRB_CTRL_HWO;
1009
1010 dwc3_ep_inc_enq(dep);
1011
1012 trace_dwc3_prepare_trb(dep, trb);
1013}
1014
1015/**
1016 * dwc3_prepare_one_trb - setup one TRB from one request
1017 * @dep: endpoint for which this request is prepared
1018 * @req: dwc3_request pointer
1019 * @chain: should this TRB be chained to the next?
1020 * @node: only for isochronous endpoints. First TRB needs different type.
1021 */
1022static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1023 struct dwc3_request *req, unsigned chain, unsigned node)
1024{
1025 struct dwc3_trb *trb;
1026 unsigned int length;
1027 dma_addr_t dma;
1028 unsigned stream_id = req->request.stream_id;
1029 unsigned short_not_ok = req->request.short_not_ok;
1030 unsigned no_interrupt = req->request.no_interrupt;
1031
1032 if (req->request.num_sgs > 0) {
1033 length = sg_dma_len(req->start_sg);
1034 dma = sg_dma_address(req->start_sg);
1035 } else {
1036 length = req->request.length;
1037 dma = req->request.dma;
1038 }
1039
1040 trb = &dep->trb_pool[dep->trb_enqueue];
1041
1042 if (!req->trb) {
1043 dwc3_gadget_move_started_request(req);
1044 req->trb = trb;
1045 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1046 }
1047
1048 req->num_trbs++;
1049
1050 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1051 stream_id, short_not_ok, no_interrupt);
1052}
1053
1054static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1055 struct dwc3_request *req)
1056{
1057 struct scatterlist *sg = req->start_sg;
1058 struct scatterlist *s;
1059 int i;
1060
1061 unsigned int remaining = req->request.num_mapped_sgs
1062 - req->num_queued_sgs;
1063
1064 for_each_sg(sg, s, remaining, i) {
1065 unsigned int length = req->request.length;
1066 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1067 unsigned int rem = length % maxp;
1068 unsigned chain = true;
1069
1070 if (sg_is_last(s))
1071 chain = false;
1072
1073 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1074 struct dwc3 *dwc = dep->dwc;
1075 struct dwc3_trb *trb;
1076
1077 req->needs_extra_trb = true;
1078
1079 /* prepare normal TRB */
1080 dwc3_prepare_one_trb(dep, req, true, i);
1081
1082 /* Now prepare one extra TRB to align transfer size */
1083 trb = &dep->trb_pool[dep->trb_enqueue];
1084 req->num_trbs++;
1085 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1086 maxp - rem, false, 1,
1087 req->request.stream_id,
1088 req->request.short_not_ok,
1089 req->request.no_interrupt);
1090 } else {
1091 dwc3_prepare_one_trb(dep, req, chain, i);
1092 }
1093
1094 /*
1095 * There can be a situation where all sgs in sglist are not
1096 * queued because of insufficient trb number. To handle this
1097 * case, update start_sg to next sg to be queued, so that
1098 * we have free trbs we can continue queuing from where we
1099 * previously stopped
1100 */
1101 if (chain)
1102 req->start_sg = sg_next(s);
1103
1104 req->num_queued_sgs++;
1105
1106 if (!dwc3_calc_trbs_left(dep))
1107 break;
1108 }
1109}
1110
1111static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1112 struct dwc3_request *req)
1113{
1114 unsigned int length = req->request.length;
1115 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1116 unsigned int rem = length % maxp;
1117
1118 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
1119 struct dwc3 *dwc = dep->dwc;
1120 struct dwc3_trb *trb;
1121
1122 req->needs_extra_trb = true;
1123
1124 /* prepare normal TRB */
1125 dwc3_prepare_one_trb(dep, req, true, 0);
1126
1127 /* Now prepare one extra TRB to align transfer size */
1128 trb = &dep->trb_pool[dep->trb_enqueue];
1129 req->num_trbs++;
1130 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1131 false, 1, req->request.stream_id,
1132 req->request.short_not_ok,
1133 req->request.no_interrupt);
1134 } else if (req->request.zero && req->request.length &&
1135 (IS_ALIGNED(req->request.length, maxp))) {
1136 struct dwc3 *dwc = dep->dwc;
1137 struct dwc3_trb *trb;
1138
1139 req->needs_extra_trb = true;
1140
1141 /* prepare normal TRB */
1142 dwc3_prepare_one_trb(dep, req, true, 0);
1143
1144 /* Now prepare one extra TRB to handle ZLP */
1145 trb = &dep->trb_pool[dep->trb_enqueue];
1146 req->num_trbs++;
1147 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1148 false, 1, req->request.stream_id,
1149 req->request.short_not_ok,
1150 req->request.no_interrupt);
1151 } else {
1152 dwc3_prepare_one_trb(dep, req, false, 0);
1153 }
1154}
1155
1156/*
1157 * dwc3_prepare_trbs - setup TRBs from requests
1158 * @dep: endpoint for which requests are being prepared
1159 *
1160 * The function goes through the requests list and sets up TRBs for the
1161 * transfers. The function returns once there are no more TRBs available or
1162 * it runs out of requests.
1163 */
1164static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1165{
1166 struct dwc3_request *req, *n;
1167
1168 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1169
1170 /*
1171 * We can get in a situation where there's a request in the started list
1172 * but there weren't enough TRBs to fully kick it in the first time
1173 * around, so it has been waiting for more TRBs to be freed up.
1174 *
1175 * In that case, we should check if we have a request with pending_sgs
1176 * in the started list and prepare TRBs for that request first,
1177 * otherwise we will prepare TRBs completely out of order and that will
1178 * break things.
1179 */
1180 list_for_each_entry(req, &dep->started_list, list) {
1181 if (req->num_pending_sgs > 0)
1182 dwc3_prepare_one_trb_sg(dep, req);
1183
1184 if (!dwc3_calc_trbs_left(dep))
1185 return;
1186 }
1187
1188 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1189 struct dwc3 *dwc = dep->dwc;
1190 int ret;
1191
1192 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1193 dep->direction);
1194 if (ret)
1195 return;
1196
1197 req->sg = req->request.sg;
1198 req->start_sg = req->sg;
1199 req->num_queued_sgs = 0;
1200 req->num_pending_sgs = req->request.num_mapped_sgs;
1201
1202 if (req->num_pending_sgs > 0)
1203 dwc3_prepare_one_trb_sg(dep, req);
1204 else
1205 dwc3_prepare_one_trb_linear(dep, req);
1206
1207 if (!dwc3_calc_trbs_left(dep))
1208 return;
1209 }
1210}
1211
1212static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1213{
1214 struct dwc3_gadget_ep_cmd_params params;
1215 struct dwc3_request *req;
1216 int starting;
1217 int ret;
1218 u32 cmd;
1219
1220 if (!dwc3_calc_trbs_left(dep))
1221 return 0;
1222
1223 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1224
1225 dwc3_prepare_trbs(dep);
1226 req = next_request(&dep->started_list);
1227 if (!req) {
1228 dep->flags |= DWC3_EP_PENDING_REQUEST;
1229 return 0;
1230 }
1231
1232 memset(&params, 0, sizeof(params));
1233
1234 if (starting) {
1235 params.param0 = upper_32_bits(req->trb_dma);
1236 params.param1 = lower_32_bits(req->trb_dma);
1237 cmd = DWC3_DEPCMD_STARTTRANSFER;
1238
1239 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1240 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1241 } else {
1242 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1243 DWC3_DEPCMD_PARAM(dep->resource_index);
1244 }
1245
1246 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1247 if (ret < 0) {
1248 /*
1249 * FIXME we need to iterate over the list of requests
1250 * here and stop, unmap, free and del each of the linked
1251 * requests instead of what we do now.
1252 */
1253 if (req->trb)
1254 memset(req->trb, 0, sizeof(struct dwc3_trb));
1255 dwc3_gadget_del_and_unmap_request(dep, req, ret);
1256 return ret;
1257 }
1258
1259 return 0;
1260}
1261
1262static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1263{
1264 u32 reg;
1265
1266 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1267 return DWC3_DSTS_SOFFN(reg);
1268}
1269
1270static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1271{
1272 if (list_empty(&dep->pending_list)) {
1273 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
1274 dep->name);
1275 dep->flags |= DWC3_EP_PENDING_REQUEST;
1276 return;
1277 }
1278
1279 dep->frame_number = DWC3_ALIGN_FRAME(dep);
1280 __dwc3_gadget_kick_transfer(dep);
1281}
1282
1283static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1284{
1285 struct dwc3 *dwc = dep->dwc;
1286
1287 if (!dep->endpoint.desc) {
1288 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1289 dep->name);
1290 return -ESHUTDOWN;
1291 }
1292
1293 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1294 &req->request, req->dep->name))
1295 return -EINVAL;
1296
1297 pm_runtime_get(dwc->dev);
1298
1299 req->request.actual = 0;
1300 req->request.status = -EINPROGRESS;
1301
1302 trace_dwc3_ep_queue(req);
1303
1304 list_add_tail(&req->list, &dep->pending_list);
1305
1306 /*
1307 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1308 * wait for a XferNotReady event so we will know what's the current
1309 * (micro-)frame number.
1310 *
1311 * Without this trick, we are very, very likely gonna get Bus Expiry
1312 * errors which will force us issue EndTransfer command.
1313 */
1314 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1315 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1316 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1317 return 0;
1318
1319 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1320 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1321 __dwc3_gadget_start_isoc(dep);
1322 return 0;
1323 }
1324 }
1325 }
1326
1327 return __dwc3_gadget_kick_transfer(dep);
1328}
1329
1330static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1331 gfp_t gfp_flags)
1332{
1333 struct dwc3_request *req = to_dwc3_request(request);
1334 struct dwc3_ep *dep = to_dwc3_ep(ep);
1335 struct dwc3 *dwc = dep->dwc;
1336
1337 unsigned long flags;
1338
1339 int ret;
1340
1341 spin_lock_irqsave(&dwc->lock, flags);
1342 ret = __dwc3_gadget_ep_queue(dep, req);
1343 spin_unlock_irqrestore(&dwc->lock, flags);
1344
1345 return ret;
1346}
1347
1348static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1349{
1350 int i;
1351
1352 /*
1353 * If request was already started, this means we had to
1354 * stop the transfer. With that we also need to ignore
1355 * all TRBs used by the request, however TRBs can only
1356 * be modified after completion of END_TRANSFER
1357 * command. So what we do here is that we wait for
1358 * END_TRANSFER completion and only after that, we jump
1359 * over TRBs by clearing HWO and incrementing dequeue
1360 * pointer.
1361 */
1362 for (i = 0; i < req->num_trbs; i++) {
1363 struct dwc3_trb *trb;
1364
1365 trb = req->trb + i;
1366 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1367 dwc3_ep_inc_deq(dep);
1368 }
1369
1370 req->num_trbs = 0;
1371}
1372
1373static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1374{
1375 struct dwc3_request *req;
1376 struct dwc3_request *tmp;
1377
1378 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1379 dwc3_gadget_ep_skip_trbs(dep, req);
1380 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1381 }
1382}
1383
1384static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1385 struct usb_request *request)
1386{
1387 struct dwc3_request *req = to_dwc3_request(request);
1388 struct dwc3_request *r = NULL;
1389
1390 struct dwc3_ep *dep = to_dwc3_ep(ep);
1391 struct dwc3 *dwc = dep->dwc;
1392
1393 unsigned long flags;
1394 int ret = 0;
1395
1396 trace_dwc3_ep_dequeue(req);
1397
1398 spin_lock_irqsave(&dwc->lock, flags);
1399
1400 list_for_each_entry(r, &dep->pending_list, list) {
1401 if (r == req)
1402 break;
1403 }
1404
1405 if (r != req) {
1406 list_for_each_entry(r, &dep->started_list, list) {
1407 if (r == req)
1408 break;
1409 }
1410 if (r == req) {
1411 /* wait until it is processed */
1412 dwc3_stop_active_transfer(dep, true);
1413
1414 if (!r->trb)
1415 goto out0;
1416
1417 dwc3_gadget_move_cancelled_request(req);
1418 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1419 goto out0;
1420 else
1421 goto out1;
1422 }
1423 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1424 request, ep->name);
1425 ret = -EINVAL;
1426 goto out0;
1427 }
1428
1429out1:
1430 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1431
1432out0:
1433 spin_unlock_irqrestore(&dwc->lock, flags);
1434
1435 return ret;
1436}
1437
1438int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1439{
1440 struct dwc3_gadget_ep_cmd_params params;
1441 struct dwc3 *dwc = dep->dwc;
1442 int ret;
1443
1444 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1445 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1446 return -EINVAL;
1447 }
1448
1449 memset(&params, 0x00, sizeof(params));
1450
1451 if (value) {
1452 struct dwc3_trb *trb;
1453
1454 unsigned transfer_in_flight;
1455 unsigned started;
1456
1457 if (dep->number > 1)
1458 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1459 else
1460 trb = &dwc->ep0_trb[dep->trb_enqueue];
1461
1462 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1463 started = !list_empty(&dep->started_list);
1464
1465 if (!protocol && ((dep->direction && transfer_in_flight) ||
1466 (!dep->direction && started))) {
1467 return -EAGAIN;
1468 }
1469
1470 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1471 &params);
1472 if (ret)
1473 dev_err(dwc->dev, "failed to set STALL on %s\n",
1474 dep->name);
1475 else
1476 dep->flags |= DWC3_EP_STALL;
1477 } else {
1478
1479 ret = dwc3_send_clear_stall_ep_cmd(dep);
1480 if (ret)
1481 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1482 dep->name);
1483 else
1484 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1485 }
1486
1487 return ret;
1488}
1489
1490static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1491{
1492 struct dwc3_ep *dep = to_dwc3_ep(ep);
1493 struct dwc3 *dwc = dep->dwc;
1494
1495 unsigned long flags;
1496
1497 int ret;
1498
1499 spin_lock_irqsave(&dwc->lock, flags);
1500 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1501 spin_unlock_irqrestore(&dwc->lock, flags);
1502
1503 return ret;
1504}
1505
1506static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1507{
1508 struct dwc3_ep *dep = to_dwc3_ep(ep);
1509 struct dwc3 *dwc = dep->dwc;
1510 unsigned long flags;
1511 int ret;
1512
1513 spin_lock_irqsave(&dwc->lock, flags);
1514 dep->flags |= DWC3_EP_WEDGE;
1515
1516 if (dep->number == 0 || dep->number == 1)
1517 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1518 else
1519 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1520 spin_unlock_irqrestore(&dwc->lock, flags);
1521
1522 return ret;
1523}
1524
1525/* -------------------------------------------------------------------------- */
1526
1527static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1528 .bLength = USB_DT_ENDPOINT_SIZE,
1529 .bDescriptorType = USB_DT_ENDPOINT,
1530 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1531};
1532
1533static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1534 .enable = dwc3_gadget_ep0_enable,
1535 .disable = dwc3_gadget_ep0_disable,
1536 .alloc_request = dwc3_gadget_ep_alloc_request,
1537 .free_request = dwc3_gadget_ep_free_request,
1538 .queue = dwc3_gadget_ep0_queue,
1539 .dequeue = dwc3_gadget_ep_dequeue,
1540 .set_halt = dwc3_gadget_ep0_set_halt,
1541 .set_wedge = dwc3_gadget_ep_set_wedge,
1542};
1543
1544static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1545 .enable = dwc3_gadget_ep_enable,
1546 .disable = dwc3_gadget_ep_disable,
1547 .alloc_request = dwc3_gadget_ep_alloc_request,
1548 .free_request = dwc3_gadget_ep_free_request,
1549 .queue = dwc3_gadget_ep_queue,
1550 .dequeue = dwc3_gadget_ep_dequeue,
1551 .set_halt = dwc3_gadget_ep_set_halt,
1552 .set_wedge = dwc3_gadget_ep_set_wedge,
1553};
1554
1555/* -------------------------------------------------------------------------- */
1556
1557static int dwc3_gadget_get_frame(struct usb_gadget *g)
1558{
1559 struct dwc3 *dwc = gadget_to_dwc(g);
1560
1561 return __dwc3_gadget_get_frame(dwc);
1562}
1563
1564static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1565{
1566 int retries;
1567
1568 int ret;
1569 u32 reg;
1570
1571 u8 link_state;
1572 u8 speed;
1573
1574 /*
1575 * According to the Databook Remote wakeup request should
1576 * be issued only when the device is in early suspend state.
1577 *
1578 * We can check that via USB Link State bits in DSTS register.
1579 */
1580 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1581
1582 speed = reg & DWC3_DSTS_CONNECTSPD;
1583 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1584 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
1585 return 0;
1586
1587 link_state = DWC3_DSTS_USBLNKST(reg);
1588
1589 switch (link_state) {
1590 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1591 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1592 break;
1593 default:
1594 return -EINVAL;
1595 }
1596
1597 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1598 if (ret < 0) {
1599 dev_err(dwc->dev, "failed to put link in Recovery\n");
1600 return ret;
1601 }
1602
1603 /* Recent versions do this automatically */
1604 if (dwc->revision < DWC3_REVISION_194A) {
1605 /* write zeroes to Link Change Request */
1606 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1607 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1608 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1609 }
1610
1611 /* poll until Link State changes to ON */
1612 retries = 20000;
1613
1614 while (retries--) {
1615 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1616
1617 /* in HS, means ON */
1618 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1619 break;
1620 }
1621
1622 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1623 dev_err(dwc->dev, "failed to send remote wakeup\n");
1624 return -EINVAL;
1625 }
1626
1627 return 0;
1628}
1629
1630static int dwc3_gadget_wakeup(struct usb_gadget *g)
1631{
1632 struct dwc3 *dwc = gadget_to_dwc(g);
1633 unsigned long flags;
1634 int ret;
1635
1636 spin_lock_irqsave(&dwc->lock, flags);
1637 ret = __dwc3_gadget_wakeup(dwc);
1638 spin_unlock_irqrestore(&dwc->lock, flags);
1639
1640 return ret;
1641}
1642
1643static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1644 int is_selfpowered)
1645{
1646 struct dwc3 *dwc = gadget_to_dwc(g);
1647 unsigned long flags;
1648
1649 spin_lock_irqsave(&dwc->lock, flags);
1650 g->is_selfpowered = !!is_selfpowered;
1651 spin_unlock_irqrestore(&dwc->lock, flags);
1652
1653 return 0;
1654}
1655
1656static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1657{
1658 u32 reg;
1659 u32 timeout = 500;
1660
1661 if (pm_runtime_suspended(dwc->dev))
1662 return 0;
1663
1664 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1665 if (is_on) {
1666 if (dwc->revision <= DWC3_REVISION_187A) {
1667 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1668 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1669 }
1670
1671 if (dwc->revision >= DWC3_REVISION_194A)
1672 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1673 reg |= DWC3_DCTL_RUN_STOP;
1674
1675 if (dwc->has_hibernation)
1676 reg |= DWC3_DCTL_KEEP_CONNECT;
1677
1678 dwc->pullups_connected = true;
1679 } else {
1680 reg &= ~DWC3_DCTL_RUN_STOP;
1681
1682 if (dwc->has_hibernation && !suspend)
1683 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1684
1685 dwc->pullups_connected = false;
1686 }
1687
1688 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1689
1690 do {
1691 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1692 reg &= DWC3_DSTS_DEVCTRLHLT;
1693 } while (--timeout && !(!is_on ^ !reg));
1694
1695 if (!timeout)
1696 return -ETIMEDOUT;
1697
1698 return 0;
1699}
1700
1701static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1702{
1703 struct dwc3 *dwc = gadget_to_dwc(g);
1704 unsigned long flags;
1705 int ret;
1706
1707 is_on = !!is_on;
1708
1709 /*
1710 * Per databook, when we want to stop the gadget, if a control transfer
1711 * is still in process, complete it and get the core into setup phase.
1712 */
1713 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1714 reinit_completion(&dwc->ep0_in_setup);
1715
1716 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1717 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1718 if (ret == 0) {
1719 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1720 return -ETIMEDOUT;
1721 }
1722 }
1723
1724 spin_lock_irqsave(&dwc->lock, flags);
1725 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1726 spin_unlock_irqrestore(&dwc->lock, flags);
1727
1728 return ret;
1729}
1730
1731static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1732{
1733 u32 reg;
1734
1735 /* Enable all but Start and End of Frame IRQs */
1736 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1737 DWC3_DEVTEN_EVNTOVERFLOWEN |
1738 DWC3_DEVTEN_CMDCMPLTEN |
1739 DWC3_DEVTEN_ERRTICERREN |
1740 DWC3_DEVTEN_WKUPEVTEN |
1741 DWC3_DEVTEN_CONNECTDONEEN |
1742 DWC3_DEVTEN_USBRSTEN |
1743 DWC3_DEVTEN_DISCONNEVTEN);
1744
1745 if (dwc->revision < DWC3_REVISION_250A)
1746 reg |= DWC3_DEVTEN_ULSTCNGEN;
1747
1748 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1749}
1750
1751static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1752{
1753 /* mask all interrupts */
1754 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1755}
1756
1757static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1758static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1759
1760/**
1761 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1762 * @dwc: pointer to our context structure
1763 *
1764 * The following looks like complex but it's actually very simple. In order to
1765 * calculate the number of packets we can burst at once on OUT transfers, we're
1766 * gonna use RxFIFO size.
1767 *
1768 * To calculate RxFIFO size we need two numbers:
1769 * MDWIDTH = size, in bits, of the internal memory bus
1770 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1771 *
1772 * Given these two numbers, the formula is simple:
1773 *
1774 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1775 *
1776 * 24 bytes is for 3x SETUP packets
1777 * 16 bytes is a clock domain crossing tolerance
1778 *
1779 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1780 */
1781static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1782{
1783 u32 ram2_depth;
1784 u32 mdwidth;
1785 u32 nump;
1786 u32 reg;
1787
1788 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1789 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1790
1791 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1792 nump = min_t(u32, nump, 16);
1793
1794 /* update NumP */
1795 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1796 reg &= ~DWC3_DCFG_NUMP_MASK;
1797 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1798 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1799}
1800
1801static int __dwc3_gadget_start(struct dwc3 *dwc)
1802{
1803 struct dwc3_ep *dep;
1804 int ret = 0;
1805 u32 reg;
1806
1807 /*
1808 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1809 * the core supports IMOD, disable it.
1810 */
1811 if (dwc->imod_interval) {
1812 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1813 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1814 } else if (dwc3_has_imod(dwc)) {
1815 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1816 }
1817
1818 /*
1819 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1820 * field instead of letting dwc3 itself calculate that automatically.
1821 *
1822 * This way, we maximize the chances that we'll be able to get several
1823 * bursts of data without going through any sort of endpoint throttling.
1824 */
1825 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1826 if (dwc3_is_usb31(dwc))
1827 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1828 else
1829 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1830
1831 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1832
1833 dwc3_gadget_setup_nump(dwc);
1834
1835 /* Start with SuperSpeed Default */
1836 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1837
1838 dep = dwc->eps[0];
1839 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1840 if (ret) {
1841 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1842 goto err0;
1843 }
1844
1845 dep = dwc->eps[1];
1846 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1847 if (ret) {
1848 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1849 goto err1;
1850 }
1851
1852 /* begin to receive SETUP packets */
1853 dwc->ep0state = EP0_SETUP_PHASE;
1854 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
1855 dwc3_ep0_out_start(dwc);
1856
1857 dwc3_gadget_enable_irq(dwc);
1858
1859 return 0;
1860
1861err1:
1862 __dwc3_gadget_ep_disable(dwc->eps[0]);
1863
1864err0:
1865 return ret;
1866}
1867
1868static int dwc3_gadget_start(struct usb_gadget *g,
1869 struct usb_gadget_driver *driver)
1870{
1871 struct dwc3 *dwc = gadget_to_dwc(g);
1872 unsigned long flags;
1873 int ret = 0;
1874 int irq;
1875
1876 irq = dwc->irq_gadget;
1877 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1878 IRQF_SHARED, "dwc3", dwc->ev_buf);
1879 if (ret) {
1880 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1881 irq, ret);
1882 goto err0;
1883 }
1884
1885 spin_lock_irqsave(&dwc->lock, flags);
1886 if (dwc->gadget_driver) {
1887 dev_err(dwc->dev, "%s is already bound to %s\n",
1888 dwc->gadget.name,
1889 dwc->gadget_driver->driver.name);
1890 ret = -EBUSY;
1891 goto err1;
1892 }
1893
1894 dwc->gadget_driver = driver;
1895
1896 if (pm_runtime_active(dwc->dev))
1897 __dwc3_gadget_start(dwc);
1898
1899 spin_unlock_irqrestore(&dwc->lock, flags);
1900
1901 return 0;
1902
1903err1:
1904 spin_unlock_irqrestore(&dwc->lock, flags);
1905 free_irq(irq, dwc);
1906
1907err0:
1908 return ret;
1909}
1910
1911static void __dwc3_gadget_stop(struct dwc3 *dwc)
1912{
1913 dwc3_gadget_disable_irq(dwc);
1914 __dwc3_gadget_ep_disable(dwc->eps[0]);
1915 __dwc3_gadget_ep_disable(dwc->eps[1]);
1916}
1917
1918static int dwc3_gadget_stop(struct usb_gadget *g)
1919{
1920 struct dwc3 *dwc = gadget_to_dwc(g);
1921 unsigned long flags;
1922
1923 spin_lock_irqsave(&dwc->lock, flags);
1924
1925 if (pm_runtime_suspended(dwc->dev))
1926 goto out;
1927
1928 __dwc3_gadget_stop(dwc);
1929
1930out:
1931 dwc->gadget_driver = NULL;
1932 spin_unlock_irqrestore(&dwc->lock, flags);
1933
1934 free_irq(dwc->irq_gadget, dwc->ev_buf);
1935
1936 return 0;
1937}
1938
1939static void dwc3_gadget_set_speed(struct usb_gadget *g,
1940 enum usb_device_speed speed)
1941{
1942 struct dwc3 *dwc = gadget_to_dwc(g);
1943 unsigned long flags;
1944 u32 reg;
1945
1946 spin_lock_irqsave(&dwc->lock, flags);
1947 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1948 reg &= ~(DWC3_DCFG_SPEED_MASK);
1949
1950 /*
1951 * WORKAROUND: DWC3 revision < 2.20a have an issue
1952 * which would cause metastability state on Run/Stop
1953 * bit if we try to force the IP to USB2-only mode.
1954 *
1955 * Because of that, we cannot configure the IP to any
1956 * speed other than the SuperSpeed
1957 *
1958 * Refers to:
1959 *
1960 * STAR#9000525659: Clock Domain Crossing on DCTL in
1961 * USB 2.0 Mode
1962 */
1963 if (dwc->revision < DWC3_REVISION_220A &&
1964 !dwc->dis_metastability_quirk) {
1965 reg |= DWC3_DCFG_SUPERSPEED;
1966 } else {
1967 switch (speed) {
1968 case USB_SPEED_LOW:
1969 reg |= DWC3_DCFG_LOWSPEED;
1970 break;
1971 case USB_SPEED_FULL:
1972 reg |= DWC3_DCFG_FULLSPEED;
1973 break;
1974 case USB_SPEED_HIGH:
1975 reg |= DWC3_DCFG_HIGHSPEED;
1976 break;
1977 case USB_SPEED_SUPER:
1978 reg |= DWC3_DCFG_SUPERSPEED;
1979 break;
1980 case USB_SPEED_SUPER_PLUS:
1981 if (dwc3_is_usb31(dwc))
1982 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
1983 else
1984 reg |= DWC3_DCFG_SUPERSPEED;
1985 break;
1986 default:
1987 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
1988
1989 if (dwc->revision & DWC3_REVISION_IS_DWC31)
1990 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
1991 else
1992 reg |= DWC3_DCFG_SUPERSPEED;
1993 }
1994 }
1995 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1996
1997 spin_unlock_irqrestore(&dwc->lock, flags);
1998}
1999
2000static const struct usb_gadget_ops dwc3_gadget_ops = {
2001 .get_frame = dwc3_gadget_get_frame,
2002 .wakeup = dwc3_gadget_wakeup,
2003 .set_selfpowered = dwc3_gadget_set_selfpowered,
2004 .pullup = dwc3_gadget_pullup,
2005 .udc_start = dwc3_gadget_start,
2006 .udc_stop = dwc3_gadget_stop,
2007 .udc_set_speed = dwc3_gadget_set_speed,
2008};
2009
2010/* -------------------------------------------------------------------------- */
2011
2012static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2013{
2014 struct dwc3 *dwc = dep->dwc;
2015
2016 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2017 dep->endpoint.maxburst = 1;
2018 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2019 if (!dep->direction)
2020 dwc->gadget.ep0 = &dep->endpoint;
2021
2022 dep->endpoint.caps.type_control = true;
2023
2024 return 0;
2025}
2026
2027static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2028{
2029 struct dwc3 *dwc = dep->dwc;
2030 int mdwidth;
2031 int kbytes;
2032 int size;
2033
2034 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2035 /* MDWIDTH is represented in bits, we need it in bytes */
2036 mdwidth /= 8;
2037
2038 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2039 if (dwc3_is_usb31(dwc))
2040 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2041 else
2042 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2043
2044 /* FIFO Depth is in MDWDITH bytes. Multiply */
2045 size *= mdwidth;
2046
2047 kbytes = size / 1024;
2048 if (kbytes == 0)
2049 kbytes = 1;
2050
2051 /*
2052 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2053 * internal overhead. We don't really know how these are used,
2054 * but documentation say it exists.
2055 */
2056 size -= mdwidth * (kbytes + 1);
2057 size /= kbytes;
2058
2059 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2060
2061 dep->endpoint.max_streams = 15;
2062 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2063 list_add_tail(&dep->endpoint.ep_list,
2064 &dwc->gadget.ep_list);
2065 dep->endpoint.caps.type_iso = true;
2066 dep->endpoint.caps.type_bulk = true;
2067 dep->endpoint.caps.type_int = true;
2068
2069 return dwc3_alloc_trb_pool(dep);
2070}
2071
2072static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2073{
2074 struct dwc3 *dwc = dep->dwc;
2075
2076 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2077 dep->endpoint.max_streams = 15;
2078 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2079 list_add_tail(&dep->endpoint.ep_list,
2080 &dwc->gadget.ep_list);
2081 dep->endpoint.caps.type_iso = true;
2082 dep->endpoint.caps.type_bulk = true;
2083 dep->endpoint.caps.type_int = true;
2084
2085 return dwc3_alloc_trb_pool(dep);
2086}
2087
2088static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2089{
2090 struct dwc3_ep *dep;
2091 bool direction = epnum & 1;
2092 int ret;
2093 u8 num = epnum >> 1;
2094
2095 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2096 if (!dep)
2097 return -ENOMEM;
2098
2099 dep->dwc = dwc;
2100 dep->number = epnum;
2101 dep->direction = direction;
2102 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2103 dwc->eps[epnum] = dep;
2104
2105 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2106 direction ? "in" : "out");
2107
2108 dep->endpoint.name = dep->name;
2109
2110 if (!(dep->number > 1)) {
2111 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2112 dep->endpoint.comp_desc = NULL;
2113 }
2114
2115 spin_lock_init(&dep->lock);
2116
2117 if (num == 0)
2118 ret = dwc3_gadget_init_control_endpoint(dep);
2119 else if (direction)
2120 ret = dwc3_gadget_init_in_endpoint(dep);
2121 else
2122 ret = dwc3_gadget_init_out_endpoint(dep);
2123
2124 if (ret)
2125 return ret;
2126
2127 dep->endpoint.caps.dir_in = direction;
2128 dep->endpoint.caps.dir_out = !direction;
2129
2130 INIT_LIST_HEAD(&dep->pending_list);
2131 INIT_LIST_HEAD(&dep->started_list);
2132 INIT_LIST_HEAD(&dep->cancelled_list);
2133
2134 return 0;
2135}
2136
2137static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2138{
2139 u8 epnum;
2140
2141 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2142
2143 for (epnum = 0; epnum < total; epnum++) {
2144 int ret;
2145
2146 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2147 if (ret)
2148 return ret;
2149 }
2150
2151 return 0;
2152}
2153
2154static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2155{
2156 struct dwc3_ep *dep;
2157 u8 epnum;
2158
2159 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2160 dep = dwc->eps[epnum];
2161 if (!dep)
2162 continue;
2163 /*
2164 * Physical endpoints 0 and 1 are special; they form the
2165 * bi-directional USB endpoint 0.
2166 *
2167 * For those two physical endpoints, we don't allocate a TRB
2168 * pool nor do we add them the endpoints list. Due to that, we
2169 * shouldn't do these two operations otherwise we would end up
2170 * with all sorts of bugs when removing dwc3.ko.
2171 */
2172 if (epnum != 0 && epnum != 1) {
2173 dwc3_free_trb_pool(dep);
2174 list_del(&dep->endpoint.ep_list);
2175 }
2176
2177 kfree(dep);
2178 }
2179}
2180
2181/* -------------------------------------------------------------------------- */
2182
2183static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2184 struct dwc3_request *req, struct dwc3_trb *trb,
2185 const struct dwc3_event_depevt *event, int status, int chain)
2186{
2187 unsigned int count;
2188
2189 dwc3_ep_inc_deq(dep);
2190
2191 trace_dwc3_complete_trb(dep, trb);
2192 req->num_trbs--;
2193
2194 /*
2195 * If we're in the middle of series of chained TRBs and we
2196 * receive a short transfer along the way, DWC3 will skip
2197 * through all TRBs including the last TRB in the chain (the
2198 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2199 * bit and SW has to do it manually.
2200 *
2201 * We're going to do that here to avoid problems of HW trying
2202 * to use bogus TRBs for transfers.
2203 */
2204 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2205 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2206
2207 /*
2208 * If we're dealing with unaligned size OUT transfer, we will be left
2209 * with one TRB pending in the ring. We need to manually clear HWO bit
2210 * from that TRB.
2211 */
2212
2213 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
2214 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2215 return 1;
2216 }
2217
2218 count = trb->size & DWC3_TRB_SIZE_MASK;
2219 req->remaining += count;
2220
2221 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2222 return 1;
2223
2224 if (event->status & DEPEVT_STATUS_SHORT && !chain)
2225 return 1;
2226
2227 if (event->status & DEPEVT_STATUS_IOC)
2228 return 1;
2229
2230 return 0;
2231}
2232
2233static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2234 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2235 int status)
2236{
2237 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2238 struct scatterlist *sg = req->sg;
2239 struct scatterlist *s;
2240 unsigned int pending = req->num_pending_sgs;
2241 unsigned int i;
2242 int ret = 0;
2243
2244 for_each_sg(sg, s, pending, i) {
2245 trb = &dep->trb_pool[dep->trb_dequeue];
2246
2247 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2248 break;
2249
2250 req->sg = sg_next(s);
2251 req->num_pending_sgs--;
2252
2253 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2254 trb, event, status, true);
2255 if (ret)
2256 break;
2257 }
2258
2259 return ret;
2260}
2261
2262static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2263 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2264 int status)
2265{
2266 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2267
2268 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2269 event, status, false);
2270}
2271
2272static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2273{
2274 /*
2275 * For OUT direction, host may send less than the setup
2276 * length. Return true for all OUT requests.
2277 */
2278 if (!req->direction)
2279 return true;
2280
2281 return req->request.actual == req->request.length;
2282}
2283
2284static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2285 const struct dwc3_event_depevt *event,
2286 struct dwc3_request *req, int status)
2287{
2288 int ret;
2289
2290 if (req->num_pending_sgs)
2291 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2292 status);
2293 else
2294 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2295 status);
2296
2297 if (req->needs_extra_trb) {
2298 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2299 status);
2300 req->needs_extra_trb = false;
2301 }
2302
2303 req->request.actual = req->request.length - req->remaining;
2304
2305 if (!dwc3_gadget_ep_request_completed(req) ||
2306 req->num_pending_sgs) {
2307 __dwc3_gadget_kick_transfer(dep);
2308 goto out;
2309 }
2310
2311 dwc3_gadget_giveback(dep, req, status);
2312
2313out:
2314 return ret;
2315}
2316
2317static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2318 const struct dwc3_event_depevt *event, int status)
2319{
2320 struct dwc3_request *req;
2321 struct dwc3_request *tmp;
2322
2323 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2324 int ret;
2325
2326 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2327 req, status);
2328 if (ret)
2329 break;
2330 }
2331}
2332
2333static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2334 const struct dwc3_event_depevt *event)
2335{
2336 dep->frame_number = event->parameters;
2337}
2338
2339static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2340 const struct dwc3_event_depevt *event)
2341{
2342 struct dwc3 *dwc = dep->dwc;
2343 unsigned status = 0;
2344 bool stop = false;
2345
2346 dwc3_gadget_endpoint_frame_from_event(dep, event);
2347
2348 if (event->status & DEPEVT_STATUS_BUSERR)
2349 status = -ECONNRESET;
2350
2351 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2352 status = -EXDEV;
2353
2354 if (list_empty(&dep->started_list))
2355 stop = true;
2356 }
2357
2358 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2359
2360 if (stop) {
2361 dwc3_stop_active_transfer(dep, true);
2362 dep->flags = DWC3_EP_ENABLED;
2363 }
2364
2365 /*
2366 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2367 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2368 */
2369 if (dwc->revision < DWC3_REVISION_183A) {
2370 u32 reg;
2371 int i;
2372
2373 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2374 dep = dwc->eps[i];
2375
2376 if (!(dep->flags & DWC3_EP_ENABLED))
2377 continue;
2378
2379 if (!list_empty(&dep->started_list))
2380 return;
2381 }
2382
2383 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2384 reg |= dwc->u1u2;
2385 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2386
2387 dwc->u1u2 = 0;
2388 }
2389}
2390
2391static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2392 const struct dwc3_event_depevt *event)
2393{
2394 dwc3_gadget_endpoint_frame_from_event(dep, event);
2395 __dwc3_gadget_start_isoc(dep);
2396}
2397
2398static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2399 const struct dwc3_event_depevt *event)
2400{
2401 struct dwc3_ep *dep;
2402 u8 epnum = event->endpoint_number;
2403 u8 cmd;
2404
2405 dep = dwc->eps[epnum];
2406
2407 if (!(dep->flags & DWC3_EP_ENABLED)) {
2408 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2409 return;
2410
2411 /* Handle only EPCMDCMPLT when EP disabled */
2412 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2413 return;
2414 }
2415
2416 if (epnum == 0 || epnum == 1) {
2417 dwc3_ep0_interrupt(dwc, event);
2418 return;
2419 }
2420
2421 switch (event->endpoint_event) {
2422 case DWC3_DEPEVT_XFERINPROGRESS:
2423 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2424 break;
2425 case DWC3_DEPEVT_XFERNOTREADY:
2426 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2427 break;
2428 case DWC3_DEPEVT_EPCMDCMPLT:
2429 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2430
2431 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2432 dep->flags &= ~(DWC3_EP_END_TRANSFER_PENDING |
2433 DWC3_EP_TRANSFER_STARTED);
2434 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2435 }
2436 break;
2437 case DWC3_DEPEVT_STREAMEVT:
2438 case DWC3_DEPEVT_XFERCOMPLETE:
2439 case DWC3_DEPEVT_RXTXFIFOEVT:
2440 break;
2441 }
2442}
2443
2444static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2445{
2446 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2447 spin_unlock(&dwc->lock);
2448 dwc->gadget_driver->disconnect(&dwc->gadget);
2449 spin_lock(&dwc->lock);
2450 }
2451}
2452
2453static void dwc3_suspend_gadget(struct dwc3 *dwc)
2454{
2455 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2456 spin_unlock(&dwc->lock);
2457 dwc->gadget_driver->suspend(&dwc->gadget);
2458 spin_lock(&dwc->lock);
2459 }
2460}
2461
2462static void dwc3_resume_gadget(struct dwc3 *dwc)
2463{
2464 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2465 spin_unlock(&dwc->lock);
2466 dwc->gadget_driver->resume(&dwc->gadget);
2467 spin_lock(&dwc->lock);
2468 }
2469}
2470
2471static void dwc3_reset_gadget(struct dwc3 *dwc)
2472{
2473 if (!dwc->gadget_driver)
2474 return;
2475
2476 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2477 spin_unlock(&dwc->lock);
2478 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2479 spin_lock(&dwc->lock);
2480 }
2481}
2482
2483static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
2484{
2485 struct dwc3 *dwc = dep->dwc;
2486 struct dwc3_gadget_ep_cmd_params params;
2487 u32 cmd;
2488 int ret;
2489
2490 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2491 !dep->resource_index)
2492 return;
2493
2494 /*
2495 * NOTICE: We are violating what the Databook says about the
2496 * EndTransfer command. Ideally we would _always_ wait for the
2497 * EndTransfer Command Completion IRQ, but that's causing too
2498 * much trouble synchronizing between us and gadget driver.
2499 *
2500 * We have discussed this with the IP Provider and it was
2501 * suggested to giveback all requests here, but give HW some
2502 * extra time to synchronize with the interconnect. We're using
2503 * an arbitrary 100us delay for that.
2504 *
2505 * Note also that a similar handling was tested by Synopsys
2506 * (thanks a lot Paul) and nothing bad has come out of it.
2507 * In short, what we're doing is:
2508 *
2509 * - Issue EndTransfer WITH CMDIOC bit set
2510 * - Wait 100us
2511 *
2512 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2513 * supports a mode to work around the above limitation. The
2514 * software can poll the CMDACT bit in the DEPCMD register
2515 * after issuing a EndTransfer command. This mode is enabled
2516 * by writing GUCTL2[14]. This polling is already done in the
2517 * dwc3_send_gadget_ep_cmd() function so if the mode is
2518 * enabled, the EndTransfer command will have completed upon
2519 * returning from this function and we don't need to delay for
2520 * 100us.
2521 *
2522 * This mode is NOT available on the DWC_usb31 IP.
2523 */
2524
2525 cmd = DWC3_DEPCMD_ENDTRANSFER;
2526 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2527 cmd |= DWC3_DEPCMD_CMDIOC;
2528 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2529 memset(&params, 0, sizeof(params));
2530 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
2531 WARN_ON_ONCE(ret);
2532 dep->resource_index = 0;
2533
2534 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2535 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2536 udelay(100);
2537 }
2538}
2539
2540static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2541{
2542 u32 epnum;
2543
2544 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2545 struct dwc3_ep *dep;
2546 int ret;
2547
2548 dep = dwc->eps[epnum];
2549 if (!dep)
2550 continue;
2551
2552 if (!(dep->flags & DWC3_EP_STALL))
2553 continue;
2554
2555 dep->flags &= ~DWC3_EP_STALL;
2556
2557 ret = dwc3_send_clear_stall_ep_cmd(dep);
2558 WARN_ON_ONCE(ret);
2559 }
2560}
2561
2562static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2563{
2564 int reg;
2565
2566 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2567 reg &= ~DWC3_DCTL_INITU1ENA;
2568 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2569
2570 reg &= ~DWC3_DCTL_INITU2ENA;
2571 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2572
2573 dwc3_disconnect_gadget(dwc);
2574
2575 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2576 dwc->setup_packet_pending = false;
2577 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2578
2579 dwc->connected = false;
2580}
2581
2582static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2583{
2584 u32 reg;
2585
2586 dwc->connected = true;
2587
2588 /*
2589 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2590 * would cause a missing Disconnect Event if there's a
2591 * pending Setup Packet in the FIFO.
2592 *
2593 * There's no suggested workaround on the official Bug
2594 * report, which states that "unless the driver/application
2595 * is doing any special handling of a disconnect event,
2596 * there is no functional issue".
2597 *
2598 * Unfortunately, it turns out that we _do_ some special
2599 * handling of a disconnect event, namely complete all
2600 * pending transfers, notify gadget driver of the
2601 * disconnection, and so on.
2602 *
2603 * Our suggested workaround is to follow the Disconnect
2604 * Event steps here, instead, based on a setup_packet_pending
2605 * flag. Such flag gets set whenever we have a SETUP_PENDING
2606 * status for EP0 TRBs and gets cleared on XferComplete for the
2607 * same endpoint.
2608 *
2609 * Refers to:
2610 *
2611 * STAR#9000466709: RTL: Device : Disconnect event not
2612 * generated if setup packet pending in FIFO
2613 */
2614 if (dwc->revision < DWC3_REVISION_188A) {
2615 if (dwc->setup_packet_pending)
2616 dwc3_gadget_disconnect_interrupt(dwc);
2617 }
2618
2619 dwc3_reset_gadget(dwc);
2620
2621 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2622 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2623 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2624 dwc->test_mode = false;
2625 dwc3_clear_stall_all_ep(dwc);
2626
2627 /* Reset device address to zero */
2628 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2629 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2630 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2631}
2632
2633static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2634{
2635 struct dwc3_ep *dep;
2636 int ret;
2637 u32 reg;
2638 u8 speed;
2639
2640 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2641 speed = reg & DWC3_DSTS_CONNECTSPD;
2642 dwc->speed = speed;
2643
2644 /*
2645 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2646 * each time on Connect Done.
2647 *
2648 * Currently we always use the reset value. If any platform
2649 * wants to set this to a different value, we need to add a
2650 * setting and update GCTL.RAMCLKSEL here.
2651 */
2652
2653 switch (speed) {
2654 case DWC3_DSTS_SUPERSPEED_PLUS:
2655 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2656 dwc->gadget.ep0->maxpacket = 512;
2657 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2658 break;
2659 case DWC3_DSTS_SUPERSPEED:
2660 /*
2661 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2662 * would cause a missing USB3 Reset event.
2663 *
2664 * In such situations, we should force a USB3 Reset
2665 * event by calling our dwc3_gadget_reset_interrupt()
2666 * routine.
2667 *
2668 * Refers to:
2669 *
2670 * STAR#9000483510: RTL: SS : USB3 reset event may
2671 * not be generated always when the link enters poll
2672 */
2673 if (dwc->revision < DWC3_REVISION_190A)
2674 dwc3_gadget_reset_interrupt(dwc);
2675
2676 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2677 dwc->gadget.ep0->maxpacket = 512;
2678 dwc->gadget.speed = USB_SPEED_SUPER;
2679 break;
2680 case DWC3_DSTS_HIGHSPEED:
2681 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2682 dwc->gadget.ep0->maxpacket = 64;
2683 dwc->gadget.speed = USB_SPEED_HIGH;
2684 break;
2685 case DWC3_DSTS_FULLSPEED:
2686 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2687 dwc->gadget.ep0->maxpacket = 64;
2688 dwc->gadget.speed = USB_SPEED_FULL;
2689 break;
2690 case DWC3_DSTS_LOWSPEED:
2691 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2692 dwc->gadget.ep0->maxpacket = 8;
2693 dwc->gadget.speed = USB_SPEED_LOW;
2694 break;
2695 }
2696
2697 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2698
2699 /* Enable USB2 LPM Capability */
2700
2701 if ((dwc->revision > DWC3_REVISION_194A) &&
2702 (speed != DWC3_DSTS_SUPERSPEED) &&
2703 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2704 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2705 reg |= DWC3_DCFG_LPM_CAP;
2706 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2707
2708 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2709 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2710
2711 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2712
2713 /*
2714 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2715 * DCFG.LPMCap is set, core responses with an ACK and the
2716 * BESL value in the LPM token is less than or equal to LPM
2717 * NYET threshold.
2718 */
2719 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2720 && dwc->has_lpm_erratum,
2721 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2722
2723 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2724 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2725
2726 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2727 } else {
2728 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2729 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2730 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2731 }
2732
2733 dep = dwc->eps[0];
2734 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2735 if (ret) {
2736 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2737 return;
2738 }
2739
2740 dep = dwc->eps[1];
2741 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2742 if (ret) {
2743 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2744 return;
2745 }
2746
2747 /*
2748 * Configure PHY via GUSB3PIPECTLn if required.
2749 *
2750 * Update GTXFIFOSIZn
2751 *
2752 * In both cases reset values should be sufficient.
2753 */
2754}
2755
2756static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2757{
2758 /*
2759 * TODO take core out of low power mode when that's
2760 * implemented.
2761 */
2762
2763 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2764 spin_unlock(&dwc->lock);
2765 dwc->gadget_driver->resume(&dwc->gadget);
2766 spin_lock(&dwc->lock);
2767 }
2768}
2769
2770static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2771 unsigned int evtinfo)
2772{
2773 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2774 unsigned int pwropt;
2775
2776 /*
2777 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2778 * Hibernation mode enabled which would show up when device detects
2779 * host-initiated U3 exit.
2780 *
2781 * In that case, device will generate a Link State Change Interrupt
2782 * from U3 to RESUME which is only necessary if Hibernation is
2783 * configured in.
2784 *
2785 * There are no functional changes due to such spurious event and we
2786 * just need to ignore it.
2787 *
2788 * Refers to:
2789 *
2790 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2791 * operational mode
2792 */
2793 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2794 if ((dwc->revision < DWC3_REVISION_250A) &&
2795 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2796 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2797 (next == DWC3_LINK_STATE_RESUME)) {
2798 return;
2799 }
2800 }
2801
2802 /*
2803 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2804 * on the link partner, the USB session might do multiple entry/exit
2805 * of low power states before a transfer takes place.
2806 *
2807 * Due to this problem, we might experience lower throughput. The
2808 * suggested workaround is to disable DCTL[12:9] bits if we're
2809 * transitioning from U1/U2 to U0 and enable those bits again
2810 * after a transfer completes and there are no pending transfers
2811 * on any of the enabled endpoints.
2812 *
2813 * This is the first half of that workaround.
2814 *
2815 * Refers to:
2816 *
2817 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2818 * core send LGO_Ux entering U0
2819 */
2820 if (dwc->revision < DWC3_REVISION_183A) {
2821 if (next == DWC3_LINK_STATE_U0) {
2822 u32 u1u2;
2823 u32 reg;
2824
2825 switch (dwc->link_state) {
2826 case DWC3_LINK_STATE_U1:
2827 case DWC3_LINK_STATE_U2:
2828 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2829 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2830 | DWC3_DCTL_ACCEPTU2ENA
2831 | DWC3_DCTL_INITU1ENA
2832 | DWC3_DCTL_ACCEPTU1ENA);
2833
2834 if (!dwc->u1u2)
2835 dwc->u1u2 = reg & u1u2;
2836
2837 reg &= ~u1u2;
2838
2839 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2840 break;
2841 default:
2842 /* do nothing */
2843 break;
2844 }
2845 }
2846 }
2847
2848 switch (next) {
2849 case DWC3_LINK_STATE_U1:
2850 if (dwc->speed == USB_SPEED_SUPER)
2851 dwc3_suspend_gadget(dwc);
2852 break;
2853 case DWC3_LINK_STATE_U2:
2854 case DWC3_LINK_STATE_U3:
2855 dwc3_suspend_gadget(dwc);
2856 break;
2857 case DWC3_LINK_STATE_RESUME:
2858 dwc3_resume_gadget(dwc);
2859 break;
2860 default:
2861 /* do nothing */
2862 break;
2863 }
2864
2865 dwc->link_state = next;
2866}
2867
2868static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2869 unsigned int evtinfo)
2870{
2871 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2872
2873 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2874 dwc3_suspend_gadget(dwc);
2875
2876 dwc->link_state = next;
2877}
2878
2879static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2880 unsigned int evtinfo)
2881{
2882 unsigned int is_ss = evtinfo & BIT(4);
2883
2884 /*
2885 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2886 * have a known issue which can cause USB CV TD.9.23 to fail
2887 * randomly.
2888 *
2889 * Because of this issue, core could generate bogus hibernation
2890 * events which SW needs to ignore.
2891 *
2892 * Refers to:
2893 *
2894 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2895 * Device Fallback from SuperSpeed
2896 */
2897 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2898 return;
2899
2900 /* enter hibernation here */
2901}
2902
2903static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2904 const struct dwc3_event_devt *event)
2905{
2906 switch (event->type) {
2907 case DWC3_DEVICE_EVENT_DISCONNECT:
2908 dwc3_gadget_disconnect_interrupt(dwc);
2909 break;
2910 case DWC3_DEVICE_EVENT_RESET:
2911 dwc3_gadget_reset_interrupt(dwc);
2912 break;
2913 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2914 dwc3_gadget_conndone_interrupt(dwc);
2915 break;
2916 case DWC3_DEVICE_EVENT_WAKEUP:
2917 dwc3_gadget_wakeup_interrupt(dwc);
2918 break;
2919 case DWC3_DEVICE_EVENT_HIBER_REQ:
2920 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2921 "unexpected hibernation event\n"))
2922 break;
2923
2924 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2925 break;
2926 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2927 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2928 break;
2929 case DWC3_DEVICE_EVENT_EOPF:
2930 /* It changed to be suspend event for version 2.30a and above */
2931 if (dwc->revision >= DWC3_REVISION_230A) {
2932 /*
2933 * Ignore suspend event until the gadget enters into
2934 * USB_STATE_CONFIGURED state.
2935 */
2936 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2937 dwc3_gadget_suspend_interrupt(dwc,
2938 event->event_info);
2939 }
2940 break;
2941 case DWC3_DEVICE_EVENT_SOF:
2942 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2943 case DWC3_DEVICE_EVENT_CMD_CMPL:
2944 case DWC3_DEVICE_EVENT_OVERFLOW:
2945 break;
2946 default:
2947 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2948 }
2949}
2950
2951static void dwc3_process_event_entry(struct dwc3 *dwc,
2952 const union dwc3_event *event)
2953{
2954 trace_dwc3_event(event->raw, dwc);
2955
2956 if (!event->type.is_devspec)
2957 dwc3_endpoint_interrupt(dwc, &event->depevt);
2958 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
2959 dwc3_gadget_interrupt(dwc, &event->devt);
2960 else
2961 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2962}
2963
2964static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
2965{
2966 struct dwc3 *dwc = evt->dwc;
2967 irqreturn_t ret = IRQ_NONE;
2968 int left;
2969 u32 reg;
2970
2971 left = evt->count;
2972
2973 if (!(evt->flags & DWC3_EVENT_PENDING))
2974 return IRQ_NONE;
2975
2976 while (left > 0) {
2977 union dwc3_event event;
2978
2979 event.raw = *(u32 *) (evt->cache + evt->lpos);
2980
2981 dwc3_process_event_entry(dwc, &event);
2982
2983 /*
2984 * FIXME we wrap around correctly to the next entry as
2985 * almost all entries are 4 bytes in size. There is one
2986 * entry which has 12 bytes which is a regular entry
2987 * followed by 8 bytes data. ATM I don't know how
2988 * things are organized if we get next to the a
2989 * boundary so I worry about that once we try to handle
2990 * that.
2991 */
2992 evt->lpos = (evt->lpos + 4) % evt->length;
2993 left -= 4;
2994 }
2995
2996 evt->count = 0;
2997 evt->flags &= ~DWC3_EVENT_PENDING;
2998 ret = IRQ_HANDLED;
2999
3000 /* Unmask interrupt */
3001 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3002 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3003 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3004
3005 if (dwc->imod_interval) {
3006 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3007 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3008 }
3009
3010 return ret;
3011}
3012
3013static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3014{
3015 struct dwc3_event_buffer *evt = _evt;
3016 struct dwc3 *dwc = evt->dwc;
3017 unsigned long flags;
3018 irqreturn_t ret = IRQ_NONE;
3019
3020 spin_lock_irqsave(&dwc->lock, flags);
3021 ret = dwc3_process_event_buf(evt);
3022 spin_unlock_irqrestore(&dwc->lock, flags);
3023
3024 return ret;
3025}
3026
3027static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3028{
3029 struct dwc3 *dwc = evt->dwc;
3030 u32 amount;
3031 u32 count;
3032 u32 reg;
3033
3034 if (pm_runtime_suspended(dwc->dev)) {
3035 pm_runtime_get(dwc->dev);
3036 disable_irq_nosync(dwc->irq_gadget);
3037 dwc->pending_events = true;
3038 return IRQ_HANDLED;
3039 }
3040
3041 /*
3042 * With PCIe legacy interrupt, test shows that top-half irq handler can
3043 * be called again after HW interrupt deassertion. Check if bottom-half
3044 * irq event handler completes before caching new event to prevent
3045 * losing events.
3046 */
3047 if (evt->flags & DWC3_EVENT_PENDING)
3048 return IRQ_HANDLED;
3049
3050 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3051 count &= DWC3_GEVNTCOUNT_MASK;
3052 if (!count)
3053 return IRQ_NONE;
3054
3055 evt->count = count;
3056 evt->flags |= DWC3_EVENT_PENDING;
3057
3058 /* Mask interrupt */
3059 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3060 reg |= DWC3_GEVNTSIZ_INTMASK;
3061 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3062
3063 amount = min(count, evt->length - evt->lpos);
3064 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3065
3066 if (amount < count)
3067 memcpy(evt->cache, evt->buf, count - amount);
3068
3069 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3070
3071 return IRQ_WAKE_THREAD;
3072}
3073
3074static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3075{
3076 struct dwc3_event_buffer *evt = _evt;
3077
3078 return dwc3_check_event_buf(evt);
3079}
3080
3081static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3082{
3083 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3084 int irq;
3085
3086 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3087 if (irq > 0)
3088 goto out;
3089
3090 if (irq == -EPROBE_DEFER)
3091 goto out;
3092
3093 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3094 if (irq > 0)
3095 goto out;
3096
3097 if (irq == -EPROBE_DEFER)
3098 goto out;
3099
3100 irq = platform_get_irq(dwc3_pdev, 0);
3101 if (irq > 0)
3102 goto out;
3103
3104 if (irq != -EPROBE_DEFER)
3105 dev_err(dwc->dev, "missing peripheral IRQ\n");
3106
3107 if (!irq)
3108 irq = -EINVAL;
3109
3110out:
3111 return irq;
3112}
3113
3114/**
3115 * dwc3_gadget_init - initializes gadget related registers
3116 * @dwc: pointer to our controller context structure
3117 *
3118 * Returns 0 on success otherwise negative errno.
3119 */
3120int dwc3_gadget_init(struct dwc3 *dwc)
3121{
3122 int ret;
3123 int irq;
3124
3125 irq = dwc3_gadget_get_irq(dwc);
3126 if (irq < 0) {
3127 ret = irq;
3128 goto err0;
3129 }
3130
3131 dwc->irq_gadget = irq;
3132
3133 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3134 sizeof(*dwc->ep0_trb) * 2,
3135 &dwc->ep0_trb_addr, GFP_KERNEL);
3136 if (!dwc->ep0_trb) {
3137 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3138 ret = -ENOMEM;
3139 goto err0;
3140 }
3141
3142 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3143 if (!dwc->setup_buf) {
3144 ret = -ENOMEM;
3145 goto err1;
3146 }
3147
3148 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3149 &dwc->bounce_addr, GFP_KERNEL);
3150 if (!dwc->bounce) {
3151 ret = -ENOMEM;
3152 goto err2;
3153 }
3154
3155 init_completion(&dwc->ep0_in_setup);
3156
3157 dwc->gadget.ops = &dwc3_gadget_ops;
3158 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3159 dwc->gadget.sg_supported = true;
3160 dwc->gadget.name = "dwc3-gadget";
3161 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
3162
3163 /*
3164 * FIXME We might be setting max_speed to <SUPER, however versions
3165 * <2.20a of dwc3 have an issue with metastability (documented
3166 * elsewhere in this driver) which tells us we can't set max speed to
3167 * anything lower than SUPER.
3168 *
3169 * Because gadget.max_speed is only used by composite.c and function
3170 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3171 * to happen so we avoid sending SuperSpeed Capability descriptor
3172 * together with our BOS descriptor as that could confuse host into
3173 * thinking we can handle super speed.
3174 *
3175 * Note that, in fact, we won't even support GetBOS requests when speed
3176 * is less than super speed because we don't have means, yet, to tell
3177 * composite.c that we are USB 2.0 + LPM ECN.
3178 */
3179 if (dwc->revision < DWC3_REVISION_220A &&
3180 !dwc->dis_metastability_quirk)
3181 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3182 dwc->revision);
3183
3184 dwc->gadget.max_speed = dwc->maximum_speed;
3185
3186 /*
3187 * REVISIT: Here we should clear all pending IRQs to be
3188 * sure we're starting from a well known location.
3189 */
3190
3191 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3192 if (ret)
3193 goto err3;
3194
3195 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3196 if (ret) {
3197 dev_err(dwc->dev, "failed to register udc\n");
3198 goto err4;
3199 }
3200
3201 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3202
3203 return 0;
3204
3205err4:
3206 dwc3_gadget_free_endpoints(dwc);
3207
3208err3:
3209 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3210 dwc->bounce_addr);
3211
3212err2:
3213 kfree(dwc->setup_buf);
3214
3215err1:
3216 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3217 dwc->ep0_trb, dwc->ep0_trb_addr);
3218
3219err0:
3220 return ret;
3221}
3222
3223/* -------------------------------------------------------------------------- */
3224
3225void dwc3_gadget_exit(struct dwc3 *dwc)
3226{
3227 usb_del_gadget_udc(&dwc->gadget);
3228 dwc3_gadget_free_endpoints(dwc);
3229 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3230 dwc->bounce_addr);
3231 kfree(dwc->setup_buf);
3232 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3233 dwc->ep0_trb, dwc->ep0_trb_addr);
3234}
3235
3236int dwc3_gadget_suspend(struct dwc3 *dwc)
3237{
3238 if (!dwc->gadget_driver)
3239 return 0;
3240
3241 dwc3_gadget_run_stop(dwc, false, false);
3242 dwc3_disconnect_gadget(dwc);
3243 __dwc3_gadget_stop(dwc);
3244
3245 return 0;
3246}
3247
3248int dwc3_gadget_resume(struct dwc3 *dwc)
3249{
3250 int ret;
3251
3252 if (!dwc->gadget_driver)
3253 return 0;
3254
3255 ret = __dwc3_gadget_start(dwc);
3256 if (ret < 0)
3257 goto err0;
3258
3259 ret = dwc3_gadget_run_stop(dwc, true, false);
3260 if (ret < 0)
3261 goto err1;
3262
3263 return 0;
3264
3265err1:
3266 __dwc3_gadget_stop(dwc);
3267
3268err0:
3269 return ret;
3270}
3271
3272void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3273{
3274 if (dwc->pending_events) {
3275 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3276 dwc->pending_events = false;
3277 enable_irq(dwc->irq_gadget);
3278 }
3279}