36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
43 #include <linux/list.h>
100 static void musb_ep_program(
struct musb *
musb,
u8 epnum,
101 struct urb *
urb,
int is_out,
107 static void musb_h_tx_flush_fifo(
struct musb_hw_ep *ep)
123 if (
WARN(retries-- < 1,
124 "Could not flush host TX%d fifo: csr: %04x\n",
131 static void musb_h_ep0_flush_fifo(
struct musb_hw_ep *ep)
147 WARN(!retries,
"Could not flush host TX%d fifo: csr: %04x\n",
158 static inline void musb_h_tx_start(
struct musb_hw_ep *ep)
174 static inline void musb_h_tx_dma_start(
struct musb_hw_ep *ep)
206 musb_start_urb(
struct musb *musb,
int is_in,
struct musb_qh *qh)
211 struct urb *
urb = next_urb(qh);
212 void *
buf = urb->transfer_buffer;
215 unsigned pipe = urb->pipe;
229 buf = urb->setup_packet;
235 offset = urb->iso_frame_desc[0].offset;
236 len = urb->iso_frame_desc[0].length;
240 buf = urb->transfer_buffer + urb->actual_length;
241 len = urb->transfer_buffer_length - urb->actual_length;
245 qh, urb, address, qh->
epnum,
246 is_in ?
"in" :
"out",
247 ({
char *
s; switch (qh->
type) {
251 default:
s =
"-intr";
break;
253 epnum, buf + offset, len);
256 musb_ep_set_qh(hw_ep, is_in, qh);
257 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
272 if ((urb->transfer_flags & URB_ISO_ASAP)
273 || (frame >= urb->start_frame)) {
280 qh->
frame = urb->start_frame;
294 musb_h_tx_start(hw_ep);
296 musb_h_tx_dma_start(hw_ep);
301 static void musb_giveback(
struct musb *musb,
struct urb *urb,
int status)
306 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
307 urb, urb->complete,
status,
308 usb_pipedevice(urb->pipe),
309 usb_pipeendpoint(urb->pipe),
310 usb_pipein(urb->pipe) ?
"in" :
"out",
311 urb->actual_length, urb->transfer_buffer_length
315 spin_unlock(&musb->
lock);
317 spin_lock(&musb->
lock);
321 static inline void musb_save_toggle(
struct musb_qh *qh,
int is_in,
337 usb_settoggle(urb->dev, qh->
epnum, !is_in, csr ? 1 : 0);
347 static void musb_advance_schedule(
struct musb *musb,
struct urb *urb,
350 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
355 status = (urb->status == -
EINPROGRESS) ? 0 : urb->status;
361 musb_save_toggle(qh, is_in, urb);
364 if (status == 0 && urb->error_count)
370 musb_giveback(musb, urb, status);
376 if (list_empty(&qh->
hep->urb_list)) {
395 musb_ep_set_qh(ep, is_in,
NULL);
406 head = qh->
ring.prev;
427 hw_ep->
epnum, is_in ?
'R' :
'T', next_urb(qh));
428 musb_start_urb(musb, is_in, qh);
455 musb_host_packet_rx(
struct musb *musb,
struct urb *urb,
u8 epnum,
u8 iso_err)
466 int pipe = urb->pipe;
467 void *
buffer = urb->transfer_buffer;
471 dev_dbg(musb->
controller,
"RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
472 urb->transfer_buffer, qh->
offset,
473 urb->transfer_buffer_length);
476 if (usb_pipeisoc(pipe)) {
478 struct usb_iso_packet_descriptor *
d;
485 d = urb->iso_frame_desc + qh->
iso_idx;
486 buf = buffer + d->offset;
488 if (rx_count > length) {
497 urb->actual_length +=
length;
498 d->actual_length =
length;
503 done = (++qh->
iso_idx >= urb->number_of_packets);
506 buf = buffer + qh->
offset;
507 length = urb->transfer_buffer_length - qh->
offset;
508 if (rx_count > length) {
515 urb->actual_length +=
length;
519 done = (urb->actual_length == urb->transfer_buffer_length)
520 || (rx_count < qh->maxpacket)
524 && (urb->transfer_flags & URB_SHORT_NOT_OK)
525 && (urb->actual_length
526 < urb->transfer_buffer_length))
535 musb_h_flush_rxfifo(hw_ep, csr);
569 musb_h_tx_flush_fifo(ep);
620 struct urb *urb,
u32 offset,
u32 length)
628 #ifdef CONFIG_USB_INVENTRA_DMA
633 if (length > pkt_size) {
656 mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
668 urb->transfer_dma + offset, length)) {
684 static void musb_ep_program(
struct musb *musb,
u8 epnum,
685 struct urb *urb,
int is_out,
694 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
700 "h_addr%02x h_port%02x bytes %d\n",
701 is_out ?
"-->" :
"<--",
702 epnum, urb, urb->dev->speed,
709 if (is_out && !len) {
723 dma_controller, hw_ep, is_out);
744 musb_writew(mbase,
MUSB_INTRTXE, int_txe & ~(1 << epnum));
749 musb_h_tx_flush_fifo(hw_ep);
766 if (usb_gettoggle(urb->dev, qh->
epnum, 1))
779 musb_h_ep0_flush_fifo(hw_ep);
784 musb_write_txfunaddr(mbase, epnum, qh->
addr_reg);
785 musb_write_txhubaddr(mbase, epnum, qh->
h_addr_reg);
786 musb_write_txhubport(mbase, epnum, qh->
h_port_reg);
800 packet_sz) - 1) << 11);
817 load_count =
min((
u32) packet_sz, len);
819 if (dma_channel && musb_tx_dma_program(dma_controller,
820 hw_ep, qh, urb, offset, len))
837 buf = qh->
sg_miter.addr + urb->sg->offset +
856 musb_rx_reinit(musb, qh, hw_ep);
859 if (usb_gettoggle(urb->dev, qh->
epnum, 0))
870 if (csr & (MUSB_RXCSR_RXPKTRDY
873 ERR(
"broken !rx_reinit, ep%d csr %04x\n",
896 packet_sz, !(urb->transfer_flags &
898 urb->transfer_dma + offset,
917 static void musb_bulk_nak_timeout(
struct musb *musb,
struct musb_hw_ep *ep,
920 struct dma_channel *
dma;
924 struct musb_qh *cur_qh, *next_qh;
937 cur_qh = first_qh(&musb->
in_bulk);
950 urb = next_urb(cur_qh);
957 musb_save_toggle(cur_qh, is_in, urb);
964 next_qh = first_qh(&musb->
in_bulk);
973 next_qh = first_qh(&musb->
out_bulk);
978 musb_start_urb(musb, is_in, next_qh);
986 static bool musb_h_ep0_continue(
struct musb *musb,
u16 len,
struct urb *urb)
997 fifo_dest = urb->transfer_buffer + urb->actual_length;
998 fifo_count =
min_t(
size_t, len, urb->transfer_buffer_length -
1000 if (fifo_count < len)
1005 urb->actual_length += fifo_count;
1006 if (len < qh->maxpacket) {
1010 }
else if (urb->actual_length <
1011 urb->transfer_buffer_length)
1033 urb->transfer_buffer_length -
1034 urb->actual_length);
1036 fifo_dest = (
u8 *) (urb->transfer_buffer
1037 + urb->actual_length);
1040 (fifo_count == 1) ?
"" :
"s",
1044 urb->actual_length += fifo_count;
1125 csr &= ~MUSB_CSR0_H_REQPKT;
1127 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1130 musb_h_ep0_flush_fifo(hw_ep);
1142 ERR(
"no URB for end 0\n");
1144 musb_h_ep0_flush_fifo(hw_ep);
1150 if (musb_h_ep0_continue(musb, len, urb)) {
1156 if (usb_pipeout(urb->pipe)
1157 || !urb->transfer_buffer_length)
1177 musb_advance_schedule(musb, urb, hw_ep, 1);
1183 #ifdef CONFIG_USB_INVENTRA_DMA
1210 struct urb *urb = next_urb(qh);
1213 struct dma_channel *
dma;
1214 bool transfer_pending =
false;
1229 dma ?
", dma" :
"");
1247 && !list_is_singular(&musb->
out_bulk)) {
1249 "NAK timeout on TX%d ep\n", epnum);
1250 musb_bulk_nak_timeout(musb, hw_ep, 0);
1253 "TX end=%d device not responding\n", epnum);
1280 musb_h_tx_flush_fifo(hw_ep);
1283 | MUSB_TXCSR_H_ERROR
1284 | MUSB_TXCSR_H_RXSTALL
1336 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1359 "CSR %04x\n", tx_csr);
1364 if (!status || dma || usb_pipeisoc(pipe)) {
1371 if (usb_pipeisoc(pipe)) {
1372 struct usb_iso_packet_descriptor *
d;
1374 d = urb->iso_frame_desc + qh->
iso_idx;
1375 d->actual_length =
length;
1377 if (++qh->
iso_idx >= urb->number_of_packets) {
1384 }
else if (dma && urb->transfer_buffer_length == qh->
offset) {
1390 else if (qh->
offset == urb->transfer_buffer_length
1391 && !(urb->transfer_flags
1396 length = urb->transfer_buffer_length -
offset;
1397 transfer_pending =
true;
1408 status = urb->status;
1414 urb->actual_length = qh->
offset;
1415 musb_advance_schedule(musb, urb, hw_ep,
USB_DIR_OUT);
1417 }
else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
1421 musb_h_tx_dma_start(hw_ep);
1445 if (!urb->transfer_buffer)
1456 urb->transfer_buffer = qh->
sg_miter.addr;
1468 if (offset + length >= urb->transfer_buffer_length)
1478 #ifdef CONFIG_USB_INVENTRA_DMA
1531 bool iso_err =
false;
1534 struct dma_channel *
dma;
1562 epnum, rx_csr, urb->actual_length,
1592 if (usb_pipebulk(urb->pipe)
1594 && !list_is_singular(&musb->
in_bulk)) {
1595 musb_bulk_nak_timeout(musb, hw_ep, 1);
1600 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1631 ERR(
"RX%d dma busy, csr %04x\n", epnum, rx_csr);
1642 #ifndef CONFIG_USB_INVENTRA_DMA
1657 xfer_len, dma ?
", dma" :
"");
1658 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1668 val &= ~(MUSB_RXCSR_DMAENAB
1674 #ifdef CONFIG_USB_INVENTRA_DMA
1675 if (usb_pipeisoc(pipe)) {
1676 struct usb_iso_packet_descriptor *
d;
1678 d = urb->iso_frame_desc + qh->
iso_idx;
1687 if (++qh->
iso_idx >= urb->number_of_packets)
1694 done = (urb->actual_length + xfer_len >=
1695 urb->transfer_buffer_length
1707 done ?
"off" :
"reset",
1715 if (
unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1717 ERR(
"Rx interrupt with no errors or packet!\n");
1724 val &= ~MUSB_RXCSR_H_REQPKT;
1730 #ifdef CONFIG_USB_INVENTRA_DMA
1732 struct dma_controller *
c;
1742 + urb->actual_length,
1744 urb->transfer_buffer_length);
1748 if (usb_pipeisoc(pipe)) {
1750 struct usb_iso_packet_descriptor *
d;
1752 d = urb->iso_frame_desc + qh->
iso_idx;
1758 if (rx_count > d->length) {
1759 if (d_status == 0) {
1764 rx_count, d->length);
1769 d->status = d_status;
1770 buf = urb->transfer_dma + d->offset;
1773 buf = urb->transfer_dma +
1782 if ((urb->transfer_flags &
1784 && (urb->transfer_buffer_length -
1788 if (rx_count < hw_ep->max_packet_sz_rx) {
1792 length = urb->transfer_buffer_length;
1814 val &= ~MUSB_RXCSR_H_REQPKT;
1842 val &= ~(MUSB_RXCSR_DMAENAB
1851 unsigned int received_len;
1860 if (!urb->transfer_buffer) {
1874 urb->transfer_buffer = qh->
sg_miter.addr;
1875 received_len = urb->actual_length;
1877 done = musb_host_packet_rx(musb, urb, epnum,
1880 received_len = urb->actual_length -
1882 qh->
sg_miter.consumed = received_len;
1885 done = musb_host_packet_rx(musb, urb,
1901 musb_advance_schedule(musb, urb, hw_ep,
USB_DIR_IN);
1910 static int musb_schedule(
1917 int best_end, epnum;
1922 struct urb *urb = next_urb(qh);
1927 hw_ep = musb->control_ep;
1943 for (epnum = 1, hw_ep = musb->
endpoints + 1;
1944 epnum < musb->nr_endpoints;
1948 if (musb_ep_get_qh(hw_ep, is_in) !=
NULL)
1960 if (diff >= 0 && best_diff > diff) {
1975 toggle = usb_gettoggle(urb->dev, qh->
epnum, !is_in);
2005 }
else if (best_end < 0) {
2015 idle = list_empty(head);
2020 qh->
hep->hcpriv = qh;
2022 musb_start_urb(musb, is_in, qh);
2026 static int musb_urb_enqueue(
2027 struct usb_hcd *hcd,
2031 unsigned long flags;
2032 struct musb *musb = hcd_to_musb(hcd);
2033 struct usb_host_endpoint *
hep = urb->ep;
2046 qh = ret ?
NULL : hep->hcpriv;
2049 spin_unlock_irqrestore(&musb->
lock, flags);
2068 qh = kzalloc(
sizeof *qh, mem_flags);
2072 spin_unlock_irqrestore(&musb->
lock, flags);
2078 INIT_LIST_HEAD(&qh->
ring);
2082 qh->
type = usb_endpoint_type(epd);
2093 ok = (usb_pipein(urb->pipe) && musb->
hb_iso_rx)
2094 || (usb_pipeout(urb->pipe) && musb->
hb_iso_tx);
2102 qh->
epnum = usb_endpoint_num(epd);
2105 qh->
addr_reg = (
u8) usb_pipedevice(urb->pipe);
2109 switch (urb->dev->speed) {
2158 struct usb_device *
parent = urb->dev->parent;
2160 if (parent != hcd->self.root_hub) {
2166 if (urb->dev->tt->hub)
2168 (
u8) urb->dev->tt->hub->devnum;
2169 if (urb->dev->tt->multi)
2180 if (hep->hcpriv || !next_urb(qh)) {
2188 ret = musb_schedule(musb, qh,
2197 spin_unlock_irqrestore(&musb->
lock, flags);
2203 spin_unlock_irqrestore(&musb->
lock, flags);
2215 static int musb_cleanup_urb(
struct urb *urb,
struct musb_qh *qh)
2218 struct musb *musb = ep->
musb;
2220 unsigned hw_end = ep->
epnum;
2222 int is_in = usb_pipein(urb->pipe);
2229 struct dma_channel *
dma;
2233 status = ep->
musb->dma_controller->channel_abort(dma);
2235 "abort %cX%d DMA for urb %p --> %d\n",
2236 is_in ?
'R' :
'T', ep->
epnum,
2243 if (ep->
epnum && is_in) {
2245 csr = musb_h_flush_rxfifo(ep, 0);
2251 }
else if (ep->
epnum) {
2252 musb_h_tx_flush_fifo(ep);
2266 musb_h_ep0_flush_fifo(ep);
2269 musb_advance_schedule(ep->
musb, urb, ep, is_in);
2273 static int musb_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
int status)
2275 struct musb *musb = hcd_to_musb(hcd);
2277 unsigned long flags;
2278 int is_in = usb_pipein(urb->pipe);
2282 usb_pipedevice(urb->pipe),
2283 usb_pipeendpoint(urb->pipe),
2284 is_in ?
"in" :
"out");
2308 || urb->urb_list.prev != &qh->
hep->urb_list
2309 || musb_ep_get_qh(qh->
hw_ep, is_in) != qh) {
2313 musb_giveback(musb, urb, 0);
2319 if (ready && list_empty(&qh->
hep->urb_list)) {
2325 ret = musb_cleanup_urb(urb, qh);
2327 spin_unlock_irqrestore(&musb->
lock, flags);
2333 musb_h_disable(
struct usb_hcd *hcd,
struct usb_host_endpoint *hep)
2336 unsigned long flags;
2337 struct musb *musb = hcd_to_musb(hcd);
2351 if (musb_ep_get_qh(qh->
hw_ep, is_in) == qh) {
2359 musb_cleanup_urb(urb, qh);
2364 while (!list_empty(&hep->urb_list)) {
2367 musb_advance_schedule(musb, urb, qh->
hw_ep, is_in);
2374 while (!list_empty(&hep->urb_list))
2375 musb_giveback(musb, next_urb(qh), -
ESHUTDOWN);
2382 spin_unlock_irqrestore(&musb->
lock, flags);
2385 static int musb_h_get_frame_number(
struct usb_hcd *hcd)
2387 struct musb *musb = hcd_to_musb(hcd);
2392 static int musb_h_start(
struct usb_hcd *hcd)
2394 struct musb *musb = hcd_to_musb(hcd);
2399 hcd->state = HC_STATE_RUNNING;
2404 static void musb_h_stop(
struct usb_hcd *hcd)
2407 hcd->state = HC_STATE_HALT;
2410 static int musb_bus_suspend(
struct usb_hcd *hcd)
2412 struct musb *musb = hcd_to_musb(hcd);
2418 switch (musb->
xceiv->state) {
2435 WARNING(
"trying to suspend as %s while active\n",
2442 static int musb_bus_resume(
struct usb_hcd *hcd)
2449 .description =
"musb-hcd",
2450 .product_desc =
"MUSB HDRC host driver",
2451 .hcd_priv_size =
sizeof(
struct musb),
2452 .flags = HCD_USB2 | HCD_MEMORY,
2458 .start = musb_h_start,
2459 .stop = musb_h_stop,
2461 .get_frame_number = musb_h_get_frame_number,
2463 .urb_enqueue = musb_urb_enqueue,
2464 .urb_dequeue = musb_urb_dequeue,
2465 .endpoint_disable = musb_h_disable,
2469 .bus_suspend = musb_bus_suspend,
2470 .bus_resume = musb_bus_resume,