xref: /openbmc/linux/drivers/usb/dwc3/gadget.c (revision 9b68f30b)
1 // 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 - https://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, n)	(((d)->frame_number + ((d)->interval * (n))) \
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  */
41 int 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 USB_TEST_J:
50 	case USB_TEST_K:
51 	case USB_TEST_SE0_NAK:
52 	case USB_TEST_PACKET:
53 	case USB_TEST_FORCE_ENABLE:
54 		reg |= mode << 1;
55 		break;
56 	default:
57 		return -EINVAL;
58 	}
59 
60 	dwc3_gadget_dctl_write_safe(dwc, 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  */
72 int 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  */
89 int 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 (!DWC3_VER_IS_PRIOR(DWC3, 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 no action before sending new link state change */
115 	dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116 
117 	/* set requested state */
118 	reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 	dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120 
121 	/*
122 	 * The following code is racy when called from dwc3_gadget_wakeup,
123 	 * and is not needed, at least on newer versions
124 	 */
125 	if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
126 		return 0;
127 
128 	/* wait for a change in DSTS */
129 	retries = 10000;
130 	while (--retries) {
131 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132 
133 		if (DWC3_DSTS_USBLNKST(reg) == state)
134 			return 0;
135 
136 		udelay(5);
137 	}
138 
139 	return -ETIMEDOUT;
140 }
141 
142 static void dwc3_ep0_reset_state(struct dwc3 *dwc)
143 {
144 	unsigned int	dir;
145 
146 	if (dwc->ep0state != EP0_SETUP_PHASE) {
147 		dir = !!dwc->ep0_expect_in;
148 		if (dwc->ep0state == EP0_DATA_PHASE)
149 			dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
150 		else
151 			dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
152 
153 		dwc->eps[0]->trb_enqueue = 0;
154 		dwc->eps[1]->trb_enqueue = 0;
155 
156 		dwc3_ep0_stall_and_restart(dwc);
157 	}
158 }
159 
160 /**
161  * dwc3_ep_inc_trb - increment a trb index.
162  * @index: Pointer to the TRB index to increment.
163  *
164  * The index should never point to the link TRB. After incrementing,
165  * if it is point to the link TRB, wrap around to the beginning. The
166  * link TRB is always at the last TRB entry.
167  */
168 static void dwc3_ep_inc_trb(u8 *index)
169 {
170 	(*index)++;
171 	if (*index == (DWC3_TRB_NUM - 1))
172 		*index = 0;
173 }
174 
175 /**
176  * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
177  * @dep: The endpoint whose enqueue pointer we're incrementing
178  */
179 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
180 {
181 	dwc3_ep_inc_trb(&dep->trb_enqueue);
182 }
183 
184 /**
185  * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
186  * @dep: The endpoint whose enqueue pointer we're incrementing
187  */
188 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
189 {
190 	dwc3_ep_inc_trb(&dep->trb_dequeue);
191 }
192 
193 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
194 		struct dwc3_request *req, int status)
195 {
196 	struct dwc3			*dwc = dep->dwc;
197 
198 	list_del(&req->list);
199 	req->remaining = 0;
200 	req->needs_extra_trb = false;
201 
202 	if (req->request.status == -EINPROGRESS)
203 		req->request.status = status;
204 
205 	if (req->trb)
206 		usb_gadget_unmap_request_by_dev(dwc->sysdev,
207 				&req->request, req->direction);
208 
209 	req->trb = NULL;
210 	trace_dwc3_gadget_giveback(req);
211 
212 	if (dep->number > 1)
213 		pm_runtime_put(dwc->dev);
214 }
215 
216 /**
217  * dwc3_gadget_giveback - call struct usb_request's ->complete callback
218  * @dep: The endpoint to whom the request belongs to
219  * @req: The request we're giving back
220  * @status: completion code for the request
221  *
222  * Must be called with controller's lock held and interrupts disabled. This
223  * function will unmap @req and call its ->complete() callback to notify upper
224  * layers that it has completed.
225  */
226 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
227 		int status)
228 {
229 	struct dwc3			*dwc = dep->dwc;
230 
231 	dwc3_gadget_del_and_unmap_request(dep, req, status);
232 	req->status = DWC3_REQUEST_STATUS_COMPLETED;
233 
234 	spin_unlock(&dwc->lock);
235 	usb_gadget_giveback_request(&dep->endpoint, &req->request);
236 	spin_lock(&dwc->lock);
237 }
238 
239 /**
240  * dwc3_send_gadget_generic_command - issue a generic command for the controller
241  * @dwc: pointer to the controller context
242  * @cmd: the command to be issued
243  * @param: command parameter
244  *
245  * Caller should take care of locking. Issue @cmd with a given @param to @dwc
246  * and wait for its completion.
247  */
248 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
249 		u32 param)
250 {
251 	u32		timeout = 500;
252 	int		status = 0;
253 	int		ret = 0;
254 	u32		reg;
255 
256 	dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
257 	dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
258 
259 	do {
260 		reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
261 		if (!(reg & DWC3_DGCMD_CMDACT)) {
262 			status = DWC3_DGCMD_STATUS(reg);
263 			if (status)
264 				ret = -EINVAL;
265 			break;
266 		}
267 	} while (--timeout);
268 
269 	if (!timeout) {
270 		ret = -ETIMEDOUT;
271 		status = -ETIMEDOUT;
272 	}
273 
274 	trace_dwc3_gadget_generic_cmd(cmd, param, status);
275 
276 	return ret;
277 }
278 
279 static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async);
280 
281 /**
282  * dwc3_send_gadget_ep_cmd - issue an endpoint command
283  * @dep: the endpoint to which the command is going to be issued
284  * @cmd: the command to be issued
285  * @params: parameters to the command
286  *
287  * Caller should handle locking. This function will issue @cmd with given
288  * @params to @dep and wait for its completion.
289  */
290 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
291 		struct dwc3_gadget_ep_cmd_params *params)
292 {
293 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
294 	struct dwc3		*dwc = dep->dwc;
295 	u32			timeout = 5000;
296 	u32			saved_config = 0;
297 	u32			reg;
298 
299 	int			cmd_status = 0;
300 	int			ret = -EINVAL;
301 
302 	/*
303 	 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
304 	 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
305 	 * endpoint command.
306 	 *
307 	 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
308 	 * settings. Restore them after the command is completed.
309 	 *
310 	 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
311 	 */
312 	if (dwc->gadget->speed <= USB_SPEED_HIGH ||
313 	    DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
314 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
315 		if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
316 			saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
317 			reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
318 		}
319 
320 		if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
321 			saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
322 			reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
323 		}
324 
325 		if (saved_config)
326 			dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
327 	}
328 
329 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
330 		int link_state;
331 
332 		/*
333 		 * Initiate remote wakeup if the link state is in U3 when
334 		 * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
335 		 * link state is in U1/U2, no remote wakeup is needed. The Start
336 		 * Transfer command will initiate the link recovery.
337 		 */
338 		link_state = dwc3_gadget_get_link_state(dwc);
339 		switch (link_state) {
340 		case DWC3_LINK_STATE_U2:
341 			if (dwc->gadget->speed >= USB_SPEED_SUPER)
342 				break;
343 
344 			fallthrough;
345 		case DWC3_LINK_STATE_U3:
346 			ret = __dwc3_gadget_wakeup(dwc, false);
347 			dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
348 					ret);
349 			break;
350 		}
351 	}
352 
353 	/*
354 	 * For some commands such as Update Transfer command, DEPCMDPARn
355 	 * registers are reserved. Since the driver often sends Update Transfer
356 	 * command, don't write to DEPCMDPARn to avoid register write delays and
357 	 * improve performance.
358 	 */
359 	if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
360 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
361 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
362 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
363 	}
364 
365 	/*
366 	 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
367 	 * not relying on XferNotReady, we can make use of a special "No
368 	 * Response Update Transfer" command where we should clear both CmdAct
369 	 * and CmdIOC bits.
370 	 *
371 	 * With this, we don't need to wait for command completion and can
372 	 * straight away issue further commands to the endpoint.
373 	 *
374 	 * NOTICE: We're making an assumption that control endpoints will never
375 	 * make use of Update Transfer command. This is a safe assumption
376 	 * because we can never have more than one request at a time with
377 	 * Control Endpoints. If anybody changes that assumption, this chunk
378 	 * needs to be updated accordingly.
379 	 */
380 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
381 			!usb_endpoint_xfer_isoc(desc))
382 		cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
383 	else
384 		cmd |= DWC3_DEPCMD_CMDACT;
385 
386 	dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
387 
388 	if (!(cmd & DWC3_DEPCMD_CMDACT) ||
389 		(DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
390 		!(cmd & DWC3_DEPCMD_CMDIOC))) {
391 		ret = 0;
392 		goto skip_status;
393 	}
394 
395 	do {
396 		reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
397 		if (!(reg & DWC3_DEPCMD_CMDACT)) {
398 			cmd_status = DWC3_DEPCMD_STATUS(reg);
399 
400 			switch (cmd_status) {
401 			case 0:
402 				ret = 0;
403 				break;
404 			case DEPEVT_TRANSFER_NO_RESOURCE:
405 				dev_WARN(dwc->dev, "No resource for %s\n",
406 					 dep->name);
407 				ret = -EINVAL;
408 				break;
409 			case DEPEVT_TRANSFER_BUS_EXPIRY:
410 				/*
411 				 * SW issues START TRANSFER command to
412 				 * isochronous ep with future frame interval. If
413 				 * future interval time has already passed when
414 				 * core receives the command, it will respond
415 				 * with an error status of 'Bus Expiry'.
416 				 *
417 				 * Instead of always returning -EINVAL, let's
418 				 * give a hint to the gadget driver that this is
419 				 * the case by returning -EAGAIN.
420 				 */
421 				ret = -EAGAIN;
422 				break;
423 			default:
424 				dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
425 			}
426 
427 			break;
428 		}
429 	} while (--timeout);
430 
431 	if (timeout == 0) {
432 		ret = -ETIMEDOUT;
433 		cmd_status = -ETIMEDOUT;
434 	}
435 
436 skip_status:
437 	trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
438 
439 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
440 		if (ret == 0)
441 			dep->flags |= DWC3_EP_TRANSFER_STARTED;
442 
443 		if (ret != -ETIMEDOUT)
444 			dwc3_gadget_ep_get_transfer_index(dep);
445 	}
446 
447 	if (saved_config) {
448 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
449 		reg |= saved_config;
450 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
451 	}
452 
453 	return ret;
454 }
455 
456 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
457 {
458 	struct dwc3 *dwc = dep->dwc;
459 	struct dwc3_gadget_ep_cmd_params params;
460 	u32 cmd = DWC3_DEPCMD_CLEARSTALL;
461 
462 	/*
463 	 * As of core revision 2.60a the recommended programming model
464 	 * is to set the ClearPendIN bit when issuing a Clear Stall EP
465 	 * command for IN endpoints. This is to prevent an issue where
466 	 * some (non-compliant) hosts may not send ACK TPs for pending
467 	 * IN transfers due to a mishandled error condition. Synopsys
468 	 * STAR 9000614252.
469 	 */
470 	if (dep->direction &&
471 	    !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
472 	    (dwc->gadget->speed >= USB_SPEED_SUPER))
473 		cmd |= DWC3_DEPCMD_CLEARPENDIN;
474 
475 	memset(&params, 0, sizeof(params));
476 
477 	return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
478 }
479 
480 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
481 		struct dwc3_trb *trb)
482 {
483 	u32		offset = (char *) trb - (char *) dep->trb_pool;
484 
485 	return dep->trb_pool_dma + offset;
486 }
487 
488 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
489 {
490 	struct dwc3		*dwc = dep->dwc;
491 
492 	if (dep->trb_pool)
493 		return 0;
494 
495 	dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
496 			sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
497 			&dep->trb_pool_dma, GFP_KERNEL);
498 	if (!dep->trb_pool) {
499 		dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
500 				dep->name);
501 		return -ENOMEM;
502 	}
503 
504 	return 0;
505 }
506 
507 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
508 {
509 	struct dwc3		*dwc = dep->dwc;
510 
511 	dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
512 			dep->trb_pool, dep->trb_pool_dma);
513 
514 	dep->trb_pool = NULL;
515 	dep->trb_pool_dma = 0;
516 }
517 
518 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
519 {
520 	struct dwc3_gadget_ep_cmd_params params;
521 
522 	memset(&params, 0x00, sizeof(params));
523 
524 	params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
525 
526 	return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
527 			&params);
528 }
529 
530 /**
531  * dwc3_gadget_start_config - configure ep resources
532  * @dep: endpoint that is being enabled
533  *
534  * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
535  * completion, it will set Transfer Resource for all available endpoints.
536  *
537  * The assignment of transfer resources cannot perfectly follow the data book
538  * due to the fact that the controller driver does not have all knowledge of the
539  * configuration in advance. It is given this information piecemeal by the
540  * composite gadget framework after every SET_CONFIGURATION and
541  * SET_INTERFACE. Trying to follow the databook programming model in this
542  * scenario can cause errors. For two reasons:
543  *
544  * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
545  * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
546  * incorrect in the scenario of multiple interfaces.
547  *
548  * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
549  * endpoint on alt setting (8.1.6).
550  *
551  * The following simplified method is used instead:
552  *
553  * All hardware endpoints can be assigned a transfer resource and this setting
554  * will stay persistent until either a core reset or hibernation. So whenever we
555  * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
556  * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
557  * guaranteed that there are as many transfer resources as endpoints.
558  *
559  * This function is called for each endpoint when it is being enabled but is
560  * triggered only when called for EP0-out, which always happens first, and which
561  * should only happen in one of the above conditions.
562  */
563 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
564 {
565 	struct dwc3_gadget_ep_cmd_params params;
566 	struct dwc3		*dwc;
567 	u32			cmd;
568 	int			i;
569 	int			ret;
570 
571 	if (dep->number)
572 		return 0;
573 
574 	memset(&params, 0x00, sizeof(params));
575 	cmd = DWC3_DEPCMD_DEPSTARTCFG;
576 	dwc = dep->dwc;
577 
578 	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
579 	if (ret)
580 		return ret;
581 
582 	for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
583 		struct dwc3_ep *dep = dwc->eps[i];
584 
585 		if (!dep)
586 			continue;
587 
588 		ret = dwc3_gadget_set_xfer_resource(dep);
589 		if (ret)
590 			return ret;
591 	}
592 
593 	return 0;
594 }
595 
596 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
597 {
598 	const struct usb_ss_ep_comp_descriptor *comp_desc;
599 	const struct usb_endpoint_descriptor *desc;
600 	struct dwc3_gadget_ep_cmd_params params;
601 	struct dwc3 *dwc = dep->dwc;
602 
603 	comp_desc = dep->endpoint.comp_desc;
604 	desc = dep->endpoint.desc;
605 
606 	memset(&params, 0x00, sizeof(params));
607 
608 	params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
609 		| DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
610 
611 	/* Burst size is only needed in SuperSpeed mode */
612 	if (dwc->gadget->speed >= USB_SPEED_SUPER) {
613 		u32 burst = dep->endpoint.maxburst;
614 
615 		params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
616 	}
617 
618 	params.param0 |= action;
619 	if (action == DWC3_DEPCFG_ACTION_RESTORE)
620 		params.param2 |= dep->saved_state;
621 
622 	if (usb_endpoint_xfer_control(desc))
623 		params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
624 
625 	if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
626 		params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
627 
628 	if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
629 		params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
630 			| DWC3_DEPCFG_XFER_COMPLETE_EN
631 			| DWC3_DEPCFG_STREAM_EVENT_EN;
632 		dep->stream_capable = true;
633 	}
634 
635 	if (!usb_endpoint_xfer_control(desc))
636 		params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
637 
638 	/*
639 	 * We are doing 1:1 mapping for endpoints, meaning
640 	 * Physical Endpoints 2 maps to Logical Endpoint 2 and
641 	 * so on. We consider the direction bit as part of the physical
642 	 * endpoint number. So USB endpoint 0x81 is 0x03.
643 	 */
644 	params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
645 
646 	/*
647 	 * We must use the lower 16 TX FIFOs even though
648 	 * HW might have more
649 	 */
650 	if (dep->direction)
651 		params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
652 
653 	if (desc->bInterval) {
654 		u8 bInterval_m1;
655 
656 		/*
657 		 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
658 		 *
659 		 * NOTE: The programming guide incorrectly stated bInterval_m1
660 		 * must be set to 0 when operating in fullspeed. Internally the
661 		 * controller does not have this limitation. See DWC_usb3x
662 		 * programming guide section 3.2.2.1.
663 		 */
664 		bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
665 
666 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
667 		    dwc->gadget->speed == USB_SPEED_FULL)
668 			dep->interval = desc->bInterval;
669 		else
670 			dep->interval = 1 << (desc->bInterval - 1);
671 
672 		params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
673 	}
674 
675 	return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
676 }
677 
678 /**
679  * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
680  * @dwc: pointer to the DWC3 context
681  * @mult: multiplier to be used when calculating the fifo_size
682  *
683  * Calculates the size value based on the equation below:
684  *
685  * DWC3 revision 280A and prior:
686  * fifo_size = mult * (max_packet / mdwidth) + 1;
687  *
688  * DWC3 revision 290A and onwards:
689  * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
690  *
691  * The max packet size is set to 1024, as the txfifo requirements mainly apply
692  * to super speed USB use cases.  However, it is safe to overestimate the fifo
693  * allocations for other scenarios, i.e. high speed USB.
694  */
695 static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
696 {
697 	int max_packet = 1024;
698 	int fifo_size;
699 	int mdwidth;
700 
701 	mdwidth = dwc3_mdwidth(dwc);
702 
703 	/* MDWIDTH is represented in bits, we need it in bytes */
704 	mdwidth >>= 3;
705 
706 	if (DWC3_VER_IS_PRIOR(DWC3, 290A))
707 		fifo_size = mult * (max_packet / mdwidth) + 1;
708 	else
709 		fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
710 	return fifo_size;
711 }
712 
713 /**
714  * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation
715  * @dwc: pointer to the DWC3 context
716  *
717  * Iterates through all the endpoint registers and clears the previous txfifo
718  * allocations.
719  */
720 void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
721 {
722 	struct dwc3_ep *dep;
723 	int fifo_depth;
724 	int size;
725 	int num;
726 
727 	if (!dwc->do_fifo_resize)
728 		return;
729 
730 	/* Read ep0IN related TXFIFO size */
731 	dep = dwc->eps[1];
732 	size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
733 	if (DWC3_IP_IS(DWC3))
734 		fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
735 	else
736 		fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
737 
738 	dwc->last_fifo_depth = fifo_depth;
739 	/* Clear existing TXFIFO for all IN eps except ep0 */
740 	for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
741 	     num += 2) {
742 		dep = dwc->eps[num];
743 		/* Don't change TXFRAMNUM on usb31 version */
744 		size = DWC3_IP_IS(DWC3) ? 0 :
745 			dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
746 				   DWC31_GTXFIFOSIZ_TXFRAMNUM;
747 
748 		dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
749 		dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
750 	}
751 	dwc->num_ep_resized = 0;
752 }
753 
754 /*
755  * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
756  * @dwc: pointer to our context structure
757  *
758  * This function will a best effort FIFO allocation in order
759  * to improve FIFO usage and throughput, while still allowing
760  * us to enable as many endpoints as possible.
761  *
762  * Keep in mind that this operation will be highly dependent
763  * on the configured size for RAM1 - which contains TxFifo -,
764  * the amount of endpoints enabled on coreConsultant tool, and
765  * the width of the Master Bus.
766  *
767  * In general, FIFO depths are represented with the following equation:
768  *
769  * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
770  *
771  * In conjunction with dwc3_gadget_check_config(), this resizing logic will
772  * ensure that all endpoints will have enough internal memory for one max
773  * packet per endpoint.
774  */
775 static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
776 {
777 	struct dwc3 *dwc = dep->dwc;
778 	int fifo_0_start;
779 	int ram1_depth;
780 	int fifo_size;
781 	int min_depth;
782 	int num_in_ep;
783 	int remaining;
784 	int num_fifos = 1;
785 	int fifo;
786 	int tmp;
787 
788 	if (!dwc->do_fifo_resize)
789 		return 0;
790 
791 	/* resize IN endpoints except ep0 */
792 	if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
793 		return 0;
794 
795 	/* bail if already resized */
796 	if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
797 		return 0;
798 
799 	ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
800 
801 	if ((dep->endpoint.maxburst > 1 &&
802 	     usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
803 	    usb_endpoint_xfer_isoc(dep->endpoint.desc))
804 		num_fifos = 3;
805 
806 	if (dep->endpoint.maxburst > 6 &&
807 	    (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
808 	     usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
809 		num_fifos = dwc->tx_fifo_resize_max_num;
810 
811 	/* FIFO size for a single buffer */
812 	fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
813 
814 	/* Calculate the number of remaining EPs w/o any FIFO */
815 	num_in_ep = dwc->max_cfg_eps;
816 	num_in_ep -= dwc->num_ep_resized;
817 
818 	/* Reserve at least one FIFO for the number of IN EPs */
819 	min_depth = num_in_ep * (fifo + 1);
820 	remaining = ram1_depth - min_depth - dwc->last_fifo_depth;
821 	remaining = max_t(int, 0, remaining);
822 	/*
823 	 * We've already reserved 1 FIFO per EP, so check what we can fit in
824 	 * addition to it.  If there is not enough remaining space, allocate
825 	 * all the remaining space to the EP.
826 	 */
827 	fifo_size = (num_fifos - 1) * fifo;
828 	if (remaining < fifo_size)
829 		fifo_size = remaining;
830 
831 	fifo_size += fifo;
832 	/* Last increment according to the TX FIFO size equation */
833 	fifo_size++;
834 
835 	/* Check if TXFIFOs start at non-zero addr */
836 	tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
837 	fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
838 
839 	fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
840 	if (DWC3_IP_IS(DWC3))
841 		dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
842 	else
843 		dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
844 
845 	/* Check fifo size allocation doesn't exceed available RAM size. */
846 	if (dwc->last_fifo_depth >= ram1_depth) {
847 		dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
848 			dwc->last_fifo_depth, ram1_depth,
849 			dep->endpoint.name, fifo_size);
850 		if (DWC3_IP_IS(DWC3))
851 			fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
852 		else
853 			fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
854 
855 		dwc->last_fifo_depth -= fifo_size;
856 		return -ENOMEM;
857 	}
858 
859 	dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
860 	dep->flags |= DWC3_EP_TXFIFO_RESIZED;
861 	dwc->num_ep_resized++;
862 
863 	return 0;
864 }
865 
866 /**
867  * __dwc3_gadget_ep_enable - initializes a hw endpoint
868  * @dep: endpoint to be initialized
869  * @action: one of INIT, MODIFY or RESTORE
870  *
871  * Caller should take care of locking. Execute all necessary commands to
872  * initialize a HW endpoint so it can be used by a gadget driver.
873  */
874 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
875 {
876 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
877 	struct dwc3		*dwc = dep->dwc;
878 
879 	u32			reg;
880 	int			ret;
881 
882 	if (!(dep->flags & DWC3_EP_ENABLED)) {
883 		ret = dwc3_gadget_resize_tx_fifos(dep);
884 		if (ret)
885 			return ret;
886 
887 		ret = dwc3_gadget_start_config(dep);
888 		if (ret)
889 			return ret;
890 	}
891 
892 	ret = dwc3_gadget_set_ep_config(dep, action);
893 	if (ret)
894 		return ret;
895 
896 	if (!(dep->flags & DWC3_EP_ENABLED)) {
897 		struct dwc3_trb	*trb_st_hw;
898 		struct dwc3_trb	*trb_link;
899 
900 		dep->type = usb_endpoint_type(desc);
901 		dep->flags |= DWC3_EP_ENABLED;
902 
903 		reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
904 		reg |= DWC3_DALEPENA_EP(dep->number);
905 		dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
906 
907 		dep->trb_dequeue = 0;
908 		dep->trb_enqueue = 0;
909 
910 		if (usb_endpoint_xfer_control(desc))
911 			goto out;
912 
913 		/* Initialize the TRB ring */
914 		memset(dep->trb_pool, 0,
915 		       sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
916 
917 		/* Link TRB. The HWO bit is never reset */
918 		trb_st_hw = &dep->trb_pool[0];
919 
920 		trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
921 		trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
922 		trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
923 		trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
924 		trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
925 	}
926 
927 	/*
928 	 * Issue StartTransfer here with no-op TRB so we can always rely on No
929 	 * Response Update Transfer command.
930 	 */
931 	if (usb_endpoint_xfer_bulk(desc) ||
932 			usb_endpoint_xfer_int(desc)) {
933 		struct dwc3_gadget_ep_cmd_params params;
934 		struct dwc3_trb	*trb;
935 		dma_addr_t trb_dma;
936 		u32 cmd;
937 
938 		memset(&params, 0, sizeof(params));
939 		trb = &dep->trb_pool[0];
940 		trb_dma = dwc3_trb_dma_offset(dep, trb);
941 
942 		params.param0 = upper_32_bits(trb_dma);
943 		params.param1 = lower_32_bits(trb_dma);
944 
945 		cmd = DWC3_DEPCMD_STARTTRANSFER;
946 
947 		ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
948 		if (ret < 0)
949 			return ret;
950 
951 		if (dep->stream_capable) {
952 			/*
953 			 * For streams, at start, there maybe a race where the
954 			 * host primes the endpoint before the function driver
955 			 * queues a request to initiate a stream. In that case,
956 			 * the controller will not see the prime to generate the
957 			 * ERDY and start stream. To workaround this, issue a
958 			 * no-op TRB as normal, but end it immediately. As a
959 			 * result, when the function driver queues the request,
960 			 * the next START_TRANSFER command will cause the
961 			 * controller to generate an ERDY to initiate the
962 			 * stream.
963 			 */
964 			dwc3_stop_active_transfer(dep, true, true);
965 
966 			/*
967 			 * All stream eps will reinitiate stream on NoStream
968 			 * rejection until we can determine that the host can
969 			 * prime after the first transfer.
970 			 *
971 			 * However, if the controller is capable of
972 			 * TXF_FLUSH_BYPASS, then IN direction endpoints will
973 			 * automatically restart the stream without the driver
974 			 * initiation.
975 			 */
976 			if (!dep->direction ||
977 			    !(dwc->hwparams.hwparams9 &
978 			      DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
979 				dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
980 		}
981 	}
982 
983 out:
984 	trace_dwc3_gadget_ep_enable(dep);
985 
986 	return 0;
987 }
988 
989 void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
990 {
991 	struct dwc3_request		*req;
992 
993 	dwc3_stop_active_transfer(dep, true, false);
994 
995 	/* If endxfer is delayed, avoid unmapping requests */
996 	if (dep->flags & DWC3_EP_DELAY_STOP)
997 		return;
998 
999 	/* - giveback all requests to gadget driver */
1000 	while (!list_empty(&dep->started_list)) {
1001 		req = next_request(&dep->started_list);
1002 
1003 		dwc3_gadget_giveback(dep, req, status);
1004 	}
1005 
1006 	while (!list_empty(&dep->pending_list)) {
1007 		req = next_request(&dep->pending_list);
1008 
1009 		dwc3_gadget_giveback(dep, req, status);
1010 	}
1011 
1012 	while (!list_empty(&dep->cancelled_list)) {
1013 		req = next_request(&dep->cancelled_list);
1014 
1015 		dwc3_gadget_giveback(dep, req, status);
1016 	}
1017 }
1018 
1019 /**
1020  * __dwc3_gadget_ep_disable - disables a hw endpoint
1021  * @dep: the endpoint to disable
1022  *
1023  * This function undoes what __dwc3_gadget_ep_enable did and also removes
1024  * requests which are currently being processed by the hardware and those which
1025  * are not yet scheduled.
1026  *
1027  * Caller should take care of locking.
1028  */
1029 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
1030 {
1031 	struct dwc3		*dwc = dep->dwc;
1032 	u32			reg;
1033 	u32			mask;
1034 
1035 	trace_dwc3_gadget_ep_disable(dep);
1036 
1037 	/* make sure HW endpoint isn't stalled */
1038 	if (dep->flags & DWC3_EP_STALL)
1039 		__dwc3_gadget_ep_set_halt(dep, 0, false);
1040 
1041 	reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
1042 	reg &= ~DWC3_DALEPENA_EP(dep->number);
1043 	dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
1044 
1045 	dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
1046 
1047 	dep->stream_capable = false;
1048 	dep->type = 0;
1049 	mask = DWC3_EP_TXFIFO_RESIZED;
1050 	/*
1051 	 * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
1052 	 * set.  Do not clear DEP flags, so that the end transfer command will
1053 	 * be reattempted during the next SETUP stage.
1054 	 */
1055 	if (dep->flags & DWC3_EP_DELAY_STOP)
1056 		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
1057 	dep->flags &= mask;
1058 
1059 	/* Clear out the ep descriptors for non-ep0 */
1060 	if (dep->number > 1) {
1061 		dep->endpoint.comp_desc = NULL;
1062 		dep->endpoint.desc = NULL;
1063 	}
1064 
1065 	return 0;
1066 }
1067 
1068 /* -------------------------------------------------------------------------- */
1069 
1070 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
1071 		const struct usb_endpoint_descriptor *desc)
1072 {
1073 	return -EINVAL;
1074 }
1075 
1076 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
1077 {
1078 	return -EINVAL;
1079 }
1080 
1081 /* -------------------------------------------------------------------------- */
1082 
1083 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
1084 		const struct usb_endpoint_descriptor *desc)
1085 {
1086 	struct dwc3_ep			*dep;
1087 	struct dwc3			*dwc;
1088 	unsigned long			flags;
1089 	int				ret;
1090 
1091 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
1092 		pr_debug("dwc3: invalid parameters\n");
1093 		return -EINVAL;
1094 	}
1095 
1096 	if (!desc->wMaxPacketSize) {
1097 		pr_debug("dwc3: missing wMaxPacketSize\n");
1098 		return -EINVAL;
1099 	}
1100 
1101 	dep = to_dwc3_ep(ep);
1102 	dwc = dep->dwc;
1103 
1104 	if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
1105 					"%s is already enabled\n",
1106 					dep->name))
1107 		return 0;
1108 
1109 	spin_lock_irqsave(&dwc->lock, flags);
1110 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1111 	spin_unlock_irqrestore(&dwc->lock, flags);
1112 
1113 	return ret;
1114 }
1115 
1116 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
1117 {
1118 	struct dwc3_ep			*dep;
1119 	struct dwc3			*dwc;
1120 	unsigned long			flags;
1121 	int				ret;
1122 
1123 	if (!ep) {
1124 		pr_debug("dwc3: invalid parameters\n");
1125 		return -EINVAL;
1126 	}
1127 
1128 	dep = to_dwc3_ep(ep);
1129 	dwc = dep->dwc;
1130 
1131 	if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
1132 					"%s is already disabled\n",
1133 					dep->name))
1134 		return 0;
1135 
1136 	spin_lock_irqsave(&dwc->lock, flags);
1137 	ret = __dwc3_gadget_ep_disable(dep);
1138 	spin_unlock_irqrestore(&dwc->lock, flags);
1139 
1140 	return ret;
1141 }
1142 
1143 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
1144 		gfp_t gfp_flags)
1145 {
1146 	struct dwc3_request		*req;
1147 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
1148 
1149 	req = kzalloc(sizeof(*req), gfp_flags);
1150 	if (!req)
1151 		return NULL;
1152 
1153 	req->direction	= dep->direction;
1154 	req->epnum	= dep->number;
1155 	req->dep	= dep;
1156 	req->status	= DWC3_REQUEST_STATUS_UNKNOWN;
1157 
1158 	trace_dwc3_alloc_request(req);
1159 
1160 	return &req->request;
1161 }
1162 
1163 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1164 		struct usb_request *request)
1165 {
1166 	struct dwc3_request		*req = to_dwc3_request(request);
1167 
1168 	trace_dwc3_free_request(req);
1169 	kfree(req);
1170 }
1171 
1172 /**
1173  * dwc3_ep_prev_trb - returns the previous TRB in the ring
1174  * @dep: The endpoint with the TRB ring
1175  * @index: The index of the current TRB in the ring
1176  *
1177  * Returns the TRB prior to the one pointed to by the index. If the
1178  * index is 0, we will wrap backwards, skip the link TRB, and return
1179  * the one just before that.
1180  */
1181 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1182 {
1183 	u8 tmp = index;
1184 
1185 	if (!tmp)
1186 		tmp = DWC3_TRB_NUM - 1;
1187 
1188 	return &dep->trb_pool[tmp - 1];
1189 }
1190 
1191 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1192 {
1193 	u8			trbs_left;
1194 
1195 	/*
1196 	 * If the enqueue & dequeue are equal then the TRB ring is either full
1197 	 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
1198 	 * pending to be processed by the driver.
1199 	 */
1200 	if (dep->trb_enqueue == dep->trb_dequeue) {
1201 		/*
1202 		 * If there is any request remained in the started_list at
1203 		 * this point, that means there is no TRB available.
1204 		 */
1205 		if (!list_empty(&dep->started_list))
1206 			return 0;
1207 
1208 		return DWC3_TRB_NUM - 1;
1209 	}
1210 
1211 	trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1212 	trbs_left &= (DWC3_TRB_NUM - 1);
1213 
1214 	if (dep->trb_dequeue < dep->trb_enqueue)
1215 		trbs_left--;
1216 
1217 	return trbs_left;
1218 }
1219 
1220 /**
1221  * dwc3_prepare_one_trb - setup one TRB from one request
1222  * @dep: endpoint for which this request is prepared
1223  * @req: dwc3_request pointer
1224  * @trb_length: buffer size of the TRB
1225  * @chain: should this TRB be chained to the next?
1226  * @node: only for isochronous endpoints. First TRB needs different type.
1227  * @use_bounce_buffer: set to use bounce buffer
1228  * @must_interrupt: set to interrupt on TRB completion
1229  */
1230 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1231 		struct dwc3_request *req, unsigned int trb_length,
1232 		unsigned int chain, unsigned int node, bool use_bounce_buffer,
1233 		bool must_interrupt)
1234 {
1235 	struct dwc3_trb		*trb;
1236 	dma_addr_t		dma;
1237 	unsigned int		stream_id = req->request.stream_id;
1238 	unsigned int		short_not_ok = req->request.short_not_ok;
1239 	unsigned int		no_interrupt = req->request.no_interrupt;
1240 	unsigned int		is_last = req->request.is_last;
1241 	struct dwc3		*dwc = dep->dwc;
1242 	struct usb_gadget	*gadget = dwc->gadget;
1243 	enum usb_device_speed	speed = gadget->speed;
1244 
1245 	if (use_bounce_buffer)
1246 		dma = dep->dwc->bounce_addr;
1247 	else if (req->request.num_sgs > 0)
1248 		dma = sg_dma_address(req->start_sg);
1249 	else
1250 		dma = req->request.dma;
1251 
1252 	trb = &dep->trb_pool[dep->trb_enqueue];
1253 
1254 	if (!req->trb) {
1255 		dwc3_gadget_move_started_request(req);
1256 		req->trb = trb;
1257 		req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1258 	}
1259 
1260 	req->num_trbs++;
1261 
1262 	trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
1263 	trb->bpl = lower_32_bits(dma);
1264 	trb->bph = upper_32_bits(dma);
1265 
1266 	switch (usb_endpoint_type(dep->endpoint.desc)) {
1267 	case USB_ENDPOINT_XFER_CONTROL:
1268 		trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
1269 		break;
1270 
1271 	case USB_ENDPOINT_XFER_ISOC:
1272 		if (!node) {
1273 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
1274 
1275 			/*
1276 			 * USB Specification 2.0 Section 5.9.2 states that: "If
1277 			 * there is only a single transaction in the microframe,
1278 			 * only a DATA0 data packet PID is used.  If there are
1279 			 * two transactions per microframe, DATA1 is used for
1280 			 * the first transaction data packet and DATA0 is used
1281 			 * for the second transaction data packet.  If there are
1282 			 * three transactions per microframe, DATA2 is used for
1283 			 * the first transaction data packet, DATA1 is used for
1284 			 * the second, and DATA0 is used for the third."
1285 			 *
1286 			 * IOW, we should satisfy the following cases:
1287 			 *
1288 			 * 1) length <= maxpacket
1289 			 *	- DATA0
1290 			 *
1291 			 * 2) maxpacket < length <= (2 * maxpacket)
1292 			 *	- DATA1, DATA0
1293 			 *
1294 			 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1295 			 *	- DATA2, DATA1, DATA0
1296 			 */
1297 			if (speed == USB_SPEED_HIGH) {
1298 				struct usb_ep *ep = &dep->endpoint;
1299 				unsigned int mult = 2;
1300 				unsigned int maxp = usb_endpoint_maxp(ep->desc);
1301 
1302 				if (req->request.length <= (2 * maxp))
1303 					mult--;
1304 
1305 				if (req->request.length <= maxp)
1306 					mult--;
1307 
1308 				trb->size |= DWC3_TRB_SIZE_PCM1(mult);
1309 			}
1310 		} else {
1311 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
1312 		}
1313 
1314 		if (!no_interrupt && !chain)
1315 			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1316 		break;
1317 
1318 	case USB_ENDPOINT_XFER_BULK:
1319 	case USB_ENDPOINT_XFER_INT:
1320 		trb->ctrl = DWC3_TRBCTL_NORMAL;
1321 		break;
1322 	default:
1323 		/*
1324 		 * This is only possible with faulty memory because we
1325 		 * checked it already :)
1326 		 */
1327 		dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1328 				usb_endpoint_type(dep->endpoint.desc));
1329 	}
1330 
1331 	/*
1332 	 * Enable Continue on Short Packet
1333 	 * when endpoint is not a stream capable
1334 	 */
1335 	if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1336 		if (!dep->stream_capable)
1337 			trb->ctrl |= DWC3_TRB_CTRL_CSP;
1338 
1339 		if (short_not_ok)
1340 			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1341 	}
1342 
1343 	/* All TRBs setup for MST must set CSP=1 when LST=0 */
1344 	if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams))
1345 		trb->ctrl |= DWC3_TRB_CTRL_CSP;
1346 
1347 	if ((!no_interrupt && !chain) || must_interrupt)
1348 		trb->ctrl |= DWC3_TRB_CTRL_IOC;
1349 
1350 	if (chain)
1351 		trb->ctrl |= DWC3_TRB_CTRL_CHN;
1352 	else if (dep->stream_capable && is_last &&
1353 		 !DWC3_MST_CAPABLE(&dwc->hwparams))
1354 		trb->ctrl |= DWC3_TRB_CTRL_LST;
1355 
1356 	if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1357 		trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1358 
1359 	/*
1360 	 * As per data book 4.2.3.2TRB Control Bit Rules section
1361 	 *
1362 	 * The controller autonomously checks the HWO field of a TRB to determine if the
1363 	 * entire TRB is valid. Therefore, software must ensure that the rest of the TRB
1364 	 * is valid before setting the HWO field to '1'. In most systems, this means that
1365 	 * software must update the fourth DWORD of a TRB last.
1366 	 *
1367 	 * However there is a possibility of CPU re-ordering here which can cause
1368 	 * controller to observe the HWO bit set prematurely.
1369 	 * Add a write memory barrier to prevent CPU re-ordering.
1370 	 */
1371 	wmb();
1372 	trb->ctrl |= DWC3_TRB_CTRL_HWO;
1373 
1374 	dwc3_ep_inc_enq(dep);
1375 
1376 	trace_dwc3_prepare_trb(dep, trb);
1377 }
1378 
1379 static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1380 {
1381 	unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1382 	unsigned int rem = req->request.length % maxp;
1383 
1384 	if ((req->request.length && req->request.zero && !rem &&
1385 			!usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1386 			(!req->direction && rem))
1387 		return true;
1388 
1389 	return false;
1390 }
1391 
1392 /**
1393  * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1394  * @dep: The endpoint that the request belongs to
1395  * @req: The request to prepare
1396  * @entry_length: The last SG entry size
1397  * @node: Indicates whether this is not the first entry (for isoc only)
1398  *
1399  * Return the number of TRBs prepared.
1400  */
1401 static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1402 		struct dwc3_request *req, unsigned int entry_length,
1403 		unsigned int node)
1404 {
1405 	unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1406 	unsigned int rem = req->request.length % maxp;
1407 	unsigned int num_trbs = 1;
1408 
1409 	if (dwc3_needs_extra_trb(dep, req))
1410 		num_trbs++;
1411 
1412 	if (dwc3_calc_trbs_left(dep) < num_trbs)
1413 		return 0;
1414 
1415 	req->needs_extra_trb = num_trbs > 1;
1416 
1417 	/* Prepare a normal TRB */
1418 	if (req->direction || req->request.length)
1419 		dwc3_prepare_one_trb(dep, req, entry_length,
1420 				req->needs_extra_trb, node, false, false);
1421 
1422 	/* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1423 	if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1424 		dwc3_prepare_one_trb(dep, req,
1425 				req->direction ? 0 : maxp - rem,
1426 				false, 1, true, false);
1427 
1428 	return num_trbs;
1429 }
1430 
1431 static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
1432 		struct dwc3_request *req)
1433 {
1434 	struct scatterlist *sg = req->start_sg;
1435 	struct scatterlist *s;
1436 	int		i;
1437 	unsigned int length = req->request.length;
1438 	unsigned int remaining = req->request.num_mapped_sgs
1439 		- req->num_queued_sgs;
1440 	unsigned int num_trbs = req->num_trbs;
1441 	bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
1442 
1443 	/*
1444 	 * If we resume preparing the request, then get the remaining length of
1445 	 * the request and resume where we left off.
1446 	 */
1447 	for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1448 		length -= sg_dma_len(s);
1449 
1450 	for_each_sg(sg, s, remaining, i) {
1451 		unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
1452 		unsigned int trb_length;
1453 		bool must_interrupt = false;
1454 		bool last_sg = false;
1455 
1456 		trb_length = min_t(unsigned int, length, sg_dma_len(s));
1457 
1458 		length -= trb_length;
1459 
1460 		/*
1461 		 * IOMMU driver is coalescing the list of sgs which shares a
1462 		 * page boundary into one and giving it to USB driver. With
1463 		 * this the number of sgs mapped is not equal to the number of
1464 		 * sgs passed. So mark the chain bit to false if it isthe last
1465 		 * mapped sg.
1466 		 */
1467 		if ((i == remaining - 1) || !length)
1468 			last_sg = true;
1469 
1470 		if (!num_trbs_left)
1471 			break;
1472 
1473 		if (last_sg) {
1474 			if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1475 				break;
1476 		} else {
1477 			/*
1478 			 * Look ahead to check if we have enough TRBs for the
1479 			 * next SG entry. If not, set interrupt on this TRB to
1480 			 * resume preparing the next SG entry when more TRBs are
1481 			 * free.
1482 			 */
1483 			if (num_trbs_left == 1 || (needs_extra_trb &&
1484 					num_trbs_left <= 2 &&
1485 					sg_dma_len(sg_next(s)) >= length)) {
1486 				struct dwc3_request *r;
1487 
1488 				/* Check if previous requests already set IOC */
1489 				list_for_each_entry(r, &dep->started_list, list) {
1490 					if (r != req && !r->request.no_interrupt)
1491 						break;
1492 
1493 					if (r == req)
1494 						must_interrupt = true;
1495 				}
1496 			}
1497 
1498 			dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1499 					must_interrupt);
1500 		}
1501 
1502 		/*
1503 		 * There can be a situation where all sgs in sglist are not
1504 		 * queued because of insufficient trb number. To handle this
1505 		 * case, update start_sg to next sg to be queued, so that
1506 		 * we have free trbs we can continue queuing from where we
1507 		 * previously stopped
1508 		 */
1509 		if (!last_sg)
1510 			req->start_sg = sg_next(s);
1511 
1512 		req->num_queued_sgs++;
1513 		req->num_pending_sgs--;
1514 
1515 		/*
1516 		 * The number of pending SG entries may not correspond to the
1517 		 * number of mapped SG entries. If all the data are queued, then
1518 		 * don't include unused SG entries.
1519 		 */
1520 		if (length == 0) {
1521 			req->num_pending_sgs = 0;
1522 			break;
1523 		}
1524 
1525 		if (must_interrupt)
1526 			break;
1527 	}
1528 
1529 	return req->num_trbs - num_trbs;
1530 }
1531 
1532 static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
1533 		struct dwc3_request *req)
1534 {
1535 	return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
1536 }
1537 
1538 /*
1539  * dwc3_prepare_trbs - setup TRBs from requests
1540  * @dep: endpoint for which requests are being prepared
1541  *
1542  * The function goes through the requests list and sets up TRBs for the
1543  * transfers. The function returns once there are no more TRBs available or
1544  * it runs out of requests.
1545  *
1546  * Returns the number of TRBs prepared or negative errno.
1547  */
1548 static int dwc3_prepare_trbs(struct dwc3_ep *dep)
1549 {
1550 	struct dwc3_request	*req, *n;
1551 	int			ret = 0;
1552 
1553 	BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1554 
1555 	/*
1556 	 * We can get in a situation where there's a request in the started list
1557 	 * but there weren't enough TRBs to fully kick it in the first time
1558 	 * around, so it has been waiting for more TRBs to be freed up.
1559 	 *
1560 	 * In that case, we should check if we have a request with pending_sgs
1561 	 * in the started list and prepare TRBs for that request first,
1562 	 * otherwise we will prepare TRBs completely out of order and that will
1563 	 * break things.
1564 	 */
1565 	list_for_each_entry(req, &dep->started_list, list) {
1566 		if (req->num_pending_sgs > 0) {
1567 			ret = dwc3_prepare_trbs_sg(dep, req);
1568 			if (!ret || req->num_pending_sgs)
1569 				return ret;
1570 		}
1571 
1572 		if (!dwc3_calc_trbs_left(dep))
1573 			return ret;
1574 
1575 		/*
1576 		 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1577 		 * burst capability may try to read and use TRBs beyond the
1578 		 * active transfer instead of stopping.
1579 		 */
1580 		if (dep->stream_capable && req->request.is_last &&
1581 		    !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
1582 			return ret;
1583 	}
1584 
1585 	list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1586 		struct dwc3	*dwc = dep->dwc;
1587 
1588 		ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1589 						    dep->direction);
1590 		if (ret)
1591 			return ret;
1592 
1593 		req->sg			= req->request.sg;
1594 		req->start_sg		= req->sg;
1595 		req->num_queued_sgs	= 0;
1596 		req->num_pending_sgs	= req->request.num_mapped_sgs;
1597 
1598 		if (req->num_pending_sgs > 0) {
1599 			ret = dwc3_prepare_trbs_sg(dep, req);
1600 			if (req->num_pending_sgs)
1601 				return ret;
1602 		} else {
1603 			ret = dwc3_prepare_trbs_linear(dep, req);
1604 		}
1605 
1606 		if (!ret || !dwc3_calc_trbs_left(dep))
1607 			return ret;
1608 
1609 		/*
1610 		 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1611 		 * burst capability may try to read and use TRBs beyond the
1612 		 * active transfer instead of stopping.
1613 		 */
1614 		if (dep->stream_capable && req->request.is_last &&
1615 		    !DWC3_MST_CAPABLE(&dwc->hwparams))
1616 			return ret;
1617 	}
1618 
1619 	return ret;
1620 }
1621 
1622 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1623 
1624 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1625 {
1626 	struct dwc3_gadget_ep_cmd_params params;
1627 	struct dwc3_request		*req;
1628 	int				starting;
1629 	int				ret;
1630 	u32				cmd;
1631 
1632 	/*
1633 	 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1634 	 * This happens when we need to stop and restart a transfer such as in
1635 	 * the case of reinitiating a stream or retrying an isoc transfer.
1636 	 */
1637 	ret = dwc3_prepare_trbs(dep);
1638 	if (ret < 0)
1639 		return ret;
1640 
1641 	starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1642 
1643 	/*
1644 	 * If there's no new TRB prepared and we don't need to restart a
1645 	 * transfer, there's no need to update the transfer.
1646 	 */
1647 	if (!ret && !starting)
1648 		return ret;
1649 
1650 	req = next_request(&dep->started_list);
1651 	if (!req) {
1652 		dep->flags |= DWC3_EP_PENDING_REQUEST;
1653 		return 0;
1654 	}
1655 
1656 	memset(&params, 0, sizeof(params));
1657 
1658 	if (starting) {
1659 		params.param0 = upper_32_bits(req->trb_dma);
1660 		params.param1 = lower_32_bits(req->trb_dma);
1661 		cmd = DWC3_DEPCMD_STARTTRANSFER;
1662 
1663 		if (dep->stream_capable)
1664 			cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1665 
1666 		if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1667 			cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1668 	} else {
1669 		cmd = DWC3_DEPCMD_UPDATETRANSFER |
1670 			DWC3_DEPCMD_PARAM(dep->resource_index);
1671 	}
1672 
1673 	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1674 	if (ret < 0) {
1675 		struct dwc3_request *tmp;
1676 
1677 		if (ret == -EAGAIN)
1678 			return ret;
1679 
1680 		dwc3_stop_active_transfer(dep, true, true);
1681 
1682 		list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1683 			dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
1684 
1685 		/* If ep isn't started, then there's no end transfer pending */
1686 		if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1687 			dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1688 
1689 		return ret;
1690 	}
1691 
1692 	if (dep->stream_capable && req->request.is_last &&
1693 	    !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
1694 		dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1695 
1696 	return 0;
1697 }
1698 
1699 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1700 {
1701 	u32			reg;
1702 
1703 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1704 	return DWC3_DSTS_SOFFN(reg);
1705 }
1706 
1707 /**
1708  * __dwc3_stop_active_transfer - stop the current active transfer
1709  * @dep: isoc endpoint
1710  * @force: set forcerm bit in the command
1711  * @interrupt: command complete interrupt after End Transfer command
1712  *
1713  * When setting force, the ForceRM bit will be set. In that case
1714  * the controller won't update the TRB progress on command
1715  * completion. It also won't clear the HWO bit in the TRB.
1716  * The command will also not complete immediately in that case.
1717  */
1718 static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1719 {
1720 	struct dwc3 *dwc = dep->dwc;
1721 	struct dwc3_gadget_ep_cmd_params params;
1722 	u32 cmd;
1723 	int ret;
1724 
1725 	cmd = DWC3_DEPCMD_ENDTRANSFER;
1726 	cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1727 	cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1728 	cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1729 	memset(&params, 0, sizeof(params));
1730 	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1731 	/*
1732 	 * If the End Transfer command was timed out while the device is
1733 	 * not in SETUP phase, it's possible that an incoming Setup packet
1734 	 * may prevent the command's completion. Let's retry when the
1735 	 * ep0state returns to EP0_SETUP_PHASE.
1736 	 */
1737 	if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
1738 		dep->flags |= DWC3_EP_DELAY_STOP;
1739 		return 0;
1740 	}
1741 	WARN_ON_ONCE(ret);
1742 	dep->resource_index = 0;
1743 
1744 	if (!interrupt) {
1745 		if (!DWC3_IP_IS(DWC3) || DWC3_VER_IS_PRIOR(DWC3, 310A))
1746 			mdelay(1);
1747 		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1748 	} else if (!ret) {
1749 		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1750 	}
1751 
1752 	dep->flags &= ~DWC3_EP_DELAY_STOP;
1753 	return ret;
1754 }
1755 
1756 /**
1757  * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1758  * @dep: isoc endpoint
1759  *
1760  * This function tests for the correct combination of BIT[15:14] from the 16-bit
1761  * microframe number reported by the XferNotReady event for the future frame
1762  * number to start the isoc transfer.
1763  *
1764  * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1765  * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1766  * XferNotReady event are invalid. The driver uses this number to schedule the
1767  * isochronous transfer and passes it to the START TRANSFER command. Because
1768  * this number is invalid, the command may fail. If BIT[15:14] matches the
1769  * internal 16-bit microframe, the START TRANSFER command will pass and the
1770  * transfer will start at the scheduled time, if it is off by 1, the command
1771  * will still pass, but the transfer will start 2 seconds in the future. For all
1772  * other conditions, the START TRANSFER command will fail with bus-expiry.
1773  *
1774  * In order to workaround this issue, we can test for the correct combination of
1775  * BIT[15:14] by sending START TRANSFER commands with different values of
1776  * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1777  * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1778  * As the result, within the 4 possible combinations for BIT[15:14], there will
1779  * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1780  * command status will result in a 2-second delay start. The smaller BIT[15:14]
1781  * value is the correct combination.
1782  *
1783  * Since there are only 4 outcomes and the results are ordered, we can simply
1784  * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1785  * deduce the smaller successful combination.
1786  *
1787  * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1788  * of BIT[15:14]. The correct combination is as follow:
1789  *
1790  * if test0 fails and test1 passes, BIT[15:14] is 'b01
1791  * if test0 fails and test1 fails, BIT[15:14] is 'b10
1792  * if test0 passes and test1 fails, BIT[15:14] is 'b11
1793  * if test0 passes and test1 passes, BIT[15:14] is 'b00
1794  *
1795  * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1796  * endpoints.
1797  */
1798 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1799 {
1800 	int cmd_status = 0;
1801 	bool test0;
1802 	bool test1;
1803 
1804 	while (dep->combo_num < 2) {
1805 		struct dwc3_gadget_ep_cmd_params params;
1806 		u32 test_frame_number;
1807 		u32 cmd;
1808 
1809 		/*
1810 		 * Check if we can start isoc transfer on the next interval or
1811 		 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1812 		 */
1813 		test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
1814 		test_frame_number |= dep->combo_num << 14;
1815 		test_frame_number += max_t(u32, 4, dep->interval);
1816 
1817 		params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1818 		params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1819 
1820 		cmd = DWC3_DEPCMD_STARTTRANSFER;
1821 		cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1822 		cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1823 
1824 		/* Redo if some other failure beside bus-expiry is received */
1825 		if (cmd_status && cmd_status != -EAGAIN) {
1826 			dep->start_cmd_status = 0;
1827 			dep->combo_num = 0;
1828 			return 0;
1829 		}
1830 
1831 		/* Store the first test status */
1832 		if (dep->combo_num == 0)
1833 			dep->start_cmd_status = cmd_status;
1834 
1835 		dep->combo_num++;
1836 
1837 		/*
1838 		 * End the transfer if the START_TRANSFER command is successful
1839 		 * to wait for the next XferNotReady to test the command again
1840 		 */
1841 		if (cmd_status == 0) {
1842 			dwc3_stop_active_transfer(dep, true, true);
1843 			return 0;
1844 		}
1845 	}
1846 
1847 	/* test0 and test1 are both completed at this point */
1848 	test0 = (dep->start_cmd_status == 0);
1849 	test1 = (cmd_status == 0);
1850 
1851 	if (!test0 && test1)
1852 		dep->combo_num = 1;
1853 	else if (!test0 && !test1)
1854 		dep->combo_num = 2;
1855 	else if (test0 && !test1)
1856 		dep->combo_num = 3;
1857 	else if (test0 && test1)
1858 		dep->combo_num = 0;
1859 
1860 	dep->frame_number &= DWC3_FRNUMBER_MASK;
1861 	dep->frame_number |= dep->combo_num << 14;
1862 	dep->frame_number += max_t(u32, 4, dep->interval);
1863 
1864 	/* Reinitialize test variables */
1865 	dep->start_cmd_status = 0;
1866 	dep->combo_num = 0;
1867 
1868 	return __dwc3_gadget_kick_transfer(dep);
1869 }
1870 
1871 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1872 {
1873 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1874 	struct dwc3 *dwc = dep->dwc;
1875 	int ret;
1876 	int i;
1877 
1878 	if (list_empty(&dep->pending_list) &&
1879 	    list_empty(&dep->started_list)) {
1880 		dep->flags |= DWC3_EP_PENDING_REQUEST;
1881 		return -EAGAIN;
1882 	}
1883 
1884 	if (!dwc->dis_start_transfer_quirk &&
1885 	    (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1886 	     DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1887 		if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
1888 			return dwc3_gadget_start_isoc_quirk(dep);
1889 	}
1890 
1891 	if (desc->bInterval <= 14 &&
1892 	    dwc->gadget->speed >= USB_SPEED_HIGH) {
1893 		u32 frame = __dwc3_gadget_get_frame(dwc);
1894 		bool rollover = frame <
1895 				(dep->frame_number & DWC3_FRNUMBER_MASK);
1896 
1897 		/*
1898 		 * frame_number is set from XferNotReady and may be already
1899 		 * out of date. DSTS only provides the lower 14 bit of the
1900 		 * current frame number. So add the upper two bits of
1901 		 * frame_number and handle a possible rollover.
1902 		 * This will provide the correct frame_number unless more than
1903 		 * rollover has happened since XferNotReady.
1904 		 */
1905 
1906 		dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1907 				     frame;
1908 		if (rollover)
1909 			dep->frame_number += BIT(14);
1910 	}
1911 
1912 	for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1913 		int future_interval = i + 1;
1914 
1915 		/* Give the controller at least 500us to schedule transfers */
1916 		if (desc->bInterval < 3)
1917 			future_interval += 3 - desc->bInterval;
1918 
1919 		dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
1920 
1921 		ret = __dwc3_gadget_kick_transfer(dep);
1922 		if (ret != -EAGAIN)
1923 			break;
1924 	}
1925 
1926 	/*
1927 	 * After a number of unsuccessful start attempts due to bus-expiry
1928 	 * status, issue END_TRANSFER command and retry on the next XferNotReady
1929 	 * event.
1930 	 */
1931 	if (ret == -EAGAIN)
1932 		ret = __dwc3_stop_active_transfer(dep, false, true);
1933 
1934 	return ret;
1935 }
1936 
1937 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1938 {
1939 	struct dwc3		*dwc = dep->dwc;
1940 
1941 	if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
1942 		dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
1943 				dep->name);
1944 		return -ESHUTDOWN;
1945 	}
1946 
1947 	if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1948 				&req->request, req->dep->name))
1949 		return -EINVAL;
1950 
1951 	if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1952 				"%s: request %pK already in flight\n",
1953 				dep->name, &req->request))
1954 		return -EINVAL;
1955 
1956 	pm_runtime_get(dwc->dev);
1957 
1958 	req->request.actual	= 0;
1959 	req->request.status	= -EINPROGRESS;
1960 
1961 	trace_dwc3_ep_queue(req);
1962 
1963 	list_add_tail(&req->list, &dep->pending_list);
1964 	req->status = DWC3_REQUEST_STATUS_QUEUED;
1965 
1966 	if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1967 		return 0;
1968 
1969 	/*
1970 	 * Start the transfer only after the END_TRANSFER is completed
1971 	 * and endpoint STALL is cleared.
1972 	 */
1973 	if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1974 	    (dep->flags & DWC3_EP_WEDGE) ||
1975 	    (dep->flags & DWC3_EP_DELAY_STOP) ||
1976 	    (dep->flags & DWC3_EP_STALL)) {
1977 		dep->flags |= DWC3_EP_DELAY_START;
1978 		return 0;
1979 	}
1980 
1981 	/*
1982 	 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1983 	 * wait for a XferNotReady event so we will know what's the current
1984 	 * (micro-)frame number.
1985 	 *
1986 	 * Without this trick, we are very, very likely gonna get Bus Expiry
1987 	 * errors which will force us issue EndTransfer command.
1988 	 */
1989 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1990 		if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1991 			if ((dep->flags & DWC3_EP_PENDING_REQUEST))
1992 				return __dwc3_gadget_start_isoc(dep);
1993 
1994 			return 0;
1995 		}
1996 	}
1997 
1998 	__dwc3_gadget_kick_transfer(dep);
1999 
2000 	return 0;
2001 }
2002 
2003 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2004 	gfp_t gfp_flags)
2005 {
2006 	struct dwc3_request		*req = to_dwc3_request(request);
2007 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
2008 	struct dwc3			*dwc = dep->dwc;
2009 
2010 	unsigned long			flags;
2011 
2012 	int				ret;
2013 
2014 	spin_lock_irqsave(&dwc->lock, flags);
2015 	ret = __dwc3_gadget_ep_queue(dep, req);
2016 	spin_unlock_irqrestore(&dwc->lock, flags);
2017 
2018 	return ret;
2019 }
2020 
2021 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
2022 {
2023 	int i;
2024 
2025 	/* If req->trb is not set, then the request has not started */
2026 	if (!req->trb)
2027 		return;
2028 
2029 	/*
2030 	 * If request was already started, this means we had to
2031 	 * stop the transfer. With that we also need to ignore
2032 	 * all TRBs used by the request, however TRBs can only
2033 	 * be modified after completion of END_TRANSFER
2034 	 * command. So what we do here is that we wait for
2035 	 * END_TRANSFER completion and only after that, we jump
2036 	 * over TRBs by clearing HWO and incrementing dequeue
2037 	 * pointer.
2038 	 */
2039 	for (i = 0; i < req->num_trbs; i++) {
2040 		struct dwc3_trb *trb;
2041 
2042 		trb = &dep->trb_pool[dep->trb_dequeue];
2043 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2044 		dwc3_ep_inc_deq(dep);
2045 	}
2046 
2047 	req->num_trbs = 0;
2048 }
2049 
2050 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
2051 {
2052 	struct dwc3_request		*req;
2053 	struct dwc3			*dwc = dep->dwc;
2054 
2055 	while (!list_empty(&dep->cancelled_list)) {
2056 		req = next_request(&dep->cancelled_list);
2057 		dwc3_gadget_ep_skip_trbs(dep, req);
2058 		switch (req->status) {
2059 		case DWC3_REQUEST_STATUS_DISCONNECTED:
2060 			dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
2061 			break;
2062 		case DWC3_REQUEST_STATUS_DEQUEUED:
2063 			dwc3_gadget_giveback(dep, req, -ECONNRESET);
2064 			break;
2065 		case DWC3_REQUEST_STATUS_STALLED:
2066 			dwc3_gadget_giveback(dep, req, -EPIPE);
2067 			break;
2068 		default:
2069 			dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
2070 			dwc3_gadget_giveback(dep, req, -ECONNRESET);
2071 			break;
2072 		}
2073 		/*
2074 		 * The endpoint is disabled, let the dwc3_remove_requests()
2075 		 * handle the cleanup.
2076 		 */
2077 		if (!dep->endpoint.desc)
2078 			break;
2079 	}
2080 }
2081 
2082 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
2083 		struct usb_request *request)
2084 {
2085 	struct dwc3_request		*req = to_dwc3_request(request);
2086 	struct dwc3_request		*r = NULL;
2087 
2088 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
2089 	struct dwc3			*dwc = dep->dwc;
2090 
2091 	unsigned long			flags;
2092 	int				ret = 0;
2093 
2094 	trace_dwc3_ep_dequeue(req);
2095 
2096 	spin_lock_irqsave(&dwc->lock, flags);
2097 
2098 	list_for_each_entry(r, &dep->cancelled_list, list) {
2099 		if (r == req)
2100 			goto out;
2101 	}
2102 
2103 	list_for_each_entry(r, &dep->pending_list, list) {
2104 		if (r == req) {
2105 			dwc3_gadget_giveback(dep, req, -ECONNRESET);
2106 			goto out;
2107 		}
2108 	}
2109 
2110 	list_for_each_entry(r, &dep->started_list, list) {
2111 		if (r == req) {
2112 			struct dwc3_request *t;
2113 
2114 			/* wait until it is processed */
2115 			dwc3_stop_active_transfer(dep, true, true);
2116 
2117 			/*
2118 			 * Remove any started request if the transfer is
2119 			 * cancelled.
2120 			 */
2121 			list_for_each_entry_safe(r, t, &dep->started_list, list)
2122 				dwc3_gadget_move_cancelled_request(r,
2123 						DWC3_REQUEST_STATUS_DEQUEUED);
2124 
2125 			dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2126 
2127 			goto out;
2128 		}
2129 	}
2130 
2131 	dev_err(dwc->dev, "request %pK was not queued to %s\n",
2132 		request, ep->name);
2133 	ret = -EINVAL;
2134 out:
2135 	spin_unlock_irqrestore(&dwc->lock, flags);
2136 
2137 	return ret;
2138 }
2139 
2140 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
2141 {
2142 	struct dwc3_gadget_ep_cmd_params	params;
2143 	struct dwc3				*dwc = dep->dwc;
2144 	struct dwc3_request			*req;
2145 	struct dwc3_request			*tmp;
2146 	int					ret;
2147 
2148 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2149 		dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
2150 		return -EINVAL;
2151 	}
2152 
2153 	memset(&params, 0x00, sizeof(params));
2154 
2155 	if (value) {
2156 		struct dwc3_trb *trb;
2157 
2158 		unsigned int transfer_in_flight;
2159 		unsigned int started;
2160 
2161 		if (dep->number > 1)
2162 			trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
2163 		else
2164 			trb = &dwc->ep0_trb[dep->trb_enqueue];
2165 
2166 		transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
2167 		started = !list_empty(&dep->started_list);
2168 
2169 		if (!protocol && ((dep->direction && transfer_in_flight) ||
2170 				(!dep->direction && started))) {
2171 			return -EAGAIN;
2172 		}
2173 
2174 		ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
2175 				&params);
2176 		if (ret)
2177 			dev_err(dwc->dev, "failed to set STALL on %s\n",
2178 					dep->name);
2179 		else
2180 			dep->flags |= DWC3_EP_STALL;
2181 	} else {
2182 		/*
2183 		 * Don't issue CLEAR_STALL command to control endpoints. The
2184 		 * controller automatically clears the STALL when it receives
2185 		 * the SETUP token.
2186 		 */
2187 		if (dep->number <= 1) {
2188 			dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2189 			return 0;
2190 		}
2191 
2192 		dwc3_stop_active_transfer(dep, true, true);
2193 
2194 		list_for_each_entry_safe(req, tmp, &dep->started_list, list)
2195 			dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
2196 
2197 		if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
2198 		    (dep->flags & DWC3_EP_DELAY_STOP)) {
2199 			dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
2200 			if (protocol)
2201 				dwc->clear_stall_protocol = dep->number;
2202 
2203 			return 0;
2204 		}
2205 
2206 		dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2207 
2208 		ret = dwc3_send_clear_stall_ep_cmd(dep);
2209 		if (ret) {
2210 			dev_err(dwc->dev, "failed to clear STALL on %s\n",
2211 					dep->name);
2212 			return ret;
2213 		}
2214 
2215 		dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2216 
2217 		if ((dep->flags & DWC3_EP_DELAY_START) &&
2218 		    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2219 			__dwc3_gadget_kick_transfer(dep);
2220 
2221 		dep->flags &= ~DWC3_EP_DELAY_START;
2222 	}
2223 
2224 	return ret;
2225 }
2226 
2227 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2228 {
2229 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
2230 	struct dwc3			*dwc = dep->dwc;
2231 
2232 	unsigned long			flags;
2233 
2234 	int				ret;
2235 
2236 	spin_lock_irqsave(&dwc->lock, flags);
2237 	ret = __dwc3_gadget_ep_set_halt(dep, value, false);
2238 	spin_unlock_irqrestore(&dwc->lock, flags);
2239 
2240 	return ret;
2241 }
2242 
2243 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
2244 {
2245 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
2246 	struct dwc3			*dwc = dep->dwc;
2247 	unsigned long			flags;
2248 	int				ret;
2249 
2250 	spin_lock_irqsave(&dwc->lock, flags);
2251 	dep->flags |= DWC3_EP_WEDGE;
2252 
2253 	if (dep->number == 0 || dep->number == 1)
2254 		ret = __dwc3_gadget_ep0_set_halt(ep, 1);
2255 	else
2256 		ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
2257 	spin_unlock_irqrestore(&dwc->lock, flags);
2258 
2259 	return ret;
2260 }
2261 
2262 /* -------------------------------------------------------------------------- */
2263 
2264 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
2265 	.bLength	= USB_DT_ENDPOINT_SIZE,
2266 	.bDescriptorType = USB_DT_ENDPOINT,
2267 	.bmAttributes	= USB_ENDPOINT_XFER_CONTROL,
2268 };
2269 
2270 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
2271 	.enable		= dwc3_gadget_ep0_enable,
2272 	.disable	= dwc3_gadget_ep0_disable,
2273 	.alloc_request	= dwc3_gadget_ep_alloc_request,
2274 	.free_request	= dwc3_gadget_ep_free_request,
2275 	.queue		= dwc3_gadget_ep0_queue,
2276 	.dequeue	= dwc3_gadget_ep_dequeue,
2277 	.set_halt	= dwc3_gadget_ep0_set_halt,
2278 	.set_wedge	= dwc3_gadget_ep_set_wedge,
2279 };
2280 
2281 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
2282 	.enable		= dwc3_gadget_ep_enable,
2283 	.disable	= dwc3_gadget_ep_disable,
2284 	.alloc_request	= dwc3_gadget_ep_alloc_request,
2285 	.free_request	= dwc3_gadget_ep_free_request,
2286 	.queue		= dwc3_gadget_ep_queue,
2287 	.dequeue	= dwc3_gadget_ep_dequeue,
2288 	.set_halt	= dwc3_gadget_ep_set_halt,
2289 	.set_wedge	= dwc3_gadget_ep_set_wedge,
2290 };
2291 
2292 /* -------------------------------------------------------------------------- */
2293 
2294 static void dwc3_gadget_enable_linksts_evts(struct dwc3 *dwc, bool set)
2295 {
2296 	u32 reg;
2297 
2298 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2299 		return;
2300 
2301 	reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2302 	if (set)
2303 		reg |= DWC3_DEVTEN_ULSTCNGEN;
2304 	else
2305 		reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2306 
2307 	dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2308 }
2309 
2310 static int dwc3_gadget_get_frame(struct usb_gadget *g)
2311 {
2312 	struct dwc3		*dwc = gadget_to_dwc(g);
2313 
2314 	return __dwc3_gadget_get_frame(dwc);
2315 }
2316 
2317 static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async)
2318 {
2319 	int			retries;
2320 
2321 	int			ret;
2322 	u32			reg;
2323 
2324 	u8			link_state;
2325 
2326 	/*
2327 	 * According to the Databook Remote wakeup request should
2328 	 * be issued only when the device is in early suspend state.
2329 	 *
2330 	 * We can check that via USB Link State bits in DSTS register.
2331 	 */
2332 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2333 
2334 	link_state = DWC3_DSTS_USBLNKST(reg);
2335 
2336 	switch (link_state) {
2337 	case DWC3_LINK_STATE_RESET:
2338 	case DWC3_LINK_STATE_RX_DET:	/* in HS, means Early Suspend */
2339 	case DWC3_LINK_STATE_U3:	/* in HS, means SUSPEND */
2340 	case DWC3_LINK_STATE_U2:	/* in HS, means Sleep (L1) */
2341 	case DWC3_LINK_STATE_U1:
2342 	case DWC3_LINK_STATE_RESUME:
2343 		break;
2344 	default:
2345 		return -EINVAL;
2346 	}
2347 
2348 	if (async)
2349 		dwc3_gadget_enable_linksts_evts(dwc, true);
2350 
2351 	ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2352 	if (ret < 0) {
2353 		dev_err(dwc->dev, "failed to put link in Recovery\n");
2354 		dwc3_gadget_enable_linksts_evts(dwc, false);
2355 		return ret;
2356 	}
2357 
2358 	/* Recent versions do this automatically */
2359 	if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
2360 		/* write zeroes to Link Change Request */
2361 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2362 		reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2363 		dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2364 	}
2365 
2366 	/*
2367 	 * Since link status change events are enabled we will receive
2368 	 * an U0 event when wakeup is successful. So bail out.
2369 	 */
2370 	if (async)
2371 		return 0;
2372 
2373 	/* poll until Link State changes to ON */
2374 	retries = 20000;
2375 
2376 	while (retries--) {
2377 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2378 
2379 		/* in HS, means ON */
2380 		if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2381 			break;
2382 	}
2383 
2384 	if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2385 		dev_err(dwc->dev, "failed to send remote wakeup\n");
2386 		return -EINVAL;
2387 	}
2388 
2389 	return 0;
2390 }
2391 
2392 static int dwc3_gadget_wakeup(struct usb_gadget *g)
2393 {
2394 	struct dwc3		*dwc = gadget_to_dwc(g);
2395 	unsigned long		flags;
2396 	int			ret;
2397 
2398 	if (!dwc->wakeup_configured) {
2399 		dev_err(dwc->dev, "remote wakeup not configured\n");
2400 		return -EINVAL;
2401 	}
2402 
2403 	spin_lock_irqsave(&dwc->lock, flags);
2404 	if (!dwc->gadget->wakeup_armed) {
2405 		dev_err(dwc->dev, "not armed for remote wakeup\n");
2406 		spin_unlock_irqrestore(&dwc->lock, flags);
2407 		return -EINVAL;
2408 	}
2409 	ret = __dwc3_gadget_wakeup(dwc, true);
2410 
2411 	spin_unlock_irqrestore(&dwc->lock, flags);
2412 
2413 	return ret;
2414 }
2415 
2416 static void dwc3_resume_gadget(struct dwc3 *dwc);
2417 
2418 static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
2419 {
2420 	struct  dwc3		*dwc = gadget_to_dwc(g);
2421 	unsigned long		flags;
2422 	int			ret;
2423 	int			link_state;
2424 
2425 	if (!dwc->wakeup_configured) {
2426 		dev_err(dwc->dev, "remote wakeup not configured\n");
2427 		return -EINVAL;
2428 	}
2429 
2430 	spin_lock_irqsave(&dwc->lock, flags);
2431 	/*
2432 	 * If the link is in U3, signal for remote wakeup and wait for the
2433 	 * link to transition to U0 before sending device notification.
2434 	 */
2435 	link_state = dwc3_gadget_get_link_state(dwc);
2436 	if (link_state == DWC3_LINK_STATE_U3) {
2437 		ret = __dwc3_gadget_wakeup(dwc, false);
2438 		if (ret) {
2439 			spin_unlock_irqrestore(&dwc->lock, flags);
2440 			return -EINVAL;
2441 		}
2442 		dwc3_resume_gadget(dwc);
2443 		dwc->suspended = false;
2444 		dwc->link_state = DWC3_LINK_STATE_U0;
2445 	}
2446 
2447 	ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION,
2448 					       DWC3_DGCMDPAR_DN_FUNC_WAKE |
2449 					       DWC3_DGCMDPAR_INTF_SEL(intf_id));
2450 	if (ret)
2451 		dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
2452 
2453 	spin_unlock_irqrestore(&dwc->lock, flags);
2454 
2455 	return ret;
2456 }
2457 
2458 static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set)
2459 {
2460 	struct dwc3		*dwc = gadget_to_dwc(g);
2461 	unsigned long		flags;
2462 
2463 	spin_lock_irqsave(&dwc->lock, flags);
2464 	dwc->wakeup_configured = !!set;
2465 	spin_unlock_irqrestore(&dwc->lock, flags);
2466 
2467 	return 0;
2468 }
2469 
2470 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2471 		int is_selfpowered)
2472 {
2473 	struct dwc3		*dwc = gadget_to_dwc(g);
2474 	unsigned long		flags;
2475 
2476 	spin_lock_irqsave(&dwc->lock, flags);
2477 	g->is_selfpowered = !!is_selfpowered;
2478 	spin_unlock_irqrestore(&dwc->lock, flags);
2479 
2480 	return 0;
2481 }
2482 
2483 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2484 {
2485 	u32 epnum;
2486 
2487 	for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2488 		struct dwc3_ep *dep;
2489 
2490 		dep = dwc->eps[epnum];
2491 		if (!dep)
2492 			continue;
2493 
2494 		dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2495 	}
2496 }
2497 
2498 static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2499 {
2500 	enum usb_ssp_rate	ssp_rate = dwc->gadget_ssp_rate;
2501 	u32			reg;
2502 
2503 	if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2504 		ssp_rate = dwc->max_ssp_rate;
2505 
2506 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2507 	reg &= ~DWC3_DCFG_SPEED_MASK;
2508 	reg &= ~DWC3_DCFG_NUMLANES(~0);
2509 
2510 	if (ssp_rate == USB_SSP_GEN_1x2)
2511 		reg |= DWC3_DCFG_SUPERSPEED;
2512 	else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2513 		reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2514 
2515 	if (ssp_rate != USB_SSP_GEN_2x1 &&
2516 	    dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2517 		reg |= DWC3_DCFG_NUMLANES(1);
2518 
2519 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2520 }
2521 
2522 static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2523 {
2524 	enum usb_device_speed	speed;
2525 	u32			reg;
2526 
2527 	speed = dwc->gadget_max_speed;
2528 	if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
2529 		speed = dwc->maximum_speed;
2530 
2531 	if (speed == USB_SPEED_SUPER_PLUS &&
2532 	    DWC3_IP_IS(DWC32)) {
2533 		__dwc3_gadget_set_ssp_rate(dwc);
2534 		return;
2535 	}
2536 
2537 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2538 	reg &= ~(DWC3_DCFG_SPEED_MASK);
2539 
2540 	/*
2541 	 * WORKAROUND: DWC3 revision < 2.20a have an issue
2542 	 * which would cause metastability state on Run/Stop
2543 	 * bit if we try to force the IP to USB2-only mode.
2544 	 *
2545 	 * Because of that, we cannot configure the IP to any
2546 	 * speed other than the SuperSpeed
2547 	 *
2548 	 * Refers to:
2549 	 *
2550 	 * STAR#9000525659: Clock Domain Crossing on DCTL in
2551 	 * USB 2.0 Mode
2552 	 */
2553 	if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2554 	    !dwc->dis_metastability_quirk) {
2555 		reg |= DWC3_DCFG_SUPERSPEED;
2556 	} else {
2557 		switch (speed) {
2558 		case USB_SPEED_FULL:
2559 			reg |= DWC3_DCFG_FULLSPEED;
2560 			break;
2561 		case USB_SPEED_HIGH:
2562 			reg |= DWC3_DCFG_HIGHSPEED;
2563 			break;
2564 		case USB_SPEED_SUPER:
2565 			reg |= DWC3_DCFG_SUPERSPEED;
2566 			break;
2567 		case USB_SPEED_SUPER_PLUS:
2568 			if (DWC3_IP_IS(DWC3))
2569 				reg |= DWC3_DCFG_SUPERSPEED;
2570 			else
2571 				reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2572 			break;
2573 		default:
2574 			dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2575 
2576 			if (DWC3_IP_IS(DWC3))
2577 				reg |= DWC3_DCFG_SUPERSPEED;
2578 			else
2579 				reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2580 		}
2581 	}
2582 
2583 	if (DWC3_IP_IS(DWC32) &&
2584 	    speed > USB_SPEED_UNKNOWN &&
2585 	    speed < USB_SPEED_SUPER_PLUS)
2586 		reg &= ~DWC3_DCFG_NUMLANES(~0);
2587 
2588 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2589 }
2590 
2591 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
2592 {
2593 	u32			reg;
2594 	u32			timeout = 2000;
2595 
2596 	if (pm_runtime_suspended(dwc->dev))
2597 		return 0;
2598 
2599 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2600 	if (is_on) {
2601 		if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
2602 			reg &= ~DWC3_DCTL_TRGTULST_MASK;
2603 			reg |= DWC3_DCTL_TRGTULST_RX_DET;
2604 		}
2605 
2606 		if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
2607 			reg &= ~DWC3_DCTL_KEEP_CONNECT;
2608 		reg |= DWC3_DCTL_RUN_STOP;
2609 
2610 		__dwc3_gadget_set_speed(dwc);
2611 		dwc->pullups_connected = true;
2612 	} else {
2613 		reg &= ~DWC3_DCTL_RUN_STOP;
2614 
2615 		dwc->pullups_connected = false;
2616 	}
2617 
2618 	dwc3_gadget_dctl_write_safe(dwc, reg);
2619 
2620 	do {
2621 		usleep_range(1000, 2000);
2622 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2623 		reg &= DWC3_DSTS_DEVCTRLHLT;
2624 	} while (--timeout && !(!is_on ^ !reg));
2625 
2626 	if (!timeout)
2627 		return -ETIMEDOUT;
2628 
2629 	return 0;
2630 }
2631 
2632 static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2633 static void __dwc3_gadget_stop(struct dwc3 *dwc);
2634 static int __dwc3_gadget_start(struct dwc3 *dwc);
2635 
2636 static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2637 {
2638 	unsigned long flags;
2639 	int ret;
2640 
2641 	spin_lock_irqsave(&dwc->lock, flags);
2642 	dwc->connected = false;
2643 
2644 	/*
2645 	 * Attempt to end pending SETUP status phase, and not wait for the
2646 	 * function to do so.
2647 	 */
2648 	if (dwc->delayed_status)
2649 		dwc3_ep0_send_delayed_status(dwc);
2650 
2651 	/*
2652 	 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2653 	 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2654 	 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2655 	 * command for any active transfers" before clearing the RunStop
2656 	 * bit.
2657 	 */
2658 	dwc3_stop_active_transfers(dwc);
2659 	spin_unlock_irqrestore(&dwc->lock, flags);
2660 
2661 	/*
2662 	 * Per databook, when we want to stop the gadget, if a control transfer
2663 	 * is still in process, complete it and get the core into setup phase.
2664 	 * In case the host is unresponsive to a SETUP transaction, forcefully
2665 	 * stall the transfer, and move back to the SETUP phase, so that any
2666 	 * pending endxfers can be executed.
2667 	 */
2668 	if (dwc->ep0state != EP0_SETUP_PHASE) {
2669 		reinit_completion(&dwc->ep0_in_setup);
2670 
2671 		ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2672 				msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2673 		if (ret == 0) {
2674 			dev_warn(dwc->dev, "wait for SETUP phase timed out\n");
2675 			spin_lock_irqsave(&dwc->lock, flags);
2676 			dwc3_ep0_reset_state(dwc);
2677 			spin_unlock_irqrestore(&dwc->lock, flags);
2678 		}
2679 	}
2680 
2681 	/*
2682 	 * Note: if the GEVNTCOUNT indicates events in the event buffer, the
2683 	 * driver needs to acknowledge them before the controller can halt.
2684 	 * Simply let the interrupt handler acknowledges and handle the
2685 	 * remaining event generated by the controller while polling for
2686 	 * DSTS.DEVCTLHLT.
2687 	 */
2688 	ret = dwc3_gadget_run_stop(dwc, false);
2689 
2690 	/*
2691 	 * Stop the gadget after controller is halted, so that if needed, the
2692 	 * events to update EP0 state can still occur while the run/stop
2693 	 * routine polls for the halted state.  DEVTEN is cleared as part of
2694 	 * gadget stop.
2695 	 */
2696 	spin_lock_irqsave(&dwc->lock, flags);
2697 	__dwc3_gadget_stop(dwc);
2698 	spin_unlock_irqrestore(&dwc->lock, flags);
2699 
2700 	return ret;
2701 }
2702 
2703 static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
2704 {
2705 	/*
2706 	 * In the Synopsys DWC_usb31 1.90a programming guide section
2707 	 * 4.1.9, it specifies that for a reconnect after a
2708 	 * device-initiated disconnect requires a core soft reset
2709 	 * (DCTL.CSftRst) before enabling the run/stop bit.
2710 	 */
2711 	dwc3_core_soft_reset(dwc);
2712 
2713 	dwc3_event_buffers_setup(dwc);
2714 	__dwc3_gadget_start(dwc);
2715 	return dwc3_gadget_run_stop(dwc, true);
2716 }
2717 
2718 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2719 {
2720 	struct dwc3		*dwc = gadget_to_dwc(g);
2721 	int			ret;
2722 
2723 	is_on = !!is_on;
2724 
2725 	dwc->softconnect = is_on;
2726 
2727 	/*
2728 	 * Avoid issuing a runtime resume if the device is already in the
2729 	 * suspended state during gadget disconnect.  DWC3 gadget was already
2730 	 * halted/stopped during runtime suspend.
2731 	 */
2732 	if (!is_on) {
2733 		pm_runtime_barrier(dwc->dev);
2734 		if (pm_runtime_suspended(dwc->dev))
2735 			return 0;
2736 	}
2737 
2738 	/*
2739 	 * Check the return value for successful resume, or error.  For a
2740 	 * successful resume, the DWC3 runtime PM resume routine will handle
2741 	 * the run stop sequence, so avoid duplicate operations here.
2742 	 */
2743 	ret = pm_runtime_get_sync(dwc->dev);
2744 	if (!ret || ret < 0) {
2745 		pm_runtime_put(dwc->dev);
2746 		return 0;
2747 	}
2748 
2749 	if (dwc->pullups_connected == is_on) {
2750 		pm_runtime_put(dwc->dev);
2751 		return 0;
2752 	}
2753 
2754 	synchronize_irq(dwc->irq_gadget);
2755 
2756 	if (!is_on)
2757 		ret = dwc3_gadget_soft_disconnect(dwc);
2758 	else
2759 		ret = dwc3_gadget_soft_connect(dwc);
2760 
2761 	pm_runtime_put(dwc->dev);
2762 
2763 	return ret;
2764 }
2765 
2766 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2767 {
2768 	u32			reg;
2769 
2770 	/* Enable all but Start and End of Frame IRQs */
2771 	reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
2772 			DWC3_DEVTEN_CMDCMPLTEN |
2773 			DWC3_DEVTEN_ERRTICERREN |
2774 			DWC3_DEVTEN_WKUPEVTEN |
2775 			DWC3_DEVTEN_CONNECTDONEEN |
2776 			DWC3_DEVTEN_USBRSTEN |
2777 			DWC3_DEVTEN_DISCONNEVTEN);
2778 
2779 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2780 		reg |= DWC3_DEVTEN_ULSTCNGEN;
2781 
2782 	/* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2783 	if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2784 		reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
2785 
2786 	dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2787 }
2788 
2789 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2790 {
2791 	/* mask all interrupts */
2792 	dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2793 }
2794 
2795 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2796 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
2797 
2798 /**
2799  * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2800  * @dwc: pointer to our context structure
2801  *
2802  * The following looks like complex but it's actually very simple. In order to
2803  * calculate the number of packets we can burst at once on OUT transfers, we're
2804  * gonna use RxFIFO size.
2805  *
2806  * To calculate RxFIFO size we need two numbers:
2807  * MDWIDTH = size, in bits, of the internal memory bus
2808  * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2809  *
2810  * Given these two numbers, the formula is simple:
2811  *
2812  * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2813  *
2814  * 24 bytes is for 3x SETUP packets
2815  * 16 bytes is a clock domain crossing tolerance
2816  *
2817  * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2818  */
2819 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2820 {
2821 	u32 ram2_depth;
2822 	u32 mdwidth;
2823 	u32 nump;
2824 	u32 reg;
2825 
2826 	ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2827 	mdwidth = dwc3_mdwidth(dwc);
2828 
2829 	nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2830 	nump = min_t(u32, nump, 16);
2831 
2832 	/* update NumP */
2833 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2834 	reg &= ~DWC3_DCFG_NUMP_MASK;
2835 	reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2836 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2837 }
2838 
2839 static int __dwc3_gadget_start(struct dwc3 *dwc)
2840 {
2841 	struct dwc3_ep		*dep;
2842 	int			ret = 0;
2843 	u32			reg;
2844 
2845 	/*
2846 	 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2847 	 * the core supports IMOD, disable it.
2848 	 */
2849 	if (dwc->imod_interval) {
2850 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2851 		dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2852 	} else if (dwc3_has_imod(dwc)) {
2853 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2854 	}
2855 
2856 	/*
2857 	 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2858 	 * field instead of letting dwc3 itself calculate that automatically.
2859 	 *
2860 	 * This way, we maximize the chances that we'll be able to get several
2861 	 * bursts of data without going through any sort of endpoint throttling.
2862 	 */
2863 	reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2864 	if (DWC3_IP_IS(DWC3))
2865 		reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2866 	else
2867 		reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2868 
2869 	dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2870 
2871 	dwc3_gadget_setup_nump(dwc);
2872 
2873 	/*
2874 	 * Currently the controller handles single stream only. So, Ignore
2875 	 * Packet Pending bit for stream selection and don't search for another
2876 	 * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2877 	 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2878 	 * the stream performance.
2879 	 */
2880 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2881 	reg |= DWC3_DCFG_IGNSTRMPP;
2882 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2883 
2884 	/* Enable MST by default if the device is capable of MST */
2885 	if (DWC3_MST_CAPABLE(&dwc->hwparams)) {
2886 		reg = dwc3_readl(dwc->regs, DWC3_DCFG1);
2887 		reg &= ~DWC3_DCFG1_DIS_MST_ENH;
2888 		dwc3_writel(dwc->regs, DWC3_DCFG1, reg);
2889 	}
2890 
2891 	/* Start with SuperSpeed Default */
2892 	dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2893 
2894 	dep = dwc->eps[0];
2895 	dep->flags = 0;
2896 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2897 	if (ret) {
2898 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2899 		goto err0;
2900 	}
2901 
2902 	dep = dwc->eps[1];
2903 	dep->flags = 0;
2904 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2905 	if (ret) {
2906 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2907 		goto err1;
2908 	}
2909 
2910 	/* begin to receive SETUP packets */
2911 	dwc->ep0state = EP0_SETUP_PHASE;
2912 	dwc->ep0_bounced = false;
2913 	dwc->link_state = DWC3_LINK_STATE_SS_DIS;
2914 	dwc->delayed_status = false;
2915 	dwc3_ep0_out_start(dwc);
2916 
2917 	dwc3_gadget_enable_irq(dwc);
2918 
2919 	return 0;
2920 
2921 err1:
2922 	__dwc3_gadget_ep_disable(dwc->eps[0]);
2923 
2924 err0:
2925 	return ret;
2926 }
2927 
2928 static int dwc3_gadget_start(struct usb_gadget *g,
2929 		struct usb_gadget_driver *driver)
2930 {
2931 	struct dwc3		*dwc = gadget_to_dwc(g);
2932 	unsigned long		flags;
2933 	int			ret;
2934 	int			irq;
2935 
2936 	irq = dwc->irq_gadget;
2937 	ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2938 			IRQF_SHARED, "dwc3", dwc->ev_buf);
2939 	if (ret) {
2940 		dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2941 				irq, ret);
2942 		return ret;
2943 	}
2944 
2945 	spin_lock_irqsave(&dwc->lock, flags);
2946 	dwc->gadget_driver	= driver;
2947 	spin_unlock_irqrestore(&dwc->lock, flags);
2948 
2949 	return 0;
2950 }
2951 
2952 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2953 {
2954 	dwc3_gadget_disable_irq(dwc);
2955 	__dwc3_gadget_ep_disable(dwc->eps[0]);
2956 	__dwc3_gadget_ep_disable(dwc->eps[1]);
2957 }
2958 
2959 static int dwc3_gadget_stop(struct usb_gadget *g)
2960 {
2961 	struct dwc3		*dwc = gadget_to_dwc(g);
2962 	unsigned long		flags;
2963 
2964 	spin_lock_irqsave(&dwc->lock, flags);
2965 	dwc->gadget_driver	= NULL;
2966 	dwc->max_cfg_eps = 0;
2967 	spin_unlock_irqrestore(&dwc->lock, flags);
2968 
2969 	free_irq(dwc->irq_gadget, dwc->ev_buf);
2970 
2971 	return 0;
2972 }
2973 
2974 static void dwc3_gadget_config_params(struct usb_gadget *g,
2975 				      struct usb_dcd_config_params *params)
2976 {
2977 	struct dwc3		*dwc = gadget_to_dwc(g);
2978 
2979 	params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2980 	params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2981 
2982 	/* Recommended BESL */
2983 	if (!dwc->dis_enblslpm_quirk) {
2984 		/*
2985 		 * If the recommended BESL baseline is 0 or if the BESL deep is
2986 		 * less than 2, Microsoft's Windows 10 host usb stack will issue
2987 		 * a usb reset immediately after it receives the extended BOS
2988 		 * descriptor and the enumeration will fail. To maintain
2989 		 * compatibility with the Windows' usb stack, let's set the
2990 		 * recommended BESL baseline to 1 and clamp the BESL deep to be
2991 		 * within 2 to 15.
2992 		 */
2993 		params->besl_baseline = 1;
2994 		if (dwc->is_utmi_l1_suspend)
2995 			params->besl_deep =
2996 				clamp_t(u8, dwc->hird_threshold, 2, 15);
2997 	}
2998 
2999 	/* U1 Device exit Latency */
3000 	if (dwc->dis_u1_entry_quirk)
3001 		params->bU1devExitLat = 0;
3002 	else
3003 		params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
3004 
3005 	/* U2 Device exit Latency */
3006 	if (dwc->dis_u2_entry_quirk)
3007 		params->bU2DevExitLat = 0;
3008 	else
3009 		params->bU2DevExitLat =
3010 				cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
3011 }
3012 
3013 static void dwc3_gadget_set_speed(struct usb_gadget *g,
3014 				  enum usb_device_speed speed)
3015 {
3016 	struct dwc3		*dwc = gadget_to_dwc(g);
3017 	unsigned long		flags;
3018 
3019 	spin_lock_irqsave(&dwc->lock, flags);
3020 	dwc->gadget_max_speed = speed;
3021 	spin_unlock_irqrestore(&dwc->lock, flags);
3022 }
3023 
3024 static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
3025 				     enum usb_ssp_rate rate)
3026 {
3027 	struct dwc3		*dwc = gadget_to_dwc(g);
3028 	unsigned long		flags;
3029 
3030 	spin_lock_irqsave(&dwc->lock, flags);
3031 	dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
3032 	dwc->gadget_ssp_rate = rate;
3033 	spin_unlock_irqrestore(&dwc->lock, flags);
3034 }
3035 
3036 static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
3037 {
3038 	struct dwc3		*dwc = gadget_to_dwc(g);
3039 	union power_supply_propval	val = {0};
3040 	int				ret;
3041 
3042 	if (dwc->usb2_phy)
3043 		return usb_phy_set_power(dwc->usb2_phy, mA);
3044 
3045 	if (!dwc->usb_psy)
3046 		return -EOPNOTSUPP;
3047 
3048 	val.intval = 1000 * mA;
3049 	ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
3050 
3051 	return ret;
3052 }
3053 
3054 /**
3055  * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
3056  * @g: pointer to the USB gadget
3057  *
3058  * Used to record the maximum number of endpoints being used in a USB composite
3059  * device. (across all configurations)  This is to be used in the calculation
3060  * of the TXFIFO sizes when resizing internal memory for individual endpoints.
3061  * It will help ensured that the resizing logic reserves enough space for at
3062  * least one max packet.
3063  */
3064 static int dwc3_gadget_check_config(struct usb_gadget *g)
3065 {
3066 	struct dwc3 *dwc = gadget_to_dwc(g);
3067 	struct usb_ep *ep;
3068 	int fifo_size = 0;
3069 	int ram1_depth;
3070 	int ep_num = 0;
3071 
3072 	if (!dwc->do_fifo_resize)
3073 		return 0;
3074 
3075 	list_for_each_entry(ep, &g->ep_list, ep_list) {
3076 		/* Only interested in the IN endpoints */
3077 		if (ep->claimed && (ep->address & USB_DIR_IN))
3078 			ep_num++;
3079 	}
3080 
3081 	if (ep_num <= dwc->max_cfg_eps)
3082 		return 0;
3083 
3084 	/* Update the max number of eps in the composition */
3085 	dwc->max_cfg_eps = ep_num;
3086 
3087 	fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
3088 	/* Based on the equation, increment by one for every ep */
3089 	fifo_size += dwc->max_cfg_eps;
3090 
3091 	/* Check if we can fit a single fifo per endpoint */
3092 	ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
3093 	if (fifo_size > ram1_depth)
3094 		return -ENOMEM;
3095 
3096 	return 0;
3097 }
3098 
3099 static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
3100 {
3101 	struct dwc3		*dwc = gadget_to_dwc(g);
3102 	unsigned long		flags;
3103 
3104 	spin_lock_irqsave(&dwc->lock, flags);
3105 	dwc->async_callbacks = enable;
3106 	spin_unlock_irqrestore(&dwc->lock, flags);
3107 }
3108 
3109 static const struct usb_gadget_ops dwc3_gadget_ops = {
3110 	.get_frame		= dwc3_gadget_get_frame,
3111 	.wakeup			= dwc3_gadget_wakeup,
3112 	.func_wakeup		= dwc3_gadget_func_wakeup,
3113 	.set_remote_wakeup	= dwc3_gadget_set_remote_wakeup,
3114 	.set_selfpowered	= dwc3_gadget_set_selfpowered,
3115 	.pullup			= dwc3_gadget_pullup,
3116 	.udc_start		= dwc3_gadget_start,
3117 	.udc_stop		= dwc3_gadget_stop,
3118 	.udc_set_speed		= dwc3_gadget_set_speed,
3119 	.udc_set_ssp_rate	= dwc3_gadget_set_ssp_rate,
3120 	.get_config_params	= dwc3_gadget_config_params,
3121 	.vbus_draw		= dwc3_gadget_vbus_draw,
3122 	.check_config		= dwc3_gadget_check_config,
3123 	.udc_async_callbacks	= dwc3_gadget_async_callbacks,
3124 };
3125 
3126 /* -------------------------------------------------------------------------- */
3127 
3128 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
3129 {
3130 	struct dwc3 *dwc = dep->dwc;
3131 
3132 	usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
3133 	dep->endpoint.maxburst = 1;
3134 	dep->endpoint.ops = &dwc3_gadget_ep0_ops;
3135 	if (!dep->direction)
3136 		dwc->gadget->ep0 = &dep->endpoint;
3137 
3138 	dep->endpoint.caps.type_control = true;
3139 
3140 	return 0;
3141 }
3142 
3143 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
3144 {
3145 	struct dwc3 *dwc = dep->dwc;
3146 	u32 mdwidth;
3147 	int size;
3148 	int maxpacket;
3149 
3150 	mdwidth = dwc3_mdwidth(dwc);
3151 
3152 	/* MDWIDTH is represented in bits, we need it in bytes */
3153 	mdwidth /= 8;
3154 
3155 	size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
3156 	if (DWC3_IP_IS(DWC3))
3157 		size = DWC3_GTXFIFOSIZ_TXFDEP(size);
3158 	else
3159 		size = DWC31_GTXFIFOSIZ_TXFDEP(size);
3160 
3161 	/*
3162 	 * maxpacket size is determined as part of the following, after assuming
3163 	 * a mult value of one maxpacket:
3164 	 * DWC3 revision 280A and prior:
3165 	 * fifo_size = mult * (max_packet / mdwidth) + 1;
3166 	 * maxpacket = mdwidth * (fifo_size - 1);
3167 	 *
3168 	 * DWC3 revision 290A and onwards:
3169 	 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
3170 	 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
3171 	 */
3172 	if (DWC3_VER_IS_PRIOR(DWC3, 290A))
3173 		maxpacket = mdwidth * (size - 1);
3174 	else
3175 		maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
3176 
3177 	/* Functionally, space for one max packet is sufficient */
3178 	size = min_t(int, maxpacket, 1024);
3179 	usb_ep_set_maxpacket_limit(&dep->endpoint, size);
3180 
3181 	dep->endpoint.max_streams = 16;
3182 	dep->endpoint.ops = &dwc3_gadget_ep_ops;
3183 	list_add_tail(&dep->endpoint.ep_list,
3184 			&dwc->gadget->ep_list);
3185 	dep->endpoint.caps.type_iso = true;
3186 	dep->endpoint.caps.type_bulk = true;
3187 	dep->endpoint.caps.type_int = true;
3188 
3189 	return dwc3_alloc_trb_pool(dep);
3190 }
3191 
3192 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
3193 {
3194 	struct dwc3 *dwc = dep->dwc;
3195 	u32 mdwidth;
3196 	int size;
3197 
3198 	mdwidth = dwc3_mdwidth(dwc);
3199 
3200 	/* MDWIDTH is represented in bits, convert to bytes */
3201 	mdwidth /= 8;
3202 
3203 	/* All OUT endpoints share a single RxFIFO space */
3204 	size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
3205 	if (DWC3_IP_IS(DWC3))
3206 		size = DWC3_GRXFIFOSIZ_RXFDEP(size);
3207 	else
3208 		size = DWC31_GRXFIFOSIZ_RXFDEP(size);
3209 
3210 	/* FIFO depth is in MDWDITH bytes */
3211 	size *= mdwidth;
3212 
3213 	/*
3214 	 * To meet performance requirement, a minimum recommended RxFIFO size
3215 	 * is defined as follow:
3216 	 * RxFIFO size >= (3 x MaxPacketSize) +
3217 	 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
3218 	 *
3219 	 * Then calculate the max packet limit as below.
3220 	 */
3221 	size -= (3 * 8) + 16;
3222 	if (size < 0)
3223 		size = 0;
3224 	else
3225 		size /= 3;
3226 
3227 	usb_ep_set_maxpacket_limit(&dep->endpoint, size);
3228 	dep->endpoint.max_streams = 16;
3229 	dep->endpoint.ops = &dwc3_gadget_ep_ops;
3230 	list_add_tail(&dep->endpoint.ep_list,
3231 			&dwc->gadget->ep_list);
3232 	dep->endpoint.caps.type_iso = true;
3233 	dep->endpoint.caps.type_bulk = true;
3234 	dep->endpoint.caps.type_int = true;
3235 
3236 	return dwc3_alloc_trb_pool(dep);
3237 }
3238 
3239 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
3240 {
3241 	struct dwc3_ep			*dep;
3242 	bool				direction = epnum & 1;
3243 	int				ret;
3244 	u8				num = epnum >> 1;
3245 
3246 	dep = kzalloc(sizeof(*dep), GFP_KERNEL);
3247 	if (!dep)
3248 		return -ENOMEM;
3249 
3250 	dep->dwc = dwc;
3251 	dep->number = epnum;
3252 	dep->direction = direction;
3253 	dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
3254 	dwc->eps[epnum] = dep;
3255 	dep->combo_num = 0;
3256 	dep->start_cmd_status = 0;
3257 
3258 	snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
3259 			direction ? "in" : "out");
3260 
3261 	dep->endpoint.name = dep->name;
3262 
3263 	if (!(dep->number > 1)) {
3264 		dep->endpoint.desc = &dwc3_gadget_ep0_desc;
3265 		dep->endpoint.comp_desc = NULL;
3266 	}
3267 
3268 	if (num == 0)
3269 		ret = dwc3_gadget_init_control_endpoint(dep);
3270 	else if (direction)
3271 		ret = dwc3_gadget_init_in_endpoint(dep);
3272 	else
3273 		ret = dwc3_gadget_init_out_endpoint(dep);
3274 
3275 	if (ret)
3276 		return ret;
3277 
3278 	dep->endpoint.caps.dir_in = direction;
3279 	dep->endpoint.caps.dir_out = !direction;
3280 
3281 	INIT_LIST_HEAD(&dep->pending_list);
3282 	INIT_LIST_HEAD(&dep->started_list);
3283 	INIT_LIST_HEAD(&dep->cancelled_list);
3284 
3285 	dwc3_debugfs_create_endpoint_dir(dep);
3286 
3287 	return 0;
3288 }
3289 
3290 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
3291 {
3292 	u8				epnum;
3293 
3294 	INIT_LIST_HEAD(&dwc->gadget->ep_list);
3295 
3296 	for (epnum = 0; epnum < total; epnum++) {
3297 		int			ret;
3298 
3299 		ret = dwc3_gadget_init_endpoint(dwc, epnum);
3300 		if (ret)
3301 			return ret;
3302 	}
3303 
3304 	return 0;
3305 }
3306 
3307 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
3308 {
3309 	struct dwc3_ep			*dep;
3310 	u8				epnum;
3311 
3312 	for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3313 		dep = dwc->eps[epnum];
3314 		if (!dep)
3315 			continue;
3316 		/*
3317 		 * Physical endpoints 0 and 1 are special; they form the
3318 		 * bi-directional USB endpoint 0.
3319 		 *
3320 		 * For those two physical endpoints, we don't allocate a TRB
3321 		 * pool nor do we add them the endpoints list. Due to that, we
3322 		 * shouldn't do these two operations otherwise we would end up
3323 		 * with all sorts of bugs when removing dwc3.ko.
3324 		 */
3325 		if (epnum != 0 && epnum != 1) {
3326 			dwc3_free_trb_pool(dep);
3327 			list_del(&dep->endpoint.ep_list);
3328 		}
3329 
3330 		dwc3_debugfs_remove_endpoint_dir(dep);
3331 		kfree(dep);
3332 	}
3333 }
3334 
3335 /* -------------------------------------------------------------------------- */
3336 
3337 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
3338 		struct dwc3_request *req, struct dwc3_trb *trb,
3339 		const struct dwc3_event_depevt *event, int status, int chain)
3340 {
3341 	unsigned int		count;
3342 
3343 	dwc3_ep_inc_deq(dep);
3344 
3345 	trace_dwc3_complete_trb(dep, trb);
3346 	req->num_trbs--;
3347 
3348 	/*
3349 	 * If we're in the middle of series of chained TRBs and we
3350 	 * receive a short transfer along the way, DWC3 will skip
3351 	 * through all TRBs including the last TRB in the chain (the
3352 	 * where CHN bit is zero. DWC3 will also avoid clearing HWO
3353 	 * bit and SW has to do it manually.
3354 	 *
3355 	 * We're going to do that here to avoid problems of HW trying
3356 	 * to use bogus TRBs for transfers.
3357 	 */
3358 	if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
3359 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3360 
3361 	/*
3362 	 * For isochronous transfers, the first TRB in a service interval must
3363 	 * have the Isoc-First type. Track and report its interval frame number.
3364 	 */
3365 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3366 	    (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
3367 		unsigned int frame_number;
3368 
3369 		frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
3370 		frame_number &= ~(dep->interval - 1);
3371 		req->request.frame_number = frame_number;
3372 	}
3373 
3374 	/*
3375 	 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3376 	 * this TRB points to the bounce buffer address, it's a MPS alignment
3377 	 * TRB. Don't add it to req->remaining calculation.
3378 	 */
3379 	if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3380 	    trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
3381 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3382 		return 1;
3383 	}
3384 
3385 	count = trb->size & DWC3_TRB_SIZE_MASK;
3386 	req->remaining += count;
3387 
3388 	if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
3389 		return 1;
3390 
3391 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
3392 		return 1;
3393 
3394 	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
3395 	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
3396 		return 1;
3397 
3398 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
3399 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
3400 		return 1;
3401 
3402 	return 0;
3403 }
3404 
3405 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
3406 		struct dwc3_request *req, const struct dwc3_event_depevt *event,
3407 		int status)
3408 {
3409 	struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3410 	struct scatterlist *sg = req->sg;
3411 	struct scatterlist *s;
3412 	unsigned int num_queued = req->num_queued_sgs;
3413 	unsigned int i;
3414 	int ret = 0;
3415 
3416 	for_each_sg(sg, s, num_queued, i) {
3417 		trb = &dep->trb_pool[dep->trb_dequeue];
3418 
3419 		req->sg = sg_next(s);
3420 		req->num_queued_sgs--;
3421 
3422 		ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
3423 				trb, event, status, true);
3424 		if (ret)
3425 			break;
3426 	}
3427 
3428 	return ret;
3429 }
3430 
3431 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
3432 		struct dwc3_request *req, const struct dwc3_event_depevt *event,
3433 		int status)
3434 {
3435 	struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3436 
3437 	return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
3438 			event, status, false);
3439 }
3440 
3441 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
3442 {
3443 	return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
3444 }
3445 
3446 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
3447 		const struct dwc3_event_depevt *event,
3448 		struct dwc3_request *req, int status)
3449 {
3450 	int request_status;
3451 	int ret;
3452 
3453 	if (req->request.num_mapped_sgs)
3454 		ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
3455 				status);
3456 	else
3457 		ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3458 				status);
3459 
3460 	req->request.actual = req->request.length - req->remaining;
3461 
3462 	if (!dwc3_gadget_ep_request_completed(req))
3463 		goto out;
3464 
3465 	if (req->needs_extra_trb) {
3466 		ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3467 				status);
3468 		req->needs_extra_trb = false;
3469 	}
3470 
3471 	/*
3472 	 * The event status only reflects the status of the TRB with IOC set.
3473 	 * For the requests that don't set interrupt on completion, the driver
3474 	 * needs to check and return the status of the completed TRBs associated
3475 	 * with the request. Use the status of the last TRB of the request.
3476 	 */
3477 	if (req->request.no_interrupt) {
3478 		struct dwc3_trb *trb;
3479 
3480 		trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
3481 		switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
3482 		case DWC3_TRBSTS_MISSED_ISOC:
3483 			/* Isoc endpoint only */
3484 			request_status = -EXDEV;
3485 			break;
3486 		case DWC3_TRB_STS_XFER_IN_PROG:
3487 			/* Applicable when End Transfer with ForceRM=0 */
3488 		case DWC3_TRBSTS_SETUP_PENDING:
3489 			/* Control endpoint only */
3490 		case DWC3_TRBSTS_OK:
3491 		default:
3492 			request_status = 0;
3493 			break;
3494 		}
3495 	} else {
3496 		request_status = status;
3497 	}
3498 
3499 	dwc3_gadget_giveback(dep, req, request_status);
3500 
3501 out:
3502 	return ret;
3503 }
3504 
3505 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
3506 		const struct dwc3_event_depevt *event, int status)
3507 {
3508 	struct dwc3_request	*req;
3509 
3510 	while (!list_empty(&dep->started_list)) {
3511 		int ret;
3512 
3513 		req = next_request(&dep->started_list);
3514 		ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
3515 				req, status);
3516 		if (ret)
3517 			break;
3518 		/*
3519 		 * The endpoint is disabled, let the dwc3_remove_requests()
3520 		 * handle the cleanup.
3521 		 */
3522 		if (!dep->endpoint.desc)
3523 			break;
3524 	}
3525 }
3526 
3527 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
3528 {
3529 	struct dwc3_request	*req;
3530 	struct dwc3		*dwc = dep->dwc;
3531 
3532 	if (!dep->endpoint.desc || !dwc->pullups_connected ||
3533 	    !dwc->connected)
3534 		return false;
3535 
3536 	if (!list_empty(&dep->pending_list))
3537 		return true;
3538 
3539 	/*
3540 	 * We only need to check the first entry of the started list. We can
3541 	 * assume the completed requests are removed from the started list.
3542 	 */
3543 	req = next_request(&dep->started_list);
3544 	if (!req)
3545 		return false;
3546 
3547 	return !dwc3_gadget_ep_request_completed(req);
3548 }
3549 
3550 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
3551 		const struct dwc3_event_depevt *event)
3552 {
3553 	dep->frame_number = event->parameters;
3554 }
3555 
3556 static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
3557 		const struct dwc3_event_depevt *event, int status)
3558 {
3559 	struct dwc3		*dwc = dep->dwc;
3560 	bool			no_started_trb = true;
3561 
3562 	dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
3563 
3564 	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3565 		goto out;
3566 
3567 	if (!dep->endpoint.desc)
3568 		return no_started_trb;
3569 
3570 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3571 		list_empty(&dep->started_list) &&
3572 		(list_empty(&dep->pending_list) || status == -EXDEV))
3573 		dwc3_stop_active_transfer(dep, true, true);
3574 	else if (dwc3_gadget_ep_should_continue(dep))
3575 		if (__dwc3_gadget_kick_transfer(dep) == 0)
3576 			no_started_trb = false;
3577 
3578 out:
3579 	/*
3580 	 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3581 	 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3582 	 */
3583 	if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3584 		u32		reg;
3585 		int		i;
3586 
3587 		for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
3588 			dep = dwc->eps[i];
3589 
3590 			if (!(dep->flags & DWC3_EP_ENABLED))
3591 				continue;
3592 
3593 			if (!list_empty(&dep->started_list))
3594 				return no_started_trb;
3595 		}
3596 
3597 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3598 		reg |= dwc->u1u2;
3599 		dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3600 
3601 		dwc->u1u2 = 0;
3602 	}
3603 
3604 	return no_started_trb;
3605 }
3606 
3607 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3608 		const struct dwc3_event_depevt *event)
3609 {
3610 	int status = 0;
3611 
3612 	if (!dep->endpoint.desc)
3613 		return;
3614 
3615 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3616 		dwc3_gadget_endpoint_frame_from_event(dep, event);
3617 
3618 	if (event->status & DEPEVT_STATUS_BUSERR)
3619 		status = -ECONNRESET;
3620 
3621 	if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3622 		status = -EXDEV;
3623 
3624 	dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3625 }
3626 
3627 static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3628 		const struct dwc3_event_depevt *event)
3629 {
3630 	int status = 0;
3631 
3632 	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3633 
3634 	if (event->status & DEPEVT_STATUS_BUSERR)
3635 		status = -ECONNRESET;
3636 
3637 	if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3638 		dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
3639 }
3640 
3641 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3642 		const struct dwc3_event_depevt *event)
3643 {
3644 	dwc3_gadget_endpoint_frame_from_event(dep, event);
3645 
3646 	/*
3647 	 * The XferNotReady event is generated only once before the endpoint
3648 	 * starts. It will be generated again when END_TRANSFER command is
3649 	 * issued. For some controller versions, the XferNotReady event may be
3650 	 * generated while the END_TRANSFER command is still in process. Ignore
3651 	 * it and wait for the next XferNotReady event after the command is
3652 	 * completed.
3653 	 */
3654 	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3655 		return;
3656 
3657 	(void) __dwc3_gadget_start_isoc(dep);
3658 }
3659 
3660 static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3661 		const struct dwc3_event_depevt *event)
3662 {
3663 	u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3664 
3665 	if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3666 		return;
3667 
3668 	/*
3669 	 * The END_TRANSFER command will cause the controller to generate a
3670 	 * NoStream Event, and it's not due to the host DP NoStream rejection.
3671 	 * Ignore the next NoStream event.
3672 	 */
3673 	if (dep->stream_capable)
3674 		dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3675 
3676 	dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3677 	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3678 	dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3679 
3680 	if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3681 		struct dwc3 *dwc = dep->dwc;
3682 
3683 		dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3684 		if (dwc3_send_clear_stall_ep_cmd(dep)) {
3685 			struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3686 
3687 			dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3688 			if (dwc->delayed_status)
3689 				__dwc3_gadget_ep0_set_halt(ep0, 1);
3690 			return;
3691 		}
3692 
3693 		dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3694 		if (dwc->clear_stall_protocol == dep->number)
3695 			dwc3_ep0_send_delayed_status(dwc);
3696 	}
3697 
3698 	if ((dep->flags & DWC3_EP_DELAY_START) &&
3699 	    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3700 		__dwc3_gadget_kick_transfer(dep);
3701 
3702 	dep->flags &= ~DWC3_EP_DELAY_START;
3703 }
3704 
3705 static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3706 		const struct dwc3_event_depevt *event)
3707 {
3708 	struct dwc3 *dwc = dep->dwc;
3709 
3710 	if (event->status == DEPEVT_STREAMEVT_FOUND) {
3711 		dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3712 		goto out;
3713 	}
3714 
3715 	/* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3716 	switch (event->parameters) {
3717 	case DEPEVT_STREAM_PRIME:
3718 		/*
3719 		 * If the host can properly transition the endpoint state from
3720 		 * idle to prime after a NoStream rejection, there's no need to
3721 		 * force restarting the endpoint to reinitiate the stream. To
3722 		 * simplify the check, assume the host follows the USB spec if
3723 		 * it primed the endpoint more than once.
3724 		 */
3725 		if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3726 			if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3727 				dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3728 			else
3729 				dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3730 		}
3731 
3732 		break;
3733 	case DEPEVT_STREAM_NOSTREAM:
3734 		if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3735 		    !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3736 		    (!DWC3_MST_CAPABLE(&dwc->hwparams) &&
3737 		     !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)))
3738 			break;
3739 
3740 		/*
3741 		 * If the host rejects a stream due to no active stream, by the
3742 		 * USB and xHCI spec, the endpoint will be put back to idle
3743 		 * state. When the host is ready (buffer added/updated), it will
3744 		 * prime the endpoint to inform the usb device controller. This
3745 		 * triggers the device controller to issue ERDY to restart the
3746 		 * stream. However, some hosts don't follow this and keep the
3747 		 * endpoint in the idle state. No prime will come despite host
3748 		 * streams are updated, and the device controller will not be
3749 		 * triggered to generate ERDY to move the next stream data. To
3750 		 * workaround this and maintain compatibility with various
3751 		 * hosts, force to reinitiate the stream until the host is ready
3752 		 * instead of waiting for the host to prime the endpoint.
3753 		 */
3754 		if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3755 			unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3756 
3757 			dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3758 		} else {
3759 			dep->flags |= DWC3_EP_DELAY_START;
3760 			dwc3_stop_active_transfer(dep, true, true);
3761 			return;
3762 		}
3763 		break;
3764 	}
3765 
3766 out:
3767 	dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3768 }
3769 
3770 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3771 		const struct dwc3_event_depevt *event)
3772 {
3773 	struct dwc3_ep		*dep;
3774 	u8			epnum = event->endpoint_number;
3775 
3776 	dep = dwc->eps[epnum];
3777 
3778 	if (!(dep->flags & DWC3_EP_ENABLED)) {
3779 		if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
3780 			return;
3781 
3782 		/* Handle only EPCMDCMPLT when EP disabled */
3783 		if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
3784 			!(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
3785 			return;
3786 	}
3787 
3788 	if (epnum == 0 || epnum == 1) {
3789 		dwc3_ep0_interrupt(dwc, event);
3790 		return;
3791 	}
3792 
3793 	switch (event->endpoint_event) {
3794 	case DWC3_DEPEVT_XFERINPROGRESS:
3795 		dwc3_gadget_endpoint_transfer_in_progress(dep, event);
3796 		break;
3797 	case DWC3_DEPEVT_XFERNOTREADY:
3798 		dwc3_gadget_endpoint_transfer_not_ready(dep, event);
3799 		break;
3800 	case DWC3_DEPEVT_EPCMDCMPLT:
3801 		dwc3_gadget_endpoint_command_complete(dep, event);
3802 		break;
3803 	case DWC3_DEPEVT_XFERCOMPLETE:
3804 		dwc3_gadget_endpoint_transfer_complete(dep, event);
3805 		break;
3806 	case DWC3_DEPEVT_STREAMEVT:
3807 		dwc3_gadget_endpoint_stream_event(dep, event);
3808 		break;
3809 	case DWC3_DEPEVT_RXTXFIFOEVT:
3810 		break;
3811 	}
3812 }
3813 
3814 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3815 {
3816 	if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
3817 		spin_unlock(&dwc->lock);
3818 		dwc->gadget_driver->disconnect(dwc->gadget);
3819 		spin_lock(&dwc->lock);
3820 	}
3821 }
3822 
3823 static void dwc3_suspend_gadget(struct dwc3 *dwc)
3824 {
3825 	if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
3826 		spin_unlock(&dwc->lock);
3827 		dwc->gadget_driver->suspend(dwc->gadget);
3828 		spin_lock(&dwc->lock);
3829 	}
3830 }
3831 
3832 static void dwc3_resume_gadget(struct dwc3 *dwc)
3833 {
3834 	if (dwc->async_callbacks && dwc->gadget_driver->resume) {
3835 		spin_unlock(&dwc->lock);
3836 		dwc->gadget_driver->resume(dwc->gadget);
3837 		spin_lock(&dwc->lock);
3838 	}
3839 }
3840 
3841 static void dwc3_reset_gadget(struct dwc3 *dwc)
3842 {
3843 	if (!dwc->gadget_driver)
3844 		return;
3845 
3846 	if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
3847 		spin_unlock(&dwc->lock);
3848 		usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
3849 		spin_lock(&dwc->lock);
3850 	}
3851 }
3852 
3853 void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3854 	bool interrupt)
3855 {
3856 	struct dwc3 *dwc = dep->dwc;
3857 
3858 	/*
3859 	 * Only issue End Transfer command to the control endpoint of a started
3860 	 * Data Phase. Typically we should only do so in error cases such as
3861 	 * invalid/unexpected direction as described in the control transfer
3862 	 * flow of the programming guide.
3863 	 */
3864 	if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
3865 		return;
3866 
3867 	if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP))
3868 		return;
3869 
3870 	if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3871 	    (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
3872 		return;
3873 
3874 	/*
3875 	 * If a Setup packet is received but yet to DMA out, the controller will
3876 	 * not process the End Transfer command of any endpoint. Polling of its
3877 	 * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
3878 	 * timeout. Delay issuing the End Transfer command until the Setup TRB is
3879 	 * prepared.
3880 	 */
3881 	if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
3882 		dep->flags |= DWC3_EP_DELAY_STOP;
3883 		return;
3884 	}
3885 
3886 	/*
3887 	 * NOTICE: We are violating what the Databook says about the
3888 	 * EndTransfer command. Ideally we would _always_ wait for the
3889 	 * EndTransfer Command Completion IRQ, but that's causing too
3890 	 * much trouble synchronizing between us and gadget driver.
3891 	 *
3892 	 * We have discussed this with the IP Provider and it was
3893 	 * suggested to giveback all requests here.
3894 	 *
3895 	 * Note also that a similar handling was tested by Synopsys
3896 	 * (thanks a lot Paul) and nothing bad has come out of it.
3897 	 * In short, what we're doing is issuing EndTransfer with
3898 	 * CMDIOC bit set and delay kicking transfer until the
3899 	 * EndTransfer command had completed.
3900 	 *
3901 	 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3902 	 * supports a mode to work around the above limitation. The
3903 	 * software can poll the CMDACT bit in the DEPCMD register
3904 	 * after issuing a EndTransfer command. This mode is enabled
3905 	 * by writing GUCTL2[14]. This polling is already done in the
3906 	 * dwc3_send_gadget_ep_cmd() function so if the mode is
3907 	 * enabled, the EndTransfer command will have completed upon
3908 	 * returning from this function.
3909 	 *
3910 	 * This mode is NOT available on the DWC_usb31 IP.  In this
3911 	 * case, if the IOC bit is not set, then delay by 1ms
3912 	 * after issuing the EndTransfer command.  This allows for the
3913 	 * controller to handle the command completely before DWC3
3914 	 * remove requests attempts to unmap USB request buffers.
3915 	 */
3916 
3917 	__dwc3_stop_active_transfer(dep, force, interrupt);
3918 }
3919 
3920 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3921 {
3922 	u32 epnum;
3923 
3924 	for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3925 		struct dwc3_ep *dep;
3926 		int ret;
3927 
3928 		dep = dwc->eps[epnum];
3929 		if (!dep)
3930 			continue;
3931 
3932 		if (!(dep->flags & DWC3_EP_STALL))
3933 			continue;
3934 
3935 		dep->flags &= ~DWC3_EP_STALL;
3936 
3937 		ret = dwc3_send_clear_stall_ep_cmd(dep);
3938 		WARN_ON_ONCE(ret);
3939 	}
3940 }
3941 
3942 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3943 {
3944 	int			reg;
3945 
3946 	dwc->suspended = false;
3947 
3948 	dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3949 
3950 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3951 	reg &= ~DWC3_DCTL_INITU1ENA;
3952 	reg &= ~DWC3_DCTL_INITU2ENA;
3953 	dwc3_gadget_dctl_write_safe(dwc, reg);
3954 
3955 	dwc->connected = false;
3956 
3957 	dwc3_disconnect_gadget(dwc);
3958 
3959 	dwc->gadget->speed = USB_SPEED_UNKNOWN;
3960 	dwc->setup_packet_pending = false;
3961 	dwc->gadget->wakeup_armed = false;
3962 	dwc3_gadget_enable_linksts_evts(dwc, false);
3963 	usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
3964 
3965 	dwc3_ep0_reset_state(dwc);
3966 }
3967 
3968 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3969 {
3970 	u32			reg;
3971 
3972 	dwc->suspended = false;
3973 
3974 	/*
3975 	 * Ideally, dwc3_reset_gadget() would trigger the function
3976 	 * drivers to stop any active transfers through ep disable.
3977 	 * However, for functions which defer ep disable, such as mass
3978 	 * storage, we will need to rely on the call to stop active
3979 	 * transfers here, and avoid allowing of request queuing.
3980 	 */
3981 	dwc->connected = false;
3982 
3983 	/*
3984 	 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3985 	 * would cause a missing Disconnect Event if there's a
3986 	 * pending Setup Packet in the FIFO.
3987 	 *
3988 	 * There's no suggested workaround on the official Bug
3989 	 * report, which states that "unless the driver/application
3990 	 * is doing any special handling of a disconnect event,
3991 	 * there is no functional issue".
3992 	 *
3993 	 * Unfortunately, it turns out that we _do_ some special
3994 	 * handling of a disconnect event, namely complete all
3995 	 * pending transfers, notify gadget driver of the
3996 	 * disconnection, and so on.
3997 	 *
3998 	 * Our suggested workaround is to follow the Disconnect
3999 	 * Event steps here, instead, based on a setup_packet_pending
4000 	 * flag. Such flag gets set whenever we have a SETUP_PENDING
4001 	 * status for EP0 TRBs and gets cleared on XferComplete for the
4002 	 * same endpoint.
4003 	 *
4004 	 * Refers to:
4005 	 *
4006 	 * STAR#9000466709: RTL: Device : Disconnect event not
4007 	 * generated if setup packet pending in FIFO
4008 	 */
4009 	if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
4010 		if (dwc->setup_packet_pending)
4011 			dwc3_gadget_disconnect_interrupt(dwc);
4012 	}
4013 
4014 	dwc3_reset_gadget(dwc);
4015 
4016 	/*
4017 	 * From SNPS databook section 8.1.2, the EP0 should be in setup
4018 	 * phase. So ensure that EP0 is in setup phase by issuing a stall
4019 	 * and restart if EP0 is not in setup phase.
4020 	 */
4021 	dwc3_ep0_reset_state(dwc);
4022 
4023 	/*
4024 	 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
4025 	 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
4026 	 * needs to ensure that it sends "a DEPENDXFER command for any active
4027 	 * transfers."
4028 	 */
4029 	dwc3_stop_active_transfers(dwc);
4030 	dwc->connected = true;
4031 
4032 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4033 	reg &= ~DWC3_DCTL_TSTCTRL_MASK;
4034 	dwc3_gadget_dctl_write_safe(dwc, reg);
4035 	dwc->test_mode = false;
4036 	dwc->gadget->wakeup_armed = false;
4037 	dwc3_gadget_enable_linksts_evts(dwc, false);
4038 	dwc3_clear_stall_all_ep(dwc);
4039 
4040 	/* Reset device address to zero */
4041 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4042 	reg &= ~(DWC3_DCFG_DEVADDR_MASK);
4043 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4044 }
4045 
4046 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
4047 {
4048 	struct dwc3_ep		*dep;
4049 	int			ret;
4050 	u32			reg;
4051 	u8			lanes = 1;
4052 	u8			speed;
4053 
4054 	if (!dwc->softconnect)
4055 		return;
4056 
4057 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
4058 	speed = reg & DWC3_DSTS_CONNECTSPD;
4059 	dwc->speed = speed;
4060 
4061 	if (DWC3_IP_IS(DWC32))
4062 		lanes = DWC3_DSTS_CONNLANES(reg) + 1;
4063 
4064 	dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4065 
4066 	/*
4067 	 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
4068 	 * each time on Connect Done.
4069 	 *
4070 	 * Currently we always use the reset value. If any platform
4071 	 * wants to set this to a different value, we need to add a
4072 	 * setting and update GCTL.RAMCLKSEL here.
4073 	 */
4074 
4075 	switch (speed) {
4076 	case DWC3_DSTS_SUPERSPEED_PLUS:
4077 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4078 		dwc->gadget->ep0->maxpacket = 512;
4079 		dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4080 
4081 		if (lanes > 1)
4082 			dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
4083 		else
4084 			dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
4085 		break;
4086 	case DWC3_DSTS_SUPERSPEED:
4087 		/*
4088 		 * WORKAROUND: DWC3 revisions <1.90a have an issue which
4089 		 * would cause a missing USB3 Reset event.
4090 		 *
4091 		 * In such situations, we should force a USB3 Reset
4092 		 * event by calling our dwc3_gadget_reset_interrupt()
4093 		 * routine.
4094 		 *
4095 		 * Refers to:
4096 		 *
4097 		 * STAR#9000483510: RTL: SS : USB3 reset event may
4098 		 * not be generated always when the link enters poll
4099 		 */
4100 		if (DWC3_VER_IS_PRIOR(DWC3, 190A))
4101 			dwc3_gadget_reset_interrupt(dwc);
4102 
4103 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4104 		dwc->gadget->ep0->maxpacket = 512;
4105 		dwc->gadget->speed = USB_SPEED_SUPER;
4106 
4107 		if (lanes > 1) {
4108 			dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4109 			dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
4110 		}
4111 		break;
4112 	case DWC3_DSTS_HIGHSPEED:
4113 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4114 		dwc->gadget->ep0->maxpacket = 64;
4115 		dwc->gadget->speed = USB_SPEED_HIGH;
4116 		break;
4117 	case DWC3_DSTS_FULLSPEED:
4118 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4119 		dwc->gadget->ep0->maxpacket = 64;
4120 		dwc->gadget->speed = USB_SPEED_FULL;
4121 		break;
4122 	}
4123 
4124 	dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
4125 
4126 	/* Enable USB2 LPM Capability */
4127 
4128 	if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
4129 	    !dwc->usb2_gadget_lpm_disable &&
4130 	    (speed != DWC3_DSTS_SUPERSPEED) &&
4131 	    (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
4132 		reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4133 		reg |= DWC3_DCFG_LPM_CAP;
4134 		dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4135 
4136 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4137 		reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
4138 
4139 		reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
4140 					    (dwc->is_utmi_l1_suspend << 4));
4141 
4142 		/*
4143 		 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
4144 		 * DCFG.LPMCap is set, core responses with an ACK and the
4145 		 * BESL value in the LPM token is less than or equal to LPM
4146 		 * NYET threshold.
4147 		 */
4148 		WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
4149 				"LPM Erratum not available on dwc3 revisions < 2.40a\n");
4150 
4151 		if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
4152 			reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
4153 
4154 		dwc3_gadget_dctl_write_safe(dwc, reg);
4155 	} else {
4156 		if (dwc->usb2_gadget_lpm_disable) {
4157 			reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4158 			reg &= ~DWC3_DCFG_LPM_CAP;
4159 			dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4160 		}
4161 
4162 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4163 		reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
4164 		dwc3_gadget_dctl_write_safe(dwc, reg);
4165 	}
4166 
4167 	dep = dwc->eps[0];
4168 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
4169 	if (ret) {
4170 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
4171 		return;
4172 	}
4173 
4174 	dep = dwc->eps[1];
4175 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
4176 	if (ret) {
4177 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
4178 		return;
4179 	}
4180 
4181 	/*
4182 	 * Configure PHY via GUSB3PIPECTLn if required.
4183 	 *
4184 	 * Update GTXFIFOSIZn
4185 	 *
4186 	 * In both cases reset values should be sufficient.
4187 	 */
4188 }
4189 
4190 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo)
4191 {
4192 	dwc->suspended = false;
4193 
4194 	/*
4195 	 * TODO take core out of low power mode when that's
4196 	 * implemented.
4197 	 */
4198 
4199 	if (dwc->async_callbacks && dwc->gadget_driver->resume) {
4200 		spin_unlock(&dwc->lock);
4201 		dwc->gadget_driver->resume(dwc->gadget);
4202 		spin_lock(&dwc->lock);
4203 	}
4204 
4205 	dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
4206 }
4207 
4208 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
4209 		unsigned int evtinfo)
4210 {
4211 	enum dwc3_link_state	next = evtinfo & DWC3_LINK_STATE_MASK;
4212 	unsigned int		pwropt;
4213 
4214 	/*
4215 	 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
4216 	 * Hibernation mode enabled which would show up when device detects
4217 	 * host-initiated U3 exit.
4218 	 *
4219 	 * In that case, device will generate a Link State Change Interrupt
4220 	 * from U3 to RESUME which is only necessary if Hibernation is
4221 	 * configured in.
4222 	 *
4223 	 * There are no functional changes due to such spurious event and we
4224 	 * just need to ignore it.
4225 	 *
4226 	 * Refers to:
4227 	 *
4228 	 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
4229 	 * operational mode
4230 	 */
4231 	pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
4232 	if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
4233 			(pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
4234 		if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
4235 				(next == DWC3_LINK_STATE_RESUME)) {
4236 			return;
4237 		}
4238 	}
4239 
4240 	/*
4241 	 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
4242 	 * on the link partner, the USB session might do multiple entry/exit
4243 	 * of low power states before a transfer takes place.
4244 	 *
4245 	 * Due to this problem, we might experience lower throughput. The
4246 	 * suggested workaround is to disable DCTL[12:9] bits if we're
4247 	 * transitioning from U1/U2 to U0 and enable those bits again
4248 	 * after a transfer completes and there are no pending transfers
4249 	 * on any of the enabled endpoints.
4250 	 *
4251 	 * This is the first half of that workaround.
4252 	 *
4253 	 * Refers to:
4254 	 *
4255 	 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
4256 	 * core send LGO_Ux entering U0
4257 	 */
4258 	if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
4259 		if (next == DWC3_LINK_STATE_U0) {
4260 			u32	u1u2;
4261 			u32	reg;
4262 
4263 			switch (dwc->link_state) {
4264 			case DWC3_LINK_STATE_U1:
4265 			case DWC3_LINK_STATE_U2:
4266 				reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4267 				u1u2 = reg & (DWC3_DCTL_INITU2ENA
4268 						| DWC3_DCTL_ACCEPTU2ENA
4269 						| DWC3_DCTL_INITU1ENA
4270 						| DWC3_DCTL_ACCEPTU1ENA);
4271 
4272 				if (!dwc->u1u2)
4273 					dwc->u1u2 = reg & u1u2;
4274 
4275 				reg &= ~u1u2;
4276 
4277 				dwc3_gadget_dctl_write_safe(dwc, reg);
4278 				break;
4279 			default:
4280 				/* do nothing */
4281 				break;
4282 			}
4283 		}
4284 	}
4285 
4286 	switch (next) {
4287 	case DWC3_LINK_STATE_U0:
4288 		if (dwc->gadget->wakeup_armed) {
4289 			dwc3_gadget_enable_linksts_evts(dwc, false);
4290 			dwc3_resume_gadget(dwc);
4291 			dwc->suspended = false;
4292 		}
4293 		break;
4294 	case DWC3_LINK_STATE_U1:
4295 		if (dwc->speed == USB_SPEED_SUPER)
4296 			dwc3_suspend_gadget(dwc);
4297 		break;
4298 	case DWC3_LINK_STATE_U2:
4299 	case DWC3_LINK_STATE_U3:
4300 		dwc3_suspend_gadget(dwc);
4301 		break;
4302 	case DWC3_LINK_STATE_RESUME:
4303 		dwc3_resume_gadget(dwc);
4304 		break;
4305 	default:
4306 		/* do nothing */
4307 		break;
4308 	}
4309 
4310 	dwc->link_state = next;
4311 }
4312 
4313 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
4314 					  unsigned int evtinfo)
4315 {
4316 	enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
4317 
4318 	if (!dwc->suspended && next == DWC3_LINK_STATE_U3) {
4319 		dwc->suspended = true;
4320 		dwc3_suspend_gadget(dwc);
4321 	}
4322 
4323 	dwc->link_state = next;
4324 }
4325 
4326 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
4327 		const struct dwc3_event_devt *event)
4328 {
4329 	switch (event->type) {
4330 	case DWC3_DEVICE_EVENT_DISCONNECT:
4331 		dwc3_gadget_disconnect_interrupt(dwc);
4332 		break;
4333 	case DWC3_DEVICE_EVENT_RESET:
4334 		dwc3_gadget_reset_interrupt(dwc);
4335 		break;
4336 	case DWC3_DEVICE_EVENT_CONNECT_DONE:
4337 		dwc3_gadget_conndone_interrupt(dwc);
4338 		break;
4339 	case DWC3_DEVICE_EVENT_WAKEUP:
4340 		dwc3_gadget_wakeup_interrupt(dwc, event->event_info);
4341 		break;
4342 	case DWC3_DEVICE_EVENT_HIBER_REQ:
4343 		dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n");
4344 		break;
4345 	case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
4346 		dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
4347 		break;
4348 	case DWC3_DEVICE_EVENT_SUSPEND:
4349 		/* It changed to be suspend event for version 2.30a and above */
4350 		if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
4351 			dwc3_gadget_suspend_interrupt(dwc, event->event_info);
4352 		break;
4353 	case DWC3_DEVICE_EVENT_SOF:
4354 	case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
4355 	case DWC3_DEVICE_EVENT_CMD_CMPL:
4356 	case DWC3_DEVICE_EVENT_OVERFLOW:
4357 		break;
4358 	default:
4359 		dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
4360 	}
4361 }
4362 
4363 static void dwc3_process_event_entry(struct dwc3 *dwc,
4364 		const union dwc3_event *event)
4365 {
4366 	trace_dwc3_event(event->raw, dwc);
4367 
4368 	if (!event->type.is_devspec)
4369 		dwc3_endpoint_interrupt(dwc, &event->depevt);
4370 	else if (event->type.type == DWC3_EVENT_TYPE_DEV)
4371 		dwc3_gadget_interrupt(dwc, &event->devt);
4372 	else
4373 		dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
4374 }
4375 
4376 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
4377 {
4378 	struct dwc3 *dwc = evt->dwc;
4379 	irqreturn_t ret = IRQ_NONE;
4380 	int left;
4381 
4382 	left = evt->count;
4383 
4384 	if (!(evt->flags & DWC3_EVENT_PENDING))
4385 		return IRQ_NONE;
4386 
4387 	while (left > 0) {
4388 		union dwc3_event event;
4389 
4390 		event.raw = *(u32 *) (evt->cache + evt->lpos);
4391 
4392 		dwc3_process_event_entry(dwc, &event);
4393 
4394 		/*
4395 		 * FIXME we wrap around correctly to the next entry as
4396 		 * almost all entries are 4 bytes in size. There is one
4397 		 * entry which has 12 bytes which is a regular entry
4398 		 * followed by 8 bytes data. ATM I don't know how
4399 		 * things are organized if we get next to the a
4400 		 * boundary so I worry about that once we try to handle
4401 		 * that.
4402 		 */
4403 		evt->lpos = (evt->lpos + 4) % evt->length;
4404 		left -= 4;
4405 	}
4406 
4407 	evt->count = 0;
4408 	ret = IRQ_HANDLED;
4409 
4410 	/* Unmask interrupt */
4411 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4412 		    DWC3_GEVNTSIZ_SIZE(evt->length));
4413 
4414 	if (dwc->imod_interval) {
4415 		dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
4416 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
4417 	}
4418 
4419 	/* Keep the clearing of DWC3_EVENT_PENDING at the end */
4420 	evt->flags &= ~DWC3_EVENT_PENDING;
4421 
4422 	return ret;
4423 }
4424 
4425 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
4426 {
4427 	struct dwc3_event_buffer *evt = _evt;
4428 	struct dwc3 *dwc = evt->dwc;
4429 	unsigned long flags;
4430 	irqreturn_t ret = IRQ_NONE;
4431 
4432 	local_bh_disable();
4433 	spin_lock_irqsave(&dwc->lock, flags);
4434 	ret = dwc3_process_event_buf(evt);
4435 	spin_unlock_irqrestore(&dwc->lock, flags);
4436 	local_bh_enable();
4437 
4438 	return ret;
4439 }
4440 
4441 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
4442 {
4443 	struct dwc3 *dwc = evt->dwc;
4444 	u32 amount;
4445 	u32 count;
4446 
4447 	if (pm_runtime_suspended(dwc->dev)) {
4448 		pm_runtime_get(dwc->dev);
4449 		disable_irq_nosync(dwc->irq_gadget);
4450 		dwc->pending_events = true;
4451 		return IRQ_HANDLED;
4452 	}
4453 
4454 	/*
4455 	 * With PCIe legacy interrupt, test shows that top-half irq handler can
4456 	 * be called again after HW interrupt deassertion. Check if bottom-half
4457 	 * irq event handler completes before caching new event to prevent
4458 	 * losing events.
4459 	 */
4460 	if (evt->flags & DWC3_EVENT_PENDING)
4461 		return IRQ_HANDLED;
4462 
4463 	count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
4464 	count &= DWC3_GEVNTCOUNT_MASK;
4465 	if (!count)
4466 		return IRQ_NONE;
4467 
4468 	evt->count = count;
4469 	evt->flags |= DWC3_EVENT_PENDING;
4470 
4471 	/* Mask interrupt */
4472 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4473 		    DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
4474 
4475 	amount = min(count, evt->length - evt->lpos);
4476 	memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
4477 
4478 	if (amount < count)
4479 		memcpy(evt->cache, evt->buf, count - amount);
4480 
4481 	dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
4482 
4483 	return IRQ_WAKE_THREAD;
4484 }
4485 
4486 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
4487 {
4488 	struct dwc3_event_buffer	*evt = _evt;
4489 
4490 	return dwc3_check_event_buf(evt);
4491 }
4492 
4493 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
4494 {
4495 	struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
4496 	int irq;
4497 
4498 	irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
4499 	if (irq > 0)
4500 		goto out;
4501 
4502 	if (irq == -EPROBE_DEFER)
4503 		goto out;
4504 
4505 	irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
4506 	if (irq > 0)
4507 		goto out;
4508 
4509 	if (irq == -EPROBE_DEFER)
4510 		goto out;
4511 
4512 	irq = platform_get_irq(dwc3_pdev, 0);
4513 
4514 out:
4515 	return irq;
4516 }
4517 
4518 static void dwc_gadget_release(struct device *dev)
4519 {
4520 	struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4521 
4522 	kfree(gadget);
4523 }
4524 
4525 /**
4526  * dwc3_gadget_init - initializes gadget related registers
4527  * @dwc: pointer to our controller context structure
4528  *
4529  * Returns 0 on success otherwise negative errno.
4530  */
4531 int dwc3_gadget_init(struct dwc3 *dwc)
4532 {
4533 	int ret;
4534 	int irq;
4535 	struct device *dev;
4536 
4537 	irq = dwc3_gadget_get_irq(dwc);
4538 	if (irq < 0) {
4539 		ret = irq;
4540 		goto err0;
4541 	}
4542 
4543 	dwc->irq_gadget = irq;
4544 
4545 	dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
4546 					  sizeof(*dwc->ep0_trb) * 2,
4547 					  &dwc->ep0_trb_addr, GFP_KERNEL);
4548 	if (!dwc->ep0_trb) {
4549 		dev_err(dwc->dev, "failed to allocate ep0 trb\n");
4550 		ret = -ENOMEM;
4551 		goto err0;
4552 	}
4553 
4554 	dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
4555 	if (!dwc->setup_buf) {
4556 		ret = -ENOMEM;
4557 		goto err1;
4558 	}
4559 
4560 	dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
4561 			&dwc->bounce_addr, GFP_KERNEL);
4562 	if (!dwc->bounce) {
4563 		ret = -ENOMEM;
4564 		goto err2;
4565 	}
4566 
4567 	init_completion(&dwc->ep0_in_setup);
4568 	dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4569 	if (!dwc->gadget) {
4570 		ret = -ENOMEM;
4571 		goto err3;
4572 	}
4573 
4574 
4575 	usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4576 	dev				= &dwc->gadget->dev;
4577 	dev->platform_data		= dwc;
4578 	dwc->gadget->ops		= &dwc3_gadget_ops;
4579 	dwc->gadget->speed		= USB_SPEED_UNKNOWN;
4580 	dwc->gadget->ssp_rate		= USB_SSP_GEN_UNKNOWN;
4581 	dwc->gadget->sg_supported	= true;
4582 	dwc->gadget->name		= "dwc3-gadget";
4583 	dwc->gadget->lpm_capable	= !dwc->usb2_gadget_lpm_disable;
4584 	dwc->gadget->wakeup_capable	= true;
4585 
4586 	/*
4587 	 * FIXME We might be setting max_speed to <SUPER, however versions
4588 	 * <2.20a of dwc3 have an issue with metastability (documented
4589 	 * elsewhere in this driver) which tells us we can't set max speed to
4590 	 * anything lower than SUPER.
4591 	 *
4592 	 * Because gadget.max_speed is only used by composite.c and function
4593 	 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4594 	 * to happen so we avoid sending SuperSpeed Capability descriptor
4595 	 * together with our BOS descriptor as that could confuse host into
4596 	 * thinking we can handle super speed.
4597 	 *
4598 	 * Note that, in fact, we won't even support GetBOS requests when speed
4599 	 * is less than super speed because we don't have means, yet, to tell
4600 	 * composite.c that we are USB 2.0 + LPM ECN.
4601 	 */
4602 	if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
4603 	    !dwc->dis_metastability_quirk)
4604 		dev_info(dwc->dev, "changing max_speed on rev %08x\n",
4605 				dwc->revision);
4606 
4607 	dwc->gadget->max_speed		= dwc->maximum_speed;
4608 	dwc->gadget->max_ssp_rate	= dwc->max_ssp_rate;
4609 
4610 	/*
4611 	 * REVISIT: Here we should clear all pending IRQs to be
4612 	 * sure we're starting from a well known location.
4613 	 */
4614 
4615 	ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
4616 	if (ret)
4617 		goto err4;
4618 
4619 	ret = usb_add_gadget(dwc->gadget);
4620 	if (ret) {
4621 		dev_err(dwc->dev, "failed to add gadget\n");
4622 		goto err5;
4623 	}
4624 
4625 	if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4626 		dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4627 	else
4628 		dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
4629 
4630 	return 0;
4631 
4632 err5:
4633 	dwc3_gadget_free_endpoints(dwc);
4634 err4:
4635 	usb_put_gadget(dwc->gadget);
4636 	dwc->gadget = NULL;
4637 err3:
4638 	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4639 			dwc->bounce_addr);
4640 
4641 err2:
4642 	kfree(dwc->setup_buf);
4643 
4644 err1:
4645 	dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4646 			dwc->ep0_trb, dwc->ep0_trb_addr);
4647 
4648 err0:
4649 	return ret;
4650 }
4651 
4652 /* -------------------------------------------------------------------------- */
4653 
4654 void dwc3_gadget_exit(struct dwc3 *dwc)
4655 {
4656 	if (!dwc->gadget)
4657 		return;
4658 
4659 	usb_del_gadget(dwc->gadget);
4660 	dwc3_gadget_free_endpoints(dwc);
4661 	usb_put_gadget(dwc->gadget);
4662 	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4663 			  dwc->bounce_addr);
4664 	kfree(dwc->setup_buf);
4665 	dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4666 			  dwc->ep0_trb, dwc->ep0_trb_addr);
4667 }
4668 
4669 int dwc3_gadget_suspend(struct dwc3 *dwc)
4670 {
4671 	unsigned long flags;
4672 	int ret;
4673 
4674 	if (!dwc->gadget_driver)
4675 		return 0;
4676 
4677 	ret = dwc3_gadget_soft_disconnect(dwc);
4678 	if (ret)
4679 		goto err;
4680 
4681 	spin_lock_irqsave(&dwc->lock, flags);
4682 	dwc3_disconnect_gadget(dwc);
4683 	spin_unlock_irqrestore(&dwc->lock, flags);
4684 
4685 	return 0;
4686 
4687 err:
4688 	/*
4689 	 * Attempt to reset the controller's state. Likely no
4690 	 * communication can be established until the host
4691 	 * performs a port reset.
4692 	 */
4693 	if (dwc->softconnect)
4694 		dwc3_gadget_soft_connect(dwc);
4695 
4696 	return ret;
4697 }
4698 
4699 int dwc3_gadget_resume(struct dwc3 *dwc)
4700 {
4701 	if (!dwc->gadget_driver || !dwc->softconnect)
4702 		return 0;
4703 
4704 	return dwc3_gadget_soft_connect(dwc);
4705 }
4706 
4707 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4708 {
4709 	if (dwc->pending_events) {
4710 		dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4711 		dwc->pending_events = false;
4712 		enable_irq(dwc->irq_gadget);
4713 	}
4714 }
4715