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