blob: 17203944d77f7fda7f566b508f6d11d649da2337 [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 Balbif62afb42018-04-11 10:34:34 +030030#define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \
31 & ~((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
60 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
61
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
177 req->started = false;
178 list_del(&req->list);
179 req->remaining = 0;
180
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 Balbi72246da2011-08-19 18:10:58 +0300211
212 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200213 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300214 spin_lock(&dwc->lock);
215}
216
Felipe Balbibfad65e2017-04-19 14:59:27 +0300217/**
218 * dwc3_send_gadget_generic_command - issue a generic command for the controller
219 * @dwc: pointer to the controller context
220 * @cmd: the command to be issued
221 * @param: command parameter
222 *
223 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
224 * and wait for its completion.
225 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500226int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300227{
228 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300229 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300230 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300231 u32 reg;
232
233 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
234 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
235
236 do {
237 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
238 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300239 status = DWC3_DGCMD_STATUS(reg);
240 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300241 ret = -EINVAL;
242 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300243 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100244 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300245
246 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300247 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300248 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300249 }
250
Felipe Balbi71f7e702016-05-23 14:16:19 +0300251 trace_dwc3_gadget_generic_cmd(cmd, param, status);
252
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300253 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300254}
255
Felipe Balbic36d8e92016-04-04 12:46:33 +0300256static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
257
Felipe Balbibfad65e2017-04-19 14:59:27 +0300258/**
259 * dwc3_send_gadget_ep_cmd - issue an endpoint command
260 * @dep: the endpoint to which the command is going to be issued
261 * @cmd: the command to be issued
262 * @params: parameters to the command
263 *
264 * Caller should handle locking. This function will issue @cmd with given
265 * @params to @dep and wait for its completion.
266 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300267int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
268 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300269{
Felipe Balbi8897a762016-09-22 10:56:08 +0300270 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300271 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000272 u32 timeout = 1000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700273 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300274 u32 reg;
275
Felipe Balbi0933df12016-05-23 14:02:33 +0300276 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300277 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300278
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300279 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700280 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
281 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
282 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300283 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700284 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
285 * settings. Restore them after the command is completed.
286 *
287 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300288 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300289 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
290 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
291 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700292 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300293 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300294 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700295
296 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
297 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
298 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
299 }
300
301 if (saved_config)
302 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300303 }
304
Felipe Balbi59999142016-09-22 12:25:28 +0300305 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300306 int needs_wakeup;
307
308 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
309 dwc->link_state == DWC3_LINK_STATE_U2 ||
310 dwc->link_state == DWC3_LINK_STATE_U3);
311
312 if (unlikely(needs_wakeup)) {
313 ret = __dwc3_gadget_wakeup(dwc);
314 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
315 ret);
316 }
317 }
318
Felipe Balbi2eb88012016-04-12 16:53:39 +0300319 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300322
Felipe Balbi8897a762016-09-22 10:56:08 +0300323 /*
324 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
325 * not relying on XferNotReady, we can make use of a special "No
326 * Response Update Transfer" command where we should clear both CmdAct
327 * and CmdIOC bits.
328 *
329 * With this, we don't need to wait for command completion and can
330 * straight away issue further commands to the endpoint.
331 *
332 * NOTICE: We're making an assumption that control endpoints will never
333 * make use of Update Transfer command. This is a safe assumption
334 * because we can never have more than one request at a time with
335 * Control Endpoints. If anybody changes that assumption, this chunk
336 * needs to be updated accordingly.
337 */
338 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
339 !usb_endpoint_xfer_isoc(desc))
340 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
341 else
342 cmd |= DWC3_DEPCMD_CMDACT;
343
344 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300345 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300346 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300348 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000349
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000350 switch (cmd_status) {
351 case 0:
352 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300353 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000354 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000355 ret = -EINVAL;
356 break;
357 case DEPEVT_TRANSFER_BUS_EXPIRY:
358 /*
359 * SW issues START TRANSFER command to
360 * isochronous ep with future frame interval. If
361 * future interval time has already passed when
362 * core receives the command, it will respond
363 * with an error status of 'Bus Expiry'.
364 *
365 * Instead of always returning -EINVAL, let's
366 * give a hint to the gadget driver that this is
367 * the case by returning -EAGAIN.
368 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000369 ret = -EAGAIN;
370 break;
371 default:
372 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
373 }
374
Felipe Balbic0ca3242016-04-04 09:11:51 +0300375 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300377 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378
Felipe Balbif6bb2252016-05-23 13:53:34 +0300379 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300380 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300381 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300382 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300383
Felipe Balbi0933df12016-05-23 14:02:33 +0300384 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
385
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300386 if (ret == 0) {
387 switch (DWC3_DEPCMD_CMD(cmd)) {
388 case DWC3_DEPCMD_STARTTRANSFER:
389 dep->flags |= DWC3_EP_TRANSFER_STARTED;
Felipe Balbid7ca7e12018-04-11 12:58:46 +0300390 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300391 break;
392 case DWC3_DEPCMD_ENDTRANSFER:
393 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
394 break;
395 default:
396 /* nothing */
397 break;
398 }
399 }
400
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700401 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300402 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700403 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300404 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
405 }
406
Felipe Balbic0ca3242016-04-04 09:11:51 +0300407 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408}
409
John Youn50c763f2016-05-31 17:49:56 -0700410static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
411{
412 struct dwc3 *dwc = dep->dwc;
413 struct dwc3_gadget_ep_cmd_params params;
414 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
415
416 /*
417 * As of core revision 2.60a the recommended programming model
418 * is to set the ClearPendIN bit when issuing a Clear Stall EP
419 * command for IN endpoints. This is to prevent an issue where
420 * some (non-compliant) hosts may not send ACK TPs for pending
421 * IN transfers due to a mishandled error condition. Synopsys
422 * STAR 9000614252.
423 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800424 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
425 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700426 cmd |= DWC3_DEPCMD_CLEARPENDIN;
427
428 memset(&params, 0, sizeof(params));
429
Felipe Balbi2cd47182016-04-12 16:42:43 +0300430 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700431}
432
Felipe Balbi72246da2011-08-19 18:10:58 +0300433static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200434 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300435{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300436 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300437
438 return dep->trb_pool_dma + offset;
439}
440
441static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
442{
443 struct dwc3 *dwc = dep->dwc;
444
445 if (dep->trb_pool)
446 return 0;
447
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530448 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300449 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
450 &dep->trb_pool_dma, GFP_KERNEL);
451 if (!dep->trb_pool) {
452 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
453 dep->name);
454 return -ENOMEM;
455 }
456
457 return 0;
458}
459
460static void dwc3_free_trb_pool(struct dwc3_ep *dep)
461{
462 struct dwc3 *dwc = dep->dwc;
463
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530464 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 dep->trb_pool, dep->trb_pool_dma);
466
467 dep->trb_pool = NULL;
468 dep->trb_pool_dma = 0;
469}
470
Felipe Balbi20d1d432018-04-09 12:49:02 +0300471static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
472{
473 struct dwc3_gadget_ep_cmd_params params;
474
475 memset(&params, 0x00, sizeof(params));
476
477 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
478
479 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
480 &params);
481}
John Younc4509602016-02-16 20:10:53 -0800482
483/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300484 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800485 * @dep: endpoint that is being enabled
486 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300487 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
488 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800489 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300490 * The assignment of transfer resources cannot perfectly follow the data book
491 * due to the fact that the controller driver does not have all knowledge of the
492 * configuration in advance. It is given this information piecemeal by the
493 * composite gadget framework after every SET_CONFIGURATION and
494 * SET_INTERFACE. Trying to follow the databook programming model in this
495 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800496 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300497 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
498 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
499 * incorrect in the scenario of multiple interfaces.
500 *
501 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800502 * endpoint on alt setting (8.1.6).
503 *
504 * The following simplified method is used instead:
505 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300506 * All hardware endpoints can be assigned a transfer resource and this setting
507 * will stay persistent until either a core reset or hibernation. So whenever we
508 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
509 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800510 * guaranteed that there are as many transfer resources as endpoints.
511 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300512 * This function is called for each endpoint when it is being enabled but is
513 * triggered only when called for EP0-out, which always happens first, and which
514 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800515 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300516static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300517{
518 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300519 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800521 int i;
522 int ret;
523
524 if (dep->number)
525 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300526
527 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800528 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300529 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
Felipe Balbi2cd47182016-04-12 16:42:43 +0300531 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800532 if (ret)
533 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
John Younc4509602016-02-16 20:10:53 -0800535 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
536 struct dwc3_ep *dep = dwc->eps[i];
537
538 if (!dep)
539 continue;
540
Felipe Balbib07c2db2018-04-09 12:46:47 +0300541 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800542 if (ret)
543 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 }
545
546 return 0;
547}
548
Felipe Balbib07c2db2018-04-09 12:46:47 +0300549static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300550{
John Youn39ebb052016-11-09 16:36:28 -0800551 const struct usb_ss_ep_comp_descriptor *comp_desc;
552 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300554 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
John Youn39ebb052016-11-09 16:36:28 -0800556 comp_desc = dep->endpoint.comp_desc;
557 desc = dep->endpoint.desc;
558
Felipe Balbi72246da2011-08-19 18:10:58 +0300559 memset(&params, 0x00, sizeof(params));
560
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300561 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900562 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
563
564 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800565 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300566 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300567 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900568 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300569
Felipe Balbia2d23f02018-04-09 12:40:48 +0300570 params.param0 |= action;
571 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600572 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600573
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300574 if (usb_endpoint_xfer_control(desc))
575 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300576
577 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
578 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200580 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300581 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
582 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300583 dep->stream_capable = true;
584 }
585
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500586 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300587 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
589 /*
590 * We are doing 1:1 mapping for endpoints, meaning
591 * Physical Endpoints 2 maps to Logical Endpoint 2 and
592 * so on. We consider the direction bit as part of the physical
593 * endpoint number. So USB endpoint 0x81 is 0x03.
594 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300595 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
597 /*
598 * We must use the lower 16 TX FIFOs even though
599 * HW might have more
600 */
601 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300602 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300603
604 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300605 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 dep->interval = 1 << (desc->bInterval - 1);
607 }
608
Felipe Balbi2cd47182016-04-12 16:42:43 +0300609 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300610}
611
Felipe Balbi72246da2011-08-19 18:10:58 +0300612/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300613 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300615 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300617 * Caller should take care of locking. Execute all necessary commands to
618 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300620static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300621{
John Youn39ebb052016-11-09 16:36:28 -0800622 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800624
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300626 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300627
628 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300629 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300630 if (ret)
631 return ret;
632 }
633
Felipe Balbib07c2db2018-04-09 12:46:47 +0300634 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 if (ret)
636 return ret;
637
638 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200639 struct dwc3_trb *trb_st_hw;
640 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300641
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 dep->type = usb_endpoint_type(desc);
643 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800644 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
646 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
647 reg |= DWC3_DALEPENA_EP(dep->number);
648 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
649
Baolin Wang76a638f2016-10-31 19:38:36 +0800650 init_waitqueue_head(&dep->wait_end_transfer);
651
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300652 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200653 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300654
John Youn0d257442016-05-19 17:26:08 -0700655 /* Initialize the TRB ring */
656 dep->trb_dequeue = 0;
657 dep->trb_enqueue = 0;
658 memset(dep->trb_pool, 0,
659 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
660
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300661 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300662 trb_st_hw = &dep->trb_pool[0];
663
Felipe Balbif6bafc62012-02-06 11:04:53 +0200664 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200665 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
666 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
667 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
668 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 }
670
Felipe Balbia97ea992016-09-29 16:28:56 +0300671 /*
672 * Issue StartTransfer here with no-op TRB so we can always rely on No
673 * Response Update Transfer command.
674 */
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300675 if (usb_endpoint_xfer_bulk(desc) ||
676 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300677 struct dwc3_gadget_ep_cmd_params params;
678 struct dwc3_trb *trb;
679 dma_addr_t trb_dma;
680 u32 cmd;
681
682 memset(&params, 0, sizeof(params));
683 trb = &dep->trb_pool[0];
684 trb_dma = dwc3_trb_dma_offset(dep, trb);
685
686 params.param0 = upper_32_bits(trb_dma);
687 params.param1 = lower_32_bits(trb_dma);
688
689 cmd = DWC3_DEPCMD_STARTTRANSFER;
690
691 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
692 if (ret < 0)
693 return ret;
Felipe Balbia97ea992016-09-29 16:28:56 +0300694 }
695
Felipe Balbi2870e502016-11-03 13:53:29 +0200696out:
697 trace_dwc3_gadget_ep_enable(dep);
698
Felipe Balbi72246da2011-08-19 18:10:58 +0300699 return 0;
700}
701
Felipe Balbi8f608e82018-03-27 10:53:29 +0300702static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200703static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300704{
705 struct dwc3_request *req;
706
Felipe Balbi8f608e82018-03-27 10:53:29 +0300707 dwc3_stop_active_transfer(dep, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300708
Felipe Balbi0e146022016-06-21 10:32:02 +0300709 /* - giveback all requests to gadget driver */
710 while (!list_empty(&dep->started_list)) {
711 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200712
Felipe Balbi0e146022016-06-21 10:32:02 +0300713 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200714 }
715
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200716 while (!list_empty(&dep->pending_list)) {
717 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300718
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200719 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300721}
722
723/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300724 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 * @dep: the endpoint to disable
726 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300727 * This function undoes what __dwc3_gadget_ep_enable did and also removes
728 * requests which are currently being processed by the hardware and those which
729 * are not yet scheduled.
730 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200731 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300732 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300733static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
734{
735 struct dwc3 *dwc = dep->dwc;
736 u32 reg;
737
Felipe Balbi2870e502016-11-03 13:53:29 +0200738 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500739
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200740 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300741
Felipe Balbi687ef982014-04-16 10:30:33 -0500742 /* make sure HW endpoint isn't stalled */
743 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500744 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500745
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
747 reg &= ~DWC3_DALEPENA_EP(dep->number);
748 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
749
Felipe Balbi879631a2011-09-30 10:58:47 +0300750 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800752 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300753
John Youn39ebb052016-11-09 16:36:28 -0800754 /* Clear out the ep descriptors for non-ep0 */
755 if (dep->number > 1) {
756 dep->endpoint.comp_desc = NULL;
757 dep->endpoint.desc = NULL;
758 }
759
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 return 0;
761}
762
763/* -------------------------------------------------------------------------- */
764
765static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
766 const struct usb_endpoint_descriptor *desc)
767{
768 return -EINVAL;
769}
770
771static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
772{
773 return -EINVAL;
774}
775
776/* -------------------------------------------------------------------------- */
777
778static int dwc3_gadget_ep_enable(struct usb_ep *ep,
779 const struct usb_endpoint_descriptor *desc)
780{
781 struct dwc3_ep *dep;
782 struct dwc3 *dwc;
783 unsigned long flags;
784 int ret;
785
786 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
787 pr_debug("dwc3: invalid parameters\n");
788 return -EINVAL;
789 }
790
791 if (!desc->wMaxPacketSize) {
792 pr_debug("dwc3: missing wMaxPacketSize\n");
793 return -EINVAL;
794 }
795
796 dep = to_dwc3_ep(ep);
797 dwc = dep->dwc;
798
Felipe Balbi95ca9612015-12-10 13:08:20 -0600799 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
800 "%s is already enabled\n",
801 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300802 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300803
Felipe Balbi72246da2011-08-19 18:10:58 +0300804 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300805 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 spin_unlock_irqrestore(&dwc->lock, flags);
807
808 return ret;
809}
810
811static int dwc3_gadget_ep_disable(struct usb_ep *ep)
812{
813 struct dwc3_ep *dep;
814 struct dwc3 *dwc;
815 unsigned long flags;
816 int ret;
817
818 if (!ep) {
819 pr_debug("dwc3: invalid parameters\n");
820 return -EINVAL;
821 }
822
823 dep = to_dwc3_ep(ep);
824 dwc = dep->dwc;
825
Felipe Balbi95ca9612015-12-10 13:08:20 -0600826 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
827 "%s is already disabled\n",
828 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300829 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830
Felipe Balbi72246da2011-08-19 18:10:58 +0300831 spin_lock_irqsave(&dwc->lock, flags);
832 ret = __dwc3_gadget_ep_disable(dep);
833 spin_unlock_irqrestore(&dwc->lock, flags);
834
835 return ret;
836}
837
838static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300839 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300840{
841 struct dwc3_request *req;
842 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300843
844 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900845 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300846 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300847
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300848 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 req->epnum = dep->number;
850 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300851
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500852 trace_dwc3_alloc_request(req);
853
Felipe Balbi72246da2011-08-19 18:10:58 +0300854 return &req->request;
855}
856
857static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
858 struct usb_request *request)
859{
860 struct dwc3_request *req = to_dwc3_request(request);
861
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500862 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300863 kfree(req);
864}
865
Felipe Balbi42626912018-04-09 13:01:43 +0300866/**
867 * dwc3_ep_prev_trb - returns the previous TRB in the ring
868 * @dep: The endpoint with the TRB ring
869 * @index: The index of the current TRB in the ring
870 *
871 * Returns the TRB prior to the one pointed to by the index. If the
872 * index is 0, we will wrap backwards, skip the link TRB, and return
873 * the one just before that.
874 */
875static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
876{
877 u8 tmp = index;
878
879 if (!tmp)
880 tmp = DWC3_TRB_NUM - 1;
881
882 return &dep->trb_pool[tmp - 1];
883}
884
885static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
886{
887 struct dwc3_trb *tmp;
888 u8 trbs_left;
889
890 /*
891 * If enqueue & dequeue are equal than it is either full or empty.
892 *
893 * One way to know for sure is if the TRB right before us has HWO bit
894 * set or not. If it has, then we're definitely full and can't fit any
895 * more transfers in our ring.
896 */
897 if (dep->trb_enqueue == dep->trb_dequeue) {
898 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
899 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
900 return 0;
901
902 return DWC3_TRB_NUM - 1;
903 }
904
905 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
906 trbs_left &= (DWC3_TRB_NUM - 1);
907
908 if (dep->trb_dequeue < dep->trb_enqueue)
909 trbs_left--;
910
911 return trbs_left;
912}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300913
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200914static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
915 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
916 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200917{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300918 struct dwc3 *dwc = dep->dwc;
919 struct usb_gadget *gadget = &dwc->gadget;
920 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200921
Felipe Balbief966b92016-04-05 13:09:51 +0300922 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530923
Felipe Balbif6bafc62012-02-06 11:04:53 +0200924 trb->size = DWC3_TRB_SIZE_LENGTH(length);
925 trb->bpl = lower_32_bits(dma);
926 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200927
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200928 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200929 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200930 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200931 break;
932
933 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300934 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530935 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300936
Manu Gautam40d829f2017-07-19 17:07:10 +0530937 /*
938 * USB Specification 2.0 Section 5.9.2 states that: "If
939 * there is only a single transaction in the microframe,
940 * only a DATA0 data packet PID is used. If there are
941 * two transactions per microframe, DATA1 is used for
942 * the first transaction data packet and DATA0 is used
943 * for the second transaction data packet. If there are
944 * three transactions per microframe, DATA2 is used for
945 * the first transaction data packet, DATA1 is used for
946 * the second, and DATA0 is used for the third."
947 *
948 * IOW, we should satisfy the following cases:
949 *
950 * 1) length <= maxpacket
951 * - DATA0
952 *
953 * 2) maxpacket < length <= (2 * maxpacket)
954 * - DATA1, DATA0
955 *
956 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
957 * - DATA2, DATA1, DATA0
958 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300959 if (speed == USB_SPEED_HIGH) {
960 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530961 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530962 unsigned int maxp = usb_endpoint_maxp(ep->desc);
963
964 if (length <= (2 * maxp))
965 mult--;
966
967 if (length <= maxp)
968 mult--;
969
970 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300971 }
972 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530973 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300974 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200975
976 /* always enable Interrupt on Missed ISOC */
977 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200978 break;
979
980 case USB_ENDPOINT_XFER_BULK:
981 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200982 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200983 break;
984 default:
985 /*
986 * This is only possible with faulty memory because we
987 * checked it already :)
988 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300989 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
990 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200991 }
992
Felipe Balbica4d44e2016-03-10 13:53:27 +0200993 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300994 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300995 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600996
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200997 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300998 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
999 }
1000
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001001 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +03001002 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +03001003 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001004
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301005 if (chain)
1006 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1007
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001008 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001009 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001010
1011 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001012
1013 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
1119 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1120 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
1240 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1241 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301242 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001243 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1244 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301245 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001246
Felipe Balbi2cd47182016-04-12 16:42:43 +03001247 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001248 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001249 /*
1250 * FIXME we need to iterate over the list of requests
1251 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001252 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001253 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001254 if (req->trb)
1255 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001256 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001257 return ret;
1258 }
1259
Felipe Balbi72246da2011-08-19 18:10:58 +03001260 return 0;
1261}
1262
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001263static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1264{
1265 u32 reg;
1266
1267 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1268 return DWC3_DSTS_SOFFN(reg);
1269}
1270
Thinh Nguyend92021f2018-11-14 22:56:54 -08001271/**
1272 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1273 * @dep: isoc endpoint
1274 *
1275 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1276 * microframe number reported by the XferNotReady event for the future frame
1277 * number to start the isoc transfer.
1278 *
1279 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1280 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1281 * XferNotReady event are invalid. The driver uses this number to schedule the
1282 * isochronous transfer and passes it to the START TRANSFER command. Because
1283 * this number is invalid, the command may fail. If BIT[15:14] matches the
1284 * internal 16-bit microframe, the START TRANSFER command will pass and the
1285 * transfer will start at the scheduled time, if it is off by 1, the command
1286 * will still pass, but the transfer will start 2 seconds in the future. For all
1287 * other conditions, the START TRANSFER command will fail with bus-expiry.
1288 *
1289 * In order to workaround this issue, we can test for the correct combination of
1290 * BIT[15:14] by sending START TRANSFER commands with different values of
1291 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1292 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1293 * As the result, within the 4 possible combinations for BIT[15:14], there will
1294 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1295 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1296 * value is the correct combination.
1297 *
1298 * Since there are only 4 outcomes and the results are ordered, we can simply
1299 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1300 * deduce the smaller successful combination.
1301 *
1302 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1303 * of BIT[15:14]. The correct combination is as follow:
1304 *
1305 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1306 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1307 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1308 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1309 *
1310 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1311 * endpoints.
1312 */
1313static void dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1314{
1315 int cmd_status = 0;
1316 bool test0;
1317 bool test1;
1318
1319 while (dep->combo_num < 2) {
1320 struct dwc3_gadget_ep_cmd_params params;
1321 u32 test_frame_number;
1322 u32 cmd;
1323
1324 /*
1325 * Check if we can start isoc transfer on the next interval or
1326 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1327 */
1328 test_frame_number = dep->frame_number & 0x3fff;
1329 test_frame_number |= dep->combo_num << 14;
1330 test_frame_number += max_t(u32, 4, dep->interval);
1331
1332 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1333 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1334
1335 cmd = DWC3_DEPCMD_STARTTRANSFER;
1336 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1337 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1338
1339 /* Redo if some other failure beside bus-expiry is received */
1340 if (cmd_status && cmd_status != -EAGAIN) {
1341 dep->start_cmd_status = 0;
1342 dep->combo_num = 0;
1343 return;
1344 }
1345
1346 /* Store the first test status */
1347 if (dep->combo_num == 0)
1348 dep->start_cmd_status = cmd_status;
1349
1350 dep->combo_num++;
1351
1352 /*
1353 * End the transfer if the START_TRANSFER command is successful
1354 * to wait for the next XferNotReady to test the command again
1355 */
1356 if (cmd_status == 0) {
1357 dwc3_stop_active_transfer(dep, true);
1358 return;
1359 }
1360 }
1361
1362 /* test0 and test1 are both completed at this point */
1363 test0 = (dep->start_cmd_status == 0);
1364 test1 = (cmd_status == 0);
1365
1366 if (!test0 && test1)
1367 dep->combo_num = 1;
1368 else if (!test0 && !test1)
1369 dep->combo_num = 2;
1370 else if (test0 && !test1)
1371 dep->combo_num = 3;
1372 else if (test0 && test1)
1373 dep->combo_num = 0;
1374
1375 dep->frame_number &= 0x3fff;
1376 dep->frame_number |= dep->combo_num << 14;
1377 dep->frame_number += max_t(u32, 4, dep->interval);
1378
1379 /* Reinitialize test variables */
1380 dep->start_cmd_status = 0;
1381 dep->combo_num = 0;
1382
1383 __dwc3_gadget_kick_transfer(dep);
1384}
1385
Felipe Balbi5828cad2018-03-27 11:14:31 +03001386static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301387{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001388 struct dwc3 *dwc = dep->dwc;
1389
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001390 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001391 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001392 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301393 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301394 return;
1395 }
1396
Thinh Nguyend92021f2018-11-14 22:56:54 -08001397 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1398 (dwc->revision <= DWC3_USB31_REVISION_160A ||
1399 (dwc->revision == DWC3_USB31_REVISION_170A &&
1400 dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1401 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1402
1403 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction) {
1404 dwc3_gadget_start_isoc_quirk(dep);
1405 return;
1406 }
1407 }
1408
Felipe Balbif62afb42018-04-11 10:34:34 +03001409 dep->frame_number = DWC3_ALIGN_FRAME(dep);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001410 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301411}
1412
Felipe Balbi72246da2011-08-19 18:10:58 +03001413static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1414{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001415 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001416
Felipe Balbibb423982015-11-16 15:31:21 -06001417 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001418 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1419 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001420 return -ESHUTDOWN;
1421 }
1422
Felipe Balbi04fb3652017-05-17 15:57:45 +03001423 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1424 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001425 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001426
Felipe Balbifc8bb912016-05-16 13:14:48 +03001427 pm_runtime_get(dwc->dev);
1428
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 req->request.actual = 0;
1430 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001431
Felipe Balbife84f522015-09-01 09:01:38 -05001432 trace_dwc3_ep_queue(req);
1433
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001434 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001435
Felipe Balbid889c232016-09-29 15:44:29 +03001436 /*
1437 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1438 * wait for a XferNotReady event so we will know what's the current
1439 * (micro-)frame number.
1440 *
1441 * Without this trick, we are very, very likely gonna get Bus Expiry
1442 * errors which will force us issue EndTransfer command.
1443 */
1444 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001445 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1446 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001447 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001448
1449 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1450 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1451 __dwc3_gadget_start_isoc(dep);
1452 return 0;
1453 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001454 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001455 }
1456
Felipe Balbi7fdca762017-09-05 14:41:34 +03001457 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001458}
1459
1460static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1461 gfp_t gfp_flags)
1462{
1463 struct dwc3_request *req = to_dwc3_request(request);
1464 struct dwc3_ep *dep = to_dwc3_ep(ep);
1465 struct dwc3 *dwc = dep->dwc;
1466
1467 unsigned long flags;
1468
1469 int ret;
1470
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001471 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001472 ret = __dwc3_gadget_ep_queue(dep, req);
1473 spin_unlock_irqrestore(&dwc->lock, flags);
1474
1475 return ret;
1476}
1477
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001478static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1479{
1480 int i;
1481
1482 /*
1483 * If request was already started, this means we had to
1484 * stop the transfer. With that we also need to ignore
1485 * all TRBs used by the request, however TRBs can only
1486 * be modified after completion of END_TRANSFER
1487 * command. So what we do here is that we wait for
1488 * END_TRANSFER completion and only after that, we jump
1489 * over TRBs by clearing HWO and incrementing dequeue
1490 * pointer.
1491 */
1492 for (i = 0; i < req->num_trbs; i++) {
1493 struct dwc3_trb *trb;
1494
1495 trb = req->trb + i;
1496 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1497 dwc3_ep_inc_deq(dep);
1498 }
1499}
1500
Felipe Balbi72246da2011-08-19 18:10:58 +03001501static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1502 struct usb_request *request)
1503{
1504 struct dwc3_request *req = to_dwc3_request(request);
1505 struct dwc3_request *r = NULL;
1506
1507 struct dwc3_ep *dep = to_dwc3_ep(ep);
1508 struct dwc3 *dwc = dep->dwc;
1509
1510 unsigned long flags;
1511 int ret = 0;
1512
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001513 trace_dwc3_ep_dequeue(req);
1514
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 spin_lock_irqsave(&dwc->lock, flags);
1516
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001517 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001518 if (r == req)
1519 break;
1520 }
1521
1522 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001523 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 if (r == req)
1525 break;
1526 }
1527 if (r == req) {
1528 /* wait until it is processed */
Felipe Balbi8f608e82018-03-27 10:53:29 +03001529 dwc3_stop_active_transfer(dep, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001530 wait_event_lock_irq(dep->wait_end_transfer,
1531 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1532 dwc->lock);
1533
1534 if (!r->trb)
Mayank Rana05645362018-03-23 10:05:33 -07001535 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001536
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001537 dwc3_gadget_ep_skip_trbs(dep, r);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301538 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001540 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001541 request, ep->name);
1542 ret = -EINVAL;
1543 goto out0;
1544 }
1545
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301546out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001547 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1548
1549out0:
1550 spin_unlock_irqrestore(&dwc->lock, flags);
1551
1552 return ret;
1553}
1554
Felipe Balbi7a608552014-09-24 14:19:52 -05001555int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001556{
1557 struct dwc3_gadget_ep_cmd_params params;
1558 struct dwc3 *dwc = dep->dwc;
1559 int ret;
1560
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001561 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1562 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1563 return -EINVAL;
1564 }
1565
Felipe Balbi72246da2011-08-19 18:10:58 +03001566 memset(&params, 0x00, sizeof(params));
1567
1568 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001569 struct dwc3_trb *trb;
1570
1571 unsigned transfer_in_flight;
1572 unsigned started;
1573
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001574 if (dep->flags & DWC3_EP_STALL)
1575 return 0;
1576
Felipe Balbi69450c42016-05-30 13:37:02 +03001577 if (dep->number > 1)
1578 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1579 else
1580 trb = &dwc->ep0_trb[dep->trb_enqueue];
1581
1582 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1583 started = !list_empty(&dep->started_list);
1584
1585 if (!protocol && ((dep->direction && transfer_in_flight) ||
1586 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001587 return -EAGAIN;
1588 }
1589
Felipe Balbi2cd47182016-04-12 16:42:43 +03001590 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1591 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001593 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001594 dep->name);
1595 else
1596 dep->flags |= DWC3_EP_STALL;
1597 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001598 if (!(dep->flags & DWC3_EP_STALL))
1599 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001600
John Youn50c763f2016-05-31 17:49:56 -07001601 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001602 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001603 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001604 dep->name);
1605 else
Alan Sterna535d812013-11-01 12:05:12 -04001606 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001608
Felipe Balbi72246da2011-08-19 18:10:58 +03001609 return ret;
1610}
1611
1612static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1613{
1614 struct dwc3_ep *dep = to_dwc3_ep(ep);
1615 struct dwc3 *dwc = dep->dwc;
1616
1617 unsigned long flags;
1618
1619 int ret;
1620
1621 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001622 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 spin_unlock_irqrestore(&dwc->lock, flags);
1624
1625 return ret;
1626}
1627
1628static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1629{
1630 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001631 struct dwc3 *dwc = dep->dwc;
1632 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001633 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001634
Paul Zimmerman249a4562012-02-24 17:32:16 -08001635 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001636 dep->flags |= DWC3_EP_WEDGE;
1637
Pratyush Anand08f0d962012-06-25 22:40:43 +05301638 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001639 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301640 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001641 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001642 spin_unlock_irqrestore(&dwc->lock, flags);
1643
1644 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001645}
1646
1647/* -------------------------------------------------------------------------- */
1648
1649static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1650 .bLength = USB_DT_ENDPOINT_SIZE,
1651 .bDescriptorType = USB_DT_ENDPOINT,
1652 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1653};
1654
1655static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1656 .enable = dwc3_gadget_ep0_enable,
1657 .disable = dwc3_gadget_ep0_disable,
1658 .alloc_request = dwc3_gadget_ep_alloc_request,
1659 .free_request = dwc3_gadget_ep_free_request,
1660 .queue = dwc3_gadget_ep0_queue,
1661 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301662 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001663 .set_wedge = dwc3_gadget_ep_set_wedge,
1664};
1665
1666static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1667 .enable = dwc3_gadget_ep_enable,
1668 .disable = dwc3_gadget_ep_disable,
1669 .alloc_request = dwc3_gadget_ep_alloc_request,
1670 .free_request = dwc3_gadget_ep_free_request,
1671 .queue = dwc3_gadget_ep_queue,
1672 .dequeue = dwc3_gadget_ep_dequeue,
1673 .set_halt = dwc3_gadget_ep_set_halt,
1674 .set_wedge = dwc3_gadget_ep_set_wedge,
1675};
1676
1677/* -------------------------------------------------------------------------- */
1678
1679static int dwc3_gadget_get_frame(struct usb_gadget *g)
1680{
1681 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001682
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001683 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001684}
1685
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001686static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001687{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001688 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001689
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001690 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001691 u32 reg;
1692
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 u8 link_state;
1694 u8 speed;
1695
Felipe Balbi72246da2011-08-19 18:10:58 +03001696 /*
1697 * According to the Databook Remote wakeup request should
1698 * be issued only when the device is in early suspend state.
1699 *
1700 * We can check that via USB Link State bits in DSTS register.
1701 */
1702 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1703
1704 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001705 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001706 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001707 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001708
1709 link_state = DWC3_DSTS_USBLNKST(reg);
1710
1711 switch (link_state) {
1712 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1713 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1714 break;
1715 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001716 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001717 }
1718
Felipe Balbi8598bde2012-01-02 18:55:57 +02001719 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1720 if (ret < 0) {
1721 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001722 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001723 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001724
Paul Zimmerman802fde92012-04-27 13:10:52 +03001725 /* Recent versions do this automatically */
1726 if (dwc->revision < DWC3_REVISION_194A) {
1727 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001728 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001729 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1730 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1731 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001732
Paul Zimmerman1d046792012-02-15 18:56:56 -08001733 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001734 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001735
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001736 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001737 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1738
1739 /* in HS, means ON */
1740 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1741 break;
1742 }
1743
1744 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1745 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001746 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001747 }
1748
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001749 return 0;
1750}
1751
1752static int dwc3_gadget_wakeup(struct usb_gadget *g)
1753{
1754 struct dwc3 *dwc = gadget_to_dwc(g);
1755 unsigned long flags;
1756 int ret;
1757
1758 spin_lock_irqsave(&dwc->lock, flags);
1759 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 spin_unlock_irqrestore(&dwc->lock, flags);
1761
1762 return ret;
1763}
1764
1765static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1766 int is_selfpowered)
1767{
1768 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001769 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001770
Paul Zimmerman249a4562012-02-24 17:32:16 -08001771 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001772 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001773 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001774
1775 return 0;
1776}
1777
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001778static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001779{
1780 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001781 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001782
Felipe Balbifc8bb912016-05-16 13:14:48 +03001783 if (pm_runtime_suspended(dwc->dev))
1784 return 0;
1785
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001787 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001788 if (dwc->revision <= DWC3_REVISION_187A) {
1789 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1790 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1791 }
1792
1793 if (dwc->revision >= DWC3_REVISION_194A)
1794 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1795 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001796
1797 if (dwc->has_hibernation)
1798 reg |= DWC3_DCTL_KEEP_CONNECT;
1799
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001800 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001801 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001803
1804 if (dwc->has_hibernation && !suspend)
1805 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1806
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001807 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001808 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001809
1810 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1811
1812 do {
1813 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001814 reg &= DWC3_DSTS_DEVCTRLHLT;
1815 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001816
1817 if (!timeout)
1818 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001819
Pratyush Anand6f17f742012-07-02 10:21:55 +05301820 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001821}
1822
1823static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1824{
1825 struct dwc3 *dwc = gadget_to_dwc(g);
1826 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301827 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001828
1829 is_on = !!is_on;
1830
Baolin Wangbb014732016-10-14 17:11:33 +08001831 /*
1832 * Per databook, when we want to stop the gadget, if a control transfer
1833 * is still in process, complete it and get the core into setup phase.
1834 */
1835 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1836 reinit_completion(&dwc->ep0_in_setup);
1837
1838 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1839 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1840 if (ret == 0) {
1841 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1842 return -ETIMEDOUT;
1843 }
1844 }
1845
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001847 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 spin_unlock_irqrestore(&dwc->lock, flags);
1849
Pratyush Anand6f17f742012-07-02 10:21:55 +05301850 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001851}
1852
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001853static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1854{
1855 u32 reg;
1856
1857 /* Enable all but Start and End of Frame IRQs */
1858 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1859 DWC3_DEVTEN_EVNTOVERFLOWEN |
1860 DWC3_DEVTEN_CMDCMPLTEN |
1861 DWC3_DEVTEN_ERRTICERREN |
1862 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001863 DWC3_DEVTEN_CONNECTDONEEN |
1864 DWC3_DEVTEN_USBRSTEN |
1865 DWC3_DEVTEN_DISCONNEVTEN);
1866
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001867 if (dwc->revision < DWC3_REVISION_250A)
1868 reg |= DWC3_DEVTEN_ULSTCNGEN;
1869
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001870 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1871}
1872
1873static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1874{
1875 /* mask all interrupts */
1876 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1877}
1878
1879static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001880static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001881
Felipe Balbi4e994722016-05-13 14:09:59 +03001882/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001883 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1884 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001885 *
1886 * The following looks like complex but it's actually very simple. In order to
1887 * calculate the number of packets we can burst at once on OUT transfers, we're
1888 * gonna use RxFIFO size.
1889 *
1890 * To calculate RxFIFO size we need two numbers:
1891 * MDWIDTH = size, in bits, of the internal memory bus
1892 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1893 *
1894 * Given these two numbers, the formula is simple:
1895 *
1896 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1897 *
1898 * 24 bytes is for 3x SETUP packets
1899 * 16 bytes is a clock domain crossing tolerance
1900 *
1901 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1902 */
1903static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1904{
1905 u32 ram2_depth;
1906 u32 mdwidth;
1907 u32 nump;
1908 u32 reg;
1909
1910 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1911 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1912
1913 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1914 nump = min_t(u32, nump, 16);
1915
1916 /* update NumP */
1917 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1918 reg &= ~DWC3_DCFG_NUMP_MASK;
1919 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1920 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1921}
1922
Felipe Balbid7be2952016-05-04 15:49:37 +03001923static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001924{
Felipe Balbi72246da2011-08-19 18:10:58 +03001925 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001926 int ret = 0;
1927 u32 reg;
1928
John Youncf40b862016-11-14 12:32:43 -08001929 /*
1930 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1931 * the core supports IMOD, disable it.
1932 */
1933 if (dwc->imod_interval) {
1934 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1935 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1936 } else if (dwc3_has_imod(dwc)) {
1937 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1938 }
1939
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001940 /*
1941 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1942 * field instead of letting dwc3 itself calculate that automatically.
1943 *
1944 * This way, we maximize the chances that we'll be able to get several
1945 * bursts of data without going through any sort of endpoint throttling.
1946 */
1947 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001948 if (dwc3_is_usb31(dwc))
1949 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1950 else
1951 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1952
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001953 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1954
Felipe Balbi4e994722016-05-13 14:09:59 +03001955 dwc3_gadget_setup_nump(dwc);
1956
Felipe Balbi72246da2011-08-19 18:10:58 +03001957 /* Start with SuperSpeed Default */
1958 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1959
1960 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001961 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 if (ret) {
1963 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001964 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001965 }
1966
1967 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001968 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001969 if (ret) {
1970 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001971 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001972 }
1973
1974 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001975 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001976 dwc3_ep0_out_start(dwc);
1977
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001978 dwc3_gadget_enable_irq(dwc);
1979
Felipe Balbid7be2952016-05-04 15:49:37 +03001980 return 0;
1981
1982err1:
1983 __dwc3_gadget_ep_disable(dwc->eps[0]);
1984
1985err0:
1986 return ret;
1987}
1988
1989static int dwc3_gadget_start(struct usb_gadget *g,
1990 struct usb_gadget_driver *driver)
1991{
1992 struct dwc3 *dwc = gadget_to_dwc(g);
1993 unsigned long flags;
1994 int ret = 0;
1995 int irq;
1996
Roger Quadros9522def2016-06-10 14:48:38 +03001997 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001998 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1999 IRQF_SHARED, "dwc3", dwc->ev_buf);
2000 if (ret) {
2001 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2002 irq, ret);
2003 goto err0;
2004 }
2005
2006 spin_lock_irqsave(&dwc->lock, flags);
2007 if (dwc->gadget_driver) {
2008 dev_err(dwc->dev, "%s is already bound to %s\n",
2009 dwc->gadget.name,
2010 dwc->gadget_driver->driver.name);
2011 ret = -EBUSY;
2012 goto err1;
2013 }
2014
2015 dwc->gadget_driver = driver;
2016
Felipe Balbifc8bb912016-05-16 13:14:48 +03002017 if (pm_runtime_active(dwc->dev))
2018 __dwc3_gadget_start(dwc);
2019
Felipe Balbi72246da2011-08-19 18:10:58 +03002020 spin_unlock_irqrestore(&dwc->lock, flags);
2021
2022 return 0;
2023
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002024err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002025 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002026 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002027
2028err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002029 return ret;
2030}
2031
Felipe Balbid7be2952016-05-04 15:49:37 +03002032static void __dwc3_gadget_stop(struct dwc3 *dwc)
2033{
2034 dwc3_gadget_disable_irq(dwc);
2035 __dwc3_gadget_ep_disable(dwc->eps[0]);
2036 __dwc3_gadget_ep_disable(dwc->eps[1]);
2037}
2038
Felipe Balbi22835b82014-10-17 12:05:12 -05002039static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002040{
2041 struct dwc3 *dwc = gadget_to_dwc(g);
2042 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08002043 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02002044 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002045
2046 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08002047
2048 if (pm_runtime_suspended(dwc->dev))
2049 goto out;
2050
Felipe Balbid7be2952016-05-04 15:49:37 +03002051 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08002052
2053 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2054 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02002055 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08002056
2057 if (!dep)
2058 continue;
2059
2060 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2061 continue;
2062
Roger Quadros498f0472018-03-09 14:47:04 +02002063 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
2064 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
2065 dwc->lock, msecs_to_jiffies(5));
2066
2067 if (ret <= 0) {
2068 /* Timed out or interrupted! There's nothing much
2069 * we can do so we just log here and print which
2070 * endpoints timed out at the end.
2071 */
2072 tmo_eps |= 1 << epnum;
2073 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
2074 }
2075 }
2076
2077 if (tmo_eps) {
2078 dev_err(dwc->dev,
2079 "end transfer timed out on endpoints 0x%x [bitmap]\n",
2080 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08002081 }
2082
2083out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002084 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002085 spin_unlock_irqrestore(&dwc->lock, flags);
2086
Felipe Balbi3f308d12016-05-16 14:17:06 +03002087 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002088
Felipe Balbi72246da2011-08-19 18:10:58 +03002089 return 0;
2090}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002091
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002092static void dwc3_gadget_set_speed(struct usb_gadget *g,
2093 enum usb_device_speed speed)
2094{
2095 struct dwc3 *dwc = gadget_to_dwc(g);
2096 unsigned long flags;
2097 u32 reg;
2098
2099 spin_lock_irqsave(&dwc->lock, flags);
2100 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2101 reg &= ~(DWC3_DCFG_SPEED_MASK);
2102
2103 /*
2104 * WORKAROUND: DWC3 revision < 2.20a have an issue
2105 * which would cause metastability state on Run/Stop
2106 * bit if we try to force the IP to USB2-only mode.
2107 *
2108 * Because of that, we cannot configure the IP to any
2109 * speed other than the SuperSpeed
2110 *
2111 * Refers to:
2112 *
2113 * STAR#9000525659: Clock Domain Crossing on DCTL in
2114 * USB 2.0 Mode
2115 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002116 if (dwc->revision < DWC3_REVISION_220A &&
2117 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002118 reg |= DWC3_DCFG_SUPERSPEED;
2119 } else {
2120 switch (speed) {
2121 case USB_SPEED_LOW:
2122 reg |= DWC3_DCFG_LOWSPEED;
2123 break;
2124 case USB_SPEED_FULL:
2125 reg |= DWC3_DCFG_FULLSPEED;
2126 break;
2127 case USB_SPEED_HIGH:
2128 reg |= DWC3_DCFG_HIGHSPEED;
2129 break;
2130 case USB_SPEED_SUPER:
2131 reg |= DWC3_DCFG_SUPERSPEED;
2132 break;
2133 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002134 if (dwc3_is_usb31(dwc))
2135 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2136 else
2137 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002138 break;
2139 default:
2140 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2141
2142 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2143 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2144 else
2145 reg |= DWC3_DCFG_SUPERSPEED;
2146 }
2147 }
2148 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2149
2150 spin_unlock_irqrestore(&dwc->lock, flags);
2151}
2152
Felipe Balbi72246da2011-08-19 18:10:58 +03002153static const struct usb_gadget_ops dwc3_gadget_ops = {
2154 .get_frame = dwc3_gadget_get_frame,
2155 .wakeup = dwc3_gadget_wakeup,
2156 .set_selfpowered = dwc3_gadget_set_selfpowered,
2157 .pullup = dwc3_gadget_pullup,
2158 .udc_start = dwc3_gadget_start,
2159 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002160 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002161};
2162
2163/* -------------------------------------------------------------------------- */
2164
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002165static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2166{
2167 struct dwc3 *dwc = dep->dwc;
2168
2169 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2170 dep->endpoint.maxburst = 1;
2171 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2172 if (!dep->direction)
2173 dwc->gadget.ep0 = &dep->endpoint;
2174
2175 dep->endpoint.caps.type_control = true;
2176
2177 return 0;
2178}
2179
2180static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2181{
2182 struct dwc3 *dwc = dep->dwc;
2183 int mdwidth;
2184 int kbytes;
2185 int size;
2186
2187 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2188 /* MDWIDTH is represented in bits, we need it in bytes */
2189 mdwidth /= 8;
2190
2191 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2192 if (dwc3_is_usb31(dwc))
2193 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2194 else
2195 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2196
2197 /* FIFO Depth is in MDWDITH bytes. Multiply */
2198 size *= mdwidth;
2199
2200 kbytes = size / 1024;
2201 if (kbytes == 0)
2202 kbytes = 1;
2203
2204 /*
2205 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2206 * internal overhead. We don't really know how these are used,
2207 * but documentation say it exists.
2208 */
2209 size -= mdwidth * (kbytes + 1);
2210 size /= kbytes;
2211
2212 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2213
2214 dep->endpoint.max_streams = 15;
2215 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2216 list_add_tail(&dep->endpoint.ep_list,
2217 &dwc->gadget.ep_list);
2218 dep->endpoint.caps.type_iso = true;
2219 dep->endpoint.caps.type_bulk = true;
2220 dep->endpoint.caps.type_int = true;
2221
2222 return dwc3_alloc_trb_pool(dep);
2223}
2224
2225static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2226{
2227 struct dwc3 *dwc = dep->dwc;
2228
2229 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2230 dep->endpoint.max_streams = 15;
2231 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2232 list_add_tail(&dep->endpoint.ep_list,
2233 &dwc->gadget.ep_list);
2234 dep->endpoint.caps.type_iso = true;
2235 dep->endpoint.caps.type_bulk = true;
2236 dep->endpoint.caps.type_int = true;
2237
2238 return dwc3_alloc_trb_pool(dep);
2239}
2240
2241static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002242{
2243 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002244 bool direction = epnum & 1;
2245 int ret;
2246 u8 num = epnum >> 1;
2247
2248 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2249 if (!dep)
2250 return -ENOMEM;
2251
2252 dep->dwc = dwc;
2253 dep->number = epnum;
2254 dep->direction = direction;
2255 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2256 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002257 dep->combo_num = 0;
2258 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002259
2260 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2261 direction ? "in" : "out");
2262
2263 dep->endpoint.name = dep->name;
2264
2265 if (!(dep->number > 1)) {
2266 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2267 dep->endpoint.comp_desc = NULL;
2268 }
2269
2270 spin_lock_init(&dep->lock);
2271
2272 if (num == 0)
2273 ret = dwc3_gadget_init_control_endpoint(dep);
2274 else if (direction)
2275 ret = dwc3_gadget_init_in_endpoint(dep);
2276 else
2277 ret = dwc3_gadget_init_out_endpoint(dep);
2278
2279 if (ret)
2280 return ret;
2281
2282 dep->endpoint.caps.dir_in = direction;
2283 dep->endpoint.caps.dir_out = !direction;
2284
2285 INIT_LIST_HEAD(&dep->pending_list);
2286 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002287 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002288
2289 return 0;
2290}
2291
2292static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2293{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002294 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002295
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002296 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2297
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002298 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002299 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002300
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002301 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2302 if (ret)
2303 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002304 }
2305
2306 return 0;
2307}
2308
2309static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2310{
2311 struct dwc3_ep *dep;
2312 u8 epnum;
2313
2314 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2315 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002316 if (!dep)
2317 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302318 /*
2319 * Physical endpoints 0 and 1 are special; they form the
2320 * bi-directional USB endpoint 0.
2321 *
2322 * For those two physical endpoints, we don't allocate a TRB
2323 * pool nor do we add them the endpoints list. Due to that, we
2324 * shouldn't do these two operations otherwise we would end up
2325 * with all sorts of bugs when removing dwc3.ko.
2326 */
2327 if (epnum != 0 && epnum != 1) {
2328 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002329 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302330 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002331
2332 kfree(dep);
2333 }
2334}
2335
Felipe Balbi72246da2011-08-19 18:10:58 +03002336/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002337
Felipe Balbi8f608e82018-03-27 10:53:29 +03002338static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2339 struct dwc3_request *req, struct dwc3_trb *trb,
2340 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302341{
2342 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302343
Felipe Balbidc55c672016-08-12 13:20:32 +03002344 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002345
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002346 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002347 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002348
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002349 /*
2350 * If we're in the middle of series of chained TRBs and we
2351 * receive a short transfer along the way, DWC3 will skip
2352 * through all TRBs including the last TRB in the chain (the
2353 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2354 * bit and SW has to do it manually.
2355 *
2356 * We're going to do that here to avoid problems of HW trying
2357 * to use bogus TRBs for transfers.
2358 */
2359 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2360 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2361
Felipe Balbic6267a52017-01-05 14:58:46 +02002362 /*
2363 * If we're dealing with unaligned size OUT transfer, we will be left
2364 * with one TRB pending in the ring. We need to manually clear HWO bit
2365 * from that TRB.
2366 */
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002367
2368 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002369 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2370 return 1;
2371 }
2372
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302373 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002374 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302375
Felipe Balbi35b27192017-03-08 13:56:37 +02002376 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2377 return 1;
2378
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002379 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302380 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002381
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002382 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302383 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002384
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302385 return 0;
2386}
2387
Felipe Balbid3692952018-03-29 13:32:10 +03002388static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2389 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2390 int status)
2391{
2392 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2393 struct scatterlist *sg = req->sg;
2394 struct scatterlist *s;
2395 unsigned int pending = req->num_pending_sgs;
2396 unsigned int i;
2397 int ret = 0;
2398
2399 for_each_sg(sg, s, pending, i) {
2400 trb = &dep->trb_pool[dep->trb_dequeue];
2401
2402 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2403 break;
2404
2405 req->sg = sg_next(s);
2406 req->num_pending_sgs--;
2407
2408 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2409 trb, event, status, true);
2410 if (ret)
2411 break;
2412 }
2413
2414 return ret;
2415}
2416
2417static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2418 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2419 int status)
2420{
2421 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2422
2423 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2424 event, status, false);
2425}
2426
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002427static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2428{
2429 return req->request.actual == req->request.length;
2430}
2431
Felipe Balbif38e35d2018-04-06 15:56:35 +03002432static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2433 const struct dwc3_event_depevt *event,
2434 struct dwc3_request *req, int status)
2435{
2436 int ret;
2437
2438 if (req->num_pending_sgs)
2439 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2440 status);
2441 else
2442 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2443 status);
2444
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002445 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002446 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2447 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002448 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002449 }
2450
2451 req->request.actual = req->request.length - req->remaining;
2452
2453 if (!dwc3_gadget_ep_request_completed(req) &&
2454 req->num_pending_sgs) {
2455 __dwc3_gadget_kick_transfer(dep);
2456 goto out;
2457 }
2458
2459 dwc3_gadget_giveback(dep, req, status);
2460
2461out:
2462 return ret;
2463}
2464
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002465static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002466 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002467{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002468 struct dwc3_request *req;
2469 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002470
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002471 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002472 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002473
Felipe Balbif38e35d2018-04-06 15:56:35 +03002474 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2475 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002476 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002477 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002478 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002479}
2480
Felipe Balbiee3638b2018-03-27 11:26:53 +03002481static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2482 const struct dwc3_event_depevt *event)
2483{
Felipe Balbif62afb42018-04-11 10:34:34 +03002484 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002485}
2486
Felipe Balbi8f608e82018-03-27 10:53:29 +03002487static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2488 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002489{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002490 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002491 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002492 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002493
Felipe Balbiee3638b2018-03-27 11:26:53 +03002494 dwc3_gadget_endpoint_frame_from_event(dep, event);
2495
Felipe Balbi72246da2011-08-19 18:10:58 +03002496 if (event->status & DEPEVT_STATUS_BUSERR)
2497 status = -ECONNRESET;
2498
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002499 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2500 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002501
2502 if (list_empty(&dep->started_list))
2503 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002504 }
2505
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002506 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002507
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002508 if (stop) {
2509 dwc3_stop_active_transfer(dep, true);
2510 dep->flags = DWC3_EP_ENABLED;
2511 }
2512
Felipe Balbifae2b902011-10-14 13:00:30 +03002513 /*
2514 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2515 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2516 */
2517 if (dwc->revision < DWC3_REVISION_183A) {
2518 u32 reg;
2519 int i;
2520
2521 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002522 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002523
2524 if (!(dep->flags & DWC3_EP_ENABLED))
2525 continue;
2526
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002527 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002528 return;
2529 }
2530
2531 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2532 reg |= dwc->u1u2;
2533 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2534
2535 dwc->u1u2 = 0;
2536 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002537}
2538
Felipe Balbi8f608e82018-03-27 10:53:29 +03002539static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2540 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002541{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002542 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002543 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002544}
2545
Felipe Balbi72246da2011-08-19 18:10:58 +03002546static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2547 const struct dwc3_event_depevt *event)
2548{
2549 struct dwc3_ep *dep;
2550 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002551 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002552
2553 dep = dwc->eps[epnum];
2554
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002555 if (!(dep->flags & DWC3_EP_ENABLED)) {
2556 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2557 return;
2558
2559 /* Handle only EPCMDCMPLT when EP disabled */
2560 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2561 return;
2562 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002563
Felipe Balbi72246da2011-08-19 18:10:58 +03002564 if (epnum == 0 || epnum == 1) {
2565 dwc3_ep0_interrupt(dwc, event);
2566 return;
2567 }
2568
2569 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002570 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002571 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 break;
2573 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002574 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002575 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002576 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002577 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2578
2579 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2580 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2581 wake_up(&dep->wait_end_transfer);
2582 }
2583 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002584 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002585 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002586 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002587 break;
2588 }
2589}
2590
2591static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2592{
2593 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2594 spin_unlock(&dwc->lock);
2595 dwc->gadget_driver->disconnect(&dwc->gadget);
2596 spin_lock(&dwc->lock);
2597 }
2598}
2599
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002600static void dwc3_suspend_gadget(struct dwc3 *dwc)
2601{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002602 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002603 spin_unlock(&dwc->lock);
2604 dwc->gadget_driver->suspend(&dwc->gadget);
2605 spin_lock(&dwc->lock);
2606 }
2607}
2608
2609static void dwc3_resume_gadget(struct dwc3 *dwc)
2610{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002611 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002612 spin_unlock(&dwc->lock);
2613 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002614 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002615 }
2616}
2617
2618static void dwc3_reset_gadget(struct dwc3 *dwc)
2619{
2620 if (!dwc->gadget_driver)
2621 return;
2622
2623 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2624 spin_unlock(&dwc->lock);
2625 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002626 spin_lock(&dwc->lock);
2627 }
2628}
2629
Felipe Balbi8f608e82018-03-27 10:53:29 +03002630static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002631{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002632 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002633 struct dwc3_gadget_ep_cmd_params params;
2634 u32 cmd;
2635 int ret;
2636
Baolin Wang76a638f2016-10-31 19:38:36 +08002637 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2638 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302639 return;
2640
Pratyush Anand57911502012-07-06 15:19:10 +05302641 /*
2642 * NOTICE: We are violating what the Databook says about the
2643 * EndTransfer command. Ideally we would _always_ wait for the
2644 * EndTransfer Command Completion IRQ, but that's causing too
2645 * much trouble synchronizing between us and gadget driver.
2646 *
2647 * We have discussed this with the IP Provider and it was
2648 * suggested to giveback all requests here, but give HW some
2649 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002650 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302651 *
2652 * Note also that a similar handling was tested by Synopsys
2653 * (thanks a lot Paul) and nothing bad has come out of it.
2654 * In short, what we're doing is:
2655 *
2656 * - Issue EndTransfer WITH CMDIOC bit set
2657 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002658 *
2659 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2660 * supports a mode to work around the above limitation. The
2661 * software can poll the CMDACT bit in the DEPCMD register
2662 * after issuing a EndTransfer command. This mode is enabled
2663 * by writing GUCTL2[14]. This polling is already done in the
2664 * dwc3_send_gadget_ep_cmd() function so if the mode is
2665 * enabled, the EndTransfer command will have completed upon
2666 * returning from this function and we don't need to delay for
2667 * 100us.
2668 *
2669 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302670 */
2671
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302672 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002673 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2674 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002675 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302676 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002677 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302678 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002679 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002680
Baolin Wang76a638f2016-10-31 19:38:36 +08002681 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2682 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002683 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002684 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002685}
2686
Felipe Balbi72246da2011-08-19 18:10:58 +03002687static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2688{
2689 u32 epnum;
2690
2691 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2692 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002693 int ret;
2694
2695 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002696 if (!dep)
2697 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002698
2699 if (!(dep->flags & DWC3_EP_STALL))
2700 continue;
2701
2702 dep->flags &= ~DWC3_EP_STALL;
2703
John Youn50c763f2016-05-31 17:49:56 -07002704 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 WARN_ON_ONCE(ret);
2706 }
2707}
2708
2709static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2710{
Felipe Balbic4430a22012-05-24 10:30:01 +03002711 int reg;
2712
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2714 reg &= ~DWC3_DCTL_INITU1ENA;
2715 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2716
2717 reg &= ~DWC3_DCTL_INITU2ENA;
2718 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002719
Felipe Balbi72246da2011-08-19 18:10:58 +03002720 dwc3_disconnect_gadget(dwc);
2721
2722 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002723 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002724 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002725
2726 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002727}
2728
Felipe Balbi72246da2011-08-19 18:10:58 +03002729static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2730{
2731 u32 reg;
2732
Felipe Balbifc8bb912016-05-16 13:14:48 +03002733 dwc->connected = true;
2734
Felipe Balbidf62df52011-10-14 15:11:49 +03002735 /*
2736 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2737 * would cause a missing Disconnect Event if there's a
2738 * pending Setup Packet in the FIFO.
2739 *
2740 * There's no suggested workaround on the official Bug
2741 * report, which states that "unless the driver/application
2742 * is doing any special handling of a disconnect event,
2743 * there is no functional issue".
2744 *
2745 * Unfortunately, it turns out that we _do_ some special
2746 * handling of a disconnect event, namely complete all
2747 * pending transfers, notify gadget driver of the
2748 * disconnection, and so on.
2749 *
2750 * Our suggested workaround is to follow the Disconnect
2751 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002752 * flag. Such flag gets set whenever we have a SETUP_PENDING
2753 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002754 * same endpoint.
2755 *
2756 * Refers to:
2757 *
2758 * STAR#9000466709: RTL: Device : Disconnect event not
2759 * generated if setup packet pending in FIFO
2760 */
2761 if (dwc->revision < DWC3_REVISION_188A) {
2762 if (dwc->setup_packet_pending)
2763 dwc3_gadget_disconnect_interrupt(dwc);
2764 }
2765
Felipe Balbi8e744752014-11-06 14:27:53 +08002766 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002767
2768 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2769 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2770 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002771 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002772 dwc3_clear_stall_all_ep(dwc);
2773
2774 /* Reset device address to zero */
2775 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2776 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2777 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002778}
2779
Felipe Balbi72246da2011-08-19 18:10:58 +03002780static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2781{
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 struct dwc3_ep *dep;
2783 int ret;
2784 u32 reg;
2785 u8 speed;
2786
Felipe Balbi72246da2011-08-19 18:10:58 +03002787 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2788 speed = reg & DWC3_DSTS_CONNECTSPD;
2789 dwc->speed = speed;
2790
John Youn5fb6fda2016-11-10 17:23:25 -08002791 /*
2792 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2793 * each time on Connect Done.
2794 *
2795 * Currently we always use the reset value. If any platform
2796 * wants to set this to a different value, we need to add a
2797 * setting and update GCTL.RAMCLKSEL here.
2798 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002799
2800 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002801 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002802 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2803 dwc->gadget.ep0->maxpacket = 512;
2804 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2805 break;
John Youn2da9ad72016-05-20 16:34:26 -07002806 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002807 /*
2808 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2809 * would cause a missing USB3 Reset event.
2810 *
2811 * In such situations, we should force a USB3 Reset
2812 * event by calling our dwc3_gadget_reset_interrupt()
2813 * routine.
2814 *
2815 * Refers to:
2816 *
2817 * STAR#9000483510: RTL: SS : USB3 reset event may
2818 * not be generated always when the link enters poll
2819 */
2820 if (dwc->revision < DWC3_REVISION_190A)
2821 dwc3_gadget_reset_interrupt(dwc);
2822
Felipe Balbi72246da2011-08-19 18:10:58 +03002823 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2824 dwc->gadget.ep0->maxpacket = 512;
2825 dwc->gadget.speed = USB_SPEED_SUPER;
2826 break;
John Youn2da9ad72016-05-20 16:34:26 -07002827 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002828 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2829 dwc->gadget.ep0->maxpacket = 64;
2830 dwc->gadget.speed = USB_SPEED_HIGH;
2831 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002832 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002833 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2834 dwc->gadget.ep0->maxpacket = 64;
2835 dwc->gadget.speed = USB_SPEED_FULL;
2836 break;
John Youn2da9ad72016-05-20 16:34:26 -07002837 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002838 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2839 dwc->gadget.ep0->maxpacket = 8;
2840 dwc->gadget.speed = USB_SPEED_LOW;
2841 break;
2842 }
2843
Thinh Nguyen61800262018-01-12 18:18:05 -08002844 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2845
Pratyush Anand2b758352013-01-14 15:59:31 +05302846 /* Enable USB2 LPM Capability */
2847
John Younee5cd412016-02-05 17:08:45 -08002848 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002849 (speed != DWC3_DSTS_SUPERSPEED) &&
2850 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302851 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2852 reg |= DWC3_DCFG_LPM_CAP;
2853 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2854
2855 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2856 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2857
Huang Rui460d0982014-10-31 11:11:18 +08002858 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302859
Huang Rui80caf7d2014-10-28 19:54:26 +08002860 /*
2861 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2862 * DCFG.LPMCap is set, core responses with an ACK and the
2863 * BESL value in the LPM token is less than or equal to LPM
2864 * NYET threshold.
2865 */
2866 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2867 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002868 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002869
2870 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2871 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2872
Pratyush Anand2b758352013-01-14 15:59:31 +05302873 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002874 } else {
2875 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2876 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2877 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302878 }
2879
Felipe Balbi72246da2011-08-19 18:10:58 +03002880 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002881 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002882 if (ret) {
2883 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2884 return;
2885 }
2886
2887 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002888 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 if (ret) {
2890 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2891 return;
2892 }
2893
2894 /*
2895 * Configure PHY via GUSB3PIPECTLn if required.
2896 *
2897 * Update GTXFIFOSIZn
2898 *
2899 * In both cases reset values should be sufficient.
2900 */
2901}
2902
2903static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2904{
Felipe Balbi72246da2011-08-19 18:10:58 +03002905 /*
2906 * TODO take core out of low power mode when that's
2907 * implemented.
2908 */
2909
Jiebing Liad14d4e2014-12-11 13:26:29 +08002910 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2911 spin_unlock(&dwc->lock);
2912 dwc->gadget_driver->resume(&dwc->gadget);
2913 spin_lock(&dwc->lock);
2914 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002915}
2916
2917static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2918 unsigned int evtinfo)
2919{
Felipe Balbifae2b902011-10-14 13:00:30 +03002920 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002921 unsigned int pwropt;
2922
2923 /*
2924 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2925 * Hibernation mode enabled which would show up when device detects
2926 * host-initiated U3 exit.
2927 *
2928 * In that case, device will generate a Link State Change Interrupt
2929 * from U3 to RESUME which is only necessary if Hibernation is
2930 * configured in.
2931 *
2932 * There are no functional changes due to such spurious event and we
2933 * just need to ignore it.
2934 *
2935 * Refers to:
2936 *
2937 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2938 * operational mode
2939 */
2940 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2941 if ((dwc->revision < DWC3_REVISION_250A) &&
2942 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2943 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2944 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002945 return;
2946 }
2947 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002948
2949 /*
2950 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2951 * on the link partner, the USB session might do multiple entry/exit
2952 * of low power states before a transfer takes place.
2953 *
2954 * Due to this problem, we might experience lower throughput. The
2955 * suggested workaround is to disable DCTL[12:9] bits if we're
2956 * transitioning from U1/U2 to U0 and enable those bits again
2957 * after a transfer completes and there are no pending transfers
2958 * on any of the enabled endpoints.
2959 *
2960 * This is the first half of that workaround.
2961 *
2962 * Refers to:
2963 *
2964 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2965 * core send LGO_Ux entering U0
2966 */
2967 if (dwc->revision < DWC3_REVISION_183A) {
2968 if (next == DWC3_LINK_STATE_U0) {
2969 u32 u1u2;
2970 u32 reg;
2971
2972 switch (dwc->link_state) {
2973 case DWC3_LINK_STATE_U1:
2974 case DWC3_LINK_STATE_U2:
2975 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2976 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2977 | DWC3_DCTL_ACCEPTU2ENA
2978 | DWC3_DCTL_INITU1ENA
2979 | DWC3_DCTL_ACCEPTU1ENA);
2980
2981 if (!dwc->u1u2)
2982 dwc->u1u2 = reg & u1u2;
2983
2984 reg &= ~u1u2;
2985
2986 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2987 break;
2988 default:
2989 /* do nothing */
2990 break;
2991 }
2992 }
2993 }
2994
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002995 switch (next) {
2996 case DWC3_LINK_STATE_U1:
2997 if (dwc->speed == USB_SPEED_SUPER)
2998 dwc3_suspend_gadget(dwc);
2999 break;
3000 case DWC3_LINK_STATE_U2:
3001 case DWC3_LINK_STATE_U3:
3002 dwc3_suspend_gadget(dwc);
3003 break;
3004 case DWC3_LINK_STATE_RESUME:
3005 dwc3_resume_gadget(dwc);
3006 break;
3007 default:
3008 /* do nothing */
3009 break;
3010 }
3011
Felipe Balbie57ebc12014-04-22 13:20:12 -05003012 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003013}
3014
Baolin Wang72704f82016-05-16 16:43:53 +08003015static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3016 unsigned int evtinfo)
3017{
3018 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3019
3020 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3021 dwc3_suspend_gadget(dwc);
3022
3023 dwc->link_state = next;
3024}
3025
Felipe Balbie1dadd32014-02-25 14:47:54 -06003026static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3027 unsigned int evtinfo)
3028{
3029 unsigned int is_ss = evtinfo & BIT(4);
3030
Felipe Balbibfad65e2017-04-19 14:59:27 +03003031 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003032 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3033 * have a known issue which can cause USB CV TD.9.23 to fail
3034 * randomly.
3035 *
3036 * Because of this issue, core could generate bogus hibernation
3037 * events which SW needs to ignore.
3038 *
3039 * Refers to:
3040 *
3041 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3042 * Device Fallback from SuperSpeed
3043 */
3044 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3045 return;
3046
3047 /* enter hibernation here */
3048}
3049
Felipe Balbi72246da2011-08-19 18:10:58 +03003050static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3051 const struct dwc3_event_devt *event)
3052{
3053 switch (event->type) {
3054 case DWC3_DEVICE_EVENT_DISCONNECT:
3055 dwc3_gadget_disconnect_interrupt(dwc);
3056 break;
3057 case DWC3_DEVICE_EVENT_RESET:
3058 dwc3_gadget_reset_interrupt(dwc);
3059 break;
3060 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3061 dwc3_gadget_conndone_interrupt(dwc);
3062 break;
3063 case DWC3_DEVICE_EVENT_WAKEUP:
3064 dwc3_gadget_wakeup_interrupt(dwc);
3065 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003066 case DWC3_DEVICE_EVENT_HIBER_REQ:
3067 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3068 "unexpected hibernation event\n"))
3069 break;
3070
3071 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3072 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003073 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3074 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3075 break;
3076 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003077 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003078 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08003079 /*
3080 * Ignore suspend event until the gadget enters into
3081 * USB_STATE_CONFIGURED state.
3082 */
3083 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3084 dwc3_gadget_suspend_interrupt(dwc,
3085 event->event_info);
3086 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003087 break;
3088 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003089 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003090 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003091 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003092 break;
3093 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003094 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003095 }
3096}
3097
3098static void dwc3_process_event_entry(struct dwc3 *dwc,
3099 const union dwc3_event *event)
3100{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003101 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003102
Felipe Balbidfc5e802017-04-26 13:44:51 +03003103 if (!event->type.is_devspec)
3104 dwc3_endpoint_interrupt(dwc, &event->depevt);
3105 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003106 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003107 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003108 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003109}
3110
Felipe Balbidea520a2016-03-30 09:39:34 +03003111static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003112{
Felipe Balbidea520a2016-03-30 09:39:34 +03003113 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003114 irqreturn_t ret = IRQ_NONE;
3115 int left;
3116 u32 reg;
3117
Felipe Balbif42f2442013-06-12 21:25:08 +03003118 left = evt->count;
3119
3120 if (!(evt->flags & DWC3_EVENT_PENDING))
3121 return IRQ_NONE;
3122
3123 while (left > 0) {
3124 union dwc3_event event;
3125
John Younebbb2d52016-11-15 13:07:02 +02003126 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003127
3128 dwc3_process_event_entry(dwc, &event);
3129
3130 /*
3131 * FIXME we wrap around correctly to the next entry as
3132 * almost all entries are 4 bytes in size. There is one
3133 * entry which has 12 bytes which is a regular entry
3134 * followed by 8 bytes data. ATM I don't know how
3135 * things are organized if we get next to the a
3136 * boundary so I worry about that once we try to handle
3137 * that.
3138 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003139 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003140 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003141 }
3142
3143 evt->count = 0;
3144 evt->flags &= ~DWC3_EVENT_PENDING;
3145 ret = IRQ_HANDLED;
3146
3147 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003148 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003149 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003150 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003151
John Youncf40b862016-11-14 12:32:43 -08003152 if (dwc->imod_interval) {
3153 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3154 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3155 }
3156
Felipe Balbif42f2442013-06-12 21:25:08 +03003157 return ret;
3158}
3159
Felipe Balbidea520a2016-03-30 09:39:34 +03003160static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003161{
Felipe Balbidea520a2016-03-30 09:39:34 +03003162 struct dwc3_event_buffer *evt = _evt;
3163 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003164 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003165 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003166
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003167 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003168 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003169 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003170
3171 return ret;
3172}
3173
Felipe Balbidea520a2016-03-30 09:39:34 +03003174static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003175{
Felipe Balbidea520a2016-03-30 09:39:34 +03003176 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003177 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003178 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003179 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003180
Felipe Balbifc8bb912016-05-16 13:14:48 +03003181 if (pm_runtime_suspended(dwc->dev)) {
3182 pm_runtime_get(dwc->dev);
3183 disable_irq_nosync(dwc->irq_gadget);
3184 dwc->pending_events = true;
3185 return IRQ_HANDLED;
3186 }
3187
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003188 /*
3189 * With PCIe legacy interrupt, test shows that top-half irq handler can
3190 * be called again after HW interrupt deassertion. Check if bottom-half
3191 * irq event handler completes before caching new event to prevent
3192 * losing events.
3193 */
3194 if (evt->flags & DWC3_EVENT_PENDING)
3195 return IRQ_HANDLED;
3196
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003197 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003198 count &= DWC3_GEVNTCOUNT_MASK;
3199 if (!count)
3200 return IRQ_NONE;
3201
Felipe Balbib15a7622011-06-30 16:57:15 +03003202 evt->count = count;
3203 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003204
Felipe Balbie8adfc32013-06-12 21:11:14 +03003205 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003206 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003207 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003208 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003209
John Younebbb2d52016-11-15 13:07:02 +02003210 amount = min(count, evt->length - evt->lpos);
3211 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3212
3213 if (amount < count)
3214 memcpy(evt->cache, evt->buf, count - amount);
3215
John Youn65aca322016-11-15 13:08:59 +02003216 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3217
Felipe Balbib15a7622011-06-30 16:57:15 +03003218 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003219}
3220
Felipe Balbidea520a2016-03-30 09:39:34 +03003221static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003222{
Felipe Balbidea520a2016-03-30 09:39:34 +03003223 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003224
Felipe Balbidea520a2016-03-30 09:39:34 +03003225 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003226}
3227
Felipe Balbi6db38122016-10-03 11:27:01 +03003228static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3229{
3230 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3231 int irq;
3232
3233 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3234 if (irq > 0)
3235 goto out;
3236
3237 if (irq == -EPROBE_DEFER)
3238 goto out;
3239
3240 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3241 if (irq > 0)
3242 goto out;
3243
3244 if (irq == -EPROBE_DEFER)
3245 goto out;
3246
3247 irq = platform_get_irq(dwc3_pdev, 0);
3248 if (irq > 0)
3249 goto out;
3250
3251 if (irq != -EPROBE_DEFER)
3252 dev_err(dwc->dev, "missing peripheral IRQ\n");
3253
3254 if (!irq)
3255 irq = -EINVAL;
3256
3257out:
3258 return irq;
3259}
3260
Felipe Balbi72246da2011-08-19 18:10:58 +03003261/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003262 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003263 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003264 *
3265 * Returns 0 on success otherwise negative errno.
3266 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003267int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003268{
Felipe Balbi6db38122016-10-03 11:27:01 +03003269 int ret;
3270 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003271
Felipe Balbi6db38122016-10-03 11:27:01 +03003272 irq = dwc3_gadget_get_irq(dwc);
3273 if (irq < 0) {
3274 ret = irq;
3275 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003276 }
3277
3278 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003279
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303280 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3281 sizeof(*dwc->ep0_trb) * 2,
3282 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003283 if (!dwc->ep0_trb) {
3284 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3285 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003286 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003287 }
3288
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003289 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003290 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003291 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003292 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003293 }
3294
Felipe Balbi905dc042017-01-05 14:46:52 +02003295 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3296 &dwc->bounce_addr, GFP_KERNEL);
3297 if (!dwc->bounce) {
3298 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003299 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003300 }
3301
Baolin Wangbb014732016-10-14 17:11:33 +08003302 init_completion(&dwc->ep0_in_setup);
3303
Felipe Balbi72246da2011-08-19 18:10:58 +03003304 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003305 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003306 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003307 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003308 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003309
3310 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003311 * FIXME We might be setting max_speed to <SUPER, however versions
3312 * <2.20a of dwc3 have an issue with metastability (documented
3313 * elsewhere in this driver) which tells us we can't set max speed to
3314 * anything lower than SUPER.
3315 *
3316 * Because gadget.max_speed is only used by composite.c and function
3317 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3318 * to happen so we avoid sending SuperSpeed Capability descriptor
3319 * together with our BOS descriptor as that could confuse host into
3320 * thinking we can handle super speed.
3321 *
3322 * Note that, in fact, we won't even support GetBOS requests when speed
3323 * is less than super speed because we don't have means, yet, to tell
3324 * composite.c that we are USB 2.0 + LPM ECN.
3325 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003326 if (dwc->revision < DWC3_REVISION_220A &&
3327 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003328 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003329 dwc->revision);
3330
3331 dwc->gadget.max_speed = dwc->maximum_speed;
3332
3333 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003334 * REVISIT: Here we should clear all pending IRQs to be
3335 * sure we're starting from a well known location.
3336 */
3337
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003338 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003339 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003340 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003341
Felipe Balbi72246da2011-08-19 18:10:58 +03003342 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3343 if (ret) {
3344 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003345 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003346 }
3347
3348 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003349
3350err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003351 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003352
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003353err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003354 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3355 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003356
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003357err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003358 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003359
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003360err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303361 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003362 dwc->ep0_trb, dwc->ep0_trb_addr);
3363
Felipe Balbi72246da2011-08-19 18:10:58 +03003364err0:
3365 return ret;
3366}
3367
Felipe Balbi7415f172012-04-30 14:56:33 +03003368/* -------------------------------------------------------------------------- */
3369
Felipe Balbi72246da2011-08-19 18:10:58 +03003370void dwc3_gadget_exit(struct dwc3 *dwc)
3371{
Felipe Balbi72246da2011-08-19 18:10:58 +03003372 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003373 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003374 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003375 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003376 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303377 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003378 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003379}
Felipe Balbi7415f172012-04-30 14:56:33 +03003380
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003381int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003382{
Roger Quadros9772b472016-04-12 11:33:29 +03003383 if (!dwc->gadget_driver)
3384 return 0;
3385
Roger Quadros1551e352017-02-15 14:16:26 +02003386 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003387 dwc3_disconnect_gadget(dwc);
3388 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003389
3390 return 0;
3391}
3392
3393int dwc3_gadget_resume(struct dwc3 *dwc)
3394{
Felipe Balbi7415f172012-04-30 14:56:33 +03003395 int ret;
3396
Roger Quadros9772b472016-04-12 11:33:29 +03003397 if (!dwc->gadget_driver)
3398 return 0;
3399
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003400 ret = __dwc3_gadget_start(dwc);
3401 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003402 goto err0;
3403
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003404 ret = dwc3_gadget_run_stop(dwc, true, false);
3405 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003406 goto err1;
3407
Felipe Balbi7415f172012-04-30 14:56:33 +03003408 return 0;
3409
3410err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003411 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003412
3413err0:
3414 return ret;
3415}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003416
3417void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3418{
3419 if (dwc->pending_events) {
3420 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3421 dwc->pending_events = false;
3422 enable_irq(dwc->irq_gadget);
3423 }
3424}