blob: eba5cd02a9e7ef8e67940d40d8f4f0ee4541ae92 [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 */
112 while (--retries) {
113 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
114
Felipe Balbi8598bde2012-01-02 18:55:57 +0200115 if (DWC3_DSTS_USBLNKST(reg) == state)
116 return 0;
117
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800118 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200119 }
120
121 dev_vdbg(dwc->dev, "link state change request timed out\n");
122
123 return -ETIMEDOUT;
124}
125
Felipe Balbi457e84b2012-01-18 18:04:09 +0200126/**
127 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
128 * @dwc: pointer to our context structure
129 *
130 * This function will a best effort FIFO allocation in order
131 * to improve FIFO usage and throughput, while still allowing
132 * us to enable as many endpoints as possible.
133 *
134 * Keep in mind that this operation will be highly dependent
135 * on the configured size for RAM1 - which contains TxFifo -,
136 * the amount of endpoints enabled on coreConsultant tool, and
137 * the width of the Master Bus.
138 *
139 * In the ideal world, we would always be able to satisfy the
140 * following equation:
141 *
142 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
143 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
144 *
145 * Unfortunately, due to many variables that's not always the case.
146 */
147int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
148{
149 int last_fifo_depth = 0;
150 int ram1_depth;
151 int fifo_size;
152 int mdwidth;
153 int num;
154
155 if (!dwc->needs_fifo_resize)
156 return 0;
157
158 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
159 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
160
161 /* MDWIDTH is represented in bits, we need it in bytes */
162 mdwidth >>= 3;
163
164 /*
165 * FIXME For now we will only allocate 1 wMaxPacketSize space
166 * for each enabled endpoint, later patches will come to
167 * improve this algorithm so that we better use the internal
168 * FIFO space
169 */
170 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
171 struct dwc3_ep *dep = dwc->eps[num];
172 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200173 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200174 int tmp;
175
176 if (!(dep->number & 1))
177 continue;
178
179 if (!(dep->flags & DWC3_EP_ENABLED))
180 continue;
181
Felipe Balbi2e81c362012-02-02 13:01:12 +0200182 if (usb_endpoint_xfer_bulk(dep->desc)
183 || usb_endpoint_xfer_isoc(dep->desc))
184 mult = 3;
185
186 /*
187 * REVISIT: the following assumes we will always have enough
188 * space available on the FIFO RAM for all possible use cases.
189 * Make sure that's true somehow and change FIFO allocation
190 * accordingly.
191 *
192 * If we have Bulk or Isochronous endpoints, we want
193 * them to be able to be very, very fast. So we're giving
194 * those endpoints a fifo_size which is enough for 3 full
195 * packets
196 */
197 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200198 tmp += mdwidth;
199
200 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200201
Felipe Balbi457e84b2012-01-18 18:04:09 +0200202 fifo_size |= (last_fifo_depth << 16);
203
204 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
205 dep->name, last_fifo_depth, fifo_size & 0xffff);
206
207 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
208 fifo_size);
209
210 last_fifo_depth += (fifo_size & 0xffff);
211 }
212
213 return 0;
214}
215
Felipe Balbi72246da2011-08-19 18:10:58 +0300216void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
217 int status)
218{
219 struct dwc3 *dwc = dep->dwc;
220
221 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200222 if (req->request.num_mapped_sgs)
223 dep->busy_slot += req->request.num_mapped_sgs;
224 else
225 dep->busy_slot++;
226
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 /*
228 * Skip LINK TRB. We can't use req->trb and check for
229 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
230 * completed (not the LINK TRB).
231 */
232 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
233 usb_endpoint_xfer_isoc(dep->desc))
234 dep->busy_slot++;
235 }
236 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200237 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300238
239 if (req->request.status == -EINPROGRESS)
240 req->request.status = status;
241
Pratyush Anand7acab782012-08-10 13:42:16 +0530242 if (dwc->ep0_bounced && dep->number == 0)
243 dwc->ep0_bounced = false;
244 else
245 usb_gadget_unmap_request(&dwc->gadget, &req->request,
246 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300247
248 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
249 req, dep->name, req->request.actual,
250 req->request.length, status);
251
252 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200253 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 spin_lock(&dwc->lock);
255}
256
257static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
258{
259 switch (cmd) {
260 case DWC3_DEPCMD_DEPSTARTCFG:
261 return "Start New Configuration";
262 case DWC3_DEPCMD_ENDTRANSFER:
263 return "End Transfer";
264 case DWC3_DEPCMD_UPDATETRANSFER:
265 return "Update Transfer";
266 case DWC3_DEPCMD_STARTTRANSFER:
267 return "Start Transfer";
268 case DWC3_DEPCMD_CLEARSTALL:
269 return "Clear Stall";
270 case DWC3_DEPCMD_SETSTALL:
271 return "Set Stall";
272 case DWC3_DEPCMD_GETSEQNUMBER:
273 return "Get Data Sequence Number";
274 case DWC3_DEPCMD_SETTRANSFRESOURCE:
275 return "Set Endpoint Transfer Resource";
276 case DWC3_DEPCMD_SETEPCONFIG:
277 return "Set Endpoint Configuration";
278 default:
279 return "UNKNOWN command";
280 }
281}
282
283int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
284 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
285{
286 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200287 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300288 u32 reg;
289
290 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
291 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300292 dwc3_gadget_ep_cmd_string(cmd), params->param0,
293 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300295 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
296 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
297 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300298
299 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
300 do {
301 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
302 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300303 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
304 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 return 0;
306 }
307
308 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300309 * We can't sleep here, because it is also called from
310 * interrupt context.
311 */
312 timeout--;
313 if (!timeout)
314 return -ETIMEDOUT;
315
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200316 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300317 } while (1);
318}
319
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300320dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200321 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300322{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300323 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324
325 return dep->trb_pool_dma + offset;
326}
327
328static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
329{
330 struct dwc3 *dwc = dep->dwc;
331
332 if (dep->trb_pool)
333 return 0;
334
335 if (dep->number == 0 || dep->number == 1)
336 return 0;
337
338 dep->trb_pool = dma_alloc_coherent(dwc->dev,
339 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
340 &dep->trb_pool_dma, GFP_KERNEL);
341 if (!dep->trb_pool) {
342 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
343 dep->name);
344 return -ENOMEM;
345 }
346
347 return 0;
348}
349
350static void dwc3_free_trb_pool(struct dwc3_ep *dep)
351{
352 struct dwc3 *dwc = dep->dwc;
353
354 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
355 dep->trb_pool, dep->trb_pool_dma);
356
357 dep->trb_pool = NULL;
358 dep->trb_pool_dma = 0;
359}
360
361static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
362{
363 struct dwc3_gadget_ep_cmd_params params;
364 u32 cmd;
365
366 memset(&params, 0x00, sizeof(params));
367
368 if (dep->number != 1) {
369 cmd = DWC3_DEPCMD_DEPSTARTCFG;
370 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300371 if (dep->number > 1) {
372 if (dwc->start_config_issued)
373 return 0;
374 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300376 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300377
378 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
379 }
380
381 return 0;
382}
383
384static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200385 const struct usb_endpoint_descriptor *desc,
386 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300387{
388 struct dwc3_gadget_ep_cmd_params params;
389
390 memset(&params, 0x00, sizeof(params));
391
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300392 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
393 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
394 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300396 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
397 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300398
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200399 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300400 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
401 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300402 dep->stream_capable = true;
403 }
404
Felipe Balbi72246da2011-08-19 18:10:58 +0300405 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300406 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407
408 /*
409 * We are doing 1:1 mapping for endpoints, meaning
410 * Physical Endpoints 2 maps to Logical Endpoint 2 and
411 * so on. We consider the direction bit as part of the physical
412 * endpoint number. So USB endpoint 0x81 is 0x03.
413 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300414 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300415
416 /*
417 * We must use the lower 16 TX FIFOs even though
418 * HW might have more
419 */
420 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300421 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300422
423 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300424 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300425 dep->interval = 1 << (desc->bInterval - 1);
426 }
427
428 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
429 DWC3_DEPCMD_SETEPCONFIG, &params);
430}
431
432static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
433{
434 struct dwc3_gadget_ep_cmd_params params;
435
436 memset(&params, 0x00, sizeof(params));
437
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300438 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300439
440 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
441 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
442}
443
444/**
445 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
446 * @dep: endpoint to be initialized
447 * @desc: USB Endpoint Descriptor
448 *
449 * Caller should take care of locking
450 */
451static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200452 const struct usb_endpoint_descriptor *desc,
453 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300454{
455 struct dwc3 *dwc = dep->dwc;
456 u32 reg;
457 int ret = -ENOMEM;
458
459 if (!(dep->flags & DWC3_EP_ENABLED)) {
460 ret = dwc3_gadget_start_config(dwc, dep);
461 if (ret)
462 return ret;
463 }
464
Felipe Balbic90bfae2011-11-29 13:11:21 +0200465 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 if (ret)
467 return ret;
468
469 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200470 struct dwc3_trb *trb_st_hw;
471 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300472
473 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
474 if (ret)
475 return ret;
476
477 dep->desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200478 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479 dep->type = usb_endpoint_type(desc);
480 dep->flags |= DWC3_EP_ENABLED;
481
482 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
483 reg |= DWC3_DALEPENA_EP(dep->number);
484 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
485
486 if (!usb_endpoint_xfer_isoc(desc))
487 return 0;
488
489 memset(&trb_link, 0, sizeof(trb_link));
490
Paul Zimmerman1d046792012-02-15 18:56:56 -0800491 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300492 trb_st_hw = &dep->trb_pool[0];
493
Felipe Balbif6bafc62012-02-06 11:04:53 +0200494 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300495
Felipe Balbif6bafc62012-02-06 11:04:53 +0200496 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
497 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
498 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
499 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 }
501
502 return 0;
503}
504
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200505static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
506static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300507{
508 struct dwc3_request *req;
509
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200510 if (!list_empty(&dep->req_queued))
511 dwc3_stop_active_transfer(dwc, dep->number);
512
Felipe Balbi72246da2011-08-19 18:10:58 +0300513 while (!list_empty(&dep->request_list)) {
514 req = next_request(&dep->request_list);
515
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200516 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300518}
519
520/**
521 * __dwc3_gadget_ep_disable - Disables a HW endpoint
522 * @dep: the endpoint to disable
523 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200524 * This function also removes requests which are currently processed ny the
525 * hardware and those which are not yet scheduled.
526 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300528static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
529{
530 struct dwc3 *dwc = dep->dwc;
531 u32 reg;
532
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200533 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
Felipe Balbi4cbd8a22014-04-16 10:30:33 -0500535 /* make sure HW endpoint isn't stalled */
536 if (dep->flags & DWC3_EP_STALL)
Felipe Balbicdc52322014-09-24 14:19:52 -0500537 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi4cbd8a22014-04-16 10:30:33 -0500538
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
540 reg &= ~DWC3_DALEPENA_EP(dep->number);
541 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
542
Felipe Balbi879631a2011-09-30 10:58:47 +0300543 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 dep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200545 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200546 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300548 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
550 return 0;
551}
552
553/* -------------------------------------------------------------------------- */
554
555static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
556 const struct usb_endpoint_descriptor *desc)
557{
558 return -EINVAL;
559}
560
561static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
562{
563 return -EINVAL;
564}
565
566/* -------------------------------------------------------------------------- */
567
568static int dwc3_gadget_ep_enable(struct usb_ep *ep,
569 const struct usb_endpoint_descriptor *desc)
570{
571 struct dwc3_ep *dep;
572 struct dwc3 *dwc;
573 unsigned long flags;
574 int ret;
575
576 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
577 pr_debug("dwc3: invalid parameters\n");
578 return -EINVAL;
579 }
580
581 if (!desc->wMaxPacketSize) {
582 pr_debug("dwc3: missing wMaxPacketSize\n");
583 return -EINVAL;
584 }
585
586 dep = to_dwc3_ep(ep);
587 dwc = dep->dwc;
588
589 switch (usb_endpoint_type(desc)) {
590 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900591 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 break;
593 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900594 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 break;
596 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900597 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300598 break;
599 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900600 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 break;
602 default:
603 dev_err(dwc->dev, "invalid endpoint transfer type\n");
604 }
605
606 if (dep->flags & DWC3_EP_ENABLED) {
607 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
608 dep->name);
609 return 0;
610 }
611
612 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
613
614 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200615 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 spin_unlock_irqrestore(&dwc->lock, flags);
617
618 return ret;
619}
620
621static int dwc3_gadget_ep_disable(struct usb_ep *ep)
622{
623 struct dwc3_ep *dep;
624 struct dwc3 *dwc;
625 unsigned long flags;
626 int ret;
627
628 if (!ep) {
629 pr_debug("dwc3: invalid parameters\n");
630 return -EINVAL;
631 }
632
633 dep = to_dwc3_ep(ep);
634 dwc = dep->dwc;
635
636 if (!(dep->flags & DWC3_EP_ENABLED)) {
637 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
638 dep->name);
639 return 0;
640 }
641
642 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
643 dep->number >> 1,
644 (dep->number & 1) ? "in" : "out");
645
646 spin_lock_irqsave(&dwc->lock, flags);
647 ret = __dwc3_gadget_ep_disable(dep);
648 spin_unlock_irqrestore(&dwc->lock, flags);
649
650 return ret;
651}
652
653static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
654 gfp_t gfp_flags)
655{
656 struct dwc3_request *req;
657 struct dwc3_ep *dep = to_dwc3_ep(ep);
658 struct dwc3 *dwc = dep->dwc;
659
660 req = kzalloc(sizeof(*req), gfp_flags);
661 if (!req) {
662 dev_err(dwc->dev, "not enough memory\n");
663 return NULL;
664 }
665
666 req->epnum = dep->number;
667 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668
669 return &req->request;
670}
671
672static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
673 struct usb_request *request)
674{
675 struct dwc3_request *req = to_dwc3_request(request);
676
677 kfree(req);
678}
679
Felipe Balbic71fc372011-11-22 11:37:34 +0200680/**
681 * dwc3_prepare_one_trb - setup one TRB from one request
682 * @dep: endpoint for which this request is prepared
683 * @req: dwc3_request pointer
684 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200685static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200686 struct dwc3_request *req, dma_addr_t dma,
687 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200688{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200689 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200690 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200691
692 unsigned int cur_slot;
693
Felipe Balbieeb720f2011-11-28 12:46:59 +0200694 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
695 dep->name, req, (unsigned long long) dma,
696 length, last ? " last" : "",
697 chain ? " chain" : "");
698
Felipe Balbif6bafc62012-02-06 11:04:53 +0200699 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200700 cur_slot = dep->free_slot;
701 dep->free_slot++;
702
703 /* Skip the LINK-TRB on ISOC */
704 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
705 usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200706 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200707
Felipe Balbieeb720f2011-11-28 12:46:59 +0200708 if (!req->trb) {
709 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200710 req->trb = trb;
711 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200712 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200713
Felipe Balbif6bafc62012-02-06 11:04:53 +0200714 trb->size = DWC3_TRB_SIZE_LENGTH(length);
715 trb->bpl = lower_32_bits(dma);
716 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200717
718 switch (usb_endpoint_type(dep->desc)) {
719 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200720 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200721 break;
722
723 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200724 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200725
726 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
727 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200728 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200729 break;
730
731 case USB_ENDPOINT_XFER_BULK:
732 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200733 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200734 break;
735 default:
736 /*
737 * This is only possible with faulty memory because we
738 * checked it already :)
739 */
740 BUG();
741 }
742
Felipe Balbif6bafc62012-02-06 11:04:53 +0200743 if (usb_endpoint_xfer_isoc(dep->desc)) {
744 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
745 trb->ctrl |= DWC3_TRB_CTRL_CSP;
746 } else {
747 if (chain)
748 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200749
Felipe Balbif6bafc62012-02-06 11:04:53 +0200750 if (last)
751 trb->ctrl |= DWC3_TRB_CTRL_LST;
752 }
753
754 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
755 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
756
757 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200758}
759
Felipe Balbi72246da2011-08-19 18:10:58 +0300760/*
761 * dwc3_prepare_trbs - setup TRBs from requests
762 * @dep: endpoint for which requests are being prepared
763 * @starting: true if the endpoint is idle and no requests are queued.
764 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800765 * The function goes through the requests list and sets up TRBs for the
766 * transfers. The function returns once there are no more TRBs available or
767 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300768 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200769static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300770{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200771 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200773 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200774 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300775
776 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
777
778 /* the first request must not be queued */
779 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200781 /* Can't wrap around on a non-isoc EP since there's no link TRB */
782 if (!usb_endpoint_xfer_isoc(dep->desc)) {
783 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
784 if (trbs_left > max)
785 trbs_left = max;
786 }
787
Felipe Balbi72246da2011-08-19 18:10:58 +0300788 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800789 * If busy & slot are equal than it is either full or empty. If we are
790 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 * full and don't do anything
792 */
793 if (!trbs_left) {
794 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200795 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300796 trbs_left = DWC3_TRB_NUM;
797 /*
798 * In case we start from scratch, we queue the ISOC requests
799 * starting from slot 1. This is done because we use ring
800 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800801 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 * after the first request so we start at slot 1 and have
803 * 7 requests proceed before we hit the first IOC.
804 * Other transfer types don't use the ring buffer and are
805 * processed from the first TRB until the last one. Since we
806 * don't wrap around we have to start at the beginning.
807 */
808 if (usb_endpoint_xfer_isoc(dep->desc)) {
809 dep->busy_slot = 1;
810 dep->free_slot = 1;
811 } else {
812 dep->busy_slot = 0;
813 dep->free_slot = 0;
814 }
815 }
816
817 /* The last TRB is a link TRB, not used for xfer */
818 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200819 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300820
821 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200822 unsigned length;
823 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300824
Felipe Balbieeb720f2011-11-28 12:46:59 +0200825 if (req->request.num_mapped_sgs > 0) {
826 struct usb_request *request = &req->request;
827 struct scatterlist *sg = request->sg;
828 struct scatterlist *s;
829 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830
Felipe Balbieeb720f2011-11-28 12:46:59 +0200831 for_each_sg(sg, s, request->num_mapped_sgs, i) {
832 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
Felipe Balbieeb720f2011-11-28 12:46:59 +0200834 length = sg_dma_len(s);
835 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300836
Paul Zimmerman1d046792012-02-15 18:56:56 -0800837 if (i == (request->num_mapped_sgs - 1) ||
838 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200839 last_one = true;
840 chain = false;
841 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Felipe Balbieeb720f2011-11-28 12:46:59 +0200843 trbs_left--;
844 if (!trbs_left)
845 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300846
Felipe Balbieeb720f2011-11-28 12:46:59 +0200847 if (last_one)
848 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300849
Felipe Balbieeb720f2011-11-28 12:46:59 +0200850 dwc3_prepare_one_trb(dep, req, dma, length,
851 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300852
Felipe Balbieeb720f2011-11-28 12:46:59 +0200853 if (last_one)
854 break;
855 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300856 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200857 dma = req->request.dma;
858 length = req->request.length;
859 trbs_left--;
860
861 if (!trbs_left)
862 last_one = 1;
863
864 /* Is this the last request? */
865 if (list_is_last(&req->list, &dep->request_list))
866 last_one = 1;
867
868 dwc3_prepare_one_trb(dep, req, dma, length,
869 last_one, false);
870
871 if (last_one)
872 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300875}
876
877static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
878 int start_new)
879{
880 struct dwc3_gadget_ep_cmd_params params;
881 struct dwc3_request *req;
882 struct dwc3 *dwc = dep->dwc;
883 int ret;
884 u32 cmd;
885
886 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
887 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
888 return -EBUSY;
889 }
890 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
891
892 /*
893 * If we are getting here after a short-out-packet we don't enqueue any
894 * new requests as we try to set the IOC bit only on the last request.
895 */
896 if (start_new) {
897 if (list_empty(&dep->req_queued))
898 dwc3_prepare_trbs(dep, start_new);
899
900 /* req points to the first request which will be sent */
901 req = next_request(&dep->req_queued);
902 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200903 dwc3_prepare_trbs(dep, start_new);
904
Felipe Balbi72246da2011-08-19 18:10:58 +0300905 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800906 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300907 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200908 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300909 }
910 if (!req) {
911 dep->flags |= DWC3_EP_PENDING_REQUEST;
912 return 0;
913 }
914
915 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300916 params.param0 = upper_32_bits(req->trb_dma);
917 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
919 if (start_new)
920 cmd = DWC3_DEPCMD_STARTTRANSFER;
921 else
922 cmd = DWC3_DEPCMD_UPDATETRANSFER;
923
924 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
925 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
926 if (ret < 0) {
927 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
928
929 /*
930 * FIXME we need to iterate over the list of requests
931 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800932 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300933 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200934 usb_gadget_unmap_request(&dwc->gadget, &req->request,
935 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300936 list_del(&req->list);
937 return ret;
938 }
939
940 dep->flags |= DWC3_EP_BUSY;
941 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
942 dep->number);
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200943
944 WARN_ON_ONCE(!dep->res_trans_idx);
945
Felipe Balbi72246da2011-08-19 18:10:58 +0300946 return 0;
947}
948
949static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
950{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200951 struct dwc3 *dwc = dep->dwc;
952 int ret;
953
Felipe Balbi72246da2011-08-19 18:10:58 +0300954 req->request.actual = 0;
955 req->request.status = -EINPROGRESS;
956 req->direction = dep->direction;
957 req->epnum = dep->number;
958
959 /*
960 * We only add to our list of requests now and
961 * start consuming the list once we get XferNotReady
962 * IRQ.
963 *
964 * That way, we avoid doing anything that we don't need
965 * to do now and defer it until the point we receive a
966 * particular token from the Host side.
967 *
968 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800969 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200971 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
972 dep->direction);
973 if (ret)
974 return ret;
975
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 list_add_tail(&req->list, &dep->request_list);
977
978 /*
979 * There is one special case: XferNotReady with
980 * empty list of requests. We need to kick the
981 * transfer here in that situation, otherwise
982 * we will be NAKing forever.
983 *
984 * If we get XferNotReady before gadget driver
985 * has a chance to queue a request, we will ACK
986 * the IRQ but won't be able to receive the data
987 * until the next request is queued. The following
988 * code is handling exactly that.
989 */
990 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
991 int ret;
992 int start_trans;
993
994 start_trans = 1;
Felipe Balbi7b7dd022012-01-18 17:09:17 +0200995 if (usb_endpoint_xfer_isoc(dep->desc) &&
Paul Zimmerman1d046792012-02-15 18:56:56 -0800996 (dep->flags & DWC3_EP_BUSY))
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 start_trans = 0;
998
Paul Zimmerman1d046792012-02-15 18:56:56 -0800999 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 if (ret && ret != -EBUSY) {
1001 struct dwc3 *dwc = dep->dwc;
1002
1003 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1004 dep->name);
1005 }
1006 };
1007
1008 return 0;
1009}
1010
1011static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1012 gfp_t gfp_flags)
1013{
1014 struct dwc3_request *req = to_dwc3_request(request);
1015 struct dwc3_ep *dep = to_dwc3_ep(ep);
1016 struct dwc3 *dwc = dep->dwc;
1017
1018 unsigned long flags;
1019
1020 int ret;
1021
1022 if (!dep->desc) {
1023 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1024 request, ep->name);
1025 return -ESHUTDOWN;
1026 }
1027
1028 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1029 request, ep->name, request->length);
1030
1031 spin_lock_irqsave(&dwc->lock, flags);
1032 ret = __dwc3_gadget_ep_queue(dep, req);
1033 spin_unlock_irqrestore(&dwc->lock, flags);
1034
1035 return ret;
1036}
1037
1038static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1039 struct usb_request *request)
1040{
1041 struct dwc3_request *req = to_dwc3_request(request);
1042 struct dwc3_request *r = NULL;
1043
1044 struct dwc3_ep *dep = to_dwc3_ep(ep);
1045 struct dwc3 *dwc = dep->dwc;
1046
1047 unsigned long flags;
1048 int ret = 0;
1049
1050 spin_lock_irqsave(&dwc->lock, flags);
1051
1052 list_for_each_entry(r, &dep->request_list, list) {
1053 if (r == req)
1054 break;
1055 }
1056
1057 if (r != req) {
1058 list_for_each_entry(r, &dep->req_queued, list) {
1059 if (r == req)
1060 break;
1061 }
1062 if (r == req) {
1063 /* wait until it is processed */
1064 dwc3_stop_active_transfer(dwc, dep->number);
1065 goto out0;
1066 }
1067 dev_err(dwc->dev, "request %p was not queued to %s\n",
1068 request, ep->name);
1069 ret = -EINVAL;
1070 goto out0;
1071 }
1072
1073 /* giveback the request */
1074 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1075
1076out0:
1077 spin_unlock_irqrestore(&dwc->lock, flags);
1078
1079 return ret;
1080}
1081
Felipe Balbicdc52322014-09-24 14:19:52 -05001082int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001083{
1084 struct dwc3_gadget_ep_cmd_params params;
1085 struct dwc3 *dwc = dep->dwc;
1086 int ret;
1087
1088 memset(&params, 0x00, sizeof(params));
1089
1090 if (value) {
Felipe Balbicdc52322014-09-24 14:19:52 -05001091 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1092 (!list_empty(&dep->req_queued) ||
1093 !list_empty(&dep->request_list)))) {
1094 dev_dbg(dwc->dev, "%s: pending request, cannot halt\n",
1095 dep->name);
1096 return -EAGAIN;
1097 }
1098
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001099 if (dep->number == 0 || dep->number == 1) {
1100 /*
1101 * Whenever EP0 is stalled, we will restart
1102 * the state machine, thus moving back to
1103 * Setup Phase
1104 */
1105 dwc->ep0state = EP0_SETUP_PHASE;
1106 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001107
1108 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1109 DWC3_DEPCMD_SETSTALL, &params);
1110 if (ret)
1111 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1112 value ? "set" : "clear",
1113 dep->name);
1114 else
1115 dep->flags |= DWC3_EP_STALL;
1116 } else {
1117 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1118 DWC3_DEPCMD_CLEARSTALL, &params);
1119 if (ret)
1120 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1121 value ? "set" : "clear",
1122 dep->name);
1123 else
Alan Sternf7557c72013-11-01 12:05:12 -04001124 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001125 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001126
Felipe Balbi72246da2011-08-19 18:10:58 +03001127 return ret;
1128}
1129
1130static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1131{
1132 struct dwc3_ep *dep = to_dwc3_ep(ep);
1133 struct dwc3 *dwc = dep->dwc;
1134
1135 unsigned long flags;
1136
1137 int ret;
1138
1139 spin_lock_irqsave(&dwc->lock, flags);
1140
1141 if (usb_endpoint_xfer_isoc(dep->desc)) {
1142 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1143 ret = -EINVAL;
1144 goto out;
1145 }
1146
Felipe Balbicdc52322014-09-24 14:19:52 -05001147 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001148out:
1149 spin_unlock_irqrestore(&dwc->lock, flags);
1150
1151 return ret;
1152}
1153
1154static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1155{
1156 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001157 struct dwc3 *dwc = dep->dwc;
1158 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001159
Paul Zimmerman249a4562012-02-24 17:32:16 -08001160 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001161 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001162 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001163
Paul Zimmerman52754552011-09-30 10:58:44 +03001164 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001165}
1166
1167/* -------------------------------------------------------------------------- */
1168
1169static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1170 .bLength = USB_DT_ENDPOINT_SIZE,
1171 .bDescriptorType = USB_DT_ENDPOINT,
1172 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1173};
1174
1175static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1176 .enable = dwc3_gadget_ep0_enable,
1177 .disable = dwc3_gadget_ep0_disable,
1178 .alloc_request = dwc3_gadget_ep_alloc_request,
1179 .free_request = dwc3_gadget_ep_free_request,
1180 .queue = dwc3_gadget_ep0_queue,
1181 .dequeue = dwc3_gadget_ep_dequeue,
1182 .set_halt = dwc3_gadget_ep_set_halt,
1183 .set_wedge = dwc3_gadget_ep_set_wedge,
1184};
1185
1186static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1187 .enable = dwc3_gadget_ep_enable,
1188 .disable = dwc3_gadget_ep_disable,
1189 .alloc_request = dwc3_gadget_ep_alloc_request,
1190 .free_request = dwc3_gadget_ep_free_request,
1191 .queue = dwc3_gadget_ep_queue,
1192 .dequeue = dwc3_gadget_ep_dequeue,
1193 .set_halt = dwc3_gadget_ep_set_halt,
1194 .set_wedge = dwc3_gadget_ep_set_wedge,
1195};
1196
1197/* -------------------------------------------------------------------------- */
1198
1199static int dwc3_gadget_get_frame(struct usb_gadget *g)
1200{
1201 struct dwc3 *dwc = gadget_to_dwc(g);
1202 u32 reg;
1203
1204 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1205 return DWC3_DSTS_SOFFN(reg);
1206}
1207
1208static int dwc3_gadget_wakeup(struct usb_gadget *g)
1209{
1210 struct dwc3 *dwc = gadget_to_dwc(g);
1211
1212 unsigned long timeout;
1213 unsigned long flags;
1214
1215 u32 reg;
1216
1217 int ret = 0;
1218
1219 u8 link_state;
1220 u8 speed;
1221
1222 spin_lock_irqsave(&dwc->lock, flags);
1223
1224 /*
1225 * According to the Databook Remote wakeup request should
1226 * be issued only when the device is in early suspend state.
1227 *
1228 * We can check that via USB Link State bits in DSTS register.
1229 */
1230 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1231
1232 speed = reg & DWC3_DSTS_CONNECTSPD;
1233 if (speed == DWC3_DSTS_SUPERSPEED) {
1234 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1235 ret = -EINVAL;
1236 goto out;
1237 }
1238
1239 link_state = DWC3_DSTS_USBLNKST(reg);
1240
1241 switch (link_state) {
1242 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1243 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1244 break;
1245 default:
1246 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1247 link_state);
1248 ret = -EINVAL;
1249 goto out;
1250 }
1251
Felipe Balbi8598bde2012-01-02 18:55:57 +02001252 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1253 if (ret < 0) {
1254 dev_err(dwc->dev, "failed to put link in Recovery\n");
1255 goto out;
1256 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001257
1258 /* write zeroes to Link Change Request */
1259 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1260 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1261
Paul Zimmerman1d046792012-02-15 18:56:56 -08001262 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 timeout = jiffies + msecs_to_jiffies(100);
1264
Paul Zimmerman1d046792012-02-15 18:56:56 -08001265 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1267
1268 /* in HS, means ON */
1269 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1270 break;
1271 }
1272
1273 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1274 dev_err(dwc->dev, "failed to send remote wakeup\n");
1275 ret = -EINVAL;
1276 }
1277
1278out:
1279 spin_unlock_irqrestore(&dwc->lock, flags);
1280
1281 return ret;
1282}
1283
1284static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1285 int is_selfpowered)
1286{
1287 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001288 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001289
Paul Zimmerman249a4562012-02-24 17:32:16 -08001290 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001291 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001292 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001293
1294 return 0;
1295}
1296
1297static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1298{
1299 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001300 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001301
1302 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001303 if (is_on) {
1304 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1305 reg |= (DWC3_DCTL_RUN_STOP
1306 | DWC3_DCTL_TRGTULST_RX_DET);
1307 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001308 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001309 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001310
1311 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1312
1313 do {
1314 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1315 if (is_on) {
1316 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1317 break;
1318 } else {
1319 if (reg & DWC3_DSTS_DEVCTRLHLT)
1320 break;
1321 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001322 timeout--;
1323 if (!timeout)
1324 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001325 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 } while (1);
1327
1328 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1329 dwc->gadget_driver
1330 ? dwc->gadget_driver->function : "no-function",
1331 is_on ? "connect" : "disconnect");
1332}
1333
1334static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1335{
1336 struct dwc3 *dwc = gadget_to_dwc(g);
1337 unsigned long flags;
1338
1339 is_on = !!is_on;
1340
1341 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001342
1343 dwc->softconnect = is_on;
1344
1345 if ((dwc->dotg && !dwc->vbus_active) ||
1346 !dwc->gadget_driver) {
1347
1348 spin_unlock_irqrestore(&dwc->lock, flags);
1349
1350 /*
1351 * Need to wait for vbus_session(on) from otg driver or to
1352 * the udc_start.
1353 */
1354 return 0;
1355 }
1356
Felipe Balbi72246da2011-08-19 18:10:58 +03001357 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001358
1359 spin_unlock_irqrestore(&dwc->lock, flags);
1360
1361 return 0;
1362}
1363
1364static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1365{
1366 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1367 unsigned long flags;
1368
1369 if (!dwc->dotg)
1370 return -EPERM;
1371
1372 is_active = !!is_active;
1373
1374 spin_lock_irqsave(&dwc->lock, flags);
1375
1376 /* Mark that the vbus was powered */
1377 dwc->vbus_active = is_active;
1378
1379 /*
1380 * Check if upper level usb_gadget_driver was already registerd with
1381 * this udc controller driver (if dwc3_gadget_start was called)
1382 */
1383 if (dwc->gadget_driver && dwc->softconnect) {
1384 if (dwc->vbus_active) {
1385 /*
1386 * Both vbus was activated by otg and pullup was
1387 * signaled by the gadget driver.
1388 */
1389 dwc3_gadget_run_stop(dwc, 1);
1390 } else {
1391 dwc3_gadget_run_stop(dwc, 0);
1392 }
1393 }
1394
Felipe Balbi72246da2011-08-19 18:10:58 +03001395 spin_unlock_irqrestore(&dwc->lock, flags);
1396
1397 return 0;
1398}
1399
1400static int dwc3_gadget_start(struct usb_gadget *g,
1401 struct usb_gadget_driver *driver)
1402{
1403 struct dwc3 *dwc = gadget_to_dwc(g);
1404 struct dwc3_ep *dep;
1405 unsigned long flags;
1406 int ret = 0;
1407 u32 reg;
1408
1409 spin_lock_irqsave(&dwc->lock, flags);
1410
1411 if (dwc->gadget_driver) {
1412 dev_err(dwc->dev, "%s is already bound to %s\n",
1413 dwc->gadget.name,
1414 dwc->gadget_driver->driver.name);
1415 ret = -EBUSY;
1416 goto err0;
1417 }
1418
1419 dwc->gadget_driver = driver;
1420 dwc->gadget.dev.driver = &driver->driver;
1421
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1423 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001424 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001425 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1426
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001427 dwc->start_config_issued = false;
1428
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 /* Start with SuperSpeed Default */
1430 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1431
1432 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001433 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001434 if (ret) {
1435 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1436 goto err0;
1437 }
1438
1439 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001440 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001441 if (ret) {
1442 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1443 goto err1;
1444 }
1445
1446 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001447 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001448 dwc3_ep0_out_start(dwc);
1449
1450 spin_unlock_irqrestore(&dwc->lock, flags);
1451
1452 return 0;
1453
1454err1:
1455 __dwc3_gadget_ep_disable(dwc->eps[0]);
1456
1457err0:
Felipe Balbi1b5c94b2013-07-15 12:36:35 +03001458 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001459 spin_unlock_irqrestore(&dwc->lock, flags);
1460
1461 return ret;
1462}
1463
1464static int dwc3_gadget_stop(struct usb_gadget *g,
1465 struct usb_gadget_driver *driver)
1466{
1467 struct dwc3 *dwc = gadget_to_dwc(g);
1468 unsigned long flags;
1469
1470 spin_lock_irqsave(&dwc->lock, flags);
1471
1472 __dwc3_gadget_ep_disable(dwc->eps[0]);
1473 __dwc3_gadget_ep_disable(dwc->eps[1]);
1474
1475 dwc->gadget_driver = NULL;
1476 dwc->gadget.dev.driver = NULL;
1477
1478 spin_unlock_irqrestore(&dwc->lock, flags);
1479
1480 return 0;
1481}
1482static const struct usb_gadget_ops dwc3_gadget_ops = {
1483 .get_frame = dwc3_gadget_get_frame,
1484 .wakeup = dwc3_gadget_wakeup,
1485 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001486 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 .pullup = dwc3_gadget_pullup,
1488 .udc_start = dwc3_gadget_start,
1489 .udc_stop = dwc3_gadget_stop,
1490};
1491
1492/* -------------------------------------------------------------------------- */
1493
1494static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1495{
1496 struct dwc3_ep *dep;
1497 u8 epnum;
1498
1499 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1500
1501 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1502 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1503 if (!dep) {
1504 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1505 epnum);
1506 return -ENOMEM;
1507 }
1508
1509 dep->dwc = dwc;
1510 dep->number = epnum;
1511 dwc->eps[epnum] = dep;
1512
1513 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1514 (epnum & 1) ? "in" : "out");
1515 dep->endpoint.name = dep->name;
1516 dep->direction = (epnum & 1);
1517
1518 if (epnum == 0 || epnum == 1) {
1519 dep->endpoint.maxpacket = 512;
Pratyush Anand462434e2013-01-18 16:53:56 +05301520 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001521 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1522 if (!epnum)
1523 dwc->gadget.ep0 = &dep->endpoint;
1524 } else {
1525 int ret;
1526
1527 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001528 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1530 list_add_tail(&dep->endpoint.ep_list,
1531 &dwc->gadget.ep_list);
1532
1533 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001534 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001535 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001537
Felipe Balbi72246da2011-08-19 18:10:58 +03001538 INIT_LIST_HEAD(&dep->request_list);
1539 INIT_LIST_HEAD(&dep->req_queued);
1540 }
1541
1542 return 0;
1543}
1544
1545static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1546{
1547 struct dwc3_ep *dep;
1548 u8 epnum;
1549
1550 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1551 dep = dwc->eps[epnum];
George Cherianfba59fb2013-05-27 14:35:49 +05301552 /*
1553 * Physical endpoints 0 and 1 are special; they form the
1554 * bi-directional USB endpoint 0.
1555 *
1556 * For those two physical endpoints, we don't allocate a TRB
1557 * pool nor do we add them the endpoints list. Due to that, we
1558 * shouldn't do these two operations otherwise we would end up
1559 * with all sorts of bugs when removing dwc3.ko.
1560 */
1561 if (epnum != 0 && epnum != 1) {
1562 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 list_del(&dep->endpoint.ep_list);
George Cherianfba59fb2013-05-27 14:35:49 +05301564 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001565
1566 kfree(dep);
1567 }
1568}
1569
1570static void dwc3_gadget_release(struct device *dev)
1571{
1572 dev_dbg(dev, "%s\n", __func__);
1573}
1574
1575/* -------------------------------------------------------------------------- */
1576static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1577 const struct dwc3_event_depevt *event, int status)
1578{
1579 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001580 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001581 unsigned int count;
1582 unsigned int s_pkt = 0;
1583
1584 do {
1585 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001586 if (!req) {
1587 WARN_ON_ONCE(1);
1588 return 1;
1589 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001590
Felipe Balbif6bafc62012-02-06 11:04:53 +02001591 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001592
Felipe Balbif6bafc62012-02-06 11:04:53 +02001593 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001594 /*
1595 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001596 * can do. If we don't clean it up we loop forever. If
1597 * we skip the TRB then it gets overwritten after a
1598 * while since we use them in a ring buffer. A BUG()
1599 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001600 * fixes the root cause instead of looking away :)
1601 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001602 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1603 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001604 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001605
1606 if (dep->direction) {
1607 if (count) {
1608 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1609 dep->name);
1610 status = -ECONNRESET;
1611 }
1612 } else {
1613 if (count && (event->status & DEPEVT_STATUS_SHORT))
1614 s_pkt = 1;
1615 }
1616
1617 /*
1618 * We assume here we will always receive the entire data block
1619 * which we should receive. Meaning, if we program RX to
1620 * receive 4K but we receive only 2K, we assume that's all we
1621 * should receive and we simply bounce the request back to the
1622 * gadget driver for further processing.
1623 */
1624 req->request.actual += req->request.length - count;
1625 dwc3_gadget_giveback(dep, req, status);
1626 if (s_pkt)
1627 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001628 if ((event->status & DEPEVT_STATUS_LST) &&
1629 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001631 if ((event->status & DEPEVT_STATUS_IOC) &&
1632 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001633 break;
1634 } while (1);
1635
Felipe Balbif6bafc62012-02-06 11:04:53 +02001636 if ((event->status & DEPEVT_STATUS_IOC) &&
1637 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 return 0;
1639 return 1;
1640}
1641
1642static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1643 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1644 int start_new)
1645{
1646 unsigned status = 0;
1647 int clean_busy;
1648
1649 if (event->status & DEPEVT_STATUS_BUSERR)
1650 status = -ECONNRESET;
1651
Paul Zimmerman1d046792012-02-15 18:56:56 -08001652 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001653 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001655
1656 /*
1657 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1658 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1659 */
1660 if (dwc->revision < DWC3_REVISION_183A) {
1661 u32 reg;
1662 int i;
1663
1664 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1665 struct dwc3_ep *dep = dwc->eps[i];
1666
1667 if (!(dep->flags & DWC3_EP_ENABLED))
1668 continue;
1669
1670 if (!list_empty(&dep->req_queued))
1671 return;
1672 }
1673
1674 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1675 reg |= dwc->u1u2;
1676 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1677
1678 dwc->u1u2 = 0;
1679 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001680}
1681
1682static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1683 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1684{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001685 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001686
1687 if (list_empty(&dep->request_list)) {
1688 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1689 dep->name);
1690 return;
1691 }
1692
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001693 mask = ~(dep->interval - 1);
1694 uf = event->parameters & mask;
1695 /* 4 micro frames in the future */
1696 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001697
1698 __dwc3_gadget_kick_transfer(dep, uf, 1);
1699}
1700
1701static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1702 const struct dwc3_event_depevt *event)
1703{
1704 struct dwc3 *dwc = dep->dwc;
1705 struct dwc3_event_depevt mod_ev = *event;
1706
1707 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001708 * We were asked to remove one request. It is possible that this
1709 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001710 * transfer index. Since we stopped the complete endpoint we don't
1711 * know how many requests were already completed (and not yet)
1712 * reported and how could be done (later). We purge them all until
1713 * the end of the list.
1714 */
1715 mod_ev.status = DEPEVT_STATUS_LST;
1716 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1717 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001718 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001719}
1720
1721static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1722 const struct dwc3_event_depevt *event)
1723{
1724 u32 param = event->parameters;
1725 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1726
1727 switch (cmd_type) {
1728 case DWC3_DEPCMD_ENDTRANSFER:
1729 dwc3_process_ep_cmd_complete(dep, event);
1730 break;
1731 case DWC3_DEPCMD_STARTTRANSFER:
1732 dep->res_trans_idx = param & 0x7f;
1733 break;
1734 default:
1735 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1736 __func__, cmd_type);
1737 break;
1738 };
1739}
1740
1741static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1742 const struct dwc3_event_depevt *event)
1743{
1744 struct dwc3_ep *dep;
1745 u8 epnum = event->endpoint_number;
1746
1747 dep = dwc->eps[epnum];
1748
1749 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1750 dwc3_ep_event_string(event->endpoint_event));
1751
1752 if (epnum == 0 || epnum == 1) {
1753 dwc3_ep0_interrupt(dwc, event);
1754 return;
1755 }
1756
1757 switch (event->endpoint_event) {
1758 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001759 dep->res_trans_idx = 0;
1760
Felipe Balbi72246da2011-08-19 18:10:58 +03001761 if (usb_endpoint_xfer_isoc(dep->desc)) {
1762 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1763 dep->name);
1764 return;
1765 }
1766
1767 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1768 break;
1769 case DWC3_DEPEVT_XFERINPROGRESS:
1770 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1771 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1772 dep->name);
1773 return;
1774 }
1775
1776 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1777 break;
1778 case DWC3_DEPEVT_XFERNOTREADY:
1779 if (usb_endpoint_xfer_isoc(dep->desc)) {
1780 dwc3_gadget_start_isoc(dwc, dep, event);
1781 } else {
1782 int ret;
1783
1784 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001785 dep->name, event->status &
1786 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001787 ? "Transfer Active"
1788 : "Transfer Not Active");
1789
1790 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1791 if (!ret || ret == -EBUSY)
1792 return;
1793
1794 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1795 dep->name);
1796 }
1797
1798 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001799 case DWC3_DEPEVT_STREAMEVT:
1800 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1801 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1802 dep->name);
1803 return;
1804 }
1805
1806 switch (event->status) {
1807 case DEPEVT_STREAMEVT_FOUND:
1808 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1809 event->parameters);
1810
1811 break;
1812 case DEPEVT_STREAMEVT_NOTFOUND:
1813 /* FALLTHROUGH */
1814 default:
1815 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1816 }
1817 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001818 case DWC3_DEPEVT_RXTXFIFOEVT:
1819 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1820 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001821 case DWC3_DEPEVT_EPCMDCMPLT:
1822 dwc3_ep_cmd_compl(dep, event);
1823 break;
1824 }
1825}
1826
1827static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1828{
1829 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1830 spin_unlock(&dwc->lock);
1831 dwc->gadget_driver->disconnect(&dwc->gadget);
1832 spin_lock(&dwc->lock);
1833 }
1834}
1835
1836static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1837{
1838 struct dwc3_ep *dep;
1839 struct dwc3_gadget_ep_cmd_params params;
1840 u32 cmd;
1841 int ret;
1842
1843 dep = dwc->eps[epnum];
1844
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001845 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 if (dep->res_trans_idx) {
1847 cmd = DWC3_DEPCMD_ENDTRANSFER;
1848 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1849 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1850 memset(&params, 0, sizeof(params));
1851 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1852 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001853 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001854 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001855 }
1856}
1857
1858static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1859{
1860 u32 epnum;
1861
1862 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1863 struct dwc3_ep *dep;
1864
1865 dep = dwc->eps[epnum];
1866 if (!(dep->flags & DWC3_EP_ENABLED))
1867 continue;
1868
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001869 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001870 }
1871}
1872
1873static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1874{
1875 u32 epnum;
1876
1877 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1878 struct dwc3_ep *dep;
1879 struct dwc3_gadget_ep_cmd_params params;
1880 int ret;
1881
1882 dep = dwc->eps[epnum];
1883
1884 if (!(dep->flags & DWC3_EP_STALL))
1885 continue;
1886
1887 dep->flags &= ~DWC3_EP_STALL;
1888
1889 memset(&params, 0, sizeof(params));
1890 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1891 DWC3_DEPCMD_CLEARSTALL, &params);
1892 WARN_ON_ONCE(ret);
1893 }
1894}
1895
1896static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1897{
1898 dev_vdbg(dwc->dev, "%s\n", __func__);
1899#if 0
1900 XXX
1901 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1902 enable it before we can disable it.
1903
1904 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1905 reg &= ~DWC3_DCTL_INITU1ENA;
1906 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1907
1908 reg &= ~DWC3_DCTL_INITU2ENA;
1909 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1910#endif
1911
1912 dwc3_stop_active_transfers(dwc);
1913 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001914 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001915
1916 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001917 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001918}
1919
1920static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1921{
1922 u32 reg;
1923
1924 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1925
1926 if (on)
1927 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1928 else
1929 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1930
1931 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1932}
1933
1934static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1935{
1936 u32 reg;
1937
1938 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1939
1940 if (on)
1941 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1942 else
1943 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1944
1945 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1946}
1947
1948static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1949{
1950 u32 reg;
1951
1952 dev_vdbg(dwc->dev, "%s\n", __func__);
1953
Felipe Balbidf62df52011-10-14 15:11:49 +03001954 /*
1955 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1956 * would cause a missing Disconnect Event if there's a
1957 * pending Setup Packet in the FIFO.
1958 *
1959 * There's no suggested workaround on the official Bug
1960 * report, which states that "unless the driver/application
1961 * is doing any special handling of a disconnect event,
1962 * there is no functional issue".
1963 *
1964 * Unfortunately, it turns out that we _do_ some special
1965 * handling of a disconnect event, namely complete all
1966 * pending transfers, notify gadget driver of the
1967 * disconnection, and so on.
1968 *
1969 * Our suggested workaround is to follow the Disconnect
1970 * Event steps here, instead, based on a setup_packet_pending
1971 * flag. Such flag gets set whenever we have a XferNotReady
1972 * event on EP0 and gets cleared on XferComplete for the
1973 * same endpoint.
1974 *
1975 * Refers to:
1976 *
1977 * STAR#9000466709: RTL: Device : Disconnect event not
1978 * generated if setup packet pending in FIFO
1979 */
1980 if (dwc->revision < DWC3_REVISION_188A) {
1981 if (dwc->setup_packet_pending)
1982 dwc3_gadget_disconnect_interrupt(dwc);
1983 }
1984
Felipe Balbi961906e2011-12-20 15:37:21 +02001985 /* after reset -> Default State */
1986 dwc->dev_state = DWC3_DEFAULT_STATE;
1987
Felipe Balbi72246da2011-08-19 18:10:58 +03001988 /* Enable PHYs */
1989 dwc3_gadget_usb2_phy_power(dwc, true);
1990 dwc3_gadget_usb3_phy_power(dwc, true);
1991
1992 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1993 dwc3_disconnect_gadget(dwc);
1994
1995 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1996 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1997 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001998 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001999
2000 dwc3_stop_active_transfers(dwc);
2001 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002002 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002003
2004 /* Reset device address to zero */
2005 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2006 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2007 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002008}
2009
2010static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2011{
2012 u32 reg;
2013 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2014
2015 /*
2016 * We change the clock only at SS but I dunno why I would want to do
2017 * this. Maybe it becomes part of the power saving plan.
2018 */
2019
2020 if (speed != DWC3_DSTS_SUPERSPEED)
2021 return;
2022
2023 /*
2024 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2025 * each time on Connect Done.
2026 */
2027 if (!usb30_clock)
2028 return;
2029
2030 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2031 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2032 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2033}
2034
2035static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
2036{
2037 switch (speed) {
2038 case USB_SPEED_SUPER:
2039 dwc3_gadget_usb2_phy_power(dwc, false);
2040 break;
2041 case USB_SPEED_HIGH:
2042 case USB_SPEED_FULL:
2043 case USB_SPEED_LOW:
2044 dwc3_gadget_usb3_phy_power(dwc, false);
2045 break;
2046 }
2047}
2048
2049static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2050{
2051 struct dwc3_gadget_ep_cmd_params params;
2052 struct dwc3_ep *dep;
2053 int ret;
2054 u32 reg;
2055 u8 speed;
2056
2057 dev_vdbg(dwc->dev, "%s\n", __func__);
2058
2059 memset(&params, 0x00, sizeof(params));
2060
Felipe Balbi72246da2011-08-19 18:10:58 +03002061 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2062 speed = reg & DWC3_DSTS_CONNECTSPD;
2063 dwc->speed = speed;
2064
2065 dwc3_update_ram_clk_sel(dwc, speed);
2066
2067 switch (speed) {
2068 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002069 /*
2070 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2071 * would cause a missing USB3 Reset event.
2072 *
2073 * In such situations, we should force a USB3 Reset
2074 * event by calling our dwc3_gadget_reset_interrupt()
2075 * routine.
2076 *
2077 * Refers to:
2078 *
2079 * STAR#9000483510: RTL: SS : USB3 reset event may
2080 * not be generated always when the link enters poll
2081 */
2082 if (dwc->revision < DWC3_REVISION_190A)
2083 dwc3_gadget_reset_interrupt(dwc);
2084
Felipe Balbi72246da2011-08-19 18:10:58 +03002085 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2086 dwc->gadget.ep0->maxpacket = 512;
2087 dwc->gadget.speed = USB_SPEED_SUPER;
2088 break;
2089 case DWC3_DCFG_HIGHSPEED:
2090 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2091 dwc->gadget.ep0->maxpacket = 64;
2092 dwc->gadget.speed = USB_SPEED_HIGH;
2093 break;
2094 case DWC3_DCFG_FULLSPEED2:
2095 case DWC3_DCFG_FULLSPEED1:
2096 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2097 dwc->gadget.ep0->maxpacket = 64;
2098 dwc->gadget.speed = USB_SPEED_FULL;
2099 break;
2100 case DWC3_DCFG_LOWSPEED:
2101 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2102 dwc->gadget.ep0->maxpacket = 8;
2103 dwc->gadget.speed = USB_SPEED_LOW;
2104 break;
2105 }
2106
2107 /* Disable unneded PHY */
2108 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2109
2110 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002111 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002112 if (ret) {
2113 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2114 return;
2115 }
2116
2117 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002118 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 if (ret) {
2120 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2121 return;
2122 }
2123
2124 /*
2125 * Configure PHY via GUSB3PIPECTLn if required.
2126 *
2127 * Update GTXFIFOSIZn
2128 *
2129 * In both cases reset values should be sufficient.
2130 */
2131}
2132
2133static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2134{
2135 dev_vdbg(dwc->dev, "%s\n", __func__);
2136
2137 /*
2138 * TODO take core out of low power mode when that's
2139 * implemented.
2140 */
2141
2142 dwc->gadget_driver->resume(&dwc->gadget);
2143}
2144
2145static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2146 unsigned int evtinfo)
2147{
Felipe Balbifae2b902011-10-14 13:00:30 +03002148 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2149
2150 /*
2151 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2152 * on the link partner, the USB session might do multiple entry/exit
2153 * of low power states before a transfer takes place.
2154 *
2155 * Due to this problem, we might experience lower throughput. The
2156 * suggested workaround is to disable DCTL[12:9] bits if we're
2157 * transitioning from U1/U2 to U0 and enable those bits again
2158 * after a transfer completes and there are no pending transfers
2159 * on any of the enabled endpoints.
2160 *
2161 * This is the first half of that workaround.
2162 *
2163 * Refers to:
2164 *
2165 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2166 * core send LGO_Ux entering U0
2167 */
2168 if (dwc->revision < DWC3_REVISION_183A) {
2169 if (next == DWC3_LINK_STATE_U0) {
2170 u32 u1u2;
2171 u32 reg;
2172
2173 switch (dwc->link_state) {
2174 case DWC3_LINK_STATE_U1:
2175 case DWC3_LINK_STATE_U2:
2176 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2177 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2178 | DWC3_DCTL_ACCEPTU2ENA
2179 | DWC3_DCTL_INITU1ENA
2180 | DWC3_DCTL_ACCEPTU1ENA);
2181
2182 if (!dwc->u1u2)
2183 dwc->u1u2 = reg & u1u2;
2184
2185 reg &= ~u1u2;
2186
2187 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2188 break;
2189 default:
2190 /* do nothing */
2191 break;
2192 }
2193 }
2194 }
2195
2196 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002197
2198 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002199}
2200
2201static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2202 const struct dwc3_event_devt *event)
2203{
2204 switch (event->type) {
2205 case DWC3_DEVICE_EVENT_DISCONNECT:
2206 dwc3_gadget_disconnect_interrupt(dwc);
2207 break;
2208 case DWC3_DEVICE_EVENT_RESET:
2209 dwc3_gadget_reset_interrupt(dwc);
2210 break;
2211 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2212 dwc3_gadget_conndone_interrupt(dwc);
2213 break;
2214 case DWC3_DEVICE_EVENT_WAKEUP:
2215 dwc3_gadget_wakeup_interrupt(dwc);
2216 break;
2217 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2218 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2219 break;
2220 case DWC3_DEVICE_EVENT_EOPF:
2221 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2222 break;
2223 case DWC3_DEVICE_EVENT_SOF:
2224 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2225 break;
2226 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2227 dev_vdbg(dwc->dev, "Erratic Error\n");
2228 break;
2229 case DWC3_DEVICE_EVENT_CMD_CMPL:
2230 dev_vdbg(dwc->dev, "Command Complete\n");
2231 break;
2232 case DWC3_DEVICE_EVENT_OVERFLOW:
2233 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302234 /*
2235 * Controllers prior to 2.30a revision has a bug where
2236 * Overflow Event may overwrite an unacknowledged event
2237 * in the event buffer. The severity of the issue depends
2238 * on the overwritten event type. Add a warning message
2239 * saying that an event is overwritten.
2240 *
2241 * TODO: In future we may need to see if we can re-enumerate
2242 * with host.
2243 */
2244 if (dwc->revision < DWC3_REVISION_230A)
2245 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002246 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302247 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2248 /*
2249 * Controllers prior to 2.30a revision has a bug, due to which
2250 * a vendor device test LMP event can not be filtered. But
2251 * this event is not handled in the current code. This is a
2252 * special event and 8 bytes of data will follow the event.
2253 * Handling this event is tricky when event buffer is almost
2254 * full. Moreover this event will not occur in normal scenario
2255 * and can only happen with special hosts in testing scenarios.
2256 * Add a warning message to indicate that this event is received
2257 * which means that event buffer might have corrupted.
2258 */
2259 if (dwc->revision < DWC3_REVISION_230A)
2260 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2261 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002262 default:
2263 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2264 }
2265}
2266
2267static void dwc3_process_event_entry(struct dwc3 *dwc,
2268 const union dwc3_event *event)
2269{
2270 /* Endpoint IRQ, handle it and return early */
2271 if (event->type.is_devspec == 0) {
2272 /* depevt */
2273 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2274 }
2275
2276 switch (event->type.type) {
2277 case DWC3_EVENT_TYPE_DEV:
2278 dwc3_gadget_interrupt(dwc, &event->devt);
2279 break;
2280 /* REVISIT what to do with Carkit and I2C events ? */
2281 default:
2282 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2283 }
2284}
2285
2286static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2287{
2288 struct dwc3_event_buffer *evt;
2289 int left;
2290 u32 count;
2291
2292 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2293 count &= DWC3_GEVNTCOUNT_MASK;
2294 if (!count)
2295 return IRQ_NONE;
2296
2297 evt = dwc->ev_buffs[buf];
2298 left = count;
2299
2300 while (left > 0) {
2301 union dwc3_event event;
2302
Felipe Balbid70d8442012-02-06 13:40:17 +02002303 event.raw = *(u32 *) (evt->buf + evt->lpos);
2304
Felipe Balbi72246da2011-08-19 18:10:58 +03002305 dwc3_process_event_entry(dwc, &event);
2306 /*
2307 * XXX we wrap around correctly to the next entry as almost all
2308 * entries are 4 bytes in size. There is one entry which has 12
2309 * bytes which is a regular entry followed by 8 bytes data. ATM
2310 * I don't know how things are organized if were get next to the
2311 * a boundary so I worry about that once we try to handle that.
2312 */
2313 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2314 left -= 4;
2315
2316 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2317 }
2318
2319 return IRQ_HANDLED;
2320}
2321
2322static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2323{
2324 struct dwc3 *dwc = _dwc;
2325 int i;
2326 irqreturn_t ret = IRQ_NONE;
2327
2328 spin_lock(&dwc->lock);
2329
Felipe Balbi9f622b22011-10-12 10:31:04 +03002330 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 irqreturn_t status;
2332
2333 status = dwc3_process_event_buf(dwc, i);
2334 if (status == IRQ_HANDLED)
2335 ret = status;
2336 }
2337
2338 spin_unlock(&dwc->lock);
2339
2340 return ret;
2341}
2342
2343/**
2344 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002345 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002346 *
2347 * Returns 0 on success otherwise negative errno.
2348 */
2349int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2350{
2351 u32 reg;
2352 int ret;
2353 int irq;
2354
2355 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2356 &dwc->ctrl_req_addr, GFP_KERNEL);
2357 if (!dwc->ctrl_req) {
2358 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2359 ret = -ENOMEM;
2360 goto err0;
2361 }
2362
2363 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2364 &dwc->ep0_trb_addr, GFP_KERNEL);
2365 if (!dwc->ep0_trb) {
2366 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2367 ret = -ENOMEM;
2368 goto err1;
2369 }
2370
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002371 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2372 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 if (!dwc->setup_buf) {
2374 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2375 ret = -ENOMEM;
2376 goto err2;
2377 }
2378
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002379 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2380 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2381 if (!dwc->ep0_bounce) {
2382 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2383 ret = -ENOMEM;
2384 goto err3;
2385 }
2386
Felipe Balbi72246da2011-08-19 18:10:58 +03002387 dev_set_name(&dwc->gadget.dev, "gadget");
2388
2389 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002390 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2392 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002393 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002394
2395 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2396
2397 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2398 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2399 dwc->gadget.dev.release = dwc3_gadget_release;
2400 dwc->gadget.name = "dwc3-gadget";
2401
2402 /*
2403 * REVISIT: Here we should clear all pending IRQs to be
2404 * sure we're starting from a well known location.
2405 */
2406
2407 ret = dwc3_gadget_init_endpoints(dwc);
2408 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002409 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002410
2411 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2412
2413 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2414 "dwc3", dwc);
2415 if (ret) {
2416 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2417 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002418 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002419 }
2420
2421 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302422 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002423 DWC3_DEVTEN_CMDCMPLTEN |
2424 DWC3_DEVTEN_ERRTICERREN |
2425 DWC3_DEVTEN_WKUPEVTEN |
2426 DWC3_DEVTEN_ULSTCNGEN |
2427 DWC3_DEVTEN_CONNECTDONEEN |
2428 DWC3_DEVTEN_USBRSTEN |
2429 DWC3_DEVTEN_DISCONNEVTEN);
2430 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2431
2432 ret = device_register(&dwc->gadget.dev);
2433 if (ret) {
2434 dev_err(dwc->dev, "failed to register gadget device\n");
2435 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002436 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002437 }
2438
2439 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2440 if (ret) {
2441 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002442 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002443 }
2444
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002445 if (dwc->dotg) {
2446 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2447 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2448 if (ret) {
2449 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2450 goto err7;
2451 }
Manu Gautamb5067272012-07-02 09:53:41 +05302452 } else {
2453 pm_runtime_no_callbacks(&dwc->gadget.dev);
2454 pm_runtime_set_active(&dwc->gadget.dev);
2455 pm_runtime_enable(&dwc->gadget.dev);
2456 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002457 }
2458
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 return 0;
2460
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002461err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002462 device_unregister(&dwc->gadget.dev);
2463
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002464err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002465 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2466 free_irq(irq, dwc);
2467
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002468err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002469 dwc3_gadget_free_endpoints(dwc);
2470
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002471err4:
2472 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2473 dwc->ep0_bounce_addr);
2474
Felipe Balbi72246da2011-08-19 18:10:58 +03002475err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002476 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002477
2478err2:
2479 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2480 dwc->ep0_trb, dwc->ep0_trb_addr);
2481
2482err1:
2483 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2484 dwc->ctrl_req, dwc->ctrl_req_addr);
2485
2486err0:
2487 return ret;
2488}
2489
2490void dwc3_gadget_exit(struct dwc3 *dwc)
2491{
2492 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002493
Manu Gautamb5067272012-07-02 09:53:41 +05302494 if (dwc->dotg) {
2495 pm_runtime_put(&dwc->gadget.dev);
2496 pm_runtime_disable(&dwc->gadget.dev);
2497 }
2498
Felipe Balbi72246da2011-08-19 18:10:58 +03002499 usb_del_gadget_udc(&dwc->gadget);
2500 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2501
2502 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2503 free_irq(irq, dwc);
2504
Felipe Balbi72246da2011-08-19 18:10:58 +03002505 dwc3_gadget_free_endpoints(dwc);
2506
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002507 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2508 dwc->ep0_bounce_addr);
2509
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002510 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002511
2512 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2513 dwc->ep0_trb, dwc->ep0_trb_addr);
2514
2515 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2516 dwc->ctrl_req, dwc->ctrl_req_addr);
2517
2518 device_unregister(&dwc->gadget.dev);
2519}