3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
55 #define USB_DEBUG_VAR usb_debug
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_transfer.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_util.h>
63 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_controller.h>
66 #include <dev/usb/usb_bus.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
70 static void usb_dma_tag_create(struct usb_dma_tag *, usb_size_t, usb_size_t);
71 static void usb_dma_tag_destroy(struct usb_dma_tag *);
72 static void usb_dma_lock_cb(void *, bus_dma_lock_op_t);
73 static void usb_pc_alloc_mem_cb(void *, bus_dma_segment_t *, int, int);
74 static void usb_pc_load_mem_cb(void *, bus_dma_segment_t *, int, int);
75 static void usb_pc_common_mem_cb(void *, bus_dma_segment_t *, int, int,
79 /*------------------------------------------------------------------------*
80 * usbd_get_page - lookup DMA-able memory for the given offset
82 * NOTE: Only call this function when the "page_cache" structure has
83 * been properly initialized !
84 *------------------------------------------------------------------------*/
86 usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
87 struct usb_page_search *res)
90 struct usb_page *page;
93 /* 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) {
107 page += (offset / USB_PAGE_SIZE);
109 offset %= USB_PAGE_SIZE;
111 res->length = USB_PAGE_SIZE - offset;
112 res->physaddr = page->physaddr + offset;
114 res->length = (usb_size_t)-1;
115 res->physaddr = page->physaddr + offset;
118 /* Case 1b - Non Kernel Virtual Address */
120 res->buffer = USB_ADD_BYTES(page->buffer, offset);
125 /* Case 2 - Plain PIO */
127 res->buffer = USB_ADD_BYTES(pc->buffer, offset);
128 res->length = (usb_size_t)-1;
134 /*------------------------------------------------------------------------*
135 * usb_pc_buffer_is_aligned - verify alignment
137 * This function is used to check if a page cache buffer is properly
138 * aligned to reduce the use of bounce buffers in PIO mode.
139 *------------------------------------------------------------------------*/
141 usb_pc_buffer_is_aligned(struct usb_page_cache *pc, usb_frlength_t offset,
142 usb_frlength_t len, usb_frlength_t mask)
144 struct usb_page_search buf_res;
147 usbd_get_page(pc, offset, &buf_res);
149 if (buf_res.length > len)
150 buf_res.length = len;
151 if (USB_P2U(buf_res.buffer) & mask)
153 if (buf_res.length & mask)
156 offset += buf_res.length;
157 len -= buf_res.length;
162 /*------------------------------------------------------------------------*
163 * usbd_copy_in - copy directly to DMA-able memory
164 *------------------------------------------------------------------------*/
166 usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
167 const void *ptr, usb_frlength_t len)
169 struct usb_page_search buf_res;
172 usbd_get_page(cache, offset, &buf_res);
174 if (buf_res.length > len) {
175 buf_res.length = len;
177 memcpy(buf_res.buffer, ptr, buf_res.length);
179 offset += buf_res.length;
180 len -= buf_res.length;
181 ptr = USB_ADD_BYTES(ptr, buf_res.length);
185 /*------------------------------------------------------------------------*
186 * usbd_copy_in_user - copy directly to DMA-able memory from userland
191 *------------------------------------------------------------------------*/
194 usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
195 const void *ptr, usb_frlength_t len)
197 struct usb_page_search buf_res;
201 usbd_get_page(cache, offset, &buf_res);
203 if (buf_res.length > len) {
204 buf_res.length = len;
206 error = copyin(ptr, buf_res.buffer, buf_res.length);
210 offset += buf_res.length;
211 len -= buf_res.length;
212 ptr = USB_ADD_BYTES(ptr, buf_res.length);
214 return (0); /* success */
218 /*------------------------------------------------------------------------*
219 * usbd_m_copy_in - copy a mbuf chain directly into DMA-able memory
220 *------------------------------------------------------------------------*/
222 struct usb_m_copy_in_arg {
223 struct usb_page_cache *cache;
224 usb_frlength_t dst_offset;
228 usbd_m_copy_in_cb(void *arg, void *src, uint32_t count)
230 struct usb_m_copy_in_arg *ua = arg;
232 usbd_copy_in(ua->cache, ua->dst_offset, src, count);
233 ua->dst_offset += count;
238 usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
239 struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len)
241 struct usb_m_copy_in_arg arg = {cache, dst_offset};
242 (void) m_apply(m, src_offset, src_len, &usbd_m_copy_in_cb, &arg);
246 /*------------------------------------------------------------------------*
247 * usb_uiomove - factored out code
248 *------------------------------------------------------------------------*/
251 usb_uiomove(struct usb_page_cache *pc, struct uio *uio,
252 usb_frlength_t pc_offset, usb_frlength_t len)
254 struct usb_page_search res;
258 usbd_get_page(pc, pc_offset, &res);
260 if (res.length > len) {
264 * "uiomove()" can sleep so one needs to make a wrapper,
265 * exiting the mutex and checking things
267 error = uiomove(res.buffer, res.length, uio);
272 pc_offset += res.length;
279 /*------------------------------------------------------------------------*
280 * usbd_copy_out - copy directly from DMA-able memory
281 *------------------------------------------------------------------------*/
283 usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
284 void *ptr, usb_frlength_t len)
286 struct usb_page_search res;
289 usbd_get_page(cache, offset, &res);
291 if (res.length > len) {
294 memcpy(ptr, res.buffer, res.length);
296 offset += res.length;
298 ptr = USB_ADD_BYTES(ptr, res.length);
302 /*------------------------------------------------------------------------*
303 * usbd_copy_out_user - copy directly from DMA-able memory to userland
308 *------------------------------------------------------------------------*/
311 usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
312 void *ptr, usb_frlength_t len)
314 struct usb_page_search res;
318 usbd_get_page(cache, offset, &res);
320 if (res.length > len) {
323 error = copyout(res.buffer, ptr, res.length);
327 offset += res.length;
329 ptr = USB_ADD_BYTES(ptr, res.length);
331 return (0); /* success */
335 /*------------------------------------------------------------------------*
336 * usbd_frame_zero - zero DMA-able memory
337 *------------------------------------------------------------------------*/
339 usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
342 struct usb_page_search res;
345 usbd_get_page(cache, offset, &res);
347 if (res.length > len) {
350 memset(res.buffer, 0, res.length);
352 offset += res.length;
359 /*------------------------------------------------------------------------*
360 * usb_dma_lock_cb - dummy callback
361 *------------------------------------------------------------------------*/
363 usb_dma_lock_cb(void *arg, bus_dma_lock_op_t op)
365 /* we use "mtx_owned()" instead of this function */
368 /*------------------------------------------------------------------------*
369 * usb_dma_tag_create - allocate a DMA tag
371 * NOTE: If the "align" parameter has a value of 1 the DMA-tag will
372 * allow multi-segment mappings. Else all mappings are single-segment.
373 *------------------------------------------------------------------------*/
375 usb_dma_tag_create(struct usb_dma_tag *udt,
376 usb_size_t size, usb_size_t align)
380 if (bus_dma_tag_create
381 ( /* parent */ udt->tag_parent->tag,
382 /* alignment */ align,
384 /* lowaddr */ (2ULL << (udt->tag_parent->dma_bits - 1)) - 1,
385 /* highaddr */ BUS_SPACE_MAXADDR,
387 /* filterarg */ NULL,
389 /* nsegments */ (align == 1 && size > 1) ?
390 (2 + (size / USB_PAGE_SIZE)) : 1,
391 /* maxsegsz */ (align == 1 && size > USB_PAGE_SIZE) ?
392 USB_PAGE_SIZE : size,
393 /* flags */ BUS_DMA_KEEP_PG_OFFSET,
394 /* lockfn */ &usb_dma_lock_cb,
402 /*------------------------------------------------------------------------*
403 * usb_dma_tag_free - free a DMA tag
404 *------------------------------------------------------------------------*/
406 usb_dma_tag_destroy(struct usb_dma_tag *udt)
408 bus_dma_tag_destroy(udt->tag);
411 /*------------------------------------------------------------------------*
412 * usb_pc_alloc_mem_cb - BUS-DMA callback function
413 *------------------------------------------------------------------------*/
415 usb_pc_alloc_mem_cb(void *arg, bus_dma_segment_t *segs,
418 usb_pc_common_mem_cb(arg, segs, nseg, error, 0);
421 /*------------------------------------------------------------------------*
422 * usb_pc_load_mem_cb - BUS-DMA callback function
423 *------------------------------------------------------------------------*/
425 usb_pc_load_mem_cb(void *arg, bus_dma_segment_t *segs,
428 usb_pc_common_mem_cb(arg, segs, nseg, error, 1);
431 /*------------------------------------------------------------------------*
432 * usb_pc_common_mem_cb - BUS-DMA callback function
433 *------------------------------------------------------------------------*/
435 usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
436 int nseg, int error, uint8_t isload)
438 struct usb_dma_parent_tag *uptag;
439 struct usb_page_cache *pc;
446 uptag = pc->tag_parent;
449 * XXX There is sometimes recursive locking here.
450 * XXX We should try to find a better solution.
451 * XXX Until further the "owned" variable does
461 pg->physaddr = rounddown2(segs->ds_addr, USB_PAGE_SIZE);
462 rem = segs->ds_addr & (USB_PAGE_SIZE - 1);
463 pc->page_offset_buf = rem;
464 pc->page_offset_end += rem;
469 for (x = 0; x != nseg - 1; x++) {
470 if (((segs[x].ds_addr + segs[x].ds_len) & (USB_PAGE_SIZE - 1)) ==
471 ((segs[x + 1].ds_addr & (USB_PAGE_SIZE - 1))))
474 * This check verifies there is no page offset
475 * hole between any of the segments. See the
476 * BUS_DMA_KEEP_PG_OFFSET flag.
478 DPRINTFN(0, "Page offset was not preserved\n");
484 while (pc->ismultiseg) {
485 off += USB_PAGE_SIZE;
486 if (off >= (segs->ds_len + rem)) {
496 pg->physaddr = rounddown2(segs->ds_addr + off, USB_PAGE_SIZE);
500 owned = mtx_owned(uptag->mtx);
502 USB_MTX_LOCK(uptag->mtx);
504 uptag->dma_error = (error ? 1 : 0);
506 (uptag->func) (uptag);
508 cv_broadcast(uptag->cv);
511 USB_MTX_UNLOCK(uptag->mtx);
514 /*------------------------------------------------------------------------*
515 * usb_pc_alloc_mem - allocate DMA'able memory
520 *------------------------------------------------------------------------*/
522 usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg,
523 usb_size_t size, usb_size_t align)
525 struct usb_dma_parent_tag *uptag;
526 struct usb_dma_tag *utag;
531 uptag = pc->tag_parent;
535 * The alignment must be greater or equal to the
536 * "size" else the object can be split between two
537 * memory pages and we get a problem!
539 while (align < size) {
547 * XXX BUS-DMA workaround - FIXME later:
549 * We assume that that the aligment at this point of
550 * the code is greater than or equal to the size and
551 * less than two times the size, so that if we double
552 * the size, the size will be greater than the
555 * The bus-dma system has a check for "alignment"
556 * being less than "size". If that check fails we end
557 * up using contigmalloc which is page based even for
558 * small allocations. Try to avoid that to save
559 * memory, hence we sometimes to a large number of
562 if (size <= (USB_PAGE_SIZE / 2)) {
567 /* get the correct DMA tag */
568 utag = usb_dma_tag_find(uptag, size, align);
572 /* allocate memory */
573 if (bus_dmamem_alloc(
574 utag->tag, &ptr, (BUS_DMA_WAITOK | BUS_DMA_COHERENT), &map)) {
577 /* setup page cache */
580 pc->page_offset_buf = 0;
581 pc->page_offset_end = size;
584 pc->ismultiseg = (align == 1);
586 USB_MTX_LOCK(uptag->mtx);
588 /* load memory into DMA */
589 err = bus_dmamap_load(
590 utag->tag, map, ptr, size, &usb_pc_alloc_mem_cb,
591 pc, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
593 if (err == EINPROGRESS) {
594 cv_wait(uptag->cv, uptag->mtx);
597 USB_MTX_UNLOCK(uptag->mtx);
599 if (err || uptag->dma_error) {
600 bus_dmamem_free(utag->tag, ptr, map);
603 memset(ptr, 0, size);
605 usb_pc_cpu_flush(pc);
610 /* reset most of the page cache */
612 pc->page_start = NULL;
613 pc->page_offset_buf = 0;
614 pc->page_offset_end = 0;
620 /*------------------------------------------------------------------------*
621 * usb_pc_free_mem - free DMA memory
623 * This function is NULL safe.
624 *------------------------------------------------------------------------*/
626 usb_pc_free_mem(struct usb_page_cache *pc)
628 if (pc && pc->buffer) {
629 bus_dmamap_unload(pc->tag, pc->map);
631 bus_dmamem_free(pc->tag, pc->buffer, pc->map);
637 /*------------------------------------------------------------------------*
638 * usb_pc_load_mem - load virtual memory into DMA
643 *------------------------------------------------------------------------*/
645 usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t sync)
647 /* setup page cache */
648 pc->page_offset_buf = 0;
649 pc->page_offset_end = size;
652 USB_MTX_ASSERT(pc->tag_parent->mtx, MA_OWNED);
656 struct usb_dma_parent_tag *uptag;
659 uptag = pc->tag_parent;
662 * We have to unload the previous loaded DMA
663 * pages before trying to load a new one!
665 bus_dmamap_unload(pc->tag, pc->map);
668 * Try to load memory into DMA.
670 err = bus_dmamap_load(
671 pc->tag, pc->map, pc->buffer, size,
672 &usb_pc_alloc_mem_cb, pc, BUS_DMA_WAITOK);
673 if (err == EINPROGRESS) {
674 cv_wait(uptag->cv, uptag->mtx);
677 if (err || uptag->dma_error) {
682 * We have to unload the previous loaded DMA
683 * pages before trying to load a new one!
685 bus_dmamap_unload(pc->tag, pc->map);
688 * Try to load memory into DMA. The callback
689 * will be called in all cases:
692 pc->tag, pc->map, pc->buffer, size,
693 &usb_pc_load_mem_cb, pc, BUS_DMA_WAITOK)) {
699 * Call callback so that refcount is decremented
702 pc->tag_parent->dma_error = 0;
703 (pc->tag_parent->func) (pc->tag_parent);
709 /*------------------------------------------------------------------------*
710 * usb_pc_cpu_invalidate - invalidate CPU cache
711 *------------------------------------------------------------------------*/
713 usb_pc_cpu_invalidate(struct usb_page_cache *pc)
715 if (pc->page_offset_end == pc->page_offset_buf) {
716 /* nothing has been loaded into this page cache! */
721 * TODO: We currently do XXX_POSTREAD and XXX_PREREAD at the
722 * same time, but in the future we should try to isolate the
723 * different cases to optimise the code. --HPS
725 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_POSTREAD);
726 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREREAD);
729 /*------------------------------------------------------------------------*
730 * usb_pc_cpu_flush - flush CPU cache
731 *------------------------------------------------------------------------*/
733 usb_pc_cpu_flush(struct usb_page_cache *pc)
735 if (pc->page_offset_end == pc->page_offset_buf) {
736 /* nothing has been loaded into this page cache! */
739 bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREWRITE);
742 /*------------------------------------------------------------------------*
743 * usb_pc_dmamap_create - create a DMA map
748 *------------------------------------------------------------------------*/
750 usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size)
752 struct usb_xfer_root *info;
753 struct usb_dma_tag *utag;
756 info = USB_DMATAG_TO_XROOT(pc->tag_parent);
762 utag = usb_dma_tag_find(pc->tag_parent, size, 1);
767 if (bus_dmamap_create(utag->tag, 0, &pc->map)) {
771 return 0; /* success */
776 return 1; /* failure */
779 /*------------------------------------------------------------------------*
780 * usb_pc_dmamap_destroy
782 * This function is NULL safe.
783 *------------------------------------------------------------------------*/
785 usb_pc_dmamap_destroy(struct usb_page_cache *pc)
788 bus_dmamap_destroy(pc->tag, pc->map);
794 /*------------------------------------------------------------------------*
795 * usb_dma_tag_find - factored out code
796 *------------------------------------------------------------------------*/
798 usb_dma_tag_find(struct usb_dma_parent_tag *udpt,
799 usb_size_t size, usb_size_t align)
801 struct usb_dma_tag *udt;
804 USB_ASSERT(align > 0, ("Invalid parameter align = 0\n"));
805 USB_ASSERT(size > 0, ("Invalid parameter size = 0\n"));
807 udt = udpt->utag_first;
808 nudt = udpt->utag_max;
811 if (udt->align == 0) {
812 usb_dma_tag_create(udt, size, align);
813 if (udt->tag == NULL) {
820 if ((udt->align == align) && (udt->size == size)) {
828 /*------------------------------------------------------------------------*
829 * usb_dma_tag_setup - initialise USB DMA tags
830 *------------------------------------------------------------------------*/
832 usb_dma_tag_setup(struct usb_dma_parent_tag *udpt,
833 struct usb_dma_tag *udt, bus_dma_tag_t dmat,
834 struct mtx *mtx, usb_dma_callback_t *func,
835 uint8_t ndmabits, uint8_t nudt)
837 memset(udpt, 0, sizeof(*udpt));
839 /* sanity checking */
843 /* something is corrupt */
846 /* initialise condition variable */
847 cv_init(udpt->cv, "USB DMA CV");
849 /* store some information */
853 udpt->utag_first = udt;
854 udpt->utag_max = nudt;
855 udpt->dma_bits = ndmabits;
858 memset(udt, 0, sizeof(*udt));
859 udt->tag_parent = udpt;
864 /*------------------------------------------------------------------------*
865 * usb_bus_tag_unsetup - factored out code
866 *------------------------------------------------------------------------*/
868 usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
870 struct usb_dma_tag *udt;
873 udt = udpt->utag_first;
874 nudt = udpt->utag_max;
878 /* destroy the USB DMA tag */
879 usb_dma_tag_destroy(udt);
885 if (udpt->utag_max) {
886 /* destroy the condition variable */
887 cv_destroy(udpt->cv);
891 /*------------------------------------------------------------------------*
894 * This function handles loading of virtual buffers into DMA and is
895 * only called when "dma_refcount" is zero.
896 *------------------------------------------------------------------------*/
898 usb_bdma_work_loop(struct usb_xfer_queue *pq)
900 struct usb_xfer_root *info;
901 struct usb_xfer *xfer;
902 usb_frcount_t nframes;
907 USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED);
910 /* some error happened */
911 USB_BUS_LOCK(info->bus);
912 usbd_transfer_done(xfer, 0);
913 USB_BUS_UNLOCK(info->bus);
916 if (!xfer->flags_int.bdma_setup) {
918 usb_frlength_t frlength_0;
921 xfer->flags_int.bdma_setup = 1;
923 /* reset BUS-DMA load state */
927 if (xfer->flags_int.isochronous_xfr) {
928 /* only one frame buffer */
930 frlength_0 = xfer->sumlen;
932 /* can be multiple frame buffers */
933 nframes = xfer->nframes;
934 frlength_0 = xfer->frlengths[0];
938 * Set DMA direction first. This is needed to
939 * select the correct cache invalidate and cache
942 isread = USB_GET_DATA_ISREAD(xfer);
943 pg = xfer->dma_page_ptr;
945 if (xfer->flags_int.control_xfr &&
946 xfer->flags_int.control_hdr) {
948 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
949 /* The device controller writes to memory */
950 xfer->frbuffers[0].isread = 1;
952 /* The host controller reads from memory */
953 xfer->frbuffers[0].isread = 0;
957 xfer->frbuffers[0].isread = isread;
961 * Setup the "page_start" pointer which points to an array of
962 * USB pages where information about the physical address of a
963 * page will be stored. Also initialise the "isread" field of
964 * the USB page caches.
966 xfer->frbuffers[0].page_start = pg;
968 info->dma_nframes = nframes;
969 info->dma_currframe = 0;
970 info->dma_frlength_0 = frlength_0;
972 pg += (frlength_0 / USB_PAGE_SIZE);
975 while (--nframes > 0) {
976 xfer->frbuffers[nframes].isread = isread;
977 xfer->frbuffers[nframes].page_start = pg;
979 pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE);
983 if (info->dma_error) {
984 USB_BUS_LOCK(info->bus);
985 usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED);
986 USB_BUS_UNLOCK(info->bus);
989 if (info->dma_currframe != info->dma_nframes) {
990 if (info->dma_currframe == 0) {
992 usb_pc_load_mem(xfer->frbuffers,
993 info->dma_frlength_0, 0);
996 nframes = info->dma_currframe;
997 usb_pc_load_mem(xfer->frbuffers + nframes,
998 xfer->frlengths[nframes], 0);
1001 /* advance frame index */
1002 info->dma_currframe++;
1007 usb_bdma_pre_sync(xfer);
1009 /* start loading next USB transfer, if any */
1010 usb_command_wrapper(pq, NULL);
1012 /* finally start the hardware */
1013 usbd_pipe_enter(xfer);
1016 /*------------------------------------------------------------------------*
1017 * usb_bdma_done_event
1019 * This function is called when the BUS-DMA has loaded virtual memory
1021 *------------------------------------------------------------------------*/
1023 usb_bdma_done_event(struct usb_dma_parent_tag *udpt)
1025 struct usb_xfer_root *info;
1027 info = USB_DMATAG_TO_XROOT(udpt);
1029 USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED);
1032 info->dma_error = udpt->dma_error;
1034 /* enter workloop again */
1035 usb_command_wrapper(&info->dma_q,
1039 /*------------------------------------------------------------------------*
1042 * This function handles DMA synchronisation that must be done before
1043 * an USB transfer is started.
1044 *------------------------------------------------------------------------*/
1046 usb_bdma_pre_sync(struct usb_xfer *xfer)
1048 struct usb_page_cache *pc;
1049 usb_frcount_t nframes;
1051 if (xfer->flags_int.isochronous_xfr) {
1052 /* only one frame buffer */
1055 /* can be multiple frame buffers */
1056 nframes = xfer->nframes;
1059 pc = xfer->frbuffers;
1063 usb_pc_cpu_invalidate(pc);
1065 usb_pc_cpu_flush(pc);
1071 /*------------------------------------------------------------------------*
1072 * usb_bdma_post_sync
1074 * This function handles DMA synchronisation that must be done after
1075 * an USB transfer is complete.
1076 *------------------------------------------------------------------------*/
1078 usb_bdma_post_sync(struct usb_xfer *xfer)
1080 struct usb_page_cache *pc;
1081 usb_frcount_t nframes;
1083 if (xfer->flags_int.isochronous_xfr) {
1084 /* only one frame buffer */
1087 /* can be multiple frame buffers */
1088 nframes = xfer->nframes;
1091 pc = xfer->frbuffers;
1095 usb_pc_cpu_invalidate(pc);