blob: a3d3c7c358307df6cc59a883c91abcfc302d9e96 [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"
56#include "io.h"
57
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020058/**
59 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
60 * @dwc: pointer to our context structure
61 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
62 *
63 * Caller should take care of locking. This function will
64 * return 0 on success or -EINVAL if wrong Test Selector
65 * is passed
66 */
67int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
68{
69 u32 reg;
70
71 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
72 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
73
74 switch (mode) {
75 case TEST_J:
76 case TEST_K:
77 case TEST_SE0_NAK:
78 case TEST_PACKET:
79 case TEST_FORCE_EN:
80 reg |= mode << 1;
81 break;
82 default:
83 return -EINVAL;
84 }
85
86 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
87
88 return 0;
89}
90
Felipe Balbi8598bde2012-01-02 18:55:57 +020091/**
92 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
104 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
105 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
106
107 /* set requested state */
108 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
109 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
110
111 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300112 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200113 while (--retries) {
114 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
115
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 if (DWC3_DSTS_USBLNKST(reg) == state)
117 return 0;
118
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800119 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200120 }
121
122 dev_vdbg(dwc->dev, "link state change request timed out\n");
123
124 return -ETIMEDOUT;
125}
126
Felipe Balbi457e84b2012-01-18 18:04:09 +0200127/**
128 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
129 * @dwc: pointer to our context structure
130 *
131 * This function will a best effort FIFO allocation in order
132 * to improve FIFO usage and throughput, while still allowing
133 * us to enable as many endpoints as possible.
134 *
135 * Keep in mind that this operation will be highly dependent
136 * on the configured size for RAM1 - which contains TxFifo -,
137 * the amount of endpoints enabled on coreConsultant tool, and
138 * the width of the Master Bus.
139 *
140 * In the ideal world, we would always be able to satisfy the
141 * following equation:
142 *
143 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
144 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
145 *
146 * Unfortunately, due to many variables that's not always the case.
147 */
148int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
149{
150 int last_fifo_depth = 0;
151 int ram1_depth;
152 int fifo_size;
153 int mdwidth;
154 int num;
155
156 if (!dwc->needs_fifo_resize)
157 return 0;
158
159 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
160 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
161
162 /* MDWIDTH is represented in bits, we need it in bytes */
163 mdwidth >>= 3;
164
165 /*
166 * FIXME For now we will only allocate 1 wMaxPacketSize space
167 * for each enabled endpoint, later patches will come to
168 * improve this algorithm so that we better use the internal
169 * FIFO space
170 */
171 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
172 struct dwc3_ep *dep = dwc->eps[num];
173 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200174 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175 int tmp;
176
177 if (!(dep->number & 1))
178 continue;
179
180 if (!(dep->flags & DWC3_EP_ENABLED))
181 continue;
182
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200183 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
184 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200185 mult = 3;
186
187 /*
188 * REVISIT: the following assumes we will always have enough
189 * space available on the FIFO RAM for all possible use cases.
190 * Make sure that's true somehow and change FIFO allocation
191 * accordingly.
192 *
193 * If we have Bulk or Isochronous endpoints, we want
194 * them to be able to be very, very fast. So we're giving
195 * those endpoints a fifo_size which is enough for 3 full
196 * packets
197 */
198 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200199 tmp += mdwidth;
200
201 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200202
Felipe Balbi457e84b2012-01-18 18:04:09 +0200203 fifo_size |= (last_fifo_depth << 16);
204
205 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
206 dep->name, last_fifo_depth, fifo_size & 0xffff);
207
208 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
209 fifo_size);
210
211 last_fifo_depth += (fifo_size & 0xffff);
212 }
213
214 return 0;
215}
216
Felipe Balbi72246da2011-08-19 18:10:58 +0300217void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
218 int status)
219{
220 struct dwc3 *dwc = dep->dwc;
221
222 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200223 if (req->request.num_mapped_sgs)
224 dep->busy_slot += req->request.num_mapped_sgs;
225 else
226 dep->busy_slot++;
227
Felipe Balbi72246da2011-08-19 18:10:58 +0300228 /*
229 * Skip LINK TRB. We can't use req->trb and check for
230 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
231 * completed (not the LINK TRB).
232 */
233 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200234 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300235 dep->busy_slot++;
236 }
237 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200238 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300239
240 if (req->request.status == -EINPROGRESS)
241 req->request.status = status;
242
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200243 usb_gadget_unmap_request(&dwc->gadget, &req->request,
244 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300245
246 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
247 req, dep->name, req->request.actual,
248 req->request.length, status);
249
250 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200251 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300252 spin_lock(&dwc->lock);
253}
254
255static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
256{
257 switch (cmd) {
258 case DWC3_DEPCMD_DEPSTARTCFG:
259 return "Start New Configuration";
260 case DWC3_DEPCMD_ENDTRANSFER:
261 return "End Transfer";
262 case DWC3_DEPCMD_UPDATETRANSFER:
263 return "Update Transfer";
264 case DWC3_DEPCMD_STARTTRANSFER:
265 return "Start Transfer";
266 case DWC3_DEPCMD_CLEARSTALL:
267 return "Clear Stall";
268 case DWC3_DEPCMD_SETSTALL:
269 return "Set Stall";
270 case DWC3_DEPCMD_GETSEQNUMBER:
271 return "Get Data Sequence Number";
272 case DWC3_DEPCMD_SETTRANSFRESOURCE:
273 return "Set Endpoint Transfer Resource";
274 case DWC3_DEPCMD_SETEPCONFIG:
275 return "Set Endpoint Configuration";
276 default:
277 return "UNKNOWN command";
278 }
279}
280
Felipe Balbi573c2762012-04-24 16:19:11 +0300281int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
282{
283 u32 timeout = 500;
284 u32 reg;
285
286 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
287 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
288
289 do {
290 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
291 if (!(reg & DWC3_DGCMD_CMDACT)) {
292 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
293 DWC3_DGCMD_STATUS(reg));
294 return 0;
295 }
296
297 /*
298 * We can't sleep here, because it's also called from
299 * interrupt context.
300 */
301 timeout--;
302 if (!timeout)
303 return -ETIMEDOUT;
304 udelay(1);
305 } while (1);
306}
307
Felipe Balbi72246da2011-08-19 18:10:58 +0300308int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
309 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
310{
311 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200312 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300313 u32 reg;
314
315 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
316 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300317 dwc3_gadget_ep_cmd_string(cmd), params->param0,
318 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300319
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300320 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
321 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
322 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300323
324 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
325 do {
326 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
327 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300328 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
329 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300330 return 0;
331 }
332
333 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300334 * We can't sleep here, because it is also called from
335 * interrupt context.
336 */
337 timeout--;
338 if (!timeout)
339 return -ETIMEDOUT;
340
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200341 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 } while (1);
343}
344
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300345dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200346 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300347{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300348 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300349
350 return dep->trb_pool_dma + offset;
351}
352
353static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
354{
355 struct dwc3 *dwc = dep->dwc;
356
357 if (dep->trb_pool)
358 return 0;
359
360 if (dep->number == 0 || dep->number == 1)
361 return 0;
362
363 dep->trb_pool = dma_alloc_coherent(dwc->dev,
364 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
365 &dep->trb_pool_dma, GFP_KERNEL);
366 if (!dep->trb_pool) {
367 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
368 dep->name);
369 return -ENOMEM;
370 }
371
372 return 0;
373}
374
375static void dwc3_free_trb_pool(struct dwc3_ep *dep)
376{
377 struct dwc3 *dwc = dep->dwc;
378
379 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
380 dep->trb_pool, dep->trb_pool_dma);
381
382 dep->trb_pool = NULL;
383 dep->trb_pool_dma = 0;
384}
385
386static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
387{
388 struct dwc3_gadget_ep_cmd_params params;
389 u32 cmd;
390
391 memset(&params, 0x00, sizeof(params));
392
393 if (dep->number != 1) {
394 cmd = DWC3_DEPCMD_DEPSTARTCFG;
395 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300396 if (dep->number > 1) {
397 if (dwc->start_config_issued)
398 return 0;
399 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300400 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300401 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300402
403 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
404 }
405
406 return 0;
407}
408
409static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200410 const struct usb_endpoint_descriptor *desc,
411 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300412{
413 struct dwc3_gadget_ep_cmd_params params;
414
415 memset(&params, 0x00, sizeof(params));
416
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300417 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
418 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
419 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300420
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300421 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
422 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200424 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300425 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
426 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300427 dep->stream_capable = true;
428 }
429
Felipe Balbi72246da2011-08-19 18:10:58 +0300430 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300431 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300432
433 /*
434 * We are doing 1:1 mapping for endpoints, meaning
435 * Physical Endpoints 2 maps to Logical Endpoint 2 and
436 * so on. We consider the direction bit as part of the physical
437 * endpoint number. So USB endpoint 0x81 is 0x03.
438 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300439 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
441 /*
442 * We must use the lower 16 TX FIFOs even though
443 * HW might have more
444 */
445 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300446 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300447
448 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300449 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300450 dep->interval = 1 << (desc->bInterval - 1);
451 }
452
453 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
454 DWC3_DEPCMD_SETEPCONFIG, &params);
455}
456
457static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
458{
459 struct dwc3_gadget_ep_cmd_params params;
460
461 memset(&params, 0x00, sizeof(params));
462
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300463 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300464
465 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
466 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
467}
468
469/**
470 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
471 * @dep: endpoint to be initialized
472 * @desc: USB Endpoint Descriptor
473 *
474 * Caller should take care of locking
475 */
476static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200477 const struct usb_endpoint_descriptor *desc,
478 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300479{
480 struct dwc3 *dwc = dep->dwc;
481 u32 reg;
482 int ret = -ENOMEM;
483
484 if (!(dep->flags & DWC3_EP_ENABLED)) {
485 ret = dwc3_gadget_start_config(dwc, dep);
486 if (ret)
487 return ret;
488 }
489
Felipe Balbic90bfae2011-11-29 13:11:21 +0200490 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300491 if (ret)
492 return ret;
493
494 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200495 struct dwc3_trb *trb_st_hw;
496 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300497
498 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
499 if (ret)
500 return ret;
501
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200502 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200503 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300504 dep->type = usb_endpoint_type(desc);
505 dep->flags |= DWC3_EP_ENABLED;
506
507 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
508 reg |= DWC3_DALEPENA_EP(dep->number);
509 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
510
511 if (!usb_endpoint_xfer_isoc(desc))
512 return 0;
513
514 memset(&trb_link, 0, sizeof(trb_link));
515
Paul Zimmerman1d046792012-02-15 18:56:56 -0800516 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 trb_st_hw = &dep->trb_pool[0];
518
Felipe Balbif6bafc62012-02-06 11:04:53 +0200519 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
Felipe Balbif6bafc62012-02-06 11:04:53 +0200521 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
522 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
523 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
524 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 }
526
527 return 0;
528}
529
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200530static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
531static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300532{
533 struct dwc3_request *req;
534
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200535 if (!list_empty(&dep->req_queued))
536 dwc3_stop_active_transfer(dwc, dep->number);
537
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 while (!list_empty(&dep->request_list)) {
539 req = next_request(&dep->request_list);
540
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200541 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300542 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300543}
544
545/**
546 * __dwc3_gadget_ep_disable - Disables a HW endpoint
547 * @dep: the endpoint to disable
548 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200549 * This function also removes requests which are currently processed ny the
550 * hardware and those which are not yet scheduled.
551 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300552 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300553static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
554{
555 struct dwc3 *dwc = dep->dwc;
556 u32 reg;
557
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200558 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
560 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
561 reg &= ~DWC3_DALEPENA_EP(dep->number);
562 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
563
Felipe Balbi879631a2011-09-30 10:58:47 +0300564 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200565 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200566 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300568 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300569
570 return 0;
571}
572
573/* -------------------------------------------------------------------------- */
574
575static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
576 const struct usb_endpoint_descriptor *desc)
577{
578 return -EINVAL;
579}
580
581static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
582{
583 return -EINVAL;
584}
585
586/* -------------------------------------------------------------------------- */
587
588static int dwc3_gadget_ep_enable(struct usb_ep *ep,
589 const struct usb_endpoint_descriptor *desc)
590{
591 struct dwc3_ep *dep;
592 struct dwc3 *dwc;
593 unsigned long flags;
594 int ret;
595
596 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
597 pr_debug("dwc3: invalid parameters\n");
598 return -EINVAL;
599 }
600
601 if (!desc->wMaxPacketSize) {
602 pr_debug("dwc3: missing wMaxPacketSize\n");
603 return -EINVAL;
604 }
605
606 dep = to_dwc3_ep(ep);
607 dwc = dep->dwc;
608
609 switch (usb_endpoint_type(desc)) {
610 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900611 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 break;
613 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900614 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 break;
616 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900617 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 break;
619 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900620 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 break;
622 default:
623 dev_err(dwc->dev, "invalid endpoint transfer type\n");
624 }
625
626 if (dep->flags & DWC3_EP_ENABLED) {
627 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
628 dep->name);
629 return 0;
630 }
631
632 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
633
634 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200635 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 spin_unlock_irqrestore(&dwc->lock, flags);
637
638 return ret;
639}
640
641static int dwc3_gadget_ep_disable(struct usb_ep *ep)
642{
643 struct dwc3_ep *dep;
644 struct dwc3 *dwc;
645 unsigned long flags;
646 int ret;
647
648 if (!ep) {
649 pr_debug("dwc3: invalid parameters\n");
650 return -EINVAL;
651 }
652
653 dep = to_dwc3_ep(ep);
654 dwc = dep->dwc;
655
656 if (!(dep->flags & DWC3_EP_ENABLED)) {
657 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
658 dep->name);
659 return 0;
660 }
661
662 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
663 dep->number >> 1,
664 (dep->number & 1) ? "in" : "out");
665
666 spin_lock_irqsave(&dwc->lock, flags);
667 ret = __dwc3_gadget_ep_disable(dep);
668 spin_unlock_irqrestore(&dwc->lock, flags);
669
670 return ret;
671}
672
673static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
674 gfp_t gfp_flags)
675{
676 struct dwc3_request *req;
677 struct dwc3_ep *dep = to_dwc3_ep(ep);
678 struct dwc3 *dwc = dep->dwc;
679
680 req = kzalloc(sizeof(*req), gfp_flags);
681 if (!req) {
682 dev_err(dwc->dev, "not enough memory\n");
683 return NULL;
684 }
685
686 req->epnum = dep->number;
687 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300688
689 return &req->request;
690}
691
692static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
693 struct usb_request *request)
694{
695 struct dwc3_request *req = to_dwc3_request(request);
696
697 kfree(req);
698}
699
Felipe Balbic71fc372011-11-22 11:37:34 +0200700/**
701 * dwc3_prepare_one_trb - setup one TRB from one request
702 * @dep: endpoint for which this request is prepared
703 * @req: dwc3_request pointer
704 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200705static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200706 struct dwc3_request *req, dma_addr_t dma,
707 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200708{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200709 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200710 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200711
712 unsigned int cur_slot;
713
Felipe Balbieeb720f2011-11-28 12:46:59 +0200714 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
715 dep->name, req, (unsigned long long) dma,
716 length, last ? " last" : "",
717 chain ? " chain" : "");
718
Felipe Balbif6bafc62012-02-06 11:04:53 +0200719 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200720 cur_slot = dep->free_slot;
721 dep->free_slot++;
722
723 /* Skip the LINK-TRB on ISOC */
724 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200725 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200726 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200727
Felipe Balbieeb720f2011-11-28 12:46:59 +0200728 if (!req->trb) {
729 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200730 req->trb = trb;
731 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200732 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200733
Felipe Balbif6bafc62012-02-06 11:04:53 +0200734 trb->size = DWC3_TRB_SIZE_LENGTH(length);
735 trb->bpl = lower_32_bits(dma);
736 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200737
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200738 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200739 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200740 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200741 break;
742
743 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200744 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200745
746 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
747 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200748 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200749 break;
750
751 case USB_ENDPOINT_XFER_BULK:
752 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200753 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200754 break;
755 default:
756 /*
757 * This is only possible with faulty memory because we
758 * checked it already :)
759 */
760 BUG();
761 }
762
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200763 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200764 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
765 trb->ctrl |= DWC3_TRB_CTRL_CSP;
766 } else {
767 if (chain)
768 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200769
Felipe Balbif6bafc62012-02-06 11:04:53 +0200770 if (last)
771 trb->ctrl |= DWC3_TRB_CTRL_LST;
772 }
773
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200774 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200775 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
776
777 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200778}
779
Felipe Balbi72246da2011-08-19 18:10:58 +0300780/*
781 * dwc3_prepare_trbs - setup TRBs from requests
782 * @dep: endpoint for which requests are being prepared
783 * @starting: true if the endpoint is idle and no requests are queued.
784 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800785 * The function goes through the requests list and sets up TRBs for the
786 * transfers. The function returns once there are no more TRBs available or
787 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300788 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200789static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300790{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200791 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300792 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200793 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200794 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300795
796 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
797
798 /* the first request must not be queued */
799 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200800
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200801 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200802 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200803 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
804 if (trbs_left > max)
805 trbs_left = max;
806 }
807
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800809 * If busy & slot are equal than it is either full or empty. If we are
810 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300811 * full and don't do anything
812 */
813 if (!trbs_left) {
814 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200815 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300816 trbs_left = DWC3_TRB_NUM;
817 /*
818 * In case we start from scratch, we queue the ISOC requests
819 * starting from slot 1. This is done because we use ring
820 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800821 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 * after the first request so we start at slot 1 and have
823 * 7 requests proceed before we hit the first IOC.
824 * Other transfer types don't use the ring buffer and are
825 * processed from the first TRB until the last one. Since we
826 * don't wrap around we have to start at the beginning.
827 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200828 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300829 dep->busy_slot = 1;
830 dep->free_slot = 1;
831 } else {
832 dep->busy_slot = 0;
833 dep->free_slot = 0;
834 }
835 }
836
837 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200838 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200839 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300840
841 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200842 unsigned length;
843 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844
Felipe Balbieeb720f2011-11-28 12:46:59 +0200845 if (req->request.num_mapped_sgs > 0) {
846 struct usb_request *request = &req->request;
847 struct scatterlist *sg = request->sg;
848 struct scatterlist *s;
849 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300850
Felipe Balbieeb720f2011-11-28 12:46:59 +0200851 for_each_sg(sg, s, request->num_mapped_sgs, i) {
852 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300853
Felipe Balbieeb720f2011-11-28 12:46:59 +0200854 length = sg_dma_len(s);
855 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300856
Paul Zimmerman1d046792012-02-15 18:56:56 -0800857 if (i == (request->num_mapped_sgs - 1) ||
858 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200859 last_one = true;
860 chain = false;
861 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300862
Felipe Balbieeb720f2011-11-28 12:46:59 +0200863 trbs_left--;
864 if (!trbs_left)
865 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300866
Felipe Balbieeb720f2011-11-28 12:46:59 +0200867 if (last_one)
868 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300869
Felipe Balbieeb720f2011-11-28 12:46:59 +0200870 dwc3_prepare_one_trb(dep, req, dma, length,
871 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300872
Felipe Balbieeb720f2011-11-28 12:46:59 +0200873 if (last_one)
874 break;
875 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200877 dma = req->request.dma;
878 length = req->request.length;
879 trbs_left--;
880
881 if (!trbs_left)
882 last_one = 1;
883
884 /* Is this the last request? */
885 if (list_is_last(&req->list, &dep->request_list))
886 last_one = 1;
887
888 dwc3_prepare_one_trb(dep, req, dma, length,
889 last_one, false);
890
891 if (last_one)
892 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300893 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300894 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300895}
896
897static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
898 int start_new)
899{
900 struct dwc3_gadget_ep_cmd_params params;
901 struct dwc3_request *req;
902 struct dwc3 *dwc = dep->dwc;
903 int ret;
904 u32 cmd;
905
906 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
907 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
908 return -EBUSY;
909 }
910 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
911
912 /*
913 * If we are getting here after a short-out-packet we don't enqueue any
914 * new requests as we try to set the IOC bit only on the last request.
915 */
916 if (start_new) {
917 if (list_empty(&dep->req_queued))
918 dwc3_prepare_trbs(dep, start_new);
919
920 /* req points to the first request which will be sent */
921 req = next_request(&dep->req_queued);
922 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200923 dwc3_prepare_trbs(dep, start_new);
924
Felipe Balbi72246da2011-08-19 18:10:58 +0300925 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800926 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300927 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200928 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300929 }
930 if (!req) {
931 dep->flags |= DWC3_EP_PENDING_REQUEST;
932 return 0;
933 }
934
935 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300936 params.param0 = upper_32_bits(req->trb_dma);
937 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300938
939 if (start_new)
940 cmd = DWC3_DEPCMD_STARTTRANSFER;
941 else
942 cmd = DWC3_DEPCMD_UPDATETRANSFER;
943
944 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
945 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
946 if (ret < 0) {
947 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
948
949 /*
950 * FIXME we need to iterate over the list of requests
951 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800952 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300953 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200954 usb_gadget_unmap_request(&dwc->gadget, &req->request,
955 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300956 list_del(&req->list);
957 return ret;
958 }
959
960 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200961
Paul Zimmermanf39a37f2012-03-29 18:16:54 +0000962 if (start_new) {
963 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
964 dep->number);
965 WARN_ON_ONCE(!dep->res_trans_idx);
966 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200967
Felipe Balbi72246da2011-08-19 18:10:58 +0300968 return 0;
969}
970
971static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
972{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200973 struct dwc3 *dwc = dep->dwc;
974 int ret;
975
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 req->request.actual = 0;
977 req->request.status = -EINPROGRESS;
978 req->direction = dep->direction;
979 req->epnum = dep->number;
980
981 /*
982 * We only add to our list of requests now and
983 * start consuming the list once we get XferNotReady
984 * IRQ.
985 *
986 * That way, we avoid doing anything that we don't need
987 * to do now and defer it until the point we receive a
988 * particular token from the Host side.
989 *
990 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800991 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200993 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
994 dep->direction);
995 if (ret)
996 return ret;
997
Felipe Balbi72246da2011-08-19 18:10:58 +0300998 list_add_tail(&req->list, &dep->request_list);
999
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001000 if (usb_endpoint_xfer_isoc(dep->desc) && (dep->flags & DWC3_EP_BUSY))
1001 dep->flags |= DWC3_EP_PENDING_REQUEST;
1002
Felipe Balbi72246da2011-08-19 18:10:58 +03001003 /*
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001004 * There are two special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001005 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001006 * 1. XferNotReady with empty list of requests. We need to kick the
1007 * transfer here in that situation, otherwise we will be NAKing
1008 * forever. If we get XferNotReady before gadget driver has a
1009 * chance to queue a request, we will ACK the IRQ but won't be
1010 * able to receive the data until the next request is queued.
1011 * The following code is handling exactly that.
1012 *
1013 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1014 * kick the transfer here after queuing a request, otherwise the
1015 * core may not see the modified TRB(s).
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 */
1017 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001018 int ret;
1019 int start_trans = 1;
1020 u8 trans_idx = dep->res_trans_idx;
Felipe Balbi72246da2011-08-19 18:10:58 +03001021
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001022 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001023 (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 start_trans = 0;
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001025 WARN_ON_ONCE(!trans_idx);
1026 } else {
1027 trans_idx = 0;
1028 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001029
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001030 ret = __dwc3_gadget_kick_transfer(dep, trans_idx, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +03001031 if (ret && ret != -EBUSY) {
1032 struct dwc3 *dwc = dep->dwc;
1033
1034 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1035 dep->name);
1036 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001037 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001038
1039 return 0;
1040}
1041
1042static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1043 gfp_t gfp_flags)
1044{
1045 struct dwc3_request *req = to_dwc3_request(request);
1046 struct dwc3_ep *dep = to_dwc3_ep(ep);
1047 struct dwc3 *dwc = dep->dwc;
1048
1049 unsigned long flags;
1050
1051 int ret;
1052
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001053 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001054 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1055 request, ep->name);
1056 return -ESHUTDOWN;
1057 }
1058
1059 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1060 request, ep->name, request->length);
1061
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301062 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1063 "trying to queue unaligned request (%d)\n", request->length);
1064
Felipe Balbi72246da2011-08-19 18:10:58 +03001065 spin_lock_irqsave(&dwc->lock, flags);
1066 ret = __dwc3_gadget_ep_queue(dep, req);
1067 spin_unlock_irqrestore(&dwc->lock, flags);
1068
1069 return ret;
1070}
1071
1072static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1073 struct usb_request *request)
1074{
1075 struct dwc3_request *req = to_dwc3_request(request);
1076 struct dwc3_request *r = NULL;
1077
1078 struct dwc3_ep *dep = to_dwc3_ep(ep);
1079 struct dwc3 *dwc = dep->dwc;
1080
1081 unsigned long flags;
1082 int ret = 0;
1083
1084 spin_lock_irqsave(&dwc->lock, flags);
1085
1086 list_for_each_entry(r, &dep->request_list, list) {
1087 if (r == req)
1088 break;
1089 }
1090
1091 if (r != req) {
1092 list_for_each_entry(r, &dep->req_queued, list) {
1093 if (r == req)
1094 break;
1095 }
1096 if (r == req) {
1097 /* wait until it is processed */
1098 dwc3_stop_active_transfer(dwc, dep->number);
1099 goto out0;
1100 }
1101 dev_err(dwc->dev, "request %p was not queued to %s\n",
1102 request, ep->name);
1103 ret = -EINVAL;
1104 goto out0;
1105 }
1106
1107 /* giveback the request */
1108 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1109
1110out0:
1111 spin_unlock_irqrestore(&dwc->lock, flags);
1112
1113 return ret;
1114}
1115
1116int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1117{
1118 struct dwc3_gadget_ep_cmd_params params;
1119 struct dwc3 *dwc = dep->dwc;
1120 int ret;
1121
1122 memset(&params, 0x00, sizeof(params));
1123
1124 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001125 if (dep->number == 0 || dep->number == 1) {
1126 /*
1127 * Whenever EP0 is stalled, we will restart
1128 * the state machine, thus moving back to
1129 * Setup Phase
1130 */
1131 dwc->ep0state = EP0_SETUP_PHASE;
1132 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001133
1134 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1135 DWC3_DEPCMD_SETSTALL, &params);
1136 if (ret)
1137 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1138 value ? "set" : "clear",
1139 dep->name);
1140 else
1141 dep->flags |= DWC3_EP_STALL;
1142 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001143 if (dep->flags & DWC3_EP_WEDGE)
1144 return 0;
1145
Felipe Balbi72246da2011-08-19 18:10:58 +03001146 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1147 DWC3_DEPCMD_CLEARSTALL, &params);
1148 if (ret)
1149 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1150 value ? "set" : "clear",
1151 dep->name);
1152 else
1153 dep->flags &= ~DWC3_EP_STALL;
1154 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001155
Felipe Balbi72246da2011-08-19 18:10:58 +03001156 return ret;
1157}
1158
1159static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1160{
1161 struct dwc3_ep *dep = to_dwc3_ep(ep);
1162 struct dwc3 *dwc = dep->dwc;
1163
1164 unsigned long flags;
1165
1166 int ret;
1167
1168 spin_lock_irqsave(&dwc->lock, flags);
1169
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001170 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001171 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1172 ret = -EINVAL;
1173 goto out;
1174 }
1175
1176 ret = __dwc3_gadget_ep_set_halt(dep, value);
1177out:
1178 spin_unlock_irqrestore(&dwc->lock, flags);
1179
1180 return ret;
1181}
1182
1183static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1184{
1185 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001186 struct dwc3 *dwc = dep->dwc;
1187 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001188
Paul Zimmerman249a4562012-02-24 17:32:16 -08001189 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001190 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001191 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001192
Paul Zimmerman52754552011-09-30 10:58:44 +03001193 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001194}
1195
1196/* -------------------------------------------------------------------------- */
1197
1198static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1199 .bLength = USB_DT_ENDPOINT_SIZE,
1200 .bDescriptorType = USB_DT_ENDPOINT,
1201 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1202};
1203
1204static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1205 .enable = dwc3_gadget_ep0_enable,
1206 .disable = dwc3_gadget_ep0_disable,
1207 .alloc_request = dwc3_gadget_ep_alloc_request,
1208 .free_request = dwc3_gadget_ep_free_request,
1209 .queue = dwc3_gadget_ep0_queue,
1210 .dequeue = dwc3_gadget_ep_dequeue,
1211 .set_halt = dwc3_gadget_ep_set_halt,
1212 .set_wedge = dwc3_gadget_ep_set_wedge,
1213};
1214
1215static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1216 .enable = dwc3_gadget_ep_enable,
1217 .disable = dwc3_gadget_ep_disable,
1218 .alloc_request = dwc3_gadget_ep_alloc_request,
1219 .free_request = dwc3_gadget_ep_free_request,
1220 .queue = dwc3_gadget_ep_queue,
1221 .dequeue = dwc3_gadget_ep_dequeue,
1222 .set_halt = dwc3_gadget_ep_set_halt,
1223 .set_wedge = dwc3_gadget_ep_set_wedge,
1224};
1225
1226/* -------------------------------------------------------------------------- */
1227
1228static int dwc3_gadget_get_frame(struct usb_gadget *g)
1229{
1230 struct dwc3 *dwc = gadget_to_dwc(g);
1231 u32 reg;
1232
1233 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1234 return DWC3_DSTS_SOFFN(reg);
1235}
1236
1237static int dwc3_gadget_wakeup(struct usb_gadget *g)
1238{
1239 struct dwc3 *dwc = gadget_to_dwc(g);
1240
1241 unsigned long timeout;
1242 unsigned long flags;
1243
1244 u32 reg;
1245
1246 int ret = 0;
1247
1248 u8 link_state;
1249 u8 speed;
1250
1251 spin_lock_irqsave(&dwc->lock, flags);
1252
1253 /*
1254 * According to the Databook Remote wakeup request should
1255 * be issued only when the device is in early suspend state.
1256 *
1257 * We can check that via USB Link State bits in DSTS register.
1258 */
1259 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1260
1261 speed = reg & DWC3_DSTS_CONNECTSPD;
1262 if (speed == DWC3_DSTS_SUPERSPEED) {
1263 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1264 ret = -EINVAL;
1265 goto out;
1266 }
1267
1268 link_state = DWC3_DSTS_USBLNKST(reg);
1269
1270 switch (link_state) {
1271 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1272 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1273 break;
1274 default:
1275 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1276 link_state);
1277 ret = -EINVAL;
1278 goto out;
1279 }
1280
Felipe Balbi8598bde2012-01-02 18:55:57 +02001281 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1282 if (ret < 0) {
1283 dev_err(dwc->dev, "failed to put link in Recovery\n");
1284 goto out;
1285 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001286
1287 /* write zeroes to Link Change Request */
1288 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1289 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1290
Paul Zimmerman1d046792012-02-15 18:56:56 -08001291 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001292 timeout = jiffies + msecs_to_jiffies(100);
1293
Paul Zimmerman1d046792012-02-15 18:56:56 -08001294 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001295 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1296
1297 /* in HS, means ON */
1298 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1299 break;
1300 }
1301
1302 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1303 dev_err(dwc->dev, "failed to send remote wakeup\n");
1304 ret = -EINVAL;
1305 }
1306
1307out:
1308 spin_unlock_irqrestore(&dwc->lock, flags);
1309
1310 return ret;
1311}
1312
1313static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1314 int is_selfpowered)
1315{
1316 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001317 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001318
Paul Zimmerman249a4562012-02-24 17:32:16 -08001319 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001320 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001321 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001322
1323 return 0;
1324}
1325
1326static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1327{
1328 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001329 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001330
1331 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001332 if (is_on) {
1333 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1334 reg |= (DWC3_DCTL_RUN_STOP
1335 | DWC3_DCTL_TRGTULST_RX_DET);
1336 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001337 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001338 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001339
1340 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1341
1342 do {
1343 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1344 if (is_on) {
1345 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1346 break;
1347 } else {
1348 if (reg & DWC3_DSTS_DEVCTRLHLT)
1349 break;
1350 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001351 timeout--;
1352 if (!timeout)
1353 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001354 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001355 } while (1);
1356
1357 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1358 dwc->gadget_driver
1359 ? dwc->gadget_driver->function : "no-function",
1360 is_on ? "connect" : "disconnect");
1361}
1362
1363static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1364{
1365 struct dwc3 *dwc = gadget_to_dwc(g);
1366 unsigned long flags;
1367
1368 is_on = !!is_on;
1369
1370 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001371
1372 dwc->softconnect = is_on;
1373
1374 if ((dwc->dotg && !dwc->vbus_active) ||
1375 !dwc->gadget_driver) {
1376
1377 spin_unlock_irqrestore(&dwc->lock, flags);
1378
1379 /*
1380 * Need to wait for vbus_session(on) from otg driver or to
1381 * the udc_start.
1382 */
1383 return 0;
1384 }
1385
Felipe Balbi72246da2011-08-19 18:10:58 +03001386 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001387
1388 spin_unlock_irqrestore(&dwc->lock, flags);
1389
1390 return 0;
1391}
1392
1393static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1394{
1395 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1396 unsigned long flags;
1397
1398 if (!dwc->dotg)
1399 return -EPERM;
1400
1401 is_active = !!is_active;
1402
1403 spin_lock_irqsave(&dwc->lock, flags);
1404
1405 /* Mark that the vbus was powered */
1406 dwc->vbus_active = is_active;
1407
1408 /*
1409 * Check if upper level usb_gadget_driver was already registerd with
1410 * this udc controller driver (if dwc3_gadget_start was called)
1411 */
1412 if (dwc->gadget_driver && dwc->softconnect) {
1413 if (dwc->vbus_active) {
1414 /*
1415 * Both vbus was activated by otg and pullup was
1416 * signaled by the gadget driver.
1417 */
1418 dwc3_gadget_run_stop(dwc, 1);
1419 } else {
1420 dwc3_gadget_run_stop(dwc, 0);
1421 }
1422 }
1423
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 spin_unlock_irqrestore(&dwc->lock, flags);
1425
1426 return 0;
1427}
1428
1429static int dwc3_gadget_start(struct usb_gadget *g,
1430 struct usb_gadget_driver *driver)
1431{
1432 struct dwc3 *dwc = gadget_to_dwc(g);
1433 struct dwc3_ep *dep;
1434 unsigned long flags;
1435 int ret = 0;
1436 u32 reg;
1437
1438 spin_lock_irqsave(&dwc->lock, flags);
1439
1440 if (dwc->gadget_driver) {
1441 dev_err(dwc->dev, "%s is already bound to %s\n",
1442 dwc->gadget.name,
1443 dwc->gadget_driver->driver.name);
1444 ret = -EBUSY;
1445 goto err0;
1446 }
1447
1448 dwc->gadget_driver = driver;
1449 dwc->gadget.dev.driver = &driver->driver;
1450
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1452 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001453
1454 /**
1455 * WORKAROUND: DWC3 revision < 2.20a have an issue
1456 * which would cause metastability state on Run/Stop
1457 * bit if we try to force the IP to USB2-only mode.
1458 *
1459 * Because of that, we cannot configure the IP to any
1460 * speed other than the SuperSpeed
1461 *
1462 * Refers to:
1463 *
1464 * STAR#9000525659: Clock Domain Crossing on DCTL in
1465 * USB 2.0 Mode
1466 */
1467 if (dwc->revision < DWC3_REVISION_220A)
1468 reg |= DWC3_DCFG_SUPERSPEED;
1469 else
1470 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1472
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001473 dwc->start_config_issued = false;
1474
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 /* Start with SuperSpeed Default */
1476 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1477
1478 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001479 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 if (ret) {
1481 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1482 goto err0;
1483 }
1484
1485 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001486 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 if (ret) {
1488 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1489 goto err1;
1490 }
1491
1492 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001493 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 dwc3_ep0_out_start(dwc);
1495
1496 spin_unlock_irqrestore(&dwc->lock, flags);
1497
1498 return 0;
1499
1500err1:
1501 __dwc3_gadget_ep_disable(dwc->eps[0]);
1502
1503err0:
1504 spin_unlock_irqrestore(&dwc->lock, flags);
1505
1506 return ret;
1507}
1508
1509static int dwc3_gadget_stop(struct usb_gadget *g,
1510 struct usb_gadget_driver *driver)
1511{
1512 struct dwc3 *dwc = gadget_to_dwc(g);
1513 unsigned long flags;
1514
1515 spin_lock_irqsave(&dwc->lock, flags);
1516
1517 __dwc3_gadget_ep_disable(dwc->eps[0]);
1518 __dwc3_gadget_ep_disable(dwc->eps[1]);
1519
1520 dwc->gadget_driver = NULL;
1521 dwc->gadget.dev.driver = NULL;
1522
1523 spin_unlock_irqrestore(&dwc->lock, flags);
1524
1525 return 0;
1526}
1527static const struct usb_gadget_ops dwc3_gadget_ops = {
1528 .get_frame = dwc3_gadget_get_frame,
1529 .wakeup = dwc3_gadget_wakeup,
1530 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001531 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001532 .pullup = dwc3_gadget_pullup,
1533 .udc_start = dwc3_gadget_start,
1534 .udc_stop = dwc3_gadget_stop,
1535};
1536
1537/* -------------------------------------------------------------------------- */
1538
1539static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1540{
1541 struct dwc3_ep *dep;
1542 u8 epnum;
1543
1544 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1545
1546 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1547 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1548 if (!dep) {
1549 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1550 epnum);
1551 return -ENOMEM;
1552 }
1553
1554 dep->dwc = dwc;
1555 dep->number = epnum;
1556 dwc->eps[epnum] = dep;
1557
1558 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1559 (epnum & 1) ? "in" : "out");
1560 dep->endpoint.name = dep->name;
1561 dep->direction = (epnum & 1);
1562
1563 if (epnum == 0 || epnum == 1) {
1564 dep->endpoint.maxpacket = 512;
1565 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1566 if (!epnum)
1567 dwc->gadget.ep0 = &dep->endpoint;
1568 } else {
1569 int ret;
1570
1571 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001572 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1574 list_add_tail(&dep->endpoint.ep_list,
1575 &dwc->gadget.ep_list);
1576
1577 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001578 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001579 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001580 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001581
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 INIT_LIST_HEAD(&dep->request_list);
1583 INIT_LIST_HEAD(&dep->req_queued);
1584 }
1585
1586 return 0;
1587}
1588
1589static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1590{
1591 struct dwc3_ep *dep;
1592 u8 epnum;
1593
1594 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1595 dep = dwc->eps[epnum];
1596 dwc3_free_trb_pool(dep);
1597
1598 if (epnum != 0 && epnum != 1)
1599 list_del(&dep->endpoint.ep_list);
1600
1601 kfree(dep);
1602 }
1603}
1604
1605static void dwc3_gadget_release(struct device *dev)
1606{
1607 dev_dbg(dev, "%s\n", __func__);
1608}
1609
1610/* -------------------------------------------------------------------------- */
1611static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1612 const struct dwc3_event_depevt *event, int status)
1613{
1614 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001615 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001616 unsigned int count;
1617 unsigned int s_pkt = 0;
1618
1619 do {
1620 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001621 if (!req) {
1622 WARN_ON_ONCE(1);
1623 return 1;
1624 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001625
Felipe Balbif6bafc62012-02-06 11:04:53 +02001626 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001627
Felipe Balbif6bafc62012-02-06 11:04:53 +02001628 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001629 /*
1630 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001631 * can do. If we don't clean it up we loop forever. If
1632 * we skip the TRB then it gets overwritten after a
1633 * while since we use them in a ring buffer. A BUG()
1634 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001635 * fixes the root cause instead of looking away :)
1636 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1638 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001639 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001640
1641 if (dep->direction) {
1642 if (count) {
1643 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1644 dep->name);
1645 status = -ECONNRESET;
1646 }
1647 } else {
1648 if (count && (event->status & DEPEVT_STATUS_SHORT))
1649 s_pkt = 1;
1650 }
1651
1652 /*
1653 * We assume here we will always receive the entire data block
1654 * which we should receive. Meaning, if we program RX to
1655 * receive 4K but we receive only 2K, we assume that's all we
1656 * should receive and we simply bounce the request back to the
1657 * gadget driver for further processing.
1658 */
1659 req->request.actual += req->request.length - count;
1660 dwc3_gadget_giveback(dep, req, status);
1661 if (s_pkt)
1662 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001663 if ((event->status & DEPEVT_STATUS_LST) &&
1664 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001665 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001666 if ((event->status & DEPEVT_STATUS_IOC) &&
1667 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001668 break;
1669 } while (1);
1670
Felipe Balbif6bafc62012-02-06 11:04:53 +02001671 if ((event->status & DEPEVT_STATUS_IOC) &&
1672 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 return 0;
1674 return 1;
1675}
1676
1677static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1678 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1679 int start_new)
1680{
1681 unsigned status = 0;
1682 int clean_busy;
1683
1684 if (event->status & DEPEVT_STATUS_BUSERR)
1685 status = -ECONNRESET;
1686
Paul Zimmerman1d046792012-02-15 18:56:56 -08001687 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001688 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001689 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001690
1691 /*
1692 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1693 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1694 */
1695 if (dwc->revision < DWC3_REVISION_183A) {
1696 u32 reg;
1697 int i;
1698
1699 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1700 struct dwc3_ep *dep = dwc->eps[i];
1701
1702 if (!(dep->flags & DWC3_EP_ENABLED))
1703 continue;
1704
1705 if (!list_empty(&dep->req_queued))
1706 return;
1707 }
1708
1709 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1710 reg |= dwc->u1u2;
1711 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1712
1713 dwc->u1u2 = 0;
1714 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001715}
1716
1717static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1718 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1719{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001720 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721
1722 if (list_empty(&dep->request_list)) {
1723 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1724 dep->name);
1725 return;
1726 }
1727
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001728 mask = ~(dep->interval - 1);
1729 uf = event->parameters & mask;
1730 /* 4 micro frames in the future */
1731 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001732
1733 __dwc3_gadget_kick_transfer(dep, uf, 1);
1734}
1735
1736static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1737 const struct dwc3_event_depevt *event)
1738{
1739 struct dwc3 *dwc = dep->dwc;
1740 struct dwc3_event_depevt mod_ev = *event;
1741
1742 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001743 * We were asked to remove one request. It is possible that this
1744 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001745 * transfer index. Since we stopped the complete endpoint we don't
1746 * know how many requests were already completed (and not yet)
1747 * reported and how could be done (later). We purge them all until
1748 * the end of the list.
1749 */
1750 mod_ev.status = DEPEVT_STATUS_LST;
1751 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1752 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001753 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001754}
1755
1756static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1757 const struct dwc3_event_depevt *event)
1758{
1759 u32 param = event->parameters;
1760 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1761
1762 switch (cmd_type) {
1763 case DWC3_DEPCMD_ENDTRANSFER:
1764 dwc3_process_ep_cmd_complete(dep, event);
1765 break;
1766 case DWC3_DEPCMD_STARTTRANSFER:
1767 dep->res_trans_idx = param & 0x7f;
1768 break;
1769 default:
1770 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1771 __func__, cmd_type);
1772 break;
1773 };
1774}
1775
1776static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1777 const struct dwc3_event_depevt *event)
1778{
1779 struct dwc3_ep *dep;
1780 u8 epnum = event->endpoint_number;
1781
1782 dep = dwc->eps[epnum];
1783
1784 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1785 dwc3_ep_event_string(event->endpoint_event));
1786
1787 if (epnum == 0 || epnum == 1) {
1788 dwc3_ep0_interrupt(dwc, event);
1789 return;
1790 }
1791
1792 switch (event->endpoint_event) {
1793 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001794 dep->res_trans_idx = 0;
1795
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001796 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001797 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1798 dep->name);
1799 return;
1800 }
1801
1802 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1803 break;
1804 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001805 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1807 dep->name);
1808 return;
1809 }
1810
1811 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1812 break;
1813 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001814 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001815 dwc3_gadget_start_isoc(dwc, dep, event);
1816 } else {
1817 int ret;
1818
1819 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001820 dep->name, event->status &
1821 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001822 ? "Transfer Active"
1823 : "Transfer Not Active");
1824
1825 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1826 if (!ret || ret == -EBUSY)
1827 return;
1828
1829 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1830 dep->name);
1831 }
1832
1833 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001834 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001835 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001836 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1837 dep->name);
1838 return;
1839 }
1840
1841 switch (event->status) {
1842 case DEPEVT_STREAMEVT_FOUND:
1843 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1844 event->parameters);
1845
1846 break;
1847 case DEPEVT_STREAMEVT_NOTFOUND:
1848 /* FALLTHROUGH */
1849 default:
1850 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1851 }
1852 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 case DWC3_DEPEVT_RXTXFIFOEVT:
1854 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1855 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001856 case DWC3_DEPEVT_EPCMDCMPLT:
1857 dwc3_ep_cmd_compl(dep, event);
1858 break;
1859 }
1860}
1861
1862static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1863{
1864 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1865 spin_unlock(&dwc->lock);
1866 dwc->gadget_driver->disconnect(&dwc->gadget);
1867 spin_lock(&dwc->lock);
1868 }
1869}
1870
1871static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1872{
1873 struct dwc3_ep *dep;
1874 struct dwc3_gadget_ep_cmd_params params;
1875 u32 cmd;
1876 int ret;
1877
1878 dep = dwc->eps[epnum];
1879
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001880 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001881 if (dep->res_trans_idx) {
1882 cmd = DWC3_DEPCMD_ENDTRANSFER;
1883 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1884 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1885 memset(&params, 0, sizeof(params));
1886 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1887 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001888 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 }
1890}
1891
1892static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1893{
1894 u32 epnum;
1895
1896 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1897 struct dwc3_ep *dep;
1898
1899 dep = dwc->eps[epnum];
1900 if (!(dep->flags & DWC3_EP_ENABLED))
1901 continue;
1902
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001903 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001904 }
1905}
1906
1907static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1908{
1909 u32 epnum;
1910
1911 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1912 struct dwc3_ep *dep;
1913 struct dwc3_gadget_ep_cmd_params params;
1914 int ret;
1915
1916 dep = dwc->eps[epnum];
1917
1918 if (!(dep->flags & DWC3_EP_STALL))
1919 continue;
1920
1921 dep->flags &= ~DWC3_EP_STALL;
1922
1923 memset(&params, 0, sizeof(params));
1924 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1925 DWC3_DEPCMD_CLEARSTALL, &params);
1926 WARN_ON_ONCE(ret);
1927 }
1928}
1929
1930static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1931{
1932 dev_vdbg(dwc->dev, "%s\n", __func__);
1933#if 0
1934 XXX
1935 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1936 enable it before we can disable it.
1937
1938 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1939 reg &= ~DWC3_DCTL_INITU1ENA;
1940 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1941
1942 reg &= ~DWC3_DCTL_INITU2ENA;
1943 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1944#endif
1945
1946 dwc3_stop_active_transfers(dwc);
1947 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001948 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001949
1950 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001951 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001952}
1953
1954static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1955{
1956 u32 reg;
1957
1958 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1959
1960 if (on)
1961 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1962 else
1963 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1964
1965 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1966}
1967
1968static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1969{
1970 u32 reg;
1971
1972 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1973
1974 if (on)
1975 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1976 else
1977 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1978
1979 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1980}
1981
1982static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1983{
1984 u32 reg;
1985
1986 dev_vdbg(dwc->dev, "%s\n", __func__);
1987
Felipe Balbidf62df52011-10-14 15:11:49 +03001988 /*
1989 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1990 * would cause a missing Disconnect Event if there's a
1991 * pending Setup Packet in the FIFO.
1992 *
1993 * There's no suggested workaround on the official Bug
1994 * report, which states that "unless the driver/application
1995 * is doing any special handling of a disconnect event,
1996 * there is no functional issue".
1997 *
1998 * Unfortunately, it turns out that we _do_ some special
1999 * handling of a disconnect event, namely complete all
2000 * pending transfers, notify gadget driver of the
2001 * disconnection, and so on.
2002 *
2003 * Our suggested workaround is to follow the Disconnect
2004 * Event steps here, instead, based on a setup_packet_pending
2005 * flag. Such flag gets set whenever we have a XferNotReady
2006 * event on EP0 and gets cleared on XferComplete for the
2007 * same endpoint.
2008 *
2009 * Refers to:
2010 *
2011 * STAR#9000466709: RTL: Device : Disconnect event not
2012 * generated if setup packet pending in FIFO
2013 */
2014 if (dwc->revision < DWC3_REVISION_188A) {
2015 if (dwc->setup_packet_pending)
2016 dwc3_gadget_disconnect_interrupt(dwc);
2017 }
2018
Felipe Balbi961906e2011-12-20 15:37:21 +02002019 /* after reset -> Default State */
2020 dwc->dev_state = DWC3_DEFAULT_STATE;
2021
Felipe Balbi72246da2011-08-19 18:10:58 +03002022 /* Enable PHYs */
2023 dwc3_gadget_usb2_phy_power(dwc, true);
2024 dwc3_gadget_usb3_phy_power(dwc, true);
2025
2026 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2027 dwc3_disconnect_gadget(dwc);
2028
2029 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2030 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002031 reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
Felipe Balbi72246da2011-08-19 18:10:58 +03002032 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002033 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002034
2035 dwc3_stop_active_transfers(dwc);
2036 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002037 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002038
2039 /* Reset device address to zero */
2040 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2041 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2042 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002043}
2044
2045static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2046{
2047 u32 reg;
2048 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2049
2050 /*
2051 * We change the clock only at SS but I dunno why I would want to do
2052 * this. Maybe it becomes part of the power saving plan.
2053 */
2054
2055 if (speed != DWC3_DSTS_SUPERSPEED)
2056 return;
2057
2058 /*
2059 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2060 * each time on Connect Done.
2061 */
2062 if (!usb30_clock)
2063 return;
2064
2065 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2066 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2067 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2068}
2069
2070static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
2071{
2072 switch (speed) {
2073 case USB_SPEED_SUPER:
2074 dwc3_gadget_usb2_phy_power(dwc, false);
2075 break;
2076 case USB_SPEED_HIGH:
2077 case USB_SPEED_FULL:
2078 case USB_SPEED_LOW:
2079 dwc3_gadget_usb3_phy_power(dwc, false);
2080 break;
2081 }
2082}
2083
2084static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2085{
2086 struct dwc3_gadget_ep_cmd_params params;
2087 struct dwc3_ep *dep;
2088 int ret;
2089 u32 reg;
2090 u8 speed;
2091
2092 dev_vdbg(dwc->dev, "%s\n", __func__);
2093
2094 memset(&params, 0x00, sizeof(params));
2095
Felipe Balbi72246da2011-08-19 18:10:58 +03002096 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2097 speed = reg & DWC3_DSTS_CONNECTSPD;
2098 dwc->speed = speed;
2099
2100 dwc3_update_ram_clk_sel(dwc, speed);
2101
2102 switch (speed) {
2103 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002104 /*
2105 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2106 * would cause a missing USB3 Reset event.
2107 *
2108 * In such situations, we should force a USB3 Reset
2109 * event by calling our dwc3_gadget_reset_interrupt()
2110 * routine.
2111 *
2112 * Refers to:
2113 *
2114 * STAR#9000483510: RTL: SS : USB3 reset event may
2115 * not be generated always when the link enters poll
2116 */
2117 if (dwc->revision < DWC3_REVISION_190A)
2118 dwc3_gadget_reset_interrupt(dwc);
2119
Felipe Balbi72246da2011-08-19 18:10:58 +03002120 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2121 dwc->gadget.ep0->maxpacket = 512;
2122 dwc->gadget.speed = USB_SPEED_SUPER;
2123 break;
2124 case DWC3_DCFG_HIGHSPEED:
2125 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2126 dwc->gadget.ep0->maxpacket = 64;
2127 dwc->gadget.speed = USB_SPEED_HIGH;
2128 break;
2129 case DWC3_DCFG_FULLSPEED2:
2130 case DWC3_DCFG_FULLSPEED1:
2131 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2132 dwc->gadget.ep0->maxpacket = 64;
2133 dwc->gadget.speed = USB_SPEED_FULL;
2134 break;
2135 case DWC3_DCFG_LOWSPEED:
2136 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2137 dwc->gadget.ep0->maxpacket = 8;
2138 dwc->gadget.speed = USB_SPEED_LOW;
2139 break;
2140 }
2141
2142 /* Disable unneded PHY */
2143 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2144
2145 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002146 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002147 if (ret) {
2148 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2149 return;
2150 }
2151
2152 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002153 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002154 if (ret) {
2155 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2156 return;
2157 }
2158
2159 /*
2160 * Configure PHY via GUSB3PIPECTLn if required.
2161 *
2162 * Update GTXFIFOSIZn
2163 *
2164 * In both cases reset values should be sufficient.
2165 */
2166}
2167
2168static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2169{
2170 dev_vdbg(dwc->dev, "%s\n", __func__);
2171
2172 /*
2173 * TODO take core out of low power mode when that's
2174 * implemented.
2175 */
2176
2177 dwc->gadget_driver->resume(&dwc->gadget);
2178}
2179
2180static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2181 unsigned int evtinfo)
2182{
Felipe Balbifae2b902011-10-14 13:00:30 +03002183 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2184
2185 /*
2186 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2187 * on the link partner, the USB session might do multiple entry/exit
2188 * of low power states before a transfer takes place.
2189 *
2190 * Due to this problem, we might experience lower throughput. The
2191 * suggested workaround is to disable DCTL[12:9] bits if we're
2192 * transitioning from U1/U2 to U0 and enable those bits again
2193 * after a transfer completes and there are no pending transfers
2194 * on any of the enabled endpoints.
2195 *
2196 * This is the first half of that workaround.
2197 *
2198 * Refers to:
2199 *
2200 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2201 * core send LGO_Ux entering U0
2202 */
2203 if (dwc->revision < DWC3_REVISION_183A) {
2204 if (next == DWC3_LINK_STATE_U0) {
2205 u32 u1u2;
2206 u32 reg;
2207
2208 switch (dwc->link_state) {
2209 case DWC3_LINK_STATE_U1:
2210 case DWC3_LINK_STATE_U2:
2211 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2212 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2213 | DWC3_DCTL_ACCEPTU2ENA
2214 | DWC3_DCTL_INITU1ENA
2215 | DWC3_DCTL_ACCEPTU1ENA);
2216
2217 if (!dwc->u1u2)
2218 dwc->u1u2 = reg & u1u2;
2219
2220 reg &= ~u1u2;
2221
2222 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2223 break;
2224 default:
2225 /* do nothing */
2226 break;
2227 }
2228 }
2229 }
2230
2231 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002232
2233 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002234}
2235
2236static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2237 const struct dwc3_event_devt *event)
2238{
2239 switch (event->type) {
2240 case DWC3_DEVICE_EVENT_DISCONNECT:
2241 dwc3_gadget_disconnect_interrupt(dwc);
2242 break;
2243 case DWC3_DEVICE_EVENT_RESET:
2244 dwc3_gadget_reset_interrupt(dwc);
2245 break;
2246 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2247 dwc3_gadget_conndone_interrupt(dwc);
2248 break;
2249 case DWC3_DEVICE_EVENT_WAKEUP:
2250 dwc3_gadget_wakeup_interrupt(dwc);
2251 break;
2252 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2253 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2254 break;
2255 case DWC3_DEVICE_EVENT_EOPF:
2256 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2257 break;
2258 case DWC3_DEVICE_EVENT_SOF:
2259 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2260 break;
2261 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2262 dev_vdbg(dwc->dev, "Erratic Error\n");
2263 break;
2264 case DWC3_DEVICE_EVENT_CMD_CMPL:
2265 dev_vdbg(dwc->dev, "Command Complete\n");
2266 break;
2267 case DWC3_DEVICE_EVENT_OVERFLOW:
2268 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302269 /*
2270 * Controllers prior to 2.30a revision has a bug where
2271 * Overflow Event may overwrite an unacknowledged event
2272 * in the event buffer. The severity of the issue depends
2273 * on the overwritten event type. Add a warning message
2274 * saying that an event is overwritten.
2275 *
2276 * TODO: In future we may need to see if we can re-enumerate
2277 * with host.
2278 */
2279 if (dwc->revision < DWC3_REVISION_230A)
2280 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002281 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302282 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2283 /*
2284 * Controllers prior to 2.30a revision has a bug, due to which
2285 * a vendor device test LMP event can not be filtered. But
2286 * this event is not handled in the current code. This is a
2287 * special event and 8 bytes of data will follow the event.
2288 * Handling this event is tricky when event buffer is almost
2289 * full. Moreover this event will not occur in normal scenario
2290 * and can only happen with special hosts in testing scenarios.
2291 * Add a warning message to indicate that this event is received
2292 * which means that event buffer might have corrupted.
2293 */
2294 if (dwc->revision < DWC3_REVISION_230A)
2295 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2296 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 default:
2298 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2299 }
2300}
2301
2302static void dwc3_process_event_entry(struct dwc3 *dwc,
2303 const union dwc3_event *event)
2304{
2305 /* Endpoint IRQ, handle it and return early */
2306 if (event->type.is_devspec == 0) {
2307 /* depevt */
2308 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2309 }
2310
2311 switch (event->type.type) {
2312 case DWC3_EVENT_TYPE_DEV:
2313 dwc3_gadget_interrupt(dwc, &event->devt);
2314 break;
2315 /* REVISIT what to do with Carkit and I2C events ? */
2316 default:
2317 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2318 }
2319}
2320
2321static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2322{
2323 struct dwc3_event_buffer *evt;
2324 int left;
2325 u32 count;
2326
2327 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2328 count &= DWC3_GEVNTCOUNT_MASK;
2329 if (!count)
2330 return IRQ_NONE;
2331
2332 evt = dwc->ev_buffs[buf];
2333 left = count;
2334
2335 while (left > 0) {
2336 union dwc3_event event;
2337
Felipe Balbid70d8442012-02-06 13:40:17 +02002338 event.raw = *(u32 *) (evt->buf + evt->lpos);
2339
Felipe Balbi72246da2011-08-19 18:10:58 +03002340 dwc3_process_event_entry(dwc, &event);
2341 /*
2342 * XXX we wrap around correctly to the next entry as almost all
2343 * entries are 4 bytes in size. There is one entry which has 12
2344 * bytes which is a regular entry followed by 8 bytes data. ATM
2345 * I don't know how things are organized if were get next to the
2346 * a boundary so I worry about that once we try to handle that.
2347 */
2348 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2349 left -= 4;
2350
2351 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2352 }
2353
2354 return IRQ_HANDLED;
2355}
2356
2357static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2358{
2359 struct dwc3 *dwc = _dwc;
2360 int i;
2361 irqreturn_t ret = IRQ_NONE;
2362
2363 spin_lock(&dwc->lock);
2364
Felipe Balbi9f622b22011-10-12 10:31:04 +03002365 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002366 irqreturn_t status;
2367
2368 status = dwc3_process_event_buf(dwc, i);
2369 if (status == IRQ_HANDLED)
2370 ret = status;
2371 }
2372
2373 spin_unlock(&dwc->lock);
2374
2375 return ret;
2376}
2377
2378/**
2379 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002380 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002381 *
2382 * Returns 0 on success otherwise negative errno.
2383 */
2384int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2385{
2386 u32 reg;
2387 int ret;
2388 int irq;
2389
2390 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2391 &dwc->ctrl_req_addr, GFP_KERNEL);
2392 if (!dwc->ctrl_req) {
2393 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2394 ret = -ENOMEM;
2395 goto err0;
2396 }
2397
2398 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2399 &dwc->ep0_trb_addr, GFP_KERNEL);
2400 if (!dwc->ep0_trb) {
2401 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2402 ret = -ENOMEM;
2403 goto err1;
2404 }
2405
Felipe Balbib0791fb2012-05-04 12:58:14 +03002406 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002407 if (!dwc->setup_buf) {
2408 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2409 ret = -ENOMEM;
2410 goto err2;
2411 }
2412
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002413 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002414 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2415 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002416 if (!dwc->ep0_bounce) {
2417 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2418 ret = -ENOMEM;
2419 goto err3;
2420 }
2421
Felipe Balbi72246da2011-08-19 18:10:58 +03002422 dev_set_name(&dwc->gadget.dev, "gadget");
2423
2424 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002425 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002426 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2427 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002428 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002429
2430 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2431
2432 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2433 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2434 dwc->gadget.dev.release = dwc3_gadget_release;
2435 dwc->gadget.name = "dwc3-gadget";
2436
2437 /*
2438 * REVISIT: Here we should clear all pending IRQs to be
2439 * sure we're starting from a well known location.
2440 */
2441
2442 ret = dwc3_gadget_init_endpoints(dwc);
2443 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002444 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002445
2446 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2447
2448 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2449 "dwc3", dwc);
2450 if (ret) {
2451 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2452 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002453 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002454 }
2455
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002456 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2457 reg |= DWC3_DCFG_LPM_CAP;
2458 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2459
2460 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2461 reg |= DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA;
2462 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2463
Felipe Balbi72246da2011-08-19 18:10:58 +03002464 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302465 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002466 DWC3_DEVTEN_CMDCMPLTEN |
2467 DWC3_DEVTEN_ERRTICERREN |
2468 DWC3_DEVTEN_WKUPEVTEN |
2469 DWC3_DEVTEN_ULSTCNGEN |
2470 DWC3_DEVTEN_CONNECTDONEEN |
2471 DWC3_DEVTEN_USBRSTEN |
2472 DWC3_DEVTEN_DISCONNEVTEN);
2473 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2474
2475 ret = device_register(&dwc->gadget.dev);
2476 if (ret) {
2477 dev_err(dwc->dev, "failed to register gadget device\n");
2478 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002479 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 }
2481
2482 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2483 if (ret) {
2484 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002485 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002486 }
2487
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002488 if (dwc->dotg) {
2489 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2490 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2491 if (ret) {
2492 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2493 goto err7;
2494 }
Manu Gautamb5067272012-07-02 09:53:41 +05302495 } else {
2496 pm_runtime_no_callbacks(&dwc->gadget.dev);
2497 pm_runtime_set_active(&dwc->gadget.dev);
2498 pm_runtime_enable(&dwc->gadget.dev);
2499 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002500 }
2501
Felipe Balbi72246da2011-08-19 18:10:58 +03002502 return 0;
2503
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002504err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002505 device_unregister(&dwc->gadget.dev);
2506
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002507err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2509 free_irq(irq, dwc);
2510
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002511err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002512 dwc3_gadget_free_endpoints(dwc);
2513
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002514err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002515 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2516 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002517
Felipe Balbi72246da2011-08-19 18:10:58 +03002518err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002519 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002520
2521err2:
2522 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2523 dwc->ep0_trb, dwc->ep0_trb_addr);
2524
2525err1:
2526 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2527 dwc->ctrl_req, dwc->ctrl_req_addr);
2528
2529err0:
2530 return ret;
2531}
2532
2533void dwc3_gadget_exit(struct dwc3 *dwc)
2534{
2535 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002536
Manu Gautamb5067272012-07-02 09:53:41 +05302537 if (dwc->dotg) {
2538 pm_runtime_put(&dwc->gadget.dev);
2539 pm_runtime_disable(&dwc->gadget.dev);
2540 }
2541
Felipe Balbi72246da2011-08-19 18:10:58 +03002542 usb_del_gadget_udc(&dwc->gadget);
2543 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2544
2545 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2546 free_irq(irq, dwc);
2547
Felipe Balbi72246da2011-08-19 18:10:58 +03002548 dwc3_gadget_free_endpoints(dwc);
2549
Felipe Balbib0791fb2012-05-04 12:58:14 +03002550 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2551 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002552
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002553 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002554
2555 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2556 dwc->ep0_trb, dwc->ep0_trb_addr);
2557
2558 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2559 dwc->ctrl_req, dwc->ctrl_req_addr);
2560
2561 device_unregister(&dwc->gadget.dev);
2562}