blob: c889ee371cb6c6f27047122ee9845c196dcd0ece [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
John Youndca01192016-05-19 17:26:05 -0700148/**
149 * dwc3_ep_inc_trb() - Increment a TRB index.
150 * @index - Pointer to the TRB index to increment.
151 *
152 * The index should never point to the link TRB. After incrementing,
153 * if it is point to the link TRB, wrap around to the beginning. The
154 * link TRB is always at the last TRB entry.
155 */
156static void dwc3_ep_inc_trb(u8 *index)
157{
158 (*index)++;
159 if (*index == (DWC3_TRB_NUM - 1))
160 *index = 0;
161}
162
Felipe Balbief966b92016-04-05 13:09:51 +0300163static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200164{
John Youndca01192016-05-19 17:26:05 -0700165 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300166}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167
Felipe Balbief966b92016-04-05 13:09:51 +0300168static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
169{
John Youndca01192016-05-19 17:26:05 -0700170 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200171}
172
Felipe Balbi72246da2011-08-19 18:10:58 +0300173void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
174 int status)
175{
176 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530177 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300178
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200179 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530180 i = 0;
181 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300182 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530183 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200184 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300185 }
186 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200187 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300188
189 if (req->request.status == -EINPROGRESS)
190 req->request.status = status;
191
Pratyush Anand0416e492012-08-10 13:42:16 +0530192 if (dwc->ep0_bounced && dep->number == 0)
193 dwc->ep0_bounced = false;
194 else
195 usb_gadget_unmap_request(&dwc->gadget, &req->request,
196 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300197
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500198 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300199
200 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200201 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300202 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300203
204 if (dep->number > 1)
205 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300206}
207
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500208int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300209{
210 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300211 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300212 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300213 u32 reg;
214
215 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
216 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
217
218 do {
219 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
220 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300221 status = DWC3_DGCMD_STATUS(reg);
222 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300223 ret = -EINVAL;
224 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300225 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300226 } while (timeout--);
227
228 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300229 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300230 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300231 }
232
Felipe Balbi71f7e702016-05-23 14:16:19 +0300233 trace_dwc3_gadget_generic_cmd(cmd, param, status);
234
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236}
237
Felipe Balbic36d8e92016-04-04 12:46:33 +0300238static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
239
Felipe Balbi2cd47182016-04-12 16:42:43 +0300240int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
241 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300242{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300243 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200244 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300245 u32 reg;
246
Felipe Balbi0933df12016-05-23 14:02:33 +0300247 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300249 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300251 /*
252 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
253 * we're issuing an endpoint command, we must check if
254 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
255 *
256 * We will also set SUSPHY bit to what it was before returning as stated
257 * by the same section on Synopsys databook.
258 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300259 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
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 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300266 }
267
Felipe Balbic36d8e92016-04-04 12:46:33 +0300268 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
269 int needs_wakeup;
270
271 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
272 dwc->link_state == DWC3_LINK_STATE_U2 ||
273 dwc->link_state == DWC3_LINK_STATE_U3);
274
275 if (unlikely(needs_wakeup)) {
276 ret = __dwc3_gadget_wakeup(dwc);
277 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
278 ret);
279 }
280 }
281
Felipe Balbi2eb88012016-04-12 16:53:39 +0300282 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
283 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
284 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
Felipe Balbi2eb88012016-04-12 16:53:39 +0300286 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300288 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300290 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000291
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000292 switch (cmd_status) {
293 case 0:
294 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300295 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000296 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000297 ret = -EINVAL;
298 break;
299 case DEPEVT_TRANSFER_BUS_EXPIRY:
300 /*
301 * SW issues START TRANSFER command to
302 * isochronous ep with future frame interval. If
303 * future interval time has already passed when
304 * core receives the command, it will respond
305 * with an error status of 'Bus Expiry'.
306 *
307 * Instead of always returning -EINVAL, let's
308 * give a hint to the gadget driver that this is
309 * the case by returning -EAGAIN.
310 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000311 ret = -EAGAIN;
312 break;
313 default:
314 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
315 }
316
Felipe Balbic0ca3242016-04-04 09:11:51 +0300317 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300318 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300319 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300320
Felipe Balbif6bb2252016-05-23 13:53:34 +0300321 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300322 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300323 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300324 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300325
Felipe Balbi0933df12016-05-23 14:02:33 +0300326 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
327
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300328 if (unlikely(susphy)) {
329 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
330 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
331 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
332 }
333
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300335}
336
John Youn50c763f2016-05-31 17:49:56 -0700337static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
338{
339 struct dwc3 *dwc = dep->dwc;
340 struct dwc3_gadget_ep_cmd_params params;
341 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
342
343 /*
344 * As of core revision 2.60a the recommended programming model
345 * is to set the ClearPendIN bit when issuing a Clear Stall EP
346 * command for IN endpoints. This is to prevent an issue where
347 * some (non-compliant) hosts may not send ACK TPs for pending
348 * IN transfers due to a mishandled error condition. Synopsys
349 * STAR 9000614252.
350 */
351 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
352 cmd |= DWC3_DEPCMD_CLEARPENDIN;
353
354 memset(&params, 0, sizeof(params));
355
Felipe Balbi2cd47182016-04-12 16:42:43 +0300356 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700357}
358
Felipe Balbi72246da2011-08-19 18:10:58 +0300359static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200360 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300361{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300362 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
364 return dep->trb_pool_dma + offset;
365}
366
367static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
368{
369 struct dwc3 *dwc = dep->dwc;
370
371 if (dep->trb_pool)
372 return 0;
373
Felipe Balbi72246da2011-08-19 18:10:58 +0300374 dep->trb_pool = dma_alloc_coherent(dwc->dev,
375 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
376 &dep->trb_pool_dma, GFP_KERNEL);
377 if (!dep->trb_pool) {
378 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
379 dep->name);
380 return -ENOMEM;
381 }
382
383 return 0;
384}
385
386static void dwc3_free_trb_pool(struct dwc3_ep *dep)
387{
388 struct dwc3 *dwc = dep->dwc;
389
390 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
391 dep->trb_pool, dep->trb_pool_dma);
392
393 dep->trb_pool = NULL;
394 dep->trb_pool_dma = 0;
395}
396
John Younc4509602016-02-16 20:10:53 -0800397static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
398
399/**
400 * dwc3_gadget_start_config - Configure EP resources
401 * @dwc: pointer to our controller context structure
402 * @dep: endpoint that is being enabled
403 *
404 * The assignment of transfer resources cannot perfectly follow the
405 * data book due to the fact that the controller driver does not have
406 * all knowledge of the configuration in advance. It is given this
407 * information piecemeal by the composite gadget framework after every
408 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
409 * programming model in this scenario can cause errors. For two
410 * reasons:
411 *
412 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
413 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
414 * multiple interfaces.
415 *
416 * 2) The databook does not mention doing more DEPXFERCFG for new
417 * endpoint on alt setting (8.1.6).
418 *
419 * The following simplified method is used instead:
420 *
421 * All hardware endpoints can be assigned a transfer resource and this
422 * setting will stay persistent until either a core reset or
423 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
424 * do DEPXFERCFG for every hardware endpoint as well. We are
425 * guaranteed that there are as many transfer resources as endpoints.
426 *
427 * This function is called for each endpoint when it is being enabled
428 * but is triggered only when called for EP0-out, which always happens
429 * first, and which should only happen in one of the above conditions.
430 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300431static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
432{
433 struct dwc3_gadget_ep_cmd_params params;
434 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800435 int i;
436 int ret;
437
438 if (dep->number)
439 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
441 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800442 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Felipe Balbi2cd47182016-04-12 16:42:43 +0300444 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800445 if (ret)
446 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300447
John Younc4509602016-02-16 20:10:53 -0800448 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
449 struct dwc3_ep *dep = dwc->eps[i];
450
451 if (!dep)
452 continue;
453
454 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
455 if (ret)
456 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300457 }
458
459 return 0;
460}
461
462static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200463 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300464 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300465 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300466{
467 struct dwc3_gadget_ep_cmd_params params;
468
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300469 if (dev_WARN_ONCE(dwc->dev, modify && restore,
470 "Can't modify and restore\n"))
471 return -EINVAL;
472
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 memset(&params, 0x00, sizeof(params));
474
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300475 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900476 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
477
478 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800479 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300480 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300481 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900482 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300483
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300484 if (modify) {
485 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
486 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600487 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
488 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300489 } else {
490 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600491 }
492
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300493 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
494
495 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
496 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300497
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200498 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300499 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
500 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300501 dep->stream_capable = true;
502 }
503
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500504 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300505 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
507 /*
508 * We are doing 1:1 mapping for endpoints, meaning
509 * Physical Endpoints 2 maps to Logical Endpoint 2 and
510 * so on. We consider the direction bit as part of the physical
511 * endpoint number. So USB endpoint 0x81 is 0x03.
512 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300513 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
515 /*
516 * We must use the lower 16 TX FIFOs even though
517 * HW might have more
518 */
519 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300520 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300521
522 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300523 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300524 dep->interval = 1 << (desc->bInterval - 1);
525 }
526
Felipe Balbi2cd47182016-04-12 16:42:43 +0300527 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528}
529
530static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
531{
532 struct dwc3_gadget_ep_cmd_params params;
533
534 memset(&params, 0x00, sizeof(params));
535
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300536 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300537
Felipe Balbi2cd47182016-04-12 16:42:43 +0300538 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
539 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540}
541
542/**
543 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
544 * @dep: endpoint to be initialized
545 * @desc: USB Endpoint Descriptor
546 *
547 * Caller should take care of locking
548 */
549static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200550 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300551 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300552 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300553{
554 struct dwc3 *dwc = dep->dwc;
555 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300556 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbi73815282015-01-27 13:48:14 -0600558 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300559
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 if (!(dep->flags & DWC3_EP_ENABLED)) {
561 ret = dwc3_gadget_start_config(dwc, dep);
562 if (ret)
563 return ret;
564 }
565
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300566 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600567 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 if (ret)
569 return ret;
570
571 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200572 struct dwc3_trb *trb_st_hw;
573 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300574
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200575 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200576 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 dep->type = usb_endpoint_type(desc);
578 dep->flags |= DWC3_EP_ENABLED;
579
580 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
581 reg |= DWC3_DALEPENA_EP(dep->number);
582 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
583
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300584 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300585 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300586
John Youn0d257442016-05-19 17:26:08 -0700587 /* Initialize the TRB ring */
588 dep->trb_dequeue = 0;
589 dep->trb_enqueue = 0;
590 memset(dep->trb_pool, 0,
591 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
592
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300593 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 trb_st_hw = &dep->trb_pool[0];
595
Felipe Balbif6bafc62012-02-06 11:04:53 +0200596 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200597 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
598 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
599 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
600 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 }
602
603 return 0;
604}
605
Paul Zimmermanb992e682012-04-27 14:17:35 +0300606static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200607static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300608{
609 struct dwc3_request *req;
Felipe Balbi69450c42016-05-30 13:37:02 +0300610 struct dwc3_trb *current_trb;
611 unsigned transfer_in_flight;
Felipe Balbi72246da2011-08-19 18:10:58 +0300612
Felipe Balbi69450c42016-05-30 13:37:02 +0300613 if (dep->number > 1)
614 current_trb = &dep->trb_pool[dep->trb_enqueue];
615 else
616 current_trb = &dwc->ep0_trb[dep->trb_enqueue];
617 transfer_in_flight = current_trb->ctrl & DWC3_TRB_CTRL_HWO;
618
619 if (transfer_in_flight && !list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300620 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200621
Pratyush Anand57911502012-07-06 15:19:10 +0530622 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200623 while (!list_empty(&dep->started_list)) {
624 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530625
626 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
627 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200628 }
629
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200630 while (!list_empty(&dep->pending_list)) {
631 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300632
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200633 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300635}
636
637/**
638 * __dwc3_gadget_ep_disable - Disables a HW endpoint
639 * @dep: the endpoint to disable
640 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200641 * This function also removes requests which are currently processed ny the
642 * hardware and those which are not yet scheduled.
643 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300645static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
646{
647 struct dwc3 *dwc = dep->dwc;
648 u32 reg;
649
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500650 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
651
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200652 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300653
Felipe Balbi687ef982014-04-16 10:30:33 -0500654 /* make sure HW endpoint isn't stalled */
655 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500656 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500657
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
659 reg &= ~DWC3_DALEPENA_EP(dep->number);
660 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
661
Felipe Balbi879631a2011-09-30 10:58:47 +0300662 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200663 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200664 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300666 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300667
668 return 0;
669}
670
671/* -------------------------------------------------------------------------- */
672
673static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
674 const struct usb_endpoint_descriptor *desc)
675{
676 return -EINVAL;
677}
678
679static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
680{
681 return -EINVAL;
682}
683
684/* -------------------------------------------------------------------------- */
685
686static int dwc3_gadget_ep_enable(struct usb_ep *ep,
687 const struct usb_endpoint_descriptor *desc)
688{
689 struct dwc3_ep *dep;
690 struct dwc3 *dwc;
691 unsigned long flags;
692 int ret;
693
694 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
695 pr_debug("dwc3: invalid parameters\n");
696 return -EINVAL;
697 }
698
699 if (!desc->wMaxPacketSize) {
700 pr_debug("dwc3: missing wMaxPacketSize\n");
701 return -EINVAL;
702 }
703
704 dep = to_dwc3_ep(ep);
705 dwc = dep->dwc;
706
Felipe Balbi95ca9612015-12-10 13:08:20 -0600707 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
708 "%s is already enabled\n",
709 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300710 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300711
Felipe Balbi72246da2011-08-19 18:10:58 +0300712 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600713 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300714 spin_unlock_irqrestore(&dwc->lock, flags);
715
716 return ret;
717}
718
719static int dwc3_gadget_ep_disable(struct usb_ep *ep)
720{
721 struct dwc3_ep *dep;
722 struct dwc3 *dwc;
723 unsigned long flags;
724 int ret;
725
726 if (!ep) {
727 pr_debug("dwc3: invalid parameters\n");
728 return -EINVAL;
729 }
730
731 dep = to_dwc3_ep(ep);
732 dwc = dep->dwc;
733
Felipe Balbi95ca9612015-12-10 13:08:20 -0600734 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
735 "%s is already disabled\n",
736 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300738
Felipe Balbi72246da2011-08-19 18:10:58 +0300739 spin_lock_irqsave(&dwc->lock, flags);
740 ret = __dwc3_gadget_ep_disable(dep);
741 spin_unlock_irqrestore(&dwc->lock, flags);
742
743 return ret;
744}
745
746static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
747 gfp_t gfp_flags)
748{
749 struct dwc3_request *req;
750 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300751
752 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900753 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300755
756 req->epnum = dep->number;
757 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
Felipe Balbi68d34c82016-05-30 13:34:58 +0300759 dep->allocated_requests++;
760
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500761 trace_dwc3_alloc_request(req);
762
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 return &req->request;
764}
765
766static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
767 struct usb_request *request)
768{
769 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300770 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300771
Felipe Balbi68d34c82016-05-30 13:34:58 +0300772 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500773 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300774 kfree(req);
775}
776
Felipe Balbic71fc372011-11-22 11:37:34 +0200777/**
778 * dwc3_prepare_one_trb - setup one TRB from one request
779 * @dep: endpoint for which this request is prepared
780 * @req: dwc3_request pointer
781 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200782static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200783 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530784 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200785{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200786 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200787
Felipe Balbi73815282015-01-27 13:48:14 -0600788 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200789 dep->name, req, (unsigned long long) dma,
790 length, last ? " last" : "",
791 chain ? " chain" : "");
792
Pratyush Anand915e2022013-01-14 15:59:35 +0530793
Felipe Balbi4faf7552016-04-05 13:14:31 +0300794 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200795
Felipe Balbieeb720f2011-11-28 12:46:59 +0200796 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200797 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200798 req->trb = trb;
799 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300800 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200801 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200802
Felipe Balbief966b92016-04-05 13:09:51 +0300803 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530804
Felipe Balbif6bafc62012-02-06 11:04:53 +0200805 trb->size = DWC3_TRB_SIZE_LENGTH(length);
806 trb->bpl = lower_32_bits(dma);
807 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200808
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200809 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200810 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200811 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200812 break;
813
814 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530815 if (!node)
816 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
817 else
818 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200819
820 /* always enable Interrupt on Missed ISOC */
821 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200822 break;
823
824 case USB_ENDPOINT_XFER_BULK:
825 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200826 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200827 break;
828 default:
829 /*
830 * This is only possible with faulty memory because we
831 * checked it already :)
832 */
833 BUG();
834 }
835
Felipe Balbica4d44e2016-03-10 13:53:27 +0200836 /* always enable Continue on Short Packet */
837 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600838
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300839 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200840 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
841
842 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530843 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200844
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530845 if (chain)
846 trb->ctrl |= DWC3_TRB_CTRL_CHN;
847
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200848 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200849 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
850
851 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500852
Felipe Balbi68d34c82016-05-30 13:34:58 +0300853 dep->queued_requests++;
854
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500855 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200856}
857
John Youn361572b2016-05-19 17:26:17 -0700858/**
859 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
860 * @dep: The endpoint with the TRB ring
861 * @index: The index of the current TRB in the ring
862 *
863 * Returns the TRB prior to the one pointed to by the index. If the
864 * index is 0, we will wrap backwards, skip the link TRB, and return
865 * the one just before that.
866 */
867static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
868{
869 if (!index)
870 index = DWC3_TRB_NUM - 2;
871 else
872 index = dep->trb_enqueue - 1;
873
874 return &dep->trb_pool[index];
875}
876
Felipe Balbic4233572016-05-12 14:08:34 +0300877static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
878{
879 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700880 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300881
882 /*
883 * If enqueue & dequeue are equal than it is either full or empty.
884 *
885 * One way to know for sure is if the TRB right before us has HWO bit
886 * set or not. If it has, then we're definitely full and can't fit any
887 * more transfers in our ring.
888 */
889 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -0700890 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
891 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
892 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +0300893
894 return DWC3_TRB_NUM - 1;
895 }
896
John Youn32db3d92016-05-19 17:26:12 -0700897 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -0700898 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -0700899
John Youn7d0a0382016-05-19 17:26:15 -0700900 if (dep->trb_dequeue < dep->trb_enqueue)
901 trbs_left--;
902
John Youn32db3d92016-05-19 17:26:12 -0700903 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300904}
905
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300906static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi55a02372016-05-30 13:38:32 +0300907 struct dwc3_request *req, unsigned int trbs_left,
908 unsigned int more_coming)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300909{
910 struct usb_request *request = &req->request;
911 struct scatterlist *sg = request->sg;
912 struct scatterlist *s;
913 unsigned int last = false;
914 unsigned int length;
915 dma_addr_t dma;
916 int i;
917
918 for_each_sg(sg, s, request->num_mapped_sgs, i) {
919 unsigned chain = true;
920
921 length = sg_dma_len(s);
922 dma = sg_dma_address(s);
923
924 if (sg_is_last(s)) {
Felipe Balbi55a02372016-05-30 13:38:32 +0300925 if (usb_endpoint_xfer_int(dep->endpoint.desc) ||
926 !more_coming)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300927 last = true;
928
929 chain = false;
930 }
931
Felipe Balbid6dc2e72016-05-30 13:42:33 +0300932 if (!trbs_left--)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300933 last = true;
934
935 if (last)
936 chain = false;
937
938 dwc3_prepare_one_trb(dep, req, dma, length,
939 last, chain, i);
940
941 if (last)
942 break;
943 }
944}
945
946static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi55a02372016-05-30 13:38:32 +0300947 struct dwc3_request *req, unsigned int trbs_left,
948 unsigned int more_coming)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300949{
950 unsigned int last = false;
951 unsigned int length;
952 dma_addr_t dma;
953
954 dma = req->request.dma;
955 length = req->request.length;
956
957 if (!trbs_left)
958 last = true;
959
960 /* Is this the last request? */
Felipe Balbi55a02372016-05-30 13:38:32 +0300961 if (usb_endpoint_xfer_int(dep->endpoint.desc) || !more_coming)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300962 last = true;
963
964 dwc3_prepare_one_trb(dep, req, dma, length,
965 last, false, 0);
966}
967
Felipe Balbi72246da2011-08-19 18:10:58 +0300968/*
969 * dwc3_prepare_trbs - setup TRBs from requests
970 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300971 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800972 * The function goes through the requests list and sets up TRBs for the
973 * transfers. The function returns once there are no more TRBs available or
974 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300975 */
Felipe Balbic4233572016-05-12 14:08:34 +0300976static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300977{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200978 struct dwc3_request *req, *n;
Felipe Balbi55a02372016-05-30 13:38:32 +0300979 unsigned int more_coming;
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 u32 trbs_left;
981
982 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
983
Felipe Balbic4233572016-05-12 14:08:34 +0300984 trbs_left = dwc3_calc_trbs_left(dep);
John Youn89bc8562016-05-19 17:26:10 -0700985 if (!trbs_left)
986 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300987
Felipe Balbi55a02372016-05-30 13:38:32 +0300988 more_coming = dep->allocated_requests - dep->queued_requests;
989
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200990 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300991 if (req->request.num_mapped_sgs > 0)
Felipe Balbi55a02372016-05-30 13:38:32 +0300992 dwc3_prepare_one_trb_sg(dep, req, trbs_left--,
993 more_coming);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300994 else
Felipe Balbi55a02372016-05-30 13:38:32 +0300995 dwc3_prepare_one_trb_linear(dep, req, trbs_left--,
996 more_coming);
Felipe Balbi72246da2011-08-19 18:10:58 +0300997
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300998 if (!trbs_left)
999 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001001}
1002
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001003static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001004{
1005 struct dwc3_gadget_ep_cmd_params params;
1006 struct dwc3_request *req;
1007 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001008 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001009 int ret;
1010 u32 cmd;
1011
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001012 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001013
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001014 dwc3_prepare_trbs(dep);
1015 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 if (!req) {
1017 dep->flags |= DWC3_EP_PENDING_REQUEST;
1018 return 0;
1019 }
1020
1021 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001022
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001023 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301024 params.param0 = upper_32_bits(req->trb_dma);
1025 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001026 cmd = DWC3_DEPCMD_STARTTRANSFER |
1027 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301028 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001029 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1030 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301031 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001032
Felipe Balbi2cd47182016-04-12 16:42:43 +03001033 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001034 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 /*
1036 * FIXME we need to iterate over the list of requests
1037 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001038 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001039 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001040 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1041 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001042 list_del(&req->list);
1043 return ret;
1044 }
1045
1046 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001047
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001048 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001049 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001050 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001051 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001052
Felipe Balbi72246da2011-08-19 18:10:58 +03001053 return 0;
1054}
1055
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301056static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1057 struct dwc3_ep *dep, u32 cur_uf)
1058{
1059 u32 uf;
1060
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001061 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001062 dwc3_trace(trace_dwc3_gadget,
1063 "ISOC ep %s run out for requests",
1064 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301065 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301066 return;
1067 }
1068
1069 /* 4 micro frames in the future */
1070 uf = cur_uf + dep->interval * 4;
1071
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001072 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301073}
1074
1075static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1076 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1077{
1078 u32 cur_uf, mask;
1079
1080 mask = ~(dep->interval - 1);
1081 cur_uf = event->parameters & mask;
1082
1083 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1084}
1085
Felipe Balbi72246da2011-08-19 18:10:58 +03001086static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1087{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001088 struct dwc3 *dwc = dep->dwc;
1089 int ret;
1090
Felipe Balbibb423982015-11-16 15:31:21 -06001091 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001092 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001093 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001094 &req->request, dep->endpoint.name);
1095 return -ESHUTDOWN;
1096 }
1097
1098 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1099 &req->request, req->dep->name)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001100 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001101 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001102 return -EINVAL;
1103 }
1104
Felipe Balbifc8bb912016-05-16 13:14:48 +03001105 pm_runtime_get(dwc->dev);
1106
Felipe Balbi72246da2011-08-19 18:10:58 +03001107 req->request.actual = 0;
1108 req->request.status = -EINPROGRESS;
1109 req->direction = dep->direction;
1110 req->epnum = dep->number;
1111
Felipe Balbife84f522015-09-01 09:01:38 -05001112 trace_dwc3_ep_queue(req);
1113
Felipe Balbi72246da2011-08-19 18:10:58 +03001114 /*
1115 * We only add to our list of requests now and
1116 * start consuming the list once we get XferNotReady
1117 * IRQ.
1118 *
1119 * That way, we avoid doing anything that we don't need
1120 * to do now and defer it until the point we receive a
1121 * particular token from the Host side.
1122 *
1123 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001124 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001125 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001126 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1127 dep->direction);
1128 if (ret)
1129 return ret;
1130
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001131 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001132
1133 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001134 * If there are no pending requests and the endpoint isn't already
1135 * busy, we will just start the request straight away.
1136 *
1137 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1138 * little bit faster.
1139 */
1140 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiba62c092016-05-30 13:41:22 +03001141 !usb_endpoint_xfer_int(dep->endpoint.desc)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001142 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001143 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001144 }
1145
1146 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001147 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001148 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001149 * 1. XferNotReady with empty list of requests. We need to kick the
1150 * transfer here in that situation, otherwise we will be NAKing
1151 * forever. If we get XferNotReady before gadget driver has a
1152 * chance to queue a request, we will ACK the IRQ but won't be
1153 * able to receive the data until the next request is queued.
1154 * The following code is handling exactly that.
1155 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001156 */
1157 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301158 /*
1159 * If xfernotready is already elapsed and it is a case
1160 * of isoc transfer, then issue END TRANSFER, so that
1161 * you can receive xfernotready again and can have
1162 * notion of current microframe.
1163 */
1164 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001165 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001166 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301167 dep->flags = DWC3_EP_ENABLED;
1168 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301169 return 0;
1170 }
1171
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001172 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001173 if (!ret)
1174 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1175
Felipe Balbia8f32812015-09-16 10:40:07 -05001176 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001177 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001178
Felipe Balbib511e5e2012-06-06 12:00:50 +03001179 /*
1180 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1181 * kick the transfer here after queuing a request, otherwise the
1182 * core may not see the modified TRB(s).
1183 */
1184 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301185 (dep->flags & DWC3_EP_BUSY) &&
1186 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001187 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001188 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001189 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001190 }
1191
Felipe Balbib997ada2012-07-26 13:26:50 +03001192 /*
1193 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1194 * right away, otherwise host will not know we have streams to be
1195 * handled.
1196 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001197 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001198 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001199
Felipe Balbia8f32812015-09-16 10:40:07 -05001200out:
1201 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001202 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001203 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001204 dep->name);
1205 if (ret == -EBUSY)
1206 ret = 0;
1207
1208 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001209}
1210
Felipe Balbi04c03d12015-12-02 10:06:45 -06001211static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1212 struct usb_request *request)
1213{
1214 dwc3_gadget_ep_free_request(ep, request);
1215}
1216
1217static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1218{
1219 struct dwc3_request *req;
1220 struct usb_request *request;
1221 struct usb_ep *ep = &dep->endpoint;
1222
Felipe Balbi60cfb372016-05-24 13:45:17 +03001223 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001224 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1225 if (!request)
1226 return -ENOMEM;
1227
1228 request->length = 0;
1229 request->buf = dwc->zlp_buf;
1230 request->complete = __dwc3_gadget_ep_zlp_complete;
1231
1232 req = to_dwc3_request(request);
1233
1234 return __dwc3_gadget_ep_queue(dep, req);
1235}
1236
Felipe Balbi72246da2011-08-19 18:10:58 +03001237static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1238 gfp_t gfp_flags)
1239{
1240 struct dwc3_request *req = to_dwc3_request(request);
1241 struct dwc3_ep *dep = to_dwc3_ep(ep);
1242 struct dwc3 *dwc = dep->dwc;
1243
1244 unsigned long flags;
1245
1246 int ret;
1247
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001248 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001249 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001250
1251 /*
1252 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1253 * setting request->zero, instead of doing magic, we will just queue an
1254 * extra usb_request ourselves so that it gets handled the same way as
1255 * any other request.
1256 */
John Yound92618982015-12-22 12:23:20 -08001257 if (ret == 0 && request->zero && request->length &&
1258 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001259 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1260
Felipe Balbi72246da2011-08-19 18:10:58 +03001261 spin_unlock_irqrestore(&dwc->lock, flags);
1262
1263 return ret;
1264}
1265
1266static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1267 struct usb_request *request)
1268{
1269 struct dwc3_request *req = to_dwc3_request(request);
1270 struct dwc3_request *r = NULL;
1271
1272 struct dwc3_ep *dep = to_dwc3_ep(ep);
1273 struct dwc3 *dwc = dep->dwc;
1274
1275 unsigned long flags;
1276 int ret = 0;
1277
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001278 trace_dwc3_ep_dequeue(req);
1279
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 spin_lock_irqsave(&dwc->lock, flags);
1281
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001282 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001283 if (r == req)
1284 break;
1285 }
1286
1287 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001288 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001289 if (r == req)
1290 break;
1291 }
1292 if (r == req) {
1293 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001294 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301295 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001296 }
1297 dev_err(dwc->dev, "request %p was not queued to %s\n",
1298 request, ep->name);
1299 ret = -EINVAL;
1300 goto out0;
1301 }
1302
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301303out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001304 /* giveback the request */
1305 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1306
1307out0:
1308 spin_unlock_irqrestore(&dwc->lock, flags);
1309
1310 return ret;
1311}
1312
Felipe Balbi7a608552014-09-24 14:19:52 -05001313int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001314{
1315 struct dwc3_gadget_ep_cmd_params params;
1316 struct dwc3 *dwc = dep->dwc;
1317 int ret;
1318
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001319 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1320 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1321 return -EINVAL;
1322 }
1323
Felipe Balbi72246da2011-08-19 18:10:58 +03001324 memset(&params, 0x00, sizeof(params));
1325
1326 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001327 struct dwc3_trb *trb;
1328
1329 unsigned transfer_in_flight;
1330 unsigned started;
1331
1332 if (dep->number > 1)
1333 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1334 else
1335 trb = &dwc->ep0_trb[dep->trb_enqueue];
1336
1337 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1338 started = !list_empty(&dep->started_list);
1339
1340 if (!protocol && ((dep->direction && transfer_in_flight) ||
1341 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001342 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001343 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001344 dep->name);
1345 return -EAGAIN;
1346 }
1347
Felipe Balbi2cd47182016-04-12 16:42:43 +03001348 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1349 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001350 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001351 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 dep->name);
1353 else
1354 dep->flags |= DWC3_EP_STALL;
1355 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001356
John Youn50c763f2016-05-31 17:49:56 -07001357 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001358 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001359 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001360 dep->name);
1361 else
Alan Sterna535d812013-11-01 12:05:12 -04001362 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001364
Felipe Balbi72246da2011-08-19 18:10:58 +03001365 return ret;
1366}
1367
1368static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1369{
1370 struct dwc3_ep *dep = to_dwc3_ep(ep);
1371 struct dwc3 *dwc = dep->dwc;
1372
1373 unsigned long flags;
1374
1375 int ret;
1376
1377 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001378 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001379 spin_unlock_irqrestore(&dwc->lock, flags);
1380
1381 return ret;
1382}
1383
1384static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1385{
1386 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001387 struct dwc3 *dwc = dep->dwc;
1388 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001389 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001390
Paul Zimmerman249a4562012-02-24 17:32:16 -08001391 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001392 dep->flags |= DWC3_EP_WEDGE;
1393
Pratyush Anand08f0d962012-06-25 22:40:43 +05301394 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001395 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301396 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001397 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001398 spin_unlock_irqrestore(&dwc->lock, flags);
1399
1400 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001401}
1402
1403/* -------------------------------------------------------------------------- */
1404
1405static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1406 .bLength = USB_DT_ENDPOINT_SIZE,
1407 .bDescriptorType = USB_DT_ENDPOINT,
1408 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1409};
1410
1411static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1412 .enable = dwc3_gadget_ep0_enable,
1413 .disable = dwc3_gadget_ep0_disable,
1414 .alloc_request = dwc3_gadget_ep_alloc_request,
1415 .free_request = dwc3_gadget_ep_free_request,
1416 .queue = dwc3_gadget_ep0_queue,
1417 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301418 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001419 .set_wedge = dwc3_gadget_ep_set_wedge,
1420};
1421
1422static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1423 .enable = dwc3_gadget_ep_enable,
1424 .disable = dwc3_gadget_ep_disable,
1425 .alloc_request = dwc3_gadget_ep_alloc_request,
1426 .free_request = dwc3_gadget_ep_free_request,
1427 .queue = dwc3_gadget_ep_queue,
1428 .dequeue = dwc3_gadget_ep_dequeue,
1429 .set_halt = dwc3_gadget_ep_set_halt,
1430 .set_wedge = dwc3_gadget_ep_set_wedge,
1431};
1432
1433/* -------------------------------------------------------------------------- */
1434
1435static int dwc3_gadget_get_frame(struct usb_gadget *g)
1436{
1437 struct dwc3 *dwc = gadget_to_dwc(g);
1438 u32 reg;
1439
1440 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1441 return DWC3_DSTS_SOFFN(reg);
1442}
1443
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001444static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001445{
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001447
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001448 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001449 u32 reg;
1450
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 u8 link_state;
1452 u8 speed;
1453
Felipe Balbi72246da2011-08-19 18:10:58 +03001454 /*
1455 * According to the Databook Remote wakeup request should
1456 * be issued only when the device is in early suspend state.
1457 *
1458 * We can check that via USB Link State bits in DSTS register.
1459 */
1460 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1461
1462 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001463 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1464 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001465 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001466 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 }
1468
1469 link_state = DWC3_DSTS_USBLNKST(reg);
1470
1471 switch (link_state) {
1472 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1473 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1474 break;
1475 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001476 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001477 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001478 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001479 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 }
1481
Felipe Balbi8598bde2012-01-02 18:55:57 +02001482 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1483 if (ret < 0) {
1484 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001485 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001486 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001487
Paul Zimmerman802fde92012-04-27 13:10:52 +03001488 /* Recent versions do this automatically */
1489 if (dwc->revision < DWC3_REVISION_194A) {
1490 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001491 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001492 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1493 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1494 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001495
Paul Zimmerman1d046792012-02-15 18:56:56 -08001496 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 timeout = jiffies + msecs_to_jiffies(100);
1498
Paul Zimmerman1d046792012-02-15 18:56:56 -08001499 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1501
1502 /* in HS, means ON */
1503 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1504 break;
1505 }
1506
1507 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1508 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001509 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001510 }
1511
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001512 return 0;
1513}
1514
1515static int dwc3_gadget_wakeup(struct usb_gadget *g)
1516{
1517 struct dwc3 *dwc = gadget_to_dwc(g);
1518 unsigned long flags;
1519 int ret;
1520
1521 spin_lock_irqsave(&dwc->lock, flags);
1522 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001523 spin_unlock_irqrestore(&dwc->lock, flags);
1524
1525 return ret;
1526}
1527
1528static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1529 int is_selfpowered)
1530{
1531 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001532 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001533
Paul Zimmerman249a4562012-02-24 17:32:16 -08001534 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001535 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001536 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001537
1538 return 0;
1539}
1540
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001541static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001542{
1543 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001544 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001545
Felipe Balbifc8bb912016-05-16 13:14:48 +03001546 if (pm_runtime_suspended(dwc->dev))
1547 return 0;
1548
Felipe Balbi72246da2011-08-19 18:10:58 +03001549 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001550 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001551 if (dwc->revision <= DWC3_REVISION_187A) {
1552 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1553 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1554 }
1555
1556 if (dwc->revision >= DWC3_REVISION_194A)
1557 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1558 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001559
1560 if (dwc->has_hibernation)
1561 reg |= DWC3_DCTL_KEEP_CONNECT;
1562
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001563 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001564 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001565 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001566
1567 if (dwc->has_hibernation && !suspend)
1568 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1569
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001570 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001571 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001572
1573 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1574
1575 do {
1576 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1577 if (is_on) {
1578 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1579 break;
1580 } else {
1581 if (reg & DWC3_DSTS_DEVCTRLHLT)
1582 break;
1583 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001584 timeout--;
1585 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301586 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001587 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 } while (1);
1589
Felipe Balbi73815282015-01-27 13:48:14 -06001590 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001591 dwc->gadget_driver
1592 ? dwc->gadget_driver->function : "no-function",
1593 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301594
1595 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001596}
1597
1598static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1599{
1600 struct dwc3 *dwc = gadget_to_dwc(g);
1601 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301602 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001603
1604 is_on = !!is_on;
1605
1606 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001607 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001608 spin_unlock_irqrestore(&dwc->lock, flags);
1609
Pratyush Anand6f17f742012-07-02 10:21:55 +05301610 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001611}
1612
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001613static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1614{
1615 u32 reg;
1616
1617 /* Enable all but Start and End of Frame IRQs */
1618 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1619 DWC3_DEVTEN_EVNTOVERFLOWEN |
1620 DWC3_DEVTEN_CMDCMPLTEN |
1621 DWC3_DEVTEN_ERRTICERREN |
1622 DWC3_DEVTEN_WKUPEVTEN |
1623 DWC3_DEVTEN_ULSTCNGEN |
1624 DWC3_DEVTEN_CONNECTDONEEN |
1625 DWC3_DEVTEN_USBRSTEN |
1626 DWC3_DEVTEN_DISCONNEVTEN);
1627
1628 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1629}
1630
1631static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1632{
1633 /* mask all interrupts */
1634 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1635}
1636
1637static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001638static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001639
Felipe Balbi4e994722016-05-13 14:09:59 +03001640/**
1641 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1642 * dwc: pointer to our context structure
1643 *
1644 * The following looks like complex but it's actually very simple. In order to
1645 * calculate the number of packets we can burst at once on OUT transfers, we're
1646 * gonna use RxFIFO size.
1647 *
1648 * To calculate RxFIFO size we need two numbers:
1649 * MDWIDTH = size, in bits, of the internal memory bus
1650 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1651 *
1652 * Given these two numbers, the formula is simple:
1653 *
1654 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1655 *
1656 * 24 bytes is for 3x SETUP packets
1657 * 16 bytes is a clock domain crossing tolerance
1658 *
1659 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1660 */
1661static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1662{
1663 u32 ram2_depth;
1664 u32 mdwidth;
1665 u32 nump;
1666 u32 reg;
1667
1668 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1669 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1670
1671 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1672 nump = min_t(u32, nump, 16);
1673
1674 /* update NumP */
1675 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1676 reg &= ~DWC3_DCFG_NUMP_MASK;
1677 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1678 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1679}
1680
Felipe Balbid7be2952016-05-04 15:49:37 +03001681static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001682{
Felipe Balbi72246da2011-08-19 18:10:58 +03001683 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 int ret = 0;
1685 u32 reg;
1686
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1688 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001689
1690 /**
1691 * WORKAROUND: DWC3 revision < 2.20a have an issue
1692 * which would cause metastability state on Run/Stop
1693 * bit if we try to force the IP to USB2-only mode.
1694 *
1695 * Because of that, we cannot configure the IP to any
1696 * speed other than the SuperSpeed
1697 *
1698 * Refers to:
1699 *
1700 * STAR#9000525659: Clock Domain Crossing on DCTL in
1701 * USB 2.0 Mode
1702 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001703 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001704 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001705 } else {
1706 switch (dwc->maximum_speed) {
1707 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07001708 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001709 break;
1710 case USB_SPEED_FULL:
John Youn2da9ad72016-05-20 16:34:26 -07001711 reg |= DWC3_DCFG_FULLSPEED1;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001712 break;
1713 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07001714 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001715 break;
John Youn75808622016-02-05 17:09:13 -08001716 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07001717 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08001718 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001719 default:
John Youn77966eb2016-02-19 17:31:01 -08001720 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1721 dwc->maximum_speed);
1722 /* fall through */
1723 case USB_SPEED_SUPER:
1724 reg |= DWC3_DCFG_SUPERSPEED;
1725 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001726 }
1727 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001728 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1729
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001730 /*
1731 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1732 * field instead of letting dwc3 itself calculate that automatically.
1733 *
1734 * This way, we maximize the chances that we'll be able to get several
1735 * bursts of data without going through any sort of endpoint throttling.
1736 */
1737 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1738 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1739 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1740
Felipe Balbi4e994722016-05-13 14:09:59 +03001741 dwc3_gadget_setup_nump(dwc);
1742
Felipe Balbi72246da2011-08-19 18:10:58 +03001743 /* Start with SuperSpeed Default */
1744 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1745
1746 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001747 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1748 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 if (ret) {
1750 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001751 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 }
1753
1754 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001755 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1756 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001757 if (ret) {
1758 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001759 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 }
1761
1762 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001763 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 dwc3_ep0_out_start(dwc);
1765
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001766 dwc3_gadget_enable_irq(dwc);
1767
Felipe Balbid7be2952016-05-04 15:49:37 +03001768 return 0;
1769
1770err1:
1771 __dwc3_gadget_ep_disable(dwc->eps[0]);
1772
1773err0:
1774 return ret;
1775}
1776
1777static int dwc3_gadget_start(struct usb_gadget *g,
1778 struct usb_gadget_driver *driver)
1779{
1780 struct dwc3 *dwc = gadget_to_dwc(g);
1781 unsigned long flags;
1782 int ret = 0;
1783 int irq;
1784
1785 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1786 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1787 IRQF_SHARED, "dwc3", dwc->ev_buf);
1788 if (ret) {
1789 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1790 irq, ret);
1791 goto err0;
1792 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001793 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001794
1795 spin_lock_irqsave(&dwc->lock, flags);
1796 if (dwc->gadget_driver) {
1797 dev_err(dwc->dev, "%s is already bound to %s\n",
1798 dwc->gadget.name,
1799 dwc->gadget_driver->driver.name);
1800 ret = -EBUSY;
1801 goto err1;
1802 }
1803
1804 dwc->gadget_driver = driver;
1805
Felipe Balbifc8bb912016-05-16 13:14:48 +03001806 if (pm_runtime_active(dwc->dev))
1807 __dwc3_gadget_start(dwc);
1808
Felipe Balbi72246da2011-08-19 18:10:58 +03001809 spin_unlock_irqrestore(&dwc->lock, flags);
1810
1811 return 0;
1812
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001813err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001814 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001815 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001816
1817err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001818 return ret;
1819}
1820
Felipe Balbid7be2952016-05-04 15:49:37 +03001821static void __dwc3_gadget_stop(struct dwc3 *dwc)
1822{
1823 dwc3_gadget_disable_irq(dwc);
1824 __dwc3_gadget_ep_disable(dwc->eps[0]);
1825 __dwc3_gadget_ep_disable(dwc->eps[1]);
1826}
1827
Felipe Balbi22835b82014-10-17 12:05:12 -05001828static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001829{
1830 struct dwc3 *dwc = gadget_to_dwc(g);
1831 unsigned long flags;
1832
1833 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001834 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 spin_unlock_irqrestore(&dwc->lock, flags);
1837
Felipe Balbi3f308d12016-05-16 14:17:06 +03001838 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001839
Felipe Balbi72246da2011-08-19 18:10:58 +03001840 return 0;
1841}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001842
Felipe Balbi72246da2011-08-19 18:10:58 +03001843static const struct usb_gadget_ops dwc3_gadget_ops = {
1844 .get_frame = dwc3_gadget_get_frame,
1845 .wakeup = dwc3_gadget_wakeup,
1846 .set_selfpowered = dwc3_gadget_set_selfpowered,
1847 .pullup = dwc3_gadget_pullup,
1848 .udc_start = dwc3_gadget_start,
1849 .udc_stop = dwc3_gadget_stop,
1850};
1851
1852/* -------------------------------------------------------------------------- */
1853
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001854static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1855 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001856{
1857 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001858 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001859
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001860 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07001861 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001862
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001864 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001865 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001866
1867 dep->dwc = dwc;
1868 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001869 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001870 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001871 dwc->eps[epnum] = dep;
1872
1873 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1874 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001875
Felipe Balbi72246da2011-08-19 18:10:58 +03001876 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03001877 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001878
Felipe Balbi73815282015-01-27 13:48:14 -06001879 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001880
Felipe Balbi72246da2011-08-19 18:10:58 +03001881 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001882 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301883 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001884 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1885 if (!epnum)
1886 dwc->gadget.ep0 = &dep->endpoint;
1887 } else {
1888 int ret;
1889
Robert Baldygae117e742013-12-13 12:23:38 +01001890 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001891 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001892 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1893 list_add_tail(&dep->endpoint.ep_list,
1894 &dwc->gadget.ep_list);
1895
1896 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001897 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001898 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001899 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001900
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001901 if (epnum == 0 || epnum == 1) {
1902 dep->endpoint.caps.type_control = true;
1903 } else {
1904 dep->endpoint.caps.type_iso = true;
1905 dep->endpoint.caps.type_bulk = true;
1906 dep->endpoint.caps.type_int = true;
1907 }
1908
1909 dep->endpoint.caps.dir_in = !!direction;
1910 dep->endpoint.caps.dir_out = !direction;
1911
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001912 INIT_LIST_HEAD(&dep->pending_list);
1913 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001914 }
1915
1916 return 0;
1917}
1918
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001919static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1920{
1921 int ret;
1922
1923 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1924
1925 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1926 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001927 dwc3_trace(trace_dwc3_gadget,
1928 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001929 return ret;
1930 }
1931
1932 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1933 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001934 dwc3_trace(trace_dwc3_gadget,
1935 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001936 return ret;
1937 }
1938
1939 return 0;
1940}
1941
Felipe Balbi72246da2011-08-19 18:10:58 +03001942static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1943{
1944 struct dwc3_ep *dep;
1945 u8 epnum;
1946
1947 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1948 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001949 if (!dep)
1950 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301951 /*
1952 * Physical endpoints 0 and 1 are special; they form the
1953 * bi-directional USB endpoint 0.
1954 *
1955 * For those two physical endpoints, we don't allocate a TRB
1956 * pool nor do we add them the endpoints list. Due to that, we
1957 * shouldn't do these two operations otherwise we would end up
1958 * with all sorts of bugs when removing dwc3.ko.
1959 */
1960 if (epnum != 0 && epnum != 1) {
1961 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301963 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001964
1965 kfree(dep);
1966 }
1967}
1968
Felipe Balbi72246da2011-08-19 18:10:58 +03001969/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001970
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301971static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1972 struct dwc3_request *req, struct dwc3_trb *trb,
1973 const struct dwc3_event_depevt *event, int status)
1974{
1975 unsigned int count;
1976 unsigned int s_pkt = 0;
1977 unsigned int trb_status;
1978
Felipe Balbi68d34c82016-05-30 13:34:58 +03001979 dep->queued_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001980 trace_dwc3_complete_trb(dep, trb);
1981
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301982 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1983 /*
1984 * We continue despite the error. There is not much we
1985 * can do. If we don't clean it up we loop forever. If
1986 * we skip the TRB then it gets overwritten after a
1987 * while since we use them in a ring buffer. A BUG()
1988 * would help. Lets hope that if this occurs, someone
1989 * fixes the root cause instead of looking away :)
1990 */
1991 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1992 dep->name, trb);
1993 count = trb->size & DWC3_TRB_SIZE_MASK;
1994
1995 if (dep->direction) {
1996 if (count) {
1997 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1998 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001999 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002000 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302001 dep->name);
2002 /*
2003 * If missed isoc occurred and there is
2004 * no request queued then issue END
2005 * TRANSFER, so that core generates
2006 * next xfernotready and we will issue
2007 * a fresh START TRANSFER.
2008 * If there are still queued request
2009 * then wait, do not issue either END
2010 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002011 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302012 * giveback.If any future queued request
2013 * is successfully transferred then we
2014 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002015 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302016 */
2017 dep->flags |= DWC3_EP_MISSED_ISOC;
2018 } else {
2019 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2020 dep->name);
2021 status = -ECONNRESET;
2022 }
2023 } else {
2024 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2025 }
2026 } else {
2027 if (count && (event->status & DEPEVT_STATUS_SHORT))
2028 s_pkt = 1;
2029 }
2030
2031 /*
2032 * We assume here we will always receive the entire data block
2033 * which we should receive. Meaning, if we program RX to
2034 * receive 4K but we receive only 2K, we assume that's all we
2035 * should receive and we simply bounce the request back to the
2036 * gadget driver for further processing.
2037 */
2038 req->request.actual += req->request.length - count;
2039 if (s_pkt)
2040 return 1;
2041 if ((event->status & DEPEVT_STATUS_LST) &&
2042 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2043 DWC3_TRB_CTRL_HWO)))
2044 return 1;
2045 if ((event->status & DEPEVT_STATUS_IOC) &&
2046 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2047 return 1;
2048 return 0;
2049}
2050
Felipe Balbi72246da2011-08-19 18:10:58 +03002051static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2052 const struct dwc3_event_depevt *event, int status)
2053{
2054 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002055 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302056 unsigned int slot;
2057 unsigned int i;
2058 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002059
2060 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002061 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002062 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002063 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002064
Ville Syrjäläd115d702015-08-31 19:48:28 +03002065 i = 0;
2066 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002067 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002068 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002069 slot++;
2070 slot %= DWC3_TRB_NUM;
2071 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002072
Ville Syrjäläd115d702015-08-31 19:48:28 +03002073 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2074 event, status);
2075 if (ret)
2076 break;
2077 } while (++i < req->request.num_mapped_sgs);
2078
2079 dwc3_gadget_giveback(dep, req, status);
2080
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302081 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002083 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002084
Felipe Balbi4cb42212016-05-18 12:37:21 +03002085 /*
2086 * Our endpoint might get disabled by another thread during
2087 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2088 * early on so DWC3_EP_BUSY flag gets cleared
2089 */
2090 if (!dep->endpoint.desc)
2091 return 1;
2092
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302093 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002094 list_empty(&dep->started_list)) {
2095 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302096 /*
2097 * If there is no entry in request list then do
2098 * not issue END TRANSFER now. Just set PENDING
2099 * flag, so that END TRANSFER is issued when an
2100 * entry is added into request list.
2101 */
2102 dep->flags = DWC3_EP_PENDING_REQUEST;
2103 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002104 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302105 dep->flags = DWC3_EP_ENABLED;
2106 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302107 return 1;
2108 }
2109
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002110 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2111 if ((event->status & DEPEVT_STATUS_IOC) &&
2112 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2113 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 return 1;
2115}
2116
2117static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002118 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002119{
2120 unsigned status = 0;
2121 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002122 u32 is_xfer_complete;
2123
2124 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002125
2126 if (event->status & DEPEVT_STATUS_BUSERR)
2127 status = -ECONNRESET;
2128
Paul Zimmerman1d046792012-02-15 18:56:56 -08002129 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002130 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002131 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002132 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002133
2134 /*
2135 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2136 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2137 */
2138 if (dwc->revision < DWC3_REVISION_183A) {
2139 u32 reg;
2140 int i;
2141
2142 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002143 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002144
2145 if (!(dep->flags & DWC3_EP_ENABLED))
2146 continue;
2147
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002148 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002149 return;
2150 }
2151
2152 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2153 reg |= dwc->u1u2;
2154 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2155
2156 dwc->u1u2 = 0;
2157 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002158
Felipe Balbi4cb42212016-05-18 12:37:21 +03002159 /*
2160 * Our endpoint might get disabled by another thread during
2161 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2162 * early on so DWC3_EP_BUSY flag gets cleared
2163 */
2164 if (!dep->endpoint.desc)
2165 return;
2166
Felipe Balbie6e709b2015-09-28 15:16:56 -05002167 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002168 int ret;
2169
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002170 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002171 if (!ret || ret == -EBUSY)
2172 return;
2173 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002174}
2175
Felipe Balbi72246da2011-08-19 18:10:58 +03002176static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2177 const struct dwc3_event_depevt *event)
2178{
2179 struct dwc3_ep *dep;
2180 u8 epnum = event->endpoint_number;
2181
2182 dep = dwc->eps[epnum];
2183
Felipe Balbi3336abb2012-06-06 09:19:35 +03002184 if (!(dep->flags & DWC3_EP_ENABLED))
2185 return;
2186
Felipe Balbi72246da2011-08-19 18:10:58 +03002187 if (epnum == 0 || epnum == 1) {
2188 dwc3_ep0_interrupt(dwc, event);
2189 return;
2190 }
2191
2192 switch (event->endpoint_event) {
2193 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002194 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002195
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002196 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002197 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002198 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002199 dep->name);
2200 return;
2201 }
2202
Jingoo Han029d97f2014-07-04 15:00:51 +09002203 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002204 break;
2205 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002206 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002207 break;
2208 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002209 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002210 dwc3_gadget_start_isoc(dwc, dep, event);
2211 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002212 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002213 int ret;
2214
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002215 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2216
Felipe Balbi73815282015-01-27 13:48:14 -06002217 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002218 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002219 : "Transfer Not Active");
2220
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002221 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002222 if (!ret || ret == -EBUSY)
2223 return;
2224
Felipe Balbiec5e7952015-11-16 16:04:13 -06002225 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002226 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 dep->name);
2228 }
2229
2230 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002231 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002232 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002233 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2234 dep->name);
2235 return;
2236 }
2237
2238 switch (event->status) {
2239 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002240 dwc3_trace(trace_dwc3_gadget,
2241 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002242 event->parameters);
2243
2244 break;
2245 case DEPEVT_STREAMEVT_NOTFOUND:
2246 /* FALLTHROUGH */
2247 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002248 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002249 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002250 }
2251 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002252 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002253 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002254 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002255 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002256 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002257 break;
2258 }
2259}
2260
2261static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2262{
2263 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2264 spin_unlock(&dwc->lock);
2265 dwc->gadget_driver->disconnect(&dwc->gadget);
2266 spin_lock(&dwc->lock);
2267 }
2268}
2269
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002270static void dwc3_suspend_gadget(struct dwc3 *dwc)
2271{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002272 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002273 spin_unlock(&dwc->lock);
2274 dwc->gadget_driver->suspend(&dwc->gadget);
2275 spin_lock(&dwc->lock);
2276 }
2277}
2278
2279static void dwc3_resume_gadget(struct dwc3 *dwc)
2280{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002281 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002282 spin_unlock(&dwc->lock);
2283 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002284 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002285 }
2286}
2287
2288static void dwc3_reset_gadget(struct dwc3 *dwc)
2289{
2290 if (!dwc->gadget_driver)
2291 return;
2292
2293 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2294 spin_unlock(&dwc->lock);
2295 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002296 spin_lock(&dwc->lock);
2297 }
2298}
2299
Paul Zimmermanb992e682012-04-27 14:17:35 +03002300static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002301{
2302 struct dwc3_ep *dep;
2303 struct dwc3_gadget_ep_cmd_params params;
2304 u32 cmd;
2305 int ret;
2306
2307 dep = dwc->eps[epnum];
2308
Felipe Balbib4996a82012-06-06 12:04:13 +03002309 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302310 return;
2311
Pratyush Anand57911502012-07-06 15:19:10 +05302312 /*
2313 * NOTICE: We are violating what the Databook says about the
2314 * EndTransfer command. Ideally we would _always_ wait for the
2315 * EndTransfer Command Completion IRQ, but that's causing too
2316 * much trouble synchronizing between us and gadget driver.
2317 *
2318 * We have discussed this with the IP Provider and it was
2319 * suggested to giveback all requests here, but give HW some
2320 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002321 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302322 *
2323 * Note also that a similar handling was tested by Synopsys
2324 * (thanks a lot Paul) and nothing bad has come out of it.
2325 * In short, what we're doing is:
2326 *
2327 * - Issue EndTransfer WITH CMDIOC bit set
2328 * - Wait 100us
2329 */
2330
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302331 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002332 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2333 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002334 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302335 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002336 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302337 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002338 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002339 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302340 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002341}
2342
2343static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2344{
2345 u32 epnum;
2346
2347 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2348 struct dwc3_ep *dep;
2349
2350 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002351 if (!dep)
2352 continue;
2353
Felipe Balbi72246da2011-08-19 18:10:58 +03002354 if (!(dep->flags & DWC3_EP_ENABLED))
2355 continue;
2356
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002357 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002358 }
2359}
2360
2361static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2362{
2363 u32 epnum;
2364
2365 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2366 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002367 int ret;
2368
2369 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002370 if (!dep)
2371 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002372
2373 if (!(dep->flags & DWC3_EP_STALL))
2374 continue;
2375
2376 dep->flags &= ~DWC3_EP_STALL;
2377
John Youn50c763f2016-05-31 17:49:56 -07002378 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 WARN_ON_ONCE(ret);
2380 }
2381}
2382
2383static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2384{
Felipe Balbic4430a22012-05-24 10:30:01 +03002385 int reg;
2386
Felipe Balbi72246da2011-08-19 18:10:58 +03002387 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2388 reg &= ~DWC3_DCTL_INITU1ENA;
2389 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2390
2391 reg &= ~DWC3_DCTL_INITU2ENA;
2392 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002393
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 dwc3_disconnect_gadget(dwc);
2395
2396 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002397 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002398 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002399
2400 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002401}
2402
Felipe Balbi72246da2011-08-19 18:10:58 +03002403static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2404{
2405 u32 reg;
2406
Felipe Balbifc8bb912016-05-16 13:14:48 +03002407 dwc->connected = true;
2408
Felipe Balbidf62df52011-10-14 15:11:49 +03002409 /*
2410 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2411 * would cause a missing Disconnect Event if there's a
2412 * pending Setup Packet in the FIFO.
2413 *
2414 * There's no suggested workaround on the official Bug
2415 * report, which states that "unless the driver/application
2416 * is doing any special handling of a disconnect event,
2417 * there is no functional issue".
2418 *
2419 * Unfortunately, it turns out that we _do_ some special
2420 * handling of a disconnect event, namely complete all
2421 * pending transfers, notify gadget driver of the
2422 * disconnection, and so on.
2423 *
2424 * Our suggested workaround is to follow the Disconnect
2425 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002426 * flag. Such flag gets set whenever we have a SETUP_PENDING
2427 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002428 * same endpoint.
2429 *
2430 * Refers to:
2431 *
2432 * STAR#9000466709: RTL: Device : Disconnect event not
2433 * generated if setup packet pending in FIFO
2434 */
2435 if (dwc->revision < DWC3_REVISION_188A) {
2436 if (dwc->setup_packet_pending)
2437 dwc3_gadget_disconnect_interrupt(dwc);
2438 }
2439
Felipe Balbi8e744752014-11-06 14:27:53 +08002440 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002441
2442 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2443 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2444 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002445 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002446
2447 dwc3_stop_active_transfers(dwc);
2448 dwc3_clear_stall_all_ep(dwc);
2449
2450 /* Reset device address to zero */
2451 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2452 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2453 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002454}
2455
2456static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2457{
2458 u32 reg;
2459 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2460
2461 /*
2462 * We change the clock only at SS but I dunno why I would want to do
2463 * this. Maybe it becomes part of the power saving plan.
2464 */
2465
John Younee5cd412016-02-05 17:08:45 -08002466 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2467 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002468 return;
2469
2470 /*
2471 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2472 * each time on Connect Done.
2473 */
2474 if (!usb30_clock)
2475 return;
2476
2477 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2478 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2479 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2480}
2481
Felipe Balbi72246da2011-08-19 18:10:58 +03002482static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2483{
Felipe Balbi72246da2011-08-19 18:10:58 +03002484 struct dwc3_ep *dep;
2485 int ret;
2486 u32 reg;
2487 u8 speed;
2488
Felipe Balbi72246da2011-08-19 18:10:58 +03002489 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2490 speed = reg & DWC3_DSTS_CONNECTSPD;
2491 dwc->speed = speed;
2492
2493 dwc3_update_ram_clk_sel(dwc, speed);
2494
2495 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002496 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002497 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2498 dwc->gadget.ep0->maxpacket = 512;
2499 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2500 break;
John Youn2da9ad72016-05-20 16:34:26 -07002501 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002502 /*
2503 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2504 * would cause a missing USB3 Reset event.
2505 *
2506 * In such situations, we should force a USB3 Reset
2507 * event by calling our dwc3_gadget_reset_interrupt()
2508 * routine.
2509 *
2510 * Refers to:
2511 *
2512 * STAR#9000483510: RTL: SS : USB3 reset event may
2513 * not be generated always when the link enters poll
2514 */
2515 if (dwc->revision < DWC3_REVISION_190A)
2516 dwc3_gadget_reset_interrupt(dwc);
2517
Felipe Balbi72246da2011-08-19 18:10:58 +03002518 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2519 dwc->gadget.ep0->maxpacket = 512;
2520 dwc->gadget.speed = USB_SPEED_SUPER;
2521 break;
John Youn2da9ad72016-05-20 16:34:26 -07002522 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002523 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2524 dwc->gadget.ep0->maxpacket = 64;
2525 dwc->gadget.speed = USB_SPEED_HIGH;
2526 break;
John Youn2da9ad72016-05-20 16:34:26 -07002527 case DWC3_DSTS_FULLSPEED2:
2528 case DWC3_DSTS_FULLSPEED1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002529 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2530 dwc->gadget.ep0->maxpacket = 64;
2531 dwc->gadget.speed = USB_SPEED_FULL;
2532 break;
John Youn2da9ad72016-05-20 16:34:26 -07002533 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002534 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2535 dwc->gadget.ep0->maxpacket = 8;
2536 dwc->gadget.speed = USB_SPEED_LOW;
2537 break;
2538 }
2539
Pratyush Anand2b758352013-01-14 15:59:31 +05302540 /* Enable USB2 LPM Capability */
2541
John Younee5cd412016-02-05 17:08:45 -08002542 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002543 (speed != DWC3_DSTS_SUPERSPEED) &&
2544 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302545 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2546 reg |= DWC3_DCFG_LPM_CAP;
2547 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2548
2549 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2550 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2551
Huang Rui460d0982014-10-31 11:11:18 +08002552 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302553
Huang Rui80caf7d2014-10-28 19:54:26 +08002554 /*
2555 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2556 * DCFG.LPMCap is set, core responses with an ACK and the
2557 * BESL value in the LPM token is less than or equal to LPM
2558 * NYET threshold.
2559 */
2560 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2561 && dwc->has_lpm_erratum,
2562 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2563
2564 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2565 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2566
Pratyush Anand2b758352013-01-14 15:59:31 +05302567 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002568 } else {
2569 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2570 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2571 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302572 }
2573
Felipe Balbi72246da2011-08-19 18:10:58 +03002574 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002575 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2576 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002577 if (ret) {
2578 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2579 return;
2580 }
2581
2582 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002583 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2584 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002585 if (ret) {
2586 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2587 return;
2588 }
2589
2590 /*
2591 * Configure PHY via GUSB3PIPECTLn if required.
2592 *
2593 * Update GTXFIFOSIZn
2594 *
2595 * In both cases reset values should be sufficient.
2596 */
2597}
2598
2599static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2600{
Felipe Balbi72246da2011-08-19 18:10:58 +03002601 /*
2602 * TODO take core out of low power mode when that's
2603 * implemented.
2604 */
2605
Jiebing Liad14d4e2014-12-11 13:26:29 +08002606 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2607 spin_unlock(&dwc->lock);
2608 dwc->gadget_driver->resume(&dwc->gadget);
2609 spin_lock(&dwc->lock);
2610 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002611}
2612
2613static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2614 unsigned int evtinfo)
2615{
Felipe Balbifae2b902011-10-14 13:00:30 +03002616 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002617 unsigned int pwropt;
2618
2619 /*
2620 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2621 * Hibernation mode enabled which would show up when device detects
2622 * host-initiated U3 exit.
2623 *
2624 * In that case, device will generate a Link State Change Interrupt
2625 * from U3 to RESUME which is only necessary if Hibernation is
2626 * configured in.
2627 *
2628 * There are no functional changes due to such spurious event and we
2629 * just need to ignore it.
2630 *
2631 * Refers to:
2632 *
2633 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2634 * operational mode
2635 */
2636 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2637 if ((dwc->revision < DWC3_REVISION_250A) &&
2638 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2639 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2640 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002641 dwc3_trace(trace_dwc3_gadget,
2642 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002643 return;
2644 }
2645 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002646
2647 /*
2648 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2649 * on the link partner, the USB session might do multiple entry/exit
2650 * of low power states before a transfer takes place.
2651 *
2652 * Due to this problem, we might experience lower throughput. The
2653 * suggested workaround is to disable DCTL[12:9] bits if we're
2654 * transitioning from U1/U2 to U0 and enable those bits again
2655 * after a transfer completes and there are no pending transfers
2656 * on any of the enabled endpoints.
2657 *
2658 * This is the first half of that workaround.
2659 *
2660 * Refers to:
2661 *
2662 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2663 * core send LGO_Ux entering U0
2664 */
2665 if (dwc->revision < DWC3_REVISION_183A) {
2666 if (next == DWC3_LINK_STATE_U0) {
2667 u32 u1u2;
2668 u32 reg;
2669
2670 switch (dwc->link_state) {
2671 case DWC3_LINK_STATE_U1:
2672 case DWC3_LINK_STATE_U2:
2673 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2674 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2675 | DWC3_DCTL_ACCEPTU2ENA
2676 | DWC3_DCTL_INITU1ENA
2677 | DWC3_DCTL_ACCEPTU1ENA);
2678
2679 if (!dwc->u1u2)
2680 dwc->u1u2 = reg & u1u2;
2681
2682 reg &= ~u1u2;
2683
2684 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2685 break;
2686 default:
2687 /* do nothing */
2688 break;
2689 }
2690 }
2691 }
2692
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002693 switch (next) {
2694 case DWC3_LINK_STATE_U1:
2695 if (dwc->speed == USB_SPEED_SUPER)
2696 dwc3_suspend_gadget(dwc);
2697 break;
2698 case DWC3_LINK_STATE_U2:
2699 case DWC3_LINK_STATE_U3:
2700 dwc3_suspend_gadget(dwc);
2701 break;
2702 case DWC3_LINK_STATE_RESUME:
2703 dwc3_resume_gadget(dwc);
2704 break;
2705 default:
2706 /* do nothing */
2707 break;
2708 }
2709
Felipe Balbie57ebc12014-04-22 13:20:12 -05002710 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002711}
2712
Felipe Balbie1dadd32014-02-25 14:47:54 -06002713static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2714 unsigned int evtinfo)
2715{
2716 unsigned int is_ss = evtinfo & BIT(4);
2717
2718 /**
2719 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2720 * have a known issue which can cause USB CV TD.9.23 to fail
2721 * randomly.
2722 *
2723 * Because of this issue, core could generate bogus hibernation
2724 * events which SW needs to ignore.
2725 *
2726 * Refers to:
2727 *
2728 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2729 * Device Fallback from SuperSpeed
2730 */
2731 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2732 return;
2733
2734 /* enter hibernation here */
2735}
2736
Felipe Balbi72246da2011-08-19 18:10:58 +03002737static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2738 const struct dwc3_event_devt *event)
2739{
2740 switch (event->type) {
2741 case DWC3_DEVICE_EVENT_DISCONNECT:
2742 dwc3_gadget_disconnect_interrupt(dwc);
2743 break;
2744 case DWC3_DEVICE_EVENT_RESET:
2745 dwc3_gadget_reset_interrupt(dwc);
2746 break;
2747 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2748 dwc3_gadget_conndone_interrupt(dwc);
2749 break;
2750 case DWC3_DEVICE_EVENT_WAKEUP:
2751 dwc3_gadget_wakeup_interrupt(dwc);
2752 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002753 case DWC3_DEVICE_EVENT_HIBER_REQ:
2754 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2755 "unexpected hibernation event\n"))
2756 break;
2757
2758 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2759 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2761 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2762 break;
2763 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002764 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002765 break;
2766 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002767 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002768 break;
2769 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002770 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002771 break;
2772 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002773 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002774 break;
2775 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002776 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002777 break;
2778 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002779 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002780 }
2781}
2782
2783static void dwc3_process_event_entry(struct dwc3 *dwc,
2784 const union dwc3_event *event)
2785{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002786 trace_dwc3_event(event->raw);
2787
Felipe Balbi72246da2011-08-19 18:10:58 +03002788 /* Endpoint IRQ, handle it and return early */
2789 if (event->type.is_devspec == 0) {
2790 /* depevt */
2791 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2792 }
2793
2794 switch (event->type.type) {
2795 case DWC3_EVENT_TYPE_DEV:
2796 dwc3_gadget_interrupt(dwc, &event->devt);
2797 break;
2798 /* REVISIT what to do with Carkit and I2C events ? */
2799 default:
2800 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2801 }
2802}
2803
Felipe Balbidea520a2016-03-30 09:39:34 +03002804static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002805{
Felipe Balbidea520a2016-03-30 09:39:34 +03002806 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002807 irqreturn_t ret = IRQ_NONE;
2808 int left;
2809 u32 reg;
2810
Felipe Balbif42f2442013-06-12 21:25:08 +03002811 left = evt->count;
2812
2813 if (!(evt->flags & DWC3_EVENT_PENDING))
2814 return IRQ_NONE;
2815
2816 while (left > 0) {
2817 union dwc3_event event;
2818
2819 event.raw = *(u32 *) (evt->buf + evt->lpos);
2820
2821 dwc3_process_event_entry(dwc, &event);
2822
2823 /*
2824 * FIXME we wrap around correctly to the next entry as
2825 * almost all entries are 4 bytes in size. There is one
2826 * entry which has 12 bytes which is a regular entry
2827 * followed by 8 bytes data. ATM I don't know how
2828 * things are organized if we get next to the a
2829 * boundary so I worry about that once we try to handle
2830 * that.
2831 */
2832 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2833 left -= 4;
2834
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002835 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002836 }
2837
2838 evt->count = 0;
2839 evt->flags &= ~DWC3_EVENT_PENDING;
2840 ret = IRQ_HANDLED;
2841
2842 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002843 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002844 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002845 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002846
2847 return ret;
2848}
2849
Felipe Balbidea520a2016-03-30 09:39:34 +03002850static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002851{
Felipe Balbidea520a2016-03-30 09:39:34 +03002852 struct dwc3_event_buffer *evt = _evt;
2853 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002854 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002855 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002856
Felipe Balbie5f68b42015-10-12 13:25:44 -05002857 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002858 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002859 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002860
2861 return ret;
2862}
2863
Felipe Balbidea520a2016-03-30 09:39:34 +03002864static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002865{
Felipe Balbidea520a2016-03-30 09:39:34 +03002866 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002867 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002868 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002869
Felipe Balbifc8bb912016-05-16 13:14:48 +03002870 if (pm_runtime_suspended(dwc->dev)) {
2871 pm_runtime_get(dwc->dev);
2872 disable_irq_nosync(dwc->irq_gadget);
2873 dwc->pending_events = true;
2874 return IRQ_HANDLED;
2875 }
2876
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002877 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 count &= DWC3_GEVNTCOUNT_MASK;
2879 if (!count)
2880 return IRQ_NONE;
2881
Felipe Balbib15a7622011-06-30 16:57:15 +03002882 evt->count = count;
2883 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002884
Felipe Balbie8adfc32013-06-12 21:11:14 +03002885 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002886 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002887 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002888 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002889
Felipe Balbib15a7622011-06-30 16:57:15 +03002890 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002891}
2892
Felipe Balbidea520a2016-03-30 09:39:34 +03002893static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002894{
Felipe Balbidea520a2016-03-30 09:39:34 +03002895 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002896
Felipe Balbidea520a2016-03-30 09:39:34 +03002897 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002898}
2899
2900/**
2901 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002902 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002903 *
2904 * Returns 0 on success otherwise negative errno.
2905 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002906int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002907{
Felipe Balbi72246da2011-08-19 18:10:58 +03002908 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002909
2910 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2911 &dwc->ctrl_req_addr, GFP_KERNEL);
2912 if (!dwc->ctrl_req) {
2913 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2914 ret = -ENOMEM;
2915 goto err0;
2916 }
2917
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302918 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002919 &dwc->ep0_trb_addr, GFP_KERNEL);
2920 if (!dwc->ep0_trb) {
2921 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2922 ret = -ENOMEM;
2923 goto err1;
2924 }
2925
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002926 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002927 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002928 ret = -ENOMEM;
2929 goto err2;
2930 }
2931
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002932 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002933 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2934 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002935 if (!dwc->ep0_bounce) {
2936 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2937 ret = -ENOMEM;
2938 goto err3;
2939 }
2940
Felipe Balbi04c03d12015-12-02 10:06:45 -06002941 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2942 if (!dwc->zlp_buf) {
2943 ret = -ENOMEM;
2944 goto err4;
2945 }
2946
Felipe Balbi72246da2011-08-19 18:10:58 +03002947 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002948 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002949 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002950 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002951 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002952
2953 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002954 * FIXME We might be setting max_speed to <SUPER, however versions
2955 * <2.20a of dwc3 have an issue with metastability (documented
2956 * elsewhere in this driver) which tells us we can't set max speed to
2957 * anything lower than SUPER.
2958 *
2959 * Because gadget.max_speed is only used by composite.c and function
2960 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2961 * to happen so we avoid sending SuperSpeed Capability descriptor
2962 * together with our BOS descriptor as that could confuse host into
2963 * thinking we can handle super speed.
2964 *
2965 * Note that, in fact, we won't even support GetBOS requests when speed
2966 * is less than super speed because we don't have means, yet, to tell
2967 * composite.c that we are USB 2.0 + LPM ECN.
2968 */
2969 if (dwc->revision < DWC3_REVISION_220A)
2970 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002971 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002972 dwc->revision);
2973
2974 dwc->gadget.max_speed = dwc->maximum_speed;
2975
2976 /*
David Cohena4b9d942013-12-09 15:55:38 -08002977 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2978 * on ep out.
2979 */
2980 dwc->gadget.quirk_ep_out_aligned_size = true;
2981
2982 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002983 * REVISIT: Here we should clear all pending IRQs to be
2984 * sure we're starting from a well known location.
2985 */
2986
2987 ret = dwc3_gadget_init_endpoints(dwc);
2988 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002989 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002990
Felipe Balbi72246da2011-08-19 18:10:58 +03002991 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2992 if (ret) {
2993 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002994 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002995 }
2996
2997 return 0;
2998
Felipe Balbi04c03d12015-12-02 10:06:45 -06002999err5:
3000 kfree(dwc->zlp_buf);
3001
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003002err4:
David Cohene1f80462013-09-11 17:42:47 -07003003 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003004 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3005 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003006
Felipe Balbi72246da2011-08-19 18:10:58 +03003007err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003008 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003009
3010err2:
3011 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3012 dwc->ep0_trb, dwc->ep0_trb_addr);
3013
3014err1:
3015 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3016 dwc->ctrl_req, dwc->ctrl_req_addr);
3017
3018err0:
3019 return ret;
3020}
3021
Felipe Balbi7415f172012-04-30 14:56:33 +03003022/* -------------------------------------------------------------------------- */
3023
Felipe Balbi72246da2011-08-19 18:10:58 +03003024void dwc3_gadget_exit(struct dwc3 *dwc)
3025{
Felipe Balbi72246da2011-08-19 18:10:58 +03003026 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003027
Felipe Balbi72246da2011-08-19 18:10:58 +03003028 dwc3_gadget_free_endpoints(dwc);
3029
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003030 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3031 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003032
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003033 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003034 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003035
3036 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3037 dwc->ep0_trb, dwc->ep0_trb_addr);
3038
3039 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3040 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003041}
Felipe Balbi7415f172012-04-30 14:56:33 +03003042
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003043int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003044{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003045 int ret;
3046
Roger Quadros9772b472016-04-12 11:33:29 +03003047 if (!dwc->gadget_driver)
3048 return 0;
3049
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003050 ret = dwc3_gadget_run_stop(dwc, false, false);
3051 if (ret < 0)
3052 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003053
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003054 dwc3_disconnect_gadget(dwc);
3055 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003056
3057 return 0;
3058}
3059
3060int dwc3_gadget_resume(struct dwc3 *dwc)
3061{
Felipe Balbi7415f172012-04-30 14:56:33 +03003062 int ret;
3063
Roger Quadros9772b472016-04-12 11:33:29 +03003064 if (!dwc->gadget_driver)
3065 return 0;
3066
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003067 ret = __dwc3_gadget_start(dwc);
3068 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003069 goto err0;
3070
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003071 ret = dwc3_gadget_run_stop(dwc, true, false);
3072 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003073 goto err1;
3074
Felipe Balbi7415f172012-04-30 14:56:33 +03003075 return 0;
3076
3077err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003078 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003079
3080err0:
3081 return ret;
3082}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003083
3084void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3085{
3086 if (dwc->pending_events) {
3087 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3088 dwc->pending_events = false;
3089 enable_irq(dwc->irq_gadget);
3090 }
3091}