blob: 3e497943e88d5b6f77a3eb2c28222c059e6688ca [file] [log] [blame]
Felipe Balbi4dc64e52011-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
5 * All rights reserved.
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the above-listed copyright holders may not be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2, as published by the Free
25 * Software Foundation.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/interrupt.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/dma-mapping.h>
50
51#include <linux/usb/ch9.h>
52#include <linux/usb/gadget.h>
53
54#include "core.h"
55#include "gadget.h"
56#include "io.h"
57
58#define DMA_ADDR_INVALID (~(dma_addr_t)0)
59
60void dwc3_map_buffer_to_dma(struct dwc3_request *req)
61{
62 struct dwc3 *dwc = req->dep->dwc;
63
Sebastian Andrzej Siewior4ae8e1c2011-08-31 17:12:02 +020064 if (req->request.length == 0) {
65 /* req->request.dma = dwc->setup_buf_addr; */
66 return;
67 }
68
Felipe Balbi4dc64e52011-08-19 18:10:58 +030069 if (req->request.dma == DMA_ADDR_INVALID) {
70 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
71 req->request.length, req->direction
72 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 req->mapped = true;
74 } else {
75 dma_sync_single_for_device(dwc->dev, req->request.dma,
76 req->request.length, req->direction
77 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
78 req->mapped = false;
79 }
80}
81
82void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
83{
84 struct dwc3 *dwc = req->dep->dwc;
85
Sebastian Andrzej Siewior4ae8e1c2011-08-31 17:12:02 +020086 if (req->request.length == 0) {
87 req->request.dma = DMA_ADDR_INVALID;
88 return;
89 }
90
Felipe Balbi4dc64e52011-08-19 18:10:58 +030091 if (req->mapped) {
92 dma_unmap_single(dwc->dev, req->request.dma,
93 req->request.length, req->direction
94 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
95 req->mapped = 0;
Felipe Balbi162e1282011-08-27 15:10:09 +030096 req->request.dma = DMA_ADDR_INVALID;
Felipe Balbi4dc64e52011-08-19 18:10:58 +030097 } else {
98 dma_sync_single_for_cpu(dwc->dev, req->request.dma,
99 req->request.length, req->direction
100 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
101 }
102}
103
104void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
105 int status)
106{
107 struct dwc3 *dwc = dep->dwc;
108
109 if (req->queued) {
110 dep->busy_slot++;
111 /*
112 * Skip LINK TRB. We can't use req->trb and check for
113 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
114 * completed (not the LINK TRB).
115 */
116 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
117 usb_endpoint_xfer_isoc(dep->desc))
118 dep->busy_slot++;
119 }
120 list_del(&req->list);
121
122 if (req->request.status == -EINPROGRESS)
123 req->request.status = status;
124
125 dwc3_unmap_buffer_from_dma(req);
126
127 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
128 req, dep->name, req->request.actual,
129 req->request.length, status);
130
131 spin_unlock(&dwc->lock);
132 req->request.complete(&req->dep->endpoint, &req->request);
133 spin_lock(&dwc->lock);
134}
135
136static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
137{
138 switch (cmd) {
139 case DWC3_DEPCMD_DEPSTARTCFG:
140 return "Start New Configuration";
141 case DWC3_DEPCMD_ENDTRANSFER:
142 return "End Transfer";
143 case DWC3_DEPCMD_UPDATETRANSFER:
144 return "Update Transfer";
145 case DWC3_DEPCMD_STARTTRANSFER:
146 return "Start Transfer";
147 case DWC3_DEPCMD_CLEARSTALL:
148 return "Clear Stall";
149 case DWC3_DEPCMD_SETSTALL:
150 return "Set Stall";
151 case DWC3_DEPCMD_GETSEQNUMBER:
152 return "Get Data Sequence Number";
153 case DWC3_DEPCMD_SETTRANSFRESOURCE:
154 return "Set Endpoint Transfer Resource";
155 case DWC3_DEPCMD_SETEPCONFIG:
156 return "Set Endpoint Configuration";
157 default:
158 return "UNKNOWN command";
159 }
160}
161
162int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
163 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
164{
165 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +0200166 u32 timeout = 500;
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300167 u32 reg;
168
169 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
170 dep->name,
171 dwc3_gadget_ep_cmd_string(cmd), params->param0.raw,
172 params->param1.raw, params->param2.raw);
173
174 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0.raw);
175 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1.raw);
176 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2.raw);
177
178 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
179 do {
180 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
181 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbic7dbe4f2011-08-27 20:29:58 +0300182 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
183 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300184 return 0;
185 }
186
187 /*
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300188 * We can't sleep here, because it is also called from
189 * interrupt context.
190 */
191 timeout--;
192 if (!timeout)
193 return -ETIMEDOUT;
194
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +0200195 udelay(1);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300196 } while (1);
197}
198
199static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
200 struct dwc3_trb_hw *trb)
201{
202 u32 offset = trb - dep->trb_pool;
203
204 return dep->trb_pool_dma + offset;
205}
206
207static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
208{
209 struct dwc3 *dwc = dep->dwc;
210
211 if (dep->trb_pool)
212 return 0;
213
214 if (dep->number == 0 || dep->number == 1)
215 return 0;
216
217 dep->trb_pool = dma_alloc_coherent(dwc->dev,
218 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
219 &dep->trb_pool_dma, GFP_KERNEL);
220 if (!dep->trb_pool) {
221 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
222 dep->name);
223 return -ENOMEM;
224 }
225
226 return 0;
227}
228
229static void dwc3_free_trb_pool(struct dwc3_ep *dep)
230{
231 struct dwc3 *dwc = dep->dwc;
232
233 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
234 dep->trb_pool, dep->trb_pool_dma);
235
236 dep->trb_pool = NULL;
237 dep->trb_pool_dma = 0;
238}
239
240static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
241{
242 struct dwc3_gadget_ep_cmd_params params;
243 u32 cmd;
244
245 memset(&params, 0x00, sizeof(params));
246
247 if (dep->number != 1) {
248 cmd = DWC3_DEPCMD_DEPSTARTCFG;
249 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
250 if (dep->number > 1)
251 cmd |= DWC3_DEPCMD_PARAM(2);
252
253 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
254 }
255
256 return 0;
257}
258
259static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
260 const struct usb_endpoint_descriptor *desc)
261{
262 struct dwc3_gadget_ep_cmd_params params;
263
264 memset(&params, 0x00, sizeof(params));
265
266 params.param0.depcfg.ep_type = usb_endpoint_type(desc);
267 params.param0.depcfg.max_packet_size =
268 le16_to_cpu(desc->wMaxPacketSize);
269
270 params.param1.depcfg.xfer_complete_enable = true;
271 params.param1.depcfg.xfer_not_ready_enable = true;
272
273 if (usb_endpoint_xfer_isoc(desc))
274 params.param1.depcfg.xfer_in_progress_enable = true;
275
276 /*
277 * We are doing 1:1 mapping for endpoints, meaning
278 * Physical Endpoints 2 maps to Logical Endpoint 2 and
279 * so on. We consider the direction bit as part of the physical
280 * endpoint number. So USB endpoint 0x81 is 0x03.
281 */
282 params.param1.depcfg.ep_number = dep->number;
283
284 /*
285 * We must use the lower 16 TX FIFOs even though
286 * HW might have more
287 */
288 if (dep->direction)
289 params.param0.depcfg.fifo_number = dep->number >> 1;
290
291 if (desc->bInterval) {
292 params.param1.depcfg.binterval_m1 = desc->bInterval - 1;
293 dep->interval = 1 << (desc->bInterval - 1);
294 }
295
296 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
297 DWC3_DEPCMD_SETEPCONFIG, &params);
298}
299
300static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
301{
302 struct dwc3_gadget_ep_cmd_params params;
303
304 memset(&params, 0x00, sizeof(params));
305
306 params.param0.depxfercfg.number_xfer_resources = 1;
307
308 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
309 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
310}
311
312/**
313 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
314 * @dep: endpoint to be initialized
315 * @desc: USB Endpoint Descriptor
316 *
317 * Caller should take care of locking
318 */
319static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
320 const struct usb_endpoint_descriptor *desc)
321{
322 struct dwc3 *dwc = dep->dwc;
323 u32 reg;
324 int ret = -ENOMEM;
325
326 if (!(dep->flags & DWC3_EP_ENABLED)) {
327 ret = dwc3_gadget_start_config(dwc, dep);
328 if (ret)
329 return ret;
330 }
331
332 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
333 if (ret)
334 return ret;
335
336 if (!(dep->flags & DWC3_EP_ENABLED)) {
337 struct dwc3_trb_hw *trb_st_hw;
338 struct dwc3_trb_hw *trb_link_hw;
339 struct dwc3_trb trb_link;
340
341 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
342 if (ret)
343 return ret;
344
345 dep->desc = desc;
346 dep->type = usb_endpoint_type(desc);
347 dep->flags |= DWC3_EP_ENABLED;
348
349 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
350 reg |= DWC3_DALEPENA_EP(dep->number);
351 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
352
353 if (!usb_endpoint_xfer_isoc(desc))
354 return 0;
355
356 memset(&trb_link, 0, sizeof(trb_link));
357
358 /* Link TRB for ISOC. The HWO but is never reset */
359 trb_st_hw = &dep->trb_pool[0];
360
361 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
362 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
363 trb_link.hwo = true;
364
365 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
366 dwc3_trb_to_hw(&trb_link, trb_link_hw);
367 }
368
369 return 0;
370}
371
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200372static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
373static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300374{
375 struct dwc3_request *req;
376
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200377 if (!list_empty(&dep->req_queued))
378 dwc3_stop_active_transfer(dwc, dep->number);
379
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300380 while (!list_empty(&dep->request_list)) {
381 req = next_request(&dep->request_list);
382
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200383 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300384 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300385}
386
387/**
388 * __dwc3_gadget_ep_disable - Disables a HW endpoint
389 * @dep: the endpoint to disable
390 *
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200391 * This function also removes requests which are currently processed ny the
392 * hardware and those which are not yet scheduled.
393 * Caller should take care of locking.
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300394 */
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300395static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
396{
397 struct dwc3 *dwc = dep->dwc;
398 u32 reg;
399
400 dep->flags &= ~DWC3_EP_ENABLED;
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200401 dwc3_remove_requests(dwc, dep);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300402
403 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
404 reg &= ~DWC3_DALEPENA_EP(dep->number);
405 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
406
407 dep->desc = NULL;
408 dep->type = 0;
409
410 return 0;
411}
412
413/* -------------------------------------------------------------------------- */
414
415static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
416 const struct usb_endpoint_descriptor *desc)
417{
418 return -EINVAL;
419}
420
421static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
422{
423 return -EINVAL;
424}
425
426/* -------------------------------------------------------------------------- */
427
428static int dwc3_gadget_ep_enable(struct usb_ep *ep,
429 const struct usb_endpoint_descriptor *desc)
430{
431 struct dwc3_ep *dep;
432 struct dwc3 *dwc;
433 unsigned long flags;
434 int ret;
435
436 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
437 pr_debug("dwc3: invalid parameters\n");
438 return -EINVAL;
439 }
440
441 if (!desc->wMaxPacketSize) {
442 pr_debug("dwc3: missing wMaxPacketSize\n");
443 return -EINVAL;
444 }
445
446 dep = to_dwc3_ep(ep);
447 dwc = dep->dwc;
448
449 switch (usb_endpoint_type(desc)) {
450 case USB_ENDPOINT_XFER_CONTROL:
451 strncat(dep->name, "-control", sizeof(dep->name));
452 break;
453 case USB_ENDPOINT_XFER_ISOC:
454 strncat(dep->name, "-isoc", sizeof(dep->name));
455 break;
456 case USB_ENDPOINT_XFER_BULK:
457 strncat(dep->name, "-bulk", sizeof(dep->name));
458 break;
459 case USB_ENDPOINT_XFER_INT:
460 strncat(dep->name, "-int", sizeof(dep->name));
461 break;
462 default:
463 dev_err(dwc->dev, "invalid endpoint transfer type\n");
464 }
465
466 if (dep->flags & DWC3_EP_ENABLED) {
467 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
468 dep->name);
469 return 0;
470 }
471
472 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
473
474 spin_lock_irqsave(&dwc->lock, flags);
475 ret = __dwc3_gadget_ep_enable(dep, desc);
476 spin_unlock_irqrestore(&dwc->lock, flags);
477
478 return ret;
479}
480
481static int dwc3_gadget_ep_disable(struct usb_ep *ep)
482{
483 struct dwc3_ep *dep;
484 struct dwc3 *dwc;
485 unsigned long flags;
486 int ret;
487
488 if (!ep) {
489 pr_debug("dwc3: invalid parameters\n");
490 return -EINVAL;
491 }
492
493 dep = to_dwc3_ep(ep);
494 dwc = dep->dwc;
495
496 if (!(dep->flags & DWC3_EP_ENABLED)) {
497 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
498 dep->name);
499 return 0;
500 }
501
502 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
503 dep->number >> 1,
504 (dep->number & 1) ? "in" : "out");
505
506 spin_lock_irqsave(&dwc->lock, flags);
507 ret = __dwc3_gadget_ep_disable(dep);
508 spin_unlock_irqrestore(&dwc->lock, flags);
509
510 return ret;
511}
512
513static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
514 gfp_t gfp_flags)
515{
516 struct dwc3_request *req;
517 struct dwc3_ep *dep = to_dwc3_ep(ep);
518 struct dwc3 *dwc = dep->dwc;
519
520 req = kzalloc(sizeof(*req), gfp_flags);
521 if (!req) {
522 dev_err(dwc->dev, "not enough memory\n");
523 return NULL;
524 }
525
526 req->epnum = dep->number;
527 req->dep = dep;
528 req->request.dma = DMA_ADDR_INVALID;
529
530 return &req->request;
531}
532
533static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
534 struct usb_request *request)
535{
536 struct dwc3_request *req = to_dwc3_request(request);
537
538 kfree(req);
539}
540
541/*
542 * dwc3_prepare_trbs - setup TRBs from requests
543 * @dep: endpoint for which requests are being prepared
544 * @starting: true if the endpoint is idle and no requests are queued.
545 *
546 * The functions goes through the requests list and setups TRBs for the
547 * transfers. The functions returns once there are not more TRBs available or
548 * it run out of requests.
549 */
550static struct dwc3_request *dwc3_prepare_trbs(struct dwc3_ep *dep,
551 bool starting)
552{
553 struct dwc3_request *req, *n, *ret = NULL;
554 struct dwc3_trb_hw *trb_hw;
555 struct dwc3_trb trb;
556 u32 trbs_left;
557
558 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
559
560 /* the first request must not be queued */
561 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
562 /*
563 * if busy & slot are equal than it is either full or empty. If we are
564 * starting to proceed requests then we are empty. Otherwise we ar
565 * full and don't do anything
566 */
567 if (!trbs_left) {
568 if (!starting)
569 return NULL;
570 trbs_left = DWC3_TRB_NUM;
571 /*
572 * In case we start from scratch, we queue the ISOC requests
573 * starting from slot 1. This is done because we use ring
574 * buffer and have no LST bit to stop us. Instead, we place
575 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
576 * after the first request so we start at slot 1 and have
577 * 7 requests proceed before we hit the first IOC.
578 * Other transfer types don't use the ring buffer and are
579 * processed from the first TRB until the last one. Since we
580 * don't wrap around we have to start at the beginning.
581 */
582 if (usb_endpoint_xfer_isoc(dep->desc)) {
583 dep->busy_slot = 1;
584 dep->free_slot = 1;
585 } else {
586 dep->busy_slot = 0;
587 dep->free_slot = 0;
588 }
589 }
590
591 /* The last TRB is a link TRB, not used for xfer */
592 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
593 return NULL;
594
595 list_for_each_entry_safe(req, n, &dep->request_list, list) {
596 unsigned int last_one = 0;
597 unsigned int cur_slot;
598
599 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
600 cur_slot = dep->free_slot;
601 dep->free_slot++;
602
603 /* Skip the LINK-TRB on ISOC */
604 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
605 usb_endpoint_xfer_isoc(dep->desc))
606 continue;
607
608 dwc3_gadget_move_request_queued(req);
609 memset(&trb, 0, sizeof(trb));
610 trbs_left--;
611
612 /* Is our TRB pool empty? */
613 if (!trbs_left)
614 last_one = 1;
615 /* Is this the last request? */
616 if (list_empty(&dep->request_list))
617 last_one = 1;
618
619 /*
620 * FIXME we shouldn't need to set LST bit always but we are
621 * facing some weird problem with the Hardware where it doesn't
622 * complete even though it has been previously started.
623 *
624 * While we're debugging the problem, as a workaround to
625 * multiple TRBs handling, use only one TRB at a time.
626 */
627 last_one = 1;
628
629 req->trb = trb_hw;
630 if (!ret)
631 ret = req;
632
633 trb.bplh = req->request.dma;
634
635 if (usb_endpoint_xfer_isoc(dep->desc)) {
636 trb.isp_imi = true;
637 trb.csp = true;
638 } else {
639 trb.lst = last_one;
640 }
641
642 switch (usb_endpoint_type(dep->desc)) {
643 case USB_ENDPOINT_XFER_CONTROL:
644 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
645 break;
646
647 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior15623d72011-08-22 17:42:19 +0200648 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300649
650 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
651 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
652 trb.ioc = last_one;
653 break;
654
655 case USB_ENDPOINT_XFER_BULK:
656 case USB_ENDPOINT_XFER_INT:
657 trb.trbctl = DWC3_TRBCTL_NORMAL;
658 break;
659 default:
660 /*
661 * This is only possible with faulty memory because we
662 * checked it already :)
663 */
664 BUG();
665 }
666
667 trb.length = req->request.length;
668 trb.hwo = true;
669
670 dwc3_trb_to_hw(&trb, trb_hw);
671 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
672
673 if (last_one)
674 break;
675 }
676
677 return ret;
678}
679
680static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
681 int start_new)
682{
683 struct dwc3_gadget_ep_cmd_params params;
684 struct dwc3_request *req;
685 struct dwc3 *dwc = dep->dwc;
686 int ret;
687 u32 cmd;
688
689 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
690 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
691 return -EBUSY;
692 }
693 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
694
695 /*
696 * If we are getting here after a short-out-packet we don't enqueue any
697 * new requests as we try to set the IOC bit only on the last request.
698 */
699 if (start_new) {
700 if (list_empty(&dep->req_queued))
701 dwc3_prepare_trbs(dep, start_new);
702
703 /* req points to the first request which will be sent */
704 req = next_request(&dep->req_queued);
705 } else {
706 /*
707 * req points to the first request where HWO changed
708 * from 0 to 1
709 */
710 req = dwc3_prepare_trbs(dep, start_new);
711 }
712 if (!req) {
713 dep->flags |= DWC3_EP_PENDING_REQUEST;
714 return 0;
715 }
716
717 memset(&params, 0, sizeof(params));
718 params.param0.depstrtxfer.transfer_desc_addr_high =
719 upper_32_bits(req->trb_dma);
720 params.param1.depstrtxfer.transfer_desc_addr_low =
721 lower_32_bits(req->trb_dma);
722
723 if (start_new)
724 cmd = DWC3_DEPCMD_STARTTRANSFER;
725 else
726 cmd = DWC3_DEPCMD_UPDATETRANSFER;
727
728 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
729 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
730 if (ret < 0) {
731 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
732
733 /*
734 * FIXME we need to iterate over the list of requests
735 * here and stop, unmap, free and del each of the linked
736 * requests instead of we do now.
737 */
738 dwc3_unmap_buffer_from_dma(req);
739 list_del(&req->list);
740 return ret;
741 }
742
743 dep->flags |= DWC3_EP_BUSY;
744 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
745 dep->number);
746 if (!dep->res_trans_idx)
747 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
748 return 0;
749}
750
751static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
752{
753 req->request.actual = 0;
754 req->request.status = -EINPROGRESS;
755 req->direction = dep->direction;
756 req->epnum = dep->number;
757
758 /*
759 * We only add to our list of requests now and
760 * start consuming the list once we get XferNotReady
761 * IRQ.
762 *
763 * That way, we avoid doing anything that we don't need
764 * to do now and defer it until the point we receive a
765 * particular token from the Host side.
766 *
767 * This will also avoid Host cancelling URBs due to too
768 * many NACKs.
769 */
770 dwc3_map_buffer_to_dma(req);
771 list_add_tail(&req->list, &dep->request_list);
772
773 /*
774 * There is one special case: XferNotReady with
775 * empty list of requests. We need to kick the
776 * transfer here in that situation, otherwise
777 * we will be NAKing forever.
778 *
779 * If we get XferNotReady before gadget driver
780 * has a chance to queue a request, we will ACK
781 * the IRQ but won't be able to receive the data
782 * until the next request is queued. The following
783 * code is handling exactly that.
784 */
785 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
786 int ret;
787 int start_trans;
788
789 start_trans = 1;
790 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
791 dep->flags & DWC3_EP_BUSY)
792 start_trans = 0;
793
794 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
795 if (ret && ret != -EBUSY) {
796 struct dwc3 *dwc = dep->dwc;
797
798 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
799 dep->name);
800 }
801 };
802
803 return 0;
804}
805
806static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
807 gfp_t gfp_flags)
808{
809 struct dwc3_request *req = to_dwc3_request(request);
810 struct dwc3_ep *dep = to_dwc3_ep(ep);
811 struct dwc3 *dwc = dep->dwc;
812
813 unsigned long flags;
814
815 int ret;
816
817 if (!dep->desc) {
818 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
819 request, ep->name);
820 return -ESHUTDOWN;
821 }
822
823 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
824 request, ep->name, request->length);
825
826 spin_lock_irqsave(&dwc->lock, flags);
827 ret = __dwc3_gadget_ep_queue(dep, req);
828 spin_unlock_irqrestore(&dwc->lock, flags);
829
830 return ret;
831}
832
833static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
834 struct usb_request *request)
835{
836 struct dwc3_request *req = to_dwc3_request(request);
837 struct dwc3_request *r = NULL;
838
839 struct dwc3_ep *dep = to_dwc3_ep(ep);
840 struct dwc3 *dwc = dep->dwc;
841
842 unsigned long flags;
843 int ret = 0;
844
845 spin_lock_irqsave(&dwc->lock, flags);
846
847 list_for_each_entry(r, &dep->request_list, list) {
848 if (r == req)
849 break;
850 }
851
852 if (r != req) {
853 list_for_each_entry(r, &dep->req_queued, list) {
854 if (r == req)
855 break;
856 }
857 if (r == req) {
858 /* wait until it is processed */
859 dwc3_stop_active_transfer(dwc, dep->number);
860 goto out0;
861 }
862 dev_err(dwc->dev, "request %p was not queued to %s\n",
863 request, ep->name);
864 ret = -EINVAL;
865 goto out0;
866 }
867
868 /* giveback the request */
869 dwc3_gadget_giveback(dep, req, -ECONNRESET);
870
871out0:
872 spin_unlock_irqrestore(&dwc->lock, flags);
873
874 return ret;
875}
876
877int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
878{
879 struct dwc3_gadget_ep_cmd_params params;
880 struct dwc3 *dwc = dep->dwc;
881 int ret;
882
883 memset(&params, 0x00, sizeof(params));
884
885 if (value) {
Felipe Balbiaa7b4d02011-08-30 15:48:08 +0300886 if (dep->number == 0 || dep->number == 1) {
887 /*
888 * Whenever EP0 is stalled, we will restart
889 * the state machine, thus moving back to
890 * Setup Phase
891 */
892 dwc->ep0state = EP0_SETUP_PHASE;
893 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300894
895 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
896 DWC3_DEPCMD_SETSTALL, &params);
897 if (ret)
898 dev_err(dwc->dev, "failed to %s STALL on %s\n",
899 value ? "set" : "clear",
900 dep->name);
901 else
902 dep->flags |= DWC3_EP_STALL;
903 } else {
904 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
905 DWC3_DEPCMD_CLEARSTALL, &params);
906 if (ret)
907 dev_err(dwc->dev, "failed to %s STALL on %s\n",
908 value ? "set" : "clear",
909 dep->name);
910 else
911 dep->flags &= ~DWC3_EP_STALL;
912 }
913 return ret;
914}
915
916static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
917{
918 struct dwc3_ep *dep = to_dwc3_ep(ep);
919 struct dwc3 *dwc = dep->dwc;
920
921 unsigned long flags;
922
923 int ret;
924
925 spin_lock_irqsave(&dwc->lock, flags);
926
927 if (usb_endpoint_xfer_isoc(dep->desc)) {
928 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
929 ret = -EINVAL;
930 goto out;
931 }
932
933 ret = __dwc3_gadget_ep_set_halt(dep, value);
934out:
935 spin_unlock_irqrestore(&dwc->lock, flags);
936
937 return ret;
938}
939
940static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
941{
942 struct dwc3_ep *dep = to_dwc3_ep(ep);
943
944 dep->flags |= DWC3_EP_WEDGE;
945
946 return usb_ep_set_halt(ep);
947}
948
949/* -------------------------------------------------------------------------- */
950
951static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
952 .bLength = USB_DT_ENDPOINT_SIZE,
953 .bDescriptorType = USB_DT_ENDPOINT,
954 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
955};
956
957static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
958 .enable = dwc3_gadget_ep0_enable,
959 .disable = dwc3_gadget_ep0_disable,
960 .alloc_request = dwc3_gadget_ep_alloc_request,
961 .free_request = dwc3_gadget_ep_free_request,
962 .queue = dwc3_gadget_ep0_queue,
963 .dequeue = dwc3_gadget_ep_dequeue,
964 .set_halt = dwc3_gadget_ep_set_halt,
965 .set_wedge = dwc3_gadget_ep_set_wedge,
966};
967
968static const struct usb_ep_ops dwc3_gadget_ep_ops = {
969 .enable = dwc3_gadget_ep_enable,
970 .disable = dwc3_gadget_ep_disable,
971 .alloc_request = dwc3_gadget_ep_alloc_request,
972 .free_request = dwc3_gadget_ep_free_request,
973 .queue = dwc3_gadget_ep_queue,
974 .dequeue = dwc3_gadget_ep_dequeue,
975 .set_halt = dwc3_gadget_ep_set_halt,
976 .set_wedge = dwc3_gadget_ep_set_wedge,
977};
978
979/* -------------------------------------------------------------------------- */
980
981static int dwc3_gadget_get_frame(struct usb_gadget *g)
982{
983 struct dwc3 *dwc = gadget_to_dwc(g);
984 u32 reg;
985
986 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
987 return DWC3_DSTS_SOFFN(reg);
988}
989
990static int dwc3_gadget_wakeup(struct usb_gadget *g)
991{
992 struct dwc3 *dwc = gadget_to_dwc(g);
993
994 unsigned long timeout;
995 unsigned long flags;
996
997 u32 reg;
998
999 int ret = 0;
1000
1001 u8 link_state;
1002 u8 speed;
1003
1004 spin_lock_irqsave(&dwc->lock, flags);
1005
1006 /*
1007 * According to the Databook Remote wakeup request should
1008 * be issued only when the device is in early suspend state.
1009 *
1010 * We can check that via USB Link State bits in DSTS register.
1011 */
1012 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1013
1014 speed = reg & DWC3_DSTS_CONNECTSPD;
1015 if (speed == DWC3_DSTS_SUPERSPEED) {
1016 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1017 ret = -EINVAL;
1018 goto out;
1019 }
1020
1021 link_state = DWC3_DSTS_USBLNKST(reg);
1022
1023 switch (link_state) {
1024 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1025 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1026 break;
1027 default:
1028 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1029 link_state);
1030 ret = -EINVAL;
1031 goto out;
1032 }
1033
1034 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1035
1036 /*
1037 * Switch link state to Recovery. In HS/FS/LS this means
1038 * RemoteWakeup Request
1039 */
1040 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1041 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1042
1043 /* wait for at least 2000us */
1044 usleep_range(2000, 2500);
1045
1046 /* write zeroes to Link Change Request */
1047 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1048 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1049
1050 /* pool until Link State change to ON */
1051 timeout = jiffies + msecs_to_jiffies(100);
1052
1053 while (!(time_after(jiffies, timeout))) {
1054 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1055
1056 /* in HS, means ON */
1057 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1058 break;
1059 }
1060
1061 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1062 dev_err(dwc->dev, "failed to send remote wakeup\n");
1063 ret = -EINVAL;
1064 }
1065
1066out:
1067 spin_unlock_irqrestore(&dwc->lock, flags);
1068
1069 return ret;
1070}
1071
1072static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1073 int is_selfpowered)
1074{
1075 struct dwc3 *dwc = gadget_to_dwc(g);
1076
1077 dwc->is_selfpowered = !!is_selfpowered;
1078
1079 return 0;
1080}
1081
1082static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1083{
1084 u32 reg;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001085 u32 timeout = 500;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001086
1087 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1088 if (is_on)
1089 reg |= DWC3_DCTL_RUN_STOP;
1090 else
1091 reg &= ~DWC3_DCTL_RUN_STOP;
1092
1093 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1094
1095 do {
1096 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1097 if (is_on) {
1098 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1099 break;
1100 } else {
1101 if (reg & DWC3_DSTS_DEVCTRLHLT)
1102 break;
1103 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001104 timeout--;
1105 if (!timeout)
1106 break;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001107 udelay(1);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001108 } while (1);
1109
1110 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1111 dwc->gadget_driver
1112 ? dwc->gadget_driver->function : "no-function",
1113 is_on ? "connect" : "disconnect");
1114}
1115
1116static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1117{
1118 struct dwc3 *dwc = gadget_to_dwc(g);
1119 unsigned long flags;
1120
1121 is_on = !!is_on;
1122
1123 spin_lock_irqsave(&dwc->lock, flags);
1124 dwc3_gadget_run_stop(dwc, is_on);
1125 spin_unlock_irqrestore(&dwc->lock, flags);
1126
1127 return 0;
1128}
1129
1130static int dwc3_gadget_start(struct usb_gadget *g,
1131 struct usb_gadget_driver *driver)
1132{
1133 struct dwc3 *dwc = gadget_to_dwc(g);
1134 struct dwc3_ep *dep;
1135 unsigned long flags;
1136 int ret = 0;
1137 u32 reg;
1138
1139 spin_lock_irqsave(&dwc->lock, flags);
1140
1141 if (dwc->gadget_driver) {
1142 dev_err(dwc->dev, "%s is already bound to %s\n",
1143 dwc->gadget.name,
1144 dwc->gadget_driver->driver.name);
1145 ret = -EBUSY;
1146 goto err0;
1147 }
1148
1149 dwc->gadget_driver = driver;
1150 dwc->gadget.dev.driver = &driver->driver;
1151
1152 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1153
Felipe Balbid195b322011-09-08 17:42:11 +03001154 reg &= ~DWC3_GCTL_SCALEDOWN(3);
1155 reg &= ~DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001156 reg &= ~DWC3_GCTL_DISSCRAMBLE;
Felipe Balbid195b322011-09-08 17:42:11 +03001157 reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001158
1159 /*
1160 * WORKAROUND: DWC3 revisions <1.90a have a bug
1161 * when The device fails to connect at SuperSpeed
1162 * and falls back to high-speed mode which causes
1163 * the device to enter in a Connect/Disconnect loop
1164 */
1165 if (dwc->revision < DWC3_REVISION_190A)
1166 reg |= DWC3_GCTL_U2RSTECN;
1167
1168 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1169
1170 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1171 reg &= ~(DWC3_DCFG_SPEED_MASK);
1172 reg |= DWC3_DCFG_SUPERSPEED;
1173 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1174
1175 /* Start with SuperSpeed Default */
1176 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1177
1178 dep = dwc->eps[0];
1179 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1180 if (ret) {
1181 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1182 goto err0;
1183 }
1184
1185 dep = dwc->eps[1];
1186 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1187 if (ret) {
1188 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1189 goto err1;
1190 }
1191
1192 /* begin to receive SETUP packets */
Felipe Balbi32e132e2011-08-27 22:28:36 +03001193 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001194 dwc3_ep0_out_start(dwc);
1195
1196 spin_unlock_irqrestore(&dwc->lock, flags);
1197
1198 return 0;
1199
1200err1:
1201 __dwc3_gadget_ep_disable(dwc->eps[0]);
1202
1203err0:
1204 spin_unlock_irqrestore(&dwc->lock, flags);
1205
1206 return ret;
1207}
1208
1209static int dwc3_gadget_stop(struct usb_gadget *g,
1210 struct usb_gadget_driver *driver)
1211{
1212 struct dwc3 *dwc = gadget_to_dwc(g);
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&dwc->lock, flags);
1216
1217 __dwc3_gadget_ep_disable(dwc->eps[0]);
1218 __dwc3_gadget_ep_disable(dwc->eps[1]);
1219
1220 dwc->gadget_driver = NULL;
1221 dwc->gadget.dev.driver = NULL;
1222
1223 spin_unlock_irqrestore(&dwc->lock, flags);
1224
1225 return 0;
1226}
1227static const struct usb_gadget_ops dwc3_gadget_ops = {
1228 .get_frame = dwc3_gadget_get_frame,
1229 .wakeup = dwc3_gadget_wakeup,
1230 .set_selfpowered = dwc3_gadget_set_selfpowered,
1231 .pullup = dwc3_gadget_pullup,
1232 .udc_start = dwc3_gadget_start,
1233 .udc_stop = dwc3_gadget_stop,
1234};
1235
1236/* -------------------------------------------------------------------------- */
1237
1238static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1239{
1240 struct dwc3_ep *dep;
1241 u8 epnum;
1242
1243 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1244
1245 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1246 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1247 if (!dep) {
1248 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1249 epnum);
1250 return -ENOMEM;
1251 }
1252
1253 dep->dwc = dwc;
1254 dep->number = epnum;
1255 dwc->eps[epnum] = dep;
1256
1257 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1258 (epnum & 1) ? "in" : "out");
1259 dep->endpoint.name = dep->name;
1260 dep->direction = (epnum & 1);
1261
1262 if (epnum == 0 || epnum == 1) {
1263 dep->endpoint.maxpacket = 512;
1264 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1265 if (!epnum)
1266 dwc->gadget.ep0 = &dep->endpoint;
1267 } else {
1268 int ret;
1269
1270 dep->endpoint.maxpacket = 1024;
1271 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1272 list_add_tail(&dep->endpoint.ep_list,
1273 &dwc->gadget.ep_list);
1274
1275 ret = dwc3_alloc_trb_pool(dep);
1276 if (ret) {
1277 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1278 return ret;
1279 }
1280 }
1281 INIT_LIST_HEAD(&dep->request_list);
1282 INIT_LIST_HEAD(&dep->req_queued);
1283 }
1284
1285 return 0;
1286}
1287
1288static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1289{
1290 struct dwc3_ep *dep;
1291 u8 epnum;
1292
1293 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1294 dep = dwc->eps[epnum];
1295 dwc3_free_trb_pool(dep);
1296
1297 if (epnum != 0 && epnum != 1)
1298 list_del(&dep->endpoint.ep_list);
1299
1300 kfree(dep);
1301 }
1302}
1303
1304static void dwc3_gadget_release(struct device *dev)
1305{
1306 dev_dbg(dev, "%s\n", __func__);
1307}
1308
1309/* -------------------------------------------------------------------------- */
1310static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1311 const struct dwc3_event_depevt *event, int status)
1312{
1313 struct dwc3_request *req;
1314 struct dwc3_trb trb;
1315 unsigned int count;
1316 unsigned int s_pkt = 0;
1317
1318 do {
1319 req = next_request(&dep->req_queued);
1320 if (!req)
1321 break;
1322
1323 dwc3_trb_to_nat(req->trb, &trb);
1324
Sebastian Andrzej Siewior679dc462011-08-19 19:59:12 +02001325 if (trb.hwo && status != -ESHUTDOWN)
1326 /*
1327 * We continue despite the error. There is not much we
1328 * can do. If we don't clean in up we loop for ever. If
1329 * we skip the TRB than it gets overwritten reused after
1330 * a while since we use them in a ring buffer. a BUG()
1331 * would help. Lets hope that if this occures, someone
1332 * fixes the root cause instead of looking away :)
1333 */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001334 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1335 dep->name, req->trb);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001336 count = trb.length;
1337
1338 if (dep->direction) {
1339 if (count) {
1340 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1341 dep->name);
1342 status = -ECONNRESET;
1343 }
1344 } else {
1345 if (count && (event->status & DEPEVT_STATUS_SHORT))
1346 s_pkt = 1;
1347 }
1348
1349 /*
1350 * We assume here we will always receive the entire data block
1351 * which we should receive. Meaning, if we program RX to
1352 * receive 4K but we receive only 2K, we assume that's all we
1353 * should receive and we simply bounce the request back to the
1354 * gadget driver for further processing.
1355 */
1356 req->request.actual += req->request.length - count;
1357 dwc3_gadget_giveback(dep, req, status);
1358 if (s_pkt)
1359 break;
1360 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1361 break;
1362 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1363 break;
1364 } while (1);
1365
1366 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1367 return 0;
1368 return 1;
1369}
1370
1371static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1372 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1373 int start_new)
1374{
1375 unsigned status = 0;
1376 int clean_busy;
1377
1378 if (event->status & DEPEVT_STATUS_BUSERR)
1379 status = -ECONNRESET;
1380
1381 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001382 if (clean_busy) {
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001383 dep->flags &= ~DWC3_EP_BUSY;
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001384 dep->res_trans_idx = 0;
1385 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001386}
1387
1388static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1389 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1390{
1391 u32 uf;
1392
1393 if (list_empty(&dep->request_list)) {
1394 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1395 dep->name);
1396 return;
1397 }
1398
1399 if (event->parameters) {
1400 u32 mask;
1401
1402 mask = ~(dep->interval - 1);
1403 uf = event->parameters & mask;
1404 /* 4 micro frames in the future */
1405 uf += dep->interval * 4;
1406 } else {
1407 uf = 0;
1408 }
1409
1410 __dwc3_gadget_kick_transfer(dep, uf, 1);
1411}
1412
1413static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1414 const struct dwc3_event_depevt *event)
1415{
1416 struct dwc3 *dwc = dep->dwc;
1417 struct dwc3_event_depevt mod_ev = *event;
1418
1419 /*
1420 * We were asked to remove one requests. It is possible that this
1421 * request and a few other were started together and have the same
1422 * transfer index. Since we stopped the complete endpoint we don't
1423 * know how many requests were already completed (and not yet)
1424 * reported and how could be done (later). We purge them all until
1425 * the end of the list.
1426 */
1427 mod_ev.status = DEPEVT_STATUS_LST;
1428 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1429 dep->flags &= ~DWC3_EP_BUSY;
1430 /* pending requets are ignored and are queued on XferNotReady */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001431}
1432
1433static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1434 const struct dwc3_event_depevt *event)
1435{
1436 u32 param = event->parameters;
1437 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1438
1439 switch (cmd_type) {
1440 case DWC3_DEPCMD_ENDTRANSFER:
1441 dwc3_process_ep_cmd_complete(dep, event);
1442 break;
1443 case DWC3_DEPCMD_STARTTRANSFER:
1444 dep->res_trans_idx = param & 0x7f;
1445 break;
1446 default:
1447 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1448 __func__, cmd_type);
1449 break;
1450 };
1451}
1452
1453static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1454 const struct dwc3_event_depevt *event)
1455{
1456 struct dwc3_ep *dep;
1457 u8 epnum = event->endpoint_number;
1458
1459 dep = dwc->eps[epnum];
1460
1461 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1462 dwc3_ep_event_string(event->endpoint_event));
1463
1464 if (epnum == 0 || epnum == 1) {
1465 dwc3_ep0_interrupt(dwc, event);
1466 return;
1467 }
1468
1469 switch (event->endpoint_event) {
1470 case DWC3_DEPEVT_XFERCOMPLETE:
1471 if (usb_endpoint_xfer_isoc(dep->desc)) {
1472 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1473 dep->name);
1474 return;
1475 }
1476
1477 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1478 break;
1479 case DWC3_DEPEVT_XFERINPROGRESS:
1480 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1481 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1482 dep->name);
1483 return;
1484 }
1485
1486 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1487 break;
1488 case DWC3_DEPEVT_XFERNOTREADY:
1489 if (usb_endpoint_xfer_isoc(dep->desc)) {
1490 dwc3_gadget_start_isoc(dwc, dep, event);
1491 } else {
1492 int ret;
1493
1494 dev_vdbg(dwc->dev, "%s: reason %s\n",
1495 dep->name, event->status
1496 ? "Transfer Active"
1497 : "Transfer Not Active");
1498
1499 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1500 if (!ret || ret == -EBUSY)
1501 return;
1502
1503 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1504 dep->name);
1505 }
1506
1507 break;
1508 case DWC3_DEPEVT_RXTXFIFOEVT:
1509 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1510 break;
1511 case DWC3_DEPEVT_STREAMEVT:
1512 dev_dbg(dwc->dev, "%s Stream Event\n", dep->name);
1513 break;
1514 case DWC3_DEPEVT_EPCMDCMPLT:
1515 dwc3_ep_cmd_compl(dep, event);
1516 break;
1517 }
1518}
1519
1520static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1521{
1522 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1523 spin_unlock(&dwc->lock);
1524 dwc->gadget_driver->disconnect(&dwc->gadget);
1525 spin_lock(&dwc->lock);
1526 }
1527}
1528
1529static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1530{
1531 struct dwc3_ep *dep;
1532 struct dwc3_gadget_ep_cmd_params params;
1533 u32 cmd;
1534 int ret;
1535
1536 dep = dwc->eps[epnum];
1537
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001538 WARN_ON(!dep->res_trans_idx);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001539 if (dep->res_trans_idx) {
1540 cmd = DWC3_DEPCMD_ENDTRANSFER;
1541 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1542 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1543 memset(&params, 0, sizeof(params));
1544 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1545 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001546 dep->res_trans_idx = 0;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001547 }
1548}
1549
1550static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1551{
1552 u32 epnum;
1553
1554 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1555 struct dwc3_ep *dep;
1556
1557 dep = dwc->eps[epnum];
1558 if (!(dep->flags & DWC3_EP_ENABLED))
1559 continue;
1560
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001561 dwc3_remove_requests(dwc, dep);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001562 }
1563}
1564
1565static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1566{
1567 u32 epnum;
1568
1569 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1570 struct dwc3_ep *dep;
1571 struct dwc3_gadget_ep_cmd_params params;
1572 int ret;
1573
1574 dep = dwc->eps[epnum];
1575
1576 if (!(dep->flags & DWC3_EP_STALL))
1577 continue;
1578
1579 dep->flags &= ~DWC3_EP_STALL;
1580
1581 memset(&params, 0, sizeof(params));
1582 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1583 DWC3_DEPCMD_CLEARSTALL, &params);
1584 WARN_ON_ONCE(ret);
1585 }
1586}
1587
1588static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1589{
1590 dev_vdbg(dwc->dev, "%s\n", __func__);
1591#if 0
1592 XXX
1593 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1594 enable it before we can disable it.
1595
1596 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1597 reg &= ~DWC3_DCTL_INITU1ENA;
1598 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1599
1600 reg &= ~DWC3_DCTL_INITU2ENA;
1601 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1602#endif
1603
1604 dwc3_stop_active_transfers(dwc);
1605 dwc3_disconnect_gadget(dwc);
1606
1607 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1608}
1609
1610static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1611{
1612 u32 reg;
1613
1614 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1615
1616 if (on)
1617 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1618 else
1619 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1620
1621 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1622}
1623
1624static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1625{
1626 u32 reg;
1627
1628 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1629
1630 if (on)
1631 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1632 else
1633 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1634
1635 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1636}
1637
1638static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1639{
1640 u32 reg;
1641
1642 dev_vdbg(dwc->dev, "%s\n", __func__);
1643
1644 /* Enable PHYs */
1645 dwc3_gadget_usb2_phy_power(dwc, true);
1646 dwc3_gadget_usb3_phy_power(dwc, true);
1647
1648 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1649 dwc3_disconnect_gadget(dwc);
1650
1651 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1652 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1654
1655 dwc3_stop_active_transfers(dwc);
1656 dwc3_clear_stall_all_ep(dwc);
1657
1658 /* Reset device address to zero */
1659 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1660 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1661 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1662
1663 /*
1664 * Wait for RxFifo to drain
1665 *
1666 * REVISIT probably shouldn't wait forever.
1667 * In case Hardware ends up in a screwed up
1668 * case, we error out, notify the user and,
1669 * maybe, WARN() or BUG() but leave the rest
1670 * of the kernel working fine.
1671 *
1672 * REVISIT the below is rather CPU intensive,
1673 * maybe we should read and if it doesn't work
1674 * sleep (not busy wait) for a few useconds.
1675 *
1676 * REVISIT why wait until the RXFIFO is empty anyway?
1677 */
1678 while (!(dwc3_readl(dwc->regs, DWC3_DSTS)
1679 & DWC3_DSTS_RXFIFOEMPTY))
1680 cpu_relax();
1681}
1682
1683static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1684{
1685 u32 reg;
1686 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1687
1688 /*
1689 * We change the clock only at SS but I dunno why I would want to do
1690 * this. Maybe it becomes part of the power saving plan.
1691 */
1692
1693 if (speed != DWC3_DSTS_SUPERSPEED)
1694 return;
1695
1696 /*
1697 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1698 * each time on Connect Done.
1699 */
1700 if (!usb30_clock)
1701 return;
1702
1703 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1704 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1705 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1706}
1707
1708static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1709{
1710 switch (speed) {
1711 case USB_SPEED_SUPER:
1712 dwc3_gadget_usb2_phy_power(dwc, false);
1713 break;
1714 case USB_SPEED_HIGH:
1715 case USB_SPEED_FULL:
1716 case USB_SPEED_LOW:
1717 dwc3_gadget_usb3_phy_power(dwc, false);
1718 break;
1719 }
1720}
1721
1722static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1723{
1724 struct dwc3_gadget_ep_cmd_params params;
1725 struct dwc3_ep *dep;
1726 int ret;
1727 u32 reg;
1728 u8 speed;
1729
1730 dev_vdbg(dwc->dev, "%s\n", __func__);
1731
1732 memset(&params, 0x00, sizeof(params));
1733
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001734 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1735 speed = reg & DWC3_DSTS_CONNECTSPD;
1736 dwc->speed = speed;
1737
1738 dwc3_update_ram_clk_sel(dwc, speed);
1739
1740 switch (speed) {
1741 case DWC3_DCFG_SUPERSPEED:
1742 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1743 dwc->gadget.ep0->maxpacket = 512;
1744 dwc->gadget.speed = USB_SPEED_SUPER;
1745 break;
1746 case DWC3_DCFG_HIGHSPEED:
1747 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1748 dwc->gadget.ep0->maxpacket = 64;
1749 dwc->gadget.speed = USB_SPEED_HIGH;
1750 break;
1751 case DWC3_DCFG_FULLSPEED2:
1752 case DWC3_DCFG_FULLSPEED1:
1753 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1754 dwc->gadget.ep0->maxpacket = 64;
1755 dwc->gadget.speed = USB_SPEED_FULL;
1756 break;
1757 case DWC3_DCFG_LOWSPEED:
1758 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1759 dwc->gadget.ep0->maxpacket = 8;
1760 dwc->gadget.speed = USB_SPEED_LOW;
1761 break;
1762 }
1763
1764 /* Disable unneded PHY */
1765 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1766
1767 dep = dwc->eps[0];
1768 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1769 if (ret) {
1770 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1771 return;
1772 }
1773
1774 dep = dwc->eps[1];
1775 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1776 if (ret) {
1777 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1778 return;
1779 }
1780
1781 /*
1782 * Configure PHY via GUSB3PIPECTLn if required.
1783 *
1784 * Update GTXFIFOSIZn
1785 *
1786 * In both cases reset values should be sufficient.
1787 */
1788}
1789
1790static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1791{
1792 dev_vdbg(dwc->dev, "%s\n", __func__);
1793
1794 /*
1795 * TODO take core out of low power mode when that's
1796 * implemented.
1797 */
1798
1799 dwc->gadget_driver->resume(&dwc->gadget);
1800}
1801
1802static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1803 unsigned int evtinfo)
1804{
1805 dev_vdbg(dwc->dev, "%s\n", __func__);
1806
1807 /* The fith bit says SuperSpeed yes or no. */
1808 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1809}
1810
1811static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1812 const struct dwc3_event_devt *event)
1813{
1814 switch (event->type) {
1815 case DWC3_DEVICE_EVENT_DISCONNECT:
1816 dwc3_gadget_disconnect_interrupt(dwc);
1817 break;
1818 case DWC3_DEVICE_EVENT_RESET:
1819 dwc3_gadget_reset_interrupt(dwc);
1820 break;
1821 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1822 dwc3_gadget_conndone_interrupt(dwc);
1823 break;
1824 case DWC3_DEVICE_EVENT_WAKEUP:
1825 dwc3_gadget_wakeup_interrupt(dwc);
1826 break;
1827 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1828 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1829 break;
1830 case DWC3_DEVICE_EVENT_EOPF:
1831 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1832 break;
1833 case DWC3_DEVICE_EVENT_SOF:
1834 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1835 break;
1836 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1837 dev_vdbg(dwc->dev, "Erratic Error\n");
1838 break;
1839 case DWC3_DEVICE_EVENT_CMD_CMPL:
1840 dev_vdbg(dwc->dev, "Command Complete\n");
1841 break;
1842 case DWC3_DEVICE_EVENT_OVERFLOW:
1843 dev_vdbg(dwc->dev, "Overflow\n");
1844 break;
1845 default:
1846 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1847 }
1848}
1849
1850static void dwc3_process_event_entry(struct dwc3 *dwc,
1851 const union dwc3_event *event)
1852{
1853 /* Endpoint IRQ, handle it and return early */
1854 if (event->type.is_devspec == 0) {
1855 /* depevt */
1856 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1857 }
1858
1859 switch (event->type.type) {
1860 case DWC3_EVENT_TYPE_DEV:
1861 dwc3_gadget_interrupt(dwc, &event->devt);
1862 break;
1863 /* REVISIT what to do with Carkit and I2C events ? */
1864 default:
1865 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1866 }
1867}
1868
1869static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1870{
1871 struct dwc3_event_buffer *evt;
1872 int left;
1873 u32 count;
1874
1875 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1876 count &= DWC3_GEVNTCOUNT_MASK;
1877 if (!count)
1878 return IRQ_NONE;
1879
1880 evt = dwc->ev_buffs[buf];
1881 left = count;
1882
1883 while (left > 0) {
1884 union dwc3_event event;
1885
1886 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1887 dwc3_process_event_entry(dwc, &event);
1888 /*
1889 * XXX we wrap around correctly to the next entry as almost all
1890 * entries are 4 bytes in size. There is one entry which has 12
1891 * bytes which is a regular entry followed by 8 bytes data. ATM
1892 * I don't know how things are organized if were get next to the
1893 * a boundary so I worry about that once we try to handle that.
1894 */
1895 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1896 left -= 4;
1897
1898 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1899 }
1900
1901 return IRQ_HANDLED;
1902}
1903
1904static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1905{
1906 struct dwc3 *dwc = _dwc;
1907 int i;
1908 irqreturn_t ret = IRQ_NONE;
1909
1910 spin_lock(&dwc->lock);
1911
1912 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1913 irqreturn_t status;
1914
1915 status = dwc3_process_event_buf(dwc, i);
1916 if (status == IRQ_HANDLED)
1917 ret = status;
1918 }
1919
1920 spin_unlock(&dwc->lock);
1921
1922 return ret;
1923}
1924
1925/**
1926 * dwc3_gadget_init - Initializes gadget related registers
1927 * @dwc: Pointer to out controller context structure
1928 *
1929 * Returns 0 on success otherwise negative errno.
1930 */
1931int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1932{
1933 u32 reg;
1934 int ret;
1935 int irq;
1936
1937 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1938 &dwc->ctrl_req_addr, GFP_KERNEL);
1939 if (!dwc->ctrl_req) {
1940 dev_err(dwc->dev, "failed to allocate ctrl request\n");
1941 ret = -ENOMEM;
1942 goto err0;
1943 }
1944
1945 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
1946 &dwc->ep0_trb_addr, GFP_KERNEL);
1947 if (!dwc->ep0_trb) {
1948 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
1949 ret = -ENOMEM;
1950 goto err1;
1951 }
1952
1953 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
1954 sizeof(*dwc->setup_buf) * 2,
1955 &dwc->setup_buf_addr, GFP_KERNEL);
1956 if (!dwc->setup_buf) {
1957 dev_err(dwc->dev, "failed to allocate setup buffer\n");
1958 ret = -ENOMEM;
1959 goto err2;
1960 }
1961
Felipe Balbi64e96342011-08-27 22:07:53 +03001962 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
1963 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
1964 if (!dwc->ep0_bounce) {
1965 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
1966 ret = -ENOMEM;
1967 goto err3;
1968 }
1969
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001970 dev_set_name(&dwc->gadget.dev, "gadget");
1971
1972 dwc->gadget.ops = &dwc3_gadget_ops;
1973 dwc->gadget.is_dualspeed = true;
1974 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1975 dwc->gadget.dev.parent = dwc->dev;
1976
1977 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
1978
1979 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
1980 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
1981 dwc->gadget.dev.release = dwc3_gadget_release;
1982 dwc->gadget.name = "dwc3-gadget";
1983
1984 /*
1985 * REVISIT: Here we should clear all pending IRQs to be
1986 * sure we're starting from a well known location.
1987 */
1988
1989 ret = dwc3_gadget_init_endpoints(dwc);
1990 if (ret)
Felipe Balbi64e96342011-08-27 22:07:53 +03001991 goto err4;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001992
1993 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1994
1995 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
1996 "dwc3", dwc);
1997 if (ret) {
1998 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1999 irq, ret);
Felipe Balbi64e96342011-08-27 22:07:53 +03002000 goto err5;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002001 }
2002
2003 /* Enable all but Start and End of Frame IRQs */
2004 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2005 DWC3_DEVTEN_EVNTOVERFLOWEN |
2006 DWC3_DEVTEN_CMDCMPLTEN |
2007 DWC3_DEVTEN_ERRTICERREN |
2008 DWC3_DEVTEN_WKUPEVTEN |
2009 DWC3_DEVTEN_ULSTCNGEN |
2010 DWC3_DEVTEN_CONNECTDONEEN |
2011 DWC3_DEVTEN_USBRSTEN |
2012 DWC3_DEVTEN_DISCONNEVTEN);
2013 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2014
2015 ret = device_register(&dwc->gadget.dev);
2016 if (ret) {
2017 dev_err(dwc->dev, "failed to register gadget device\n");
2018 put_device(&dwc->gadget.dev);
Felipe Balbi64e96342011-08-27 22:07:53 +03002019 goto err6;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002020 }
2021
2022 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2023 if (ret) {
2024 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi64e96342011-08-27 22:07:53 +03002025 goto err7;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002026 }
2027
2028 return 0;
2029
Felipe Balbi64e96342011-08-27 22:07:53 +03002030err7:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002031 device_unregister(&dwc->gadget.dev);
2032
Felipe Balbi64e96342011-08-27 22:07:53 +03002033err6:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002034 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2035 free_irq(irq, dwc);
2036
Felipe Balbi64e96342011-08-27 22:07:53 +03002037err5:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002038 dwc3_gadget_free_endpoints(dwc);
2039
Felipe Balbi64e96342011-08-27 22:07:53 +03002040err4:
2041 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2042 dwc->ep0_bounce_addr);
2043
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002044err3:
2045 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2046 dwc->setup_buf, dwc->setup_buf_addr);
2047
2048err2:
2049 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2050 dwc->ep0_trb, dwc->ep0_trb_addr);
2051
2052err1:
2053 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2054 dwc->ctrl_req, dwc->ctrl_req_addr);
2055
2056err0:
2057 return ret;
2058}
2059
2060void dwc3_gadget_exit(struct dwc3 *dwc)
2061{
2062 int irq;
2063 int i;
2064
2065 usb_del_gadget_udc(&dwc->gadget);
2066 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2067
2068 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2069 free_irq(irq, dwc);
2070
2071 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2072 __dwc3_gadget_ep_disable(dwc->eps[i]);
2073
2074 dwc3_gadget_free_endpoints(dwc);
2075
Felipe Balbi64e96342011-08-27 22:07:53 +03002076 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2077 dwc->ep0_bounce_addr);
2078
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002079 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2080 dwc->setup_buf, dwc->setup_buf_addr);
2081
2082 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2083 dwc->ep0_trb, dwc->ep0_trb_addr);
2084
2085 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2086 dwc->ctrl_req, dwc->ctrl_req_addr);
2087
2088 device_unregister(&dwc->gadget.dev);
2089}