blob: 98d8a4549103a1f182c2108c6b8f61aa2973d0a7 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
149{
150 dep->trb_enqueue++;
151}
152
153static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
154{
155 dep->trb_dequeue++;
156}
157
158static int dwc3_ep_is_last_trb(unsigned int index)
159{
160 return (index % DWC3_TRB_NUM) == (DWC3_TRB_NUM - 1);
161}
162
Felipe Balbi72246da2011-08-19 18:10:58 +0300163void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
164 int status)
165{
166 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530167 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300168
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200169 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530170 i = 0;
171 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300172 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530173 /*
174 * Skip LINK TRB. We can't use req->trb and check for
175 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
176 * just completed (not the LINK TRB).
177 */
Felipe Balbief966b92016-04-05 13:09:51 +0300178 if (dwc3_ep_is_last_trb(dep->trb_dequeue) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200179 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbief966b92016-04-05 13:09:51 +0300180 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530181 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200182 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300183 }
184 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200185 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300186
187 if (req->request.status == -EINPROGRESS)
188 req->request.status = status;
189
Pratyush Anand0416e492012-08-10 13:42:16 +0530190 if (dwc->ep0_bounced && dep->number == 0)
191 dwc->ep0_bounced = false;
192 else
193 usb_gadget_unmap_request(&dwc->gadget, &req->request,
194 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300195
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500196 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300197
198 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200199 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300200 spin_lock(&dwc->lock);
201}
202
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500203int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300204{
205 u32 timeout = 500;
206 u32 reg;
207
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500208 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500209
Felipe Balbib09bb642012-04-24 16:19:11 +0300210 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
211 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
212
213 do {
214 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
215 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600216 dwc3_trace(trace_dwc3_gadget,
217 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300218 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530219 if (DWC3_DGCMD_STATUS(reg))
220 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300221 return 0;
222 }
223
224 /*
225 * We can't sleep here, because it's also called from
226 * interrupt context.
227 */
228 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600229 if (!timeout) {
230 dwc3_trace(trace_dwc3_gadget,
231 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300232 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600233 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300234 udelay(1);
235 } while (1);
236}
237
Felipe Balbic36d8e92016-04-04 12:46:33 +0300238static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
241 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
242{
243 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200244 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300245 u32 reg;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300246
247 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300248 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300249
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500250 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300251
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300252 /*
253 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
254 * we're issuing an endpoint command, we must check if
255 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
256 *
257 * We will also set SUSPHY bit to what it was before returning as stated
258 * by the same section on Synopsys databook.
259 */
260 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
261 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
262 susphy = true;
263 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
264 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
265 }
266
Felipe Balbic36d8e92016-04-04 12:46:33 +0300267 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
268 int needs_wakeup;
269
270 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
271 dwc->link_state == DWC3_LINK_STATE_U2 ||
272 dwc->link_state == DWC3_LINK_STATE_U3);
273
274 if (unlikely(needs_wakeup)) {
275 ret = __dwc3_gadget_wakeup(dwc);
276 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
277 ret);
278 }
279 }
280
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300281 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
282 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
283 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300284
285 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
286 do {
287 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
288 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600289 dwc3_trace(trace_dwc3_gadget,
290 "Command Complete --> %d",
Felipe Balbi164f6e12011-08-27 20:29:58 +0300291 DWC3_DEPCMD_STATUS(reg));
Subbaraya Sundeep Bhatta76e838c2015-05-21 15:46:48 +0530292 if (DWC3_DEPCMD_STATUS(reg))
Felipe Balbic0ca3242016-04-04 09:11:51 +0300293 break;
294 ret = 0;
295 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 }
297
298 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 * We can't sleep here, because it is also called from
300 * interrupt context.
301 */
302 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600303 if (!timeout) {
304 dwc3_trace(trace_dwc3_gadget,
305 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300306 ret = -ETIMEDOUT;
307 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600308 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300309
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200310 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300311 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300312
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300313 if (unlikely(susphy)) {
314 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
315 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
316 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
317 }
318
Felipe Balbic0ca3242016-04-04 09:11:51 +0300319 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300320}
321
322static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200323 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300324{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300325 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300326
327 return dep->trb_pool_dma + offset;
328}
329
330static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
331{
332 struct dwc3 *dwc = dep->dwc;
333
334 if (dep->trb_pool)
335 return 0;
336
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 dep->trb_pool = dma_alloc_coherent(dwc->dev,
338 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
339 &dep->trb_pool_dma, GFP_KERNEL);
340 if (!dep->trb_pool) {
341 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
342 dep->name);
343 return -ENOMEM;
344 }
345
346 return 0;
347}
348
349static void dwc3_free_trb_pool(struct dwc3_ep *dep)
350{
351 struct dwc3 *dwc = dep->dwc;
352
353 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
354 dep->trb_pool, dep->trb_pool_dma);
355
356 dep->trb_pool = NULL;
357 dep->trb_pool_dma = 0;
358}
359
John Younc4509602016-02-16 20:10:53 -0800360static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
361
362/**
363 * dwc3_gadget_start_config - Configure EP resources
364 * @dwc: pointer to our controller context structure
365 * @dep: endpoint that is being enabled
366 *
367 * The assignment of transfer resources cannot perfectly follow the
368 * data book due to the fact that the controller driver does not have
369 * all knowledge of the configuration in advance. It is given this
370 * information piecemeal by the composite gadget framework after every
371 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
372 * programming model in this scenario can cause errors. For two
373 * reasons:
374 *
375 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
376 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
377 * multiple interfaces.
378 *
379 * 2) The databook does not mention doing more DEPXFERCFG for new
380 * endpoint on alt setting (8.1.6).
381 *
382 * The following simplified method is used instead:
383 *
384 * All hardware endpoints can be assigned a transfer resource and this
385 * setting will stay persistent until either a core reset or
386 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
387 * do DEPXFERCFG for every hardware endpoint as well. We are
388 * guaranteed that there are as many transfer resources as endpoints.
389 *
390 * This function is called for each endpoint when it is being enabled
391 * but is triggered only when called for EP0-out, which always happens
392 * first, and which should only happen in one of the above conditions.
393 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300394static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
395{
396 struct dwc3_gadget_ep_cmd_params params;
397 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800398 int i;
399 int ret;
400
401 if (dep->number)
402 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300403
404 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800405 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406
John Younc4509602016-02-16 20:10:53 -0800407 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
408 if (ret)
409 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300410
John Younc4509602016-02-16 20:10:53 -0800411 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
412 struct dwc3_ep *dep = dwc->eps[i];
413
414 if (!dep)
415 continue;
416
417 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
418 if (ret)
419 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300420 }
421
422 return 0;
423}
424
425static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200426 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300427 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600428 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300429{
430 struct dwc3_gadget_ep_cmd_params params;
431
432 memset(&params, 0x00, sizeof(params));
433
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300434 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900435 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
436
437 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800438 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Chanho Parkd2e9a132012-08-31 16:54:07 +0900439 u32 burst = dep->endpoint.maxburst - 1;
440
441 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
442 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Felipe Balbi4b345c92012-07-16 14:08:16 +0300444 if (ignore)
445 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
446
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600447 if (restore) {
448 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
449 params.param2 |= dep->saved_state;
450 }
451
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300452 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
453 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300454
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200455 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300456 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
457 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300458 dep->stream_capable = true;
459 }
460
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500461 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300462 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300463
464 /*
465 * We are doing 1:1 mapping for endpoints, meaning
466 * Physical Endpoints 2 maps to Logical Endpoint 2 and
467 * so on. We consider the direction bit as part of the physical
468 * endpoint number. So USB endpoint 0x81 is 0x03.
469 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300470 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300471
472 /*
473 * We must use the lower 16 TX FIFOs even though
474 * HW might have more
475 */
476 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300477 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300478
479 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300480 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300481 dep->interval = 1 << (desc->bInterval - 1);
482 }
483
484 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
485 DWC3_DEPCMD_SETEPCONFIG, &params);
486}
487
488static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
489{
490 struct dwc3_gadget_ep_cmd_params params;
491
492 memset(&params, 0x00, sizeof(params));
493
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300494 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300495
496 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
497 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
498}
499
500/**
501 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
502 * @dep: endpoint to be initialized
503 * @desc: USB Endpoint Descriptor
504 *
505 * Caller should take care of locking
506 */
507static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200508 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300509 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600510 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300511{
512 struct dwc3 *dwc = dep->dwc;
513 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300514 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300515
Felipe Balbi73815282015-01-27 13:48:14 -0600516 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300517
Felipe Balbi72246da2011-08-19 18:10:58 +0300518 if (!(dep->flags & DWC3_EP_ENABLED)) {
519 ret = dwc3_gadget_start_config(dwc, dep);
520 if (ret)
521 return ret;
522 }
523
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600524 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
525 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300526 if (ret)
527 return ret;
528
529 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200530 struct dwc3_trb *trb_st_hw;
531 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200533 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200534 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 dep->type = usb_endpoint_type(desc);
536 dep->flags |= DWC3_EP_ENABLED;
537
538 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
539 reg |= DWC3_DALEPENA_EP(dep->number);
540 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
541
542 if (!usb_endpoint_xfer_isoc(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200543 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
Paul Zimmerman1d046792012-02-15 18:56:56 -0800545 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300546 trb_st_hw = &dep->trb_pool[0];
547
Felipe Balbif6bafc62012-02-06 11:04:53 +0200548 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700549 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300550
Felipe Balbif6bafc62012-02-06 11:04:53 +0200551 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
552 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
553 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
554 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 }
556
Felipe Balbie901aa12016-03-16 14:01:37 +0200557out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500558 switch (usb_endpoint_type(desc)) {
559 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200560 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500561 break;
562 case USB_ENDPOINT_XFER_ISOC:
563 strlcat(dep->name, "-isoc", sizeof(dep->name));
564 break;
565 case USB_ENDPOINT_XFER_BULK:
566 strlcat(dep->name, "-bulk", sizeof(dep->name));
567 break;
568 case USB_ENDPOINT_XFER_INT:
569 strlcat(dep->name, "-int", sizeof(dep->name));
570 break;
571 default:
572 dev_err(dwc->dev, "invalid endpoint transfer type\n");
573 }
574
Felipe Balbi72246da2011-08-19 18:10:58 +0300575 return 0;
576}
577
Paul Zimmermanb992e682012-04-27 14:17:35 +0300578static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200579static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300580{
581 struct dwc3_request *req;
582
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200583 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300584 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200585
Pratyush Anand57911502012-07-06 15:19:10 +0530586 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200587 while (!list_empty(&dep->started_list)) {
588 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530589
590 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
591 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200592 }
593
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200594 while (!list_empty(&dep->pending_list)) {
595 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200597 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300598 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300599}
600
601/**
602 * __dwc3_gadget_ep_disable - Disables a HW endpoint
603 * @dep: the endpoint to disable
604 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200605 * This function also removes requests which are currently processed ny the
606 * hardware and those which are not yet scheduled.
607 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300608 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300609static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
610{
611 struct dwc3 *dwc = dep->dwc;
612 u32 reg;
613
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500614 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
615
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200616 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300617
Felipe Balbi687ef982014-04-16 10:30:33 -0500618 /* make sure HW endpoint isn't stalled */
619 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500620 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500621
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
623 reg &= ~DWC3_DALEPENA_EP(dep->number);
624 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
625
Felipe Balbi879631a2011-09-30 10:58:47 +0300626 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200627 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200628 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300630 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300631
Felipe Balbiaa739972015-07-20 14:48:13 -0500632 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
633 dep->number >> 1,
634 (dep->number & 1) ? "in" : "out");
635
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 return 0;
637}
638
639/* -------------------------------------------------------------------------- */
640
641static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
642 const struct usb_endpoint_descriptor *desc)
643{
644 return -EINVAL;
645}
646
647static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
648{
649 return -EINVAL;
650}
651
652/* -------------------------------------------------------------------------- */
653
654static int dwc3_gadget_ep_enable(struct usb_ep *ep,
655 const struct usb_endpoint_descriptor *desc)
656{
657 struct dwc3_ep *dep;
658 struct dwc3 *dwc;
659 unsigned long flags;
660 int ret;
661
662 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
663 pr_debug("dwc3: invalid parameters\n");
664 return -EINVAL;
665 }
666
667 if (!desc->wMaxPacketSize) {
668 pr_debug("dwc3: missing wMaxPacketSize\n");
669 return -EINVAL;
670 }
671
672 dep = to_dwc3_ep(ep);
673 dwc = dep->dwc;
674
Felipe Balbi95ca9612015-12-10 13:08:20 -0600675 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
676 "%s is already enabled\n",
677 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300678 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300679
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600681 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 spin_unlock_irqrestore(&dwc->lock, flags);
683
684 return ret;
685}
686
687static int dwc3_gadget_ep_disable(struct usb_ep *ep)
688{
689 struct dwc3_ep *dep;
690 struct dwc3 *dwc;
691 unsigned long flags;
692 int ret;
693
694 if (!ep) {
695 pr_debug("dwc3: invalid parameters\n");
696 return -EINVAL;
697 }
698
699 dep = to_dwc3_ep(ep);
700 dwc = dep->dwc;
701
Felipe Balbi95ca9612015-12-10 13:08:20 -0600702 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
703 "%s is already disabled\n",
704 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300706
Felipe Balbi72246da2011-08-19 18:10:58 +0300707 spin_lock_irqsave(&dwc->lock, flags);
708 ret = __dwc3_gadget_ep_disable(dep);
709 spin_unlock_irqrestore(&dwc->lock, flags);
710
711 return ret;
712}
713
714static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
715 gfp_t gfp_flags)
716{
717 struct dwc3_request *req;
718 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300719
720 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900721 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300723
724 req->epnum = dep->number;
725 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300726
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500727 trace_dwc3_alloc_request(req);
728
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 return &req->request;
730}
731
732static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
733 struct usb_request *request)
734{
735 struct dwc3_request *req = to_dwc3_request(request);
736
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500737 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 kfree(req);
739}
740
Felipe Balbic71fc372011-11-22 11:37:34 +0200741/**
742 * dwc3_prepare_one_trb - setup one TRB from one request
743 * @dep: endpoint for which this request is prepared
744 * @req: dwc3_request pointer
745 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200746static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200747 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530748 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200749{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200750 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200751
Felipe Balbi73815282015-01-27 13:48:14 -0600752 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200753 dep->name, req, (unsigned long long) dma,
754 length, last ? " last" : "",
755 chain ? " chain" : "");
756
Pratyush Anand915e2022013-01-14 15:59:35 +0530757
Felipe Balbi70fdb272016-04-05 12:47:15 +0300758 trb = &dep->trb_pool[dep->trb_enqueue % DWC3_TRB_NUM];
Felipe Balbic71fc372011-11-22 11:37:34 +0200759
Felipe Balbieeb720f2011-11-28 12:46:59 +0200760 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200761 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200762 req->trb = trb;
763 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi70fdb272016-04-05 12:47:15 +0300764 req->first_trb_index = dep->trb_enqueue % DWC3_TRB_NUM;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200765 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200766
Felipe Balbief966b92016-04-05 13:09:51 +0300767 dwc3_ep_inc_enq(dep);
Zhuang Jin Can5cd8c482014-05-16 05:57:57 +0800768 /* Skip the LINK-TRB on ISOC */
Felipe Balbief966b92016-04-05 13:09:51 +0300769 if (dwc3_ep_is_last_trb(dep->trb_enqueue) &&
Zhuang Jin Can5cd8c482014-05-16 05:57:57 +0800770 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbief966b92016-04-05 13:09:51 +0300771 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530772
Felipe Balbif6bafc62012-02-06 11:04:53 +0200773 trb->size = DWC3_TRB_SIZE_LENGTH(length);
774 trb->bpl = lower_32_bits(dma);
775 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200776
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200777 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200778 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200779 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780 break;
781
782 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530783 if (!node)
784 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
785 else
786 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200787
788 /* always enable Interrupt on Missed ISOC */
789 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200790 break;
791
792 case USB_ENDPOINT_XFER_BULK:
793 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200794 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200795 break;
796 default:
797 /*
798 * This is only possible with faulty memory because we
799 * checked it already :)
800 */
801 BUG();
802 }
803
Felipe Balbica4d44e2016-03-10 13:53:27 +0200804 /* always enable Continue on Short Packet */
805 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600806
Felipe Balbica4d44e2016-03-10 13:53:27 +0200807 if (!req->request.no_interrupt)
808 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
809
810 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530811 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200812
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530813 if (chain)
814 trb->ctrl |= DWC3_TRB_CTRL_CHN;
815
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200816 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200817 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
818
819 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500820
821 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200822}
823
Felipe Balbi72246da2011-08-19 18:10:58 +0300824/*
825 * dwc3_prepare_trbs - setup TRBs from requests
826 * @dep: endpoint for which requests are being prepared
827 * @starting: true if the endpoint is idle and no requests are queued.
828 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800829 * The function goes through the requests list and sets up TRBs for the
830 * transfers. The function returns once there are no more TRBs available or
831 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300832 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200833static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300834{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200835 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200837 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300839
840 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
841
842 /* the first request must not be queued */
Felipe Balbi70fdb272016-04-05 12:47:15 +0300843 trbs_left = (dep->trb_dequeue - dep->trb_enqueue) % DWC3_TRB_NUM;
Felipe Balbic71fc372011-11-22 11:37:34 +0200844
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200845 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200846 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi70fdb272016-04-05 12:47:15 +0300847 max = DWC3_TRB_NUM - (dep->trb_enqueue % DWC3_TRB_NUM);
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200848 if (trbs_left > max)
849 trbs_left = max;
850 }
851
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800853 * If busy & slot are equal than it is either full or empty. If we are
854 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 * full and don't do anything
856 */
857 if (!trbs_left) {
858 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200859 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 trbs_left = DWC3_TRB_NUM;
861 /*
862 * In case we start from scratch, we queue the ISOC requests
863 * starting from slot 1. This is done because we use ring
864 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800865 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300866 * after the first request so we start at slot 1 and have
867 * 7 requests proceed before we hit the first IOC.
868 * Other transfer types don't use the ring buffer and are
869 * processed from the first TRB until the last one. Since we
870 * don't wrap around we have to start at the beginning.
871 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200872 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi53fd8812016-04-04 15:33:41 +0300873 dep->trb_dequeue = 1;
874 dep->trb_enqueue = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +0300875 } else {
Felipe Balbi53fd8812016-04-04 15:33:41 +0300876 dep->trb_dequeue = 0;
877 dep->trb_enqueue = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 }
879 }
880
881 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200882 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200883 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300884
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200885 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200886 unsigned length;
887 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530888 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
Felipe Balbieeb720f2011-11-28 12:46:59 +0200890 if (req->request.num_mapped_sgs > 0) {
891 struct usb_request *request = &req->request;
892 struct scatterlist *sg = request->sg;
893 struct scatterlist *s;
894 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300895
Felipe Balbieeb720f2011-11-28 12:46:59 +0200896 for_each_sg(sg, s, request->num_mapped_sgs, i) {
897 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbieeb720f2011-11-28 12:46:59 +0200899 length = sg_dma_len(s);
900 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
Paul Zimmerman1d046792012-02-15 18:56:56 -0800902 if (i == (request->num_mapped_sgs - 1) ||
903 sg_is_last(s)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200904 if (list_empty(&dep->pending_list))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530905 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200906 chain = false;
907 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300908
Felipe Balbieeb720f2011-11-28 12:46:59 +0200909 trbs_left--;
910 if (!trbs_left)
911 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300912
Felipe Balbieeb720f2011-11-28 12:46:59 +0200913 if (last_one)
914 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300915
Felipe Balbieeb720f2011-11-28 12:46:59 +0200916 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530917 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Felipe Balbieeb720f2011-11-28 12:46:59 +0200919 if (last_one)
920 break;
921 }
Amit Virdi39e60632015-01-13 14:27:21 +0530922
923 if (last_one)
924 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300925 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200926 dma = req->request.dma;
927 length = req->request.length;
928 trbs_left--;
929
930 if (!trbs_left)
931 last_one = 1;
932
933 /* Is this the last request? */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200934 if (list_is_last(&req->list, &dep->pending_list))
Felipe Balbieeb720f2011-11-28 12:46:59 +0200935 last_one = 1;
936
937 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530938 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200939
940 if (last_one)
941 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300942 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300943 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300944}
945
946static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
947 int start_new)
948{
949 struct dwc3_gadget_ep_cmd_params params;
950 struct dwc3_request *req;
951 struct dwc3 *dwc = dep->dwc;
952 int ret;
953 u32 cmd;
954
955 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600956 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300957 return -EBUSY;
958 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300959
960 /*
961 * If we are getting here after a short-out-packet we don't enqueue any
962 * new requests as we try to set the IOC bit only on the last request.
963 */
964 if (start_new) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200965 if (list_empty(&dep->started_list))
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 dwc3_prepare_trbs(dep, start_new);
967
968 /* req points to the first request which will be sent */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200969 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200971 dwc3_prepare_trbs(dep, start_new);
972
Felipe Balbi72246da2011-08-19 18:10:58 +0300973 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800974 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300975 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200976 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300977 }
978 if (!req) {
979 dep->flags |= DWC3_EP_PENDING_REQUEST;
980 return 0;
981 }
982
983 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300984
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530985 if (start_new) {
986 params.param0 = upper_32_bits(req->trb_dma);
987 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300988 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530989 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530991 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300992
993 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
994 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
995 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 /*
997 * FIXME we need to iterate over the list of requests
998 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800999 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001001 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1002 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001003 list_del(&req->list);
1004 return ret;
1005 }
1006
1007 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001008
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001009 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001010 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001011 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001012 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001013 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001014
Felipe Balbi72246da2011-08-19 18:10:58 +03001015 return 0;
1016}
1017
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301018static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1019 struct dwc3_ep *dep, u32 cur_uf)
1020{
1021 u32 uf;
1022
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001023 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001024 dwc3_trace(trace_dwc3_gadget,
1025 "ISOC ep %s run out for requests",
1026 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301027 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301028 return;
1029 }
1030
1031 /* 4 micro frames in the future */
1032 uf = cur_uf + dep->interval * 4;
1033
1034 __dwc3_gadget_kick_transfer(dep, uf, 1);
1035}
1036
1037static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1038 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1039{
1040 u32 cur_uf, mask;
1041
1042 mask = ~(dep->interval - 1);
1043 cur_uf = event->parameters & mask;
1044
1045 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1046}
1047
Felipe Balbi72246da2011-08-19 18:10:58 +03001048static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1049{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001050 struct dwc3 *dwc = dep->dwc;
1051 int ret;
1052
Felipe Balbibb423982015-11-16 15:31:21 -06001053 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001054 dwc3_trace(trace_dwc3_gadget,
1055 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001056 &req->request, dep->endpoint.name);
1057 return -ESHUTDOWN;
1058 }
1059
1060 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1061 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001062 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1063 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001064 return -EINVAL;
1065 }
1066
Felipe Balbi72246da2011-08-19 18:10:58 +03001067 req->request.actual = 0;
1068 req->request.status = -EINPROGRESS;
1069 req->direction = dep->direction;
1070 req->epnum = dep->number;
1071
Felipe Balbife84f522015-09-01 09:01:38 -05001072 trace_dwc3_ep_queue(req);
1073
Felipe Balbi72246da2011-08-19 18:10:58 +03001074 /*
1075 * We only add to our list of requests now and
1076 * start consuming the list once we get XferNotReady
1077 * IRQ.
1078 *
1079 * That way, we avoid doing anything that we don't need
1080 * to do now and defer it until the point we receive a
1081 * particular token from the Host side.
1082 *
1083 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001084 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001085 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001086 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1087 dep->direction);
1088 if (ret)
1089 return ret;
1090
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001091 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001092
1093 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001094 * If there are no pending requests and the endpoint isn't already
1095 * busy, we will just start the request straight away.
1096 *
1097 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1098 * little bit faster.
1099 */
1100 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001101 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001102 !(dep->flags & DWC3_EP_BUSY)) {
1103 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbia8f32812015-09-16 10:40:07 -05001104 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001105 }
1106
1107 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001108 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001109 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001110 * 1. XferNotReady with empty list of requests. We need to kick the
1111 * transfer here in that situation, otherwise we will be NAKing
1112 * forever. If we get XferNotReady before gadget driver has a
1113 * chance to queue a request, we will ACK the IRQ but won't be
1114 * able to receive the data until the next request is queued.
1115 * The following code is handling exactly that.
1116 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 */
1118 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301119 /*
1120 * If xfernotready is already elapsed and it is a case
1121 * of isoc transfer, then issue END TRANSFER, so that
1122 * you can receive xfernotready again and can have
1123 * notion of current microframe.
1124 */
1125 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001126 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001127 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301128 dep->flags = DWC3_EP_ENABLED;
1129 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301130 return 0;
1131 }
1132
Felipe Balbib511e5e2012-06-06 12:00:50 +03001133 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbi89185912015-09-15 09:49:14 -05001134 if (!ret)
1135 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1136
Felipe Balbia8f32812015-09-16 10:40:07 -05001137 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001138 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001139
Felipe Balbib511e5e2012-06-06 12:00:50 +03001140 /*
1141 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1142 * kick the transfer here after queuing a request, otherwise the
1143 * core may not see the modified TRB(s).
1144 */
1145 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301146 (dep->flags & DWC3_EP_BUSY) &&
1147 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001148 WARN_ON_ONCE(!dep->resource_index);
1149 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001150 false);
Felipe Balbia8f32812015-09-16 10:40:07 -05001151 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001152 }
1153
Felipe Balbib997ada2012-07-26 13:26:50 +03001154 /*
1155 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1156 * right away, otherwise host will not know we have streams to be
1157 * handled.
1158 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001159 if (dep->stream_capable)
Felipe Balbib997ada2012-07-26 13:26:50 +03001160 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbib997ada2012-07-26 13:26:50 +03001161
Felipe Balbia8f32812015-09-16 10:40:07 -05001162out:
1163 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001164 dwc3_trace(trace_dwc3_gadget,
1165 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001166 dep->name);
1167 if (ret == -EBUSY)
1168 ret = 0;
1169
1170 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001171}
1172
Felipe Balbi04c03d12015-12-02 10:06:45 -06001173static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1174 struct usb_request *request)
1175{
1176 dwc3_gadget_ep_free_request(ep, request);
1177}
1178
1179static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1180{
1181 struct dwc3_request *req;
1182 struct usb_request *request;
1183 struct usb_ep *ep = &dep->endpoint;
1184
1185 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1186 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1187 if (!request)
1188 return -ENOMEM;
1189
1190 request->length = 0;
1191 request->buf = dwc->zlp_buf;
1192 request->complete = __dwc3_gadget_ep_zlp_complete;
1193
1194 req = to_dwc3_request(request);
1195
1196 return __dwc3_gadget_ep_queue(dep, req);
1197}
1198
Felipe Balbi72246da2011-08-19 18:10:58 +03001199static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1200 gfp_t gfp_flags)
1201{
1202 struct dwc3_request *req = to_dwc3_request(request);
1203 struct dwc3_ep *dep = to_dwc3_ep(ep);
1204 struct dwc3 *dwc = dep->dwc;
1205
1206 unsigned long flags;
1207
1208 int ret;
1209
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001210 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001211 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001212
1213 /*
1214 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1215 * setting request->zero, instead of doing magic, we will just queue an
1216 * extra usb_request ourselves so that it gets handled the same way as
1217 * any other request.
1218 */
John Yound92618982015-12-22 12:23:20 -08001219 if (ret == 0 && request->zero && request->length &&
1220 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001221 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1222
Felipe Balbi72246da2011-08-19 18:10:58 +03001223 spin_unlock_irqrestore(&dwc->lock, flags);
1224
1225 return ret;
1226}
1227
1228static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1229 struct usb_request *request)
1230{
1231 struct dwc3_request *req = to_dwc3_request(request);
1232 struct dwc3_request *r = NULL;
1233
1234 struct dwc3_ep *dep = to_dwc3_ep(ep);
1235 struct dwc3 *dwc = dep->dwc;
1236
1237 unsigned long flags;
1238 int ret = 0;
1239
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001240 trace_dwc3_ep_dequeue(req);
1241
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 spin_lock_irqsave(&dwc->lock, flags);
1243
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001244 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001245 if (r == req)
1246 break;
1247 }
1248
1249 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001250 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001251 if (r == req)
1252 break;
1253 }
1254 if (r == req) {
1255 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001256 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301257 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001258 }
1259 dev_err(dwc->dev, "request %p was not queued to %s\n",
1260 request, ep->name);
1261 ret = -EINVAL;
1262 goto out0;
1263 }
1264
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301265out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 /* giveback the request */
1267 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1268
1269out0:
1270 spin_unlock_irqrestore(&dwc->lock, flags);
1271
1272 return ret;
1273}
1274
Felipe Balbi7a608552014-09-24 14:19:52 -05001275int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001276{
1277 struct dwc3_gadget_ep_cmd_params params;
1278 struct dwc3 *dwc = dep->dwc;
1279 int ret;
1280
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001281 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1282 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1283 return -EINVAL;
1284 }
1285
Felipe Balbi72246da2011-08-19 18:10:58 +03001286 memset(&params, 0x00, sizeof(params));
1287
1288 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001289 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001290 (!list_empty(&dep->started_list) ||
1291 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001292 dwc3_trace(trace_dwc3_gadget,
1293 "%s: pending request, cannot halt\n",
Felipe Balbi7a608552014-09-24 14:19:52 -05001294 dep->name);
1295 return -EAGAIN;
1296 }
1297
Felipe Balbi72246da2011-08-19 18:10:58 +03001298 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1299 DWC3_DEPCMD_SETSTALL, &params);
1300 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001301 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 dep->name);
1303 else
1304 dep->flags |= DWC3_EP_STALL;
1305 } else {
1306 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1307 DWC3_DEPCMD_CLEARSTALL, &params);
1308 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001309 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001310 dep->name);
1311 else
Alan Sterna535d812013-11-01 12:05:12 -04001312 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001313 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001314
Felipe Balbi72246da2011-08-19 18:10:58 +03001315 return ret;
1316}
1317
1318static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1319{
1320 struct dwc3_ep *dep = to_dwc3_ep(ep);
1321 struct dwc3 *dwc = dep->dwc;
1322
1323 unsigned long flags;
1324
1325 int ret;
1326
1327 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001328 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 spin_unlock_irqrestore(&dwc->lock, flags);
1330
1331 return ret;
1332}
1333
1334static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1335{
1336 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001337 struct dwc3 *dwc = dep->dwc;
1338 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001339 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001340
Paul Zimmerman249a4562012-02-24 17:32:16 -08001341 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001342 dep->flags |= DWC3_EP_WEDGE;
1343
Pratyush Anand08f0d962012-06-25 22:40:43 +05301344 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001345 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301346 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001347 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001348 spin_unlock_irqrestore(&dwc->lock, flags);
1349
1350 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001351}
1352
1353/* -------------------------------------------------------------------------- */
1354
1355static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1356 .bLength = USB_DT_ENDPOINT_SIZE,
1357 .bDescriptorType = USB_DT_ENDPOINT,
1358 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1359};
1360
1361static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1362 .enable = dwc3_gadget_ep0_enable,
1363 .disable = dwc3_gadget_ep0_disable,
1364 .alloc_request = dwc3_gadget_ep_alloc_request,
1365 .free_request = dwc3_gadget_ep_free_request,
1366 .queue = dwc3_gadget_ep0_queue,
1367 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301368 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001369 .set_wedge = dwc3_gadget_ep_set_wedge,
1370};
1371
1372static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1373 .enable = dwc3_gadget_ep_enable,
1374 .disable = dwc3_gadget_ep_disable,
1375 .alloc_request = dwc3_gadget_ep_alloc_request,
1376 .free_request = dwc3_gadget_ep_free_request,
1377 .queue = dwc3_gadget_ep_queue,
1378 .dequeue = dwc3_gadget_ep_dequeue,
1379 .set_halt = dwc3_gadget_ep_set_halt,
1380 .set_wedge = dwc3_gadget_ep_set_wedge,
1381};
1382
1383/* -------------------------------------------------------------------------- */
1384
1385static int dwc3_gadget_get_frame(struct usb_gadget *g)
1386{
1387 struct dwc3 *dwc = gadget_to_dwc(g);
1388 u32 reg;
1389
1390 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1391 return DWC3_DSTS_SOFFN(reg);
1392}
1393
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001394static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001395{
Felipe Balbi72246da2011-08-19 18:10:58 +03001396 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001397
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001398 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001399 u32 reg;
1400
Felipe Balbi72246da2011-08-19 18:10:58 +03001401 u8 link_state;
1402 u8 speed;
1403
Felipe Balbi72246da2011-08-19 18:10:58 +03001404 /*
1405 * According to the Databook Remote wakeup request should
1406 * be issued only when the device is in early suspend state.
1407 *
1408 * We can check that via USB Link State bits in DSTS register.
1409 */
1410 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1411
1412 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001413 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1414 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001415 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001416 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 }
1418
1419 link_state = DWC3_DSTS_USBLNKST(reg);
1420
1421 switch (link_state) {
1422 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1423 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1424 break;
1425 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001426 dwc3_trace(trace_dwc3_gadget,
1427 "can't wakeup from '%s'\n",
1428 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001429 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 }
1431
Felipe Balbi8598bde2012-01-02 18:55:57 +02001432 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1433 if (ret < 0) {
1434 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001435 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001436 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001437
Paul Zimmerman802fde92012-04-27 13:10:52 +03001438 /* Recent versions do this automatically */
1439 if (dwc->revision < DWC3_REVISION_194A) {
1440 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001441 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001442 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1443 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1444 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001445
Paul Zimmerman1d046792012-02-15 18:56:56 -08001446 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001447 timeout = jiffies + msecs_to_jiffies(100);
1448
Paul Zimmerman1d046792012-02-15 18:56:56 -08001449 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1451
1452 /* in HS, means ON */
1453 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1454 break;
1455 }
1456
1457 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1458 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001459 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 }
1461
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001462 return 0;
1463}
1464
1465static int dwc3_gadget_wakeup(struct usb_gadget *g)
1466{
1467 struct dwc3 *dwc = gadget_to_dwc(g);
1468 unsigned long flags;
1469 int ret;
1470
1471 spin_lock_irqsave(&dwc->lock, flags);
1472 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001473 spin_unlock_irqrestore(&dwc->lock, flags);
1474
1475 return ret;
1476}
1477
1478static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1479 int is_selfpowered)
1480{
1481 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001482 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001483
Paul Zimmerman249a4562012-02-24 17:32:16 -08001484 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001485 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001486 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001487
1488 return 0;
1489}
1490
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001491static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001492{
1493 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001494 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001495
1496 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001497 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001498 if (dwc->revision <= DWC3_REVISION_187A) {
1499 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1500 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1501 }
1502
1503 if (dwc->revision >= DWC3_REVISION_194A)
1504 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1505 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001506
1507 if (dwc->has_hibernation)
1508 reg |= DWC3_DCTL_KEEP_CONNECT;
1509
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001510 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001511 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001512 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001513
1514 if (dwc->has_hibernation && !suspend)
1515 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1516
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001517 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001518 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001519
1520 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1521
1522 do {
1523 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1524 if (is_on) {
1525 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1526 break;
1527 } else {
1528 if (reg & DWC3_DSTS_DEVCTRLHLT)
1529 break;
1530 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001531 timeout--;
1532 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301533 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001534 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 } while (1);
1536
Felipe Balbi73815282015-01-27 13:48:14 -06001537 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001538 dwc->gadget_driver
1539 ? dwc->gadget_driver->function : "no-function",
1540 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301541
1542 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001543}
1544
1545static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1546{
1547 struct dwc3 *dwc = gadget_to_dwc(g);
1548 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301549 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001550
1551 is_on = !!is_on;
1552
1553 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001554 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 spin_unlock_irqrestore(&dwc->lock, flags);
1556
Pratyush Anand6f17f742012-07-02 10:21:55 +05301557 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558}
1559
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001560static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1561{
1562 u32 reg;
1563
1564 /* Enable all but Start and End of Frame IRQs */
1565 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1566 DWC3_DEVTEN_EVNTOVERFLOWEN |
1567 DWC3_DEVTEN_CMDCMPLTEN |
1568 DWC3_DEVTEN_ERRTICERREN |
1569 DWC3_DEVTEN_WKUPEVTEN |
1570 DWC3_DEVTEN_ULSTCNGEN |
1571 DWC3_DEVTEN_CONNECTDONEEN |
1572 DWC3_DEVTEN_USBRSTEN |
1573 DWC3_DEVTEN_DISCONNEVTEN);
1574
1575 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1576}
1577
1578static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1579{
1580 /* mask all interrupts */
1581 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1582}
1583
1584static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001585static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001586
Felipe Balbi72246da2011-08-19 18:10:58 +03001587static int dwc3_gadget_start(struct usb_gadget *g,
1588 struct usb_gadget_driver *driver)
1589{
1590 struct dwc3 *dwc = gadget_to_dwc(g);
1591 struct dwc3_ep *dep;
1592 unsigned long flags;
1593 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001594 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 u32 reg;
1596
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001597 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1598 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbidea520a2016-03-30 09:39:34 +03001599 IRQF_SHARED, "dwc3", dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001600 if (ret) {
1601 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1602 irq, ret);
1603 goto err0;
1604 }
1605
Felipe Balbi72246da2011-08-19 18:10:58 +03001606 spin_lock_irqsave(&dwc->lock, flags);
1607
1608 if (dwc->gadget_driver) {
1609 dev_err(dwc->dev, "%s is already bound to %s\n",
1610 dwc->gadget.name,
1611 dwc->gadget_driver->driver.name);
1612 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001613 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001614 }
1615
1616 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001617
Felipe Balbi72246da2011-08-19 18:10:58 +03001618 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1619 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001620
1621 /**
1622 * WORKAROUND: DWC3 revision < 2.20a have an issue
1623 * which would cause metastability state on Run/Stop
1624 * bit if we try to force the IP to USB2-only mode.
1625 *
1626 * Because of that, we cannot configure the IP to any
1627 * speed other than the SuperSpeed
1628 *
1629 * Refers to:
1630 *
1631 * STAR#9000525659: Clock Domain Crossing on DCTL in
1632 * USB 2.0 Mode
1633 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001634 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001635 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001636 } else {
1637 switch (dwc->maximum_speed) {
1638 case USB_SPEED_LOW:
1639 reg |= DWC3_DSTS_LOWSPEED;
1640 break;
1641 case USB_SPEED_FULL:
1642 reg |= DWC3_DSTS_FULLSPEED1;
1643 break;
1644 case USB_SPEED_HIGH:
1645 reg |= DWC3_DSTS_HIGHSPEED;
1646 break;
John Youn75808622016-02-05 17:09:13 -08001647 case USB_SPEED_SUPER_PLUS:
1648 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1649 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001650 default:
John Youn77966eb2016-02-19 17:31:01 -08001651 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1652 dwc->maximum_speed);
1653 /* fall through */
1654 case USB_SPEED_SUPER:
1655 reg |= DWC3_DCFG_SUPERSPEED;
1656 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001657 }
1658 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001659 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1660
1661 /* Start with SuperSpeed Default */
1662 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1663
1664 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001665 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1666 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001667 if (ret) {
1668 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001669 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001670 }
1671
1672 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001673 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1674 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 if (ret) {
1676 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001677 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001678 }
1679
1680 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001681 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001682 dwc3_ep0_out_start(dwc);
1683
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001684 dwc3_gadget_enable_irq(dwc);
1685
Felipe Balbi72246da2011-08-19 18:10:58 +03001686 spin_unlock_irqrestore(&dwc->lock, flags);
1687
1688 return 0;
1689
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001690err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001691 __dwc3_gadget_ep_disable(dwc->eps[0]);
1692
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001693err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001694 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001695
1696err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001697 spin_unlock_irqrestore(&dwc->lock, flags);
1698
Felipe Balbidea520a2016-03-30 09:39:34 +03001699 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001700
1701err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 return ret;
1703}
1704
Felipe Balbi22835b82014-10-17 12:05:12 -05001705static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001706{
1707 struct dwc3 *dwc = gadget_to_dwc(g);
1708 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001709 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001710
1711 spin_lock_irqsave(&dwc->lock, flags);
1712
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001713 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 __dwc3_gadget_ep_disable(dwc->eps[0]);
1715 __dwc3_gadget_ep_disable(dwc->eps[1]);
1716
1717 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001718
1719 spin_unlock_irqrestore(&dwc->lock, flags);
1720
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001721 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001722 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001723
Felipe Balbi72246da2011-08-19 18:10:58 +03001724 return 0;
1725}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001726
Felipe Balbi72246da2011-08-19 18:10:58 +03001727static const struct usb_gadget_ops dwc3_gadget_ops = {
1728 .get_frame = dwc3_gadget_get_frame,
1729 .wakeup = dwc3_gadget_wakeup,
1730 .set_selfpowered = dwc3_gadget_set_selfpowered,
1731 .pullup = dwc3_gadget_pullup,
1732 .udc_start = dwc3_gadget_start,
1733 .udc_stop = dwc3_gadget_stop,
1734};
1735
1736/* -------------------------------------------------------------------------- */
1737
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001738static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1739 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001740{
1741 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001742 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001744 for (i = 0; i < num; i++) {
1745 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001746
Felipe Balbi72246da2011-08-19 18:10:58 +03001747 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001748 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001750
1751 dep->dwc = dwc;
1752 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001753 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001754 dwc->eps[epnum] = dep;
1755
1756 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1757 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001758
Felipe Balbi72246da2011-08-19 18:10:58 +03001759 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001760
Felipe Balbi73815282015-01-27 13:48:14 -06001761 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001762
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001764 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301765 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001766 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1767 if (!epnum)
1768 dwc->gadget.ep0 = &dep->endpoint;
1769 } else {
1770 int ret;
1771
Robert Baldygae117e742013-12-13 12:23:38 +01001772 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001773 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001774 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1775 list_add_tail(&dep->endpoint.ep_list,
1776 &dwc->gadget.ep_list);
1777
1778 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001779 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001781 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001782
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001783 if (epnum == 0 || epnum == 1) {
1784 dep->endpoint.caps.type_control = true;
1785 } else {
1786 dep->endpoint.caps.type_iso = true;
1787 dep->endpoint.caps.type_bulk = true;
1788 dep->endpoint.caps.type_int = true;
1789 }
1790
1791 dep->endpoint.caps.dir_in = !!direction;
1792 dep->endpoint.caps.dir_out = !direction;
1793
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001794 INIT_LIST_HEAD(&dep->pending_list);
1795 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001796 }
1797
1798 return 0;
1799}
1800
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001801static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1802{
1803 int ret;
1804
1805 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1806
1807 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1808 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001809 dwc3_trace(trace_dwc3_gadget,
1810 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001811 return ret;
1812 }
1813
1814 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1815 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001816 dwc3_trace(trace_dwc3_gadget,
1817 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001818 return ret;
1819 }
1820
1821 return 0;
1822}
1823
Felipe Balbi72246da2011-08-19 18:10:58 +03001824static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1825{
1826 struct dwc3_ep *dep;
1827 u8 epnum;
1828
1829 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1830 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001831 if (!dep)
1832 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301833 /*
1834 * Physical endpoints 0 and 1 are special; they form the
1835 * bi-directional USB endpoint 0.
1836 *
1837 * For those two physical endpoints, we don't allocate a TRB
1838 * pool nor do we add them the endpoints list. Due to that, we
1839 * shouldn't do these two operations otherwise we would end up
1840 * with all sorts of bugs when removing dwc3.ko.
1841 */
1842 if (epnum != 0 && epnum != 1) {
1843 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001844 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301845 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001846
1847 kfree(dep);
1848 }
1849}
1850
Felipe Balbi72246da2011-08-19 18:10:58 +03001851/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001852
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301853static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1854 struct dwc3_request *req, struct dwc3_trb *trb,
1855 const struct dwc3_event_depevt *event, int status)
1856{
1857 unsigned int count;
1858 unsigned int s_pkt = 0;
1859 unsigned int trb_status;
1860
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001861 trace_dwc3_complete_trb(dep, trb);
1862
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301863 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1864 /*
1865 * We continue despite the error. There is not much we
1866 * can do. If we don't clean it up we loop forever. If
1867 * we skip the TRB then it gets overwritten after a
1868 * while since we use them in a ring buffer. A BUG()
1869 * would help. Lets hope that if this occurs, someone
1870 * fixes the root cause instead of looking away :)
1871 */
1872 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1873 dep->name, trb);
1874 count = trb->size & DWC3_TRB_SIZE_MASK;
1875
1876 if (dep->direction) {
1877 if (count) {
1878 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1879 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001880 dwc3_trace(trace_dwc3_gadget,
1881 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301882 dep->name);
1883 /*
1884 * If missed isoc occurred and there is
1885 * no request queued then issue END
1886 * TRANSFER, so that core generates
1887 * next xfernotready and we will issue
1888 * a fresh START TRANSFER.
1889 * If there are still queued request
1890 * then wait, do not issue either END
1891 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001892 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301893 * giveback.If any future queued request
1894 * is successfully transferred then we
1895 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001896 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301897 */
1898 dep->flags |= DWC3_EP_MISSED_ISOC;
1899 } else {
1900 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1901 dep->name);
1902 status = -ECONNRESET;
1903 }
1904 } else {
1905 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1906 }
1907 } else {
1908 if (count && (event->status & DEPEVT_STATUS_SHORT))
1909 s_pkt = 1;
1910 }
1911
1912 /*
1913 * We assume here we will always receive the entire data block
1914 * which we should receive. Meaning, if we program RX to
1915 * receive 4K but we receive only 2K, we assume that's all we
1916 * should receive and we simply bounce the request back to the
1917 * gadget driver for further processing.
1918 */
1919 req->request.actual += req->request.length - count;
1920 if (s_pkt)
1921 return 1;
1922 if ((event->status & DEPEVT_STATUS_LST) &&
1923 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1924 DWC3_TRB_CTRL_HWO)))
1925 return 1;
1926 if ((event->status & DEPEVT_STATUS_IOC) &&
1927 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1928 return 1;
1929 return 0;
1930}
1931
Felipe Balbi72246da2011-08-19 18:10:58 +03001932static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1933 const struct dwc3_event_depevt *event, int status)
1934{
1935 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001936 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301937 unsigned int slot;
1938 unsigned int i;
1939 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001940
1941 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001942 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001943 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001944 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001945
Ville Syrjäläd115d702015-08-31 19:48:28 +03001946 i = 0;
1947 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03001948 slot = req->first_trb_index + i;
Ville Syrjäläd115d702015-08-31 19:48:28 +03001949 if ((slot == DWC3_TRB_NUM - 1) &&
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301950 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001951 slot++;
1952 slot %= DWC3_TRB_NUM;
1953 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001954
Ville Syrjäläd115d702015-08-31 19:48:28 +03001955 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1956 event, status);
1957 if (ret)
1958 break;
1959 } while (++i < req->request.num_mapped_sgs);
1960
1961 dwc3_gadget_giveback(dep, req, status);
1962
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301963 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001964 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03001965 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001966
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301967 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001968 list_empty(&dep->started_list)) {
1969 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301970 /*
1971 * If there is no entry in request list then do
1972 * not issue END TRANSFER now. Just set PENDING
1973 * flag, so that END TRANSFER is issued when an
1974 * entry is added into request list.
1975 */
1976 dep->flags = DWC3_EP_PENDING_REQUEST;
1977 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001978 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301979 dep->flags = DWC3_EP_ENABLED;
1980 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301981 return 1;
1982 }
1983
Felipe Balbi72246da2011-08-19 18:10:58 +03001984 return 1;
1985}
1986
1987static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09001988 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001989{
1990 unsigned status = 0;
1991 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05001992 u32 is_xfer_complete;
1993
1994 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001995
1996 if (event->status & DEPEVT_STATUS_BUSERR)
1997 status = -ECONNRESET;
1998
Paul Zimmerman1d046792012-02-15 18:56:56 -08001999 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002000 if (clean_busy && (is_xfer_complete ||
2001 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002002 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002003
2004 /*
2005 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2006 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2007 */
2008 if (dwc->revision < DWC3_REVISION_183A) {
2009 u32 reg;
2010 int i;
2011
2012 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002013 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002014
2015 if (!(dep->flags & DWC3_EP_ENABLED))
2016 continue;
2017
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002018 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002019 return;
2020 }
2021
2022 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2023 reg |= dwc->u1u2;
2024 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2025
2026 dwc->u1u2 = 0;
2027 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002028
Felipe Balbie6e709b2015-09-28 15:16:56 -05002029 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002030 int ret;
2031
Felipe Balbie6e709b2015-09-28 15:16:56 -05002032 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002033 if (!ret || ret == -EBUSY)
2034 return;
2035 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002036}
2037
Felipe Balbi72246da2011-08-19 18:10:58 +03002038static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2039 const struct dwc3_event_depevt *event)
2040{
2041 struct dwc3_ep *dep;
2042 u8 epnum = event->endpoint_number;
2043
2044 dep = dwc->eps[epnum];
2045
Felipe Balbi3336abb2012-06-06 09:19:35 +03002046 if (!(dep->flags & DWC3_EP_ENABLED))
2047 return;
2048
Felipe Balbi72246da2011-08-19 18:10:58 +03002049 if (epnum == 0 || epnum == 1) {
2050 dwc3_ep0_interrupt(dwc, event);
2051 return;
2052 }
2053
2054 switch (event->endpoint_event) {
2055 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002056 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002057
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002058 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002059 dwc3_trace(trace_dwc3_gadget,
2060 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002061 dep->name);
2062 return;
2063 }
2064
Jingoo Han029d97f2014-07-04 15:00:51 +09002065 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 break;
2067 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002068 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002069 break;
2070 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002071 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002072 dwc3_gadget_start_isoc(dwc, dep, event);
2073 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002074 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002075 int ret;
2076
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002077 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2078
Felipe Balbi73815282015-01-27 13:48:14 -06002079 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002080 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002081 : "Transfer Not Active");
2082
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002083 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
Felipe Balbi72246da2011-08-19 18:10:58 +03002084 if (!ret || ret == -EBUSY)
2085 return;
2086
Felipe Balbiec5e7952015-11-16 16:04:13 -06002087 dwc3_trace(trace_dwc3_gadget,
2088 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002089 dep->name);
2090 }
2091
2092 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002093 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002094 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002095 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2096 dep->name);
2097 return;
2098 }
2099
2100 switch (event->status) {
2101 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002102 dwc3_trace(trace_dwc3_gadget,
2103 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002104 event->parameters);
2105
2106 break;
2107 case DEPEVT_STREAMEVT_NOTFOUND:
2108 /* FALLTHROUGH */
2109 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002110 dwc3_trace(trace_dwc3_gadget,
2111 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002112 }
2113 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002115 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002116 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002117 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002118 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 break;
2120 }
2121}
2122
2123static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2124{
2125 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2126 spin_unlock(&dwc->lock);
2127 dwc->gadget_driver->disconnect(&dwc->gadget);
2128 spin_lock(&dwc->lock);
2129 }
2130}
2131
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002132static void dwc3_suspend_gadget(struct dwc3 *dwc)
2133{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002134 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002135 spin_unlock(&dwc->lock);
2136 dwc->gadget_driver->suspend(&dwc->gadget);
2137 spin_lock(&dwc->lock);
2138 }
2139}
2140
2141static void dwc3_resume_gadget(struct dwc3 *dwc)
2142{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002143 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002144 spin_unlock(&dwc->lock);
2145 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002146 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002147 }
2148}
2149
2150static void dwc3_reset_gadget(struct dwc3 *dwc)
2151{
2152 if (!dwc->gadget_driver)
2153 return;
2154
2155 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2156 spin_unlock(&dwc->lock);
2157 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002158 spin_lock(&dwc->lock);
2159 }
2160}
2161
Paul Zimmermanb992e682012-04-27 14:17:35 +03002162static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002163{
2164 struct dwc3_ep *dep;
2165 struct dwc3_gadget_ep_cmd_params params;
2166 u32 cmd;
2167 int ret;
2168
2169 dep = dwc->eps[epnum];
2170
Felipe Balbib4996a82012-06-06 12:04:13 +03002171 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302172 return;
2173
Pratyush Anand57911502012-07-06 15:19:10 +05302174 /*
2175 * NOTICE: We are violating what the Databook says about the
2176 * EndTransfer command. Ideally we would _always_ wait for the
2177 * EndTransfer Command Completion IRQ, but that's causing too
2178 * much trouble synchronizing between us and gadget driver.
2179 *
2180 * We have discussed this with the IP Provider and it was
2181 * suggested to giveback all requests here, but give HW some
2182 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002183 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302184 *
2185 * Note also that a similar handling was tested by Synopsys
2186 * (thanks a lot Paul) and nothing bad has come out of it.
2187 * In short, what we're doing is:
2188 *
2189 * - Issue EndTransfer WITH CMDIOC bit set
2190 * - Wait 100us
2191 */
2192
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302193 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002194 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2195 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002196 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302197 memset(&params, 0, sizeof(params));
2198 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2199 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002200 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002201 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302202 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002203}
2204
2205static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2206{
2207 u32 epnum;
2208
2209 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2210 struct dwc3_ep *dep;
2211
2212 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002213 if (!dep)
2214 continue;
2215
Felipe Balbi72246da2011-08-19 18:10:58 +03002216 if (!(dep->flags & DWC3_EP_ENABLED))
2217 continue;
2218
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002219 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002220 }
2221}
2222
2223static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2224{
2225 u32 epnum;
2226
2227 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2228 struct dwc3_ep *dep;
2229 struct dwc3_gadget_ep_cmd_params params;
2230 int ret;
2231
2232 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002233 if (!dep)
2234 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002235
2236 if (!(dep->flags & DWC3_EP_STALL))
2237 continue;
2238
2239 dep->flags &= ~DWC3_EP_STALL;
2240
2241 memset(&params, 0, sizeof(params));
2242 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2243 DWC3_DEPCMD_CLEARSTALL, &params);
2244 WARN_ON_ONCE(ret);
2245 }
2246}
2247
2248static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2249{
Felipe Balbic4430a22012-05-24 10:30:01 +03002250 int reg;
2251
Felipe Balbi72246da2011-08-19 18:10:58 +03002252 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2253 reg &= ~DWC3_DCTL_INITU1ENA;
2254 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2255
2256 reg &= ~DWC3_DCTL_INITU2ENA;
2257 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002258
Felipe Balbi72246da2011-08-19 18:10:58 +03002259 dwc3_disconnect_gadget(dwc);
2260
2261 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002262 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002263 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002264}
2265
Felipe Balbi72246da2011-08-19 18:10:58 +03002266static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2267{
2268 u32 reg;
2269
Felipe Balbidf62df52011-10-14 15:11:49 +03002270 /*
2271 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2272 * would cause a missing Disconnect Event if there's a
2273 * pending Setup Packet in the FIFO.
2274 *
2275 * There's no suggested workaround on the official Bug
2276 * report, which states that "unless the driver/application
2277 * is doing any special handling of a disconnect event,
2278 * there is no functional issue".
2279 *
2280 * Unfortunately, it turns out that we _do_ some special
2281 * handling of a disconnect event, namely complete all
2282 * pending transfers, notify gadget driver of the
2283 * disconnection, and so on.
2284 *
2285 * Our suggested workaround is to follow the Disconnect
2286 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002287 * flag. Such flag gets set whenever we have a SETUP_PENDING
2288 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002289 * same endpoint.
2290 *
2291 * Refers to:
2292 *
2293 * STAR#9000466709: RTL: Device : Disconnect event not
2294 * generated if setup packet pending in FIFO
2295 */
2296 if (dwc->revision < DWC3_REVISION_188A) {
2297 if (dwc->setup_packet_pending)
2298 dwc3_gadget_disconnect_interrupt(dwc);
2299 }
2300
Felipe Balbi8e744752014-11-06 14:27:53 +08002301 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002302
2303 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2304 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2305 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002306 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002307
2308 dwc3_stop_active_transfers(dwc);
2309 dwc3_clear_stall_all_ep(dwc);
2310
2311 /* Reset device address to zero */
2312 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2313 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2314 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002315}
2316
2317static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2318{
2319 u32 reg;
2320 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2321
2322 /*
2323 * We change the clock only at SS but I dunno why I would want to do
2324 * this. Maybe it becomes part of the power saving plan.
2325 */
2326
John Younee5cd412016-02-05 17:08:45 -08002327 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2328 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002329 return;
2330
2331 /*
2332 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2333 * each time on Connect Done.
2334 */
2335 if (!usb30_clock)
2336 return;
2337
2338 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2339 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2340 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2341}
2342
Felipe Balbi72246da2011-08-19 18:10:58 +03002343static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2344{
Felipe Balbi72246da2011-08-19 18:10:58 +03002345 struct dwc3_ep *dep;
2346 int ret;
2347 u32 reg;
2348 u8 speed;
2349
Felipe Balbi72246da2011-08-19 18:10:58 +03002350 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2351 speed = reg & DWC3_DSTS_CONNECTSPD;
2352 dwc->speed = speed;
2353
2354 dwc3_update_ram_clk_sel(dwc, speed);
2355
2356 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002357 case DWC3_DCFG_SUPERSPEED_PLUS:
2358 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2359 dwc->gadget.ep0->maxpacket = 512;
2360 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2361 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002362 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002363 /*
2364 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2365 * would cause a missing USB3 Reset event.
2366 *
2367 * In such situations, we should force a USB3 Reset
2368 * event by calling our dwc3_gadget_reset_interrupt()
2369 * routine.
2370 *
2371 * Refers to:
2372 *
2373 * STAR#9000483510: RTL: SS : USB3 reset event may
2374 * not be generated always when the link enters poll
2375 */
2376 if (dwc->revision < DWC3_REVISION_190A)
2377 dwc3_gadget_reset_interrupt(dwc);
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2380 dwc->gadget.ep0->maxpacket = 512;
2381 dwc->gadget.speed = USB_SPEED_SUPER;
2382 break;
2383 case DWC3_DCFG_HIGHSPEED:
2384 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2385 dwc->gadget.ep0->maxpacket = 64;
2386 dwc->gadget.speed = USB_SPEED_HIGH;
2387 break;
2388 case DWC3_DCFG_FULLSPEED2:
2389 case DWC3_DCFG_FULLSPEED1:
2390 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2391 dwc->gadget.ep0->maxpacket = 64;
2392 dwc->gadget.speed = USB_SPEED_FULL;
2393 break;
2394 case DWC3_DCFG_LOWSPEED:
2395 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2396 dwc->gadget.ep0->maxpacket = 8;
2397 dwc->gadget.speed = USB_SPEED_LOW;
2398 break;
2399 }
2400
Pratyush Anand2b758352013-01-14 15:59:31 +05302401 /* Enable USB2 LPM Capability */
2402
John Younee5cd412016-02-05 17:08:45 -08002403 if ((dwc->revision > DWC3_REVISION_194A) &&
2404 (speed != DWC3_DCFG_SUPERSPEED) &&
2405 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302406 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2407 reg |= DWC3_DCFG_LPM_CAP;
2408 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2409
2410 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2411 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2412
Huang Rui460d0982014-10-31 11:11:18 +08002413 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302414
Huang Rui80caf7d2014-10-28 19:54:26 +08002415 /*
2416 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2417 * DCFG.LPMCap is set, core responses with an ACK and the
2418 * BESL value in the LPM token is less than or equal to LPM
2419 * NYET threshold.
2420 */
2421 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2422 && dwc->has_lpm_erratum,
2423 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2424
2425 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2426 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2427
Pratyush Anand2b758352013-01-14 15:59:31 +05302428 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002429 } else {
2430 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2431 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2432 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302433 }
2434
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002436 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2437 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 if (ret) {
2439 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2440 return;
2441 }
2442
2443 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002444 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2445 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002446 if (ret) {
2447 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2448 return;
2449 }
2450
2451 /*
2452 * Configure PHY via GUSB3PIPECTLn if required.
2453 *
2454 * Update GTXFIFOSIZn
2455 *
2456 * In both cases reset values should be sufficient.
2457 */
2458}
2459
2460static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2461{
Felipe Balbi72246da2011-08-19 18:10:58 +03002462 /*
2463 * TODO take core out of low power mode when that's
2464 * implemented.
2465 */
2466
Jiebing Liad14d4e2014-12-11 13:26:29 +08002467 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2468 spin_unlock(&dwc->lock);
2469 dwc->gadget_driver->resume(&dwc->gadget);
2470 spin_lock(&dwc->lock);
2471 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002472}
2473
2474static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2475 unsigned int evtinfo)
2476{
Felipe Balbifae2b902011-10-14 13:00:30 +03002477 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002478 unsigned int pwropt;
2479
2480 /*
2481 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2482 * Hibernation mode enabled which would show up when device detects
2483 * host-initiated U3 exit.
2484 *
2485 * In that case, device will generate a Link State Change Interrupt
2486 * from U3 to RESUME which is only necessary if Hibernation is
2487 * configured in.
2488 *
2489 * There are no functional changes due to such spurious event and we
2490 * just need to ignore it.
2491 *
2492 * Refers to:
2493 *
2494 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2495 * operational mode
2496 */
2497 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2498 if ((dwc->revision < DWC3_REVISION_250A) &&
2499 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2500 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2501 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002502 dwc3_trace(trace_dwc3_gadget,
2503 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002504 return;
2505 }
2506 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002507
2508 /*
2509 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2510 * on the link partner, the USB session might do multiple entry/exit
2511 * of low power states before a transfer takes place.
2512 *
2513 * Due to this problem, we might experience lower throughput. The
2514 * suggested workaround is to disable DCTL[12:9] bits if we're
2515 * transitioning from U1/U2 to U0 and enable those bits again
2516 * after a transfer completes and there are no pending transfers
2517 * on any of the enabled endpoints.
2518 *
2519 * This is the first half of that workaround.
2520 *
2521 * Refers to:
2522 *
2523 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2524 * core send LGO_Ux entering U0
2525 */
2526 if (dwc->revision < DWC3_REVISION_183A) {
2527 if (next == DWC3_LINK_STATE_U0) {
2528 u32 u1u2;
2529 u32 reg;
2530
2531 switch (dwc->link_state) {
2532 case DWC3_LINK_STATE_U1:
2533 case DWC3_LINK_STATE_U2:
2534 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2535 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2536 | DWC3_DCTL_ACCEPTU2ENA
2537 | DWC3_DCTL_INITU1ENA
2538 | DWC3_DCTL_ACCEPTU1ENA);
2539
2540 if (!dwc->u1u2)
2541 dwc->u1u2 = reg & u1u2;
2542
2543 reg &= ~u1u2;
2544
2545 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2546 break;
2547 default:
2548 /* do nothing */
2549 break;
2550 }
2551 }
2552 }
2553
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002554 switch (next) {
2555 case DWC3_LINK_STATE_U1:
2556 if (dwc->speed == USB_SPEED_SUPER)
2557 dwc3_suspend_gadget(dwc);
2558 break;
2559 case DWC3_LINK_STATE_U2:
2560 case DWC3_LINK_STATE_U3:
2561 dwc3_suspend_gadget(dwc);
2562 break;
2563 case DWC3_LINK_STATE_RESUME:
2564 dwc3_resume_gadget(dwc);
2565 break;
2566 default:
2567 /* do nothing */
2568 break;
2569 }
2570
Felipe Balbie57ebc12014-04-22 13:20:12 -05002571 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002572}
2573
Felipe Balbie1dadd32014-02-25 14:47:54 -06002574static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2575 unsigned int evtinfo)
2576{
2577 unsigned int is_ss = evtinfo & BIT(4);
2578
2579 /**
2580 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2581 * have a known issue which can cause USB CV TD.9.23 to fail
2582 * randomly.
2583 *
2584 * Because of this issue, core could generate bogus hibernation
2585 * events which SW needs to ignore.
2586 *
2587 * Refers to:
2588 *
2589 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2590 * Device Fallback from SuperSpeed
2591 */
2592 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2593 return;
2594
2595 /* enter hibernation here */
2596}
2597
Felipe Balbi72246da2011-08-19 18:10:58 +03002598static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2599 const struct dwc3_event_devt *event)
2600{
2601 switch (event->type) {
2602 case DWC3_DEVICE_EVENT_DISCONNECT:
2603 dwc3_gadget_disconnect_interrupt(dwc);
2604 break;
2605 case DWC3_DEVICE_EVENT_RESET:
2606 dwc3_gadget_reset_interrupt(dwc);
2607 break;
2608 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2609 dwc3_gadget_conndone_interrupt(dwc);
2610 break;
2611 case DWC3_DEVICE_EVENT_WAKEUP:
2612 dwc3_gadget_wakeup_interrupt(dwc);
2613 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002614 case DWC3_DEVICE_EVENT_HIBER_REQ:
2615 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2616 "unexpected hibernation event\n"))
2617 break;
2618
2619 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2620 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002621 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2622 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2623 break;
2624 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002625 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002626 break;
2627 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002628 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002629 break;
2630 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002631 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002632 break;
2633 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002634 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002635 break;
2636 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002637 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002638 break;
2639 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002640 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002641 }
2642}
2643
2644static void dwc3_process_event_entry(struct dwc3 *dwc,
2645 const union dwc3_event *event)
2646{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002647 trace_dwc3_event(event->raw);
2648
Felipe Balbi72246da2011-08-19 18:10:58 +03002649 /* Endpoint IRQ, handle it and return early */
2650 if (event->type.is_devspec == 0) {
2651 /* depevt */
2652 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2653 }
2654
2655 switch (event->type.type) {
2656 case DWC3_EVENT_TYPE_DEV:
2657 dwc3_gadget_interrupt(dwc, &event->devt);
2658 break;
2659 /* REVISIT what to do with Carkit and I2C events ? */
2660 default:
2661 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2662 }
2663}
2664
Felipe Balbidea520a2016-03-30 09:39:34 +03002665static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002666{
Felipe Balbidea520a2016-03-30 09:39:34 +03002667 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002668 irqreturn_t ret = IRQ_NONE;
2669 int left;
2670 u32 reg;
2671
Felipe Balbif42f2442013-06-12 21:25:08 +03002672 left = evt->count;
2673
2674 if (!(evt->flags & DWC3_EVENT_PENDING))
2675 return IRQ_NONE;
2676
2677 while (left > 0) {
2678 union dwc3_event event;
2679
2680 event.raw = *(u32 *) (evt->buf + evt->lpos);
2681
2682 dwc3_process_event_entry(dwc, &event);
2683
2684 /*
2685 * FIXME we wrap around correctly to the next entry as
2686 * almost all entries are 4 bytes in size. There is one
2687 * entry which has 12 bytes which is a regular entry
2688 * followed by 8 bytes data. ATM I don't know how
2689 * things are organized if we get next to the a
2690 * boundary so I worry about that once we try to handle
2691 * that.
2692 */
2693 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2694 left -= 4;
2695
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002696 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002697 }
2698
2699 evt->count = 0;
2700 evt->flags &= ~DWC3_EVENT_PENDING;
2701 ret = IRQ_HANDLED;
2702
2703 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002704 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002705 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002706 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002707
2708 return ret;
2709}
2710
Felipe Balbidea520a2016-03-30 09:39:34 +03002711static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002712{
Felipe Balbidea520a2016-03-30 09:39:34 +03002713 struct dwc3_event_buffer *evt = _evt;
2714 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002715 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002716 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002717
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002718 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002719 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002720 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002721
2722 return ret;
2723}
2724
Felipe Balbidea520a2016-03-30 09:39:34 +03002725static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002726{
Felipe Balbidea520a2016-03-30 09:39:34 +03002727 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002728 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002729 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002730
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002731 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002732 count &= DWC3_GEVNTCOUNT_MASK;
2733 if (!count)
2734 return IRQ_NONE;
2735
Felipe Balbib15a7622011-06-30 16:57:15 +03002736 evt->count = count;
2737 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002738
Felipe Balbie8adfc32013-06-12 21:11:14 +03002739 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002740 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002741 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002742 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002743
Felipe Balbib15a7622011-06-30 16:57:15 +03002744 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002745}
2746
Felipe Balbidea520a2016-03-30 09:39:34 +03002747static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002748{
Felipe Balbidea520a2016-03-30 09:39:34 +03002749 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002750
Felipe Balbidea520a2016-03-30 09:39:34 +03002751 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002752}
2753
2754/**
2755 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002756 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 *
2758 * Returns 0 on success otherwise negative errno.
2759 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002760int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002761{
Felipe Balbi72246da2011-08-19 18:10:58 +03002762 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002763
2764 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2765 &dwc->ctrl_req_addr, GFP_KERNEL);
2766 if (!dwc->ctrl_req) {
2767 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2768 ret = -ENOMEM;
2769 goto err0;
2770 }
2771
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302772 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002773 &dwc->ep0_trb_addr, GFP_KERNEL);
2774 if (!dwc->ep0_trb) {
2775 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2776 ret = -ENOMEM;
2777 goto err1;
2778 }
2779
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002780 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002781 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 ret = -ENOMEM;
2783 goto err2;
2784 }
2785
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002786 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002787 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2788 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002789 if (!dwc->ep0_bounce) {
2790 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2791 ret = -ENOMEM;
2792 goto err3;
2793 }
2794
Felipe Balbi04c03d12015-12-02 10:06:45 -06002795 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2796 if (!dwc->zlp_buf) {
2797 ret = -ENOMEM;
2798 goto err4;
2799 }
2800
Felipe Balbi72246da2011-08-19 18:10:58 +03002801 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002802 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002803 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002805 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002806
2807 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002808 * FIXME We might be setting max_speed to <SUPER, however versions
2809 * <2.20a of dwc3 have an issue with metastability (documented
2810 * elsewhere in this driver) which tells us we can't set max speed to
2811 * anything lower than SUPER.
2812 *
2813 * Because gadget.max_speed is only used by composite.c and function
2814 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2815 * to happen so we avoid sending SuperSpeed Capability descriptor
2816 * together with our BOS descriptor as that could confuse host into
2817 * thinking we can handle super speed.
2818 *
2819 * Note that, in fact, we won't even support GetBOS requests when speed
2820 * is less than super speed because we don't have means, yet, to tell
2821 * composite.c that we are USB 2.0 + LPM ECN.
2822 */
2823 if (dwc->revision < DWC3_REVISION_220A)
2824 dwc3_trace(trace_dwc3_gadget,
2825 "Changing max_speed on rev %08x\n",
2826 dwc->revision);
2827
2828 dwc->gadget.max_speed = dwc->maximum_speed;
2829
2830 /*
David Cohena4b9d942013-12-09 15:55:38 -08002831 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2832 * on ep out.
2833 */
2834 dwc->gadget.quirk_ep_out_aligned_size = true;
2835
2836 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002837 * REVISIT: Here we should clear all pending IRQs to be
2838 * sure we're starting from a well known location.
2839 */
2840
2841 ret = dwc3_gadget_init_endpoints(dwc);
2842 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002843 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002844
Felipe Balbi72246da2011-08-19 18:10:58 +03002845 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2846 if (ret) {
2847 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002848 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002849 }
2850
2851 return 0;
2852
Felipe Balbi04c03d12015-12-02 10:06:45 -06002853err5:
2854 kfree(dwc->zlp_buf);
2855
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002856err4:
David Cohene1f80462013-09-11 17:42:47 -07002857 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002858 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2859 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002860
Felipe Balbi72246da2011-08-19 18:10:58 +03002861err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002862 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002863
2864err2:
2865 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2866 dwc->ep0_trb, dwc->ep0_trb_addr);
2867
2868err1:
2869 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2870 dwc->ctrl_req, dwc->ctrl_req_addr);
2871
2872err0:
2873 return ret;
2874}
2875
Felipe Balbi7415f172012-04-30 14:56:33 +03002876/* -------------------------------------------------------------------------- */
2877
Felipe Balbi72246da2011-08-19 18:10:58 +03002878void dwc3_gadget_exit(struct dwc3 *dwc)
2879{
Felipe Balbi72246da2011-08-19 18:10:58 +03002880 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002881
Felipe Balbi72246da2011-08-19 18:10:58 +03002882 dwc3_gadget_free_endpoints(dwc);
2883
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002884 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2885 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002886
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002887 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002888 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002889
2890 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2891 dwc->ep0_trb, dwc->ep0_trb_addr);
2892
2893 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2894 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002895}
Felipe Balbi7415f172012-04-30 14:56:33 +03002896
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002897int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002898{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002899 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002900 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002901 dwc3_gadget_run_stop(dwc, true, true);
2902 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002903
Felipe Balbi7415f172012-04-30 14:56:33 +03002904 __dwc3_gadget_ep_disable(dwc->eps[0]);
2905 __dwc3_gadget_ep_disable(dwc->eps[1]);
2906
2907 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2908
2909 return 0;
2910}
2911
2912int dwc3_gadget_resume(struct dwc3 *dwc)
2913{
2914 struct dwc3_ep *dep;
2915 int ret;
2916
2917 /* Start with SuperSpeed Default */
2918 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2919
2920 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002921 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2922 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002923 if (ret)
2924 goto err0;
2925
2926 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002927 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2928 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002929 if (ret)
2930 goto err1;
2931
2932 /* begin to receive SETUP packets */
2933 dwc->ep0state = EP0_SETUP_PHASE;
2934 dwc3_ep0_out_start(dwc);
2935
2936 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2937
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002938 if (dwc->pullups_connected) {
2939 dwc3_gadget_enable_irq(dwc);
2940 dwc3_gadget_run_stop(dwc, true, false);
2941 }
2942
Felipe Balbi7415f172012-04-30 14:56:33 +03002943 return 0;
2944
2945err1:
2946 __dwc3_gadget_ep_disable(dwc->eps[0]);
2947
2948err0:
2949 return ret;
2950}