blob: 64d01ff8c1193cfbc307672c5bc21d3ca0270857 [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;
177
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300178 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300179 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200180 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300181
182 if (req->request.status == -EINPROGRESS)
183 req->request.status = status;
184
Pratyush Anand0416e492012-08-10 13:42:16 +0530185 if (dwc->ep0_bounced && dep->number == 0)
186 dwc->ep0_bounced = false;
187 else
188 usb_gadget_unmap_request(&dwc->gadget, &req->request,
189 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300190
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500191 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300192
193 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200194 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300195 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300196
197 if (dep->number > 1)
198 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300199}
200
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500201int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300202{
203 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300204 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300205 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300206 u32 reg;
207
208 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
209 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
210
211 do {
212 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
213 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300214 status = DWC3_DGCMD_STATUS(reg);
215 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300216 ret = -EINVAL;
217 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300218 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300219 } while (timeout--);
220
221 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300222 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300223 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300224 }
225
Felipe Balbi71f7e702016-05-23 14:16:19 +0300226 trace_dwc3_gadget_generic_cmd(cmd, param, status);
227
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300228 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300229}
230
Felipe Balbic36d8e92016-04-04 12:46:33 +0300231static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
232
Felipe Balbi2cd47182016-04-12 16:42:43 +0300233int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
234 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300235{
Felipe Balbi8897a762016-09-22 10:56:08 +0300236 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300237 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200238 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300239 u32 reg;
240
Felipe Balbi0933df12016-05-23 14:02:33 +0300241 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300242 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300243 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300244
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300245 /*
246 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
247 * we're issuing an endpoint command, we must check if
248 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
249 *
250 * We will also set SUSPHY bit to what it was before returning as stated
251 * by the same section on Synopsys databook.
252 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300253 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
254 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
255 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
256 susphy = true;
257 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
258 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
259 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300260 }
261
Felipe Balbi59999142016-09-22 12:25:28 +0300262 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300263 int needs_wakeup;
264
265 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
266 dwc->link_state == DWC3_LINK_STATE_U2 ||
267 dwc->link_state == DWC3_LINK_STATE_U3);
268
269 if (unlikely(needs_wakeup)) {
270 ret = __dwc3_gadget_wakeup(dwc);
271 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
272 ret);
273 }
274 }
275
Felipe Balbi2eb88012016-04-12 16:53:39 +0300276 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
277 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
278 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300279
Felipe Balbi8897a762016-09-22 10:56:08 +0300280 /*
281 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
282 * not relying on XferNotReady, we can make use of a special "No
283 * Response Update Transfer" command where we should clear both CmdAct
284 * and CmdIOC bits.
285 *
286 * With this, we don't need to wait for command completion and can
287 * straight away issue further commands to the endpoint.
288 *
289 * NOTICE: We're making an assumption that control endpoints will never
290 * make use of Update Transfer command. This is a safe assumption
291 * because we can never have more than one request at a time with
292 * Control Endpoints. If anybody changes that assumption, this chunk
293 * needs to be updated accordingly.
294 */
295 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
296 !usb_endpoint_xfer_isoc(desc))
297 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
298 else
299 cmd |= DWC3_DEPCMD_CMDACT;
300
301 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300303 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300304 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300305 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000306
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000307 switch (cmd_status) {
308 case 0:
309 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300310 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000311 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000312 ret = -EINVAL;
313 break;
314 case DEPEVT_TRANSFER_BUS_EXPIRY:
315 /*
316 * SW issues START TRANSFER command to
317 * isochronous ep with future frame interval. If
318 * future interval time has already passed when
319 * core receives the command, it will respond
320 * with an error status of 'Bus Expiry'.
321 *
322 * Instead of always returning -EINVAL, let's
323 * give a hint to the gadget driver that this is
324 * the case by returning -EAGAIN.
325 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000326 ret = -EAGAIN;
327 break;
328 default:
329 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
330 }
331
Felipe Balbic0ca3242016-04-04 09:11:51 +0300332 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300334 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335
Felipe Balbif6bb2252016-05-23 13:53:34 +0300336 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300337 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300338 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300339 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300340
Felipe Balbi0933df12016-05-23 14:02:33 +0300341 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
342
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300343 if (unlikely(susphy)) {
344 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
345 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
346 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
347 }
348
Felipe Balbic0ca3242016-04-04 09:11:51 +0300349 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300350}
351
John Youn50c763f2016-05-31 17:49:56 -0700352static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
353{
354 struct dwc3 *dwc = dep->dwc;
355 struct dwc3_gadget_ep_cmd_params params;
356 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
357
358 /*
359 * As of core revision 2.60a the recommended programming model
360 * is to set the ClearPendIN bit when issuing a Clear Stall EP
361 * command for IN endpoints. This is to prevent an issue where
362 * some (non-compliant) hosts may not send ACK TPs for pending
363 * IN transfers due to a mishandled error condition. Synopsys
364 * STAR 9000614252.
365 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800366 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
367 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700368 cmd |= DWC3_DEPCMD_CLEARPENDIN;
369
370 memset(&params, 0, sizeof(params));
371
Felipe Balbi2cd47182016-04-12 16:42:43 +0300372 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700373}
374
Felipe Balbi72246da2011-08-19 18:10:58 +0300375static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200376 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300377{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300378 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379
380 return dep->trb_pool_dma + offset;
381}
382
383static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
384{
385 struct dwc3 *dwc = dep->dwc;
386
387 if (dep->trb_pool)
388 return 0;
389
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 dep->trb_pool = dma_alloc_coherent(dwc->dev,
391 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
392 &dep->trb_pool_dma, GFP_KERNEL);
393 if (!dep->trb_pool) {
394 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
395 dep->name);
396 return -ENOMEM;
397 }
398
399 return 0;
400}
401
402static void dwc3_free_trb_pool(struct dwc3_ep *dep)
403{
404 struct dwc3 *dwc = dep->dwc;
405
406 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
407 dep->trb_pool, dep->trb_pool_dma);
408
409 dep->trb_pool = NULL;
410 dep->trb_pool_dma = 0;
411}
412
John Younc4509602016-02-16 20:10:53 -0800413static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
414
415/**
416 * dwc3_gadget_start_config - Configure EP resources
417 * @dwc: pointer to our controller context structure
418 * @dep: endpoint that is being enabled
419 *
420 * The assignment of transfer resources cannot perfectly follow the
421 * data book due to the fact that the controller driver does not have
422 * all knowledge of the configuration in advance. It is given this
423 * information piecemeal by the composite gadget framework after every
424 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
425 * programming model in this scenario can cause errors. For two
426 * reasons:
427 *
428 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
429 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
430 * multiple interfaces.
431 *
432 * 2) The databook does not mention doing more DEPXFERCFG for new
433 * endpoint on alt setting (8.1.6).
434 *
435 * The following simplified method is used instead:
436 *
437 * All hardware endpoints can be assigned a transfer resource and this
438 * setting will stay persistent until either a core reset or
439 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
440 * do DEPXFERCFG for every hardware endpoint as well. We are
441 * guaranteed that there are as many transfer resources as endpoints.
442 *
443 * This function is called for each endpoint when it is being enabled
444 * but is triggered only when called for EP0-out, which always happens
445 * first, and which should only happen in one of the above conditions.
446 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300447static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
448{
449 struct dwc3_gadget_ep_cmd_params params;
450 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800451 int i;
452 int ret;
453
454 if (dep->number)
455 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
457 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800458 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300459
Felipe Balbi2cd47182016-04-12 16:42:43 +0300460 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800461 if (ret)
462 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300463
John Younc4509602016-02-16 20:10:53 -0800464 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
465 struct dwc3_ep *dep = dwc->eps[i];
466
467 if (!dep)
468 continue;
469
470 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
471 if (ret)
472 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
474
475 return 0;
476}
477
478static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200479 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300480 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300481 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300482{
483 struct dwc3_gadget_ep_cmd_params params;
484
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300485 if (dev_WARN_ONCE(dwc->dev, modify && restore,
486 "Can't modify and restore\n"))
487 return -EINVAL;
488
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 memset(&params, 0x00, sizeof(params));
490
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300491 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900492 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
493
494 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800495 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300496 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300497 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900498 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300500 if (modify) {
501 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
502 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600503 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
504 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300505 } else {
506 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600507 }
508
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300509 if (usb_endpoint_xfer_control(desc))
510 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300511
512 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
513 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200515 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300516 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
517 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300518 dep->stream_capable = true;
519 }
520
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500521 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300522 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 /*
525 * We are doing 1:1 mapping for endpoints, meaning
526 * Physical Endpoints 2 maps to Logical Endpoint 2 and
527 * so on. We consider the direction bit as part of the physical
528 * endpoint number. So USB endpoint 0x81 is 0x03.
529 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300530 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300531
532 /*
533 * We must use the lower 16 TX FIFOs even though
534 * HW might have more
535 */
536 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300537 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300538
539 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300540 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 dep->interval = 1 << (desc->bInterval - 1);
542 }
543
Felipe Balbi2cd47182016-04-12 16:42:43 +0300544 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300545}
546
547static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
548{
549 struct dwc3_gadget_ep_cmd_params params;
550
551 memset(&params, 0x00, sizeof(params));
552
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300553 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
Felipe Balbi2cd47182016-04-12 16:42:43 +0300555 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
556 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300557}
558
559/**
560 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
561 * @dep: endpoint to be initialized
562 * @desc: USB Endpoint Descriptor
563 *
564 * Caller should take care of locking
565 */
566static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200567 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300568 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300569 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300570{
571 struct dwc3 *dwc = dep->dwc;
572 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300573 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300574
Felipe Balbi73815282015-01-27 13:48:14 -0600575 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300576
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 if (!(dep->flags & DWC3_EP_ENABLED)) {
578 ret = dwc3_gadget_start_config(dwc, dep);
579 if (ret)
580 return ret;
581 }
582
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300583 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600584 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 if (ret)
586 return ret;
587
588 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200589 struct dwc3_trb *trb_st_hw;
590 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200592 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200593 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 dep->type = usb_endpoint_type(desc);
595 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800596 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300597
598 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
599 reg |= DWC3_DALEPENA_EP(dep->number);
600 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
601
Baolin Wang76a638f2016-10-31 19:38:36 +0800602 init_waitqueue_head(&dep->wait_end_transfer);
603
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300604 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300605 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
John Youn0d257442016-05-19 17:26:08 -0700607 /* Initialize the TRB ring */
608 dep->trb_dequeue = 0;
609 dep->trb_enqueue = 0;
610 memset(dep->trb_pool, 0,
611 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
612
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300613 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 trb_st_hw = &dep->trb_pool[0];
615
Felipe Balbif6bafc62012-02-06 11:04:53 +0200616 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200617 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
618 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
619 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
620 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 }
622
Felipe Balbia97ea992016-09-29 16:28:56 +0300623 /*
624 * Issue StartTransfer here with no-op TRB so we can always rely on No
625 * Response Update Transfer command.
626 */
627 if (usb_endpoint_xfer_bulk(desc)) {
628 struct dwc3_gadget_ep_cmd_params params;
629 struct dwc3_trb *trb;
630 dma_addr_t trb_dma;
631 u32 cmd;
632
633 memset(&params, 0, sizeof(params));
634 trb = &dep->trb_pool[0];
635 trb_dma = dwc3_trb_dma_offset(dep, trb);
636
637 params.param0 = upper_32_bits(trb_dma);
638 params.param1 = lower_32_bits(trb_dma);
639
640 cmd = DWC3_DEPCMD_STARTTRANSFER;
641
642 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
643 if (ret < 0)
644 return ret;
645
646 dep->flags |= DWC3_EP_BUSY;
647
648 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
649 WARN_ON_ONCE(!dep->resource_index);
650 }
651
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 return 0;
653}
654
Paul Zimmermanb992e682012-04-27 14:17:35 +0300655static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200656static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300657{
658 struct dwc3_request *req;
659
Felipe Balbi0e146022016-06-21 10:32:02 +0300660 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300661
Felipe Balbi0e146022016-06-21 10:32:02 +0300662 /* - giveback all requests to gadget driver */
663 while (!list_empty(&dep->started_list)) {
664 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200665
Felipe Balbi0e146022016-06-21 10:32:02 +0300666 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200667 }
668
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200669 while (!list_empty(&dep->pending_list)) {
670 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300671
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200672 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300674}
675
676/**
677 * __dwc3_gadget_ep_disable - Disables a HW endpoint
678 * @dep: the endpoint to disable
679 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200680 * This function also removes requests which are currently processed ny the
681 * hardware and those which are not yet scheduled.
682 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300684static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
685{
686 struct dwc3 *dwc = dep->dwc;
687 u32 reg;
688
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500689 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
690
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200691 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300692
Felipe Balbi687ef982014-04-16 10:30:33 -0500693 /* make sure HW endpoint isn't stalled */
694 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500695 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500696
Felipe Balbi72246da2011-08-19 18:10:58 +0300697 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
698 reg &= ~DWC3_DALEPENA_EP(dep->number);
699 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
700
Felipe Balbi879631a2011-09-30 10:58:47 +0300701 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200702 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200703 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800705 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300706
707 return 0;
708}
709
710/* -------------------------------------------------------------------------- */
711
712static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
713 const struct usb_endpoint_descriptor *desc)
714{
715 return -EINVAL;
716}
717
718static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
719{
720 return -EINVAL;
721}
722
723/* -------------------------------------------------------------------------- */
724
725static int dwc3_gadget_ep_enable(struct usb_ep *ep,
726 const struct usb_endpoint_descriptor *desc)
727{
728 struct dwc3_ep *dep;
729 struct dwc3 *dwc;
730 unsigned long flags;
731 int ret;
732
733 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
734 pr_debug("dwc3: invalid parameters\n");
735 return -EINVAL;
736 }
737
738 if (!desc->wMaxPacketSize) {
739 pr_debug("dwc3: missing wMaxPacketSize\n");
740 return -EINVAL;
741 }
742
743 dep = to_dwc3_ep(ep);
744 dwc = dep->dwc;
745
Felipe Balbi95ca9612015-12-10 13:08:20 -0600746 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
747 "%s is already enabled\n",
748 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300749 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600752 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 spin_unlock_irqrestore(&dwc->lock, flags);
754
755 return ret;
756}
757
758static int dwc3_gadget_ep_disable(struct usb_ep *ep)
759{
760 struct dwc3_ep *dep;
761 struct dwc3 *dwc;
762 unsigned long flags;
763 int ret;
764
765 if (!ep) {
766 pr_debug("dwc3: invalid parameters\n");
767 return -EINVAL;
768 }
769
770 dep = to_dwc3_ep(ep);
771 dwc = dep->dwc;
772
Felipe Balbi95ca9612015-12-10 13:08:20 -0600773 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
774 "%s is already disabled\n",
775 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300776 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300777
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 spin_lock_irqsave(&dwc->lock, flags);
779 ret = __dwc3_gadget_ep_disable(dep);
780 spin_unlock_irqrestore(&dwc->lock, flags);
781
782 return ret;
783}
784
785static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
786 gfp_t gfp_flags)
787{
788 struct dwc3_request *req;
789 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300790
791 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900792 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300793 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300794
795 req->epnum = dep->number;
796 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300797
Felipe Balbi68d34c82016-05-30 13:34:58 +0300798 dep->allocated_requests++;
799
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500800 trace_dwc3_alloc_request(req);
801
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 return &req->request;
803}
804
805static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
806 struct usb_request *request)
807{
808 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300809 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300810
Felipe Balbi68d34c82016-05-30 13:34:58 +0300811 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500812 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300813 kfree(req);
814}
815
Felipe Balbi2c78c022016-08-12 13:13:10 +0300816static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
817
Felipe Balbic71fc372011-11-22 11:37:34 +0200818/**
819 * dwc3_prepare_one_trb - setup one TRB from one request
820 * @dep: endpoint for which this request is prepared
821 * @req: dwc3_request pointer
822 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200823static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200824 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300825 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200826{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200827 struct dwc3_trb *trb;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300828 struct dwc3 *dwc = dep->dwc;
829 struct usb_gadget *gadget = &dwc->gadget;
830 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200831
Felipe Balbi4faf7552016-04-05 13:14:31 +0300832 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200833
Felipe Balbieeb720f2011-11-28 12:46:59 +0200834 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200835 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200836 req->trb = trb;
837 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300838 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200839 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200840
Felipe Balbief966b92016-04-05 13:09:51 +0300841 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530842
Felipe Balbif6bafc62012-02-06 11:04:53 +0200843 trb->size = DWC3_TRB_SIZE_LENGTH(length);
844 trb->bpl = lower_32_bits(dma);
845 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200846
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200847 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200848 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200849 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200850 break;
851
852 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300853 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530854 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300855
856 if (speed == USB_SPEED_HIGH) {
857 struct usb_ep *ep = &dep->endpoint;
858 trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
859 }
860 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530861 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300862 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200863
864 /* always enable Interrupt on Missed ISOC */
865 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200866 break;
867
868 case USB_ENDPOINT_XFER_BULK:
869 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200870 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200871 break;
872 default:
873 /*
874 * This is only possible with faulty memory because we
875 * checked it already :)
876 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300877 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
878 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200879 }
880
Felipe Balbica4d44e2016-03-10 13:53:27 +0200881 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300882 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300883 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600884
Felipe Balbic9508c82016-10-05 14:26:23 +0300885 if (req->request.short_not_ok)
886 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
887 }
888
Felipe Balbi2c78c022016-08-12 13:13:10 +0300889 if ((!req->request.no_interrupt && !chain) ||
890 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300891 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200892
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530893 if (chain)
894 trb->ctrl |= DWC3_TRB_CTRL_CHN;
895
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200896 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200897 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
898
899 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500900
901 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200902}
903
John Youn361572b2016-05-19 17:26:17 -0700904/**
905 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
906 * @dep: The endpoint with the TRB ring
907 * @index: The index of the current TRB in the ring
908 *
909 * Returns the TRB prior to the one pointed to by the index. If the
910 * index is 0, we will wrap backwards, skip the link TRB, and return
911 * the one just before that.
912 */
913static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
914{
Felipe Balbi45438a02016-08-11 12:26:59 +0300915 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -0700916
Felipe Balbi45438a02016-08-11 12:26:59 +0300917 if (!tmp)
918 tmp = DWC3_TRB_NUM - 1;
919
920 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -0700921}
922
Felipe Balbic4233572016-05-12 14:08:34 +0300923static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
924{
925 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700926 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300927
928 /*
929 * If enqueue & dequeue are equal than it is either full or empty.
930 *
931 * One way to know for sure is if the TRB right before us has HWO bit
932 * set or not. If it has, then we're definitely full and can't fit any
933 * more transfers in our ring.
934 */
935 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -0700936 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
937 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
938 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +0300939
940 return DWC3_TRB_NUM - 1;
941 }
942
John Youn9d7aba72016-08-26 18:43:01 -0700943 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -0700944 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -0700945
John Youn9d7aba72016-08-26 18:43:01 -0700946 if (dep->trb_dequeue < dep->trb_enqueue)
947 trbs_left--;
948
John Youn32db3d92016-05-19 17:26:12 -0700949 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300950}
951
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300952static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300953 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300954{
Felipe Balbi1f512112016-08-12 13:17:27 +0300955 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300956 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300957 unsigned int length;
958 dma_addr_t dma;
959 int i;
960
Felipe Balbi1f512112016-08-12 13:17:27 +0300961 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300962 unsigned chain = true;
963
964 length = sg_dma_len(s);
965 dma = sg_dma_address(s);
966
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300967 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300968 chain = false;
969
970 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300971 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300972
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300973 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300974 break;
975 }
976}
977
978static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300979 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300980{
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300981 unsigned int length;
982 dma_addr_t dma;
983
984 dma = req->request.dma;
985 length = req->request.length;
986
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300987 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300988 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300989}
990
Felipe Balbi72246da2011-08-19 18:10:58 +0300991/*
992 * dwc3_prepare_trbs - setup TRBs from requests
993 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800995 * The function goes through the requests list and sets up TRBs for the
996 * transfers. The function returns once there are no more TRBs available or
997 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300998 */
Felipe Balbic4233572016-05-12 14:08:34 +0300999static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001000{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001001 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001002
1003 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1004
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001005 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001006 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001007
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001008 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001009 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001010 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001011 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001012 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001013
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001014 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001015 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001017}
1018
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001019static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001020{
1021 struct dwc3_gadget_ep_cmd_params params;
1022 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001023 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 int ret;
1025 u32 cmd;
1026
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001027 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001028
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001029 dwc3_prepare_trbs(dep);
1030 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001031 if (!req) {
1032 dep->flags |= DWC3_EP_PENDING_REQUEST;
1033 return 0;
1034 }
1035
1036 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001037
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001038 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301039 params.param0 = upper_32_bits(req->trb_dma);
1040 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001041 cmd = DWC3_DEPCMD_STARTTRANSFER |
1042 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301043 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001044 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1045 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301046 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001047
Felipe Balbi2cd47182016-04-12 16:42:43 +03001048 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001049 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001050 /*
1051 * FIXME we need to iterate over the list of requests
1052 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001053 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001054 */
Felipe Balbi15b8d932016-09-22 10:59:12 +03001055 dwc3_gadget_giveback(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001056 return ret;
1057 }
1058
1059 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001060
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001061 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001062 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001063 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001064 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001065
Felipe Balbi72246da2011-08-19 18:10:58 +03001066 return 0;
1067}
1068
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301069static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1070 struct dwc3_ep *dep, u32 cur_uf)
1071{
1072 u32 uf;
1073
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001074 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001075 dwc3_trace(trace_dwc3_gadget,
1076 "ISOC ep %s run out for requests",
1077 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301078 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301079 return;
1080 }
1081
1082 /* 4 micro frames in the future */
1083 uf = cur_uf + dep->interval * 4;
1084
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001085 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301086}
1087
1088static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1089 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1090{
1091 u32 cur_uf, mask;
1092
1093 mask = ~(dep->interval - 1);
1094 cur_uf = event->parameters & mask;
1095
1096 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1097}
1098
Felipe Balbi72246da2011-08-19 18:10:58 +03001099static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1100{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001101 struct dwc3 *dwc = dep->dwc;
1102 int ret;
1103
Felipe Balbibb423982015-11-16 15:31:21 -06001104 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001105 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001106 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001107 &req->request, dep->endpoint.name);
1108 return -ESHUTDOWN;
1109 }
1110
1111 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1112 &req->request, req->dep->name)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001113 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001114 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001115 return -EINVAL;
1116 }
1117
Felipe Balbifc8bb912016-05-16 13:14:48 +03001118 pm_runtime_get(dwc->dev);
1119
Felipe Balbi72246da2011-08-19 18:10:58 +03001120 req->request.actual = 0;
1121 req->request.status = -EINPROGRESS;
1122 req->direction = dep->direction;
1123 req->epnum = dep->number;
1124
Felipe Balbife84f522015-09-01 09:01:38 -05001125 trace_dwc3_ep_queue(req);
1126
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001127 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1128 dep->direction);
1129 if (ret)
1130 return ret;
1131
Felipe Balbi1f512112016-08-12 13:17:27 +03001132 req->sg = req->request.sg;
1133 req->num_pending_sgs = req->request.num_mapped_sgs;
1134
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001135 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001136
Felipe Balbid889c232016-09-29 15:44:29 +03001137 /*
1138 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1139 * wait for a XferNotReady event so we will know what's the current
1140 * (micro-)frame number.
1141 *
1142 * Without this trick, we are very, very likely gonna get Bus Expiry
1143 * errors which will force us issue EndTransfer command.
1144 */
1145 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1146 if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1147 list_empty(&dep->started_list)) {
Felipe Balbi08a36b52016-08-11 14:27:52 +03001148 dwc3_stop_active_transfer(dwc, dep->number, true);
1149 dep->flags = DWC3_EP_ENABLED;
1150 }
1151 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001152 }
1153
Felipe Balbi594e1212016-08-24 14:38:10 +03001154 if (!dwc3_calc_trbs_left(dep))
1155 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001156
Felipe Balbi08a36b52016-08-11 14:27:52 +03001157 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001158 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001159 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001160 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001161 dep->name);
1162 if (ret == -EBUSY)
1163 ret = 0;
1164
1165 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001166}
1167
Felipe Balbi04c03d12015-12-02 10:06:45 -06001168static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1169 struct usb_request *request)
1170{
1171 dwc3_gadget_ep_free_request(ep, request);
1172}
1173
1174static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1175{
1176 struct dwc3_request *req;
1177 struct usb_request *request;
1178 struct usb_ep *ep = &dep->endpoint;
1179
Felipe Balbi60cfb372016-05-24 13:45:17 +03001180 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001181 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1182 if (!request)
1183 return -ENOMEM;
1184
1185 request->length = 0;
1186 request->buf = dwc->zlp_buf;
1187 request->complete = __dwc3_gadget_ep_zlp_complete;
1188
1189 req = to_dwc3_request(request);
1190
1191 return __dwc3_gadget_ep_queue(dep, req);
1192}
1193
Felipe Balbi72246da2011-08-19 18:10:58 +03001194static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1195 gfp_t gfp_flags)
1196{
1197 struct dwc3_request *req = to_dwc3_request(request);
1198 struct dwc3_ep *dep = to_dwc3_ep(ep);
1199 struct dwc3 *dwc = dep->dwc;
1200
1201 unsigned long flags;
1202
1203 int ret;
1204
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001205 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001206 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001207
1208 /*
1209 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1210 * setting request->zero, instead of doing magic, we will just queue an
1211 * extra usb_request ourselves so that it gets handled the same way as
1212 * any other request.
1213 */
John Yound92618982015-12-22 12:23:20 -08001214 if (ret == 0 && request->zero && request->length &&
1215 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001216 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1217
Felipe Balbi72246da2011-08-19 18:10:58 +03001218 spin_unlock_irqrestore(&dwc->lock, flags);
1219
1220 return ret;
1221}
1222
1223static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1224 struct usb_request *request)
1225{
1226 struct dwc3_request *req = to_dwc3_request(request);
1227 struct dwc3_request *r = NULL;
1228
1229 struct dwc3_ep *dep = to_dwc3_ep(ep);
1230 struct dwc3 *dwc = dep->dwc;
1231
1232 unsigned long flags;
1233 int ret = 0;
1234
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001235 trace_dwc3_ep_dequeue(req);
1236
Felipe Balbi72246da2011-08-19 18:10:58 +03001237 spin_lock_irqsave(&dwc->lock, flags);
1238
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001239 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001240 if (r == req)
1241 break;
1242 }
1243
1244 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001245 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001246 if (r == req)
1247 break;
1248 }
1249 if (r == req) {
1250 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001251 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301252 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001253 }
1254 dev_err(dwc->dev, "request %p was not queued to %s\n",
1255 request, ep->name);
1256 ret = -EINVAL;
1257 goto out0;
1258 }
1259
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301260out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001261 /* giveback the request */
1262 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1263
1264out0:
1265 spin_unlock_irqrestore(&dwc->lock, flags);
1266
1267 return ret;
1268}
1269
Felipe Balbi7a608552014-09-24 14:19:52 -05001270int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001271{
1272 struct dwc3_gadget_ep_cmd_params params;
1273 struct dwc3 *dwc = dep->dwc;
1274 int ret;
1275
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001276 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1277 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1278 return -EINVAL;
1279 }
1280
Felipe Balbi72246da2011-08-19 18:10:58 +03001281 memset(&params, 0x00, sizeof(params));
1282
1283 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001284 struct dwc3_trb *trb;
1285
1286 unsigned transfer_in_flight;
1287 unsigned started;
1288
1289 if (dep->number > 1)
1290 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1291 else
1292 trb = &dwc->ep0_trb[dep->trb_enqueue];
1293
1294 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1295 started = !list_empty(&dep->started_list);
1296
1297 if (!protocol && ((dep->direction && transfer_in_flight) ||
1298 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001299 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001300 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001301 dep->name);
1302 return -EAGAIN;
1303 }
1304
Felipe Balbi2cd47182016-04-12 16:42:43 +03001305 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1306 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001308 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001309 dep->name);
1310 else
1311 dep->flags |= DWC3_EP_STALL;
1312 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001313
John Youn50c763f2016-05-31 17:49:56 -07001314 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001315 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001316 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001317 dep->name);
1318 else
Alan Sterna535d812013-11-01 12:05:12 -04001319 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001320 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001321
Felipe Balbi72246da2011-08-19 18:10:58 +03001322 return ret;
1323}
1324
1325static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1326{
1327 struct dwc3_ep *dep = to_dwc3_ep(ep);
1328 struct dwc3 *dwc = dep->dwc;
1329
1330 unsigned long flags;
1331
1332 int ret;
1333
1334 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001335 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 spin_unlock_irqrestore(&dwc->lock, flags);
1337
1338 return ret;
1339}
1340
1341static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1342{
1343 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001344 struct dwc3 *dwc = dep->dwc;
1345 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001346 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001347
Paul Zimmerman249a4562012-02-24 17:32:16 -08001348 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001349 dep->flags |= DWC3_EP_WEDGE;
1350
Pratyush Anand08f0d962012-06-25 22:40:43 +05301351 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001352 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301353 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001354 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001355 spin_unlock_irqrestore(&dwc->lock, flags);
1356
1357 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001358}
1359
1360/* -------------------------------------------------------------------------- */
1361
1362static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1363 .bLength = USB_DT_ENDPOINT_SIZE,
1364 .bDescriptorType = USB_DT_ENDPOINT,
1365 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1366};
1367
1368static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1369 .enable = dwc3_gadget_ep0_enable,
1370 .disable = dwc3_gadget_ep0_disable,
1371 .alloc_request = dwc3_gadget_ep_alloc_request,
1372 .free_request = dwc3_gadget_ep_free_request,
1373 .queue = dwc3_gadget_ep0_queue,
1374 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301375 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001376 .set_wedge = dwc3_gadget_ep_set_wedge,
1377};
1378
1379static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1380 .enable = dwc3_gadget_ep_enable,
1381 .disable = dwc3_gadget_ep_disable,
1382 .alloc_request = dwc3_gadget_ep_alloc_request,
1383 .free_request = dwc3_gadget_ep_free_request,
1384 .queue = dwc3_gadget_ep_queue,
1385 .dequeue = dwc3_gadget_ep_dequeue,
1386 .set_halt = dwc3_gadget_ep_set_halt,
1387 .set_wedge = dwc3_gadget_ep_set_wedge,
1388};
1389
1390/* -------------------------------------------------------------------------- */
1391
1392static int dwc3_gadget_get_frame(struct usb_gadget *g)
1393{
1394 struct dwc3 *dwc = gadget_to_dwc(g);
1395 u32 reg;
1396
1397 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1398 return DWC3_DSTS_SOFFN(reg);
1399}
1400
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001401static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001402{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001403 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001404
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001405 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001406 u32 reg;
1407
Felipe Balbi72246da2011-08-19 18:10:58 +03001408 u8 link_state;
1409 u8 speed;
1410
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 /*
1412 * According to the Databook Remote wakeup request should
1413 * be issued only when the device is in early suspend state.
1414 *
1415 * We can check that via USB Link State bits in DSTS register.
1416 */
1417 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1418
1419 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001420 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1421 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001422 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001423 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 }
1425
1426 link_state = DWC3_DSTS_USBLNKST(reg);
1427
1428 switch (link_state) {
1429 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1430 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1431 break;
1432 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001433 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001434 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001435 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001436 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001437 }
1438
Felipe Balbi8598bde2012-01-02 18:55:57 +02001439 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1440 if (ret < 0) {
1441 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001442 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001443 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001444
Paul Zimmerman802fde92012-04-27 13:10:52 +03001445 /* Recent versions do this automatically */
1446 if (dwc->revision < DWC3_REVISION_194A) {
1447 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001448 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001449 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1450 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1451 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001452
Paul Zimmerman1d046792012-02-15 18:56:56 -08001453 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001454 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001455
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001456 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001457 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1458
1459 /* in HS, means ON */
1460 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1461 break;
1462 }
1463
1464 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1465 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001466 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 }
1468
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001469 return 0;
1470}
1471
1472static int dwc3_gadget_wakeup(struct usb_gadget *g)
1473{
1474 struct dwc3 *dwc = gadget_to_dwc(g);
1475 unsigned long flags;
1476 int ret;
1477
1478 spin_lock_irqsave(&dwc->lock, flags);
1479 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 spin_unlock_irqrestore(&dwc->lock, flags);
1481
1482 return ret;
1483}
1484
1485static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1486 int is_selfpowered)
1487{
1488 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001489 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001490
Paul Zimmerman249a4562012-02-24 17:32:16 -08001491 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001492 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001493 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001494
1495 return 0;
1496}
1497
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001498static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001499{
1500 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001501 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001502
Felipe Balbifc8bb912016-05-16 13:14:48 +03001503 if (pm_runtime_suspended(dwc->dev))
1504 return 0;
1505
Felipe Balbi72246da2011-08-19 18:10:58 +03001506 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001507 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001508 if (dwc->revision <= DWC3_REVISION_187A) {
1509 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1510 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1511 }
1512
1513 if (dwc->revision >= DWC3_REVISION_194A)
1514 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1515 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001516
1517 if (dwc->has_hibernation)
1518 reg |= DWC3_DCTL_KEEP_CONNECT;
1519
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001520 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001521 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001522 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001523
1524 if (dwc->has_hibernation && !suspend)
1525 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1526
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001527 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001528 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001529
1530 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1531
1532 do {
1533 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001534 reg &= DWC3_DSTS_DEVCTRLHLT;
1535 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001536
1537 if (!timeout)
1538 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539
Felipe Balbi73815282015-01-27 13:48:14 -06001540 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001541 dwc->gadget_driver
1542 ? dwc->gadget_driver->function : "no-function",
1543 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301544
1545 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001546}
1547
1548static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1549{
1550 struct dwc3 *dwc = gadget_to_dwc(g);
1551 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301552 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001553
1554 is_on = !!is_on;
1555
Baolin Wangbb014732016-10-14 17:11:33 +08001556 /*
1557 * Per databook, when we want to stop the gadget, if a control transfer
1558 * is still in process, complete it and get the core into setup phase.
1559 */
1560 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1561 reinit_completion(&dwc->ep0_in_setup);
1562
1563 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1564 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1565 if (ret == 0) {
1566 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1567 return -ETIMEDOUT;
1568 }
1569 }
1570
Felipe Balbi72246da2011-08-19 18:10:58 +03001571 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001572 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 spin_unlock_irqrestore(&dwc->lock, flags);
1574
Pratyush Anand6f17f742012-07-02 10:21:55 +05301575 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001576}
1577
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001578static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1579{
1580 u32 reg;
1581
1582 /* Enable all but Start and End of Frame IRQs */
1583 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1584 DWC3_DEVTEN_EVNTOVERFLOWEN |
1585 DWC3_DEVTEN_CMDCMPLTEN |
1586 DWC3_DEVTEN_ERRTICERREN |
1587 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001588 DWC3_DEVTEN_CONNECTDONEEN |
1589 DWC3_DEVTEN_USBRSTEN |
1590 DWC3_DEVTEN_DISCONNEVTEN);
1591
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001592 if (dwc->revision < DWC3_REVISION_250A)
1593 reg |= DWC3_DEVTEN_ULSTCNGEN;
1594
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001595 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1596}
1597
1598static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1599{
1600 /* mask all interrupts */
1601 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1602}
1603
1604static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001605static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001606
Felipe Balbi4e994722016-05-13 14:09:59 +03001607/**
1608 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1609 * dwc: pointer to our context structure
1610 *
1611 * The following looks like complex but it's actually very simple. In order to
1612 * calculate the number of packets we can burst at once on OUT transfers, we're
1613 * gonna use RxFIFO size.
1614 *
1615 * To calculate RxFIFO size we need two numbers:
1616 * MDWIDTH = size, in bits, of the internal memory bus
1617 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1618 *
1619 * Given these two numbers, the formula is simple:
1620 *
1621 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1622 *
1623 * 24 bytes is for 3x SETUP packets
1624 * 16 bytes is a clock domain crossing tolerance
1625 *
1626 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1627 */
1628static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1629{
1630 u32 ram2_depth;
1631 u32 mdwidth;
1632 u32 nump;
1633 u32 reg;
1634
1635 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1636 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1637
1638 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1639 nump = min_t(u32, nump, 16);
1640
1641 /* update NumP */
1642 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1643 reg &= ~DWC3_DCFG_NUMP_MASK;
1644 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1645 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1646}
1647
Felipe Balbid7be2952016-05-04 15:49:37 +03001648static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001649{
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001651 int ret = 0;
1652 u32 reg;
1653
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1655 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001656
1657 /**
1658 * WORKAROUND: DWC3 revision < 2.20a have an issue
1659 * which would cause metastability state on Run/Stop
1660 * bit if we try to force the IP to USB2-only mode.
1661 *
1662 * Because of that, we cannot configure the IP to any
1663 * speed other than the SuperSpeed
1664 *
1665 * Refers to:
1666 *
1667 * STAR#9000525659: Clock Domain Crossing on DCTL in
1668 * USB 2.0 Mode
1669 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001670 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001671 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001672 } else {
1673 switch (dwc->maximum_speed) {
1674 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07001675 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001676 break;
1677 case USB_SPEED_FULL:
John Youn2da9ad72016-05-20 16:34:26 -07001678 reg |= DWC3_DCFG_FULLSPEED1;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001679 break;
1680 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07001681 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001682 break;
John Youn75808622016-02-05 17:09:13 -08001683 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07001684 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08001685 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001686 default:
John Youn77966eb2016-02-19 17:31:01 -08001687 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1688 dwc->maximum_speed);
1689 /* fall through */
1690 case USB_SPEED_SUPER:
1691 reg |= DWC3_DCFG_SUPERSPEED;
1692 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001693 }
1694 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001695 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1696
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001697 /*
1698 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1699 * field instead of letting dwc3 itself calculate that automatically.
1700 *
1701 * This way, we maximize the chances that we'll be able to get several
1702 * bursts of data without going through any sort of endpoint throttling.
1703 */
1704 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1705 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1706 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1707
Felipe Balbi4e994722016-05-13 14:09:59 +03001708 dwc3_gadget_setup_nump(dwc);
1709
Felipe Balbi72246da2011-08-19 18:10:58 +03001710 /* Start with SuperSpeed Default */
1711 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1712
1713 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001714 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1715 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001716 if (ret) {
1717 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001718 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001719 }
1720
1721 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001722 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1723 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001724 if (ret) {
1725 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001726 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 }
1728
1729 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001730 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001731 dwc3_ep0_out_start(dwc);
1732
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001733 dwc3_gadget_enable_irq(dwc);
1734
Felipe Balbid7be2952016-05-04 15:49:37 +03001735 return 0;
1736
1737err1:
1738 __dwc3_gadget_ep_disable(dwc->eps[0]);
1739
1740err0:
1741 return ret;
1742}
1743
1744static int dwc3_gadget_start(struct usb_gadget *g,
1745 struct usb_gadget_driver *driver)
1746{
1747 struct dwc3 *dwc = gadget_to_dwc(g);
1748 unsigned long flags;
1749 int ret = 0;
1750 int irq;
1751
Roger Quadros9522def2016-06-10 14:48:38 +03001752 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001753 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1754 IRQF_SHARED, "dwc3", dwc->ev_buf);
1755 if (ret) {
1756 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1757 irq, ret);
1758 goto err0;
1759 }
1760
1761 spin_lock_irqsave(&dwc->lock, flags);
1762 if (dwc->gadget_driver) {
1763 dev_err(dwc->dev, "%s is already bound to %s\n",
1764 dwc->gadget.name,
1765 dwc->gadget_driver->driver.name);
1766 ret = -EBUSY;
1767 goto err1;
1768 }
1769
1770 dwc->gadget_driver = driver;
1771
Felipe Balbifc8bb912016-05-16 13:14:48 +03001772 if (pm_runtime_active(dwc->dev))
1773 __dwc3_gadget_start(dwc);
1774
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 spin_unlock_irqrestore(&dwc->lock, flags);
1776
1777 return 0;
1778
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001779err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001781 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001782
1783err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 return ret;
1785}
1786
Felipe Balbid7be2952016-05-04 15:49:37 +03001787static void __dwc3_gadget_stop(struct dwc3 *dwc)
1788{
1789 dwc3_gadget_disable_irq(dwc);
1790 __dwc3_gadget_ep_disable(dwc->eps[0]);
1791 __dwc3_gadget_ep_disable(dwc->eps[1]);
1792}
1793
Felipe Balbi22835b82014-10-17 12:05:12 -05001794static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001795{
1796 struct dwc3 *dwc = gadget_to_dwc(g);
1797 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001798 int epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03001799
1800 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001801
1802 if (pm_runtime_suspended(dwc->dev))
1803 goto out;
1804
Felipe Balbid7be2952016-05-04 15:49:37 +03001805 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001806
1807 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1808 struct dwc3_ep *dep = dwc->eps[epnum];
1809
1810 if (!dep)
1811 continue;
1812
1813 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1814 continue;
1815
1816 wait_event_lock_irq(dep->wait_end_transfer,
1817 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1818 dwc->lock);
1819 }
1820
1821out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001822 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 spin_unlock_irqrestore(&dwc->lock, flags);
1824
Felipe Balbi3f308d12016-05-16 14:17:06 +03001825 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001826
Felipe Balbi72246da2011-08-19 18:10:58 +03001827 return 0;
1828}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001829
Felipe Balbi72246da2011-08-19 18:10:58 +03001830static const struct usb_gadget_ops dwc3_gadget_ops = {
1831 .get_frame = dwc3_gadget_get_frame,
1832 .wakeup = dwc3_gadget_wakeup,
1833 .set_selfpowered = dwc3_gadget_set_selfpowered,
1834 .pullup = dwc3_gadget_pullup,
1835 .udc_start = dwc3_gadget_start,
1836 .udc_stop = dwc3_gadget_stop,
1837};
1838
1839/* -------------------------------------------------------------------------- */
1840
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001841static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1842 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001843{
1844 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001845 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001847 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07001848 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001849
Felipe Balbi72246da2011-08-19 18:10:58 +03001850 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001851 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001852 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001853
1854 dep->dwc = dwc;
1855 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001856 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001857 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 dwc->eps[epnum] = dep;
1859
1860 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1861 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001862
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03001864 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03001865
Felipe Balbi73815282015-01-27 13:48:14 -06001866 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001867
Felipe Balbi72246da2011-08-19 18:10:58 +03001868 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001869 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301870 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001871 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1872 if (!epnum)
1873 dwc->gadget.ep0 = &dep->endpoint;
1874 } else {
1875 int ret;
1876
Robert Baldygae117e742013-12-13 12:23:38 +01001877 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001878 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001879 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1880 list_add_tail(&dep->endpoint.ep_list,
1881 &dwc->gadget.ep_list);
1882
1883 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001884 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001886 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001887
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001888 if (epnum == 0 || epnum == 1) {
1889 dep->endpoint.caps.type_control = true;
1890 } else {
1891 dep->endpoint.caps.type_iso = true;
1892 dep->endpoint.caps.type_bulk = true;
1893 dep->endpoint.caps.type_int = true;
1894 }
1895
1896 dep->endpoint.caps.dir_in = !!direction;
1897 dep->endpoint.caps.dir_out = !direction;
1898
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001899 INIT_LIST_HEAD(&dep->pending_list);
1900 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001901 }
1902
1903 return 0;
1904}
1905
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001906static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1907{
1908 int ret;
1909
1910 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1911
1912 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1913 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001914 dwc3_trace(trace_dwc3_gadget,
1915 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001916 return ret;
1917 }
1918
1919 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1920 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001921 dwc3_trace(trace_dwc3_gadget,
1922 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001923 return ret;
1924 }
1925
1926 return 0;
1927}
1928
Felipe Balbi72246da2011-08-19 18:10:58 +03001929static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1930{
1931 struct dwc3_ep *dep;
1932 u8 epnum;
1933
1934 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1935 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001936 if (!dep)
1937 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301938 /*
1939 * Physical endpoints 0 and 1 are special; they form the
1940 * bi-directional USB endpoint 0.
1941 *
1942 * For those two physical endpoints, we don't allocate a TRB
1943 * pool nor do we add them the endpoints list. Due to that, we
1944 * shouldn't do these two operations otherwise we would end up
1945 * with all sorts of bugs when removing dwc3.ko.
1946 */
1947 if (epnum != 0 && epnum != 1) {
1948 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001949 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301950 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001951
1952 kfree(dep);
1953 }
1954}
1955
Felipe Balbi72246da2011-08-19 18:10:58 +03001956/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001957
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301958static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1959 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03001960 const struct dwc3_event_depevt *event, int status,
1961 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301962{
1963 unsigned int count;
1964 unsigned int s_pkt = 0;
1965 unsigned int trb_status;
1966
Felipe Balbidc55c672016-08-12 13:20:32 +03001967 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03001968
1969 if (req->trb == trb)
1970 dep->queued_requests--;
1971
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001972 trace_dwc3_complete_trb(dep, trb);
1973
Felipe Balbie5b36ae2016-08-10 11:13:26 +03001974 /*
1975 * If we're in the middle of series of chained TRBs and we
1976 * receive a short transfer along the way, DWC3 will skip
1977 * through all TRBs including the last TRB in the chain (the
1978 * where CHN bit is zero. DWC3 will also avoid clearing HWO
1979 * bit and SW has to do it manually.
1980 *
1981 * We're going to do that here to avoid problems of HW trying
1982 * to use bogus TRBs for transfers.
1983 */
1984 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
1985 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1986
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301987 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03001988 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03001989
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301990 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03001991 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301992
1993 if (dep->direction) {
1994 if (count) {
1995 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1996 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001997 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001998 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301999 dep->name);
2000 /*
2001 * If missed isoc occurred and there is
2002 * no request queued then issue END
2003 * TRANSFER, so that core generates
2004 * next xfernotready and we will issue
2005 * a fresh START TRANSFER.
2006 * If there are still queued request
2007 * then wait, do not issue either END
2008 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002009 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302010 * giveback.If any future queued request
2011 * is successfully transferred then we
2012 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002013 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302014 */
2015 dep->flags |= DWC3_EP_MISSED_ISOC;
2016 } else {
2017 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2018 dep->name);
2019 status = -ECONNRESET;
2020 }
2021 } else {
2022 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2023 }
2024 } else {
2025 if (count && (event->status & DEPEVT_STATUS_SHORT))
2026 s_pkt = 1;
2027 }
2028
Felipe Balbi7c705df2016-08-10 12:35:30 +03002029 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302030 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002031
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302032 if ((event->status & DEPEVT_STATUS_IOC) &&
2033 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2034 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002035
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302036 return 0;
2037}
2038
Felipe Balbi72246da2011-08-19 18:10:58 +03002039static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2040 const struct dwc3_event_depevt *event, int status)
2041{
Felipe Balbi31162af2016-08-11 14:38:37 +03002042 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002043 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002044 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302045 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002046
Felipe Balbi31162af2016-08-11 14:38:37 +03002047 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002048 unsigned length;
2049 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002050 int chain;
2051
Felipe Balbi1f512112016-08-12 13:17:27 +03002052 length = req->request.length;
2053 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002054 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002055 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002056 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002057 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002058 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002059
Felipe Balbi1f512112016-08-12 13:17:27 +03002060 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002061 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002062
Felipe Balbi1f512112016-08-12 13:17:27 +03002063 req->sg = sg_next(s);
2064 req->num_pending_sgs--;
2065
Felipe Balbi31162af2016-08-11 14:38:37 +03002066 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2067 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002068 if (ret)
2069 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002070 }
2071 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002072 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002073 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002074 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002075 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002076
Felipe Balbic7de5732016-07-29 03:17:58 +03002077 /*
2078 * We assume here we will always receive the entire data block
2079 * which we should receive. Meaning, if we program RX to
2080 * receive 4K but we receive only 2K, we assume that's all we
2081 * should receive and we simply bounce the request back to the
2082 * gadget driver for further processing.
2083 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002084 actual = length - req->request.actual;
2085 req->request.actual = actual;
2086
2087 if (ret && chain && (actual < length) && req->num_pending_sgs)
2088 return __dwc3_gadget_kick_transfer(dep, 0);
2089
Ville Syrjäläd115d702015-08-31 19:48:28 +03002090 dwc3_gadget_giveback(dep, req, status);
2091
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002092 if (ret) {
2093 if ((event->status & DEPEVT_STATUS_IOC) &&
2094 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2095 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002096 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002097 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002098 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002099
Felipe Balbi4cb42212016-05-18 12:37:21 +03002100 /*
2101 * Our endpoint might get disabled by another thread during
2102 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2103 * early on so DWC3_EP_BUSY flag gets cleared
2104 */
2105 if (!dep->endpoint.desc)
2106 return 1;
2107
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302108 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002109 list_empty(&dep->started_list)) {
2110 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302111 /*
2112 * If there is no entry in request list then do
2113 * not issue END TRANSFER now. Just set PENDING
2114 * flag, so that END TRANSFER is issued when an
2115 * entry is added into request list.
2116 */
2117 dep->flags = DWC3_EP_PENDING_REQUEST;
2118 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002119 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302120 dep->flags = DWC3_EP_ENABLED;
2121 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302122 return 1;
2123 }
2124
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002125 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2126 return 0;
2127
Felipe Balbi72246da2011-08-19 18:10:58 +03002128 return 1;
2129}
2130
2131static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002132 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002133{
2134 unsigned status = 0;
2135 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002136 u32 is_xfer_complete;
2137
2138 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002139
2140 if (event->status & DEPEVT_STATUS_BUSERR)
2141 status = -ECONNRESET;
2142
Paul Zimmerman1d046792012-02-15 18:56:56 -08002143 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002144 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002145 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002146 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002147
2148 /*
2149 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2150 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2151 */
2152 if (dwc->revision < DWC3_REVISION_183A) {
2153 u32 reg;
2154 int i;
2155
2156 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002157 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002158
2159 if (!(dep->flags & DWC3_EP_ENABLED))
2160 continue;
2161
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002162 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002163 return;
2164 }
2165
2166 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2167 reg |= dwc->u1u2;
2168 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2169
2170 dwc->u1u2 = 0;
2171 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002172
Felipe Balbi4cb42212016-05-18 12:37:21 +03002173 /*
2174 * Our endpoint might get disabled by another thread during
2175 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2176 * early on so DWC3_EP_BUSY flag gets cleared
2177 */
2178 if (!dep->endpoint.desc)
2179 return;
2180
Felipe Balbie6e709b2015-09-28 15:16:56 -05002181 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002182 int ret;
2183
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002184 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002185 if (!ret || ret == -EBUSY)
2186 return;
2187 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002188}
2189
Felipe Balbi72246da2011-08-19 18:10:58 +03002190static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2191 const struct dwc3_event_depevt *event)
2192{
2193 struct dwc3_ep *dep;
2194 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002195 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002196
2197 dep = dwc->eps[epnum];
2198
Baolin Wang76a638f2016-10-31 19:38:36 +08002199 if (!(dep->flags & DWC3_EP_ENABLED) &&
2200 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Felipe Balbi3336abb2012-06-06 09:19:35 +03002201 return;
2202
Felipe Balbi72246da2011-08-19 18:10:58 +03002203 if (epnum == 0 || epnum == 1) {
2204 dwc3_ep0_interrupt(dwc, event);
2205 return;
2206 }
2207
2208 switch (event->endpoint_event) {
2209 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002210 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002211
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002212 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002213 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002214 return;
2215 }
2216
Jingoo Han029d97f2014-07-04 15:00:51 +09002217 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002218 break;
2219 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002220 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002221 break;
2222 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002223 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002224 dwc3_gadget_start_isoc(dwc, dep, event);
2225 } else {
2226 int ret;
2227
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002228 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002229 if (!ret || ret == -EBUSY)
2230 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03002231 }
2232
2233 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002234 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002235 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002236 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2237 dep->name);
2238 return;
2239 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002240 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002241 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002242 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2243
2244 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2245 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2246 wake_up(&dep->wait_end_transfer);
2247 }
2248 break;
2249 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002250 break;
2251 }
2252}
2253
2254static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2255{
2256 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2257 spin_unlock(&dwc->lock);
2258 dwc->gadget_driver->disconnect(&dwc->gadget);
2259 spin_lock(&dwc->lock);
2260 }
2261}
2262
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002263static void dwc3_suspend_gadget(struct dwc3 *dwc)
2264{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002265 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002266 spin_unlock(&dwc->lock);
2267 dwc->gadget_driver->suspend(&dwc->gadget);
2268 spin_lock(&dwc->lock);
2269 }
2270}
2271
2272static void dwc3_resume_gadget(struct dwc3 *dwc)
2273{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002274 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002275 spin_unlock(&dwc->lock);
2276 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002277 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002278 }
2279}
2280
2281static void dwc3_reset_gadget(struct dwc3 *dwc)
2282{
2283 if (!dwc->gadget_driver)
2284 return;
2285
2286 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2287 spin_unlock(&dwc->lock);
2288 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002289 spin_lock(&dwc->lock);
2290 }
2291}
2292
Paul Zimmermanb992e682012-04-27 14:17:35 +03002293static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002294{
2295 struct dwc3_ep *dep;
2296 struct dwc3_gadget_ep_cmd_params params;
2297 u32 cmd;
2298 int ret;
2299
2300 dep = dwc->eps[epnum];
2301
Baolin Wang76a638f2016-10-31 19:38:36 +08002302 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2303 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302304 return;
2305
Pratyush Anand57911502012-07-06 15:19:10 +05302306 /*
2307 * NOTICE: We are violating what the Databook says about the
2308 * EndTransfer command. Ideally we would _always_ wait for the
2309 * EndTransfer Command Completion IRQ, but that's causing too
2310 * much trouble synchronizing between us and gadget driver.
2311 *
2312 * We have discussed this with the IP Provider and it was
2313 * suggested to giveback all requests here, but give HW some
2314 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002315 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302316 *
2317 * Note also that a similar handling was tested by Synopsys
2318 * (thanks a lot Paul) and nothing bad has come out of it.
2319 * In short, what we're doing is:
2320 *
2321 * - Issue EndTransfer WITH CMDIOC bit set
2322 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002323 *
2324 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2325 * supports a mode to work around the above limitation. The
2326 * software can poll the CMDACT bit in the DEPCMD register
2327 * after issuing a EndTransfer command. This mode is enabled
2328 * by writing GUCTL2[14]. This polling is already done in the
2329 * dwc3_send_gadget_ep_cmd() function so if the mode is
2330 * enabled, the EndTransfer command will have completed upon
2331 * returning from this function and we don't need to delay for
2332 * 100us.
2333 *
2334 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302335 */
2336
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302337 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002338 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2339 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002340 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302341 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002342 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302343 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002344 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002345 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002346
Baolin Wang76a638f2016-10-31 19:38:36 +08002347 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2348 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002349 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002350 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002351}
2352
Felipe Balbi72246da2011-08-19 18:10:58 +03002353static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2354{
2355 u32 epnum;
2356
2357 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2358 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 int ret;
2360
2361 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002362 if (!dep)
2363 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002364
2365 if (!(dep->flags & DWC3_EP_STALL))
2366 continue;
2367
2368 dep->flags &= ~DWC3_EP_STALL;
2369
John Youn50c763f2016-05-31 17:49:56 -07002370 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002371 WARN_ON_ONCE(ret);
2372 }
2373}
2374
2375static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2376{
Felipe Balbic4430a22012-05-24 10:30:01 +03002377 int reg;
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2380 reg &= ~DWC3_DCTL_INITU1ENA;
2381 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2382
2383 reg &= ~DWC3_DCTL_INITU2ENA;
2384 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002385
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 dwc3_disconnect_gadget(dwc);
2387
2388 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002389 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002390 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002391
2392 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002393}
2394
Felipe Balbi72246da2011-08-19 18:10:58 +03002395static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2396{
2397 u32 reg;
2398
Felipe Balbifc8bb912016-05-16 13:14:48 +03002399 dwc->connected = true;
2400
Felipe Balbidf62df52011-10-14 15:11:49 +03002401 /*
2402 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2403 * would cause a missing Disconnect Event if there's a
2404 * pending Setup Packet in the FIFO.
2405 *
2406 * There's no suggested workaround on the official Bug
2407 * report, which states that "unless the driver/application
2408 * is doing any special handling of a disconnect event,
2409 * there is no functional issue".
2410 *
2411 * Unfortunately, it turns out that we _do_ some special
2412 * handling of a disconnect event, namely complete all
2413 * pending transfers, notify gadget driver of the
2414 * disconnection, and so on.
2415 *
2416 * Our suggested workaround is to follow the Disconnect
2417 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002418 * flag. Such flag gets set whenever we have a SETUP_PENDING
2419 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002420 * same endpoint.
2421 *
2422 * Refers to:
2423 *
2424 * STAR#9000466709: RTL: Device : Disconnect event not
2425 * generated if setup packet pending in FIFO
2426 */
2427 if (dwc->revision < DWC3_REVISION_188A) {
2428 if (dwc->setup_packet_pending)
2429 dwc3_gadget_disconnect_interrupt(dwc);
2430 }
2431
Felipe Balbi8e744752014-11-06 14:27:53 +08002432 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002433
2434 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2435 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2436 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002437 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 dwc3_clear_stall_all_ep(dwc);
2439
2440 /* Reset device address to zero */
2441 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2442 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2443 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002444}
2445
2446static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2447{
2448 u32 reg;
2449 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2450
2451 /*
2452 * We change the clock only at SS but I dunno why I would want to do
2453 * this. Maybe it becomes part of the power saving plan.
2454 */
2455
John Younee5cd412016-02-05 17:08:45 -08002456 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2457 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002458 return;
2459
2460 /*
2461 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2462 * each time on Connect Done.
2463 */
2464 if (!usb30_clock)
2465 return;
2466
2467 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2468 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2469 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2470}
2471
Felipe Balbi72246da2011-08-19 18:10:58 +03002472static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2473{
Felipe Balbi72246da2011-08-19 18:10:58 +03002474 struct dwc3_ep *dep;
2475 int ret;
2476 u32 reg;
2477 u8 speed;
2478
Felipe Balbi72246da2011-08-19 18:10:58 +03002479 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2480 speed = reg & DWC3_DSTS_CONNECTSPD;
2481 dwc->speed = speed;
2482
2483 dwc3_update_ram_clk_sel(dwc, speed);
2484
2485 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002486 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002487 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2488 dwc->gadget.ep0->maxpacket = 512;
2489 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2490 break;
John Youn2da9ad72016-05-20 16:34:26 -07002491 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002492 /*
2493 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2494 * would cause a missing USB3 Reset event.
2495 *
2496 * In such situations, we should force a USB3 Reset
2497 * event by calling our dwc3_gadget_reset_interrupt()
2498 * routine.
2499 *
2500 * Refers to:
2501 *
2502 * STAR#9000483510: RTL: SS : USB3 reset event may
2503 * not be generated always when the link enters poll
2504 */
2505 if (dwc->revision < DWC3_REVISION_190A)
2506 dwc3_gadget_reset_interrupt(dwc);
2507
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2509 dwc->gadget.ep0->maxpacket = 512;
2510 dwc->gadget.speed = USB_SPEED_SUPER;
2511 break;
John Youn2da9ad72016-05-20 16:34:26 -07002512 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002513 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2514 dwc->gadget.ep0->maxpacket = 64;
2515 dwc->gadget.speed = USB_SPEED_HIGH;
2516 break;
John Youn2da9ad72016-05-20 16:34:26 -07002517 case DWC3_DSTS_FULLSPEED2:
2518 case DWC3_DSTS_FULLSPEED1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002519 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2520 dwc->gadget.ep0->maxpacket = 64;
2521 dwc->gadget.speed = USB_SPEED_FULL;
2522 break;
John Youn2da9ad72016-05-20 16:34:26 -07002523 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002524 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2525 dwc->gadget.ep0->maxpacket = 8;
2526 dwc->gadget.speed = USB_SPEED_LOW;
2527 break;
2528 }
2529
Pratyush Anand2b758352013-01-14 15:59:31 +05302530 /* Enable USB2 LPM Capability */
2531
John Younee5cd412016-02-05 17:08:45 -08002532 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002533 (speed != DWC3_DSTS_SUPERSPEED) &&
2534 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302535 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2536 reg |= DWC3_DCFG_LPM_CAP;
2537 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2538
2539 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2540 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2541
Huang Rui460d0982014-10-31 11:11:18 +08002542 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302543
Huang Rui80caf7d2014-10-28 19:54:26 +08002544 /*
2545 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2546 * DCFG.LPMCap is set, core responses with an ACK and the
2547 * BESL value in the LPM token is less than or equal to LPM
2548 * NYET threshold.
2549 */
2550 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2551 && dwc->has_lpm_erratum,
2552 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2553
2554 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2555 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2556
Pratyush Anand2b758352013-01-14 15:59:31 +05302557 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002558 } else {
2559 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2560 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2561 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302562 }
2563
Felipe Balbi72246da2011-08-19 18:10:58 +03002564 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002565 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2566 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002567 if (ret) {
2568 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2569 return;
2570 }
2571
2572 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002573 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2574 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002575 if (ret) {
2576 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2577 return;
2578 }
2579
2580 /*
2581 * Configure PHY via GUSB3PIPECTLn if required.
2582 *
2583 * Update GTXFIFOSIZn
2584 *
2585 * In both cases reset values should be sufficient.
2586 */
2587}
2588
2589static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2590{
Felipe Balbi72246da2011-08-19 18:10:58 +03002591 /*
2592 * TODO take core out of low power mode when that's
2593 * implemented.
2594 */
2595
Jiebing Liad14d4e2014-12-11 13:26:29 +08002596 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2597 spin_unlock(&dwc->lock);
2598 dwc->gadget_driver->resume(&dwc->gadget);
2599 spin_lock(&dwc->lock);
2600 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002601}
2602
2603static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2604 unsigned int evtinfo)
2605{
Felipe Balbifae2b902011-10-14 13:00:30 +03002606 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002607 unsigned int pwropt;
2608
2609 /*
2610 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2611 * Hibernation mode enabled which would show up when device detects
2612 * host-initiated U3 exit.
2613 *
2614 * In that case, device will generate a Link State Change Interrupt
2615 * from U3 to RESUME which is only necessary if Hibernation is
2616 * configured in.
2617 *
2618 * There are no functional changes due to such spurious event and we
2619 * just need to ignore it.
2620 *
2621 * Refers to:
2622 *
2623 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2624 * operational mode
2625 */
2626 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2627 if ((dwc->revision < DWC3_REVISION_250A) &&
2628 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2629 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2630 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002631 dwc3_trace(trace_dwc3_gadget,
2632 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002633 return;
2634 }
2635 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002636
2637 /*
2638 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2639 * on the link partner, the USB session might do multiple entry/exit
2640 * of low power states before a transfer takes place.
2641 *
2642 * Due to this problem, we might experience lower throughput. The
2643 * suggested workaround is to disable DCTL[12:9] bits if we're
2644 * transitioning from U1/U2 to U0 and enable those bits again
2645 * after a transfer completes and there are no pending transfers
2646 * on any of the enabled endpoints.
2647 *
2648 * This is the first half of that workaround.
2649 *
2650 * Refers to:
2651 *
2652 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2653 * core send LGO_Ux entering U0
2654 */
2655 if (dwc->revision < DWC3_REVISION_183A) {
2656 if (next == DWC3_LINK_STATE_U0) {
2657 u32 u1u2;
2658 u32 reg;
2659
2660 switch (dwc->link_state) {
2661 case DWC3_LINK_STATE_U1:
2662 case DWC3_LINK_STATE_U2:
2663 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2664 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2665 | DWC3_DCTL_ACCEPTU2ENA
2666 | DWC3_DCTL_INITU1ENA
2667 | DWC3_DCTL_ACCEPTU1ENA);
2668
2669 if (!dwc->u1u2)
2670 dwc->u1u2 = reg & u1u2;
2671
2672 reg &= ~u1u2;
2673
2674 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2675 break;
2676 default:
2677 /* do nothing */
2678 break;
2679 }
2680 }
2681 }
2682
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002683 switch (next) {
2684 case DWC3_LINK_STATE_U1:
2685 if (dwc->speed == USB_SPEED_SUPER)
2686 dwc3_suspend_gadget(dwc);
2687 break;
2688 case DWC3_LINK_STATE_U2:
2689 case DWC3_LINK_STATE_U3:
2690 dwc3_suspend_gadget(dwc);
2691 break;
2692 case DWC3_LINK_STATE_RESUME:
2693 dwc3_resume_gadget(dwc);
2694 break;
2695 default:
2696 /* do nothing */
2697 break;
2698 }
2699
Felipe Balbie57ebc12014-04-22 13:20:12 -05002700 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701}
2702
Baolin Wang72704f82016-05-16 16:43:53 +08002703static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2704 unsigned int evtinfo)
2705{
2706 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2707
2708 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2709 dwc3_suspend_gadget(dwc);
2710
2711 dwc->link_state = next;
2712}
2713
Felipe Balbie1dadd32014-02-25 14:47:54 -06002714static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2715 unsigned int evtinfo)
2716{
2717 unsigned int is_ss = evtinfo & BIT(4);
2718
2719 /**
2720 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2721 * have a known issue which can cause USB CV TD.9.23 to fail
2722 * randomly.
2723 *
2724 * Because of this issue, core could generate bogus hibernation
2725 * events which SW needs to ignore.
2726 *
2727 * Refers to:
2728 *
2729 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2730 * Device Fallback from SuperSpeed
2731 */
2732 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2733 return;
2734
2735 /* enter hibernation here */
2736}
2737
Felipe Balbi72246da2011-08-19 18:10:58 +03002738static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2739 const struct dwc3_event_devt *event)
2740{
2741 switch (event->type) {
2742 case DWC3_DEVICE_EVENT_DISCONNECT:
2743 dwc3_gadget_disconnect_interrupt(dwc);
2744 break;
2745 case DWC3_DEVICE_EVENT_RESET:
2746 dwc3_gadget_reset_interrupt(dwc);
2747 break;
2748 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2749 dwc3_gadget_conndone_interrupt(dwc);
2750 break;
2751 case DWC3_DEVICE_EVENT_WAKEUP:
2752 dwc3_gadget_wakeup_interrupt(dwc);
2753 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002754 case DWC3_DEVICE_EVENT_HIBER_REQ:
2755 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2756 "unexpected hibernation event\n"))
2757 break;
2758
2759 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2760 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002761 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2762 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2763 break;
2764 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002765 /* It changed to be suspend event for version 2.30a and above */
2766 if (dwc->revision < DWC3_REVISION_230A) {
2767 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
2768 } else {
2769 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
2770
2771 /*
2772 * Ignore suspend event until the gadget enters into
2773 * USB_STATE_CONFIGURED state.
2774 */
2775 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2776 dwc3_gadget_suspend_interrupt(dwc,
2777 event->event_info);
2778 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002779 break;
2780 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002781 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002783 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002784 break;
2785 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002786 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002787 }
2788}
2789
2790static void dwc3_process_event_entry(struct dwc3 *dwc,
2791 const union dwc3_event *event)
2792{
Felipe Balbi43c96be2016-09-26 13:23:34 +03002793 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002794
Felipe Balbi72246da2011-08-19 18:10:58 +03002795 /* Endpoint IRQ, handle it and return early */
2796 if (event->type.is_devspec == 0) {
2797 /* depevt */
2798 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2799 }
2800
2801 switch (event->type.type) {
2802 case DWC3_EVENT_TYPE_DEV:
2803 dwc3_gadget_interrupt(dwc, &event->devt);
2804 break;
2805 /* REVISIT what to do with Carkit and I2C events ? */
2806 default:
2807 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2808 }
2809}
2810
Felipe Balbidea520a2016-03-30 09:39:34 +03002811static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002812{
Felipe Balbidea520a2016-03-30 09:39:34 +03002813 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002814 irqreturn_t ret = IRQ_NONE;
2815 int left;
2816 u32 reg;
2817
Felipe Balbif42f2442013-06-12 21:25:08 +03002818 left = evt->count;
2819
2820 if (!(evt->flags & DWC3_EVENT_PENDING))
2821 return IRQ_NONE;
2822
2823 while (left > 0) {
2824 union dwc3_event event;
2825
2826 event.raw = *(u32 *) (evt->buf + evt->lpos);
2827
2828 dwc3_process_event_entry(dwc, &event);
2829
2830 /*
2831 * FIXME we wrap around correctly to the next entry as
2832 * almost all entries are 4 bytes in size. There is one
2833 * entry which has 12 bytes which is a regular entry
2834 * followed by 8 bytes data. ATM I don't know how
2835 * things are organized if we get next to the a
2836 * boundary so I worry about that once we try to handle
2837 * that.
2838 */
2839 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2840 left -= 4;
2841
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002842 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002843 }
2844
2845 evt->count = 0;
2846 evt->flags &= ~DWC3_EVENT_PENDING;
2847 ret = IRQ_HANDLED;
2848
2849 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002850 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002851 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002852 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002853
2854 return ret;
2855}
2856
Felipe Balbidea520a2016-03-30 09:39:34 +03002857static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002858{
Felipe Balbidea520a2016-03-30 09:39:34 +03002859 struct dwc3_event_buffer *evt = _evt;
2860 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002861 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002862 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002863
Felipe Balbie5f68b42015-10-12 13:25:44 -05002864 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002865 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002866 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002867
2868 return ret;
2869}
2870
Felipe Balbidea520a2016-03-30 09:39:34 +03002871static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002872{
Felipe Balbidea520a2016-03-30 09:39:34 +03002873 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002874 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002875 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002876
Felipe Balbifc8bb912016-05-16 13:14:48 +03002877 if (pm_runtime_suspended(dwc->dev)) {
2878 pm_runtime_get(dwc->dev);
2879 disable_irq_nosync(dwc->irq_gadget);
2880 dwc->pending_events = true;
2881 return IRQ_HANDLED;
2882 }
2883
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002884 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002885 count &= DWC3_GEVNTCOUNT_MASK;
2886 if (!count)
2887 return IRQ_NONE;
2888
Felipe Balbib15a7622011-06-30 16:57:15 +03002889 evt->count = count;
2890 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002891
Felipe Balbie8adfc32013-06-12 21:11:14 +03002892 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002893 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002894 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002895 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002896
Felipe Balbib15a7622011-06-30 16:57:15 +03002897 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002898}
2899
Felipe Balbidea520a2016-03-30 09:39:34 +03002900static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002901{
Felipe Balbidea520a2016-03-30 09:39:34 +03002902 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002903
Felipe Balbidea520a2016-03-30 09:39:34 +03002904 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002905}
2906
Felipe Balbi6db38122016-10-03 11:27:01 +03002907static int dwc3_gadget_get_irq(struct dwc3 *dwc)
2908{
2909 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
2910 int irq;
2911
2912 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
2913 if (irq > 0)
2914 goto out;
2915
2916 if (irq == -EPROBE_DEFER)
2917 goto out;
2918
2919 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
2920 if (irq > 0)
2921 goto out;
2922
2923 if (irq == -EPROBE_DEFER)
2924 goto out;
2925
2926 irq = platform_get_irq(dwc3_pdev, 0);
2927 if (irq > 0)
2928 goto out;
2929
2930 if (irq != -EPROBE_DEFER)
2931 dev_err(dwc->dev, "missing peripheral IRQ\n");
2932
2933 if (!irq)
2934 irq = -EINVAL;
2935
2936out:
2937 return irq;
2938}
2939
Felipe Balbi72246da2011-08-19 18:10:58 +03002940/**
2941 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002942 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002943 *
2944 * Returns 0 on success otherwise negative errno.
2945 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002946int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002947{
Felipe Balbi6db38122016-10-03 11:27:01 +03002948 int ret;
2949 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03002950
Felipe Balbi6db38122016-10-03 11:27:01 +03002951 irq = dwc3_gadget_get_irq(dwc);
2952 if (irq < 0) {
2953 ret = irq;
2954 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03002955 }
2956
2957 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002958
2959 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2960 &dwc->ctrl_req_addr, GFP_KERNEL);
2961 if (!dwc->ctrl_req) {
2962 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2963 ret = -ENOMEM;
2964 goto err0;
2965 }
2966
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302967 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002968 &dwc->ep0_trb_addr, GFP_KERNEL);
2969 if (!dwc->ep0_trb) {
2970 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2971 ret = -ENOMEM;
2972 goto err1;
2973 }
2974
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002975 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002976 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002977 ret = -ENOMEM;
2978 goto err2;
2979 }
2980
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002981 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002982 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2983 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002984 if (!dwc->ep0_bounce) {
2985 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2986 ret = -ENOMEM;
2987 goto err3;
2988 }
2989
Felipe Balbi04c03d12015-12-02 10:06:45 -06002990 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2991 if (!dwc->zlp_buf) {
2992 ret = -ENOMEM;
2993 goto err4;
2994 }
2995
Baolin Wangbb014732016-10-14 17:11:33 +08002996 init_completion(&dwc->ep0_in_setup);
2997
Felipe Balbi72246da2011-08-19 18:10:58 +03002998 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002999 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003000 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003001 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003002 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003003
3004 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003005 * FIXME We might be setting max_speed to <SUPER, however versions
3006 * <2.20a of dwc3 have an issue with metastability (documented
3007 * elsewhere in this driver) which tells us we can't set max speed to
3008 * anything lower than SUPER.
3009 *
3010 * Because gadget.max_speed is only used by composite.c and function
3011 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3012 * to happen so we avoid sending SuperSpeed Capability descriptor
3013 * together with our BOS descriptor as that could confuse host into
3014 * thinking we can handle super speed.
3015 *
3016 * Note that, in fact, we won't even support GetBOS requests when speed
3017 * is less than super speed because we don't have means, yet, to tell
3018 * composite.c that we are USB 2.0 + LPM ECN.
3019 */
3020 if (dwc->revision < DWC3_REVISION_220A)
3021 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003022 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003023 dwc->revision);
3024
3025 dwc->gadget.max_speed = dwc->maximum_speed;
3026
3027 /*
David Cohena4b9d942013-12-09 15:55:38 -08003028 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3029 * on ep out.
3030 */
3031 dwc->gadget.quirk_ep_out_aligned_size = true;
3032
3033 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003034 * REVISIT: Here we should clear all pending IRQs to be
3035 * sure we're starting from a well known location.
3036 */
3037
3038 ret = dwc3_gadget_init_endpoints(dwc);
3039 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003040 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003041
Felipe Balbi72246da2011-08-19 18:10:58 +03003042 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3043 if (ret) {
3044 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003045 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003046 }
3047
3048 return 0;
3049
Felipe Balbi04c03d12015-12-02 10:06:45 -06003050err5:
3051 kfree(dwc->zlp_buf);
3052
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003053err4:
David Cohene1f80462013-09-11 17:42:47 -07003054 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003055 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3056 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003057
Felipe Balbi72246da2011-08-19 18:10:58 +03003058err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003059 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003060
3061err2:
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003062 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003063 dwc->ep0_trb, dwc->ep0_trb_addr);
3064
3065err1:
3066 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3067 dwc->ctrl_req, dwc->ctrl_req_addr);
3068
3069err0:
3070 return ret;
3071}
3072
Felipe Balbi7415f172012-04-30 14:56:33 +03003073/* -------------------------------------------------------------------------- */
3074
Felipe Balbi72246da2011-08-19 18:10:58 +03003075void dwc3_gadget_exit(struct dwc3 *dwc)
3076{
Felipe Balbi72246da2011-08-19 18:10:58 +03003077 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003078
Felipe Balbi72246da2011-08-19 18:10:58 +03003079 dwc3_gadget_free_endpoints(dwc);
3080
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003081 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3082 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003083
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003084 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003085 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003086
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003087 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003088 dwc->ep0_trb, dwc->ep0_trb_addr);
3089
3090 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3091 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003092}
Felipe Balbi7415f172012-04-30 14:56:33 +03003093
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003094int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003095{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003096 int ret;
3097
Roger Quadros9772b472016-04-12 11:33:29 +03003098 if (!dwc->gadget_driver)
3099 return 0;
3100
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003101 ret = dwc3_gadget_run_stop(dwc, false, false);
3102 if (ret < 0)
3103 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003104
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003105 dwc3_disconnect_gadget(dwc);
3106 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003107
3108 return 0;
3109}
3110
3111int dwc3_gadget_resume(struct dwc3 *dwc)
3112{
Felipe Balbi7415f172012-04-30 14:56:33 +03003113 int ret;
3114
Roger Quadros9772b472016-04-12 11:33:29 +03003115 if (!dwc->gadget_driver)
3116 return 0;
3117
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003118 ret = __dwc3_gadget_start(dwc);
3119 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003120 goto err0;
3121
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003122 ret = dwc3_gadget_run_stop(dwc, true, false);
3123 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003124 goto err1;
3125
Felipe Balbi7415f172012-04-30 14:56:33 +03003126 return 0;
3127
3128err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003129 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003130
3131err0:
3132 return ret;
3133}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003134
3135void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3136{
3137 if (dwc->pending_events) {
3138 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3139 dwc->pending_events = false;
3140 enable_irq(dwc->irq_gadget);
3141 }
3142}