blob: f06071824200e5acab6f65fd2d3c59a9b394950a [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 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020052#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030053
54#include "core.h"
55#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053056#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030057#include "io.h"
58
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020059/**
60 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
61 * @dwc: pointer to our context structure
62 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
63 *
64 * Caller should take care of locking. This function will
65 * return 0 on success or -EINVAL if wrong Test Selector
66 * is passed
67 */
68int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
69{
70 u32 reg;
71
72 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
73 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
74
75 switch (mode) {
76 case TEST_J:
77 case TEST_K:
78 case TEST_SE0_NAK:
79 case TEST_PACKET:
80 case TEST_FORCE_EN:
81 reg |= mode << 1;
82 break;
83 default:
84 return -EINVAL;
85 }
86
87 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
88
89 return 0;
90}
91
Felipe Balbi8598bde2012-01-02 18:55:57 +020092/**
93 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
94 * @dwc: pointer to our context structure
95 * @state: the state to put link into
96 *
97 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080098 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020099 */
100int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
101{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800102 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200103 u32 reg;
104
Paul Zimmerman88df4272012-04-27 13:10:52 +0300105 /*
106 * Wait until device controller is ready. Only applies to 1.94a and
107 * later RTL.
108 */
109 if (dwc->revision >= DWC3_REVISION_194A) {
110 while (--retries) {
111 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
112 if (reg & DWC3_DSTS_DCNRD)
113 udelay(5);
114 else
115 break;
116 }
117
118 if (retries <= 0)
119 return -ETIMEDOUT;
120 }
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
123 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
124
125 /* set requested state */
126 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
127 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
128
Paul Zimmerman88df4272012-04-27 13:10:52 +0300129 /*
130 * The following code is racy when called from dwc3_gadget_wakeup,
131 * and is not needed, at least on newer versions
132 */
133 if (dwc->revision >= DWC3_REVISION_194A)
134 return 0;
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300137 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200138 while (--retries) {
139 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
140
Felipe Balbi8598bde2012-01-02 18:55:57 +0200141 if (DWC3_DSTS_USBLNKST(reg) == state)
142 return 0;
143
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800144 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200145 }
146
147 dev_vdbg(dwc->dev, "link state change request timed out\n");
148
149 return -ETIMEDOUT;
150}
151
Felipe Balbi457e84b2012-01-18 18:04:09 +0200152/**
153 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
154 * @dwc: pointer to our context structure
155 *
156 * This function will a best effort FIFO allocation in order
157 * to improve FIFO usage and throughput, while still allowing
158 * us to enable as many endpoints as possible.
159 *
160 * Keep in mind that this operation will be highly dependent
161 * on the configured size for RAM1 - which contains TxFifo -,
162 * the amount of endpoints enabled on coreConsultant tool, and
163 * the width of the Master Bus.
164 *
165 * In the ideal world, we would always be able to satisfy the
166 * following equation:
167 *
168 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
169 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
170 *
171 * Unfortunately, due to many variables that's not always the case.
172 */
173int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
174{
175 int last_fifo_depth = 0;
176 int ram1_depth;
177 int fifo_size;
178 int mdwidth;
179 int num;
180
181 if (!dwc->needs_fifo_resize)
182 return 0;
183
184 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
185 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
186
187 /* MDWIDTH is represented in bits, we need it in bytes */
188 mdwidth >>= 3;
189
190 /*
191 * FIXME For now we will only allocate 1 wMaxPacketSize space
192 * for each enabled endpoint, later patches will come to
193 * improve this algorithm so that we better use the internal
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530194 * FIFO space. Also consider the case where TxFIFO RAM space
195 * may change dynamically based on the USB configuration.
Felipe Balbi457e84b2012-01-18 18:04:09 +0200196 */
197 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
198 struct dwc3_ep *dep = dwc->eps[num];
199 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200200 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200201 int tmp;
202
203 if (!(dep->number & 1))
204 continue;
205
206 if (!(dep->flags & DWC3_EP_ENABLED))
207 continue;
208
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530209 if (((dep->endpoint.maxburst > 1) &&
210 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200211 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200212 mult = 3;
213
214 /*
215 * REVISIT: the following assumes we will always have enough
216 * space available on the FIFO RAM for all possible use cases.
217 * Make sure that's true somehow and change FIFO allocation
218 * accordingly.
219 *
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530220 * If we have Bulk (burst only) or Isochronous endpoints, we
221 * want them to be able to be very, very fast. So we're giving
Felipe Balbi2e81c362012-02-02 13:01:12 +0200222 * those endpoints a fifo_size which is enough for 3 full
223 * packets
224 */
225 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200226 tmp += mdwidth;
227
228 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200229
Felipe Balbi457e84b2012-01-18 18:04:09 +0200230 fifo_size |= (last_fifo_depth << 16);
231
232 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
233 dep->name, last_fifo_depth, fifo_size & 0xffff);
234
235 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
236 fifo_size);
237
238 last_fifo_depth += (fifo_size & 0xffff);
239 }
240
241 return 0;
242}
243
Felipe Balbi72246da2011-08-19 18:10:58 +0300244void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
245 int status)
246{
247 struct dwc3 *dwc = dep->dwc;
248
249 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530250 req->queued = false;
251
Felipe Balbieeb720f2011-11-28 12:46:59 +0200252 if (req->request.num_mapped_sgs)
253 dep->busy_slot += req->request.num_mapped_sgs;
254 else
255 dep->busy_slot++;
256
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 /*
258 * Skip LINK TRB. We can't use req->trb and check for
259 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
260 * completed (not the LINK TRB).
261 */
262 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200263 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 dep->busy_slot++;
265 }
266 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200267 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300268
269 if (req->request.status == -EINPROGRESS)
270 req->request.status = status;
271
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530272 if (dwc->ep0_bounced && dep->number == 0)
273 dwc->ep0_bounced = false;
274 else
275 usb_gadget_unmap_request(&dwc->gadget, &req->request,
276 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300277
278 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
279 req, dep->name, req->request.actual,
280 req->request.length, status);
281
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530282 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200284 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 spin_lock(&dwc->lock);
286}
287
288static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
289{
290 switch (cmd) {
291 case DWC3_DEPCMD_DEPSTARTCFG:
292 return "Start New Configuration";
293 case DWC3_DEPCMD_ENDTRANSFER:
294 return "End Transfer";
295 case DWC3_DEPCMD_UPDATETRANSFER:
296 return "Update Transfer";
297 case DWC3_DEPCMD_STARTTRANSFER:
298 return "Start Transfer";
299 case DWC3_DEPCMD_CLEARSTALL:
300 return "Clear Stall";
301 case DWC3_DEPCMD_SETSTALL:
302 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300303 case DWC3_DEPCMD_GETEPSTATE:
304 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 case DWC3_DEPCMD_SETTRANSFRESOURCE:
306 return "Set Endpoint Transfer Resource";
307 case DWC3_DEPCMD_SETEPCONFIG:
308 return "Set Endpoint Configuration";
309 default:
310 return "UNKNOWN command";
311 }
312}
313
Felipe Balbi573c2762012-04-24 16:19:11 +0300314int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
315{
316 u32 timeout = 500;
317 u32 reg;
318
319 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
320 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
321
322 do {
323 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
324 if (!(reg & DWC3_DGCMD_CMDACT)) {
325 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
326 DWC3_DGCMD_STATUS(reg));
327 return 0;
328 }
329
330 /*
331 * We can't sleep here, because it's also called from
332 * interrupt context.
333 */
334 timeout--;
335 if (!timeout)
336 return -ETIMEDOUT;
337 udelay(1);
338 } while (1);
339}
340
Felipe Balbi72246da2011-08-19 18:10:58 +0300341int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
342 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
343{
344 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200345 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300346 u32 reg;
347
348 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
349 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300350 dwc3_gadget_ep_cmd_string(cmd), params->param0,
351 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300353 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
354 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
355 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300356
357 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
358 do {
359 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
360 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300361 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
362 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530363 /* SW issues START TRANSFER command to isochronous ep
364 * with future frame interval. If future interval time
365 * has already passed when core recieves command, core
366 * will respond with an error(bit13 in Command complete
367 * event. Hence return error in this case.
368 */
369 if (reg & 0x2000)
370 return -EAGAIN;
371 else
372 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300373 }
374
375 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 * We can't sleep here, because it is also called from
377 * interrupt context.
378 */
379 timeout--;
380 if (!timeout)
381 return -ETIMEDOUT;
382
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200383 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300384 } while (1);
385}
386
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300387dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200388 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300389{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300390 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300391
392 return dep->trb_pool_dma + offset;
393}
394
395static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
396{
397 struct dwc3 *dwc = dep->dwc;
398
399 if (dep->trb_pool)
400 return 0;
401
402 if (dep->number == 0 || dep->number == 1)
403 return 0;
404
405 dep->trb_pool = dma_alloc_coherent(dwc->dev,
406 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
407 &dep->trb_pool_dma, GFP_KERNEL);
408 if (!dep->trb_pool) {
409 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
410 dep->name);
411 return -ENOMEM;
412 }
413
414 return 0;
415}
416
417static void dwc3_free_trb_pool(struct dwc3_ep *dep)
418{
419 struct dwc3 *dwc = dep->dwc;
420
421 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
422 dep->trb_pool, dep->trb_pool_dma);
423
424 dep->trb_pool = NULL;
425 dep->trb_pool_dma = 0;
426}
427
428static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
429{
430 struct dwc3_gadget_ep_cmd_params params;
431 u32 cmd;
432
433 memset(&params, 0x00, sizeof(params));
434
435 if (dep->number != 1) {
436 cmd = DWC3_DEPCMD_DEPSTARTCFG;
437 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300438 if (dep->number > 1) {
439 if (dwc->start_config_issued)
440 return 0;
441 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300443 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300444
445 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
446 }
447
448 return 0;
449}
450
451static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200452 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300453 const struct usb_ss_ep_comp_descriptor *comp_desc,
454 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300455{
456 struct dwc3_gadget_ep_cmd_params params;
457
458 memset(&params, 0x00, sizeof(params));
459
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300460 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900461 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
462
463 /* Burst size is only needed in SuperSpeed mode */
464 if (dwc->gadget.speed == USB_SPEED_SUPER) {
465 u32 burst = dep->endpoint.maxburst - 1;
466
467 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
468 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300469
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300470 if (ignore)
471 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300472
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300473 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
474 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200476 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300477 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
478 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300479 dep->stream_capable = true;
480 }
481
Felipe Balbi72246da2011-08-19 18:10:58 +0300482 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300483 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300484
485 /*
486 * We are doing 1:1 mapping for endpoints, meaning
487 * Physical Endpoints 2 maps to Logical Endpoint 2 and
488 * so on. We consider the direction bit as part of the physical
489 * endpoint number. So USB endpoint 0x81 is 0x03.
490 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300491 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300492
493 /*
494 * We must use the lower 16 TX FIFOs even though
495 * HW might have more
496 */
497 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300498 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
500 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300501 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300502 dep->interval = 1 << (desc->bInterval - 1);
503 }
504
505 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
506 DWC3_DEPCMD_SETEPCONFIG, &params);
507}
508
509static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
510{
511 struct dwc3_gadget_ep_cmd_params params;
512
513 memset(&params, 0x00, sizeof(params));
514
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300515 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300516
517 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
518 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
519}
520
521/**
522 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
523 * @dep: endpoint to be initialized
524 * @desc: USB Endpoint Descriptor
525 *
526 * Caller should take care of locking
527 */
528static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200529 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300530 const struct usb_ss_ep_comp_descriptor *comp_desc,
531 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300532{
533 struct dwc3 *dwc = dep->dwc;
534 u32 reg;
535 int ret = -ENOMEM;
536
537 if (!(dep->flags & DWC3_EP_ENABLED)) {
538 ret = dwc3_gadget_start_config(dwc, dep);
539 if (ret)
540 return ret;
541 }
542
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300543 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 if (ret)
545 return ret;
546
547 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200548 struct dwc3_trb *trb_st_hw;
549 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300550
551 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
552 if (ret)
553 return ret;
554
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200555 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200556 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557 dep->type = usb_endpoint_type(desc);
558 dep->flags |= DWC3_EP_ENABLED;
559
560 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
561 reg |= DWC3_DALEPENA_EP(dep->number);
562 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
563
564 if (!usb_endpoint_xfer_isoc(desc))
565 return 0;
566
567 memset(&trb_link, 0, sizeof(trb_link));
568
Paul Zimmerman1d046792012-02-15 18:56:56 -0800569 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 trb_st_hw = &dep->trb_pool[0];
571
Felipe Balbif6bafc62012-02-06 11:04:53 +0200572 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
Felipe Balbif6bafc62012-02-06 11:04:53 +0200574 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
575 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
576 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
577 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 }
579
580 return 0;
581}
582
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200583static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
584static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300585{
586 struct dwc3_request *req;
587
Felipe Balbib129eb72012-02-17 12:10:04 +0200588 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200589 dwc3_stop_active_transfer(dwc, dep->number);
590
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530591 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530592 while (!list_empty(&dep->req_queued)) {
593 req = next_request(&dep->req_queued);
594
595 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
596 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200597 }
598
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 while (!list_empty(&dep->request_list)) {
600 req = next_request(&dep->request_list);
601
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200602 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300603 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300604}
605
606/**
607 * __dwc3_gadget_ep_disable - Disables a HW endpoint
608 * @dep: the endpoint to disable
609 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200610 * This function also removes requests which are currently processed ny the
611 * hardware and those which are not yet scheduled.
612 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300614static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
615{
616 struct dwc3 *dwc = dep->dwc;
617 u32 reg;
618
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200619 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300620
621 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
622 reg &= ~DWC3_DALEPENA_EP(dep->number);
623 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
624
Felipe Balbi879631a2011-09-30 10:58:47 +0300625 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200626 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200627 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300628 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300629 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300630
631 return 0;
632}
633
634/* -------------------------------------------------------------------------- */
635
636static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
637 const struct usb_endpoint_descriptor *desc)
638{
639 return -EINVAL;
640}
641
642static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
643{
644 return -EINVAL;
645}
646
647/* -------------------------------------------------------------------------- */
648
649static int dwc3_gadget_ep_enable(struct usb_ep *ep,
650 const struct usb_endpoint_descriptor *desc)
651{
652 struct dwc3_ep *dep;
653 struct dwc3 *dwc;
654 unsigned long flags;
655 int ret;
656
657 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
658 pr_debug("dwc3: invalid parameters\n");
659 return -EINVAL;
660 }
661
662 if (!desc->wMaxPacketSize) {
663 pr_debug("dwc3: missing wMaxPacketSize\n");
664 return -EINVAL;
665 }
666
667 dep = to_dwc3_ep(ep);
668 dwc = dep->dwc;
669
Felipe Balbi14395072012-08-15 12:28:29 +0300670 if (dep->flags & DWC3_EP_ENABLED) {
671 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
672 dep->name);
673 return 0;
674 }
675
Felipe Balbi72246da2011-08-19 18:10:58 +0300676 switch (usb_endpoint_type(desc)) {
677 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900678 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300679 break;
680 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900681 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 break;
683 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900684 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 break;
686 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900687 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 break;
689 default:
690 dev_err(dwc->dev, "invalid endpoint transfer type\n");
691 }
692
Felipe Balbi72246da2011-08-19 18:10:58 +0300693 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
694
695 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300696 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530697 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300698 spin_unlock_irqrestore(&dwc->lock, flags);
699
700 return ret;
701}
702
703static int dwc3_gadget_ep_disable(struct usb_ep *ep)
704{
705 struct dwc3_ep *dep;
706 struct dwc3 *dwc;
707 unsigned long flags;
708 int ret;
709
710 if (!ep) {
711 pr_debug("dwc3: invalid parameters\n");
712 return -EINVAL;
713 }
714
715 dep = to_dwc3_ep(ep);
716 dwc = dep->dwc;
717
718 if (!(dep->flags & DWC3_EP_ENABLED)) {
719 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
720 dep->name);
721 return 0;
722 }
723
724 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
725 dep->number >> 1,
726 (dep->number & 1) ? "in" : "out");
727
728 spin_lock_irqsave(&dwc->lock, flags);
729 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530730 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300731 spin_unlock_irqrestore(&dwc->lock, flags);
732
733 return ret;
734}
735
736static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
737 gfp_t gfp_flags)
738{
739 struct dwc3_request *req;
740 struct dwc3_ep *dep = to_dwc3_ep(ep);
741 struct dwc3 *dwc = dep->dwc;
742
743 req = kzalloc(sizeof(*req), gfp_flags);
744 if (!req) {
745 dev_err(dwc->dev, "not enough memory\n");
746 return NULL;
747 }
748
749 req->epnum = dep->number;
750 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300751
752 return &req->request;
753}
754
755static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
756 struct usb_request *request)
757{
758 struct dwc3_request *req = to_dwc3_request(request);
759
760 kfree(req);
761}
762
Felipe Balbic71fc372011-11-22 11:37:34 +0200763/**
764 * dwc3_prepare_one_trb - setup one TRB from one request
765 * @dep: endpoint for which this request is prepared
766 * @req: dwc3_request pointer
767 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200768static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200769 struct dwc3_request *req, dma_addr_t dma,
770 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200771{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200772 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200773 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200774
775 unsigned int cur_slot;
776
Felipe Balbieeb720f2011-11-28 12:46:59 +0200777 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
778 dep->name, req, (unsigned long long) dma,
779 length, last ? " last" : "",
780 chain ? " chain" : "");
781
Felipe Balbif6bafc62012-02-06 11:04:53 +0200782 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200783 cur_slot = dep->free_slot;
784 dep->free_slot++;
785
786 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530787 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200788 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530789 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200790
Felipe Balbieeb720f2011-11-28 12:46:59 +0200791 if (!req->trb) {
792 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200793 req->trb = trb;
794 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200795 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200796
Felipe Balbif6bafc62012-02-06 11:04:53 +0200797 trb->size = DWC3_TRB_SIZE_LENGTH(length);
798 trb->bpl = lower_32_bits(dma);
799 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200800
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200801 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200802 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200803 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200804 break;
805
806 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200807 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200808
Pratyush Ananddf023422012-05-21 12:42:54 +0530809 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200810 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200811 break;
812
813 case USB_ENDPOINT_XFER_BULK:
814 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200815 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200816 break;
817 default:
818 /*
819 * This is only possible with faulty memory because we
820 * checked it already :)
821 */
822 BUG();
823 }
824
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200825 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200826 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
827 trb->ctrl |= DWC3_TRB_CTRL_CSP;
828 } else {
829 if (chain)
830 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200831
Felipe Balbif6bafc62012-02-06 11:04:53 +0200832 if (last)
833 trb->ctrl |= DWC3_TRB_CTRL_LST;
834 }
835
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200836 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200837 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
838
839 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200840}
841
Felipe Balbi72246da2011-08-19 18:10:58 +0300842/*
843 * dwc3_prepare_trbs - setup TRBs from requests
844 * @dep: endpoint for which requests are being prepared
845 * @starting: true if the endpoint is idle and no requests are queued.
846 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800847 * The function goes through the requests list and sets up TRBs for the
848 * transfers. The function returns once there are no more TRBs available or
849 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200851static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300852{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200853 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200855 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200856 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300857
858 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
859
860 /* the first request must not be queued */
861 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200862
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200863 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200864 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200865 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
866 if (trbs_left > max)
867 trbs_left = max;
868 }
869
Felipe Balbi72246da2011-08-19 18:10:58 +0300870 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800871 * If busy & slot are equal than it is either full or empty. If we are
872 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 * full and don't do anything
874 */
875 if (!trbs_left) {
876 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200877 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 trbs_left = DWC3_TRB_NUM;
879 /*
880 * In case we start from scratch, we queue the ISOC requests
881 * starting from slot 1. This is done because we use ring
882 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800883 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 * after the first request so we start at slot 1 and have
885 * 7 requests proceed before we hit the first IOC.
886 * Other transfer types don't use the ring buffer and are
887 * processed from the first TRB until the last one. Since we
888 * don't wrap around we have to start at the beginning.
889 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200890 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300891 dep->busy_slot = 1;
892 dep->free_slot = 1;
893 } else {
894 dep->busy_slot = 0;
895 dep->free_slot = 0;
896 }
897 }
898
899 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200900 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200901 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300902
903 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200904 unsigned length;
905 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300906
Felipe Balbieeb720f2011-11-28 12:46:59 +0200907 if (req->request.num_mapped_sgs > 0) {
908 struct usb_request *request = &req->request;
909 struct scatterlist *sg = request->sg;
910 struct scatterlist *s;
911 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300912
Felipe Balbieeb720f2011-11-28 12:46:59 +0200913 for_each_sg(sg, s, request->num_mapped_sgs, i) {
914 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300915
Felipe Balbieeb720f2011-11-28 12:46:59 +0200916 length = sg_dma_len(s);
917 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Paul Zimmerman1d046792012-02-15 18:56:56 -0800919 if (i == (request->num_mapped_sgs - 1) ||
920 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200921 last_one = true;
922 chain = false;
923 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300924
Felipe Balbieeb720f2011-11-28 12:46:59 +0200925 trbs_left--;
926 if (!trbs_left)
927 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300928
Felipe Balbieeb720f2011-11-28 12:46:59 +0200929 if (last_one)
930 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300931
Felipe Balbieeb720f2011-11-28 12:46:59 +0200932 dwc3_prepare_one_trb(dep, req, dma, length,
933 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300934
Felipe Balbieeb720f2011-11-28 12:46:59 +0200935 if (last_one)
936 break;
937 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530938 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200940 dma = req->request.dma;
941 length = req->request.length;
942 trbs_left--;
943
944 if (!trbs_left)
945 last_one = 1;
946
947 /* Is this the last request? */
948 if (list_is_last(&req->list, &dep->request_list))
949 last_one = 1;
950
951 dwc3_prepare_one_trb(dep, req, dma, length,
952 last_one, false);
953
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530954 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200955 if (last_one)
956 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300957 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300959}
960
961static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
962 int start_new)
963{
964 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530965 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 struct dwc3 *dwc = dep->dwc;
967 int ret;
968 u32 cmd;
969
970 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
971 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
972 return -EBUSY;
973 }
974 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
975
976 /*
977 * If we are getting here after a short-out-packet we don't enqueue any
978 * new requests as we try to set the IOC bit only on the last request.
979 */
980 if (start_new) {
981 if (list_empty(&dep->req_queued))
982 dwc3_prepare_trbs(dep, start_new);
983
984 /* req points to the first request which will be sent */
985 req = next_request(&dep->req_queued);
986 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200987 dwc3_prepare_trbs(dep, start_new);
988
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800990 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200992 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300993 }
994 if (!req) {
995 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530996 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 return 0;
998 }
999
1000 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001001 params.param0 = upper_32_bits(req->trb_dma);
1002 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001003
1004 if (start_new)
1005 cmd = DWC3_DEPCMD_STARTTRANSFER;
1006 else
1007 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1008
1009 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1010 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1011 if (ret < 0) {
1012 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1013
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301014 if ((ret == -EAGAIN) && start_new &&
1015 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1016 /* If bit13 in Command complete event is set, software
1017 * must issue ENDTRANDFER command and wait for
1018 * Xfernotready event to queue the requests again.
1019 */
1020 if (!dep->resource_index) {
1021 dep->resource_index =
1022 dwc3_gadget_ep_get_transfer_index(dwc,
1023 dep->number);
1024 WARN_ON_ONCE(!dep->resource_index);
1025 }
1026 dwc3_stop_active_transfer(dwc, dep->number);
1027 list_for_each_entry_safe_reverse(req1, n,
1028 &dep->req_queued, list) {
1029 req1->trb = NULL;
1030 dwc3_gadget_move_request_list_front(req1);
1031 if (req->request.num_mapped_sgs)
1032 dep->busy_slot +=
1033 req->request.num_mapped_sgs;
1034 else
1035 dep->busy_slot++;
1036 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1037 DWC3_TRB_NUM - 1)
1038 dep->busy_slot++;
1039 }
1040 return ret;
1041 } else {
1042 /*
1043 * FIXME we need to iterate over the list of requests
1044 * here and stop, unmap, free and del each of the linked
1045 * requests instead of what we do now.
1046 */
1047 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001048 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301049 list_del(&req->list);
1050 return ret;
1051 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001052 }
1053
1054 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001055
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001056 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001057 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001058 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001059 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001060 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001061
Felipe Balbi72246da2011-08-19 18:10:58 +03001062 return 0;
1063}
1064
Pratyush Anand73939b02012-05-25 18:54:56 +05301065static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1066 struct dwc3_ep *dep, u32 cur_uf)
1067{
1068 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301069 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301070
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301071 dep->current_uf = cur_uf;
1072
Pratyush Anand73939b02012-05-25 18:54:56 +05301073 if (list_empty(&dep->request_list)) {
1074 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1075 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301076 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301077 return;
1078 }
1079
1080 /* 4 micro frames in the future */
1081 uf = cur_uf + dep->interval * 4;
1082
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301083 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1084 if (ret < 0)
1085 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301086}
1087
1088static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1089 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1090{
1091 u32 cur_uf, mask;
1092
1093 mask = ~(dep->interval - 1);
1094 cur_uf = event->parameters & mask;
1095
1096 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1097}
1098
Felipe Balbi72246da2011-08-19 18:10:58 +03001099static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1100{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001101 struct dwc3 *dwc = dep->dwc;
1102 int ret;
1103
Manu Gautamd2b99e12013-02-11 15:53:34 +05301104 if (req->request.status == -EINPROGRESS) {
1105 ret = -EBUSY;
1106 dev_err(dwc->dev, "%s: %p request already in queue",
1107 dep->name, req);
1108 return ret;
1109 }
1110
Felipe Balbi72246da2011-08-19 18:10:58 +03001111 req->request.actual = 0;
1112 req->request.status = -EINPROGRESS;
1113 req->direction = dep->direction;
1114 req->epnum = dep->number;
1115
1116 /*
1117 * We only add to our list of requests now and
1118 * start consuming the list once we get XferNotReady
1119 * IRQ.
1120 *
1121 * That way, we avoid doing anything that we don't need
1122 * to do now and defer it until the point we receive a
1123 * particular token from the Host side.
1124 *
1125 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001126 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001127 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001128 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1129 dep->direction);
1130 if (ret)
1131 return ret;
1132
Felipe Balbi72246da2011-08-19 18:10:58 +03001133 list_add_tail(&req->list, &dep->request_list);
1134
1135 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001136 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001137 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001138 * 1. XferNotReady with empty list of requests. We need to kick the
1139 * transfer here in that situation, otherwise we will be NAKing
1140 * forever. If we get XferNotReady before gadget driver has a
1141 * chance to queue a request, we will ACK the IRQ but won't be
1142 * able to receive the data until the next request is queued.
1143 * The following code is handling exactly that.
1144 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 */
1146 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301147 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001148
Pratyush Anandac417602012-08-30 12:21:43 +05301149 /*
1150 * If xfernotready is already elapsed and it is a case
1151 * of isoc transfer, then issue END TRANSFER, so that
1152 * you can receive xfernotready again and can have
1153 * notion of current microframe.
1154 */
1155 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301156 /* If xfernotready event is recieved before issuing
1157 * START TRANSFER command, don't issue END TRANSFER.
1158 * Rather start queueing the requests by issuing START
1159 * TRANSFER command.
1160 */
1161 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301162 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301163 else
1164 __dwc3_gadget_start_isoc(dwc, dep,
1165 dep->current_uf);
1166 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301167 return 0;
1168 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001169
Felipe Balbi46485a02012-06-06 12:00:50 +03001170 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301171 if (ret && ret != -EBUSY) {
1172 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001173 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1174 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301175 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001176 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001177
Felipe Balbi46485a02012-06-06 12:00:50 +03001178 /*
1179 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1180 * kick the transfer here after queuing a request, otherwise the
1181 * core may not see the modified TRB(s).
1182 */
1183 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301184 (dep->flags & DWC3_EP_BUSY) &&
1185 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001186 WARN_ON_ONCE(!dep->resource_index);
1187 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001188 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301189 if (ret && ret != -EBUSY) {
1190 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001191 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1192 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301193 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001194 }
1195
Felipe Balbi72246da2011-08-19 18:10:58 +03001196 return 0;
1197}
1198
1199static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1200 gfp_t gfp_flags)
1201{
1202 struct dwc3_request *req = to_dwc3_request(request);
1203 struct dwc3_ep *dep = to_dwc3_ep(ep);
1204 struct dwc3 *dwc = dep->dwc;
1205
1206 unsigned long flags;
1207
1208 int ret;
1209
Manu Gautam22f93042013-02-20 15:12:02 +05301210 spin_lock_irqsave(&dwc->lock, flags);
1211
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001212 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301213 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001214 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1215 request, ep->name);
1216 return -ESHUTDOWN;
1217 }
1218
1219 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1220 request, ep->name, request->length);
1221
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301222 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1223 "trying to queue unaligned request (%d)\n", request->length);
1224
Felipe Balbi72246da2011-08-19 18:10:58 +03001225 ret = __dwc3_gadget_ep_queue(dep, req);
1226 spin_unlock_irqrestore(&dwc->lock, flags);
1227
1228 return ret;
1229}
1230
1231static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1232 struct usb_request *request)
1233{
1234 struct dwc3_request *req = to_dwc3_request(request);
1235 struct dwc3_request *r = NULL;
1236
1237 struct dwc3_ep *dep = to_dwc3_ep(ep);
1238 struct dwc3 *dwc = dep->dwc;
1239
1240 unsigned long flags;
1241 int ret = 0;
1242
1243 spin_lock_irqsave(&dwc->lock, flags);
1244
1245 list_for_each_entry(r, &dep->request_list, list) {
1246 if (r == req)
1247 break;
1248 }
1249
1250 if (r != req) {
1251 list_for_each_entry(r, &dep->req_queued, list) {
1252 if (r == req)
1253 break;
1254 }
1255 if (r == req) {
1256 /* wait until it is processed */
1257 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301258 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001259 }
1260 dev_err(dwc->dev, "request %p was not queued to %s\n",
1261 request, ep->name);
1262 ret = -EINVAL;
1263 goto out0;
1264 }
1265
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301266out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301267 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001268 /* giveback the request */
1269 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1270
1271out0:
1272 spin_unlock_irqrestore(&dwc->lock, flags);
1273
1274 return ret;
1275}
1276
1277int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1278{
1279 struct dwc3_gadget_ep_cmd_params params;
1280 struct dwc3 *dwc = dep->dwc;
1281 int ret;
1282
1283 memset(&params, 0x00, sizeof(params));
1284
1285 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001286 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1287 DWC3_DEPCMD_SETSTALL, &params);
1288 if (ret)
1289 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1290 value ? "set" : "clear",
1291 dep->name);
1292 else
1293 dep->flags |= DWC3_EP_STALL;
1294 } else {
1295 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1296 DWC3_DEPCMD_CLEARSTALL, &params);
1297 if (ret)
1298 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1299 value ? "set" : "clear",
1300 dep->name);
1301 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301302 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001303 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001304
Felipe Balbi72246da2011-08-19 18:10:58 +03001305 return ret;
1306}
1307
1308static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1309{
1310 struct dwc3_ep *dep = to_dwc3_ep(ep);
1311 struct dwc3 *dwc = dep->dwc;
1312
1313 unsigned long flags;
1314
1315 int ret;
1316
1317 spin_lock_irqsave(&dwc->lock, flags);
1318
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001319 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001320 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1321 ret = -EINVAL;
1322 goto out;
1323 }
1324
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301325 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 ret = __dwc3_gadget_ep_set_halt(dep, value);
1327out:
1328 spin_unlock_irqrestore(&dwc->lock, flags);
1329
1330 return ret;
1331}
1332
1333static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1334{
1335 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001336 struct dwc3 *dwc = dep->dwc;
1337 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001338
Paul Zimmerman249a4562012-02-24 17:32:16 -08001339 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301340 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001341 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001342 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001343
Pratyush Anandeb840752012-06-25 22:40:43 +05301344 if (dep->number == 0 || dep->number == 1)
1345 return dwc3_gadget_ep0_set_halt(ep, 1);
1346 else
1347 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001348}
1349
1350/* -------------------------------------------------------------------------- */
1351
1352static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1353 .bLength = USB_DT_ENDPOINT_SIZE,
1354 .bDescriptorType = USB_DT_ENDPOINT,
1355 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1356};
1357
1358static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1359 .enable = dwc3_gadget_ep0_enable,
1360 .disable = dwc3_gadget_ep0_disable,
1361 .alloc_request = dwc3_gadget_ep_alloc_request,
1362 .free_request = dwc3_gadget_ep_free_request,
1363 .queue = dwc3_gadget_ep0_queue,
1364 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301365 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001366 .set_wedge = dwc3_gadget_ep_set_wedge,
1367};
1368
1369static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1370 .enable = dwc3_gadget_ep_enable,
1371 .disable = dwc3_gadget_ep_disable,
1372 .alloc_request = dwc3_gadget_ep_alloc_request,
1373 .free_request = dwc3_gadget_ep_free_request,
1374 .queue = dwc3_gadget_ep_queue,
1375 .dequeue = dwc3_gadget_ep_dequeue,
1376 .set_halt = dwc3_gadget_ep_set_halt,
1377 .set_wedge = dwc3_gadget_ep_set_wedge,
1378};
1379
1380/* -------------------------------------------------------------------------- */
1381
1382static int dwc3_gadget_get_frame(struct usb_gadget *g)
1383{
1384 struct dwc3 *dwc = gadget_to_dwc(g);
1385 u32 reg;
1386
1387 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1388 return DWC3_DSTS_SOFFN(reg);
1389}
1390
1391static int dwc3_gadget_wakeup(struct usb_gadget *g)
1392{
1393 struct dwc3 *dwc = gadget_to_dwc(g);
1394
1395 unsigned long timeout;
1396 unsigned long flags;
1397
1398 u32 reg;
1399
1400 int ret = 0;
1401
1402 u8 link_state;
1403 u8 speed;
1404
1405 spin_lock_irqsave(&dwc->lock, flags);
1406
1407 /*
1408 * According to the Databook Remote wakeup request should
1409 * be issued only when the device is in early suspend state.
1410 *
1411 * We can check that via USB Link State bits in DSTS register.
1412 */
1413 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1414
1415 speed = reg & DWC3_DSTS_CONNECTSPD;
1416 if (speed == DWC3_DSTS_SUPERSPEED) {
1417 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1418 ret = -EINVAL;
1419 goto out;
1420 }
1421
1422 link_state = DWC3_DSTS_USBLNKST(reg);
1423
1424 switch (link_state) {
1425 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1426 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1427 break;
1428 default:
1429 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1430 link_state);
1431 ret = -EINVAL;
1432 goto out;
1433 }
1434
Felipe Balbi8598bde2012-01-02 18:55:57 +02001435 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1436 if (ret < 0) {
1437 dev_err(dwc->dev, "failed to put link in Recovery\n");
1438 goto out;
1439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001440
Paul Zimmerman88df4272012-04-27 13:10:52 +03001441 /* Recent versions do this automatically */
1442 if (dwc->revision < DWC3_REVISION_194A) {
1443 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001444 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001445 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1446 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1447 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001448
Paul Zimmerman1d046792012-02-15 18:56:56 -08001449 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 timeout = jiffies + msecs_to_jiffies(100);
1451
Paul Zimmerman1d046792012-02-15 18:56:56 -08001452 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001453 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1454
1455 /* in HS, means ON */
1456 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1457 break;
1458 }
1459
1460 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1461 dev_err(dwc->dev, "failed to send remote wakeup\n");
1462 ret = -EINVAL;
1463 }
1464
1465out:
1466 spin_unlock_irqrestore(&dwc->lock, flags);
1467
1468 return ret;
1469}
1470
1471static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1472 int is_selfpowered)
1473{
1474 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001475 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001476
Paul Zimmerman249a4562012-02-24 17:32:16 -08001477 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001478 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001479 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001480
1481 return 0;
1482}
1483
Pratyush Anand77473f72012-07-02 10:21:55 +05301484static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001485{
1486 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001487 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001488
1489 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001490 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001491 if (dwc->revision <= DWC3_REVISION_187A) {
1492 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1493 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1494 }
1495
1496 if (dwc->revision >= DWC3_REVISION_194A)
1497 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1498 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001499 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001501 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001502
1503 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1504
1505 do {
1506 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1507 if (is_on) {
1508 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1509 break;
1510 } else {
1511 if (reg & DWC3_DSTS_DEVCTRLHLT)
1512 break;
1513 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 timeout--;
1515 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301516 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001517 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001518 } while (1);
1519
1520 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1521 dwc->gadget_driver
1522 ? dwc->gadget_driver->function : "no-function",
1523 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301524
1525 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001526}
1527
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301528static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1529{
1530 struct dwc3 *dwc = gadget_to_dwc(g);
1531 struct dwc3_otg *dotg = dwc->dotg;
1532
1533 if (dotg && dotg->otg.phy)
1534 return usb_phy_set_power(dotg->otg.phy, mA);
1535
1536 return -ENOTSUPP;
1537}
1538
Felipe Balbi72246da2011-08-19 18:10:58 +03001539static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1540{
1541 struct dwc3 *dwc = gadget_to_dwc(g);
1542 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301543 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001544
1545 is_on = !!is_on;
1546
1547 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001548
1549 dwc->softconnect = is_on;
1550
1551 if ((dwc->dotg && !dwc->vbus_active) ||
1552 !dwc->gadget_driver) {
1553
1554 spin_unlock_irqrestore(&dwc->lock, flags);
1555
1556 /*
1557 * Need to wait for vbus_session(on) from otg driver or to
1558 * the udc_start.
1559 */
1560 return 0;
1561 }
1562
Pratyush Anand77473f72012-07-02 10:21:55 +05301563 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001564
1565 spin_unlock_irqrestore(&dwc->lock, flags);
1566
Pratyush Anand77473f72012-07-02 10:21:55 +05301567 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001568}
1569
Jack Pham09e5c8e2013-03-06 18:53:43 -08001570static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1571
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001572static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1573{
1574 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1575 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301576 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001577
1578 if (!dwc->dotg)
1579 return -EPERM;
1580
1581 is_active = !!is_active;
1582
1583 spin_lock_irqsave(&dwc->lock, flags);
1584
1585 /* Mark that the vbus was powered */
1586 dwc->vbus_active = is_active;
1587
1588 /*
1589 * Check if upper level usb_gadget_driver was already registerd with
1590 * this udc controller driver (if dwc3_gadget_start was called)
1591 */
1592 if (dwc->gadget_driver && dwc->softconnect) {
1593 if (dwc->vbus_active) {
1594 /*
1595 * Both vbus was activated by otg and pullup was
1596 * signaled by the gadget driver.
1597 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301598 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001599 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301600 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001601 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001602 }
1603
1604 /*
1605 * Clearing run/stop bit might occur before disconnect event is seen.
1606 * Make sure to let gadget driver know in that case.
1607 */
1608 if (!dwc->vbus_active && dwc->start_config_issued) {
1609 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1610 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001611 }
1612
Felipe Balbi72246da2011-08-19 18:10:58 +03001613 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301614 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001615}
1616
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301617/* Required gadget re-initialization before switching to gadget in OTG mode */
1618void dwc3_gadget_restart(struct dwc3 *dwc)
1619{
1620 struct dwc3_ep *dep;
1621 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301622 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301623
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301624 /* Enable all but Start and End of Frame IRQs */
1625 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1626 DWC3_DEVTEN_CMDCMPLTEN |
1627 DWC3_DEVTEN_ERRTICERREN |
1628 DWC3_DEVTEN_WKUPEVTEN |
1629 DWC3_DEVTEN_ULSTCNGEN |
1630 DWC3_DEVTEN_CONNECTDONEEN |
1631 DWC3_DEVTEN_USBRSTEN |
1632 DWC3_DEVTEN_DISCONNEVTEN);
1633 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1634
1635 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1636 if (dwc->revision >= DWC3_REVISION_194A) {
1637 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1638 reg |= DWC3_DCFG_LPM_CAP;
1639 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1640
1641 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1642 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1643
1644 /* TODO: This should be configurable */
1645 reg |= DWC3_DCTL_HIRD_THRES(28);
1646
1647 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1648 }
1649
1650 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1651 reg &= ~(DWC3_DCFG_SPEED_MASK);
1652
1653 /**
1654 * WORKAROUND: DWC3 revision < 2.20a have an issue
1655 * which would cause metastability state on Run/Stop
1656 * bit if we try to force the IP to USB2-only mode.
1657 *
1658 * Because of that, we cannot configure the IP to any
1659 * speed other than the SuperSpeed
1660 *
1661 * Refers to:
1662 *
1663 * STAR#9000525659: Clock Domain Crossing on DCTL in
1664 * USB 2.0 Mode
1665 */
1666 if (dwc->revision < DWC3_REVISION_220A)
1667 reg |= DWC3_DCFG_SUPERSPEED;
1668 else
1669 reg |= dwc->maximum_speed;
1670 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1671
1672 dwc->start_config_issued = false;
1673
1674 /* Start with SuperSpeed Default */
1675 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301676
1677 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301678 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301679 dep = dwc->eps[0];
1680 dep->flags = 0;
1681 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1682 if (ret) {
1683 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1684 return;
1685 }
1686
1687 dep = dwc->eps[1];
1688 dep->flags = 0;
1689 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1690 if (ret) {
1691 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1692 return;
1693 }
1694
1695 /* begin to receive SETUP packets */
1696 dwc->ep0state = EP0_SETUP_PHASE;
1697 dwc3_ep0_out_start(dwc);
1698}
1699
Felipe Balbi72246da2011-08-19 18:10:58 +03001700static int dwc3_gadget_start(struct usb_gadget *g,
1701 struct usb_gadget_driver *driver)
1702{
1703 struct dwc3 *dwc = gadget_to_dwc(g);
1704 struct dwc3_ep *dep;
1705 unsigned long flags;
1706 int ret = 0;
1707 u32 reg;
1708
1709 spin_lock_irqsave(&dwc->lock, flags);
1710
1711 if (dwc->gadget_driver) {
1712 dev_err(dwc->dev, "%s is already bound to %s\n",
1713 dwc->gadget.name,
1714 dwc->gadget_driver->driver.name);
1715 ret = -EBUSY;
1716 goto err0;
1717 }
1718
1719 dwc->gadget_driver = driver;
1720 dwc->gadget.dev.driver = &driver->driver;
1721
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1723 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001724
1725 /**
1726 * WORKAROUND: DWC3 revision < 2.20a have an issue
1727 * which would cause metastability state on Run/Stop
1728 * bit if we try to force the IP to USB2-only mode.
1729 *
1730 * Because of that, we cannot configure the IP to any
1731 * speed other than the SuperSpeed
1732 *
1733 * Refers to:
1734 *
1735 * STAR#9000525659: Clock Domain Crossing on DCTL in
1736 * USB 2.0 Mode
1737 */
1738 if (dwc->revision < DWC3_REVISION_220A)
1739 reg |= DWC3_DCFG_SUPERSPEED;
1740 else
1741 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001742 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1743
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001744 dwc->start_config_issued = false;
1745
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 /* Start with SuperSpeed Default */
1747 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1748
1749 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001750 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001751 if (ret) {
1752 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1753 goto err0;
1754 }
1755
1756 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001757 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 if (ret) {
1759 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1760 goto err1;
1761 }
1762
1763 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001764 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 dwc3_ep0_out_start(dwc);
1766
1767 spin_unlock_irqrestore(&dwc->lock, flags);
1768
1769 return 0;
1770
1771err1:
1772 __dwc3_gadget_ep_disable(dwc->eps[0]);
1773
1774err0:
1775 spin_unlock_irqrestore(&dwc->lock, flags);
1776
1777 return ret;
1778}
1779
1780static int dwc3_gadget_stop(struct usb_gadget *g,
1781 struct usb_gadget_driver *driver)
1782{
1783 struct dwc3 *dwc = gadget_to_dwc(g);
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(&dwc->lock, flags);
1787
1788 __dwc3_gadget_ep_disable(dwc->eps[0]);
1789 __dwc3_gadget_ep_disable(dwc->eps[1]);
1790
1791 dwc->gadget_driver = NULL;
1792 dwc->gadget.dev.driver = NULL;
1793
1794 spin_unlock_irqrestore(&dwc->lock, flags);
1795
1796 return 0;
1797}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001798
Felipe Balbi72246da2011-08-19 18:10:58 +03001799static const struct usb_gadget_ops dwc3_gadget_ops = {
1800 .get_frame = dwc3_gadget_get_frame,
1801 .wakeup = dwc3_gadget_wakeup,
1802 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001803 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301804 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001805 .pullup = dwc3_gadget_pullup,
1806 .udc_start = dwc3_gadget_start,
1807 .udc_stop = dwc3_gadget_stop,
1808};
1809
1810/* -------------------------------------------------------------------------- */
1811
1812static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1813{
1814 struct dwc3_ep *dep;
1815 u8 epnum;
1816
1817 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1818
1819 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1820 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1821 if (!dep) {
1822 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1823 epnum);
1824 return -ENOMEM;
1825 }
1826
1827 dep->dwc = dwc;
1828 dep->number = epnum;
1829 dwc->eps[epnum] = dep;
1830
1831 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1832 (epnum & 1) ? "in" : "out");
1833 dep->endpoint.name = dep->name;
1834 dep->direction = (epnum & 1);
1835
1836 if (epnum == 0 || epnum == 1) {
1837 dep->endpoint.maxpacket = 512;
1838 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1839 if (!epnum)
1840 dwc->gadget.ep0 = &dep->endpoint;
1841 } else {
1842 int ret;
1843
1844 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001845 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1847 list_add_tail(&dep->endpoint.ep_list,
1848 &dwc->gadget.ep_list);
1849
1850 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001851 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001852 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001854
Felipe Balbi72246da2011-08-19 18:10:58 +03001855 INIT_LIST_HEAD(&dep->request_list);
1856 INIT_LIST_HEAD(&dep->req_queued);
1857 }
1858
1859 return 0;
1860}
1861
1862static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1863{
1864 struct dwc3_ep *dep;
1865 u8 epnum;
1866
1867 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1868 dep = dwc->eps[epnum];
1869 dwc3_free_trb_pool(dep);
1870
1871 if (epnum != 0 && epnum != 1)
1872 list_del(&dep->endpoint.ep_list);
1873
1874 kfree(dep);
1875 }
1876}
1877
1878static void dwc3_gadget_release(struct device *dev)
1879{
1880 dev_dbg(dev, "%s\n", __func__);
1881}
1882
1883/* -------------------------------------------------------------------------- */
1884static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1885 const struct dwc3_event_depevt *event, int status)
1886{
1887 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001888 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 unsigned int count;
1890 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301891 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001892
1893 do {
1894 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001895 if (!req) {
1896 WARN_ON_ONCE(1);
1897 return 1;
1898 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001899
Felipe Balbif6bafc62012-02-06 11:04:53 +02001900 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001901
Felipe Balbif6bafc62012-02-06 11:04:53 +02001902 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001903 /*
1904 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001905 * 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
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001909 * fixes the root cause instead of looking away :)
1910 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001911 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1912 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001913 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001914
1915 if (dep->direction) {
1916 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301917 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1918 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1919 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1920 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301921 /*
1922 * If missed isoc occurred and there is
1923 * no request queued then issue END
1924 * TRANSFER, so that core generates
1925 * next xfernotready and we will issue
1926 * a fresh START TRANSFER.
1927 * If there are still queued request
1928 * then wait, do not issue either END
1929 * or UPDATE TRANSFER, just attach next
1930 * request in request_list during
1931 * giveback.If any future queued request
1932 * is successfully transferred then we
1933 * will issue UPDATE TRANSFER for all
1934 * request in the request_list.
1935 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301936 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301937 dbg_event(dep->number, "MISSED ISOC",
1938 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05301939 } else {
1940 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1941 dep->name);
1942 status = -ECONNRESET;
1943 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301944 } else {
1945 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001946 }
1947 } else {
1948 if (count && (event->status & DEPEVT_STATUS_SHORT))
1949 s_pkt = 1;
1950 }
1951
1952 /*
1953 * We assume here we will always receive the entire data block
1954 * which we should receive. Meaning, if we program RX to
1955 * receive 4K but we receive only 2K, we assume that's all we
1956 * should receive and we simply bounce the request back to the
1957 * gadget driver for further processing.
1958 */
1959 req->request.actual += req->request.length - count;
1960 dwc3_gadget_giveback(dep, req, status);
1961 if (s_pkt)
1962 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001963 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301964 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1965 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001966 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001967 if ((event->status & DEPEVT_STATUS_IOC) &&
1968 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001969 break;
1970 } while (1);
1971
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301972 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1973 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301974 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301975 /*
1976 * If there is no entry in request list then do
1977 * not issue END TRANSFER now. Just set PENDING
1978 * flag, so that END TRANSFER is issued when an
1979 * entry is added into request list.
1980 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301981 dep->flags |= DWC3_EP_PENDING_REQUEST;
1982 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301983 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301984 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05301985 return 1;
1986 }
1987
Felipe Balbif6bafc62012-02-06 11:04:53 +02001988 if ((event->status & DEPEVT_STATUS_IOC) &&
1989 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001990 return 0;
1991 return 1;
1992}
1993
1994static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1995 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1996 int start_new)
1997{
1998 unsigned status = 0;
1999 int clean_busy;
2000
2001 if (event->status & DEPEVT_STATUS_BUSERR)
2002 status = -ECONNRESET;
2003
Paul Zimmerman1d046792012-02-15 18:56:56 -08002004 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002005 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002007
2008 /*
2009 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2010 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2011 */
2012 if (dwc->revision < DWC3_REVISION_183A) {
2013 u32 reg;
2014 int i;
2015
2016 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002017 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002018
2019 if (!(dep->flags & DWC3_EP_ENABLED))
2020 continue;
2021
2022 if (!list_empty(&dep->req_queued))
2023 return;
2024 }
2025
2026 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2027 reg |= dwc->u1u2;
2028 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2029
2030 dwc->u1u2 = 0;
2031 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002032}
2033
Felipe Balbi72246da2011-08-19 18:10:58 +03002034static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2035 const struct dwc3_event_depevt *event)
2036{
2037 struct dwc3_ep *dep;
2038 u8 epnum = event->endpoint_number;
2039
2040 dep = dwc->eps[epnum];
2041
Felipe Balbia09be0a2012-06-06 09:19:35 +03002042 if (!(dep->flags & DWC3_EP_ENABLED))
2043 return;
2044
Felipe Balbi72246da2011-08-19 18:10:58 +03002045 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2046 dwc3_ep_event_string(event->endpoint_event));
2047
2048 if (epnum == 0 || epnum == 1) {
2049 dwc3_ep0_interrupt(dwc, event);
2050 return;
2051 }
2052
2053 switch (event->endpoint_event) {
2054 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002055 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002056
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002057 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002058 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2059 dep->name);
2060 return;
2061 }
2062
2063 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2064 break;
2065 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002066 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002067 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2068 dep->name);
2069 return;
2070 }
2071
2072 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2073 break;
2074 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002075 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002076 dwc3_gadget_start_isoc(dwc, dep, event);
2077 } else {
2078 int ret;
2079
2080 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002081 dep->name, event->status &
2082 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002083 ? "Transfer Active"
2084 : "Transfer Not Active");
2085
2086 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2087 if (!ret || ret == -EBUSY)
2088 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302089 else
2090 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002091
2092 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2093 dep->name);
2094 }
2095
2096 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002097 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002098 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002099 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2100 dep->name);
2101 return;
2102 }
2103
2104 switch (event->status) {
2105 case DEPEVT_STREAMEVT_FOUND:
2106 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2107 event->parameters);
2108
2109 break;
2110 case DEPEVT_STREAMEVT_NOTFOUND:
2111 /* FALLTHROUGH */
2112 default:
2113 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2114 }
2115 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002116 case DWC3_DEPEVT_RXTXFIFOEVT:
2117 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2118 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002120 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002121 break;
2122 }
2123}
2124
2125static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2126{
2127 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2128 spin_unlock(&dwc->lock);
2129 dwc->gadget_driver->disconnect(&dwc->gadget);
2130 spin_lock(&dwc->lock);
2131 }
2132}
2133
2134static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2135{
2136 struct dwc3_ep *dep;
2137 struct dwc3_gadget_ep_cmd_params params;
2138 u32 cmd;
2139 int ret;
2140
2141 dep = dwc->eps[epnum];
2142
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002143 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302144 return;
2145
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302146 /*
2147 * NOTICE: We are violating what the Databook says about the
2148 * EndTransfer command. Ideally we would _always_ wait for the
2149 * EndTransfer Command Completion IRQ, but that's causing too
2150 * much trouble synchronizing between us and gadget driver.
2151 *
2152 * We have discussed this with the IP Provider and it was
2153 * suggested to giveback all requests here, but give HW some
2154 * extra time to synchronize with the interconnect. We're using
2155 * an arbitraty 100us delay for that.
2156 *
2157 * Note also that a similar handling was tested by Synopsys
2158 * (thanks a lot Paul) and nothing bad has come out of it.
2159 * In short, what we're doing is:
2160 *
2161 * - Issue EndTransfer WITH CMDIOC bit set
2162 * - Wait 100us
2163 */
2164
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302165 cmd = DWC3_DEPCMD_ENDTRANSFER;
2166 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002167 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302168 memset(&params, 0, sizeof(params));
2169 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2170 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002171 dep->resource_index = 0;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302172
2173 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002174}
2175
2176static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2177{
2178 u32 epnum;
2179
2180 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2181 struct dwc3_ep *dep;
2182
2183 dep = dwc->eps[epnum];
2184 if (!(dep->flags & DWC3_EP_ENABLED))
2185 continue;
2186
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002187 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002188 }
2189}
2190
2191static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2192{
2193 u32 epnum;
2194
2195 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2196 struct dwc3_ep *dep;
2197 struct dwc3_gadget_ep_cmd_params params;
2198 int ret;
2199
2200 dep = dwc->eps[epnum];
2201
2202 if (!(dep->flags & DWC3_EP_STALL))
2203 continue;
2204
2205 dep->flags &= ~DWC3_EP_STALL;
2206
2207 memset(&params, 0, sizeof(params));
2208 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2209 DWC3_DEPCMD_CLEARSTALL, &params);
2210 WARN_ON_ONCE(ret);
2211 }
2212}
2213
2214static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2215{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002216 int reg;
2217
Felipe Balbi72246da2011-08-19 18:10:58 +03002218 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002219
2220 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2221 reg &= ~DWC3_DCTL_INITU1ENA;
2222 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2223
2224 reg &= ~DWC3_DCTL_INITU2ENA;
2225 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002226
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302227 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002228 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002229 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002230
2231 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002232 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002233}
2234
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002235static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002236{
2237 u32 reg;
2238
2239 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2240
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002241 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002242 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002243 else
2244 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002245
2246 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2247}
2248
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002249static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002250{
2251 u32 reg;
2252
2253 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2254
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002255 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002256 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002257 else
2258 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002259
2260 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2261}
2262
2263static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2264{
2265 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302266 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002267
2268 dev_vdbg(dwc->dev, "%s\n", __func__);
2269
Felipe Balbidf62df52011-10-14 15:11:49 +03002270 /*
2271 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2272 * would cause a missing Disconnect Event if there's a
2273 * pending Setup Packet in the FIFO.
2274 *
2275 * There's no suggested workaround on the official Bug
2276 * report, which states that "unless the driver/application
2277 * is doing any special handling of a disconnect event,
2278 * there is no functional issue".
2279 *
2280 * Unfortunately, it turns out that we _do_ some special
2281 * handling of a disconnect event, namely complete all
2282 * pending transfers, notify gadget driver of the
2283 * disconnection, and so on.
2284 *
2285 * Our suggested workaround is to follow the Disconnect
2286 * Event steps here, instead, based on a setup_packet_pending
2287 * flag. Such flag gets set whenever we have a XferNotReady
2288 * event on EP0 and gets cleared on XferComplete for the
2289 * same endpoint.
2290 *
2291 * Refers to:
2292 *
2293 * STAR#9000466709: RTL: Device : Disconnect event not
2294 * generated if setup packet pending in FIFO
2295 */
2296 if (dwc->revision < DWC3_REVISION_188A) {
2297 if (dwc->setup_packet_pending)
2298 dwc3_gadget_disconnect_interrupt(dwc);
2299 }
2300
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302301 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002302 /* after reset -> Default State */
2303 dwc->dev_state = DWC3_DEFAULT_STATE;
2304
Paul Zimmerman88df4272012-04-27 13:10:52 +03002305 /* Recent versions support automatic phy suspend and don't need this */
2306 if (dwc->revision < DWC3_REVISION_194A) {
2307 /* Resume PHYs */
2308 dwc3_gadget_usb2_phy_suspend(dwc, false);
2309 dwc3_gadget_usb3_phy_suspend(dwc, false);
2310 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002311
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302312 if (dotg && dotg->otg.phy)
2313 usb_phy_set_power(dotg->otg.phy, 0);
2314
Felipe Balbi72246da2011-08-19 18:10:58 +03002315 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2316 dwc3_disconnect_gadget(dwc);
2317
2318 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2319 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2320 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002321 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002322
2323 dwc3_stop_active_transfers(dwc);
2324 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002325 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002326
2327 /* Reset device address to zero */
2328 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2329 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2330 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002331}
2332
2333static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2334{
2335 u32 reg;
2336 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2337
2338 /*
2339 * We change the clock only at SS but I dunno why I would want to do
2340 * this. Maybe it becomes part of the power saving plan.
2341 */
2342
2343 if (speed != DWC3_DSTS_SUPERSPEED)
2344 return;
2345
2346 /*
2347 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2348 * each time on Connect Done.
2349 */
2350 if (!usb30_clock)
2351 return;
2352
2353 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2354 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2355 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2356}
2357
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002358static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002359{
2360 switch (speed) {
2361 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002362 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 break;
2364 case USB_SPEED_HIGH:
2365 case USB_SPEED_FULL:
2366 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002367 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002368 break;
2369 }
2370}
2371
2372static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2373{
2374 struct dwc3_gadget_ep_cmd_params params;
2375 struct dwc3_ep *dep;
2376 int ret;
2377 u32 reg;
2378 u8 speed;
2379
2380 dev_vdbg(dwc->dev, "%s\n", __func__);
2381
2382 memset(&params, 0x00, sizeof(params));
2383
Felipe Balbi72246da2011-08-19 18:10:58 +03002384 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2385 speed = reg & DWC3_DSTS_CONNECTSPD;
2386 dwc->speed = speed;
2387
2388 dwc3_update_ram_clk_sel(dwc, speed);
2389
2390 switch (speed) {
2391 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002392 /*
2393 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2394 * would cause a missing USB3 Reset event.
2395 *
2396 * In such situations, we should force a USB3 Reset
2397 * event by calling our dwc3_gadget_reset_interrupt()
2398 * routine.
2399 *
2400 * Refers to:
2401 *
2402 * STAR#9000483510: RTL: SS : USB3 reset event may
2403 * not be generated always when the link enters poll
2404 */
2405 if (dwc->revision < DWC3_REVISION_190A)
2406 dwc3_gadget_reset_interrupt(dwc);
2407
Felipe Balbi72246da2011-08-19 18:10:58 +03002408 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2409 dwc->gadget.ep0->maxpacket = 512;
2410 dwc->gadget.speed = USB_SPEED_SUPER;
2411 break;
2412 case DWC3_DCFG_HIGHSPEED:
2413 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2414 dwc->gadget.ep0->maxpacket = 64;
2415 dwc->gadget.speed = USB_SPEED_HIGH;
2416 break;
2417 case DWC3_DCFG_FULLSPEED2:
2418 case DWC3_DCFG_FULLSPEED1:
2419 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2420 dwc->gadget.ep0->maxpacket = 64;
2421 dwc->gadget.speed = USB_SPEED_FULL;
2422 break;
2423 case DWC3_DCFG_LOWSPEED:
2424 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2425 dwc->gadget.ep0->maxpacket = 8;
2426 dwc->gadget.speed = USB_SPEED_LOW;
2427 break;
2428 }
2429
Paul Zimmerman88df4272012-04-27 13:10:52 +03002430 /* Recent versions support automatic phy suspend and don't need this */
2431 if (dwc->revision < DWC3_REVISION_194A) {
2432 /* Suspend unneeded PHY */
2433 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2434 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002435
2436 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002437 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 if (ret) {
2439 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2440 return;
2441 }
2442
2443 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002444 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002445 if (ret) {
2446 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2447 return;
2448 }
2449
2450 /*
2451 * Configure PHY via GUSB3PIPECTLn if required.
2452 *
2453 * Update GTXFIFOSIZn
2454 *
2455 * In both cases reset values should be sufficient.
2456 */
2457}
2458
2459static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2460{
2461 dev_vdbg(dwc->dev, "%s\n", __func__);
2462
2463 /*
2464 * TODO take core out of low power mode when that's
2465 * implemented.
2466 */
2467
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302468 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002469 dwc->gadget_driver->resume(&dwc->gadget);
2470}
2471
2472static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2473 unsigned int evtinfo)
2474{
Felipe Balbifae2b902011-10-14 13:00:30 +03002475 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2476
2477 /*
2478 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2479 * on the link partner, the USB session might do multiple entry/exit
2480 * of low power states before a transfer takes place.
2481 *
2482 * Due to this problem, we might experience lower throughput. The
2483 * suggested workaround is to disable DCTL[12:9] bits if we're
2484 * transitioning from U1/U2 to U0 and enable those bits again
2485 * after a transfer completes and there are no pending transfers
2486 * on any of the enabled endpoints.
2487 *
2488 * This is the first half of that workaround.
2489 *
2490 * Refers to:
2491 *
2492 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2493 * core send LGO_Ux entering U0
2494 */
2495 if (dwc->revision < DWC3_REVISION_183A) {
2496 if (next == DWC3_LINK_STATE_U0) {
2497 u32 u1u2;
2498 u32 reg;
2499
2500 switch (dwc->link_state) {
2501 case DWC3_LINK_STATE_U1:
2502 case DWC3_LINK_STATE_U2:
2503 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2504 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2505 | DWC3_DCTL_ACCEPTU2ENA
2506 | DWC3_DCTL_INITU1ENA
2507 | DWC3_DCTL_ACCEPTU1ENA);
2508
2509 if (!dwc->u1u2)
2510 dwc->u1u2 = reg & u1u2;
2511
2512 reg &= ~u1u2;
2513
2514 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2515 break;
2516 default:
2517 /* do nothing */
2518 break;
2519 }
2520 }
2521 }
2522
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302523 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302524 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2525 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302526 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302527 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302528 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302529 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302530 dwc->gadget_driver->suspend(&dwc->gadget);
2531 }
2532
Felipe Balbifae2b902011-10-14 13:00:30 +03002533 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002534
2535 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002536}
2537
2538static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2539 const struct dwc3_event_devt *event)
2540{
2541 switch (event->type) {
2542 case DWC3_DEVICE_EVENT_DISCONNECT:
2543 dwc3_gadget_disconnect_interrupt(dwc);
2544 break;
2545 case DWC3_DEVICE_EVENT_RESET:
2546 dwc3_gadget_reset_interrupt(dwc);
2547 break;
2548 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2549 dwc3_gadget_conndone_interrupt(dwc);
2550 break;
2551 case DWC3_DEVICE_EVENT_WAKEUP:
2552 dwc3_gadget_wakeup_interrupt(dwc);
2553 break;
2554 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2555 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2556 break;
2557 case DWC3_DEVICE_EVENT_EOPF:
2558 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2559 break;
2560 case DWC3_DEVICE_EVENT_SOF:
2561 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2562 break;
2563 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302564 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 dev_vdbg(dwc->dev, "Erratic Error\n");
2566 break;
2567 case DWC3_DEVICE_EVENT_CMD_CMPL:
2568 dev_vdbg(dwc->dev, "Command Complete\n");
2569 break;
2570 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302571 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302573 /*
2574 * Controllers prior to 2.30a revision has a bug where
2575 * Overflow Event may overwrite an unacknowledged event
2576 * in the event buffer. The severity of the issue depends
2577 * on the overwritten event type. Add a warning message
2578 * saying that an event is overwritten.
2579 *
2580 * TODO: In future we may need to see if we can re-enumerate
2581 * with host.
2582 */
2583 if (dwc->revision < DWC3_REVISION_230A)
2584 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002585 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302586 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2587 /*
2588 * Controllers prior to 2.30a revision has a bug, due to which
2589 * a vendor device test LMP event can not be filtered. But
2590 * this event is not handled in the current code. This is a
2591 * special event and 8 bytes of data will follow the event.
2592 * Handling this event is tricky when event buffer is almost
2593 * full. Moreover this event will not occur in normal scenario
2594 * and can only happen with special hosts in testing scenarios.
2595 * Add a warning message to indicate that this event is received
2596 * which means that event buffer might have corrupted.
2597 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302598 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302599 if (dwc->revision < DWC3_REVISION_230A)
2600 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2601 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002602 default:
2603 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2604 }
2605}
2606
2607static void dwc3_process_event_entry(struct dwc3 *dwc,
2608 const union dwc3_event *event)
2609{
2610 /* Endpoint IRQ, handle it and return early */
2611 if (event->type.is_devspec == 0) {
2612 /* depevt */
2613 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2614 }
2615
2616 switch (event->type.type) {
2617 case DWC3_EVENT_TYPE_DEV:
2618 dwc3_gadget_interrupt(dwc, &event->devt);
2619 break;
2620 /* REVISIT what to do with Carkit and I2C events ? */
2621 default:
2622 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2623 }
2624}
2625
2626static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2627{
2628 struct dwc3_event_buffer *evt;
2629 int left;
2630 u32 count;
2631
2632 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2633 count &= DWC3_GEVNTCOUNT_MASK;
2634 if (!count)
2635 return IRQ_NONE;
2636
2637 evt = dwc->ev_buffs[buf];
2638 left = count;
2639
2640 while (left > 0) {
2641 union dwc3_event event;
2642
Felipe Balbid70d8442012-02-06 13:40:17 +02002643 event.raw = *(u32 *) (evt->buf + evt->lpos);
2644
Felipe Balbi72246da2011-08-19 18:10:58 +03002645 dwc3_process_event_entry(dwc, &event);
2646 /*
2647 * XXX we wrap around correctly to the next entry as almost all
2648 * entries are 4 bytes in size. There is one entry which has 12
2649 * bytes which is a regular entry followed by 8 bytes data. ATM
2650 * I don't know how things are organized if were get next to the
2651 * a boundary so I worry about that once we try to handle that.
2652 */
2653 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2654 left -= 4;
2655
2656 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2657 }
2658
2659 return IRQ_HANDLED;
2660}
2661
2662static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2663{
2664 struct dwc3 *dwc = _dwc;
2665 int i;
2666 irqreturn_t ret = IRQ_NONE;
2667
2668 spin_lock(&dwc->lock);
2669
Felipe Balbi9f622b22011-10-12 10:31:04 +03002670 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002671 irqreturn_t status;
2672
2673 status = dwc3_process_event_buf(dwc, i);
2674 if (status == IRQ_HANDLED)
2675 ret = status;
2676 }
2677
2678 spin_unlock(&dwc->lock);
2679
2680 return ret;
2681}
2682
2683/**
2684 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002685 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002686 *
2687 * Returns 0 on success otherwise negative errno.
2688 */
2689int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2690{
2691 u32 reg;
2692 int ret;
2693 int irq;
2694
2695 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2696 &dwc->ctrl_req_addr, GFP_KERNEL);
2697 if (!dwc->ctrl_req) {
2698 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2699 ret = -ENOMEM;
2700 goto err0;
2701 }
2702
2703 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2704 &dwc->ep0_trb_addr, GFP_KERNEL);
2705 if (!dwc->ep0_trb) {
2706 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2707 ret = -ENOMEM;
2708 goto err1;
2709 }
2710
Felipe Balbib0791fb2012-05-04 12:58:14 +03002711 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002712 if (!dwc->setup_buf) {
2713 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2714 ret = -ENOMEM;
2715 goto err2;
2716 }
2717
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002718 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002719 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2720 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002721 if (!dwc->ep0_bounce) {
2722 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2723 ret = -ENOMEM;
2724 goto err3;
2725 }
2726
Felipe Balbi72246da2011-08-19 18:10:58 +03002727 dev_set_name(&dwc->gadget.dev, "gadget");
2728
2729 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302730 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002731 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2732 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002733 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002734
2735 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2736
2737 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2738 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2739 dwc->gadget.dev.release = dwc3_gadget_release;
2740 dwc->gadget.name = "dwc3-gadget";
2741
2742 /*
2743 * REVISIT: Here we should clear all pending IRQs to be
2744 * sure we're starting from a well known location.
2745 */
2746
2747 ret = dwc3_gadget_init_endpoints(dwc);
2748 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002749 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002750
2751 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2752
2753 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2754 "dwc3", dwc);
2755 if (ret) {
2756 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2757 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002758 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002759 }
2760
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002761 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2762 reg |= DWC3_DCFG_LPM_CAP;
2763 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2764
Felipe Balbi72246da2011-08-19 18:10:58 +03002765 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302766 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002767 DWC3_DEVTEN_CMDCMPLTEN |
2768 DWC3_DEVTEN_ERRTICERREN |
2769 DWC3_DEVTEN_WKUPEVTEN |
2770 DWC3_DEVTEN_ULSTCNGEN |
2771 DWC3_DEVTEN_CONNECTDONEEN |
2772 DWC3_DEVTEN_USBRSTEN |
2773 DWC3_DEVTEN_DISCONNEVTEN);
2774 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2775
Paul Zimmerman88df4272012-04-27 13:10:52 +03002776 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2777 if (dwc->revision >= DWC3_REVISION_194A) {
2778 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2779 reg |= DWC3_DCFG_LPM_CAP;
2780 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2781
2782 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2783 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2784
2785 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302786 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002787
2788 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2789
Pratyush Anand50ed8342012-06-06 19:36:17 +05302790 dwc3_gadget_usb2_phy_suspend(dwc, false);
2791 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002792 }
2793
Felipe Balbi72246da2011-08-19 18:10:58 +03002794 ret = device_register(&dwc->gadget.dev);
2795 if (ret) {
2796 dev_err(dwc->dev, "failed to register gadget device\n");
2797 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002798 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002799 }
2800
2801 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2802 if (ret) {
2803 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002804 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 }
2806
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002807 if (dwc->dotg) {
2808 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2809 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2810 if (ret) {
2811 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2812 goto err7;
2813 }
Manu Gautamb5067272012-07-02 09:53:41 +05302814 } else {
2815 pm_runtime_no_callbacks(&dwc->gadget.dev);
2816 pm_runtime_set_active(&dwc->gadget.dev);
2817 pm_runtime_enable(&dwc->gadget.dev);
2818 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002819 }
2820
Felipe Balbi72246da2011-08-19 18:10:58 +03002821 return 0;
2822
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002823err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002824 device_unregister(&dwc->gadget.dev);
2825
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002826err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002827 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2828 free_irq(irq, dwc);
2829
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002830err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002831 dwc3_gadget_free_endpoints(dwc);
2832
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002833err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002834 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2835 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002836
Felipe Balbi72246da2011-08-19 18:10:58 +03002837err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002838 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002839
2840err2:
2841 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2842 dwc->ep0_trb, dwc->ep0_trb_addr);
2843
2844err1:
2845 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2846 dwc->ctrl_req, dwc->ctrl_req_addr);
2847
2848err0:
2849 return ret;
2850}
2851
2852void dwc3_gadget_exit(struct dwc3 *dwc)
2853{
2854 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002855
Manu Gautamb5067272012-07-02 09:53:41 +05302856 if (dwc->dotg) {
2857 pm_runtime_put(&dwc->gadget.dev);
2858 pm_runtime_disable(&dwc->gadget.dev);
2859 }
2860
Felipe Balbi72246da2011-08-19 18:10:58 +03002861 usb_del_gadget_udc(&dwc->gadget);
2862 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2863
2864 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2865 free_irq(irq, dwc);
2866
Felipe Balbi72246da2011-08-19 18:10:58 +03002867 dwc3_gadget_free_endpoints(dwc);
2868
Felipe Balbib0791fb2012-05-04 12:58:14 +03002869 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2870 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002871
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002872 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002873
2874 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2875 dwc->ep0_trb, dwc->ep0_trb_addr);
2876
2877 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2878 dwc->ctrl_req, dwc->ctrl_req_addr);
2879
2880 device_unregister(&dwc->gadget.dev);
2881}