Index: ehci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/ehci.c,v retrieving revision 1.6 diff -u -r1.6 ehci.c --- ehci.c 19 Mar 2004 07:14:23 -0000 1.6 +++ ehci.c 9 Apr 2004 22:32:58 -0000 @@ -622,7 +622,7 @@ pipe = xfer->pipe; - p = KERNADDR(&xfer->dmabuf, 0); + p = xfer->buffer; m = min(sc->sc_noport, xfer->length * 8 - 1); memset(p, 0, xfer->length); for (i = 1; i <= m; i++) { @@ -1571,7 +1571,7 @@ index = UGETW(req->wIndex); if (len != 0) - buf = KERNADDR(&xfer->dmabuf, 0); + buf = xfer->buffer; #define C(x,y) ((x) | ((y) << 8)) switch(C(req->bRequest, req->bmRequestType)) { @@ -2136,18 +2136,17 @@ ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep) { ehci_soft_qtd_t *next, *cur; - ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys; + ehci_physaddr_t dataphys, nextphys; u_int32_t qtdstatus; - int len, curlen, offset; + int adj, len, curlen, maxp, offset, pagelen, seg, segoff; int i; - usb_dma_t *dma = &xfer->dmabuf; + struct usb_dma_mapping *dma = &xfer->dmamap; DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen)); offset = 0; len = alen; - dataphys = DMAADDR(dma, 0); - dataphyslastpage = EHCI_PAGE(DMAADDR(dma, len - 1)); + maxp = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize); qtdstatus = htole32( EHCI_QTD_ACTIVE | EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) | @@ -2161,60 +2160,60 @@ *sp = cur; if (cur == NULL) goto nomem; + seg = 0; + segoff = 0; for (;;) { - dataphyspage = EHCI_PAGE(dataphys); + curlen = 0; + /* The EHCI hardware can handle at most 5 pages. */ -#if defined(__NetBSD__) || defined(__OpenBSD__) - if (dataphyslastpage - dataphyspage < - EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) { - /* we can handle it in this QTD */ - curlen = len; -#elif defined(__FreeBSD__) - /* XXX This is pretty broken: Because we do not allocate - * a contiguous buffer (contiguous in physical pages) we - * can only transfer one page in one go. - * So check whether the start and end of the buffer are on - * the same page. - */ - if (dataphyspage == dataphyslastpage) { - curlen = len; -#endif - } else { -#if defined(__NetBSD__) || defined(__OpenBSD__) - /* must use multiple TDs, fill as much as possible. */ - curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE - - EHCI_PAGE_OFFSET(dataphys); -#ifdef DIAGNOSTIC - if (curlen > len) { - printf("ehci_alloc_sqtd_chain: curlen=0x%x " - "len=0x%x offs=0x%x\n", curlen, len, - EHCI_PAGE_OFFSET(dataphys)); - printf("lastpage=0x%x page=0x%x phys=0x%x\n", - dataphyslastpage, dataphyspage, - dataphys); - curlen = len; + for (i = 0; i < EHCI_QTD_NBUFFERS && curlen < len; i++) { + KASSERT(seg < dma->nsegs, + ("ehci_alloc_sqtd_chain: overrun")); + dataphys = dma->segs[seg].ds_addr + segoff; + pagelen = dma->segs[seg].ds_len - segoff; + if (pagelen > len - curlen) + pagelen = len - curlen; + if (pagelen > EHCI_PAGE_SIZE - + EHCI_PAGE_OFFSET(dataphys)) + pagelen = EHCI_PAGE_SIZE - + EHCI_PAGE_OFFSET(dataphys); + segoff += pagelen; + if (segoff >= dma->segs[seg].ds_len) { + KASSERT(segoff == dma->segs[seg].ds_len, + ("ehci_alloc_sqtd_chain: overlap")); + seg++; + segoff = 0; } -#endif -#elif defined(__FreeBSD__) - /* See comment above (XXX) */ - curlen = EHCI_PAGE_SIZE - - EHCI_PAGE_MASK(dataphys); -#endif - /* XXX true for EHCI? */ - /* the length must be a multiple of the max size */ - curlen -= curlen % UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize); - DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, " - "curlen=%d\n", curlen)); -#ifdef DIAGNOSTIC - if (curlen == 0) - panic("ehci_alloc_std: curlen == 0"); -#endif + cur->qtd.qtd_buffer[i] = htole32(dataphys); + cur->qtd.qtd_buffer_hi[i] = 0; + curlen += pagelen; + + /* + * Must stop if there is any gap before or after + * the page boundary. + */ + if (EHCI_PAGE_OFFSET(dataphys + pagelen) != 0) + break; + if (seg < dma->nsegs && EHCI_PAGE_OFFSET(segoff + + dma->segs[seg].ds_addr) != 0) + break; } - DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x " - "dataphyslastpage=0x%08x len=%d curlen=%d\n", - dataphys, dataphyslastpage, - len, curlen)); + /* Adjust down to a multiple of maxp if not at the end. */ + if (curlen < len && curlen % maxp != 0) { + adj = curlen % maxp; + curlen -= adj; + KASSERT(curlen > 0, + ("ehci_alloc_sqtd_chain: need to copy")); + segoff -= adj; + if (segoff < 0) { + seg--; + segoff += dma->segs[seg].ds_len; + } + KASSERT(seg >= 0 && segoff >= 0, + ("ehci_alloc_sqtd_chain: adjust to maxp")); + } + len -= curlen; if (len != 0) { @@ -2227,19 +2226,6 @@ nextphys = EHCI_NULL; } - for (i = 0; i * EHCI_PAGE_SIZE < curlen; i++) { - ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE; - if (i != 0) /* use offset only in first buffer */ - a = EHCI_PAGE(a); - cur->qtd.qtd_buffer[i] = htole32(a); - cur->qtd.qtd_buffer_hi[i] = 0; -#ifdef DIAGNOSTIC - if (i >= EHCI_QTD_NBUFFERS) { - printf("ehci_alloc_sqtd_chain: i=%d\n", i); - goto nomem; - } -#endif - } cur->nextqtd = next; cur->qtd.qtd_next = cur->qtd.qtd_altnext = htole32(nextphys); cur->qtd.qtd_status = @@ -2252,7 +2238,6 @@ break; DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n")); offset += curlen; - dataphys = DMAADDR(dma, offset); cur = next; } cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC); Index: ehci_pci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/ehci_pci.c,v retrieving revision 1.10 diff -u -r1.10 ehci_pci.c --- ehci_pci.c 17 Mar 2004 17:50:47 -0000 1.10 +++ ehci_pci.c 6 Apr 2004 01:54:43 -0000 @@ -58,6 +58,8 @@ #include #include #include +#include +#include #include #include #include @@ -263,15 +265,37 @@ } sc->sc_ncomp = ncomp; + /* Allocate a parent dma tag for DMA maps */ + err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, + BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, + USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, + &sc->sc_bus.parent_dmatag); + if (err) { + device_printf(self, "Could not allocate parent DMA tag (%d)\n", + err); + ehci_pci_detach(self); + return ENXIO; + } + + /* Allocate a dma tag for transfer buffers */ + err = bus_dma_tag_create(sc->sc_bus.parent_dmatag, 1, 0, + BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, + BUS_SPACE_MAXSIZE_32BIT, USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, + busdma_lock_mutex, &Giant, &sc->sc_bus.buffer_dmatag); + if (err) { + device_printf(self, "Could not allocate buffer DMA tag (%d)\n", + err); + ehci_pci_detach(self); + return ENXIO; + } + err = ehci_init(sc); if (!err) err = device_probe_and_attach(sc->sc_bus.bdev); if (err) { device_printf(self, "USB init failed err=%d\n", err); -#if 0 /* TODO */ ehci_pci_detach(self); -#endif return EIO; } return 0; @@ -292,6 +316,11 @@ */ if (sc->iot && sc->ioh) bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0); + + if (sc->sc_bus.parent_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.parent_dmatag); + if (sc->sc_bus.buffer_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.buffer_dmatag); if (sc->irq_res && sc->ih) { int err = bus_teardown_intr(self, sc->irq_res, sc->ih); Index: if_axe.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/if_axe.c,v retrieving revision 1.11 diff -u -r1.11 if_axe.c --- if_axe.c 14 Mar 2004 07:12:23 -0000 1.11 +++ if_axe.c 21 Mar 2004 12:52:41 -0000 @@ -112,8 +112,6 @@ { 0, 0 } }; -Static struct usb_qdat axe_qdat; - Static int axe_match(device_ptr_t); Static int axe_attach(device_ptr_t); Static int axe_detach(device_ptr_t); @@ -507,8 +505,8 @@ ifp->if_baudrate = 10000000; ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; - axe_qdat.ifp = ifp; - axe_qdat.if_rxstart = axe_rxstart; + sc->axe_qdat.ifp = ifp; + sc->axe_qdat.if_rxstart = axe_rxstart; if (mii_phy_probe(self, &sc->axe_miibus, axe_ifmedia_upd, axe_ifmedia_sts)) { @@ -710,7 +708,7 @@ } ifp->if_ipackets++; - m->m_pkthdr.rcvif = (struct ifnet *)&axe_qdat; + m->m_pkthdr.rcvif = (struct ifnet *)&sc->axe_qdat; m->m_pkthdr.len = m->m_len = total_len; /* Put the packet on the special USB input queue. */ Index: if_axereg.h =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/if_axereg.h,v retrieving revision 1.2 diff -u -r1.2 if_axereg.h --- if_axereg.h 15 Jun 2003 21:45:43 -0000 1.2 +++ if_axereg.h 24 Sep 2003 23:25:52 -0000 @@ -168,6 +168,7 @@ unsigned char axe_ipgs[3]; unsigned char axe_phyaddrs[2]; struct timeval axe_rx_notice; + struct usb_qdat axe_qdat; }; #if 0 Index: if_kue.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/if_kue.c,v retrieving revision 1.53 diff -u -r1.53 if_kue.c --- if_kue.c 14 Mar 2004 07:12:23 -0000 1.53 +++ if_kue.c 21 Mar 2004 12:52:41 -0000 @@ -153,6 +153,8 @@ u_int16_t, char *, int); Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t); Static int kue_load_fw(struct kue_softc *); +Static void kue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, + usbd_status status); Static device_method_t kue_methods[] = { /* Device interface */ @@ -423,6 +425,7 @@ sc->kue_iface = uaa->iface; sc->kue_udev = uaa->device; sc->kue_unit = device_get_unit(self); + sc->kue_intrxfer = usbd_alloc_xfer(sc->kue_udev); id = usbd_get_interface_descriptor(uaa->iface); @@ -535,6 +538,7 @@ usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); + usbd_free_xfer(sc->kue_intrxfer); if (sc->kue_mcfilters != NULL) free(sc->kue_mcfilters, M_USBDEV); @@ -633,6 +637,30 @@ } Static void +kue_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) +{ + struct kue_softc *sc = addr; + int len; + + if (status == USBD_CANCELLED) + return; + + if (status != USBD_NORMAL_COMPLETION) { + printf("kue_intr: status=%d\n", status); + if (status == USBD_STALLED) + usbd_clear_endpoint_stall_async( + sc->kue_ep[KUE_ENDPT_INTR]); + return; + } + + usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); + + printf("kue_intr: status %d cnt %d: %02x %02x %02x %02x %02x %02x %02x %02x\n", status, len, sc->kue_idat[0], sc->kue_idat[1], sc->kue_idat[2], + sc->kue_idat[3], sc->kue_idat[4], sc->kue_idat[5], sc->kue_idat[6], + sc->kue_idat[7]); +} + +Static void kue_rxstart(struct ifnet *ifp) { struct kue_softc *sc; @@ -939,6 +967,10 @@ KUE_UNLOCK(sc); return; } + + err = usbd_open_pipe_intr(sc->kue_iface, sc->kue_ed[KUE_ENDPT_INTR], + USBD_SHORT_XFER_OK, &sc->kue_ep[KUE_ENDPT_INTR], sc, sc->kue_idat, + sizeof(sc->kue_idat), kue_intr, USBD_DEFAULT_INTERVAL); /* Start up the receive pipe. */ for (i = 0; i < KUE_RX_LIST_CNT; i++) { Index: if_kuereg.h =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/if_kuereg.h,v retrieving revision 1.13 diff -u -r1.13 if_kuereg.h --- if_kuereg.h 4 Oct 2003 21:41:01 -0000 1.13 +++ if_kuereg.h 19 Oct 2003 18:24:38 -0000 @@ -164,6 +164,8 @@ struct kue_ether_desc kue_desc; int kue_ed[KUE_ENDPT_MAX]; usbd_pipe_handle kue_ep[KUE_ENDPT_MAX]; + usbd_xfer_handle kue_intrxfer; + u_int8_t kue_idat[8]; int kue_unit; int kue_if_flags; u_int16_t kue_rxfilt; Index: ohci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/ohci.c,v retrieving revision 1.142 diff -u -r1.142 ohci.c --- ohci.c 26 Mar 2004 18:56:58 -0000 1.142 +++ ohci.c 10 Apr 2004 01:07:58 -0000 @@ -499,11 +499,11 @@ ohci_soft_td_t *sp, ohci_soft_td_t **ep) { ohci_soft_td_t *next, *cur; - ohci_physaddr_t dataphys; + ohci_physaddr_t dataphys, physend; u_int32_t tdflags; int offset = 0; - int len, curlen; - usb_dma_t *dma = &xfer->dmabuf; + int len, maxp, curlen, curlen2, seg, segoff; + struct usb_dma_mapping *dma = &xfer->dmamap; u_int16_t flags = xfer->flags; DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen)); @@ -511,22 +511,22 @@ len = alen; cur = sp; + maxp = UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize); tdflags = htole32( (rd ? OHCI_TD_IN : OHCI_TD_OUT) | (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) | OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_NOINTR); + seg = 0; + segoff = 0; for (;;) { next = ohci_alloc_std(sc); if (next == NULL) goto nomem; - dataphys = DMAADDR(dma, offset); - /* * The OHCI hardware can handle at most one 4k crossing. - * XXX - currently we only allocate contigous buffers, but - * the OHCI spec says: If during the data transfer the buffer + * The OHCI spec says: If during the data transfer the buffer * address contained in the HC's working copy of * CurrentBufferPointer crosses a 4K boundary, the upper 20 * bits of Buffer End are copied to the working value of @@ -534,36 +534,67 @@ * be the 0th byte in the same 4K page that contains the * last byte of the buffer (the 4K boundary crossing may * occur within a data packet transfer.) - * - * If/when dma has multiple segments, this will need to - * properly handle fragmenting TD's. - * - * We can describe the above using maxsegsz = 4k and nsegs = 2 - * in the future. */ - if (OHCI_PAGE(dataphys) == OHCI_PAGE(DMAADDR(dma, offset + - len - 1)) || len - (OHCI_PAGE_SIZE - - OHCI_PAGE_OFFSET(dataphys)) <= OHCI_PAGE_SIZE) { - /* we can handle it in this TD */ + KASSERT(seg < dma->nsegs, ("ohci_alloc_std_chain: overrun")); + dataphys = dma->segs[seg].ds_addr + segoff; + curlen = dma->segs[seg].ds_len - segoff; + if (curlen > len) curlen = len; + physend = dataphys + curlen - 1; + if (OHCI_PAGE(dataphys) != OHCI_PAGE(physend)) { + /* Truncate to two OHCI pages if there are more. */ + if (curlen > 2 * OHCI_PAGE_SIZE - + OHCI_PAGE_OFFSET(dataphys)) + curlen = 2 * OHCI_PAGE_SIZE - + OHCI_PAGE_OFFSET(dataphys); + if (curlen < len) + curlen -= curlen % maxp; + physend = dataphys + curlen - 1; + } else if (OHCI_PAGE_OFFSET(physend + 1) == 0 && curlen < len && + curlen + segoff == dma->segs[seg].ds_len) { + /* We can possibly include another segment. */ + KASSERT(seg + 1 < dma->nsegs, + ("ohci_alloc_std_chain: overrun2")); + seg++; + + /* Determine how much of the second segment to use. */ + curlen2 = dma->segs[seg].ds_len; + if (curlen + curlen2 > len) + curlen2 = len - curlen; + if (OHCI_PAGE(dma->segs[seg].ds_addr) != + OHCI_PAGE(dma->segs[seg].ds_addr + curlen2 - 1)) + curlen2 = OHCI_PAGE_SIZE - + OHCI_PAGE_OFFSET(dma->segs[seg].ds_addr); + if (curlen + curlen2 < len) + curlen2 -= (curlen + curlen2) % maxp; + + if (curlen2 > 0) { + /* We can include a second segment */ + segoff = curlen2; + physend = dma->segs[seg].ds_addr + curlen2 - 1; + curlen += curlen2; + } else { + /* Second segment not usable now. */ + seg--; + segoff += curlen; + } } else { - /* XXX The calculation below is wrong and could - * result in a packet that is not a multiple of the - * MaxPacketSize in the case where the buffer does not - * start on an appropriate address (like for example in - * the case of an mbuf cluster). You'll get an early - * short packet. + /* Simple case where there is just one OHCI page. */ + segoff += curlen; + } + if (curlen == 0 && len != 0) { + /* + * A maxp length packet would need to be split. + * This shouldn't be possible if PAGE_SIZE >= 4k + * and the buffer is contiguous in virtual memory. */ - /* must use multiple TDs, fill as much as possible. */ - curlen = 2 * OHCI_PAGE_SIZE - - OHCI_PAGE_OFFSET(dataphys); - /* the length must be a multiple of the max size */ - curlen -= curlen % - UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize); -#ifdef DIAGNOSTIC - if (curlen == 0) - panic("ohci_alloc_std: curlen == 0"); -#endif + panic("ohci_alloc_std_chain: XXX need to copy"); + } + if (segoff >= dma->segs[seg].ds_len) { + KASSERT(segoff == dma->segs[seg].ds_len, + ("ohci_alloc_std_chain: overlap")); + seg++; + segoff = 0; } DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x " "len=%d curlen=%d\n", @@ -574,7 +605,7 @@ cur->td.td_cbp = htole32(dataphys); cur->nexttd = next; cur->td.td_nexttd = htole32(next->physaddr); - cur->td.td_be = htole32(DMAADDR(dma, offset + curlen - 1)); + cur->td.td_be = htole32(physend); cur->len = curlen; cur->flags = OHCI_ADD_LEN; cur->xfer = xfer; @@ -1561,7 +1592,8 @@ OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY); if (xfer->flags & USBD_SHORT_XFER_OK) data->td.td_flags |= htole32(OHCI_TD_R); - data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0)); + /* XXX */ + data->td.td_cbp = htole32(xfer->dmamap.segs[0].ds_addr); data->nexttd = tail; data->td.td_nexttd = htole32(tail->physaddr); data->td.td_be = htole32(le32toh(data->td.td_cbp) + @@ -1605,7 +1637,7 @@ pipe = xfer->pipe; - p = KERNADDR(&xfer->dmabuf, 0); + p = xfer->buffer; m = min(sc->sc_noport, xfer->length * 8 - 1); memset(p, 0, xfer->length); for (i = 1; i <= m; i++) { @@ -2453,7 +2485,7 @@ index = UGETW(req->wIndex); if (len != 0) - buf = KERNADDR(&xfer->dmabuf, 0); + buf = xfer->buffer; #define C(x,y) ((x) | ((y) << 8)) switch(C(req->bRequest, req->bmRequestType)) { @@ -3062,7 +3094,8 @@ OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY); if (xfer->flags & USBD_SHORT_XFER_OK) data->td.td_flags |= htole32(OHCI_TD_R); - data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0)); + /* XXX */ + data->td.td_cbp = htole32(xfer->dmamap.segs[0].ds_addr); data->nexttd = tail; data->td.td_nexttd = htole32(tail->physaddr); data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1); @@ -3295,13 +3328,14 @@ } sitd = opipe->tail.itd; - buf = DMAADDR(&xfer->dmabuf, 0); + buf = xfer->dmamap.segs[0].ds_addr; bp0 = OHCI_PAGE(buf); offs = OHCI_PAGE_OFFSET(buf); nframes = xfer->nframes; xfer->hcpriv = sitd; for (i = ncur = 0; i < nframes; i++, ncur++) { noffs = offs + xfer->frlengths[i]; + /* XXX, fixme. */ if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */ OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */ Index: ohci_pci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/ohci_pci.c,v retrieving revision 1.39 diff -u -r1.39 ohci_pci.c --- ohci_pci.c 17 Mar 2004 17:50:47 -0000 1.39 +++ ohci_pci.c 6 Apr 2004 01:52:22 -0000 @@ -55,6 +55,8 @@ #include #include #include +#include +#include #include #include #include @@ -289,6 +291,30 @@ ohci_pci_detach(self); return ENXIO; } + + /* Allocate a parent dma tag for DMA maps */ + err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, + BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, + USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, + &sc->sc_bus.parent_dmatag); + if (err) { + device_printf(self, "Could not allocate parent DMA tag (%d)\n", + err); + ohci_pci_detach(self); + return ENXIO; + } + /* Allocate a dma tag for transfer buffers */ + err = bus_dma_tag_create(sc->sc_bus.parent_dmatag, 1, 0, + BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, + BUS_SPACE_MAXSIZE_32BIT, USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, + busdma_lock_mutex, &Giant, &sc->sc_bus.buffer_dmatag); + if (err) { + device_printf(self, "Could not allocate transfer tag (%d)\n", + err); + ohci_pci_detach(self); + return ENXIO; + } + err = ohci_init(sc); if (!err) err = device_probe_and_attach(sc->sc_bus.bdev); @@ -317,6 +343,11 @@ if (sc->iot && sc->ioh) bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); + + if (sc->sc_bus.parent_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.parent_dmatag); + if (sc->sc_bus.buffer_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.buffer_dmatag); if (sc->irq_res && sc->ih) { int err = bus_teardown_intr(self, sc->irq_res, sc->ih); Index: uhci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/uhci.c,v retrieving revision 1.150 diff -u -r1.150 uhci.c --- uhci.c 12 Jan 2004 14:18:55 -0000 1.150 +++ uhci.c 11 Apr 2004 16:57:22 -0000 @@ -69,6 +69,7 @@ #include #include #include +#include #include #if defined(DIAGNOSTIC) && defined(__i386__) #include @@ -91,7 +92,7 @@ #include /* Use bandwidth reclamation for control transfers. Some devices choke on it. */ -/*#define UHCI_CTL_LOOP */ +#define UHCI_CTL_LOOP #if defined(__FreeBSD__) #include @@ -140,6 +141,10 @@ #endif #endif +Static int timedloop = 1; +SYSCTL_INT(_debug, OID_AUTO, usb_timedloop, CTLFLAG_RW, &timedloop, 0, + "enable timed bandwidth reclaimation"); + struct uhci_pipe { struct usbd_pipe pipe; int nexttoggle; @@ -197,7 +202,8 @@ Static void uhci_free_std_chain(uhci_softc_t *, uhci_soft_td_t *, uhci_soft_td_t *); Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *, - uhci_softc_t *, int, int, u_int16_t, usb_dma_t *, + uhci_softc_t *, int, int, u_int16_t, + usbd_xfer_handle xfer, uhci_soft_td_t **, uhci_soft_td_t **); Static void uhci_poll_hub(void *); Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle); @@ -205,6 +211,7 @@ Static void uhci_idone(uhci_intr_info_t *); Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status); +Static void uhci_transfer_complete(usbd_xfer_handle xfer); Static void uhci_timeout(void *); Static void uhci_timeout_task(void *); @@ -217,6 +224,7 @@ Static int uhci_str(usb_string_descriptor_t *, int, char *); Static void uhci_add_loop(uhci_softc_t *sc); Static void uhci_rem_loop(uhci_softc_t *sc); +Static void uhci_looptimeout(void *); Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe); Static void uhci_device_isoc_enter(usbd_xfer_handle); @@ -379,13 +387,17 @@ uhci_device_isoc_done, }; -#define uhci_add_intr_info(sc, ii) \ - LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list) -#define uhci_del_intr_info(ii) \ - do { \ - LIST_REMOVE((ii), list); \ - (ii)->list.le_prev = NULL; \ - } while (0) +#define uhci_add_intr_info(sc, ii) do { \ + KASSERT(!(ii)->onlist, ("uhci_add_intr_info")); \ + (ii)->onlist = 1; \ + LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list); \ +} while (0) +#define uhci_del_intr_info(ii) do { \ + KASSERT((ii)->onlist, ("uhci_del_intr_info")); \ + (ii)->onlist = 0; \ + LIST_REMOVE((ii), list); \ + (ii)->list.le_prev = NULL; \ +} while (0) #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL) Static __inline__ uhci_soft_qh_t * @@ -491,7 +503,7 @@ clsqh = uhci_alloc_sqh(sc); if (clsqh == NULL) return (USBD_NOMEM); - clsqh->hlink = bsqh; + clsqh->hlink = chsqh; clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH); clsqh->elink = NULL; clsqh->qh.qh_elink = htole32(UHCI_PTR_T); @@ -531,6 +543,7 @@ SIMPLEQ_INIT(&sc->sc_free_xfers); usb_callout_init(sc->sc_poll_handle); + usb_callout_init(sc->sc_loop_callout); /* Set up the bus struct. */ sc->sc_bus.methods = &uhci_bus_methods; @@ -635,6 +648,8 @@ if (xfer != NULL) { memset(xfer, 0, sizeof (struct uhci_xfer)); UXFER(xfer)->iinfo.sc = sc; + usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task, + xfer); #ifdef DIAGNOSTIC UXFER(xfer)->iinfo.isdone = 1; xfer->busy_free = XFER_BUSY; @@ -648,6 +663,7 @@ { struct uhci_softc *sc = (struct uhci_softc *)bus; + KASSERT(!UXFER(xfer)->iinfo.onlist, ("uhci_freex")); #ifdef DIAGNOSTIC if (xfer->busy_free != XFER_BUSY) { printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer, @@ -730,6 +746,9 @@ if (cmd & UHCI_CMD_RS) uhci_run(sc, 0); /* in case BIOS has started it */ + uhci_globalreset(sc); + uhci_reset(sc); + /* restore saved state */ UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum); @@ -953,7 +972,8 @@ { usbd_xfer_handle xfer = addr; usbd_pipe_handle pipe = xfer->pipe; - uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; + usbd_device_handle dev = pipe->device; + uhci_softc_t *sc = (uhci_softc_t *)dev->bus; int s; u_char *p; @@ -961,7 +981,7 @@ usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer); - p = KERNADDR(&xfer->dmabuf, 0); + p = xfer->buffer; p[0] = 0; if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC)) p[0] |= 1<<1; @@ -974,9 +994,9 @@ xfer->actlen = 1; xfer->status = USBD_NORMAL_COMPLETION; s = splusb(); - xfer->device->bus->intr_context++; - usb_transfer_complete(xfer); - xfer->device->bus->intr_context--; + dev->bus->intr_context++; + uhci_transfer_complete(xfer); + dev->bus->intr_context--; splx(s); } @@ -1004,6 +1024,7 @@ #endif if (++sc->sc_loops == 1) { DPRINTFN(5,("uhci_start_loop: add\n")); + usb_uncallout(sc->sc_loop_callout, uhci_looptimeout, sc); /* Note, we don't loop back the soft pointer. */ sc->sc_last_qh->qh.qh_hlink = htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH); @@ -1018,10 +1039,25 @@ #endif if (--sc->sc_loops == 0) { DPRINTFN(5,("uhci_end_loop: remove\n")); - sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T); + if (timedloop) + usb_callout(sc->sc_loop_callout, + MS_TO_TICKS(UHCI_LOOP_DELAY_MSEC), uhci_looptimeout, + sc); + else + sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T); } } +/* Remove the bandwidth reclamation loop after a timeout has expired */ +Static void +uhci_looptimeout(void *addr) +{ + uhci_softc_t *sc = addr; + + DPRINTFN(5,("uhci_looptimeout: remove loop\n")); + sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T); +} + /* Add high speed control QH, called at splusb(). */ void uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh) @@ -1062,7 +1098,7 @@ * In this case we set the T bit and wait a little for the HC * to stop looking at the TD. */ - if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { + while (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { sqh->qh.qh_elink = htole32(UHCI_PTR_T); delay(UHCI_QH_REMOVE_DELAY); } @@ -1102,7 +1138,7 @@ DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh)); /* See comment in uhci_remove_hs_ctrl() */ - if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { + while (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { sqh->qh.qh_elink = htole32(UHCI_PTR_T); delay(UHCI_QH_REMOVE_DELAY); } @@ -1143,7 +1179,7 @@ DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh)); uhci_rem_loop(sc); /* See comment in uhci_remove_hs_ctrl() */ - if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { + while (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { sqh->qh.qh_elink = htole32(UHCI_PTR_T); delay(UHCI_QH_REMOVE_DELAY); } @@ -1268,7 +1304,7 @@ uhci_softintr(void *v) { uhci_softc_t *sc = v; - uhci_intr_info_t *ii; + uhci_intr_info_t *ii, *next_ii; DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev), sc->sc_bus.intr_context)); @@ -1286,8 +1322,11 @@ * We scan all interrupt descriptors to see if any have * completed. */ - LIST_FOREACH(ii, &sc->sc_intrhead, list) + for (ii = LIST_FIRST(&sc->sc_intrhead); ii != NULL; ii = next_ii) { + next_ii = LIST_NEXT(ii, list); uhci_check_intr(sc, ii); + KASSERT(next_ii == NULL || next_ii->onlist, ("next_ii gone")); + } #ifdef USB_USE_SOFTINTR if (sc->sc_softwake) { @@ -1305,6 +1344,7 @@ { uhci_soft_td_t *std, *lstd; u_int32_t status; + int cnt, n; DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii)); #ifdef DIAGNOSTIC @@ -1328,6 +1368,9 @@ return; } #endif + cnt = 0; + for (std = ii->stdstart; std != lstd; std = std->link.std) + cnt++; /* * If the last TD is still active we need to check whether there * is an error somewhere in the middle, or whether there was a @@ -1335,19 +1378,29 @@ */ if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) { DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii)); + n = 0; for (std = ii->stdstart; std != lstd; std = std->link.std) { status = le32toh(std->td.td_status); /* If there's an active TD the xfer isn't done. */ if (status & UHCI_TD_ACTIVE) break; /* Any kind of error makes the xfer done. */ - if (status & UHCI_TD_STALLED) + if (status & UHCI_TD_STALLED) { + printf("%p: stalled (0x%x) at %d/%d\n", ii, + status, n, cnt); goto done; + } /* We want short packets, and it is short: it's done */ if ((status & UHCI_TD_SPD) && UHCI_TD_GET_ACTLEN(status) < - UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) + UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) { +#if 0 + printf("%p: short pkt (0x%x) at %d/%d\n", ii, + status, n, cnt); +#endif goto done; + } + n++; } DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n", ii, ii->stdstart)); @@ -1471,6 +1524,7 @@ xfer->pipe->endpoint->edesc->bEndpointAddress, sbuf)); #endif + printf("status: %x\n", status); if (status == UHCI_TD_STALLED) xfer->status = USBD_STALLED; @@ -1481,7 +1535,7 @@ } end: - usb_transfer_complete(xfer); + uhci_transfer_complete(xfer); DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii)); } @@ -1504,7 +1558,6 @@ } /* Execute the abort in a process context. */ - usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer); usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task); } @@ -1643,6 +1696,7 @@ std = KERNADDR(&dma, offs); std->physaddr = DMAADDR(&dma, offs); std->link.std = sc->sc_freetds; + std->aux_dma.block = NULL; /* XXX */ sc->sc_freetds = std; } } @@ -1663,6 +1717,11 @@ } std->td.td_token = htole32(TD_IS_FREE); #endif + /* XXX */ + if (std->aux_dma.block != NULL) { + usb_freemem(&sc->sc_bus, &std->aux_dma); + std->aux_dma.block = NULL; + } std->link.std = sc->sc_freetds; sc->sc_freetds = std; } @@ -1716,12 +1775,12 @@ usbd_status uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len, - int rd, u_int16_t flags, usb_dma_t *dma, + int rd, u_int16_t flags, usbd_xfer_handle xfer, uhci_soft_td_t **sp, uhci_soft_td_t **ep) { - uhci_soft_td_t *p, *lastp; - uhci_physaddr_t lastlink; - int i, ntd, l, tog, maxp; + struct usb_dma_mapping *dma = &xfer->dmamap; + uhci_soft_td_t *p, *prevp, *startp; + int err, i, ntd, l, tog, maxp, seg, segoff; u_int32_t status; int addr = upipe->pipe.device->address; int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; @@ -1744,29 +1803,31 @@ return (USBD_NORMAL_COMPLETION); } tog = upipe->nexttoggle; - if (ntd % 2 == 0) - tog ^= 1; - upipe->nexttoggle = tog ^ 1; - lastp = NULL; - lastlink = UHCI_PTR_T; - ntd--; + prevp = NULL; + startp = NULL; status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE); if (upipe->pipe.device->speed == USB_SPEED_LOW) status |= UHCI_TD_LS; if (flags & USBD_SHORT_XFER_OK) status |= UHCI_TD_SPD; - for (i = ntd; i >= 0; i--) { + seg = 0; + segoff = 0; + for (i = 0; i < ntd; i++) { p = uhci_alloc_std(sc); if (p == NULL) { - uhci_free_std_chain(sc, lastp, NULL); + uhci_free_std_chain(sc, startp, NULL); return (USBD_NOMEM); } - p->link.std = lastp; - p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD); - lastp = p; - lastlink = p->physaddr; + p->link.std = NULL; + if (prevp != NULL) { + prevp->link.std = p; + prevp->td.td_link = htole32(p->physaddr | UHCI_PTR_VF | + UHCI_PTR_TD); + } else { + startp = p; + } p->td.td_status = htole32(status); - if (i == ntd) { + if (i == ntd - 1) { /* last TD */ l = len % maxp; if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER)) @@ -1777,10 +1838,44 @@ p->td.td_token = htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) : UHCI_TD_OUT(l, endpt, addr, tog)); - p->td.td_buffer = htole32(DMAADDR(dma, i * maxp)); + + KASSERT(seg < dma->nsegs, + ("uhci_alloc_std_chain: too few segments")); + if (l > dma->segs[seg].ds_len - segoff) { + /* UHCI can't handle non-contiguous data. */ + err = usb_allocmem(&sc->sc_bus, l, maxp, &p->aux_dma); + if (err) { + uhci_free_std_chain(sc, startp, NULL); + return (err); + } + if (!rd) + bcopy((char *)xfer->buffer + i * maxp, + KERNADDR(&p->aux_dma, 0), l); + p->td.td_buffer = htole32(DMAADDR(&p->aux_dma, 0)); + l -= dma->segs[seg].ds_len - segoff; + seg++; + KASSERT(seg < dma->nsegs, + ("uhci_alloc_std_chain: too few segments 2")); + segoff = 0; + } else { + p->td.td_buffer = htole32(dma->segs[seg].ds_addr + + segoff); + } + segoff += l; + if (segoff >= dma->segs[seg].ds_len) { + KASSERT(segoff == dma->segs[seg].ds_len, + ("uhci_alloc_std_chain: overlap")); + if (i * maxp + l != len) { + seg++; + segoff = 0; + } + } + prevp = p; tog ^= 1; } - *sp = lastp; + prevp->td.td_link = htole32(UHCI_PTR_T | UHCI_PTR_VF | UHCI_PTR_TD); + upipe->nexttoggle = tog; + *sp = startp; DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n", upipe->nexttoggle)); return (USBD_NORMAL_COMPLETION); @@ -1847,8 +1942,8 @@ upipe->u.bulk.isread = isread; upipe->u.bulk.length = len; - err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, - &xfer->dmabuf, &data, &dataend); + err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, xfer, + &data, &dataend); if (err) return (err); dataend->td.td_status |= htole32(UHCI_TD_IOC); @@ -1932,7 +2027,8 @@ s = splusb(); xfer->status = status; /* make software ignore it */ usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer); - usb_transfer_complete(xfer); + usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task); + uhci_transfer_complete(xfer); splx(s); return; } @@ -1946,6 +2042,7 @@ s = splusb(); xfer->status = status; /* make software ignore it */ usb_uncallout(xfer->timeout_handle, uhci_timeout, ii); + usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task); DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii)); for (std = ii->stdstart; std != NULL; std = std->link.std) std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC)); @@ -1978,10 +2075,48 @@ #ifdef DIAGNOSTIC ii->isdone = 1; #endif - usb_transfer_complete(xfer); + uhci_transfer_complete(xfer); splx(s); } +/* + * Perform any UHCI-specific transfer completion operations, then + * call usb_transfer_complete(). + */ +Static void +uhci_transfer_complete(usbd_xfer_handle xfer) +{ + uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; + struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; + uhci_soft_td_t *p; + int l, maxp, ofs; + + /* Copy back from any auxillary buffers after a read operation. */ + /* XXX, must be an easier way to detect reads... */ + if (((xfer->rqflags & URQ_REQUEST) && + (xfer->request.bmRequestType & UT_READ)) || + (xfer->pipe->endpoint->edesc->bEndpointAddress & UE_DIR_IN)) { + maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize); + ofs = 0; + for (p = ii->stdstart; p != NULL && ofs < xfer->actlen; + p = p->link.std) { + if (p->aux_dma.block != NULL) { + bus_dmamap_sync(p->aux_dma.block->tag, + p->aux_dma.block->map, + BUS_DMASYNC_POSTWRITE); + l = xfer->actlen - ofs; + if (l > maxp) + l = maxp; + bcopy(KERNADDR(&p->aux_dma, 0), + (char *)xfer->buffer + ofs, l); + } + ofs += maxp; + } + } + + usb_transfer_complete(xfer); +} + /* Close a device bulk pipe. */ void uhci_device_bulk_close(usbd_pipe_handle pipe) @@ -2080,9 +2215,8 @@ upipe->u.intr.isread = isread; - err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread, - xfer->flags, &xfer->dmabuf, &data, - &dataend); + err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread, xfer->flags, + xfer, &data, &dataend); if (err) return (err); dataend->td.td_status |= htole32(UHCI_TD_IOC); @@ -2220,7 +2354,7 @@ if (len != 0) { upipe->nexttoggle = 1; err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, - &xfer->dmabuf, &data, &dataend); + xfer, &data, &dataend); if (err) return (err); next = data; @@ -2348,7 +2482,7 @@ struct iso *iso = &upipe->u.iso; uhci_soft_td_t *std; u_int32_t buf, len, status; - int s, i, next, nframes; + int s, i, next, nframes, seg, segoff; DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p " "nframes=%d\n", @@ -2378,7 +2512,8 @@ xfer->status = USBD_IN_PROGRESS; UXFER(xfer)->curframe = next; - buf = DMAADDR(&xfer->dmabuf, 0); + seg = 0; + segoff = 0; status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) | UHCI_TD_ACTIVE | UHCI_TD_IOS); @@ -2389,7 +2524,17 @@ if (++next >= UHCI_VFRAMELIST_COUNT) next = 0; len = xfer->frlengths[i]; - std->td.td_buffer = htole32(buf); + KASSERT(seg < xfer->dmamap.nsegs, + ("uhci_device_isoc_enter: too few segments")); + std->td.td_buffer = htole32(xfer->dmamap.segs[seg].ds_addr + + segoff); + segoff += len; + if (segoff >= xfer->dmamap.segs[seg].ds_len) { + KASSERT(segoff == xfer->dmamap.segs[seg].ds_len, + ("uhci_device_isoc_enter: overlap")); + segoff = 0; + seg++; + } if (i == nframes - 1) status |= UHCI_TD_IOC; std->td.td_status = htole32(status); @@ -2500,7 +2645,7 @@ UXFER(xfer)->iinfo.isdone = 1; #endif /* Run callback and remove from interrupt list. */ - usb_transfer_complete(xfer); + uhci_transfer_complete(xfer); splx(s); } @@ -2611,9 +2756,10 @@ DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen)); - if (ii->xfer != xfer) + if (ii->xfer != xfer) { /* Not on interrupt list, ignore it. */ return; + } if (!uhci_active_intr_info(ii)) return; @@ -2648,6 +2794,8 @@ return; } #endif + ii->stdstart = NULL; + ii->stdend = NULL; } void @@ -2677,8 +2825,8 @@ /* This alloc cannot fail since we freed the chain above. */ uhci_alloc_std_chain(upipe, sc, xfer->length, - upipe->u.intr.isread, xfer->flags, - &xfer->dmabuf, &data, &dataend); + upipe->u.intr.isread, xfer->flags, xfer, + &data, &dataend); dataend->td.td_status |= htole32(UHCI_TD_IOC); #ifdef USB_DEBUG @@ -2706,8 +2854,11 @@ /* The ii is already on the examined list, just leave it. */ } else { DPRINTFN(5,("uhci_device_intr_done: removing\n")); - if (uhci_active_intr_info(ii)) + if (uhci_active_intr_info(ii)) { uhci_del_intr_info(ii); + ii->stdstart = NULL; + ii->stdend = NULL; + } } } @@ -2736,6 +2887,8 @@ if (upipe->u.ctl.length != 0) uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend); + ii->stdstart = NULL; + ii->stdend = NULL; DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen)); } @@ -2759,6 +2912,8 @@ uhci_remove_bulk(sc, upipe->u.bulk.sqh); uhci_free_std_chain(sc, ii->stdstart, NULL); + ii->stdstart = NULL; + ii->stdend = NULL; DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen)); } @@ -2791,7 +2946,8 @@ DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh)); /* See comment in uhci_remove_ctrl() */ - if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { + while (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { + printf("removing active intr QH\n"); sqh->qh.qh_elink = htole32(UHCI_PTR_T); delay(UHCI_QH_REMOVE_DELAY); } @@ -3159,7 +3315,7 @@ index = UGETW(req->wIndex); if (len != 0) - buf = KERNADDR(&xfer->dmabuf, 0); + buf = xfer->buffer; #define C(x,y) ((x) | ((y) << 8)) switch(C(req->bRequest, req->bmRequestType)) { @@ -3449,7 +3605,7 @@ ret: xfer->status = err; s = splusb(); - usb_transfer_complete(xfer); + uhci_transfer_complete(xfer); splx(s); return (USBD_IN_PROGRESS); } @@ -3485,7 +3641,7 @@ #ifdef DIAGNOSTIC UXFER(xfer)->iinfo.isdone = 1; #endif - usb_transfer_complete(xfer); + uhci_transfer_complete(xfer); } usbd_status Index: uhci_pci.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/uhci_pci.c,v retrieving revision 1.53 diff -u -r1.53 uhci_pci.c --- uhci_pci.c 17 Mar 2004 17:50:47 -0000 1.53 +++ uhci_pci.c 6 Apr 2004 01:52:28 -0000 @@ -54,6 +54,8 @@ #include #include #include +#include +#include #include #include #if defined(__FreeBSD__) @@ -328,6 +330,29 @@ #endif pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); + /* Allocate a parent dma tag for DMA maps */ + err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, + BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, + USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, + &sc->sc_bus.parent_dmatag); + if (err) { + device_printf(self, "Could not allocate parent DMA tag (%d)\n", + err); + uhci_pci_detach(self); + return ENXIO; + } + /* Allocate a dma tag for transfer buffers */ + err = bus_dma_tag_create(sc->sc_bus.parent_dmatag, 1, 0, + BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, + BUS_SPACE_MAXSIZE_32BIT, USB_DMA_NSEG, BUS_SPACE_MAXSIZE_32BIT, 0, + busdma_lock_mutex, &Giant, &sc->sc_bus.buffer_dmatag); + if (err) { + device_printf(self, "Could not allocate transfer tag (%d)\n", + err); + uhci_pci_detach(self); + return ENXIO; + } + err = uhci_init(sc); if (!err) err = device_probe_and_attach(sc->sc_bus.bdev); @@ -361,6 +386,11 @@ */ if (sc->iot && sc->ioh) bus_space_write_2(sc->iot, sc->ioh, UHCI_INTR, 0); + + if (sc->sc_bus.parent_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.parent_dmatag); + if (sc->sc_bus.buffer_dmatag != NULL) + bus_dma_tag_destroy(sc->sc_bus.buffer_dmatag); if (sc->irq_res && sc->ih) { int err = bus_teardown_intr(self, sc->irq_res, sc->ih); Index: uhcireg.h =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/uhcireg.h,v retrieving revision 1.21 diff -u -r1.21 uhcireg.h --- uhcireg.h 4 Jul 2003 01:50:38 -0000 1.21 +++ uhcireg.h 19 Jul 2003 00:20:00 -0000 @@ -128,6 +128,13 @@ #define UHCI_QH_REMOVE_DELAY 5 /* + * Keep the bandwidth reclaimation loop in place for this long after + * all transfers have completed. This avoids waiting until the start + * of the next 1ms frame if another transfer arrives soon. + */ +#define UHCI_LOOP_DELAY_MSEC 5 + +/* * The Queue Heads and Transfer Descriptors are accessed * by both the CPU and the USB controller which run * concurrently. This means that they have to be accessed @@ -140,8 +147,8 @@ */ typedef struct { - uhci_physaddr_t td_link; - u_int32_t td_status; + volatile uhci_physaddr_t td_link; + volatile u_int32_t td_status; #define UHCI_TD_GET_ACTLEN(s) (((s) + 1) & 0x3ff) #define UHCI_TD_ZERO_ACTLEN(t) ((t) | 0x3ff) #define UHCI_TD_BITSTUFF 0x00020000 @@ -157,7 +164,7 @@ #define UHCI_TD_GET_ERRCNT(s) (((s) >> 27) & 3) #define UHCI_TD_SET_ERRCNT(n) ((n) << 27) #define UHCI_TD_SPD 0x20000000 - u_int32_t td_token; + volatile u_int32_t td_token; #define UHCI_TD_PID_IN 0x00000069 #define UHCI_TD_PID_OUT 0x000000e1 #define UHCI_TD_PID_SETUP 0x0000002d @@ -171,7 +178,7 @@ #define UHCI_TD_SET_MAXLEN(l) (((l)-1) << 21) #define UHCI_TD_GET_MAXLEN(s) ((((s) >> 21) + 1) & 0x7ff) #define UHCI_TD_MAXLEN_MASK 0xffe00000 - u_int32_t td_buffer; + volatile u_int32_t td_buffer; } uhci_td_t; #define UHCI_TD_ERROR (UHCI_TD_BITSTUFF|UHCI_TD_CRCTO|UHCI_TD_BABBLE|UHCI_TD_DBUFFER|UHCI_TD_STALLED) @@ -186,8 +193,8 @@ UHCI_TD_SET_DT(dt)) typedef struct { - uhci_physaddr_t qh_hlink; - uhci_physaddr_t qh_elink; + volatile uhci_physaddr_t qh_hlink; + volatile uhci_physaddr_t qh_elink; } uhci_qh_t; #endif /* _DEV_PCI_UHCIREG_H_ */ Index: uhcivar.h =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/uhcivar.h,v retrieving revision 1.36 diff -u -r1.36 uhcivar.h --- uhcivar.h 15 Jul 2003 23:19:49 -0000 1.36 +++ uhcivar.h 9 Apr 2004 22:58:06 -0000 @@ -75,6 +75,7 @@ uhci_soft_td_t *stdstart; uhci_soft_td_t *stdend; LIST_ENTRY(uhci_intr_info) list; + int onlist; #ifdef DIAGNOSTIC int isdone; #endif @@ -96,6 +97,7 @@ uhci_td_t td; /* The real TD, must be first */ uhci_soft_td_qh_t link; /* soft version of the td_link field */ uhci_physaddr_t physaddr; /* TD's physical address. */ + usb_dma_t aux_dma; /* Auxillary storage if needed. */ }; /* * Make the size such that it is a multiple of UHCI_TD_ALIGN. This way @@ -155,6 +157,7 @@ uhci_soft_qh_t *sc_bulk_end; /* last bulk transfer */ uhci_soft_qh_t *sc_last_qh; /* dummy QH at the end */ u_int32_t sc_loops; /* number of QHs that wants looping */ + usb_callout_t sc_loop_callout; /* timer for delayed de-looping */ uhci_soft_td_t *sc_freetds; /* TD free list */ uhci_soft_qh_t *sc_freeqhs; /* QH free list */ Index: umass.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/umass.c,v retrieving revision 1.105 diff -u -r1.105 umass.c --- umass.c 7 Mar 2004 05:33:09 -0000 1.105 +++ umass.c 21 Mar 2004 12:52:43 -0000 @@ -437,6 +437,10 @@ UMASS_PROTO_ATAPI | UMASS_PROTO_CBI_I, FORCE_SHORT_INQUIRY }, + { 0x090a, 0x1100, RID_WILDCARD, + UMASS_PROTO_UFI | UMASS_PROTO_CBI, + NO_QUIRKS + }, { VID_EOT, PID_EOT, RID_EOT, 0, 0 } }; Index: usb_mem.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/usb_mem.c,v retrieving revision 1.5 diff -u -r1.5 usb_mem.c --- usb_mem.c 4 Oct 2003 22:13:21 -0000 1.5 +++ usb_mem.c 5 Apr 2004 23:35:31 -0000 @@ -142,7 +142,8 @@ s = splusb(); /* First check the free list. */ for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) { - if (p->tag == tag && p->size >= size && p->align >= align) { + if (p->tag == tag && p->size >= size && p->size < size * 2 && + p->align >= align) { LIST_REMOVE(p, next); usb_blk_nfree--; splx(s); @@ -229,7 +230,7 @@ usbd_status usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p) { - bus_dma_tag_t tag = bus->dmatag; + bus_dma_tag_t tag = bus->parent_dmatag; usbd_status err; struct usb_frag_dma *f; usb_dma_block_t *b; Index: usb_subr.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/usb_subr.c,v retrieving revision 1.63 diff -u -r1.63 usb_subr.c --- usb_subr.c 1 Apr 2004 18:55:28 -0000 1.63 +++ usb_subr.c 9 Apr 2004 23:55:32 -0000 @@ -524,6 +524,8 @@ break; } } + printf("usbd_fill_iface_data: endpt %d maxpkt %d\n", endpt, + UGETW(ed->wMaxPacketSize)); ifc->endpoints[endpt].refcnt = 0; p += ed->bLength; } @@ -874,6 +876,7 @@ for (confi = 0; confi < dd->bNumConfigurations; confi++) { DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n", confi)); + printf("usbd_probe_and_attach: trying config idx=%d\n", confi); err = usbd_set_config_index(dev, confi, 1); if (err) { #ifdef USB_DEBUG @@ -930,6 +933,7 @@ #endif } } + printf("found=%d\n", found); if (found != 0) { #if defined(__FreeBSD__) /* remove the last created child again; it is unused */ @@ -1095,6 +1099,12 @@ addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->speed)); + printf("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, " + "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", + addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, + dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, + dev->speed); + if (dd->bDescriptorType != UDESC_DEVICE) { /* Illegal device descriptor */ Index: usbdi.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/usbdi.c,v retrieving revision 1.85 diff -u -r1.85 usbdi.c --- usbdi.c 4 Mar 2004 20:49:03 -0000 1.85 +++ usbdi.c 11 Apr 2004 19:18:03 -0000 @@ -85,6 +85,10 @@ Static usbd_status usbd_open_pipe_ival (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int); Static int usbd_xfer_isread(usbd_xfer_handle xfer); +Static void usbd_start_transfer(void *arg, bus_dma_segment_t *segs, int nseg, + int error); +Static void usbd_alloc_callback(void *arg, bus_dma_segment_t *segs, int nseg, + int error); Static int usbd_nbuses = 0; @@ -281,7 +285,7 @@ usbd_transfer(usbd_xfer_handle xfer) { usbd_pipe_handle pipe = xfer->pipe; - usb_dma_t *dmap = &xfer->dmabuf; + struct usb_dma_mapping *dmap = &xfer->dmamap; usbd_status err; u_int size; int s; @@ -300,43 +304,36 @@ size = xfer->length; /* If there is no buffer, allocate one. */ if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) { - struct usbd_bus *bus = pipe->device->bus; + bus_dma_tag_t tag = pipe->device->bus->buffer_dmatag; #ifdef DIAGNOSTIC if (xfer->rqflags & URQ_AUTO_DMABUF) printf("usbd_transfer: has old buffer!\n"); #endif - err = bus->methods->allocm(bus, dmap, size); + err = bus_dmamap_create(tag, 0, &dmap->map); if (err) - return (err); - xfer->rqflags |= URQ_AUTO_DMABUF; - } - - /* Copy data if going out. */ - if (!(xfer->flags & USBD_NO_COPY) && size != 0 && - !usbd_xfer_isread(xfer)) - memcpy(KERNADDR(dmap, 0), xfer->buffer, size); - - err = pipe->methods->transfer(xfer); + return (USBD_NOMEM); - if (err != USBD_IN_PROGRESS && err) { - /* The transfer has not been queued, so free buffer. */ - if (xfer->rqflags & URQ_AUTO_DMABUF) { - struct usbd_bus *bus = pipe->device->bus; - - bus->methods->freem(bus, &xfer->dmabuf); + xfer->rqflags |= URQ_AUTO_DMABUF; + err = bus_dmamap_load(tag, dmap->map, xfer->buffer, size, + usbd_start_transfer, xfer, 0); + if (err != 0 && err != EINPROGRESS) { xfer->rqflags &= ~URQ_AUTO_DMABUF; + bus_dmamap_destroy(tag, dmap->map); + return (USBD_INVAL); } + } else if (size != 0) { + usbd_start_transfer(xfer, dmap->segs, dmap->nsegs, 0); + } else { + usbd_start_transfer(xfer, NULL, 0, 0); } - + if (!(xfer->flags & USBD_SYNCHRONOUS)) - return (err); + return (xfer->done ? 0 : USBD_IN_PROGRESS); /* Sync transfer, wait for completion. */ - if (err != USBD_IN_PROGRESS) - return (err); s = splusb(); - if (!xfer->done) { + while (!xfer->done) { if (pipe->device->bus->use_polling) panic("usbd_transfer: not done"); tsleep(xfer, PRIBIO, "usbsyn", 0); @@ -345,6 +342,51 @@ return (xfer->status); } +Static void +usbd_start_transfer(void *arg, bus_dma_segment_t *segs, int nseg, int error) +{ + usbd_xfer_handle xfer = arg; + usbd_pipe_handle pipe = xfer->pipe; + struct usb_dma_mapping *dmap = &xfer->dmamap; + bus_dma_tag_t tag = pipe->device->bus->buffer_dmatag; + int err, i; + + if (error != 0) { + KASSERT(xfer->rqflags & URQ_AUTO_DMABUF, + ("usbd_start_transfer: error with non-auto buf")); + if (nseg > 0) + bus_dmamap_unload(tag, dmap->map); + bus_dmamap_destroy(tag, dmap->map); + /* XXX */ + usb_insert_transfer(xfer); + xfer->status = USBD_IOERROR; + usb_transfer_complete(xfer); + return; + } + + if (segs != dmap->segs) { + for (i = 0; i < nseg; i++) + dmap->segs[i] = segs[i]; + dmap->nsegs = nseg; + } + + if (segs > 0 && !usbd_xfer_isread(xfer)) + bus_dmamap_sync(tag, dmap->map, BUS_DMASYNC_PREREAD); + err = pipe->methods->transfer(xfer); + if (err != USBD_IN_PROGRESS && err) { + if (xfer->rqflags & URQ_AUTO_DMABUF) { + bus_dmamap_unload(tag, dmap->map); + bus_dmamap_destroy(tag, dmap->map); + xfer->rqflags &= ~URQ_AUTO_DMABUF; + } + /* XXX */ + usb_insert_transfer(xfer); + xfer->status = err; + usb_transfer_complete(xfer); + return; + } +} + /* Like usbd_transfer(), but waits for completion. */ usbd_status usbd_sync_transfer(usbd_xfer_handle xfer) @@ -353,42 +395,103 @@ return (usbd_transfer(xfer)); } +struct usbd_allocstate { + usbd_xfer_handle xfer; + int done; + int error; + int waiting; +}; + void * usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size) { - struct usbd_bus *bus = xfer->device->bus; + struct usbd_allocstate allocstate; + struct usb_dma_mapping *dmap = &xfer->dmamap; + bus_dma_tag_t tag = xfer->device->bus->buffer_dmatag; + void *buf; usbd_status err; + int error, s; -#ifdef DIAGNOSTIC - if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) - printf("usbd_alloc_buffer: xfer already has a buffer\n"); -#endif - err = bus->methods->allocm(bus, &xfer->dmabuf, size); + KASSERT((xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) == 0, + ("usbd_alloc_buffer: xfer already has a buffer")); + err = bus_dmamap_create(tag, 0, &dmap->map); if (err) return (NULL); + buf = malloc(size, M_USB, M_WAITOK); + + allocstate.xfer = xfer; + allocstate.done = 0; + allocstate.error = 0; + allocstate.waiting = 0; + error = bus_dmamap_load(tag, dmap->map, buf, size, usbd_alloc_callback, + &allocstate, 0); + if (error && error != EINPROGRESS) { + bus_dmamap_destroy(tag, dmap->map); + free(buf, M_USB); + return (NULL); + } + if (error == EINPROGRESS) { + /* Wait for completion. */ + s = splusb(); + allocstate.waiting = 1; + while (!allocstate.done) + tsleep(&allocstate, PRIBIO, "usbdab", 0); + splx(s); + error = allocstate.error; + } + if (error) { + bus_dmamap_unload(tag, dmap->map); + bus_dmamap_destroy(tag, dmap->map); + free(buf, M_USB); + return (NULL); + } + + xfer->allocbuf = buf; xfer->rqflags |= URQ_DEV_DMABUF; - return (KERNADDR(&xfer->dmabuf, 0)); + return (buf); } void usbd_free_buffer(usbd_xfer_handle xfer) { -#ifdef DIAGNOSTIC - if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) { - printf("usbd_free_buffer: no buffer\n"); - return; - } -#endif - xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF); - xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf); + struct usb_dma_mapping *dmap = &xfer->dmamap; + bus_dma_tag_t tag = xfer->device->bus->buffer_dmatag; + + KASSERT((xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) == + URQ_DEV_DMABUF, ("usbd_free_buffer: no/auto buffer")); + + xfer->rqflags &= ~URQ_DEV_DMABUF; + bus_dmamap_unload(tag, dmap->map); + bus_dmamap_destroy(tag, dmap->map); + free(xfer->allocbuf, M_USB); + xfer->allocbuf = NULL; } void * usbd_get_buffer(usbd_xfer_handle xfer) { if (!(xfer->rqflags & URQ_DEV_DMABUF)) - return (0); - return (KERNADDR(&xfer->dmabuf, 0)); + return (NULL); + return (xfer->allocbuf); +} + +Static void +usbd_alloc_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error) +{ + struct usbd_allocstate *allocstate = arg; + usbd_xfer_handle xfer = allocstate->xfer; + struct usb_dma_mapping *dmap = &xfer->dmamap; + int i; + + allocstate->error = error; + if (error == 0) { + for (i = 0; i < nseg; i++) + dmap->segs[i] = segs[i]; + dmap->nsegs = nseg; + } + allocstate->done = 1; + if (allocstate->waiting) + wakeup(&allocstate); } usbd_xfer_handle @@ -746,6 +849,7 @@ pipe, xfer, pipe->methods)); /* Make the HC abort it (and invoke the callback). */ pipe->methods->abort(xfer); + KASSERT(SIMPLEQ_FIRST(&pipe->queue) != xfer, ("usbd_ar_pipe")); /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */ } pipe->aborting = 0; @@ -757,9 +861,11 @@ usb_transfer_complete(usbd_xfer_handle xfer) { usbd_pipe_handle pipe = xfer->pipe; - usb_dma_t *dmap = &xfer->dmabuf; + struct usb_dma_mapping *dmap = &xfer->dmamap; + bus_dma_tag_t tag = pipe->device->bus->buffer_dmatag; + usbd_status status; int repeat = pipe->repeat; - int polling; + int polling, xfer_flags; SPLUSBCHECK; @@ -784,23 +890,14 @@ if (polling) pipe->running = 0; - if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 && - usbd_xfer_isread(xfer)) { -#ifdef DIAGNOSTIC - if (xfer->actlen > xfer->length) { - printf("usb_transfer_complete: actlen > len %d > %d\n", - xfer->actlen, xfer->length); - xfer->actlen = xfer->length; - } -#endif - memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen); - } + if (xfer->actlen != 0 && usbd_xfer_isread(xfer)) + bus_dmamap_sync(tag, dmap->map, BUS_DMASYNC_POSTWRITE); - /* if we allocated the buffer in usbd_transfer() we free it here. */ + /* if we mapped the buffer in usbd_transfer() we unmap it here. */ if (xfer->rqflags & URQ_AUTO_DMABUF) { if (!repeat) { - struct usbd_bus *bus = pipe->device->bus; - bus->methods->freem(bus, dmap); + bus_dmamap_unload(tag, dmap->map); + bus_dmamap_destroy(tag, dmap->map); xfer->rqflags &= ~URQ_AUTO_DMABUF; } } @@ -830,30 +927,33 @@ xfer->status = USBD_SHORT_XFER; } - if (xfer->callback) - xfer->callback(xfer, xfer->priv, xfer->status); - -#ifdef DIAGNOSTIC - if (pipe->methods->done != NULL) + /* Copy any xfer fields in case the xfer goes away in the callback. */ + status = xfer->status; + xfer_flags = xfer->flags; + /* + * For repeat operations, call the callback first, as the xfer + * will not go away and the "done" method may modify it. Otherwise + * reverse the order in case the callback wants to free or reuse + * the xfer. + */ + if (repeat) { + if (xfer->callback) + xfer->callback(xfer, xfer->priv, status); pipe->methods->done(xfer); - else - printf("usb_transfer_complete: pipe->methods->done == NULL\n"); -#else - pipe->methods->done(xfer); -#endif - - if ((xfer->flags & USBD_SYNCHRONOUS) && !polling) - wakeup(xfer); + } else { + pipe->methods->done(xfer); + if (xfer->callback) + xfer->callback(xfer, xfer->priv, status); - if (!repeat) { /* XXX should we stop the queue on all errors? */ - if ((xfer->status == USBD_CANCELLED || - xfer->status == USBD_TIMEOUT) && + if ((status == USBD_CANCELLED || status == USBD_TIMEOUT) && pipe->iface != NULL) /* not control pipe */ pipe->running = 0; else usbd_start_next(pipe); } + if ((xfer_flags & USBD_SYNCHRONOUS) && !polling) + wakeup(xfer); } usbd_status @@ -874,6 +974,7 @@ xfer->busy_free = XFER_ONQU; #endif s = splusb(); + KASSERT(SIMPLEQ_FIRST(&pipe->queue) != xfer, ("usb_insert_transfer")); SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next); if (pipe->running) err = USBD_IN_PROGRESS; Index: usbdi_util.c =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/usbdi_util.c,v retrieving revision 1.31 diff -u -r1.31 usbdi_util.c --- usbdi_util.c 24 Aug 2003 17:55:55 -0000 1.31 +++ usbdi_util.c 21 Sep 2003 15:28:30 -0000 @@ -440,7 +440,7 @@ } usbd_get_xfer_status(xfer, NULL, NULL, size, &err); DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size)); - if (err) { + if (err == USBD_STALLED) { DPRINTF(("usbd_bulk_transfer: error=%d\n", err)); usbd_clear_endpoint_stall(pipe); } Index: usbdivar.h =================================================================== RCS file: /dump/FreeBSD-CVS/src/sys/dev/usb/usbdivar.h,v retrieving revision 1.40 diff -u -r1.40 usbdivar.h --- usbdivar.h 15 Jul 2003 22:42:37 -0000 1.40 +++ usbdivar.h 5 Apr 2004 23:20:49 -0000 @@ -124,7 +124,8 @@ #endif #endif - bus_dma_tag_t dmatag; /* DMA tag */ + bus_dma_tag_t parent_dmatag; /* Base DMA tag */ + bus_dma_tag_t buffer_dmatag; /* Tag for transfer buffers */ }; struct usbd_device { @@ -180,6 +181,15 @@ struct usbd_pipe_methods *methods; }; +#define USB_DMA_NSEG (btoc(MAXPHYS) + 1) + +/* DMA-capable memory buffer. */ +struct usb_dma_mapping { + bus_dma_segment_t segs[USB_DMA_NSEG]; /* The physical segments. */ + int nsegs; /* Number of segments. */ + bus_dmamap_t map; /* DMA mapping. */ +}; + struct usbd_xfer { struct usbd_pipe *pipe; void *priv; @@ -207,7 +217,8 @@ /* For memory allocation */ struct usbd_device *device; - usb_dma_t dmabuf; + struct usb_dma_mapping dmamap; + void *allocbuf; int rqflags; #define URQ_REQUEST 0x01