blob: dd1342403bb24212e7ca1aeebcb1fbb317914798 [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.
Thinh Nguyenddae7972021-04-22 16:51:43 -0700733 *
734 * However, if the controller is capable of
735 * TXF_FLUSH_BYPASS, then IN direction endpoints will
736 * automatically restart the stream without the driver
737 * initiation.
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700738 */
Thinh Nguyenddae7972021-04-22 16:51:43 -0700739 if (!dep->direction ||
740 !(dwc->hwparams.hwparams9 &
741 DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
742 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700743 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300744 }
745
Felipe Balbi2870e502016-11-03 13:53:29 +0200746out:
747 trace_dwc3_gadget_ep_enable(dep);
748
Felipe Balbi72246da2011-08-19 18:10:58 +0300749 return 0;
750}
751
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200752static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300753{
754 struct dwc3_request *req;
755
Felipe Balbic5353b22019-02-13 13:00:54 +0200756 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300757
Felipe Balbi0e146022016-06-21 10:32:02 +0300758 /* - giveback all requests to gadget driver */
759 while (!list_empty(&dep->started_list)) {
760 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200761
Felipe Balbi0e146022016-06-21 10:32:02 +0300762 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200763 }
764
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200765 while (!list_empty(&dep->pending_list)) {
766 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300767
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200768 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200770
771 while (!list_empty(&dep->cancelled_list)) {
772 req = next_request(&dep->cancelled_list);
773
774 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
775 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300776}
777
778/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300779 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300780 * @dep: the endpoint to disable
781 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300782 * This function undoes what __dwc3_gadget_ep_enable did and also removes
783 * requests which are currently being processed by the hardware and those which
784 * are not yet scheduled.
785 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200786 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300787 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300788static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
789{
790 struct dwc3 *dwc = dep->dwc;
791 u32 reg;
792
Felipe Balbi2870e502016-11-03 13:53:29 +0200793 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500794
Felipe Balbi687ef982014-04-16 10:30:33 -0500795 /* make sure HW endpoint isn't stalled */
796 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500797 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500798
Felipe Balbi72246da2011-08-19 18:10:58 +0300799 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
800 reg &= ~DWC3_DALEPENA_EP(dep->number);
801 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
802
John Youn39ebb052016-11-09 16:36:28 -0800803 /* Clear out the ep descriptors for non-ep0 */
804 if (dep->number > 1) {
805 dep->endpoint.comp_desc = NULL;
806 dep->endpoint.desc = NULL;
807 }
808
Wesley Chengf09ddcf2021-03-11 15:59:02 -0800809 dwc3_remove_requests(dwc, dep);
810
Wesley Cheng5aef62972021-03-24 11:31:04 -0700811 dep->stream_capable = false;
812 dep->type = 0;
813 dep->flags = 0;
814
Felipe Balbi72246da2011-08-19 18:10:58 +0300815 return 0;
816}
817
818/* -------------------------------------------------------------------------- */
819
820static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
821 const struct usb_endpoint_descriptor *desc)
822{
823 return -EINVAL;
824}
825
826static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
827{
828 return -EINVAL;
829}
830
831/* -------------------------------------------------------------------------- */
832
833static int dwc3_gadget_ep_enable(struct usb_ep *ep,
834 const struct usb_endpoint_descriptor *desc)
835{
836 struct dwc3_ep *dep;
837 struct dwc3 *dwc;
838 unsigned long flags;
839 int ret;
840
841 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
842 pr_debug("dwc3: invalid parameters\n");
843 return -EINVAL;
844 }
845
846 if (!desc->wMaxPacketSize) {
847 pr_debug("dwc3: missing wMaxPacketSize\n");
848 return -EINVAL;
849 }
850
851 dep = to_dwc3_ep(ep);
852 dwc = dep->dwc;
853
Felipe Balbi95ca9612015-12-10 13:08:20 -0600854 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
855 "%s is already enabled\n",
856 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300857 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300858
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300860 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300861 spin_unlock_irqrestore(&dwc->lock, flags);
862
863 return ret;
864}
865
866static int dwc3_gadget_ep_disable(struct usb_ep *ep)
867{
868 struct dwc3_ep *dep;
869 struct dwc3 *dwc;
870 unsigned long flags;
871 int ret;
872
873 if (!ep) {
874 pr_debug("dwc3: invalid parameters\n");
875 return -EINVAL;
876 }
877
878 dep = to_dwc3_ep(ep);
879 dwc = dep->dwc;
880
Felipe Balbi95ca9612015-12-10 13:08:20 -0600881 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
882 "%s is already disabled\n",
883 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300885
Felipe Balbi72246da2011-08-19 18:10:58 +0300886 spin_lock_irqsave(&dwc->lock, flags);
887 ret = __dwc3_gadget_ep_disable(dep);
888 spin_unlock_irqrestore(&dwc->lock, flags);
889
890 return ret;
891}
892
893static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300894 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300895{
896 struct dwc3_request *req;
897 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
899 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900900 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300901 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300902
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300903 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300904 req->epnum = dep->number;
905 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200906 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300907
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500908 trace_dwc3_alloc_request(req);
909
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 return &req->request;
911}
912
913static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
914 struct usb_request *request)
915{
916 struct dwc3_request *req = to_dwc3_request(request);
917
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500918 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300919 kfree(req);
920}
921
Felipe Balbi42626912018-04-09 13:01:43 +0300922/**
923 * dwc3_ep_prev_trb - returns the previous TRB in the ring
924 * @dep: The endpoint with the TRB ring
925 * @index: The index of the current TRB in the ring
926 *
927 * Returns the TRB prior to the one pointed to by the index. If the
928 * index is 0, we will wrap backwards, skip the link TRB, and return
929 * the one just before that.
930 */
931static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
932{
933 u8 tmp = index;
934
935 if (!tmp)
936 tmp = DWC3_TRB_NUM - 1;
937
938 return &dep->trb_pool[tmp - 1];
939}
940
941static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
942{
943 struct dwc3_trb *tmp;
944 u8 trbs_left;
945
946 /*
947 * If enqueue & dequeue are equal than it is either full or empty.
948 *
949 * One way to know for sure is if the TRB right before us has HWO bit
950 * set or not. If it has, then we're definitely full and can't fit any
951 * more transfers in our ring.
952 */
953 if (dep->trb_enqueue == dep->trb_dequeue) {
954 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
955 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
956 return 0;
957
958 return DWC3_TRB_NUM - 1;
959 }
960
961 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
962 trbs_left &= (DWC3_TRB_NUM - 1);
963
964 if (dep->trb_dequeue < dep->trb_enqueue)
965 trbs_left--;
966
967 return trbs_left;
968}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300969
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200970static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300971 dma_addr_t dma, unsigned int length, unsigned int chain,
972 unsigned int node, unsigned int stream_id,
973 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700974 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200975{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300976 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800977 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300978 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200979
Felipe Balbif6bafc62012-02-06 11:04:53 +0200980 trb->size = DWC3_TRB_SIZE_LENGTH(length);
981 trb->bpl = lower_32_bits(dma);
982 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200983
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200984 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200985 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200986 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200987 break;
988
989 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300990 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530991 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300992
Manu Gautam40d829f2017-07-19 17:07:10 +0530993 /*
994 * USB Specification 2.0 Section 5.9.2 states that: "If
995 * there is only a single transaction in the microframe,
996 * only a DATA0 data packet PID is used. If there are
997 * two transactions per microframe, DATA1 is used for
998 * the first transaction data packet and DATA0 is used
999 * for the second transaction data packet. If there are
1000 * three transactions per microframe, DATA2 is used for
1001 * the first transaction data packet, DATA1 is used for
1002 * the second, and DATA0 is used for the third."
1003 *
1004 * IOW, we should satisfy the following cases:
1005 *
1006 * 1) length <= maxpacket
1007 * - DATA0
1008 *
1009 * 2) maxpacket < length <= (2 * maxpacket)
1010 * - DATA1, DATA0
1011 *
1012 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1013 * - DATA2, DATA1, DATA0
1014 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001015 if (speed == USB_SPEED_HIGH) {
1016 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301017 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301018 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1019
1020 if (length <= (2 * maxp))
1021 mult--;
1022
1023 if (length <= maxp)
1024 mult--;
1025
1026 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001027 }
1028 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301029 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001030 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001031
1032 /* always enable Interrupt on Missed ISOC */
1033 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001034 break;
1035
1036 case USB_ENDPOINT_XFER_BULK:
1037 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001038 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001039 break;
1040 default:
1041 /*
1042 * This is only possible with faulty memory because we
1043 * checked it already :)
1044 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001045 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1046 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001047 }
1048
Tejas Joglekar244add82018-12-10 16:08:13 +05301049 /*
1050 * Enable Continue on Short Packet
1051 * when endpoint is not a stream capable
1052 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001053 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301054 if (!dep->stream_capable)
1055 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001056
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001057 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001058 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1059 }
1060
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001061 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001062 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001063
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301064 if (chain)
1065 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001066 else if (dep->stream_capable && is_last)
1067 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301068
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001069 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001070 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001071
1072 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001073
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301074 dwc3_ep_inc_enq(dep);
1075
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001076 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001077}
1078
John Youn361572b2016-05-19 17:26:17 -07001079/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001080 * dwc3_prepare_one_trb - setup one TRB from one request
1081 * @dep: endpoint for which this request is prepared
1082 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001083 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001084 * @chain: should this TRB be chained to the next?
1085 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001086 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001087 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001088 */
1089static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001090 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001091 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1092 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001093{
1094 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301095 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001096 unsigned int stream_id = req->request.stream_id;
1097 unsigned int short_not_ok = req->request.short_not_ok;
1098 unsigned int no_interrupt = req->request.no_interrupt;
1099 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301100
Thinh Nguyen2b803572020-09-24 01:21:30 -07001101 if (use_bounce_buffer)
1102 dma = dep->dwc->bounce_addr;
1103 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301104 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001105 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301106 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001107
1108 trb = &dep->trb_pool[dep->trb_enqueue];
1109
1110 if (!req->trb) {
1111 dwc3_gadget_move_started_request(req);
1112 req->trb = trb;
1113 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001114 }
1115
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001116 req->num_trbs++;
1117
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001118 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001119 stream_id, short_not_ok, no_interrupt, is_last,
1120 must_interrupt);
1121}
1122
1123static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1124{
1125 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1126 unsigned int rem = req->request.length % maxp;
1127
1128 if ((req->request.length && req->request.zero && !rem &&
1129 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1130 (!req->direction && rem))
1131 return true;
1132
1133 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001134}
1135
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001136/**
1137 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1138 * @dep: The endpoint that the request belongs to
1139 * @req: The request to prepare
1140 * @entry_length: The last SG entry size
1141 * @node: Indicates whether this is not the first entry (for isoc only)
1142 *
1143 * Return the number of TRBs prepared.
1144 */
1145static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1146 struct dwc3_request *req, unsigned int entry_length,
1147 unsigned int node)
1148{
1149 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1150 unsigned int rem = req->request.length % maxp;
1151 unsigned int num_trbs = 1;
1152
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001153 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001154 num_trbs++;
1155
1156 if (dwc3_calc_trbs_left(dep) < num_trbs)
1157 return 0;
1158
1159 req->needs_extra_trb = num_trbs > 1;
1160
1161 /* Prepare a normal TRB */
1162 if (req->direction || req->request.length)
1163 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001164 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001165
1166 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1167 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1168 dwc3_prepare_one_trb(dep, req,
1169 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001170 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001171
1172 return num_trbs;
1173}
1174
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001175static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001176 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001177{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301178 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001179 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001180 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001181 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301182 unsigned int remaining = req->request.num_mapped_sgs
1183 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001184 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001185 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301186
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001187 /*
1188 * If we resume preparing the request, then get the remaining length of
1189 * the request and resume where we left off.
1190 */
1191 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1192 length -= sg_dma_len(s);
1193
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301194 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001195 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001196 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001197 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001198 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001199
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001200 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1201
1202 length -= trb_length;
1203
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001204 /*
1205 * IOMMU driver is coalescing the list of sgs which shares a
1206 * page boundary into one and giving it to USB driver. With
1207 * this the number of sgs mapped is not equal to the number of
1208 * sgs passed. So mark the chain bit to false if it isthe last
1209 * mapped sg.
1210 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001211 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001212 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001213
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001214 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001215 break;
1216
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001217 if (last_sg) {
1218 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001219 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001220 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001221 /*
1222 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001223 * next SG entry. If not, set interrupt on this TRB to
1224 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001225 * free.
1226 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001227 if (num_trbs_left == 1 || (needs_extra_trb &&
1228 num_trbs_left <= 2 &&
1229 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001230 must_interrupt = true;
1231
1232 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1233 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001234 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001235
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301236 /*
1237 * There can be a situation where all sgs in sglist are not
1238 * queued because of insufficient trb number. To handle this
1239 * case, update start_sg to next sg to be queued, so that
1240 * we have free trbs we can continue queuing from where we
1241 * previously stopped
1242 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001243 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301244 req->start_sg = sg_next(s);
1245
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301246 req->num_queued_sgs++;
1247
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001248 /*
1249 * The number of pending SG entries may not correspond to the
1250 * number of mapped SG entries. If all the data are queued, then
1251 * don't include unused SG entries.
1252 */
1253 if (length == 0) {
1254 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1255 break;
1256 }
1257
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001258 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001259 break;
1260 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001261
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001262 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001263}
1264
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001265static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001266 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001267{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001268 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001269}
1270
Felipe Balbi72246da2011-08-19 18:10:58 +03001271/*
1272 * dwc3_prepare_trbs - setup TRBs from requests
1273 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001274 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001275 * The function goes through the requests list and sets up TRBs for the
1276 * transfers. The function returns once there are no more TRBs available or
1277 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001278 *
1279 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001281static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001282{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001283 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001284 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001285
1286 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1287
Felipe Balbid86c5a62016-10-25 13:48:52 +03001288 /*
1289 * We can get in a situation where there's a request in the started list
1290 * but there weren't enough TRBs to fully kick it in the first time
1291 * around, so it has been waiting for more TRBs to be freed up.
1292 *
1293 * In that case, we should check if we have a request with pending_sgs
1294 * in the started list and prepare TRBs for that request first,
1295 * otherwise we will prepare TRBs completely out of order and that will
1296 * break things.
1297 */
1298 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001299 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001300 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001301 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001302 return ret;
1303 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001304
1305 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001306 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001307
1308 /*
1309 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1310 * burst capability may try to read and use TRBs beyond the
1311 * active transfer instead of stopping.
1312 */
1313 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001314 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001315 }
1316
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001317 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001318 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001319
1320 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1321 dep->direction);
1322 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001323 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001324
1325 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301326 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301327 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001328 req->num_pending_sgs = req->request.num_mapped_sgs;
1329
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001330 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001331 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001332 if (req->num_pending_sgs)
1333 return ret;
1334 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001335 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001336 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001337
Thinh Nguyen490410b2020-09-24 01:21:55 -07001338 if (!ret || !dwc3_calc_trbs_left(dep))
1339 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001340
1341 /*
1342 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1343 * burst capability may try to read and use TRBs beyond the
1344 * active transfer instead of stopping.
1345 */
1346 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001347 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001348 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001349
1350 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001351}
1352
Thinh Nguyen8d990872020-03-29 16:12:57 -07001353static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1354
Felipe Balbi7fdca762017-09-05 14:41:34 +03001355static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001356{
1357 struct dwc3_gadget_ep_cmd_params params;
1358 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001359 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001360 int ret;
1361 u32 cmd;
1362
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001363 /*
1364 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1365 * This happens when we need to stop and restart a transfer such as in
1366 * the case of reinitiating a stream or retrying an isoc transfer.
1367 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001368 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001369 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001370 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001371
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001372 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001373
Thinh Nguyen23384842020-09-30 17:44:38 -07001374 /*
1375 * If there's no new TRB prepared and we don't need to restart a
1376 * transfer, there's no need to update the transfer.
1377 */
1378 if (!ret && !starting)
1379 return ret;
1380
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001381 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001382 if (!req) {
1383 dep->flags |= DWC3_EP_PENDING_REQUEST;
1384 return 0;
1385 }
1386
1387 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001388
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001389 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301390 params.param0 = upper_32_bits(req->trb_dma);
1391 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001392 cmd = DWC3_DEPCMD_STARTTRANSFER;
1393
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301394 if (dep->stream_capable)
1395 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1396
Felipe Balbi7fdca762017-09-05 14:41:34 +03001397 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1398 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301399 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001400 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1401 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301402 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001403
Felipe Balbi2cd47182016-04-12 16:42:43 +03001404 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001405 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001406 struct dwc3_request *tmp;
1407
1408 if (ret == -EAGAIN)
1409 return ret;
1410
1411 dwc3_stop_active_transfer(dep, true, true);
1412
1413 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001414 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
Thinh Nguyen8d990872020-03-29 16:12:57 -07001415
1416 /* If ep isn't started, then there's no end transfer pending */
1417 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1418 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1419
Felipe Balbi72246da2011-08-19 18:10:58 +03001420 return ret;
1421 }
1422
Thinh Nguyene0d19562020-05-05 19:46:57 -07001423 if (dep->stream_capable && req->request.is_last)
1424 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1425
Felipe Balbi72246da2011-08-19 18:10:58 +03001426 return 0;
1427}
1428
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001429static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1430{
1431 u32 reg;
1432
1433 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1434 return DWC3_DSTS_SOFFN(reg);
1435}
1436
Thinh Nguyend92021f2018-11-14 22:56:54 -08001437/**
1438 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1439 * @dep: isoc endpoint
1440 *
1441 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1442 * microframe number reported by the XferNotReady event for the future frame
1443 * number to start the isoc transfer.
1444 *
1445 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1446 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1447 * XferNotReady event are invalid. The driver uses this number to schedule the
1448 * isochronous transfer and passes it to the START TRANSFER command. Because
1449 * this number is invalid, the command may fail. If BIT[15:14] matches the
1450 * internal 16-bit microframe, the START TRANSFER command will pass and the
1451 * transfer will start at the scheduled time, if it is off by 1, the command
1452 * will still pass, but the transfer will start 2 seconds in the future. For all
1453 * other conditions, the START TRANSFER command will fail with bus-expiry.
1454 *
1455 * In order to workaround this issue, we can test for the correct combination of
1456 * BIT[15:14] by sending START TRANSFER commands with different values of
1457 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1458 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1459 * As the result, within the 4 possible combinations for BIT[15:14], there will
1460 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1461 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1462 * value is the correct combination.
1463 *
1464 * Since there are only 4 outcomes and the results are ordered, we can simply
1465 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1466 * deduce the smaller successful combination.
1467 *
1468 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1469 * of BIT[15:14]. The correct combination is as follow:
1470 *
1471 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1472 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1473 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1474 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1475 *
1476 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1477 * endpoints.
1478 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001479static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301480{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001481 int cmd_status = 0;
1482 bool test0;
1483 bool test1;
1484
1485 while (dep->combo_num < 2) {
1486 struct dwc3_gadget_ep_cmd_params params;
1487 u32 test_frame_number;
1488 u32 cmd;
1489
1490 /*
1491 * Check if we can start isoc transfer on the next interval or
1492 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1493 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001494 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001495 test_frame_number |= dep->combo_num << 14;
1496 test_frame_number += max_t(u32, 4, dep->interval);
1497
1498 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1499 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1500
1501 cmd = DWC3_DEPCMD_STARTTRANSFER;
1502 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1503 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1504
1505 /* Redo if some other failure beside bus-expiry is received */
1506 if (cmd_status && cmd_status != -EAGAIN) {
1507 dep->start_cmd_status = 0;
1508 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001509 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001510 }
1511
1512 /* Store the first test status */
1513 if (dep->combo_num == 0)
1514 dep->start_cmd_status = cmd_status;
1515
1516 dep->combo_num++;
1517
1518 /*
1519 * End the transfer if the START_TRANSFER command is successful
1520 * to wait for the next XferNotReady to test the command again
1521 */
1522 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001523 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001524 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001525 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301526 }
1527
Thinh Nguyend92021f2018-11-14 22:56:54 -08001528 /* test0 and test1 are both completed at this point */
1529 test0 = (dep->start_cmd_status == 0);
1530 test1 = (cmd_status == 0);
1531
1532 if (!test0 && test1)
1533 dep->combo_num = 1;
1534 else if (!test0 && !test1)
1535 dep->combo_num = 2;
1536 else if (test0 && !test1)
1537 dep->combo_num = 3;
1538 else if (test0 && test1)
1539 dep->combo_num = 0;
1540
Michael Grzeschikca143782020-07-01 20:24:51 +02001541 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001542 dep->frame_number |= dep->combo_num << 14;
1543 dep->frame_number += max_t(u32, 4, dep->interval);
1544
1545 /* Reinitialize test variables */
1546 dep->start_cmd_status = 0;
1547 dep->combo_num = 0;
1548
Felipe Balbi25abad62018-08-14 10:41:19 +03001549 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001550}
1551
Felipe Balbi25abad62018-08-14 10:41:19 +03001552static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301553{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001554 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001555 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001556 int ret;
1557 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001558
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001559 if (list_empty(&dep->pending_list) &&
1560 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301561 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001562 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301563 }
1564
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001565 if (!dwc->dis_start_transfer_quirk &&
1566 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1567 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001568 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001569 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001570 }
1571
Michael Olbrichc5a70922020-07-01 20:24:52 +02001572 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001573 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001574 u32 frame = __dwc3_gadget_get_frame(dwc);
1575 bool rollover = frame <
1576 (dep->frame_number & DWC3_FRNUMBER_MASK);
1577
1578 /*
1579 * frame_number is set from XferNotReady and may be already
1580 * out of date. DSTS only provides the lower 14 bit of the
1581 * current frame number. So add the upper two bits of
1582 * frame_number and handle a possible rollover.
1583 * This will provide the correct frame_number unless more than
1584 * rollover has happened since XferNotReady.
1585 */
1586
1587 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1588 frame;
1589 if (rollover)
1590 dep->frame_number += BIT(14);
1591 }
1592
Felipe Balbid5370102018-08-14 10:42:43 +03001593 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1594 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1595
1596 ret = __dwc3_gadget_kick_transfer(dep);
1597 if (ret != -EAGAIN)
1598 break;
1599 }
1600
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001601 /*
1602 * After a number of unsuccessful start attempts due to bus-expiry
1603 * status, issue END_TRANSFER command and retry on the next XferNotReady
1604 * event.
1605 */
1606 if (ret == -EAGAIN) {
1607 struct dwc3_gadget_ep_cmd_params params;
1608 u32 cmd;
1609
1610 cmd = DWC3_DEPCMD_ENDTRANSFER |
1611 DWC3_DEPCMD_CMDIOC |
1612 DWC3_DEPCMD_PARAM(dep->resource_index);
1613
1614 dep->resource_index = 0;
1615 memset(&params, 0, sizeof(params));
1616
1617 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1618 if (!ret)
1619 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1620 }
1621
Felipe Balbid5370102018-08-14 10:42:43 +03001622 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301623}
1624
Felipe Balbi72246da2011-08-19 18:10:58 +03001625static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1626{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001627 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001628
Wesley Chengf09ddcf2021-03-11 15:59:02 -08001629 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001630 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1631 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001632 return -ESHUTDOWN;
1633 }
1634
Felipe Balbi04fb3652017-05-17 15:57:45 +03001635 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1636 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001637 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001638
Felipe Balbib2b6d602019-01-11 12:58:52 +02001639 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1640 "%s: request %pK already in flight\n",
1641 dep->name, &req->request))
1642 return -EINVAL;
1643
Felipe Balbifc8bb912016-05-16 13:14:48 +03001644 pm_runtime_get(dwc->dev);
1645
Felipe Balbi72246da2011-08-19 18:10:58 +03001646 req->request.actual = 0;
1647 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001648
Felipe Balbife84f522015-09-01 09:01:38 -05001649 trace_dwc3_ep_queue(req);
1650
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001651 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001652 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001653
Thinh Nguyene0d19562020-05-05 19:46:57 -07001654 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1655 return 0;
1656
Thinh Nguyenc5036722020-09-02 18:42:58 -07001657 /*
1658 * Start the transfer only after the END_TRANSFER is completed
1659 * and endpoint STALL is cleared.
1660 */
1661 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1662 (dep->flags & DWC3_EP_WEDGE) ||
1663 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001664 dep->flags |= DWC3_EP_DELAY_START;
1665 return 0;
1666 }
1667
Felipe Balbid889c232016-09-29 15:44:29 +03001668 /*
1669 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1670 * wait for a XferNotReady event so we will know what's the current
1671 * (micro-)frame number.
1672 *
1673 * Without this trick, we are very, very likely gonna get Bus Expiry
1674 * errors which will force us issue EndTransfer command.
1675 */
1676 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001677 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1678 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001679 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001680
1681 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001682 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001683 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001684 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001685 }
1686
Felipe Balbi7fdca762017-09-05 14:41:34 +03001687 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001688}
1689
1690static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1691 gfp_t gfp_flags)
1692{
1693 struct dwc3_request *req = to_dwc3_request(request);
1694 struct dwc3_ep *dep = to_dwc3_ep(ep);
1695 struct dwc3 *dwc = dep->dwc;
1696
1697 unsigned long flags;
1698
1699 int ret;
1700
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001701 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 ret = __dwc3_gadget_ep_queue(dep, req);
1703 spin_unlock_irqrestore(&dwc->lock, flags);
1704
1705 return ret;
1706}
1707
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001708static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1709{
1710 int i;
1711
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001712 /* If req->trb is not set, then the request has not started */
1713 if (!req->trb)
1714 return;
1715
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001716 /*
1717 * If request was already started, this means we had to
1718 * stop the transfer. With that we also need to ignore
1719 * all TRBs used by the request, however TRBs can only
1720 * be modified after completion of END_TRANSFER
1721 * command. So what we do here is that we wait for
1722 * END_TRANSFER completion and only after that, we jump
1723 * over TRBs by clearing HWO and incrementing dequeue
1724 * pointer.
1725 */
1726 for (i = 0; i < req->num_trbs; i++) {
1727 struct dwc3_trb *trb;
1728
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001729 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001730 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1731 dwc3_ep_inc_deq(dep);
1732 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001733
1734 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001735}
1736
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001737static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1738{
1739 struct dwc3_request *req;
1740 struct dwc3_request *tmp;
Ray Chi04dd6e72021-03-28 02:17:42 +08001741 struct dwc3 *dwc = dep->dwc;
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001742
1743 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1744 dwc3_gadget_ep_skip_trbs(dep, req);
Ray Chi04dd6e72021-03-28 02:17:42 +08001745 switch (req->status) {
1746 case DWC3_REQUEST_STATUS_DISCONNECTED:
1747 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1748 break;
1749 case DWC3_REQUEST_STATUS_DEQUEUED:
1750 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1751 break;
1752 case DWC3_REQUEST_STATUS_STALLED:
1753 dwc3_gadget_giveback(dep, req, -EPIPE);
1754 break;
1755 default:
1756 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
1757 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1758 break;
1759 }
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001760 }
1761}
1762
Felipe Balbi72246da2011-08-19 18:10:58 +03001763static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1764 struct usb_request *request)
1765{
1766 struct dwc3_request *req = to_dwc3_request(request);
1767 struct dwc3_request *r = NULL;
1768
1769 struct dwc3_ep *dep = to_dwc3_ep(ep);
1770 struct dwc3 *dwc = dep->dwc;
1771
1772 unsigned long flags;
1773 int ret = 0;
1774
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001775 trace_dwc3_ep_dequeue(req);
1776
Felipe Balbi72246da2011-08-19 18:10:58 +03001777 spin_lock_irqsave(&dwc->lock, flags);
1778
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001779 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001781 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001782 }
1783
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001786 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1787 goto out;
1788 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001789 }
1790
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001791 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001792 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001793 struct dwc3_request *t;
1794
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001796 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001797
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001798 /*
1799 * Remove any started request if the transfer is
1800 * cancelled.
1801 */
1802 list_for_each_entry_safe(r, t, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001803 dwc3_gadget_move_cancelled_request(r,
1804 DWC3_REQUEST_STATUS_DEQUEUED);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001805
Thinh Nguyena5c76822021-01-04 22:42:39 -08001806 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1807
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001808 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001809 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001810 }
1811
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001812 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1813 request, ep->name);
1814 ret = -EINVAL;
1815out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001816 spin_unlock_irqrestore(&dwc->lock, flags);
1817
1818 return ret;
1819}
1820
Felipe Balbi7a608552014-09-24 14:19:52 -05001821int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001822{
1823 struct dwc3_gadget_ep_cmd_params params;
1824 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001825 struct dwc3_request *req;
1826 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001827 int ret;
1828
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001829 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1830 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1831 return -EINVAL;
1832 }
1833
Felipe Balbi72246da2011-08-19 18:10:58 +03001834 memset(&params, 0x00, sizeof(params));
1835
1836 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001837 struct dwc3_trb *trb;
1838
Felipe Balbie319bd62020-08-13 08:35:38 +03001839 unsigned int transfer_in_flight;
1840 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001841
1842 if (dep->number > 1)
1843 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1844 else
1845 trb = &dwc->ep0_trb[dep->trb_enqueue];
1846
1847 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1848 started = !list_empty(&dep->started_list);
1849
1850 if (!protocol && ((dep->direction && transfer_in_flight) ||
1851 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001852 return -EAGAIN;
1853 }
1854
Felipe Balbi2cd47182016-04-12 16:42:43 +03001855 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1856 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001858 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 dep->name);
1860 else
1861 dep->flags |= DWC3_EP_STALL;
1862 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001863 /*
1864 * Don't issue CLEAR_STALL command to control endpoints. The
1865 * controller automatically clears the STALL when it receives
1866 * the SETUP token.
1867 */
1868 if (dep->number <= 1) {
1869 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1870 return 0;
1871 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001872
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001873 dwc3_stop_active_transfer(dep, true, true);
1874
1875 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001876 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001877
1878 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1879 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1880 return 0;
1881 }
1882
1883 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1884
John Youn50c763f2016-05-31 17:49:56 -07001885 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001886 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001887 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001888 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001889 return ret;
1890 }
1891
1892 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1893
Thinh Nguyenc5036722020-09-02 18:42:58 -07001894 if ((dep->flags & DWC3_EP_DELAY_START) &&
1895 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1896 __dwc3_gadget_kick_transfer(dep);
1897
1898 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001899 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001900
Felipe Balbi72246da2011-08-19 18:10:58 +03001901 return ret;
1902}
1903
1904static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1905{
1906 struct dwc3_ep *dep = to_dwc3_ep(ep);
1907 struct dwc3 *dwc = dep->dwc;
1908
1909 unsigned long flags;
1910
1911 int ret;
1912
1913 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001914 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001915 spin_unlock_irqrestore(&dwc->lock, flags);
1916
1917 return ret;
1918}
1919
1920static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1921{
1922 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001923 struct dwc3 *dwc = dep->dwc;
1924 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001925 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001926
Paul Zimmerman249a4562012-02-24 17:32:16 -08001927 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001928 dep->flags |= DWC3_EP_WEDGE;
1929
Pratyush Anand08f0d962012-06-25 22:40:43 +05301930 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001931 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301932 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001933 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001934 spin_unlock_irqrestore(&dwc->lock, flags);
1935
1936 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001937}
1938
1939/* -------------------------------------------------------------------------- */
1940
1941static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1942 .bLength = USB_DT_ENDPOINT_SIZE,
1943 .bDescriptorType = USB_DT_ENDPOINT,
1944 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1945};
1946
1947static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1948 .enable = dwc3_gadget_ep0_enable,
1949 .disable = dwc3_gadget_ep0_disable,
1950 .alloc_request = dwc3_gadget_ep_alloc_request,
1951 .free_request = dwc3_gadget_ep_free_request,
1952 .queue = dwc3_gadget_ep0_queue,
1953 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301954 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001955 .set_wedge = dwc3_gadget_ep_set_wedge,
1956};
1957
1958static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1959 .enable = dwc3_gadget_ep_enable,
1960 .disable = dwc3_gadget_ep_disable,
1961 .alloc_request = dwc3_gadget_ep_alloc_request,
1962 .free_request = dwc3_gadget_ep_free_request,
1963 .queue = dwc3_gadget_ep_queue,
1964 .dequeue = dwc3_gadget_ep_dequeue,
1965 .set_halt = dwc3_gadget_ep_set_halt,
1966 .set_wedge = dwc3_gadget_ep_set_wedge,
1967};
1968
1969/* -------------------------------------------------------------------------- */
1970
1971static int dwc3_gadget_get_frame(struct usb_gadget *g)
1972{
1973 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001974
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001975 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001976}
1977
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001978static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001979{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001980 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001981
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001982 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 u32 reg;
1984
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 /*
1988 * According to the Databook Remote wakeup request should
1989 * be issued only when the device is in early suspend state.
1990 *
1991 * We can check that via USB Link State bits in DSTS register.
1992 */
1993 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1994
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 link_state = DWC3_DSTS_USBLNKST(reg);
1996
1997 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001998 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001999 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
2000 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyenc560e762021-04-19 19:11:12 -07002001 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
2002 case DWC3_LINK_STATE_U1:
Thinh Nguyend0550cd2020-01-31 16:25:50 -08002003 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03002004 break;
2005 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002006 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002007 }
2008
Felipe Balbi8598bde2012-01-02 18:55:57 +02002009 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2010 if (ret < 0) {
2011 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002012 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02002013 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002014
Paul Zimmerman802fde92012-04-27 13:10:52 +03002015 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002016 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002017 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03002018 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002019 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2020 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2021 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002022
Paul Zimmerman1d046792012-02-15 18:56:56 -08002023 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002024 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03002025
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002026 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002027 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2028
2029 /* in HS, means ON */
2030 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2031 break;
2032 }
2033
2034 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2035 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002036 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002037 }
2038
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002039 return 0;
2040}
2041
2042static int dwc3_gadget_wakeup(struct usb_gadget *g)
2043{
2044 struct dwc3 *dwc = gadget_to_dwc(g);
2045 unsigned long flags;
2046 int ret;
2047
2048 spin_lock_irqsave(&dwc->lock, flags);
2049 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002050 spin_unlock_irqrestore(&dwc->lock, flags);
2051
2052 return ret;
2053}
2054
2055static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2056 int is_selfpowered)
2057{
2058 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002059 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002060
Paul Zimmerman249a4562012-02-24 17:32:16 -08002061 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002062 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002063 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002064
2065 return 0;
2066}
2067
Wesley Chengae7e8612020-09-28 17:20:59 -07002068static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2069{
2070 u32 epnum;
2071
2072 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2073 struct dwc3_ep *dep;
2074
2075 dep = dwc->eps[epnum];
2076 if (!dep)
2077 continue;
2078
2079 dwc3_remove_requests(dwc, dep);
2080 }
2081}
2082
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002083static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2084{
2085 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2086 u32 reg;
2087
2088 if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2089 ssp_rate = dwc->max_ssp_rate;
2090
2091 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2092 reg &= ~DWC3_DCFG_SPEED_MASK;
2093 reg &= ~DWC3_DCFG_NUMLANES(~0);
2094
2095 if (ssp_rate == USB_SSP_GEN_1x2)
2096 reg |= DWC3_DCFG_SUPERSPEED;
2097 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2098 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2099
2100 if (ssp_rate != USB_SSP_GEN_2x1 &&
2101 dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2102 reg |= DWC3_DCFG_NUMLANES(1);
2103
2104 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2105}
2106
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002107static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2108{
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002109 enum usb_device_speed speed;
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002110 u32 reg;
2111
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002112 speed = dwc->gadget_max_speed;
Thinh Nguyen93f1d432021-03-08 18:16:50 -08002113 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002114 speed = dwc->maximum_speed;
2115
2116 if (speed == USB_SPEED_SUPER_PLUS &&
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002117 DWC3_IP_IS(DWC32)) {
2118 __dwc3_gadget_set_ssp_rate(dwc);
2119 return;
2120 }
2121
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002122 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2123 reg &= ~(DWC3_DCFG_SPEED_MASK);
2124
2125 /*
2126 * WORKAROUND: DWC3 revision < 2.20a have an issue
2127 * which would cause metastability state on Run/Stop
2128 * bit if we try to force the IP to USB2-only mode.
2129 *
2130 * Because of that, we cannot configure the IP to any
2131 * speed other than the SuperSpeed
2132 *
2133 * Refers to:
2134 *
2135 * STAR#9000525659: Clock Domain Crossing on DCTL in
2136 * USB 2.0 Mode
2137 */
2138 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2139 !dwc->dis_metastability_quirk) {
2140 reg |= DWC3_DCFG_SUPERSPEED;
2141 } else {
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002142 switch (speed) {
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002143 case USB_SPEED_FULL:
2144 reg |= DWC3_DCFG_FULLSPEED;
2145 break;
2146 case USB_SPEED_HIGH:
2147 reg |= DWC3_DCFG_HIGHSPEED;
2148 break;
2149 case USB_SPEED_SUPER:
2150 reg |= DWC3_DCFG_SUPERSPEED;
2151 break;
2152 case USB_SPEED_SUPER_PLUS:
2153 if (DWC3_IP_IS(DWC3))
2154 reg |= DWC3_DCFG_SUPERSPEED;
2155 else
2156 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2157 break;
2158 default:
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002159 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002160
2161 if (DWC3_IP_IS(DWC3))
2162 reg |= DWC3_DCFG_SUPERSPEED;
2163 else
2164 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2165 }
2166 }
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002167
2168 if (DWC3_IP_IS(DWC32) &&
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002169 speed > USB_SPEED_UNKNOWN &&
2170 speed < USB_SPEED_SUPER_PLUS)
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002171 reg &= ~DWC3_DCFG_NUMLANES(~0);
2172
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002173 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2174}
2175
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002176static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002177{
2178 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002179 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002180
Felipe Balbifc8bb912016-05-16 13:14:48 +03002181 if (pm_runtime_suspended(dwc->dev))
2182 return 0;
2183
Felipe Balbi72246da2011-08-19 18:10:58 +03002184 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002185 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002186 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002187 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2188 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2189 }
2190
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002191 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002192 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2193 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002194
2195 if (dwc->has_hibernation)
2196 reg |= DWC3_DCTL_KEEP_CONNECT;
2197
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002198 __dwc3_gadget_set_speed(dwc);
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002199 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002200 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002201 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002202
2203 if (dwc->has_hibernation && !suspend)
2204 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2205
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002206 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002207 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002208
Thinh Nguyen5b738212019-10-23 19:15:43 -07002209 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002210
2211 do {
2212 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002213 reg &= DWC3_DSTS_DEVCTRLHLT;
2214 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002215
2216 if (!timeout)
2217 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002218
Pratyush Anand6f17f742012-07-02 10:21:55 +05302219 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002220}
2221
Wesley Chengae7e8612020-09-28 17:20:59 -07002222static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2223static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chenga1383b32020-12-29 15:00:37 -08002224static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002225
Felipe Balbi72246da2011-08-19 18:10:58 +03002226static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2227{
2228 struct dwc3 *dwc = gadget_to_dwc(g);
2229 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302230 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002231
2232 is_on = !!is_on;
2233
Baolin Wangbb014732016-10-14 17:11:33 +08002234 /*
2235 * Per databook, when we want to stop the gadget, if a control transfer
2236 * is still in process, complete it and get the core into setup phase.
2237 */
2238 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2239 reinit_completion(&dwc->ep0_in_setup);
2240
2241 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2242 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2243 if (ret == 0) {
2244 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2245 return -ETIMEDOUT;
2246 }
2247 }
2248
Wesley Chengae7e8612020-09-28 17:20:59 -07002249 /*
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002250 * Check the return value for successful resume, or error. For a
2251 * successful resume, the DWC3 runtime PM resume routine will handle
2252 * the run stop sequence, so avoid duplicate operations here.
2253 */
2254 ret = pm_runtime_get_sync(dwc->dev);
2255 if (!ret || ret < 0) {
2256 pm_runtime_put(dwc->dev);
2257 return 0;
2258 }
2259
2260 /*
Wesley Chengae7e8612020-09-28 17:20:59 -07002261 * Synchronize any pending event handling before executing the controller
2262 * halt routine.
2263 */
2264 if (!is_on) {
2265 dwc3_gadget_disable_irq(dwc);
2266 synchronize_irq(dwc->irq_gadget);
2267 }
2268
Felipe Balbi72246da2011-08-19 18:10:58 +03002269 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002270
2271 if (!is_on) {
2272 u32 count;
2273
Wesley Chengf09ddcf2021-03-11 15:59:02 -08002274 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002275 /*
2276 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2277 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2278 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2279 * command for any active transfers" before clearing the RunStop
2280 * bit.
2281 */
2282 dwc3_stop_active_transfers(dwc);
2283 __dwc3_gadget_stop(dwc);
2284
2285 /*
2286 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2287 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2288 * "software needs to acknowledge the events that are generated
2289 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2290 * to be set to '1'."
2291 */
2292 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2293 count &= DWC3_GEVNTCOUNT_MASK;
2294 if (count > 0) {
2295 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2296 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2297 dwc->ev_buf->length;
2298 }
Wesley Chenga1383b32020-12-29 15:00:37 -08002299 } else {
2300 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002301 }
2302
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002303 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002304 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002305 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002306
Pratyush Anand6f17f742012-07-02 10:21:55 +05302307 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002308}
2309
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002310static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2311{
2312 u32 reg;
2313
2314 /* Enable all but Start and End of Frame IRQs */
Thinh Nguyen132ee0d2021-01-13 19:55:29 -08002315 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002316 DWC3_DEVTEN_CMDCMPLTEN |
2317 DWC3_DEVTEN_ERRTICERREN |
2318 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002319 DWC3_DEVTEN_CONNECTDONEEN |
2320 DWC3_DEVTEN_USBRSTEN |
2321 DWC3_DEVTEN_DISCONNEVTEN);
2322
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002323 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002324 reg |= DWC3_DEVTEN_ULSTCNGEN;
2325
Jack Phamd1d90dd2021-04-28 02:01:10 -07002326 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2327 if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
Jack Pham6f26ebb2021-04-28 02:01:11 -07002328 reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
Jack Phamd1d90dd2021-04-28 02:01:10 -07002329
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002330 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2331}
2332
2333static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2334{
2335 /* mask all interrupts */
2336 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2337}
2338
2339static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002340static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002341
Felipe Balbi4e994722016-05-13 14:09:59 +03002342/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002343 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2344 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002345 *
2346 * The following looks like complex but it's actually very simple. In order to
2347 * calculate the number of packets we can burst at once on OUT transfers, we're
2348 * gonna use RxFIFO size.
2349 *
2350 * To calculate RxFIFO size we need two numbers:
2351 * MDWIDTH = size, in bits, of the internal memory bus
2352 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2353 *
2354 * Given these two numbers, the formula is simple:
2355 *
2356 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2357 *
2358 * 24 bytes is for 3x SETUP packets
2359 * 16 bytes is a clock domain crossing tolerance
2360 *
2361 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2362 */
2363static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2364{
2365 u32 ram2_depth;
2366 u32 mdwidth;
2367 u32 nump;
2368 u32 reg;
2369
2370 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
Thinh Nguyend00be772021-03-27 17:54:01 -07002371 mdwidth = dwc3_mdwidth(dwc);
Felipe Balbi4e994722016-05-13 14:09:59 +03002372
2373 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2374 nump = min_t(u32, nump, 16);
2375
2376 /* update NumP */
2377 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2378 reg &= ~DWC3_DCFG_NUMP_MASK;
2379 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2380 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2381}
2382
Felipe Balbid7be2952016-05-04 15:49:37 +03002383static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002384{
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 int ret = 0;
2387 u32 reg;
2388
John Youncf40b862016-11-14 12:32:43 -08002389 /*
2390 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2391 * the core supports IMOD, disable it.
2392 */
2393 if (dwc->imod_interval) {
2394 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2395 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2396 } else if (dwc3_has_imod(dwc)) {
2397 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2398 }
2399
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002400 /*
2401 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2402 * field instead of letting dwc3 itself calculate that automatically.
2403 *
2404 * This way, we maximize the chances that we'll be able to get several
2405 * bursts of data without going through any sort of endpoint throttling.
2406 */
2407 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002408 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002409 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002410 else
2411 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002412
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002413 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2414
Felipe Balbi4e994722016-05-13 14:09:59 +03002415 dwc3_gadget_setup_nump(dwc);
2416
Thinh Nguyene66bbfb2021-04-12 20:00:45 -07002417 /*
2418 * Currently the controller handles single stream only. So, Ignore
2419 * Packet Pending bit for stream selection and don't search for another
2420 * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2421 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2422 * the stream performance.
2423 */
2424 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2425 reg |= DWC3_DCFG_IGNSTRMPP;
2426 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2427
Felipe Balbi72246da2011-08-19 18:10:58 +03002428 /* Start with SuperSpeed Default */
2429 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2430
2431 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002432 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002433 if (ret) {
2434 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002435 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002436 }
2437
2438 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002439 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002440 if (ret) {
2441 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002442 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002443 }
2444
2445 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002446 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002447 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002448 dwc3_ep0_out_start(dwc);
2449
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002450 dwc3_gadget_enable_irq(dwc);
2451
Felipe Balbid7be2952016-05-04 15:49:37 +03002452 return 0;
2453
2454err1:
2455 __dwc3_gadget_ep_disable(dwc->eps[0]);
2456
2457err0:
2458 return ret;
2459}
2460
2461static int dwc3_gadget_start(struct usb_gadget *g,
2462 struct usb_gadget_driver *driver)
2463{
2464 struct dwc3 *dwc = gadget_to_dwc(g);
2465 unsigned long flags;
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002466 int ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002467 int irq;
2468
Roger Quadros9522def2016-06-10 14:48:38 +03002469 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002470 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2471 IRQF_SHARED, "dwc3", dwc->ev_buf);
2472 if (ret) {
2473 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2474 irq, ret);
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002475 return ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002476 }
2477
2478 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002479 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 spin_unlock_irqrestore(&dwc->lock, flags);
2481
2482 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002483}
2484
Felipe Balbid7be2952016-05-04 15:49:37 +03002485static void __dwc3_gadget_stop(struct dwc3 *dwc)
2486{
2487 dwc3_gadget_disable_irq(dwc);
2488 __dwc3_gadget_ep_disable(dwc->eps[0]);
2489 __dwc3_gadget_ep_disable(dwc->eps[1]);
2490}
2491
Felipe Balbi22835b82014-10-17 12:05:12 -05002492static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002493{
2494 struct dwc3 *dwc = gadget_to_dwc(g);
2495 unsigned long flags;
2496
2497 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002498 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002499 spin_unlock_irqrestore(&dwc->lock, flags);
2500
Felipe Balbi3f308d12016-05-16 14:17:06 +03002501 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002502
Felipe Balbi72246da2011-08-19 18:10:58 +03002503 return 0;
2504}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002505
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302506static void dwc3_gadget_config_params(struct usb_gadget *g,
2507 struct usb_dcd_config_params *params)
2508{
2509 struct dwc3 *dwc = gadget_to_dwc(g);
2510
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002511 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2512 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2513
2514 /* Recommended BESL */
2515 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002516 /*
2517 * If the recommended BESL baseline is 0 or if the BESL deep is
2518 * less than 2, Microsoft's Windows 10 host usb stack will issue
2519 * a usb reset immediately after it receives the extended BOS
2520 * descriptor and the enumeration will fail. To maintain
2521 * compatibility with the Windows' usb stack, let's set the
2522 * recommended BESL baseline to 1 and clamp the BESL deep to be
2523 * within 2 to 15.
2524 */
2525 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002526 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002527 params->besl_deep =
2528 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002529 }
2530
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302531 /* U1 Device exit Latency */
2532 if (dwc->dis_u1_entry_quirk)
2533 params->bU1devExitLat = 0;
2534 else
2535 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2536
2537 /* U2 Device exit Latency */
2538 if (dwc->dis_u2_entry_quirk)
2539 params->bU2DevExitLat = 0;
2540 else
2541 params->bU2DevExitLat =
2542 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2543}
2544
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002545static void dwc3_gadget_set_speed(struct usb_gadget *g,
2546 enum usb_device_speed speed)
2547{
2548 struct dwc3 *dwc = gadget_to_dwc(g);
2549 unsigned long flags;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002550
2551 spin_lock_irqsave(&dwc->lock, flags);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002552 dwc->gadget_max_speed = speed;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002553 spin_unlock_irqrestore(&dwc->lock, flags);
2554}
2555
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002556static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
2557 enum usb_ssp_rate rate)
2558{
2559 struct dwc3 *dwc = gadget_to_dwc(g);
2560 unsigned long flags;
2561
2562 spin_lock_irqsave(&dwc->lock, flags);
Thinh Nguyencdb651b2021-03-08 18:16:44 -08002563 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002564 dwc->gadget_ssp_rate = rate;
2565 spin_unlock_irqrestore(&dwc->lock, flags);
2566}
2567
Wesley Cheng82c46b82020-12-29 15:03:29 -08002568static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2569{
2570 struct dwc3 *dwc = gadget_to_dwc(g);
Ray Chi99288de2021-02-22 19:51:49 +08002571 union power_supply_propval val = {0};
2572 int ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002573
2574 if (dwc->usb2_phy)
2575 return usb_phy_set_power(dwc->usb2_phy, mA);
2576
Ray Chi99288de2021-02-22 19:51:49 +08002577 if (!dwc->usb_psy)
2578 return -EOPNOTSUPP;
2579
Ray Chi8a5b5c32021-03-28 02:28:08 +08002580 val.intval = 1000 * mA;
Ray Chi99288de2021-02-22 19:51:49 +08002581 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
2582
2583 return ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002584}
2585
Felipe Balbi72246da2011-08-19 18:10:58 +03002586static const struct usb_gadget_ops dwc3_gadget_ops = {
2587 .get_frame = dwc3_gadget_get_frame,
2588 .wakeup = dwc3_gadget_wakeup,
2589 .set_selfpowered = dwc3_gadget_set_selfpowered,
2590 .pullup = dwc3_gadget_pullup,
2591 .udc_start = dwc3_gadget_start,
2592 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002593 .udc_set_speed = dwc3_gadget_set_speed,
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002594 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302595 .get_config_params = dwc3_gadget_config_params,
Wesley Cheng82c46b82020-12-29 15:03:29 -08002596 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002597};
2598
2599/* -------------------------------------------------------------------------- */
2600
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002601static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2602{
2603 struct dwc3 *dwc = dep->dwc;
2604
2605 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2606 dep->endpoint.maxburst = 1;
2607 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2608 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002609 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002610
2611 dep->endpoint.caps.type_control = true;
2612
2613 return 0;
2614}
2615
2616static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2617{
2618 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002619 u32 mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002620 int size;
2621
Thinh Nguyend00be772021-03-27 17:54:01 -07002622 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002623
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002624 /* MDWIDTH is represented in bits, we need it in bytes */
2625 mdwidth /= 8;
2626
2627 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002628 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002629 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002630 else
2631 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002632
2633 /* FIFO Depth is in MDWDITH bytes. Multiply */
2634 size *= mdwidth;
2635
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002636 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002637 * To meet performance requirement, a minimum TxFIFO size of 3x
2638 * MaxPacketSize is recommended for endpoints that support burst and a
2639 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2640 * support burst. Use those numbers and we can calculate the max packet
2641 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002642 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002643 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2644 size /= 3;
2645 else
2646 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002647
2648 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2649
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002650 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002651 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2652 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002653 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002654 dep->endpoint.caps.type_iso = true;
2655 dep->endpoint.caps.type_bulk = true;
2656 dep->endpoint.caps.type_int = true;
2657
2658 return dwc3_alloc_trb_pool(dep);
2659}
2660
2661static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2662{
2663 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002664 u32 mdwidth;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002665 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002666
Thinh Nguyend00be772021-03-27 17:54:01 -07002667 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002668
2669 /* MDWIDTH is represented in bits, convert to bytes */
2670 mdwidth /= 8;
2671
2672 /* All OUT endpoints share a single RxFIFO space */
2673 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002674 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002675 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002676 else
2677 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002678
2679 /* FIFO depth is in MDWDITH bytes */
2680 size *= mdwidth;
2681
2682 /*
2683 * To meet performance requirement, a minimum recommended RxFIFO size
2684 * is defined as follow:
2685 * RxFIFO size >= (3 x MaxPacketSize) +
2686 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2687 *
2688 * Then calculate the max packet limit as below.
2689 */
2690 size -= (3 * 8) + 16;
2691 if (size < 0)
2692 size = 0;
2693 else
2694 size /= 3;
2695
2696 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002697 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002698 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2699 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002700 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002701 dep->endpoint.caps.type_iso = true;
2702 dep->endpoint.caps.type_bulk = true;
2703 dep->endpoint.caps.type_int = true;
2704
2705 return dwc3_alloc_trb_pool(dep);
2706}
2707
2708static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002709{
2710 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002711 bool direction = epnum & 1;
2712 int ret;
2713 u8 num = epnum >> 1;
2714
2715 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2716 if (!dep)
2717 return -ENOMEM;
2718
2719 dep->dwc = dwc;
2720 dep->number = epnum;
2721 dep->direction = direction;
2722 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2723 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002724 dep->combo_num = 0;
2725 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002726
2727 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2728 direction ? "in" : "out");
2729
2730 dep->endpoint.name = dep->name;
2731
2732 if (!(dep->number > 1)) {
2733 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2734 dep->endpoint.comp_desc = NULL;
2735 }
2736
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002737 if (num == 0)
2738 ret = dwc3_gadget_init_control_endpoint(dep);
2739 else if (direction)
2740 ret = dwc3_gadget_init_in_endpoint(dep);
2741 else
2742 ret = dwc3_gadget_init_out_endpoint(dep);
2743
2744 if (ret)
2745 return ret;
2746
2747 dep->endpoint.caps.dir_in = direction;
2748 dep->endpoint.caps.dir_out = !direction;
2749
2750 INIT_LIST_HEAD(&dep->pending_list);
2751 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002752 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002753
2754 return 0;
2755}
2756
2757static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2758{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002759 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002760
Peter Chene81a7012020-08-21 10:55:48 +08002761 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002762
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002763 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002764 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002765
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002766 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2767 if (ret)
2768 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002769 }
2770
2771 return 0;
2772}
2773
2774static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2775{
2776 struct dwc3_ep *dep;
2777 u8 epnum;
2778
2779 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2780 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002781 if (!dep)
2782 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302783 /*
2784 * Physical endpoints 0 and 1 are special; they form the
2785 * bi-directional USB endpoint 0.
2786 *
2787 * For those two physical endpoints, we don't allocate a TRB
2788 * pool nor do we add them the endpoints list. Due to that, we
2789 * shouldn't do these two operations otherwise we would end up
2790 * with all sorts of bugs when removing dwc3.ko.
2791 */
2792 if (epnum != 0 && epnum != 1) {
2793 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002794 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302795 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002796
2797 kfree(dep);
2798 }
2799}
2800
Felipe Balbi72246da2011-08-19 18:10:58 +03002801/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002802
Felipe Balbi8f608e82018-03-27 10:53:29 +03002803static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2804 struct dwc3_request *req, struct dwc3_trb *trb,
2805 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302806{
2807 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302808
Felipe Balbidc55c672016-08-12 13:20:32 +03002809 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002810
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002811 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002812 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002813
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002814 /*
2815 * If we're in the middle of series of chained TRBs and we
2816 * receive a short transfer along the way, DWC3 will skip
2817 * through all TRBs including the last TRB in the chain (the
2818 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2819 * bit and SW has to do it manually.
2820 *
2821 * We're going to do that here to avoid problems of HW trying
2822 * to use bogus TRBs for transfers.
2823 */
2824 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2825 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2826
Felipe Balbic6267a52017-01-05 14:58:46 +02002827 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002828 * For isochronous transfers, the first TRB in a service interval must
2829 * have the Isoc-First type. Track and report its interval frame number.
2830 */
2831 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2832 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2833 unsigned int frame_number;
2834
2835 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2836 frame_number &= ~(dep->interval - 1);
2837 req->request.frame_number = frame_number;
2838 }
2839
2840 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002841 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2842 * this TRB points to the bounce buffer address, it's a MPS alignment
2843 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002844 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002845 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2846 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002847 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2848 return 1;
2849 }
2850
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302851 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002852 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302853
Felipe Balbi35b27192017-03-08 13:56:37 +02002854 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2855 return 1;
2856
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002857 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302858 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002859
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002860 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2861 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302862 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002863
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302864 return 0;
2865}
2866
Felipe Balbid3692952018-03-29 13:32:10 +03002867static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2868 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2869 int status)
2870{
2871 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2872 struct scatterlist *sg = req->sg;
2873 struct scatterlist *s;
2874 unsigned int pending = req->num_pending_sgs;
2875 unsigned int i;
2876 int ret = 0;
2877
2878 for_each_sg(sg, s, pending, i) {
2879 trb = &dep->trb_pool[dep->trb_dequeue];
2880
Felipe Balbid3692952018-03-29 13:32:10 +03002881 req->sg = sg_next(s);
2882 req->num_pending_sgs--;
2883
2884 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2885 trb, event, status, true);
2886 if (ret)
2887 break;
2888 }
2889
2890 return ret;
2891}
2892
2893static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2894 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2895 int status)
2896{
2897 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2898
2899 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2900 event, status, false);
2901}
2902
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002903static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2904{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002905 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002906}
2907
Felipe Balbif38e35d2018-04-06 15:56:35 +03002908static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2909 const struct dwc3_event_depevt *event,
2910 struct dwc3_request *req, int status)
2911{
2912 int ret;
2913
2914 if (req->num_pending_sgs)
2915 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2916 status);
2917 else
2918 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2919 status);
2920
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002921 req->request.actual = req->request.length - req->remaining;
2922
2923 if (!dwc3_gadget_ep_request_completed(req))
2924 goto out;
2925
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002926 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002927 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2928 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002929 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002930 }
2931
Felipe Balbif38e35d2018-04-06 15:56:35 +03002932 dwc3_gadget_giveback(dep, req, status);
2933
2934out:
2935 return ret;
2936}
2937
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002938static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002939 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002940{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002941 struct dwc3_request *req;
2942 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002943
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002944 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002945 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002946
Felipe Balbif38e35d2018-04-06 15:56:35 +03002947 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2948 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002949 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002950 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002951 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002952}
2953
Thinh Nguyend9feef92020-03-31 01:40:42 -07002954static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2955{
2956 struct dwc3_request *req;
Wesley Cheng02fa4b92021-03-19 02:31:24 -07002957 struct dwc3 *dwc = dep->dwc;
2958
2959 if (!dep->endpoint.desc || !dwc->pullups_connected ||
2960 !dwc->connected)
2961 return false;
Thinh Nguyend9feef92020-03-31 01:40:42 -07002962
2963 if (!list_empty(&dep->pending_list))
2964 return true;
2965
2966 /*
2967 * We only need to check the first entry of the started list. We can
2968 * assume the completed requests are removed from the started list.
2969 */
2970 req = next_request(&dep->started_list);
2971 if (!req)
2972 return false;
2973
2974 return !dwc3_gadget_ep_request_completed(req);
2975}
2976
Felipe Balbiee3638b2018-03-27 11:26:53 +03002977static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2978 const struct dwc3_event_depevt *event)
2979{
Felipe Balbif62afb42018-04-11 10:34:34 +03002980 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002981}
2982
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002983static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2984 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002985{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002986 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002987 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002988
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002989 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002990
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002991 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2992 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002993
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002994 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2995 list_empty(&dep->started_list) &&
2996 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002997 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002998 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002999 if (__dwc3_gadget_kick_transfer(dep) == 0)
3000 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03003001
Thinh Nguyenb6842d42020-05-05 19:46:33 -07003002out:
Felipe Balbifae2b902011-10-14 13:00:30 +03003003 /*
3004 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3005 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3006 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003007 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003008 u32 reg;
3009 int i;
3010
3011 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05003012 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03003013
3014 if (!(dep->flags & DWC3_EP_ENABLED))
3015 continue;
3016
Felipe Balbiaa3342c2016-03-14 11:01:31 +02003017 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003018 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03003019 }
3020
3021 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3022 reg |= dwc->u1u2;
3023 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3024
3025 dwc->u1u2 = 0;
3026 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003027
3028 return no_started_trb;
3029}
3030
3031static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3032 const struct dwc3_event_depevt *event)
3033{
3034 int status = 0;
3035
3036 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3037 dwc3_gadget_endpoint_frame_from_event(dep, event);
3038
3039 if (event->status & DEPEVT_STATUS_BUSERR)
3040 status = -ECONNRESET;
3041
3042 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3043 status = -EXDEV;
3044
3045 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03003046}
3047
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003048static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3049 const struct dwc3_event_depevt *event)
3050{
3051 int status = 0;
3052
3053 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3054
3055 if (event->status & DEPEVT_STATUS_BUSERR)
3056 status = -ECONNRESET;
3057
Thinh Nguyene0d19562020-05-05 19:46:57 -07003058 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3059 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03003060}
3061
Felipe Balbi8f608e82018-03-27 10:53:29 +03003062static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3063 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03003064{
Felipe Balbiee3638b2018-03-27 11:26:53 +03003065 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07003066
3067 /*
3068 * The XferNotReady event is generated only once before the endpoint
3069 * starts. It will be generated again when END_TRANSFER command is
3070 * issued. For some controller versions, the XferNotReady event may be
3071 * generated while the END_TRANSFER command is still in process. Ignore
3072 * it and wait for the next XferNotReady event after the command is
3073 * completed.
3074 */
3075 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3076 return;
3077
Felipe Balbi25abad62018-08-14 10:41:19 +03003078 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03003079}
3080
Thinh Nguyen8266b082020-07-30 16:29:03 -07003081static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3082 const struct dwc3_event_depevt *event)
3083{
3084 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3085
3086 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3087 return;
3088
3089 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3090 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3091 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3092
3093 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3094 struct dwc3 *dwc = dep->dwc;
3095
3096 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3097 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3098 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3099
3100 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3101 if (dwc->delayed_status)
3102 __dwc3_gadget_ep0_set_halt(ep0, 1);
3103 return;
3104 }
3105
3106 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3107 if (dwc->delayed_status)
3108 dwc3_ep0_send_delayed_status(dwc);
3109 }
3110
3111 if ((dep->flags & DWC3_EP_DELAY_START) &&
3112 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3113 __dwc3_gadget_kick_transfer(dep);
3114
3115 dep->flags &= ~DWC3_EP_DELAY_START;
3116}
3117
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003118static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3119 const struct dwc3_event_depevt *event)
3120{
3121 struct dwc3 *dwc = dep->dwc;
3122
3123 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3124 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3125 goto out;
3126 }
3127
3128 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3129 switch (event->parameters) {
3130 case DEPEVT_STREAM_PRIME:
3131 /*
3132 * If the host can properly transition the endpoint state from
3133 * idle to prime after a NoStream rejection, there's no need to
3134 * force restarting the endpoint to reinitiate the stream. To
3135 * simplify the check, assume the host follows the USB spec if
3136 * it primed the endpoint more than once.
3137 */
3138 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3139 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3140 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3141 else
3142 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3143 }
3144
3145 break;
3146 case DEPEVT_STREAM_NOSTREAM:
3147 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3148 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3149 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3150 break;
3151
3152 /*
3153 * If the host rejects a stream due to no active stream, by the
3154 * USB and xHCI spec, the endpoint will be put back to idle
3155 * state. When the host is ready (buffer added/updated), it will
3156 * prime the endpoint to inform the usb device controller. This
3157 * triggers the device controller to issue ERDY to restart the
3158 * stream. However, some hosts don't follow this and keep the
3159 * endpoint in the idle state. No prime will come despite host
3160 * streams are updated, and the device controller will not be
3161 * triggered to generate ERDY to move the next stream data. To
3162 * workaround this and maintain compatibility with various
3163 * hosts, force to reinitate the stream until the host is ready
3164 * instead of waiting for the host to prime the endpoint.
3165 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003166 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3167 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3168
3169 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3170 } else {
3171 dep->flags |= DWC3_EP_DELAY_START;
3172 dwc3_stop_active_transfer(dep, true, true);
3173 return;
3174 }
3175 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003176 }
3177
3178out:
3179 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3180}
3181
Felipe Balbi72246da2011-08-19 18:10:58 +03003182static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3183 const struct dwc3_event_depevt *event)
3184{
3185 struct dwc3_ep *dep;
3186 u8 epnum = event->endpoint_number;
3187
3188 dep = dwc->eps[epnum];
3189
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003190 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003191 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003192 return;
3193
3194 /* Handle only EPCMDCMPLT when EP disabled */
3195 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3196 return;
3197 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003198
Felipe Balbi72246da2011-08-19 18:10:58 +03003199 if (epnum == 0 || epnum == 1) {
3200 dwc3_ep0_interrupt(dwc, event);
3201 return;
3202 }
3203
3204 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003205 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003206 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 break;
3208 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003209 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003210 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003211 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003212 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003213 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003214 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003215 dwc3_gadget_endpoint_transfer_complete(dep, event);
3216 break;
3217 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003218 dwc3_gadget_endpoint_stream_event(dep, event);
3219 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003220 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003221 break;
3222 }
3223}
3224
3225static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3226{
3227 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3228 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003229 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003230 spin_lock(&dwc->lock);
3231 }
3232}
3233
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003234static void dwc3_suspend_gadget(struct dwc3 *dwc)
3235{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003236 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003237 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003238 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003239 spin_lock(&dwc->lock);
3240 }
3241}
3242
3243static void dwc3_resume_gadget(struct dwc3 *dwc)
3244{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003245 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003246 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003247 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003248 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003249 }
3250}
3251
3252static void dwc3_reset_gadget(struct dwc3 *dwc)
3253{
3254 if (!dwc->gadget_driver)
3255 return;
3256
Peter Chene81a7012020-08-21 10:55:48 +08003257 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003258 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003259 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003260 spin_lock(&dwc->lock);
3261 }
3262}
3263
Felipe Balbic5353b22019-02-13 13:00:54 +02003264static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3265 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003266{
Felipe Balbi72246da2011-08-19 18:10:58 +03003267 struct dwc3_gadget_ep_cmd_params params;
3268 u32 cmd;
3269 int ret;
3270
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003271 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3272 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303273 return;
3274
Pratyush Anand57911502012-07-06 15:19:10 +05303275 /*
3276 * NOTICE: We are violating what the Databook says about the
3277 * EndTransfer command. Ideally we would _always_ wait for the
3278 * EndTransfer Command Completion IRQ, but that's causing too
3279 * much trouble synchronizing between us and gadget driver.
3280 *
3281 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003282 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303283 *
3284 * Note also that a similar handling was tested by Synopsys
3285 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003286 * In short, what we're doing is issuing EndTransfer with
3287 * CMDIOC bit set and delay kicking transfer until the
3288 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003289 *
3290 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3291 * supports a mode to work around the above limitation. The
3292 * software can poll the CMDACT bit in the DEPCMD register
3293 * after issuing a EndTransfer command. This mode is enabled
3294 * by writing GUCTL2[14]. This polling is already done in the
3295 * dwc3_send_gadget_ep_cmd() function so if the mode is
3296 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003297 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003298 *
3299 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303300 */
3301
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303302 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003303 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003304 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003305 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303306 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003307 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303308 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003309 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003310
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003311 /*
3312 * The END_TRANSFER command will cause the controller to generate a
3313 * NoStream Event, and it's not due to the host DP NoStream rejection.
3314 * Ignore the next NoStream event.
3315 */
3316 if (dep->stream_capable)
3317 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3318
Thinh Nguyend3abda52019-11-27 13:10:47 -08003319 if (!interrupt)
3320 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003321 else
3322 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003323}
3324
Felipe Balbi72246da2011-08-19 18:10:58 +03003325static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3326{
3327 u32 epnum;
3328
3329 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3330 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003331 int ret;
3332
3333 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003334 if (!dep)
3335 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003336
3337 if (!(dep->flags & DWC3_EP_STALL))
3338 continue;
3339
3340 dep->flags &= ~DWC3_EP_STALL;
3341
John Youn50c763f2016-05-31 17:49:56 -07003342 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003343 WARN_ON_ONCE(ret);
3344 }
3345}
3346
3347static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3348{
Felipe Balbic4430a22012-05-24 10:30:01 +03003349 int reg;
3350
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003351 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3352
Felipe Balbi72246da2011-08-19 18:10:58 +03003353 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3354 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003355 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003356 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003357
Felipe Balbi72246da2011-08-19 18:10:58 +03003358 dwc3_disconnect_gadget(dwc);
3359
Peter Chene81a7012020-08-21 10:55:48 +08003360 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003361 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003362 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003363
3364 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003365}
3366
Felipe Balbi72246da2011-08-19 18:10:58 +03003367static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3368{
3369 u32 reg;
3370
Felipe Balbidf62df52011-10-14 15:11:49 +03003371 /*
Wesley Cheng71ca43f2021-03-19 02:31:25 -07003372 * Ideally, dwc3_reset_gadget() would trigger the function
3373 * drivers to stop any active transfers through ep disable.
3374 * However, for functions which defer ep disable, such as mass
3375 * storage, we will need to rely on the call to stop active
3376 * transfers here, and avoid allowing of request queuing.
3377 */
3378 dwc->connected = false;
3379
3380 /*
Felipe Balbidf62df52011-10-14 15:11:49 +03003381 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3382 * would cause a missing Disconnect Event if there's a
3383 * pending Setup Packet in the FIFO.
3384 *
3385 * There's no suggested workaround on the official Bug
3386 * report, which states that "unless the driver/application
3387 * is doing any special handling of a disconnect event,
3388 * there is no functional issue".
3389 *
3390 * Unfortunately, it turns out that we _do_ some special
3391 * handling of a disconnect event, namely complete all
3392 * pending transfers, notify gadget driver of the
3393 * disconnection, and so on.
3394 *
3395 * Our suggested workaround is to follow the Disconnect
3396 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003397 * flag. Such flag gets set whenever we have a SETUP_PENDING
3398 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003399 * same endpoint.
3400 *
3401 * Refers to:
3402 *
3403 * STAR#9000466709: RTL: Device : Disconnect event not
3404 * generated if setup packet pending in FIFO
3405 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003406 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003407 if (dwc->setup_packet_pending)
3408 dwc3_gadget_disconnect_interrupt(dwc);
3409 }
3410
Felipe Balbi8e744752014-11-06 14:27:53 +08003411 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003412 /*
3413 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3414 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3415 * needs to ensure that it sends "a DEPENDXFER command for any active
3416 * transfers."
3417 */
3418 dwc3_stop_active_transfers(dwc);
Wesley Chengf09ddcf2021-03-11 15:59:02 -08003419 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003420
3421 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3422 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003423 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003424 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003425 dwc3_clear_stall_all_ep(dwc);
3426
3427 /* Reset device address to zero */
3428 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3429 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3430 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003431}
3432
Felipe Balbi72246da2011-08-19 18:10:58 +03003433static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3434{
Felipe Balbi72246da2011-08-19 18:10:58 +03003435 struct dwc3_ep *dep;
3436 int ret;
3437 u32 reg;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003438 u8 lanes = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003439 u8 speed;
3440
Felipe Balbi72246da2011-08-19 18:10:58 +03003441 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3442 speed = reg & DWC3_DSTS_CONNECTSPD;
3443 dwc->speed = speed;
3444
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003445 if (DWC3_IP_IS(DWC32))
3446 lanes = DWC3_DSTS_CONNLANES(reg) + 1;
3447
3448 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
3449
John Youn5fb6fda2016-11-10 17:23:25 -08003450 /*
3451 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3452 * each time on Connect Done.
3453 *
3454 * Currently we always use the reset value. If any platform
3455 * wants to set this to a different value, we need to add a
3456 * setting and update GCTL.RAMCLKSEL here.
3457 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003458
3459 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003460 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003461 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003462 dwc->gadget->ep0->maxpacket = 512;
3463 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003464
3465 if (lanes > 1)
3466 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
3467 else
3468 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
John Youn75808622016-02-05 17:09:13 -08003469 break;
John Youn2da9ad72016-05-20 16:34:26 -07003470 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003471 /*
3472 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3473 * would cause a missing USB3 Reset event.
3474 *
3475 * In such situations, we should force a USB3 Reset
3476 * event by calling our dwc3_gadget_reset_interrupt()
3477 * routine.
3478 *
3479 * Refers to:
3480 *
3481 * STAR#9000483510: RTL: SS : USB3 reset event may
3482 * not be generated always when the link enters poll
3483 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003484 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003485 dwc3_gadget_reset_interrupt(dwc);
3486
Felipe Balbi72246da2011-08-19 18:10:58 +03003487 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003488 dwc->gadget->ep0->maxpacket = 512;
3489 dwc->gadget->speed = USB_SPEED_SUPER;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003490
3491 if (lanes > 1) {
3492 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3493 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
3494 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003495 break;
John Youn2da9ad72016-05-20 16:34:26 -07003496 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003497 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003498 dwc->gadget->ep0->maxpacket = 64;
3499 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003500 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003501 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003502 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003503 dwc->gadget->ep0->maxpacket = 64;
3504 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003505 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003506 }
3507
Peter Chene81a7012020-08-21 10:55:48 +08003508 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003509
Pratyush Anand2b758352013-01-14 15:59:31 +05303510 /* Enable USB2 LPM Capability */
3511
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003512 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003513 !dwc->usb2_gadget_lpm_disable &&
John Youn2da9ad72016-05-20 16:34:26 -07003514 (speed != DWC3_DSTS_SUPERSPEED) &&
3515 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303516 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3517 reg |= DWC3_DCFG_LPM_CAP;
3518 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3519
3520 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3521 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3522
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003523 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3524 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303525
Huang Rui80caf7d2014-10-28 19:54:26 +08003526 /*
3527 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3528 * DCFG.LPMCap is set, core responses with an ACK and the
3529 * BESL value in the LPM token is less than or equal to LPM
3530 * NYET threshold.
3531 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003532 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003533 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003534
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003535 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003536 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003537
Thinh Nguyen5b738212019-10-23 19:15:43 -07003538 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003539 } else {
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003540 if (dwc->usb2_gadget_lpm_disable) {
3541 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3542 reg &= ~DWC3_DCFG_LPM_CAP;
3543 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3544 }
3545
Felipe Balbi356363b2013-12-19 16:37:05 -06003546 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3547 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003548 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303549 }
3550
Felipe Balbi72246da2011-08-19 18:10:58 +03003551 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003552 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003553 if (ret) {
3554 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3555 return;
3556 }
3557
3558 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003559 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003560 if (ret) {
3561 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3562 return;
3563 }
3564
3565 /*
3566 * Configure PHY via GUSB3PIPECTLn if required.
3567 *
3568 * Update GTXFIFOSIZn
3569 *
3570 * In both cases reset values should be sufficient.
3571 */
3572}
3573
3574static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3575{
Felipe Balbi72246da2011-08-19 18:10:58 +03003576 /*
3577 * TODO take core out of low power mode when that's
3578 * implemented.
3579 */
3580
Jiebing Liad14d4e2014-12-11 13:26:29 +08003581 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3582 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003583 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003584 spin_lock(&dwc->lock);
3585 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003586}
3587
3588static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3589 unsigned int evtinfo)
3590{
Felipe Balbifae2b902011-10-14 13:00:30 +03003591 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003592 unsigned int pwropt;
3593
3594 /*
3595 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3596 * Hibernation mode enabled which would show up when device detects
3597 * host-initiated U3 exit.
3598 *
3599 * In that case, device will generate a Link State Change Interrupt
3600 * from U3 to RESUME which is only necessary if Hibernation is
3601 * configured in.
3602 *
3603 * There are no functional changes due to such spurious event and we
3604 * just need to ignore it.
3605 *
3606 * Refers to:
3607 *
3608 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3609 * operational mode
3610 */
3611 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003612 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003613 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3614 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3615 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003616 return;
3617 }
3618 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003619
3620 /*
3621 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3622 * on the link partner, the USB session might do multiple entry/exit
3623 * of low power states before a transfer takes place.
3624 *
3625 * Due to this problem, we might experience lower throughput. The
3626 * suggested workaround is to disable DCTL[12:9] bits if we're
3627 * transitioning from U1/U2 to U0 and enable those bits again
3628 * after a transfer completes and there are no pending transfers
3629 * on any of the enabled endpoints.
3630 *
3631 * This is the first half of that workaround.
3632 *
3633 * Refers to:
3634 *
3635 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3636 * core send LGO_Ux entering U0
3637 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003638 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003639 if (next == DWC3_LINK_STATE_U0) {
3640 u32 u1u2;
3641 u32 reg;
3642
3643 switch (dwc->link_state) {
3644 case DWC3_LINK_STATE_U1:
3645 case DWC3_LINK_STATE_U2:
3646 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3647 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3648 | DWC3_DCTL_ACCEPTU2ENA
3649 | DWC3_DCTL_INITU1ENA
3650 | DWC3_DCTL_ACCEPTU1ENA);
3651
3652 if (!dwc->u1u2)
3653 dwc->u1u2 = reg & u1u2;
3654
3655 reg &= ~u1u2;
3656
Thinh Nguyen5b738212019-10-23 19:15:43 -07003657 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003658 break;
3659 default:
3660 /* do nothing */
3661 break;
3662 }
3663 }
3664 }
3665
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003666 switch (next) {
3667 case DWC3_LINK_STATE_U1:
3668 if (dwc->speed == USB_SPEED_SUPER)
3669 dwc3_suspend_gadget(dwc);
3670 break;
3671 case DWC3_LINK_STATE_U2:
3672 case DWC3_LINK_STATE_U3:
3673 dwc3_suspend_gadget(dwc);
3674 break;
3675 case DWC3_LINK_STATE_RESUME:
3676 dwc3_resume_gadget(dwc);
3677 break;
3678 default:
3679 /* do nothing */
3680 break;
3681 }
3682
Felipe Balbie57ebc12014-04-22 13:20:12 -05003683 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003684}
3685
Baolin Wang72704f82016-05-16 16:43:53 +08003686static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3687 unsigned int evtinfo)
3688{
3689 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3690
3691 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3692 dwc3_suspend_gadget(dwc);
3693
3694 dwc->link_state = next;
3695}
3696
Felipe Balbie1dadd32014-02-25 14:47:54 -06003697static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3698 unsigned int evtinfo)
3699{
3700 unsigned int is_ss = evtinfo & BIT(4);
3701
Felipe Balbibfad65e2017-04-19 14:59:27 +03003702 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003703 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3704 * have a known issue which can cause USB CV TD.9.23 to fail
3705 * randomly.
3706 *
3707 * Because of this issue, core could generate bogus hibernation
3708 * events which SW needs to ignore.
3709 *
3710 * Refers to:
3711 *
3712 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3713 * Device Fallback from SuperSpeed
3714 */
3715 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3716 return;
3717
3718 /* enter hibernation here */
3719}
3720
Felipe Balbi72246da2011-08-19 18:10:58 +03003721static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3722 const struct dwc3_event_devt *event)
3723{
3724 switch (event->type) {
3725 case DWC3_DEVICE_EVENT_DISCONNECT:
3726 dwc3_gadget_disconnect_interrupt(dwc);
3727 break;
3728 case DWC3_DEVICE_EVENT_RESET:
3729 dwc3_gadget_reset_interrupt(dwc);
3730 break;
3731 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3732 dwc3_gadget_conndone_interrupt(dwc);
3733 break;
3734 case DWC3_DEVICE_EVENT_WAKEUP:
3735 dwc3_gadget_wakeup_interrupt(dwc);
3736 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003737 case DWC3_DEVICE_EVENT_HIBER_REQ:
3738 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3739 "unexpected hibernation event\n"))
3740 break;
3741
3742 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3743 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003744 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3745 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3746 break;
Jack Pham6f26ebb2021-04-28 02:01:11 -07003747 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003748 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003749 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003750 /*
3751 * Ignore suspend event until the gadget enters into
3752 * USB_STATE_CONFIGURED state.
3753 */
Peter Chene81a7012020-08-21 10:55:48 +08003754 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003755 dwc3_gadget_suspend_interrupt(dwc,
3756 event->event_info);
3757 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003758 break;
3759 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003760 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003761 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003762 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003763 break;
3764 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003765 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003766 }
3767}
3768
3769static void dwc3_process_event_entry(struct dwc3 *dwc,
3770 const union dwc3_event *event)
3771{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003772 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003773
Felipe Balbidfc5e802017-04-26 13:44:51 +03003774 if (!event->type.is_devspec)
3775 dwc3_endpoint_interrupt(dwc, &event->depevt);
3776 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003777 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003778 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003779 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003780}
3781
Felipe Balbidea520a2016-03-30 09:39:34 +03003782static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003783{
Felipe Balbidea520a2016-03-30 09:39:34 +03003784 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003785 irqreturn_t ret = IRQ_NONE;
3786 int left;
3787 u32 reg;
3788
Felipe Balbif42f2442013-06-12 21:25:08 +03003789 left = evt->count;
3790
3791 if (!(evt->flags & DWC3_EVENT_PENDING))
3792 return IRQ_NONE;
3793
3794 while (left > 0) {
3795 union dwc3_event event;
3796
John Younebbb2d52016-11-15 13:07:02 +02003797 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003798
3799 dwc3_process_event_entry(dwc, &event);
3800
3801 /*
3802 * FIXME we wrap around correctly to the next entry as
3803 * almost all entries are 4 bytes in size. There is one
3804 * entry which has 12 bytes which is a regular entry
3805 * followed by 8 bytes data. ATM I don't know how
3806 * things are organized if we get next to the a
3807 * boundary so I worry about that once we try to handle
3808 * that.
3809 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003810 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003811 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003812 }
3813
3814 evt->count = 0;
3815 evt->flags &= ~DWC3_EVENT_PENDING;
3816 ret = IRQ_HANDLED;
3817
3818 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003819 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003820 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003821 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003822
John Youncf40b862016-11-14 12:32:43 -08003823 if (dwc->imod_interval) {
3824 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3825 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3826 }
3827
Felipe Balbif42f2442013-06-12 21:25:08 +03003828 return ret;
3829}
3830
Felipe Balbidea520a2016-03-30 09:39:34 +03003831static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003832{
Felipe Balbidea520a2016-03-30 09:39:34 +03003833 struct dwc3_event_buffer *evt = _evt;
3834 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003835 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003836 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003837
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003838 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003839 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003840 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003841
3842 return ret;
3843}
3844
Felipe Balbidea520a2016-03-30 09:39:34 +03003845static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003846{
Felipe Balbidea520a2016-03-30 09:39:34 +03003847 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003848 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003849 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003850 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003851
Felipe Balbifc8bb912016-05-16 13:14:48 +03003852 if (pm_runtime_suspended(dwc->dev)) {
3853 pm_runtime_get(dwc->dev);
3854 disable_irq_nosync(dwc->irq_gadget);
3855 dwc->pending_events = true;
3856 return IRQ_HANDLED;
3857 }
3858
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003859 /*
3860 * With PCIe legacy interrupt, test shows that top-half irq handler can
3861 * be called again after HW interrupt deassertion. Check if bottom-half
3862 * irq event handler completes before caching new event to prevent
3863 * losing events.
3864 */
3865 if (evt->flags & DWC3_EVENT_PENDING)
3866 return IRQ_HANDLED;
3867
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003868 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003869 count &= DWC3_GEVNTCOUNT_MASK;
3870 if (!count)
3871 return IRQ_NONE;
3872
Felipe Balbib15a7622011-06-30 16:57:15 +03003873 evt->count = count;
3874 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003875
Felipe Balbie8adfc32013-06-12 21:11:14 +03003876 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003877 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003878 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003879 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003880
John Younebbb2d52016-11-15 13:07:02 +02003881 amount = min(count, evt->length - evt->lpos);
3882 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3883
3884 if (amount < count)
3885 memcpy(evt->cache, evt->buf, count - amount);
3886
John Youn65aca322016-11-15 13:08:59 +02003887 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3888
Felipe Balbib15a7622011-06-30 16:57:15 +03003889 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003890}
3891
Felipe Balbidea520a2016-03-30 09:39:34 +03003892static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003893{
Felipe Balbidea520a2016-03-30 09:39:34 +03003894 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003895
Felipe Balbidea520a2016-03-30 09:39:34 +03003896 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003897}
3898
Felipe Balbi6db38122016-10-03 11:27:01 +03003899static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3900{
3901 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3902 int irq;
3903
Hans de Goedef146b40b2019-10-05 23:04:48 +02003904 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003905 if (irq > 0)
3906 goto out;
3907
3908 if (irq == -EPROBE_DEFER)
3909 goto out;
3910
Hans de Goedef146b40b2019-10-05 23:04:48 +02003911 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003912 if (irq > 0)
3913 goto out;
3914
3915 if (irq == -EPROBE_DEFER)
3916 goto out;
3917
3918 irq = platform_get_irq(dwc3_pdev, 0);
3919 if (irq > 0)
3920 goto out;
3921
Felipe Balbi6db38122016-10-03 11:27:01 +03003922 if (!irq)
3923 irq = -EINVAL;
3924
3925out:
3926 return irq;
3927}
3928
Peter Chene81a7012020-08-21 10:55:48 +08003929static void dwc_gadget_release(struct device *dev)
3930{
3931 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3932
3933 kfree(gadget);
3934}
3935
Felipe Balbi72246da2011-08-19 18:10:58 +03003936/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003937 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003938 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003939 *
3940 * Returns 0 on success otherwise negative errno.
3941 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003942int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003943{
Felipe Balbi6db38122016-10-03 11:27:01 +03003944 int ret;
3945 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003946 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003947
Felipe Balbi6db38122016-10-03 11:27:01 +03003948 irq = dwc3_gadget_get_irq(dwc);
3949 if (irq < 0) {
3950 ret = irq;
3951 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003952 }
3953
3954 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003955
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303956 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3957 sizeof(*dwc->ep0_trb) * 2,
3958 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003959 if (!dwc->ep0_trb) {
3960 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3961 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003962 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003963 }
3964
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003965 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003966 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003967 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003968 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003969 }
3970
Felipe Balbi905dc042017-01-05 14:46:52 +02003971 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3972 &dwc->bounce_addr, GFP_KERNEL);
3973 if (!dwc->bounce) {
3974 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003975 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003976 }
3977
Baolin Wangbb014732016-10-14 17:11:33 +08003978 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003979 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3980 if (!dwc->gadget) {
3981 ret = -ENOMEM;
3982 goto err3;
3983 }
Baolin Wangbb014732016-10-14 17:11:33 +08003984
Peter Chene81a7012020-08-21 10:55:48 +08003985
3986 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3987 dev = &dwc->gadget->dev;
3988 dev->platform_data = dwc;
3989 dwc->gadget->ops = &dwc3_gadget_ops;
3990 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003991 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
Peter Chene81a7012020-08-21 10:55:48 +08003992 dwc->gadget->sg_supported = true;
3993 dwc->gadget->name = "dwc3-gadget";
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003994 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03003995
3996 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003997 * FIXME We might be setting max_speed to <SUPER, however versions
3998 * <2.20a of dwc3 have an issue with metastability (documented
3999 * elsewhere in this driver) which tells us we can't set max speed to
4000 * anything lower than SUPER.
4001 *
4002 * Because gadget.max_speed is only used by composite.c and function
4003 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4004 * to happen so we avoid sending SuperSpeed Capability descriptor
4005 * together with our BOS descriptor as that could confuse host into
4006 * thinking we can handle super speed.
4007 *
4008 * Note that, in fact, we won't even support GetBOS requests when speed
4009 * is less than super speed because we don't have means, yet, to tell
4010 * composite.c that we are USB 2.0 + LPM ECN.
4011 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07004012 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02004013 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02004014 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004015 dwc->revision);
4016
Peter Chene81a7012020-08-21 10:55:48 +08004017 dwc->gadget->max_speed = dwc->maximum_speed;
Thinh Nguyen67848142021-01-19 17:36:21 -08004018 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004019
4020 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03004021 * REVISIT: Here we should clear all pending IRQs to be
4022 * sure we're starting from a well known location.
4023 */
4024
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00004025 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03004026 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03004027 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08004028
4029 ret = usb_add_gadget(dwc->gadget);
4030 if (ret) {
4031 dev_err(dwc->dev, "failed to add gadget\n");
4032 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03004033 }
4034
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08004035 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4036 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4037 else
4038 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02004039
Felipe Balbi72246da2011-08-19 18:10:58 +03004040 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004041
Peter Chene81a7012020-08-21 10:55:48 +08004042err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004043 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08004044err4:
4045 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004046err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004047 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4048 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004049
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004050err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004051 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004052
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004053err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304054 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004055 dwc->ep0_trb, dwc->ep0_trb_addr);
4056
Felipe Balbi72246da2011-08-19 18:10:58 +03004057err0:
4058 return ret;
4059}
4060
Felipe Balbi7415f172012-04-30 14:56:33 +03004061/* -------------------------------------------------------------------------- */
4062
Felipe Balbi72246da2011-08-19 18:10:58 +03004063void dwc3_gadget_exit(struct dwc3 *dwc)
4064{
Jack Phambb9c74a2021-05-01 02:35:58 -07004065 usb_del_gadget(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004066 dwc3_gadget_free_endpoints(dwc);
Jack Phambb9c74a2021-05-01 02:35:58 -07004067 usb_put_gadget(dwc->gadget);
Felipe Balbi905dc042017-01-05 14:46:52 +02004068 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004069 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004070 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304071 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004072 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004073}
Felipe Balbi7415f172012-04-30 14:56:33 +03004074
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004075int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004076{
Roger Quadros9772b472016-04-12 11:33:29 +03004077 if (!dwc->gadget_driver)
4078 return 0;
4079
Roger Quadros1551e352017-02-15 14:16:26 +02004080 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004081 dwc3_disconnect_gadget(dwc);
4082 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004083
4084 return 0;
4085}
4086
4087int dwc3_gadget_resume(struct dwc3 *dwc)
4088{
Felipe Balbi7415f172012-04-30 14:56:33 +03004089 int ret;
4090
Roger Quadros9772b472016-04-12 11:33:29 +03004091 if (!dwc->gadget_driver)
4092 return 0;
4093
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004094 ret = __dwc3_gadget_start(dwc);
4095 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004096 goto err0;
4097
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004098 ret = dwc3_gadget_run_stop(dwc, true, false);
4099 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004100 goto err1;
4101
Felipe Balbi7415f172012-04-30 14:56:33 +03004102 return 0;
4103
4104err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004105 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004106
4107err0:
4108 return ret;
4109}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004110
4111void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4112{
4113 if (dwc->pending_events) {
4114 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4115 dwc->pending_events = false;
4116 enable_irq(dwc->irq_gadget);
4117 }
4118}