blob: f82882023d54ca0e9a91a63af3caf89e80c17d7e [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200149{
Felipe Balbief966b92016-04-05 13:09:51 +0300150 dep->trb_enqueue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300151 dep->trb_enqueue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300152}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200153
Felipe Balbief966b92016-04-05 13:09:51 +0300154static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
155{
156 dep->trb_dequeue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300157 dep->trb_dequeue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static int dwc3_ep_is_last_trb(unsigned int index)
161{
Felipe Balbi4faf7552016-04-05 13:14:31 +0300162 return index == DWC3_TRB_NUM - 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200163}
164
Felipe Balbi72246da2011-08-19 18:10:58 +0300165void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
166 int status)
167{
168 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530169 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200171 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530172 i = 0;
173 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300174 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530175 /*
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
179 */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300180 if (dwc3_ep_is_last_trb(dep->trb_dequeue))
Felipe Balbief966b92016-04-05 13:09:51 +0300181 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530182 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200183 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 }
185 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200186 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Pratyush Anand0416e492012-08-10 13:42:16 +0530191 if (dwc->ep0_bounced && dep->number == 0)
192 dwc->ep0_bounced = false;
193 else
194 usb_gadget_unmap_request(&dwc->gadget, &req->request,
195 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
202}
203
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500204int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300205{
206 u32 timeout = 500;
207 u32 reg;
208
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500209 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500210
Felipe Balbib09bb642012-04-24 16:19:11 +0300211 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
212 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
213
214 do {
215 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
216 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600217 dwc3_trace(trace_dwc3_gadget,
218 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300219 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530220 if (DWC3_DGCMD_STATUS(reg))
221 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300222 return 0;
223 }
224
225 /*
226 * We can't sleep here, because it's also called from
227 * interrupt context.
228 */
229 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600230 if (!timeout) {
231 dwc3_trace(trace_dwc3_gadget,
232 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300233 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600234 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300235 udelay(1);
236 } while (1);
237}
238
Felipe Balbic36d8e92016-04-04 12:46:33 +0300239static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
240
Felipe Balbi72246da2011-08-19 18:10:58 +0300241int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
242 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
243{
244 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200245 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 u32 reg;
247
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300249 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500251 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300252
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300253 /*
254 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
255 * we're issuing an endpoint command, we must check if
256 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
257 *
258 * We will also set SUSPHY bit to what it was before returning as stated
259 * by the same section on Synopsys databook.
260 */
261 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
262 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
263 susphy = true;
264 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
265 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
266 }
267
Felipe Balbic36d8e92016-04-04 12:46:33 +0300268 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
269 int needs_wakeup;
270
271 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
272 dwc->link_state == DWC3_LINK_STATE_U2 ||
273 dwc->link_state == DWC3_LINK_STATE_U3);
274
275 if (unlikely(needs_wakeup)) {
276 ret = __dwc3_gadget_wakeup(dwc);
277 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
278 ret);
279 }
280 }
281
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300282 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
283 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
284 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
286 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
287 do {
288 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
289 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000290 int cmd_status = DWC3_DEPCMD_STATUS(reg);
291
Felipe Balbi73815282015-01-27 13:48:14 -0600292 dwc3_trace(trace_dwc3_gadget,
293 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294 cmd_status);
295
296 switch (cmd_status) {
297 case 0:
298 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300299 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000300 case DEPEVT_TRANSFER_NO_RESOURCE:
301 dwc3_trace(trace_dwc3_gadget, "%s: no resource available");
302 ret = -EINVAL;
303 break;
304 case DEPEVT_TRANSFER_BUS_EXPIRY:
305 /*
306 * SW issues START TRANSFER command to
307 * isochronous ep with future frame interval. If
308 * future interval time has already passed when
309 * core receives the command, it will respond
310 * with an error status of 'Bus Expiry'.
311 *
312 * Instead of always returning -EINVAL, let's
313 * give a hint to the gadget driver that this is
314 * the case by returning -EAGAIN.
315 */
316 dwc3_trace(trace_dwc3_gadget, "%s: bus expiry");
317 ret = -EAGAIN;
318 break;
319 default:
320 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
321 }
322
Felipe Balbic0ca3242016-04-04 09:11:51 +0300323 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 }
325
326 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 * We can't sleep here, because it is also called from
328 * interrupt context.
329 */
330 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600331 if (!timeout) {
332 dwc3_trace(trace_dwc3_gadget,
333 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 ret = -ETIMEDOUT;
335 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600336 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300337
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200338 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300339 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300340
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300341 if (unlikely(susphy)) {
342 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
343 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
344 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
345 }
346
Felipe Balbic0ca3242016-04-04 09:11:51 +0300347 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300348}
349
John Youn50c763f2016-05-31 17:49:56 -0700350static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
351{
352 struct dwc3 *dwc = dep->dwc;
353 struct dwc3_gadget_ep_cmd_params params;
354 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
355
356 /*
357 * As of core revision 2.60a the recommended programming model
358 * is to set the ClearPendIN bit when issuing a Clear Stall EP
359 * command for IN endpoints. This is to prevent an issue where
360 * some (non-compliant) hosts may not send ACK TPs for pending
361 * IN transfers due to a mishandled error condition. Synopsys
362 * STAR 9000614252.
363 */
364 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
365 cmd |= DWC3_DEPCMD_CLEARPENDIN;
366
367 memset(&params, 0, sizeof(params));
368
369 return dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
370}
371
Felipe Balbi72246da2011-08-19 18:10:58 +0300372static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200373 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300374{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300375 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
377 return dep->trb_pool_dma + offset;
378}
379
380static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
381{
382 struct dwc3 *dwc = dep->dwc;
383
384 if (dep->trb_pool)
385 return 0;
386
Felipe Balbi72246da2011-08-19 18:10:58 +0300387 dep->trb_pool = dma_alloc_coherent(dwc->dev,
388 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
389 &dep->trb_pool_dma, GFP_KERNEL);
390 if (!dep->trb_pool) {
391 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
392 dep->name);
393 return -ENOMEM;
394 }
395
396 return 0;
397}
398
399static void dwc3_free_trb_pool(struct dwc3_ep *dep)
400{
401 struct dwc3 *dwc = dep->dwc;
402
403 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
404 dep->trb_pool, dep->trb_pool_dma);
405
406 dep->trb_pool = NULL;
407 dep->trb_pool_dma = 0;
408}
409
John Younc4509602016-02-16 20:10:53 -0800410static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
411
412/**
413 * dwc3_gadget_start_config - Configure EP resources
414 * @dwc: pointer to our controller context structure
415 * @dep: endpoint that is being enabled
416 *
417 * The assignment of transfer resources cannot perfectly follow the
418 * data book due to the fact that the controller driver does not have
419 * all knowledge of the configuration in advance. It is given this
420 * information piecemeal by the composite gadget framework after every
421 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
422 * programming model in this scenario can cause errors. For two
423 * reasons:
424 *
425 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
426 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
427 * multiple interfaces.
428 *
429 * 2) The databook does not mention doing more DEPXFERCFG for new
430 * endpoint on alt setting (8.1.6).
431 *
432 * The following simplified method is used instead:
433 *
434 * All hardware endpoints can be assigned a transfer resource and this
435 * setting will stay persistent until either a core reset or
436 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
437 * do DEPXFERCFG for every hardware endpoint as well. We are
438 * guaranteed that there are as many transfer resources as endpoints.
439 *
440 * This function is called for each endpoint when it is being enabled
441 * but is triggered only when called for EP0-out, which always happens
442 * first, and which should only happen in one of the above conditions.
443 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300444static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
445{
446 struct dwc3_gadget_ep_cmd_params params;
447 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800448 int i;
449 int ret;
450
451 if (dep->number)
452 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300453
454 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800455 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
John Younc4509602016-02-16 20:10:53 -0800457 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
458 if (ret)
459 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300460
John Younc4509602016-02-16 20:10:53 -0800461 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
462 struct dwc3_ep *dep = dwc->eps[i];
463
464 if (!dep)
465 continue;
466
467 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
468 if (ret)
469 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 }
471
472 return 0;
473}
474
475static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200476 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300477 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600478 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300479{
480 struct dwc3_gadget_ep_cmd_params params;
481
482 memset(&params, 0x00, sizeof(params));
483
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300484 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900485 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
486
487 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800488 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300489 u32 burst = dep->endpoint.maxburst;
490 u32 nump;
491 u32 reg;
Chanho Parkd2e9a132012-08-31 16:54:07 +0900492
Felipe Balbi676e3492016-04-26 10:49:07 +0300493 /* update NumP */
494 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
495 nump = DWC3_DCFG_NUMP(reg);
496 nump = max(nump, burst);
497 reg &= ~DWC3_DCFG_NUMP_MASK;
498 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
499 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
500
501 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900502 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
Felipe Balbi4b345c92012-07-16 14:08:16 +0300504 if (ignore)
505 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
506
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600507 if (restore) {
508 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
509 params.param2 |= dep->saved_state;
510 }
511
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300512 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
513 | 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
544 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
545 DWC3_DEPCMD_SETEPCONFIG, &params);
546}
547
548static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
549{
550 struct dwc3_gadget_ep_cmd_params params;
551
552 memset(&params, 0x00, sizeof(params));
553
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300554 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
556 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
557 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
558}
559
560/**
561 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
562 * @dep: endpoint to be initialized
563 * @desc: USB Endpoint Descriptor
564 *
565 * Caller should take care of locking
566 */
567static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200568 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300569 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600570 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300571{
572 struct dwc3 *dwc = dep->dwc;
573 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300574 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300575
Felipe Balbi73815282015-01-27 13:48:14 -0600576 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300577
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 if (!(dep->flags & DWC3_EP_ENABLED)) {
579 ret = dwc3_gadget_start_config(dwc, dep);
580 if (ret)
581 return ret;
582 }
583
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600584 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
585 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 if (ret)
587 return ret;
588
589 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200590 struct dwc3_trb *trb_st_hw;
591 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200593 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200594 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 dep->type = usb_endpoint_type(desc);
596 dep->flags |= DWC3_EP_ENABLED;
597
598 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
599 reg |= DWC3_DALEPENA_EP(dep->number);
600 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
601
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300602 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200603 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300604
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300605 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 trb_st_hw = &dep->trb_pool[0];
607
Felipe Balbif6bafc62012-02-06 11:04:53 +0200608 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700609 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300610
Felipe Balbif6bafc62012-02-06 11:04:53 +0200611 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
612 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
613 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
614 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 }
616
Felipe Balbie901aa12016-03-16 14:01:37 +0200617out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500618 switch (usb_endpoint_type(desc)) {
619 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200620 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500621 break;
622 case USB_ENDPOINT_XFER_ISOC:
623 strlcat(dep->name, "-isoc", sizeof(dep->name));
624 break;
625 case USB_ENDPOINT_XFER_BULK:
626 strlcat(dep->name, "-bulk", sizeof(dep->name));
627 break;
628 case USB_ENDPOINT_XFER_INT:
629 strlcat(dep->name, "-int", sizeof(dep->name));
630 break;
631 default:
632 dev_err(dwc->dev, "invalid endpoint transfer type\n");
633 }
634
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 return 0;
636}
637
Paul Zimmermanb992e682012-04-27 14:17:35 +0300638static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200639static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300640{
641 struct dwc3_request *req;
642
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200643 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300644 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200645
Pratyush Anand57911502012-07-06 15:19:10 +0530646 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200647 while (!list_empty(&dep->started_list)) {
648 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530649
650 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
651 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200652 }
653
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200654 while (!list_empty(&dep->pending_list)) {
655 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300656
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200657 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300659}
660
661/**
662 * __dwc3_gadget_ep_disable - Disables a HW endpoint
663 * @dep: the endpoint to disable
664 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200665 * This function also removes requests which are currently processed ny the
666 * hardware and those which are not yet scheduled.
667 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300669static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
670{
671 struct dwc3 *dwc = dep->dwc;
672 u32 reg;
673
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500674 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
675
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200676 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300677
Felipe Balbi687ef982014-04-16 10:30:33 -0500678 /* make sure HW endpoint isn't stalled */
679 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500680 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500681
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
683 reg &= ~DWC3_DALEPENA_EP(dep->number);
684 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
685
Felipe Balbi879631a2011-09-30 10:58:47 +0300686 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200687 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200688 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300689 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300690 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300691
Felipe Balbiaa739972015-07-20 14:48:13 -0500692 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
693 dep->number >> 1,
694 (dep->number & 1) ? "in" : "out");
695
Felipe Balbi72246da2011-08-19 18:10:58 +0300696 return 0;
697}
698
699/* -------------------------------------------------------------------------- */
700
701static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
702 const struct usb_endpoint_descriptor *desc)
703{
704 return -EINVAL;
705}
706
707static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
708{
709 return -EINVAL;
710}
711
712/* -------------------------------------------------------------------------- */
713
714static int dwc3_gadget_ep_enable(struct usb_ep *ep,
715 const struct usb_endpoint_descriptor *desc)
716{
717 struct dwc3_ep *dep;
718 struct dwc3 *dwc;
719 unsigned long flags;
720 int ret;
721
722 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
723 pr_debug("dwc3: invalid parameters\n");
724 return -EINVAL;
725 }
726
727 if (!desc->wMaxPacketSize) {
728 pr_debug("dwc3: missing wMaxPacketSize\n");
729 return -EINVAL;
730 }
731
732 dep = to_dwc3_ep(ep);
733 dwc = dep->dwc;
734
Felipe Balbi95ca9612015-12-10 13:08:20 -0600735 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
736 "%s is already enabled\n",
737 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300738 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300739
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600741 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300742 spin_unlock_irqrestore(&dwc->lock, flags);
743
744 return ret;
745}
746
747static int dwc3_gadget_ep_disable(struct usb_ep *ep)
748{
749 struct dwc3_ep *dep;
750 struct dwc3 *dwc;
751 unsigned long flags;
752 int ret;
753
754 if (!ep) {
755 pr_debug("dwc3: invalid parameters\n");
756 return -EINVAL;
757 }
758
759 dep = to_dwc3_ep(ep);
760 dwc = dep->dwc;
761
Felipe Balbi95ca9612015-12-10 13:08:20 -0600762 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
763 "%s is already disabled\n",
764 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300765 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300766
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 spin_lock_irqsave(&dwc->lock, flags);
768 ret = __dwc3_gadget_ep_disable(dep);
769 spin_unlock_irqrestore(&dwc->lock, flags);
770
771 return ret;
772}
773
774static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
775 gfp_t gfp_flags)
776{
777 struct dwc3_request *req;
778 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300779
780 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900781 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300783
784 req->epnum = dep->number;
785 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300786
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500787 trace_dwc3_alloc_request(req);
788
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 return &req->request;
790}
791
792static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
793 struct usb_request *request)
794{
795 struct dwc3_request *req = to_dwc3_request(request);
796
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500797 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300798 kfree(req);
799}
800
Felipe Balbic71fc372011-11-22 11:37:34 +0200801/**
802 * dwc3_prepare_one_trb - setup one TRB from one request
803 * @dep: endpoint for which this request is prepared
804 * @req: dwc3_request pointer
805 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200806static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200807 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530808 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200809{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200810 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200811
Felipe Balbi73815282015-01-27 13:48:14 -0600812 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200813 dep->name, req, (unsigned long long) dma,
814 length, last ? " last" : "",
815 chain ? " chain" : "");
816
Pratyush Anand915e2022013-01-14 15:59:35 +0530817
Felipe Balbi4faf7552016-04-05 13:14:31 +0300818 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200819
Felipe Balbieeb720f2011-11-28 12:46:59 +0200820 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200821 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200822 req->trb = trb;
823 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300824 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200825 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200826
Felipe Balbief966b92016-04-05 13:09:51 +0300827 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300828 /* Skip the LINK-TRB */
829 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300830 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530831
Felipe Balbif6bafc62012-02-06 11:04:53 +0200832 trb->size = DWC3_TRB_SIZE_LENGTH(length);
833 trb->bpl = lower_32_bits(dma);
834 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200835
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200836 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200837 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200838 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200839 break;
840
841 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530842 if (!node)
843 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
844 else
845 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200846
847 /* always enable Interrupt on Missed ISOC */
848 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200849 break;
850
851 case USB_ENDPOINT_XFER_BULK:
852 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200853 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200854 break;
855 default:
856 /*
857 * This is only possible with faulty memory because we
858 * checked it already :)
859 */
860 BUG();
861 }
862
Felipe Balbica4d44e2016-03-10 13:53:27 +0200863 /* always enable Continue on Short Packet */
864 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600865
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300866 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200867 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
868
869 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530870 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200871
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530872 if (chain)
873 trb->ctrl |= DWC3_TRB_CTRL_CHN;
874
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200875 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200876 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
877
878 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500879
880 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200881}
882
Felipe Balbic4233572016-05-12 14:08:34 +0300883static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
884{
885 struct dwc3_trb *tmp;
886
887 /*
888 * If enqueue & dequeue are equal than it is either full or empty.
889 *
890 * One way to know for sure is if the TRB right before us has HWO bit
891 * set or not. If it has, then we're definitely full and can't fit any
892 * more transfers in our ring.
893 */
894 if (dep->trb_enqueue == dep->trb_dequeue) {
895 /* If we're full, enqueue/dequeue are > 0 */
896 if (dep->trb_enqueue) {
897 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
898 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
899 return 0;
900 }
901
902 return DWC3_TRB_NUM - 1;
903 }
904
905 return dep->trb_dequeue - dep->trb_enqueue;
906}
907
Felipe Balbi72246da2011-08-19 18:10:58 +0300908/*
909 * dwc3_prepare_trbs - setup TRBs from requests
910 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300911 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800912 * The function goes through the requests list and sets up TRBs for the
913 * transfers. The function returns once there are no more TRBs available or
914 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300915 */
Felipe Balbic4233572016-05-12 14:08:34 +0300916static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300917{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200918 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300919 u32 trbs_left;
Felipe Balbic71fc372011-11-22 11:37:34 +0200920 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300921
922 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
923
Felipe Balbic4233572016-05-12 14:08:34 +0300924 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300925
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200926 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200927 unsigned length;
928 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530929 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300930
Felipe Balbieeb720f2011-11-28 12:46:59 +0200931 if (req->request.num_mapped_sgs > 0) {
932 struct usb_request *request = &req->request;
933 struct scatterlist *sg = request->sg;
934 struct scatterlist *s;
935 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300936
Felipe Balbieeb720f2011-11-28 12:46:59 +0200937 for_each_sg(sg, s, request->num_mapped_sgs, i) {
938 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300939
Felipe Balbieeb720f2011-11-28 12:46:59 +0200940 length = sg_dma_len(s);
941 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300942
Paul Zimmerman1d046792012-02-15 18:56:56 -0800943 if (i == (request->num_mapped_sgs - 1) ||
944 sg_is_last(s)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200945 if (list_empty(&dep->pending_list))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530946 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200947 chain = false;
948 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300949
Felipe Balbieeb720f2011-11-28 12:46:59 +0200950 trbs_left--;
951 if (!trbs_left)
952 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300953
Felipe Balbieeb720f2011-11-28 12:46:59 +0200954 if (last_one)
955 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300956
Felipe Balbieeb720f2011-11-28 12:46:59 +0200957 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530958 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300959
Felipe Balbieeb720f2011-11-28 12:46:59 +0200960 if (last_one)
961 break;
962 }
Amit Virdi39e60632015-01-13 14:27:21 +0530963
964 if (last_one)
965 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200967 dma = req->request.dma;
968 length = req->request.length;
969 trbs_left--;
970
971 if (!trbs_left)
972 last_one = 1;
973
974 /* Is this the last request? */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200975 if (list_is_last(&req->list, &dep->pending_list))
Felipe Balbieeb720f2011-11-28 12:46:59 +0200976 last_one = 1;
977
978 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530979 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200980
981 if (last_one)
982 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300983 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300985}
986
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300987static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300988{
989 struct dwc3_gadget_ep_cmd_params params;
990 struct dwc3_request *req;
991 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300992 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300993 int ret;
994 u32 cmd;
995
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300996 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300997
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300998 dwc3_prepare_trbs(dep);
999 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 if (!req) {
1001 dep->flags |= DWC3_EP_PENDING_REQUEST;
1002 return 0;
1003 }
1004
1005 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001006
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001007 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301008 params.param0 = upper_32_bits(req->trb_dma);
1009 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001010 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301011 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001012 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301013 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001014
1015 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1016 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1017 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001018 /*
1019 * FIXME we need to iterate over the list of requests
1020 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001021 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001022 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001023 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1024 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001025 list_del(&req->list);
1026 return ret;
1027 }
1028
1029 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001030
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001031 if (starting) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001032 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001033 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001034 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001035 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001036
Felipe Balbi72246da2011-08-19 18:10:58 +03001037 return 0;
1038}
1039
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301040static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1041 struct dwc3_ep *dep, u32 cur_uf)
1042{
1043 u32 uf;
1044
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001045 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001046 dwc3_trace(trace_dwc3_gadget,
1047 "ISOC ep %s run out for requests",
1048 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301049 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301050 return;
1051 }
1052
1053 /* 4 micro frames in the future */
1054 uf = cur_uf + dep->interval * 4;
1055
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001056 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301057}
1058
1059static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1060 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1061{
1062 u32 cur_uf, mask;
1063
1064 mask = ~(dep->interval - 1);
1065 cur_uf = event->parameters & mask;
1066
1067 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1068}
1069
Felipe Balbi72246da2011-08-19 18:10:58 +03001070static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1071{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001072 struct dwc3 *dwc = dep->dwc;
1073 int ret;
1074
Felipe Balbibb423982015-11-16 15:31:21 -06001075 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001076 dwc3_trace(trace_dwc3_gadget,
1077 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001078 &req->request, dep->endpoint.name);
1079 return -ESHUTDOWN;
1080 }
1081
1082 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1083 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001084 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1085 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001086 return -EINVAL;
1087 }
1088
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 req->request.actual = 0;
1090 req->request.status = -EINPROGRESS;
1091 req->direction = dep->direction;
1092 req->epnum = dep->number;
1093
Felipe Balbife84f522015-09-01 09:01:38 -05001094 trace_dwc3_ep_queue(req);
1095
Felipe Balbi72246da2011-08-19 18:10:58 +03001096 /*
1097 * We only add to our list of requests now and
1098 * start consuming the list once we get XferNotReady
1099 * IRQ.
1100 *
1101 * That way, we avoid doing anything that we don't need
1102 * to do now and defer it until the point we receive a
1103 * particular token from the Host side.
1104 *
1105 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001106 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001107 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001108 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1109 dep->direction);
1110 if (ret)
1111 return ret;
1112
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001113 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001114
1115 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001116 * If there are no pending requests and the endpoint isn't already
1117 * busy, we will just start the request straight away.
1118 *
1119 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1120 * little bit faster.
1121 */
1122 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001123 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001124 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001125 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001126 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001127 }
1128
1129 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001130 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001131 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001132 * 1. XferNotReady with empty list of requests. We need to kick the
1133 * transfer here in that situation, otherwise we will be NAKing
1134 * forever. If we get XferNotReady before gadget driver has a
1135 * chance to queue a request, we will ACK the IRQ but won't be
1136 * able to receive the data until the next request is queued.
1137 * The following code is handling exactly that.
1138 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001139 */
1140 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301141 /*
1142 * If xfernotready is already elapsed and it is a case
1143 * of isoc transfer, then issue END TRANSFER, so that
1144 * you can receive xfernotready again and can have
1145 * notion of current microframe.
1146 */
1147 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001148 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001149 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301150 dep->flags = DWC3_EP_ENABLED;
1151 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301152 return 0;
1153 }
1154
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001155 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001156 if (!ret)
1157 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1158
Felipe Balbia8f32812015-09-16 10:40:07 -05001159 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001160 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001161
Felipe Balbib511e5e2012-06-06 12:00:50 +03001162 /*
1163 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1164 * kick the transfer here after queuing a request, otherwise the
1165 * core may not see the modified TRB(s).
1166 */
1167 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301168 (dep->flags & DWC3_EP_BUSY) &&
1169 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001170 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001171 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001172 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001173 }
1174
Felipe Balbib997ada2012-07-26 13:26:50 +03001175 /*
1176 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1177 * right away, otherwise host will not know we have streams to be
1178 * handled.
1179 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001180 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001181 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001182
Felipe Balbia8f32812015-09-16 10:40:07 -05001183out:
1184 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001185 dwc3_trace(trace_dwc3_gadget,
1186 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001187 dep->name);
1188 if (ret == -EBUSY)
1189 ret = 0;
1190
1191 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001192}
1193
Felipe Balbi04c03d12015-12-02 10:06:45 -06001194static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1195 struct usb_request *request)
1196{
1197 dwc3_gadget_ep_free_request(ep, request);
1198}
1199
1200static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1201{
1202 struct dwc3_request *req;
1203 struct usb_request *request;
1204 struct usb_ep *ep = &dep->endpoint;
1205
1206 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1207 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1208 if (!request)
1209 return -ENOMEM;
1210
1211 request->length = 0;
1212 request->buf = dwc->zlp_buf;
1213 request->complete = __dwc3_gadget_ep_zlp_complete;
1214
1215 req = to_dwc3_request(request);
1216
1217 return __dwc3_gadget_ep_queue(dep, req);
1218}
1219
Felipe Balbi72246da2011-08-19 18:10:58 +03001220static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1221 gfp_t gfp_flags)
1222{
1223 struct dwc3_request *req = to_dwc3_request(request);
1224 struct dwc3_ep *dep = to_dwc3_ep(ep);
1225 struct dwc3 *dwc = dep->dwc;
1226
1227 unsigned long flags;
1228
1229 int ret;
1230
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001231 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001233
1234 /*
1235 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1236 * setting request->zero, instead of doing magic, we will just queue an
1237 * extra usb_request ourselves so that it gets handled the same way as
1238 * any other request.
1239 */
John Yound92618982015-12-22 12:23:20 -08001240 if (ret == 0 && request->zero && request->length &&
1241 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001242 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1243
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 spin_unlock_irqrestore(&dwc->lock, flags);
1245
1246 return ret;
1247}
1248
1249static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1250 struct usb_request *request)
1251{
1252 struct dwc3_request *req = to_dwc3_request(request);
1253 struct dwc3_request *r = NULL;
1254
1255 struct dwc3_ep *dep = to_dwc3_ep(ep);
1256 struct dwc3 *dwc = dep->dwc;
1257
1258 unsigned long flags;
1259 int ret = 0;
1260
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001261 trace_dwc3_ep_dequeue(req);
1262
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 spin_lock_irqsave(&dwc->lock, flags);
1264
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001265 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 if (r == req)
1267 break;
1268 }
1269
1270 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001271 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 if (r == req)
1273 break;
1274 }
1275 if (r == req) {
1276 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001277 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301278 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001279 }
1280 dev_err(dwc->dev, "request %p was not queued to %s\n",
1281 request, ep->name);
1282 ret = -EINVAL;
1283 goto out0;
1284 }
1285
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301286out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001287 /* giveback the request */
1288 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1289
1290out0:
1291 spin_unlock_irqrestore(&dwc->lock, flags);
1292
1293 return ret;
1294}
1295
Felipe Balbi7a608552014-09-24 14:19:52 -05001296int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001297{
1298 struct dwc3_gadget_ep_cmd_params params;
1299 struct dwc3 *dwc = dep->dwc;
1300 int ret;
1301
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001302 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1303 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1304 return -EINVAL;
1305 }
1306
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 memset(&params, 0x00, sizeof(params));
1308
1309 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001310 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001311 (!list_empty(&dep->started_list) ||
1312 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001313 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001314 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001315 dep->name);
1316 return -EAGAIN;
1317 }
1318
Felipe Balbi72246da2011-08-19 18:10:58 +03001319 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1320 DWC3_DEPCMD_SETSTALL, &params);
1321 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001322 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 dep->name);
1324 else
1325 dep->flags |= DWC3_EP_STALL;
1326 } else {
John Youn50c763f2016-05-31 17:49:56 -07001327 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001328 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001329 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001330 dep->name);
1331 else
Alan Sterna535d812013-11-01 12:05:12 -04001332 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001333 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001334
Felipe Balbi72246da2011-08-19 18:10:58 +03001335 return ret;
1336}
1337
1338static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1339{
1340 struct dwc3_ep *dep = to_dwc3_ep(ep);
1341 struct dwc3 *dwc = dep->dwc;
1342
1343 unsigned long flags;
1344
1345 int ret;
1346
1347 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001348 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001349 spin_unlock_irqrestore(&dwc->lock, flags);
1350
1351 return ret;
1352}
1353
1354static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1355{
1356 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001357 struct dwc3 *dwc = dep->dwc;
1358 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001359 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001360
Paul Zimmerman249a4562012-02-24 17:32:16 -08001361 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001362 dep->flags |= DWC3_EP_WEDGE;
1363
Pratyush Anand08f0d962012-06-25 22:40:43 +05301364 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001365 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301366 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001367 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001368 spin_unlock_irqrestore(&dwc->lock, flags);
1369
1370 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001371}
1372
1373/* -------------------------------------------------------------------------- */
1374
1375static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1376 .bLength = USB_DT_ENDPOINT_SIZE,
1377 .bDescriptorType = USB_DT_ENDPOINT,
1378 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1379};
1380
1381static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1382 .enable = dwc3_gadget_ep0_enable,
1383 .disable = dwc3_gadget_ep0_disable,
1384 .alloc_request = dwc3_gadget_ep_alloc_request,
1385 .free_request = dwc3_gadget_ep_free_request,
1386 .queue = dwc3_gadget_ep0_queue,
1387 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301388 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001389 .set_wedge = dwc3_gadget_ep_set_wedge,
1390};
1391
1392static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1393 .enable = dwc3_gadget_ep_enable,
1394 .disable = dwc3_gadget_ep_disable,
1395 .alloc_request = dwc3_gadget_ep_alloc_request,
1396 .free_request = dwc3_gadget_ep_free_request,
1397 .queue = dwc3_gadget_ep_queue,
1398 .dequeue = dwc3_gadget_ep_dequeue,
1399 .set_halt = dwc3_gadget_ep_set_halt,
1400 .set_wedge = dwc3_gadget_ep_set_wedge,
1401};
1402
1403/* -------------------------------------------------------------------------- */
1404
1405static int dwc3_gadget_get_frame(struct usb_gadget *g)
1406{
1407 struct dwc3 *dwc = gadget_to_dwc(g);
1408 u32 reg;
1409
1410 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1411 return DWC3_DSTS_SOFFN(reg);
1412}
1413
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001414static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001415{
Felipe Balbi72246da2011-08-19 18:10:58 +03001416 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001417
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001418 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001419 u32 reg;
1420
Felipe Balbi72246da2011-08-19 18:10:58 +03001421 u8 link_state;
1422 u8 speed;
1423
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 /*
1425 * According to the Databook Remote wakeup request should
1426 * be issued only when the device is in early suspend state.
1427 *
1428 * We can check that via USB Link State bits in DSTS register.
1429 */
1430 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1431
1432 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001433 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1434 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001435 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001436 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001437 }
1438
1439 link_state = DWC3_DSTS_USBLNKST(reg);
1440
1441 switch (link_state) {
1442 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1443 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1444 break;
1445 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001446 dwc3_trace(trace_dwc3_gadget,
1447 "can't wakeup from '%s'\n",
1448 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001449 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 }
1451
Felipe Balbi8598bde2012-01-02 18:55:57 +02001452 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1453 if (ret < 0) {
1454 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001455 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001456 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001457
Paul Zimmerman802fde92012-04-27 13:10:52 +03001458 /* Recent versions do this automatically */
1459 if (dwc->revision < DWC3_REVISION_194A) {
1460 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001461 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001462 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1463 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1464 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001465
Paul Zimmerman1d046792012-02-15 18:56:56 -08001466 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 timeout = jiffies + msecs_to_jiffies(100);
1468
Paul Zimmerman1d046792012-02-15 18:56:56 -08001469 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001470 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1471
1472 /* in HS, means ON */
1473 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1474 break;
1475 }
1476
1477 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1478 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001479 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 }
1481
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001482 return 0;
1483}
1484
1485static int dwc3_gadget_wakeup(struct usb_gadget *g)
1486{
1487 struct dwc3 *dwc = gadget_to_dwc(g);
1488 unsigned long flags;
1489 int ret;
1490
1491 spin_lock_irqsave(&dwc->lock, flags);
1492 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001493 spin_unlock_irqrestore(&dwc->lock, flags);
1494
1495 return ret;
1496}
1497
1498static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1499 int is_selfpowered)
1500{
1501 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001502 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001503
Paul Zimmerman249a4562012-02-24 17:32:16 -08001504 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001505 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001506 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001507
1508 return 0;
1509}
1510
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001511static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001512{
1513 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001514 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001515
1516 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001517 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001518 if (dwc->revision <= DWC3_REVISION_187A) {
1519 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1520 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1521 }
1522
1523 if (dwc->revision >= DWC3_REVISION_194A)
1524 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1525 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001526
1527 if (dwc->has_hibernation)
1528 reg |= DWC3_DCTL_KEEP_CONNECT;
1529
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001530 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001531 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001532 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001533
1534 if (dwc->has_hibernation && !suspend)
1535 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1536
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001537 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001538 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001539
1540 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1541
1542 do {
1543 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1544 if (is_on) {
1545 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1546 break;
1547 } else {
1548 if (reg & DWC3_DSTS_DEVCTRLHLT)
1549 break;
1550 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001551 timeout--;
1552 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301553 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001554 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 } while (1);
1556
Felipe Balbi73815282015-01-27 13:48:14 -06001557 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 dwc->gadget_driver
1559 ? dwc->gadget_driver->function : "no-function",
1560 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301561
1562 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001563}
1564
1565static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1566{
1567 struct dwc3 *dwc = gadget_to_dwc(g);
1568 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301569 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001570
1571 is_on = !!is_on;
1572
1573 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001574 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001575 spin_unlock_irqrestore(&dwc->lock, flags);
1576
Pratyush Anand6f17f742012-07-02 10:21:55 +05301577 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001578}
1579
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001580static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1581{
1582 u32 reg;
1583
1584 /* Enable all but Start and End of Frame IRQs */
1585 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1586 DWC3_DEVTEN_EVNTOVERFLOWEN |
1587 DWC3_DEVTEN_CMDCMPLTEN |
1588 DWC3_DEVTEN_ERRTICERREN |
1589 DWC3_DEVTEN_WKUPEVTEN |
1590 DWC3_DEVTEN_ULSTCNGEN |
1591 DWC3_DEVTEN_CONNECTDONEEN |
1592 DWC3_DEVTEN_USBRSTEN |
1593 DWC3_DEVTEN_DISCONNEVTEN);
1594
1595 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 Balbid7be2952016-05-04 15:49:37 +03001607static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001608{
Felipe Balbi72246da2011-08-19 18:10:58 +03001609 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001610 int ret = 0;
1611 u32 reg;
1612
Felipe Balbi72246da2011-08-19 18:10:58 +03001613 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1614 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001615
1616 /**
1617 * WORKAROUND: DWC3 revision < 2.20a have an issue
1618 * which would cause metastability state on Run/Stop
1619 * bit if we try to force the IP to USB2-only mode.
1620 *
1621 * Because of that, we cannot configure the IP to any
1622 * speed other than the SuperSpeed
1623 *
1624 * Refers to:
1625 *
1626 * STAR#9000525659: Clock Domain Crossing on DCTL in
1627 * USB 2.0 Mode
1628 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001629 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001630 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001631 } else {
1632 switch (dwc->maximum_speed) {
1633 case USB_SPEED_LOW:
1634 reg |= DWC3_DSTS_LOWSPEED;
1635 break;
1636 case USB_SPEED_FULL:
1637 reg |= DWC3_DSTS_FULLSPEED1;
1638 break;
1639 case USB_SPEED_HIGH:
1640 reg |= DWC3_DSTS_HIGHSPEED;
1641 break;
John Youn75808622016-02-05 17:09:13 -08001642 case USB_SPEED_SUPER_PLUS:
1643 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1644 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001645 default:
John Youn77966eb2016-02-19 17:31:01 -08001646 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1647 dwc->maximum_speed);
1648 /* fall through */
1649 case USB_SPEED_SUPER:
1650 reg |= DWC3_DCFG_SUPERSPEED;
1651 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001652 }
1653 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1655
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001656 /*
1657 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1658 * field instead of letting dwc3 itself calculate that automatically.
1659 *
1660 * This way, we maximize the chances that we'll be able to get several
1661 * bursts of data without going through any sort of endpoint throttling.
1662 */
1663 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1664 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1665 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1666
Felipe Balbi72246da2011-08-19 18:10:58 +03001667 /* Start with SuperSpeed Default */
1668 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1669
1670 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001671 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1672 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 if (ret) {
1674 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001675 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001676 }
1677
1678 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001679 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1680 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001681 if (ret) {
1682 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001683 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 }
1685
1686 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001687 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001688 dwc3_ep0_out_start(dwc);
1689
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001690 dwc3_gadget_enable_irq(dwc);
1691
Felipe Balbid7be2952016-05-04 15:49:37 +03001692 return 0;
1693
1694err1:
1695 __dwc3_gadget_ep_disable(dwc->eps[0]);
1696
1697err0:
1698 return ret;
1699}
1700
1701static int dwc3_gadget_start(struct usb_gadget *g,
1702 struct usb_gadget_driver *driver)
1703{
1704 struct dwc3 *dwc = gadget_to_dwc(g);
1705 unsigned long flags;
1706 int ret = 0;
1707 int irq;
1708
1709 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1710 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1711 IRQF_SHARED, "dwc3", dwc->ev_buf);
1712 if (ret) {
1713 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1714 irq, ret);
1715 goto err0;
1716 }
1717
1718 spin_lock_irqsave(&dwc->lock, flags);
1719 if (dwc->gadget_driver) {
1720 dev_err(dwc->dev, "%s is already bound to %s\n",
1721 dwc->gadget.name,
1722 dwc->gadget_driver->driver.name);
1723 ret = -EBUSY;
1724 goto err1;
1725 }
1726
1727 dwc->gadget_driver = driver;
1728
1729 __dwc3_gadget_start(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001730 spin_unlock_irqrestore(&dwc->lock, flags);
1731
1732 return 0;
1733
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001734err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001735 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001736 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001737
1738err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001739 return ret;
1740}
1741
Felipe Balbid7be2952016-05-04 15:49:37 +03001742static void __dwc3_gadget_stop(struct dwc3 *dwc)
1743{
1744 dwc3_gadget_disable_irq(dwc);
1745 __dwc3_gadget_ep_disable(dwc->eps[0]);
1746 __dwc3_gadget_ep_disable(dwc->eps[1]);
1747}
1748
Felipe Balbi22835b82014-10-17 12:05:12 -05001749static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001750{
1751 struct dwc3 *dwc = gadget_to_dwc(g);
1752 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001753 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001754
1755 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001756 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001757 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 spin_unlock_irqrestore(&dwc->lock, flags);
1759
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001760 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001761 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001762
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 return 0;
1764}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001765
Felipe Balbi72246da2011-08-19 18:10:58 +03001766static const struct usb_gadget_ops dwc3_gadget_ops = {
1767 .get_frame = dwc3_gadget_get_frame,
1768 .wakeup = dwc3_gadget_wakeup,
1769 .set_selfpowered = dwc3_gadget_set_selfpowered,
1770 .pullup = dwc3_gadget_pullup,
1771 .udc_start = dwc3_gadget_start,
1772 .udc_stop = dwc3_gadget_stop,
1773};
1774
1775/* -------------------------------------------------------------------------- */
1776
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001777static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1778 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001779{
1780 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001781 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001782
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001783 for (i = 0; i < num; i++) {
1784 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001785
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001787 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001788 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001789
1790 dep->dwc = dwc;
1791 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001792 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001793 dwc->eps[epnum] = dep;
1794
1795 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1796 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001797
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001799
Felipe Balbi73815282015-01-27 13:48:14 -06001800 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001801
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001803 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301804 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001805 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1806 if (!epnum)
1807 dwc->gadget.ep0 = &dep->endpoint;
1808 } else {
1809 int ret;
1810
Robert Baldygae117e742013-12-13 12:23:38 +01001811 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001812 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001813 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1814 list_add_tail(&dep->endpoint.ep_list,
1815 &dwc->gadget.ep_list);
1816
1817 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001818 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001819 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001820 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001821
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001822 if (epnum == 0 || epnum == 1) {
1823 dep->endpoint.caps.type_control = true;
1824 } else {
1825 dep->endpoint.caps.type_iso = true;
1826 dep->endpoint.caps.type_bulk = true;
1827 dep->endpoint.caps.type_int = true;
1828 }
1829
1830 dep->endpoint.caps.dir_in = !!direction;
1831 dep->endpoint.caps.dir_out = !direction;
1832
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001833 INIT_LIST_HEAD(&dep->pending_list);
1834 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 }
1836
1837 return 0;
1838}
1839
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001840static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1841{
1842 int ret;
1843
1844 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1845
1846 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1847 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001848 dwc3_trace(trace_dwc3_gadget,
1849 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001850 return ret;
1851 }
1852
1853 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1854 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001855 dwc3_trace(trace_dwc3_gadget,
1856 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001857 return ret;
1858 }
1859
1860 return 0;
1861}
1862
Felipe Balbi72246da2011-08-19 18:10:58 +03001863static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1864{
1865 struct dwc3_ep *dep;
1866 u8 epnum;
1867
1868 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1869 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001870 if (!dep)
1871 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301872 /*
1873 * Physical endpoints 0 and 1 are special; they form the
1874 * bi-directional USB endpoint 0.
1875 *
1876 * For those two physical endpoints, we don't allocate a TRB
1877 * pool nor do we add them the endpoints list. Due to that, we
1878 * shouldn't do these two operations otherwise we would end up
1879 * with all sorts of bugs when removing dwc3.ko.
1880 */
1881 if (epnum != 0 && epnum != 1) {
1882 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001883 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301884 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001885
1886 kfree(dep);
1887 }
1888}
1889
Felipe Balbi72246da2011-08-19 18:10:58 +03001890/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001891
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301892static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1893 struct dwc3_request *req, struct dwc3_trb *trb,
1894 const struct dwc3_event_depevt *event, int status)
1895{
1896 unsigned int count;
1897 unsigned int s_pkt = 0;
1898 unsigned int trb_status;
1899
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001900 trace_dwc3_complete_trb(dep, trb);
1901
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301902 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1903 /*
1904 * We continue despite the error. There is not much we
1905 * can do. If we don't clean it up we loop forever. If
1906 * we skip the TRB then it gets overwritten after a
1907 * while since we use them in a ring buffer. A BUG()
1908 * would help. Lets hope that if this occurs, someone
1909 * fixes the root cause instead of looking away :)
1910 */
1911 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1912 dep->name, trb);
1913 count = trb->size & DWC3_TRB_SIZE_MASK;
1914
1915 if (dep->direction) {
1916 if (count) {
1917 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1918 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001919 dwc3_trace(trace_dwc3_gadget,
1920 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301921 dep->name);
1922 /*
1923 * If missed isoc occurred and there is
1924 * no request queued then issue END
1925 * TRANSFER, so that core generates
1926 * next xfernotready and we will issue
1927 * a fresh START TRANSFER.
1928 * If there are still queued request
1929 * then wait, do not issue either END
1930 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001931 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301932 * giveback.If any future queued request
1933 * is successfully transferred then we
1934 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001935 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301936 */
1937 dep->flags |= DWC3_EP_MISSED_ISOC;
1938 } else {
1939 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1940 dep->name);
1941 status = -ECONNRESET;
1942 }
1943 } else {
1944 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1945 }
1946 } else {
1947 if (count && (event->status & DEPEVT_STATUS_SHORT))
1948 s_pkt = 1;
1949 }
1950
1951 /*
1952 * We assume here we will always receive the entire data block
1953 * which we should receive. Meaning, if we program RX to
1954 * receive 4K but we receive only 2K, we assume that's all we
1955 * should receive and we simply bounce the request back to the
1956 * gadget driver for further processing.
1957 */
1958 req->request.actual += req->request.length - count;
1959 if (s_pkt)
1960 return 1;
1961 if ((event->status & DEPEVT_STATUS_LST) &&
1962 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1963 DWC3_TRB_CTRL_HWO)))
1964 return 1;
1965 if ((event->status & DEPEVT_STATUS_IOC) &&
1966 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1967 return 1;
1968 return 0;
1969}
1970
Felipe Balbi72246da2011-08-19 18:10:58 +03001971static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1972 const struct dwc3_event_depevt *event, int status)
1973{
1974 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001975 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301976 unsigned int slot;
1977 unsigned int i;
1978 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001979
1980 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001981 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001982 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001983 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001984
Ville Syrjäläd115d702015-08-31 19:48:28 +03001985 i = 0;
1986 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03001987 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03001988 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03001989 slot++;
1990 slot %= DWC3_TRB_NUM;
1991 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001992
Ville Syrjäläd115d702015-08-31 19:48:28 +03001993 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1994 event, status);
1995 if (ret)
1996 break;
1997 } while (++i < req->request.num_mapped_sgs);
1998
1999 dwc3_gadget_giveback(dep, req, status);
2000
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302001 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002002 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002003 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002004
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302005 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002006 list_empty(&dep->started_list)) {
2007 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302008 /*
2009 * If there is no entry in request list then do
2010 * not issue END TRANSFER now. Just set PENDING
2011 * flag, so that END TRANSFER is issued when an
2012 * entry is added into request list.
2013 */
2014 dep->flags = DWC3_EP_PENDING_REQUEST;
2015 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002016 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302017 dep->flags = DWC3_EP_ENABLED;
2018 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302019 return 1;
2020 }
2021
Felipe Balbi72246da2011-08-19 18:10:58 +03002022 return 1;
2023}
2024
2025static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002026 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002027{
2028 unsigned status = 0;
2029 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002030 u32 is_xfer_complete;
2031
2032 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002033
2034 if (event->status & DEPEVT_STATUS_BUSERR)
2035 status = -ECONNRESET;
2036
Paul Zimmerman1d046792012-02-15 18:56:56 -08002037 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002038 if (clean_busy && (is_xfer_complete ||
2039 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002041
2042 /*
2043 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2044 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2045 */
2046 if (dwc->revision < DWC3_REVISION_183A) {
2047 u32 reg;
2048 int i;
2049
2050 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002051 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002052
2053 if (!(dep->flags & DWC3_EP_ENABLED))
2054 continue;
2055
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002056 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002057 return;
2058 }
2059
2060 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2061 reg |= dwc->u1u2;
2062 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2063
2064 dwc->u1u2 = 0;
2065 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002066
Felipe Balbie6e709b2015-09-28 15:16:56 -05002067 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002068 int ret;
2069
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002070 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002071 if (!ret || ret == -EBUSY)
2072 return;
2073 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002074}
2075
Felipe Balbi72246da2011-08-19 18:10:58 +03002076static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2077 const struct dwc3_event_depevt *event)
2078{
2079 struct dwc3_ep *dep;
2080 u8 epnum = event->endpoint_number;
2081
2082 dep = dwc->eps[epnum];
2083
Felipe Balbi3336abb2012-06-06 09:19:35 +03002084 if (!(dep->flags & DWC3_EP_ENABLED))
2085 return;
2086
Felipe Balbi72246da2011-08-19 18:10:58 +03002087 if (epnum == 0 || epnum == 1) {
2088 dwc3_ep0_interrupt(dwc, event);
2089 return;
2090 }
2091
2092 switch (event->endpoint_event) {
2093 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002094 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002095
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002096 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002097 dwc3_trace(trace_dwc3_gadget,
2098 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002099 dep->name);
2100 return;
2101 }
2102
Jingoo Han029d97f2014-07-04 15:00:51 +09002103 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002104 break;
2105 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002106 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002107 break;
2108 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002109 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002110 dwc3_gadget_start_isoc(dwc, dep, event);
2111 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002112 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002113 int ret;
2114
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002115 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2116
Felipe Balbi73815282015-01-27 13:48:14 -06002117 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002118 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 : "Transfer Not Active");
2120
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002121 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002122 if (!ret || ret == -EBUSY)
2123 return;
2124
Felipe Balbiec5e7952015-11-16 16:04:13 -06002125 dwc3_trace(trace_dwc3_gadget,
2126 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002127 dep->name);
2128 }
2129
2130 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002131 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002132 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002133 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2134 dep->name);
2135 return;
2136 }
2137
2138 switch (event->status) {
2139 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002140 dwc3_trace(trace_dwc3_gadget,
2141 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002142 event->parameters);
2143
2144 break;
2145 case DEPEVT_STREAMEVT_NOTFOUND:
2146 /* FALLTHROUGH */
2147 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002148 dwc3_trace(trace_dwc3_gadget,
2149 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002150 }
2151 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002152 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002153 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002154 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002155 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002156 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 break;
2158 }
2159}
2160
2161static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2162{
2163 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2164 spin_unlock(&dwc->lock);
2165 dwc->gadget_driver->disconnect(&dwc->gadget);
2166 spin_lock(&dwc->lock);
2167 }
2168}
2169
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002170static void dwc3_suspend_gadget(struct dwc3 *dwc)
2171{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002172 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002173 spin_unlock(&dwc->lock);
2174 dwc->gadget_driver->suspend(&dwc->gadget);
2175 spin_lock(&dwc->lock);
2176 }
2177}
2178
2179static void dwc3_resume_gadget(struct dwc3 *dwc)
2180{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002181 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002182 spin_unlock(&dwc->lock);
2183 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002184 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002185 }
2186}
2187
2188static void dwc3_reset_gadget(struct dwc3 *dwc)
2189{
2190 if (!dwc->gadget_driver)
2191 return;
2192
2193 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2194 spin_unlock(&dwc->lock);
2195 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002196 spin_lock(&dwc->lock);
2197 }
2198}
2199
Paul Zimmermanb992e682012-04-27 14:17:35 +03002200static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002201{
2202 struct dwc3_ep *dep;
2203 struct dwc3_gadget_ep_cmd_params params;
2204 u32 cmd;
2205 int ret;
2206
2207 dep = dwc->eps[epnum];
2208
Felipe Balbib4996a82012-06-06 12:04:13 +03002209 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302210 return;
2211
Pratyush Anand57911502012-07-06 15:19:10 +05302212 /*
2213 * NOTICE: We are violating what the Databook says about the
2214 * EndTransfer command. Ideally we would _always_ wait for the
2215 * EndTransfer Command Completion IRQ, but that's causing too
2216 * much trouble synchronizing between us and gadget driver.
2217 *
2218 * We have discussed this with the IP Provider and it was
2219 * suggested to giveback all requests here, but give HW some
2220 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002221 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302222 *
2223 * Note also that a similar handling was tested by Synopsys
2224 * (thanks a lot Paul) and nothing bad has come out of it.
2225 * In short, what we're doing is:
2226 *
2227 * - Issue EndTransfer WITH CMDIOC bit set
2228 * - Wait 100us
2229 */
2230
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302231 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002232 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2233 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002234 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302235 memset(&params, 0, sizeof(params));
2236 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2237 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002238 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002239 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302240 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002241}
2242
2243static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2244{
2245 u32 epnum;
2246
2247 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2248 struct dwc3_ep *dep;
2249
2250 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002251 if (!dep)
2252 continue;
2253
Felipe Balbi72246da2011-08-19 18:10:58 +03002254 if (!(dep->flags & DWC3_EP_ENABLED))
2255 continue;
2256
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002257 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002258 }
2259}
2260
2261static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2262{
2263 u32 epnum;
2264
2265 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2266 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002267 int ret;
2268
2269 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002270 if (!dep)
2271 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002272
2273 if (!(dep->flags & DWC3_EP_STALL))
2274 continue;
2275
2276 dep->flags &= ~DWC3_EP_STALL;
2277
John Youn50c763f2016-05-31 17:49:56 -07002278 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002279 WARN_ON_ONCE(ret);
2280 }
2281}
2282
2283static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2284{
Felipe Balbic4430a22012-05-24 10:30:01 +03002285 int reg;
2286
Felipe Balbi72246da2011-08-19 18:10:58 +03002287 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2288 reg &= ~DWC3_DCTL_INITU1ENA;
2289 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2290
2291 reg &= ~DWC3_DCTL_INITU2ENA;
2292 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002293
Felipe Balbi72246da2011-08-19 18:10:58 +03002294 dwc3_disconnect_gadget(dwc);
2295
2296 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002297 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002298 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002299}
2300
Felipe Balbi72246da2011-08-19 18:10:58 +03002301static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2302{
2303 u32 reg;
2304
Felipe Balbidf62df52011-10-14 15:11:49 +03002305 /*
2306 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2307 * would cause a missing Disconnect Event if there's a
2308 * pending Setup Packet in the FIFO.
2309 *
2310 * There's no suggested workaround on the official Bug
2311 * report, which states that "unless the driver/application
2312 * is doing any special handling of a disconnect event,
2313 * there is no functional issue".
2314 *
2315 * Unfortunately, it turns out that we _do_ some special
2316 * handling of a disconnect event, namely complete all
2317 * pending transfers, notify gadget driver of the
2318 * disconnection, and so on.
2319 *
2320 * Our suggested workaround is to follow the Disconnect
2321 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002322 * flag. Such flag gets set whenever we have a SETUP_PENDING
2323 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002324 * same endpoint.
2325 *
2326 * Refers to:
2327 *
2328 * STAR#9000466709: RTL: Device : Disconnect event not
2329 * generated if setup packet pending in FIFO
2330 */
2331 if (dwc->revision < DWC3_REVISION_188A) {
2332 if (dwc->setup_packet_pending)
2333 dwc3_gadget_disconnect_interrupt(dwc);
2334 }
2335
Felipe Balbi8e744752014-11-06 14:27:53 +08002336 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002337
2338 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2339 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2340 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002341 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002342
2343 dwc3_stop_active_transfers(dwc);
2344 dwc3_clear_stall_all_ep(dwc);
2345
2346 /* Reset device address to zero */
2347 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2348 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2349 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002350}
2351
2352static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2353{
2354 u32 reg;
2355 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2356
2357 /*
2358 * We change the clock only at SS but I dunno why I would want to do
2359 * this. Maybe it becomes part of the power saving plan.
2360 */
2361
John Younee5cd412016-02-05 17:08:45 -08002362 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2363 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002364 return;
2365
2366 /*
2367 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2368 * each time on Connect Done.
2369 */
2370 if (!usb30_clock)
2371 return;
2372
2373 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2374 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2375 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2376}
2377
Felipe Balbi72246da2011-08-19 18:10:58 +03002378static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2379{
Felipe Balbi72246da2011-08-19 18:10:58 +03002380 struct dwc3_ep *dep;
2381 int ret;
2382 u32 reg;
2383 u8 speed;
2384
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2386 speed = reg & DWC3_DSTS_CONNECTSPD;
2387 dwc->speed = speed;
2388
2389 dwc3_update_ram_clk_sel(dwc, speed);
2390
2391 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002392 case DWC3_DCFG_SUPERSPEED_PLUS:
2393 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2394 dwc->gadget.ep0->maxpacket = 512;
2395 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2396 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002397 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002398 /*
2399 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2400 * would cause a missing USB3 Reset event.
2401 *
2402 * In such situations, we should force a USB3 Reset
2403 * event by calling our dwc3_gadget_reset_interrupt()
2404 * routine.
2405 *
2406 * Refers to:
2407 *
2408 * STAR#9000483510: RTL: SS : USB3 reset event may
2409 * not be generated always when the link enters poll
2410 */
2411 if (dwc->revision < DWC3_REVISION_190A)
2412 dwc3_gadget_reset_interrupt(dwc);
2413
Felipe Balbi72246da2011-08-19 18:10:58 +03002414 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2415 dwc->gadget.ep0->maxpacket = 512;
2416 dwc->gadget.speed = USB_SPEED_SUPER;
2417 break;
2418 case DWC3_DCFG_HIGHSPEED:
2419 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2420 dwc->gadget.ep0->maxpacket = 64;
2421 dwc->gadget.speed = USB_SPEED_HIGH;
2422 break;
2423 case DWC3_DCFG_FULLSPEED2:
2424 case DWC3_DCFG_FULLSPEED1:
2425 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2426 dwc->gadget.ep0->maxpacket = 64;
2427 dwc->gadget.speed = USB_SPEED_FULL;
2428 break;
2429 case DWC3_DCFG_LOWSPEED:
2430 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2431 dwc->gadget.ep0->maxpacket = 8;
2432 dwc->gadget.speed = USB_SPEED_LOW;
2433 break;
2434 }
2435
Pratyush Anand2b758352013-01-14 15:59:31 +05302436 /* Enable USB2 LPM Capability */
2437
John Younee5cd412016-02-05 17:08:45 -08002438 if ((dwc->revision > DWC3_REVISION_194A) &&
2439 (speed != DWC3_DCFG_SUPERSPEED) &&
2440 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302441 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2442 reg |= DWC3_DCFG_LPM_CAP;
2443 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2444
2445 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2446 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2447
Huang Rui460d0982014-10-31 11:11:18 +08002448 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302449
Huang Rui80caf7d2014-10-28 19:54:26 +08002450 /*
2451 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2452 * DCFG.LPMCap is set, core responses with an ACK and the
2453 * BESL value in the LPM token is less than or equal to LPM
2454 * NYET threshold.
2455 */
2456 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2457 && dwc->has_lpm_erratum,
2458 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2459
2460 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2461 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2462
Pratyush Anand2b758352013-01-14 15:59:31 +05302463 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002464 } else {
2465 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2466 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2467 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302468 }
2469
Felipe Balbi72246da2011-08-19 18:10:58 +03002470 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002471 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2472 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002473 if (ret) {
2474 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2475 return;
2476 }
2477
2478 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002479 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2480 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002481 if (ret) {
2482 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2483 return;
2484 }
2485
2486 /*
2487 * Configure PHY via GUSB3PIPECTLn if required.
2488 *
2489 * Update GTXFIFOSIZn
2490 *
2491 * In both cases reset values should be sufficient.
2492 */
2493}
2494
2495static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2496{
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 /*
2498 * TODO take core out of low power mode when that's
2499 * implemented.
2500 */
2501
Jiebing Liad14d4e2014-12-11 13:26:29 +08002502 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2503 spin_unlock(&dwc->lock);
2504 dwc->gadget_driver->resume(&dwc->gadget);
2505 spin_lock(&dwc->lock);
2506 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002507}
2508
2509static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2510 unsigned int evtinfo)
2511{
Felipe Balbifae2b902011-10-14 13:00:30 +03002512 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002513 unsigned int pwropt;
2514
2515 /*
2516 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2517 * Hibernation mode enabled which would show up when device detects
2518 * host-initiated U3 exit.
2519 *
2520 * In that case, device will generate a Link State Change Interrupt
2521 * from U3 to RESUME which is only necessary if Hibernation is
2522 * configured in.
2523 *
2524 * There are no functional changes due to such spurious event and we
2525 * just need to ignore it.
2526 *
2527 * Refers to:
2528 *
2529 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2530 * operational mode
2531 */
2532 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2533 if ((dwc->revision < DWC3_REVISION_250A) &&
2534 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2535 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2536 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002537 dwc3_trace(trace_dwc3_gadget,
2538 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002539 return;
2540 }
2541 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002542
2543 /*
2544 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2545 * on the link partner, the USB session might do multiple entry/exit
2546 * of low power states before a transfer takes place.
2547 *
2548 * Due to this problem, we might experience lower throughput. The
2549 * suggested workaround is to disable DCTL[12:9] bits if we're
2550 * transitioning from U1/U2 to U0 and enable those bits again
2551 * after a transfer completes and there are no pending transfers
2552 * on any of the enabled endpoints.
2553 *
2554 * This is the first half of that workaround.
2555 *
2556 * Refers to:
2557 *
2558 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2559 * core send LGO_Ux entering U0
2560 */
2561 if (dwc->revision < DWC3_REVISION_183A) {
2562 if (next == DWC3_LINK_STATE_U0) {
2563 u32 u1u2;
2564 u32 reg;
2565
2566 switch (dwc->link_state) {
2567 case DWC3_LINK_STATE_U1:
2568 case DWC3_LINK_STATE_U2:
2569 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2570 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2571 | DWC3_DCTL_ACCEPTU2ENA
2572 | DWC3_DCTL_INITU1ENA
2573 | DWC3_DCTL_ACCEPTU1ENA);
2574
2575 if (!dwc->u1u2)
2576 dwc->u1u2 = reg & u1u2;
2577
2578 reg &= ~u1u2;
2579
2580 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2581 break;
2582 default:
2583 /* do nothing */
2584 break;
2585 }
2586 }
2587 }
2588
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002589 switch (next) {
2590 case DWC3_LINK_STATE_U1:
2591 if (dwc->speed == USB_SPEED_SUPER)
2592 dwc3_suspend_gadget(dwc);
2593 break;
2594 case DWC3_LINK_STATE_U2:
2595 case DWC3_LINK_STATE_U3:
2596 dwc3_suspend_gadget(dwc);
2597 break;
2598 case DWC3_LINK_STATE_RESUME:
2599 dwc3_resume_gadget(dwc);
2600 break;
2601 default:
2602 /* do nothing */
2603 break;
2604 }
2605
Felipe Balbie57ebc12014-04-22 13:20:12 -05002606 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002607}
2608
Felipe Balbie1dadd32014-02-25 14:47:54 -06002609static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2610 unsigned int evtinfo)
2611{
2612 unsigned int is_ss = evtinfo & BIT(4);
2613
2614 /**
2615 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2616 * have a known issue which can cause USB CV TD.9.23 to fail
2617 * randomly.
2618 *
2619 * Because of this issue, core could generate bogus hibernation
2620 * events which SW needs to ignore.
2621 *
2622 * Refers to:
2623 *
2624 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2625 * Device Fallback from SuperSpeed
2626 */
2627 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2628 return;
2629
2630 /* enter hibernation here */
2631}
2632
Felipe Balbi72246da2011-08-19 18:10:58 +03002633static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2634 const struct dwc3_event_devt *event)
2635{
2636 switch (event->type) {
2637 case DWC3_DEVICE_EVENT_DISCONNECT:
2638 dwc3_gadget_disconnect_interrupt(dwc);
2639 break;
2640 case DWC3_DEVICE_EVENT_RESET:
2641 dwc3_gadget_reset_interrupt(dwc);
2642 break;
2643 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2644 dwc3_gadget_conndone_interrupt(dwc);
2645 break;
2646 case DWC3_DEVICE_EVENT_WAKEUP:
2647 dwc3_gadget_wakeup_interrupt(dwc);
2648 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002649 case DWC3_DEVICE_EVENT_HIBER_REQ:
2650 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2651 "unexpected hibernation event\n"))
2652 break;
2653
2654 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2655 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002656 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2657 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2658 break;
2659 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002660 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002661 break;
2662 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002663 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002664 break;
2665 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002666 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002667 break;
2668 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002669 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002670 break;
2671 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002672 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002673 break;
2674 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002675 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002676 }
2677}
2678
2679static void dwc3_process_event_entry(struct dwc3 *dwc,
2680 const union dwc3_event *event)
2681{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002682 trace_dwc3_event(event->raw);
2683
Felipe Balbi72246da2011-08-19 18:10:58 +03002684 /* Endpoint IRQ, handle it and return early */
2685 if (event->type.is_devspec == 0) {
2686 /* depevt */
2687 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2688 }
2689
2690 switch (event->type.type) {
2691 case DWC3_EVENT_TYPE_DEV:
2692 dwc3_gadget_interrupt(dwc, &event->devt);
2693 break;
2694 /* REVISIT what to do with Carkit and I2C events ? */
2695 default:
2696 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2697 }
2698}
2699
Felipe Balbidea520a2016-03-30 09:39:34 +03002700static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002701{
Felipe Balbidea520a2016-03-30 09:39:34 +03002702 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002703 irqreturn_t ret = IRQ_NONE;
2704 int left;
2705 u32 reg;
2706
Felipe Balbif42f2442013-06-12 21:25:08 +03002707 left = evt->count;
2708
2709 if (!(evt->flags & DWC3_EVENT_PENDING))
2710 return IRQ_NONE;
2711
2712 while (left > 0) {
2713 union dwc3_event event;
2714
2715 event.raw = *(u32 *) (evt->buf + evt->lpos);
2716
2717 dwc3_process_event_entry(dwc, &event);
2718
2719 /*
2720 * FIXME we wrap around correctly to the next entry as
2721 * almost all entries are 4 bytes in size. There is one
2722 * entry which has 12 bytes which is a regular entry
2723 * followed by 8 bytes data. ATM I don't know how
2724 * things are organized if we get next to the a
2725 * boundary so I worry about that once we try to handle
2726 * that.
2727 */
2728 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2729 left -= 4;
2730
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002731 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002732 }
2733
2734 evt->count = 0;
2735 evt->flags &= ~DWC3_EVENT_PENDING;
2736 ret = IRQ_HANDLED;
2737
2738 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002739 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002740 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002741 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002742
2743 return ret;
2744}
2745
Felipe Balbidea520a2016-03-30 09:39:34 +03002746static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002747{
Felipe Balbidea520a2016-03-30 09:39:34 +03002748 struct dwc3_event_buffer *evt = _evt;
2749 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002750 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002751 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002752
Felipe Balbie5f68b42015-10-12 13:25:44 -05002753 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002754 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002755 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002756
2757 return ret;
2758}
2759
Felipe Balbidea520a2016-03-30 09:39:34 +03002760static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002761{
Felipe Balbidea520a2016-03-30 09:39:34 +03002762 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002763 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002764 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002765
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002766 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002767 count &= DWC3_GEVNTCOUNT_MASK;
2768 if (!count)
2769 return IRQ_NONE;
2770
Felipe Balbib15a7622011-06-30 16:57:15 +03002771 evt->count = count;
2772 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002773
Felipe Balbie8adfc32013-06-12 21:11:14 +03002774 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002775 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002776 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002777 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002778
Felipe Balbib15a7622011-06-30 16:57:15 +03002779 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002780}
2781
Felipe Balbidea520a2016-03-30 09:39:34 +03002782static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002783{
Felipe Balbidea520a2016-03-30 09:39:34 +03002784 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002785
Felipe Balbidea520a2016-03-30 09:39:34 +03002786 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002787}
2788
2789/**
2790 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002791 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002792 *
2793 * Returns 0 on success otherwise negative errno.
2794 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002795int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002796{
Felipe Balbi72246da2011-08-19 18:10:58 +03002797 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002798
2799 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2800 &dwc->ctrl_req_addr, GFP_KERNEL);
2801 if (!dwc->ctrl_req) {
2802 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2803 ret = -ENOMEM;
2804 goto err0;
2805 }
2806
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302807 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002808 &dwc->ep0_trb_addr, GFP_KERNEL);
2809 if (!dwc->ep0_trb) {
2810 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2811 ret = -ENOMEM;
2812 goto err1;
2813 }
2814
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002815 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002816 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002817 ret = -ENOMEM;
2818 goto err2;
2819 }
2820
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002821 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002822 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2823 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002824 if (!dwc->ep0_bounce) {
2825 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2826 ret = -ENOMEM;
2827 goto err3;
2828 }
2829
Felipe Balbi04c03d12015-12-02 10:06:45 -06002830 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2831 if (!dwc->zlp_buf) {
2832 ret = -ENOMEM;
2833 goto err4;
2834 }
2835
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002837 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002838 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002839 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002840 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002841
2842 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002843 * FIXME We might be setting max_speed to <SUPER, however versions
2844 * <2.20a of dwc3 have an issue with metastability (documented
2845 * elsewhere in this driver) which tells us we can't set max speed to
2846 * anything lower than SUPER.
2847 *
2848 * Because gadget.max_speed is only used by composite.c and function
2849 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2850 * to happen so we avoid sending SuperSpeed Capability descriptor
2851 * together with our BOS descriptor as that could confuse host into
2852 * thinking we can handle super speed.
2853 *
2854 * Note that, in fact, we won't even support GetBOS requests when speed
2855 * is less than super speed because we don't have means, yet, to tell
2856 * composite.c that we are USB 2.0 + LPM ECN.
2857 */
2858 if (dwc->revision < DWC3_REVISION_220A)
2859 dwc3_trace(trace_dwc3_gadget,
2860 "Changing max_speed on rev %08x\n",
2861 dwc->revision);
2862
2863 dwc->gadget.max_speed = dwc->maximum_speed;
2864
2865 /*
David Cohena4b9d942013-12-09 15:55:38 -08002866 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2867 * on ep out.
2868 */
2869 dwc->gadget.quirk_ep_out_aligned_size = true;
2870
2871 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002872 * REVISIT: Here we should clear all pending IRQs to be
2873 * sure we're starting from a well known location.
2874 */
2875
2876 ret = dwc3_gadget_init_endpoints(dwc);
2877 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002878 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002879
Felipe Balbi72246da2011-08-19 18:10:58 +03002880 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2881 if (ret) {
2882 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002883 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002884 }
2885
2886 return 0;
2887
Felipe Balbi04c03d12015-12-02 10:06:45 -06002888err5:
2889 kfree(dwc->zlp_buf);
2890
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002891err4:
David Cohene1f80462013-09-11 17:42:47 -07002892 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002893 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2894 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002895
Felipe Balbi72246da2011-08-19 18:10:58 +03002896err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002897 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002898
2899err2:
2900 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2901 dwc->ep0_trb, dwc->ep0_trb_addr);
2902
2903err1:
2904 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2905 dwc->ctrl_req, dwc->ctrl_req_addr);
2906
2907err0:
2908 return ret;
2909}
2910
Felipe Balbi7415f172012-04-30 14:56:33 +03002911/* -------------------------------------------------------------------------- */
2912
Felipe Balbi72246da2011-08-19 18:10:58 +03002913void dwc3_gadget_exit(struct dwc3 *dwc)
2914{
Felipe Balbi72246da2011-08-19 18:10:58 +03002915 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002916
Felipe Balbi72246da2011-08-19 18:10:58 +03002917 dwc3_gadget_free_endpoints(dwc);
2918
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002919 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2920 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002921
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002922 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002923 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002924
2925 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2926 dwc->ep0_trb, dwc->ep0_trb_addr);
2927
2928 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2929 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002930}
Felipe Balbi7415f172012-04-30 14:56:33 +03002931
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002932int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002933{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002934 int ret;
2935
Roger Quadros9772b472016-04-12 11:33:29 +03002936 if (!dwc->gadget_driver)
2937 return 0;
2938
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002939 ret = dwc3_gadget_run_stop(dwc, false, false);
2940 if (ret < 0)
2941 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002942
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002943 dwc3_disconnect_gadget(dwc);
2944 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002945
2946 return 0;
2947}
2948
2949int dwc3_gadget_resume(struct dwc3 *dwc)
2950{
Felipe Balbi7415f172012-04-30 14:56:33 +03002951 int ret;
2952
Roger Quadros9772b472016-04-12 11:33:29 +03002953 if (!dwc->gadget_driver)
2954 return 0;
2955
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002956 ret = __dwc3_gadget_start(dwc);
2957 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002958 goto err0;
2959
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002960 ret = dwc3_gadget_run_stop(dwc, true, false);
2961 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002962 goto err1;
2963
Felipe Balbi7415f172012-04-30 14:56:33 +03002964 return 0;
2965
2966err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002967 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002968
2969err0:
2970 return ret;
2971}