blob: f8f4af8c48e862f444f1dfcb8ce50555ff69307c [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 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://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) {
49 case TEST_J:
50 case TEST_K:
51 case TEST_SE0_NAK:
52 case TEST_PACKET:
53 case TEST_FORCE_EN:
54 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
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 */
98 if (dwc->revision >= DWC3_REVISION_194A) {
99 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
Felipe Balbi8598bde2012-01-02 18:55:57 +0200111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
114 /* set requested state */
115 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
117
Paul Zimmerman802fde92012-04-27 13:10:52 +0300118 /*
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
121 */
122 if (dwc->revision >= DWC3_REVISION_194A)
123 return 0;
124
Felipe Balbi8598bde2012-01-02 18:55:57 +0200125 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300126 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200127 while (--retries) {
128 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
129
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 if (DWC3_DSTS_USBLNKST(reg) == state)
131 return 0;
132
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800133 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200134 }
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 return -ETIMEDOUT;
137}
138
John Youndca01192016-05-19 17:26:05 -0700139/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700142 *
143 * The index should never point to the link TRB. After incrementing,
144 * if it is point to the link TRB, wrap around to the beginning. The
145 * link TRB is always at the last TRB entry.
146 */
147static void dwc3_ep_inc_trb(u8 *index)
148{
149 (*index)++;
150 if (*index == (DWC3_TRB_NUM - 1))
151 *index = 0;
152}
153
Felipe Balbibfad65e2017-04-19 14:59:27 +0300154/**
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
157 */
Felipe Balbief966b92016-04-05 13:09:51 +0300158static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159{
John Youndca01192016-05-19 17:26:05 -0700160 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300161}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162
Felipe Balbibfad65e2017-04-19 14:59:27 +0300163/**
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
166 */
Felipe Balbief966b92016-04-05 13:09:51 +0300167static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
168{
John Youndca01192016-05-19 17:26:05 -0700169 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200170}
171
Wei Yongjun69102512018-03-29 02:20:10 +0000172static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300173 struct dwc3_request *req, int status)
174{
175 struct dwc3 *dwc = dep->dwc;
176
Felipe Balbic91815b2018-03-26 13:14:47 +0300177 list_del(&req->list);
178 req->remaining = 0;
Jack Phambd6742242019-01-10 12:39:55 -0800179 req->needs_extra_trb = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300180
181 if (req->request.status == -EINPROGRESS)
182 req->request.status = status;
183
184 if (req->trb)
185 usb_gadget_unmap_request_by_dev(dwc->sysdev,
186 &req->request, req->direction);
187
188 req->trb = NULL;
189 trace_dwc3_gadget_giveback(req);
190
191 if (dep->number > 1)
192 pm_runtime_put(dwc->dev);
193}
194
Felipe Balbibfad65e2017-04-19 14:59:27 +0300195/**
196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
197 * @dep: The endpoint to whom the request belongs to
198 * @req: The request we're giving back
199 * @status: completion code for the request
200 *
201 * Must be called with controller's lock held and interrupts disabled. This
202 * function will unmap @req and call its ->complete() callback to notify upper
203 * layers that it has completed.
204 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300205void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
206 int status)
207{
208 struct dwc3 *dwc = dep->dwc;
209
Felipe Balbic91815b2018-03-26 13:14:47 +0300210 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbia3af5e32019-01-11 12:57:09 +0200211 req->status = DWC3_REQUEST_STATUS_COMPLETED;
Felipe Balbi72246da2011-08-19 18:10:58 +0300212
213 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200214 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300215 spin_lock(&dwc->lock);
216}
217
Felipe Balbibfad65e2017-04-19 14:59:27 +0300218/**
219 * dwc3_send_gadget_generic_command - issue a generic command for the controller
220 * @dwc: pointer to the controller context
221 * @cmd: the command to be issued
222 * @param: command parameter
223 *
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
226 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500227int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300228{
229 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300230 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300231 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300232 u32 reg;
233
234 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
236
237 do {
238 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
239 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300240 status = DWC3_DGCMD_STATUS(reg);
241 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300242 ret = -EINVAL;
243 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300244 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100245 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246
247 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300248 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300249 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250 }
251
Felipe Balbi71f7e702016-05-23 14:16:19 +0300252 trace_dwc3_gadget_generic_cmd(cmd, param, status);
253
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300254 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300255}
256
Felipe Balbic36d8e92016-04-04 12:46:33 +0300257static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
258
Felipe Balbibfad65e2017-04-19 14:59:27 +0300259/**
260 * dwc3_send_gadget_ep_cmd - issue an endpoint command
261 * @dep: the endpoint to which the command is going to be issued
262 * @cmd: the command to be issued
263 * @params: parameters to the command
264 *
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
267 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300268int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300270{
Felipe Balbi8897a762016-09-22 10:56:08 +0300271 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300272 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000273 u32 timeout = 1000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700274 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275 u32 reg;
276
Felipe Balbi0933df12016-05-23 14:02:33 +0300277 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300278 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300279
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300280 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700281 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
282 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
283 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
287 *
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300289 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300290 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700293 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300294 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300295 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700296
297 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
300 }
301
302 if (saved_config)
303 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300304 }
305
Felipe Balbi59999142016-09-22 12:25:28 +0300306 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300307 int needs_wakeup;
308
309 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
310 dwc->link_state == DWC3_LINK_STATE_U2 ||
311 dwc->link_state == DWC3_LINK_STATE_U3);
312
313 if (unlikely(needs_wakeup)) {
314 ret = __dwc3_gadget_wakeup(dwc);
315 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316 ret);
317 }
318 }
319
Felipe Balbi2eb88012016-04-12 16:53:39 +0300320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
322 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300323
Felipe Balbi8897a762016-09-22 10:56:08 +0300324 /*
325 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
326 * not relying on XferNotReady, we can make use of a special "No
327 * Response Update Transfer" command where we should clear both CmdAct
328 * and CmdIOC bits.
329 *
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
332 *
333 * NOTICE: We're making an assumption that control endpoints will never
334 * make use of Update Transfer command. This is a safe assumption
335 * because we can never have more than one request at a time with
336 * Control Endpoints. If anybody changes that assumption, this chunk
337 * needs to be updated accordingly.
338 */
339 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
340 !usb_endpoint_xfer_isoc(desc))
341 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
342 else
343 cmd |= DWC3_DEPCMD_CMDACT;
344
345 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300346 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300347 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300348 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300349 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000350
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000351 switch (cmd_status) {
352 case 0:
353 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300354 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000355 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000356 ret = -EINVAL;
357 break;
358 case DEPEVT_TRANSFER_BUS_EXPIRY:
359 /*
360 * SW issues START TRANSFER command to
361 * isochronous ep with future frame interval. If
362 * future interval time has already passed when
363 * core receives the command, it will respond
364 * with an error status of 'Bus Expiry'.
365 *
366 * Instead of always returning -EINVAL, let's
367 * give a hint to the gadget driver that this is
368 * the case by returning -EAGAIN.
369 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000370 ret = -EAGAIN;
371 break;
372 default:
373 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
374 }
375
Felipe Balbic0ca3242016-04-04 09:11:51 +0300376 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300377 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300378 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300379
Felipe Balbif6bb2252016-05-23 13:53:34 +0300380 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300381 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300382 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300383 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300384
Felipe Balbi0933df12016-05-23 14:02:33 +0300385 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
386
Felipe Balbiacbfa6c2019-01-21 12:58:27 +0200387 if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
388 dep->flags |= DWC3_EP_TRANSFER_STARTED;
389 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300390 }
391
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700392 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300393 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700394 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
396 }
397
Felipe Balbic0ca3242016-04-04 09:11:51 +0300398 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300399}
400
John Youn50c763f2016-05-31 17:49:56 -0700401static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
402{
403 struct dwc3 *dwc = dep->dwc;
404 struct dwc3_gadget_ep_cmd_params params;
405 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
406
407 /*
408 * As of core revision 2.60a the recommended programming model
409 * is to set the ClearPendIN bit when issuing a Clear Stall EP
410 * command for IN endpoints. This is to prevent an issue where
411 * some (non-compliant) hosts may not send ACK TPs for pending
412 * IN transfers due to a mishandled error condition. Synopsys
413 * STAR 9000614252.
414 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700417 cmd |= DWC3_DEPCMD_CLEARPENDIN;
418
419 memset(&params, 0, sizeof(params));
420
Felipe Balbi2cd47182016-04-12 16:42:43 +0300421 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700422}
423
Felipe Balbi72246da2011-08-19 18:10:58 +0300424static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200425 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300426{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300427 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300428
429 return dep->trb_pool_dma + offset;
430}
431
432static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
433{
434 struct dwc3 *dwc = dep->dwc;
435
436 if (dep->trb_pool)
437 return 0;
438
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530439 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
441 &dep->trb_pool_dma, GFP_KERNEL);
442 if (!dep->trb_pool) {
443 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
444 dep->name);
445 return -ENOMEM;
446 }
447
448 return 0;
449}
450
451static void dwc3_free_trb_pool(struct dwc3_ep *dep)
452{
453 struct dwc3 *dwc = dep->dwc;
454
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300456 dep->trb_pool, dep->trb_pool_dma);
457
458 dep->trb_pool = NULL;
459 dep->trb_pool_dma = 0;
460}
461
Felipe Balbi20d1d432018-04-09 12:49:02 +0300462static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
463{
464 struct dwc3_gadget_ep_cmd_params params;
465
466 memset(&params, 0x00, sizeof(params));
467
468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
469
470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471 &params);
472}
John Younc4509602016-02-16 20:10:53 -0800473
474/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300475 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800476 * @dep: endpoint that is being enabled
477 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800480 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300481 * The assignment of transfer resources cannot perfectly follow the data book
482 * due to the fact that the controller driver does not have all knowledge of the
483 * configuration in advance. It is given this information piecemeal by the
484 * composite gadget framework after every SET_CONFIGURATION and
485 * SET_INTERFACE. Trying to follow the databook programming model in this
486 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800487 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300488 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
489 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
490 * incorrect in the scenario of multiple interfaces.
491 *
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800493 * endpoint on alt setting (8.1.6).
494 *
495 * The following simplified method is used instead:
496 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300497 * All hardware endpoints can be assigned a transfer resource and this setting
498 * will stay persistent until either a core reset or hibernation. So whenever we
499 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
500 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800501 * guaranteed that there are as many transfer resources as endpoints.
502 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300503 * This function is called for each endpoint when it is being enabled but is
504 * triggered only when called for EP0-out, which always happens first, and which
505 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800506 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300507static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300508{
509 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300510 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300511 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800512 int i;
513 int ret;
514
515 if (dep->number)
516 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
518 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800519 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300520 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521
Felipe Balbi2cd47182016-04-12 16:42:43 +0300522 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800523 if (ret)
524 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525
John Younc4509602016-02-16 20:10:53 -0800526 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527 struct dwc3_ep *dep = dwc->eps[i];
528
529 if (!dep)
530 continue;
531
Felipe Balbib07c2db2018-04-09 12:46:47 +0300532 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800533 if (ret)
534 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 }
536
537 return 0;
538}
539
Felipe Balbib07c2db2018-04-09 12:46:47 +0300540static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300541{
John Youn39ebb052016-11-09 16:36:28 -0800542 const struct usb_ss_ep_comp_descriptor *comp_desc;
543 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300545 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300546
John Youn39ebb052016-11-09 16:36:28 -0800547 comp_desc = dep->endpoint.comp_desc;
548 desc = dep->endpoint.desc;
549
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 memset(&params, 0x00, sizeof(params));
551
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300552 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
554
555 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800556 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300557 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300558 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900559 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300560
Felipe Balbia2d23f02018-04-09 12:40:48 +0300561 params.param0 |= action;
562 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600563 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600564
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300565 if (usb_endpoint_xfer_control(desc))
566 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300567
568 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200571 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300572 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
573 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300574 dep->stream_capable = true;
575 }
576
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500577 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300578 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
580 /*
581 * We are doing 1:1 mapping for endpoints, meaning
582 * Physical Endpoints 2 maps to Logical Endpoint 2 and
583 * so on. We consider the direction bit as part of the physical
584 * endpoint number. So USB endpoint 0x81 is 0x03.
585 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300586 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
588 /*
589 * We must use the lower 16 TX FIFOs even though
590 * HW might have more
591 */
592 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300593 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594
595 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300596 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 dep->interval = 1 << (desc->bInterval - 1);
598 }
599
Felipe Balbi2cd47182016-04-12 16:42:43 +0300600 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300601}
602
Felipe Balbi72246da2011-08-19 18:10:58 +0300603/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300605 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300606 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300608 * Caller should take care of locking. Execute all necessary commands to
609 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300611static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300612{
John Youn39ebb052016-11-09 16:36:28 -0800613 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800615
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300617 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300618
619 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300620 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 if (ret)
622 return ret;
623 }
624
Felipe Balbib07c2db2018-04-09 12:46:47 +0300625 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 if (ret)
627 return ret;
628
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200630 struct dwc3_trb *trb_st_hw;
631 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300632
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 dep->type = usb_endpoint_type(desc);
634 dep->flags |= DWC3_EP_ENABLED;
635
636 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
637 reg |= DWC3_DALEPENA_EP(dep->number);
638 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
639
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300640 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200641 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300642
John Youn0d257442016-05-19 17:26:08 -0700643 /* Initialize the TRB ring */
644 dep->trb_dequeue = 0;
645 dep->trb_enqueue = 0;
646 memset(dep->trb_pool, 0,
647 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
648
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300649 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 trb_st_hw = &dep->trb_pool[0];
651
Felipe Balbif6bafc62012-02-06 11:04:53 +0200652 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200653 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 }
658
Felipe Balbia97ea992016-09-29 16:28:56 +0300659 /*
660 * Issue StartTransfer here with no-op TRB so we can always rely on No
661 * Response Update Transfer command.
662 */
Anurag Kumar Vulisha26d62b42018-12-01 16:43:27 +0530663 if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300664 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300665 struct dwc3_gadget_ep_cmd_params params;
666 struct dwc3_trb *trb;
667 dma_addr_t trb_dma;
668 u32 cmd;
669
670 memset(&params, 0, sizeof(params));
671 trb = &dep->trb_pool[0];
672 trb_dma = dwc3_trb_dma_offset(dep, trb);
673
674 params.param0 = upper_32_bits(trb_dma);
675 params.param1 = lower_32_bits(trb_dma);
676
677 cmd = DWC3_DEPCMD_STARTTRANSFER;
678
679 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
680 if (ret < 0)
681 return ret;
Felipe Balbia97ea992016-09-29 16:28:56 +0300682 }
683
Felipe Balbi2870e502016-11-03 13:53:29 +0200684out:
685 trace_dwc3_gadget_ep_enable(dep);
686
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 return 0;
688}
689
Felipe Balbic5353b22019-02-13 13:00:54 +0200690static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
691 bool interrupt);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200692static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300693{
694 struct dwc3_request *req;
695
Felipe Balbic5353b22019-02-13 13:00:54 +0200696 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300697
Felipe Balbi0e146022016-06-21 10:32:02 +0300698 /* - giveback all requests to gadget driver */
699 while (!list_empty(&dep->started_list)) {
700 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200701
Felipe Balbi0e146022016-06-21 10:32:02 +0300702 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200703 }
704
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200705 while (!list_empty(&dep->pending_list)) {
706 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300707
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200708 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300709 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200710
711 while (!list_empty(&dep->cancelled_list)) {
712 req = next_request(&dep->cancelled_list);
713
714 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
715 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300716}
717
718/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300719 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 * @dep: the endpoint to disable
721 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300722 * This function undoes what __dwc3_gadget_ep_enable did and also removes
723 * requests which are currently being processed by the hardware and those which
724 * are not yet scheduled.
725 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200726 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300728static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
729{
730 struct dwc3 *dwc = dep->dwc;
731 u32 reg;
732
Felipe Balbi2870e502016-11-03 13:53:29 +0200733 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500734
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200735 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300736
Felipe Balbi687ef982014-04-16 10:30:33 -0500737 /* make sure HW endpoint isn't stalled */
738 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500739 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500740
Felipe Balbi72246da2011-08-19 18:10:58 +0300741 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
742 reg &= ~DWC3_DALEPENA_EP(dep->number);
743 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
744
Felipe Balbi879631a2011-09-30 10:58:47 +0300745 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200747 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300748
John Youn39ebb052016-11-09 16:36:28 -0800749 /* Clear out the ep descriptors for non-ep0 */
750 if (dep->number > 1) {
751 dep->endpoint.comp_desc = NULL;
752 dep->endpoint.desc = NULL;
753 }
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 return 0;
756}
757
758/* -------------------------------------------------------------------------- */
759
760static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
761 const struct usb_endpoint_descriptor *desc)
762{
763 return -EINVAL;
764}
765
766static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
767{
768 return -EINVAL;
769}
770
771/* -------------------------------------------------------------------------- */
772
773static int dwc3_gadget_ep_enable(struct usb_ep *ep,
774 const struct usb_endpoint_descriptor *desc)
775{
776 struct dwc3_ep *dep;
777 struct dwc3 *dwc;
778 unsigned long flags;
779 int ret;
780
781 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
782 pr_debug("dwc3: invalid parameters\n");
783 return -EINVAL;
784 }
785
786 if (!desc->wMaxPacketSize) {
787 pr_debug("dwc3: missing wMaxPacketSize\n");
788 return -EINVAL;
789 }
790
791 dep = to_dwc3_ep(ep);
792 dwc = dep->dwc;
793
Felipe Balbi95ca9612015-12-10 13:08:20 -0600794 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
795 "%s is already enabled\n",
796 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300797 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300798
Felipe Balbi72246da2011-08-19 18:10:58 +0300799 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300800 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300801 spin_unlock_irqrestore(&dwc->lock, flags);
802
803 return ret;
804}
805
806static int dwc3_gadget_ep_disable(struct usb_ep *ep)
807{
808 struct dwc3_ep *dep;
809 struct dwc3 *dwc;
810 unsigned long flags;
811 int ret;
812
813 if (!ep) {
814 pr_debug("dwc3: invalid parameters\n");
815 return -EINVAL;
816 }
817
818 dep = to_dwc3_ep(ep);
819 dwc = dep->dwc;
820
Felipe Balbi95ca9612015-12-10 13:08:20 -0600821 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
822 "%s is already disabled\n",
823 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300824 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300825
Felipe Balbi72246da2011-08-19 18:10:58 +0300826 spin_lock_irqsave(&dwc->lock, flags);
827 ret = __dwc3_gadget_ep_disable(dep);
828 spin_unlock_irqrestore(&dwc->lock, flags);
829
830 return ret;
831}
832
833static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300834 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300835{
836 struct dwc3_request *req;
837 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300838
839 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900840 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300841 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300843 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 req->epnum = dep->number;
845 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200846 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300847
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500848 trace_dwc3_alloc_request(req);
849
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 return &req->request;
851}
852
853static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
854 struct usb_request *request)
855{
856 struct dwc3_request *req = to_dwc3_request(request);
857
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500858 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 kfree(req);
860}
861
Felipe Balbi42626912018-04-09 13:01:43 +0300862/**
863 * dwc3_ep_prev_trb - returns the previous TRB in the ring
864 * @dep: The endpoint with the TRB ring
865 * @index: The index of the current TRB in the ring
866 *
867 * Returns the TRB prior to the one pointed to by the index. If the
868 * index is 0, we will wrap backwards, skip the link TRB, and return
869 * the one just before that.
870 */
871static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
872{
873 u8 tmp = index;
874
875 if (!tmp)
876 tmp = DWC3_TRB_NUM - 1;
877
878 return &dep->trb_pool[tmp - 1];
879}
880
881static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
882{
883 struct dwc3_trb *tmp;
884 u8 trbs_left;
885
886 /*
887 * If enqueue & dequeue are equal than it is either full or empty.
888 *
889 * One way to know for sure is if the TRB right before us has HWO bit
890 * set or not. If it has, then we're definitely full and can't fit any
891 * more transfers in our ring.
892 */
893 if (dep->trb_enqueue == dep->trb_dequeue) {
894 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
895 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
896 return 0;
897
898 return DWC3_TRB_NUM - 1;
899 }
900
901 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
902 trbs_left &= (DWC3_TRB_NUM - 1);
903
904 if (dep->trb_dequeue < dep->trb_enqueue)
905 trbs_left--;
906
907 return trbs_left;
908}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300909
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200910static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
911 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
912 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200913{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300914 struct dwc3 *dwc = dep->dwc;
915 struct usb_gadget *gadget = &dwc->gadget;
916 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200917
Felipe Balbif6bafc62012-02-06 11:04:53 +0200918 trb->size = DWC3_TRB_SIZE_LENGTH(length);
919 trb->bpl = lower_32_bits(dma);
920 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200921
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200922 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200923 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200924 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200925 break;
926
927 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300928 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530929 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300930
Manu Gautam40d829f2017-07-19 17:07:10 +0530931 /*
932 * USB Specification 2.0 Section 5.9.2 states that: "If
933 * there is only a single transaction in the microframe,
934 * only a DATA0 data packet PID is used. If there are
935 * two transactions per microframe, DATA1 is used for
936 * the first transaction data packet and DATA0 is used
937 * for the second transaction data packet. If there are
938 * three transactions per microframe, DATA2 is used for
939 * the first transaction data packet, DATA1 is used for
940 * the second, and DATA0 is used for the third."
941 *
942 * IOW, we should satisfy the following cases:
943 *
944 * 1) length <= maxpacket
945 * - DATA0
946 *
947 * 2) maxpacket < length <= (2 * maxpacket)
948 * - DATA1, DATA0
949 *
950 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
951 * - DATA2, DATA1, DATA0
952 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300953 if (speed == USB_SPEED_HIGH) {
954 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530955 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530956 unsigned int maxp = usb_endpoint_maxp(ep->desc);
957
958 if (length <= (2 * maxp))
959 mult--;
960
961 if (length <= maxp)
962 mult--;
963
964 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300965 }
966 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300968 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200969
970 /* always enable Interrupt on Missed ISOC */
971 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200972 break;
973
974 case USB_ENDPOINT_XFER_BULK:
975 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200976 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200977 break;
978 default:
979 /*
980 * This is only possible with faulty memory because we
981 * checked it already :)
982 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300983 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
984 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200985 }
986
Tejas Joglekar244add82018-12-10 16:08:13 +0530987 /*
988 * Enable Continue on Short Packet
989 * when endpoint is not a stream capable
990 */
Felipe Balbic9508c82016-10-05 14:26:23 +0300991 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +0530992 if (!dep->stream_capable)
993 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600994
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200995 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300996 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
997 }
998
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200999 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301000 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001001 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001002
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301003 if (chain)
1004 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1005
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001006 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001007 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001008
1009 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001010
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301011 dwc3_ep_inc_enq(dep);
1012
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001013 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001014}
1015
John Youn361572b2016-05-19 17:26:17 -07001016/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001017 * dwc3_prepare_one_trb - setup one TRB from one request
1018 * @dep: endpoint for which this request is prepared
1019 * @req: dwc3_request pointer
1020 * @chain: should this TRB be chained to the next?
1021 * @node: only for isochronous endpoints. First TRB needs different type.
1022 */
1023static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1024 struct dwc3_request *req, unsigned chain, unsigned node)
1025{
1026 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301027 unsigned int length;
1028 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001029 unsigned stream_id = req->request.stream_id;
1030 unsigned short_not_ok = req->request.short_not_ok;
1031 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301032
1033 if (req->request.num_sgs > 0) {
1034 length = sg_dma_len(req->start_sg);
1035 dma = sg_dma_address(req->start_sg);
1036 } else {
1037 length = req->request.length;
1038 dma = req->request.dma;
1039 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001040
1041 trb = &dep->trb_pool[dep->trb_enqueue];
1042
1043 if (!req->trb) {
1044 dwc3_gadget_move_started_request(req);
1045 req->trb = trb;
1046 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001047 }
1048
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001049 req->num_trbs++;
1050
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001051 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1052 stream_id, short_not_ok, no_interrupt);
1053}
1054
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001055static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001056 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001057{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301058 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001059 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 int i;
1061
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301062 unsigned int remaining = req->request.num_mapped_sgs
1063 - req->num_queued_sgs;
1064
1065 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001066 unsigned int length = req->request.length;
1067 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1068 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001069 unsigned chain = true;
1070
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001071 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001072 chain = false;
1073
Felipe Balbic6267a52017-01-05 14:58:46 +02001074 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1075 struct dwc3 *dwc = dep->dwc;
1076 struct dwc3_trb *trb;
1077
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001078 req->needs_extra_trb = true;
Felipe Balbic6267a52017-01-05 14:58:46 +02001079
1080 /* prepare normal TRB */
1081 dwc3_prepare_one_trb(dep, req, true, i);
1082
1083 /* Now prepare one extra TRB to align transfer size */
1084 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001085 req->num_trbs++;
Felipe Balbic6267a52017-01-05 14:58:46 +02001086 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001087 maxp - rem, false, 1,
Felipe Balbic6267a52017-01-05 14:58:46 +02001088 req->request.stream_id,
1089 req->request.short_not_ok,
1090 req->request.no_interrupt);
1091 } else {
1092 dwc3_prepare_one_trb(dep, req, chain, i);
1093 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001094
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301095 /*
1096 * There can be a situation where all sgs in sglist are not
1097 * queued because of insufficient trb number. To handle this
1098 * case, update start_sg to next sg to be queued, so that
1099 * we have free trbs we can continue queuing from where we
1100 * previously stopped
1101 */
1102 if (chain)
1103 req->start_sg = sg_next(s);
1104
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301105 req->num_queued_sgs++;
1106
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001107 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001108 break;
1109 }
1110}
1111
1112static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001113 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001114{
Felipe Balbic6267a52017-01-05 14:58:46 +02001115 unsigned int length = req->request.length;
1116 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1117 unsigned int rem = length % maxp;
1118
Tejas Joglekar1e19cdc2019-01-22 13:26:51 +05301119 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001120 struct dwc3 *dwc = dep->dwc;
1121 struct dwc3_trb *trb;
1122
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001123 req->needs_extra_trb = true;
Felipe Balbic6267a52017-01-05 14:58:46 +02001124
1125 /* prepare normal TRB */
1126 dwc3_prepare_one_trb(dep, req, true, 0);
1127
1128 /* Now prepare one extra TRB to align transfer size */
1129 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001130 req->num_trbs++;
Felipe Balbic6267a52017-01-05 14:58:46 +02001131 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001132 false, 1, req->request.stream_id,
Felipe Balbic6267a52017-01-05 14:58:46 +02001133 req->request.short_not_ok,
1134 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001135 } else if (req->request.zero && req->request.length &&
Thinh Nguyen4ea438d2018-07-27 18:52:41 -07001136 (IS_ALIGNED(req->request.length, maxp))) {
Felipe Balbid6e5a542017-04-07 16:34:38 +03001137 struct dwc3 *dwc = dep->dwc;
1138 struct dwc3_trb *trb;
1139
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001140 req->needs_extra_trb = true;
Felipe Balbid6e5a542017-04-07 16:34:38 +03001141
1142 /* prepare normal TRB */
1143 dwc3_prepare_one_trb(dep, req, true, 0);
1144
1145 /* Now prepare one extra TRB to handle ZLP */
1146 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001147 req->num_trbs++;
Felipe Balbid6e5a542017-04-07 16:34:38 +03001148 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001149 false, 1, req->request.stream_id,
Felipe Balbid6e5a542017-04-07 16:34:38 +03001150 req->request.short_not_ok,
1151 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001152 } else {
1153 dwc3_prepare_one_trb(dep, req, false, 0);
1154 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001155}
1156
Felipe Balbi72246da2011-08-19 18:10:58 +03001157/*
1158 * dwc3_prepare_trbs - setup TRBs from requests
1159 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001160 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001161 * The function goes through the requests list and sets up TRBs for the
1162 * transfers. The function returns once there are no more TRBs available or
1163 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001164 */
Felipe Balbic4233572016-05-12 14:08:34 +03001165static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001166{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001167 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001168
1169 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1170
Felipe Balbid86c5a62016-10-25 13:48:52 +03001171 /*
1172 * We can get in a situation where there's a request in the started list
1173 * but there weren't enough TRBs to fully kick it in the first time
1174 * around, so it has been waiting for more TRBs to be freed up.
1175 *
1176 * In that case, we should check if we have a request with pending_sgs
1177 * in the started list and prepare TRBs for that request first,
1178 * otherwise we will prepare TRBs completely out of order and that will
1179 * break things.
1180 */
1181 list_for_each_entry(req, &dep->started_list, list) {
1182 if (req->num_pending_sgs > 0)
1183 dwc3_prepare_one_trb_sg(dep, req);
1184
1185 if (!dwc3_calc_trbs_left(dep))
1186 return;
1187 }
1188
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001189 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001190 struct dwc3 *dwc = dep->dwc;
1191 int ret;
1192
1193 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1194 dep->direction);
1195 if (ret)
1196 return;
1197
1198 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301199 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301200 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001201 req->num_pending_sgs = req->request.num_mapped_sgs;
1202
Felipe Balbi1f512112016-08-12 13:17:27 +03001203 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001204 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001205 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001206 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001207
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001208 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001209 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001210 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001211}
1212
Felipe Balbi7fdca762017-09-05 14:41:34 +03001213static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001214{
1215 struct dwc3_gadget_ep_cmd_params params;
1216 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001217 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001218 int ret;
1219 u32 cmd;
1220
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001221 if (!dwc3_calc_trbs_left(dep))
1222 return 0;
1223
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001224 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001225
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001226 dwc3_prepare_trbs(dep);
1227 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 if (!req) {
1229 dep->flags |= DWC3_EP_PENDING_REQUEST;
1230 return 0;
1231 }
1232
1233 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001234
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001235 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301236 params.param0 = upper_32_bits(req->trb_dma);
1237 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001238 cmd = DWC3_DEPCMD_STARTTRANSFER;
1239
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301240 if (dep->stream_capable)
1241 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1242
Felipe Balbi7fdca762017-09-05 14:41:34 +03001243 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1244 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301245 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001246 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1247 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301248 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001249
Felipe Balbi2cd47182016-04-12 16:42:43 +03001250 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001251 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 /*
1253 * FIXME we need to iterate over the list of requests
1254 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001255 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001257 if (req->trb)
1258 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001259 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001260 return ret;
1261 }
1262
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 return 0;
1264}
1265
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001266static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1267{
1268 u32 reg;
1269
1270 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1271 return DWC3_DSTS_SOFFN(reg);
1272}
1273
Thinh Nguyend92021f2018-11-14 22:56:54 -08001274/**
1275 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1276 * @dep: isoc endpoint
1277 *
1278 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1279 * microframe number reported by the XferNotReady event for the future frame
1280 * number to start the isoc transfer.
1281 *
1282 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1283 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1284 * XferNotReady event are invalid. The driver uses this number to schedule the
1285 * isochronous transfer and passes it to the START TRANSFER command. Because
1286 * this number is invalid, the command may fail. If BIT[15:14] matches the
1287 * internal 16-bit microframe, the START TRANSFER command will pass and the
1288 * transfer will start at the scheduled time, if it is off by 1, the command
1289 * will still pass, but the transfer will start 2 seconds in the future. For all
1290 * other conditions, the START TRANSFER command will fail with bus-expiry.
1291 *
1292 * In order to workaround this issue, we can test for the correct combination of
1293 * BIT[15:14] by sending START TRANSFER commands with different values of
1294 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1295 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1296 * As the result, within the 4 possible combinations for BIT[15:14], there will
1297 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1298 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1299 * value is the correct combination.
1300 *
1301 * Since there are only 4 outcomes and the results are ordered, we can simply
1302 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1303 * deduce the smaller successful combination.
1304 *
1305 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1306 * of BIT[15:14]. The correct combination is as follow:
1307 *
1308 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1309 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1310 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1311 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1312 *
1313 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1314 * endpoints.
1315 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001316static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301317{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001318 int cmd_status = 0;
1319 bool test0;
1320 bool test1;
1321
1322 while (dep->combo_num < 2) {
1323 struct dwc3_gadget_ep_cmd_params params;
1324 u32 test_frame_number;
1325 u32 cmd;
1326
1327 /*
1328 * Check if we can start isoc transfer on the next interval or
1329 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1330 */
1331 test_frame_number = dep->frame_number & 0x3fff;
1332 test_frame_number |= dep->combo_num << 14;
1333 test_frame_number += max_t(u32, 4, dep->interval);
1334
1335 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1336 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1337
1338 cmd = DWC3_DEPCMD_STARTTRANSFER;
1339 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1340 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1341
1342 /* Redo if some other failure beside bus-expiry is received */
1343 if (cmd_status && cmd_status != -EAGAIN) {
1344 dep->start_cmd_status = 0;
1345 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001346 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001347 }
1348
1349 /* Store the first test status */
1350 if (dep->combo_num == 0)
1351 dep->start_cmd_status = cmd_status;
1352
1353 dep->combo_num++;
1354
1355 /*
1356 * End the transfer if the START_TRANSFER command is successful
1357 * to wait for the next XferNotReady to test the command again
1358 */
1359 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001360 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001361 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001362 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301363 }
1364
Thinh Nguyend92021f2018-11-14 22:56:54 -08001365 /* test0 and test1 are both completed at this point */
1366 test0 = (dep->start_cmd_status == 0);
1367 test1 = (cmd_status == 0);
1368
1369 if (!test0 && test1)
1370 dep->combo_num = 1;
1371 else if (!test0 && !test1)
1372 dep->combo_num = 2;
1373 else if (test0 && !test1)
1374 dep->combo_num = 3;
1375 else if (test0 && test1)
1376 dep->combo_num = 0;
1377
1378 dep->frame_number &= 0x3fff;
1379 dep->frame_number |= dep->combo_num << 14;
1380 dep->frame_number += max_t(u32, 4, dep->interval);
1381
1382 /* Reinitialize test variables */
1383 dep->start_cmd_status = 0;
1384 dep->combo_num = 0;
1385
Felipe Balbi25abad62018-08-14 10:41:19 +03001386 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001387}
1388
Felipe Balbi25abad62018-08-14 10:41:19 +03001389static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301390{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001391 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001392 int ret;
1393 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001394
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301395 if (list_empty(&dep->pending_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301396 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001397 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301398 }
1399
Thinh Nguyend92021f2018-11-14 22:56:54 -08001400 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1401 (dwc->revision <= DWC3_USB31_REVISION_160A ||
1402 (dwc->revision == DWC3_USB31_REVISION_170A &&
1403 dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1404 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1405
Felipe Balbi25abad62018-08-14 10:41:19 +03001406 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1407 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001408 }
1409
Felipe Balbid5370102018-08-14 10:42:43 +03001410 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1411 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1412
1413 ret = __dwc3_gadget_kick_transfer(dep);
1414 if (ret != -EAGAIN)
1415 break;
1416 }
1417
1418 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301419}
1420
Felipe Balbi72246da2011-08-19 18:10:58 +03001421static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1422{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001423 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001424
Felipe Balbibb423982015-11-16 15:31:21 -06001425 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001426 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1427 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001428 return -ESHUTDOWN;
1429 }
1430
Felipe Balbi04fb3652017-05-17 15:57:45 +03001431 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1432 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001433 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001434
Felipe Balbib2b6d602019-01-11 12:58:52 +02001435 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1436 "%s: request %pK already in flight\n",
1437 dep->name, &req->request))
1438 return -EINVAL;
1439
Felipe Balbifc8bb912016-05-16 13:14:48 +03001440 pm_runtime_get(dwc->dev);
1441
Felipe Balbi72246da2011-08-19 18:10:58 +03001442 req->request.actual = 0;
1443 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001444
Felipe Balbife84f522015-09-01 09:01:38 -05001445 trace_dwc3_ep_queue(req);
1446
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001447 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001448 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001449
Felipe Balbid889c232016-09-29 15:44:29 +03001450 /*
1451 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1452 * wait for a XferNotReady event so we will know what's the current
1453 * (micro-)frame number.
1454 *
1455 * Without this trick, we are very, very likely gonna get Bus Expiry
1456 * errors which will force us issue EndTransfer command.
1457 */
1458 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001459 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1460 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001461 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001462
1463 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1464 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
Felipe Balbi25abad62018-08-14 10:41:19 +03001465 return __dwc3_gadget_start_isoc(dep);
Felipe Balbife990ce2018-03-29 13:23:53 +03001466 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001467 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001468 }
1469
Felipe Balbi7fdca762017-09-05 14:41:34 +03001470 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001471}
1472
1473static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1474 gfp_t gfp_flags)
1475{
1476 struct dwc3_request *req = to_dwc3_request(request);
1477 struct dwc3_ep *dep = to_dwc3_ep(ep);
1478 struct dwc3 *dwc = dep->dwc;
1479
1480 unsigned long flags;
1481
1482 int ret;
1483
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001484 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001485 ret = __dwc3_gadget_ep_queue(dep, req);
1486 spin_unlock_irqrestore(&dwc->lock, flags);
1487
1488 return ret;
1489}
1490
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001491static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1492{
1493 int i;
1494
1495 /*
1496 * If request was already started, this means we had to
1497 * stop the transfer. With that we also need to ignore
1498 * all TRBs used by the request, however TRBs can only
1499 * be modified after completion of END_TRANSFER
1500 * command. So what we do here is that we wait for
1501 * END_TRANSFER completion and only after that, we jump
1502 * over TRBs by clearing HWO and incrementing dequeue
1503 * pointer.
1504 */
1505 for (i = 0; i < req->num_trbs; i++) {
1506 struct dwc3_trb *trb;
1507
1508 trb = req->trb + i;
1509 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1510 dwc3_ep_inc_deq(dep);
1511 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001512
1513 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001514}
1515
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001516static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1517{
1518 struct dwc3_request *req;
1519 struct dwc3_request *tmp;
1520
1521 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1522 dwc3_gadget_ep_skip_trbs(dep, req);
1523 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1524 }
1525}
1526
Felipe Balbi72246da2011-08-19 18:10:58 +03001527static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1528 struct usb_request *request)
1529{
1530 struct dwc3_request *req = to_dwc3_request(request);
1531 struct dwc3_request *r = NULL;
1532
1533 struct dwc3_ep *dep = to_dwc3_ep(ep);
1534 struct dwc3 *dwc = dep->dwc;
1535
1536 unsigned long flags;
1537 int ret = 0;
1538
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001539 trace_dwc3_ep_dequeue(req);
1540
Felipe Balbi72246da2011-08-19 18:10:58 +03001541 spin_lock_irqsave(&dwc->lock, flags);
1542
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001543 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001544 if (r == req)
1545 break;
1546 }
1547
1548 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001549 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001550 if (r == req)
1551 break;
1552 }
1553 if (r == req) {
1554 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001555 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001556
Felipe Balbicf3113d2017-02-17 11:12:44 +02001557 if (!r->trb)
Mayank Rana05645362018-03-23 10:05:33 -07001558 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001559
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001560 dwc3_gadget_move_cancelled_request(req);
Felipe Balbi9f455812019-01-21 13:01:16 +02001561 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1562 goto out0;
1563 else
1564 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001565 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001566 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001567 request, ep->name);
1568 ret = -EINVAL;
1569 goto out0;
1570 }
1571
Felipe Balbi9f455812019-01-21 13:01:16 +02001572out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1574
1575out0:
1576 spin_unlock_irqrestore(&dwc->lock, flags);
1577
1578 return ret;
1579}
1580
Felipe Balbi7a608552014-09-24 14:19:52 -05001581int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001582{
1583 struct dwc3_gadget_ep_cmd_params params;
1584 struct dwc3 *dwc = dep->dwc;
1585 int ret;
1586
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001587 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1588 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1589 return -EINVAL;
1590 }
1591
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 memset(&params, 0x00, sizeof(params));
1593
1594 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001595 struct dwc3_trb *trb;
1596
1597 unsigned transfer_in_flight;
1598 unsigned started;
1599
1600 if (dep->number > 1)
1601 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1602 else
1603 trb = &dwc->ep0_trb[dep->trb_enqueue];
1604
1605 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1606 started = !list_empty(&dep->started_list);
1607
1608 if (!protocol && ((dep->direction && transfer_in_flight) ||
1609 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001610 return -EAGAIN;
1611 }
1612
Felipe Balbi2cd47182016-04-12 16:42:43 +03001613 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1614 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001616 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001617 dep->name);
1618 else
1619 dep->flags |= DWC3_EP_STALL;
1620 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001621
John Youn50c763f2016-05-31 17:49:56 -07001622 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001624 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 dep->name);
1626 else
Alan Sterna535d812013-11-01 12:05:12 -04001627 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001628 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001629
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 return ret;
1631}
1632
1633static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1634{
1635 struct dwc3_ep *dep = to_dwc3_ep(ep);
1636 struct dwc3 *dwc = dep->dwc;
1637
1638 unsigned long flags;
1639
1640 int ret;
1641
1642 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001643 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001644 spin_unlock_irqrestore(&dwc->lock, flags);
1645
1646 return ret;
1647}
1648
1649static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1650{
1651 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001652 struct dwc3 *dwc = dep->dwc;
1653 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001654 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001655
Paul Zimmerman249a4562012-02-24 17:32:16 -08001656 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001657 dep->flags |= DWC3_EP_WEDGE;
1658
Pratyush Anand08f0d962012-06-25 22:40:43 +05301659 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001660 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301661 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001662 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001663 spin_unlock_irqrestore(&dwc->lock, flags);
1664
1665 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001666}
1667
1668/* -------------------------------------------------------------------------- */
1669
1670static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1671 .bLength = USB_DT_ENDPOINT_SIZE,
1672 .bDescriptorType = USB_DT_ENDPOINT,
1673 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1674};
1675
1676static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1677 .enable = dwc3_gadget_ep0_enable,
1678 .disable = dwc3_gadget_ep0_disable,
1679 .alloc_request = dwc3_gadget_ep_alloc_request,
1680 .free_request = dwc3_gadget_ep_free_request,
1681 .queue = dwc3_gadget_ep0_queue,
1682 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301683 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 .set_wedge = dwc3_gadget_ep_set_wedge,
1685};
1686
1687static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1688 .enable = dwc3_gadget_ep_enable,
1689 .disable = dwc3_gadget_ep_disable,
1690 .alloc_request = dwc3_gadget_ep_alloc_request,
1691 .free_request = dwc3_gadget_ep_free_request,
1692 .queue = dwc3_gadget_ep_queue,
1693 .dequeue = dwc3_gadget_ep_dequeue,
1694 .set_halt = dwc3_gadget_ep_set_halt,
1695 .set_wedge = dwc3_gadget_ep_set_wedge,
1696};
1697
1698/* -------------------------------------------------------------------------- */
1699
1700static int dwc3_gadget_get_frame(struct usb_gadget *g)
1701{
1702 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001703
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001704 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001705}
1706
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001707static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001708{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001709 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001710
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001711 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001712 u32 reg;
1713
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 u8 link_state;
1715 u8 speed;
1716
Felipe Balbi72246da2011-08-19 18:10:58 +03001717 /*
1718 * According to the Databook Remote wakeup request should
1719 * be issued only when the device is in early suspend state.
1720 *
1721 * We can check that via USB Link State bits in DSTS register.
1722 */
1723 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1724
1725 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001726 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001727 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001728 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001729
1730 link_state = DWC3_DSTS_USBLNKST(reg);
1731
1732 switch (link_state) {
1733 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1734 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1735 break;
1736 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001737 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001738 }
1739
Felipe Balbi8598bde2012-01-02 18:55:57 +02001740 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1741 if (ret < 0) {
1742 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001743 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001744 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001745
Paul Zimmerman802fde92012-04-27 13:10:52 +03001746 /* Recent versions do this automatically */
1747 if (dwc->revision < DWC3_REVISION_194A) {
1748 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001749 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001750 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1751 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1752 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001753
Paul Zimmerman1d046792012-02-15 18:56:56 -08001754 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001755 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001756
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001757 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1759
1760 /* in HS, means ON */
1761 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1762 break;
1763 }
1764
1765 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1766 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001767 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 }
1769
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001770 return 0;
1771}
1772
1773static int dwc3_gadget_wakeup(struct usb_gadget *g)
1774{
1775 struct dwc3 *dwc = gadget_to_dwc(g);
1776 unsigned long flags;
1777 int ret;
1778
1779 spin_lock_irqsave(&dwc->lock, flags);
1780 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001781 spin_unlock_irqrestore(&dwc->lock, flags);
1782
1783 return ret;
1784}
1785
1786static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1787 int is_selfpowered)
1788{
1789 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001790 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001791
Paul Zimmerman249a4562012-02-24 17:32:16 -08001792 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001793 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001794 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001795
1796 return 0;
1797}
1798
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001799static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001800{
1801 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001802 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001803
Felipe Balbifc8bb912016-05-16 13:14:48 +03001804 if (pm_runtime_suspended(dwc->dev))
1805 return 0;
1806
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001808 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001809 if (dwc->revision <= DWC3_REVISION_187A) {
1810 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1811 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1812 }
1813
1814 if (dwc->revision >= DWC3_REVISION_194A)
1815 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1816 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001817
1818 if (dwc->has_hibernation)
1819 reg |= DWC3_DCTL_KEEP_CONNECT;
1820
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001821 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001822 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001824
1825 if (dwc->has_hibernation && !suspend)
1826 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1827
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001828 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001829 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001830
Thinh Nguyen5b738212019-10-23 19:15:43 -07001831 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001832
1833 do {
1834 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001835 reg &= DWC3_DSTS_DEVCTRLHLT;
1836 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001837
1838 if (!timeout)
1839 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001840
Pratyush Anand6f17f742012-07-02 10:21:55 +05301841 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001842}
1843
1844static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1845{
1846 struct dwc3 *dwc = gadget_to_dwc(g);
1847 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301848 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001849
1850 is_on = !!is_on;
1851
Baolin Wangbb014732016-10-14 17:11:33 +08001852 /*
1853 * Per databook, when we want to stop the gadget, if a control transfer
1854 * is still in process, complete it and get the core into setup phase.
1855 */
1856 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1857 reinit_completion(&dwc->ep0_in_setup);
1858
1859 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1860 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1861 if (ret == 0) {
1862 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1863 return -ETIMEDOUT;
1864 }
1865 }
1866
Felipe Balbi72246da2011-08-19 18:10:58 +03001867 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001868 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001869 spin_unlock_irqrestore(&dwc->lock, flags);
1870
Pratyush Anand6f17f742012-07-02 10:21:55 +05301871 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001872}
1873
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001874static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1875{
1876 u32 reg;
1877
1878 /* Enable all but Start and End of Frame IRQs */
1879 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1880 DWC3_DEVTEN_EVNTOVERFLOWEN |
1881 DWC3_DEVTEN_CMDCMPLTEN |
1882 DWC3_DEVTEN_ERRTICERREN |
1883 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001884 DWC3_DEVTEN_CONNECTDONEEN |
1885 DWC3_DEVTEN_USBRSTEN |
1886 DWC3_DEVTEN_DISCONNEVTEN);
1887
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001888 if (dwc->revision < DWC3_REVISION_250A)
1889 reg |= DWC3_DEVTEN_ULSTCNGEN;
1890
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001891 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1892}
1893
1894static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1895{
1896 /* mask all interrupts */
1897 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1898}
1899
1900static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001901static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001902
Felipe Balbi4e994722016-05-13 14:09:59 +03001903/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001904 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1905 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001906 *
1907 * The following looks like complex but it's actually very simple. In order to
1908 * calculate the number of packets we can burst at once on OUT transfers, we're
1909 * gonna use RxFIFO size.
1910 *
1911 * To calculate RxFIFO size we need two numbers:
1912 * MDWIDTH = size, in bits, of the internal memory bus
1913 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1914 *
1915 * Given these two numbers, the formula is simple:
1916 *
1917 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1918 *
1919 * 24 bytes is for 3x SETUP packets
1920 * 16 bytes is a clock domain crossing tolerance
1921 *
1922 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1923 */
1924static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1925{
1926 u32 ram2_depth;
1927 u32 mdwidth;
1928 u32 nump;
1929 u32 reg;
1930
1931 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1932 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1933
1934 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1935 nump = min_t(u32, nump, 16);
1936
1937 /* update NumP */
1938 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1939 reg &= ~DWC3_DCFG_NUMP_MASK;
1940 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1941 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1942}
1943
Felipe Balbid7be2952016-05-04 15:49:37 +03001944static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001945{
Felipe Balbi72246da2011-08-19 18:10:58 +03001946 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001947 int ret = 0;
1948 u32 reg;
1949
John Youncf40b862016-11-14 12:32:43 -08001950 /*
1951 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1952 * the core supports IMOD, disable it.
1953 */
1954 if (dwc->imod_interval) {
1955 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1956 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1957 } else if (dwc3_has_imod(dwc)) {
1958 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1959 }
1960
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001961 /*
1962 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1963 * field instead of letting dwc3 itself calculate that automatically.
1964 *
1965 * This way, we maximize the chances that we'll be able to get several
1966 * bursts of data without going through any sort of endpoint throttling.
1967 */
1968 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001969 if (dwc3_is_usb31(dwc))
1970 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1971 else
1972 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1973
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001974 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1975
Felipe Balbi4e994722016-05-13 14:09:59 +03001976 dwc3_gadget_setup_nump(dwc);
1977
Felipe Balbi72246da2011-08-19 18:10:58 +03001978 /* Start with SuperSpeed Default */
1979 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1980
1981 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001982 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 if (ret) {
1984 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001985 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 }
1987
1988 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001989 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001990 if (ret) {
1991 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001992 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001993 }
1994
1995 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001996 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08001997 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998 dwc3_ep0_out_start(dwc);
1999
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002000 dwc3_gadget_enable_irq(dwc);
2001
Felipe Balbid7be2952016-05-04 15:49:37 +03002002 return 0;
2003
2004err1:
2005 __dwc3_gadget_ep_disable(dwc->eps[0]);
2006
2007err0:
2008 return ret;
2009}
2010
2011static int dwc3_gadget_start(struct usb_gadget *g,
2012 struct usb_gadget_driver *driver)
2013{
2014 struct dwc3 *dwc = gadget_to_dwc(g);
2015 unsigned long flags;
2016 int ret = 0;
2017 int irq;
2018
Roger Quadros9522def2016-06-10 14:48:38 +03002019 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002020 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2021 IRQF_SHARED, "dwc3", dwc->ev_buf);
2022 if (ret) {
2023 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2024 irq, ret);
2025 goto err0;
2026 }
2027
2028 spin_lock_irqsave(&dwc->lock, flags);
2029 if (dwc->gadget_driver) {
2030 dev_err(dwc->dev, "%s is already bound to %s\n",
2031 dwc->gadget.name,
2032 dwc->gadget_driver->driver.name);
2033 ret = -EBUSY;
2034 goto err1;
2035 }
2036
2037 dwc->gadget_driver = driver;
2038
Felipe Balbifc8bb912016-05-16 13:14:48 +03002039 if (pm_runtime_active(dwc->dev))
2040 __dwc3_gadget_start(dwc);
2041
Felipe Balbi72246da2011-08-19 18:10:58 +03002042 spin_unlock_irqrestore(&dwc->lock, flags);
2043
2044 return 0;
2045
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002046err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002047 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002048 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002049
2050err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002051 return ret;
2052}
2053
Felipe Balbid7be2952016-05-04 15:49:37 +03002054static void __dwc3_gadget_stop(struct dwc3 *dwc)
2055{
2056 dwc3_gadget_disable_irq(dwc);
2057 __dwc3_gadget_ep_disable(dwc->eps[0]);
2058 __dwc3_gadget_ep_disable(dwc->eps[1]);
2059}
2060
Felipe Balbi22835b82014-10-17 12:05:12 -05002061static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002062{
2063 struct dwc3 *dwc = gadget_to_dwc(g);
2064 unsigned long flags;
2065
2066 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08002067
2068 if (pm_runtime_suspended(dwc->dev))
2069 goto out;
2070
Felipe Balbid7be2952016-05-04 15:49:37 +03002071 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08002072
Baolin Wang76a638f2016-10-31 19:38:36 +08002073out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002074 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002075 spin_unlock_irqrestore(&dwc->lock, flags);
2076
Felipe Balbi3f308d12016-05-16 14:17:06 +03002077 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002078
Felipe Balbi72246da2011-08-19 18:10:58 +03002079 return 0;
2080}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002081
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302082static void dwc3_gadget_config_params(struct usb_gadget *g,
2083 struct usb_dcd_config_params *params)
2084{
2085 struct dwc3 *dwc = gadget_to_dwc(g);
2086
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002087 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2088 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2089
2090 /* Recommended BESL */
2091 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002092 /*
2093 * If the recommended BESL baseline is 0 or if the BESL deep is
2094 * less than 2, Microsoft's Windows 10 host usb stack will issue
2095 * a usb reset immediately after it receives the extended BOS
2096 * descriptor and the enumeration will fail. To maintain
2097 * compatibility with the Windows' usb stack, let's set the
2098 * recommended BESL baseline to 1 and clamp the BESL deep to be
2099 * within 2 to 15.
2100 */
2101 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002102 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002103 params->besl_deep =
2104 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002105 }
2106
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302107 /* U1 Device exit Latency */
2108 if (dwc->dis_u1_entry_quirk)
2109 params->bU1devExitLat = 0;
2110 else
2111 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2112
2113 /* U2 Device exit Latency */
2114 if (dwc->dis_u2_entry_quirk)
2115 params->bU2DevExitLat = 0;
2116 else
2117 params->bU2DevExitLat =
2118 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2119}
2120
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002121static void dwc3_gadget_set_speed(struct usb_gadget *g,
2122 enum usb_device_speed speed)
2123{
2124 struct dwc3 *dwc = gadget_to_dwc(g);
2125 unsigned long flags;
2126 u32 reg;
2127
2128 spin_lock_irqsave(&dwc->lock, flags);
2129 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2130 reg &= ~(DWC3_DCFG_SPEED_MASK);
2131
2132 /*
2133 * WORKAROUND: DWC3 revision < 2.20a have an issue
2134 * which would cause metastability state on Run/Stop
2135 * bit if we try to force the IP to USB2-only mode.
2136 *
2137 * Because of that, we cannot configure the IP to any
2138 * speed other than the SuperSpeed
2139 *
2140 * Refers to:
2141 *
2142 * STAR#9000525659: Clock Domain Crossing on DCTL in
2143 * USB 2.0 Mode
2144 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002145 if (dwc->revision < DWC3_REVISION_220A &&
2146 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002147 reg |= DWC3_DCFG_SUPERSPEED;
2148 } else {
2149 switch (speed) {
2150 case USB_SPEED_LOW:
2151 reg |= DWC3_DCFG_LOWSPEED;
2152 break;
2153 case USB_SPEED_FULL:
2154 reg |= DWC3_DCFG_FULLSPEED;
2155 break;
2156 case USB_SPEED_HIGH:
2157 reg |= DWC3_DCFG_HIGHSPEED;
2158 break;
2159 case USB_SPEED_SUPER:
2160 reg |= DWC3_DCFG_SUPERSPEED;
2161 break;
2162 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002163 if (dwc3_is_usb31(dwc))
2164 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2165 else
2166 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002167 break;
2168 default:
2169 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2170
2171 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2172 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2173 else
2174 reg |= DWC3_DCFG_SUPERSPEED;
2175 }
2176 }
2177 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2178
2179 spin_unlock_irqrestore(&dwc->lock, flags);
2180}
2181
Felipe Balbi72246da2011-08-19 18:10:58 +03002182static const struct usb_gadget_ops dwc3_gadget_ops = {
2183 .get_frame = dwc3_gadget_get_frame,
2184 .wakeup = dwc3_gadget_wakeup,
2185 .set_selfpowered = dwc3_gadget_set_selfpowered,
2186 .pullup = dwc3_gadget_pullup,
2187 .udc_start = dwc3_gadget_start,
2188 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002189 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302190 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002191};
2192
2193/* -------------------------------------------------------------------------- */
2194
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002195static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2196{
2197 struct dwc3 *dwc = dep->dwc;
2198
2199 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2200 dep->endpoint.maxburst = 1;
2201 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2202 if (!dep->direction)
2203 dwc->gadget.ep0 = &dep->endpoint;
2204
2205 dep->endpoint.caps.type_control = true;
2206
2207 return 0;
2208}
2209
2210static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2211{
2212 struct dwc3 *dwc = dep->dwc;
2213 int mdwidth;
2214 int kbytes;
2215 int size;
2216
2217 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2218 /* MDWIDTH is represented in bits, we need it in bytes */
2219 mdwidth /= 8;
2220
2221 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2222 if (dwc3_is_usb31(dwc))
2223 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2224 else
2225 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2226
2227 /* FIFO Depth is in MDWDITH bytes. Multiply */
2228 size *= mdwidth;
2229
2230 kbytes = size / 1024;
2231 if (kbytes == 0)
2232 kbytes = 1;
2233
2234 /*
2235 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2236 * internal overhead. We don't really know how these are used,
2237 * but documentation say it exists.
2238 */
2239 size -= mdwidth * (kbytes + 1);
2240 size /= kbytes;
2241
2242 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2243
2244 dep->endpoint.max_streams = 15;
2245 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2246 list_add_tail(&dep->endpoint.ep_list,
2247 &dwc->gadget.ep_list);
2248 dep->endpoint.caps.type_iso = true;
2249 dep->endpoint.caps.type_bulk = true;
2250 dep->endpoint.caps.type_int = true;
2251
2252 return dwc3_alloc_trb_pool(dep);
2253}
2254
2255static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2256{
2257 struct dwc3 *dwc = dep->dwc;
2258
2259 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2260 dep->endpoint.max_streams = 15;
2261 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2262 list_add_tail(&dep->endpoint.ep_list,
2263 &dwc->gadget.ep_list);
2264 dep->endpoint.caps.type_iso = true;
2265 dep->endpoint.caps.type_bulk = true;
2266 dep->endpoint.caps.type_int = true;
2267
2268 return dwc3_alloc_trb_pool(dep);
2269}
2270
2271static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002272{
2273 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002274 bool direction = epnum & 1;
2275 int ret;
2276 u8 num = epnum >> 1;
2277
2278 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2279 if (!dep)
2280 return -ENOMEM;
2281
2282 dep->dwc = dwc;
2283 dep->number = epnum;
2284 dep->direction = direction;
2285 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2286 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002287 dep->combo_num = 0;
2288 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002289
2290 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2291 direction ? "in" : "out");
2292
2293 dep->endpoint.name = dep->name;
2294
2295 if (!(dep->number > 1)) {
2296 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2297 dep->endpoint.comp_desc = NULL;
2298 }
2299
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002300 if (num == 0)
2301 ret = dwc3_gadget_init_control_endpoint(dep);
2302 else if (direction)
2303 ret = dwc3_gadget_init_in_endpoint(dep);
2304 else
2305 ret = dwc3_gadget_init_out_endpoint(dep);
2306
2307 if (ret)
2308 return ret;
2309
2310 dep->endpoint.caps.dir_in = direction;
2311 dep->endpoint.caps.dir_out = !direction;
2312
2313 INIT_LIST_HEAD(&dep->pending_list);
2314 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002315 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002316
2317 return 0;
2318}
2319
2320static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2321{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002322 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002323
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002324 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2325
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002326 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002327 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002328
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002329 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2330 if (ret)
2331 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002332 }
2333
2334 return 0;
2335}
2336
2337static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2338{
2339 struct dwc3_ep *dep;
2340 u8 epnum;
2341
2342 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2343 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002344 if (!dep)
2345 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302346 /*
2347 * Physical endpoints 0 and 1 are special; they form the
2348 * bi-directional USB endpoint 0.
2349 *
2350 * For those two physical endpoints, we don't allocate a TRB
2351 * pool nor do we add them the endpoints list. Due to that, we
2352 * shouldn't do these two operations otherwise we would end up
2353 * with all sorts of bugs when removing dwc3.ko.
2354 */
2355 if (epnum != 0 && epnum != 1) {
2356 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002357 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302358 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002359
2360 kfree(dep);
2361 }
2362}
2363
Felipe Balbi72246da2011-08-19 18:10:58 +03002364/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002365
Felipe Balbi8f608e82018-03-27 10:53:29 +03002366static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2367 struct dwc3_request *req, struct dwc3_trb *trb,
2368 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302369{
2370 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302371
Felipe Balbidc55c672016-08-12 13:20:32 +03002372 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002373
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002374 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002375 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002376
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002377 /*
2378 * If we're in the middle of series of chained TRBs and we
2379 * receive a short transfer along the way, DWC3 will skip
2380 * through all TRBs including the last TRB in the chain (the
2381 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2382 * bit and SW has to do it manually.
2383 *
2384 * We're going to do that here to avoid problems of HW trying
2385 * to use bogus TRBs for transfers.
2386 */
2387 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2388 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2389
Felipe Balbic6267a52017-01-05 14:58:46 +02002390 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002391 * For isochronous transfers, the first TRB in a service interval must
2392 * have the Isoc-First type. Track and report its interval frame number.
2393 */
2394 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2395 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2396 unsigned int frame_number;
2397
2398 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2399 frame_number &= ~(dep->interval - 1);
2400 req->request.frame_number = frame_number;
2401 }
2402
2403 /*
Felipe Balbic6267a52017-01-05 14:58:46 +02002404 * If we're dealing with unaligned size OUT transfer, we will be left
2405 * with one TRB pending in the ring. We need to manually clear HWO bit
2406 * from that TRB.
2407 */
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002408
2409 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002410 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2411 return 1;
2412 }
2413
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302414 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002415 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302416
Felipe Balbi35b27192017-03-08 13:56:37 +02002417 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2418 return 1;
2419
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002420 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302421 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002422
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002423 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302424 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002425
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302426 return 0;
2427}
2428
Felipe Balbid3692952018-03-29 13:32:10 +03002429static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2430 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2431 int status)
2432{
2433 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2434 struct scatterlist *sg = req->sg;
2435 struct scatterlist *s;
2436 unsigned int pending = req->num_pending_sgs;
2437 unsigned int i;
2438 int ret = 0;
2439
2440 for_each_sg(sg, s, pending, i) {
2441 trb = &dep->trb_pool[dep->trb_dequeue];
2442
2443 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2444 break;
2445
2446 req->sg = sg_next(s);
2447 req->num_pending_sgs--;
2448
2449 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2450 trb, event, status, true);
2451 if (ret)
2452 break;
2453 }
2454
2455 return ret;
2456}
2457
2458static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2459 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2460 int status)
2461{
2462 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2463
2464 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2465 event, status, false);
2466}
2467
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002468static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2469{
Thinh Nguyenea0d7622019-12-13 18:40:45 -08002470 /*
2471 * For OUT direction, host may send less than the setup
2472 * length. Return true for all OUT requests.
2473 */
2474 if (!req->direction)
2475 return true;
2476
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002477 return req->request.actual == req->request.length;
2478}
2479
Felipe Balbif38e35d2018-04-06 15:56:35 +03002480static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2481 const struct dwc3_event_depevt *event,
2482 struct dwc3_request *req, int status)
2483{
2484 int ret;
2485
2486 if (req->num_pending_sgs)
2487 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2488 status);
2489 else
2490 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2491 status);
2492
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002493 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002494 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2495 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002496 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002497 }
2498
2499 req->request.actual = req->request.length - req->remaining;
2500
Tejas Joglekar8c7d4b72019-11-13 11:45:16 +05302501 if (!dwc3_gadget_ep_request_completed(req) ||
Felipe Balbif38e35d2018-04-06 15:56:35 +03002502 req->num_pending_sgs) {
2503 __dwc3_gadget_kick_transfer(dep);
2504 goto out;
2505 }
2506
2507 dwc3_gadget_giveback(dep, req, status);
2508
2509out:
2510 return ret;
2511}
2512
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002513static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002514 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002515{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002516 struct dwc3_request *req;
2517 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002518
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002519 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002520 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002521
Felipe Balbif38e35d2018-04-06 15:56:35 +03002522 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2523 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002524 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002525 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002526 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002527}
2528
Felipe Balbiee3638b2018-03-27 11:26:53 +03002529static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2530 const struct dwc3_event_depevt *event)
2531{
Felipe Balbif62afb42018-04-11 10:34:34 +03002532 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002533}
2534
Felipe Balbi8f608e82018-03-27 10:53:29 +03002535static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2536 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002537{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002538 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002539 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002540 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002541
Felipe Balbiee3638b2018-03-27 11:26:53 +03002542 dwc3_gadget_endpoint_frame_from_event(dep, event);
2543
Felipe Balbi72246da2011-08-19 18:10:58 +03002544 if (event->status & DEPEVT_STATUS_BUSERR)
2545 status = -ECONNRESET;
2546
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002547 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2548 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002549
2550 if (list_empty(&dep->started_list))
2551 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002552 }
2553
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002554 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002555
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002556 if (stop) {
Felipe Balbic5353b22019-02-13 13:00:54 +02002557 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002558 dep->flags = DWC3_EP_ENABLED;
2559 }
2560
Felipe Balbifae2b902011-10-14 13:00:30 +03002561 /*
2562 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2563 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2564 */
2565 if (dwc->revision < DWC3_REVISION_183A) {
2566 u32 reg;
2567 int i;
2568
2569 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002570 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002571
2572 if (!(dep->flags & DWC3_EP_ENABLED))
2573 continue;
2574
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002575 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002576 return;
2577 }
2578
2579 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2580 reg |= dwc->u1u2;
2581 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2582
2583 dwc->u1u2 = 0;
2584 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002585}
2586
Felipe Balbi8f608e82018-03-27 10:53:29 +03002587static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2588 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002589{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002590 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi25abad62018-08-14 10:41:19 +03002591 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002592}
2593
Felipe Balbi72246da2011-08-19 18:10:58 +03002594static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2595 const struct dwc3_event_depevt *event)
2596{
2597 struct dwc3_ep *dep;
2598 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002599 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002600
2601 dep = dwc->eps[epnum];
2602
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002603 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02002604 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002605 return;
2606
2607 /* Handle only EPCMDCMPLT when EP disabled */
2608 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2609 return;
2610 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002611
Felipe Balbi72246da2011-08-19 18:10:58 +03002612 if (epnum == 0 || epnum == 1) {
2613 dwc3_ep0_interrupt(dwc, event);
2614 return;
2615 }
2616
2617 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002618 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002619 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002620 break;
2621 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002622 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002623 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002624 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002625 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2626
2627 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02002628 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Felipe Balbifec90952018-08-01 13:56:50 +03002629 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
Baolin Wang76a638f2016-10-31 19:38:36 +08002630 }
2631 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002632 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002633 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002634 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002635 break;
2636 }
2637}
2638
2639static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2640{
2641 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2642 spin_unlock(&dwc->lock);
2643 dwc->gadget_driver->disconnect(&dwc->gadget);
2644 spin_lock(&dwc->lock);
2645 }
2646}
2647
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002648static void dwc3_suspend_gadget(struct dwc3 *dwc)
2649{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002650 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002651 spin_unlock(&dwc->lock);
2652 dwc->gadget_driver->suspend(&dwc->gadget);
2653 spin_lock(&dwc->lock);
2654 }
2655}
2656
2657static void dwc3_resume_gadget(struct dwc3 *dwc)
2658{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002659 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002660 spin_unlock(&dwc->lock);
2661 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002662 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002663 }
2664}
2665
2666static void dwc3_reset_gadget(struct dwc3 *dwc)
2667{
2668 if (!dwc->gadget_driver)
2669 return;
2670
2671 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2672 spin_unlock(&dwc->lock);
2673 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002674 spin_lock(&dwc->lock);
2675 }
2676}
2677
Felipe Balbic5353b22019-02-13 13:00:54 +02002678static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
2679 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002680{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002681 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 struct dwc3_gadget_ep_cmd_params params;
2683 u32 cmd;
2684 int ret;
2685
Felipe Balbi3aec9912019-01-21 13:08:44 +02002686 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302687 return;
2688
Pratyush Anand57911502012-07-06 15:19:10 +05302689 /*
2690 * NOTICE: We are violating what the Databook says about the
2691 * EndTransfer command. Ideally we would _always_ wait for the
2692 * EndTransfer Command Completion IRQ, but that's causing too
2693 * much trouble synchronizing between us and gadget driver.
2694 *
2695 * We have discussed this with the IP Provider and it was
2696 * suggested to giveback all requests here, but give HW some
2697 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002698 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302699 *
2700 * Note also that a similar handling was tested by Synopsys
2701 * (thanks a lot Paul) and nothing bad has come out of it.
2702 * In short, what we're doing is:
2703 *
2704 * - Issue EndTransfer WITH CMDIOC bit set
2705 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002706 *
2707 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2708 * supports a mode to work around the above limitation. The
2709 * software can poll the CMDACT bit in the DEPCMD register
2710 * after issuing a EndTransfer command. This mode is enabled
2711 * by writing GUCTL2[14]. This polling is already done in the
2712 * dwc3_send_gadget_ep_cmd() function so if the mode is
2713 * enabled, the EndTransfer command will have completed upon
2714 * returning from this function and we don't need to delay for
2715 * 100us.
2716 *
2717 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302718 */
2719
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302720 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002721 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02002722 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03002723 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302724 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002725 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302726 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002727 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002728
Thinh Nguyend3abda52019-11-27 13:10:47 -08002729 if (!interrupt)
2730 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2731
Felipe Balbi3aec9912019-01-21 13:08:44 +02002732 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
John Youn06281d42016-08-22 15:39:13 -07002733 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002734}
2735
Felipe Balbi72246da2011-08-19 18:10:58 +03002736static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2737{
2738 u32 epnum;
2739
2740 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2741 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002742 int ret;
2743
2744 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002745 if (!dep)
2746 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002747
2748 if (!(dep->flags & DWC3_EP_STALL))
2749 continue;
2750
2751 dep->flags &= ~DWC3_EP_STALL;
2752
John Youn50c763f2016-05-31 17:49:56 -07002753 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002754 WARN_ON_ONCE(ret);
2755 }
2756}
2757
2758static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2759{
Felipe Balbic4430a22012-05-24 10:30:01 +03002760 int reg;
2761
Felipe Balbi72246da2011-08-19 18:10:58 +03002762 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2763 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03002764 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07002765 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002766
Felipe Balbi72246da2011-08-19 18:10:58 +03002767 dwc3_disconnect_gadget(dwc);
2768
2769 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002770 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002771 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002772
2773 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002774}
2775
Felipe Balbi72246da2011-08-19 18:10:58 +03002776static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2777{
2778 u32 reg;
2779
Felipe Balbifc8bb912016-05-16 13:14:48 +03002780 dwc->connected = true;
2781
Felipe Balbidf62df52011-10-14 15:11:49 +03002782 /*
2783 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2784 * would cause a missing Disconnect Event if there's a
2785 * pending Setup Packet in the FIFO.
2786 *
2787 * There's no suggested workaround on the official Bug
2788 * report, which states that "unless the driver/application
2789 * is doing any special handling of a disconnect event,
2790 * there is no functional issue".
2791 *
2792 * Unfortunately, it turns out that we _do_ some special
2793 * handling of a disconnect event, namely complete all
2794 * pending transfers, notify gadget driver of the
2795 * disconnection, and so on.
2796 *
2797 * Our suggested workaround is to follow the Disconnect
2798 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002799 * flag. Such flag gets set whenever we have a SETUP_PENDING
2800 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002801 * same endpoint.
2802 *
2803 * Refers to:
2804 *
2805 * STAR#9000466709: RTL: Device : Disconnect event not
2806 * generated if setup packet pending in FIFO
2807 */
2808 if (dwc->revision < DWC3_REVISION_188A) {
2809 if (dwc->setup_packet_pending)
2810 dwc3_gadget_disconnect_interrupt(dwc);
2811 }
2812
Felipe Balbi8e744752014-11-06 14:27:53 +08002813 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002814
2815 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2816 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07002817 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002818 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002819 dwc3_clear_stall_all_ep(dwc);
2820
2821 /* Reset device address to zero */
2822 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2823 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2824 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002825}
2826
Felipe Balbi72246da2011-08-19 18:10:58 +03002827static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2828{
Felipe Balbi72246da2011-08-19 18:10:58 +03002829 struct dwc3_ep *dep;
2830 int ret;
2831 u32 reg;
2832 u8 speed;
2833
Felipe Balbi72246da2011-08-19 18:10:58 +03002834 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2835 speed = reg & DWC3_DSTS_CONNECTSPD;
2836 dwc->speed = speed;
2837
John Youn5fb6fda2016-11-10 17:23:25 -08002838 /*
2839 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2840 * each time on Connect Done.
2841 *
2842 * Currently we always use the reset value. If any platform
2843 * wants to set this to a different value, we need to add a
2844 * setting and update GCTL.RAMCLKSEL here.
2845 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002846
2847 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002848 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002849 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2850 dwc->gadget.ep0->maxpacket = 512;
2851 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2852 break;
John Youn2da9ad72016-05-20 16:34:26 -07002853 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002854 /*
2855 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2856 * would cause a missing USB3 Reset event.
2857 *
2858 * In such situations, we should force a USB3 Reset
2859 * event by calling our dwc3_gadget_reset_interrupt()
2860 * routine.
2861 *
2862 * Refers to:
2863 *
2864 * STAR#9000483510: RTL: SS : USB3 reset event may
2865 * not be generated always when the link enters poll
2866 */
2867 if (dwc->revision < DWC3_REVISION_190A)
2868 dwc3_gadget_reset_interrupt(dwc);
2869
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2871 dwc->gadget.ep0->maxpacket = 512;
2872 dwc->gadget.speed = USB_SPEED_SUPER;
2873 break;
John Youn2da9ad72016-05-20 16:34:26 -07002874 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002875 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2876 dwc->gadget.ep0->maxpacket = 64;
2877 dwc->gadget.speed = USB_SPEED_HIGH;
2878 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002879 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002880 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2881 dwc->gadget.ep0->maxpacket = 64;
2882 dwc->gadget.speed = USB_SPEED_FULL;
2883 break;
John Youn2da9ad72016-05-20 16:34:26 -07002884 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002885 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2886 dwc->gadget.ep0->maxpacket = 8;
2887 dwc->gadget.speed = USB_SPEED_LOW;
2888 break;
2889 }
2890
Thinh Nguyen61800262018-01-12 18:18:05 -08002891 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2892
Pratyush Anand2b758352013-01-14 15:59:31 +05302893 /* Enable USB2 LPM Capability */
2894
John Younee5cd412016-02-05 17:08:45 -08002895 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002896 (speed != DWC3_DSTS_SUPERSPEED) &&
2897 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302898 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2899 reg |= DWC3_DCFG_LPM_CAP;
2900 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2901
2902 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2903 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2904
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07002905 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
2906 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05302907
Huang Rui80caf7d2014-10-28 19:54:26 +08002908 /*
2909 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2910 * DCFG.LPMCap is set, core responses with an ACK and the
2911 * BESL value in the LPM token is less than or equal to LPM
2912 * NYET threshold.
2913 */
2914 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2915 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002916 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002917
2918 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
Thinh Nguyen2e487d22019-04-25 13:55:30 -07002919 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08002920
Thinh Nguyen5b738212019-10-23 19:15:43 -07002921 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002922 } else {
2923 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2924 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07002925 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302926 }
2927
Felipe Balbi72246da2011-08-19 18:10:58 +03002928 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002929 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002930 if (ret) {
2931 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2932 return;
2933 }
2934
2935 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002936 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002937 if (ret) {
2938 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2939 return;
2940 }
2941
2942 /*
2943 * Configure PHY via GUSB3PIPECTLn if required.
2944 *
2945 * Update GTXFIFOSIZn
2946 *
2947 * In both cases reset values should be sufficient.
2948 */
2949}
2950
2951static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2952{
Felipe Balbi72246da2011-08-19 18:10:58 +03002953 /*
2954 * TODO take core out of low power mode when that's
2955 * implemented.
2956 */
2957
Jiebing Liad14d4e2014-12-11 13:26:29 +08002958 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2959 spin_unlock(&dwc->lock);
2960 dwc->gadget_driver->resume(&dwc->gadget);
2961 spin_lock(&dwc->lock);
2962 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002963}
2964
2965static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2966 unsigned int evtinfo)
2967{
Felipe Balbifae2b902011-10-14 13:00:30 +03002968 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002969 unsigned int pwropt;
2970
2971 /*
2972 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2973 * Hibernation mode enabled which would show up when device detects
2974 * host-initiated U3 exit.
2975 *
2976 * In that case, device will generate a Link State Change Interrupt
2977 * from U3 to RESUME which is only necessary if Hibernation is
2978 * configured in.
2979 *
2980 * There are no functional changes due to such spurious event and we
2981 * just need to ignore it.
2982 *
2983 * Refers to:
2984 *
2985 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2986 * operational mode
2987 */
2988 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2989 if ((dwc->revision < DWC3_REVISION_250A) &&
2990 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2991 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2992 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002993 return;
2994 }
2995 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002996
2997 /*
2998 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2999 * on the link partner, the USB session might do multiple entry/exit
3000 * of low power states before a transfer takes place.
3001 *
3002 * Due to this problem, we might experience lower throughput. The
3003 * suggested workaround is to disable DCTL[12:9] bits if we're
3004 * transitioning from U1/U2 to U0 and enable those bits again
3005 * after a transfer completes and there are no pending transfers
3006 * on any of the enabled endpoints.
3007 *
3008 * This is the first half of that workaround.
3009 *
3010 * Refers to:
3011 *
3012 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3013 * core send LGO_Ux entering U0
3014 */
3015 if (dwc->revision < DWC3_REVISION_183A) {
3016 if (next == DWC3_LINK_STATE_U0) {
3017 u32 u1u2;
3018 u32 reg;
3019
3020 switch (dwc->link_state) {
3021 case DWC3_LINK_STATE_U1:
3022 case DWC3_LINK_STATE_U2:
3023 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3024 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3025 | DWC3_DCTL_ACCEPTU2ENA
3026 | DWC3_DCTL_INITU1ENA
3027 | DWC3_DCTL_ACCEPTU1ENA);
3028
3029 if (!dwc->u1u2)
3030 dwc->u1u2 = reg & u1u2;
3031
3032 reg &= ~u1u2;
3033
Thinh Nguyen5b738212019-10-23 19:15:43 -07003034 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003035 break;
3036 default:
3037 /* do nothing */
3038 break;
3039 }
3040 }
3041 }
3042
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003043 switch (next) {
3044 case DWC3_LINK_STATE_U1:
3045 if (dwc->speed == USB_SPEED_SUPER)
3046 dwc3_suspend_gadget(dwc);
3047 break;
3048 case DWC3_LINK_STATE_U2:
3049 case DWC3_LINK_STATE_U3:
3050 dwc3_suspend_gadget(dwc);
3051 break;
3052 case DWC3_LINK_STATE_RESUME:
3053 dwc3_resume_gadget(dwc);
3054 break;
3055 default:
3056 /* do nothing */
3057 break;
3058 }
3059
Felipe Balbie57ebc12014-04-22 13:20:12 -05003060 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003061}
3062
Baolin Wang72704f82016-05-16 16:43:53 +08003063static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3064 unsigned int evtinfo)
3065{
3066 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3067
3068 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3069 dwc3_suspend_gadget(dwc);
3070
3071 dwc->link_state = next;
3072}
3073
Felipe Balbie1dadd32014-02-25 14:47:54 -06003074static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3075 unsigned int evtinfo)
3076{
3077 unsigned int is_ss = evtinfo & BIT(4);
3078
Felipe Balbibfad65e2017-04-19 14:59:27 +03003079 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003080 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3081 * have a known issue which can cause USB CV TD.9.23 to fail
3082 * randomly.
3083 *
3084 * Because of this issue, core could generate bogus hibernation
3085 * events which SW needs to ignore.
3086 *
3087 * Refers to:
3088 *
3089 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3090 * Device Fallback from SuperSpeed
3091 */
3092 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3093 return;
3094
3095 /* enter hibernation here */
3096}
3097
Felipe Balbi72246da2011-08-19 18:10:58 +03003098static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3099 const struct dwc3_event_devt *event)
3100{
3101 switch (event->type) {
3102 case DWC3_DEVICE_EVENT_DISCONNECT:
3103 dwc3_gadget_disconnect_interrupt(dwc);
3104 break;
3105 case DWC3_DEVICE_EVENT_RESET:
3106 dwc3_gadget_reset_interrupt(dwc);
3107 break;
3108 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3109 dwc3_gadget_conndone_interrupt(dwc);
3110 break;
3111 case DWC3_DEVICE_EVENT_WAKEUP:
3112 dwc3_gadget_wakeup_interrupt(dwc);
3113 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003114 case DWC3_DEVICE_EVENT_HIBER_REQ:
3115 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3116 "unexpected hibernation event\n"))
3117 break;
3118
3119 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3120 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003121 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3122 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3123 break;
3124 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003125 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003126 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08003127 /*
3128 * Ignore suspend event until the gadget enters into
3129 * USB_STATE_CONFIGURED state.
3130 */
3131 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3132 dwc3_gadget_suspend_interrupt(dwc,
3133 event->event_info);
3134 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003135 break;
3136 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003137 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003138 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003139 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003140 break;
3141 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003142 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003143 }
3144}
3145
3146static void dwc3_process_event_entry(struct dwc3 *dwc,
3147 const union dwc3_event *event)
3148{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003149 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003150
Felipe Balbidfc5e802017-04-26 13:44:51 +03003151 if (!event->type.is_devspec)
3152 dwc3_endpoint_interrupt(dwc, &event->depevt);
3153 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003154 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003155 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003156 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003157}
3158
Felipe Balbidea520a2016-03-30 09:39:34 +03003159static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003160{
Felipe Balbidea520a2016-03-30 09:39:34 +03003161 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003162 irqreturn_t ret = IRQ_NONE;
3163 int left;
3164 u32 reg;
3165
Felipe Balbif42f2442013-06-12 21:25:08 +03003166 left = evt->count;
3167
3168 if (!(evt->flags & DWC3_EVENT_PENDING))
3169 return IRQ_NONE;
3170
3171 while (left > 0) {
3172 union dwc3_event event;
3173
John Younebbb2d52016-11-15 13:07:02 +02003174 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003175
3176 dwc3_process_event_entry(dwc, &event);
3177
3178 /*
3179 * FIXME we wrap around correctly to the next entry as
3180 * almost all entries are 4 bytes in size. There is one
3181 * entry which has 12 bytes which is a regular entry
3182 * followed by 8 bytes data. ATM I don't know how
3183 * things are organized if we get next to the a
3184 * boundary so I worry about that once we try to handle
3185 * that.
3186 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003187 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003188 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003189 }
3190
3191 evt->count = 0;
3192 evt->flags &= ~DWC3_EVENT_PENDING;
3193 ret = IRQ_HANDLED;
3194
3195 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003196 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003197 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003198 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003199
John Youncf40b862016-11-14 12:32:43 -08003200 if (dwc->imod_interval) {
3201 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3202 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3203 }
3204
Felipe Balbif42f2442013-06-12 21:25:08 +03003205 return ret;
3206}
3207
Felipe Balbidea520a2016-03-30 09:39:34 +03003208static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003209{
Felipe Balbidea520a2016-03-30 09:39:34 +03003210 struct dwc3_event_buffer *evt = _evt;
3211 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003212 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003213 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003214
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003215 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003216 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003217 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003218
3219 return ret;
3220}
3221
Felipe Balbidea520a2016-03-30 09:39:34 +03003222static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003223{
Felipe Balbidea520a2016-03-30 09:39:34 +03003224 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003225 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003226 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003227 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003228
Felipe Balbifc8bb912016-05-16 13:14:48 +03003229 if (pm_runtime_suspended(dwc->dev)) {
3230 pm_runtime_get(dwc->dev);
3231 disable_irq_nosync(dwc->irq_gadget);
3232 dwc->pending_events = true;
3233 return IRQ_HANDLED;
3234 }
3235
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003236 /*
3237 * With PCIe legacy interrupt, test shows that top-half irq handler can
3238 * be called again after HW interrupt deassertion. Check if bottom-half
3239 * irq event handler completes before caching new event to prevent
3240 * losing events.
3241 */
3242 if (evt->flags & DWC3_EVENT_PENDING)
3243 return IRQ_HANDLED;
3244
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003245 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003246 count &= DWC3_GEVNTCOUNT_MASK;
3247 if (!count)
3248 return IRQ_NONE;
3249
Felipe Balbib15a7622011-06-30 16:57:15 +03003250 evt->count = count;
3251 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003252
Felipe Balbie8adfc32013-06-12 21:11:14 +03003253 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003254 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003255 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003256 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003257
John Younebbb2d52016-11-15 13:07:02 +02003258 amount = min(count, evt->length - evt->lpos);
3259 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3260
3261 if (amount < count)
3262 memcpy(evt->cache, evt->buf, count - amount);
3263
John Youn65aca322016-11-15 13:08:59 +02003264 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3265
Felipe Balbib15a7622011-06-30 16:57:15 +03003266 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003267}
3268
Felipe Balbidea520a2016-03-30 09:39:34 +03003269static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003270{
Felipe Balbidea520a2016-03-30 09:39:34 +03003271 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003272
Felipe Balbidea520a2016-03-30 09:39:34 +03003273 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003274}
3275
Felipe Balbi6db38122016-10-03 11:27:01 +03003276static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3277{
3278 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3279 int irq;
3280
Hans de Goedef146b402019-10-05 23:04:48 +02003281 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003282 if (irq > 0)
3283 goto out;
3284
3285 if (irq == -EPROBE_DEFER)
3286 goto out;
3287
Hans de Goedef146b402019-10-05 23:04:48 +02003288 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003289 if (irq > 0)
3290 goto out;
3291
3292 if (irq == -EPROBE_DEFER)
3293 goto out;
3294
3295 irq = platform_get_irq(dwc3_pdev, 0);
3296 if (irq > 0)
3297 goto out;
3298
Felipe Balbi6db38122016-10-03 11:27:01 +03003299 if (!irq)
3300 irq = -EINVAL;
3301
3302out:
3303 return irq;
3304}
3305
Felipe Balbi72246da2011-08-19 18:10:58 +03003306/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003307 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003308 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003309 *
3310 * Returns 0 on success otherwise negative errno.
3311 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003312int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003313{
Felipe Balbi6db38122016-10-03 11:27:01 +03003314 int ret;
3315 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003316
Felipe Balbi6db38122016-10-03 11:27:01 +03003317 irq = dwc3_gadget_get_irq(dwc);
3318 if (irq < 0) {
3319 ret = irq;
3320 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003321 }
3322
3323 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003324
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303325 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3326 sizeof(*dwc->ep0_trb) * 2,
3327 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003328 if (!dwc->ep0_trb) {
3329 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3330 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003331 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003332 }
3333
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003334 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003335 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003336 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003337 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003338 }
3339
Felipe Balbi905dc042017-01-05 14:46:52 +02003340 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3341 &dwc->bounce_addr, GFP_KERNEL);
3342 if (!dwc->bounce) {
3343 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003344 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003345 }
3346
Baolin Wangbb014732016-10-14 17:11:33 +08003347 init_completion(&dwc->ep0_in_setup);
3348
Felipe Balbi72246da2011-08-19 18:10:58 +03003349 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003350 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003351 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003352 dwc->gadget.name = "dwc3-gadget";
Thinh Nguyenc7299692019-04-25 14:28:24 -07003353 dwc->gadget.lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003354
3355 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003356 * FIXME We might be setting max_speed to <SUPER, however versions
3357 * <2.20a of dwc3 have an issue with metastability (documented
3358 * elsewhere in this driver) which tells us we can't set max speed to
3359 * anything lower than SUPER.
3360 *
3361 * Because gadget.max_speed is only used by composite.c and function
3362 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3363 * to happen so we avoid sending SuperSpeed Capability descriptor
3364 * together with our BOS descriptor as that could confuse host into
3365 * thinking we can handle super speed.
3366 *
3367 * Note that, in fact, we won't even support GetBOS requests when speed
3368 * is less than super speed because we don't have means, yet, to tell
3369 * composite.c that we are USB 2.0 + LPM ECN.
3370 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003371 if (dwc->revision < DWC3_REVISION_220A &&
3372 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003373 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003374 dwc->revision);
3375
3376 dwc->gadget.max_speed = dwc->maximum_speed;
3377
3378 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003379 * REVISIT: Here we should clear all pending IRQs to be
3380 * sure we're starting from a well known location.
3381 */
3382
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003383 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003384 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003385 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003386
Felipe Balbi72246da2011-08-19 18:10:58 +03003387 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3388 if (ret) {
3389 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003390 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003391 }
3392
Roger Quadros169e3b62019-01-10 17:04:28 +02003393 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3394
Felipe Balbi72246da2011-08-19 18:10:58 +03003395 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003396
3397err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003398 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003399
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003400err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003401 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3402 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003403
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003404err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003405 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003406
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003407err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303408 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003409 dwc->ep0_trb, dwc->ep0_trb_addr);
3410
Felipe Balbi72246da2011-08-19 18:10:58 +03003411err0:
3412 return ret;
3413}
3414
Felipe Balbi7415f172012-04-30 14:56:33 +03003415/* -------------------------------------------------------------------------- */
3416
Felipe Balbi72246da2011-08-19 18:10:58 +03003417void dwc3_gadget_exit(struct dwc3 *dwc)
3418{
Felipe Balbi72246da2011-08-19 18:10:58 +03003419 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003420 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003421 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003422 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003423 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303424 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003425 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003426}
Felipe Balbi7415f172012-04-30 14:56:33 +03003427
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003428int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003429{
Roger Quadros9772b472016-04-12 11:33:29 +03003430 if (!dwc->gadget_driver)
3431 return 0;
3432
Roger Quadros1551e352017-02-15 14:16:26 +02003433 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003434 dwc3_disconnect_gadget(dwc);
3435 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003436
3437 return 0;
3438}
3439
3440int dwc3_gadget_resume(struct dwc3 *dwc)
3441{
Felipe Balbi7415f172012-04-30 14:56:33 +03003442 int ret;
3443
Roger Quadros9772b472016-04-12 11:33:29 +03003444 if (!dwc->gadget_driver)
3445 return 0;
3446
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003447 ret = __dwc3_gadget_start(dwc);
3448 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003449 goto err0;
3450
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003451 ret = dwc3_gadget_run_stop(dwc, true, false);
3452 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003453 goto err1;
3454
Felipe Balbi7415f172012-04-30 14:56:33 +03003455 return 0;
3456
3457err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003458 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003459
3460err0:
3461 return ret;
3462}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003463
3464void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3465{
3466 if (dwc->pending_events) {
3467 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3468 dwc->pending_events = false;
3469 enable_irq(dwc->irq_gadget);
3470 }
3471}