3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
53 #define USB_DEBUG_VAR usb_debug
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_transfer.h>
59 #include <dev/usb/usb_device.h>
60 #include <dev/usb/usb_util.h>
61 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_controller.h>
64 #include <dev/usb/usb_bus.h>
65 #endif /* USB_GLOBAL_INCLUDE_FILE */
68 static void usb_dma_tag_create(struct usb_dma_tag *, usb_size_t, usb_size_t);
69 static void usb_dma_tag_destroy(struct usb_dma_tag *);
70 static void usb_dma_lock_cb(void *, bus_dma_lock_op_t);
71 static void usb_pc_alloc_mem_cb(void *, bus_dma_segment_t *, int, int);
72 static void usb_pc_load_mem_cb(void *, bus_dma_segment_t *, int, int);
73 static void usb_pc_common_mem_cb(void *, bus_dma_segment_t *, int, int,
77 /*------------------------------------------------------------------------*
78 * usbd_get_page - lookup DMA-able memory for the given offset
80 * NOTE: Only call this function when the "page_cache" structure has
81 * been properly initialized !
82 *------------------------------------------------------------------------*/
84 usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
85 struct usb_page_search *res)
88 struct usb_page *page;
92 /* Case 1 - something has been loaded into DMA */
96 /* Case 1a - Kernel Virtual Address */
98 res->buffer = USB_ADD_BYTES(pc->buffer, offset);
100 offset += pc->page_offset_buf;
102 /* compute destination page */
104 page = pc->page_start;
106 if (pc->ismultiseg) {
108 page += (offset / USB_PAGE_SIZE);
110 offset %= USB_PAGE_SIZE;
112 res->length = USB_PAGE_SIZE - offset;
113 res->physaddr = page->physaddr + offset;
115 res->length = (usb_size_t)-1;
116 res->physaddr = page->physaddr + offset;
120 /* Case 1b - Non Kernel Virtual Address */
122 res->buffer = USB_ADD_BYTES(page->buffer, offset);
127 /* Case 2 - Plain PIO */
129 res->buffer = USB_ADD_BYTES(pc->buffer, offset);
130 res->length = (usb_size_t)-1;
136 /*------------------------------------------------------------------------*
137 * usb_pc_buffer_is_aligned - verify alignment
139 * This function is used to check if a page cache buffer is properly
140 * aligned to reduce the use of bounce buffers in PIO mode.
141 *------------------------------------------------------------------------*/
143 usb_pc_buffer_is_aligned(struct usb_page_cache *pc, usb_frlength_t offset,
144 usb_frlength_t len, usb_frlength_t mask)
146 struct usb_page_search buf_res;
150 usbd_get_page(pc, offset, &buf_res);
152 if (buf_res.length > len)
153 buf_res.length = len;
154 if (USB_P2U(buf_res.buffer) & mask)
156 if (buf_res.length & mask)
159 offset += buf_res.length;
160 len -= buf_res.length;
165 /*------------------------------------------------------------------------*
166 * usbd_copy_in - copy directly to DMA-able memory
167 *------------------------------------------------------------------------*/
169 usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
170 const void *ptr, usb_frlength_t len)
172 struct usb_page_search buf_res;
176 usbd_get_page(cache, offset, &buf_res);
178 if (buf_res.length > len) {
179 buf_res.length = len;
181 memcpy(buf_res.buffer, ptr, buf_res.length);
183 offset += buf_res.length;
184 len -= buf_res.length;
185 ptr = USB_ADD_BYTES(ptr, buf_res.length);
189 /*------------------------------------------------------------------------*
190 * usbd_copy_in_user - copy directly to DMA-able memory from userland
195 *------------------------------------------------------------------------*/
198 usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
199 const void *ptr, usb_frlength_t len)
201 struct usb_page_search buf_res;
206 usbd_get_page(cache, offset, &buf_res);
208 if (buf_res.length > len) {
209 buf_res.length = len;
211 error = copyin(ptr, buf_res.buffer, buf_res.length);
215 offset += buf_res.length;
216 len -= buf_res.length;
217 ptr = USB_ADD_BYTES(ptr, buf_res.length);
219 return (0); /* success */
223 /*------------------------------------------------------------------------*
224 * usbd_m_copy_in - copy a mbuf chain directly into DMA-able memory
225 *------------------------------------------------------------------------*/
227 struct usb_m_copy_in_arg {
228 struct usb_page_cache *cache;
229 usb_frlength_t dst_offset;
233 usbd_m_copy_in_cb(void *arg, void *src, uint32_t count)
235 register struct usb_m_copy_in_arg *ua = arg;
237 usbd_copy_in(ua->cache, ua->dst_offset, src, count);
238 ua->dst_offset += count;
243 usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
244 struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len)
246 struct usb_m_copy_in_arg arg = {cache, dst_offset};
249 error = m_apply(m, src_offset, src_len, &usbd_m_copy_in_cb, &arg);
253 /*------------------------------------------------------------------------*
254 * usb_uiomove - factored out code
255 *------------------------------------------------------------------------*/
258 usb_uiomove(struct usb_page_cache *pc, struct uio *uio,
259 usb_frlength_t pc_offset, usb_frlength_t len)
261 struct usb_page_search res;
266 usbd_get_page(pc, pc_offset, &res);
268 if (res.length > len) {
272 * "uiomove()" can sleep so one needs to make a wrapper,
273 * exiting the mutex and checking things
275 error = uiomove(res.buffer, res.length, uio);
280 pc_offset += res.length;
287 /*------------------------------------------------------------------------*
288 * usbd_copy_out - copy directly from DMA-able memory
289 *------------------------------------------------------------------------*/
291 usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
292 void *ptr, usb_frlength_t len)
294 struct usb_page_search res;
298 usbd_get_page(cache, offset, &res);
300 if (res.length > len) {
303 memcpy(ptr, res.buffer, res.length);
305 offset += res.length;
307 ptr = USB_ADD_BYTES(ptr, res.length);
311 /*------------------------------------------------------------------------*
312 * usbd_copy_out_user - copy directly from DMA-able memory to userland
317 *------------------------------------------------------------------------*/
320 usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
321 void *ptr, usb_frlength_t len)
323 struct usb_page_search res;
328 usbd_get_page(cache, offset, &res);
330 if (res.length > len) {
333 error = copyout(res.buffer, ptr, res.length);
337 offset += res.length;
339 ptr = USB_ADD_BYTES(ptr, res.length);
341 return (0); /* success */
345 /*------------------------------------------------------------------------*
346 * usbd_frame_zero - zero DMA-able memory
347 *------------------------------------------------------------------------*/
349 usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
352 struct usb_page_search res;
356 usbd_get_page(cache, offset, &res);
358 if (res.length > len) {
361 memset(res.buffer, 0, res.length);
363 offset += res.length;
370 /*------------------------------------------------------------------------*
371 * usb_dma_lock_cb - dummy callback
372 *------------------------------------------------------------------------*/
374 usb_dma_lock_cb(void *arg, bus_dma_lock_op_t op)
376 /* we use "mtx_owned()" instead of this function */
379 /*------------------------------------------------------------------------*
380 * usb_dma_tag_create - allocate a DMA tag
382 * NOTE: If the "align" parameter has a value of 1 the DMA-tag will
383 * allow multi-segment mappings. Else all mappings are single-segment.
384 *------------------------------------------------------------------------*/
386 usb_dma_tag_create(struct usb_dma_tag *udt,
387 usb_size_t size, usb_size_t align)
391 if (bus_dma_tag_create
392 ( /* parent */ udt->tag_parent->tag,
393 /* alignment */ align,
395 /* lowaddr */ (2ULL << (udt->tag_parent->dma_bits - 1)) - 1,
396 /* highaddr */ BUS_SPACE_MAXADDR,
398 /* filterarg */ NULL,
400 /* nsegments */ (align == 1 && size > 1) ?
401 (2 + (size / USB_PAGE_SIZE)) : 1,
402 /* maxsegsz */ (align == 1 && size > USB_PAGE_SIZE) ?
403 USB_PAGE_SIZE : size,
404 /* flags */ BUS_DMA_KEEP_PG_OFFSET,
405 /* lockfn */ &usb_dma_lock_cb,
413 /*------------------------------------------------------------------------*
414 * usb_dma_tag_free - free a DMA tag
415 *------------------------------------------------------------------------*/
417 usb_dma_tag_destroy(struct usb_dma_tag *udt)
419 bus_dma_tag_destroy(udt->tag);
422 /*------------------------------------------------------------------------*
423 * usb_pc_alloc_mem_cb - BUS-DMA callback function
424 *------------------------------------------------------------------------*/
426 usb_pc_alloc_mem_cb(void *arg, bus_dma_segment_t *segs,
429 usb_pc_common_mem_cb(arg, segs, nseg, error, 0);
432 /*------------------------------------------------------------------------*
433 * usb_pc_load_mem_cb - BUS-DMA callback function
434 *------------------------------------------------------------------------*/
436 usb_pc_load_mem_cb(void *arg, bus_dma_segment_t *segs,
439 usb_pc_common_mem_cb(arg, segs, nseg, error, 1);
442 /*------------------------------------------------------------------------*
443 * usb_pc_common_mem_cb - BUS-DMA callback function
444 *------------------------------------------------------------------------*/
446 usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
447 int nseg, int error, uint8_t isload)
449 struct usb_dma_parent_tag *uptag;
450 struct usb_page_cache *pc;
457 uptag = pc->tag_parent;
460 * XXX There is sometimes recursive locking here.
461 * XXX We should try to find a better solution.
462 * XXX Until further the "owned" variable does
472 pg->physaddr = segs->ds_addr & ~(USB_PAGE_SIZE - 1);
473 rem = segs->ds_addr & (USB_PAGE_SIZE - 1);
474 pc->page_offset_buf = rem;
475 pc->page_offset_end += rem;
478 ((segs->ds_addr + segs->ds_len) & (USB_PAGE_SIZE - 1)) !=
479 ((segs + 1)->ds_addr & (USB_PAGE_SIZE - 1))) {
481 * This check verifies there is no page offset hole
482 * between the first and second segment. See the
483 * BUS_DMA_KEEP_PG_OFFSET flag.
485 DPRINTFN(0, "Page offset was not preserved\n");
490 while (pc->ismultiseg) {
491 off += USB_PAGE_SIZE;
492 if (off >= (segs->ds_len + rem)) {
502 pg->physaddr = (segs->ds_addr + off) & ~(USB_PAGE_SIZE - 1);
506 owned = mtx_owned(uptag->mtx);
508 mtx_lock(uptag->mtx);
510 uptag->dma_error = (error ? 1 : 0);
512 (uptag->func) (uptag);
514 cv_broadcast(uptag->cv);
517 mtx_unlock(uptag->mtx);
520 /*------------------------------------------------------------------------*
521 * usb_pc_alloc_mem - allocate DMA'able memory
526 *------------------------------------------------------------------------*/
528 usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg,
529 usb_size_t size, usb_size_t align)
531 struct usb_dma_parent_tag *uptag;
532 struct usb_dma_tag *utag;
537 uptag = pc->tag_parent;
541 * The alignment must be greater or equal to the
542 * "size" else the object can be split between two
543 * memory pages and we get a problem!
545 while (align < size) {
553 * XXX BUS-DMA workaround - FIXME later:
555 * We assume that that the aligment at this point of
556 * the code is greater than or equal to the size and
557 * less than two times the size, so that if we double
558 * the size, the size will be greater than the
561 * The bus-dma system has a check for "alignment"
562 * being less than "size". If that check fails we end
563 * up using contigmalloc which is page based even for
564 * small allocations. Try to avoid that to save
565 * memory, hence we sometimes to a large number of
568 if (size <= (USB_PAGE_SIZE / 2)) {
573 /* get the correct DMA tag */
574 utag = usb_dma_tag_find(uptag, size, align);
578 /* allocate memory */
579 if (bus_dmamem_alloc(
580 utag->tag, &ptr, (BUS_DMA_WAITOK | BUS_DMA_COHERENT), &map)) {
583 /* setup page cache */
586 pc->page_offset_buf = 0;
587 pc->page_offset_end = size;
590 pc->ismultiseg = (align == 1);
592 mtx_lock(uptag->mtx);
594 /* load memory into DMA */
595 err = bus_dmamap_load(
596 utag->tag, map, ptr, size, &usb_pc_alloc_mem_cb,
597 pc, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
599 if (err == EINPROGRESS) {
600 cv_wait(uptag->cv, uptag->mtx);
603 mtx_unlock(uptag->mtx);
605 if (err || uptag->dma_error) {
606 bus_dmamem_free(utag->tag, ptr, map);
609 memset(ptr, 0, size);
611 usb_pc_cpu_flush(pc);
616 /* reset most of the page cache */
618 pc->page_start = NULL;
619 pc->page_offset_buf = 0;
620 pc->page_offset_end = 0;
626 /*------------------------------------------------------------------------*
627 * usb_pc_free_mem - free DMA memory
629 * This function is NULL safe.
630 *------------------------------------------------------------------------*/
632 usb_pc_free_mem(struct usb_page_cache *pc)
634 if (pc && pc->buffer) {
636 bus_dmamap_unload(pc->tag, pc->map);
638 bus_dmamem_free(pc->tag, pc->buffer, pc->map);
644 /*------------------------------------------------------------------------*
645 * usb_pc_load_mem - load virtual memory into DMA
650 *------------------------------------------------------------------------*/
652 usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t sync)
654 /* setup page cache */
655 pc->page_offset_buf = 0;
656 pc->page_offset_end = size;
659 mtx_assert(pc->tag_parent->mtx, MA_OWNED);
663 struct usb_dma_parent_tag *uptag;
666 uptag = pc->tag_parent;
669 * We have to unload the previous loaded DMA
670 * pages before trying to load a new one!
672 bus_dmamap_unload(pc->tag, pc->map);
675 * Try to load memory into DMA.
677 err = bus_dmamap_load(
678 pc->tag, pc->map, pc->buffer, size,
679 &usb_pc_alloc_mem_cb, pc, BUS_DMA_WAITOK);
680 if (err == EINPROGRESS) {
681 cv_wait(uptag->cv, uptag->mtx);
684 if (err || uptag->dma_error) {
690 * We have to unload the previous loaded DMA
691 * pages before trying to load a new one!
693 bus_dmamap_unload(pc->tag, pc->map);
696 * Try to load memory into DMA. The callback
697 * will be called in all cases:
700 pc->tag, pc->map, pc->buffer, size,
701 &usb_pc_load_mem_cb, pc, BUS_DMA_WAITOK)) {
707 * Call callback so that refcount is decremented
710 pc->tag_parent->dma_error = 0;
711 (pc->tag_parent->func) (pc->tag_parent);
717 /*------------------------------------------------------------------------*
718 * usb_pc_cpu_invalidate - invalidate CPU cache
719 *------------------------------------------------------------------------*/
721 usb_pc_cpu_invalidate(struct usb_page_cache *pc)
723 if (pc->page_offset_end == pc->page_offset_buf) {
724 /* nothing has been loaded into this page cache! */
729 * TODO: We currently do XXX_POSTREAD and XXX_PREREAD at the
730 * same time, but in the future we should try to isolate the
731 * different cases to optimise the code. --HPS
733 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_POSTREAD);
734 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREREAD);
737 /*------------------------------------------------------------------------*
738 * usb_pc_cpu_flush - flush CPU cache
739 *------------------------------------------------------------------------*/
741 usb_pc_cpu_flush(struct usb_page_cache *pc)
743 if (pc->page_offset_end == pc->page_offset_buf) {
744 /* nothing has been loaded into this page cache! */
747 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREWRITE);
750 /*------------------------------------------------------------------------*
751 * usb_pc_dmamap_create - create a DMA map
756 *------------------------------------------------------------------------*/
758 usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size)
760 struct usb_xfer_root *info;
761 struct usb_dma_tag *utag;
764 info = USB_DMATAG_TO_XROOT(pc->tag_parent);
770 utag = usb_dma_tag_find(pc->tag_parent, size, 1);
775 if (bus_dmamap_create(utag->tag, 0, &pc->map)) {
779 return 0; /* success */
784 return 1; /* failure */
787 /*------------------------------------------------------------------------*
788 * usb_pc_dmamap_destroy
790 * This function is NULL safe.
791 *------------------------------------------------------------------------*/
793 usb_pc_dmamap_destroy(struct usb_page_cache *pc)
796 bus_dmamap_destroy(pc->tag, pc->map);
802 /*------------------------------------------------------------------------*
803 * usb_dma_tag_find - factored out code
804 *------------------------------------------------------------------------*/
806 usb_dma_tag_find(struct usb_dma_parent_tag *udpt,
807 usb_size_t size, usb_size_t align)
809 struct usb_dma_tag *udt;
812 USB_ASSERT(align > 0, ("Invalid parameter align = 0\n"));
813 USB_ASSERT(size > 0, ("Invalid parameter size = 0\n"));
815 udt = udpt->utag_first;
816 nudt = udpt->utag_max;
820 if (udt->align == 0) {
821 usb_dma_tag_create(udt, size, align);
822 if (udt->tag == NULL) {
829 if ((udt->align == align) && (udt->size == size)) {
837 /*------------------------------------------------------------------------*
838 * usb_dma_tag_setup - initialise USB DMA tags
839 *------------------------------------------------------------------------*/
841 usb_dma_tag_setup(struct usb_dma_parent_tag *udpt,
842 struct usb_dma_tag *udt, bus_dma_tag_t dmat,
843 struct mtx *mtx, usb_dma_callback_t *func,
844 uint8_t ndmabits, uint8_t nudt)
846 memset(udpt, 0, sizeof(*udpt));
848 /* sanity checking */
852 /* something is corrupt */
855 /* initialise condition variable */
856 cv_init(udpt->cv, "USB DMA CV");
858 /* store some information */
862 udpt->utag_first = udt;
863 udpt->utag_max = nudt;
864 udpt->dma_bits = ndmabits;
867 memset(udt, 0, sizeof(*udt));
868 udt->tag_parent = udpt;
873 /*------------------------------------------------------------------------*
874 * usb_bus_tag_unsetup - factored out code
875 *------------------------------------------------------------------------*/
877 usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
879 struct usb_dma_tag *udt;
882 udt = udpt->utag_first;
883 nudt = udpt->utag_max;
888 /* destroy the USB DMA tag */
889 usb_dma_tag_destroy(udt);
895 if (udpt->utag_max) {
896 /* destroy the condition variable */
897 cv_destroy(udpt->cv);
901 /*------------------------------------------------------------------------*
904 * This function handles loading of virtual buffers into DMA and is
905 * only called when "dma_refcount" is zero.
906 *------------------------------------------------------------------------*/
908 usb_bdma_work_loop(struct usb_xfer_queue *pq)
910 struct usb_xfer_root *info;
911 struct usb_xfer *xfer;
912 usb_frcount_t nframes;
917 mtx_assert(info->xfer_mtx, MA_OWNED);
920 /* some error happened */
921 USB_BUS_LOCK(info->bus);
922 usbd_transfer_done(xfer, 0);
923 USB_BUS_UNLOCK(info->bus);
926 if (!xfer->flags_int.bdma_setup) {
928 usb_frlength_t frlength_0;
931 xfer->flags_int.bdma_setup = 1;
933 /* reset BUS-DMA load state */
937 if (xfer->flags_int.isochronous_xfr) {
938 /* only one frame buffer */
940 frlength_0 = xfer->sumlen;
942 /* can be multiple frame buffers */
943 nframes = xfer->nframes;
944 frlength_0 = xfer->frlengths[0];
948 * Set DMA direction first. This is needed to
949 * select the correct cache invalidate and cache
952 isread = USB_GET_DATA_ISREAD(xfer);
953 pg = xfer->dma_page_ptr;
955 if (xfer->flags_int.control_xfr &&
956 xfer->flags_int.control_hdr) {
958 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
959 /* The device controller writes to memory */
960 xfer->frbuffers[0].isread = 1;
962 /* The host controller reads from memory */
963 xfer->frbuffers[0].isread = 0;
967 xfer->frbuffers[0].isread = isread;
971 * Setup the "page_start" pointer which points to an array of
972 * USB pages where information about the physical address of a
973 * page will be stored. Also initialise the "isread" field of
974 * the USB page caches.
976 xfer->frbuffers[0].page_start = pg;
978 info->dma_nframes = nframes;
979 info->dma_currframe = 0;
980 info->dma_frlength_0 = frlength_0;
982 pg += (frlength_0 / USB_PAGE_SIZE);
985 while (--nframes > 0) {
986 xfer->frbuffers[nframes].isread = isread;
987 xfer->frbuffers[nframes].page_start = pg;
989 pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE);
994 if (info->dma_error) {
995 USB_BUS_LOCK(info->bus);
996 usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED);
997 USB_BUS_UNLOCK(info->bus);
1000 if (info->dma_currframe != info->dma_nframes) {
1002 if (info->dma_currframe == 0) {
1004 usb_pc_load_mem(xfer->frbuffers,
1005 info->dma_frlength_0, 0);
1008 nframes = info->dma_currframe;
1009 usb_pc_load_mem(xfer->frbuffers + nframes,
1010 xfer->frlengths[nframes], 0);
1013 /* advance frame index */
1014 info->dma_currframe++;
1019 usb_bdma_pre_sync(xfer);
1021 /* start loading next USB transfer, if any */
1022 usb_command_wrapper(pq, NULL);
1024 /* finally start the hardware */
1025 usbd_pipe_enter(xfer);
1028 /*------------------------------------------------------------------------*
1029 * usb_bdma_done_event
1031 * This function is called when the BUS-DMA has loaded virtual memory
1033 *------------------------------------------------------------------------*/
1035 usb_bdma_done_event(struct usb_dma_parent_tag *udpt)
1037 struct usb_xfer_root *info;
1039 info = USB_DMATAG_TO_XROOT(udpt);
1041 mtx_assert(info->xfer_mtx, MA_OWNED);
1044 info->dma_error = udpt->dma_error;
1046 /* enter workloop again */
1047 usb_command_wrapper(&info->dma_q,
1051 /*------------------------------------------------------------------------*
1054 * This function handles DMA synchronisation that must be done before
1055 * an USB transfer is started.
1056 *------------------------------------------------------------------------*/
1058 usb_bdma_pre_sync(struct usb_xfer *xfer)
1060 struct usb_page_cache *pc;
1061 usb_frcount_t nframes;
1063 if (xfer->flags_int.isochronous_xfr) {
1064 /* only one frame buffer */
1067 /* can be multiple frame buffers */
1068 nframes = xfer->nframes;
1071 pc = xfer->frbuffers;
1076 usb_pc_cpu_invalidate(pc);
1078 usb_pc_cpu_flush(pc);
1084 /*------------------------------------------------------------------------*
1085 * usb_bdma_post_sync
1087 * This function handles DMA synchronisation that must be done after
1088 * an USB transfer is complete.
1089 *------------------------------------------------------------------------*/
1091 usb_bdma_post_sync(struct usb_xfer *xfer)
1093 struct usb_page_cache *pc;
1094 usb_frcount_t nframes;
1096 if (xfer->flags_int.isochronous_xfr) {
1097 /* only one frame buffer */
1100 /* can be multiple frame buffers */
1101 nframes = xfer->nframes;
1104 pc = xfer->frbuffers;
1108 usb_pc_cpu_invalidate(pc);