1/* $FreeBSD: stable/11/sys/dev/usb/usb_transfer.c 363665 2020-07-29 14:33:31Z markj $ */
2/*-
3 * Copyright (c) 2008 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#ifdef USB_GLOBAL_INCLUDE_FILE
28#include USB_GLOBAL_INCLUDE_FILE
29#else
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>
37#include <sys/bus.h>
38#include <sys/module.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/condvar.h>
42#include <sys/sysctl.h>
43#include <sys/sx.h>
44#include <sys/unistd.h>
45#include <sys/callout.h>
46#include <sys/malloc.h>
47#include <sys/priv.h>
48#include <sys/proc.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52#include <dev/usb/usbdi_util.h>
53
54#define	USB_DEBUG_VAR usb_debug
55
56#include <dev/usb/usb_core.h>
57#include <dev/usb/usb_busdma.h>
58#include <dev/usb/usb_process.h>
59#include <dev/usb/usb_transfer.h>
60#include <dev/usb/usb_device.h>
61#include <dev/usb/usb_debug.h>
62#include <dev/usb/usb_util.h>
63
64#include <dev/usb/usb_controller.h>
65#include <dev/usb/usb_bus.h>
66#include <dev/usb/usb_pf.h>
67#endif			/* USB_GLOBAL_INCLUDE_FILE */
68
69struct usb_std_packet_size {
70	struct {
71		uint16_t min;		/* inclusive */
72		uint16_t max;		/* inclusive */
73	}	range;
74
75	uint16_t fixed[4];
76};
77
78static usb_callback_t usb_request_callback;
79
80static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = {
81
82	/* This transfer is used for generic control endpoint transfers */
83
84	[0] = {
85		.type = UE_CONTROL,
86		.endpoint = 0x00,	/* Control endpoint */
87		.direction = UE_DIR_ANY,
88		.bufsize = USB_EP0_BUFSIZE,	/* bytes */
89		.flags = {.proxy_buffer = 1,},
90		.callback = &usb_request_callback,
91		.usb_mode = USB_MODE_DUAL,	/* both modes */
92	},
93
94	/* This transfer is used for generic clear stall only */
95
96	[1] = {
97		.type = UE_CONTROL,
98		.endpoint = 0x00,	/* Control pipe */
99		.direction = UE_DIR_ANY,
100		.bufsize = sizeof(struct usb_device_request),
101		.callback = &usb_do_clear_stall_callback,
102		.timeout = 1000,	/* 1 second */
103		.interval = 50,	/* 50ms */
104		.usb_mode = USB_MODE_HOST,
105	},
106};
107
108static const struct usb_config usb_control_ep_quirk_cfg[USB_CTRL_XFER_MAX] = {
109
110	/* This transfer is used for generic control endpoint transfers */
111
112	[0] = {
113		.type = UE_CONTROL,
114		.endpoint = 0x00,	/* Control endpoint */
115		.direction = UE_DIR_ANY,
116		.bufsize = 65535,	/* bytes */
117		.callback = &usb_request_callback,
118		.usb_mode = USB_MODE_DUAL,	/* both modes */
119	},
120
121	/* This transfer is used for generic clear stall only */
122
123	[1] = {
124		.type = UE_CONTROL,
125		.endpoint = 0x00,	/* Control pipe */
126		.direction = UE_DIR_ANY,
127		.bufsize = sizeof(struct usb_device_request),
128		.callback = &usb_do_clear_stall_callback,
129		.timeout = 1000,	/* 1 second */
130		.interval = 50,	/* 50ms */
131		.usb_mode = USB_MODE_HOST,
132	},
133};
134
135/* function prototypes */
136
137static void	usbd_update_max_frame_size(struct usb_xfer *);
138static void	usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t);
139static void	usbd_control_transfer_init(struct usb_xfer *);
140static int	usbd_setup_ctrl_transfer(struct usb_xfer *);
141static void	usb_callback_proc(struct usb_proc_msg *);
142static void	usbd_callback_ss_done_defer(struct usb_xfer *);
143static void	usbd_callback_wrapper(struct usb_xfer_queue *);
144static void	usbd_transfer_start_cb(void *);
145static uint8_t	usbd_callback_wrapper_sub(struct usb_xfer *);
146static void	usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
147		    uint8_t type, enum usb_dev_speed speed);
148
149/*------------------------------------------------------------------------*
150 *	usb_request_callback
151 *------------------------------------------------------------------------*/
152static void
153usb_request_callback(struct usb_xfer *xfer, usb_error_t error)
154{
155	if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
156		usb_handle_request_callback(xfer, error);
157	else
158		usbd_do_request_callback(xfer, error);
159}
160
161/*------------------------------------------------------------------------*
162 *	usbd_update_max_frame_size
163 *
164 * This function updates the maximum frame size, hence high speed USB
165 * can transfer multiple consecutive packets.
166 *------------------------------------------------------------------------*/
167static void
168usbd_update_max_frame_size(struct usb_xfer *xfer)
169{
170	/* compute maximum frame size */
171	/* this computation should not overflow 16-bit */
172	/* max = 15 * 1024 */
173
174	xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count;
175}
176
177/*------------------------------------------------------------------------*
178 *	usbd_get_dma_delay
179 *
180 * The following function is called when we need to
181 * synchronize with DMA hardware.
182 *
183 * Returns:
184 *    0: no DMA delay required
185 * Else: milliseconds of DMA delay
186 *------------------------------------------------------------------------*/
187usb_timeout_t
188usbd_get_dma_delay(struct usb_device *udev)
189{
190	const struct usb_bus_methods *mtod;
191	uint32_t temp;
192
193	mtod = udev->bus->methods;
194	temp = 0;
195
196	if (mtod->get_dma_delay) {
197		(mtod->get_dma_delay) (udev, &temp);
198		/*
199		 * Round up and convert to milliseconds. Note that we use
200		 * 1024 milliseconds per second. to save a division.
201		 */
202		temp += 0x3FF;
203		temp /= 0x400;
204	}
205	return (temp);
206}
207
208/*------------------------------------------------------------------------*
209 *	usbd_transfer_setup_sub_malloc
210 *
211 * This function will allocate one or more DMA'able memory chunks
212 * according to "size", "align" and "count" arguments. "ppc" is
213 * pointed to a linear array of USB page caches afterwards.
214 *
215 * If the "align" argument is equal to "1" a non-contiguous allocation
216 * can happen. Else if the "align" argument is greater than "1", the
217 * allocation will always be contiguous in memory.
218 *
219 * Returns:
220 *    0: Success
221 * Else: Failure
222 *------------------------------------------------------------------------*/
223#if USB_HAVE_BUSDMA
224uint8_t
225usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm,
226    struct usb_page_cache **ppc, usb_size_t size, usb_size_t align,
227    usb_size_t count)
228{
229	struct usb_page_cache *pc;
230	struct usb_page *pg;
231	void *buf;
232	usb_size_t n_dma_pc;
233	usb_size_t n_dma_pg;
234	usb_size_t n_obj;
235	usb_size_t x;
236	usb_size_t y;
237	usb_size_t r;
238	usb_size_t z;
239
240	USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n",
241	    align));
242	USB_ASSERT(size > 0, ("Invalid size = 0\n"));
243
244	if (count == 0) {
245		return (0);		/* nothing to allocate */
246	}
247	/*
248	 * Make sure that the size is aligned properly.
249	 */
250	size = -((-size) & (-align));
251
252	/*
253	 * Try multi-allocation chunks to reduce the number of DMA
254	 * allocations, hence DMA allocations are slow.
255	 */
256	if (align == 1) {
257		/* special case - non-cached multi page DMA memory */
258		n_dma_pc = count;
259		n_dma_pg = (2 + (size / USB_PAGE_SIZE));
260		n_obj = 1;
261	} else if (size >= USB_PAGE_SIZE) {
262		n_dma_pc = count;
263		n_dma_pg = 1;
264		n_obj = 1;
265	} else {
266		/* compute number of objects per page */
267#ifdef USB_DMA_SINGLE_ALLOC
268		n_obj = 1;
269#else
270		n_obj = (USB_PAGE_SIZE / size);
271#endif
272		/*
273		 * Compute number of DMA chunks, rounded up
274		 * to nearest one:
275		 */
276		n_dma_pc = howmany(count, n_obj);
277		n_dma_pg = 1;
278	}
279
280	/*
281	 * DMA memory is allocated once, but mapped twice. That's why
282	 * there is one list for auto-free and another list for
283	 * non-auto-free which only holds the mapping and not the
284	 * allocation.
285	 */
286	if (parm->buf == NULL) {
287		/* reserve memory (auto-free) */
288		parm->dma_page_ptr += n_dma_pc * n_dma_pg;
289		parm->dma_page_cache_ptr += n_dma_pc;
290
291		/* reserve memory (no-auto-free) */
292		parm->dma_page_ptr += count * n_dma_pg;
293		parm->xfer_page_cache_ptr += count;
294		return (0);
295	}
296	for (x = 0; x != n_dma_pc; x++) {
297		/* need to initialize the page cache */
298		parm->dma_page_cache_ptr[x].tag_parent =
299		    &parm->curr_xfer->xroot->dma_parent_tag;
300	}
301	for (x = 0; x != count; x++) {
302		/* need to initialize the page cache */
303		parm->xfer_page_cache_ptr[x].tag_parent =
304		    &parm->curr_xfer->xroot->dma_parent_tag;
305	}
306
307	if (ppc != NULL) {
308		if (n_obj != 1)
309			*ppc = parm->xfer_page_cache_ptr;
310		else
311			*ppc = parm->dma_page_cache_ptr;
312	}
313	r = count;			/* set remainder count */
314	z = n_obj * size;		/* set allocation size */
315	pc = parm->xfer_page_cache_ptr;
316	pg = parm->dma_page_ptr;
317
318	if (n_obj == 1) {
319	    /*
320	     * Avoid mapping memory twice if only a single object
321	     * should be allocated per page cache:
322	     */
323	    for (x = 0; x != n_dma_pc; x++) {
324		if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
325		    pg, z, align)) {
326			return (1);	/* failure */
327		}
328		/* Make room for one DMA page cache and "n_dma_pg" pages */
329		parm->dma_page_cache_ptr++;
330		pg += n_dma_pg;
331	    }
332	} else {
333	    for (x = 0; x != n_dma_pc; x++) {
334
335		if (r < n_obj) {
336			/* compute last remainder */
337			z = r * size;
338			n_obj = r;
339		}
340		if (usb_pc_alloc_mem(parm->dma_page_cache_ptr,
341		    pg, z, align)) {
342			return (1);	/* failure */
343		}
344		/* Set beginning of current buffer */
345		buf = parm->dma_page_cache_ptr->buffer;
346		/* Make room for one DMA page cache and "n_dma_pg" pages */
347		parm->dma_page_cache_ptr++;
348		pg += n_dma_pg;
349
350		for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) {
351
352			/* Load sub-chunk into DMA */
353			if (usb_pc_dmamap_create(pc, size)) {
354				return (1);	/* failure */
355			}
356			pc->buffer = USB_ADD_BYTES(buf, y * size);
357			pc->page_start = pg;
358
359			mtx_lock(pc->tag_parent->mtx);
360			if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) {
361				mtx_unlock(pc->tag_parent->mtx);
362				return (1);	/* failure */
363			}
364			mtx_unlock(pc->tag_parent->mtx);
365		}
366	    }
367	}
368
369	parm->xfer_page_cache_ptr = pc;
370	parm->dma_page_ptr = pg;
371	return (0);
372}
373#endif
374
375/*------------------------------------------------------------------------*
376 *	usbd_get_max_frame_length
377 *
378 * This function returns the maximum single frame length as computed by
379 * usbd_transfer_setup(). It is useful when computing buffer sizes for
380 * devices having multiple alternate settings. The SuperSpeed endpoint
381 * companion pointer is allowed to be NULL.
382 *------------------------------------------------------------------------*/
383uint32_t
384usbd_get_max_frame_length(const struct usb_endpoint_descriptor *edesc,
385    const struct usb_endpoint_ss_comp_descriptor *ecomp,
386    enum usb_dev_speed speed)
387{
388	uint32_t max_packet_size;
389	uint32_t max_packet_count;
390	uint8_t type;
391
392	max_packet_size = UGETW(edesc->wMaxPacketSize);
393	max_packet_count = 1;
394	type = (edesc->bmAttributes & UE_XFERTYPE);
395
396	switch (speed) {
397	case USB_SPEED_HIGH:
398		switch (type) {
399		case UE_ISOCHRONOUS:
400		case UE_INTERRUPT:
401			max_packet_count +=
402			    (max_packet_size >> 11) & 3;
403
404			/* check for invalid max packet count */
405			if (max_packet_count > 3)
406				max_packet_count = 3;
407			break;
408		default:
409			break;
410		}
411		max_packet_size &= 0x7FF;
412		break;
413	case USB_SPEED_SUPER:
414		max_packet_count += (max_packet_size >> 11) & 3;
415
416		if (ecomp != NULL)
417			max_packet_count += ecomp->bMaxBurst;
418
419		if ((max_packet_count == 0) ||
420		    (max_packet_count > 16))
421			max_packet_count = 16;
422
423		switch (type) {
424		case UE_CONTROL:
425			max_packet_count = 1;
426			break;
427		case UE_ISOCHRONOUS:
428			if (ecomp != NULL) {
429				uint8_t mult;
430
431				mult = UE_GET_SS_ISO_MULT(
432				    ecomp->bmAttributes) + 1;
433				if (mult > 3)
434					mult = 3;
435
436				max_packet_count *= mult;
437			}
438			break;
439		default:
440			break;
441		}
442		max_packet_size &= 0x7FF;
443		break;
444	default:
445		break;
446	}
447	return (max_packet_size * max_packet_count);
448}
449
450/*------------------------------------------------------------------------*
451 *	usbd_transfer_setup_sub - transfer setup subroutine
452 *
453 * This function must be called from the "xfer_setup" callback of the
454 * USB Host or Device controller driver when setting up an USB
455 * transfer. This function will setup correct packet sizes, buffer
456 * sizes, flags and more, that are stored in the "usb_xfer"
457 * structure.
458 *------------------------------------------------------------------------*/
459void
460usbd_transfer_setup_sub(struct usb_setup_params *parm)
461{
462	enum {
463		REQ_SIZE = 8,
464		MIN_PKT = 8,
465	};
466	struct usb_xfer *xfer = parm->curr_xfer;
467	const struct usb_config *setup = parm->curr_setup;
468	struct usb_endpoint_ss_comp_descriptor *ecomp;
469	struct usb_endpoint_descriptor *edesc;
470	struct usb_std_packet_size std_size;
471	usb_frcount_t n_frlengths;
472	usb_frcount_t n_frbuffers;
473	usb_frcount_t x;
474	uint16_t maxp_old;
475	uint8_t type;
476	uint8_t zmps;
477
478	/*
479	 * Sanity check. The following parameters must be initialized before
480	 * calling this function.
481	 */
482	if ((parm->hc_max_packet_size == 0) ||
483	    (parm->hc_max_packet_count == 0) ||
484	    (parm->hc_max_frame_size == 0)) {
485		parm->err = USB_ERR_INVAL;
486		goto done;
487	}
488	edesc = xfer->endpoint->edesc;
489	ecomp = xfer->endpoint->ecomp;
490
491	type = (edesc->bmAttributes & UE_XFERTYPE);
492
493	xfer->flags = setup->flags;
494	xfer->nframes = setup->frames;
495	xfer->timeout = setup->timeout;
496	xfer->callback = setup->callback;
497	xfer->interval = setup->interval;
498	xfer->endpointno = edesc->bEndpointAddress;
499	xfer->max_packet_size = UGETW(edesc->wMaxPacketSize);
500	xfer->max_packet_count = 1;
501	/* make a shadow copy: */
502	xfer->flags_int.usb_mode = parm->udev->flags.usb_mode;
503
504	parm->bufsize = setup->bufsize;
505
506	switch (parm->speed) {
507	case USB_SPEED_HIGH:
508		switch (type) {
509		case UE_ISOCHRONOUS:
510		case UE_INTERRUPT:
511			xfer->max_packet_count +=
512			    (xfer->max_packet_size >> 11) & 3;
513
514			/* check for invalid max packet count */
515			if (xfer->max_packet_count > 3)
516				xfer->max_packet_count = 3;
517			break;
518		default:
519			break;
520		}
521		xfer->max_packet_size &= 0x7FF;
522		break;
523	case USB_SPEED_SUPER:
524		xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3;
525
526		if (ecomp != NULL)
527			xfer->max_packet_count += ecomp->bMaxBurst;
528
529		if ((xfer->max_packet_count == 0) ||
530		    (xfer->max_packet_count > 16))
531			xfer->max_packet_count = 16;
532
533		switch (type) {
534		case UE_CONTROL:
535			xfer->max_packet_count = 1;
536			break;
537		case UE_ISOCHRONOUS:
538			if (ecomp != NULL) {
539				uint8_t mult;
540
541				mult = UE_GET_SS_ISO_MULT(
542				    ecomp->bmAttributes) + 1;
543				if (mult > 3)
544					mult = 3;
545
546				xfer->max_packet_count *= mult;
547			}
548			break;
549		default:
550			break;
551		}
552		xfer->max_packet_size &= 0x7FF;
553		break;
554	default:
555		break;
556	}
557	/* range check "max_packet_count" */
558
559	if (xfer->max_packet_count > parm->hc_max_packet_count) {
560		xfer->max_packet_count = parm->hc_max_packet_count;
561	}
562
563	/* store max packet size value before filtering */
564
565	maxp_old = xfer->max_packet_size;
566
567	/* filter "wMaxPacketSize" according to HC capabilities */
568
569	if ((xfer->max_packet_size > parm->hc_max_packet_size) ||
570	    (xfer->max_packet_size == 0)) {
571		xfer->max_packet_size = parm->hc_max_packet_size;
572	}
573	/* filter "wMaxPacketSize" according to standard sizes */
574
575	usbd_get_std_packet_size(&std_size, type, parm->speed);
576
577	if (std_size.range.min || std_size.range.max) {
578
579		if (xfer->max_packet_size < std_size.range.min) {
580			xfer->max_packet_size = std_size.range.min;
581		}
582		if (xfer->max_packet_size > std_size.range.max) {
583			xfer->max_packet_size = std_size.range.max;
584		}
585	} else {
586
587		if (xfer->max_packet_size >= std_size.fixed[3]) {
588			xfer->max_packet_size = std_size.fixed[3];
589		} else if (xfer->max_packet_size >= std_size.fixed[2]) {
590			xfer->max_packet_size = std_size.fixed[2];
591		} else if (xfer->max_packet_size >= std_size.fixed[1]) {
592			xfer->max_packet_size = std_size.fixed[1];
593		} else {
594			/* only one possibility left */
595			xfer->max_packet_size = std_size.fixed[0];
596		}
597	}
598
599	/*
600	 * Check if the max packet size was outside its allowed range
601	 * and clamped to a valid value:
602	 */
603	if (maxp_old != xfer->max_packet_size)
604		xfer->flags_int.maxp_was_clamped = 1;
605
606	/* compute "max_frame_size" */
607
608	usbd_update_max_frame_size(xfer);
609
610	/* check interrupt interval and transfer pre-delay */
611
612	if (type == UE_ISOCHRONOUS) {
613
614		uint16_t frame_limit;
615
616		xfer->interval = 0;	/* not used, must be zero */
617		xfer->flags_int.isochronous_xfr = 1;	/* set flag */
618
619		if (xfer->timeout == 0) {
620			/*
621			 * set a default timeout in
622			 * case something goes wrong!
623			 */
624			xfer->timeout = 1000 / 4;
625		}
626		switch (parm->speed) {
627		case USB_SPEED_LOW:
628		case USB_SPEED_FULL:
629			frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER;
630			xfer->fps_shift = 0;
631			break;
632		default:
633			frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER;
634			xfer->fps_shift = edesc->bInterval;
635			if (xfer->fps_shift > 0)
636				xfer->fps_shift--;
637			if (xfer->fps_shift > 3)
638				xfer->fps_shift = 3;
639			if (xfer->flags.pre_scale_frames != 0)
640				xfer->nframes <<= (3 - xfer->fps_shift);
641			break;
642		}
643
644		if (xfer->nframes > frame_limit) {
645			/*
646			 * this is not going to work
647			 * cross hardware
648			 */
649			parm->err = USB_ERR_INVAL;
650			goto done;
651		}
652		if (xfer->nframes == 0) {
653			/*
654			 * this is not a valid value
655			 */
656			parm->err = USB_ERR_ZERO_NFRAMES;
657			goto done;
658		}
659	} else {
660
661		/*
662		 * If a value is specified use that else check the
663		 * endpoint descriptor!
664		 */
665		if (type == UE_INTERRUPT) {
666
667			uint32_t temp;
668
669			if (xfer->interval == 0) {
670
671				xfer->interval = edesc->bInterval;
672
673				switch (parm->speed) {
674				case USB_SPEED_LOW:
675				case USB_SPEED_FULL:
676					break;
677				default:
678					/* 125us -> 1ms */
679					if (xfer->interval < 4)
680						xfer->interval = 1;
681					else if (xfer->interval > 16)
682						xfer->interval = (1 << (16 - 4));
683					else
684						xfer->interval =
685						    (1 << (xfer->interval - 4));
686					break;
687				}
688			}
689
690			if (xfer->interval == 0) {
691				/*
692				 * One millisecond is the smallest
693				 * interval we support:
694				 */
695				xfer->interval = 1;
696			}
697
698			xfer->fps_shift = 0;
699			temp = 1;
700
701			while ((temp != 0) && (temp < xfer->interval)) {
702				xfer->fps_shift++;
703				temp *= 2;
704			}
705
706			switch (parm->speed) {
707			case USB_SPEED_LOW:
708			case USB_SPEED_FULL:
709				break;
710			default:
711				xfer->fps_shift += 3;
712				break;
713			}
714		}
715	}
716
717	/*
718	 * NOTE: we do not allow "max_packet_size" or "max_frame_size"
719	 * to be equal to zero when setting up USB transfers, hence
720	 * this leads to a lot of extra code in the USB kernel.
721	 */
722
723	if ((xfer->max_frame_size == 0) ||
724	    (xfer->max_packet_size == 0)) {
725
726		zmps = 1;
727
728		if ((parm->bufsize <= MIN_PKT) &&
729		    (type != UE_CONTROL) &&
730		    (type != UE_BULK)) {
731
732			/* workaround */
733			xfer->max_packet_size = MIN_PKT;
734			xfer->max_packet_count = 1;
735			parm->bufsize = 0;	/* automatic setup length */
736			usbd_update_max_frame_size(xfer);
737
738		} else {
739			parm->err = USB_ERR_ZERO_MAXP;
740			goto done;
741		}
742
743	} else {
744		zmps = 0;
745	}
746
747	/*
748	 * check if we should setup a default
749	 * length:
750	 */
751
752	if (parm->bufsize == 0) {
753
754		parm->bufsize = xfer->max_frame_size;
755
756		if (type == UE_ISOCHRONOUS) {
757			parm->bufsize *= xfer->nframes;
758		}
759	}
760	/*
761	 * check if we are about to setup a proxy
762	 * type of buffer:
763	 */
764
765	if (xfer->flags.proxy_buffer) {
766
767		/* round bufsize up */
768
769		parm->bufsize += (xfer->max_frame_size - 1);
770
771		if (parm->bufsize < xfer->max_frame_size) {
772			/* length wrapped around */
773			parm->err = USB_ERR_INVAL;
774			goto done;
775		}
776		/* subtract remainder */
777
778		parm->bufsize -= (parm->bufsize % xfer->max_frame_size);
779
780		/* add length of USB device request structure, if any */
781
782		if (type == UE_CONTROL) {
783			parm->bufsize += REQ_SIZE;	/* SETUP message */
784		}
785	}
786	xfer->max_data_length = parm->bufsize;
787
788	/* Setup "n_frlengths" and "n_frbuffers" */
789
790	if (type == UE_ISOCHRONOUS) {
791		n_frlengths = xfer->nframes;
792		n_frbuffers = 1;
793	} else {
794
795		if (type == UE_CONTROL) {
796			xfer->flags_int.control_xfr = 1;
797			if (xfer->nframes == 0) {
798				if (parm->bufsize <= REQ_SIZE) {
799					/*
800					 * there will never be any data
801					 * stage
802					 */
803					xfer->nframes = 1;
804				} else {
805					xfer->nframes = 2;
806				}
807			}
808		} else {
809			if (xfer->nframes == 0) {
810				xfer->nframes = 1;
811			}
812		}
813
814		n_frlengths = xfer->nframes;
815		n_frbuffers = xfer->nframes;
816	}
817
818	/*
819	 * check if we have room for the
820	 * USB device request structure:
821	 */
822
823	if (type == UE_CONTROL) {
824
825		if (xfer->max_data_length < REQ_SIZE) {
826			/* length wrapped around or too small bufsize */
827			parm->err = USB_ERR_INVAL;
828			goto done;
829		}
830		xfer->max_data_length -= REQ_SIZE;
831	}
832	/*
833	 * Setup "frlengths" and shadow "frlengths" for keeping the
834	 * initial frame lengths when a USB transfer is complete. This
835	 * information is useful when computing isochronous offsets.
836	 */
837	xfer->frlengths = parm->xfer_length_ptr;
838	parm->xfer_length_ptr += 2 * n_frlengths;
839
840	/* setup "frbuffers" */
841	xfer->frbuffers = parm->xfer_page_cache_ptr;
842	parm->xfer_page_cache_ptr += n_frbuffers;
843
844	/* initialize max frame count */
845	xfer->max_frame_count = xfer->nframes;
846
847	/*
848	 * check if we need to setup
849	 * a local buffer:
850	 */
851
852	if (!xfer->flags.ext_buffer) {
853#if USB_HAVE_BUSDMA
854		struct usb_page_search page_info;
855		struct usb_page_cache *pc;
856
857		if (usbd_transfer_setup_sub_malloc(parm,
858		    &pc, parm->bufsize, 1, 1)) {
859			parm->err = USB_ERR_NOMEM;
860		} else if (parm->buf != NULL) {
861
862			usbd_get_page(pc, 0, &page_info);
863
864			xfer->local_buffer = page_info.buffer;
865
866			usbd_xfer_set_frame_offset(xfer, 0, 0);
867
868			if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
869				usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
870			}
871		}
872#else
873		/* align data */
874		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
875
876		if (parm->buf != NULL) {
877			xfer->local_buffer =
878			    USB_ADD_BYTES(parm->buf, parm->size[0]);
879
880			usbd_xfer_set_frame_offset(xfer, 0, 0);
881
882			if ((type == UE_CONTROL) && (n_frbuffers > 1)) {
883				usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1);
884			}
885		}
886		parm->size[0] += parm->bufsize;
887
888		/* align data again */
889		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
890#endif
891	}
892	/*
893	 * Compute maximum buffer size
894	 */
895
896	if (parm->bufsize_max < parm->bufsize) {
897		parm->bufsize_max = parm->bufsize;
898	}
899#if USB_HAVE_BUSDMA
900	if (xfer->flags_int.bdma_enable) {
901		/*
902		 * Setup "dma_page_ptr".
903		 *
904		 * Proof for formula below:
905		 *
906		 * Assume there are three USB frames having length "a", "b" and
907		 * "c". These USB frames will at maximum need "z"
908		 * "usb_page" structures. "z" is given by:
909		 *
910		 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) +
911		 * ((c / USB_PAGE_SIZE) + 2);
912		 *
913		 * Constraining "a", "b" and "c" like this:
914		 *
915		 * (a + b + c) <= parm->bufsize
916		 *
917		 * We know that:
918		 *
919		 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2));
920		 *
921		 * Here is the general formula:
922		 */
923		xfer->dma_page_ptr = parm->dma_page_ptr;
924		parm->dma_page_ptr += (2 * n_frbuffers);
925		parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE);
926	}
927#endif
928	if (zmps) {
929		/* correct maximum data length */
930		xfer->max_data_length = 0;
931	}
932	/* subtract USB frame remainder from "hc_max_frame_size" */
933
934	xfer->max_hc_frame_size =
935	    (parm->hc_max_frame_size -
936	    (parm->hc_max_frame_size % xfer->max_frame_size));
937
938	if (xfer->max_hc_frame_size == 0) {
939		parm->err = USB_ERR_INVAL;
940		goto done;
941	}
942
943	/* initialize frame buffers */
944
945	if (parm->buf) {
946		for (x = 0; x != n_frbuffers; x++) {
947			xfer->frbuffers[x].tag_parent =
948			    &xfer->xroot->dma_parent_tag;
949#if USB_HAVE_BUSDMA
950			if (xfer->flags_int.bdma_enable &&
951			    (parm->bufsize_max > 0)) {
952
953				if (usb_pc_dmamap_create(
954				    xfer->frbuffers + x,
955				    parm->bufsize_max)) {
956					parm->err = USB_ERR_NOMEM;
957					goto done;
958				}
959			}
960#endif
961		}
962	}
963done:
964	if (parm->err) {
965		/*
966		 * Set some dummy values so that we avoid division by zero:
967		 */
968		xfer->max_hc_frame_size = 1;
969		xfer->max_frame_size = 1;
970		xfer->max_packet_size = 1;
971		xfer->max_data_length = 0;
972		xfer->nframes = 0;
973		xfer->max_frame_count = 0;
974	}
975}
976
977static uint8_t
978usbd_transfer_setup_has_bulk(const struct usb_config *setup_start,
979    uint16_t n_setup)
980{
981	while (n_setup--) {
982		uint8_t type = setup_start[n_setup].type;
983		if (type == UE_BULK || type == UE_BULK_INTR ||
984		    type == UE_TYPE_ANY)
985			return (1);
986	}
987	return (0);
988}
989
990/*------------------------------------------------------------------------*
991 *	usbd_transfer_setup - setup an array of USB transfers
992 *
993 * NOTE: You must always call "usbd_transfer_unsetup" after calling
994 * "usbd_transfer_setup" if success was returned.
995 *
996 * The idea is that the USB device driver should pre-allocate all its
997 * transfers by one call to this function.
998 *
999 * Return values:
1000 *    0: Success
1001 * Else: Failure
1002 *------------------------------------------------------------------------*/
1003usb_error_t
1004usbd_transfer_setup(struct usb_device *udev,
1005    const uint8_t *ifaces, struct usb_xfer **ppxfer,
1006    const struct usb_config *setup_start, uint16_t n_setup,
1007    void *priv_sc, struct mtx *xfer_mtx)
1008{
1009	const struct usb_config *setup_end = setup_start + n_setup;
1010	const struct usb_config *setup;
1011	struct usb_setup_params *parm;
1012	struct usb_endpoint *ep;
1013	struct usb_xfer_root *info;
1014	struct usb_xfer *xfer;
1015	void *buf = NULL;
1016	usb_error_t error = 0;
1017	uint16_t n;
1018	uint16_t refcount;
1019	uint8_t do_unlock;
1020
1021	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1022	    "usbd_transfer_setup can sleep!");
1023
1024	/* do some checking first */
1025
1026	if (n_setup == 0) {
1027		DPRINTFN(6, "setup array has zero length!\n");
1028		return (USB_ERR_INVAL);
1029	}
1030	if (ifaces == NULL) {
1031		DPRINTFN(6, "ifaces array is NULL!\n");
1032		return (USB_ERR_INVAL);
1033	}
1034	if (xfer_mtx == NULL) {
1035		DPRINTFN(6, "using global lock\n");
1036		xfer_mtx = &Giant;
1037	}
1038
1039	/* more sanity checks */
1040
1041	for (setup = setup_start, n = 0;
1042	    setup != setup_end; setup++, n++) {
1043		if (setup->bufsize == (usb_frlength_t)-1) {
1044			error = USB_ERR_BAD_BUFSIZE;
1045			DPRINTF("invalid bufsize\n");
1046		}
1047		if (setup->callback == NULL) {
1048			error = USB_ERR_NO_CALLBACK;
1049			DPRINTF("no callback\n");
1050		}
1051		ppxfer[n] = NULL;
1052	}
1053
1054	if (error)
1055		return (error);
1056
1057	/* Protect scratch area */
1058	do_unlock = usbd_ctrl_lock(udev);
1059
1060	refcount = 0;
1061	info = NULL;
1062
1063	parm = &udev->scratch.xfer_setup[0].parm;
1064	memset(parm, 0, sizeof(*parm));
1065
1066	parm->udev = udev;
1067	parm->speed = usbd_get_speed(udev);
1068	parm->hc_max_packet_count = 1;
1069
1070	if (parm->speed >= USB_SPEED_MAX) {
1071		parm->err = USB_ERR_INVAL;
1072		goto done;
1073	}
1074	/* setup all transfers */
1075
1076	while (1) {
1077
1078		if (buf) {
1079			/*
1080			 * Initialize the "usb_xfer_root" structure,
1081			 * which is common for all our USB transfers.
1082			 */
1083			info = USB_ADD_BYTES(buf, 0);
1084
1085			info->memory_base = buf;
1086			info->memory_size = parm->size[0];
1087
1088#if USB_HAVE_BUSDMA
1089			info->dma_page_cache_start = USB_ADD_BYTES(buf, parm->size[4]);
1090			info->dma_page_cache_end = USB_ADD_BYTES(buf, parm->size[5]);
1091#endif
1092			info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm->size[5]);
1093			info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm->size[2]);
1094
1095			cv_init(&info->cv_drain, "WDRAIN");
1096
1097			info->xfer_mtx = xfer_mtx;
1098#if USB_HAVE_BUSDMA
1099			usb_dma_tag_setup(&info->dma_parent_tag,
1100			    parm->dma_tag_p, udev->bus->dma_parent_tag[0].tag,
1101			    xfer_mtx, &usb_bdma_done_event, udev->bus->dma_bits,
1102			    parm->dma_tag_max);
1103#endif
1104
1105			info->bus = udev->bus;
1106			info->udev = udev;
1107
1108			TAILQ_INIT(&info->done_q.head);
1109			info->done_q.command = &usbd_callback_wrapper;
1110#if USB_HAVE_BUSDMA
1111			TAILQ_INIT(&info->dma_q.head);
1112			info->dma_q.command = &usb_bdma_work_loop;
1113#endif
1114			info->done_m[0].hdr.pm_callback = &usb_callback_proc;
1115			info->done_m[0].xroot = info;
1116			info->done_m[1].hdr.pm_callback = &usb_callback_proc;
1117			info->done_m[1].xroot = info;
1118
1119			/*
1120			 * In device side mode control endpoint
1121			 * requests need to run from a separate
1122			 * context, else there is a chance of
1123			 * deadlock!
1124			 */
1125			if (setup_start == usb_control_ep_cfg ||
1126			    setup_start == usb_control_ep_quirk_cfg)
1127				info->done_p =
1128				    USB_BUS_CONTROL_XFER_PROC(udev->bus);
1129			else if (xfer_mtx == &Giant)
1130				info->done_p =
1131				    USB_BUS_GIANT_PROC(udev->bus);
1132			else if (usbd_transfer_setup_has_bulk(setup_start, n_setup))
1133				info->done_p =
1134				    USB_BUS_NON_GIANT_BULK_PROC(udev->bus);
1135			else
1136				info->done_p =
1137				    USB_BUS_NON_GIANT_ISOC_PROC(udev->bus);
1138		}
1139		/* reset sizes */
1140
1141		parm->size[0] = 0;
1142		parm->buf = buf;
1143		parm->size[0] += sizeof(info[0]);
1144
1145		for (setup = setup_start, n = 0;
1146		    setup != setup_end; setup++, n++) {
1147
1148			/* skip USB transfers without callbacks: */
1149			if (setup->callback == NULL) {
1150				continue;
1151			}
1152			/* see if there is a matching endpoint */
1153			ep = usbd_get_endpoint(udev,
1154			    ifaces[setup->if_index], setup);
1155
1156			/*
1157			 * Check that the USB PIPE is valid and that
1158			 * the endpoint mode is proper.
1159			 *
1160			 * Make sure we don't allocate a streams
1161			 * transfer when such a combination is not
1162			 * valid.
1163			 */
1164			if ((ep == NULL) || (ep->methods == NULL) ||
1165			    ((ep->ep_mode != USB_EP_MODE_STREAMS) &&
1166			    (ep->ep_mode != USB_EP_MODE_DEFAULT)) ||
1167			    (setup->stream_id != 0 &&
1168			    (setup->stream_id >= USB_MAX_EP_STREAMS ||
1169			    (ep->ep_mode != USB_EP_MODE_STREAMS)))) {
1170				if (setup->flags.no_pipe_ok)
1171					continue;
1172				if ((setup->usb_mode != USB_MODE_DUAL) &&
1173				    (setup->usb_mode != udev->flags.usb_mode))
1174					continue;
1175				parm->err = USB_ERR_NO_PIPE;
1176				goto done;
1177			}
1178
1179			/* align data properly */
1180			parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1181
1182			/* store current setup pointer */
1183			parm->curr_setup = setup;
1184
1185			if (buf) {
1186				/*
1187				 * Common initialization of the
1188				 * "usb_xfer" structure.
1189				 */
1190				xfer = USB_ADD_BYTES(buf, parm->size[0]);
1191				xfer->address = udev->address;
1192				xfer->priv_sc = priv_sc;
1193				xfer->xroot = info;
1194
1195				usb_callout_init_mtx(&xfer->timeout_handle,
1196				    &udev->bus->bus_mtx, 0);
1197			} else {
1198				/*
1199				 * Setup a dummy xfer, hence we are
1200				 * writing to the "usb_xfer"
1201				 * structure pointed to by "xfer"
1202				 * before we have allocated any
1203				 * memory:
1204				 */
1205				xfer = &udev->scratch.xfer_setup[0].dummy;
1206				memset(xfer, 0, sizeof(*xfer));
1207				refcount++;
1208			}
1209
1210			/* set transfer endpoint pointer */
1211			xfer->endpoint = ep;
1212
1213			/* set transfer stream ID */
1214			xfer->stream_id = setup->stream_id;
1215
1216			parm->size[0] += sizeof(xfer[0]);
1217			parm->methods = xfer->endpoint->methods;
1218			parm->curr_xfer = xfer;
1219
1220			/*
1221			 * Call the Host or Device controller transfer
1222			 * setup routine:
1223			 */
1224			(udev->bus->methods->xfer_setup) (parm);
1225
1226			/* check for error */
1227			if (parm->err)
1228				goto done;
1229
1230			if (buf) {
1231				/*
1232				 * Increment the endpoint refcount. This
1233				 * basically prevents setting a new
1234				 * configuration and alternate setting
1235				 * when USB transfers are in use on
1236				 * the given interface. Search the USB
1237				 * code for "endpoint->refcount_alloc" if you
1238				 * want more information.
1239				 */
1240				USB_BUS_LOCK(info->bus);
1241				if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX)
1242					parm->err = USB_ERR_INVAL;
1243
1244				xfer->endpoint->refcount_alloc++;
1245
1246				if (xfer->endpoint->refcount_alloc == 0)
1247					panic("usbd_transfer_setup(): Refcount wrapped to zero\n");
1248				USB_BUS_UNLOCK(info->bus);
1249
1250				/*
1251				 * Whenever we set ppxfer[] then we
1252				 * also need to increment the
1253				 * "setup_refcount":
1254				 */
1255				info->setup_refcount++;
1256
1257				/*
1258				 * Transfer is successfully setup and
1259				 * can be used:
1260				 */
1261				ppxfer[n] = xfer;
1262			}
1263
1264			/* check for error */
1265			if (parm->err)
1266				goto done;
1267		}
1268
1269		if (buf != NULL || parm->err != 0)
1270			goto done;
1271
1272		/* if no transfers, nothing to do */
1273		if (refcount == 0)
1274			goto done;
1275
1276		/* align data properly */
1277		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1278
1279		/* store offset temporarily */
1280		parm->size[1] = parm->size[0];
1281
1282		/*
1283		 * The number of DMA tags required depends on
1284		 * the number of endpoints. The current estimate
1285		 * for maximum number of DMA tags per endpoint
1286		 * is three:
1287		 * 1) for loading memory
1288		 * 2) for allocating memory
1289		 * 3) for fixing memory [UHCI]
1290		 */
1291		parm->dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX);
1292
1293		/*
1294		 * DMA tags for QH, TD, Data and more.
1295		 */
1296		parm->dma_tag_max += 8;
1297
1298		parm->dma_tag_p += parm->dma_tag_max;
1299
1300		parm->size[0] += ((uint8_t *)parm->dma_tag_p) -
1301		    ((uint8_t *)0);
1302
1303		/* align data properly */
1304		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1305
1306		/* store offset temporarily */
1307		parm->size[3] = parm->size[0];
1308
1309		parm->size[0] += ((uint8_t *)parm->dma_page_ptr) -
1310		    ((uint8_t *)0);
1311
1312		/* align data properly */
1313		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1314
1315		/* store offset temporarily */
1316		parm->size[4] = parm->size[0];
1317
1318		parm->size[0] += ((uint8_t *)parm->dma_page_cache_ptr) -
1319		    ((uint8_t *)0);
1320
1321		/* store end offset temporarily */
1322		parm->size[5] = parm->size[0];
1323
1324		parm->size[0] += ((uint8_t *)parm->xfer_page_cache_ptr) -
1325		    ((uint8_t *)0);
1326
1327		/* store end offset temporarily */
1328
1329		parm->size[2] = parm->size[0];
1330
1331		/* align data properly */
1332		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1333
1334		parm->size[6] = parm->size[0];
1335
1336		parm->size[0] += ((uint8_t *)parm->xfer_length_ptr) -
1337		    ((uint8_t *)0);
1338
1339		/* align data properly */
1340		parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1341
1342		/* allocate zeroed memory */
1343		buf = malloc(parm->size[0], M_USB, M_WAITOK | M_ZERO);
1344#if (USB_HAVE_MALLOC_WAITOK == 0)
1345		if (buf == NULL) {
1346			parm->err = USB_ERR_NOMEM;
1347			DPRINTFN(0, "cannot allocate memory block for "
1348			    "configuration (%d bytes)\n",
1349			    parm->size[0]);
1350			goto done;
1351                }
1352#endif
1353		parm->dma_tag_p = USB_ADD_BYTES(buf, parm->size[1]);
1354		parm->dma_page_ptr = USB_ADD_BYTES(buf, parm->size[3]);
1355		parm->dma_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[4]);
1356		parm->xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm->size[5]);
1357		parm->xfer_length_ptr = USB_ADD_BYTES(buf, parm->size[6]);
1358	}
1359
1360done:
1361	if (buf) {
1362		if (info->setup_refcount == 0) {
1363			/*
1364			 * "usbd_transfer_unsetup_sub" will unlock
1365			 * the bus mutex before returning !
1366			 */
1367			USB_BUS_LOCK(info->bus);
1368
1369			/* something went wrong */
1370			usbd_transfer_unsetup_sub(info, 0);
1371		}
1372	}
1373
1374	/* check if any errors happened */
1375	if (parm->err)
1376		usbd_transfer_unsetup(ppxfer, n_setup);
1377
1378	error = parm->err;
1379
1380	if (do_unlock)
1381		usbd_ctrl_unlock(udev);
1382
1383	return (error);
1384}
1385
1386/*------------------------------------------------------------------------*
1387 *	usbd_transfer_unsetup_sub - factored out code
1388 *------------------------------------------------------------------------*/
1389static void
1390usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay)
1391{
1392#if USB_HAVE_BUSDMA
1393	struct usb_page_cache *pc;
1394#endif
1395
1396	USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
1397
1398	/* wait for any outstanding DMA operations */
1399
1400	if (needs_delay) {
1401		usb_timeout_t temp;
1402		temp = usbd_get_dma_delay(info->udev);
1403		if (temp != 0) {
1404			usb_pause_mtx(&info->bus->bus_mtx,
1405			    USB_MS_TO_TICKS(temp));
1406		}
1407	}
1408
1409	/* make sure that our done messages are not queued anywhere */
1410	usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]);
1411
1412	USB_BUS_UNLOCK(info->bus);
1413
1414#if USB_HAVE_BUSDMA
1415	/* free DMA'able memory, if any */
1416	pc = info->dma_page_cache_start;
1417	while (pc != info->dma_page_cache_end) {
1418		usb_pc_free_mem(pc);
1419		pc++;
1420	}
1421
1422	/* free DMA maps in all "xfer->frbuffers" */
1423	pc = info->xfer_page_cache_start;
1424	while (pc != info->xfer_page_cache_end) {
1425		usb_pc_dmamap_destroy(pc);
1426		pc++;
1427	}
1428
1429	/* free all DMA tags */
1430	usb_dma_tag_unsetup(&info->dma_parent_tag);
1431#endif
1432
1433	cv_destroy(&info->cv_drain);
1434
1435	/*
1436	 * free the "memory_base" last, hence the "info" structure is
1437	 * contained within the "memory_base"!
1438	 */
1439	free(info->memory_base, M_USB);
1440}
1441
1442/*------------------------------------------------------------------------*
1443 *	usbd_transfer_unsetup - unsetup/free an array of USB transfers
1444 *
1445 * NOTE: All USB transfers in progress will get called back passing
1446 * the error code "USB_ERR_CANCELLED" before this function
1447 * returns.
1448 *------------------------------------------------------------------------*/
1449void
1450usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup)
1451{
1452	struct usb_xfer *xfer;
1453	struct usb_xfer_root *info;
1454	uint8_t needs_delay = 0;
1455
1456	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1457	    "usbd_transfer_unsetup can sleep!");
1458
1459	while (n_setup--) {
1460		xfer = pxfer[n_setup];
1461
1462		if (xfer == NULL)
1463			continue;
1464
1465		info = xfer->xroot;
1466
1467		USB_XFER_LOCK(xfer);
1468		USB_BUS_LOCK(info->bus);
1469
1470		/*
1471		 * HINT: when you start/stop a transfer, it might be a
1472		 * good idea to directly use the "pxfer[]" structure:
1473		 *
1474		 * usbd_transfer_start(sc->pxfer[0]);
1475		 * usbd_transfer_stop(sc->pxfer[0]);
1476		 *
1477		 * That way, if your code has many parts that will not
1478		 * stop running under the same lock, in other words
1479		 * "xfer_mtx", the usbd_transfer_start and
1480		 * usbd_transfer_stop functions will simply return
1481		 * when they detect a NULL pointer argument.
1482		 *
1483		 * To avoid any races we clear the "pxfer[]" pointer
1484		 * while holding the private mutex of the driver:
1485		 */
1486		pxfer[n_setup] = NULL;
1487
1488		USB_BUS_UNLOCK(info->bus);
1489		USB_XFER_UNLOCK(xfer);
1490
1491		usbd_transfer_drain(xfer);
1492
1493#if USB_HAVE_BUSDMA
1494		if (xfer->flags_int.bdma_enable)
1495			needs_delay = 1;
1496#endif
1497		/*
1498		 * NOTE: default endpoint does not have an
1499		 * interface, even if endpoint->iface_index == 0
1500		 */
1501		USB_BUS_LOCK(info->bus);
1502		xfer->endpoint->refcount_alloc--;
1503		USB_BUS_UNLOCK(info->bus);
1504
1505		usb_callout_drain(&xfer->timeout_handle);
1506
1507		USB_BUS_LOCK(info->bus);
1508
1509		USB_ASSERT(info->setup_refcount != 0, ("Invalid setup "
1510		    "reference count\n"));
1511
1512		info->setup_refcount--;
1513
1514		if (info->setup_refcount == 0) {
1515			usbd_transfer_unsetup_sub(info,
1516			    needs_delay);
1517		} else {
1518			USB_BUS_UNLOCK(info->bus);
1519		}
1520	}
1521}
1522
1523/*------------------------------------------------------------------------*
1524 *	usbd_control_transfer_init - factored out code
1525 *
1526 * In USB Device Mode we have to wait for the SETUP packet which
1527 * containst the "struct usb_device_request" structure, before we can
1528 * transfer any data. In USB Host Mode we already have the SETUP
1529 * packet at the moment the USB transfer is started. This leads us to
1530 * having to setup the USB transfer at two different places in
1531 * time. This function just contains factored out control transfer
1532 * initialisation code, so that we don't duplicate the code.
1533 *------------------------------------------------------------------------*/
1534static void
1535usbd_control_transfer_init(struct usb_xfer *xfer)
1536{
1537	struct usb_device_request req;
1538
1539	/* copy out the USB request header */
1540
1541	usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1542
1543	/* setup remainder */
1544
1545	xfer->flags_int.control_rem = UGETW(req.wLength);
1546
1547	/* copy direction to endpoint variable */
1548
1549	xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT);
1550	xfer->endpointno |=
1551	    (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT;
1552}
1553
1554/*------------------------------------------------------------------------*
1555 *	usbd_control_transfer_did_data
1556 *
1557 * This function returns non-zero if a control endpoint has
1558 * transferred the first DATA packet after the SETUP packet.
1559 * Else it returns zero.
1560 *------------------------------------------------------------------------*/
1561static uint8_t
1562usbd_control_transfer_did_data(struct usb_xfer *xfer)
1563{
1564	struct usb_device_request req;
1565
1566	/* SETUP packet is not yet sent */
1567	if (xfer->flags_int.control_hdr != 0)
1568		return (0);
1569
1570	/* copy out the USB request header */
1571	usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req));
1572
1573	/* compare remainder to the initial value */
1574	return (xfer->flags_int.control_rem != UGETW(req.wLength));
1575}
1576
1577/*------------------------------------------------------------------------*
1578 *	usbd_setup_ctrl_transfer
1579 *
1580 * This function handles initialisation of control transfers. Control
1581 * transfers are special in that regard that they can both transmit
1582 * and receive data.
1583 *
1584 * Return values:
1585 *    0: Success
1586 * Else: Failure
1587 *------------------------------------------------------------------------*/
1588static int
1589usbd_setup_ctrl_transfer(struct usb_xfer *xfer)
1590{
1591	usb_frlength_t len;
1592
1593	/* Check for control endpoint stall */
1594	if (xfer->flags.stall_pipe && xfer->flags_int.control_act) {
1595		/* the control transfer is no longer active */
1596		xfer->flags_int.control_stall = 1;
1597		xfer->flags_int.control_act = 0;
1598	} else {
1599		/* don't stall control transfer by default */
1600		xfer->flags_int.control_stall = 0;
1601	}
1602
1603	/* Check for invalid number of frames */
1604	if (xfer->nframes > 2) {
1605		/*
1606		 * If you need to split a control transfer, you
1607		 * have to do one part at a time. Only with
1608		 * non-control transfers you can do multiple
1609		 * parts a time.
1610		 */
1611		DPRINTFN(0, "Too many frames: %u\n",
1612		    (unsigned int)xfer->nframes);
1613		goto error;
1614	}
1615
1616	/*
1617         * Check if there is a control
1618         * transfer in progress:
1619         */
1620	if (xfer->flags_int.control_act) {
1621
1622		if (xfer->flags_int.control_hdr) {
1623
1624			/* clear send header flag */
1625
1626			xfer->flags_int.control_hdr = 0;
1627
1628			/* setup control transfer */
1629			if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1630				usbd_control_transfer_init(xfer);
1631			}
1632		}
1633		/* get data length */
1634
1635		len = xfer->sumlen;
1636
1637	} else {
1638
1639		/* the size of the SETUP structure is hardcoded ! */
1640
1641		if (xfer->frlengths[0] != sizeof(struct usb_device_request)) {
1642			DPRINTFN(0, "Wrong framelength %u != %zu\n",
1643			    xfer->frlengths[0], sizeof(struct
1644			    usb_device_request));
1645			goto error;
1646		}
1647		/* check USB mode */
1648		if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1649
1650			/* check number of frames */
1651			if (xfer->nframes != 1) {
1652				/*
1653			         * We need to receive the setup
1654			         * message first so that we know the
1655			         * data direction!
1656			         */
1657				DPRINTF("Misconfigured transfer\n");
1658				goto error;
1659			}
1660			/*
1661			 * Set a dummy "control_rem" value.  This
1662			 * variable will be overwritten later by a
1663			 * call to "usbd_control_transfer_init()" !
1664			 */
1665			xfer->flags_int.control_rem = 0xFFFF;
1666		} else {
1667
1668			/* setup "endpoint" and "control_rem" */
1669
1670			usbd_control_transfer_init(xfer);
1671		}
1672
1673		/* set transfer-header flag */
1674
1675		xfer->flags_int.control_hdr = 1;
1676
1677		/* get data length */
1678
1679		len = (xfer->sumlen - sizeof(struct usb_device_request));
1680	}
1681
1682	/* update did data flag */
1683
1684	xfer->flags_int.control_did_data =
1685	    usbd_control_transfer_did_data(xfer);
1686
1687	/* check if there is a length mismatch */
1688
1689	if (len > xfer->flags_int.control_rem) {
1690		DPRINTFN(0, "Length (%d) greater than "
1691		    "remaining length (%d)\n", len,
1692		    xfer->flags_int.control_rem);
1693		goto error;
1694	}
1695	/* check if we are doing a short transfer */
1696
1697	if (xfer->flags.force_short_xfer) {
1698		xfer->flags_int.control_rem = 0;
1699	} else {
1700		if ((len != xfer->max_data_length) &&
1701		    (len != xfer->flags_int.control_rem) &&
1702		    (xfer->nframes != 1)) {
1703			DPRINTFN(0, "Short control transfer without "
1704			    "force_short_xfer set\n");
1705			goto error;
1706		}
1707		xfer->flags_int.control_rem -= len;
1708	}
1709
1710	/* the status part is executed when "control_act" is 0 */
1711
1712	if ((xfer->flags_int.control_rem > 0) ||
1713	    (xfer->flags.manual_status)) {
1714		/* don't execute the STATUS stage yet */
1715		xfer->flags_int.control_act = 1;
1716
1717		/* sanity check */
1718		if ((!xfer->flags_int.control_hdr) &&
1719		    (xfer->nframes == 1)) {
1720			/*
1721		         * This is not a valid operation!
1722		         */
1723			DPRINTFN(0, "Invalid parameter "
1724			    "combination\n");
1725			goto error;
1726		}
1727	} else {
1728		/* time to execute the STATUS stage */
1729		xfer->flags_int.control_act = 0;
1730	}
1731	return (0);			/* success */
1732
1733error:
1734	return (1);			/* failure */
1735}
1736
1737/*------------------------------------------------------------------------*
1738 *	usbd_transfer_submit - start USB hardware for the given transfer
1739 *
1740 * This function should only be called from the USB callback.
1741 *------------------------------------------------------------------------*/
1742void
1743usbd_transfer_submit(struct usb_xfer *xfer)
1744{
1745	struct usb_xfer_root *info;
1746	struct usb_bus *bus;
1747	usb_frcount_t x;
1748
1749	info = xfer->xroot;
1750	bus = info->bus;
1751
1752	DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n",
1753	    xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ?
1754	    "read" : "write");
1755
1756#ifdef USB_DEBUG
1757	if (USB_DEBUG_VAR > 0) {
1758		USB_BUS_LOCK(bus);
1759
1760		usb_dump_endpoint(xfer->endpoint);
1761
1762		USB_BUS_UNLOCK(bus);
1763	}
1764#endif
1765
1766	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1767	USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED);
1768
1769	/* Only open the USB transfer once! */
1770	if (!xfer->flags_int.open) {
1771		xfer->flags_int.open = 1;
1772
1773		DPRINTF("open\n");
1774
1775		USB_BUS_LOCK(bus);
1776		(xfer->endpoint->methods->open) (xfer);
1777		USB_BUS_UNLOCK(bus);
1778	}
1779	/* set "transferring" flag */
1780	xfer->flags_int.transferring = 1;
1781
1782#if USB_HAVE_POWERD
1783	/* increment power reference */
1784	usbd_transfer_power_ref(xfer, 1);
1785#endif
1786	/*
1787	 * Check if the transfer is waiting on a queue, most
1788	 * frequently the "done_q":
1789	 */
1790	if (xfer->wait_queue) {
1791		USB_BUS_LOCK(bus);
1792		usbd_transfer_dequeue(xfer);
1793		USB_BUS_UNLOCK(bus);
1794	}
1795	/* clear "did_dma_delay" flag */
1796	xfer->flags_int.did_dma_delay = 0;
1797
1798	/* clear "did_close" flag */
1799	xfer->flags_int.did_close = 0;
1800
1801#if USB_HAVE_BUSDMA
1802	/* clear "bdma_setup" flag */
1803	xfer->flags_int.bdma_setup = 0;
1804#endif
1805	/* by default we cannot cancel any USB transfer immediately */
1806	xfer->flags_int.can_cancel_immed = 0;
1807
1808	/* clear lengths and frame counts by default */
1809	xfer->sumlen = 0;
1810	xfer->actlen = 0;
1811	xfer->aframes = 0;
1812
1813	/* clear any previous errors */
1814	xfer->error = 0;
1815
1816	/* Check if the device is still alive */
1817	if (info->udev->state < USB_STATE_POWERED) {
1818		USB_BUS_LOCK(bus);
1819		/*
1820		 * Must return cancelled error code else
1821		 * device drivers can hang.
1822		 */
1823		usbd_transfer_done(xfer, USB_ERR_CANCELLED);
1824		USB_BUS_UNLOCK(bus);
1825		return;
1826	}
1827
1828	/* sanity check */
1829	if (xfer->nframes == 0) {
1830		if (xfer->flags.stall_pipe) {
1831			/*
1832			 * Special case - want to stall without transferring
1833			 * any data:
1834			 */
1835			DPRINTF("xfer=%p nframes=0: stall "
1836			    "or clear stall!\n", xfer);
1837			USB_BUS_LOCK(bus);
1838			xfer->flags_int.can_cancel_immed = 1;
1839			/* start the transfer */
1840			usb_command_wrapper(&xfer->endpoint->
1841			    endpoint_q[xfer->stream_id], xfer);
1842			USB_BUS_UNLOCK(bus);
1843			return;
1844		}
1845		USB_BUS_LOCK(bus);
1846		usbd_transfer_done(xfer, USB_ERR_INVAL);
1847		USB_BUS_UNLOCK(bus);
1848		return;
1849	}
1850	/* compute some variables */
1851
1852	for (x = 0; x != xfer->nframes; x++) {
1853		/* make a copy of the frlenghts[] */
1854		xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x];
1855		/* compute total transfer length */
1856		xfer->sumlen += xfer->frlengths[x];
1857		if (xfer->sumlen < xfer->frlengths[x]) {
1858			/* length wrapped around */
1859			USB_BUS_LOCK(bus);
1860			usbd_transfer_done(xfer, USB_ERR_INVAL);
1861			USB_BUS_UNLOCK(bus);
1862			return;
1863		}
1864	}
1865
1866	/* clear some internal flags */
1867
1868	xfer->flags_int.short_xfer_ok = 0;
1869	xfer->flags_int.short_frames_ok = 0;
1870
1871	/* check if this is a control transfer */
1872
1873	if (xfer->flags_int.control_xfr) {
1874
1875		if (usbd_setup_ctrl_transfer(xfer)) {
1876			USB_BUS_LOCK(bus);
1877			usbd_transfer_done(xfer, USB_ERR_STALLED);
1878			USB_BUS_UNLOCK(bus);
1879			return;
1880		}
1881	}
1882	/*
1883	 * Setup filtered version of some transfer flags,
1884	 * in case of data read direction
1885	 */
1886	if (USB_GET_DATA_ISREAD(xfer)) {
1887
1888		if (xfer->flags.short_frames_ok) {
1889			xfer->flags_int.short_xfer_ok = 1;
1890			xfer->flags_int.short_frames_ok = 1;
1891		} else if (xfer->flags.short_xfer_ok) {
1892			xfer->flags_int.short_xfer_ok = 1;
1893
1894			/* check for control transfer */
1895			if (xfer->flags_int.control_xfr) {
1896				/*
1897				 * 1) Control transfers do not support
1898				 * reception of multiple short USB
1899				 * frames in host mode and device side
1900				 * mode, with exception of:
1901				 *
1902				 * 2) Due to sometimes buggy device
1903				 * side firmware we need to do a
1904				 * STATUS stage in case of short
1905				 * control transfers in USB host mode.
1906				 * The STATUS stage then becomes the
1907				 * "alt_next" to the DATA stage.
1908				 */
1909				xfer->flags_int.short_frames_ok = 1;
1910			}
1911		}
1912	}
1913	/*
1914	 * Check if BUS-DMA support is enabled and try to load virtual
1915	 * buffers into DMA, if any:
1916	 */
1917#if USB_HAVE_BUSDMA
1918	if (xfer->flags_int.bdma_enable) {
1919		/* insert the USB transfer last in the BUS-DMA queue */
1920		usb_command_wrapper(&xfer->xroot->dma_q, xfer);
1921		return;
1922	}
1923#endif
1924	/*
1925	 * Enter the USB transfer into the Host Controller or
1926	 * Device Controller schedule:
1927	 */
1928	usbd_pipe_enter(xfer);
1929}
1930
1931/*------------------------------------------------------------------------*
1932 *	usbd_pipe_enter - factored out code
1933 *------------------------------------------------------------------------*/
1934void
1935usbd_pipe_enter(struct usb_xfer *xfer)
1936{
1937	struct usb_endpoint *ep;
1938
1939	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1940
1941	USB_BUS_LOCK(xfer->xroot->bus);
1942
1943	ep = xfer->endpoint;
1944
1945	DPRINTF("enter\n");
1946
1947	/* the transfer can now be cancelled */
1948	xfer->flags_int.can_cancel_immed = 1;
1949
1950	/* enter the transfer */
1951	(ep->methods->enter) (xfer);
1952
1953	/* check for transfer error */
1954	if (xfer->error) {
1955		/* some error has happened */
1956		usbd_transfer_done(xfer, 0);
1957		USB_BUS_UNLOCK(xfer->xroot->bus);
1958		return;
1959	}
1960
1961	/* start the transfer */
1962	usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer);
1963	USB_BUS_UNLOCK(xfer->xroot->bus);
1964}
1965
1966/*------------------------------------------------------------------------*
1967 *	usbd_transfer_start - start an USB transfer
1968 *
1969 * NOTE: Calling this function more than one time will only
1970 *       result in a single transfer start, until the USB transfer
1971 *       completes.
1972 *------------------------------------------------------------------------*/
1973void
1974usbd_transfer_start(struct usb_xfer *xfer)
1975{
1976	if (xfer == NULL) {
1977		/* transfer is gone */
1978		return;
1979	}
1980	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
1981
1982	/* mark the USB transfer started */
1983
1984	if (!xfer->flags_int.started) {
1985		/* lock the BUS lock to avoid races updating flags_int */
1986		USB_BUS_LOCK(xfer->xroot->bus);
1987		xfer->flags_int.started = 1;
1988		USB_BUS_UNLOCK(xfer->xroot->bus);
1989	}
1990	/* check if the USB transfer callback is already transferring */
1991
1992	if (xfer->flags_int.transferring) {
1993		return;
1994	}
1995	USB_BUS_LOCK(xfer->xroot->bus);
1996	/* call the USB transfer callback */
1997	usbd_callback_ss_done_defer(xfer);
1998	USB_BUS_UNLOCK(xfer->xroot->bus);
1999}
2000
2001/*------------------------------------------------------------------------*
2002 *	usbd_transfer_stop - stop an USB transfer
2003 *
2004 * NOTE: Calling this function more than one time will only
2005 *       result in a single transfer stop.
2006 * NOTE: When this function returns it is not safe to free nor
2007 *       reuse any DMA buffers. See "usbd_transfer_drain()".
2008 *------------------------------------------------------------------------*/
2009void
2010usbd_transfer_stop(struct usb_xfer *xfer)
2011{
2012	struct usb_endpoint *ep;
2013
2014	if (xfer == NULL) {
2015		/* transfer is gone */
2016		return;
2017	}
2018	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2019
2020	/* check if the USB transfer was ever opened */
2021
2022	if (!xfer->flags_int.open) {
2023		if (xfer->flags_int.started) {
2024			/* nothing to do except clearing the "started" flag */
2025			/* lock the BUS lock to avoid races updating flags_int */
2026			USB_BUS_LOCK(xfer->xroot->bus);
2027			xfer->flags_int.started = 0;
2028			USB_BUS_UNLOCK(xfer->xroot->bus);
2029		}
2030		return;
2031	}
2032	/* try to stop the current USB transfer */
2033
2034	USB_BUS_LOCK(xfer->xroot->bus);
2035	/* override any previous error */
2036	xfer->error = USB_ERR_CANCELLED;
2037
2038	/*
2039	 * Clear "open" and "started" when both private and USB lock
2040	 * is locked so that we don't get a race updating "flags_int"
2041	 */
2042	xfer->flags_int.open = 0;
2043	xfer->flags_int.started = 0;
2044
2045	/*
2046	 * Check if we can cancel the USB transfer immediately.
2047	 */
2048	if (xfer->flags_int.transferring) {
2049		if (xfer->flags_int.can_cancel_immed &&
2050		    (!xfer->flags_int.did_close)) {
2051			DPRINTF("close\n");
2052			/*
2053			 * The following will lead to an USB_ERR_CANCELLED
2054			 * error code being passed to the USB callback.
2055			 */
2056			(xfer->endpoint->methods->close) (xfer);
2057			/* only close once */
2058			xfer->flags_int.did_close = 1;
2059		} else {
2060			/* need to wait for the next done callback */
2061		}
2062	} else {
2063		DPRINTF("close\n");
2064
2065		/* close here and now */
2066		(xfer->endpoint->methods->close) (xfer);
2067
2068		/*
2069		 * Any additional DMA delay is done by
2070		 * "usbd_transfer_unsetup()".
2071		 */
2072
2073		/*
2074		 * Special case. Check if we need to restart a blocked
2075		 * endpoint.
2076		 */
2077		ep = xfer->endpoint;
2078
2079		/*
2080		 * If the current USB transfer is completing we need
2081		 * to start the next one:
2082		 */
2083		if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
2084			usb_command_wrapper(
2085			    &ep->endpoint_q[xfer->stream_id], NULL);
2086		}
2087	}
2088
2089	USB_BUS_UNLOCK(xfer->xroot->bus);
2090}
2091
2092/*------------------------------------------------------------------------*
2093 *	usbd_transfer_pending
2094 *
2095 * This function will check if an USB transfer is pending which is a
2096 * little bit complicated!
2097 * Return values:
2098 * 0: Not pending
2099 * 1: Pending: The USB transfer will receive a callback in the future.
2100 *------------------------------------------------------------------------*/
2101uint8_t
2102usbd_transfer_pending(struct usb_xfer *xfer)
2103{
2104	struct usb_xfer_root *info;
2105	struct usb_xfer_queue *pq;
2106
2107	if (xfer == NULL) {
2108		/* transfer is gone */
2109		return (0);
2110	}
2111	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2112
2113	if (xfer->flags_int.transferring) {
2114		/* trivial case */
2115		return (1);
2116	}
2117	USB_BUS_LOCK(xfer->xroot->bus);
2118	if (xfer->wait_queue) {
2119		/* we are waiting on a queue somewhere */
2120		USB_BUS_UNLOCK(xfer->xroot->bus);
2121		return (1);
2122	}
2123	info = xfer->xroot;
2124	pq = &info->done_q;
2125
2126	if (pq->curr == xfer) {
2127		/* we are currently scheduled for callback */
2128		USB_BUS_UNLOCK(xfer->xroot->bus);
2129		return (1);
2130	}
2131	/* we are not pending */
2132	USB_BUS_UNLOCK(xfer->xroot->bus);
2133	return (0);
2134}
2135
2136/*------------------------------------------------------------------------*
2137 *	usbd_transfer_drain
2138 *
2139 * This function will stop the USB transfer and wait for any
2140 * additional BUS-DMA and HW-DMA operations to complete. Buffers that
2141 * are loaded into DMA can safely be freed or reused after that this
2142 * function has returned.
2143 *------------------------------------------------------------------------*/
2144void
2145usbd_transfer_drain(struct usb_xfer *xfer)
2146{
2147	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2148	    "usbd_transfer_drain can sleep!");
2149
2150	if (xfer == NULL) {
2151		/* transfer is gone */
2152		return;
2153	}
2154	if (xfer->xroot->xfer_mtx != &Giant) {
2155		USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED);
2156	}
2157	USB_XFER_LOCK(xfer);
2158
2159	usbd_transfer_stop(xfer);
2160
2161	while (usbd_transfer_pending(xfer) ||
2162	    xfer->flags_int.doing_callback) {
2163
2164		/*
2165		 * It is allowed that the callback can drop its
2166		 * transfer mutex. In that case checking only
2167		 * "usbd_transfer_pending()" is not enough to tell if
2168		 * the USB transfer is fully drained. We also need to
2169		 * check the internal "doing_callback" flag.
2170		 */
2171		xfer->flags_int.draining = 1;
2172
2173		/*
2174		 * Wait until the current outstanding USB
2175		 * transfer is complete !
2176		 */
2177		cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx);
2178	}
2179	USB_XFER_UNLOCK(xfer);
2180}
2181
2182struct usb_page_cache *
2183usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex)
2184{
2185	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2186
2187	return (&xfer->frbuffers[frindex]);
2188}
2189
2190void *
2191usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex)
2192{
2193	struct usb_page_search page_info;
2194
2195	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2196
2197	usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info);
2198	return (page_info.buffer);
2199}
2200
2201/*------------------------------------------------------------------------*
2202 *	usbd_xfer_get_fps_shift
2203 *
2204 * The following function is only useful for isochronous transfers. It
2205 * returns how many times the frame execution rate has been shifted
2206 * down.
2207 *
2208 * Return value:
2209 * Success: 0..3
2210 * Failure: 0
2211 *------------------------------------------------------------------------*/
2212uint8_t
2213usbd_xfer_get_fps_shift(struct usb_xfer *xfer)
2214{
2215	return (xfer->fps_shift);
2216}
2217
2218usb_frlength_t
2219usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex)
2220{
2221	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2222
2223	return (xfer->frlengths[frindex]);
2224}
2225
2226/*------------------------------------------------------------------------*
2227 *	usbd_xfer_set_frame_data
2228 *
2229 * This function sets the pointer of the buffer that should
2230 * loaded directly into DMA for the given USB frame. Passing "ptr"
2231 * equal to NULL while the corresponding "frlength" is greater
2232 * than zero gives undefined results!
2233 *------------------------------------------------------------------------*/
2234void
2235usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2236    void *ptr, usb_frlength_t len)
2237{
2238	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2239
2240	/* set virtual address to load and length */
2241	xfer->frbuffers[frindex].buffer = ptr;
2242	usbd_xfer_set_frame_len(xfer, frindex, len);
2243}
2244
2245void
2246usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
2247    void **ptr, int *len)
2248{
2249	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2250
2251	if (ptr != NULL)
2252		*ptr = xfer->frbuffers[frindex].buffer;
2253	if (len != NULL)
2254		*len = xfer->frlengths[frindex];
2255}
2256
2257/*------------------------------------------------------------------------*
2258 *	usbd_xfer_old_frame_length
2259 *
2260 * This function returns the framelength of the given frame at the
2261 * time the transfer was submitted. This function can be used to
2262 * compute the starting data pointer of the next isochronous frame
2263 * when an isochronous transfer has completed.
2264 *------------------------------------------------------------------------*/
2265usb_frlength_t
2266usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex)
2267{
2268	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2269
2270	return (xfer->frlengths[frindex + xfer->max_frame_count]);
2271}
2272
2273void
2274usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes,
2275    int *nframes)
2276{
2277	if (actlen != NULL)
2278		*actlen = xfer->actlen;
2279	if (sumlen != NULL)
2280		*sumlen = xfer->sumlen;
2281	if (aframes != NULL)
2282		*aframes = xfer->aframes;
2283	if (nframes != NULL)
2284		*nframes = xfer->nframes;
2285}
2286
2287/*------------------------------------------------------------------------*
2288 *	usbd_xfer_set_frame_offset
2289 *
2290 * This function sets the frame data buffer offset relative to the beginning
2291 * of the USB DMA buffer allocated for this USB transfer.
2292 *------------------------------------------------------------------------*/
2293void
2294usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
2295    usb_frcount_t frindex)
2296{
2297	KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame "
2298	    "when the USB buffer is external\n"));
2299	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2300
2301	/* set virtual address to load */
2302	xfer->frbuffers[frindex].buffer =
2303	    USB_ADD_BYTES(xfer->local_buffer, offset);
2304}
2305
2306void
2307usbd_xfer_set_interval(struct usb_xfer *xfer, int i)
2308{
2309	xfer->interval = i;
2310}
2311
2312void
2313usbd_xfer_set_timeout(struct usb_xfer *xfer, int t)
2314{
2315	xfer->timeout = t;
2316}
2317
2318void
2319usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n)
2320{
2321	xfer->nframes = n;
2322}
2323
2324usb_frcount_t
2325usbd_xfer_max_frames(struct usb_xfer *xfer)
2326{
2327	return (xfer->max_frame_count);
2328}
2329
2330usb_frlength_t
2331usbd_xfer_max_len(struct usb_xfer *xfer)
2332{
2333	return (xfer->max_data_length);
2334}
2335
2336usb_frlength_t
2337usbd_xfer_max_framelen(struct usb_xfer *xfer)
2338{
2339	return (xfer->max_frame_size);
2340}
2341
2342void
2343usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
2344    usb_frlength_t len)
2345{
2346	KASSERT(frindex < xfer->max_frame_count, ("frame index overflow"));
2347
2348	xfer->frlengths[frindex] = len;
2349}
2350
2351/*------------------------------------------------------------------------*
2352 *	usb_callback_proc - factored out code
2353 *
2354 * This function performs USB callbacks.
2355 *------------------------------------------------------------------------*/
2356static void
2357usb_callback_proc(struct usb_proc_msg *_pm)
2358{
2359	struct usb_done_msg *pm = (void *)_pm;
2360	struct usb_xfer_root *info = pm->xroot;
2361
2362	/* Change locking order */
2363	USB_BUS_UNLOCK(info->bus);
2364
2365	/*
2366	 * We exploit the fact that the mutex is the same for all
2367	 * callbacks that will be called from this thread:
2368	 */
2369	mtx_lock(info->xfer_mtx);
2370	USB_BUS_LOCK(info->bus);
2371
2372	/* Continue where we lost track */
2373	usb_command_wrapper(&info->done_q,
2374	    info->done_q.curr);
2375
2376	mtx_unlock(info->xfer_mtx);
2377}
2378
2379/*------------------------------------------------------------------------*
2380 *	usbd_callback_ss_done_defer
2381 *
2382 * This function will defer the start, stop and done callback to the
2383 * correct thread.
2384 *------------------------------------------------------------------------*/
2385static void
2386usbd_callback_ss_done_defer(struct usb_xfer *xfer)
2387{
2388	struct usb_xfer_root *info = xfer->xroot;
2389	struct usb_xfer_queue *pq = &info->done_q;
2390
2391	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2392
2393	if (pq->curr != xfer) {
2394		usbd_transfer_enqueue(pq, xfer);
2395	}
2396	if (!pq->recurse_1) {
2397
2398		/*
2399	         * We have to postpone the callback due to the fact we
2400	         * will have a Lock Order Reversal, LOR, if we try to
2401	         * proceed !
2402	         */
2403		(void) usb_proc_msignal(info->done_p,
2404		    &info->done_m[0], &info->done_m[1]);
2405	} else {
2406		/* clear second recurse flag */
2407		pq->recurse_2 = 0;
2408	}
2409	return;
2410
2411}
2412
2413/*------------------------------------------------------------------------*
2414 *	usbd_callback_wrapper
2415 *
2416 * This is a wrapper for USB callbacks. This wrapper does some
2417 * auto-magic things like figuring out if we can call the callback
2418 * directly from the current context or if we need to wakeup the
2419 * interrupt process.
2420 *------------------------------------------------------------------------*/
2421static void
2422usbd_callback_wrapper(struct usb_xfer_queue *pq)
2423{
2424	struct usb_xfer *xfer = pq->curr;
2425	struct usb_xfer_root *info = xfer->xroot;
2426
2427	USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2428	if ((pq->recurse_3 != 0 || mtx_owned(info->xfer_mtx) == 0) &&
2429	    SCHEDULER_STOPPED() == 0) {
2430		/*
2431	       	 * Cases that end up here:
2432		 *
2433		 * 5) HW interrupt done callback or other source.
2434		 * 6) HW completed transfer during callback
2435		 */
2436		DPRINTFN(3, "case 5 and 6\n");
2437
2438		/*
2439	         * We have to postpone the callback due to the fact we
2440	         * will have a Lock Order Reversal, LOR, if we try to
2441	         * proceed!
2442		 *
2443		 * Postponing the callback also ensures that other USB
2444		 * transfer queues get a chance.
2445	         */
2446		(void) usb_proc_msignal(info->done_p,
2447		    &info->done_m[0], &info->done_m[1]);
2448		return;
2449	}
2450	/*
2451	 * Cases that end up here:
2452	 *
2453	 * 1) We are starting a transfer
2454	 * 2) We are prematurely calling back a transfer
2455	 * 3) We are stopping a transfer
2456	 * 4) We are doing an ordinary callback
2457	 */
2458	DPRINTFN(3, "case 1-4\n");
2459	/* get next USB transfer in the queue */
2460	info->done_q.curr = NULL;
2461
2462	/* set flag in case of drain */
2463	xfer->flags_int.doing_callback = 1;
2464
2465	USB_BUS_UNLOCK(info->bus);
2466	USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED);
2467
2468	/* set correct USB state for callback */
2469	if (!xfer->flags_int.transferring) {
2470		xfer->usb_state = USB_ST_SETUP;
2471		if (!xfer->flags_int.started) {
2472			/* we got stopped before we even got started */
2473			USB_BUS_LOCK(info->bus);
2474			goto done;
2475		}
2476	} else {
2477
2478		if (usbd_callback_wrapper_sub(xfer)) {
2479			/* the callback has been deferred */
2480			USB_BUS_LOCK(info->bus);
2481			goto done;
2482		}
2483#if USB_HAVE_POWERD
2484		/* decrement power reference */
2485		usbd_transfer_power_ref(xfer, -1);
2486#endif
2487		xfer->flags_int.transferring = 0;
2488
2489		if (xfer->error) {
2490			xfer->usb_state = USB_ST_ERROR;
2491		} else {
2492			/* set transferred state */
2493			xfer->usb_state = USB_ST_TRANSFERRED;
2494#if USB_HAVE_BUSDMA
2495			/* sync DMA memory, if any */
2496			if (xfer->flags_int.bdma_enable &&
2497			    (!xfer->flags_int.bdma_no_post_sync)) {
2498				usb_bdma_post_sync(xfer);
2499			}
2500#endif
2501		}
2502	}
2503
2504#if USB_HAVE_PF
2505	if (xfer->usb_state != USB_ST_SETUP) {
2506		USB_BUS_LOCK(info->bus);
2507		usbpf_xfertap(xfer, USBPF_XFERTAP_DONE);
2508		USB_BUS_UNLOCK(info->bus);
2509	}
2510#endif
2511	/* call processing routine */
2512	(xfer->callback) (xfer, xfer->error);
2513
2514	/* pickup the USB mutex again */
2515	USB_BUS_LOCK(info->bus);
2516
2517	/*
2518	 * Check if we got started after that we got cancelled, but
2519	 * before we managed to do the callback.
2520	 */
2521	if ((!xfer->flags_int.open) &&
2522	    (xfer->flags_int.started) &&
2523	    (xfer->usb_state == USB_ST_ERROR)) {
2524		/* clear flag in case of drain */
2525		xfer->flags_int.doing_callback = 0;
2526		/* try to loop, but not recursivly */
2527		usb_command_wrapper(&info->done_q, xfer);
2528		return;
2529	}
2530
2531done:
2532	/* clear flag in case of drain */
2533	xfer->flags_int.doing_callback = 0;
2534
2535	/*
2536	 * Check if we are draining.
2537	 */
2538	if (xfer->flags_int.draining &&
2539	    (!xfer->flags_int.transferring)) {
2540		/* "usbd_transfer_drain()" is waiting for end of transfer */
2541		xfer->flags_int.draining = 0;
2542		cv_broadcast(&info->cv_drain);
2543	}
2544
2545	/* do the next callback, if any */
2546	usb_command_wrapper(&info->done_q,
2547	    info->done_q.curr);
2548}
2549
2550/*------------------------------------------------------------------------*
2551 *	usb_dma_delay_done_cb
2552 *
2553 * This function is called when the DMA delay has been exectuded, and
2554 * will make sure that the callback is called to complete the USB
2555 * transfer. This code path is usually only used when there is an USB
2556 * error like USB_ERR_CANCELLED.
2557 *------------------------------------------------------------------------*/
2558void
2559usb_dma_delay_done_cb(struct usb_xfer *xfer)
2560{
2561	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2562
2563	DPRINTFN(3, "Completed %p\n", xfer);
2564
2565	/* queue callback for execution, again */
2566	usbd_transfer_done(xfer, 0);
2567}
2568
2569/*------------------------------------------------------------------------*
2570 *	usbd_transfer_dequeue
2571 *
2572 *  - This function is used to remove an USB transfer from a USB
2573 *  transfer queue.
2574 *
2575 *  - This function can be called multiple times in a row.
2576 *------------------------------------------------------------------------*/
2577void
2578usbd_transfer_dequeue(struct usb_xfer *xfer)
2579{
2580	struct usb_xfer_queue *pq;
2581
2582	pq = xfer->wait_queue;
2583	if (pq) {
2584		TAILQ_REMOVE(&pq->head, xfer, wait_entry);
2585		xfer->wait_queue = NULL;
2586	}
2587}
2588
2589/*------------------------------------------------------------------------*
2590 *	usbd_transfer_enqueue
2591 *
2592 *  - This function is used to insert an USB transfer into a USB *
2593 *  transfer queue.
2594 *
2595 *  - This function can be called multiple times in a row.
2596 *------------------------------------------------------------------------*/
2597void
2598usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
2599{
2600	/*
2601	 * Insert the USB transfer into the queue, if it is not
2602	 * already on a USB transfer queue:
2603	 */
2604	if (xfer->wait_queue == NULL) {
2605		xfer->wait_queue = pq;
2606		TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry);
2607	}
2608}
2609
2610/*------------------------------------------------------------------------*
2611 *	usbd_transfer_done
2612 *
2613 *  - This function is used to remove an USB transfer from the busdma,
2614 *  pipe or interrupt queue.
2615 *
2616 *  - This function is used to queue the USB transfer on the done
2617 *  queue.
2618 *
2619 *  - This function is used to stop any USB transfer timeouts.
2620 *------------------------------------------------------------------------*/
2621void
2622usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error)
2623{
2624	struct usb_xfer_root *info = xfer->xroot;
2625
2626	USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED);
2627
2628	DPRINTF("err=%s\n", usbd_errstr(error));
2629
2630	/*
2631	 * If we are not transferring then just return.
2632	 * This can happen during transfer cancel.
2633	 */
2634	if (!xfer->flags_int.transferring) {
2635		DPRINTF("not transferring\n");
2636		/* end of control transfer, if any */
2637		xfer->flags_int.control_act = 0;
2638		return;
2639	}
2640	/* only set transfer error, if not already set */
2641	if (xfer->error == USB_ERR_NORMAL_COMPLETION)
2642		xfer->error = error;
2643
2644	/* stop any callouts */
2645	usb_callout_stop(&xfer->timeout_handle);
2646
2647	/*
2648	 * If we are waiting on a queue, just remove the USB transfer
2649	 * from the queue, if any. We should have the required locks
2650	 * locked to do the remove when this function is called.
2651	 */
2652	usbd_transfer_dequeue(xfer);
2653
2654#if USB_HAVE_BUSDMA
2655	if (mtx_owned(info->xfer_mtx)) {
2656		struct usb_xfer_queue *pq;
2657
2658		/*
2659		 * If the private USB lock is not locked, then we assume
2660		 * that the BUS-DMA load stage has been passed:
2661		 */
2662		pq = &info->dma_q;
2663
2664		if (pq->curr == xfer) {
2665			/* start the next BUS-DMA load, if any */
2666			usb_command_wrapper(pq, NULL);
2667		}
2668	}
2669#endif
2670	/* keep some statistics */
2671	if (xfer->error == USB_ERR_CANCELLED) {
2672		info->udev->stats_cancelled.uds_requests
2673		    [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2674	} else if (xfer->error != USB_ERR_NORMAL_COMPLETION) {
2675		info->udev->stats_err.uds_requests
2676		    [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2677	} else {
2678		info->udev->stats_ok.uds_requests
2679		    [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++;
2680	}
2681
2682	/* call the USB transfer callback */
2683	usbd_callback_ss_done_defer(xfer);
2684}
2685
2686/*------------------------------------------------------------------------*
2687 *	usbd_transfer_start_cb
2688 *
2689 * This function is called to start the USB transfer when
2690 * "xfer->interval" is greater than zero, and and the endpoint type is
2691 * BULK or CONTROL.
2692 *------------------------------------------------------------------------*/
2693static void
2694usbd_transfer_start_cb(void *arg)
2695{
2696	struct usb_xfer *xfer = arg;
2697	struct usb_endpoint *ep = xfer->endpoint;
2698
2699	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2700
2701	DPRINTF("start\n");
2702
2703#if USB_HAVE_PF
2704	usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2705#endif
2706
2707	/* the transfer can now be cancelled */
2708	xfer->flags_int.can_cancel_immed = 1;
2709
2710	/* start USB transfer, if no error */
2711	if (xfer->error == 0)
2712		(ep->methods->start) (xfer);
2713
2714	/* check for transfer error */
2715	if (xfer->error) {
2716		/* some error has happened */
2717		usbd_transfer_done(xfer, 0);
2718	}
2719}
2720
2721/*------------------------------------------------------------------------*
2722 *	usbd_xfer_set_stall
2723 *
2724 * This function is used to set the stall flag outside the
2725 * callback. This function is NULL safe.
2726 *------------------------------------------------------------------------*/
2727void
2728usbd_xfer_set_stall(struct usb_xfer *xfer)
2729{
2730	if (xfer == NULL) {
2731		/* tearing down */
2732		return;
2733	}
2734	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2735
2736	/* avoid any races by locking the USB mutex */
2737	USB_BUS_LOCK(xfer->xroot->bus);
2738	xfer->flags.stall_pipe = 1;
2739	USB_BUS_UNLOCK(xfer->xroot->bus);
2740}
2741
2742int
2743usbd_xfer_is_stalled(struct usb_xfer *xfer)
2744{
2745	return (xfer->endpoint->is_stalled);
2746}
2747
2748/*------------------------------------------------------------------------*
2749 *	usbd_transfer_clear_stall
2750 *
2751 * This function is used to clear the stall flag outside the
2752 * callback. This function is NULL safe.
2753 *------------------------------------------------------------------------*/
2754void
2755usbd_transfer_clear_stall(struct usb_xfer *xfer)
2756{
2757	if (xfer == NULL) {
2758		/* tearing down */
2759		return;
2760	}
2761	USB_XFER_LOCK_ASSERT(xfer, MA_OWNED);
2762
2763	/* avoid any races by locking the USB mutex */
2764	USB_BUS_LOCK(xfer->xroot->bus);
2765
2766	xfer->flags.stall_pipe = 0;
2767
2768	USB_BUS_UNLOCK(xfer->xroot->bus);
2769}
2770
2771/*------------------------------------------------------------------------*
2772 *	usbd_pipe_start
2773 *
2774 * This function is used to add an USB transfer to the pipe transfer list.
2775 *------------------------------------------------------------------------*/
2776void
2777usbd_pipe_start(struct usb_xfer_queue *pq)
2778{
2779	struct usb_endpoint *ep;
2780	struct usb_xfer *xfer;
2781	uint8_t type;
2782
2783	xfer = pq->curr;
2784	ep = xfer->endpoint;
2785
2786	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2787
2788	/*
2789	 * If the endpoint is already stalled we do nothing !
2790	 */
2791	if (ep->is_stalled) {
2792		return;
2793	}
2794	/*
2795	 * Check if we are supposed to stall the endpoint:
2796	 */
2797	if (xfer->flags.stall_pipe) {
2798		struct usb_device *udev;
2799		struct usb_xfer_root *info;
2800
2801		/* clear stall command */
2802		xfer->flags.stall_pipe = 0;
2803
2804		/* get pointer to USB device */
2805		info = xfer->xroot;
2806		udev = info->udev;
2807
2808		/*
2809		 * Only stall BULK and INTERRUPT endpoints.
2810		 */
2811		type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2812		if ((type == UE_BULK) ||
2813		    (type == UE_INTERRUPT)) {
2814			uint8_t did_stall;
2815
2816			did_stall = 1;
2817
2818			if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2819				(udev->bus->methods->set_stall) (
2820				    udev, ep, &did_stall);
2821			} else if (udev->ctrl_xfer[1]) {
2822				info = udev->ctrl_xfer[1]->xroot;
2823				usb_proc_msignal(
2824				    USB_BUS_CS_PROC(info->bus),
2825				    &udev->cs_msg[0], &udev->cs_msg[1]);
2826			} else {
2827				/* should not happen */
2828				DPRINTFN(0, "No stall handler\n");
2829			}
2830			/*
2831			 * Check if we should stall. Some USB hardware
2832			 * handles set- and clear-stall in hardware.
2833			 */
2834			if (did_stall) {
2835				/*
2836				 * The transfer will be continued when
2837				 * the clear-stall control endpoint
2838				 * message is received.
2839				 */
2840				ep->is_stalled = 1;
2841				return;
2842			}
2843		} else if (type == UE_ISOCHRONOUS) {
2844
2845			/*
2846			 * Make sure any FIFO overflow or other FIFO
2847			 * error conditions go away by resetting the
2848			 * endpoint FIFO through the clear stall
2849			 * method.
2850			 */
2851			if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2852				(udev->bus->methods->clear_stall) (udev, ep);
2853			}
2854		}
2855	}
2856	/* Set or clear stall complete - special case */
2857	if (xfer->nframes == 0) {
2858		/* we are complete */
2859		xfer->aframes = 0;
2860		usbd_transfer_done(xfer, 0);
2861		return;
2862	}
2863	/*
2864	 * Handled cases:
2865	 *
2866	 * 1) Start the first transfer queued.
2867	 *
2868	 * 2) Re-start the current USB transfer.
2869	 */
2870	/*
2871	 * Check if there should be any
2872	 * pre transfer start delay:
2873	 */
2874	if (xfer->interval > 0) {
2875		type = (ep->edesc->bmAttributes & UE_XFERTYPE);
2876		if ((type == UE_BULK) ||
2877		    (type == UE_CONTROL)) {
2878			usbd_transfer_timeout_ms(xfer,
2879			    &usbd_transfer_start_cb,
2880			    xfer->interval);
2881			return;
2882		}
2883	}
2884	DPRINTF("start\n");
2885
2886#if USB_HAVE_PF
2887	usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT);
2888#endif
2889	/* the transfer can now be cancelled */
2890	xfer->flags_int.can_cancel_immed = 1;
2891
2892	/* start USB transfer, if no error */
2893	if (xfer->error == 0)
2894		(ep->methods->start) (xfer);
2895
2896	/* check for transfer error */
2897	if (xfer->error) {
2898		/* some error has happened */
2899		usbd_transfer_done(xfer, 0);
2900	}
2901}
2902
2903/*------------------------------------------------------------------------*
2904 *	usbd_transfer_timeout_ms
2905 *
2906 * This function is used to setup a timeout on the given USB
2907 * transfer. If the timeout has been deferred the callback given by
2908 * "cb" will get called after "ms" milliseconds.
2909 *------------------------------------------------------------------------*/
2910void
2911usbd_transfer_timeout_ms(struct usb_xfer *xfer,
2912    void (*cb) (void *arg), usb_timeout_t ms)
2913{
2914	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2915
2916	/* defer delay */
2917	usb_callout_reset(&xfer->timeout_handle,
2918	    USB_MS_TO_TICKS(ms) + USB_CALLOUT_ZERO_TICKS, cb, xfer);
2919}
2920
2921/*------------------------------------------------------------------------*
2922 *	usbd_callback_wrapper_sub
2923 *
2924 *  - This function will update variables in an USB transfer after
2925 *  that the USB transfer is complete.
2926 *
2927 *  - This function is used to start the next USB transfer on the
2928 *  ep transfer queue, if any.
2929 *
2930 * NOTE: In some special cases the USB transfer will not be removed from
2931 * the pipe queue, but remain first. To enforce USB transfer removal call
2932 * this function passing the error code "USB_ERR_CANCELLED".
2933 *
2934 * Return values:
2935 * 0: Success.
2936 * Else: The callback has been deferred.
2937 *------------------------------------------------------------------------*/
2938static uint8_t
2939usbd_callback_wrapper_sub(struct usb_xfer *xfer)
2940{
2941	struct usb_endpoint *ep;
2942	struct usb_bus *bus;
2943	usb_frcount_t x;
2944
2945	bus = xfer->xroot->bus;
2946
2947	if ((!xfer->flags_int.open) &&
2948	    (!xfer->flags_int.did_close)) {
2949		DPRINTF("close\n");
2950		USB_BUS_LOCK(bus);
2951		(xfer->endpoint->methods->close) (xfer);
2952		USB_BUS_UNLOCK(bus);
2953		/* only close once */
2954		xfer->flags_int.did_close = 1;
2955		return (1);		/* wait for new callback */
2956	}
2957	/*
2958	 * If we have a non-hardware induced error we
2959	 * need to do the DMA delay!
2960	 */
2961	if (xfer->error != 0 && !xfer->flags_int.did_dma_delay &&
2962	    (xfer->error == USB_ERR_CANCELLED ||
2963	    xfer->error == USB_ERR_TIMEOUT ||
2964	    bus->methods->start_dma_delay != NULL)) {
2965
2966		usb_timeout_t temp;
2967
2968		/* only delay once */
2969		xfer->flags_int.did_dma_delay = 1;
2970
2971		/* we can not cancel this delay */
2972		xfer->flags_int.can_cancel_immed = 0;
2973
2974		temp = usbd_get_dma_delay(xfer->xroot->udev);
2975
2976		DPRINTFN(3, "DMA delay, %u ms, "
2977		    "on %p\n", temp, xfer);
2978
2979		if (temp != 0) {
2980			USB_BUS_LOCK(bus);
2981			/*
2982			 * Some hardware solutions have dedicated
2983			 * events when it is safe to free DMA'ed
2984			 * memory. For the other hardware platforms we
2985			 * use a static delay.
2986			 */
2987			if (bus->methods->start_dma_delay != NULL) {
2988				(bus->methods->start_dma_delay) (xfer);
2989			} else {
2990				usbd_transfer_timeout_ms(xfer,
2991				    (void (*)(void *))&usb_dma_delay_done_cb,
2992				    temp);
2993			}
2994			USB_BUS_UNLOCK(bus);
2995			return (1);	/* wait for new callback */
2996		}
2997	}
2998	/* check actual number of frames */
2999	if (xfer->aframes > xfer->nframes) {
3000		if (xfer->error == 0) {
3001			panic("%s: actual number of frames, %d, is "
3002			    "greater than initial number of frames, %d\n",
3003			    __FUNCTION__, xfer->aframes, xfer->nframes);
3004		} else {
3005			/* just set some valid value */
3006			xfer->aframes = xfer->nframes;
3007		}
3008	}
3009	/* compute actual length */
3010	xfer->actlen = 0;
3011
3012	for (x = 0; x != xfer->aframes; x++) {
3013		xfer->actlen += xfer->frlengths[x];
3014	}
3015
3016	/*
3017	 * Frames that were not transferred get zero actual length in
3018	 * case the USB device driver does not check the actual number
3019	 * of frames transferred, "xfer->aframes":
3020	 */
3021	for (; x < xfer->nframes; x++) {
3022		usbd_xfer_set_frame_len(xfer, x, 0);
3023	}
3024
3025	/* check actual length */
3026	if (xfer->actlen > xfer->sumlen) {
3027		if (xfer->error == 0) {
3028			panic("%s: actual length, %d, is greater than "
3029			    "initial length, %d\n",
3030			    __FUNCTION__, xfer->actlen, xfer->sumlen);
3031		} else {
3032			/* just set some valid value */
3033			xfer->actlen = xfer->sumlen;
3034		}
3035	}
3036	DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n",
3037	    xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen,
3038	    xfer->aframes, xfer->nframes);
3039
3040	if (xfer->error) {
3041		/* end of control transfer, if any */
3042		xfer->flags_int.control_act = 0;
3043
3044#if USB_HAVE_TT_SUPPORT
3045		switch (xfer->error) {
3046		case USB_ERR_NORMAL_COMPLETION:
3047		case USB_ERR_SHORT_XFER:
3048		case USB_ERR_STALLED:
3049		case USB_ERR_CANCELLED:
3050			/* nothing to do */
3051			break;
3052		default:
3053			/* try to reset the TT, if any */
3054			USB_BUS_LOCK(bus);
3055			uhub_tt_buffer_reset_async_locked(xfer->xroot->udev, xfer->endpoint);
3056			USB_BUS_UNLOCK(bus);
3057			break;
3058		}
3059#endif
3060		/* check if we should block the execution queue */
3061		if ((xfer->error != USB_ERR_CANCELLED) &&
3062		    (xfer->flags.pipe_bof)) {
3063			DPRINTFN(2, "xfer=%p: Block On Failure "
3064			    "on endpoint=%p\n", xfer, xfer->endpoint);
3065			goto done;
3066		}
3067	} else {
3068		/* check for short transfers */
3069		if (xfer->actlen < xfer->sumlen) {
3070
3071			/* end of control transfer, if any */
3072			xfer->flags_int.control_act = 0;
3073
3074			if (!xfer->flags_int.short_xfer_ok) {
3075				xfer->error = USB_ERR_SHORT_XFER;
3076				if (xfer->flags.pipe_bof) {
3077					DPRINTFN(2, "xfer=%p: Block On Failure on "
3078					    "Short Transfer on endpoint %p.\n",
3079					    xfer, xfer->endpoint);
3080					goto done;
3081				}
3082			}
3083		} else {
3084			/*
3085			 * Check if we are in the middle of a
3086			 * control transfer:
3087			 */
3088			if (xfer->flags_int.control_act) {
3089				DPRINTFN(5, "xfer=%p: Control transfer "
3090				    "active on endpoint=%p\n", xfer, xfer->endpoint);
3091				goto done;
3092			}
3093		}
3094	}
3095
3096	ep = xfer->endpoint;
3097
3098	/*
3099	 * If the current USB transfer is completing we need to start the
3100	 * next one:
3101	 */
3102	USB_BUS_LOCK(bus);
3103	if (ep->endpoint_q[xfer->stream_id].curr == xfer) {
3104		usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL);
3105
3106		if (ep->endpoint_q[xfer->stream_id].curr != NULL ||
3107		    TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) {
3108			/* there is another USB transfer waiting */
3109		} else {
3110			/* this is the last USB transfer */
3111			/* clear isochronous sync flag */
3112			xfer->endpoint->is_synced = 0;
3113		}
3114	}
3115	USB_BUS_UNLOCK(bus);
3116done:
3117	return (0);
3118}
3119
3120/*------------------------------------------------------------------------*
3121 *	usb_command_wrapper
3122 *
3123 * This function is used to execute commands non-recursivly on an USB
3124 * transfer.
3125 *------------------------------------------------------------------------*/
3126void
3127usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer)
3128{
3129	if (xfer) {
3130		/*
3131		 * If the transfer is not already processing,
3132		 * queue it!
3133		 */
3134		if (pq->curr != xfer) {
3135			usbd_transfer_enqueue(pq, xfer);
3136			if (pq->curr != NULL) {
3137				/* something is already processing */
3138				DPRINTFN(6, "busy %p\n", pq->curr);
3139				return;
3140			}
3141		}
3142	} else {
3143		/* Get next element in queue */
3144		pq->curr = NULL;
3145	}
3146
3147	if (!pq->recurse_1) {
3148
3149		/* clear third recurse flag */
3150		pq->recurse_3 = 0;
3151
3152		do {
3153			/* set two first recurse flags */
3154			pq->recurse_1 = 1;
3155			pq->recurse_2 = 1;
3156
3157			if (pq->curr == NULL) {
3158				xfer = TAILQ_FIRST(&pq->head);
3159				if (xfer) {
3160					TAILQ_REMOVE(&pq->head, xfer,
3161					    wait_entry);
3162					xfer->wait_queue = NULL;
3163					pq->curr = xfer;
3164				} else {
3165					break;
3166				}
3167			}
3168			DPRINTFN(6, "cb %p (enter)\n", pq->curr);
3169			(pq->command) (pq);
3170			DPRINTFN(6, "cb %p (leave)\n", pq->curr);
3171
3172			/*
3173			 * Set third recurse flag to indicate
3174			 * recursion happened:
3175			 */
3176			pq->recurse_3 = 1;
3177
3178		} while (!pq->recurse_2);
3179
3180		/* clear first recurse flag */
3181		pq->recurse_1 = 0;
3182
3183	} else {
3184		/* clear second recurse flag */
3185		pq->recurse_2 = 0;
3186	}
3187}
3188
3189/*------------------------------------------------------------------------*
3190 *	usbd_ctrl_transfer_setup
3191 *
3192 * This function is used to setup the default USB control endpoint
3193 * transfer.
3194 *------------------------------------------------------------------------*/
3195void
3196usbd_ctrl_transfer_setup(struct usb_device *udev)
3197{
3198	struct usb_xfer *xfer;
3199	uint8_t no_resetup;
3200	uint8_t iface_index;
3201
3202	/* check for root HUB */
3203	if (udev->parent_hub == NULL)
3204		return;
3205repeat:
3206
3207	xfer = udev->ctrl_xfer[0];
3208	if (xfer) {
3209		USB_XFER_LOCK(xfer);
3210		no_resetup =
3211		    ((xfer->address == udev->address) &&
3212		    (udev->ctrl_ep_desc.wMaxPacketSize[0] ==
3213		    udev->ddesc.bMaxPacketSize));
3214		if (udev->flags.usb_mode == USB_MODE_DEVICE) {
3215			if (no_resetup) {
3216				/*
3217				 * NOTE: checking "xfer->address" and
3218				 * starting the USB transfer must be
3219				 * atomic!
3220				 */
3221				usbd_transfer_start(xfer);
3222			}
3223		}
3224		USB_XFER_UNLOCK(xfer);
3225	} else {
3226		no_resetup = 0;
3227	}
3228
3229	if (no_resetup) {
3230		/*
3231	         * All parameters are exactly the same like before.
3232	         * Just return.
3233	         */
3234		return;
3235	}
3236	/*
3237	 * Update wMaxPacketSize for the default control endpoint:
3238	 */
3239	udev->ctrl_ep_desc.wMaxPacketSize[0] =
3240	    udev->ddesc.bMaxPacketSize;
3241
3242	/*
3243	 * Unsetup any existing USB transfer:
3244	 */
3245	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
3246
3247	/*
3248	 * Reset clear stall error counter.
3249	 */
3250	udev->clear_stall_errors = 0;
3251
3252	/*
3253	 * Try to setup a new USB transfer for the
3254	 * default control endpoint:
3255	 */
3256	iface_index = 0;
3257	if (usbd_transfer_setup(udev, &iface_index,
3258	    udev->ctrl_xfer, udev->bus->control_ep_quirk ?
3259	    usb_control_ep_quirk_cfg : usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL,
3260	    &udev->device_mtx)) {
3261		DPRINTFN(0, "could not setup default "
3262		    "USB transfer\n");
3263	} else {
3264		goto repeat;
3265	}
3266}
3267
3268/*------------------------------------------------------------------------*
3269 *	usbd_clear_data_toggle - factored out code
3270 *
3271 * NOTE: the intention of this function is not to reset the hardware
3272 * data toggle.
3273 *------------------------------------------------------------------------*/
3274void
3275usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep)
3276{
3277	USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3278
3279	/* check that we have a valid case */
3280	if (udev->flags.usb_mode == USB_MODE_HOST &&
3281	    udev->parent_hub != NULL &&
3282	    udev->bus->methods->clear_stall != NULL &&
3283	    ep->methods != NULL) {
3284		(udev->bus->methods->clear_stall) (udev, ep);
3285	}
3286}
3287
3288/*------------------------------------------------------------------------*
3289 *	usbd_clear_data_toggle - factored out code
3290 *
3291 * NOTE: the intention of this function is not to reset the hardware
3292 * data toggle on the USB device side.
3293 *------------------------------------------------------------------------*/
3294void
3295usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep)
3296{
3297	DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep);
3298
3299	USB_BUS_LOCK(udev->bus);
3300	ep->toggle_next = 0;
3301	/* some hardware needs a callback to clear the data toggle */
3302	usbd_clear_stall_locked(udev, ep);
3303	USB_BUS_UNLOCK(udev->bus);
3304}
3305
3306/*------------------------------------------------------------------------*
3307 *	usbd_clear_stall_callback - factored out clear stall callback
3308 *
3309 * Input parameters:
3310 *  xfer1: Clear Stall Control Transfer
3311 *  xfer2: Stalled USB Transfer
3312 *
3313 * This function is NULL safe.
3314 *
3315 * Return values:
3316 *   0: In progress
3317 *   Else: Finished
3318 *
3319 * Clear stall config example:
3320 *
3321 * static const struct usb_config my_clearstall =  {
3322 *	.type = UE_CONTROL,
3323 *	.endpoint = 0,
3324 *	.direction = UE_DIR_ANY,
3325 *	.interval = 50, //50 milliseconds
3326 *	.bufsize = sizeof(struct usb_device_request),
3327 *	.timeout = 1000, //1.000 seconds
3328 *	.callback = &my_clear_stall_callback, // **
3329 *	.usb_mode = USB_MODE_HOST,
3330 * };
3331 *
3332 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback"
3333 * passing the correct parameters.
3334 *------------------------------------------------------------------------*/
3335uint8_t
3336usbd_clear_stall_callback(struct usb_xfer *xfer1,
3337    struct usb_xfer *xfer2)
3338{
3339	struct usb_device_request req;
3340
3341	if (xfer2 == NULL) {
3342		/* looks like we are tearing down */
3343		DPRINTF("NULL input parameter\n");
3344		return (0);
3345	}
3346	USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED);
3347	USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED);
3348
3349	switch (USB_GET_STATE(xfer1)) {
3350	case USB_ST_SETUP:
3351
3352		/*
3353		 * pre-clear the data toggle to DATA0 ("umass.c" and
3354		 * "ata-usb.c" depends on this)
3355		 */
3356
3357		usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint);
3358
3359		/* setup a clear-stall packet */
3360
3361		req.bmRequestType = UT_WRITE_ENDPOINT;
3362		req.bRequest = UR_CLEAR_FEATURE;
3363		USETW(req.wValue, UF_ENDPOINT_HALT);
3364		req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress;
3365		req.wIndex[1] = 0;
3366		USETW(req.wLength, 0);
3367
3368		/*
3369		 * "usbd_transfer_setup_sub()" will ensure that
3370		 * we have sufficient room in the buffer for
3371		 * the request structure!
3372		 */
3373
3374		/* copy in the transfer */
3375
3376		usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req));
3377
3378		/* set length */
3379		xfer1->frlengths[0] = sizeof(req);
3380		xfer1->nframes = 1;
3381
3382		usbd_transfer_submit(xfer1);
3383		return (0);
3384
3385	case USB_ST_TRANSFERRED:
3386		break;
3387
3388	default:			/* Error */
3389		if (xfer1->error == USB_ERR_CANCELLED) {
3390			return (0);
3391		}
3392		break;
3393	}
3394	return (1);			/* Clear Stall Finished */
3395}
3396
3397/*------------------------------------------------------------------------*
3398 *	usbd_transfer_poll
3399 *
3400 * The following function gets called from the USB keyboard driver and
3401 * UMASS when the system has paniced.
3402 *
3403 * NOTE: It is currently not possible to resume normal operation on
3404 * the USB controller which has been polled, due to clearing of the
3405 * "up_dsleep" and "up_msleep" flags.
3406 *------------------------------------------------------------------------*/
3407void
3408usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max)
3409{
3410	struct usb_xfer *xfer;
3411	struct usb_xfer_root *xroot;
3412	struct usb_device *udev;
3413	struct usb_proc_msg *pm;
3414	uint16_t n;
3415	uint16_t drop_bus;
3416	uint16_t drop_xfer;
3417
3418	for (n = 0; n != max; n++) {
3419		/* Extra checks to avoid panic */
3420		xfer = ppxfer[n];
3421		if (xfer == NULL)
3422			continue;	/* no USB transfer */
3423		xroot = xfer->xroot;
3424		if (xroot == NULL)
3425			continue;	/* no USB root */
3426		udev = xroot->udev;
3427		if (udev == NULL)
3428			continue;	/* no USB device */
3429		if (udev->bus == NULL)
3430			continue;	/* no BUS structure */
3431		if (udev->bus->methods == NULL)
3432			continue;	/* no BUS methods */
3433		if (udev->bus->methods->xfer_poll == NULL)
3434			continue;	/* no poll method */
3435
3436		/* make sure that the BUS mutex is not locked */
3437		drop_bus = 0;
3438		while (mtx_owned(&xroot->udev->bus->bus_mtx) && !SCHEDULER_STOPPED()) {
3439			mtx_unlock(&xroot->udev->bus->bus_mtx);
3440			drop_bus++;
3441		}
3442
3443		/* make sure that the transfer mutex is not locked */
3444		drop_xfer = 0;
3445		while (mtx_owned(xroot->xfer_mtx) && !SCHEDULER_STOPPED()) {
3446			mtx_unlock(xroot->xfer_mtx);
3447			drop_xfer++;
3448		}
3449
3450		/* Make sure cv_signal() and cv_broadcast() is not called */
3451		USB_BUS_CONTROL_XFER_PROC(udev->bus)->up_msleep = 0;
3452		USB_BUS_EXPLORE_PROC(udev->bus)->up_msleep = 0;
3453		USB_BUS_GIANT_PROC(udev->bus)->up_msleep = 0;
3454		USB_BUS_NON_GIANT_ISOC_PROC(udev->bus)->up_msleep = 0;
3455		USB_BUS_NON_GIANT_BULK_PROC(udev->bus)->up_msleep = 0;
3456
3457		/* poll USB hardware */
3458		(udev->bus->methods->xfer_poll) (udev->bus);
3459
3460		USB_BUS_LOCK(xroot->bus);
3461
3462		/* check for clear stall */
3463		if (udev->ctrl_xfer[1] != NULL) {
3464
3465			/* poll clear stall start */
3466			pm = &udev->cs_msg[0].hdr;
3467			(pm->pm_callback) (pm);
3468			/* poll clear stall done thread */
3469			pm = &udev->ctrl_xfer[1]->
3470			    xroot->done_m[0].hdr;
3471			(pm->pm_callback) (pm);
3472		}
3473
3474		/* poll done thread */
3475		pm = &xroot->done_m[0].hdr;
3476		(pm->pm_callback) (pm);
3477
3478		USB_BUS_UNLOCK(xroot->bus);
3479
3480		/* restore transfer mutex */
3481		while (drop_xfer--)
3482			mtx_lock(xroot->xfer_mtx);
3483
3484		/* restore BUS mutex */
3485		while (drop_bus--)
3486			mtx_lock(&xroot->udev->bus->bus_mtx);
3487	}
3488}
3489
3490static void
3491usbd_get_std_packet_size(struct usb_std_packet_size *ptr,
3492    uint8_t type, enum usb_dev_speed speed)
3493{
3494	static const uint16_t intr_range_max[USB_SPEED_MAX] = {
3495		[USB_SPEED_LOW] = 8,
3496		[USB_SPEED_FULL] = 64,
3497		[USB_SPEED_HIGH] = 1024,
3498		[USB_SPEED_VARIABLE] = 1024,
3499		[USB_SPEED_SUPER] = 1024,
3500	};
3501
3502	static const uint16_t isoc_range_max[USB_SPEED_MAX] = {
3503		[USB_SPEED_LOW] = 0,	/* invalid */
3504		[USB_SPEED_FULL] = 1023,
3505		[USB_SPEED_HIGH] = 1024,
3506		[USB_SPEED_VARIABLE] = 3584,
3507		[USB_SPEED_SUPER] = 1024,
3508	};
3509
3510	static const uint16_t control_min[USB_SPEED_MAX] = {
3511		[USB_SPEED_LOW] = 8,
3512		[USB_SPEED_FULL] = 8,
3513		[USB_SPEED_HIGH] = 64,
3514		[USB_SPEED_VARIABLE] = 512,
3515		[USB_SPEED_SUPER] = 512,
3516	};
3517
3518	static const uint16_t bulk_min[USB_SPEED_MAX] = {
3519		[USB_SPEED_LOW] = 8,
3520		[USB_SPEED_FULL] = 8,
3521		[USB_SPEED_HIGH] = 512,
3522		[USB_SPEED_VARIABLE] = 512,
3523		[USB_SPEED_SUPER] = 1024,
3524	};
3525
3526	uint16_t temp;
3527
3528	memset(ptr, 0, sizeof(*ptr));
3529
3530	switch (type) {
3531	case UE_INTERRUPT:
3532		ptr->range.max = intr_range_max[speed];
3533		break;
3534	case UE_ISOCHRONOUS:
3535		ptr->range.max = isoc_range_max[speed];
3536		break;
3537	default:
3538		if (type == UE_BULK)
3539			temp = bulk_min[speed];
3540		else /* UE_CONTROL */
3541			temp = control_min[speed];
3542
3543		/* default is fixed */
3544		ptr->fixed[0] = temp;
3545		ptr->fixed[1] = temp;
3546		ptr->fixed[2] = temp;
3547		ptr->fixed[3] = temp;
3548
3549		if (speed == USB_SPEED_FULL) {
3550			/* multiple sizes */
3551			ptr->fixed[1] = 16;
3552			ptr->fixed[2] = 32;
3553			ptr->fixed[3] = 64;
3554		}
3555		if ((speed == USB_SPEED_VARIABLE) &&
3556		    (type == UE_BULK)) {
3557			/* multiple sizes */
3558			ptr->fixed[2] = 1024;
3559			ptr->fixed[3] = 1536;
3560		}
3561		break;
3562	}
3563}
3564
3565void	*
3566usbd_xfer_softc(struct usb_xfer *xfer)
3567{
3568	return (xfer->priv_sc);
3569}
3570
3571void *
3572usbd_xfer_get_priv(struct usb_xfer *xfer)
3573{
3574	return (xfer->priv_fifo);
3575}
3576
3577void
3578usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr)
3579{
3580	xfer->priv_fifo = ptr;
3581}
3582
3583uint8_t
3584usbd_xfer_state(struct usb_xfer *xfer)
3585{
3586	return (xfer->usb_state);
3587}
3588
3589void
3590usbd_xfer_set_flag(struct usb_xfer *xfer, int flag)
3591{
3592	switch (flag) {
3593		case USB_FORCE_SHORT_XFER:
3594			xfer->flags.force_short_xfer = 1;
3595			break;
3596		case USB_SHORT_XFER_OK:
3597			xfer->flags.short_xfer_ok = 1;
3598			break;
3599		case USB_MULTI_SHORT_OK:
3600			xfer->flags.short_frames_ok = 1;
3601			break;
3602		case USB_MANUAL_STATUS:
3603			xfer->flags.manual_status = 1;
3604			break;
3605	}
3606}
3607
3608void
3609usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag)
3610{
3611	switch (flag) {
3612		case USB_FORCE_SHORT_XFER:
3613			xfer->flags.force_short_xfer = 0;
3614			break;
3615		case USB_SHORT_XFER_OK:
3616			xfer->flags.short_xfer_ok = 0;
3617			break;
3618		case USB_MULTI_SHORT_OK:
3619			xfer->flags.short_frames_ok = 0;
3620			break;
3621		case USB_MANUAL_STATUS:
3622			xfer->flags.manual_status = 0;
3623			break;
3624	}
3625}
3626
3627/*
3628 * The following function returns in milliseconds when the isochronous
3629 * transfer was completed by the hardware. The returned value wraps
3630 * around 65536 milliseconds.
3631 */
3632uint16_t
3633usbd_xfer_get_timestamp(struct usb_xfer *xfer)
3634{
3635	return (xfer->isoc_time_complete);
3636}
3637
3638/*
3639 * The following function returns non-zero if the max packet size
3640 * field was clamped to a valid value. Else it returns zero.
3641 */
3642uint8_t
3643usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer)
3644{
3645	return (xfer->flags_int.maxp_was_clamped);
3646}
3647