net: DCB: Validate DCB_ATTR_DCB_BUFFER argument
[linux/fpc-iii.git] / drivers / usb / dwc3 / gadget.c
blob422554434251900fb5f64fed5f931c0453674a59
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
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>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
25 #include "debug.h"
26 #include "core.h"
27 #include "gadget.h"
28 #include "io.h"
30 #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
31 & ~((d)->interval - 1))
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)
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
43 u32 reg;
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
48 switch (mode) {
49 case TEST_J:
50 case TEST_K:
51 case TEST_SE0_NAK:
52 case TEST_PACKET:
53 case TEST_FORCE_EN:
54 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
60 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
62 return 0;
65 /**
66 * dwc3_gadget_get_link_state - gets current state of usb link
67 * @dwc: pointer to our context structure
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
72 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
74 u32 reg;
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
78 return DWC3_DSTS_USBLNKST(reg);
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
86 * Caller should take care of locking. This function will
87 * return 0 on success or -ETIMEDOUT.
89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
91 int retries = 10000;
92 u32 reg;
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
98 if (dwc->revision >= DWC3_REVISION_194A) {
99 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
107 if (retries <= 0)
108 return -ETIMEDOUT;
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
114 /* set requested state */
115 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
122 if (dwc->revision >= DWC3_REVISION_194A)
123 return 0;
125 /* wait for a change in DSTS */
126 retries = 10000;
127 while (--retries) {
128 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
130 if (DWC3_DSTS_USBLNKST(reg) == state)
131 return 0;
133 udelay(5);
136 return -ETIMEDOUT;
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
143 * The index should never point to the link TRB. After incrementing,
144 * if it is point to the link TRB, wrap around to the beginning. The
145 * link TRB is always at the last TRB entry.
147 static void dwc3_ep_inc_trb(u8 *index)
149 (*index)++;
150 if (*index == (DWC3_TRB_NUM - 1))
151 *index = 0;
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
160 dwc3_ep_inc_trb(&dep->trb_enqueue);
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
169 dwc3_ep_inc_trb(&dep->trb_dequeue);
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173 struct dwc3_request *req, int status)
175 struct dwc3 *dwc = dep->dwc;
177 list_del(&req->list);
178 req->remaining = 0;
179 req->needs_extra_trb = false;
181 if (req->request.status == -EINPROGRESS)
182 req->request.status = status;
184 if (req->trb)
185 usb_gadget_unmap_request_by_dev(dwc->sysdev,
186 &req->request, req->direction);
188 req->trb = NULL;
189 trace_dwc3_gadget_giveback(req);
191 if (dep->number > 1)
192 pm_runtime_put(dwc->dev);
196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
197 * @dep: The endpoint to whom the request belongs to
198 * @req: The request we're giving back
199 * @status: completion code for the request
201 * Must be called with controller's lock held and interrupts disabled. This
202 * function will unmap @req and call its ->complete() callback to notify upper
203 * layers that it has completed.
205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
206 int status)
208 struct dwc3 *dwc = dep->dwc;
210 dwc3_gadget_del_and_unmap_request(dep, req, status);
211 req->status = DWC3_REQUEST_STATUS_COMPLETED;
213 spin_unlock(&dwc->lock);
214 usb_gadget_giveback_request(&dep->endpoint, &req->request);
215 spin_lock(&dwc->lock);
219 * dwc3_send_gadget_generic_command - issue a generic command for the controller
220 * @dwc: pointer to the controller context
221 * @cmd: the command to be issued
222 * @param: command parameter
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
227 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
229 u32 timeout = 500;
230 int status = 0;
231 int ret = 0;
232 u32 reg;
234 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
237 do {
238 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
239 if (!(reg & DWC3_DGCMD_CMDACT)) {
240 status = DWC3_DGCMD_STATUS(reg);
241 if (status)
242 ret = -EINVAL;
243 break;
245 } while (--timeout);
247 if (!timeout) {
248 ret = -ETIMEDOUT;
249 status = -ETIMEDOUT;
252 trace_dwc3_gadget_generic_cmd(cmd, param, status);
254 return ret;
257 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
260 * dwc3_send_gadget_ep_cmd - issue an endpoint command
261 * @dep: the endpoint to which the command is going to be issued
262 * @cmd: the command to be issued
263 * @params: parameters to the command
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
268 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269 struct dwc3_gadget_ep_cmd_params *params)
271 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
272 struct dwc3 *dwc = dep->dwc;
273 u32 timeout = 1000;
274 u32 saved_config = 0;
275 u32 reg;
277 int cmd_status = 0;
278 int ret = -EINVAL;
281 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
282 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
283 * endpoint command.
285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
290 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
293 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
294 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
297 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
302 if (saved_config)
303 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
306 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
307 int needs_wakeup;
309 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
310 dwc->link_state == DWC3_LINK_STATE_U2 ||
311 dwc->link_state == DWC3_LINK_STATE_U3);
313 if (unlikely(needs_wakeup)) {
314 ret = __dwc3_gadget_wakeup(dwc);
315 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316 ret);
320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
322 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
325 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
326 * not relying on XferNotReady, we can make use of a special "No
327 * Response Update Transfer" command where we should clear both CmdAct
328 * and CmdIOC bits.
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
333 * NOTICE: We're making an assumption that control endpoints will never
334 * make use of Update Transfer command. This is a safe assumption
335 * because we can never have more than one request at a time with
336 * Control Endpoints. If anybody changes that assumption, this chunk
337 * needs to be updated accordingly.
339 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
340 !usb_endpoint_xfer_isoc(desc))
341 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
342 else
343 cmd |= DWC3_DEPCMD_CMDACT;
345 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
346 do {
347 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
348 if (!(reg & DWC3_DEPCMD_CMDACT)) {
349 cmd_status = DWC3_DEPCMD_STATUS(reg);
351 switch (cmd_status) {
352 case 0:
353 ret = 0;
354 break;
355 case DEPEVT_TRANSFER_NO_RESOURCE:
356 ret = -EINVAL;
357 break;
358 case DEPEVT_TRANSFER_BUS_EXPIRY:
360 * SW issues START TRANSFER command to
361 * isochronous ep with future frame interval. If
362 * future interval time has already passed when
363 * core receives the command, it will respond
364 * with an error status of 'Bus Expiry'.
366 * Instead of always returning -EINVAL, let's
367 * give a hint to the gadget driver that this is
368 * the case by returning -EAGAIN.
370 ret = -EAGAIN;
371 break;
372 default:
373 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
376 break;
378 } while (--timeout);
380 if (timeout == 0) {
381 ret = -ETIMEDOUT;
382 cmd_status = -ETIMEDOUT;
385 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
387 if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
388 dep->flags |= DWC3_EP_TRANSFER_STARTED;
389 dwc3_gadget_ep_get_transfer_index(dep);
392 if (saved_config) {
393 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
394 reg |= saved_config;
395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
398 return ret;
401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
403 struct dwc3 *dwc = dep->dwc;
404 struct dwc3_gadget_ep_cmd_params params;
405 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
408 * As of core revision 2.60a the recommended programming model
409 * is to set the ClearPendIN bit when issuing a Clear Stall EP
410 * command for IN endpoints. This is to prevent an issue where
411 * some (non-compliant) hosts may not send ACK TPs for pending
412 * IN transfers due to a mishandled error condition. Synopsys
413 * STAR 9000614252.
415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416 (dwc->gadget.speed >= USB_SPEED_SUPER))
417 cmd |= DWC3_DEPCMD_CLEARPENDIN;
419 memset(&params, 0, sizeof(params));
421 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
424 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
425 struct dwc3_trb *trb)
427 u32 offset = (char *) trb - (char *) dep->trb_pool;
429 return dep->trb_pool_dma + offset;
432 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
434 struct dwc3 *dwc = dep->dwc;
436 if (dep->trb_pool)
437 return 0;
439 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
440 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
441 &dep->trb_pool_dma, GFP_KERNEL);
442 if (!dep->trb_pool) {
443 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
444 dep->name);
445 return -ENOMEM;
448 return 0;
451 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
453 struct dwc3 *dwc = dep->dwc;
455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
456 dep->trb_pool, dep->trb_pool_dma);
458 dep->trb_pool = NULL;
459 dep->trb_pool_dma = 0;
462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
464 struct dwc3_gadget_ep_cmd_params params;
466 memset(&params, 0x00, sizeof(params));
468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471 &params);
475 * dwc3_gadget_start_config - configure ep resources
476 * @dep: endpoint that is being enabled
478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
481 * The assignment of transfer resources cannot perfectly follow the data book
482 * due to the fact that the controller driver does not have all knowledge of the
483 * configuration in advance. It is given this information piecemeal by the
484 * composite gadget framework after every SET_CONFIGURATION and
485 * SET_INTERFACE. Trying to follow the databook programming model in this
486 * scenario can cause errors. For two reasons:
488 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
489 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
490 * incorrect in the scenario of multiple interfaces.
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493 * endpoint on alt setting (8.1.6).
495 * The following simplified method is used instead:
497 * All hardware endpoints can be assigned a transfer resource and this setting
498 * will stay persistent until either a core reset or hibernation. So whenever we
499 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
500 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
501 * guaranteed that there are as many transfer resources as endpoints.
503 * This function is called for each endpoint when it is being enabled but is
504 * triggered only when called for EP0-out, which always happens first, and which
505 * should only happen in one of the above conditions.
507 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
509 struct dwc3_gadget_ep_cmd_params params;
510 struct dwc3 *dwc;
511 u32 cmd;
512 int i;
513 int ret;
515 if (dep->number)
516 return 0;
518 memset(&params, 0x00, sizeof(params));
519 cmd = DWC3_DEPCMD_DEPSTARTCFG;
520 dwc = dep->dwc;
522 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
523 if (ret)
524 return ret;
526 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527 struct dwc3_ep *dep = dwc->eps[i];
529 if (!dep)
530 continue;
532 ret = dwc3_gadget_set_xfer_resource(dep);
533 if (ret)
534 return ret;
537 return 0;
540 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
542 const struct usb_ss_ep_comp_descriptor *comp_desc;
543 const struct usb_endpoint_descriptor *desc;
544 struct dwc3_gadget_ep_cmd_params params;
545 struct dwc3 *dwc = dep->dwc;
547 comp_desc = dep->endpoint.comp_desc;
548 desc = dep->endpoint.desc;
550 memset(&params, 0x00, sizeof(params));
552 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
555 /* Burst size is only needed in SuperSpeed mode */
556 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
557 u32 burst = dep->endpoint.maxburst;
558 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
561 params.param0 |= action;
562 if (action == DWC3_DEPCFG_ACTION_RESTORE)
563 params.param2 |= dep->saved_state;
565 if (usb_endpoint_xfer_control(desc))
566 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
568 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
571 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
572 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
573 | DWC3_DEPCFG_STREAM_EVENT_EN;
574 dep->stream_capable = true;
577 if (!usb_endpoint_xfer_control(desc))
578 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
581 * We are doing 1:1 mapping for endpoints, meaning
582 * Physical Endpoints 2 maps to Logical Endpoint 2 and
583 * so on. We consider the direction bit as part of the physical
584 * endpoint number. So USB endpoint 0x81 is 0x03.
586 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
589 * We must use the lower 16 TX FIFOs even though
590 * HW might have more
592 if (dep->direction)
593 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
595 if (desc->bInterval) {
596 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
597 dep->interval = 1 << (desc->bInterval - 1);
600 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
605 * @dep: endpoint to be initialized
606 * @action: one of INIT, MODIFY or RESTORE
608 * Caller should take care of locking. Execute all necessary commands to
609 * initialize a HW endpoint so it can be used by a gadget driver.
611 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
613 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
614 struct dwc3 *dwc = dep->dwc;
616 u32 reg;
617 int ret;
619 if (!(dep->flags & DWC3_EP_ENABLED)) {
620 ret = dwc3_gadget_start_config(dep);
621 if (ret)
622 return ret;
625 ret = dwc3_gadget_set_ep_config(dep, action);
626 if (ret)
627 return ret;
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
630 struct dwc3_trb *trb_st_hw;
631 struct dwc3_trb *trb_link;
633 dep->type = usb_endpoint_type(desc);
634 dep->flags |= DWC3_EP_ENABLED;
636 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
637 reg |= DWC3_DALEPENA_EP(dep->number);
638 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
640 if (usb_endpoint_xfer_control(desc))
641 goto out;
643 /* Initialize the TRB ring */
644 dep->trb_dequeue = 0;
645 dep->trb_enqueue = 0;
646 memset(dep->trb_pool, 0,
647 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
649 /* Link TRB. The HWO bit is never reset */
650 trb_st_hw = &dep->trb_pool[0];
652 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
653 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
660 * Issue StartTransfer here with no-op TRB so we can always rely on No
661 * Response Update Transfer command.
663 if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
664 usb_endpoint_xfer_int(desc)) {
665 struct dwc3_gadget_ep_cmd_params params;
666 struct dwc3_trb *trb;
667 dma_addr_t trb_dma;
668 u32 cmd;
670 memset(&params, 0, sizeof(params));
671 trb = &dep->trb_pool[0];
672 trb_dma = dwc3_trb_dma_offset(dep, trb);
674 params.param0 = upper_32_bits(trb_dma);
675 params.param1 = lower_32_bits(trb_dma);
677 cmd = DWC3_DEPCMD_STARTTRANSFER;
679 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
680 if (ret < 0)
681 return ret;
684 out:
685 trace_dwc3_gadget_ep_enable(dep);
687 return 0;
690 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
691 bool interrupt);
692 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
694 struct dwc3_request *req;
696 dwc3_stop_active_transfer(dep, true, false);
698 /* - giveback all requests to gadget driver */
699 while (!list_empty(&dep->started_list)) {
700 req = next_request(&dep->started_list);
702 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
705 while (!list_empty(&dep->pending_list)) {
706 req = next_request(&dep->pending_list);
708 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
711 while (!list_empty(&dep->cancelled_list)) {
712 req = next_request(&dep->cancelled_list);
714 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
719 * __dwc3_gadget_ep_disable - disables a hw endpoint
720 * @dep: the endpoint to disable
722 * This function undoes what __dwc3_gadget_ep_enable did and also removes
723 * requests which are currently being processed by the hardware and those which
724 * are not yet scheduled.
726 * Caller should take care of locking.
728 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
730 struct dwc3 *dwc = dep->dwc;
731 u32 reg;
733 trace_dwc3_gadget_ep_disable(dep);
735 dwc3_remove_requests(dwc, dep);
737 /* make sure HW endpoint isn't stalled */
738 if (dep->flags & DWC3_EP_STALL)
739 __dwc3_gadget_ep_set_halt(dep, 0, false);
741 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
742 reg &= ~DWC3_DALEPENA_EP(dep->number);
743 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
745 dep->stream_capable = false;
746 dep->type = 0;
747 dep->flags = 0;
749 /* Clear out the ep descriptors for non-ep0 */
750 if (dep->number > 1) {
751 dep->endpoint.comp_desc = NULL;
752 dep->endpoint.desc = NULL;
755 return 0;
758 /* -------------------------------------------------------------------------- */
760 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
761 const struct usb_endpoint_descriptor *desc)
763 return -EINVAL;
766 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
768 return -EINVAL;
771 /* -------------------------------------------------------------------------- */
773 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
774 const struct usb_endpoint_descriptor *desc)
776 struct dwc3_ep *dep;
777 struct dwc3 *dwc;
778 unsigned long flags;
779 int ret;
781 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
782 pr_debug("dwc3: invalid parameters\n");
783 return -EINVAL;
786 if (!desc->wMaxPacketSize) {
787 pr_debug("dwc3: missing wMaxPacketSize\n");
788 return -EINVAL;
791 dep = to_dwc3_ep(ep);
792 dwc = dep->dwc;
794 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
795 "%s is already enabled\n",
796 dep->name))
797 return 0;
799 spin_lock_irqsave(&dwc->lock, flags);
800 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
801 spin_unlock_irqrestore(&dwc->lock, flags);
803 return ret;
806 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
808 struct dwc3_ep *dep;
809 struct dwc3 *dwc;
810 unsigned long flags;
811 int ret;
813 if (!ep) {
814 pr_debug("dwc3: invalid parameters\n");
815 return -EINVAL;
818 dep = to_dwc3_ep(ep);
819 dwc = dep->dwc;
821 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
822 "%s is already disabled\n",
823 dep->name))
824 return 0;
826 spin_lock_irqsave(&dwc->lock, flags);
827 ret = __dwc3_gadget_ep_disable(dep);
828 spin_unlock_irqrestore(&dwc->lock, flags);
830 return ret;
833 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
834 gfp_t gfp_flags)
836 struct dwc3_request *req;
837 struct dwc3_ep *dep = to_dwc3_ep(ep);
839 req = kzalloc(sizeof(*req), gfp_flags);
840 if (!req)
841 return NULL;
843 req->direction = dep->direction;
844 req->epnum = dep->number;
845 req->dep = dep;
846 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
848 trace_dwc3_alloc_request(req);
850 return &req->request;
853 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
854 struct usb_request *request)
856 struct dwc3_request *req = to_dwc3_request(request);
858 trace_dwc3_free_request(req);
859 kfree(req);
863 * dwc3_ep_prev_trb - returns the previous TRB in the ring
864 * @dep: The endpoint with the TRB ring
865 * @index: The index of the current TRB in the ring
867 * Returns the TRB prior to the one pointed to by the index. If the
868 * index is 0, we will wrap backwards, skip the link TRB, and return
869 * the one just before that.
871 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
873 u8 tmp = index;
875 if (!tmp)
876 tmp = DWC3_TRB_NUM - 1;
878 return &dep->trb_pool[tmp - 1];
881 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
883 struct dwc3_trb *tmp;
884 u8 trbs_left;
887 * If enqueue & dequeue are equal than it is either full or empty.
889 * One way to know for sure is if the TRB right before us has HWO bit
890 * set or not. If it has, then we're definitely full and can't fit any
891 * more transfers in our ring.
893 if (dep->trb_enqueue == dep->trb_dequeue) {
894 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
895 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
896 return 0;
898 return DWC3_TRB_NUM - 1;
901 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
902 trbs_left &= (DWC3_TRB_NUM - 1);
904 if (dep->trb_dequeue < dep->trb_enqueue)
905 trbs_left--;
907 return trbs_left;
910 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
911 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
912 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
914 struct dwc3 *dwc = dep->dwc;
915 struct usb_gadget *gadget = &dwc->gadget;
916 enum usb_device_speed speed = gadget->speed;
918 trb->size = DWC3_TRB_SIZE_LENGTH(length);
919 trb->bpl = lower_32_bits(dma);
920 trb->bph = upper_32_bits(dma);
922 switch (usb_endpoint_type(dep->endpoint.desc)) {
923 case USB_ENDPOINT_XFER_CONTROL:
924 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
925 break;
927 case USB_ENDPOINT_XFER_ISOC:
928 if (!node) {
929 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
932 * USB Specification 2.0 Section 5.9.2 states that: "If
933 * there is only a single transaction in the microframe,
934 * only a DATA0 data packet PID is used. If there are
935 * two transactions per microframe, DATA1 is used for
936 * the first transaction data packet and DATA0 is used
937 * for the second transaction data packet. If there are
938 * three transactions per microframe, DATA2 is used for
939 * the first transaction data packet, DATA1 is used for
940 * the second, and DATA0 is used for the third."
942 * IOW, we should satisfy the following cases:
944 * 1) length <= maxpacket
945 * - DATA0
947 * 2) maxpacket < length <= (2 * maxpacket)
948 * - DATA1, DATA0
950 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
951 * - DATA2, DATA1, DATA0
953 if (speed == USB_SPEED_HIGH) {
954 struct usb_ep *ep = &dep->endpoint;
955 unsigned int mult = 2;
956 unsigned int maxp = usb_endpoint_maxp(ep->desc);
958 if (length <= (2 * maxp))
959 mult--;
961 if (length <= maxp)
962 mult--;
964 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
966 } else {
967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
970 /* always enable Interrupt on Missed ISOC */
971 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
972 break;
974 case USB_ENDPOINT_XFER_BULK:
975 case USB_ENDPOINT_XFER_INT:
976 trb->ctrl = DWC3_TRBCTL_NORMAL;
977 break;
978 default:
980 * This is only possible with faulty memory because we
981 * checked it already :)
983 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
984 usb_endpoint_type(dep->endpoint.desc));
988 * Enable Continue on Short Packet
989 * when endpoint is not a stream capable
991 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
992 if (!dep->stream_capable)
993 trb->ctrl |= DWC3_TRB_CTRL_CSP;
995 if (short_not_ok)
996 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
999 if ((!no_interrupt && !chain) ||
1000 (dwc3_calc_trbs_left(dep) == 1))
1001 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1003 if (chain)
1004 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1006 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1007 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1009 trb->ctrl |= DWC3_TRB_CTRL_HWO;
1011 dwc3_ep_inc_enq(dep);
1013 trace_dwc3_prepare_trb(dep, trb);
1017 * dwc3_prepare_one_trb - setup one TRB from one request
1018 * @dep: endpoint for which this request is prepared
1019 * @req: dwc3_request pointer
1020 * @trb_length: buffer size of the TRB
1021 * @chain: should this TRB be chained to the next?
1022 * @node: only for isochronous endpoints. First TRB needs different type.
1024 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1025 struct dwc3_request *req, unsigned int trb_length,
1026 unsigned chain, unsigned node)
1028 struct dwc3_trb *trb;
1029 dma_addr_t dma;
1030 unsigned stream_id = req->request.stream_id;
1031 unsigned short_not_ok = req->request.short_not_ok;
1032 unsigned no_interrupt = req->request.no_interrupt;
1034 if (req->request.num_sgs > 0)
1035 dma = sg_dma_address(req->start_sg);
1036 else
1037 dma = req->request.dma;
1039 trb = &dep->trb_pool[dep->trb_enqueue];
1041 if (!req->trb) {
1042 dwc3_gadget_move_started_request(req);
1043 req->trb = trb;
1044 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1047 req->num_trbs++;
1049 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
1050 stream_id, short_not_ok, no_interrupt);
1053 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1054 struct dwc3_request *req)
1056 struct scatterlist *sg = req->start_sg;
1057 struct scatterlist *s;
1058 int i;
1059 unsigned int length = req->request.length;
1060 unsigned int remaining = req->request.num_mapped_sgs
1061 - req->num_queued_sgs;
1064 * If we resume preparing the request, then get the remaining length of
1065 * the request and resume where we left off.
1067 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1068 length -= sg_dma_len(s);
1070 for_each_sg(sg, s, remaining, i) {
1071 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1072 unsigned int rem = length % maxp;
1073 unsigned int trb_length;
1074 unsigned chain = true;
1076 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1078 length -= trb_length;
1081 * IOMMU driver is coalescing the list of sgs which shares a
1082 * page boundary into one and giving it to USB driver. With
1083 * this the number of sgs mapped is not equal to the number of
1084 * sgs passed. So mark the chain bit to false if it isthe last
1085 * mapped sg.
1087 if ((i == remaining - 1) || !length)
1088 chain = false;
1090 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1091 struct dwc3 *dwc = dep->dwc;
1092 struct dwc3_trb *trb;
1094 req->needs_extra_trb = true;
1096 /* prepare normal TRB */
1097 dwc3_prepare_one_trb(dep, req, trb_length, true, i);
1099 /* Now prepare one extra TRB to align transfer size */
1100 trb = &dep->trb_pool[dep->trb_enqueue];
1101 req->num_trbs++;
1102 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1103 maxp - rem, false, 1,
1104 req->request.stream_id,
1105 req->request.short_not_ok,
1106 req->request.no_interrupt);
1107 } else if (req->request.zero && req->request.length &&
1108 !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1109 !rem && !chain) {
1110 struct dwc3 *dwc = dep->dwc;
1111 struct dwc3_trb *trb;
1113 req->needs_extra_trb = true;
1115 /* Prepare normal TRB */
1116 dwc3_prepare_one_trb(dep, req, trb_length, true, i);
1118 /* Prepare one extra TRB to handle ZLP */
1119 trb = &dep->trb_pool[dep->trb_enqueue];
1120 req->num_trbs++;
1121 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1122 !req->direction, 1,
1123 req->request.stream_id,
1124 req->request.short_not_ok,
1125 req->request.no_interrupt);
1127 /* Prepare one more TRB to handle MPS alignment */
1128 if (!req->direction) {
1129 trb = &dep->trb_pool[dep->trb_enqueue];
1130 req->num_trbs++;
1131 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp,
1132 false, 1, req->request.stream_id,
1133 req->request.short_not_ok,
1134 req->request.no_interrupt);
1136 } else {
1137 dwc3_prepare_one_trb(dep, req, trb_length, chain, i);
1141 * There can be a situation where all sgs in sglist are not
1142 * queued because of insufficient trb number. To handle this
1143 * case, update start_sg to next sg to be queued, so that
1144 * we have free trbs we can continue queuing from where we
1145 * previously stopped
1147 if (chain)
1148 req->start_sg = sg_next(s);
1150 req->num_queued_sgs++;
1153 * The number of pending SG entries may not correspond to the
1154 * number of mapped SG entries. If all the data are queued, then
1155 * don't include unused SG entries.
1157 if (length == 0) {
1158 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1159 break;
1162 if (!dwc3_calc_trbs_left(dep))
1163 break;
1167 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1168 struct dwc3_request *req)
1170 unsigned int length = req->request.length;
1171 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1172 unsigned int rem = length % maxp;
1174 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
1175 struct dwc3 *dwc = dep->dwc;
1176 struct dwc3_trb *trb;
1178 req->needs_extra_trb = true;
1180 /* prepare normal TRB */
1181 dwc3_prepare_one_trb(dep, req, length, true, 0);
1183 /* Now prepare one extra TRB to align transfer size */
1184 trb = &dep->trb_pool[dep->trb_enqueue];
1185 req->num_trbs++;
1186 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1187 false, 1, req->request.stream_id,
1188 req->request.short_not_ok,
1189 req->request.no_interrupt);
1190 } else if (req->request.zero && req->request.length &&
1191 !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1192 (IS_ALIGNED(req->request.length, maxp))) {
1193 struct dwc3 *dwc = dep->dwc;
1194 struct dwc3_trb *trb;
1196 req->needs_extra_trb = true;
1198 /* prepare normal TRB */
1199 dwc3_prepare_one_trb(dep, req, length, true, 0);
1201 /* Prepare one extra TRB to handle ZLP */
1202 trb = &dep->trb_pool[dep->trb_enqueue];
1203 req->num_trbs++;
1204 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1205 !req->direction, 1, req->request.stream_id,
1206 req->request.short_not_ok,
1207 req->request.no_interrupt);
1209 /* Prepare one more TRB to handle MPS alignment for OUT */
1210 if (!req->direction) {
1211 trb = &dep->trb_pool[dep->trb_enqueue];
1212 req->num_trbs++;
1213 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp,
1214 false, 1, req->request.stream_id,
1215 req->request.short_not_ok,
1216 req->request.no_interrupt);
1218 } else {
1219 dwc3_prepare_one_trb(dep, req, length, false, 0);
1224 * dwc3_prepare_trbs - setup TRBs from requests
1225 * @dep: endpoint for which requests are being prepared
1227 * The function goes through the requests list and sets up TRBs for the
1228 * transfers. The function returns once there are no more TRBs available or
1229 * it runs out of requests.
1231 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1233 struct dwc3_request *req, *n;
1235 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1238 * We can get in a situation where there's a request in the started list
1239 * but there weren't enough TRBs to fully kick it in the first time
1240 * around, so it has been waiting for more TRBs to be freed up.
1242 * In that case, we should check if we have a request with pending_sgs
1243 * in the started list and prepare TRBs for that request first,
1244 * otherwise we will prepare TRBs completely out of order and that will
1245 * break things.
1247 list_for_each_entry(req, &dep->started_list, list) {
1248 if (req->num_pending_sgs > 0)
1249 dwc3_prepare_one_trb_sg(dep, req);
1251 if (!dwc3_calc_trbs_left(dep))
1252 return;
1255 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1256 struct dwc3 *dwc = dep->dwc;
1257 int ret;
1259 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1260 dep->direction);
1261 if (ret)
1262 return;
1264 req->sg = req->request.sg;
1265 req->start_sg = req->sg;
1266 req->num_queued_sgs = 0;
1267 req->num_pending_sgs = req->request.num_mapped_sgs;
1269 if (req->num_pending_sgs > 0)
1270 dwc3_prepare_one_trb_sg(dep, req);
1271 else
1272 dwc3_prepare_one_trb_linear(dep, req);
1274 if (!dwc3_calc_trbs_left(dep))
1275 return;
1279 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1281 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1283 struct dwc3_gadget_ep_cmd_params params;
1284 struct dwc3_request *req;
1285 int starting;
1286 int ret;
1287 u32 cmd;
1289 if (!dwc3_calc_trbs_left(dep))
1290 return 0;
1292 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1294 dwc3_prepare_trbs(dep);
1295 req = next_request(&dep->started_list);
1296 if (!req) {
1297 dep->flags |= DWC3_EP_PENDING_REQUEST;
1298 return 0;
1301 memset(&params, 0, sizeof(params));
1303 if (starting) {
1304 params.param0 = upper_32_bits(req->trb_dma);
1305 params.param1 = lower_32_bits(req->trb_dma);
1306 cmd = DWC3_DEPCMD_STARTTRANSFER;
1308 if (dep->stream_capable)
1309 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1311 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1312 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1313 } else {
1314 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1315 DWC3_DEPCMD_PARAM(dep->resource_index);
1318 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1319 if (ret < 0) {
1320 struct dwc3_request *tmp;
1322 if (ret == -EAGAIN)
1323 return ret;
1325 dwc3_stop_active_transfer(dep, true, true);
1327 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1328 dwc3_gadget_move_cancelled_request(req);
1330 /* If ep isn't started, then there's no end transfer pending */
1331 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1332 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1334 return ret;
1337 return 0;
1340 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1342 u32 reg;
1344 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1345 return DWC3_DSTS_SOFFN(reg);
1349 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1350 * @dep: isoc endpoint
1352 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1353 * microframe number reported by the XferNotReady event for the future frame
1354 * number to start the isoc transfer.
1356 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1357 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1358 * XferNotReady event are invalid. The driver uses this number to schedule the
1359 * isochronous transfer and passes it to the START TRANSFER command. Because
1360 * this number is invalid, the command may fail. If BIT[15:14] matches the
1361 * internal 16-bit microframe, the START TRANSFER command will pass and the
1362 * transfer will start at the scheduled time, if it is off by 1, the command
1363 * will still pass, but the transfer will start 2 seconds in the future. For all
1364 * other conditions, the START TRANSFER command will fail with bus-expiry.
1366 * In order to workaround this issue, we can test for the correct combination of
1367 * BIT[15:14] by sending START TRANSFER commands with different values of
1368 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1369 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1370 * As the result, within the 4 possible combinations for BIT[15:14], there will
1371 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1372 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1373 * value is the correct combination.
1375 * Since there are only 4 outcomes and the results are ordered, we can simply
1376 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1377 * deduce the smaller successful combination.
1379 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1380 * of BIT[15:14]. The correct combination is as follow:
1382 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1383 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1384 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1385 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1387 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1388 * endpoints.
1390 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1392 int cmd_status = 0;
1393 bool test0;
1394 bool test1;
1396 while (dep->combo_num < 2) {
1397 struct dwc3_gadget_ep_cmd_params params;
1398 u32 test_frame_number;
1399 u32 cmd;
1402 * Check if we can start isoc transfer on the next interval or
1403 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1405 test_frame_number = dep->frame_number & 0x3fff;
1406 test_frame_number |= dep->combo_num << 14;
1407 test_frame_number += max_t(u32, 4, dep->interval);
1409 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1410 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1412 cmd = DWC3_DEPCMD_STARTTRANSFER;
1413 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1414 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1416 /* Redo if some other failure beside bus-expiry is received */
1417 if (cmd_status && cmd_status != -EAGAIN) {
1418 dep->start_cmd_status = 0;
1419 dep->combo_num = 0;
1420 return 0;
1423 /* Store the first test status */
1424 if (dep->combo_num == 0)
1425 dep->start_cmd_status = cmd_status;
1427 dep->combo_num++;
1430 * End the transfer if the START_TRANSFER command is successful
1431 * to wait for the next XferNotReady to test the command again
1433 if (cmd_status == 0) {
1434 dwc3_stop_active_transfer(dep, true, true);
1435 return 0;
1439 /* test0 and test1 are both completed at this point */
1440 test0 = (dep->start_cmd_status == 0);
1441 test1 = (cmd_status == 0);
1443 if (!test0 && test1)
1444 dep->combo_num = 1;
1445 else if (!test0 && !test1)
1446 dep->combo_num = 2;
1447 else if (test0 && !test1)
1448 dep->combo_num = 3;
1449 else if (test0 && test1)
1450 dep->combo_num = 0;
1452 dep->frame_number &= 0x3fff;
1453 dep->frame_number |= dep->combo_num << 14;
1454 dep->frame_number += max_t(u32, 4, dep->interval);
1456 /* Reinitialize test variables */
1457 dep->start_cmd_status = 0;
1458 dep->combo_num = 0;
1460 return __dwc3_gadget_kick_transfer(dep);
1463 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1465 struct dwc3 *dwc = dep->dwc;
1466 int ret;
1467 int i;
1469 if (list_empty(&dep->pending_list)) {
1470 dep->flags |= DWC3_EP_PENDING_REQUEST;
1471 return -EAGAIN;
1474 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1475 (dwc->revision <= DWC3_USB31_REVISION_160A ||
1476 (dwc->revision == DWC3_USB31_REVISION_170A &&
1477 dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1478 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1480 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1481 return dwc3_gadget_start_isoc_quirk(dep);
1484 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1485 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1487 ret = __dwc3_gadget_kick_transfer(dep);
1488 if (ret != -EAGAIN)
1489 break;
1492 return ret;
1495 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1497 struct dwc3 *dwc = dep->dwc;
1499 if (!dep->endpoint.desc) {
1500 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1501 dep->name);
1502 return -ESHUTDOWN;
1505 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1506 &req->request, req->dep->name))
1507 return -EINVAL;
1509 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1510 "%s: request %pK already in flight\n",
1511 dep->name, &req->request))
1512 return -EINVAL;
1514 pm_runtime_get(dwc->dev);
1516 req->request.actual = 0;
1517 req->request.status = -EINPROGRESS;
1519 trace_dwc3_ep_queue(req);
1521 list_add_tail(&req->list, &dep->pending_list);
1522 req->status = DWC3_REQUEST_STATUS_QUEUED;
1524 /* Start the transfer only after the END_TRANSFER is completed */
1525 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1526 dep->flags |= DWC3_EP_DELAY_START;
1527 return 0;
1531 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1532 * wait for a XferNotReady event so we will know what's the current
1533 * (micro-)frame number.
1535 * Without this trick, we are very, very likely gonna get Bus Expiry
1536 * errors which will force us issue EndTransfer command.
1538 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1539 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1540 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1541 return 0;
1543 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1544 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1545 return __dwc3_gadget_start_isoc(dep);
1550 return __dwc3_gadget_kick_transfer(dep);
1553 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1554 gfp_t gfp_flags)
1556 struct dwc3_request *req = to_dwc3_request(request);
1557 struct dwc3_ep *dep = to_dwc3_ep(ep);
1558 struct dwc3 *dwc = dep->dwc;
1560 unsigned long flags;
1562 int ret;
1564 spin_lock_irqsave(&dwc->lock, flags);
1565 ret = __dwc3_gadget_ep_queue(dep, req);
1566 spin_unlock_irqrestore(&dwc->lock, flags);
1568 return ret;
1571 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1573 int i;
1575 /* If req->trb is not set, then the request has not started */
1576 if (!req->trb)
1577 return;
1580 * If request was already started, this means we had to
1581 * stop the transfer. With that we also need to ignore
1582 * all TRBs used by the request, however TRBs can only
1583 * be modified after completion of END_TRANSFER
1584 * command. So what we do here is that we wait for
1585 * END_TRANSFER completion and only after that, we jump
1586 * over TRBs by clearing HWO and incrementing dequeue
1587 * pointer.
1589 for (i = 0; i < req->num_trbs; i++) {
1590 struct dwc3_trb *trb;
1592 trb = &dep->trb_pool[dep->trb_dequeue];
1593 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1594 dwc3_ep_inc_deq(dep);
1597 req->num_trbs = 0;
1600 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1602 struct dwc3_request *req;
1603 struct dwc3_request *tmp;
1605 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1606 dwc3_gadget_ep_skip_trbs(dep, req);
1607 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1611 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1612 struct usb_request *request)
1614 struct dwc3_request *req = to_dwc3_request(request);
1615 struct dwc3_request *r = NULL;
1617 struct dwc3_ep *dep = to_dwc3_ep(ep);
1618 struct dwc3 *dwc = dep->dwc;
1620 unsigned long flags;
1621 int ret = 0;
1623 trace_dwc3_ep_dequeue(req);
1625 spin_lock_irqsave(&dwc->lock, flags);
1627 list_for_each_entry(r, &dep->pending_list, list) {
1628 if (r == req)
1629 break;
1632 if (r != req) {
1633 list_for_each_entry(r, &dep->started_list, list) {
1634 if (r == req)
1635 break;
1637 if (r == req) {
1638 /* wait until it is processed */
1639 dwc3_stop_active_transfer(dep, true, true);
1641 if (!r->trb)
1642 goto out0;
1644 dwc3_gadget_move_cancelled_request(req);
1645 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1646 goto out0;
1647 else
1648 goto out1;
1650 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1651 request, ep->name);
1652 ret = -EINVAL;
1653 goto out0;
1656 out1:
1657 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1659 out0:
1660 spin_unlock_irqrestore(&dwc->lock, flags);
1662 return ret;
1665 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1667 struct dwc3_gadget_ep_cmd_params params;
1668 struct dwc3 *dwc = dep->dwc;
1669 struct dwc3_request *req;
1670 struct dwc3_request *tmp;
1671 int ret;
1673 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1674 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1675 return -EINVAL;
1678 memset(&params, 0x00, sizeof(params));
1680 if (value) {
1681 struct dwc3_trb *trb;
1683 unsigned transfer_in_flight;
1684 unsigned started;
1686 if (dep->number > 1)
1687 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1688 else
1689 trb = &dwc->ep0_trb[dep->trb_enqueue];
1691 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1692 started = !list_empty(&dep->started_list);
1694 if (!protocol && ((dep->direction && transfer_in_flight) ||
1695 (!dep->direction && started))) {
1696 return -EAGAIN;
1699 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1700 &params);
1701 if (ret)
1702 dev_err(dwc->dev, "failed to set STALL on %s\n",
1703 dep->name);
1704 else
1705 dep->flags |= DWC3_EP_STALL;
1706 } else {
1708 * Don't issue CLEAR_STALL command to control endpoints. The
1709 * controller automatically clears the STALL when it receives
1710 * the SETUP token.
1712 if (dep->number <= 1) {
1713 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1714 return 0;
1717 ret = dwc3_send_clear_stall_ep_cmd(dep);
1718 if (ret) {
1719 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1720 dep->name);
1721 return ret;
1724 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1726 dwc3_stop_active_transfer(dep, true, true);
1728 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1729 dwc3_gadget_move_cancelled_request(req);
1731 list_for_each_entry_safe(req, tmp, &dep->pending_list, list)
1732 dwc3_gadget_move_cancelled_request(req);
1734 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) {
1735 dep->flags &= ~DWC3_EP_DELAY_START;
1736 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1740 return ret;
1743 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1745 struct dwc3_ep *dep = to_dwc3_ep(ep);
1746 struct dwc3 *dwc = dep->dwc;
1748 unsigned long flags;
1750 int ret;
1752 spin_lock_irqsave(&dwc->lock, flags);
1753 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1754 spin_unlock_irqrestore(&dwc->lock, flags);
1756 return ret;
1759 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1761 struct dwc3_ep *dep = to_dwc3_ep(ep);
1762 struct dwc3 *dwc = dep->dwc;
1763 unsigned long flags;
1764 int ret;
1766 spin_lock_irqsave(&dwc->lock, flags);
1767 dep->flags |= DWC3_EP_WEDGE;
1769 if (dep->number == 0 || dep->number == 1)
1770 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1771 else
1772 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1773 spin_unlock_irqrestore(&dwc->lock, flags);
1775 return ret;
1778 /* -------------------------------------------------------------------------- */
1780 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1781 .bLength = USB_DT_ENDPOINT_SIZE,
1782 .bDescriptorType = USB_DT_ENDPOINT,
1783 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1786 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1787 .enable = dwc3_gadget_ep0_enable,
1788 .disable = dwc3_gadget_ep0_disable,
1789 .alloc_request = dwc3_gadget_ep_alloc_request,
1790 .free_request = dwc3_gadget_ep_free_request,
1791 .queue = dwc3_gadget_ep0_queue,
1792 .dequeue = dwc3_gadget_ep_dequeue,
1793 .set_halt = dwc3_gadget_ep0_set_halt,
1794 .set_wedge = dwc3_gadget_ep_set_wedge,
1797 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1798 .enable = dwc3_gadget_ep_enable,
1799 .disable = dwc3_gadget_ep_disable,
1800 .alloc_request = dwc3_gadget_ep_alloc_request,
1801 .free_request = dwc3_gadget_ep_free_request,
1802 .queue = dwc3_gadget_ep_queue,
1803 .dequeue = dwc3_gadget_ep_dequeue,
1804 .set_halt = dwc3_gadget_ep_set_halt,
1805 .set_wedge = dwc3_gadget_ep_set_wedge,
1808 /* -------------------------------------------------------------------------- */
1810 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1812 struct dwc3 *dwc = gadget_to_dwc(g);
1814 return __dwc3_gadget_get_frame(dwc);
1817 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1819 int retries;
1821 int ret;
1822 u32 reg;
1824 u8 link_state;
1827 * According to the Databook Remote wakeup request should
1828 * be issued only when the device is in early suspend state.
1830 * We can check that via USB Link State bits in DSTS register.
1832 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1834 link_state = DWC3_DSTS_USBLNKST(reg);
1836 switch (link_state) {
1837 case DWC3_LINK_STATE_RESET:
1838 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1839 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1840 case DWC3_LINK_STATE_RESUME:
1841 break;
1842 default:
1843 return -EINVAL;
1846 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1847 if (ret < 0) {
1848 dev_err(dwc->dev, "failed to put link in Recovery\n");
1849 return ret;
1852 /* Recent versions do this automatically */
1853 if (dwc->revision < DWC3_REVISION_194A) {
1854 /* write zeroes to Link Change Request */
1855 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1856 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1857 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1860 /* poll until Link State changes to ON */
1861 retries = 20000;
1863 while (retries--) {
1864 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1866 /* in HS, means ON */
1867 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1868 break;
1871 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1872 dev_err(dwc->dev, "failed to send remote wakeup\n");
1873 return -EINVAL;
1876 return 0;
1879 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1881 struct dwc3 *dwc = gadget_to_dwc(g);
1882 unsigned long flags;
1883 int ret;
1885 spin_lock_irqsave(&dwc->lock, flags);
1886 ret = __dwc3_gadget_wakeup(dwc);
1887 spin_unlock_irqrestore(&dwc->lock, flags);
1889 return ret;
1892 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1893 int is_selfpowered)
1895 struct dwc3 *dwc = gadget_to_dwc(g);
1896 unsigned long flags;
1898 spin_lock_irqsave(&dwc->lock, flags);
1899 g->is_selfpowered = !!is_selfpowered;
1900 spin_unlock_irqrestore(&dwc->lock, flags);
1902 return 0;
1905 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1907 u32 reg;
1908 u32 timeout = 500;
1910 if (pm_runtime_suspended(dwc->dev))
1911 return 0;
1913 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1914 if (is_on) {
1915 if (dwc->revision <= DWC3_REVISION_187A) {
1916 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1917 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1920 if (dwc->revision >= DWC3_REVISION_194A)
1921 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1922 reg |= DWC3_DCTL_RUN_STOP;
1924 if (dwc->has_hibernation)
1925 reg |= DWC3_DCTL_KEEP_CONNECT;
1927 dwc->pullups_connected = true;
1928 } else {
1929 reg &= ~DWC3_DCTL_RUN_STOP;
1931 if (dwc->has_hibernation && !suspend)
1932 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1934 dwc->pullups_connected = false;
1937 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1939 do {
1940 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1941 reg &= DWC3_DSTS_DEVCTRLHLT;
1942 } while (--timeout && !(!is_on ^ !reg));
1944 if (!timeout)
1945 return -ETIMEDOUT;
1947 return 0;
1950 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1952 struct dwc3 *dwc = gadget_to_dwc(g);
1953 unsigned long flags;
1954 int ret;
1956 is_on = !!is_on;
1959 * Per databook, when we want to stop the gadget, if a control transfer
1960 * is still in process, complete it and get the core into setup phase.
1962 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1963 reinit_completion(&dwc->ep0_in_setup);
1965 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1966 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1967 if (ret == 0) {
1968 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1969 return -ETIMEDOUT;
1973 spin_lock_irqsave(&dwc->lock, flags);
1974 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1975 spin_unlock_irqrestore(&dwc->lock, flags);
1977 return ret;
1980 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1982 u32 reg;
1984 /* Enable all but Start and End of Frame IRQs */
1985 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1986 DWC3_DEVTEN_EVNTOVERFLOWEN |
1987 DWC3_DEVTEN_CMDCMPLTEN |
1988 DWC3_DEVTEN_ERRTICERREN |
1989 DWC3_DEVTEN_WKUPEVTEN |
1990 DWC3_DEVTEN_CONNECTDONEEN |
1991 DWC3_DEVTEN_USBRSTEN |
1992 DWC3_DEVTEN_DISCONNEVTEN);
1994 if (dwc->revision < DWC3_REVISION_250A)
1995 reg |= DWC3_DEVTEN_ULSTCNGEN;
1997 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2000 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2002 /* mask all interrupts */
2003 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2006 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2007 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
2010 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2011 * @dwc: pointer to our context structure
2013 * The following looks like complex but it's actually very simple. In order to
2014 * calculate the number of packets we can burst at once on OUT transfers, we're
2015 * gonna use RxFIFO size.
2017 * To calculate RxFIFO size we need two numbers:
2018 * MDWIDTH = size, in bits, of the internal memory bus
2019 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2021 * Given these two numbers, the formula is simple:
2023 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2025 * 24 bytes is for 3x SETUP packets
2026 * 16 bytes is a clock domain crossing tolerance
2028 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2030 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2032 u32 ram2_depth;
2033 u32 mdwidth;
2034 u32 nump;
2035 u32 reg;
2037 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2038 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2040 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2041 nump = min_t(u32, nump, 16);
2043 /* update NumP */
2044 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2045 reg &= ~DWC3_DCFG_NUMP_MASK;
2046 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2047 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2050 static int __dwc3_gadget_start(struct dwc3 *dwc)
2052 struct dwc3_ep *dep;
2053 int ret = 0;
2054 u32 reg;
2057 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2058 * the core supports IMOD, disable it.
2060 if (dwc->imod_interval) {
2061 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2062 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2063 } else if (dwc3_has_imod(dwc)) {
2064 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2068 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2069 * field instead of letting dwc3 itself calculate that automatically.
2071 * This way, we maximize the chances that we'll be able to get several
2072 * bursts of data without going through any sort of endpoint throttling.
2074 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2075 if (dwc3_is_usb31(dwc))
2076 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2077 else
2078 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2080 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2082 dwc3_gadget_setup_nump(dwc);
2084 /* Start with SuperSpeed Default */
2085 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2087 dep = dwc->eps[0];
2088 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2089 if (ret) {
2090 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2091 goto err0;
2094 dep = dwc->eps[1];
2095 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2096 if (ret) {
2097 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2098 goto err1;
2101 /* begin to receive SETUP packets */
2102 dwc->ep0state = EP0_SETUP_PHASE;
2103 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
2104 dwc3_ep0_out_start(dwc);
2106 dwc3_gadget_enable_irq(dwc);
2108 return 0;
2110 err1:
2111 __dwc3_gadget_ep_disable(dwc->eps[0]);
2113 err0:
2114 return ret;
2117 static int dwc3_gadget_start(struct usb_gadget *g,
2118 struct usb_gadget_driver *driver)
2120 struct dwc3 *dwc = gadget_to_dwc(g);
2121 unsigned long flags;
2122 int ret = 0;
2123 int irq;
2125 irq = dwc->irq_gadget;
2126 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2127 IRQF_SHARED, "dwc3", dwc->ev_buf);
2128 if (ret) {
2129 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2130 irq, ret);
2131 goto err0;
2134 spin_lock_irqsave(&dwc->lock, flags);
2135 if (dwc->gadget_driver) {
2136 dev_err(dwc->dev, "%s is already bound to %s\n",
2137 dwc->gadget.name,
2138 dwc->gadget_driver->driver.name);
2139 ret = -EBUSY;
2140 goto err1;
2143 dwc->gadget_driver = driver;
2145 if (pm_runtime_active(dwc->dev))
2146 __dwc3_gadget_start(dwc);
2148 spin_unlock_irqrestore(&dwc->lock, flags);
2150 return 0;
2152 err1:
2153 spin_unlock_irqrestore(&dwc->lock, flags);
2154 free_irq(irq, dwc);
2156 err0:
2157 return ret;
2160 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2162 dwc3_gadget_disable_irq(dwc);
2163 __dwc3_gadget_ep_disable(dwc->eps[0]);
2164 __dwc3_gadget_ep_disable(dwc->eps[1]);
2167 static int dwc3_gadget_stop(struct usb_gadget *g)
2169 struct dwc3 *dwc = gadget_to_dwc(g);
2170 unsigned long flags;
2172 spin_lock_irqsave(&dwc->lock, flags);
2174 if (pm_runtime_suspended(dwc->dev))
2175 goto out;
2177 __dwc3_gadget_stop(dwc);
2179 out:
2180 dwc->gadget_driver = NULL;
2181 spin_unlock_irqrestore(&dwc->lock, flags);
2183 free_irq(dwc->irq_gadget, dwc->ev_buf);
2185 return 0;
2188 static void dwc3_gadget_config_params(struct usb_gadget *g,
2189 struct usb_dcd_config_params *params)
2191 struct dwc3 *dwc = gadget_to_dwc(g);
2193 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2194 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2196 /* Recommended BESL */
2197 if (!dwc->dis_enblslpm_quirk) {
2199 * If the recommended BESL baseline is 0 or if the BESL deep is
2200 * less than 2, Microsoft's Windows 10 host usb stack will issue
2201 * a usb reset immediately after it receives the extended BOS
2202 * descriptor and the enumeration will fail. To maintain
2203 * compatibility with the Windows' usb stack, let's set the
2204 * recommended BESL baseline to 1 and clamp the BESL deep to be
2205 * within 2 to 15.
2207 params->besl_baseline = 1;
2208 if (dwc->is_utmi_l1_suspend)
2209 params->besl_deep =
2210 clamp_t(u8, dwc->hird_threshold, 2, 15);
2213 /* U1 Device exit Latency */
2214 if (dwc->dis_u1_entry_quirk)
2215 params->bU1devExitLat = 0;
2216 else
2217 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2219 /* U2 Device exit Latency */
2220 if (dwc->dis_u2_entry_quirk)
2221 params->bU2DevExitLat = 0;
2222 else
2223 params->bU2DevExitLat =
2224 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2227 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2228 enum usb_device_speed speed)
2230 struct dwc3 *dwc = gadget_to_dwc(g);
2231 unsigned long flags;
2232 u32 reg;
2234 spin_lock_irqsave(&dwc->lock, flags);
2235 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2236 reg &= ~(DWC3_DCFG_SPEED_MASK);
2239 * WORKAROUND: DWC3 revision < 2.20a have an issue
2240 * which would cause metastability state on Run/Stop
2241 * bit if we try to force the IP to USB2-only mode.
2243 * Because of that, we cannot configure the IP to any
2244 * speed other than the SuperSpeed
2246 * Refers to:
2248 * STAR#9000525659: Clock Domain Crossing on DCTL in
2249 * USB 2.0 Mode
2251 if (dwc->revision < DWC3_REVISION_220A &&
2252 !dwc->dis_metastability_quirk) {
2253 reg |= DWC3_DCFG_SUPERSPEED;
2254 } else {
2255 switch (speed) {
2256 case USB_SPEED_LOW:
2257 reg |= DWC3_DCFG_LOWSPEED;
2258 break;
2259 case USB_SPEED_FULL:
2260 reg |= DWC3_DCFG_FULLSPEED;
2261 break;
2262 case USB_SPEED_HIGH:
2263 reg |= DWC3_DCFG_HIGHSPEED;
2264 break;
2265 case USB_SPEED_SUPER:
2266 reg |= DWC3_DCFG_SUPERSPEED;
2267 break;
2268 case USB_SPEED_SUPER_PLUS:
2269 if (dwc3_is_usb31(dwc))
2270 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2271 else
2272 reg |= DWC3_DCFG_SUPERSPEED;
2273 break;
2274 default:
2275 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2277 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2278 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2279 else
2280 reg |= DWC3_DCFG_SUPERSPEED;
2283 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2285 spin_unlock_irqrestore(&dwc->lock, flags);
2288 static const struct usb_gadget_ops dwc3_gadget_ops = {
2289 .get_frame = dwc3_gadget_get_frame,
2290 .wakeup = dwc3_gadget_wakeup,
2291 .set_selfpowered = dwc3_gadget_set_selfpowered,
2292 .pullup = dwc3_gadget_pullup,
2293 .udc_start = dwc3_gadget_start,
2294 .udc_stop = dwc3_gadget_stop,
2295 .udc_set_speed = dwc3_gadget_set_speed,
2296 .get_config_params = dwc3_gadget_config_params,
2299 /* -------------------------------------------------------------------------- */
2301 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2303 struct dwc3 *dwc = dep->dwc;
2305 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2306 dep->endpoint.maxburst = 1;
2307 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2308 if (!dep->direction)
2309 dwc->gadget.ep0 = &dep->endpoint;
2311 dep->endpoint.caps.type_control = true;
2313 return 0;
2316 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2318 struct dwc3 *dwc = dep->dwc;
2319 int mdwidth;
2320 int size;
2322 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2323 /* MDWIDTH is represented in bits, we need it in bytes */
2324 mdwidth /= 8;
2326 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2327 if (dwc3_is_usb31(dwc))
2328 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2329 else
2330 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2332 /* FIFO Depth is in MDWDITH bytes. Multiply */
2333 size *= mdwidth;
2336 * To meet performance requirement, a minimum TxFIFO size of 3x
2337 * MaxPacketSize is recommended for endpoints that support burst and a
2338 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2339 * support burst. Use those numbers and we can calculate the max packet
2340 * limit as below.
2342 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2343 size /= 3;
2344 else
2345 size /= 2;
2347 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2349 dep->endpoint.max_streams = 15;
2350 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2351 list_add_tail(&dep->endpoint.ep_list,
2352 &dwc->gadget.ep_list);
2353 dep->endpoint.caps.type_iso = true;
2354 dep->endpoint.caps.type_bulk = true;
2355 dep->endpoint.caps.type_int = true;
2357 return dwc3_alloc_trb_pool(dep);
2360 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2362 struct dwc3 *dwc = dep->dwc;
2363 int mdwidth;
2364 int size;
2366 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2368 /* MDWIDTH is represented in bits, convert to bytes */
2369 mdwidth /= 8;
2371 /* All OUT endpoints share a single RxFIFO space */
2372 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2373 if (dwc3_is_usb31(dwc))
2374 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2375 else
2376 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
2378 /* FIFO depth is in MDWDITH bytes */
2379 size *= mdwidth;
2382 * To meet performance requirement, a minimum recommended RxFIFO size
2383 * is defined as follow:
2384 * RxFIFO size >= (3 x MaxPacketSize) +
2385 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2387 * Then calculate the max packet limit as below.
2389 size -= (3 * 8) + 16;
2390 if (size < 0)
2391 size = 0;
2392 else
2393 size /= 3;
2395 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2396 dep->endpoint.max_streams = 15;
2397 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2398 list_add_tail(&dep->endpoint.ep_list,
2399 &dwc->gadget.ep_list);
2400 dep->endpoint.caps.type_iso = true;
2401 dep->endpoint.caps.type_bulk = true;
2402 dep->endpoint.caps.type_int = true;
2404 return dwc3_alloc_trb_pool(dep);
2407 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2409 struct dwc3_ep *dep;
2410 bool direction = epnum & 1;
2411 int ret;
2412 u8 num = epnum >> 1;
2414 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2415 if (!dep)
2416 return -ENOMEM;
2418 dep->dwc = dwc;
2419 dep->number = epnum;
2420 dep->direction = direction;
2421 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2422 dwc->eps[epnum] = dep;
2423 dep->combo_num = 0;
2424 dep->start_cmd_status = 0;
2426 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2427 direction ? "in" : "out");
2429 dep->endpoint.name = dep->name;
2431 if (!(dep->number > 1)) {
2432 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2433 dep->endpoint.comp_desc = NULL;
2436 if (num == 0)
2437 ret = dwc3_gadget_init_control_endpoint(dep);
2438 else if (direction)
2439 ret = dwc3_gadget_init_in_endpoint(dep);
2440 else
2441 ret = dwc3_gadget_init_out_endpoint(dep);
2443 if (ret)
2444 return ret;
2446 dep->endpoint.caps.dir_in = direction;
2447 dep->endpoint.caps.dir_out = !direction;
2449 INIT_LIST_HEAD(&dep->pending_list);
2450 INIT_LIST_HEAD(&dep->started_list);
2451 INIT_LIST_HEAD(&dep->cancelled_list);
2453 return 0;
2456 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2458 u8 epnum;
2460 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2462 for (epnum = 0; epnum < total; epnum++) {
2463 int ret;
2465 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2466 if (ret)
2467 return ret;
2470 return 0;
2473 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2475 struct dwc3_ep *dep;
2476 u8 epnum;
2478 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2479 dep = dwc->eps[epnum];
2480 if (!dep)
2481 continue;
2483 * Physical endpoints 0 and 1 are special; they form the
2484 * bi-directional USB endpoint 0.
2486 * For those two physical endpoints, we don't allocate a TRB
2487 * pool nor do we add them the endpoints list. Due to that, we
2488 * shouldn't do these two operations otherwise we would end up
2489 * with all sorts of bugs when removing dwc3.ko.
2491 if (epnum != 0 && epnum != 1) {
2492 dwc3_free_trb_pool(dep);
2493 list_del(&dep->endpoint.ep_list);
2496 kfree(dep);
2500 /* -------------------------------------------------------------------------- */
2502 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2503 struct dwc3_request *req, struct dwc3_trb *trb,
2504 const struct dwc3_event_depevt *event, int status, int chain)
2506 unsigned int count;
2508 dwc3_ep_inc_deq(dep);
2510 trace_dwc3_complete_trb(dep, trb);
2511 req->num_trbs--;
2514 * If we're in the middle of series of chained TRBs and we
2515 * receive a short transfer along the way, DWC3 will skip
2516 * through all TRBs including the last TRB in the chain (the
2517 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2518 * bit and SW has to do it manually.
2520 * We're going to do that here to avoid problems of HW trying
2521 * to use bogus TRBs for transfers.
2523 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2524 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2527 * For isochronous transfers, the first TRB in a service interval must
2528 * have the Isoc-First type. Track and report its interval frame number.
2530 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2531 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2532 unsigned int frame_number;
2534 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2535 frame_number &= ~(dep->interval - 1);
2536 req->request.frame_number = frame_number;
2540 * If we're dealing with unaligned size OUT transfer, we will be left
2541 * with one TRB pending in the ring. We need to manually clear HWO bit
2542 * from that TRB.
2545 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
2546 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2547 return 1;
2550 count = trb->size & DWC3_TRB_SIZE_MASK;
2551 req->remaining += count;
2553 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2554 return 1;
2556 if (event->status & DEPEVT_STATUS_SHORT && !chain)
2557 return 1;
2559 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2560 (trb->ctrl & DWC3_TRB_CTRL_LST))
2561 return 1;
2563 return 0;
2566 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2567 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2568 int status)
2570 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2571 struct scatterlist *sg = req->sg;
2572 struct scatterlist *s;
2573 unsigned int pending = req->num_pending_sgs;
2574 unsigned int i;
2575 int ret = 0;
2577 for_each_sg(sg, s, pending, i) {
2578 trb = &dep->trb_pool[dep->trb_dequeue];
2580 req->sg = sg_next(s);
2581 req->num_pending_sgs--;
2583 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2584 trb, event, status, true);
2585 if (ret)
2586 break;
2589 return ret;
2592 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2593 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2594 int status)
2596 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2598 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2599 event, status, false);
2602 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2604 return req->num_pending_sgs == 0;
2607 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2608 const struct dwc3_event_depevt *event,
2609 struct dwc3_request *req, int status)
2611 int ret;
2613 if (req->num_pending_sgs)
2614 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2615 status);
2616 else
2617 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2618 status);
2620 if (req->needs_extra_trb) {
2621 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
2623 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2624 status);
2626 /* Reclaim MPS padding TRB for ZLP */
2627 if (!req->direction && req->request.zero && req->request.length &&
2628 !usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2629 (IS_ALIGNED(req->request.length, maxp)))
2630 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, status);
2632 req->needs_extra_trb = false;
2635 req->request.actual = req->request.length - req->remaining;
2637 if (!dwc3_gadget_ep_request_completed(req)) {
2638 __dwc3_gadget_kick_transfer(dep);
2639 goto out;
2642 dwc3_gadget_giveback(dep, req, status);
2644 out:
2645 return ret;
2648 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2649 const struct dwc3_event_depevt *event, int status)
2651 struct dwc3_request *req;
2652 struct dwc3_request *tmp;
2654 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2655 int ret;
2657 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2658 req, status);
2659 if (ret)
2660 break;
2664 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2665 const struct dwc3_event_depevt *event)
2667 dep->frame_number = event->parameters;
2670 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2671 const struct dwc3_event_depevt *event)
2673 struct dwc3 *dwc = dep->dwc;
2674 unsigned status = 0;
2675 bool stop = false;
2677 dwc3_gadget_endpoint_frame_from_event(dep, event);
2679 if (event->status & DEPEVT_STATUS_BUSERR)
2680 status = -ECONNRESET;
2682 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2683 status = -EXDEV;
2685 if (list_empty(&dep->started_list))
2686 stop = true;
2689 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2691 if (stop)
2692 dwc3_stop_active_transfer(dep, true, true);
2695 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2696 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2698 if (dwc->revision < DWC3_REVISION_183A) {
2699 u32 reg;
2700 int i;
2702 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2703 dep = dwc->eps[i];
2705 if (!(dep->flags & DWC3_EP_ENABLED))
2706 continue;
2708 if (!list_empty(&dep->started_list))
2709 return;
2712 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2713 reg |= dwc->u1u2;
2714 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2716 dwc->u1u2 = 0;
2720 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2721 const struct dwc3_event_depevt *event)
2723 dwc3_gadget_endpoint_frame_from_event(dep, event);
2724 (void) __dwc3_gadget_start_isoc(dep);
2727 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2728 const struct dwc3_event_depevt *event)
2730 struct dwc3_ep *dep;
2731 u8 epnum = event->endpoint_number;
2732 u8 cmd;
2734 dep = dwc->eps[epnum];
2736 if (!(dep->flags & DWC3_EP_ENABLED)) {
2737 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
2738 return;
2740 /* Handle only EPCMDCMPLT when EP disabled */
2741 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2742 return;
2745 if (epnum == 0 || epnum == 1) {
2746 dwc3_ep0_interrupt(dwc, event);
2747 return;
2750 switch (event->endpoint_event) {
2751 case DWC3_DEPEVT_XFERINPROGRESS:
2752 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2753 break;
2754 case DWC3_DEPEVT_XFERNOTREADY:
2755 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2756 break;
2757 case DWC3_DEPEVT_EPCMDCMPLT:
2758 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2760 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2761 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2762 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2763 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2764 if ((dep->flags & DWC3_EP_DELAY_START) &&
2765 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2766 __dwc3_gadget_kick_transfer(dep);
2768 dep->flags &= ~DWC3_EP_DELAY_START;
2770 break;
2771 case DWC3_DEPEVT_STREAMEVT:
2772 case DWC3_DEPEVT_XFERCOMPLETE:
2773 case DWC3_DEPEVT_RXTXFIFOEVT:
2774 break;
2778 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2780 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2781 spin_unlock(&dwc->lock);
2782 dwc->gadget_driver->disconnect(&dwc->gadget);
2783 spin_lock(&dwc->lock);
2787 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2789 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2790 spin_unlock(&dwc->lock);
2791 dwc->gadget_driver->suspend(&dwc->gadget);
2792 spin_lock(&dwc->lock);
2796 static void dwc3_resume_gadget(struct dwc3 *dwc)
2798 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2799 spin_unlock(&dwc->lock);
2800 dwc->gadget_driver->resume(&dwc->gadget);
2801 spin_lock(&dwc->lock);
2805 static void dwc3_reset_gadget(struct dwc3 *dwc)
2807 if (!dwc->gadget_driver)
2808 return;
2810 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2811 spin_unlock(&dwc->lock);
2812 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2813 spin_lock(&dwc->lock);
2817 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
2818 bool interrupt)
2820 struct dwc3 *dwc = dep->dwc;
2821 struct dwc3_gadget_ep_cmd_params params;
2822 u32 cmd;
2823 int ret;
2825 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
2826 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2827 return;
2830 * NOTICE: We are violating what the Databook says about the
2831 * EndTransfer command. Ideally we would _always_ wait for the
2832 * EndTransfer Command Completion IRQ, but that's causing too
2833 * much trouble synchronizing between us and gadget driver.
2835 * We have discussed this with the IP Provider and it was
2836 * suggested to giveback all requests here, but give HW some
2837 * extra time to synchronize with the interconnect. We're using
2838 * an arbitrary 100us delay for that.
2840 * Note also that a similar handling was tested by Synopsys
2841 * (thanks a lot Paul) and nothing bad has come out of it.
2842 * In short, what we're doing is:
2844 * - Issue EndTransfer WITH CMDIOC bit set
2845 * - Wait 100us
2847 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2848 * supports a mode to work around the above limitation. The
2849 * software can poll the CMDACT bit in the DEPCMD register
2850 * after issuing a EndTransfer command. This mode is enabled
2851 * by writing GUCTL2[14]. This polling is already done in the
2852 * dwc3_send_gadget_ep_cmd() function so if the mode is
2853 * enabled, the EndTransfer command will have completed upon
2854 * returning from this function and we don't need to delay for
2855 * 100us.
2857 * This mode is NOT available on the DWC_usb31 IP.
2860 cmd = DWC3_DEPCMD_ENDTRANSFER;
2861 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2862 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
2863 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2864 memset(&params, 0, sizeof(params));
2865 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
2866 WARN_ON_ONCE(ret);
2867 dep->resource_index = 0;
2869 if (!interrupt)
2870 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2871 else
2872 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2874 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2875 udelay(100);
2878 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2880 u32 epnum;
2882 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2883 struct dwc3_ep *dep;
2884 int ret;
2886 dep = dwc->eps[epnum];
2887 if (!dep)
2888 continue;
2890 if (!(dep->flags & DWC3_EP_STALL))
2891 continue;
2893 dep->flags &= ~DWC3_EP_STALL;
2895 ret = dwc3_send_clear_stall_ep_cmd(dep);
2896 WARN_ON_ONCE(ret);
2900 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2902 int reg;
2904 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2905 reg &= ~DWC3_DCTL_INITU1ENA;
2906 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2908 reg &= ~DWC3_DCTL_INITU2ENA;
2909 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2911 dwc3_disconnect_gadget(dwc);
2913 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2914 dwc->setup_packet_pending = false;
2915 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2917 dwc->connected = false;
2920 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2922 u32 reg;
2924 dwc->connected = true;
2927 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2928 * would cause a missing Disconnect Event if there's a
2929 * pending Setup Packet in the FIFO.
2931 * There's no suggested workaround on the official Bug
2932 * report, which states that "unless the driver/application
2933 * is doing any special handling of a disconnect event,
2934 * there is no functional issue".
2936 * Unfortunately, it turns out that we _do_ some special
2937 * handling of a disconnect event, namely complete all
2938 * pending transfers, notify gadget driver of the
2939 * disconnection, and so on.
2941 * Our suggested workaround is to follow the Disconnect
2942 * Event steps here, instead, based on a setup_packet_pending
2943 * flag. Such flag gets set whenever we have a SETUP_PENDING
2944 * status for EP0 TRBs and gets cleared on XferComplete for the
2945 * same endpoint.
2947 * Refers to:
2949 * STAR#9000466709: RTL: Device : Disconnect event not
2950 * generated if setup packet pending in FIFO
2952 if (dwc->revision < DWC3_REVISION_188A) {
2953 if (dwc->setup_packet_pending)
2954 dwc3_gadget_disconnect_interrupt(dwc);
2957 dwc3_reset_gadget(dwc);
2959 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2960 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2961 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2962 dwc->test_mode = false;
2963 dwc3_clear_stall_all_ep(dwc);
2965 /* Reset device address to zero */
2966 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2967 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2968 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2971 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2973 struct dwc3_ep *dep;
2974 int ret;
2975 u32 reg;
2976 u8 speed;
2978 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2979 speed = reg & DWC3_DSTS_CONNECTSPD;
2980 dwc->speed = speed;
2983 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2984 * each time on Connect Done.
2986 * Currently we always use the reset value. If any platform
2987 * wants to set this to a different value, we need to add a
2988 * setting and update GCTL.RAMCLKSEL here.
2991 switch (speed) {
2992 case DWC3_DSTS_SUPERSPEED_PLUS:
2993 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2994 dwc->gadget.ep0->maxpacket = 512;
2995 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2996 break;
2997 case DWC3_DSTS_SUPERSPEED:
2999 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3000 * would cause a missing USB3 Reset event.
3002 * In such situations, we should force a USB3 Reset
3003 * event by calling our dwc3_gadget_reset_interrupt()
3004 * routine.
3006 * Refers to:
3008 * STAR#9000483510: RTL: SS : USB3 reset event may
3009 * not be generated always when the link enters poll
3011 if (dwc->revision < DWC3_REVISION_190A)
3012 dwc3_gadget_reset_interrupt(dwc);
3014 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3015 dwc->gadget.ep0->maxpacket = 512;
3016 dwc->gadget.speed = USB_SPEED_SUPER;
3017 break;
3018 case DWC3_DSTS_HIGHSPEED:
3019 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3020 dwc->gadget.ep0->maxpacket = 64;
3021 dwc->gadget.speed = USB_SPEED_HIGH;
3022 break;
3023 case DWC3_DSTS_FULLSPEED:
3024 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3025 dwc->gadget.ep0->maxpacket = 64;
3026 dwc->gadget.speed = USB_SPEED_FULL;
3027 break;
3028 case DWC3_DSTS_LOWSPEED:
3029 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3030 dwc->gadget.ep0->maxpacket = 8;
3031 dwc->gadget.speed = USB_SPEED_LOW;
3032 break;
3035 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3037 /* Enable USB2 LPM Capability */
3039 if ((dwc->revision > DWC3_REVISION_194A) &&
3040 (speed != DWC3_DSTS_SUPERSPEED) &&
3041 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
3042 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3043 reg |= DWC3_DCFG_LPM_CAP;
3044 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3046 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3047 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3049 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3050 (dwc->is_utmi_l1_suspend << 4));
3053 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3054 * DCFG.LPMCap is set, core responses with an ACK and the
3055 * BESL value in the LPM token is less than or equal to LPM
3056 * NYET threshold.
3058 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3059 && dwc->has_lpm_erratum,
3060 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
3062 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3063 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
3065 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3066 } else {
3067 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3068 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3069 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3072 dep = dwc->eps[0];
3073 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3074 if (ret) {
3075 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3076 return;
3079 dep = dwc->eps[1];
3080 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3081 if (ret) {
3082 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3083 return;
3087 * Configure PHY via GUSB3PIPECTLn if required.
3089 * Update GTXFIFOSIZn
3091 * In both cases reset values should be sufficient.
3095 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3098 * TODO take core out of low power mode when that's
3099 * implemented.
3102 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3103 spin_unlock(&dwc->lock);
3104 dwc->gadget_driver->resume(&dwc->gadget);
3105 spin_lock(&dwc->lock);
3109 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3110 unsigned int evtinfo)
3112 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3113 unsigned int pwropt;
3116 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3117 * Hibernation mode enabled which would show up when device detects
3118 * host-initiated U3 exit.
3120 * In that case, device will generate a Link State Change Interrupt
3121 * from U3 to RESUME which is only necessary if Hibernation is
3122 * configured in.
3124 * There are no functional changes due to such spurious event and we
3125 * just need to ignore it.
3127 * Refers to:
3129 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3130 * operational mode
3132 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3133 if ((dwc->revision < DWC3_REVISION_250A) &&
3134 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3135 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3136 (next == DWC3_LINK_STATE_RESUME)) {
3137 return;
3142 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3143 * on the link partner, the USB session might do multiple entry/exit
3144 * of low power states before a transfer takes place.
3146 * Due to this problem, we might experience lower throughput. The
3147 * suggested workaround is to disable DCTL[12:9] bits if we're
3148 * transitioning from U1/U2 to U0 and enable those bits again
3149 * after a transfer completes and there are no pending transfers
3150 * on any of the enabled endpoints.
3152 * This is the first half of that workaround.
3154 * Refers to:
3156 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3157 * core send LGO_Ux entering U0
3159 if (dwc->revision < DWC3_REVISION_183A) {
3160 if (next == DWC3_LINK_STATE_U0) {
3161 u32 u1u2;
3162 u32 reg;
3164 switch (dwc->link_state) {
3165 case DWC3_LINK_STATE_U1:
3166 case DWC3_LINK_STATE_U2:
3167 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3168 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3169 | DWC3_DCTL_ACCEPTU2ENA
3170 | DWC3_DCTL_INITU1ENA
3171 | DWC3_DCTL_ACCEPTU1ENA);
3173 if (!dwc->u1u2)
3174 dwc->u1u2 = reg & u1u2;
3176 reg &= ~u1u2;
3178 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3179 break;
3180 default:
3181 /* do nothing */
3182 break;
3187 switch (next) {
3188 case DWC3_LINK_STATE_U1:
3189 if (dwc->speed == USB_SPEED_SUPER)
3190 dwc3_suspend_gadget(dwc);
3191 break;
3192 case DWC3_LINK_STATE_U2:
3193 case DWC3_LINK_STATE_U3:
3194 dwc3_suspend_gadget(dwc);
3195 break;
3196 case DWC3_LINK_STATE_RESUME:
3197 dwc3_resume_gadget(dwc);
3198 break;
3199 default:
3200 /* do nothing */
3201 break;
3204 dwc->link_state = next;
3207 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3208 unsigned int evtinfo)
3210 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3212 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3213 dwc3_suspend_gadget(dwc);
3215 dwc->link_state = next;
3218 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3219 unsigned int evtinfo)
3221 unsigned int is_ss = evtinfo & BIT(4);
3224 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3225 * have a known issue which can cause USB CV TD.9.23 to fail
3226 * randomly.
3228 * Because of this issue, core could generate bogus hibernation
3229 * events which SW needs to ignore.
3231 * Refers to:
3233 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3234 * Device Fallback from SuperSpeed
3236 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3237 return;
3239 /* enter hibernation here */
3242 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3243 const struct dwc3_event_devt *event)
3245 switch (event->type) {
3246 case DWC3_DEVICE_EVENT_DISCONNECT:
3247 dwc3_gadget_disconnect_interrupt(dwc);
3248 break;
3249 case DWC3_DEVICE_EVENT_RESET:
3250 dwc3_gadget_reset_interrupt(dwc);
3251 break;
3252 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3253 dwc3_gadget_conndone_interrupt(dwc);
3254 break;
3255 case DWC3_DEVICE_EVENT_WAKEUP:
3256 dwc3_gadget_wakeup_interrupt(dwc);
3257 break;
3258 case DWC3_DEVICE_EVENT_HIBER_REQ:
3259 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3260 "unexpected hibernation event\n"))
3261 break;
3263 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3264 break;
3265 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3266 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3267 break;
3268 case DWC3_DEVICE_EVENT_EOPF:
3269 /* It changed to be suspend event for version 2.30a and above */
3270 if (dwc->revision >= DWC3_REVISION_230A) {
3272 * Ignore suspend event until the gadget enters into
3273 * USB_STATE_CONFIGURED state.
3275 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3276 dwc3_gadget_suspend_interrupt(dwc,
3277 event->event_info);
3279 break;
3280 case DWC3_DEVICE_EVENT_SOF:
3281 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3282 case DWC3_DEVICE_EVENT_CMD_CMPL:
3283 case DWC3_DEVICE_EVENT_OVERFLOW:
3284 break;
3285 default:
3286 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3290 static void dwc3_process_event_entry(struct dwc3 *dwc,
3291 const union dwc3_event *event)
3293 trace_dwc3_event(event->raw, dwc);
3295 if (!event->type.is_devspec)
3296 dwc3_endpoint_interrupt(dwc, &event->depevt);
3297 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3298 dwc3_gadget_interrupt(dwc, &event->devt);
3299 else
3300 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3303 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3305 struct dwc3 *dwc = evt->dwc;
3306 irqreturn_t ret = IRQ_NONE;
3307 int left;
3308 u32 reg;
3310 left = evt->count;
3312 if (!(evt->flags & DWC3_EVENT_PENDING))
3313 return IRQ_NONE;
3315 while (left > 0) {
3316 union dwc3_event event;
3318 event.raw = *(u32 *) (evt->cache + evt->lpos);
3320 dwc3_process_event_entry(dwc, &event);
3323 * FIXME we wrap around correctly to the next entry as
3324 * almost all entries are 4 bytes in size. There is one
3325 * entry which has 12 bytes which is a regular entry
3326 * followed by 8 bytes data. ATM I don't know how
3327 * things are organized if we get next to the a
3328 * boundary so I worry about that once we try to handle
3329 * that.
3331 evt->lpos = (evt->lpos + 4) % evt->length;
3332 left -= 4;
3335 evt->count = 0;
3336 evt->flags &= ~DWC3_EVENT_PENDING;
3337 ret = IRQ_HANDLED;
3339 /* Unmask interrupt */
3340 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3341 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3342 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3344 if (dwc->imod_interval) {
3345 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3346 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3349 return ret;
3352 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3354 struct dwc3_event_buffer *evt = _evt;
3355 struct dwc3 *dwc = evt->dwc;
3356 unsigned long flags;
3357 irqreturn_t ret = IRQ_NONE;
3359 spin_lock_irqsave(&dwc->lock, flags);
3360 ret = dwc3_process_event_buf(evt);
3361 spin_unlock_irqrestore(&dwc->lock, flags);
3363 return ret;
3366 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3368 struct dwc3 *dwc = evt->dwc;
3369 u32 amount;
3370 u32 count;
3371 u32 reg;
3373 if (pm_runtime_suspended(dwc->dev)) {
3374 pm_runtime_get(dwc->dev);
3375 disable_irq_nosync(dwc->irq_gadget);
3376 dwc->pending_events = true;
3377 return IRQ_HANDLED;
3381 * With PCIe legacy interrupt, test shows that top-half irq handler can
3382 * be called again after HW interrupt deassertion. Check if bottom-half
3383 * irq event handler completes before caching new event to prevent
3384 * losing events.
3386 if (evt->flags & DWC3_EVENT_PENDING)
3387 return IRQ_HANDLED;
3389 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3390 count &= DWC3_GEVNTCOUNT_MASK;
3391 if (!count)
3392 return IRQ_NONE;
3394 evt->count = count;
3395 evt->flags |= DWC3_EVENT_PENDING;
3397 /* Mask interrupt */
3398 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3399 reg |= DWC3_GEVNTSIZ_INTMASK;
3400 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3402 amount = min(count, evt->length - evt->lpos);
3403 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3405 if (amount < count)
3406 memcpy(evt->cache, evt->buf, count - amount);
3408 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3410 return IRQ_WAKE_THREAD;
3413 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3415 struct dwc3_event_buffer *evt = _evt;
3417 return dwc3_check_event_buf(evt);
3420 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3422 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3423 int irq;
3425 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
3426 if (irq > 0)
3427 goto out;
3429 if (irq == -EPROBE_DEFER)
3430 goto out;
3432 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
3433 if (irq > 0)
3434 goto out;
3436 if (irq == -EPROBE_DEFER)
3437 goto out;
3439 irq = platform_get_irq(dwc3_pdev, 0);
3440 if (irq > 0)
3441 goto out;
3443 if (!irq)
3444 irq = -EINVAL;
3446 out:
3447 return irq;
3451 * dwc3_gadget_init - initializes gadget related registers
3452 * @dwc: pointer to our controller context structure
3454 * Returns 0 on success otherwise negative errno.
3456 int dwc3_gadget_init(struct dwc3 *dwc)
3458 int ret;
3459 int irq;
3461 irq = dwc3_gadget_get_irq(dwc);
3462 if (irq < 0) {
3463 ret = irq;
3464 goto err0;
3467 dwc->irq_gadget = irq;
3469 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3470 sizeof(*dwc->ep0_trb) * 2,
3471 &dwc->ep0_trb_addr, GFP_KERNEL);
3472 if (!dwc->ep0_trb) {
3473 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3474 ret = -ENOMEM;
3475 goto err0;
3478 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3479 if (!dwc->setup_buf) {
3480 ret = -ENOMEM;
3481 goto err1;
3484 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3485 &dwc->bounce_addr, GFP_KERNEL);
3486 if (!dwc->bounce) {
3487 ret = -ENOMEM;
3488 goto err2;
3491 init_completion(&dwc->ep0_in_setup);
3493 dwc->gadget.ops = &dwc3_gadget_ops;
3494 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3495 dwc->gadget.sg_supported = true;
3496 dwc->gadget.name = "dwc3-gadget";
3497 dwc->gadget.lpm_capable = true;
3500 * FIXME We might be setting max_speed to <SUPER, however versions
3501 * <2.20a of dwc3 have an issue with metastability (documented
3502 * elsewhere in this driver) which tells us we can't set max speed to
3503 * anything lower than SUPER.
3505 * Because gadget.max_speed is only used by composite.c and function
3506 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3507 * to happen so we avoid sending SuperSpeed Capability descriptor
3508 * together with our BOS descriptor as that could confuse host into
3509 * thinking we can handle super speed.
3511 * Note that, in fact, we won't even support GetBOS requests when speed
3512 * is less than super speed because we don't have means, yet, to tell
3513 * composite.c that we are USB 2.0 + LPM ECN.
3515 if (dwc->revision < DWC3_REVISION_220A &&
3516 !dwc->dis_metastability_quirk)
3517 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3518 dwc->revision);
3520 dwc->gadget.max_speed = dwc->maximum_speed;
3523 * REVISIT: Here we should clear all pending IRQs to be
3524 * sure we're starting from a well known location.
3527 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3528 if (ret)
3529 goto err3;
3531 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3532 if (ret) {
3533 dev_err(dwc->dev, "failed to register udc\n");
3534 goto err4;
3537 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3539 return 0;
3541 err4:
3542 dwc3_gadget_free_endpoints(dwc);
3544 err3:
3545 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3546 dwc->bounce_addr);
3548 err2:
3549 kfree(dwc->setup_buf);
3551 err1:
3552 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3553 dwc->ep0_trb, dwc->ep0_trb_addr);
3555 err0:
3556 return ret;
3559 /* -------------------------------------------------------------------------- */
3561 void dwc3_gadget_exit(struct dwc3 *dwc)
3563 usb_del_gadget_udc(&dwc->gadget);
3564 dwc3_gadget_free_endpoints(dwc);
3565 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3566 dwc->bounce_addr);
3567 kfree(dwc->setup_buf);
3568 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3569 dwc->ep0_trb, dwc->ep0_trb_addr);
3572 int dwc3_gadget_suspend(struct dwc3 *dwc)
3574 if (!dwc->gadget_driver)
3575 return 0;
3577 dwc3_gadget_run_stop(dwc, false, false);
3578 dwc3_disconnect_gadget(dwc);
3579 __dwc3_gadget_stop(dwc);
3581 return 0;
3584 int dwc3_gadget_resume(struct dwc3 *dwc)
3586 int ret;
3588 if (!dwc->gadget_driver)
3589 return 0;
3591 ret = __dwc3_gadget_start(dwc);
3592 if (ret < 0)
3593 goto err0;
3595 ret = dwc3_gadget_run_stop(dwc, true, false);
3596 if (ret < 0)
3597 goto err1;
3599 return 0;
3601 err1:
3602 __dwc3_gadget_stop(dwc);
3604 err0:
3605 return ret;
3608 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3610 if (dwc->pending_events) {
3611 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3612 dwc->pending_events = false;
3613 enable_irq(dwc->irq_gadget);