aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/udc
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-10-08 06:47:31 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-10-08 06:47:31 -0400
commit463311960e9312245418af98dce8c0161fd6b827 (patch)
tree9529b6063b10f1b85408ef4757d1917dfdb8292d /drivers/usb/gadget/udc
parent87d7bcee4f5973a593b0d50134364cfe5652ff33 (diff)
parent4ed9a3d455558406cad83d38764ee659de25851c (diff)
Merge tag 'usb-3.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
Pull USB updates from Greg KH: "Here's the big USB patchset for 3.18-rc1. Also in here is the PHY tree, as it seems to fit well with the USB tree for various reasons... Anyway, lots of little changes in here, all over the place, full details in the changelog All have been in the linux-next tree for a while with no issues" * tag 'usb-3.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb: (244 commits) USB: host: st: fix typo 'CONFIG_USB_EHCI_HCD_ST' uas: Reduce number of function arguments for uas_alloc_foo functions xhci: Allow xHCI drivers to be built as separate modules xhci: Export symbols used by host-controller drivers xhci: Check for XHCI_COMP_MODE_QUIRK when disabling D3cold xhci: Introduce xhci_init_driver() usb: hcd: add generic PHY support usb: rename phy to usb_phy in HCD usb: gadget: uvc: fix up uvcg_v4l2_get_unmapped_area typo USB: host: st: fix ehci/ohci driver selection usb: host: ehci-exynos: Remove unnecessary usb-phy support usb: core: return -ENOTSUPP for all targeted hosts USB: Remove .owner field for driver usb: core: log higher level message on malformed LANGID descriptor usb: Add LED triggers for USB activity usb: Rename usb-common.c usb: gadget: Refactor request completion usb: gadget: Introduce usb_gadget_giveback_request() usb: dwc2/gadget: move phy bus legth initialization phy: remove .owner field for drivers using module_platform_driver ...
Diffstat (limited to 'drivers/usb/gadget/udc')
-rw-r--r--drivers/usb/gadget/udc/Kconfig17
-rw-r--r--drivers/usb/gadget/udc/Makefile1
-rw-r--r--drivers/usb/gadget/udc/amd5536udc.c2
-rw-r--r--drivers/usb/gadget/udc/at91_udc.c2
-rw-r--r--drivers/usb/gadget/udc/atmel_usba_udc.c4
-rw-r--r--drivers/usb/gadget/udc/bcm63xx_udc.c2
-rw-r--r--drivers/usb/gadget/udc/dummy_hcd.c10
-rw-r--r--drivers/usb/gadget/udc/fotg210-udc.c2
-rw-r--r--drivers/usb/gadget/udc/fsl_qe_udc.c6
-rw-r--r--drivers/usb/gadget/udc/fsl_udc_core.c6
-rw-r--r--drivers/usb/gadget/udc/fusb300_udc.c2
-rw-r--r--drivers/usb/gadget/udc/goku_udc.c2
-rw-r--r--drivers/usb/gadget/udc/gr_udc.c83
-rw-r--r--drivers/usb/gadget/udc/gr_udc.h7
-rw-r--r--drivers/usb/gadget/udc/lpc32xx_udc.c2
-rw-r--r--drivers/usb/gadget/udc/m66592-udc.c2
-rw-r--r--drivers/usb/gadget/udc/mv_u3d_core.c8
-rw-r--r--drivers/usb/gadget/udc/mv_udc_core.c8
-rw-r--r--drivers/usb/gadget/udc/net2272.c2
-rw-r--r--drivers/usb/gadget/udc/net2280.c2
-rw-r--r--drivers/usb/gadget/udc/omap_udc.c2
-rw-r--r--drivers/usb/gadget/udc/pch_udc.c2
-rw-r--r--drivers/usb/gadget/udc/pxa25x_udc.c2
-rw-r--r--drivers/usb/gadget/udc/pxa27x_udc.c2
-rw-r--r--drivers/usb/gadget/udc/r8a66597-udc.c12
-rw-r--r--drivers/usb/gadget/udc/s3c-hsudc.c3
-rw-r--r--drivers/usb/gadget/udc/s3c2410_udc.c2
-rw-r--r--drivers/usb/gadget/udc/udc-core.c51
-rw-r--r--drivers/usb/gadget/udc/udc-xilinx.c2180
29 files changed, 2350 insertions, 76 deletions
diff --git a/drivers/usb/gadget/udc/Kconfig b/drivers/usb/gadget/udc/Kconfig
index 34ebaa68504c..3ea287b0e448 100644
--- a/drivers/usb/gadget/udc/Kconfig
+++ b/drivers/usb/gadget/udc/Kconfig
@@ -163,7 +163,7 @@ config USB_R8A66597
163 163
164config USB_RENESAS_USBHS_UDC 164config USB_RENESAS_USBHS_UDC
165 tristate 'Renesas USBHS controller' 165 tristate 'Renesas USBHS controller'
166 depends on USB_RENESAS_USBHS 166 depends on USB_RENESAS_USBHS && HAS_DMA
167 help 167 help
168 Renesas USBHS is a discrete USB host and peripheral controller chip 168 Renesas USBHS is a discrete USB host and peripheral controller chip
169 that supports both full and high speed USB 2.0 data transfers. 169 that supports both full and high speed USB 2.0 data transfers.
@@ -354,6 +354,21 @@ config USB_EG20T
354 ML7213/ML7831 is completely compatible for Intel EG20T PCH. 354 ML7213/ML7831 is completely compatible for Intel EG20T PCH.
355 355
356 This driver can be used with Intel's Quark X1000 SOC platform 356 This driver can be used with Intel's Quark X1000 SOC platform
357
358config USB_GADGET_XILINX
359 tristate "Xilinx USB Driver"
360 depends on OF || COMPILE_TEST
361 help
362 USB peripheral controller driver for Xilinx USB2 device.
363 Xilinx USB2 device is a soft IP which supports both full
364 and high speed USB 2.0 data transfers. It has seven configurable
365 endpoints(bulk or interrupt or isochronous), as well as
366 endpoint zero(for control transfers).
367
368 Say "y" to link the driver statically, or "m" to build a
369 dynamically linked module called "udc-xilinx" and force all
370 gadget drivers to also be dynamically linked.
371
357# 372#
358# LAST -- dummy/emulated controller 373# LAST -- dummy/emulated controller
359# 374#
diff --git a/drivers/usb/gadget/udc/Makefile b/drivers/usb/gadget/udc/Makefile
index 4096122bb283..a7f4491593f1 100644
--- a/drivers/usb/gadget/udc/Makefile
+++ b/drivers/usb/gadget/udc/Makefile
@@ -29,3 +29,4 @@ obj-$(CONFIG_USB_FUSB300) += fusb300_udc.o
29obj-$(CONFIG_USB_FOTG210_UDC) += fotg210-udc.o 29obj-$(CONFIG_USB_FOTG210_UDC) += fotg210-udc.o
30obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o 30obj-$(CONFIG_USB_MV_U3D) += mv_u3d_core.o
31obj-$(CONFIG_USB_GR_UDC) += gr_udc.o 31obj-$(CONFIG_USB_GR_UDC) += gr_udc.o
32obj-$(CONFIG_USB_GADGET_XILINX) += udc-xilinx.o
diff --git a/drivers/usb/gadget/udc/amd5536udc.c b/drivers/usb/gadget/udc/amd5536udc.c
index 41b062eb4de0..3b9d13848a4f 100644
--- a/drivers/usb/gadget/udc/amd5536udc.c
+++ b/drivers/usb/gadget/udc/amd5536udc.c
@@ -841,7 +841,7 @@ __acquires(ep->dev->lock)
841 &req->req, req->req.length, ep->ep.name, sts); 841 &req->req, req->req.length, ep->ep.name, sts);
842 842
843 spin_unlock(&dev->lock); 843 spin_unlock(&dev->lock);
844 req->req.complete(&ep->ep, &req->req); 844 usb_gadget_giveback_request(&ep->ep, &req->req);
845 spin_lock(&dev->lock); 845 spin_lock(&dev->lock);
846 ep->halted = halted; 846 ep->halted = halted;
847} 847}
diff --git a/drivers/usb/gadget/udc/at91_udc.c b/drivers/usb/gadget/udc/at91_udc.c
index cfd18bcca723..9968f5331fe4 100644
--- a/drivers/usb/gadget/udc/at91_udc.c
+++ b/drivers/usb/gadget/udc/at91_udc.c
@@ -267,7 +267,7 @@ static void done(struct at91_ep *ep, struct at91_request *req, int status)
267 267
268 ep->stopped = 1; 268 ep->stopped = 1;
269 spin_unlock(&udc->lock); 269 spin_unlock(&udc->lock);
270 req->req.complete(&ep->ep, &req->req); 270 usb_gadget_giveback_request(&ep->ep, &req->req);
271 spin_lock(&udc->lock); 271 spin_lock(&udc->lock);
272 ep->stopped = stopped; 272 ep->stopped = stopped;
273 273
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
index c9fe67e29d35..1529926e20a0 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.c
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
@@ -463,7 +463,7 @@ static void receive_data(struct usba_ep *ep)
463 list_del_init(&req->queue); 463 list_del_init(&req->queue);
464 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); 464 usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
465 spin_unlock(&udc->lock); 465 spin_unlock(&udc->lock);
466 req->req.complete(&ep->ep, &req->req); 466 usb_gadget_giveback_request(&ep->ep, &req->req);
467 spin_lock(&udc->lock); 467 spin_lock(&udc->lock);
468 } 468 }
469 469
@@ -495,7 +495,7 @@ request_complete(struct usba_ep *ep, struct usba_request *req, int status)
495 ep->ep.name, req, req->req.status, req->req.actual); 495 ep->ep.name, req, req->req.status, req->req.actual);
496 496
497 spin_unlock(&udc->lock); 497 spin_unlock(&udc->lock);
498 req->req.complete(&ep->ep, &req->req); 498 usb_gadget_giveback_request(&ep->ep, &req->req);
499 spin_lock(&udc->lock); 499 spin_lock(&udc->lock);
500} 500}
501 501
diff --git a/drivers/usb/gadget/udc/bcm63xx_udc.c b/drivers/usb/gadget/udc/bcm63xx_udc.c
index e969eb809a85..2235b8808700 100644
--- a/drivers/usb/gadget/udc/bcm63xx_udc.c
+++ b/drivers/usb/gadget/udc/bcm63xx_udc.c
@@ -1088,7 +1088,7 @@ static int bcm63xx_ep_disable(struct usb_ep *ep)
1088 breq->req.status = -ESHUTDOWN; 1088 breq->req.status = -ESHUTDOWN;
1089 1089
1090 spin_unlock_irqrestore(&udc->lock, flags); 1090 spin_unlock_irqrestore(&udc->lock, flags);
1091 breq->req.complete(&iudma->bep->ep, &breq->req); 1091 usb_gadget_giveback_request(&iudma->bep->ep, &breq->req);
1092 spin_lock_irqsave(&udc->lock, flags); 1092 spin_lock_irqsave(&udc->lock, flags);
1093 } 1093 }
1094 } 1094 }
diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c
index 2b54955d3166..81dc5959e36b 100644
--- a/drivers/usb/gadget/udc/dummy_hcd.c
+++ b/drivers/usb/gadget/udc/dummy_hcd.c
@@ -258,7 +258,7 @@ static void nuke(struct dummy *dum, struct dummy_ep *ep)
258 req->req.status = -ESHUTDOWN; 258 req->req.status = -ESHUTDOWN;
259 259
260 spin_unlock(&dum->lock); 260 spin_unlock(&dum->lock);
261 req->req.complete(&ep->ep, &req->req); 261 usb_gadget_giveback_request(&ep->ep, &req->req);
262 spin_lock(&dum->lock); 262 spin_lock(&dum->lock);
263 } 263 }
264} 264}
@@ -658,7 +658,7 @@ static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
658 spin_unlock(&dum->lock); 658 spin_unlock(&dum->lock);
659 _req->actual = _req->length; 659 _req->actual = _req->length;
660 _req->status = 0; 660 _req->status = 0;
661 _req->complete(_ep, _req); 661 usb_gadget_giveback_request(_ep, _req);
662 spin_lock(&dum->lock); 662 spin_lock(&dum->lock);
663 } else 663 } else
664 list_add_tail(&req->queue, &ep->queue); 664 list_add_tail(&req->queue, &ep->queue);
@@ -702,7 +702,7 @@ static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
702 dev_dbg(udc_dev(dum), 702 dev_dbg(udc_dev(dum),
703 "dequeued req %p from %s, len %d buf %p\n", 703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf); 704 req, _ep->name, _req->length, _req->buf);
705 _req->complete(_ep, _req); 705 usb_gadget_giveback_request(_ep, _req);
706 } 706 }
707 local_irq_restore(flags); 707 local_irq_restore(flags);
708 return retval; 708 return retval;
@@ -1385,7 +1385,7 @@ top:
1385 list_del_init(&req->queue); 1385 list_del_init(&req->queue);
1386 1386
1387 spin_unlock(&dum->lock); 1387 spin_unlock(&dum->lock);
1388 req->req.complete(&ep->ep, &req->req); 1388 usb_gadget_giveback_request(&ep->ep, &req->req);
1389 spin_lock(&dum->lock); 1389 spin_lock(&dum->lock);
1390 1390
1391 /* requests might have been unlinked... */ 1391 /* requests might have been unlinked... */
@@ -1761,7 +1761,7 @@ restart:
1761 req); 1761 req);
1762 1762
1763 spin_unlock(&dum->lock); 1763 spin_unlock(&dum->lock);
1764 req->req.complete(&ep->ep, &req->req); 1764 usb_gadget_giveback_request(&ep->ep, &req->req);
1765 spin_lock(&dum->lock); 1765 spin_lock(&dum->lock);
1766 ep->already_seen = 0; 1766 ep->already_seen = 0;
1767 goto restart; 1767 goto restart;
diff --git a/drivers/usb/gadget/udc/fotg210-udc.c b/drivers/usb/gadget/udc/fotg210-udc.c
index e143d69f6017..1d315921bf34 100644
--- a/drivers/usb/gadget/udc/fotg210-udc.c
+++ b/drivers/usb/gadget/udc/fotg210-udc.c
@@ -70,7 +70,7 @@ static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
70 req->req.status = status; 70 req->req.status = status;
71 71
72 spin_unlock(&ep->fotg210->lock); 72 spin_unlock(&ep->fotg210->lock);
73 req->req.complete(&ep->ep, &req->req); 73 usb_gadget_giveback_request(&ep->ep, &req->req);
74 spin_lock(&ep->fotg210->lock); 74 spin_lock(&ep->fotg210->lock);
75 75
76 if (ep->epnum) { 76 if (ep->epnum) {
diff --git a/drivers/usb/gadget/udc/fsl_qe_udc.c b/drivers/usb/gadget/udc/fsl_qe_udc.c
index 732430804841..dd18ea38e391 100644
--- a/drivers/usb/gadget/udc/fsl_qe_udc.c
+++ b/drivers/usb/gadget/udc/fsl_qe_udc.c
@@ -118,10 +118,7 @@ static void done(struct qe_ep *ep, struct qe_req *req, int status)
118 ep->stopped = 1; 118 ep->stopped = 1;
119 spin_unlock(&udc->lock); 119 spin_unlock(&udc->lock);
120 120
121 /* this complete() should a func implemented by gadget layer, 121 usb_gadget_giveback_request(&ep->ep, &req->req);
122 * eg fsg->bulk_in_complete() */
123 if (req->req.complete)
124 req->req.complete(&ep->ep, &req->req);
125 122
126 spin_lock(&udc->lock); 123 spin_lock(&udc->lock);
127 124
@@ -2728,4 +2725,3 @@ module_platform_driver(udc_driver);
2728MODULE_DESCRIPTION(DRIVER_DESC); 2725MODULE_DESCRIPTION(DRIVER_DESC);
2729MODULE_AUTHOR(DRIVER_AUTHOR); 2726MODULE_AUTHOR(DRIVER_AUTHOR);
2730MODULE_LICENSE("GPL"); 2727MODULE_LICENSE("GPL");
2731
diff --git a/drivers/usb/gadget/udc/fsl_udc_core.c b/drivers/usb/gadget/udc/fsl_udc_core.c
index 75b23ea077a7..c3620791a315 100644
--- a/drivers/usb/gadget/udc/fsl_udc_core.c
+++ b/drivers/usb/gadget/udc/fsl_udc_core.c
@@ -197,10 +197,8 @@ __acquires(ep->udc->lock)
197 ep->stopped = 1; 197 ep->stopped = 1;
198 198
199 spin_unlock(&ep->udc->lock); 199 spin_unlock(&ep->udc->lock);
200 /* complete() is from gadget layer, 200
201 * eg fsg->bulk_in_complete() */ 201 usb_gadget_giveback_request(&ep->ep, &req->req);
202 if (req->req.complete)
203 req->req.complete(&ep->ep, &req->req);
204 202
205 spin_lock(&ep->udc->lock); 203 spin_lock(&ep->udc->lock);
206 ep->stopped = stopped; 204 ep->stopped = stopped;
diff --git a/drivers/usb/gadget/udc/fusb300_udc.c b/drivers/usb/gadget/udc/fusb300_udc.c
index 5c5d1adda7eb..8286df72add4 100644
--- a/drivers/usb/gadget/udc/fusb300_udc.c
+++ b/drivers/usb/gadget/udc/fusb300_udc.c
@@ -876,7 +876,7 @@ static void done(struct fusb300_ep *ep, struct fusb300_request *req,
876 req->req.status = status; 876 req->req.status = status;
877 877
878 spin_unlock(&ep->fusb300->lock); 878 spin_unlock(&ep->fusb300->lock);
879 req->req.complete(&ep->ep, &req->req); 879 usb_gadget_giveback_request(&ep->ep, &req->req);
880 spin_lock(&ep->fusb300->lock); 880 spin_lock(&ep->fusb300->lock);
881 881
882 if (ep->epnum) { 882 if (ep->epnum) {
diff --git a/drivers/usb/gadget/udc/goku_udc.c b/drivers/usb/gadget/udc/goku_udc.c
index 6c85839e15ad..bf9c5ef8b56b 100644
--- a/drivers/usb/gadget/udc/goku_udc.c
+++ b/drivers/usb/gadget/udc/goku_udc.c
@@ -320,7 +320,7 @@ done(struct goku_ep *ep, struct goku_request *req, int status)
320 /* don't modify queue heads during completion callback */ 320 /* don't modify queue heads during completion callback */
321 ep->stopped = 1; 321 ep->stopped = 1;
322 spin_unlock(&dev->lock); 322 spin_unlock(&dev->lock);
323 req->req.complete(&ep->ep, &req->req); 323 usb_gadget_giveback_request(&ep->ep, &req->req);
324 spin_lock(&dev->lock); 324 spin_lock(&dev->lock);
325 ep->stopped = stopped; 325 ep->stopped = stopped;
326} 326}
diff --git a/drivers/usb/gadget/udc/gr_udc.c b/drivers/usb/gadget/udc/gr_udc.c
index 08df5c4f46ce..1b3048a6a2a3 100644
--- a/drivers/usb/gadget/udc/gr_udc.c
+++ b/drivers/usb/gadget/udc/gr_udc.c
@@ -318,8 +318,26 @@ static void gr_finish_request(struct gr_ep *ep, struct gr_request *req,
318 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in); 318 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
319 gr_free_dma_desc_chain(dev, req); 319 gr_free_dma_desc_chain(dev, req);
320 320
321 if (ep->is_in) /* For OUT, actual gets updated bit by bit */ 321 if (ep->is_in) { /* For OUT, req->req.actual gets updated bit by bit */
322 req->req.actual = req->req.length; 322 req->req.actual = req->req.length;
323 } else if (req->oddlen && req->req.actual > req->evenlen) {
324 /*
325 * Copy to user buffer in this case where length was not evenly
326 * divisible by ep->ep.maxpacket and the last descriptor was
327 * actually used.
328 */
329 char *buftail = ((char *)req->req.buf + req->evenlen);
330
331 memcpy(buftail, ep->tailbuf, req->oddlen);
332
333 if (req->req.actual > req->req.length) {
334 /* We got more data than was requested */
335 dev_dbg(ep->dev->dev, "Overflow for ep %s\n",
336 ep->ep.name);
337 gr_dbgprint_request("OVFL", ep, req);
338 req->req.status = -EOVERFLOW;
339 }
340 }
323 341
324 if (!status) { 342 if (!status) {
325 if (ep->is_in) 343 if (ep->is_in)
@@ -339,7 +357,7 @@ static void gr_finish_request(struct gr_ep *ep, struct gr_request *req,
339 } else if (req->req.complete) { 357 } else if (req->req.complete) {
340 spin_unlock(&dev->lock); 358 spin_unlock(&dev->lock);
341 359
342 req->req.complete(&ep->ep, &req->req); 360 usb_gadget_giveback_request(&ep->ep, &req->req);
343 361
344 spin_lock(&dev->lock); 362 spin_lock(&dev->lock);
345 } 363 }
@@ -379,6 +397,15 @@ static void gr_start_dma(struct gr_ep *ep)
379 /* A descriptor should already have been allocated */ 397 /* A descriptor should already have been allocated */
380 BUG_ON(!req->curr_desc); 398 BUG_ON(!req->curr_desc);
381 399
400 /*
401 * The DMA controller can not handle smaller OUT buffers than
402 * ep->ep.maxpacket. It could lead to buffer overruns if an unexpectedly
403 * long packet are received. Therefore an internal bounce buffer gets
404 * used when such a request gets enabled.
405 */
406 if (!ep->is_in && req->oddlen)
407 req->last_desc->data = ep->tailbuf_paddr;
408
382 wmb(); /* Make sure all is settled before handing it over to DMA */ 409 wmb(); /* Make sure all is settled before handing it over to DMA */
383 410
384 /* Set the descriptor pointer in the hardware */ 411 /* Set the descriptor pointer in the hardware */
@@ -480,11 +507,11 @@ static int gr_setup_out_desc_list(struct gr_ep *ep, struct gr_request *req,
480 dma_addr_t start = req->req.dma + bytes_used; 507 dma_addr_t start = req->req.dma + bytes_used;
481 u16 size = min(bytes_left, ep->bytes_per_buffer); 508 u16 size = min(bytes_left, ep->bytes_per_buffer);
482 509
483 /* Should not happen however - gr_queue stops such lengths */ 510 if (size < ep->bytes_per_buffer) {
484 if (size < ep->bytes_per_buffer) 511 /* Prepare using bounce buffer */
485 dev_warn(ep->dev->dev, 512 req->evenlen = req->req.length - bytes_left;
486 "Buffer overrun risk: %u < %u bytes/buffer\n", 513 req->oddlen = size;
487 size, ep->bytes_per_buffer); 514 }
488 515
489 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags); 516 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags);
490 if (ret) 517 if (ret)
@@ -584,18 +611,6 @@ static int gr_queue(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags)
584 return -EINVAL; 611 return -EINVAL;
585 } 612 }
586 613
587 /*
588 * The DMA controller can not handle smaller OUT buffers than
589 * maxpacket. It could lead to buffer overruns if unexpectedly long
590 * packet are received.
591 */
592 if (!ep->is_in && (req->req.length % ep->ep.maxpacket) != 0) {
593 dev_err(dev->dev,
594 "OUT request length %d is not multiple of maxpacket\n",
595 req->req.length);
596 return -EMSGSIZE;
597 }
598
599 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) { 614 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
600 dev_err(dev->dev, "-ESHUTDOWN"); 615 dev_err(dev->dev, "-ESHUTDOWN");
601 return -ESHUTDOWN; 616 return -ESHUTDOWN;
@@ -1286,8 +1301,8 @@ static int gr_handle_out_ep(struct gr_ep *ep)
1286 if (ctrl & GR_DESC_OUT_CTRL_SE) 1301 if (ctrl & GR_DESC_OUT_CTRL_SE)
1287 req->setup = 1; 1302 req->setup = 1;
1288 1303
1289 if (len < ep->ep.maxpacket || req->req.actual == req->req.length) { 1304 if (len < ep->ep.maxpacket || req->req.actual >= req->req.length) {
1290 /* Short packet or the expected size - we are done */ 1305 /* Short packet or >= expected size - we are done */
1291 1306
1292 if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) { 1307 if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) {
1293 /* 1308 /*
@@ -2015,6 +2030,11 @@ static int gr_ep_init(struct gr_udc *dev, int num, int is_in, u32 maxplimit)
2015 } 2030 }
2016 list_add_tail(&ep->ep_list, &dev->ep_list); 2031 list_add_tail(&ep->ep_list, &dev->ep_list);
2017 2032
2033 ep->tailbuf = dma_alloc_coherent(dev->dev, ep->ep.maxpacket_limit,
2034 &ep->tailbuf_paddr, GFP_ATOMIC);
2035 if (!ep->tailbuf)
2036 return -ENOMEM;
2037
2018 return 0; 2038 return 0;
2019} 2039}
2020 2040
@@ -2067,9 +2087,24 @@ static int gr_udc_init(struct gr_udc *dev)
2067 return 0; 2087 return 0;
2068} 2088}
2069 2089
2090static void gr_ep_remove(struct gr_udc *dev, int num, int is_in)
2091{
2092 struct gr_ep *ep;
2093
2094 if (is_in)
2095 ep = &dev->epi[num];
2096 else
2097 ep = &dev->epo[num];
2098
2099 if (ep->tailbuf)
2100 dma_free_coherent(dev->dev, ep->ep.maxpacket_limit,
2101 ep->tailbuf, ep->tailbuf_paddr);
2102}
2103
2070static int gr_remove(struct platform_device *pdev) 2104static int gr_remove(struct platform_device *pdev)
2071{ 2105{
2072 struct gr_udc *dev = platform_get_drvdata(pdev); 2106 struct gr_udc *dev = platform_get_drvdata(pdev);
2107 int i;
2073 2108
2074 if (dev->added) 2109 if (dev->added)
2075 usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */ 2110 usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */
@@ -2084,6 +2119,11 @@ static int gr_remove(struct platform_device *pdev)
2084 gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req); 2119 gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req);
2085 gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req); 2120 gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req);
2086 2121
2122 for (i = 0; i < dev->nepo; i++)
2123 gr_ep_remove(dev, i, 0);
2124 for (i = 0; i < dev->nepi; i++)
2125 gr_ep_remove(dev, i, 1);
2126
2087 return 0; 2127 return 0;
2088} 2128}
2089static int gr_request_irq(struct gr_udc *dev, int irq) 2129static int gr_request_irq(struct gr_udc *dev, int irq)
@@ -2131,7 +2171,6 @@ static int gr_probe(struct platform_device *pdev)
2131 dev->gadget.name = driver_name; 2171 dev->gadget.name = driver_name;
2132 dev->gadget.max_speed = USB_SPEED_HIGH; 2172 dev->gadget.max_speed = USB_SPEED_HIGH;
2133 dev->gadget.ops = &gr_ops; 2173 dev->gadget.ops = &gr_ops;
2134 dev->gadget.quirk_ep_out_aligned_size = true;
2135 2174
2136 spin_lock_init(&dev->lock); 2175 spin_lock_init(&dev->lock);
2137 dev->regs = regs; 2176 dev->regs = regs;
diff --git a/drivers/usb/gadget/udc/gr_udc.h b/drivers/usb/gadget/udc/gr_udc.h
index 8388897d9ec3..4297c4e8021f 100644
--- a/drivers/usb/gadget/udc/gr_udc.h
+++ b/drivers/usb/gadget/udc/gr_udc.h
@@ -156,6 +156,10 @@ struct gr_ep {
156 struct list_head queue; 156 struct list_head queue;
157 157
158 struct list_head ep_list; 158 struct list_head ep_list;
159
160 /* Bounce buffer for end of "odd" sized OUT requests */
161 void *tailbuf;
162 dma_addr_t tailbuf_paddr;
159}; 163};
160 164
161struct gr_request { 165struct gr_request {
@@ -167,6 +171,9 @@ struct gr_request {
167 struct gr_dma_desc *curr_desc; /* Current descriptor */ 171 struct gr_dma_desc *curr_desc; /* Current descriptor */
168 struct gr_dma_desc *last_desc; /* Last in the chain */ 172 struct gr_dma_desc *last_desc; /* Last in the chain */
169 173
174 u16 evenlen; /* Size of even length head (if oddlen != 0) */
175 u16 oddlen; /* Size of odd length tail if buffer length is "odd" */
176
170 u8 setup; /* Setup packet */ 177 u8 setup; /* Setup packet */
171}; 178};
172 179
diff --git a/drivers/usb/gadget/udc/lpc32xx_udc.c b/drivers/usb/gadget/udc/lpc32xx_udc.c
index 1629ad7dcb80..feab0bac8fdc 100644
--- a/drivers/usb/gadget/udc/lpc32xx_udc.c
+++ b/drivers/usb/gadget/udc/lpc32xx_udc.c
@@ -1479,7 +1479,7 @@ static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
1479 1479
1480 ep->req_pending = 0; 1480 ep->req_pending = 0;
1481 spin_unlock(&udc->lock); 1481 spin_unlock(&udc->lock);
1482 req->req.complete(&ep->ep, &req->req); 1482 usb_gadget_giveback_request(&ep->ep, &req->req);
1483 spin_lock(&udc->lock); 1483 spin_lock(&udc->lock);
1484} 1484}
1485 1485
diff --git a/drivers/usb/gadget/udc/m66592-udc.c b/drivers/usb/gadget/udc/m66592-udc.c
index de88d33b44b2..898565687a8c 100644
--- a/drivers/usb/gadget/udc/m66592-udc.c
+++ b/drivers/usb/gadget/udc/m66592-udc.c
@@ -729,7 +729,7 @@ __acquires(m66592->lock)
729 restart = 1; 729 restart = 1;
730 730
731 spin_unlock(&ep->m66592->lock); 731 spin_unlock(&ep->m66592->lock);
732 req->req.complete(&ep->ep, &req->req); 732 usb_gadget_giveback_request(&ep->ep, &req->req);
733 spin_lock(&ep->m66592->lock); 733 spin_lock(&ep->m66592->lock);
734 734
735 if (restart) { 735 if (restart) {
diff --git a/drivers/usb/gadget/udc/mv_u3d_core.c b/drivers/usb/gadget/udc/mv_u3d_core.c
index 16248711c152..046a1f808b0d 100644
--- a/drivers/usb/gadget/udc/mv_u3d_core.c
+++ b/drivers/usb/gadget/udc/mv_u3d_core.c
@@ -222,12 +222,8 @@ void mv_u3d_done(struct mv_u3d_ep *ep, struct mv_u3d_req *req, int status)
222 } 222 }
223 223
224 spin_unlock(&ep->u3d->lock); 224 spin_unlock(&ep->u3d->lock);
225 /* 225
226 * complete() is from gadget layer, 226 usb_gadget_giveback_request(&ep->ep, &req->req);
227 * eg fsg->bulk_in_complete()
228 */
229 if (req->req.complete)
230 req->req.complete(&ep->ep, &req->req);
231 227
232 spin_lock(&ep->u3d->lock); 228 spin_lock(&ep->u3d->lock);
233} 229}
diff --git a/drivers/usb/gadget/udc/mv_udc_core.c b/drivers/usb/gadget/udc/mv_udc_core.c
index 040fb169b162..3c5db80ae325 100644
--- a/drivers/usb/gadget/udc/mv_udc_core.c
+++ b/drivers/usb/gadget/udc/mv_udc_core.c
@@ -248,12 +248,8 @@ static void done(struct mv_ep *ep, struct mv_req *req, int status)
248 ep->stopped = 1; 248 ep->stopped = 1;
249 249
250 spin_unlock(&ep->udc->lock); 250 spin_unlock(&ep->udc->lock);
251 /* 251
252 * complete() is from gadget layer, 252 usb_gadget_giveback_request(&ep->ep, &req->req);
253 * eg fsg->bulk_in_complete()
254 */
255 if (req->req.complete)
256 req->req.complete(&ep->ep, &req->req);
257 253
258 spin_lock(&ep->udc->lock); 254 spin_lock(&ep->udc->lock);
259 ep->stopped = stopped; 255 ep->stopped = stopped;
diff --git a/drivers/usb/gadget/udc/net2272.c b/drivers/usb/gadget/udc/net2272.c
index 059cfe527982..84d7162a8022 100644
--- a/drivers/usb/gadget/udc/net2272.c
+++ b/drivers/usb/gadget/udc/net2272.c
@@ -394,7 +394,7 @@ net2272_done(struct net2272_ep *ep, struct net2272_request *req, int status)
394 /* don't modify queue heads during completion callback */ 394 /* don't modify queue heads during completion callback */
395 ep->stopped = 1; 395 ep->stopped = 1;
396 spin_unlock(&dev->lock); 396 spin_unlock(&dev->lock);
397 req->req.complete(&ep->ep, &req->req); 397 usb_gadget_giveback_request(&ep->ep, &req->req);
398 spin_lock(&dev->lock); 398 spin_lock(&dev->lock);
399 ep->stopped = stopped; 399 ep->stopped = stopped;
400} 400}
diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
index 2e95715b50c0..8d13337e2dde 100644
--- a/drivers/usb/gadget/udc/net2280.c
+++ b/drivers/usb/gadget/udc/net2280.c
@@ -928,7 +928,7 @@ done(struct net2280_ep *ep, struct net2280_request *req, int status)
928 /* don't modify queue heads during completion callback */ 928 /* don't modify queue heads during completion callback */
929 ep->stopped = 1; 929 ep->stopped = 1;
930 spin_unlock(&dev->lock); 930 spin_unlock(&dev->lock);
931 req->req.complete(&ep->ep, &req->req); 931 usb_gadget_giveback_request(&ep->ep, &req->req);
932 spin_lock(&dev->lock); 932 spin_lock(&dev->lock);
933 ep->stopped = stopped; 933 ep->stopped = stopped;
934} 934}
diff --git a/drivers/usb/gadget/udc/omap_udc.c b/drivers/usb/gadget/udc/omap_udc.c
index e731373fd4d7..dcdfea46003b 100644
--- a/drivers/usb/gadget/udc/omap_udc.c
+++ b/drivers/usb/gadget/udc/omap_udc.c
@@ -315,7 +315,7 @@ done(struct omap_ep *ep, struct omap_req *req, int status)
315 /* don't modify queue heads during completion callback */ 315 /* don't modify queue heads during completion callback */
316 ep->stopped = 1; 316 ep->stopped = 1;
317 spin_unlock(&ep->udc->lock); 317 spin_unlock(&ep->udc->lock);
318 req->req.complete(&ep->ep, &req->req); 318 usb_gadget_giveback_request(&ep->ep, &req->req);
319 spin_lock(&ep->udc->lock); 319 spin_lock(&ep->udc->lock);
320 ep->stopped = stopped; 320 ep->stopped = stopped;
321} 321}
diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c
index 460d953c91b6..ccbe3d4a2a50 100644
--- a/drivers/usb/gadget/udc/pch_udc.c
+++ b/drivers/usb/gadget/udc/pch_udc.c
@@ -1490,7 +1490,7 @@ static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req,
1490 spin_unlock(&dev->lock); 1490 spin_unlock(&dev->lock);
1491 if (!ep->in) 1491 if (!ep->in)
1492 pch_udc_ep_clear_rrdy(ep); 1492 pch_udc_ep_clear_rrdy(ep);
1493 req->req.complete(&ep->ep, &req->req); 1493 usb_gadget_giveback_request(&ep->ep, &req->req);
1494 spin_lock(&dev->lock); 1494 spin_lock(&dev->lock);
1495 ep->halted = halted; 1495 ep->halted = halted;
1496} 1496}
diff --git a/drivers/usb/gadget/udc/pxa25x_udc.c b/drivers/usb/gadget/udc/pxa25x_udc.c
index 251e4d5ee152..42f7eeb8ff6f 100644
--- a/drivers/usb/gadget/udc/pxa25x_udc.c
+++ b/drivers/usb/gadget/udc/pxa25x_udc.c
@@ -347,7 +347,7 @@ static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
347 347
348 /* don't modify queue heads during completion callback */ 348 /* don't modify queue heads during completion callback */
349 ep->stopped = 1; 349 ep->stopped = 1;
350 req->req.complete(&ep->ep, &req->req); 350 usb_gadget_giveback_request(&ep->ep, &req->req);
351 ep->stopped = stopped; 351 ep->stopped = stopped;
352} 352}
353 353
diff --git a/drivers/usb/gadget/udc/pxa27x_udc.c b/drivers/usb/gadget/udc/pxa27x_udc.c
index 597d39f89420..4868369eeec6 100644
--- a/drivers/usb/gadget/udc/pxa27x_udc.c
+++ b/drivers/usb/gadget/udc/pxa27x_udc.c
@@ -758,7 +758,7 @@ static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
758 if (pflags) 758 if (pflags)
759 spin_unlock_irqrestore(&ep->lock, *pflags); 759 spin_unlock_irqrestore(&ep->lock, *pflags);
760 local_irq_save(flags); 760 local_irq_save(flags);
761 req->req.complete(&req->udc_usb_ep->usb_ep, &req->req); 761 usb_gadget_giveback_request(&req->udc_usb_ep->usb_ep, &req->req);
762 local_irq_restore(flags); 762 local_irq_restore(flags);
763 if (pflags) 763 if (pflags)
764 spin_lock_irqsave(&ep->lock, *pflags); 764 spin_lock_irqsave(&ep->lock, *pflags);
diff --git a/drivers/usb/gadget/udc/r8a66597-udc.c b/drivers/usb/gadget/udc/r8a66597-udc.c
index de2a8713b428..f8186613b53e 100644
--- a/drivers/usb/gadget/udc/r8a66597-udc.c
+++ b/drivers/usb/gadget/udc/r8a66597-udc.c
@@ -430,7 +430,7 @@ static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
430 ep->pipenum = pipenum; 430 ep->pipenum = pipenum;
431 ep->ep.maxpacket = usb_endpoint_maxp(desc); 431 ep->ep.maxpacket = usb_endpoint_maxp(desc);
432 r8a66597->pipenum2ep[pipenum] = ep; 432 r8a66597->pipenum2ep[pipenum] = ep;
433 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK] 433 r8a66597->epaddr2ep[usb_endpoint_num(desc)]
434 = ep; 434 = ep;
435 INIT_LIST_HEAD(&ep->queue); 435 INIT_LIST_HEAD(&ep->queue);
436} 436}
@@ -464,7 +464,7 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
464 if (ep->pipenum) /* already allocated pipe */ 464 if (ep->pipenum) /* already allocated pipe */
465 return 0; 465 return 0;
466 466
467 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 467 switch (usb_endpoint_type(desc)) {
468 case USB_ENDPOINT_XFER_BULK: 468 case USB_ENDPOINT_XFER_BULK:
469 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) { 469 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
470 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) { 470 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
@@ -509,7 +509,7 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
509 } 509 }
510 ep->type = info.type; 510 ep->type = info.type;
511 511
512 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 512 info.epnum = usb_endpoint_num(desc);
513 info.maxpacket = usb_endpoint_maxp(desc); 513 info.maxpacket = usb_endpoint_maxp(desc);
514 info.interval = desc->bInterval; 514 info.interval = desc->bInterval;
515 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 515 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
@@ -925,7 +925,7 @@ __acquires(r8a66597->lock)
925 sudmac_free_channel(ep->r8a66597, ep, req); 925 sudmac_free_channel(ep->r8a66597, ep, req);
926 926
927 spin_unlock(&ep->r8a66597->lock); 927 spin_unlock(&ep->r8a66597->lock);
928 req->req.complete(&ep->ep, &req->req); 928 usb_gadget_giveback_request(&ep->ep, &req->req);
929 spin_lock(&ep->r8a66597->lock); 929 spin_lock(&ep->r8a66597->lock);
930 930
931 if (restart) { 931 if (restart) {
@@ -1846,10 +1846,8 @@ static int r8a66597_sudmac_ioremap(struct r8a66597 *r8a66597,
1846 1846
1847 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sudmac"); 1847 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sudmac");
1848 r8a66597->sudmac_reg = devm_ioremap_resource(&pdev->dev, res); 1848 r8a66597->sudmac_reg = devm_ioremap_resource(&pdev->dev, res);
1849 if (IS_ERR(r8a66597->sudmac_reg)) { 1849 if (IS_ERR(r8a66597->sudmac_reg))
1850 dev_err(&pdev->dev, "ioremap error(sudmac).\n");
1851 return PTR_ERR(r8a66597->sudmac_reg); 1850 return PTR_ERR(r8a66597->sudmac_reg);
1852 }
1853 1851
1854 return 0; 1852 return 0;
1855} 1853}
diff --git a/drivers/usb/gadget/udc/s3c-hsudc.c b/drivers/usb/gadget/udc/s3c-hsudc.c
index 10c6a128250c..dfbf55797360 100644
--- a/drivers/usb/gadget/udc/s3c-hsudc.c
+++ b/drivers/usb/gadget/udc/s3c-hsudc.c
@@ -258,8 +258,7 @@ static void s3c_hsudc_complete_request(struct s3c_hsudc_ep *hsep,
258 258
259 hsep->stopped = 1; 259 hsep->stopped = 1;
260 spin_unlock(&hsudc->lock); 260 spin_unlock(&hsudc->lock);
261 if (hsreq->req.complete != NULL) 261 usb_gadget_giveback_request(&hsep->ep, &hsreq->req);
262 hsreq->req.complete(&hsep->ep, &hsreq->req);
263 spin_lock(&hsudc->lock); 262 spin_lock(&hsudc->lock);
264 hsep->stopped = stopped; 263 hsep->stopped = stopped;
265} 264}
diff --git a/drivers/usb/gadget/udc/s3c2410_udc.c b/drivers/usb/gadget/udc/s3c2410_udc.c
index 357b58e0087b..ff423d15beff 100644
--- a/drivers/usb/gadget/udc/s3c2410_udc.c
+++ b/drivers/usb/gadget/udc/s3c2410_udc.c
@@ -272,7 +272,7 @@ static void s3c2410_udc_done(struct s3c2410_ep *ep,
272 status = req->req.status; 272 status = req->req.status;
273 273
274 ep->halted = 1; 274 ep->halted = 1;
275 req->req.complete(&ep->ep, &req->req); 275 usb_gadget_giveback_request(&ep->ep, &req->req);
276 ep->halted = halted; 276 ep->halted = halted;
277} 277}
278 278
diff --git a/drivers/usb/gadget/udc/udc-core.c b/drivers/usb/gadget/udc/udc-core.c
index b0d98172bc07..f107bb60a5ab 100644
--- a/drivers/usb/gadget/udc/udc-core.c
+++ b/drivers/usb/gadget/udc/udc-core.c
@@ -27,6 +27,7 @@
27 27
28#include <linux/usb/ch9.h> 28#include <linux/usb/ch9.h>
29#include <linux/usb/gadget.h> 29#include <linux/usb/gadget.h>
30#include <linux/usb.h>
30 31
31/** 32/**
32 * struct usb_udc - describes one usb device controller 33 * struct usb_udc - describes one usb device controller
@@ -106,11 +107,42 @@ EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
106 107
107/* ------------------------------------------------------------------------- */ 108/* ------------------------------------------------------------------------- */
108 109
110/**
111 * usb_gadget_giveback_request - give the request back to the gadget layer
112 * Context: in_interrupt()
113 *
114 * This is called by device controller drivers in order to return the
115 * completed request back to the gadget layer.
116 */
117void usb_gadget_giveback_request(struct usb_ep *ep,
118 struct usb_request *req)
119{
120 if (likely(req->status == 0))
121 usb_led_activity(USB_LED_EVENT_GADGET);
122
123 req->complete(ep, req);
124}
125EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
126
127/* ------------------------------------------------------------------------- */
128
109static void usb_gadget_state_work(struct work_struct *work) 129static void usb_gadget_state_work(struct work_struct *work)
110{ 130{
111 struct usb_gadget *gadget = work_to_gadget(work); 131 struct usb_gadget *gadget = work_to_gadget(work);
132 struct usb_udc *udc = NULL;
133
134 mutex_lock(&udc_lock);
135 list_for_each_entry(udc, &udc_list, list)
136 if (udc->gadget == gadget)
137 goto found;
138 mutex_unlock(&udc_lock);
139
140 return;
141
142found:
143 mutex_unlock(&udc_lock);
112 144
113 sysfs_notify(&gadget->dev.kobj, NULL, "state"); 145 sysfs_notify(&udc->dev.kobj, NULL, "state");
114} 146}
115 147
116void usb_gadget_set_state(struct usb_gadget *gadget, 148void usb_gadget_set_state(struct usb_gadget *gadget,
@@ -124,6 +156,23 @@ EXPORT_SYMBOL_GPL(usb_gadget_set_state);
124/* ------------------------------------------------------------------------- */ 156/* ------------------------------------------------------------------------- */
125 157
126/** 158/**
159 * usb_gadget_udc_reset - notifies the udc core that bus reset occurs
160 * @gadget: The gadget which bus reset occurs
161 * @driver: The gadget driver we want to notify
162 *
163 * If the udc driver has bus reset handler, it needs to call this when the bus
164 * reset occurs, it notifies the gadget driver that the bus reset occurs as
165 * well as updates gadget state.
166 */
167void usb_gadget_udc_reset(struct usb_gadget *gadget,
168 struct usb_gadget_driver *driver)
169{
170 driver->reset(gadget);
171 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
172}
173EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
174
175/**
127 * usb_gadget_udc_start - tells usb device controller to start up 176 * usb_gadget_udc_start - tells usb device controller to start up
128 * @gadget: The gadget we want to get started 177 * @gadget: The gadget we want to get started
129 * @driver: The driver we want to bind to @gadget 178 * @driver: The driver we want to bind to @gadget
diff --git a/drivers/usb/gadget/udc/udc-xilinx.c b/drivers/usb/gadget/udc/udc-xilinx.c
new file mode 100644
index 000000000000..ed27e1687a4e
--- /dev/null
+++ b/drivers/usb/gadget/udc/udc-xilinx.c
@@ -0,0 +1,2180 @@
1/*
2 * Xilinx USB peripheral controller driver
3 *
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
7 * Copyright (C) 2010 - 2014 Xilinx, Inc.
8 *
9 * Some parts of this driver code is based on the driver for at91-series
10 * USB peripheral controller (at91_udc.c).
11 *
12 * This program is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation;
15 * either version 2 of the License, or (at your option) any
16 * later version.
17 */
18
19#include <linux/delay.h>
20#include <linux/device.h>
21#include <linux/dma-mapping.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/module.h>
25#include <linux/of_address.h>
26#include <linux/of_device.h>
27#include <linux/of_platform.h>
28#include <linux/of_irq.h>
29#include <linux/prefetch.h>
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
33/* Register offsets for the USB device.*/
34#define XUSB_EP0_CONFIG_OFFSET 0x0000 /* EP0 Config Reg Offset */
35#define XUSB_SETUP_PKT_ADDR_OFFSET 0x0080 /* Setup Packet Address */
36#define XUSB_ADDRESS_OFFSET 0x0100 /* Address Register */
37#define XUSB_CONTROL_OFFSET 0x0104 /* Control Register */
38#define XUSB_STATUS_OFFSET 0x0108 /* Status Register */
39#define XUSB_FRAMENUM_OFFSET 0x010C /* Frame Number Register */
40#define XUSB_IER_OFFSET 0x0110 /* Interrupt Enable Register */
41#define XUSB_BUFFREADY_OFFSET 0x0114 /* Buffer Ready Register */
42#define XUSB_TESTMODE_OFFSET 0x0118 /* Test Mode Register */
43#define XUSB_DMA_RESET_OFFSET 0x0200 /* DMA Soft Reset Register */
44#define XUSB_DMA_CONTROL_OFFSET 0x0204 /* DMA Control Register */
45#define XUSB_DMA_DSAR_ADDR_OFFSET 0x0208 /* DMA source Address Reg */
46#define XUSB_DMA_DDAR_ADDR_OFFSET 0x020C /* DMA destination Addr Reg */
47#define XUSB_DMA_LENGTH_OFFSET 0x0210 /* DMA Length Register */
48#define XUSB_DMA_STATUS_OFFSET 0x0214 /* DMA Status Register */
49
50/* Endpoint Configuration Space offsets */
51#define XUSB_EP_CFGSTATUS_OFFSET 0x00 /* Endpoint Config Status */
52#define XUSB_EP_BUF0COUNT_OFFSET 0x08 /* Buffer 0 Count */
53#define XUSB_EP_BUF1COUNT_OFFSET 0x0C /* Buffer 1 Count */
54
55#define XUSB_CONTROL_USB_READY_MASK 0x80000000 /* USB ready Mask */
56#define XUSB_CONTROL_USB_RMTWAKE_MASK 0x40000000 /* Remote wake up mask */
57
58/* Interrupt register related masks.*/
59#define XUSB_STATUS_GLOBAL_INTR_MASK 0x80000000 /* Global Intr Enable */
60#define XUSB_STATUS_DMADONE_MASK 0x04000000 /* DMA done Mask */
61#define XUSB_STATUS_DMAERR_MASK 0x02000000 /* DMA Error Mask */
62#define XUSB_STATUS_DMABUSY_MASK 0x80000000 /* DMA Error Mask */
63#define XUSB_STATUS_RESUME_MASK 0x01000000 /* USB Resume Mask */
64#define XUSB_STATUS_RESET_MASK 0x00800000 /* USB Reset Mask */
65#define XUSB_STATUS_SUSPEND_MASK 0x00400000 /* USB Suspend Mask */
66#define XUSB_STATUS_DISCONNECT_MASK 0x00200000 /* USB Disconnect Mask */
67#define XUSB_STATUS_FIFO_BUFF_RDY_MASK 0x00100000 /* FIFO Buff Ready Mask */
68#define XUSB_STATUS_FIFO_BUFF_FREE_MASK 0x00080000 /* FIFO Buff Free Mask */
69#define XUSB_STATUS_SETUP_PACKET_MASK 0x00040000 /* Setup packet received */
70#define XUSB_STATUS_EP1_BUFF2_COMP_MASK 0x00000200 /* EP 1 Buff 2 Processed */
71#define XUSB_STATUS_EP1_BUFF1_COMP_MASK 0x00000002 /* EP 1 Buff 1 Processed */
72#define XUSB_STATUS_EP0_BUFF2_COMP_MASK 0x00000100 /* EP 0 Buff 2 Processed */
73#define XUSB_STATUS_EP0_BUFF1_COMP_MASK 0x00000001 /* EP 0 Buff 1 Processed */
74#define XUSB_STATUS_HIGH_SPEED_MASK 0x00010000 /* USB Speed Mask */
75/* Suspend,Reset,Suspend and Disconnect Mask */
76#define XUSB_STATUS_INTR_EVENT_MASK 0x01E00000
77/* Buffers completion Mask */
78#define XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK 0x0000FEFF
79/* Mask for buffer 0 and buffer 1 completion for all Endpoints */
80#define XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK 0x00000101
81#define XUSB_STATUS_EP_BUFF2_SHIFT 8 /* EP buffer offset */
82
83/* Endpoint Configuration Status Register */
84#define XUSB_EP_CFG_VALID_MASK 0x80000000 /* Endpoint Valid bit */
85#define XUSB_EP_CFG_STALL_MASK 0x40000000 /* Endpoint Stall bit */
86#define XUSB_EP_CFG_DATA_TOGGLE_MASK 0x08000000 /* Endpoint Data toggle */
87
88/* USB device specific global configuration constants.*/
89#define XUSB_MAX_ENDPOINTS 8 /* Maximum End Points */
90#define XUSB_EP_NUMBER_ZERO 0 /* End point Zero */
91/* DPRAM is the source address for DMA transfer */
92#define XUSB_DMA_READ_FROM_DPRAM 0x80000000
93#define XUSB_DMA_DMASR_BUSY 0x80000000 /* DMA busy */
94#define XUSB_DMA_DMASR_ERROR 0x40000000 /* DMA Error */
95/*
96 * When this bit is set, the DMA buffer ready bit is set by hardware upon
97 * DMA transfer completion.
98 */
99#define XUSB_DMA_BRR_CTRL 0x40000000 /* DMA bufready ctrl bit */
100/* Phase States */
101#define SETUP_PHASE 0x0000 /* Setup Phase */
102#define DATA_PHASE 0x0001 /* Data Phase */
103#define STATUS_PHASE 0x0002 /* Status Phase */
104
105#define EP0_MAX_PACKET 64 /* Endpoint 0 maximum packet length */
106#define STATUSBUFF_SIZE 2 /* Buffer size for GET_STATUS command */
107#define EPNAME_SIZE 4 /* Buffer size for endpoint name */
108
109/* container_of helper macros */
110#define to_udc(g) container_of((g), struct xusb_udc, gadget)
111#define to_xusb_ep(ep) container_of((ep), struct xusb_ep, ep_usb)
112#define to_xusb_req(req) container_of((req), struct xusb_req, usb_req)
113
114/**
115 * struct xusb_req - Xilinx USB device request structure
116 * @usb_req: Linux usb request structure
117 * @queue: usb device request queue
118 * @ep: pointer to xusb_endpoint structure
119 */
120struct xusb_req {
121 struct usb_request usb_req;
122 struct list_head queue;
123 struct xusb_ep *ep;
124};
125
126/**
127 * struct xusb_ep - USB end point structure.
128 * @ep_usb: usb endpoint instance
129 * @queue: endpoint message queue
130 * @udc: xilinx usb peripheral driver instance pointer
131 * @desc: pointer to the usb endpoint descriptor
132 * @rambase: the endpoint buffer address
133 * @offset: the endpoint register offset value
134 * @name: name of the endpoint
135 * @epnumber: endpoint number
136 * @maxpacket: maximum packet size the endpoint can store
137 * @buffer0count: the size of the packet recieved in the first buffer
138 * @buffer1count: the size of the packet received in the second buffer
139 * @curbufnum: current buffer of endpoint that will be processed next
140 * @buffer0ready: the busy state of first buffer
141 * @buffer1ready: the busy state of second buffer
142 * @is_in: endpoint direction (IN or OUT)
143 * @is_iso: endpoint type(isochronous or non isochronous)
144 */
145struct xusb_ep {
146 struct usb_ep ep_usb;
147 struct list_head queue;
148 struct xusb_udc *udc;
149 const struct usb_endpoint_descriptor *desc;
150 u32 rambase;
151 u32 offset;
152 char name[4];
153 u16 epnumber;
154 u16 maxpacket;
155 u16 buffer0count;
156 u16 buffer1count;
157 u8 curbufnum;
158 bool buffer0ready;
159 bool buffer1ready;
160 bool is_in;
161 bool is_iso;
162};
163
164/**
165 * struct xusb_udc - USB peripheral driver structure
166 * @gadget: USB gadget driver instance
167 * @ep: an array of endpoint structures
168 * @driver: pointer to the usb gadget driver instance
169 * @setup: usb_ctrlrequest structure for control requests
170 * @req: pointer to dummy request for get status command
171 * @dev: pointer to device structure in gadget
172 * @usb_state: device in suspended state or not
173 * @remote_wkp: remote wakeup enabled by host
174 * @setupseqtx: tx status
175 * @setupseqrx: rx status
176 * @addr: the usb device base address
177 * @lock: instance of spinlock
178 * @dma_enabled: flag indicating whether the dma is included in the system
179 * @read_fn: function pointer to read device registers
180 * @write_fn: function pointer to write to device registers
181 */
182struct xusb_udc {
183 struct usb_gadget gadget;
184 struct xusb_ep ep[8];
185 struct usb_gadget_driver *driver;
186 struct usb_ctrlrequest setup;
187 struct xusb_req *req;
188 struct device *dev;
189 u32 usb_state;
190 u32 remote_wkp;
191 u32 setupseqtx;
192 u32 setupseqrx;
193 void __iomem *addr;
194 spinlock_t lock;
195 bool dma_enabled;
196
197 unsigned int (*read_fn)(void __iomem *);
198 void (*write_fn)(void __iomem *, u32, u32);
199};
200
201/* Endpoint buffer start addresses in the core */
202static u32 rambase[8] = { 0x22, 0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500,
203 0x1600 };
204
205static const char driver_name[] = "xilinx-udc";
206static const char ep0name[] = "ep0";
207
208/* Control endpoint configuration.*/
209static const struct usb_endpoint_descriptor config_bulk_out_desc = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212 .bEndpointAddress = USB_DIR_OUT,
213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
214 .wMaxPacketSize = cpu_to_le16(EP0_MAX_PACKET),
215};
216
217/**
218 * xudc_write32 - little endian write to device registers
219 * @addr: base addr of device registers
220 * @offset: register offset
221 * @val: data to be written
222 */
223static void xudc_write32(void __iomem *addr, u32 offset, u32 val)
224{
225 iowrite32(val, addr + offset);
226}
227
228/**
229 * xudc_read32 - little endian read from device registers
230 * @addr: addr of device register
231 * Return: value at addr
232 */
233static unsigned int xudc_read32(void __iomem *addr)
234{
235 return ioread32(addr);
236}
237
238/**
239 * xudc_write32_be - big endian write to device registers
240 * @addr: base addr of device registers
241 * @offset: register offset
242 * @val: data to be written
243 */
244static void xudc_write32_be(void __iomem *addr, u32 offset, u32 val)
245{
246 iowrite32be(val, addr + offset);
247}
248
249/**
250 * xudc_read32_be - big endian read from device registers
251 * @addr: addr of device register
252 * Return: value at addr
253 */
254static unsigned int xudc_read32_be(void __iomem *addr)
255{
256 return ioread32be(addr);
257}
258
259/**
260 * xudc_wrstatus - Sets up the usb device status stages.
261 * @udc: pointer to the usb device controller structure.
262 */
263static void xudc_wrstatus(struct xusb_udc *udc)
264{
265 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
266 u32 epcfgreg;
267
268 epcfgreg = udc->read_fn(udc->addr + ep0->offset)|
269 XUSB_EP_CFG_DATA_TOGGLE_MASK;
270 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
271 udc->write_fn(udc->addr, ep0->offset + XUSB_EP_BUF0COUNT_OFFSET, 0);
272 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
273}
274
275/**
276 * xudc_epconfig - Configures the given endpoint.
277 * @ep: pointer to the usb device endpoint structure.
278 * @udc: pointer to the usb peripheral controller structure.
279 *
280 * This function configures a specific endpoint with the given configuration
281 * data.
282 */
283static void xudc_epconfig(struct xusb_ep *ep, struct xusb_udc *udc)
284{
285 u32 epcfgreg;
286
287 /*
288 * Configure the end point direction, type, Max Packet Size and the
289 * EP buffer location.
290 */
291 epcfgreg = ((ep->is_in << 29) | (ep->is_iso << 28) |
292 (ep->ep_usb.maxpacket << 15) | (ep->rambase));
293 udc->write_fn(udc->addr, ep->offset, epcfgreg);
294
295 /* Set the Buffer count and the Buffer ready bits.*/
296 udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF0COUNT_OFFSET,
297 ep->buffer0count);
298 udc->write_fn(udc->addr, ep->offset + XUSB_EP_BUF1COUNT_OFFSET,
299 ep->buffer1count);
300 if (ep->buffer0ready)
301 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
302 1 << ep->epnumber);
303 if (ep->buffer1ready)
304 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
305 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
306}
307
308/**
309 * xudc_start_dma - Starts DMA transfer.
310 * @ep: pointer to the usb device endpoint structure.
311 * @src: DMA source address.
312 * @dst: DMA destination address.
313 * @length: number of bytes to transfer.
314 *
315 * Return: 0 on success, error code on failure
316 *
317 * This function starts DMA transfer by writing to DMA source,
318 * destination and lenth registers.
319 */
320static int xudc_start_dma(struct xusb_ep *ep, dma_addr_t src,
321 dma_addr_t dst, u32 length)
322{
323 struct xusb_udc *udc = ep->udc;
324 int rc = 0;
325 u32 timeout = 500;
326 u32 reg;
327
328 /*
329 * Set the addresses in the DMA source and
330 * destination registers and then set the length
331 * into the DMA length register.
332 */
333 udc->write_fn(udc->addr, XUSB_DMA_DSAR_ADDR_OFFSET, src);
334 udc->write_fn(udc->addr, XUSB_DMA_DDAR_ADDR_OFFSET, dst);
335 udc->write_fn(udc->addr, XUSB_DMA_LENGTH_OFFSET, length);
336
337 /*
338 * Wait till DMA transaction is complete and
339 * check whether the DMA transaction was
340 * successful.
341 */
342 do {
343 reg = udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET);
344 if (!(reg & XUSB_DMA_DMASR_BUSY))
345 break;
346
347 /*
348 * We can't sleep here, because it's also called from
349 * interrupt context.
350 */
351 timeout--;
352 if (!timeout) {
353 dev_err(udc->dev, "DMA timeout\n");
354 return -ETIMEDOUT;
355 }
356 udelay(1);
357 } while (1);
358
359 if ((udc->read_fn(udc->addr + XUSB_DMA_STATUS_OFFSET) &
360 XUSB_DMA_DMASR_ERROR) == XUSB_DMA_DMASR_ERROR){
361 dev_err(udc->dev, "DMA Error\n");
362 rc = -EINVAL;
363 }
364
365 return rc;
366}
367
368/**
369 * xudc_dma_send - Sends IN data using DMA.
370 * @ep: pointer to the usb device endpoint structure.
371 * @req: pointer to the usb request structure.
372 * @buffer: pointer to data to be sent.
373 * @length: number of bytes to send.
374 *
375 * Return: 0 on success, -EAGAIN if no buffer is free and error
376 * code on failure.
377 *
378 * This function sends data using DMA.
379 */
380static int xudc_dma_send(struct xusb_ep *ep, struct xusb_req *req,
381 u8 *buffer, u32 length)
382{
383 u32 *eprambase;
384 dma_addr_t src;
385 dma_addr_t dst;
386 struct xusb_udc *udc = ep->udc;
387
388 src = req->usb_req.dma + req->usb_req.actual;
389 if (req->usb_req.length)
390 dma_sync_single_for_device(udc->dev, src,
391 length, DMA_TO_DEVICE);
392 if (!ep->curbufnum && !ep->buffer0ready) {
393 /* Get the Buffer address and copy the transmit data.*/
394 eprambase = (u32 __force *)(udc->addr + ep->rambase);
395 dst = virt_to_phys(eprambase);
396 udc->write_fn(udc->addr, ep->offset +
397 XUSB_EP_BUF0COUNT_OFFSET, length);
398 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
399 XUSB_DMA_BRR_CTRL | (1 << ep->epnumber));
400 ep->buffer0ready = 1;
401 ep->curbufnum = 1;
402 } else if (ep->curbufnum && !ep->buffer1ready) {
403 /* Get the Buffer address and copy the transmit data.*/
404 eprambase = (u32 __force *)(udc->addr + ep->rambase +
405 ep->ep_usb.maxpacket);
406 dst = virt_to_phys(eprambase);
407 udc->write_fn(udc->addr, ep->offset +
408 XUSB_EP_BUF1COUNT_OFFSET, length);
409 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
410 XUSB_DMA_BRR_CTRL | (1 << (ep->epnumber +
411 XUSB_STATUS_EP_BUFF2_SHIFT)));
412 ep->buffer1ready = 1;
413 ep->curbufnum = 0;
414 } else {
415 /* None of ping pong buffers are ready currently .*/
416 return -EAGAIN;
417 }
418
419 return xudc_start_dma(ep, src, dst, length);
420}
421
422/**
423 * xudc_dma_receive - Receives OUT data using DMA.
424 * @ep: pointer to the usb device endpoint structure.
425 * @req: pointer to the usb request structure.
426 * @buffer: pointer to storage buffer of received data.
427 * @length: number of bytes to receive.
428 *
429 * Return: 0 on success, -EAGAIN if no buffer is free and error
430 * code on failure.
431 *
432 * This function receives data using DMA.
433 */
434static int xudc_dma_receive(struct xusb_ep *ep, struct xusb_req *req,
435 u8 *buffer, u32 length)
436{
437 u32 *eprambase;
438 dma_addr_t src;
439 dma_addr_t dst;
440 struct xusb_udc *udc = ep->udc;
441
442 dst = req->usb_req.dma + req->usb_req.actual;
443 if (!ep->curbufnum && !ep->buffer0ready) {
444 /* Get the Buffer address and copy the transmit data */
445 eprambase = (u32 __force *)(udc->addr + ep->rambase);
446 src = virt_to_phys(eprambase);
447 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
448 XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
449 (1 << ep->epnumber));
450 ep->buffer0ready = 1;
451 ep->curbufnum = 1;
452 } else if (ep->curbufnum && !ep->buffer1ready) {
453 /* Get the Buffer address and copy the transmit data */
454 eprambase = (u32 __force *)(udc->addr +
455 ep->rambase + ep->ep_usb.maxpacket);
456 src = virt_to_phys(eprambase);
457 udc->write_fn(udc->addr, XUSB_DMA_CONTROL_OFFSET,
458 XUSB_DMA_BRR_CTRL | XUSB_DMA_READ_FROM_DPRAM |
459 (1 << (ep->epnumber +
460 XUSB_STATUS_EP_BUFF2_SHIFT)));
461 ep->buffer1ready = 1;
462 ep->curbufnum = 0;
463 } else {
464 /* None of the ping-pong buffers are ready currently */
465 return -EAGAIN;
466 }
467
468 return xudc_start_dma(ep, src, dst, length);
469}
470
471/**
472 * xudc_eptxrx - Transmits or receives data to or from an endpoint.
473 * @ep: pointer to the usb endpoint configuration structure.
474 * @req: pointer to the usb request structure.
475 * @bufferptr: pointer to buffer containing the data to be sent.
476 * @bufferlen: The number of data bytes to be sent.
477 *
478 * Return: 0 on success, -EAGAIN if no buffer is free.
479 *
480 * This function copies the transmit/receive data to/from the end point buffer
481 * and enables the buffer for transmission/reception.
482 */
483static int xudc_eptxrx(struct xusb_ep *ep, struct xusb_req *req,
484 u8 *bufferptr, u32 bufferlen)
485{
486 u32 *eprambase;
487 u32 bytestosend;
488 int rc = 0;
489 struct xusb_udc *udc = ep->udc;
490
491 bytestosend = bufferlen;
492 if (udc->dma_enabled) {
493 if (ep->is_in)
494 rc = xudc_dma_send(ep, req, bufferptr, bufferlen);
495 else
496 rc = xudc_dma_receive(ep, req, bufferptr, bufferlen);
497 return rc;
498 }
499 /* Put the transmit buffer into the correct ping-pong buffer.*/
500 if (!ep->curbufnum && !ep->buffer0ready) {
501 /* Get the Buffer address and copy the transmit data.*/
502 eprambase = (u32 __force *)(udc->addr + ep->rambase);
503 if (ep->is_in) {
504 memcpy(eprambase, bufferptr, bytestosend);
505 udc->write_fn(udc->addr, ep->offset +
506 XUSB_EP_BUF0COUNT_OFFSET, bufferlen);
507 } else {
508 memcpy(bufferptr, eprambase, bytestosend);
509 }
510 /*
511 * Enable the buffer for transmission.
512 */
513 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
514 1 << ep->epnumber);
515 ep->buffer0ready = 1;
516 ep->curbufnum = 1;
517 } else if (ep->curbufnum && !ep->buffer1ready) {
518 /* Get the Buffer address and copy the transmit data.*/
519 eprambase = (u32 __force *)(udc->addr + ep->rambase +
520 ep->ep_usb.maxpacket);
521 if (ep->is_in) {
522 memcpy(eprambase, bufferptr, bytestosend);
523 udc->write_fn(udc->addr, ep->offset +
524 XUSB_EP_BUF1COUNT_OFFSET, bufferlen);
525 } else {
526 memcpy(bufferptr, eprambase, bytestosend);
527 }
528 /*
529 * Enable the buffer for transmission.
530 */
531 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
532 1 << (ep->epnumber + XUSB_STATUS_EP_BUFF2_SHIFT));
533 ep->buffer1ready = 1;
534 ep->curbufnum = 0;
535 } else {
536 /* None of the ping-pong buffers are ready currently */
537 return -EAGAIN;
538 }
539 return rc;
540}
541
542/**
543 * xudc_done - Exeutes the endpoint data transfer completion tasks.
544 * @ep: pointer to the usb device endpoint structure.
545 * @req: pointer to the usb request structure.
546 * @status: Status of the data transfer.
547 *
548 * Deletes the message from the queue and updates data transfer completion
549 * status.
550 */
551static void xudc_done(struct xusb_ep *ep, struct xusb_req *req, int status)
552{
553 struct xusb_udc *udc = ep->udc;
554
555 list_del_init(&req->queue);
556
557 if (req->usb_req.status == -EINPROGRESS)
558 req->usb_req.status = status;
559 else
560 status = req->usb_req.status;
561
562 if (status && status != -ESHUTDOWN)
563 dev_dbg(udc->dev, "%s done %p, status %d\n",
564 ep->ep_usb.name, req, status);
565 /* unmap request if DMA is present*/
566 if (udc->dma_enabled && ep->epnumber && req->usb_req.length)
567 usb_gadget_unmap_request(&udc->gadget, &req->usb_req,
568 ep->is_in);
569
570 if (req->usb_req.complete) {
571 spin_unlock(&udc->lock);
572 req->usb_req.complete(&ep->ep_usb, &req->usb_req);
573 spin_lock(&udc->lock);
574 }
575}
576
577/**
578 * xudc_read_fifo - Reads the data from the given endpoint buffer.
579 * @ep: pointer to the usb device endpoint structure.
580 * @req: pointer to the usb request structure.
581 *
582 * Return: 0 if request is completed and -EAGAIN if not completed.
583 *
584 * Pulls OUT packet data from the endpoint buffer.
585 */
586static int xudc_read_fifo(struct xusb_ep *ep, struct xusb_req *req)
587{
588 u8 *buf;
589 u32 is_short, count, bufferspace;
590 u8 bufoffset;
591 u8 two_pkts = 0;
592 int ret;
593 int retval = -EAGAIN;
594 struct xusb_udc *udc = ep->udc;
595
596 if (ep->buffer0ready && ep->buffer1ready) {
597 dev_dbg(udc->dev, "Packet NOT ready!\n");
598 return retval;
599 }
600top:
601 if (ep->curbufnum)
602 bufoffset = XUSB_EP_BUF1COUNT_OFFSET;
603 else
604 bufoffset = XUSB_EP_BUF0COUNT_OFFSET;
605
606 count = udc->read_fn(udc->addr + ep->offset + bufoffset);
607
608 if (!ep->buffer0ready && !ep->buffer1ready)
609 two_pkts = 1;
610
611 buf = req->usb_req.buf + req->usb_req.actual;
612 prefetchw(buf);
613 bufferspace = req->usb_req.length - req->usb_req.actual;
614 is_short = count < ep->ep_usb.maxpacket;
615
616 if (unlikely(!bufferspace)) {
617 /*
618 * This happens when the driver's buffer
619 * is smaller than what the host sent.
620 * discard the extra data.
621 */
622 if (req->usb_req.status != -EOVERFLOW)
623 dev_dbg(udc->dev, "%s overflow %d\n",
624 ep->ep_usb.name, count);
625 req->usb_req.status = -EOVERFLOW;
626 xudc_done(ep, req, -EOVERFLOW);
627 return 0;
628 }
629
630 ret = xudc_eptxrx(ep, req, buf, count);
631 switch (ret) {
632 case 0:
633 req->usb_req.actual += min(count, bufferspace);
634 dev_dbg(udc->dev, "read %s, %d bytes%s req %p %d/%d\n",
635 ep->ep_usb.name, count, is_short ? "/S" : "", req,
636 req->usb_req.actual, req->usb_req.length);
637 bufferspace -= count;
638 /* Completion */
639 if ((req->usb_req.actual == req->usb_req.length) || is_short) {
640 if (udc->dma_enabled && req->usb_req.length)
641 dma_sync_single_for_cpu(udc->dev,
642 req->usb_req.dma,
643 req->usb_req.actual,
644 DMA_FROM_DEVICE);
645 xudc_done(ep, req, 0);
646 return 0;
647 }
648 if (two_pkts) {
649 two_pkts = 0;
650 goto top;
651 }
652 break;
653 case -EAGAIN:
654 dev_dbg(udc->dev, "receive busy\n");
655 break;
656 case -EINVAL:
657 case -ETIMEDOUT:
658 /* DMA error, dequeue the request */
659 xudc_done(ep, req, -ECONNRESET);
660 retval = 0;
661 break;
662 }
663
664 return retval;
665}
666
667/**
668 * xudc_write_fifo - Writes data into the given endpoint buffer.
669 * @ep: pointer to the usb device endpoint structure.
670 * @req: pointer to the usb request structure.
671 *
672 * Return: 0 if request is completed and -EAGAIN if not completed.
673 *
674 * Loads endpoint buffer for an IN packet.
675 */
676static int xudc_write_fifo(struct xusb_ep *ep, struct xusb_req *req)
677{
678 u32 max;
679 u32 length;
680 int ret;
681 int retval = -EAGAIN;
682 struct xusb_udc *udc = ep->udc;
683 int is_last, is_short = 0;
684 u8 *buf;
685
686 max = le16_to_cpu(ep->desc->wMaxPacketSize);
687 buf = req->usb_req.buf + req->usb_req.actual;
688 prefetch(buf);
689 length = req->usb_req.length - req->usb_req.actual;
690 length = min(length, max);
691
692 ret = xudc_eptxrx(ep, req, buf, length);
693 switch (ret) {
694 case 0:
695 req->usb_req.actual += length;
696 if (unlikely(length != max)) {
697 is_last = is_short = 1;
698 } else {
699 if (likely(req->usb_req.length !=
700 req->usb_req.actual) || req->usb_req.zero)
701 is_last = 0;
702 else
703 is_last = 1;
704 }
705 dev_dbg(udc->dev, "%s: wrote %s %d bytes%s%s %d left %p\n",
706 __func__, ep->ep_usb.name, length, is_last ? "/L" : "",
707 is_short ? "/S" : "",
708 req->usb_req.length - req->usb_req.actual, req);
709 /* completion */
710 if (is_last) {
711 xudc_done(ep, req, 0);
712 retval = 0;
713 }
714 break;
715 case -EAGAIN:
716 dev_dbg(udc->dev, "Send busy\n");
717 break;
718 case -EINVAL:
719 case -ETIMEDOUT:
720 /* DMA error, dequeue the request */
721 xudc_done(ep, req, -ECONNRESET);
722 retval = 0;
723 break;
724 }
725
726 return retval;
727}
728
729/**
730 * xudc_nuke - Cleans up the data transfer message list.
731 * @ep: pointer to the usb device endpoint structure.
732 * @status: Status of the data transfer.
733 */
734static void xudc_nuke(struct xusb_ep *ep, int status)
735{
736 struct xusb_req *req;
737
738 while (!list_empty(&ep->queue)) {
739 req = list_first_entry(&ep->queue, struct xusb_req, queue);
740 xudc_done(ep, req, status);
741 }
742}
743
744/**
745 * xudc_ep_set_halt - Stalls/unstalls the given endpoint.
746 * @_ep: pointer to the usb device endpoint structure.
747 * @value: value to indicate stall/unstall.
748 *
749 * Return: 0 for success and error value on failure
750 */
751static int xudc_ep_set_halt(struct usb_ep *_ep, int value)
752{
753 struct xusb_ep *ep = to_xusb_ep(_ep);
754 struct xusb_udc *udc;
755 unsigned long flags;
756 u32 epcfgreg;
757
758 if (!_ep || (!ep->desc && ep->epnumber)) {
759 pr_debug("%s: bad ep or descriptor\n", __func__);
760 return -EINVAL;
761 }
762 udc = ep->udc;
763
764 if (ep->is_in && (!list_empty(&ep->queue)) && value) {
765 dev_dbg(udc->dev, "requests pending can't halt\n");
766 return -EAGAIN;
767 }
768
769 if (ep->buffer0ready || ep->buffer1ready) {
770 dev_dbg(udc->dev, "HW buffers busy can't halt\n");
771 return -EAGAIN;
772 }
773
774 spin_lock_irqsave(&udc->lock, flags);
775
776 if (value) {
777 /* Stall the device.*/
778 epcfgreg = udc->read_fn(udc->addr + ep->offset);
779 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
780 udc->write_fn(udc->addr, ep->offset, epcfgreg);
781 } else {
782 /* Unstall the device.*/
783 epcfgreg = udc->read_fn(udc->addr + ep->offset);
784 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
785 udc->write_fn(udc->addr, ep->offset, epcfgreg);
786 if (ep->epnumber) {
787 /* Reset the toggle bit.*/
788 epcfgreg = udc->read_fn(ep->udc->addr + ep->offset);
789 epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
790 udc->write_fn(udc->addr, ep->offset, epcfgreg);
791 }
792 }
793
794 spin_unlock_irqrestore(&udc->lock, flags);
795 return 0;
796}
797
798/**
799 * xudc_ep_enable - Enables the given endpoint.
800 * @ep: pointer to the xusb endpoint structure.
801 * @desc: pointer to usb endpoint descriptor.
802 *
803 * Return: 0 for success and error value on failure
804 */
805static int __xudc_ep_enable(struct xusb_ep *ep,
806 const struct usb_endpoint_descriptor *desc)
807{
808 struct xusb_udc *udc = ep->udc;
809 u32 tmp;
810 u32 epcfg;
811 u32 ier;
812 u16 maxpacket;
813
814 ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
815 /* Bit 3...0:endpoint number */
816 ep->epnumber = (desc->bEndpointAddress & 0x0f);
817 ep->desc = desc;
818 ep->ep_usb.desc = desc;
819 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
820 ep->ep_usb.maxpacket = maxpacket = le16_to_cpu(desc->wMaxPacketSize);
821
822 switch (tmp) {
823 case USB_ENDPOINT_XFER_CONTROL:
824 dev_dbg(udc->dev, "only one control endpoint\n");
825 /* NON- ISO */
826 ep->is_iso = 0;
827 return -EINVAL;
828 case USB_ENDPOINT_XFER_INT:
829 /* NON- ISO */
830 ep->is_iso = 0;
831 if (maxpacket > 64) {
832 dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
833 return -EINVAL;
834 }
835 break;
836 case USB_ENDPOINT_XFER_BULK:
837 /* NON- ISO */
838 ep->is_iso = 0;
839 if (!(is_power_of_2(maxpacket) && maxpacket >= 8 &&
840 maxpacket <= 512)) {
841 dev_dbg(udc->dev, "bogus maxpacket %d\n", maxpacket);
842 return -EINVAL;
843 }
844 break;
845 case USB_ENDPOINT_XFER_ISOC:
846 /* ISO */
847 ep->is_iso = 1;
848 break;
849 }
850
851 ep->buffer0ready = 0;
852 ep->buffer1ready = 0;
853 ep->curbufnum = 0;
854 ep->rambase = rambase[ep->epnumber];
855 xudc_epconfig(ep, udc);
856
857 dev_dbg(udc->dev, "Enable Endpoint %d max pkt is %d\n",
858 ep->epnumber, maxpacket);
859
860 /* Enable the End point.*/
861 epcfg = udc->read_fn(udc->addr + ep->offset);
862 epcfg |= XUSB_EP_CFG_VALID_MASK;
863 udc->write_fn(udc->addr, ep->offset, epcfg);
864 if (ep->epnumber)
865 ep->rambase <<= 2;
866
867 /* Enable buffer completion interrupts for endpoint */
868 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
869 ier |= (XUSB_STATUS_INTR_BUFF_COMP_SHIFT_MASK << ep->epnumber);
870 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
871
872 /* for OUT endpoint set buffers ready to receive */
873 if (ep->epnumber && !ep->is_in) {
874 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
875 1 << ep->epnumber);
876 ep->buffer0ready = 1;
877 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET,
878 (1 << (ep->epnumber +
879 XUSB_STATUS_EP_BUFF2_SHIFT)));
880 ep->buffer1ready = 1;
881 }
882
883 return 0;
884}
885
886/**
887 * xudc_ep_enable - Enables the given endpoint.
888 * @_ep: pointer to the usb endpoint structure.
889 * @desc: pointer to usb endpoint descriptor.
890 *
891 * Return: 0 for success and error value on failure
892 */
893static int xudc_ep_enable(struct usb_ep *_ep,
894 const struct usb_endpoint_descriptor *desc)
895{
896 struct xusb_ep *ep;
897 struct xusb_udc *udc;
898 unsigned long flags;
899 int ret;
900
901 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
902 pr_debug("%s: bad ep or descriptor\n", __func__);
903 return -EINVAL;
904 }
905
906 ep = to_xusb_ep(_ep);
907 udc = ep->udc;
908
909 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
910 dev_dbg(udc->dev, "bogus device state\n");
911 return -ESHUTDOWN;
912 }
913
914 spin_lock_irqsave(&udc->lock, flags);
915 ret = __xudc_ep_enable(ep, desc);
916 spin_unlock_irqrestore(&udc->lock, flags);
917
918 return ret;
919}
920
921/**
922 * xudc_ep_disable - Disables the given endpoint.
923 * @_ep: pointer to the usb endpoint structure.
924 *
925 * Return: 0 for success and error value on failure
926 */
927static int xudc_ep_disable(struct usb_ep *_ep)
928{
929 struct xusb_ep *ep;
930 unsigned long flags;
931 u32 epcfg;
932 struct xusb_udc *udc;
933
934 if (!_ep) {
935 pr_debug("%s: invalid ep\n", __func__);
936 return -EINVAL;
937 }
938
939 ep = to_xusb_ep(_ep);
940 udc = ep->udc;
941
942 spin_lock_irqsave(&udc->lock, flags);
943
944 xudc_nuke(ep, -ESHUTDOWN);
945
946 /* Restore the endpoint's pristine config */
947 ep->desc = NULL;
948 ep->ep_usb.desc = NULL;
949
950 dev_dbg(udc->dev, "USB Ep %d disable\n ", ep->epnumber);
951 /* Disable the endpoint.*/
952 epcfg = udc->read_fn(udc->addr + ep->offset);
953 epcfg &= ~XUSB_EP_CFG_VALID_MASK;
954 udc->write_fn(udc->addr, ep->offset, epcfg);
955
956 spin_unlock_irqrestore(&udc->lock, flags);
957 return 0;
958}
959
960/**
961 * xudc_ep_alloc_request - Initializes the request queue.
962 * @_ep: pointer to the usb endpoint structure.
963 * @gfp_flags: Flags related to the request call.
964 *
965 * Return: pointer to request structure on success and a NULL on failure.
966 */
967static struct usb_request *xudc_ep_alloc_request(struct usb_ep *_ep,
968 gfp_t gfp_flags)
969{
970 struct xusb_ep *ep = to_xusb_ep(_ep);
971 struct xusb_udc *udc;
972 struct xusb_req *req;
973
974 udc = ep->udc;
975 req = kzalloc(sizeof(*req), gfp_flags);
976 if (!req) {
977 dev_err(udc->dev, "%s:not enough memory", __func__);
978 return NULL;
979 }
980
981 req->ep = ep;
982 INIT_LIST_HEAD(&req->queue);
983 return &req->usb_req;
984}
985
986/**
987 * xudc_free_request - Releases the request from queue.
988 * @_ep: pointer to the usb device endpoint structure.
989 * @_req: pointer to the usb request structure.
990 */
991static void xudc_free_request(struct usb_ep *_ep, struct usb_request *_req)
992{
993 struct xusb_req *req = to_xusb_req(_req);
994
995 kfree(req);
996}
997
998/**
999 * xudc_ep0_queue - Adds the request to endpoint 0 queue.
1000 * @ep0: pointer to the xusb endpoint 0 structure.
1001 * @req: pointer to the xusb request structure.
1002 *
1003 * Return: 0 for success and error value on failure
1004 */
1005static int __xudc_ep0_queue(struct xusb_ep *ep0, struct xusb_req *req)
1006{
1007 struct xusb_udc *udc = ep0->udc;
1008 u32 length;
1009 u8 *corebuf;
1010
1011 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1012 dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
1013 return -EINVAL;
1014 }
1015 if (!list_empty(&ep0->queue)) {
1016 dev_dbg(udc->dev, "%s:ep0 busy\n", __func__);
1017 return -EBUSY;
1018 }
1019
1020 req->usb_req.status = -EINPROGRESS;
1021 req->usb_req.actual = 0;
1022
1023 list_add_tail(&req->queue, &ep0->queue);
1024
1025 if (udc->setup.bRequestType & USB_DIR_IN) {
1026 prefetch(req->usb_req.buf);
1027 length = req->usb_req.length;
1028 corebuf = (void __force *) ((ep0->rambase << 2) +
1029 udc->addr);
1030 length = req->usb_req.actual = min_t(u32, length,
1031 EP0_MAX_PACKET);
1032 memcpy(corebuf, req->usb_req.buf, length);
1033 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, length);
1034 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1035 } else {
1036 if (udc->setup.wLength) {
1037 /* Enable EP0 buffer to receive data */
1038 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
1039 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1040 } else {
1041 xudc_wrstatus(udc);
1042 }
1043 }
1044
1045 return 0;
1046}
1047
1048/**
1049 * xudc_ep0_queue - Adds the request to endpoint 0 queue.
1050 * @_ep: pointer to the usb endpoint 0 structure.
1051 * @_req: pointer to the usb request structure.
1052 * @gfp_flags: Flags related to the request call.
1053 *
1054 * Return: 0 for success and error value on failure
1055 */
1056static int xudc_ep0_queue(struct usb_ep *_ep, struct usb_request *_req,
1057 gfp_t gfp_flags)
1058{
1059 struct xusb_req *req = to_xusb_req(_req);
1060 struct xusb_ep *ep0 = to_xusb_ep(_ep);
1061 struct xusb_udc *udc = ep0->udc;
1062 unsigned long flags;
1063 int ret;
1064
1065 spin_lock_irqsave(&udc->lock, flags);
1066 ret = __xudc_ep0_queue(ep0, req);
1067 spin_unlock_irqrestore(&udc->lock, flags);
1068
1069 return ret;
1070}
1071
1072/**
1073 * xudc_ep_queue - Adds the request to endpoint queue.
1074 * @_ep: pointer to the usb endpoint structure.
1075 * @_req: pointer to the usb request structure.
1076 * @gfp_flags: Flags related to the request call.
1077 *
1078 * Return: 0 for success and error value on failure
1079 */
1080static int xudc_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1081 gfp_t gfp_flags)
1082{
1083 struct xusb_req *req = to_xusb_req(_req);
1084 struct xusb_ep *ep = to_xusb_ep(_ep);
1085 struct xusb_udc *udc = ep->udc;
1086 int ret;
1087 unsigned long flags;
1088
1089 if (!ep->desc) {
1090 dev_dbg(udc->dev, "%s:queing request to disabled %s\n",
1091 __func__, ep->name);
1092 return -ESHUTDOWN;
1093 }
1094
1095 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
1096 dev_dbg(udc->dev, "%s, bogus device state\n", __func__);
1097 return -EINVAL;
1098 }
1099
1100 spin_lock_irqsave(&udc->lock, flags);
1101
1102 _req->status = -EINPROGRESS;
1103 _req->actual = 0;
1104
1105 if (udc->dma_enabled) {
1106 ret = usb_gadget_map_request(&udc->gadget, &req->usb_req,
1107 ep->is_in);
1108 if (ret) {
1109 dev_dbg(udc->dev, "gadget_map failed ep%d\n",
1110 ep->epnumber);
1111 spin_unlock_irqrestore(&udc->lock, flags);
1112 return -EAGAIN;
1113 }
1114 }
1115
1116 if (list_empty(&ep->queue)) {
1117 if (ep->is_in) {
1118 dev_dbg(udc->dev, "xudc_write_fifo from ep_queue\n");
1119 if (!xudc_write_fifo(ep, req))
1120 req = NULL;
1121 } else {
1122 dev_dbg(udc->dev, "xudc_read_fifo from ep_queue\n");
1123 if (!xudc_read_fifo(ep, req))
1124 req = NULL;
1125 }
1126 }
1127
1128 if (req != NULL)
1129 list_add_tail(&req->queue, &ep->queue);
1130
1131 spin_unlock_irqrestore(&udc->lock, flags);
1132 return 0;
1133}
1134
1135/**
1136 * xudc_ep_dequeue - Removes the request from the queue.
1137 * @_ep: pointer to the usb device endpoint structure.
1138 * @_req: pointer to the usb request structure.
1139 *
1140 * Return: 0 for success and error value on failure
1141 */
1142static int xudc_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1143{
1144 struct xusb_ep *ep = to_xusb_ep(_ep);
1145 struct xusb_req *req = to_xusb_req(_req);
1146 struct xusb_udc *udc = ep->udc;
1147 unsigned long flags;
1148
1149 spin_lock_irqsave(&udc->lock, flags);
1150 /* Make sure it's actually queued on this endpoint */
1151 list_for_each_entry(req, &ep->queue, queue) {
1152 if (&req->usb_req == _req)
1153 break;
1154 }
1155 if (&req->usb_req != _req) {
1156 spin_unlock_irqrestore(&ep->udc->lock, flags);
1157 return -EINVAL;
1158 }
1159 xudc_done(ep, req, -ECONNRESET);
1160 spin_unlock_irqrestore(&udc->lock, flags);
1161
1162 return 0;
1163}
1164
1165/**
1166 * xudc_ep0_enable - Enables the given endpoint.
1167 * @ep: pointer to the usb endpoint structure.
1168 * @desc: pointer to usb endpoint descriptor.
1169 *
1170 * Return: error always.
1171 *
1172 * endpoint 0 enable should not be called by gadget layer.
1173 */
1174static int xudc_ep0_enable(struct usb_ep *ep,
1175 const struct usb_endpoint_descriptor *desc)
1176{
1177 return -EINVAL;
1178}
1179
1180/**
1181 * xudc_ep0_disable - Disables the given endpoint.
1182 * @ep: pointer to the usb endpoint structure.
1183 *
1184 * Return: error always.
1185 *
1186 * endpoint 0 disable should not be called by gadget layer.
1187 */
1188static int xudc_ep0_disable(struct usb_ep *ep)
1189{
1190 return -EINVAL;
1191}
1192
1193static const struct usb_ep_ops xusb_ep0_ops = {
1194 .enable = xudc_ep0_enable,
1195 .disable = xudc_ep0_disable,
1196 .alloc_request = xudc_ep_alloc_request,
1197 .free_request = xudc_free_request,
1198 .queue = xudc_ep0_queue,
1199 .dequeue = xudc_ep_dequeue,
1200 .set_halt = xudc_ep_set_halt,
1201};
1202
1203static const struct usb_ep_ops xusb_ep_ops = {
1204 .enable = xudc_ep_enable,
1205 .disable = xudc_ep_disable,
1206 .alloc_request = xudc_ep_alloc_request,
1207 .free_request = xudc_free_request,
1208 .queue = xudc_ep_queue,
1209 .dequeue = xudc_ep_dequeue,
1210 .set_halt = xudc_ep_set_halt,
1211};
1212
1213/**
1214 * xudc_get_frame - Reads the current usb frame number.
1215 * @gadget: pointer to the usb gadget structure.
1216 *
1217 * Return: current frame number for success and error value on failure.
1218 */
1219static int xudc_get_frame(struct usb_gadget *gadget)
1220{
1221 struct xusb_udc *udc;
1222 int frame;
1223
1224 if (!gadget)
1225 return -ENODEV;
1226
1227 udc = to_udc(gadget);
1228 frame = udc->read_fn(udc->addr + XUSB_FRAMENUM_OFFSET);
1229 return frame;
1230}
1231
1232/**
1233 * xudc_wakeup - Send remote wakeup signal to host
1234 * @gadget: pointer to the usb gadget structure.
1235 *
1236 * Return: 0 on success and error on failure
1237 */
1238static int xudc_wakeup(struct usb_gadget *gadget)
1239{
1240 struct xusb_udc *udc = to_udc(gadget);
1241 u32 crtlreg;
1242 int status = -EINVAL;
1243 unsigned long flags;
1244
1245 spin_lock_irqsave(&udc->lock, flags);
1246
1247 /* Remote wake up not enabled by host */
1248 if (!udc->remote_wkp)
1249 goto done;
1250
1251 crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
1252 crtlreg |= XUSB_CONTROL_USB_RMTWAKE_MASK;
1253 /* set remote wake up bit */
1254 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1255 /*
1256 * wait for a while and reset remote wake up bit since this bit
1257 * is not cleared by HW after sending remote wakeup to host.
1258 */
1259 mdelay(2);
1260
1261 crtlreg &= ~XUSB_CONTROL_USB_RMTWAKE_MASK;
1262 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1263 status = 0;
1264done:
1265 spin_unlock_irqrestore(&udc->lock, flags);
1266 return status;
1267}
1268
1269/**
1270 * xudc_pullup - start/stop USB traffic
1271 * @gadget: pointer to the usb gadget structure.
1272 * @is_on: flag to start or stop
1273 *
1274 * Return: 0 always
1275 *
1276 * This function starts/stops SIE engine of IP based on is_on.
1277 */
1278static int xudc_pullup(struct usb_gadget *gadget, int is_on)
1279{
1280 struct xusb_udc *udc = to_udc(gadget);
1281 unsigned long flags;
1282 u32 crtlreg;
1283
1284 spin_lock_irqsave(&udc->lock, flags);
1285
1286 crtlreg = udc->read_fn(udc->addr + XUSB_CONTROL_OFFSET);
1287 if (is_on)
1288 crtlreg |= XUSB_CONTROL_USB_READY_MASK;
1289 else
1290 crtlreg &= ~XUSB_CONTROL_USB_READY_MASK;
1291
1292 udc->write_fn(udc->addr, XUSB_CONTROL_OFFSET, crtlreg);
1293
1294 spin_unlock_irqrestore(&udc->lock, flags);
1295
1296 return 0;
1297}
1298
1299/**
1300 * xudc_eps_init - initialize endpoints.
1301 * @udc: pointer to the usb device controller structure.
1302 */
1303static void xudc_eps_init(struct xusb_udc *udc)
1304{
1305 u32 ep_number;
1306
1307 INIT_LIST_HEAD(&udc->gadget.ep_list);
1308
1309 for (ep_number = 0; ep_number < XUSB_MAX_ENDPOINTS; ep_number++) {
1310 struct xusb_ep *ep = &udc->ep[ep_number];
1311
1312 if (ep_number) {
1313 list_add_tail(&ep->ep_usb.ep_list,
1314 &udc->gadget.ep_list);
1315 usb_ep_set_maxpacket_limit(&ep->ep_usb,
1316 (unsigned short) ~0);
1317 snprintf(ep->name, EPNAME_SIZE, "ep%d", ep_number);
1318 ep->ep_usb.name = ep->name;
1319 ep->ep_usb.ops = &xusb_ep_ops;
1320 } else {
1321 ep->ep_usb.name = ep0name;
1322 usb_ep_set_maxpacket_limit(&ep->ep_usb, EP0_MAX_PACKET);
1323 ep->ep_usb.ops = &xusb_ep0_ops;
1324 }
1325
1326 ep->udc = udc;
1327 ep->epnumber = ep_number;
1328 ep->desc = NULL;
1329 /*
1330 * The configuration register address offset between
1331 * each endpoint is 0x10.
1332 */
1333 ep->offset = XUSB_EP0_CONFIG_OFFSET + (ep_number * 0x10);
1334 ep->is_in = 0;
1335 ep->is_iso = 0;
1336 ep->maxpacket = 0;
1337 xudc_epconfig(ep, udc);
1338
1339 /* Initialize one queue per endpoint */
1340 INIT_LIST_HEAD(&ep->queue);
1341 }
1342}
1343
1344/**
1345 * xudc_stop_activity - Stops any further activity on the device.
1346 * @udc: pointer to the usb device controller structure.
1347 */
1348static void xudc_stop_activity(struct xusb_udc *udc)
1349{
1350 int i;
1351 struct xusb_ep *ep;
1352
1353 for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
1354 ep = &udc->ep[i];
1355 xudc_nuke(ep, -ESHUTDOWN);
1356 }
1357}
1358
1359/**
1360 * xudc_start - Starts the device.
1361 * @gadget: pointer to the usb gadget structure
1362 * @driver: pointer to gadget driver structure
1363 *
1364 * Return: zero on success and error on failure
1365 */
1366static int xudc_start(struct usb_gadget *gadget,
1367 struct usb_gadget_driver *driver)
1368{
1369 struct xusb_udc *udc = to_udc(gadget);
1370 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
1371 const struct usb_endpoint_descriptor *desc = &config_bulk_out_desc;
1372 unsigned long flags;
1373 int ret = 0;
1374
1375 spin_lock_irqsave(&udc->lock, flags);
1376
1377 if (udc->driver) {
1378 dev_err(udc->dev, "%s is already bound to %s\n",
1379 udc->gadget.name, udc->driver->driver.name);
1380 ret = -EBUSY;
1381 goto err;
1382 }
1383
1384 /* hook up the driver */
1385 udc->driver = driver;
1386 udc->gadget.speed = driver->max_speed;
1387
1388 /* Enable the control endpoint. */
1389 ret = __xudc_ep_enable(ep0, desc);
1390
1391 /* Set device address and remote wakeup to 0 */
1392 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1393 udc->remote_wkp = 0;
1394err:
1395 spin_unlock_irqrestore(&udc->lock, flags);
1396 return ret;
1397}
1398
1399/**
1400 * xudc_stop - stops the device.
1401 * @gadget: pointer to the usb gadget structure
1402 * @driver: pointer to usb gadget driver structure
1403 *
1404 * Return: zero always
1405 */
1406static int xudc_stop(struct usb_gadget *gadget,
1407 struct usb_gadget_driver *driver)
1408{
1409 struct xusb_udc *udc = to_udc(gadget);
1410 unsigned long flags;
1411
1412 spin_lock_irqsave(&udc->lock, flags);
1413
1414 udc->gadget.speed = USB_SPEED_UNKNOWN;
1415 udc->driver = NULL;
1416
1417 /* Set device address and remote wakeup to 0 */
1418 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1419 udc->remote_wkp = 0;
1420
1421 xudc_stop_activity(udc);
1422
1423 spin_unlock_irqrestore(&udc->lock, flags);
1424
1425 return 0;
1426}
1427
1428static const struct usb_gadget_ops xusb_udc_ops = {
1429 .get_frame = xudc_get_frame,
1430 .wakeup = xudc_wakeup,
1431 .pullup = xudc_pullup,
1432 .udc_start = xudc_start,
1433 .udc_stop = xudc_stop,
1434};
1435
1436/**
1437 * xudc_clear_stall_all_ep - clears stall of every endpoint.
1438 * @udc: pointer to the udc structure.
1439 */
1440static void xudc_clear_stall_all_ep(struct xusb_udc *udc)
1441{
1442 struct xusb_ep *ep;
1443 u32 epcfgreg;
1444 int i;
1445
1446 for (i = 0; i < XUSB_MAX_ENDPOINTS; i++) {
1447 ep = &udc->ep[i];
1448 epcfgreg = udc->read_fn(udc->addr + ep->offset);
1449 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
1450 udc->write_fn(udc->addr, ep->offset, epcfgreg);
1451 if (ep->epnumber) {
1452 /* Reset the toggle bit.*/
1453 epcfgreg = udc->read_fn(udc->addr + ep->offset);
1454 epcfgreg &= ~XUSB_EP_CFG_DATA_TOGGLE_MASK;
1455 udc->write_fn(udc->addr, ep->offset, epcfgreg);
1456 }
1457 }
1458}
1459
1460/**
1461 * xudc_startup_handler - The usb device controller interrupt handler.
1462 * @udc: pointer to the udc structure.
1463 * @intrstatus: The mask value containing the interrupt sources.
1464 *
1465 * This function handles the RESET,SUSPEND,RESUME and DISCONNECT interrupts.
1466 */
1467static void xudc_startup_handler(struct xusb_udc *udc, u32 intrstatus)
1468{
1469 u32 intrreg;
1470
1471 if (intrstatus & XUSB_STATUS_RESET_MASK) {
1472
1473 dev_dbg(udc->dev, "Reset\n");
1474
1475 if (intrstatus & XUSB_STATUS_HIGH_SPEED_MASK)
1476 udc->gadget.speed = USB_SPEED_HIGH;
1477 else
1478 udc->gadget.speed = USB_SPEED_FULL;
1479
1480 xudc_stop_activity(udc);
1481 xudc_clear_stall_all_ep(udc);
1482 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
1483
1484 /* Set device address and remote wakeup to 0 */
1485 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
1486 udc->remote_wkp = 0;
1487
1488 /* Enable the suspend, resume and disconnect */
1489 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1490 intrreg |= XUSB_STATUS_SUSPEND_MASK | XUSB_STATUS_RESUME_MASK |
1491 XUSB_STATUS_DISCONNECT_MASK;
1492 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1493 }
1494 if (intrstatus & XUSB_STATUS_SUSPEND_MASK) {
1495
1496 dev_dbg(udc->dev, "Suspend\n");
1497
1498 /* Enable the reset, resume and disconnect */
1499 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1500 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
1501 XUSB_STATUS_DISCONNECT_MASK;
1502 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1503
1504 udc->usb_state = USB_STATE_SUSPENDED;
1505
1506 if (udc->driver->suspend) {
1507 spin_unlock(&udc->lock);
1508 udc->driver->suspend(&udc->gadget);
1509 spin_lock(&udc->lock);
1510 }
1511 }
1512 if (intrstatus & XUSB_STATUS_RESUME_MASK) {
1513 bool condition = (udc->usb_state != USB_STATE_SUSPENDED);
1514
1515 dev_WARN_ONCE(udc->dev, condition,
1516 "Resume IRQ while not suspended\n");
1517
1518 dev_dbg(udc->dev, "Resume\n");
1519
1520 /* Enable the reset, suspend and disconnect */
1521 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1522 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_SUSPEND_MASK |
1523 XUSB_STATUS_DISCONNECT_MASK;
1524 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1525
1526 udc->usb_state = 0;
1527
1528 if (udc->driver->resume) {
1529 spin_unlock(&udc->lock);
1530 udc->driver->resume(&udc->gadget);
1531 spin_lock(&udc->lock);
1532 }
1533 }
1534 if (intrstatus & XUSB_STATUS_DISCONNECT_MASK) {
1535
1536 dev_dbg(udc->dev, "Disconnect\n");
1537
1538 /* Enable the reset, resume and suspend */
1539 intrreg = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1540 intrreg |= XUSB_STATUS_RESET_MASK | XUSB_STATUS_RESUME_MASK |
1541 XUSB_STATUS_SUSPEND_MASK;
1542 udc->write_fn(udc->addr, XUSB_IER_OFFSET, intrreg);
1543
1544 if (udc->driver && udc->driver->disconnect) {
1545 spin_unlock(&udc->lock);
1546 udc->driver->disconnect(&udc->gadget);
1547 spin_lock(&udc->lock);
1548 }
1549 }
1550}
1551
1552/**
1553 * xudc_ep0_stall - Stall endpoint zero.
1554 * @udc: pointer to the udc structure.
1555 *
1556 * This function stalls endpoint zero.
1557 */
1558static void xudc_ep0_stall(struct xusb_udc *udc)
1559{
1560 u32 epcfgreg;
1561 struct xusb_ep *ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO];
1562
1563 epcfgreg = udc->read_fn(udc->addr + ep0->offset);
1564 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
1565 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
1566}
1567
1568/**
1569 * xudc_setaddress - executes SET_ADDRESS command
1570 * @udc: pointer to the udc structure.
1571 *
1572 * This function executes USB SET_ADDRESS command
1573 */
1574static void xudc_setaddress(struct xusb_udc *udc)
1575{
1576 struct xusb_ep *ep0 = &udc->ep[0];
1577 struct xusb_req *req = udc->req;
1578 int ret;
1579
1580 req->usb_req.length = 0;
1581 ret = __xudc_ep0_queue(ep0, req);
1582 if (ret == 0)
1583 return;
1584
1585 dev_err(udc->dev, "Can't respond to SET ADDRESS request\n");
1586 xudc_ep0_stall(udc);
1587}
1588
1589/**
1590 * xudc_getstatus - executes GET_STATUS command
1591 * @udc: pointer to the udc structure.
1592 *
1593 * This function executes USB GET_STATUS command
1594 */
1595static void xudc_getstatus(struct xusb_udc *udc)
1596{
1597 struct xusb_ep *ep0 = &udc->ep[0];
1598 struct xusb_req *req = udc->req;
1599 struct xusb_ep *target_ep;
1600 u16 status = 0;
1601 u32 epcfgreg;
1602 int epnum;
1603 u32 halt;
1604 int ret;
1605
1606 switch (udc->setup.bRequestType & USB_RECIP_MASK) {
1607 case USB_RECIP_DEVICE:
1608 /* Get device status */
1609 status = 1 << USB_DEVICE_SELF_POWERED;
1610 if (udc->remote_wkp)
1611 status |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1612 break;
1613 case USB_RECIP_INTERFACE:
1614 break;
1615 case USB_RECIP_ENDPOINT:
1616 epnum = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
1617 target_ep = &udc->ep[epnum];
1618 epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
1619 halt = epcfgreg & XUSB_EP_CFG_STALL_MASK;
1620 if (udc->setup.wIndex & USB_DIR_IN) {
1621 if (!target_ep->is_in)
1622 goto stall;
1623 } else {
1624 if (target_ep->is_in)
1625 goto stall;
1626 }
1627 if (halt)
1628 status = 1 << USB_ENDPOINT_HALT;
1629 break;
1630 default:
1631 goto stall;
1632 }
1633
1634 req->usb_req.length = 2;
1635 *(u16 *)req->usb_req.buf = cpu_to_le16(status);
1636 ret = __xudc_ep0_queue(ep0, req);
1637 if (ret == 0)
1638 return;
1639stall:
1640 dev_err(udc->dev, "Can't respond to getstatus request\n");
1641 xudc_ep0_stall(udc);
1642}
1643
1644/**
1645 * xudc_set_clear_feature - Executes the set feature and clear feature commands.
1646 * @udc: pointer to the usb device controller structure.
1647 *
1648 * Processes the SET_FEATURE and CLEAR_FEATURE commands.
1649 */
1650static void xudc_set_clear_feature(struct xusb_udc *udc)
1651{
1652 struct xusb_ep *ep0 = &udc->ep[0];
1653 struct xusb_req *req = udc->req;
1654 struct xusb_ep *target_ep;
1655 u8 endpoint;
1656 u8 outinbit;
1657 u32 epcfgreg;
1658 int flag = (udc->setup.bRequest == USB_REQ_SET_FEATURE ? 1 : 0);
1659 int ret;
1660
1661 switch (udc->setup.bRequestType) {
1662 case USB_RECIP_DEVICE:
1663 switch (udc->setup.wValue) {
1664 case USB_DEVICE_TEST_MODE:
1665 /*
1666 * The Test Mode will be executed
1667 * after the status phase.
1668 */
1669 break;
1670 case USB_DEVICE_REMOTE_WAKEUP:
1671 if (flag)
1672 udc->remote_wkp = 1;
1673 else
1674 udc->remote_wkp = 0;
1675 break;
1676 default:
1677 xudc_ep0_stall(udc);
1678 break;
1679 }
1680 break;
1681 case USB_RECIP_ENDPOINT:
1682 if (!udc->setup.wValue) {
1683 endpoint = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
1684 target_ep = &udc->ep[endpoint];
1685 outinbit = udc->setup.wIndex & USB_ENDPOINT_DIR_MASK;
1686 outinbit = outinbit >> 7;
1687
1688 /* Make sure direction matches.*/
1689 if (outinbit != target_ep->is_in) {
1690 xudc_ep0_stall(udc);
1691 return;
1692 }
1693 epcfgreg = udc->read_fn(udc->addr + target_ep->offset);
1694 if (!endpoint) {
1695 /* Clear the stall.*/
1696 epcfgreg &= ~XUSB_EP_CFG_STALL_MASK;
1697 udc->write_fn(udc->addr,
1698 target_ep->offset, epcfgreg);
1699 } else {
1700 if (flag) {
1701 epcfgreg |= XUSB_EP_CFG_STALL_MASK;
1702 udc->write_fn(udc->addr,
1703 target_ep->offset,
1704 epcfgreg);
1705 } else {
1706 /* Unstall the endpoint.*/
1707 epcfgreg &= ~(XUSB_EP_CFG_STALL_MASK |
1708 XUSB_EP_CFG_DATA_TOGGLE_MASK);
1709 udc->write_fn(udc->addr,
1710 target_ep->offset,
1711 epcfgreg);
1712 }
1713 }
1714 }
1715 break;
1716 default:
1717 xudc_ep0_stall(udc);
1718 return;
1719 }
1720
1721 req->usb_req.length = 0;
1722 ret = __xudc_ep0_queue(ep0, req);
1723 if (ret == 0)
1724 return;
1725
1726 dev_err(udc->dev, "Can't respond to SET/CLEAR FEATURE\n");
1727 xudc_ep0_stall(udc);
1728}
1729
1730/**
1731 * xudc_handle_setup - Processes the setup packet.
1732 * @udc: pointer to the usb device controller structure.
1733 *
1734 * Process setup packet and delegate to gadget layer.
1735 */
1736static void xudc_handle_setup(struct xusb_udc *udc)
1737{
1738 struct xusb_ep *ep0 = &udc->ep[0];
1739 struct usb_ctrlrequest setup;
1740 u32 *ep0rambase;
1741
1742 /* Load up the chapter 9 command buffer.*/
1743 ep0rambase = (u32 __force *) (udc->addr + XUSB_SETUP_PKT_ADDR_OFFSET);
1744 memcpy(&setup, ep0rambase, 8);
1745
1746 udc->setup = setup;
1747 udc->setup.wValue = cpu_to_le16(setup.wValue);
1748 udc->setup.wIndex = cpu_to_le16(setup.wIndex);
1749 udc->setup.wLength = cpu_to_le16(setup.wLength);
1750
1751 /* Clear previous requests */
1752 xudc_nuke(ep0, -ECONNRESET);
1753
1754 if (udc->setup.bRequestType & USB_DIR_IN) {
1755 /* Execute the get command.*/
1756 udc->setupseqrx = STATUS_PHASE;
1757 udc->setupseqtx = DATA_PHASE;
1758 } else {
1759 /* Execute the put command.*/
1760 udc->setupseqrx = DATA_PHASE;
1761 udc->setupseqtx = STATUS_PHASE;
1762 }
1763
1764 switch (udc->setup.bRequest) {
1765 case USB_REQ_GET_STATUS:
1766 /* Data+Status phase form udc */
1767 if ((udc->setup.bRequestType &
1768 (USB_DIR_IN | USB_TYPE_MASK)) !=
1769 (USB_DIR_IN | USB_TYPE_STANDARD))
1770 break;
1771 xudc_getstatus(udc);
1772 return;
1773 case USB_REQ_SET_ADDRESS:
1774 /* Status phase from udc */
1775 if (udc->setup.bRequestType != (USB_DIR_OUT |
1776 USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1777 break;
1778 xudc_setaddress(udc);
1779 return;
1780 case USB_REQ_CLEAR_FEATURE:
1781 case USB_REQ_SET_FEATURE:
1782 /* Requests with no data phase, status phase from udc */
1783 if ((udc->setup.bRequestType & USB_TYPE_MASK)
1784 != USB_TYPE_STANDARD)
1785 break;
1786 xudc_set_clear_feature(udc);
1787 return;
1788 default:
1789 break;
1790 }
1791
1792 spin_unlock(&udc->lock);
1793 if (udc->driver->setup(&udc->gadget, &setup) < 0)
1794 xudc_ep0_stall(udc);
1795 spin_lock(&udc->lock);
1796}
1797
1798/**
1799 * xudc_ep0_out - Processes the endpoint 0 OUT token.
1800 * @udc: pointer to the usb device controller structure.
1801 */
1802static void xudc_ep0_out(struct xusb_udc *udc)
1803{
1804 struct xusb_ep *ep0 = &udc->ep[0];
1805 struct xusb_req *req;
1806 u8 *ep0rambase;
1807 unsigned int bytes_to_rx;
1808 void *buffer;
1809
1810 req = list_first_entry(&ep0->queue, struct xusb_req, queue);
1811
1812 switch (udc->setupseqrx) {
1813 case STATUS_PHASE:
1814 /*
1815 * This resets both state machines for the next
1816 * Setup packet.
1817 */
1818 udc->setupseqrx = SETUP_PHASE;
1819 udc->setupseqtx = SETUP_PHASE;
1820 req->usb_req.actual = req->usb_req.length;
1821 xudc_done(ep0, req, 0);
1822 break;
1823 case DATA_PHASE:
1824 bytes_to_rx = udc->read_fn(udc->addr +
1825 XUSB_EP_BUF0COUNT_OFFSET);
1826 /* Copy the data to be received from the DPRAM. */
1827 ep0rambase = (u8 __force *) (udc->addr +
1828 (ep0->rambase << 2));
1829 buffer = req->usb_req.buf + req->usb_req.actual;
1830 req->usb_req.actual = req->usb_req.actual + bytes_to_rx;
1831 memcpy(buffer, ep0rambase, bytes_to_rx);
1832
1833 if (req->usb_req.length == req->usb_req.actual) {
1834 /* Data transfer completed get ready for Status stage */
1835 xudc_wrstatus(udc);
1836 } else {
1837 /* Enable EP0 buffer to receive data */
1838 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, 0);
1839 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1840 }
1841 break;
1842 default:
1843 break;
1844 }
1845}
1846
1847/**
1848 * xudc_ep0_in - Processes the endpoint 0 IN token.
1849 * @udc: pointer to the usb device controller structure.
1850 */
1851static void xudc_ep0_in(struct xusb_udc *udc)
1852{
1853 struct xusb_ep *ep0 = &udc->ep[0];
1854 struct xusb_req *req;
1855 unsigned int bytes_to_tx;
1856 void *buffer;
1857 u32 epcfgreg;
1858 u16 count = 0;
1859 u16 length;
1860 u8 *ep0rambase;
1861 u8 test_mode = udc->setup.wIndex >> 8;
1862
1863 req = list_first_entry(&ep0->queue, struct xusb_req, queue);
1864 bytes_to_tx = req->usb_req.length - req->usb_req.actual;
1865
1866 switch (udc->setupseqtx) {
1867 case STATUS_PHASE:
1868 switch (udc->setup.bRequest) {
1869 case USB_REQ_SET_ADDRESS:
1870 /* Set the address of the device.*/
1871 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET,
1872 udc->setup.wValue);
1873 break;
1874 case USB_REQ_SET_FEATURE:
1875 if (udc->setup.bRequestType ==
1876 USB_RECIP_DEVICE) {
1877 if (udc->setup.wValue ==
1878 USB_DEVICE_TEST_MODE)
1879 udc->write_fn(udc->addr,
1880 XUSB_TESTMODE_OFFSET,
1881 test_mode);
1882 }
1883 break;
1884 }
1885 req->usb_req.actual = req->usb_req.length;
1886 xudc_done(ep0, req, 0);
1887 break;
1888 case DATA_PHASE:
1889 if (!bytes_to_tx) {
1890 /*
1891 * We're done with data transfer, next
1892 * will be zero length OUT with data toggle of
1893 * 1. Setup data_toggle.
1894 */
1895 epcfgreg = udc->read_fn(udc->addr + ep0->offset);
1896 epcfgreg |= XUSB_EP_CFG_DATA_TOGGLE_MASK;
1897 udc->write_fn(udc->addr, ep0->offset, epcfgreg);
1898 udc->setupseqtx = STATUS_PHASE;
1899 } else {
1900 length = count = min_t(u32, bytes_to_tx,
1901 EP0_MAX_PACKET);
1902 /* Copy the data to be transmitted into the DPRAM. */
1903 ep0rambase = (u8 __force *) (udc->addr +
1904 (ep0->rambase << 2));
1905 buffer = req->usb_req.buf + req->usb_req.actual;
1906 req->usb_req.actual = req->usb_req.actual + length;
1907 memcpy(ep0rambase, buffer, length);
1908 }
1909 udc->write_fn(udc->addr, XUSB_EP_BUF0COUNT_OFFSET, count);
1910 udc->write_fn(udc->addr, XUSB_BUFFREADY_OFFSET, 1);
1911 break;
1912 default:
1913 break;
1914 }
1915}
1916
1917/**
1918 * xudc_ctrl_ep_handler - Endpoint 0 interrupt handler.
1919 * @udc: pointer to the udc structure.
1920 * @intrstatus: It's the mask value for the interrupt sources on endpoint 0.
1921 *
1922 * Processes the commands received during enumeration phase.
1923 */
1924static void xudc_ctrl_ep_handler(struct xusb_udc *udc, u32 intrstatus)
1925{
1926
1927 if (intrstatus & XUSB_STATUS_SETUP_PACKET_MASK) {
1928 xudc_handle_setup(udc);
1929 } else {
1930 if (intrstatus & XUSB_STATUS_FIFO_BUFF_RDY_MASK)
1931 xudc_ep0_out(udc);
1932 else if (intrstatus & XUSB_STATUS_FIFO_BUFF_FREE_MASK)
1933 xudc_ep0_in(udc);
1934 }
1935}
1936
1937/**
1938 * xudc_nonctrl_ep_handler - Non control endpoint interrupt handler.
1939 * @udc: pointer to the udc structure.
1940 * @epnum: End point number for which the interrupt is to be processed
1941 * @intrstatus: mask value for interrupt sources of endpoints other
1942 * than endpoint 0.
1943 *
1944 * Processes the buffer completion interrupts.
1945 */
1946static void xudc_nonctrl_ep_handler(struct xusb_udc *udc, u8 epnum,
1947 u32 intrstatus)
1948{
1949
1950 struct xusb_req *req;
1951 struct xusb_ep *ep;
1952
1953 ep = &udc->ep[epnum];
1954 /* Process the End point interrupts.*/
1955 if (intrstatus & (XUSB_STATUS_EP0_BUFF1_COMP_MASK << epnum))
1956 ep->buffer0ready = 0;
1957 if (intrstatus & (XUSB_STATUS_EP0_BUFF2_COMP_MASK << epnum))
1958 ep->buffer1ready = 0;
1959
1960 if (list_empty(&ep->queue))
1961 return;
1962
1963 req = list_first_entry(&ep->queue, struct xusb_req, queue);
1964
1965 if (ep->is_in)
1966 xudc_write_fifo(ep, req);
1967 else
1968 xudc_read_fifo(ep, req);
1969}
1970
1971/**
1972 * xudc_irq - The main interrupt handler.
1973 * @irq: The interrupt number.
1974 * @_udc: pointer to the usb device controller structure.
1975 *
1976 * Return: IRQ_HANDLED after the interrupt is handled.
1977 */
1978static irqreturn_t xudc_irq(int irq, void *_udc)
1979{
1980 struct xusb_udc *udc = _udc;
1981 u32 intrstatus;
1982 u32 ier;
1983 u8 index;
1984 u32 bufintr;
1985 unsigned long flags;
1986
1987 spin_lock_irqsave(&udc->lock, flags);
1988
1989 /*
1990 * Event interrupts are level sensitive hence first disable
1991 * IER, read ISR and figure out active interrupts.
1992 */
1993 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
1994 ier &= ~XUSB_STATUS_INTR_EVENT_MASK;
1995 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
1996
1997 /* Read the Interrupt Status Register.*/
1998 intrstatus = udc->read_fn(udc->addr + XUSB_STATUS_OFFSET);
1999
2000 /* Call the handler for the event interrupt.*/
2001 if (intrstatus & XUSB_STATUS_INTR_EVENT_MASK) {
2002 /*
2003 * Check if there is any action to be done for :
2004 * - USB Reset received {XUSB_STATUS_RESET_MASK}
2005 * - USB Suspend received {XUSB_STATUS_SUSPEND_MASK}
2006 * - USB Resume received {XUSB_STATUS_RESUME_MASK}
2007 * - USB Disconnect received {XUSB_STATUS_DISCONNECT_MASK}
2008 */
2009 xudc_startup_handler(udc, intrstatus);
2010 }
2011
2012 /* Check the buffer completion interrupts */
2013 if (intrstatus & XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK) {
2014 /* Enable Reset, Suspend, Resume and Disconnect */
2015 ier = udc->read_fn(udc->addr + XUSB_IER_OFFSET);
2016 ier |= XUSB_STATUS_INTR_EVENT_MASK;
2017 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
2018
2019 if (intrstatus & XUSB_STATUS_EP0_BUFF1_COMP_MASK)
2020 xudc_ctrl_ep_handler(udc, intrstatus);
2021
2022 for (index = 1; index < 8; index++) {
2023 bufintr = ((intrstatus &
2024 (XUSB_STATUS_EP1_BUFF1_COMP_MASK <<
2025 (index - 1))) || (intrstatus &
2026 (XUSB_STATUS_EP1_BUFF2_COMP_MASK <<
2027 (index - 1))));
2028 if (bufintr) {
2029 xudc_nonctrl_ep_handler(udc, index,
2030 intrstatus);
2031 }
2032 }
2033 }
2034
2035 spin_unlock_irqrestore(&udc->lock, flags);
2036 return IRQ_HANDLED;
2037}
2038
2039/**
2040 * xudc_probe - The device probe function for driver initialization.
2041 * @pdev: pointer to the platform device structure.
2042 *
2043 * Return: 0 for success and error value on failure
2044 */
2045static int xudc_probe(struct platform_device *pdev)
2046{
2047 struct device_node *np = pdev->dev.of_node;
2048 struct resource *res;
2049 struct xusb_udc *udc;
2050 struct xusb_ep *ep0;
2051 int irq;
2052 int ret;
2053 u32 ier;
2054 u8 *buff;
2055
2056 udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
2057 if (!udc)
2058 return -ENOMEM;
2059
2060 /* Create a dummy request for GET_STATUS, SET_ADDRESS */
2061 udc->req = devm_kzalloc(&pdev->dev, sizeof(struct xusb_req),
2062 GFP_KERNEL);
2063 if (!udc->req)
2064 return -ENOMEM;
2065
2066 buff = devm_kzalloc(&pdev->dev, STATUSBUFF_SIZE, GFP_KERNEL);
2067 if (!buff)
2068 return -ENOMEM;
2069
2070 udc->req->usb_req.buf = buff;
2071
2072 /* Map the registers */
2073 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2074 udc->addr = devm_ioremap_resource(&pdev->dev, res);
2075 if (!udc->addr)
2076 return -ENOMEM;
2077
2078 irq = platform_get_irq(pdev, 0);
2079 if (irq < 0) {
2080 dev_err(&pdev->dev, "unable to get irq\n");
2081 return irq;
2082 }
2083 ret = devm_request_irq(&pdev->dev, irq, xudc_irq, 0,
2084 dev_name(&pdev->dev), udc);
2085 if (ret < 0) {
2086 dev_dbg(&pdev->dev, "unable to request irq %d", irq);
2087 goto fail;
2088 }
2089
2090 udc->dma_enabled = of_property_read_bool(np, "xlnx,has-builtin-dma");
2091
2092 /* Setup gadget structure */
2093 udc->gadget.ops = &xusb_udc_ops;
2094 udc->gadget.max_speed = USB_SPEED_HIGH;
2095 udc->gadget.speed = USB_SPEED_UNKNOWN;
2096 udc->gadget.ep0 = &udc->ep[XUSB_EP_NUMBER_ZERO].ep_usb;
2097 udc->gadget.name = driver_name;
2098
2099 spin_lock_init(&udc->lock);
2100
2101 /* Check for IP endianness */
2102 udc->write_fn = xudc_write32_be;
2103 udc->read_fn = xudc_read32_be;
2104 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, TEST_J);
2105 if ((udc->read_fn(udc->addr + XUSB_TESTMODE_OFFSET))
2106 != TEST_J) {
2107 udc->write_fn = xudc_write32;
2108 udc->read_fn = xudc_read32;
2109 }
2110 udc->write_fn(udc->addr, XUSB_TESTMODE_OFFSET, 0);
2111
2112 xudc_eps_init(udc);
2113
2114 ep0 = &udc->ep[0];
2115
2116 /* Set device address to 0.*/
2117 udc->write_fn(udc->addr, XUSB_ADDRESS_OFFSET, 0);
2118
2119 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2120 if (ret)
2121 goto fail;
2122
2123 udc->dev = &udc->gadget.dev;
2124
2125 /* Enable the interrupts.*/
2126 ier = XUSB_STATUS_GLOBAL_INTR_MASK | XUSB_STATUS_INTR_EVENT_MASK |
2127 XUSB_STATUS_FIFO_BUFF_RDY_MASK | XUSB_STATUS_FIFO_BUFF_FREE_MASK |
2128 XUSB_STATUS_SETUP_PACKET_MASK |
2129 XUSB_STATUS_INTR_BUFF_COMP_ALL_MASK;
2130
2131 udc->write_fn(udc->addr, XUSB_IER_OFFSET, ier);
2132
2133 platform_set_drvdata(pdev, udc);
2134
2135 dev_vdbg(&pdev->dev, "%s at 0x%08X mapped to 0x%08X %s\n",
2136 driver_name, (u32)res->start, (u32 __force)udc->addr,
2137 udc->dma_enabled ? "with DMA" : "without DMA");
2138
2139 return 0;
2140fail:
2141 dev_err(&pdev->dev, "probe failed, %d\n", ret);
2142 return ret;
2143}
2144
2145/**
2146 * xudc_remove - Releases the resources allocated during the initialization.
2147 * @pdev: pointer to the platform device structure.
2148 *
2149 * Return: 0 always
2150 */
2151static int xudc_remove(struct platform_device *pdev)
2152{
2153 struct xusb_udc *udc = platform_get_drvdata(pdev);
2154
2155 usb_del_gadget_udc(&udc->gadget);
2156
2157 return 0;
2158}
2159
2160/* Match table for of_platform binding */
2161static const struct of_device_id usb_of_match[] = {
2162 { .compatible = "xlnx,usb2-device-4.00.a", },
2163 { /* end of list */ },
2164};
2165MODULE_DEVICE_TABLE(of, usb_of_match);
2166
2167static struct platform_driver xudc_driver = {
2168 .driver = {
2169 .name = driver_name,
2170 .of_match_table = usb_of_match,
2171 },
2172 .probe = xudc_probe,
2173 .remove = xudc_remove,
2174};
2175
2176module_platform_driver(xudc_driver);
2177
2178MODULE_DESCRIPTION("Xilinx udc driver");
2179MODULE_AUTHOR("Xilinx, Inc");
2180MODULE_LICENSE("GPL");