• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $FreeBSD: releng/12.2/stand/usb/usb_busdma_loader.c 291399 2015-11-27 18:13:28Z zbb $ */
2 /*-
3  * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <los_memory.h>
28 #include "los_vm_iomap.h"
29 #include "los_vm_map.h"
30 #include <user_copy.h>
31 
32 #include "implementation/global_implementation.h"
33 
34 #if USB_HAVE_BUSDMA
35 static void	usb_pc_common_mem_cb(struct usb_page_cache *pc,
36 							    void *vaddr, uint32_t length);
37 #endif
38 
39 void
usb_dma_cache_invalid(void * addr,unsigned int size)40 usb_dma_cache_invalid(void *addr, unsigned int size)
41 {
42 	UINTPTR start = (UINTPTR)addr & ~(USB_CACHE_ALIGN_SIZE - 1);
43 	UINTPTR end = (UINTPTR)addr + size;
44 
45 	end = ALIGN(end, USB_CACHE_ALIGN_SIZE);
46 	DCacheInvRange(start, end);
47 }
48 
49 void
usb_dma_cache_flush(void * addr,unsigned int size)50 usb_dma_cache_flush(void *addr, unsigned int size)
51 {
52 	UINTPTR start = (UINTPTR)addr & ~(USB_CACHE_ALIGN_SIZE - 1);
53 	UINTPTR end = (UINTPTR)addr + size;
54 
55 	end = ALIGN(end, USB_CACHE_ALIGN_SIZE);
56 	DCacheFlushRange(start, end);
57 }
58 
59 /*------------------------------------------------------------------------*
60  *  usbd_get_page - lookup DMA-able memory for the given offset
61  *
62  * NOTE: Only call this function when the "page_cache" structure has
63  * been properly initialized !
64  *------------------------------------------------------------------------*/
65 void
usbd_get_page(struct usb_page_cache * pc,usb_frlength_t offset,struct usb_page_search * res)66 usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
67 	struct usb_page_search *res)
68 {
69 #if USB_HAVE_BUSDMA
70 	struct usb_page *page;
71 	if (pc->page_start) {
72 		/* Case 1 - something has been loaded into DMA */
73 
74 		if (pc->buffer) {
75 
76 			/* Case 1a - Kernel Virtual Address */
77 
78 			res->buffer = USB_ADD_BYTES(pc->buffer, offset);
79 		}
80 		offset += pc->page_offset_buf;
81 
82 		/* compute destination page */
83 
84 		page = pc->page_start;
85 
86 		if (pc->ismultiseg) {
87 
88 			page += (offset / USB_PAGE_SIZE);
89 
90 			offset %= USB_PAGE_SIZE;
91 
92 			res->length = USB_PAGE_SIZE - offset;
93 			res->physaddr = page->physaddr + offset;
94 		} else {
95 			res->length = (usb_size_t)-1;
96 			res->physaddr = page->physaddr + offset;
97 		}
98 		if (!pc->buffer) {
99 
100 			/* Case 1b - Non Kernel Virtual Address */
101 
102 			res->buffer = USB_ADD_BYTES(page->buffer, offset);
103 		}
104 		return;
105 	}
106 #endif
107 	/* Case 2 - Plain PIO */
108 
109 	res->buffer = USB_ADD_BYTES(pc->buffer, offset);
110 	res->length = (usb_size_t)-1;
111 #if USB_HAVE_BUSDMA
112 	res->physaddr = 0;
113 #endif
114 }
115 
116 /*------------------------------------------------------------------------*
117  *  usbd_copy_in - copy directly to DMA-able memory
118  *------------------------------------------------------------------------*/
119 void
usbd_copy_in(struct usb_page_cache * cache,usb_frlength_t offset,const void * ptr,usb_frlength_t len)120 usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
121 	const void *ptr, usb_frlength_t len)
122 {
123 	struct usb_page_search buf_res;
124 	int ret;
125 
126 	while (len != 0) {
127 
128 		usbd_get_page(cache, offset, &buf_res);
129 
130 		if (buf_res.length > len) {
131 			buf_res.length = len;
132 		}
133 		ret = memcpy_s(buf_res.buffer, buf_res.length, ptr, buf_res.length);
134 		if (ret != EOK) {
135 			return;
136 		}
137 
138 		offset += buf_res.length;
139 		len -= buf_res.length;
140 		ptr = USB_ADD_BYTES(ptr, buf_res.length);
141 	}
142 }
143 
144 /*------------------------------------------------------------------------*
145  *  usbd_copy_out - copy directly from DMA-able memory
146  *------------------------------------------------------------------------*/
147 void
usbd_copy_out(struct usb_page_cache * cache,usb_frlength_t offset,void * ptr,usb_frlength_t len)148 usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
149 	void *ptr, usb_frlength_t len)
150 {
151 	struct usb_page_search res;
152 
153 	while (len != 0) {
154 
155 		usbd_get_page(cache, offset, &res);
156 
157 		if (res.length > len) {
158 			res.length = len;
159 		}
160 		(void)memcpy_s(ptr, len, res.buffer, res.length);
161 
162 		offset += res.length;
163 		len -= res.length;
164 		ptr = USB_ADD_BYTES(ptr, res.length);
165 	}
166 }
167 
168 int
copyin(const void * uaddr,void * kaddr,size_t len)169 copyin(const void *uaddr, void *kaddr, size_t len)
170 {
171 	size_t ret = LOS_ArchCopyFromUser(kaddr, uaddr, len);
172 	return ret ? EFAULT : 0;
173 }
174 
175 int
copyout(const void * kaddr,void * uaddr,size_t len)176 copyout(const void *kaddr, void *uaddr, size_t len)
177 {
178 	size_t ret = LOS_ArchCopyToUser(uaddr, kaddr, len);
179 	return ret ? EFAULT : 0;
180 }
181 
182 /* In user mode, the src buffer is from user */
183 int
usbd_copy_from_user(void * dest,uint32_t dest_len,const void * src,uint32_t src_len)184 usbd_copy_from_user(void *dest, uint32_t dest_len, const void *src, uint32_t src_len)
185 {
186     int ret;
187 
188     if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)src, src_len)) {
189         ret = memcpy_s(dest, dest_len, src, src_len);
190     } else {
191         ret = ((dest_len >= src_len) ? LOS_ArchCopyFromUser(dest, src, src_len) : ERANGE_AND_RESET);
192     }
193 
194     return ret ? EFAULT : 0;
195 }
196 
197 /* In user mode, the dest buffer is from user */
198 int
usbd_copy_to_user(void * dest,uint32_t dest_len,const void * src,uint32_t src_len)199 usbd_copy_to_user(void *dest, uint32_t dest_len, const void *src, uint32_t src_len)
200 {
201     int ret;
202 
203     if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)dest, dest_len)) {
204         ret = memcpy_s(dest, dest_len, src, src_len);
205     } else {
206         ret = ((dest_len >= src_len) ? LOS_ArchCopyToUser(dest, src, src_len) : ERANGE_AND_RESET);
207     }
208 
209     return ret ? EFAULT : 0;
210 }
211 
212 /*------------------------------------------------------------------------*
213  *  usbd_frame_zero - zero DMA-able memory
214  *------------------------------------------------------------------------*/
215 void
usbd_frame_zero(struct usb_page_cache * cache,usb_frlength_t offset,usb_frlength_t len)216 usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
217 	usb_frlength_t len)
218 {
219 	struct usb_page_search res;
220 
221 	while (len != 0) {
222 
223 		usbd_get_page(cache, offset, &res);
224 
225 		if (res.length > len) {
226 			res.length = len;
227 		}
228 		(void)memset_s(res.buffer, res.length, 0, res.length);
229 
230 		offset += res.length;
231 		len -= res.length;
232 	}
233 }
234 
235 #if USB_HAVE_BUSDMA
236 /*------------------------------------------------------------------------*
237  *	usb_pc_common_mem_cb - BUS-DMA callback function
238  *------------------------------------------------------------------------*/
239 static void
usb_pc_common_mem_cb(struct usb_page_cache * pc,void * dma_handle,uint32_t length)240 usb_pc_common_mem_cb(struct usb_page_cache *pc, void *dma_handle, uint32_t length)
241 {
242 	struct usb_page *pg;
243 	usb_size_t rem;
244 	bus_size_t off;
245 	bus_addr_t phys = (bus_addr_t)(UINTPTR)dma_handle;
246 
247 	pg = pc->page_start;
248 	pg->physaddr = phys & ~(USB_PAGE_SIZE - 1);
249 	rem = phys & (USB_PAGE_SIZE - 1);
250 	pc->page_offset_buf = rem;
251 	pc->page_offset_end += rem;
252 	length += rem;
253 
254 	for (off = USB_PAGE_SIZE; off < length; off += USB_PAGE_SIZE) {
255 		pg++;
256 		pg->physaddr = (phys + off) & ~(USB_PAGE_SIZE - 1);
257 	}
258 }
259 
usb_mem_cache2Uncache(void * buffer)260 void *usb_mem_cache2Uncache(void *buffer)
261 {
262 	UINTPTR addr = (UINTPTR)buffer;
263 	if ((addr >= KERNEL_VMM_BASE) && (addr < KERNEL_VMM_BASE + KERNEL_VMM_SIZE)) {
264 		return (void*)VMM_TO_UNCACHED_ADDR(addr);
265 	}
266 	return buffer;
267 }
268 
269 /*------------------------------------------------------------------------*
270  *	usb_pc_alloc_mem - allocate DMA'able memory
271  *
272  * Returns:
273  *	0: Success
274  * Else: Failure
275  *------------------------------------------------------------------------*/
276 uint8_t
usb_pc_alloc_mem(struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)277 usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg,
278 			    usb_size_t size, usb_size_t align)
279 {
280 	void *ptr;
281 	DMA_ADDR_T dma_handle;
282 
283 	/* allocate zeroed memory */
284 	if (align < USB_CACHE_ALIGN_SIZE) {
285 		ptr = LOS_DmaMemAlloc(&dma_handle, size, USB_CACHE_ALIGN_SIZE, DMA_NOCACHE);
286 	} else {
287 		ptr = LOS_DmaMemAlloc(&dma_handle, size, align, DMA_NOCACHE);
288 	}
289 	if (ptr == NULL)
290 		goto error;
291 
292 	(void)memset_s(ptr, size, 0, size);
293 	/* setup page cache */
294 	pc->buffer = (uint8_t *)ptr;
295 	pc->page_start = pg;
296 	pc->page_offset_buf = 0;
297 	pc->page_offset_end = size;
298 	pc->map = NULL;
299 	pc->tag = (bus_dma_tag_t)ptr;
300 	pc->ismultiseg = (align == 1);
301 
302 	/* compute physical address */
303 	usb_pc_common_mem_cb(pc, (void *)(UINTPTR)dma_handle, size);
304 
305 	usb_pc_cpu_flush(pc);
306 	return (0);
307 
308 error:
309 	/* reset most of the page cache */
310 	pc->buffer = NULL;
311 	pc->page_start = NULL;
312 	pc->page_offset_buf = 0;
313 	pc->page_offset_end = 0;
314 	pc->map = NULL;
315 	pc->tag = NULL;
316 	return (1);
317 }
318 
319 /*------------------------------------------------------------------------*
320  *	usb_pc_free_mem - free DMA memory
321  *
322  * This function is NULL safe.
323  *------------------------------------------------------------------------*/
324 void
usb_pc_free_mem(struct usb_page_cache * pc)325 usb_pc_free_mem(struct usb_page_cache *pc)
326 {
327 	if ((pc != NULL) && (pc->buffer != NULL)) {
328 		LOS_DmaMemFree(pc->tag);
329 		pc->buffer = NULL;
330 	}
331 }
332 
333 /*------------------------------------------------------------------------*
334  *	usb_pc_load_mem - load virtual memory into DMA
335  *
336  * Return values:
337  * 0: Success
338  * Else: Error
339  *------------------------------------------------------------------------*/
340 uint8_t
usb_pc_load_mem(struct usb_page_cache * pc,usb_size_t size,uint8_t data_sync)341 usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t data_sync)
342 {
343 	/* setup page cache */
344 	pc->page_offset_buf = 0;
345 	pc->page_offset_end = size;
346 	pc->ismultiseg = 1;
347 
348 	mtx_assert(pc->tag_parent->mtx, MA_OWNED);
349 
350 	if (size > 0) {
351 		/* compute physical address */
352 #if defined (LOSCFG_DRIVERS_HDF_USB_DDK_HOST) || defined (LOSCFG_DRIVERS_HDF_USB_DDK_DEVICE)
353 		usb_pc_common_mem_cb(pc, (void *)VMM_TO_UNCACHED_ADDR((unsigned long)pc->buffer), size);
354 #else
355 		usb_pc_common_mem_cb(pc, (void *)(UINTPTR)LOS_DmaVaddrToPaddr(pc->buffer), size);
356 #endif
357 	}
358 	if (data_sync == 0) {
359 		/*
360 		 * Call callback so that refcount is decremented
361 		 * properly:
362 		 */
363 		pc->tag_parent->dma_error = 0;
364 		(pc->tag_parent->func) (pc->tag_parent);
365 	}
366 	return (0);
367 }
368 
369 /*------------------------------------------------------------------------*
370  *	usb_pc_cpu_invalidate - invalidate CPU cache
371  *------------------------------------------------------------------------*/
372 void
usb_pc_cpu_invalidate(struct usb_page_cache * pc)373 usb_pc_cpu_invalidate(struct usb_page_cache *pc)
374 {
375 	if (pc->page_offset_end == pc->page_offset_buf) {
376 		/* nothing has been loaded into this page cache! */
377 		return;
378 	}
379 	usb_dma_cache_invalid(pc->buffer, pc->page_offset_end - pc->page_offset_buf);
380 }
381 
382 /*------------------------------------------------------------------------*
383  *	usb_pc_cpu_flush - flush CPU cache
384  *------------------------------------------------------------------------*/
385 void
usb_pc_cpu_flush(struct usb_page_cache * pc)386 usb_pc_cpu_flush(struct usb_page_cache *pc)
387 {
388 	if (pc->page_offset_end == pc->page_offset_buf) {
389 		/* nothing has been loaded into this page cache! */
390 		return;
391 	}
392 	usb_dma_cache_flush(pc->buffer, pc->page_offset_end - pc->page_offset_buf);
393 }
394 
395 /*------------------------------------------------------------------------*
396  *	usb_pc_dmamap_create - create a DMA map
397  *
398  * Returns:
399  *	0: Success
400  * Else: Failure
401  *------------------------------------------------------------------------*/
402 uint8_t
usb_pc_dmamap_create(struct usb_page_cache * pc,usb_size_t size)403 usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size)
404 {
405 	return (0);	/* NOP, success */
406 }
407 
408 /*------------------------------------------------------------------------*
409  *	usb_pc_dmamap_destroy
410  *
411  * This function is NULL safe.
412  *------------------------------------------------------------------------*/
413 void
usb_pc_dmamap_destroy(struct usb_page_cache * pc)414 usb_pc_dmamap_destroy(struct usb_page_cache *pc)
415 {
416 	/* NOP */
417 }
418 
419 /*------------------------------------------------------------------------*
420  *	usb_dma_tag_setup - initialise USB DMA tags
421  *------------------------------------------------------------------------*/
422 void
usb_dma_tag_setup(struct usb_dma_parent_tag * udpt,struct usb_dma_tag * udt,bus_dma_tag_t dmat,struct mtx * mtx,usb_dma_callback_t * func,uint8_t ndmabits,uint8_t nudt)423 usb_dma_tag_setup(struct usb_dma_parent_tag *udpt,
424 			    struct usb_dma_tag *udt, bus_dma_tag_t dmat,
425 			    struct mtx *mtx, usb_dma_callback_t *func,
426 			    uint8_t ndmabits, uint8_t nudt)
427 {
428 	(void)memset_s(udpt, sizeof(*udpt), 0, sizeof(*udpt));
429 
430 	/* sanity checking */
431 	if ((nudt == 0) ||
432 		(ndmabits == 0) ||
433 		(mtx == NULL)) {
434 		/* something is corrupt */
435 		return;
436 	}
437 	/* initialise condition variable */
438 	cv_init(udpt->cv, "USB DMA CV");
439 
440 	/* store some information */
441 	udpt->mtx = mtx;
442 	udpt->func = func;
443 	udpt->tag = dmat;
444 	udpt->utag_first = udt;
445 	udpt->utag_max = nudt;
446 	udpt->dma_bits = ndmabits;
447 
448 	while (nudt--) {
449 		(void)memset_s(udt, sizeof(*udt), 0, sizeof(*udt));
450 		udt->tag_parent = udpt;
451 		udt++;
452 	}
453 }
454 
455 /*------------------------------------------------------------------------*
456  *	usb_bus_tag_unsetup - factored out code
457  *------------------------------------------------------------------------*/
458 void
usb_dma_tag_unsetup(struct usb_dma_parent_tag * udpt)459 usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
460 {
461 	struct usb_dma_tag *udt;
462 	uint8_t nudt;
463 
464 	udt = udpt->utag_first;
465 	nudt = udpt->utag_max;
466 
467 	while (nudt--) {
468 		udt->align = 0;
469 		udt++;
470 	}
471 
472 	if (udpt->utag_max) {
473 		/* destroy the condition variable */
474 		cv_destroy(udpt->cv);
475 	}
476 }
477 
478 /*------------------------------------------------------------------------*
479  *	usb_bdma_work_loop
480  *
481  * This function handles loading of virtual buffers into DMA and is
482  * only called when "dma_refcount" is zero.
483  *------------------------------------------------------------------------*/
484 void
usb_bdma_work_loop(struct usb_xfer_queue * pq)485 usb_bdma_work_loop(struct usb_xfer_queue *pq)
486 {
487 	struct usb_xfer_root *info;
488 	struct usb_xfer *xfer;
489 	usb_frcount_t nframes;
490 
491 	xfer = pq->curr;
492 	info = xfer->xroot;
493 
494 	mtx_assert(info->xfer_mtx, MA_OWNED);
495 
496 	if (xfer->error) {
497 		/* some error happened */
498 		USB_BUS_LOCK(info->bus);
499 		usbd_transfer_done(xfer, USB_ERR_NORMAL_COMPLETION);
500 		USB_BUS_UNLOCK(info->bus);
501 		return;
502 	}
503 	if (!xfer->flags_int.bdma_setup) {
504 		struct usb_page *pg;
505 		usb_frlength_t frlength_0;
506 		uint8_t isread;
507 
508 		xfer->flags_int.bdma_setup = 1;
509 
510 		/* reset BUS-DMA load state */
511 
512 		info->dma_error = 0;
513 
514 		if (xfer->flags_int.isochronous_xfr) {
515 			/* only one frame buffer */
516 			nframes = 1;
517 			frlength_0 = xfer->sumlen;
518 		} else {
519 			/* can be multiple frame buffers */
520 			nframes = xfer->nframes;
521 			frlength_0 = xfer->frlengths[0];
522 		}
523 
524 		/*
525 		 * Set DMA direction first. This is needed to
526 		 * select the correct cache invalidate and cache
527 		 * flush operations.
528 		 */
529 		isread = USB_GET_DATA_ISREAD(xfer);
530 		pg = xfer->dma_page_ptr;
531 
532 		if (xfer->flags_int.control_xfr &&
533 		    xfer->flags_int.control_hdr) {
534 			/* special case */
535 			if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
536 				/* The device controller writes to memory */
537 				xfer->frbuffers[0].isread = 1;
538 			} else {
539 				/* The host controller reads from memory */
540 				xfer->frbuffers[0].isread = 0;
541 			}
542 		} else {
543 			/* default case */
544 			xfer->frbuffers[0].isread = isread;
545 		}
546 
547 		/*
548 		 * Setup the "page_start" pointer which points to an array of
549 		 * USB pages where information about the physical address of a
550 		 * page will be stored. Also initialise the "isread" field of
551 		 * the USB page caches.
552 		 */
553 		xfer->frbuffers[0].page_start = pg;
554 
555 		info->dma_nframes = nframes;
556 		info->dma_currframe = 0;
557 		info->dma_frlength_0 = frlength_0;
558 
559 		pg += (frlength_0 / USB_PAGE_SIZE);
560 		pg += 2;
561 
562 		while (--nframes > 0) {
563 			xfer->frbuffers[nframes].isread = isread;
564 			xfer->frbuffers[nframes].page_start = pg;
565 
566 			pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE);
567 			pg += 2;
568 		}
569 
570 	}
571 	if (info->dma_error) {
572 		USB_BUS_LOCK(info->bus);
573 		usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED);
574 		USB_BUS_UNLOCK(info->bus);
575 		return;
576 	}
577 	if (info->dma_currframe != info->dma_nframes) {
578 
579 		if (info->dma_currframe == 0) {
580 			/* special case */
581 			(void)usb_pc_load_mem(xfer->frbuffers,
582 			    info->dma_frlength_0, 0);
583 		} else {
584 			/* default case */
585 			nframes = info->dma_currframe;
586 			(void)usb_pc_load_mem(xfer->frbuffers + nframes,
587 			    xfer->frlengths[nframes], 0);
588 		}
589 
590 		/* advance frame index */
591 		info->dma_currframe++;
592 
593 		return;
594 	}
595 	/* go ahead */
596 	usb_bdma_pre_sync(xfer);
597 
598 	/* start loading next USB transfer, if any */
599 	usb_command_wrapper(pq, NULL);
600 
601 	/* finally start the hardware */
602 	usbd_pipe_enter(xfer);
603 }
604 
605 /*------------------------------------------------------------------------*
606  *	usb_bdma_done_event
607  *
608  * This function is called when the BUS-DMA has loaded virtual memory
609  * into DMA, if any.
610  *------------------------------------------------------------------------*/
611 void
usb_bdma_done_event(struct usb_dma_parent_tag * udpt)612 usb_bdma_done_event(struct usb_dma_parent_tag *udpt)
613 {
614 	struct usb_xfer_root *info;
615 
616 	info = USB_DMATAG_TO_XROOT(udpt);
617 
618 	mtx_assert(info->xfer_mtx, MA_OWNED);
619 
620 	/* copy error */
621 	info->dma_error = udpt->dma_error;
622 
623 	/* enter workloop again */
624 	usb_command_wrapper(&info->dma_q,
625 	    info->dma_q.curr);
626 }
627 
628 static usb_frcount_t
usb_bdma_frame_num(struct usb_xfer * xfer)629 usb_bdma_frame_num(struct usb_xfer *xfer)
630 {
631 	if (xfer->flags_int.isochronous_xfr) {
632 		/* only one frame buffer */
633 		return (1);
634 	} else {
635 		/* can be multiple frame buffers */
636 		return (xfer->nframes);
637 	}
638 }
639 
640 /*------------------------------------------------------------------------*
641  *	usb_bdma_pre_sync
642  *
643  * This function handles DMA synchronisation that must be done before
644  * an USB transfer is started.
645  *------------------------------------------------------------------------*/
646 void
usb_bdma_pre_sync(struct usb_xfer * xfer)647 usb_bdma_pre_sync(struct usb_xfer *xfer)
648 {
649 	struct usb_page_cache *pc;
650 	usb_frcount_t nframes;
651 
652 	nframes = usb_bdma_frame_num(xfer);
653 	pc = xfer->frbuffers;
654 
655 	while (nframes--) {
656 
657 		if (pc->isread) {
658 			usb_pc_cpu_invalidate(pc);
659 		} else {
660 			usb_pc_cpu_flush(pc);
661 		}
662 		pc++;
663 	}
664 }
665 
666 /*------------------------------------------------------------------------*
667  *	usb_bdma_post_sync
668  *
669  * This function handles DMA synchronisation that must be done after
670  * an USB transfer is complete.
671  *------------------------------------------------------------------------*/
672 void
usb_bdma_post_sync(struct usb_xfer * xfer)673 usb_bdma_post_sync(struct usb_xfer *xfer)
674 {
675 	struct usb_page_cache *pc;
676 	usb_frcount_t nframes;
677 
678 	nframes = usb_bdma_frame_num(xfer);
679 	pc = xfer->frbuffers;
680 
681 	while (nframes--) {
682 		if (pc->isread) {
683 			usb_pc_cpu_invalidate(pc);
684 		}
685 		pc++;
686 	}
687 }
688 #endif
689