36 #include <linux/kernel.h>
37 #include <linux/list.h>
39 #include <linux/module.h>
44 #include <linux/slab.h>
93 #define is_buffer_mapped(req) (is_dma_capable() && \
94 (req->map_state != UN_MAPPED))
149 "not unmapping a never mapped buffer\n");
189 req = to_musb_request(request);
197 spin_unlock(&musb->
lock);
198 unmap_dma_buffer(req, musb);
199 if (request->status == 0)
201 ep->end_point.name, request,
205 ep->end_point.name, request,
209 spin_lock(&musb->
lock);
221 struct musb *musb = ep->
musb;
255 while (!list_empty(&ep->
req_list)) {
270 static inline int max_ep_writesize(
struct musb *musb,
struct musb_ep *ep)
273 return ep->
hw_ep->max_packet_sz_tx;
279 #ifdef CONFIG_USB_INVENTRA_DMA
320 static void txstate(
struct musb *musb,
struct musb_request *req)
332 if (!musb_ep->
desc) {
348 fifo_count =
min(max_ep_writesize(musb, musb_ep),
367 #ifndef CONFIG_MUSB_PIO_ONLY
374 musb_ep->
dma->max_len);
380 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
382 if (request_size < musb_ep->packet_sz)
383 musb_ep->
dma->desired_mode = 0;
385 musb_ep->
dma->desired_mode = 1;
389 musb_ep->
dma->desired_mode,
390 request->
dma + request->
actual, request_size);
392 if (musb_ep->
dma->desired_mode == 0) {
420 #elif defined(CONFIG_USB_TI_CPPI_DMA)
454 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
469 unmap_dma_buffer(req, musb);
473 request->
actual += fifo_count;
481 musb_ep->
end_point.name, use_dma ?
"dma" :
"pio",
498 struct musb_ep *musb_ep = &musb->
endpoints[epnum].ep_in;
503 req = next_request(musb_ep);
517 csr &= ~MUSB_TXCSR_P_SENTSTALL;
546 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
551 request->
actual += musb_ep->
dma->actual_len;
553 epnum, csr, musb_ep->
dma->actual_len, request);
563 #
if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
573 if (csr & MUSB_TXCSR_TXPKTRDY)
578 | MUSB_TXCSR_TXPKTRDY);
593 req = musb_ep->
desc ? next_request(musb_ep) :
NULL;
607 #ifdef CONFIG_USB_INVENTRA_DMA
641 static void rxstate(
struct musb *musb,
struct musb_request *req)
645 struct musb_ep *musb_ep;
654 musb_ep = &hw_ep->
ep_in;
661 if (!musb_ep->
desc) {
721 #ifdef CONFIG_USB_INVENTRA_DMA
729 channel = musb_ep->
dma;
770 musb_ep->
dma->desired_mode = 1;
774 musb_ep->
hw_ep->rx_double_buffered)
780 (
unsigned)fifo_count);
781 musb_ep->
dma->desired_mode = 0;
795 #elif defined(CONFIG_USB_UX500_DMA)
801 int transfer_size = 0;
804 channel = musb_ep->
dma;
807 if (fifo_count < musb_ep->packet_sz)
808 transfer_size = fifo_count;
816 (
unsigned)fifo_count);
824 if (transfer_size <= musb_ep->packet_sz) {
825 musb_ep->
dma->desired_mode = 0;
827 musb_ep->
dma->desired_mode = 1;
850 fifo_count =
min_t(
unsigned, len, fifo_count);
852 #ifdef CONFIG_USB_TUSB_OMAP_DMA
874 unmap_dma_buffer(req, musb);
886 request->
actual += fifo_count;
894 csr &= ~MUSB_RXCSR_RXPKTRDY;
901 fifo_count < musb_ep->packet_sz)
914 struct musb_ep *musb_ep;
920 musb_ep = &hw_ep->
ep_in;
926 req = next_request(musb_ep);
936 csr, dma ?
" (dma)" :
"", request);
940 csr &= ~MUSB_RXCSR_P_SENTSTALL;
947 csr &= ~MUSB_RXCSR_P_OVERRUN;
973 request->
actual += musb_ep->
dma->actual_len;
978 musb_ep->
dma->actual_len, request);
980 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
981 defined(CONFIG_USB_UX500_DMA)
987 csr &= ~MUSB_RXCSR_RXPKTRDY;
993 && (musb_ep->
dma->actual_len
999 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
1016 req = next_request(musb_ep);
1020 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1021 defined(CONFIG_USB_UX500_DMA)
1030 static int musb_gadget_enable(
struct usb_ep *ep,
1033 unsigned long flags;
1034 struct musb_ep *musb_ep;
1047 musb_ep = to_musb_ep(ep);
1048 hw_ep = musb_ep->
hw_ep;
1050 musb = musb_ep->
musb;
1051 mbase = musb->
mregs;
1056 if (musb_ep->
desc) {
1060 musb_ep->
type = usb_endpoint_type(desc);
1063 if (usb_endpoint_num(desc) != epnum)
1067 tmp = usb_endpoint_maxp(desc);
1068 if (tmp & ~0x07ff) {
1071 if (usb_endpoint_dir_in(desc))
1080 musb_ep->
hb_mult = (tmp >> 11) & 3;
1092 if (usb_endpoint_dir_in(desc)) {
1097 if (!musb_ep->
is_in)
1105 int_txe |= (1 << epnum);
1145 int_rxe |= (1 << epnum);
1189 musb_ep->
desc = desc;
1194 pr_debug(
"%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1196 ({
char *
s; switch (musb_ep->
type) {
1199 default:
s =
"iso";
break;
1201 musb_ep->
is_in ?
"IN" :
"OUT",
1202 musb_ep->
dma ?
"dma, " :
"",
1208 spin_unlock_irqrestore(&musb->
lock, flags);
1215 static int musb_gadget_disable(
struct usb_ep *ep)
1217 unsigned long flags;
1220 struct musb_ep *musb_ep;
1224 musb_ep = to_musb_ep(ep);
1225 musb = musb_ep->
musb;
1233 if (musb_ep->
is_in) {
1235 int_txe &= ~(1 << epnum);
1240 int_rxe &= ~(1 << epnum);
1253 spin_unlock_irqrestore(&(musb->
lock), flags);
1266 struct musb_ep *musb_ep = to_musb_ep(ep);
1267 struct musb *musb = musb_ep->
musb;
1270 request = kzalloc(
sizeof *request, gfp_flags);
1278 request->
ep = musb_ep;
1289 kfree(to_musb_request(req));
1307 req->
tx ?
"TX/IN" :
"RX/OUT",
1320 struct musb_ep *musb_ep;
1324 unsigned long lockflags;
1331 musb_ep = to_musb_ep(ep);
1332 musb = musb_ep->
musb;
1334 request = to_musb_request(req);
1335 request->
musb = musb;
1337 if (request->
ep != musb_ep)
1348 map_dma_buffer(request, musb, musb_ep);
1353 if (!musb_ep->
desc) {
1355 req, ep->
name,
"disabled");
1368 spin_unlock_irqrestore(&musb->
lock, lockflags);
1374 struct musb_ep *musb_ep = to_musb_ep(ep);
1377 unsigned long flags;
1379 struct musb *musb = musb_ep->
musb;
1381 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1419 spin_unlock_irqrestore(&musb->
lock, flags);
1429 static int musb_gadget_set_halt(
struct usb_ep *ep,
int value)
1431 struct musb_ep *musb_ep = to_musb_ep(ep);
1433 struct musb *musb = musb_ep->
musb;
1436 unsigned long flags;
1443 mbase = musb->
mregs;
1454 request = next_request(musb_ep);
1463 if (musb_ep->
is_in) {
1465 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1476 if (musb_ep->
is_in) {
1483 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1485 csr &= ~MUSB_TXCSR_TXPKTRDY;
1495 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1501 if (!musb_ep->
busy && !value && request) {
1507 spin_unlock_irqrestore(&musb->
lock, flags);
1514 static int musb_gadget_set_wedge(
struct usb_ep *ep)
1516 struct musb_ep *musb_ep = to_musb_ep(ep);
1523 return usb_ep_set_halt(ep);
1526 static int musb_gadget_fifo_status(
struct usb_ep *ep)
1528 struct musb_ep *musb_ep = to_musb_ep(ep);
1533 struct musb *musb = musb_ep->
musb;
1536 unsigned long flags;
1544 spin_unlock_irqrestore(&musb->
lock, flags);
1549 static void musb_gadget_fifo_flush(
struct usb_ep *ep)
1551 struct musb_ep *musb_ep = to_musb_ep(ep);
1552 struct musb *musb = musb_ep->
musb;
1556 unsigned long flags;
1559 mbase = musb->
mregs;
1566 musb_writew(mbase,
MUSB_INTRTXE, int_txe & ~(1 << epnum));
1568 if (musb_ep->
is_in) {
1570 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1577 csr &= ~MUSB_TXCSR_TXPKTRDY;
1591 spin_unlock_irqrestore(&musb->
lock, flags);
1594 static const struct usb_ep_ops musb_ep_ops = {
1595 .enable = musb_gadget_enable,
1596 .disable = musb_gadget_disable,
1599 .queue = musb_gadget_queue,
1600 .dequeue = musb_gadget_dequeue,
1601 .set_halt = musb_gadget_set_halt,
1602 .set_wedge = musb_gadget_set_wedge,
1603 .fifo_status = musb_gadget_fifo_status,
1604 .fifo_flush = musb_gadget_fifo_flush
1609 static int musb_gadget_get_frame(
struct usb_gadget *gadget)
1611 struct musb *musb = gadget_to_musb(gadget);
1616 static int musb_gadget_wakeup(
struct usb_gadget *gadget)
1618 struct musb *musb = gadget_to_musb(gadget);
1620 unsigned long flags;
1627 switch (musb->
xceiv->state) {
1650 while (devctl & MUSB_DEVCTL_SESSION) {
1656 spin_unlock_irqrestore(&musb->
lock, flags);
1657 otg_start_srp(musb->
xceiv->otg);
1661 musb_platform_try_idle(musb,
1686 spin_unlock_irqrestore(&musb->
lock, flags);
1691 musb_gadget_set_self_powered(
struct usb_gadget *gadget,
int is_selfpowered)
1693 struct musb *musb = gadget_to_musb(gadget);
1699 static void musb_pullup(
struct musb *musb,
int is_on)
1712 is_on ?
"on" :
"off");
1730 static int musb_gadget_vbus_draw(
struct usb_gadget *gadget,
unsigned mA)
1732 struct musb *musb = gadget_to_musb(gadget);
1734 if (!musb->
xceiv->set_power)
1736 return usb_phy_set_power(musb->
xceiv, mA);
1739 static int musb_gadget_pullup(
struct usb_gadget *gadget,
int is_on)
1741 struct musb *musb = gadget_to_musb(gadget);
1742 unsigned long flags;
1754 musb_pullup(musb, is_on);
1756 spin_unlock_irqrestore(&musb->
lock, flags);
1763 static int musb_gadget_start(
struct usb_gadget *
g,
1769 .get_frame = musb_gadget_get_frame,
1770 .wakeup = musb_gadget_wakeup,
1771 .set_selfpowered = musb_gadget_set_self_powered,
1773 .vbus_draw = musb_gadget_vbus_draw,
1774 .pullup = musb_gadget_pullup,
1775 .udc_start = musb_gadget_start,
1776 .udc_stop = musb_gadget_stop,
1788 static void musb_gadget_release(
struct device *
dev)
1791 dev_dbg(dev,
"%s\n", __func__);
1796 init_peripheral_ep(
struct musb *musb,
struct musb_ep *ep,
u8 epnum,
int is_in)
1800 memset(ep, 0,
sizeof *ep);
1811 is_in ?
"in" :
"out"));
1832 static inline void __devinit musb_g_init_endpoints(
struct musb *musb)
1839 INIT_LIST_HEAD(&(musb->
g.ep_list));
1841 for (epnum = 0, hw_ep = musb->
endpoints;
1842 epnum < musb->nr_endpoints;
1845 init_peripheral_ep(musb, &hw_ep->
ep_in, epnum, 0);
1849 init_peripheral_ep(musb, &hw_ep->
ep_in,
1854 init_peripheral_ep(musb, &hw_ep->
ep_out,
1874 musb->
g.ops = &musb_gadget_operations;
1881 musb->
g.dev.dma_mask = musb->
controller->dma_mask;
1882 musb->
g.dev.release = musb_gadget_release;
1887 musb_g_init_endpoints(musb);
1890 musb_platform_try_idle(musb, 0);
1903 musb->
g.dev.parent =
NULL;
1911 if (musb->
g.dev.parent)
1926 static int musb_gadget_start(
struct usb_gadget *g,
1929 struct musb *musb = gadget_to_musb(g);
1931 struct usb_hcd *hcd = musb_to_hcd(musb);
1932 unsigned long flags;
1950 otg_set_peripheral(otg, &musb->
g);
1952 spin_unlock_irqrestore(&musb->
lock, flags);
1966 otg_set_vbus(otg, 1);
1968 hcd->self.uses_pio_for_control = 1;
1993 musb_pullup(musb, 0);
2002 i < musb->nr_endpoints;
2023 static int musb_gadget_stop(
struct usb_gadget *g,
2026 struct musb *musb = gadget_to_musb(g);
2027 unsigned long flags;
2041 (
void) musb_gadget_vbus_draw(&musb->
g, 0);
2044 stop_activity(musb, driver);
2045 otg_set_peripheral(musb->
xceiv->otg,
NULL);
2050 musb_platform_try_idle(musb, 0);
2051 spin_unlock_irqrestore(&musb->
lock, flags);
2072 switch (musb->
xceiv->state) {
2079 spin_unlock(&musb->
lock);
2081 spin_lock(&musb->
lock);
2085 WARNING(
"unhandled RESUME transition (%s)\n",
2098 switch (musb->
xceiv->state) {
2106 spin_unlock(&musb->
lock);
2108 spin_lock(&musb->
lock);
2115 WARNING(
"unhandled SUSPEND transition (%s)\n",
2123 musb_gadget_wakeup(&musb->
g);
2135 musb_writeb(mregs,
MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2138 (
void) musb_gadget_vbus_draw(&musb->
g, 0);
2142 spin_unlock(&musb->
lock);
2144 spin_lock(&musb->
lock);
2147 switch (musb->
xceiv->state) {
2181 ?
"B-Device" :
"A-Device",
2194 musb_writeb(mbase,
MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2210 musb->
g.b_hnp_enable = 0;
2211 musb->
g.a_alt_hnp_support = 0;
2212 musb->
g.a_hnp_support = 0;
2217 if (devctl & MUSB_DEVCTL_BDEVICE) {
2219 musb->
g.is_a_peripheral = 0;
2222 musb->
g.is_a_peripheral = 1;
2226 (
void) musb_gadget_vbus_draw(&musb->
g, 8);