blob: 90f4f9e69b227f1fb555b3af2269639af7d042d4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
Alexander A. Klimov10623b82020-07-11 15:58:04 +02005 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbid5370102018-08-14 10:42:43 +030030#define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
Felipe Balbif62afb42018-04-11 10:34:34 +030031 & ~((d)->interval - 1))
32
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020033/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030034 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020035 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030038 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 */
41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48 switch (mode) {
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +020049 case USB_TEST_J:
50 case USB_TEST_K:
51 case USB_TEST_SE0_NAK:
52 case USB_TEST_PACKET:
53 case USB_TEST_FORCE_ENABLE:
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020054 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
Thinh Nguyen5b738212019-10-23 19:15:43 -070060 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020061
62 return 0;
63}
64
Felipe Balbi8598bde2012-01-02 18:55:57 +020065/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030066 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030067 * @dwc: pointer to our context structure
68 *
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
71 */
72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78 return DWC3_DSTS_USBLNKST(reg);
79}
80
81/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030082 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020083 * @dwc: pointer to our context structure
84 * @state: the state to put link into
85 *
86 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080087 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 */
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080091 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 u32 reg;
93
Paul Zimmerman802fde92012-04-27 13:10:52 +030094 /*
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
97 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -070098 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
Felipe Balbi8598bde2012-01-02 18:55:57 +0200111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
Thinh Nguyen2e708fa2019-10-23 19:15:55 -0700114 /* set no action before sending new link state change */
115 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 /* set requested state */
118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120
Paul Zimmerman802fde92012-04-27 13:10:52 +0300121 /*
122 * The following code is racy when called from dwc3_gadget_wakeup,
123 * and is not needed, at least on newer versions
124 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +0300126 return 0;
127
Felipe Balbi8598bde2012-01-02 18:55:57 +0200128 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300129 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 while (--retries) {
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 if (DWC3_DSTS_USBLNKST(reg) == state)
134 return 0;
135
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800136 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 }
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 return -ETIMEDOUT;
140}
141
John Youndca01192016-05-19 17:26:05 -0700142/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300143 * dwc3_ep_inc_trb - increment a trb index.
144 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700145 *
146 * The index should never point to the link TRB. After incrementing,
147 * if it is point to the link TRB, wrap around to the beginning. The
148 * link TRB is always at the last TRB entry.
149 */
150static void dwc3_ep_inc_trb(u8 *index)
151{
152 (*index)++;
153 if (*index == (DWC3_TRB_NUM - 1))
154 *index = 0;
155}
156
Felipe Balbibfad65e2017-04-19 14:59:27 +0300157/**
158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159 * @dep: The endpoint whose enqueue pointer we're incrementing
160 */
Felipe Balbief966b92016-04-05 13:09:51 +0300161static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162{
John Youndca01192016-05-19 17:26:05 -0700163 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300164}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200165
Felipe Balbibfad65e2017-04-19 14:59:27 +0300166/**
167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168 * @dep: The endpoint whose enqueue pointer we're incrementing
169 */
Felipe Balbief966b92016-04-05 13:09:51 +0300170static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171{
John Youndca01192016-05-19 17:26:05 -0700172 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200173}
174
Wei Yongjun69102512018-03-29 02:20:10 +0000175static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300176 struct dwc3_request *req, int status)
177{
178 struct dwc3 *dwc = dep->dwc;
179
Felipe Balbic91815b2018-03-26 13:14:47 +0300180 list_del(&req->list);
181 req->remaining = 0;
Jack Phambd6742242019-01-10 12:39:55 -0800182 req->needs_extra_trb = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300183
184 if (req->request.status == -EINPROGRESS)
185 req->request.status = status;
186
187 if (req->trb)
188 usb_gadget_unmap_request_by_dev(dwc->sysdev,
189 &req->request, req->direction);
190
191 req->trb = NULL;
192 trace_dwc3_gadget_giveback(req);
193
194 if (dep->number > 1)
195 pm_runtime_put(dwc->dev);
196}
197
Felipe Balbibfad65e2017-04-19 14:59:27 +0300198/**
199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
200 * @dep: The endpoint to whom the request belongs to
201 * @req: The request we're giving back
202 * @status: completion code for the request
203 *
204 * Must be called with controller's lock held and interrupts disabled. This
205 * function will unmap @req and call its ->complete() callback to notify upper
206 * layers that it has completed.
207 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300208void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
209 int status)
210{
211 struct dwc3 *dwc = dep->dwc;
212
Felipe Balbic91815b2018-03-26 13:14:47 +0300213 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbia3af5e32019-01-11 12:57:09 +0200214 req->status = DWC3_REQUEST_STATUS_COMPLETED;
Felipe Balbi72246da2011-08-19 18:10:58 +0300215
216 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200217 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 spin_lock(&dwc->lock);
219}
220
Felipe Balbibfad65e2017-04-19 14:59:27 +0300221/**
222 * dwc3_send_gadget_generic_command - issue a generic command for the controller
223 * @dwc: pointer to the controller context
224 * @cmd: the command to be issued
225 * @param: command parameter
226 *
227 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
228 * and wait for its completion.
229 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300230int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231 u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300232{
233 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300234 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236 u32 reg;
237
238 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
239 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240
241 do {
242 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
243 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300244 status = DWC3_DGCMD_STATUS(reg);
245 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 ret = -EINVAL;
247 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300248 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100249 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250
251 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300252 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300253 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300254 }
255
Felipe Balbi71f7e702016-05-23 14:16:19 +0300256 trace_dwc3_gadget_generic_cmd(cmd, param, status);
257
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300258 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300259}
260
Felipe Balbic36d8e92016-04-04 12:46:33 +0300261static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262
Felipe Balbibfad65e2017-04-19 14:59:27 +0300263/**
264 * dwc3_send_gadget_ep_cmd - issue an endpoint command
265 * @dep: the endpoint to which the command is going to be issued
266 * @cmd: the command to be issued
267 * @params: parameters to the command
268 *
269 * Caller should handle locking. This function will issue @cmd with given
270 * @params to @dep and wait for its completion.
271 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300272int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
Felipe Balbi2cd47182016-04-12 16:42:43 +0300273 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300274{
Felipe Balbi8897a762016-09-22 10:56:08 +0300275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300276 struct dwc3 *dwc = dep->dwc;
Yu Chen1c0e69a2020-05-21 16:46:43 +0800277 u32 timeout = 5000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700278 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 u32 reg;
280
Felipe Balbi0933df12016-05-23 14:02:33 +0300281 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300282 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300283
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700285 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
286 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
287 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300288 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700289 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
290 * settings. Restore them after the command is completed.
291 *
292 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300293 */
Peter Chene81a7012020-08-21 10:55:48 +0800294 if (dwc->gadget->speed <= USB_SPEED_HIGH) {
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300295 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
296 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700297 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300298 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300299 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700300
301 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
302 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
303 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
304 }
305
306 if (saved_config)
307 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300308 }
309
Felipe Balbi59999142016-09-22 12:25:28 +0300310 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Thinh Nguyenc560e762021-04-19 19:11:12 -0700311 int link_state;
Felipe Balbic36d8e92016-04-04 12:46:33 +0300312
Thinh Nguyenc560e762021-04-19 19:11:12 -0700313 link_state = dwc3_gadget_get_link_state(dwc);
314 if (link_state == DWC3_LINK_STATE_U1 ||
315 link_state == DWC3_LINK_STATE_U2 ||
316 link_state == DWC3_LINK_STATE_U3) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300317 ret = __dwc3_gadget_wakeup(dwc);
318 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
319 ret);
320 }
321 }
322
Felipe Balbi2eb88012016-04-12 16:53:39 +0300323 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
324 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
325 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326
Felipe Balbi8897a762016-09-22 10:56:08 +0300327 /*
328 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
329 * not relying on XferNotReady, we can make use of a special "No
330 * Response Update Transfer" command where we should clear both CmdAct
331 * and CmdIOC bits.
332 *
333 * With this, we don't need to wait for command completion and can
334 * straight away issue further commands to the endpoint.
335 *
336 * NOTICE: We're making an assumption that control endpoints will never
337 * make use of Update Transfer command. This is a safe assumption
338 * because we can never have more than one request at a time with
339 * Control Endpoints. If anybody changes that assumption, this chunk
340 * needs to be updated accordingly.
341 */
342 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
343 !usb_endpoint_xfer_isoc(desc))
344 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
345 else
346 cmd |= DWC3_DEPCMD_CMDACT;
347
348 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300349 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300350 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300351 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300352 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000353
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000354 switch (cmd_status) {
355 case 0:
356 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300357 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000358 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700359 dev_WARN(dwc->dev, "No resource for %s\n",
360 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000361 ret = -EINVAL;
362 break;
363 case DEPEVT_TRANSFER_BUS_EXPIRY:
364 /*
365 * SW issues START TRANSFER command to
366 * isochronous ep with future frame interval. If
367 * future interval time has already passed when
368 * core receives the command, it will respond
369 * with an error status of 'Bus Expiry'.
370 *
371 * Instead of always returning -EINVAL, let's
372 * give a hint to the gadget driver that this is
373 * the case by returning -EAGAIN.
374 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000375 ret = -EAGAIN;
376 break;
377 default:
378 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
379 }
380
Felipe Balbic0ca3242016-04-04 09:11:51 +0300381 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300383 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300384
Felipe Balbif6bb2252016-05-23 13:53:34 +0300385 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300386 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300387 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300388 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300389
Felipe Balbi0933df12016-05-23 14:02:33 +0300390 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
391
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700392 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
393 if (ret == 0)
394 dep->flags |= DWC3_EP_TRANSFER_STARTED;
395
396 if (ret != -ETIMEDOUT)
397 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300398 }
399
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700400 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300401 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700402 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300403 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
404 }
405
Felipe Balbic0ca3242016-04-04 09:11:51 +0300406 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407}
408
John Youn50c763f2016-05-31 17:49:56 -0700409static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
410{
411 struct dwc3 *dwc = dep->dwc;
412 struct dwc3_gadget_ep_cmd_params params;
413 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
414
415 /*
416 * As of core revision 2.60a the recommended programming model
417 * is to set the ClearPendIN bit when issuing a Clear Stall EP
418 * command for IN endpoints. This is to prevent an issue where
419 * some (non-compliant) hosts may not send ACK TPs for pending
420 * IN transfers due to a mishandled error condition. Synopsys
421 * STAR 9000614252.
422 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700423 if (dep->direction &&
424 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800425 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700426 cmd |= DWC3_DEPCMD_CLEARPENDIN;
427
428 memset(&params, 0, sizeof(params));
429
Felipe Balbi2cd47182016-04-12 16:42:43 +0300430 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700431}
432
Felipe Balbi72246da2011-08-19 18:10:58 +0300433static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200434 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300435{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300436 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300437
438 return dep->trb_pool_dma + offset;
439}
440
441static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
442{
443 struct dwc3 *dwc = dep->dwc;
444
445 if (dep->trb_pool)
446 return 0;
447
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530448 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300449 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
450 &dep->trb_pool_dma, GFP_KERNEL);
451 if (!dep->trb_pool) {
452 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
453 dep->name);
454 return -ENOMEM;
455 }
456
457 return 0;
458}
459
460static void dwc3_free_trb_pool(struct dwc3_ep *dep)
461{
462 struct dwc3 *dwc = dep->dwc;
463
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530464 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 dep->trb_pool, dep->trb_pool_dma);
466
467 dep->trb_pool = NULL;
468 dep->trb_pool_dma = 0;
469}
470
Felipe Balbi20d1d432018-04-09 12:49:02 +0300471static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
472{
473 struct dwc3_gadget_ep_cmd_params params;
474
475 memset(&params, 0x00, sizeof(params));
476
477 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
478
479 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
480 &params);
481}
John Younc4509602016-02-16 20:10:53 -0800482
483/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300484 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800485 * @dep: endpoint that is being enabled
486 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300487 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
488 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800489 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300490 * The assignment of transfer resources cannot perfectly follow the data book
491 * due to the fact that the controller driver does not have all knowledge of the
492 * configuration in advance. It is given this information piecemeal by the
493 * composite gadget framework after every SET_CONFIGURATION and
494 * SET_INTERFACE. Trying to follow the databook programming model in this
495 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800496 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300497 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
498 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
499 * incorrect in the scenario of multiple interfaces.
500 *
501 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800502 * endpoint on alt setting (8.1.6).
503 *
504 * The following simplified method is used instead:
505 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300506 * All hardware endpoints can be assigned a transfer resource and this setting
507 * will stay persistent until either a core reset or hibernation. So whenever we
508 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
509 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800510 * guaranteed that there are as many transfer resources as endpoints.
511 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300512 * This function is called for each endpoint when it is being enabled but is
513 * triggered only when called for EP0-out, which always happens first, and which
514 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800515 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300516static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300517{
518 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300519 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800521 int i;
522 int ret;
523
524 if (dep->number)
525 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300526
527 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800528 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300529 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
Felipe Balbi2cd47182016-04-12 16:42:43 +0300531 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800532 if (ret)
533 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
John Younc4509602016-02-16 20:10:53 -0800535 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
536 struct dwc3_ep *dep = dwc->eps[i];
537
538 if (!dep)
539 continue;
540
Felipe Balbib07c2db2018-04-09 12:46:47 +0300541 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800542 if (ret)
543 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 }
545
546 return 0;
547}
548
Felipe Balbib07c2db2018-04-09 12:46:47 +0300549static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300550{
John Youn39ebb052016-11-09 16:36:28 -0800551 const struct usb_ss_ep_comp_descriptor *comp_desc;
552 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300554 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
John Youn39ebb052016-11-09 16:36:28 -0800556 comp_desc = dep->endpoint.comp_desc;
557 desc = dep->endpoint.desc;
558
Felipe Balbi72246da2011-08-19 18:10:58 +0300559 memset(&params, 0x00, sizeof(params));
560
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300561 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900562 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
563
564 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800565 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300566 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300567
Felipe Balbi676e3492016-04-26 10:49:07 +0300568 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900569 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
Felipe Balbia2d23f02018-04-09 12:40:48 +0300571 params.param0 |= action;
572 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600573 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600574
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300575 if (usb_endpoint_xfer_control(desc))
576 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300577
578 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
579 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200581 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300582 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700583 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300584 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300585 dep->stream_capable = true;
586 }
587
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500588 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300589 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
591 /*
592 * We are doing 1:1 mapping for endpoints, meaning
593 * Physical Endpoints 2 maps to Logical Endpoint 2 and
594 * so on. We consider the direction bit as part of the physical
595 * endpoint number. So USB endpoint 0x81 is 0x03.
596 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300597 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
599 /*
600 * We must use the lower 16 TX FIFOs even though
601 * HW might have more
602 */
603 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300604 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300605
606 if (desc->bInterval) {
Thinh Nguyena1679af2021-02-08 13:53:10 -0800607 u8 bInterval_m1;
608
609 /*
Thinh Nguyen3232a3c2021-04-15 00:41:58 -0700610 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
611 *
612 * NOTE: The programming guide incorrectly stated bInterval_m1
613 * must be set to 0 when operating in fullspeed. Internally the
614 * controller does not have this limitation. See DWC_usb3x
615 * programming guide section 3.2.2.1.
Thinh Nguyena1679af2021-02-08 13:53:10 -0800616 */
617 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
Thinh Nguyena1679af2021-02-08 13:53:10 -0800618
Thinh Nguyen4b049f52021-02-08 13:53:16 -0800619 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
620 dwc->gadget->speed == USB_SPEED_FULL)
621 dep->interval = desc->bInterval;
622 else
623 dep->interval = 1 << (desc->bInterval - 1);
624
Thinh Nguyena1679af2021-02-08 13:53:10 -0800625 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 }
627
Felipe Balbi2cd47182016-04-12 16:42:43 +0300628 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300629}
630
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700631static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
632 bool interrupt);
633
Felipe Balbi72246da2011-08-19 18:10:58 +0300634/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300635 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300637 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300638 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300639 * Caller should take care of locking. Execute all necessary commands to
640 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300642static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300643{
John Youn39ebb052016-11-09 16:36:28 -0800644 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800646
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300648 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300649
650 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300651 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 if (ret)
653 return ret;
654 }
655
Felipe Balbib07c2db2018-04-09 12:46:47 +0300656 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 if (ret)
658 return ret;
659
660 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200661 struct dwc3_trb *trb_st_hw;
662 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300663
Felipe Balbi72246da2011-08-19 18:10:58 +0300664 dep->type = usb_endpoint_type(desc);
665 dep->flags |= DWC3_EP_ENABLED;
666
667 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
668 reg |= DWC3_DALEPENA_EP(dep->number);
669 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
670
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300671 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200672 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300673
John Youn0d257442016-05-19 17:26:08 -0700674 /* Initialize the TRB ring */
675 dep->trb_dequeue = 0;
676 dep->trb_enqueue = 0;
677 memset(dep->trb_pool, 0,
678 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
679
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300680 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 trb_st_hw = &dep->trb_pool[0];
682
Felipe Balbif6bafc62012-02-06 11:04:53 +0200683 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200684 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
685 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
686 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
687 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 }
689
Felipe Balbia97ea992016-09-29 16:28:56 +0300690 /*
691 * Issue StartTransfer here with no-op TRB so we can always rely on No
692 * Response Update Transfer command.
693 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700694 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300695 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300696 struct dwc3_gadget_ep_cmd_params params;
697 struct dwc3_trb *trb;
698 dma_addr_t trb_dma;
699 u32 cmd;
700
701 memset(&params, 0, sizeof(params));
702 trb = &dep->trb_pool[0];
703 trb_dma = dwc3_trb_dma_offset(dep, trb);
704
705 params.param0 = upper_32_bits(trb_dma);
706 params.param1 = lower_32_bits(trb_dma);
707
708 cmd = DWC3_DEPCMD_STARTTRANSFER;
709
710 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
711 if (ret < 0)
712 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700713
714 if (dep->stream_capable) {
715 /*
716 * For streams, at start, there maybe a race where the
717 * host primes the endpoint before the function driver
718 * queues a request to initiate a stream. In that case,
719 * the controller will not see the prime to generate the
720 * ERDY and start stream. To workaround this, issue a
721 * no-op TRB as normal, but end it immediately. As a
722 * result, when the function driver queues the request,
723 * the next START_TRANSFER command will cause the
724 * controller to generate an ERDY to initiate the
725 * stream.
726 */
727 dwc3_stop_active_transfer(dep, true, true);
728
729 /*
730 * All stream eps will reinitiate stream on NoStream
731 * rejection until we can determine that the host can
732 * prime after the first transfer.
733 */
734 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
735 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300736 }
737
Felipe Balbi2870e502016-11-03 13:53:29 +0200738out:
739 trace_dwc3_gadget_ep_enable(dep);
740
Felipe Balbi72246da2011-08-19 18:10:58 +0300741 return 0;
742}
743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300745{
746 struct dwc3_request *req;
747
Felipe Balbic5353b22019-02-13 13:00:54 +0200748 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300749
Felipe Balbi0e146022016-06-21 10:32:02 +0300750 /* - giveback all requests to gadget driver */
751 while (!list_empty(&dep->started_list)) {
752 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200753
Felipe Balbi0e146022016-06-21 10:32:02 +0300754 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200755 }
756
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200757 while (!list_empty(&dep->pending_list)) {
758 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300759
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200760 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200762
763 while (!list_empty(&dep->cancelled_list)) {
764 req = next_request(&dep->cancelled_list);
765
766 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
767 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300768}
769
770/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300771 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 * @dep: the endpoint to disable
773 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300774 * This function undoes what __dwc3_gadget_ep_enable did and also removes
775 * requests which are currently being processed by the hardware and those which
776 * are not yet scheduled.
777 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200778 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300779 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300780static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
781{
782 struct dwc3 *dwc = dep->dwc;
783 u32 reg;
784
Felipe Balbi2870e502016-11-03 13:53:29 +0200785 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500786
Felipe Balbi687ef982014-04-16 10:30:33 -0500787 /* make sure HW endpoint isn't stalled */
788 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500789 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500790
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
792 reg &= ~DWC3_DALEPENA_EP(dep->number);
793 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
794
John Youn39ebb052016-11-09 16:36:28 -0800795 /* Clear out the ep descriptors for non-ep0 */
796 if (dep->number > 1) {
797 dep->endpoint.comp_desc = NULL;
798 dep->endpoint.desc = NULL;
799 }
800
Wesley Chengf09ddcf2021-03-11 15:59:02 -0800801 dwc3_remove_requests(dwc, dep);
802
Wesley Cheng5aef62972021-03-24 11:31:04 -0700803 dep->stream_capable = false;
804 dep->type = 0;
805 dep->flags = 0;
806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 return 0;
808}
809
810/* -------------------------------------------------------------------------- */
811
812static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
813 const struct usb_endpoint_descriptor *desc)
814{
815 return -EINVAL;
816}
817
818static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
819{
820 return -EINVAL;
821}
822
823/* -------------------------------------------------------------------------- */
824
825static int dwc3_gadget_ep_enable(struct usb_ep *ep,
826 const struct usb_endpoint_descriptor *desc)
827{
828 struct dwc3_ep *dep;
829 struct dwc3 *dwc;
830 unsigned long flags;
831 int ret;
832
833 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
834 pr_debug("dwc3: invalid parameters\n");
835 return -EINVAL;
836 }
837
838 if (!desc->wMaxPacketSize) {
839 pr_debug("dwc3: missing wMaxPacketSize\n");
840 return -EINVAL;
841 }
842
843 dep = to_dwc3_ep(ep);
844 dwc = dep->dwc;
845
Felipe Balbi95ca9612015-12-10 13:08:20 -0600846 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
847 "%s is already enabled\n",
848 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300849 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300850
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300852 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 spin_unlock_irqrestore(&dwc->lock, flags);
854
855 return ret;
856}
857
858static int dwc3_gadget_ep_disable(struct usb_ep *ep)
859{
860 struct dwc3_ep *dep;
861 struct dwc3 *dwc;
862 unsigned long flags;
863 int ret;
864
865 if (!ep) {
866 pr_debug("dwc3: invalid parameters\n");
867 return -EINVAL;
868 }
869
870 dep = to_dwc3_ep(ep);
871 dwc = dep->dwc;
872
Felipe Balbi95ca9612015-12-10 13:08:20 -0600873 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
874 "%s is already disabled\n",
875 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300877
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 spin_lock_irqsave(&dwc->lock, flags);
879 ret = __dwc3_gadget_ep_disable(dep);
880 spin_unlock_irqrestore(&dwc->lock, flags);
881
882 return ret;
883}
884
885static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300886 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300887{
888 struct dwc3_request *req;
889 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300890
891 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900892 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300893 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300895 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300896 req->epnum = dep->number;
897 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200898 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300899
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500900 trace_dwc3_alloc_request(req);
901
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 return &req->request;
903}
904
905static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
906 struct usb_request *request)
907{
908 struct dwc3_request *req = to_dwc3_request(request);
909
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500910 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300911 kfree(req);
912}
913
Felipe Balbi42626912018-04-09 13:01:43 +0300914/**
915 * dwc3_ep_prev_trb - returns the previous TRB in the ring
916 * @dep: The endpoint with the TRB ring
917 * @index: The index of the current TRB in the ring
918 *
919 * Returns the TRB prior to the one pointed to by the index. If the
920 * index is 0, we will wrap backwards, skip the link TRB, and return
921 * the one just before that.
922 */
923static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
924{
925 u8 tmp = index;
926
927 if (!tmp)
928 tmp = DWC3_TRB_NUM - 1;
929
930 return &dep->trb_pool[tmp - 1];
931}
932
933static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
934{
935 struct dwc3_trb *tmp;
936 u8 trbs_left;
937
938 /*
939 * If enqueue & dequeue are equal than it is either full or empty.
940 *
941 * One way to know for sure is if the TRB right before us has HWO bit
942 * set or not. If it has, then we're definitely full and can't fit any
943 * more transfers in our ring.
944 */
945 if (dep->trb_enqueue == dep->trb_dequeue) {
946 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
947 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
948 return 0;
949
950 return DWC3_TRB_NUM - 1;
951 }
952
953 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
954 trbs_left &= (DWC3_TRB_NUM - 1);
955
956 if (dep->trb_dequeue < dep->trb_enqueue)
957 trbs_left--;
958
959 return trbs_left;
960}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300961
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200962static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300963 dma_addr_t dma, unsigned int length, unsigned int chain,
964 unsigned int node, unsigned int stream_id,
965 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700966 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200967{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300968 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800969 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300970 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200971
Felipe Balbif6bafc62012-02-06 11:04:53 +0200972 trb->size = DWC3_TRB_SIZE_LENGTH(length);
973 trb->bpl = lower_32_bits(dma);
974 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200975
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200976 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200977 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200978 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200979 break;
980
981 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300982 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530983 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300984
Manu Gautam40d829f2017-07-19 17:07:10 +0530985 /*
986 * USB Specification 2.0 Section 5.9.2 states that: "If
987 * there is only a single transaction in the microframe,
988 * only a DATA0 data packet PID is used. If there are
989 * two transactions per microframe, DATA1 is used for
990 * the first transaction data packet and DATA0 is used
991 * for the second transaction data packet. If there are
992 * three transactions per microframe, DATA2 is used for
993 * the first transaction data packet, DATA1 is used for
994 * the second, and DATA0 is used for the third."
995 *
996 * IOW, we should satisfy the following cases:
997 *
998 * 1) length <= maxpacket
999 * - DATA0
1000 *
1001 * 2) maxpacket < length <= (2 * maxpacket)
1002 * - DATA1, DATA0
1003 *
1004 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1005 * - DATA2, DATA1, DATA0
1006 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001007 if (speed == USB_SPEED_HIGH) {
1008 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301009 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301010 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1011
1012 if (length <= (2 * maxp))
1013 mult--;
1014
1015 if (length <= maxp)
1016 mult--;
1017
1018 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001019 }
1020 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301021 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001022 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001023
1024 /* always enable Interrupt on Missed ISOC */
1025 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001026 break;
1027
1028 case USB_ENDPOINT_XFER_BULK:
1029 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001030 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001031 break;
1032 default:
1033 /*
1034 * This is only possible with faulty memory because we
1035 * checked it already :)
1036 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001037 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1038 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001039 }
1040
Tejas Joglekar244add82018-12-10 16:08:13 +05301041 /*
1042 * Enable Continue on Short Packet
1043 * when endpoint is not a stream capable
1044 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001045 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301046 if (!dep->stream_capable)
1047 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001048
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001049 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001050 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1051 }
1052
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001053 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001054 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001055
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301056 if (chain)
1057 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001058 else if (dep->stream_capable && is_last)
1059 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301060
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001061 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001062 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001063
1064 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001065
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301066 dwc3_ep_inc_enq(dep);
1067
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001068 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001069}
1070
John Youn361572b2016-05-19 17:26:17 -07001071/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001072 * dwc3_prepare_one_trb - setup one TRB from one request
1073 * @dep: endpoint for which this request is prepared
1074 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001075 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001076 * @chain: should this TRB be chained to the next?
1077 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001078 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001079 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001080 */
1081static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001082 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001083 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1084 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001085{
1086 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301087 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001088 unsigned int stream_id = req->request.stream_id;
1089 unsigned int short_not_ok = req->request.short_not_ok;
1090 unsigned int no_interrupt = req->request.no_interrupt;
1091 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301092
Thinh Nguyen2b803572020-09-24 01:21:30 -07001093 if (use_bounce_buffer)
1094 dma = dep->dwc->bounce_addr;
1095 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301096 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001097 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301098 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001099
1100 trb = &dep->trb_pool[dep->trb_enqueue];
1101
1102 if (!req->trb) {
1103 dwc3_gadget_move_started_request(req);
1104 req->trb = trb;
1105 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001106 }
1107
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001108 req->num_trbs++;
1109
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001110 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001111 stream_id, short_not_ok, no_interrupt, is_last,
1112 must_interrupt);
1113}
1114
1115static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1116{
1117 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1118 unsigned int rem = req->request.length % maxp;
1119
1120 if ((req->request.length && req->request.zero && !rem &&
1121 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1122 (!req->direction && rem))
1123 return true;
1124
1125 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001126}
1127
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001128/**
1129 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1130 * @dep: The endpoint that the request belongs to
1131 * @req: The request to prepare
1132 * @entry_length: The last SG entry size
1133 * @node: Indicates whether this is not the first entry (for isoc only)
1134 *
1135 * Return the number of TRBs prepared.
1136 */
1137static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1138 struct dwc3_request *req, unsigned int entry_length,
1139 unsigned int node)
1140{
1141 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1142 unsigned int rem = req->request.length % maxp;
1143 unsigned int num_trbs = 1;
1144
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001145 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001146 num_trbs++;
1147
1148 if (dwc3_calc_trbs_left(dep) < num_trbs)
1149 return 0;
1150
1151 req->needs_extra_trb = num_trbs > 1;
1152
1153 /* Prepare a normal TRB */
1154 if (req->direction || req->request.length)
1155 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001156 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001157
1158 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1159 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1160 dwc3_prepare_one_trb(dep, req,
1161 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001162 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001163
1164 return num_trbs;
1165}
1166
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001167static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001168 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001169{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301170 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001171 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001172 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001173 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301174 unsigned int remaining = req->request.num_mapped_sgs
1175 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001176 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001177 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301178
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001179 /*
1180 * If we resume preparing the request, then get the remaining length of
1181 * the request and resume where we left off.
1182 */
1183 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1184 length -= sg_dma_len(s);
1185
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301186 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001187 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001188 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001189 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001190 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001191
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001192 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1193
1194 length -= trb_length;
1195
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001196 /*
1197 * IOMMU driver is coalescing the list of sgs which shares a
1198 * page boundary into one and giving it to USB driver. With
1199 * this the number of sgs mapped is not equal to the number of
1200 * sgs passed. So mark the chain bit to false if it isthe last
1201 * mapped sg.
1202 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001203 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001204 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001205
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001206 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001207 break;
1208
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001209 if (last_sg) {
1210 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001211 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001212 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001213 /*
1214 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001215 * next SG entry. If not, set interrupt on this TRB to
1216 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001217 * free.
1218 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001219 if (num_trbs_left == 1 || (needs_extra_trb &&
1220 num_trbs_left <= 2 &&
1221 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001222 must_interrupt = true;
1223
1224 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1225 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001226 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001227
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301228 /*
1229 * There can be a situation where all sgs in sglist are not
1230 * queued because of insufficient trb number. To handle this
1231 * case, update start_sg to next sg to be queued, so that
1232 * we have free trbs we can continue queuing from where we
1233 * previously stopped
1234 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001235 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301236 req->start_sg = sg_next(s);
1237
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301238 req->num_queued_sgs++;
1239
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001240 /*
1241 * The number of pending SG entries may not correspond to the
1242 * number of mapped SG entries. If all the data are queued, then
1243 * don't include unused SG entries.
1244 */
1245 if (length == 0) {
1246 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1247 break;
1248 }
1249
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001250 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001251 break;
1252 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001253
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001254 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001255}
1256
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001257static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001258 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001259{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001260 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001261}
1262
Felipe Balbi72246da2011-08-19 18:10:58 +03001263/*
1264 * dwc3_prepare_trbs - setup TRBs from requests
1265 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001267 * The function goes through the requests list and sets up TRBs for the
1268 * transfers. The function returns once there are no more TRBs available or
1269 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001270 *
1271 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001273static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001274{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001275 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001276 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001277
1278 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1279
Felipe Balbid86c5a62016-10-25 13:48:52 +03001280 /*
1281 * We can get in a situation where there's a request in the started list
1282 * but there weren't enough TRBs to fully kick it in the first time
1283 * around, so it has been waiting for more TRBs to be freed up.
1284 *
1285 * In that case, we should check if we have a request with pending_sgs
1286 * in the started list and prepare TRBs for that request first,
1287 * otherwise we will prepare TRBs completely out of order and that will
1288 * break things.
1289 */
1290 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001291 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001292 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001293 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001294 return ret;
1295 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001296
1297 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001298 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001299
1300 /*
1301 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1302 * burst capability may try to read and use TRBs beyond the
1303 * active transfer instead of stopping.
1304 */
1305 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001306 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001307 }
1308
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001309 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001310 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001311
1312 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1313 dep->direction);
1314 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001315 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001316
1317 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301318 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301319 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001320 req->num_pending_sgs = req->request.num_mapped_sgs;
1321
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001322 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001323 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001324 if (req->num_pending_sgs)
1325 return ret;
1326 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001327 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001328 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001329
Thinh Nguyen490410b2020-09-24 01:21:55 -07001330 if (!ret || !dwc3_calc_trbs_left(dep))
1331 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001332
1333 /*
1334 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1335 * burst capability may try to read and use TRBs beyond the
1336 * active transfer instead of stopping.
1337 */
1338 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001339 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001340 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001341
1342 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001343}
1344
Thinh Nguyen8d990872020-03-29 16:12:57 -07001345static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1346
Felipe Balbi7fdca762017-09-05 14:41:34 +03001347static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001348{
1349 struct dwc3_gadget_ep_cmd_params params;
1350 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001351 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 int ret;
1353 u32 cmd;
1354
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001355 /*
1356 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1357 * This happens when we need to stop and restart a transfer such as in
1358 * the case of reinitiating a stream or retrying an isoc transfer.
1359 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001360 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001361 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001362 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001363
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001364 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001365
Thinh Nguyen23384842020-09-30 17:44:38 -07001366 /*
1367 * If there's no new TRB prepared and we don't need to restart a
1368 * transfer, there's no need to update the transfer.
1369 */
1370 if (!ret && !starting)
1371 return ret;
1372
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001373 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001374 if (!req) {
1375 dep->flags |= DWC3_EP_PENDING_REQUEST;
1376 return 0;
1377 }
1378
1379 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001380
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001381 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301382 params.param0 = upper_32_bits(req->trb_dma);
1383 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001384 cmd = DWC3_DEPCMD_STARTTRANSFER;
1385
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301386 if (dep->stream_capable)
1387 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1388
Felipe Balbi7fdca762017-09-05 14:41:34 +03001389 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1390 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301391 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001392 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1393 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301394 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001395
Felipe Balbi2cd47182016-04-12 16:42:43 +03001396 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001397 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001398 struct dwc3_request *tmp;
1399
1400 if (ret == -EAGAIN)
1401 return ret;
1402
1403 dwc3_stop_active_transfer(dep, true, true);
1404
1405 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001406 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
Thinh Nguyen8d990872020-03-29 16:12:57 -07001407
1408 /* If ep isn't started, then there's no end transfer pending */
1409 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1410 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1411
Felipe Balbi72246da2011-08-19 18:10:58 +03001412 return ret;
1413 }
1414
Thinh Nguyene0d19562020-05-05 19:46:57 -07001415 if (dep->stream_capable && req->request.is_last)
1416 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1417
Felipe Balbi72246da2011-08-19 18:10:58 +03001418 return 0;
1419}
1420
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001421static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1422{
1423 u32 reg;
1424
1425 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1426 return DWC3_DSTS_SOFFN(reg);
1427}
1428
Thinh Nguyend92021f2018-11-14 22:56:54 -08001429/**
1430 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1431 * @dep: isoc endpoint
1432 *
1433 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1434 * microframe number reported by the XferNotReady event for the future frame
1435 * number to start the isoc transfer.
1436 *
1437 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1438 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1439 * XferNotReady event are invalid. The driver uses this number to schedule the
1440 * isochronous transfer and passes it to the START TRANSFER command. Because
1441 * this number is invalid, the command may fail. If BIT[15:14] matches the
1442 * internal 16-bit microframe, the START TRANSFER command will pass and the
1443 * transfer will start at the scheduled time, if it is off by 1, the command
1444 * will still pass, but the transfer will start 2 seconds in the future. For all
1445 * other conditions, the START TRANSFER command will fail with bus-expiry.
1446 *
1447 * In order to workaround this issue, we can test for the correct combination of
1448 * BIT[15:14] by sending START TRANSFER commands with different values of
1449 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1450 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1451 * As the result, within the 4 possible combinations for BIT[15:14], there will
1452 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1453 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1454 * value is the correct combination.
1455 *
1456 * Since there are only 4 outcomes and the results are ordered, we can simply
1457 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1458 * deduce the smaller successful combination.
1459 *
1460 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1461 * of BIT[15:14]. The correct combination is as follow:
1462 *
1463 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1464 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1465 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1466 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1467 *
1468 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1469 * endpoints.
1470 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001471static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301472{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001473 int cmd_status = 0;
1474 bool test0;
1475 bool test1;
1476
1477 while (dep->combo_num < 2) {
1478 struct dwc3_gadget_ep_cmd_params params;
1479 u32 test_frame_number;
1480 u32 cmd;
1481
1482 /*
1483 * Check if we can start isoc transfer on the next interval or
1484 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1485 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001486 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001487 test_frame_number |= dep->combo_num << 14;
1488 test_frame_number += max_t(u32, 4, dep->interval);
1489
1490 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1491 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1492
1493 cmd = DWC3_DEPCMD_STARTTRANSFER;
1494 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1495 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1496
1497 /* Redo if some other failure beside bus-expiry is received */
1498 if (cmd_status && cmd_status != -EAGAIN) {
1499 dep->start_cmd_status = 0;
1500 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001501 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001502 }
1503
1504 /* Store the first test status */
1505 if (dep->combo_num == 0)
1506 dep->start_cmd_status = cmd_status;
1507
1508 dep->combo_num++;
1509
1510 /*
1511 * End the transfer if the START_TRANSFER command is successful
1512 * to wait for the next XferNotReady to test the command again
1513 */
1514 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001515 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001516 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001517 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301518 }
1519
Thinh Nguyend92021f2018-11-14 22:56:54 -08001520 /* test0 and test1 are both completed at this point */
1521 test0 = (dep->start_cmd_status == 0);
1522 test1 = (cmd_status == 0);
1523
1524 if (!test0 && test1)
1525 dep->combo_num = 1;
1526 else if (!test0 && !test1)
1527 dep->combo_num = 2;
1528 else if (test0 && !test1)
1529 dep->combo_num = 3;
1530 else if (test0 && test1)
1531 dep->combo_num = 0;
1532
Michael Grzeschikca143782020-07-01 20:24:51 +02001533 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001534 dep->frame_number |= dep->combo_num << 14;
1535 dep->frame_number += max_t(u32, 4, dep->interval);
1536
1537 /* Reinitialize test variables */
1538 dep->start_cmd_status = 0;
1539 dep->combo_num = 0;
1540
Felipe Balbi25abad62018-08-14 10:41:19 +03001541 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001542}
1543
Felipe Balbi25abad62018-08-14 10:41:19 +03001544static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301545{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001546 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001547 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001548 int ret;
1549 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001550
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001551 if (list_empty(&dep->pending_list) &&
1552 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301553 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001554 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301555 }
1556
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001557 if (!dwc->dis_start_transfer_quirk &&
1558 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1559 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001560 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001561 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001562 }
1563
Michael Olbrichc5a70922020-07-01 20:24:52 +02001564 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001565 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001566 u32 frame = __dwc3_gadget_get_frame(dwc);
1567 bool rollover = frame <
1568 (dep->frame_number & DWC3_FRNUMBER_MASK);
1569
1570 /*
1571 * frame_number is set from XferNotReady and may be already
1572 * out of date. DSTS only provides the lower 14 bit of the
1573 * current frame number. So add the upper two bits of
1574 * frame_number and handle a possible rollover.
1575 * This will provide the correct frame_number unless more than
1576 * rollover has happened since XferNotReady.
1577 */
1578
1579 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1580 frame;
1581 if (rollover)
1582 dep->frame_number += BIT(14);
1583 }
1584
Felipe Balbid5370102018-08-14 10:42:43 +03001585 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1586 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1587
1588 ret = __dwc3_gadget_kick_transfer(dep);
1589 if (ret != -EAGAIN)
1590 break;
1591 }
1592
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001593 /*
1594 * After a number of unsuccessful start attempts due to bus-expiry
1595 * status, issue END_TRANSFER command and retry on the next XferNotReady
1596 * event.
1597 */
1598 if (ret == -EAGAIN) {
1599 struct dwc3_gadget_ep_cmd_params params;
1600 u32 cmd;
1601
1602 cmd = DWC3_DEPCMD_ENDTRANSFER |
1603 DWC3_DEPCMD_CMDIOC |
1604 DWC3_DEPCMD_PARAM(dep->resource_index);
1605
1606 dep->resource_index = 0;
1607 memset(&params, 0, sizeof(params));
1608
1609 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1610 if (!ret)
1611 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1612 }
1613
Felipe Balbid5370102018-08-14 10:42:43 +03001614 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301615}
1616
Felipe Balbi72246da2011-08-19 18:10:58 +03001617static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1618{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001619 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001620
Wesley Chengf09ddcf2021-03-11 15:59:02 -08001621 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001622 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1623 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001624 return -ESHUTDOWN;
1625 }
1626
Felipe Balbi04fb3652017-05-17 15:57:45 +03001627 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1628 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001629 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001630
Felipe Balbib2b6d602019-01-11 12:58:52 +02001631 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1632 "%s: request %pK already in flight\n",
1633 dep->name, &req->request))
1634 return -EINVAL;
1635
Felipe Balbifc8bb912016-05-16 13:14:48 +03001636 pm_runtime_get(dwc->dev);
1637
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 req->request.actual = 0;
1639 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001640
Felipe Balbife84f522015-09-01 09:01:38 -05001641 trace_dwc3_ep_queue(req);
1642
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001643 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001644 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001645
Thinh Nguyene0d19562020-05-05 19:46:57 -07001646 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1647 return 0;
1648
Thinh Nguyenc5036722020-09-02 18:42:58 -07001649 /*
1650 * Start the transfer only after the END_TRANSFER is completed
1651 * and endpoint STALL is cleared.
1652 */
1653 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1654 (dep->flags & DWC3_EP_WEDGE) ||
1655 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001656 dep->flags |= DWC3_EP_DELAY_START;
1657 return 0;
1658 }
1659
Felipe Balbid889c232016-09-29 15:44:29 +03001660 /*
1661 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1662 * wait for a XferNotReady event so we will know what's the current
1663 * (micro-)frame number.
1664 *
1665 * Without this trick, we are very, very likely gonna get Bus Expiry
1666 * errors which will force us issue EndTransfer command.
1667 */
1668 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001669 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1670 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001671 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001672
1673 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001674 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001675 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001676 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001677 }
1678
Felipe Balbi7fdca762017-09-05 14:41:34 +03001679 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001680}
1681
1682static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1683 gfp_t gfp_flags)
1684{
1685 struct dwc3_request *req = to_dwc3_request(request);
1686 struct dwc3_ep *dep = to_dwc3_ep(ep);
1687 struct dwc3 *dwc = dep->dwc;
1688
1689 unsigned long flags;
1690
1691 int ret;
1692
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001693 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 ret = __dwc3_gadget_ep_queue(dep, req);
1695 spin_unlock_irqrestore(&dwc->lock, flags);
1696
1697 return ret;
1698}
1699
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001700static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1701{
1702 int i;
1703
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001704 /* If req->trb is not set, then the request has not started */
1705 if (!req->trb)
1706 return;
1707
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001708 /*
1709 * If request was already started, this means we had to
1710 * stop the transfer. With that we also need to ignore
1711 * all TRBs used by the request, however TRBs can only
1712 * be modified after completion of END_TRANSFER
1713 * command. So what we do here is that we wait for
1714 * END_TRANSFER completion and only after that, we jump
1715 * over TRBs by clearing HWO and incrementing dequeue
1716 * pointer.
1717 */
1718 for (i = 0; i < req->num_trbs; i++) {
1719 struct dwc3_trb *trb;
1720
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001721 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001722 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1723 dwc3_ep_inc_deq(dep);
1724 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001725
1726 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001727}
1728
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001729static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1730{
1731 struct dwc3_request *req;
1732 struct dwc3_request *tmp;
Ray Chi04dd6e72021-03-28 02:17:42 +08001733 struct dwc3 *dwc = dep->dwc;
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001734
1735 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1736 dwc3_gadget_ep_skip_trbs(dep, req);
Ray Chi04dd6e72021-03-28 02:17:42 +08001737 switch (req->status) {
1738 case DWC3_REQUEST_STATUS_DISCONNECTED:
1739 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1740 break;
1741 case DWC3_REQUEST_STATUS_DEQUEUED:
1742 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1743 break;
1744 case DWC3_REQUEST_STATUS_STALLED:
1745 dwc3_gadget_giveback(dep, req, -EPIPE);
1746 break;
1747 default:
1748 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
1749 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1750 break;
1751 }
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001752 }
1753}
1754
Felipe Balbi72246da2011-08-19 18:10:58 +03001755static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1756 struct usb_request *request)
1757{
1758 struct dwc3_request *req = to_dwc3_request(request);
1759 struct dwc3_request *r = NULL;
1760
1761 struct dwc3_ep *dep = to_dwc3_ep(ep);
1762 struct dwc3 *dwc = dep->dwc;
1763
1764 unsigned long flags;
1765 int ret = 0;
1766
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001767 trace_dwc3_ep_dequeue(req);
1768
Felipe Balbi72246da2011-08-19 18:10:58 +03001769 spin_lock_irqsave(&dwc->lock, flags);
1770
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001771 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001773 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001774 }
1775
Felipe Balbi72246da2011-08-19 18:10:58 +03001776 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001777 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001778 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1779 goto out;
1780 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001781 }
1782
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001783 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001785 struct dwc3_request *t;
1786
Felipe Balbi72246da2011-08-19 18:10:58 +03001787 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001788 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001789
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001790 /*
1791 * Remove any started request if the transfer is
1792 * cancelled.
1793 */
1794 list_for_each_entry_safe(r, t, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001795 dwc3_gadget_move_cancelled_request(r,
1796 DWC3_REQUEST_STATUS_DEQUEUED);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001797
Thinh Nguyena5c76822021-01-04 22:42:39 -08001798 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1799
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001800 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001801 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 }
1803
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001804 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1805 request, ep->name);
1806 ret = -EINVAL;
1807out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001808 spin_unlock_irqrestore(&dwc->lock, flags);
1809
1810 return ret;
1811}
1812
Felipe Balbi7a608552014-09-24 14:19:52 -05001813int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001814{
1815 struct dwc3_gadget_ep_cmd_params params;
1816 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001817 struct dwc3_request *req;
1818 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001819 int ret;
1820
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001821 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1822 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1823 return -EINVAL;
1824 }
1825
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 memset(&params, 0x00, sizeof(params));
1827
1828 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001829 struct dwc3_trb *trb;
1830
Felipe Balbie319bd62020-08-13 08:35:38 +03001831 unsigned int transfer_in_flight;
1832 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001833
1834 if (dep->number > 1)
1835 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1836 else
1837 trb = &dwc->ep0_trb[dep->trb_enqueue];
1838
1839 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1840 started = !list_empty(&dep->started_list);
1841
1842 if (!protocol && ((dep->direction && transfer_in_flight) ||
1843 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001844 return -EAGAIN;
1845 }
1846
Felipe Balbi2cd47182016-04-12 16:42:43 +03001847 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1848 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001849 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001850 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001851 dep->name);
1852 else
1853 dep->flags |= DWC3_EP_STALL;
1854 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001855 /*
1856 * Don't issue CLEAR_STALL command to control endpoints. The
1857 * controller automatically clears the STALL when it receives
1858 * the SETUP token.
1859 */
1860 if (dep->number <= 1) {
1861 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1862 return 0;
1863 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001864
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001865 dwc3_stop_active_transfer(dep, true, true);
1866
1867 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001868 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001869
1870 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1871 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1872 return 0;
1873 }
1874
1875 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1876
John Youn50c763f2016-05-31 17:49:56 -07001877 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001878 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001879 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001880 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001881 return ret;
1882 }
1883
1884 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1885
Thinh Nguyenc5036722020-09-02 18:42:58 -07001886 if ((dep->flags & DWC3_EP_DELAY_START) &&
1887 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1888 __dwc3_gadget_kick_transfer(dep);
1889
1890 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001891 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001892
Felipe Balbi72246da2011-08-19 18:10:58 +03001893 return ret;
1894}
1895
1896static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1897{
1898 struct dwc3_ep *dep = to_dwc3_ep(ep);
1899 struct dwc3 *dwc = dep->dwc;
1900
1901 unsigned long flags;
1902
1903 int ret;
1904
1905 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001906 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001907 spin_unlock_irqrestore(&dwc->lock, flags);
1908
1909 return ret;
1910}
1911
1912static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1913{
1914 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001915 struct dwc3 *dwc = dep->dwc;
1916 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001917 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001918
Paul Zimmerman249a4562012-02-24 17:32:16 -08001919 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 dep->flags |= DWC3_EP_WEDGE;
1921
Pratyush Anand08f0d962012-06-25 22:40:43 +05301922 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001923 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301924 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001925 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001926 spin_unlock_irqrestore(&dwc->lock, flags);
1927
1928 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001929}
1930
1931/* -------------------------------------------------------------------------- */
1932
1933static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1934 .bLength = USB_DT_ENDPOINT_SIZE,
1935 .bDescriptorType = USB_DT_ENDPOINT,
1936 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1937};
1938
1939static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1940 .enable = dwc3_gadget_ep0_enable,
1941 .disable = dwc3_gadget_ep0_disable,
1942 .alloc_request = dwc3_gadget_ep_alloc_request,
1943 .free_request = dwc3_gadget_ep_free_request,
1944 .queue = dwc3_gadget_ep0_queue,
1945 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301946 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001947 .set_wedge = dwc3_gadget_ep_set_wedge,
1948};
1949
1950static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1951 .enable = dwc3_gadget_ep_enable,
1952 .disable = dwc3_gadget_ep_disable,
1953 .alloc_request = dwc3_gadget_ep_alloc_request,
1954 .free_request = dwc3_gadget_ep_free_request,
1955 .queue = dwc3_gadget_ep_queue,
1956 .dequeue = dwc3_gadget_ep_dequeue,
1957 .set_halt = dwc3_gadget_ep_set_halt,
1958 .set_wedge = dwc3_gadget_ep_set_wedge,
1959};
1960
1961/* -------------------------------------------------------------------------- */
1962
1963static int dwc3_gadget_get_frame(struct usb_gadget *g)
1964{
1965 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001966
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001967 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001968}
1969
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001970static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001971{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001972 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001973
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001974 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001975 u32 reg;
1976
Felipe Balbi72246da2011-08-19 18:10:58 +03001977 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001978
Felipe Balbi72246da2011-08-19 18:10:58 +03001979 /*
1980 * According to the Databook Remote wakeup request should
1981 * be issued only when the device is in early suspend state.
1982 *
1983 * We can check that via USB Link State bits in DSTS register.
1984 */
1985 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1986
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 link_state = DWC3_DSTS_USBLNKST(reg);
1988
1989 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001990 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001991 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1992 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyenc560e762021-04-19 19:11:12 -07001993 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
1994 case DWC3_LINK_STATE_U1:
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001995 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001996 break;
1997 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001998 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001999 }
2000
Felipe Balbi8598bde2012-01-02 18:55:57 +02002001 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2002 if (ret < 0) {
2003 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002004 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02002005 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002006
Paul Zimmerman802fde92012-04-27 13:10:52 +03002007 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002008 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002009 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03002010 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002011 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2012 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2013 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002014
Paul Zimmerman1d046792012-02-15 18:56:56 -08002015 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002016 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03002017
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002018 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002019 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2020
2021 /* in HS, means ON */
2022 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2023 break;
2024 }
2025
2026 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2027 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002028 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002029 }
2030
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002031 return 0;
2032}
2033
2034static int dwc3_gadget_wakeup(struct usb_gadget *g)
2035{
2036 struct dwc3 *dwc = gadget_to_dwc(g);
2037 unsigned long flags;
2038 int ret;
2039
2040 spin_lock_irqsave(&dwc->lock, flags);
2041 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002042 spin_unlock_irqrestore(&dwc->lock, flags);
2043
2044 return ret;
2045}
2046
2047static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2048 int is_selfpowered)
2049{
2050 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002051 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002052
Paul Zimmerman249a4562012-02-24 17:32:16 -08002053 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002054 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002055 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002056
2057 return 0;
2058}
2059
Wesley Chengae7e8612020-09-28 17:20:59 -07002060static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2061{
2062 u32 epnum;
2063
2064 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2065 struct dwc3_ep *dep;
2066
2067 dep = dwc->eps[epnum];
2068 if (!dep)
2069 continue;
2070
2071 dwc3_remove_requests(dwc, dep);
2072 }
2073}
2074
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002075static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2076{
2077 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2078 u32 reg;
2079
2080 if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2081 ssp_rate = dwc->max_ssp_rate;
2082
2083 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2084 reg &= ~DWC3_DCFG_SPEED_MASK;
2085 reg &= ~DWC3_DCFG_NUMLANES(~0);
2086
2087 if (ssp_rate == USB_SSP_GEN_1x2)
2088 reg |= DWC3_DCFG_SUPERSPEED;
2089 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2090 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2091
2092 if (ssp_rate != USB_SSP_GEN_2x1 &&
2093 dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2094 reg |= DWC3_DCFG_NUMLANES(1);
2095
2096 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2097}
2098
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002099static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2100{
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002101 enum usb_device_speed speed;
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002102 u32 reg;
2103
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002104 speed = dwc->gadget_max_speed;
Thinh Nguyen93f1d432021-03-08 18:16:50 -08002105 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002106 speed = dwc->maximum_speed;
2107
2108 if (speed == USB_SPEED_SUPER_PLUS &&
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002109 DWC3_IP_IS(DWC32)) {
2110 __dwc3_gadget_set_ssp_rate(dwc);
2111 return;
2112 }
2113
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002114 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2115 reg &= ~(DWC3_DCFG_SPEED_MASK);
2116
2117 /*
2118 * WORKAROUND: DWC3 revision < 2.20a have an issue
2119 * which would cause metastability state on Run/Stop
2120 * bit if we try to force the IP to USB2-only mode.
2121 *
2122 * Because of that, we cannot configure the IP to any
2123 * speed other than the SuperSpeed
2124 *
2125 * Refers to:
2126 *
2127 * STAR#9000525659: Clock Domain Crossing on DCTL in
2128 * USB 2.0 Mode
2129 */
2130 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2131 !dwc->dis_metastability_quirk) {
2132 reg |= DWC3_DCFG_SUPERSPEED;
2133 } else {
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002134 switch (speed) {
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002135 case USB_SPEED_FULL:
2136 reg |= DWC3_DCFG_FULLSPEED;
2137 break;
2138 case USB_SPEED_HIGH:
2139 reg |= DWC3_DCFG_HIGHSPEED;
2140 break;
2141 case USB_SPEED_SUPER:
2142 reg |= DWC3_DCFG_SUPERSPEED;
2143 break;
2144 case USB_SPEED_SUPER_PLUS:
2145 if (DWC3_IP_IS(DWC3))
2146 reg |= DWC3_DCFG_SUPERSPEED;
2147 else
2148 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2149 break;
2150 default:
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002151 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002152
2153 if (DWC3_IP_IS(DWC3))
2154 reg |= DWC3_DCFG_SUPERSPEED;
2155 else
2156 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2157 }
2158 }
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002159
2160 if (DWC3_IP_IS(DWC32) &&
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002161 speed > USB_SPEED_UNKNOWN &&
2162 speed < USB_SPEED_SUPER_PLUS)
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002163 reg &= ~DWC3_DCFG_NUMLANES(~0);
2164
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002165 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2166}
2167
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002168static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002169{
2170 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002171 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002172
Felipe Balbifc8bb912016-05-16 13:14:48 +03002173 if (pm_runtime_suspended(dwc->dev))
2174 return 0;
2175
Felipe Balbi72246da2011-08-19 18:10:58 +03002176 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002177 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002178 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002179 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2180 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2181 }
2182
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002183 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002184 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2185 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002186
2187 if (dwc->has_hibernation)
2188 reg |= DWC3_DCTL_KEEP_CONNECT;
2189
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002190 __dwc3_gadget_set_speed(dwc);
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002191 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002192 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002194
2195 if (dwc->has_hibernation && !suspend)
2196 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2197
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002198 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002199 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002200
Thinh Nguyen5b738212019-10-23 19:15:43 -07002201 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002202
2203 do {
2204 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002205 reg &= DWC3_DSTS_DEVCTRLHLT;
2206 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002207
2208 if (!timeout)
2209 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002210
Pratyush Anand6f17f742012-07-02 10:21:55 +05302211 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002212}
2213
Wesley Chengae7e8612020-09-28 17:20:59 -07002214static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2215static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chenga1383b32020-12-29 15:00:37 -08002216static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002217
Felipe Balbi72246da2011-08-19 18:10:58 +03002218static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2219{
2220 struct dwc3 *dwc = gadget_to_dwc(g);
2221 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302222 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002223
2224 is_on = !!is_on;
2225
Baolin Wangbb014732016-10-14 17:11:33 +08002226 /*
2227 * Per databook, when we want to stop the gadget, if a control transfer
2228 * is still in process, complete it and get the core into setup phase.
2229 */
2230 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2231 reinit_completion(&dwc->ep0_in_setup);
2232
2233 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2234 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2235 if (ret == 0) {
2236 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2237 return -ETIMEDOUT;
2238 }
2239 }
2240
Wesley Chengae7e8612020-09-28 17:20:59 -07002241 /*
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002242 * Check the return value for successful resume, or error. For a
2243 * successful resume, the DWC3 runtime PM resume routine will handle
2244 * the run stop sequence, so avoid duplicate operations here.
2245 */
2246 ret = pm_runtime_get_sync(dwc->dev);
2247 if (!ret || ret < 0) {
2248 pm_runtime_put(dwc->dev);
2249 return 0;
2250 }
2251
2252 /*
Wesley Chengae7e8612020-09-28 17:20:59 -07002253 * Synchronize any pending event handling before executing the controller
2254 * halt routine.
2255 */
2256 if (!is_on) {
2257 dwc3_gadget_disable_irq(dwc);
2258 synchronize_irq(dwc->irq_gadget);
2259 }
2260
Felipe Balbi72246da2011-08-19 18:10:58 +03002261 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002262
2263 if (!is_on) {
2264 u32 count;
2265
Wesley Chengf09ddcf2021-03-11 15:59:02 -08002266 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002267 /*
2268 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2269 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2270 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2271 * command for any active transfers" before clearing the RunStop
2272 * bit.
2273 */
2274 dwc3_stop_active_transfers(dwc);
2275 __dwc3_gadget_stop(dwc);
2276
2277 /*
2278 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2279 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2280 * "software needs to acknowledge the events that are generated
2281 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2282 * to be set to '1'."
2283 */
2284 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2285 count &= DWC3_GEVNTCOUNT_MASK;
2286 if (count > 0) {
2287 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2288 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2289 dwc->ev_buf->length;
2290 }
Wesley Chenga1383b32020-12-29 15:00:37 -08002291 } else {
2292 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002293 }
2294
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002295 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002296 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002297 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002298
Pratyush Anand6f17f742012-07-02 10:21:55 +05302299 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002300}
2301
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002302static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2303{
2304 u32 reg;
2305
2306 /* Enable all but Start and End of Frame IRQs */
Thinh Nguyen132ee0d2021-01-13 19:55:29 -08002307 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002308 DWC3_DEVTEN_CMDCMPLTEN |
2309 DWC3_DEVTEN_ERRTICERREN |
2310 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002311 DWC3_DEVTEN_CONNECTDONEEN |
2312 DWC3_DEVTEN_USBRSTEN |
2313 DWC3_DEVTEN_DISCONNEVTEN);
2314
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002315 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002316 reg |= DWC3_DEVTEN_ULSTCNGEN;
2317
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002318 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2319}
2320
2321static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2322{
2323 /* mask all interrupts */
2324 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2325}
2326
2327static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002328static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002329
Felipe Balbi4e994722016-05-13 14:09:59 +03002330/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002331 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2332 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002333 *
2334 * The following looks like complex but it's actually very simple. In order to
2335 * calculate the number of packets we can burst at once on OUT transfers, we're
2336 * gonna use RxFIFO size.
2337 *
2338 * To calculate RxFIFO size we need two numbers:
2339 * MDWIDTH = size, in bits, of the internal memory bus
2340 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2341 *
2342 * Given these two numbers, the formula is simple:
2343 *
2344 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2345 *
2346 * 24 bytes is for 3x SETUP packets
2347 * 16 bytes is a clock domain crossing tolerance
2348 *
2349 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2350 */
2351static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2352{
2353 u32 ram2_depth;
2354 u32 mdwidth;
2355 u32 nump;
2356 u32 reg;
2357
2358 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
Thinh Nguyend00be772021-03-27 17:54:01 -07002359 mdwidth = dwc3_mdwidth(dwc);
Felipe Balbi4e994722016-05-13 14:09:59 +03002360
2361 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2362 nump = min_t(u32, nump, 16);
2363
2364 /* update NumP */
2365 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2366 reg &= ~DWC3_DCFG_NUMP_MASK;
2367 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2368 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2369}
2370
Felipe Balbid7be2952016-05-04 15:49:37 +03002371static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002372{
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002374 int ret = 0;
2375 u32 reg;
2376
John Youncf40b862016-11-14 12:32:43 -08002377 /*
2378 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2379 * the core supports IMOD, disable it.
2380 */
2381 if (dwc->imod_interval) {
2382 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2383 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2384 } else if (dwc3_has_imod(dwc)) {
2385 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2386 }
2387
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002388 /*
2389 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2390 * field instead of letting dwc3 itself calculate that automatically.
2391 *
2392 * This way, we maximize the chances that we'll be able to get several
2393 * bursts of data without going through any sort of endpoint throttling.
2394 */
2395 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002396 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002397 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002398 else
2399 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002400
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002401 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2402
Felipe Balbi4e994722016-05-13 14:09:59 +03002403 dwc3_gadget_setup_nump(dwc);
2404
Thinh Nguyene66bbfb2021-04-12 20:00:45 -07002405 /*
2406 * Currently the controller handles single stream only. So, Ignore
2407 * Packet Pending bit for stream selection and don't search for another
2408 * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2409 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2410 * the stream performance.
2411 */
2412 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2413 reg |= DWC3_DCFG_IGNSTRMPP;
2414 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2415
Felipe Balbi72246da2011-08-19 18:10:58 +03002416 /* Start with SuperSpeed Default */
2417 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2418
2419 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002420 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002421 if (ret) {
2422 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002423 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002424 }
2425
2426 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002427 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002428 if (ret) {
2429 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002430 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002431 }
2432
2433 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002434 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002435 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002436 dwc3_ep0_out_start(dwc);
2437
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002438 dwc3_gadget_enable_irq(dwc);
2439
Felipe Balbid7be2952016-05-04 15:49:37 +03002440 return 0;
2441
2442err1:
2443 __dwc3_gadget_ep_disable(dwc->eps[0]);
2444
2445err0:
2446 return ret;
2447}
2448
2449static int dwc3_gadget_start(struct usb_gadget *g,
2450 struct usb_gadget_driver *driver)
2451{
2452 struct dwc3 *dwc = gadget_to_dwc(g);
2453 unsigned long flags;
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002454 int ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002455 int irq;
2456
Roger Quadros9522def2016-06-10 14:48:38 +03002457 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002458 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2459 IRQF_SHARED, "dwc3", dwc->ev_buf);
2460 if (ret) {
2461 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2462 irq, ret);
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002463 return ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002464 }
2465
2466 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002467 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002468 spin_unlock_irqrestore(&dwc->lock, flags);
2469
2470 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002471}
2472
Felipe Balbid7be2952016-05-04 15:49:37 +03002473static void __dwc3_gadget_stop(struct dwc3 *dwc)
2474{
2475 dwc3_gadget_disable_irq(dwc);
2476 __dwc3_gadget_ep_disable(dwc->eps[0]);
2477 __dwc3_gadget_ep_disable(dwc->eps[1]);
2478}
2479
Felipe Balbi22835b82014-10-17 12:05:12 -05002480static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002481{
2482 struct dwc3 *dwc = gadget_to_dwc(g);
2483 unsigned long flags;
2484
2485 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002486 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 spin_unlock_irqrestore(&dwc->lock, flags);
2488
Felipe Balbi3f308d12016-05-16 14:17:06 +03002489 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002490
Felipe Balbi72246da2011-08-19 18:10:58 +03002491 return 0;
2492}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002493
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302494static void dwc3_gadget_config_params(struct usb_gadget *g,
2495 struct usb_dcd_config_params *params)
2496{
2497 struct dwc3 *dwc = gadget_to_dwc(g);
2498
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002499 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2500 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2501
2502 /* Recommended BESL */
2503 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002504 /*
2505 * If the recommended BESL baseline is 0 or if the BESL deep is
2506 * less than 2, Microsoft's Windows 10 host usb stack will issue
2507 * a usb reset immediately after it receives the extended BOS
2508 * descriptor and the enumeration will fail. To maintain
2509 * compatibility with the Windows' usb stack, let's set the
2510 * recommended BESL baseline to 1 and clamp the BESL deep to be
2511 * within 2 to 15.
2512 */
2513 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002514 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002515 params->besl_deep =
2516 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002517 }
2518
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302519 /* U1 Device exit Latency */
2520 if (dwc->dis_u1_entry_quirk)
2521 params->bU1devExitLat = 0;
2522 else
2523 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2524
2525 /* U2 Device exit Latency */
2526 if (dwc->dis_u2_entry_quirk)
2527 params->bU2DevExitLat = 0;
2528 else
2529 params->bU2DevExitLat =
2530 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2531}
2532
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002533static void dwc3_gadget_set_speed(struct usb_gadget *g,
2534 enum usb_device_speed speed)
2535{
2536 struct dwc3 *dwc = gadget_to_dwc(g);
2537 unsigned long flags;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002538
2539 spin_lock_irqsave(&dwc->lock, flags);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002540 dwc->gadget_max_speed = speed;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002541 spin_unlock_irqrestore(&dwc->lock, flags);
2542}
2543
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002544static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
2545 enum usb_ssp_rate rate)
2546{
2547 struct dwc3 *dwc = gadget_to_dwc(g);
2548 unsigned long flags;
2549
2550 spin_lock_irqsave(&dwc->lock, flags);
Thinh Nguyencdb651b2021-03-08 18:16:44 -08002551 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002552 dwc->gadget_ssp_rate = rate;
2553 spin_unlock_irqrestore(&dwc->lock, flags);
2554}
2555
Wesley Cheng82c46b82020-12-29 15:03:29 -08002556static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2557{
2558 struct dwc3 *dwc = gadget_to_dwc(g);
Ray Chi99288de2021-02-22 19:51:49 +08002559 union power_supply_propval val = {0};
2560 int ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002561
2562 if (dwc->usb2_phy)
2563 return usb_phy_set_power(dwc->usb2_phy, mA);
2564
Ray Chi99288de2021-02-22 19:51:49 +08002565 if (!dwc->usb_psy)
2566 return -EOPNOTSUPP;
2567
Ray Chi8a5b5c32021-03-28 02:28:08 +08002568 val.intval = 1000 * mA;
Ray Chi99288de2021-02-22 19:51:49 +08002569 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
2570
2571 return ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002572}
2573
Felipe Balbi72246da2011-08-19 18:10:58 +03002574static const struct usb_gadget_ops dwc3_gadget_ops = {
2575 .get_frame = dwc3_gadget_get_frame,
2576 .wakeup = dwc3_gadget_wakeup,
2577 .set_selfpowered = dwc3_gadget_set_selfpowered,
2578 .pullup = dwc3_gadget_pullup,
2579 .udc_start = dwc3_gadget_start,
2580 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002581 .udc_set_speed = dwc3_gadget_set_speed,
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002582 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302583 .get_config_params = dwc3_gadget_config_params,
Wesley Cheng82c46b82020-12-29 15:03:29 -08002584 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002585};
2586
2587/* -------------------------------------------------------------------------- */
2588
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002589static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2590{
2591 struct dwc3 *dwc = dep->dwc;
2592
2593 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2594 dep->endpoint.maxburst = 1;
2595 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2596 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002597 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002598
2599 dep->endpoint.caps.type_control = true;
2600
2601 return 0;
2602}
2603
2604static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2605{
2606 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002607 u32 mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002608 int size;
2609
Thinh Nguyend00be772021-03-27 17:54:01 -07002610 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002611
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002612 /* MDWIDTH is represented in bits, we need it in bytes */
2613 mdwidth /= 8;
2614
2615 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002616 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002617 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002618 else
2619 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002620
2621 /* FIFO Depth is in MDWDITH bytes. Multiply */
2622 size *= mdwidth;
2623
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002624 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002625 * To meet performance requirement, a minimum TxFIFO size of 3x
2626 * MaxPacketSize is recommended for endpoints that support burst and a
2627 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2628 * support burst. Use those numbers and we can calculate the max packet
2629 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002630 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002631 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2632 size /= 3;
2633 else
2634 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002635
2636 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2637
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002638 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002639 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2640 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002641 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002642 dep->endpoint.caps.type_iso = true;
2643 dep->endpoint.caps.type_bulk = true;
2644 dep->endpoint.caps.type_int = true;
2645
2646 return dwc3_alloc_trb_pool(dep);
2647}
2648
2649static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2650{
2651 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002652 u32 mdwidth;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002653 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002654
Thinh Nguyend00be772021-03-27 17:54:01 -07002655 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002656
2657 /* MDWIDTH is represented in bits, convert to bytes */
2658 mdwidth /= 8;
2659
2660 /* All OUT endpoints share a single RxFIFO space */
2661 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002662 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002663 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002664 else
2665 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002666
2667 /* FIFO depth is in MDWDITH bytes */
2668 size *= mdwidth;
2669
2670 /*
2671 * To meet performance requirement, a minimum recommended RxFIFO size
2672 * is defined as follow:
2673 * RxFIFO size >= (3 x MaxPacketSize) +
2674 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2675 *
2676 * Then calculate the max packet limit as below.
2677 */
2678 size -= (3 * 8) + 16;
2679 if (size < 0)
2680 size = 0;
2681 else
2682 size /= 3;
2683
2684 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002685 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002686 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2687 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002688 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002689 dep->endpoint.caps.type_iso = true;
2690 dep->endpoint.caps.type_bulk = true;
2691 dep->endpoint.caps.type_int = true;
2692
2693 return dwc3_alloc_trb_pool(dep);
2694}
2695
2696static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002697{
2698 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002699 bool direction = epnum & 1;
2700 int ret;
2701 u8 num = epnum >> 1;
2702
2703 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2704 if (!dep)
2705 return -ENOMEM;
2706
2707 dep->dwc = dwc;
2708 dep->number = epnum;
2709 dep->direction = direction;
2710 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2711 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002712 dep->combo_num = 0;
2713 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002714
2715 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2716 direction ? "in" : "out");
2717
2718 dep->endpoint.name = dep->name;
2719
2720 if (!(dep->number > 1)) {
2721 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2722 dep->endpoint.comp_desc = NULL;
2723 }
2724
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002725 if (num == 0)
2726 ret = dwc3_gadget_init_control_endpoint(dep);
2727 else if (direction)
2728 ret = dwc3_gadget_init_in_endpoint(dep);
2729 else
2730 ret = dwc3_gadget_init_out_endpoint(dep);
2731
2732 if (ret)
2733 return ret;
2734
2735 dep->endpoint.caps.dir_in = direction;
2736 dep->endpoint.caps.dir_out = !direction;
2737
2738 INIT_LIST_HEAD(&dep->pending_list);
2739 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002740 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002741
2742 return 0;
2743}
2744
2745static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2746{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002747 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002748
Peter Chene81a7012020-08-21 10:55:48 +08002749 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002750
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002751 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002752 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002753
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002754 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2755 if (ret)
2756 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 }
2758
2759 return 0;
2760}
2761
2762static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2763{
2764 struct dwc3_ep *dep;
2765 u8 epnum;
2766
2767 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2768 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002769 if (!dep)
2770 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302771 /*
2772 * Physical endpoints 0 and 1 are special; they form the
2773 * bi-directional USB endpoint 0.
2774 *
2775 * For those two physical endpoints, we don't allocate a TRB
2776 * pool nor do we add them the endpoints list. Due to that, we
2777 * shouldn't do these two operations otherwise we would end up
2778 * with all sorts of bugs when removing dwc3.ko.
2779 */
2780 if (epnum != 0 && epnum != 1) {
2781 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302783 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002784
2785 kfree(dep);
2786 }
2787}
2788
Felipe Balbi72246da2011-08-19 18:10:58 +03002789/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002790
Felipe Balbi8f608e82018-03-27 10:53:29 +03002791static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2792 struct dwc3_request *req, struct dwc3_trb *trb,
2793 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302794{
2795 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302796
Felipe Balbidc55c672016-08-12 13:20:32 +03002797 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002798
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002799 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002800 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002801
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002802 /*
2803 * If we're in the middle of series of chained TRBs and we
2804 * receive a short transfer along the way, DWC3 will skip
2805 * through all TRBs including the last TRB in the chain (the
2806 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2807 * bit and SW has to do it manually.
2808 *
2809 * We're going to do that here to avoid problems of HW trying
2810 * to use bogus TRBs for transfers.
2811 */
2812 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2813 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2814
Felipe Balbic6267a52017-01-05 14:58:46 +02002815 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002816 * For isochronous transfers, the first TRB in a service interval must
2817 * have the Isoc-First type. Track and report its interval frame number.
2818 */
2819 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2820 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2821 unsigned int frame_number;
2822
2823 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2824 frame_number &= ~(dep->interval - 1);
2825 req->request.frame_number = frame_number;
2826 }
2827
2828 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002829 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2830 * this TRB points to the bounce buffer address, it's a MPS alignment
2831 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002832 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002833 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2834 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002835 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2836 return 1;
2837 }
2838
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302839 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002840 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302841
Felipe Balbi35b27192017-03-08 13:56:37 +02002842 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2843 return 1;
2844
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002845 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302846 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002847
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002848 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2849 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302850 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002851
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302852 return 0;
2853}
2854
Felipe Balbid3692952018-03-29 13:32:10 +03002855static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2856 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2857 int status)
2858{
2859 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2860 struct scatterlist *sg = req->sg;
2861 struct scatterlist *s;
2862 unsigned int pending = req->num_pending_sgs;
2863 unsigned int i;
2864 int ret = 0;
2865
2866 for_each_sg(sg, s, pending, i) {
2867 trb = &dep->trb_pool[dep->trb_dequeue];
2868
Felipe Balbid3692952018-03-29 13:32:10 +03002869 req->sg = sg_next(s);
2870 req->num_pending_sgs--;
2871
2872 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2873 trb, event, status, true);
2874 if (ret)
2875 break;
2876 }
2877
2878 return ret;
2879}
2880
2881static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2882 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2883 int status)
2884{
2885 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2886
2887 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2888 event, status, false);
2889}
2890
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002891static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2892{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002893 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002894}
2895
Felipe Balbif38e35d2018-04-06 15:56:35 +03002896static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2897 const struct dwc3_event_depevt *event,
2898 struct dwc3_request *req, int status)
2899{
2900 int ret;
2901
2902 if (req->num_pending_sgs)
2903 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2904 status);
2905 else
2906 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2907 status);
2908
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002909 req->request.actual = req->request.length - req->remaining;
2910
2911 if (!dwc3_gadget_ep_request_completed(req))
2912 goto out;
2913
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002914 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002915 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2916 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002917 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002918 }
2919
Felipe Balbif38e35d2018-04-06 15:56:35 +03002920 dwc3_gadget_giveback(dep, req, status);
2921
2922out:
2923 return ret;
2924}
2925
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002926static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002927 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002928{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002929 struct dwc3_request *req;
2930 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002931
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002932 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002933 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002934
Felipe Balbif38e35d2018-04-06 15:56:35 +03002935 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2936 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002937 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002938 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002939 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002940}
2941
Thinh Nguyend9feef92020-03-31 01:40:42 -07002942static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2943{
2944 struct dwc3_request *req;
Wesley Cheng02fa4b92021-03-19 02:31:24 -07002945 struct dwc3 *dwc = dep->dwc;
2946
2947 if (!dep->endpoint.desc || !dwc->pullups_connected ||
2948 !dwc->connected)
2949 return false;
Thinh Nguyend9feef92020-03-31 01:40:42 -07002950
2951 if (!list_empty(&dep->pending_list))
2952 return true;
2953
2954 /*
2955 * We only need to check the first entry of the started list. We can
2956 * assume the completed requests are removed from the started list.
2957 */
2958 req = next_request(&dep->started_list);
2959 if (!req)
2960 return false;
2961
2962 return !dwc3_gadget_ep_request_completed(req);
2963}
2964
Felipe Balbiee3638b2018-03-27 11:26:53 +03002965static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2966 const struct dwc3_event_depevt *event)
2967{
Felipe Balbif62afb42018-04-11 10:34:34 +03002968 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002969}
2970
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002971static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2972 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002973{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002974 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002975 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002976
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002977 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002978
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002979 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2980 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002981
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002982 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2983 list_empty(&dep->started_list) &&
2984 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002985 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002986 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002987 if (__dwc3_gadget_kick_transfer(dep) == 0)
2988 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002989
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002990out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002991 /*
2992 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2993 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2994 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002995 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002996 u32 reg;
2997 int i;
2998
2999 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05003000 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03003001
3002 if (!(dep->flags & DWC3_EP_ENABLED))
3003 continue;
3004
Felipe Balbiaa3342c2016-03-14 11:01:31 +02003005 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003006 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03003007 }
3008
3009 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3010 reg |= dwc->u1u2;
3011 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3012
3013 dwc->u1u2 = 0;
3014 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003015
3016 return no_started_trb;
3017}
3018
3019static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3020 const struct dwc3_event_depevt *event)
3021{
3022 int status = 0;
3023
3024 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3025 dwc3_gadget_endpoint_frame_from_event(dep, event);
3026
3027 if (event->status & DEPEVT_STATUS_BUSERR)
3028 status = -ECONNRESET;
3029
3030 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3031 status = -EXDEV;
3032
3033 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03003034}
3035
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003036static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3037 const struct dwc3_event_depevt *event)
3038{
3039 int status = 0;
3040
3041 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3042
3043 if (event->status & DEPEVT_STATUS_BUSERR)
3044 status = -ECONNRESET;
3045
Thinh Nguyene0d19562020-05-05 19:46:57 -07003046 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3047 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03003048}
3049
Felipe Balbi8f608e82018-03-27 10:53:29 +03003050static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3051 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03003052{
Felipe Balbiee3638b2018-03-27 11:26:53 +03003053 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07003054
3055 /*
3056 * The XferNotReady event is generated only once before the endpoint
3057 * starts. It will be generated again when END_TRANSFER command is
3058 * issued. For some controller versions, the XferNotReady event may be
3059 * generated while the END_TRANSFER command is still in process. Ignore
3060 * it and wait for the next XferNotReady event after the command is
3061 * completed.
3062 */
3063 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3064 return;
3065
Felipe Balbi25abad62018-08-14 10:41:19 +03003066 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03003067}
3068
Thinh Nguyen8266b082020-07-30 16:29:03 -07003069static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3070 const struct dwc3_event_depevt *event)
3071{
3072 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3073
3074 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3075 return;
3076
3077 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3078 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3079 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3080
3081 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3082 struct dwc3 *dwc = dep->dwc;
3083
3084 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3085 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3086 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3087
3088 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3089 if (dwc->delayed_status)
3090 __dwc3_gadget_ep0_set_halt(ep0, 1);
3091 return;
3092 }
3093
3094 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3095 if (dwc->delayed_status)
3096 dwc3_ep0_send_delayed_status(dwc);
3097 }
3098
3099 if ((dep->flags & DWC3_EP_DELAY_START) &&
3100 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3101 __dwc3_gadget_kick_transfer(dep);
3102
3103 dep->flags &= ~DWC3_EP_DELAY_START;
3104}
3105
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003106static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3107 const struct dwc3_event_depevt *event)
3108{
3109 struct dwc3 *dwc = dep->dwc;
3110
3111 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3112 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3113 goto out;
3114 }
3115
3116 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3117 switch (event->parameters) {
3118 case DEPEVT_STREAM_PRIME:
3119 /*
3120 * If the host can properly transition the endpoint state from
3121 * idle to prime after a NoStream rejection, there's no need to
3122 * force restarting the endpoint to reinitiate the stream. To
3123 * simplify the check, assume the host follows the USB spec if
3124 * it primed the endpoint more than once.
3125 */
3126 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3127 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3128 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3129 else
3130 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3131 }
3132
3133 break;
3134 case DEPEVT_STREAM_NOSTREAM:
3135 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3136 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3137 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3138 break;
3139
3140 /*
3141 * If the host rejects a stream due to no active stream, by the
3142 * USB and xHCI spec, the endpoint will be put back to idle
3143 * state. When the host is ready (buffer added/updated), it will
3144 * prime the endpoint to inform the usb device controller. This
3145 * triggers the device controller to issue ERDY to restart the
3146 * stream. However, some hosts don't follow this and keep the
3147 * endpoint in the idle state. No prime will come despite host
3148 * streams are updated, and the device controller will not be
3149 * triggered to generate ERDY to move the next stream data. To
3150 * workaround this and maintain compatibility with various
3151 * hosts, force to reinitate the stream until the host is ready
3152 * instead of waiting for the host to prime the endpoint.
3153 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003154 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3155 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3156
3157 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3158 } else {
3159 dep->flags |= DWC3_EP_DELAY_START;
3160 dwc3_stop_active_transfer(dep, true, true);
3161 return;
3162 }
3163 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003164 }
3165
3166out:
3167 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3168}
3169
Felipe Balbi72246da2011-08-19 18:10:58 +03003170static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3171 const struct dwc3_event_depevt *event)
3172{
3173 struct dwc3_ep *dep;
3174 u8 epnum = event->endpoint_number;
3175
3176 dep = dwc->eps[epnum];
3177
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003178 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003179 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003180 return;
3181
3182 /* Handle only EPCMDCMPLT when EP disabled */
3183 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3184 return;
3185 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003186
Felipe Balbi72246da2011-08-19 18:10:58 +03003187 if (epnum == 0 || epnum == 1) {
3188 dwc3_ep0_interrupt(dwc, event);
3189 return;
3190 }
3191
3192 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003193 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003194 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003195 break;
3196 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003197 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003198 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003199 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003200 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003201 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003202 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003203 dwc3_gadget_endpoint_transfer_complete(dep, event);
3204 break;
3205 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003206 dwc3_gadget_endpoint_stream_event(dep, event);
3207 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003208 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003209 break;
3210 }
3211}
3212
3213static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3214{
3215 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3216 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003217 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003218 spin_lock(&dwc->lock);
3219 }
3220}
3221
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003222static void dwc3_suspend_gadget(struct dwc3 *dwc)
3223{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003224 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003225 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003226 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003227 spin_lock(&dwc->lock);
3228 }
3229}
3230
3231static void dwc3_resume_gadget(struct dwc3 *dwc)
3232{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003233 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003234 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003235 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003236 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003237 }
3238}
3239
3240static void dwc3_reset_gadget(struct dwc3 *dwc)
3241{
3242 if (!dwc->gadget_driver)
3243 return;
3244
Peter Chene81a7012020-08-21 10:55:48 +08003245 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003246 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003247 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003248 spin_lock(&dwc->lock);
3249 }
3250}
3251
Felipe Balbic5353b22019-02-13 13:00:54 +02003252static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3253 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003254{
Felipe Balbi72246da2011-08-19 18:10:58 +03003255 struct dwc3_gadget_ep_cmd_params params;
3256 u32 cmd;
3257 int ret;
3258
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003259 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3260 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303261 return;
3262
Pratyush Anand57911502012-07-06 15:19:10 +05303263 /*
3264 * NOTICE: We are violating what the Databook says about the
3265 * EndTransfer command. Ideally we would _always_ wait for the
3266 * EndTransfer Command Completion IRQ, but that's causing too
3267 * much trouble synchronizing between us and gadget driver.
3268 *
3269 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003270 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303271 *
3272 * Note also that a similar handling was tested by Synopsys
3273 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003274 * In short, what we're doing is issuing EndTransfer with
3275 * CMDIOC bit set and delay kicking transfer until the
3276 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003277 *
3278 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3279 * supports a mode to work around the above limitation. The
3280 * software can poll the CMDACT bit in the DEPCMD register
3281 * after issuing a EndTransfer command. This mode is enabled
3282 * by writing GUCTL2[14]. This polling is already done in the
3283 * dwc3_send_gadget_ep_cmd() function so if the mode is
3284 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003285 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003286 *
3287 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303288 */
3289
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303290 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003291 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003292 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003293 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303294 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003295 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303296 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003297 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003298
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003299 /*
3300 * The END_TRANSFER command will cause the controller to generate a
3301 * NoStream Event, and it's not due to the host DP NoStream rejection.
3302 * Ignore the next NoStream event.
3303 */
3304 if (dep->stream_capable)
3305 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3306
Thinh Nguyend3abda52019-11-27 13:10:47 -08003307 if (!interrupt)
3308 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003309 else
3310 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003311}
3312
Felipe Balbi72246da2011-08-19 18:10:58 +03003313static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3314{
3315 u32 epnum;
3316
3317 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3318 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003319 int ret;
3320
3321 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003322 if (!dep)
3323 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003324
3325 if (!(dep->flags & DWC3_EP_STALL))
3326 continue;
3327
3328 dep->flags &= ~DWC3_EP_STALL;
3329
John Youn50c763f2016-05-31 17:49:56 -07003330 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003331 WARN_ON_ONCE(ret);
3332 }
3333}
3334
3335static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3336{
Felipe Balbic4430a22012-05-24 10:30:01 +03003337 int reg;
3338
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003339 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3340
Felipe Balbi72246da2011-08-19 18:10:58 +03003341 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3342 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003343 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003344 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003345
Felipe Balbi72246da2011-08-19 18:10:58 +03003346 dwc3_disconnect_gadget(dwc);
3347
Peter Chene81a7012020-08-21 10:55:48 +08003348 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003349 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003350 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003351
3352 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003353}
3354
Felipe Balbi72246da2011-08-19 18:10:58 +03003355static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3356{
3357 u32 reg;
3358
Felipe Balbidf62df52011-10-14 15:11:49 +03003359 /*
Wesley Cheng71ca43f2021-03-19 02:31:25 -07003360 * Ideally, dwc3_reset_gadget() would trigger the function
3361 * drivers to stop any active transfers through ep disable.
3362 * However, for functions which defer ep disable, such as mass
3363 * storage, we will need to rely on the call to stop active
3364 * transfers here, and avoid allowing of request queuing.
3365 */
3366 dwc->connected = false;
3367
3368 /*
Felipe Balbidf62df52011-10-14 15:11:49 +03003369 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3370 * would cause a missing Disconnect Event if there's a
3371 * pending Setup Packet in the FIFO.
3372 *
3373 * There's no suggested workaround on the official Bug
3374 * report, which states that "unless the driver/application
3375 * is doing any special handling of a disconnect event,
3376 * there is no functional issue".
3377 *
3378 * Unfortunately, it turns out that we _do_ some special
3379 * handling of a disconnect event, namely complete all
3380 * pending transfers, notify gadget driver of the
3381 * disconnection, and so on.
3382 *
3383 * Our suggested workaround is to follow the Disconnect
3384 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003385 * flag. Such flag gets set whenever we have a SETUP_PENDING
3386 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003387 * same endpoint.
3388 *
3389 * Refers to:
3390 *
3391 * STAR#9000466709: RTL: Device : Disconnect event not
3392 * generated if setup packet pending in FIFO
3393 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003394 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003395 if (dwc->setup_packet_pending)
3396 dwc3_gadget_disconnect_interrupt(dwc);
3397 }
3398
Felipe Balbi8e744752014-11-06 14:27:53 +08003399 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003400 /*
3401 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3402 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3403 * needs to ensure that it sends "a DEPENDXFER command for any active
3404 * transfers."
3405 */
3406 dwc3_stop_active_transfers(dwc);
Wesley Chengf09ddcf2021-03-11 15:59:02 -08003407 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003408
3409 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3410 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003411 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003412 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003413 dwc3_clear_stall_all_ep(dwc);
3414
3415 /* Reset device address to zero */
3416 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3417 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3418 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003419}
3420
Felipe Balbi72246da2011-08-19 18:10:58 +03003421static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3422{
Felipe Balbi72246da2011-08-19 18:10:58 +03003423 struct dwc3_ep *dep;
3424 int ret;
3425 u32 reg;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003426 u8 lanes = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003427 u8 speed;
3428
Felipe Balbi72246da2011-08-19 18:10:58 +03003429 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3430 speed = reg & DWC3_DSTS_CONNECTSPD;
3431 dwc->speed = speed;
3432
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003433 if (DWC3_IP_IS(DWC32))
3434 lanes = DWC3_DSTS_CONNLANES(reg) + 1;
3435
3436 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
3437
John Youn5fb6fda2016-11-10 17:23:25 -08003438 /*
3439 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3440 * each time on Connect Done.
3441 *
3442 * Currently we always use the reset value. If any platform
3443 * wants to set this to a different value, we need to add a
3444 * setting and update GCTL.RAMCLKSEL here.
3445 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003446
3447 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003448 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003449 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003450 dwc->gadget->ep0->maxpacket = 512;
3451 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003452
3453 if (lanes > 1)
3454 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
3455 else
3456 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
John Youn75808622016-02-05 17:09:13 -08003457 break;
John Youn2da9ad72016-05-20 16:34:26 -07003458 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003459 /*
3460 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3461 * would cause a missing USB3 Reset event.
3462 *
3463 * In such situations, we should force a USB3 Reset
3464 * event by calling our dwc3_gadget_reset_interrupt()
3465 * routine.
3466 *
3467 * Refers to:
3468 *
3469 * STAR#9000483510: RTL: SS : USB3 reset event may
3470 * not be generated always when the link enters poll
3471 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003472 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003473 dwc3_gadget_reset_interrupt(dwc);
3474
Felipe Balbi72246da2011-08-19 18:10:58 +03003475 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003476 dwc->gadget->ep0->maxpacket = 512;
3477 dwc->gadget->speed = USB_SPEED_SUPER;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003478
3479 if (lanes > 1) {
3480 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3481 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
3482 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003483 break;
John Youn2da9ad72016-05-20 16:34:26 -07003484 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003485 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003486 dwc->gadget->ep0->maxpacket = 64;
3487 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003488 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003489 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003490 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003491 dwc->gadget->ep0->maxpacket = 64;
3492 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003493 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003494 }
3495
Peter Chene81a7012020-08-21 10:55:48 +08003496 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003497
Pratyush Anand2b758352013-01-14 15:59:31 +05303498 /* Enable USB2 LPM Capability */
3499
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003500 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003501 !dwc->usb2_gadget_lpm_disable &&
John Youn2da9ad72016-05-20 16:34:26 -07003502 (speed != DWC3_DSTS_SUPERSPEED) &&
3503 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303504 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3505 reg |= DWC3_DCFG_LPM_CAP;
3506 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3507
3508 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3509 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3510
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003511 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3512 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303513
Huang Rui80caf7d2014-10-28 19:54:26 +08003514 /*
3515 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3516 * DCFG.LPMCap is set, core responses with an ACK and the
3517 * BESL value in the LPM token is less than or equal to LPM
3518 * NYET threshold.
3519 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003520 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003521 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003522
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003523 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003524 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003525
Thinh Nguyen5b738212019-10-23 19:15:43 -07003526 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003527 } else {
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003528 if (dwc->usb2_gadget_lpm_disable) {
3529 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3530 reg &= ~DWC3_DCFG_LPM_CAP;
3531 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3532 }
3533
Felipe Balbi356363b2013-12-19 16:37:05 -06003534 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3535 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003536 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303537 }
3538
Felipe Balbi72246da2011-08-19 18:10:58 +03003539 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003540 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003541 if (ret) {
3542 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3543 return;
3544 }
3545
3546 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003547 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003548 if (ret) {
3549 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3550 return;
3551 }
3552
3553 /*
3554 * Configure PHY via GUSB3PIPECTLn if required.
3555 *
3556 * Update GTXFIFOSIZn
3557 *
3558 * In both cases reset values should be sufficient.
3559 */
3560}
3561
3562static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3563{
Felipe Balbi72246da2011-08-19 18:10:58 +03003564 /*
3565 * TODO take core out of low power mode when that's
3566 * implemented.
3567 */
3568
Jiebing Liad14d4e2014-12-11 13:26:29 +08003569 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3570 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003571 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003572 spin_lock(&dwc->lock);
3573 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003574}
3575
3576static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3577 unsigned int evtinfo)
3578{
Felipe Balbifae2b902011-10-14 13:00:30 +03003579 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003580 unsigned int pwropt;
3581
3582 /*
3583 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3584 * Hibernation mode enabled which would show up when device detects
3585 * host-initiated U3 exit.
3586 *
3587 * In that case, device will generate a Link State Change Interrupt
3588 * from U3 to RESUME which is only necessary if Hibernation is
3589 * configured in.
3590 *
3591 * There are no functional changes due to such spurious event and we
3592 * just need to ignore it.
3593 *
3594 * Refers to:
3595 *
3596 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3597 * operational mode
3598 */
3599 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003600 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003601 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3602 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3603 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003604 return;
3605 }
3606 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003607
3608 /*
3609 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3610 * on the link partner, the USB session might do multiple entry/exit
3611 * of low power states before a transfer takes place.
3612 *
3613 * Due to this problem, we might experience lower throughput. The
3614 * suggested workaround is to disable DCTL[12:9] bits if we're
3615 * transitioning from U1/U2 to U0 and enable those bits again
3616 * after a transfer completes and there are no pending transfers
3617 * on any of the enabled endpoints.
3618 *
3619 * This is the first half of that workaround.
3620 *
3621 * Refers to:
3622 *
3623 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3624 * core send LGO_Ux entering U0
3625 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003626 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003627 if (next == DWC3_LINK_STATE_U0) {
3628 u32 u1u2;
3629 u32 reg;
3630
3631 switch (dwc->link_state) {
3632 case DWC3_LINK_STATE_U1:
3633 case DWC3_LINK_STATE_U2:
3634 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3635 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3636 | DWC3_DCTL_ACCEPTU2ENA
3637 | DWC3_DCTL_INITU1ENA
3638 | DWC3_DCTL_ACCEPTU1ENA);
3639
3640 if (!dwc->u1u2)
3641 dwc->u1u2 = reg & u1u2;
3642
3643 reg &= ~u1u2;
3644
Thinh Nguyen5b738212019-10-23 19:15:43 -07003645 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003646 break;
3647 default:
3648 /* do nothing */
3649 break;
3650 }
3651 }
3652 }
3653
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003654 switch (next) {
3655 case DWC3_LINK_STATE_U1:
3656 if (dwc->speed == USB_SPEED_SUPER)
3657 dwc3_suspend_gadget(dwc);
3658 break;
3659 case DWC3_LINK_STATE_U2:
3660 case DWC3_LINK_STATE_U3:
3661 dwc3_suspend_gadget(dwc);
3662 break;
3663 case DWC3_LINK_STATE_RESUME:
3664 dwc3_resume_gadget(dwc);
3665 break;
3666 default:
3667 /* do nothing */
3668 break;
3669 }
3670
Felipe Balbie57ebc12014-04-22 13:20:12 -05003671 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003672}
3673
Baolin Wang72704f82016-05-16 16:43:53 +08003674static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3675 unsigned int evtinfo)
3676{
3677 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3678
3679 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3680 dwc3_suspend_gadget(dwc);
3681
3682 dwc->link_state = next;
3683}
3684
Felipe Balbie1dadd32014-02-25 14:47:54 -06003685static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3686 unsigned int evtinfo)
3687{
3688 unsigned int is_ss = evtinfo & BIT(4);
3689
Felipe Balbibfad65e2017-04-19 14:59:27 +03003690 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003691 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3692 * have a known issue which can cause USB CV TD.9.23 to fail
3693 * randomly.
3694 *
3695 * Because of this issue, core could generate bogus hibernation
3696 * events which SW needs to ignore.
3697 *
3698 * Refers to:
3699 *
3700 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3701 * Device Fallback from SuperSpeed
3702 */
3703 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3704 return;
3705
3706 /* enter hibernation here */
3707}
3708
Felipe Balbi72246da2011-08-19 18:10:58 +03003709static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3710 const struct dwc3_event_devt *event)
3711{
3712 switch (event->type) {
3713 case DWC3_DEVICE_EVENT_DISCONNECT:
3714 dwc3_gadget_disconnect_interrupt(dwc);
3715 break;
3716 case DWC3_DEVICE_EVENT_RESET:
3717 dwc3_gadget_reset_interrupt(dwc);
3718 break;
3719 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3720 dwc3_gadget_conndone_interrupt(dwc);
3721 break;
3722 case DWC3_DEVICE_EVENT_WAKEUP:
3723 dwc3_gadget_wakeup_interrupt(dwc);
3724 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003725 case DWC3_DEVICE_EVENT_HIBER_REQ:
3726 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3727 "unexpected hibernation event\n"))
3728 break;
3729
3730 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3731 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003732 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3733 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3734 break;
3735 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003736 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003737 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003738 /*
3739 * Ignore suspend event until the gadget enters into
3740 * USB_STATE_CONFIGURED state.
3741 */
Peter Chene81a7012020-08-21 10:55:48 +08003742 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003743 dwc3_gadget_suspend_interrupt(dwc,
3744 event->event_info);
3745 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003746 break;
3747 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003748 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003749 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003750 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003751 break;
3752 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003753 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003754 }
3755}
3756
3757static void dwc3_process_event_entry(struct dwc3 *dwc,
3758 const union dwc3_event *event)
3759{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003760 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003761
Felipe Balbidfc5e802017-04-26 13:44:51 +03003762 if (!event->type.is_devspec)
3763 dwc3_endpoint_interrupt(dwc, &event->depevt);
3764 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003765 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003766 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003767 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003768}
3769
Felipe Balbidea520a2016-03-30 09:39:34 +03003770static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003771{
Felipe Balbidea520a2016-03-30 09:39:34 +03003772 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003773 irqreturn_t ret = IRQ_NONE;
3774 int left;
3775 u32 reg;
3776
Felipe Balbif42f2442013-06-12 21:25:08 +03003777 left = evt->count;
3778
3779 if (!(evt->flags & DWC3_EVENT_PENDING))
3780 return IRQ_NONE;
3781
3782 while (left > 0) {
3783 union dwc3_event event;
3784
John Younebbb2d52016-11-15 13:07:02 +02003785 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003786
3787 dwc3_process_event_entry(dwc, &event);
3788
3789 /*
3790 * FIXME we wrap around correctly to the next entry as
3791 * almost all entries are 4 bytes in size. There is one
3792 * entry which has 12 bytes which is a regular entry
3793 * followed by 8 bytes data. ATM I don't know how
3794 * things are organized if we get next to the a
3795 * boundary so I worry about that once we try to handle
3796 * that.
3797 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003798 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003799 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003800 }
3801
3802 evt->count = 0;
3803 evt->flags &= ~DWC3_EVENT_PENDING;
3804 ret = IRQ_HANDLED;
3805
3806 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003807 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003808 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003809 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003810
John Youncf40b862016-11-14 12:32:43 -08003811 if (dwc->imod_interval) {
3812 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3813 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3814 }
3815
Felipe Balbif42f2442013-06-12 21:25:08 +03003816 return ret;
3817}
3818
Felipe Balbidea520a2016-03-30 09:39:34 +03003819static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003820{
Felipe Balbidea520a2016-03-30 09:39:34 +03003821 struct dwc3_event_buffer *evt = _evt;
3822 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003823 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003824 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003825
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003826 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003827 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003828 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003829
3830 return ret;
3831}
3832
Felipe Balbidea520a2016-03-30 09:39:34 +03003833static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003834{
Felipe Balbidea520a2016-03-30 09:39:34 +03003835 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003836 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003837 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003838 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003839
Felipe Balbifc8bb912016-05-16 13:14:48 +03003840 if (pm_runtime_suspended(dwc->dev)) {
3841 pm_runtime_get(dwc->dev);
3842 disable_irq_nosync(dwc->irq_gadget);
3843 dwc->pending_events = true;
3844 return IRQ_HANDLED;
3845 }
3846
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003847 /*
3848 * With PCIe legacy interrupt, test shows that top-half irq handler can
3849 * be called again after HW interrupt deassertion. Check if bottom-half
3850 * irq event handler completes before caching new event to prevent
3851 * losing events.
3852 */
3853 if (evt->flags & DWC3_EVENT_PENDING)
3854 return IRQ_HANDLED;
3855
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003856 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003857 count &= DWC3_GEVNTCOUNT_MASK;
3858 if (!count)
3859 return IRQ_NONE;
3860
Felipe Balbib15a7622011-06-30 16:57:15 +03003861 evt->count = count;
3862 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003863
Felipe Balbie8adfc32013-06-12 21:11:14 +03003864 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003865 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003866 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003867 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003868
John Younebbb2d52016-11-15 13:07:02 +02003869 amount = min(count, evt->length - evt->lpos);
3870 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3871
3872 if (amount < count)
3873 memcpy(evt->cache, evt->buf, count - amount);
3874
John Youn65aca322016-11-15 13:08:59 +02003875 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3876
Felipe Balbib15a7622011-06-30 16:57:15 +03003877 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003878}
3879
Felipe Balbidea520a2016-03-30 09:39:34 +03003880static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003881{
Felipe Balbidea520a2016-03-30 09:39:34 +03003882 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003883
Felipe Balbidea520a2016-03-30 09:39:34 +03003884 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003885}
3886
Felipe Balbi6db38122016-10-03 11:27:01 +03003887static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3888{
3889 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3890 int irq;
3891
Hans de Goedef146b40b2019-10-05 23:04:48 +02003892 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003893 if (irq > 0)
3894 goto out;
3895
3896 if (irq == -EPROBE_DEFER)
3897 goto out;
3898
Hans de Goedef146b40b2019-10-05 23:04:48 +02003899 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003900 if (irq > 0)
3901 goto out;
3902
3903 if (irq == -EPROBE_DEFER)
3904 goto out;
3905
3906 irq = platform_get_irq(dwc3_pdev, 0);
3907 if (irq > 0)
3908 goto out;
3909
Felipe Balbi6db38122016-10-03 11:27:01 +03003910 if (!irq)
3911 irq = -EINVAL;
3912
3913out:
3914 return irq;
3915}
3916
Peter Chene81a7012020-08-21 10:55:48 +08003917static void dwc_gadget_release(struct device *dev)
3918{
3919 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3920
3921 kfree(gadget);
3922}
3923
Felipe Balbi72246da2011-08-19 18:10:58 +03003924/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003925 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003926 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003927 *
3928 * Returns 0 on success otherwise negative errno.
3929 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003930int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003931{
Felipe Balbi6db38122016-10-03 11:27:01 +03003932 int ret;
3933 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003934 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003935
Felipe Balbi6db38122016-10-03 11:27:01 +03003936 irq = dwc3_gadget_get_irq(dwc);
3937 if (irq < 0) {
3938 ret = irq;
3939 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003940 }
3941
3942 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003943
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303944 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3945 sizeof(*dwc->ep0_trb) * 2,
3946 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003947 if (!dwc->ep0_trb) {
3948 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3949 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003950 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003951 }
3952
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003953 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003954 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003955 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003956 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003957 }
3958
Felipe Balbi905dc042017-01-05 14:46:52 +02003959 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3960 &dwc->bounce_addr, GFP_KERNEL);
3961 if (!dwc->bounce) {
3962 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003963 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003964 }
3965
Baolin Wangbb014732016-10-14 17:11:33 +08003966 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003967 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3968 if (!dwc->gadget) {
3969 ret = -ENOMEM;
3970 goto err3;
3971 }
Baolin Wangbb014732016-10-14 17:11:33 +08003972
Peter Chene81a7012020-08-21 10:55:48 +08003973
3974 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3975 dev = &dwc->gadget->dev;
3976 dev->platform_data = dwc;
3977 dwc->gadget->ops = &dwc3_gadget_ops;
3978 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003979 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
Peter Chene81a7012020-08-21 10:55:48 +08003980 dwc->gadget->sg_supported = true;
3981 dwc->gadget->name = "dwc3-gadget";
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003982 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03003983
3984 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003985 * FIXME We might be setting max_speed to <SUPER, however versions
3986 * <2.20a of dwc3 have an issue with metastability (documented
3987 * elsewhere in this driver) which tells us we can't set max speed to
3988 * anything lower than SUPER.
3989 *
3990 * Because gadget.max_speed is only used by composite.c and function
3991 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3992 * to happen so we avoid sending SuperSpeed Capability descriptor
3993 * together with our BOS descriptor as that could confuse host into
3994 * thinking we can handle super speed.
3995 *
3996 * Note that, in fact, we won't even support GetBOS requests when speed
3997 * is less than super speed because we don't have means, yet, to tell
3998 * composite.c that we are USB 2.0 + LPM ECN.
3999 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07004000 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02004001 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02004002 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004003 dwc->revision);
4004
Peter Chene81a7012020-08-21 10:55:48 +08004005 dwc->gadget->max_speed = dwc->maximum_speed;
Thinh Nguyen67848142021-01-19 17:36:21 -08004006 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004007
4008 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03004009 * REVISIT: Here we should clear all pending IRQs to be
4010 * sure we're starting from a well known location.
4011 */
4012
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00004013 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03004014 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03004015 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08004016
4017 ret = usb_add_gadget(dwc->gadget);
4018 if (ret) {
4019 dev_err(dwc->dev, "failed to add gadget\n");
4020 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03004021 }
4022
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08004023 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4024 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4025 else
4026 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02004027
Felipe Balbi72246da2011-08-19 18:10:58 +03004028 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004029
Peter Chene81a7012020-08-21 10:55:48 +08004030err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004031 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08004032err4:
4033 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004034err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004035 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4036 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004037
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004038err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004039 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004040
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004041err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304042 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004043 dwc->ep0_trb, dwc->ep0_trb_addr);
4044
Felipe Balbi72246da2011-08-19 18:10:58 +03004045err0:
4046 return ret;
4047}
4048
Felipe Balbi7415f172012-04-30 14:56:33 +03004049/* -------------------------------------------------------------------------- */
4050
Felipe Balbi72246da2011-08-19 18:10:58 +03004051void dwc3_gadget_exit(struct dwc3 *dwc)
4052{
Peter Chene81a7012020-08-21 10:55:48 +08004053 usb_del_gadget_udc(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004054 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02004055 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004056 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004057 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304058 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004059 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004060}
Felipe Balbi7415f172012-04-30 14:56:33 +03004061
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004062int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004063{
Roger Quadros9772b472016-04-12 11:33:29 +03004064 if (!dwc->gadget_driver)
4065 return 0;
4066
Roger Quadros1551e352017-02-15 14:16:26 +02004067 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004068 dwc3_disconnect_gadget(dwc);
4069 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004070
4071 return 0;
4072}
4073
4074int dwc3_gadget_resume(struct dwc3 *dwc)
4075{
Felipe Balbi7415f172012-04-30 14:56:33 +03004076 int ret;
4077
Roger Quadros9772b472016-04-12 11:33:29 +03004078 if (!dwc->gadget_driver)
4079 return 0;
4080
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004081 ret = __dwc3_gadget_start(dwc);
4082 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004083 goto err0;
4084
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004085 ret = dwc3_gadget_run_stop(dwc, true, false);
4086 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004087 goto err1;
4088
Felipe Balbi7415f172012-04-30 14:56:33 +03004089 return 0;
4090
4091err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004092 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004093
4094err0:
4095 return ret;
4096}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004097
4098void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4099{
4100 if (dwc->pending_events) {
4101 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4102 dwc->pending_events = false;
4103 enable_irq(dwc->irq_gadget);
4104 }
4105}